HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dispatchBuffer.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
8 #define PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/version.h"
14 #include "pxr/imaging/hdSt/api.h"
17 
18 #include <memory>
19 
21 
23 
24 using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
25 
26 /// \class HdStDispatchBuffer
27 ///
28 /// A VBO of a simple array of unsigned integers.
29 ///
30 /// This buffer is used to prepare data on the GPU for indirect dispatch i.e.
31 /// to be consumed by MultiDrawIndirect or DispatchComputeIndirect. At the
32 /// same time, interleaved subsets of the array are bound in several different
33 /// ways to provide additional data interface to shaders.
34 ///
35 /// For each binding, we define 'BufferResourceView' on top of the uint array.
36 /// HdBufferArray aggregates those views and HdResourceBinder binds them
37 /// with specified binding method and interleaved offset.
38 ///
39 /// Example:
40 /// DrawElements + Instance culling : 14 integers for each drawitem
41 ///
42 /// BufferResourceViews BufferResourceViews
43 /// for draw for cull
44 ///
45 /// +----draw item 0----+----------------------------> destination buffer
46 /// | count | --+
47 /// | instanceCount | |
48 /// | first | |----> MDI dispatch
49 /// | baseVertex | |
50 /// | baseInstance | --+-------------------------> drawitem index
51 /// | cullCount | ----+
52 /// | cullInstanceCount | |------------------------> MDI dispatch
53 /// | cullFirstVertex | |
54 /// | cullBaseInstance | ----+
55 /// | modelDC | --+
56 /// | constantDC | |----> DrawingCoord0 -------> DrawingCoord0
57 /// | elementDC | |
58 /// | primitiveDC | --+
59 /// | fvarDC | --+
60 /// | instanceIndexDC | |----> DrawingCoord1 -------> DrawingCoord1
61 /// | shaderDC | --+
62 /// | (instanceDC[0]) | --+
63 /// | (instanceDC[1]) | |----> DrawingCoordI -------> DrawingCoordI
64 /// | (instanceDC[2]) | |
65 /// | ... | --+
66 /// +----draw item 1----+
67 /// | count |
68 /// | instanceCount |
69 /// | ... |
70 ///
71 /// XXX: it would be better to generalize this class not only for dispatch
72 /// buffer, if we see other similar use-cases.
73 ///
75 {
76 public:
77  /// Constructor. commandNumUints is given in how many integers.
78  HDST_API
79  HdStDispatchBuffer(HdStResourceRegistry* resourceRegistry,
80  TfToken const &role,
81  int count,
82  unsigned int commandNumUints);
83 
84  /// Destructor.
85  HDST_API
86  ~HdStDispatchBuffer() override;
87 
88  /// Update entire buffer data
89  HDST_API
90  void CopyData(std::vector<uint32_t> const &data);
91 
92  /// Add an interleaved view to this buffer.
93  HDST_API
95  HdTupleType tupleType, int offset);
96 
97  /// Returns the dispatch count
98  int GetCount() const { return _count; }
99 
100  /// Returns the number of uints in a single draw command.
101  unsigned int GetCommandNumUints() const { return _commandNumUints; }
102 
103  /// Returns a bar which locates all interleaved resources of the entire
104  /// buffer.
106  return _bar;
107  }
108 
109  /// Returns entire buffer as a single HdStBufferResource.
111  return _entireResource;
112  }
113 
114  // HdBufferArray overrides. they are not supported in this class.
115  HDST_API
116  bool GarbageCollect() override;
117  HDST_API
118  void Reallocate(
119  std::vector<HdBufferArrayRangeSharedPtr> const &,
120  HdBufferArraySharedPtr const &) override;
121 
122  HDST_API
123  void DebugDump(std::ostream &out) const override;
124 
125  /// Returns the GPU resource. If the buffer array contains more than one
126  /// resource, this method raises a coding error.
127  HDST_API
129 
130  /// Returns the named GPU resource. This method returns the first found
131  /// resource. In HDST_SAFE_MODE it checks all underlying GPU buffers
132  /// in _resourceMap and raises a coding error if there are more than
133  /// one GPU buffers exist.
134  HDST_API
136 
137  /// Returns the list of all named GPU resources for this bufferArray.
138  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
139 
140 protected:
141  /// Adds a new, named GPU resource and returns it.
142  HDST_API
144  HdTupleType tupleType,
145  int offset,
146  int stride);
147 
148 private:
149  HdStResourceRegistry *_resourceRegistry;
150  int _count;
151  unsigned int _commandNumUints;
152  HdStBufferResourceNamedList _resourceList;
153  HdStBufferResourceSharedPtr _entireResource;
154  HdStBufferArrayRangeSharedPtr _bar; // Alternative to range list in base class
155 };
156 
157 
159 
160 #endif // PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
HDST_API void CopyData(std::vector< uint32_t > const &data)
Update entire buffer data.
std::shared_ptr< class HdStBufferArrayRange > HdStBufferArrayRangeSharedPtr
std::shared_ptr< class HdStDispatchBuffer > HdStDispatchBufferSharedPtr
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
HDST_API ~HdStDispatchBuffer() override
Destructor.
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
HDST_API bool GarbageCollect() override
Performs compaction if necessary and returns true if it becomes empty.
HdStBufferResourceSharedPtr GetEntireResource() const
Returns entire buffer as a single HdStBufferResource.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &, HdBufferArraySharedPtr const &) override
Definition: token.h:70
GLintptr offset
Definition: glcorearb.h:665
HdStBufferArrayRangeSharedPtr GetBufferArrayRange() const
HDST_API HdStBufferResourceSharedPtr GetResource() const
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
HDST_API void AddBufferResourceView(TfToken const &name, HdTupleType tupleType, int offset)
Add an interleaved view to this buffer.
GLuint const GLchar * name
Definition: glcorearb.h:786
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define HDST_API
Definition: api.h:23
int GetCount() const
Returns the dispatch count.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
unsigned int GetCommandNumUints() const
Returns the number of uints in a single draw command.
HDST_API HdStDispatchBuffer(HdStResourceRegistry *resourceRegistry, TfToken const &role, int count, unsigned int commandNumUints)
Constructor. commandNumUints is given in how many integers.
GLint GLsizei count
Definition: glcorearb.h:405
Definition: format.h:1821