7#include <QtQuick3DRuntimeRender/private/qssgrhieffectsystem_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
9#include <QtQuick3DRuntimeRender/private/qssgrhiquadrenderer_p.h>
10#include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h>
11#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
12#include <QtQuick3DRuntimeRender/private/qssgrenderbuffer_p.h>
13#include <QtQuick3DRuntimeRender/private/qssgrenderimage_p.h>
17#include <qtquick3d_tracepoints_p.h>
19#include <QtQuick3DUtils/private/qssgassert_p.h>
21#include <QtCore/qloggingcategory.h>
25Q_DECLARE_LOGGING_CATEGORY(lcEffectSystem);
42 delete renderPassDescriptor;
48QSSGRhiEffectSystem::QSSGRhiEffectSystem(
const std::shared_ptr<QSSGRenderContextInterface> &sgContext)
49 : m_sgContext(sgContext)
53QSSGRhiEffectSystem::~QSSGRhiEffectSystem()
56 if (
const auto &rhiCtx = m_sgContext->rhiContext(); rhiCtx && rhiCtx->isValid())
57 QSSGRhiContextPrivate::get(rhiCtx.get())->cleanupDrawCallDataForCid(
this);
61void QSSGRhiEffectSystem::setup(QSize outputSize)
63 if (outputSize.isEmpty()) {
67 m_outSize = outputSize;
70QSSGRhiEffectTexture *QSSGRhiEffectSystem::findTexture(
const QByteArray &bufferName)
72 auto findTexture = [bufferName](
const QSSGRhiEffectTexture *rt){
return rt->name == bufferName; };
73 const auto foundIt = std::find_if(m_textures.cbegin(), m_textures.cend(), findTexture);
74 QSSGRhiEffectTexture *result = foundIt == m_textures.cend() ?
nullptr : *foundIt;
78QSSGRhiEffectTexture *QSSGRhiEffectSystem::getTexture(
const QByteArray &bufferName,
80 QRhiTexture::Format format,
82 const QSSGRenderEffect *inEffect,
85 QSSGRhiEffectTexture *result = findTexture(bufferName);
86 const bool gotMatch = result !=
nullptr;
98 const quint32 currentFrame = m_sgContext->renderer()->frameCount();
100 QSSGRhiEffectTexture *notUsedInFrame =
nullptr;
101 for (QSSGRhiEffectTexture *t : std::as_const(m_textures)) {
102 if (!t->name.isEmpty())
104 if (t->texture->pixelSize() == size && t->texture->format() == format) {
108 if (!notUsedInFrame && t->lastUsedFrame != currentFrame)
112 result = notUsedInFrame;
118 result =
new QSSGRhiEffectTexture {};
119 m_textures.append(result);
122 const auto &rhiCtx = m_sgContext->rhiContext();
123 QRhi *rhi = rhiCtx->rhi();
124 const bool formatChanged = result->texture && result->texture->format() != format;
125 const bool needsRebuild = result->texture && (result->texture->pixelSize() != size || formatChanged);
127 QRhiTexture::Flags flags = QRhiTexture::RenderTarget;
129 flags |= QRhiTexture::UsedAsTransferSource;
131 if (needsRebuild && result->lastUsedFrame == currentFrame) {
137 m_pendingClears.remove(result->renderTarget);
138 result->renderTarget->deleteLater();
139 result->renderTarget =
nullptr;
140 result->renderPassDescriptor->deleteLater();
141 result->renderPassDescriptor =
nullptr;
142 result->texture->deleteLater();
143 result->texture =
nullptr;
146 if (!result->texture) {
148 result->texture = rhi->newTextureArray(format, viewCount, size, 1, flags);
150 result->texture = rhi->newTexture(format, size, 1, flags);
151 result->texture->create();
152 }
else if (needsRebuild) {
153 result->texture->setFlags(flags);
154 result->texture->setPixelSize(size);
155 result->texture->setFormat(format);
156 result->texture->create();
159 const bool newRenderTarget = !result->renderTarget;
160 if (newRenderTarget) {
161 QRhiColorAttachment colorAttachment(result->texture);
162 colorAttachment.setMultiViewCount(viewCount);
163 QRhiTextureRenderTargetDescription desc(colorAttachment);
164 result->renderTarget = rhi->newTextureRenderTarget(desc);
165 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
166 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
167 result->renderTarget->create();
168 m_pendingClears.insert(result->renderTarget);
169 }
else if (needsRebuild) {
171 delete result->renderPassDescriptor;
172 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
173 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
175 result->renderTarget->create();
176 m_pendingClears.insert(result->renderTarget);
179 if (!gotMatch || newRenderTarget) {
180 QByteArray rtName = inEffect->debugObjectName.toLatin1();
181 rtName += QByteArrayLiteral(
" effect pass ");
182 rtName += bufferName;
183 result->renderTarget->setName(rtName);
186 result->name = bufferName;
187 result->lastUsedFrame = currentFrame;
191void QSSGRhiEffectSystem::releaseTexture(QSSGRhiEffectTexture *texture)
195 if (!texture->flags.isSceneLifetime())
199void QSSGRhiEffectSystem::releaseTextures()
201 for (
auto *t : std::as_const(m_textures))
205QRhiTexture *QSSGRhiEffectSystem::process(
const QSSGRenderLayer &layer,
206 QRhiTexture *inTexture,
207 QRhiTexture *inDepthTexture,
208 QRhiTexture *inNormalTexture,
209 QRhiTexture *inMotionVectorTexture)
211 QSSG_ASSERT(m_sgContext !=
nullptr,
return inTexture);
212 QSSG_ASSERT(layer.firstEffect !=
nullptr,
return inTexture);
213 const auto &rhiContext = m_sgContext->rhiContext();
214 const auto &renderer = m_sgContext->renderer();
215 QSSG_ASSERT(rhiContext && renderer,
return inTexture);
217 const auto viewCount = layer.viewCount;
219 m_depthTexture = inDepthTexture;
220 m_normalTexture = inNormalTexture;
221 m_motionVectorTexture = inMotionVectorTexture;
222 m_cameraClipRange = layer.renderedCameras[0]->clipPlanes;
224 bool usesProjectionMatrix =
false;
225 bool usesViewMatrix =
false;
226 for (
const QSSGRenderEffect *eff = layer.firstEffect; eff; eff = eff->m_nextEffect) {
227 if (eff->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix)
228 || eff->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix))
230 usesProjectionMatrix =
true;
232 if (eff->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix))
233 usesViewMatrix =
true;
236 if (usesProjectionMatrix) {
237 m_projectionMatrices.resize(viewCount);
238 const QMatrix4x4 clipSpaceCorrMatrix = rhiContext->rhi()->clipSpaceCorrMatrix();
239 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
240 m_projectionMatrices[viewIndex] = clipSpaceCorrMatrix * layer.renderedCameras[viewIndex]->projection;
243 if (usesViewMatrix) {
244 QMatrix4x4 camGlobalTransforms[2] { QMatrix4x4{Qt::Uninitialized}, QMatrix4x4{Qt::Uninitialized} };
245 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
246 camGlobalTransforms[viewIndex] = layer.renderData->getGlobalTransform(*layer.renderedCameras[viewIndex]);
247 m_viewMatrices.resize(viewCount);
248 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
249 m_viewMatrices[viewIndex] = camGlobalTransforms[viewIndex].inverted();
252 m_currentUbufIndex = 0;
254 QSSGRenderEffect *currentEffect =
const_cast<QSSGRenderEffect *>(layer.firstEffect);
255 QSSGRhiEffectTexture firstTex{ inTexture,
nullptr,
nullptr, {}, {}, {} };
256 auto *latestOutput = doRenderEffect(currentEffect, &firstTex, viewCount);
257 firstTex.texture =
nullptr;
259 while ((currentEffect = currentEffect->m_nextEffect)) {
260 QSSGRhiEffectTexture *effectOut = doRenderEffect(currentEffect, latestOutput, viewCount);
261 releaseTexture(latestOutput);
262 latestOutput = effectOut;
266 return latestOutput ? latestOutput->texture :
nullptr;
269void QSSGRhiEffectSystem::releaseResources()
271 qDeleteAll(m_textures);
274 m_shaderPipelines.clear();
277QSSGRenderTextureFormat::Format QSSGRhiEffectSystem::overriddenOutputFormat(
const QSSGRenderEffect *inEffect)
279 QSSGRenderTextureFormat::Format format = QSSGRenderTextureFormat::Unknown;
280 for (
const QSSGCommand *cmd : inEffect->commands) {
281 if (cmd->m_type == CommandType::BindTarget) {
282 const QSSGBindTarget *targetCmd =
static_cast<
const QSSGBindTarget *>(cmd);
283 format = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown
284 ? inEffect->outputFormat : targetCmd->m_outputFormat.format;
290QSSGRhiEffectTexture *QSSGRhiEffectSystem::doRenderEffect(
const QSSGRenderEffect *inEffect,
291 QSSGRhiEffectTexture *inTexture,
295 qCDebug(lcEffectSystem) <<
"START effect " << inEffect->className;
296 QSSGRhiEffectTexture *finalOutputTexture =
nullptr;
297 QSSGRhiEffectTexture *currentOutput =
nullptr;
298 QSSGRhiEffectTexture *currentInput = inTexture;
299 for (
const QSSGCommand *theCommand : inEffect->commands) {
300 qCDebug(lcEffectSystem).noquote() <<
" >" << theCommand->typeAsString() <<
"--" << theCommand->debugString();
302 switch (theCommand->m_type) {
303 case CommandType::AllocateBuffer:
304 allocateBufferCmd(
static_cast<
const QSSGAllocateBuffer *>(theCommand), inTexture, inEffect, viewCount);
307 case CommandType::ApplyBufferValue: {
308 auto *applyCommand =
static_cast<
const QSSGApplyBufferValue *>(theCommand);
311
312
313
314
315
316
317
318
319
321 auto *buffer = applyCommand->m_bufferName.isEmpty() ? inTexture : findTexture(applyCommand->m_bufferName);
322 if (applyCommand->m_samplerName.isEmpty())
323 currentInput = buffer;
325 addTextureToShaderPipeline(applyCommand->m_samplerName, buffer->texture, buffer->desc);
329 case CommandType::ApplyInstanceValue:
330 applyInstanceValueCmd(
static_cast<
const QSSGApplyInstanceValue *>(theCommand), inEffect);
333 case CommandType::ApplyValue:
334 applyValueCmd(
static_cast<
const QSSGApplyValue *>(theCommand), inEffect);
337 case CommandType::BindBuffer: {
338 auto *bindCmd =
static_cast<
const QSSGBindBuffer *>(theCommand);
339 currentOutput = findTexture(bindCmd->m_bufferName);
343 case CommandType::BindShader:
344 bindShaderCmd(
static_cast<
const QSSGBindShader *>(theCommand), inEffect, viewCount);
347 case CommandType::BindTarget: {
348 auto targetCmd =
static_cast<
const QSSGBindTarget*>(theCommand);
350 QSSGRenderTextureFormat::Format f = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown ?
351 inEffect->outputFormat : targetCmd->m_outputFormat.format;
353 QRhiTexture::Format rhiFormat = f == QSSGRenderTextureFormat::Unknown ?
354 currentInput->texture->format() : QSSGBufferManager::toRhiFormat(f);
355 qCDebug(lcEffectSystem) <<
" Target format override" << QSSGBaseTypeHelpers::toString(f) <<
"Effective RHI format" << rhiFormat;
357 QByteArray tmpName = QByteArrayLiteral(
"__output_").append(QByteArray::number(m_currentUbufIndex));
358 currentOutput = getTexture(tmpName, m_outSize, rhiFormat,
true, inEffect, viewCount);
359 finalOutputTexture = currentOutput;
363 case CommandType::Render:
364 renderCmd(inEffect, currentInput, currentOutput, viewCount);
365 currentInput = inTexture;
369 qWarning() <<
"Effect command" << theCommand->typeAsString() <<
"not implemented";
374 qCDebug(lcEffectSystem) <<
"END effect " << inEffect->className;
375 return finalOutputTexture;
378void QSSGRhiEffectSystem::allocateBufferCmd(
const QSSGAllocateBuffer *inCmd,
379 QSSGRhiEffectTexture *inTexture,
380 const QSSGRenderEffect *inEffect,
384 QSize bufferSize(m_outSize * qreal(inCmd->m_sizeMultiplier));
386 QSSGRenderTextureFormat f = inCmd->m_format;
387 QRhiTexture::Format rhiFormat = (f == QSSGRenderTextureFormat::Unknown) ? inTexture->texture->format()
388 : QSSGBufferManager::toRhiFormat(f);
390 QSSGRhiEffectTexture *buf = getTexture(inCmd->m_name, bufferSize, rhiFormat,
false, inEffect, viewCount);
391 auto filter = QSSGRhiHelpers::toRhi(inCmd->m_filterOp);
392 auto tiling = QSSGRhiHelpers::toRhi(inCmd->m_texCoordOp);
393 buf->desc = { filter, filter, QRhiSampler::None, tiling, tiling, QRhiSampler::Repeat };
394 buf->flags = inCmd->m_bufferFlags;
397void QSSGRhiEffectSystem::applyInstanceValueCmd(
const QSSGApplyInstanceValue *inCmd,
const QSSGRenderEffect *inEffect)
399 if (!m_currentShaderPipeline)
402 const bool setAll = inCmd->m_propertyName.isEmpty();
403 for (
const QSSGRenderEffect::Property &property : std::as_const(inEffect->properties)) {
404 if (setAll || property.name == inCmd->m_propertyName) {
405 if (property.shaderDataType == QSSGRenderShaderValue::StorageBuffer
406 || property.shaderDataType == QSSGRenderShaderValue::Image) {
407 m_currentShaderPipeline->setShaderResources(m_currentUBufData, *m_sgContext->bufferManager(),
408 property.name, property.value, property.shaderDataType);
410 m_currentShaderPipeline->setUniformValue(m_currentUBufData, property.name, property.value, property.shaderDataType);
415 for (
const QSSGRenderEffect::TextureProperty &textureProperty : std::as_const(inEffect->textureProperties)) {
416 if (setAll || textureProperty.name == inCmd->m_propertyName) {
417 bool texAdded =
false;
418 QSSGRenderImage *image = textureProperty.texImage;
420 const auto &theBufferManager(m_sgContext->bufferManager());
421 const QSSGRenderImageTexture texture = theBufferManager->loadRenderImage(image);
422 if (texture.m_texture) {
423 const QSSGRhiSamplerDescription desc{
424 QSSGRhiHelpers::toRhi(textureProperty.minFilterType),
425 QSSGRhiHelpers::toRhi(textureProperty.magFilterType),
426 textureProperty.mipFilterType != QSSGRenderTextureFilterOp::None ? QSSGRhiHelpers::toRhi(textureProperty.mipFilterType) : QRhiSampler::None,
427 QSSGRhiHelpers::toRhi(textureProperty.horizontalClampType),
428 QSSGRhiHelpers::toRhi(textureProperty.verticalClampType),
429 QSSGRhiHelpers::toRhi(textureProperty.zClampType)
431 addTextureToShaderPipeline(textureProperty.name, texture.m_texture, desc);
437 qCDebug(lcEffectSystem) <<
"Using dummy texture for property" << textureProperty.name;
438 addTextureToShaderPipeline(textureProperty.name,
nullptr, {});
444void QSSGRhiEffectSystem::applyValueCmd(
const QSSGApplyValue *inCmd,
const QSSGRenderEffect *inEffect)
446 if (!m_currentShaderPipeline)
449 const auto &properties = inEffect->properties;
450 const auto foundIt = std::find_if(properties.cbegin(), properties.cend(), [inCmd](
const QSSGRenderEffect::Property &prop) {
451 return (prop.name == inCmd->m_propertyName);
454 if (foundIt != properties.cend())
455 m_currentShaderPipeline->setUniformValue(m_currentUBufData, inCmd->m_propertyName, inCmd->m_value, foundIt->shaderDataType);
457 qWarning() <<
"Could not find effect property" << inCmd->m_propertyName;
461 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
467 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
469 " return vec2(uv.x, 1.0 - uv.y);\n"
472QSSGRhiShaderPipelinePtr QSSGRhiEffectSystem::buildShaderForEffect(
const QSSGBindShader &inCmd,
473 QSSGProgramGenerator &generator,
474 QSSGShaderLibraryManager &shaderLib,
475 QSSGShaderCache &shaderCache,
476 bool isYUpInFramebuffer,
479 const auto &key = inCmd.m_shaderPathKey;
480 qCDebug(lcEffectSystem) <<
" generating new shader pipeline for: " << key;
482 generator.beginProgram();
485 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Vertex);
486 QSSGStageGeneratorBase *vStage = generator.getStage(QSSGShaderGeneratorStage::Vertex);
489 vStage->append(isYUpInFramebuffer ? effect_builtin_textureMapUV : effect_builtin_textureMapUVFlipped);
493 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment);
494 QSSGStageGeneratorBase *fStage = generator.getStage(QSSGShaderGeneratorStage::Fragment);
498 return generator.compileGeneratedRhiShader(key,
499 shaderLib.getShaderMetaData(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features,
502 QSSGRhiShaderPipeline::UsedWithoutIa,
508void QSSGRhiEffectSystem::bindShaderCmd(
const QSSGBindShader *inCmd,
const QSSGRenderEffect *inEffect, quint8 viewCount)
513 m_currentTextures.clear();
514 m_pendingClears.clear();
515 m_currentShaderPipeline =
nullptr;
517 const auto &rhiCtx = m_sgContext->rhiContext();
518 QRhi *rhi = rhiCtx->rhi();
519 const auto &shaderLib = m_sgContext->shaderLibraryManager();
520 const auto &shaderCache = m_sgContext->shaderCache();
527 QSSGEffectSceneCacheKey cacheKey;
528 cacheKey.m_shaderPathKey = inCmd->m_shaderPathKey;
529 cacheKey.m_cmd = quintptr(inCmd);
530 cacheKey.m_ubufIndex = m_currentUbufIndex;
531 cacheKey.updateHashCode();
534 const auto it = m_shaderPipelines.constFind(cacheKey);
535 if (it != m_shaderPipelines.cend())
536 m_currentShaderPipeline = (*it).get();
539 QSSGShaderFeatures features;
540 if (!m_currentShaderPipeline) {
541 features = shaderLib->getShaderMetaData(inCmd->m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features;
542 qsbcKey = QQsbCollection::EntryDesc::generateSha(inCmd->m_shaderPathKey, QQsbCollection::toFeatureSet(features));
546 if (!m_currentShaderPipeline && !shaderLib->m_preGeneratedShaderEntries.isEmpty()) {
547 const QQsbCollection::EntryMap &pregenEntries = shaderLib->m_preGeneratedShaderEntries;
548 const auto foundIt = pregenEntries.constFind(QQsbCollection::Entry(qsbcKey));
549 if (foundIt != pregenEntries.cend()) {
553 const auto &shader = shaderCache->newPipelineFromPregenerated(inCmd->m_shaderPathKey,
557 QSSGRhiShaderPipeline::UsedWithoutIa);
558 m_shaderPipelines.insert(cacheKey, shader);
559 m_currentShaderPipeline = shader.get();
563 if (!m_currentShaderPipeline) {
569 const auto &shaderPipeline = shaderCache->tryNewPipelineFromPersistentCache(qsbcKey,
570 inCmd->m_shaderPathKey,
572 QSSGRhiShaderPipeline::UsedWithoutIa);
573 if (shaderPipeline) {
574 m_shaderPipelines.insert(cacheKey, shaderPipeline);
575 m_currentShaderPipeline = shaderPipeline.get();
579 if (!m_currentShaderPipeline) {
581 Q_TRACE_SCOPE(QSSG_generateShader);
582 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DGenerateShader);
583 const auto &generator = m_sgContext->shaderProgramGenerator();
584 if (
auto stages = buildShaderForEffect(*inCmd, *generator, *shaderLib, *shaderCache, rhi->isYUpInFramebuffer(), viewCount)) {
585 m_shaderPipelines.insert(cacheKey, stages);
586 m_currentShaderPipeline = stages.get();
588 Q_QUICK3D_PROFILE_END_WITH_ID(QQuick3DProfiler::Quick3DGenerateShader, 0, inEffect->profilingId);
591 const auto &rhiContext = m_sgContext->rhiContext();
593 if (m_currentShaderPipeline) {
594 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
595 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
596 QSSGRhiDrawCallData &dcd = QSSGRhiContextPrivate::get(rhiContext.get())->drawCallData({ cacheKey1, cacheKey2,
597 nullptr,
nullptr, 0 });
598 m_currentShaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
599 m_currentUBufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
601 m_currentUBufData =
nullptr;
604 QSSGRhiContextStats::get(*rhiContext).registerEffectShaderGenerationTime(timer.elapsed());
607void QSSGRhiEffectSystem::renderCmd(
const QSSGRenderEffect *inEffect, QSSGRhiEffectTexture *inTexture, QSSGRhiEffectTexture *target, quint8 viewCount)
609 if (!m_currentShaderPipeline)
613 qWarning(
"No effect render target?");
618 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTexture"), inTexture->texture, inTexture->desc);
619 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTextureArray"), inTexture->texture, inTexture->desc);
621 const auto &rhiContext = m_sgContext->rhiContext();
622 const auto &renderer = m_sgContext->renderer();
623 m_sgContext->bufferManager()->commitBufferResourceUpdates();
625 QRhiCommandBuffer *cb = rhiContext->commandBuffer();
626 cb->debugMarkBegin(QByteArrayLiteral(
"Post-processing effect"));
627 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
629 for (QRhiTextureRenderTarget *rt : std::as_const(m_pendingClears)) {
635 if (rt != target->renderTarget) {
636 cb->beginPass(rt, Qt::transparent, { 1.0f, 0 },
nullptr, rhiContext->commonPassFlags());
637 QSSGRHICTX_STAT(rhiContext, beginRenderPass(rt));
639 QSSGRHICTX_STAT(rhiContext, endRenderPass());
642 m_pendingClears.clear();
644 const QSize inputSize = inTexture->texture->pixelSize();
645 const QSize outputSize = target->texture->pixelSize();
646 addCommonEffectUniforms(inEffect, inputSize, outputSize, viewCount);
648 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiContext.get());
650 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
651 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
652 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ cacheKey1, cacheKey2,
nullptr,
nullptr, 0 });
653 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
654 m_currentUBufData =
nullptr;
656 QRhiResourceUpdateBatch *rub = rhiContext->rhi()->nextResourceUpdateBatch();
659 const QRhiShaderResourceBinding::StageFlags VISIBILITY_ALL =
660 QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage;
661 QSSGRhiShaderResourceBindingList bindings;
662 for (
const QSSGRhiTexture &rhiTex : std::as_const(m_currentTextures)) {
663 int binding = m_currentShaderPipeline->bindingForTexture(rhiTex.name);
666 qCDebug(lcEffectSystem) <<
" -> texture binding" << binding <<
"for" << rhiTex.name;
668 QRhiTexture *texture = rhiTex.texture ? rhiTex.texture
669 : QSSGRhiHelpers::dummyTextureForSampler(
671 QSSGRhiHelpers::samplerType(*m_currentShaderPipeline, rhiTex.name),
673 bindings.addTexture(binding,
674 QRhiShaderResourceBinding::FragmentStage,
676 rhiContext->sampler(rhiTex.samplerDesc));
678 bindings.addUniformBuffer(0, VISIBILITY_ALL, dcd.ubuf);
679 m_currentShaderPipeline->applyAndClearStorageBindings(VISIBILITY_ALL, bindings);
683 renderer->rhiQuadRenderer()->prepareQuad(rhiContext.get(), rub);
685 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
687 QSSGRhiGraphicsPipelineState ps;
688 ps.viewport = QRhiViewport(0, 0,
float(outputSize.width()),
float(outputSize.height()));
689 ps.samples = target->renderTarget->sampleCount();
690 ps.viewCount = viewCount;
691 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, m_currentShaderPipeline);
693 renderer->rhiQuadRenderer()->recordRenderQuadPass(rhiContext.get(), &ps, srb, target->renderTarget, QSSGRhiQuadRenderer::UvCoords);
694 m_currentUbufIndex++;
696 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"post_processing_effect"));
699void QSSGRhiEffectSystem::addCommonEffectUniforms(
const QSSGRenderEffect *inEffect,
const QSize &inputSize,
const QSize &outputSize, quint8 viewCount)
701 const auto &rhiContext = m_sgContext->rhiContext();
702 QRhi *rhi = rhiContext->rhi();
705 if (rhi->isYUpInFramebuffer() != rhi->isYUpInNDC())
706 mvp.data()[5] = -1.0f;
707 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_modelViewProjection", mvp, QSSGRenderShaderValue::Matrix4x4);
709 const bool usesProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix);
710 const bool usesInverseProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix);
711 if (usesProjectionMatrix || usesInverseProjectionMatrix) {
713 if (usesProjectionMatrix)
714 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices[0], QSSGRenderShaderValue::Matrix4x4);
715 if (usesInverseProjectionMatrix)
716 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inverseProjectionMatrix", m_projectionMatrices[0].inverted(), QSSGRenderShaderValue::Matrix4x4);
718 if (usesProjectionMatrix)
719 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
720 if (usesInverseProjectionMatrix) {
721 QVarLengthArray<QMatrix4x4, 2> invertedProjections(viewCount);
722 for (quint8 viewIndex = 0; viewIndex < viewCount; ++viewIndex)
723 invertedProjections[viewIndex] = m_projectionMatrices[viewIndex].inverted();
724 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_inverseProjectionMatrix", invertedProjections.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
729 if (inEffect->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix)) {
731 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices[0], QSSGRenderShaderValue::Matrix4x4);
733 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
736 QVector2D size(inputSize.width(), inputSize.height());
737 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inputSize", size, QSSGRenderShaderValue::Vec2);
739 size = QVector2D(outputSize.width(), outputSize.height());
740 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_outputSize", size, QSSGRenderShaderValue::Vec2);
742 float fc =
float(m_sgContext->renderer()->frameCount());
743 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_frame_num", fc, QSSGRenderShaderValue::Float);
748 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_cameraProperties", m_cameraClipRange, QSSGRenderShaderValue::Vec2);
750 float vp = rhi->isYUpInFramebuffer() ? 1.0f : -1.0f;
751 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_normalAdjustViewportFactor", vp, QSSGRenderShaderValue::Float);
753 const float nearClip = rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f;
754 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_nearClipValue", nearClip, QSSGRenderShaderValue::Float);
756 const QVector4D rhiProperties(
757 rhi->isYUpInFramebuffer() ? 1.0f : -1.0f,
758 rhi->isYUpInNDC() ? 1.0f : -1.0f,
759 rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f,
762 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_rhi_properties", rhiProperties, QSSGRenderShaderValue::Vec4);
764 if (m_depthTexture) {
765 static const QSSGRhiSamplerDescription depthSamplerDesc {
766 QRhiSampler::Nearest, QRhiSampler::Nearest,
768 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
770 addTextureToShaderPipeline(
"qt_depthTexture", m_depthTexture, depthSamplerDesc);
771 addTextureToShaderPipeline(
"qt_depthTextureArray", m_depthTexture, depthSamplerDesc);
774 if (m_normalTexture) {
775 static const QSSGRhiSamplerDescription normalSamplerDesc {
776 QRhiSampler::Nearest, QRhiSampler::Nearest,
778 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
780 addTextureToShaderPipeline(
"qt_normalTexture", m_normalTexture, normalSamplerDesc);
782 if (m_motionVectorTexture) {
783 static const QSSGRhiSamplerDescription motionVectorSamplerDesc {
784 QRhiSampler::Linear, QRhiSampler::Linear,
786 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
788 addTextureToShaderPipeline(
"qt_motionVectorTexture", m_motionVectorTexture, motionVectorSamplerDesc);
792void QSSGRhiEffectSystem::addTextureToShaderPipeline(
const QByteArray &name,
793 QRhiTexture *texture,
794 const QSSGRhiSamplerDescription &samplerDescription)
796 if (!m_currentShaderPipeline)
799 static const QSSGRhiSamplerDescription defaultDescription { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
800 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat };
801 bool validDescription = samplerDescription.magFilter != QRhiSampler::None;
806 m_currentTextures.insert(name, { name, texture, validDescription ? samplerDescription : defaultDescription});
Combined button and popup list for selecting options.
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