deferred.cpp 23 KB

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