shaderCode.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 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/hdSt/resourceRegistry.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 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
44 using HdBindingRequestVector = std::vector<class HdBindingRequest>;
45 
46 using HdStShaderCodeSharedPtr =
47  std::shared_ptr<class HdStShaderCode>;
48 using HdStShaderCodeSharedPtrVector =
49  std::vector<HdStShaderCodeSharedPtr>;
50 
51 using HdSt_MaterialParamVector =
52  std::vector<class HdSt_MaterialParam>;
53 using HdBufferSourceSharedPtr =
54  std::shared_ptr<class HdBufferSource>;
55 using HdBufferSourceSharedPtrVector =
56  std::vector<HdBufferSourceSharedPtr>;
57 using HdBufferArrayRangeSharedPtr =
58  std::shared_ptr<class HdBufferArrayRange>;
59 using HdStTextureHandleSharedPtr =
60  std::shared_ptr<class HdStTextureHandle>;
61 using HdComputationSharedPtr =
62  std::shared_ptr<class HdComputation>;
63 
64 class HioGlslfx;
65 class HdSt_ResourceBinder;
67 
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 
92  virtual ID ComputeHash() const = 0;
93 
95  HDST_API
96  static ID ComputeHash(HdStShaderCodeSharedPtrVector const &shaders);
97 
108  virtual ID ComputeTextureSourceHash() const;
109 
112  virtual std::string GetSource(TfToken const &shaderStageKey) const = 0;
113 
117  HDST_API
118  VtDictionary GetLayout(TfTokenVector const &shaderStageKeys) const;
119 
120  // XXX: Should be pure-virtual
122  HDST_API
123  virtual HdSt_MaterialParamVector const& GetParams() const;
124 
126  HDST_API
127  virtual bool IsEnabledPrimvarFiltering() const;
128 
130  HDST_API
131  virtual TfTokenVector const& GetPrimvarNames() const;
132 
134 
138 
152  HdTextureType type;
154  HdStTextureHandleSharedPtr handle;
155 
158  size_t hash;
159  };
160  using NamedTextureHandleVector = std::vector<NamedTextureHandle>;
161 
164  HDST_API
165  virtual NamedTextureHandleVector const & GetNamedTextureHandles() const;
166 
168 
169  // XXX: Should be pure-virtual
172  HDST_API
173  virtual HdBufferArrayRangeSharedPtr const& GetShaderData() const;
174 
178  virtual void BindResources(int program,
179  HdSt_ResourceBinder const &binder) = 0;
180 
182  virtual void UnbindResources(int program,
183  HdSt_ResourceBinder const &binder) = 0;
184 
186  virtual void AddBindings(HdBindingRequestVector* customBindings) = 0;
187 
193  HDST_API
194  virtual TfToken GetMaterialTag() const;
195 
201  public:
202  HDST_API
203  void AddSource(HdBufferArrayRangeSharedPtr const &range,
204  HdBufferSourceSharedPtr const &source);
205 
206  HDST_API
207  void AddSources(HdBufferArrayRangeSharedPtr const &range,
208  HdBufferSourceSharedPtrVector &&sources);
209 
210  HDST_API
211  void AddComputation(HdBufferArrayRangeSharedPtr const &range,
212  HdComputationSharedPtr const &computation,
213  HdStComputeQueue const queue);
214 
215  HdStResourceRegistry * GetResourceRegistry() const {
216  return _registry;
217  }
218 
219  private:
220  friend class HdStResourceRegistry;
222  HdStResourceRegistry *_registry;
223  };
224 
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 
247 PXR_NAMESPACE_CLOSE_SCOPE
248 
249 #endif //HDST_SHADER_H
virtual HDST_API HdSt_MaterialParamVector const & GetParams() const
Returns the shader parameters for this shader.
Information necessary to bind textures and create accessor for the texture.
Definition: shaderCode.h:142
virtual HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const
This function is called after textures have been allocated and loaded to add buffer sources and compu...
A map with string keys and VtValue values.
Definition: dictionary.h:63
virtual HDST_API HdBufferArrayRangeSharedPtr const & GetShaderData() const
Returns a buffer which stores parameter fallback values and texture handles.
virtual std::string GetSource(TfToken const &shaderStageKey) const =0
Returns the shader source provided by this shader for shaderStageKey.
HDST_API VtDictionary GetLayout(TfTokenVector const &shaderStageKeys) const
Returns the resource layout for the shader stages specified by shaderStageKeys.
HdTextureType type
Equal to handle->GetTextureObject()->GetTextureType().
Definition: shaderCode.h:152
virtual void AddBindings(HdBindingRequestVector *customBindings)=0
Add custom bindings (used by codegen)
TfToken name
Name by which the texture will be accessed, i.e., the name of the accesor for thexture will be HdGet_...
Definition: shaderCode.h:146
virtual HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const
Textures that need to be bound for this shader.
virtual void BindResources(int program, HdSt_ResourceBinder const &binder)=0
Binds shader-specific resources to program XXX: this interface is meant to be used for bridging the G...
virtual HDST_API TfTokenVector const & GetPrimvarNames() const
Returns the names of primvar that are used by this shader.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual ID ComputeTextureSourceHash() const
Returns the hash value of the paths of the texture prims consumed by this shader.
virtual HDST_API TfToken GetMaterialTag() const
Material tags can be set in the meta-data of a glslfx file to control what rprim collection that prim...
A central registry of all GPU resources.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition: shaderCode.h:75
A class representing the config and shader source of a glslfx file.
Definition: glslfx.h:150
virtual HDST_API bool IsEnabledPrimvarFiltering() const
Returns whether primvar filtering is enabled for this shader.
virtual ID ComputeHash() const =0
Returns the hash value of the shader code and configuration.
The context available in implementations of AddResourcesFromTextures.
Definition: shaderCode.h:200
HdStTextureHandleSharedPtr handle
The texture.
Definition: shaderCode.h:154
virtual void UnbindResources(int program, HdSt_ResourceBinder const &binder)=0
Unbinds shader-specific resources.
size_t hash
A hash unique to the corresponding asset; used to split draw batches when not using bindless textures...
Definition: shaderCode.h:158
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...