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_UNIT_TEST_HELPER_H
25 #define PXR_IMAGING_HD_ST_UNIT_TEST_HELPER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/lightingShader.h"
29 #include "pxr/imaging/hdSt/renderDelegate.h"
30 #include "pxr/imaging/hdSt/renderPassState.h"
31 
32 #include "pxr/imaging/hd/unitTestDelegate.h"
33 #include "pxr/imaging/hd/driver.h"
34 #include "pxr/imaging/hd/engine.h"
35 #include "pxr/imaging/hd/renderPass.h"
36 #include "pxr/imaging/hio/glslfx.h"
37 
38 #include "pxr/base/gf/vec4d.h"
39 #include "pxr/base/gf/matrix4d.h"
40 
41 #include <memory>
42 #include <vector>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 using HgiUniquePtr = std::unique_ptr<class Hgi>;
47 
48 /// \class HdSt_TestDriver
49 ///
50 /// A unit test driver that exercises the core engine.
51 ///
52 /// \note This test driver does NOT assume OpenGL is available; in the even
53 /// that is is not available, all OpenGL calls become no-ops, but all other work
54 /// is performed as usual.
55 ///
56 class HdSt_TestDriver final {
57 public:
58     HdSt_TestDriver();
59     HdSt_TestDriver(TfToken const &reprName);
60     HdSt_TestDriver(HdReprSelector const &reprToken);
61     ~HdSt_TestDriver();
62 
63     /// Draw
64     void Draw(bool withGuides=false);
65 
66     /// Draw with external renderPass
67     void Draw(HdRenderPassSharedPtr const &renderPass, bool withGuides);
68 
69     /// Set camera to renderpass
70     void SetCamera(GfMatrix4d const &modelViewMatrix,
71                    GfMatrix4d const &projectionMatrix,
72                    GfVec4d const &viewport);
73 
74     void SetCameraClipPlanes(std::vector<GfVec4d> const& clipPlanes);
75 
76     /// Set cull style
77     void SetCullStyle(HdCullStyle cullStyle);
78 
79     /// Returns the renderpass
80     HdRenderPassSharedPtr const &GetRenderPass();
81 
82     /// Returns the renderPassState
GetRenderPassState()83     HdStRenderPassStateSharedPtr const &GetRenderPassState() const {
84         return _renderPassState;
85     }
86 
87     /// Returns the UnitTest delegate
GetDelegate()88     HdUnitTestDelegate& GetDelegate() { return *_sceneDelegate; }
89 
90     /// Switch repr
91     void SetRepr(HdReprSelector const &reprToken);
92 
93 private:
94 
95     void _Init(HdReprSelector const &reprToken);
96 
97     // Hgi and HdDriver should be constructed before HdEngine to ensure they
98     // are destructed last. Hgi may be used during engine/delegate destruction.
99     HgiUniquePtr _hgi;
100     HdDriver _hgiDriver;
101 
102     HdEngine _engine;
103     HdStRenderDelegate   _renderDelegate;
104     HdRenderIndex       *_renderIndex;
105     HdUnitTestDelegate *_sceneDelegate;
106 
107     SdfPath _cameraId;
108     HdReprSelector _reprToken;
109 
110     HdRenderPassSharedPtr _renderPass;
111     HdStRenderPassStateSharedPtr _renderPassState;
112     HdRprimCollection          _collection;
113 };
114 
115 /// \class HdSt_TestLightingShader
116 ///
117 /// A custom lighting shader for unit tests.
118 ///
119 using HdSt_TestLightingShaderSharedPtr =
120     std::shared_ptr<class HdSt_TestLightingShader>;
121 
122 class HdSt_TestLightingShader : public HdStLightingShader
123 {
124 public:
125     HDST_API
126     HdSt_TestLightingShader();
127     HDST_API
128     ~HdSt_TestLightingShader() override;
129 
130     /// HdStShaderCode overrides
131     HDST_API
132     ID ComputeHash() const override;
133     std::string GetSource(TfToken const &shaderStageKey) const override;
134     HDST_API
135     void BindResources(int program,
136                        HdSt_ResourceBinder const &binder,
137                        HdRenderPassState const &state) override;
138     HDST_API
139     void UnbindResources(int program,
140                          HdSt_ResourceBinder const &binder,
141                          HdRenderPassState const &state) override;
142     void AddBindings(HdBindingRequestVector *customBindings) override;
143 
144     /// HdStLightingShader overrides
145     void SetCamera(GfMatrix4d const &worldToViewMatrix,
146                    GfMatrix4d const &projectionMatrix) override;
147 
148     void SetSceneAmbient(GfVec3f const &color);
149     void SetLight(int light, GfVec3f const &dir, GfVec3f const &color);
150 
151 private:
152     struct Light {
153         GfVec3f dir;
154         GfVec3f eyeDir;
155         GfVec3f color;
156     };
157     Light _lights[2];
158     GfVec3f _sceneAmbient;
159     std::unique_ptr<HioGlslfx> _glslfx;
160 };
161 
162 
163 PXR_NAMESPACE_CLOSE_SCOPE
164 
165 #endif  // PXR_IMAGING_HD_ST_UNIT_TEST_HELPER_H
166