diff options
author | 2017-01-02 14:05:50 +0100 | |
---|---|---|
committer | 2017-01-02 14:05:50 +0100 | |
commit | fadb547f630d377b1ff69a7ab09f852855930c56 (patch) | |
tree | a77a08bbed897113aec7a95657fc5e0d902fc0b6 /3rdparty/bgfx/examples/09-hdr/hdr.cpp | |
parent | 04faaaf7ffb422f390262e119fe50db21683a831 (diff) |
Updated BGFX (nw)
Diffstat (limited to '3rdparty/bgfx/examples/09-hdr/hdr.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/09-hdr/hdr.cpp | 180 |
1 files changed, 101 insertions, 79 deletions
diff --git a/3rdparty/bgfx/examples/09-hdr/hdr.cpp b/3rdparty/bgfx/examples/09-hdr/hdr.cpp index 3b6d34bca7c..ba940c88656 100644 --- a/3rdparty/bgfx/examples/09-hdr/hdr.cpp +++ b/3rdparty/bgfx/examples/09-hdr/hdr.cpp @@ -1,11 +1,12 @@ /* - * Copyright 2011-2016 Branimir Karadzic. All rights reserved. + * Copyright 2011-2017 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ #include "common.h" #include "bgfx_utils.h" #include "imgui/imgui.h" +#include <bx/rng.h> static float s_texelHalf = 0.0f; @@ -35,7 +36,7 @@ bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl; void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f) { - if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) ) + if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) ) { bgfx::TransientVertexBuffer vb; bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl); @@ -155,29 +156,9 @@ class ExampleHDR : public entry::AppI // Enable m_debug text. bgfx::setDebug(m_debug); - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff - , 1.0f - , 0 - ); - // Create vertex stream declaration. PosColorTexCoord0Vertex::init(); - // Set view m_debug names. - bgfx::setViewName(0, "Skybox"); - bgfx::setViewName(1, "Mesh"); - bgfx::setViewName(2, "Luminance"); - bgfx::setViewName(3, "Downscale luminance 0"); - bgfx::setViewName(4, "Downscale luminance 1"); - bgfx::setViewName(5, "Downscale luminance 2"); - bgfx::setViewName(6, "Downscale luminance 3"); - bgfx::setViewName(7, "Brightness"); - bgfx::setViewName(8, "Blur vertical"); - bgfx::setViewName(9, "Blur horizontal + tonemap"); - m_uffizi = loadTexture("textures/uffizi.dds" , 0 | BGFX_TEXTURE_U_CLAMP @@ -357,54 +338,92 @@ class ExampleHDR : public entry::AppI // 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 and frame buffers."); + 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 }; + 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]; + // Set views. - for (uint32_t ii = 0; ii < 6; ++ii) - { - bgfx::setViewRect(ii, 0, 0, bgfx::BackbufferRatio::Equal); - } - bgfx::setViewFrameBuffer(0, m_fbh); - bgfx::setViewFrameBuffer(1, m_fbh); - bgfx::setViewClear(1, BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL); + bgfx::setViewName(hdrSkybox, "Skybox"); + bgfx::setViewClear(hdrSkybox, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0); + bgfx::setViewRect(hdrSkybox, 0, 0, bgfx::BackbufferRatio::Equal); + bgfx::setViewFrameBuffer(hdrSkybox, m_fbh); - bgfx::setViewRect(2, 0, 0, 128, 128); - bgfx::setViewFrameBuffer(2, m_lum[0]); + bgfx::setViewName(hdrMesh, "Mesh"); + bgfx::setViewClear(hdrMesh, BGFX_CLEAR_DISCARD_DEPTH | BGFX_CLEAR_DISCARD_STENCIL); + bgfx::setViewRect(hdrMesh, 0, 0, bgfx::BackbufferRatio::Equal); + bgfx::setViewFrameBuffer(hdrMesh, m_fbh); - bgfx::setViewRect(3, 0, 0, 64, 64); - bgfx::setViewFrameBuffer(3, m_lum[1]); + bgfx::setViewName(hdrLuminance, "Luminance"); + bgfx::setViewRect(hdrLuminance, 0, 0, 128, 128); + bgfx::setViewFrameBuffer(hdrLuminance, m_lum[0]); - bgfx::setViewRect(4, 0, 0, 16, 16); - bgfx::setViewFrameBuffer(4, m_lum[2]); + bgfx::setViewName(hdrLumScale0, "Downscale luminance 0"); + bgfx::setViewRect(hdrLumScale0, 0, 0, 64, 64); + bgfx::setViewFrameBuffer(hdrLumScale0, m_lum[1]); - bgfx::setViewRect(5, 0, 0, 4, 4); - bgfx::setViewFrameBuffer(5, m_lum[3]); + bgfx::setViewName(hdrLumScale1, "Downscale luminance 1"); + bgfx::setViewRect(hdrLumScale1, 0, 0, 16, 16); + bgfx::setViewFrameBuffer(hdrLumScale1, m_lum[2]); - bgfx::setViewRect(6, 0, 0, 1, 1); - bgfx::setViewFrameBuffer(6, m_lum[4]); + bgfx::setViewName(hdrLumScale2, "Downscale luminance 2"); + bgfx::setViewRect(hdrLumScale2, 0, 0, 4, 4); + bgfx::setViewFrameBuffer(hdrLumScale2, m_lum[3]); - bgfx::setViewRect(7, 0, 0, bgfx::BackbufferRatio::Half); - bgfx::setViewFrameBuffer(7, m_bright); + bgfx::setViewName(hdrLumScale3, "Downscale luminance 3"); + bgfx::setViewRect(hdrLumScale3, 0, 0, 1, 1); + bgfx::setViewFrameBuffer(hdrLumScale3, m_lum[4]); - bgfx::setViewRect(8, 0, 0, bgfx::BackbufferRatio::Eighth); - bgfx::setViewFrameBuffer(8, m_blur); + bgfx::setViewName(hdrBrightness, "Brightness"); + bgfx::setViewRect(hdrBrightness, 0, 0, bgfx::BackbufferRatio::Half); + bgfx::setViewFrameBuffer(hdrBrightness, m_bright); - bgfx::setViewRect(9, 0, 0, bgfx::BackbufferRatio::Equal); + bgfx::setViewName(hdrVBlur, "Blur vertical"); + bgfx::setViewRect(hdrVBlur, 0, 0, bgfx::BackbufferRatio::Eighth); + bgfx::setViewFrameBuffer(hdrVBlur, m_blur); - float view[16]; - float proj[16]; + bgfx::setViewName(hdrHBlurTonemap, "Blur horizontal + tonemap"); + bgfx::setViewRect(hdrHBlurTonemap, 0, 0, bgfx::BackbufferRatio::Equal); + bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE; + bgfx::setViewFrameBuffer(hdrHBlurTonemap, invalid); - bx::mtxIdentity(view); + float proj[16]; bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f); + uint8_t order[] = + { + hdrSkybox, + hdrMesh, + hdrLuminance, + hdrLumScale0, + hdrLumScale1, + hdrLumScale2, + hdrLumScale3, + hdrBrightness, + hdrVBlur, + hdrHBlurTonemap + }; + bgfx::setViewOrder(0, BX_COUNTOF(order), order); + // Set view and projection matrix for view 0. - for (uint32_t ii = 0; ii < 10; ++ii) + for (uint32_t ii = 0; ii < BX_COUNTOF(order); ++ii) { - bgfx::setViewTransform(ii, view, proj); + bgfx::setViewTransform(ii, NULL, proj); } - float at[3] = { 0.0f, 1.0f, 0.0f }; + float at[3] = { 0.0f, 1.0f, 0.0f }; float eye[3] = { 0.0f, 1.0f, -2.5f }; float mtx[16]; @@ -416,88 +435,89 @@ class ExampleHDR : public entry::AppI float temp[4]; bx::vec3MulMtx(temp, eye, mtx); + float view[16]; bx::mtxLookAt(view, temp, at); bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f); - // Set view and projection matrix for view 1. - bgfx::setViewTransform(1, view, proj); + // Set view and projection matrix for view hdrMesh. + bgfx::setViewTransform(hdrMesh, view, proj); - bgfx::setUniform(u_mtx, mtx); + float tonemap[4] = { m_middleGray, square(m_white), m_threshold, m_time }; - // Render skybox into view 0. + // Render skybox into view hdrSkybox. bgfx::setTexture(0, s_texCube, m_uffizi); - bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); + bgfx::setUniform(u_mtx, mtx); screenSpaceQuad( (float)m_width, (float)m_height, true); - bgfx::submit(0, m_skyProgram); + bgfx::submit(hdrSkybox, m_skyProgram); - // Render m_mesh into view 1 + // Render m_mesh into view hdrMesh. bgfx::setTexture(0, s_texCube, m_uffizi); - meshSubmit(m_mesh, 1, m_meshProgram, NULL); + bgfx::setUniform(u_tonemap, tonemap); + meshSubmit(m_mesh, hdrMesh, m_meshProgram, NULL); // Calculate luminance. setOffsets2x2Lum(u_offset, 128, 128); bgfx::setTexture(0, s_texColor, m_fbtextures[0]); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad(128.0f, 128.0f, m_caps->originBottomLeft); - bgfx::submit(2, m_lumProgram); + bgfx::submit(hdrLuminance, m_lumProgram); // Downscale luminance 0. setOffsets4x4Lum(u_offset, 128, 128); - bgfx::setTexture(0, s_texColor, m_lum[0]); + bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[0]) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad(64.0f, 64.0f, m_caps->originBottomLeft); - bgfx::submit(3, m_lumAvgProgram); + bgfx::submit(hdrLumScale0, m_lumAvgProgram); // Downscale luminance 1. setOffsets4x4Lum(u_offset, 64, 64); - bgfx::setTexture(0, s_texColor, m_lum[1]); + bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[1]) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad(16.0f, 16.0f, m_caps->originBottomLeft); - bgfx::submit(4, m_lumAvgProgram); + bgfx::submit(hdrLumScale1, m_lumAvgProgram); // Downscale luminance 2. setOffsets4x4Lum(u_offset, 16, 16); - bgfx::setTexture(0, s_texColor, m_lum[2]); + bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[2]) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad(4.0f, 4.0f, m_caps->originBottomLeft); - bgfx::submit(5, m_lumAvgProgram); + bgfx::submit(hdrLumScale2, m_lumAvgProgram); // Downscale luminance 3. setOffsets4x4Lum(u_offset, 4, 4); - bgfx::setTexture(0, s_texColor, m_lum[3]); + bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[3]) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad(1.0f, 1.0f, m_caps->originBottomLeft); - bgfx::submit(6, m_lumAvgProgram); - - float tonemap[4] = { m_middleGray, square(m_white), m_threshold, m_time }; - bgfx::setUniform(u_tonemap, tonemap); + bgfx::submit(hdrLumScale3, m_lumAvgProgram); // m_bright pass m_threshold is tonemap[3]. setOffsets4x4Lum(u_offset, m_width/2, m_height/2); bgfx::setTexture(0, s_texColor, m_fbtextures[0]); - bgfx::setTexture(1, s_texLum, m_lum[4]); + bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); + bgfx::setUniform(u_tonemap, tonemap); screenSpaceQuad( (float)m_width/2.0f, (float)m_height/2.0f, m_caps->originBottomLeft); - bgfx::submit(7, m_brightProgram); + bgfx::submit(hdrBrightness, m_brightProgram); // m_blur m_bright pass vertically. - bgfx::setTexture(0, s_texColor, m_bright); + bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_bright) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); + bgfx::setUniform(u_tonemap, tonemap); screenSpaceQuad( (float)m_width/8.0f, (float)m_height/8.0f, m_caps->originBottomLeft); - bgfx::submit(8, m_blurProgram); + bgfx::submit(hdrVBlur, m_blurProgram); // m_blur m_bright pass horizontally, do tonemaping and combine. bgfx::setTexture(0, s_texColor, m_fbtextures[0]); - bgfx::setTexture(1, s_texLum, m_lum[4]); - bgfx::setTexture(2, s_texBlur, m_blur); + bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) ); + bgfx::setTexture(2, s_texBlur, bgfx::getTexture(m_blur) ); bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE); screenSpaceQuad( (float)m_width, (float)m_height, m_caps->originBottomLeft); - bgfx::submit(9, m_tonemapProgram); + bgfx::submit(hdrHBlurTonemap, m_tonemapProgram); if (bgfx::isValid(m_rb) ) { - bgfx::blit(9, m_rb, 0, 0, m_lum[4]); + bgfx::blit(hdrHBlurTonemap, m_rb, 0, 0, bgfx::getTexture(m_lum[4]) ); bgfx::readTexture(m_rb, &m_lumBgra8); } @@ -539,6 +559,8 @@ class ExampleHDR : public entry::AppI bgfx::FrameBufferHandle m_bright; bgfx::FrameBufferHandle m_blur; + bx::RngMwc m_rng; + uint32_t m_width; uint32_t m_height; uint32_t m_debug; |