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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
8 #define PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
13 #include "pxr/imaging/hd/version.h"
14 #include "pxr/imaging/hd/enums.h"
15 #include "pxr/imaging/hgi/enums.h"
16 #include "pxr/usd/sdf/path.h"
17 
18 #include <memory>
19 
21 
23  std::shared_ptr<class HdSt_GeometricShader>;
25  std::shared_ptr<class HdStResourceRegistry>;
26 struct HdSt_ShaderKey;
27 class HioGlslfx;
28 
29 /// \class HdSt_GeometricShader
30 ///
31 /// Storm breaks down the concept of a shader program into distinct
32 /// conceptual pieces that are then stitched together during code generation.
33 /// The pieces are:
34 /// (i) geometric shader
35 /// (ii) material shader
36 /// (iii) lighting shader
37 /// (iv) render pass shader
38 ///
39 /// The geometric shader contains the entry points for the relevant shader
40 /// stages and uses geometry opinions (such as cullstyle, double sided, etc)
41 /// to generate shader code variants via mixins.
42 ///
44 public:
45  /// Used in HdSt_CodeGen to generate the appropriate shader source
46  enum class PrimitiveType {
48  PRIM_BASIS_CURVES_LINES, // when linear (or) non-refined cubic
49  PRIM_BASIS_CURVES_LINEAR_PATCHES, // refined linear curves
50  PRIM_BASIS_CURVES_CUBIC_PATCHES, // refined cubic curves
52  PRIM_MESH_REFINED_TRIANGLES, // e.g: loop subdiv
53  PRIM_MESH_COARSE_QUADS, // e.g: quadrangulation for ptex
54  PRIM_MESH_REFINED_QUADS, // e.g: catmark/bilinear subdiv
55  PRIM_MESH_COARSE_TRIQUADS, // e.g: triangulated quadrangulation
56  PRIM_MESH_REFINED_TRIQUADS, // e.g: triangulated catmark/bilinear
57  PRIM_MESH_BSPLINE, // e.g. catmark limit surface patches
58  PRIM_MESH_BOXSPLINETRIANGLE, // e.g. loop limit surface patches
59  PRIM_VOLUME, // Triangles of bounding box of a volume.
60  PRIM_COMPUTE // A compute shader, e.g frustum culling
61  };
62 
63  /// static query functions for PrimitiveType
64  static inline bool IsPrimTypePoints (PrimitiveType primType) {
65  return primType == PrimitiveType::PRIM_POINTS;
66  }
67 
68  static inline bool IsPrimTypeBasisCurves(PrimitiveType primType) {
69  return (primType == PrimitiveType::PRIM_BASIS_CURVES_LINES ||
72  }
73 
74  static inline bool IsPrimTypeMesh(PrimitiveType primType) {
75  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
83  }
84 
85  static inline bool IsPrimTypeTriangles(PrimitiveType primType) {
86  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
88  primType == PrimitiveType::PRIM_VOLUME);
89  }
90 
91  static inline bool IsPrimTypeQuads(PrimitiveType primType) {
92  return (primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
94  }
95 
96  static inline bool IsPrimTypeTriQuads(PrimitiveType primType) {
97  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIQUADS ||
99  }
100 
101  static inline bool IsPrimTypeRefinedMesh(PrimitiveType primType) {
102  return (primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
105  primType == PrimitiveType::PRIM_MESH_BSPLINE ||
107  }
108 
109  static inline bool IsPrimTypePatches(PrimitiveType primType) {
110  return primType == PrimitiveType::PRIM_MESH_BSPLINE ||
114  }
115 
116  static inline bool IsPrimTypeCompute(PrimitiveType primType) {
117  return primType == PrimitiveType::PRIM_COMPUTE;
118  }
119 
120  // Face-varying patch type
121  enum class FvarPatchType {
128  PATCH_NONE
129  };
130 
131  HDST_API
132  HdSt_GeometricShader(std::string const &glslfxString,
133  PrimitiveType primType,
134  HdCullStyle cullStyle,
135  bool useHardwareFaceCulling,
136  bool hasMirroredTransform,
137  bool doubleSided,
138  bool useMetalTessellation,
139  HdPolygonMode polygonMode,
140  bool cullingPass,
141  FvarPatchType fvarPatchType,
142  SdfPath const &debugId = SdfPath(),
143  float lineWidth = 0);
144 
145  HDST_API
146  ~HdSt_GeometricShader() override;
147 
148  // HdShader overrides
149  HDST_API
150  ID ComputeHash() const override;
151  HDST_API
152  std::string GetSource(TfToken const &shaderStageKey) const override;
153  HDST_API
154  void BindResources(int program,
155  HdSt_ResourceBinder const &binder) override;
156 
157  HDST_API
158  void UnbindResources(int program,
159  HdSt_ResourceBinder const &binder) override;
160  HDST_API
161  void AddBindings(HdStBindingRequestVector *customBindings) override;
162 
163  /// Returns true if this geometric shader is used for GPU frustum culling.
164  bool IsFrustumCullingPass() const {
165  return _frustumCullingPass;
166  }
167 
169  return _primType;
170  }
171 
172  bool GetUseMetalTessellation() const {
173  return _useMetalTessellation;
174  }
175 
176  float GetLineWidth() const {
177  return _lineWidth;
178  }
179 
181  return _polygonMode;
182  }
183 
184  /// member query functions for PrimitiveType
185  bool IsPrimTypePoints() const {
186  return IsPrimTypePoints(_primType);
187  }
188 
189  bool IsPrimTypeBasisCurves() const {
190  return IsPrimTypeBasisCurves(_primType);
191  }
192 
193  bool IsPrimTypeMesh() const {
194  return IsPrimTypeMesh(_primType);
195  }
196 
197  bool IsPrimTypeTriangles() const {
198  return IsPrimTypeTriangles(_primType);
199  }
200 
201  bool IsPrimTypeQuads() const {
202  return IsPrimTypeQuads(_primType);
203  }
204 
205  bool IsPrimTypeTriQuads() const {
206  return IsPrimTypeTriQuads(_primType);
207  }
208 
209  bool IsPrimTypeRefinedMesh() const {
210  return IsPrimTypeRefinedMesh(_primType);
211  }
212 
213  bool IsPrimTypePatches() const {
214  return IsPrimTypePatches(_primType);
215  }
216 
217  bool IsPrimTypeCompute() const {
218  return IsPrimTypeCompute(_primType);
219  }
220 
222  return _fvarPatchType;
223  }
224 
225  // Returns the primitive index size based on the primitive type
226  // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
227  HDST_API
228  int GetPrimitiveIndexSize() const;
229 
230  // Returns the number of vertices output for patch evaluation,
231  // i.e. the number of tessellation control shader invocations.
232  HDST_API
233  int GetNumPatchEvalVerts() const;
234 
235  // Returns the primitive index size for the geometry shader shade
236  // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
237  HDST_API
239 
240  // Returns the HgiPrimitiveType for the primitive type.
241  HDST_API
243 
244  // Resolve the cull mode from the cull style in the render state.
245  HDST_API
246  HgiCullMode ResolveCullMode(HdCullStyle const renderStateCullStyle) const;
247 
248  // Factory for convenience.
249  HDST_API
251  HdSt_ShaderKey const &shaderKey,
252  HdStResourceRegistrySharedPtr const &resourceRegistry);
253 
254 private:
255  PrimitiveType _primType;
256  HdCullStyle _cullStyle;
257  bool _useHardwareFaceCulling;
258  bool _hasMirroredTransform;
259  bool _doubleSided;
260  bool _useMetalTessellation;
261  HdPolygonMode _polygonMode;
262  float _lineWidth;
263 
264  std::unique_ptr<HioGlslfx> _glslfx;
265  bool _frustumCullingPass;
266  FvarPatchType _fvarPatchType;
267  ID _hash;
268 
269  // No copying
270  HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
271  HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
272 
273  HioGlslfx const * _GetGlslfx() const override;
274 };
275 
276 
278 
279 #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:105
static bool IsPrimTypeCompute(PrimitiveType primType)
bool IsPrimTypeTriQuads() const
static bool IsPrimTypeTriangles(PrimitiveType primType)
HdPolygonMode GetPolygonMode() const
HDST_API int GetNumPatchEvalVerts() const
bool IsPrimTypeCompute() const
HdPolygonMode
Definition: enums.h:120
bool IsPrimTypeTriangles() const
std::vector< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:23
std::shared_ptr< class HdSt_GeometricShader > HdSt_GeometricShaderSharedPtr
Definition: drawItem.h:19
bool IsPrimTypeRefinedMesh() const
bool IsPrimTypeQuads() const
static bool IsPrimTypeTriQuads(PrimitiveType primType)
static bool IsPrimTypeMesh(PrimitiveType primType)
HgiCullMode
Definition: enums.h:435
bool IsPrimTypeMesh() const
HDST_API int GetPrimitiveIndexSize() const
PrimitiveType
Used in HdSt_CodeGen to generate the appropriate shader source.
Definition: token.h:70
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:596
bool IsPrimTypePatches() const
HDST_API ~HdSt_GeometricShader() override
static bool IsPrimTypePatches(PrimitiveType primType)
Definition: path.h:273
bool GetUseMetalTessellation() const
static HDST_API HdSt_GeometricShaderSharedPtr Create(HdSt_ShaderKey const &shaderKey, HdStResourceRegistrySharedPtr const &resourceRegistry)
HDST_API ID ComputeHash() const override
HDST_API int GetNumPrimitiveVertsForGeometryShader() const
bool IsPrimTypeBasisCurves() const
float GetLineWidth() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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:23
HDST_API HgiPrimitiveType GetHgiPrimitiveType() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
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:30
GLbitfield GLuint program
Definition: glcorearb.h:1931