diff options
Diffstat (limited to '3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp | 232 |
1 files changed, 115 insertions, 117 deletions
diff --git a/3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp b/3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp index 4c9a020604a..8f16c432521 100644 --- a/3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp +++ b/3rdparty/bgfx/examples/16-shadowmaps/shadowmaps.cpp @@ -20,7 +20,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 @@ -184,10 +184,12 @@ static bgfx::UniformHandle s_shadowMap[ShadowMapRenderTargets::Count]; static bgfx::FrameBufferHandle s_rtShadowMap[ShadowMapRenderTargets::Count]; static bgfx::FrameBufferHandle s_rtBlur; -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]; @@ -207,7 +209,7 @@ void mtxBillboard(float* __restrict _result _result[15] = 1.0f; } -void mtxYawPitchRoll(float* __restrict _result +void mtxYawPitchRoll(float* _result , float _yaw , float _pitch , float _roll @@ -812,15 +814,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); @@ -851,15 +853,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; @@ -977,7 +979,7 @@ struct Mesh } } - bgfx::VertexDecl m_decl; + bgfx::VertexLayout m_layout; typedef std::vector<Group> GroupArray; GroupArray m_groups; }; @@ -993,7 +995,7 @@ struct PosColorTexCoord0Vertex static void init() { - ms_decl + ms_layout .begin() .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) @@ -1001,17 +1003,17 @@ struct PosColorTexCoord0Vertex .end(); } - static bgfx::VertexDecl ms_decl; + static bgfx::VertexLayout ms_layout; }; -bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl; +bgfx::VertexLayout PosColorTexCoord0Vertex::ms_layout; void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = true, float _width = 1.0f, float _height = 1.0f) { - if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) ) + if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_layout) ) { bgfx::TransientVertexBuffer vb; - bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl); + bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_layout); PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data; const float zz = 0.0f; @@ -1061,12 +1063,13 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBott } } -void worldSpaceFrustumCorners(float* _corners24f +void worldSpaceFrustumCorners( + float* _corners24f , float _near , float _far , float _projWidth , float _projHeight - , const float* __restrict _invViewMtx + , const float* _invViewMtx ) { // Define frustum corners in view space. @@ -1076,7 +1079,7 @@ void worldSpaceFrustumCorners(float* _corners24f const float fh = _far * _projHeight; const uint8_t numCorners = 8; - const float corners[numCorners][3] = + const bx::Vec3 corners[numCorners] = { { -nw, nh, _near }, { nw, nh, _near }, @@ -1092,7 +1095,7 @@ void worldSpaceFrustumCorners(float* _corners24f float (*out)[3] = (float(*)[3])_corners24f; for (uint8_t ii = 0; ii < numCorners; ++ii) { - bx::vec3MulMtx( (float*)&out[ii], (float*)&corners[ii], _invViewMtx); + bx::store(&out[ii], bx::mul(corners[ii], _invViewMtx) ); } } @@ -1284,8 +1287,8 @@ struct SceneSettings class ExampleShadowmaps : public entry::AppI { public: - ExampleShadowmaps(const char* _name, const char* _description) - : entry::AppI(_name, _description) + ExampleShadowmaps(const char* _name, const char* _description, const char* _url) + : entry::AppI(_name, _description, _url) { } @@ -1334,26 +1337,26 @@ public: // Uniforms. s_uniforms.init(); - s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); - s_shadowMap[0] = bgfx::createUniform("s_shadowMap0", bgfx::UniformType::Int1); - s_shadowMap[1] = bgfx::createUniform("s_shadowMap1", bgfx::UniformType::Int1); - s_shadowMap[2] = bgfx::createUniform("s_shadowMap2", bgfx::UniformType::Int1); - s_shadowMap[3] = bgfx::createUniform("s_shadowMap3", bgfx::UniformType::Int1); + s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler); + s_shadowMap[0] = bgfx::createUniform("s_shadowMap0", bgfx::UniformType::Sampler); + s_shadowMap[1] = bgfx::createUniform("s_shadowMap1", bgfx::UniformType::Sampler); + s_shadowMap[2] = bgfx::createUniform("s_shadowMap2", bgfx::UniformType::Sampler); + s_shadowMap[3] = bgfx::createUniform("s_shadowMap3", bgfx::UniformType::Sampler); // Programs. s_programs.init(); // Vertex declarations. - bgfx::VertexDecl PosNormalTexcoordDecl; - PosNormalTexcoordDecl.begin() + bgfx::VertexLayout PosNormalTexcoordLayout; + PosNormalTexcoordLayout.begin() .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true) .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float) .end(); - m_posDecl.begin(); - m_posDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float); - m_posDecl.end(); + m_posLayout.begin(); + m_posLayout.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float); + m_posLayout.end(); PosColorTexCoord0Vertex::init(); @@ -1367,8 +1370,8 @@ public: m_treeMesh.load("meshes/tree.bin"); m_cubeMesh.load("meshes/cube.bin"); m_hollowcubeMesh.load("meshes/hollowcube.bin"); - m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) ); - m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) ); + m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordLayout, s_planeIndices, BX_COUNTOF(s_planeIndices) ); + m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordLayout, s_planeIndices, BX_COUNTOF(s_planeIndices) ); // Materials. m_defaultMaterial = @@ -1901,9 +1904,8 @@ public: s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8); // Setup camera. - float initialPos[3] = { 0.0f, 60.0f, -105.0f }; cameraCreate(); - cameraSetPosition(initialPos); + cameraSetPosition({ 0.0f, 60.0f, -105.0f }); cameraSetVerticalAngle(-0.45f); m_timeAccumulatorLight = 0.0f; @@ -2313,6 +2315,45 @@ public: , caps->homogeneousDepth ); + // Update render target size. + uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo); + if (bLtChanged || m_currentShadowMapSize != shadowMapSize) + { + m_currentShadowMapSize = shadowMapSize; + s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef; + + { + bgfx::destroy(s_rtShadowMap[0]); + + bgfx::TextureHandle fbtextures[] = + { + bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT), + bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT), + }; + s_rtShadowMap[0] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); + } + + if (LightType::DirectionalLight == m_settings.m_lightType) + { + for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii) + { + { + bgfx::destroy(s_rtShadowMap[ii]); + + bgfx::TextureHandle fbtextures[] = + { + bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT), + bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT), + }; + s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); + } + } + } + + bgfx::destroy(s_rtBlur); + s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8); + } + if (LightType::SpotLight == m_settings.m_lightType) { const float fovy = m_settings.m_coverageSpotL; @@ -2333,9 +2374,8 @@ public: lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far; } - float at[3]; - bx::vec3Add(at, m_pointLight.m_position.m_v, m_pointLight.m_spotDirectionInner.m_v); - bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load(m_pointLight.m_position.m_v), bx::load(at) ); + const bx::Vec3 at = bx::add(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(m_pointLight.m_spotDirectionInner.m_v) ); + bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load<bx::Vec3>(m_pointLight.m_position.m_v), at); } else if (LightType::PointLight == m_settings.m_lightType) { @@ -2404,9 +2444,9 @@ public: float tmp[3] = { - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[0]), - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[4]), - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[8]), + -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[0]) ), + -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[4]) ), + -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[8]) ), }; bx::mtxTranspose(mtxYpr[ii], mtxTmp); @@ -2455,16 +2495,16 @@ public: float mtxProj[16]; bx::mtxOrtho( - mtxProj - , 1.0f - , -1.0f - , 1.0f - , -1.0f - , -currentSmSettings->m_far - , currentSmSettings->m_far - , 0.0f - , caps->homogeneousDepth - ); + mtxProj + , 1.0f + , -1.0f + , 1.0f + , -1.0f + , -currentSmSettings->m_far + , currentSmSettings->m_far + , 0.0f + , caps->homogeneousDepth + ); const uint8_t numCorners = 8; float frustumCorners[maxNumSplits][numCorners][3]; @@ -2473,34 +2513,24 @@ public: // Compute frustum corners for one split in world space. worldSpaceFrustumCorners( (float*)frustumCorners[ii], splitSlices[nn], splitSlices[ff], projWidth, projHeight, mtxViewInv); - float min[3] = { 9000.0f, 9000.0f, 9000.0f }; - float max[3] = { -9000.0f, -9000.0f, -9000.0f }; + bx::Vec3 min = { 9000.0f, 9000.0f, 9000.0f }; + bx::Vec3 max = { -9000.0f, -9000.0f, -9000.0f }; for (uint8_t jj = 0; jj < numCorners; ++jj) { // Transform to light space. - float lightSpaceFrustumCorner[3]; - bx::vec3MulMtx(lightSpaceFrustumCorner, frustumCorners[ii][jj], lightView[0]); + const bx::Vec3 xyz = bx::mul(bx::load<bx::Vec3>(frustumCorners[ii][jj]), lightView[0]); // Update bounding box. - min[0] = bx::min(min[0], lightSpaceFrustumCorner[0]); - max[0] = bx::max(max[0], lightSpaceFrustumCorner[0]); - min[1] = bx::min(min[1], lightSpaceFrustumCorner[1]); - max[1] = bx::max(max[1], lightSpaceFrustumCorner[1]); - min[2] = bx::min(min[2], lightSpaceFrustumCorner[2]); - max[2] = bx::max(max[2], lightSpaceFrustumCorner[2]); + min = bx::min(min, xyz); + max = bx::max(max, xyz); } - float minproj[3]; - float maxproj[3]; - bx::vec3MulMtxH(minproj, min, mtxProj); - bx::vec3MulMtxH(maxproj, max, mtxProj); - - float offsetx, offsety; - float scalex, scaley; + const bx::Vec3 minproj = bx::mulH(min, mtxProj); + const bx::Vec3 maxproj = bx::mulH(max, mtxProj); - scalex = 2.0f / (maxproj[0] - minproj[0]); - scaley = 2.0f / (maxproj[1] - minproj[1]); + float scalex = 2.0f / (maxproj.x - minproj.x); + float scaley = 2.0f / (maxproj.y - minproj.y); if (m_settings.m_stabilize) { @@ -2509,8 +2539,8 @@ public: scaley = quantizer / bx::ceil(quantizer / scaley); } - offsetx = 0.5f * (maxproj[0] + minproj[0]) * scalex; - offsety = 0.5f * (maxproj[1] + minproj[1]) * scaley; + float offsetx = 0.5f * (maxproj.x + minproj.x) * scalex; + float offsety = 0.5f * (maxproj.y + minproj.y) * scaley; if (m_settings.m_stabilize) { @@ -2535,6 +2565,7 @@ public: for (uint8_t ii = 0; ii < RENDERVIEW_DRAWDEPTH_3_ID+1; ++ii) { bgfx::setViewFrameBuffer(ii, invalidRt); + bgfx::setViewRect(ii, 0, 0, m_viewState.m_width, m_viewState.m_height); } // Determine on-screen rectangle size where depth buffer will be drawn. @@ -2773,7 +2804,7 @@ public: // Craft stencil mask for point light shadow map packing. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack) { - if (6 == bgfx::getAvailTransientVertexBuffer(6, m_posDecl) ) + if (6 == bgfx::getAvailTransientVertexBuffer(6, m_posLayout) ) { struct Pos { @@ -2781,7 +2812,7 @@ public: }; bgfx::TransientVertexBuffer vb; - bgfx::allocTransientVertexBuffer(&vb, 6, m_posDecl); + bgfx::allocTransientVertexBuffer(&vb, 6, m_posLayout); Pos* vertex = (Pos*)vb.data; const float min = 0.0f; @@ -3171,44 +3202,6 @@ public: } } - // Update render target size. - uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo); - if (bLtChanged || m_currentShadowMapSize != shadowMapSize) - { - m_currentShadowMapSize = shadowMapSize; - s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef; - - { - bgfx::destroy(s_rtShadowMap[0]); - - bgfx::TextureHandle fbtextures[] = - { - bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT), - bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT), - }; - s_rtShadowMap[0] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); - } - - if (LightType::DirectionalLight == m_settings.m_lightType) - { - for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii) - { - { - bgfx::destroy(s_rtShadowMap[ii]); - - bgfx::TextureHandle fbtextures[] = - { - bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT), - bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT), - }; - s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); - } - } - } - - bgfx::destroy(s_rtBlur); - s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8); - } // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. @@ -3229,7 +3222,7 @@ public: ViewState m_viewState; ClearValues m_clearValues; - bgfx::VertexDecl m_posDecl; + bgfx::VertexLayout m_posLayout; bgfx::TextureHandle m_texFigure; bgfx::TextureHandle m_texFlare; @@ -3261,4 +3254,9 @@ public: } // namespace -ENTRY_IMPLEMENT_MAIN(ExampleShadowmaps, "16-shadowmaps", "Shadow maps example."); +ENTRY_IMPLEMENT_MAIN( + ExampleShadowmaps + , "16-shadowmaps" + , "Shadow maps example." + , "https://bkaradzic.github.io/bgfx/examples.html#shadowmaps" + ); |