| 1 |
// Copyright (C) 2021 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
#include <QImage> |
| 6 |
#include <QPainter> |
| 7 |
|
| 8 |
// Create a 3D texture of 256x256x3, where we will render to one of the slices |
| 9 |
// in a render pass, while providing data to two other slices in texture |
| 10 |
// uploads. Finally we use the texture to in the on-screen rendering: texture 3 |
| 11 |
// quads with the same volume texture, in each draw the shader will sample with |
| 12 |
// a w that selects slice 0, 1, or 2, respectively. |
| 13 |
|
| 14 |
const int TEXTURE_DEPTH = 3; |
| 15 |
|
| 16 |
// Tests mipmap generation and linear-mipmap filtering when true. Note: Be |
| 17 |
// aware what a mipmap chain for a 3D texture is and isn't, do not confuse with |
| 18 |
// 2D (array) textures. The expected result on-screen is the black-red (slice |
| 19 |
// 1) content mixed in to slice 0 and 2 on lower mip levels (resize the window |
| 20 |
// to a smaller size -> the effect becomes more apparent, with totally taking |
| 21 |
// over at small sizes) |
| 22 |
// See https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-textures-intro |
| 23 |
// |
| 24 |
const bool MIPMAP = true; |
| 25 |
|
| 26 |
static float quadVertexData[] = |
| 27 |
{ // Y up, CCW |
| 28 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 29 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 30 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 31 |
0.5f, 0.5f, 1.0f, 0.0f |
| 32 |
}; |
| 33 |
|
| 34 |
static quint16 quadIndexData[] = |
| 35 |
{ |
| 36 |
0, 1, 2, 0, 2, 3 |
| 37 |
}; |
| 38 |
|
| 39 |
struct { |
| 40 |
QList<QRhiResource *> releasePool; |
| 41 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 42 |
QRhiTexture *tex = nullptr; |
| 43 |
QRhiTextureRenderTarget *rt = nullptr; |
| 44 |
QRhiRenderPassDescriptor *rtRp = nullptr; |
| 45 |
QRhiBuffer *vbuf = nullptr; |
| 46 |
QRhiBuffer *ibuf = nullptr; |
| 47 |
QRhiBuffer *ubuf = nullptr; |
| 48 |
QRhiSampler *sampler = nullptr; |
| 49 |
QRhiGraphicsPipeline *ps = nullptr; |
| 50 |
QRhiShaderResourceBindings *srb = nullptr; |
| 51 |
QMatrix4x4 winProj; |
| 52 |
QVector3D slice1ClearColor; |
| 53 |
} d; |
| 54 |
|
| 55 |
void Window::customInit() |
| 56 |
{ |
| 57 |
if (!m_r->isFeatureSupported(QRhi::ThreeDimensionalTextures)) |
| 58 |
qWarning("3D textures are not supported"); |
| 59 |
|
| 60 |
QRhiTexture::Flags texFlags = QRhiTexture::RenderTarget; |
| 61 |
if (MIPMAP) |
| 62 |
texFlags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips; |
| 63 |
d.tex = m_r->newTexture(QRhiTexture::RGBA8, |
| 64 |
256, 256, TEXTURE_DEPTH, |
| 65 |
1, texFlags); |
| 66 |
d.releasePool << d.tex; |
| 67 |
d.tex->create(); |
| 68 |
|
| 69 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 70 |
|
| 71 |
QImage imgWhiteText(256, 256, QImage::Format_RGBA8888); |
| 72 |
imgWhiteText.fill(Qt::blue); |
| 73 |
QPainter p(&imgWhiteText); |
| 74 |
p.setPen(Qt::white); |
| 75 |
p.drawText(10, 10, "Slice 2:\nWhite text on blue background"); |
| 76 |
p.end(); |
| 77 |
|
| 78 |
// imgWhiteText -> slice 2 of the 3D texture |
| 79 |
QRhiTextureUploadEntry uploadSlice2(2, 0, QRhiTextureSubresourceUploadDescription(imgWhiteText)); |
| 80 |
d.initialUpdates->uploadTexture(d.tex, uploadSlice2); |
| 81 |
|
| 82 |
QImage imgYellowText(256, 256, QImage::Format_RGBA8888); |
| 83 |
imgYellowText.fill(Qt::green); |
| 84 |
p.begin(&imgYellowText); |
| 85 |
p.setPen(Qt::yellow); |
| 86 |
p.drawText(10, 10, "Slice 0: Yellow text on green background"); |
| 87 |
p.end(); |
| 88 |
|
| 89 |
// imgYellowText -> slice 0 of the 3D texture |
| 90 |
QRhiTextureUploadEntry uploadSlice0(0, 0, QRhiTextureSubresourceUploadDescription(imgYellowText)); |
| 91 |
d.initialUpdates->uploadTexture(d.tex, uploadSlice0); |
| 92 |
|
| 93 |
// slice 1 -> clear to some color in a render pass |
| 94 |
QRhiColorAttachment att(d.tex); |
| 95 |
att.setLayer(1); |
| 96 |
QRhiTextureRenderTargetDescription rtDesc(att); |
| 97 |
d.rt = m_r->newTextureRenderTarget(rtDesc); |
| 98 |
d.releasePool << d.rt; |
| 99 |
d.rtRp = d.rt->newCompatibleRenderPassDescriptor(); |
| 100 |
d.releasePool << d.rtRp; |
| 101 |
d.rt->setRenderPassDescriptor(d.rtRp); |
| 102 |
d.rt->create(); |
| 103 |
|
| 104 |
// set up resources needed to render a quad |
| 105 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData)); |
| 106 |
d.vbuf->create(); |
| 107 |
d.releasePool << d.vbuf; |
| 108 |
|
| 109 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData)); |
| 110 |
d.ibuf->create(); |
| 111 |
d.releasePool << d.ibuf; |
| 112 |
|
| 113 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData); |
| 114 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData); |
| 115 |
|
| 116 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, m_r->ubufAligned(72) * TEXTURE_DEPTH); |
| 117 |
d.ubuf->create(); |
| 118 |
d.releasePool << d.ubuf; |
| 119 |
|
| 120 |
for (int i = 0; i < TEXTURE_DEPTH; ++i) { |
| 121 |
qint32 flip = 0; |
| 122 |
if (i == 1) // this slice will be rendered to in a render pass so needs flipping with OpenGL when on screen |
| 123 |
flip = m_r->isYUpInFramebuffer() ? 1 : 0; |
| 124 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, m_r->ubufAligned(72) * i + 64, 4, &flip); |
| 125 |
qint32 idx = i; |
| 126 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, m_r->ubufAligned(72) * i + 68, 4, &idx); |
| 127 |
} |
| 128 |
|
| 129 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, MIPMAP ? QRhiSampler::Linear : QRhiSampler::None, |
| 130 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 131 |
d.releasePool << d.sampler; |
| 132 |
d.sampler->create(); |
| 133 |
|
| 134 |
d.srb = m_r->newShaderResourceBindings(); |
| 135 |
d.releasePool << d.srb; |
| 136 |
d.srb->setBindings({ |
| 137 |
QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, 72), |
| 138 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler) |
| 139 |
}); |
| 140 |
d.srb->create(); |
| 141 |
|
| 142 |
d.ps = m_r->newGraphicsPipeline(); |
| 143 |
d.releasePool << d.ps; |
| 144 |
d.ps->setShaderStages({ |
| 145 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture3d.vert.qsb")) }, |
| 146 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture3d.frag.qsb")) } |
| 147 |
}); |
| 148 |
QRhiVertexInputLayout inputLayout; |
| 149 |
inputLayout.setBindings({ |
| 150 |
{ 4 * sizeof(float) } |
| 151 |
}); |
| 152 |
inputLayout.setAttributes({ |
| 153 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 154 |
{ 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) } |
| 155 |
}); |
| 156 |
d.ps->setVertexInputLayout(inputLayout); |
| 157 |
d.ps->setShaderResourceBindings(d.srb); |
| 158 |
d.ps->setRenderPassDescriptor(m_rp); |
| 159 |
d.ps->create(); |
| 160 |
} |
| 161 |
|
| 162 |
void Window::customRelease() |
| 163 |
{ |
| 164 |
qDeleteAll(d.releasePool); |
| 165 |
d.releasePool.clear(); |
| 166 |
} |
| 167 |
|
| 168 |
void Window::customRender() |
| 169 |
{ |
| 170 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 171 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 172 |
if (d.initialUpdates) { |
| 173 |
u->merge(d.initialUpdates); |
| 174 |
d.initialUpdates->release(); |
| 175 |
d.initialUpdates = nullptr; |
| 176 |
} |
| 177 |
|
| 178 |
if (d.winProj != m_proj) { |
| 179 |
d.winProj = m_proj; |
| 180 |
for (int i = 0; i < TEXTURE_DEPTH; ++i) { |
| 181 |
QMatrix4x4 mvp = m_proj; |
| 182 |
switch (i) { |
| 183 |
case 0: |
| 184 |
mvp.translate(-2.0f, 0.0f, 0.0f); |
| 185 |
break; |
| 186 |
case 2: |
| 187 |
mvp.translate(2.0f, 0.0f, 0.0f); |
| 188 |
break; |
| 189 |
default: |
| 190 |
break; |
| 191 |
} |
| 192 |
u->updateDynamicBuffer(d.ubuf, m_r->ubufAligned(72) * i, 64, mvp.constData()); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
const QColor slice1ClearColor = QColor::fromRgbF(d.slice1ClearColor.x(), d.slice1ClearColor.y(), d.slice1ClearColor.z()); |
| 197 |
d.slice1ClearColor.setX(d.slice1ClearColor.x() + 0.01f); |
| 198 |
if (d.slice1ClearColor.x() > 1.0f) |
| 199 |
d.slice1ClearColor.setX(0.0f); |
| 200 |
cb->beginPass(d.rt, slice1ClearColor, { 1.0f, 0 }, u); |
| 201 |
cb->endPass(); |
| 202 |
|
| 203 |
// now all 3 slices have something, regenerate the mipmaps |
| 204 |
u = m_r->nextResourceUpdateBatch(); |
| 205 |
if (MIPMAP) |
| 206 |
u->generateMips(d.tex); |
| 207 |
|
| 208 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 209 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 210 |
cb->setGraphicsPipeline(d.ps); |
| 211 |
QRhiCommandBuffer::DynamicOffset dynOfs = { 0, 0 }; |
| 212 |
cb->setShaderResources(d.srb, 1, &dynOfs); |
| 213 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 214 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 215 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 216 |
cb->drawIndexed(6); |
| 217 |
|
| 218 |
dynOfs.second = m_r->ubufAligned(72); |
| 219 |
cb->setShaderResources(d.srb, 1, &dynOfs); |
| 220 |
cb->drawIndexed(6); |
| 221 |
|
| 222 |
dynOfs.second = m_r->ubufAligned(72) * 2; |
| 223 |
cb->setShaderResources(d.srb, 1, &dynOfs); |
| 224 |
cb->drawIndexed(6); |
| 225 |
|
| 226 |
cb->endPass(); |
| 227 |
} |