HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderPassState.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_IMAGING_HD_ST_RENDER_PASS_STATE_H
25 #define PXR_IMAGING_HD_ST_RENDER_PASS_STATE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
31 
32 #include <memory>
33 
35 
38 struct HgiMultiSampleState;
40 class HgiCapabilities;
41 
42 using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
43 using HdStRenderPassStateSharedPtr = std::shared_ptr<class HdStRenderPassState>;
44 
45 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
46 
47 using HdStShaderCodeSharedPtr = std::shared_ptr<class HdStShaderCode>;
48 using HdStLightingShaderSharedPtr = std::shared_ptr<class HdStLightingShader>;
50  std::shared_ptr<class HdStRenderPassShader>;
52  std::shared_ptr<class HdSt_FallbackLightingShader>;
54  std::shared_ptr<class HdSt_GeometricShader>;
55 using HdStShaderCodeSharedPtrVector = std::vector<HdStShaderCodeSharedPtr>;
56 class HdRenderIndex;
57 class HdSt_ResourceBinder;
58 
59 /// \class HdStRenderPassState
60 ///
61 /// A set of rendering parameters used among render passes.
62 ///
63 /// Parameters are expressed as GL states, uniforms or shaders.
64 ///
66 {
67 public:
68  HDST_API
70  HDST_API
72  HDST_API
73  ~HdStRenderPassState() override;
74 
75  HDST_API
76  void
77  Prepare(HdResourceRegistrySharedPtr const &resourceRegistry) override;
78 
79  /// XXX: Bind and Unbind set./restore the following GL state.
80  /// This will be reworked to use Hgi in the near future.
81  /// Following states may be changed and restored to
82  /// the GL default at Unbind().
83  /// glEnable(GL_BLEND);
84  /// glEnable(GL_CULL_FACE);
85  /// glEnable(GL_POLYGON_OFFSET_FILL)
86  /// glEnable(GL_PROGRAM_POINT_SIZE);
87  /// glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE)
88  /// glEnable(GL_DEPTH_TEST);
89  /// glEnable(GL_STENCIL_TEST);
90  /// glPolygonOffset()
91  /// glBlend*()
92  /// glColorMask()
93  /// glCullFace()
94  /// glDepthFunc()
95  /// glDepthMask()
96  /// glLineWidth()
97  /// glStencilFunc()
98  /// glStencilOp()
99  HDST_API
100  void Bind(HgiCapabilities const &hgiCapabilities);
101  HDST_API
102  void Unbind(HgiCapabilities const &hgiCapabilities);
103 
104  /// If set to true (default) and the render pass is rendering into a
105  /// multi-sampled aovs, the aovs will be resolved at the end of the render
106  /// pass. If false or the aov is not multi-sampled or the render pass is not
107  /// rendering into the multi-sampled aov, no resolution takes place.
108  HDST_API
109  void SetResolveAovMultiSample(bool state);
110  HDST_API
111  bool GetResolveAovMultiSample() const;
112 
113  /// Set lighting shader
114  HDST_API
115  void SetLightingShader(HdStLightingShaderSharedPtr const &lightingShader);
117  return _lightingShader;
118  }
119 
120  /// renderpass shader
121  HDST_API
122  void SetRenderPassShader(HdStRenderPassShaderSharedPtr const &renderPassShader);
124  return _renderPassShader;
125  }
126 
127  HDST_API
129  HdSt_ResourceBinder const &binder,
130  HdSt_GeometricShaderSharedPtr const &geometricShader);
131 
132  HDST_API
133  void ApplyStateFromCamera();
134 
135  /// scene materials
136  HDST_API
137  void SetUseSceneMaterials(bool state);
138  bool GetUseSceneMaterials() const {
139  return _useSceneMaterials;
140  }
141 
142  /// returns shaders (lighting/renderpass)
143  HDST_API
145 
146  HDST_API
147  size_t GetShaderHash() const;
148 
149  /// Camera setter API
150  ///
151  /// Set matrices, viewport and clipping planes explicitly that are used
152  /// when there is no HdCamera in the render pass state.
153  ///
154  /// This is used by render pass that do not have an associated HdCamera
155  /// such as the shadow render pass.
156  HDST_API
157  void SetCameraFramingState(GfMatrix4d const &worldToViewMatrix,
158  GfMatrix4d const &projectionMatrix,
159  GfVec4d const &viewport,
160  ClipPlanesVector const & clipPlanes);
161 
162  GfMatrix4d GetCullMatrix() const { return _cullMatrix; }
163 
164  /// Overrides the case when no HdCamera is given. In the case, uses
165  /// matrix specified by SetCameraFramingState.
166  HDST_API
167  GfMatrix4d GetWorldToViewMatrix() const override;
168 
169  /// Overrides the case when no HdCamera is given. In the case, uses
170  /// matrix specified by SetCameraFramingState.
171  HDST_API
172  GfMatrix4d GetProjectionMatrix() const override;
173 
174  /// Overrides the case when no HdCamera is given. In the case, uses
175  /// clip planes specified by SetCameraFramingState.
176  HDST_API ClipPlanesVector const & GetClipPlanes() const override;
177 
178  /// Helper to compute and get the y-up Viewport
179  /// This is either using the modern camera framing, which is always y-down,
180  /// or the legacy viewport.
181  HDST_API
182  GfVec4i ComputeViewport() const;
183 
184  // Helper to get graphics cmds descriptor describing textures
185  // we render into and the blend state, constructed from
186  // AOV bindings.
187  //
188  HDST_API
190  HdRenderIndex const * renderIndex) const;
191 
192  // Helper to initialize graphics pipeline descriptor state including
193  // any additional state from the geometric shader.
194  HDST_API
196  HgiGraphicsPipelineDesc * pipeDesc,
197  HdSt_GeometricShaderSharedPtr const & geometricShader) const;
198 
199  /// Generates the hash for the settings used to init the graphics pipeline.
200  HDST_API
201  uint64_t GetGraphicsPipelineHash() const;
202 
203 private:
204  bool _UseAlphaMask() const;
205  unsigned int _GetFramebufferHeight() const;
206  GfRange2f _ComputeFlippedFilmbackWindow() const;
207  // A 4d-vector v encodes a 2d-transform as follows:
208  // (x, y) |-> (v[0] * x + v[2], v[1] * y + v[3]).
209  using _AxisAlignedAffineTransform = GfVec4f;
210  // Computes the transform from pixel coordinates to the horizontally
211  // normalized filmback space which has the following properties:
212  // 1. x = -1 and +1 corresponds to the left and right edge of the filmback,
213  // respectively.
214  // 2. (0, 0) corresponds to the center of the filmback.
215  // 3. Moving a unit in either the x- or y-direction moves by the same
216  // distance on the filmback. In other words, y = -1/a and +1/a
217  // corresponds to the bottom and top edge of the filmback, respectively,
218  // where a is the camera's aspect ratio.
219  _AxisAlignedAffineTransform
220  _ComputeImageToHorizontallyNormalizedFilmback() const;
221 
222  // Helper to set up the aov attachment desc so that it matches the blend
223  // setting of the render pipeline state.
224  // If an aovIndex is specified then the color mask will be correlated.
225  void _InitAttachmentDesc(HgiAttachmentDesc &attachmentDesc,
226  int aovIndex = -1) const;
227 
228  void _InitPrimitiveState(
229  HgiGraphicsPipelineDesc * pipeDesc,
230  HdSt_GeometricShaderSharedPtr const & geometricShader) const;
231  void _InitAttachmentState(HgiGraphicsPipelineDesc * pipeDesc) const;
232  void _InitDepthStencilState(HgiDepthStencilState * depthState) const;
233  void _InitMultiSampleState(HgiMultiSampleState * multisampleState) const;
234  void _InitRasterizationState(
235  HgiRasterizationState * rasterizationState,
236  HdSt_GeometricShaderSharedPtr const & geometricShader) const;
237 
238  // ---------------------------------------------------------------------- //
239  // Camera state used when no HdCamera available
240  // ---------------------------------------------------------------------- //
241 
242  GfMatrix4d _worldToViewMatrix;
243  GfMatrix4d _projectionMatrix;
244  ClipPlanesVector _clipPlanes;
245 
246  GfMatrix4d _cullMatrix; // updated during Prepare(..)
247 
248  // ---------------------------------------------------------------------- //
249  // Shader Objects
250  // ---------------------------------------------------------------------- //
251  HdStRenderPassShaderSharedPtr _renderPassShader;
252  HdSt_FallbackLightingShaderSharedPtr _fallbackLightingShader;
253  HdStLightingShaderSharedPtr _lightingShader;
254 
255  HdBufferArrayRangeSharedPtr _renderPassStateBar;
256  size_t _clipPlanesBufferSize;
257  float _alphaThresholdCurrent;
258  bool _resolveMultiSampleAov;
259  bool _useSceneMaterials;
260 };
261 
263 
264 #endif // PXR_IMAGING_HD_ST_RENDER_PASS_STATE_H
Definition: vec4i.h:60
HDST_API ~HdStRenderPassState() override
HDST_API GfMatrix4d GetProjectionMatrix() const override
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
HDST_API void SetUseSceneMaterials(bool state)
scene materials
HDST_API void SetLightingShader(HdStLightingShaderSharedPtr const &lightingShader)
Set lighting shader.
std::shared_ptr< class HdResourceRegistry > HdResourceRegistrySharedPtr
std::shared_ptr< class HdStLightingShader > HdStLightingShaderSharedPtr
HDST_API void SetRenderPassShader(HdStRenderPassShaderSharedPtr const &renderPassShader)
renderpass shader
HDST_API size_t GetShaderHash() const
HDST_API void Unbind(HgiCapabilities const &hgiCapabilities)
std::shared_ptr< class HdSt_GeometricShader > HdSt_GeometricShaderSharedPtr
Definition: drawItem.h:36
HDST_API void InitGraphicsPipelineDesc(HgiGraphicsPipelineDesc *pipeDesc, HdSt_GeometricShaderSharedPtr const &geometricShader) const
Definition: vec4d.h:62
HdStLightingShaderSharedPtr const & GetLightingShader() const
std::shared_ptr< class HdSt_FallbackLightingShader > HdSt_FallbackLightingShaderSharedPtr
std::shared_ptr< class HdStRenderPassState > HdStRenderPassStateSharedPtr
Definition: commandBuffer.h:45
HDST_API void ApplyStateFromCamera()
HDST_API void Bind(HgiCapabilities const &hgiCapabilities)
HDST_API HgiGraphicsCmdsDesc MakeGraphicsCmdsDesc(HdRenderIndex const *renderIndex) const
std::vector< GfVec4d > ClipPlanesVector
Sets the camera.
GfMatrix4d GetCullMatrix() const
HDST_API ClipPlanesVector const & GetClipPlanes() const override
HDST_API void SetCameraFramingState(GfMatrix4d const &worldToViewMatrix, GfMatrix4d const &projectionMatrix, GfVec4d const &viewport, ClipPlanesVector const &clipPlanes)
HDST_API bool GetResolveAovMultiSample() const
std::shared_ptr< class HdStRenderPassShader > HdStRenderPassShaderSharedPtr
Definition: vec4f.h:62
HDST_API GfMatrix4d GetWorldToViewMatrix() const override
GLuint shader
Definition: glcorearb.h:785
HDST_API HdStRenderPassState()
HDST_API GfVec4i ComputeViewport() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
HDST_API void Prepare(HdResourceRegistrySharedPtr const &resourceRegistry) override
HDST_API void SetResolveAovMultiSample(bool state)
#define HDST_API
Definition: api.h:40
std::vector< HdStShaderCodeSharedPtr > HdStShaderCodeSharedPtrVector
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:44
HDST_API void ApplyStateFromGeometricShader(HdSt_ResourceBinder const &binder, HdSt_GeometricShaderSharedPtr const &geometricShader)
HDST_API uint64_t GetGraphicsPipelineHash() const
Generates the hash for the settings used to init the graphics pipeline.
bool GetUseSceneMaterials() const
HdStRenderPassShaderSharedPtr const & GetRenderPassShader() const
HDST_API HdStShaderCodeSharedPtrVector GetShaders() const
returns shaders (lighting/renderpass)