-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinenode.cpp
More file actions
278 lines (239 loc) · 9.81 KB
/
Copy pathlinenode.cpp
File metadata and controls
278 lines (239 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "linenode_p.h"
#include <QtGui/QColor>
#include <QtQuick/QSGMaterial>
class LineShader : public QSGMaterialShader
{
public:
LineShader(Qt::PenJoinStyle joinStyle) : QSGMaterialShader() {
// Round joins/caps use a per-segment capsule shader; everything else uses the
// bevel (non-overlapping ribbon) shader. Both read the same uniform block and
// vertex attributes, so only updateUniformData below is shared.
const QString base = joinStyle == Qt::RoundJoin ? QStringLiteral(":/shaders/LineNodeRound")
: QStringLiteral(":/shaders/LineNodeBevel");
setShaderFileName(VertexStage, base + QStringLiteral(".vert.qsb"));
setShaderFileName(FragmentStage, base + QStringLiteral(".frag.qsb"));
}
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
};
// Set of helper functions that Qt ought to provide, but apparently doesn't
static int uniformBufferAppendFloat(QByteArray *buf, int offset, float f)
{
memcpy(buf->data() + offset, &f, sizeof(float));
return offset + sizeof(float);
}
static int uniformBufferAppendColor(QByteArray *buf, int offset, QColor c)
{
// Premultiplied alpha: the scene graph blends with (ONE, ONE_MINUS_SRC_ALPHA), and the
// frag shader scales the whole vec4 by coverage*qt_Opacity. So the colour's own alpha
// has to be baked into the RGB here, otherwise it wouldn't attenuate the colour at all
// (only qt_Opacity would appear to "work").
float a = float(c.alphaF());
float cv[4] = { float(c.redF()) * a,
float(c.greenF()) * a,
float(c.blueF()) * a,
a };
memcpy(buf->data() + offset, cv, 16);
return offset + 16;
}
static int uniformBufferAppendVec4(QByteArray *buf, int offset, float x, float y, float z, float w)
{
float v[4] = { x, y, z, w };
memcpy(buf->data() + offset, v, 16);
return offset + 16;
}
static int uniformBufferAppendMatrix4x4(QByteArray *buf, int offset, const QMatrix4x4 &m)
{
memcpy(buf->data() + offset, m.constData(), 64);
return offset + 64;
}
bool LineShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
{
QByteArray *buf = state.uniformData();
Q_ASSERT(buf->size() >= (16 + 4 + (4 * 3) + 7) * 4);
LineMaterial *mat = static_cast<LineMaterial *>(newMaterial);
const QMatrix4x4 &dt = mat->state.dataTransform;
int offset = 0;
offset = uniformBufferAppendMatrix4x4(buf, offset, state.combinedMatrix());
// dataScaleOffset: xy = the (diagonal) scale from real-world units to pixels,
// zw = the pixel offset (translation) stored in column 3 of dataTransform.
offset = uniformBufferAppendVec4(buf, offset, dt(0, 0), dt(1, 1), dt(0, 3), dt(1, 3));
offset = uniformBufferAppendColor(buf, offset, mat->state.color);
offset = uniformBufferAppendColor(buf, offset, mat->state.warningMinColor);
offset = uniformBufferAppendColor(buf, offset, mat->state.warningMaxColor);
offset = uniformBufferAppendFloat(buf, offset, state.opacity());
offset = uniformBufferAppendFloat(buf, offset, mat->state.height);
offset = uniformBufferAppendFloat(buf, offset, mat->state.lineWidth);
offset = uniformBufferAppendFloat(buf, offset, mat->state.warningMinValue);
offset = uniformBufferAppendFloat(buf, offset, mat->state.warningMaxValue);
offset = uniformBufferAppendFloat(buf, offset, mat->state.fillDirection);
offset = uniformBufferAppendFloat(buf, offset, mat->state.aa);
return true;
}
static const QSGGeometry::AttributeSet &attributes()
{
static QSGGeometry::Attribute attr[] = {
QSGGeometry::Attribute::createWithAttributeType(0, 4, QSGGeometry::FloatType, QSGGeometry::PositionAttribute),
QSGGeometry::Attribute::createWithAttributeType(1, 4, QSGGeometry::FloatType, QSGGeometry::UnknownAttribute)
};
static QSGGeometry::AttributeSet set = { 2, 8 * sizeof(float), attr };
return set;
}
LineMaterial::LineMaterial() : QSGMaterial()
{
setFlag(Blending);
}
QSGMaterialType *LineMaterial::type() const
{
// Distinct types per shader, so the renderer picks the right pipeline (and doesn't
// batch a round-join node together with a bevel-join one).
static QSGMaterialType roundType;
static QSGMaterialType bevelType;
return joinStyle == Qt::RoundJoin ? &roundType : &bevelType;
}
QSGMaterialShader *LineMaterial::createShader(QSGRendererInterface::RenderMode rm) const
{
Q_UNUSED(rm);
return new LineShader(joinStyle);
}
int LineMaterial::compare(const QSGMaterial *m) const
{
const LineMaterial *other = static_cast<const LineMaterial *>(m);
if (int diff = int(state.color.rgb()) - int(other->state.color.rgb()))
return diff;
if (int diff = int(state.warningMinColor.rgb()) - int(other->state.warningMinColor.rgb()))
return diff;
if (int diff = int(state.warningMaxColor.rgb()) - int(other->state.warningMaxColor.rgb()))
return diff;
if (int diff = state.height - other->state.height)
return diff;
if (int diff = state.lineWidth - other->state.lineWidth)
return diff;
if (int diff = state.warningMinValue - other->state.warningMinValue)
return diff;
if (int diff = state.warningMaxValue - other->state.warningMaxValue)
return diff;
if (int diff = state.fillDirection - other->state.fillDirection)
return diff;
if (int diff = state.aa - other->state.aa)
return diff;
return 0;
}
LineNode::LineNode()
: QSGGeometryNode()
, m_geometry(attributes(), 0)
, m_material(new LineMaterial)
{
qsgnode_set_description(this, QLatin1String("qqchart_linenode"));
setGeometry(&m_geometry);
m_material->setFlag(QSGMaterial::Blending);
setMaterial(m_material);
setFlag(OwnsMaterial);
}
void LineNode::updateGeometry(const QRectF &bounds, const QVector<LineVertex> *v, qreal timeScale)
{
float vscale = bounds.height() / (m_maxValue - m_minValue);
// TODO only adjust the x offset each frame
QMatrix4x4 &matrix = m_material->state.dataTransform;
matrix.setToIdentity();
matrix.scale(timeScale, -vscale);
matrix.translate(bounds.width() / timeScale - v->last().x, -m_maxValue);
markDirty(QSGNode::DirtyMaterial);
// Stroking draws each datapoint's 4 vertices as a standalone quad (two triangles) via an
// index buffer, so the per-segment capsules in the shader don't get joined by spurious
// strip triangles. Filling still wants the continuous ribbon, and wireframe just shows
// the raw vertices.
const int verticesPerSample = 4;
const bool filling = m_material->state.fillDirection != 0.f;
// Only the round-join shader wants standalone per-segment quads; the bevel shader (and
// fills) draw the continuous ribbon, and wireframe draws the raw vertices.
const bool indexed = m_material->joinStyle == Qt::RoundJoin && !m_wireframe && !filling;
const int samples = v->size() / verticesPerSample;
const int indexCount = indexed ? samples * 6 : 0;
m_geometry.setDrawingMode(m_wireframe ? QSGGeometry::DrawLineStrip
: indexed ? QSGGeometry::DrawTriangles // round: per-segment quads
: QSGGeometry::DrawTriangleStrip); // bevel stroke or fill: ribbon
if (v->size()) {
if (m_geometry.vertexCount() != v->size() || m_geometry.indexCount() != indexCount)
m_geometry.allocate(v->size(), indexCount);
// TODO limit on left side to stay in bounds
memcpy(m_geometry.vertexData(), v->constData(), sizeof(LineVertex) * v->size());
if (indexed) {
quint16 *idx = m_geometry.indexDataAsUShort();
for (int s = 0; s < samples; ++s) {
const quint16 b = quint16(s * verticesPerSample);
*idx++ = b; *idx++ = b + 1; *idx++ = b + 2; // corners prev-, prev+, cur-
*idx++ = b + 2; *idx++ = b + 1; *idx++ = b + 3; // corners cur-, prev+, cur+
}
}
markDirty(QSGNode::DirtyGeometry);
}
}
void LineNode::setHeight(float height)
{
m_material->state.height = height;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setLineWidth(float width)
{
m_material->state.lineWidth = width;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setColor(QColor color)
{
m_material->state.color = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMinColor(QColor color)
{
m_material->state.warningMinColor = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMaxColor(QColor color)
{
m_material->state.warningMaxColor = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setMinValue(qreal v)
{
m_minValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setMaxValue(qreal v)
{
m_maxValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMinValue(qreal v)
{
m_material->state.warningMinValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMaxValue(qreal v)
{
m_material->state.warningMaxValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setFillDirection(qreal v)
{
m_material->state.fillDirection = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setSpread(qreal v)
{
m_material->state.aa = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWireframe(bool v)
{
m_wireframe = v;
}
void LineNode::setJoinStyle(Qt::PenJoinStyle v)
{
if (m_material->joinStyle == v)
return;
m_material->joinStyle = v;
// Changes both the shader/pipeline (type()) and the geometry layout (indexed vs strip),
// so LineGraph flags both a property and a geometry change when this is set.
markDirty(QSGNode::DirtyMaterial);
markDirty(QSGNode::DirtyGeometry);
}