diff --git a/bindings/bf/bgfx.bf b/bindings/bf/bgfx.bf
index 1a2f7e1cc..4b0b800cc 100644
--- a/bindings/bf/bgfx.bf
+++ b/bindings/bf/bgfx.bf
@@ -2074,6 +2074,7 @@ public static class bgfx
public uint32 size;
public uint32 startIndex;
public IndexBufferHandle handle;
+ public uint8 isIndex16;
}
[CRepr]
@@ -2842,9 +2843,10 @@ public static class bgfx
///
/// TransientIndexBuffer structure is filled and is valid for the duration of frame, and it can be reused for multiple draw calls.
/// Number of indices to allocate.
+ /// Set to `true` if input indices will be 32-bit.
///
[LinkName("bgfx_alloc_transient_index_buffer")]
- public static extern void alloc_transient_index_buffer(TransientIndexBuffer* _tib, uint32 _num);
+ public static extern void alloc_transient_index_buffer(TransientIndexBuffer* _tib, uint32 _num, bool _index32);
///
/// Allocate transient vertex buffer.
diff --git a/bindings/cs/bgfx.cs b/bindings/cs/bgfx.cs
index 679fb416d..03fe1bedd 100644
--- a/bindings/cs/bgfx.cs
+++ b/bindings/cs/bgfx.cs
@@ -2051,6 +2051,7 @@ public static partial class bgfx
public uint size;
public uint startIndex;
public IndexBufferHandle handle;
+ public byte isIndex16;
}
public unsafe struct TransientVertexBuffer
@@ -2798,9 +2799,10 @@ public static partial class bgfx
///
/// TransientIndexBuffer structure is filled and is valid for the duration of frame, and it can be reused for multiple draw calls.
/// Number of indices to allocate.
+ /// Set to `true` if input indices will be 32-bit.
///
[DllImport(DllName, EntryPoint="bgfx_alloc_transient_index_buffer", CallingConvention = CallingConvention.Cdecl)]
- public static extern unsafe void alloc_transient_index_buffer(TransientIndexBuffer* _tib, uint _num);
+ public static extern unsafe void alloc_transient_index_buffer(TransientIndexBuffer* _tib, uint _num, bool _index32);
///
/// Allocate transient vertex buffer.
diff --git a/bindings/d/funcs.d b/bindings/d/funcs.d
index 890382fca..40da13ff9 100644
--- a/bindings/d/funcs.d
+++ b/bindings/d/funcs.d
@@ -567,8 +567,9 @@ version(BindBgfx_Static)
* for the duration of frame, and it can be reused for multiple draw
* calls.
* _num = Number of indices to allocate.
+ * _index32 = Set to `true` if input indices will be 32-bit.
*/
- void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint _num);
+ void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint _num, bool _index32);
/**
* Allocate transient vertex buffer.
@@ -2751,8 +2752,9 @@ else
* for the duration of frame, and it can be reused for multiple draw
* calls.
* _num = Number of indices to allocate.
+ * _index32 = Set to `true` if input indices will be 32-bit.
*/
- alias da_bgfx_alloc_transient_index_buffer = void function(bgfx_transient_index_buffer_t* _tib, uint _num);
+ alias da_bgfx_alloc_transient_index_buffer = void function(bgfx_transient_index_buffer_t* _tib, uint _num, bool _index32);
da_bgfx_alloc_transient_index_buffer bgfx_alloc_transient_index_buffer;
/**
diff --git a/bindings/d/types.d b/bindings/d/types.d
index e4b5e44b2..49be1ad41 100644
--- a/bindings/d/types.d
+++ b/bindings/d/types.d
@@ -10,7 +10,7 @@ public import core.stdc.stdarg : va_list;
extern(C) @nogc nothrow:
-enum uint BGFX_API_VERSION = 111;
+enum uint BGFX_API_VERSION = 112;
alias bgfx_view_id_t = ushort;
@@ -891,6 +891,7 @@ struct bgfx_transient_index_buffer_t
uint size; /// Data size.
uint startIndex; /// First index.
bgfx_index_buffer_handle_t handle; /// Index buffer handle.
+ bool isIndex16; /// Index buffer format is 16-bits if true, otherwise it is 32-bit.
}
/// Transient vertex buffer.
diff --git a/examples/common/imgui/imgui.cpp b/examples/common/imgui/imgui.cpp
index 34558323f..5dda5ea59 100644
--- a/examples/common/imgui/imgui.cpp
+++ b/examples/common/imgui/imgui.cpp
@@ -96,7 +96,7 @@ struct OcornutImguiContext
}
bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_layout);
- bgfx::allocTransientIndexBuffer(&tib, numIndices);
+ bgfx::allocTransientIndexBuffer(&tib, numIndices, sizeof(ImDrawIdx) == 4);
ImDrawVert* verts = (ImDrawVert*)tvb.data;
bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h
index 1d5deb1f9..e96534295 100644
--- a/include/bgfx/bgfx.h
+++ b/include/bgfx/bgfx.h
@@ -823,6 +823,7 @@ namespace bgfx
uint32_t size; //!< Data size.
uint32_t startIndex; //!< First index.
IndexBufferHandle handle; //!< Index buffer handle.
+ bool isIndex16; //!< Index buffer format is 16-bits if true, otherwise it is 32-bit.
};
/// Transient vertex buffer.
@@ -2443,6 +2444,7 @@ namespace bgfx
/// for the duration of frame, and it can be reused for multiple draw
/// calls.
/// @param[in] _num Number of indices to allocate.
+ /// @param[in] _index32 Set to `true` if input indices will be 32-bit.
///
/// @remarks
/// Only 16-bit index buffer is supported.
@@ -2452,7 +2454,8 @@ namespace bgfx
void allocTransientIndexBuffer(
TransientIndexBuffer* _tib
, uint32_t _num
- );
+ , bool _index32 = false
+ );
/// Allocate transient vertex buffer.
///
diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h
index 09a7ec035..1dd0658f0 100644
--- a/include/bgfx/c99/bgfx.h
+++ b/include/bgfx/c99/bgfx.h
@@ -711,6 +711,7 @@ typedef struct bgfx_transient_index_buffer_s
uint32_t size; /** Data size. */
uint32_t startIndex; /** First index. */
bgfx_index_buffer_handle_t handle; /** Index buffer handle. */
+ bool isIndex16; /** Index buffer format is 16-bits if true, otherwise it is 32-bit. */
} bgfx_transient_index_buffer_t;
@@ -1546,9 +1547,10 @@ BGFX_C_API uint32_t bgfx_get_avail_instance_data_buffer(uint32_t _num, uint16_t
* for the duration of frame, and it can be reused for multiple draw
* calls.
* @param[in] _num Number of indices to allocate.
+ * @param[in] _index32 Set to `true` if input indices will be 32-bit.
*
*/
-BGFX_C_API void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint32_t _num);
+BGFX_C_API void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint32_t _num, bool _index32);
/**
* Allocate transient vertex buffer.
@@ -3585,7 +3587,7 @@ struct bgfx_interface_vtbl
uint32_t (*get_avail_transient_index_buffer)(uint32_t _num);
uint32_t (*get_avail_transient_vertex_buffer)(uint32_t _num, const bgfx_vertex_layout_t * _layout);
uint32_t (*get_avail_instance_data_buffer)(uint32_t _num, uint16_t _stride);
- void (*alloc_transient_index_buffer)(bgfx_transient_index_buffer_t* _tib, uint32_t _num);
+ void (*alloc_transient_index_buffer)(bgfx_transient_index_buffer_t* _tib, uint32_t _num, bool _index32);
void (*alloc_transient_vertex_buffer)(bgfx_transient_vertex_buffer_t* _tvb, uint32_t _num, const bgfx_vertex_layout_t * _layout);
bool (*alloc_transient_buffers)(bgfx_transient_vertex_buffer_t* _tvb, const bgfx_vertex_layout_t * _layout, uint32_t _numVertices, bgfx_transient_index_buffer_t* _tib, uint32_t _numIndices);
void (*alloc_instance_data_buffer)(bgfx_instance_data_buffer_t* _idb, uint32_t _num, uint16_t _stride);
diff --git a/include/bgfx/defines.h b/include/bgfx/defines.h
index 8a6c9f9a5..344f55d9f 100644
--- a/include/bgfx/defines.h
+++ b/include/bgfx/defines.h
@@ -15,7 +15,7 @@
#ifndef BGFX_DEFINES_H_HEADER_GUARD
#define BGFX_DEFINES_H_HEADER_GUARD
-#define BGFX_API_VERSION UINT32_C(111)
+#define BGFX_API_VERSION UINT32_C(112)
/**
* Color RGB/alpha/depth write. When it's not specified write will be disabled.
diff --git a/scripts/bgfx.idl b/scripts/bgfx.idl
index b4220cefa..0094f4e68 100644
--- a/scripts/bgfx.idl
+++ b/scripts/bgfx.idl
@@ -1,7 +1,7 @@
-- vim: syntax=lua
-- bgfx interface
-version(111)
+version(112)
typedef "bool"
typedef "char"
@@ -806,8 +806,8 @@ struct.Init { ctor }
---
--- @attention It is illegal to create this structure on stack and pass it to any bgfx API.
struct.Memory
- .data "uint8_t*" [[Pointer to data.]]
- .size "uint32_t" [[Data size.]]
+ .data "uint8_t*" --- Pointer to data.
+ .size "uint32_t" --- Data size.
--- Transient index buffer.
struct.TransientIndexBuffer
@@ -815,6 +815,7 @@ struct.TransientIndexBuffer
.size "uint32_t" --- Data size.
.startIndex "uint32_t" --- First index.
.handle "IndexBufferHandle" --- Index buffer handle.
+ .isIndex16 "bool" --- Index buffer format is 16-bits if true, otherwise it is 32-bit.
--- Transient vertex buffer.
struct.TransientVertexBuffer
@@ -1472,10 +1473,12 @@ func.getAvailInstanceDataBuffer
---
func.allocTransientIndexBuffer
"void"
- .tib "TransientIndexBuffer*" { out } --- TransientIndexBuffer structure is filled and is valid
- --- for the duration of frame, and it can be reused for multiple draw
- --- calls.
- .num "uint32_t" --- Number of indices to allocate.
+ .tib "TransientIndexBuffer*" { out } --- TransientIndexBuffer structure is filled and is valid
+ --- for the duration of frame, and it can be reused for multiple draw
+ --- calls.
+ .num "uint32_t" --- Number of indices to allocate.
+ .index32 "bool" --- Set to `true` if input indices will be 32-bit.
+ { default = false }
--- Allocate transient vertex buffer.
func.allocTransientVertexBuffer
diff --git a/src/bgfx.cpp b/src/bgfx.cpp
index f3946c780..8fc24883d 100644
--- a/src/bgfx.cpp
+++ b/src/bgfx.cpp
@@ -3644,7 +3644,8 @@ namespace bgfx
void Encoder::setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
{
BGFX_CHECK_HANDLE("setIndexBuffer", s_ctx->m_indexBufferHandle, _handle);
- BGFX_ENCODER(setIndexBuffer(_handle, _firstIndex, _numIndices) );
+ const IndexBuffer& ib = s_ctx->m_indexBuffers[_handle.idx];
+ BGFX_ENCODER(setIndexBuffer(_handle, ib, _firstIndex, _numIndices) );
}
void Encoder::setIndexBuffer(DynamicIndexBufferHandle _handle)
@@ -4158,17 +4159,19 @@ namespace bgfx
return s_ctx->getAvailTransientVertexBuffer(_num, _stride);
}
- void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num)
+ void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num, bool _index32)
{
BX_ASSERT(NULL != _tib, "_tib can't be NULL");
BX_ASSERT(0 < _num, "Requesting 0 indices.");
- s_ctx->allocTransientIndexBuffer(_tib, _num);
- BX_ASSERT(_num == _tib->size/2
+ s_ctx->allocTransientIndexBuffer(_tib, _num, _index32);
+ const uint32_t indexSize = _tib->isIndex16 ? 2 : 4;
+ BX_ASSERT(_num == _tib->size/ indexSize
, "Failed to allocate transient index buffer (requested %d, available %d). "
"Use bgfx::getAvailTransient* functions to ensure availability."
, _num
- , _tib->size/2
+ , _tib->size/indexSize
);
+ BX_UNUSED(indexSize);
}
void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexLayout& _layout)
@@ -5279,7 +5282,7 @@ BX_STATIC_ASSERT(FLAGS_MASK_TEST(0
BX_STATIC_ASSERT(FLAGS_MASK_TEST(0
| BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE
- , BGFX_SUBMIT_RESERVED_MASK
+ , BGFX_SUBMIT_INTERNAL_RESERVED_MASK
) );
BX_STATIC_ASSERT( (0
diff --git a/src/bgfx.idl.inl b/src/bgfx.idl.inl
index 2005c8fa7..7f34be66e 100644
--- a/src/bgfx.idl.inl
+++ b/src/bgfx.idl.inl
@@ -345,9 +345,9 @@ BGFX_C_API uint32_t bgfx_get_avail_instance_data_buffer(uint32_t _num, uint16_t
return bgfx::getAvailInstanceDataBuffer(_num, _stride);
}
-BGFX_C_API void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint32_t _num)
+BGFX_C_API void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint32_t _num, bool _index32)
{
- bgfx::allocTransientIndexBuffer((bgfx::TransientIndexBuffer*)_tib, _num);
+ bgfx::allocTransientIndexBuffer((bgfx::TransientIndexBuffer*)_tib, _num, _index32);
}
BGFX_C_API void bgfx_alloc_transient_vertex_buffer(bgfx_transient_vertex_buffer_t* _tvb, uint32_t _num, const bgfx_vertex_layout_t * _layout)
diff --git a/src/bgfx_p.h b/src/bgfx_p.h
index e875f4818..c2c17af15 100644
--- a/src/bgfx_p.h
+++ b/src/bgfx_p.h
@@ -216,8 +216,10 @@ namespace stl = std;
#define BGFX_STATE_INTERNAL_SCISSOR UINT64_C(0x2000000000000000)
#define BGFX_STATE_INTERNAL_OCCLUSION_QUERY UINT64_C(0x4000000000000000)
-#define BGFX_SUBMIT_RESERVED_MASK UINT8_C(0xff)
+#define BGFX_SUBMIT_INTERNAL_NONE UINT8_C(0x00)
+#define BGFX_SUBMIT_INTERNAL_INDEX32 UINT8_C(0x40)
#define BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE UINT8_C(0x80)
+#define BGFX_SUBMIT_INTERNAL_RESERVED_MASK UINT8_C(0xff)
#define BGFX_RENDERER_DIRECT3D9_NAME "Direct3D 9"
#define BGFX_RENDERER_DIRECT3D11_NAME "Direct3D 11"
@@ -1659,6 +1661,11 @@ namespace bgfx
return 0 != tmp;
}
+ bool isIndex16() const
+ {
+ return 0 == (m_submitFlags & BGFX_SUBMIT_INTERNAL_INDEX32);
+ }
+
Stream m_stream[BGFX_CONFIG_MAX_VERTEX_STREAMS];
uint64_t m_stateFlags;
uint64_t m_stencil;
@@ -1754,6 +1761,7 @@ namespace bgfx
{
String m_name;
uint32_t m_size;
+ uint16_t m_flags;
};
struct VertexBuffer
@@ -2097,11 +2105,11 @@ namespace bgfx
return num;
}
- uint32_t allocTransientIndexBuffer(uint32_t& _num)
+ uint32_t allocTransientIndexBuffer(uint32_t& _num, uint32_t _indexSize)
{
- uint32_t offset = bx::strideAlign(m_iboffset, sizeof(uint16_t) );
+ uint32_t offset = bx::strideAlign(m_iboffset, _indexSize);
uint32_t num = getAvailTransientIndexBuffer(_num);
- m_iboffset = offset + num*sizeof(uint16_t);
+ m_iboffset = offset + num*_indexSize;
_num = num;
return offset;
@@ -2417,12 +2425,13 @@ namespace bgfx
m_draw.m_numMatrices = uint16_t(bx::min(_cache+_num, BGFX_CONFIG_MAX_MATRIX_CACHE-1) - _cache);
}
- void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
+ void setIndexBuffer(IndexBufferHandle _handle, const IndexBuffer& _ib, uint32_t _firstIndex, uint32_t _numIndices)
{
BX_ASSERT(UINT8_MAX != m_draw.m_streamMask, "bgfx::setVertexCount was already called for this draw call.");
m_draw.m_startIndex = _firstIndex;
m_draw.m_numIndices = _numIndices;
m_draw.m_indexBuffer = _handle;
+ m_draw.m_submitFlags |= 0 == (_ib.m_flags & BGFX_BUFFER_INDEX32) ? BGFX_SUBMIT_INTERNAL_NONE : BGFX_SUBMIT_INTERNAL_INDEX32;
}
void setIndexBuffer(const DynamicIndexBuffer& _dib, uint32_t _firstIndex, uint32_t _numIndices)
@@ -2432,16 +2441,19 @@ namespace bgfx
m_draw.m_startIndex = _dib.m_startIndex + _firstIndex;
m_draw.m_numIndices = bx::min(_numIndices, _dib.m_size/indexSize);
m_draw.m_indexBuffer = _dib.m_handle;
+ m_draw.m_submitFlags |= 0 == (_dib.m_flags & BGFX_BUFFER_INDEX32) ? BGFX_SUBMIT_INTERNAL_NONE : BGFX_SUBMIT_INTERNAL_INDEX32;
}
void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices)
{
BX_ASSERT(UINT8_MAX != m_draw.m_streamMask, "bgfx::setVertexCount was already called for this draw call.");
- const uint32_t numIndices = bx::min(_numIndices, _tib->size/2);
+ const uint32_t indexSize = _tib->isIndex16 ? 2 : 4;
+ const uint32_t numIndices = bx::min(_numIndices, _tib->size/indexSize);
m_draw.m_indexBuffer = _tib->handle;
m_draw.m_startIndex = _tib->startIndex + _firstIndex;
m_draw.m_numIndices = numIndices;
- m_discard = 0 == numIndices;
+ m_draw.m_submitFlags |= _tib->isIndex16 ? BGFX_SUBMIT_INTERNAL_NONE : BGFX_SUBMIT_INTERNAL_INDEX32;
+ m_discard = 0 == numIndices;
}
void setVertexBuffer(
@@ -3106,7 +3118,8 @@ namespace bgfx
if (isValid(handle) )
{
IndexBuffer& ib = m_indexBuffers[handle.idx];
- ib.m_size = _mem->size;
+ ib.m_size = _mem->size;
+ ib.m_flags = _flags;
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateIndexBuffer);
cmdbuf.write(handle);
@@ -3703,18 +3716,21 @@ namespace bgfx
BX_ALIGNED_FREE(g_allocator, _tib, 16);
}
- BGFX_API_FUNC(void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num) )
+ BGFX_API_FUNC(void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num, bool _index32) )
{
BGFX_MUTEX_SCOPE(m_resourceApiLock);
- uint32_t offset = m_submit->allocTransientIndexBuffer(_num);
+ const bool isIndex16 = !_index32;
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const uint32_t offset = m_submit->allocTransientIndexBuffer(_num, indexSize);
TransientIndexBuffer& tib = *m_submit->m_transientIb;
_tib->data = &tib.data[offset];
- _tib->size = _num * 2;
+ _tib->size = _num * indexSize;
_tib->handle = tib.handle;
- _tib->startIndex = bx::strideAlign(offset, 2)/2;
+ _tib->startIndex = bx::strideAlign(offset, indexSize) / indexSize;
+ _tib->isIndex16 = isIndex16;
}
TransientVertexBuffer* createTransientVertexBuffer(uint32_t _size, const VertexLayout* _layout = NULL)
diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp
index faf1460df..0439384cd 100644
--- a/src/renderer_d3d11.cpp
+++ b/src/renderer_d3d11.cpp
@@ -6192,16 +6192,18 @@ namespace bgfx { namespace d3d11
}
}
- if (currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx)
+ if (currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx
+ || currentState.isIndex16() != draw.isIndex16() )
{
currentState.m_indexBuffer = draw.m_indexBuffer;
+ currentState.m_submitFlags = draw.m_submitFlags;
uint16_t handle = draw.m_indexBuffer.idx;
if (kInvalidHandle != handle)
{
const IndexBufferD3D11& ib = m_indexBuffers[handle];
deviceCtx->IASetIndexBuffer(ib.m_ptr
- , 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT
+ , draw.isIndex16() ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT
, 0
);
}
diff --git a/src/renderer_d3d12.cpp b/src/renderer_d3d12.cpp
index 4f4818823..76c2cbb12 100644
--- a/src/renderer_d3d12.cpp
+++ b/src/renderer_d3d12.cpp
@@ -4069,8 +4069,12 @@ namespace bgfx { namespace d3d12
BufferD3D12& ib = s_renderD3D12->m_indexBuffers[_draw.m_indexBuffer.idx];
ib.setState(_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
- const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
- const uint32_t indexSize = hasIndex16 ? 2 : 4;
+ const bool isIndex16 = _draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const DXGI_FORMAT indexFormat = isIndex16
+ ? DXGI_FORMAT_R16_UINT
+ : DXGI_FORMAT_R32_UINT
+ ;
numIndices = UINT32_MAX == _draw.m_numIndices
? ib.m_size / indexSize
@@ -4080,10 +4084,7 @@ namespace bgfx { namespace d3d12
D3D12_INDEX_BUFFER_VIEW ibv;
ibv.BufferLocation = ib.m_gpuVA;
ibv.SizeInBytes = ib.m_size;
- ibv.Format = hasIndex16
- ? DXGI_FORMAT_R16_UINT
- : DXGI_FORMAT_R32_UINT
- ;
+ ibv.Format = indexFormat;
_commandList->IASetIndexBuffer(&ibv);
_commandList->ExecuteIndirect(
@@ -4148,8 +4149,9 @@ namespace bgfx { namespace d3d12
BufferD3D12& ib = s_renderD3D12->m_indexBuffers[_draw.m_indexBuffer.idx];
ib.setState(_commandList, D3D12_RESOURCE_STATE_GENERIC_READ);
- const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
- const uint32_t indexSize = hasIndex16 ? 2 : 4;
+ const bool isIndex16 = _draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const DXGI_FORMAT indexFormat = isIndex16 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
numIndices = UINT32_MAX == _draw.m_numIndices
? ib.m_size / indexSize
@@ -4160,10 +4162,7 @@ namespace bgfx { namespace d3d12
cmd.cbv = _cbv;
cmd.ibv.BufferLocation = ib.m_gpuVA;
cmd.ibv.SizeInBytes = ib.m_size;
- cmd.ibv.Format = hasIndex16
- ? DXGI_FORMAT_R16_UINT
- : DXGI_FORMAT_R32_UINT
- ;
+ cmd.ibv.Format = indexFormat;
uint32_t numVertices;
uint8_t numStreams = fill(_commandList, cmd.vbv, _draw, numVertices);
diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp
index b9f61fbda..bb63e0c22 100644
--- a/src/renderer_d3d9.cpp
+++ b/src/renderer_d3d9.cpp
@@ -4310,7 +4310,8 @@ namespace bgfx { namespace d3d9
if (UINT32_MAX == draw.m_numIndices)
{
const IndexBufferD3D9& ib = m_indexBuffers[draw.m_indexBuffer.idx];
- const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
+ const bool isIndex16 = draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
numIndices = ib.m_size/indexSize;
numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
numInstances = draw.m_numInstances;
diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp
index 5f5cbf569..ba6280b8b 100644
--- a/src/renderer_gl.cpp
+++ b/src/renderer_gl.cpp
@@ -8037,10 +8037,10 @@ namespace bgfx { namespace gl
if (isValid(draw.m_indexBuffer) )
{
- const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
- const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
- const uint32_t indexSize = hasIndex16 ? 2 : 4;
- const GLenum indexFormat = hasIndex16
+ const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
+ const bool isIndex16 = draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const GLenum indexFormat = isIndex16
? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
@@ -8061,10 +8061,10 @@ namespace bgfx { namespace gl
}
else if (prim.m_min <= draw.m_numIndices)
{
- numIndices = draw.m_numIndices;
+ numIndices = draw.m_numIndices;
numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
- numInstances = draw.m_numInstances;
- numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
+ numInstances = draw.m_numInstances;
+ numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
GL_CHECK(glDrawElementsInstanced(prim.m_type
, numIndices
@@ -8077,8 +8077,8 @@ namespace bgfx { namespace gl
else
{
numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
- numInstances = draw.m_numInstances;
- numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
+ numInstances = draw.m_numInstances;
+ numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
GL_CHECK(glDrawArraysInstanced(prim.m_type
, 0
diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm
index 28694d835..1fb736c52 100644
--- a/src/renderer_mtl.mm
+++ b/src/renderer_mtl.mm
@@ -4591,8 +4591,9 @@ namespace bgfx { namespace mtl
if (isValid(draw.m_indexBuffer) )
{
- const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx];
- MTLIndexType indexType = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
+ const bool isIndex16 = draw.isIndex16();
+ const MTLIndexType indexFormat = isIndex16 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
+ const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx];
numDrawIndirect = UINT16_MAX == draw.m_numIndirect
? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
@@ -4601,7 +4602,7 @@ namespace bgfx { namespace mtl
for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
{
- rce.drawIndexedPrimitives(prim.m_type,indexType, ib.m_ptr, 0, vb.m_ptr, (draw.m_startIndirect + ii )* BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
+ rce.drawIndexedPrimitives(prim.m_type, indexFormat, ib.m_ptr, 0, vb.m_ptr, (draw.m_startIndirect + ii )* BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
}
}
else
@@ -4613,7 +4614,7 @@ namespace bgfx { namespace mtl
for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
{
- rce.drawPrimitives(prim.m_type,vb.m_ptr, (draw.m_startIndirect + ii) * BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
+ rce.drawPrimitives(prim.m_type, vb.m_ptr, (draw.m_startIndirect + ii) * BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
}
}
}
@@ -4621,28 +4622,28 @@ namespace bgfx { namespace mtl
{
if (isValid(draw.m_indexBuffer) )
{
- const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx];
- MTLIndexType indexType = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
+ const bool isIndex16 = draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const MTLIndexType indexFormat = isIndex16 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
+ const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx];
if (UINT32_MAX == draw.m_numIndices)
{
- const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
numIndices = ib.m_size/indexSize;
numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
numInstances = draw.m_numInstances;
numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
- rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.m_ptr, 0, draw.m_numInstances);
+ rce.drawIndexedPrimitives(prim.m_type, numIndices, indexFormat, ib.m_ptr, 0, draw.m_numInstances);
}
else if (prim.m_min <= draw.m_numIndices)
{
- const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
numIndices = draw.m_numIndices;
numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
numInstances = draw.m_numInstances;
numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
- rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.m_ptr, draw.m_startIndex * indexSize,numInstances);
+ rce.drawIndexedPrimitives(prim.m_type, numIndices, indexFormat, ib.m_ptr, draw.m_startIndex * indexSize,numInstances);
}
}
else
diff --git a/src/renderer_vk.cpp b/src/renderer_vk.cpp
index 62907b0e4..6eb63c8fc 100644
--- a/src/renderer_vk.cpp
+++ b/src/renderer_vk.cpp
@@ -7184,10 +7184,10 @@ VK_DESTROY
}
else
{
- BufferVK& ib = m_indexBuffers[draw.m_indexBuffer.idx];
-
- const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
- const uint32_t indexSize = hasIndex16 ? 2 : 4;
+ const bool isIndex16 = draw.isIndex16();
+ const uint32_t indexSize = isIndex16 ? 2 : 4;
+ const VkIndexType indexFormat = isIndex16 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32;
+ const BufferVK& ib = m_indexBuffers[draw.m_indexBuffer.idx];
numIndices = UINT32_MAX == draw.m_numIndices
? ib.m_size / indexSize
@@ -7197,9 +7197,7 @@ VK_DESTROY
vkCmdBindIndexBuffer(m_commandBuffer
, ib.m_buffer
, 0
- , hasIndex16
- ? VK_INDEX_TYPE_UINT16
- : VK_INDEX_TYPE_UINT32
+ , indexFormat
);
vkCmdDrawIndexed(m_commandBuffer
, numIndices
diff --git a/src/renderer_webgpu.cpp b/src/renderer_webgpu.cpp
index 038670dd9..2a9a198ec 100644
--- a/src/renderer_webgpu.cpp
+++ b/src/renderer_webgpu.cpp
@@ -1782,7 +1782,7 @@ namespace bgfx { namespace webgpu
, FrameBufferHandle _fbh
, uint8_t _numStreams
, const VertexLayout** _vertexDecls
- , bool _index32
+ , bool _isIndex16
, ProgramHandle _program
, uint8_t _numInstanceData
)
@@ -2103,7 +2103,7 @@ namespace bgfx { namespace webgpu
input.vertexBuffers[stream].attributes = &input.attributes[firstAttrib];
}
- input.desc.indexFormat = _index32 ? wgpu::IndexFormat::Uint32 : wgpu::IndexFormat::Uint16;
+ input.desc.indexFormat = _isIndex16 ? wgpu::IndexFormat::Uint16 : wgpu::IndexFormat::Uint32;
pd.desc.vertexState = &input.desc;
@@ -2122,7 +2122,7 @@ namespace bgfx { namespace webgpu
, uint32_t _rgba
, FrameBufferHandle _fbh
, VertexLayoutHandle _declHandle
- , bool _index32
+ , bool _isIndex16
, ProgramHandle _program
, uint8_t _numInstanceData
)
@@ -2135,7 +2135,7 @@ namespace bgfx { namespace webgpu
, _fbh
, 1
, &decl
- , _index32
+ , _isIndex16
, _program
, _numInstanceData
);
@@ -4478,14 +4478,6 @@ namespace bgfx { namespace webgpu
rce.SetVertexBuffer(idx, vb.m_ptr, offset);
}
- bool index32 = false;
-
- if (isValid(draw.m_indexBuffer))
- {
- const IndexBufferWgpu& ib = m_indexBuffers[draw.m_indexBuffer.idx];
- index32 = 0 != (ib.m_flags & BGFX_BUFFER_INDEX32);
- }
-
currentState.m_numVertices = numVertices;
if (!isValid(currentProgram))
@@ -4505,7 +4497,7 @@ namespace bgfx { namespace webgpu
, fbh
, numStreams
, decls
- , index32
+ , draw.isIndex16()
, currentProgram
, uint8_t(draw.m_instanceDataStride / 16)
);
@@ -4646,8 +4638,7 @@ namespace bgfx { namespace webgpu
if (isValid(draw.m_indexBuffer) )
{
const IndexBufferWgpu& ib = m_indexBuffers[draw.m_indexBuffer.idx];
-
- const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
+ const uint32_t indexSize = draw.isIndex16() ? 2 : 4;
if (UINT32_MAX == draw.m_numIndices)
{