All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simpleShadowArray.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_GLF_SIMPLE_SHADOW_ARRAY_H
25 #define PXR_IMAGING_GLF_SIMPLE_SHADOW_ARRAY_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
32 #include "pxr/base/tf/refPtr.h"
33 #include "pxr/base/tf/weakPtr.h"
34 #include "pxr/base/gf/matrix4d.h"
35 #include "pxr/base/gf/vec2i.h"
36 #include "pxr/base/gf/vec4d.h"
37 #include "pxr/imaging/garch/glApi.h"
38 
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
44 class GlfSimpleShadowArray : public TfRefBase,
45  public TfWeakBase
46 {
47 public:
48  GLF_API
49  GlfSimpleShadowArray();
50  GLF_API
51  ~GlfSimpleShadowArray() override;
52 
53  // Disallow copies
54  GlfSimpleShadowArray(const GlfSimpleShadowArray&) = delete;
55  GlfSimpleShadowArray& operator=(const GlfSimpleShadowArray&) = delete;
56 
57  // Driven by the env var GLF_ENABLE_BINDLESS_SHADOW_TEXTURE, this returns
58  // whether bindless shadow maps are enabled, which in turn dictates the API
59  // to use. See below.
60  GLF_API static
61  bool GetBindlessShadowMapsEnabled();
62 
64 
65  // Set the 2D size of the shadow map texture array.
66  GLF_API
67  void SetSize(GfVec2i const & size);
68 
69  // Set the depth of the shadow map texture array, which corresponds to the
70  // number of shadow maps necessary. Each shadow casting light uses one
71  // shadow map.
72  GLF_API
73  void SetNumLayers(size_t numLayers);
74 
75  // Returns the GL texture id of the texture array.
76  GLF_API
77  GLuint GetShadowMapTexture() const;
78 
79  // Returns the GL sampler id of the sampler object used to read the raw
80  // depth values.
81  GLF_API
82  GLuint GetShadowMapDepthSampler() const;
83 
84  // Returns the GL sampler id of the sampler object used for depth comparison
85  GLF_API
86  GLuint GetShadowMapCompareSampler() const;
87 
89 
90  // Set the resolutions of all the shadow maps necessary. The number of
91  // resolutions corresponds to the number of shadow map textures necessary,
92  // which is currently one per shadow casting light.
93  GLF_API
94  void SetShadowMapResolutions(std::vector<GfVec2i> const& resolutions);
95 
96  // Returns a vector of the 64bit bindless handles corresponding to the
97  // bindless shadow map textures.
98  GLF_API
99  std::vector<uint64_t> const& GetBindlessShadowMapHandles() const;
100 
102 
103  // Returns the number of shadow map generation passes required, which is
104  // currently one per shadow map (corresponding to a shadow casting light).
105  GLF_API
106  size_t GetNumShadowMapPasses() const;
107 
108  // Returns the shadow map resolution for a given pass. For bindful shadows,
109  // this returns a single size for all passes, while for bindless, it returns
110  // the resolution of the corresponding shadow map,
111  GLF_API
112  GfVec2i GetShadowMapSize(size_t pass) const;
113 
114  // Get/Set the view (world to shadow camera) transform to use for a given
115  // shadow map generation pass.
116  GLF_API
117  GfMatrix4d GetViewMatrix(size_t index) const;
118  GLF_API
119  void SetViewMatrix(size_t index, GfMatrix4d const & matrix);
120 
121  // Get/Set the projection transform to use for a given shadow map generation
122  // pass.
123  GLF_API
124  GfMatrix4d GetProjectionMatrix(size_t index) const;
125  GLF_API
126  void SetProjectionMatrix(size_t index, GfMatrix4d const & matrix);
127 
128  GLF_API
129  GfMatrix4d GetWorldToShadowMatrix(size_t index) const;
130 
131  // Bind necessary resources for a given shadow map generation pass.
132  GLF_API
133  void BeginCapture(size_t index, bool clear);
134 
135  // Unbind necssary resources after a shadow map gneration pass.
136  GLF_API
137  void EndCapture(size_t index);
138 
139 private:
140  void _AllocResources();
141  void _AllocBindfulTextures();
142  void _AllocBindlessTextures();
143  void _FreeResources();
144  void _FreeBindfulTextures();
145  void _FreeBindlessTextures();
146  bool _ShadowMapExists() const;
147  void _BindFramebuffer(size_t index);
148  void _UnbindFramebuffer();
149 
150 private:
151  // bindful state
152  GfVec2i _size;
153  size_t _numLayers;
154  GLuint _bindfulTexture;
155  GLuint _shadowDepthSampler;
156 
157  // bindless state
158  std::vector<GfVec2i> _resolutions;
159  std::vector<GLuint> _bindlessTextures;
160  std::vector<uint64_t> _bindlessTextureHandles;
161 
162  // common state
163  std::vector<GfMatrix4d> _viewMatrix;
164  std::vector<GfMatrix4d> _projectionMatrix;
165 
166  GLuint _framebuffer;
167 
168  GLuint _shadowCompareSampler;
169 
170  GLuint _unbindRestoreDrawFramebuffer;
171  GLuint _unbindRestoreReadFramebuffer;
172 
173  GLint _unbindRestoreViewport[4];
174 };
175 
176 
177 PXR_NAMESPACE_CLOSE_SCOPE
178 
179 #endif
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
Standard pointer typedefs.
Pointer storage with deletion detection.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:71
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Reference counting.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141