Commit e51c6f68 authored by Laszlo Agocs's avatar Laszlo Agocs

vk: Make readback size calculation more sophisticated

parent 13bd1f59
......@@ -181,6 +181,171 @@ QRhiImplementation::~QRhiImplementation()
qDeleteAll(resUpdPool);
}
bool QRhiImplementation::isCompressedFormat(QRhiTexture::Format format)
{
return (format >= QRhiTexture::BC1 && format <= QRhiTexture::BC7)
|| (format >= QRhiTexture::ETC2_RGB8 && format <= QRhiTexture::ETC2_RGBA8)
|| (format >= QRhiTexture::ASTC_4x4 && format <= QRhiTexture::ASTC_12x12);
}
void QRhiImplementation::compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
quint32 *bpl, quint32 *byteSize)
{
int xdim = 4;
int ydim = 4;
quint32 blockSize = 0;
switch (format) {
case QRhiTexture::BC1:
blockSize = 8;
break;
case QRhiTexture::BC2:
blockSize = 16;
break;
case QRhiTexture::BC3:
blockSize = 16;
break;
case QRhiTexture::BC4:
blockSize = 8;
break;
case QRhiTexture::BC5:
blockSize = 16;
break;
case QRhiTexture::BC6H:
blockSize = 16;
break;
case QRhiTexture::BC7:
blockSize = 16;
break;
case QRhiTexture::ETC2_RGB8:
blockSize = 8;
break;
case QRhiTexture::ETC2_RGB8A1:
blockSize = 8;
break;
case QRhiTexture::ETC2_RGBA8:
blockSize = 16;
break;
case QRhiTexture::ASTC_4x4:
blockSize = 16;
break;
case QRhiTexture::ASTC_5x4:
blockSize = 16;
xdim = 5;
break;
case QRhiTexture::ASTC_5x5:
blockSize = 16;
xdim = ydim = 5;
break;
case QRhiTexture::ASTC_6x5:
blockSize = 16;
xdim = 6;
ydim = 5;
break;
case QRhiTexture::ASTC_6x6:
blockSize = 16;
xdim = ydim = 6;
break;
case QRhiTexture::ASTC_8x5:
blockSize = 16;
xdim = 8;
ydim = 5;
break;
case QRhiTexture::ASTC_8x6:
blockSize = 16;
xdim = 8;
ydim = 6;
break;
case QRhiTexture::ASTC_8x8:
blockSize = 16;
xdim = ydim = 8;
break;
case QRhiTexture::ASTC_10x5:
blockSize = 16;
xdim = 10;
ydim = 5;
break;
case QRhiTexture::ASTC_10x6:
blockSize = 16;
xdim = 10;
ydim = 6;
break;
case QRhiTexture::ASTC_10x8:
blockSize = 16;
xdim = 10;
ydim = 8;
break;
case QRhiTexture::ASTC_10x10:
blockSize = 16;
xdim = ydim = 10;
break;
case QRhiTexture::ASTC_12x10:
blockSize = 16;
xdim = 12;
ydim = 10;
break;
case QRhiTexture::ASTC_12x12:
blockSize = 16;
xdim = ydim = 12;
break;
default:
Q_UNREACHABLE();
break;
}
const quint32 wblocks = (size.width() + xdim - 1) / xdim;
const quint32 hblocks = (size.height() + ydim - 1) / ydim;
if (bpl)
*bpl = wblocks * blockSize;
if (byteSize)
*byteSize = wblocks * hblocks * blockSize;
}
void QRhiImplementation::textureFormatInfo(QRhiTexture::Format format, const QSize &size,
quint32 *bpl, quint32 *byteSize)
{
if (isCompressedFormat(format)) {
compressedFormatInfo(format, size, bpl, byteSize);
return;
}
quint32 bpc = 0;
switch (format) {
case QRhiTexture::RGBA8:
bpc = 4;
break;
case QRhiTexture::BGRA8:
bpc = 4;
break;
case QRhiTexture::R8:
bpc = 1;
break;
case QRhiTexture::R16:
bpc = 2;
break;
case QRhiTexture::D16:
bpc = 2;
break;
case QRhiTexture::D32:
bpc = 4;
break;
default:
Q_UNREACHABLE();
break;
}
if (bpl)
*bpl = size.width() * bpc;
if (byteSize)
*byteSize = size.width() * size.height() * bpc;
}
QRhi::QRhi()
{
}
......
......@@ -121,8 +121,18 @@ public:
virtual QMatrix4x4 clipSpaceCorrMatrix() const = 0;
virtual bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const = 0;
protected:
bool isCompressedFormat(QRhiTexture::Format format);
void compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
quint32 *bpl, quint32 *byteSize);
void textureFormatInfo(QRhiTexture::Format format, const QSize &size,
quint32 *bpl, quint32 *byteSize);
QVector<QRhiResourceUpdateBatch *> resUpdPool;
QBitArray resUpdPoolMap;
friend class QRhi;
friend struct QRhiResourceUpdateBatchPrivate;
};
struct QRhiResourceUpdateBatchPrivate
......
......@@ -526,47 +526,6 @@ QRhi::FrameOpResult QRhiD3D11::finish()
return QRhi::FrameOpSuccess;
}
static inline bool isCompressedFormat(QRhiTexture::Format format)
{
return format >= QRhiTexture::BC1 && format <= QRhiTexture::BC7;
}
static void compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
quint32 *bpl, quint32 *alignedHeight)
{
quint32 blockSize = 0;
switch (format) {
case QRhiTexture::BC1:
blockSize = 8;
break;
case QRhiTexture::BC2:
blockSize = 16;
break;
case QRhiTexture::BC3:
blockSize = 16;
break;
case QRhiTexture::BC4:
blockSize = 8;
break;
case QRhiTexture::BC5:
blockSize = 16;
break;
case QRhiTexture::BC6H:
blockSize = 16;
break;
case QRhiTexture::BC7:
blockSize = 16;
break;
default:
Q_UNREACHABLE();
break;
}
if (bpl)
*bpl = qMax<quint32>(1, (size.width() + 3) / 4) * blockSize;
if (alignedHeight)
*alignedHeight = qMax<quint32>(1, (size.height() + 3) / 4);
}
void QRhiD3D11::commitResourceUpdates(QRhiResourceUpdateBatch *resourceUpdates)
{
QRhiResourceUpdateBatchPrivate *ud = QRhiResourceUpdateBatchPrivate::get(resourceUpdates);
......
......@@ -1393,8 +1393,7 @@ bool QRhiVulkan::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &
aRb.result = result;
QVkTexture *tex = QRHI_RES(QVkTexture, aRb.desc.texture);
// ###
aRb.bufSize = tex->m_pixelSize.width() * tex->m_pixelSize.height() * 4;
textureFormatInfo(tex->m_format, tex->m_pixelSize, nullptr, &aRb.bufSize);
// Create a host visible buffer.
VkBufferCreateInfo bufferInfo;
......@@ -1412,7 +1411,7 @@ bool QRhiVulkan::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &
if (err == VK_SUCCESS) {
aRb.bufAlloc = allocation;
} else {
qWarning("Failed to create readback buffer of size %d: %d", aRb.bufSize, err);
qWarning("Failed to create readback buffer of size %u: %d", aRb.bufSize, err);
return false;
}
......
......@@ -475,7 +475,7 @@ public:
QRhiReadbackResult *result;
VkBuffer buf;
QVkAlloc bufAlloc;
int bufSize;
quint32 bufSize;
};
QVector<ActiveReadback> activeReadbacks;
......
vk: readback size/formats
mtl, d3d, gl: rhi without a window
mtl, d3d, gl: implement finish()
mtl, d3d, gl: readback
......@@ -51,6 +50,7 @@ dxc for d3d as an alternative to fxc?
hlsl -> dxc -> spirv -> spirv-cross hmmm...
+++ done
vk: readback size/formats
vk: readback
vk: implement finish()
some wait-gpu stuff for waiting for readback results inside a begin-endFrame f.ex.
......
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