diff options
author | 2018-12-05 19:45:08 +0100 | |
---|---|---|
committer | 2018-12-05 19:45:08 +0100 | |
commit | 0bd02131b644b61088789f52f31b750c9aecaa6d (patch) | |
tree | 811c679a1bba8b24fc7967cdfe73640254d64156 /3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp | |
parent | 9a81ec7eaf00d73a23db5c003dc45b55d4b76c4a (diff) |
3rdparty: Updated bgfx, bimg, and bx to latest upstream. [Ryan Holtz]
Diffstat (limited to '3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp | 1178 |
1 files changed, 791 insertions, 387 deletions
diff --git a/3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp b/3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp index 3418dd96fec..669f1e0204c 100644 --- a/3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp +++ b/3rdparty/bgfx/examples/common/debugdraw/debugdraw.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2011-2017 Branimir Karadzic. All rights reserved. + * Copyright 2011-2018 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ @@ -9,11 +9,16 @@ #include "../bgfx_utils.h" #include "../packrect.h" +#include <bx/mutex.h> #include <bx/math.h> #include <bx/sort.h> #include <bx/uint32_t.h> #include <bx/handlealloc.h> +#ifndef DEBUG_DRAW_CONFIG_MAX_GEOMETRY +# define DEBUG_DRAW_CONFIG_MAX_GEOMETRY 256 +#endif // DEBUG_DRAW_CONFIG_MAX_GEOMETRY + struct DebugVertex { float m_x; @@ -82,6 +87,40 @@ struct DebugShapeVertex bgfx::VertexDecl DebugShapeVertex::ms_decl; +struct DebugMeshVertex +{ + float m_x; + float m_y; + float m_z; + + static void init() + { + ms_decl + .begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .end(); + } + + static bgfx::VertexDecl ms_decl; +}; + +bgfx::VertexDecl DebugMeshVertex::ms_decl; + +static DebugShapeVertex s_quadVertices[4] = +{ + {-1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } }, + { 1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } }, + {-1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } }, + { 1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } }, + +}; + +static const uint16_t s_quadIndices[6] = +{ + 0, 1, 2, + 1, 3, 2, +}; + static DebugShapeVertex s_cubeVertices[8] = { {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } }, @@ -127,18 +166,18 @@ static uint8_t getCircleLod(uint8_t _lod) static void circle(float* _out, float _angle) { - float sa = bx::fsin(_angle); - float ca = bx::fcos(_angle); + float sa = bx::sin(_angle); + float ca = bx::cos(_angle); _out[0] = sa; _out[1] = ca; } static void squircle(float* _out, float _angle) { - float sa = bx::fsin(_angle); - float ca = bx::fcos(_angle); - _out[0] = bx::fsqrt(bx::fabs(sa) ) * bx::fsign(sa); - _out[1] = bx::fsqrt(bx::fabs(ca) ) * bx::fsign(ca); + float sa = bx::sin(_angle); + float ca = bx::cos(_angle); + _out[0] = bx::sqrt(bx::abs(sa) ) * bx::sign(sa); + _out[1] = bx::sqrt(bx::abs(ca) ) * bx::sign(ca); } uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0) @@ -155,7 +194,7 @@ uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = { static const float scale = 1.0f; static const float golden = 1.6180339887f; - static const float len = bx::fsqrt(golden*golden + 1.0f); + static const float len = bx::sqrt(golden*golden + 1.0f); static const float ss = 1.0f/len * scale; static const float ll = ss*golden; @@ -266,7 +305,7 @@ uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0); } - uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::fpow(4.0f, _subdiv0) ); + uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::pow(4.0f, _subdiv0) ); return numVertices; } @@ -299,8 +338,10 @@ void getPoint(float* _result, Axis::Enum _axis, float _x, float _y) #include "vs_debugdraw_lines_stipple.bin.h" #include "fs_debugdraw_lines_stipple.bin.h" #include "vs_debugdraw_fill.bin.h" +#include "vs_debugdraw_fill_mesh.bin.h" #include "fs_debugdraw_fill.bin.h" #include "vs_debugdraw_fill_lit.bin.h" +#include "vs_debugdraw_fill_lit_mesh.bin.h" #include "fs_debugdraw_fill_lit.bin.h" #include "vs_debugdraw_fill_texture.bin.h" #include "fs_debugdraw_fill_texture.bin.h" @@ -312,8 +353,10 @@ static const bgfx::EmbeddedShader s_embeddedShaders[] = BGFX_EMBEDDED_SHADER(vs_debugdraw_lines_stipple), BGFX_EMBEDDED_SHADER(fs_debugdraw_lines_stipple), BGFX_EMBEDDED_SHADER(vs_debugdraw_fill), + BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_mesh), BGFX_EMBEDDED_SHADER(fs_debugdraw_fill), BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit), + BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit_mesh), BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_lit), BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_texture), BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_texture), @@ -333,6 +376,8 @@ struct SpriteT SpriteHandle create(uint16_t _width, uint16_t _height) { + bx::MutexScope lock(m_lock); + SpriteHandle handle = { bx::kInvalidHandle }; if (m_handleAlloc.getNumHandles() < m_handleAlloc.getMaxHandles() ) @@ -341,7 +386,15 @@ struct SpriteT if (m_ra.find(_width, _height, pack) ) { handle.idx = m_handleAlloc.alloc(); - m_pack[handle.idx] = pack; + + if (isValid(handle) ) + { + m_pack[handle.idx] = pack; + } + else + { + m_ra.clear(pack); + } } } @@ -360,33 +413,196 @@ struct SpriteT return m_pack[_sprite.idx]; } + bx::Mutex m_lock; bx::HandleAllocT<MaxHandlesT> m_handleAlloc; Pack2D m_pack[MaxHandlesT]; RectPack2DT<256> m_ra; }; -struct DebugDraw +template<uint16_t MaxHandlesT = DEBUG_DRAW_CONFIG_MAX_GEOMETRY> +struct GeometryT { - DebugDraw() - : m_depthTestLess(true) - , m_state(State::Count) + GeometryT() { } - void init(bool _depthTestLess, bx::AllocatorI* _allocator) + GeometryHandle create(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32) { - m_allocator = _allocator; - m_depthTestLess = _depthTestLess; + BX_UNUSED(_numVertices, _vertices, _numIndices, _indices, _index32); + + GeometryHandle handle; + { + bx::MutexScope lock(m_lock); + handle = { m_handleAlloc.alloc() }; + } + + if (isValid(handle) ) + { + Geometry& geometry = m_geometry[handle.idx]; + geometry.m_vbh = bgfx::createVertexBuffer( + bgfx::copy(_vertices, _numVertices*sizeof(DdVertex) ) + , DebugMeshVertex::ms_decl + ); + + geometry.m_topologyNumIndices[0] = _numIndices; + geometry.m_topologyNumIndices[1] = bgfx::topologyConvert( + bgfx::TopologyConvert::TriListToLineList + , NULL + , 0 + , _indices + , _numIndices + , _index32 + ); + + const uint32_t indexSize = _index32 ? sizeof(uint32_t) : sizeof(uint16_t); + + const uint32_t numIndices = 0 + + geometry.m_topologyNumIndices[0] + + geometry.m_topologyNumIndices[1] + ; + const bgfx::Memory* mem = bgfx::alloc(numIndices*indexSize ); + uint8_t* indexData = mem->data; + + bx::memCopy(indexData, _indices, _numIndices*indexSize ); + bgfx::topologyConvert( + bgfx::TopologyConvert::TriListToLineList + , &indexData[geometry.m_topologyNumIndices[0]*indexSize ] + , geometry.m_topologyNumIndices[1]*indexSize + , _indices + , _numIndices + , _index32 + ); + + geometry.m_ibh = bgfx::createIndexBuffer( + mem + , _index32 ? BGFX_BUFFER_INDEX32 : BGFX_BUFFER_NONE + ); + } + + return handle; + } + + void destroy(GeometryHandle _handle) + { + bx::MutexScope lock(m_lock); + Geometry& geometry = m_geometry[_handle.idx]; + bgfx::destroy(geometry.m_vbh); + bgfx::destroy(geometry.m_ibh); + + m_handleAlloc.free(_handle.idx); + } + + struct Geometry + { + Geometry() + { + m_vbh.idx = bx::kInvalidHandle; + m_ibh.idx = bx::kInvalidHandle; + m_topologyNumIndices[0] = 0; + m_topologyNumIndices[1] = 0; + } + + bgfx::VertexBufferHandle m_vbh; + bgfx::IndexBufferHandle m_ibh; + uint32_t m_topologyNumIndices[2]; + }; + + bx::Mutex m_lock; + bx::HandleAllocT<MaxHandlesT> m_handleAlloc; + Geometry m_geometry[MaxHandlesT]; +}; + +struct Attrib +{ + uint64_t m_state; + float m_offset; + float m_scale; + float m_spin; + uint32_t m_abgr; + bool m_stipple; + bool m_wireframe; + uint8_t m_lod; +}; + +struct Program +{ + enum Enum + { + Lines, + LinesStipple, + Fill, + FillMesh, + FillLit, + FillLitMesh, + FillTexture, + + Count + }; +}; + +struct Mesh +{ + enum Enum + { + Sphere0, + Sphere1, + Sphere2, + Sphere3, + + Cone0, + Cone1, + Cone2, + Cone3, + + Cylinder0, + Cylinder1, + Cylinder2, + Cylinder3, + + Capsule0, + Capsule1, + Capsule2, + Capsule3, + + Quad, + + Cube, + + Count, + + SphereMaxLod = Sphere3 - Sphere0, + ConeMaxLod = Cone3 - Cone0, + CylinderMaxLod = Cylinder3 - Cylinder0, + CapsuleMaxLod = Capsule3 - Capsule0, + }; + + uint32_t m_startVertex; + uint32_t m_numVertices; + uint32_t m_startIndex[2]; + uint32_t m_numIndices[2]; +}; +typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite; +typedef GeometryT<DEBUG_DRAW_CONFIG_MAX_GEOMETRY> Geometry; + +struct DebugDrawShared +{ + void init(bx::AllocatorI* _allocator) + { if (NULL == _allocator) { static bx::DefaultAllocator allocator; m_allocator = &allocator; } + else + { + m_allocator = _allocator; + } DebugVertex::init(); DebugUvVertex::init(); DebugShapeVertex::init(); + DebugMeshVertex::init(); bgfx::RendererType::Enum type = bgfx::getRendererType(); @@ -408,12 +624,24 @@ struct DebugDraw , true ); + m_program[Program::FillMesh] = bgfx::createProgram( + bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_mesh") + , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill") + , true + ); + m_program[Program::FillLit] = bgfx::createProgram( bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit") , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit") , true ); + m_program[Program::FillLitMesh] = bgfx::createProgram( + bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit_mesh") + , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit") + , true + ); + m_program[Program::FillTexture] = bgfx::createProgram( bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_texture") , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_texture") @@ -684,6 +912,15 @@ struct DebugDraw startIndex += numIndices + numLineListIndices; } + m_mesh[Mesh::Quad].m_startVertex = startVertex; + m_mesh[Mesh::Quad].m_numVertices = BX_COUNTOF(s_quadVertices); + m_mesh[Mesh::Quad].m_startIndex[0] = startIndex; + m_mesh[Mesh::Quad].m_numIndices[0] = BX_COUNTOF(s_quadIndices); + m_mesh[Mesh::Quad].m_startIndex[1] = 0; + m_mesh[Mesh::Quad].m_numIndices[1] = 0; + startVertex += BX_COUNTOF(s_quadVertices); + startIndex += BX_COUNTOF(s_quadIndices); + m_mesh[Mesh::Cube].m_startVertex = startVertex; m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices); m_mesh[Mesh::Cube].m_startIndex[0] = startIndex; @@ -696,7 +933,7 @@ struct DebugDraw const bgfx::Memory* vb = bgfx::alloc(startVertex*stride); const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) ); - for (uint32_t mesh = Mesh::Sphere0; mesh < Mesh::Cube; ++mesh) + for (uint32_t mesh = Mesh::Sphere0; mesh < Mesh::Quad; ++mesh) { Mesh::Enum id = Mesh::Enum(mesh); bx::memCopy(&vb->data[m_mesh[id].m_startVertex * stride] @@ -713,6 +950,16 @@ struct DebugDraw BX_FREE(m_allocator, indices[id]); } + bx::memCopy(&vb->data[m_mesh[Mesh::Quad].m_startVertex * stride] + , s_quadVertices + , sizeof(s_quadVertices) + ); + + bx::memCopy(&ib->data[m_mesh[Mesh::Quad].m_startIndex[0] * sizeof(uint16_t)] + , s_quadIndices + , sizeof(s_quadIndices) + ); + bx::memCopy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride] , s_cubeVertices , sizeof(s_cubeVertices) @@ -725,12 +972,6 @@ struct DebugDraw m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl); m_ibh = bgfx::createIndexBuffer(ib); - - m_viewId = 0; - m_pos = 0; - m_indexPos = 0; - m_vertexPos = 0; - m_posQuad = 0; } void shutdown() @@ -773,20 +1014,73 @@ struct DebugDraw m_sprite.destroy(_handle); } - void begin(uint8_t _viewId) + GeometryHandle createGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32) + { + return m_geometry.create(_numVertices, _vertices, _numIndices, _indices, _index32); + } + + void destroy(GeometryHandle _handle) + { + m_geometry.destroy(_handle); + } + + bx::AllocatorI* m_allocator; + + Sprite m_sprite; + Geometry m_geometry; + + Mesh m_mesh[Mesh::Count]; + + bgfx::UniformHandle s_texColor; + bgfx::TextureHandle m_texture; + bgfx::ProgramHandle m_program[Program::Count]; + bgfx::UniformHandle u_params; + + bgfx::VertexBufferHandle m_vbh; + bgfx::IndexBufferHandle m_ibh; +}; + +static DebugDrawShared s_dds; + +struct DebugDrawEncoderImpl +{ + DebugDrawEncoderImpl() + : m_depthTestLess(true) + , m_state(State::Count) + , m_defaultEncoder(NULL) + { + } + + void init(bgfx::Encoder* _encoder) + { + m_defaultEncoder = _encoder; + } + + void shutdown() + { + } + + void begin(bgfx::ViewId _viewId, bool _depthTestLess, bgfx::Encoder* _encoder) { BX_CHECK(State::Count == m_state); - m_viewId = _viewId; - m_state = State::None; - m_stack = 0; + m_viewId = _viewId; + m_encoder = _encoder == NULL ? m_defaultEncoder : _encoder; + m_state = State::None; + m_stack = 0; + m_depthTestLess = _depthTestLess; + + m_pos = 0; + m_indexPos = 0; + m_vertexPos = 0; + m_posQuad = 0; Attrib& attrib = m_attrib[0]; attrib.m_state = 0 - | BGFX_STATE_RGB_WRITE + | BGFX_STATE_WRITE_RGB | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER) | BGFX_STATE_CULL_CW - | BGFX_STATE_DEPTH_WRITE + | BGFX_STATE_WRITE_Z ; attrib.m_scale = 1.0f; attrib.m_spin = 0.0f; @@ -807,7 +1101,8 @@ struct DebugDraw flushQuad(); flush(); - m_state = State::Count; + m_encoder = NULL; + m_state = State::Count; } void push() @@ -830,10 +1125,29 @@ struct DebugDraw --m_stack; } - void setTransform(const void* _mtx, uint16_t _num = 1) + void setDepthTestLess(bool _depthTestLess) { BX_CHECK(State::Count != m_state); - flush(); + if (m_depthTestLess != _depthTestLess) + { + m_depthTestLess = _depthTestLess; + Attrib& attrib = m_attrib[m_stack]; + if (attrib.m_state & BGFX_STATE_DEPTH_TEST_MASK) + { + flush(); + attrib.m_state &= ~BGFX_STATE_DEPTH_TEST_MASK; + attrib.m_state |= _depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER; + } + } + } + + void setTransform(const void* _mtx, uint16_t _num = 1, bool _flush = true) + { + BX_CHECK(State::Count != m_state); + if (_flush) + { + flush(); + } MatrixStack& stack = m_mtxStack[m_mtxStackCurrent]; @@ -844,7 +1158,7 @@ struct DebugDraw } bgfx::Transform transform; - stack.mtx = bgfx::allocTransform(&transform, _num); + stack.mtx = m_encoder->allocTransform(&transform, _num); stack.num = _num; stack.data = transform.data; bx::memCopy(transform.data, _mtx, _num*64); @@ -862,11 +1176,14 @@ struct DebugDraw setTranslate(_pos[0], _pos[1], _pos[2]); } - void pushTransform(const void* _mtx, uint16_t _num) + void pushTransform(const void* _mtx, uint16_t _num, bool _flush = true) { BX_CHECK(m_mtxStackCurrent < BX_COUNTOF(m_mtxStack), "Out of matrix stack!"); BX_CHECK(State::Count != m_state); - flush(); + if (_flush) + { + flush(); + } float* mtx = NULL; @@ -887,13 +1204,16 @@ struct DebugDraw } m_mtxStackCurrent++; - setTransform(mtx, _num); + setTransform(mtx, _num, _flush); } - void popTransform() + void popTransform(bool _flush = true) { BX_CHECK(State::Count != m_state); - flush(); + if (_flush) + { + flush(); + } m_mtxStackCurrent--; } @@ -919,7 +1239,7 @@ struct DebugDraw uint64_t state = m_attrib[m_stack].m_state & ~(0 | BGFX_STATE_DEPTH_TEST_MASK - | BGFX_STATE_DEPTH_WRITE + | BGFX_STATE_WRITE_Z | BGFX_STATE_CULL_CW | BGFX_STATE_CULL_CCW ); @@ -930,7 +1250,7 @@ struct DebugDraw ; state |= _depthWrite - ? BGFX_STATE_DEPTH_WRITE + ? BGFX_STATE_WRITE_Z : 0 ; @@ -1106,29 +1426,29 @@ struct DebugDraw const Attrib& attrib = m_attrib[m_stack]; if (attrib.m_wireframe) { - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); + moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_min.z); + lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_min.z); + lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_min.z); + lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_min.z); close(); - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); + moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_max.z); + lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_max.z); + lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_max.z); + lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_max.z); close(); - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); + moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_min.z); + lineTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_max.z); - moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); + moveTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_min.z); + lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_max.z); - moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); + moveTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_min.z); + lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_max.z); - moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); + moveTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_min.z); + lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_max.z); } else { @@ -1140,12 +1460,12 @@ struct DebugDraw void draw(const Cylinder& _cylinder, bool _capsule) { - drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule); + drawCylinder(&_cylinder.m_pos.x, &_cylinder.m_end.x, _cylinder.m_radius, _capsule); } void draw(const Disk& _disk) { - drawCircle(_disk.m_normal, _disk.m_center, _disk.m_radius, 0.0f); + drawCircle(&_disk.m_normal.x, &_disk.m_center.x, _disk.m_radius, 0.0f); } void draw(const Obb& _obb) @@ -1198,55 +1518,184 @@ struct DebugDraw , 0.0f , 0.0f , 0.0f - , _sphere.m_center[0] - , _sphere.m_center[1] - , _sphere.m_center[2] + , _sphere.m_center.x + , _sphere.m_center.y + , _sphere.m_center.z ); uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod - ? uint8_t(Mesh::SphereMaxLod) - : attrib.m_lod - ; + ? uint8_t(Mesh::SphereMaxLod) + : attrib.m_lod + ; draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe); } + void setUParams(const Attrib& _attrib, bool _wireframe) + { + const float flip = 0 == (_attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f; + const uint8_t alpha = _attrib.m_abgr >> 24; + + float params[4][4] = + { + { // lightDir + 0.0f * flip, + -1.0f * flip, + 0.0f * flip, + 3.0f, // shininess + }, + { // skyColor + 1.0f, + 0.9f, + 0.8f, + 0.0f, // unused + }, + { // groundColor.xyz0 + 0.2f, + 0.22f, + 0.5f, + 0.0f, // unused + }, + { // matColor + ( (_attrib.m_abgr) & 0xff) / 255.0f, + ( (_attrib.m_abgr >> 8) & 0xff) / 255.0f, + ( (_attrib.m_abgr >> 16) & 0xff) / 255.0f, + (alpha) / 255.0f, + }, + }; + + bx::vec3Norm(params[0], params[0]); + m_encoder->setUniform(s_dds.u_params, params, 4); + + m_encoder->setState(0 + | _attrib.m_state + | (_wireframe ? BGFX_STATE_PT_LINES | BGFX_STATE_LINEAA | BGFX_STATE_BLEND_ALPHA + : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0) + ); + } + + void draw(GeometryHandle _handle) + { + const Geometry::Geometry& geometry = s_dds.m_geometry.m_geometry[_handle.idx]; + m_encoder->setVertexBuffer(0, geometry.m_vbh); + + const Attrib& attrib = m_attrib[m_stack]; + const bool wireframe = attrib.m_wireframe; + setUParams(attrib, wireframe); + + if (wireframe) + { + m_encoder->setIndexBuffer( + geometry.m_ibh + , geometry.m_topologyNumIndices[0] + , geometry.m_topologyNumIndices[1] + ); + } + else if (0 != geometry.m_topologyNumIndices[0]) + { + m_encoder->setIndexBuffer( + geometry.m_ibh + , 0 + , geometry.m_topologyNumIndices[0] + ); + } + + m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx); + bgfx::ProgramHandle program = s_dds.m_program[wireframe ? Program::FillMesh : Program::FillLitMesh]; + m_encoder->submit(m_viewId, program); + } + + void draw(bool _lineList, uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices) + { + flush(); + + if (_numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, DebugMeshVertex::ms_decl) ) + { + bgfx::TransientVertexBuffer tvb; + bgfx::allocTransientVertexBuffer(&tvb, _numVertices, DebugMeshVertex::ms_decl); + bx::memCopy(tvb.data, _vertices, _numVertices * DebugMeshVertex::ms_decl.m_stride); + m_encoder->setVertexBuffer(0, &tvb); + + const Attrib& attrib = m_attrib[m_stack]; + const bool wireframe = _lineList || attrib.m_wireframe; + setUParams(attrib, wireframe); + + if (0 < _numIndices) + { + uint32_t numIndices = _numIndices; + bgfx::TransientIndexBuffer tib; + if (!_lineList && wireframe) + { + numIndices = bgfx::topologyConvert( + bgfx::TopologyConvert::TriListToLineList + , NULL + , 0 + , _indices + , _numIndices + , false + ); + + + bgfx::allocTransientIndexBuffer(&tib, numIndices); + bgfx::topologyConvert( + bgfx::TopologyConvert::TriListToLineList + , tib.data + , numIndices * sizeof(uint16_t) + , _indices + , _numIndices + , false + ); + } + else + { + bgfx::allocTransientIndexBuffer(&tib, numIndices); + bx::memCopy(tib.data, _indices, numIndices * sizeof(uint16_t) ); + } + m_encoder->setIndexBuffer(&tib); + } + + m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx); + bgfx::ProgramHandle program = s_dds.m_program[wireframe ? Program::FillMesh : Program::FillLitMesh]; + m_encoder->submit(m_viewId, program); + } + } + void drawFrustum(const float* _viewProj) { Plane planes[6]; buildFrustumPlanes(planes, _viewProj); - float points[24]; - intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]); - intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]); - intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]); - intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]); - intersectPlanes(&points[12], planes[1], planes[2], planes[4]); - intersectPlanes(&points[15], planes[1], planes[3], planes[4]); - intersectPlanes(&points[18], planes[1], planes[3], planes[5]); - intersectPlanes(&points[21], planes[1], planes[2], planes[5]); - - moveTo(&points[ 0]); - lineTo(&points[ 3]); - lineTo(&points[ 6]); - lineTo(&points[ 9]); + bx::Vec3 points[8]; + points[0] = intersectPlanes(planes[0], planes[2], planes[4]); + points[1] = intersectPlanes(planes[0], planes[3], planes[4]); + points[2] = intersectPlanes(planes[0], planes[3], planes[5]); + points[3] = intersectPlanes(planes[0], planes[2], planes[5]); + points[4] = intersectPlanes(planes[1], planes[2], planes[4]); + points[5] = intersectPlanes(planes[1], planes[3], planes[4]); + points[6] = intersectPlanes(planes[1], planes[3], planes[5]); + points[7] = intersectPlanes(planes[1], planes[2], planes[5]); + + moveTo(&points[0].x); + lineTo(&points[1].x); + lineTo(&points[2].x); + lineTo(&points[3].x); close(); - moveTo(&points[12]); - lineTo(&points[15]); - lineTo(&points[18]); - lineTo(&points[21]); + moveTo(&points[4].x); + lineTo(&points[5].x); + lineTo(&points[6].x); + lineTo(&points[7].x); close(); - moveTo(&points[ 0]); - lineTo(&points[12]); + moveTo(&points[0].x); + lineTo(&points[4].x); - moveTo(&points[ 3]); - lineTo(&points[15]); + moveTo(&points[1].x); + lineTo(&points[5].x); - moveTo(&points[ 6]); - lineTo(&points[18]); + moveTo(&points[2].x); + lineTo(&points[6].x); - moveTo(&points[ 9]); - lineTo(&points[21]); + moveTo(&points[3].x); + lineTo(&points[7].x); } void drawFrustum(const void* _viewProj) @@ -1260,12 +1709,12 @@ struct DebugDraw const uint32_t num = getCircleLod(attrib.m_lod); const float step = bx::kPi * 2.0f / num; - _degrees = bx::fwrap(_degrees, 360.0f); + _degrees = bx::wrap(_degrees, 360.0f); float pos[3]; getPoint(pos, _axis - , bx::fsin(step * 0)*_radius - , bx::fcos(step * 0)*_radius + , bx::sin(step * 0)*_radius + , bx::cos(step * 0)*_radius ); moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); @@ -1275,22 +1724,22 @@ struct DebugDraw for (uint32_t ii = 1; ii < n+1; ++ii) { getPoint(pos, _axis - , bx::fsin(step * ii)*_radius - , bx::fcos(step * ii)*_radius + , bx::sin(step * ii)*_radius + , bx::cos(step * ii)*_radius ); lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); } moveTo(_x, _y, _z); getPoint(pos, _axis - , bx::fsin(step * 0)*_radius - , bx::fcos(step * 0)*_radius + , bx::sin(step * 0)*_radius + , bx::cos(step * 0)*_radius ); lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); getPoint(pos, _axis - , bx::fsin(step * n)*_radius - , bx::fcos(step * n)*_radius + , bx::sin(step * n)*_radius + , bx::cos(step * n)*_radius ); moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); lineTo(_x, _y, _z); @@ -1301,7 +1750,7 @@ struct DebugDraw const Attrib& attrib = m_attrib[m_stack]; const uint32_t num = getCircleLod(attrib.m_lod); const float step = bx::kPi * 2.0f / num; - _weight = bx::fclamp(_weight, 0.0f, 2.0f); + _weight = bx::clamp(_weight, 0.0f, 2.0f); float udir[3]; float vdir[3]; @@ -1316,8 +1765,8 @@ struct DebugDraw circle(xy0, 0.0f); squircle(xy1, 0.0f); - bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius); - bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius); + bx::vec3Mul(pos, udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius); + bx::vec3Mul(tmp0, vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius); bx::vec3Add(tmp1, pos, tmp0); bx::vec3Add(pos, tmp1, _center); moveTo(pos); @@ -1328,8 +1777,8 @@ struct DebugDraw circle(xy0, angle); squircle(xy1, angle); - bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius); - bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius); + bx::vec3Mul(pos, udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius); + bx::vec3Mul(tmp0, vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius); bx::vec3Add(tmp1, pos, tmp0); bx::vec3Add(pos, tmp1, _center); lineTo(pos); @@ -1348,7 +1797,7 @@ struct DebugDraw const Attrib& attrib = m_attrib[m_stack]; const uint32_t num = getCircleLod(attrib.m_lod); const float step = bx::kPi * 2.0f / num; - _weight = bx::fclamp(_weight, 0.0f, 2.0f); + _weight = bx::clamp(_weight, 0.0f, 2.0f); float xy0[2]; float xy1[2]; @@ -1357,8 +1806,8 @@ struct DebugDraw float pos[3]; getPoint(pos, _axis - , bx::flerp(xy0[0], xy1[0], _weight)*_radius - , bx::flerp(xy0[1], xy1[1], _weight)*_radius + , bx::lerp(xy0[0], xy1[0], _weight)*_radius + , bx::lerp(xy0[1], xy1[1], _weight)*_radius ); moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); @@ -1369,8 +1818,8 @@ struct DebugDraw squircle(xy1, angle); getPoint(pos, _axis - , bx::flerp(xy0[0], xy1[0], _weight)*_radius - , bx::flerp(xy0[1], xy1[1], _weight)*_radius + , bx::lerp(xy0[0], xy1[0], _weight)*_radius + , bx::lerp(xy0[1], xy1[1], _weight)*_radius ); lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z); } @@ -1380,49 +1829,63 @@ struct DebugDraw void drawQuad(const float* _normal, const float* _center, float _size) { const Attrib& attrib = m_attrib[m_stack]; + if (attrib.m_wireframe) + { + float udir[3]; + float vdir[3]; - float udir[3]; - float vdir[3]; - - bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin); + bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin); - const float halfExtent = _size*0.5f; + const float halfExtent = _size*0.5f; - float umin[3]; - bx::vec3Mul(umin, udir, -halfExtent); + float umin[3]; + bx::vec3Mul(umin, udir, -halfExtent); - float umax[3]; - bx::vec3Mul(umax, udir, halfExtent); + float umax[3]; + bx::vec3Mul(umax, udir, halfExtent); - float vmin[3]; - bx::vec3Mul(vmin, vdir, -halfExtent); + float vmin[3]; + bx::vec3Mul(vmin, vdir, -halfExtent); - float vmax[3]; - bx::vec3Mul(vmax, vdir, halfExtent); + float vmax[3]; + bx::vec3Mul(vmax, vdir, halfExtent); - float pt[3]; - float tmp[3]; - bx::vec3Add(tmp, umin, vmin); - bx::vec3Add(pt, _center, tmp); - moveTo(pt); + float pt[3]; + float tmp[3]; + bx::vec3Add(tmp, umin, vmin); + bx::vec3Add(pt, _center, tmp); + moveTo(pt); - bx::vec3Add(tmp, umax, vmin); - bx::vec3Add(pt, _center, tmp); - lineTo(pt); + bx::vec3Add(tmp, umax, vmin); + bx::vec3Add(pt, _center, tmp); + lineTo(pt); - bx::vec3Add(tmp, umax, vmax); - bx::vec3Add(pt, _center, tmp); - lineTo(pt); + bx::vec3Add(tmp, umax, vmax); + bx::vec3Add(pt, _center, tmp); + lineTo(pt); - bx::vec3Add(tmp, umin, vmax); - bx::vec3Add(pt, _center, tmp); - lineTo(pt); + bx::vec3Add(tmp, umin, vmax); + bx::vec3Add(pt, _center, tmp); + lineTo(pt); - close(); + close(); + } + else + { + float mtx[16]; + bx::mtxFromNormal(mtx, _normal, _size*0.5f, _center, attrib.m_spin); + draw(Mesh::Quad, mtx, 1, false); + } } void drawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size) { + if (!isValid(_handle) ) + { + drawQuad(_normal, _center, _size); + return; + } + if (m_posQuad == BX_COUNTOF(m_cacheQuad) ) { flushQuad(); @@ -1435,7 +1898,7 @@ struct DebugDraw bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin); - const Pack2D& pack = m_sprite.get(_handle); + const Pack2D& pack = s_dds.m_sprite.get(_handle); const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE; const float us = pack.m_x * invTextureSize; const float vs = pack.m_y * invTextureSize; @@ -1560,26 +2023,30 @@ struct DebugDraw if (_capsule) { uint8_t lod = attrib.m_lod > Mesh::CapsuleMaxLod - ? uint8_t(Mesh::CapsuleMaxLod) - : attrib.m_lod - ; + ? uint8_t(Mesh::CapsuleMaxLod) + : attrib.m_lod + ; draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe); Sphere sphere; - bx::vec3Move(sphere.m_center, _from); - sphere.m_radius = _radius; + sphere.m_center.x = _from[0]; + sphere.m_center.y = _from[1]; + sphere.m_center.z = _from[2]; + sphere.m_radius = _radius; draw(sphere); - bx::vec3Move(sphere.m_center, _to); + sphere.m_center.x = _to[0]; + sphere.m_center.y = _to[1]; + sphere.m_center.z = _to[2]; draw(sphere); } else { uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod - ? uint8_t(Mesh::CylinderMaxLod) - : attrib.m_lod - ; - draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe); + ? uint8_t(Mesh::CylinderMaxLod) + : attrib.m_lod + ; + draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe); } } @@ -1745,6 +2212,7 @@ struct DebugDraw moveTo(_axis, 0.0f, -halfExtent); lineTo(_axis, 0.0f, halfExtent); + popTransform(); pop(); } @@ -1769,124 +2237,30 @@ struct DebugDraw pop(); } -private: - struct Mesh - { - enum Enum - { - Sphere0, - Sphere1, - Sphere2, - Sphere3, - - Cone0, - Cone1, - Cone2, - Cone3, - - Cylinder0, - Cylinder1, - Cylinder2, - Cylinder3, - - Capsule0, - Capsule1, - Capsule2, - Capsule3, - - Cube, - - Count, - - SphereMaxLod = Sphere3 - Sphere0, - ConeMaxLod = Cone3 - Cone0, - CylinderMaxLod = Cylinder3 - Cylinder0, - CapsuleMaxLod = Capsule3 - Capsule0, - }; - - uint32_t m_startVertex; - uint32_t m_numVertices; - uint32_t m_startIndex[2]; - uint32_t m_numIndices[2]; - }; - - struct Program - { - enum Enum - { - Lines, - LinesStipple, - Fill, - FillLit, - FillTexture, - - Count - }; - }; - void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe) { - pushTransform(_mtx, _num); - - const Mesh& mesh = m_mesh[_mesh]; + pushTransform(_mtx, _num, false /* flush */); - const Attrib& attrib = m_attrib[m_stack]; + const Mesh& mesh = s_dds.m_mesh[_mesh]; if (0 != mesh.m_numIndices[_wireframe]) { - bgfx::setIndexBuffer(m_ibh + m_encoder->setIndexBuffer(s_dds.m_ibh , mesh.m_startIndex[_wireframe] , mesh.m_numIndices[_wireframe] ); } - const float flip = 0 == (attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f; - const uint8_t alpha = attrib.m_abgr>>24; - - float params[4][4] = - { - { // lightDir - 0.0f * flip, - -1.0f * flip, - 0.0f * flip, - 3.0f, // shininess - }, - { // skyColor - 1.0f, - 0.9f, - 0.8f, - 0.0f, // unused - }, - { // groundColor.xyz0 - 0.2f, - 0.22f, - 0.5f, - 0.0f, // unused - }, - { // matColor - ( (attrib.m_abgr )&0xff)/255.0f, - ( (attrib.m_abgr>> 8)&0xff)/255.0f, - ( (attrib.m_abgr>>16)&0xff)/255.0f, - ( alpha )/255.0f, - }, - }; - - bx::vec3Norm(params[0], params[0]); - - bgfx::setUniform(u_params, params, 4); + const Attrib& attrib = m_attrib[m_stack]; + setUParams(attrib, _wireframe); MatrixStack& stack = m_mtxStack[m_mtxStackCurrent]; - bgfx::setTransform(stack.mtx, stack.num); + m_encoder->setTransform(stack.mtx, stack.num); - bgfx::setVertexBuffer(0, m_vbh, mesh.m_startVertex, mesh.m_numVertices); - bgfx::setState(0 - | attrib.m_state - | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA - : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0) - ); - bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]); + m_encoder->setVertexBuffer(0, s_dds.m_vbh, mesh.m_startVertex, mesh.m_numVertices); + m_encoder->submit(m_viewId, s_dds.m_program[_wireframe ? Program::Fill : Program::FillLit]); - popTransform(); + popTransform(false /* flush */); } void softFlush() @@ -1913,18 +2287,18 @@ private: const Attrib& attrib = m_attrib[m_stack]; - bgfx::setVertexBuffer(0, &tvb); - bgfx::setIndexBuffer(&tib); - bgfx::setState(0 - | BGFX_STATE_RGB_WRITE + m_encoder->setVertexBuffer(0, &tvb); + m_encoder->setIndexBuffer(&tib); + m_encoder->setState(0 + | BGFX_STATE_WRITE_RGB | BGFX_STATE_PT_LINES | attrib.m_state | BGFX_STATE_LINEAA | BGFX_STATE_BLEND_ALPHA ); - bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx); - bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0]; - bgfx::submit(m_viewId, program); + m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx); + bgfx::ProgramHandle program = s_dds.m_program[attrib.m_stipple ? 1 : 0]; + m_encoder->submit(m_viewId, program); } m_state = State::None; @@ -1962,14 +2336,14 @@ private: const Attrib& attrib = m_attrib[m_stack]; - bgfx::setVertexBuffer(0, &tvb); - bgfx::setIndexBuffer(&tib); - bgfx::setState(0 + m_encoder->setVertexBuffer(0, &tvb); + m_encoder->setIndexBuffer(&tib); + m_encoder->setState(0 | (attrib.m_state & ~BGFX_STATE_CULL_MASK) ); - bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx); - bgfx::setTexture(0, s_texColor, m_texture); - bgfx::submit(m_viewId, m_program[Program::FillTexture]); + m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx); + m_encoder->setTexture(0, s_dds.s_texColor, s_dds.m_texture); + m_encoder->submit(m_viewId, s_dds.m_program[Program::FillTexture]); } m_posQuad = 0; @@ -1988,19 +2362,18 @@ private: }; }; - static const uint32_t cacheSize = 1024; - static const uint32_t stackSize = 16; - BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements."); - DebugVertex m_cache[cacheSize+1]; - uint16_t m_indices[cacheSize*2]; + static const uint32_t kCacheSize = 1024; + static const uint32_t kStackSize = 16; + static const uint32_t kCacheQuadSize = 1024; + BX_STATIC_ASSERT(kCacheSize >= 3, "Cache must be at least 3 elements."); + + DebugVertex m_cache[kCacheSize+1]; + DebugUvVertex m_cacheQuad[kCacheQuadSize]; + uint16_t m_indices[kCacheSize*2]; uint16_t m_pos; + uint16_t m_posQuad; uint16_t m_indexPos; uint16_t m_vertexPos; - - static const uint32_t cacheQuadSize = 1024; - DebugUvVertex m_cacheQuad[cacheQuadSize]; - uint16_t m_posQuad; - uint32_t m_mtxStackCurrent; struct MatrixStack @@ -2019,250 +2392,281 @@ private: MatrixStack m_mtxStack[32]; - uint8_t m_viewId; - uint8_t m_stack; - bool m_depthTestLess; - - struct Attrib - { - uint64_t m_state; - float m_offset; - float m_scale; - float m_spin; - uint32_t m_abgr; - bool m_stipple; - bool m_wireframe; - uint8_t m_lod; - }; + bgfx::ViewId m_viewId; + uint8_t m_stack; + bool m_depthTestLess; - Attrib m_attrib[stackSize]; + Attrib m_attrib[kStackSize]; State::Enum m_state; - Mesh m_mesh[Mesh::Count]; - - typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite; - Sprite m_sprite; - - bgfx::UniformHandle s_texColor; - bgfx::TextureHandle m_texture; - bgfx::ProgramHandle m_program[Program::Count]; - bgfx::UniformHandle u_params; - - bgfx::VertexBufferHandle m_vbh; - bgfx::IndexBufferHandle m_ibh; - - bx::AllocatorI* m_allocator; + bgfx::Encoder* m_encoder; + bgfx::Encoder* m_defaultEncoder; }; -static DebugDraw s_dd; +static DebugDrawEncoderImpl s_dde; +BX_STATIC_ASSERT(sizeof(DebugDrawEncoderImpl) <= sizeof(DebugDrawEncoder), "Size must match"); -void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator) +void ddInit(bx::AllocatorI* _allocator) { - s_dd.init(_depthTestLess, _allocator); + s_dds.init(_allocator); + s_dde.init(bgfx::begin() ); } void ddShutdown() { - s_dd.shutdown(); + s_dde.shutdown(); + s_dds.shutdown(); } SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data) { - return s_dd.createSprite(_width, _height, _data); + return s_dds.createSprite(_width, _height, _data); } void ddDestroy(SpriteHandle _handle) { - s_dd.destroy(_handle); + s_dds.destroy(_handle); +} + +GeometryHandle ddCreateGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32) +{ + return s_dds.createGeometry(_numVertices, _vertices, _numIndices, _indices, _index32); +} + +void ddDestroy(GeometryHandle _handle) +{ + s_dds.destroy(_handle); +} + +#define DEBUG_DRAW_ENCODER(_func) reinterpret_cast<DebugDrawEncoderImpl*>(this)->_func + +DebugDrawEncoder::DebugDrawEncoder() +{ + DEBUG_DRAW_ENCODER(init(s_dde.m_defaultEncoder) ); +} + +DebugDrawEncoder::~DebugDrawEncoder() +{ + DEBUG_DRAW_ENCODER(shutdown() ); +} + +void DebugDrawEncoder::begin(uint16_t _viewId, bool _depthTestLess, bgfx::Encoder* _encoder) +{ + DEBUG_DRAW_ENCODER(begin(_viewId, _depthTestLess, _encoder) ); +} + +void DebugDrawEncoder::end() +{ + DEBUG_DRAW_ENCODER(end() ); +} + +void DebugDrawEncoder::push() +{ + DEBUG_DRAW_ENCODER(push() ); +} + +void DebugDrawEncoder::pop() +{ + DEBUG_DRAW_ENCODER(pop() ); +} + +void DebugDrawEncoder::setDepthTestLess(bool _depthTestLess) +{ + DEBUG_DRAW_ENCODER(setDepthTestLess(_depthTestLess) ); +} + +void DebugDrawEncoder::setState(bool _depthTest, bool _depthWrite, bool _clockwise) +{ + DEBUG_DRAW_ENCODER(setState(_depthTest, _depthWrite, _clockwise) ); } -void ddBegin(uint8_t _viewId) +void DebugDrawEncoder::setColor(uint32_t _abgr) { - s_dd.begin(_viewId); + DEBUG_DRAW_ENCODER(setColor(_abgr) ); } -void ddEnd() +void DebugDrawEncoder::setLod(uint8_t _lod) { - s_dd.end(); + DEBUG_DRAW_ENCODER(setLod(_lod) ); } -void ddPush() +void DebugDrawEncoder::setWireframe(bool _wireframe) { - s_dd.push(); + DEBUG_DRAW_ENCODER(setWireframe(_wireframe) ); } -void ddPop() +void DebugDrawEncoder::setStipple(bool _stipple, float _scale, float _offset) { - s_dd.pop(); + DEBUG_DRAW_ENCODER(setStipple(_stipple, _scale, _offset) ); } -void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise) +void DebugDrawEncoder::setSpin(float _spin) { - s_dd.setState(_depthTest, _depthWrite, _clockwise); + DEBUG_DRAW_ENCODER(setSpin(_spin) ); } -void ddSetColor(uint32_t _abgr) +void DebugDrawEncoder::setTransform(const void* _mtx) { - s_dd.setColor(_abgr); + DEBUG_DRAW_ENCODER(setTransform(_mtx) ); } -void ddSetLod(uint8_t _lod) +void DebugDrawEncoder::setTranslate(float _x, float _y, float _z) { - s_dd.setLod(_lod); + DEBUG_DRAW_ENCODER(setTranslate(_x, _y, _z) ); } -void ddSetWireframe(bool _wireframe) +void DebugDrawEncoder::pushTransform(const void* _mtx) { - s_dd.setWireframe(_wireframe); + DEBUG_DRAW_ENCODER(pushTransform(_mtx, 1) ); } -void ddSetStipple(bool _stipple, float _scale, float _offset) +void DebugDrawEncoder::popTransform() { - s_dd.setStipple(_stipple, _scale, _offset); + DEBUG_DRAW_ENCODER(popTransform() ); } -void ddSetSpin(float _spin) +void DebugDrawEncoder::moveTo(float _x, float _y, float _z) { - s_dd.setSpin(_spin); + DEBUG_DRAW_ENCODER(moveTo(_x, _y, _z) ); } -void ddSetTransform(const void* _mtx) +void DebugDrawEncoder::moveTo(const void* _pos) { - s_dd.setTransform(_mtx); + DEBUG_DRAW_ENCODER(moveTo(_pos) ); } -void ddSetTranslate(float _x, float _y, float _z) +void DebugDrawEncoder::lineTo(float _x, float _y, float _z) { - s_dd.setTranslate(_x, _y, _z); + DEBUG_DRAW_ENCODER(lineTo(_x, _y, _z) ); } -void ddMoveTo(float _x, float _y, float _z) +void DebugDrawEncoder::lineTo(const void* _pos) { - s_dd.moveTo(_x, _y, _z); + DEBUG_DRAW_ENCODER(lineTo(_pos) ); } -void ddMoveTo(const void* _pos) +void DebugDrawEncoder::close() { - s_dd.moveTo(_pos); + DEBUG_DRAW_ENCODER(close() ); } -void ddLineTo(float _x, float _y, float _z) +void DebugDrawEncoder::draw(const Aabb& _aabb) { - s_dd.lineTo(_x, _y, _z); + DEBUG_DRAW_ENCODER(draw(_aabb) ); } -void ddLineTo(const void* _pos) +void DebugDrawEncoder::draw(const Cylinder& _cylinder) { - s_dd.lineTo(_pos); + DEBUG_DRAW_ENCODER(draw(_cylinder, false) ); } -void ddClose() +void DebugDrawEncoder::draw(const Capsule& _capsule) { - s_dd.close(); + DEBUG_DRAW_ENCODER(draw(*( (const Cylinder*)&_capsule), true) ); } -void ddDraw(const Aabb& _aabb) +void DebugDrawEncoder::draw(const Disk& _disk) { - s_dd.draw(_aabb); + DEBUG_DRAW_ENCODER(draw(_disk) ); } -void ddDraw(const Cylinder& _cylinder) +void DebugDrawEncoder::draw(const Obb& _obb) { - s_dd.draw(_cylinder, false); + DEBUG_DRAW_ENCODER(draw(_obb) ); } -void ddDraw(const Capsule& _capsule) +void DebugDrawEncoder::draw(const Sphere& _sphere) { - s_dd.draw( *( (const Cylinder*)&_capsule), true); + DEBUG_DRAW_ENCODER(draw(_sphere) ); } -void ddDraw(const Disk& _disk) +void DebugDrawEncoder::draw(const Cone& _cone) { - s_dd.draw(_disk); + DEBUG_DRAW_ENCODER(drawCone(&_cone.m_pos.x, &_cone.m_end.x, _cone.m_radius) ); } -void ddDraw(const Obb& _obb) +void DebugDrawEncoder::draw(GeometryHandle _handle) { - s_dd.draw(_obb); + DEBUG_DRAW_ENCODER(draw(_handle) ); } -void ddDraw(const Sphere& _sphere) +void DebugDrawEncoder::drawLineList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices) { - s_dd.draw(_sphere); + DEBUG_DRAW_ENCODER(draw(true, _numVertices, _vertices, _numIndices, _indices) ); } -void ddDraw(const Cone& _cone) +void DebugDrawEncoder::drawTriList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices) { - ddDrawCone(_cone.m_pos, _cone.m_end, _cone.m_radius); + DEBUG_DRAW_ENCODER(draw(false, _numVertices, _vertices, _numIndices, _indices) ); } -void ddDrawFrustum(const void* _viewProj) +void DebugDrawEncoder::drawFrustum(const void* _viewProj) { - s_dd.drawFrustum(_viewProj); + DEBUG_DRAW_ENCODER(drawFrustum(_viewProj) ); } -void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees) +void DebugDrawEncoder::drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees) { - s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees); + DEBUG_DRAW_ENCODER(drawArc(_axis, _x, _y, _z, _radius, _degrees) ); } -void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight) +void DebugDrawEncoder::drawCircle(const void* _normal, const void* _center, float _radius, float _weight) { - s_dd.drawCircle(_normal, _center, _radius, _weight); + DEBUG_DRAW_ENCODER(drawCircle(_normal, _center, _radius, _weight) ); } -void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight) +void DebugDrawEncoder::drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight) { - s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight); + DEBUG_DRAW_ENCODER(drawCircle(_axis, _x, _y, _z, _radius, _weight) ); } -void ddDrawQuad(const float* _normal, const float* _center, float _size) +void DebugDrawEncoder::drawQuad(const float* _normal, const float* _center, float _size) { - s_dd.drawQuad(_normal, _center, _size); + DEBUG_DRAW_ENCODER(drawQuad(_normal, _center, _size) ); } -void ddDrawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size) +void DebugDrawEncoder::drawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size) { - s_dd.drawQuad(_handle, _normal, _center, _size); + DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) ); } -void ddDrawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size) +void DebugDrawEncoder::drawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size) { - s_dd.drawQuad(_handle, _normal, _center, _size); + DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) ); } -void ddDrawCone(const void* _from, const void* _to, float _radius) +void DebugDrawEncoder::drawCone(const void* _from, const void* _to, float _radius) { - s_dd.drawCone(_from, _to, _radius); + DEBUG_DRAW_ENCODER(drawCone(_from, _to, _radius) ); } -void ddDrawCylinder(const void* _from, const void* _to, float _radius) +void DebugDrawEncoder::drawCylinder(const void* _from, const void* _to, float _radius) { - s_dd.drawCylinder(_from, _to, _radius, false); + DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, false) ); } -void ddDrawCapsule(const void* _from, const void* _to, float _radius) +void DebugDrawEncoder::drawCapsule(const void* _from, const void* _to, float _radius) { - s_dd.drawCylinder(_from, _to, _radius, true); + DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, true) ); } -void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight, float _thickness) +void DebugDrawEncoder::drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness) { - s_dd.drawAxis(_x, _y, _z, _len, _hightlight, _thickness); + DEBUG_DRAW_ENCODER(drawAxis(_x, _y, _z, _len, _highlight, _thickness) ); } -void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step) +void DebugDrawEncoder::drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step) { - s_dd.drawGrid(_normal, _center, _size, _step); + DEBUG_DRAW_ENCODER(drawGrid(_normal, _center, _size, _step) ); } -void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step) +void DebugDrawEncoder::drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step) { - s_dd.drawGrid(_axis, _center, _size, _step); + DEBUG_DRAW_ENCODER(drawGrid(_axis, _center, _size, _step) ); } -void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight) +void DebugDrawEncoder::drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _highlight) { - s_dd.drawOrb(_x, _y, _z, _radius, _hightlight); + DEBUG_DRAW_ENCODER(drawOrb(_x, _y, _z, _radius, _highlight) ); } |