summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/09-hdr/hdr.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/09-hdr/hdr.cpp')
-rw-r--r--3rdparty/bgfx/examples/09-hdr/hdr.cpp186
1 files changed, 113 insertions, 73 deletions
diff --git a/3rdparty/bgfx/examples/09-hdr/hdr.cpp b/3rdparty/bgfx/examples/09-hdr/hdr.cpp
index ff9c8df6477..500acf0560a 100644
--- a/3rdparty/bgfx/examples/09-hdr/hdr.cpp
+++ b/3rdparty/bgfx/examples/09-hdr/hdr.cpp
@@ -8,6 +8,9 @@
#include "imgui/imgui.h"
#include <bx/rng.h>
+namespace
+{
+
static float s_texelHalf = 0.0f;
struct PosColorTexCoord0Vertex
@@ -88,7 +91,7 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBott
vertex[2].m_u = maxu;
vertex[2].m_v = maxv;
- bgfx::setVertexBuffer(&vb);
+ bgfx::setVertexBuffer(0, &vb);
}
}
@@ -134,20 +137,21 @@ void setOffsets4x4Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _he
bgfx::setUniform(_handle, offsets, num);
}
-inline float square(float _x)
-{
- return _x*_x;
-}
-
class ExampleHDR : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleHDR(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);
@@ -184,9 +188,7 @@ class ExampleHDR : public entry::AppI
m_mesh = meshLoad("meshes/bunny.bin");
- m_fbtextures[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP);
- m_fbtextures[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY);
- m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
+ m_fbh.idx = bgfx::kInvalidHandle;
m_lum[0] = bgfx::createFrameBuffer(128, 128, bgfx::TextureFormat::BGRA8);
m_lum[1] = bgfx::createFrameBuffer( 64, 64, bgfx::TextureFormat::BGRA8);
@@ -204,7 +206,7 @@ class ExampleHDR : public entry::AppI
}
else
{
- m_rb.idx = bgfx::invalidHandle;
+ m_rb.idx = bgfx::kInvalidHandle;
}
// Imgui.
@@ -227,7 +229,7 @@ class ExampleHDR : public entry::AppI
m_time = 0.0f;
}
- virtual int shutdown() BX_OVERRIDE
+ virtual int shutdown() override
{
// Cleanup.
imguiDestroy();
@@ -236,32 +238,36 @@ class ExampleHDR : public entry::AppI
for (uint32_t ii = 0; ii < BX_COUNTOF(m_lum); ++ii)
{
- bgfx::destroyFrameBuffer(m_lum[ii]);
+ bgfx::destroy(m_lum[ii]);
+ }
+ bgfx::destroy(m_bright);
+ bgfx::destroy(m_blur);
+
+ if (bgfx::isValid(m_fbh) )
+ {
+ bgfx::destroy(m_fbh);
}
- bgfx::destroyFrameBuffer(m_bright);
- bgfx::destroyFrameBuffer(m_blur);
- bgfx::destroyFrameBuffer(m_fbh);
-
- bgfx::destroyProgram(m_meshProgram);
- bgfx::destroyProgram(m_skyProgram);
- bgfx::destroyProgram(m_tonemapProgram);
- bgfx::destroyProgram(m_lumProgram);
- bgfx::destroyProgram(m_lumAvgProgram);
- bgfx::destroyProgram(m_blurProgram);
- bgfx::destroyProgram(m_brightProgram);
- bgfx::destroyTexture(m_uffizi);
+
+ bgfx::destroy(m_meshProgram);
+ bgfx::destroy(m_skyProgram);
+ bgfx::destroy(m_tonemapProgram);
+ bgfx::destroy(m_lumProgram);
+ bgfx::destroy(m_lumAvgProgram);
+ bgfx::destroy(m_blurProgram);
+ bgfx::destroy(m_brightProgram);
+ bgfx::destroy(m_uffizi);
if (bgfx::isValid(m_rb) )
{
- bgfx::destroyTexture(m_rb);
+ bgfx::destroy(m_rb);
}
- bgfx::destroyUniform(s_texCube);
- bgfx::destroyUniform(s_texColor);
- bgfx::destroyUniform(s_texLum);
- bgfx::destroyUniform(s_texBlur);
- bgfx::destroyUniform(u_mtx);
- bgfx::destroyUniform(u_tonemap);
- bgfx::destroyUniform(u_offset);
+ bgfx::destroy(s_texCube);
+ bgfx::destroy(s_texColor);
+ bgfx::destroy(s_texLum);
+ bgfx::destroy(s_texBlur);
+ bgfx::destroy(u_mtx);
+ bgfx::destroy(u_tonemap);
+ bgfx::destroy(u_offset);
// Shutdown bgfx.
bgfx::shutdown();
@@ -269,11 +275,12 @@ class ExampleHDR : 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) )
{
- if (m_oldWidth != m_width
+ if (!bgfx::isValid(m_fbh)
+ || m_oldWidth != m_width
|| m_oldHeight != m_height
|| m_oldReset != m_reset)
{
@@ -284,10 +291,37 @@ class ExampleHDR : public entry::AppI
uint32_t msaa = (m_reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
- bgfx::destroyFrameBuffer(m_fbh);
+ if (bgfx::isValid(m_fbh) )
+ {
+ bgfx::destroy(m_fbh);
+ }
+
+ m_fbtextures[0] = bgfx::createTexture2D(
+ uint16_t(m_width)
+ , uint16_t(m_height)
+ , false
+ , 1
+ , bgfx::TextureFormat::BGRA8
+ , ((msaa + 1) << BGFX_TEXTURE_RT_MSAA_SHIFT) | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP
+ );
+
+ const uint32_t textureFlags = BGFX_TEXTURE_RT_WRITE_ONLY|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT);
+
+ bgfx::TextureFormat::Enum depthFormat =
+ bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::D16, textureFlags) ? bgfx::TextureFormat::D16
+ : bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::D24S8, textureFlags) ? bgfx::TextureFormat::D24S8
+ : bgfx::TextureFormat::D32
+ ;
+
+ m_fbtextures[1] = bgfx::createTexture2D(
+ uint16_t(m_width)
+ , uint16_t(m_height)
+ , false
+ , 1
+ , depthFormat
+ , textureFlags
+ );
- m_fbtextures[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, ((msaa + 1) << BGFX_TEXTURE_RT_MSAA_SHIFT) | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP);
- m_fbtextures[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT) );
m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
}
@@ -301,25 +335,35 @@ class ExampleHDR : public entry::AppI
, uint16_t(m_height)
);
- imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 2, &m_scrollArea);
- imguiSeparatorLine();
+ showExampleDialog(this);
- imguiSlider("Speed", m_speed, 0.0f, 1.0f, 0.01f);
- imguiSeparator();
+ 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 / 2.0f)
+ , ImGuiWindowFlags_AlwaysAutoResize
+ );
+
+ ImGui::SliderFloat("Speed", &m_speed, 0.0f, 1.0f);
+ ImGui::Separator();
- imguiSlider("Middle gray", m_middleGray, 0.1f, 1.0f, 0.01f);
- imguiSlider("White point", m_white, 0.1f, 2.0f, 0.01f);
- imguiSlider("Threshold", m_threshold, 0.1f, 2.0f, 0.01f);
+ ImGui::SliderFloat("Middle gray", &m_middleGray, 0.1f, 1.0f);
+ ImGui::SliderFloat("White point", &m_white, 0.1f, 2.0f);
+ ImGui::SliderFloat("Threshold", &m_threshold, 0.1f, 2.0f);
if (bgfx::isValid(m_rb) )
{
union { uint32_t color; uint8_t bgra[4]; } cast = { m_lumBgra8 };
float exponent = cast.bgra[3]/255.0f * 255.0f - 128.0f;
float lumAvg = cast.bgra[2]/255.0f * bx::fexp2(exponent);
- imguiSlider("Lum Avg", lumAvg, 0.0f, 1.0f, 0.01f, false);
+ ImGui::SliderFloat("Lum Avg", &lumAvg, 0.0f, 1.0f);
}
- imguiEndScrollArea();
+ ImGui::End();
+
imguiEndFrame();
// This dummy draw call is here to make sure that view 0 is cleared
@@ -331,29 +375,22 @@ class ExampleHDR : public entry::AppI
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
- const double toMs = 1000.0/freq;
m_time += (float)(frameTime*m_speed/freq);
- // Use m_debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/09-hdr");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Using multiple views with frame buffers, and view order remapping.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
-
- uint8_t shuffle[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ bgfx::ViewId shuffle[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
bx::shuffle(&m_rng, shuffle, BX_COUNTOF(shuffle) );
- uint8_t hdrSkybox = shuffle[0];
- uint8_t hdrMesh = shuffle[1];
- uint8_t hdrLuminance = shuffle[2];
- uint8_t hdrLumScale0 = shuffle[3];
- uint8_t hdrLumScale1 = shuffle[4];
- uint8_t hdrLumScale2 = shuffle[5];
- uint8_t hdrLumScale3 = shuffle[6];
- uint8_t hdrBrightness = shuffle[7];
- uint8_t hdrVBlur = shuffle[8];
- uint8_t hdrHBlurTonemap = shuffle[9];
+ bgfx::ViewId hdrSkybox = shuffle[0];
+ bgfx::ViewId hdrMesh = shuffle[1];
+ bgfx::ViewId hdrLuminance = shuffle[2];
+ bgfx::ViewId hdrLumScale0 = shuffle[3];
+ bgfx::ViewId hdrLumScale1 = shuffle[4];
+ bgfx::ViewId hdrLumScale2 = shuffle[5];
+ bgfx::ViewId hdrLumScale3 = shuffle[6];
+ bgfx::ViewId hdrBrightness = shuffle[7];
+ bgfx::ViewId hdrVBlur = shuffle[8];
+ bgfx::ViewId hdrHBlurTonemap = shuffle[9];
// Set views.
bgfx::setViewName(hdrSkybox, "Skybox");
@@ -399,10 +436,11 @@ class ExampleHDR : public entry::AppI
bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
bgfx::setViewFrameBuffer(hdrHBlurTonemap, invalid);
+ const bgfx::Caps* caps = bgfx::getCaps();
float proj[16];
- bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
+ bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
- uint8_t order[] =
+ bgfx::ViewId order[] =
{
hdrSkybox,
hdrMesh,
@@ -437,12 +475,12 @@ class ExampleHDR : public entry::AppI
float view[16];
bx::mtxLookAt(view, temp, at);
- bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
+ bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
// Set view and projection matrix for view hdrMesh.
bgfx::setViewTransform(hdrMesh, view, proj);
- float tonemap[4] = { m_middleGray, square(m_white), m_threshold, m_time };
+ float tonemap[4] = { m_middleGray, bx::fsq(m_white), m_threshold, m_time };
// Render skybox into view hdrSkybox.
bgfx::setTexture(0, s_texCube, m_uffizi);
@@ -582,4 +620,6 @@ class ExampleHDR : public entry::AppI
float m_time;
};
-ENTRY_IMPLEMENT_MAIN(ExampleHDR);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleHDR, "09-hdr", "Using multiple views with frame buffers, and view order remapping.");