Vulkan-Hpp
shaders.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #include "shaders.hpp"
17 
18 #include "SPIRV/GlslangToSpv.h"
19 #include "glslang/Public/ResourceLimits.h"
20 
21 #include <vulkan/vulkan.hpp>
22 
23 namespace vk
24 {
25  namespace su
26  {
28  {
29  switch ( stage )
30  {
31  case vk::ShaderStageFlagBits::eVertex: return EShLangVertex;
32  case vk::ShaderStageFlagBits::eTessellationControl: return EShLangTessControl;
33  case vk::ShaderStageFlagBits::eTessellationEvaluation: return EShLangTessEvaluation;
34  case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry;
35  case vk::ShaderStageFlagBits::eFragment: return EShLangFragment;
36  case vk::ShaderStageFlagBits::eCompute: return EShLangCompute;
37  case vk::ShaderStageFlagBits::eRaygenNV: return EShLangRayGenNV;
38  case vk::ShaderStageFlagBits::eAnyHitNV: return EShLangAnyHitNV;
39  case vk::ShaderStageFlagBits::eClosestHitNV: return EShLangClosestHitNV;
40  case vk::ShaderStageFlagBits::eMissNV: return EShLangMissNV;
41  case vk::ShaderStageFlagBits::eIntersectionNV: return EShLangIntersectNV;
42  case vk::ShaderStageFlagBits::eCallableNV: return EShLangCallableNV;
43  case vk::ShaderStageFlagBits::eTaskNV: return EShLangTaskNV;
44  case vk::ShaderStageFlagBits::eMeshNV: return EShLangMeshNV;
45  default: assert( false && "Unknown shader stage" ); return EShLangVertex;
46  }
47  }
48 
49  bool GLSLtoSPV( const vk::ShaderStageFlagBits shaderType, std::string const & glslShader, std::vector<unsigned int> & spvShader )
50  {
51  EShLanguage stage = translateShaderStage( shaderType );
52 
53  const char * shaderStrings[1];
54  shaderStrings[0] = glslShader.data();
55 
56  glslang::TShader shader( stage );
57  shader.setStrings( shaderStrings, 1 );
58 
59  // Enable SPIR-V and Vulkan rules when parsing GLSL
60  EShMessages messages = (EShMessages)( EShMsgSpvRules | EShMsgVulkanRules );
61 
62  if ( !shader.parse( GetDefaultResources(), 100, false, messages ) )
63  {
64  puts( shader.getInfoLog() );
65  puts( shader.getInfoDebugLog() );
66  return false; // something didn't work
67  }
68 
69  glslang::TProgram program;
70  program.addShader( &shader );
71 
72  //
73  // Program-level processing...
74  //
75 
76  if ( !program.link( messages ) )
77  {
78  puts( shader.getInfoLog() );
79  puts( shader.getInfoDebugLog() );
80  fflush( stdout );
81  return false;
82  }
83 
84  glslang::GlslangToSpv( *program.getIntermediate( stage ), spvShader );
85  return true;
86  }
87 
88  vk::ShaderModule createShaderModule( vk::Device const & device, vk::ShaderStageFlagBits shaderStage, std::string const & shaderText )
89  {
90  std::vector<unsigned int> shaderSPV;
91  if ( !GLSLtoSPV( shaderStage, shaderText, shaderSPV ) )
92  {
93  throw std::runtime_error( "Could not convert glsl shader to spir-v -> terminating" );
94  }
95 
97  }
98  } // namespace su
99 } // namespace vk
VULKAN_HPP_NODISCARD Result createShaderModule(const vk::ShaderModuleCreateInfo *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::ShaderModule *pShaderModule, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
vk::ShaderModule createShaderModule(vk::Device const &device, vk::ShaderStageFlagBits shaderStage, std::string const &shaderText)
Definition: shaders.cpp:88
bool GLSLtoSPV(const vk::ShaderStageFlagBits shaderType, std::string const &glslShader, std::vector< unsigned int > &spvShader)
Definition: shaders.cpp:49
EShLanguage translateShaderStage(vk::ShaderStageFlagBits stage)
Definition: shaders.cpp:27
Definition: vulkan.cppm:23
ShaderStageFlagBits