Vulkan-Hpp
shaders.hpp
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 <string>
17 #include <vector>
18 #include <vulkan/vulkan.hpp>
19 
20 namespace vk
21 {
22  namespace su
23  {
24  vk::ShaderModule createShaderModule( vk::Device const & device, vk::ShaderStageFlagBits shaderStage, std::string const & shaderText );
25 
26  bool GLSLtoSPV( const vk::ShaderStageFlagBits shaderType, std::string const & glslShader, std::vector<unsigned int> & spvShader );
27  } // namespace su
28 } // namespace vk
29 
30 // vertex shader with (P)osition and (C)olor in and (C)olor out
31 const std::string vertexShaderText_PC_C = R"(
32 #version 400
33 
34 #extension GL_ARB_separate_shader_objects : enable
35 #extension GL_ARB_shading_language_420pack : enable
36 
37 layout (std140, binding = 0) uniform buffer
38 {
39  mat4 mvp;
40 } uniformBuffer;
41 
42 layout (location = 0) in vec4 pos;
43 layout (location = 1) in vec4 inColor;
44 
45 layout (location = 0) out vec4 outColor;
46 
47 void main()
48 {
49  outColor = inColor;
50  gl_Position = uniformBuffer.mvp * pos;
51 }
52 )";
53 
54 // vertex shader with (P)osition and (T)exCoord in and (T)exCoord out
55 const std::string vertexShaderText_PT_T = R"(
56 #version 400
57 
58 #extension GL_ARB_separate_shader_objects : enable
59 #extension GL_ARB_shading_language_420pack : enable
60 
61 layout (std140, binding = 0) uniform buffer
62 {
63  mat4 mvp;
64 } uniformBuffer;
65 
66 layout (location = 0) in vec4 pos;
67 layout (location = 1) in vec2 inTexCoord;
68 
69 layout (location = 0) out vec2 outTexCoord;
70 
71 void main()
72 {
73  outTexCoord = inTexCoord;
74  gl_Position = uniformBuffer.mvp * pos;
75 }
76 )";
77 
78 // fragment shader with (C)olor in and (C)olor out
79 const std::string fragmentShaderText_C_C = R"(
80 #version 400
81 
82 #extension GL_ARB_separate_shader_objects : enable
83 #extension GL_ARB_shading_language_420pack : enable
84 
85 layout (location = 0) in vec4 color;
86 
87 layout (location = 0) out vec4 outColor;
88 
89 void main()
90 {
91  outColor = color;
92 }
93 )";
94 
95 // fragment shader with (T)exCoord in and (C)olor out
96 const std::string fragmentShaderText_T_C = R"(
97 #version 400
98 
99 #extension GL_ARB_separate_shader_objects : enable
100 #extension GL_ARB_shading_language_420pack : enable
101 
102 layout (binding = 1) uniform sampler2D tex;
103 
104 layout (location = 0) in vec2 inTexCoord;
105 
106 layout (location = 0) out vec4 outColor;
107 
108 void main()
109 {
110  outColor = texture(tex, inTexCoord);
111 }
112 )";
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
Definition: vulkan.cppm:23
ShaderStageFlagBits
const std::string fragmentShaderText_T_C
Definition: shaders.hpp:96
const std::string vertexShaderText_PC_C
Definition: shaders.hpp:31
const std::string fragmentShaderText_C_C
Definition: shaders.hpp:79
const std::string vertexShaderText_PT_T
Definition: shaders.hpp:55