1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HGIVULKAN_SHADERCOMPILER_H
25 #define PXR_IMAGING_HGIVULKAN_SHADERCOMPILER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hgi/enums.h"
29 #include "pxr/imaging/hgiVulkan/api.h"
30 #include "pxr/imaging/hgiVulkan/vulkan.h"
31 
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string>
35 #include <vector>
36 
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class HgiVulkanDevice;
41 
42 struct HgiVulkanDescriptorSetInfo
43 {
44   uint32_t setNumber;
45   VkDescriptorSetLayoutCreateInfo createInfo;
46   std::vector<VkDescriptorSetLayoutBinding> bindings;
47 };
48 
49 using HgiVulkanDescriptorSetInfoVector =
50     std::vector<HgiVulkanDescriptorSetInfo>;
51 
52 using VkDescriptorSetLayoutVector = std::vector<VkDescriptorSetLayout>;
53 
54 
55 /// Compiles ascii shader code (glsl) into spirv binary code (spirvOut).
56 /// Returns true if successful. Errors can optionally be captured.
57 /// numShaderCodes determines how many strings are provided via shaderCodes.
58 /// 'name' is purely for debugging compile errors. It can be anything.
59 HGIVULKAN_API
60 bool HgiVulkanCompileGLSL(
61     const char* name,
62     const char* shaderCodes[],
63     uint8_t numShaderCodes,
64     HgiShaderStage stage,
65     std::vector<unsigned int>* spirvOUT,
66     std::string* errors = nullptr);
67 
68 /// Uses spirv-reflection to create new descriptor set layout information for
69 /// the provided spirv.
70 /// This information can be merged with the info of the other shader stage
71 /// modules to create the pipeline layout.
72 /// During Hgi pipeline layout creation we know the shader modules
73 /// (HgiShaderProgram), but not the HgiResourceBindings so we must use
74 /// spirv reflection to discover the descriptorSet info for the module.
75 HGIVULKAN_API
76 HgiVulkanDescriptorSetInfoVector HgiVulkanGatherDescriptorSetInfo(
77     std::vector<unsigned int> const& spirv);
78 
79 /// Given all of the DescriptorSetInfos of all of the shader modules in a
80 /// shader program, this function merges them and creates the descriptorSet
81 /// layouts needed during pipeline layout creation.
82 /// The caller takes ownership of the returned layouts and must destroy them.
83 HGIVULKAN_API
84 VkDescriptorSetLayoutVector HgiVulkanMakeDescriptorSetLayouts(
85     HgiVulkanDevice* device,
86     std::vector<HgiVulkanDescriptorSetInfoVector> const& infos,
87     std::string const& debugName);
88 
89 PXR_NAMESPACE_CLOSE_SCOPE
90 
91 #endif
92