summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/13-stencil/stencil.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/13-stencil/stencil.cpp')
-rw-r--r--3rdparty/bgfx/examples/13-stencil/stencil.cpp134
1 files changed, 65 insertions, 69 deletions
diff --git a/3rdparty/bgfx/examples/13-stencil/stencil.cpp b/3rdparty/bgfx/examples/13-stencil/stencil.cpp
index 5308757e87f..f2253f39bbf 100644
--- a/3rdparty/bgfx/examples/13-stencil/stencil.cpp
+++ b/3rdparty/bgfx/examples/13-stencil/stencil.cpp
@@ -15,7 +15,7 @@
namespace bgfx
{
- int32_t read(bx::ReaderI* _reader, bgfx::VertexDecl& _decl, bx::Error* _err = NULL);
+ int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err = NULL);
}
namespace
@@ -43,7 +43,7 @@ struct PosNormalTexcoordVertex
static void init()
{
- ms_decl
+ ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
@@ -51,10 +51,10 @@ struct PosNormalTexcoordVertex
.end();
}
- static bgfx::VertexDecl ms_decl;
+ static bgfx::VertexLayout ms_layout;
};
-bgfx::VertexDecl PosNormalTexcoordVertex::ms_decl;
+bgfx::VertexLayout PosNormalTexcoordVertex::ms_layout;
static const float s_texcoord = 5.0f;
static PosNormalTexcoordVertex s_hplaneVertices[] =
@@ -131,8 +131,9 @@ static bgfx::UniformHandle s_texColor;
void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
{
- for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
+ for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
+ const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
@@ -142,8 +143,9 @@ void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float
void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj)
{
- for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
+ for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
+ const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
@@ -153,8 +155,9 @@ void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _pr
void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
{
- for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
+ for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 )
{
+ const uint32_t ntz = bx::uint32_cnttz(viewMask);
viewMask >>= ntz;
view += ntz;
@@ -162,40 +165,39 @@ void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _wid
}
}
-void mtxReflected(float*__restrict _result
- , const float* __restrict _p /* plane */
- , const float* __restrict _n /* normal */
- )
+void mtxReflected(float* _result, const bx::Vec3& _pos, const bx::Vec3& _normal)
{
- float dot = bx::vec3Dot(_p, _n);
-
- _result[ 0] = 1.0f - 2.0f * _n[0] * _n[0]; //1-2Nx^2
- _result[ 1] = -2.0f * _n[0] * _n[1]; //-2*Nx*Ny
- _result[ 2] = -2.0f * _n[0] * _n[2]; //-2*NxNz
- _result[ 3] = 0.0f; //0
-
- _result[ 4] = -2.0f * _n[0] * _n[1]; //-2*NxNy
- _result[ 5] = 1.0f - 2.0f * _n[1] * _n[1]; //1-2*Ny^2
- _result[ 6] = -2.0f * _n[1] * _n[2]; //-2*NyNz
- _result[ 7] = 0.0f; //0
-
- _result[ 8] = -2.0f * _n[0] * _n[2]; //-2*NxNz
- _result[ 9] = -2.0f * _n[1] * _n[2]; //-2NyNz
- _result[10] = 1.0f - 2.0f * _n[2] * _n[2]; //1-2*Nz^2
- _result[11] = 0.0f; //0
-
- _result[12] = 2.0f * dot * _n[0]; //2*dot*Nx
- _result[13] = 2.0f * dot * _n[1]; //2*dot*Ny
- _result[14] = 2.0f * dot * _n[2]; //2*dot*Nz
- _result[15] = 1.0f; //1
+ const float nx = _normal.x;
+ const float ny = _normal.y;
+ const float nz = _normal.z;
+
+ _result[ 0] = 1.0f - 2.0f * nx * nx;
+ _result[ 1] = - 2.0f * nx * ny;
+ _result[ 2] = - 2.0f * nx * nz;
+ _result[ 3] = 0.0f;
+
+ _result[ 4] = - 2.0f * nx * ny;
+ _result[ 5] = 1.0f - 2.0f * ny * ny;
+ _result[ 6] = - 2.0f * ny * nz;
+ _result[ 7] = 0.0f;
+
+ _result[ 8] = - 2.0f * nx * nz;
+ _result[ 9] = - 2.0f * ny * nz;
+ _result[10] = 1.0f - 2.0f * nz * nz;
+ _result[11] = 0.0f;
+
+ const float dot = bx::dot(_pos, _normal);
+
+ _result[12] = 2.0f * dot * nx;
+ _result[13] = 2.0f * dot * ny;
+ _result[14] = 2.0f * dot * nz;
+ _result[15] = 1.0f;
}
-void mtxShadow(float* __restrict _result
- , const float* __restrict _ground
- , const float* __restrict _light
- )
+void mtxShadow(float* _result, const float* _ground, const float* _light)
{
- float dot = _ground[0] * _light[0]
+ const float dot =
+ _ground[0] * _light[0]
+ _ground[1] * _light[1]
+ _ground[2] * _light[2]
+ _ground[3] * _light[3]
@@ -222,10 +224,7 @@ void mtxShadow(float* __restrict _result
_result[15] = dot - _light[3] * _ground[3];
}
-void mtxBillboard(float* __restrict _result
- , const float* __restrict _view
- , const float* __restrict _pos
- , const float* __restrict _scale)
+void mtxBillboard(float* _result, const float* _view, const float* _pos, const float* _scale)
{
_result[ 0] = _view[0] * _scale[0];
_result[ 1] = _view[4] * _scale[0];
@@ -623,15 +622,15 @@ struct Group
struct Mesh
{
- void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
+ void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices)
{
Group group;
const bgfx::Memory* mem;
uint32_t size;
- size = _numVertices*_decl.getStride();
+ size = _numVertices*_layout.getStride();
mem = bgfx::makeRef(_vertices, size);
- group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
+ group.m_vbh = bgfx::createVertexBuffer(mem, _layout);
size = _numIndices*2;
mem = bgfx::makeRef(_indices, size);
@@ -662,15 +661,15 @@ struct Mesh
bx::read(reader, group.m_aabb);
bx::read(reader, group.m_obb);
- bgfx::read(reader, m_decl);
- uint16_t stride = m_decl.getStride();
+ bgfx::read(reader, m_layout);
+ uint16_t stride = m_layout.getStride();
uint16_t numVertices;
bx::read(reader, numVertices);
const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
bx::read(reader, mem->data, mem->size);
- group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
+ group.m_vbh = bgfx::createVertexBuffer(mem, m_layout);
}
break;
@@ -781,7 +780,7 @@ struct Mesh
}
}
- bgfx::VertexDecl m_decl;
+ bgfx::VertexLayout m_layout;
typedef std::vector<Group> GroupArray;
GroupArray m_groups;
};
@@ -790,8 +789,8 @@ struct Mesh
class ExampleStencil : public entry::AppI
{
public:
- ExampleStencil(const char* _name, const char* _description)
- : entry::AppI(_name, _description)
+ ExampleStencil(const char* _name, const char* _description, const char* _url)
+ : entry::AppI(_name, _description, _url)
{
}
@@ -823,7 +822,7 @@ public:
s_uniforms.init();
- s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
+ s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
m_programTextureLighting = loadProgram("vs_stencil_texture_lighting", "fs_stencil_texture_lighting");
m_programColorLighting = loadProgram("vs_stencil_color_lighting", "fs_stencil_color_lighting" );
@@ -833,9 +832,9 @@ public:
m_bunnyMesh.load("meshes/bunny.bin");
m_columnMesh.load("meshes/column.bin");
- m_cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_decl, s_cubeIndices, BX_COUNTOF(s_cubeIndices) );
- m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
- m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
+ m_cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_layout, s_cubeIndices, BX_COUNTOF(s_cubeIndices) );
+ m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_layout, s_planeIndices, BX_COUNTOF(s_planeIndices) );
+ m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_layout, s_planeIndices, BX_COUNTOF(s_planeIndices) );
m_figureTex = loadTexture("textures/figure-rgba.dds");
m_flareTex = loadTexture("textures/flare.dds");
@@ -866,9 +865,8 @@ public:
const bgfx::Caps* caps = bgfx::getCaps();
bx::mtxProj(m_viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f, caps->homogeneousDepth);
- float initialPos[3] = { 0.0f, 18.0f, -40.0f };
cameraCreate();
- cameraSetPosition(initialPos);
+ cameraSetPosition({ 0.0f, 18.0f, -40.0f });
cameraSetVerticalAngle(-0.35f);
cameraGetViewMtx(m_viewState.m_view);
@@ -1125,18 +1123,15 @@ public:
// Compute reflected matrix.
float reflectMtx[16];
- float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
- float normal[3] = { 0.0f, 1.0f, 0.0f };
- mtxReflected(reflectMtx, plane_pos, normal);
+ mtxReflected(reflectMtx, { 0.0f, 0.01f, 0.0f }, { 0.0f, 1.0f, 0.0f });
// Reflect lights.
- float reflectedLights[MAX_NUM_LIGHTS][4];
for (uint8_t ii = 0; ii < numLights; ++ii)
{
- bx::vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx);
- reflectedLights[ii][3] = lightPosRadius[ii][3];
+ bx::Vec3 reflected = bx::mul(bx::load<bx::Vec3>(lightPosRadius[ii]), reflectMtx);
+ bx::store(&s_uniforms.m_lightPosRadius[ii], reflected);
+ s_uniforms.m_lightPosRadius[ii][3] = lightPosRadius[ii][3];
}
- bx::memCopy(s_uniforms.m_lightPosRadius, reflectedLights, numLights * 4*sizeof(float) );
// Reflect and submit bunny.
float mtxReflectedBunny[16];
@@ -1226,11 +1221,7 @@ public:
}
// Ground plane.
- float ground[4];
- float plane_pos[3] = { 0.0f, 0.0f, 0.0f };
- float normal[3] = { 0.0f, 1.0f, 0.0f };
- bx::memCopy(ground, normal, sizeof(float) * 3);
- ground[3] = -bx::vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting
+ float ground[4] = { 0.0f, 1.0f, 0.0f, -bx::dot(bx::Vec3{ 0.0f, 0.0f, 0.0f }, bx::Vec3{ 0.0f, 1.0f, 0.0f }) - 0.01f };
for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId)
{
@@ -1411,4 +1402,9 @@ public:
} // namespace
-ENTRY_IMPLEMENT_MAIN(ExampleStencil, "13-stencil", "Stencil reflections and shadows.");
+ENTRY_IMPLEMENT_MAIN(
+ ExampleStencil
+ , "13-stencil"
+ , "Stencil reflections and shadows."
+ , "https://bkaradzic.github.io/bgfx/examples.html#stencil"
+ );