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 HDST_GEOMETRIC_SHADER_H
25 #define HDST_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/hio/glslfx.h"
35 
36 #include <boost/scoped_ptr.hpp>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 
41 typedef boost::shared_ptr<class HdSt_GeometricShader> HdSt_GeometricShaderSharedPtr;
42 
47 class HdSt_GeometricShader : public HdStShaderCode {
48 public:
50  enum class PrimitiveType {
51  PRIM_POINTS,
52  PRIM_BASIS_CURVES_LINES, // when linear (or) non-refined cubic
53  PRIM_BASIS_CURVES_LINEAR_PATCHES, // refined linear curves
54  PRIM_BASIS_CURVES_CUBIC_PATCHES, // refined cubic curves
55  PRIM_MESH_COARSE_TRIANGLES,
56  PRIM_MESH_REFINED_TRIANGLES, // e.g: loop subdiv
57  PRIM_MESH_COARSE_QUADS, // e.g: quadrangulation for ptex
58  PRIM_MESH_REFINED_QUADS, // e.g: catmark/bilinear subdiv
59  PRIM_MESH_BSPLINE, // e.g. catmark limit surface patches
60  PRIM_MESH_BOXSPLINETRIANGLE, // e.g. loop limit surface patches
61  PRIM_VOLUME // Simply draws triangles of bounding
62  // box of a volume.
63  };
64 
66  static inline bool IsPrimTypePoints (PrimitiveType primType) {
67  return primType == PrimitiveType::PRIM_POINTS;
68  }
69 
70  static inline bool IsPrimTypeBasisCurves(PrimitiveType primType) {
71  return (primType == PrimitiveType::PRIM_BASIS_CURVES_LINES ||
72  primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
73  primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES);
74  }
75 
76  static inline bool IsPrimTypeMesh(PrimitiveType primType) {
77  return (primType == PrimitiveType::PRIM_MESH_COARSE_TRIANGLES ||
78  primType == PrimitiveType::PRIM_MESH_REFINED_TRIANGLES ||
79  primType == PrimitiveType::PRIM_MESH_COARSE_QUADS ||
80  primType == PrimitiveType::PRIM_MESH_REFINED_QUADS ||
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 IsPrimTypePatches(PrimitiveType primType) {
97  return primType == PrimitiveType::PRIM_MESH_BSPLINE ||
98  primType == PrimitiveType::PRIM_MESH_BOXSPLINETRIANGLE ||
99  primType == PrimitiveType::PRIM_BASIS_CURVES_CUBIC_PATCHES ||
100  primType == PrimitiveType::PRIM_BASIS_CURVES_LINEAR_PATCHES;
101  }
102 
103  HDST_API
104  HdSt_GeometricShader(std::string const &glslfxString,
105  PrimitiveType primType,
106  HdCullStyle cullStyle,
107  HdPolygonMode polygonMode,
108  bool cullingPass,
109  SdfPath const &debugId = SdfPath(),
110  float lineWidth = 0);
111 
112  HDST_API
113  virtual ~HdSt_GeometricShader();
114 
115  // HdShader overrides
116  HDST_API
117  virtual ID ComputeHash() const;
118  HDST_API
119  virtual std::string GetSource(TfToken const &shaderStageKey) const;
120  HDST_API
121  virtual void BindResources(HdSt_ResourceBinder const &binder, int program);
122  HDST_API
123  virtual void UnbindResources(HdSt_ResourceBinder const &binder, int program);
124  HDST_API
125  virtual void AddBindings(HdBindingRequestVector *customBindings);
126 
128  bool IsCullingPass() const {
129  return _cullingPass;
130  }
131 
132  PrimitiveType GetPrimitiveType() const {
133  return _primType;
134  }
135 
137  inline bool IsPrimTypePoints() const {
138  return IsPrimTypePoints(_primType);
139  }
140 
141  inline bool IsPrimTypeBasisCurves() const {
142  return IsPrimTypeBasisCurves(_primType);
143  }
144 
145  inline bool IsPrimTypeMesh() const {
146  return IsPrimTypeMesh(_primType);
147  }
148 
149  inline bool IsPrimTypeTriangles() const {
150  return IsPrimTypeTriangles(_primType);
151  }
152 
153  inline bool IsPrimTypeQuads() const {
154  return IsPrimTypeQuads(_primType);
155  }
156 
157  inline bool IsPrimTypePatches() const {
158  return IsPrimTypePatches(_primType);
159  }
160 
162  HDST_API
163  GLenum GetPrimitiveMode() const;
164 
165  // Returns the primitive index size based on the primitive mode
166  // 3 for triangles, 4 for quads, 16 for regular b-spline patches etc.
167  HDST_API
168  int GetPrimitiveIndexSize() const;
169 
170  // Returns the primitive index size for the geometry shader shade
171  // 1 for points, 2 for lines, 3 for triangles, 4 for lines_adjacency
172  HDST_API
173  int GetNumPrimitiveVertsForGeometryShader() const;
174 
176  template <typename KEY>
177  static HdSt_GeometricShaderSharedPtr Create(
178  KEY const &shaderKey,
179  HdStResourceRegistrySharedPtr const &resourceRegistry) {
180 
182  geometricShaderInstance;
183 
184  // lookup registry
185  std::unique_lock<std::mutex> regLock =
186  resourceRegistry->RegisterGeometricShader(
187  HdStShaderKey::ComputeHash(shaderKey), &geometricShaderInstance);
188 
189  if (geometricShaderInstance.IsFirstInstance()) {
190  geometricShaderInstance.SetValue(
191  HdSt_GeometricShaderSharedPtr(
192  new HdSt_GeometricShader(
193  HdStShaderKey::GetGLSLFXString(shaderKey),
194  shaderKey.GetPrimitiveType(),
195  shaderKey.GetCullStyle(),
196  shaderKey.GetPolygonMode(),
197  shaderKey.IsCullingPass(),
198  SdfPath(),
199  shaderKey.GetLineWidth())));
200  }
201  return geometricShaderInstance.GetValue();
202  }
203 
204 private:
205  PrimitiveType _primType;
206  HdCullStyle _cullStyle;
207  HdPolygonMode _polygonMode;
208  float _lineWidth;
209  // depth offset?
210 
211  boost::scoped_ptr<HioGlslfx> _glslfx;
212  bool _cullingPass;
213  ID _hash;
214 
215  // No copying
216  HdSt_GeometricShader(const HdSt_GeometricShader &) = delete;
217  HdSt_GeometricShader &operator =(const HdSt_GeometricShader &) = delete;
218 };
219 
220 
221 PXR_NAMESPACE_CLOSE_SCOPE
222 
223 #endif // HDST_GEOMETRIC_SHADER_H
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)
virtual void UnbindResources(HdSt_ResourceBinder const &binder, int program)=0
Unbinds shader-specific resources.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual void BindResources(HdSt_ResourceBinder const &binder, int program)=0
Binds shader-specific resources to program XXX: this interface is meant to be used for bridging the G...
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:59
virtual ID ComputeHash() const =0
Returns the hash value of this shader.
This class is used as a pointer to the shared instance in HdInstanceRegistry.