Loading...
Searching...
No Matches
geometricShader.h
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"
12#include "pxr/imaging/hdSt/shaderCode.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
20PXR_NAMESPACE_OPEN_SCOPE
21
22using HdSt_GeometricShaderSharedPtr =
23 std::shared_ptr<class HdSt_GeometricShader>;
24using HdStResourceRegistrySharedPtr =
25 std::shared_ptr<class HdStResourceRegistry>;
26struct HdSt_ShaderKey;
27class HioGlslfx;
28
43class HdSt_GeometricShader : public HdStShaderCode {
44public:
46 enum class PrimitiveType {
47 PRIM_POINTS,
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
51 PRIM_MESH_COARSE_TRIANGLES,
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
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 ||
70 primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
71 primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES);
72 }
73
74 static inline bool IsPrimTypeMesh(PrimitiveType primType) {
75 return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
76 primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
77 primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
78 primType == PrimitiveType::PRIM_MESH_REFINED_QUADS ||
79 primType == PrimitiveType::PRIM_MESH_COARSE_TRIQUADS ||
80 primType == PrimitiveType::PRIM_MESH_REFINED_TRIQUADS ||
81 primType == PrimitiveType::PRIM_MESH_BSPLINE ||
82 primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE);
83 }
84
85 static inline bool IsPrimTypeTriangles(PrimitiveType primType) {
86 return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
87 primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
88 primType == PrimitiveType::PRIM_VOLUME);
89 }
90
91 static inline bool IsPrimTypeQuads(PrimitiveType primType) {
92 return (primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
93 primType == PrimitiveType::PRIM_MESH_REFINED_QUADS);
94 }
95
96 static inline bool IsPrimTypeTriQuads(PrimitiveType primType) {
97 return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIQUADS ||
98 primType == PrimitiveType::PRIM_MESH_REFINED_TRIQUADS);
99 }
100
101 static inline bool IsPrimTypeRefinedMesh(PrimitiveType primType) {
102 return (primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
103 primType == PrimitiveType::PRIM_MESH_REFINED_QUADS ||
104 primType == PrimitiveType::PRIM_MESH_REFINED_TRIQUADS ||
105 primType == PrimitiveType::PRIM_MESH_BSPLINE ||
106 primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE);
107 }
108
109 static inline bool IsPrimTypePatches(PrimitiveType primType) {
110 return primType == PrimitiveType::PRIM_MESH_BSPLINE ||
111 primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE ||
112 primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
113 primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES;
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 {
122 PATCH_COARSE_TRIANGLES,
123 PATCH_REFINED_TRIANGLES,
124 PATCH_COARSE_QUADS,
125 PATCH_REFINED_QUADS,
126 PATCH_BSPLINE,
127 PATCH_BOXSPLINETRIANGLE,
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 uint32_t vertexCountFallback = 0,
145 TfToken const &depthQualifier = TfToken(),
146 bool useHardwareClipPlanes = true);
147
148 HDST_API
149 ~HdSt_GeometricShader() override;
150
151 // HdShader overrides
152 HDST_API
153 ID ComputeHash() const override;
154 HDST_API
155 std::string GetSource(TfToken const &shaderStageKey) const override;
156 HDST_API
157 void BindResources(int program,
158 HdSt_ResourceBinder const &binder) override;
159
160 HDST_API
161 void UnbindResources(int program,
162 HdSt_ResourceBinder const &binder) override;
163 HDST_API
164 void AddBindings(HdStBindingRequestVector *customBindings) override;
165
167 bool IsFrustumCullingPass() const {
168 return _frustumCullingPass;
169 }
170
171 PrimitiveType GetPrimitiveType() const {
172 return _primType;
173 }
174
175 bool GetUseMetalTessellation() const {
176 return _useMetalTessellation;
177 }
178
179 float GetLineWidth() const {
180 return _lineWidth;
181 }
182
183 HdPolygonMode GetPolygonMode() const {
184 return _polygonMode;
185 }
186
187 uint32_t GetVertexCountFallback() const {
188 return _vertexCountFallback;
189 }
190
191 const TfToken GetDepthQualifier() const {
192 return _depthQualifier;
193 }
194
195 bool GetUseHardwareClipPlanes() const {
196 return _useHardwareClipPlanes;
197 }
198
200 bool IsPrimTypePoints() const {
201 return IsPrimTypePoints(_primType);
202 }
203
204 bool IsPrimTypeBasisCurves() const {
205 return IsPrimTypeBasisCurves(_primType);
206 }
207
208 bool IsPrimTypeMesh() const {
209 return IsPrimTypeMesh(_primType);
210 }
211
212 bool IsPrimTypeTriangles() const {
213 return IsPrimTypeTriangles(_primType);
214 }
215
216 bool IsPrimTypeQuads() const {
217 return IsPrimTypeQuads(_primType);
218 }
219
220 bool IsPrimTypeTriQuads() const {
221 return IsPrimTypeTriQuads(_primType);
222 }
223
224 bool IsPrimTypeRefinedMesh() const {
225 return IsPrimTypeRefinedMesh(_primType);
226 }
227
228 bool IsPrimTypePatches() const {
229 return IsPrimTypePatches(_primType);
230 }
231
232 bool IsPrimTypeCompute() const {
233 return IsPrimTypeCompute(_primType);
234 }
235
236 FvarPatchType GetFvarPatchType() const {
237 return _fvarPatchType;
238 }
239
240 // Returns the primitive index size based on the primitive type
241 // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
242 HDST_API
243 int GetPrimitiveIndexSize() const;
244
245 // Returns the number of vertices output for patch evaluation,
246 // i.e. the number of tessellation control shader invocations.
247 HDST_API
248 int GetNumPatchEvalVerts() const;
249
250 // Returns the primitive index size for the geometry shader shade
251 // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
252 HDST_API
253 int GetNumPrimitiveVertsForGeometryShader() const;
254
255 // Returns the HgiPrimitiveType for the primitive type.
256 HDST_API
257 HgiPrimitiveType GetHgiPrimitiveType() const;
258
259 // Resolve the cull mode from the cull style in the render state.
260 HDST_API
261 HgiCullMode ResolveCullMode(HdCullStyle const renderStateCullStyle) const;
262
263 // Factory for convenience.
264 HDST_API
265 static HdSt_GeometricShaderSharedPtr Create(
266 HdSt_ShaderKey const &shaderKey,
267 HdStResourceRegistrySharedPtr const &resourceRegistry);
268
269private:
270 PrimitiveType _primType;
271 HdCullStyle _cullStyle;
272 bool _useHardwareFaceCulling;
273 bool _hasMirroredTransform;
274 bool _doubleSided;
275 bool _useMetalTessellation;
276 HdPolygonMode _polygonMode;
277 float _lineWidth;
278 uint32_t _vertexCountFallback;
279 const TfToken _depthQualifier;
280 bool _useHardwareClipPlanes;
281
282 std::unique_ptr<HioGlslfx> _glslfx;
283 bool _frustumCullingPass;
284 FvarPatchType _fvarPatchType;
285 ID _hash;
286
287 // No copying
288 HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
289 HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
290
291 HioGlslfx const * _GetGlslfx() const override;
292};
293
294
295PXR_NAMESPACE_CLOSE_SCOPE
296
297#endif // PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition shaderCode.h:59
virtual void BindResources(int program, HdSt_ResourceBinder const &binder)=0
Binds shader-specific resources to program XXX: this interface is meant to be used for bridging the G...
virtual void AddBindings(HdStBindingRequestVector *customBindings)=0
Add custom bindings (used by codegen)
virtual std::string GetSource(TfToken const &shaderStageKey) const =0
Returns the shader source provided by this shader for shaderStageKey.
virtual void UnbindResources(int program, HdSt_ResourceBinder const &binder)=0
Unbinds shader-specific resources.
virtual ID ComputeHash() const =0
Returns the hash value of the shader code and configuration.
A class representing the config and shader source of a glslfx file.
Definition glslfx.h:134
A path value used to locate objects in layers or scenegraphs.
Definition path.h:281
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81