1 | // Copyright (C) 2023 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qssgdebugdrawsystem_p.h" |
5 | #include "qssgrenderhelpers_p.h" |
6 | |
7 | #include "qssgrenderer_p.h" |
8 | #include "qssglayerrenderdata_p.h" |
9 | #include "qssgrhiparticles_p.h" |
10 | #include "qssgrhiquadrenderer_p.h" |
11 | #include "../qssgrendercontextcore.h" |
12 | #include "../qssgrhicustommaterialsystem_p.h" |
13 | #include "../resourcemanager/qssgrenderbuffermanager_p.h" |
14 | #include "../qssgrenderdefaultmaterialshadergenerator_p.h" |
15 | #include "rendererimpl/qssgshadowmaphelpers_p.h" |
16 | #include <QtQuick3DUtils/private/qssgassert_p.h> |
17 | |
18 | #include <QtCore/qbitarray.h> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | static constexpr float QSSG_PI = float(M_PI); |
23 | static constexpr float QSSG_HALFPI = float(M_PI_2); |
24 | |
25 | static const QRhiShaderResourceBinding::StageFlags RENDERER_VISIBILITY_ALL = |
26 | QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage; |
27 | |
28 | static QSSGRhiShaderPipelinePtr shadersForDefaultMaterial(QSSGRhiGraphicsPipelineState *ps, |
29 | QSSGSubsetRenderable &subsetRenderable, |
30 | const QSSGShaderFeatures &featureSet) |
31 | { |
32 | auto &renderer(subsetRenderable.renderer); |
33 | const auto &shaderPipeline = QSSGRendererPrivate::getShaderPipelineForDefaultMaterial(renderer&: *renderer, inRenderable&: subsetRenderable, inFeatureSet: featureSet); |
34 | if (shaderPipeline) |
35 | QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps&: *ps, pipeline: shaderPipeline.get()); |
36 | return shaderPipeline; |
37 | } |
38 | |
39 | static QSSGRhiShaderPipelinePtr shadersForParticleMaterial(QSSGRhiGraphicsPipelineState *ps, |
40 | QSSGParticlesRenderable &particleRenderable) |
41 | { |
42 | const auto &renderer(particleRenderable.renderer); |
43 | const auto &shaderCache = renderer->contextInterface()->shaderCache(); |
44 | auto featureLevel = particleRenderable.particles.m_featureLevel; |
45 | const auto &shaderPipeline = shaderCache->getBuiltInRhiShaders().getRhiParticleShader(featureLevel, viewCount: ps->viewCount); |
46 | if (shaderPipeline) |
47 | QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps&: *ps, pipeline: shaderPipeline.get()); |
48 | return shaderPipeline; |
49 | } |
50 | |
51 | static void updateUniformsForDefaultMaterial(QSSGRhiShaderPipeline &shaderPipeline, |
52 | QSSGRhiContext *rhiCtx, |
53 | const QSSGLayerRenderData &inData, |
54 | char *ubufData, |
55 | QSSGRhiGraphicsPipelineState *ps, |
56 | QSSGSubsetRenderable &subsetRenderable, |
57 | const QSSGRenderCameraList &cameras, |
58 | const QVector2D *depthAdjust, |
59 | const QMatrix4x4 *alteredModelViewProjection) |
60 | { |
61 | const auto &renderer(subsetRenderable.renderer); |
62 | const QMatrix4x4 clipSpaceCorrMatrix = rhiCtx->rhi()->clipSpaceCorrMatrix(); |
63 | QSSGRenderMvpArray alteredMvpList; |
64 | if (alteredModelViewProjection) |
65 | alteredMvpList[0] = *alteredModelViewProjection; |
66 | |
67 | const auto &modelNode = subsetRenderable.modelContext.model; |
68 | QRhiTexture *lightmapTexture = inData.getLightmapTexture(modelContext: subsetRenderable.modelContext); |
69 | |
70 | const QMatrix4x4 &localInstanceTransform(modelNode.localInstanceTransform); |
71 | const QMatrix4x4 &globalInstanceTransform(modelNode.globalInstanceTransform); |
72 | const QMatrix4x4 &modelMatrix(modelNode.usesBoneTexture() ? QMatrix4x4() : subsetRenderable.globalTransform); |
73 | |
74 | QSSGMaterialShaderGenerator::setRhiMaterialProperties(*renderer->contextInterface(), |
75 | shaders&: shaderPipeline, |
76 | ubufData, |
77 | inPipelineState: ps, |
78 | inMaterial: subsetRenderable.material, |
79 | inKey: subsetRenderable.shaderDescription, |
80 | inProperties: inData.getDefaultMaterialPropertyTable(), |
81 | inCameras: cameras, |
82 | inModelViewProjections: alteredModelViewProjection ? alteredMvpList : subsetRenderable.modelContext.modelViewProjections, |
83 | inNormalMatrix: subsetRenderable.modelContext.normalMatrix, |
84 | inGlobalTransform: modelMatrix, |
85 | clipSpaceCorrMatrix, |
86 | localInstanceTransform, |
87 | globalInstanceTransform, |
88 | inMorphWeights: toDataView(type: modelNode.morphWeights), |
89 | inFirstImage: subsetRenderable.firstImage, |
90 | inOpacity: subsetRenderable.opacity, |
91 | inRenderProperties: inData, |
92 | inLights: subsetRenderable.lights, |
93 | reflectionProbe: subsetRenderable.reflectionProbe, |
94 | receivesShadows: subsetRenderable.renderableFlags.receivesShadows(), |
95 | receivesReflections: subsetRenderable.renderableFlags.receivesReflections(), |
96 | shadowDepthAdjust: depthAdjust, |
97 | lightmapTexture); |
98 | } |
99 | |
100 | std::pair<QSSGBounds3, QSSGBounds3> RenderHelpers::calculateSortedObjectBounds(const QSSGRenderableObjectList &sortedOpaqueObjects, |
101 | const QSSGRenderableObjectList &sortedTransparentObjects) |
102 | { |
103 | QSSGBounds3 boundsCasting; |
104 | QSSGBounds3 boundsReceiving; |
105 | for (const auto handles : { &sortedOpaqueObjects, &sortedTransparentObjects }) { |
106 | // Since we may have nodes that are not a child of the camera parent we go through all |
107 | // the opaque objects and include them in the bounds. Failing to do this can result in |
108 | // too small bounds. |
109 | for (const QSSGRenderableObjectHandle &handle : *handles) { |
110 | const QSSGRenderableObject &obj = *handle.obj; |
111 | // We skip objects not casting or receiving shadows since they don't influence or need to be covered by the shadow map |
112 | if (obj.renderableFlags.castsShadows()) |
113 | boundsCasting.include(b: obj.globalBounds); |
114 | if (obj.renderableFlags.receivesShadows()) |
115 | boundsReceiving.include(b: obj.globalBounds); |
116 | } |
117 | } |
118 | return { boundsCasting, boundsReceiving }; |
119 | } |
120 | |
121 | static QSSGBoxPoints computeFrustumBounds(const QSSGRenderCamera &inCamera) |
122 | { |
123 | QMatrix4x4 viewProjection; |
124 | inCamera.calculateViewProjectionMatrix(outMatrix&: viewProjection); |
125 | |
126 | bool invertible = false; |
127 | QMatrix4x4 inv = viewProjection.inverted(invertible: &invertible); |
128 | Q_ASSERT(invertible); |
129 | |
130 | return { inv.map(point: QVector3D(-1, -1, -1)), inv.map(point: QVector3D(+1, -1, -1)), inv.map(point: QVector3D(+1, +1, -1)), |
131 | inv.map(point: QVector3D(-1, +1, -1)), inv.map(point: QVector3D(-1, -1, +1)), inv.map(point: QVector3D(+1, -1, +1)), |
132 | inv.map(point: QVector3D(+1, +1, +1)), inv.map(point: QVector3D(-1, +1, +1)) }; |
133 | } |
134 | |
135 | static QSSGBoxPoints computeFrustumBoundsWithNearFar(const QSSGRenderCamera &inCamera, float clipNear, float clipFar, float pcfRadius) |
136 | { |
137 | QMatrix4x4 viewProjection; |
138 | inCamera.calculateViewProjectionMatrix(outMatrix&: viewProjection, clipNear, clipFar); |
139 | |
140 | bool invertible = false; |
141 | QMatrix4x4 inv = viewProjection.inverted(invertible: &invertible); |
142 | Q_ASSERT(invertible); |
143 | |
144 | QSSGBoxPoints pts = { inv.map(point: QVector3D(-1, -1, -1)), inv.map(point: QVector3D(+1, -1, -1)), |
145 | inv.map(point: QVector3D(+1, +1, -1)), inv.map(point: QVector3D(-1, +1, -1)), |
146 | inv.map(point: QVector3D(-1, -1, +1)), inv.map(point: QVector3D(+1, -1, +1)), |
147 | inv.map(point: QVector3D(+1, +1, +1)), inv.map(point: QVector3D(-1, +1, +1)) }; |
148 | |
149 | if (pcfRadius > 0.f) { |
150 | // Expand the frustum in all directions to cover the pcf radius |
151 | const QVector3D origin = inv.map(point: QVector3D(0, 0, 0)); |
152 | const QVector3D x = (inv.map(point: QVector3D(1, 0, 0)) - origin).normalized() * pcfRadius; |
153 | const QVector3D y = (inv.map(point: QVector3D(0, 1, 0)) - origin).normalized() * pcfRadius; |
154 | const QVector3D z = (inv.map(point: QVector3D(0, 0, 1)) - origin).normalized() * pcfRadius; |
155 | |
156 | // bottom top |
157 | // 4__________5 7__________6 |
158 | // \ / \ / |
159 | // \ / \ / |
160 | // \____/ \____/ |
161 | // 0 1 3 2 |
162 | // left |
163 | pts[0] -= x; |
164 | pts[3] -= x; |
165 | pts[7] -= x; |
166 | pts[4] -= x; |
167 | // right |
168 | pts[1] += x; |
169 | pts[5] += x; |
170 | pts[6] += x; |
171 | pts[2] += x; |
172 | // top |
173 | pts[3] += y; |
174 | pts[2] += y; |
175 | pts[6] += y; |
176 | pts[7] += y; |
177 | // bottom |
178 | pts[0] -= y; |
179 | pts[1] -= y; |
180 | pts[5] -= y; |
181 | pts[4] -= y; |
182 | // back |
183 | pts[0] -= z; |
184 | pts[1] -= z; |
185 | pts[2] -= z; |
186 | pts[3] -= z; |
187 | // front |
188 | pts[4] += z; |
189 | pts[5] += z; |
190 | pts[6] += z; |
191 | pts[7] += z; |
192 | } |
193 | |
194 | return pts; |
195 | } |
196 | |
197 | static std::unique_ptr<QSSGRenderCamera> computeShadowCameraFromFrustum(const QSSGRenderCamera &inCamera, |
198 | const QMatrix4x4 &lightMatrix, |
199 | const QMatrix4x4 &lightMatrixInverted, |
200 | const QVector3D &lightPivot, |
201 | const QVector3D &lightForward, |
202 | const QVector3D &lightUp, |
203 | const float shadowMapResolution, |
204 | const float pcfRadius, |
205 | float clipRange, |
206 | float frustumStartT, |
207 | float frustumEndT, |
208 | const QSSGBounds3 &castingBox, |
209 | const QSSGBounds3 &receivingBox, |
210 | QSSGDebugDrawSystem *debugDrawSystem, |
211 | const bool drawCascades, |
212 | const bool drawSceneCascadeIntersection) |
213 | { |
214 | if (!castingBox.isFinite() || castingBox.isEmpty() || !receivingBox.isFinite() || receivingBox.isEmpty()) |
215 | return nullptr; // Return early, no casting or receiving objects means no shadows |
216 | |
217 | Q_ASSERT(frustumStartT <= frustumEndT); |
218 | Q_ASSERT(frustumStartT >= 0.f); |
219 | Q_ASSERT(frustumEndT <= 1.0f); |
220 | |
221 | auto transformPoints = [&](const QSSGBoxPoints &points) { |
222 | QSSGBoxPoints result; |
223 | for (int i = 0; i < int(points.size()); ++i) { |
224 | result[i] = lightMatrix.map(point: points[i]); |
225 | } |
226 | return result; |
227 | }; |
228 | |
229 | const float clipNear = 1.0f + clipRange * frustumStartT; |
230 | const float clipFar = 1.0f + clipRange * frustumEndT; |
231 | |
232 | QSSGBoxPoints frustumPoints = computeFrustumBoundsWithNearFar(inCamera, clipNear, clipFar, pcfRadius); |
233 | if (drawCascades) |
234 | ShadowmapHelpers::addDebugFrustum(frustumPoints, color: QColorConstants::Black, debugDrawSystem); |
235 | |
236 | QList<QVector3D> receivingSliced = ShadowmapHelpers::intersectBoxByFrustum(frustumPoints, |
237 | box: receivingBox.toQSSGBoxPoints(), |
238 | debugDrawSystem: drawSceneCascadeIntersection ? debugDrawSystem : nullptr, |
239 | color: QColorConstants::DarkGray); |
240 | if (receivingSliced.isEmpty()) |
241 | return nullptr; |
242 | |
243 | QSSGBounds3 receivingFrustumSlicedLightSpace; |
244 | for (const QVector3D &point : receivingSliced) |
245 | receivingFrustumSlicedLightSpace.include(v: lightMatrix.map(point)); |
246 | |
247 | // Slice casting box by frustumBounds' left, right, up, down planes |
248 | QList<QVector3D> castingPointsLightSpace = ShadowmapHelpers::intersectBoxByBox(boxBounds: receivingFrustumSlicedLightSpace, |
249 | box: transformPoints(castingBox.toQSSGBoxPointsNoEmptyCheck())); |
250 | if (castingPointsLightSpace.isEmpty()) |
251 | return nullptr; |
252 | |
253 | // Create box containing casting and receiving from light space: |
254 | QSSGBounds3 castReceiveBounds; |
255 | for (const QVector3D &p : castingPointsLightSpace) { |
256 | castReceiveBounds.include(v: p); |
257 | } |
258 | |
259 | for (const QVector3D &p : receivingFrustumSlicedLightSpace.toQSSGBoxPointsNoEmptyCheck()) { |
260 | float zMax = qMax(a: p.z(), b: castReceiveBounds.maximum.z()); |
261 | float zMin = qMin(a: p.z(), b: castReceiveBounds.minimum.z()); |
262 | castReceiveBounds.maximum.setZ(zMax); |
263 | castReceiveBounds.minimum.setZ(zMin); |
264 | } |
265 | |
266 | QVector3D boundsCenterWorld = lightMatrixInverted.map(point: castReceiveBounds.center()); |
267 | QVector3D boundsDims = castReceiveBounds.dimensions(); |
268 | boundsDims.setZ(boundsDims.z() * 1.01f); // Expand slightly in z direction to avoid pancaking precision errors |
269 | |
270 | // We expand the shadowmap to cover the bounds with one extra texel on all sides |
271 | const float texelExpandFactor = shadowMapResolution / (shadowMapResolution - 2); |
272 | |
273 | QRectF theViewport(0.0f, 0.0f, boundsDims.x() * texelExpandFactor, boundsDims.y() * texelExpandFactor); |
274 | |
275 | auto camera = std::make_unique<QSSGRenderCamera>(args: QSSGRenderGraphObject::Type::OrthographicCamera); |
276 | camera->clipNear = -0.5f * boundsDims.z(); |
277 | camera->clipFar = 0.5f * boundsDims.z(); |
278 | camera->fov = qDegreesToRadians(degrees: 90.f); |
279 | camera->parent = nullptr; |
280 | camera->localTransform = QSSGRenderNode::calculateTransformMatrix(position: boundsCenterWorld, |
281 | scale: QSSGRenderNode::initScale, |
282 | pivot: lightPivot, |
283 | rotation: QQuaternion::fromDirection(direction: lightForward, up: lightUp)); |
284 | camera->calculateGlobalVariables(inViewport: theViewport); |
285 | |
286 | return camera; |
287 | } |
288 | |
289 | static QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> setupCascadingCamerasForShadowMap(const QSSGRenderCamera &inCamera, |
290 | const QSSGRenderLight *inLight, |
291 | const int shadowMapResolution, |
292 | const float pcfRadius, |
293 | const QSSGBounds3 &castingObjectsBox, |
294 | const QSSGBounds3 &receivingObjectsBox, |
295 | QSSGDebugDrawSystem *debugDrawSystem, |
296 | bool drawCascades, |
297 | bool drawSceneCascadeIntersection) |
298 | { |
299 | Q_ASSERT(inLight->type == QSSGRenderLight::Type::DirectionalLight); |
300 | QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> result; |
301 | |
302 | const QVector3D lightDir = inLight->getDirection(); |
303 | const QVector3D lightPivot = inLight->pivot; |
304 | |
305 | const QVector3D forward = lightDir.normalized(); |
306 | const QVector3D right = qFuzzyCompare(p1: qAbs(t: forward.y()), p2: 1.0f) |
307 | ? QVector3D::crossProduct(v1: forward, v2: QVector3D(1, 0, 0)).normalized() |
308 | : QVector3D::crossProduct(v1: forward, v2: QVector3D(0, 1, 0)).normalized(); |
309 | const QVector3D up = QVector3D::crossProduct(v1: right, v2: forward).normalized(); |
310 | |
311 | QMatrix4x4 lightMatrix; |
312 | lightMatrix.setRow(index: 0, value: QVector4D(right, 0.0f)); |
313 | lightMatrix.setRow(index: 1, value: QVector4D(up, 0.0f)); |
314 | lightMatrix.setRow(index: 2, value: QVector4D(forward, 0.0f)); |
315 | lightMatrix.setRow(index: 3, value: QVector4D(0.0f, 0.0f, 0.0f, 1.0f)); |
316 | QMatrix4x4 lightMatrixInverted = lightMatrix.inverted(); |
317 | |
318 | const float clipRange = qMax(a: 2.0f, b: qMin(a: inLight->m_shadowMapFar, b: inCamera.clipFar)) - 1.0f; |
319 | |
320 | const auto computeSplitRanges = [inLight](const QVarLengthArray<float, 3> &splits) -> QVarLengthArray<QPair<float, float>, 4> { |
321 | QVarLengthArray<QPair<float, float>, 4> ranges; |
322 | const float csmBlendRatio = inLight->m_csmBlendRatio; |
323 | float t0 = 0.f; |
324 | for (qsizetype i = 0; i < splits.length(); i++) { |
325 | const float tI = qBound(min: qMin(a: t0 + 0.01f, b: 1.0f), val: splits[i], max: 1.0f); |
326 | ranges.emplace_back(args&: t0, args: qMin(a: 1.0f, b: tI + csmBlendRatio)); |
327 | t0 = tI; |
328 | } |
329 | ranges.emplace_back(args&: t0, args: 1.0f); |
330 | return ranges; |
331 | }; |
332 | |
333 | const auto computeFrustums = [&](const QVarLengthArray<float, 3> &splits) { |
334 | for (const auto &range : computeSplitRanges(splits)) { |
335 | auto camera = computeShadowCameraFromFrustum(inCamera, |
336 | lightMatrix, |
337 | lightMatrixInverted, |
338 | lightPivot, |
339 | lightForward: forward, |
340 | lightUp: up, |
341 | shadowMapResolution, |
342 | pcfRadius, |
343 | clipRange, |
344 | frustumStartT: range.first, |
345 | frustumEndT: range.second, |
346 | castingBox: castingObjectsBox, |
347 | receivingBox: receivingObjectsBox, |
348 | debugDrawSystem, |
349 | drawCascades, |
350 | drawSceneCascadeIntersection); |
351 | result.emplace_back(args: std::move(camera)); |
352 | } |
353 | }; |
354 | |
355 | switch (inLight->m_csmNumSplits) { |
356 | case 0: { |
357 | computeFrustums({}); |
358 | break; |
359 | } |
360 | case 1: { |
361 | computeFrustums({ inLight->m_csmSplit1 }); |
362 | break; |
363 | } |
364 | case 2: { |
365 | computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2 }); |
366 | break; |
367 | } |
368 | case 3: { |
369 | computeFrustums({ inLight->m_csmSplit1, inLight->m_csmSplit2, inLight->m_csmSplit3 }); |
370 | break; |
371 | } |
372 | default: |
373 | Q_UNREACHABLE(); |
374 | break; |
375 | } |
376 | |
377 | return result; |
378 | } |
379 | |
380 | static void setupCubeReflectionCameras(const QSSGRenderReflectionProbe *inProbe, QSSGRenderCamera inCameras[6]) |
381 | { |
382 | Q_ASSERT(inProbe != nullptr); |
383 | |
384 | // setup light matrix |
385 | quint32 mapRes = 1 << inProbe->reflectionMapRes; |
386 | QRectF theViewport(0.0f, 0.0f, (float)mapRes, (float)mapRes); |
387 | static const QQuaternion rotOfs[6] { |
388 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: -QSSG_HALFPI), roll: qRadiansToDegrees(radians: QSSG_PI)), |
389 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: QSSG_HALFPI), roll: qRadiansToDegrees(radians: QSSG_PI)), |
390 | QQuaternion::fromEulerAngles(pitch: qRadiansToDegrees(radians: QSSG_HALFPI), yaw: 0.f, roll: 0.f), |
391 | QQuaternion::fromEulerAngles(pitch: qRadiansToDegrees(radians: -QSSG_HALFPI), yaw: 0.f, roll: 0.f), |
392 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: QSSG_PI), roll: qRadiansToDegrees(radians: -QSSG_PI)), |
393 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: 0.f, roll: qRadiansToDegrees(radians: QSSG_PI)), |
394 | }; |
395 | |
396 | const QVector3D inProbePos = inProbe->getGlobalPos(); |
397 | const QVector3D inProbePivot = inProbe->pivot; |
398 | |
399 | for (int i = 0; i < 6; ++i) { |
400 | inCameras[i].parent = nullptr; |
401 | inCameras[i].clipNear = 1.0f; |
402 | inCameras[i].clipFar = qMax<float>(a: 2.0f, b: 10000.0f); |
403 | inCameras[i].fov = qDegreesToRadians(degrees: 90.f); |
404 | |
405 | inCameras[i].localTransform = QSSGRenderNode::calculateTransformMatrix(position: inProbePos, scale: QSSGRenderNode::initScale, pivot: inProbePivot, rotation: rotOfs[i]); |
406 | inCameras[i].calculateGlobalVariables(inViewport: theViewport); |
407 | } |
408 | } |
409 | |
410 | static void addOpaqueDepthPrePassBindings(QSSGRhiContext *rhiCtx, |
411 | QSSGRhiShaderPipeline *shaderPipeline, |
412 | QSSGRenderableImage *renderableImage, |
413 | QSSGRhiShaderResourceBindingList &bindings, |
414 | bool isCustomMaterialMeshSubset = false) |
415 | { |
416 | static const auto imageAffectsAlpha = [](QSSGRenderableImage::Type mapType) { |
417 | return mapType == QSSGRenderableImage::Type::BaseColor || |
418 | mapType == QSSGRenderableImage::Type::Diffuse || |
419 | mapType == QSSGRenderableImage::Type::Translucency || |
420 | mapType == QSSGRenderableImage::Type::Opacity; |
421 | }; |
422 | |
423 | while (renderableImage) { |
424 | const auto mapType = renderableImage->m_mapType; |
425 | if (imageAffectsAlpha(mapType)) { |
426 | const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(type: mapType); |
427 | const int samplerHint = int(mapType); |
428 | int samplerBinding = shaderPipeline->bindingForTexture(name: samplerName, hint: samplerHint); |
429 | if (samplerBinding >= 0) { |
430 | QRhiTexture *texture = renderableImage->m_texture.m_texture; |
431 | if (samplerBinding >= 0 && texture) { |
432 | const bool mipmapped = texture->flags().testFlag(flag: QRhiTexture::MipMapped); |
433 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_minFilterType), |
434 | .magFilter: QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_magFilterType), |
435 | .mipmap: mipmapped ? QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None, |
436 | .hTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_horizontalTilingMode), |
437 | .vTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_verticalTilingMode), |
438 | .zTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_depthTilingMode) |
439 | }); |
440 | bindings.addTexture(binding: samplerBinding, stage: RENDERER_VISIBILITY_ALL, tex: texture, sampler); |
441 | } |
442 | } // else this is not necessarily an error, e.g. having metalness/roughness maps with metalness disabled |
443 | } |
444 | renderableImage = renderableImage->m_nextImage; |
445 | } |
446 | // For custom Materials we can't know which maps affect alpha, so map all |
447 | if (isCustomMaterialMeshSubset) { |
448 | QVector<QShaderDescription::InOutVariable> samplerVars = |
449 | shaderPipeline->fragmentStage()->shader().description().combinedImageSamplers(); |
450 | for (const QShaderDescription::InOutVariable &var : shaderPipeline->vertexStage()->shader().description().combinedImageSamplers()) { |
451 | auto it = std::find_if(first: samplerVars.cbegin(), last: samplerVars.cend(), |
452 | pred: [&var](const QShaderDescription::InOutVariable &v) { return var.binding == v.binding; }); |
453 | if (it == samplerVars.cend()) |
454 | samplerVars.append(t: var); |
455 | } |
456 | |
457 | int maxSamplerBinding = -1; |
458 | for (const QShaderDescription::InOutVariable &var : samplerVars) |
459 | maxSamplerBinding = qMax(a: maxSamplerBinding, b: var.binding); |
460 | |
461 | // Will need to set unused image-samplers to something dummy |
462 | // because the shader code contains all custom property textures, |
463 | // and not providing a binding for all of them is invalid with some |
464 | // graphics APIs (and will need a real texture because setting a |
465 | // null handle or similar is not permitted with some of them so the |
466 | // srb does not accept null QRhiTextures either; but first let's |
467 | // figure out what bindings are unused in this frame) |
468 | QBitArray samplerBindingsSpecified(maxSamplerBinding + 1); |
469 | |
470 | if (maxSamplerBinding >= 0) { |
471 | // custom property textures |
472 | int customTexCount = shaderPipeline->extraTextureCount(); |
473 | for (int i = 0; i < customTexCount; ++i) { |
474 | const QSSGRhiTexture &t(shaderPipeline->extraTextureAt(index: i)); |
475 | const int samplerBinding = shaderPipeline->bindingForTexture(name: t.name); |
476 | if (samplerBinding >= 0) { |
477 | samplerBindingsSpecified.setBit(samplerBinding); |
478 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: t.samplerDesc); |
479 | bindings.addTexture(binding: samplerBinding, |
480 | stage: RENDERER_VISIBILITY_ALL, |
481 | tex: t.texture, |
482 | sampler); |
483 | } |
484 | } |
485 | } |
486 | |
487 | // use a dummy texture for the unused samplers in the shader |
488 | QRhiSampler *dummySampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, .magFilter: QRhiSampler::Nearest, .mipmap: QRhiSampler::None, |
489 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
490 | QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch(); |
491 | QRhiTexture *dummyTexture = rhiCtx->dummyTexture(flags: {}, rub: resourceUpdates); |
492 | QRhiTexture *dummyCubeTexture = rhiCtx->dummyTexture(flags: QRhiTexture::CubeMap, rub: resourceUpdates); |
493 | rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates); |
494 | |
495 | for (const QShaderDescription::InOutVariable &var : samplerVars) { |
496 | if (!samplerBindingsSpecified.testBit(i: var.binding)) { |
497 | QRhiTexture *t = var.type == QShaderDescription::SamplerCube ? dummyCubeTexture : dummyTexture; |
498 | bindings.addTexture(binding: var.binding, stage: RENDERER_VISIBILITY_ALL, tex: t, sampler: dummySampler); |
499 | } |
500 | } |
501 | } |
502 | } |
503 | |
504 | static void setupCubeShadowCameras(const QSSGRenderLight *inLight, float shadowMapFar, QSSGRenderCamera inCameras[6]) |
505 | { |
506 | Q_ASSERT(inLight != nullptr); |
507 | Q_ASSERT(inLight->type != QSSGRenderLight::Type::DirectionalLight); |
508 | |
509 | // setup light matrix |
510 | quint32 mapRes = inLight->m_shadowMapRes; |
511 | QRectF theViewport(0.0f, 0.0f, (float)mapRes, (float)mapRes); |
512 | static const QQuaternion rotOfs[6] { |
513 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: -QSSG_HALFPI), roll: qRadiansToDegrees(radians: QSSG_PI)), |
514 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: QSSG_HALFPI), roll: qRadiansToDegrees(radians: QSSG_PI)), |
515 | QQuaternion::fromEulerAngles(pitch: qRadiansToDegrees(radians: QSSG_HALFPI), yaw: 0.f, roll: 0.f), |
516 | QQuaternion::fromEulerAngles(pitch: qRadiansToDegrees(radians: -QSSG_HALFPI), yaw: 0.f, roll: 0.f), |
517 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: qRadiansToDegrees(radians: QSSG_PI), roll: qRadiansToDegrees(radians: -QSSG_PI)), |
518 | QQuaternion::fromEulerAngles(pitch: 0.f, yaw: 0.f, roll: qRadiansToDegrees(radians: QSSG_PI)), |
519 | }; |
520 | |
521 | const QVector3D inLightPos = inLight->getGlobalPos(); |
522 | constexpr QVector3D lightPivot = QVector3D(0, 0, 0); |
523 | |
524 | for (int i = 0; i < 6; ++i) { |
525 | inCameras[i].parent = nullptr; |
526 | inCameras[i].clipNear = 1.0f; |
527 | inCameras[i].clipFar = shadowMapFar; |
528 | inCameras[i].fov = qDegreesToRadians(degrees: 90.f); |
529 | inCameras[i].localTransform = QSSGRenderNode::calculateTransformMatrix(position: inLightPos, scale: QSSGRenderNode::initScale, pivot: lightPivot, rotation: rotOfs[i]); |
530 | inCameras[i].calculateGlobalVariables(inViewport: theViewport); |
531 | } |
532 | |
533 | /* |
534 | if ( inLight->type == RenderLightTypes::Point ) return; |
535 | |
536 | QVector3D viewDirs[6]; |
537 | QVector3D viewUp[6]; |
538 | QMatrix3x3 theDirMatrix( inLight->m_GlobalTransform.getUpper3x3() ); |
539 | |
540 | viewDirs[0] = theDirMatrix.transform( QVector3D( 1.f, 0.f, 0.f ) ); |
541 | viewDirs[2] = theDirMatrix.transform( QVector3D( 0.f, -1.f, 0.f ) ); |
542 | viewDirs[4] = theDirMatrix.transform( QVector3D( 0.f, 0.f, 1.f ) ); |
543 | viewDirs[0].normalize(); viewDirs[2].normalize(); viewDirs[4].normalize(); |
544 | viewDirs[1] = -viewDirs[0]; |
545 | viewDirs[3] = -viewDirs[2]; |
546 | viewDirs[5] = -viewDirs[4]; |
547 | |
548 | viewUp[0] = viewDirs[2]; |
549 | viewUp[1] = viewDirs[2]; |
550 | viewUp[2] = viewDirs[5]; |
551 | viewUp[3] = viewDirs[4]; |
552 | viewUp[4] = viewDirs[2]; |
553 | viewUp[5] = viewDirs[2]; |
554 | |
555 | for (int i = 0; i < 6; ++i) |
556 | { |
557 | inCameras[i].LookAt( inLightPos, viewUp[i], inLightPos + viewDirs[i] ); |
558 | inCameras[i].CalculateGlobalVariables( theViewport, QVector2D( theViewport.m_Width, |
559 | theViewport.m_Height ) ); |
560 | } |
561 | */ |
562 | } |
563 | |
564 | static int setupInstancing(QSSGSubsetRenderable *renderable, QSSGRhiGraphicsPipelineState *ps, QSSGRhiContext *rhiCtx, const QVector3D &cameraDirection, const QVector3D &cameraPosition) |
565 | { |
566 | // TODO: non-static so it can be used from QSSGCustomMaterialSystem::rhiPrepareRenderable()? |
567 | const bool instancing = QSSGLayerRenderData::prepareInstancing(rhiCtx, renderable, cameraDirection, cameraPosition, minThreshold: renderable->instancingLodMin, maxThreshold: renderable->instancingLodMax); |
568 | int instanceBufferBinding = 0; |
569 | if (instancing) { |
570 | auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps&: *ps); |
571 | // set up new bindings for instanced buffers |
572 | const quint32 stride = renderable->modelContext.model.instanceTable->stride(); |
573 | QVarLengthArray<QRhiVertexInputBinding, 8> bindings; |
574 | std::copy(first: ia.inputLayout.cbeginBindings(), last: ia.inputLayout.cendBindings(), result: std::back_inserter(x&: bindings)); |
575 | bindings.append(t: { stride, QRhiVertexInputBinding::PerInstance }); |
576 | instanceBufferBinding = bindings.size() - 1; |
577 | ia.inputLayout.setBindings(first: bindings.cbegin(), last: bindings.cend()); |
578 | } |
579 | return instanceBufferBinding; |
580 | } |
581 | |
582 | static void rhiPrepareResourcesForReflectionMap(QSSGRhiContext *rhiCtx, |
583 | QSSGPassKey passKey, |
584 | const QSSGLayerRenderData &inData, |
585 | QSSGReflectionMapEntry *pEntry, |
586 | QSSGRhiGraphicsPipelineState *ps, |
587 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
588 | QSSGRenderCamera &inCamera, |
589 | QSSGRenderer &renderer, |
590 | QSSGRenderTextureCubeFace cubeFace) |
591 | { |
592 | using namespace RenderHelpers; |
593 | |
594 | if ((inData.layer.background == QSSGRenderLayer::Background::SkyBox && inData.layer.lightProbe) || |
595 | inData.layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap) |
596 | rhiPrepareSkyBoxForReflectionMap(rhiCtx, passKey, layer&: inData.layer, inCamera, renderer, entry: pEntry, cubeFace); |
597 | |
598 | QSSGShaderFeatures features = inData.getShaderFeatures(); |
599 | // because of alteredCamera/alteredMvp below |
600 | features.set(feature: QSSGShaderFeatures::Feature::DisableMultiView, val: true); |
601 | |
602 | const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable(); |
603 | |
604 | for (const auto &handle : sortedOpaqueObjects) { |
605 | QSSGRenderableObject &inObject = *handle.obj; |
606 | |
607 | QMatrix4x4 modelViewProjection; |
608 | if (inObject.type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || inObject.type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
609 | QSSGSubsetRenderable &renderable(static_cast<QSSGSubsetRenderable &>(inObject)); |
610 | const bool hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(inDataStore: renderable.shaderDescription) > 0; |
611 | modelViewProjection = hasSkinning ? pEntry->m_viewProjection |
612 | : pEntry->m_viewProjection * renderable.globalTransform; |
613 | } |
614 | |
615 | // here we pass on our own alteredCamera and alteredModelViewProjection |
616 | rhiPrepareRenderable(rhiCtx, passKey, inData, inObject, renderPassDescriptor: pEntry->m_rhiRenderPassDesc, ps, featureSet: features, samples: 1, viewCount: 1, |
617 | alteredCamera: &inCamera, alteredModelViewProjection: &modelViewProjection, cubeFace, entry: pEntry); |
618 | } |
619 | } |
620 | |
621 | static inline void addDepthTextureBindings(QSSGRhiContext *rhiCtx, |
622 | QSSGRhiShaderPipeline *shaderPipeline, |
623 | QSSGRhiShaderResourceBindingList &bindings) |
624 | { |
625 | if (shaderPipeline->depthTexture()) { |
626 | const int depthTextureBinding = shaderPipeline->bindingForTexture(name: "qt_depthTexture", hint: int(QSSGRhiSamplerBindingHints::DepthTexture)); |
627 | const int depthTextureArrayBinding = shaderPipeline->bindingForTexture(name: "qt_depthTextureArray", hint: int(QSSGRhiSamplerBindingHints::DepthTextureArray)); |
628 | if (depthTextureBinding >= 0 || depthTextureArrayBinding >= 0) { |
629 | // nearest min/mag, no mipmap |
630 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, .magFilter: QRhiSampler::Nearest, .mipmap: QRhiSampler::None, |
631 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
632 | if (depthTextureBinding >= 0) |
633 | bindings.addTexture(binding: depthTextureBinding, stage: QRhiShaderResourceBinding::FragmentStage, tex: shaderPipeline->depthTexture(), sampler); |
634 | if (depthTextureArrayBinding >= 0) |
635 | bindings.addTexture(binding: depthTextureBinding, stage: QRhiShaderResourceBinding::FragmentStage, tex: shaderPipeline->depthTexture(), sampler); |
636 | } // else ignore, not an error |
637 | } |
638 | |
639 | // SSAO texture |
640 | if (shaderPipeline->ssaoTexture()) { |
641 | const int ssaoTextureBinding = shaderPipeline->bindingForTexture(name: "qt_aoTexture", hint: int(QSSGRhiSamplerBindingHints::AoTexture)); |
642 | const int ssaoTextureArrayBinding = shaderPipeline->bindingForTexture(name: "qt_aoTextureArray", hint: int(QSSGRhiSamplerBindingHints::AoTextureArray)); |
643 | if (ssaoTextureBinding >= 0 || ssaoTextureArrayBinding >= 0) { |
644 | // linear min/mag, no mipmap |
645 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: QRhiSampler::None, |
646 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
647 | if (ssaoTextureBinding >= 0) { |
648 | bindings.addTexture(binding: ssaoTextureBinding, |
649 | stage: QRhiShaderResourceBinding::FragmentStage, |
650 | tex: shaderPipeline->ssaoTexture(), sampler); |
651 | } |
652 | if (ssaoTextureArrayBinding >= 0) { |
653 | bindings.addTexture(binding: ssaoTextureArrayBinding, |
654 | stage: QRhiShaderResourceBinding::FragmentStage, |
655 | tex: shaderPipeline->ssaoTexture(), sampler); |
656 | } |
657 | } // else ignore, not an error |
658 | } |
659 | } |
660 | |
661 | static void rhiPrepareResourcesForShadowMap(QSSGRhiContext *rhiCtx, |
662 | const QSSGLayerRenderData &inData, |
663 | QSSGPassKey passKey, |
664 | QSSGShadowMapEntry *pEntry, |
665 | QSSGRhiGraphicsPipelineState *ps, |
666 | const QVector2D *depthAdjust, |
667 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
668 | QSSGRenderCamera &inCamera, |
669 | bool orthographic, |
670 | QSSGRenderTextureCubeFace cubeFace, |
671 | quint32 cascadeIndex) |
672 | { |
673 | QSSGShaderFeatures featureSet; |
674 | if (orthographic) |
675 | featureSet.set(feature: QSSGShaderFeatures::Feature::OrthoShadowPass, val: true); |
676 | else |
677 | featureSet.set(feature: QSSGShaderFeatures::Feature::PerspectiveShadowPass, val: true); |
678 | |
679 | // Do note how updateUniformsForDefaultMaterial() get a single camera and a |
680 | // custom mvp; make sure multiview is disabled in the shader generator using |
681 | // the common flag, instead of it having to write logic for checking for |
682 | // OrthoShadowPoss || CubeShadowPass. |
683 | featureSet.set(feature: QSSGShaderFeatures::Feature::DisableMultiView, val: true); |
684 | |
685 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face: cubeFace); |
686 | const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable(); |
687 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
688 | |
689 | for (const auto &handle : sortedOpaqueObjects) { |
690 | QSSGRenderableObject *theObject = handle.obj; |
691 | QSSG_ASSERT(theObject->renderableFlags.castsShadows(), continue); |
692 | |
693 | QSSGShaderFeatures objectFeatureSet = featureSet; |
694 | const bool isOpaqueDepthPrePass = theObject->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass; |
695 | if (isOpaqueDepthPrePass) |
696 | objectFeatureSet.set(feature: QSSGShaderFeatures::Feature::OpaqueDepthPrePass, val: true); |
697 | |
698 | QSSGRhiDrawCallData *dcd = nullptr; |
699 | QMatrix4x4 modelViewProjection; |
700 | QSSGSubsetRenderable &renderable(static_cast<QSSGSubsetRenderable &>(*theObject)); |
701 | if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
702 | const bool hasSkinning = defaultMaterialShaderKeyProperties.m_boneCount.getValue(inDataStore: renderable.shaderDescription) > 0; |
703 | modelViewProjection = hasSkinning ? pEntry->m_lightViewProjection[cascadeIndex] |
704 | : pEntry->m_lightViewProjection[cascadeIndex] * renderable.globalTransform; |
705 | // cascadeIndex is 0..3 for directional light and 0 for the pointlight & spotlight |
706 | // cubeFaceIdx is 0 for directional & spotlight and 0..5 for the pointlight |
707 | // pEntry is unique per light and a light can only be one of directional, point, or spotlight. |
708 | const quintptr entryIdx = cascadeIndex + cubeFaceIdx + (quintptr(renderable.subset.offset) << 3); |
709 | dcd = &rhiCtxD->drawCallData(key: { .cid: passKey, .model: &renderable.modelContext.model, .entry: pEntry, .entryIdx: entryIdx }); |
710 | } |
711 | |
712 | QSSGRhiShaderResourceBindingList bindings; |
713 | QSSGRhiShaderPipelinePtr shaderPipeline; |
714 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(*theObject)); |
715 | if (theObject->type == QSSGSubsetRenderable::Type::DefaultMaterialMeshSubset) { |
716 | const auto &material = static_cast<const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial()); |
717 | ps->cullMode = QSSGRhiHelpers::toCullMode(cullFaceMode: material.cullMode); |
718 | const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(inDataStore: subsetRenderable.shaderDescription); |
719 | |
720 | shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet: objectFeatureSet); |
721 | if (!shaderPipeline) |
722 | continue; |
723 | shaderPipeline->ensureCombinedUniformBuffer(ubuf: &dcd->ubuf); |
724 | char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
725 | // calls updateUni with an alteredCamera and alteredModelViewProjection |
726 | QSSGRenderCameraList cameras({ &inCamera }); |
727 | updateUniformsForDefaultMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras, depthAdjust, alteredModelViewProjection: &modelViewProjection); |
728 | if (blendParticles) |
729 | QSSGParticleRenderer::updateUniformsForParticleModel(shaderPipeline&: *shaderPipeline, ubufData, model: &subsetRenderable.modelContext.model, offset: subsetRenderable.subset.offset); |
730 | dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
731 | if (blendParticles) |
732 | QSSGParticleRenderer::prepareParticlesForModel(shaderPipeline&: *shaderPipeline, rhiCtx, bindings, model: &subsetRenderable.modelContext.model); |
733 | } else if (theObject->type == QSSGSubsetRenderable::Type::CustomMaterialMeshSubset) { |
734 | const auto &material = static_cast<const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial()); |
735 | ps->cullMode = QSSGRhiHelpers::toCullMode(cullFaceMode: material.m_cullMode); |
736 | |
737 | QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get()); |
738 | shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, material, renderable&: subsetRenderable, defaultMaterialShaderKeyProperties: inData.getDefaultMaterialPropertyTable(), featureSet: objectFeatureSet); |
739 | if (!shaderPipeline) |
740 | continue; |
741 | shaderPipeline->ensureCombinedUniformBuffer(ubuf: &dcd->ubuf); |
742 | char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
743 | // inCamera is the shadow camera, not the same as inData.renderedCameras |
744 | QSSGRenderCameraList cameras({ &inCamera }); |
745 | customMaterialSystem.updateUniformsForCustomMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, material, renderable&: subsetRenderable, |
746 | cameras, depthAdjust, alteredModelViewProjection: &modelViewProjection); |
747 | dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
748 | } |
749 | |
750 | if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
751 | |
752 | QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps&: *ps, pipeline: shaderPipeline.get()); |
753 | auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps&: *ps); |
754 | ia = subsetRenderable.subset.rhi.ia; |
755 | const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData); |
756 | int instanceBufferBinding = setupInstancing(renderable: &subsetRenderable, ps, rhiCtx, cameraDirection: cameraDatas[0].direction, cameraPosition: cameraDatas[0].position); |
757 | QSSGRhiHelpers::bakeVertexInputLocations(ia: &ia, shaders: *shaderPipeline, instanceBufferBinding); |
758 | |
759 | |
760 | bindings.addUniformBuffer(binding: 0, stage: RENDERER_VISIBILITY_ALL, buf: dcd->ubuf); |
761 | |
762 | // Depth and SSAO textures, in case a custom material's shader code does something with them. |
763 | addDepthTextureBindings(rhiCtx, shaderPipeline: shaderPipeline.get(), bindings); |
764 | |
765 | if (isOpaqueDepthPrePass) { |
766 | addOpaqueDepthPrePassBindings(rhiCtx, |
767 | shaderPipeline: shaderPipeline.get(), |
768 | renderableImage: subsetRenderable.firstImage, |
769 | bindings, |
770 | isCustomMaterialMeshSubset: (theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset)); |
771 | } |
772 | |
773 | // There is no screen texture at this stage. But the shader from a |
774 | // custom material may rely on it, and an object with that material |
775 | // can end up in the shadow map's object list. So bind a dummy |
776 | // texture then due to the lack of other options. |
777 | const int screenTextureBinding = shaderPipeline->bindingForTexture(name: "qt_screenTexture", hint: int(QSSGRhiSamplerBindingHints::ScreenTexture)); |
778 | const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(name: "qt_screenTextureArray", hint: int(QSSGRhiSamplerBindingHints::ScreenTextureArray)); |
779 | if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) { |
780 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, .magFilter: QRhiSampler::Nearest, .mipmap: QRhiSampler::None, |
781 | .hTiling: QRhiSampler::Repeat, .vTiling: QRhiSampler::Repeat, .zTiling: QRhiSampler::Repeat }); |
782 | if (screenTextureBinding >= 0) { |
783 | QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch(); |
784 | QRhiTexture *dummyTexture = rhiCtx->dummyTexture(flags: {}, rub: resourceUpdates); |
785 | rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates); |
786 | bindings.addTexture(binding: screenTextureBinding, |
787 | stage: QRhiShaderResourceBinding::FragmentStage, |
788 | tex: dummyTexture, sampler); |
789 | } |
790 | if (screenTextureArrayBinding >= 0) { |
791 | QRhiResourceUpdateBatch *resourceUpdates = rhiCtx->rhi()->nextResourceUpdateBatch(); |
792 | QRhiTexture *dummyTexture = rhiCtx->dummyTexture(flags: {}, rub: resourceUpdates, size: QSize(64, 64), fillColor: Qt::black, arraySize: rhiCtx->mainPassViewCount()); |
793 | rhiCtx->commandBuffer()->resourceUpdate(resourceUpdates); |
794 | bindings.addTexture(binding: screenTextureArrayBinding, |
795 | stage: QRhiShaderResourceBinding::FragmentStage, |
796 | tex: dummyTexture, sampler); |
797 | } |
798 | } |
799 | |
800 | // Skinning |
801 | if (QRhiTexture *boneTexture = inData.getBonemapTexture(modelContext: subsetRenderable.modelContext)) { |
802 | int binding = shaderPipeline->bindingForTexture(name: "qt_boneTexture"); |
803 | if (binding >= 0) { |
804 | QRhiSampler *boneSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
805 | .magFilter: QRhiSampler::Nearest, |
806 | .mipmap: QRhiSampler::None, |
807 | .hTiling: QRhiSampler::ClampToEdge, |
808 | .vTiling: QRhiSampler::ClampToEdge, |
809 | .zTiling: QRhiSampler::Repeat |
810 | }); |
811 | bindings.addTexture(binding, |
812 | stage: QRhiShaderResourceBinding::VertexStage, |
813 | tex: boneTexture, |
814 | sampler: boneSampler); |
815 | } |
816 | } |
817 | |
818 | // Morphing |
819 | auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture; |
820 | if (targetsTexture) { |
821 | int binding = shaderPipeline->bindingForTexture(name: "qt_morphTargetTexture"); |
822 | if (binding >= 0) { |
823 | QRhiSampler *targetsSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
824 | .magFilter: QRhiSampler::Nearest, |
825 | .mipmap: QRhiSampler::None, |
826 | .hTiling: QRhiSampler::ClampToEdge, |
827 | .vTiling: QRhiSampler::ClampToEdge, |
828 | .zTiling: QRhiSampler::ClampToEdge |
829 | }); |
830 | bindings.addTexture(binding, stage: QRhiShaderResourceBinding::VertexStage, tex: subsetRenderable.subset.rhi.targetsTexture, sampler: targetsSampler); |
831 | } |
832 | } |
833 | |
834 | QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings); |
835 | subsetRenderable.rhiRenderData.shadowPass.pipeline = rhiCtxD->pipeline(ps: *ps, rpDesc: pEntry->m_rhiRenderPassDesc[cascadeIndex], srb); |
836 | subsetRenderable.rhiRenderData.shadowPass.srb[cubeFaceIdx] = srb; |
837 | } |
838 | } |
839 | } |
840 | |
841 | static void fillTargetBlend(QRhiGraphicsPipeline::TargetBlend *targetBlend, QSSGRenderDefaultMaterial::MaterialBlendMode materialBlend) |
842 | { |
843 | // Assuming default values in the other TargetBlend fields |
844 | switch (materialBlend) { |
845 | case QSSGRenderDefaultMaterial::MaterialBlendMode::Screen: |
846 | targetBlend->srcColor = QRhiGraphicsPipeline::SrcAlpha; |
847 | targetBlend->dstColor = QRhiGraphicsPipeline::One; |
848 | targetBlend->srcAlpha = QRhiGraphicsPipeline::One; |
849 | targetBlend->dstAlpha = QRhiGraphicsPipeline::One; |
850 | break; |
851 | case QSSGRenderDefaultMaterial::MaterialBlendMode::Multiply: |
852 | targetBlend->srcColor = QRhiGraphicsPipeline::DstColor; |
853 | targetBlend->dstColor = QRhiGraphicsPipeline::Zero; |
854 | targetBlend->srcAlpha = QRhiGraphicsPipeline::One; |
855 | targetBlend->dstAlpha = QRhiGraphicsPipeline::One; |
856 | break; |
857 | default: |
858 | // Use SourceOver for everything else |
859 | targetBlend->srcColor = QRhiGraphicsPipeline::SrcAlpha; |
860 | targetBlend->dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
861 | targetBlend->srcAlpha = QRhiGraphicsPipeline::One; |
862 | targetBlend->dstAlpha = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
863 | break; |
864 | } |
865 | } |
866 | |
867 | void RenderHelpers::rhiPrepareRenderable(QSSGRhiContext *rhiCtx, |
868 | QSSGPassKey passKey, |
869 | const QSSGLayerRenderData &inData, |
870 | QSSGRenderableObject &inObject, |
871 | QRhiRenderPassDescriptor *renderPassDescriptor, |
872 | QSSGRhiGraphicsPipelineState *ps, |
873 | QSSGShaderFeatures featureSet, |
874 | int samples, |
875 | int viewCount, |
876 | QSSGRenderCamera *alteredCamera, |
877 | QMatrix4x4 *alteredModelViewProjection, |
878 | QSSGRenderTextureCubeFace cubeFace, |
879 | QSSGReflectionMapEntry *entry) |
880 | { |
881 | const auto &defaultMaterialShaderKeyProperties = inData.getDefaultMaterialPropertyTable(); |
882 | |
883 | switch (inObject.type) { |
884 | case QSSGRenderableObject::Type::DefaultMaterialMeshSubset: |
885 | { |
886 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(inObject)); |
887 | |
888 | if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(flag: QSSGRenderableObjectFlag::ReceivesReflections)) |
889 | featureSet.set(feature: QSSGShaderFeatures::Feature::ReflectionProbe, val: true); |
890 | |
891 | if ((cubeFace != QSSGRenderTextureCubeFaceNone)) { |
892 | // Disable tonemapping for the reflection pass |
893 | featureSet.disableTonemapping(); |
894 | } |
895 | |
896 | if (subsetRenderable.renderableFlags.rendersWithLightmap()) |
897 | featureSet.set(feature: QSSGShaderFeatures::Feature::Lightmap, val: true); |
898 | |
899 | const auto &shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet); |
900 | if (shaderPipeline) { |
901 | // Unlike the subsetRenderable (which is allocated per frame so is |
902 | // not persistent in any way), the model reference is persistent in |
903 | // the sense that it references the model node in the scene graph. |
904 | // Combined with the layer node (multiple View3Ds may share the |
905 | // same scene!), this is suitable as a key to get the uniform |
906 | // buffers that were used with the rendering of the same model in |
907 | // the previous frame. |
908 | QSSGRhiShaderResourceBindingList bindings; |
909 | const auto &modelNode = subsetRenderable.modelContext.model; |
910 | const bool blendParticles = defaultMaterialShaderKeyProperties.m_blendParticles.getValue(inDataStore: subsetRenderable.shaderDescription); |
911 | |
912 | |
913 | // NOTE: |
914 | // - entryIdx should 0 for QSSGRenderTextureCubeFaceNone. |
915 | // In all other cases the entryIdx is a combination of the cubeface idx and the subset offset, where the lower bits |
916 | // are the cubeface idx. |
917 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face: cubeFace); |
918 | const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * (cubeFaceIdx + (quintptr(subsetRenderable.subset.offset) << 3)); |
919 | // If there's an entry we merge that with the address of the material |
920 | const auto entryPartA = reinterpret_cast<quintptr>(&subsetRenderable.material); |
921 | const auto entryPartB = reinterpret_cast<quintptr>(entry); |
922 | const void *entryId = reinterpret_cast<const void *>(entryPartA ^ entryPartB); |
923 | |
924 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
925 | QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData(key: { .cid: passKey, .model: &modelNode, .entry: entryId, .entryIdx: entryIdx }); |
926 | |
927 | shaderPipeline->ensureCombinedUniformBuffer(ubuf: &dcd.ubuf); |
928 | char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
929 | if (alteredCamera) { |
930 | Q_ASSERT(alteredModelViewProjection); |
931 | QSSGRenderCameraList cameras({ alteredCamera }); |
932 | updateUniformsForDefaultMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras, depthAdjust: nullptr, alteredModelViewProjection); |
933 | } else { |
934 | Q_ASSERT(!alteredModelViewProjection); |
935 | updateUniformsForDefaultMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras: inData.renderedCameras, depthAdjust: nullptr, alteredModelViewProjection: nullptr); |
936 | } |
937 | |
938 | if (blendParticles) |
939 | QSSGParticleRenderer::updateUniformsForParticleModel(shaderPipeline&: *shaderPipeline, ubufData, model: &subsetRenderable.modelContext.model, offset: subsetRenderable.subset.offset); |
940 | dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
941 | |
942 | if (blendParticles) |
943 | QSSGParticleRenderer::prepareParticlesForModel(shaderPipeline&: *shaderPipeline, rhiCtx, bindings, model: &subsetRenderable.modelContext.model); |
944 | |
945 | // Skinning |
946 | if (QRhiTexture *boneTexture = inData.getBonemapTexture(modelContext: subsetRenderable.modelContext)) { |
947 | int binding = shaderPipeline->bindingForTexture(name: "qt_boneTexture"); |
948 | if (binding >= 0) { |
949 | QRhiSampler *boneSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
950 | .magFilter: QRhiSampler::Nearest, |
951 | .mipmap: QRhiSampler::None, |
952 | .hTiling: QRhiSampler::ClampToEdge, |
953 | .vTiling: QRhiSampler::ClampToEdge, |
954 | .zTiling: QRhiSampler::Repeat |
955 | }); |
956 | bindings.addTexture(binding, |
957 | stage: QRhiShaderResourceBinding::VertexStage, |
958 | tex: boneTexture, |
959 | sampler: boneSampler); |
960 | } |
961 | } |
962 | // Morphing |
963 | auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture; |
964 | if (targetsTexture) { |
965 | int binding = shaderPipeline->bindingForTexture(name: "qt_morphTargetTexture"); |
966 | if (binding >= 0) { |
967 | QRhiSampler *targetsSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
968 | .magFilter: QRhiSampler::Nearest, |
969 | .mipmap: QRhiSampler::None, |
970 | .hTiling: QRhiSampler::ClampToEdge, |
971 | .vTiling: QRhiSampler::ClampToEdge, |
972 | .zTiling: QRhiSampler::ClampToEdge |
973 | }); |
974 | bindings.addTexture(binding, stage: QRhiShaderResourceBinding::VertexStage, tex: subsetRenderable.subset.rhi.targetsTexture, sampler: targetsSampler); |
975 | } |
976 | } |
977 | |
978 | ps->samples = samples; |
979 | ps->viewCount = viewCount; |
980 | |
981 | const auto &material = static_cast<const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial()); |
982 | ps->cullMode = QSSGRhiHelpers::toCullMode(cullFaceMode: material.cullMode); |
983 | fillTargetBlend(targetBlend: &ps->targetBlend, materialBlend: material.blendMode); |
984 | |
985 | auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps&: *ps); |
986 | |
987 | ia = subsetRenderable.subset.rhi.ia; |
988 | const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData); |
989 | QVector3D cameraDirection = cameraDatas[0].direction; |
990 | if (alteredCamera) |
991 | cameraDirection = alteredCamera->getScalingCorrectDirection(); |
992 | QVector3D cameraPosition = cameraDatas[0].position; |
993 | if (alteredCamera) |
994 | cameraPosition = alteredCamera->getGlobalPos(); |
995 | int instanceBufferBinding = setupInstancing(renderable: &subsetRenderable, ps, rhiCtx, cameraDirection, cameraPosition); |
996 | QSSGRhiHelpers::bakeVertexInputLocations(ia: &ia, shaders: *shaderPipeline, instanceBufferBinding); |
997 | |
998 | bindings.addUniformBuffer(binding: 0, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf, offset: 0, size: shaderPipeline->ub0Size()); |
999 | |
1000 | if (shaderPipeline->isLightingEnabled()) { |
1001 | bindings.addUniformBuffer(binding: 1, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf, |
1002 | offset: shaderPipeline->ub0LightDataOffset(), |
1003 | size: shaderPipeline->ub0LightDataSize()); |
1004 | |
1005 | if (shaderPipeline->shadowMapCount() > 0) { |
1006 | bindings.addUniformBuffer(binding: 2, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf, |
1007 | offset: shaderPipeline->ub0ShadowDataOffset(), |
1008 | size: shaderPipeline->ub0ShadowDataSize()); |
1009 | } |
1010 | } |
1011 | |
1012 | // Texture maps |
1013 | QSSGRenderableImage *renderableImage = subsetRenderable.firstImage; |
1014 | while (renderableImage) { |
1015 | const char *samplerName = QSSGMaterialShaderGenerator::getSamplerName(type: renderableImage->m_mapType); |
1016 | const int samplerHint = int(renderableImage->m_mapType); |
1017 | int samplerBinding = shaderPipeline->bindingForTexture(name: samplerName, hint: samplerHint); |
1018 | if (samplerBinding >= 0) { |
1019 | QRhiTexture *texture = renderableImage->m_texture.m_texture; |
1020 | if (samplerBinding >= 0 && texture) { |
1021 | const bool mipmapped = texture->flags().testFlag(flag: QRhiTexture::MipMapped); |
1022 | QSSGRhiSamplerDescription samplerDesc = { |
1023 | .minFilter: QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_minFilterType), |
1024 | .magFilter: QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_magFilterType), |
1025 | .mipmap: mipmapped ? QSSGRhiHelpers::toRhi(op: renderableImage->m_imageNode.m_mipFilterType) : QRhiSampler::None, |
1026 | .hTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_horizontalTilingMode), |
1027 | .vTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_verticalTilingMode), |
1028 | .zTiling: QSSGRhiHelpers::toRhi(tiling: renderableImage->m_imageNode.m_depthTilingMode) |
1029 | }; |
1030 | rhiCtx->checkAndAdjustForNPoT(texture, samplerDescription: &samplerDesc); |
1031 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: samplerDesc); |
1032 | bindings.addTexture(binding: samplerBinding, stage: RENDERER_VISIBILITY_ALL, tex: texture, sampler); |
1033 | } |
1034 | } // else this is not necessarily an error, e.g. having metalness/roughness maps with metalness disabled |
1035 | renderableImage = renderableImage->m_nextImage; |
1036 | } |
1037 | |
1038 | if (shaderPipeline->isLightingEnabled()) { |
1039 | // Shadow map textures |
1040 | const int shadowMapCount = shaderPipeline->shadowMapCount(); |
1041 | QVarLengthArray<QSize, 4> usedTextureArraySizes; |
1042 | for (int i = 0; i < shadowMapCount; ++i) { |
1043 | QSSGRhiShadowMapProperties &shadowMapProperties(shaderPipeline->shadowMapAt(index: i)); |
1044 | const QByteArray &name(shadowMapProperties.shadowMapTextureUniformName); |
1045 | if (shadowMapProperties.cachedBinding < 0) |
1046 | shadowMapProperties.cachedBinding = shaderPipeline->bindingForTexture(name); |
1047 | if (shadowMapProperties.cachedBinding < 0) { |
1048 | qWarning(msg: "No combined image sampler for shadow map texture '%s'", name.data()); |
1049 | continue; |
1050 | } |
1051 | |
1052 | // Re-use same texture array if already created |
1053 | if (shadowMapProperties.shadowMapTexture->flags() & QRhiTexture::TextureArray) { |
1054 | if (usedTextureArraySizes.contains(t: shadowMapProperties.shadowMapTexture->pixelSize())) |
1055 | continue; |
1056 | usedTextureArraySizes.append(t: shadowMapProperties.shadowMapTexture->pixelSize()); |
1057 | } |
1058 | |
1059 | QRhiTexture *texture = shadowMapProperties.shadowMapTexture; |
1060 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: QRhiSampler::None, |
1061 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
1062 | Q_ASSERT(texture && sampler); |
1063 | bindings.addTexture(binding: shadowMapProperties.cachedBinding, stage: QRhiShaderResourceBinding::FragmentStage, |
1064 | tex: texture, sampler); |
1065 | } |
1066 | |
1067 | // Prioritize reflection texture over Light Probe texture because |
1068 | // reflection texture also contains the irradiance and pre filtered |
1069 | // values for the light probe. |
1070 | if (featureSet.isSet(feature: QSSGShaderFeatures::Feature::ReflectionProbe)) { |
1071 | int reflectionSampler = shaderPipeline->bindingForTexture(name: "qt_reflectionMap"); |
1072 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: QRhiSampler::Linear, |
1073 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
1074 | QRhiTexture* reflectionTexture = inData.getReflectionMapManager()->reflectionMapEntry(probeIdx: subsetRenderable.reflectionProbeIndex)->m_rhiPrefilteredCube; |
1075 | if (reflectionSampler >= 0 && reflectionTexture) |
1076 | bindings.addTexture(binding: reflectionSampler, stage: QRhiShaderResourceBinding::FragmentStage, tex: reflectionTexture, sampler); |
1077 | } else if (shaderPipeline->lightProbeTexture()) { |
1078 | int binding = shaderPipeline->bindingForTexture(name: "qt_lightProbe", hint: int(QSSGRhiSamplerBindingHints::LightProbe)); |
1079 | if (binding >= 0) { |
1080 | auto tiling = shaderPipeline->lightProbeTiling(); |
1081 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: QRhiSampler::Linear, // enables mipmapping |
1082 | .hTiling: QSSGRhiHelpers::toRhi(tiling: tiling.first), .vTiling: QSSGRhiHelpers::toRhi(tiling: tiling.second), .zTiling: QRhiSampler::Repeat }); |
1083 | bindings.addTexture(binding, stage: QRhiShaderResourceBinding::FragmentStage, |
1084 | tex: shaderPipeline->lightProbeTexture(), sampler); |
1085 | } else { |
1086 | qWarning(msg: "Could not find sampler for lightprobe"); |
1087 | } |
1088 | } |
1089 | |
1090 | // Screen Texture |
1091 | if (shaderPipeline->screenTexture()) { |
1092 | const int screenTextureBinding = shaderPipeline->bindingForTexture(name: "qt_screenTexture", hint: int(QSSGRhiSamplerBindingHints::ScreenTexture)); |
1093 | const int screenTextureArrayBinding = shaderPipeline->bindingForTexture(name: "qt_screenTextureArray", hint: int(QSSGRhiSamplerBindingHints::ScreenTextureArray)); |
1094 | if (screenTextureBinding >= 0 || screenTextureArrayBinding >= 0) { |
1095 | // linear min/mag, mipmap filtering depends on the |
1096 | // texture, with SCREEN_TEXTURE there are no mipmaps, but |
1097 | // once SCREEN_MIP_TEXTURE is seen the texture (the same |
1098 | // one) has mipmaps generated. |
1099 | QRhiSampler::Filter mipFilter = shaderPipeline->screenTexture()->flags().testFlag(flag: QRhiTexture::MipMapped) |
1100 | ? QRhiSampler::Linear : QRhiSampler::None; |
1101 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: mipFilter, |
1102 | .hTiling: QRhiSampler::Repeat, .vTiling: QRhiSampler::Repeat, .zTiling: QRhiSampler::Repeat }); |
1103 | if (screenTextureBinding >= 0) { |
1104 | bindings.addTexture(binding: screenTextureBinding, |
1105 | stage: QRhiShaderResourceBinding::FragmentStage, |
1106 | tex: shaderPipeline->screenTexture(), sampler); |
1107 | } |
1108 | if (screenTextureArrayBinding >= 0) { |
1109 | bindings.addTexture(binding: screenTextureArrayBinding, |
1110 | stage: QRhiShaderResourceBinding::FragmentStage, |
1111 | tex: shaderPipeline->screenTexture(), sampler); |
1112 | } |
1113 | } // else ignore, not an error |
1114 | } |
1115 | |
1116 | if (shaderPipeline->lightmapTexture()) { |
1117 | int binding = shaderPipeline->bindingForTexture(name: "qt_lightmap", hint: int(QSSGRhiSamplerBindingHints::LightmapTexture)); |
1118 | if (binding >= 0) { |
1119 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, .magFilter: QRhiSampler::Linear, .mipmap: QRhiSampler::None, |
1120 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
1121 | bindings.addTexture(binding, |
1122 | stage: QRhiShaderResourceBinding::FragmentStage, |
1123 | tex: shaderPipeline->lightmapTexture(), sampler); |
1124 | } // else ignore, not an error |
1125 | } |
1126 | } |
1127 | |
1128 | // Depth and SSAO textures |
1129 | addDepthTextureBindings(rhiCtx, shaderPipeline: shaderPipeline.get(), bindings); |
1130 | |
1131 | // Instead of always doing a QHash find in srb(), store the binding |
1132 | // list and the srb object in the per-model+material |
1133 | // QSSGRhiUniformBufferSet. While this still needs comparing the |
1134 | // binding list, to see if something has changed, it results in |
1135 | // significant gains with lots of models in the scene (because the |
1136 | // srb hash table becomes large then, so avoiding the lookup as |
1137 | // much as possible is helpful) |
1138 | QRhiShaderResourceBindings *&srb = dcd.srb; |
1139 | bool srbChanged = false; |
1140 | if (!srb || bindings != dcd.bindings) { |
1141 | srb = rhiCtxD->srb(bindings); |
1142 | rhiCtxD->releaseCachedSrb(bindings&: dcd.bindings); |
1143 | dcd.bindings = bindings; |
1144 | srbChanged = true; |
1145 | } |
1146 | |
1147 | if (cubeFace != QSSGRenderTextureCubeFaceNone) |
1148 | subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx] = srb; |
1149 | else |
1150 | subsetRenderable.rhiRenderData.mainPass.srb = srb; |
1151 | |
1152 | const auto pipelineKey = QSSGGraphicsPipelineStateKey::create(state: *ps, rpDesc: renderPassDescriptor, srb); |
1153 | if (dcd.pipeline |
1154 | && !srbChanged |
1155 | && dcd.renderTargetDescriptionHash == pipelineKey.extra.renderTargetDescriptionHash // we have the hash code anyway, use it to early out upon mismatch |
1156 | && dcd.renderTargetDescription == pipelineKey.renderTargetDescription |
1157 | && dcd.ps == *ps) |
1158 | { |
1159 | if (cubeFace != QSSGRenderTextureCubeFaceNone) |
1160 | subsetRenderable.rhiRenderData.reflectionPass.pipeline = dcd.pipeline; |
1161 | else |
1162 | subsetRenderable.rhiRenderData.mainPass.pipeline = dcd.pipeline; |
1163 | } else { |
1164 | if (cubeFace != QSSGRenderTextureCubeFaceNone) { |
1165 | subsetRenderable.rhiRenderData.reflectionPass.pipeline = rhiCtxD->pipeline(key: pipelineKey, |
1166 | rpDesc: renderPassDescriptor, |
1167 | srb); |
1168 | dcd.pipeline = subsetRenderable.rhiRenderData.reflectionPass.pipeline; |
1169 | } else { |
1170 | subsetRenderable.rhiRenderData.mainPass.pipeline = rhiCtxD->pipeline(key: pipelineKey, |
1171 | rpDesc: renderPassDescriptor, |
1172 | srb); |
1173 | dcd.pipeline = subsetRenderable.rhiRenderData.mainPass.pipeline; |
1174 | } |
1175 | dcd.renderTargetDescriptionHash = pipelineKey.extra.renderTargetDescriptionHash; |
1176 | dcd.renderTargetDescription = pipelineKey.renderTargetDescription; |
1177 | dcd.ps = *ps; |
1178 | } |
1179 | } |
1180 | break; |
1181 | } |
1182 | case QSSGRenderableObject::Type::CustomMaterialMeshSubset: |
1183 | { |
1184 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(inObject)); |
1185 | const QSSGRenderCustomMaterial &material = static_cast<const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial()); |
1186 | QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get()); |
1187 | |
1188 | featureSet.set(feature: QSSGShaderFeatures::Feature::LightProbe, val: inData.layer.lightProbe || material.m_iblProbe); |
1189 | |
1190 | if ((cubeFace == QSSGRenderTextureCubeFaceNone) && subsetRenderable.reflectionProbeIndex >= 0 && subsetRenderable.renderableFlags.testFlag(flag: QSSGRenderableObjectFlag::ReceivesReflections)) |
1191 | featureSet.set(feature: QSSGShaderFeatures::Feature::ReflectionProbe, val: true); |
1192 | |
1193 | if (cubeFace != QSSGRenderTextureCubeFaceNone) { |
1194 | // Disable tonemapping for the reflection pass |
1195 | featureSet.disableTonemapping(); |
1196 | } |
1197 | |
1198 | if (subsetRenderable.renderableFlags.rendersWithLightmap()) |
1199 | featureSet.set(feature: QSSGShaderFeatures::Feature::Lightmap, val: true); |
1200 | |
1201 | customMaterialSystem.rhiPrepareRenderable(ps, passKey, renderable&: subsetRenderable, featureSet, |
1202 | material, layerData: inData, renderPassDescriptor, samples, viewCount, |
1203 | camera: alteredCamera, cubeFace, modelViewProjection: alteredModelViewProjection, entry); |
1204 | break; |
1205 | } |
1206 | case QSSGRenderableObject::Type::Particles: |
1207 | { |
1208 | QSSGParticlesRenderable &particleRenderable(static_cast<QSSGParticlesRenderable &>(inObject)); |
1209 | const auto &shaderPipeline = shadersForParticleMaterial(ps, particleRenderable); |
1210 | if (shaderPipeline) { |
1211 | QSSGParticleRenderer::rhiPrepareRenderable(shaderPipeline&: *shaderPipeline, passKey, rhiCtx, ps, renderable&: particleRenderable, inData, renderPassDescriptor, samples, viewCount, |
1212 | alteredCamera, cubeFace, entry); |
1213 | } |
1214 | break; |
1215 | } |
1216 | } |
1217 | } |
1218 | |
1219 | void RenderHelpers::rhiRenderRenderable(QSSGRhiContext *rhiCtx, |
1220 | const QSSGRhiGraphicsPipelineState &state, |
1221 | QSSGRenderableObject &object, |
1222 | bool *needsSetViewport, |
1223 | QSSGRenderTextureCubeFace cubeFace) |
1224 | { |
1225 | switch (object.type) { |
1226 | case QSSGRenderableObject::Type::DefaultMaterialMeshSubset: |
1227 | { |
1228 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(object)); |
1229 | |
1230 | QRhiGraphicsPipeline *ps = subsetRenderable.rhiRenderData.mainPass.pipeline; |
1231 | QRhiShaderResourceBindings *srb = subsetRenderable.rhiRenderData.mainPass.srb; |
1232 | |
1233 | if (cubeFace != QSSGRenderTextureCubeFaceNone) { |
1234 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face: cubeFace); |
1235 | ps = subsetRenderable.rhiRenderData.reflectionPass.pipeline; |
1236 | srb = subsetRenderable.rhiRenderData.reflectionPass.srb[cubeFaceIdx]; |
1237 | } |
1238 | |
1239 | if (!ps || !srb) |
1240 | return; |
1241 | |
1242 | QRhiBuffer *vertexBuffer = subsetRenderable.subset.rhi.vertexBuffer->buffer(); |
1243 | QRhiBuffer *indexBuffer = subsetRenderable.subset.rhi.indexBuffer ? subsetRenderable.subset.rhi.indexBuffer->buffer() : nullptr; |
1244 | |
1245 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1246 | // QRhi optimizes out unnecessary binding of the same pipline |
1247 | cb->setGraphicsPipeline(ps); |
1248 | cb->setShaderResources(srb); |
1249 | |
1250 | if (*needsSetViewport) { |
1251 | cb->setViewport(state.viewport); |
1252 | if (state.flags.testFlag(flag: QSSGRhiGraphicsPipelineState::Flag::UsesScissor)) |
1253 | cb->setScissor(state.scissor); |
1254 | *needsSetViewport = false; |
1255 | } |
1256 | |
1257 | QRhiCommandBuffer::VertexInput vertexBuffers[2]; |
1258 | int vertexBufferCount = 1; |
1259 | vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0); |
1260 | quint32 instances = 1; |
1261 | if ( subsetRenderable.modelContext.model.instancing()) { |
1262 | instances = subsetRenderable.modelContext.model.instanceCount(); |
1263 | // If the instance count is 0, the bail out before trying to do any |
1264 | // draw calls. Making an instanced draw call with a count of 0 is invalid |
1265 | // for Metal and likely other API's as well. |
1266 | // It is possible that the particale system may produce 0 instances here |
1267 | if (instances == 0) |
1268 | return; |
1269 | vertexBuffers[1] = QRhiCommandBuffer::VertexInput(subsetRenderable.instanceBuffer, 0); |
1270 | vertexBufferCount = 2; |
1271 | } |
1272 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall); |
1273 | if (state.flags.testFlag(flag: QSSGRhiGraphicsPipelineState::Flag::UsesStencilRef)) |
1274 | cb->setStencilRef(state.stencilRef); |
1275 | if (indexBuffer) { |
1276 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers, indexBuf: indexBuffer, indexOffset: 0, indexFormat: subsetRenderable.subset.rhi.indexBuffer->indexFormat()); |
1277 | cb->drawIndexed(indexCount: subsetRenderable.subset.lodCount(lodLevel: subsetRenderable.subsetLevelOfDetail), instanceCount: instances, firstIndex: subsetRenderable.subset.lodOffset(lodLevel: subsetRenderable.subsetLevelOfDetail)); |
1278 | QSSGRHICTX_STAT(rhiCtx, drawIndexed(subsetRenderable.subset.lodCount(subsetRenderable.subsetLevelOfDetail), instances)); |
1279 | } else { |
1280 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers); |
1281 | cb->draw(vertexCount: subsetRenderable.subset.count, instanceCount: instances, firstVertex: subsetRenderable.subset.offset); |
1282 | QSSGRHICTX_STAT(rhiCtx, draw(subsetRenderable.subset.count, instances)); |
1283 | } |
1284 | Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (subsetRenderable.subset.count | quint64(instances) << 32), |
1285 | QVector<int>({subsetRenderable.modelContext.model.profilingId, |
1286 | subsetRenderable.material.profilingId})); |
1287 | break; |
1288 | } |
1289 | case QSSGRenderableObject::Type::CustomMaterialMeshSubset: |
1290 | { |
1291 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(object)); |
1292 | QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get()); |
1293 | customMaterialSystem.rhiRenderRenderable(rhiCtx, renderable&: subsetRenderable, needsSetViewport, cubeFace, state); |
1294 | break; |
1295 | } |
1296 | case QSSGRenderableObject::Type::Particles: |
1297 | { |
1298 | QSSGParticlesRenderable &renderable(static_cast<QSSGParticlesRenderable &>(object)); |
1299 | QSSGParticleRenderer::rhiRenderRenderable(rhiCtx, renderable, needsSetViewport, cubeFace, state); |
1300 | break; |
1301 | } |
1302 | } |
1303 | } |
1304 | |
1305 | void RenderHelpers::rhiRenderShadowMap(QSSGRhiContext *rhiCtx, |
1306 | QSSGPassKey passKey, |
1307 | QSSGRhiGraphicsPipelineState &ps, |
1308 | QSSGRenderShadowMap &shadowMapManager, |
1309 | const QSSGRenderCamera &camera, |
1310 | QSSGRenderCamera *debugCamera, |
1311 | const QSSGShaderLightList &globalLights, |
1312 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
1313 | QSSGRenderer &renderer, |
1314 | const QSSGBounds3 &castingObjectsBox, |
1315 | const QSSGBounds3 &receivingObjectsBox) |
1316 | { |
1317 | const QSSGLayerRenderData &layerData = *QSSGLayerRenderData::getCurrent(renderer); |
1318 | |
1319 | static const auto rhiRenderOneShadowMap = [](QSSGRhiContext *rhiCtx, |
1320 | QSSGRhiGraphicsPipelineState *ps, |
1321 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
1322 | int cubeFace) { |
1323 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1324 | bool needsSetViewport = true; |
1325 | |
1326 | for (const auto &handle : sortedOpaqueObjects) { |
1327 | QSSGRenderableObject *theObject = handle.obj; |
1328 | QSSG_ASSERT(theObject->renderableFlags.castsShadows(), continue); |
1329 | if (theObject->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || theObject->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
1330 | QSSGSubsetRenderable *renderable(static_cast<QSSGSubsetRenderable *>(theObject)); |
1331 | |
1332 | QRhiBuffer *vertexBuffer = renderable->subset.rhi.vertexBuffer->buffer(); |
1333 | QRhiBuffer *indexBuffer = renderable->subset.rhi.indexBuffer |
1334 | ? renderable->subset.rhi.indexBuffer->buffer() |
1335 | : nullptr; |
1336 | |
1337 | // Ideally we shouldn't need to deal with this, as only "valid" objects should be processed at this point. |
1338 | if (!renderable->rhiRenderData.shadowPass.pipeline) |
1339 | continue; |
1340 | |
1341 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall); |
1342 | |
1343 | cb->setGraphicsPipeline(renderable->rhiRenderData.shadowPass.pipeline); |
1344 | |
1345 | QRhiShaderResourceBindings *srb = renderable->rhiRenderData.shadowPass.srb[cubeFace]; |
1346 | cb->setShaderResources(srb); |
1347 | |
1348 | if (needsSetViewport) { |
1349 | cb->setViewport(ps->viewport); |
1350 | needsSetViewport = false; |
1351 | } |
1352 | |
1353 | QRhiCommandBuffer::VertexInput vertexBuffers[2]; |
1354 | int vertexBufferCount = 1; |
1355 | vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0); |
1356 | quint32 instances = 1; |
1357 | if (renderable->modelContext.model.instancing()) { |
1358 | instances = renderable->modelContext.model.instanceCount(); |
1359 | vertexBuffers[1] = QRhiCommandBuffer::VertexInput(renderable->instanceBuffer, 0); |
1360 | vertexBufferCount = 2; |
1361 | } |
1362 | if (indexBuffer) { |
1363 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers, indexBuf: indexBuffer, indexOffset: 0, indexFormat: renderable->subset.rhi.indexBuffer->indexFormat()); |
1364 | cb->drawIndexed(indexCount: renderable->subset.count, instanceCount: instances, firstIndex: renderable->subset.offset); |
1365 | QSSGRHICTX_STAT(rhiCtx, drawIndexed(renderable->subset.count, instances)); |
1366 | } else { |
1367 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers); |
1368 | cb->draw(vertexCount: renderable->subset.count, instanceCount: instances, firstVertex: renderable->subset.offset); |
1369 | QSSGRHICTX_STAT(rhiCtx, draw(renderable->subset.count, instances)); |
1370 | } |
1371 | Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (renderable->subset.count | quint64(instances) << 32), |
1372 | QVector<int>({renderable->modelContext.model.profilingId, |
1373 | renderable->material.profilingId})); |
1374 | } |
1375 | } |
1376 | }; |
1377 | |
1378 | QRhi *rhi = rhiCtx->rhi(); |
1379 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1380 | |
1381 | // We need to deal with a clip depth range of [0, 1] or |
1382 | // [-1, 1], depending on the graphics API underneath. |
1383 | QVector2D depthAdjust; // (d + depthAdjust[0]) * depthAdjust[1] = d mapped to [0, 1] |
1384 | if (rhi->isClipDepthZeroToOne()) { |
1385 | // d is [0, 1] so no need for any mapping |
1386 | depthAdjust[0] = 0.0f; |
1387 | depthAdjust[1] = 1.0f; |
1388 | } else { |
1389 | // d is [-1, 1] |
1390 | depthAdjust[0] = 1.0f; |
1391 | depthAdjust[1] = 0.5f; |
1392 | } |
1393 | |
1394 | QSSGDebugDrawSystem *debugDrawSystem = renderer.contextInterface()->debugDrawSystem().get(); |
1395 | const bool drawDirectionalLightShadowBoxes = layerData.layer.drawDirectionalLightShadowBoxes; |
1396 | const bool drawShadowCastingBounds = layerData.layer.drawShadowCastingBounds; |
1397 | const bool drawShadowReceivingBounds = layerData.layer.drawShadowReceivingBounds; |
1398 | const bool drawCascades = layerData.layer.drawCascades; |
1399 | const bool drawSceneCascadeIntersection = layerData.layer.drawSceneCascadeIntersection; |
1400 | const bool disableShadowCameraUpdate = layerData.layer.disableShadowCameraUpdate; |
1401 | |
1402 | if (drawShadowCastingBounds) |
1403 | ShadowmapHelpers::addDebugBox(boxUnsorted: castingObjectsBox.toQSSGBoxPointsNoEmptyCheck(), color: QColorConstants::Red, debugDrawSystem); |
1404 | if (drawShadowReceivingBounds) |
1405 | ShadowmapHelpers::addDebugBox(boxUnsorted: receivingObjectsBox.toQSSGBoxPointsNoEmptyCheck(), color: QColorConstants::Green, debugDrawSystem); |
1406 | |
1407 | // Create shadow map for each light in the scene |
1408 | for (int i = 0, ie = globalLights.size(); i != ie; ++i) { |
1409 | if (!globalLights[i].shadows || globalLights[i].light->m_fullyBaked) |
1410 | continue; |
1411 | |
1412 | QSSGShadowMapEntry *pEntry = shadowMapManager.shadowMapEntry(lightIdx: i); |
1413 | if (!pEntry) |
1414 | continue; |
1415 | |
1416 | const auto &light = globalLights[i].light; |
1417 | Q_ASSERT(pEntry->m_rhiDepthStencil[0]); |
1418 | if (pEntry->m_rhiDepthTextureArray) { |
1419 | const QSize size = pEntry->m_rhiDepthTextureArray->pixelSize(); |
1420 | ps.viewport = QRhiViewport(0, 0, float(size.width()), float(size.height())); |
1421 | |
1422 | Q_ASSERT(light->type == QSSGRenderLight::Type::DirectionalLight || light->type == QSSGRenderLight::Type::SpotLight); |
1423 | |
1424 | // This is just a way to store the old camera so we can use it for debug |
1425 | // drawing. There are probably cleaner ways to do this |
1426 | if (!disableShadowCameraUpdate && debugCamera) { |
1427 | debugCamera->clipNear = camera.clipNear; |
1428 | debugCamera->clipFar = camera.clipFar; |
1429 | debugCamera->projection = camera.projection; |
1430 | debugCamera->globalTransform = camera.globalTransform; |
1431 | } |
1432 | |
1433 | QVarLengthArray<std::unique_ptr<QSSGRenderCamera>, 4> cascades; |
1434 | if (light->type == QSSGRenderLight::Type::DirectionalLight) { |
1435 | const float pcfRadius = light->m_softShadowQuality == QSSGRenderLight::SoftShadowQuality::Hard ? 0.f : light->m_pcfFactor; |
1436 | cascades = setupCascadingCamerasForShadowMap(inCamera: disableShadowCameraUpdate ? *debugCamera : camera, |
1437 | inLight: light, |
1438 | shadowMapResolution: size.width(), |
1439 | pcfRadius, |
1440 | castingObjectsBox, |
1441 | receivingObjectsBox, |
1442 | debugDrawSystem, |
1443 | drawCascades, |
1444 | drawSceneCascadeIntersection); |
1445 | const float shadowMapFar = qMax(a: 2.0f, b: qMin(a: light->m_shadowMapFar, b: camera.clipFar)); |
1446 | |
1447 | // Write the split distances from value 0 in the z-axis of the eye view-space |
1448 | pEntry->m_csmSplits[0] = shadowMapFar * (light->m_csmNumSplits > 0 ? light->m_csmSplit1 : 1.0f); |
1449 | pEntry->m_csmSplits[1] = shadowMapFar * (light->m_csmNumSplits > 1 ? light->m_csmSplit2 : 1.0f); |
1450 | pEntry->m_csmSplits[2] = shadowMapFar * (light->m_csmNumSplits > 2 ? light->m_csmSplit3 : 1.0f); |
1451 | pEntry->m_csmSplits[3] = shadowMapFar * 1.0f; |
1452 | pEntry->m_shadowMapFar = shadowMapFar; |
1453 | } else if (light->type == QSSGRenderLight::Type::SpotLight) { |
1454 | auto spotlightCamera = std::make_unique<QSSGRenderCamera>(args: QSSGRenderCamera::Type::PerspectiveCamera); |
1455 | spotlightCamera->fov = qDegreesToRadians(degrees: light->m_coneAngle * 2.0f); |
1456 | spotlightCamera->clipNear = 1.0f; |
1457 | spotlightCamera->clipFar = light->m_shadowMapFar; |
1458 | const QVector3D lightDir = light->getDirection(); |
1459 | const QVector3D lightPos = light->getGlobalPos() - lightDir * spotlightCamera->clipNear; |
1460 | const QVector3D lightPivot = light->pivot; |
1461 | const QVector3D forward = lightDir.normalized(); |
1462 | const QVector3D right = qFuzzyCompare(p1: qAbs(t: forward.y()), p2: 1.0f) |
1463 | ? QVector3D::crossProduct(v1: forward, v2: QVector3D(1, 0, 0)).normalized() |
1464 | : QVector3D::crossProduct(v1: forward, v2: QVector3D(0, 1, 0)).normalized(); |
1465 | const QVector3D up = QVector3D::crossProduct(v1: right, v2: forward).normalized(); |
1466 | spotlightCamera->localTransform = QSSGRenderNode::calculateTransformMatrix(position: lightPos, |
1467 | scale: QSSGRenderNode::initScale, |
1468 | pivot: lightPivot, |
1469 | rotation: QQuaternion::fromDirection(direction: forward, up)); |
1470 | QRectF theViewport(0.0f, 0.0f, (float)light->m_shadowMapRes, (float)light->m_shadowMapRes); |
1471 | spotlightCamera->calculateGlobalVariables(inViewport: theViewport); |
1472 | cascades.push_back(t: std::move(spotlightCamera)); |
1473 | pEntry->m_shadowMapFar = light->m_shadowMapFar; |
1474 | } else { |
1475 | Q_UNREACHABLE(); |
1476 | } |
1477 | |
1478 | memset(s: pEntry->m_csmActive, c: 0, n: sizeof(pEntry->m_csmActive)); |
1479 | |
1480 | for (int cascadeIndex = 0; cascadeIndex < cascades.length(); cascadeIndex++) { |
1481 | const auto &cascadeCamera = cascades[cascadeIndex]; |
1482 | if (!cascadeCamera) |
1483 | continue; |
1484 | pEntry->m_csmActive[cascadeIndex] = 1.f; |
1485 | cascadeCamera->calculateViewProjectionMatrix(outMatrix&: pEntry->m_lightViewProjection[cascadeIndex]); |
1486 | pEntry->m_lightView = cascadeCamera->globalTransform.inverted(); // pre-calculate this for the material |
1487 | const bool isOrtho = cascadeCamera->type == QSSGRenderGraphObject::Type::OrthographicCamera; |
1488 | rhiPrepareResourcesForShadowMap(rhiCtx, inData: layerData, passKey, pEntry, ps: &ps, depthAdjust: &depthAdjust, sortedOpaqueObjects, inCamera&: *cascadeCamera, orthographic: isOrtho, cubeFace: QSSGRenderTextureCubeFaceNone, cascadeIndex); |
1489 | // Render into the 2D texture pEntry->m_rhiDepthMap, using |
1490 | // pEntry->m_rhiDepthStencil as the (throwaway) depth/stencil buffer. |
1491 | QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[cascadeIndex]; |
1492 | cb->beginPass(rt, colorClearValue: Qt::white, depthStencilClearValue: { 1.0f, 0 }, resourceUpdates: nullptr, flags: rhiCtx->commonPassFlags()); |
1493 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass); |
1494 | QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt)); |
1495 | rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, 0); |
1496 | cb->endPass(); |
1497 | QSSGRHICTX_STAT(rhiCtx, endRenderPass()); |
1498 | |
1499 | if (drawDirectionalLightShadowBoxes) |
1500 | ShadowmapHelpers::addDirectionalLightDebugBox(box: computeFrustumBounds(inCamera: *cascadeCamera), debugDrawSystem); |
1501 | } |
1502 | Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QByteArrayLiteral("shadow_map")); |
1503 | } else { |
1504 | Q_ASSERT(pEntry->m_rhiDepthCube); |
1505 | const QSize size = pEntry->m_rhiDepthCube->pixelSize(); |
1506 | ps.viewport = QRhiViewport(0, 0, float(size.width()), float(size.height())); |
1507 | |
1508 | QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1509 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1510 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1511 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1512 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1513 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} }; |
1514 | const float shadowMapFar = qMax<float>(a: 2.0f, b: light->m_shadowMapFar); |
1515 | setupCubeShadowCameras(inLight: light, shadowMapFar, inCameras: theCameras); |
1516 | pEntry->m_lightView = QMatrix4x4(); |
1517 | pEntry->m_shadowMapFar = shadowMapFar; |
1518 | |
1519 | const bool swapYFaces = !rhi->isYUpInFramebuffer(); |
1520 | for (const auto face : QSSGRenderTextureCubeFaces) { |
1521 | theCameras[quint8(face)].calculateViewProjectionMatrix(outMatrix&: pEntry->m_lightViewProjection[0]); |
1522 | pEntry->m_lightCubeView[quint8(face)] = theCameras[quint8(face)].globalTransform.inverted(); // pre-calculate this for the material |
1523 | |
1524 | rhiPrepareResourcesForShadowMap(rhiCtx, |
1525 | inData: layerData, |
1526 | passKey, |
1527 | pEntry, |
1528 | ps: &ps, |
1529 | depthAdjust: &depthAdjust, |
1530 | sortedOpaqueObjects, |
1531 | inCamera&: theCameras[quint8(face)], |
1532 | orthographic: false, |
1533 | cubeFace: face, |
1534 | cascadeIndex: 0); |
1535 | } |
1536 | |
1537 | for (const auto face : QSSGRenderTextureCubeFaces) { |
1538 | // Render into one face of the cubemap texture pEntry->m_rhiDephCube, using |
1539 | // pEntry->m_rhiDepthStencil as the (throwaway) depth/stencil buffer. |
1540 | |
1541 | QSSGRenderTextureCubeFace outFace = face; |
1542 | // FACE S T GL |
1543 | // +x -z, -y right |
1544 | // -x +z, -y left |
1545 | // +y +x, +z top |
1546 | // -y +x, -z bottom |
1547 | // +z +x, -y front |
1548 | // -z -x, -y back |
1549 | // FACE S T D3D |
1550 | // +x -z, +y right |
1551 | // -x +z, +y left |
1552 | // +y +x, -z bottom |
1553 | // -y +x, +z top |
1554 | // +z +x, +y front |
1555 | // -z -x, +y back |
1556 | if (swapYFaces) { |
1557 | // +Y and -Y faces get swapped (D3D, Vulkan, Metal). |
1558 | // See shadowMapping.glsllib. This is complemented there by reversing T as well. |
1559 | if (outFace == QSSGRenderTextureCubeFace::PosY) |
1560 | outFace = QSSGRenderTextureCubeFace::NegY; |
1561 | else if (outFace == QSSGRenderTextureCubeFace::NegY) |
1562 | outFace = QSSGRenderTextureCubeFace::PosY; |
1563 | } |
1564 | QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[quint8(outFace)]; |
1565 | cb->beginPass(rt, colorClearValue: Qt::white, depthStencilClearValue: { 1.0f, 0 }, resourceUpdates: nullptr, flags: rhiCtx->commonPassFlags()); |
1566 | QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt)); |
1567 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass); |
1568 | rhiRenderOneShadowMap(rhiCtx, &ps, sortedOpaqueObjects, quint8(face)); |
1569 | cb->endPass(); |
1570 | QSSGRHICTX_STAT(rhiCtx, endRenderPass()); |
1571 | Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME("shadow_cube", 0, outFace)); |
1572 | } |
1573 | } |
1574 | } |
1575 | } |
1576 | |
1577 | void RenderHelpers::rhiRenderReflectionMap(QSSGRhiContext *rhiCtx, |
1578 | QSSGPassKey passKey, |
1579 | const QSSGLayerRenderData &inData, |
1580 | QSSGRhiGraphicsPipelineState *ps, |
1581 | QSSGRenderReflectionMap &reflectionMapManager, |
1582 | const QVector<QSSGRenderReflectionProbe *> &reflectionProbes, |
1583 | const QSSGRenderableObjectList &reflectionPassObjects, |
1584 | QSSGRenderer &renderer) |
1585 | { |
1586 | QRhi *rhi = rhiCtx->rhi(); |
1587 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1588 | |
1589 | const bool renderSkybox = (inData.layer.background == QSSGRenderLayer::Background::SkyBox || |
1590 | inData.layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap) |
1591 | && rhiCtx->rhi()->isFeatureSupported(feature: QRhi::TexelFetch); |
1592 | |
1593 | for (int i = 0, ie = reflectionProbes.size(); i != ie; ++i) { |
1594 | QSSGReflectionMapEntry *pEntry = reflectionMapManager.reflectionMapEntry(probeIdx: i); |
1595 | if (!pEntry) |
1596 | continue; |
1597 | |
1598 | if (!pEntry->m_needsRender) |
1599 | continue; |
1600 | |
1601 | if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame && pEntry->m_rendered) |
1602 | continue; |
1603 | |
1604 | if (reflectionProbes[i]->texture) |
1605 | continue; |
1606 | |
1607 | Q_ASSERT(pEntry->m_rhiDepthStencil); |
1608 | Q_ASSERT(pEntry->m_rhiCube); |
1609 | |
1610 | const QSize size = pEntry->m_rhiCube->pixelSize(); |
1611 | ps->viewport = QRhiViewport(0, 0, float(size.width()), float(size.height())); |
1612 | |
1613 | QSSGRenderCamera theCameras[6] { QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1614 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1615 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1616 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1617 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera}, |
1618 | QSSGRenderCamera{QSSGRenderCamera::Type::PerspectiveCamera} }; |
1619 | setupCubeReflectionCameras(inProbe: reflectionProbes[i], inCameras: theCameras); |
1620 | const bool swapYFaces = !rhi->isYUpInFramebuffer(); |
1621 | for (const auto face : QSSGRenderTextureCubeFaces) { |
1622 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face); |
1623 | theCameras[cubeFaceIdx].calculateViewProjectionMatrix(outMatrix&: pEntry->m_viewProjection); |
1624 | |
1625 | rhiPrepareResourcesForReflectionMap(rhiCtx, passKey, inData, pEntry, ps, |
1626 | sortedOpaqueObjects: reflectionPassObjects, inCamera&: theCameras[cubeFaceIdx], renderer, cubeFace: face); |
1627 | } |
1628 | QRhiRenderPassDescriptor *renderPassDesc = nullptr; |
1629 | for (auto face : QSSGRenderTextureCubeFaces) { |
1630 | if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces) |
1631 | face = pEntry->m_timeSliceFace; |
1632 | |
1633 | QSSGRenderTextureCubeFace outFace = face; |
1634 | // Faces are swapped similarly to shadow maps due to differences in backends |
1635 | // Prefilter step handles correcting orientation differences in the final render |
1636 | if (swapYFaces) { |
1637 | if (outFace == QSSGRenderTextureCubeFace::PosY) |
1638 | outFace = QSSGRenderTextureCubeFace::NegY; |
1639 | else if (outFace == QSSGRenderTextureCubeFace::NegY) |
1640 | outFace = QSSGRenderTextureCubeFace::PosY; |
1641 | } |
1642 | QRhiTextureRenderTarget *rt = pEntry->m_rhiRenderTargets[quint8(outFace)]; |
1643 | cb->beginPass(rt, colorClearValue: reflectionProbes[i]->clearColor, depthStencilClearValue: { 1.0f, 0 }, resourceUpdates: nullptr, flags: rhiCtx->commonPassFlags()); |
1644 | QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rt)); |
1645 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderPass); |
1646 | |
1647 | if (renderSkybox && pEntry->m_skyBoxSrbs[quint8(face)]) { |
1648 | const auto &shaderCache = renderer.contextInterface()->shaderCache(); |
1649 | const bool isSkyBox = inData.layer.background == QSSGRenderLayer::Background::SkyBox; |
1650 | const auto &shaderPipeline = isSkyBox ? shaderCache->getBuiltInRhiShaders().getRhiSkyBoxShader(tonemapMode: QSSGRenderLayer::TonemapMode::None, isRGBE: inData.layer.skyBoxIsRgbe8, viewCount: 1) |
1651 | : shaderCache->getBuiltInRhiShaders().getRhiSkyBoxCubeShader(viewCount: 1); |
1652 | Q_ASSERT(shaderPipeline); |
1653 | QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps&: *ps, pipeline: shaderPipeline.get()); |
1654 | QRhiShaderResourceBindings *srb = pEntry->m_skyBoxSrbs[quint8(face)]; |
1655 | if (!renderPassDesc) |
1656 | renderPassDesc = rt->newCompatibleRenderPassDescriptor(); |
1657 | rt->setRenderPassDescriptor(renderPassDesc); |
1658 | isSkyBox ? renderer.rhiQuadRenderer()->recordRenderQuad(rhiCtx, ps, srb, rpDesc: renderPassDesc, flags: {}) |
1659 | : renderer.rhiCubeRenderer()->recordRenderCube(rhiCtx, ps, srb, rpDesc: renderPassDesc, flags: {}); |
1660 | } |
1661 | |
1662 | bool needsSetViewport = true; |
1663 | for (const auto &handle : reflectionPassObjects) |
1664 | rhiRenderRenderable(rhiCtx, state: *ps, object&: *handle.obj, needsSetViewport: &needsSetViewport, cubeFace: face); |
1665 | |
1666 | cb->endPass(); |
1667 | QSSGRHICTX_STAT(rhiCtx, endRenderPass()); |
1668 | Q_QUICK3D_PROFILE_END_WITH_STRING(QQuick3DProfiler::Quick3DRenderPass, 0, QSSG_RENDERPASS_NAME("reflection_cube", 0, outFace)); |
1669 | |
1670 | if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces) |
1671 | break; |
1672 | } |
1673 | if (renderPassDesc) |
1674 | renderPassDesc->deleteLater(); |
1675 | |
1676 | pEntry->renderMips(rhiCtx); |
1677 | |
1678 | if (pEntry->m_timeSlicing == QSSGRenderReflectionProbe::ReflectionTimeSlicing::IndividualFaces) |
1679 | pEntry->m_timeSliceFace = QSSGBaseTypeHelpers::next(face: pEntry->m_timeSliceFace); // Wraps |
1680 | |
1681 | if (reflectionProbes[i]->refreshMode == QSSGRenderReflectionProbe::ReflectionRefreshMode::FirstFrame) |
1682 | pEntry->m_rendered = true; |
1683 | |
1684 | reflectionProbes[i]->hasScheduledUpdate = false; |
1685 | pEntry->m_needsRender = false; |
1686 | } |
1687 | } |
1688 | |
1689 | bool RenderHelpers::rhiPrepareAoTexture(QSSGRhiContext *rhiCtx, const QSize &size, QSSGRhiRenderableTexture *renderableTex) |
1690 | { |
1691 | QRhi *rhi = rhiCtx->rhi(); |
1692 | bool needsBuild = false; |
1693 | |
1694 | if (!renderableTex->texture) { |
1695 | QRhiTexture::Flags flags = QRhiTexture::RenderTarget; |
1696 | // the ambient occlusion texture is always non-msaa, even if multisampling is used in the main pass |
1697 | if (rhiCtx->mainPassViewCount() <= 1) |
1698 | renderableTex->texture = rhi->newTexture(format: QRhiTexture::RGBA8, pixelSize: size, sampleCount: 1, flags); |
1699 | else |
1700 | renderableTex->texture = rhi->newTextureArray(format: QRhiTexture::RGBA8, arraySize: rhiCtx->mainPassViewCount(), pixelSize: size, sampleCount: 1, flags); |
1701 | needsBuild = true; |
1702 | } else if (renderableTex->texture->pixelSize() != size) { |
1703 | renderableTex->texture->setPixelSize(size); |
1704 | needsBuild = true; |
1705 | } |
1706 | |
1707 | if (needsBuild) { |
1708 | if (!renderableTex->texture->create()) { |
1709 | qWarning(msg: "Failed to build ambient occlusion texture (size %dx%d)", size.width(), size.height()); |
1710 | renderableTex->reset(); |
1711 | return false; |
1712 | } |
1713 | renderableTex->resetRenderTarget(); |
1714 | QRhiTextureRenderTargetDescription desc; |
1715 | QRhiColorAttachment colorAttachment(renderableTex->texture); |
1716 | colorAttachment.setMultiViewCount(rhiCtx->mainPassViewCount()); |
1717 | desc.setColorAttachments({ colorAttachment }); |
1718 | renderableTex->rt = rhi->newTextureRenderTarget(desc); |
1719 | renderableTex->rt->setName(QByteArrayLiteral("Ambient occlusion")); |
1720 | renderableTex->rpDesc = renderableTex->rt->newCompatibleRenderPassDescriptor(); |
1721 | renderableTex->rt->setRenderPassDescriptor(renderableTex->rpDesc); |
1722 | if (!renderableTex->rt->create()) { |
1723 | qWarning(msg: "Failed to build render target for ambient occlusion texture"); |
1724 | renderableTex->reset(); |
1725 | return false; |
1726 | } |
1727 | } |
1728 | |
1729 | return true; |
1730 | } |
1731 | |
1732 | void RenderHelpers::rhiRenderAoTexture(QSSGRhiContext *rhiCtx, |
1733 | QSSGPassKey passKey, |
1734 | QSSGRenderer &renderer, |
1735 | QSSGRhiShaderPipeline &shaderPipeline, |
1736 | QSSGRhiGraphicsPipelineState &ps, |
1737 | const QSSGAmbientOcclusionSettings &ao, |
1738 | const QSSGRhiRenderableTexture &rhiAoTexture, |
1739 | const QSSGRhiRenderableTexture &rhiDepthTexture, |
1740 | const QSSGRenderCamera &camera) |
1741 | { |
1742 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
1743 | |
1744 | // no texelFetch in GLSL <= 120 and GLSL ES 100 |
1745 | if (!rhiCtx->rhi()->isFeatureSupported(feature: QRhi::TexelFetch)) { |
1746 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1747 | // just clear and stop there |
1748 | cb->beginPass(rt: rhiAoTexture.rt, colorClearValue: Qt::white, depthStencilClearValue: { 1.0f, 0 }); |
1749 | QSSGRHICTX_STAT(rhiCtx, beginRenderPass(rhiAoTexture.rt)); |
1750 | cb->endPass(); |
1751 | QSSGRHICTX_STAT(rhiCtx, endRenderPass()); |
1752 | return; |
1753 | } |
1754 | |
1755 | QSSGRhiGraphicsPipelineStatePrivate::setShaderPipeline(ps, pipeline: &shaderPipeline); |
1756 | |
1757 | const float R2 = ao.aoDistance * ao.aoDistance * 0.16f; |
1758 | const QSize textureSize = rhiAoTexture.texture->pixelSize(); |
1759 | const float rw = float(textureSize.width()); |
1760 | const float rh = float(textureSize.height()); |
1761 | const float fov = camera.verticalFov(aspectRatio: rw / rh); |
1762 | const float tanHalfFovY = tanf(x: 0.5f * fov * (rh / rw)); |
1763 | const float invFocalLenX = tanHalfFovY * (rw / rh); |
1764 | |
1765 | const QVector4D aoProps(ao.aoStrength * 0.01f, ao.aoDistance * 0.4f, ao.aoSoftness * 0.02f, ao.aoBias); |
1766 | const QVector4D aoProps2(float(ao.aoSamplerate), (ao.aoDither) ? 1.0f : 0.0f, 0.0f, 0.0f); |
1767 | const QVector4D aoScreenConst(1.0f / R2, rh / (2.0f * tanHalfFovY), 1.0f / rw, 1.0f / rh); |
1768 | const QVector4D uvToEyeConst(2.0f * invFocalLenX, -2.0f * tanHalfFovY, -invFocalLenX, tanHalfFovY); |
1769 | const QVector2D cameraProps(camera.clipNear, camera.clipFar); |
1770 | |
1771 | // layout(std140, binding = 0) uniform buf { |
1772 | // vec4 aoProperties; |
1773 | // vec4 aoProperties2; |
1774 | // vec4 aoScreenConst; |
1775 | // vec4 uvToEyeConst; |
1776 | // vec2 cameraProperties; |
1777 | |
1778 | const int UBUF_SIZE = 72; |
1779 | QSSGRhiDrawCallData &dcd(rhiCtxD->drawCallData(key: { .cid: passKey, .model: nullptr, .entry: nullptr, .entryIdx: 0 })); |
1780 | if (!dcd.ubuf) { |
1781 | dcd.ubuf = rhiCtx->rhi()->newBuffer(type: QRhiBuffer::Dynamic, usage: QRhiBuffer::UniformBuffer, size: UBUF_SIZE); |
1782 | dcd.ubuf->create(); |
1783 | } |
1784 | |
1785 | char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
1786 | memcpy(dest: ubufData, src: &aoProps, n: 16); |
1787 | memcpy(dest: ubufData + 16, src: &aoProps2, n: 16); |
1788 | memcpy(dest: ubufData + 32, src: &aoScreenConst, n: 16); |
1789 | memcpy(dest: ubufData + 48, src: &uvToEyeConst, n: 16); |
1790 | memcpy(dest: ubufData + 64, src: &cameraProps, n: 8); |
1791 | dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
1792 | |
1793 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, .magFilter: QRhiSampler::Nearest, .mipmap: QRhiSampler::None, |
1794 | .hTiling: QRhiSampler::ClampToEdge, .vTiling: QRhiSampler::ClampToEdge, .zTiling: QRhiSampler::Repeat }); |
1795 | QSSGRhiShaderResourceBindingList bindings; |
1796 | bindings.addUniformBuffer(binding: 0, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf); |
1797 | // binding 1 is either a sampler2D or sampler2DArray, matching |
1798 | // rhiDepthTexture.texture, no special casing needed here |
1799 | bindings.addTexture(binding: 1, stage: QRhiShaderResourceBinding::FragmentStage, tex: rhiDepthTexture.texture, sampler); |
1800 | QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings); |
1801 | |
1802 | renderer.rhiQuadRenderer()->prepareQuad(rhiCtx, maybeRub: nullptr); |
1803 | renderer.rhiQuadRenderer()->recordRenderQuadPass(rhiCtx, ps: &ps, srb, rt: rhiAoTexture.rt, flags: {}); |
1804 | } |
1805 | |
1806 | bool RenderHelpers::rhiPrepareScreenTexture(QSSGRhiContext *rhiCtx, const QSize &size, bool wantsMips, QSSGRhiRenderableTexture *renderableTex) |
1807 | { |
1808 | QRhi *rhi = rhiCtx->rhi(); |
1809 | bool needsBuild = false; |
1810 | QRhiTexture::Flags flags = QRhiTexture::RenderTarget; |
1811 | if (wantsMips) |
1812 | flags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips; |
1813 | |
1814 | if (!renderableTex->texture) { |
1815 | // always non-msaa, even if multisampling is used in the main pass |
1816 | if (rhiCtx->mainPassViewCount() <= 1) |
1817 | renderableTex->texture = rhi->newTexture(format: QRhiTexture::RGBA8, pixelSize: size, sampleCount: 1, flags); |
1818 | else |
1819 | renderableTex->texture = rhi->newTextureArray(format: QRhiTexture::RGBA8, arraySize: rhiCtx->mainPassViewCount(), pixelSize: size, sampleCount: 1, flags); |
1820 | needsBuild = true; |
1821 | } else if (renderableTex->texture->pixelSize() != size) { |
1822 | renderableTex->texture->setPixelSize(size); |
1823 | needsBuild = true; |
1824 | } |
1825 | |
1826 | if (!renderableTex->depthStencil && !renderableTex->depthTexture) { |
1827 | if (rhiCtx->mainPassViewCount() <= 1) |
1828 | renderableTex->depthStencil = rhi->newRenderBuffer(type: QRhiRenderBuffer::DepthStencil, pixelSize: size); |
1829 | else |
1830 | renderableTex->depthTexture = rhi->newTextureArray(format: QRhiTexture::D24S8, arraySize: rhiCtx->mainPassViewCount(), pixelSize: size, sampleCount: 1, flags: QRhiTexture::RenderTarget); |
1831 | needsBuild = true; |
1832 | } else { |
1833 | if (renderableTex->depthStencil && renderableTex->depthStencil->pixelSize() != size) { |
1834 | renderableTex->depthStencil->setPixelSize(size); |
1835 | needsBuild = true; |
1836 | } else if (renderableTex->depthTexture && renderableTex->depthTexture->pixelSize() != size) { |
1837 | renderableTex->depthTexture->setPixelSize(size); |
1838 | needsBuild = true; |
1839 | } |
1840 | } |
1841 | |
1842 | if (needsBuild) { |
1843 | if (!renderableTex->texture->create()) { |
1844 | qWarning(msg: "Failed to build screen texture (size %dx%d)", size.width(), size.height()); |
1845 | renderableTex->reset(); |
1846 | return false; |
1847 | } |
1848 | if (renderableTex->depthStencil && !renderableTex->depthStencil->create()) { |
1849 | qWarning(msg: "Failed to build depth-stencil buffer for screen texture (size %dx%d)", |
1850 | size.width(), size.height()); |
1851 | renderableTex->reset(); |
1852 | return false; |
1853 | } else if (renderableTex->depthTexture && !renderableTex->depthTexture->create()) { |
1854 | qWarning(msg: "Failed to build depth-stencil texture array (multiview) for screen texture (size %dx%d)", |
1855 | size.width(), size.height()); |
1856 | renderableTex->reset(); |
1857 | return false; |
1858 | } |
1859 | renderableTex->resetRenderTarget(); |
1860 | QRhiTextureRenderTargetDescription desc; |
1861 | QRhiColorAttachment colorAttachment(renderableTex->texture); |
1862 | colorAttachment.setMultiViewCount(rhiCtx->mainPassViewCount()); |
1863 | desc.setColorAttachments({ colorAttachment }); |
1864 | if (renderableTex->depthStencil) |
1865 | desc.setDepthStencilBuffer(renderableTex->depthStencil); |
1866 | else if (renderableTex->depthTexture) |
1867 | desc.setDepthTexture(renderableTex->depthTexture); |
1868 | renderableTex->rt = rhi->newTextureRenderTarget(desc); |
1869 | renderableTex->rt->setName(QByteArrayLiteral("Screen texture")); |
1870 | renderableTex->rpDesc = renderableTex->rt->newCompatibleRenderPassDescriptor(); |
1871 | renderableTex->rt->setRenderPassDescriptor(renderableTex->rpDesc); |
1872 | if (!renderableTex->rt->create()) { |
1873 | qWarning(msg: "Failed to build render target for screen texture"); |
1874 | renderableTex->reset(); |
1875 | return false; |
1876 | } |
1877 | } |
1878 | |
1879 | return true; |
1880 | } |
1881 | |
1882 | void RenderHelpers::rhiPrepareGrid(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, QSSGRenderLayer &layer, QSSGRenderCameraList &cameras, QSSGRenderer &renderer) |
1883 | { |
1884 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
1885 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
1886 | cb->debugMarkBegin(QByteArrayLiteral("Quick3D prepare grid")); |
1887 | |
1888 | QSSGRhiShaderResourceBindingList bindings; |
1889 | |
1890 | int uniformBinding = 0; |
1891 | const int ubufSize = cameras.count() >= 2 ? 276 : 148; |
1892 | |
1893 | QSSGRhiDrawCallData &dcd(rhiCtxD->drawCallData(key: { .cid: passKey, .model: nullptr, .entry: nullptr, .entryIdx: 0 })); // Change to Grid? |
1894 | |
1895 | QRhi *rhi = rhiCtx->rhi(); |
1896 | if (!dcd.ubuf) { |
1897 | dcd.ubuf = rhi->newBuffer(type: QRhiBuffer::Dynamic, usage: QRhiBuffer::UniformBuffer, size: ubufSize); |
1898 | dcd.ubuf->create(); |
1899 | } |
1900 | |
1901 | // Param |
1902 | const float nearF = cameras[0]->clipNear; |
1903 | const float farF = cameras[0]->clipFar; |
1904 | const float scale = layer.gridScale; |
1905 | const quint32 gridFlags = layer.gridFlags; |
1906 | |
1907 | const float yFactor = rhi->isYUpInNDC() ? 1.0f : -1.0f; |
1908 | |
1909 | quint32 ubufOffset = 0; |
1910 | char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
1911 | |
1912 | for (qsizetype viewIdx = 0; viewIdx < cameras.count(); ++viewIdx) { |
1913 | QMatrix4x4 viewProj(Qt::Uninitialized); |
1914 | cameras[viewIdx]->calculateViewProjectionMatrix(outMatrix&: viewProj); |
1915 | QMatrix4x4 invViewProj = viewProj.inverted(); |
1916 | quint32 viewDataOffset = ubufOffset; |
1917 | memcpy(dest: ubufData + viewDataOffset + viewIdx * 64, src: viewProj.constData(), n: 64); |
1918 | viewDataOffset += 64 * cameras.count(); |
1919 | memcpy(dest: ubufData + viewDataOffset + viewIdx * 64, src: invViewProj.constData(), n: 64); |
1920 | } |
1921 | ubufOffset += (64 + 64) * cameras.count(); |
1922 | |
1923 | memcpy(dest: ubufData + ubufOffset, src: &nearF, n: 4); |
1924 | ubufOffset += 4; |
1925 | memcpy(dest: ubufData + ubufOffset, src: &farF, n: 4); |
1926 | ubufOffset += 4; |
1927 | memcpy(dest: ubufData + ubufOffset, src: &scale, n: 4); |
1928 | ubufOffset += 4; |
1929 | memcpy(dest: ubufData + ubufOffset, src: &yFactor, n: 4); |
1930 | ubufOffset += 4; |
1931 | memcpy(dest: ubufData + ubufOffset, src: &gridFlags, n: 4); |
1932 | |
1933 | dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
1934 | |
1935 | bindings.addUniformBuffer(binding: uniformBinding, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf); |
1936 | |
1937 | layer.gridSrb = rhiCtxD->srb(bindings); |
1938 | renderer.rhiQuadRenderer()->prepareQuad(rhiCtx, maybeRub: nullptr); |
1939 | |
1940 | cb->debugMarkEnd(); |
1941 | } |
1942 | |
1943 | static void rhiPrepareSkyBox_helper(QSSGRhiContext *rhiCtx, |
1944 | QSSGPassKey passKey, |
1945 | QSSGRenderLayer &layer, |
1946 | QSSGRenderCameraList &cameras, |
1947 | QSSGRenderer &renderer, |
1948 | QSSGReflectionMapEntry *entry = nullptr, |
1949 | QSSGRenderTextureCubeFace cubeFace = QSSGRenderTextureCubeFaceNone) |
1950 | { |
1951 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
1952 | const bool cubeMapMode = layer.background == QSSGRenderLayer::Background::SkyBoxCubeMap; |
1953 | const QSSGRenderImageTexture lightProbeTexture = |
1954 | cubeMapMode ? renderer.contextInterface()->bufferManager()->loadRenderImage(image: layer.skyBoxCubeMap, inMipMode: QSSGBufferManager::MipModeDisable) |
1955 | : renderer.contextInterface()->bufferManager()->loadRenderImage(image: layer.lightProbe, inMipMode: QSSGBufferManager::MipModeBsdf); |
1956 | const bool hasValidTexture = lightProbeTexture.m_texture != nullptr; |
1957 | if (hasValidTexture) { |
1958 | if (cubeFace == QSSGRenderTextureCubeFaceNone) |
1959 | layer.skyBoxIsRgbe8 = lightProbeTexture.m_flags.isRgbe8(); |
1960 | |
1961 | QSSGRhiShaderResourceBindingList bindings; |
1962 | |
1963 | QRhiSampler *sampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Linear, |
1964 | .magFilter: QRhiSampler::Linear, |
1965 | .mipmap: cubeMapMode ? QRhiSampler::None : QRhiSampler::Linear, // cube map doesn't have mipmaps |
1966 | .hTiling: QRhiSampler::Repeat, |
1967 | .vTiling: QRhiSampler::ClampToEdge, |
1968 | .zTiling: QRhiSampler::Repeat }); |
1969 | int samplerBinding = 1; //the shader code is hand-written, so we don't need to look that up |
1970 | const quint32 ubufSize = cameras.count() >= 2 ? 416 : 240; // same ubuf layout for both skybox and skyboxcube |
1971 | bindings.addTexture(binding: samplerBinding, |
1972 | stage: QRhiShaderResourceBinding::FragmentStage, |
1973 | tex: lightProbeTexture.m_texture, sampler); |
1974 | |
1975 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face: cubeFace); |
1976 | const quintptr entryIdx = quintptr(cubeFace != QSSGRenderTextureCubeFaceNone) * cubeFaceIdx; |
1977 | QSSGRhiDrawCallData &dcd = rhiCtxD->drawCallData(key: { .cid: passKey, .model: nullptr, .entry: entry, .entryIdx: entryIdx }); |
1978 | |
1979 | QRhi *rhi = rhiCtx->rhi(); |
1980 | if (!dcd.ubuf) { |
1981 | dcd.ubuf = rhi->newBuffer(type: QRhiBuffer::Dynamic, usage: QRhiBuffer::UniformBuffer, size: ubufSize); |
1982 | dcd.ubuf->create(); |
1983 | } |
1984 | |
1985 | float adjustY = rhi->isYUpInNDC() ? 1.0f : -1.0f; |
1986 | const float exposure = layer.lightProbeSettings.probeExposure; |
1987 | // orientation |
1988 | const QMatrix3x3 &rotationMatrix(layer.lightProbeSettings.probeOrientation); |
1989 | const float blurAmount = layer.skyboxBlurAmount; |
1990 | const float maxMipLevel = float(lightProbeTexture.m_mipmapCount - 2); |
1991 | |
1992 | const QVector4D skyboxProperties = { |
1993 | adjustY, |
1994 | exposure, |
1995 | blurAmount, |
1996 | maxMipLevel |
1997 | }; |
1998 | |
1999 | char *ubufData = dcd.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
2000 | quint32 ubufOffset = 0; |
2001 | // skyboxProperties |
2002 | memcpy(dest: ubufData + ubufOffset, src: &skyboxProperties, n: 16); |
2003 | ubufOffset += 16; |
2004 | // orientation |
2005 | memcpy(dest: ubufData + ubufOffset, src: rotationMatrix.constData(), n: 12); |
2006 | ubufOffset += 16; |
2007 | memcpy(dest: ubufData + ubufOffset, src: (char *)rotationMatrix.constData() + 12, n: 12); |
2008 | ubufOffset += 16; |
2009 | memcpy(dest: ubufData + ubufOffset, src: (char *)rotationMatrix.constData() + 24, n: 12); |
2010 | ubufOffset += 16; |
2011 | |
2012 | for (qsizetype viewIdx = 0; viewIdx < cameras.count(); ++viewIdx) { |
2013 | const QMatrix4x4 &inverseProjection = cameras[viewIdx]->projection.inverted(); |
2014 | const QMatrix4x4 &viewMatrix = cameras[viewIdx]->globalTransform; |
2015 | QMatrix4x4 viewProjection(Qt::Uninitialized); // For cube mode |
2016 | cameras[viewIdx]->calculateViewProjectionWithoutTranslation(near: 0.1f, far: 5.0f, outMatrix&: viewProjection); |
2017 | |
2018 | quint32 viewDataOffset = ubufOffset; |
2019 | memcpy(dest: ubufData + viewDataOffset + viewIdx * 64, src: viewProjection.constData(), n: 64); |
2020 | viewDataOffset += cameras.count() * 64; |
2021 | memcpy(dest: ubufData + viewDataOffset + viewIdx * 64, src: inverseProjection.constData(), n: 64); |
2022 | viewDataOffset += cameras.count() * 64; |
2023 | memcpy(dest: ubufData + viewDataOffset + viewIdx * 48, src: viewMatrix.constData(), n: 48); |
2024 | } |
2025 | dcd.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
2026 | |
2027 | bindings.addUniformBuffer(binding: 0, stage: RENDERER_VISIBILITY_ALL, buf: dcd.ubuf); |
2028 | |
2029 | if (cubeFace != QSSGRenderTextureCubeFaceNone) { |
2030 | const auto cubeFaceIdx = QSSGBaseTypeHelpers::indexOfCubeFace(face: cubeFace); |
2031 | entry->m_skyBoxSrbs[cubeFaceIdx] = rhiCtxD->srb(bindings); |
2032 | } else { |
2033 | layer.skyBoxSrb = rhiCtxD->srb(bindings); |
2034 | } |
2035 | |
2036 | if (cubeMapMode) |
2037 | renderer.rhiCubeRenderer()->prepareCube(rhiCtx, maybeRub: nullptr); |
2038 | else |
2039 | renderer.rhiQuadRenderer()->prepareQuad(rhiCtx, maybeRub: nullptr); |
2040 | } |
2041 | } |
2042 | |
2043 | void RenderHelpers::rhiPrepareSkyBox(QSSGRhiContext *rhiCtx, |
2044 | QSSGPassKey passKey, |
2045 | QSSGRenderLayer &layer, |
2046 | QSSGRenderCameraList &cameras, |
2047 | QSSGRenderer &renderer) |
2048 | { |
2049 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
2050 | cb->debugMarkBegin(QByteArrayLiteral("Quick3D prepare skybox")); |
2051 | |
2052 | rhiPrepareSkyBox_helper(rhiCtx, passKey, layer, cameras, renderer); |
2053 | |
2054 | cb->debugMarkEnd(); |
2055 | } |
2056 | |
2057 | void RenderHelpers::rhiPrepareSkyBoxForReflectionMap(QSSGRhiContext *rhiCtx, |
2058 | QSSGPassKey passKey, |
2059 | QSSGRenderLayer &layer, |
2060 | QSSGRenderCamera &inCamera, |
2061 | QSSGRenderer &renderer, |
2062 | QSSGReflectionMapEntry *entry, |
2063 | QSSGRenderTextureCubeFace cubeFace) |
2064 | { |
2065 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
2066 | cb->debugMarkBegin(QByteArrayLiteral("Quick3D prepare skybox for reflection cube map")); |
2067 | |
2068 | QSSGRenderCameraList cameras({ &inCamera }); |
2069 | rhiPrepareSkyBox_helper(rhiCtx, passKey, layer, cameras, renderer, entry, cubeFace); |
2070 | |
2071 | cb->debugMarkEnd(); |
2072 | } |
2073 | |
2074 | bool RenderHelpers::rhiPrepareDepthPass(QSSGRhiContext *rhiCtx, |
2075 | QSSGPassKey passKey, |
2076 | const QSSGRhiGraphicsPipelineState &basePipelineState, |
2077 | QRhiRenderPassDescriptor *rpDesc, |
2078 | QSSGLayerRenderData &inData, |
2079 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
2080 | const QSSGRenderableObjectList &sortedTransparentObjects, |
2081 | int samples, |
2082 | int viewCount) |
2083 | { |
2084 | static const auto rhiPrepareDepthPassForObject = [](QSSGRhiContext *rhiCtx, |
2085 | QSSGPassKey passKey, |
2086 | QSSGLayerRenderData &inData, |
2087 | QSSGRenderableObject *obj, |
2088 | QRhiRenderPassDescriptor *rpDesc, |
2089 | QSSGRhiGraphicsPipelineState *ps) { |
2090 | QSSGRhiShaderPipelinePtr shaderPipeline; |
2091 | QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(q: rhiCtx); |
2092 | |
2093 | const bool isOpaqueDepthPrePass = obj->depthWriteMode == QSSGDepthDrawMode::OpaquePrePass; |
2094 | QSSGShaderFeatures featureSet; |
2095 | featureSet.set(feature: QSSGShaderFeatures::Feature::DepthPass, val: true); |
2096 | if (isOpaqueDepthPrePass) |
2097 | featureSet.set(feature: QSSGShaderFeatures::Feature::OpaqueDepthPrePass, val: true); |
2098 | |
2099 | QSSGRhiDrawCallData *dcd = nullptr; |
2100 | if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
2101 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(*obj)); |
2102 | const void *modelNode = &subsetRenderable.modelContext.model; |
2103 | dcd = &rhiCtxD->drawCallData(key: { .cid: passKey, .model: modelNode, .entry: &subsetRenderable.material, .entryIdx: 0 }); |
2104 | } |
2105 | |
2106 | if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset) { |
2107 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(*obj)); |
2108 | const auto &material = static_cast<const QSSGRenderDefaultMaterial &>(subsetRenderable.getMaterial()); |
2109 | ps->cullMode = QSSGRhiHelpers::toCullMode(cullFaceMode: material.cullMode); |
2110 | |
2111 | shaderPipeline = shadersForDefaultMaterial(ps, subsetRenderable, featureSet); |
2112 | if (shaderPipeline) { |
2113 | shaderPipeline->ensureCombinedUniformBuffer(ubuf: &dcd->ubuf); |
2114 | char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
2115 | updateUniformsForDefaultMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, subsetRenderable, cameras: inData.renderedCameras, depthAdjust: nullptr, alteredModelViewProjection: nullptr); |
2116 | dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
2117 | } else { |
2118 | return false; |
2119 | } |
2120 | } else if (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
2121 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(*obj)); |
2122 | |
2123 | const auto &customMaterial = static_cast<const QSSGRenderCustomMaterial &>(subsetRenderable.getMaterial()); |
2124 | |
2125 | ps->cullMode = QSSGRhiHelpers::toCullMode(cullFaceMode: customMaterial.m_cullMode); |
2126 | |
2127 | QSSGCustomMaterialSystem &customMaterialSystem(*subsetRenderable.renderer->contextInterface()->customMaterialSystem().get()); |
2128 | shaderPipeline = customMaterialSystem.shadersForCustomMaterial(ps, material: customMaterial, renderable&: subsetRenderable, defaultMaterialShaderKeyProperties: inData.getDefaultMaterialPropertyTable(), featureSet); |
2129 | |
2130 | if (shaderPipeline) { |
2131 | shaderPipeline->ensureCombinedUniformBuffer(ubuf: &dcd->ubuf); |
2132 | char *ubufData = dcd->ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
2133 | customMaterialSystem.updateUniformsForCustomMaterial(shaderPipeline&: *shaderPipeline, rhiCtx, inData, ubufData, ps, material: customMaterial, renderable&: subsetRenderable, |
2134 | cameras: inData.renderedCameras, depthAdjust: nullptr, alteredModelViewProjection: nullptr); |
2135 | dcd->ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
2136 | } else { |
2137 | return false; |
2138 | } |
2139 | } |
2140 | |
2141 | // the rest is common, only relying on QSSGSubsetRenderableBase, not the subclasses |
2142 | if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
2143 | QSSGSubsetRenderable &subsetRenderable(static_cast<QSSGSubsetRenderable &>(*obj)); |
2144 | auto &ia = QSSGRhiInputAssemblerStatePrivate::get(ps&: *ps); |
2145 | ia = subsetRenderable.subset.rhi.ia; |
2146 | |
2147 | const QSSGRenderCameraDataList &cameraDatas(*inData.renderedCameraData); |
2148 | int instanceBufferBinding = setupInstancing(renderable: &subsetRenderable, ps, rhiCtx, cameraDirection: cameraDatas[0].direction, cameraPosition: cameraDatas[0].position); |
2149 | QSSGRhiHelpers::bakeVertexInputLocations(ia: &ia, shaders: *shaderPipeline, instanceBufferBinding); |
2150 | |
2151 | QSSGRhiShaderResourceBindingList bindings; |
2152 | bindings.addUniformBuffer(binding: 0, stage: RENDERER_VISIBILITY_ALL, buf: dcd->ubuf); |
2153 | |
2154 | // Depth and SSAO textures, in case a custom material's shader code does something with them. |
2155 | addDepthTextureBindings(rhiCtx, shaderPipeline: shaderPipeline.get(), bindings); |
2156 | |
2157 | if (isOpaqueDepthPrePass) { |
2158 | addOpaqueDepthPrePassBindings(rhiCtx, |
2159 | shaderPipeline: shaderPipeline.get(), |
2160 | renderableImage: subsetRenderable.firstImage, |
2161 | bindings, |
2162 | isCustomMaterialMeshSubset: (obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset)); |
2163 | } |
2164 | |
2165 | // Skinning |
2166 | if (QRhiTexture *boneTexture = inData.getBonemapTexture(modelContext: subsetRenderable.modelContext)) { |
2167 | int binding = shaderPipeline->bindingForTexture(name: "qt_boneTexture"); |
2168 | if (binding >= 0) { |
2169 | QRhiSampler *boneSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
2170 | .magFilter: QRhiSampler::Nearest, |
2171 | .mipmap: QRhiSampler::None, |
2172 | .hTiling: QRhiSampler::ClampToEdge, |
2173 | .vTiling: QRhiSampler::ClampToEdge, |
2174 | .zTiling: QRhiSampler::Repeat |
2175 | }); |
2176 | bindings.addTexture(binding, |
2177 | stage: QRhiShaderResourceBinding::VertexStage, |
2178 | tex: boneTexture, |
2179 | sampler: boneSampler); |
2180 | } |
2181 | } |
2182 | |
2183 | // Morphing |
2184 | auto *targetsTexture = subsetRenderable.subset.rhi.targetsTexture; |
2185 | if (targetsTexture) { |
2186 | int binding = shaderPipeline->bindingForTexture(name: "qt_morphTargetTexture"); |
2187 | if (binding >= 0) { |
2188 | QRhiSampler *targetsSampler = rhiCtx->sampler(samplerDescription: { .minFilter: QRhiSampler::Nearest, |
2189 | .magFilter: QRhiSampler::Nearest, |
2190 | .mipmap: QRhiSampler::None, |
2191 | .hTiling: QRhiSampler::ClampToEdge, |
2192 | .vTiling: QRhiSampler::ClampToEdge, |
2193 | .zTiling: QRhiSampler::ClampToEdge |
2194 | }); |
2195 | bindings.addTexture(binding, stage: QRhiShaderResourceBinding::VertexStage, tex: subsetRenderable.subset.rhi.targetsTexture, sampler: targetsSampler); |
2196 | } |
2197 | } |
2198 | |
2199 | QRhiShaderResourceBindings *srb = rhiCtxD->srb(bindings); |
2200 | |
2201 | subsetRenderable.rhiRenderData.depthPrePass.pipeline = rhiCtxD->pipeline(ps: *ps, |
2202 | rpDesc, |
2203 | srb); |
2204 | subsetRenderable.rhiRenderData.depthPrePass.srb = srb; |
2205 | } |
2206 | |
2207 | return true; |
2208 | }; |
2209 | |
2210 | // Phase 1 (prepare) for the Z prepass or the depth texture generation. |
2211 | // These renders opaque (Z prepass), or opaque and transparent (depth |
2212 | // texture), objects with depth test/write enabled, and color write |
2213 | // disabled, using a very simple set of shaders. |
2214 | |
2215 | QSSGRhiGraphicsPipelineState ps = basePipelineState; // viewport and others are filled out already |
2216 | // We took a copy of the pipeline state since we do not want to conflict |
2217 | // with what rhiPrepare() collects for its own use. So here just change |
2218 | // whatever we need. |
2219 | |
2220 | ps.samples = samples; |
2221 | ps.viewCount = viewCount; |
2222 | ps.flags |= { QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled, QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled }; |
2223 | ps.targetBlend.colorWrite = {}; |
2224 | |
2225 | for (const QSSGRenderableObjectHandle &handle : sortedOpaqueObjects) { |
2226 | if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps)) |
2227 | return false; |
2228 | } |
2229 | |
2230 | for (const QSSGRenderableObjectHandle &handle : sortedTransparentObjects) { |
2231 | if (!rhiPrepareDepthPassForObject(rhiCtx, passKey, inData, handle.obj, rpDesc, &ps)) |
2232 | return false; |
2233 | } |
2234 | |
2235 | return true; |
2236 | } |
2237 | |
2238 | void RenderHelpers::rhiRenderDepthPass(QSSGRhiContext *rhiCtx, |
2239 | const QSSGRhiGraphicsPipelineState &pipelineState, |
2240 | const QSSGRenderableObjectList &sortedOpaqueObjects, |
2241 | const QSSGRenderableObjectList &sortedTransparentObjects, |
2242 | bool *needsSetViewport) |
2243 | { |
2244 | static const auto rhiRenderDepthPassForImp = [](QSSGRhiContext *rhiCtx, |
2245 | const QSSGRhiGraphicsPipelineState &pipelineState, |
2246 | const QSSGRenderableObjectList &objects, |
2247 | bool *needsSetViewport) { |
2248 | for (const auto &oh : objects) { |
2249 | QSSGRenderableObject *obj = oh.obj; |
2250 | |
2251 | // casts to SubsetRenderableBase so it works for both default and custom materials |
2252 | if (obj->type == QSSGRenderableObject::Type::DefaultMaterialMeshSubset || obj->type == QSSGRenderableObject::Type::CustomMaterialMeshSubset) { |
2253 | QRhiCommandBuffer *cb = rhiCtx->commandBuffer(); |
2254 | QSSGSubsetRenderable *subsetRenderable(static_cast<QSSGSubsetRenderable *>(obj)); |
2255 | |
2256 | QRhiBuffer *vertexBuffer = subsetRenderable->subset.rhi.vertexBuffer->buffer(); |
2257 | QRhiBuffer *indexBuffer = subsetRenderable->subset.rhi.indexBuffer |
2258 | ? subsetRenderable->subset.rhi.indexBuffer->buffer() |
2259 | : nullptr; |
2260 | |
2261 | QRhiGraphicsPipeline *ps = subsetRenderable->rhiRenderData.depthPrePass.pipeline; |
2262 | if (!ps) |
2263 | return; |
2264 | |
2265 | QRhiShaderResourceBindings *srb = subsetRenderable->rhiRenderData.depthPrePass.srb; |
2266 | if (!srb) |
2267 | return; |
2268 | |
2269 | Q_QUICK3D_PROFILE_START(QQuick3DProfiler::Quick3DRenderCall); |
2270 | cb->setGraphicsPipeline(ps); |
2271 | cb->setShaderResources(srb); |
2272 | |
2273 | if (*needsSetViewport) { |
2274 | cb->setViewport(pipelineState.viewport); |
2275 | *needsSetViewport = false; |
2276 | } |
2277 | |
2278 | QRhiCommandBuffer::VertexInput vertexBuffers[2]; |
2279 | int vertexBufferCount = 1; |
2280 | vertexBuffers[0] = QRhiCommandBuffer::VertexInput(vertexBuffer, 0); |
2281 | quint32 instances = 1; |
2282 | if (subsetRenderable->modelContext.model.instancing()) { |
2283 | instances = subsetRenderable->modelContext.model.instanceCount(); |
2284 | vertexBuffers[1] = QRhiCommandBuffer::VertexInput(subsetRenderable->instanceBuffer, 0); |
2285 | vertexBufferCount = 2; |
2286 | } |
2287 | |
2288 | if (indexBuffer) { |
2289 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers, indexBuf: indexBuffer, indexOffset: 0, indexFormat: subsetRenderable->subset.rhi.indexBuffer->indexFormat()); |
2290 | cb->drawIndexed(indexCount: subsetRenderable->subset.count, instanceCount: instances, firstIndex: subsetRenderable->subset.offset); |
2291 | QSSGRHICTX_STAT(rhiCtx, drawIndexed(subsetRenderable->subset.count, instances)); |
2292 | } else { |
2293 | cb->setVertexInput(startBinding: 0, bindingCount: vertexBufferCount, bindings: vertexBuffers); |
2294 | cb->draw(vertexCount: subsetRenderable->subset.count, instanceCount: instances, firstVertex: subsetRenderable->subset.offset); |
2295 | QSSGRHICTX_STAT(rhiCtx, draw(subsetRenderable->subset.count, instances)); |
2296 | } |
2297 | Q_QUICK3D_PROFILE_END_WITH_IDS(QQuick3DProfiler::Quick3DRenderCall, (subsetRenderable->subset.count | quint64(instances) << 32), |
2298 | QVector<int>({subsetRenderable->modelContext.model.profilingId, |
2299 | subsetRenderable->material.profilingId})); |
2300 | } |
2301 | } |
2302 | }; |
2303 | |
2304 | rhiRenderDepthPassForImp(rhiCtx, pipelineState, sortedOpaqueObjects, needsSetViewport); |
2305 | rhiRenderDepthPassForImp(rhiCtx, pipelineState, sortedTransparentObjects, needsSetViewport); |
2306 | } |
2307 | |
2308 | bool RenderHelpers::rhiPrepareDepthTexture(QSSGRhiContext *rhiCtx, const QSize &size, QSSGRhiRenderableTexture *renderableTex) |
2309 | { |
2310 | QRhi *rhi = rhiCtx->rhi(); |
2311 | bool needsBuild = false; |
2312 | |
2313 | if (!renderableTex->texture) { |
2314 | QRhiTexture::Format format = QRhiTexture::D32F; |
2315 | if (!rhi->isTextureFormatSupported(format)) |
2316 | format = QRhiTexture::D16; |
2317 | if (!rhi->isTextureFormatSupported(format)) |
2318 | qWarning(msg: "Depth texture not supported"); |
2319 | // the depth texture is always non-msaa, even if multisampling is used in the main pass |
2320 | if (rhiCtx->mainPassViewCount() <= 1) |
2321 | renderableTex->texture = rhiCtx->rhi()->newTexture(format, pixelSize: size, sampleCount: 1, flags: QRhiTexture::RenderTarget); |
2322 | else |
2323 | renderableTex->texture = rhiCtx->rhi()->newTextureArray(format, arraySize: rhiCtx->mainPassViewCount(), pixelSize: size, sampleCount: 1, flags: QRhiTexture::RenderTarget); |
2324 | needsBuild = true; |
2325 | } else if (renderableTex->texture->pixelSize() != size) { |
2326 | renderableTex->texture->setPixelSize(size); |
2327 | needsBuild = true; |
2328 | } |
2329 | |
2330 | if (needsBuild) { |
2331 | if (!renderableTex->texture->create()) { |
2332 | qWarning(msg: "Failed to build depth texture (size %dx%d, format %d)", |
2333 | size.width(), size.height(), int(renderableTex->texture->format())); |
2334 | renderableTex->reset(); |
2335 | return false; |
2336 | } |
2337 | renderableTex->resetRenderTarget(); |
2338 | QRhiTextureRenderTargetDescription rtDesc; |
2339 | rtDesc.setDepthTexture(renderableTex->texture); |
2340 | renderableTex->rt = rhi->newTextureRenderTarget(desc: rtDesc); |
2341 | renderableTex->rt->setName(QByteArrayLiteral("Depth texture")); |
2342 | renderableTex->rpDesc = renderableTex->rt->newCompatibleRenderPassDescriptor(); |
2343 | renderableTex->rt->setRenderPassDescriptor(renderableTex->rpDesc); |
2344 | if (!renderableTex->rt->create()) { |
2345 | qWarning(msg: "Failed to build render target for depth texture"); |
2346 | renderableTex->reset(); |
2347 | return false; |
2348 | } |
2349 | } |
2350 | |
2351 | return true; |
2352 | } |
2353 | |
2354 | QT_END_NAMESPACE |
2355 |
Definitions
- QSSG_PI
- QSSG_HALFPI
- RENDERER_VISIBILITY_ALL
- shadersForDefaultMaterial
- shadersForParticleMaterial
- updateUniformsForDefaultMaterial
- calculateSortedObjectBounds
- computeFrustumBounds
- computeFrustumBoundsWithNearFar
- computeShadowCameraFromFrustum
- setupCascadingCamerasForShadowMap
- setupCubeReflectionCameras
- addOpaqueDepthPrePassBindings
- setupCubeShadowCameras
- setupInstancing
- rhiPrepareResourcesForReflectionMap
- addDepthTextureBindings
- rhiPrepareResourcesForShadowMap
- fillTargetBlend
- rhiPrepareRenderable
- rhiRenderRenderable
- rhiRenderShadowMap
- rhiRenderReflectionMap
- rhiPrepareAoTexture
- rhiRenderAoTexture
- rhiPrepareScreenTexture
- rhiPrepareGrid
- rhiPrepareSkyBox_helper
- rhiPrepareSkyBox
- rhiPrepareSkyBoxForReflectionMap
- rhiPrepareDepthPass
- rhiRenderDepthPass
Start learning QML with our Intro Training
Find out more