Browse Source

Workaround VK scratch buffer issue.

Бранимир Караџић 8 months ago
parent
commit
72f9b8b516
2 changed files with 24 additions and 23 deletions
  1. 8 8
      src/bgfx.cpp
  2. 16 15
      src/renderer_vk.cpp

+ 8 - 8
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)
 	void TextVideoMemBlitter::init(uint8_t scale)
 	{
 	{
@@ -710,8 +710,8 @@ namespace bgfx
 
 
 		m_program = createProgram(vsh, fsh, true);
 		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);
 		m_scale = bx::max<uint8_t>(scale, 1);
 	}
 	}
 
 
@@ -808,12 +808,12 @@ namespace bgfx
 			uint32_t startVertex = 0;
 			uint32_t startVertex = 0;
 			uint32_t numIndices = 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;
 				xx = xx < _mem.m_width ? xx : 0;
 				const TextVideoMem::MemSlot* line = &_mem.m_mem[yy*_mem.m_width+xx];
 				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;
 					uint32_t ch = line->character;
 					const uint8_t attr = line->attribute;
 					const uint8_t attr = line->attribute;
@@ -856,7 +856,7 @@ namespace bgfx
 					line++;
 					line++;
 				}
 				}
 
 
-				if (numIndices >= numBatchIndices)
+				if (numIndices >= kNumBatchIndices)
 				{
 				{
 					break;
 					break;
 				}
 				}

+ 16 - 15
src/renderer_vk.cpp

@@ -2700,12 +2700,13 @@ VK_IMPORT_DEVICE
 		void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
 		void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
 		{
 		{
 			const uint32_t numVertices = _numIndices*4/6;
 			const uint32_t numVertices = _numIndices*4/6;
-			if (0 < numVertices && m_backBuffer.isRenderable() )
+
+			if (0 < numVertices
+			&&  m_backBuffer.isRenderable() )
 			{
 			{
-				m_indexBuffers[_blitter.m_ib->handle.idx].update(m_commandBuffer, 0, _numIndices*2, _blitter.m_ib->data);
+				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);
 				m_vertexBuffers[_blitter.m_vb->handle.idx].update(m_commandBuffer, 0, numVertices*_blitter.m_layout.m_stride, _blitter.m_vb->data, true);
 
 
-
 				VkRenderPassBeginInfo rpbi;
 				VkRenderPassBeginInfo rpbi;
 				rpbi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
 				rpbi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
 				rpbi.pNext = NULL;
 				rpbi.pNext = NULL;
@@ -2720,7 +2721,6 @@ VK_IMPORT_DEVICE
 
 
 				vkCmdBeginRenderPass(m_commandBuffer, &rpbi, VK_SUBPASS_CONTENTS_INLINE);
 				vkCmdBeginRenderPass(m_commandBuffer, &rpbi, VK_SUBPASS_CONTENTS_INLINE);
 				vkCmdDrawIndexed(m_commandBuffer, _numIndices, 1, 0, 0, 0);
 				vkCmdDrawIndexed(m_commandBuffer, _numIndices, 1, 0, 0, 0);
-
 				vkCmdEndRenderPass(m_commandBuffer);
 				vkCmdEndRenderPass(m_commandBuffer);
 			}
 			}
 		}
 		}
@@ -4454,25 +4454,26 @@ VK_IMPORT_DEVICE
 			return createHostBuffer(_size, flags, _buffer, _memory, _data);
 			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);
 			BGFX_PROFILER_SCOPE("allocFromScratchStagingBuffer", kColorResource);
 
 
 			StagingBufferVK result;
 			StagingBufferVK result;
 			ScratchBufferVK &scratch = m_scratchStagingBuffer[m_cmd.m_currentFrameInFlight];
 			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);
 				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_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)
 					if (_data != NULL)
 					{
 					{
@@ -4489,9 +4490,9 @@ VK_IMPORT_DEVICE
 
 
 			VK_CHECK(createStagingBuffer(_size, &result.m_buffer, &result.m_deviceMem, _data));
 			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_offset = 0;
-			result.m_data = NULL;
+			result.m_data   = NULL;
 
 
 			return result;
 			return result;
 		}
 		}
@@ -4839,7 +4840,7 @@ VK_DESTROY
 		BGFX_PROFILER_SCOPE("BufferVK::update", kColorFrame);
 		BGFX_PROFILER_SCOPE("BufferVK::update", kColorFrame);
 		BX_UNUSED(_discard);
 		BX_UNUSED(_discard);
 
 
-		StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data);
+		StagingBufferVK stagingBuffer = s_renderVK->allocFromScratchStagingBuffer(_size, 8, _data, !_discard);
 
 
 		VkBufferCopy region;
 		VkBufferCopy region;
 		region.srcOffset = stagingBuffer.m_offset;
 		region.srcOffset = stagingBuffer.m_offset;