app.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 / static_cast<float>(state.m_width);
  210. float invScreenScaleY = 1.0f / static_cast<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), static_cast<float>(state.m_width - m_widgetWidth + 3) ) * invScreenScaleX;
  214. float offsetY = -bx::min(bx::max(m_position.y - m_widgetHeight * 0.5f, -3.0f), static_cast<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 / static_cast<float>(m_content.m_width);
  232. const float invMagScaleY = 1.0f / static_cast<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), static_cast<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), static_cast<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. // Draw models into scene
  432. {
  433. bgfx::setViewName(view, "forward scene");
  434. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x7fb8ffff, 1.0f, 0);
  435. const float viewScale = m_state.m_renderNativeResolution
  436. ? 1.0f
  437. : 1.0f / m_state.m_fsr.m_config.m_superSamplingFactor
  438. ;
  439. const uint16_t viewRectWidth = uint16_t(bx::ceil(m_state.m_size[0] * viewScale) );
  440. const uint16_t viewRectHeight = uint16_t(bx::ceil(m_state.m_size[1] * viewScale) );
  441. const uint16_t viewRectY = caps->originBottomLeft ? m_state.m_size[1] - viewRectHeight : 0;
  442. bgfx::setViewRect(view, 0, viewRectY, viewRectWidth, viewRectHeight);
  443. bgfx::setViewTransform(view, m_state.m_view, m_state.m_proj);
  444. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  445. bgfx::setState(0
  446. | BGFX_STATE_WRITE_RGB
  447. | BGFX_STATE_WRITE_A
  448. | BGFX_STATE_WRITE_Z
  449. | BGFX_STATE_DEPTH_TEST_LESS
  450. );
  451. drawAllModels(view, m_state.m_forwardProgram, m_state.m_modelUniforms);
  452. ++view;
  453. }
  454. // optionally run FSR
  455. if (!m_state.m_renderNativeResolution)
  456. {
  457. view = m_state.m_fsr.computeFsr(view, m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR]);
  458. }
  459. // render result to screen
  460. {
  461. bgfx::TextureHandle srcTexture = m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR];
  462. if (!m_state.m_renderNativeResolution)
  463. {
  464. srcTexture = m_state.m_fsr.getResultTexture();
  465. }
  466. m_magnifierWidget.updateContent(view, m_state, caps, srcTexture);
  467. float orthoProj[16];
  468. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  469. bgfx::setViewName(view, "display");
  470. bgfx::setViewClear(view, BGFX_CLEAR_NONE, 0, 1.0f, 0);
  471. bgfx::setViewRect(view, 0, 0, uint16_t(m_state.m_width), uint16_t(m_state.m_height) );
  472. bgfx::setViewTransform(view, NULL, orthoProj);
  473. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  474. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  475. bgfx::setTexture(0, m_state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  476. screenSpaceTriangle(float(m_state.m_width), float(m_state.m_height), m_state.m_texelHalf, caps->originBottomLeft);
  477. bgfx::submit(view, m_state.m_copyLinearToGammaProgram);
  478. }
  479. m_magnifierWidget.drawToScreen(view, m_state);
  480. ++view;
  481. // Draw UI
  482. 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) );
  483. showExampleDialog(this);
  484. ImGui::SetNextWindowPos(ImVec2(m_state.m_width - m_state.m_width / 4.0f - 10.0f, 10.0f), ImGuiCond_FirstUseEver);
  485. ImGui::SetNextWindowSize(ImVec2(m_state.m_width / 4.0f, m_state.m_height / 1.2f), ImGuiCond_FirstUseEver);
  486. ImGui::Begin("Settings", NULL, 0);
  487. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  488. const ImVec2 itemSize = ImGui::GetItemRectSize();
  489. {
  490. ImGui::Checkbox("Animate scene", &m_state.m_animateScene);
  491. if (ImGui::Combo("Antialiasing", &m_state.m_antiAliasingSetting, "none\0""4x\0""16x\0""\0") )
  492. {
  493. resize();
  494. }
  495. ImGui::Checkbox("Render native resolution", &m_state.m_renderNativeResolution);
  496. if (ImGui::IsItemHovered() )
  497. {
  498. ImGui::SetTooltip("Disable super sampling and FSR.");
  499. }
  500. ImGui::Image(m_magnifierWidget.m_content.m_texture, ImVec2(itemSize.x * 0.94f, itemSize.x * 0.94f) );
  501. if (!m_state.m_renderNativeResolution)
  502. {
  503. ImGui::SliderFloat("Super sampling", &m_state.m_fsr.m_config.m_superSamplingFactor, 1.0f, 2.0f);
  504. if (ImGui::IsItemHovered() )
  505. {
  506. ImGui::BeginTooltip();
  507. ImGui::Text("2.0 means the scene is rendered at half window resolution.");
  508. ImGui::Text("1.0 means the scene is rendered at native window resolution.");
  509. ImGui::EndTooltip();
  510. }
  511. ImGui::Separator();
  512. if (m_state.m_fsr.supports16BitPrecision() )
  513. {
  514. ImGui::Checkbox("Use 16 Bit", &m_state.m_fsr.m_config.m_fsr16Bit);
  515. if (ImGui::IsItemHovered() )
  516. {
  517. ImGui::BeginTooltip();
  518. ImGui::Text("For better performance and less memory consumption use 16 Bit precision.");
  519. ImGui::Text("If disabled use 32 Bit per channel precision for FSR which works better on older hardware.");
  520. ImGui::Text("FSR in 16 Bit precision is also prone to be broken in Direct3D11, Direct3D12 works though.");
  521. ImGui::EndTooltip();
  522. }
  523. }
  524. ImGui::Checkbox("Apply FSR", &m_state.m_fsr.m_config.m_applyFsr);
  525. if (ImGui::IsItemHovered() )
  526. {
  527. ImGui::SetTooltip("Compare between FSR and bilinear interpolation of source image.");
  528. }
  529. if (m_state.m_fsr.m_config.m_applyFsr)
  530. {
  531. ImGui::Checkbox("Apply FSR sharpening", &m_state.m_fsr.m_config.m_applyFsrRcas);
  532. if (ImGui::IsItemHovered() )
  533. {
  534. ImGui::SetTooltip("Apply the FSR RCAS sharpening pass.");
  535. }
  536. if (m_state.m_fsr.m_config.m_applyFsrRcas)
  537. {
  538. ImGui::SliderFloat("Sharpening attenuation", &m_state.m_fsr.m_config.m_rcasAttenuation, 0.01f, 2.0f);
  539. if (ImGui::IsItemHovered() )
  540. {
  541. ImGui::SetTooltip("Lower value means sharper.");
  542. }
  543. }
  544. }
  545. }
  546. }
  547. ImGui::End();
  548. imguiEndFrame();
  549. // Advance to next frame. Rendering thread will be kicked to
  550. // process submitted rendering primitives.
  551. m_state.m_currFrame = bgfx::frame();
  552. return true;
  553. }
  554. return false;
  555. }
  556. void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, ModelUniforms &_uniforms)
  557. {
  558. const int32_t width = 6;
  559. const int32_t length = 20;
  560. float c0[] = { 235.0f / 255.0f, 126.0f / 255.0f, 30.0f / 255.0f}; // orange
  561. float c1[] = { 235.0f / 255.0f, 146.0f / 255.0f, 251.0f / 255.0f}; // purple
  562. float c2[] = { 199.0f / 255.0f, 0.0f / 255.0f, 57.0f / 255.0f}; // pink
  563. for (int32_t zz = 0; zz < length; ++zz)
  564. {
  565. // make a color gradient, nothing special about this for example
  566. float *ca = c0;
  567. float *cb = c1;
  568. float lerpVal = float(zz) / float(length);
  569. if (0.5f <= lerpVal)
  570. {
  571. ca = c1;
  572. cb = c2;
  573. }
  574. lerpVal = bx::fract(2.0f * lerpVal);
  575. float r = bx::lerp(ca[0], cb[0], lerpVal);
  576. float g = bx::lerp(ca[1], cb[1], lerpVal);
  577. float b = bx::lerp(ca[2], cb[2], lerpVal);
  578. for (int32_t xx = 0; xx < width; ++xx)
  579. {
  580. const float angle = m_state.m_animationTime + float(zz) * (bx::kPi2 / length) + float(xx) * (bx::kPiHalf / width);
  581. const float posX = 2.0f * xx - width + 1.0f;
  582. const float posY = bx::sin(angle);
  583. const float posZ = 2.0f * zz - length + 1.0f;
  584. const float scale = s_meshScale[MeshHollowCube];
  585. float mtx[16];
  586. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, posX, posY, posZ);
  587. bgfx::setTexture(0, m_state.s_albedo, m_state.m_groundTexture);
  588. bgfx::setTexture(1, m_state.s_normal, m_state.m_normalTexture);
  589. _uniforms.m_color[0] = r;
  590. _uniforms.m_color[1] = g;
  591. _uniforms.m_color[2] = b;
  592. _uniforms.submit();
  593. meshSubmit(m_state.m_meshes[MeshHollowCube], _pass, _program, mtx);
  594. }
  595. }
  596. // draw box as ground plane
  597. {
  598. const float posY = -2.0f;
  599. const float scale = length;
  600. float mtx[16];
  601. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, 0.0f, -scale + posY, 0.0f);
  602. _uniforms.m_color[0] = 0.5f;
  603. _uniforms.m_color[1] = 0.5f;
  604. _uniforms.m_color[2] = 0.5f;
  605. _uniforms.submit();
  606. meshSubmit(m_state.m_meshes[MeshCube], _pass, m_state.m_gridProgram, mtx);
  607. }
  608. }
  609. void resize()
  610. {
  611. destroyFramebuffers();
  612. createFramebuffers();
  613. m_state.m_fsr.resize(m_state.m_width, m_state.m_height);
  614. }
  615. void createFramebuffers()
  616. {
  617. m_state.m_size[0] = m_state.m_width;
  618. m_state.m_size[1] = m_state.m_height;
  619. constexpr uint64_t msaaFlags[] =
  620. {
  621. BGFX_TEXTURE_NONE,
  622. BGFX_TEXTURE_RT_MSAA_X4,
  623. BGFX_TEXTURE_RT_MSAA_X16,
  624. };
  625. const uint64_t msaa = msaaFlags[m_state.m_antiAliasingSetting];
  626. const uint64_t colorFlags = 0
  627. | BGFX_TEXTURE_RT
  628. | BGFX_SAMPLER_U_CLAMP
  629. | BGFX_SAMPLER_V_CLAMP
  630. | msaa
  631. ;
  632. const uint64_t depthFlags = 0
  633. | BGFX_TEXTURE_RT_WRITE_ONLY
  634. | msaa
  635. ;
  636. m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR] = bgfx::createTexture2D(
  637. uint16_t(m_state.m_size[0])
  638. , uint16_t(m_state.m_size[1])
  639. , false
  640. , 1
  641. , bgfx::TextureFormat::RGBA16F
  642. , colorFlags
  643. );
  644. m_state.m_frameBufferTex[FRAMEBUFFER_RT_DEPTH] = bgfx::createTexture2D(
  645. uint16_t(m_state.m_size[0])
  646. , uint16_t(m_state.m_size[1])
  647. , false
  648. , 1
  649. , bgfx::TextureFormat::D32F
  650. , depthFlags
  651. );
  652. m_state.m_frameBuffer = bgfx::createFrameBuffer(
  653. BX_COUNTOF(m_state.m_frameBufferTex)
  654. , m_state.m_frameBufferTex
  655. , true
  656. );
  657. }
  658. // all buffers set to destroy their textures
  659. void destroyFramebuffers()
  660. {
  661. bgfx::destroy(m_state.m_frameBuffer);
  662. }
  663. void updateUniforms()
  664. {
  665. m_state.m_modelUniforms.m_lightPosition[0] = 0.0f;
  666. m_state.m_modelUniforms.m_lightPosition[1] = 6.0f;
  667. m_state.m_modelUniforms.m_lightPosition[2] = 10.0f;
  668. }
  669. AppState m_state;
  670. MagnifierWidget m_magnifierWidget;
  671. };
  672. } // namespace
  673. ENTRY_IMPLEMENT_MAIN(
  674. ExampleFsr
  675. , "46-fsr"
  676. , "AMD FidelityFX Super Resolution (FSR)\n"
  677. "\n"
  678. "For an optimal FSR result high quality antialiasing for the low resolution source image and negative texture LOD bias is recommended."
  679. );