1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#include "qssgrendererimplshaders_p.h"
6
7#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrenderlight_p.h>
9#include "../qssgrendercontextcore.h"
10#include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h>
11#include <QtQuick3DRuntimeRender/private/qssgrendershaderlibrarymanager_p.h>
12#include <QtQuick3DRuntimeRender/private/qssgrendershadercodegenerator_p.h>
13#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterialshadergenerator_p.h>
14#include <QtQuick3DRuntimeRender/private/qssgvertexpipelineimpl_p.h>
15
16// this file contains the getXxxxShader implementations suitable for the QRhi-based rendering path
17
18QT_BEGIN_NAMESPACE
19
20QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getBuiltinRhiShader(const QByteArray &name,
21 BuiltinShader &storage,
22 int viewCount)
23{
24 if (storage.shaderPipeline && storage.viewCount != viewCount)
25 storage = {};
26
27 if (!storage.shaderPipeline) {
28 // loadBuiltin must always return a valid QSSGRhiShaderPipeline.
29 // There will just be no stages if loading fails.
30 storage.shaderPipeline = m_shaderCache.loadBuiltinUncached(inKey: name, viewCount);
31 storage.viewCount = viewCount;
32 }
33
34 return storage.shaderPipeline;
35}
36
37void QSSGBuiltInRhiShaderCache::releaseCachedResources()
38{
39 m_cache = {};
40}
41
42QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiCubemapShadowBlurXShader()
43{
44 return getBuiltinRhiShader(QByteArrayLiteral("cubeshadowblurx"), storage&: m_cache.cubemapShadowBlurXRhiShader);
45}
46
47QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiCubemapShadowBlurYShader()
48{
49 return getBuiltinRhiShader(QByteArrayLiteral("cubeshadowblury"), storage&: m_cache.cubemapShadowBlurYRhiShader);
50}
51
52QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiGridShader(int viewCount)
53{
54 return getBuiltinRhiShader(QByteArrayLiteral("grid"), storage&: m_cache.gridShader, viewCount);
55}
56
57QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiOrthographicShadowBlurXShader()
58{
59 return getBuiltinRhiShader(QByteArrayLiteral("orthoshadowblurx"), storage&: m_cache.orthographicShadowBlurXRhiShader);
60}
61
62QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiOrthographicShadowBlurYShader()
63{
64 return getBuiltinRhiShader(QByteArrayLiteral("orthoshadowblury"), storage&: m_cache.orthographicShadowBlurYRhiShader);
65}
66
67QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiSsaoShader(int viewCount)
68{
69 return getBuiltinRhiShader(QByteArrayLiteral("ssao"), storage&: m_cache.ssaoRhiShader, viewCount);
70}
71
72QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiSkyBoxCubeShader(int viewCount)
73{
74 return getBuiltinRhiShader(QByteArrayLiteral("skyboxcube"), storage&: m_cache.skyBoxCubeRhiShader, viewCount);
75}
76
77static inline constexpr size_t getSkyboxIndex(QSSGRenderLayer::TonemapMode tonemapMode, bool isRGBE)
78{
79 switch (tonemapMode) {
80 case QSSGRenderLayer::TonemapMode::None:
81 return 0 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
82 case QSSGRenderLayer::TonemapMode::Linear:
83 return 1 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
84 case QSSGRenderLayer::TonemapMode::Aces:
85 return 2 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
86 case QSSGRenderLayer::TonemapMode::HejlDawson:
87 return 3 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
88 case QSSGRenderLayer::TonemapMode::Filmic:
89 return 4 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
90 case QSSGRenderLayer::TonemapMode::Custom:
91 return 5 + (size_t(isRGBE) * QSSGRenderLayer::TonemapModeCount);
92 }
93
94 Q_UNREACHABLE_RETURN(0);
95}
96
97QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiSkyBoxShader(QSSGRenderLayer::TonemapMode tonemapMode, bool isRGBE, int viewCount)
98{
99 // Skybox shader is special and has multiple possible shaders so we have to do
100 // a bit of manual work here (mapping resolved in getSkyboxIndex()).
101 static constexpr char variant[][23] { "skybox_hdr_none",
102 "skybox_hdr_linear",
103 "skybox_hdr_aces",
104 "skybox_hdr_hejldawson",
105 "skybox_hdr_filmic",
106 "skybox_hdr_custom",
107 "skybox_rgbe_none",
108 "skybox_rgbe_linear",
109 "skybox_rgbe_aces",
110 "skybox_rgbe_hejldawson",
111 "skybox_rgbe_filmic",
112 "skybox_rgbe_custom",
113 };
114
115 const size_t skyboxIndex = getSkyboxIndex(tonemapMode, isRGBE);
116 return getBuiltinRhiShader(name: QByteArray::fromRawData(data: variant[skyboxIndex], size: std::char_traits<char>::length(s: variant[skyboxIndex])), storage&: m_cache.skyBoxRhiShader[skyboxIndex], viewCount);
117}
118
119QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiSupersampleResolveShader(int viewCount)
120{
121 return getBuiltinRhiShader(QByteArrayLiteral("ssaaresolve"), storage&: m_cache.supersampleResolveRhiShader, viewCount);
122}
123
124QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiProgressiveAAShader()
125{
126 return getBuiltinRhiShader(QByteArrayLiteral("progressiveaa"), storage&: m_cache.progressiveAARhiShader);
127}
128
129QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiParticleShader(QSSGRenderParticles::FeatureLevel featureLevel, int viewCount)
130{
131 switch (featureLevel) {
132 case QSSGRenderParticles::FeatureLevel::Simple:
133 return getBuiltinRhiShader(QByteArrayLiteral("particlesnolightsimple"), storage&: m_cache.particlesNoLightingSimpleRhiShader, viewCount);
134 break;
135 case QSSGRenderParticles::FeatureLevel::Mapped:
136 return getBuiltinRhiShader(QByteArrayLiteral("particlesnolightmapped"), storage&: m_cache.particlesNoLightingMappedRhiShader, viewCount);
137 break;
138 case QSSGRenderParticles::FeatureLevel::Animated:
139 return getBuiltinRhiShader(QByteArrayLiteral("particlesnolightanimated"), storage&: m_cache.particlesNoLightingAnimatedRhiShader, viewCount);
140 break;
141 case QSSGRenderParticles::FeatureLevel::SimpleVLight:
142 return getBuiltinRhiShader(QByteArrayLiteral("particlesvlightsimple"), storage&: m_cache.particlesVLightingSimpleRhiShader, viewCount);
143 break;
144 case QSSGRenderParticles::FeatureLevel::MappedVLight:
145 return getBuiltinRhiShader(QByteArrayLiteral("particlesvlightmapped"), storage&: m_cache.particlesVLightingMappedRhiShader, viewCount);
146 break;
147 case QSSGRenderParticles::FeatureLevel::AnimatedVLight:
148 return getBuiltinRhiShader(QByteArrayLiteral("particlesvlightanimated"), storage&: m_cache.particlesVLightingAnimatedRhiShader, viewCount);
149 break;
150 case QSSGRenderParticles::FeatureLevel::Line:
151 return getBuiltinRhiShader(QByteArrayLiteral("lineparticles"), storage&: m_cache.lineParticlesRhiShader, viewCount);
152 break;
153 case QSSGRenderParticles::FeatureLevel::LineMapped:
154 return getBuiltinRhiShader(QByteArrayLiteral("lineparticlesmapped"), storage&: m_cache.lineParticlesMappedRhiShader, viewCount);
155 break;
156 case QSSGRenderParticles::FeatureLevel::LineAnimated:
157 return getBuiltinRhiShader(QByteArrayLiteral("lineparticlesanimated"), storage&: m_cache.lineParticlesAnimatedRhiShader, viewCount);
158 break;
159 case QSSGRenderParticles::FeatureLevel::LineVLight:
160 return getBuiltinRhiShader(QByteArrayLiteral("lineparticlesvlightsimple"), storage&: m_cache.lineParticlesVLightRhiShader, viewCount);
161 break;
162 case QSSGRenderParticles::FeatureLevel::LineMappedVLight:
163 return getBuiltinRhiShader(QByteArrayLiteral("lineparticlesvlightmapped"), storage&: m_cache.lineParticlesMappedVLightRhiShader, viewCount);
164 break;
165 case QSSGRenderParticles::FeatureLevel::LineAnimatedVLight:
166 return getBuiltinRhiShader(QByteArrayLiteral("lineparticlesvlightanimated"), storage&: m_cache.lineParticlesAnimatedVLightRhiShader, viewCount);
167 break;
168 }
169 return getBuiltinRhiShader(QByteArrayLiteral("particlesnolightanimated"), storage&: m_cache.particlesNoLightingAnimatedRhiShader, viewCount);
170}
171
172QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiSimpleQuadShader(int viewCount)
173{
174 return getBuiltinRhiShader(QByteArrayLiteral("simplequad"), storage&: m_cache.simpleQuadRhiShader, viewCount);
175}
176
177QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiLightmapUVRasterizationShader(LightmapUVRasterizationShaderMode mode)
178{
179 switch (mode) {
180 case LightmapUVRasterizationShaderMode::Uv:
181 return getBuiltinRhiShader(QByteArrayLiteral("lightmapuvraster_uv"), storage&: m_cache.lightmapUVRasterShader_uv);
182 case LightmapUVRasterizationShaderMode::UvTangent:
183 return getBuiltinRhiShader(QByteArrayLiteral("lightmapuvraster_uv_tangent"), storage&: m_cache.lightmapUVRasterShader_uv_tangent);
184 case LightmapUVRasterizationShaderMode::Default:
185 return getBuiltinRhiShader(QByteArrayLiteral("lightmapuvraster"), storage&: m_cache.lightmapUVRasterShader);
186 }
187
188 Q_UNREACHABLE_RETURN(getBuiltinRhiShader(QByteArrayLiteral("lightmapuvraster"), m_cache.lightmapUVRasterShader));
189}
190
191QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiLightmapDilateShader()
192{
193 return getBuiltinRhiShader(QByteArrayLiteral("lightmapdilate"), storage&: m_cache.lightmapDilateShader);
194}
195
196QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiDebugObjectShader()
197{
198 return getBuiltinRhiShader(QByteArrayLiteral("debugobject"), storage&: m_cache.debugObjectShader);
199}
200
201QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiReflectionprobePreFilterShader()
202{
203 return getBuiltinRhiShader(QByteArrayLiteral("reflectionprobeprefilter"), storage&: m_cache.reflectionprobePreFilterShader);
204}
205
206QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhienvironmentmapPreFilterShader(bool isRGBE)
207{
208 static constexpr char variant[][29] { "environmentmapprefilter", "environmentmapprefilter_rgbe" };
209 const quint8 idx = quint8(isRGBE);
210 return getBuiltinRhiShader(name: QByteArray::fromRawData(data: variant[idx], size: std::char_traits<char>::length(s: variant[idx])), storage&: m_cache.environmentmapPreFilterShader[idx]);
211}
212
213QSSGRhiShaderPipelinePtr QSSGBuiltInRhiShaderCache::getRhiEnvironmentmapShader()
214{
215 return getBuiltinRhiShader(QByteArrayLiteral("environmentmap"), storage&: m_cache.environmentmapShader);
216}
217
218QT_END_NAMESPACE
219

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtquick3d/src/runtimerender/rendererimpl/qssgrendererimplshaders_rhi.cpp