Loading...
Searching...
No Matches
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
28
29#include "pxr/pxr.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 <memory>
43#include <string>
44
45PXR_NAMESPACE_OPEN_SCOPE
46
47using HdRenderPassSharedPtr = std::shared_ptr<HdRenderPass>;
48
50class UsdImaging_TestTask final : public HdTask
51{
52public:
53 UsdImaging_TestTask(HdRenderPassSharedPtr const &renderPass,
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
80private:
81 HdRenderPassSharedPtr _renderPass;
82 TfTokenVector _renderTags;
83};
84
93class UsdImaging_TestDriver final {
94public:
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
187 ~UsdImaging_TestDriver()
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
204 void MarkRprimDirty(SdfPath path, HdDirtyBits flag) {
205 _delegate->GetRenderIndex().GetChangeTracker()
206 .MarkRprimDirty(path, flag);
207 }
208
210 UsdImagingDelegate& GetDelegate() {
211 return *_delegate;
212 }
213
215 UsdStageRefPtr const& GetStage() {
216 return _stage;
217 }
218
219private:
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
246PXR_NAMESPACE_CLOSE_SCOPE
247
248#endif //PXR_USD_IMAGING_USD_IMAGING_UNIT_TEST_HELPER_H
The application-facing entry point top-level entry point for accessing Hydra.
Definition: engine.h:48
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:121
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers, const std::string &instanceName=std::string())
Create a render index with the given render delegate.
Describes one or more authored display representations for an rprim.
Definition: repr.h:49
A named, semantic collection of objects.
Adapter class providing data exchange with the client scene graph.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
The primary translation layer between the Hydra (Hd) core and the Usd scene graph.
Definition: delegate.h:84
USDIMAGING_API void Populate(UsdPrim const &rootPrim)
Populates the rootPrim in the HdRenderIndex.
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...
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457