Commit f386a4fd authored by Laszlo Agocs's avatar Laszlo Agocs

Add dest.pos. to texture uploads

parent 4839bfb4
......@@ -3,6 +3,7 @@ TEMPLATE = subdirs
SUBDIRS += \
hellominimalcrossgfxtriangle \
compressedtexture_bc1 \
texuploads \
plainqwindow_gles2 \
offscreen_gles2
......
......@@ -431,6 +431,9 @@ int main(int argc, char **argv)
if (cmdLineParser.isSet(mtlOption))
graphicsApi = Metal;
qDebug("Selected graphics API is %s", qPrintable(graphicsApiName()));
qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText()));
// OpenGL specifics.
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
......
/****************************************************************************
**
** Copyright (C) 2018 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$
**
****************************************************************************/
#include "../shared/examplefw.h"
#include "../shared/cube.h"
#include <QPainter>
struct {
QRhiBuffer *vbuf = nullptr;
QRhiBuffer *ubuf = nullptr;
QRhiTexture *tex = nullptr;
QRhiSampler *sampler = nullptr;
QRhiShaderResourceBindings *srb = nullptr;
QRhiGraphicsPipeline *ps = nullptr;
float rotation = 0;
QRhiResourceUpdateBatch *initialUpdates = nullptr;
int frameCount = 0;
QImage customImage;
} d;
void Window::customInit()
{
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
d.vbuf->build();
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
d.ubuf->build();
QImage baseImage(QLatin1String(":/qt256.png"));
d.tex = m_r->newTexture(QRhiTexture::RGBA8, baseImage.size());
d.tex->build();
// As an alternative to what some of the other examples do, prepare an
// update batch right here instead of relying on vbufReady and similar flags.
d.initialUpdates = m_r->nextResourceUpdateBatch();
d.initialUpdates->uploadStaticBuffer(d.vbuf, cube);
qint32 flip = 0;
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
d.initialUpdates->uploadTexture(d.tex, baseImage);
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge);
d.sampler->build();
d.srb = m_r->newShaderResourceBindings();
d.srb->setBindings({
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf),
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler)
});
d.srb->build();
d.ps = m_r->newGraphicsPipeline();
d.ps->setDepthTest(true);
d.ps->setDepthWrite(true);
d.ps->setDepthOp(QRhiGraphicsPipeline::Less);
d.ps->setCullMode(QRhiGraphicsPipeline::Back);
d.ps->setFrontFace(QRhiGraphicsPipeline::CCW);
const QBakedShader vs = getShader(QLatin1String(":/texture.vert.qsb"));
if (!vs.isValid())
qFatal("Failed to load shader pack (vertex)");
const QBakedShader fs = getShader(QLatin1String(":/texture.frag.qsb"));
if (!fs.isValid())
qFatal("Failed to load shader pack (fragment)");
d.ps->setShaderStages({
{ QRhiGraphicsShaderStage::Vertex, vs },
{ QRhiGraphicsShaderStage::Fragment, fs }
});
QRhiVertexInputLayout inputLayout;
inputLayout.bindings = {
{ 3 * sizeof(float) },
{ 2 * sizeof(float) }
};
inputLayout.attributes = {
{ 0, 0, QRhiVertexInputLayout::Attribute::Float3, 0 },
{ 1, 1, QRhiVertexInputLayout::Attribute::Float2, 0 }
};
d.ps->setVertexInputLayout(inputLayout);
d.ps->setShaderResourceBindings(d.srb);
d.ps->setRenderPassDescriptor(m_rp);
d.ps->build();
d.customImage = QImage(128, 64, QImage::Format_RGBA8888);
d.customImage.fill(Qt::red);
QPainter painter(&d.customImage);
painter.drawText(5, 25, "Hello world");
painter.end();
}
void Window::customRelease()
{
if (d.ps) {
d.ps->releaseAndDestroy();
d.ps = nullptr;
}
if (d.srb) {
d.srb->releaseAndDestroy();
d.srb = nullptr;
}
if (d.ubuf) {
d.ubuf->releaseAndDestroy();
d.ubuf = nullptr;
}
if (d.vbuf) {
d.vbuf->releaseAndDestroy();
d.vbuf = nullptr;
}
if (d.sampler) {
d.sampler->releaseAndDestroy();
d.sampler = nullptr;
}
if (d.tex) {
d.tex->releaseAndDestroy();
d.tex = nullptr;
}
}
void Window::customRender()
{
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
// take the initial set of updates, if this is the first frame
if (d.initialUpdates) {
u->merge(d.initialUpdates);
d.initialUpdates->release();
d.initialUpdates = nullptr;
}
d.rotation += 1.0f;
QMatrix4x4 mvp = m_proj;
mvp.scale(0.5f);
mvp.rotate(d.rotation, 0, 1, 0);
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
// After some time, partially change the texture.
if (d.frameCount > 100) {
QRhiTextureUploadDescription desc;
QRhiTextureUploadDescription::Layer layer;
QRhiTextureUploadDescription::Layer::MipLevel mipDesc;
mipDesc.image = d.customImage;
// The image here is smaller than the original. Use a non-zero position
// to make it more interesting.
mipDesc.destinationTopLeft = QPointF(100, 20);
layer.mipImages.append(mipDesc);
desc.layers.append(layer);
u->uploadTexture(d.tex, desc);
}
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
const QSize outputSizeInPixels = m_sc->effectivePixelSize();
m_r->beginPass(m_sc->currentFrameRenderTarget(), cb, { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 }, u);
m_r->setGraphicsPipeline(cb, d.ps);
m_r->setViewport(cb, { 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
m_r->setVertexInput(cb, 0, { { d.vbuf, 0 }, { d.vbuf, 36 * 3 * sizeof(float) } });
m_r->draw(cb, 36);
m_r->endPass(cb);
d.frameCount += 1;
}
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
texuploads.cpp
RESOURCES = texuploads.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/texuploads
INSTALLS += target
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="qt256.png">../shared/qt256.png</file>
<file alias="texture.vert.qsb">../shared/texture.vert.qsb</file>
<file alias="texture.frag.qsb">../shared/texture.frag.qsb</file>
</qresource>
</RCC>
......@@ -267,13 +267,14 @@ struct Q_RHI_EXPORT QRhiTextureUploadDescription
MipLevel(const QByteArray &compressedData_) : compressedData(compressedData_) { }
QImage image;
QByteArray compressedData;
QPointF topLeft; // (0, 0) by default
QSizeF size; // empty = entire image
QPointF destinationTopLeft; // for non-compressed only. (0, 0) by default.
};
Layer() { }
Layer(const QVector<MipLevel> &mipImages_) : mipImages(mipImages_) { }
QVector<MipLevel> mipImages;
};
QRhiTextureUploadDescription() { }
QRhiTextureUploadDescription(const QVector<Layer> &layers_) : layers(layers_) { }
QVector<Layer> layers; // 6 layers for cubemaps, 1 otherwise
......
......@@ -743,12 +743,16 @@ void QRhiD3D11::commitResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
const QRhiTextureUploadDescription::Layer::MipLevel mipDesc(layerDesc.mipImages[level]);
UINT subres = D3D11CalcSubresource(level, layer, texD->mipLevelCount);
if (!mipDesc.image.isNull()) {
const float x = mipDesc.destinationTopLeft.x();
const float y = mipDesc.destinationTopLeft.y();
D3D11_BOX box;
box.left = box.top = box.front = 0;
box.left = x;
box.top = y;
box.front = 0;
// back, right, bottom are exclusive
box.right = x + mipDesc.image.width();
box.bottom = y + mipDesc.image.height();
box.back = 1;
box.right = mipDesc.image.width();
box.bottom = mipDesc.image.height();
context->UpdateSubresource(texD->tex, subres, &box,
mipDesc.image.constBits(), mipDesc.image.bytesPerLine(), 0);
} else if (!mipDesc.compressedData.isEmpty() && isCompressedFormat(texD->m_format)) {
......
......@@ -522,8 +522,10 @@ void QRhiGles2::commitResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
f->glBindTexture(targetBase + layer, texD->texture);
for (int level = 0, levelCount = layerDesc.mipImages.count(); level != levelCount; ++level) {
const QRhiTextureUploadDescription::Layer::MipLevel mipDesc(layerDesc.mipImages[level]);
const float x = mipDesc.destinationTopLeft.x();
const float y = mipDesc.destinationTopLeft.y();
f->glTexSubImage2D(targetBase + layer, level,
0, 0, mipDesc.image.width(), mipDesc.image.height(),
x, y, mipDesc.image.width(), mipDesc.image.height(),
texD->glformat, texD->gltype, mipDesc.image.constBits());
}
}
......
......@@ -2004,6 +2004,8 @@ void QRhiVulkan::commitResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdate
imageSizeBytes = mipDesc.image.sizeInBytes();
if (imageSizeBytes > 0) {
src = mipDesc.image.constBits();
copyInfo.imageOffset.x = mipDesc.destinationTopLeft.x();
copyInfo.imageOffset.y = mipDesc.destinationTopLeft.y();
copyInfo.imageExtent.width = mipDesc.image.width();
copyInfo.imageExtent.height = mipDesc.image.height();
copyInfos.append(copyInfo);
......
vk, d3d, gl, mtl: tex upload with rect
mtl: tex upload with pos
copyimage (color, with rect?, no resolve or transforms here)
mtl: rhi without a window, offscreen frame
mtl: readback (tex, backbuffer)
......@@ -55,6 +55,7 @@ dxc for d3d as an alternative to fxc?
hlsl -> dxc -> spirv -> spirv-cross hmmm...
+++ done
vk, d3d, gl: tex upload with pos
res upd batch combine
gl: offscreen frame, readback
d3d: readback
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment