From 72f9b8b51607823c80ea5774776734978ac2c9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sat, 29 Mar 2025 08:21:13 -0700 Subject: [PATCH] Workaround VK scratch buffer issue. --- src/bgfx.cpp | 16 ++++++++-------- src/renderer_vk.cpp | 33 +++++++++++++++++---------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 902f76e8c..f34c186a3 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -668,9 +668,9 @@ namespace bgfx } } - static const uint32_t numCharsPerBatch = 1024; - static const uint32_t numBatchVertices = numCharsPerBatch*4; - static const uint32_t numBatchIndices = numCharsPerBatch*6; + static constexpr uint32_t kNumCharsPerBatch = 1024; + static constexpr uint32_t kNumBatchVertices = kNumCharsPerBatch*4; + static constexpr uint32_t kNumBatchIndices = kNumCharsPerBatch*6; void TextVideoMemBlitter::init(uint8_t scale) { @@ -710,8 +710,8 @@ namespace bgfx m_program = createProgram(vsh, fsh, true); - m_vb = s_ctx->createTransientVertexBuffer(numBatchVertices*m_layout.m_stride, &m_layout); - m_ib = s_ctx->createTransientIndexBuffer(numBatchIndices*2); + m_vb = s_ctx->createTransientVertexBuffer(kNumBatchVertices*m_layout.m_stride, &m_layout); + m_ib = s_ctx->createTransientIndexBuffer(kNumBatchIndices*2); m_scale = bx::max(scale, 1); } @@ -808,12 +808,12 @@ namespace bgfx uint32_t startVertex = 0; uint32_t numIndices = 0; - for (; yy < _mem.m_height && numIndices < numBatchIndices; ++yy) + for (; yy < _mem.m_height && numIndices < kNumBatchIndices; ++yy) { xx = xx < _mem.m_width ? xx : 0; const TextVideoMem::MemSlot* line = &_mem.m_mem[yy*_mem.m_width+xx]; - for (; xx < _mem.m_width && numIndices < numBatchIndices; ++xx) + for (; xx < _mem.m_width && numIndices < kNumBatchIndices; ++xx) { uint32_t ch = line->character; const uint8_t attr = line->attribute; @@ -856,7 +856,7 @@ namespace bgfx line++; } - if (numIndices >= numBatchIndices) + if (numIndices >= kNumBatchIndices) { break; } diff --git a/src/renderer_vk.cpp b/src/renderer_vk.cpp index c7d560a7f..e6eb6748c 100644 --- a/src/renderer_vk.cpp +++ b/src/renderer_vk.cpp @@ -2700,11 +2700,12 @@ VK_IMPORT_DEVICE void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override { const uint32_t numVertices = _numIndices*4/6; - if (0 < numVertices && m_backBuffer.isRenderable() ) - { - m_indexBuffers[_blitter.m_ib->handle.idx].update(m_commandBuffer, 0, _numIndices*2, _blitter.m_ib->data); - m_vertexBuffers[_blitter.m_vb->handle.idx].update(m_commandBuffer, 0, numVertices*_blitter.m_layout.m_stride, _blitter.m_vb->data, true); + if (0 < numVertices + && m_backBuffer.isRenderable() ) + { + m_indexBuffers[_blitter.m_ib->handle.idx].update(m_commandBuffer, 0, _numIndices*2, _blitter.m_ib->data, true); + m_vertexBuffers[_blitter.m_vb->handle.idx].update(m_commandBuffer, 0, numVertices*_blitter.m_layout.m_stride, _blitter.m_vb->data, true); VkRenderPassBeginInfo rpbi; rpbi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; @@ -2720,7 +2721,6 @@ VK_IMPORT_DEVICE vkCmdBeginRenderPass(m_commandBuffer, &rpbi, VK_SUBPASS_CONTENTS_INLINE); vkCmdDrawIndexed(m_commandBuffer, _numIndices, 1, 0, 0, 0); - vkCmdEndRenderPass(m_commandBuffer); } } @@ -4454,25 +4454,26 @@ VK_IMPORT_DEVICE return createHostBuffer(_size, flags, _buffer, _memory, _data); } - StagingBufferVK allocFromScratchStagingBuffer(uint32_t _size, uint32_t _align, const void *_data = NULL) + StagingBufferVK allocFromScratchStagingBuffer(uint32_t _size, uint32_t _align, const void* _data = NULL, bool _tryScratch = true) { BGFX_PROFILER_SCOPE("allocFromScratchStagingBuffer", kColorResource); StagingBufferVK result; ScratchBufferVK &scratch = m_scratchStagingBuffer[m_cmd.m_currentFrameInFlight]; - if (_size <= BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRATCH_BUFFER) + if (_tryScratch + && _size <= BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRATCH_BUFFER) { const uint32_t scratchOffset = scratch.alloc(_size, _align); - if (scratchOffset != UINT32_MAX) + if (UINT32_MAX != scratchOffset) { - result.m_isFromScratch = true; - result.m_size = _size; - result.m_offset = scratchOffset; - result.m_buffer = scratch.m_buffer; + result.m_isFromScratch = true; + result.m_size = _size; + result.m_offset = scratchOffset; + result.m_buffer = scratch.m_buffer; result.m_deviceMem = scratch.m_deviceMem; - result.m_data = scratch.m_data + result.m_offset; + result.m_data = scratch.m_data + result.m_offset; if (_data != NULL) { @@ -4489,9 +4490,9 @@ VK_IMPORT_DEVICE VK_CHECK(createStagingBuffer(_size, &result.m_buffer, &result.m_deviceMem, _data)); - result.m_size = _size; + result.m_size = _size; result.m_offset = 0; - result.m_data = NULL; + result.m_data = NULL; return result; } @@ -4839,7 +4840,7 @@ VK_DESTROY BGFX_PROFILER_SCOPE("BufferVK::update", kColorFrame); BX_UNUSED(_discard); - StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data); + StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data, !_discard); VkBufferCopy region; region.srcOffset = stagingBuffer.m_offset;