From fe97af0c9ad58f0f823f3b1942cd9ad383ba85d3 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 7 Jan 2020 15:15:09 +0100 Subject: rhi: Add manual test for RGBA16F and compute Uses the two compute shaders from Qt Quick 3D. Demonstrates and tests both RGBA16F textures and using them (and doing load/store with mip levels individually) in combination with compute. Task-number: QTBUG-81213 Change-Id: I3f0f250d5997a26c857b7c45517684c63b44e58e Reviewed-by: Johan Helsing --- .../float16texture_with_compute/buildshaders.sh | 3 + .../float16texture_with_compute.cpp | 312 +++++++++++++++++++++ .../float16texture_with_compute.pro | 8 + .../float16texture_with_compute.qrc | 9 + .../rhi/float16texture_with_compute/load.comp | 19 ++ .../rhi/float16texture_with_compute/load.comp.qsb | Bin 0 -> 1596 bytes .../rhi/float16texture_with_compute/prefilter.comp | 50 ++++ .../float16texture_with_compute/prefilter.comp.qsb | Bin 0 -> 3659 bytes tests/manual/rhi/rhi.pro | 1 + 9 files changed, 402 insertions(+) create mode 100755 tests/manual/rhi/float16texture_with_compute/buildshaders.sh create mode 100644 tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.cpp create mode 100644 tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.pro create mode 100644 tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.qrc create mode 100644 tests/manual/rhi/float16texture_with_compute/load.comp create mode 100644 tests/manual/rhi/float16texture_with_compute/load.comp.qsb create mode 100644 tests/manual/rhi/float16texture_with_compute/prefilter.comp create mode 100644 tests/manual/rhi/float16texture_with_compute/prefilter.comp.qsb (limited to 'tests/manual/rhi') diff --git a/tests/manual/rhi/float16texture_with_compute/buildshaders.sh b/tests/manual/rhi/float16texture_with_compute/buildshaders.sh new file mode 100755 index 0000000000..7d4d27741d --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/buildshaders.sh @@ -0,0 +1,3 @@ +#!/bin/sh +qsb --glsl "430,310 es" --hlsl 50 --msl 12 load.comp -o load.comp.qsb +qsb --glsl "430,310 es" --hlsl 50 --msl 12 prefilter.comp -o prefilter.comp.qsb diff --git a/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.cpp b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.cpp new file mode 100644 index 0000000000..9d74fe797b --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.cpp @@ -0,0 +1,312 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// An advanced version of floattexture. Instead of RGBA32F, we use RGBA16F, and +// also generate the floating point data from rgba with compute. Then there's a +// compute pass using the BSDF prefiltering taken from Qt Quick 3D, which +// generates all the mip levels. + +// Why do we animate the scale of the quad rendered to the window? To have +// different mip levels used, to prove that all of them are generated +// correctly, without artifacts (which would occur if memory barriers were not +// correctly generated by QRhi). For full verification use RenderDoc or similar. + +#include "../shared/examplefw.h" +#include + +static float vertexData[] = +{ // Y up, CCW + -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 1.0f, 0.0f +}; + +static quint16 indexData[] = +{ + 0, 1, 2, 0, 2, 3 +}; + +static const int MAX_MIP_LEVELS = 20; + +struct { + QVector releasePool; + + QRhiBuffer *vbuf = nullptr; + QRhiBuffer *ibuf = nullptr; + QRhiBuffer *ubuf = nullptr; + QRhiTexture *texRgba = nullptr; + QRhiTexture *texFloat16 = nullptr; + QRhiSampler *sampler = nullptr; + QRhiShaderResourceBindings *srb = nullptr; + QRhiGraphicsPipeline *ps = nullptr; + + QRhiBuffer *computeUBuf_load = nullptr; + QRhiShaderResourceBindings *computeBindings_load = nullptr; + QRhiComputePipeline *computePipeline_load = nullptr; + QRhiBuffer *computeUBuf_prefilter = nullptr; + QRhiShaderResourceBindings *computeBindings_prefilter[MAX_MIP_LEVELS]; + QRhiComputePipeline *computePipeline_prefilter = nullptr; + + QRhiResourceUpdateBatch *initialUpdates = nullptr; + bool computeDone = false; + int mipCount; + int prefilterUBufElemSize; + quint32 prefilterNumWorkGroups[MAX_MIP_LEVELS][3]; + float scale = 2.5f; + int scale_dir = -1; +} d; + +void recordUploadThenFilterFloat16TextureWithCompute(QRhiCommandBuffer *cb) +{ + const int w = d.texRgba->pixelSize().width() / 16; + const int h = d.texRgba->pixelSize().height() / 16; + + cb->beginComputePass(); + + cb->setComputePipeline(d.computePipeline_load); + cb->setShaderResources(); + cb->dispatch(w, h, 1); + + cb->setComputePipeline(d.computePipeline_prefilter); + for (int level = 1; level < d.mipCount; ++level) { + const int i = level - 1; + const int mipW = d.prefilterNumWorkGroups[i][0]; + const int mipH = d.prefilterNumWorkGroups[i][1]; + QPair dynamicOffset = { 0, quint32(d.prefilterUBufElemSize * i) }; + cb->setShaderResources(d.computeBindings_prefilter[i], 1, &dynamicOffset); + cb->dispatch(mipW, mipH, 1); + } + + cb->endComputePass(); +} + +void Window::customInit() +{ + if (!m_r->isFeatureSupported(QRhi::Compute)) + qFatal("Compute is not supported"); + + if (!m_r->isTextureFormatSupported(QRhiTexture::RGBA16F)) + qFatal("RGBA16F texture format is not supported"); + + d.initialUpdates = m_r->nextResourceUpdateBatch(); + + // load rgba8 image data + + QImage image; + image.load(QLatin1String(":/qt256.png")); + image = image.convertToFormat(QImage::Format_RGBA8888); + Q_ASSERT(!image.isNull()); + d.texRgba = m_r->newTexture(QRhiTexture::RGBA8, image.size(), 1, QRhiTexture::UsedWithLoadStore); + d.texRgba->build(); + d.releasePool << d.texRgba; + + d.initialUpdates->uploadTexture(d.texRgba, image); + + d.mipCount = m_r->mipLevelsForSize(image.size()); + Q_ASSERT(d.mipCount <= MAX_MIP_LEVELS); + + d.texFloat16 = m_r->newTexture(QRhiTexture::RGBA16F, image.size(), 1, QRhiTexture::UsedWithLoadStore | QRhiTexture::MipMapped); + d.releasePool << d.texFloat16; + d.texFloat16->build(); + + // compute + + d.computeUBuf_load = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 12); + d.computeUBuf_load->build(); + d.releasePool << d.computeUBuf_load; + + quint32 numWorkGroups[3] = { quint32(image.width()), quint32(image.height()), 0 }; + d.initialUpdates->updateDynamicBuffer(d.computeUBuf_load, 0, 12, numWorkGroups); + + d.computeBindings_load = m_r->newShaderResourceBindings(); + d.computeBindings_load->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf_load), + QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texRgba, 0), + QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, 0) + }); + d.computeBindings_load->build(); + d.releasePool << d.computeBindings_load; + + d.computePipeline_load = m_r->newComputePipeline(); + d.computePipeline_load->setShaderResourceBindings(d.computeBindings_load); + d.computePipeline_load->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/load.comp.qsb")) }); + d.computePipeline_load->build(); + d.releasePool << d.computePipeline_load; + + d.prefilterUBufElemSize = m_r->ubufAligned(12); + d.computeUBuf_prefilter = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, d.prefilterUBufElemSize * d.mipCount); + d.computeUBuf_prefilter->build(); + d.releasePool << d.computeUBuf_prefilter; + + int mipW = image.width() >> 1; + int mipH = image.height() >> 1; + for (int level = 1; level < d.mipCount; ++level) { + const int i = level - 1; + d.prefilterNumWorkGroups[i][0] = quint32(mipW); + d.prefilterNumWorkGroups[i][1] = quint32(mipH); + d.prefilterNumWorkGroups[i][2] = 0; + d.initialUpdates->updateDynamicBuffer(d.computeUBuf_prefilter, d.prefilterUBufElemSize * i, 12, d.prefilterNumWorkGroups[i]); + mipW = mipW > 2 ? mipW >> 1 : 1; + mipH = mipH > 2 ? mipH >> 1 : 1; + + d.computeBindings_prefilter[i] = m_r->newShaderResourceBindings(); + d.computeBindings_prefilter[i]->setBindings({ + QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::ComputeStage, d.computeUBuf_prefilter, 12), + QRhiShaderResourceBinding::imageLoad(1, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, level - 1), + QRhiShaderResourceBinding::imageStore(2, QRhiShaderResourceBinding::ComputeStage, d.texFloat16, level) + }); + d.computeBindings_prefilter[i]->build(); + d.releasePool << d.computeBindings_prefilter[i]; + } + + d.computePipeline_prefilter = m_r->newComputePipeline(); + d.computePipeline_prefilter->setShaderResourceBindings(d.computeBindings_prefilter[0]); // just need a layout compatible one + d.computePipeline_prefilter->setShaderStage({ QRhiShaderStage::Compute, getShader(QLatin1String(":/prefilter.comp.qsb")) }); + d.computePipeline_prefilter->build(); + d.releasePool << d.computePipeline_prefilter; + + // graphics + + d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)); + d.vbuf->build(); + d.releasePool << d.vbuf; + + d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData)); + d.ibuf->build(); + d.releasePool << d.ibuf; + + d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); + d.ubuf->build(); + d.releasePool << d.ubuf; + + // enable mipmaps + d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::Linear, + QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); + d.releasePool << d.sampler; + d.sampler->build(); + + d.srb = m_r->newShaderResourceBindings(); + d.releasePool << d.srb; + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.texFloat16, d.sampler) + }); + d.srb->build(); + + d.ps = m_r->newGraphicsPipeline(); + d.releasePool << d.ps; + d.ps->setShaderStages({ + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } + }); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 4 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float2, 0 }, + { 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) } + }); + d.ps->setVertexInputLayout(inputLayout); + d.ps->setShaderResourceBindings(d.srb); + d.ps->setRenderPassDescriptor(m_rp); + d.ps->build(); + + d.initialUpdates->uploadStaticBuffer(d.vbuf, vertexData); + d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData); + + qint32 flip = 0; + d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); +} + +void Window::customRelease() +{ + qDeleteAll(d.releasePool); + d.releasePool.clear(); +} + +void Window::customRender() +{ + QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); + QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); + if (d.initialUpdates) { + u->merge(d.initialUpdates); + d.initialUpdates->release(); + d.initialUpdates = nullptr; + } + + QMatrix4x4 mvp = m_proj; + mvp.scale(d.scale); + d.scale += d.scale_dir * 0.01f; + if (qFuzzyIsNull(d.scale) || d.scale >= 2.5f) + d.scale_dir *= -1; + u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); + + cb->resourceUpdate(u); + + // If not yet done, then do a compute pass that uploads level 0, doing an + // rgba8 -> float16 conversion. Follow that with another compute pass to do + // the filtering and generate all the mip levels. + if (!d.computeDone) { + recordUploadThenFilterFloat16TextureWithCompute(cb); + d.computeDone = true; + } + + const QSize outputSizeInPixels = m_sc->currentPixelSize(); + cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); + cb->setGraphicsPipeline(d.ps); + cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); + cb->drawIndexed(6); + cb->endPass(); +} diff --git a/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.pro b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.pro new file mode 100644 index 0000000000..7708e845d0 --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +QT += gui-private + +SOURCES = \ + float16texture_with_compute.cpp + +RESOURCES = float16texture_with_compute.qrc diff --git a/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.qrc b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.qrc new file mode 100644 index 0000000000..ce3c52aac5 --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/float16texture_with_compute.qrc @@ -0,0 +1,9 @@ + + + load.comp.qsb + prefilter.comp.qsb + ../shared/texture.vert.qsb + ../shared/texture.frag.qsb + ../shared/qt256.png + + diff --git a/tests/manual/rhi/float16texture_with_compute/load.comp b/tests/manual/rhi/float16texture_with_compute/load.comp new file mode 100644 index 0000000000..eb7bc1ece4 --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/load.comp @@ -0,0 +1,19 @@ +#version 440 + +layout(local_size_x = 16, local_size_y = 16) in; +layout(rgba8, binding = 1) readonly uniform image2D inputImage; +layout(rgba16f, binding = 2) writeonly uniform image2D outputImage; + +// There is no equivalent of gl_NumWorkGroups in HLSL. So instead pass the +// values in in a uniform buffer. +layout(std140, binding = 0) uniform numWorkGroupsBuf { + uvec3 numWorkGroups; +}; + +void main() +{ + if (gl_GlobalInvocationID.x >= numWorkGroups.x || gl_GlobalInvocationID.y >= numWorkGroups.y) + return; + vec4 value = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.xy)); + imageStore(outputImage, ivec2(gl_GlobalInvocationID.xy), value); +} diff --git a/tests/manual/rhi/float16texture_with_compute/load.comp.qsb b/tests/manual/rhi/float16texture_with_compute/load.comp.qsb new file mode 100644 index 0000000000..bfdd7f5446 Binary files /dev/null and b/tests/manual/rhi/float16texture_with_compute/load.comp.qsb differ diff --git a/tests/manual/rhi/float16texture_with_compute/prefilter.comp b/tests/manual/rhi/float16texture_with_compute/prefilter.comp new file mode 100644 index 0000000000..ba09ecb129 --- /dev/null +++ b/tests/manual/rhi/float16texture_with_compute/prefilter.comp @@ -0,0 +1,50 @@ +#version 440 + +layout(local_size_x = 16, local_size_y = 16) in; +layout(rgba16f, binding = 1) readonly uniform image2D inputImage; +layout(rgba16f, binding = 2) writeonly uniform image2D outputImage; + +// There is no equivalent of gl_NumWorkGroups in HLSL. So instead pass the +// values in in a uniform buffer. +layout(std140, binding = 0) uniform numWorkGroupsBuf { + uvec3 numWorkGroups; +}; + +int wrapMod( in int a, in int base ) +{ + return ( a >= 0 ) ? a % base : -(a % base) + base; +} + +void getWrappedCoords( inout int sX, inout int sY, in int width, in int height ) +{ + if (sY < 0) { sX -= width >> 1; sY = -sY; } + if (sY >= height) { sX += width >> 1; sY = height - sY; } + sX = wrapMod( sX, width ); +} + +void main() +{ + int prevWidth = int(numWorkGroups.x) << 1; + int prevHeight = int(numWorkGroups.y) << 1; + if (gl_GlobalInvocationID.x >= numWorkGroups.x || gl_GlobalInvocationID.y >= numWorkGroups.y) + return; + vec4 accumVal = vec4(0.0); + for (int sy = -2; sy <= 2; ++sy) { + for (int sx = -2; sx <= 2; ++sx) { + int sampleX = sx + (int(gl_GlobalInvocationID.x) << 1); + int sampleY = sy + (int(gl_GlobalInvocationID.y) << 1); + getWrappedCoords(sampleX, sampleY, prevWidth, prevHeight); + if ((sampleY * prevWidth + sampleX) < 0 ) + sampleY = prevHeight + sampleY; + ivec2 pos = ivec2(sampleX, sampleY); + vec4 value = imageLoad(inputImage, pos); + float filterPdf = 1.0 / ( 1.0 + float(sx*sx + sy*sy)*2.0 ); + filterPdf /= 4.71238898; + accumVal[0] += filterPdf * value.r; + accumVal[1] += filterPdf * value.g; + accumVal[2] += filterPdf * value.b; + accumVal[3] += filterPdf * value.a; + } + } + imageStore(outputImage, ivec2(gl_GlobalInvocationID.xy), accumVal); +} diff --git a/tests/manual/rhi/float16texture_with_compute/prefilter.comp.qsb b/tests/manual/rhi/float16texture_with_compute/prefilter.comp.qsb new file mode 100644 index 0000000000..77618a12b9 Binary files /dev/null and b/tests/manual/rhi/float16texture_with_compute/prefilter.comp.qsb differ diff --git a/tests/manual/rhi/rhi.pro b/tests/manual/rhi/rhi.pro index 4768ee1c6d..5208e5dea5 100644 --- a/tests/manual/rhi/rhi.pro +++ b/tests/manual/rhi/rhi.pro @@ -14,6 +14,7 @@ SUBDIRS += \ triquadcube \ offscreen \ floattexture \ + float16texture_with_compute \ mrt \ shadowmap \ computebuffer \ -- cgit v1.2.3 From 1f267b7e6def96f8041f29a9b4c7da753c736458 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Thu, 9 Jan 2020 12:38:27 +0100 Subject: rhi: Add render-to-cubemap manual test What's more, demonstrate two types of rendering to a cubemap: one by one to each face, and by attaching all faces as color attachments in one go. Both are used by Qt Quick 3D in connection with shadows, so this proves that the same is possible to implement with QRhi. Task-number: QTBUG-81261 Change-Id: I5c7077224d7cae0dd6ea02ac30a9e6f9f1f0c229 Reviewed-by: Eirik Aavitsland --- tests/manual/rhi/cubemap_render/buildshader.bat | 6 + tests/manual/rhi/cubemap_render/cubemap_mrt.frag | 28 ++ .../manual/rhi/cubemap_render/cubemap_mrt.frag.qsb | Bin 0 -> 1688 bytes tests/manual/rhi/cubemap_render/cubemap_mrt.vert | 20 + .../manual/rhi/cubemap_render/cubemap_mrt.vert.qsb | Bin 0 -> 1223 bytes .../manual/rhi/cubemap_render/cubemap_oneface.frag | 13 + .../rhi/cubemap_render/cubemap_oneface.frag.qsb | Bin 0 -> 993 bytes .../manual/rhi/cubemap_render/cubemap_oneface.vert | 15 + .../rhi/cubemap_render/cubemap_oneface.vert.qsb | Bin 0 -> 1073 bytes tests/manual/rhi/cubemap_render/cubemap_render.cpp | 466 +++++++++++++++++++++ tests/manual/rhi/cubemap_render/cubemap_render.pro | 8 + tests/manual/rhi/cubemap_render/cubemap_render.qrc | 10 + .../manual/rhi/cubemap_render/cubemap_sample.frag | 10 + .../rhi/cubemap_render/cubemap_sample.frag.qsb | Bin 0 -> 1028 bytes .../manual/rhi/cubemap_render/cubemap_sample.vert | 16 + .../rhi/cubemap_render/cubemap_sample.vert.qsb | Bin 0 -> 1145 bytes tests/manual/rhi/rhi.pro | 1 + tests/manual/rhi/shared/examplefw.h | 3 + 18 files changed, 596 insertions(+) create mode 100755 tests/manual/rhi/cubemap_render/buildshader.bat create mode 100644 tests/manual/rhi/cubemap_render/cubemap_mrt.frag create mode 100644 tests/manual/rhi/cubemap_render/cubemap_mrt.frag.qsb create mode 100644 tests/manual/rhi/cubemap_render/cubemap_mrt.vert create mode 100644 tests/manual/rhi/cubemap_render/cubemap_mrt.vert.qsb create mode 100644 tests/manual/rhi/cubemap_render/cubemap_oneface.frag create mode 100644 tests/manual/rhi/cubemap_render/cubemap_oneface.frag.qsb create mode 100644 tests/manual/rhi/cubemap_render/cubemap_oneface.vert create mode 100644 tests/manual/rhi/cubemap_render/cubemap_oneface.vert.qsb create mode 100644 tests/manual/rhi/cubemap_render/cubemap_render.cpp create mode 100644 tests/manual/rhi/cubemap_render/cubemap_render.pro create mode 100644 tests/manual/rhi/cubemap_render/cubemap_render.qrc create mode 100644 tests/manual/rhi/cubemap_render/cubemap_sample.frag create mode 100644 tests/manual/rhi/cubemap_render/cubemap_sample.frag.qsb create mode 100644 tests/manual/rhi/cubemap_render/cubemap_sample.vert create mode 100644 tests/manual/rhi/cubemap_render/cubemap_sample.vert.qsb (limited to 'tests/manual/rhi') diff --git a/tests/manual/rhi/cubemap_render/buildshader.bat b/tests/manual/rhi/cubemap_render/buildshader.bat new file mode 100755 index 0000000000..3886c138d8 --- /dev/null +++ b/tests/manual/rhi/cubemap_render/buildshader.bat @@ -0,0 +1,6 @@ +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_oneface.vert -o cubemap_oneface.vert.qsb +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_oneface.frag -o cubemap_oneface.frag.qsb +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_mrt.vert -o cubemap_mrt.vert.qsb +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_mrt.frag -o cubemap_mrt.frag.qsb +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_sample.vert -o cubemap_sample.vert.qsb +qsb --glsl "300 es,120" --hlsl 50 --msl 12 cubemap_sample.frag -o cubemap_sample.frag.qsb diff --git a/tests/manual/rhi/cubemap_render/cubemap_mrt.frag b/tests/manual/rhi/cubemap_render/cubemap_mrt.frag new file mode 100644 index 0000000000..06a08f6cd6 --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_mrt.frag @@ -0,0 +1,28 @@ +#version 440 + +layout(location = 0) out vec4 c0; +layout(location = 1) out vec4 c1; +layout(location = 2) out vec4 c2; +layout(location = 3) out vec4 c3; +layout(location = 4) out vec4 c4; +layout(location = 5) out vec4 c5; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; + vec3 color0; + vec3 color1; + vec3 color2; + vec3 color3; + vec3 color4; + vec3 color5; +} ubuf; + +void main() +{ + c0 = vec4(ubuf.color0, 1.0); + c1 = vec4(ubuf.color1, 1.0); + c2 = vec4(ubuf.color2, 1.0); + c3 = vec4(ubuf.color3, 1.0); + c4 = vec4(ubuf.color4, 1.0); + c5 = vec4(ubuf.color5, 1.0); +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_mrt.frag.qsb b/tests/manual/rhi/cubemap_render/cubemap_mrt.frag.qsb new file mode 100644 index 0000000000..7a0be83a81 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_mrt.frag.qsb differ diff --git a/tests/manual/rhi/cubemap_render/cubemap_mrt.vert b/tests/manual/rhi/cubemap_render/cubemap_mrt.vert new file mode 100644 index 0000000000..6d82feccc8 --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_mrt.vert @@ -0,0 +1,20 @@ +#version 440 + +layout(location = 0) in vec4 position; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; + vec3 color0; + vec3 color1; + vec3 color2; + vec3 color3; + vec3 color4; + vec3 color5; +} ubuf; + +out gl_PerVertex { vec4 gl_Position; }; + +void main() +{ + gl_Position = ubuf.mvp * position; +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_mrt.vert.qsb b/tests/manual/rhi/cubemap_render/cubemap_mrt.vert.qsb new file mode 100644 index 0000000000..9358ac2792 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_mrt.vert.qsb differ diff --git a/tests/manual/rhi/cubemap_render/cubemap_oneface.frag b/tests/manual/rhi/cubemap_render/cubemap_oneface.frag new file mode 100644 index 0000000000..8bafe689be --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_oneface.frag @@ -0,0 +1,13 @@ +#version 440 + +layout(location = 0) out vec4 fragColor; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; + vec3 color; +} ubuf; + +void main() +{ + fragColor = vec4(ubuf.color, 1.0); +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_oneface.frag.qsb b/tests/manual/rhi/cubemap_render/cubemap_oneface.frag.qsb new file mode 100644 index 0000000000..a6187939a0 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_oneface.frag.qsb differ diff --git a/tests/manual/rhi/cubemap_render/cubemap_oneface.vert b/tests/manual/rhi/cubemap_render/cubemap_oneface.vert new file mode 100644 index 0000000000..2db9076a74 --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_oneface.vert @@ -0,0 +1,15 @@ +#version 440 + +layout(location = 0) in vec4 position; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; + vec3 color; +} ubuf; + +out gl_PerVertex { vec4 gl_Position; }; + +void main() +{ + gl_Position = ubuf.mvp * position; +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_oneface.vert.qsb b/tests/manual/rhi/cubemap_render/cubemap_oneface.vert.qsb new file mode 100644 index 0000000000..58772179e5 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_oneface.vert.qsb differ diff --git a/tests/manual/rhi/cubemap_render/cubemap_render.cpp b/tests/manual/rhi/cubemap_render/cubemap_render.cpp new file mode 100644 index 0000000000..a08e96f0c6 --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_render.cpp @@ -0,0 +1,466 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// Demonstrates rendering to two cubemaps in two different ways: +// - one by one, to each face, +// - if the supported max number of color attachments is greater than 4: in +// one go with all 6 faces attached as render targets. +// +// Finally, show what we got in a skybox-ish thing. Press the arrow keys to +// switch between the two cubemaps. (the only difference should be their +// background clear color) + +#define EXAMPLEFW_KEYPRESS_EVENTS +#include "../shared/examplefw.h" +#include "../shared/cube.h" + +// each face is 512x512 +static const QSize cubemapSize(512, 512); + +// each cubemap face gets a 256x256 quad in the center +static float halfQuadVertexData[] = +{ // Y up, CCW + -0.5f, 0.5f, + -0.5f, -0.5f, + 0.5f, -0.5f, + 0.5f, 0.5f, +}; + +static quint16 halfQuadIndexData[] = +{ + 0, 1, 2, 0, 2, 3 +}; + +struct { + QVector releasePool; + + QRhiTexture *cubemap1 = nullptr; + QRhiTexture *cubemap2 = nullptr; + bool canDoMrt = false; + + QRhiBuffer *half_quad_vbuf = nullptr; + QRhiBuffer *half_quad_ibuf = nullptr; + + QRhiBuffer *oneface_ubuf = nullptr; + int ubufSizePerFace; + QRhiTextureRenderTarget *oneface_rt[6]; + QRhiRenderPassDescriptor *oneface_rp = nullptr; + QRhiShaderResourceBindings *oneface_srb = nullptr; + QRhiGraphicsPipeline *oneface_ps = nullptr; + + QRhiBuffer *mrt_ubuf = nullptr; + QRhiTextureRenderTarget *mrt_rt = nullptr; + QRhiRenderPassDescriptor *mrt_rp = nullptr; + QRhiShaderResourceBindings *mrt_srb = nullptr; + QRhiGraphicsPipeline *mrt_ps = nullptr; + + QRhiBuffer *vbuf = nullptr; + QRhiBuffer *ubuf = nullptr; + QRhiSampler *sampler = nullptr; + QRhiShaderResourceBindings *srb = nullptr; + QRhiGraphicsPipeline *ps = nullptr; + + QRhiResourceUpdateBatch *initialUpdates = nullptr; + QMatrix4x4 winProj; + float rx = 0; +} d; + +void initializePerFaceRendering(QRhi *rhi) +{ + d.cubemap1 = rhi->newTexture(QRhiTexture::RGBA8, cubemapSize, 1, QRhiTexture::CubeMap | QRhiTexture::RenderTarget); + d.cubemap1->build(); + d.releasePool << d.cubemap1; + + d.ubufSizePerFace = rhi->ubufAligned(64 + 12); + d.oneface_ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, d.ubufSizePerFace * 6); + d.oneface_ubuf->build(); + d.releasePool << d.oneface_ubuf; + + for (int face = 0; face < 6; ++face) { + QRhiColorAttachment att(d.cubemap1); + att.setLayer(face); + QRhiTextureRenderTargetDescription rtDesc(att); + d.oneface_rt[face] = rhi->newTextureRenderTarget(rtDesc); + if (face == 0) { + d.oneface_rp = d.oneface_rt[0]->newCompatibleRenderPassDescriptor(); + d.releasePool << d.oneface_rp; + } + d.oneface_rt[face]->setRenderPassDescriptor(d.oneface_rp); + d.oneface_rt[face]->build(); + d.releasePool << d.oneface_rt[face]; + } + + d.oneface_srb = rhi->newShaderResourceBindings(); + const QRhiShaderResourceBinding::StageFlags visibility = + QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage; + d.oneface_srb->setBindings({ + QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, visibility, d.oneface_ubuf, 64 + 12) + }); + d.oneface_srb->build(); + d.releasePool << d.oneface_srb; + + d.oneface_ps = rhi->newGraphicsPipeline(); + d.oneface_ps->setShaderStages({ + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/cubemap_oneface.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/cubemap_oneface.frag.qsb")) } + }); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 2 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float2, 0 }, + }); + d.oneface_ps->setVertexInputLayout(inputLayout); + d.oneface_ps->setShaderResourceBindings(d.oneface_srb); + d.oneface_ps->setRenderPassDescriptor(d.oneface_rp); + d.oneface_ps->build(); + d.releasePool << d.oneface_ps; + + // wasteful to duplicate the mvp as well but will do for now + for (int face = 0; face < 6; ++face) { + const int offset = d.ubufSizePerFace * face; + QMatrix4x4 identity; + d.initialUpdates->updateDynamicBuffer(d.oneface_ubuf, offset, 64, identity.constData()); + // will use a different color for each face + QColor c; + switch (face) { + case 0: + c = Qt::red; + break; + case 1: + c = Qt::green; + break; + case 2: + c = Qt::blue; + break; + case 3: + c = Qt::yellow; + break; + case 4: + c = Qt::lightGray; + break; + case 5: + c = Qt::cyan; + break; + } + float color[] = { float(c.redF()), float(c.greenF()), float(c.blueF()) }; + d.initialUpdates->updateDynamicBuffer(d.oneface_ubuf, offset + 64, 12, color); + } +} + +// 6 render passes, 1 draw call each, targeting one cubemap face at a time +void renderPerFace(QRhiCommandBuffer *cb) +{ + for (int face = 0; face < 6; ++face) { + cb->beginPass(d.oneface_rt[face], Qt::black, { 1.0f, 0 }); + cb->setGraphicsPipeline(d.oneface_ps); + cb->setViewport({ 0, 0, + float(d.oneface_rt[face]->pixelSize().width()), + float(d.oneface_rt[face]->pixelSize().height()) }); + const QRhiCommandBuffer::DynamicOffset dynamicOffset(0, face * d.ubufSizePerFace); + cb->setShaderResources(nullptr, 1, &dynamicOffset); + QRhiCommandBuffer::VertexInput vbufBinding(d.half_quad_vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding, d.half_quad_ibuf, 0, QRhiCommandBuffer::IndexUInt16); + cb->drawIndexed(6); + cb->endPass(); + } +} + +void initializeMrtRendering(QRhi *rhi) +{ + d.cubemap2 = rhi->newTexture(QRhiTexture::RGBA8, cubemapSize, 1, QRhiTexture::CubeMap | QRhiTexture::RenderTarget); + d.cubemap2->build(); + d.releasePool << d.cubemap2; + + d.mrt_ubuf = rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 6 * 16); // note that vec3 is aligned to 16 bytes + d.mrt_ubuf->build(); + d.releasePool << d.mrt_ubuf; + + QVarLengthArray attachments; + for (int face = 0; face < 6; ++face) { + QRhiColorAttachment att(d.cubemap2); + att.setLayer(face); + attachments.append(att); + } + QRhiTextureRenderTargetDescription rtDesc; + rtDesc.setColorAttachments(attachments.cbegin(), attachments.cend()); + d.mrt_rt = rhi->newTextureRenderTarget(rtDesc); + d.mrt_rp = d.mrt_rt->newCompatibleRenderPassDescriptor(); + d.releasePool << d.mrt_rp; + d.mrt_rt->setRenderPassDescriptor(d.mrt_rp); + d.mrt_rt->build(); + d.releasePool << d.mrt_rt; + + d.mrt_srb = rhi->newShaderResourceBindings(); + const QRhiShaderResourceBinding::StageFlags visibility = + QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage; + d.mrt_srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, visibility, d.mrt_ubuf) + }); + d.mrt_srb->build(); + d.releasePool << d.mrt_srb; + + d.mrt_ps = rhi->newGraphicsPipeline(); + d.mrt_ps->setShaderStages({ + { QRhiShaderStage::Vertex, getShader(QLatin1String(":/cubemap_mrt.vert.qsb")) }, + { QRhiShaderStage::Fragment, getShader(QLatin1String(":/cubemap_mrt.frag.qsb")) } + }); + QVarLengthArray targetBlends; + for (int face = 0; face < 6; ++face) + targetBlends.append({}); // default to blend = false, color write = all, which is good + d.mrt_ps->setTargetBlends(targetBlends.cbegin(), targetBlends.cend()); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 2 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float2, 0 }, + }); + d.mrt_ps->setVertexInputLayout(inputLayout); + d.mrt_ps->setShaderResourceBindings(d.mrt_srb); + d.mrt_ps->setRenderPassDescriptor(d.mrt_rp); + d.mrt_ps->build(); + d.releasePool << d.mrt_ps; + + QMatrix4x4 identity; + d.initialUpdates->updateDynamicBuffer(d.mrt_ubuf, 0, 64, identity.constData()); + for (int face = 0; face < 6; ++face) { + const int offset = 64 + face * 16; + // will use a different color for each face + QColor c; + switch (face) { + case 0: + c = Qt::red; + break; + case 1: + c = Qt::green; + break; + case 2: + c = Qt::blue; + break; + case 3: + c = Qt::yellow; + break; + case 4: + c = Qt::lightGray; + break; + case 5: + c = Qt::cyan; + break; + } + float color[] = { float(c.redF()), float(c.greenF()), float(c.blueF()) }; + d.initialUpdates->updateDynamicBuffer(d.mrt_ubuf, offset, 12, color); + } +} + +// 1 render pass, 1 draw call, with all 6 faces attached and written to +void renderWithMrt(QRhiCommandBuffer *cb) +{ + // use a different clear color to differentiate from cubemap1 (because the + // results are expected to be identical otherwise) + cb->beginPass(d.mrt_rt, Qt::magenta, { 1.0f, 0 }); + cb->setGraphicsPipeline(d.mrt_ps); + cb->setViewport({ 0, 0, + float(d.mrt_rt->pixelSize().width()), + float(d.mrt_rt->pixelSize().height()) }); + cb->setShaderResources(); + QRhiCommandBuffer::VertexInput vbufBinding(d.half_quad_vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding, d.half_quad_ibuf, 0, QRhiCommandBuffer::IndexUInt16); + cb->drawIndexed(6); + cb->endPass(); +} + +void Window::customInit() +{ + d.half_quad_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(halfQuadVertexData)); + d.half_quad_vbuf->build(); + d.releasePool << d.half_quad_vbuf; + + d.half_quad_ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(halfQuadIndexData)); + d.half_quad_ibuf->build(); + d.releasePool << d.half_quad_ibuf; + + d.initialUpdates = m_r->nextResourceUpdateBatch(); + d.initialUpdates->uploadStaticBuffer(d.half_quad_vbuf, 0, sizeof(halfQuadVertexData), halfQuadVertexData); + d.initialUpdates->uploadStaticBuffer(d.half_quad_ibuf, halfQuadIndexData); + + initializePerFaceRendering(m_r); + + d.canDoMrt = m_r->resourceLimit(QRhi::MaxColorAttachments) >= 6; + if (d.canDoMrt) + initializeMrtRendering(m_r); + else + qWarning("Not enough color attachments (need 6, supports %d)", m_r->resourceLimit(QRhi::MaxColorAttachments)); + + + // onscreen stuff + d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); + d.vbuf->build(); + d.releasePool << d.vbuf; + d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); + + d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64); + d.ubuf->build(); + d.releasePool << d.ubuf; + + d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, + QRhiSampler::Repeat, QRhiSampler::Repeat); + d.sampler->build(); + d.releasePool << d.sampler; + + d.srb = m_r->newShaderResourceBindings(); + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.cubemap1, d.sampler) + }); + d.srb->build(); + d.releasePool << d.srb; + + d.ps = m_r->newGraphicsPipeline(); + d.ps->setDepthTest(true); + d.ps->setDepthWrite(true); + d.ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual); + d.ps->setCullMode(QRhiGraphicsPipeline::Front); // we are inside the cube so cull front, not back + d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); // front is ccw in the cube data + QShader vs = getShader(QLatin1String(":/cubemap_sample.vert.qsb")); + Q_ASSERT(vs.isValid()); + QShader fs = getShader(QLatin1String(":/cubemap_sample.frag.qsb")); + Q_ASSERT(fs.isValid()); + d.ps->setShaderStages({ + { QRhiShaderStage::Vertex, vs }, + { QRhiShaderStage::Fragment, fs } + }); + QRhiVertexInputLayout inputLayout; + inputLayout.setBindings({ + { 3 * sizeof(float) } + }); + inputLayout.setAttributes({ + { 0, 0, QRhiVertexInputAttribute::Float3, 0 } + }); + d.ps->setVertexInputLayout(inputLayout); + d.ps->setShaderResourceBindings(d.srb); + d.ps->setRenderPassDescriptor(m_rp); + d.ps->build(); + d.releasePool << d.ps; + + if (d.canDoMrt) + qDebug("Use the arrow keys to switch between the two generated cubemaps"); +} + +void Window::customRelease() +{ + qDeleteAll(d.releasePool); + d.releasePool.clear(); +} + +void Window::customRender() +{ + const QSize outputSizeInPixels = m_sc->currentPixelSize(); + QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); + QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); + if (d.initialUpdates) { + u->merge(d.initialUpdates); + d.initialUpdates->release(); + d.initialUpdates = nullptr; + } + + QMatrix4x4 mvp = m_r->clipSpaceCorrMatrix(); + mvp.perspective(90.0f, outputSizeInPixels.width() / (float) outputSizeInPixels.height(), 0.01f, 1000.0f); + mvp.scale(10); + mvp.rotate(d.rx, 1, 0, 0); + d.rx += 0.5f; + u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); + + cb->resourceUpdate(u); + + renderPerFace(cb); + + if (d.canDoMrt) + renderWithMrt(cb); + + cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); + cb->setGraphicsPipeline(d.ps); + cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height())); + cb->setShaderResources(); + const QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); + cb->setVertexInput(0, 1, &vbufBinding); + cb->draw(36); + cb->endPass(); +} + +void Window::keyPressEvent(QKeyEvent *e) +{ + switch (e->key()) { + case Qt::Key_Left: + case Qt::Key_Up: + qDebug("Showing first cubemap (generated by rendering to the faces one by one; black background)"); + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.cubemap1, d.sampler) + }); + d.srb->build(); + break; + case Qt::Key_Right: + case Qt::Key_Down: + if (d.canDoMrt) { + qDebug("Showing second cubemap (generated with multiple render targets; magenta background)"); + d.srb->setBindings({ + QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), + QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.cubemap2, d.sampler) + }); + d.srb->build(); + } + break; + default: + e->ignore(); + break; + } +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_render.pro b/tests/manual/rhi/cubemap_render/cubemap_render.pro new file mode 100644 index 0000000000..71208cc01f --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_render.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +QT += gui-private + +SOURCES = \ + cubemap_render.cpp + +RESOURCES = cubemap_render.qrc diff --git a/tests/manual/rhi/cubemap_render/cubemap_render.qrc b/tests/manual/rhi/cubemap_render/cubemap_render.qrc new file mode 100644 index 0000000000..f6029d3f3a --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_render.qrc @@ -0,0 +1,10 @@ + + + cubemap_oneface.vert.qsb + cubemap_oneface.frag.qsb + cubemap_mrt.vert.qsb + cubemap_mrt.frag.qsb + cubemap_sample.vert.qsb + cubemap_sample.frag.qsb + + diff --git a/tests/manual/rhi/cubemap_render/cubemap_sample.frag b/tests/manual/rhi/cubemap_render/cubemap_sample.frag new file mode 100644 index 0000000000..13a365ed0c --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_sample.frag @@ -0,0 +1,10 @@ +#version 440 + +layout(location = 0) in vec3 v_coord; +layout(location = 0) out vec4 fragColor; +layout(binding = 1) uniform samplerCube tex; + +void main() +{ + fragColor = vec4(texture(tex, v_coord).rgb, 1.0); +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_sample.frag.qsb b/tests/manual/rhi/cubemap_render/cubemap_sample.frag.qsb new file mode 100644 index 0000000000..f26e3985b6 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_sample.frag.qsb differ diff --git a/tests/manual/rhi/cubemap_render/cubemap_sample.vert b/tests/manual/rhi/cubemap_render/cubemap_sample.vert new file mode 100644 index 0000000000..f1caf1a74f --- /dev/null +++ b/tests/manual/rhi/cubemap_render/cubemap_sample.vert @@ -0,0 +1,16 @@ +#version 440 + +layout(location = 0) in vec4 position; +layout(location = 0) out vec3 v_coord; + +layout(std140, binding = 0) uniform buf { + mat4 mvp; +} ubuf; + +out gl_PerVertex { vec4 gl_Position; }; + +void main() +{ + v_coord = position.xyz; + gl_Position = ubuf.mvp * position; +} diff --git a/tests/manual/rhi/cubemap_render/cubemap_sample.vert.qsb b/tests/manual/rhi/cubemap_render/cubemap_sample.vert.qsb new file mode 100644 index 0000000000..0cbc144011 Binary files /dev/null and b/tests/manual/rhi/cubemap_render/cubemap_sample.vert.qsb differ diff --git a/tests/manual/rhi/rhi.pro b/tests/manual/rhi/rhi.pro index 5208e5dea5..4bee164969 100644 --- a/tests/manual/rhi/rhi.pro +++ b/tests/manual/rhi/rhi.pro @@ -9,6 +9,7 @@ SUBDIRS += \ msaarenderbuffer \ cubemap \ cubemap_scissor \ + cubemap_render \ multiwindow \ multiwindow_threaded \ triquadcube \ diff --git a/tests/manual/rhi/shared/examplefw.h b/tests/manual/rhi/shared/examplefw.h index dc388274d7..84895cf530 100644 --- a/tests/manual/rhi/shared/examplefw.h +++ b/tests/manual/rhi/shared/examplefw.h @@ -148,6 +148,9 @@ protected: void exposeEvent(QExposeEvent *) override; bool event(QEvent *) override; +#ifdef EXAMPLEFW_KEYPRESS_EVENTS + void keyPressEvent(QKeyEvent *e) override; +#endif bool m_running = false; bool m_notExposed = false; -- cgit v1.2.3