deferred.cpp 23 KB

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