mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-24 16:48:18 -05:00
Fixed 09-hdr for Emscripten.
This commit is contained in:
parent
2234678dde
commit
f149fcec94
2 changed files with 370 additions and 319 deletions
|
@ -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_TEXTURE_RT_MSAA_SHIFT)|BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
|
||||
fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT) );
|
||||
fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
|
||||
}
|
||||
// Imgui.
|
||||
imguiCreate();
|
||||
|
||||
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_mz
|
||||
, width
|
||||
, height
|
||||
);
|
||||
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;
|
||||
|
||||
imguiBeginScrollArea("Settings", width - width / 5 - 10, 10, width / 5, height / 3, &scrollArea);
|
||||
imguiSeparatorLine();
|
||||
m_oldWidth = 0;
|
||||
m_oldHeight = 0;
|
||||
m_oldReset = m_reset;
|
||||
|
||||
imguiSlider("Speed", speed, 0.0f, 1.0f, 0.01f);
|
||||
imguiSeparator();
|
||||
m_speed = 0.37f;
|
||||
m_middleGray = 0.18f;
|
||||
m_white = 1.1f;
|
||||
m_threshold = 1.5f;
|
||||
|
||||
imguiSlider("Middle gray", middleGray, 0.1f, 1.0f, 0.01f);
|
||||
imguiSlider("White point", white, 0.1f, 2.0f, 0.01f);
|
||||
imguiSlider("Threshold", threshold, 0.1f, 2.0f, 0.01f);
|
||||
m_scrollArea = 0;
|
||||
|
||||
imguiEndScrollArea();
|
||||
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() );
|
||||
const double toMs = 1000.0/freq;
|
||||
|
||||
time += (float)(frameTime*speed/freq);
|
||||
|
||||
// Use 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, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
|
||||
|
||||
// Set views.
|
||||
for (uint32_t ii = 0; ii < 6; ++ii)
|
||||
{
|
||||
bgfx::setViewRect(ii, 0, 0, width, height);
|
||||
}
|
||||
bgfx::setViewFrameBuffer(0, fbh);
|
||||
bgfx::setViewFrameBuffer(1, fbh);
|
||||
bgfx::setViewClear(1, BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL);
|
||||
|
||||
bgfx::setViewRect(2, 0, 0, 128, 128);
|
||||
bgfx::setViewFrameBuffer(2, lum[0]);
|
||||
|
||||
bgfx::setViewRect(3, 0, 0, 64, 64);
|
||||
bgfx::setViewFrameBuffer(3, lum[1]);
|
||||
|
||||
bgfx::setViewRect(4, 0, 0, 16, 16);
|
||||
bgfx::setViewFrameBuffer(4, lum[2]);
|
||||
|
||||
bgfx::setViewRect(5, 0, 0, 4, 4);
|
||||
bgfx::setViewFrameBuffer(5, lum[3]);
|
||||
|
||||
bgfx::setViewRect(6, 0, 0, 1, 1);
|
||||
bgfx::setViewFrameBuffer(6, lum[4]);
|
||||
|
||||
bgfx::setViewRect(7, 0, 0, width/2, height/2);
|
||||
bgfx::setViewFrameBuffer(7, bright);
|
||||
|
||||
bgfx::setViewRect(8, 0, 0, width/8, height/8);
|
||||
bgfx::setViewFrameBuffer(8, blur);
|
||||
|
||||
bgfx::setViewRect(9, 0, 0, width, height);
|
||||
|
||||
float view[16];
|
||||
float proj[16];
|
||||
|
||||
bx::mtxIdentity(view);
|
||||
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
|
||||
|
||||
// Set view and projection matrix for view 0.
|
||||
for (uint32_t ii = 0; ii < 10; ++ii)
|
||||
{
|
||||
bgfx::setViewTransform(ii, view, proj);
|
||||
}
|
||||
|
||||
float at[3] = { 0.0f, 1.0f, 0.0f };
|
||||
float eye[3] = { 0.0f, 1.0f, -2.5f };
|
||||
|
||||
float mtx[16];
|
||||
bx::mtxRotateXY(mtx
|
||||
, 0.0f
|
||||
, time
|
||||
);
|
||||
|
||||
float temp[4];
|
||||
bx::vec3MulMtx(temp, eye, mtx);
|
||||
|
||||
bx::mtxLookAt(view, temp, at);
|
||||
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
|
||||
|
||||
// Set view and projection matrix for view 1.
|
||||
bgfx::setViewTransform(1, view, proj);
|
||||
|
||||
bgfx::setUniform(u_mtx, mtx);
|
||||
|
||||
// Render skybox into view 0.
|
||||
bgfx::setTexture(0, s_texCube, uffizi);
|
||||
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)width, (float)height, true);
|
||||
bgfx::submit(0, skyProgram);
|
||||
|
||||
// Render mesh into view 1
|
||||
bgfx::setTexture(0, s_texCube, uffizi);
|
||||
meshSubmit(mesh, 1, meshProgram, NULL);
|
||||
|
||||
// Calculate luminance.
|
||||
setOffsets2x2Lum(u_offset, 128, 128);
|
||||
bgfx::setTexture(0, s_texColor, fbtextures[0]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(128.0f, 128.0f, s_originBottomLeft);
|
||||
bgfx::submit(2, lumProgram);
|
||||
|
||||
// Downscale luminance 0.
|
||||
setOffsets4x4Lum(u_offset, 128, 128);
|
||||
bgfx::setTexture(0, s_texColor, lum[0]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(64.0f, 64.0f, s_originBottomLeft);
|
||||
bgfx::submit(3, lumAvgProgram);
|
||||
|
||||
// Downscale luminance 1.
|
||||
setOffsets4x4Lum(u_offset, 64, 64);
|
||||
bgfx::setTexture(0, s_texColor, lum[1]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(16.0f, 16.0f, s_originBottomLeft);
|
||||
bgfx::submit(4, lumAvgProgram);
|
||||
|
||||
// Downscale luminance 2.
|
||||
setOffsets4x4Lum(u_offset, 16, 16);
|
||||
bgfx::setTexture(0, s_texColor, lum[2]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(4.0f, 4.0f, s_originBottomLeft);
|
||||
bgfx::submit(5, lumAvgProgram);
|
||||
|
||||
// Downscale luminance 3.
|
||||
setOffsets4x4Lum(u_offset, 4, 4);
|
||||
bgfx::setTexture(0, s_texColor, lum[3]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(1.0f, 1.0f, s_originBottomLeft);
|
||||
bgfx::submit(6, lumAvgProgram);
|
||||
|
||||
float tonemap[4] = { middleGray, square(white), threshold, time };
|
||||
bgfx::setUniform(u_tonemap, tonemap);
|
||||
|
||||
// Bright pass threshold is tonemap[3].
|
||||
setOffsets4x4Lum(u_offset, width/2, height/2);
|
||||
bgfx::setTexture(0, s_texColor, fbtextures[0]);
|
||||
bgfx::setTexture(1, s_texLum, lum[4]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)width/2.0f, (float)height/2.0f, s_originBottomLeft);
|
||||
bgfx::submit(7, brightProgram);
|
||||
|
||||
// Blur bright pass vertically.
|
||||
bgfx::setTexture(0, s_texColor, bright);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)width/8.0f, (float)height/8.0f, s_originBottomLeft);
|
||||
bgfx::submit(8, blurProgram);
|
||||
|
||||
// Blur bright pass horizontally, do tonemaping and combine.
|
||||
bgfx::setTexture(0, s_texColor, fbtextures[0]);
|
||||
bgfx::setTexture(1, s_texLum, lum[4]);
|
||||
bgfx::setTexture(2, s_texBlur, blur);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)width, (float)height, s_originBottomLeft);
|
||||
bgfx::submit(9, tonemapProgram);
|
||||
|
||||
// Advance to next frame. Rendering thread will be kicked to
|
||||
// process submitted rendering primitives.
|
||||
bgfx::frame();
|
||||
m_time = 0.0f;
|
||||
}
|
||||
|
||||
// Cleanup.
|
||||
imguiDestroy();
|
||||
|
||||
meshUnload(mesh);
|
||||
|
||||
for (uint32_t ii = 0; ii < BX_COUNTOF(lum); ++ii)
|
||||
virtual int shutdown() BX_OVERRIDE
|
||||
{
|
||||
bgfx::destroyFrameBuffer(lum[ii]);
|
||||
// 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);
|
||||
|
||||
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;
|
||||
}
|
||||
bgfx::destroyFrameBuffer(bright);
|
||||
bgfx::destroyFrameBuffer(blur);
|
||||
bgfx::destroyFrameBuffer(fbh);
|
||||
|
||||
bgfx::destroyProgram(meshProgram);
|
||||
bgfx::destroyProgram(skyProgram);
|
||||
bgfx::destroyProgram(tonemapProgram);
|
||||
bgfx::destroyProgram(lumProgram);
|
||||
bgfx::destroyProgram(lumAvgProgram);
|
||||
bgfx::destroyProgram(blurProgram);
|
||||
bgfx::destroyProgram(brightProgram);
|
||||
bgfx::destroyTexture(uffizi);
|
||||
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;
|
||||
|
||||
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);
|
||||
uint32_t msaa = (m_reset&BGFX_RESET_MSAA_MASK)>>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)<<BGFX_TEXTURE_RT_MSAA_SHIFT)|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|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT) );
|
||||
m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
|
||||
}
|
||||
|
||||
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_mz
|
||||
, m_width
|
||||
, m_height
|
||||
);
|
||||
|
||||
imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
|
||||
imguiSeparatorLine();
|
||||
|
||||
imguiSlider("m_speed", m_speed, 0.0f, 1.0f, 0.01f);
|
||||
imguiSeparator();
|
||||
|
||||
imguiSlider("Middle gray", m_middleGray, 0.1f, 1.0f, 0.01f);
|
||||
imguiSlider("m_white point", m_white, 0.1f, 2.0f, 0.01f);
|
||||
imguiSlider("m_threshold", m_threshold, 0.1f, 2.0f, 0.01f);
|
||||
|
||||
imguiEndScrollArea();
|
||||
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() );
|
||||
const double toMs = 1000.0/freq;
|
||||
|
||||
m_time += (float)(frameTime*m_speed/freq);
|
||||
|
||||
// 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, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
|
||||
|
||||
// Set views.
|
||||
for (uint32_t ii = 0; ii < 6; ++ii)
|
||||
{
|
||||
bgfx::setViewRect(ii, 0, 0, m_width, m_height);
|
||||
}
|
||||
bgfx::setViewFrameBuffer(0, m_fbh);
|
||||
bgfx::setViewFrameBuffer(1, m_fbh);
|
||||
bgfx::setViewClear(1, BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL);
|
||||
|
||||
bgfx::setViewRect(2, 0, 0, 128, 128);
|
||||
bgfx::setViewFrameBuffer(2, m_lum[0]);
|
||||
|
||||
bgfx::setViewRect(3, 0, 0, 64, 64);
|
||||
bgfx::setViewFrameBuffer(3, m_lum[1]);
|
||||
|
||||
bgfx::setViewRect(4, 0, 0, 16, 16);
|
||||
bgfx::setViewFrameBuffer(4, m_lum[2]);
|
||||
|
||||
bgfx::setViewRect(5, 0, 0, 4, 4);
|
||||
bgfx::setViewFrameBuffer(5, m_lum[3]);
|
||||
|
||||
bgfx::setViewRect(6, 0, 0, 1, 1);
|
||||
bgfx::setViewFrameBuffer(6, m_lum[4]);
|
||||
|
||||
bgfx::setViewRect(7, 0, 0, m_width/2, m_height/2);
|
||||
bgfx::setViewFrameBuffer(7, m_bright);
|
||||
|
||||
bgfx::setViewRect(8, 0, 0, m_width/8, m_height/8);
|
||||
bgfx::setViewFrameBuffer(8, m_blur);
|
||||
|
||||
bgfx::setViewRect(9, 0, 0, m_width, m_height);
|
||||
|
||||
float view[16];
|
||||
float proj[16];
|
||||
|
||||
bx::mtxIdentity(view);
|
||||
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
|
||||
|
||||
// Set view and projection matrix for view 0.
|
||||
for (uint32_t ii = 0; ii < 10; ++ii)
|
||||
{
|
||||
bgfx::setViewTransform(ii, view, proj);
|
||||
}
|
||||
|
||||
float at[3] = { 0.0f, 1.0f, 0.0f };
|
||||
float eye[3] = { 0.0f, 1.0f, -2.5f };
|
||||
|
||||
float mtx[16];
|
||||
bx::mtxRotateXY(mtx
|
||||
, 0.0f
|
||||
, m_time
|
||||
);
|
||||
|
||||
float temp[4];
|
||||
bx::vec3MulMtx(temp, eye, mtx);
|
||||
|
||||
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);
|
||||
|
||||
bgfx::setUniform(u_mtx, mtx);
|
||||
|
||||
// Render skybox into view 0.
|
||||
bgfx::setTexture(0, s_texCube, m_uffizi);
|
||||
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)m_width, (float)m_height, true);
|
||||
bgfx::submit(0, m_skyProgram);
|
||||
|
||||
// Render m_mesh into view 1
|
||||
bgfx::setTexture(0, s_texCube, m_uffizi);
|
||||
meshSubmit(m_mesh, 1, 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, s_originBottomLeft);
|
||||
bgfx::submit(2, m_lumProgram);
|
||||
|
||||
// Downscale luminance 0.
|
||||
setOffsets4x4Lum(u_offset, 128, 128);
|
||||
bgfx::setTexture(0, s_texColor, m_lum[0]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(64.0f, 64.0f, s_originBottomLeft);
|
||||
bgfx::submit(3, m_lumAvgProgram);
|
||||
|
||||
// Downscale luminance 1.
|
||||
setOffsets4x4Lum(u_offset, 64, 64);
|
||||
bgfx::setTexture(0, s_texColor, m_lum[1]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(16.0f, 16.0f, s_originBottomLeft);
|
||||
bgfx::submit(4, m_lumAvgProgram);
|
||||
|
||||
// Downscale luminance 2.
|
||||
setOffsets4x4Lum(u_offset, 16, 16);
|
||||
bgfx::setTexture(0, s_texColor, m_lum[2]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(4.0f, 4.0f, s_originBottomLeft);
|
||||
bgfx::submit(5, m_lumAvgProgram);
|
||||
|
||||
// Downscale luminance 3.
|
||||
setOffsets4x4Lum(u_offset, 4, 4);
|
||||
bgfx::setTexture(0, s_texColor, m_lum[3]);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad(1.0f, 1.0f, s_originBottomLeft);
|
||||
bgfx::submit(6, m_lumAvgProgram);
|
||||
|
||||
float tonemap[4] = { m_middleGray, square(m_white), m_threshold, m_time };
|
||||
bgfx::setUniform(u_tonemap, tonemap);
|
||||
|
||||
// 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::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)m_width/2.0f, (float)m_height/2.0f, s_originBottomLeft);
|
||||
bgfx::submit(7, m_brightProgram);
|
||||
|
||||
// m_blur m_bright pass vertically.
|
||||
bgfx::setTexture(0, s_texColor, m_bright);
|
||||
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)m_width/8.0f, (float)m_height/8.0f, s_originBottomLeft);
|
||||
bgfx::submit(8, 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::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
|
||||
screenSpaceQuad( (float)m_width, (float)m_height, s_originBottomLeft);
|
||||
bgfx::submit(9, m_tonemapProgram);
|
||||
|
||||
// Advance to next frame. Rendering thread will be kicked to
|
||||
// process submitted rendering primitives.
|
||||
bgfx::frame();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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::FrameBufferHandle m_fbh;
|
||||
bgfx::FrameBufferHandle m_lum[5];
|
||||
bgfx::FrameBufferHandle m_bright;
|
||||
bgfx::FrameBufferHandle m_blur;
|
||||
|
||||
uint32_t m_width;
|
||||
uint32_t m_height;
|
||||
uint32_t m_debug;
|
||||
uint32_t m_reset;
|
||||
|
||||
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;
|
||||
|
||||
float m_time;
|
||||
};
|
||||
|
||||
ENTRY_IMPLEMENT_MAIN(HDR);
|
||||
|
|
|
@ -74,8 +74,6 @@ class DrawStress : public entry::AppI
|
|||
{
|
||||
void init(int /*_argc*/, char** /*_argv*/) BX_OVERRIDE
|
||||
{
|
||||
BX_UNUSED(_argc, _argv);
|
||||
|
||||
m_width = 1280;
|
||||
m_height = 720;
|
||||
m_debug = BGFX_DEBUG_TEXT;
|
||||
|
@ -171,8 +169,7 @@ class DrawStress : public entry::AppI
|
|||
|
||||
bool update() BX_OVERRIDE
|
||||
{
|
||||
entry::MouseState mouseState;
|
||||
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &mouseState) )
|
||||
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
||||
{
|
||||
int64_t now = bx::getHPCounter();
|
||||
static int64_t last = now;
|
||||
|
@ -210,11 +207,11 @@ class DrawStress : public entry::AppI
|
|||
|
||||
float time = (float)( (now-m_timeOffset)/freq);
|
||||
|
||||
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_mz
|
||||
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_mz
|
||||
, m_width
|
||||
, m_height
|
||||
);
|
||||
|
@ -316,6 +313,8 @@ class DrawStress : public entry::AppI
|
|||
return false;
|
||||
}
|
||||
|
||||
entry::MouseState m_mouseState;
|
||||
|
||||
uint32_t m_width;
|
||||
uint32_t m_height;
|
||||
uint32_t m_debug;
|
||||
|
|
Loading…
Reference in a new issue