app.cpp 23 KB

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