deferred.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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_mz
  378. , m_width
  379. , m_height
  380. );
  381. imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
  382. imguiSeparatorLine();
  383. imguiSlider("Num lights", m_numLights, 1, 2048);
  384. if (imguiCheck("Show G-Buffer.", m_showGBuffer) )
  385. {
  386. m_showGBuffer = !m_showGBuffer;
  387. }
  388. if (imguiCheck("Show light scissor.", m_showScissorRects) )
  389. {
  390. m_showScissorRects = !m_showScissorRects;
  391. }
  392. if (imguiCheck("Animate mesh.", m_animateMesh) )
  393. {
  394. m_animateMesh = !m_animateMesh;
  395. }
  396. imguiSlider("Lights animation speed", m_lightAnimationSpeed, 0.0f, 0.4f, 0.01f);
  397. imguiEndScrollArea();
  398. imguiEndFrame();
  399. // Update camera.
  400. cameraUpdate(deltaTime, m_mouseState);
  401. float view[16];
  402. cameraGetViewMtx(view);
  403. // Setup views
  404. float vp[16];
  405. float invMvp[16];
  406. {
  407. bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, m_width, m_height);
  408. bgfx::setViewRect(RENDER_PASS_LIGHT_ID, 0, 0, m_width, m_height);
  409. bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, m_width, m_height);
  410. bgfx::setViewRect(RENDER_PASS_DEBUG_LIGHTS_ID, 0, 0, m_width, m_height);
  411. bgfx::setViewRect(RENDER_PASS_DEBUG_GBUFFER_ID, 0, 0, m_width, m_height);
  412. bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, m_lightBuffer);
  413. float proj[16];
  414. mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  415. bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
  416. bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
  417. bx::mtxMul(vp, view, proj);
  418. bx::mtxInverse(invMvp, vp);
  419. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  420. bgfx::setViewTransform(RENDER_PASS_LIGHT_ID, NULL, proj);
  421. bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
  422. const float aspectRatio = float(m_height)/float(m_width);
  423. const float size = 10.0f;
  424. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  425. bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
  426. bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f);
  427. bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
  428. }
  429. const uint32_t dim = 11;
  430. const float offset = (float(dim-1) * 3.0f) * 0.5f;
  431. // Draw into geometry pass.
  432. for (uint32_t yy = 0; yy < dim; ++yy)
  433. {
  434. for (uint32_t xx = 0; xx < dim; ++xx)
  435. {
  436. float mtx[16];
  437. if (m_animateMesh)
  438. {
  439. bx::mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
  440. }
  441. else
  442. {
  443. bx::mtxIdentity(mtx);
  444. }
  445. mtx[12] = -offset + float(xx)*3.0f;
  446. mtx[13] = -offset + float(yy)*3.0f;
  447. mtx[14] = 0.0f;
  448. // Set transform for draw call.
  449. bgfx::setTransform(mtx);
  450. // Set vertex and index buffer.
  451. bgfx::setVertexBuffer(m_vbh);
  452. bgfx::setIndexBuffer(m_ibh);
  453. // Bind textures.
  454. bgfx::setTexture(0, s_texColor, m_textureColor);
  455. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  456. // Set render states.
  457. bgfx::setState(0
  458. | BGFX_STATE_RGB_WRITE
  459. | BGFX_STATE_ALPHA_WRITE
  460. | BGFX_STATE_DEPTH_WRITE
  461. | BGFX_STATE_DEPTH_TEST_LESS
  462. | BGFX_STATE_MSAA
  463. );
  464. // Submit primitive for rendering to view 0.
  465. bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
  466. }
  467. }
  468. // Draw lights into light buffer.
  469. for (int32_t light = 0; light < m_numLights; ++light)
  470. {
  471. Sphere lightPosRadius;
  472. float lightTime = time * m_lightAnimationSpeed * (sinf(light/float(m_numLights) * bx::piHalf ) * 0.5f + 0.5f);
  473. lightPosRadius.m_center[0] = sinf( ( (lightTime + light*0.47f) + bx::piHalf*1.37f ) )*offset;
  474. lightPosRadius.m_center[1] = cosf( ( (lightTime + light*0.69f) + bx::piHalf*1.49f ) )*offset;
  475. lightPosRadius.m_center[2] = sinf( ( (lightTime + light*0.37f) + bx::piHalf*1.57f ) )*2.0f;
  476. lightPosRadius.m_radius = 2.0f;
  477. Aabb aabb;
  478. sphereToAabb(aabb, lightPosRadius);
  479. float box[8][3] =
  480. {
  481. { aabb.m_min[0], aabb.m_min[1], aabb.m_min[2] },
  482. { aabb.m_min[0], aabb.m_min[1], aabb.m_max[2] },
  483. { aabb.m_min[0], aabb.m_max[1], aabb.m_min[2] },
  484. { aabb.m_min[0], aabb.m_max[1], aabb.m_max[2] },
  485. { aabb.m_max[0], aabb.m_min[1], aabb.m_min[2] },
  486. { aabb.m_max[0], aabb.m_min[1], aabb.m_max[2] },
  487. { aabb.m_max[0], aabb.m_max[1], aabb.m_min[2] },
  488. { aabb.m_max[0], aabb.m_max[1], aabb.m_max[2] },
  489. };
  490. float xyz[3];
  491. bx::vec3MulMtxH(xyz, box[0], vp);
  492. float minx = xyz[0];
  493. float miny = xyz[1];
  494. float maxx = xyz[0];
  495. float maxy = xyz[1];
  496. float maxz = xyz[2];
  497. for (uint32_t ii = 1; ii < 8; ++ii)
  498. {
  499. bx::vec3MulMtxH(xyz, box[ii], vp);
  500. minx = bx::fmin(minx, xyz[0]);
  501. miny = bx::fmin(miny, xyz[1]);
  502. maxx = bx::fmax(maxx, xyz[0]);
  503. maxy = bx::fmax(maxy, xyz[1]);
  504. maxz = bx::fmax(maxz, xyz[2]);
  505. }
  506. // Cull light if it's fully behind camera.
  507. if (maxz >= 0.0f)
  508. {
  509. float x0 = bx::fclamp( (minx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  510. float y0 = bx::fclamp( (miny * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  511. float x1 = bx::fclamp( (maxx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  512. float y1 = bx::fclamp( (maxy * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  513. if (m_showScissorRects)
  514. {
  515. bgfx::TransientVertexBuffer tvb;
  516. bgfx::TransientIndexBuffer tib;
  517. if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
  518. {
  519. uint32_t abgr = 0x8000ff00;
  520. DebugVertex* vertex = (DebugVertex*)tvb.data;
  521. vertex->m_x = x0;
  522. vertex->m_y = y0;
  523. vertex->m_z = 0.0f;
  524. vertex->m_abgr = abgr;
  525. ++vertex;
  526. vertex->m_x = x1;
  527. vertex->m_y = y0;
  528. vertex->m_z = 0.0f;
  529. vertex->m_abgr = abgr;
  530. ++vertex;
  531. vertex->m_x = x1;
  532. vertex->m_y = y1;
  533. vertex->m_z = 0.0f;
  534. vertex->m_abgr = abgr;
  535. ++vertex;
  536. vertex->m_x = x0;
  537. vertex->m_y = y1;
  538. vertex->m_z = 0.0f;
  539. vertex->m_abgr = abgr;
  540. uint16_t* indices = (uint16_t*)tib.data;
  541. *indices++ = 0;
  542. *indices++ = 1;
  543. *indices++ = 1;
  544. *indices++ = 2;
  545. *indices++ = 2;
  546. *indices++ = 3;
  547. *indices++ = 3;
  548. *indices++ = 0;
  549. bgfx::setVertexBuffer(&tvb);
  550. bgfx::setIndexBuffer(&tib);
  551. bgfx::setState(0
  552. | BGFX_STATE_RGB_WRITE
  553. | BGFX_STATE_PT_LINES
  554. | BGFX_STATE_BLEND_ALPHA
  555. );
  556. bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID, m_lineProgram);
  557. }
  558. }
  559. uint8_t val = light&7;
  560. float lightRgbInnerR[4] =
  561. {
  562. val & 0x1 ? 1.0f : 0.25f,
  563. val & 0x2 ? 1.0f : 0.25f,
  564. val & 0x4 ? 1.0f : 0.25f,
  565. 0.8f,
  566. };
  567. // Draw light.
  568. bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
  569. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
  570. bgfx::setUniform(u_mtx, invMvp);
  571. const uint16_t scissorHeight = uint16_t(y1-y0);
  572. bgfx::setScissor(uint16_t(x0), m_height-scissorHeight-uint16_t(y0), uint16_t(x1-x0), scissorHeight);
  573. bgfx::setTexture(0, s_normal, m_gbuffer, 1);
  574. bgfx::setTexture(1, s_depth, m_gbuffer, 2);
  575. bgfx::setState(0
  576. | BGFX_STATE_RGB_WRITE
  577. | BGFX_STATE_ALPHA_WRITE
  578. | BGFX_STATE_BLEND_ADD
  579. );
  580. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  581. bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram);
  582. }
  583. }
  584. // Combine color and light buffers.
  585. bgfx::setTexture(0, s_albedo, m_gbuffer, 0);
  586. bgfx::setTexture(1, s_light, m_lightBuffer, 0);
  587. bgfx::setState(0
  588. | BGFX_STATE_RGB_WRITE
  589. | BGFX_STATE_ALPHA_WRITE
  590. );
  591. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  592. bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
  593. if (m_showGBuffer)
  594. {
  595. const float aspectRatio = float(m_width)/float(m_height);
  596. // Draw m_debug m_gbuffer.
  597. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
  598. {
  599. float mtx[16];
  600. bx::mtxSRT(mtx
  601. , aspectRatio, 1.0f, 1.0f
  602. , 0.0f, 0.0f, 0.0f
  603. , -7.9f - BX_COUNTOF(m_gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
  604. );
  605. bgfx::setTransform(mtx);
  606. bgfx::setVertexBuffer(m_vbh);
  607. bgfx::setIndexBuffer(m_ibh, 0, 6);
  608. bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
  609. bgfx::setState(BGFX_STATE_RGB_WRITE);
  610. bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID, m_debugProgram);
  611. }
  612. }
  613. }
  614. // Advance to next frame. Rendering thread will be kicked to
  615. // process submitted rendering primitives.
  616. bgfx::frame();
  617. return true;
  618. }
  619. return false;
  620. }
  621. bgfx::VertexBufferHandle m_vbh;
  622. bgfx::IndexBufferHandle m_ibh;
  623. bgfx::UniformHandle s_texColor;
  624. bgfx::UniformHandle s_texNormal;
  625. bgfx::UniformHandle s_albedo;
  626. bgfx::UniformHandle s_normal;
  627. bgfx::UniformHandle s_depth;
  628. bgfx::UniformHandle s_light;
  629. bgfx::UniformHandle u_mtx;
  630. bgfx::UniformHandle u_lightPosRadius;
  631. bgfx::UniformHandle u_lightRgbInnerR;
  632. bgfx::ProgramHandle m_geomProgram;
  633. bgfx::ProgramHandle m_lightProgram;
  634. bgfx::ProgramHandle m_combineProgram;
  635. bgfx::ProgramHandle m_debugProgram;
  636. bgfx::ProgramHandle m_lineProgram;
  637. bgfx::TextureHandle m_textureColor;
  638. bgfx::TextureHandle m_textureNormal;
  639. bgfx::TextureHandle m_gbufferTex[3];
  640. bgfx::FrameBufferHandle m_gbuffer;
  641. bgfx::FrameBufferHandle m_lightBuffer;
  642. uint32_t m_width;
  643. uint32_t m_height;
  644. uint32_t m_debug;
  645. uint32_t m_reset;
  646. uint32_t m_oldWidth;
  647. uint32_t m_oldHeight;
  648. uint32_t m_oldReset;
  649. int32_t m_scrollArea;
  650. int32_t m_numLights;
  651. float m_lightAnimationSpeed;
  652. bool m_animateMesh;
  653. bool m_showScissorRects;
  654. bool m_showGBuffer;
  655. entry::MouseState m_mouseState;
  656. const bgfx::Caps* m_caps;
  657. int64_t m_timeOffset;
  658. };
  659. ENTRY_IMPLEMENT_MAIN(Deferred);