app.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright 2021 Richard Schubert. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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 indentify 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.resolution.width = m_state.m_width;
  313. init.resolution.height = m_state.m_height;
  314. init.resolution.reset = m_state.m_reset;
  315. bgfx::init(init);
  316. // Enable debug text.
  317. bgfx::setDebug(m_state.m_debug);
  318. // Create uniforms for screen passes and models
  319. m_state.m_modelUniforms.init();
  320. // Create texture sampler uniforms (used when we bind textures)
  321. m_state.s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
  322. m_state.s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler);
  323. m_state.s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler);
  324. // Create program from shaders.
  325. m_state.m_forwardProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward");
  326. m_state.m_gridProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward_grid");
  327. m_state.m_copyLinearToGammaProgram = loadProgram("vs_fsr_screenquad", "fs_fsr_copy_linear_to_gamma");
  328. // Load some meshes
  329. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  330. {
  331. m_state.m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  332. }
  333. m_state.m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
  334. m_state.m_normalTexture = loadTexture("textures/fieldstone-n.dds");
  335. createFramebuffers();
  336. // Vertex decl
  337. PosTexCoord0Vertex::init();
  338. // Init camera
  339. cameraCreate();
  340. cameraSetPosition({-10.0f, 2.5f, -0.0f});
  341. cameraSetVerticalAngle(-0.2f);
  342. cameraSetHorizontalAngle(0.8f);
  343. // Init "prev" matrices, will be same for first frame
  344. cameraGetViewMtx(m_state.m_view);
  345. 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);
  346. // Get renderer capabilities info.
  347. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  348. m_state.m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  349. const uint32_t magnifierSize = 32;
  350. m_magnifierWidget.init(magnifierSize, magnifierSize);
  351. m_magnifierWidget.setPosition(m_state.m_width * 0.5f, m_state.m_height * 0.5f);
  352. imguiCreate();
  353. m_state.m_fsr.init(_width, _height);
  354. }
  355. int32_t shutdown() override
  356. {
  357. m_state.m_fsr.destroy();
  358. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  359. {
  360. meshUnload(m_state.m_meshes[ii]);
  361. }
  362. bgfx::destroy(m_state.m_normalTexture);
  363. bgfx::destroy(m_state.m_groundTexture);
  364. bgfx::destroy(m_state.m_forwardProgram);
  365. bgfx::destroy(m_state.m_gridProgram);
  366. bgfx::destroy(m_state.m_copyLinearToGammaProgram);
  367. m_state.m_modelUniforms.destroy();
  368. m_magnifierWidget.destroy();
  369. bgfx::destroy(m_state.s_albedo);
  370. bgfx::destroy(m_state.s_color);
  371. bgfx::destroy(m_state.s_normal);
  372. destroyFramebuffers();
  373. cameraDestroy();
  374. imguiDestroy();
  375. bgfx::shutdown();
  376. return 0;
  377. }
  378. bool update() override
  379. {
  380. if (!entry::processEvents(m_state.m_width, m_state.m_height, m_state.m_debug, m_state.m_reset, &m_state.m_mouseState) )
  381. {
  382. // skip processing when minimized, otherwise crashing
  383. if (0 == m_state.m_width
  384. || 0 == m_state.m_height)
  385. {
  386. return true;
  387. }
  388. if (m_state.m_mouseState.m_buttons[entry::MouseButton::Left]
  389. && !ImGui::MouseOverArea() )
  390. {
  391. m_magnifierWidget.setPosition(
  392. float(m_state.m_mouseState.m_mx)
  393. , float(m_state.m_mouseState.m_my)
  394. );
  395. }
  396. // Update frame timer
  397. int64_t now = bx::getHPCounter();
  398. static int64_t last = now;
  399. const int64_t frameTime = now - last;
  400. last = now;
  401. const double freq = double(bx::getHPFrequency() );
  402. const float deltaTime = float(frameTime / freq);
  403. const bgfx::Caps* caps = bgfx::getCaps();
  404. if (m_state.m_size[0] != (int32_t)m_state.m_width || m_state.m_size[1] != (int32_t)m_state.m_height)
  405. {
  406. resize();
  407. }
  408. // update animation time
  409. const float rotationSpeed = 0.25f;
  410. if (m_state.m_animateScene)
  411. {
  412. m_state.m_animationTime += deltaTime * rotationSpeed;
  413. if (bx::kPi2 < m_state.m_animationTime)
  414. {
  415. m_state.m_animationTime -= bx::kPi2;
  416. }
  417. }
  418. // Update camera
  419. cameraUpdate(deltaTime * 0.15f, m_state.m_mouseState, ImGui::MouseOverArea() );
  420. cameraGetViewMtx(m_state.m_view);
  421. updateUniforms();
  422. bx::mtxProj(
  423. m_state.m_proj
  424. , m_state.m_fovY
  425. , float(m_state.m_size[0]) / float(m_state.m_size[1])
  426. , 0.01f
  427. , 100.0f
  428. , caps->homogeneousDepth
  429. );
  430. bgfx::ViewId view = 0;
  431. // Clear full frame buffer to avoid sampling into garbage during FSR pass
  432. if (!m_state.m_renderNativeResolution)
  433. {
  434. bgfx::setViewRect(view, 0, 0, (uint16_t)m_state.m_width, (uint16_t)m_state.m_height);
  435. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
  436. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  437. bgfx::touch(view);
  438. ++view;
  439. }
  440. // Draw models into scene
  441. {
  442. bgfx::setViewName(view, "forward scene");
  443. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x7fb8ffff, 1.0f, 0);
  444. const float viewScale = m_state.m_renderNativeResolution
  445. ? 1.0f
  446. : 1.0f / m_state.m_fsr.m_config.m_superSamplingFactor
  447. ;
  448. const uint16_t viewRectWidth = uint16_t(bx::ceil(m_state.m_size[0] * viewScale) );
  449. const uint16_t viewRectHeight = uint16_t(bx::ceil(m_state.m_size[1] * viewScale) );
  450. const uint16_t viewRectY = uint16_t(caps->originBottomLeft ? m_state.m_size[1] - viewRectHeight : 0);
  451. bgfx::setViewRect(view, 0, viewRectY, viewRectWidth, viewRectHeight);
  452. bgfx::setViewTransform(view, m_state.m_view, m_state.m_proj);
  453. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  454. bgfx::setState(0
  455. | BGFX_STATE_WRITE_RGB
  456. | BGFX_STATE_WRITE_A
  457. | BGFX_STATE_WRITE_Z
  458. | BGFX_STATE_DEPTH_TEST_LESS
  459. );
  460. drawAllModels(view, m_state.m_forwardProgram, m_state.m_modelUniforms);
  461. ++view;
  462. }
  463. // optionally run FSR
  464. if (!m_state.m_renderNativeResolution)
  465. {
  466. view = m_state.m_fsr.computeFsr(view, m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR]);
  467. }
  468. // render result to screen
  469. {
  470. bgfx::TextureHandle srcTexture = m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR];
  471. if (!m_state.m_renderNativeResolution)
  472. {
  473. srcTexture = m_state.m_fsr.getResultTexture();
  474. }
  475. m_magnifierWidget.updateContent(view, m_state, caps, srcTexture);
  476. float orthoProj[16];
  477. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  478. bgfx::setViewName(view, "display");
  479. bgfx::setViewClear(view, BGFX_CLEAR_NONE, 0, 1.0f, 0);
  480. bgfx::setViewRect(view, 0, 0, uint16_t(m_state.m_width), uint16_t(m_state.m_height) );
  481. bgfx::setViewTransform(view, NULL, orthoProj);
  482. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  483. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  484. bgfx::setTexture(0, m_state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  485. screenSpaceTriangle(float(m_state.m_width), float(m_state.m_height), m_state.m_texelHalf, caps->originBottomLeft);
  486. bgfx::submit(view, m_state.m_copyLinearToGammaProgram);
  487. }
  488. m_magnifierWidget.drawToScreen(view, m_state);
  489. ++view;
  490. // Draw UI
  491. 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) );
  492. showExampleDialog(this);
  493. ImGui::SetNextWindowPos(ImVec2(m_state.m_width - m_state.m_width / 4.0f - 10.0f, 10.0f), ImGuiCond_FirstUseEver);
  494. ImGui::SetNextWindowSize(ImVec2(m_state.m_width / 4.0f, m_state.m_height / 1.2f), ImGuiCond_FirstUseEver);
  495. ImGui::Begin("Settings", NULL, 0);
  496. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  497. const ImVec2 itemSize = ImGui::GetItemRectSize();
  498. {
  499. ImGui::Checkbox("Animate scene", &m_state.m_animateScene);
  500. if (ImGui::Combo("Antialiasing", &m_state.m_antiAliasingSetting, "none\0""4x\0""16x\0""\0") )
  501. {
  502. resize();
  503. }
  504. ImGui::Checkbox("Render native resolution", &m_state.m_renderNativeResolution);
  505. if (ImGui::IsItemHovered() )
  506. {
  507. ImGui::SetTooltip("Disable super sampling and FSR.");
  508. }
  509. ImGui::Image(m_magnifierWidget.m_content.m_texture, ImVec2(itemSize.x * 0.94f, itemSize.x * 0.94f) );
  510. if (!m_state.m_renderNativeResolution)
  511. {
  512. ImGui::SliderFloat("Super sampling", &m_state.m_fsr.m_config.m_superSamplingFactor, 1.0f, 2.0f);
  513. if (ImGui::IsItemHovered() )
  514. {
  515. ImGui::BeginTooltip();
  516. ImGui::Text("2.0 means the scene is rendered at half window resolution.");
  517. ImGui::Text("1.0 means the scene is rendered at native window resolution.");
  518. ImGui::EndTooltip();
  519. }
  520. ImGui::Separator();
  521. if (m_state.m_fsr.supports16BitPrecision() )
  522. {
  523. ImGui::Checkbox("Use 16 Bit", &m_state.m_fsr.m_config.m_fsr16Bit);
  524. if (ImGui::IsItemHovered() )
  525. {
  526. ImGui::BeginTooltip();
  527. ImGui::Text("For better performance and less memory consumption use 16 Bit precision.");
  528. ImGui::Text("If disabled use 32 Bit per channel precision for FSR which works better on older hardware.");
  529. ImGui::Text("FSR in 16 Bit precision is also prone to be broken in Direct3D11, Direct3D12 works though.");
  530. ImGui::EndTooltip();
  531. }
  532. }
  533. ImGui::Checkbox("Apply FSR", &m_state.m_fsr.m_config.m_applyFsr);
  534. if (ImGui::IsItemHovered() )
  535. {
  536. ImGui::SetTooltip("Compare between FSR and bilinear interpolation of source image.");
  537. }
  538. if (m_state.m_fsr.m_config.m_applyFsr)
  539. {
  540. ImGui::Checkbox("Apply FSR sharpening", &m_state.m_fsr.m_config.m_applyFsrRcas);
  541. if (ImGui::IsItemHovered() )
  542. {
  543. ImGui::SetTooltip("Apply the FSR RCAS sharpening pass.");
  544. }
  545. if (m_state.m_fsr.m_config.m_applyFsrRcas)
  546. {
  547. ImGui::SliderFloat("Sharpening attenuation", &m_state.m_fsr.m_config.m_rcasAttenuation, 0.01f, 2.0f);
  548. if (ImGui::IsItemHovered() )
  549. {
  550. ImGui::SetTooltip("Lower value means sharper.");
  551. }
  552. }
  553. }
  554. }
  555. }
  556. ImGui::End();
  557. imguiEndFrame();
  558. // Advance to next frame. Rendering thread will be kicked to
  559. // process submitted rendering primitives.
  560. m_state.m_currFrame = bgfx::frame();
  561. return true;
  562. }
  563. return false;
  564. }
  565. void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, ModelUniforms &_uniforms)
  566. {
  567. const int32_t width = 6;
  568. const int32_t length = 20;
  569. float c0[] = { 235.0f / 255.0f, 126.0f / 255.0f, 30.0f / 255.0f}; // orange
  570. float c1[] = { 235.0f / 255.0f, 146.0f / 255.0f, 251.0f / 255.0f}; // purple
  571. float c2[] = { 199.0f / 255.0f, 0.0f / 255.0f, 57.0f / 255.0f}; // pink
  572. for (int32_t zz = 0; zz < length; ++zz)
  573. {
  574. // make a color gradient, nothing special about this for example
  575. float *ca = c0;
  576. float *cb = c1;
  577. float lerpVal = float(zz) / float(length);
  578. if (0.5f <= lerpVal)
  579. {
  580. ca = c1;
  581. cb = c2;
  582. }
  583. lerpVal = bx::fract(2.0f * lerpVal);
  584. float r = bx::lerp(ca[0], cb[0], lerpVal);
  585. float g = bx::lerp(ca[1], cb[1], lerpVal);
  586. float b = bx::lerp(ca[2], cb[2], lerpVal);
  587. for (int32_t xx = 0; xx < width; ++xx)
  588. {
  589. const float angle = m_state.m_animationTime + float(zz) * (bx::kPi2 / length) + float(xx) * (bx::kPiHalf / width);
  590. const float posX = 2.0f * xx - width + 1.0f;
  591. const float posY = bx::sin(angle);
  592. const float posZ = 2.0f * zz - length + 1.0f;
  593. const float scale = s_meshScale[MeshHollowCube];
  594. float mtx[16];
  595. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, posX, posY, posZ);
  596. bgfx::setTexture(0, m_state.s_albedo, m_state.m_groundTexture);
  597. bgfx::setTexture(1, m_state.s_normal, m_state.m_normalTexture);
  598. _uniforms.m_color[0] = r;
  599. _uniforms.m_color[1] = g;
  600. _uniforms.m_color[2] = b;
  601. _uniforms.submit();
  602. meshSubmit(m_state.m_meshes[MeshHollowCube], _pass, _program, mtx);
  603. }
  604. }
  605. // draw box as ground plane
  606. {
  607. const float posY = -2.0f;
  608. const float scale = length;
  609. float mtx[16];
  610. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, 0.0f, -scale + posY, 0.0f);
  611. _uniforms.m_color[0] = 0.5f;
  612. _uniforms.m_color[1] = 0.5f;
  613. _uniforms.m_color[2] = 0.5f;
  614. _uniforms.submit();
  615. meshSubmit(m_state.m_meshes[MeshCube], _pass, m_state.m_gridProgram, mtx);
  616. }
  617. }
  618. void resize()
  619. {
  620. destroyFramebuffers();
  621. createFramebuffers();
  622. m_state.m_fsr.resize(m_state.m_width, m_state.m_height);
  623. }
  624. void createFramebuffers()
  625. {
  626. m_state.m_size[0] = m_state.m_width;
  627. m_state.m_size[1] = m_state.m_height;
  628. constexpr uint64_t msaaFlags[] =
  629. {
  630. BGFX_TEXTURE_NONE,
  631. BGFX_TEXTURE_RT_MSAA_X4,
  632. BGFX_TEXTURE_RT_MSAA_X16,
  633. };
  634. const uint64_t msaa = msaaFlags[m_state.m_antiAliasingSetting];
  635. const uint64_t colorFlags = 0
  636. | BGFX_TEXTURE_RT
  637. | BGFX_SAMPLER_U_CLAMP
  638. | BGFX_SAMPLER_V_CLAMP
  639. | msaa
  640. ;
  641. const uint64_t depthFlags = 0
  642. | BGFX_TEXTURE_RT_WRITE_ONLY
  643. | msaa
  644. ;
  645. m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR] = bgfx::createTexture2D(
  646. uint16_t(m_state.m_size[0])
  647. , uint16_t(m_state.m_size[1])
  648. , false
  649. , 1
  650. , bgfx::TextureFormat::RGBA16F
  651. , colorFlags
  652. );
  653. m_state.m_frameBufferTex[FRAMEBUFFER_RT_DEPTH] = bgfx::createTexture2D(
  654. uint16_t(m_state.m_size[0])
  655. , uint16_t(m_state.m_size[1])
  656. , false
  657. , 1
  658. , bgfx::TextureFormat::D32F
  659. , depthFlags
  660. );
  661. m_state.m_frameBuffer = bgfx::createFrameBuffer(
  662. BX_COUNTOF(m_state.m_frameBufferTex)
  663. , m_state.m_frameBufferTex
  664. , true
  665. );
  666. }
  667. // all buffers set to destroy their textures
  668. void destroyFramebuffers()
  669. {
  670. bgfx::destroy(m_state.m_frameBuffer);
  671. }
  672. void updateUniforms()
  673. {
  674. m_state.m_modelUniforms.m_lightPosition[0] = 0.0f;
  675. m_state.m_modelUniforms.m_lightPosition[1] = 6.0f;
  676. m_state.m_modelUniforms.m_lightPosition[2] = 10.0f;
  677. }
  678. AppState m_state;
  679. MagnifierWidget m_magnifierWidget;
  680. };
  681. } // namespace
  682. ENTRY_IMPLEMENT_MAIN(
  683. ExampleFsr
  684. , "46-fsr"
  685. , "AMD FidelityFX Super Resolution (FSR)\n"
  686. "\n"
  687. "For an optimal FSR result high quality antialiasing for the low resolution source image and negative texture LOD bias is recommended."
  688. );