bgfx/examples/09-hdr/hdr.cpp

633 lines
18 KiB
C++
Raw Normal View History

2013-02-27 09:24:16 +04:00
/*
2019-01-14 04:13:25 +03:00
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-02-27 09:24:16 +04:00
*/
#include "common.h"
2014-05-04 02:18:28 +04:00
#include "bgfx_utils.h"
#include "imgui/imgui.h"
2016-11-21 00:41:31 +03:00
#include <bx/rng.h>
2013-02-27 09:24:16 +04:00
2017-06-26 07:44:04 +03:00
namespace
{
2013-02-27 09:24:16 +04:00
static float s_texelHalf = 0.0f;
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();
2013-02-27 09:24:16 +04:00
}
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 (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
2013-02-27 09:24:16 +04:00
{
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)
{
2014-05-04 02:18:28 +04:00
float temp = minv;
minv = maxv;
maxv = temp;
2013-02-27 09:24:16 +04:00
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;
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, &vb);
2013-02-27 09:24:16 +04:00
}
}
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;
2017-03-12 09:17:34 +03:00
uint16_t num = 0;
2013-02-27 09:24:16 +04:00
for (uint32_t yy = 0; yy < 3; ++yy)
{
for (uint32_t xx = 0; xx < 3; ++xx)
{
offsets[num][0] = (xx - s_texelHalf) * du;
2013-02-27 09:31:47 +04:00
offsets[num][1] = (yy - s_texelHalf) * dv;
2013-02-27 09:24:16 +04:00
++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;
2017-03-12 01:44:00 +03:00
uint16_t num = 0;
2013-02-27 09:24:16 +04:00
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;
2013-02-27 09:31:47 +04:00
offsets[num][1] = (yy - 1.0f - s_texelHalf) * dv;
2013-02-27 09:24:16 +04:00
++num;
}
}
bgfx::setUniform(_handle, offsets, num);
}
2016-03-07 02:29:22 +03:00
class ExampleHDR : public entry::AppI
2013-02-27 09:24:16 +04:00
{
2017-06-26 07:44:04 +03:00
public:
ExampleHDR(const char* _name, const char* _description)
: entry::AppI(_name, _description)
{
}
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2014-02-06 11:07:11 +04:00
{
2015-10-24 06:57:04 +03:00
Args args(_argc, _argv);
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
2015-10-21 08:45:35 +03:00
m_reset = BGFX_RESET_VSYNC;
2015-08-22 07:06:46 +03:00
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
2015-08-22 07:06:46 +03:00
// Enable m_debug text.
bgfx::setDebug(m_debug);
// Create vertex stream declaration.
PosColorTexCoord0Vertex::init();
m_uffizi = loadTexture("textures/uffizi.ktx"
2015-08-22 07:06:46 +03:00
, 0
| BGFX_SAMPLER_U_CLAMP
| BGFX_SAMPLER_V_CLAMP
| BGFX_SAMPLER_W_CLAMP
2015-08-22 07:06:46 +03:00
);
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::Sampler);
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
s_texLum = bgfx::createUniform("s_texLum", bgfx::UniformType::Sampler);
s_texBlur = bgfx::createUniform("s_texBlur", bgfx::UniformType::Sampler);
2015-08-22 07:06:46 +03:00
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");
2017-07-26 05:47:07 +03:00
m_fbh.idx = bgfx::kInvalidHandle;
2015-08-22 07:06:46 +03:00
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;
2015-10-21 04:35:02 +03:00
if ( (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) == (bgfx::getCaps()->supported & (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) ) )
{
2016-08-20 07:05:37 +03:00
m_rb = bgfx::createTexture2D(1, 1, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_READ_BACK);
}
else
{
2017-06-10 07:57:08 +03:00
m_rb.idx = bgfx::kInvalidHandle;
}
2015-08-22 07:06:46 +03:00
// Imgui.
imguiCreate();
2016-04-02 08:51:35 +03:00
m_caps = bgfx::getCaps();
s_texelHalf = bgfx::RendererType::Direct3D9 == m_caps->rendererType ? 0.5f : 0.0f;
2015-08-22 07:06:46 +03:00
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;
}
2017-07-15 10:17:29 +03:00
virtual int shutdown() override
2013-02-27 09:24:16 +04:00
{
2015-08-22 07:06:46 +03:00
// Cleanup.
imguiDestroy();
2014-02-06 11:07:11 +04:00
2015-08-22 07:06:46 +03:00
meshUnload(m_mesh);
2013-02-27 09:24:16 +04:00
2015-08-22 07:06:46 +03:00
for (uint32_t ii = 0; ii < BX_COUNTOF(m_lum); ++ii)
2014-09-27 22:31:04 +04:00
{
bgfx::destroy(m_lum[ii]);
2014-09-27 22:31:04 +04:00
}
bgfx::destroy(m_bright);
bgfx::destroy(m_blur);
2017-07-26 05:47:07 +03:00
if (bgfx::isValid(m_fbh) )
{
bgfx::destroy(m_fbh);
}
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);
2015-10-21 07:32:28 +03:00
if (bgfx::isValid(m_rb) )
{
bgfx::destroy(m_rb);
2015-10-21 07:32:28 +03:00
}
2015-08-22 07:06:46 +03:00
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);
2015-08-22 07:06:46 +03:00
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
2013-02-27 09:24:16 +04:00
2017-07-15 10:17:29 +03:00
bool update() override
2015-08-22 07:06:46 +03:00
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
2017-07-26 05:47:07 +03:00
if (!bgfx::isValid(m_fbh)
|| m_oldWidth != m_width
2015-08-22 07:06:46 +03:00
|| 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;
2017-07-26 05:47:07 +03:00
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
, (uint64_t(msaa + 1) << BGFX_TEXTURE_RT_MSAA_SHIFT) | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP
2017-07-26 05:47:07 +03:00
);
const uint64_t textureFlags = BGFX_TEXTURE_RT_WRITE_ONLY|(uint64_t(msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT);
2017-07-26 05:47:07 +03:00
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
);
2015-08-22 07:06:46 +03:00
m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
}
2013-02-27 09:24:16 +04:00
2015-08-22 07:06:46 +03:00
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)
2015-08-22 07:06:46 +03:00
, m_mouseState.m_mz
2017-03-12 09:17:34 +03:00
, uint16_t(m_width)
, uint16_t(m_height)
2015-08-22 07:06:46 +03:00
);
2013-02-27 09:24:16 +04:00
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
2017-06-27 08:21:20 +03:00
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
2017-12-02 08:04:27 +03:00
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(m_width / 5.0f, m_height / 2.0f)
, ImGuiCond_FirstUseEver
2017-06-27 08:21:20 +03:00
);
2017-06-27 08:51:56 +03:00
ImGui::Begin("Settings"
2017-06-12 06:43:55 +03:00
, NULL
2017-12-02 08:04:27 +03:00
, 0
2017-06-12 06:43:55 +03:00
);
2013-02-27 09:24:16 +04:00
2017-06-12 06:43:55 +03:00
ImGui::SliderFloat("Speed", &m_speed, 0.0f, 1.0f);
ImGui::Separator();
2013-02-27 09:24:16 +04:00
2017-06-12 06:43:55 +03:00
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);
2013-02-27 09:24:16 +04:00
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;
2018-01-14 02:33:50 +03:00
float lumAvg = cast.bgra[2]/255.0f * bx::exp2(exponent);
2017-06-12 06:43:55 +03:00
ImGui::SliderFloat("Lum Avg", &lumAvg, 0.0f, 1.0f);
}
2017-06-12 06:43:55 +03:00
ImGui::End();
2015-08-22 07:06:46 +03:00
imguiEndFrame();
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
m_time += (float)(frameTime*m_speed/freq);
2017-11-28 03:34:27 +03:00
bgfx::ViewId shuffle[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2016-11-21 00:41:31 +03:00
bx::shuffle(&m_rng, shuffle, BX_COUNTOF(shuffle) );
2017-11-28 03:34:27 +03:00
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];
2015-08-22 07:06:46 +03:00
// Set views.
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);
2015-08-22 07:06:46 +03:00
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);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrLuminance, "Luminance");
bgfx::setViewRect(hdrLuminance, 0, 0, 128, 128);
bgfx::setViewFrameBuffer(hdrLuminance, m_lum[0]);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrLumScale0, "Downscale luminance 0");
bgfx::setViewRect(hdrLumScale0, 0, 0, 64, 64);
bgfx::setViewFrameBuffer(hdrLumScale0, m_lum[1]);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrLumScale1, "Downscale luminance 1");
bgfx::setViewRect(hdrLumScale1, 0, 0, 16, 16);
bgfx::setViewFrameBuffer(hdrLumScale1, m_lum[2]);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrLumScale2, "Downscale luminance 2");
bgfx::setViewRect(hdrLumScale2, 0, 0, 4, 4);
bgfx::setViewFrameBuffer(hdrLumScale2, m_lum[3]);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrLumScale3, "Downscale luminance 3");
bgfx::setViewRect(hdrLumScale3, 0, 0, 1, 1);
bgfx::setViewFrameBuffer(hdrLumScale3, m_lum[4]);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrBrightness, "Brightness");
bgfx::setViewRect(hdrBrightness, 0, 0, bgfx::BackbufferRatio::Half);
bgfx::setViewFrameBuffer(hdrBrightness, m_bright);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrVBlur, "Blur vertical");
bgfx::setViewRect(hdrVBlur, 0, 0, bgfx::BackbufferRatio::Eighth);
bgfx::setViewFrameBuffer(hdrVBlur, m_blur);
2015-08-22 07:06:46 +03:00
bgfx::setViewName(hdrHBlurTonemap, "Blur horizontal + tonemap");
bgfx::setViewRect(hdrHBlurTonemap, 0, 0, bgfx::BackbufferRatio::Equal);
2016-11-21 01:10:54 +03:00
bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
bgfx::setViewFrameBuffer(hdrHBlurTonemap, invalid);
2015-08-22 07:06:46 +03:00
2017-06-13 08:43:07 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
float proj[16];
2017-06-13 08:43:07 +03:00
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
2015-08-22 07:06:46 +03:00
2017-11-27 20:06:57 +03:00
bgfx::ViewId order[] =
{
hdrSkybox,
hdrMesh,
hdrLuminance,
hdrLumScale0,
hdrLumScale1,
hdrLumScale2,
hdrLumScale3,
hdrBrightness,
hdrVBlur,
hdrHBlurTonemap
};
2016-11-20 23:59:52 +03:00
bgfx::setViewOrder(0, BX_COUNTOF(order), order);
2015-08-22 07:06:46 +03:00
// Set view and projection matrix for view 0.
2017-03-12 09:17:34 +03:00
for (uint8_t ii = 0; ii < BX_COUNTOF(order); ++ii)
2015-08-22 07:06:46 +03:00
{
bgfx::setViewTransform(ii, NULL, proj);
2015-08-22 07:06:46 +03:00
}
2018-11-17 08:54:20 +03:00
const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };
2015-08-22 07:06:46 +03:00
float mtx[16];
bx::mtxRotateXY(mtx
, 0.0f
, m_time
);
2018-11-17 08:54:20 +03:00
const bx::Vec3 tmp = bx::mul(eye, mtx);
2015-08-22 07:06:46 +03:00
float view[16];
2018-11-17 08:54:20 +03:00
bx::mtxLookAt(view, tmp, at);
2017-06-13 08:43:07 +03:00
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
2015-08-22 07:06:46 +03:00
// Set view and projection matrix for view hdrMesh.
bgfx::setViewTransform(hdrMesh, view, proj);
2015-08-22 07:06:46 +03:00
2018-01-14 02:33:50 +03:00
float tonemap[4] = { m_middleGray, bx::square(m_white), m_threshold, m_time };
2015-08-22 07:06:46 +03:00
// Render skybox into view hdrSkybox.
2015-08-22 07:06:46 +03:00
bgfx::setTexture(0, s_texCube, m_uffizi);
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
bgfx::setUniform(u_mtx, mtx);
2015-08-22 07:06:46 +03:00
screenSpaceQuad( (float)m_width, (float)m_height, true);
bgfx::submit(hdrSkybox, m_skyProgram);
2015-08-22 07:06:46 +03:00
// Render m_mesh into view hdrMesh.
2015-08-22 07:06:46 +03:00
bgfx::setTexture(0, s_texCube, m_uffizi);
bgfx::setUniform(u_tonemap, tonemap);
meshSubmit(m_mesh, hdrMesh, m_meshProgram, NULL);
2015-08-22 07:06:46 +03:00
// Calculate luminance.
setOffsets2x2Lum(u_offset, 128, 128);
bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad(128.0f, 128.0f, m_caps->originBottomLeft);
bgfx::submit(hdrLuminance, m_lumProgram);
2015-08-22 07:06:46 +03:00
// Downscale luminance 0.
setOffsets4x4Lum(u_offset, 128, 128);
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[0]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad(64.0f, 64.0f, m_caps->originBottomLeft);
bgfx::submit(hdrLumScale0, m_lumAvgProgram);
2015-08-22 07:06:46 +03:00
// Downscale luminance 1.
setOffsets4x4Lum(u_offset, 64, 64);
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[1]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad(16.0f, 16.0f, m_caps->originBottomLeft);
bgfx::submit(hdrLumScale1, m_lumAvgProgram);
2015-08-22 07:06:46 +03:00
// Downscale luminance 2.
setOffsets4x4Lum(u_offset, 16, 16);
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[2]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad(4.0f, 4.0f, m_caps->originBottomLeft);
bgfx::submit(hdrLumScale2, m_lumAvgProgram);
2015-08-22 07:06:46 +03:00
// Downscale luminance 3.
setOffsets4x4Lum(u_offset, 4, 4);
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[3]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad(1.0f, 1.0f, m_caps->originBottomLeft);
bgfx::submit(hdrLumScale3, m_lumAvgProgram);
2015-08-22 07:06:46 +03:00
// 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, bgfx::getTexture(m_lum[4]) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
bgfx::setUniform(u_tonemap, tonemap);
2016-04-02 08:51:35 +03:00
screenSpaceQuad( (float)m_width/2.0f, (float)m_height/2.0f, m_caps->originBottomLeft);
bgfx::submit(hdrBrightness, m_brightProgram);
2015-08-22 07:06:46 +03:00
// m_blur m_bright pass vertically.
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_bright) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
bgfx::setUniform(u_tonemap, tonemap);
2016-04-02 08:51:35 +03:00
screenSpaceQuad( (float)m_width/8.0f, (float)m_height/8.0f, m_caps->originBottomLeft);
bgfx::submit(hdrVBlur, m_blurProgram);
2015-08-22 07:06:46 +03:00
// m_blur m_bright pass horizontally, do tonemaping and combine.
bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) );
bgfx::setTexture(2, s_texBlur, bgfx::getTexture(m_blur) );
bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
2016-04-02 08:51:35 +03:00
screenSpaceQuad( (float)m_width, (float)m_height, m_caps->originBottomLeft);
bgfx::submit(hdrHBlurTonemap, m_tonemapProgram);
2015-08-22 07:06:46 +03:00
if (bgfx::isValid(m_rb) )
{
bgfx::blit(hdrHBlurTonemap, m_rb, 0, 0, bgfx::getTexture(m_lum[4]) );
bgfx::readTexture(m_rb, &m_lumBgra8);
}
2015-08-22 07:06:46 +03:00
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2017-06-30 09:19:20 +03:00
return true;
2014-09-27 22:31:04 +04:00
}
2013-02-27 09:24:16 +04:00
2015-08-22 07:06:46 +03:00
return false;
2013-02-27 09:24:16 +04:00
}
2015-08-22 07:06:46 +03:00
entry::MouseState m_mouseState;
bgfx::ProgramHandle m_skyProgram;
bgfx::ProgramHandle m_lumProgram;
bgfx::ProgramHandle m_lumAvgProgram;
bgfx::ProgramHandle m_blurProgram;
bgfx::ProgramHandle m_brightProgram;
bgfx::ProgramHandle m_meshProgram;
bgfx::ProgramHandle m_tonemapProgram;
bgfx::TextureHandle m_uffizi;
bgfx::UniformHandle s_texCube;
bgfx::UniformHandle s_texColor;
bgfx::UniformHandle s_texLum;
bgfx::UniformHandle s_texBlur;
bgfx::UniformHandle u_mtx;
bgfx::UniformHandle u_tonemap;
bgfx::UniformHandle u_offset;
Mesh* m_mesh;
bgfx::TextureHandle m_fbtextures[2];
bgfx::TextureHandle m_rb;
2015-08-22 07:06:46 +03:00
bgfx::FrameBufferHandle m_fbh;
bgfx::FrameBufferHandle m_lum[5];
bgfx::FrameBufferHandle m_bright;
bgfx::FrameBufferHandle m_blur;
2016-11-21 00:41:31 +03:00
bx::RngMwc m_rng;
2015-08-22 07:06:46 +03:00
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
uint32_t m_lumBgra8;
2015-08-22 07:06:46 +03:00
uint32_t m_oldWidth;
uint32_t m_oldHeight;
uint32_t m_oldReset;
float m_speed;
float m_middleGray;
float m_white;
float m_threshold;
int32_t m_scrollArea;
2016-04-02 08:51:35 +03:00
const bgfx::Caps* m_caps;
2015-08-22 07:06:46 +03:00
float m_time;
};
2013-02-27 09:24:16 +04:00
2017-06-26 07:44:04 +03:00
} // namespace
2017-07-03 06:19:25 +03:00
ENTRY_IMPLEMENT_MAIN(ExampleHDR, "09-hdr", "Using multiple views with frame buffers, and view order remapping.");