From f149fcec944195efe3d1dd568271c16248cfc50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 21 Aug 2015 21:06:46 -0700 Subject: [PATCH] Fixed 09-hdr for Emscripten. --- examples/09-hdr/hdr.cpp | 672 ++++++++++++++------------ examples/17-drawstress/drawstress.cpp | 17 +- 2 files changed, 370 insertions(+), 319 deletions(-) diff --git a/examples/09-hdr/hdr.cpp b/examples/09-hdr/hdr.cpp index e9f07b2b..c009da80 100644 --- a/examples/09-hdr/hdr.cpp +++ b/examples/09-hdr/hdr.cpp @@ -139,341 +139,393 @@ inline float square(float _x) return _x*_x; } -int _main_(int /*_argc*/, char** /*_argv*/) +class HDR : public entry::AppI { - PosColorTexCoord0Vertex::init(); - - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; - - bgfx::init(); - bgfx::reset(width, height, reset); - - // Enable debug text. - bgfx::setDebug(debug); - - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff - , 1.0f - , 0 - ); - - // Set view 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"); - - bgfx::TextureHandle uffizi = loadTexture("uffizi.dds" - , 0 - | BGFX_TEXTURE_U_CLAMP - | BGFX_TEXTURE_V_CLAMP - | BGFX_TEXTURE_W_CLAMP - ); - - bgfx::ProgramHandle skyProgram = loadProgram("vs_hdr_skybox", "fs_hdr_skybox"); - bgfx::ProgramHandle lumProgram = loadProgram("vs_hdr_lum", "fs_hdr_lum"); - bgfx::ProgramHandle lumAvgProgram = loadProgram("vs_hdr_lumavg", "fs_hdr_lumavg"); - bgfx::ProgramHandle blurProgram = loadProgram("vs_hdr_blur", "fs_hdr_blur"); - bgfx::ProgramHandle brightProgram = loadProgram("vs_hdr_bright", "fs_hdr_bright"); - bgfx::ProgramHandle meshProgram = loadProgram("vs_hdr_mesh", "fs_hdr_mesh"); - bgfx::ProgramHandle tonemapProgram = loadProgram("vs_hdr_tonemap", "fs_hdr_tonemap"); - - bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1); - bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); - bgfx::UniformHandle s_texLum = bgfx::createUniform("s_texLum", bgfx::UniformType::Int1); - bgfx::UniformHandle s_texBlur = bgfx::createUniform("s_texBlur", bgfx::UniformType::Int1); - bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4); - bgfx::UniformHandle u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformType::Vec4); - bgfx::UniformHandle u_offset = bgfx::createUniform("u_offset", bgfx::UniformType::Vec4, 16); - - Mesh* mesh = meshLoad("meshes/bunny.bin"); - - bgfx::FrameBufferHandle fbh; - bgfx::TextureHandle fbtextures[] = + void init(int /*_argc*/, char** /*_argv*/) BX_OVERRIDE { - bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT|BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP), - bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY), - }; - fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; - bgfx::FrameBufferHandle lum[5]; - lum[0] = bgfx::createFrameBuffer(128, 128, bgfx::TextureFormat::BGRA8); - lum[1] = bgfx::createFrameBuffer( 64, 64, bgfx::TextureFormat::BGRA8); - lum[2] = bgfx::createFrameBuffer( 16, 16, bgfx::TextureFormat::BGRA8); - lum[3] = bgfx::createFrameBuffer( 4, 4, bgfx::TextureFormat::BGRA8); - lum[4] = bgfx::createFrameBuffer( 1, 1, bgfx::TextureFormat::BGRA8); + bgfx::init(); + bgfx::reset(m_width, m_height, m_reset); - bgfx::FrameBufferHandle bright; - bright = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Half, bgfx::TextureFormat::BGRA8); + // Enable m_debug text. + bgfx::setDebug(m_debug); - bgfx::FrameBufferHandle blur; - blur = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Eighth, bgfx::TextureFormat::BGRA8); + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 0x303030ff + , 1.0f + , 0 + ); - // Imgui. - imguiCreate(); + // Create vertex stream declaration. + PosColorTexCoord0Vertex::init(); - const bgfx::RendererType::Enum renderer = bgfx::getRendererType(); - s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f; - s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer; + // 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"); - uint32_t oldWidth = 0; - uint32_t oldHeight = 0; - uint32_t oldReset = reset; + m_uffizi = loadTexture("uffizi.dds" + , 0 + | BGFX_TEXTURE_U_CLAMP + | BGFX_TEXTURE_V_CLAMP + | BGFX_TEXTURE_W_CLAMP + ); - float speed = 0.37f; - float middleGray = 0.18f; - float white = 1.1f; - float threshold = 1.5f; + m_skyProgram = loadProgram("vs_hdr_skybox", "fs_hdr_skybox"); + m_lumProgram = loadProgram("vs_hdr_lum", "fs_hdr_lum"); + m_lumAvgProgram = loadProgram("vs_hdr_lumavg", "fs_hdr_lumavg"); + m_blurProgram = loadProgram("vs_hdr_blur", "fs_hdr_blur"); + m_brightProgram = loadProgram("vs_hdr_bright", "fs_hdr_bright"); + m_meshProgram = loadProgram("vs_hdr_mesh", "fs_hdr_mesh"); + m_tonemapProgram = loadProgram("vs_hdr_tonemap", "fs_hdr_tonemap"); - int32_t scrollArea = 0; + s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1); + s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); + s_texLum = bgfx::createUniform("s_texLum", bgfx::UniformType::Int1); + s_texBlur = bgfx::createUniform("s_texBlur", bgfx::UniformType::Int1); + u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4); + u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformType::Vec4); + u_offset = bgfx::createUniform("u_offset", bgfx::UniformType::Vec4, 16); - float time = 0.0f; + m_mesh = meshLoad("meshes/bunny.bin"); - entry::MouseState mouseState; - while (!entry::processEvents(width, height, debug, reset, &mouseState) ) - { - if (oldWidth != width - || oldHeight != height - || oldReset != reset) - { - // Recreate variable size render targets when resolution changes. - oldWidth = width; - oldHeight = height; - oldReset = reset; + m_fbtextures[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT|BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP); + m_fbtextures[1] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY); + m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true); - uint32_t msaa = (reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT; + m_lum[0] = bgfx::createFrameBuffer(128, 128, bgfx::TextureFormat::BGRA8); + m_lum[1] = bgfx::createFrameBuffer( 64, 64, bgfx::TextureFormat::BGRA8); + m_lum[2] = bgfx::createFrameBuffer( 16, 16, bgfx::TextureFormat::BGRA8); + m_lum[3] = bgfx::createFrameBuffer( 4, 4, bgfx::TextureFormat::BGRA8); + m_lum[4] = bgfx::createFrameBuffer( 1, 1, bgfx::TextureFormat::BGRA8); - bgfx::destroyFrameBuffer(fbh); + m_bright = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Half, bgfx::TextureFormat::BGRA8); + m_blur = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Eighth, bgfx::TextureFormat::BGRA8); - fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::BGRA8, ( (msaa+1)<>BGFX_RESET_MSAA_SHIFT; - // Shutdown bgfx. - bgfx::shutdown(); + bgfx::destroyFrameBuffer(m_fbh); - return 0; -} + m_fbtextures[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, ( (msaa+1)<