hdr.cpp 18 KB

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