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
146 HDST_API
147 ~HdSt_GeometricShader() override;
148
149 // HdShader overrides
150 HDST_API
151 ID ComputeHash() const override;
152 HDST_API
153 std::string GetSource(TfToken const &shaderStageKey) const override;
154 HDST_API
155 void BindResources(int program,
156 HdSt_ResourceBinder const &binder) override;
157
158 HDST_API
159 void UnbindResources(int program,
160 HdSt_ResourceBinder const &binder) override;
161 HDST_API
162 void AddBindings(HdStBindingRequestVector *customBindings) override;
163
165 bool IsFrustumCullingPass() const {
166 return _frustumCullingPass;
167 }
168
169 PrimitiveType GetPrimitiveType() const {
170 return _primType;
171 }
172
173 bool GetUseMetalTessellation() const {
174 return _useMetalTessellation;
175 }
176
177 float GetLineWidth() const {
178 return _lineWidth;
179 }
180
181 HdPolygonMode GetPolygonMode() const {
182 return _polygonMode;
183 }
184
185 uint32_t GetVertexCountFallback() const {
186 return _vertexCountFallback;
187 }
188
190 bool IsPrimTypePoints() const {
191 return IsPrimTypePoints(_primType);
192 }
193
194 bool IsPrimTypeBasisCurves() const {
195 return IsPrimTypeBasisCurves(_primType);
196 }
197
198 bool IsPrimTypeMesh() const {
199 return IsPrimTypeMesh(_primType);
200 }
201
202 bool IsPrimTypeTriangles() const {
203 return IsPrimTypeTriangles(_primType);
204 }
205
206 bool IsPrimTypeQuads() const {
207 return IsPrimTypeQuads(_primType);
208 }
209
210 bool IsPrimTypeTriQuads() const {
211 return IsPrimTypeTriQuads(_primType);
212 }
213
214 bool IsPrimTypeRefinedMesh() const {
215 return IsPrimTypeRefinedMesh(_primType);
216 }
217
218 bool IsPrimTypePatches() const {
219 return IsPrimTypePatches(_primType);
220 }
221
222 bool IsPrimTypeCompute() const {
223 return IsPrimTypeCompute(_primType);
224 }
225
226 FvarPatchType GetFvarPatchType() const {
227 return _fvarPatchType;
228 }
229
230 // Returns the primitive index size based on the primitive type
231 // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
232 HDST_API
233 int GetPrimitiveIndexSize() const;
234
235 // Returns the number of vertices output for patch evaluation,
236 // i.e. the number of tessellation control shader invocations.
237 HDST_API
238 int GetNumPatchEvalVerts() const;
239
240 // Returns the primitive index size for the geometry shader shade
241 // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
242 HDST_API
243 int GetNumPrimitiveVertsForGeometryShader() const;
244
245 // Returns the HgiPrimitiveType for the primitive type.
246 HDST_API
247 HgiPrimitiveType GetHgiPrimitiveType() const;
248
249 // Resolve the cull mode from the cull style in the render state.
250 HDST_API
251 HgiCullMode ResolveCullMode(HdCullStyle const renderStateCullStyle) const;
252
253 // Factory for convenience.
254 HDST_API
255 static HdSt_GeometricShaderSharedPtr Create(
256 HdSt_ShaderKey const &shaderKey,
257 HdStResourceRegistrySharedPtr const &resourceRegistry);
258
259private:
260 PrimitiveType _primType;
261 HdCullStyle _cullStyle;
262 bool _useHardwareFaceCulling;
263 bool _hasMirroredTransform;
264 bool _doubleSided;
265 bool _useMetalTessellation;
266 HdPolygonMode _polygonMode;
267 float _lineWidth;
268 uint32_t _vertexCountFallback;
269
270 std::unique_ptr<HioGlslfx> _glslfx;
271 bool _frustumCullingPass;
272 FvarPatchType _fvarPatchType;
273 ID _hash;
274
275 // No copying
276 HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
277 HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
278
279 HioGlslfx const * _GetGlslfx() const override;
280};
281
282
283PXR_NAMESPACE_CLOSE_SCOPE
284
285#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:71