Skip to content

Commit 72f9b8b

Browse files
committed
Workaround VK scratch buffer issue.
1 parent 93e6125 commit 72f9b8b

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/bgfx.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ namespace bgfx
668668
}
669669
}
670670

671-
static const uint32_t numCharsPerBatch = 1024;
672-
static const uint32_t numBatchVertices = numCharsPerBatch*4;
673-
static const uint32_t numBatchIndices = numCharsPerBatch*6;
671+
static constexpr uint32_t kNumCharsPerBatch = 1024;
672+
static constexpr uint32_t kNumBatchVertices = kNumCharsPerBatch*4;
673+
static constexpr uint32_t kNumBatchIndices = kNumCharsPerBatch*6;
674674

675675
void TextVideoMemBlitter::init(uint8_t scale)
676676
{
@@ -710,8 +710,8 @@ namespace bgfx
710710

711711
m_program = createProgram(vsh, fsh, true);
712712

713-
m_vb = s_ctx->createTransientVertexBuffer(numBatchVertices*m_layout.m_stride, &m_layout);
714-
m_ib = s_ctx->createTransientIndexBuffer(numBatchIndices*2);
713+
m_vb = s_ctx->createTransientVertexBuffer(kNumBatchVertices*m_layout.m_stride, &m_layout);
714+
m_ib = s_ctx->createTransientIndexBuffer(kNumBatchIndices*2);
715715
m_scale = bx::max<uint8_t>(scale, 1);
716716
}
717717

@@ -808,12 +808,12 @@ namespace bgfx
808808
uint32_t startVertex = 0;
809809
uint32_t numIndices = 0;
810810

811-
for (; yy < _mem.m_height && numIndices < numBatchIndices; ++yy)
811+
for (; yy < _mem.m_height && numIndices < kNumBatchIndices; ++yy)
812812
{
813813
xx = xx < _mem.m_width ? xx : 0;
814814
const TextVideoMem::MemSlot* line = &_mem.m_mem[yy*_mem.m_width+xx];
815815

816-
for (; xx < _mem.m_width && numIndices < numBatchIndices; ++xx)
816+
for (; xx < _mem.m_width && numIndices < kNumBatchIndices; ++xx)
817817
{
818818
uint32_t ch = line->character;
819819
const uint8_t attr = line->attribute;
@@ -856,7 +856,7 @@ namespace bgfx
856856
line++;
857857
}
858858

859-
if (numIndices >= numBatchIndices)
859+
if (numIndices >= kNumBatchIndices)
860860
{
861861
break;
862862
}

src/renderer_vk.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -2700,12 +2700,13 @@ VK_IMPORT_DEVICE
27002700
void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
27012701
{
27022702
const uint32_t numVertices = _numIndices*4/6;
2703-
if (0 < numVertices && m_backBuffer.isRenderable() )
2703+
2704+
if (0 < numVertices
2705+
&& m_backBuffer.isRenderable() )
27042706
{
2705-
m_indexBuffers[_blitter.m_ib->handle.idx].update(m_commandBuffer, 0, _numIndices*2, _blitter.m_ib->data);
2707+
m_indexBuffers[_blitter.m_ib->handle.idx].update(m_commandBuffer, 0, _numIndices*2, _blitter.m_ib->data, true);
27062708
m_vertexBuffers[_blitter.m_vb->handle.idx].update(m_commandBuffer, 0, numVertices*_blitter.m_layout.m_stride, _blitter.m_vb->data, true);
27072709

2708-
27092710
VkRenderPassBeginInfo rpbi;
27102711
rpbi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
27112712
rpbi.pNext = NULL;
@@ -2720,7 +2721,6 @@ VK_IMPORT_DEVICE
27202721

27212722
vkCmdBeginRenderPass(m_commandBuffer, &rpbi, VK_SUBPASS_CONTENTS_INLINE);
27222723
vkCmdDrawIndexed(m_commandBuffer, _numIndices, 1, 0, 0, 0);
2723-
27242724
vkCmdEndRenderPass(m_commandBuffer);
27252725
}
27262726
}
@@ -4454,25 +4454,26 @@ VK_IMPORT_DEVICE
44544454
return createHostBuffer(_size, flags, _buffer, _memory, _data);
44554455
}
44564456

4457-
StagingBufferVK allocFromScratchStagingBuffer(uint32_t _size, uint32_t _align, const void *_data = NULL)
4457+
StagingBufferVK allocFromScratchStagingBuffer(uint32_t _size, uint32_t _align, const void* _data = NULL, bool _tryScratch = true)
44584458
{
44594459
BGFX_PROFILER_SCOPE("allocFromScratchStagingBuffer", kColorResource);
44604460

44614461
StagingBufferVK result;
44624462
ScratchBufferVK &scratch = m_scratchStagingBuffer[m_cmd.m_currentFrameInFlight];
44634463

4464-
if (_size <= BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRATCH_BUFFER)
4464+
if (_tryScratch
4465+
&& _size <= BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRATCH_BUFFER)
44654466
{
44664467
const uint32_t scratchOffset = scratch.alloc(_size, _align);
44674468

4468-
if (scratchOffset != UINT32_MAX)
4469+
if (UINT32_MAX != scratchOffset)
44694470
{
4470-
result.m_isFromScratch = true;
4471-
result.m_size = _size;
4472-
result.m_offset = scratchOffset;
4473-
result.m_buffer = scratch.m_buffer;
4471+
result.m_isFromScratch = true;
4472+
result.m_size = _size;
4473+
result.m_offset = scratchOffset;
4474+
result.m_buffer = scratch.m_buffer;
44744475
result.m_deviceMem = scratch.m_deviceMem;
4475-
result.m_data = scratch.m_data + result.m_offset;
4476+
result.m_data = scratch.m_data + result.m_offset;
44764477

44774478
if (_data != NULL)
44784479
{
@@ -4489,9 +4490,9 @@ VK_IMPORT_DEVICE
44894490

44904491
VK_CHECK(createStagingBuffer(_size, &result.m_buffer, &result.m_deviceMem, _data));
44914492

4492-
result.m_size = _size;
4493+
result.m_size = _size;
44934494
result.m_offset = 0;
4494-
result.m_data = NULL;
4495+
result.m_data = NULL;
44954496

44964497
return result;
44974498
}
@@ -4839,7 +4840,7 @@ VK_DESTROY
48394840
BGFX_PROFILER_SCOPE("BufferVK::update", kColorFrame);
48404841
BX_UNUSED(_discard);
48414842

4842-
StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data);
4843+
StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data, !_discard);
48434844

48444845
VkBufferCopy region;
48454846
region.srcOffset = stagingBuffer.m_offset;

0 commit comments

Comments
 (0)