diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2022-07-07 12:27:44 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2022-07-12 17:16:37 +0200 |
commit | 7de0f3e9ccff3b9eea1c240530834c52ca56d1c0 (patch) | |
tree | 3133d1a10a47fadd34d901fecd57ebea7d2e5c9f /src/gui/rhi/qrhimetal.mm | |
parent | 7b6350fa7743fa4ca10f5aebe0962b6544604e33 (diff) |
rhi: Clean up some inconsistencies
Some of the offsets are already quint32 in the API (vertex input
attributes, dynamic offsets, offsets in draw calls), matching the
reality of the underlying 3D APIs, but many buffer-related functions
use int as of now, simply because that used to be the default choice,
and the same goes for sizes (such as buffer or range sizes). This is
not quite consistent and should be cleaned up if for nothing else then
just to make the classes consistent, but also because no 3D API use a
signed type for offsets, sizes, and strides. (except OpenGL for some)
When it comes to strides (for vertex inputs and raw image texture
uploads), those are already all quint32s. This is straightforward
because most of the 3D APIs use 32-bit uints for these regardless of
the architecture.
Sizes and offsets are often architecture-dependent (Vulkan, Metal),
but there is at least one API where they are always 32-bit even on
64-bit Windows (UINT == unsigned int, D3D11). In addition, we do not
really care about buffer or texture data larger than 4 GB, at least
not without realistic use cases and real world testing, which are
quite unlikely to materialize for now (esp. since we still have the
width/height of 2D textures limited to 16 or 32K in many cases even on
desktops, whereas 2GB+ buffers are not guaranteed in practice even
when an API seemingly allows it).
In any case, the important change here is the signed->unsigned
switch. A number of casts can now be removed here and there in the
backends, because the offsets and sizes are now unsigned as well,
matching the underlying API reality. The size can be potentially
increased later on with minimal effort, if that becomes necessary for
some reason.
Change-Id: I404dbc365ac397eaeeb3bd2da9ce7eb98916da5f
Reviewed-by: Inho Lee <inho.lee@qt.io>
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Diffstat (limited to 'src/gui/rhi/qrhimetal.mm')
-rw-r--r-- | src/gui/rhi/qrhimetal.mm | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/gui/rhi/qrhimetal.mm b/src/gui/rhi/qrhimetal.mm index 1a9ae651a3..ed416feb7c 100644 --- a/src/gui/rhi/qrhimetal.mm +++ b/src/gui/rhi/qrhimetal.mm @@ -192,7 +192,7 @@ struct QMetalBufferData bool slotted; id<MTLBuffer> buf[QMTL_FRAMES_IN_FLIGHT]; struct BufferUpdate { - int offset; + quint32 offset; QRhiBufferData data; }; QVarLengthArray<BufferUpdate, 16> pendingUpdates[QMTL_FRAMES_IN_FLIGHT]; @@ -472,7 +472,7 @@ QRhiSwapChain *QRhiMetal::createSwapChain() return new QMetalSwapChain(this); } -QRhiBuffer *QRhiMetal::createBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, int size) +QRhiBuffer *QRhiMetal::createBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, quint32 size) { return new QMetalBuffer(this, type, usage, size); } @@ -772,7 +772,7 @@ void QRhiMetal::enqueueShaderResourceBindings(QMetalShaderResourceBindings *srbD struct Buffer { int nativeBinding; id<MTLBuffer> mtlbuf; - uint offset; + quint32 offset; }; struct Texture { int nativeBinding; @@ -799,7 +799,7 @@ void QRhiMetal::enqueueShaderResourceBindings(QMetalShaderResourceBindings *srbD { QMetalBuffer *bufD = QRHI_RES(QMetalBuffer, b->u.ubuf.buf); id<MTLBuffer> mtlbuf = bufD->d->buf[bufD->d->slotted ? currentFrameSlot : 0]; - uint offset = uint(b->u.ubuf.offset); + quint32 offset = b->u.ubuf.offset; for (int i = 0; i < dynamicOffsetCount; ++i) { const QRhiCommandBuffer::DynamicOffset &dynOfs(dynamicOffsets[i]); if (dynOfs.first == b->binding) { @@ -895,7 +895,7 @@ void QRhiMetal::enqueueShaderResourceBindings(QMetalShaderResourceBindings *srbD { QMetalBuffer *bufD = QRHI_RES(QMetalBuffer, b->u.sbuf.buf); id<MTLBuffer> mtlbuf = bufD->d->buf[0]; - uint offset = uint(b->u.sbuf.offset); + quint32 offset = b->u.sbuf.offset; if (b->stage.testFlag(QRhiShaderResourceBinding::VertexStage)) { const int nativeBinding = mapBinding(b->binding, VERTEX, nativeResourceBindingMaps, BindingType::Buffer); if (nativeBinding >= 0) @@ -1365,7 +1365,7 @@ void QRhiMetal::drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount, return; const quint32 indexOffset = cbD->currentIndexOffset + firstIndex * (cbD->currentIndexFormat == QRhiCommandBuffer::IndexUInt16 ? 2 : 4); - Q_ASSERT(indexOffset == aligned<quint32>(indexOffset, 4)); + Q_ASSERT(indexOffset == aligned(indexOffset, 4u)); QMetalBuffer *ibufD = QRHI_RES(QMetalBuffer, cbD->currentIndexBuffer); id<MTLBuffer> mtlbuf = ibufD->d->buf[ibufD->d->slotted ? currentFrameSlot : 0]; @@ -1971,17 +1971,17 @@ void QRhiMetal::executeBufferHostWritesForSlot(QMetalBuffer *bufD, int slot) return; void *p = [bufD->d->buf[slot] contents]; - int changeBegin = -1; - int changeEnd = -1; + quint32 changeBegin = UINT32_MAX; + quint32 changeEnd = 0; for (const QMetalBufferData::BufferUpdate &u : qAsConst(bufD->d->pendingUpdates[slot])) { memcpy(static_cast<char *>(p) + u.offset, u.data.constData(), size_t(u.data.size())); - if (changeBegin == -1 || u.offset < changeBegin) + if (u.offset < changeBegin) changeBegin = u.offset; - if (changeEnd == -1 || u.offset + u.data.size() > changeEnd) + if (u.offset + u.data.size() > changeEnd) changeEnd = u.offset + u.data.size(); } #ifdef Q_OS_MACOS - if (changeBegin >= 0 && bufD->d->managed) + if (changeBegin < UINT32_MAX && changeBegin < changeEnd && bufD->d->managed) [bufD->d->buf[slot] didModifyRange: NSMakeRange(NSUInteger(changeBegin), NSUInteger(changeEnd - changeBegin))]; #endif @@ -2254,7 +2254,7 @@ void QRhiMetal::finishActiveReadbacks(bool forced) f(); } -QMetalBuffer::QMetalBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, int size) +QMetalBuffer::QMetalBuffer(QRhiImplementation *rhi, Type type, UsageFlags usage, quint32 size) : QRhiBuffer(rhi, type, usage, size), d(new QMetalBufferData) { @@ -2300,8 +2300,8 @@ bool QMetalBuffer::create() return false; } - const uint nonZeroSize = m_size <= 0 ? 256 : uint(m_size); - const uint roundedSize = m_usage.testFlag(QRhiBuffer::UniformBuffer) ? aligned<uint>(nonZeroSize, 256) : nonZeroSize; + const quint32 nonZeroSize = m_size <= 0 ? 256 : m_size; + const quint32 roundedSize = m_usage.testFlag(QRhiBuffer::UniformBuffer) ? aligned(nonZeroSize, 256u) : nonZeroSize; d->managed = false; MTLResourceOptions opts = MTLResourceStorageModeShared; |