All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
geometricShader.h
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"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hdSt/shaderCode.h"
31 #include "pxr/imaging/hdSt/resourceRegistry.h"
32 #include "pxr/imaging/hdSt/shaderKey.h"
33 #include "pxr/usd/sdf/path.h"
34 #include "pxr/imaging/garch/gl.h"
35 #include "pxr/imaging/hio/glslfx.h"
36 
37 #include <boost/scoped_ptr.hpp>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 
42 typedef boost::shared_ptr<class HdSt_GeometricShader>
43  HdSt_GeometricShaderSharedPtr;
44 
49 class HdSt_GeometricShader : public HdStShaderCode {
50 public:
52  enum class PrimitiveType {
53  PRIM_POINTS,
54  PRIM_BASIS_CURVES_LINES, // when linear (or) non-refined cubic
55  PRIM_BASIS_CURVES_LINEAR_PATCHES, // refined linear curves
56  PRIM_BASIS_CURVES_CUBIC_PATCHES, // refined cubic curves
57  PRIM_MESH_COARSE_TRIANGLES,
58  PRIM_MESH_REFINED_TRIANGLES, // e.g: loop subdiv
59  PRIM_MESH_COARSE_QUADS, // e.g: quadrangulation for ptex
60  PRIM_MESH_REFINED_QUADS, // e.g: catmark/bilinear subdiv
61  PRIM_MESH_BSPLINE, // e.g. catmark limit surface patches
62  PRIM_MESH_BOXSPLINETRIANGLE, // e.g. loop limit surface patches
63  PRIM_VOLUME // Simply draws triangles of bounding
64  // box of a volume.
65  };
66 
68  static inline bool IsPrimTypePoints (PrimitiveType primType) {
69  return primType == PrimitiveType::PRIM_POINTS;
70  }
71 
72  static inline bool IsPrimTypeBasisCurves(PrimitiveType primType) {
73  return (primType == PrimitiveType::PRIM_BASIS_CURVES_LINES ||
74  primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
75  primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES);
76  }
77 
78  static inline bool IsPrimTypeMesh(PrimitiveType primType) {
79  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
80  primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
81  primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
82  primType == PrimitiveType::PRIM_MESH_REFINED_QUADS ||
83  primType == PrimitiveType::PRIM_MESH_BSPLINE ||
84  primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE);
85  }
86 
87  static inline bool IsPrimTypeTriangles(PrimitiveType primType) {
88  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
89  primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
90  primType == PrimitiveType::PRIM_VOLUME);
91  }
92 
93  static inline bool IsPrimTypeQuads(PrimitiveType primType) {
94  return (primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
95  primType == PrimitiveType::PRIM_MESH_REFINED_QUADS);
96  }
97 
98  static inline bool IsPrimTypePatches(PrimitiveType primType) {
99  return primType == PrimitiveType::PRIM_MESH_BSPLINE ||
100  primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE ||
101  primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
102  primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES;
103  }
104 
105  HDST_API
106  HdSt_GeometricShader(std::string const &glslfxString,
107  PrimitiveType primType,
108  HdCullStyle cullStyle,
109  HdPolygonMode polygonMode,
110  bool cullingPass,
111  SdfPath const &debugId = SdfPath(),
112  float lineWidth = 0);
113 
114  HDST_API
115  virtual ~HdSt_GeometricShader();
116 
117  // HdShader overrides
118  HDST_API
119  virtual ID ComputeHash() const;
120  HDST_API
121  virtual std::string GetSource(TfToken const &shaderStageKey) const;
122  HDST_API
123  virtual void BindResources(int program,
124  HdSt_ResourceBinder const &binder,
125  HdRenderPassState const &state) override;
126 
127  HDST_API
128  virtual void UnbindResources(int program,
129  HdSt_ResourceBinder const &binder,
130  HdRenderPassState const &state) override;
131  HDST_API
132  virtual void AddBindings(HdBindingRequestVector *customBindings);
133 
135  bool IsCullingPass() const {
136  return _cullingPass;
137  }
138 
139  PrimitiveType GetPrimitiveType() const {
140  return _primType;
141  }
142 
144  inline bool IsPrimTypePoints() const {
145  return IsPrimTypePoints(_primType);
146  }
147 
148  inline bool IsPrimTypeBasisCurves() const {
149  return IsPrimTypeBasisCurves(_primType);
150  }
151 
152  inline bool IsPrimTypeMesh() const {
153  return IsPrimTypeMesh(_primType);
154  }
155 
156  inline bool IsPrimTypeTriangles() const {
157  return IsPrimTypeTriangles(_primType);
158  }
159 
160  inline bool IsPrimTypeQuads() const {
161  return IsPrimTypeQuads(_primType);
162  }
163 
164  inline bool IsPrimTypePatches() const {
165  return IsPrimTypePatches(_primType);
166  }
167 
169  HDST_API
170  GLenum GetPrimitiveMode() const;
171 
172  // Returns the primitive index size based on the primitive mode
173  // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
174  HDST_API
175  int GetPrimitiveIndexSize() const;
176 
177  // Returns the primitive index size for the geometry shader shade
178  // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
179  HDST_API
180  int GetNumPrimitiveVertsForGeometryShader() const;
181 
183  template <typename KEY>
184  static HdSt_GeometricShaderSharedPtr Create(
185  KEY const &shaderKey,
186  HdStResourceRegistrySharedPtr const &resourceRegistry) {
187 
188  HdInstance<HdSt_GeometricShaderSharedPtr> geometricShaderInstance =
189  resourceRegistry->RegisterGeometricShader(
190  HdStShaderKey::ComputeHash(shaderKey));
191 
192  if (geometricShaderInstance.IsFirstInstance()) {
193  geometricShaderInstance.SetValue(
194  HdSt_GeometricShaderSharedPtr(
195  new HdSt_GeometricShader(
196  HdStShaderKey::GetGLSLFXString(shaderKey),
197  shaderKey.GetPrimitiveType(),
198  shaderKey.GetCullStyle(),
199  shaderKey.GetPolygonMode(),
200  shaderKey.IsCullingPass(),
201  SdfPath(),
202  shaderKey.GetLineWidth())));
203  }
204  return geometricShaderInstance.GetValue();
205  }
206 
207 private:
208  PrimitiveType _primType;
209  HdCullStyle _cullStyle;
210  HdPolygonMode _polygonMode;
211  float _lineWidth;
212  // depth offset?
213 
214  boost::scoped_ptr<HioGlslfx> _glslfx;
215  bool _cullingPass;
216  ID _hash;
217 
218  // No copying
219  HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
220  HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
221 };
222 
223 
224 PXR_NAMESPACE_CLOSE_SCOPE
225 
226 #endif // PXR_IMAGING_HD_ST_GEOMETRIC_SHADER_H
virtual void BindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state)=0
Binds shader-specific resources to program XXX: this interface is meant to be used for bridging the G...
virtual void UnbindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state)=0
Unbinds shader-specific resources.
virtual std::string GetSource(TfToken const &shaderStageKey) const =0
Returns the shader source provided by this shader for shaderStageKey.
virtual void AddBindings(HdBindingRequestVector *customBindings)=0
Add custom bindings (used by codegen)
A set of rendering parameters used among render passes.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition: shaderCode.h:61
virtual ID ComputeHash() const =0
Returns the hash value of this shader.
This class is used as an interface to a shared instance in HdInstanceRegistry.