| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
#include "../shared/examplefw.h" |
| 52 |
#include "../shared/cube.h" |
| 53 |
#include <QPainter> |
| 54 |
|
| 55 |
struct { |
| 56 |
QRhiBuffer *vbuf = nullptr; |
| 57 |
QRhiBuffer *ubuf = nullptr; |
| 58 |
QRhiTexture *tex = nullptr; |
| 59 |
QRhiSampler *sampler = nullptr; |
| 60 |
QRhiShaderResourceBindings *srb = nullptr; |
| 61 |
QRhiGraphicsPipeline *ps = nullptr; |
| 62 |
QVector<QRhiResource *> releasePool; |
| 63 |
|
| 64 |
float rotation = 0; |
| 65 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 66 |
int frameCount = 0; |
| 67 |
QImage customImage; |
| 68 |
QRhiTexture *newTex = nullptr; |
| 69 |
QRhiTexture *importedTex = nullptr; |
| 70 |
int testStage = 0; |
| 71 |
|
| 72 |
QRhiShaderResourceBinding bindings[2]; |
| 73 |
} d; |
| 74 |
|
| 75 |
void Window::customInit() |
| 76 |
{ |
| 77 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube)); |
| 78 |
d.releasePool << d.vbuf; |
| 79 |
d.vbuf->build(); |
| 80 |
|
| 81 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 82 |
d.releasePool << d.ubuf; |
| 83 |
d.ubuf->build(); |
| 84 |
|
| 85 |
QImage baseImage(QLatin1String(":/qt256.png")); |
| 86 |
d.tex = m_r->newTexture(QRhiTexture::RGBA8, baseImage.size(), 1, QRhiTexture::UsedAsTransferSource); |
| 87 |
d.releasePool << d.tex; |
| 88 |
d.tex->build(); |
| 89 |
|
| 90 |
// As an alternative to what some of the other examples do, prepare an |
| 91 |
// update batch right here instead of relying on vbufReady and similar flags. |
| 92 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 93 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube); |
| 94 |
qint32 flip = 0; |
| 95 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 96 |
d.initialUpdates->uploadTexture(d.tex, baseImage); |
| 97 |
|
| 98 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 99 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 100 |
d.releasePool << d.sampler; |
| 101 |
d.sampler->build(); |
| 102 |
|
| 103 |
d.srb = m_r->newShaderResourceBindings(); |
| 104 |
d.releasePool << d.srb; |
| 105 |
|
| 106 |
d.bindings[0] = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf); |
| 107 |
d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler); |
| 108 |
d.srb->setBindings(d.bindings, d.bindings + 2); |
| 109 |
d.srb->build(); |
| 110 |
|
| 111 |
d.ps = m_r->newGraphicsPipeline(); |
| 112 |
d.releasePool << d.ps; |
| 113 |
|
| 114 |
d.ps->setDepthTest(true); |
| 115 |
d.ps->setDepthWrite(true); |
| 116 |
d.ps->setDepthOp(QRhiGraphicsPipeline::Less); |
| 117 |
|
| 118 |
d.ps->setCullMode(QRhiGraphicsPipeline::Back); |
| 119 |
d.ps->setFrontFace(QRhiGraphicsPipeline::CCW); |
| 120 |
|
| 121 |
const QShader vs = getShader(QLatin1String(":/texture.vert.qsb")); |
| 122 |
if (!vs.isValid()) |
| 123 |
qFatal("Failed to load shader pack (vertex)"); |
| 124 |
const QShader fs = getShader(QLatin1String(":/texture.frag.qsb")); |
| 125 |
if (!fs.isValid()) |
| 126 |
qFatal("Failed to load shader pack (fragment)"); |
| 127 |
|
| 128 |
d.ps->setShaderStages({ |
| 129 |
{ QRhiShaderStage::Vertex, vs }, |
| 130 |
{ QRhiShaderStage::Fragment, fs } |
| 131 |
}); |
| 132 |
|
| 133 |
QRhiVertexInputLayout inputLayout; |
| 134 |
inputLayout.setBindings({ |
| 135 |
{ 3 * sizeof(float) }, |
| 136 |
{ 2 * sizeof(float) } |
| 137 |
}); |
| 138 |
inputLayout.setAttributes({ |
| 139 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 140 |
{ 1, 1, QRhiVertexInputAttribute::Float2, 0 } |
| 141 |
}); |
| 142 |
|
| 143 |
d.ps->setVertexInputLayout(inputLayout); |
| 144 |
d.ps->setShaderResourceBindings(d.srb); |
| 145 |
d.ps->setRenderPassDescriptor(m_rp); |
| 146 |
|
| 147 |
d.ps->build(); |
| 148 |
|
| 149 |
d.customImage = QImage(128, 64, QImage::Format_RGBA8888); |
| 150 |
d.customImage.fill(Qt::red); |
| 151 |
QPainter painter(&d.customImage); |
| 152 |
// the text may look different on different platforms, so no guarantee the |
| 153 |
// output on the screen will be identical everywhere |
| 154 |
painter.drawText(5, 25, "Hello world"); |
| 155 |
painter.end(); |
| 156 |
} |
| 157 |
|
| 158 |
void Window::customRelease() |
| 159 |
{ |
| 160 |
qDeleteAll(d.releasePool); |
| 161 |
d.releasePool.clear(); |
| 162 |
} |
| 163 |
|
| 164 |
void Window::customRender() |
| 165 |
{ |
| 166 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 167 |
|
| 168 |
// take the initial set of updates, if this is the first frame |
| 169 |
if (d.initialUpdates) { |
| 170 |
u->merge(d.initialUpdates); |
| 171 |
d.initialUpdates->release(); |
| 172 |
d.initialUpdates = nullptr; |
| 173 |
} |
| 174 |
|
| 175 |
d.rotation += 1.0f; |
| 176 |
QMatrix4x4 mvp = m_proj; |
| 177 |
mvp.scale(0.5f); |
| 178 |
mvp.rotate(d.rotation, 0, 1, 0); |
| 179 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 180 |
|
| 181 |
if (d.frameCount > 0 && (d.frameCount % 100) == 0) { |
| 182 |
d.testStage += 1; |
| 183 |
qDebug("testStage = %d", d.testStage); |
| 184 |
|
| 185 |
// Partially change the texture. |
| 186 |
if (d.testStage == 1) { |
| 187 |
QRhiTextureSubresourceUploadDescription mipDesc(d.customImage); |
| 188 |
// The image here is smaller than the original. Use a non-zero position |
| 189 |
// to make it more interesting. |
| 190 |
mipDesc.setDestinationTopLeft(QPoint(100, 20)); |
| 191 |
QRhiTextureUploadDescription desc({ 0, 0, mipDesc }); |
| 192 |
u->uploadTexture(d.tex, desc); |
| 193 |
} |
| 194 |
|
| 195 |
// Exercise image copying. |
| 196 |
if (d.testStage == 2) { |
| 197 |
const QSize sz = d.tex->pixelSize(); |
| 198 |
d.newTex = m_r->newTexture(QRhiTexture::RGBA8, sz); |
| 199 |
d.releasePool << d.newTex; |
| 200 |
d.newTex->build(); |
| 201 |
|
| 202 |
QImage empty(sz.width(), sz.height(), QImage::Format_RGBA8888); |
| 203 |
empty.fill(Qt::blue); |
| 204 |
u->uploadTexture(d.newTex, empty); |
| 205 |
|
| 206 |
// Copy the left-half of tex to the right-half of newTex, while |
| 207 |
// leaving the left-half of newTex blue. Keep a 20 pixel gap at |
| 208 |
// the top. |
| 209 |
QRhiTextureCopyDescription desc; |
| 210 |
desc.setSourceTopLeft(QPoint(0, 20)); |
| 211 |
desc.setPixelSize(QSize(sz.width() / 2, sz.height() - 20)); |
| 212 |
desc.setDestinationTopLeft(QPoint(sz.width() / 2, 20)); |
| 213 |
u->copyTexture(d.newTex, d.tex, desc); |
| 214 |
|
| 215 |
// Now replace d.tex with d.newTex as the shader resource. |
| 216 |
d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.newTex, d.sampler); |
| 217 |
d.srb->setBindings(d.bindings, d.bindings + 2); |
| 218 |
// "rebuild", whatever that means for a given backend. This srb is |
| 219 |
// already live as the ps in the setGraphicsPipeline references it, |
| 220 |
// but that's fine. Changes will be picked up automatically. |
| 221 |
d.srb->build(); |
| 222 |
} |
| 223 |
|
| 224 |
// Exercise simple, full texture copy. |
| 225 |
if (d.testStage == 4) |
| 226 |
u->copyTexture(d.newTex, d.tex); |
| 227 |
|
| 228 |
// Now again upload customImage but this time only a part of it. |
| 229 |
if (d.testStage == 5) { |
| 230 |
QRhiTextureSubresourceUploadDescription mipDesc(d.customImage); |
| 231 |
mipDesc.setDestinationTopLeft(QPoint(10, 120)); |
| 232 |
mipDesc.setSourceSize(QSize(50, 40)); |
| 233 |
mipDesc.setSourceTopLeft(QPoint(20, 10)); |
| 234 |
QRhiTextureUploadDescription desc({ 0, 0, mipDesc }); |
| 235 |
u->uploadTexture(d.newTex, desc); |
| 236 |
} |
| 237 |
|
| 238 |
// Exercise texture object export/import. |
| 239 |
if (d.testStage == 6) { |
| 240 |
const QRhiTexture::NativeTexture nativeTexture = d.tex->nativeTexture(); |
| 241 |
if (nativeTexture.object) { |
| 242 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 243 |
if (graphicsApi == Metal) { |
| 244 |
qDebug() << "Metal texture: " << *(void**)nativeTexture.object; |
| 245 |
// Now could cast to id<MTLTexture> and do something with |
| 246 |
// it, keeping in mind that copy operations are only done |
| 247 |
// in beginPass, while rendering into a texture may only |
| 248 |
// have proper results in current_frame + 2, or after a |
| 249 |
// finish(). The QRhiTexture still owns the native object. |
| 250 |
} |
| 251 |
#endif |
| 252 |
// omit for other backends, the idea is the same |
| 253 |
|
| 254 |
d.importedTex = m_r->newTexture(QRhiTexture::RGBA8, d.tex->pixelSize()); |
| 255 |
d.releasePool << d.importedTex; |
| 256 |
if (!d.importedTex->buildFrom(nativeTexture)) |
| 257 |
qWarning("Texture import failed"); |
| 258 |
|
| 259 |
// now d.tex and d.importedTex use the same MTLTexture |
| 260 |
// underneath (owned by d.tex) |
| 261 |
|
| 262 |
// switch to showing d.importedTex |
| 263 |
d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.importedTex, d.sampler); |
| 264 |
d.srb->setBindings(d.bindings, d.bindings + 2); |
| 265 |
d.srb->build(); |
| 266 |
} else { |
| 267 |
qWarning("Accessing native texture object is not supported"); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// Exercise uploading uncompressed data without a QImage. |
| 272 |
if (d.testStage == 7) { |
| 273 |
d.bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.newTex, d.sampler); |
| 274 |
d.srb->setBindings(d.bindings, d.bindings + 2); |
| 275 |
d.srb->build(); |
| 276 |
|
| 277 |
const QSize sz(221, 139); |
| 278 |
QByteArray data; |
| 279 |
data.resize(sz.width() * sz.height() * 4); |
| 280 |
for (int y = 0; y < sz.height(); ++y) { |
| 281 |
uchar *p = reinterpret_cast<uchar *>(data.data()) + y * sz.width() * 4; |
| 282 |
for (int x = 0; x < sz.width(); ++x) { |
| 283 |
*p++ = 0; *p++ = 0; *p++ = y * (255 / sz.height()); |
| 284 |
*p++ = 255; |
| 285 |
} |
| 286 |
} |
| 287 |
QRhiTextureSubresourceUploadDescription mipDesc(data.constData(), data.size()); |
| 288 |
mipDesc.setSourceSize(sz); |
| 289 |
mipDesc.setDestinationTopLeft(QPoint(5, 25)); |
| 290 |
QRhiTextureUploadDescription desc({ 0, 0, mipDesc }); |
| 291 |
u->uploadTexture(d.newTex, desc); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 296 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 297 |
|
| 298 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 299 |
|
| 300 |
cb->setGraphicsPipeline(d.ps); |
| 301 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 302 |
cb->setShaderResources(); |
| 303 |
const QRhiCommandBuffer::VertexInput vbufBindings[] = { |
| 304 |
{ d.vbuf, 0 }, |
| 305 |
{ d.vbuf, 36 * 3 * sizeof(float) } |
| 306 |
}; |
| 307 |
cb->setVertexInput(0, 2, vbufBindings); |
| 308 |
cb->draw(36); |
| 309 |
|
| 310 |
cb->endPass(); |
| 311 |
|
| 312 |
d.frameCount += 1; |
| 313 |
} |