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"
31 
33 
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/base/tf/token.h"
36 #include "pxr/base/vt/value.h"
37 
38 #include <memory>
39 #include <vector>
40 
42 
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 HdStComputations 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 HdStComputation
82 /// \see HdResourceRegistry
83 /// \see HdExtComputation
84 /// \see HdBufferArrayRange
86 {
87 public:
88  /// Constructs a new GPU ExtComputation computation.
89  /// resource provides the set of input data and kernel to execute this
90  /// computation.
91  /// compPrimvars identifies the primvar data being computed
92  ///
93  /// dispatchCount specifies the number of kernel invocations to execute.
94  /// elementCount specifies the number of elements to allocate for output.
96  SdfPath const &id,
98  HdExtComputationPrimvarDescriptorVector const &compPrimvars,
99  int dispatchCount,
100  int elementCount);
101 
102  /// Creates a GPU computation implementing the given abstract computation.
103  /// When created this allocates HdStExtCompGpuComputationResource.
104  /// Nothing is assigned GPU resources unless the source is subsequently
105  /// added to the hdResourceRegistry and the registry is committed.
106  ///
107  /// This delayed allocation allow Rprims to share computed primvar data and
108  /// avoid duplicate allocations GPU resources for computation inputs and
109  /// outputs.
110  ///
111  /// \param[in] sceneDelegate the delegate to pull scene inputs from.
112  /// \param[in] sourceComp the abstract computation in the HdRenderIndex
113  /// this instance actually implements.
114  /// \param[in] compPrimvars identifies the primvar data being computed.
115  /// \see HdExtComputation
116  HDST_API
119  HdSceneDelegate *sceneDelegate,
120  HdExtComputation const *sourceComp,
121  HdExtComputationPrimvarDescriptorVector const &compPrimvars);
122 
123  HDST_API
124  ~HdStExtCompGpuComputation() override;
125 
126  /// Adds the output buffer specs generated by this computation to the
127  /// passed in vector of buffer specs.
128  /// \param[out] specs the vector of HdBufferSpec to add this computation
129  /// output buffer layout requirements to.
130  HDST_API
131  void GetBufferSpecs(HdBufferSpecVector *specs) const override;
132 
133  /// Executes the computation on the GPU.
134  /// Called by HdResourceRegistry::Commit with the HdBufferArrayRange given
135  /// to the HdResourceRegistry when the computation was added to the
136  /// registry.
137  /// \param[inout] range the buffer array range to save the computation
138  /// result to.
139  /// \param[in] resourceRegistry the registry that is current committing
140  /// resources to the GPU.
141  HDST_API
143  HdResourceRegistry *resourceRegistry) override;
144 
145  /// Gets the number of GPU kernel invocations to execute.
146  /// It can be useful for this to be different than the number of output
147  /// elements, e.g. to run a per-curve kernel computing multiple points
148  /// per-curve.
149  HDST_API
150  int GetDispatchCount() const;
151 
152  /// Gets the number of elements in the output primvar.
153  /// The number of elements produced by the computation must be known before
154  /// doing the computation. The allocation of GPU resources needs to know
155  /// the size to allocate before the kernel can run.
156  HDST_API
157  int GetNumOutputElements() const override;
158 
159  /// Gets the shared GPU resource holder for the computation.
160  HDST_API
162 
163 private:
164  SdfPath _id;
167  int _dispatchCount;
168  int _elementCount;
169 
170  HdStExtCompGpuComputation() = delete;
172  const HdStExtCompGpuComputation &) = delete;
173  HdStExtCompGpuComputation &operator = (
174  const HdStExtCompGpuComputation &) = delete;
175 };
176 
177 
178 /// Obtains a set of ExtComputation primvar source computations needed for this
179 /// Rprim.
180 ///
181 /// The list of computed primvar descriptors for an interpolation mode
182 /// is passed in.
183 ///
184 /// The scene delegate also provides information about which output on
185 /// which computation is providing the source of the primvar.
186 ///
187 /// Based on the information, the function creates the necessary
188 /// computations and appends them on to the sources list (the sources vector
189 /// need not be empty).
190 ///
191 /// The caller is expected to pass these computation on these computations
192 /// onto the resource registry (associating them with BARs if it is
193 /// expected the primvar will be downloaded) Additional sources that
194 /// should be associated with BARs but do not otherwise need to be scheduled
195 /// for commit will be returned in reserveOnlySources.
196 ///
197 /// The computation may also need to add sources that are resolved against
198 /// internal BARs that are not to be associated with the primvar BAR. Those
199 /// are returned in the separateComputationSources vector.
200 /// The caller is expected to add them to the resource registry if the
201 /// computation is needed.
202 HDST_API
204  const SdfPath &id,
205  HdSceneDelegate *sceneDelegate,
206  HdExtComputationPrimvarDescriptorVector const& allCompPrimvars,
207  HdDirtyBits dirtyBits,
209  HdBufferSourceSharedPtrVector *reserveOnlySources,
210  HdBufferSourceSharedPtrVector *separateComputationSources,
212 
213 
215 
216 #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:158
HDST_API void GetBufferSpecs(HdBufferSpecVector *specs) const override
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:2542
HDST_API ~HdStExtCompGpuComputation() override
HDST_API void Execute(HdBufferArrayRangeSharedPtr const &range, HdResourceRegistry *resourceRegistry) override
Definition: path.h:290
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:1432
std::vector< std::pair< HdStComputationSharedPtr, HdStComputeQueue >> HdStComputationComputeQueuePairVector
std::shared_ptr< class HdStExtCompGpuComputation > HdStExtCompGpuComputationSharedPtr
#define HDST_API
Definition: api.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:44
HDST_API int GetDispatchCount() const
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector