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