Skip to content

Commit

Permalink
Exposed topology API.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Feb 23, 2016
1 parent e94ac9d commit d4d4d41
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/23-vectordisplay/vectordisplay.cpp
Expand Up @@ -186,7 +186,7 @@ void VectorDisplay::endFrame()

bgfx::setTexture(0, s_texColor, m_lineTexId);

bgfx::setVertexBuffer(m_vertexBuffers[i], m_vertexBuffersSize[i]); // explicitly feed vertex number!
bgfx::setVertexBuffer(m_vertexBuffers[i], 0, m_vertexBuffersSize[i]); // explicitly feed vertex number!

bgfx::setState(0
| BGFX_STATE_RGB_WRITE
Expand Down
4 changes: 2 additions & 2 deletions examples/common/font/text_buffer_manager.cpp
Expand Up @@ -822,8 +822,8 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id,
);
}

bgfx::setVertexBuffer(vbh, bc.textBuffer->getVertexCount() );
bgfx::setIndexBuffer(ibh, bc.textBuffer->getIndexCount() );
bgfx::setVertexBuffer(vbh, 0, bc.textBuffer->getVertexCount() );
bgfx::setIndexBuffer(ibh, 0, bc.textBuffer->getIndexCount() );
}
break;

Expand Down
88 changes: 76 additions & 12 deletions include/bgfx/bgfx.h
Expand Up @@ -264,24 +264,45 @@ namespace bgfx
/// Backbuffer ratios:
enum Enum
{
Equal,
Half,
Quarter,
Eighth,
Sixteenth,
Double,
Equal, //!< Equal to backbuffer.
Half, //!< One half size of backbuffer.
Quarter, //!< One quarter size of backbuffer.
Eighth, //!< One eighth size of backbuffer.
Sixteenth, //!< One sixteenth size of backbuffer.
Double, //!< Double size of backbuffer.

Count
};
};

/// Occlusion query result.
///
/// @attention C99 equivalent is `bgfx_occlusion_query_result_t`.
///
struct OcclusionQueryResult
{
enum Enum
{
Invisible,
Visible,
NoResult,
Invisible, //!< Query failed test.
Visible, //!< Query passed test.
NoResult, //!< Query result is not available yet.

Count
};
};

/// Topology conversion function.
///
/// @attention C99 equivalent is `bgfx_topology_convert_t`.
///
struct TopologyConvert
{
enum Enum
{
TriListFlipWinding, //!< Flip winding order of triangle list.
TriListToLineList, //!< Convert triangle list to line list.
TriStripToTriList, //!< Convert triangle strip to triangle list.
LineStripToLineList, //!< Convert line strip to line list.

Count
};
Expand Down Expand Up @@ -703,6 +724,24 @@ namespace bgfx
///
uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon = 0.001f);

/// Convert index buffer for use with different primitive topologies.
///
/// @param[in] _conversion Conversion type, see `TopologyConvert::Enum`.
/// @param[in] _dst Destination index buffer. If this argument it NULL
/// function will return number of indices after conversion.
/// @param[in] _dstSize Destination index buffer in bytes. It must be
/// large enough to contain output indices. If destination size is
/// insufficient index buffer will be truncated.
/// @param[in] _indices Source indices.
/// @param[in] _numIndices Number of input indices.
/// @param[in] _index32 Set to `true` if input indices are 32-bit.
///
/// @returns Number of output indices after conversion.
///
/// @attention C99 equivalent is `bgfx_topology_convert`.
///
uint32_t topologyConvert(TopologyConvert::Enum _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32);

/// Swizzle RGBA8 image to BGRA8.
///
/// @param[in] _width Width of input image (pixels).
Expand Down Expand Up @@ -1861,6 +1900,14 @@ namespace bgfx
///
void setUniform(UniformHandle _handle, const void* _value, uint16_t _num = 1);

/// Set index buffer for draw primitive.
///
/// @param[in] _handle Index buffer.
///
/// @attention C99 equivalent is `bgfx_set_index_buffer`.
///
void setIndexBuffer(IndexBufferHandle _handle);

/// Set index buffer for draw primitive.
///
/// @param[in] _handle Index buffer.
Expand All @@ -1869,7 +1916,15 @@ namespace bgfx
///
/// @attention C99 equivalent is `bgfx_set_index_buffer`.
///
void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);

/// Set index buffer for draw primitive.
///
/// @param[in] _handle Dynamic index buffer.
///
/// @attention C99 equivalent is `bgfx_set_dynamic_index_buffer`.
///
void setIndexBuffer(DynamicIndexBufferHandle _handle);

/// Set index buffer for draw primitive.
///
Expand All @@ -1879,7 +1934,7 @@ namespace bgfx
///
/// @attention C99 equivalent is `bgfx_set_dynamic_index_buffer`.
///
void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex = 0, uint32_t _numIndices = UINT32_MAX);
void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices);

/// Set index buffer for draw primitive.
///
Expand Down Expand Up @@ -1920,11 +1975,20 @@ namespace bgfx
/// Set vertex buffer for draw primitive.
///
/// @param[in] _handle Dynamic vertex buffer.
///
/// @attention C99 equivalent is `bgfx_set_dynamic_vertex_buffer`.
///
void setVertexBuffer(DynamicVertexBufferHandle _handle);

/// Set vertex buffer for draw primitive.
///
/// @param[in] _handle Dynamic vertex buffer.
/// @param[in] _startVertex First vertex to render.
/// @param[in] _numVertices Number of vertices to render.
///
/// @attention C99 equivalent is `bgfx_set_dynamic_vertex_buffer`.
///
void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices = UINT32_MAX);
void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices);

/// Set vertex buffer for draw primitive.
///
Expand Down
2 changes: 1 addition & 1 deletion include/bgfx/bgfxdefines.h
Expand Up @@ -6,7 +6,7 @@
#ifndef BGFX_DEFINES_H_HEADER_GUARD
#define BGFX_DEFINES_H_HEADER_GUARD

#define BGFX_API_VERSION UINT32_C(9)
#define BGFX_API_VERSION UINT32_C(10)

///
#define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001) //!< Enable RGB write.
Expand Down
13 changes: 12 additions & 1 deletion include/bgfx/c99/bgfx.h
Expand Up @@ -220,6 +220,17 @@ typedef enum bgfx_occlusion_query_result

} bgfx_occlusion_query_result_t;

typedef enum bgfx_topology_convert
{
BGFX_TOPOLOGY_CONVERT_TRI_LIST_FLIP_WINDING,
BGFX_TOPOLOGY_CONVERT_TRI_LIST_TO_LINE_LIST,
BGFX_TOPOLOGY_CONVERT_TRI_STRIP_TO_TRI_LIST,
BGFX_TOPOLOGY_CONVERT_LINE_STRIP_TO_LINE_LIST,

BGFX_TOPOLOGY_CONVERT_COUNT

} bgfx_topology_convert_t;

#define BGFX_HANDLE_T(_name) \
typedef struct _name { uint16_t idx; } _name##_t

Expand Down Expand Up @@ -753,7 +764,7 @@ BGFX_C_API void bgfx_set_transient_index_buffer(const bgfx_transient_index_buffe
BGFX_C_API void bgfx_set_vertex_buffer(bgfx_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices);

/**/
BGFX_C_API void bgfx_set_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _numVertices);
BGFX_C_API void bgfx_set_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices);

/**/
BGFX_C_API void bgfx_set_transient_vertex_buffer(const bgfx_transient_vertex_buffer_t* _tvb, uint32_t _startVertex, uint32_t _numVertices);
Expand Down
3 changes: 2 additions & 1 deletion include/bgfx/c99/bgfxplatform.h
Expand Up @@ -77,6 +77,7 @@ typedef struct bgfx_interface_vtbl
void (*vertex_unpack)(float _output[4], bgfx_attrib_t _attr, const bgfx_vertex_decl_t* _decl, const void* _data, uint32_t _index);
void (*vertex_convert)(const bgfx_vertex_decl_t* _destDecl, void* _destData, const bgfx_vertex_decl_t* _srcDecl, const void* _srcData, uint32_t _num);
uint16_t (*weld_vertices)(uint16_t* _output, const bgfx_vertex_decl_t* _decl, const void* _data, uint16_t _num, 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 (*image_swizzle_bgra8)(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
void (*image_rgba8_downsample_2x2)(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
uint8_t (*get_supported_renderers)(bgfx_renderer_type_t _enum[BGFX_RENDERER_TYPE_COUNT]);
Expand Down Expand Up @@ -170,7 +171,7 @@ typedef struct bgfx_interface_vtbl
void (*set_dynamic_index_buffer)(bgfx_dynamic_index_buffer_handle_t _handle, uint32_t _firstIndex, uint32_t _numIndices);
void (*set_transient_index_buffer)(const bgfx_transient_index_buffer_t* _tib, uint32_t _firstIndex, uint32_t _numIndices);
void (*set_vertex_buffer)(bgfx_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices);
void (*set_dynamic_vertex_buffer)(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _numVertices);
void (*set_dynamic_vertex_buffer)(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices);
void (*set_transient_vertex_buffer)(const bgfx_transient_vertex_buffer_t* _tvb, uint32_t _startVertex, uint32_t _numVertices);
void (*set_instance_data_buffer)(const bgfx_instance_data_buffer_t* _idb, uint32_t _num);
void (*set_instance_data_from_vertex_buffer)(bgfx_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _num);
Expand Down
80 changes: 59 additions & 21 deletions src/bgfx.cpp
Expand Up @@ -19,6 +19,7 @@
#endif // BGFX_CONFIG_PROFILER_REMOTERY_BUILD_LIB

#include <bx/crtimpl.h>
#include "topology.h"

namespace bgfx
{
Expand Down Expand Up @@ -2376,6 +2377,11 @@ namespace bgfx
flushTextureUpdateBatch(_cmdbuf);
}

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);
}

uint8_t getSupportedRenderers(RendererType::Enum _enum[RendererType::Count])
{
uint8_t num = 0;
Expand Down Expand Up @@ -3396,12 +3402,22 @@ namespace bgfx
s_ctx->setUniform(_handle, _value, _num);
}

void setIndexBuffer(IndexBufferHandle _handle)
{
setIndexBuffer(_handle, 0, UINT32_MAX);
}

void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
{
BGFX_CHECK_MAIN_THREAD();
s_ctx->setIndexBuffer(_handle, _firstIndex, _numIndices);
}

void setIndexBuffer(DynamicIndexBufferHandle _handle)
{
setIndexBuffer(_handle, 0, UINT32_MAX);
}

void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
{
BGFX_CHECK_MAIN_THREAD();
Expand Down Expand Up @@ -3432,10 +3448,15 @@ namespace bgfx
s_ctx->setVertexBuffer(_handle, _startVertex, _numVertices);
}

void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices)
void setVertexBuffer(DynamicVertexBufferHandle _handle)
{
setVertexBuffer(_handle, 0, UINT32_MAX);
}

void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices)
{
BGFX_CHECK_MAIN_THREAD();
s_ctx->setVertexBuffer(_handle, _numVertices);
s_ctx->setVertexBuffer(_handle, _startVertex, _numVertices);
}

void setVertexBuffer(const TransientVertexBuffer* _tvb)
Expand Down Expand Up @@ -3630,23 +3651,34 @@ BX_STATIC_ASSERT(FLAGS_MASK_TEST(0

#undef FLAGS_MASK_TEST

BX_STATIC_ASSERT(bgfx::Fatal::Count == bgfx::Fatal::Enum(BGFX_FATAL_COUNT) );
BX_STATIC_ASSERT(bgfx::RendererType::Count == bgfx::RendererType::Enum(BGFX_RENDERER_TYPE_COUNT) );
BX_STATIC_ASSERT(bgfx::Attrib::Count == bgfx::Attrib::Enum(BGFX_ATTRIB_COUNT) );
BX_STATIC_ASSERT(bgfx::AttribType::Count == bgfx::AttribType::Enum(BGFX_ATTRIB_TYPE_COUNT) );
BX_STATIC_ASSERT(bgfx::TextureFormat::Count == bgfx::TextureFormat::Enum(BGFX_TEXTURE_FORMAT_COUNT) );
BX_STATIC_ASSERT(bgfx::UniformType::Count == bgfx::UniformType::Enum(BGFX_UNIFORM_TYPE_COUNT) );
BX_STATIC_ASSERT(bgfx::RenderFrame::Count == bgfx::RenderFrame::Enum(BGFX_RENDER_FRAME_COUNT) );

BX_STATIC_ASSERT(sizeof(bgfx::Memory) == sizeof(bgfx_memory_t) );
BX_STATIC_ASSERT(sizeof(bgfx::VertexDecl) == sizeof(bgfx_vertex_decl_t) );
BX_STATIC_ASSERT(sizeof(bgfx::TransientIndexBuffer) == sizeof(bgfx_transient_index_buffer_t) );
BX_STATIC_ASSERT(sizeof(bgfx::TransientVertexBuffer) == sizeof(bgfx_transient_vertex_buffer_t) );
BX_STATIC_ASSERT(sizeof(bgfx::InstanceDataBuffer) == sizeof(bgfx_instance_data_buffer_t) );
BX_STATIC_ASSERT(sizeof(bgfx::TextureInfo) == sizeof(bgfx_texture_info_t) );
BX_STATIC_ASSERT(sizeof(bgfx::Caps) == sizeof(bgfx_caps_t) );
BX_STATIC_ASSERT(sizeof(bgfx::PlatformData) == sizeof(bgfx_platform_data_t) );
BX_STATIC_ASSERT(sizeof(bgfx::InternalData) == sizeof(bgfx_internal_data_t) );
#define BGFX_C99_ENUM_CHECK(_enum, _c99enumcount) \
BX_STATIC_ASSERT(_enum::Count == _enum::Enum(_c99enumcount) )

BGFX_C99_ENUM_CHECK(bgfx::Fatal, BGFX_FATAL_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::RendererType, BGFX_RENDERER_TYPE_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::Attrib, BGFX_ATTRIB_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::AttribType, BGFX_ATTRIB_TYPE_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::TextureFormat, BGFX_TEXTURE_FORMAT_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::UniformType, BGFX_UNIFORM_TYPE_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::BackbufferRatio, BGFX_BACKBUFFER_RATIO_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::OcclusionQueryResult, BGFX_OCCLUSION_QUERY_RESULT_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::TopologyConvert, BGFX_TOPOLOGY_CONVERT_COUNT);
BGFX_C99_ENUM_CHECK(bgfx::RenderFrame, BGFX_RENDER_FRAME_COUNT);
#undef BGFX_C99_ENUM_CHECK

#define BGFX_C99_STRUCT_SIZE_CHECK(_cppstruct, _c99struct) \
BX_STATIC_ASSERT(sizeof(_cppstruct) == sizeof(_c99struct) )

BGFX_C99_STRUCT_SIZE_CHECK(bgfx::Memory, bgfx_memory_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::VertexDecl, bgfx_vertex_decl_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::TransientIndexBuffer, bgfx_transient_index_buffer_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::TransientVertexBuffer, bgfx_transient_vertex_buffer_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::InstanceDataBuffer, bgfx_instance_data_buffer_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::TextureInfo, bgfx_texture_info_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::Caps, bgfx_caps_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::PlatformData, bgfx_platform_data_t);
BGFX_C99_STRUCT_SIZE_CHECK(bgfx::InternalData, bgfx_internal_data_t);
#undef BGFX_C99_STRUCT_SIZE_CHECK

namespace bgfx
{
Expand Down Expand Up @@ -3775,6 +3807,11 @@ BGFX_C_API uint16_t bgfx_weld_vertices(uint16_t* _output, const bgfx_vertex_decl
return bgfx::weldVertices(_output, decl, _data, _num, _epsilon);
}

uint32_t bgfx_topology_convert(bgfx_topology_convert_t _conversion, void* _dst, uint32_t _dstSize, const void* _indices, uint32_t _numIndices, bool _index32)
{
return bgfx::topologyConvert(bgfx::TopologyConvert::Enum(_conversion), _dst, _dstSize, _indices, _numIndices, _index32);
}

BGFX_C_API void bgfx_image_swizzle_bgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
bgfx::imageSwizzleBgra8(_width, _height, _pitch, _src, _dst);
Expand Down Expand Up @@ -4354,10 +4391,10 @@ BGFX_C_API void bgfx_set_vertex_buffer(bgfx_vertex_buffer_handle_t _handle, uint
bgfx::setVertexBuffer(handle.cpp, _startVertex, _numVertices);
}

BGFX_C_API void bgfx_set_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _numVertices)
BGFX_C_API void bgfx_set_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices)
{
union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
bgfx::setVertexBuffer(handle.cpp, _numVertices);
bgfx::setVertexBuffer(handle.cpp, _startVertex, _numVertices);
}

BGFX_C_API void bgfx_set_transient_vertex_buffer(const bgfx_transient_vertex_buffer_t* _tvb, uint32_t _startVertex, uint32_t _numVertices)
Expand Down Expand Up @@ -4547,6 +4584,7 @@ BGFX_C_API bgfx_interface_vtbl_t* bgfx_get_interface(uint32_t _version)
BGFX_IMPORT_FUNC(vertex_unpack) \
BGFX_IMPORT_FUNC(vertex_convert) \
BGFX_IMPORT_FUNC(weld_vertices) \
BGFX_IMPORT_FUNC(topology_convert) \
BGFX_IMPORT_FUNC(image_swizzle_bgra8) \
BGFX_IMPORT_FUNC(image_rgba8_downsample_2x2) \
BGFX_IMPORT_FUNC(get_supported_renderers) \
Expand Down
12 changes: 6 additions & 6 deletions src/bgfx_p.h
Expand Up @@ -1538,18 +1538,18 @@ namespace bgfx
m_draw.m_vertexBuffer = _handle;
}

void setVertexBuffer(const DynamicVertexBuffer& _dvb, uint32_t _numVertices)
void setVertexBuffer(const DynamicVertexBuffer& _dvb, uint32_t _startVertex, uint32_t _numVertices)
{
m_draw.m_startVertex = _dvb.m_startVertex;
m_draw.m_numVertices = bx::uint32_min(_dvb.m_numVertices, _numVertices);
m_draw.m_startVertex = _dvb.m_startVertex + _startVertex;
m_draw.m_numVertices = bx::uint32_min(bx::uint32_imax(0, _dvb.m_numVertices - _startVertex), _numVertices);
m_draw.m_vertexBuffer = _dvb.m_handle;
m_draw.m_vertexDecl = _dvb.m_decl;
}

void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices)
{
m_draw.m_startVertex = _tvb->startVertex + _startVertex;
m_draw.m_numVertices = bx::uint32_min(_tvb->size/_tvb->stride, _numVertices);
m_draw.m_numVertices = bx::uint32_min(bx::uint32_imax(0, _tvb->size/_tvb->stride - _startVertex), _numVertices);
m_draw.m_vertexBuffer = _tvb->handle;
m_draw.m_vertexDecl = _tvb->decl;
}
Expand Down Expand Up @@ -3650,10 +3650,10 @@ namespace bgfx
m_submit->setVertexBuffer(_handle, _startVertex, _numVertices);
}

BGFX_API_FUNC(void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices) )
BGFX_API_FUNC(void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices) )
{
BGFX_CHECK_HANDLE("setVertexBuffer", m_dynamicVertexBufferHandle, _handle);
m_submit->setVertexBuffer(m_dynamicVertexBuffers[_handle.idx], _numVertices);
m_submit->setVertexBuffer(m_dynamicVertexBuffers[_handle.idx], _startVertex, _numVertices);
}

BGFX_API_FUNC(void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices) )
Expand Down

0 comments on commit d4d4d41

Please sign in to comment.