HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderCode.h
Go to the documentation of this file.
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_SHADER_CODE_H
8 #define PXR_IMAGING_HD_ST_SHADER_CODE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
13 #include "pxr/imaging/hdSt/enums.h"
14 #include "pxr/imaging/hd/version.h"
15 
16 #include "pxr/usd/sdf/path.h"
17 
18 #include "pxr/base/tf/token.h"
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
25 
26 
27 using HdStBindingRequestVector = std::vector<class HdStBindingRequest>;
28 
30  std::shared_ptr<class HdStShaderCode>;
32  std::vector<HdStShaderCodeSharedPtr>;
33 
35  std::vector<class HdSt_MaterialParam>;
37  std::shared_ptr<class HdBufferSource>;
39  std::vector<HdBufferSourceSharedPtr>;
41  std::shared_ptr<class HdBufferArrayRange>;
43  std::shared_ptr<class HdStTextureHandle>;
45  std::shared_ptr<class HdStComputation>;
46 
47 class HioGlslfx;
48 class HdSt_ResourceBinder;
50 
51 /// \class HdStShaderCode
52 ///
53 /// A base class representing the implementation (code) of a shader,
54 /// used in conjunction with HdRenderPass.
55 ///
56 /// This interface provides a simple way for clients to affect the
57 /// composition of shading programs used for a render pass.
58 class HdStShaderCode : public std::enable_shared_from_this<HdStShaderCode>
59 {
60 public:
61  typedef size_t ID;
62 
63  HDST_API
65  HDST_API
66  virtual ~HdStShaderCode();
67 
68  /// Returns the hash value of the shader code and configuration.
69  ///
70  /// It is computed from the the GLSL code as well as the resource
71  /// signature of the shader (as determined from its parameters).
72  /// If two shaders have the same hash, the GLSL code as expanded
73  /// by codegen should also be the same.
74  ///
75  virtual ID ComputeHash() const = 0;
76 
77  /// Returns the combined hash values of multiple shaders.
78  HDST_API
80 
81  /// Returns the hash value of the paths of the texture prims
82  /// consumed by this shader.
83  ///
84  /// Unless textures are bindless, shaders using different textures
85  /// cannot be used in the same draw batch. Since textures can be
86  /// animated, it can happen that two texture prims use the same
87  /// texture at some time but different textures at other times. To
88  /// avoid re-computing the draw batches over time, we use the this
89  /// hash when grouping the draw batches.
90  ///
91  HDST_API
92  virtual ID ComputeTextureSourceHash() const;
93 
94  /// Returns the shader source provided by this shader
95  /// for \a shaderStageKey
96  virtual std::string GetSource(TfToken const &shaderStageKey) const = 0;
97 
98  /// Returns the resource layout for the shader stages specified by
99  /// \a shaderStageKeys. This is initialized using the shader's
100  /// HioGlslfx configuration.
101  HDST_API
102  VtDictionary GetLayout(TfTokenVector const &shaderStageKeys) const;
103 
104  // XXX: Should be pure-virtual
105  /// Returns the shader parameters for this shader.
106  HDST_API
107  virtual HdSt_MaterialParamVector const& GetParams() const;
108 
109  /// Returns whether primvar filtering is enabled for this shader.
110  HDST_API
111  virtual bool IsEnabledPrimvarFiltering() const;
112 
113  /// Returns the names of primvar that are used by this shader.
114  HDST_API
115  virtual TfTokenVector const& GetPrimvarNames() const;
116 
117  /// @}
118 
119  ///
120  /// \name Texture system
121  /// @{
122 
123  /// Information necessary to bind textures and create accessor
124  /// for the texture. Can be used for a single texture, or an array of
125  /// textures.
126  ///
128  /// Name by which the texture(s) will be accessed, i.e., the name
129  /// of the accesor for the texture(s) will be HdGet_name(...).
130  ///
132  /// Equal to handle->GetTextureObject()->GetTextureType().
133  /// Saved here for convenience (note that name and type
134  /// completely determine the creation of the texture accesor
135  /// HdGet_name(...)).
136  ///
138  /// The texture(s). Multiple handles indicate an array of textures.
139  std::vector<HdStTextureHandleSharedPtr> handles;
140 
141  /// A hash unique to the corresponding asset; used to
142  /// split draw batches when not using bindless textures.
143  size_t hash;
144  };
145  using NamedTextureHandleVector = std::vector<NamedTextureHandle>;
146 
147  /// Textures that need to be bound for this shader.
148  ///
149  HDST_API
150  virtual NamedTextureHandleVector const & GetNamedTextureHandles() const;
151 
152  /// @}
153 
154  // XXX: Should be pure-virtual
155  /// Returns a buffer which stores parameter fallback values and texture
156  /// handles.
157  HDST_API
158  virtual HdBufferArrayRangeSharedPtr const& GetShaderData() const;
159 
160  /// Binds shader-specific resources to \a program
161  /// XXX: this interface is meant to be used for bridging
162  /// the GlfSimpleLightingContext mechanism, and not for generic use-cases.
163  virtual void BindResources(int program,
164  HdSt_ResourceBinder const &binder) = 0;
165 
166  /// Unbinds shader-specific resources.
167  virtual void UnbindResources(int program,
168  HdSt_ResourceBinder const &binder) = 0;
169 
170  /// Add custom bindings (used by codegen)
171  virtual void AddBindings(HdStBindingRequestVector* customBindings) = 0;
172 
173  /// Material tags can be set in the meta-data of a glslfx file to control
174  /// what rprim collection that prims using this shader should go into.
175  /// E.g. We can use it to split opaque and translucent prims into different
176  /// collections. When no material tags are specified in the shader, a empty
177  /// token is returned.
178  HDST_API
179  virtual TfToken GetMaterialTag() const;
180 
181  /// \class ResourceContext
182  ///
183  /// The context available in implementations of
184  /// AddResourcesFromTextures.
186  public:
187  HDST_API
190 
191  HDST_API
192  void AddSources(HdBufferArrayRangeSharedPtr const &range,
194 
195  HDST_API
197  HdStComputationSharedPtr const &computation,
198  HdStComputeQueue const queue);
199 
201  return _registry;
202  }
203 
204  private:
205  friend class HdStResourceRegistry;
207  HdStResourceRegistry *_registry;
208  };
209 
210  /// This function is called after textures have been allocated and
211  /// loaded to add buffer sources and computations to the resource
212  /// registry that require texture meta data not available until
213  /// the texture is allocated or loaded. For example, the OpenGl
214  /// texture sampler handle (in the bindless case) is not available
215  /// until after the texture commit phase.
216  ///
217  HDST_API
218  virtual void AddResourcesFromTextures(ResourceContext &ctx) const;
219 
220 private:
221 
222  // No copying
223  HdStShaderCode(const HdStShaderCode &) = delete;
224  HdStShaderCode &operator =(const HdStShaderCode &) = delete;
225 
226  // Returns the HioGlslfx instance used to configure this shader.
227  // This can return nullptr for shaders without a GLSLFX instance.
228  virtual HioGlslfx const * _GetGlslfx() const;
229 };
230 
231 
233 
234 #endif //HDST_SHADER_H
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
GLenum GLint * range
Definition: glcorearb.h:1925
virtual HDST_API HdBufferArrayRangeSharedPtr const & GetShaderData() const
std::vector< HdStTextureHandleSharedPtr > handles
The texture(s). Multiple handles indicate an array of textures.
Definition: shaderCode.h:139
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::vector< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:23
HdStResourceRegistry * GetResourceRegistry() const
Definition: shaderCode.h:200
virtual HDST_API HdSt_MaterialParamVector const & GetParams() const
Returns the shader parameters for this shader.
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:27
virtual HDST_API TfTokenVector const & GetPrimvarNames() const
Returns the names of primvar that are used by this shader.
virtual void UnbindResources(int program, HdSt_ResourceBinder const &binder)=0
Unbinds shader-specific resources.
HDST_API VtDictionary GetLayout(TfTokenVector const &shaderStageKeys) const
virtual HDST_API TfToken GetMaterialTag() const
virtual HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const
GLsizei GLenum * sources
Definition: glcorearb.h:2542
Definition: token.h:70
HDST_API void AddSource(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtr const &source)
virtual std::string GetSource(TfToken const &shaderStageKey) const =0
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
std::shared_ptr< class HdStComputation > HdStComputationSharedPtr
Definition: computation.h:24
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the queue
Definition: thread.h:632
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
GLsizei GLsizei GLuint * shaders
Definition: glcorearb.h:797
HdStComputeQueue
virtual HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const
virtual HDST_API ID ComputeTextureSourceHash() const
virtual HDST_API ~HdStShaderCode()
HDST_API void AddComputation(HdBufferArrayRangeSharedPtr const &range, HdStComputationSharedPtr const &computation, HdStComputeQueue const queue)
std::vector< NamedTextureHandle > NamedTextureHandleVector
Definition: shaderCode.h:145
virtual ID ComputeHash() const =0
std::shared_ptr< class HdStTextureHandle > HdStTextureHandleSharedPtr
#define HDST_API
Definition: api.h:23
virtual HDST_API bool IsEnabledPrimvarFiltering() const
Returns whether primvar filtering is enabled for this shader.
virtual void AddBindings(HdStBindingRequestVector *customBindings)=0
Add custom bindings (used by codegen)
std::vector< HdStShaderCodeSharedPtr > HdStShaderCodeSharedPtrVector
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
HdStTextureType
Definition: enums.h:32
HDST_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtrVector &&sources)
virtual void BindResources(int program, HdSt_ResourceBinder const &binder)=0
HDST_API HdStShaderCode()
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
GLbitfield GLuint program
Definition: glcorearb.h:1931
std::vector< class HdSt_MaterialParam > HdSt_MaterialParamVector