2
0

deferred.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. 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 Deferred : public entry::AppI
  182. {
  183. void init(int /*_argc*/, char** /*_argv*/) BX_OVERRIDE
  184. {
  185. m_width = 1280;
  186. m_height = 720;
  187. m_debug = BGFX_DEBUG_TEXT;
  188. m_reset = BGFX_RESET_VSYNC;
  189. bgfx::init();
  190. bgfx::reset(m_width, m_height, m_reset);
  191. // Enable m_debug text.
  192. bgfx::setDebug(m_debug);
  193. // Set clear color palette for index 0
  194. bgfx::setClearColor(0, UINT32_C(0x00000000) );
  195. // Set clear color palette for index 1
  196. bgfx::setClearColor(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("fieldstone-rgba.dds");
  246. // Load normal texture.
  247. m_textureNormal = loadTexture("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. s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
  259. // Get renderer capabilities info.
  260. m_caps = bgfx::getCaps();
  261. m_oldWidth = 0;
  262. m_oldHeight = 0;
  263. m_oldReset = m_reset;
  264. m_scrollArea = 0;
  265. m_numLights = 512;
  266. m_lightAnimationSpeed = 0.3f;
  267. m_animateMesh = true;
  268. m_showScissorRects = false;
  269. m_showGBuffer = true;
  270. cameraCreate();
  271. const float initialPos[3] = { 0.0f, 0.0f, -15.0f };
  272. cameraSetPosition(initialPos);
  273. cameraSetVerticalAngle(0.0f);
  274. }
  275. virtual int shutdown() BX_OVERRIDE
  276. {
  277. // Cleanup.
  278. cameraDestroy();
  279. imguiDestroy();
  280. if (bgfx::isValid(m_gbuffer) )
  281. {
  282. bgfx::destroyFrameBuffer(m_gbuffer);
  283. bgfx::destroyFrameBuffer(m_lightBuffer);
  284. }
  285. bgfx::destroyIndexBuffer(m_ibh);
  286. bgfx::destroyVertexBuffer(m_vbh);
  287. bgfx::destroyProgram(m_geomProgram);
  288. bgfx::destroyProgram(m_lightProgram);
  289. bgfx::destroyProgram(m_combineProgram);
  290. bgfx::destroyProgram(m_debugProgram);
  291. bgfx::destroyProgram(m_lineProgram);
  292. bgfx::destroyTexture(m_textureColor);
  293. bgfx::destroyTexture(m_textureNormal);
  294. bgfx::destroyUniform(s_texColor);
  295. bgfx::destroyUniform(s_texNormal);
  296. bgfx::destroyUniform(s_albedo);
  297. bgfx::destroyUniform(s_normal);
  298. bgfx::destroyUniform(s_depth);
  299. bgfx::destroyUniform(s_light);
  300. bgfx::destroyUniform(u_lightPosRadius);
  301. bgfx::destroyUniform(u_lightRgbInnerR);
  302. bgfx::destroyUniform(u_mtx);
  303. // Shutdown bgfx.
  304. bgfx::shutdown();
  305. return 0;
  306. }
  307. bool update() BX_OVERRIDE
  308. {
  309. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  310. {
  311. int64_t now = bx::getHPCounter();
  312. static int64_t last = now;
  313. const int64_t frameTime = now - last;
  314. last = now;
  315. const double freq = double(bx::getHPFrequency() );
  316. const double toMs = 1000.0/freq;
  317. const float deltaTime = float(frameTime/freq);
  318. float time = (float)( (now-m_timeOffset)/freq);
  319. // Use m_debug font to print information about this example.
  320. bgfx::dbgTextClear();
  321. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/21-deferred");
  322. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: MRT rendering and deferred shading.");
  323. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  324. if (2 > m_caps->maxFBAttachments)
  325. {
  326. // When multiple render targets (MRT) is not supported by GPU,
  327. // implement alternative code path that doesn't use MRT.
  328. bool blink = uint32_t(time*3.0f)&1;
  329. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " MRT not supported by GPU. ");
  330. // Set view 0 default viewport.
  331. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  332. // This dummy draw call is here to make sure that view 0 is cleared
  333. // if no other draw calls are submitted to view 0.
  334. bgfx::touch(0);
  335. }
  336. else
  337. {
  338. if (m_oldWidth != m_width
  339. || m_oldHeight != m_height
  340. || m_oldReset != m_reset
  341. || !bgfx::isValid(m_gbuffer) )
  342. {
  343. // Recreate variable size render targets when resolution changes.
  344. m_oldWidth = m_width;
  345. m_oldHeight = m_height;
  346. m_oldReset = m_reset;
  347. if (bgfx::isValid(m_gbuffer) )
  348. {
  349. bgfx::destroyFrameBuffer(m_gbuffer);
  350. }
  351. const uint32_t samplerFlags = 0
  352. | BGFX_TEXTURE_RT
  353. | BGFX_TEXTURE_MIN_POINT
  354. | BGFX_TEXTURE_MAG_POINT
  355. | BGFX_TEXTURE_MIP_POINT
  356. | BGFX_TEXTURE_U_CLAMP
  357. | BGFX_TEXTURE_V_CLAMP
  358. ;
  359. m_gbufferTex[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  360. m_gbufferTex[1] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  361. m_gbufferTex[2] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::D24, samplerFlags);
  362. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
  363. if (bgfx::isValid(m_lightBuffer) )
  364. {
  365. bgfx::destroyFrameBuffer(m_lightBuffer);
  366. }
  367. m_lightBuffer = bgfx::createFrameBuffer(m_width, m_height, bgfx::TextureFormat::BGRA8, samplerFlags);
  368. }
  369. imguiBeginFrame(m_mouseState.m_mx
  370. , m_mouseState.m_my
  371. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  372. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  373. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  374. , m_mouseState.m_mz
  375. , m_width
  376. , m_height
  377. );
  378. imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
  379. imguiSeparatorLine();
  380. imguiSlider("Num lights", m_numLights, 1, 2048);
  381. if (imguiCheck("Show G-Buffer.", m_showGBuffer) )
  382. {
  383. m_showGBuffer = !m_showGBuffer;
  384. }
  385. if (imguiCheck("Show light scissor.", m_showScissorRects) )
  386. {
  387. m_showScissorRects = !m_showScissorRects;
  388. }
  389. if (imguiCheck("Animate mesh.", m_animateMesh) )
  390. {
  391. m_animateMesh = !m_animateMesh;
  392. }
  393. imguiSlider("Lights animation speed", m_lightAnimationSpeed, 0.0f, 0.4f, 0.01f);
  394. imguiEndScrollArea();
  395. imguiEndFrame();
  396. // Update camera.
  397. cameraUpdate(deltaTime, m_mouseState);
  398. float view[16];
  399. cameraGetViewMtx(view);
  400. // Setup views
  401. float vp[16];
  402. float invMvp[16];
  403. {
  404. bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, m_width, m_height);
  405. bgfx::setViewRect(RENDER_PASS_LIGHT_ID, 0, 0, m_width, m_height);
  406. bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, m_width, m_height);
  407. bgfx::setViewRect(RENDER_PASS_DEBUG_LIGHTS_ID, 0, 0, m_width, m_height);
  408. bgfx::setViewRect(RENDER_PASS_DEBUG_GBUFFER_ID, 0, 0, m_width, m_height);
  409. bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, m_lightBuffer);
  410. float proj[16];
  411. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, s_originBottomLeft);
  412. bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
  413. bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
  414. bx::mtxMul(vp, view, proj);
  415. bx::mtxInverse(invMvp, vp);
  416. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  417. bgfx::setViewTransform(RENDER_PASS_LIGHT_ID, NULL, proj);
  418. bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
  419. const float aspectRatio = float(m_height)/float(m_width);
  420. const float size = 10.0f;
  421. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  422. bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
  423. bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f);
  424. bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
  425. }
  426. const uint32_t dim = 11;
  427. const float offset = (float(dim-1) * 3.0f) * 0.5f;
  428. // Draw into geometry pass.
  429. for (uint32_t yy = 0; yy < dim; ++yy)
  430. {
  431. for (uint32_t xx = 0; xx < dim; ++xx)
  432. {
  433. float mtx[16];
  434. if (m_animateMesh)
  435. {
  436. bx::mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
  437. }
  438. else
  439. {
  440. bx::mtxIdentity(mtx);
  441. }
  442. mtx[12] = -offset + float(xx)*3.0f;
  443. mtx[13] = -offset + float(yy)*3.0f;
  444. mtx[14] = 0.0f;
  445. // Set transform for draw call.
  446. bgfx::setTransform(mtx);
  447. // Set vertex and index buffer.
  448. bgfx::setVertexBuffer(m_vbh);
  449. bgfx::setIndexBuffer(m_ibh);
  450. // Bind textures.
  451. bgfx::setTexture(0, s_texColor, m_textureColor);
  452. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  453. // Set render states.
  454. bgfx::setState(0
  455. | BGFX_STATE_RGB_WRITE
  456. | BGFX_STATE_ALPHA_WRITE
  457. | BGFX_STATE_DEPTH_WRITE
  458. | BGFX_STATE_DEPTH_TEST_LESS
  459. | BGFX_STATE_MSAA
  460. );
  461. // Submit primitive for rendering to view 0.
  462. bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
  463. }
  464. }
  465. // Draw lights into light buffer.
  466. for (int32_t light = 0; light < m_numLights; ++light)
  467. {
  468. Sphere lightPosRadius;
  469. float lightTime = time * m_lightAnimationSpeed * (sinf(light/float(m_numLights) * bx::piHalf ) * 0.5f + 0.5f);
  470. lightPosRadius.m_center[0] = sinf( ( (lightTime + light*0.47f) + bx::piHalf*1.37f ) )*offset;
  471. lightPosRadius.m_center[1] = cosf( ( (lightTime + light*0.69f) + bx::piHalf*1.49f ) )*offset;
  472. lightPosRadius.m_center[2] = sinf( ( (lightTime + light*0.37f) + bx::piHalf*1.57f ) )*2.0f;
  473. lightPosRadius.m_radius = 2.0f;
  474. Aabb aabb;
  475. sphereToAabb(aabb, lightPosRadius);
  476. float box[8][3] =
  477. {
  478. { aabb.m_min[0], aabb.m_min[1], aabb.m_min[2] },
  479. { aabb.m_min[0], aabb.m_min[1], aabb.m_max[2] },
  480. { aabb.m_min[0], aabb.m_max[1], aabb.m_min[2] },
  481. { aabb.m_min[0], aabb.m_max[1], aabb.m_max[2] },
  482. { aabb.m_max[0], aabb.m_min[1], aabb.m_min[2] },
  483. { aabb.m_max[0], aabb.m_min[1], aabb.m_max[2] },
  484. { aabb.m_max[0], aabb.m_max[1], aabb.m_min[2] },
  485. { aabb.m_max[0], aabb.m_max[1], aabb.m_max[2] },
  486. };
  487. float xyz[3];
  488. bx::vec3MulMtxH(xyz, box[0], vp);
  489. float minx = xyz[0];
  490. float miny = xyz[1];
  491. float maxx = xyz[0];
  492. float maxy = xyz[1];
  493. float maxz = xyz[2];
  494. for (uint32_t ii = 1; ii < 8; ++ii)
  495. {
  496. bx::vec3MulMtxH(xyz, box[ii], vp);
  497. minx = bx::fmin(minx, xyz[0]);
  498. miny = bx::fmin(miny, xyz[1]);
  499. maxx = bx::fmax(maxx, xyz[0]);
  500. maxy = bx::fmax(maxy, xyz[1]);
  501. maxz = bx::fmax(maxz, xyz[2]);
  502. }
  503. // Cull light if it's fully behind camera.
  504. if (maxz >= 0.0f)
  505. {
  506. float x0 = bx::fclamp( (minx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  507. float y0 = bx::fclamp( (miny * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  508. float x1 = bx::fclamp( (maxx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
  509. float y1 = bx::fclamp( (maxy * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
  510. if (m_showScissorRects)
  511. {
  512. bgfx::TransientVertexBuffer tvb;
  513. bgfx::TransientIndexBuffer tib;
  514. if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
  515. {
  516. uint32_t abgr = 0x8000ff00;
  517. DebugVertex* vertex = (DebugVertex*)tvb.data;
  518. vertex->m_x = x0;
  519. vertex->m_y = y0;
  520. vertex->m_z = 0.0f;
  521. vertex->m_abgr = abgr;
  522. ++vertex;
  523. vertex->m_x = x1;
  524. vertex->m_y = y0;
  525. vertex->m_z = 0.0f;
  526. vertex->m_abgr = abgr;
  527. ++vertex;
  528. vertex->m_x = x1;
  529. vertex->m_y = y1;
  530. vertex->m_z = 0.0f;
  531. vertex->m_abgr = abgr;
  532. ++vertex;
  533. vertex->m_x = x0;
  534. vertex->m_y = y1;
  535. vertex->m_z = 0.0f;
  536. vertex->m_abgr = abgr;
  537. uint16_t* indices = (uint16_t*)tib.data;
  538. *indices++ = 0;
  539. *indices++ = 1;
  540. *indices++ = 1;
  541. *indices++ = 2;
  542. *indices++ = 2;
  543. *indices++ = 3;
  544. *indices++ = 3;
  545. *indices++ = 0;
  546. bgfx::setVertexBuffer(&tvb);
  547. bgfx::setIndexBuffer(&tib);
  548. bgfx::setState(0
  549. | BGFX_STATE_RGB_WRITE
  550. | BGFX_STATE_PT_LINES
  551. | BGFX_STATE_BLEND_ALPHA
  552. );
  553. bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID, m_lineProgram);
  554. }
  555. }
  556. uint8_t val = light&7;
  557. float lightRgbInnerR[4] =
  558. {
  559. val & 0x1 ? 1.0f : 0.25f,
  560. val & 0x2 ? 1.0f : 0.25f,
  561. val & 0x4 ? 1.0f : 0.25f,
  562. 0.8f,
  563. };
  564. // Draw light.
  565. bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
  566. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
  567. bgfx::setUniform(u_mtx, invMvp);
  568. const uint16_t scissorHeight = uint16_t(y1-y0);
  569. bgfx::setScissor(uint16_t(x0), m_height-scissorHeight-uint16_t(y0), uint16_t(x1-x0), scissorHeight);
  570. bgfx::setTexture(0, s_normal, m_gbuffer, 1);
  571. bgfx::setTexture(1, s_depth, m_gbuffer, 2);
  572. bgfx::setState(0
  573. | BGFX_STATE_RGB_WRITE
  574. | BGFX_STATE_ALPHA_WRITE
  575. | BGFX_STATE_BLEND_ADD
  576. );
  577. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  578. bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram);
  579. }
  580. }
  581. // Combine color and light buffers.
  582. bgfx::setTexture(0, s_albedo, m_gbuffer, 0);
  583. bgfx::setTexture(1, s_light, m_lightBuffer, 0);
  584. bgfx::setState(0
  585. | BGFX_STATE_RGB_WRITE
  586. | BGFX_STATE_ALPHA_WRITE
  587. );
  588. screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, s_originBottomLeft);
  589. bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
  590. if (m_showGBuffer)
  591. {
  592. const float aspectRatio = float(m_width)/float(m_height);
  593. // Draw m_debug m_gbuffer.
  594. for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
  595. {
  596. float mtx[16];
  597. bx::mtxSRT(mtx
  598. , aspectRatio, 1.0f, 1.0f
  599. , 0.0f, 0.0f, 0.0f
  600. , -7.9f - BX_COUNTOF(m_gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
  601. );
  602. bgfx::setTransform(mtx);
  603. bgfx::setVertexBuffer(m_vbh);
  604. bgfx::setIndexBuffer(m_ibh, 0, 6);
  605. bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
  606. bgfx::setState(BGFX_STATE_RGB_WRITE);
  607. bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID, m_debugProgram);
  608. }
  609. }
  610. }
  611. // Advance to next frame. Rendering thread will be kicked to
  612. // process submitted rendering primitives.
  613. bgfx::frame();
  614. return true;
  615. }
  616. return false;
  617. }
  618. bgfx::VertexBufferHandle m_vbh;
  619. bgfx::IndexBufferHandle m_ibh;
  620. bgfx::UniformHandle s_texColor;
  621. bgfx::UniformHandle s_texNormal;
  622. bgfx::UniformHandle s_albedo;
  623. bgfx::UniformHandle s_normal;
  624. bgfx::UniformHandle s_depth;
  625. bgfx::UniformHandle s_light;
  626. bgfx::UniformHandle u_mtx;
  627. bgfx::UniformHandle u_lightPosRadius;
  628. bgfx::UniformHandle u_lightRgbInnerR;
  629. bgfx::ProgramHandle m_geomProgram;
  630. bgfx::ProgramHandle m_lightProgram;
  631. bgfx::ProgramHandle m_combineProgram;
  632. bgfx::ProgramHandle m_debugProgram;
  633. bgfx::ProgramHandle m_lineProgram;
  634. bgfx::TextureHandle m_textureColor;
  635. bgfx::TextureHandle m_textureNormal;
  636. bgfx::TextureHandle m_gbufferTex[3];
  637. bgfx::FrameBufferHandle m_gbuffer;
  638. bgfx::FrameBufferHandle m_lightBuffer;
  639. uint32_t m_width;
  640. uint32_t m_height;
  641. uint32_t m_debug;
  642. uint32_t m_reset;
  643. uint32_t m_oldWidth;
  644. uint32_t m_oldHeight;
  645. uint32_t m_oldReset;
  646. int32_t m_scrollArea;
  647. int32_t m_numLights;
  648. float m_lightAnimationSpeed;
  649. bool m_animateMesh;
  650. bool m_showScissorRects;
  651. bool m_showGBuffer;
  652. entry::MouseState m_mouseState;
  653. const bgfx::Caps* m_caps;
  654. int64_t m_timeOffset;
  655. };
  656. ENTRY_IMPLEMENT_MAIN(Deferred);