diff options
Diffstat (limited to '3rdparty/bgfx/examples/19-oit/oit.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/19-oit/oit.cpp | 570 |
1 files changed, 309 insertions, 261 deletions
diff --git a/3rdparty/bgfx/examples/19-oit/oit.cpp b/3rdparty/bgfx/examples/19-oit/oit.cpp index 66baed03cd1..b9bbd041934 100644 --- a/3rdparty/bgfx/examples/19-oit/oit.cpp +++ b/3rdparty/bgfx/examples/19-oit/oit.cpp @@ -146,322 +146,370 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBott } } -int _main_(int _argc, char** _argv) +class ExampleOIT : public entry::AppI { - Args args(_argc, _argv); + void init(int _argc, char** _argv) BX_OVERRIDE + { + Args args(_argc, _argv); - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; - bgfx::init(args.m_type, args.m_pciId); - bgfx::reset(width, height, reset); + bgfx::init(args.m_type, args.m_pciId); + bgfx::reset(m_width, m_height, m_reset); - // Create vertex stream declaration. - PosColorVertex::init(); - PosColorTexCoord0Vertex::init(); + // Enable debug text. + bgfx::setDebug(m_debug); - // Enable debug text. - bgfx::setDebug(debug); + // Create vertex stream declaration. + PosColorVertex::init(); + PosColorTexCoord0Vertex::init(); - // Get renderer capabilities info. - const bgfx::Caps* caps = bgfx::getCaps(); + // Get renderer capabilities info. + const bgfx::Caps* caps = bgfx::getCaps(); - // Setup root path for binary shaders. Shader binaries are different - // for each renderer. - switch (caps->rendererType) - { - default: - break; + // Setup root path for binary shaders. Shader binaries are different + // for each renderer. + switch (caps->rendererType) + { + default: + break; - case bgfx::RendererType::OpenGL: - case bgfx::RendererType::OpenGLES: - s_flipV = true; - break; - } + case bgfx::RendererType::OpenGL: + case bgfx::RendererType::OpenGLES: + s_flipV = true; + break; + } - // Imgui. - imguiCreate(); + // Imgui. + imguiCreate(); - const bgfx::Memory* mem; + // Create static vertex buffer. + m_vbh = bgfx::createVertexBuffer( + bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ) + , PosColorVertex::ms_decl + ); - // Create static vertex buffer. - mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ); - bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl); + // Create static index buffer. + m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) ); - // Create static index buffer. - mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ); - bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem); + // Create texture sampler uniforms. + s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Int1); + s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Int1); + u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); - // Create texture sampler uniforms. - bgfx::UniformHandle s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Int1); - bgfx::UniformHandle s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Int1); - bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); + m_blend = loadProgram("vs_oit", "fs_oit" ); + m_wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" ); + m_wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" ); + m_wbPass = loadProgram("vs_oit", "fs_oit_wb" ); + m_wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" ); - bgfx::ProgramHandle blend = loadProgram("vs_oit", "fs_oit" ); - bgfx::ProgramHandle wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" ); - bgfx::ProgramHandle wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" ); - bgfx::ProgramHandle wbPass = loadProgram("vs_oit", "fs_oit_wb" ); - bgfx::ProgramHandle wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" ); + m_fbtextures[0].idx = bgfx::invalidHandle; + m_fbtextures[1].idx = bgfx::invalidHandle; + m_fbh.idx = bgfx::invalidHandle; - bgfx::TextureHandle fbtextures[2] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE }; - bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE; + m_mode = 1; + m_scrollArea = 0; + m_frontToBack = true; + m_fadeInOut = false; - int64_t timeOffset = bx::getHPCounter(); + m_oldWidth = 0; + m_oldHeight = 0; + m_oldReset = m_reset; - uint32_t mode = 1; - int32_t scrollArea = 0; - bool frontToBack = true; - bool fadeInOut = false; + m_timeOffset = bx::getHPCounter(); + } - uint32_t oldWidth = 0; - uint32_t oldHeight = 0; - uint32_t oldReset = reset; + int shutdown() BX_OVERRIDE + { + // Cleanup. + imguiDestroy(); + + bgfx::destroyFrameBuffer(m_fbh); + bgfx::destroyIndexBuffer(m_ibh); + bgfx::destroyVertexBuffer(m_vbh); + bgfx::destroyProgram(m_blend); + bgfx::destroyProgram(m_wbSeparatePass); + bgfx::destroyProgram(m_wbSeparateBlit); + bgfx::destroyProgram(m_wbPass); + bgfx::destroyProgram(m_wbBlit); + bgfx::destroyUniform(s_texColor0); + bgfx::destroyUniform(s_texColor1); + bgfx::destroyUniform(u_color); + + // Shutdown bgfx. + bgfx::shutdown(); + + return 0; + } - entry::MouseState mouseState; - while (!entry::processEvents(width, height, debug, reset, &mouseState) ) + bool update() BX_OVERRIDE { - if (oldWidth != width - || oldHeight != height - || oldReset != reset - || !bgfx::isValid(fbh) ) + if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) { - // Recreate variable size render targets when resolution changes. - oldWidth = width; - oldHeight = height; - oldReset = reset; - - if (bgfx::isValid(fbh) ) + if (m_oldWidth != m_width + || m_oldHeight != m_height + || m_oldReset != m_reset + || !bgfx::isValid(m_fbh) ) { - bgfx::destroyFrameBuffer(fbh); - } + // Recreate variable size render targets when resolution changes. + m_oldWidth = m_width; + m_oldHeight = m_height; + m_oldReset = m_reset; - fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT); - fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT); - fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); - } + if (bgfx::isValid(m_fbh) ) + { + bgfx::destroyFrameBuffer(m_fbh); + } - imguiBeginFrame(mouseState.m_mx - , mouseState.m_my - , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0) - | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0) - | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0) - , mouseState.m_mz - , width - , height - ); + m_fbtextures[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT); + m_fbtextures[1] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT); + m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true); + } - imguiBeginScrollArea("Settings", width - width / 4 - 10, 10, width / 4, height / 3, &scrollArea); - imguiSeparatorLine(); + 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 + , m_width + , m_height + ); - imguiLabel("Blend mode:"); + imguiBeginScrollArea("Settings", m_width - m_width / 4 - 10, 10, m_width / 4, m_height / 3, &m_scrollArea); + imguiSeparatorLine(); - mode = imguiChoose(mode - , "None" - , "Separate" - , "MRT Independent" - ); + imguiLabel("Blend mode:"); - imguiSeparatorLine(); + m_mode = imguiChoose(m_mode + , "None" + , "Separate" + , "MRT Independent" + ); - if (imguiCheck("Front to back", frontToBack) ) - { - frontToBack ^= true; - } + imguiSeparatorLine(); - if (imguiCheck("Fade in/out", fadeInOut) ) - { - fadeInOut ^= true; - } + if (imguiCheck("Front to back", m_frontToBack) ) + { + m_frontToBack ^= true; + } - imguiEndScrollArea(); - imguiEndFrame(); + if (imguiCheck("Fade in/out", m_fadeInOut) ) + { + m_fadeInOut ^= true; + } - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - bgfx::setViewRect(1, 0, 0, width, height); + imguiEndScrollArea(); + imguiEndFrame(); - 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; + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); + bgfx::setViewRect(1, 0, 0, m_width, m_height); - float time = (float)( (now-timeOffset)/freq); + 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; - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - // Reference: - // Weighted, Blended Order-Independent Transparency - // http://jcgt.org/published/0002/02/09/ - // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency."); - bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + float time = (float)( (now-m_timeOffset)/freq); - float at[3] = { 0.0f, 0.0f, 0.0f }; - float eye[3] = { 0.0f, 0.0f, -7.0f }; + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + // Reference: + // Weighted, Blended Order-Independent Transparency + // http://jcgt.org/published/0002/02/09/ + // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); - float view[16]; - float proj[16]; + float at[3] = { 0.0f, 0.0f, 0.0f }; + float eye[3] = { 0.0f, 0.0f, -7.0f }; - // Set view and projection matrix for view 0. - bx::mtxLookAt(view, eye, at); - mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f); + float view[16]; + float proj[16]; - bgfx::setViewTransform(0, view, proj); + // Set view and projection matrix for view 0. + bx::mtxLookAt(view, eye, at); + mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f); - // Set palette color for index 0 - bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f); + bgfx::setViewTransform(0, view, proj); - // Set palette color for index 1 - bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f); + // Set palette color for index 0 + bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f); - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 1.0f // Depth - , 0 // Stencil - , 0 // FB texture 0, color palette 0 - , 1 == mode ? 1 : 0 // FB texture 1, color palette 1 - ); + // Set palette color for index 1 + bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f); - bgfx::setViewClear(1 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 1.0f // Depth - , 0 // Stencil - , 0 // Color palette 0 - ); + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 1.0f // Depth + , 0 // Stencil + , 0 // FB texture 0, color palette 0 + , 1 == m_mode ? 1 : 0 // FB texture 1, color palette 1 + ); - bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE; - bgfx::setViewFrameBuffer(0, 0 == mode ? invalid : fbh); + bgfx::setViewClear(1 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 1.0f // Depth + , 0 // Stencil + , 0 // Color palette 0 + ); - // Set view and projection matrix for view 1. - bx::mtxIdentity(view); - bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f); - bgfx::setViewTransform(1, view, proj); + bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE; + bgfx::setViewFrameBuffer(0, 0 == m_mode ? invalid : m_fbh); - for (uint32_t depth = 0; depth < 3; ++depth) - { - uint32_t zz = frontToBack ? 2-depth : depth; + // Set view and projection matrix for view 1. + bx::mtxIdentity(view); + bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f); + bgfx::setViewTransform(1, view, proj); - for (uint32_t yy = 0; yy < 3; ++yy) + for (uint32_t depth = 0; depth < 3; ++depth) { - for (uint32_t xx = 0; xx < 3; ++xx) - { - float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f }; - - if (fadeInOut - && zz == 1) - { - color[3] = sinf(time*3.0f)*0.49f+0.5f; - } + uint32_t zz = m_frontToBack ? 2-depth : depth; - bgfx::setUniform(u_color, color); - - BX_UNUSED(time); - float mtx[16]; - bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f); - //mtxIdentity(mtx); - mtx[12] = -2.5f + float(xx)*2.5f; - mtx[13] = -2.5f + float(yy)*2.5f; - mtx[14] = -2.5f + float(zz)*2.5f; - - // Set transform for draw call. - bgfx::setTransform(mtx); - - // Set vertex and index buffer. - bgfx::setVertexBuffer(vbh); - bgfx::setIndexBuffer(ibh); - - const uint64_t state = 0 - | BGFX_STATE_CULL_CW - | BGFX_STATE_RGB_WRITE - | BGFX_STATE_ALPHA_WRITE - | BGFX_STATE_DEPTH_TEST_LESS - | BGFX_STATE_MSAA - ; - - bgfx::ProgramHandle program = BGFX_INVALID_HANDLE; - switch (mode) + for (uint32_t yy = 0; yy < 3; ++yy) + { + for (uint32_t xx = 0; xx < 3; ++xx) { - case 0: - // Set vertex and fragment shaders. - program = blend; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_ALPHA - ); - break; - - case 1: - // Set vertex and fragment shaders. - program = wbSeparatePass; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA) - ); - break; - - default: - // Set vertex and fragment shaders. - program = wbPass; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE) - | BGFX_STATE_BLEND_INDEPENDENT - , 0 - | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR) - ); - break; + float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f }; + + if (m_fadeInOut + && zz == 1) + { + color[3] = sinf(time*3.0f)*0.49f+0.5f; + } + + bgfx::setUniform(u_color, color); + + BX_UNUSED(time); + float mtx[16]; + bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f); + //mtxIdentity(mtx); + mtx[12] = -2.5f + float(xx)*2.5f; + mtx[13] = -2.5f + float(yy)*2.5f; + mtx[14] = -2.5f + float(zz)*2.5f; + + // Set transform for draw call. + bgfx::setTransform(mtx); + + // Set vertex and index buffer. + bgfx::setVertexBuffer(m_vbh); + bgfx::setIndexBuffer(m_ibh); + + const uint64_t state = 0 + | BGFX_STATE_CULL_CW + | BGFX_STATE_RGB_WRITE + | BGFX_STATE_ALPHA_WRITE + | BGFX_STATE_DEPTH_TEST_LESS + | BGFX_STATE_MSAA + ; + + bgfx::ProgramHandle program = BGFX_INVALID_HANDLE; + switch (m_mode) + { + case 0: + // Set vertex and fragment shaders. + program = m_blend; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_ALPHA + ); + break; + + case 1: + // Set vertex and fragment shaders. + program = m_wbSeparatePass; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA) + ); + break; + + default: + // Set vertex and fragment shaders. + program = m_wbPass; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE) + | BGFX_STATE_BLEND_INDEPENDENT + , 0 + | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR) + ); + break; + } + + // Submit primitive for rendering to view 0. + bgfx::submit(0, program); } - - // Submit primitive for rendering to view 0. - bgfx::submit(0, program); } } - } - if (0 != mode) - { - bgfx::setTexture(0, s_texColor0, fbtextures[0]); - bgfx::setTexture(1, s_texColor1, fbtextures[1]); - bgfx::setState(0 - | BGFX_STATE_RGB_WRITE - | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA) - ); - screenSpaceQuad( (float)width, (float)height, s_flipV); - bgfx::submit(1 - , 1 == mode ? wbSeparateBlit : wbBlit - ); + if (0 != m_mode) + { + bgfx::setTexture(0, s_texColor0, m_fbtextures[0]); + bgfx::setTexture(1, s_texColor1, m_fbtextures[1]); + bgfx::setState(0 + | BGFX_STATE_RGB_WRITE + | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA) + ); + screenSpaceQuad( (float)m_width, (float)m_height, s_flipV); + bgfx::submit(1 + , 1 == m_mode ? m_wbSeparateBlit : m_wbBlit + ); + } + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + + return true; } - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); + return false; } - // Cleanup. - imguiDestroy(); - - bgfx::destroyFrameBuffer(fbh); - bgfx::destroyIndexBuffer(ibh); - bgfx::destroyVertexBuffer(vbh); - bgfx::destroyProgram(blend); - bgfx::destroyProgram(wbSeparatePass); - bgfx::destroyProgram(wbSeparateBlit); - bgfx::destroyProgram(wbPass); - bgfx::destroyProgram(wbBlit); - bgfx::destroyUniform(s_texColor0); - bgfx::destroyUniform(s_texColor1); - bgfx::destroyUniform(u_color); - - // Shutdown bgfx. - bgfx::shutdown(); - - return 0; -} + uint32_t m_width; + uint32_t m_height; + uint32_t m_debug; + uint32_t m_reset; + + uint32_t m_mode; + int32_t m_scrollArea; + bool m_frontToBack; + bool m_fadeInOut; + + uint32_t m_oldWidth; + uint32_t m_oldHeight; + uint32_t m_oldReset; + + entry::MouseState m_mouseState; + + int64_t m_timeOffset; + + bgfx::VertexBufferHandle m_vbh; + bgfx::IndexBufferHandle m_ibh; + + bgfx::UniformHandle s_texColor0; + bgfx::UniformHandle s_texColor1; + bgfx::UniformHandle u_color; + + bgfx::ProgramHandle m_blend; + bgfx::ProgramHandle m_wbSeparatePass; + bgfx::ProgramHandle m_wbSeparateBlit; + bgfx::ProgramHandle m_wbPass; + bgfx::ProgramHandle m_wbBlit; + + bgfx::TextureHandle m_fbtextures[2]; + bgfx::FrameBufferHandle m_fbh; +}; + +ENTRY_IMPLEMENT_MAIN(ExampleOIT); |