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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
25 #define PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/version.h"
31 #include "pxr/imaging/hdSt/api.h"
34 
35 #include <memory>
36 
38 
40 
41 using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
42 
43 /// \class HdStDispatchBuffer
44 ///
45 /// A VBO of a simple array of unsigned integers.
46 ///
47 /// This buffer is used to prepare data on the GPU for indirect dispatch i.e.
48 /// to be consumed by MultiDrawIndirect or DispatchComputeIndirect. At the
49 /// same time, interleaved subsets of the array are bound in several different
50 /// ways to provide additional data interface to shaders.
51 ///
52 /// For each binding, we define 'BufferResourceView' on top of the uint array.
53 /// HdBufferArray aggregates those views and HdResourceBinder binds them
54 /// with specified binding method and interleaved offset.
55 ///
56 /// Example:
57 /// DrawElements + Instance culling : 14 integers for each drawitem
58 ///
59 /// BufferResourceViews BufferResourceViews
60 /// for draw for cull
61 ///
62 /// +----draw item 0----+----------------------------> destination buffer
63 /// | count | --+
64 /// | instanceCount | |
65 /// | first | |----> MDI dispatch
66 /// | baseVertex | |
67 /// | baseInstance | --+-------------------------> drawitem index
68 /// | cullCount | ----+
69 /// | cullInstanceCount | |------------------------> MDI dispatch
70 /// | cullFirstVertex | |
71 /// | cullBaseInstance | ----+
72 /// | modelDC | --+
73 /// | constantDC | |----> DrawingCoord0 -------> DrawingCoord0
74 /// | elementDC | |
75 /// | primitiveDC | --+
76 /// | fvarDC | --+
77 /// | instanceIndexDC | |----> DrawingCoord1 -------> DrawingCoord1
78 /// | shaderDC | --+
79 /// | (instanceDC[0]) | --+
80 /// | (instanceDC[1]) | |----> DrawingCoordI -------> DrawingCoordI
81 /// | (instanceDC[2]) | |
82 /// | ... | --+
83 /// +----draw item 1----+
84 /// | count |
85 /// | instanceCount |
86 /// | ... |
87 ///
88 /// XXX: it would be better to generalize this class not only for dispatch
89 /// buffer, if we see other similar use-cases.
90 ///
92 {
93 public:
94  /// Constructor. commandNumUints is given in how many integers.
95  HDST_API
96  HdStDispatchBuffer(HdStResourceRegistry* resourceRegistry,
97  TfToken const &role,
98  int count,
99  unsigned int commandNumUints);
100 
101  /// Destructor.
102  HDST_API
103  ~HdStDispatchBuffer() override;
104 
105  /// Update entire buffer data
106  HDST_API
107  void CopyData(std::vector<uint32_t> const &data);
108 
109  /// Add an interleaved view to this buffer.
110  HDST_API
111  void AddBufferResourceView(TfToken const &name,
112  HdTupleType tupleType, int offset);
113 
114  /// Returns the dispatch count
115  int GetCount() const { return _count; }
116 
117  /// Returns the number of uints in a single draw command.
118  unsigned int GetCommandNumUints() const { return _commandNumUints; }
119 
120  /// Returns a bar which locates all interleaved resources of the entire
121  /// buffer.
123  return _bar;
124  }
125 
126  /// Returns entire buffer as a single HdStBufferResource.
128  return _entireResource;
129  }
130 
131  // HdBufferArray overrides. they are not supported in this class.
132  HDST_API
133  bool GarbageCollect() override;
134  HDST_API
135  void Reallocate(
136  std::vector<HdBufferArrayRangeSharedPtr> const &,
137  HdBufferArraySharedPtr const &) override;
138 
139  HDST_API
140  void DebugDump(std::ostream &out) const override;
141 
142  /// Returns the GPU resource. If the buffer array contains more than one
143  /// resource, this method raises a coding error.
144  HDST_API
146 
147  /// Returns the named GPU resource. This method returns the first found
148  /// resource. In HDST_SAFE_MODE it checks all underlying GPU buffers
149  /// in _resourceMap and raises a coding error if there are more than
150  /// one GPU buffers exist.
151  HDST_API
153 
154  /// Returns the list of all named GPU resources for this bufferArray.
155  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
156 
157 protected:
158  /// Adds a new, named GPU resource and returns it.
159  HDST_API
161  HdTupleType tupleType,
162  int offset,
163  int stride);
164 
165 private:
166  HdStResourceRegistry *_resourceRegistry;
167  int _count;
168  unsigned int _commandNumUints;
169  HdStBufferResourceNamedList _resourceList;
170  HdStBufferResourceSharedPtr _entireResource;
171  HdStBufferArrayRangeSharedPtr _bar; // Alternative to range list in base class
172 };
173 
174 
176 
177 #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:43
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:87
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:1441
#define HDST_API
Definition: api.h:40
int GetCount() const
Returns the dispatch count.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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:895