assao.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * Copyright 2018 Attila Kocsis. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. /*
  6. * Reference(s):
  7. * - ASSAO is a SSAO implementation tuned for scalability and flexibility.
  8. * https://web.archive.org/web/20181214222937/https://software.intel.com/en-us/articles/adaptive-screen-space-ambient-occlusion
  9. * https://github.com/GameTechDev/ASSAO
  10. */
  11. #include <common.h>
  12. #include <camera.h>
  13. #include <bgfx_utils.h>
  14. #include <imgui/imgui.h>
  15. #include <bx/rng.h>
  16. #include <bx/os.h>
  17. #define USE_ASSAO 0
  18. namespace
  19. {
  20. // Render passes
  21. #define RENDER_PASS_GBUFFER 0 // GBuffer for normals and albedo
  22. #define RENDER_PASS_COMBINE 1 // Directional light and final result
  23. // Gbuffer has multiple render targets
  24. #define GBUFFER_RT_NORMAL 0
  25. #define GBUFFER_RT_COLOR 1
  26. #define GBUFFER_RT_DEPTH 2
  27. // Random meshes we draw
  28. #define MODEL_COUNT 120 // In this demo, a model is a mesh plus a transform
  29. #define SAMPLER_POINT_CLAMP (BGFX_SAMPLER_POINT|BGFX_SAMPLER_UVW_CLAMP)
  30. #define SAMPLER_POINT_MIRROR (BGFX_SAMPLER_POINT|BGFX_SAMPLER_UVW_MIRROR)
  31. #define SAMPLER_LINEAR_CLAMP (BGFX_SAMPLER_UVW_CLAMP)
  32. #define SSAO_DEPTH_MIP_LEVELS 4
  33. static const char * s_meshPaths[] =
  34. {
  35. "meshes/cube.bin",
  36. "meshes/orb.bin",
  37. "meshes/column.bin",
  38. "meshes/bunny_decimated.bin",
  39. "meshes/tree.bin",
  40. "meshes/hollowcube.bin"
  41. };
  42. static const float s_meshScale[] =
  43. {
  44. 0.25f,
  45. 0.5f,
  46. 0.05f,
  47. 0.5f,
  48. 0.05f,
  49. 0.25f
  50. };
  51. // Vertex layout for our screen space quad (used in deferred rendering)
  52. struct PosTexCoord0Vertex
  53. {
  54. float m_x;
  55. float m_y;
  56. float m_z;
  57. float m_u;
  58. float m_v;
  59. static void init()
  60. {
  61. ms_layout
  62. .begin()
  63. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  64. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  65. .end();
  66. }
  67. static bgfx::VertexLayout ms_layout;
  68. };
  69. bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
  70. // Utility function to draw a screen space quad for deferred rendering
  71. void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  72. {
  73. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout))
  74. {
  75. bgfx::TransientVertexBuffer vb;
  76. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
  77. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
  78. const float minx = -_width;
  79. const float maxx = _width;
  80. const float miny = 0.0f;
  81. const float maxy = _height * 2.0f;
  82. const float texelHalfW = _texelHalf / _textureWidth;
  83. const float texelHalfH = _texelHalf / _textureHeight;
  84. const float minu = -1.0f + texelHalfW;
  85. const float maxu = 1.0f + texelHalfH;
  86. const float zz = 0.0f;
  87. float minv = texelHalfH;
  88. float maxv = 2.0f + texelHalfH;
  89. if (_originBottomLeft)
  90. {
  91. float temp = minv;
  92. minv = maxv;
  93. maxv = temp;
  94. minv -= 1.0f;
  95. maxv -= 1.0f;
  96. }
  97. vertex[0].m_x = minx;
  98. vertex[0].m_y = miny;
  99. vertex[0].m_z = zz;
  100. vertex[0].m_u = minu;
  101. vertex[0].m_v = minv;
  102. vertex[1].m_x = maxx;
  103. vertex[1].m_y = miny;
  104. vertex[1].m_z = zz;
  105. vertex[1].m_u = maxu;
  106. vertex[1].m_v = minv;
  107. vertex[2].m_x = maxx;
  108. vertex[2].m_y = maxy;
  109. vertex[2].m_z = zz;
  110. vertex[2].m_u = maxu;
  111. vertex[2].m_v = maxv;
  112. bgfx::setVertexBuffer(0, &vb);
  113. }
  114. }
  115. struct Settings
  116. {
  117. float m_radius; // [0.0, ~ ] World (view) space size of the occlusion sphere.
  118. float m_shadowMultiplier; // [0.0, 5.0] Effect strength linear multiplier
  119. float m_shadowPower; // [0.5, 5.0] Effect strength pow modifier
  120. float m_shadowClamp; // [0.0, 1.0] Effect max limit (applied after multiplier but before blur)
  121. float m_horizonAngleThreshold; // [0.0, 0.2] Limits self-shadowing (makes the sampling area less of a hemisphere, more of a spherical cone, to avoid self-shadowing and various artifacts due to low tessellation and depth buffer imprecision, etc.)
  122. float m_fadeOutFrom; // [0.0, ~ ] Distance to start start fading out the effect.
  123. float m_fadeOutTo; // [0.0, ~ ] Distance at which the effect is faded out.
  124. int32_t m_qualityLevel; // [ -1, 3 ] Effect quality; -1 - lowest (low, half res checkerboard), 0 - low, 1 - medium, 2 - high, 3 - very high / adaptive; each quality level is roughly 2x more costly than the previous, except the q3 which is variable but, in general, above q2.
  125. float m_adaptiveQualityLimit; // [0.0, 1.0] (only for Quality Level 3)
  126. int32_t m_blurPassCount; // [ 0, 6] Number of edge-sensitive smart blur passes to apply. Quality 0 is an exception with only one 'dumb' blur pass used.
  127. float m_sharpness; // [0.0, 1.0] (How much to bleed over edges; 1: not at all, 0.5: half-half; 0.0: completely ignore edges)
  128. float m_temporalSupersamplingAngleOffset; // [0.0, PI] Used to rotate sampling kernel; If using temporal AA / supersampling, suggested to rotate by ( (frame%3)/3.0*PI ) or similar. Kernel is already symmetrical, which is why we use PI and not 2*PI.
  129. float m_temporalSupersamplingRadiusOffset; // [0.0, 2.0] Used to scale sampling kernel; If using temporal AA / supersampling, suggested to scale by ( 1.0f + (((frame%3)-1.0)/3.0)*0.1 ) or similar.
  130. float m_detailShadowStrength; // [0.0, 5.0] Used for high-res detail AO using neighboring depth pixels: adds a lot of detail but also reduces temporal stability (adds aliasing).
  131. bool m_generateNormals; // [true/false] If true normals will be generated from depth.
  132. Settings()
  133. {
  134. m_radius = 1.2f;
  135. m_shadowMultiplier = 1.0f;
  136. m_shadowPower = 1.50f;
  137. m_shadowClamp = 0.98f;
  138. m_horizonAngleThreshold = 0.06f;
  139. m_fadeOutFrom = 50.0f;
  140. m_fadeOutTo = 200.0f;
  141. m_adaptiveQualityLimit = 0.45f;
  142. m_qualityLevel = 3;
  143. m_blurPassCount = 2;
  144. m_sharpness = 0.98f;
  145. m_temporalSupersamplingAngleOffset = 0.0f;
  146. m_temporalSupersamplingRadiusOffset = 1.0f;
  147. m_detailShadowStrength = 0.5f;
  148. m_generateNormals = true;
  149. }
  150. };
  151. struct Uniforms
  152. {
  153. enum { NumVec4 = 19 };
  154. void init()
  155. {
  156. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4);
  157. }
  158. void submit()
  159. {
  160. bgfx::setUniform(u_params, m_params, NumVec4);
  161. }
  162. void destroy()
  163. {
  164. bgfx::destroy(u_params);
  165. }
  166. union
  167. {
  168. struct
  169. {
  170. /* 0 */ struct { float m_viewportPixelSize[2]; float m_halfViewportPixelSize[2]; };
  171. /* 1 */ struct { float m_depthUnpackConsts[2]; float m_unused0[2]; };
  172. /* 2 */ struct { float m_ndcToViewMul[2]; float m_ndcToViewAdd[2]; };
  173. /* 3 */ struct { float m_perPassFullResCoordOffset[2]; float m_perPassFullResUVOffset[2]; };
  174. /* 4 */ struct { float m_viewport2xPixelSize[2]; float m_viewport2xPixelSize_x_025[2]; };
  175. /* 5 */ struct { float m_effectRadius; float m_effectShadowStrength; float m_effectShadowPow; float m_effectShadowClamp; };
  176. /* 6 */ struct { float m_effectFadeOutMul; float m_effectFadeOutAdd; float m_effectHorizonAngleThreshold; float m_effectSamplingRadiusNearLimitRec; };
  177. /* 7 */ struct { float m_depthPrecisionOffsetMod; float m_negRecEffectRadius; float m_loadCounterAvgDiv; float m_adaptiveSampleCountLimit; };
  178. /* 8 */ struct { float m_invSharpness; float m_passIndex; float m_quarterResPixelSize[2]; };
  179. /* 9-13 */ struct { float m_patternRotScaleMatrices[5][4]; };
  180. /* 14 */ struct { float m_normalsUnpackMul; float m_normalsUnpackAdd; float m_detailAOStrength; float m_layer; };
  181. /* 15-18 */ struct { float m_normalsWorldToViewspaceMatrix[16]; };
  182. };
  183. float m_params[NumVec4 * 4];
  184. };
  185. bgfx::UniformHandle u_params;
  186. };
  187. void vec2Set(float* _v, float _x, float _y)
  188. {
  189. _v[0] = _x;
  190. _v[1] = _y;
  191. }
  192. void vec4Set(float* _v, float _x, float _y, float _z, float _w)
  193. {
  194. _v[0] = _x;
  195. _v[1] = _y;
  196. _v[2] = _z;
  197. _v[3] = _w;
  198. }
  199. void vec4iSet(int32_t* _v, int32_t _x, int32_t _y, int32_t _z, int32_t _w)
  200. {
  201. _v[0] = _x;
  202. _v[1] = _y;
  203. _v[2] = _z;
  204. _v[3] = _w;
  205. }
  206. static const int32_t cMaxBlurPassCount = 6;
  207. class ExampleASSAO : public entry::AppI
  208. {
  209. public:
  210. ExampleASSAO(const char* _name, const char* _description, const char* _url)
  211. : entry::AppI(_name, _description, _url)
  212. , m_currFrame(UINT32_MAX)
  213. , m_enableSSAO(true)
  214. , m_enableTexturing(true)
  215. , m_texelHalf(0.0f)
  216. , m_framebufferGutter(true)
  217. {
  218. }
  219. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  220. {
  221. Args args(_argc, _argv);
  222. m_width = _width;
  223. m_height = _height;
  224. m_debug = BGFX_DEBUG_NONE;
  225. m_reset = BGFX_RESET_VSYNC;
  226. bgfx::Init init;
  227. init.type = args.m_type;
  228. init.vendorId = args.m_pciId;
  229. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  230. init.platformData.ndt = entry::getNativeDisplayHandle();
  231. init.resolution.width = m_width;
  232. init.resolution.height = m_height;
  233. init.resolution.reset = m_reset;
  234. bgfx::init(init);
  235. // Enable debug text.
  236. bgfx::setDebug(m_debug);
  237. // Labeling for renderdoc captures, etc
  238. bgfx::setViewName(RENDER_PASS_GBUFFER, "gbuffer");
  239. bgfx::setViewName(RENDER_PASS_COMBINE, "post combine");
  240. // Set up screen clears
  241. bgfx::setViewClear(RENDER_PASS_GBUFFER
  242. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  243. , 0
  244. , 1.0f
  245. , 0
  246. );
  247. // Create uniforms
  248. u_combineParams = bgfx::createUniform("u_combineParams", bgfx::UniformType::Vec4, 2);
  249. u_rect = bgfx::createUniform("u_rect", bgfx::UniformType::Vec4); // viewport/scissor rect for compute
  250. m_uniforms.init();
  251. // Create texture sampler uniforms (used when we bind textures)
  252. s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler); // Normal gbuffer
  253. s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler); // Normal gbuffer
  254. s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler); // Color (albedo) gbuffer
  255. s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
  256. s_ao = bgfx::createUniform("s_ao", bgfx::UniformType::Sampler);
  257. s_blurInput = bgfx::createUniform("s_blurInput", bgfx::UniformType::Sampler);
  258. s_finalSSAO = bgfx::createUniform("s_finalSSAO", bgfx::UniformType::Sampler);
  259. s_depthSource = bgfx::createUniform("s_depthSource", bgfx::UniformType::Sampler);
  260. s_viewspaceDepthSource = bgfx::createUniform("s_viewspaceDepthSource", bgfx::UniformType::Sampler);
  261. s_viewspaceDepthSourceMirror = bgfx::createUniform("s_viewspaceDepthSourceMirror", bgfx::UniformType::Sampler);
  262. s_importanceMap = bgfx::createUniform("s_importanceMap", bgfx::UniformType::Sampler);
  263. // Create program from shaders.
  264. m_gbufferProgram = loadProgram("vs_assao_gbuffer", "fs_assao_gbuffer"); // Gbuffer
  265. m_combineProgram = loadProgram("vs_assao", "fs_assao_deferred_combine");
  266. m_prepareDepthsProgram = loadProgram("cs_assao_prepare_depths", NULL);
  267. m_prepareDepthsAndNormalsProgram = loadProgram("cs_assao_prepare_depths_and_normals", NULL);
  268. m_prepareDepthsHalfProgram = loadProgram("cs_assao_prepare_depths_half", NULL);
  269. m_prepareDepthsAndNormalsHalfProgram = loadProgram("cs_assao_prepare_depths_and_normals_half", NULL);
  270. m_prepareDepthMipProgram = loadProgram("cs_assao_prepare_depth_mip", NULL);
  271. m_generateQ0Program = loadProgram("cs_assao_generate_q0", NULL);
  272. m_generateQ1Program = loadProgram("cs_assao_generate_q1", NULL);
  273. m_generateQ2Program = loadProgram("cs_assao_generate_q2", NULL);
  274. m_generateQ3Program = loadProgram("cs_assao_generate_q3", NULL);
  275. m_generateQ3BaseProgram = loadProgram("cs_assao_generate_q3base", NULL);
  276. m_smartBlurProgram = loadProgram("cs_assao_smart_blur", NULL);
  277. m_smartBlurWideProgram = loadProgram("cs_assao_smart_blur_wide", NULL);
  278. m_nonSmartBlurProgram = loadProgram("cs_assao_non_smart_blur", NULL);
  279. m_applyProgram = loadProgram("cs_assao_apply", NULL);
  280. m_nonSmartApplyProgram = loadProgram("cs_assao_non_smart_apply", NULL);
  281. m_nonSmartHalfApplyProgram = loadProgram("cs_assao_non_smart_half_apply", NULL);
  282. m_generateImportanceMapProgram = loadProgram("cs_assao_generate_importance_map", NULL);
  283. m_postprocessImportanceMapAProgram = loadProgram("cs_assao_postprocess_importance_map_a", NULL);
  284. m_postprocessImportanceMapBProgram = loadProgram("cs_assao_postprocess_importance_map_b", NULL);
  285. m_loadCounterClearProgram = loadProgram("cs_assao_load_counter_clear", NULL);
  286. // Load some meshes
  287. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  288. {
  289. m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  290. }
  291. // Randomly create some models
  292. bx::RngMwc mwc; // Random number generator
  293. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  294. {
  295. Model& model = m_models[ii];
  296. model.mesh = 1 + mwc.gen() % (BX_COUNTOF(s_meshPaths) - 1);
  297. model.position[0] = (((mwc.gen() % 256)) - 128.0f) / 20.0f;
  298. model.position[1] = 0;
  299. model.position[2] = (((mwc.gen() % 256)) - 128.0f) / 20.0f;
  300. }
  301. // Load ground. We'll just use the cube since I don't have a ground model right now
  302. m_ground = meshLoad("meshes/cube.bin");
  303. m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
  304. const bgfx::Memory* mem = bgfx::alloc(4);
  305. bx::memSet(mem->data, 0xc0, 4);
  306. m_modelTexture = bgfx::createTexture2D(1,1, false, 1, bgfx::TextureFormat::RGBA8, 0, mem);
  307. m_recreateFrameBuffers = false;
  308. createFramebuffers();
  309. m_loadCounter = bgfx::createDynamicIndexBuffer(1, BGFX_BUFFER_COMPUTE_READ_WRITE | BGFX_BUFFER_INDEX32);
  310. // Vertex layout
  311. PosTexCoord0Vertex::init();
  312. // Init camera
  313. cameraCreate();
  314. cameraSetPosition({ 0.0f, 1.5f, 0.0f });
  315. cameraSetVerticalAngle(-0.3f);
  316. m_fovY = 60.0f;
  317. // Get renderer capabilities info.
  318. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  319. m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  320. imguiCreate();
  321. }
  322. int32_t shutdown() override
  323. {
  324. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  325. {
  326. meshUnload(m_meshes[ii]);
  327. }
  328. meshUnload(m_ground);
  329. bgfx::destroy(m_groundTexture);
  330. bgfx::destroy(m_modelTexture);
  331. // Cleanup.
  332. bgfx::destroy(m_gbufferProgram);
  333. bgfx::destroy(m_combineProgram);
  334. bgfx::destroy(m_prepareDepthsProgram);
  335. bgfx::destroy(m_prepareDepthsAndNormalsProgram);
  336. bgfx::destroy(m_prepareDepthsHalfProgram);
  337. bgfx::destroy(m_prepareDepthsAndNormalsHalfProgram);
  338. bgfx::destroy(m_prepareDepthMipProgram);
  339. bgfx::destroy(m_generateQ0Program);
  340. bgfx::destroy(m_generateQ1Program);
  341. bgfx::destroy(m_generateQ2Program);
  342. bgfx::destroy(m_generateQ3Program);
  343. bgfx::destroy(m_generateQ3BaseProgram);
  344. bgfx::destroy(m_smartBlurProgram);
  345. bgfx::destroy(m_smartBlurWideProgram);
  346. bgfx::destroy(m_nonSmartBlurProgram);
  347. bgfx::destroy(m_applyProgram);
  348. bgfx::destroy(m_nonSmartApplyProgram);
  349. bgfx::destroy(m_nonSmartHalfApplyProgram);
  350. bgfx::destroy(m_generateImportanceMapProgram);
  351. bgfx::destroy(m_postprocessImportanceMapAProgram);
  352. bgfx::destroy(m_postprocessImportanceMapBProgram);
  353. bgfx::destroy(m_loadCounterClearProgram);
  354. bgfx::destroy(m_combineProgram);
  355. m_uniforms.destroy();
  356. bgfx::destroy(u_combineParams);
  357. bgfx::destroy(u_rect);
  358. bgfx::destroy(s_normal);
  359. bgfx::destroy(s_depth);
  360. bgfx::destroy(s_color);
  361. bgfx::destroy(s_albedo);
  362. bgfx::destroy(s_ao);
  363. bgfx::destroy(s_blurInput);
  364. bgfx::destroy(s_finalSSAO);
  365. bgfx::destroy(s_depthSource);
  366. bgfx::destroy(s_viewspaceDepthSource);
  367. bgfx::destroy(s_viewspaceDepthSourceMirror);
  368. bgfx::destroy(s_importanceMap);
  369. bgfx::destroy(m_loadCounter);
  370. destroyFramebuffers();
  371. cameraDestroy();
  372. imguiDestroy();
  373. // Shutdown bgfx.
  374. bgfx::shutdown();
  375. return 0;
  376. }
  377. bool update() override
  378. {
  379. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState))
  380. {
  381. // Update frame timer
  382. int64_t now = bx::getHPCounter();
  383. static int64_t last = now;
  384. const int64_t frameTime = now - last;
  385. last = now;
  386. const double freq = double(bx::getHPFrequency());
  387. const float deltaTime = float(frameTime / freq);
  388. const bgfx::Caps* caps = bgfx::getCaps();
  389. if (m_size[0] != (int32_t)m_width + 2*m_border
  390. || m_size[1] != (int32_t)m_height + 2*m_border
  391. || m_recreateFrameBuffers)
  392. {
  393. destroyFramebuffers();
  394. createFramebuffers();
  395. m_recreateFrameBuffers = false;
  396. }
  397. // Update camera
  398. cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
  399. // Set up matrices for gbuffer
  400. cameraGetViewMtx(m_view);
  401. bx::mtxProj(m_proj, m_fovY, float(m_size[0]) / float(m_size[1]), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  402. bx::mtxProj(m_proj2, m_fovY, float(m_size[0]) / float(m_size[1]), 0.1f, 100.0f, false);
  403. bgfx::setViewRect(RENDER_PASS_GBUFFER, 0, 0, uint16_t(m_size[0]), uint16_t(m_size[1]));
  404. bgfx::setViewTransform(RENDER_PASS_GBUFFER, m_view, m_proj);
  405. // Make sure when we draw it goes into gbuffer and not backbuffer
  406. bgfx::setViewFrameBuffer(RENDER_PASS_GBUFFER, m_gbuffer);
  407. // Draw everything into g-buffer
  408. drawAllModels(RENDER_PASS_GBUFFER, m_gbufferProgram);
  409. // Set up transform matrix for fullscreen quad
  410. #if USE_ASSAO == 1
  411. float orthoProj[16];
  412. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  413. bgfx::setViewTransform(RENDER_PASS_COMBINE, NULL, orthoProj);
  414. bgfx::setViewRect(RENDER_PASS_COMBINE, 0, 0, uint16_t(m_width), uint16_t(m_height));
  415. // Bind vertex buffer and draw quad
  416. screenSpaceQuad((float)m_width, (float)m_height, m_texelHalf, caps->originBottomLeft);
  417. //bgfx::submit(RENDER_PASS_COMBINE, m_combineProgram);
  418. bgfx::touch(RENDER_PASS_COMBINE);
  419. BX_UNUSED(orthoProj, caps)
  420. #endif
  421. // ASSAO passes
  422. #if USE_ASSAO == 0
  423. updateUniforms(0);
  424. bgfx::ViewId view = 2;
  425. bgfx::setViewName(view, "ASSAO");
  426. {
  427. bgfx::setTexture(0, s_depthSource, bgfx::getTexture(m_gbuffer, GBUFFER_RT_DEPTH), SAMPLER_POINT_CLAMP);
  428. m_uniforms.submit();
  429. if (m_settings.m_generateNormals)
  430. {
  431. bgfx::setImage(5, m_normals, 0, bgfx::Access::Write, bgfx::TextureFormat::RGBA8);
  432. }
  433. if (m_settings.m_qualityLevel < 0)
  434. {
  435. for (int32_t j = 0; j < 2; ++j)
  436. {
  437. bgfx::setImage((uint8_t)(j + 1), m_halfDepths[j == 0 ? 0 : 3], 0, bgfx::Access::Write, bgfx::TextureFormat::R16F);
  438. }
  439. bgfx::dispatch(view, m_settings.m_generateNormals ? m_prepareDepthsAndNormalsHalfProgram : m_prepareDepthsHalfProgram, (m_halfSize[0] + 7) / 8, (m_halfSize[1] + 7) / 8);
  440. }
  441. else
  442. {
  443. for(int32_t j = 0; j < 4; ++j)
  444. {
  445. bgfx::setImage((uint8_t)(j+1), m_halfDepths[j], 0, bgfx::Access::Write, bgfx::TextureFormat::R16F);
  446. }
  447. bgfx::dispatch(view, m_settings.m_generateNormals ? m_prepareDepthsAndNormalsProgram : m_prepareDepthsProgram, (m_halfSize[0] + 7) / 8, (m_halfSize[1] + 7) / 8);
  448. }
  449. }
  450. // only do mipmaps for higher quality levels (not beneficial on quality level 1, and detrimental on quality level 0)
  451. if (m_settings.m_qualityLevel > 1)
  452. {
  453. uint16_t mipWidth = (uint16_t)m_halfSize[0];
  454. uint16_t mipHeight = (uint16_t)m_halfSize[1];
  455. for (uint8_t i = 1; i < SSAO_DEPTH_MIP_LEVELS; i++)
  456. {
  457. mipWidth = (uint16_t)bx::max(1, mipWidth >> 1);
  458. mipHeight = (uint16_t)bx::max(1, mipHeight >> 1);
  459. for (uint8_t j = 0; j < 4; ++j)
  460. {
  461. bgfx::setImage(j, m_halfDepths[j], i-1, bgfx::Access::Read, bgfx::TextureFormat::R16F);
  462. bgfx::setImage(j + 4, m_halfDepths[j], i, bgfx::Access::Write, bgfx::TextureFormat::R16F);
  463. }
  464. m_uniforms.submit();
  465. float rect[4] = { 0.0f, 0.0f, (float)mipWidth, (float)mipHeight };
  466. bgfx::setUniform(u_rect, rect);
  467. bgfx::dispatch(view, m_prepareDepthMipProgram, (mipWidth + 7) / 8, (mipHeight + 7) / 8);
  468. }
  469. }
  470. // for adaptive quality, importance map pass
  471. for (int32_t ssaoPass = 0; ssaoPass < 2; ++ssaoPass)
  472. {
  473. if (ssaoPass == 0
  474. && m_settings.m_qualityLevel < 3)
  475. {
  476. continue;
  477. }
  478. bool adaptiveBasePass = (ssaoPass == 0);
  479. BX_UNUSED(adaptiveBasePass);
  480. int32_t passCount = 4;
  481. int32_t halfResNumX = (m_halfResOutScissorRect[2] - m_halfResOutScissorRect[0] + 7) / 8;
  482. int32_t halfResNumY = (m_halfResOutScissorRect[3] - m_halfResOutScissorRect[1] + 7) / 8;
  483. float halfResRect[4] = { (float)m_halfResOutScissorRect[0], (float)m_halfResOutScissorRect[1], (float)m_halfResOutScissorRect[2], (float)m_halfResOutScissorRect[3] };
  484. for (int32_t pass = 0; pass < passCount; pass++)
  485. {
  486. if (m_settings.m_qualityLevel < 0
  487. && (pass == 1 || pass == 2) )
  488. {
  489. continue;
  490. }
  491. int32_t blurPasses = m_settings.m_blurPassCount;
  492. blurPasses = bx::min(blurPasses, cMaxBlurPassCount);
  493. if (m_settings.m_qualityLevel == 3)
  494. {
  495. // if adaptive, at least one blur pass needed as the first pass needs to read the final texture results - kind of awkward
  496. if (adaptiveBasePass)
  497. {
  498. blurPasses = 0;
  499. }
  500. else
  501. {
  502. blurPasses = bx::max(1, blurPasses);
  503. }
  504. }
  505. else if (m_settings.m_qualityLevel <= 0)
  506. {
  507. // just one blur pass allowed for minimum quality
  508. blurPasses = bx::min(1, m_settings.m_blurPassCount);
  509. }
  510. updateUniforms(pass);
  511. bgfx::TextureHandle pPingRT = m_pingPongHalfResultA;
  512. bgfx::TextureHandle pPongRT = m_pingPongHalfResultB;
  513. // Generate
  514. {
  515. bgfx::setImage(6, blurPasses == 0 ? m_finalResults : pPingRT, 0, bgfx::Access::Write, bgfx::TextureFormat::RG8);
  516. bgfx::setUniform(u_rect, halfResRect);
  517. bgfx::setTexture(0, s_viewspaceDepthSource, m_halfDepths[pass], SAMPLER_POINT_CLAMP);
  518. bgfx::setTexture(1, s_viewspaceDepthSourceMirror, m_halfDepths[pass], SAMPLER_POINT_MIRROR);
  519. if (m_settings.m_generateNormals)
  520. bgfx::setImage(2, m_normals,0, bgfx::Access::Read, bgfx::TextureFormat::RGBA8);
  521. else
  522. bgfx::setImage(2, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL), 0, bgfx::Access::Read, bgfx::TextureFormat::RGBA8);
  523. if (!adaptiveBasePass && (m_settings.m_qualityLevel == 3))
  524. {
  525. bgfx::setBuffer(3, m_loadCounter, bgfx::Access::Read);
  526. bgfx::setTexture(4, s_importanceMap, m_importanceMap, SAMPLER_LINEAR_CLAMP);
  527. bgfx::setImage(5, m_finalResults, 0, bgfx::Access::Read, bgfx::TextureFormat::RG8);
  528. }
  529. bgfx::ProgramHandle programs[5] = { m_generateQ0Program, m_generateQ1Program , m_generateQ2Program , m_generateQ3Program , m_generateQ3BaseProgram };
  530. int32_t programIndex = bx::max(0, (!adaptiveBasePass) ? (m_settings.m_qualityLevel) : (4));
  531. m_uniforms.m_layer = blurPasses == 0 ? (float)pass : 0.0f;
  532. m_uniforms.submit();
  533. bgfx::dispatch(view, programs[programIndex], halfResNumX, halfResNumY);
  534. }
  535. // Blur
  536. if (blurPasses > 0)
  537. {
  538. int32_t wideBlursRemaining = bx::max(0, blurPasses - 2);
  539. for (int32_t i = 0; i < blurPasses; i++)
  540. {
  541. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  542. bgfx::touch(view);
  543. m_uniforms.m_layer = ((i == (blurPasses - 1)) ? (float)pass : 0.0f);
  544. m_uniforms.submit();
  545. bgfx::setUniform(u_rect, halfResRect);
  546. bgfx::setImage(0, i == (blurPasses - 1) ? m_finalResults : pPongRT, 0, bgfx::Access::Write, bgfx::TextureFormat::RG8);
  547. bgfx::setTexture(1, s_blurInput, pPingRT, m_settings.m_qualityLevel > 0 ? SAMPLER_POINT_MIRROR : SAMPLER_LINEAR_CLAMP);
  548. if (m_settings.m_qualityLevel > 0)
  549. {
  550. if (wideBlursRemaining > 0)
  551. {
  552. bgfx::dispatch(view, m_smartBlurWideProgram, halfResNumX, halfResNumY);
  553. wideBlursRemaining--;
  554. }
  555. else
  556. {
  557. bgfx::dispatch(view, m_smartBlurProgram, halfResNumX, halfResNumY);
  558. }
  559. }
  560. else
  561. {
  562. bgfx::dispatch(view, m_nonSmartBlurProgram, halfResNumX, halfResNumY); // just for quality level 0 (and -1)
  563. }
  564. bgfx::TextureHandle temp = pPingRT;
  565. pPingRT = pPongRT;
  566. pPongRT = temp;
  567. }
  568. }
  569. }
  570. if (ssaoPass == 0 && m_settings.m_qualityLevel == 3)
  571. { // Generate importance map
  572. m_uniforms.submit();
  573. bgfx::setImage(0, m_importanceMap, 0, bgfx::Access::Write, bgfx::TextureFormat::R8);
  574. bgfx::setTexture(1, s_finalSSAO, m_finalResults, SAMPLER_POINT_CLAMP);
  575. bgfx::dispatch(view, m_generateImportanceMapProgram, (m_quarterSize[0] + 7) / 8, (m_quarterSize[1] + 7) / 8);
  576. m_uniforms.submit();
  577. bgfx::setImage(0, m_importanceMapPong, 0, bgfx::Access::Write, bgfx::TextureFormat::R8);
  578. bgfx::setTexture(1, s_importanceMap, m_importanceMap);
  579. bgfx::dispatch(view, m_postprocessImportanceMapAProgram, (m_quarterSize[0] + 7) / 8, (m_quarterSize[1] + 7) / 8);
  580. bgfx::setBuffer(0, m_loadCounter, bgfx::Access::ReadWrite);
  581. bgfx::dispatch(view, m_loadCounterClearProgram, 1,1);
  582. m_uniforms.submit();
  583. bgfx::setImage(0, m_importanceMap, 0, bgfx::Access::Write, bgfx::TextureFormat::R8);
  584. bgfx::setTexture(1, s_importanceMap, m_importanceMapPong);
  585. bgfx::setBuffer(2, m_loadCounter, bgfx::Access::ReadWrite);
  586. bgfx::dispatch(view, m_postprocessImportanceMapBProgram, (m_quarterSize[0]+7) / 8, (m_quarterSize[1]+7) / 8);
  587. ++view;
  588. }
  589. }
  590. // Apply
  591. {
  592. // select 4 deinterleaved AO textures (texture array)
  593. bgfx::setImage(0, m_aoMap, 0, bgfx::Access::Write, bgfx::TextureFormat::R8);
  594. bgfx::setTexture(1, s_finalSSAO, m_finalResults);
  595. m_uniforms.submit();
  596. float rect[4] = {(float)m_fullResOutScissorRect[0], (float)m_fullResOutScissorRect[1], (float)m_fullResOutScissorRect[2], (float)m_fullResOutScissorRect[3] };
  597. bgfx::setUniform(u_rect, rect);
  598. bgfx::ProgramHandle program;
  599. if (m_settings.m_qualityLevel < 0)
  600. program = m_nonSmartHalfApplyProgram;
  601. else if (m_settings.m_qualityLevel == 0)
  602. program = m_nonSmartApplyProgram;
  603. else
  604. program = m_applyProgram;
  605. bgfx::dispatch(view, program, (m_fullResOutScissorRect[2]- m_fullResOutScissorRect[0] + 7) / 8,
  606. (m_fullResOutScissorRect[3] - m_fullResOutScissorRect[1] + 7) / 8);
  607. ++view;
  608. }
  609. { // combine
  610. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  611. bgfx::setViewName(view, "Combine");
  612. bgfx::setViewRect(view, 0, 0, (uint16_t)m_width, (uint16_t)m_height);
  613. float orthoProj[16];
  614. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  615. bgfx::setViewTransform(view, NULL, orthoProj);
  616. bgfx::setTexture(0, s_color, bgfx::getTexture(m_gbuffer, GBUFFER_RT_COLOR), SAMPLER_POINT_CLAMP);
  617. bgfx::setTexture(1, s_normal, bgfx::getTexture(m_gbuffer, GBUFFER_RT_NORMAL), SAMPLER_POINT_CLAMP);
  618. bgfx::setTexture(2, s_ao, m_aoMap, SAMPLER_POINT_CLAMP);
  619. m_uniforms.submit();
  620. float combineParams[8] = { m_enableTexturing ? 1.0f : 0.0f, m_enableSSAO ? 1.0f : 0.0f, 0.0f,0.0f,
  621. (float)(m_size[0]-2*m_border) / (float)m_size[0], (float)(m_size[1] - 2 * m_border) / (float)m_size[1],
  622. (float)m_border / (float)m_size[0], (float)m_border / (float)m_size[1] };
  623. bgfx::setUniform(u_combineParams, combineParams, 2);
  624. screenSpaceQuad((float)m_width, (float)m_height, m_texelHalf, caps->originBottomLeft);
  625. bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
  626. bgfx::submit(view, m_combineProgram);
  627. ++view;
  628. }
  629. #endif
  630. // Draw UI
  631. imguiBeginFrame(m_mouseState.m_mx
  632. , m_mouseState.m_my
  633. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  634. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  635. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  636. , m_mouseState.m_mz
  637. , uint16_t(m_width)
  638. , uint16_t(m_height)
  639. );
  640. showExampleDialog(this);
  641. ImGui::SetNextWindowPos(
  642. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  643. , ImGuiCond_FirstUseEver
  644. );
  645. ImGui::SetNextWindowSize(
  646. ImVec2(m_width / 4.0f, m_height / 1.3f)
  647. , ImGuiCond_FirstUseEver
  648. );
  649. ImGui::Begin("Settings"
  650. , NULL
  651. , 0
  652. );
  653. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  654. ImGui::Checkbox("Enable SSAO", &m_enableSSAO);
  655. ImGui::Checkbox("Enable Texturing & Lighting", &m_enableTexturing);
  656. ImGui::Separator();
  657. int32_t quality = m_settings.m_qualityLevel + 1;
  658. if (ImGui::Combo("Quality Level", &quality, "Lowest (Half Resolution)\0Low\0Medium\0High\0Adaptive\0\0"))
  659. {
  660. m_settings.m_qualityLevel = quality - 1;
  661. }
  662. ImGui::Checkbox("Generate Normals", &m_settings.m_generateNormals);
  663. if (ImGui::Checkbox("Framebuffer Gutter", &m_framebufferGutter))
  664. {
  665. m_recreateFrameBuffers = true;
  666. }
  667. ImGui::SliderFloat("Effect Radius", &m_settings.m_radius, 0.0f, 4.0f);
  668. ImGui::SliderFloat("Effect Strength", &m_settings.m_shadowMultiplier, 0.0f, 5.0f);
  669. ImGui::SliderFloat("Effect Power", &m_settings.m_shadowPower, 0.5f, 4.0f);
  670. ImGui::SliderFloat("Effect Max Limit", &m_settings.m_shadowClamp, 0.0f, 1.0f);
  671. ImGui::SliderFloat("Horizon Angle Threshold", &m_settings.m_horizonAngleThreshold, 0.0f, 0.2f);
  672. ImGui::SliderFloat("Fade Out From", &m_settings.m_fadeOutFrom, 0.0f, 100.0f);
  673. ImGui::SliderFloat("Fade Out To", &m_settings.m_fadeOutTo, 0.0f, 300.0f);
  674. if (m_settings.m_qualityLevel == 3)
  675. {
  676. ImGui::SliderFloat("Adaptive Quality Limit", &m_settings.m_adaptiveQualityLimit, 0.0f, 1.0f);
  677. }
  678. ImGui::SliderInt("Blur Pass Count", &m_settings.m_blurPassCount, 0, 6);
  679. ImGui::SliderFloat("Sharpness", &m_settings.m_sharpness, 0.0f, 1.0f);
  680. ImGui::SliderFloat("Temporal Supersampling Angle Offset", &m_settings.m_temporalSupersamplingAngleOffset, 0.0f, bx::kPi);
  681. ImGui::SliderFloat("Temporal Supersampling Radius Offset", &m_settings.m_temporalSupersamplingRadiusOffset, 0.0f, 2.0f);
  682. ImGui::SliderFloat("Detail Shadow Strength", &m_settings.m_detailShadowStrength, 0.0f, 4.0f);
  683. ImGui::End();
  684. imguiEndFrame();
  685. // Advance to next frame. Rendering thread will be kicked to
  686. // process submitted rendering primitives.
  687. m_currFrame = bgfx::frame();
  688. return true;
  689. }
  690. return false;
  691. }
  692. void drawAllModels(uint8_t _pass, bgfx::ProgramHandle _program)
  693. {
  694. for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
  695. {
  696. const Model& model = m_models[ii];
  697. // Set up transform matrix for each model
  698. float scale = s_meshScale[model.mesh];
  699. float mtx[16];
  700. bx::mtxSRT(mtx
  701. , scale
  702. , scale
  703. , scale
  704. , 0.0f
  705. , 0.0f
  706. , 0.0f
  707. , model.position[0]
  708. , model.position[1]
  709. , model.position[2]
  710. );
  711. // Submit mesh to gbuffer
  712. bgfx::setTexture(0, s_albedo, m_modelTexture);
  713. meshSubmit(m_meshes[model.mesh], _pass, _program, mtx);
  714. }
  715. // Draw ground
  716. float mtxScale[16];
  717. const float scale = 10.0f;
  718. bx::mtxScale(mtxScale, scale, scale, scale);
  719. float mtxTrans[16];
  720. bx::mtxTranslate(mtxTrans
  721. , 0.0f
  722. , -10.0f
  723. , 0.0f
  724. );
  725. float mtx[16];
  726. bx::mtxMul(mtx, mtxScale, mtxTrans);
  727. bgfx::setTexture(0, s_albedo, m_groundTexture);
  728. meshSubmit(m_ground, _pass, _program, mtx);
  729. }
  730. void createFramebuffers()
  731. {
  732. // update resolution and camera FOV if there's border expansion
  733. const int32_t drawResolutionBorderExpansionFactor = 12; // will be expanded by Height / expansionFactor
  734. const float fovY = 60.0f;
  735. m_border = 0;
  736. if (m_framebufferGutter)
  737. {
  738. m_border = (bx::min(m_width, m_height) / drawResolutionBorderExpansionFactor) / 2 * 2;
  739. int32_t expandedSceneResolutionY = m_height + m_border * 2;
  740. float yScaleDueToBorder = (expandedSceneResolutionY * 0.5f) / (float)(m_height * 0.5f);
  741. float nonExpandedTan = bx::tan(bx::toRad(fovY / 2.0f));
  742. m_fovY = bx::toDeg(bx::atan(nonExpandedTan * yScaleDueToBorder) * 2.0f);
  743. }
  744. else
  745. {
  746. m_fovY = fovY;
  747. }
  748. m_size[0] = m_width + 2 * m_border;
  749. m_size[1] = m_height + 2 * m_border;
  750. m_halfSize[0] = (m_size[0] + 1) / 2;
  751. m_halfSize[1] = (m_size[1] + 1) / 2;
  752. m_quarterSize[0] = (m_halfSize[0] + 1) / 2;
  753. m_quarterSize[1] = (m_halfSize[1] + 1) / 2;
  754. vec4iSet(m_fullResOutScissorRect, m_border, m_border, m_width + m_border, m_height + m_border);
  755. vec4iSet(m_halfResOutScissorRect, m_fullResOutScissorRect[0] / 2, m_fullResOutScissorRect[1] / 2, (m_fullResOutScissorRect[2] + 1) / 2, (m_fullResOutScissorRect[3] + 1) / 2);
  756. int32_t blurEnlarge = cMaxBlurPassCount + bx::max(0, cMaxBlurPassCount - 2); // +1 for max normal blurs, +2 for wide blurs
  757. vec4iSet(m_halfResOutScissorRect, bx::max(0, m_halfResOutScissorRect[0] - blurEnlarge), bx::max(0, m_halfResOutScissorRect[1] - blurEnlarge),
  758. bx::min(m_halfSize[0], m_halfResOutScissorRect[2] + blurEnlarge), bx::min(m_halfSize[1], m_halfResOutScissorRect[3] + blurEnlarge));
  759. // Make gbuffer and related textures
  760. const uint64_t tsFlags = 0
  761. | BGFX_TEXTURE_RT
  762. | BGFX_SAMPLER_MIN_POINT
  763. | BGFX_SAMPLER_MAG_POINT
  764. | BGFX_SAMPLER_MIP_POINT
  765. | BGFX_SAMPLER_U_CLAMP
  766. | BGFX_SAMPLER_V_CLAMP
  767. ;
  768. bgfx::TextureHandle gbufferTex[3];
  769. gbufferTex[GBUFFER_RT_NORMAL] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
  770. gbufferTex[GBUFFER_RT_COLOR] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
  771. gbufferTex[GBUFFER_RT_DEPTH] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::D32F, tsFlags);
  772. m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(gbufferTex), gbufferTex, true);
  773. for (int32_t i = 0; i < 4; i++)
  774. {
  775. m_halfDepths[i] = bgfx::createTexture2D(uint16_t(m_halfSize[0]), uint16_t(m_halfSize[1]), true, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_COMPUTE_WRITE | SAMPLER_POINT_CLAMP);
  776. }
  777. m_pingPongHalfResultA = bgfx::createTexture2D(uint16_t(m_halfSize[0]), uint16_t(m_halfSize[1]), false, 2, bgfx::TextureFormat::RG8, BGFX_TEXTURE_COMPUTE_WRITE);
  778. m_pingPongHalfResultB = bgfx::createTexture2D(uint16_t(m_halfSize[0]), uint16_t(m_halfSize[1]), false, 2, bgfx::TextureFormat::RG8, BGFX_TEXTURE_COMPUTE_WRITE);
  779. m_finalResults = bgfx::createTexture2D(uint16_t(m_halfSize[0]), uint16_t(m_halfSize[1]), false, 4, bgfx::TextureFormat::RG8, BGFX_TEXTURE_COMPUTE_WRITE | SAMPLER_LINEAR_CLAMP);
  780. m_normals = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_COMPUTE_WRITE);
  781. m_importanceMap = bgfx::createTexture2D(uint16_t(m_quarterSize[0]), uint16_t(m_quarterSize[1]), false, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_COMPUTE_WRITE | SAMPLER_LINEAR_CLAMP);
  782. m_importanceMapPong = bgfx::createTexture2D(uint16_t(m_quarterSize[0]), uint16_t(m_quarterSize[1]), false, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_COMPUTE_WRITE | SAMPLER_LINEAR_CLAMP);
  783. m_aoMap = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_COMPUTE_WRITE | SAMPLER_POINT_CLAMP);
  784. }
  785. void destroyFramebuffers()
  786. {
  787. bgfx::destroy(m_gbuffer);
  788. for (uint32_t ii = 0; ii < BX_COUNTOF(m_halfDepths); ++ii)
  789. {
  790. bgfx::destroy(m_halfDepths[ii]);
  791. }
  792. bgfx::destroy(m_pingPongHalfResultA);
  793. bgfx::destroy(m_pingPongHalfResultB);
  794. bgfx::destroy(m_finalResults);
  795. bgfx::destroy(m_normals);
  796. bgfx::destroy(m_aoMap);
  797. bgfx::destroy(m_importanceMap);
  798. bgfx::destroy(m_importanceMapPong);
  799. }
  800. void updateUniforms(int32_t _pass)
  801. {
  802. vec2Set(m_uniforms.m_viewportPixelSize, 1.0f / (float)m_size[0], 1.0f / (float)m_size[1]);
  803. vec2Set(m_uniforms.m_halfViewportPixelSize, 1.0f / (float)m_halfSize[0], 1.0f / (float)m_halfSize[1]);
  804. vec2Set(m_uniforms.m_viewport2xPixelSize, m_uniforms.m_viewportPixelSize[0] * 2.0f, m_uniforms.m_viewportPixelSize[1] * 2.0f);
  805. vec2Set(m_uniforms.m_viewport2xPixelSize_x_025, m_uniforms.m_viewport2xPixelSize[0] * 0.25f, m_uniforms.m_viewport2xPixelSize[1] * 0.25f);
  806. float depthLinearizeMul = -m_proj2[3*4+2]; // float depthLinearizeMul = ( clipFar * clipNear ) / ( clipFar - clipNear );
  807. float depthLinearizeAdd = m_proj2[2*4+2]; // float depthLinearizeAdd = clipFar / ( clipFar - clipNear );
  808. // correct the handedness issue. need to make sure this below is correct, but I think it is.
  809. if (depthLinearizeMul * depthLinearizeAdd < 0)
  810. {
  811. depthLinearizeAdd = -depthLinearizeAdd;
  812. }
  813. vec2Set(m_uniforms.m_depthUnpackConsts, depthLinearizeMul, depthLinearizeAdd);
  814. float tanHalfFOVY = 1.0f / m_proj2[1*4+1]; // = tanf( drawContext.Camera.GetYFOV( ) * 0.5f );
  815. float tanHalfFOVX = 1.0F / m_proj2[0]; // = tanHalfFOVY * drawContext.Camera.GetAspect( );
  816. if (bgfx::getRendererType() == bgfx::RendererType::OpenGL)
  817. {
  818. vec2Set(m_uniforms.m_ndcToViewMul, tanHalfFOVX * 2.0f, tanHalfFOVY * 2.0f);
  819. vec2Set(m_uniforms.m_ndcToViewAdd, tanHalfFOVX * -1.0f, tanHalfFOVY * -1.0f);
  820. }
  821. else
  822. {
  823. vec2Set(m_uniforms.m_ndcToViewMul, tanHalfFOVX * 2.0f, tanHalfFOVY * -2.0f);
  824. vec2Set(m_uniforms.m_ndcToViewAdd, tanHalfFOVX * -1.0f, tanHalfFOVY * 1.0f);
  825. }
  826. m_uniforms.m_effectRadius = bx::clamp(m_settings.m_radius, 0.0f, 100000.0f);
  827. m_uniforms.m_effectShadowStrength = bx::clamp(m_settings.m_shadowMultiplier * 4.3f, 0.0f, 10.0f);
  828. m_uniforms.m_effectShadowPow = bx::clamp(m_settings.m_shadowPower, 0.0f, 10.0f);
  829. m_uniforms.m_effectShadowClamp = bx::clamp(m_settings.m_shadowClamp, 0.0f, 1.0f);
  830. m_uniforms.m_effectFadeOutMul = -1.0f / (m_settings.m_fadeOutTo - m_settings.m_fadeOutFrom);
  831. m_uniforms.m_effectFadeOutAdd = m_settings.m_fadeOutFrom / (m_settings.m_fadeOutTo - m_settings.m_fadeOutFrom) + 1.0f;
  832. m_uniforms.m_effectHorizonAngleThreshold = bx::clamp(m_settings.m_horizonAngleThreshold, 0.0f, 1.0f);
  833. // 1.2 seems to be around the best trade off - 1.0 means on-screen radius will stop/slow growing when the camera is at 1.0 distance, so, depending on FOV, basically filling up most of the screen
  834. // This setting is viewspace-dependent and not screen size dependent intentionally, so that when you change FOV the effect stays (relatively) similar.
  835. float effectSamplingRadiusNearLimit = (m_settings.m_radius * 1.2f);
  836. // if the depth precision is switched to 32bit float, this can be set to something closer to 1 (0.9999 is fine)
  837. m_uniforms.m_depthPrecisionOffsetMod = 0.9992f;
  838. // used to get average load per pixel; 9.0 is there to compensate for only doing every 9th InterlockedAdd in PSPostprocessImportanceMapB for performance reasons
  839. m_uniforms.m_loadCounterAvgDiv = 9.0f / (float)(m_quarterSize[0] * m_quarterSize[1] * 255.0);
  840. // Special settings for lowest quality level - just nerf the effect a tiny bit
  841. if (m_settings.m_qualityLevel <= 0)
  842. {
  843. effectSamplingRadiusNearLimit *= 1.50f;
  844. if (m_settings.m_qualityLevel < 0)
  845. {
  846. m_uniforms.m_effectRadius *= 0.8f;
  847. }
  848. }
  849. effectSamplingRadiusNearLimit /= tanHalfFOVY; // to keep the effect same regardless of FOV
  850. m_uniforms.m_effectSamplingRadiusNearLimitRec = 1.0f / effectSamplingRadiusNearLimit;
  851. m_uniforms.m_adaptiveSampleCountLimit = m_settings.m_adaptiveQualityLimit;
  852. m_uniforms.m_negRecEffectRadius = -1.0f / m_uniforms.m_effectRadius;
  853. if (bgfx::getCaps()->originBottomLeft)
  854. {
  855. vec2Set(m_uniforms.m_perPassFullResCoordOffset, (float)(_pass % 2), 1.0f-(float)(_pass / 2));
  856. vec2Set(m_uniforms.m_perPassFullResUVOffset, ((_pass % 2) - 0.0f) / m_size[0], (1.0f-((_pass / 2) - 0.0f)) / m_size[1]);
  857. }
  858. else
  859. {
  860. vec2Set(m_uniforms.m_perPassFullResCoordOffset, (float)(_pass % 2), (float)(_pass / 2));
  861. vec2Set(m_uniforms.m_perPassFullResUVOffset, ((_pass % 2) - 0.0f) / m_size[0], ((_pass / 2) - 0.0f) / m_size[1]);
  862. }
  863. m_uniforms.m_invSharpness = bx::clamp(1.0f - m_settings.m_sharpness, 0.0f, 1.0f);
  864. m_uniforms.m_passIndex = (float)_pass;
  865. vec2Set(m_uniforms.m_quarterResPixelSize, 1.0f / (float)m_quarterSize[0], 1.0f / (float)m_quarterSize[1]);
  866. float additionalAngleOffset = m_settings.m_temporalSupersamplingAngleOffset; // if using temporal supersampling approach (like "Progressive Rendering Using Multi-frame Sampling" from GPU Pro 7, etc.)
  867. float additionalRadiusScale = m_settings.m_temporalSupersamplingRadiusOffset; // if using temporal supersampling approach (like "Progressive Rendering Using Multi-frame Sampling" from GPU Pro 7, etc.)
  868. const int32_t subPassCount = 5;
  869. for (int32_t subPass = 0; subPass < subPassCount; subPass++)
  870. {
  871. int32_t a = _pass;
  872. int32_t b = subPass;
  873. int32_t spmap[5]{ 0, 1, 4, 3, 2 };
  874. b = spmap[subPass];
  875. float ca, sa;
  876. float angle0 = ((float)a + (float)b / (float)subPassCount) * (3.1415926535897932384626433832795f) * 0.5f;
  877. angle0 += additionalAngleOffset;
  878. ca = bx::cos(angle0);
  879. sa = bx::sin(angle0);
  880. float scale = 1.0f + (a - 1.5f + (b - (subPassCount - 1.0f) * 0.5f) / (float)subPassCount) * 0.07f;
  881. scale *= additionalRadiusScale;
  882. vec4Set(m_uniforms.m_patternRotScaleMatrices[subPass], scale * ca, scale * -sa, -scale * sa, -scale * ca);
  883. }
  884. m_uniforms.m_normalsUnpackMul = 2.0f;
  885. m_uniforms.m_normalsUnpackAdd = -1.0f;
  886. m_uniforms.m_detailAOStrength = m_settings.m_detailShadowStrength;
  887. if (m_settings.m_generateNormals)
  888. {
  889. bx::mtxIdentity(m_uniforms.m_normalsWorldToViewspaceMatrix);
  890. }
  891. else
  892. {
  893. bx::mtxTranspose(m_uniforms.m_normalsWorldToViewspaceMatrix, m_view);
  894. }
  895. }
  896. uint32_t m_width;
  897. uint32_t m_height;
  898. uint32_t m_debug;
  899. uint32_t m_reset;
  900. entry::MouseState m_mouseState;
  901. Uniforms m_uniforms;
  902. // Resource handles
  903. bgfx::ProgramHandle m_gbufferProgram;
  904. bgfx::ProgramHandle m_combineProgram;
  905. bgfx::ProgramHandle m_prepareDepthsProgram;
  906. bgfx::ProgramHandle m_prepareDepthsAndNormalsProgram;
  907. bgfx::ProgramHandle m_prepareDepthsHalfProgram;
  908. bgfx::ProgramHandle m_prepareDepthsAndNormalsHalfProgram;
  909. bgfx::ProgramHandle m_prepareDepthMipProgram;
  910. bgfx::ProgramHandle m_generateQ0Program;
  911. bgfx::ProgramHandle m_generateQ1Program;
  912. bgfx::ProgramHandle m_generateQ2Program;
  913. bgfx::ProgramHandle m_generateQ3Program;
  914. bgfx::ProgramHandle m_generateQ3BaseProgram;
  915. bgfx::ProgramHandle m_smartBlurProgram;
  916. bgfx::ProgramHandle m_smartBlurWideProgram;
  917. bgfx::ProgramHandle m_nonSmartBlurProgram;
  918. bgfx::ProgramHandle m_applyProgram;
  919. bgfx::ProgramHandle m_nonSmartApplyProgram;
  920. bgfx::ProgramHandle m_nonSmartHalfApplyProgram;
  921. bgfx::ProgramHandle m_generateImportanceMapProgram;
  922. bgfx::ProgramHandle m_postprocessImportanceMapAProgram;
  923. bgfx::ProgramHandle m_postprocessImportanceMapBProgram;
  924. bgfx::ProgramHandle m_loadCounterClearProgram;
  925. bgfx::FrameBufferHandle m_gbuffer;
  926. // Shader uniforms
  927. bgfx::UniformHandle u_rect;
  928. bgfx::UniformHandle u_combineParams;
  929. // Uniforms to identify texture samples
  930. bgfx::UniformHandle s_normal;
  931. bgfx::UniformHandle s_depth;
  932. bgfx::UniformHandle s_color;
  933. bgfx::UniformHandle s_albedo;
  934. bgfx::UniformHandle s_ao;
  935. bgfx::UniformHandle s_blurInput;
  936. bgfx::UniformHandle s_finalSSAO;
  937. bgfx::UniformHandle s_depthSource;
  938. bgfx::UniformHandle s_viewspaceDepthSource;
  939. bgfx::UniformHandle s_viewspaceDepthSourceMirror;
  940. bgfx::UniformHandle s_importanceMap;
  941. // Various render targets
  942. bgfx::TextureHandle m_halfDepths[4];
  943. bgfx::TextureHandle m_pingPongHalfResultA;
  944. bgfx::TextureHandle m_pingPongHalfResultB;
  945. bgfx::TextureHandle m_finalResults;
  946. bgfx::TextureHandle m_aoMap;
  947. bgfx::TextureHandle m_normals;
  948. // Only needed for quality level 3 (adaptive quality)
  949. bgfx::TextureHandle m_importanceMap;
  950. bgfx::TextureHandle m_importanceMapPong;
  951. bgfx::DynamicIndexBufferHandle m_loadCounter;
  952. struct Model
  953. {
  954. uint32_t mesh; // Index of mesh in m_meshes
  955. float position[3];
  956. };
  957. Model m_models[MODEL_COUNT];
  958. Mesh* m_meshes[BX_COUNTOF(s_meshPaths)];
  959. Mesh* m_ground;
  960. bgfx::TextureHandle m_groundTexture;
  961. bgfx::TextureHandle m_modelTexture;
  962. uint32_t m_currFrame;
  963. // UI
  964. Settings m_settings;
  965. bool m_enableSSAO;
  966. bool m_enableTexturing;
  967. float m_texelHalf;
  968. float m_fovY;
  969. bool m_framebufferGutter;
  970. bool m_recreateFrameBuffers;
  971. float m_view[16];
  972. float m_proj[16];
  973. float m_proj2[16];
  974. int32_t m_size[2];
  975. int32_t m_halfSize[2];
  976. int32_t m_quarterSize[2];
  977. int32_t m_fullResOutScissorRect[4];
  978. int32_t m_halfResOutScissorRect[4];
  979. int32_t m_border;
  980. };
  981. } // namespace
  982. ENTRY_IMPLEMENT_MAIN(
  983. ExampleASSAO
  984. , "39-assao"
  985. , "Adaptive Screen Space Ambient Occlusion."
  986. , "https://bkaradzic.github.io/bgfx/examples.html#assao"
  987. );