hdr.cpp 18 KB

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