Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 330 vs 331 for /trunk/Source Code/Rendering/Lighting/ShadowMapping/CascadedShadowMaps.cpp

Diff revisions: vs.
  @@ -19,7 +19,11 @@
19 19
20 20 namespace Divide {
21 21
22 - CascadedShadowMaps::CascadedShadowMaps(Light* light, Camera* shadowCamera, F32 numSplits) : ShadowMap(light, shadowCamera, SHADOW_TYPE_CSM)
22 + CascadedShadowMaps::CascadedShadowMaps(Light* light,
23 + Camera* shadowCamera,
24 + F32 numSplits) : ShadowMap(light,
25 + shadowCamera,
26 + SHADOW_TYPE_CSM)
23 27 {
24 28 _dirLight = dynamic_cast<DirectionalLight*>(_light);
25 29 _splitLogFactor = _dirLight->csmSplitLogFactor();
  @@ -33,8 +37,9 @@
33 37 _shadowFloatValues.resize(_numSplits);
34 38 _horizBlur = 0;
35 39 _vertBlur = 0;
36 - _renderPolicy = New Framebuffer::FramebufferTarget(Framebuffer::defaultPolicy());
37 - _renderPolicy->_clearBuffersOnBind = false; //<we clear the FB on each face draw call, not on Begin()
40 + _renderPolicy = MemoryManager_NEW Framebuffer::FramebufferTarget(Framebuffer::defaultPolicy());
41 + // We clear the FB on each face draw call, not on Begin()
42 + _renderPolicy->_clearBuffersOnBind = false;
38 43
39 44 ResourceDescriptor shadowPreviewShader("fbPreview.Layered.LinearDepth.ESM");
40 45 shadowPreviewShader.setThreadedLoading(false);
  @@ -79,21 +84,21 @@
79 84 {
80 85 RemoveResource(_previewDepthMapShader);
81 86 RemoveResource(_blurDepthMapShader);
82 - MemoryManager::SAFE_DELETE( _blurBuffer );
83 - MemoryManager::SAFE_DELETE( _renderPolicy );
87 + MemoryManager::DELETE( _blurBuffer );
88 + MemoryManager::DELETE( _renderPolicy );
84 89 }
85 90
86 - void CascadedShadowMaps::init(ShadowMapInfo* const smi){
91 + void CascadedShadowMaps::init(ShadowMapInfo* const smi) {
87 92 _numSplits = smi->numLayers();
88 93 resolution(smi->resolution(), _light->shadowMapResolutionFactor());
89 94 _init = true;
90 95 }
91 96
92 - void CascadedShadowMaps::resolution(U16 resolution, U8 resolutionFactor){
97 + void CascadedShadowMaps::resolution(U16 resolution, U8 resolutionFactor) {
93 98 U16 tempResolution = resolution * resolutionFactor;
94 - if(_resolution != tempResolution){
99 + if (_resolution != tempResolution) {
95 100 _resolution = tempResolution;
96 - //Initialize the FB's with a variable resolution
101 + // Initialize the FB's with a variable resolution
97 102 PRINT_FN(Locale::get("LIGHT_INIT_SHADOW_FB"), _light->getGUID());
98 103 _depthMap->Create(_resolution, _resolution);
99 104 vec2<U16> screenResolution = GFX_DEVICE.getRenderTarget(GFXDevice::RENDER_TARGET_SCREEN)->getResolution();
  @@ -102,14 +107,16 @@
102 107 _blurBuffer->Create(_resolution, _resolution);
103 108 updateResolution(screenResolution.width, screenResolution.height);
104 109 }
110 +
105 111 ShadowMap::resolution(resolution, resolutionFactor);
106 112 }
107 113
108 - void CascadedShadowMaps::updateResolution(I32 newWidth, I32 newHeight){
114 + void CascadedShadowMaps::updateResolution(I32 newWidth, I32 newHeight) {
109 115 ShadowMap::updateResolution(newWidth, newHeight);
110 116 }
111 117
112 - void CascadedShadowMaps::render(SceneRenderState& renderState, const DELEGATE_CBK<>& sceneRenderFunction){
118 + void CascadedShadowMaps::render(SceneRenderState& renderState,
119 + const DELEGATE_CBK<>& sceneRenderFunction) {
113 120 //Only if we have a valid callback;
114 121 if (!sceneRenderFunction) {
115 122 ERROR_FN(Locale::get("ERROR_LIGHT_INVALID_SHADOW_CALLBACK"), _light->getGUID());
  @@ -120,7 +127,8 @@
120 127 _splitLogFactor = _dirLight->csmSplitLogFactor();
121 128 _nearClipOffset = _dirLight->csmNearClipOffset();
122 129 const vec2<F32>& currentPlanes = renderState.getCameraConst().getZPlanes();
123 - if (_sceneZPlanes != currentPlanes){
130 +
131 + if (_sceneZPlanes != currentPlanes) {
124 132 _sceneZPlanes = currentPlanes;
125 133 CalculateSplitDepths(sceneCamera);
126 134 }
  @@ -132,17 +140,18 @@
132 140
133 141 _depthMap->Begin(*_renderPolicy);
134 142 renderState.getCameraMgr().pushActiveCamera(_shadowCamera, false);
135 - for (U8 i = 0; i < _numSplits; ++i){
143 + for (U8 i = 0; i < _numSplits; ++i) {
136 144 ApplyFrustumSplit(i);
137 145 _depthMap->DrawToLayer(TextureDescriptor::Color0, i, true);
138 146 GFX_DEVICE.getRenderer()->render(sceneRenderFunction, renderState);
139 147 LightManager::getInstance().registerShadowPass();
140 148 }
141 149 _depthMap->End();
150 +
142 151 renderState.getCameraMgr().popActiveCamera();
143 152 }
144 153
145 - void CascadedShadowMaps::CalculateSplitDepths(const Camera& cam){
154 + void CascadedShadowMaps::CalculateSplitDepths(const Camera& cam) {
146 155 const mat4<F32>& projMatrixCache = cam.getProjectionMatrix();
147 156
148 157 F32 N = _numSplits;
  @@ -150,49 +159,72 @@
150 159 F32 farPlane = _sceneZPlanes.y;
151 160 _splitDepths[0] = nearPlane;
152 161 _splitDepths[_numSplits] = farPlane;
153 - for (I32 i = 1; i < (I32)_numSplits; ++i)
154 - _splitDepths[i] = _splitLogFactor * nearPlane * (F32)std::pow(farPlane / nearPlane, i / N) + (1.0f - _splitLogFactor) * ((nearPlane + (i / N)) * (farPlane - nearPlane));
155 - for (U8 i = 0; i < _numSplits; ++i)
156 - _light->setShadowFloatValue(i, 0.5f*(-_splitDepths[i + 1] * projMatrixCache.mat[10] + projMatrixCache.mat[14]) / _splitDepths[i + 1] + 0.5f);
162 + for (I32 i = 1; i < (I32)_numSplits; ++i) {
163 + _splitDepths[i] = _splitLogFactor *
164 + nearPlane *
165 + (F32)std::pow(farPlane / nearPlane, i / N) +
166 + (1.0f - _splitLogFactor) *
167 + ((nearPlane + (i / N)) *
168 + (farPlane - nearPlane));
169 + }
170 +
171 + for (U8 i = 0; i < _numSplits; ++i) {
172 + _light->setShadowFloatValue(i,
173 + 0.5f *
174 + (-_splitDepths[i + 1] *
175 + projMatrixCache.mat[10] +
176 + projMatrixCache.mat[14]) /
177 + _splitDepths[i + 1] + 0.5f);
178 + }
157 179 }
158 180
159 - void CascadedShadowMaps::ApplyFrustumSplit(U8 pass){
181 + void CascadedShadowMaps::ApplyFrustumSplit(U8 pass) {
160 182 F32 minZ = _splitDepths[pass];
161 183 F32 maxZ = _splitDepths[pass + 1];
162 184
163 - for (U8 i = 0; i < 4; i++)
185 + for (U8 i = 0; i < 4; ++i) {
164 186 _splitFrustumCornersVS[i] = _frustumCornersVS[i + 4] * (minZ / _sceneZPlanes.y);
187 + }
165 188
166 - for (U8 i = 4; i < 8; i++)
189 + for (U8 i = 4; i < 8; ++i) {
167 190 _splitFrustumCornersVS[i] = _frustumCornersVS[i] * (maxZ / _sceneZPlanes.y);
191 + }
168 192
169 - for (U8 i = 0; i < 8; i++)
193 + for (U8 i = 0; i < 8; ++i) {
170 194 _frustumCornersWS[i].set(_viewInvMatrixCache.transform(_splitFrustumCornersVS[i]));
195 + }
171 196
172 197 vec3<F32> frustumCentroid(0.0f);
173 - for (U8 i = 0; i < 8; ++i)
198 +
199 + for (U8 i = 0; i < 8; ++i) {
174 200 frustumCentroid += _frustumCornersWS[i];
201 + }
175 202 // Find the centroid
176 203 frustumCentroid /= 8;
177 204
178 - // Position the shadow-caster camera so that it's looking at the centroid, and backed up in the direction of the sunlight
179 - F32 distFromCentroid = std::max((maxZ - minZ), _splitFrustumCornersVS[4].distance(_splitFrustumCornersVS[5]));// + _nearClipOffset;
205 + // Position the shadow-caster camera so that it's looking at the centroid,
206 + // and backed up in the direction of the sunlight
207 + F32 distFromCentroid = std::max((maxZ - minZ),
208 + _splitFrustumCornersVS[4].distance(_splitFrustumCornersVS[5]));
209 + // + _nearClipOffset;
180 210 vec3<F32> currentEye = frustumCentroid - (_lightPosition * distFromCentroid);
181 211 const mat4<F32>& viewMatrix = _shadowCamera->lookAt(currentEye, frustumCentroid);
182 212 // Determine the position of the frustum corners in light space
183 - for (U8 i = 0; i < 8; i++){
213 + for (U8 i = 0; i < 8; ++i) {
184 214 _frustumCornersLS[i].set(viewMatrix.transform(_frustumCornersWS[i]));
185 215 }
186 216 // Create an orthographic camera for use as a shadow caster
187 217 vec2<F32> clipPlanes;
188 218 vec4<F32> clipRect;
219 +
189 220 if ( _dirLight->csmStabilize() ) {
190 221 BoundingSphere frustumSphere( _frustumCornersLS );
191 222 clipPlanes.set( 0.0f, frustumSphere.getDiameter() );
192 223 clipPlanes += _nearClipOffset;
193 224 clipRect.set( UNIT_RECT * frustumSphere.getRadius() );
194 225 } else {
195 - // Calculate an orthographic projection by sizing a bounding box to the frustum coordinates in light space
226 + // Calculate an orthographic projection by sizing a bounding box to the
227 + // frustum coordinates in light space
196 228 BoundingBox frustumBox( _frustumCornersLS );
197 229 vec3<F32> maxes = frustumBox.getMax();
198 230 vec3<F32> mins = frustumBox.getMin();
  @@ -216,9 +248,10 @@
216 248 _shadowCamera->renderLookAt();
217 249 }
218 250
219 - void CascadedShadowMaps::postRender(){
220 - if (GFX_DEVICE.shadowDetailLevel() == DETAIL_LOW)
251 + void CascadedShadowMaps::postRender() {
252 + if (GFX_DEVICE.shadowDetailLevel() == DETAIL_LOW) {
221 253 return;
254 + }
222 255
223 256 GFX_DEVICE.toggle2D(true);
224 257
  @@ -265,11 +298,13 @@
265 298 for (U8 i = 0; i < _numSplits; ++i){
266 299 _previewDepthMapShader->Uniform("layer", i);
267 300 _previewDepthMapShader->Uniform("zPlanes", vec2<F32>(_splitDepths[i], _splitDepths[i + 1]));
268 - GFX_DEVICE.renderInViewport(vec4<I32>(130 * i, 0, 128, 128), DELEGATE_BIND((void(GFXDevice::*)(U32, size_t, ShaderProgram* const))&GFXDevice::drawPoints,
269 - &GFX_DEVICE,
270 - 1,
271 - GFX_DEVICE.getDefaultStateBlock(true),
272 - _previewDepthMapShader));
301 + GFX_DEVICE.renderInViewport(vec4<I32>(130 * i, 0, 128, 128),
302 + DELEGATE_BIND((void(GFXDevice::*)(U32, size_t, ShaderProgram* const))
303 + &GFXDevice::drawPoints,
304 + &GFX_DEVICE,
305 + 1,
306 + GFX_DEVICE.getDefaultStateBlock(true),
307 + _previewDepthMapShader));
273 308 }
274 309 }
275 310