DXUtilities.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  22. #include <stdio.h>
  23. #include <tchar.h>
  24. #include <dxgi1_4.h>
  25. #include <d3d12.h>
  26. #include <d3dcompiler.h>
  27. #include <wrl.h>
  28. #include <iostream>
  29. #include <fstream>
  30. #include <string>
  31. #include <vector>
  32. namespace DX
  33. {
  34. using namespace Microsoft::WRL;
  35. inline LPTSTR GetErrorDescription(HRESULT hr, WCHAR* buffer, size_t size)
  36. {
  37. if (FACILITY_WINDOWS == HRESULT_FACILITY(hr))
  38. hr = HRESULT_CODE(hr);
  39. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  40. nullptr, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, DWORD(size), nullptr);
  41. return buffer;
  42. }
  43. inline void ThrowIfFailed(HRESULT hr)
  44. {
  45. if (FAILED(hr))
  46. {
  47. // Set a breakpoint on this line to catch Win32 API errors.
  48. const size_t size = 1024;
  49. WCHAR buffer[size];
  50. GetErrorDescription(hr, buffer, size);
  51. char str[size];
  52. size_t i = 0;
  53. wcstombs_s(&i, str, 1024, buffer, 1024);
  54. throw std::runtime_error(str);
  55. }
  56. }
  57. inline std::vector<uint8_t> ReadBinaryFile(const std::string& filename)
  58. {
  59. std::ifstream ifs(filename, std::ios::in | std::ios::binary | std::ios::ate);
  60. if (!ifs)
  61. throw std::runtime_error("ReadBinaryFile");
  62. std::streampos len = ifs.tellg();
  63. std::vector<uint8_t> data;
  64. data.resize(size_t(len));
  65. ifs.seekg(0, std::ios::beg);
  66. ifs.read(reinterpret_cast<char*>(data.data()), len);
  67. ifs.close();
  68. return data;
  69. }
  70. }