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 
39 class Hgi;
40 
41 using HdStDispatchBufferSharedPtr = std::shared_ptr<class HdStDispatchBuffer>;
42 
43 /// \class HdStDispatchBuffer
44 ///
45 /// A VBO of a simple array of GLuint.
46 ///
47 /// This buffer is used to prepare data on the GPU for indirect dispatch i.e.
48 /// to be consumed by glMultiDrawIndirect or glDispatchComputeIndirect. 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 GLuint 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 public:
93  /// Constructor. commandNumUints is given in how many integers.
94  HDST_API
95  HdStDispatchBuffer(Hgi* hgi, TfToken const &role, int count,
96  unsigned int commandNumUints);
97 
98  /// Destructor.
99  HDST_API
101 
102  /// Update entire buffer data
103  HDST_API
104  void CopyData(std::vector<GLuint> const &data);
105 
106  /// Add an interleaved view to this buffer.
107  HDST_API
108  void AddBufferResourceView(TfToken const &name,
109  HdTupleType tupleType, int offset);
110 
111  /// Returns the dispatch count
112  int GetCount() const { return _count; }
113 
114  /// Returns the number of GLuints in a single draw command.
115  unsigned int GetCommandNumUints() const { return _commandNumUints; }
116 
117  /// Returns a bar which locates all interleaved resources of the entire
118  /// buffer.
120  return _bar;
121  }
122 
123  /// Returns entire buffer as a single HdBufferResource.
125  return _entireResource;
126  }
127 
128  // HdBufferArray overrides. they are not supported in this class.
129  HDST_API
130  virtual bool GarbageCollect();
131  HDST_API
132  virtual void Reallocate(
133  std::vector<HdBufferArrayRangeSharedPtr> const &,
134  HdBufferArraySharedPtr const &);
135 
136  HDST_API
137  virtual void DebugDump(std::ostream &out) const;
138 
139  /// Returns the GPU resource. If the buffer array contains more than one
140  /// resource, this method raises a coding error.
141  HDST_API
143 
144  /// Returns the named GPU resource. This method returns the first found
145  /// resource. In HDST_SAFE_MODE it checks all underlying GL buffers
146  /// in _resourceMap and raises a coding error if there are more than
147  /// one GL buffers exist.
148  HDST_API
150 
151  /// Returns the list of all named GPU resources for this bufferArray.
152  HdStBufferResourceGLNamedList const& GetResources() const {return _resourceList;}
153 
154 protected:
155  /// Adds a new, named GPU resource and returns it.
156  HDST_API
158  HdTupleType tupleType,
159  int offset,
160  int stride);
161 
162 private:
163  class Hgi *_hgi;
164  int _count;
165  unsigned int _commandNumUints;
166  HdStBufferResourceGLNamedList _resourceList;
167  HdStBufferResourceGLSharedPtr _entireResource;
168  HdStBufferArrayRangeGLSharedPtr _bar; // Alternative to range list in base class
169 };
170 
171 
173 
174 #endif // PXR_IMAGING_HD_ST_DISPATCH_BUFFER_H
std::shared_ptr< class HdStDispatchBuffer > HdStDispatchBufferSharedPtr
HDST_API HdStBufferResourceGLSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API HdStDispatchBuffer(Hgi *hgi, TfToken const &role, int count, unsigned int commandNumUints)
Constructor. commandNumUints is given in how many integers.
GLuint const GLchar * name
Definition: glew.h:1814
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:44
std::shared_ptr< class HdStBufferResourceGL > HdStBufferResourceGLSharedPtr
HDST_API HdStBufferResourceGLSharedPtr GetResource() const
std::vector< std::pair< TfToken, HdStBufferResourceGLSharedPtr > > HdStBufferResourceGLNamedList
virtual HDST_API bool GarbageCollect()
Performs compaction if necessary and returns true if it becomes empty.
Definition: token.h:87
HdStBufferResourceGLSharedPtr GetEntireResource() const
Returns entire buffer as a single HdBufferResource.
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &, HdBufferArraySharedPtr const &)
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
HDST_API void AddBufferResourceView(TfToken const &name, HdTupleType tupleType, int offset)
Add an interleaved view to this buffer.
std::shared_ptr< class HdStBufferArrayRangeGL > HdStBufferArrayRangeGLSharedPtr
HDST_API ~HdStDispatchBuffer()
Destructor.
GLsizei stride
Definition: glew.h:1523
Definition: hgi.h:106
HdStBufferArrayRangeGLSharedPtr GetBufferArrayRange() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
#define HDST_API
Definition: api.h:40
int GetCount() const
Returns the dispatch count.
GLuint GLuint GLsizei count
Definition: glew.h:1253
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HDST_API void CopyData(std::vector< GLuint > const &data)
Update entire buffer data.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
unsigned int GetCommandNumUints() const
Returns the number of GLuints in a single draw command.
GLintptr offset
Definition: glew.h:1682