summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp')
-rw-r--r--3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp246
1 files changed, 126 insertions, 120 deletions
diff --git a/3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp b/3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp
index 00134c7c4c5..3d292ec4454 100644
--- a/3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp
+++ b/3rdparty/bgfx/examples/31-rsm/reflectiveshadowmap.cpp
@@ -3,12 +3,15 @@
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
-#include "common.h"
-#include "camera.h"
-#include "bgfx_utils.h"
-#include "imgui/imgui.h"
+#include <common.h>
+#include <camera.h>
+#include <bgfx_utils.h>
+#include <imgui/imgui.h>
#include <bx/rng.h>
+namespace
+{
+
/*
* Intro
* =====
@@ -176,15 +179,16 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf
vertex[2].m_u = maxu;
vertex[2].m_v = maxv;
- bgfx::setVertexBuffer(&vb);
+ bgfx::setVertexBuffer(0, &vb);
}
}
class ExampleRSM : public entry::AppI
{
public:
- ExampleRSM()
- : m_reading(0)
+ ExampleRSM(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ , m_reading(0)
, m_currFrame(UINT32_MAX)
, m_cameraSpin(false)
, m_lightElevation(35.0f)
@@ -195,13 +199,13 @@ public:
{
}
- void init(int _argc, char** _argv) BX_OVERRIDE
+ void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
- m_width = 1280;
- m_height = 720;
- m_debug = BGFX_DEBUG_TEXT;
+ m_width = _width;
+ m_height = _height;
+ m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
@@ -219,25 +223,25 @@ public:
// Set up screen clears
bgfx::setViewClear(RENDER_PASS_GBUFFER
- , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
- , 0
- , 1.0f
- , 0
- );
+ , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
+ , 0
+ , 1.0f
+ , 0
+ );
bgfx::setViewClear(RENDER_PASS_LIGHT_BUFFER
- , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
- , 0
- , 1.0f
- , 0
- );
+ , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
+ , 0
+ , 1.0f
+ , 0
+ );
bgfx::setViewClear(RENDER_PASS_SHADOW_MAP
- , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
- , 0
- , 1.0f
- , 0
- );
+ , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
+ , 0
+ , 1.0f
+ , 0
+ );
// Create uniforms
u_tint = bgfx::createUniform("u_tint", bgfx::UniformType::Vec4); // Tint for when you click on items
@@ -371,7 +375,7 @@ public:
imguiCreate();
}
- int shutdown() BX_OVERRIDE
+ int shutdown() override
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
{
@@ -382,39 +386,39 @@ public:
meshUnload(m_lightSphere);
// Cleanup.
- bgfx::destroyProgram(m_gbufferProgram);
- bgfx::destroyProgram(m_lightProgram);
- bgfx::destroyProgram(m_combineProgram);
- bgfx::destroyProgram(m_shadowProgram);
-
- bgfx::destroyUniform(u_tint);
- bgfx::destroyUniform(u_lightDir);
- bgfx::destroyUniform(u_sphereInfo);
- bgfx::destroyUniform(u_invMvp);
- bgfx::destroyUniform(u_invMvpShadow);
- bgfx::destroyUniform(u_lightMtx);
- bgfx::destroyUniform(u_shadowDimsInv);
- bgfx::destroyUniform(u_rsmAmount);
- bgfx::destroyUniform(s_normal);
- bgfx::destroyUniform(s_depth);
- bgfx::destroyUniform(s_light);
- bgfx::destroyUniform(s_color);
- bgfx::destroyUniform(s_shadowMap);
- bgfx::destroyUniform(s_rsm);
-
- bgfx::destroyFrameBuffer(m_gbuffer);
- bgfx::destroyFrameBuffer(m_lightBuffer);
- bgfx::destroyFrameBuffer(m_shadowBuffer);
+ bgfx::destroy(m_gbufferProgram);
+ bgfx::destroy(m_lightProgram);
+ bgfx::destroy(m_combineProgram);
+ bgfx::destroy(m_shadowProgram);
+
+ bgfx::destroy(u_tint);
+ bgfx::destroy(u_lightDir);
+ bgfx::destroy(u_sphereInfo);
+ bgfx::destroy(u_invMvp);
+ bgfx::destroy(u_invMvpShadow);
+ bgfx::destroy(u_lightMtx);
+ bgfx::destroy(u_shadowDimsInv);
+ bgfx::destroy(u_rsmAmount);
+ bgfx::destroy(s_normal);
+ bgfx::destroy(s_depth);
+ bgfx::destroy(s_light);
+ bgfx::destroy(s_color);
+ bgfx::destroy(s_shadowMap);
+ bgfx::destroy(s_rsm);
+
+ bgfx::destroy(m_gbuffer);
+ bgfx::destroy(m_lightBuffer);
+ bgfx::destroy(m_shadowBuffer);
for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
{
- bgfx::destroyTexture(m_gbufferTex[ii]);
+ bgfx::destroy(m_gbufferTex[ii]);
}
- bgfx::destroyTexture(m_lightBufferTex);
+ bgfx::destroy(m_lightBufferTex);
for (uint32_t ii = 0; ii < BX_COUNTOF(m_shadowBufferTex); ++ii)
{
- bgfx::destroyTexture(m_shadowBufferTex[ii]);
+ bgfx::destroy(m_shadowBufferTex[ii]);
}
cameraDestroy();
@@ -427,7 +431,7 @@ public:
return 0;
}
- bool update() BX_OVERRIDE
+ bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
@@ -437,15 +441,8 @@ public:
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency());
- const double toMs = 1000.0 / freq;
const float deltaTime = float(frameTime/freq);
- // Use debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/31-rsm");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Global Illumination with Reflective Shadow Map.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
-
// Update camera
cameraUpdate(deltaTime*0.15f, m_mouseState);
@@ -477,12 +474,8 @@ public:
bx::mtxLookAt(smView, lightEye, lightAt);
const float area = 10.0f;
- bgfx::RendererType::Enum renderer = bgfx::getRendererType();
- bool flipV = false
- || renderer == bgfx::RendererType::OpenGL
- || renderer == bgfx::RendererType::OpenGLES
- ;
- bx::mtxOrtho(smProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, flipV);
+ const bgfx::Caps* caps = bgfx::getCaps();
+ bx::mtxOrtho(smProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(RENDER_PASS_SHADOW_MAP, smView, smProj);
bgfx::setViewFrameBuffer(RENDER_PASS_SHADOW_MAP, m_shadowBuffer);
bgfx::setViewRect(RENDER_PASS_SHADOW_MAP, 0, 0, SHADOW_MAP_DIM, SHADOW_MAP_DIM);
@@ -509,10 +502,10 @@ public:
bx::mtxInverse(invMvpShadow, lightMtx);
// Draw some lights (these should really be instanced but for this example they aren't...)
- const unsigned MAX_SPHERE = 32;
- for (uint32_t i = 0; i < MAX_SPHERE; i++)
+ const uint32_t kMaxSpheres = 32;
+ for (uint32_t i = 0; i < kMaxSpheres; i++)
{
- for (uint32_t j = 0; j < MAX_SPHERE; j++)
+ for (uint32_t j = 0; j < kMaxSpheres; j++)
{
// These are used in the fragment shader
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL) ); // Normal for lighting calculations
@@ -525,8 +518,8 @@ public:
bgfx::setUniform(u_invMvp, invMvp);
bgfx::setUniform(u_invMvpShadow, invMvpShadow);
float sphereInfo[4];
- sphereInfo[0] = ((float)i/(MAX_SPHERE-1));
- sphereInfo[1] = ((float)j/(MAX_SPHERE-1));
+ sphereInfo[0] = ((float)i/(kMaxSpheres-1));
+ sphereInfo[1] = ((float)j/(kMaxSpheres-1));
sphereInfo[2] = m_vplRadius;
sphereInfo[3] = 0.0; // Unused
bgfx::setUniform(u_sphereInfo, sphereInfo);
@@ -539,12 +532,12 @@ public:
;
meshSubmit(
- m_lightSphere,
- RENDER_PASS_LIGHT_BUFFER,
- m_lightProgram,
- NULL,
- lightDrawState
- );
+ m_lightSphere
+ , RENDER_PASS_LIGHT_BUFFER
+ , m_lightProgram
+ , NULL
+ , lightDrawState
+ );
}
}
@@ -572,13 +565,13 @@ public:
// Set up state for combine pass
// point of this is to avoid doing depth test, which is in the default state
bgfx::setState(0
- | BGFX_STATE_RGB_WRITE
- | BGFX_STATE_ALPHA_WRITE
- );
+ | BGFX_STATE_RGB_WRITE
+ | BGFX_STATE_ALPHA_WRITE
+ );
// Set up transform matrix for fullscreen quad
float orthoProj[16];
- bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
+ bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(RENDER_PASS_COMBINE, NULL, orthoProj);
bgfx::setViewRect(RENDER_PASS_COMBINE, 0, 0, uint16_t(m_width), uint16_t(m_height) );
// Bind vertex buffer and draw quad
@@ -587,23 +580,34 @@ public:
// Draw UI
imguiBeginFrame(m_mouseState.m_mx
- , m_mouseState.m_my
- , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
- | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
- | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
- , m_mouseState.m_mz
- , uint16_t(m_width)
- , uint16_t(m_height)
- );
-
- imguiBeginArea("RSM:", 10, 100, 300, 400);
-
- imguiSlider("rsm amount", m_rsmAmount, 0.0f, 0.7f, 0.01f);
- imguiSlider("vpl radius", m_vplRadius, 0.25f, 20.0f, 0.1f);
- imguiSlider("light azimuth", m_lightAzimuth, 0.0f, 360.0f, 0.01f);
- imguiSlider("light elevation", m_lightElevation, 35.0f, 90.0f, 0.01f);
-
- imguiEndArea();
+ , m_mouseState.m_my
+ , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
+ | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
+ | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
+ , m_mouseState.m_mz
+ , uint16_t(m_width)
+ , uint16_t(m_height)
+ );
+
+ showExampleDialog(this);
+
+ ImGui::SetNextWindowPos(
+ ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
+ , ImGuiSetCond_FirstUseEver
+ );
+ ImGui::Begin("Settings"
+ , NULL
+ , ImVec2(m_width / 5.0f, m_height / 3.0f)
+ , ImGuiWindowFlags_AlwaysAutoResize
+ );
+
+ ImGui::SliderFloat("RSM Amount", &m_rsmAmount, 0.0f, 0.7f);
+ ImGui::SliderFloat("VPL Radius", &m_vplRadius, 0.25f, 20.0f);
+ ImGui::SliderFloat("Light Azimuth", &m_lightAzimuth, 0.0f, 360.0f);
+ ImGui::SliderFloat("Light Elevation", &m_lightElevation, 35.0f, 90.0f);
+
+ ImGui::End();
+
imguiEndFrame();
updateLightDir();
@@ -628,16 +632,16 @@ public:
float scale = s_meshScale[model.mesh];
float mtx[16];
bx::mtxSRT(mtx
- , scale
- , scale
- , scale
- , 0.0f
- , 0.0f
- , 0.0f
- , model.position[0]
- , model.position[1]
- , model.position[2]
- );
+ , scale
+ , scale
+ , scale
+ , 0.0f
+ , 0.0f
+ , 0.0f
+ , model.position[0]
+ , model.position[1]
+ , model.position[2]
+ );
// Submit mesh to gbuffer
bgfx::setUniform(u_tint, model.color);
@@ -650,16 +654,16 @@ public:
float mtxScale[16];
float scale = 10.0;
bx::mtxScale(mtxScale
- , scale
- , scale
- , scale
- );
+ , scale
+ , scale
+ , scale
+ );
float mtxTrans[16];
bx::mtxTranslate(mtxTrans
- , 0.0f
- , -10.0f
- , 0.0f
- );
+ , 0.0f
+ , -10.0f
+ , 0.0f
+ );
float mtx[16];
bx::mtxMul(mtx, mtxScale, mtxTrans);
meshSubmit(m_ground, _pass, _program, mtx);
@@ -667,8 +671,8 @@ public:
void updateLightDir()
{
- float el = m_lightElevation * (bx::pi/180.0f);
- float az = m_lightAzimuth * (bx::pi/180.0f);
+ float el = m_lightElevation * (bx::kPi/180.0f);
+ float az = m_lightAzimuth * (bx::kPi/180.0f);
m_lightDir[0] = bx::fcos(el)*bx::fcos(az);
m_lightDir[2] = bx::fcos(el)*bx::fsin(az);
m_lightDir[1] = bx::fsin(el);
@@ -746,4 +750,6 @@ public:
float m_texelHalf;
};
-ENTRY_IMPLEMENT_MAIN(ExampleRSM);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleRSM, "31-rsm", "Global Illumination with Reflective Shadow Map.");