oit.cpp 13 KB

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