hdr.cpp 16 KB

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