app.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * Copyright 2021 Richard Schubert. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. *
  5. * AMD FidelityFX Super Resolution 1.0 (FSR)
  6. * Based on https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/sample/
  7. */
  8. #include <common.h>
  9. #include <camera.h>
  10. #include <bgfx_utils.h>
  11. #include <imgui/imgui.h>
  12. #include <bx/rng.h>
  13. #include <bx/os.h>
  14. #include "fsr.h"
  15. namespace
  16. {
  17. #define FRAMEBUFFER_RT_COLOR 0
  18. #define FRAMEBUFFER_RT_DEPTH 1
  19. #define FRAMEBUFFER_RENDER_TARGETS 2
  20. enum Meshes
  21. {
  22. MeshCube = 0,
  23. MeshHollowCube,
  24. };
  25. static const char *s_meshPaths[] =
  26. {
  27. "meshes/cube.bin",
  28. "meshes/hollowcube.bin",
  29. };
  30. static const float s_meshScale[] =
  31. {
  32. 0.45f,
  33. 0.30f,
  34. };
  35. // Vertex decl for our screen space quad (used in deferred rendering)
  36. struct PosTexCoord0Vertex
  37. {
  38. float m_x;
  39. float m_y;
  40. float m_z;
  41. float m_u;
  42. float m_v;
  43. static void init()
  44. {
  45. ms_layout
  46. .begin()
  47. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  48. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  49. .end();
  50. }
  51. static bgfx::VertexLayout ms_layout;
  52. };
  53. bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
  54. void screenSpaceTriangle(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f, float _offsetX = 0.0f, float _offsetY = 0.0f)
  55. {
  56. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
  57. {
  58. bgfx::TransientVertexBuffer vb;
  59. bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
  60. PosTexCoord0Vertex *vertex = (PosTexCoord0Vertex *)vb.data;
  61. const float minx = -_width - _offsetX;
  62. const float maxx = _width - _offsetX;
  63. const float miny = 0.0f - _offsetY;
  64. const float maxy = _height * 2.0f - _offsetY;
  65. const float texelHalfW = _texelHalf / _textureWidth;
  66. const float texelHalfH = _texelHalf / _textureHeight;
  67. const float minu = -1.0f + texelHalfW;
  68. const float maxu = 1.0f + texelHalfW;
  69. const float zz = 0.0f;
  70. float minv = texelHalfH;
  71. float maxv = 2.0f + texelHalfH;
  72. if (_originBottomLeft)
  73. {
  74. float temp = minv;
  75. minv = maxv;
  76. maxv = temp;
  77. minv -= 1.0f;
  78. maxv -= 1.0f;
  79. }
  80. vertex[0].m_x = minx;
  81. vertex[0].m_y = miny;
  82. vertex[0].m_z = zz;
  83. vertex[0].m_u = minu;
  84. vertex[0].m_v = minv;
  85. vertex[1].m_x = maxx;
  86. vertex[1].m_y = miny;
  87. vertex[1].m_z = zz;
  88. vertex[1].m_u = maxu;
  89. vertex[1].m_v = minv;
  90. vertex[2].m_x = maxx;
  91. vertex[2].m_y = maxy;
  92. vertex[2].m_z = zz;
  93. vertex[2].m_u = maxu;
  94. vertex[2].m_v = maxv;
  95. bgfx::setVertexBuffer(0, &vb);
  96. }
  97. }
  98. struct ModelUniforms
  99. {
  100. enum
  101. {
  102. NumVec4 = 2
  103. };
  104. void init()
  105. {
  106. u_params = bgfx::createUniform("u_modelParams", bgfx::UniformType::Vec4, NumVec4);
  107. };
  108. void submit() const
  109. {
  110. bgfx::setUniform(u_params, m_params, NumVec4);
  111. };
  112. void destroy()
  113. {
  114. bgfx::destroy(u_params);
  115. }
  116. union
  117. {
  118. struct
  119. {
  120. /* 0 */ struct
  121. {
  122. float m_color[3];
  123. float m_unused0;
  124. };
  125. /* 1 */ struct
  126. {
  127. float m_lightPosition[3];
  128. float m_unused1;
  129. };
  130. };
  131. float m_params[NumVec4 * 4];
  132. };
  133. bgfx::UniformHandle u_params;
  134. };
  135. struct AppState
  136. {
  137. uint32_t m_width;
  138. uint32_t m_height;
  139. uint32_t m_debug;
  140. uint32_t m_reset;
  141. entry::MouseState m_mouseState;
  142. // Resource handles
  143. bgfx::ProgramHandle m_forwardProgram;
  144. bgfx::ProgramHandle m_gridProgram;
  145. bgfx::ProgramHandle m_copyLinearToGammaProgram;
  146. // Shader uniforms
  147. ModelUniforms m_modelUniforms;
  148. // Uniforms to identify texture samplers
  149. bgfx::UniformHandle s_albedo;
  150. bgfx::UniformHandle s_color;
  151. bgfx::UniformHandle s_normal;
  152. bgfx::FrameBufferHandle m_frameBuffer;
  153. bgfx::TextureHandle m_frameBufferTex[FRAMEBUFFER_RENDER_TARGETS];
  154. Mesh *m_meshes[BX_COUNTOF(s_meshPaths)];
  155. bgfx::TextureHandle m_groundTexture;
  156. bgfx::TextureHandle m_normalTexture;
  157. uint32_t m_currFrame{UINT32_MAX};
  158. float m_lightRotation = 0.0f;
  159. float m_texelHalf = 0.0f;
  160. float m_fovY = 60.0f;
  161. float m_animationTime = 0.0f;
  162. float m_view[16];
  163. float m_proj[16];
  164. int32_t m_size[2];
  165. // UI parameters
  166. bool m_renderNativeResolution = false;
  167. bool m_animateScene = false;
  168. int32_t m_antiAliasingSetting = 2;
  169. Fsr m_fsr;
  170. };
  171. struct RenderTarget
  172. {
  173. void init(uint32_t _width, uint32_t _height, bgfx::TextureFormat::Enum _format, uint64_t _flags)
  174. {
  175. m_width = _width;
  176. m_height = _height;
  177. m_texture = bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), false, 1, _format, _flags);
  178. m_buffer = bgfx::createFrameBuffer(1, &m_texture, true);
  179. }
  180. void destroy()
  181. {
  182. // also responsible for destroying texture
  183. bgfx::destroy(m_buffer);
  184. }
  185. uint32_t m_width;
  186. uint32_t m_height;
  187. bgfx::TextureHandle m_texture;
  188. bgfx::FrameBufferHandle m_buffer;
  189. };
  190. struct MagnifierWidget
  191. {
  192. void init(uint32_t _width, uint32_t _height)
  193. {
  194. m_content.init(_width, _height, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT);
  195. createWidgetTexture(_width + 6, _height + 6);
  196. }
  197. void destroy()
  198. {
  199. bgfx::destroy(m_widgetTexture);
  200. m_content.destroy();
  201. }
  202. void setPosition(float x, float y)
  203. {
  204. m_position.x = x;
  205. m_position.y = y;
  206. }
  207. void drawToScreen(bgfx::ViewId &view, AppState const &state)
  208. {
  209. float invScreenScaleX = 1.0f / float(state.m_width);
  210. float invScreenScaleY = 1.0f / float(state.m_height);
  211. float scaleX = m_widgetWidth * invScreenScaleX;
  212. float scaleY = m_widgetHeight * invScreenScaleY;
  213. float offsetX = -bx::min(bx::max(m_position.x - m_widgetWidth * 0.5f, -3.0f), float(state.m_width - m_widgetWidth + 3) ) * invScreenScaleX;
  214. float offsetY = -bx::min(bx::max(m_position.y - m_widgetHeight * 0.5f, -3.0f), float(state.m_height - m_widgetHeight + 3) ) * invScreenScaleY;
  215. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS | BGFX_STATE_BLEND_ALPHA);
  216. bgfx::setTexture(0, state.s_color, m_widgetTexture);
  217. screenSpaceTriangle(float(m_widgetWidth), float(m_widgetHeight), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
  218. bgfx::submit(view, state.m_copyLinearToGammaProgram);
  219. }
  220. void updateContent(bgfx::ViewId &view, AppState const &state, const bgfx::Caps *caps, bgfx::TextureHandle srcTexture)
  221. {
  222. float orthoProj[16];
  223. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  224. {
  225. // clear out transform stack
  226. float identity[16];
  227. bx::mtxIdentity(identity);
  228. bgfx::setTransform(identity);
  229. }
  230. const float verticalPos = caps->originBottomLeft ? state.m_height - m_position.y : m_position.y;
  231. const float invMagScaleX = 1.0f / float(m_content.m_width);
  232. const float invMagScaleY = 1.0f / float(m_content.m_height);
  233. const float scaleX = state.m_width * invMagScaleX;
  234. const float scaleY = state.m_height * invMagScaleY;
  235. const float offsetX = bx::min(bx::max(m_position.x - m_content.m_width * 0.5f, 0.0f), float(state.m_width - m_content.m_width) ) * scaleX / state.m_width;
  236. const float offsetY = bx::min(bx::max(verticalPos - m_content.m_height * 0.5f, 0.0f), float(state.m_height - m_content.m_height) ) * scaleY / state.m_height;
  237. bgfx::setViewName(view, "magnifier");
  238. bgfx::setViewRect(view, 0, 0, uint16_t(m_content.m_width), uint16_t(m_content.m_height) );
  239. bgfx::setViewTransform(view, NULL, orthoProj);
  240. bgfx::setViewFrameBuffer(view, m_content.m_buffer);
  241. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  242. bgfx::setTexture(0, state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  243. screenSpaceTriangle(float(state.m_width), float(state.m_height), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
  244. bgfx::submit(view, state.m_copyLinearToGammaProgram);
  245. ++view;
  246. }
  247. uint32_t m_widgetWidth{0};
  248. uint32_t m_widgetHeight{0};
  249. bgfx::TextureHandle m_widgetTexture;
  250. RenderTarget m_content;
  251. ImVec2 m_position;
  252. private:
  253. void createWidgetTexture(uint32_t _width, uint32_t _height)
  254. {
  255. const bgfx::Memory *mem = bgfx::alloc(_width * _height * sizeof(uint32_t) );
  256. uint32_t *pixels = (uint32_t*)mem->data;
  257. bx::memSet(pixels, 0, mem->size);
  258. const uint32_t white = 0xFFFFFFFF;
  259. const uint32_t black = 0xFF000000;
  260. const uint32_t y0 = 1;
  261. const uint32_t y1 = _height - 3;
  262. for (uint32_t x = 0; x < _width - 4; x++)
  263. {
  264. pixels[(y0 + 0) * _width + x + 1] = white;
  265. pixels[(y0 + 1) * _width + x + 2] = black;
  266. pixels[(y1 + 0) * _width + x + 1] = white;
  267. pixels[(y1 + 1) * _width + x + 2] = black;
  268. }
  269. const uint32_t x0 = 1;
  270. const uint32_t x1 = _width - 3;
  271. for (uint32_t y = 0; y < _height - 3; y++)
  272. {
  273. pixels[(y + 1) * _width + x0 + 0] = white;
  274. pixels[(y + 2) * _width + x0 + 1] = black;
  275. pixels[(y + 1) * _width + x1 + 0] = white;
  276. pixels[(y + 2) * _width + x1 + 1] = black;
  277. }
  278. pixels[(y1 + 0) * _width + 2] = white;
  279. m_widgetWidth = _width;
  280. m_widgetHeight = _height;
  281. m_widgetTexture = bgfx::createTexture2D(
  282. uint16_t(_width)
  283. , uint16_t(_height)
  284. , false
  285. , 1
  286. , bgfx::TextureFormat::BGRA8
  287. , BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP
  288. , mem
  289. );
  290. }
  291. };
  292. class ExampleFsr : public entry::AppI
  293. {
  294. public:
  295. ExampleFsr(const char *_name, const char *_description)
  296. : entry::AppI(_name, _description)
  297. {
  298. }
  299. void init(int32_t _argc, const char *const *_argv, uint32_t _width, uint32_t _height) override
  300. {
  301. Args args(_argc, _argv);
  302. m_state.m_width = _width;
  303. m_state.m_height = _height;
  304. m_state.m_debug = BGFX_DEBUG_NONE;
  305. m_state.m_reset = 0
  306. | BGFX_RESET_VSYNC
  307. | BGFX_RESET_MAXANISOTROPY
  308. ;
  309. bgfx::Init init;
  310. init.type = args.m_type;
  311. init.vendorId = args.m_pciId;
  312. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  313. init.platformData.ndt = entry::getNativeDisplayHandle();
  314. init.resolution.width = m_state.m_width;
  315. init.resolution.height = m_state.m_height;
  316. init.resolution.reset = m_state.m_reset;
  317. bgfx::init(init);
  318. // Enable debug text.
  319. bgfx::setDebug(m_state.m_debug);
  320. // Create uniforms for screen passes and models
  321. m_state.m_modelUniforms.init();
  322. // Create texture sampler uniforms (used when we bind textures)
  323. m_state.s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
  324. m_state.s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler);
  325. m_state.s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler);
  326. // Create program from shaders.
  327. m_state.m_forwardProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward");
  328. m_state.m_gridProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward_grid");
  329. m_state.m_copyLinearToGammaProgram = loadProgram("vs_fsr_screenquad", "fs_fsr_copy_linear_to_gamma");
  330. // Load some meshes
  331. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  332. {
  333. m_state.m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  334. }
  335. m_state.m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
  336. m_state.m_normalTexture = loadTexture("textures/fieldstone-n.dds");
  337. createFramebuffers();
  338. // Vertex decl
  339. PosTexCoord0Vertex::init();
  340. // Init camera
  341. cameraCreate();
  342. cameraSetPosition({-10.0f, 2.5f, -0.0f});
  343. cameraSetVerticalAngle(-0.2f);
  344. cameraSetHorizontalAngle(0.8f);
  345. // Init "prev" matrices, will be same for first frame
  346. cameraGetViewMtx(m_state.m_view);
  347. bx::mtxProj(m_state.m_proj, m_state.m_fovY, float(m_state.m_size[0]) / float(m_state.m_size[1]), 0.01f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  348. // Get renderer capabilities info.
  349. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  350. m_state.m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  351. const uint32_t magnifierSize = 32;
  352. m_magnifierWidget.init(magnifierSize, magnifierSize);
  353. m_magnifierWidget.setPosition(m_state.m_width * 0.5f, m_state.m_height * 0.5f);
  354. imguiCreate();
  355. m_state.m_fsr.init(_width, _height);
  356. }
  357. int32_t shutdown() override
  358. {
  359. m_state.m_fsr.destroy();
  360. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  361. {
  362. meshUnload(m_state.m_meshes[ii]);
  363. }
  364. bgfx::destroy(m_state.m_normalTexture);
  365. bgfx::destroy(m_state.m_groundTexture);
  366. bgfx::destroy(m_state.m_forwardProgram);
  367. bgfx::destroy(m_state.m_gridProgram);
  368. bgfx::destroy(m_state.m_copyLinearToGammaProgram);
  369. m_state.m_modelUniforms.destroy();
  370. m_magnifierWidget.destroy();
  371. bgfx::destroy(m_state.s_albedo);
  372. bgfx::destroy(m_state.s_color);
  373. bgfx::destroy(m_state.s_normal);
  374. destroyFramebuffers();
  375. cameraDestroy();
  376. imguiDestroy();
  377. bgfx::shutdown();
  378. return 0;
  379. }
  380. bool update() override
  381. {
  382. if (!entry::processEvents(m_state.m_width, m_state.m_height, m_state.m_debug, m_state.m_reset, &m_state.m_mouseState) )
  383. {
  384. // skip processing when minimized, otherwise crashing
  385. if (0 == m_state.m_width
  386. || 0 == m_state.m_height)
  387. {
  388. return true;
  389. }
  390. if (m_state.m_mouseState.m_buttons[entry::MouseButton::Left]
  391. && !ImGui::MouseOverArea() )
  392. {
  393. m_magnifierWidget.setPosition(
  394. float(m_state.m_mouseState.m_mx)
  395. , float(m_state.m_mouseState.m_my)
  396. );
  397. }
  398. // Update frame timer
  399. int64_t now = bx::getHPCounter();
  400. static int64_t last = now;
  401. const int64_t frameTime = now - last;
  402. last = now;
  403. const double freq = double(bx::getHPFrequency() );
  404. const float deltaTime = float(frameTime / freq);
  405. const bgfx::Caps* caps = bgfx::getCaps();
  406. if (m_state.m_size[0] != (int32_t)m_state.m_width || m_state.m_size[1] != (int32_t)m_state.m_height)
  407. {
  408. resize();
  409. }
  410. // update animation time
  411. const float rotationSpeed = 0.25f;
  412. if (m_state.m_animateScene)
  413. {
  414. m_state.m_animationTime += deltaTime * rotationSpeed;
  415. if (bx::kPi2 < m_state.m_animationTime)
  416. {
  417. m_state.m_animationTime -= bx::kPi2;
  418. }
  419. }
  420. // Update camera
  421. cameraUpdate(deltaTime * 0.15f, m_state.m_mouseState, ImGui::MouseOverArea() );
  422. cameraGetViewMtx(m_state.m_view);
  423. updateUniforms();
  424. bx::mtxProj(
  425. m_state.m_proj
  426. , m_state.m_fovY
  427. , float(m_state.m_size[0]) / float(m_state.m_size[1])
  428. , 0.01f
  429. , 100.0f
  430. , caps->homogeneousDepth
  431. );
  432. bgfx::ViewId view = 0;
  433. // Clear full frame buffer to avoid sampling into garbage during FSR pass
  434. if (!m_state.m_renderNativeResolution)
  435. {
  436. bgfx::setViewRect(view, 0, 0, (uint16_t)m_state.m_width, (uint16_t)m_state.m_height);
  437. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
  438. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  439. bgfx::touch(view);
  440. ++view;
  441. }
  442. // Draw models into scene
  443. {
  444. bgfx::setViewName(view, "forward scene");
  445. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x7fb8ffff, 1.0f, 0);
  446. const float viewScale = m_state.m_renderNativeResolution
  447. ? 1.0f
  448. : 1.0f / m_state.m_fsr.m_config.m_superSamplingFactor
  449. ;
  450. const uint16_t viewRectWidth = uint16_t(bx::ceil(m_state.m_size[0] * viewScale) );
  451. const uint16_t viewRectHeight = uint16_t(bx::ceil(m_state.m_size[1] * viewScale) );
  452. const uint16_t viewRectY = uint16_t(caps->originBottomLeft ? m_state.m_size[1] - viewRectHeight : 0);
  453. bgfx::setViewRect(view, 0, viewRectY, viewRectWidth, viewRectHeight);
  454. bgfx::setViewTransform(view, m_state.m_view, m_state.m_proj);
  455. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  456. bgfx::setState(0
  457. | BGFX_STATE_WRITE_RGB
  458. | BGFX_STATE_WRITE_A
  459. | BGFX_STATE_WRITE_Z
  460. | BGFX_STATE_DEPTH_TEST_LESS
  461. );
  462. drawAllModels(view, m_state.m_forwardProgram, m_state.m_modelUniforms);
  463. ++view;
  464. }
  465. // optionally run FSR
  466. if (!m_state.m_renderNativeResolution)
  467. {
  468. view = m_state.m_fsr.computeFsr(view, m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR]);
  469. }
  470. // render result to screen
  471. {
  472. bgfx::TextureHandle srcTexture = m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR];
  473. if (!m_state.m_renderNativeResolution)
  474. {
  475. srcTexture = m_state.m_fsr.getResultTexture();
  476. }
  477. m_magnifierWidget.updateContent(view, m_state, caps, srcTexture);
  478. float orthoProj[16];
  479. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  480. bgfx::setViewName(view, "display");
  481. bgfx::setViewClear(view, BGFX_CLEAR_NONE, 0, 1.0f, 0);
  482. bgfx::setViewRect(view, 0, 0, uint16_t(m_state.m_width), uint16_t(m_state.m_height) );
  483. bgfx::setViewTransform(view, NULL, orthoProj);
  484. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  485. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  486. bgfx::setTexture(0, m_state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  487. screenSpaceTriangle(float(m_state.m_width), float(m_state.m_height), m_state.m_texelHalf, caps->originBottomLeft);
  488. bgfx::submit(view, m_state.m_copyLinearToGammaProgram);
  489. }
  490. m_magnifierWidget.drawToScreen(view, m_state);
  491. ++view;
  492. // Draw UI
  493. imguiBeginFrame(m_state.m_mouseState.m_mx, m_state.m_mouseState.m_my, (m_state.m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0) | (m_state.m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0) | (m_state.m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0), m_state.m_mouseState.m_mz, uint16_t(m_state.m_width), uint16_t(m_state.m_height) );
  494. showExampleDialog(this);
  495. ImGui::SetNextWindowPos(ImVec2(m_state.m_width - m_state.m_width / 4.0f - 10.0f, 10.0f), ImGuiCond_FirstUseEver);
  496. ImGui::SetNextWindowSize(ImVec2(m_state.m_width / 4.0f, m_state.m_height / 1.2f), ImGuiCond_FirstUseEver);
  497. ImGui::Begin("Settings", NULL, 0);
  498. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  499. const ImVec2 itemSize = ImGui::GetItemRectSize();
  500. {
  501. ImGui::Checkbox("Animate scene", &m_state.m_animateScene);
  502. if (ImGui::Combo("Antialiasing", &m_state.m_antiAliasingSetting, "none\0""4x\0""16x\0""\0") )
  503. {
  504. resize();
  505. }
  506. ImGui::Checkbox("Render native resolution", &m_state.m_renderNativeResolution);
  507. if (ImGui::IsItemHovered() )
  508. {
  509. ImGui::SetTooltip("Disable super sampling and FSR.");
  510. }
  511. ImGui::Image(m_magnifierWidget.m_content.m_texture, ImVec2(itemSize.x * 0.94f, itemSize.x * 0.94f) );
  512. if (!m_state.m_renderNativeResolution)
  513. {
  514. ImGui::SliderFloat("Super sampling", &m_state.m_fsr.m_config.m_superSamplingFactor, 1.0f, 2.0f);
  515. if (ImGui::IsItemHovered() )
  516. {
  517. ImGui::BeginTooltip();
  518. ImGui::Text("2.0 means the scene is rendered at half window resolution.");
  519. ImGui::Text("1.0 means the scene is rendered at native window resolution.");
  520. ImGui::EndTooltip();
  521. }
  522. ImGui::Separator();
  523. if (m_state.m_fsr.supports16BitPrecision() )
  524. {
  525. ImGui::Checkbox("Use 16 Bit", &m_state.m_fsr.m_config.m_fsr16Bit);
  526. if (ImGui::IsItemHovered() )
  527. {
  528. ImGui::BeginTooltip();
  529. ImGui::Text("For better performance and less memory consumption use 16 Bit precision.");
  530. ImGui::Text("If disabled use 32 Bit per channel precision for FSR which works better on older hardware.");
  531. ImGui::Text("FSR in 16 Bit precision is also prone to be broken in Direct3D11, Direct3D12 works though.");
  532. ImGui::EndTooltip();
  533. }
  534. }
  535. ImGui::Checkbox("Apply FSR", &m_state.m_fsr.m_config.m_applyFsr);
  536. if (ImGui::IsItemHovered() )
  537. {
  538. ImGui::SetTooltip("Compare between FSR and bilinear interpolation of source image.");
  539. }
  540. if (m_state.m_fsr.m_config.m_applyFsr)
  541. {
  542. ImGui::Checkbox("Apply FSR sharpening", &m_state.m_fsr.m_config.m_applyFsrRcas);
  543. if (ImGui::IsItemHovered() )
  544. {
  545. ImGui::SetTooltip("Apply the FSR RCAS sharpening pass.");
  546. }
  547. if (m_state.m_fsr.m_config.m_applyFsrRcas)
  548. {
  549. ImGui::SliderFloat("Sharpening attenuation", &m_state.m_fsr.m_config.m_rcasAttenuation, 0.01f, 2.0f);
  550. if (ImGui::IsItemHovered() )
  551. {
  552. ImGui::SetTooltip("Lower value means sharper.");
  553. }
  554. }
  555. }
  556. }
  557. }
  558. ImGui::End();
  559. imguiEndFrame();
  560. // Advance to next frame. Rendering thread will be kicked to
  561. // process submitted rendering primitives.
  562. m_state.m_currFrame = bgfx::frame();
  563. return true;
  564. }
  565. return false;
  566. }
  567. void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, ModelUniforms &_uniforms)
  568. {
  569. const int32_t width = 6;
  570. const int32_t length = 20;
  571. float c0[] = { 235.0f / 255.0f, 126.0f / 255.0f, 30.0f / 255.0f}; // orange
  572. float c1[] = { 235.0f / 255.0f, 146.0f / 255.0f, 251.0f / 255.0f}; // purple
  573. float c2[] = { 199.0f / 255.0f, 0.0f / 255.0f, 57.0f / 255.0f}; // pink
  574. for (int32_t zz = 0; zz < length; ++zz)
  575. {
  576. // make a color gradient, nothing special about this for example
  577. float *ca = c0;
  578. float *cb = c1;
  579. float lerpVal = float(zz) / float(length);
  580. if (0.5f <= lerpVal)
  581. {
  582. ca = c1;
  583. cb = c2;
  584. }
  585. lerpVal = bx::fract(2.0f * lerpVal);
  586. float r = bx::lerp(ca[0], cb[0], lerpVal);
  587. float g = bx::lerp(ca[1], cb[1], lerpVal);
  588. float b = bx::lerp(ca[2], cb[2], lerpVal);
  589. for (int32_t xx = 0; xx < width; ++xx)
  590. {
  591. const float angle = m_state.m_animationTime + float(zz) * (bx::kPi2 / length) + float(xx) * (bx::kPiHalf / width);
  592. const float posX = 2.0f * xx - width + 1.0f;
  593. const float posY = bx::sin(angle);
  594. const float posZ = 2.0f * zz - length + 1.0f;
  595. const float scale = s_meshScale[MeshHollowCube];
  596. float mtx[16];
  597. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, posX, posY, posZ);
  598. bgfx::setTexture(0, m_state.s_albedo, m_state.m_groundTexture);
  599. bgfx::setTexture(1, m_state.s_normal, m_state.m_normalTexture);
  600. _uniforms.m_color[0] = r;
  601. _uniforms.m_color[1] = g;
  602. _uniforms.m_color[2] = b;
  603. _uniforms.submit();
  604. meshSubmit(m_state.m_meshes[MeshHollowCube], _pass, _program, mtx);
  605. }
  606. }
  607. // draw box as ground plane
  608. {
  609. const float posY = -2.0f;
  610. const float scale = length;
  611. float mtx[16];
  612. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, 0.0f, -scale + posY, 0.0f);
  613. _uniforms.m_color[0] = 0.5f;
  614. _uniforms.m_color[1] = 0.5f;
  615. _uniforms.m_color[2] = 0.5f;
  616. _uniforms.submit();
  617. meshSubmit(m_state.m_meshes[MeshCube], _pass, m_state.m_gridProgram, mtx);
  618. }
  619. }
  620. void resize()
  621. {
  622. destroyFramebuffers();
  623. createFramebuffers();
  624. m_state.m_fsr.resize(m_state.m_width, m_state.m_height);
  625. }
  626. void createFramebuffers()
  627. {
  628. m_state.m_size[0] = m_state.m_width;
  629. m_state.m_size[1] = m_state.m_height;
  630. constexpr uint64_t msaaFlags[] =
  631. {
  632. BGFX_TEXTURE_NONE,
  633. BGFX_TEXTURE_RT_MSAA_X4,
  634. BGFX_TEXTURE_RT_MSAA_X16,
  635. };
  636. const uint64_t msaa = msaaFlags[m_state.m_antiAliasingSetting];
  637. const uint64_t colorFlags = 0
  638. | BGFX_TEXTURE_RT
  639. | BGFX_SAMPLER_U_CLAMP
  640. | BGFX_SAMPLER_V_CLAMP
  641. | msaa
  642. ;
  643. const uint64_t depthFlags = 0
  644. | BGFX_TEXTURE_RT_WRITE_ONLY
  645. | msaa
  646. ;
  647. m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR] = bgfx::createTexture2D(
  648. uint16_t(m_state.m_size[0])
  649. , uint16_t(m_state.m_size[1])
  650. , false
  651. , 1
  652. , bgfx::TextureFormat::RGBA16F
  653. , colorFlags
  654. );
  655. m_state.m_frameBufferTex[FRAMEBUFFER_RT_DEPTH] = bgfx::createTexture2D(
  656. uint16_t(m_state.m_size[0])
  657. , uint16_t(m_state.m_size[1])
  658. , false
  659. , 1
  660. , bgfx::TextureFormat::D32F
  661. , depthFlags
  662. );
  663. m_state.m_frameBuffer = bgfx::createFrameBuffer(
  664. BX_COUNTOF(m_state.m_frameBufferTex)
  665. , m_state.m_frameBufferTex
  666. , true
  667. );
  668. }
  669. // all buffers set to destroy their textures
  670. void destroyFramebuffers()
  671. {
  672. bgfx::destroy(m_state.m_frameBuffer);
  673. }
  674. void updateUniforms()
  675. {
  676. m_state.m_modelUniforms.m_lightPosition[0] = 0.0f;
  677. m_state.m_modelUniforms.m_lightPosition[1] = 6.0f;
  678. m_state.m_modelUniforms.m_lightPosition[2] = 10.0f;
  679. }
  680. AppState m_state;
  681. MagnifierWidget m_magnifierWidget;
  682. };
  683. } // namespace
  684. ENTRY_IMPLEMENT_MAIN(
  685. ExampleFsr
  686. , "46-fsr"
  687. , "AMD FidelityFX Super Resolution (FSR)\n"
  688. "\n"
  689. "For an optimal FSR result high quality antialiasing for the low resolution source image and negative texture LOD bias is recommended."
  690. );