Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgiblbaker.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7#include <QFile>
8#include <QFileInfo>
9#include <QScopeGuard>
10
11#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
12#include <QtQuick3DRuntimeRender/private/qssgrenderloadedtexture_p.h>
13#include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h>
14
15#if QT_CONFIG(opengl)
16#include <QOffscreenSurface>
17#include <QOpenGLContext>
18#endif
19
21
22#define GL_FLOAT 0x1406
23#define GL_HALF_FLOAT 0x140B
24#define GL_UNSIGNED_BYTE 0x1401
25#define GL_RGBA 0x1908
26#define GL_RGBA8 0x8058
27#define GL_RGBA16F 0x881A
28#define GL_RGBA32F 0x8814
29
31
32const QStringList QSSGIblBaker::inputExtensions() const
33{
34 return { QStringLiteral("hdr"), QStringLiteral("exr")};
35}
36
37const QString QSSGIblBaker::outputExtension() const
38{
39 return QStringLiteral(".ktx");
40}
41
42namespace {
43void writeUInt32(QIODevice &device, quint32 value)
44{
45 device.write(reinterpret_cast<char *>(&value), sizeof(qint32));
46}
47
48void appendBinaryVector(QVector<char> &dest, const quint32 src)
49{
50 qsizetype oldsize = dest.size();
51 dest.resize(dest.size() + sizeof(src));
52 memcpy(dest.data() + oldsize, &src, sizeof(src));
53}
54
55void appendBinaryVector(QVector<char> &dest, const std::string &src)
56{
57 qsizetype oldsize = dest.size();
58 dest.resize(dest.size() + src.size() + 1);
59 memcpy(dest.data() + oldsize, src.c_str(), src.size() + 1);
60}
61}
62
63// Vertex data for rendering environment cube map
64static const float cube[] = {
65 -1.0f, -1.0f, -1.0f, // -X side
66 -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
67
68 -1.0f, -1.0f, -1.0f, // -Z side
69 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
70
71 -1.0f, -1.0f, -1.0f, // -Y side
72 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
73
74 -1.0f, 1.0f, -1.0f, // +Y side
75 -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
76
77 1.0f, 1.0f, -1.0f, // +X side
78 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
79
80 -1.0f, 1.0f, 1.0f, // +Z side
81 -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
82
83 0.0f, 1.0f, // -X side
84 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
85
86 1.0f, 1.0f, // -Z side
87 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
88
89 1.0f, 0.0f, // -Y side
90 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
91
92 1.0f, 0.0f, // +Y side
93 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
94
95 1.0f, 0.0f, // +X side
96 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
97
98 0.0f, 0.0f, // +Z side
99 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
100};
101
102QString renderToKTXFileInternal(const char *name, const QString &inPath, const QString &outPath, QRhi::Implementation impl, QRhiInitParams *initParams)
103{
104 qDebug() << "Using RHI backend" << name;
105
106 // Open output file
107 QFile ktxOutputFile(outPath);
108 if (!ktxOutputFile.open(QIODevice::WriteOnly)) {
109 return QStringLiteral("Could not open file: %1").arg(outPath);
110 }
111
112 QScopedPointer<QRhi> rhi(QRhi::create(impl, initParams, QRhi::Flags(), nullptr));
113 if (!rhi)
114 return QStringLiteral("Failed to initialize QRhi");
115
116 qDebug() << rhi->driverInfo();
117
118 QRhiCommandBuffer *cb;
119 rhi->beginOffscreenFrame(&cb);
120
121 const auto rhiContext = std::make_unique<QSSGRhiContext>(rhi.get());
122 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiContext.get());
123 rhiCtxD->setCommandBuffer(cb);
124
125 QScopedPointer<QSSGLoadedTexture> inImage(QSSGLoadedTexture::loadHdrImage(QSSGInputUtil::getStreamForFile(inPath), FORMAT));
126 if (!inImage)
127 return QStringLiteral("Failed to load hdr file");
128
129 auto shaderCache = std::make_unique<QSSGShaderCache>(*rhiContext);
130
131 // The objective of this method is to take the equirectangular texture
132 // provided by inImage and create a cubeMap that contains both pre-filtered
133 // specular environment maps, as well as a irradiance map for diffuse
134 // operations.
135 // To achieve this though we first convert convert the Equirectangular texture
136 // to a cubeMap with genereated mip map levels (no filtering) to make the
137 // process of creating the prefiltered and irradiance maps eaiser. This
138 // intermediate texture as well as the original equirectangular texture are
139 // destroyed after this frame completes, and all further associations with
140 // the source lightProbe texture are instead associated with the final
141 // generated environment map.
142 // The intermediate environment cubemap is used to generate the final
143 // cubemap. This cubemap will generate 6 mip levels for each face
144 // (the remaining faces are unused). This is what the contents of each
145 // face mip level looks like:
146 // 0: Pre-filtered with roughness 0 (basically unfiltered)
147 // 1: Pre-filtered with roughness 0.25
148 // 2: Pre-filtered with roughness 0.5
149 // 3: Pre-filtered with roughness 0.75
150 // 4: Pre-filtered with rougnness 1.0
151 // 5: Irradiance map (ideally at least 16x16)
152 // It would be better if we could use a separate cubemap for irradiance, but
153 // right now there is a 1:1 association between texture sources on the front-
154 // end and backend.
155
156 // Right now minimum face size needs to be 512x512 to be able to have 6 reasonably sized mips
157 const int suggestedSize = qMax(512.f, inImage->height * 0.5f);
158 const QSize environmentMapSize(suggestedSize, suggestedSize);
159 const bool isRGBE = inImage->format.format == QSSGRenderTextureFormat::Format::RGBE8;
160 const int colorSpace = inImage->isSRGB ? 1 : 0; // 0 Linear | 1 sRGB
161
162 // Phase 1: Convert the Equirectangular texture to a Cubemap
163 QRhiTexture *envCubeMap = rhi->newTexture(QRhiTexture::RGBA16F,
164 environmentMapSize,
165 1,
166 QRhiTexture::RenderTarget | QRhiTexture::CubeMap | QRhiTexture::MipMapped
167 | QRhiTexture::UsedWithGenerateMips);
168 if (!envCubeMap->create()) {
169 return QStringLiteral("Failed to create Environment Cube Map");
170 }
171 envCubeMap->deleteLater();
172
173 // Create a renderbuffer the size of a the cubeMap face
174 QRhiRenderBuffer *envMapRenderBuffer = rhi->newRenderBuffer(QRhiRenderBuffer::Color, environmentMapSize);
175 if (!envMapRenderBuffer->create()) {
176 return QStringLiteral("Failed to create Environment Map Render Buffer");
177 }
178 envMapRenderBuffer->deleteLater();
179
180 // Setup the 6 render targets for each cube face
181 QVarLengthArray<QRhiTextureRenderTarget *, 6> renderTargets;
182 QRhiRenderPassDescriptor *renderPassDesc = nullptr;
183 for (int face = 0; face < 6; ++face) {
184 QRhiColorAttachment att(envCubeMap);
185 att.setLayer(face);
186 QRhiTextureRenderTargetDescription rtDesc;
187 rtDesc.setColorAttachments({ att });
188 auto renderTarget = rhi->newTextureRenderTarget(rtDesc);
189 renderTarget->setDescription(rtDesc);
190 if (!renderPassDesc)
191 renderPassDesc = renderTarget->newCompatibleRenderPassDescriptor();
192 renderTarget->setRenderPassDescriptor(renderPassDesc);
193 if (!renderTarget->create()) {
194 return QStringLiteral("Failed to build env map render target");
195 }
196 renderTarget->deleteLater();
197 renderTargets << renderTarget;
198 }
199 renderPassDesc->deleteLater();
200
201 // Setup the sampler for reading the equirectangular loaded texture
202 QSize size(inImage->width, inImage->height);
203 auto *sourceTexture = rhi->newTexture(QRhiTexture::RGBA16F, size, 1);
204 if (!sourceTexture->create()) {
205 return QStringLiteral("Failed to create source env map texture");
206 }
207 sourceTexture->deleteLater();
208
209 // Upload the equirectangular texture
210 QRhiTextureUploadDescription desc;
211 if (inImage->textureFileData.isValid()) {
212 desc = { { 0,
213 0,
214 { inImage->textureFileData.data().constData() + inImage->textureFileData.dataOffset(0),
215 quint32(inImage->textureFileData.dataLength(0)) } } };
216 } else {
217 desc = { { 0, 0, { inImage->data, inImage->dataSizeInBytes } } };
218 }
219 auto *rub = rhi->nextResourceUpdateBatch();
220 rub->uploadTexture(sourceTexture, desc);
221
222 const QSSGRhiSamplerDescription samplerDesc {
223 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
224 };
225 QRhiSampler *sampler = rhiContext->sampler(samplerDesc);
226
227 // Load shader and setup render pipeline
228 const auto &envMapShaderStages = shaderCache->getBuiltInRhiShaders().getRhiEnvironmentmapShader();
229
230 // Vertex Buffer - Just a single cube that will be viewed from inside
231 QRhiBuffer *vertexBuffer = rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
232 vertexBuffer->create();
233 vertexBuffer->deleteLater();
234 rub->uploadStaticBuffer(vertexBuffer, cube);
235
236 // Uniform Buffer - 2x mat4
237 int ubufElementSize = rhi->ubufAligned(128);
238 QRhiBuffer *uBuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, ubufElementSize * 6);
239 uBuf->create();
240 uBuf->deleteLater();
241
242 int ubufEnvMapElementSize = rhi->ubufAligned(4);
243 QRhiBuffer *uBufEnvMap = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, ubufEnvMapElementSize * 6);
244 uBufEnvMap->create();
245 uBufEnvMap->deleteLater();
246
247 // Shader Resource Bindings
248 QRhiShaderResourceBindings *envMapSrb = rhi->newShaderResourceBindings();
249 envMapSrb->setBindings(
250 { QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::VertexStage, uBuf, 128),
251 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(2, QRhiShaderResourceBinding::FragmentStage, uBufEnvMap, ubufEnvMapElementSize),
252 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, sourceTexture, sampler) });
253 envMapSrb->create();
254 envMapSrb->deleteLater();
255
256 // Pipeline
257 QRhiGraphicsPipeline *envMapPipeline = rhi->newGraphicsPipeline();
258 envMapPipeline->setCullMode(QRhiGraphicsPipeline::Front);
259 envMapPipeline->setFrontFace(QRhiGraphicsPipeline::CCW);
260 envMapPipeline->setShaderStages({ *envMapShaderStages->vertexStage(), *envMapShaderStages->fragmentStage() });
261
262 QRhiVertexInputLayout inputLayout;
263 inputLayout.setBindings({ { 3 * sizeof(float) } });
264 inputLayout.setAttributes({ { 0, 0, QRhiVertexInputAttribute::Float3, 0 } });
265
266 envMapPipeline->setVertexInputLayout(inputLayout);
267 envMapPipeline->setShaderResourceBindings(envMapSrb);
268 envMapPipeline->setRenderPassDescriptor(renderPassDesc);
269 if (!envMapPipeline->create()) {
270 return QStringLiteral("Failed to create source env map pipeline state");
271 }
272 envMapPipeline->deleteLater();
273
274 // Do the actual render passes
275 cb->debugMarkBegin("Environment Cubemap Generation");
276 const QRhiCommandBuffer::VertexInput vbufBinding(vertexBuffer, 0);
277
278 // Set the Uniform Data
279 QMatrix4x4 mvp = rhi->clipSpaceCorrMatrix();
280 mvp.perspective(90.0f, 1.0f, 0.1f, 10.0f);
281
282 auto lookAt = [](const QVector3D &eye, const QVector3D &center, const QVector3D &up) {
283 QMatrix4x4 viewMatrix;
284 viewMatrix.lookAt(eye, center, up);
285 return viewMatrix;
286 };
287 QVarLengthArray<QMatrix4x4, 6> views;
288 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(1.0, 0.0, 0.0), QVector3D(0.0f, -1.0f, 0.0f)));
289 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(-1.0, 0.0, 0.0), QVector3D(0.0f, -1.0f, 0.0f)));
290 if (rhi->isYUpInFramebuffer()) {
291 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, 1.0, 0.0), QVector3D(0.0f, 0.0f, 1.0f)));
292 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, -1.0, 0.0), QVector3D(0.0f, 0.0f, -1.0f)));
293 } else {
294 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, -1.0, 0.0), QVector3D(0.0f, 0.0f, -1.0f)));
295 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, 1.0, 0.0), QVector3D(0.0f, 0.0f, 1.0f)));
296 }
297 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, 0.0, 1.0), QVector3D(0.0f, -1.0f, 0.0f)));
298 views.append(lookAt(QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, 0.0, -1.0), QVector3D(0.0f, -1.0f, 0.0f)));
299 for (int face = 0; face < 6; ++face) {
300 rub->updateDynamicBuffer(uBuf, face * ubufElementSize, 64, mvp.constData());
301 rub->updateDynamicBuffer(uBuf, face * ubufElementSize + 64, 64, views[face].constData());
302 rub->updateDynamicBuffer(uBufEnvMap, face * ubufEnvMapElementSize, 4, &colorSpace);
303 }
304 cb->resourceUpdate(rub);
305
306 for (int face = 0; face < 6; ++face) {
307 cb->beginPass(renderTargets[face], QColor(0, 0, 0, 1), { 1.0f, 0 }, nullptr, rhiContext->commonPassFlags());
308
309 // Execute render pass
310 cb->setGraphicsPipeline(envMapPipeline);
311 cb->setVertexInput(0, 1, &vbufBinding);
312 cb->setViewport(QRhiViewport(0, 0, environmentMapSize.width(), environmentMapSize.height()));
313 QVector<QPair<int, quint32>> dynamicOffset = {
314 { 0, quint32(ubufElementSize * face) },
315 { 2, quint32(ubufEnvMapElementSize * face )}
316 };
317 cb->setShaderResources(envMapSrb, 2, dynamicOffset.constData());
318
319 cb->draw(36);
320 cb->endPass();
321 }
322 cb->debugMarkEnd();
323
324 if (!isRGBE) {
325 // Generate mipmaps for envMap
326 rub = rhi->nextResourceUpdateBatch();
327 rub->generateMips(envCubeMap);
328 cb->resourceUpdate(rub);
329 }
330
331 // Phase 2: Generate the pre-filtered environment cubemap
332 cb->debugMarkBegin("Pre-filtered Environment Cubemap Generation");
333 QRhiTexture *preFilteredEnvCubeMap = rhi->newTexture(QRhiTexture::RGBA16F,
334 environmentMapSize,
335 1,
336 QRhiTexture::RenderTarget | QRhiTexture::CubeMap | QRhiTexture::MipMapped);
337 if (!preFilteredEnvCubeMap->create())
338 qWarning("Failed to create Pre-filtered Environment Cube Map");
339 int mipmapCount = rhi->mipLevelsForSize(environmentMapSize);
340 mipmapCount = qMin(mipmapCount, 6); // don't create more than 6 mip levels
341 QMap<int, QSize> mipLevelSizes;
342 QMap<int, QVarLengthArray<QRhiTextureRenderTarget *, 6>> renderTargetsMap;
343 QRhiRenderPassDescriptor *renderPassDescriptorPhase2 = nullptr;
344
345 // Create a renderbuffer for each mip level
346 for (int mipLevel = 0; mipLevel < mipmapCount; ++mipLevel) {
347 const QSize levelSize = QSize(environmentMapSize.width() * std::pow(0.5, mipLevel),
348 environmentMapSize.height() * std::pow(0.5, mipLevel));
349 mipLevelSizes.insert(mipLevel, levelSize);
350 // Setup Render targets (6 * mipmapCount)
351 QVarLengthArray<QRhiTextureRenderTarget *, 6> renderTargets;
352 for (int face = 0; face < 6; ++face) {
353 QRhiColorAttachment att(preFilteredEnvCubeMap);
354 att.setLayer(face);
355 att.setLevel(mipLevel);
356 QRhiTextureRenderTargetDescription rtDesc;
357 rtDesc.setColorAttachments({ att });
358 auto renderTarget = rhi->newTextureRenderTarget(rtDesc);
359 renderTarget->setDescription(rtDesc);
360 if (!renderPassDescriptorPhase2)
361 renderPassDescriptorPhase2 = renderTarget->newCompatibleRenderPassDescriptor();
362 renderTarget->setRenderPassDescriptor(renderPassDescriptorPhase2);
363 if (!renderTarget->create())
364 qWarning("Failed to build prefilter env map render target");
365 renderTarget->deleteLater();
366 renderTargets << renderTarget;
367 }
368 renderTargetsMap.insert(mipLevel, renderTargets);
369 renderPassDescriptorPhase2->deleteLater();
370 }
371
372 // Load the prefilter shader stages
373 const auto &prefilterShaderStages = shaderCache->getBuiltInRhiShaders().getRhienvironmentmapPreFilterShader(isRGBE);
374
375 // Create a new Sampler
376 const QSSGRhiSamplerDescription samplerMipMapDesc {
377 QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::Repeat
378 };
379
380 QRhiSampler *envMapCubeSampler = nullptr;
381 // Only use mipmap interpoliation if not using RGBE
382 if (!isRGBE)
383 envMapCubeSampler = rhiContext->sampler(samplerMipMapDesc);
384 else
385 envMapCubeSampler = sampler;
386
387 // Reuse Vertex Buffer from phase 1
388 // Reuse UniformBuffer from phase 1 (for vertex shader)
389
390 // UniformBuffer
391 // float roughness;
392 // float resolution;
393 // float lodBias;
394 // int sampleCount;
395 // int distribution;
396
397 int ubufPrefilterElementSize = rhi->ubufAligned(20);
398 QRhiBuffer *uBufPrefilter = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, ubufPrefilterElementSize * mipmapCount);
399 uBufPrefilter->create();
400 uBufPrefilter->deleteLater();
401
402 // Shader Resource Bindings
403 QRhiShaderResourceBindings *preFilterSrb = rhi->newShaderResourceBindings();
404 preFilterSrb->setBindings({
405 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::VertexStage, uBuf, 128),
406 QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(2, QRhiShaderResourceBinding::FragmentStage, uBufPrefilter, 20),
407 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, envCubeMap, envMapCubeSampler)
408 });
409 preFilterSrb->create();
410 preFilterSrb->deleteLater();
411
412 // Pipeline
413 QRhiGraphicsPipeline *prefilterPipeline = rhi->newGraphicsPipeline();
414 prefilterPipeline->setCullMode(QRhiGraphicsPipeline::Front);
415 prefilterPipeline->setFrontFace(QRhiGraphicsPipeline::CCW);
416 prefilterPipeline->setDepthOp(QRhiGraphicsPipeline::LessOrEqual);
417 prefilterPipeline->setShaderStages({
418 *prefilterShaderStages->vertexStage(),
419 *prefilterShaderStages->fragmentStage()
420 });
421 // same as phase 1
422 prefilterPipeline->setVertexInputLayout(inputLayout);
423 prefilterPipeline->setShaderResourceBindings(preFilterSrb);
424 prefilterPipeline->setRenderPassDescriptor(renderPassDescriptorPhase2);
425 if (!prefilterPipeline->create())
426 return QStringLiteral("Failed to create pre-filter env map pipeline state");
427 prefilterPipeline->deleteLater();
428
429 // Uniform Data
430 // set the roughness uniform buffer data
431 rub = rhi->nextResourceUpdateBatch();
432 const float resolution = environmentMapSize.width();
433 const float lodBias = 0.0f;
434 const int sampleCount = 1024;
435 for (int mipLevel = 0; mipLevel < mipmapCount; ++mipLevel) {
436 Q_ASSERT(mipmapCount - 2);
437 const float roughness = float(mipLevel) / float(mipmapCount - 2);
438 const int distribution = mipLevel == (mipmapCount - 1) ? 0 : 1; // last mip level is for irradiance
439 rub->updateDynamicBuffer(uBufPrefilter, mipLevel * ubufPrefilterElementSize, 4, &roughness);
440 rub->updateDynamicBuffer(uBufPrefilter, mipLevel * ubufPrefilterElementSize + 4, 4, &resolution);
441 rub->updateDynamicBuffer(uBufPrefilter, mipLevel * ubufPrefilterElementSize + 4 + 4, 4, &lodBias);
442 rub->updateDynamicBuffer(uBufPrefilter, mipLevel * ubufPrefilterElementSize + 4 + 4 + 4, 4, &sampleCount);
443 rub->updateDynamicBuffer(uBufPrefilter, mipLevel * ubufPrefilterElementSize + 4 + 4 + 4 + 4, 4, &distribution);
444 }
445
446 cb->resourceUpdate(rub);
447
448 // Render
449 for (int mipLevel = 0; mipLevel < mipmapCount; ++mipLevel) {
450 for (int face = 0; face < 6; ++face) {
451 cb->beginPass(renderTargetsMap[mipLevel][face], QColor(0, 0, 0, 1), { 1.0f, 0 }, nullptr, rhiContext->commonPassFlags());
452 cb->setGraphicsPipeline(prefilterPipeline);
453 cb->setVertexInput(0, 1, &vbufBinding);
454 cb->setViewport(QRhiViewport(0, 0, mipLevelSizes[mipLevel].width(), mipLevelSizes[mipLevel].height()));
455 QVector<QPair<int, quint32>> dynamicOffsets = {
456 { 0, quint32(ubufElementSize * face) },
457 { 2, quint32(ubufPrefilterElementSize * mipLevel) }
458 };
459 cb->setShaderResources(preFilterSrb, 2, dynamicOffsets.constData());
460 cb->draw(36);
461 cb->endPass();
462 }
463 }
464 cb->debugMarkEnd();
465
466 // Write ktx
467
468 const quint32 numberOfMipmapLevels = renderTargetsMap.size();
469 const quint32 numberOfFaces = 6;
470
471 constexpr size_t KTX_IDENTIFIER_LENGTH = 12;
472 constexpr char ktxIdentifier[KTX_IDENTIFIER_LENGTH] = { '\xAB', 'K', 'T', 'X', ' ', '1',
473 '1', '\xBB', '\r', '\n', '\x1A', '\n' };
474 constexpr quint32 platformEndianIdentifier = 0x04030201;
475 QVector<char> keyValueData;
476
477 // Prepare Key/Value array
478 {
479 // Add a key to the metadata to know it was created by our IBL baker
480 static const char key[] = "QT_IBL_BAKER_VERSION";
481 static const char value[] = "1";
482
483 constexpr size_t keyAndValueByteSize = sizeof(key) + sizeof(value); // NB: 2x null terminator
484 appendBinaryVector(keyValueData, keyAndValueByteSize);
485 appendBinaryVector(keyValueData, key);
486 appendBinaryVector(keyValueData, value);
487
488 // Pad until next multiple of 4
489 const size_t padding = 3 - ((keyAndValueByteSize + 3) % 4); // Pad until next multiple of 4
490 keyValueData.resize(keyValueData.size() + padding);
491 }
492
493 // Header
494
495 // identifier
496 ktxOutputFile.write(ktxIdentifier, KTX_IDENTIFIER_LENGTH);
497
498 // endianness
499 writeUInt32(ktxOutputFile, quint32(platformEndianIdentifier));
500
501 // glType
502 writeUInt32(ktxOutputFile, quint32(GL_HALF_FLOAT));
503
504 // glTypeSize (in bytes per component)
505 writeUInt32(ktxOutputFile, quint32(FORMAT.getSizeofFormat()) / quint32(FORMAT.getNumberOfComponent()));
506
507 // glFormat
508 writeUInt32(ktxOutputFile, quint32(GL_RGBA));
509
510 // glInternalFormat
511 writeUInt32(ktxOutputFile, quint32(GL_RGBA16F));
512
513 // glBaseInternalFormat
514 writeUInt32(ktxOutputFile, quint32(GL_RGBA));
515
516 // pixelWidth
517 writeUInt32(ktxOutputFile, quint32(environmentMapSize.width()));
518
519 // pixelHeight
520 writeUInt32(ktxOutputFile, quint32(environmentMapSize.height()));
521
522 // pixelDepth
523 writeUInt32(ktxOutputFile, quint32(0));
524
525 // numberOfArrayElements
526 writeUInt32(ktxOutputFile, quint32(0));
527
528 // numberOfFaces
529 writeUInt32(ktxOutputFile, quint32(numberOfFaces));
530
531 // numberOfMipLevels
532 writeUInt32(ktxOutputFile, quint32(numberOfMipmapLevels));
533
534 // bytesOfKeyValueData
535 writeUInt32(ktxOutputFile, quint32(keyValueData.size()));
536
537 // Key/Value
538 ktxOutputFile.write(keyValueData.data(), keyValueData.size());
539
540 // Images
541 for (quint32 mipmap_level = 0; mipmap_level < numberOfMipmapLevels; mipmap_level++) {
542 quint32 imageSize = 0;
543 for (size_t face = 0; face < numberOfFaces; face++) {
544 QRhiTextureRenderTarget *renderTarget = renderTargetsMap[mipmap_level][face];
545
546 // Read back texture
547 Q_ASSERT(rhi->isRecordingFrame());
548
549 const auto descr = renderTarget->description();
550 const auto texture = descr.cbeginColorAttachments()->texture();
551
552 QRhiReadbackResult result;
553 QRhiReadbackDescription readbackDesc(texture); // null src == read from swapchain backbuffer
554 readbackDesc.setLayer(int(face));
555 readbackDesc.setLevel(mipmap_level);
556
557 QRhiResourceUpdateBatch *resourceUpdates = rhi->nextResourceUpdateBatch();
558 resourceUpdates->readBackTexture(readbackDesc, &result);
559
560 cb->resourceUpdate(resourceUpdates);
561 rhi->finish(); // make sure the readback has finished, stall the pipeline if needed
562
563 // Write imageSize once size is known
564 if (imageSize == 0) {
565 imageSize = result.data.size();
566 writeUInt32(ktxOutputFile, quint32(imageSize));
567 }
568
569 ktxOutputFile.write(result.data);
570 }
571 }
572
573 ktxOutputFile.close();
574
575 preFilteredEnvCubeMap->deleteLater();
576
577 rhi->endOffscreenFrame();
578 rhi->finish();
579
580 return {};
581}
582
583void adjustToPlatformQuirks(QRhi::Implementation &impl)
584{
585#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
586 // A macOS VM may not have Metal support at all. We have to decide at this
587 // point, it will be too late afterwards, and the only way is to see if
588 // MTLCreateSystemDefaultDevice succeeds.
589 if (impl == QRhi::Metal) {
590 QRhiMetalInitParams rhiParams;
591 QRhi *tempRhi = QRhi::create(impl, &rhiParams, {});
592 if (!tempRhi) {
593 impl = QRhi::OpenGLES2;
594 qDebug("Metal does not seem to be supported. Falling back to OpenGL.");
595 } else {
596 delete tempRhi;
597 }
598 }
599#else
600 Q_UNUSED(impl);
601#endif
602}
603
605{
606 QRhi::Implementation implementation = QRhi::Implementation::Null;
607
608 // check env.vars., fall back to platform-specific defaults when backend is not set
609 const QByteArray rhiBackend = qgetenv("QSG_RHI_BACKEND");
610 if (rhiBackend == QByteArrayLiteral("gl") || rhiBackend == QByteArrayLiteral("gles2")
611 || rhiBackend == QByteArrayLiteral("opengl")) {
612 implementation = QRhi::OpenGLES2;
613 } else if (rhiBackend == QByteArrayLiteral("d3d11") || rhiBackend == QByteArrayLiteral("d3d")) {
614 implementation = QRhi::D3D11;
615 } else if (rhiBackend == QByteArrayLiteral("d3d12")) {
616 implementation = QRhi::D3D12;
617 } else if (rhiBackend == QByteArrayLiteral("vulkan")) {
618 implementation = QRhi::Vulkan;
619 } else if (rhiBackend == QByteArrayLiteral("metal")) {
620 implementation = QRhi::Metal;
621 } else if (rhiBackend == QByteArrayLiteral("null")) {
622 implementation = QRhi::Null;
623 } else {
624 if (!rhiBackend.isEmpty()) {
625 qWarning("Unknown key \"%s\" for QSG_RHI_BACKEND, falling back to default backend.", rhiBackend.constData());
626 }
627#if defined(Q_OS_WIN)
628 implementation = QRhi::D3D11;
629#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
630 implementation = QRhi::Metal;
631#elif QT_CONFIG(opengl)
632 implementation = QRhi::OpenGLES2;
633#else
634 implementation = QRhi::Vulkan;
635#endif
636 }
637
638 adjustToPlatformQuirks(implementation);
639
640 return implementation;
641}
642
643QString renderToKTXFile(const QString &inPath, const QString &outPath)
644{
645 const auto rhiImplementation = getRhiImplementation();
646
647#if QT_CONFIG(opengl)
648 if (rhiImplementation == QRhi::OpenGLES2) {
649 QRhiGles2InitParams params;
650 if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
651 // OpenGL 3.2 or higher
652 params.format.setProfile(QSurfaceFormat::CoreProfile);
653 params.format.setVersion(3, 2);
654 } else {
655 // OpenGL ES 3.0 or higher
656 params.format.setVersion(3, 0);
657 }
658 params.fallbackSurface = QRhiGles2InitParams::newFallbackSurface();
659 const QString result = renderToKTXFileInternal("OpenGL", inPath, outPath, QRhi::OpenGLES2, &params);
660 delete params.fallbackSurface;
661 return result;
662 }
663#endif
664
665#if QT_CONFIG(vulkan)
666 if (rhiImplementation == QRhi::Vulkan) {
667 QVulkanInstance vulkanInstance;
668 vulkanInstance.create();
669 QRhiVulkanInitParams params;
670 params.inst = &vulkanInstance;
671 return renderToKTXFileInternal("Vulkan", inPath, outPath, QRhi::Vulkan, &params);
672 }
673#endif
674
675#ifdef Q_OS_WIN
676 if (rhiImplementation == QRhi::D3D11) {
677 QRhiD3D11InitParams params;
678 return renderToKTXFileInternal("Direct3D 11", inPath, outPath, QRhi::D3D11, &params);
679 } else if (rhiImplementation == QRhi::D3D12) {
680 QRhiD3D12InitParams params;
681 return renderToKTXFileInternal("Direct3D 12", inPath, outPath, QRhi::D3D12, &params);
682 }
683#endif
684
685#if QT_CONFIG(metal)
686 if (rhiImplementation == QRhi::Metal) {
687 QRhiMetalInitParams params;
688 return renderToKTXFileInternal("Metal", inPath, outPath, QRhi::Metal, &params);
689 }
690#endif
691
692 return QStringLiteral("No RHI backend");
693}
694
695const QString QSSGIblBaker::import(const QString &sourceFile, const QDir &savePath, QStringList *generatedFiles)
696{
697 qDebug() << "IBL lightprobe baker" << sourceFile;
698
699 QString outFileName = savePath.absoluteFilePath(QFileInfo(sourceFile).baseName() + QStringLiteral(".ktx"));
700
701 QString error = renderToKTXFile(sourceFile, outFileName);
702 if (!error.isEmpty())
703 return error;
704
705 m_generatedFiles.append(outFileName);
706
707 if (generatedFiles)
708 *generatedFiles = m_generatedFiles;
709
710 return QString();
711}
712
713QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define GL_RGBA16F
Definition qopenglext.h:913
#define GL_HALF_FLOAT
void adjustToPlatformQuirks(QRhi::Implementation &impl)
QRhi::Implementation getRhiImplementation()
QString renderToKTXFile(const QString &inPath, const QString &outPath)
QString renderToKTXFileInternal(const char *name, const QString &inPath, const QString &outPath, QRhi::Implementation impl, QRhiInitParams *initParams)
static constexpr QSSGRenderTextureFormat FORMAT(QSSGRenderTextureFormat::RGBA16F)
static const float cube[]
#define GL_RGBA