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.
125  ///
127  /// Name by which the texture will be accessed, i.e., the name
128  /// of the accesor for thexture will be HdGet_name(...).
129  ///
131  /// Equal to handle->GetTextureObject()->GetTextureType().
132  /// Saved here for convenience (note that name and type
133  /// completely determine the creation of the texture accesor
134  /// HdGet_name(...)).
135  ///
137  /// The texture.
139 
140  /// A hash unique to the corresponding asset; used to
141  /// split draw batches when not using bindless textures.
142  size_t hash;
143  };
144  using NamedTextureHandleVector = std::vector<NamedTextureHandle>;
145 
146  /// Textures that need to be bound for this shader.
147  ///
148  HDST_API
149  virtual NamedTextureHandleVector const & GetNamedTextureHandles() const;
150 
151  /// @}
152 
153  // XXX: Should be pure-virtual
154  /// Returns a buffer which stores parameter fallback values and texture
155  /// handles.
156  HDST_API
157  virtual HdBufferArrayRangeSharedPtr const& GetShaderData() const;
158 
159  /// Binds shader-specific resources to \a program
160  /// XXX: this interface is meant to be used for bridging
161  /// the GlfSimpleLightingContext mechanism, and not for generic use-cases.
162  virtual void BindResources(int program,
163  HdSt_ResourceBinder const &binder) = 0;
164 
165  /// Unbinds shader-specific resources.
166  virtual void UnbindResources(int program,
167  HdSt_ResourceBinder const &binder) = 0;
168 
169  /// Add custom bindings (used by codegen)
170  virtual void AddBindings(HdStBindingRequestVector* customBindings) = 0;
171 
172  /// Material tags can be set in the meta-data of a glslfx file to control
173  /// what rprim collection that prims using this shader should go into.
174  /// E.g. We can use it to split opaque and translucent prims into different
175  /// collections. When no material tags are specified in the shader, a empty
176  /// token is returned.
177  HDST_API
178  virtual TfToken GetMaterialTag() const;
179 
180  /// \class ResourceContext
181  ///
182  /// The context available in implementations of
183  /// AddResourcesFromTextures.
185  public:
186  HDST_API
189 
190  HDST_API
191  void AddSources(HdBufferArrayRangeSharedPtr const &range,
193 
194  HDST_API
196  HdStComputationSharedPtr const &computation,
197  HdStComputeQueue const queue);
198 
200  return _registry;
201  }
202 
203  private:
204  friend class HdStResourceRegistry;
206  HdStResourceRegistry *_registry;
207  };
208 
209  /// This function is called after textures have been allocated and
210  /// loaded to add buffer sources and computations to the resource
211  /// registry that require texture meta data not available until
212  /// the texture is allocated or loaded. For example, the OpenGl
213  /// texture sampler handle (in the bindless case) is not available
214  /// until after the texture commit phase.
215  ///
216  HDST_API
217  virtual void AddResourcesFromTextures(ResourceContext &ctx) const;
218 
219 private:
220 
221  // No copying
222  HdStShaderCode(const HdStShaderCode &) = delete;
223  HdStShaderCode &operator =(const HdStShaderCode &) = delete;
224 
225  // Returns the HioGlslfx instance used to configure this shader.
226  // This can return nullptr for shaders without a GLSLFX instance.
227  virtual HioGlslfx const * _GetGlslfx() const;
228 };
229 
230 
232 
233 #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< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:23
HdStResourceRegistry * GetResourceRegistry() const
Definition: shaderCode.h:199
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)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::vector< NamedTextureHandle > NamedTextureHandleVector
Definition: shaderCode.h:144
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:30
HDST_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtrVector &&sources)
virtual void BindResources(int program, HdSt_ResourceBinder const &binder)=0
HDST_API HdStShaderCode()
HdStTextureHandleSharedPtr handle
The texture.
Definition: shaderCode.h:138
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
GLbitfield GLuint program
Definition: glcorearb.h:1931
std::vector< class HdSt_MaterialParam > HdSt_MaterialParamVector