Subversion Repository Public Repository

Divide-Framework

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "stdafx.h"

#include "Headers/ShadowMap.h"
#include "Headers/CubeShadowMap.h"
#include "Headers/SingleShadowMap.h"
#include "Scenes/Headers/SceneState.h"
#include "Headers/CascadedShadowMaps.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Rendering/Lighting/Headers/DirectionalLight.h"
#include "Rendering/Lighting/Headers/LightPool.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Buffers/RenderTarget/Headers/RenderTarget.h"

namespace Divide {

std::array<ShadowMap::LayerUsageMask, to_base(ShadowType::COUNT)> ShadowMap::_depthMapUsage;


ShadowMap::ShadowMap(GFXDevice& context, Light* light, const ShadowCameraPool& shadowCameras, ShadowType type)
    : _context(context),
      _init(false),
      _light(light),
      _shadowCameras(shadowCameras),
      _shadowMapType(type),
      _arrayOffset(findDepthMapLayer(_shadowMapType))
{
    commitDepthMapLayer(_shadowMapType, _arrayOffset);
}

ShadowMap::~ShadowMap()
{
    freeDepthMapLayer(_shadowMapType, _arrayOffset);
}

void ShadowMap::resetShadowMaps(GFXDevice& context) {
    ACKNOWLEDGE_UNUSED(context);

    for (U32 i = 0; i < to_base(ShadowType::COUNT); ++i) {
        _depthMapUsage[i].fill(false);
    }
}

void ShadowMap::initShadowMaps(GFXDevice& context) {
    std::array<U16, to_base(ShadowType::COUNT)> resolutions;
    resolutions.fill(512);
    if (context.shadowDetailLevel() == RenderDetailLevel::ULTRA) {
        // Cap cubemap resolution
        resolutions.fill(2048);
        resolutions[to_base(ShadowType::CUBEMAP)] = 1024;
    } else if (context.shadowDetailLevel() == RenderDetailLevel::HIGH) {
        resolutions.fill(1024);
    }

    RenderTargetHandle crtTarget;
    for (U32 i = 0; i < to_base(ShadowType::COUNT); ++i) {
        switch (static_cast<ShadowType>(i)) {
            case ShadowType::SINGLE: {
                SamplerDescriptor depthMapSampler;
                depthMapSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
                depthMapSampler.setFilters(TextureFilter::LINEAR);
                depthMapSampler._useRefCompare = true;
                depthMapSampler._cmpFunc = ComparisonFunction::LEQUAL;

                // Default filters, LINEAR is OK for this
                TextureDescriptor depthMapDescriptor(TextureType::TEXTURE_2D_ARRAY,
                                                     GFXImageFormat::DEPTH_COMPONENT,
                                                     GFXDataFormat::UNSIGNED_INT);
                depthMapDescriptor.setLayerCount(Config::Lighting::MAX_SHADOW_CASTING_LIGHTS);
                depthMapDescriptor.setSampler(depthMapSampler);

                vectorImpl<RTAttachmentDescriptor> att = {
                    { depthMapDescriptor, RTAttachment::Type::Depth },
                };

                RenderTargetDescriptor desc = {};
                desc._name = "Single_ShadowMap";
                desc._resolution = resolutions[i];
                desc._attachmentCount = to_U32(att.size());
                desc._attachments = att.data();

                crtTarget = context.allocateRT(RenderTargetUsage::SHADOW, desc);
            } break;

            case ShadowType::LAYERED: {
                SamplerDescriptor depthMapSampler;
                depthMapSampler.setFilters(TextureFilter::LINEAR_MIPMAP_LINEAR);
                depthMapSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
                depthMapSampler.setAnisotropy(8);
                depthMapSampler._useRefCompare = false;

                TextureDescriptor depthMapDescriptor(TextureType::TEXTURE_2D_ARRAY,
                                                     GFXImageFormat::RG32F,
                                                     GFXDataFormat::FLOAT_32);
                depthMapDescriptor.setLayerCount(Config::Lighting::MAX_SPLITS_PER_LIGHT *
                                                 Config::Lighting::MAX_SHADOW_CASTING_LIGHTS);
                depthMapDescriptor.setSampler(depthMapSampler);

                SamplerDescriptor depthSampler;
                depthSampler.setFilters(TextureFilter::NEAREST);
                depthSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
                depthSampler._useRefCompare = true;

                TextureDescriptor depthDescriptor(TextureType::TEXTURE_2D_ARRAY,
                                                  GFXImageFormat::DEPTH_COMPONENT,
                                                  GFXDataFormat::UNSIGNED_INT);
                depthDescriptor.setLayerCount(Config::Lighting::MAX_SPLITS_PER_LIGHT *
                                              Config::Lighting::MAX_SHADOW_CASTING_LIGHTS);
                depthDescriptor.setSampler(depthSampler);

                vectorImpl<RTAttachmentDescriptor> att = {
                    { depthMapDescriptor, RTAttachment::Type::Colour },
                    { depthDescriptor, RTAttachment::Type::Depth },
                };

                RenderTargetDescriptor desc = {};
                desc._name = "CSM_ShadowMap";
                desc._resolution = resolutions[i];
                desc._attachmentCount = to_U32(att.size());
                desc._attachments = att.data();

                crtTarget = context.allocateRT(RenderTargetUsage::SHADOW, desc);

            } break;

            case ShadowType::CUBEMAP: {
                // Default filters, LINEAR is OK for this
                SamplerDescriptor depthMapSampler;
                depthMapSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
                depthMapSampler.setFilters(TextureFilter::LINEAR);
                depthMapSampler._useRefCompare = true;  //< Use compare function
                depthMapSampler._cmpFunc = ComparisonFunction::LEQUAL;  //< Use less or equal

                TextureDescriptor depthMapDescriptor(TextureType::TEXTURE_CUBE_ARRAY,
                                                     GFXImageFormat::DEPTH_COMPONENT,
                                                     GFXDataFormat::UNSIGNED_INT);
                depthMapDescriptor.setSampler(depthMapSampler);
                depthMapDescriptor.setLayerCount(Config::Lighting::MAX_SHADOW_CASTING_LIGHTS);

                vectorImpl<RTAttachmentDescriptor> att = {
                    { depthMapDescriptor, RTAttachment::Type::Depth },
                };

                RenderTargetDescriptor desc = {};
                desc._name = "Cube_ShadowMap";
                desc._resolution = resolutions[i];
                desc._attachmentCount = to_U32(att.size());
                desc._attachments = att.data();

                crtTarget = context.allocateRT(RenderTargetUsage::SHADOW, desc);
            } break;
        };

        _depthMapUsage[i].fill(false);
    }
}

void ShadowMap::clearShadowMaps(GFXDevice& context) {
    for (U32 i = 0; i < to_base(ShadowType::COUNT); ++i) {
        context.renderTarget(RenderTargetID(RenderTargetUsage::SHADOW, i), nullptr);
    }
}

void ShadowMap::bindShadowMaps(GFXDevice& context) {
    for (U8 i = 0; i < to_base(ShadowType::COUNT); ++i) {
        RTAttachment::Type attachment
            = static_cast<ShadowType>(i) == ShadowType::LAYERED
                                          ? RTAttachment::Type::Colour
                                          : RTAttachment::Type::Depth;

        U8 bindSlot = LightPool::getShadowBindSlotOffset(static_cast<ShadowType>(i));
        context.renderTarget(RenderTargetID(RenderTargetUsage::SHADOW, i)).bind(bindSlot, attachment, 0);
    }
}

void ShadowMap::clearShadowMapBuffers(GFXDevice& context) {
    for (U8 i = 0; i < to_base(ShadowType::COUNT); ++i) {
        context.renderTarget(RenderTargetID(RenderTargetUsage::SHADOW, i)).clear(RenderTarget::defaultPolicy());
    }
}

U16 ShadowMap::findDepthMapLayer(ShadowType shadowType) {
    LayerUsageMask& usageMask = _depthMapUsage[to_U32(shadowType)];
    U16 layer = std::numeric_limits<U16>::max();
    for (U16 i = 0; i < to_base(Config::Lighting::MAX_SHADOW_CASTING_LIGHTS); ++i) {
        if (usageMask[i] == false) {
            layer = i;
            break;
        }
    }

    return layer;
}

RenderTargetID ShadowMap::getDepthMapID() {
    return RenderTargetID(RenderTargetUsage::SHADOW, to_U32(getShadowMapType()));
}

const RenderTargetID ShadowMap::getDepthMapID() const {
    return RenderTargetID(RenderTargetUsage::SHADOW, to_U32(getShadowMapType()));
}

RenderTarget& ShadowMap::getDepthMap() {
    return _context.renderTarget(getDepthMapID());
}

const RenderTarget& ShadowMap::getDepthMap() const {
    return _context.renderTarget(getDepthMapID());
}

void ShadowMap::commitDepthMapLayer(ShadowType shadowType, U32 layer) {
    DIVIDE_ASSERT(layer < Config::Lighting::MAX_SHADOW_CASTING_LIGHTS,
        "ShadowMap::commitDepthMapLayer error: Insufficient texture layers for shadow mapping");
    LayerUsageMask& usageMask = _depthMapUsage[to_U32(shadowType)];
    usageMask[layer] = true;
}

bool ShadowMap::freeDepthMapLayer(ShadowType shadowType, U32 layer) {
    DIVIDE_ASSERT(layer < Config::Lighting::MAX_SHADOW_CASTING_LIGHTS,
        "ShadowMap::freeDepthMapLayer error: Invalid layer value");
    LayerUsageMask& usageMask = _depthMapUsage[to_U32(shadowType)];
    usageMask[layer] = false;

    return true;
}

ShadowMapInfo::ShadowMapInfo(Light* light)
    : _light(light),
      _shadowMap(nullptr)
{

    _numLayers = 1;
}

ShadowMapInfo::~ShadowMapInfo()
{
    MemoryManager::DELETE(_shadowMap);
}

ShadowMap* ShadowMapInfo::createShadowMap(GFXDevice& context, const SceneRenderState& renderState, const ShadowCameraPool& shadowCameras) {
    if (_shadowMap) {
        return _shadowMap;
    }

    if (!_light->castsShadows()) {
        return nullptr;
    }

    switch (_light->getLightType()) {
        case LightType::POINT: {
            _numLayers = 6;
            _shadowMap = MemoryManager_NEW CubeShadowMap(context, _light, shadowCameras);
        } break;
        case LightType::DIRECTIONAL: {
            DirectionalLight* dirLight = static_cast<DirectionalLight*>(_light);
            _numLayers = dirLight->csmSplitCount();
            _shadowMap = MemoryManager_NEW CascadedShadowMaps(context, _light, shadowCameras, _numLayers);
        } break;
        case LightType::SPOT: {
            _shadowMap = MemoryManager_NEW SingleShadowMap(context, _light, shadowCameras);
        } break;
        default:
            break;
    };

    _shadowMap->init(this);

    return _shadowMap;
}

};

Commits for Divide-Framework/trunk/Source Code/Rendering/Lighting/ShadowMapping/ShadowMap.cpp

Diff revisions: vs.
Revision Author Commited Message
911 Diff Diff IonutCava picture IonutCava Fri 30 Jun, 2017 21:08:09 +0000

[Ionut]
- Use RTAttachmentDescriptor and RenderTargetDescriptor to initialise render targets.
— All formats and attachments need to be specified upfront!

909 Diff Diff IonutCava picture IonutCava Thu 29 Jun, 2017 23:57:18 +0000

[Ionut]
- Rework RenderTarget class
— RTAttachment now deal with textures directly
— RT size needs to be specified upfront
- Application class is no longer a Singleton but passed around in the PlatformContext

907 Diff Diff IonutCava picture IonutCava Wed 28 Jun, 2017 23:16:40 +0000

[Ionut]
- Rework texture loading, mip map generation, sampler usage, etc
- ResourceDescriptor for textures now ONLY takes a TextureDescriptor as a property descriptor. Sampler must be assigned to the tex descriptor:
— ToDo: Separate samplers completely from textures and assign them from a pool to textures at bind time

899 Diff Diff IonutCava picture IonutCava Fri 23 Jun, 2017 15:37:44 +0000

[IonutCava]
- Move sRGB conversion and fog calculation to post-processing stages (fog in postProcessing using the depth buffer and sRGB in the presentToScreen step)
- Split the bloom.glsl file into multiple shaders: bloom, tonemap and luminanceCalc for ease of use purposes
- Rework RenderBin sorting: sort translucent/transparent nodes by material in depth passes
- Sort order is no longer part of RenderBin for better control

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

883 Diff Diff IonutCava picture IonutCava Fri 24 Mar, 2017 17:15:58 +0000

[IonutCava]
- More terrain work
- Removed GS-based multi pass for shadows. Replaced with a regular multi-draw submit system (ease-of-use reasons)

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.

847 Diff Diff IonutCava picture IonutCava Sun 05 Feb, 2017 21:16:24 +0000

[IonutCava]
- Code rot fixes:
— Terrain loading (8 bit heightmap only for now)
— Terrain rendering
— Water Plane loading and rendering
— Reflection and Refraction rendering passes (hack for now: need environment cubemaps as separate from reflect/refract maps for planar rendering)
-— ToDo: Separate Reflection / Refraction render targets to Planar R/R and Cube R/R. Environment probes should use Cube Reflections, Water should use planar reflection targets.
- Added a way to register debug views: texture, shader and params. Viewports get automatically scaled and positioned
— ToDo: Add a hover-preview feature
— ToDo: Add a click-lock preview feature

830 IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper