bloom.cpp 16 KB

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