NVScaler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // The MIT License(MIT)
  2. //
  3. // Copyright(c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files(the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #include "NVScaler.h"
  22. #include <dxgi1_4.h>
  23. #include <d3d11_3.h>
  24. #include <d3dcompiler.h>
  25. #include <iostream>
  26. #include "DXUtilities.h"
  27. #include "DeviceResources.h"
  28. #include "Utilities.h"
  29. NVScaler::NVScaler(DeviceResources& deviceResources, const std::vector<std::string>& shaderPaths)
  30. : m_deviceResources(deviceResources)
  31. , m_outputWidth(1)
  32. , m_outputHeight(1)
  33. {
  34. NISOptimizer opt(true, NISGPUArchitecture::NVIDIA_Generic);
  35. m_blockWidth = opt.GetOptimalBlockWidth();
  36. m_blockHeight = opt.GetOptimalBlockHeight();
  37. uint32_t threadGroupSize = opt.GetOptimalThreadGroupSize();
  38. DX::Defines defines;
  39. defines.add("NIS_SCALER", true);
  40. defines.add("NIS_HDR_MODE", uint32_t(NISHDRMode::None));
  41. defines.add("NIS_BLOCK_WIDTH", m_blockWidth);
  42. defines.add("NIS_BLOCK_HEIGHT", m_blockHeight);
  43. defines.add("NIS_THREAD_GROUP_SIZE", threadGroupSize);
  44. std::string shaderName = "NIS_Main.hlsl";
  45. std::string shaderFolder;
  46. for (auto& e : shaderPaths)
  47. {
  48. if (std::filesystem::exists(e + "/" + shaderName))
  49. {
  50. shaderFolder = e;
  51. break;
  52. }
  53. }
  54. if (shaderFolder.empty())
  55. throw std::runtime_error("Shader file not found" + shaderName);
  56. std::wstring wShaderFilename = widen(shaderFolder + "/" + "NIS_Main.hlsl");
  57. DX::IncludeHeader includeHeader({ shaderFolder });
  58. DX::CompileComputeShader(m_deviceResources.device(),
  59. wShaderFilename.c_str(),
  60. "main",
  61. &m_computeShader,
  62. defines.get(),
  63. &includeHeader);
  64. const int rowPitch = kFilterSize * 4;
  65. const int imageSize = rowPitch * kPhaseCount;
  66. m_deviceResources.createTexture2D(kFilterSize / 4, kPhaseCount, DXGI_FORMAT_R32G32B32A32_FLOAT, D3D11_USAGE_DEFAULT,
  67. coef_scale, rowPitch, imageSize, &m_coef_scale);
  68. m_deviceResources.createTexture2D(kFilterSize / 4, kPhaseCount, DXGI_FORMAT_R32G32B32A32_FLOAT, D3D11_USAGE_DEFAULT,
  69. coef_usm, rowPitch, imageSize, &m_coef_usm);
  70. m_deviceResources.createSRV(m_coef_scale.Get(), DXGI_FORMAT_R32G32B32A32_FLOAT, &m_coef_scaleSRV);
  71. m_deviceResources.createSRV(m_coef_usm.Get(), DXGI_FORMAT_R32G32B32A32_FLOAT, &m_coef_usmSRV);
  72. m_deviceResources.createLinearClampSampler(&m_LinearClampSampler);
  73. m_deviceResources.createConstBuffer(&m_config, sizeof(NISConfig), &m_csBuffer);
  74. }
  75. void NVScaler::update(float sharpness, uint32_t inputWidth, uint32_t inputHeight, uint32_t outputWidth, uint32_t outputHeight)
  76. {
  77. NVScalerUpdateConfig(m_config, sharpness,
  78. 0, 0, inputWidth, inputHeight, inputWidth, inputHeight,
  79. 0, 0, outputWidth, outputHeight, outputWidth, outputHeight,
  80. NISHDRMode::None);
  81. m_deviceResources.updateConstBuffer(&m_config, sizeof(NISConfig), m_csBuffer.Get());
  82. m_outputWidth = outputWidth;
  83. m_outputHeight = outputHeight;
  84. }
  85. void NVScaler::dispatch(ID3D11ShaderResourceView* const* input, ID3D11UnorderedAccessView* const* output)
  86. {
  87. auto context = m_deviceResources.context();
  88. context->CSSetShaderResources(0, 1, input);
  89. context->CSSetUnorderedAccessViews(0, 1, output, nullptr);
  90. context->CSSetShaderResources(1, 1, m_coef_scaleSRV.GetAddressOf());
  91. context->CSSetShaderResources(2, 1, m_coef_usmSRV.GetAddressOf());
  92. context->CSSetSamplers(0, 1, m_LinearClampSampler.GetAddressOf());
  93. context->CSSetConstantBuffers(0, 1, m_csBuffer.GetAddressOf());
  94. context->CSSetShader(m_computeShader.Get(), nullptr, 0);
  95. context->Dispatch(UINT(std::ceil(m_outputWidth / float(m_blockWidth))), UINT(std::ceil(m_outputHeight / float(m_blockHeight))), 1);
  96. }