deferred.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. #include "camera.h"
  9. #include "bounds.h"
  10. #define RENDER_PASS_GEOMETRY_ID 0
  11. #define RENDER_PASS_LIGHT_ID 1
  12. #define RENDER_PASS_COMBINE_ID 2
  13. #define RENDER_PASS_DEBUG_LIGHTS_ID 3
  14. #define RENDER_PASS_DEBUG_GBUFFER_ID 4
  15. static float s_texelHalf = 0.0f;
  16. static bool s_originBottomLeft = false;
  17. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  18. {
  19. bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_originBottomLeft);
  20. }
  21. struct PosNormalTangentTexcoordVertex
  22. {
  23. float m_x;
  24. float m_y;
  25. float m_z;
  26. uint32_t m_normal;
  27. uint32_t m_tangent;
  28. int16_t m_u;
  29. int16_t m_v;
  30. static void init()
  31. {
  32. ms_decl
  33. .begin()
  34. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  35. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  36. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  37. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
  38. .end();
  39. }
  40. static bgfx::VertexDecl ms_decl;
  41. };
  42. bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
  43. struct PosTexCoord0Vertex
  44. {
  45. float m_x;
  46. float m_y;
  47. float m_z;
  48. float m_u;
  49. float m_v;
  50. static void init()
  51. {
  52. ms_decl
  53. .begin()
  54. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  55. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  56. .end();
  57. }
  58. static bgfx::VertexDecl ms_decl;
  59. };
  60. bgfx::VertexDecl PosTexCoord0Vertex::ms_decl;
  61. struct DebugVertex
  62. {
  63. float m_x;
  64. float m_y;
  65. float m_z;
  66. uint32_t m_abgr;
  67. static void init()
  68. {
  69. ms_decl
  70. .begin()
  71. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  72. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  73. .end();
  74. }
  75. static bgfx::VertexDecl ms_decl;
  76. };
  77. bgfx::VertexDecl DebugVertex::ms_decl;
  78. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  79. {
  80. union
  81. {
  82. uint32_t ui32;
  83. uint8_t arr[4];
  84. } un;
  85. un.arr[0] = _x;
  86. un.arr[1] = _y;
  87. un.arr[2] = _z;
  88. un.arr[3] = _w;
  89. return un.ui32;
  90. }
  91. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  92. {
  93. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  94. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  95. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  96. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  97. return packUint32(xx, yy, zz, ww);
  98. }
  99. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  100. {
  101. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  102. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  103. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  104. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  105. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  106. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  107. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  108. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  109. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  110. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  111. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  112. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  113. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  114. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  115. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  116. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  117. { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  118. { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  119. { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  120. { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  121. {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  122. {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  123. {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  124. {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  125. };
  126. static const uint16_t s_cubeIndices[36] =
  127. {
  128. 0, 2, 1,
  129. 1, 2, 3,
  130. 4, 5, 6,
  131. 5, 7, 6,
  132. 8, 10, 9,
  133. 9, 10, 11,
  134. 12, 13, 14,
  135. 13, 15, 14,
  136. 16, 18, 17,
  137. 17, 18, 19,
  138. 20, 21, 22,
  139. 21, 23, 22,
  140. };
  141. void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  142. {
  143. if (bgfx::checkAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_decl) )
  144. {
  145. bgfx::TransientVertexBuffer vb;
  146. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_decl);
  147. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
  148. const float minx = -_width;
  149. const float maxx = _width;
  150. const float miny = 0.0f;
  151. const float maxy = _height*2.0f;
  152. const float texelHalfW = _texelHalf/_textureWidth;
  153. const float texelHalfH = _texelHalf/_textureHeight;
  154. const float minu = -1.0f + texelHalfW;
  155. const float maxu = 1.0f + texelHalfH;
  156. const float zz = 0.0f;
  157. float minv = texelHalfH;
  158. float maxv = 2.0f + texelHalfH;
  159. if (_originBottomLeft)
  160. {
  161. float temp = minv;
  162. minv = maxv;
  163. maxv = temp;
  164. minv -= 1.0f;
  165. maxv -= 1.0f;
  166. }
  167. vertex[0].m_x = minx;
  168. vertex[0].m_y = miny;
  169. vertex[0].m_z = zz;
  170. vertex[0].m_u = minu;
  171. vertex[0].m_v = minv;
  172. vertex[1].m_x = maxx;
  173. vertex[1].m_y = miny;
  174. vertex[1].m_z = zz;
  175. vertex[1].m_u = maxu;
  176. vertex[1].m_v = minv;
  177. vertex[2].m_x = maxx;
  178. vertex[2].m_y = maxy;
  179. vertex[2].m_z = zz;
  180. vertex[2].m_u = maxu;
  181. vertex[2].m_v = maxv;
  182. bgfx::setVertexBuffer(&vb);
  183. }
  184. }
  185. class Deferred : public entry::AppI
  186. {
  187. void init(int /*_argc*/, char** /*_argv*/) BX_OVERRIDE
  188. {
  189. m_width = 1280;
  190. m_height = 720;
  191. m_debug = BGFX_DEBUG_TEXT;
  192. m_reset = BGFX_RESET_VSYNC;
  193. bgfx::init();
  194. bgfx::reset(m_width, m_height, m_reset);
  195. // Enable m_debug text.
  196. bgfx::setDebug(m_debug);
  197. // Set clear color palette for index 0
  198. bgfx::setClearColor(0, UINT32_C(0x00000000) );
  199. // Set clear color palette for index 1
  200. bgfx::setClearColor(1, UINT32_C(0x303030ff) );
  201. // Set geometry pass view clear state.
  202. bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID
  203. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  204. , 1.0f
  205. , 0
  206. , 1
  207. );
  208. // Set light pass view clear state.
  209. bgfx::setViewClear(RENDER_PASS_LIGHT_ID
  210. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  211. , 1.0f
  212. , 0
  213. , 0
  214. );
  215. // Create vertex stream declaration.
  216. PosNormalTangentTexcoordVertex::init();
  217. PosTexCoord0Vertex::init();
  218. DebugVertex::init();
  219. calcTangents(s_cubeVertices
  220. , BX_COUNTOF(s_cubeVertices)
  221. , PosNormalTangentTexcoordVertex::ms_decl
  222. , s_cubeIndices
  223. , BX_COUNTOF(s_cubeIndices)
  224. );
  225. // Create static vertex buffer.
  226. m_vbh = bgfx::createVertexBuffer(
  227. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  228. , PosNormalTangentTexcoordVertex::ms_decl
  229. );
  230. // Create static index buffer.
  231. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  232. // Create texture sampler uniforms.
  233. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  234. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  235. s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Int1);
  236. s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Int1);
  237. s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Int1);
  238. s_light = bgfx::createUniform("s_light", bgfx::UniformType::Int1);
  239. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  240. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
  241. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
  242. // Create program from shaders.
  243. m_geomProgram = loadProgram("vs_deferred_geom", "fs_deferred_geom");
  244. m_lightProgram = loadProgram("vs_deferred_light", "fs_deferred_light");
  245. m_combineProgram = loadProgram("vs_deferred_combine", "fs_deferred_combine");
  246. m_debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug");
  247. m_lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line");
  248. // Load diffuse texture.
  249. m_textureColor = loadTexture("fieldstone-rgba.dds");
  250. // Load normal texture.
  251. m_textureNormal = loadTexture("fieldstone-n.dds");
  252. m_gbufferTex[0].idx = bgfx::invalidHandle;
  253. m_gbufferTex[1].idx = bgfx::invalidHandle;
  254. m_gbufferTex[2].idx = bgfx::invalidHandle;
  255. m_gbuffer.idx = bgfx::invalidHandle;
  256. m_lightBuffer.idx = bgfx::invalidHandle;
  257. // Imgui.
  258. imguiCreate();
  259. m_timeOffset = bx::getHPCounter();
  260. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  261. s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  262. s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
  263. // Get renderer capabilities info.
  264. m_caps = bgfx::getCaps();
  265. m_oldWidth = 0;
  266. m_oldHeight = 0;
  267. m_oldReset = m_reset;
  268. m_scrollArea = 0;
  269. m_numLights = 512;
  270. m_lightAnimationSpeed = 0.3f;
  271. m_animateMesh = true;
  272. m_showScissorRects = false;
  273. m_showGBuffer = true;
  274. cameraCreate();
  275. const float initialPos[3] = { 0.0f, 0.0f, -15.0f };
  276. cameraSetPosition(initialPos);
  277. cameraSetVerticalAngle(0.0f);
  278. }
  279. virtual int shutdown() BX_OVERRIDE
  280. {
  281. // Cleanup.
  282. cameraDestroy();
  283. imguiDestroy();
  284. if (bgfx::isValid(m_gbuffer) )
  285. {
  286. bgfx::destroyFrameBuffer(m_gbuffer);
  287. bgfx::destroyFrameBuffer(m_lightBuffer);
  288. }
  289. bgfx::destroyIndexBuffer(m_ibh);
  290. bgfx::destroyVertexBuffer(m_vbh);
  291. bgfx::destroyProgram(m_geomProgram);
  292. bgfx::destroyProgram(m_lightProgram);
  293. bgfx::destroyProgram(m_combineProgram);
  294. bgfx::destroyProgram(m_debugProgram);
  295. bgfx::destroyProgram(m_lineProgram);
  296. bgfx::destroyTexture(m_textureColor);
  297. bgfx::destroyTexture(m_textureNormal);
  298. bgfx::destroyUniform(s_texColor);
  299. bgfx::destroyUniform(s_texNormal);
  300. bgfx::destroyUniform(s_albedo);
  301. bgfx::destroyUniform(s_normal);
  302. bgfx::destroyUniform(s_depth);
  303. bgfx::destroyUniform(s_light);
  304. bgfx::destroyUniform(u_lightPosRadius);
  305. bgfx::destroyUniform(u_lightRgbInnerR);
  306. bgfx::destroyUniform(u_mtx);
  307. // Shutdown bgfx.
  308. bgfx::shutdown();
  309. return 0;
  310. }
  311. bool update() BX_OVERRIDE
  312. {
  313. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  314. {
  315. int64_t now = bx::getHPCounter();
  316. static int64_t last = now;
  317. const int64_t frameTime = now - last;
  318. last = now;
  319. const double freq = double(bx::getHPFrequency() );
  320. const double toMs = 1000.0/freq;
  321. const float deltaTime = float(frameTime/freq);
  322. float time = (float)( (now-m_timeOffset)/freq);
  323. // Use m_debug font to print information about this example.
  324. bgfx::dbgTextClear();
  325. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/21-deferred");
  326. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: MRT rendering and deferred shading.");
  327. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  328. if (2 > m_caps->maxFBAttachments)
  329. {
  330. // When multiple render targets (MRT) is not supported by GPU,
  331. // implement alternative code path that doesn't use MRT.
  332. bool blink = uint32_t(time*3.0f)&1;
  333. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " MRT not supported by GPU. ");
  334. // Set view 0 default viewport.
  335. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  336. // This dummy draw call is here to make sure that view 0 is cleared
  337. // if no other draw calls are submitted to view 0.
  338. bgfx::touch(0);
  339. }
  340. else
  341. {
  342. if (m_oldWidth != m_width
  343. || m_oldHeight != m_height
  344. || m_oldReset != m_reset
  345. || !bgfx::isValid(m_gbuffer) )
  346. {
  347. // Recreate variable size render targets when resolution changes.
  348. m_oldWidth = m_width;
  349. m_oldHeight = m_height;
  350. m_oldReset = m_reset;
  351. if (bgfx::isValid(m_gbuffer) )
  352. {
  353. bgfx::destroyFrameBuffer(m_gbuffer);
  354. }
  355. const uint32_t samplerFlags = 0
  356. | BGFX_TEXTURE_RT
  357. | BGFX_TEXTURE_MIN_POINT
  358. | BGFX_TEXTURE_MAG_POINT
  359. | BGFX_TEXTURE_MIP_POINT
  360. | BGFX_TEXTURE_U_CLAMP
  361. | BGFX_TEXTURE_V_CLAMP
  362. ;
  363. m_gbufferTex[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  364. m_gbufferTex[1] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  365. m_gbufferTex[2] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::D24, samplerFlags);
  366. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
  367. if (bgfx::isValid(m_lightBuffer) )
  368. {
  369. bgfx::destroyFrameBuffer(m_lightBuffer);
  370. }
  371. m_lightBuffer = bgfx::createFrameBuffer(m_width, m_height, bgfx::TextureFormat::BGRA8, samplerFlags);
  372. }
  373. imguiBeginFrame(m_mouseState.m_mx
  374. , m_mouseState.m_my
  375. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  376. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  377. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  378. , m_mouseState.m_mz
  379. , m_width
  380. , m_height
  381. );
  382. imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
  383. imguiSeparatorLine();
  384. imguiSlider("Num lights", m_numLights, 1, 2048);
  385. if (imguiCheck("Show G-Buffer.", m_showGBuffer) )
  386. {
  387. m_showGBuffer = !m_showGBuffer;
  388. }
  389. if (imguiCheck("Show light scissor.", m_showScissorRects) )
  390. {
  391. m_showScissorRects = !m_showScissorRects;
  392. }
  393. if (imguiCheck("Animate mesh.", m_animateMesh) )
  394. {
  395. m_animateMesh = !m_animateMesh;
  396. }
  397. imguiSlider("Lights animation speed", m_lightAnimationSpeed, 0.0f, 0.4f, 0.01f);
  398. imguiEndScrollArea();
  399. imguiEndFrame();
  400. // Update camera.
  401. cameraUpdate(deltaTime, m_mouseState);
  402. float view[16];
  403. cameraGetViewMtx(view);
  404. // Setup views
  405. float vp[16];
  406. float invMvp[16];
  407. {
  408. bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, m_width, m_height);
  409. bgfx::setViewRect(RENDER_PASS_LIGHT_ID, 0, 0, m_width, m_height);
  410. bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, m_width, m_height);
  411. bgfx::setViewRect(RENDER_PASS_DEBUG_LIGHTS_ID, 0, 0, m_width, m_height);
  412. bgfx::setViewRect(RENDER_PASS_DEBUG_GBUFFER_ID, 0, 0, m_width, m_height);
  413. bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, m_lightBuffer);
  414. float proj[16];
  415. mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  416. bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
  417. bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
  418. bx::mtxMul(vp, view, proj);
  419. bx::mtxInverse(invMvp, vp);
  420. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  421. bgfx::setViewTransform(RENDER_PASS_LIGHT_ID, NULL, proj);
  422. bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
  423. const float aspectRatio = float(m_height)/float(m_width);
  424. const float size = 10.0f;
  425. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  426. bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
  427. bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f);
  428. bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
  429. }
  430. const uint32_t dim = 11;
  431. const float offset = (float(dim-1) * 3.0f) * 0.5f;
  432. // Draw into geometry pass.
  433. for (uint32_t yy = 0; yy < dim; ++yy)
  434. {
  435. for (uint32_t xx = 0; xx < dim; ++xx)
  436. {
  437. float mtx[16];
  438. if (m_animateMesh)
  439. {
  440. bx::mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
  441. }
  442. else
  443. {
  444. bx::mtxIdentity(mtx);
  445. }
  446. mtx[12] = -offset + float(xx)*3.0f;
  447. mtx[13] = -offset + float(yy)*3.0f;
  448. mtx[14] = 0.0f;
  449. // Set transform for draw call.
  450. bgfx::setTransform(mtx);
  451. // Set vertex and index buffer.
  452. bgfx::setVertexBuffer(m_vbh);
  453. bgfx::setIndexBuffer(m_ibh);
  454. // Bind textures.
  455. bgfx::setTexture(0, s_texColor, m_textureColor);
  456. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  457. // Set render states.
  458. bgfx::setState(0
  459. | BGFX_STATE_RGB_WRITE
  460. | BGFX_STATE_ALPHA_WRITE
  461. | BGFX_STATE_DEPTH_WRITE
  462. | BGFX_STATE_DEPTH_TEST_LESS
  463. | BGFX_STATE_MSAA
  464. );
  465. // Submit primitive for rendering to view 0.
  466. bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
  467. }
  468. }
  469. // Draw lights into light buffer.
  470. for (int32_t light = 0; light < m_numLights; ++light)
  471. {
  472. Sphere lightPosRadius;
  473. float lightTime = time * m_lightAnimationSpeed * (sinf(light/float(m_numLights) * bx::piHalf ) * 0.5f + 0.5f);
  474. lightPosRadius.m_center[0] = sinf( ( (lightTime + light*0.47f) + bx::piHalf*1.37f ) )*offset;
  475. lightPosRadius.m_center[1] = cosf( ( (lightTime + light*0.69f) + bx::piHalf*1.49f ) )*offset;
  476. lightPosRadius.m_center[2] = sinf( ( (lightTime + light*0.37f) + bx::piHalf*1.57f ) )*2.0f;
  477. lightPosRadius.m_radius = 2.0f;
  478. Aabb aabb;
  479. sphereToAabb(aabb, lightPosRadius);
  480. float box[8][3] =
  481. {
  482. { aabb.m_min[0], aabb.m_min[1], aabb.m_min[2] },
  483. { aabb.m_min[0], aabb.m_min[1], aabb.m_max[2] },
  484. { aabb.m_min[0], aabb.m_max[1], aabb.m_min[2] },
  485. { aabb.m_min[0], aabb.m_max[1], aabb.m_max[2] },
  486. { aabb.m_max[0], aabb.m_min[1], aabb.m_min[2] },
  487. { aabb.m_max[0], aabb.m_min[1], aabb.m_max[2] },
  488. { aabb.m_max[0], aabb.m_max[1], aabb.m_min[2] },
  489. { aabb.m_max[0], aabb.m_max[1], aabb.m_max[2] },
  490. };
  491. float xyz[3];
  492. bx::vec3MulMtxH(xyz, box[0], vp);
  493. float minx = xyz[0];
  494. float miny = xyz[1];
  495. float maxx = xyz[0];
  496. float maxy = xyz[1];
  497. float maxz = xyz[2];
  498. for (uint32_t ii = 1; ii < 8; ++ii)
  499. {
  500. bx::vec3MulMtxH(xyz, box[ii], vp);
  501. minx = bx::fmin(minx, xyz[0]);
  502. miny = bx::fmin(miny, xyz[1]);
  503. maxx = bx::fmax(maxx, xyz[0]);
  504. maxy = bx::fmax(maxy, xyz[1]);
  505. maxz = bx::fmax(maxz, xyz[2]);
  506. }
  507. // Cull light if it's fully behind camera.
  508. if (maxz >= 0.0f)
  509. {
  510. float x0 = bx::fclamp( (minx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  511. float y0 = bx::fclamp( (miny * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  512. float x1 = bx::fclamp( (maxx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  513. float y1 = bx::fclamp( (maxy * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  514. if (m_showScissorRects)
  515. {
  516. bgfx::TransientVertexBuffer tvb;
  517. bgfx::TransientIndexBuffer tib;
  518. if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
  519. {
  520. uint32_t abgr = 0x8000ff00;
  521. DebugVertex* vertex = (DebugVertex*)tvb.data;
  522. vertex->m_x = x0;
  523. vertex->m_y = y0;
  524. vertex->m_z = 0.0f;
  525. vertex->m_abgr = abgr;
  526. ++vertex;
  527. vertex->m_x = x1;
  528. vertex->m_y = y0;
  529. vertex->m_z = 0.0f;
  530. vertex->m_abgr = abgr;
  531. ++vertex;
  532. vertex->m_x = x1;
  533. vertex->m_y = y1;
  534. vertex->m_z = 0.0f;
  535. vertex->m_abgr = abgr;
  536. ++vertex;
  537. vertex->m_x = x0;
  538. vertex->m_y = y1;
  539. vertex->m_z = 0.0f;
  540. vertex->m_abgr = abgr;
  541. uint16_t* indices = (uint16_t*)tib.data;
  542. *indices++ = 0;
  543. *indices++ = 1;
  544. *indices++ = 1;
  545. *indices++ = 2;
  546. *indices++ = 2;
  547. *indices++ = 3;
  548. *indices++ = 3;
  549. *indices++ = 0;
  550. bgfx::setVertexBuffer(&tvb);
  551. bgfx::setIndexBuffer(&tib);
  552. bgfx::setState(0
  553. | BGFX_STATE_RGB_WRITE
  554. | BGFX_STATE_PT_LINES
  555. | BGFX_STATE_BLEND_ALPHA
  556. );
  557. bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID, m_lineProgram);
  558. }
  559. }
  560. uint8_t val = light&7;
  561. float lightRgbInnerR[4] =
  562. {
  563. val & 0x1 ? 1.0f : 0.25f,
  564. val & 0x2 ? 1.0f : 0.25f,
  565. val & 0x4 ? 1.0f : 0.25f,
  566. 0.8f,
  567. };
  568. // Draw light.
  569. bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
  570. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
  571. bgfx::setUniform(u_mtx, invMvp);
  572. const uint16_t scissorHeight = uint16_t(y1-y0);
  573. bgfx::setScissor(uint16_t(x0), m_height-scissorHeight-uint16_t(y0), uint16_t(x1-x0), scissorHeight);
  574. bgfx::setTexture(0, s_normal, m_gbuffer, 1);
  575. bgfx::setTexture(1, s_depth, m_gbuffer, 2);
  576. bgfx::setState(0
  577. | BGFX_STATE_RGB_WRITE
  578. | BGFX_STATE_ALPHA_WRITE
  579. | BGFX_STATE_BLEND_ADD
  580. );
  581. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  582. bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram);
  583. }
  584. }
  585. // Combine color and light buffers.
  586. bgfx::setTexture(0, s_albedo, m_gbuffer, 0);
  587. bgfx::setTexture(1, s_light, m_lightBuffer, 0);
  588. bgfx::setState(0
  589. | BGFX_STATE_RGB_WRITE
  590. | BGFX_STATE_ALPHA_WRITE
  591. );
  592. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  593. bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
  594. if (m_showGBuffer)
  595. {
  596. const float aspectRatio = float(m_width)/float(m_height);
  597. // Draw m_debug m_gbuffer.
  598. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
  599. {
  600. float mtx[16];
  601. bx::mtxSRT(mtx
  602. , aspectRatio, 1.0f, 1.0f
  603. , 0.0f, 0.0f, 0.0f
  604. , -7.9f - BX_COUNTOF(m_gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
  605. );
  606. bgfx::setTransform(mtx);
  607. bgfx::setVertexBuffer(m_vbh);
  608. bgfx::setIndexBuffer(m_ibh, 0, 6);
  609. bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
  610. bgfx::setState(BGFX_STATE_RGB_WRITE);
  611. bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID, m_debugProgram);
  612. }
  613. }
  614. }
  615. // Advance to next frame. Rendering thread will be kicked to
  616. // process submitted rendering primitives.
  617. bgfx::frame();
  618. return true;
  619. }
  620. return false;
  621. }
  622. bgfx::VertexBufferHandle m_vbh;
  623. bgfx::IndexBufferHandle m_ibh;
  624. bgfx::UniformHandle s_texColor;
  625. bgfx::UniformHandle s_texNormal;
  626. bgfx::UniformHandle s_albedo;
  627. bgfx::UniformHandle s_normal;
  628. bgfx::UniformHandle s_depth;
  629. bgfx::UniformHandle s_light;
  630. bgfx::UniformHandle u_mtx;
  631. bgfx::UniformHandle u_lightPosRadius;
  632. bgfx::UniformHandle u_lightRgbInnerR;
  633. bgfx::ProgramHandle m_geomProgram;
  634. bgfx::ProgramHandle m_lightProgram;
  635. bgfx::ProgramHandle m_combineProgram;
  636. bgfx::ProgramHandle m_debugProgram;
  637. bgfx::ProgramHandle m_lineProgram;
  638. bgfx::TextureHandle m_textureColor;
  639. bgfx::TextureHandle m_textureNormal;
  640. bgfx::TextureHandle m_gbufferTex[3];
  641. bgfx::FrameBufferHandle m_gbuffer;
  642. bgfx::FrameBufferHandle m_lightBuffer;
  643. uint32_t m_width;
  644. uint32_t m_height;
  645. uint32_t m_debug;
  646. uint32_t m_reset;
  647. uint32_t m_oldWidth;
  648. uint32_t m_oldHeight;
  649. uint32_t m_oldReset;
  650. int32_t m_scrollArea;
  651. int32_t m_numLights;
  652. float m_lightAnimationSpeed;
  653. bool m_animateMesh;
  654. bool m_showScissorRects;
  655. bool m_showGBuffer;
  656. entry::MouseState m_mouseState;
  657. const bgfx::Caps* m_caps;
  658. int64_t m_timeOffset;
  659. };
  660. ENTRY_IMPLEMENT_MAIN(Deferred);