NAP
shader.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "rendertarget.h"
9 #include "vertexattributedeclaration.h"
10 #include "samplerdeclaration.h"
11 #include "shadervariabledeclarations.h"
12 #include "shaderconstant.h"
13 #include "shaderconstantdeclaration.h"
14 #include "uniform.h"
15 
16 // External Includes
17 #include <utility/dllexport.h>
18 #include <nap/resource.h>
19 #include <rtti/factory.h>
20 
21 namespace nap
22 {
23  // Forward Declares
24  class RenderService;
25  class Core;
26 
30  class NAPAPI BaseShader : public Resource
31  {
32  RTTI_ENABLE(Resource)
33  public:
34  BaseShader(Core& core);
35  virtual ~BaseShader();
36 
40  const SamplerDeclarations& getSamplerDeclarations() const { return mSamplerDeclarations; }
41 
45  const std::vector<BufferObjectDeclaration>& getUBODeclarations() const { return mUBODeclarations; }
46 
50  const std::vector<BufferObjectDeclaration>& getSSBODeclarations() const { return mSSBODeclarations; }
51 
55  const ShaderConstantDeclarations& getConstantDeclarations() const { return mConstantDeclarations; }
56 
60  const std::string& getDisplayName() const { return mDisplayName; }
61 
65  VkDescriptorSetLayout getDescriptorSetLayout() const { return mDescriptorSetLayout; }
66 
70  void clear();
71 
72  protected:
73  RenderService* mRenderService = nullptr;
74  std::string mDisplayName;
79  VkDescriptorSetLayout mDescriptorSetLayout = VK_NULL_HANDLE;
80 
87  bool initLayout(VkDevice device, nap::utility::ErrorState& errorState);
88 
94  bool verifyShaderVariableDeclarations(utility::ErrorState& errorState);
95  };
96 
97 
105  class NAPAPI Shader : public BaseShader
106  {
107  RTTI_ENABLE(BaseShader)
108  public:
113  {
114  Vertex = 0x01,
115  Fragment = 0x02
116  };
118 
119  Shader(Core& core);
120  ~Shader();
121 
125  const VertexAttributeDeclarations& getAttributes() const { return mShaderAttributes; }
126 
130  VkShaderModule getVertexModule() const { return mVertexModule; }
131 
135  VkShaderModule getFragmentModule() const { return mFragmentModule; }
136 
137  protected:
150  bool load(const std::string& displayName, const std::vector<std::string>& searchPaths, const char* vertShader, int vertSize, const char* fragShader, int fragSize, utility::ErrorState& errorState);
151 
160  bool loadDefault(const std::string& displayName, utility::ErrorState& errorState);
161 
162  private:
163  VertexAttributeDeclarations mShaderAttributes;
164  VkShaderModule mVertexModule = VK_NULL_HANDLE;
165  VkShaderModule mFragmentModule = VK_NULL_HANDLE;
166  };
167 
168 
176  class NAPAPI ComputeShader : public BaseShader
177  {
178  RTTI_ENABLE(BaseShader)
179  public:
180  ComputeShader(Core& core);
181  ~ComputeShader();
182 
186  VkShaderModule getComputeModule() const { return mComputeModule; }
187 
191  const glm::uvec3& getWorkGroupSize() const { return mWorkGroupSize; }
192 
198  const std::unordered_map<uint, std::string>& getWorkGroupSizeOverrides() const { return mWorkGroupSizeOverrides; }
199 
200  bool mEnableMaxGroupSizeDefault = false;
201 
202  protected:
213  virtual bool load(const std::string& displayName, const std::vector<std::string>& searchPaths, const char* compShader, int compSize, utility::ErrorState& errorState);
214 
215  private:
216  glm::uvec3 mWorkGroupSize;
217  std::unordered_map<uint, std::string> mWorkGroupSizeOverrides;
218  VkShaderModule mComputeModule = VK_NULL_HANDLE;
219  };
220 
221 
227  class NAPAPI ShaderFromFile : public Shader
228  {
229  RTTI_ENABLE(Shader)
230  public:
231  ShaderFromFile(Core& core);
232 
238  virtual bool init(utility::ErrorState& error) override;
239 
240  std::string mVertPath;
241  std::string mFragPath;
242  bool mRestrictModuleIncludes = false;
243  };
244 
245 
251  class NAPAPI ComputeShaderFromFile : public ComputeShader
252  {
253  RTTI_ENABLE(ComputeShader)
254  public:
256 
262  virtual bool init(utility::ErrorState& error) override;
263 
264  std::string mComputePath;
265  bool mRestrictModuleIncludes = false;
266  };
267 }
268 
nap::ComputeShader
Definition: shader.h:176
nap::ShaderFromFile::mFragPath
std::string mFragPath
Property: 'mFragShader' path to the fragment shader on disk.
Definition: shader.h:241
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::BaseShader::mSamplerDeclarations
SamplerDeclarations mSamplerDeclarations
All sampler declarations.
Definition: shader.h:77
nap::BaseShader::mUBODeclarations
BufferObjectDeclarationList mUBODeclarations
All uniform buffer object declarations.
Definition: shader.h:75
nap::BaseShader::getConstantDeclarations
const ShaderConstantDeclarations & getConstantDeclarations() const
Definition: shader.h:55
nap::BaseShader::mDisplayName
std::string mDisplayName
Filename of shader used as display name.
Definition: shader.h:74
nap::VertexAttributeDeclarations
std::unordered_map< std::string, std::unique_ptr< VertexAttributeDeclaration > > VertexAttributeDeclarations
Definition: vertexattributedeclaration.h:31
nap::ComputeShader::getWorkGroupSizeOverrides
const std::unordered_map< uint, std::string > & getWorkGroupSizeOverrides() const
Definition: shader.h:198
nap::ShaderFromFile::mVertPath
std::string mVertPath
Property: 'mVertShader' path to the vertex shader on disk.
Definition: shader.h:240
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ShaderFromFile
Definition: shader.h:227
nap::ComputeShaderFromFile
Definition: shader.h:251
nap::ComputeShader::getWorkGroupSize
const glm::uvec3 & getWorkGroupSize() const
Definition: shader.h:191
nap::BaseShader::getSamplerDeclarations
const SamplerDeclarations & getSamplerDeclarations() const
Definition: shader.h:40
nap::Shader
Definition: shader.h:105
nap::BaseShader::mSSBODeclarations
BufferObjectDeclarationList mSSBODeclarations
All storage buffer object declarations.
Definition: shader.h:76
nap::ShaderConstantDeclarations
std::vector< ShaderConstantDeclaration > ShaderConstantDeclarations
Definition: shaderconstantdeclaration.h:43
nap::RenderService
Definition: renderservice.h:275
nap::Core
Definition: core.h:82
nap::BaseShader::getDisplayName
const std::string & getDisplayName() const
Definition: shader.h:60
nap::Shader::getVertexModule
VkShaderModule getVertexModule() const
Definition: shader.h:130
nap::Shader::getAttributes
const VertexAttributeDeclarations & getAttributes() const
Definition: shader.h:125
nap::BaseShader::getUBODeclarations
const std::vector< BufferObjectDeclaration > & getUBODeclarations() const
Definition: shader.h:45
nap::Shader::ShaderTypeFlags
uint ShaderTypeFlags
Definition: shader.h:117
nap::BufferObjectDeclarationList
std::vector< nap::BufferObjectDeclaration > BufferObjectDeclarationList
Definition: shadervariabledeclarations.h:191
nap::ComputeShaderFromFile::mComputePath
std::string mComputePath
Property: 'ComputeShader' path to the vertex shader on disk.
Definition: shader.h:264
nap::BaseShader::getSSBODeclarations
const std::vector< BufferObjectDeclaration > & getSSBODeclarations() const
Definition: shader.h:50
nap
Definition: templateapp.h:17
nap::SamplerDeclarations
std::vector< SamplerDeclaration > SamplerDeclarations
Definition: samplerdeclaration.h:46
nap::BaseShader::getDescriptorSetLayout
VkDescriptorSetLayout getDescriptorSetLayout() const
Definition: shader.h:65
nap::Resource
Definition: resource.h:19
nap::BaseShader::mConstantDeclarations
ShaderConstantDeclarations mConstantDeclarations
All shader constant declarations.
Definition: shader.h:78
nap::BaseShader
Definition: shader.h:30
nap::Shader::getFragmentModule
VkShaderModule getFragmentModule() const
Definition: shader.h:135
nap::Shader::EShaderType
EShaderType
Definition: shader.h:112
nap::ComputeShader::getComputeModule
VkShaderModule getComputeModule() const
Definition: shader.h:186