unitTestHelper.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
8 #define PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
9 
11 
12 #include "pxr/pxr.h"
14 
15 #include "pxr/imaging/hd/changeTracker.h"
16 #include "pxr/imaging/hd/engine.h"
17 #include "pxr/imaging/hd/renderIndex.h"
18 #include "pxr/imaging/hd/renderPass.h"
19 #include "pxr/imaging/hd/rprim.h"
20 #include "pxr/imaging/hd/rprimCollection.h"
21 #include "pxr/imaging/hd/tokens.h"
22 #include "pxr/imaging/hd/unitTestNullRenderDelegate.h"
23 #include "pxr/imaging/hd/unitTestNullRenderPass.h"
24 
25 #include <memory>
26 #include <string>
27 
28 PXR_NAMESPACE_OPEN_SCOPE
29 
30 using HdRenderPassSharedPtr = std::shared_ptr<HdRenderPass>;
31 
33 class UsdImaging_TestTask final : public HdTask
34 {
35 public:
36  UsdImaging_TestTask(HdRenderPassSharedPtr const &renderPass,
37  TfTokenVector const &renderTags)
38  : HdTask(SdfPath::EmptyPath())
39  , _renderPass(renderPass)
40  , _renderTags(renderTags)
41  {
42  }
43 
44  virtual void Sync(HdSceneDelegate* delegate,
45  HdTaskContext* ctx,
46  HdDirtyBits* dirtyBits) override {
47  _renderPass->Sync();
48 
49  *dirtyBits = HdChangeTracker::Clean;
50  }
51 
52  virtual void Prepare(HdTaskContext* ctx,
53  HdRenderIndex* renderIndex) override {
54  }
55 
56  virtual void Execute(HdTaskContext* ctx) override {
57  }
58 
59  virtual const TfTokenVector &GetRenderTags() const override {
60  return _renderTags;
61  }
62 
63 private:
64  HdRenderPassSharedPtr _renderPass;
65  TfTokenVector _renderTags;
66 };
67 
76 class UsdImaging_TestDriver final {
77 public:
78  UsdImaging_TestDriver(std::string const& usdFilePath)
79  : _engine()
80  , _renderDelegate()
81  , _renderIndex(nullptr)
82  , _delegate(nullptr)
83  , _geometryPass()
84  , _stage()
85  {
87  HdTokens->geometry,
88  HdReprSelector(HdReprTokens->hull));
89 
90  TfTokenVector renderTags;
91  renderTags.push_back(HdRenderTagTokens->geometry);
92 
93  _Init(UsdStage::Open(usdFilePath),
94  collection,
96  renderTags);
97  }
98 
99  UsdImaging_TestDriver(std::string const& usdFilePath,
100  TfToken const &collectionName,
101  TfToken const &reprName,
102  TfTokenVector const &renderTags)
103  : _engine()
104  , _renderDelegate()
105  , _renderIndex(nullptr)
106  , _delegate(nullptr)
107  , _geometryPass()
108  , _stage()
109  {
111  collectionName,
112  HdReprSelector(reprName));
113 
114  _Init(UsdStage::Open(usdFilePath),
115  collection,
117  renderTags);
118  }
119 
120  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage)
121  : _engine()
122  , _renderDelegate()
123  , _renderIndex(nullptr)
124  , _delegate(nullptr)
125  , _geometryPass()
126  , _stage()
127  {
129  HdTokens->geometry,
130  HdReprSelector(HdReprTokens->hull));
131 
132  TfTokenVector renderTags;
133  renderTags.push_back(HdRenderTagTokens->geometry);
134 
135  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
136  }
137 
138  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
139  TfToken const &collectionName,
140  TfToken const &reprName,
141  TfTokenVector const &renderTags)
142  : _engine()
143  , _renderDelegate()
144  , _renderIndex(nullptr)
145  , _delegate(nullptr)
146  , _geometryPass()
147  , _stage()
148  {
150  collectionName,
151  HdReprSelector(reprName));
152 
153  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
154  }
155 
156  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
157  HdRprimCollection const &collection,
158  SdfPath const &delegateId,
159  TfTokenVector const &renderTags)
160  : _engine()
161  , _renderDelegate()
162  , _renderIndex(nullptr)
163  , _delegate(nullptr)
164  , _geometryPass()
165  , _stage()
166  {
167  _Init(usdStage, collection, delegateId, renderTags);
168  }
169 
170  ~UsdImaging_TestDriver()
171  {
172  delete _delegate;
173  delete _renderIndex;
174  }
175 
176  void Draw() {
177  HdTaskSharedPtrVector tasks = {
178  std::make_shared<UsdImaging_TestTask>(_geometryPass, _renderTags)
179  };
180  _engine.Execute(&_delegate->GetRenderIndex(), &tasks);
181  }
182  void SetTime(double time) {
183  _delegate->SetTime(time);
184  }
185 
187  void MarkRprimDirty(SdfPath path, HdDirtyBits flag) {
188  _delegate->GetRenderIndex().GetChangeTracker()
189  .MarkRprimDirty(path, flag);
190  }
191 
193  UsdImagingDelegate& GetDelegate() {
194  return *_delegate;
195  }
196 
198  UsdStageRefPtr const& GetStage() {
199  return _stage;
200  }
201 
202 private:
203  HdEngine _engine;
204  Hd_UnitTestNullRenderDelegate _renderDelegate;
205  HdRenderIndex *_renderIndex;
206  UsdImagingDelegate *_delegate;
207  HdRenderPassSharedPtr _geometryPass;
208  UsdStageRefPtr _stage;
209  TfTokenVector _renderTags;
210 
211  void _Init(UsdStageRefPtr const& usdStage,
212  HdRprimCollection const &collection,
213  SdfPath const &delegateId,
214  TfTokenVector const &renderTags) {
215  _renderIndex = HdRenderIndex::New(&_renderDelegate, HdDriverVector());
216  TF_VERIFY(_renderIndex != nullptr);
217  _delegate = new UsdImagingDelegate(_renderIndex, delegateId);
218 
219  _stage = usdStage;
220  _delegate->Populate(_stage->GetPseudoRoot());
221 
222  _geometryPass = HdRenderPassSharedPtr(
223  new Hd_UnitTestNullRenderPass(_renderIndex, collection));
224 
225  _renderTags = renderTags;
226  }
227 };
228 
229 PXR_NAMESPACE_CLOSE_SCOPE
230 
231 #endif //PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
The render index is part of the Hydra 1.0 API and is only used for emulation purposes so that HdScene...
Definition: renderIndex.h:110
virtual void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits)=0
Sync Phase: Obtain task state from Scene delegate based on change processing.
virtual HD_API const TfTokenVector & GetRenderTags() const
Render Tag Gather.
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers, const std::string &instanceName=std::string(), const std::string &appName=std::string())
"instanceName" is an optional identifier useful for applications to associate this render index with ...
virtual void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex)=0
Prepare Phase: Resolve bindings and manage resources.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
virtual void Execute(HdTaskContext *ctx)=0
Execute Phase: Runs the task.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:267
The application-facing entry point top-level entry point for accessing Hydra.
Definition: engine.h:31
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:489
A named, semantic collection of objects.
HdTask represents a unit of work to perform during a Hydra render.
Definition: task.h:43
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
USDIMAGING_API void Populate(UsdPrim const &rootPrim)
Populates the rootPrim in the HdRenderIndex.
Describes one or more authored display representations for an rprim.
Definition: repr.h:31
static USD_API UsdStageRefPtr Open(const std::string &filePath, InitialLoadSet load=LoadAll)
Attempt to find a matching existing stage in a cache if UsdStageCacheContext objects exist on the sta...
The primary translation layer between the Hydra (Hd) core and the Usd scene graph.
Definition: delegate.h:67