NAP
bufferbindinginstance.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 "bufferbinding.h"
9 #include "gpubuffer.h"
10 #include "structbuffer.h"
11 
12 // External Includes
13 #include <rtti/objectptr.h>
14 #include <glm/glm.hpp>
15 #include <utility/dllexport.h>
16 #include <nap/resource.h>
17 
18 namespace nap
19 {
20  // Forward Declares
21  class BufferBindingInstance;
22 
23  // Called when the bound buffer resource changes
24  using BufferBindingChangedCallback = std::function<void(BufferBindingInstance&)>;
25 
73  class NAPAPI BufferBindingInstance
74  {
75  RTTI_ENABLE()
76  public:
77  // Constructor
78  BufferBindingInstance(const std::string& bindingName, const BufferBindingChangedCallback& bindingChangedCallback) :
79  mBindingName(bindingName),
80  mBindingChangedCallback(bindingChangedCallback)
81  { }
82 
83  // Default Destructor
84  virtual ~BufferBindingInstance() = default;
85 
90  virtual const ShaderVariableDeclaration& getDeclaration() const = 0;
91 
95  bool hasBuffer() const { return mBuffer != nullptr; }
96 
100  const GPUBuffer& getBuffer() const { assert(mBuffer != nullptr); return *mBuffer; }
101 
105  GPUBuffer& getBuffer() { assert(mBuffer != nullptr); return *mBuffer; }
106 
110  const std::string& getBindingName() const { return mBindingName; }
111 
112  protected:
113  const std::string mBindingName;
115  GPUBuffer* mBuffer = nullptr;
116 
117  private:
118  friend class BaseMaterial;
119  friend class BaseMaterialInstance;
120 
129  static std::unique_ptr<BufferBindingInstance> createBufferBindingInstanceFromDeclaration(const BufferObjectDeclaration& declaration, const BufferBinding* binding, BufferBindingChangedCallback bindingChangedCallback, utility::ErrorState& errorState);
130 
141  template<typename INSTANCE_TYPE, typename RESOURCE_TYPE, typename DECLARATION_TYPE>
142  static std::unique_ptr<INSTANCE_TYPE> createBufferBindingInstance(const std::string& bindingName, const DECLARATION_TYPE& declaration, const BufferBinding* binding, BufferBindingChangedCallback bufferChangedCallback, utility::ErrorState& errorState);
143  };
144 
145 
147  // BufferBindingStructInstance
149 
176  {
177  RTTI_ENABLE(BufferBindingInstance)
178  public:
179  // Constructor
180  BufferBindingStructInstance(const std::string& bindingName, const ShaderVariableStructBufferDeclaration& declaration, const BufferBindingChangedCallback& bindingChangedCallback) :
181  BufferBindingInstance(bindingName, bindingChangedCallback),
182  mDeclaration(&declaration)
183  { }
184 
185  // Copy construction and copy assignment not allowed
187  BufferBindingStructInstance& operator=(const BufferBindingStructInstance&) = delete;
188 
193  virtual const ShaderVariableDeclaration& getDeclaration() const override { return *mDeclaration; }
194 
198  const ShaderVariableStructBufferDeclaration& getStructDeclaration() const { return *mDeclaration; }
199 
203  const StructBuffer& getBuffer() const { assert(hasBuffer()); return static_cast<const StructBuffer&>(*mBuffer); };
204 
208  StructBuffer& getBuffer() { assert(hasBuffer()); return static_cast<StructBuffer&>(*mBuffer); };
209 
214  void setBuffer(StructBuffer& buffer);
215 
220  void setBuffer(const BufferBindingStruct& resource);
221 
222  private:
223  friend class BaseMaterial;
224  friend class BaseMaterialInstance;
225 
229  void raiseChanged() { if (mBindingChangedCallback) mBindingChangedCallback(*this); }
230 
231  // To be called when dynamic buffers are updated by upload
232  // This requires the descriptor buffer info in the material instance to be updated
233  nap::Slot<> mBufferChangedSlot = { [&]() -> void { raiseChanged(); } };
234 
235  const ShaderVariableStructBufferDeclaration* mDeclaration;
236  };
237 
238 
240  // BufferBindingNumericInstance
242 
247  {
248  RTTI_ENABLE(BufferBindingInstance)
249  public:
250  // Constructor
251  BufferBindingNumericInstance(const std::string& bindingName, const ShaderVariableValueArrayDeclaration& declaration, const BufferBindingChangedCallback& bindingChangedCallback) :
252  BufferBindingInstance(bindingName, bindingChangedCallback),
253  mDeclaration(&declaration)
254  { }
255 
259  virtual const ShaderVariableDeclaration& getDeclaration() const override { return *mDeclaration; }
260 
264  const ShaderVariableValueArrayDeclaration& getNumericDeclaration() const { return *mDeclaration; }
265 
266  protected:
268  };
269 
270 
275  template<typename T>
277  {
278  RTTI_ENABLE(BufferBindingNumericInstance)
279  public:
280  // Constructor
281  TypedBufferBindingNumericInstance(const std::string& bindingName, const ShaderVariableValueArrayDeclaration& declaration, const BufferBindingChangedCallback& bindingChangedCallback) :
282  BufferBindingNumericInstance(bindingName, declaration, bindingChangedCallback)
283  { }
284 
288  const GPUBufferNumeric& getBuffer() const { assert(hasBuffer()); return static_cast<const GPUBufferNumeric&>(*mBuffer); }
289 
293  GPUBufferNumeric& getBuffer() { assert(hasBuffer()); return static_cast<GPUBufferNumeric&>(*mBuffer); }
294 
299  void setBuffer(TypedGPUBufferNumeric<T>& buffer);
300 
305  void setBuffer(const TypedBufferBindingNumeric<T>& resource);
306 
310  const TypedGPUBufferNumeric<T>& getTypedBuffer() const { assert(mBuffer != nullptr); return static_cast<const TypedGPUBufferNumeric<T>&>(*mBuffer); }
311 
315  TypedGPUBufferNumeric<T>& getTypedBuffer() { assert(mBuffer != nullptr); return static_cast<TypedGPUBufferNumeric<T>&>(*mBuffer); }
316 
317  private:
321  void raiseChanged() { if (mBindingChangedCallback) mBindingChangedCallback(*this); }
322 
323  // To be called when dynamic buffers are updated by upload
324  // This requires the descriptor buffer info in the material instance to be updated
325  nap::Slot<> mBufferChangedSlot = { [&]() -> void { raiseChanged(); } };
326  };
327 
328 
330  // TypedBufferBindingNumericInstance type definitions
332 
341 
342 
344  // Template definitions
346 
347  template<class T>
349  {
350  NAP_ASSERT_MSG(mDeclaration->mStride == buffer.getElementSize(), "Buffer declaration stride is not equal to buffer element size")
351 
352  if (mBuffer != nullptr)
353  mBuffer->bufferChanged.disconnect(mBufferChangedSlot);
354 
355  mBuffer = &buffer;
356  mBuffer->bufferChanged.connect(mBufferChangedSlot);
357  raiseChanged();
358  }
359 
360  template<class T>
362  {
363  NAP_ASSERT_MSG(mDeclaration->mStride == resource.mBuffer.getElementSize(), "Buffer declaration stride is not equal to buffer element size")
364 
365  if (mBuffer != nullptr)
366  mBuffer->bufferChanged.disconnect(mBufferChangedSlot);
367 
368  mBuffer = resource.mBuffer.get();
369  mBuffer->bufferChanged.connect(mBufferChangedSlot);
370  raiseChanged();
371  }
372 }
nap::ShaderVariableDeclaration
Definition: shadervariabledeclarations.h:58
nap::BufferBindingInstance::hasBuffer
bool hasBuffer() const
Definition: bufferbindinginstance.h:95
nap::BufferBindingNumericInstance::getNumericDeclaration
const ShaderVariableValueArrayDeclaration & getNumericDeclaration() const
Definition: bufferbindinginstance.h:264
nap::BaseMaterialInstance
Definition: materialinstance.h:101
nap::BufferBinding
Definition: bufferbinding.h:68
nap::BufferBindingInstance::BufferBindingInstance
BufferBindingInstance(const std::string &bindingName, const BufferBindingChangedCallback &bindingChangedCallback)
Definition: bufferbindinginstance.h:78
nap::GPUBuffer::bufferChanged
nap::Signal bufferChanged
Definition: gpubuffer.h:146
nap::BufferBindingNumericInstance::getDeclaration
virtual const ShaderVariableDeclaration & getDeclaration() const override
Definition: bufferbindinginstance.h:259
nap::BaseMaterial
Definition: material.h:41
nap::TypedBufferBindingNumericInstance::getTypedBuffer
const TypedGPUBufferNumeric< T > & getTypedBuffer() const
Definition: bufferbindinginstance.h:310
nap::BufferBindingNumericInstance
Definition: bufferbindinginstance.h:246
nap::BufferBindingInstance::mBindingChangedCallback
BufferBindingChangedCallback mBindingChangedCallback
Definition: bufferbindinginstance.h:114
nap::GPUBufferNumeric
Definition: gpubuffer.h:222
nap::Slot
Definition: signalslot.h:18
nap::TypedBufferBindingNumericInstance::TypedBufferBindingNumericInstance
TypedBufferBindingNumericInstance(const std::string &bindingName, const ShaderVariableValueArrayDeclaration &declaration, const BufferBindingChangedCallback &bindingChangedCallback)
Definition: bufferbindinginstance.h:281
nap::TypedBufferBindingNumericInstance::setBuffer
void setBuffer(TypedGPUBufferNumeric< T > &buffer)
Definition: bufferbindinginstance.h:348
nap::BufferBindingInstance
Definition: bufferbindinginstance.h:73
nap::ShaderVariableStructBufferDeclaration
Definition: shadervariabledeclarations.h:140
nap::utility::ErrorState
Definition: errorstate.h:19
nap::TypedBufferBindingNumeric::mBuffer
rtti::ObjectPtr< TypedGPUBufferNumeric< T > > mBuffer
Definition: bufferbinding.h:146
nap::BufferBindingInstance::mBuffer
GPUBuffer * mBuffer
Definition: bufferbindinginstance.h:115
nap::BufferBindingInstance::getBuffer
const GPUBuffer & getBuffer() const
Definition: bufferbindinginstance.h:100
nap::BufferBindingStructInstance::getStructDeclaration
const ShaderVariableStructBufferDeclaration & getStructDeclaration() const
Definition: bufferbindinginstance.h:198
nap::BufferBindingChangedCallback
std::function< void(BufferBindingInstance &)> BufferBindingChangedCallback
Definition: bufferbindinginstance.h:24
nap::BufferBindingNumericInstance::BufferBindingNumericInstance
BufferBindingNumericInstance(const std::string &bindingName, const ShaderVariableValueArrayDeclaration &declaration, const BufferBindingChangedCallback &bindingChangedCallback)
Definition: bufferbindinginstance.h:251
nap::BufferBindingStructInstance
Definition: bufferbindinginstance.h:175
nap::TypedBufferBindingNumericInstance::getTypedBuffer
TypedGPUBufferNumeric< T > & getTypedBuffer()
Definition: bufferbindinginstance.h:315
nap::StructBuffer
Definition: structbuffer.h:36
nap::TypedBufferBindingNumeric
Definition: bufferbinding.h:135
nap::GPUBufferNumeric::getElementSize
virtual uint32 getElementSize() const override
Definition: gpubuffer.h:253
nap::BufferBindingInstance::mBindingName
const std::string mBindingName
Definition: bufferbindinginstance.h:113
nap::BufferBindingStructInstance::BufferBindingStructInstance
BufferBindingStructInstance(const std::string &bindingName, const ShaderVariableStructBufferDeclaration &declaration, const BufferBindingChangedCallback &bindingChangedCallback)
Definition: bufferbindinginstance.h:180
nap
Definition: templateapp.h:17
nap::BufferBindingStructInstance::getBuffer
StructBuffer & getBuffer()
Definition: bufferbindinginstance.h:208
nap::BufferBindingInstance::getBuffer
GPUBuffer & getBuffer()
Definition: bufferbindinginstance.h:105
nap::GPUBuffer
Definition: gpubuffer.h:64
nap::TypedGPUBufferNumeric
Definition: gpubuffer.h:308
nap::BufferObjectDeclaration
Definition: shadervariabledeclarations.h:171
nap::Signal::connect
void connect(Slot< Args... > &slot)
Definition: signalslot.h:285
nap::TypedBufferBindingNumericInstance::getBuffer
const GPUBufferNumeric & getBuffer() const
Definition: bufferbindinginstance.h:288
nap::BufferBindingStructInstance::getBuffer
const StructBuffer & getBuffer() const
Definition: bufferbindinginstance.h:203
nap::BufferBindingInstance::getBindingName
const std::string & getBindingName() const
Definition: bufferbindinginstance.h:110
nap::BufferBindingNumericInstance::mDeclaration
const ShaderVariableValueArrayDeclaration * mDeclaration
Definition: bufferbindinginstance.h:267
nap::BufferBindingStruct
Definition: bufferbinding.h:154
nap::TypedBufferBindingNumericInstance
Definition: bufferbindinginstance.h:276
nap::ShaderVariableValueArrayDeclaration
Definition: shadervariabledeclarations.h:155
nap::BufferBindingStructInstance::getDeclaration
virtual const ShaderVariableDeclaration & getDeclaration() const override
Definition: bufferbindinginstance.h:193
nap::TypedBufferBindingNumericInstance::getBuffer
GPUBufferNumeric & getBuffer()
Definition: bufferbindinginstance.h:293