summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/21-deferred/deferred.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/21-deferred/deferred.cpp')
-rw-r--r--3rdparty/bgfx/examples/21-deferred/deferred.cpp246
1 files changed, 112 insertions, 134 deletions
diff --git a/3rdparty/bgfx/examples/21-deferred/deferred.cpp b/3rdparty/bgfx/examples/21-deferred/deferred.cpp
index de37b19f83f..45c8aaf7faf 100644
--- a/3rdparty/bgfx/examples/21-deferred/deferred.cpp
+++ b/3rdparty/bgfx/examples/21-deferred/deferred.cpp
@@ -9,6 +9,9 @@
#include "camera.h"
#include "bounds.h"
+namespace
+{
+
#define RENDER_PASS_GEOMETRY_ID 0
#define RENDER_PASS_LIGHT_ID 1
#define RENDER_PASS_COMBINE_ID 2
@@ -86,57 +89,32 @@ struct DebugVertex
bgfx::VertexDecl DebugVertex::ms_decl;
-uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
-{
- union
- {
- uint32_t ui32;
- uint8_t arr[4];
- } un;
-
- un.arr[0] = _x;
- un.arr[1] = _y;
- un.arr[2] = _z;
- un.arr[3] = _w;
-
- return un.ui32;
-}
-
-uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
-{
- const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
- const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
- const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
- const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
- return packUint32(xx, yy, zz, ww);
-}
-
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
{
- {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
- { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
- { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
- { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
- { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
- {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
- {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
};
static const uint16_t s_cubeIndices[36] =
@@ -208,19 +186,25 @@ 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 ExampleDeferred : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleDeferred(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ {
+ }
+
+ 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);
@@ -298,11 +282,11 @@ class ExampleDeferred : public entry::AppI
// Load normal texture.
m_textureNormal = loadTexture("textures/fieldstone-n.dds");
- m_gbufferTex[0].idx = bgfx::invalidHandle;
- m_gbufferTex[1].idx = bgfx::invalidHandle;
- m_gbufferTex[2].idx = bgfx::invalidHandle;
- m_gbuffer.idx = bgfx::invalidHandle;
- m_lightBuffer.idx = bgfx::invalidHandle;
+ m_gbufferTex[0].idx = bgfx::kInvalidHandle;
+ m_gbufferTex[1].idx = bgfx::kInvalidHandle;
+ m_gbufferTex[2].idx = bgfx::kInvalidHandle;
+ m_gbuffer.idx = bgfx::kInvalidHandle;
+ m_lightBuffer.idx = bgfx::kInvalidHandle;
// Imgui.
imguiCreate();
@@ -332,7 +316,7 @@ class ExampleDeferred : public entry::AppI
cameraSetVerticalAngle(0.0f);
}
- virtual int shutdown() BX_OVERRIDE
+ virtual int shutdown() override
{
// Cleanup.
cameraDestroy();
@@ -340,32 +324,32 @@ class ExampleDeferred : public entry::AppI
if (bgfx::isValid(m_gbuffer) )
{
- bgfx::destroyFrameBuffer(m_gbuffer);
- bgfx::destroyFrameBuffer(m_lightBuffer);
+ bgfx::destroy(m_gbuffer);
+ bgfx::destroy(m_lightBuffer);
}
- bgfx::destroyIndexBuffer(m_ibh);
- bgfx::destroyVertexBuffer(m_vbh);
+ bgfx::destroy(m_ibh);
+ bgfx::destroy(m_vbh);
- bgfx::destroyProgram(m_geomProgram);
- bgfx::destroyProgram(m_lightProgram);
- bgfx::destroyProgram(m_combineProgram);
- bgfx::destroyProgram(m_debugProgram);
- bgfx::destroyProgram(m_lineProgram);
+ bgfx::destroy(m_geomProgram);
+ bgfx::destroy(m_lightProgram);
+ bgfx::destroy(m_combineProgram);
+ bgfx::destroy(m_debugProgram);
+ bgfx::destroy(m_lineProgram);
- bgfx::destroyTexture(m_textureColor);
- bgfx::destroyTexture(m_textureNormal);
- bgfx::destroyUniform(s_texColor);
- bgfx::destroyUniform(s_texNormal);
+ bgfx::destroy(m_textureColor);
+ bgfx::destroy(m_textureNormal);
+ bgfx::destroy(s_texColor);
+ bgfx::destroy(s_texNormal);
- bgfx::destroyUniform(s_albedo);
- bgfx::destroyUniform(s_normal);
- bgfx::destroyUniform(s_depth);
- bgfx::destroyUniform(s_light);
+ bgfx::destroy(s_albedo);
+ bgfx::destroy(s_normal);
+ bgfx::destroy(s_depth);
+ bgfx::destroy(s_light);
- bgfx::destroyUniform(u_lightPosRadius);
- bgfx::destroyUniform(u_lightRgbInnerR);
- bgfx::destroyUniform(u_mtx);
+ bgfx::destroy(u_lightPosRadius);
+ bgfx::destroy(u_lightRgbInnerR);
+ bgfx::destroy(u_mtx);
// Shutdown bgfx.
bgfx::shutdown();
@@ -373,32 +357,37 @@ class ExampleDeferred : public entry::AppI
return 0;
}
- bool update() BX_OVERRIDE
+ bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
+ 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)
+ );
+
+ showExampleDialog(this);
+
int64_t now = bx::getHPCounter();
static int64_t last = now;
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);
float time = (float)( (now-m_timeOffset)/freq);
- // Use m_debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/21-deferred");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: MRT rendering and deferred shading.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
-
if (2 > m_caps->limits.maxFBAttachments)
{
// When multiple render targets (MRT) is not supported by GPU,
// implement alternative code path that doesn't use MRT.
bool blink = uint32_t(time*3.0f)&1;
- bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " MRT not supported by GPU. ");
+ bgfx::dbgTextPrintf(0, 0, blink ? 0x1f : 0x01, " MRT not supported by GPU. ");
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
@@ -421,7 +410,7 @@ class ExampleDeferred : public entry::AppI
if (bgfx::isValid(m_gbuffer) )
{
- bgfx::destroyFrameBuffer(m_gbuffer);
+ bgfx::destroy(m_gbuffer);
}
const uint32_t samplerFlags = 0
@@ -439,46 +428,29 @@ class ExampleDeferred : public entry::AppI
if (bgfx::isValid(m_lightBuffer) )
{
- bgfx::destroyFrameBuffer(m_lightBuffer);
+ bgfx::destroy(m_lightBuffer);
}
m_lightBuffer = bgfx::createFrameBuffer(uint16_t(m_width), uint16_t(m_height), bgfx::TextureFormat::BGRA8, samplerFlags);
}
- 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)
- );
-
- imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
- imguiSeparatorLine();
+ 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
+ );
- imguiSlider("Num lights", m_numLights, 1, 2048);
-
- if (imguiCheck("Show G-Buffer.", m_showGBuffer) )
- {
- m_showGBuffer = !m_showGBuffer;
- }
+ ImGui::SliderInt("Num lights", &m_numLights, 1, 2048);
+ ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer);
+ ImGui::Checkbox("Show light scissor.", &m_showScissorRects);
+ ImGui::Checkbox("Animate mesh.", &m_animateMesh);
+ ImGui::SliderFloat("Anim.speed", &m_lightAnimationSpeed, 0.0f, 0.4f);
- if (imguiCheck("Show light scissor.", m_showScissorRects) )
- {
- m_showScissorRects = !m_showScissorRects;
- }
-
- if (imguiCheck("Animate mesh.", m_animateMesh) )
- {
- m_animateMesh = !m_animateMesh;
- }
-
- imguiSlider("Lights animation speed", m_lightAnimationSpeed, 0.0f, 0.4f, 0.01f);
-
- imguiEndScrollArea();
- imguiEndFrame();
+ ImGui::End();
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
@@ -507,16 +479,18 @@ class ExampleDeferred : public entry::AppI
bx::mtxMul(vp, view, proj);
bx::mtxInverse(invMvp, vp);
- bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
+ const bgfx::Caps* caps = bgfx::getCaps();
+
+ bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(RENDER_PASS_LIGHT_ID, NULL, proj);
bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
const float aspectRatio = float(m_height)/float(m_width);
const float size = 10.0f;
- bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
+ bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
- bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f);
+ bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
}
@@ -545,7 +519,7 @@ class ExampleDeferred : public entry::AppI
bgfx::setTransform(mtx);
// Set vertex and index buffer.
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Bind textures.
@@ -571,10 +545,10 @@ class ExampleDeferred : public entry::AppI
{
Sphere lightPosRadius;
- float lightTime = time * m_lightAnimationSpeed * (bx::fsin(light/float(m_numLights) * bx::piHalf ) * 0.5f + 0.5f);
- lightPosRadius.m_center[0] = bx::fsin( ( (lightTime + light*0.47f) + bx::piHalf*1.37f ) )*offset;
- lightPosRadius.m_center[1] = bx::fcos( ( (lightTime + light*0.69f) + bx::piHalf*1.49f ) )*offset;
- lightPosRadius.m_center[2] = bx::fsin( ( (lightTime + light*0.37f) + bx::piHalf*1.57f ) )*2.0f;
+ float lightTime = time * m_lightAnimationSpeed * (bx::fsin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f);
+ lightPosRadius.m_center[0] = bx::fsin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
+ lightPosRadius.m_center[1] = bx::fcos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
+ lightPosRadius.m_center[2] = bx::fsin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
lightPosRadius.m_radius = 2.0f;
Aabb aabb;
@@ -660,7 +634,7 @@ class ExampleDeferred : public entry::AppI
*indices++ = 3;
*indices++ = 0;
- bgfx::setVertexBuffer(&tvb);
+ bgfx::setVertexBuffer(0, &tvb);
bgfx::setIndexBuffer(&tib);
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
@@ -723,7 +697,7 @@ class ExampleDeferred : public entry::AppI
);
bgfx::setTransform(mtx);
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh, 0, 6);
bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
bgfx::setState(BGFX_STATE_RGB_WRITE);
@@ -732,6 +706,8 @@ class ExampleDeferred : public entry::AppI
}
}
+ imguiEndFrame();
+
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
@@ -790,4 +766,6 @@ class ExampleDeferred : public entry::AppI
int64_t m_timeOffset;
};
-ENTRY_IMPLEMENT_MAIN(ExampleDeferred);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleDeferred, "21-deferred", "MRT rendering and deferred shading.");