All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unitTestHelper.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 USDIMAGING_UNIT_TEST_HELPER
25 #define USDIMAGING_UNIT_TEST_HELPER
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usdImaging/usdImaging/delegate.h"
31 
32 #include "pxr/imaging/hd/changeTracker.h"
33 #include "pxr/imaging/hd/engine.h"
34 #include "pxr/imaging/hd/renderIndex.h"
35 #include "pxr/imaging/hd/renderPass.h"
36 #include "pxr/imaging/hd/rprim.h"
37 #include "pxr/imaging/hd/rprimCollection.h"
38 #include "pxr/imaging/hd/tokens.h"
39 #include "pxr/imaging/hd/unitTestNullRenderDelegate.h"
40 #include "pxr/imaging/hd/unitTestNullRenderPass.h"
41 
42 #include <string>
43 #include <boost/shared_ptr.hpp>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
47 
48 typedef boost::shared_ptr<HdRenderPass> HdRenderPassSharedPtr;
49 
51 class UsdImaging_TestTask final : public HdTask
52 {
53 public:
54  UsdImaging_TestTask(HdRenderPassSharedPtr const &renderPass,
55  TfTokenVector const &renderTags)
56  : HdTask(SdfPath::EmptyPath())
57  , _renderPass(renderPass)
58  , _renderTags(renderTags)
59  {
60  }
61 
62  virtual void Sync(HdSceneDelegate* delegate,
63  HdTaskContext* ctx,
64  HdDirtyBits* dirtyBits) override {
65  _renderPass->Sync();
66 
67  *dirtyBits = HdChangeTracker::Clean;
68  }
69 
70  virtual void Prepare(HdTaskContext* ctx,
71  HdRenderIndex* renderIndex) override {
72  }
73 
74  virtual void Execute(HdTaskContext* ctx) override {
75  }
76 
77  virtual const TfTokenVector &GetRenderTags() const override {
78  return _renderTags;
79  }
80 
81 private:
82  HdRenderPassSharedPtr _renderPass;
83  TfTokenVector _renderTags;
84 };
85 
94 class UsdImaging_TestDriver final {
95 public:
96  UsdImaging_TestDriver(std::string const& usdFilePath)
97  : _engine()
98  , _renderDelegate()
99  , _renderIndex(nullptr)
100  , _delegate(nullptr)
101  , _geometryPass()
102  , _stage()
103  {
105  HdTokens->geometry,
106  HdReprSelector(HdReprTokens->hull));
107 
108  TfTokenVector renderTags;
109  renderTags.push_back(HdRenderTagTokens->geometry);
110 
111  _Init(UsdStage::Open(usdFilePath),
112  collection,
114  renderTags);
115  }
116 
117  UsdImaging_TestDriver(std::string const& usdFilePath,
118  TfToken const &collectionName,
119  TfToken const &reprName,
120  TfTokenVector const &renderTags)
121  : _engine()
122  , _renderDelegate()
123  , _renderIndex(nullptr)
124  , _delegate(nullptr)
125  , _geometryPass()
126  , _stage()
127  {
129  collectionName,
130  HdReprSelector(reprName));
131 
132  _Init(UsdStage::Open(usdFilePath),
133  collection,
135  renderTags);
136  }
137 
138  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage)
139  : _engine()
140  , _renderDelegate()
141  , _renderIndex(nullptr)
142  , _delegate(nullptr)
143  , _geometryPass()
144  , _stage()
145  {
147  HdTokens->geometry,
148  HdReprSelector(HdReprTokens->hull));
149 
150  TfTokenVector renderTags;
151  renderTags.push_back(HdRenderTagTokens->geometry);
152 
153  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
154  }
155 
156  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
157  TfToken const &collectionName,
158  TfToken const &reprName,
159  TfTokenVector const &renderTags)
160  : _engine()
161  , _renderDelegate()
162  , _renderIndex(nullptr)
163  , _delegate(nullptr)
164  , _geometryPass()
165  , _stage()
166  {
168  collectionName,
169  HdReprSelector(reprName));
170 
171  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
172  }
173 
174  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
175  HdRprimCollection const &collection,
176  SdfPath const &delegateId,
177  TfTokenVector const &renderTags)
178  : _engine()
179  , _renderDelegate()
180  , _renderIndex(nullptr)
181  , _delegate(nullptr)
182  , _geometryPass()
183  , _stage()
184  {
185  _Init(usdStage, collection, delegateId, renderTags);
186  }
187 
188  ~UsdImaging_TestDriver()
189  {
190  delete _delegate;
191  delete _renderIndex;
192  }
193 
194  void Draw() {
195  HdTaskSharedPtrVector tasks = {
196  boost::make_shared<UsdImaging_TestTask>(_geometryPass, _renderTags)
197  };
198  _engine.Execute(&_delegate->GetRenderIndex(), &tasks);
199  }
200  void SetTime(double time) {
201  _delegate->SetTime(time);
202  }
203 
205  void MarkRprimDirty(SdfPath path, HdDirtyBits flag) {
206  _delegate->GetRenderIndex().GetChangeTracker()
207  .MarkRprimDirty(path, flag);
208  }
209 
211  UsdImagingDelegate& GetDelegate() {
212  return *_delegate;
213  }
214 
216  UsdStageRefPtr const& GetStage() {
217  return _stage;
218  }
219 
220 private:
221  HdEngine _engine;
222  Hd_UnitTestNullRenderDelegate _renderDelegate;
223  HdRenderIndex *_renderIndex;
224  UsdImagingDelegate *_delegate;
225  HdRenderPassSharedPtr _geometryPass;
226  UsdStageRefPtr _stage;
227  TfTokenVector _renderTags;
228 
229  void _Init(UsdStageRefPtr const& usdStage,
230  HdRprimCollection const &collection,
231  SdfPath const &delegateId,
232  TfTokenVector const &renderTags) {
233  _renderIndex = HdRenderIndex::New(&_renderDelegate);
234  TF_VERIFY(_renderIndex != nullptr);
235  _delegate = new UsdImagingDelegate(_renderIndex, delegateId);
236 
237  _stage = usdStage;
238  _delegate->Populate(_stage->GetPseudoRoot());
239 
240  _geometryPass = HdRenderPassSharedPtr(
241  new Hd_UnitTestNullRenderPass(_renderIndex, collection));
242 
243  _renderTags = renderTags;
244  }
245 };
246 
247 PXR_NAMESPACE_CLOSE_SCOPE
248 
249 #endif //USDIMAGING_UNIT_TEST_HELPER
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
USD_API UsdPrim GetPseudoRoot() const
Return the stage&#39;s &quot;pseudo-root&quot; prim, whose name is defined by Usd.
static USDIMAGING_API void Populate(std::vector< UsdImagingDelegate * > const &delegates, UsdPrimVector const &rootPrims, std::vector< SdfPathVector > const &excludedPrimPaths, std::vector< SdfPathVector > const &invisedPrimPaths)
Populates the rootPrim in the HdRenderIndex in each delegate, excluding all paths in the excludedPrim...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
The application-facing entry point top-level entry point for accessing Hydra.
Definition: engine.h:48
Adapter class providing data exchange with the client scene graph.
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate)
Create a render index with the given render delegate.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
Describes one or more authored display representations for an rprim.
Definition: repr.h:48
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:86