All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
surfaceShader.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_SURFACE_SHADER_H
25 #define PXR_IMAGING_HD_ST_SURFACE_SHADER_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/hdSt/shaderCode.h"
31 
32 #include "pxr/usd/sdf/path.h"
33 
34 #include "pxr/base/vt/value.h"
35 #include "pxr/base/tf/token.h"
36 
37 #include <memory>
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
43 class HdSceneDelegate;
44 
45 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
46 
47 using HdStSurfaceShaderSharedPtr = std::shared_ptr<class HdStSurfaceShader>;
48 
49 using HdBufferSpecVector = std::vector<struct HdBufferSpec>;
50 using HdStResourceRegistrySharedPtr =
51  std::shared_ptr<class HdStResourceRegistry>;
52 
62 public:
63  HDST_API
65  HDST_API
66  ~HdStSurfaceShader() override;
67 
68 
69  // ---------------------------------------------------------------------- //
71  // ---------------------------------------------------------------------- //
72  HDST_API
73  std::string GetSource(TfToken const &shaderStageKey) const override;
74  HDST_API
75  HdSt_MaterialParamVector const& GetParams() const override;
76  HDST_API
77  void SetEnabledPrimvarFiltering(bool enabled);
78  HDST_API
79  bool IsEnabledPrimvarFiltering() const override;
80  HDST_API
81  TfTokenVector const& GetPrimvarNames() const override;
82  HDST_API
83  HdBufferArrayRangeSharedPtr const& GetShaderData() const override;
84  HDST_API
85  NamedTextureHandleVector const & GetNamedTextureHandles() const override;
86  HDST_API
87  void BindResources(int program,
88  HdSt_ResourceBinder const &binder,
89  HdRenderPassState const &state) override;
90  HDST_API
91  void UnbindResources(int program,
92  HdSt_ResourceBinder const &binder,
93  HdRenderPassState const &state) override;
94  HDST_API
95  void AddBindings(HdBindingRequestVector *customBindings) override;
96  HDST_API
97  ID ComputeHash() const override;
98 
99  HDST_API
100  ID ComputeTextureSourceHash() const override;
101 
102  HDST_API
103  TfToken GetMaterialTag() const override;
104 
106  HDST_API
107  void SetFragmentSource(const std::string &source);
108  HDST_API
109  void SetGeometrySource(const std::string &source);
110  HDST_API
111  void SetParams(const HdSt_MaterialParamVector &params);
112  HDST_API
113  void SetNamedTextureHandles(const NamedTextureHandleVector &);
114  HDST_API
115  void SetBufferSources(
116  HdBufferSpecVector const &bufferSpecs,
117  HdBufferSourceSharedPtrVector &&bufferSources,
118  HdStResourceRegistrySharedPtr const &resourceRegistry);
119 
127  HDST_API
128  void AddResourcesFromTextures(ResourceContext &ctx) const override;
129 
130  HDST_API
131  void SetMaterialTag(TfToken const &materialTag);
132 
134  HDST_API
135  virtual void Reload();
136 
138  HDST_API
139  static bool CanAggregate(HdStShaderCodeSharedPtr const &shaderA,
140  HdStShaderCodeSharedPtr const &shaderB);
141 
145  HDST_API
147  const HdSt_MaterialParam &param,
148  HdBufferSpecVector * const specs,
149  HdBufferSourceSharedPtrVector * const sources);
150 
151 protected:
152  HDST_API
153  void _SetSource(TfToken const &shaderStageKey, std::string const &source);
154 
155  HDST_API
156  ID _ComputeHash() const;
157 
158  HDST_API
159  ID _ComputeTextureSourceHash() const;
160 
161 private:
162  std::string _fragmentSource;
163  std::string _geometrySource;
164 
165  // Shader Parameters
166  HdSt_MaterialParamVector _params;
167  HdBufferSpecVector _paramSpec;
168  HdBufferArrayRangeSharedPtr _paramArray;
169  TfTokenVector _primvarNames;
170  bool _isEnabledPrimvarFiltering;
171 
172  mutable size_t _computedHash;
173  mutable bool _isValidComputedHash;
174 
175  mutable size_t _computedTextureSourceHash;
176  mutable bool _isValidComputedTextureSourceHash;
177 
178  NamedTextureHandleVector _namedTextureHandles;
179 
180  TfToken _materialTag;
181 
182  // No copying
183  HdStSurfaceShader(const HdStSurfaceShader &) = delete;
184  HdStSurfaceShader &operator =(const HdStSurfaceShader &) = delete;
185 };
186 
187 
188 PXR_NAMESPACE_CLOSE_SCOPE
189 
190 #endif //PXR_IMAGING_HD_ST_SURFACE_SHADER_H
HDST_API HdSt_MaterialParamVector const & GetParams() const override
Returns the shader parameters for this shader.
HDST_API ID ComputeTextureSourceHash() const override
Returns the hash value of the paths of the texture prims consumed by this shader. ...
virtual HDST_API void Reload()
If the prim is based on asset, reload that asset.
HDST_API HdBufferArrayRangeSharedPtr const & GetShaderData() const override
Returns a buffer which stores parameter fallback values and texture handles.
HDST_API void AddBindings(HdBindingRequestVector *customBindings) override
Add custom bindings (used by codegen)
HDST_API bool IsEnabledPrimvarFiltering() const override
Returns whether primvar filtering is enabled for this shader.
HDST_API void UnbindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state) override
Unbinds shader-specific resources.
A set of rendering parameters used among render passes.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
static HDST_API void AddFallbackValueToSpecsAndSources(const HdSt_MaterialParam &param, HdBufferSpecVector *const specs, HdBufferSourceSharedPtrVector *const sources)
Adds the fallback value of the given material param to buffer specs and sources using the param&#39;s nam...
Adapter class providing data exchange with the client scene graph.
HDST_API ID ComputeHash() const override
Returns the hash value of the shader code and configuration.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
HDST_API std::string GetSource(TfToken const &shaderStageKey) const override
Returns the shader source provided by this shader for shaderStageKey.
A scene-based SurfaceShader object.
Definition: surfaceShader.h:61
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition: shaderCode.h:75
HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const override
Called after textures have been committed.
HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const override
Textures that need to be bound for this shader.
HDST_API void SetFragmentSource(const std::string &source)
Setter method for prim.
The context available in implementations of AddResourcesFromTextures.
Definition: shaderCode.h:196
HDST_API void BindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state) override
Binds shader-specific resources to program XXX: this interface is meant to be used for bridging the G...
HDST_API TfToken GetMaterialTag() const override
Material tags can be set in the meta-data of a glslfx file to control what rprim collection that prim...
static HDST_API bool CanAggregate(HdStShaderCodeSharedPtr const &shaderA, HdStShaderCodeSharedPtr const &shaderB)
Returns if the two shaders can be aggregated into the same draw batch.
HDST_API TfTokenVector const & GetPrimvarNames() const override
Returns the names of primvar that are used by this shader.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...