VKUtilities.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <fstream>
  23. #include <vector>
  24. // With NIS_DXC, bindings are specified in HLSL shader.
  25. // Otherwise, we adjust bindings at build-time using arguments to dxc.exe
  26. #ifdef NIS_DXC
  27. static const uint32_t CB_BINDING = 0;
  28. static const uint32_t IN_TEX_BINDING = 2;
  29. static const uint32_t OUT_TEX_BINDING = 3;
  30. static const uint32_t COEF_SCALAR_BINDING = 4;
  31. static const uint32_t COEF_USM_BINDING = 5;
  32. #else
  33. static const uint32_t CB_BINDING = 0;
  34. static const uint32_t IN_TEX_BINDING = 3;
  35. static const uint32_t OUT_TEX_BINDING = 2;
  36. static const uint32_t COEF_SCALAR_BINDING = 4;
  37. static const uint32_t COEF_USM_BINDING = 5;
  38. #endif
  39. static const VkDescriptorType CB_DESC_TYPE = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  40. static const VkDescriptorType IN_TEX_DESC_TYPE = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  41. static const VkDescriptorType OUT_TEX_DESC_TYPE = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  42. #define VK_COMMON_DESC_LAYOUT(immutableSampler) \
  43. { CB_BINDING, CB_DESC_TYPE, 1, VK_SHADER_STAGE_COMPUTE_BIT}, \
  44. { 1, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, (immutableSampler) }, \
  45. { OUT_TEX_BINDING, OUT_TEX_DESC_TYPE, 1, VK_SHADER_STAGE_COMPUTE_BIT }, \
  46. { IN_TEX_BINDING, IN_TEX_DESC_TYPE, 1, VK_SHADER_STAGE_COMPUTE_BIT }
  47. // TODO: combine with DXUtilities.h:IncludeHeader::Open()
  48. inline std::vector<char> readBytes(const std::string& filename)
  49. {
  50. std::ifstream file(filename, std::ios::ate | std::ios::binary);
  51. if (!file.is_open())
  52. {
  53. fprintf(stderr, "Failed to open: %s\n", filename.c_str());
  54. assert(0);
  55. }
  56. const auto sizeBytes = file.tellg();
  57. std::vector<char> buffer(sizeBytes);
  58. file.seekg(0);
  59. file.read(buffer.data(), sizeBytes);
  60. file.close();
  61. return buffer;
  62. }