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>
15#include <qtquick3d_tracepoints_p.h>
17#include <QtQuick3DUtils/private/qssgassert_p.h>
19#include <QtCore/qloggingcategory.h>
23Q_DECLARE_LOGGING_CATEGORY(lcEffectSystem);
39 delete renderPassDescriptor;
45QSSGRhiEffectSystem::QSSGRhiEffectSystem(
const std::shared_ptr<QSSGRenderContextInterface> &sgContext)
46 : m_sgContext(sgContext)
50QSSGRhiEffectSystem::~QSSGRhiEffectSystem()
53 if (
const auto &rhiCtx = m_sgContext->rhiContext(); rhiCtx && rhiCtx->isValid())
54 QSSGRhiContextPrivate::get(rhiCtx.get())->cleanupDrawCallDataForCid(
this);
58void QSSGRhiEffectSystem::setup(QSize outputSize)
60 if (outputSize.isEmpty()) {
64 m_outSize = outputSize;
67QSSGRhiEffectTexture *QSSGRhiEffectSystem::findTexture(
const QByteArray &bufferName)
69 auto findTexture = [bufferName](
const QSSGRhiEffectTexture *rt){
return rt->name == bufferName; };
70 const auto foundIt = std::find_if(m_textures.cbegin(), m_textures.cend(), findTexture);
71 QSSGRhiEffectTexture *result = foundIt == m_textures.cend() ?
nullptr : *foundIt;
75QSSGRhiEffectTexture *QSSGRhiEffectSystem::getTexture(
const QByteArray &bufferName,
77 QRhiTexture::Format format,
79 const QSSGRenderEffect *inEffect,
82 QSSGRhiEffectTexture *result = findTexture(bufferName);
83 const bool gotMatch = result !=
nullptr;
93 auto findUnused = [](
const QSSGRhiEffectTexture *rt){
return rt->name.isEmpty(); };
94 const auto found = std::find_if(m_textures.cbegin(), m_textures.cend(), findUnused);
95 if (found != m_textures.cend()) {
102 result =
new QSSGRhiEffectTexture {};
103 m_textures.append(result);
106 const auto &rhiCtx = m_sgContext->rhiContext();
107 QRhi *rhi = rhiCtx->rhi();
108 const bool formatChanged = result->texture && result->texture->format() != format;
109 const bool needsRebuild = result->texture && (result->texture->pixelSize() != size || formatChanged);
111 QRhiTexture::Flags flags = QRhiTexture::RenderTarget;
113 flags |= QRhiTexture::UsedAsTransferSource;
115 if (!result->texture) {
117 result->texture = rhi->newTextureArray(format, viewCount, size, 1, flags);
119 result->texture = rhi->newTexture(format, size, 1, flags);
120 result->texture->create();
121 }
else if (needsRebuild) {
122 result->texture->setFlags(flags);
123 result->texture->setPixelSize(size);
124 result->texture->setFormat(format);
125 result->texture->create();
128 if (!result->renderTarget) {
129 QRhiColorAttachment colorAttachment(result->texture);
130 colorAttachment.setMultiViewCount(viewCount);
131 QRhiTextureRenderTargetDescription desc(colorAttachment);
132 result->renderTarget = rhi->newTextureRenderTarget(desc);
133 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
134 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
135 result->renderTarget->create();
136 m_pendingClears.insert(result->renderTarget);
137 }
else if (needsRebuild) {
139 delete result->renderPassDescriptor;
140 result->renderPassDescriptor = result->renderTarget->newCompatibleRenderPassDescriptor();
141 result->renderTarget->setRenderPassDescriptor(result->renderPassDescriptor);
143 result->renderTarget->create();
144 m_pendingClears.insert(result->renderTarget);
148 QByteArray rtName = inEffect->debugObjectName.toLatin1();
149 rtName += QByteArrayLiteral(
" effect pass ");
150 rtName += bufferName;
151 result->renderTarget->setName(rtName);
154 result->name = bufferName;
158void QSSGRhiEffectSystem::releaseTexture(QSSGRhiEffectTexture *texture)
162 if (!texture->flags.isSceneLifetime())
166void QSSGRhiEffectSystem::releaseTextures()
168 for (
auto *t : std::as_const(m_textures))
172QRhiTexture *QSSGRhiEffectSystem::process(
const QSSGRenderLayer &layer,
173 QRhiTexture *inTexture,
174 QRhiTexture *inDepthTexture,
175 QRhiTexture *inNormalTexture,
176 QRhiTexture *inMotionVectorTexture)
178 QSSG_ASSERT(m_sgContext !=
nullptr,
return inTexture);
179 QSSG_ASSERT(layer.firstEffect !=
nullptr,
return inTexture);
180 const auto &rhiContext = m_sgContext->rhiContext();
181 const auto &renderer = m_sgContext->renderer();
182 QSSG_ASSERT(rhiContext && renderer,
return inTexture);
184 const auto viewCount = layer.viewCount;
186 m_depthTexture = inDepthTexture;
187 m_normalTexture = inNormalTexture;
188 m_motionVectorTexture = inMotionVectorTexture;
189 m_cameraClipRange = layer.renderedCameras[0]->clipPlanes;
191 bool usesProjectionMatrix =
false;
192 bool usesViewMatrix =
false;
193 for (
const QSSGRenderEffect *eff = layer.firstEffect; eff; eff = eff->m_nextEffect) {
194 if (eff->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix)
195 || eff->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix))
197 usesProjectionMatrix =
true;
199 if (eff->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix))
200 usesViewMatrix =
true;
203 if (usesProjectionMatrix) {
204 m_projectionMatrices.resize(viewCount);
205 const QMatrix4x4 clipSpaceCorrMatrix = rhiContext->rhi()->clipSpaceCorrMatrix();
206 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
207 m_projectionMatrices[viewIndex] = clipSpaceCorrMatrix * layer.renderedCameras[viewIndex]->projection;
210 if (usesViewMatrix) {
211 QMatrix4x4 camGlobalTransforms[2] { QMatrix4x4{Qt::Uninitialized}, QMatrix4x4{Qt::Uninitialized} };
212 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
213 camGlobalTransforms[viewIndex] = layer.renderData->getGlobalTransform(*layer.renderedCameras[viewIndex]);
214 m_viewMatrices.resize(viewCount);
215 for (
int viewIndex = 0; viewIndex < viewCount; ++viewIndex)
216 m_viewMatrices[viewIndex] = camGlobalTransforms[viewIndex].inverted();
219 m_currentUbufIndex = 0;
221 QSSGRenderEffect *currentEffect =
const_cast<QSSGRenderEffect *>(layer.firstEffect);
222 QSSGRhiEffectTexture firstTex{ inTexture,
nullptr,
nullptr, {}, {}, {} };
223 auto *latestOutput = doRenderEffect(currentEffect, &firstTex, viewCount);
224 firstTex.texture =
nullptr;
226 while ((currentEffect = currentEffect->m_nextEffect)) {
227 QSSGRhiEffectTexture *effectOut = doRenderEffect(currentEffect, latestOutput, viewCount);
228 releaseTexture(latestOutput);
229 latestOutput = effectOut;
233 return latestOutput ? latestOutput->texture :
nullptr;
236void QSSGRhiEffectSystem::releaseResources()
238 qDeleteAll(m_textures);
241 m_shaderPipelines.clear();
244QSSGRenderTextureFormat::Format QSSGRhiEffectSystem::overriddenOutputFormat(
const QSSGRenderEffect *inEffect)
246 QSSGRenderTextureFormat::Format format = QSSGRenderTextureFormat::Unknown;
247 for (
const QSSGCommand *cmd : inEffect->commands) {
248 if (cmd->m_type == CommandType::BindTarget) {
249 const QSSGBindTarget *targetCmd =
static_cast<
const QSSGBindTarget *>(cmd);
250 format = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown
251 ? inEffect->outputFormat : targetCmd->m_outputFormat.format;
257QSSGRhiEffectTexture *QSSGRhiEffectSystem::doRenderEffect(
const QSSGRenderEffect *inEffect,
258 QSSGRhiEffectTexture *inTexture,
262 qCDebug(lcEffectSystem) <<
"START effect " << inEffect->className;
263 QSSGRhiEffectTexture *finalOutputTexture =
nullptr;
264 QSSGRhiEffectTexture *currentOutput =
nullptr;
265 QSSGRhiEffectTexture *currentInput = inTexture;
266 for (
const QSSGCommand *theCommand : inEffect->commands) {
267 qCDebug(lcEffectSystem).noquote() <<
" >" << theCommand->typeAsString() <<
"--" << theCommand->debugString();
269 switch (theCommand->m_type) {
270 case CommandType::AllocateBuffer:
271 allocateBufferCmd(
static_cast<
const QSSGAllocateBuffer *>(theCommand), inTexture, inEffect, viewCount);
274 case CommandType::ApplyBufferValue: {
275 auto *applyCommand =
static_cast<
const QSSGApplyBufferValue *>(theCommand);
278
279
280
281
282
283
284
285
286
288 auto *buffer = applyCommand->m_bufferName.isEmpty() ? inTexture : findTexture(applyCommand->m_bufferName);
289 if (applyCommand->m_samplerName.isEmpty())
290 currentInput = buffer;
292 addTextureToShaderPipeline(applyCommand->m_samplerName, buffer->texture, buffer->desc);
296 case CommandType::ApplyInstanceValue:
297 applyInstanceValueCmd(
static_cast<
const QSSGApplyInstanceValue *>(theCommand), inEffect);
300 case CommandType::ApplyValue:
301 applyValueCmd(
static_cast<
const QSSGApplyValue *>(theCommand), inEffect);
304 case CommandType::BindBuffer: {
305 auto *bindCmd =
static_cast<
const QSSGBindBuffer *>(theCommand);
306 currentOutput = findTexture(bindCmd->m_bufferName);
310 case CommandType::BindShader:
311 bindShaderCmd(
static_cast<
const QSSGBindShader *>(theCommand), inEffect, viewCount);
314 case CommandType::BindTarget: {
315 auto targetCmd =
static_cast<
const QSSGBindTarget*>(theCommand);
317 QSSGRenderTextureFormat::Format f = targetCmd->m_outputFormat == QSSGRenderTextureFormat::Unknown ?
318 inEffect->outputFormat : targetCmd->m_outputFormat.format;
320 QRhiTexture::Format rhiFormat = f == QSSGRenderTextureFormat::Unknown ?
321 currentInput->texture->format() : QSSGBufferManager::toRhiFormat(f);
322 qCDebug(lcEffectSystem) <<
" Target format override" << QSSGBaseTypeHelpers::toString(f) <<
"Effective RHI format" << rhiFormat;
324 QByteArray tmpName = QByteArrayLiteral(
"__output_").append(QByteArray::number(m_currentUbufIndex));
325 currentOutput = getTexture(tmpName, m_outSize, rhiFormat,
true, inEffect, viewCount);
326 finalOutputTexture = currentOutput;
330 case CommandType::Render:
331 renderCmd(inEffect, currentInput, currentOutput, viewCount);
332 currentInput = inTexture;
336 qWarning() <<
"Effect command" << theCommand->typeAsString() <<
"not implemented";
341 qCDebug(lcEffectSystem) <<
"END effect " << inEffect->className;
342 return finalOutputTexture;
345void QSSGRhiEffectSystem::allocateBufferCmd(
const QSSGAllocateBuffer *inCmd,
346 QSSGRhiEffectTexture *inTexture,
347 const QSSGRenderEffect *inEffect,
351 QSize bufferSize(m_outSize * qreal(inCmd->m_sizeMultiplier));
353 QSSGRenderTextureFormat f = inCmd->m_format;
354 QRhiTexture::Format rhiFormat = (f == QSSGRenderTextureFormat::Unknown) ? inTexture->texture->format()
355 : QSSGBufferManager::toRhiFormat(f);
357 QSSGRhiEffectTexture *buf = getTexture(inCmd->m_name, bufferSize, rhiFormat,
false, inEffect, viewCount);
358 auto filter = QSSGRhiHelpers::toRhi(inCmd->m_filterOp);
359 auto tiling = QSSGRhiHelpers::toRhi(inCmd->m_texCoordOp);
360 buf->desc = { filter, filter, QRhiSampler::None, tiling, tiling, QRhiSampler::Repeat };
361 buf->flags = inCmd->m_bufferFlags;
364void QSSGRhiEffectSystem::applyInstanceValueCmd(
const QSSGApplyInstanceValue *inCmd,
const QSSGRenderEffect *inEffect)
366 if (!m_currentShaderPipeline)
369 const bool setAll = inCmd->m_propertyName.isEmpty();
370 for (
const QSSGRenderEffect::Property &property : std::as_const(inEffect->properties)) {
371 if (setAll || property.name == inCmd->m_propertyName) {
372 m_currentShaderPipeline->setUniformValue(m_currentUBufData, property.name, property.value, property.shaderDataType);
376 for (
const QSSGRenderEffect::TextureProperty &textureProperty : std::as_const(inEffect->textureProperties)) {
377 if (setAll || textureProperty.name == inCmd->m_propertyName) {
378 bool texAdded =
false;
379 QSSGRenderImage *image = textureProperty.texImage;
381 const auto &theBufferManager(m_sgContext->bufferManager());
382 const QSSGRenderImageTexture texture = theBufferManager->loadRenderImage(image);
383 if (texture.m_texture) {
384 const QSSGRhiSamplerDescription desc{
385 QSSGRhiHelpers::toRhi(textureProperty.minFilterType),
386 QSSGRhiHelpers::toRhi(textureProperty.magFilterType),
387 textureProperty.mipFilterType != QSSGRenderTextureFilterOp::None ? QSSGRhiHelpers::toRhi(textureProperty.mipFilterType) : QRhiSampler::None,
388 QSSGRhiHelpers::toRhi(textureProperty.horizontalClampType),
389 QSSGRhiHelpers::toRhi(textureProperty.verticalClampType),
390 QSSGRhiHelpers::toRhi(textureProperty.zClampType)
392 addTextureToShaderPipeline(textureProperty.name, texture.m_texture, desc);
398 qCDebug(lcEffectSystem) <<
"Using dummy texture for property" << textureProperty.name;
399 addTextureToShaderPipeline(textureProperty.name,
nullptr, {});
405void QSSGRhiEffectSystem::applyValueCmd(
const QSSGApplyValue *inCmd,
const QSSGRenderEffect *inEffect)
407 if (!m_currentShaderPipeline)
410 const auto &properties = inEffect->properties;
411 const auto foundIt = std::find_if(properties.cbegin(), properties.cend(), [inCmd](
const QSSGRenderEffect::Property &prop) {
412 return (prop.name == inCmd->m_propertyName);
415 if (foundIt != properties.cend())
416 m_currentShaderPipeline->setUniformValue(m_currentUBufData, inCmd->m_propertyName, inCmd->m_value, foundIt->shaderDataType);
418 qWarning() <<
"Could not find effect property" << inCmd->m_propertyName;
422 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
428 "vec2 qt_effectTextureMapUV(vec2 uv)\n"
430 " return vec2(uv.x, 1.0 - uv.y);\n"
433QSSGRhiShaderPipelinePtr QSSGRhiEffectSystem::buildShaderForEffect(
const QSSGBindShader &inCmd,
434 QSSGProgramGenerator &generator,
435 QSSGShaderLibraryManager &shaderLib,
436 QSSGShaderCache &shaderCache,
437 bool isYUpInFramebuffer,
440 const auto &key = inCmd.m_shaderPathKey;
441 qCDebug(lcEffectSystem) <<
" generating new shader pipeline for: " << key;
443 generator.beginProgram();
446 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Vertex);
447 QSSGStageGeneratorBase *vStage = generator.getStage(QSSGShaderGeneratorStage::Vertex);
450 vStage->append(isYUpInFramebuffer ? effect_builtin_textureMapUV : effect_builtin_textureMapUVFlipped);
454 const QByteArray src = shaderLib.getShaderSource(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment);
455 QSSGStageGeneratorBase *fStage = generator.getStage(QSSGShaderGeneratorStage::Fragment);
459 return generator.compileGeneratedRhiShader(key,
460 shaderLib.getShaderMetaData(inCmd.m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features,
463 QSSGRhiShaderPipeline::UsedWithoutIa,
469void QSSGRhiEffectSystem::bindShaderCmd(
const QSSGBindShader *inCmd,
const QSSGRenderEffect *inEffect, quint8 viewCount)
474 m_currentTextures.clear();
475 m_pendingClears.clear();
476 m_currentShaderPipeline =
nullptr;
478 const auto &rhiCtx = m_sgContext->rhiContext();
479 QRhi *rhi = rhiCtx->rhi();
480 const auto &shaderLib = m_sgContext->shaderLibraryManager();
481 const auto &shaderCache = m_sgContext->shaderCache();
488 QSSGEffectSceneCacheKey cacheKey;
489 cacheKey.m_shaderPathKey = inCmd->m_shaderPathKey;
490 cacheKey.m_cmd = quintptr(inCmd);
491 cacheKey.m_ubufIndex = m_currentUbufIndex;
492 cacheKey.updateHashCode();
495 const auto it = m_shaderPipelines.constFind(cacheKey);
496 if (it != m_shaderPipelines.cend())
497 m_currentShaderPipeline = (*it).get();
500 QSSGShaderFeatures features;
501 if (!m_currentShaderPipeline) {
502 features = shaderLib->getShaderMetaData(inCmd->m_shaderPathKey, QSSGShaderCache::ShaderType::Fragment).features;
503 qsbcKey = QQsbCollection::EntryDesc::generateSha(inCmd->m_shaderPathKey, QQsbCollection::toFeatureSet(features));
507 if (!m_currentShaderPipeline && !shaderLib->m_preGeneratedShaderEntries.isEmpty()) {
508 const QQsbCollection::EntryMap &pregenEntries = shaderLib->m_preGeneratedShaderEntries;
509 const auto foundIt = pregenEntries.constFind(QQsbCollection::Entry(qsbcKey));
510 if (foundIt != pregenEntries.cend()) {
514 const auto &shader = shaderCache->newPipelineFromPregenerated(inCmd->m_shaderPathKey,
518 QSSGRhiShaderPipeline::UsedWithoutIa);
519 m_shaderPipelines.insert(cacheKey, shader);
520 m_currentShaderPipeline = shader.get();
524 if (!m_currentShaderPipeline) {
530 const auto &shaderPipeline = shaderCache->tryNewPipelineFromPersistentCache(qsbcKey,
531 inCmd->m_shaderPathKey,
533 QSSGRhiShaderPipeline::UsedWithoutIa);
534 if (shaderPipeline) {
535 m_shaderPipelines.insert(cacheKey, shaderPipeline);
536 m_currentShaderPipeline = shaderPipeline.get();
540 if (!m_currentShaderPipeline) {
542 Q_TRACE_SCOPE(QSSG_generateShader);
543 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DGenerateShader);
544 const auto &generator = m_sgContext->shaderProgramGenerator();
545 if (
auto stages = buildShaderForEffect(*inCmd, *generator, *shaderLib, *shaderCache, rhi->isYUpInFramebuffer(), viewCount)) {
546 m_shaderPipelines.insert(cacheKey, stages);
547 m_currentShaderPipeline = stages.get();
549 Q_QUICK3D_PROFILE_END_WITH_ID(QQuick3DProfiler::Quick3DGenerateShader, 0, inEffect->profilingId);
552 const auto &rhiContext = m_sgContext->rhiContext();
554 if (m_currentShaderPipeline) {
555 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
556 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
557 QSSGRhiDrawCallData &dcd = QSSGRhiContextPrivate::get(rhiContext.get())->drawCallData({ cacheKey1, cacheKey2,
558 nullptr,
nullptr, 0 });
559 m_currentShaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
560 m_currentUBufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
562 m_currentUBufData =
nullptr;
565 QSSGRhiContextStats::get(*rhiContext).registerEffectShaderGenerationTime(timer.elapsed());
568void QSSGRhiEffectSystem::renderCmd(
const QSSGRenderEffect *inEffect, QSSGRhiEffectTexture *inTexture, QSSGRhiEffectTexture *target, quint8 viewCount)
570 if (!m_currentShaderPipeline)
574 qWarning(
"No effect render target?");
579 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTexture"), inTexture->texture, inTexture->desc);
580 addTextureToShaderPipeline(QByteArrayLiteral(
"qt_inputTextureArray"), inTexture->texture, inTexture->desc);
582 const auto &rhiContext = m_sgContext->rhiContext();
583 const auto &renderer = m_sgContext->renderer();
585 QRhiCommandBuffer *cb = rhiContext->commandBuffer();
586 cb->debugMarkBegin(QByteArrayLiteral(
"Post-processing effect"));
587 Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass);
589 for (QRhiTextureRenderTarget *rt : std::as_const(m_pendingClears)) {
595 if (rt != target->renderTarget) {
596 cb->beginPass(rt, Qt::transparent, { 1.0f, 0 },
nullptr, rhiContext->commonPassFlags());
597 QSSGRHICTX_STAT(rhiContext, beginRenderPass(rt));
599 QSSGRHICTX_STAT(rhiContext, endRenderPass());
602 m_pendingClears.clear();
604 const QSize inputSize = inTexture->texture->pixelSize();
605 const QSize outputSize = target->texture->pixelSize();
606 addCommonEffectUniforms(inEffect, inputSize, outputSize, viewCount);
608 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiContext.get());
610 const void *cacheKey1 =
reinterpret_cast<
const void *>(
this);
611 const void *cacheKey2 =
reinterpret_cast<
const void *>(qintptr(m_currentUbufIndex));
612 QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData({ cacheKey1, cacheKey2,
nullptr,
nullptr, 0 });
613 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
614 m_currentUBufData =
nullptr;
616 QRhiResourceUpdateBatch *rub = rhiContext->rhi()->nextResourceUpdateBatch();
617 renderer->rhiQuadRenderer()->prepareQuad(rhiContext.get(), rub);
620 const QRhiShaderResourceBinding::StageFlags VISIBILITY_ALL =
621 QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage;
622 QSSGRhiShaderResourceBindingList bindings;
623 for (
const QSSGRhiTexture &rhiTex : std::as_const(m_currentTextures)) {
624 int binding = m_currentShaderPipeline->bindingForTexture(rhiTex.name);
627 qCDebug(lcEffectSystem) <<
" -> texture binding" << binding <<
"for" << rhiTex.name;
629 QRhiTexture *texture = rhiTex.texture ? rhiTex.texture : rhiContext->dummyTexture({}, rub);
630 bindings.addTexture(binding,
631 QRhiShaderResourceBinding::FragmentStage,
633 rhiContext->sampler(rhiTex.samplerDesc));
635 bindings.addUniformBuffer(0, VISIBILITY_ALL, dcd.ubuf);
637 QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings);
639 QSSGRhiGraphicsPipelineState ps;
640 ps.viewport = QRhiViewport(0, 0,
float(outputSize.width()),
float(outputSize.height()));
641 ps.samples = target->renderTarget->sampleCount();
642 ps.viewCount = viewCount;
643 QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, m_currentShaderPipeline);
645 renderer->rhiQuadRenderer()->recordRenderQuadPass(rhiContext.get(), &ps, srb, target->renderTarget, QSSGRhiQuadRenderer::UvCoords);
646 m_currentUbufIndex++;
648 Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral(
"post_processing_effect"));
651void QSSGRhiEffectSystem::addCommonEffectUniforms(
const QSSGRenderEffect *inEffect,
const QSize &inputSize,
const QSize &outputSize, quint8 viewCount)
653 const auto &rhiContext = m_sgContext->rhiContext();
654 QRhi *rhi = rhiContext->rhi();
657 if (rhi->isYUpInFramebuffer() != rhi->isYUpInNDC())
658 mvp.data()[5] = -1.0f;
659 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_modelViewProjection", mvp, QSSGRenderShaderValue::Matrix4x4);
661 const bool usesProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesProjectionMatrix);
662 const bool usesInverseProjectionMatrix = inEffect->testFlag(QSSGRenderEffect::Flags::UsesInverseProjectionMatrix);
663 if (usesProjectionMatrix || usesInverseProjectionMatrix) {
665 if (usesProjectionMatrix)
666 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices[0], QSSGRenderShaderValue::Matrix4x4);
667 if (usesInverseProjectionMatrix)
668 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inverseProjectionMatrix", m_projectionMatrices[0].inverted(), QSSGRenderShaderValue::Matrix4x4);
670 if (usesProjectionMatrix)
671 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_projectionMatrix", m_projectionMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
672 if (usesInverseProjectionMatrix) {
673 QVarLengthArray<QMatrix4x4, 2> invertedProjections(viewCount);
674 for (quint8 viewIndex = 0; viewIndex < viewCount; ++viewIndex)
675 invertedProjections[viewIndex] = m_projectionMatrices[viewIndex].inverted();
676 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_inverseProjectionMatrix", invertedProjections.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
681 if (inEffect->testFlag(QSSGRenderEffect::Flags::UsesViewMatrix)) {
683 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices[0], QSSGRenderShaderValue::Matrix4x4);
685 m_currentShaderPipeline->setUniformArray(m_currentUBufData,
"qt_viewMatrix", m_viewMatrices.constData(), viewCount, QSSGRenderShaderValue::Matrix4x4);
688 QVector2D size(inputSize.width(), inputSize.height());
689 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_inputSize", size, QSSGRenderShaderValue::Vec2);
691 size = QVector2D(outputSize.width(), outputSize.height());
692 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_outputSize", size, QSSGRenderShaderValue::Vec2);
694 float fc =
float(m_sgContext->renderer()->frameCount());
695 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_frame_num", fc, QSSGRenderShaderValue::Float);
700 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_cameraProperties", m_cameraClipRange, QSSGRenderShaderValue::Vec2);
702 float vp = rhi->isYUpInFramebuffer() ? 1.0f : -1.0f;
703 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_normalAdjustViewportFactor", vp, QSSGRenderShaderValue::Float);
705 const float nearClip = rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f;
706 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_nearClipValue", nearClip, QSSGRenderShaderValue::Float);
708 const QVector4D rhiProperties(
709 rhi->isYUpInFramebuffer() ? 1.0f : -1.0f,
710 rhi->isYUpInNDC() ? 1.0f : -1.0f,
711 rhi->isClipDepthZeroToOne() ? 0.0f : -1.0f,
714 m_currentShaderPipeline->setUniformValue(m_currentUBufData,
"qt_rhi_properties", rhiProperties, QSSGRenderShaderValue::Vec4);
716 if (m_depthTexture) {
717 static const QSSGRhiSamplerDescription depthSamplerDesc {
718 QRhiSampler::Nearest, QRhiSampler::Nearest,
720 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
722 addTextureToShaderPipeline(
"qt_depthTexture", m_depthTexture, depthSamplerDesc);
723 addTextureToShaderPipeline(
"qt_depthTextureArray", m_depthTexture, depthSamplerDesc);
726 if (m_normalTexture) {
727 static const QSSGRhiSamplerDescription normalSamplerDesc {
728 QRhiSampler::Nearest, QRhiSampler::Nearest,
730 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
732 addTextureToShaderPipeline(
"qt_normalTexture", m_normalTexture, normalSamplerDesc);
734 if (m_motionVectorTexture) {
735 static const QSSGRhiSamplerDescription motionVectorSamplerDesc {
736 QRhiSampler::Linear, QRhiSampler::Linear,
738 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
740 addTextureToShaderPipeline(
"qt_motionVectorTexture", m_motionVectorTexture, motionVectorSamplerDesc);
744void QSSGRhiEffectSystem::addTextureToShaderPipeline(
const QByteArray &name,
745 QRhiTexture *texture,
746 const QSSGRhiSamplerDescription &samplerDescription)
748 if (!m_currentShaderPipeline)
751 static const QSSGRhiSamplerDescription defaultDescription { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
752 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat };
753 bool validDescription = samplerDescription.magFilter != QRhiSampler::None;
758 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