Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrendershaderkeys_p.h
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#ifndef QSSG_RENDER_SHADER_KEY_H
6#define QSSG_RENDER_SHADER_KEY_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DUtils/private/qssgdataref_p.h>
20#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
21#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterial_p.h>
22#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
23
24QT_BEGIN_NAMESPACE
25// We have an ever expanding set of properties we like to hash into one or more 32 bit
26// quantities.
27// Furthermore we would like this set of properties to be convertable to string
28// So the shader cache file itself is somewhat human readable/diagnosable.
29// To do this we create a set of objects that act as properties to the master shader key.
30// These objects are tallied in order to figure out their actual offset into the shader key's
31// data store. They are also run through in order to create the string shader cache key.
32
33struct QSSGShaderKeyPropertyBase
34{
35 QByteArrayView name;
36 quint32 offset;
37 explicit constexpr QSSGShaderKeyPropertyBase(const char *inName = "") : name(inName), offset(0) {}
38 quint32 getOffset() const { return offset; }
39 void setOffset(quint32 of) { offset = of; }
40
41 template<quint32 TBitWidth>
42 quint32 getMaskTemplate() const
43 {
44 quint32 bit = offset % 32;
45 quint32 startValue = (1 << TBitWidth) - 1;
46 quint32 mask = startValue << bit;
47 return mask;
48 }
49
50 quint32 getIdx() const { return offset / 32; }
51
52protected:
53 void internalToString(QByteArray &ioStr, const QByteArrayView &inBuffer) const
54 {
55 ioStr.append(name);
56 ioStr.append('=');
57 ioStr.append(inBuffer);
58 }
59
60 static void internalToString(QByteArray &ioStr, const QByteArrayView &name, bool inValue)
61 {
62 if (inValue) {
63 ioStr.append(name);
64 ioStr.append('=');
65 ioStr.append(inValue ? QByteArrayView("true") : QByteArrayView("false"));
66 }
67 }
68 static bool getBoolValue(const QByteArray& str, const QByteArrayView &name)
69 {
70 const int index = str.indexOf(name);
71 if (index < 0)
72 return false;
73 const qsizetype nameLen = name.size();
74 if (str[index + nameLen] != '=')
75 return false;
76 if (str.mid(index + nameLen + 1, 4) == QByteArrayView("true"))
77 return true;
78 return false;
79 }
80};
81
82struct QSSGShaderKeyBoolean : public QSSGShaderKeyPropertyBase
83{
84 enum {
86 };
87
88 explicit constexpr QSSGShaderKeyBoolean(const char *inName = "") : QSSGShaderKeyPropertyBase(inName) {}
89
90 quint32 getMask() const { return getMaskTemplate<BitWidth>(); }
91 void setValue(QSSGDataRef<quint32> inDataStore, bool inValue) const
92 {
93 const qint32 idx = qint32(getIdx());
94 Q_ASSERT(idx >= 0 && idx <= INT32_MAX);
95 Q_ASSERT(inDataStore.size() > idx);
96 quint32 mask = getMask();
97 quint32 &target = inDataStore[idx];
98 if (inValue) {
99 target = target | mask;
100 } else {
101 mask = ~mask;
102 target = target & mask;
103 }
104 }
105
106 bool getValue(QSSGDataView<quint32> inDataStore) const
107 {
108 quint32 idx = getIdx();
109 quint32 mask = getMask();
110 const quint32 &target = inDataStore[idx];
111 return (target & mask) ? true : false;
112 }
113
114 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
115 {
116 bool isHigh = getValue(inKeySet);
117 internalToString(ioStr, name, isHigh);
118 }
119 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
120 {
121 setValue(inKeySet, getBoolValue(ioStr, name));
122 }
123};
124
125template<quint32 TBitWidth>
126struct QSSGShaderKeyUnsigned : public QSSGShaderKeyPropertyBase
127{
128 enum {
129 BitWidth = TBitWidth,
130 };
131 explicit constexpr QSSGShaderKeyUnsigned(const char *inName = "") : QSSGShaderKeyPropertyBase(inName) {}
132 quint32 getMask() const { return getMaskTemplate<BitWidth>(); }
133 void setValue(QSSGDataRef<quint32> inDataStore, quint32 inValue) const
134 {
135 quint32 startValue = (1 << TBitWidth) - 1;
136 // Ensure inValue is within range of bit width.
137 inValue = inValue & startValue;
138 quint32 bit = offset % 32;
139 quint32 mask = getMask();
140 quint32 idx = getIdx();
141 inValue = inValue << bit;
142 quint32 &target = inDataStore[idx];
143 // Get rid of existing value
144 quint32 inverseMask = ~mask;
145 target = target & inverseMask;
146 target = target | inValue;
147 }
148
149 quint32 getValue(QSSGDataView<quint32> inDataStore) const
150 {
151 quint32 idx = getIdx();
152 quint32 bit = offset % 32;
153 quint32 mask = getMask();
154 const quint32 &target = inDataStore[idx];
155
156 quint32 retval = target & mask;
157 retval = retval >> bit;
158 return retval;
159 }
160
161 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
162 {
163 quint32 value = getValue(inKeySet);
164 char buf[64];
165 memset(buf, 0, sizeof (buf));
166 toStr(value, toDataRef(buf, 64));
167 internalToString(ioStr, buf);
168 }
169
170 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
171 {
172 const qsizetype nameLen = name.size();
173 const qsizetype strOffset = ioStr.indexOf(name);
174 if (strOffset >= 0) {
175 /* The key is stored as name=val */
176 if (ioStr[strOffset + nameLen] != '=')
177 return;
178 const QByteArray s = ioStr.right(ioStr.size() - strOffset - nameLen - 1);
179 int i = 0;
180 while (QChar(QLatin1Char(s[i])).isDigit())
181 i++;
182 const quint32 value = s.left(i).toInt();
183 setValue(inKeySet, value);
184 }
185 }
186
187private:
188 static quint32 toStr(quint32 item, QSSGDataRef<char> buffer)
189 {
190 // hope the buffer is big enough...
191 return static_cast<quint32>(::snprintf(buffer.begin(), buffer.size(), "%u", item));
192 }
193};
194
196{
198 R = 0,
199 G = 1,
200 B = 2,
201 A = 3,
202 };
203 explicit QSSGShaderKeyTextureChannel(const char *inName = "") : QSSGShaderKeyUnsigned<2>(inName) {}
204
205 TexturChannelBits getTextureChannel(QSSGDataView<quint32> inKeySet) const
206 {
207 return TexturChannelBits(getValue(inKeySet));
208 }
209
210 void setTextureChannel(TexturChannelBits channel, QSSGDataRef<quint32> inKeySet)
211 {
212 setValue(inKeySet, quint32(channel));
213 }
214 static constexpr char textureChannelToChar[4] = {
215 'R',
216 'G',
217 'B',
218 'A'
219 };
220 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
221 {
222 ioStr.append(name);
223 ioStr.append('=');
224 ioStr.append(textureChannelToChar[getTextureChannel(inKeySet)]);
225 }
226 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
227 {
228 const qsizetype nameLen = name.size();
229 const qsizetype strOffset = ioStr.indexOf(name);
230 if (strOffset >= 0) {
231 /* The key is stored as name=ch */
232 if (ioStr[strOffset + nameLen] != '=')
233 return;
234 const char ch = ioStr[strOffset + nameLen + 1];
235 if (ch == 'R')
236 setValue(inKeySet, TexturChannelBits::R);
237 else if (ch == 'G')
238 setValue(inKeySet, TexturChannelBits::G);
239 else if (ch == 'B')
240 setValue(inKeySet, TexturChannelBits::B);
241 else if (ch == 'A')
242 setValue(inKeySet, TexturChannelBits::A);
243 }
244 }
245};
246
248{
250 Enabled = 1 << 0,
251 EnvMap = 1 << 1,
252 LightProbe = 1 << 2,
253 Identity = 1 << 3,
254 UsesUV1 = 1 << 4,
255 Linear = 1 << 5
256 };
257
258 explicit QSSGShaderKeyImageMap(const char *inName = "") : QSSGShaderKeyUnsigned<6>(inName) {}
259
260 bool getBitValue(ImageMapBits imageBit, QSSGDataView<quint32> inKeySet) const
261 {
262 return (getValue(inKeySet) & imageBit) ? true : false;
263 }
264
265 void setBitValue(ImageMapBits imageBit, bool inValue, QSSGDataRef<quint32> inKeySet)
266 {
267 quint32 theValue = getValue(inKeySet);
268 quint32 mask = imageBit;
269 if (inValue) {
270 theValue = theValue | mask;
271 } else {
272 mask = ~mask;
273 theValue = theValue & mask;
274 }
275 setValue(inKeySet, theValue);
276 }
277
278 bool isEnabled(QSSGDataView<quint32> inKeySet) const { return getBitValue(Enabled, inKeySet); }
279 void setEnabled(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(Enabled, val, inKeySet); }
280
281 bool isEnvMap(QSSGDataView<quint32> inKeySet) const { return getBitValue(EnvMap, inKeySet); }
282 void setEnvMap(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(EnvMap, val, inKeySet); }
283
284 bool isLightProbe(QSSGDataView<quint32> inKeySet) const { return getBitValue(LightProbe, inKeySet); }
285 void setLightProbe(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(LightProbe, val, inKeySet); }
286
287 bool isIdentityTransform(QSSGDataView<quint32> inKeySet) const { return getBitValue(Identity, inKeySet); }
288 void setIdentityTransform(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(Identity, val, inKeySet); }
289
290 bool isUsingUV1(QSSGDataView<quint32> inKeySet) const { return getBitValue(UsesUV1, inKeySet); }
291 void setUsesUV1(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(UsesUV1, val, inKeySet); }
292
293 bool isLinear(QSSGDataView<quint32> inKeySet) const { return getBitValue(Linear, inKeySet); }
294 void setLinear(QSSGDataRef<quint32> inKeySet, bool val) { setBitValue(Linear, val, inKeySet); }
295
296 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
297 {
298 ioStr.append(name);
299 ioStr.append(QByteArrayView("={"));
300 internalToString(ioStr, QByteArrayView("enabled"), isEnabled(inKeySet));
301 ioStr.append(';');
302 internalToString(ioStr, QByteArrayView("envMap"), isEnvMap(inKeySet));
303 ioStr.append(';');
304 internalToString(ioStr, QByteArrayView("lightProbe"), isLightProbe(inKeySet));
305 ioStr.append(';');
306 internalToString(ioStr, QByteArrayView("identity"), isIdentityTransform(inKeySet));
307 ioStr.append(';');
308 internalToString(ioStr, QByteArrayView("usesUV1"), isUsingUV1(inKeySet));
309 ioStr.append(';');
310 internalToString(ioStr, QByteArrayView("linear"), isLinear(inKeySet));
311 ioStr.append('}');
312 }
313};
314
316{
317 explicit QSSGShaderKeySpecularModel(const char *inName = "") : QSSGShaderKeyUnsigned<2>(inName) {}
318
319 void setSpecularModel(QSSGDataRef<quint32> inKeySet, QSSGRenderDefaultMaterial::MaterialSpecularModel inModel)
320 {
321 setValue(inKeySet, quint32(inModel));
322 }
323
325 {
326 return static_cast<QSSGRenderDefaultMaterial::MaterialSpecularModel>(getValue(inKeySet));
327 }
328
329 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
330 {
331 ioStr.append(name);
332 ioStr.append('=');
333 switch (getSpecularModel(inKeySet)) {
334 case QSSGRenderDefaultMaterial::MaterialSpecularModel::SchlickGGX:
335 ioStr.append(QByteArrayView("SchlickGGX"));
336 break;
337 case QSSGRenderDefaultMaterial::MaterialSpecularModel::BlinnPhong:
338 ioStr.append(QByteArrayView("BlinnPhong"));
339 break;
340 }
341 }
342 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
343 {
344 const qsizetype nameLen = name.size();
345 const int strOffset = ioStr.indexOf(name);
346 if (strOffset >= 0) {
347 /* The key is stored as name=specularMode; */
348 if (ioStr[strOffset + nameLen] != '=')
349 return;
350 const int codeOffsetBegin = strOffset + nameLen + 1;
351 int codeOffset = 0;
352 while (ioStr[codeOffsetBegin + codeOffset] != ';')
353 codeOffset++;
354 const QByteArray val = ioStr.mid(codeOffsetBegin, codeOffset);
355 if (val == QByteArrayView("SchlickGGX"))
356 setSpecularModel(inKeySet, QSSGRenderDefaultMaterial::MaterialSpecularModel::SchlickGGX);
357 if (val == QByteArrayView("BlinnPhong"))
358 setSpecularModel(inKeySet, QSSGRenderDefaultMaterial::MaterialSpecularModel::BlinnPhong);
359 }
360 }
361};
362
364{
365 explicit QSSGShaderKeyDiffuseModel(const char *inName = "") : QSSGShaderKeyUnsigned<2>(inName) {}
366
367 void setDiffuseModel(QSSGDataRef<quint32> inKeySet, QSSGRenderDefaultMaterial::MaterialDiffuseModel inModel)
368 {
369 setValue(inKeySet, quint32(inModel));
370 }
371
373 {
374 return static_cast<QSSGRenderDefaultMaterial::MaterialDiffuseModel>(getValue(inKeySet));
375 }
376
377 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
378 {
379 ioStr.append(name);
380 ioStr.append('=');
381 switch (getDiffuseModel(inKeySet)) {
382 case QSSGRenderDefaultMaterial::MaterialDiffuseModel::Burley:
383 ioStr.append(QByteArrayView("Burley"));
384 break;
385 case QSSGRenderDefaultMaterial::MaterialDiffuseModel::Lambert:
386 ioStr.append(QByteArrayView("Lambert"));
387 break;
388 case QSSGRenderDefaultMaterial::MaterialDiffuseModel::LambertWrap:
389 ioStr.append(QByteArrayView("LambertWrap"));
390 break;
391 }
392 }
393 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
394 {
395 const qsizetype nameLen = name.size();
396 const int strOffset = ioStr.indexOf(name);
397 if (strOffset >= 0) {
398 /* The key is stored as name=specularMode; */
399 if (ioStr[strOffset + nameLen] != '=')
400 return;
401 const int codeOffsetBegin = strOffset + nameLen + 1;
402 int codeOffset = 0;
403 while (ioStr[codeOffsetBegin + codeOffset] != ';')
404 codeOffset++;
405 const QByteArray val = ioStr.mid(codeOffsetBegin, codeOffset);
406 if (val == QByteArrayView("Burley"))
407 setDiffuseModel(inKeySet, QSSGRenderDefaultMaterial::MaterialDiffuseModel::Burley);
408 if (val == QByteArrayView("Lambert"))
409 setDiffuseModel(inKeySet, QSSGRenderDefaultMaterial::MaterialDiffuseModel::Lambert);
410 if (val == QByteArrayView("LambertWrap"))
411 setDiffuseModel(inKeySet, QSSGRenderDefaultMaterial::MaterialDiffuseModel::LambertWrap);
412 }
413 }
414};
415
417{
418 explicit QSSGShaderKeyAlphaMode(const char *inName = "") : QSSGShaderKeyUnsigned<2>(inName) {}
419
420 void setAlphaMode(QSSGDataRef<quint32> inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode inMode)
421 {
422 setValue(inKeySet, quint32(inMode));
423 }
424
425 QSSGRenderDefaultMaterial::MaterialAlphaMode getAlphaMode(QSSGDataView<quint32> inKeySet) const
426 {
427 return static_cast<QSSGRenderDefaultMaterial::MaterialAlphaMode>(getValue(inKeySet));
428 }
429
430 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
431 {
432 ioStr.append(name);
433 ioStr.append('=');
434 switch (getAlphaMode(inKeySet)) {
435 case QSSGRenderDefaultMaterial::MaterialAlphaMode::Default:
436 ioStr.append(QByteArrayView("Default"));
437 break;
438 case QSSGRenderDefaultMaterial::MaterialAlphaMode::Mask:
439 ioStr.append(QByteArrayView("Mask"));
440 break;
441 case QSSGRenderDefaultMaterial::MaterialAlphaMode::Blend:
442 ioStr.append(QByteArrayView("Blend"));
443 break;
444 case QSSGRenderDefaultMaterial::MaterialAlphaMode::Opaque:
445 ioStr.append(QByteArrayView("Opaque"));
446 break;
447 }
448 }
449 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
450 {
451 const qsizetype nameLen = name.size();
452 const qsizetype strOffset = ioStr.indexOf(name);
453 if (strOffset >= 0) {
454 /* The key is stored as name=alphaMode; */
455 if (ioStr[strOffset + nameLen] != '=')
456 return;
457 const int codeOffsetBegin = strOffset + nameLen + 1;
458 int codeOffset = 0;
459 while (ioStr[codeOffsetBegin + codeOffset] != ';')
460 codeOffset++;
461 const QByteArray val = ioStr.mid(codeOffsetBegin, codeOffset);
462 if (val == QByteArrayView("Default"))
463 setAlphaMode(inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode::Default);
464 if (val == QByteArrayView("Mask"))
465 setAlphaMode(inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode::Mask);
466 if (val == QByteArrayView("Blend"))
467 setAlphaMode(inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode::Blend);
468 if (val == QByteArrayView("Opaque"))
469 setAlphaMode(inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode::Opaque);
470 }
471 }
472};
473
475{
477 Position = 1 << 0,
478 Normal = 1 << 1,
479 TexCoord0 = 1 << 2,
480 TexCoord1 = 1 << 3,
481 Tangent = 1 << 4,
482 Binormal = 1 << 5,
483 Color = 1 << 6,
486 };
487
488 explicit QSSGShaderKeyVertexAttribute(const char *inName = "") : QSSGShaderKeyUnsigned<9>(inName) {}
489
490 bool getBitValue(VertexAttributeBits bit, QSSGDataView<quint32> inKeySet) const
491 {
492 return (getValue(inKeySet) & bit) ? true : false;
493 }
494 void setBitValue(VertexAttributeBits bit, QSSGDataRef<quint32> inKeySet, bool value) const
495 {
496 quint32 v = getValue(inKeySet);
497 v = value ? (v | bit) : (v & ~bit);
498 setValue(inKeySet, v);
499 }
500
501 void toString(QByteArray &ioStr, QSSGDataView<quint32> inKeySet) const
502 {
503 ioStr.append(name);
504 ioStr.append(QByteArrayView("={"));
505 internalToString(ioStr, QByteArrayView("position"), getBitValue(Position, inKeySet));
506 ioStr.append(';');
507 internalToString(ioStr, QByteArrayView("normal"), getBitValue(Normal, inKeySet));
508 ioStr.append(';');
509 internalToString(ioStr, QByteArrayView("texcoord0"), getBitValue(TexCoord0, inKeySet));
510 ioStr.append(';');
511 internalToString(ioStr, QByteArrayView("texcoord1"), getBitValue(TexCoord1, inKeySet));
512 ioStr.append(';');
513 internalToString(ioStr, QByteArrayView("tangent"), getBitValue(Tangent, inKeySet));
514 ioStr.append(';');
515 internalToString(ioStr, QByteArrayView("binormal"), getBitValue(Binormal, inKeySet));
516 ioStr.append(';');
517 internalToString(ioStr, QByteArrayView("color"), getBitValue(Color, inKeySet));
518 ioStr.append(';');
519 internalToString(ioStr, QByteArrayView("texcoordlightmap"), getBitValue(TexCoordLightmap, inKeySet));
520 ioStr.append(';');
521 internalToString(ioStr, QByteArrayView("joint&weight"), getBitValue(JointAndWeight, inKeySet));
522 ioStr.append('}');
523 }
524 void fromString(const QByteArray &ioStr, QSSGDataRef<quint32> inKeySet)
525 {
526 const qsizetype nameLen = name.size();
527 const qsizetype strOffset = ioStr.indexOf(name);
528 if (strOffset >= 0) {
529 /* The key is stored as name={;;;;;;} */
530 if (ioStr[strOffset + nameLen] != '=')
531 return;
532 if (ioStr[strOffset + nameLen + 1] != '{')
533 return;
534 const int codeOffsetBegin = strOffset + nameLen + 2;
535 int codeOffset = 0;
536 while (ioStr[codeOffsetBegin + codeOffset] != '}')
537 codeOffset++;
538 const QByteArray val = ioStr.mid(codeOffsetBegin, codeOffset);
539 const QVector<QByteArray> list = val.split(';');
540 if (list.size() != 8)
541 return;
542 setBitValue(Position, inKeySet, getBoolValue(list[0], QByteArrayView("position")));
543 setBitValue(Normal, inKeySet, getBoolValue(list[1], QByteArrayView("normal")));
544 setBitValue(TexCoord0, inKeySet, getBoolValue(list[2], QByteArrayView("texcoord0")));
545 setBitValue(TexCoord1, inKeySet, getBoolValue(list[3], QByteArrayView("texcoord1")));
546 setBitValue(Tangent, inKeySet, getBoolValue(list[4], QByteArrayView("tangent")));
547 setBitValue(Binormal, inKeySet, getBoolValue(list[5], QByteArrayView("binormal")));
548 setBitValue(Color, inKeySet, getBoolValue(list[6], QByteArrayView("color")));
549 setBitValue(TexCoordLightmap, inKeySet, getBoolValue(list[7], QByteArrayView("texcoordlightmap")));
550 }
551 }
552};
553
555{
556 enum {
558 };
598
653
655 : m_hasLighting("hasLighting")
656 , m_hasPunctualLights("hasPunctualLights")
657 , m_hasShadows("hasShadows")
658 , m_hasIbl("hasIbl")
659 , m_specularEnabled("specularEnabled")
660 , m_fresnelScaleBiasEnabled("fresnelScaleBiasEnabled")
661 , m_clearcoatFresnelScaleBiasEnabled("clearcoatFresnelScaleBiasEnabled")
662 , m_fresnelEnabled("fresnelEnabled")
663 , m_baseColorSingleChannelEnabled("baseColorSingleChannelEnabled")
664 , m_specularSingleChannelEnabled("specularSingleChannelEnabled")
665 , m_emissiveSingleChannelEnabled("emissiveSingleChannelEnabled")
666 , m_invertOpacityMapValue("invertOpacityMapValue")
667 , m_vertexColorsEnabled("vertexColorsEnabled")
668 , m_vertexColorsMaskEnabled("vertexColorsMaskEnabled")
669 , m_vertexColorRedMask("vertexColorRedMask")
670 , m_vertexColorGreenMask("vertexColorGreenMask")
671 , m_vertexColorBlueMask("vertexColorBlueMask")
672 , m_vertexColorAlphaMask("vertexColorAlphaMask")
673 , m_specularModel("specularModel")
674 , m_diffuseModel("diffuseModel")
675 , m_boneCount("boneCount")
676 , m_isDoubleSided("isDoubleSided")
677 , m_overridesPosition("overridesPosition")
678 , m_usesProjectionMatrix("usesProjectionMatrix")
679 , m_usesInverseProjectionMatrix("usesInverseProjectionMatrix")
680 , m_usesPointsTopology("usesPointsTopology")
681 , m_usesVarColor("usesVarColor")
682 , m_alphaMode("alphaMode")
683 , m_vertexAttributes("vertexAttributes")
684 , m_usesFloatJointIndices("usesFloatJointIndices")
685 , m_usesInstancing("usesInstancing")
686 , m_targetCount("targetCount")
687 , m_targetPositionOffset("targetPositionOffset")
688 , m_targetNormalOffset("targetNormalOffset")
689 , m_targetTangentOffset("targetTangentOffset")
690 , m_targetBinormalOffset("targetBinormalOffset")
691 , m_targetTexCoord0Offset("targetTexCoord0Offset")
692 , m_targetTexCoord1Offset("targetTexCoord1Offset")
693 , m_targetColorOffset("targetColorOffset")
694 , m_blendParticles("blendParticles")
695 , m_clearcoatEnabled("clearcoatEnabled")
696 , m_transmissionEnabled("transmissionEnabled")
697 , m_specularAAEnabled("specularAAEnabled")
698 , m_lightmapEnabled("lightmapEnabled")
699 , m_specularGlossyEnabled("specularGlossyEnabled")
700 , m_metallicRoughnessEnabled("metallicRoughnessEnabled")
701 , m_debugMode("debugMode")
702 , m_fogEnabled("fogEnabled")
703 , m_viewCount("viewCount")
704 , m_usesViewIndex("usesViewIndex")
705 , m_orderIndependentTransparency("orderIndependentTransparency")
706 {
707 m_imageMaps[0].name = "diffuseMap";
708 m_imageMaps[1].name = "emissiveMap";
709 m_imageMaps[2].name = "specularMap";
710 m_imageMaps[3].name = "baseColorMap";
711 m_imageMaps[4].name = "bumpMap";
712 m_imageMaps[5].name = "specularAmountMap";
713 m_imageMaps[6].name = "normalMap";
714 m_imageMaps[7].name = "clearcoatNormalMap";
715 m_imageMaps[8].name = "opacityMap";
716 m_imageMaps[9].name = "roughnessMap";
717 m_imageMaps[10].name = "metalnessMap";
718 m_imageMaps[11].name = "occlusionMap";
719 m_imageMaps[12].name = "translucencyMap";
720 m_imageMaps[13].name = "heightMap";
721 m_imageMaps[14].name = "clearcoatMap";
722 m_imageMaps[15].name = "clearcoatRoughnessMap";
723 m_imageMaps[16].name = "transmissionMap";
724 m_imageMaps[17].name = "thicknessMap";
725
726 m_textureChannels[0].name = "opacityMap_channel";
727 m_textureChannels[1].name = "roughnessMap_channel";
728 m_textureChannels[2].name = "metalnessMap_channel";
729 m_textureChannels[3].name = "occlusionMap_channel";
730 m_textureChannels[4].name = "translucencyMap_channel";
731 m_textureChannels[5].name = "heightMap_channel";
732 m_textureChannels[6].name = "clearcoatMap_channel";
733 m_textureChannels[7].name = "clearcoatRoughnessMap_channel";
734 m_textureChannels[8].name = "transmissionMap_channel";
735 m_textureChannels[9].name = "thicknessMap_channel";
736 m_textureChannels[10].name = "baseColorMap_channel";
737 m_textureChannels[11].name = "specularAmountMap_channel";
738 m_textureChannels[12].name = "emissiveMap_channel";
739
740 init();
741 }
742
743 template<typename TVisitor>
744 void visitProperties(TVisitor &inVisitor)
745 {
746 inVisitor.visit(m_hasLighting);
747 inVisitor.visit(m_hasPunctualLights);
748 inVisitor.visit(m_hasShadows);
749 inVisitor.visit(m_hasIbl);
750 inVisitor.visit(m_specularEnabled);
751 inVisitor.visit(m_fresnelEnabled);
752 inVisitor.visit(m_fresnelScaleBiasEnabled);
753 inVisitor.visit(m_clearcoatFresnelScaleBiasEnabled);
754 inVisitor.visit(m_baseColorSingleChannelEnabled);
755 inVisitor.visit(m_specularSingleChannelEnabled);
756 inVisitor.visit(m_emissiveSingleChannelEnabled);
757 inVisitor.visit(m_invertOpacityMapValue);
758 inVisitor.visit(m_vertexColorsEnabled);
759 inVisitor.visit(m_vertexColorsMaskEnabled);
760 inVisitor.visit(m_vertexColorRedMask);
761 inVisitor.visit(m_vertexColorGreenMask);
762 inVisitor.visit(m_vertexColorBlueMask);
763 inVisitor.visit(m_vertexColorAlphaMask);
764 inVisitor.visit(m_specularModel);
765 inVisitor.visit(m_diffuseModel);
766
767 for (quint32 idx = 0, end = ImageMapCount; idx < end; ++idx)
768 inVisitor.visit(m_imageMaps[idx]);
769
770 for (auto &textureChannel : m_textureChannels)
771 inVisitor.visit(textureChannel);
772
773 inVisitor.visit(m_boneCount);
774 inVisitor.visit(m_isDoubleSided);
775 inVisitor.visit(m_overridesPosition);
776 inVisitor.visit(m_usesProjectionMatrix);
777 inVisitor.visit(m_usesInverseProjectionMatrix);
778 inVisitor.visit(m_usesPointsTopology);
779 inVisitor.visit(m_usesVarColor);
780 inVisitor.visit(m_alphaMode);
781 inVisitor.visit(m_vertexAttributes);
782 inVisitor.visit(m_usesFloatJointIndices);
783 inVisitor.visit(m_usesInstancing);
784 inVisitor.visit(m_targetCount);
785 inVisitor.visit(m_targetPositionOffset);
786 inVisitor.visit(m_targetNormalOffset);
787 inVisitor.visit(m_targetTangentOffset);
788 inVisitor.visit(m_targetBinormalOffset);
789 inVisitor.visit(m_targetTexCoord0Offset);
790 inVisitor.visit(m_targetTexCoord1Offset);
791 inVisitor.visit(m_targetColorOffset);
792 inVisitor.visit(m_blendParticles);
793 inVisitor.visit(m_clearcoatEnabled);
794 inVisitor.visit(m_transmissionEnabled);
795 inVisitor.visit(m_specularAAEnabled);
796 inVisitor.visit(m_lightmapEnabled);
797 inVisitor.visit(m_specularGlossyEnabled);
798 inVisitor.visit(m_metallicRoughnessEnabled);
799 inVisitor.visit(m_debugMode);
800 inVisitor.visit(m_fogEnabled);
801 inVisitor.visit(m_viewCount);
802 inVisitor.visit(m_usesViewIndex);
803 inVisitor.visit(m_orderIndependentTransparency);
804 }
805
807 {
810 template<typename TPropType>
811 void visit(TPropType &inProp)
812 {
813 // if we cross the 32 bit border we just move
814 // to the next dword.
815 // This cost a few extra bits but prevents tedious errors like
816 // loosing shader key bits because they got moved beyond the 32 border
817 quint32 bit = m_offset % 32;
818 if (bit + TPropType::BitWidth > 32) {
819 m_offset += 32 - bit;
820 }
821
822 inProp.setOffset(m_offset);
823 m_offset += TPropType::BitWidth;
824 }
825 };
826
828 {
830 template<typename P>
831 constexpr void visit(const P &prop)
832 {
833 size += prop.name.size();
834 }
835 };
836
838 {
841
842 template<typename P>
843 void visit(P &prop)
844 {
845 offsetVisitor.visit(prop);
846 stringSizeVisitor.visit(prop);
847 }
848 };
849
850 void init()
851 {
852 InitVisitor visitor;
853 visitProperties(visitor);
854
855 // If this assert fires, then the default material key needs more bits.
856 Q_ASSERT(visitor.offsetVisitor.m_offset < 768);
857 // This is so we can do some guestimate of how big the string buffer needs
858 // to be to avoid doing a lot of allocations when concatenating the strings.
859 m_stringBufferSizeHint = visitor.stringSizeVisitor.size;
860 }
861};
862
864{
865 enum {
867 };
868 quint32 m_dataBuffer[DataBufferSize]; // 24 * 4 * 8 = 768 bits
870
872 {
873 for (size_t idx = 0; idx < DataBufferSize; ++idx)
874 m_dataBuffer[idx] = 0;
875 }
876
878 {
879 for (size_t idx = 0; idx < DataBufferSize; ++idx)
880 m_dataBuffer[idx] = 0;
881 }
882
883 size_t hash() const
884 {
885 size_t retval = 0;
886 for (size_t idx = 0; idx < DataBufferSize; ++idx)
887 retval = retval ^ qHash(m_dataBuffer[idx]);
888 return retval ^ m_featureSetHash;
889 }
890
891 bool operator==(const QSSGShaderDefaultMaterialKey &other) const
892 {
893 bool retval = true;
894 for (size_t idx = 0; idx < DataBufferSize && retval; ++idx)
895 retval = m_dataBuffer[idx] == other.m_dataBuffer[idx];
896 return retval && m_featureSetHash == other.m_featureSetHash;
897 }
898
899 // Cast operators to make getting properties easier.
902
904 {
908 template<typename TPropType>
909 void visit(const TPropType &prop)
910 {
912 if (m_str.size())
913 m_str.append(';');
915 // if the only thing we added was the semicolon
916 // then nuke the semicolon
917 if (originalSize && m_str.size() == (originalSize + 1))
919 }
920 };
921
923 {
926 StringInVisitor(const QByteArray &s, QSSGDataRef<quint32> ks) : m_str(s), m_keyStore(ks) {}
927
928 template<typename TPropType>
929 void visit(TPropType &prop)
930 {
931 prop.fromString(m_str, m_keyStore);
932 }
933 };
934
935 void toString(QByteArray &ioString, const QSSGShaderDefaultMaterialKeyProperties &inProperties) const
936 {
937 ioString.reserve(inProperties.m_stringBufferSizeHint);
938 StringVisitor theVisitor(ioString, *this);
939 const_cast<QSSGShaderDefaultMaterialKeyProperties &>(inProperties).visitProperties(theVisitor);
940 }
941 void fromString(QByteArray &ioString, QSSGShaderDefaultMaterialKeyProperties &inProperties)
942 {
943 StringInVisitor theVisitor(ioString, *this);
944 inProperties.visitProperties(theVisitor);
945 }
947 {
948 QByteArray ret;
949 ret.resize(sizeof(m_dataBuffer));
950 memcpy(ret.data(), m_dataBuffer, sizeof(m_dataBuffer));
951 return ret;
952 }
953 bool fromByteArray(const QByteArray &data) const
954 {
955 if (data.size() != sizeof(m_dataBuffer))
956 return false;
957 memcpy((void *)m_dataBuffer, data.data(), sizeof(m_dataBuffer));
958 return true;
959 }
960};
961
962Q_STATIC_ASSERT(std::is_trivially_destructible<QSSGShaderDefaultMaterialKey>::value);
963
964
966{
967 return key.hash();
968}
969
970QT_END_NAMESPACE
971
972#endif
size_t qHash(const QSSGShaderDefaultMaterialKey &key)
QSSGShaderKeyUnsigned< 8 > m_targetTexCoord1Offset
QSSGShaderKeyTextureChannel m_textureChannels[SingleChannelImageCount]
QSSGShaderKeyUnsigned< 16 > m_vertexColorAlphaMask
QSSGShaderKeyImageMap m_imageMaps[ImageMapCount]
QSSGShaderKeyUnsigned< 8 > m_targetPositionOffset
QSSGShaderKeyUnsigned< 8 > m_targetTexCoord0Offset
QSSGShaderKeyUnsigned< 16 > m_vertexColorBlueMask
QSSGShaderKeyVertexAttribute m_vertexAttributes
QSSGShaderKeyUnsigned< 8 > m_targetBinormalOffset
QSSGShaderKeyUnsigned< 16 > m_vertexColorGreenMask
QSSGShaderKeyUnsigned< 3 > m_orderIndependentTransparency
QSSGShaderKeyUnsigned< 16 > m_vertexColorRedMask
StringInVisitor(const QByteArray &s, QSSGDataRef< quint32 > ks)
void toString(QByteArray &ioString, const QSSGShaderDefaultMaterialKeyProperties &inProperties) const
quint32 m_dataBuffer[DataBufferSize]
QSSGShaderDefaultMaterialKey(size_t inFeatureSetHash)
bool operator==(const QSSGShaderDefaultMaterialKey &other) const
void fromString(QByteArray &ioString, QSSGShaderDefaultMaterialKeyProperties &inProperties)
bool fromByteArray(const QByteArray &data) const
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
QSSGRenderDefaultMaterial::MaterialAlphaMode getAlphaMode(QSSGDataView< quint32 > inKeySet) const
QSSGShaderKeyAlphaMode(const char *inName="")
void setAlphaMode(QSSGDataRef< quint32 > inKeySet, QSSGRenderDefaultMaterial::MaterialAlphaMode inMode)
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
void setValue(QSSGDataRef< quint32 > inDataStore, bool inValue) const
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
constexpr QSSGShaderKeyBoolean(const char *inName="")
bool getValue(QSSGDataView< quint32 > inDataStore) const
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
QSSGShaderKeyDiffuseModel(const char *inName="")
void setDiffuseModel(QSSGDataRef< quint32 > inKeySet, QSSGRenderDefaultMaterial::MaterialDiffuseModel inModel)
QSSGRenderDefaultMaterial::MaterialDiffuseModel getDiffuseModel(QSSGDataView< quint32 > inKeySet) const
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
void setIdentityTransform(QSSGDataRef< quint32 > inKeySet, bool val)
void setBitValue(ImageMapBits imageBit, bool inValue, QSSGDataRef< quint32 > inKeySet)
void setEnabled(QSSGDataRef< quint32 > inKeySet, bool val)
bool isUsingUV1(QSSGDataView< quint32 > inKeySet) const
QSSGShaderKeyImageMap(const char *inName="")
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
bool isIdentityTransform(QSSGDataView< quint32 > inKeySet) const
bool isEnabled(QSSGDataView< quint32 > inKeySet) const
bool isLightProbe(QSSGDataView< quint32 > inKeySet) const
void setLinear(QSSGDataRef< quint32 > inKeySet, bool val)
void setUsesUV1(QSSGDataRef< quint32 > inKeySet, bool val)
bool isEnvMap(QSSGDataView< quint32 > inKeySet) const
bool getBitValue(ImageMapBits imageBit, QSSGDataView< quint32 > inKeySet) const
void setLightProbe(QSSGDataRef< quint32 > inKeySet, bool val)
bool isLinear(QSSGDataView< quint32 > inKeySet) const
void setEnvMap(QSSGDataRef< quint32 > inKeySet, bool val)
QSSGRenderDefaultMaterial::MaterialSpecularModel getSpecularModel(QSSGDataView< quint32 > inKeySet) const
void setSpecularModel(QSSGDataRef< quint32 > inKeySet, QSSGRenderDefaultMaterial::MaterialSpecularModel inModel)
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
QSSGShaderKeySpecularModel(const char *inName="")
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
void setTextureChannel(TexturChannelBits channel, QSSGDataRef< quint32 > inKeySet)
static constexpr char textureChannelToChar[4]
TexturChannelBits getTextureChannel(QSSGDataView< quint32 > inKeySet) const
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
QSSGShaderKeyTextureChannel(const char *inName="")
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
constexpr QSSGShaderKeyUnsigned(const char *inName="")
void setValue(QSSGDataRef< quint32 > inDataStore, quint32 inValue) const
quint32 getValue(QSSGDataView< quint32 > inDataStore) const
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
void toString(QByteArray &ioStr, QSSGDataView< quint32 > inKeySet) const
bool getBitValue(VertexAttributeBits bit, QSSGDataView< quint32 > inKeySet) const
QSSGShaderKeyVertexAttribute(const char *inName="")
void fromString(const QByteArray &ioStr, QSSGDataRef< quint32 > inKeySet)
void setBitValue(VertexAttributeBits bit, QSSGDataRef< quint32 > inKeySet, bool value) const