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 "uniform.h"
13 
14 // External Includes
15 #include <utility/dllexport.h>
16 #include <nap/resource.h>
17 #include <rtti/factory.h>
18 
19 namespace nap
20 {
21  // Forward Declares
22  class RenderService;
23  class Core;
24 
28  class NAPAPI BaseShader : public Resource
29  {
30  RTTI_ENABLE(Resource)
31  public:
32  BaseShader(Core& core);
33  virtual ~BaseShader();
34 
38  const SamplerDeclarations& getSamplerDeclarations() const { return mSamplerDeclarations; }
39 
43  const std::vector<BufferObjectDeclaration>& getUBODeclarations() const { return mUBODeclarations; }
44 
48  const std::vector<BufferObjectDeclaration>& getSSBODeclarations() const { return mSSBODeclarations; }
49 
53  const std::string& getDisplayName() const { return mDisplayName; }
54 
58  VkDescriptorSetLayout getDescriptorSetLayout() const { return mDescriptorSetLayout; }
59 
60  protected:
61  RenderService* mRenderService = nullptr;
62  std::string mDisplayName;
66  VkDescriptorSetLayout mDescriptorSetLayout = VK_NULL_HANDLE;
67 
74  bool initLayout(VkDevice device, nap::utility::ErrorState& errorState);
75 
81  bool verifyShaderVariableDeclarations(utility::ErrorState& errorState);
82  };
83 
84 
92  class NAPAPI Shader : public BaseShader
93  {
94  public:
95  Shader(Core& core);
96  ~Shader();
97 
101  const VertexAttributeDeclarations& getAttributes() const { return mShaderAttributes; }
102 
106  VkShaderModule getVertexModule() const { return mVertexModule; }
107 
111  VkShaderModule getFragmentModule() const { return mFragmentModule; }
112 
113  protected:
125  bool load(const std::string& displayName, const char* vertShader, int vertSize, const char* fragShader, int fragSize, utility::ErrorState& errorState);
126 
135  bool loadDefault(const std::string& displayName, utility::ErrorState& errorState);
136 
137  private:
138  VertexAttributeDeclarations mShaderAttributes;
139  VkShaderModule mVertexModule = VK_NULL_HANDLE;
140  VkShaderModule mFragmentModule = VK_NULL_HANDLE;
141  };
142 
143 
151  class NAPAPI ComputeShader : public BaseShader
152  {
153  RTTI_ENABLE(Resource)
154  public:
155  ComputeShader(Core& core);
156  ~ComputeShader();
157 
161  VkShaderModule getComputeModule() const { return mComputeModule; }
162 
166  glm::u32vec3 getWorkGroupSize() const { return mWorkGroupSize; }
167 
175  const std::vector<int>& getWorkGroupSizeConstantIds() const { return mWorkGroupSizeConstantIds; }
176 
177  protected:
187  virtual bool load(const std::string& displayName, const char* compShader, int compSize, utility::ErrorState& errorState);
188 
189  private:
190  glm::u32vec3 mWorkGroupSize;
191  VkShaderModule mComputeModule = VK_NULL_HANDLE;
192 
193  std::vector<int> mWorkGroupSizeConstantIds;
194  };
195 
196 
202  class NAPAPI ShaderFromFile : public Shader
203  {
204  RTTI_ENABLE(Shader)
205  public:
206  ShaderFromFile(Core& core);
207 
213  virtual bool init(utility::ErrorState& error) override;
214 
215  std::string mVertPath;
216  std::string mFragPath;
217  };
218 
219 
225  class NAPAPI ComputeShaderFromFile : public ComputeShader
226  {
227  RTTI_ENABLE(ComputeShader)
228  public:
230 
236  virtual bool init(utility::ErrorState& error) override;
237 
238  std::string mComputePath;
239  };
240 }
241 
nap::ComputeShader
Definition: shader.h:151
nap::ShaderFromFile::mFragPath
std::string mFragPath
Property: 'mFragShader' path to the fragment shader on disk.
Definition: shader.h:216
nap::BaseShader::mSamplerDeclarations
SamplerDeclarations mSamplerDeclarations
All sampler declarations.
Definition: shader.h:65
nap::BaseShader::mUBODeclarations
BufferObjectDeclarationList mUBODeclarations
All uniform buffer object declarations.
Definition: shader.h:63
nap::BaseShader::mDisplayName
std::string mDisplayName
Filename of shader used as display name.
Definition: shader.h:62
nap::VertexAttributeDeclarations
std::unordered_map< std::string, std::unique_ptr< VertexAttributeDeclaration > > VertexAttributeDeclarations
Definition: vertexattributedeclaration.h:31
nap::ShaderFromFile::mVertPath
std::string mVertPath
Property: 'mVertShader' path to the vertex shader on disk.
Definition: shader.h:215
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ShaderFromFile
Definition: shader.h:202
nap::ComputeShaderFromFile
Definition: shader.h:225
nap::BaseShader::getSamplerDeclarations
const SamplerDeclarations & getSamplerDeclarations() const
Definition: shader.h:38
nap::Shader
Definition: shader.h:92
nap::BaseShader::mSSBODeclarations
BufferObjectDeclarationList mSSBODeclarations
All storage buffer object declarations.
Definition: shader.h:64
nap::ComputeShader::getWorkGroupSize
glm::u32vec3 getWorkGroupSize() const
Definition: shader.h:166
nap::RenderService
Definition: renderservice.h:268
nap::Core
Definition: core.h:82
nap::BaseShader::getDisplayName
const std::string & getDisplayName() const
Definition: shader.h:53
nap::Shader::getVertexModule
VkShaderModule getVertexModule() const
Definition: shader.h:106
nap::Shader::getAttributes
const VertexAttributeDeclarations & getAttributes() const
Definition: shader.h:101
nap::BaseShader::getUBODeclarations
const std::vector< BufferObjectDeclaration > & getUBODeclarations() const
Definition: shader.h:43
nap::BufferObjectDeclarationList
std::vector< nap::BufferObjectDeclaration > BufferObjectDeclarationList
Definition: shadervariabledeclarations.h:184
nap::ComputeShaderFromFile::mComputePath
std::string mComputePath
Property: 'ComputeShader' path to the vertex shader on disk.
Definition: shader.h:238
nap::BaseShader::getSSBODeclarations
const std::vector< BufferObjectDeclaration > & getSSBODeclarations() const
Definition: shader.h:48
nap
Definition: assert.h:10
nap::SamplerDeclarations
std::vector< SamplerDeclaration > SamplerDeclarations
Definition: samplerdeclaration.h:43
nap::BaseShader::getDescriptorSetLayout
VkDescriptorSetLayout getDescriptorSetLayout() const
Definition: shader.h:58
nap::Resource
Definition: resource.h:19
nap::ComputeShader::getWorkGroupSizeConstantIds
const std::vector< int > & getWorkGroupSizeConstantIds() const
Definition: shader.h:175
nap::BaseShader
Definition: shader.h:28
nap::Shader::getFragmentModule
VkShaderModule getFragmentModule() const
Definition: shader.h:111
nap::ComputeShader::getComputeModule
VkShaderModule getComputeModule() const
Definition: shader.h:161