oit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright 2011-2014 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.begin();
  17. ms_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  18. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  19. ms_decl.end();
  20. }
  21. static bgfx::VertexDecl ms_decl;
  22. };
  23. bgfx::VertexDecl PosColorVertex::ms_decl;
  24. struct PosColorTexCoord0Vertex
  25. {
  26. float m_x;
  27. float m_y;
  28. float m_z;
  29. uint32_t m_rgba;
  30. float m_u;
  31. float m_v;
  32. static void init()
  33. {
  34. ms_decl.begin();
  35. ms_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  36. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  37. ms_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  38. ms_decl.end();
  39. }
  40. static bgfx::VertexDecl ms_decl;
  41. };
  42. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  43. static PosColorVertex s_cubeVertices[8] =
  44. {
  45. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  46. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  47. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  48. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  49. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  50. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  51. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  52. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  53. };
  54. static const uint16_t s_cubeIndices[36] =
  55. {
  56. 0, 1, 2, // 0
  57. 1, 3, 2,
  58. 4, 6, 5, // 2
  59. 5, 6, 7,
  60. 0, 2, 4, // 4
  61. 4, 2, 6,
  62. 1, 5, 3, // 6
  63. 5, 7, 3,
  64. 0, 4, 1, // 8
  65. 4, 5, 1,
  66. 2, 3, 6, // 10
  67. 6, 3, 7,
  68. };
  69. static float s_texelHalf = 0.0f;
  70. static bool s_flipV = false;
  71. void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
  72. {
  73. if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
  74. {
  75. bgfx::TransientVertexBuffer vb;
  76. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
  77. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  78. const float zz = 0.0f;
  79. const float minx = -_width;
  80. const float maxx = _width;
  81. const float miny = 0.0f;
  82. const float maxy = _height*2.0f;
  83. const float texelHalfW = s_texelHalf/_textureWidth;
  84. const float texelHalfH = s_texelHalf/_textureHeight;
  85. const float minu = -1.0f + texelHalfW;
  86. const float maxu = 1.0f + texelHalfW;
  87. float minv = texelHalfH;
  88. float maxv = 2.0f + texelHalfH;
  89. if (_originBottomLeft)
  90. {
  91. float tmp = minv;
  92. minv = maxv;
  93. maxv = tmp;
  94. minv -= 1.0f;
  95. maxv -= 1.0f;
  96. }
  97. vertex[0].m_x = minx;
  98. vertex[0].m_y = miny;
  99. vertex[0].m_z = zz;
  100. vertex[0].m_rgba = 0xffffffff;
  101. vertex[0].m_u = minu;
  102. vertex[0].m_v = minv;
  103. vertex[1].m_x = maxx;
  104. vertex[1].m_y = miny;
  105. vertex[1].m_z = zz;
  106. vertex[1].m_rgba = 0xffffffff;
  107. vertex[1].m_u = maxu;
  108. vertex[1].m_v = minv;
  109. vertex[2].m_x = maxx;
  110. vertex[2].m_y = maxy;
  111. vertex[2].m_z = zz;
  112. vertex[2].m_rgba = 0xffffffff;
  113. vertex[2].m_u = maxu;
  114. vertex[2].m_v = maxv;
  115. bgfx::setVertexBuffer(&vb);
  116. }
  117. }
  118. int _main_(int /*_argc*/, char** /*_argv*/)
  119. {
  120. // Create vertex stream declaration.
  121. PosColorVertex::init();
  122. PosColorTexCoord0Vertex::init();
  123. uint32_t width = 1280;
  124. uint32_t height = 720;
  125. uint32_t debug = BGFX_DEBUG_TEXT;
  126. uint32_t reset = BGFX_RESET_VSYNC;
  127. bgfx::init();
  128. bgfx::reset(width, height, reset);
  129. // Enable debug text.
  130. bgfx::setDebug(debug);
  131. // Get renderer capabilities info.
  132. const bgfx::Caps* caps = bgfx::getCaps();
  133. // Setup root path for binary shaders. Shader binaries are different
  134. // for each renderer.
  135. switch (caps->rendererType)
  136. {
  137. default:
  138. break;
  139. case bgfx::RendererType::OpenGL:
  140. case bgfx::RendererType::OpenGLES:
  141. s_flipV = true;
  142. break;
  143. }
  144. // Imgui.
  145. void* data = load("font/droidsans.ttf");
  146. imguiCreate(data);
  147. free(data);
  148. const bgfx::Memory* mem;
  149. // Create static vertex buffer.
  150. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  151. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl);
  152. // Create static index buffer.
  153. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  154. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  155. // Create texture sampler uniforms.
  156. bgfx::UniformHandle u_texColor0 = bgfx::createUniform("u_texColor0", bgfx::UniformType::Uniform1iv);
  157. bgfx::UniformHandle u_texColor1 = bgfx::createUniform("u_texColor1", bgfx::UniformType::Uniform1iv);
  158. bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Uniform4fv);
  159. bgfx::ProgramHandle blend = loadProgram("vs_oit", "fs_oit" );
  160. bgfx::ProgramHandle wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" );
  161. bgfx::ProgramHandle wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" );
  162. bgfx::ProgramHandle wbPass = loadProgram("vs_oit", "fs_oit_wb" );
  163. bgfx::ProgramHandle wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" );
  164. bgfx::TextureHandle fbtextures[2] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE };
  165. bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  166. int64_t timeOffset = bx::getHPCounter();
  167. uint32_t mode = 1;
  168. int32_t scrollArea = 0;
  169. bool frontToBack = true;
  170. bool fadeInOut = false;
  171. uint32_t oldWidth = 0;
  172. uint32_t oldHeight = 0;
  173. uint32_t oldReset = reset;
  174. entry::MouseState mouseState;
  175. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  176. {
  177. if (oldWidth != width
  178. || oldHeight != height
  179. || oldReset != reset
  180. || !bgfx::isValid(fbh) )
  181. {
  182. // Recreate variable size render targets when resolution changes.
  183. oldWidth = width;
  184. oldHeight = height;
  185. oldReset = reset;
  186. if (bgfx::isValid(fbh) )
  187. {
  188. bgfx::destroyFrameBuffer(fbh);
  189. }
  190. fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT),
  191. fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT),
  192. fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  193. }
  194. imguiBeginFrame(mouseState.m_mx
  195. , mouseState.m_my
  196. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  197. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  198. , 0
  199. , width
  200. , height
  201. );
  202. imguiBeginScrollArea("Settings", width - width / 4 - 10, 10, width / 4, height / 3, &scrollArea);
  203. imguiSeparatorLine();
  204. imguiLabel("Blend mode:");
  205. mode = imguiChoose(mode
  206. , "None"
  207. , "Separate"
  208. , "MRT Independent"
  209. );
  210. imguiSeparatorLine();
  211. if (imguiCheck("Front to back", frontToBack) )
  212. {
  213. frontToBack ^= true;
  214. }
  215. if (imguiCheck("Fade in/out", fadeInOut) )
  216. {
  217. fadeInOut ^= true;
  218. }
  219. imguiEndScrollArea();
  220. imguiEndFrame();
  221. // Set view 0 default viewport.
  222. bgfx::setViewRectMask(0x3, 0, 0, width, height);
  223. int64_t now = bx::getHPCounter();
  224. static int64_t last = now;
  225. const int64_t frameTime = now - last;
  226. last = now;
  227. const double freq = double(bx::getHPFrequency() );
  228. const double toMs = 1000.0/freq;
  229. float time = (float)( (now-timeOffset)/freq);
  230. // Use debug font to print information about this example.
  231. bgfx::dbgTextClear();
  232. // Reference:
  233. // Weighted, Blended Order-Independent Transparency
  234. // http://jcgt.org/published/0002/02/09/
  235. // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html
  236. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit");
  237. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency.");
  238. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  239. float at[3] = { 0.0f, 0.0f, 0.0f };
  240. float eye[3] = { 0.0f, 0.0f, -7.0f };
  241. float view[16];
  242. float proj[16];
  243. // Set view and projection matrix for view 0.
  244. mtxLookAt(view, eye, at);
  245. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  246. bgfx::setViewTransform(0, view, proj);
  247. bgfx::setViewClearMask(0x3
  248. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  249. , 0x00000000
  250. , 1.0f
  251. , 0
  252. );
  253. bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
  254. bgfx::setViewFrameBuffer(0, 0 == mode ? invalid : fbh);
  255. // Set view and projection matrix for view 1.
  256. mtxIdentity(view);
  257. mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  258. bgfx::setViewTransform(1, view, proj);
  259. for (uint32_t depth = 0; depth < 3; ++depth)
  260. {
  261. uint32_t zz = frontToBack ? 2-depth : depth;
  262. for (uint32_t yy = 0; yy < 3; ++yy)
  263. {
  264. for (uint32_t xx = 0; xx < 3; ++xx)
  265. {
  266. float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f };
  267. if (fadeInOut
  268. && zz == 1)
  269. {
  270. color[3] = sinf(time*3.0f)*0.49f+0.5f;
  271. }
  272. bgfx::setUniform(u_color, color);
  273. BX_UNUSED(time);
  274. float mtx[16];
  275. mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  276. //mtxIdentity(mtx);
  277. mtx[12] = -2.5f + float(xx)*2.5f;
  278. mtx[13] = -2.5f + float(yy)*2.5f;
  279. mtx[14] = -2.5f + float(zz)*2.5f; //0.0f; // sinf(time + ( (xx+1)*(yy+1)/9.0f)*float(M_PI) )*50.0f+50.0f; //90.0f - (xx+1)*(yy+1)*10.0f;
  280. // Set transform for draw call.
  281. bgfx::setTransform(mtx);
  282. // Set vertex and index buffer.
  283. bgfx::setVertexBuffer(vbh);
  284. bgfx::setIndexBuffer(ibh);
  285. const uint64_t state = 0
  286. | BGFX_STATE_CULL_CW
  287. | BGFX_STATE_RGB_WRITE
  288. | BGFX_STATE_ALPHA_WRITE
  289. | BGFX_STATE_DEPTH_TEST_LESS
  290. | BGFX_STATE_MSAA
  291. ;
  292. switch (mode)
  293. {
  294. case 0:
  295. // Set vertex and fragment shaders.
  296. bgfx::setProgram(blend);
  297. // Set render states.
  298. bgfx::setState(state
  299. | BGFX_STATE_BLEND_ALPHA
  300. );
  301. break;
  302. case 1:
  303. // Set vertex and fragment shaders.
  304. bgfx::setProgram(wbSeparatePass);
  305. // Set render states.
  306. bgfx::setState(state
  307. | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  308. );
  309. break;
  310. default:
  311. // Set vertex and fragment shaders.
  312. bgfx::setProgram(wbPass);
  313. // Set render states.
  314. bgfx::setState(state
  315. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
  316. | BGFX_STATE_BLEND_INDEPENDENT
  317. , 0
  318. | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR)
  319. );
  320. break;
  321. }
  322. // Submit primitive for rendering to view 0.
  323. bgfx::submit(0);
  324. }
  325. }
  326. }
  327. if (0 != mode)
  328. {
  329. bgfx::setTexture(0, u_texColor0, fbtextures[0]);
  330. bgfx::setTexture(1, u_texColor1, fbtextures[1]);
  331. bgfx::setProgram(1 == mode ? wbSeparateBlit : wbBlit);
  332. bgfx::setState(0
  333. | BGFX_STATE_RGB_WRITE
  334. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA)
  335. );
  336. screenSpaceQuad( (float)width, (float)height, s_flipV);
  337. bgfx::submit(1);
  338. }
  339. // Advance to next frame. Rendering thread will be kicked to
  340. // process submitted rendering primitives.
  341. bgfx::frame();
  342. }
  343. // Cleanup.
  344. imguiDestroy();
  345. bgfx::destroyFrameBuffer(fbh);
  346. bgfx::destroyIndexBuffer(ibh);
  347. bgfx::destroyVertexBuffer(vbh);
  348. bgfx::destroyProgram(blend);
  349. bgfx::destroyProgram(wbSeparatePass);
  350. bgfx::destroyProgram(wbSeparateBlit);
  351. bgfx::destroyProgram(wbPass);
  352. bgfx::destroyProgram(wbBlit);
  353. bgfx::destroyUniform(u_texColor0);
  354. bgfx::destroyUniform(u_texColor1);
  355. bgfx::destroyUniform(u_color);
  356. // Shutdown bgfx.
  357. bgfx::shutdown();
  358. return 0;
  359. }