HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simpleLightingShader.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HDST_SIMPLE_LIGHTING_SHADER_H
8 #define PXR_IMAGING_HDST_SIMPLE_LIGHTING_SHADER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
14 
15 #include "pxr/imaging/hd/version.h"
16 
19 
20 #include <memory>
21 #include <string>
22 
24 
25 class HdRenderIndex;
26 class HdRenderParam;
27 class HdSceneDelegate;
28 class HdStRenderBuffer;
31  std::shared_ptr<class HdStSimpleLightingShader>;
32 using HdRenderPassAovBindingVector = std::vector<HdRenderPassAovBinding>;
33 
35 
36 /// \class HdStSimpleLightingShader
37 ///
38 /// A shader that supports simple lighting functionality.
39 ///
41 {
42 public:
43  HDST_API
45  HDST_API
46  ~HdStSimpleLightingShader() override;
47 
48  /// HdShader overrides
49  HDST_API
50  ID ComputeHash() const override;
51  HDST_API
52  std::string GetSource(TfToken const &shaderStageKey) const override;
53  HDST_API
54  void BindResources(int program,
55  HdSt_ResourceBinder const &binder) override;
56  HDST_API
57  void UnbindResources(int program,
58  HdSt_ResourceBinder const &binder) override;
59 
60  /// Add a custom binding request for use when this shader executes.
61  HDST_API
62  void AddBufferBinding(HdStBindingRequest const& req);
63 
64  /// Remove \p name from custom binding.
65  HDST_API
66  void RemoveBufferBinding(TfToken const &name);
67 
68  /// Clear all custom bindings associated with this shader.
69  HDST_API
70  void ClearBufferBindings();
71 
72  HDST_API
73  void AddBindings(HdStBindingRequestVector *customBindings) override;
74 
75  /// Adds computations to create the dome light textures that
76  /// are pre-calculated from the environment map texture.
77  HDST_API
78  void AddResourcesFromTextures(ResourceContext &ctx) const override;
79 
80  /// HdStShaderCode overrides
81  HDST_API
82  HdSt_MaterialParamVector const& GetParams() const override;
83 
84  /// HdStLightingShader overrides
85  HDST_API
86  void SetCamera(
87  GfMatrix4d const &worldToViewMatrix,
88  GfMatrix4d const &projectionMatrix) override;
89 
90  GlfSimpleLightingContextRefPtr GetLightingContext() const {
91  return _lightingContext;
92  };
93 
94  /// Allocates texture handles (texture loading happens later during commit)
95  /// needed for lights.
96  ///
97  /// Call after lighting context has been set or updated in Sync-phase.
98  ///
99  HDST_API
101  HdRenderIndex const &renderIndex,
102  const SdfPath& graphPath);
103 
104  /// The dome light environment map used as source for the other
105  /// dome light textures.
108  return _domeLightEnvironmentTextureHandle;
109  }
110 
111  /// The textures computed from the dome light environment map that
112  /// the shader needs to bind for the dome light shading.
113  HDST_API
114  NamedTextureHandleVector const &GetNamedTextureHandles() const override;
115 
116  /// Get one of the textures that need to be computed from the dome
117  /// light environment map.
118  HDST_API
120  const TfToken &name) const;
121 
122  /// The dome light environment cubemap texture generated from the
123  /// latlong dome light texture.
124  HDST_API
127 
129  return _shadowAovBindings;
130  }
131 
132  HDST_API
133  void SetDomeLightCubemapTargetMemory(unsigned int targetMemoryMB);
134 
135  HDST_API
136  void SetMaxLights(uint32_t maxLights);
137 
138  HDST_API
139  static constexpr uint32_t GetMaxShadows() {
140  // Could only be higher with a texture atlas/array, since GL limits
141  // the number of bound samplers per shader stage.
142  return 16;
143  }
144 
145 private:
146  void _AllocateShadowTextures(
147  HdStResourceRegistry* const resourceRegistry,
148  const SdfPath& graphPath);
149  void _CleanupAovBindings();
150 
151  GlfSimpleLightingContextRefPtr _lightingContext;
152  std::unique_ptr<class HioGlslfx> _glslfx;
153 
154  // Lexicographic ordering for stable output between runs.
155  std::map<TfToken, HdStBindingRequest> _customBuffers;
156 
157  // The environment map used as source for the dome light textures.
158  //
159  // Handle is allocated in AllocateTextureHandles. Actual loading
160  // happens during commit.
161  HdStTextureHandleSharedPtr _domeLightEnvironmentTextureHandle;
162 
163  // Other dome light textures.
164  NamedTextureHandleVector _namedTextureHandles;
165 
166  NamedTextureHandleVector _domeLightTextureHandles;
167 
168  // Maximum target memory of computed dome light cubemap texture (in MB)
169  unsigned int _domeLightCubemapTargetMemoryMB;
170 
171  NamedTextureHandle _shadowTextureHandle;
172 
173  HdSt_MaterialParamVector _lightTextureParams;
174 
175  HdRenderParam *_renderParam;
176 
177  HdRenderPassAovBindingVector _shadowAovBindings;
178 
179  std::vector<HdStPooledRenderBufferUniquePtr> _shadowBuffers;
180 
181  HdStPooledRenderBufferUniquePtr _shadowBufferFallback;
182 
183  uint32_t _maxLights;
184 };
185 
186 
188 
189 #endif // PXR_IMAGING_HDST_SIMPLE_LIGHTING_SHADER_H
const HdStTextureHandleSharedPtr & GetDomeLightEnvironmentTextureHandle() const
GlfSimpleLightingContextRefPtr GetLightingContext() const
HDST_API void SetMaxLights(uint32_t maxLights)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
HDST_API void UnbindResources(int program, HdSt_ResourceBinder const &binder) override
Unbinds shader-specific resources.
HDST_API void SetDomeLightCubemapTargetMemory(unsigned int targetMemoryMB)
std::vector< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:23
HDST_API void BindResources(int program, HdSt_ResourceBinder const &binder) override
HDST_API ID ComputeHash() const override
HdShader overrides.
Definition: token.h:70
HDST_API const HdStTextureHandleSharedPtr & GetDomeLightEnvironmentCubemapTextureHandle() const
std::unique_ptr< HdStPooledRenderBuffer > HdStPooledRenderBufferUniquePtr
HDST_API std::string GetSource(TfToken const &shaderStageKey) const override
HdRenderPassAovBindingVector const & GetShadowAovBindings()
HDST_API HdSt_MaterialParamVector const & GetParams() const override
HdStShaderCode overrides.
HDST_API NamedTextureHandleVector const & GetNamedTextureHandles() const override
HDST_API void AllocateTextureHandles(HdRenderIndex const &renderIndex, const SdfPath &graphPath)
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:280
std::shared_ptr< class HdStSimpleLightingShader > HdStSimpleLightingShaderSharedPtr
static HDST_API constexpr uint32_t GetMaxShadows()
HDST_API void AddResourcesFromTextures(ResourceContext &ctx) const override
HDST_API void SetCamera(GfMatrix4d const &worldToViewMatrix, GfMatrix4d const &projectionMatrix) override
HdStLightingShader overrides.
HDST_API ~HdStSimpleLightingShader() override
std::vector< NamedTextureHandle > NamedTextureHandleVector
Definition: shaderCode.h:145
std::shared_ptr< class HdStTextureHandle > HdStTextureHandleSharedPtr
#define HDST_API
Definition: api.h:23
HDST_API void AddBufferBinding(HdStBindingRequest const &req)
Add a custom binding request for use when this shader executes.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HDST_API const HdStTextureHandleSharedPtr & GetTextureHandle(const TfToken &name) const
HDST_API void RemoveBufferBinding(TfToken const &name)
Remove name from custom binding.
TF_DECLARE_REF_PTRS(GlfBindingMap)
HDST_API void ClearBufferBindings()
Clear all custom bindings associated with this shader.
std::vector< HdRenderPassAovBinding > HdRenderPassAovBindingVector
Definition: aov.h:137
GLbitfield GLuint program
Definition: glcorearb.h:1931
HDST_API void AddBindings(HdStBindingRequestVector *customBindings) override
Add custom bindings (used by codegen)
std::vector< class HdSt_MaterialParam > HdSt_MaterialParamVector
HDST_API HdStSimpleLightingShader()