| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#define EXAMPLEFW_IMGUI |
| 5 |
#include "../shared/examplefw.h" |
| 6 |
#include "../shared/cube.h" |
| 7 |
|
| 8 |
// Another tessellation test. Compatible with Direct 3D via hand-written hull |
| 9 |
// and domain shaders, but this already pushes the limits of what is sensible |
| 10 |
// when it comes to injecting hand-written HLSL code to get tessellation |
| 11 |
// functional (cbuffer layout, resource registers all need to be figured out |
| 12 |
// manually and works only as long as the GLSL source is not changing, etc.). |
| 13 |
// Note that the domain shader must use SampleLevel (textureLod), it won't |
| 14 |
// compile for ds_5_0 otherwise. |
| 15 |
|
| 16 |
static const quint32 UBUF_SIZE = 80; |
| 17 |
|
| 18 |
struct { |
| 19 |
QList<QRhiResource *> releasePool; |
| 20 |
|
| 21 |
QRhiBuffer *vbuf; |
| 22 |
QRhiBuffer *ubuf; |
| 23 |
QRhiTexture *tex; |
| 24 |
QRhiSampler *sampler; |
| 25 |
QRhiShaderResourceBindings *srb; |
| 26 |
QRhiGraphicsPipeline *psWire; |
| 27 |
QRhiGraphicsPipeline *psSolid; |
| 28 |
bool rotate = true; |
| 29 |
float rotation = 0.0f; |
| 30 |
float viewZ = 0.0f; |
| 31 |
float displacementAmount = 0.0f; |
| 32 |
int tessInner = 4; |
| 33 |
int tessOuter = 4; |
| 34 |
bool useTex = false; |
| 35 |
bool wireframe = true; |
| 36 |
|
| 37 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 38 |
} d; |
| 39 |
|
| 40 |
void Window::customInit() |
| 41 |
{ |
| 42 |
if (!m_r->isFeatureSupported(QRhi::Tessellation)) |
| 43 |
qFatal("Tessellation is not supported"); |
| 44 |
|
| 45 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 46 |
|
| 47 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 48 |
d.vbuf->create(); |
| 49 |
d.releasePool << d.vbuf; |
| 50 |
|
| 51 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); |
| 52 |
|
| 53 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, m_r->ubufAligned(UBUF_SIZE)); |
| 54 |
d.ubuf->create(); |
| 55 |
d.releasePool << d.ubuf; |
| 56 |
|
| 57 |
QImage image; |
| 58 |
image.load(":/heightmap.png"); |
| 59 |
if (image.isNull()) |
| 60 |
qFatal("Failed to load displacement map"); |
| 61 |
|
| 62 |
d.tex = m_r->newTexture(QRhiTexture::RGBA8, image.size()); |
| 63 |
d.tex->create(); |
| 64 |
d.releasePool << d.tex; |
| 65 |
|
| 66 |
d.initialUpdates->uploadTexture(d.tex, image); |
| 67 |
|
| 68 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 69 |
QRhiSampler::Repeat, QRhiSampler::Repeat); |
| 70 |
d.releasePool << d.sampler; |
| 71 |
d.sampler->create(); |
| 72 |
|
| 73 |
d.srb = m_r->newShaderResourceBindings(); |
| 74 |
d.releasePool << d.srb; |
| 75 |
d.srb->setBindings({ |
| 76 |
QRhiShaderResourceBinding::uniformBuffer(0, |
| 77 |
QRhiShaderResourceBinding::TessellationControlStage |
| 78 |
| QRhiShaderResourceBinding::TessellationEvaluationStage, |
| 79 |
d.ubuf), |
| 80 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::TessellationEvaluationStage, d.tex, d.sampler) |
| 81 |
}); |
| 82 |
d.srb->create(); |
| 83 |
|
| 84 |
QRhiVertexInputLayout inputLayout; |
| 85 |
inputLayout.setBindings({ |
| 86 |
{ 3 * sizeof(float) }, |
| 87 |
{ 2 * sizeof(float) }, |
| 88 |
{ 3 * sizeof(float) } |
| 89 |
}); |
| 90 |
inputLayout.setAttributes({ |
| 91 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 92 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 }, |
| 93 |
{ 2, 2, QRhiVertexInputAttribute::Float3, 0 } |
| 94 |
}); |
| 95 |
|
| 96 |
const QRhiShaderStage stages[] = { |
| 97 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/material.vert.qsb")) }, |
| 98 |
{ QRhiShaderStage::TessellationControl, getShader(QLatin1String(":/material.tesc.qsb")) }, |
| 99 |
{ QRhiShaderStage::TessellationEvaluation, getShader(QLatin1String(":/material.tese.qsb")) }, |
| 100 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/material.frag.qsb")) } |
| 101 |
}; |
| 102 |
|
| 103 |
d.psWire = m_r->newGraphicsPipeline(); |
| 104 |
d.releasePool << d.psWire; |
| 105 |
d.psWire->setTopology(QRhiGraphicsPipeline::Patches); |
| 106 |
d.psWire->setPatchControlPointCount(3); |
| 107 |
d.psWire->setShaderStages(stages, stages + 4); |
| 108 |
d.psWire->setDepthTest(true); |
| 109 |
d.psWire->setDepthWrite(true); |
| 110 |
d.psWire->setCullMode(QRhiGraphicsPipeline::Back); |
| 111 |
d.psWire->setPolygonMode(QRhiGraphicsPipeline::Line); |
| 112 |
d.psWire->setVertexInputLayout(inputLayout); |
| 113 |
d.psWire->setShaderResourceBindings(d.srb); |
| 114 |
d.psWire->setRenderPassDescriptor(m_rp); |
| 115 |
d.psWire->create(); |
| 116 |
|
| 117 |
d.psSolid = m_r->newGraphicsPipeline(); |
| 118 |
d.releasePool << d.psSolid; |
| 119 |
d.psSolid->setTopology(QRhiGraphicsPipeline::Patches); |
| 120 |
d.psSolid->setPatchControlPointCount(3); |
| 121 |
d.psSolid->setShaderStages(stages, stages + 4); |
| 122 |
d.psSolid->setDepthTest(true); |
| 123 |
d.psSolid->setDepthWrite(true); |
| 124 |
d.psSolid->setCullMode(QRhiGraphicsPipeline::Back); |
| 125 |
d.psSolid->setVertexInputLayout(inputLayout); |
| 126 |
d.psSolid->setShaderResourceBindings(d.srb); |
| 127 |
d.psSolid->setRenderPassDescriptor(m_rp); |
| 128 |
d.psSolid->create(); |
| 129 |
} |
| 130 |
|
| 131 |
void Window::customRelease() |
| 132 |
{ |
| 133 |
qDeleteAll(d.releasePool); |
| 134 |
d.releasePool.clear(); |
| 135 |
} |
| 136 |
|
| 137 |
void Window::customRender() |
| 138 |
{ |
| 139 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 140 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 141 |
QRhiResourceUpdateBatch *u = nullptr; |
| 142 |
if (d.initialUpdates) { |
| 143 |
u = d.initialUpdates; |
| 144 |
d.initialUpdates = nullptr; |
| 145 |
} |
| 146 |
|
| 147 |
char *p = d.ubuf->beginFullDynamicBufferUpdateForCurrentFrame(); |
| 148 |
QMatrix4x4 mvp = m_proj; |
| 149 |
mvp.translate(0, 0, d.viewZ); |
| 150 |
mvp.rotate(d.rotation, 1, 1, 0); |
| 151 |
mvp.scale(0.5f); |
| 152 |
|
| 153 |
memcpy(p, mvp.constData(), 64); |
| 154 |
memcpy(p + 64, &d.displacementAmount, sizeof(float)); |
| 155 |
float tessInnerFloat = d.tessInner; |
| 156 |
memcpy(p + 68, &tessInnerFloat, sizeof(float)); |
| 157 |
float tessOuterFloat = d.tessOuter; |
| 158 |
memcpy(p + 72, &tessOuterFloat, sizeof(float)); |
| 159 |
qint32 useTex = d.useTex ? 1 : 0; |
| 160 |
memcpy(p + 76, &useTex, sizeof(qint32)); |
| 161 |
|
| 162 |
d.ubuf->endFullDynamicBufferUpdateForCurrentFrame(); |
| 163 |
|
| 164 |
const QRhiCommandBuffer::VertexInput vbufBinding[] = { |
| 165 |
{ d.vbuf, 0 }, |
| 166 |
{ d.vbuf, quint32(36 * 3 * sizeof(float)) }, |
| 167 |
{ d.vbuf, quint32(36 * (3 + 2) * sizeof(float)) } |
| 168 |
}; |
| 169 |
|
| 170 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u, QRhiCommandBuffer::DoNotTrackResourcesForCompute); |
| 171 |
|
| 172 |
cb->setGraphicsPipeline(d.wireframe ? d.psWire : d.psSolid); |
| 173 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 174 |
cb->setShaderResources(d.srb); |
| 175 |
cb->setVertexInput(0, 3, vbufBinding); |
| 176 |
cb->draw(36); |
| 177 |
|
| 178 |
m_imguiRenderer->render(); |
| 179 |
|
| 180 |
cb->endPass(); |
| 181 |
|
| 182 |
if (d.rotate) |
| 183 |
d.rotation += 1; |
| 184 |
} |
| 185 |
|
| 186 |
void Window::customGui() |
| 187 |
{ |
| 188 |
ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_FirstUseEver); |
| 189 |
ImGui::SetNextWindowSize(ImVec2(500, 250), ImGuiCond_FirstUseEver); |
| 190 |
ImGui::Begin("Test"); |
| 191 |
ImGui::SliderInt("Inner", &d.tessInner, 0, 20); |
| 192 |
ImGui::SliderInt("Outer", &d.tessOuter, 0, 20); |
| 193 |
ImGui::SliderFloat("Displacement", &d.displacementAmount, 0.0f, 4.0f); |
| 194 |
ImGui::Checkbox("Use displacement texture", &d.useTex); |
| 195 |
ImGui::SliderFloat("Z", &d.viewZ, -16.0f, 4.0f); |
| 196 |
ImGui::Checkbox("Rotate", &d.rotate); |
| 197 |
ImGui::Checkbox("Wireframe", &d.wireframe); |
| 198 |
ImGui::End(); |
| 199 |
} |