deferred.cpp 23 KB

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