bloom.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright 2018 Eric Arnebäck. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. /*
  6. * Reference(s):
  7. * - Next Generation Post Processing in Call of Duty: Advanced Warfare
  8. * https://web.archive.org/web/20180920045230/http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  9. */
  10. #include "common.h"
  11. #include "bgfx_utils.h"
  12. #include "imgui/imgui.h"
  13. #include "camera.h"
  14. #include "bounds.h"
  15. namespace
  16. {
  17. // pass that render the geometry of the boxes.
  18. #define RENDER_PASS_GEOMETRY_ID 0
  19. // the first downsample pass.
  20. #define RENDER_PASS_DOWNSAMPLE0_ID 1
  21. // the first upsample pass.
  22. #define RENDER_PASS_UPSAMPLE0_ID ( (TEX_CHAIN_LEN-1) + 1)
  23. // the final pass the combines the bloom with the g-buffer.
  24. #define RENDER_PASS_COMBINE_ID ( (TEX_CHAIN_LEN-1) + 1 + (TEX_CHAIN_LEN-1) )
  25. // number of downsampled and then upsampled textures(used for bloom.)
  26. #define TEX_CHAIN_LEN 5
  27. static float s_texelHalf = 0.0f;
  28. struct PosVertex
  29. {
  30. float m_x;
  31. float m_y;
  32. float m_z;
  33. static void init()
  34. {
  35. ms_decl
  36. .begin()
  37. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  38. .end();
  39. }
  40. static bgfx::VertexLayout ms_decl;
  41. };
  42. bgfx::VertexLayout PosVertex::ms_decl;
  43. struct PosTexCoord0Vertex
  44. {
  45. float m_x;
  46. float m_y;
  47. float m_z;
  48. float m_u;
  49. float m_v;
  50. static void init()
  51. {
  52. ms_decl
  53. .begin()
  54. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  55. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  56. .end();
  57. }
  58. static bgfx::VertexLayout ms_decl;
  59. };
  60. bgfx::VertexLayout PosTexCoord0Vertex::ms_decl;
  61. constexpr float cs = 0.29f;
  62. static PosVertex s_cubeVertices[24] =
  63. {
  64. {-cs, cs, cs },
  65. { cs, cs, cs },
  66. {-cs, -cs, cs },
  67. { cs, -cs, cs },
  68. {-cs, cs, -cs },
  69. { cs, cs, -cs },
  70. {-cs, -cs, -cs },
  71. { cs, -cs, -cs },
  72. {-cs, cs, cs },
  73. { cs, cs, cs },
  74. {-cs, cs, -cs },
  75. { cs, cs, -cs },
  76. {-cs, -cs, cs },
  77. { cs, -cs, cs },
  78. {-cs, -cs, -cs },
  79. { cs, -cs, -cs },
  80. { cs, -cs, cs },
  81. { cs, cs, cs },
  82. { cs, -cs, -cs },
  83. { cs, cs, -cs },
  84. {-cs, -cs, cs },
  85. {-cs, cs, cs },
  86. {-cs, -cs, -cs },
  87. {-cs, cs, -cs },
  88. };
  89. static const uint16_t s_cubeIndices[36] =
  90. {
  91. 0, 2, 1,
  92. 1, 2, 3,
  93. 4, 5, 6,
  94. 5, 7, 6,
  95. 8, 10, 9,
  96. 9, 10, 11,
  97. 12, 13, 14,
  98. 13, 15, 14,
  99. 16, 18, 17,
  100. 17, 18, 19,
  101. 20, 21, 22,
  102. 21, 23, 22,
  103. };
  104. void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  105. {
  106. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_decl) )
  107. {
  108. bgfx::TransientVertexBuffer vb;
  109. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_decl);
  110. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
  111. const float minx = -_width;
  112. const float maxx = _width;
  113. const float miny = 0.0f;
  114. const float maxy = _height*2.0f;
  115. const float texelHalfW = _texelHalf/_textureWidth;
  116. const float texelHalfH = _texelHalf/_textureHeight;
  117. const float minu = -1.0f + texelHalfW;
  118. const float maxu = 1.0f + texelHalfH;
  119. const float zz = 0.0f;
  120. float minv = texelHalfH;
  121. float maxv = 2.0f + texelHalfH;
  122. if (_originBottomLeft)
  123. {
  124. float temp = minv;
  125. minv = maxv;
  126. maxv = temp;
  127. minv -= 1.0f;
  128. maxv -= 1.0f;
  129. }
  130. vertex[0].m_x = minx;
  131. vertex[0].m_y = miny;
  132. vertex[0].m_z = zz;
  133. vertex[0].m_u = minu;
  134. vertex[0].m_v = minv;
  135. vertex[1].m_x = maxx;
  136. vertex[1].m_y = miny;
  137. vertex[1].m_z = zz;
  138. vertex[1].m_u = maxu;
  139. vertex[1].m_v = minv;
  140. vertex[2].m_x = maxx;
  141. vertex[2].m_y = maxy;
  142. vertex[2].m_z = zz;
  143. vertex[2].m_u = maxu;
  144. vertex[2].m_v = maxv;
  145. bgfx::setVertexBuffer(0, &vb);
  146. }
  147. }
  148. class ExampleDeferred : public entry::AppI
  149. {
  150. public:
  151. ExampleDeferred(const char* _name, const char* _description)
  152. : entry::AppI(_name, _description)
  153. {
  154. }
  155. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  156. {
  157. Args args(_argc, _argv);
  158. m_width = _width;
  159. m_height = _height;
  160. m_debug = BGFX_DEBUG_TEXT;
  161. m_reset = BGFX_RESET_VSYNC;
  162. bgfx::Init init;
  163. init.type = args.m_type;
  164. init.vendorId = args.m_pciId;
  165. init.resolution.width = m_width;
  166. init.resolution.height = m_height;
  167. init.resolution.reset = m_reset;
  168. bgfx::init(init);
  169. // Enable m_debug text.
  170. bgfx::setDebug(m_debug);
  171. // Set palette color for index 0
  172. bgfx::setPaletteColor(0, UINT32_C(0x00000000) );
  173. // Set geometry pass view clear state.
  174. bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID
  175. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  176. , 1.0f
  177. , 0
  178. , 0
  179. , 0
  180. );
  181. // we need to clear the textures in the chain, before downsampling into them.
  182. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  183. {
  184. bgfx::setViewClear(RENDER_PASS_DOWNSAMPLE0_ID + ii
  185. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  186. , 1.0f
  187. , 0
  188. , 0
  189. );
  190. }
  191. // Create vertex stream declaration.
  192. PosVertex::init();
  193. PosTexCoord0Vertex::init();
  194. // Create static vertex buffer.
  195. m_vbh = bgfx::createVertexBuffer(
  196. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  197. , PosVertex::ms_decl
  198. );
  199. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  200. s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
  201. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
  202. s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler);
  203. s_light = bgfx::createUniform("s_light", bgfx::UniformType::Sampler);
  204. u_pixelSize = bgfx::createUniform("u_pixelSize", bgfx::UniformType::Vec4);
  205. u_intensity = bgfx::createUniform("u_intensity", bgfx::UniformType::Vec4);
  206. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
  207. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  208. // Create program from shaders.
  209. m_geomProgram = loadProgram("vs_albedo_output", "fs_albedo_output");
  210. m_downsampleProgram = loadProgram("vs_fullscreen", "fs_downsample");
  211. m_upsampleProgram = loadProgram("vs_fullscreen", "fs_upsample");
  212. m_combineProgram = loadProgram("vs_fullscreen", "fs_bloom_combine");
  213. m_gbuffer = BGFX_INVALID_HANDLE;
  214. for (int ii = 0; ii < TEX_CHAIN_LEN; ++ii)
  215. {
  216. m_texChainFb[ii] = BGFX_INVALID_HANDLE;
  217. }
  218. // Imgui.
  219. imguiCreate();
  220. m_timeOffset = bx::getHPCounter();
  221. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  222. s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  223. // Get renderer capabilities info.
  224. m_caps = bgfx::getCaps();
  225. m_oldWidth = 0;
  226. m_oldHeight = 0;
  227. m_oldReset = m_reset;
  228. m_scrollArea = 0;
  229. cameraCreate();
  230. cameraSetPosition({ 0.0f, 0.0f, -15.0f });
  231. cameraSetVerticalAngle(0.0f);
  232. }
  233. virtual int shutdown() override
  234. {
  235. // Cleanup.
  236. cameraDestroy();
  237. imguiDestroy();
  238. if (bgfx::isValid(m_gbuffer) )
  239. {
  240. bgfx::destroy(m_gbuffer);
  241. }
  242. for (int ii = 0; ii < TEX_CHAIN_LEN; ++ii)
  243. {
  244. bgfx::destroy(m_texChainFb[ii]);
  245. }
  246. bgfx::destroy(m_ibh);
  247. bgfx::destroy(m_vbh);
  248. bgfx::destroy(m_geomProgram);
  249. bgfx::destroy(m_downsampleProgram);
  250. bgfx::destroy(m_upsampleProgram);
  251. bgfx::destroy(m_combineProgram);
  252. bgfx::destroy(s_albedo);
  253. bgfx::destroy(s_tex);
  254. bgfx::destroy(s_depth);
  255. bgfx::destroy(s_light);
  256. bgfx::destroy(u_mtx);
  257. bgfx::destroy(u_pixelSize);
  258. bgfx::destroy(u_intensity);
  259. bgfx::destroy(u_color);
  260. // Shutdown bgfx.
  261. bgfx::shutdown();
  262. return 0;
  263. }
  264. bool update() override
  265. {
  266. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  267. {
  268. imguiBeginFrame(
  269. 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. int64_t now = bx::getHPCounter();
  280. static int64_t last = now;
  281. const int64_t frameTime = now - last;
  282. last = now;
  283. const double freq = double(bx::getHPFrequency() );
  284. const float deltaTime = float(frameTime/freq);
  285. float time = (float)( (now-m_timeOffset)/freq);
  286. if (2 > m_caps->limits.maxFBAttachments)
  287. {
  288. // When multiple render targets (MRT) is not supported by GPU,
  289. // implement alternative code path that doesn't use MRT.
  290. bool blink = uint32_t(time*3.0f)&1;
  291. bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " MRT not supported by GPU. ");
  292. // Set view 0 default viewport.
  293. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  294. // This dummy draw call is here to make sure that view 0 is cleared
  295. // if no other draw calls are submitted to view 0.
  296. bgfx::touch(0);
  297. }
  298. else
  299. {
  300. if (m_oldWidth != m_width
  301. || m_oldHeight != m_height
  302. || m_oldReset != m_reset
  303. || !bgfx::isValid(m_gbuffer) )
  304. {
  305. // Recreate variable size render targets when resolution changes.
  306. m_oldWidth = m_width;
  307. m_oldHeight = m_height;
  308. m_oldReset = m_reset;
  309. if (bgfx::isValid(m_gbuffer) )
  310. {
  311. bgfx::destroy(m_gbuffer);
  312. }
  313. const uint64_t tsFlags = 0
  314. | BGFX_TEXTURE_RT
  315. | BGFX_SAMPLER_U_CLAMP
  316. | BGFX_SAMPLER_V_CLAMP
  317. ;
  318. for (int ii = 0; ii < TEX_CHAIN_LEN; ++ii)
  319. {
  320. if (bgfx::isValid(m_texChainFb[ii]) )
  321. {
  322. bgfx::destroy(m_texChainFb[ii]);
  323. }
  324. const float dim = float(1 << ii);
  325. m_texChainFb[ii] = bgfx::createFrameBuffer(
  326. (uint16_t)(m_width / dim)
  327. , (uint16_t)(m_height / dim)
  328. , bgfx::TextureFormat::RGBA32F
  329. , tsFlags
  330. );
  331. }
  332. bgfx::TextureHandle gbufferTex[] =
  333. {
  334. bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::RGBA32F, tsFlags),
  335. bgfx::getTexture(m_texChainFb[0]),
  336. bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D24S8, tsFlags),
  337. };
  338. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(gbufferTex), gbufferTex, true);
  339. }
  340. ImGui::SetNextWindowPos(
  341. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  342. , ImGuiCond_FirstUseEver
  343. );
  344. ImGui::SetNextWindowSize(
  345. ImVec2(m_width / 5.0f, m_height / 6.0f)
  346. , ImGuiCond_FirstUseEver
  347. );
  348. ImGui::Begin("Settings"
  349. , NULL
  350. , 0
  351. );
  352. ImGui::SliderFloat("intensity", &m_intensity, 0.0f, 3.0f);
  353. ImGui::End();
  354. // Update camera.
  355. cameraUpdate(deltaTime, m_mouseState);
  356. float view[16];
  357. cameraGetViewMtx(view);
  358. float proj[16];
  359. // Setup views
  360. {
  361. bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  362. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  363. {
  364. const float dim = float(1 << (ii + 1) );
  365. bgfx::setViewRect(RENDER_PASS_DOWNSAMPLE0_ID + ii, 0, 0
  366. , uint16_t(m_width / dim)
  367. , uint16_t(m_height / dim)
  368. );
  369. }
  370. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  371. {
  372. const float dim = float(1 << (TEX_CHAIN_LEN - ii - 2) );
  373. bgfx::setViewRect(RENDER_PASS_UPSAMPLE0_ID + ii, 0, 0
  374. , uint16_t(m_width / dim)
  375. , uint16_t(m_height / dim)
  376. );
  377. }
  378. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, m_caps->homogeneousDepth);
  379. bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
  380. bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
  381. bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, uint16_t(m_width), uint16_t(m_height));
  382. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, m_caps->homogeneousDepth);
  383. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  384. {
  385. bgfx::setViewTransform(RENDER_PASS_DOWNSAMPLE0_ID + ii, NULL, proj);
  386. bgfx::setViewFrameBuffer(RENDER_PASS_DOWNSAMPLE0_ID + ii, m_texChainFb[ii+1]);
  387. }
  388. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  389. {
  390. bgfx::setViewTransform(RENDER_PASS_UPSAMPLE0_ID + ii, NULL, proj);
  391. bgfx::setViewFrameBuffer(RENDER_PASS_UPSAMPLE0_ID + ii, m_texChainFb[TEX_CHAIN_LEN - ii - 2]);
  392. }
  393. bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
  394. }
  395. const uint32_t kNum = 9;
  396. const int kNumColors = 5;
  397. const float color[4*kNumColors] =
  398. { // Reference(s):
  399. // - Palette
  400. // https://web.archive.org/web/20180219034657/http://www.colourlovers.com/palette/3647908/RGB_Ice_Cream
  401. 0.847f*0.2f, 0.365f*0.2f, 0.408f*0.2f, 1.0f,
  402. 0.976f*0.2f, 0.827f*0.2f, 0.533f*0.2f, 1.0f,
  403. 0.533f*0.2f, 0.867f*0.2f, 0.741f*0.2f, 1.0f,
  404. 0.894f*0.2f, 0.620f*0.2f, 0.416f*0.2f, 1.0f,
  405. 0.584f*0.2f, 0.788f*0.2f, 0.882f*0.2f, 1.0f,
  406. };
  407. // Render a whole bunch of colored cubes to the g-buffer.
  408. for (uint32_t xx = 0; xx < kNum; ++xx)
  409. {
  410. bgfx::setUniform(u_color, &color[4 * (xx % kNumColors)]);
  411. float mtx[16];
  412. bx::mtxIdentity(mtx);
  413. const float tt = (float)xx / (float)kNum + 0.07f * time;
  414. const float rr = bx::sin(0.47f * time * bx::kPi2) + 1.4f;
  415. mtx[12] = bx::sin(tt * bx::kPi2)*rr;
  416. mtx[13] = bx::cos(tt * bx::kPi2)*rr;
  417. mtx[14] = 0.2f * (float)xx / (float)kNum;
  418. // Set transform for draw call.
  419. bgfx::setTransform(mtx);
  420. // Set vertex and index buffer.
  421. bgfx::setVertexBuffer(0, m_vbh);
  422. bgfx::setIndexBuffer(m_ibh);
  423. // Set render states.
  424. bgfx::setState(0
  425. | BGFX_STATE_WRITE_RGB
  426. | BGFX_STATE_WRITE_A
  427. | BGFX_STATE_WRITE_Z
  428. | BGFX_STATE_DEPTH_TEST_LESS
  429. | BGFX_STATE_MSAA
  430. );
  431. // Submit primitive for rendering to view 0.
  432. bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
  433. }
  434. // Now downsample.
  435. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN-1; ++ii)
  436. {
  437. const float dim = float(1 << (ii + 1) );
  438. const float pixelSize[4] =
  439. {
  440. 1.0f / (m_width / dim),
  441. 1.0f / (m_height / dim),
  442. 0.0f,
  443. 0.0f,
  444. };
  445. bgfx::setUniform(u_pixelSize, pixelSize);
  446. bgfx::setTexture(0, s_tex, bgfx::getTexture(m_texChainFb[ii]) );
  447. bgfx::setState(0
  448. | BGFX_STATE_WRITE_RGB
  449. | BGFX_STATE_WRITE_A
  450. );
  451. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
  452. bgfx::submit(RENDER_PASS_DOWNSAMPLE0_ID + ii, m_downsampleProgram);
  453. }
  454. // Now upsample.
  455. for (uint16_t ii = 0; ii < TEX_CHAIN_LEN - 1; ++ii)
  456. {
  457. const float dim = float(1 << (TEX_CHAIN_LEN - 2 - ii) );
  458. const float pixelSize[4] =
  459. {
  460. 1.0f / (float)(m_width / dim),
  461. 1.0f / (float)(m_height / dim),
  462. 0.0f,
  463. 0.0f,
  464. };
  465. const float intensity[4] = { m_intensity, 0.0f, 0.0f, 0.0f };
  466. bgfx::setUniform(u_pixelSize, pixelSize);
  467. bgfx::setUniform(u_intensity, intensity);
  468. // Combine color and light buffers.
  469. bgfx::setTexture(0, s_tex, bgfx::getTexture(m_texChainFb[TEX_CHAIN_LEN - 1 - ii]) );
  470. // As we upscale, we also sum with the previous mip level. We do this by alpha blending.
  471. bgfx::setState(0
  472. | BGFX_STATE_WRITE_RGB
  473. | BGFX_STATE_WRITE_A
  474. | BGFX_STATE_BLEND_ADD
  475. );
  476. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
  477. bgfx::submit(RENDER_PASS_UPSAMPLE0_ID + ii, m_upsampleProgram);
  478. }
  479. // Do final pass, that combines the bloom with the g-buffer.
  480. bgfx::setTexture(0, s_albedo, bgfx::getTexture(m_gbuffer, 0) );
  481. bgfx::setTexture(1, s_light, bgfx::getTexture(m_texChainFb[0]) );
  482. bgfx::setState(0
  483. | BGFX_STATE_WRITE_RGB
  484. | BGFX_STATE_WRITE_A
  485. );
  486. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
  487. bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
  488. }
  489. imguiEndFrame();
  490. // Advance to next frame. Rendering thread will be kicked to
  491. // process submitted rendering primitives.
  492. bgfx::frame();
  493. return true;
  494. }
  495. return false;
  496. }
  497. bgfx::VertexBufferHandle m_vbh;
  498. bgfx::IndexBufferHandle m_ibh;
  499. bgfx::UniformHandle s_albedo;
  500. bgfx::UniformHandle s_tex;
  501. bgfx::UniformHandle s_depth;
  502. bgfx::UniformHandle s_light;
  503. bgfx::UniformHandle u_pixelSize;
  504. bgfx::UniformHandle u_intensity;
  505. bgfx::UniformHandle u_color;
  506. bgfx::UniformHandle u_mtx;
  507. bgfx::ProgramHandle m_geomProgram;
  508. bgfx::ProgramHandle m_downsampleProgram;
  509. bgfx::ProgramHandle m_upsampleProgram;
  510. bgfx::ProgramHandle m_combineProgram;
  511. bgfx::FrameBufferHandle m_gbuffer;
  512. bgfx::FrameBufferHandle m_texChainFb[TEX_CHAIN_LEN];
  513. uint32_t m_width;
  514. uint32_t m_height;
  515. uint32_t m_debug;
  516. uint32_t m_reset;
  517. float m_intensity = 1.0f;
  518. uint32_t m_oldWidth;
  519. uint32_t m_oldHeight;
  520. uint32_t m_oldReset;
  521. int32_t m_scrollArea;
  522. entry::MouseState m_mouseState;
  523. const bgfx::Caps* m_caps;
  524. int64_t m_timeOffset;
  525. };
  526. } // namespace
  527. ENTRY_IMPLEMENT_MAIN(ExampleDeferred, "38-bloom", "Bloom.");