app.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 <cmath>
  15. #include <algorithm>
  16. #include "fsr.h"
  17. namespace
  18. {
  19. #define FRAMEBUFFER_RT_COLOR 0
  20. #define FRAMEBUFFER_RT_DEPTH 1
  21. #define FRAMEBUFFER_RENDER_TARGETS 2
  22. enum Meshes
  23. {
  24. MeshCube = 0,
  25. MeshHollowCube
  26. };
  27. static const char *s_meshPaths[] =
  28. {
  29. "meshes/cube.bin",
  30. "meshes/hollowcube.bin"};
  31. static const float s_meshScale[] =
  32. {
  33. 0.45f,
  34. 0.30f};
  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. const bool destroyTextures = true;
  179. m_buffer = bgfx::createFrameBuffer(1, &m_texture, destroyTextures);
  180. }
  181. void destroy()
  182. {
  183. // also responsible for destroying texture
  184. bgfx::destroy(m_buffer);
  185. }
  186. uint32_t m_width;
  187. uint32_t m_height;
  188. bgfx::TextureHandle m_texture;
  189. bgfx::FrameBufferHandle m_buffer;
  190. };
  191. struct MagnifierWidget
  192. {
  193. void init(uint32_t _width, uint32_t _height)
  194. {
  195. m_content.init(_width, _height, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT);
  196. createWidgetTexture(_width + 6, _height + 6);
  197. }
  198. void destroy()
  199. {
  200. bgfx::destroy(m_widgetTexture);
  201. m_content.destroy();
  202. }
  203. void setPosition(float x, float y)
  204. {
  205. m_position.x = x;
  206. m_position.y = y;
  207. }
  208. void drawToScreen(bgfx::ViewId &view, AppState const &state, const bgfx::Caps *caps)
  209. {
  210. float invScreenScaleX = 1.0f / static_cast<float>(state.m_width);
  211. float invScreenScaleY = 1.0f / static_cast<float>(state.m_height);
  212. float scaleX = m_widgetWidth * invScreenScaleX;
  213. float scaleY = m_widgetHeight * invScreenScaleY;
  214. float offsetX = -std::min(std::max(m_position.x - m_widgetWidth * 0.5f, -3.0f), static_cast<float>(state.m_width - m_widgetWidth + 3)) * invScreenScaleX;
  215. float offsetY = -std::min(std::max(m_position.y - m_widgetHeight * 0.5f, -3.0f), static_cast<float>(state.m_height - m_widgetHeight + 3)) * invScreenScaleY;
  216. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS | BGFX_STATE_BLEND_ALPHA);
  217. bgfx::setTexture(0, state.s_color, m_widgetTexture);
  218. screenSpaceTriangle(float(m_widgetWidth), float(m_widgetHeight), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
  219. bgfx::submit(view, state.m_copyLinearToGammaProgram);
  220. }
  221. void updateContent(bgfx::ViewId &view, AppState const &state, const bgfx::Caps *caps, bgfx::TextureHandle srcTexture)
  222. {
  223. float orthoProj[16];
  224. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  225. {
  226. // clear out transform stack
  227. float identity[16];
  228. bx::mtxIdentity(identity);
  229. bgfx::setTransform(identity);
  230. }
  231. float const verticalPos = caps->originBottomLeft ? state.m_height - m_position.y : m_position.y;
  232. float const invMagScaleX = 1.0f / static_cast<float>(m_content.m_width);
  233. float const invMagScaleY = 1.0f / static_cast<float>(m_content.m_height);
  234. float const scaleX = state.m_width * invMagScaleX;
  235. float const scaleY = state.m_height * invMagScaleY;
  236. float const offsetX = std::min(std::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;
  237. float const offsetY = std::min(std::max(verticalPos - m_content.m_height * 0.5f, 0.0f), static_cast<float>(state.m_height - m_content.m_height)) * scaleY / state.m_height;
  238. bgfx::setViewName(view, "magnifier");
  239. bgfx::setViewRect(view, 0, 0, uint16_t(m_content.m_width), uint16_t(m_content.m_height));
  240. bgfx::setViewTransform(view, NULL, orthoProj);
  241. bgfx::setViewFrameBuffer(view, m_content.m_buffer);
  242. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  243. bgfx::setTexture(0, state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  244. screenSpaceTriangle(float(state.m_width), float(state.m_height), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
  245. bgfx::submit(view, state.m_copyLinearToGammaProgram);
  246. ++view;
  247. }
  248. uint32_t m_widgetWidth{0};
  249. uint32_t m_widgetHeight{0};
  250. bgfx::TextureHandle m_widgetTexture;
  251. RenderTarget m_content;
  252. ImVec2 m_position;
  253. private:
  254. void createWidgetTexture(uint32_t _width, uint32_t _height)
  255. {
  256. const bgfx::Memory *mem = bgfx::alloc(_width * _height * sizeof(uint32_t));
  257. uint32_t *pixels = const_cast<uint32_t *>((uint32_t *const)(mem->data));
  258. memset(pixels, 0, mem->size);
  259. uint32_t const white = 0xFFFFFFFF;
  260. uint32_t const black = 0xFF000000;
  261. uint32_t const y0 = 1;
  262. uint32_t const y1 = _height - 3;
  263. for (uint32_t x = 0; x < _width - 4; x++)
  264. {
  265. pixels[(y0 + 0) * _width + x + 1] = white;
  266. pixels[(y0 + 1) * _width + x + 2] = black;
  267. pixels[(y1 + 0) * _width + x + 1] = white;
  268. pixels[(y1 + 1) * _width + x + 2] = black;
  269. }
  270. uint32_t const x0 = 1;
  271. uint32_t const x1 = _width - 3;
  272. for (uint32_t y = 0; y < _height - 3; y++)
  273. {
  274. pixels[(y + 1) * _width + x0 + 0] = white;
  275. pixels[(y + 2) * _width + x0 + 1] = black;
  276. pixels[(y + 1) * _width + x1 + 0] = white;
  277. pixels[(y + 2) * _width + x1 + 1] = black;
  278. }
  279. pixels[(y1 + 0) * _width + 2] = white;
  280. m_widgetWidth = _width;
  281. m_widgetHeight = _height;
  282. m_widgetTexture = bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP, mem);
  283. }
  284. };
  285. class ExampleFsr : public entry::AppI
  286. {
  287. public:
  288. ExampleFsr(const char *_name, const char *_description)
  289. : entry::AppI(_name, _description)
  290. {
  291. }
  292. void init(int32_t _argc, const char *const *_argv, uint32_t _width, uint32_t _height) override
  293. {
  294. Args args(_argc, _argv);
  295. m_state.m_width = _width;
  296. m_state.m_height = _height;
  297. m_state.m_debug = BGFX_DEBUG_NONE;
  298. m_state.m_reset = BGFX_RESET_MAXANISOTROPY;
  299. bgfx::Init init;
  300. init.type = args.m_type;
  301. init.vendorId = args.m_pciId;
  302. init.resolution.width = m_state.m_width;
  303. init.resolution.height = m_state.m_height;
  304. init.resolution.reset = m_state.m_reset;
  305. bgfx::init(init);
  306. // Enable debug text.
  307. bgfx::setDebug(m_state.m_debug);
  308. // Create uniforms for screen passes and models
  309. m_state.m_modelUniforms.init();
  310. // Create texture sampler uniforms (used when we bind textures)
  311. m_state.s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
  312. m_state.s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler);
  313. m_state.s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler);
  314. // Create program from shaders.
  315. m_state.m_forwardProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward");
  316. m_state.m_gridProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward_grid");
  317. m_state.m_copyLinearToGammaProgram = loadProgram("vs_fsr_screenquad", "fs_fsr_copy_linear_to_gamma");
  318. // Load some meshes
  319. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  320. {
  321. m_state.m_meshes[ii] = meshLoad(s_meshPaths[ii]);
  322. }
  323. m_state.m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
  324. m_state.m_normalTexture = loadTexture("textures/fieldstone-n.dds");
  325. createFramebuffers();
  326. // Vertex decl
  327. PosTexCoord0Vertex::init();
  328. // Init camera
  329. cameraCreate();
  330. cameraSetPosition({-10.0f, 2.5f, -0.0f});
  331. cameraSetVerticalAngle(-0.2f);
  332. cameraSetHorizontalAngle(0.8f);
  333. // Init "prev" matrices, will be same for first frame
  334. cameraGetViewMtx(m_state.m_view);
  335. 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);
  336. // Get renderer capabilities info.
  337. const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  338. m_state.m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
  339. const uint32_t magnifierSize = 32;
  340. m_magnifierWidget.init(magnifierSize, magnifierSize);
  341. m_magnifierWidget.setPosition(m_state.m_width * 0.5f, m_state.m_height * 0.5f);
  342. imguiCreate();
  343. m_state.m_fsr.init(_width, _height);
  344. }
  345. int32_t shutdown() override
  346. {
  347. m_state.m_fsr.destroy();
  348. for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
  349. {
  350. meshUnload(m_state.m_meshes[ii]);
  351. }
  352. bgfx::destroy(m_state.m_normalTexture);
  353. bgfx::destroy(m_state.m_groundTexture);
  354. bgfx::destroy(m_state.m_forwardProgram);
  355. bgfx::destroy(m_state.m_gridProgram);
  356. bgfx::destroy(m_state.m_copyLinearToGammaProgram);
  357. m_state.m_modelUniforms.destroy();
  358. m_magnifierWidget.destroy();
  359. bgfx::destroy(m_state.s_albedo);
  360. bgfx::destroy(m_state.s_color);
  361. bgfx::destroy(m_state.s_normal);
  362. destroyFramebuffers();
  363. cameraDestroy();
  364. imguiDestroy();
  365. bgfx::shutdown();
  366. return 0;
  367. }
  368. bool update() override
  369. {
  370. if (!entry::processEvents(m_state.m_width, m_state.m_height, m_state.m_debug, m_state.m_reset, &m_state.m_mouseState))
  371. {
  372. // skip processing when minimized, otherwise crashing
  373. if (0 == m_state.m_width || 0 == m_state.m_height)
  374. {
  375. return true;
  376. }
  377. if (m_state.m_mouseState.m_buttons[entry::MouseButton::Left] && !ImGui::MouseOverArea())
  378. {
  379. m_magnifierWidget.setPosition(static_cast<float>(m_state.m_mouseState.m_mx),
  380. static_cast<float>(m_state.m_mouseState.m_my));
  381. }
  382. // Update frame timer
  383. int64_t now = bx::getHPCounter();
  384. static int64_t last = now;
  385. const int64_t frameTime = now - last;
  386. last = now;
  387. const double freq = double(bx::getHPFrequency());
  388. const float deltaTime = float(frameTime / freq);
  389. const bgfx::Caps *caps = bgfx::getCaps();
  390. if (m_state.m_size[0] != (int32_t)m_state.m_width || m_state.m_size[1] != (int32_t)m_state.m_height)
  391. {
  392. resize();
  393. }
  394. // update animation time
  395. const float rotationSpeed = 0.25f;
  396. if (m_state.m_animateScene)
  397. {
  398. m_state.m_animationTime += deltaTime * rotationSpeed;
  399. if (bx::kPi2 < m_state.m_animationTime)
  400. {
  401. m_state.m_animationTime -= bx::kPi2;
  402. }
  403. }
  404. // Update camera
  405. cameraUpdate(deltaTime * 0.15f, m_state.m_mouseState, ImGui::MouseOverArea());
  406. cameraGetViewMtx(m_state.m_view);
  407. updateUniforms();
  408. 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, caps->homogeneousDepth);
  409. bgfx::ViewId view = 0;
  410. // Draw models into scene
  411. {
  412. bgfx::setViewName(view, "forward scene");
  413. bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x7fb8ffff, 1.0f, 0);
  414. float const viewScale = m_state.m_renderNativeResolution ? 1.0f : 1.0f / m_state.m_fsr.m_config.m_superSamplingFactor;
  415. uint16_t const viewRectWidth = uint16_t(ceilf(m_state.m_size[0] * viewScale));
  416. uint16_t const viewRectHeight = uint16_t(ceilf(m_state.m_size[1] * viewScale));
  417. uint16_t const viewRectY = caps->originBottomLeft ? m_state.m_size[1] - viewRectHeight : 0;
  418. bgfx::setViewRect(view, 0, viewRectY, viewRectWidth, viewRectHeight);
  419. bgfx::setViewTransform(view, m_state.m_view, m_state.m_proj);
  420. bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
  421. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS);
  422. drawAllModels(view, m_state.m_forwardProgram, m_state.m_modelUniforms);
  423. ++view;
  424. }
  425. // optionally run FSR
  426. if (!m_state.m_renderNativeResolution)
  427. {
  428. view = m_state.m_fsr.computeFsr(view, m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR]);
  429. }
  430. // render result to screen
  431. {
  432. bgfx::TextureHandle srcTexture = m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR];
  433. if (!m_state.m_renderNativeResolution)
  434. {
  435. srcTexture = m_state.m_fsr.getResultTexture();
  436. }
  437. m_magnifierWidget.updateContent(view, m_state, caps, srcTexture);
  438. float orthoProj[16];
  439. bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  440. {
  441. // clear out transform stack
  442. float identity[16];
  443. bx::mtxIdentity(identity);
  444. bgfx::setTransform(identity);
  445. }
  446. bgfx::setViewName(view, "display");
  447. bgfx::setViewClear(view, BGFX_CLEAR_NONE, 0, 1.0f, 0);
  448. bgfx::setViewRect(view, 0, 0, uint16_t(m_state.m_width), uint16_t(m_state.m_height));
  449. bgfx::setViewTransform(view, NULL, orthoProj);
  450. bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
  451. bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
  452. bgfx::setTexture(0, m_state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  453. screenSpaceTriangle(float(m_state.m_width), float(m_state.m_height), m_state.m_texelHalf, caps->originBottomLeft);
  454. bgfx::submit(view, m_state.m_copyLinearToGammaProgram);
  455. }
  456. m_magnifierWidget.drawToScreen(view, m_state, caps);
  457. ++view;
  458. // Draw UI
  459. 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));
  460. showExampleDialog(this);
  461. ImGui::SetNextWindowPos(ImVec2(m_state.m_width - m_state.m_width / 4.0f - 10.0f, 10.0f), ImGuiCond_FirstUseEver);
  462. ImGui::SetNextWindowSize(ImVec2(m_state.m_width / 4.0f, m_state.m_height / 1.2f), ImGuiCond_FirstUseEver);
  463. ImGui::Begin("Settings", NULL, 0);
  464. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  465. ImVec2 const itemSize = ImGui::GetItemRectSize();
  466. {
  467. ImGui::Checkbox("Animate scene", &m_state.m_animateScene);
  468. if (ImGui::Combo("Antialiasing", &m_state.m_antiAliasingSetting, "none\0"
  469. "4x\0"
  470. "16x\0"
  471. "\0"))
  472. {
  473. resize();
  474. }
  475. ImGui::Checkbox("Render native resolution", &m_state.m_renderNativeResolution);
  476. if (ImGui::IsItemHovered())
  477. ImGui::SetTooltip("Disable super sampling and FSR.");
  478. ImGui::Image(m_magnifierWidget.m_content.m_texture, ImVec2(itemSize.x * 0.94f, itemSize.x * 0.94f));
  479. if (!m_state.m_renderNativeResolution)
  480. {
  481. ImGui::SliderFloat("Super sampling", &m_state.m_fsr.m_config.m_superSamplingFactor, 1.0f, 2.0f);
  482. if (ImGui::IsItemHovered())
  483. {
  484. ImGui::BeginTooltip();
  485. ImGui::Text("2.0 means the scene is rendered at half window resolution.");
  486. ImGui::Text("1.0 means the scene is rendered at native window resolution.");
  487. ImGui::EndTooltip();
  488. }
  489. ImGui::Separator();
  490. if (m_state.m_fsr.supports16BitPrecision())
  491. {
  492. ImGui::Checkbox("Use 16 Bit", &m_state.m_fsr.m_config.m_fsr16Bit);
  493. if (ImGui::IsItemHovered())
  494. {
  495. ImGui::BeginTooltip();
  496. ImGui::Text("For better performance and less memory consumption use 16 Bit precision.");
  497. ImGui::Text("If disabled use 32 Bit per channel precision for FSR which works better on older hardware.");
  498. ImGui::Text("FSR in 16 Bit precision is also prone to be broken in Direct3D11, Direct3D12 works though.");
  499. ImGui::EndTooltip();
  500. }
  501. }
  502. ImGui::Checkbox("Apply FSR", &m_state.m_fsr.m_config.m_applyFsr);
  503. if (ImGui::IsItemHovered())
  504. ImGui::SetTooltip("Compare between FSR and bilinear interpolation of source image.");
  505. if (m_state.m_fsr.m_config.m_applyFsr)
  506. {
  507. ImGui::Checkbox("Apply FSR sharpening", &m_state.m_fsr.m_config.m_applyFsrRcas);
  508. if (ImGui::IsItemHovered())
  509. ImGui::SetTooltip("Apply the FSR RCAS sharpening pass.");
  510. if (m_state.m_fsr.m_config.m_applyFsrRcas)
  511. {
  512. ImGui::SliderFloat("Sharpening attenuation", &m_state.m_fsr.m_config.m_rcasAttenuation, 0.01f, 2.0f);
  513. if (ImGui::IsItemHovered())
  514. ImGui::SetTooltip("Lower value means sharper.");
  515. }
  516. }
  517. }
  518. }
  519. ImGui::End();
  520. imguiEndFrame();
  521. // Advance to next frame. Rendering thread will be kicked to
  522. // process submitted rendering primitives.
  523. m_state.m_currFrame = bgfx::frame();
  524. return true;
  525. }
  526. return false;
  527. }
  528. void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, ModelUniforms &_uniforms)
  529. {
  530. const int32_t width = 6;
  531. const int32_t length = 20;
  532. float c0[] = {235.0f / 255.0f, 126.0f / 255.0f, 30.0f / 255.0f}; // orange
  533. float c1[] = {235.0f / 255.0f, 146.0f / 255.0f, 251.0f / 255.0f}; // purple
  534. float c2[] = {199.0f / 255.0f, 0.0f / 255.0f, 57.0f / 255.0f}; // pink
  535. for (int32_t zz = 0; zz < length; ++zz)
  536. {
  537. // make a color gradient, nothing special about this for example
  538. float *ca = c0;
  539. float *cb = c1;
  540. float lerpVal = float(zz) / float(length);
  541. if (0.5f <= lerpVal)
  542. {
  543. ca = c1;
  544. cb = c2;
  545. }
  546. lerpVal = bx::fract(2.0f * lerpVal);
  547. float r = bx::lerp(ca[0], cb[0], lerpVal);
  548. float g = bx::lerp(ca[1], cb[1], lerpVal);
  549. float b = bx::lerp(ca[2], cb[2], lerpVal);
  550. for (int32_t xx = 0; xx < width; ++xx)
  551. {
  552. const float angle = m_state.m_animationTime + float(zz) * (bx::kPi2 / length) + float(xx) * (bx::kPiHalf / width);
  553. const float posX = 2.0f * xx - width + 1.0f;
  554. const float posY = bx::sin(angle);
  555. const float posZ = 2.0f * zz - length + 1.0f;
  556. const float scale = s_meshScale[MeshHollowCube];
  557. float mtx[16];
  558. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, posX, posY, posZ);
  559. bgfx::setTexture(0, m_state.s_albedo, m_state.m_groundTexture);
  560. bgfx::setTexture(1, m_state.s_normal, m_state.m_normalTexture);
  561. _uniforms.m_color[0] = r;
  562. _uniforms.m_color[1] = g;
  563. _uniforms.m_color[2] = b;
  564. _uniforms.submit();
  565. meshSubmit(m_state.m_meshes[MeshHollowCube], _pass, _program, mtx);
  566. }
  567. }
  568. // draw box as ground plane
  569. {
  570. const float posY = -2.0f;
  571. const float scale = length;
  572. float mtx[16];
  573. bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, 0.0f, -scale + posY, 0.0f);
  574. _uniforms.m_color[0] = 0.5f;
  575. _uniforms.m_color[1] = 0.5f;
  576. _uniforms.m_color[2] = 0.5f;
  577. _uniforms.submit();
  578. meshSubmit(m_state.m_meshes[MeshCube], _pass, m_state.m_gridProgram, mtx);
  579. }
  580. }
  581. void resize()
  582. {
  583. destroyFramebuffers();
  584. createFramebuffers();
  585. m_state.m_fsr.resize(m_state.m_width, m_state.m_height);
  586. }
  587. void createFramebuffers()
  588. {
  589. m_state.m_size[0] = m_state.m_width;
  590. m_state.m_size[1] = m_state.m_height;
  591. uint64_t constexpr msaaFlags[] = {BGFX_TEXTURE_NONE, BGFX_TEXTURE_RT_MSAA_X4, BGFX_TEXTURE_RT_MSAA_X16};
  592. const uint64_t msaa = msaaFlags[m_state.m_antiAliasingSetting];
  593. const uint64_t colorFlags = 0 | BGFX_TEXTURE_RT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | msaa;
  594. const uint64_t depthFlags = 0 | BGFX_TEXTURE_RT_WRITE_ONLY | msaa;
  595. m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR] = bgfx::createTexture2D(uint16_t(m_state.m_size[0]), uint16_t(m_state.m_size[1]), false, 1, bgfx::TextureFormat::RGBA16F, colorFlags);
  596. m_state.m_frameBufferTex[FRAMEBUFFER_RT_DEPTH] = bgfx::createTexture2D(uint16_t(m_state.m_size[0]), uint16_t(m_state.m_size[1]), false, 1, bgfx::TextureFormat::D24S8, depthFlags);
  597. m_state.m_frameBuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_state.m_frameBufferTex), m_state.m_frameBufferTex, true);
  598. }
  599. // all buffers set to destroy their textures
  600. void destroyFramebuffers()
  601. {
  602. bgfx::destroy(m_state.m_frameBuffer);
  603. }
  604. void updateUniforms()
  605. {
  606. m_state.m_modelUniforms.m_lightPosition[0] = 0.0f;
  607. m_state.m_modelUniforms.m_lightPosition[1] = 6.0f;
  608. m_state.m_modelUniforms.m_lightPosition[2] = 10.0f;
  609. }
  610. AppState m_state;
  611. MagnifierWidget m_magnifierWidget;
  612. };
  613. } // namespace
  614. ENTRY_IMPLEMENT_MAIN(ExampleFsr, "46-fsr", "AMD FidelityFX Super Resolution (FSR)\n\nFor an optimal FSR result high quality antialiasing for the low resolution source image and negative texture LOD bias is recommended.");