Workaround VK scratch buffer issue.

This commit is contained in:
Бранимир Караџић
2025-03-29 08:21:13 -07:00
parent 93e6125b30
commit 72f9b8b516
2 changed files with 25 additions and 24 deletions

View File

@@ -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<uint8_t>(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;
}

View File

@@ -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;