1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2012 NVIDIA Corporation.
4 ** Copyright (C) 2019 The Qt Company Ltd.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Quick 3D.
8 **
9 ** $QT_BEGIN_LICENSE:GPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 or (at your option) any later version
21 ** approved by the KDE Free Qt Foundation. The licenses are as published by
22 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
23 ** included in the packaging of this file. Please review the following
24 ** information to ensure the GNU General Public License requirements will
25 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26 **
27 ** $QT_END_LICENSE$
28 **
29 ****************************************************************************/
30 
31 #ifndef QSSG_RENDER_SHADOW_MAP_H
32 #define QSSG_RENDER_SHADOW_MAP_H
33 
34 //
35 //  W A R N I N G
36 //  -------------
37 //
38 // This file is not part of the Qt API.  It exists purely as an
39 // implementation detail.  This header file may change from version to
40 // version without notice, or even be removed.
41 //
42 // We mean it.
43 //
44 
45 #include <QtQuick3DRuntimeRender/private/qssgrendercontextcore_p.h>
46 #include <QtGui/QMatrix4x4>
47 #include <QtGui/QVector3D>
48 #include <QtQuick3DRender/private/qssgrenderbasetypes_p.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 struct QSSGLayerRenderData;
53 
54 enum class ShadowMapModes
55 {
56     SSM, ///< standard shadow mapping
57     VSM, ///< variance shadow mapping
58     CUBE, ///< cubemap omnidirectional shadows
59 };
60 
61 enum class ShadowFilterValues
62 {
63     NONE = 1 << 0, ///< hard shadows
64     PCF = 1 << 1, ///< Percentage close filtering
65     BLUR = 1 << 2, ///< Gausian Blur
66 };
67 
68 struct QSSGShadowMapEntry
69 {
QSSGShadowMapEntryQSSGShadowMapEntry70     QSSGShadowMapEntry()
71         : m_lightIndex(std::numeric_limits<quint32>::max())
72         , m_shadowMapMode(ShadowMapModes::SSM)
73         , m_shadowFilterFlags(ShadowFilterValues::NONE)
74     {
75     }
76 
QSSGShadowMapEntryQSSGShadowMapEntry77     QSSGShadowMapEntry(quint32 index,
78                          ShadowMapModes mode,
79                          ShadowFilterValues filter,
80                          const QSSGRef<QSSGRenderTexture2D> &depthMap,
81                          const QSSGRef<QSSGRenderTexture2D> &depthCopy,
82                          const QSSGRef<QSSGRenderTexture2D> &depthTemp)
83         : m_lightIndex(index)
84         , m_shadowMapMode(mode)
85         , m_shadowFilterFlags(filter)
86         , m_depthMap(depthMap)
87         , m_depthCopy(depthCopy)
88         , m_depthCube(nullptr)
89         , m_cubeCopy(nullptr)
90         , m_depthRender(depthTemp)
91     {
92     }
93 
QSSGShadowMapEntryQSSGShadowMapEntry94     QSSGShadowMapEntry(quint32 index,
95                          ShadowMapModes mode,
96                          ShadowFilterValues filter,
97                          const QSSGRef<QSSGRenderTextureCube> &depthCube,
98                          const QSSGRef<QSSGRenderTextureCube> &cubeTmp,
99                          const QSSGRef<QSSGRenderTexture2D> &depthTemp)
100         : m_lightIndex(index)
101         , m_shadowMapMode(mode)
102         , m_shadowFilterFlags(filter)
103         , m_depthMap(nullptr)
104         , m_depthCopy(nullptr)
105         , m_depthCube(depthCube)
106         , m_cubeCopy(cubeTmp)
107         , m_depthRender(depthTemp)
108     {
109     }
110 
111     quint32 m_lightIndex; ///< the light index it belongs to
112     ShadowMapModes m_shadowMapMode; ///< shadow map method
113     ShadowFilterValues m_shadowFilterFlags; ///< shadow filter mode
114 
115     // PKC : Adding the DepthRender buffer allows us to have a depth+stencil format when filling
116     // the shadow maps (depth+stencil is necessary), but use a more compact format for the
117     // actual
118     // shadow map used at shade time.  See if it's worth adding.
119     QSSGRef<QSSGRenderTexture2D> m_depthMap; ///< shadow map texture
120     QSSGRef<QSSGRenderTexture2D> m_depthCopy; ///< shadow map buffer used during blur passes
121     QSSGRef<QSSGRenderTextureCube> m_depthCube; ///< shadow cube map
122     QSSGRef<QSSGRenderTextureCube> m_cubeCopy; ///< cube map buffer used during the blur passes
123     QSSGRef<QSSGRenderTexture2D> m_depthRender; ///< shadow depth+stencil map used during rendering
124 
125     QMatrix4x4 m_lightVP; ///< light view projection matrix
126     QMatrix4x4 m_lightCubeView[6]; ///< light cubemap view matrices
127     QMatrix4x4 m_lightView; ///< light view transform
128 };
129 
130 class QSSGRenderShadowMap
131 {
132     typedef QVector<QSSGShadowMapEntry> TShadowMapEntryList;
133 
134 public:
135     QAtomicInt ref;
136     QSSGRef<QSSGRenderContextInterface> m_context;
137 
138     QSSGRenderShadowMap(const QSSGRef<QSSGRenderContextInterface> &inContext);
139     ~QSSGRenderShadowMap();
140 
141     /*
142      * @brief Add a shadow map entry
143      *		  This creates a new shadow map if it does not exist or changed
144      *
145      * @param[in] index		shadow map entry index
146      * @param[in] width		shadow map width
147      * @param[in] height	shadow map height
148      * @param[in] format	shadow map format
149      * @param[in] samples	shadow map sample count
150      * @param[in] mode		shadow map mode like SSM, VCM
151      * @param[in] filter	soft shadow map mode filter like PCF
152      *
153      * @ return no return
154      */
155     void addShadowMapEntry(qint32 index,
156                            qint32 width,
157                            qint32 height,
158                            QSSGRenderTextureFormat format,
159                            qint32 samples,
160                            ShadowMapModes mode,
161                            ShadowFilterValues filter);
162 
163     /*
164      * @brief Get a shadow map entry
165      *
166      * @param[in] index		shadow map entry index
167      *
168      * @ return shadow map entry or nullptr
169      */
170     QSSGShadowMapEntry *getShadowMapEntry(int index);
171 
172     /*
173      * @brief Get shadow map entry count
174      *
175      * @ return count of shadow map entries
176      */
getShadowMapEntryCount()177     qint32 getShadowMapEntryCount() { return m_shadowMapList.size(); }
178 
179     static QSSGRef<QSSGRenderShadowMap> create(const QSSGRef<QSSGRenderContextInterface> &inContext);
180 
181 private:
182     TShadowMapEntryList m_shadowMapList; ///< List of shadow map entries
183 };
184 QT_END_NAMESPACE
185 
186 #endif
187