hdr.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "imgui/imgui.h"
  8. #include <bx/rng.h>
  9. static float s_texelHalf = 0.0f;
  10. struct PosColorTexCoord0Vertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_rgba;
  16. float m_u;
  17. float m_v;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  24. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  25. .end();
  26. }
  27. static bgfx::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  30. void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
  31. {
  32. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
  33. {
  34. bgfx::TransientVertexBuffer vb;
  35. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
  36. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  37. const float zz = 0.0f;
  38. const float minx = -_width;
  39. const float maxx = _width;
  40. const float miny = 0.0f;
  41. const float maxy = _height*2.0f;
  42. const float texelHalfW = s_texelHalf/_textureWidth;
  43. const float texelHalfH = s_texelHalf/_textureHeight;
  44. const float minu = -1.0f + texelHalfW;
  45. const float maxu = 1.0f + texelHalfW;
  46. float minv = texelHalfH;
  47. float maxv = 2.0f + texelHalfH;
  48. if (_originBottomLeft)
  49. {
  50. float temp = minv;
  51. minv = maxv;
  52. maxv = temp;
  53. minv -= 1.0f;
  54. maxv -= 1.0f;
  55. }
  56. vertex[0].m_x = minx;
  57. vertex[0].m_y = miny;
  58. vertex[0].m_z = zz;
  59. vertex[0].m_rgba = 0xffffffff;
  60. vertex[0].m_u = minu;
  61. vertex[0].m_v = minv;
  62. vertex[1].m_x = maxx;
  63. vertex[1].m_y = miny;
  64. vertex[1].m_z = zz;
  65. vertex[1].m_rgba = 0xffffffff;
  66. vertex[1].m_u = maxu;
  67. vertex[1].m_v = minv;
  68. vertex[2].m_x = maxx;
  69. vertex[2].m_y = maxy;
  70. vertex[2].m_z = zz;
  71. vertex[2].m_rgba = 0xffffffff;
  72. vertex[2].m_u = maxu;
  73. vertex[2].m_v = maxv;
  74. bgfx::setVertexBuffer(0, &vb);
  75. }
  76. }
  77. void setOffsets2x2Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height)
  78. {
  79. float offsets[16][4];
  80. float du = 1.0f/_width;
  81. float dv = 1.0f/_height;
  82. uint16_t num = 0;
  83. for (uint32_t yy = 0; yy < 3; ++yy)
  84. {
  85. for (uint32_t xx = 0; xx < 3; ++xx)
  86. {
  87. offsets[num][0] = (xx - s_texelHalf) * du;
  88. offsets[num][1] = (yy - s_texelHalf) * dv;
  89. ++num;
  90. }
  91. }
  92. bgfx::setUniform(_handle, offsets, num);
  93. }
  94. void setOffsets4x4Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height)
  95. {
  96. float offsets[16][4];
  97. float du = 1.0f/_width;
  98. float dv = 1.0f/_height;
  99. uint16_t num = 0;
  100. for (uint32_t yy = 0; yy < 4; ++yy)
  101. {
  102. for (uint32_t xx = 0; xx < 4; ++xx)
  103. {
  104. offsets[num][0] = (xx - 1.0f - s_texelHalf) * du;
  105. offsets[num][1] = (yy - 1.0f - s_texelHalf) * dv;
  106. ++num;
  107. }
  108. }
  109. bgfx::setUniform(_handle, offsets, num);
  110. }
  111. inline float square(float _x)
  112. {
  113. return _x*_x;
  114. }
  115. class ExampleHDR : public entry::AppI
  116. {
  117. void init(int _argc, char** _argv) BX_OVERRIDE
  118. {
  119. Args args(_argc, _argv);
  120. m_width = 1280;
  121. m_height = 720;
  122. m_debug = BGFX_DEBUG_TEXT;
  123. m_reset = BGFX_RESET_VSYNC;
  124. bgfx::init(args.m_type, args.m_pciId);
  125. bgfx::reset(m_width, m_height, m_reset);
  126. // Enable m_debug text.
  127. bgfx::setDebug(m_debug);
  128. // Create vertex stream declaration.
  129. PosColorTexCoord0Vertex::init();
  130. m_uffizi = loadTexture("textures/uffizi.dds"
  131. , 0
  132. | BGFX_TEXTURE_U_CLAMP
  133. | BGFX_TEXTURE_V_CLAMP
  134. | BGFX_TEXTURE_W_CLAMP
  135. );
  136. m_skyProgram = loadProgram("vs_hdr_skybox", "fs_hdr_skybox");
  137. m_lumProgram = loadProgram("vs_hdr_lum", "fs_hdr_lum");
  138. m_lumAvgProgram = loadProgram("vs_hdr_lumavg", "fs_hdr_lumavg");
  139. m_blurProgram = loadProgram("vs_hdr_blur", "fs_hdr_blur");
  140. m_brightProgram = loadProgram("vs_hdr_bright", "fs_hdr_bright");
  141. m_meshProgram = loadProgram("vs_hdr_mesh", "fs_hdr_mesh");
  142. m_tonemapProgram = loadProgram("vs_hdr_tonemap", "fs_hdr_tonemap");
  143. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  144. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  145. s_texLum = bgfx::createUniform("s_texLum", bgfx::UniformType::Int1);
  146. s_texBlur = bgfx::createUniform("s_texBlur", bgfx::UniformType::Int1);
  147. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  148. u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformType::Vec4);
  149. u_offset = bgfx::createUniform("u_offset", bgfx::UniformType::Vec4, 16);
  150. m_mesh = meshLoad("meshes/bunny.bin");
  151. m_fbtextures[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP);
  152. m_fbtextures[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY);
  153. m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
  154. m_lum[0] = bgfx::createFrameBuffer(128, 128, bgfx::TextureFormat::BGRA8);
  155. m_lum[1] = bgfx::createFrameBuffer( 64, 64, bgfx::TextureFormat::BGRA8);
  156. m_lum[2] = bgfx::createFrameBuffer( 16, 16, bgfx::TextureFormat::BGRA8);
  157. m_lum[3] = bgfx::createFrameBuffer( 4, 4, bgfx::TextureFormat::BGRA8);
  158. m_lum[4] = bgfx::createFrameBuffer( 1, 1, bgfx::TextureFormat::BGRA8);
  159. m_bright = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Half, bgfx::TextureFormat::BGRA8);
  160. m_blur = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Eighth, bgfx::TextureFormat::BGRA8);
  161. m_lumBgra8 = 0;
  162. if ( (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) == (bgfx::getCaps()->supported & (BGFX_CAPS_TEXTURE_BLIT|BGFX_CAPS_TEXTURE_READ_BACK) ) )
  163. {
  164. m_rb = bgfx::createTexture2D(1, 1, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_READ_BACK);
  165. }
  166. else
  167. {
  168. m_rb.idx = bgfx::kInvalidHandle;
  169. }
  170. // Imgui.
  171. imguiCreate();
  172. m_caps = bgfx::getCaps();
  173. s_texelHalf = bgfx::RendererType::Direct3D9 == m_caps->rendererType ? 0.5f : 0.0f;
  174. m_oldWidth = 0;
  175. m_oldHeight = 0;
  176. m_oldReset = m_reset;
  177. m_speed = 0.37f;
  178. m_middleGray = 0.18f;
  179. m_white = 1.1f;
  180. m_threshold = 1.5f;
  181. m_scrollArea = 0;
  182. m_time = 0.0f;
  183. }
  184. virtual int shutdown() BX_OVERRIDE
  185. {
  186. // Cleanup.
  187. imguiDestroy();
  188. meshUnload(m_mesh);
  189. for (uint32_t ii = 0; ii < BX_COUNTOF(m_lum); ++ii)
  190. {
  191. bgfx::destroyFrameBuffer(m_lum[ii]);
  192. }
  193. bgfx::destroyFrameBuffer(m_bright);
  194. bgfx::destroyFrameBuffer(m_blur);
  195. bgfx::destroyFrameBuffer(m_fbh);
  196. bgfx::destroyProgram(m_meshProgram);
  197. bgfx::destroyProgram(m_skyProgram);
  198. bgfx::destroyProgram(m_tonemapProgram);
  199. bgfx::destroyProgram(m_lumProgram);
  200. bgfx::destroyProgram(m_lumAvgProgram);
  201. bgfx::destroyProgram(m_blurProgram);
  202. bgfx::destroyProgram(m_brightProgram);
  203. bgfx::destroyTexture(m_uffizi);
  204. if (bgfx::isValid(m_rb) )
  205. {
  206. bgfx::destroyTexture(m_rb);
  207. }
  208. bgfx::destroyUniform(s_texCube);
  209. bgfx::destroyUniform(s_texColor);
  210. bgfx::destroyUniform(s_texLum);
  211. bgfx::destroyUniform(s_texBlur);
  212. bgfx::destroyUniform(u_mtx);
  213. bgfx::destroyUniform(u_tonemap);
  214. bgfx::destroyUniform(u_offset);
  215. // Shutdown bgfx.
  216. bgfx::shutdown();
  217. return 0;
  218. }
  219. bool update() BX_OVERRIDE
  220. {
  221. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  222. {
  223. if (m_oldWidth != m_width
  224. || m_oldHeight != m_height
  225. || m_oldReset != m_reset)
  226. {
  227. // Recreate variable size render targets when resolution changes.
  228. m_oldWidth = m_width;
  229. m_oldHeight = m_height;
  230. m_oldReset = m_reset;
  231. uint32_t msaa = (m_reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  232. bgfx::destroyFrameBuffer(m_fbh);
  233. m_fbtextures[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, ((msaa + 1) << BGFX_TEXTURE_RT_MSAA_SHIFT) | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP);
  234. m_fbtextures[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT) );
  235. m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
  236. }
  237. imguiBeginFrame(m_mouseState.m_mx
  238. , m_mouseState.m_my
  239. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  240. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  241. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  242. , m_mouseState.m_mz
  243. , uint16_t(m_width)
  244. , uint16_t(m_height)
  245. );
  246. imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 2, &m_scrollArea);
  247. imguiSeparatorLine();
  248. imguiSlider("Speed", m_speed, 0.0f, 1.0f, 0.01f);
  249. imguiSeparator();
  250. imguiSlider("Middle gray", m_middleGray, 0.1f, 1.0f, 0.01f);
  251. imguiSlider("White point", m_white, 0.1f, 2.0f, 0.01f);
  252. imguiSlider("Threshold", m_threshold, 0.1f, 2.0f, 0.01f);
  253. if (bgfx::isValid(m_rb) )
  254. {
  255. union { uint32_t color; uint8_t bgra[4]; } cast = { m_lumBgra8 };
  256. float exponent = cast.bgra[3]/255.0f * 255.0f - 128.0f;
  257. float lumAvg = cast.bgra[2]/255.0f * bx::fexp2(exponent);
  258. imguiSlider("Lum Avg", lumAvg, 0.0f, 1.0f, 0.01f, false);
  259. }
  260. imguiEndScrollArea();
  261. imguiEndFrame();
  262. // This dummy draw call is here to make sure that view 0 is cleared
  263. // if no other draw calls are submitted to view 0.
  264. bgfx::touch(0);
  265. int64_t now = bx::getHPCounter();
  266. static int64_t last = now;
  267. const int64_t frameTime = now - last;
  268. last = now;
  269. const double freq = double(bx::getHPFrequency() );
  270. const double toMs = 1000.0/freq;
  271. m_time += (float)(frameTime*m_speed/freq);
  272. // Use m_debug font to print information about this example.
  273. bgfx::dbgTextClear();
  274. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/09-hdr");
  275. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Using multiple views with frame buffers, and view order remapping.");
  276. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  277. uint8_t shuffle[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  278. bx::shuffle(&m_rng, shuffle, BX_COUNTOF(shuffle) );
  279. uint8_t hdrSkybox = shuffle[0];
  280. uint8_t hdrMesh = shuffle[1];
  281. uint8_t hdrLuminance = shuffle[2];
  282. uint8_t hdrLumScale0 = shuffle[3];
  283. uint8_t hdrLumScale1 = shuffle[4];
  284. uint8_t hdrLumScale2 = shuffle[5];
  285. uint8_t hdrLumScale3 = shuffle[6];
  286. uint8_t hdrBrightness = shuffle[7];
  287. uint8_t hdrVBlur = shuffle[8];
  288. uint8_t hdrHBlurTonemap = shuffle[9];
  289. // Set views.
  290. bgfx::setViewName(hdrSkybox, "Skybox");
  291. bgfx::setViewClear(hdrSkybox, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
  292. bgfx::setViewRect(hdrSkybox, 0, 0, bgfx::BackbufferRatio::Equal);
  293. bgfx::setViewFrameBuffer(hdrSkybox, m_fbh);
  294. bgfx::setViewName(hdrMesh, "Mesh");
  295. bgfx::setViewClear(hdrMesh, BGFX_CLEAR_DISCARD_DEPTH | BGFX_CLEAR_DISCARD_STENCIL);
  296. bgfx::setViewRect(hdrMesh, 0, 0, bgfx::BackbufferRatio::Equal);
  297. bgfx::setViewFrameBuffer(hdrMesh, m_fbh);
  298. bgfx::setViewName(hdrLuminance, "Luminance");
  299. bgfx::setViewRect(hdrLuminance, 0, 0, 128, 128);
  300. bgfx::setViewFrameBuffer(hdrLuminance, m_lum[0]);
  301. bgfx::setViewName(hdrLumScale0, "Downscale luminance 0");
  302. bgfx::setViewRect(hdrLumScale0, 0, 0, 64, 64);
  303. bgfx::setViewFrameBuffer(hdrLumScale0, m_lum[1]);
  304. bgfx::setViewName(hdrLumScale1, "Downscale luminance 1");
  305. bgfx::setViewRect(hdrLumScale1, 0, 0, 16, 16);
  306. bgfx::setViewFrameBuffer(hdrLumScale1, m_lum[2]);
  307. bgfx::setViewName(hdrLumScale2, "Downscale luminance 2");
  308. bgfx::setViewRect(hdrLumScale2, 0, 0, 4, 4);
  309. bgfx::setViewFrameBuffer(hdrLumScale2, m_lum[3]);
  310. bgfx::setViewName(hdrLumScale3, "Downscale luminance 3");
  311. bgfx::setViewRect(hdrLumScale3, 0, 0, 1, 1);
  312. bgfx::setViewFrameBuffer(hdrLumScale3, m_lum[4]);
  313. bgfx::setViewName(hdrBrightness, "Brightness");
  314. bgfx::setViewRect(hdrBrightness, 0, 0, bgfx::BackbufferRatio::Half);
  315. bgfx::setViewFrameBuffer(hdrBrightness, m_bright);
  316. bgfx::setViewName(hdrVBlur, "Blur vertical");
  317. bgfx::setViewRect(hdrVBlur, 0, 0, bgfx::BackbufferRatio::Eighth);
  318. bgfx::setViewFrameBuffer(hdrVBlur, m_blur);
  319. bgfx::setViewName(hdrHBlurTonemap, "Blur horizontal + tonemap");
  320. bgfx::setViewRect(hdrHBlurTonemap, 0, 0, bgfx::BackbufferRatio::Equal);
  321. bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
  322. bgfx::setViewFrameBuffer(hdrHBlurTonemap, invalid);
  323. float proj[16];
  324. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  325. uint8_t order[] =
  326. {
  327. hdrSkybox,
  328. hdrMesh,
  329. hdrLuminance,
  330. hdrLumScale0,
  331. hdrLumScale1,
  332. hdrLumScale2,
  333. hdrLumScale3,
  334. hdrBrightness,
  335. hdrVBlur,
  336. hdrHBlurTonemap
  337. };
  338. bgfx::setViewOrder(0, BX_COUNTOF(order), order);
  339. // Set view and projection matrix for view 0.
  340. for (uint8_t ii = 0; ii < BX_COUNTOF(order); ++ii)
  341. {
  342. bgfx::setViewTransform(ii, NULL, proj);
  343. }
  344. float at[3] = { 0.0f, 1.0f, 0.0f };
  345. float eye[3] = { 0.0f, 1.0f, -2.5f };
  346. float mtx[16];
  347. bx::mtxRotateXY(mtx
  348. , 0.0f
  349. , m_time
  350. );
  351. float temp[4];
  352. bx::vec3MulMtx(temp, eye, mtx);
  353. float view[16];
  354. bx::mtxLookAt(view, temp, at);
  355. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  356. // Set view and projection matrix for view hdrMesh.
  357. bgfx::setViewTransform(hdrMesh, view, proj);
  358. float tonemap[4] = { m_middleGray, square(m_white), m_threshold, m_time };
  359. // Render skybox into view hdrSkybox.
  360. bgfx::setTexture(0, s_texCube, m_uffizi);
  361. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  362. bgfx::setUniform(u_mtx, mtx);
  363. screenSpaceQuad( (float)m_width, (float)m_height, true);
  364. bgfx::submit(hdrSkybox, m_skyProgram);
  365. // Render m_mesh into view hdrMesh.
  366. bgfx::setTexture(0, s_texCube, m_uffizi);
  367. bgfx::setUniform(u_tonemap, tonemap);
  368. meshSubmit(m_mesh, hdrMesh, m_meshProgram, NULL);
  369. // Calculate luminance.
  370. setOffsets2x2Lum(u_offset, 128, 128);
  371. bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
  372. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  373. screenSpaceQuad(128.0f, 128.0f, m_caps->originBottomLeft);
  374. bgfx::submit(hdrLuminance, m_lumProgram);
  375. // Downscale luminance 0.
  376. setOffsets4x4Lum(u_offset, 128, 128);
  377. bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[0]) );
  378. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  379. screenSpaceQuad(64.0f, 64.0f, m_caps->originBottomLeft);
  380. bgfx::submit(hdrLumScale0, m_lumAvgProgram);
  381. // Downscale luminance 1.
  382. setOffsets4x4Lum(u_offset, 64, 64);
  383. bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[1]) );
  384. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  385. screenSpaceQuad(16.0f, 16.0f, m_caps->originBottomLeft);
  386. bgfx::submit(hdrLumScale1, m_lumAvgProgram);
  387. // Downscale luminance 2.
  388. setOffsets4x4Lum(u_offset, 16, 16);
  389. bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[2]) );
  390. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  391. screenSpaceQuad(4.0f, 4.0f, m_caps->originBottomLeft);
  392. bgfx::submit(hdrLumScale2, m_lumAvgProgram);
  393. // Downscale luminance 3.
  394. setOffsets4x4Lum(u_offset, 4, 4);
  395. bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[3]) );
  396. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  397. screenSpaceQuad(1.0f, 1.0f, m_caps->originBottomLeft);
  398. bgfx::submit(hdrLumScale3, m_lumAvgProgram);
  399. // m_bright pass m_threshold is tonemap[3].
  400. setOffsets4x4Lum(u_offset, m_width/2, m_height/2);
  401. bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
  402. bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) );
  403. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  404. bgfx::setUniform(u_tonemap, tonemap);
  405. screenSpaceQuad( (float)m_width/2.0f, (float)m_height/2.0f, m_caps->originBottomLeft);
  406. bgfx::submit(hdrBrightness, m_brightProgram);
  407. // m_blur m_bright pass vertically.
  408. bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_bright) );
  409. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  410. bgfx::setUniform(u_tonemap, tonemap);
  411. screenSpaceQuad( (float)m_width/8.0f, (float)m_height/8.0f, m_caps->originBottomLeft);
  412. bgfx::submit(hdrVBlur, m_blurProgram);
  413. // m_blur m_bright pass horizontally, do tonemaping and combine.
  414. bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
  415. bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) );
  416. bgfx::setTexture(2, s_texBlur, bgfx::getTexture(m_blur) );
  417. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  418. screenSpaceQuad( (float)m_width, (float)m_height, m_caps->originBottomLeft);
  419. bgfx::submit(hdrHBlurTonemap, m_tonemapProgram);
  420. if (bgfx::isValid(m_rb) )
  421. {
  422. bgfx::blit(hdrHBlurTonemap, m_rb, 0, 0, bgfx::getTexture(m_lum[4]) );
  423. bgfx::readTexture(m_rb, &m_lumBgra8);
  424. }
  425. // Advance to next frame. Rendering thread will be kicked to
  426. // process submitted rendering primitives.
  427. bgfx::frame();
  428. return true;
  429. }
  430. return false;
  431. }
  432. entry::MouseState m_mouseState;
  433. bgfx::ProgramHandle m_skyProgram;
  434. bgfx::ProgramHandle m_lumProgram;
  435. bgfx::ProgramHandle m_lumAvgProgram;
  436. bgfx::ProgramHandle m_blurProgram;
  437. bgfx::ProgramHandle m_brightProgram;
  438. bgfx::ProgramHandle m_meshProgram;
  439. bgfx::ProgramHandle m_tonemapProgram;
  440. bgfx::TextureHandle m_uffizi;
  441. bgfx::UniformHandle s_texCube;
  442. bgfx::UniformHandle s_texColor;
  443. bgfx::UniformHandle s_texLum;
  444. bgfx::UniformHandle s_texBlur;
  445. bgfx::UniformHandle u_mtx;
  446. bgfx::UniformHandle u_tonemap;
  447. bgfx::UniformHandle u_offset;
  448. Mesh* m_mesh;
  449. bgfx::TextureHandle m_fbtextures[2];
  450. bgfx::TextureHandle m_rb;
  451. bgfx::FrameBufferHandle m_fbh;
  452. bgfx::FrameBufferHandle m_lum[5];
  453. bgfx::FrameBufferHandle m_bright;
  454. bgfx::FrameBufferHandle m_blur;
  455. bx::RngMwc m_rng;
  456. uint32_t m_width;
  457. uint32_t m_height;
  458. uint32_t m_debug;
  459. uint32_t m_reset;
  460. uint32_t m_lumBgra8;
  461. uint32_t m_oldWidth;
  462. uint32_t m_oldHeight;
  463. uint32_t m_oldReset;
  464. float m_speed;
  465. float m_middleGray;
  466. float m_white;
  467. float m_threshold;
  468. int32_t m_scrollArea;
  469. const bgfx::Caps* m_caps;
  470. float m_time;
  471. };
  472. ENTRY_IMPLEMENT_MAIN(ExampleHDR);