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
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
164 bool IsFrustumCullingPass() const {
165 return _frustumCullingPass;
166 }
167
168 PrimitiveType GetPrimitiveType() const {
169 return _primType;
170 }
171
172 bool GetUseMetalTessellation() const {
173 return _useMetalTessellation;
174 }
175
176 float GetLineWidth() const {
177 return _lineWidth;
178 }
179
180 HdPolygonMode GetPolygonMode() const {
181 return _polygonMode;
182 }
183
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
221 FvarPatchType GetFvarPatchType() const {
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
238 int GetNumPrimitiveVertsForGeometryShader() const;
239
240 // Returns the HgiPrimitiveType for the primitive type.
241 HDST_API
242 HgiPrimitiveType GetHgiPrimitiveType() const;
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
250 static HdSt_GeometricShaderSharedPtr Create(
251 HdSt_ShaderKey const &shaderKey,
252 HdStResourceRegistrySharedPtr const &resourceRegistry);
253
254private:
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
277PXR_NAMESPACE_CLOSE_SCOPE
278
279#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:274
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71