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_IMAGING_HD_RENDER_PASS_STATE_H
25 #define PXR_IMAGING_HD_RENDER_PASS_STATE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/aov.h"
30 #include "pxr/imaging/hd/version.h"
31 #include "pxr/imaging/hd/enums.h"
32 
33 #include "pxr/imaging/cameraUtil/framing.h"
34 
35 #include "pxr/usd/sdf/path.h"
36 
37 #include "pxr/base/tf/token.h"
38 #include "pxr/base/vt/value.h"
39 #include "pxr/base/gf/matrix4d.h"
40 #include "pxr/base/gf/vec2f.h"
41 #include "pxr/base/gf/vec4d.h"
42 #include "pxr/base/gf/vec4f.h"
43 
44 #include <memory>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 
49 using HdRenderPassStateSharedPtr = std::shared_ptr<class HdRenderPassState>;
50 using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
51 
52 class HdCamera;
53 
54 /// \class HdRenderPassState
55 ///
56 /// A set of rendering parameters used among render passes.
57 ///
58 /// Parameters are expressed as GL states, uniforms or shaders.
59 ///
60 class HdRenderPassState
61 {
62 public:
63     HD_API
64     HdRenderPassState();
65     HD_API
66     virtual ~HdRenderPassState();
67 
68     /// Schedule to update renderPassState parameters.
69     /// e.g. camera matrix, override color, id blend factor.
70     /// Prepare, called once per frame after the sync phase, but prior to
71     /// the commit phase.
72     HD_API
73     virtual void Prepare(HdResourceRegistrySharedPtr const &resourceRegistry);
74 
75     // ---------------------------------------------------------------------- //
76     /// \name Camera and framing state
77     // ---------------------------------------------------------------------- //
78 
79     using ClipPlanesVector = std::vector<GfVec4d>;
80 
81     /// Camera setter API
82     ///
83     /// Sets the camera transform (aka view inverse matrix) and physical
84     /// parameters, the framing information and a possible overide value
85     /// for the window policy used to conform the camera frustum if its
86     /// aspect ratio is not matching the display window.
87     ///
88     /// Note: using std::pair<bool, ...> here instead of std::optional<...>
89     /// since the latter is only available in C++17 or later.
90     HD_API
91     void SetCameraAndFraming(
92         HdCamera const *camera,
93         CameraUtilFraming const &framing,
94         const std::pair<bool, CameraUtilConformWindowPolicy> &
95                                             overrideWindowPolicy);
96 
97     /// Get camera
98     HdCamera const *
GetCamera()99     GetCamera() const { return _camera; }
100 
101     /// Get framing information determining how the filmback plane maps
102     /// to pixels.
103     const CameraUtilFraming &
GetFraming()104     GetFraming() const { return _framing; }
105 
106     /// The override value for the window policy to conform the camera
107     /// frustum that can be specified by the application.
108     const std::pair<bool, CameraUtilConformWindowPolicy> &
GetOverrideWindowPolicy()109     GetOverrideWindowPolicy() const { return _overrideWindowPolicy; }
110 
111     /// The resolved window policy to conform the camera frustum.
112     /// This is either the override value specified by the application or
113     /// the value from the scene delegate's camera.
114     HD_API
115     CameraUtilConformWindowPolicy
116     GetWindowPolicy() const;
117 
118     /// Camera setter API
119     /// The view, projection and clipping plane info of the camera will be used.
120     ///
121     /// \deprecated Use the more expressive SetCameraAndFraming instead.
122     HD_API
123     void SetCameraAndViewport(HdCamera const *camera,
124                               GfVec4d const& viewport);
125     /// Camera getter API
126     ///
127     /// For backwards compatibility, use the worldToView matrix of the HdCamera
128     /// if given. Otherwise, use the HdCamera's transform.
129     ///
130     /// The HdRenderPassState also has a fallback value for the view
131     /// matrix that is used if no HdCamera was specified, that can be set with,
132     /// e.g.g, HdStRenderPassState::SetCameraFramingState.
133     ///
134     HD_API
135     GfMatrix4d GetWorldToViewMatrix() const;
136 
137     /// It is expected that an HdCamera was specified that has physically based
138     /// attributes. The projection matrix is computed from those attributes and
139     /// the conform window policy is applied.
140     ///
141     /// For backwards compatibility with scene and render delegates:
142     /// if the HdCamera has no physically based attributes (more precisely,
143     /// the scene delegate provided a VtValue for focalLength that is either
144     /// empty or 1.0f), the HdCamera's projection matrix is used.
145     /// The HdRenderPassState also has a fallback value for the projection
146     /// matrix that is used if no HdCamera was specified, that can be set with,
147     /// e.g.g, HdStRenderPassState::SetCameraFramingState.
148     ///
149     HD_API
150     GfMatrix4d GetProjectionMatrix() const;
151 
152     /// Only use when clients did not specify a camera framing.
153     ///
154     /// \deprecated
GetViewport()155     GfVec4f const & GetViewport() const { return _viewport; }
156 
157     HD_API
158     ClipPlanesVector const & GetClipPlanes() const;
159 
GetCullMatrix()160     GfMatrix4d GetCullMatrix() const { return _cullMatrix; }
161 
162     // ---------------------------------------------------------------------- //
163     /// \name Application rendering state
164     // ---------------------------------------------------------------------- //
165 
166     /// Set an override color for rendering where the R, G and B components
167     /// are the color and the alpha component is the blend value
168     HD_API
169     void SetOverrideColor(GfVec4f const &color);
GetOverrideColor()170     const GfVec4f& GetOverrideColor() const { return _overrideColor; }
171 
172     /// Set a wireframe color for rendering where the R, G and B components
173     /// are the color and the alpha component is the blend value
174     HD_API
175     void SetWireframeColor(GfVec4f const &color);
GetWireframeColor()176     const GfVec4f& GetWireframeColor() const { return _wireframeColor; }
177 
178     HD_API
179     void SetMaskColor(GfVec4f const &color);
GetMaskColor()180     const GfVec4f& GetMaskColor() const { return _maskColor; }
181 
182     HD_API
183     void SetIndicatorColor(GfVec4f const &color);
GetIndicatorColor()184     const GfVec4f& GetIndicatorColor() const { return _indicatorColor; }
185 
186     /// Set a point color for rendering where the R, G and B components
187     /// are the color and the alpha component is the blend value
188     HD_API
189     void SetPointColor(GfVec4f const &color);
GetPointColor()190     const GfVec4f& GetPointColor() const { return _pointColor; }
191 
192     /// Set the point size for unselected points.
193     HD_API
194     void SetPointSize(float size);
GetPointSize()195     float GetPointSize() const { return _pointSize; }
196 
197     /// Set the point size for selected points.
198     HD_API
199     void SetPointSelectedSize(float size);
GetPointSelectedSize()200     float GetPointSelectedSize() const { return _pointSelectedSize; }
201 
202     /// XXX: Hacky way of disabling lighting
203     HD_API
204     void SetLightingEnabled(bool enabled);
GetLightingEnabled()205     bool GetLightingEnabled() const { return _lightingEnabled; }
206 
207     HD_API
208     void SetClippingEnabled(bool enabled);
GetClippingEnabled()209     bool GetClippingEnabled() const { return _clippingEnabled; }
210 
211     // ---------------------------------------------------------------------- //
212     /// \name Render pipeline state
213     // ---------------------------------------------------------------------- //
214 
215     /// Set the attachments for this renderpass to render into.
216     HD_API
217     void SetAovBindings(HdRenderPassAovBindingVector const &aovBindings);
218     HD_API
219     HdRenderPassAovBindingVector const& GetAovBindings() const;
220 
221     /// Set the AOVs that this renderpass needs to read from.
222     HD_API
223     void SetAovInputBindings(HdRenderPassAovBindingVector const &aovBindings);
224     HD_API
225     HdRenderPassAovBindingVector const& GetAovInputBindings() const;
226 
227     /// Returns true if the render pass wants to render into the multi-sample
228     /// aovs. Returns false if the render wants to render into the resolve aovs.
229     HD_API
230     void SetUseAovMultiSample(bool state);
231     HD_API
232     bool GetUseAovMultiSample() const;
233 
234     HD_API
235     void SetCullStyle(HdCullStyle cullStyle);
236     HD_API
GetCullStyle()237     HdCullStyle GetCullStyle() const { return _cullStyle; }
238 
239     HD_API
240     void SetAlphaThreshold(float alphaThreshold);
GetAlphaThreshold()241     float GetAlphaThreshold() const { return _alphaThreshold; }
242 
243     HD_API
244     void SetTessLevel(float level);
GetTessLevel()245     float GetTessLevel() const { return _tessLevel; }
246 
247     HD_API
248     void SetDrawingRange(GfVec2f const &drawRange);
GetDrawingRange()249     GfVec2f GetDrawingRange() const { return _drawRange; } // in pixel
GetDrawingRangeNDC()250     GfVec2f GetDrawingRangeNDC() const { // in ndc
251         return GfVec2f(2*_drawRange[0]/_viewport[2],
252                        2*_drawRange[1]/_viewport[3]);
253     }
254 
255     HD_API
256     void SetDepthBiasUseDefault(bool useDefault);
GetDepthBiasUseDefault()257     bool GetDepthBiasUseDefault() const { return _depthBiasUseDefault; }
258 
259     HD_API
260     void SetDepthBiasEnabled(bool enabled);
GetDepthBiasEnabled()261     bool GetDepthBiasEnabled() const { return _depthBiasEnabled; }
262 
263     HD_API
264     void SetDepthBias(float constantFactor, float slopeFactor);
265 
266     HD_API
267     void SetDepthFunc(HdCompareFunction depthFunc);
GetDepthFunc()268     HdCompareFunction GetDepthFunc() const { return _depthFunc; }
269 
270     HD_API
271     void SetEnableDepthMask(bool state);
272     HD_API
273     bool GetEnableDepthMask();
274 
275     HD_API
276     void SetEnableDepthTest(bool enabled);
277     HD_API
278     bool GetEnableDepthTest() const;
279 
280     HD_API
281     void SetStencil(HdCompareFunction func, int ref, int mask,
282                     HdStencilOp fail, HdStencilOp zfail, HdStencilOp zpass);
GetStencilFunc()283     HdCompareFunction GetStencilFunc() const { return _stencilFunc; }
GetStencilRef()284     int GetStencilRef() const { return _stencilRef; }
GetStencilMask()285     int GetStencilMask() const { return _stencilMask; }
GetStencilFailOp()286     HdStencilOp GetStencilFailOp() const { return _stencilFailOp; }
GetStencilDepthFailOp()287     HdStencilOp GetStencilDepthFailOp() const { return _stencilZFailOp; }
GetStencilDepthPassOp()288     HdStencilOp GetStencilDepthPassOp() const { return _stencilZPassOp; }
289     HD_API
290     void SetStencilEnabled(bool enabled);
291     HD_API
292     bool GetStencilEnabled() const;
293 
294     HD_API
295     void SetLineWidth(float width);
GetLineWidth()296     float GetLineWidth() const { return _lineWidth; }
297 
298     HD_API
299     void SetBlend(HdBlendOp colorOp,
300                   HdBlendFactor colorSrcFactor,
301                   HdBlendFactor colorDstFactor,
302                   HdBlendOp alphaOp,
303                   HdBlendFactor alphaSrcFactor,
304                   HdBlendFactor alphaDstFactor);
GetBlendColorOp()305     HdBlendOp GetBlendColorOp() { return _blendColorOp; }
GetBlendColorSrcFactor()306     HdBlendFactor GetBlendColorSrcFactor() { return _blendColorSrcFactor; }
GetBlendColorDstFactor()307     HdBlendFactor GetBlendColorDstFactor() { return _blendColorDstFactor; }
GetBlendAlphaOp()308     HdBlendOp GetBlendAlphaOp() { return _blendAlphaOp; }
GetBlendAlphaSrcFactor()309     HdBlendFactor GetBlendAlphaSrcFactor() { return _blendAlphaSrcFactor; }
GetBlendAlphaDstFactor()310     HdBlendFactor GetBlendAlphaDstFactor() { return _blendAlphaDstFactor; }
311     HD_API
312     void SetBlendConstantColor(GfVec4f const & color);
GetBlendConstantColor()313     const GfVec4f& GetBlendConstantColor() const { return _blendConstantColor; }
314     HD_API
315     void SetBlendEnabled(bool enabled);
316 
317     HD_API
318     void SetAlphaToCoverageEnabled(bool enabled);
GetAlphaToCoverageEnabled()319     bool GetAlphaToCoverageEnabled() const { return _alphaToCoverageEnabled; }
320 
321     HD_API
322     void SetColorMaskUseDefault(bool useDefault);
GetColorMaskUseDefault()323     bool GetColorMaskUseDefault() const { return _colorMaskUseDefault;}
324 
325     enum ColorMask {
326         ColorMaskNone,
327         ColorMaskRGB,
328         ColorMaskRGBA
329     };
330 
331     HD_API
332     void SetColorMasks(std::vector<ColorMask> const& masks);
GetColorMasks()333     std::vector<ColorMask> const& GetColorMasks() const { return _colorMasks; }
334 
335 protected:
336     // ---------------------------------------------------------------------- //
337     // Camera and framing state
338     // ---------------------------------------------------------------------- //
339     HdCamera const *_camera;
340     GfVec4f _viewport;
341     CameraUtilFraming _framing;
342     std::pair<bool, CameraUtilConformWindowPolicy> _overrideWindowPolicy;
343     GfMatrix4d _cullMatrix; // updated during Prepare(..)
344 
345     // Used by applications setting the view matrix directly instead of
346     // using an HdCamera. Will be removed eventually.
347     GfMatrix4d _worldToViewMatrix;
348     // Used by applications setting the projection matrix directly instead
349     // of using an HdCamera. Will be removed eventually.
350     GfMatrix4d _projectionMatrix;
351     // Used by applications setting the clip planes directly instead
352     // of using an HdCamera. Will be removed eventually.
353     ClipPlanesVector _clipPlanes;
354 
355     // ---------------------------------------------------------------------- //
356     // Application rendering state
357     // ---------------------------------------------------------------------- //
358     GfVec4f _overrideColor;
359     GfVec4f _wireframeColor;
360     GfVec4f _pointColor;
361     float _pointSize;
362     bool _lightingEnabled;
363     bool _clippingEnabled;
364 
365     GfVec4f _maskColor;
366     GfVec4f _indicatorColor;
367     float _pointSelectedSize;
368 
369     // ---------------------------------------------------------------------- //
370     // Render pipeline state
371     // ---------------------------------------------------------------------- //
372     float _alphaThreshold;
373     float _tessLevel;
374     GfVec2f _drawRange;
375 
376     bool _depthBiasUseDefault; // inherit existing state, ignore values below.
377     bool _depthBiasEnabled;
378     float _depthBiasConstantFactor;
379     float _depthBiasSlopeFactor;
380     HdCompareFunction _depthFunc;
381     bool _depthMaskEnabled;
382     bool _depthTestEnabled;
383 
384     HdCullStyle _cullStyle;
385 
386     // Stencil RenderPassState
387     HdCompareFunction _stencilFunc;
388     int _stencilRef;
389     int _stencilMask;
390     HdStencilOp _stencilFailOp;
391     HdStencilOp _stencilZFailOp;
392     HdStencilOp _stencilZPassOp;
393     bool _stencilEnabled;
394 
395     // Line width
396     float _lineWidth;
397 
398     // Blending
399     HdBlendOp _blendColorOp;
400     HdBlendFactor _blendColorSrcFactor;
401     HdBlendFactor _blendColorDstFactor;
402     HdBlendOp _blendAlphaOp;
403     HdBlendFactor _blendAlphaSrcFactor;
404     HdBlendFactor _blendAlphaDstFactor;
405     GfVec4f _blendConstantColor;
406     bool _blendEnabled;
407 
408     // alpha to coverage
409     bool _alphaToCoverageEnabled;
410 
411     bool _colorMaskUseDefault;
412     std::vector<ColorMask> _colorMasks;
413 
414     HdRenderPassAovBindingVector _aovBindings;
415     HdRenderPassAovBindingVector _aovInputBindings;
416     bool _useMultiSampleAov;
417 };
418 
419 PXR_NAMESPACE_CLOSE_SCOPE
420 
421 #endif  // PXR_IMAGING_HD_RENDER_PASS_STATE_H
422