oit.cpp 13 KB

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