deferred.cpp 22 KB

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