From 977188e9deed476cddcde49a59f6323ce16c609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sat, 30 May 2020 21:38:41 -0700 Subject: [PATCH] Added weldVertices 32-bit indices support. --- bindings/cs/bgfx.cs | 3 ++- bindings/d/funcs.d | 6 ++++-- bindings/d/types.d | 2 +- include/bgfx/bgfx.h | 8 ++++--- include/bgfx/c99/bgfx.h | 5 +++-- include/bgfx/defines.h | 2 +- scripts/bgfx.idl | 19 ++++++++-------- src/bgfx.cpp | 5 +++++ src/bgfx.idl.inl | 4 ++-- src/version.h | 4 ++-- src/vertexlayout.cpp | 48 ++++++++++++++++++++++++++--------------- src/vertexlayout.h | 3 +++ 12 files changed, 69 insertions(+), 40 deletions(-) diff --git a/bindings/cs/bgfx.cs b/bindings/cs/bgfx.cs index 4aa3ae04a..fca974045 100644 --- a/bindings/cs/bgfx.cs +++ b/bindings/cs/bgfx.cs @@ -2352,10 +2352,11 @@ public static partial class bgfx /// Vertex stream layout. /// Vertex stream. /// Number of vertices in vertex stream. + /// Set to `true` if input indices are 32-bit. /// Error tolerance for vertex position comparison. /// [DllImport(DllName, EntryPoint="bgfx_weld_vertices", CallingConvention = CallingConvention.Cdecl)] - public static extern unsafe ushort weld_vertices(ushort* _output, VertexLayout* _layout, void* _data, ushort _num, float _epsilon); + public static extern unsafe uint weld_vertices(void* _output, VertexLayout* _layout, void* _data, uint _num, bool _index32, float _epsilon); /// /// Convert index buffer for use with different primitive topologies. diff --git a/bindings/d/funcs.d b/bindings/d/funcs.d index dc8201e3f..6b6fd1292 100644 --- a/bindings/d/funcs.d +++ b/bindings/d/funcs.d @@ -115,9 +115,10 @@ version(BindBgfx_Static) * _layout = Vertex stream layout. * _data = Vertex stream. * _num = Number of vertices in vertex stream. + * _index32 = Set to `true` if input indices are 32-bit. * _epsilon = Error tolerance for vertex position comparison. */ - ushort bgfx_weld_vertices(ushort* _output, const(bgfx_vertex_layout_t)* _layout, const(void)* _data, ushort _num, float _epsilon); + uint bgfx_weld_vertices(void* _output, const(bgfx_vertex_layout_t)* _layout, const(void)* _data, uint _num, bool _index32, float _epsilon); /** * Convert index buffer for use with different primitive topologies. @@ -2198,9 +2199,10 @@ else * _layout = Vertex stream layout. * _data = Vertex stream. * _num = Number of vertices in vertex stream. + * _index32 = Set to `true` if input indices are 32-bit. * _epsilon = Error tolerance for vertex position comparison. */ - alias da_bgfx_weld_vertices = ushort function(ushort* _output, const(bgfx_vertex_layout_t)* _layout, const(void)* _data, ushort _num, float _epsilon); + alias da_bgfx_weld_vertices = uint function(void* _output, const(bgfx_vertex_layout_t)* _layout, const(void)* _data, uint _num, bool _index32, float _epsilon); da_bgfx_weld_vertices bgfx_weld_vertices; /** diff --git a/bindings/d/types.d b/bindings/d/types.d index 65cfd9935..208c36ada 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 = 106; +enum uint BGFX_API_VERSION = 107; alias bgfx_view_id_t = ushort; diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index 7b619c006..f27f562f5 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -1835,16 +1835,18 @@ namespace bgfx /// @param[in] _layout Vertex stream layout. /// @param[in] _data Vertex stream. /// @param[in] _num Number of vertices in vertex stream. + /// @param[in] _index32 Set to `true` if input indices are 32-bit. /// @param[in] _epsilon Error tolerance for vertex position comparison. /// @returns Number of unique vertices after vertex welding. /// /// @attention C99 equivalent is `bgfx_weld_vertices`. /// - uint16_t weldVertices( - uint16_t* _output + uint32_t weldVertices( + void* _output , const VertexLayout& _layout , const void* _data - , uint16_t _num + , uint32_t _num + , bool _index32 , float _epsilon = 0.001f ); diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 5e7457784..2e04803b6 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -1011,12 +1011,13 @@ BGFX_C_API void bgfx_vertex_convert(const bgfx_vertex_layout_t * _dstLayout, voi * @param[in] _layout Vertex stream layout. * @param[in] _data Vertex stream. * @param[in] _num Number of vertices in vertex stream. + * @param[in] _index32 Set to `true` if input indices are 32-bit. * @param[in] _epsilon Error tolerance for vertex position comparison. * * @returns Number of unique vertices after vertex welding. * */ -BGFX_C_API uint16_t bgfx_weld_vertices(uint16_t* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint16_t _num, float _epsilon); +BGFX_C_API uint32_t bgfx_weld_vertices(void* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon); /** * Convert index buffer for use with different primitive topologies. @@ -3469,7 +3470,7 @@ struct bgfx_interface_vtbl void (*vertex_pack)(const float _input[4], bool _inputNormalized, bgfx_attrib_t _attr, const bgfx_vertex_layout_t * _layout, void* _data, uint32_t _index); void (*vertex_unpack)(float _output[4], bgfx_attrib_t _attr, const bgfx_vertex_layout_t * _layout, const void* _data, uint32_t _index); void (*vertex_convert)(const bgfx_vertex_layout_t * _dstLayout, void* _dstData, const bgfx_vertex_layout_t * _srcLayout, const void* _srcData, uint32_t _num); - uint16_t (*weld_vertices)(uint16_t* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint16_t _num, float _epsilon); + uint32_t (*weld_vertices)(void* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon); uint32_t (*topology_convert)(bgfx_topology_convert_t _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32); void (*topology_sort_tri_list)(bgfx_topology_sort_t _sort, void* _dst, uint32_t _dstSize, const float _dir[3], const float _pos[3], const void* _vertices, uint32_t _stride, const void* _indices, uint32_t _numIndices, bool _index32); uint8_t (*get_supported_renderers)(uint8_t _max, bgfx_renderer_type_t* _enum); diff --git a/include/bgfx/defines.h b/include/bgfx/defines.h index 4c9b8d5f0..8de44bb75 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(106) +#define BGFX_API_VERSION UINT32_C(107) /** * 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 2388f0120..1e9dc6829 100644 --- a/scripts/bgfx.idl +++ b/scripts/bgfx.idl @@ -1,7 +1,7 @@ -- vim: syntax=lua -- bgfx interface -version(106) +version(107) typedef "bool" typedef "char" @@ -1060,14 +1060,15 @@ func.vertexConvert --- Weld vertices. func.weldVertices - "uint16_t" --- Number of unique vertices after vertex welding. - .output "uint16_t*" --- Welded vertices remapping table. The size of buffer - --- must be the same as number of vertices. - .layout "const VertexLayout &" --- Vertex stream layout. - .data "const void*" --- Vertex stream. - .num "uint16_t" --- Number of vertices in vertex stream. - .epsilon "float" --- Error tolerance for vertex position comparison. - { default = "0.001f" } + "uint32_t" --- Number of unique vertices after vertex welding. + .output "void*" --- Welded vertices remapping table. The size of buffer + --- must be the same as number of vertices. + .layout "const VertexLayout &" --- Vertex stream layout. + .data "const void*" --- Vertex stream. + .num "uint32_t" --- Number of vertices in vertex stream. + .index32 "bool" --- Set to `true` if input indices are 32-bit. + .epsilon "float" --- Error tolerance for vertex position comparison. + { default = "0.001f" } --- Convert index buffer for use with different primitive topologies. func.topologyConvert diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 25af01ac3..850f1b701 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -3254,6 +3254,11 @@ namespace bgfx flushTextureUpdateBatch(_cmdbuf); } + uint32_t weldVertices(void* _output, const VertexLayout& _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon) + { + return weldVertices(_output, _layout, _data, _num, _index32, _epsilon, g_allocator); + } + uint32_t topologyConvert(TopologyConvert::Enum _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32) { return topologyConvert(_conversion, _dst, _dstSize, _indices, _numIndices, _index32, g_allocator); diff --git a/src/bgfx.idl.inl b/src/bgfx.idl.inl index 4aa0ea12c..965dd67be 100644 --- a/src/bgfx.idl.inl +++ b/src/bgfx.idl.inl @@ -114,10 +114,10 @@ BGFX_C_API void bgfx_vertex_convert(const bgfx_vertex_layout_t * _dstLayout, voi bgfx::vertexConvert(dstLayout, _dstData, srcLayout, _srcData, _num); } -BGFX_C_API uint16_t bgfx_weld_vertices(uint16_t* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint16_t _num, float _epsilon) +BGFX_C_API uint32_t bgfx_weld_vertices(void* _output, const bgfx_vertex_layout_t * _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon) { const bgfx::VertexLayout & layout = *(const bgfx::VertexLayout *)_layout; - return bgfx::weldVertices(_output, layout, _data, _num, _epsilon); + return bgfx::weldVertices(_output, layout, _data, _num, _index32, _epsilon); } BGFX_C_API uint32_t bgfx_topology_convert(bgfx_topology_convert_t _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32) diff --git a/src/version.h b/src/version.h index f70d5853e..86bf1444c 100644 --- a/src/version.h +++ b/src/version.h @@ -9,5 +9,5 @@ * */ -#define BGFX_REV_NUMBER 7218 -#define BGFX_REV_SHA1 "1c063fdc67d601fc90aea7b8e9493924880a33f5" +#define BGFX_REV_NUMBER 7224 +#define BGFX_REV_SHA1 "dfaf8168227fedde5a8ad2503ed6e33013bdf49f" diff --git a/src/vertexlayout.cpp b/src/vertexlayout.cpp index 9cf8fcf4f..c3f28d81d 100644 --- a/src/vertexlayout.cpp +++ b/src/vertexlayout.cpp @@ -725,22 +725,23 @@ namespace bgfx return xx*xx + yy*yy + zz*zz; } - uint16_t weldVerticesRef(uint16_t* _output, const VertexLayout& _layout, const void* _data, uint16_t _num, float _epsilon) + template + static IndexT weldVerticesRef(IndexT* _output, const VertexLayout& _layout, const void* _data, uint32_t _num, float _epsilon) { // Brute force slow vertex welding... const float epsilonSq = _epsilon*_epsilon; uint32_t numVertices = 0; - bx::memSet(_output, 0xff, _num*sizeof(uint16_t) ); + bx::memSet(_output, 0xff, _num*sizeof(IndexT) ); for (uint32_t ii = 0; ii < _num; ++ii) { - if (UINT16_MAX != _output[ii]) + if (IndexT(-1) != _output[ii]) { continue; } - _output[ii] = (uint16_t)ii; + _output[ii] = (IndexT)ii; ++numVertices; float pos[4]; @@ -748,7 +749,7 @@ namespace bgfx for (uint32_t jj = 0; jj < _num; ++jj) { - if (UINT16_MAX != _output[jj]) + if (IndexT(-1) != _output[jj]) { continue; } @@ -758,15 +759,16 @@ namespace bgfx if (sqLength(test, pos) < epsilonSq) { - _output[jj] = (uint16_t)ii; + _output[jj] = IndexT(ii); } } } - return (uint16_t)numVertices; + return IndexT(numVertices); } - uint16_t weldVertices(uint16_t* _output, const VertexLayout& _layout, const void* _data, uint16_t _num, float _epsilon) + template + static IndexT weldVertices(IndexT* _output, const VertexLayout& _layout, const void* _data, uint32_t _num, float _epsilon, bx::AllocatorI* _allocator) { const uint32_t hashSize = bx::uint32_nextpow2(_num); const uint32_t hashMask = hashSize-1; @@ -774,11 +776,11 @@ namespace bgfx uint32_t numVertices = 0; - const uint32_t size = sizeof(uint16_t)*(hashSize + _num); - uint16_t* hashTable = (uint16_t*)alloca(size); + const uint32_t size = sizeof(IndexT)*(hashSize + _num); + IndexT* hashTable = (IndexT*)BX_ALLOC(_allocator, size); bx::memSet(hashTable, 0xff, size); - uint16_t* next = hashTable + hashSize; + IndexT* next = hashTable + hashSize; for (uint32_t ii = 0; ii < _num; ++ii) { @@ -786,8 +788,8 @@ namespace bgfx vertexUnpack(pos, Attrib::Position, _layout, _data, ii); uint32_t hashValue = bx::hash(pos, 3*sizeof(float) ) & hashMask; - uint16_t offset = hashTable[hashValue]; - for (; UINT16_MAX != offset; offset = next[offset]) + IndexT offset = hashTable[hashValue]; + for (; IndexT(-1) != offset; offset = next[offset]) { float test[4]; vertexUnpack(test, Attrib::Position, _layout, _data, _output[offset]); @@ -799,16 +801,28 @@ namespace bgfx } } - if (UINT16_MAX == offset) + if (IndexT(-1) == offset) { - _output[ii] = (uint16_t)ii; + _output[ii] = IndexT(ii); next[ii] = hashTable[hashValue]; - hashTable[hashValue] = (uint16_t)ii; + hashTable[hashValue] = IndexT(ii); numVertices++; } } - return (uint16_t)numVertices; + BX_FREE(_allocator, hashTable); + + return IndexT(numVertices); + } + + uint32_t weldVertices(void* _output, const VertexLayout& _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon, bx::AllocatorI* _allocator) + { + if (_index32) + { + return weldVertices( (uint32_t*)_output, _layout, _data, _num, _epsilon, _allocator); + } + + return weldVertices( (uint16_t*)_output, _layout, _data, _num, _epsilon, _allocator); } } // namespace bgfx diff --git a/src/vertexlayout.h b/src/vertexlayout.h index 7505eacdc..36e19bcd5 100644 --- a/src/vertexlayout.h +++ b/src/vertexlayout.h @@ -35,6 +35,9 @@ namespace bgfx /// int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err = NULL); + /// + uint32_t weldVertices(void* _output, const VertexLayout& _layout, const void* _data, uint32_t _num, bool _index32, float _epsilon, bx::AllocatorI* _allocator); + } // namespace bgfx #endif // BGFX_VERTEXDECL_H_HEADER_GUARD