hdr.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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. static bool s_originBottomLeft = false;
  10. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  11. {
  12. bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_originBottomLeft);
  13. }
  14. struct PosColorTexCoord0Vertex
  15. {
  16. float m_x;
  17. float m_y;
  18. float m_z;
  19. uint32_t m_rgba;
  20. float m_u;
  21. float m_v;
  22. static void init()
  23. {
  24. ms_decl
  25. .begin()
  26. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  27. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  28. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  29. .end();
  30. }
  31. static bgfx::VertexDecl ms_decl;
  32. };
  33. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  34. void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
  35. {
  36. if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
  37. {
  38. bgfx::TransientVertexBuffer vb;
  39. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
  40. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  41. const float zz = 0.0f;
  42. const float minx = -_width;
  43. const float maxx = _width;
  44. const float miny = 0.0f;
  45. const float maxy = _height*2.0f;
  46. const float texelHalfW = s_texelHalf/_textureWidth;
  47. const float texelHalfH = s_texelHalf/_textureHeight;
  48. const float minu = -1.0f + texelHalfW;
  49. const float maxu = 1.0f + texelHalfW;
  50. float minv = texelHalfH;
  51. float maxv = 2.0f + texelHalfH;
  52. if (_originBottomLeft)
  53. {
  54. float temp = minv;
  55. minv = maxv;
  56. maxv = temp;
  57. minv -= 1.0f;
  58. maxv -= 1.0f;
  59. }
  60. vertex[0].m_x = minx;
  61. vertex[0].m_y = miny;
  62. vertex[0].m_z = zz;
  63. vertex[0].m_rgba = 0xffffffff;
  64. vertex[0].m_u = minu;
  65. vertex[0].m_v = minv;
  66. vertex[1].m_x = maxx;
  67. vertex[1].m_y = miny;
  68. vertex[1].m_z = zz;
  69. vertex[1].m_rgba = 0xffffffff;
  70. vertex[1].m_u = maxu;
  71. vertex[1].m_v = minv;
  72. vertex[2].m_x = maxx;
  73. vertex[2].m_y = maxy;
  74. vertex[2].m_z = zz;
  75. vertex[2].m_rgba = 0xffffffff;
  76. vertex[2].m_u = maxu;
  77. vertex[2].m_v = maxv;
  78. bgfx::setVertexBuffer(&vb);
  79. }
  80. }
  81. void setOffsets2x2Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height)
  82. {
  83. float offsets[16][4];
  84. float du = 1.0f/_width;
  85. float dv = 1.0f/_height;
  86. uint32_t num = 0;
  87. for (uint32_t yy = 0; yy < 3; ++yy)
  88. {
  89. for (uint32_t xx = 0; xx < 3; ++xx)
  90. {
  91. offsets[num][0] = (xx - s_texelHalf) * du;
  92. offsets[num][1] = (yy - s_texelHalf) * dv;
  93. ++num;
  94. }
  95. }
  96. bgfx::setUniform(_handle, offsets, num);
  97. }
  98. void setOffsets4x4Lum(bgfx::UniformHandle _handle, uint32_t _width, uint32_t _height)
  99. {
  100. float offsets[16][4];
  101. float du = 1.0f/_width;
  102. float dv = 1.0f/_height;
  103. uint32_t num = 0;
  104. for (uint32_t yy = 0; yy < 4; ++yy)
  105. {
  106. for (uint32_t xx = 0; xx < 4; ++xx)
  107. {
  108. offsets[num][0] = (xx - 1.0f - s_texelHalf) * du;
  109. offsets[num][1] = (yy - 1.0f - s_texelHalf) * dv;
  110. ++num;
  111. }
  112. }
  113. bgfx::setUniform(_handle, offsets, num);
  114. }
  115. inline float square(float _x)
  116. {
  117. return _x*_x;
  118. }
  119. int _main_(int /*_argc*/, char** /*_argv*/)
  120. {
  121. PosColorTexCoord0Vertex::init();
  122. uint32_t width = 1280;
  123. uint32_t height = 720;
  124. uint32_t debug = BGFX_DEBUG_TEXT;
  125. uint32_t reset = BGFX_RESET_VSYNC;
  126. bgfx::init();
  127. bgfx::reset(width, height, reset);
  128. // Enable debug text.
  129. bgfx::setDebug(debug);
  130. // Set view 0 clear state.
  131. bgfx::setViewClear(0
  132. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  133. , 0x303030ff
  134. , 1.0f
  135. , 0
  136. );
  137. // Set view debug names.
  138. bgfx::setViewName(0, "Skybox");
  139. bgfx::setViewName(1, "Mesh");
  140. bgfx::setViewName(2, "Luminance");
  141. bgfx::setViewName(3, "Downscale luminance 0");
  142. bgfx::setViewName(4, "Downscale luminance 1");
  143. bgfx::setViewName(5, "Downscale luminance 2");
  144. bgfx::setViewName(6, "Downscale luminance 3");
  145. bgfx::setViewName(7, "Brightness");
  146. bgfx::setViewName(8, "Blur vertical");
  147. bgfx::setViewName(9, "Blur horizontal + tonemap");
  148. bgfx::TextureHandle uffizi = loadTexture("uffizi.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
  149. bgfx::ProgramHandle skyProgram = loadProgram("vs_hdr_skybox", "fs_hdr_skybox");
  150. bgfx::ProgramHandle lumProgram = loadProgram("vs_hdr_lum", "fs_hdr_lum");
  151. bgfx::ProgramHandle lumAvgProgram = loadProgram("vs_hdr_lumavg", "fs_hdr_lumavg");
  152. bgfx::ProgramHandle blurProgram = loadProgram("vs_hdr_blur", "fs_hdr_blur");
  153. bgfx::ProgramHandle brightProgram = loadProgram("vs_hdr_bright", "fs_hdr_bright");
  154. bgfx::ProgramHandle meshProgram = loadProgram("vs_hdr_mesh", "fs_hdr_mesh");
  155. bgfx::ProgramHandle tonemapProgram = loadProgram("vs_hdr_tonemap", "fs_hdr_tonemap");
  156. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f);
  157. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1i);
  158. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1i);
  159. bgfx::UniformHandle u_texLum = bgfx::createUniform("u_texLum", bgfx::UniformType::Uniform1i);
  160. bgfx::UniformHandle u_texBlur = bgfx::createUniform("u_texBlur", bgfx::UniformType::Uniform1i);
  161. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Uniform4x4fv);
  162. bgfx::UniformHandle u_tonemap = bgfx::createUniform("u_tonemap", bgfx::UniformType::Uniform4fv);
  163. bgfx::UniformHandle u_offset = bgfx::createUniform("u_offset", bgfx::UniformType::Uniform4fv, 16);
  164. Mesh* mesh = meshLoad("meshes/bunny.bin");
  165. bgfx::FrameBufferHandle fbh;
  166. bgfx::TextureHandle fbtextures[] =
  167. {
  168. bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT|BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP),
  169. bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
  170. };
  171. fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  172. bgfx::FrameBufferHandle lum[5];
  173. lum[0] = bgfx::createFrameBuffer(128, 128, bgfx::TextureFormat::BGRA8);
  174. lum[1] = bgfx::createFrameBuffer( 64, 64, bgfx::TextureFormat::BGRA8);
  175. lum[2] = bgfx::createFrameBuffer( 16, 16, bgfx::TextureFormat::BGRA8);
  176. lum[3] = bgfx::createFrameBuffer( 4, 4, bgfx::TextureFormat::BGRA8);
  177. lum[4] = bgfx::createFrameBuffer( 1, 1, bgfx::TextureFormat::BGRA8);
  178. bgfx::FrameBufferHandle bright;
  179. bright = bgfx::createFrameBuffer(width/2, height/2, bgfx::TextureFormat::BGRA8);
  180. bgfx::FrameBufferHandle blur;
  181. blur = bgfx::createFrameBuffer(width/8, height/8, bgfx::TextureFormat::BGRA8);
  182. void* data = load("font/droidsans.ttf");
  183. imguiCreate(data);
  184. free(data);
  185. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  186. s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  187. s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
  188. uint32_t oldWidth = 0;
  189. uint32_t oldHeight = 0;
  190. uint32_t oldReset = reset;
  191. float speed = 0.37f;
  192. float middleGray = 0.18f;
  193. float white = 1.1f;
  194. float threshold = 1.5f;
  195. int32_t scrollArea = 0;
  196. float time = 0.0f;
  197. entry::MouseState mouseState;
  198. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  199. {
  200. if (oldWidth != width
  201. || oldHeight != height
  202. || oldReset != reset)
  203. {
  204. // Recreate variable size render targets when resolution changes.
  205. oldWidth = width;
  206. oldHeight = height;
  207. oldReset = reset;
  208. uint32_t msaa = (reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  209. bgfx::destroyFrameBuffer(fbh);
  210. bgfx::destroyFrameBuffer(bright);
  211. bgfx::destroyFrameBuffer(blur);
  212. 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);
  213. fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY|( (msaa+1)<<BGFX_TEXTURE_RT_MSAA_SHIFT) );
  214. fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  215. bright = bgfx::createFrameBuffer(width/2, height/2, bgfx::TextureFormat::BGRA8);
  216. blur = bgfx::createFrameBuffer(width/8, height/8, bgfx::TextureFormat::BGRA8);
  217. }
  218. imguiBeginFrame(mouseState.m_mx
  219. , mouseState.m_my
  220. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  221. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  222. , 0
  223. , width
  224. , height
  225. );
  226. imguiBeginScrollArea("Settings", width - width / 5 - 10, 10, width / 5, height / 3, &scrollArea);
  227. imguiSeparatorLine();
  228. imguiSlider("Speed", speed, 0.0f, 1.0f, 0.01f);
  229. imguiSeparator();
  230. imguiSlider("Middle gray", middleGray, 0.1f, 1.0f, 0.01f);
  231. imguiSlider("White point", white, 0.1f, 2.0f, 0.01f);
  232. imguiSlider("Threshold", threshold, 0.1f, 2.0f, 0.01f);
  233. imguiEndScrollArea();
  234. imguiEndFrame();
  235. // This dummy draw call is here to make sure that view 0 is cleared
  236. // if no other draw calls are submitted to view 0.
  237. bgfx::submit(0);
  238. int64_t now = bx::getHPCounter();
  239. static int64_t last = now;
  240. const int64_t frameTime = now - last;
  241. last = now;
  242. const double freq = double(bx::getHPFrequency() );
  243. const double toMs = 1000.0/freq;
  244. time += (float)(frameTime*speed/freq);
  245. bgfx::setUniform(u_time, &time);
  246. // Use debug font to print information about this example.
  247. bgfx::dbgTextClear();
  248. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/09-hdr");
  249. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Using multiple views and frame buffers.");
  250. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  251. // Set views.
  252. for (uint32_t ii = 0; ii < 6; ++ii)
  253. {
  254. bgfx::setViewRect(ii, 0, 0, width, height);
  255. }
  256. bgfx::setViewFrameBuffer(0, fbh);
  257. bgfx::setViewFrameBuffer(1, fbh);
  258. bgfx::setViewRect(2, 0, 0, 128, 128);
  259. bgfx::setViewFrameBuffer(2, lum[0]);
  260. bgfx::setViewRect(3, 0, 0, 64, 64);
  261. bgfx::setViewFrameBuffer(3, lum[1]);
  262. bgfx::setViewRect(4, 0, 0, 16, 16);
  263. bgfx::setViewFrameBuffer(4, lum[2]);
  264. bgfx::setViewRect(5, 0, 0, 4, 4);
  265. bgfx::setViewFrameBuffer(5, lum[3]);
  266. bgfx::setViewRect(6, 0, 0, 1, 1);
  267. bgfx::setViewFrameBuffer(6, lum[4]);
  268. bgfx::setViewRect(7, 0, 0, width/2, height/2);
  269. bgfx::setViewFrameBuffer(7, bright);
  270. bgfx::setViewRect(8, 0, 0, width/8, height/8);
  271. bgfx::setViewFrameBuffer(8, blur);
  272. bgfx::setViewRect(9, 0, 0, width, height);
  273. float view[16];
  274. float proj[16];
  275. bx::mtxIdentity(view);
  276. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  277. // Set view and projection matrix for view 0.
  278. for (uint32_t ii = 0; ii < 10; ++ii)
  279. {
  280. bgfx::setViewTransform(ii, view, proj);
  281. }
  282. float at[3] = { 0.0f, 1.0f, 0.0f };
  283. float eye[3] = { 0.0f, 1.0f, -2.5f };
  284. float mtx[16];
  285. bx::mtxRotateXY(mtx
  286. , 0.0f
  287. , time
  288. );
  289. float temp[4];
  290. bx::vec3MulMtx(temp, eye, mtx);
  291. bx::mtxLookAt(view, temp, at);
  292. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  293. // Set view and projection matrix for view 1.
  294. bgfx::setViewTransform(1, view, proj);
  295. bgfx::setUniform(u_mtx, mtx);
  296. // Render skybox into view 0.
  297. bgfx::setTexture(0, u_texCube, uffizi);
  298. bgfx::setProgram(skyProgram);
  299. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  300. screenSpaceQuad( (float)width, (float)height, true);
  301. bgfx::submit(0);
  302. // Render mesh into view 1
  303. bgfx::setTexture(0, u_texCube, uffizi);
  304. meshSubmit(mesh, 1, meshProgram, NULL);
  305. // Calculate luminance.
  306. setOffsets2x2Lum(u_offset, 128, 128);
  307. bgfx::setTexture(0, u_texColor, fbtextures[0]);
  308. bgfx::setProgram(lumProgram);
  309. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  310. screenSpaceQuad(128.0f, 128.0f, s_originBottomLeft);
  311. bgfx::submit(2);
  312. // Downscale luminance 0.
  313. setOffsets4x4Lum(u_offset, 128, 128);
  314. bgfx::setTexture(0, u_texColor, lum[0]);
  315. bgfx::setProgram(lumAvgProgram);
  316. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  317. screenSpaceQuad(64.0f, 64.0f, s_originBottomLeft);
  318. bgfx::submit(3);
  319. // Downscale luminance 1.
  320. setOffsets4x4Lum(u_offset, 64, 64);
  321. bgfx::setTexture(0, u_texColor, lum[1]);
  322. bgfx::setProgram(lumAvgProgram);
  323. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  324. screenSpaceQuad(16.0f, 16.0f, s_originBottomLeft);
  325. bgfx::submit(4);
  326. // Downscale luminance 2.
  327. setOffsets4x4Lum(u_offset, 16, 16);
  328. bgfx::setTexture(0, u_texColor, lum[2]);
  329. bgfx::setProgram(lumAvgProgram);
  330. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  331. screenSpaceQuad(4.0f, 4.0f, s_originBottomLeft);
  332. bgfx::submit(5);
  333. // Downscale luminance 3.
  334. setOffsets4x4Lum(u_offset, 4, 4);
  335. bgfx::setTexture(0, u_texColor, lum[3]);
  336. bgfx::setProgram(lumAvgProgram);
  337. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  338. screenSpaceQuad(1.0f, 1.0f, s_originBottomLeft);
  339. bgfx::submit(6);
  340. float tonemap[4] = { middleGray, square(white), threshold, 0.0f };
  341. bgfx::setUniform(u_tonemap, tonemap);
  342. // Bright pass threshold is tonemap[3].
  343. setOffsets4x4Lum(u_offset, width/2, height/2);
  344. bgfx::setTexture(0, u_texColor, fbtextures[0]);
  345. bgfx::setTexture(1, u_texLum, lum[4]);
  346. bgfx::setProgram(brightProgram);
  347. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  348. screenSpaceQuad( (float)width/2.0f, (float)height/2.0f, s_originBottomLeft);
  349. bgfx::submit(7);
  350. // Blur bright pass vertically.
  351. bgfx::setTexture(0, u_texColor, bright);
  352. bgfx::setProgram(blurProgram);
  353. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  354. screenSpaceQuad( (float)width/8.0f, (float)height/8.0f, s_originBottomLeft);
  355. bgfx::submit(8);
  356. // Blur bright pass horizontally, do tonemaping and combine.
  357. bgfx::setTexture(0, u_texColor, fbtextures[0]);
  358. bgfx::setTexture(1, u_texLum, lum[4]);
  359. bgfx::setTexture(2, u_texBlur, blur);
  360. bgfx::setProgram(tonemapProgram);
  361. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  362. screenSpaceQuad( (float)width, (float)height, s_originBottomLeft);
  363. bgfx::submit(9);
  364. // Advance to next frame. Rendering thread will be kicked to
  365. // process submitted rendering primitives.
  366. bgfx::frame();
  367. }
  368. // Cleanup.
  369. imguiDestroy();
  370. meshUnload(mesh);
  371. for (uint32_t ii = 0; ii < BX_COUNTOF(lum); ++ii)
  372. {
  373. bgfx::destroyFrameBuffer(lum[ii]);
  374. }
  375. bgfx::destroyFrameBuffer(bright);
  376. bgfx::destroyFrameBuffer(blur);
  377. bgfx::destroyFrameBuffer(fbh);
  378. bgfx::destroyProgram(meshProgram);
  379. bgfx::destroyProgram(skyProgram);
  380. bgfx::destroyProgram(tonemapProgram);
  381. bgfx::destroyProgram(lumProgram);
  382. bgfx::destroyProgram(lumAvgProgram);
  383. bgfx::destroyProgram(blurProgram);
  384. bgfx::destroyProgram(brightProgram);
  385. bgfx::destroyTexture(uffizi);
  386. bgfx::destroyUniform(u_time);
  387. bgfx::destroyUniform(u_texCube);
  388. bgfx::destroyUniform(u_texColor);
  389. bgfx::destroyUniform(u_texLum);
  390. bgfx::destroyUniform(u_texBlur);
  391. bgfx::destroyUniform(u_mtx);
  392. bgfx::destroyUniform(u_tonemap);
  393. bgfx::destroyUniform(u_offset);
  394. // Shutdown bgfx.
  395. bgfx::shutdown();
  396. return 0;
  397. }