deferred.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright 2011-2014 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_GEOMETRY_BIT (1<<RENDER_PASS_GEOMETRY_ID)
  12. #define RENDER_PASS_LIGHT_ID 1
  13. #define RENDER_PASS_LIGHT_BIT (1<<RENDER_PASS_LIGHT_ID)
  14. #define RENDER_PASS_COMBINE_ID 2
  15. #define RENDER_PASS_COMBINE_BIT (1<<RENDER_PASS_COMBINE_ID)
  16. #define RENDER_PASS_DEBUG_LIGHTS_ID 3
  17. #define RENDER_PASS_DEBUG_LIGHTS_BIT (1<<RENDER_PASS_DEBUG_LIGHTS_ID)
  18. #define RENDER_PASS_DEBUG_GBUFFER_ID 4
  19. #define RENDER_PASS_DEBUG_GBUFFER_BIT (1<<RENDER_PASS_DEBUG_GBUFFER_ID)
  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 view 0 clear state.
  195. bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID
  196. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  197. , 0x303030ff
  198. , 1.0f
  199. , 0
  200. );
  201. bgfx::setViewClear(RENDER_PASS_LIGHT_ID
  202. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  203. , 0
  204. , 1.0f
  205. , 0
  206. );
  207. // Create vertex stream declaration.
  208. PosNormalTangentTexcoordVertex::init();
  209. PosTexCoord0Vertex::init();
  210. DebugVertex::init();
  211. calcTangents(s_cubeVertices
  212. , BX_COUNTOF(s_cubeVertices)
  213. , PosNormalTangentTexcoordVertex::ms_decl
  214. , s_cubeIndices
  215. , BX_COUNTOF(s_cubeIndices)
  216. );
  217. // Create static vertex buffer.
  218. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  219. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  220. , PosNormalTangentTexcoordVertex::ms_decl
  221. );
  222. // Create static index buffer.
  223. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  224. // Create texture sampler uniforms.
  225. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Uniform1iv);
  226. bgfx::UniformHandle s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Uniform1iv);
  227. bgfx::UniformHandle s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Uniform1iv);
  228. bgfx::UniformHandle s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Uniform1iv);
  229. bgfx::UniformHandle s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Uniform1iv);
  230. bgfx::UniformHandle s_light = bgfx::createUniform("s_light", bgfx::UniformType::Uniform1iv);
  231. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Uniform4x4fv);
  232. bgfx::UniformHandle u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Uniform4fv);
  233. bgfx::UniformHandle u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Uniform4fv);
  234. // Create program from shaders.
  235. bgfx::ProgramHandle geomProgram = loadProgram("vs_deferred_geom", "fs_deferred_geom");
  236. bgfx::ProgramHandle lightProgram = loadProgram("vs_deferred_light", "fs_deferred_light");
  237. bgfx::ProgramHandle combineProgram = loadProgram("vs_deferred_combine", "fs_deferred_combine");
  238. bgfx::ProgramHandle debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug");
  239. bgfx::ProgramHandle lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line");
  240. // Load diffuse texture.
  241. bgfx::TextureHandle textureColor = loadTexture("fieldstone-rgba.dds");
  242. // Load normal texture.
  243. bgfx::TextureHandle textureNormal = loadTexture("fieldstone-n.dds");
  244. bgfx::TextureHandle gbufferTex[3] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE };
  245. bgfx::FrameBufferHandle gbuffer = BGFX_INVALID_HANDLE;
  246. bgfx::FrameBufferHandle lightBuffer = BGFX_INVALID_HANDLE;
  247. void* data = load("font/droidsans.ttf");
  248. imguiCreate(data);
  249. free(data);
  250. const int64_t timeOffset = bx::getHPCounter();
  251. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  252. const float texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  253. const bool originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
  254. // Get renderer capabilities info.
  255. const bgfx::Caps* caps = bgfx::getCaps();
  256. uint32_t oldWidth = 0;
  257. uint32_t oldHeight = 0;
  258. uint32_t oldReset = reset;
  259. int32_t scrollArea = 0;
  260. int32_t numLights = 512;
  261. float lightAnimationSpeed = 0.3f;
  262. bool animateMesh = true;
  263. bool showScissorRects = false;
  264. bool showGBuffer = true;
  265. float view[16];
  266. float initialPos[3] = { 0.0f, 0.0f, -15.0f };
  267. cameraCreate();
  268. cameraSetPosition(initialPos);
  269. cameraSetVerticalAngle(0.0f);
  270. cameraGetViewMtx(view);
  271. entry::MouseState mouseState;
  272. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  273. {
  274. int64_t now = bx::getHPCounter();
  275. static int64_t last = now;
  276. const int64_t frameTime = now - last;
  277. last = now;
  278. const double freq = double(bx::getHPFrequency() );
  279. const double toMs = 1000.0/freq;
  280. const float deltaTime = float(frameTime/freq);
  281. float time = (float)( (now-timeOffset)/freq);
  282. // Use debug font to print information about this example.
  283. bgfx::dbgTextClear();
  284. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/21-deferred");
  285. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: MRT rendering and deferred shading.");
  286. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  287. if (2 > caps->maxFBAttachments)
  288. {
  289. // When multiple render targets (MRT) is not supported by GPU,
  290. // implement alternative code path that doesn't use MRT.
  291. bool blink = uint32_t(time*3.0f)&1;
  292. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " MRT not supported by GPU. ");
  293. // Set view 0 default viewport.
  294. bgfx::setViewRect(0, 0, 0, width, height);
  295. // This dummy draw call is here to make sure that view 0 is cleared
  296. // if no other draw calls are submitted to view 0.
  297. bgfx::submit(0);
  298. }
  299. else
  300. {
  301. if (oldWidth != width
  302. || oldHeight != height
  303. || oldReset != reset
  304. || !bgfx::isValid(gbuffer) )
  305. {
  306. // Recreate variable size render targets when resolution changes.
  307. oldWidth = width;
  308. oldHeight = height;
  309. oldReset = reset;
  310. if (bgfx::isValid(gbuffer) )
  311. {
  312. bgfx::destroyFrameBuffer(gbuffer);
  313. }
  314. const uint32_t samplerFlags = 0
  315. | BGFX_TEXTURE_RT
  316. | BGFX_TEXTURE_MIN_POINT
  317. | BGFX_TEXTURE_MAG_POINT
  318. | BGFX_TEXTURE_MIP_POINT
  319. | BGFX_TEXTURE_U_CLAMP
  320. | BGFX_TEXTURE_V_CLAMP
  321. ;
  322. gbufferTex[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  323. gbufferTex[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::BGRA8, samplerFlags);
  324. gbufferTex[2] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::D24, samplerFlags);
  325. gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(gbufferTex), gbufferTex, true);
  326. if (bgfx::isValid(lightBuffer) )
  327. {
  328. bgfx::destroyFrameBuffer(lightBuffer);
  329. }
  330. lightBuffer = bgfx::createFrameBuffer(width, height, bgfx::TextureFormat::BGRA8, samplerFlags);
  331. }
  332. imguiBeginFrame(mouseState.m_mx
  333. , mouseState.m_my
  334. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  335. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  336. , 0
  337. , width
  338. , height
  339. );
  340. imguiBeginScrollArea("Settings", width - width / 5 - 10, 10, width / 5, height / 3, &scrollArea);
  341. imguiSeparatorLine();
  342. imguiSlider("Num lights", &numLights, 1, 2048);
  343. if (imguiCheck("Show G-Buffer.", showGBuffer) )
  344. {
  345. showGBuffer = !showGBuffer;
  346. }
  347. if (imguiCheck("Show light scissor.", showScissorRects) )
  348. {
  349. showScissorRects = !showScissorRects;
  350. }
  351. if (imguiCheck("Animate mesh.", animateMesh) )
  352. {
  353. animateMesh = !animateMesh;
  354. }
  355. imguiSlider("Lights animation speed", &lightAnimationSpeed, 0.0f, 0.4f, 0.01f);
  356. imguiEndScrollArea();
  357. imguiEndFrame();
  358. // Update camera.
  359. cameraUpdate(deltaTime, mouseState.m_mx, mouseState.m_my, !!mouseState.m_buttons[entry::MouseButton::Right]);
  360. cameraGetViewMtx(view);
  361. // Setup views
  362. float vp[16];
  363. float invMvp[16];
  364. {
  365. bgfx::setViewRectMask(0
  366. | RENDER_PASS_GEOMETRY_BIT
  367. | RENDER_PASS_LIGHT_BIT
  368. | RENDER_PASS_COMBINE_BIT
  369. | RENDER_PASS_DEBUG_LIGHTS_BIT
  370. | RENDER_PASS_DEBUG_GBUFFER_BIT
  371. , 0, 0, width, height
  372. );
  373. bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, lightBuffer);
  374. float proj[16];
  375. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  376. bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, gbuffer);
  377. bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
  378. mtxMul(vp, view, proj);
  379. mtxInverse(invMvp, vp);
  380. mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  381. bgfx::setViewTransformMask(0
  382. | RENDER_PASS_LIGHT_BIT
  383. | RENDER_PASS_COMBINE_BIT
  384. , NULL, proj
  385. );
  386. const float aspectRatio = float(height)/float(width);
  387. const float size = 10.0f;
  388. mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  389. bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
  390. mtxOrtho(proj, 0.0f, (float)width, 0.0f, (float)height, 0.0f, 1000.0f);
  391. bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
  392. }
  393. const uint32_t dim = 11;
  394. const float offset = (float(dim-1) * 3.0f) * 0.5f;
  395. // Draw into geometry pass.
  396. for (uint32_t yy = 0; yy < dim; ++yy)
  397. {
  398. for (uint32_t xx = 0; xx < dim; ++xx)
  399. {
  400. float mtx[16];
  401. if (animateMesh)
  402. {
  403. mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
  404. }
  405. else
  406. {
  407. mtxIdentity(mtx);
  408. }
  409. mtx[12] = -offset + float(xx)*3.0f;
  410. mtx[13] = -offset + float(yy)*3.0f;
  411. mtx[14] = 0.0f;
  412. // Set transform for draw call.
  413. bgfx::setTransform(mtx);
  414. // Set vertex and fragment shaders.
  415. bgfx::setProgram(geomProgram);
  416. // Set vertex and index buffer.
  417. bgfx::setVertexBuffer(vbh);
  418. bgfx::setIndexBuffer(ibh);
  419. // Bind textures.
  420. bgfx::setTexture(0, s_texColor, textureColor);
  421. bgfx::setTexture(1, s_texNormal, textureNormal);
  422. // Set render states.
  423. bgfx::setState(0
  424. | BGFX_STATE_RGB_WRITE
  425. | BGFX_STATE_ALPHA_WRITE
  426. | BGFX_STATE_DEPTH_WRITE
  427. | BGFX_STATE_DEPTH_TEST_LESS
  428. | BGFX_STATE_MSAA
  429. );
  430. // Submit primitive for rendering to view 0.
  431. bgfx::submit(RENDER_PASS_GEOMETRY_ID);
  432. }
  433. }
  434. // Draw lights into light buffer.
  435. for (int32_t light = 0; light < numLights; ++light)
  436. {
  437. Sphere lightPosRadius;
  438. float lightTime = time * lightAnimationSpeed * (sin(light/float(numLights) * float(M_PI_2) ) * 0.5f + 0.5f);
  439. lightPosRadius.m_center[0] = sin( ( (lightTime + light*0.47f) + float(M_PI_2)*1.37f ) )*offset;
  440. lightPosRadius.m_center[1] = cos( ( (lightTime + light*0.69f) + float(M_PI_2)*1.49f ) )*offset;
  441. lightPosRadius.m_center[2] = sin( ( (lightTime + light*0.37f) + float(M_PI_2)*1.57f ) )*2.0f;
  442. lightPosRadius.m_radius = 2.0f;
  443. Aabb aabb;
  444. sphereToAabb(aabb, lightPosRadius);
  445. float box[8][3] =
  446. {
  447. aabb.m_min[0], aabb.m_min[1], aabb.m_min[2],
  448. aabb.m_min[0], aabb.m_min[1], aabb.m_max[2],
  449. aabb.m_min[0], aabb.m_max[1], aabb.m_min[2],
  450. aabb.m_min[0], aabb.m_max[1], aabb.m_max[2],
  451. aabb.m_max[0], aabb.m_min[1], aabb.m_min[2],
  452. aabb.m_max[0], aabb.m_min[1], aabb.m_max[2],
  453. aabb.m_max[0], aabb.m_max[1], aabb.m_min[2],
  454. aabb.m_max[0], aabb.m_max[1], aabb.m_max[2],
  455. };
  456. float xyz[3];
  457. vec3MulMtxH(xyz, box[0], vp);
  458. float minx = xyz[0];
  459. float miny = xyz[1];
  460. float maxx = xyz[0];
  461. float maxy = xyz[1];
  462. float maxz = xyz[2];
  463. for (uint32_t ii = 1; ii < 8; ++ii)
  464. {
  465. vec3MulMtxH(xyz, box[ii], vp);
  466. minx = fminf(minx, xyz[0]);
  467. miny = fminf(miny, xyz[1]);
  468. maxx = fmaxf(maxx, xyz[0]);
  469. maxy = fmaxf(maxy, xyz[1]);
  470. maxz = fmaxf(maxz, xyz[2]);
  471. }
  472. // Cull light if it's fully behind camera.
  473. if (maxz >= 0.0f)
  474. {
  475. float x0 = fclamp( (minx * 0.5f + 0.5f) * width, 0.0f, (float)width);
  476. float y0 = fclamp( (miny * 0.5f + 0.5f) * height, 0.0f, (float)height);
  477. float x1 = fclamp( (maxx * 0.5f + 0.5f) * width, 0.0f, (float)width);
  478. float y1 = fclamp( (maxy * 0.5f + 0.5f) * height, 0.0f, (float)height);
  479. if (showScissorRects)
  480. {
  481. bgfx::TransientVertexBuffer tvb;
  482. bgfx::TransientIndexBuffer tib;
  483. if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
  484. {
  485. uint32_t abgr = 0x8000ff00;
  486. DebugVertex* vertex = (DebugVertex*)tvb.data;
  487. vertex->m_x = x0;
  488. vertex->m_y = y0;
  489. vertex->m_z = 0.0f;
  490. vertex->m_abgr = abgr;
  491. ++vertex;
  492. vertex->m_x = x1;
  493. vertex->m_y = y0;
  494. vertex->m_z = 0.0f;
  495. vertex->m_abgr = abgr;
  496. ++vertex;
  497. vertex->m_x = x1;
  498. vertex->m_y = y1;
  499. vertex->m_z = 0.0f;
  500. vertex->m_abgr = abgr;
  501. ++vertex;
  502. vertex->m_x = x0;
  503. vertex->m_y = y1;
  504. vertex->m_z = 0.0f;
  505. vertex->m_abgr = abgr;
  506. uint16_t* indices = (uint16_t*)tib.data;
  507. *indices++ = 0;
  508. *indices++ = 1;
  509. *indices++ = 1;
  510. *indices++ = 2;
  511. *indices++ = 2;
  512. *indices++ = 3;
  513. *indices++ = 3;
  514. *indices++ = 0;
  515. bgfx::setProgram(lineProgram);
  516. bgfx::setVertexBuffer(&tvb);
  517. bgfx::setIndexBuffer(&tib);
  518. bgfx::setState(0
  519. | BGFX_STATE_RGB_WRITE
  520. | BGFX_STATE_PT_LINES
  521. | BGFX_STATE_BLEND_ALPHA
  522. );
  523. bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID);
  524. }
  525. }
  526. uint8_t val = light&7;
  527. float lightRgbInnerR[4] =
  528. {
  529. val & 0x1 ? 1.0f : 0.25f,
  530. val & 0x2 ? 1.0f : 0.25f,
  531. val & 0x4 ? 1.0f : 0.25f,
  532. 0.8f,
  533. };
  534. // Draw light.
  535. bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
  536. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
  537. bgfx::setUniform(u_mtx, invMvp);
  538. const uint16_t scissorHeight = uint16_t(y1-y0);
  539. bgfx::setScissor(uint16_t(x0), height-scissorHeight-uint16_t(y0), uint16_t(x1-x0), scissorHeight);
  540. bgfx::setTexture(0, s_normal, gbuffer, 1);
  541. bgfx::setTexture(1, s_depth, gbuffer, 2);
  542. bgfx::setProgram(lightProgram);
  543. bgfx::setState(0
  544. | BGFX_STATE_RGB_WRITE
  545. | BGFX_STATE_ALPHA_WRITE
  546. | BGFX_STATE_BLEND_ADD
  547. );
  548. screenSpaceQuad( (float)width, (float)height, texelHalf, originBottomLeft);
  549. bgfx::submit(RENDER_PASS_LIGHT_ID);
  550. }
  551. }
  552. // Combine color and light buffers.
  553. bgfx::setTexture(0, s_albedo, gbuffer, 0);
  554. bgfx::setTexture(1, s_light, lightBuffer, 0);
  555. bgfx::setProgram(combineProgram);
  556. bgfx::setState(0
  557. | BGFX_STATE_RGB_WRITE
  558. | BGFX_STATE_ALPHA_WRITE
  559. );
  560. screenSpaceQuad( (float)width, (float)height, texelHalf, originBottomLeft);
  561. bgfx::submit(RENDER_PASS_COMBINE_ID);
  562. if (showGBuffer)
  563. {
  564. const float aspectRatio = float(width)/float(height);
  565. // Draw debug GBuffer.
  566. for (uint32_t ii = 0; ii < BX_COUNTOF(gbufferTex); ++ii)
  567. {
  568. float mtx[16];
  569. mtxSRT(mtx
  570. , aspectRatio, 1.0f, 1.0f
  571. , 0.0f, 0.0f, 0.0f
  572. , -7.9f - BX_COUNTOF(gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
  573. );
  574. bgfx::setTransform(mtx);
  575. bgfx::setProgram(debugProgram);
  576. bgfx::setVertexBuffer(vbh);
  577. bgfx::setIndexBuffer(ibh, 0, 6);
  578. bgfx::setTexture(0, s_texColor, gbufferTex[ii]);
  579. bgfx::setState(BGFX_STATE_RGB_WRITE);
  580. bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID);
  581. }
  582. }
  583. }
  584. // Advance to next frame. Rendering thread will be kicked to
  585. // process submitted rendering primitives.
  586. bgfx::frame();
  587. }
  588. // Cleanup.
  589. cameraDestroy();
  590. imguiDestroy();
  591. bgfx::destroyFrameBuffer(gbuffer);
  592. bgfx::destroyFrameBuffer(lightBuffer);
  593. bgfx::destroyIndexBuffer(ibh);
  594. bgfx::destroyVertexBuffer(vbh);
  595. bgfx::destroyProgram(geomProgram);
  596. bgfx::destroyProgram(lightProgram);
  597. bgfx::destroyProgram(combineProgram);
  598. bgfx::destroyProgram(debugProgram);
  599. bgfx::destroyProgram(lineProgram);
  600. bgfx::destroyTexture(textureColor);
  601. bgfx::destroyTexture(textureNormal);
  602. bgfx::destroyUniform(s_texColor);
  603. bgfx::destroyUniform(s_texNormal);
  604. bgfx::destroyUniform(s_albedo);
  605. bgfx::destroyUniform(s_normal);
  606. bgfx::destroyUniform(s_depth);
  607. bgfx::destroyUniform(s_light);
  608. bgfx::destroyUniform(u_lightPosRadius);
  609. bgfx::destroyUniform(u_lightRgbInnerR);
  610. bgfx::destroyUniform(u_mtx);
  611. // Shutdown bgfx.
  612. bgfx::shutdown();
  613. return 0;
  614. }