HDK
 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 PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
25 #define PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
26 
27 /// \file usdImaging/unitTestHelper.h
28 
29 #include "pxr/pxr.h"
31 
33 #include "pxr/imaging/hd/engine.h"
36 #include "pxr/imaging/hd/rprim.h"
38 #include "pxr/imaging/hd/tokens.h"
41 
42 #include <memory>
43 #include <string>
44 
46 
47 using HdRenderPassSharedPtr = std::shared_ptr<HdRenderPass>;
48 
49 /// A simple test task that just causes sync processing
50 class UsdImaging_TestTask final : public HdTask
51 {
52 public:
54  TfTokenVector const &renderTags)
55  : HdTask(SdfPath::EmptyPath())
56  , _renderPass(renderPass)
57  , _renderTags(renderTags)
58  {
59  }
60 
61  virtual void Sync(HdSceneDelegate* delegate,
62  HdTaskContext* ctx,
63  HdDirtyBits* dirtyBits) override {
64  _renderPass->Sync();
65 
66  *dirtyBits = HdChangeTracker::Clean;
67  }
68 
69  virtual void Prepare(HdTaskContext* ctx,
70  HdRenderIndex* renderIndex) override {
71  }
72 
73  virtual void Execute(HdTaskContext* ctx) override {
74  }
75 
76  virtual const TfTokenVector &GetRenderTags() const override {
77  return _renderTags;
78  }
79 
80 private:
81  HdRenderPassSharedPtr _renderPass;
82  TfTokenVector _renderTags;
83 };
84 
85 /// \class UsdImaging_TestDriver
86 ///
87 /// A unit test driver that exercises the core engine.
88 ///
89 /// \note This test driver uses a Null render delegate, so
90 /// no images are produced. It just tests interaction between Hydra and
91 /// UsdImaging during Hydra's Sync phase. of Hydra
92 ///
93 class UsdImaging_TestDriver final {
94 public:
95  UsdImaging_TestDriver(std::string const& usdFilePath)
96  : _engine()
97  , _renderDelegate()
98  , _renderIndex(nullptr)
99  , _delegate(nullptr)
100  , _geometryPass()
101  , _stage()
102  {
104  HdTokens->geometry,
105  HdReprSelector(HdReprTokens->hull));
106 
107  TfTokenVector renderTags;
108  renderTags.push_back(HdRenderTagTokens->geometry);
109 
110  _Init(UsdStage::Open(usdFilePath),
111  collection,
113  renderTags);
114  }
115 
116  UsdImaging_TestDriver(std::string const& usdFilePath,
117  TfToken const &collectionName,
118  TfToken const &reprName,
119  TfTokenVector const &renderTags)
120  : _engine()
121  , _renderDelegate()
122  , _renderIndex(nullptr)
123  , _delegate(nullptr)
124  , _geometryPass()
125  , _stage()
126  {
128  collectionName,
129  HdReprSelector(reprName));
130 
131  _Init(UsdStage::Open(usdFilePath),
132  collection,
134  renderTags);
135  }
136 
137  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage)
138  : _engine()
139  , _renderDelegate()
140  , _renderIndex(nullptr)
141  , _delegate(nullptr)
142  , _geometryPass()
143  , _stage()
144  {
146  HdTokens->geometry,
147  HdReprSelector(HdReprTokens->hull));
148 
149  TfTokenVector renderTags;
150  renderTags.push_back(HdRenderTagTokens->geometry);
151 
152  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
153  }
154 
155  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
156  TfToken const &collectionName,
157  TfToken const &reprName,
158  TfTokenVector const &renderTags)
159  : _engine()
160  , _renderDelegate()
161  , _renderIndex(nullptr)
162  , _delegate(nullptr)
163  , _geometryPass()
164  , _stage()
165  {
167  collectionName,
168  HdReprSelector(reprName));
169 
170  _Init(usdStage, collection, SdfPath::AbsoluteRootPath(), renderTags);
171  }
172 
173  UsdImaging_TestDriver(UsdStageRefPtr const& usdStage,
174  HdRprimCollection const &collection,
175  SdfPath const &delegateId,
176  TfTokenVector const &renderTags)
177  : _engine()
178  , _renderDelegate()
179  , _renderIndex(nullptr)
180  , _delegate(nullptr)
181  , _geometryPass()
182  , _stage()
183  {
184  _Init(usdStage, collection, delegateId, renderTags);
185  }
186 
188  {
189  delete _delegate;
190  delete _renderIndex;
191  }
192 
193  void Draw() {
194  HdTaskSharedPtrVector tasks = {
195  std::make_shared<UsdImaging_TestTask>(_geometryPass, _renderTags)
196  };
197  _engine.Execute(&_delegate->GetRenderIndex(), &tasks);
198  }
199  void SetTime(double time) {
200  _delegate->SetTime(time);
201  }
202 
203  /// Marks an rprim in the RenderIndex as dirty with the given dirty flags.
205  _delegate->GetRenderIndex().GetChangeTracker()
206  .MarkRprimDirty(path, flag);
207  }
208 
209  /// Returns the underlying delegate for this driver.
211  return *_delegate;
212  }
213 
214  /// Returns the populated UsdStage for this driver.
215  UsdStageRefPtr const& GetStage() {
216  return _stage;
217  }
218 
219 private:
220  HdEngine _engine;
221  Hd_UnitTestNullRenderDelegate _renderDelegate;
222  HdRenderIndex *_renderIndex;
223  UsdImagingDelegate *_delegate;
224  HdRenderPassSharedPtr _geometryPass;
225  UsdStageRefPtr _stage;
226  TfTokenVector _renderTags;
227 
228  void _Init(UsdStageRefPtr const& usdStage,
229  HdRprimCollection const &collection,
230  SdfPath const &delegateId,
231  TfTokenVector const &renderTags) {
232  _renderIndex = HdRenderIndex::New(&_renderDelegate, HdDriverVector());
233  TF_VERIFY(_renderIndex != nullptr);
234  _delegate = new UsdImagingDelegate(_renderIndex, delegateId);
235 
236  _stage = usdStage;
237  _delegate->Populate(_stage->GetPseudoRoot());
238 
239  _geometryPass = HdRenderPassSharedPtr(
240  new Hd_UnitTestNullRenderPass(_renderIndex, collection));
241 
242  _renderTags = renderTags;
243  }
244 };
245 
247 
248 #endif //PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
USDIMAGING_API void Populate(UsdPrim const &rootPrim)
Populates the rootPrim in the HdRenderIndex.
UsdImaging_TestDriver(UsdStageRefPtr const &usdStage, HdRprimCollection const &collection, SdfPath const &delegateId, TfTokenVector const &renderTags)
virtual void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
static SDF_API const SdfPath & AbsoluteRootPath()
std::shared_ptr< class HdRenderPass > HdRenderPassSharedPtr
Definition: engine.h:41
GT_API const UT_StringHolder time
USDIMAGING_API void SetTime(UsdTimeCode time)
uint32_t HdDirtyBits
Definition: types.h:127
void MarkRprimDirty(SdfPath path, HdDirtyBits flag)
Marks an rprim in the RenderIndex as dirty with the given dirty flags.
void SetTime(double time)
std::vector< HdTaskSharedPtr > HdTaskSharedPtrVector
Definition: renderIndex.h:70
virtual void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
virtual const TfTokenVector & GetRenderTags() const override
Definition: token.h:87
std::vector< HdDriver * > HdDriverVector
UsdImagingDelegate & GetDelegate()
Returns the underlying delegate for this driver.
UsdImaging_TestDriver(std::string const &usdFilePath)
UsdStageRefPtr const & GetStage()
Returns the populated UsdStage for this driver.
HD_API void MarkRprimDirty(SdfPath const &id, HdDirtyBits bits=AllDirty)
HdRenderIndex & GetRenderIndex()
Returns the RenderIndex owned by this delegate.
std::shared_ptr< HdRenderPass > HdRenderPassSharedPtr
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
Definition: task.h:54
Definition: path.h:288
HD_API void Execute(HdRenderIndex *index, HdTaskSharedPtrVector *tasks)
Execute tasks.
static USD_API UsdStageRefPtr Open(const std::string &filePath, InitialLoadSet load=LoadAll)
std::unordered_map< TfToken, VtValue, TfToken::HashFunctor > HdTaskContext
Definition: renderIndex.h:73
GLsizei const GLchar *const * path
Definition: glew.h:6461
GLsizei const GLchar *const * string
Definition: glew.h:1844
HdChangeTracker & GetChangeTracker()
Definition: renderIndex.h:194
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
UsdImaging_TestDriver(UsdStageRefPtr const &usdStage)
A simple test task that just causes sync processing.
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers)
UsdImaging_TestDriver(UsdStageRefPtr const &usdStage, TfToken const &collectionName, TfToken const &reprName, TfTokenVector const &renderTags)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
UsdImaging_TestDriver(std::string const &usdFilePath, TfToken const &collectionName, TfToken const &reprName, TfTokenVector const &renderTags)
virtual void Execute(HdTaskContext *ctx) override
UsdImaging_TestTask(HdRenderPassSharedPtr const &renderPass, TfTokenVector const &renderTags)