oit.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright 2011-2025 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. namespace
  9. {
  10. struct PosColorVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_abgr;
  16. static void init()
  17. {
  18. ms_layout
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  22. .end();
  23. }
  24. static bgfx::VertexLayout ms_layout;
  25. };
  26. bgfx::VertexLayout PosColorVertex::ms_layout;
  27. struct PosColorTexCoord0Vertex
  28. {
  29. float m_x;
  30. float m_y;
  31. float m_z;
  32. uint32_t m_rgba;
  33. float m_u;
  34. float m_v;
  35. static void init()
  36. {
  37. ms_layout
  38. .begin()
  39. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  40. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  41. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  42. .end();
  43. }
  44. static bgfx::VertexLayout ms_layout;
  45. };
  46. bgfx::VertexLayout PosColorTexCoord0Vertex::ms_layout;
  47. static PosColorVertex s_cubeVertices[8] =
  48. {
  49. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  50. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  51. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  52. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  53. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  54. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  55. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  56. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  57. };
  58. static const uint16_t s_cubeIndices[36] =
  59. {
  60. 0, 1, 2, // 0
  61. 1, 3, 2,
  62. 4, 6, 5, // 2
  63. 5, 6, 7,
  64. 0, 2, 4, // 4
  65. 4, 2, 6,
  66. 1, 5, 3, // 6
  67. 5, 7, 3,
  68. 0, 4, 1, // 8
  69. 4, 5, 1,
  70. 2, 3, 6, // 10
  71. 6, 3, 7,
  72. };
  73. static float s_texelHalf = 0.0f;
  74. static bool s_flipV = false;
  75. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  76. {
  77. bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
  78. }
  79. void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
  80. {
  81. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_layout) )
  82. {
  83. bgfx::TransientVertexBuffer vb;
  84. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_layout);
  85. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  86. const float zz = 0.0f;
  87. const float minx = -_width;
  88. const float maxx = _width;
  89. const float miny = 0.0f;
  90. const float maxy = _height*2.0f;
  91. const float texelHalfW = s_texelHalf/_textureWidth;
  92. const float texelHalfH = s_texelHalf/_textureHeight;
  93. const float minu = -1.0f + texelHalfW;
  94. const float maxu = 1.0f + texelHalfW;
  95. float minv = texelHalfH;
  96. float maxv = 2.0f + texelHalfH;
  97. if (_originBottomLeft)
  98. {
  99. float tmp = minv;
  100. minv = maxv;
  101. maxv = tmp;
  102. minv -= 1.0f;
  103. maxv -= 1.0f;
  104. }
  105. vertex[0].m_x = minx;
  106. vertex[0].m_y = miny;
  107. vertex[0].m_z = zz;
  108. vertex[0].m_rgba = 0xffffffff;
  109. vertex[0].m_u = minu;
  110. vertex[0].m_v = minv;
  111. vertex[1].m_x = maxx;
  112. vertex[1].m_y = miny;
  113. vertex[1].m_z = zz;
  114. vertex[1].m_rgba = 0xffffffff;
  115. vertex[1].m_u = maxu;
  116. vertex[1].m_v = minv;
  117. vertex[2].m_x = maxx;
  118. vertex[2].m_y = maxy;
  119. vertex[2].m_z = zz;
  120. vertex[2].m_rgba = 0xffffffff;
  121. vertex[2].m_u = maxu;
  122. vertex[2].m_v = maxv;
  123. bgfx::setVertexBuffer(0, &vb);
  124. }
  125. }
  126. class ExampleOIT : public entry::AppI
  127. {
  128. public:
  129. ExampleOIT(const char* _name, const char* _description, const char* _url)
  130. : entry::AppI(_name, _description, _url)
  131. {
  132. }
  133. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  134. {
  135. Args args(_argc, _argv);
  136. m_width = _width;
  137. m_height = _height;
  138. m_debug = BGFX_DEBUG_NONE;
  139. m_reset = BGFX_RESET_VSYNC;
  140. bgfx::Init init;
  141. init.type = args.m_type;
  142. init.vendorId = args.m_pciId;
  143. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  144. init.platformData.ndt = entry::getNativeDisplayHandle();
  145. init.platformData.type = entry::getNativeWindowHandleType();
  146. init.resolution.width = m_width;
  147. init.resolution.height = m_height;
  148. init.resolution.reset = m_reset;
  149. bgfx::init(init);
  150. // Enable debug text.
  151. bgfx::setDebug(m_debug);
  152. // Create vertex stream declaration.
  153. PosColorVertex::init();
  154. PosColorTexCoord0Vertex::init();
  155. // Get renderer capabilities info.
  156. const bgfx::Caps* caps = bgfx::getCaps();
  157. // Setup root path for binary shaders. Shader binaries are different
  158. // for each renderer.
  159. switch (caps->rendererType)
  160. {
  161. default:
  162. break;
  163. case bgfx::RendererType::OpenGL:
  164. case bgfx::RendererType::OpenGLES:
  165. s_flipV = true;
  166. break;
  167. }
  168. // Imgui.
  169. imguiCreate();
  170. // Create static vertex buffer.
  171. m_vbh = bgfx::createVertexBuffer(
  172. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  173. , PosColorVertex::ms_layout
  174. );
  175. // Create static index buffer.
  176. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  177. // Create texture sampler uniforms.
  178. s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Sampler);
  179. s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Sampler);
  180. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
  181. m_blend = loadProgram("vs_oit", "fs_oit" );
  182. m_wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" );
  183. m_wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" );
  184. m_wbPass = loadProgram("vs_oit", "fs_oit_wb" );
  185. m_wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" );
  186. m_fbtextures[0].idx = bgfx::kInvalidHandle;
  187. m_fbtextures[1].idx = bgfx::kInvalidHandle;
  188. m_fbh.idx = bgfx::kInvalidHandle;
  189. m_mode = 1;
  190. m_frontToBack = true;
  191. m_fadeInOut = false;
  192. m_oldWidth = 0;
  193. m_oldHeight = 0;
  194. m_oldReset = m_reset;
  195. m_mrtSupported = true
  196. && 2 <= caps->limits.maxFBAttachments
  197. && bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT)
  198. && bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT)
  199. ;
  200. m_timeOffset = bx::getHPCounter();
  201. }
  202. int shutdown() override
  203. {
  204. // Cleanup.
  205. imguiDestroy();
  206. if (bgfx::isValid(m_fbh) )
  207. {
  208. bgfx::destroy(m_fbh);
  209. }
  210. bgfx::destroy(m_ibh);
  211. bgfx::destroy(m_vbh);
  212. bgfx::destroy(m_blend);
  213. bgfx::destroy(m_wbSeparatePass);
  214. bgfx::destroy(m_wbSeparateBlit);
  215. bgfx::destroy(m_wbPass);
  216. bgfx::destroy(m_wbBlit);
  217. bgfx::destroy(s_texColor0);
  218. bgfx::destroy(s_texColor1);
  219. bgfx::destroy(u_color);
  220. // Shutdown bgfx.
  221. bgfx::shutdown();
  222. return 0;
  223. }
  224. bool update() override
  225. {
  226. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  227. {
  228. imguiBeginFrame(m_mouseState.m_mx
  229. , m_mouseState.m_my
  230. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  231. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  232. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  233. , m_mouseState.m_mz
  234. , uint16_t(m_width)
  235. , uint16_t(m_height)
  236. );
  237. showExampleDialog(this
  238. , !m_mrtSupported
  239. ? "MRT or frame buffer texture format are not supported."
  240. : NULL
  241. );
  242. if (m_mrtSupported)
  243. {
  244. if (m_oldWidth != m_width
  245. || m_oldHeight != m_height
  246. || m_oldReset != m_reset
  247. || !bgfx::isValid(m_fbh) )
  248. {
  249. // Recreate variable size render targets when resolution changes.
  250. m_oldWidth = m_width;
  251. m_oldHeight = m_height;
  252. m_oldReset = m_reset;
  253. if (bgfx::isValid(m_fbh) )
  254. {
  255. bgfx::destroy(m_fbh);
  256. }
  257. m_fbtextures[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT);
  258. m_fbtextures[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT);
  259. m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true);
  260. }
  261. ImGui::SetNextWindowPos(
  262. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  263. , ImGuiCond_FirstUseEver
  264. );
  265. ImGui::SetNextWindowSize(
  266. ImVec2(m_width / 5.0f, m_height / 3.0f)
  267. , ImGuiCond_FirstUseEver
  268. );
  269. ImGui::Begin("Settings"
  270. , NULL
  271. , 0
  272. );
  273. ImGui::Separator();
  274. ImGui::Text("Blend mode:");
  275. ImGui::RadioButton("None", &m_mode, 0);
  276. ImGui::RadioButton("Separate", &m_mode, 1);
  277. ImGui::RadioButton("MRT Independent", &m_mode, 2);
  278. ImGui::Separator();
  279. ImGui::Checkbox("Front to back", &m_frontToBack);
  280. ImGui::Checkbox("Fade in/out", &m_fadeInOut);
  281. ImGui::End();
  282. // Set view 0 default viewport.
  283. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  284. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  285. int64_t now = bx::getHPCounter();
  286. const double freq = double(bx::getHPFrequency() );
  287. float time = (float)( (now-m_timeOffset)/freq);
  288. // Reference(s):
  289. // - Weighted, Blended Order-Independent Transparency
  290. // https://web.archive.org/save/http://jcgt.org/published/0002/02/09/
  291. // https://web.archive.org/web/20181126040455/http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html
  292. //
  293. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  294. const bx::Vec3 eye = { 0.0f, 0.0f, -7.0f };
  295. float view[16];
  296. float proj[16];
  297. // Set view and projection matrix for view 0.
  298. bx::mtxLookAt(view, eye, at);
  299. mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  300. bgfx::setViewTransform(0, view, proj);
  301. // Set palette color for index 0
  302. bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f);
  303. // Set palette color for index 1
  304. bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f);
  305. bgfx::setViewClear(0
  306. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  307. , 1.0f // Depth
  308. , 0 // Stencil
  309. , 0 // FB texture 0, color palette 0
  310. , 1 == m_mode ? 1 : 0 // FB texture 1, color palette 1
  311. );
  312. bgfx::setViewClear(1
  313. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  314. , 1.0f // Depth
  315. , 0 // Stencil
  316. , 0 // Color palette 0
  317. );
  318. bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
  319. bgfx::setViewFrameBuffer(0, 0 == m_mode ? invalid : m_fbh);
  320. // Set view and projection matrix for view 1.
  321. bx::mtxIdentity(view);
  322. const bgfx::Caps* caps = bgfx::getCaps();
  323. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
  324. bgfx::setViewTransform(1, view, proj);
  325. for (uint32_t depth = 0; depth < 3; ++depth)
  326. {
  327. uint32_t zz = m_frontToBack ? 2-depth : depth;
  328. for (uint32_t yy = 0; yy < 3; ++yy)
  329. {
  330. for (uint32_t xx = 0; xx < 3; ++xx)
  331. {
  332. float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f };
  333. if (m_fadeInOut
  334. && zz == 1)
  335. {
  336. color[3] = bx::sin(time*3.0f)*0.49f+0.5f;
  337. }
  338. bgfx::setUniform(u_color, color);
  339. BX_UNUSED(time);
  340. float mtx[16];
  341. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  342. //mtxIdentity(mtx);
  343. mtx[12] = -2.5f + float(xx)*2.5f;
  344. mtx[13] = -2.5f + float(yy)*2.5f;
  345. mtx[14] = -2.5f + float(zz)*2.5f;
  346. // Set transform for draw call.
  347. bgfx::setTransform(mtx);
  348. // Set vertex and index buffer.
  349. bgfx::setVertexBuffer(0, m_vbh);
  350. bgfx::setIndexBuffer(m_ibh);
  351. const uint64_t state = 0
  352. | BGFX_STATE_CULL_CW
  353. | BGFX_STATE_WRITE_RGB
  354. | BGFX_STATE_WRITE_A
  355. | BGFX_STATE_DEPTH_TEST_LESS
  356. | BGFX_STATE_MSAA
  357. ;
  358. const uint64_t stateNoDepth = 0
  359. | BGFX_STATE_CULL_CW
  360. | BGFX_STATE_WRITE_RGB
  361. | BGFX_STATE_WRITE_A
  362. | BGFX_STATE_DEPTH_TEST_ALWAYS
  363. | BGFX_STATE_MSAA
  364. ;
  365. bgfx::ProgramHandle program = BGFX_INVALID_HANDLE;
  366. switch (m_mode)
  367. {
  368. case 0:
  369. // Set vertex and fragment shaders.
  370. program = m_blend;
  371. // Set render states.
  372. bgfx::setState(state
  373. | BGFX_STATE_BLEND_ALPHA
  374. );
  375. break;
  376. case 1:
  377. // Set vertex and fragment shaders.
  378. program = m_wbSeparatePass;
  379. // Set render states.
  380. bgfx::setState(stateNoDepth
  381. | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  382. );
  383. break;
  384. default:
  385. // Set vertex and fragment shaders.
  386. program = m_wbPass;
  387. // Set render states.
  388. bgfx::setState(stateNoDepth
  389. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
  390. | BGFX_STATE_BLEND_INDEPENDENT
  391. , 0
  392. | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR)
  393. );
  394. break;
  395. }
  396. // Submit primitive for rendering to view 0.
  397. bgfx::submit(0, program);
  398. }
  399. }
  400. }
  401. if (0 != m_mode)
  402. {
  403. bgfx::setTexture(0, s_texColor0, m_fbtextures[0]);
  404. bgfx::setTexture(1, s_texColor1, m_fbtextures[1]);
  405. bgfx::setState(0
  406. | BGFX_STATE_WRITE_RGB
  407. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA)
  408. );
  409. screenSpaceQuad( (float)m_width, (float)m_height, s_flipV);
  410. bgfx::submit(1
  411. , 1 == m_mode ? m_wbSeparateBlit : m_wbBlit
  412. );
  413. }
  414. }
  415. imguiEndFrame();
  416. // Advance to next frame. Rendering thread will be kicked to
  417. // process submitted rendering primitives.
  418. bgfx::frame();
  419. return true;
  420. }
  421. return false;
  422. }
  423. uint32_t m_width;
  424. uint32_t m_height;
  425. uint32_t m_debug;
  426. uint32_t m_reset;
  427. int32_t m_mode;
  428. bool m_frontToBack;
  429. bool m_fadeInOut;
  430. bool m_mrtSupported;
  431. uint32_t m_oldWidth;
  432. uint32_t m_oldHeight;
  433. uint32_t m_oldReset;
  434. entry::MouseState m_mouseState;
  435. int64_t m_timeOffset;
  436. bgfx::VertexBufferHandle m_vbh;
  437. bgfx::IndexBufferHandle m_ibh;
  438. bgfx::UniformHandle s_texColor0;
  439. bgfx::UniformHandle s_texColor1;
  440. bgfx::UniformHandle u_color;
  441. bgfx::ProgramHandle m_blend;
  442. bgfx::ProgramHandle m_wbSeparatePass;
  443. bgfx::ProgramHandle m_wbSeparateBlit;
  444. bgfx::ProgramHandle m_wbPass;
  445. bgfx::ProgramHandle m_wbBlit;
  446. bgfx::TextureHandle m_fbtextures[2];
  447. bgfx::FrameBufferHandle m_fbh;
  448. };
  449. } // namespace
  450. ENTRY_IMPLEMENT_MAIN(
  451. ExampleOIT
  452. , "19-oit"
  453. , "Weighted, Blended Order Independent Transparency."
  454. , "https://bkaradzic.github.io/bgfx/examples.html#oit"
  455. );