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