7#include <QtQuick3DRuntimeRender/private/qssgshadermaterialadapter_p.h>
9#include <ssg/qssgrenderextensions.h>
10#include <ssg/qssgrenderhelpers.h>
11#include <ssg/qssgrendercontextcore.h>
12#include <ssg/qquick3dextensionhelpers.h>
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
175
176
177
178
179
180
181
182
183
184
185
188
189
190
191
192
193
194
195
198
199
200
201
202
203
204
205
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
229static constexpr float g_vertexData[] = { -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
230 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f };
235void main()
236{
237 qt_inputUV = attr_uv;
238 gl_Position = vec4(attr_pos, 0.0, 1.0);
239}
240)";
243void main()
244{
245 qt_inputUV = attr_uv;
246 qt_inputUV.y = 1.0 - qt_inputUV.y;
247 gl_Position = vec4(attr_pos, 0.0, 1.0);
248}
249)";
252void main()
253{
254 qt_customMain();
255}
256)";
259void MAIN()
260{
261 FRAGCOLOR = vec4(1.0, 0.0, 1.0, 1.0);
262}
263)";
267 static const char *argKey =
"/*%QT_ARGS_MAIN%*/";
268 const int argKeyLen =
int(strlen(argKey));
269 const int argKeyPos = snippet.indexOf(argKey);
271 snippet = snippet.left(argKeyPos) + QByteArrayLiteral(
"inout vec3 VERTEX") + snippet.mid(argKeyPos + argKeyLen);
276QRhiTexture::Format rhiTextureFormatFromTextureDataFormat(QQuick3DTextureData::Format format)
279 case QQuick3DTextureData::RGBA8:
280 return QRhiTexture::Format::RGBA8;
281 case QQuick3DTextureData::RGBA16F:
282 return QRhiTexture::Format::RGBA16F;
283 case QQuick3DTextureData::RGBA32F:
284 return QRhiTexture::Format::RGBA32F;
285 case QQuick3DTextureData::RGBE8:
286 return QRhiTexture::Format::RGBA8;
287 case QQuick3DTextureData::R8:
288 return QRhiTexture::Format::R8;
289 case QQuick3DTextureData::R16:
290 return QRhiTexture::Format::R16;
291 case QQuick3DTextureData::R16F:
292 return QRhiTexture::Format::R16F;
293 case QQuick3DTextureData::R32F:
294 return QRhiTexture::Format::R32F;
296 return QRhiTexture::Format::RGBA8;
298 Q_UNREACHABLE_RETURN(QRhiTexture::Format::RGBA8);
301QByteArray generateFinalVertexShaderCode(QRhi *rhi,
302 QSSGShaderCustomMaterialAdapter::StringPairList uniforms,
303 QSSGShaderCustomMaterialAdapter::StringPairList varyings)
306 const bool doFlip = rhi->isYUpInNDC() && !rhi->isYUpInFramebuffer();
307 QByteArray vertexShader = doFlip ? mainVertexSnippetFlipped : mainVertexSnippet;
309 QByteArrayList metadata;
310 QSSGShaderCustomMaterialAdapter::ShaderCodeAndMetaData result;
312 QSSGShaderCustomMaterialAdapter::CustomShaderPrepWorkData scratch;
313 QSSGShaderCustomMaterialAdapter::beginPrepareCustomShader(&scratch, &result, vertexShader, QSSGShaderCache::ShaderType::Vertex,
false);
314 QSSGShaderCustomMaterialAdapter::finishPrepareCustomShader(&buf,
317 QSSGShaderCache::ShaderType::Vertex,
325 insertVertexMainArgs(result.first);
326 return result.first + buf;
329QByteArray generateFinalFragmentShaderCode(
const QByteArray &baseFragmentShaderCode,
330 QSSGShaderCustomMaterialAdapter::StringPairList uniforms,
331 QSSGShaderCustomMaterialAdapter::StringPairList varyings)
333 QSSGShaderCustomMaterialAdapter::ShaderCodeAndMetaData result;
335 QByteArrayList metadata;
336 QSSGShaderCustomMaterialAdapter::CustomShaderPrepWorkData scratch;
337 QSSGShaderCustomMaterialAdapter::beginPrepareCustomShader(&scratch, &result, baseFragmentShaderCode, QSSGShaderCache::ShaderType::Fragment,
false);
338 QSSGShaderCustomMaterialAdapter::finishPrepareCustomShader(&buf,
341 QSSGShaderCache::ShaderType::Fragment,
349 return result.first + buf;
352QSSGRhiShaderPipelinePtr compileShader(QSSGRenderContextInterface *sgContext,
353 const QByteArray &shaderPathKey,
354 const QByteArray &vertexShader,
355 const QByteArray &fragmentShader)
357 QSSGProgramGenerator *generator = sgContext->shaderProgramGenerator().get();
358 QSSGShaderLibraryManager *shaderLib = sgContext->shaderLibraryManager().get();
359 QSSGShaderCache *shaderCache = sgContext->shaderCache().get();
361 generator->beginProgram();
362 auto vertex = generator->getStage(QSSGShaderGeneratorStage::Vertex);
363 vertex->addIncoming(
"attr_pos",
"vec2");
364 vertex->addIncoming(
"attr_uv",
"vec2");
365 vertex->append(vertexShader);
367 generator->getStage(QSSGShaderGeneratorStage::Fragment)->append(fragmentShader);
369 QSSGShaderFeatures features;
370 const QByteArray key = shaderPathKey +
':'
371 + QCryptographicHash::hash(QByteArray(vertexShader + fragmentShader), QCryptographicHash::Algorithm::Sha1)
373 return generator->compileGeneratedRhiShader(key, features, *shaderLib, *shaderCache, QSSGRhiShaderPipeline::UsedWithoutIa, { }, 1,
false);
385 void render(QSSGFrameData &data)
override;
399 QPointer<QQuick3DQuadTextureProvider> m_ext;
401 QSSGRhiShaderResourceBindingList m_srbBindings;
402 QSSGRhiShaderPipelinePtr m_shaderPipeline;
404 std::unique_ptr<QRhiBuffer> m_vertexBuffer;
405 std::unique_ptr<QRhiBuffer> m_indexBuffer;
407 std::unique_ptr<QRhiTexture> m_outputTexture;
408 std::unique_ptr<QRhiTexture> m_outputTextureOld;
409 std::unique_ptr<QRhiTextureRenderTarget> m_renderTarget;
410 std::unique_ptr<QRhiRenderPassDescriptor> m_renderPassDesc;
412 std::unique_ptr<QRhiGraphicsPipeline> m_graphicsPipeline;
413 QRhiShaderResourceBindings* m_srb =
nullptr;
422 QSSGRenderContextInterface *ctxIfx = data.contextInterface();
423 auto bufferManager = ctxIfx->bufferManager().get();
424 QSSGRhiContext *rhiCtx = ctxIfx->rhiContext().get();
428 QSSGExtensionId extensionId = m_ext ? QQuick3DExtensionHelpers::getExtensionId(*m_ext) : QSSGExtensionId { };
429 if (QQuick3DExtensionHelpers::isNull(extensionId))
433 bool needsRebuild =
false;
434 if (dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Dimensions)
436 if (dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Format)
438 if (dirtyFlag & QQuick3DQuadTextureProvider::Dirty::FragmentShader)
440 if (m_shaderPipeline && !needsRebuild)
441 return dirtyFlag & QQuick3DQuadTextureProvider::Dirty::TrackedProperty;
443 if (!m_shaderPipeline || dirtyFlag & QQuick3DQuadTextureProvider::Dirty::FragmentShader) {
447 for (
const auto &u : std::as_const(propertyUniforms)) {
448 if (u.shaderDataType == QSSGRenderShaderValue::Texture) {
449 QSSGRenderImage *image = u.value.value<QSSGRenderImage *>();
450 const QSSGRenderImageTexture texture = image ? bufferManager->loadRenderImage(image) : QSSGRenderImageTexture { };
451 if (!image || !texture.m_texture) {
457 QByteArray fragmentShaderCode = (!fragmentShaderSource.isEmpty() ? fragmentShaderSource : fallbackFragmentShaderStr)
458 + mainFragmentSnippet;
460 QSSGShaderCustomMaterialAdapter::StringPairList baseUniforms;
461 for (
const auto &u : std::as_const(propertyUniforms))
462 baseUniforms.append({ u.typeName, u.name });
464 baseUniforms.append({
"vec2",
"qt_outputSize" });
466 QSSGShaderCustomMaterialAdapter::StringPairList baseInputOutputs;
467 baseInputOutputs.append({
"vec2",
"qt_inputUV" });
469 QByteArray vertexShader = generateFinalVertexShaderCode(rhiCtx->rhi(), baseUniforms, baseInputOutputs);
470 QByteArray fragmentShader = generateFinalFragmentShaderCode(fragmentShaderCode, baseUniforms, baseInputOutputs);
472 QSSGRenderContextInterface *sgContext = data.contextInterface();
474 m_shaderPipeline = compileShader(sgContext, shaderPathKey, vertexShader, fragmentShader);
477 if (!m_shaderPipeline) {
478 fragmentShaderCode = fallbackFragmentShaderStr + mainFragmentSnippet;
479 fragmentShader = generateFinalFragmentShaderCode(fragmentShaderCode, baseUniforms, baseInputOutputs);
480 m_shaderPipeline = compileShader(sgContext, shaderPathKey, vertexShader, fragmentShader);
484 QRhi *rhi = rhiCtx->rhi();
486 if (!m_outputTexture || dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Dimensions
487 || dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Format) {
499 m_outputTextureOld = std::move(m_outputTexture);
500 m_outputTexture.reset(rhi->newTexture(rhiTextureFormatFromTextureDataFormat(format), QSize(width, height), 1, QRhiTexture::RenderTarget));
501 m_outputTexture->create();
503 m_renderTarget.reset(rhi->newTextureRenderTarget({ m_outputTexture.get() }));
504 m_renderPassDesc.reset(m_renderTarget->newCompatibleRenderPassDescriptor());
505 m_renderTarget->setRenderPassDescriptor(m_renderPassDesc.get());
506 m_renderTarget->create();
508 QSSGRenderExtensionHelpers::registerRenderResult(data, extensionId, m_outputTexture.get());
510 m_graphicsPipeline.reset();
518 QSSGRenderContextInterface *ctxIfx = data.contextInterface();
519 QSSGRhiContext *rhiCtx = ctxIfx->rhiContext().get();
520 if (!rhiCtx || !m_shaderPipeline)
523 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
524 QRhi *rhi = rhiCtx->rhi();
526 QSSGRhiDrawCallData *dcd = &rhiCtxD->drawCallData({ (
void *)
this,
nullptr,
nullptr,
nullptr, 0 });
528 const int uniformStride = rhiCtx->rhi()->ubufAligned(m_shaderPipeline->ub0Size());
529 dcd->ubuf = rhiCtx->rhi()->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, uniformStride);
533 const bool trackedPropertyDirty = dirtyFlag & QQuick3DQuadTextureProvider::Dirty::TrackedProperty;
534 const bool dimensionsDirty = dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Dimensions;
535 const bool pipelineDirty = !m_graphicsPipeline || (dirtyFlag & QQuick3DQuadTextureProvider::Dirty::Format)
536 || (dirtyFlag & QQuick3DQuadTextureProvider::Dirty::FragmentShader);
538 const bool srbDirty = pipelineDirty || trackedPropertyDirty || !m_srb;
541 if (trackedPropertyDirty || dimensionsDirty) {
543 char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
545 if (trackedPropertyDirty) {
547 m_shaderPipeline->resetExtraTextures();
549 QSSGBufferManager *bufferManager = ctxIfx->bufferManager().get();
551 for (
const auto &u : std::as_const(propertyUniforms)) {
552 m_shaderPipeline->setShaderResources(ubufData, *bufferManager, u.name, u.value, u.shaderDataType);
556 if (dimensionsDirty) {
557 m_shaderPipeline->setUniformValue(ubufData,
"qt_outputSize", QVector2D(width, height), QSSGRenderShaderValue::Vec2);
559 dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame();
563 if (!m_vertexBuffer && !m_indexBuffer) {
565 m_vertexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, 5 * 4 *
sizeof(
float)));
566 m_vertexBuffer->create();
569 m_indexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, 6 *
sizeof(uint16_t)));
570 m_indexBuffer->create();
571 QRhiResourceUpdateBatch *updates = rhi->nextResourceUpdateBatch();
572 updates->uploadStaticBuffer(m_vertexBuffer.get(),
g_vertexData);
573 updates->uploadStaticBuffer(m_indexBuffer.get(), g_indexData);
574 rhiCtx->commandBuffer()->resourceUpdate(updates);
579 rhiCtxD->releaseCachedSrb(m_srbBindings);
580 m_srbBindings = QSSGRhiShaderResourceBindingList();
581 m_srbBindings.addUniformBuffer(0, QRhiShaderResourceBinding::FragmentStage, dcd->ubuf);
584 int maxSamplerBinding = -1;
585 QVector<QShaderDescription::InOutVariable> samplerVars = m_shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
586 for (
const QShaderDescription::InOutVariable &var :
587 m_shaderPipeline->vertexStage()->shader().description().combinedImageSamplers()) {
588 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(), [&var](
const QShaderDescription::InOutVariable &v) {
589 return var.binding == v.binding;
591 if (it == samplerVars.cend())
592 samplerVars.append(var);
594 for (
const QShaderDescription::InOutVariable &var : std::as_const(samplerVars))
595 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
597 if (maxSamplerBinding >= 0) {
599 int customTexCount = m_shaderPipeline->extraTextureCount();
600 for (
int i = 0; i < customTexCount; ++i) {
601 const QSSGRhiTexture &t(m_shaderPipeline->extraTextureAt(i));
602 const int samplerBinding = m_shaderPipeline->bindingForTexture(t.name);
603 if (samplerBinding >= 0) {
604 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
605 m_srbBindings.addTexture(samplerBinding, QRhiShaderResourceBinding::FragmentStage, t.texture, sampler);
610 m_srb = rhiCtxD->srb(m_srbBindings);
616 m_graphicsPipeline.reset(rhi->newGraphicsPipeline());
617 m_graphicsPipeline->setShaderStages({ *m_shaderPipeline->vertexStage(), *m_shaderPipeline->fragmentStage() });
620 QRhiVertexInputLayout inputLayout;
621 inputLayout.setBindings({ { 5 *
sizeof(
float) } });
622 inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
623 { 0, 1, QRhiVertexInputAttribute::Float2, 3 *
sizeof(
float) } });
624 m_graphicsPipeline->setVertexInputLayout(inputLayout);
625 m_graphicsPipeline->setShaderResourceBindings(m_srb);
626 m_graphicsPipeline->setRenderPassDescriptor(m_renderPassDesc.get());
628 m_graphicsPipeline->create();
636 const auto &ctxIfx = data.contextInterface();
637 const auto &rhiCtx = ctxIfx->rhiContext();
641 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
642 cb->debugMarkBegin(QByteArrayLiteral(
"Quick3D Quad Texture Provider"));
644 cb->beginPass(m_renderTarget.get(), Qt::transparent, { 1.0f, 0 },
nullptr, rhiCtx->commonPassFlags());
645 cb->setViewport(QRhiViewport(0, 0,
width,
height));
648 cb->setGraphicsPipeline(m_graphicsPipeline.get());
650 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx.get());
651 auto srb = rhiCtxD->srb(m_srbBindings);
652 cb->setShaderResources(srb);
654 QRhiCommandBuffer::VertexInput vb(m_vertexBuffer.get(), 0);
655 cb->setVertexInput(0, 1, &vb, m_indexBuffer.get(), QRhiCommandBuffer::IndexFormat::IndexUInt16);
664 m_outputTextureOld.reset();
668 : QQuick3DTextureProviderExtension(parent)
669 , QQuick3DPropertyChangedTracker(
this, QQuick3DSuperClassInfo<QQuick3DQuadTextureProvider>())
683 if (m_dirtyFlag & Dirty::Dimensions) {
685 n->height = m_height;
688 if (m_dirtyFlag & Dirty::Format)
689 n->format = m_format;
691 if (m_dirtyFlag & Dirty::FragmentShader) {
692 const QQmlContext *context = qmlContext(
this);
693 n->fragmentShaderSource = !m_fragmentShaderCode.isEmpty() ? m_fragmentShaderCode.toUtf8()
694 : !m_fragmentShader.isEmpty() ? QSSGShaderUtils::resolveShader(m_fragmentShader, context, n->shaderPathKey)
698 if (m_dirtyFlag & Dirty::TrackedProperty) {
699 n->propertyUniforms = extractProperties();
702 if (m_dirtyFlag != 0)
703 emit surfaceChanged();
705 n->dirtyFlag = m_dirtyFlag;
713 QQuick3DTextureProviderExtension::itemChange(change, value);
714 if (change == ItemSceneChange)
715 updateTrackedSceneManager(value.sceneManager);
722 markDirty(Dirty::TrackedProperty);
733 return m_fragmentShader;
738 if (m_fragmentShader == newFragmentShader)
740 m_fragmentShader = newFragmentShader;
741 emit fragmentShaderChanged();
742 markDirty(Dirty::FragmentShader);
747 return m_fragmentShaderCode;
752 if (m_fragmentShaderCode == newFragmentShaderCode)
754 m_fragmentShaderCode = newFragmentShaderCode;
755 emit fragmentShaderCodeChanged();
756 markDirty(Dirty::FragmentShader);
767 newWidth = qMax(1, newWidth);
768 if (m_width == newWidth)
772 markDirty(Dirty::Dimensions);
782 newHeight = qMax(1, newHeight);
783 if (m_height == newHeight)
785 m_height = newHeight;
786 emit heightChanged();
787 markDirty(Dirty::Dimensions);
797 if (m_format == newFormat)
799 m_format = newFormat;
800 emit formatChanged();
801 markDirty(Dirty::Format);
QSSGQuadTextureProvider(QQuick3DQuadTextureProvider *ext)
void render(QSSGFrameData &data) override
Record the render pass.
QList< QSSGBaseTypeProperty > propertyUniforms
QQuick3DQuadTextureProvider::DirtyT dirtyFlag
void prepareRender(QSSGFrameData &data) override
Prepare data for rendering.
QByteArray fragmentShaderSource
void resetForFrame() override
Called each time a new frame starts.
bool prepareData(QSSGFrameData &data) override
Called after scene data is collected, but before any render data or rendering in the current frame ha...
~QSSGQuadTextureProvider() override
QQuick3DTextureData::Format format
Combined button and popup list for selecting options.
static const QByteArray mainVertexSnippet
static const QByteArray mainFragmentSnippet
static const QByteArray fallbackFragmentShaderStr
static constexpr uint16_t g_indexData[]
static QT_BEGIN_NAMESPACE constexpr float g_vertexData[]
\qmltype QuadTextureProvider \nativetype QQuick3DQuadTextureProvider \inqmlmodule QtQuick3D....
static void insertVertexMainArgs(QByteArray &snippet)
static const QByteArray mainVertexSnippetFlipped