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,
198  bool firstDrawBatch) const;
199 
200  /// Generates the hash for the settings used to init the graphics pipeline.
201  HDST_API
202  uint64_t GetGraphicsPipelineHash(
203  HdSt_GeometricShaderSharedPtr const & geometricShader,
204  bool firstDrawBatch) const;
205 
206  // A 4d-vector v encodes a 2d-transform as follows:
207  // (x, y) |-> (v[0] * x + v[2], v[1] * y + v[3]).
209 
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  HDST_API
222 
223 private:
224  bool _UseAlphaMask() const;
225  unsigned int _GetFramebufferHeight() const;
226  GfRange2f _ComputeFlippedFilmbackWindow() const;
227 
228  // Helper to set up the aov attachment desc so that it matches the blend
229  // setting of the render pipeline state.
230  // If an aovIndex is specified then the color mask will be correlated.
231  void _InitAttachmentDesc(HgiAttachmentDesc &attachmentDesc,
232  HdRenderPassAovBinding const & binding,
233  HdRenderBuffer const * renderBuffer,
234  int aovIndex) const;
235 
236  void _InitPrimitiveState(
237  HgiGraphicsPipelineDesc * pipeDesc,
238  HdSt_GeometricShaderSharedPtr const & geometricShader) const;
239  void _InitAttachmentState(HgiGraphicsPipelineDesc * pipeDesc,
240  bool firstDrawBatch) const;
241  void _InitDepthStencilState(HgiDepthStencilState * depthState) const;
242  void _InitMultiSampleState(HgiMultiSampleState * multisampleState) const;
243  void _InitRasterizationState(
244  HgiRasterizationState * rasterizationState,
245  HdSt_GeometricShaderSharedPtr const & geometricShader) const;
246 
247  // ---------------------------------------------------------------------- //
248  // Camera state used when no HdCamera available
249  // ---------------------------------------------------------------------- //
250 
251  GfMatrix4d _worldToViewMatrix;
252  GfMatrix4d _projectionMatrix;
253  ClipPlanesVector _clipPlanes;
254 
255  GfMatrix4d _cullMatrix; // updated during Prepare(..)
256 
257  // ---------------------------------------------------------------------- //
258  // Shader Objects
259  // ---------------------------------------------------------------------- //
260  HdStRenderPassShaderSharedPtr _renderPassShader;
261  HdSt_FallbackLightingShaderSharedPtr _fallbackLightingShader;
262  HdStLightingShaderSharedPtr _lightingShader;
263 
264  HdBufferArrayRangeSharedPtr _renderPassStateBar;
265  size_t _clipPlanesBufferSize;
266  float _alphaThresholdCurrent;
267  bool _resolveMultiSampleAov;
268  bool _useSceneMaterials;
269 };
270 
272 
273 #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.
HDST_API void InitGraphicsPipelineDesc(HgiGraphicsPipelineDesc *pipeDesc, HdSt_GeometricShaderSharedPtr const &geometricShader, bool firstDrawBatch) const
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
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:1432
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 uint64_t GetGraphicsPipelineHash(HdSt_GeometricShaderSharedPtr const &geometricShader, bool firstDrawBatch) const
Generates the hash for the settings used to init the graphics pipeline.
HDST_API void ApplyStateFromGeometricShader(HdSt_ResourceBinder const &binder, HdSt_GeometricShaderSharedPtr const &geometricShader)
bool GetUseSceneMaterials() const
HDST_API AxisAlignedAffineTransform ComputeImageToHorizontallyNormalizedFilmback() const
HdStRenderPassShaderSharedPtr const & GetRenderPassShader() const
HDST_API HdStShaderCodeSharedPtrVector GetShaders() const
returns shaders (lighting/renderpass)