/* * Copyright 2011-2015 Branimir Karadzic. All rights reserved. * License: http://www.opensource.org/licenses/BSD-2-Clause */ #include "common.h" #include "bgfx_utils.h" #include "imgui/imgui.h" static float s_texelHalf = 0.0f; static bool s_originBottomLeft = false; struct PosColorTexCoord0Vertex { float m_x; float m_y; float m_z; uint32_t m_rgba; float m_u; float m_v; static void init() { ms_decl .begin() .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float) .end(); } static bgfx::VertexDecl ms_decl; }; 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) ) { bgfx::TransientVertexBuffer vb; bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl); PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data; const float zz = 0.0f; const float minx = -_width; const float maxx = _width; const float miny = 0.0f; const float maxy = _height*2.0f; const float texelHalfW = s_texelHalf/_textureWidth; const float texelHalfH = s_texelHalf/_textureHeight; const float minu = -1.0f + texelHalfW; const float maxu = 1.0f + texelHalfW; float minv = texelHalfH; float maxv = 2.0f + texelHalfH; if (_originBottomLeft) { float temp = minv; minv = maxv; maxv = temp; minv -= 1.0f; maxv -= 1.0f; } vertex[0].m_x = minx; vertex[0].m_y = miny; vertex[0].m_z = zz; vertex[0].m_rgba = 0xffffffff; vertex[0].m_u = minu; vertex[0].m_v = minv; vertex[1].m_x = maxx; vertex[1].m_y = miny; vertex[1].m_z = zz; vertex[1].m_rgba = 0xffffffff; vertex[1].m_u = maxu; vertex[1].m_v = minv; vertex[2].m_x = maxx; vertex[2].m_y = maxy; vertex[2].m_z = zz; vertex[2].m_rgba = 0xffffffff; vertex[2].m_u = maxu; vertex[2].m_v = maxv; bgfx::setVertexBuffer(&vb); } } void setOffsets2x2Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height) { float offsets[16][4]; float du = 1.0f/_width; float dv = 1.0f/_height; uint32_t num = 0; for (uint32_t yy = 0; yy < 3; ++yy) { for (uint32_t xx = 0; xx < 3; ++xx) { offsets[num][0] = (xx - s_texelHalf) * du; offsets[num][1] = (yy - s_texelHalf) * dv; ++num; } } bgfx::setUniform(_handle, offsets, num); } void setOffsets4x4Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height) { float offsets[16][4]; float du = 1.0f/_width; float dv = 1.0f/_height; uint32_t num = 0; for (uint32_t yy = 0; yy < 4; ++yy) { for (uint32_t xx = 0; xx < 4; ++xx) { offsets[num][0] = (xx - 1.0f - s_texelHalf) * du; offsets[num][1] = (yy - 1.0f - s_texelHalf) * dv; ++num; } } bgfx::setUniform(_handle, offsets, num); } inline float square(float _x) { return _x*_x; } class HDR : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { Args args(_argc, _argv); 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(m_width, m_height, m_reset); // 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("uffizi.dds" , 0 | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | BGFX_TEXTURE_W_CLAMP ); 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"); 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); m_mesh = meshLoad("meshes/bunny.bin"); 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); 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); m_bright = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Half, bgfx::TextureFormat::BGRA8); m_blur = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Eighth, bgfx::TextureFormat::BGRA8); m_lumBgra8 = 0; if ( (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) == (bgfx::getCaps()->supported & (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) ) ) { m_rb = bgfx::createTexture2D(1, 1, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_READ_BACK); } else { m_rb.idx = bgfx::invalidHandle; } // Imgui. imguiCreate(); 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; m_oldWidth = 0; m_oldHeight = 0; m_oldReset = m_reset; m_speed = 0.37f; m_middleGray = 0.18f; m_white = 1.1f; m_threshold = 1.5f; m_scrollArea = 0; m_time = 0.0f; } virtual int shutdown() BX_OVERRIDE { // Cleanup. imguiDestroy(); meshUnload(m_mesh); for (uint32_t ii = 0; ii < BX_COUNTOF(m_lum); ++ii) { bgfx::destroyFrameBuffer(m_lum[ii]); } 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); if (bgfx::isValid(m_rb) ) { bgfx::destroyTexture(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); // Shutdown bgfx. bgfx::shutdown(); return 0; } bool update() BX_OVERRIDE { if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) { if (m_oldWidth != m_width || m_oldHeight != m_height || m_oldReset != m_reset) { // Recreate variable size render targets when resolution changes. m_oldWidth = m_width; m_oldHeight = m_height; m_oldReset = m_reset; uint32_t msaa = (m_reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT; bgfx::destroyFrameBuffer(m_fbh); m_fbtextures[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, ( (msaa+1)<