185void QSSGCustomMaterialSystem::rhiPrepareRenderable(QSSGRhiGraphicsPipelineState *ps,
187 QSSGSubsetRenderable &renderable,
188 const QSSGShaderFeatures &featureSet,
189 const QSSGRenderCustomMaterial &material,
190 const QSSGLayerRenderData &layerData,
191 QRhiRenderPassDescriptor *renderPassDescriptor,
194 QSSGRenderCamera *alteredCamera,
195 QSSGRenderTextureCubeFace cubeFace,
196 QMatrix4x4 *alteredModelViewProjection,
197 QSSGReflectionMapEntry *entry,
200 QSSGRhiContext *rhiCtx = context->rhiContext().get();
202 QRhiGraphicsPipeline::TargetBlend blend;
203 if (material.m_renderFlags.testFlag(QSSGRenderCustomMaterial::RenderFlag::Blending)) {
205 blend.srcColor = material.m_srcBlend;
206 blend.srcAlpha = material.m_srcAlphaBlend;
207 blend.dstColor = material.m_dstBlend;
208 blend.dstAlpha = material.m_dstAlphaBlend;
211 const QSSGCullFaceMode cullMode = material.m_cullMode;
213 const auto &defaultMaterialShaderKeyProperties = layerData.getDefaultMaterialPropertyTable();
215 const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(renderable.shaderDescription);
217 const auto &shaderPipeline = shadersForCustomMaterial(ps, material, renderable, defaultMaterialShaderKeyProperties, featureSet);
219 if (shaderPipeline) {
220 QSSGRhiShaderResourceBindingList bindings;
221 const auto &modelNode = renderable.modelContext.model;
227 const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(cubeFace);
228 const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * (cubeFaceIdx + (quintptr(renderable.subset.offset) << 3));
230 const auto entryPartA =
reinterpret_cast<quintptr>(&material);
231 const auto entryPartB =
reinterpret_cast<quintptr>(entry);
232 const void *entryKey =
reinterpret_cast<
const void *>(entryPartA ^ entryPartB);
234 QSSGRhiDrawCallData &dcd = QSSGRhiContextPrivate::get(rhiCtx)->drawCallData({ passKey, &modelNode, entryKey, entryIdx });
236 shaderPipeline->ensureCombinedUniformBuffer(&dcd.ubuf);
237 char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame();
238 if (!alteredCamera) {
239 updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, layerData, ubufData, ps, material, renderable, layerData.renderedCameras,
nullptr,
nullptr);
241 QSSGRenderCameraList cameras({ alteredCamera });
242 updateUniformsForCustomMaterial(*shaderPipeline, rhiCtx, layerData, ubufData, ps, material, renderable, cameras,
nullptr, alteredModelViewProjection);
245 QSSGParticleRenderer::updateUniformsForParticleModel(*shaderPipeline, ubufData, &renderable.modelContext.model, renderable.subset.offset);
246 dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame();
249 QSSGParticleRenderer::prepareParticlesForModel(*shaderPipeline, rhiCtx, bindings, &renderable.modelContext.model);
250 bool instancing =
false;
251 if (!alteredCamera) {
252 const QSSGRenderCameraDataList &cameraDatas(*layerData.renderedCameraData);
253 instancing = QSSGLayerRenderData::prepareInstancing(rhiCtx, &renderable, cameraDatas[0].direction, cameraDatas[0].position, renderable.instancingLodMin, renderable.instancingLodMax);
255 const auto &altCamTransform = layerData.getGlobalTransform(*alteredCamera);
256 instancing = QSSGLayerRenderData::prepareInstancing(rhiCtx, &renderable, QSSGRenderNode::getScalingCorrectDirection(altCamTransform), QSSGRenderNode::getGlobalPos(altCamTransform), renderable.instancingLodMin, renderable.instancingLodMax);
259 ps->samples = samples;
260 ps->viewCount = viewCount;
262 ps->cullMode = QSSGRhiHelpers::toCullMode(cullMode);
265 ps->targetBlend[0] = blend;
267 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
269 ia = renderable.subset.rhi.ia;
272 int instanceBufferBinding = 0;
275 const quint32 stride = renderable.modelContext.model.instanceTable->stride();
276 QVarLengthArray<QRhiVertexInputBinding, 8> bindings;
277 std::copy(ia.inputLayout.cbeginBindings(),
278 ia.inputLayout.cendBindings(),
279 std::back_inserter(bindings));
280 bindings.append({ stride, QRhiVertexInputBinding::PerInstance });
281 instanceBufferBinding = bindings.size() - 1;
282 ia.inputLayout.setBindings(bindings.cbegin(), bindings.cend());
285 QSSGRhiHelpers::bakeVertexInputLocations(&ia, *shaderPipeline, instanceBufferBinding);
287 QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch();
288 QRhiTexture *dummyTexture = rhiCtx->dummyTexture({}, resourceUpdates);
289 QRhiTexture *dummyTexture3D = rhiCtx->dummyTexture(QRhiTexture::ThreeDimensional, resourceUpdates);
290 QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(QRhiTexture::CubeMap, resourceUpdates);
291 rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates);
293 bindings.addUniformBuffer(0, CUSTOM_MATERIAL_VISIBILITY_ALL, dcd.ubuf, 0, shaderPipeline->ub0Size());
294 bindings.addUniformBuffer(1, CUSTOM_MATERIAL_VISIBILITY_ALL, dcd.ubuf, shaderPipeline->ub0LightDataOffset(),
sizeof(QSSGShaderLightsUniformData));
295 bindings.addUniformBuffer(2, CUSTOM_MATERIAL_VISIBILITY_ALL, dcd.ubuf, shaderPipeline->ub0DirectionalLightDataOffset(),
sizeof(QSSGShaderDirectionalLightsUniformData));
297 QVector<QShaderDescription::InOutVariable> samplerVars =
298 shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers();
299 for (
const QShaderDescription::InOutVariable &var : shaderPipeline->vertexStage()->shader().description().combinedImageSamplers()) {
300 auto it = std::find_if(samplerVars.cbegin(), samplerVars.cend(),
301 [&var](
const QShaderDescription::InOutVariable &v) {
return var.binding == v.binding; });
302 if (it == samplerVars.cend())
303 samplerVars.append(var);
306 int maxSamplerBinding = -1;
307 for (
const QShaderDescription::InOutVariable &var : samplerVars)
308 maxSamplerBinding = qMax(maxSamplerBinding, var.binding);
317 QBitArray samplerBindingsSpecified(maxSamplerBinding + 1);
320 samplerBindingsSpecified.setBit(shaderPipeline->bindingForTexture(
"qt_particleTexture"));
323 if (QRhiTexture *boneTexture = layerData.getBonemapTexture(renderable.modelContext)) {
324 int binding = shaderPipeline->bindingForTexture(
"qt_boneTexture");
326 QRhiSampler *boneSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
327 QRhiSampler::Nearest,
329 QRhiSampler::ClampToEdge,
330 QRhiSampler::ClampToEdge,
333 bindings.addTexture(binding,
334 QRhiShaderResourceBinding::VertexStage,
337 samplerBindingsSpecified.setBit(binding);
342 auto *targetsTexture = renderable.subset.rhi.targetsTexture;
343 if (targetsTexture) {
344 int binding = shaderPipeline->bindingForTexture(
"qt_morphTargetTexture");
346 QRhiSampler *targetsSampler = rhiCtx->sampler({ QRhiSampler::Nearest,
347 QRhiSampler::Nearest,
349 QRhiSampler::ClampToEdge,
350 QRhiSampler::ClampToEdge,
351 QRhiSampler::ClampToEdge
353 bindings.addTexture(binding, QRhiShaderResourceBinding::VertexStage, renderable.subset.rhi.targetsTexture, targetsSampler);
354 samplerBindingsSpecified.setBit(binding);
361 if (featureSet.isSet(QSSGShaderFeatures::Feature::ReflectionProbe)) {
362 int reflectionSampler = shaderPipeline->bindingForTexture(
"qt_reflectionMap");
363 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear,
364 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
365 QRhiTexture* reflectionTexture = layerData.getReflectionMapManager()->reflectionMapEntry(renderable.reflectionProbeIndex)->m_rhiPrefilteredCube;
366 if (reflectionSampler >= 0 && reflectionTexture) {
367 bindings.addTexture(reflectionSampler, QRhiShaderResourceBinding::FragmentStage, reflectionTexture, sampler);
368 samplerBindingsSpecified.setBit(reflectionSampler);
370 }
else if (shaderPipeline->lightProbeTexture()) {
371 int binding = shaderPipeline->bindingForTexture(
"qt_lightProbe",
int(QSSGRhiSamplerBindingHints::LightProbe));
373 samplerBindingsSpecified.setBit(binding);
374 QPair<QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp> tiling = shaderPipeline->lightProbeTiling();
375 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear,
376 QSSGRhiHelpers::toRhi(tiling.first), QSSGRhiHelpers::toRhi(tiling.second), QRhiSampler::Repeat });
377 bindings.addTexture(binding,
378 QRhiShaderResourceBinding::FragmentStage,
379 shaderPipeline->lightProbeTexture(), sampler);
383 if (shaderPipeline->screenTexture()) {
384 const int screenTextureBinding = shaderPipeline->bindingForTexture(
"qt_screenTexture",
int(QSSGRhiSamplerBindingHints::ScreenTexture));
385 const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_screenTextureArray",
int(QSSGRhiSamplerBindingHints::ScreenTextureArray));
386 if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) {
391 QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(QRhiTexture::MipMapped)
392 ? QRhiSampler::Linear : QRhiSampler::None;
393 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, mipFilter,
394 QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
395 if (screenTextureBinding >= 0) {
396 samplerBindingsSpecified.setBit(screenTextureBinding);
397 bindings.addTexture(screenTextureBinding,
398 QRhiShaderResourceBinding::FragmentStage,
399 shaderPipeline->screenTexture(), sampler);
401 if (screenTextureArrayBinding >= 0) {
402 samplerBindingsSpecified.setBit(screenTextureArrayBinding);
403 bindings.addTexture(screenTextureArrayBinding,
404 QRhiShaderResourceBinding::FragmentStage,
405 shaderPipeline->screenTexture(), sampler);
410 if (shaderPipeline->depthTexture()) {
411 const int depthTextureBinding = shaderPipeline->bindingForTexture(
"qt_depthTexture",
int(QSSGRhiSamplerBindingHints::DepthTexture));
412 const int depthTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_depthTextureArray",
int(QSSGRhiSamplerBindingHints::DepthTextureArray));
413 if (depthTextureBinding >= 0 || depthTextureArrayBinding >= 0) {
415 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
416 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
417 if (depthTextureBinding >= 0) {
418 samplerBindingsSpecified.setBit(depthTextureBinding);
419 bindings.addTexture(depthTextureBinding,
420 CUSTOM_MATERIAL_VISIBILITY_ALL,
421 shaderPipeline->depthTexture(), sampler);
423 if (depthTextureArrayBinding >= 0) {
424 samplerBindingsSpecified.setBit(depthTextureArrayBinding);
425 bindings.addTexture(depthTextureArrayBinding,
426 CUSTOM_MATERIAL_VISIBILITY_ALL,
427 shaderPipeline->depthTexture(), sampler);
432 if (shaderPipeline->normalTexture()) {
433 const int normalTextureBinding = shaderPipeline->bindingForTexture(
"qt_normalTexture",
int(QSSGRhiSamplerBindingHints::NormalTexture));
434 if (normalTextureBinding >= 0) {
436 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
437 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
438 samplerBindingsSpecified.setBit(normalTextureBinding);
439 bindings.addTexture(normalTextureBinding, CUSTOM_MATERIAL_VISIBILITY_ALL, shaderPipeline->normalTexture(), sampler);
443 if (shaderPipeline->ssaoTexture()) {
444 const int ssaoTextureBinding = shaderPipeline->bindingForTexture(
"qt_aoTexture",
int(QSSGRhiSamplerBindingHints::AoTexture));
445 const int ssaoTextureArrayBinding = shaderPipeline->bindingForTexture(
"qt_aoTextureArray",
int(QSSGRhiSamplerBindingHints::AoTextureArray));
446 if (ssaoTextureBinding >= 0 || ssaoTextureArrayBinding >= 0) {
448 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
449 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
450 if (ssaoTextureBinding >= 0) {
451 samplerBindingsSpecified.setBit(ssaoTextureBinding);
452 bindings.addTexture(ssaoTextureBinding,
453 QRhiShaderResourceBinding::FragmentStage,
454 shaderPipeline->ssaoTexture(), sampler);
456 if (ssaoTextureArrayBinding >= 0) {
457 samplerBindingsSpecified.setBit(ssaoTextureArrayBinding);
458 bindings.addTexture(ssaoTextureArrayBinding,
459 QRhiShaderResourceBinding::FragmentStage,
460 shaderPipeline->ssaoTexture(), sampler);
465 if (shaderPipeline->lightmapTexture()) {
466 int binding = shaderPipeline->bindingForTexture(
"qt_lightmap",
int(QSSGRhiSamplerBindingHints::LightmapTexture));
468 samplerBindingsSpecified.setBit(binding);
469 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
470 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
471 bindings.addTexture(binding,
472 QRhiShaderResourceBinding::FragmentStage,
473 shaderPipeline->lightmapTexture(), sampler);
477 if (shaderPipeline->MotionVectorTexture()) {
478 int binding = shaderPipeline->bindingForTexture(
"qt_motionVectorTexture",
int(QSSGRhiSamplerBindingHints::MotionVectorTexture));
480 samplerBindingsSpecified.setBit(binding);
481 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
482 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
483 bindings.addTexture(binding,
484 QRhiShaderResourceBinding::FragmentStage,
485 shaderPipeline->MotionVectorTexture(), sampler);
491 auto shadowMapAtlas = shaderPipeline->shadowMapAtlasTexture();
492 if (shadowMapAtlas) {
493 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_texture");
495 samplerBindingsSpecified.setBit(binding);
496 QRhiTexture *texture = shadowMapAtlas;
497 QRhiSampler *sampler = rhiCtx->sampler({ QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
498 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
499 Q_ASSERT(texture && sampler);
500 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
505 if (
auto shadowMapBlueNoise = shaderPipeline->shadowMapBlueNoiseTexture()) {
506 int binding = shaderPipeline->bindingForTexture(
"qt_shadowmap_blue_noise_texture");
508 samplerBindingsSpecified.setBit(binding);
509 QRhiTexture *texture = shadowMapBlueNoise;
510 QRhiSampler *sampler = rhiCtx->sampler(
511 { QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::Repeat, QRhiSampler::Repeat, QRhiSampler::Repeat });
512 Q_ASSERT(texture && sampler);
513 bindings.addTexture(binding, QRhiShaderResourceBinding::FragmentStage, texture, sampler);
517 QSSGRenderableImage *renderableImage = renderable.firstImage;
518 while (renderableImage) {
519 const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(renderableImage->m_mapType);
520 const int samplerHint =
int(renderableImage->m_mapType);
521 int samplerBinding = shaderPipeline->bindingForTexture(samplerName, samplerHint);
522 if (samplerBinding >= 0) {
523 QRhiTexture *texture = renderableImage->m_texture.m_texture;
524 if (samplerBinding >= 0 && texture) {
525 const bool mipmapped = texture->flags().testFlag(QRhiTexture::MipMapped);
526 QSSGRhiSamplerDescription samplerDesc = {
527 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_minFilterType),
528 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_magFilterType),
529 mipmapped ? QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None,
530 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_horizontalTilingMode),
531 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_verticalTilingMode),
532 QSSGRhiHelpers::toRhi(renderableImage->m_imageNode.m_depthTilingMode)
534 rhiCtx->checkAndAdjustForNPoT(texture, &samplerDesc);
535 QRhiSampler *sampler = rhiCtx->sampler(samplerDesc);
536 samplerBindingsSpecified.setBit(samplerBinding);
537 bindings.addTexture(samplerBinding,
538 CUSTOM_MATERIAL_VISIBILITY_ALL,
542 renderableImage = renderableImage->m_nextImage;
545 if (maxSamplerBinding >= 0) {
547 int customTexCount = shaderPipeline->extraTextureCount();
548 for (
int i = 0; i < customTexCount; ++i) {
549 QSSGRhiTexture &t(shaderPipeline->extraTextureAt(i));
550 const int samplerBinding = shaderPipeline->bindingForTexture(t.name);
551 if (samplerBinding >= 0) {
552 samplerBindingsSpecified.setBit(samplerBinding);
553 rhiCtx->checkAndAdjustForNPoT(t.texture, &t.samplerDesc);
554 QRhiSampler *sampler = rhiCtx->sampler(t.samplerDesc);
555 bindings.addTexture(samplerBinding,
556 CUSTOM_MATERIAL_VISIBILITY_ALL,
563 QRhiSampler *dummySampler = rhiCtx->sampler({ QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
564 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat });
566 for (
const QShaderDescription::InOutVariable &var : samplerVars) {
567 if (!samplerBindingsSpecified.testBit(var.binding)) {
568 QRhiTexture *t =
nullptr;
569 if (var.type == QShaderDescription::SamplerCube)
570 t = dummyCubeTexture;
571 else if (var.type == QShaderDescription::Sampler3D)
575 bindings.addTexture(var.binding, CUSTOM_MATERIAL_VISIBILITY_ALL, t, dummySampler);
580 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
582 if (oit && layerData.layer.oitMethod == QSSGRenderLayer::OITMethod::LinkedList)
583 RenderHelpers::addAccumulatorImageBindings(shaderPipeline.get(), bindings);
586 QRhiShaderResourceBindings *&srb = dcd.srb;
587 bool srbChanged =
false;
588 if (!srb || bindings != dcd.bindings) {
589 srb = rhiCtxD->srb(bindings);
590 rhiCtxD->releaseCachedSrb(dcd.bindings);
591 dcd.bindings = bindings;
595 if (cubeFace == QSSGRenderTextureCubeFaceNone)
596 renderable.rhiRenderData.mainPass.srb = srb;
598 renderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx] = srb;
600 const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(*ps, renderPassDescriptor, srb);
603 && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash
604 && dcd.renderTargetDescription == pipelineKey.renderTargetDescription
607 if (cubeFace == QSSGRenderTextureCubeFaceNone)
608 renderable.rhiRenderData.mainPass.pipeline = dcd.pipeline;
610 renderable.rhiRenderData.reflectionPass.pipeline = dcd.pipeline;
612 if (cubeFace == QSSGRenderTextureCubeFaceNone) {
613 renderable.rhiRenderData.mainPass.pipeline = rhiCtxD->pipeline(pipelineKey,
614 renderPassDescriptor,
616 dcd.pipeline = renderable.rhiRenderData.mainPass.pipeline;
618 renderable.rhiRenderData.reflectionPass.pipeline = rhiCtxD->pipeline(pipelineKey,
619 renderPassDescriptor,
621 dcd.pipeline = renderable.rhiRenderData.reflectionPass.pipeline;
624 dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash;
625 dcd.renderTargetDescription = pipelineKey.renderTargetDescription;