oit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. 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 (bgfx::checkAvailTransientVertexBuffer(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(&vb);
  122. }
  123. }
  124. int _main_(int /*_argc*/, char** /*_argv*/)
  125. {
  126. uint32_t width = 1280;
  127. uint32_t height = 720;
  128. uint32_t debug = BGFX_DEBUG_TEXT;
  129. uint32_t reset = BGFX_RESET_VSYNC;
  130. bgfx::init();
  131. bgfx::reset(width, height, reset);
  132. // Create vertex stream declaration.
  133. PosColorVertex::init();
  134. PosColorTexCoord0Vertex::init();
  135. // Enable debug text.
  136. bgfx::setDebug(debug);
  137. // Get renderer capabilities info.
  138. const bgfx::Caps* caps = bgfx::getCaps();
  139. // Setup root path for binary shaders. Shader binaries are different
  140. // for each renderer.
  141. switch (caps->rendererType)
  142. {
  143. default:
  144. break;
  145. case bgfx::RendererType::OpenGL:
  146. case bgfx::RendererType::OpenGLES:
  147. s_flipV = true;
  148. break;
  149. }
  150. // Imgui.
  151. uint32_t size;
  152. void* data = load("font/droidsans.ttf", &size);
  153. imguiCreate(data, size);
  154. free(data);
  155. const bgfx::Memory* mem;
  156. // Create static vertex buffer.
  157. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  158. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl);
  159. // Create static index buffer.
  160. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  161. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  162. // Create texture sampler uniforms.
  163. bgfx::UniformHandle u_texColor0 = bgfx::createUniform("u_texColor0", bgfx::UniformType::Uniform1iv);
  164. bgfx::UniformHandle u_texColor1 = bgfx::createUniform("u_texColor1", bgfx::UniformType::Uniform1iv);
  165. bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Uniform4fv);
  166. bgfx::ProgramHandle blend = loadProgram("vs_oit", "fs_oit" );
  167. bgfx::ProgramHandle wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" );
  168. bgfx::ProgramHandle wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" );
  169. bgfx::ProgramHandle wbPass = loadProgram("vs_oit", "fs_oit_wb" );
  170. bgfx::ProgramHandle wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" );
  171. bgfx::TextureHandle fbtextures[2] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE };
  172. bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  173. int64_t timeOffset = bx::getHPCounter();
  174. uint32_t mode = 1;
  175. int32_t scrollArea = 0;
  176. bool frontToBack = true;
  177. bool fadeInOut = false;
  178. uint32_t oldWidth = 0;
  179. uint32_t oldHeight = 0;
  180. uint32_t oldReset = reset;
  181. entry::MouseState mouseState;
  182. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  183. {
  184. if (oldWidth != width
  185. || oldHeight != height
  186. || oldReset != reset
  187. || !bgfx::isValid(fbh) )
  188. {
  189. // Recreate variable size render targets when resolution changes.
  190. oldWidth = width;
  191. oldHeight = height;
  192. oldReset = reset;
  193. if (bgfx::isValid(fbh) )
  194. {
  195. bgfx::destroyFrameBuffer(fbh);
  196. }
  197. fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT);
  198. fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT);
  199. fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  200. }
  201. imguiBeginFrame(mouseState.m_mx
  202. , mouseState.m_my
  203. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  204. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  205. , 0
  206. , width
  207. , height
  208. );
  209. imguiBeginScrollArea("Settings", width - width / 4 - 10, 10, width / 4, height / 3, &scrollArea);
  210. imguiSeparatorLine();
  211. imguiLabel("Blend mode:");
  212. mode = imguiChoose(mode
  213. , "None"
  214. , "Separate"
  215. , "MRT Independent"
  216. );
  217. imguiSeparatorLine();
  218. if (imguiCheck("Front to back", frontToBack) )
  219. {
  220. frontToBack ^= true;
  221. }
  222. if (imguiCheck("Fade in/out", fadeInOut) )
  223. {
  224. fadeInOut ^= true;
  225. }
  226. imguiEndScrollArea();
  227. imguiEndFrame();
  228. // Set view 0 default viewport.
  229. bgfx::setViewRect(0, 0, 0, width, height);
  230. bgfx::setViewRect(1, 0, 0, width, height);
  231. int64_t now = bx::getHPCounter();
  232. static int64_t last = now;
  233. const int64_t frameTime = now - last;
  234. last = now;
  235. const double freq = double(bx::getHPFrequency() );
  236. const double toMs = 1000.0/freq;
  237. float time = (float)( (now-timeOffset)/freq);
  238. // Use debug font to print information about this example.
  239. bgfx::dbgTextClear();
  240. // Reference:
  241. // Weighted, Blended Order-Independent Transparency
  242. // http://jcgt.org/published/0002/02/09/
  243. // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html
  244. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit");
  245. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency.");
  246. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  247. float at[3] = { 0.0f, 0.0f, 0.0f };
  248. float eye[3] = { 0.0f, 0.0f, -7.0f };
  249. float view[16];
  250. float proj[16];
  251. // Set view and projection matrix for view 0.
  252. bx::mtxLookAt(view, eye, at);
  253. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  254. bgfx::setViewTransform(0, view, proj);
  255. // Set clear color palette for index 0
  256. bgfx::setClearColor(0, 0.0f, 0.0f, 0.0f, 0.0f);
  257. // Set clear color palette for index 1
  258. bgfx::setClearColor(1, 1.0f, 1.0f, 1.0f, 1.0f);
  259. bgfx::setViewClear(0
  260. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  261. , 1.0f // Depth
  262. , 0 // Stencil
  263. , 0 // FB texture 0, color palette 0
  264. , 1 == mode ? 1 : 0 // FB texture 1, color palette 1
  265. );
  266. bgfx::setViewClear(1
  267. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  268. , 1.0f // Depth
  269. , 0 // Stencil
  270. , 0 // Color palette 0
  271. );
  272. bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
  273. bgfx::setViewFrameBuffer(0, 0 == mode ? invalid : fbh);
  274. // Set view and projection matrix for view 1.
  275. bx::mtxIdentity(view);
  276. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  277. bgfx::setViewTransform(1, view, proj);
  278. for (uint32_t depth = 0; depth < 3; ++depth)
  279. {
  280. uint32_t zz = frontToBack ? 2-depth : depth;
  281. for (uint32_t yy = 0; yy < 3; ++yy)
  282. {
  283. for (uint32_t xx = 0; xx < 3; ++xx)
  284. {
  285. float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f };
  286. if (fadeInOut
  287. && zz == 1)
  288. {
  289. color[3] = sinf(time*3.0f)*0.49f+0.5f;
  290. }
  291. bgfx::setUniform(u_color, color);
  292. BX_UNUSED(time);
  293. float mtx[16];
  294. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  295. //mtxIdentity(mtx);
  296. mtx[12] = -2.5f + float(xx)*2.5f;
  297. mtx[13] = -2.5f + float(yy)*2.5f;
  298. mtx[14] = -2.5f + float(zz)*2.5f;
  299. // Set transform for draw call.
  300. bgfx::setTransform(mtx);
  301. // Set vertex and index buffer.
  302. bgfx::setVertexBuffer(vbh);
  303. bgfx::setIndexBuffer(ibh);
  304. const uint64_t state = 0
  305. | BGFX_STATE_CULL_CW
  306. | BGFX_STATE_RGB_WRITE
  307. | BGFX_STATE_ALPHA_WRITE
  308. | BGFX_STATE_DEPTH_TEST_LESS
  309. | BGFX_STATE_MSAA
  310. ;
  311. switch (mode)
  312. {
  313. case 0:
  314. // Set vertex and fragment shaders.
  315. bgfx::setProgram(blend);
  316. // Set render states.
  317. bgfx::setState(state
  318. | BGFX_STATE_BLEND_ALPHA
  319. );
  320. break;
  321. case 1:
  322. // Set vertex and fragment shaders.
  323. bgfx::setProgram(wbSeparatePass);
  324. // Set render states.
  325. bgfx::setState(state
  326. | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  327. );
  328. break;
  329. default:
  330. // Set vertex and fragment shaders.
  331. bgfx::setProgram(wbPass);
  332. // Set render states.
  333. bgfx::setState(state
  334. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
  335. | BGFX_STATE_BLEND_INDEPENDENT
  336. , 0
  337. | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR)
  338. );
  339. break;
  340. }
  341. // Submit primitive for rendering to view 0.
  342. bgfx::submit(0);
  343. }
  344. }
  345. }
  346. if (0 != mode)
  347. {
  348. bgfx::setTexture(0, u_texColor0, fbtextures[0]);
  349. bgfx::setTexture(1, u_texColor1, fbtextures[1]);
  350. bgfx::setProgram(1 == mode ? wbSeparateBlit : wbBlit);
  351. bgfx::setState(0
  352. | BGFX_STATE_RGB_WRITE
  353. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA)
  354. );
  355. screenSpaceQuad( (float)width, (float)height, s_flipV);
  356. bgfx::submit(1);
  357. }
  358. // Advance to next frame. Rendering thread will be kicked to
  359. // process submitted rendering primitives.
  360. bgfx::frame();
  361. }
  362. // Cleanup.
  363. imguiDestroy();
  364. bgfx::destroyFrameBuffer(fbh);
  365. bgfx::destroyIndexBuffer(ibh);
  366. bgfx::destroyVertexBuffer(vbh);
  367. bgfx::destroyProgram(blend);
  368. bgfx::destroyProgram(wbSeparatePass);
  369. bgfx::destroyProgram(wbSeparateBlit);
  370. bgfx::destroyProgram(wbPass);
  371. bgfx::destroyProgram(wbBlit);
  372. bgfx::destroyUniform(u_texColor0);
  373. bgfx::destroyUniform(u_texColor1);
  374. bgfx::destroyUniform(u_color);
  375. // Shutdown bgfx.
  376. bgfx::shutdown();
  377. return 0;
  378. }