HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
extCompGpuComputation.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 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_EXT_COMP_GPU_COMPUTATION_H
8 #define PXR_IMAGING_HD_ST_EXT_COMP_GPU_COMPUTATION_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
14 
16 
17 #include "pxr/usd/sdf/path.h"
18 #include "pxr/base/tf/token.h"
19 #include "pxr/base/vt/value.h"
20 
21 #include <memory>
22 #include <vector>
23 
25 
26 
27 class HdSceneDelegate;
28 class HdExtComputation;
29 using HdStGLSLProgramSharedPtr= std::shared_ptr<class HdStGLSLProgram>;
31  std::vector<struct HdExtComputationPrimvarDescriptor>;
32 
34  std::shared_ptr<class HdStExtCompGpuComputation>;
35 
36 
37 /// \class HdStExtCompGpuComputation
38 /// A Computation that represents a GPU implementation of a ExtComputation.
39 ///
40 /// The computation implements the basic:
41 /// input HdBufferArrayRange -> processing -> output HdBufferArrayRange
42 /// model of HdStComputations where processing happens in Execute during the
43 /// Execute phase of HdResourceRegistry::Commit.
44 ///
45 /// The computation is performed in three stages by three companion classes:
46 ///
47 /// 1. Input HdBuffersources are committed into the input HdBufferArrayRange
48 /// during the Resolve phase of the HdResourceRegistry::Commit processing.
49 ///
50 /// 2. HdStExtCompGpuComputationResource holds the committed GPU resident
51 /// resources along with the compiled compute shading kernel to execute.
52 /// The values of the HdBufferArrayRanges for the inputs are stored in this
53 /// object. The resource can store heterogenous sources with differing number
54 /// of elements as may be required by computations.
55 ///
56 /// 3. HdStExtCompGpuComputation executes the kernel using the committed GPU
57 /// resident resources and stores the results to the destination
58 /// HdBufferArrayRange given in Execute. The destination HdBufferArrayRange is
59 /// allocated by the owning HdRprim that registers the computation with the
60 /// HdResourceRegistry by calling HdResourceRegistry::AddComputation.
61 ///
62 /// \see HdStExtCompGpuComputationResource
63 /// \see HdRprim
64 /// \see HdStComputation
65 /// \see HdResourceRegistry
66 /// \see HdExtComputation
67 /// \see HdBufferArrayRange
69 {
70 public:
71  /// Constructs a new GPU ExtComputation computation.
72  /// resource provides the set of input data and kernel to execute this
73  /// computation.
74  /// compPrimvars identifies the primvar data being computed
75  ///
76  /// dispatchCount specifies the number of kernel invocations to execute.
77  /// elementCount specifies the number of elements to allocate for output.
79  SdfPath const &id,
81  HdExtComputationPrimvarDescriptorVector const &compPrimvars,
82  int dispatchCount,
83  int elementCount);
84 
85  /// Creates a GPU computation implementing the given abstract computation.
86  /// When created this allocates HdStExtCompGpuComputationResource.
87  /// Nothing is assigned GPU resources unless the source is subsequently
88  /// added to the hdResourceRegistry and the registry is committed.
89  ///
90  /// This delayed allocation allow Rprims to share computed primvar data and
91  /// avoid duplicate allocations GPU resources for computation inputs and
92  /// outputs.
93  ///
94  /// \param[in] sceneDelegate the delegate to pull scene inputs from.
95  /// \param[in] sourceComp the abstract computation in the HdRenderIndex
96  /// this instance actually implements.
97  /// \param[in] compPrimvars identifies the primvar data being computed.
98  /// \see HdExtComputation
99  HDST_API
102  HdSceneDelegate *sceneDelegate,
103  HdExtComputation const *sourceComp,
104  HdExtComputationPrimvarDescriptorVector const &compPrimvars);
105 
106  HDST_API
107  ~HdStExtCompGpuComputation() override;
108 
109  /// Adds the output buffer specs generated by this computation to the
110  /// passed in vector of buffer specs.
111  /// \param[out] specs the vector of HdBufferSpec to add this computation
112  /// output buffer layout requirements to.
113  HDST_API
114  void GetBufferSpecs(HdBufferSpecVector *specs) const override;
115 
116  /// Executes the computation on the GPU.
117  /// Called by HdResourceRegistry::Commit with the HdBufferArrayRange given
118  /// to the HdResourceRegistry when the computation was added to the
119  /// registry.
120  /// \param[inout] range the buffer array range to save the computation
121  /// result to.
122  /// \param[in] resourceRegistry the registry that is current committing
123  /// resources to the GPU.
124  HDST_API
126  HdResourceRegistry *resourceRegistry) override;
127 
128  /// Gets the number of GPU kernel invocations to execute.
129  /// It can be useful for this to be different than the number of output
130  /// elements, e.g. to run a per-curve kernel computing multiple points
131  /// per-curve.
132  HDST_API
133  int GetDispatchCount() const;
134 
135  /// Gets the number of elements in the output primvar.
136  /// The number of elements produced by the computation must be known before
137  /// doing the computation. The allocation of GPU resources needs to know
138  /// the size to allocate before the kernel can run.
139  HDST_API
140  int GetNumOutputElements() const override;
141 
142  /// Gets the shared GPU resource holder for the computation.
143  HDST_API
145 
146 private:
147  SdfPath _id;
150  int _dispatchCount;
151  int _elementCount;
152 
153  HdStExtCompGpuComputation() = delete;
155  const HdStExtCompGpuComputation &) = delete;
156  HdStExtCompGpuComputation &operator = (
157  const HdStExtCompGpuComputation &) = delete;
158 };
159 
160 
161 /// Obtains a set of ExtComputation primvar source computations needed for this
162 /// Rprim.
163 ///
164 /// The list of computed primvar descriptors for an interpolation mode
165 /// is passed in.
166 ///
167 /// The scene delegate also provides information about which output on
168 /// which computation is providing the source of the primvar.
169 ///
170 /// Based on the information, the function creates the necessary
171 /// computations and appends them on to the sources list (the sources vector
172 /// need not be empty).
173 ///
174 /// The caller is expected to pass these computation on these computations
175 /// onto the resource registry (associating them with BARs if it is
176 /// expected the primvar will be downloaded) Additional sources that
177 /// should be associated with BARs but do not otherwise need to be scheduled
178 /// for commit will be returned in reserveOnlySources.
179 ///
180 /// The computation may also need to add sources that are resolved against
181 /// internal BARs that are not to be associated with the primvar BAR. Those
182 /// are returned in the separateComputationSources vector.
183 /// The caller is expected to add them to the resource registry if the
184 /// computation is needed.
185 HDST_API
187  const SdfPath &id,
188  HdSceneDelegate *sceneDelegate,
189  HdExtComputationPrimvarDescriptorVector const& allCompPrimvars,
190  HdDirtyBits dirtyBits,
192  HdBufferSourceSharedPtrVector *reserveOnlySources,
193  HdBufferSourceSharedPtrVector *separateComputationSources,
195 
196 
198 
199 #endif // PXR_IMAGING_HD_ST_EXT_COMP_GPU_COMPUTATION_H
HDST_API HdStExtCompGpuComputationResourceSharedPtr const & GetResource() const
Gets the shared GPU resource holder for the computation.
std::shared_ptr< class HdStExtCompGpuComputationResource > HdStExtCompGpuComputationResourceSharedPtr
GLenum GLint * range
Definition: glcorearb.h:1925
uint32_t HdDirtyBits
Definition: types.h:143
HDST_API void GetBufferSpecs(HdBufferSpecVector *specs) const override
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:27
static HDST_API HdStExtCompGpuComputationSharedPtr CreateGpuComputation(HdSceneDelegate *sceneDelegate, HdExtComputation const *sourceComp, HdExtComputationPrimvarDescriptorVector const &compPrimvars)
GLsizei GLenum * sources
Definition: glcorearb.h:2542
HDST_API ~HdStExtCompGpuComputation() override
HDST_API void Execute(HdBufferArrayRangeSharedPtr const &range, HdResourceRegistry *resourceRegistry) override
Definition: path.h:273
std::vector< struct HdBufferSpec > HdBufferSpecVector
HDST_API int GetNumOutputElements() const override
HDST_API void HdSt_GetExtComputationPrimvarsComputations(const SdfPath &id, HdSceneDelegate *sceneDelegate, HdExtComputationPrimvarDescriptorVector const &allCompPrimvars, HdDirtyBits dirtyBits, HdBufferSourceSharedPtrVector *sources, HdBufferSourceSharedPtrVector *reserveOnlySources, HdBufferSourceSharedPtrVector *separateComputationSources, HdStComputationComputeQueuePairVector *computations)
std::shared_ptr< class HdStGLSLProgram > HdStGLSLProgramSharedPtr
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::vector< std::pair< HdStComputationSharedPtr, HdStComputeQueue >> HdStComputationComputeQueuePairVector
std::shared_ptr< class HdStExtCompGpuComputation > HdStExtCompGpuComputationSharedPtr
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
HDST_API int GetDispatchCount() const
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector