HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
geometricShader.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_GEOMETRIC_SHADER_H
25 #define PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
30 #include "pxr/imaging/hd/version.h"
31 #include "pxr/imaging/hd/enums.h"
32 #include "pxr/imaging/hgi/enums.h"
33 #include "pxr/usd/sdf/path.h"
34 
35 #include <memory>
36 
38 
40  std::shared_ptr<class HdSt_GeometricShader>;
42  std::shared_ptr<class HdStResourceRegistry>;
43 struct HdSt_ShaderKey;
44 class HioGlslfx;
45 
46 /// \class HdSt_GeometricShader
47 ///
48 /// Storm breaks down the concept of a shader program into distinct
49 /// conceptual pieces that are then stitched together during code generation.
50 /// The pieces are:
51 /// (i) geometric shader
52 /// (ii) material shader
53 /// (iii) lighting shader
54 /// (iv) render pass shader
55 ///
56 /// The geometric shader contains the entry points for the relevant shader
57 /// stages and uses geometry opinions (such as cullstyle, double sided, etc)
58 /// to generate shader code variants via mixins.
59 ///
61 public:
62  /// Used in HdSt_CodeGen to generate the appropriate shader source
63  enum class PrimitiveType {
65  PRIM_BASIS_CURVES_LINES, // when linear (or) non-refined cubic
66  PRIM_BASIS_CURVES_LINEAR_PATCHES, // refined linear curves
67  PRIM_BASIS_CURVES_CUBIC_PATCHES, // refined cubic curves
69  PRIM_MESH_REFINED_TRIANGLES, // e.g: loop subdiv
70  PRIM_MESH_COARSE_QUADS, // e.g: quadrangulation for ptex
71  PRIM_MESH_REFINED_QUADS, // e.g: catmark/bilinear subdiv
72  PRIM_MESH_COARSE_TRIQUADS, // e.g: triangulated quadrangulation
73  PRIM_MESH_REFINED_TRIQUADS, // e.g: triangulated catmark/bilinear
74  PRIM_MESH_BSPLINE, // e.g. catmark limit surface patches
75  PRIM_MESH_BOXSPLINETRIANGLE, // e.g. loop limit surface patches
76  PRIM_VOLUME, // Triangles of bounding box of a volume.
77  PRIM_COMPUTE // A compute shader, e.g frustum culling
78  };
79 
80  /// static query functions for PrimitiveType
81  static inline bool IsPrimTypePoints (PrimitiveType primType) {
82  return primType == PrimitiveType::PRIM_POINTS;
83  }
84 
85  static inline bool IsPrimTypeBasisCurves(PrimitiveType primType) {
86  return (primType == PrimitiveType::PRIM_BASIS_CURVES_LINES ||
89  }
90 
91  static inline bool IsPrimTypeMesh(PrimitiveType primType) {
92  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
100  }
101 
102  static inline bool IsPrimTypeTriangles(PrimitiveType primType) {
103  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
105  primType == PrimitiveType::PRIM_VOLUME);
106  }
107 
108  static inline bool IsPrimTypeQuads(PrimitiveType primType) {
109  return (primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
111  }
112 
113  static inline bool IsPrimTypeTriQuads(PrimitiveType primType) {
114  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIQUADS ||
116  }
117 
118  static inline bool IsPrimTypeRefinedMesh(PrimitiveType primType) {
119  return (primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
122  primType == PrimitiveType::PRIM_MESH_BSPLINE ||
124  }
125 
126  static inline bool IsPrimTypePatches(PrimitiveType primType) {
127  return primType == PrimitiveType::PRIM_MESH_BSPLINE ||
131  }
132 
133  static inline bool IsPrimTypeCompute(PrimitiveType primType) {
134  return primType == PrimitiveType::PRIM_COMPUTE;
135  }
136 
137  // Face-varying patch type
138  enum class FvarPatchType {
145  PATCH_NONE
146  };
147 
148  HDST_API
149  HdSt_GeometricShader(std::string const &glslfxString,
150  PrimitiveType primType,
151  HdCullStyle cullStyle,
152  bool useHardwareFaceCulling,
153  bool hasMirroredTransform,
154  bool doubleSided,
155  bool useMetalTessellation,
156  HdPolygonMode polygonMode,
157  bool cullingPass,
158  FvarPatchType fvarPatchType,
159  SdfPath const &debugId = SdfPath(),
160  float lineWidth = 0);
161 
162  HDST_API
163  ~HdSt_GeometricShader() override;
164 
165  // HdShader overrides
166  HDST_API
167  ID ComputeHash() const override;
168  HDST_API
169  std::string GetSource(TfToken const &shaderStageKey) const override;
170  HDST_API
171  void BindResources(int program,
172  HdSt_ResourceBinder const &binder) override;
173 
174  HDST_API
175  void UnbindResources(int program,
176  HdSt_ResourceBinder const &binder) override;
177  HDST_API
178  void AddBindings(HdStBindingRequestVector *customBindings) override;
179 
180  /// Returns true if this geometric shader is used for GPU frustum culling.
181  bool IsFrustumCullingPass() const {
182  return _frustumCullingPass;
183  }
184 
186  return _primType;
187  }
188 
189  bool GetUseMetalTessellation() const {
190  return _useMetalTessellation;
191  }
192 
193  float GetLineWidth() const {
194  return _lineWidth;
195  }
196 
198  return _polygonMode;
199  }
200 
201  /// member query functions for PrimitiveType
202  bool IsPrimTypePoints() const {
203  return IsPrimTypePoints(_primType);
204  }
205 
206  bool IsPrimTypeBasisCurves() const {
207  return IsPrimTypeBasisCurves(_primType);
208  }
209 
210  bool IsPrimTypeMesh() const {
211  return IsPrimTypeMesh(_primType);
212  }
213 
214  bool IsPrimTypeTriangles() const {
215  return IsPrimTypeTriangles(_primType);
216  }
217 
218  bool IsPrimTypeQuads() const {
219  return IsPrimTypeQuads(_primType);
220  }
221 
222  bool IsPrimTypeTriQuads() const {
223  return IsPrimTypeTriQuads(_primType);
224  }
225 
226  bool IsPrimTypeRefinedMesh() const {
227  return IsPrimTypeRefinedMesh(_primType);
228  }
229 
230  bool IsPrimTypePatches() const {
231  return IsPrimTypePatches(_primType);
232  }
233 
234  bool IsPrimTypeCompute() const {
235  return IsPrimTypeCompute(_primType);
236  }
237 
239  return _fvarPatchType;
240  }
241 
242  // Returns the primitive index size based on the primitive type
243  // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
244  HDST_API
245  int GetPrimitiveIndexSize() const;
246 
247  // Returns the number of vertices output for patch evaluation,
248  // i.e. the number of tessellation control shader invocations.
249  HDST_API
250  int GetNumPatchEvalVerts() const;
251 
252  // Returns the primitive index size for the geometry shader shade
253  // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
254  HDST_API
256 
257  // Returns the HgiPrimitiveType for the primitive type.
258  HDST_API
260 
261  // Resolve the cull mode from the cull style in the render state.
262  HDST_API
263  HgiCullMode ResolveCullMode(HdCullStyle const renderStateCullStyle) const;
264 
265  // Factory for convenience.
267  HdSt_ShaderKey const &shaderKey,
268  HdStResourceRegistrySharedPtr const &resourceRegistry);
269 
270 private:
271  PrimitiveType _primType;
272  HdCullStyle _cullStyle;
273  bool _useHardwareFaceCulling;
274  bool _hasMirroredTransform;
275  bool _doubleSided;
276  bool _useMetalTessellation;
277  HdPolygonMode _polygonMode;
278  float _lineWidth;
279 
280  std::unique_ptr<HioGlslfx> _glslfx;
281  bool _frustumCullingPass;
282  FvarPatchType _fvarPatchType;
283  ID _hash;
284 
285  // No copying
286  HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
287  HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
288 
289  HioGlslfx const * _GetGlslfx() const override;
290 };
291 
292 
294 
295 #endif // PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
FvarPatchType GetFvarPatchType() const
PrimitiveType GetPrimitiveType() const
HDST_API std::string GetSource(TfToken const &shaderStageKey) const override
static bool IsPrimTypeQuads(PrimitiveType primType)
HdCullStyle
Definition: enums.h:122
static bool IsPrimTypeCompute(PrimitiveType primType)
bool IsPrimTypeTriQuads() const
static bool IsPrimTypeTriangles(PrimitiveType primType)
HdPolygonMode GetPolygonMode() const
HDST_API int GetNumPatchEvalVerts() const
bool IsPrimTypeCompute() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
HdPolygonMode
Definition: enums.h:137
bool IsPrimTypeTriangles() const
std::vector< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:40
std::shared_ptr< class HdSt_GeometricShader > HdSt_GeometricShaderSharedPtr
Definition: drawItem.h:36
bool IsPrimTypeRefinedMesh() const
bool IsPrimTypeQuads() const
static bool IsPrimTypeTriQuads(PrimitiveType primType)
static bool IsPrimTypeMesh(PrimitiveType primType)
HgiCullMode
Definition: enums.h:449
bool IsPrimTypeMesh() const
HDST_API int GetPrimitiveIndexSize() const
PrimitiveType
Used in HdSt_CodeGen to generate the appropriate shader source.
Definition: token.h:87
static bool IsPrimTypeRefinedMesh(PrimitiveType primType)
HDST_API HdSt_GeometricShader(std::string const &glslfxString, PrimitiveType primType, HdCullStyle cullStyle, bool useHardwareFaceCulling, bool hasMirroredTransform, bool doubleSided, bool useMetalTessellation, HdPolygonMode polygonMode, bool cullingPass, FvarPatchType fvarPatchType, SdfPath const &debugId=SdfPath(), float lineWidth=0)
HDST_API void UnbindResources(int program, HdSt_ResourceBinder const &binder) override
Unbinds shader-specific resources.
static bool IsPrimTypeBasisCurves(PrimitiveType primType)
bool IsPrimTypePoints() const
member query functions for PrimitiveType
HgiPrimitiveType
Definition: enums.h:610
bool IsPrimTypePatches() const
HDST_API ~HdSt_GeometricShader() override
static bool IsPrimTypePatches(PrimitiveType primType)
Definition: path.h:290
bool GetUseMetalTessellation() const
HDST_API ID ComputeHash() const override
static HdSt_GeometricShaderSharedPtr Create(HdSt_ShaderKey const &shaderKey, HdStResourceRegistrySharedPtr const &resourceRegistry)
HDST_API int GetNumPrimitiveVertsForGeometryShader() const
bool IsPrimTypeBasisCurves() const
float GetLineWidth() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
HDST_API void BindResources(int program, HdSt_ResourceBinder const &binder) override
static bool IsPrimTypePoints(PrimitiveType primType)
static query functions for PrimitiveType
#define HDST_API
Definition: api.h:40
HDST_API HgiPrimitiveType GetHgiPrimitiveType() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool IsFrustumCullingPass() const
Returns true if this geometric shader is used for GPU frustum culling.
HDST_API void AddBindings(HdStBindingRequestVector *customBindings) override
Add custom bindings (used by codegen)
HDST_API HgiCullMode ResolveCullMode(HdCullStyle const renderStateCullStyle) const
std::shared_ptr< class HdStResourceRegistry > HdStResourceRegistrySharedPtr
Definition: commandBuffer.h:47
GLbitfield GLuint program
Definition: glcorearb.h:1931