NAP
material.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 // External includes
8 #include <nap/resourceptr.h>
9 #include <utility/dllexport.h>
10 #include <nap/resource.h>
11 
12 // Local includes
13 #include "uniformcontainer.h"
14 #include "materialcommon.h"
15 #include "shader.h"
16 
17 namespace nap
18 {
19  // Forward Declares
20  struct DescriptorSet;
21  class DescriptorSetCache;
22  class Core;
23 
24  // Common material property names
25  namespace material
26  {
27  constexpr const char* shader = "Shader";
28  constexpr const char* uniforms = "Uniforms";
29  constexpr const char* samplers = "Samplers";
30  constexpr const char* buffers = "Buffers";
31  constexpr const char* constants = "Constants";
32  constexpr const char* vbindings = "VertexAttributeBindings";
33  }
34 
41  class BaseMaterial : public Resource, public UniformContainer
42  {
43  RTTI_ENABLE(Resource)
44  public:
48  BaseMaterial(Core& core);
49  virtual ~BaseMaterial() = default;
50 
54  const BaseShader& getShader() const { assert(mShader != nullptr); return *mShader; }
55 
56  std::vector<ResourcePtr<UniformStruct>> mUniforms;
57  std::vector<ResourcePtr<BufferBinding>> mBuffers;
58  std::vector<ResourcePtr<Sampler>> mSamplers;
59  std::vector<ResourcePtr<ShaderConstant>> mConstants;
60 
61  protected:
62  bool rebuild(const BaseShader& shader, utility::ErrorState& errorState);
63 
64  private:
65  using UniformStructMap = std::unordered_map<std::string, std::unique_ptr<UniformStruct>>;
66  RenderService* mRenderService = nullptr;
67  const BaseShader* mShader = nullptr;
68  };
69 
70 
72  // Material
74 
83  class NAPAPI Material : public BaseMaterial
84  {
85  RTTI_ENABLE(BaseMaterial)
86  public:
90  Material(Core& core);
91 
96  {
101  VertexAttributeBinding(const std::string& meshAttributeID, const std::string& shaderAttributeID);
102 
103  // Default constructor
104  VertexAttributeBinding() = default;
105 
106  std::string mMeshAttributeID;
107  std::string mShaderAttributeID;
108  };
109 
116  virtual bool init(utility::ErrorState& errorState) override;
117 
121  const Shader& getShader() const { assert(Material::mShader != nullptr); return *Material::mShader; }
122 
128  EBlendMode getBlendMode() const { assert(mBlendMode != EBlendMode::NotSet); return mBlendMode; }
129 
135  void setBlendMode(EBlendMode blendMode) { mBlendMode = blendMode; }
136 
142  EDepthMode getDepthMode() const { assert(mDepthMode != EDepthMode::NotSet); return mDepthMode; }
143 
149  void setDepthMode(EDepthMode depthMode) { mDepthMode = depthMode; }
150 
155  const VertexAttributeBinding* findVertexAttributeBinding(const std::string& shaderAttributeID) const;
156 
160  static const std::vector<VertexAttributeBinding>& sGetDefaultVertexAttributeBindings();
161 
162  std::vector<VertexAttributeBinding> mVertexAttributeBindings;
163  ResourcePtr<Shader> mShader = nullptr;
166  };
167 
168 
170  // Compute Material
172 
183  class NAPAPI ComputeMaterial : public BaseMaterial
184  {
185  RTTI_ENABLE(BaseMaterial)
186  public:
190  ComputeMaterial(Core& core);
191 
198  virtual bool init(utility::ErrorState& errorState) override;
199 
203  const ComputeShader& getShader() const { assert(ComputeMaterial::mShader != nullptr); return *ComputeMaterial::mShader; }
204 
208  glm::uvec3 getWorkGroupSize() const;
209 
210  ResourcePtr<ComputeShader> mShader = nullptr;
211  };
212 }
nap::ComputeShader
Definition: shader.h:176
nap::Material::getShader
const Shader & getShader() const
Definition: material.h:121
nap::BaseMaterial::BaseMaterial
BaseMaterial(Core &core)
nap::BaseMaterial
Definition: material.h:41
nap::ComputeMaterial
Definition: material.h:183
nap::EBlendMode
EBlendMode
Definition: materialcommon.h:18
nap::BaseMaterial::getShader
const BaseShader & getShader() const
Definition: material.h:54
nap::Material::setDepthMode
void setDepthMode(EDepthMode depthMode)
Definition: material.h:149
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::EDepthMode::InheritFromBlendMode
@ InheritFromBlendMode
Transparent objects do not write depth, but do read depth. Opaque objects read and write depth.
nap::Material::setBlendMode
void setBlendMode(EBlendMode blendMode)
Definition: material.h:135
nap::EDepthMode
EDepthMode
Definition: materialcommon.h:40
nap::ComputeMaterial::mShader
ResourcePtr< ComputeShader > mShader
Property: 'Shader' The compute shader that this material is using.
Definition: material.h:210
nap::Material::mShader
ResourcePtr< Shader > mShader
Property: 'Shader' The shader that this material is using.
Definition: material.h:163
nap::material::shader
constexpr const char * shader
Definition: material.h:27
nap::utility::ErrorState
Definition: errorstate.h:19
nap::EDepthMode::NotSet
@ NotSet
Default value for MaterialInstances, means that the Material's blend is used instead.
nap::material::buffers
constexpr const char * buffers
Definition: material.h:30
nap::Material
Definition: material.h:83
nap::Material::mVertexAttributeBindings
std::vector< VertexAttributeBinding > mVertexAttributeBindings
Property: 'VertexAttributeBindings' Optional, mapping from mesh vertex attr to shader vertex attr.
Definition: material.h:162
nap::EBlendMode::Opaque
@ Opaque
Regular opaque, similar to (One, Zero) blend.
nap::BaseMaterial::~BaseMaterial
virtual ~BaseMaterial()=default
nap::Material::getDepthMode
EDepthMode getDepthMode() const
Definition: material.h:142
nap::Shader
Definition: shader.h:105
nap::Material::VertexAttributeBinding::mMeshAttributeID
std::string mMeshAttributeID
mesh vertex buffer name
Definition: material.h:106
nap::EBlendMode::NotSet
@ NotSet
Default value for MaterialInstances, means that the Material's blend mode is used instead.
nap::RenderService
Definition: renderservice.h:275
nap::BaseMaterial::mConstants
std::vector< ResourcePtr< ShaderConstant > > mConstants
Property: 'Constants' Static constants (as read from file, or as set in code before calling init())
Definition: material.h:59
nap::Core
Definition: core.h:82
nap::material::samplers
constexpr const char * samplers
Definition: material.h:29
nap::BaseMaterial::mUniforms
std::vector< ResourcePtr< UniformStruct > > mUniforms
Property: 'Uniforms' Static uniforms (as read from file, or as set in code before calling init())
Definition: material.h:56
nap::Material::VertexAttributeBinding::mShaderAttributeID
std::string mShaderAttributeID
shader vertex buffer name
Definition: material.h:107
nap::material::uniforms
constexpr const char * uniforms
Definition: material.h:28
nap::BaseMaterial::mBuffers
std::vector< ResourcePtr< BufferBinding > > mBuffers
Property: 'Buffers' Static buffer bindings (as read from file, or as set in code before calling init(...
Definition: material.h:57
nap
Definition: templateapp.h:17
nap::ComputeMaterial::getShader
const ComputeShader & getShader() const
Definition: material.h:203
nap::BaseMaterial::rebuild
bool rebuild(const BaseShader &shader, utility::ErrorState &errorState)
nap::Resource
Definition: resource.h:19
nap::material::constants
constexpr const char * constants
Definition: material.h:31
nap::Material::getBlendMode
EBlendMode getBlendMode() const
Definition: material.h:128
nap::UniformContainer
Definition: uniformcontainer.h:20
nap::BaseShader
Definition: shader.h:30
nap::material::vbindings
constexpr const char * vbindings
Definition: material.h:32
nap::BaseMaterial::mSamplers
std::vector< ResourcePtr< Sampler > > mSamplers
Property: 'Samplers' Static samplers (as read from file, or as set in code before calling init())
Definition: material.h:58
nap::Material::VertexAttributeBinding
Definition: material.h:95