Commit 16f0753d authored by Laszlo Agocs's avatar Laszlo Agocs

gl: add support for offset in static buffer upload

parent f38f0bb6
......@@ -50,8 +50,9 @@
#include "../shared/examplefw.h"
// Renders into a non-multisample and then a multisample (4x) texture and then
// uses those textures to draw two quads.
// Renders into a non-multisample and a multisample (4x) texture, and then uses
// those textures to draw two quads. Note that this uses an MSAA sampler in the
// shader, not resolves. Not supported on the GL(ES) backend atm.
static float vertexData[] =
{ // Y up, CCW
......@@ -88,6 +89,9 @@ struct {
QRhiGraphicsPipeline *psRight = nullptr;
QRhiResourceUpdateBatch *initialUpdates = nullptr;
int rightOfs;
QMatrix4x4 winProj;
QMatrix4x4 triBaseMvp;
float triRot = 0;
QRhiShaderResourceBindings *triSrb = nullptr;
QRhiGraphicsPipeline *msaaTriPs = nullptr;
......@@ -99,6 +103,8 @@ struct {
QRhiRenderPassDescriptor *rtRp = nullptr;
} d;
//#define NO_MSAA
void Window::customInit()
{
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData) + sizeof(triangleData));
......@@ -113,7 +119,11 @@ void Window::customInit()
d.tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
d.tex->build();
#ifndef NO_MSAA
d.msaaTex = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 4, QRhiTexture::RenderTarget);
#else
d.msaaTex = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget);
#endif
d.msaaTex->build();
d.initialUpdates = m_r->nextResourceUpdateBatch();
......@@ -160,7 +170,11 @@ void Window::customInit()
d.psRight = m_r->newGraphicsPipeline();
d.psRight->setShaderStages({
{ QRhiGraphicsShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) },
#ifndef NO_MSAA
{ QRhiGraphicsShaderStage::Fragment, getShader(QLatin1String(":/texture_ms4.frag.qsb")) }
#else
{ QRhiGraphicsShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) }
#endif
});
d.psRight->setVertexInputLayout(d.psLeft->vertexInputLayout());
d.psRight->setShaderResourceBindings(d.srbRight);
......@@ -201,7 +215,11 @@ void Window::customInit()
d.triPs->setRenderPassDescriptor(d.rtRp);
d.triPs->build();
d.msaaTriPs = m_r->newGraphicsPipeline();
#ifndef NO_MSAA
d.msaaTriPs->setSampleCount(4);
#else
d.msaaTriPs->setSampleCount(1);
#endif
d.msaaTriPs->setShaderStages(d.triPs->shaderStages());
d.msaaTriPs->setVertexInputLayout(d.triPs->vertexInputLayout());
d.msaaTriPs->setShaderResourceBindings(d.triSrb);
......@@ -312,25 +330,37 @@ void Window::customRender()
d.initialUpdates = nullptr;
// onscreen ubuf
QMatrix4x4 mvp = m_proj; // aspect ratio is then wrong when resizing but oh well
mvp.scale(2);
mvp.translate(-0.8f, 0, 0);
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
qint32 flip = 0;
qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0;
u->updateDynamicBuffer(d.ubuf, 64, 4, &flip);
mvp.translate(1.6f, 0, 0);
u->updateDynamicBuffer(d.ubuf, d.rightOfs, 64, mvp.constData());
u->updateDynamicBuffer(d.ubuf, d.rightOfs + 64, 4, &flip);
// offscreen ubuf
mvp = m_r->clipSpaceCorrMatrix();
mvp.perspective(45.0f, d.msaaTex->pixelSize().width() / float(d.msaaTex->pixelSize().height()), 0.01f, 1000.0f);
mvp.translate(0, 0, -2);
u->updateDynamicBuffer(d.triUbuf, 0, 64, mvp.constData());
d.triBaseMvp = m_r->clipSpaceCorrMatrix();
d.triBaseMvp.perspective(45.0f, d.msaaTex->pixelSize().width() / float(d.msaaTex->pixelSize().height()), 0.01f, 1000.0f);
d.triBaseMvp.translate(0, 0, -2);
float opacity = 1.0f;
u->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity);
}
if (d.winProj != m_proj) {
// onscreen buf, window size dependent
d.winProj = m_proj;
QMatrix4x4 mvp = m_proj;
mvp.scale(2);
mvp.translate(-0.8f, 0, 0);
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
mvp.translate(1.6f, 0, 0);
u->updateDynamicBuffer(d.ubuf, d.rightOfs, 64, mvp.constData());
}
// offscreen buf, apply the rotation on every frame
QMatrix4x4 triMvp = d.triBaseMvp;
triMvp.rotate(d.triRot, 0, 1, 0);
d.triRot += 1;
u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData());
cb->resourceUpdate(u);
// offscreen
cb->beginPass(d.rt, { 0.5f, 0.2f, 0, 1 }, { 1, 0 });
cb->setGraphicsPipeline(d.triPs);
......@@ -340,7 +370,7 @@ void Window::customRender()
cb->endPass();
// offscreen msaa
cb->beginPass(d.msaaRt, { 0.5f, 0.2f, 0, 1 }, { 1, 0 }, u);
cb->beginPass(d.msaaRt, { 0.5f, 0.2f, 0, 1 }, { 1, 0 });
cb->setGraphicsPipeline(d.msaaTriPs);
cb->setViewport({ 0, 0, float(d.msaaTex->pixelSize().width()), float(d.msaaTex->pixelSize().height()) });
cb->setVertexInput(0, { { d.vbuf, sizeof(vertexData) } });
......
......@@ -551,19 +551,19 @@ void QRhiGles2::enqueueResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdate
for (const QRhiResourceUpdateBatchPrivate::StaticBufferUpload &u : ud->staticBufferUploads) {
QGles2Buffer *bufD = QRHI_RES(QGles2Buffer, u.buf);
Q_ASSERT(bufD->m_type != QRhiBuffer::Dynamic);
Q_ASSERT(u.data.size() == bufD->m_size);
Q_ASSERT(u.offset + u.data.size() <= bufD->m_size);
if (bufD->m_usage.testFlag(QRhiBuffer::UniformBuffer)) {
memcpy(bufD->ubuf.data() + u.offset, u.data.constData(), u.data.size());
bufD->ubufChangeRange = { 0, u.data.size() };
} else {
QGles2CommandBuffer::Command cmd;
cmd.cmd = QGles2CommandBuffer::Command::BufferData;
cmd.args.bufferData.target = bufD->target;
cmd.args.bufferData.buffer = bufD->buffer;
cmd.args.bufferData.size = u.data.size();
cmd.args.bufferData.data = cbD->retainData(u.data);
cmd.cmd = QGles2CommandBuffer::Command::BufferSubData;
cmd.args.bufferSubData.target = bufD->target;
cmd.args.bufferSubData.buffer = bufD->buffer;
cmd.args.bufferSubData.offset = u.offset;
cmd.args.bufferSubData.size = u.data.size();
cmd.args.bufferSubData.data = cbD->retainData(u.data);
cbD->commands.append(cmd);
// ### offset??
}
}
......@@ -1057,10 +1057,6 @@ void QRhiGles2::executeCommandBuffer(QRhiCommandBuffer *cb)
}
f->glClear(cmd.args.clear.mask);
break;
case QGles2CommandBuffer::Command::BufferData:
f->glBindBuffer(cmd.args.bufferData.target, cmd.args.bufferData.buffer);
f->glBufferData(cmd.args.bufferData.target, cmd.args.bufferData.size, cmd.args.bufferData.data, GL_STATIC_DRAW);
break;
case QGles2CommandBuffer::Command::BufferSubData:
f->glBindBuffer(cmd.args.bufferSubData.target, cmd.args.bufferSubData.buffer);
f->glBufferSubData(cmd.args.bufferSubData.target, cmd.args.bufferSubData.offset, cmd.args.bufferSubData.size,
......
......@@ -289,12 +289,6 @@ struct QGles2CommandBuffer : public QRhiCommandBuffer
struct {
QRhiTextureRenderTarget *rt;
} bindFramebuffer;
struct {
GLenum target;
GLuint buffer;
int size;
const void *data; // must come from retainData()
} bufferData;
struct {
GLenum target;
GLuint buffer;
......
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