Commit 6c868e83 authored by Laszlo Agocs's avatar Laszlo Agocs

gl: implement basic readback support

This is not as good as other backends when it comes to formats,
but the gl backend is lacking in that area anyways.
parent d98468e2
......@@ -198,7 +198,7 @@ int main(int argc, char **argv)
QString fn = QString::asprintf("frame%d.png", frame);
fn = QFileInfo(fn).absoluteFilePath();
qDebug("Saving into %s", qPrintable(fn));
image.save(fn);
image.mirrored().save(fn); // gl has isYUpInFramebuffer == true so mirror
} else {
qWarning("Readback failed!");
}
......
......@@ -269,7 +269,10 @@ void ExampleWindow::render()
QString fn = QString::asprintf("frame%d.png", frameNo);
fn = QFileInfo(fn).absoluteFilePath();
qDebug("Saving into %s", qPrintable(fn));
image.save(fn);
if (m_r->isYUpInFramebuffer())
image.mirrored().save(fn);
else
image.save(fn);
}
delete rbResult;
};
......
......@@ -444,13 +444,19 @@ QRhi::FrameOpResult QRhiGles2::endOffscreenFrame()
bool QRhiGles2::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
{
Q_UNUSED(cb);
Q_UNUSED(rb);
Q_UNUSED(result);
Q_ASSERT(inFrame && !inPass);
return false;
QGles2CommandBuffer *cbD = QRHI_RES(QGles2CommandBuffer, cb);
QGles2CommandBuffer::Command cmd;
cmd.cmd = QGles2CommandBuffer::Command::ReadPixels;
cmd.args.readPixels.tex = rb.texture;
cmd.args.readPixels.layer = rb.layer;
cmd.args.readPixels.level = rb.level;
cmd.args.readPixels.result = result;
cbD->commands.append(cmd);
return true;
}
QRhi::FrameOpResult QRhiGles2::finish()
......@@ -887,6 +893,35 @@ void QRhiGles2::executeCommandBuffer(QRhiCommandBuffer *cb)
}
f->glClear(cmd.args.clear.mask);
break;
case QGles2CommandBuffer::Command::ReadPixels:
{
// ### more formats, layer, level
QRhiReadbackResult *result = cmd.args.readPixels.result;
GLuint fbo = 0;
if (cmd.args.readPixels.tex) {
QGles2Texture *texD = QRHI_RES(QGles2Texture, cmd.args.readPixels.tex);
result->pixelSize = texD->m_pixelSize;
result->format = texD->m_format;
// this is going to be suboptimal but will do for now
f->glGenFramebuffers(1, &fbo);
f->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texD->target, texD->texture, 0);
} else {
result->pixelSize = currentSwapChain->pixelSize;
result->format = QRhiTexture::RGBA8;
}
result->data.resize(result->pixelSize.width() * result->pixelSize.height() * 4);
f->glReadPixels(0, 0, result->pixelSize.width(), result->pixelSize.height(),
GL_RGBA, GL_UNSIGNED_BYTE,
result->data.data());
if (fbo) {
f->glBindFramebuffer(GL_FRAMEBUFFER, ctx->defaultFramebufferObject());
f->glDeleteFramebuffers(1, &fbo);
}
if (result->completed)
result->completed();
}
break;
default:
break;
}
......
......@@ -210,7 +210,8 @@ struct QGles2CommandBuffer : public QRhiCommandBuffer
DrawIndexed,
BindGraphicsPipeline,
BindFramebuffer,
Clear
Clear,
ReadPixels
};
Cmd cmd;
union {
......@@ -263,6 +264,12 @@ struct QGles2CommandBuffer : public QRhiCommandBuffer
struct {
QRhiTextureRenderTarget *rt;
} bindFramebuffer;
struct {
QRhiTexture *tex;
int layer;
int level;
QRhiReadbackResult *result;
} readPixels;
} args;
};
......
gl, mtl: rhi without a window, offscreen frame
gl, mtl: readback (tex, backbuffer)
mtl: rhi without a window, offscreen frame
mtl: readback (tex, backbuffer)
gl, mtl: compressed textures
gl, mtl: srgb (tex, swapchain buf)
multi window? (multi swapchain) -> trouble
......@@ -10,6 +10,7 @@ mtl: cbuffers, textures, samplers set should be batched too
mtl: msaa (onscreen)
test cubemap
test cubemap face as target
face cubemap readback? (test vk/d3d, impl for gl)
cbuffer alignment rules - some things fail to translate (to hlsl e.g. with structs), which is fine but how to mitigate
resource import/export, what's the co-op story?
copy-only passes for kicking off transfers early? (copy/transfer queue?)
......@@ -28,7 +29,7 @@ d3d: support DxcCompiler (in addition to d3dcompiler?) when runtime compiling hl
gl: more ubuf types
gl: reduce state, reduce texparam
gl: tex size stuff (npot etc.)
gl: tex formats
gl: tex formats (texture, readback)
more QImage->tex formats
vk compressed tex: could it consume a complete ktx without any memcpys?
multi-buffer (region) readback?
......@@ -50,6 +51,7 @@ dxc for d3d as an alternative to fxc?
hlsl -> dxc -> spirv -> spirv-cross hmmm...
+++ done
gl: offscreen frame, readback
d3d: readback
d3d: offscreen frame
vk: read back the backbuffer
......
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