5#include <QtQuick3DRuntimeRender/private/qssgrhieffectsystem_p.h>
6#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
7#include <QtQuick3DRuntimeRender/private/qssgrhiquadrenderer_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h>
9#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
13#include <qtquick3d_tracepoints_p.h>
15#include <QtQuick3DUtils/private/qssgassert_p.h>
17#include <QtCore/qloggingcategory.h>
21Q_DECLARE_LOGGING_CATEGORY(lcEffectSystem);
37 delete renderPassDescriptor;
43QSSGRhiEffectSystem::QSSGRhiEffectSystem(
const std::shared_ptr<QSSGRenderContextInterface> &sgContext)
44 : m_sgContext(sgContext)
48QSSGRhiEffectSystem::~QSSGRhiEffectSystem()
53void QSSGRhiEffectSystem::setup(QSize outputSize)
55 if (outputSize.isEmpty()) {
59 m_outSize = outputSize;
62QSSGRhiEffectTexture *QSSGRhiEffectSystem::findTexture(
const QByteArray &bufferName)
64 auto findTexture = [bufferName](
const QSSGRhiEffectTexture *rt){
return rt->name == bufferName; };
65 const auto foundIt = std::find_if(m_textures.cbegin(), m_textures.cend(), findTexture);
66 QSSGRhiEffectTexture *result = foundIt == m_textures.cend() ?
nullptr : *foundIt;
70QSSGRhiEffectTexture *QSSGRhiEffectSystem::getTexture(
const QByteArray &bufferName,
72 QRhiTexture::Format format,
74 const QSSGRenderEffect *inEffect,
77 QSSGRhiEffectTexture *result = findTexture(bufferName);
78 const bool gotMatch = result !=
nullptr;
88 auto findUnused = [](
const QSSGRhiEffectTexture *rt){
return rt->name.isEmpty(); };
89 const auto found = std::find_if(m_textures.cbegin(), m_textures.cend(), findUnused);
90 if (found != m_textures.cend()) {
97 result =
new QSSGRhiEffectTexture {};
98 m_textures.append(result);
101 const auto &rhiCtx = m_sgContext->rhiContext();
102 QRhi *rhi = rhiCtx->rhi();
103 const bool formatChanged = result->texture && result->texture->format() != format;
104 const bool needsRebuild = result->texture && (result->texture->pixelSize() != size || formatChanged);
106 QRhiTexture::Flags flags = QRhiTexture::RenderTarget;
108 flags |= QRhiTexture::UsedAsTransferSource;
110 if (!result->texture) {
112 result->texture = rhi->newTextureArray(format, viewCount, size, 1, flags);
114 result->texture = rhi->newTexture(format, size, 1, flags);
115 result->texture->create();
116 }
else if (needsRebuild) {
117 result->texture->setFlags(flags);
118 result->texture->setPixelSize(size);
119 result->texture->setFormat(format);
120 result->texture->create();
123 if (!result->renderTarget) {
124 QRhiColorAttachment colorAttachment(result->texture);
125 colorAttachment.setMultiViewCount(viewCount);
126 QRhiTextureRenderTargetDescription desc(colorAttachment);
127 result->renderTarget = rhi->newTextureRenderTarget(desc);
128 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
129 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
130 result->renderTarget->create();
131 m_pendingClears.insert(result->renderTarget);
132 }
else if (needsRebuild) {
134 delete result->renderPassDescriptor;
135 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
136 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
138 result->renderTarget->create();
139 m_pendingClears.insert(result->renderTarget);
143 QByteArray rtName = inEffect->debugObjectName.toLatin1();
144 rtName += QByteArrayLiteral(
" effect pass ");
145 rtName += bufferName;
146 result->renderTarget->setName(rtName);
149 result->name = bufferName;
153void QSSGRhiEffectSystem::releaseTexture(QSSGRhiEffectTexture *texture)
157 if (!texture->flags.isSceneLifetime())
161void QSSGRhiEffectSystem::releaseTextures()
163 for (
auto *t : std::as_const(m_textures))
167QRhiTexture *QSSGRhiEffectSystem::process(
const QSSGRenderLayer &layer,
168 QRhiTexture *inTexture,
169 QRhiTexture *inDepthTexture,
170 QRhiTexture *inNormalTexture)
172 QSSG_ASSERT(m_sgContext !=
nullptr,
return inTexture);
173 QSSG_ASSERT(layer.firstEffect !=
nullptr,
return inTexture);
174 const auto &rhiContext = m_sgContext->rhiContext();
175 const auto &renderer = m_sgContext->renderer();
176 QSSG_ASSERT(rhiContext && renderer,
return inTexture);
178 const auto viewCount = layer.viewCount;
180 m_depthTexture = inDepthTexture;
181 m_normalTexture = inNormalTexture;
182 m_cameraClipRange = layer.renderedCameras[0]->clipPlanes;
184 bool usesProjectionMatrix =
false;
185 bool usesViewMatrix =
false;
186 for (
const QSSGRenderEffect *eff = layer.firstEffect; eff; eff = eff->m_nextEffect) {
187 if (eff->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix)
188 || eff->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix))
190 usesProjectionMatrix =
true;
192 if (eff->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix))
193 usesViewMatrix =
true;
196 if (usesProjectionMatrix) {
197 m_projectionMatrices.resize(viewCount);
198 const QMatrix4x4 clipSpaceCorrMatrix = rhiContext->rhi()->clipSpaceCorrMatrix();
199 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
200 m_projectionMatrices[viewIndex] = clipSpaceCorrMatrix * layer.renderedCameras[viewIndex]->projection;
203 if (usesViewMatrix) {
204 QMatrix4x4 camGlobalTransforms[2] { QMatrix4x4{Qt::Uninitialized}, QMatrix4x4{Qt::Uninitialized} };
205 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
206 camGlobalTransforms[viewIndex] = layer.renderData->getGlobalTransform(*layer.renderedCameras[viewIndex]);
207 m_viewMatrices.resize(viewCount);
208 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
209 m_viewMatrices[viewIndex] = camGlobalTransforms[viewIndex].inverted();
212 m_currentUbufIndex = 0;
214 QSSGRenderEffect *currentEffect =
const_cast<QSSGRenderEffect *>(layer.firstEffect);
215 QSSGRhiEffectTexture firstTex{ inTexture,
nullptr,
nullptr, {}, {}, {} };
216 auto *latestOutput = doRenderEffect(currentEffect, &firstTex, viewCount);
217 firstTex.texture =
nullptr;
219 while ((currentEffect = currentEffect->m_nextEffect)) {
220 QSSGRhiEffectTexture *effectOut = doRenderEffect(currentEffect, latestOutput, viewCount);
221 releaseTexture(latestOutput);
222 latestOutput = effectOut;
226 return latestOutput ? latestOutput->texture :
nullptr;
229void QSSGRhiEffectSystem::releaseResources()
231 qDeleteAll(m_textures);
234 m_shaderPipelines.clear();
237QSSGRenderTextureFormat::Format QSSGRhiEffectSystem::overriddenOutputFormat(
const QSSGRenderEffect *inEffect)
239 QSSGRenderTextureFormat::Format format = QSSGRenderTextureFormat::Unknown;
240 for (
const QSSGCommand *cmd : inEffect->commands) {
241 if (cmd->m_type == CommandType::BindTarget) {
242 const QSSGBindTarget *targetCmd =
static_cast<
const QSSGBindTarget *>(cmd);
243 format = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown
244 ? inEffect->outputFormat : targetCmd->m_outputFormat.format;
250QSSGRhiEffectTexture *QSSGRhiEffectSystem::doRenderEffect(
const QSSGRenderEffect *inEffect,
251 QSSGRhiEffectTexture *inTexture,
255 qCDebug(lcEffectSystem) <<
"START effect " << inEffect->className;
256 QSSGRhiEffectTexture *finalOutputTexture =
nullptr;
257 QSSGRhiEffectTexture *currentOutput =
nullptr;
258 QSSGRhiEffectTexture *currentInput = inTexture;
259 for (
const QSSGCommand *theCommand : inEffect->commands) {
260 qCDebug(lcEffectSystem).noquote() <<
" >" << theCommand->typeAsString() <<
"--" << theCommand->debugString();
262 switch (theCommand->m_type) {
263 case CommandType::AllocateBuffer:
264 allocateBufferCmd(
static_cast<
const QSSGAllocateBuffer *>(theCommand), inTexture, inEffect, viewCount);
267 case CommandType::ApplyBufferValue: {
268 auto *applyCommand =
static_cast<
const QSSGApplyBufferValue *>(theCommand);
271
272
273
274
275
276
277
278
279
281 auto *buffer = applyCommand->m_bufferName.isEmpty() ? inTexture : findTexture(applyCommand->m_bufferName);
282 if (applyCommand->m_samplerName.isEmpty())
283 currentInput = buffer;
285 addTextureToShaderPipeline(applyCommand->m_samplerName, buffer->texture, buffer->desc);
289 case CommandType::ApplyInstanceValue:
290 applyInstanceValueCmd(
static_cast<
const QSSGApplyInstanceValue *>(theCommand), inEffect);
293 case CommandType::ApplyValue:
294 applyValueCmd(
static_cast<
const QSSGApplyValue *>(theCommand), inEffect);
297 case CommandType::BindBuffer: {
298 auto *bindCmd =
static_cast<
const QSSGBindBuffer *>(theCommand);
299 currentOutput = findTexture(bindCmd->m_bufferName);
303 case CommandType::BindShader:
304 bindShaderCmd(
static_cast<
const QSSGBindShader *>(theCommand), inEffect, viewCount);
307 case CommandType::BindTarget: {
308 auto targetCmd =
static_cast<
const QSSGBindTarget*>(theCommand);
310 QSSGRenderTextureFormat::Format f = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown ?
311 inEffect->outputFormat : targetCmd->m_outputFormat.format;
313 QRhiTexture::Format rhiFormat = f == QSSGRenderTextureFormat::Unknown ?
314 currentInput->texture->format() : QSSGBufferManager::toRhiFormat(f);
315 qCDebug(lcEffectSystem) <<
" Target format override" << QSSGBaseTypeHelpers::toString(f) <<
"Effective RHI format" << rhiFormat;
317 QByteArray tmpName = QByteArrayLiteral(
"__output_").append(QByteArray::number(m_currentUbufIndex));
318 currentOutput = getTexture(tmpName, m_outSize, rhiFormat,
true, inEffect, viewCount);
319 finalOutputTexture = currentOutput;
323 case CommandType::Render:
324 renderCmd(inEffect, currentInput, currentOutput, viewCount);
325 currentInput = inTexture;
329 qWarning() <<
"Effect command" << theCommand->typeAsString() <<
"not implemented";
334 qCDebug(lcEffectSystem) <<
"END effect " << inEffect->className;
335 return finalOutputTexture;
338void QSSGRhiEffectSystem::allocateBufferCmd(
const QSSGAllocateBuffer *inCmd,
339 QSSGRhiEffectTexture *inTexture,
340 const QSSGRenderEffect *inEffect,
344 QSize bufferSize(m_outSize * qreal(inCmd->m_sizeMultiplier));
346 QSSGRenderTextureFormat f = inCmd->m_format;
347 QRhiTexture::Format rhiFormat = (f == QSSGRenderTextureFormat::Unknown) ? inTexture->texture->format()
348 : QSSGBufferManager::toRhiFormat(f);
350 QSSGRhiEffectTexture *buf = getTexture(inCmd->m_name, bufferSize, rhiFormat,
false, inEffect, viewCount);
351 auto filter = QSSGRhiHelpers::toRhi(inCmd->m_filterOp);
352 auto tiling = QSSGRhiHelpers::toRhi(inCmd->m_texCoordOp);
353 buf->desc = { filter, filter, QRhiSampler::None, tiling, tiling, QRhiSampler::Repeat };
354 buf->flags = inCmd->m_bufferFlags;
357void QSSGRhiEffectSystem::applyInstanceValueCmd(
const QSSGApplyInstanceValue *inCmd,
const QSSGRenderEffect *inEffect)
359 if (!m_currentShaderPipeline)
362 const bool setAll = inCmd->m_propertyName.isEmpty();
363 for (
const QSSGRenderEffect::Property &property : std::as_const(inEffect->properties)) {
364 if (setAll || property.name == inCmd->m_propertyName) {
365 m_currentShaderPipeline->setUniformValue(m_currentUBufData, property.name, property.value, property.shaderDataType);
369 for (
const QSSGRenderEffect::TextureProperty &textureProperty : std::as_const(inEffect->textureProperties)) {
370 if (setAll || textureProperty.name == inCmd->m_propertyName) {
371 bool texAdded =
false;
372 QSSGRenderImage *image = textureProperty.texImage;
374 const auto &theBufferManager(m_sgContext->bufferManager());
375 const QSSGRenderImageTexture texture = theBufferManager->loadRenderImage(image);
376 if (texture.m_texture) {
377 const QSSGRhiSamplerDescription desc{
378 QSSGRhiHelpers::toRhi(textureProperty.minFilterType),
379 QSSGRhiHelpers::toRhi(textureProperty.magFilterType),
380 textureProperty.mipFilterType != QSSGRenderTextureFilterOp::None ? QSSGRhiHelpers::toRhi(textureProperty.mipFilterType) : QRhiSampler::None,
381 QSSGRhiHelpers::toRhi(textureProperty.horizontalClampType),
382 QSSGRhiHelpers::toRhi(textureProperty.verticalClampType),
383 QSSGRhiHelpers::toRhi(textureProperty.zClampType)
385 addTextureToShaderPipeline(textureProperty.name, texture.m_texture, desc);
391 qCDebug(lcEffectSystem) <<
"Using dummy texture for property" << textureProperty.name;
392 addTextureToShaderPipeline(textureProperty.name,
nullptr, {});
398void QSSGRhiEffectSystem::applyValueCmd(
const QSSGApplyValue *inCmd,
const QSSGRenderEffect *inEffect)
400 if (!m_currentShaderPipeline)
403 const auto &properties = inEffect->properties;
404 const auto foundIt = std::find_if(properties.cbegin(), properties.cend(), [inCmd](
const QSSGRenderEffect::Property &prop) {
405 return (prop.name == inCmd->m_propertyName);
408 if (foundIt != properties.cend())
409 m_currentShaderPipeline->setUniformValue(m_currentUBufData, inCmd->m_propertyName, inCmd->m_value, foundIt->shaderDataType);
411 qWarning() <<
"Could not find effect property" << inCmd->m_propertyName;
415 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
421 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
423 " return vec2(uv.x, 1.0 - uv.y);\n"
426QSSGRhiShaderPipelinePtr QSSGRhiEffectSystem::buildShaderForEffect(
const QSSGBindShader &inCmd,
427 QSSGProgramGenerator &generator,
428 QSSGShaderLibraryManager &shaderLib,
429 QSSGShaderCache &shaderCache,
430 bool isYUpInFramebuffer,
433 const auto &key = inCmd.m_shaderPathKey;
434 qCDebug(lcEffectSystem) <<
" generating new shader pipeline for: " << key;
436 generator.beginProgram();
439 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Vertex);
440 QSSGStageGeneratorBase *vStage = generator.getStage(QSSGShaderGeneratorStage::Vertex);
443 vStage->append(isYUpInFramebuffer ? effect_builtin_textureMapUV : effect_builtin_textureMapUVFlipped);
447 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment);
448 QSSGStageGeneratorBase *fStage = generator.getStage(QSSGShaderGeneratorStage::Fragment);
452 return generator.compileGeneratedRhiShader(key,
453 shaderLib.getShaderMetaData(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features,
456 QSSGRhiShaderPipeline::UsedWithoutIa,
461void QSSGRhiEffectSystem::bindShaderCmd(
const QSSGBindShader *inCmd,
const QSSGRenderEffect *inEffect, quint8 viewCount)
466 m_currentTextures.clear();
467 m_pendingClears.clear();
468 m_currentShaderPipeline =
nullptr;
470 const auto &rhiCtx = m_sgContext->rhiContext();
471 QRhi *rhi = rhiCtx->rhi();
472 const auto &shaderLib = m_sgContext->shaderLibraryManager();
473 const auto &shaderCache = m_sgContext->shaderCache();
480 QSSGEffectSceneCacheKey cacheKey;
481 cacheKey.m_shaderPathKey = inCmd->m_shaderPathKey;
482 cacheKey.m_cmd = quintptr(inCmd);
483 cacheKey.m_ubufIndex = m_currentUbufIndex;
484 cacheKey.updateHashCode();
487 const auto it = m_shaderPipelines.constFind(cacheKey);
488 if (it != m_shaderPipelines.cend())
489 m_currentShaderPipeline = (*it).get();
492 QSSGShaderFeatures features;
493 if (!m_currentShaderPipeline) {
494 features = shaderLib->getShaderMetaData(inCmd->m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features;
495 qsbcKey = QQsbCollection::EntryDesc::generateSha(inCmd->m_shaderPathKey, QQsbCollection::toFeatureSet(features));
499 if (!m_currentShaderPipeline && !shaderLib->m_preGeneratedShaderEntries.isEmpty()) {
500 const QQsbCollection::EntryMap &pregenEntries = shaderLib->m_preGeneratedShaderEntries;
501 const auto foundIt = pregenEntries.constFind(QQsbCollection::Entry(qsbcKey));
502 if (foundIt != pregenEntries.cend()) {
506 const auto &shader = shaderCache->newPipelineFromPregenerated(inCmd->m_shaderPathKey,
510 QSSGRhiShaderPipeline::UsedWithoutIa);
511 m_shaderPipelines.insert(cacheKey, shader);
512 m_currentShaderPipeline = shader.get();
516 if (!m_currentShaderPipeline) {
522 const auto &shaderPipeline = shaderCache->tryNewPipelineFromPersistentCache(qsbcKey,
523 inCmd->m_shaderPathKey,
525 QSSGRhiShaderPipeline::UsedWithoutIa);
526 if (shaderPipeline) {
527 m_shaderPipelines.insert(cacheKey, shaderPipeline);
528 m_currentShaderPipeline = shaderPipeline.get();
532 if (!m_currentShaderPipeline) {
534 Q_TRACE_SCOPE(QSSG_generateShader);
535 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DGenerateShader);
536 const auto &generator = m_sgContext->shaderProgramGenerator();
537 if (
auto stages = buildShaderForEffect(*inCmd, *generator, *shaderLib, *shaderCache, rhi->isYUpInFramebuffer(), viewCount)) {
538 m_shaderPipelines.insert(cacheKey, stages);
539 m_currentShaderPipeline = stages.get();
541 Q_QUICK3D_PROFILE_END_WITH_ID(QQuick3DProfiler::Quick3DGenerateShader, 0, inEffect->profilingId);
544 const auto &rhiContext = m_sgContext->rhiContext();
546 if (m_currentShaderPipeline) {
547 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
548 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
549 QSSGRhiDrawCallData &dcd = QSSGRhiContextPrivate::get(rhiContext.get())->drawCallData({ cacheKey1, cacheKey2,
nullptr, 0 });
550 m_currentShaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
551 m_currentUBufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
553 m_currentUBufData =
nullptr;
556 QSSGRhiContextStats::get(*rhiContext).registerEffectShaderGenerationTime(timer.elapsed());
559void QSSGRhiEffectSystem::renderCmd(
const QSSGRenderEffect *inEffect, QSSGRhiEffectTexture *inTexture, QSSGRhiEffectTexture *target, quint8 viewCount)
561 if (!m_currentShaderPipeline)
565 qWarning(
"No effect render target?");
570 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTexture"), inTexture->texture, inTexture->desc);
571 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTextureArray"), inTexture->texture, inTexture->desc);
573 const auto &rhiContext = m_sgContext->rhiContext();
574 const auto &renderer = m_sgContext->renderer();
576 QRhiCommandBuffer *cb = rhiContext->commandBuffer();
577 cb->debugMarkBegin(QByteArrayLiteral(
"Post-processing effect"));
578 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
580 for (QRhiTextureRenderTarget *rt : m_pendingClears) {
586 if (rt != target->renderTarget) {
587 cb->beginPass(rt, Qt::transparent, { 1.0f, 0 },
nullptr, rhiContext->commonPassFlags());
588 QSSGRHICTX_STAT(rhiContext, beginRenderPass(rt));
590 QSSGRHICTX_STAT(rhiContext, endRenderPass());
593 m_pendingClears.clear();
595 const QSize inputSize = inTexture->texture->pixelSize();
596 const QSize outputSize = target->texture->pixelSize();
597 addCommonEffectUniforms(inEffect, inputSize, outputSize, viewCount);
599 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiContext.get());
601 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
602 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
603 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ cacheKey1, cacheKey2,
nullptr, 0 });
604 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
605 m_currentUBufData =
nullptr;
607 QRhiResourceUpdateBatch *rub = rhiContext->rhi()->nextResourceUpdateBatch();
608 renderer->rhiQuadRenderer()->prepareQuad(rhiContext.get(), rub);
611 const QRhiShaderResourceBinding::StageFlags VISIBILITY_ALL =
612 QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage;
613 QSSGRhiShaderResourceBindingList bindings;
614 for (
const QSSGRhiTexture &rhiTex : m_currentTextures) {
615 int binding = m_currentShaderPipeline->bindingForTexture(rhiTex.name);
618 qCDebug(lcEffectSystem) <<
" -> texture binding" << binding <<
"for" << rhiTex.name;
620 QRhiTexture *texture = rhiTex.texture ? rhiTex.texture : rhiContext->dummyTexture({}, rub);
621 bindings.addTexture(binding,
622 QRhiShaderResourceBinding::FragmentStage,
624 rhiContext->sampler(rhiTex.samplerDesc));
626 bindings.addUniformBuffer(0, VISIBILITY_ALL, dcd.ubuf);
628 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
630 QSSGRhiGraphicsPipelineState ps;
631 ps.viewport = QRhiViewport(0, 0,
float(outputSize.width()),
float(outputSize.height()));
632 ps.samples = target->renderTarget->sampleCount();
633 ps.viewCount = viewCount;
634 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, m_currentShaderPipeline);
636 renderer->rhiQuadRenderer()->recordRenderQuadPass(rhiContext.get(), &ps, srb, target->renderTarget, QSSGRhiQuadRenderer::UvCoords);
637 m_currentUbufIndex++;
639 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"post_processing_effect"));
642void QSSGRhiEffectSystem::addCommonEffectUniforms(
const QSSGRenderEffect *inEffect,
const QSize &inputSize,
const QSize &outputSize, quint8 viewCount)
644 const auto &rhiContext = m_sgContext->rhiContext();
645 QRhi *rhi = rhiContext->rhi();
648 if (rhi->isYUpInFramebuffer() != rhi->isYUpInNDC())
649 mvp.data()[5] = -1.0f;
650 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_modelViewProjection", mvp, QSSGRenderShaderValue::Matrix4x4);
652 const bool usesProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix);
653 const bool usesInverseProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix);
654 if (usesProjectionMatrix || usesInverseProjectionMatrix) {
656 if (usesProjectionMatrix)
657 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices[0], QSSGRenderShaderValue::Matrix4x4);
658 if (usesInverseProjectionMatrix)
659 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inverseProjectionMatrix", m_projectionMatrices[0].inverted(), QSSGRenderShaderValue::Matrix4x4);
661 if (usesProjectionMatrix)
662 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
663 if (usesInverseProjectionMatrix) {
664 QVarLengthArray<QMatrix4x4, 2> invertedProjections(viewCount);
665 for (quint8 viewIndex = 0; viewIndex < viewCount; ++viewIndex)
666 invertedProjections[viewIndex] = m_projectionMatrices[viewIndex].inverted();
667 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_inverseProjectionMatrix", invertedProjections.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
672 if (inEffect->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix)) {
674 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices[0], QSSGRenderShaderValue::Matrix4x4);
676 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
679 QVector2D size(inputSize.width(), inputSize.height());
680 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inputSize", size, QSSGRenderShaderValue::Vec2);
682 size = QVector2D(outputSize.width(), outputSize.height());
683 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_outputSize", size, QSSGRenderShaderValue::Vec2);
685 float fc =
float(m_sgContext->renderer()->frameCount());
686 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_frame_num", fc, QSSGRenderShaderValue::Float);
691 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_cameraProperties", m_cameraClipRange, QSSGRenderShaderValue::Vec2);
693 float vp = rhi->isYUpInFramebuffer() ? 1.0f : -1.0f;
694 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_normalAdjustViewportFactor", vp, QSSGRenderShaderValue::Float);
696 const float nearClip = rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f;
697 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_nearClipValue", nearClip, QSSGRenderShaderValue::Float);
699 const QVector4D rhiProperties(
700 rhi->isYUpInFramebuffer() ? 1.0f : -1.0f,
701 rhi->isYUpInNDC() ? 1.0f : -1.0f,
702 rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f,
705 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_rhi_properties", rhiProperties, QSSGRenderShaderValue::Vec4);
707 if (m_depthTexture) {
708 static const QSSGRhiSamplerDescription depthSamplerDesc {
709 QRhiSampler::Nearest, QRhiSampler::Nearest,
711 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
713 addTextureToShaderPipeline(
"qt_depthTexture", m_depthTexture, depthSamplerDesc);
714 addTextureToShaderPipeline(
"qt_depthTextureArray", m_depthTexture, depthSamplerDesc);
717 if (m_normalTexture) {
718 static const QSSGRhiSamplerDescription normalSamplerDesc {
719 QRhiSampler::Nearest, QRhiSampler::Nearest,
721 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
723 addTextureToShaderPipeline(
"qt_normalTexture", m_normalTexture, normalSamplerDesc);
727void QSSGRhiEffectSystem::addTextureToShaderPipeline(
const QByteArray &name,
728 QRhiTexture *texture,
729 const QSSGRhiSamplerDescription &samplerDescription)
731 if (!m_currentShaderPipeline)
734 static const QSSGRhiSamplerDescription defaultDescription { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
735 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat };
736 bool validDescription = samplerDescription.magFilter != QRhiSampler::None;
741 m_currentTextures.insert(name, { name, texture, validDescription ? samplerDescription : defaultDescription});
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
static const char * effect_builtin_textureMapUVFlipped
static const char * effect_builtin_textureMapUV
QSSGRhiSamplerDescription desc
QSSGAllocateBufferFlags flags
QRhiTextureRenderTarget * renderTarget
QRhiRenderPassDescriptor * renderPassDescriptor
QSSGRhiEffectTexture & operator=(const QSSGRhiEffectTexture &)=delete