All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
material.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_MATERIAL_H
25 #define PXR_IMAGING_HD_ST_MATERIAL_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hdSt/materialNetwork.h"
30 #include "pxr/imaging/hdSt/shaderCode.h"
31 #include "pxr/imaging/hd/material.h"
32 #include "pxr/imaging/hf/perfLog.h"
33 #include "pxr/base/tf/envSetting.h"
34 
35 #include <memory>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 using HdStSurfaceShaderSharedPtr = std::shared_ptr<class HdStSurfaceShader>;
40 
41 using HdStTextureResourceSharedPtr =
42  std::shared_ptr<class HdStTextureResource>;
43 using HdStTextureResourceHandleSharedPtr =
44  std::shared_ptr<class HdStTextureResourceHandle>;
45 using HdStTextureResourceHandleSharedPtrVector =
46  std::vector<HdStTextureResourceHandleSharedPtr>;
47 using HdStResourceRegistrySharedPtr =
48  std::shared_ptr<class HdStResourceRegistry>;
49 
50 class HioGlslfx;
51 
52 HDST_API
53 extern TfEnvSetting<bool> HDST_USE_NEW_TEXTURE_SYSTEM;
54 
55 class HdStMaterial final: public HdMaterial {
56 public:
57  HF_MALLOC_TAG_NEW("new HdStMaterial");
58 
59  HDST_API
60  HdStMaterial(SdfPath const& id);
61  HDST_API
62  virtual ~HdStMaterial();
63 
65  HDST_API
66  virtual void Sync(HdSceneDelegate *sceneDelegate,
67  HdRenderParam *renderParam,
68  HdDirtyBits *dirtyBits) override;
69 
73  HDST_API
74  virtual HdDirtyBits GetInitialDirtyBitsMask() const override;
75 
77  HDST_API
78  virtual void Reload() override;
79 
81  HDST_API
82  HdStShaderCodeSharedPtr GetShaderCode() const;
83 
86  inline HdTextureResource::ID GetTextureResourceID(
87  HdSceneDelegate* sceneDelegate,
88  SdfPath const& textureId) const;
89 
94  inline bool HasPtex() const;
95 
97  inline bool HasLimitSurfaceEvaluation() const;
98 
99  // Returns true if the material has a displacement terminal.
100  inline bool HasDisplacement() const;
101 
102  // Returns the material's render pass tag.
103  inline const TfToken& GetMaterialTag() const;
104 
108  HDST_API
109  void SetSurfaceShader(HdStSurfaceShaderSharedPtr &shaderCode);
110 
111 private:
112  // Uses HdSceneDelegate::GetTextureResourceID and
113  // HdSceneDelegate::GetTextureResource (which will be obsoleted
114  // and removed at some point). Also resolves to a 1x1-texture with
115  // fallback value if the above calls return invalid results.
116  HdStTextureResourceHandleSharedPtr
117  _GetTextureResourceHandleFromSceneDelegate(
118  HdSceneDelegate * sceneDelegate,
119  HdStResourceRegistrySharedPtr const& resourceRegistry,
120  HdStMaterialNetwork::TextureDescriptor const &desc);
121 
122  // Processes the texture descriptors from a material network to
123  // create textures using either the Storm texture system or the
124  // HdSceneDelegate::GetTextureResource/ID.
125  //
126  // Adds buffer specs/sources necessary for textures, e.g., bindless
127  // handles or sampling transform for field textures.
128  void _ProcessTextureDescriptors(
129  HdSceneDelegate * sceneDelegate,
130  HdStResourceRegistrySharedPtr const& resourceRegistry,
131  std::weak_ptr<HdStShaderCode> const &shaderCode,
132  HdStMaterialNetwork::TextureDescriptorVector const &descs,
133  HdStShaderCode::NamedTextureHandleVector * texturesFromStorm,
134  HdStShaderCode::TextureDescriptorVector * texturesFromSceneDelegate,
135  HdBufferSpecVector * specs,
136  HdBufferSourceSharedPtrVector * sources);
137 
138  bool
139  _GetHasLimitSurfaceEvaluation(VtDictionary const & metadata) const;
140 
141  void _InitFallbackShader();
142 
143  static HioGlslfx *_fallbackGlslfx;
144 
145  HdStSurfaceShaderSharedPtr _surfaceShader;
146 
147  // Holds fallback textures if a texture cannot be found, but also holds
148  // texture we discovered inside a material network that could not be found
149  // in the resource registry (no Bprim inserted).
150  HdStTextureResourceHandleSharedPtrVector _internalTextureResourceHandles;
151 
152  bool _isInitialized : 1;
153  bool _hasPtex : 1;
154  bool _hasLimitSurfaceEvaluation : 1;
155  bool _hasDisplacement : 1;
156 
157  TfToken _materialTag;
158 
159  HdStMaterialNetwork _networkProcessor;
160 };
161 
162 inline HdTextureResource::ID
163 HdStMaterial::GetTextureResourceID(HdSceneDelegate* sceneDelegate,
164  SdfPath const& textureId) const
165 {
166  return sceneDelegate->GetTextureResourceID(textureId);
167 }
168 
169 inline bool HdStMaterial::HasPtex() const
170 {
171  return _hasPtex;
172 }
173 
174 inline bool HdStMaterial::HasLimitSurfaceEvaluation() const
175 {
176  return _hasLimitSurfaceEvaluation;
177 }
178 
179 inline bool HdStMaterial::HasDisplacement() const
180 {
181  return _hasDisplacement;
182 }
183 
184 inline const TfToken& HdStMaterial::GetMaterialTag() const
185 {
186  return _materialTag;
187 }
188 
189 PXR_NAMESPACE_CLOSE_SCOPE
190 
191 #endif // PXR_IMAGING_HD_ST_MATERIAL_H
virtual HdDirtyBits GetInitialDirtyBitsMask() const =0
Returns the minimal set of dirty bits to place in the change tracker for use in the first sync of thi...
virtual void Sync(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits)=0
Synchronizes state from the delegate to this object.
A map with string keys and VtValue values.
Definition: dictionary.h:63
Helps HdStMaterial process a Hydra material network into shader source code and parameters values...
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Adapter class providing data exchange with the client scene graph.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
A class representing the config and shader source of a glslfx file.
Definition: glslfx.h:148
virtual HD_API HdTextureResource::ID GetTextureResourceID(SdfPath const &textureId)
Returns the texture resource ID for a given texture ID.
virtual void Reload()=0
Causes the shader to be reloaded.
Hydra Schema for a material object.
Definition: material.h:37