Skip to content

Commit

Permalink
Vulkan: improve staging data performance by using scratch buffers per…
Browse files Browse the repository at this point in the history
… frame. (#3295)

* Vulkan: improve staging data performance by using scratch buffers per frame.

* vulkan: Add alignment parameter to request scratch space.

* Align staging buffers to texel block size.

* Fix scratch buffer allocation bug.

* Fix some non-deterministic behavior found by Valgrind. Paranoid printing.

* Remove debugging printing

* Fix alignment of converted formats.

* Remove forgotten debug print.
  • Loading branch information
mcourteaux authored Jun 21, 2024
1 parent d9c74e9 commit 1109f3c
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/bgfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,7 @@ namespace bgfx

void Context::flushTextureUpdateBatch(CommandBuffer& _cmdbuf)
{
BGFX_PROFILER_SCOPE("flushTextureUpdateBatch", 0xff2040ff);
if (m_textureUpdateBatch.sort() )
{
const uint32_t pos = _cmdbuf.m_pos;
Expand Down
15 changes: 15 additions & 0 deletions src/bgfx_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,9 @@ namespace bgfx
bind.m_idx = kInvalidHandle;
bind.m_type = 0;
bind.m_samplerFlags = 0;
bind.m_format = 0;
bind.m_access = 0;
bind.m_mip = 0;
}
}
};
Expand Down Expand Up @@ -2168,6 +2171,8 @@ namespace bgfx
bx::memSet(m_occlusion, 0xff, sizeof(m_occlusion) );

m_perfStats.viewStats = m_viewStats;

bx::memSet(&m_renderItemBind[0], 0, sizeof(m_renderItemBind));
}

~Frame()
Expand Down Expand Up @@ -2445,6 +2450,13 @@ namespace bgfx
{
EncoderImpl()
{
// Although it will be cleared by the discard(), the fact that the
// struct is padded to have a size equal to the cache line size,
// will leaves bytes uninitialized. This will influence the hashing
// as it reads those bytes too. To make this deterministic, we will
// clear all bytes (inclusively the padding) before we start.
bx::memSet(&m_bind, 0, sizeof(m_bind));

discard(BGFX_DISCARD_ALL);
}

Expand Down Expand Up @@ -2725,6 +2737,9 @@ namespace bgfx
? BGFX_SAMPLER_INTERNAL_DEFAULT
: _flags
;
bind.m_format = 0;
bind.m_access = 0;
bind.m_mip = 0;

if (isValid(_sampler) )
{
Expand Down
16 changes: 16 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,22 @@ BX_STATIC_ASSERT(bx::isPowerOf2(BGFX_CONFIG_MAX_VIEWS), "BGFX_CONFIG_MAX_VIEWS m
# define BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE (2<<20)
#endif // BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE

#ifndef BGFX_CONFIG_PER_FRAME_SCRATCH_STAGING_BUFFER_SIZE
/// Amount of scratch buffer size (per in-flight frame) that will be reserved
/// for staging data for copying to the device (such as vertex buffer data,
/// texture data, etc). This buffer will be used instead of allocating memory
/// on device separately for every data copy.
/// Note: Currently only used by the Vulkan backend.
# define BGFX_CONFIG_PER_FRAME_SCRATCH_STAGING_BUFFER_SIZE (32<<20)
#endif

#ifndef BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRACH_BUFFER
/// The threshold of data size above which the staging scratch buffer will
/// not be used, but instead a separate device memory allocation will take
/// place to stage the data for copying to device.
# define BGFX_CONFIG_MAX_STAGING_SIZE_FOR_SCRACH_BUFFER (16 << 20)
#endif

#ifndef BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT
# define BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT 5
#endif // BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT
Expand Down
Loading

0 comments on commit 1109f3c

Please sign in to comment.