67 qDebug() <<
"Using RHI backend" << name;
70 QFile ktxOutputFile(outPath);
71 if (!ktxOutputFile.open(QIODevice::WriteOnly)) {
72 return QStringLiteral(
"Could not open file: %1").arg(outPath);
75 QScopedPointer<QRhi> rhi(QRhi::create(impl, initParams, QRhi::Flags(),
nullptr));
77 return QStringLiteral(
"Failed to initialize QRhi");
79 qDebug() << rhi->driverInfo();
81 QRhiCommandBuffer *cb;
82 rhi->beginOffscreenFrame(&cb);
84 const auto rhiContext = std::make_unique<QSSGRhiContext>(rhi.get());
85 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiContext.get());
86 rhiCtxD->setCommandBuffer(cb);
88 QScopedPointer<QSSGLoadedTexture> inImage(QSSGLoadedTexture::loadHdrImage(QSSGInputUtil::getStreamForFile(inPath), FORMAT));
90 return QStringLiteral(
"Failed to load hdr file");
92 auto shaderCache = std::make_unique<QSSGShaderCache>(*rhiContext);
94 QSSGRenderImageTexture envMap;
95 if (!QSSGBufferManager::createEnvironmentMap(rhiContext.get(), shaderCache.get(), inImage.get(), &envMap, inPath))
96 return QStringLiteral(
"Failed to generate the environment map");
97 QRhiTexture *preFilteredEnvCubeMap = envMap.m_texture;
98 if (preFilteredEnvCubeMap->format() != QRhiTexture::RGBA16F)
99 return QStringLiteral(
"Unexpected environment map format");
100 const QSize environmentMapSize = preFilteredEnvCubeMap->pixelSize();
104 const quint32 numberOfMipmapLevels = envMap.m_mipmapCount;
105 const quint32 numberOfFaces = 6;
107 constexpr size_t KTX_IDENTIFIER_LENGTH = 12;
108 constexpr char ktxIdentifier[KTX_IDENTIFIER_LENGTH] = {
'\xAB',
'K',
'T',
'X',
' ',
'1',
109 '1',
'\xBB',
'\r',
'\n',
'\x1A',
'\n' };
110 constexpr quint32 platformEndianIdentifier = 0x04030201;
111 QVector<
char> keyValueData;
116 static const char key[] =
"QT_IBL_BAKER_VERSION";
117 static const char value[] =
"1";
119 constexpr size_t keyAndValueByteSize =
sizeof(key) +
sizeof(value);
120 appendBinaryVector(keyValueData, keyAndValueByteSize);
121 appendBinaryVector(keyValueData, key);
122 appendBinaryVector(keyValueData, value);
125 const size_t padding = 3 - ((keyAndValueByteSize + 3) % 4);
126 keyValueData.resize(keyValueData.size() + padding);
132 ktxOutputFile.write(ktxIdentifier, KTX_IDENTIFIER_LENGTH);
135 writeUInt32(ktxOutputFile, quint32(platformEndianIdentifier));
141 writeUInt32(ktxOutputFile, quint32(FORMAT.getSizeofFormat()) / quint32(FORMAT.getNumberOfComponent()));
144 writeUInt32(ktxOutputFile, quint32(
GL_RGBA));
147 writeUInt32(ktxOutputFile, quint32(
GL_RGBA16F));
150 writeUInt32(ktxOutputFile, quint32(
GL_RGBA));
153 writeUInt32(ktxOutputFile, quint32(environmentMapSize.width()));
156 writeUInt32(ktxOutputFile, quint32(environmentMapSize.height()));
159 writeUInt32(ktxOutputFile, quint32(0));
162 writeUInt32(ktxOutputFile, quint32(0));
165 writeUInt32(ktxOutputFile, quint32(numberOfFaces));
168 writeUInt32(ktxOutputFile, quint32(numberOfMipmapLevels));
171 writeUInt32(ktxOutputFile, quint32(keyValueData.size()));
174 ktxOutputFile.write(keyValueData.data(), keyValueData.size());
177 for (quint32 mipmap_level = 0; mipmap_level < numberOfMipmapLevels; mipmap_level++) {
178 quint32 imageSize = 0;
179 for (size_t face = 0; face < numberOfFaces; face++) {
181 Q_ASSERT(rhi->isRecordingFrame());
183 QRhiReadbackResult result;
184 QRhiReadbackDescription readbackDesc(preFilteredEnvCubeMap);
185 readbackDesc.setLayer(
int(face));
186 readbackDesc.setLevel(mipmap_level);
188 QRhiResourceUpdateBatch *resourceUpdates = rhi->nextResourceUpdateBatch();
189 resourceUpdates->readBackTexture(readbackDesc, &result);
191 cb->resourceUpdate(resourceUpdates);
195 if (imageSize == 0) {
196 imageSize = result.data.size();
197 writeUInt32(ktxOutputFile, quint32(imageSize));
200 ktxOutputFile.write(result.data);
204 ktxOutputFile.close();
206 preFilteredEnvCubeMap->deleteLater();
208 rhi->endOffscreenFrame();
237 QRhi::Implementation implementation = QRhi::Implementation::Null;
240 const QByteArray rhiBackend = qgetenv(
"QSG_RHI_BACKEND");
241 if (rhiBackend == QByteArrayLiteral(
"gl") || rhiBackend == QByteArrayLiteral(
"gles2")
242 || rhiBackend == QByteArrayLiteral(
"opengl")) {
243 implementation = QRhi::OpenGLES2;
244 }
else if (rhiBackend == QByteArrayLiteral(
"d3d11") || rhiBackend == QByteArrayLiteral(
"d3d")) {
245 implementation = QRhi::D3D11;
246 }
else if (rhiBackend == QByteArrayLiteral(
"d3d12")) {
247 implementation = QRhi::D3D12;
248 }
else if (rhiBackend == QByteArrayLiteral(
"vulkan")) {
249 implementation = QRhi::Vulkan;
250 }
else if (rhiBackend == QByteArrayLiteral(
"metal")) {
251 implementation = QRhi::Metal;
252 }
else if (rhiBackend == QByteArrayLiteral(
"null")) {
253 implementation = QRhi::Null;
255 if (!rhiBackend.isEmpty()) {
256 qWarning(
"Unknown key \"%s\" for QSG_RHI_BACKEND, falling back to default backend.", rhiBackend.constData());
259 implementation = QRhi::D3D11;
260#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
261 implementation = QRhi::Metal;
262#elif QT_CONFIG(opengl)
263 implementation = QRhi::OpenGLES2;
265 implementation = QRhi::Vulkan;
269 adjustToPlatformQuirks(implementation);
271 return implementation;
276 const auto rhiImplementation = getRhiImplementation();
279 if (rhiImplementation == QRhi::OpenGLES2) {
280 QRhiGles2InitParams params;
281 if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
283 params.format.setProfile(QSurfaceFormat::CoreProfile);
284 params.format.setVersion(3, 2);
287 params.format.setVersion(3, 0);
289 params.fallbackSurface = QRhiGles2InitParams::newFallbackSurface();
290 const QString result = renderToKTXFileInternal(
"OpenGL", inPath, outPath, QRhi::OpenGLES2, ¶ms);
291 delete params.fallbackSurface;
297 if (rhiImplementation == QRhi::Vulkan) {
298 QVulkanInstance vulkanInstance;
299 vulkanInstance.create();
300 QRhiVulkanInitParams params;
301 params.inst = &vulkanInstance;
302 return renderToKTXFileInternal(
"Vulkan", inPath, outPath, QRhi::Vulkan, ¶ms);
307 if (rhiImplementation == QRhi::D3D11) {
308 QRhiD3D11InitParams params;
309 return renderToKTXFileInternal(
"Direct3D 11", inPath, outPath, QRhi::D3D11, ¶ms);
310 }
else if (rhiImplementation == QRhi::D3D12) {
311 QRhiD3D12InitParams params;
312 return renderToKTXFileInternal(
"Direct3D 12", inPath, outPath, QRhi::D3D12, ¶ms);
317 if (rhiImplementation == QRhi::Metal) {
318 QRhiMetalInitParams params;
319 return renderToKTXFileInternal(
"Metal", inPath, outPath, QRhi::Metal, ¶ms);
323 return QStringLiteral(
"No RHI backend");