HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
surfaceShader.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_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"
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 
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>;
51  std::shared_ptr<class HdStResourceRegistry>;
52 
53 /// \class HdStSurfaceShader
54 ///
55 /// A scene-based SurfaceShader object.
56 ///
57 /// When surface shaders are expresed in the scene graph, the HdSceneDelegate
58 /// can use this object to express these surface shaders in Storm. In addition
59 /// to the shader itself, a binding from the Rprim to the SurfaceShader must be
60 /// expressed as well.
62 public:
63  HDST_API
65  HDST_API
66  ~HdStSurfaceShader() override;
67 
68 
69  // ---------------------------------------------------------------------- //
70  /// \name HdShader Virtual Interface //
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
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 
105  /// Setter method for prim
106  HDST_API
107  void SetFragmentSource(const std::string &source);
108  HDST_API
109  void SetGeometrySource(const std::string &source);
110  HDST_API
112  HDST_API
114  HDST_API
115  void SetBufferSources(
116  HdBufferSpecVector const &bufferSpecs,
117  HdBufferSourceSharedPtrVector &&bufferSources,
118  HdStResourceRegistrySharedPtr const &resourceRegistry);
119 
120  /// Called after textures have been committed.
121  ///
122  /// Shader can return buffer sources for different BARs (most
123  /// likely, the shader bar) that require texture metadata such as
124  /// the bindless texture handle which is only available after the
125  /// commit.
126  ///
127  HDST_API
128  void AddResourcesFromTextures(ResourceContext &ctx) const override;
129 
130  HDST_API
131  void SetMaterialTag(TfToken const &materialTag);
132 
133  /// If the prim is based on asset, reload that asset.
134  HDST_API
135  virtual void Reload();
136 
137  /// Returns if the two shaders can be aggregated into the same draw batch.
138  HDST_API
139  static bool CanAggregate(HdStShaderCodeSharedPtr const &shaderA,
140  HdStShaderCodeSharedPtr const &shaderB);
141 
142  /// Adds the fallback value of the given material param to
143  /// buffer specs and sources using the param's name.
144  ///
145  HDST_API
147  const HdSt_MaterialParam &param,
148  HdBufferSpecVector * const specs,
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
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 
189 
190 #endif //PXR_IMAGING_HD_ST_SURFACE_SHADER_H
HDST_API ID ComputeTextureSourceHash() const override
Setter method for prim.
GLenum GLfloat param
Definition: glcorearb.h:103
HDST_API HdStSurfaceShader()
HDST_API HdSt_MaterialParamVector const & GetParams() const override
Setter method for prim.
HDST_API HdBufferArrayRangeSharedPtr const & GetShaderData() const override
Setter method for prim.
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:44
HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const override
HDST_API void SetParams(const HdSt_MaterialParamVector &params)
Setter method for prim.
GLbitfield GLuint program
Definition: glcorearb.h:1930
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:2538
static HDST_API void AddFallbackValueToSpecsAndSources(const HdSt_MaterialParam &param, HdBufferSpecVector *const specs, HdBufferSourceSharedPtrVector *const sources)
HDST_API ID _ComputeHash() const
Setter method for prim.
HDST_API void SetGeometrySource(const std::string &source)
Setter method for prim.
HDST_API std::string GetSource(TfToken const &shaderStageKey) const override
Setter method for prim.
GLsizei GLenum * sources
Definition: glcorearb.h:2541
Definition: token.h:87
HDST_API void BindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state) override
Setter method for prim.
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
HDST_API bool IsEnabledPrimvarFiltering() const override
Setter method for prim.
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
HDST_API void SetNamedTextureHandles(const NamedTextureHandleVector &)
Setter method for prim.
HDST_API void AddBindings(HdBindingRequestVector *customBindings) override
Setter method for prim.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
std::shared_ptr< class HdStSurfaceShader > HdStSurfaceShaderSharedPtr
Definition: material.h:38
HDST_API void SetFragmentSource(const std::string &source)
Setter method for prim.
std::vector< struct HdBufferSpec > HdBufferSpecVector
virtual HDST_API void Reload()
If the prim is based on asset, reload that asset.
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 ~HdStSurfaceShader() override
HDST_API ID _ComputeTextureSourceHash() const
Setter method for prim.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
std::vector< NamedTextureHandle > NamedTextureHandleVector
Definition: shaderCode.h:154
HDST_API TfTokenVector const & GetPrimvarNames() const override
Setter method for prim.
#define HDST_API
Definition: api.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HDST_API ID ComputeHash() const override
Setter method for prim.
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
HDST_API void SetMaterialTag(TfToken const &materialTag)
Setter method for prim.
HDST_API void SetEnabledPrimvarFiltering(bool enabled)
Setter method for prim.
GLenum const GLfloat * params
Definition: glcorearb.h:104
HDST_API void _SetSource(TfToken const &shaderStageKey, std::string const &source)
Setter method for prim.
HDST_API void SetBufferSources(HdBufferSpecVector const &bufferSpecs, HdBufferSourceSharedPtrVector &&bufferSources, HdStResourceRegistrySharedPtr const &resourceRegistry)
Setter method for prim.
std::shared_ptr< class HdStResourceRegistry > HdStResourceRegistrySharedPtr
Definition: commandBuffer.h:47
std::shared_ptr< class HdStShaderCode > HdStShaderCodeSharedPtr
Definition: drawItem.h:34
std::vector< class HdSt_MaterialParam > HdSt_MaterialParamVector
HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const override
Setter method for prim.
HDST_API TfToken GetMaterialTag() const override
Setter method for prim.
HDST_API void UnbindResources(int program, HdSt_ResourceBinder const &binder, HdRenderPassState const &state) override
Setter method for prim.
std::vector< class HdBindingRequest > HdBindingRequestVector
Definition: binding.h:41