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 
30  class BaseMaterial : public Resource, public UniformContainer
31  {
32  RTTI_ENABLE(Resource)
33  public:
37  BaseMaterial(Core& core);
38  virtual ~BaseMaterial() = default;
39 
43  const BaseShader& getShader() { assert(mShader != nullptr); return *mShader; }
44 
45  std::vector<ResourcePtr<UniformStruct>> mUniforms;
46  std::vector<ResourcePtr<BufferBinding>> mBuffers;
47  std::vector<ResourcePtr<Sampler>> mSamplers;
48 
49  protected:
50  bool rebuild(const BaseShader& shader, utility::ErrorState& errorState);
51 
52  private:
53  using UniformStructMap = std::unordered_map<std::string, std::unique_ptr<UniformStruct>>;
54  using UniformStructArrayMap = std::unordered_map<std::string, std::unique_ptr<UniformStructArray>>;
55  RenderService* mRenderService = nullptr;
56  const BaseShader* mShader = nullptr;
57  };
58 
59 
61  // Material
63 
72  class NAPAPI Material : public BaseMaterial
73  {
74  RTTI_ENABLE(BaseMaterial)
75  public:
79  Material(Core& core);
80 
85  {
90  VertexAttributeBinding(const std::string& meshAttributeID, const std::string& shaderAttributeID);
91 
92  // Default constructor
93  VertexAttributeBinding() = default;
94 
95  std::string mMeshAttributeID;
96  std::string mShaderAttributeID;
97  };
98 
105  virtual bool init(utility::ErrorState& errorState) override;
106 
110  const Shader& getShader() const { assert(Material::mShader != nullptr); return *Material::mShader; }
111 
117  EBlendMode getBlendMode() const { assert(mBlendMode != EBlendMode::NotSet); return mBlendMode; }
118 
124  void setBlendMode(EBlendMode blendMode) { mBlendMode = blendMode; }
125 
131  EDepthMode getDepthMode() const { assert(mDepthMode != EDepthMode::NotSet); return mDepthMode; }
132 
138  void setDepthMode(EDepthMode depthMode) { mDepthMode = depthMode; }
139 
144  const VertexAttributeBinding* findVertexAttributeBinding(const std::string& shaderAttributeID) const;
145 
149  static const std::vector<VertexAttributeBinding>& sGetDefaultVertexAttributeBindings();
150 
151  std::vector<VertexAttributeBinding> mVertexAttributeBindings;
152  ResourcePtr<Shader> mShader = nullptr;
155  };
156 
157 
159  // Compute Material
161 
172  class NAPAPI ComputeMaterial : public BaseMaterial
173  {
174  RTTI_ENABLE(BaseMaterial)
175  public:
179  ComputeMaterial(Core& core);
180 
187  virtual bool init(utility::ErrorState& errorState) override;
188 
192  const ComputeShader& getShader() const { assert(ComputeMaterial::mShader != nullptr); return *ComputeMaterial::mShader; }
193 
194  ResourcePtr<ComputeShader> mShader = nullptr;
195  };
196 }
nap::ComputeShader
Definition: shader.h:151
nap::Material::getShader
const Shader & getShader() const
Definition: material.h:110
nap::BaseMaterial::BaseMaterial
BaseMaterial(Core &core)
nap::BaseMaterial
Definition: material.h:30
nap::ComputeMaterial
Definition: material.h:172
nap::EBlendMode
EBlendMode
Definition: materialcommon.h:18
nap::Material::setDepthMode
void setDepthMode(EDepthMode depthMode)
Definition: material.h:138
nap::rtti::ObjectPtr
Definition: objectptr.h:184
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:124
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:194
nap::Material::mShader
ResourcePtr< Shader > mShader
Property: 'Shader' The shader that this material is using.
Definition: material.h:152
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
Definition: material.h:72
nap::Material::mVertexAttributeBindings
std::vector< VertexAttributeBinding > mVertexAttributeBindings
Property: 'VertexAttributeBindings' Optional, mapping from mesh vertex attr to shader vertex attr.
Definition: material.h:151
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:131
nap::Shader
Definition: shader.h:92
nap::Material::VertexAttributeBinding::mMeshAttributeID
std::string mMeshAttributeID
mesh vertex buffer name
Definition: material.h:95
nap::EBlendMode::NotSet
@ NotSet
Default value for MaterialInstances, means that the Material's blend mode is used instead.
nap::RenderService
Definition: renderservice.h:268
nap::Core
Definition: core.h:82
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:45
nap::Material::VertexAttributeBinding::mShaderAttributeID
std::string mShaderAttributeID
shader vertex buffer name
Definition: material.h:96
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:46
nap
Definition: assert.h:10
nap::ComputeMaterial::getShader
const ComputeShader & getShader() const
Definition: material.h:192
nap::BaseMaterial::rebuild
bool rebuild(const BaseShader &shader, utility::ErrorState &errorState)
nap::Resource
Definition: resource.h:19
nap::Material::getBlendMode
EBlendMode getBlendMode() const
Definition: material.h:117
nap::UniformContainer
Definition: uniformcontainer.h:19
nap::BaseShader
Definition: shader.h:28
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:47
nap::Material::VertexAttributeBinding
Definition: material.h:84
nap::BaseMaterial::getShader
const BaseShader & getShader()
Definition: material.h:43