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"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/enums.h"
31 
32 #include "pxr/usd/sdf/path.h"
33 
34 #include "pxr/base/tf/token.h"
35 
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
41 
42 
43 using HdBindingRequestVector = std::vector<class HdBindingRequest>;
44 
46  std::shared_ptr<class HdStShaderCode>;
48  std::vector<HdStShaderCodeSharedPtr>;
49 
51  std::shared_ptr<class HdStTextureResourceHandle>;
53  std::vector<class HdSt_MaterialParam>;
55  std::shared_ptr<class HdBufferSource>;
57  std::vector<HdBufferSourceSharedPtr>;
59  std::shared_ptr<class HdBufferArrayRange>;
61  std::shared_ptr<class HdStTextureHandle>;
63  std::shared_ptr<class HdComputation>;
64 
65 class HdRenderPassState;
66 class HdSt_ResourceBinder;
68 
69 /// \class HdStShaderCode
70 ///
71 /// A base class representing the implementation (code) of a shader,
72 /// used in conjunction with HdRenderPass.
73 ///
74 /// This interface provides a simple way for clients to affect the
75 /// composition of shading programs used for a render pass.
76 class HdStShaderCode : public std::enable_shared_from_this<HdStShaderCode>
77 {
78 public:
79  typedef size_t ID;
80 
81  HDST_API
83  HDST_API
84  virtual ~HdStShaderCode();
85 
86  /// Returns the hash value of the shader code and configuration.
87  ///
88  /// It is computed from the the GLSL code as well as the resource
89  /// signature of the shader (as determined from its parameters).
90  /// If two shaders have the same hash, the GLSL code as expanded
91  /// by codegen should also be the same.
92  ///
93  virtual ID ComputeHash() const = 0;
94 
95  /// Returns the combined hash values of multiple shaders.
96  HDST_API
98 
99  /// Returns the hash value of the paths of the texture prims
100  /// consumed by this shader.
101  ///
102  /// Unless textures are bindless, shaders using different textures
103  /// cannot be used in the same draw batch. Since textures can be
104  /// animated, it can happen that two texture prims use the same
105  /// texture at some time but different textures at other times. To
106  /// avoid re-computing the draw batches over time, we use the this
107  /// hash when grouping the draw batches.
108  ///
109  virtual ID ComputeTextureSourceHash() const;
110 
111  /// Returns the shader source provided by this shader
112  /// for \a shaderStageKey
113  virtual std::string GetSource(TfToken const &shaderStageKey) const = 0;
114 
115  // XXX: Should be pure-virtual
116  /// Returns the shader parameters for this shader.
117  HDST_API
118  virtual HdSt_MaterialParamVector const& GetParams() const;
119 
120  /// Returns whether primvar filtering is enabled for this shader.
121  HDST_API
122  virtual bool IsEnabledPrimvarFiltering() const;
123 
124  /// Returns the names of primvar that are used by this shader.
125  HDST_API
126  virtual TfTokenVector const& GetPrimvarNames() const;
127 
128  ///
129  /// \name Old texture system
130  /// @{
137  int type;
139  };
140  typedef std::vector<TextureDescriptor> TextureDescriptorVector;
141 
142  // XXX: DOC
143  HDST_API
144  virtual TextureDescriptorVector GetTextures() const;
145 
146  /// @}
147 
148  ///
149  /// \name New texture system
150  /// @{
151 
152  /// The old texture system relied on the scene delegate to load
153  /// textures. This system will soon be deprecated (including
154  /// HdTextureResource and HdSceneDelegate::GetTextureResource) in
155  /// favor of a new system where the render delegate is loading
156  /// textures (by allocating HdStTextureHandle).
157 
158  /// During the transition period, both, HdTextureResource and
159  /// HdStTextureHandle can be used at the same time by populating
160  /// both GetTextures() and GetNamedTextureHandles() - in
161  /// particular, we can use the new texture system for texture
162  /// types that it already supports and keep using the old
163  /// texture system for the unsupported texture types.
164 
165  /// Information necessary to bind textures and create accessor
166  /// for the texture.
167  ///
169  /// Name by which the texture will be accessed, i.e., the name
170  /// of the accesor for thexture will be HdGet_name(...).
171  ///
173  /// Equal to handle->GetTextureObject()->GetTextureType().
174  /// Saved here for convenience (note that name and type
175  /// completely determine the creation of the texture accesor
176  /// HdGet_name(...)).
177  ///
179  /// The texture.
181 
182  /// The path of the corresponding texture prim - used to
183  /// compute the hash for draw batches when not using bindless textures.
185  };
186  using NamedTextureHandleVector = std::vector<NamedTextureHandle>;
187 
188  /// Textures that need to be bound for this shader.
189  ///
190  HDST_API
191  virtual NamedTextureHandleVector const & GetNamedTextureHandles() const;
192 
193  /// @}
194 
195  // XXX: Should be pure-virtual
196  /// Returns a buffer which stores parameter fallback values and texture
197  /// handles.
198  HDST_API
199  virtual HdBufferArrayRangeSharedPtr const& GetShaderData() const;
200 
201  /// Binds shader-specific resources to \a program
202  /// XXX: this interface is meant to be used for bridging
203  /// the GlfSimpleLightingContext mechanism, and not for generic use-cases.
204  virtual void BindResources(int program,
205  HdSt_ResourceBinder const &binder,
206  HdRenderPassState const &state) = 0;
207 
208  /// Unbinds shader-specific resources.
209  virtual void UnbindResources(int program,
210  HdSt_ResourceBinder const &binder,
211  HdRenderPassState const &state) = 0;
212 
213  /// Add custom bindings (used by codegen)
214  virtual void AddBindings(HdBindingRequestVector* customBindings) = 0;
215 
216  /// Material tags can be set in the meta-data of a glslfx file to control
217  /// what rprim collection that prims using this shader should go into.
218  /// E.g. We can use it to split opaque and translucent prims into different
219  /// collections. When no material tags are specified in the shader, a empty
220  /// token is returned.
221  HDST_API
222  virtual TfToken GetMaterialTag() const;
223 
224  /// \class ResourceContext
225  ///
226  /// The context available in implementations of
227  /// AddResourcesFromTextures.
229  public:
230  HDST_API
233 
234  HDST_API
235  void AddSources(HdBufferArrayRangeSharedPtr const &range,
237 
238  HDST_API
240  HdComputationSharedPtr const &computation);
241 
242  private:
243  friend class HdStResourceRegistry;
245  HdStResourceRegistry *_registry;
246  };
247 
248  /// This function is called after textures have been allocated and
249  /// loaded to add buffer sources and computations to the resource
250  /// registry that require texture meta data not available until
251  /// the texture is allocated or loaded. For example, the OpenGl
252  /// texture sampler handle (in the bindless case) is not available
253  /// until after the texture commit phase.
254  ///
255  HDST_API
256  virtual void AddResourcesFromTextures(ResourceContext &ctx) const;
257 
258 private:
259 
260  // No copying
261  HdStShaderCode(const HdStShaderCode &) = delete;
262  HdStShaderCode &operator =(const HdStShaderCode &) = delete;
263 };
264 
265 
267 
268 #endif //HDST_SHADER_H
GLenum GLint * range
Definition: glew.h:3500
GLsizei GLenum * sources
Definition: glew.h:3954
virtual void UnbindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state)=0
Unbinds shader-specific resources.
virtual HDST_API HdBufferArrayRangeSharedPtr const & GetShaderData() const
HdStTextureResourceHandleSharedPtr handle
Definition: shaderCode.h:133
std::vector< TextureDescriptor > TextureDescriptorVector
Definition: shaderCode.h:140
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
virtual HDST_API HdSt_MaterialParamVector const & GetParams() const
Returns the shader parameters for this shader.
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:44
virtual void BindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state)=0
virtual HDST_API TfTokenVector const & GetPrimvarNames() const
Returns the names of primvar that are used by this shader.
virtual ID ComputeTextureSourceHash() const
virtual HDST_API TfToken GetMaterialTag() const
virtual HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const
Definition: token.h:87
std::shared_ptr< class HdStTextureResourceHandle > HdStTextureResourceHandleSharedPtr
Definition: material.h:44
HDST_API void AddSource(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtr const &source)
virtual std::string GetSource(TfToken const &shaderStageKey) const =0
GLsizei GLsizei GLuint * shaders
Definition: glew.h:1827
HdTextureType
Definition: enums.h:221
std::shared_ptr< class HdComputation > HdComputationSharedPtr
Definition: computation.h:41
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
Definition: path.h:288
virtual HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const
virtual HDST_API ~HdStShaderCode()
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
std::vector< NamedTextureHandle > NamedTextureHandleVector
Definition: shaderCode.h:186
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.
std::vector< HdStShaderCodeSharedPtr > HdStShaderCodeSharedPtrVector
virtual void AddBindings(HdBindingRequestVector *customBindings)=0
Add custom bindings (used by codegen)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
HDST_API void AddSources(HdBufferArrayRangeSharedPtr const &range, HdBufferSourceSharedPtrVector &&sources)
GLuint program
Definition: glew.h:5881
HDST_API HdStShaderCode()
HDST_API void AddComputation(HdBufferArrayRangeSharedPtr const &range, HdComputationSharedPtr const &computation)
HdStTextureHandleSharedPtr handle
The texture.
Definition: shaderCode.h:180
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
virtual HDST_API TextureDescriptorVector GetTextures() const
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
Definition: drawItem.h:34
std::vector< class HdSt_MaterialParam > HdSt_MaterialParamVector
std::vector< class HdBindingRequest > HdBindingRequestVector
Definition: binding.h:39