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
#include "Headers/EnvironmentProbe.h"

#include "Dynamics/Entities/Headers/Impostor.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Platform/Video/Headers/IMPrimitive.h"
#include "Platform/Video/Headers/RenderStateBlock.h"
#include "Platform/Video/Textures/Headers/Texture.h"
#include "Platform/Video/Shaders/Headers/ShaderProgram.h"

namespace Divide {

namespace {
    U32 g_maxEnvironmentProbes = 32;
};

vectorImpl<bool> EnvironmentProbe::s_availableSlices;
RenderTargetHandle EnvironmentProbe::s_reflection;

EnvironmentProbe::EnvironmentProbe(ProbeType type) :
    GUIDWrapper(),
    _type(type),
    _updateRate(1),
    _currentUpdateCall(0),
    _aabb(vec3<F32>(-1), vec3<F32>(1))
{
    assert(!s_availableSlices.empty());
        
    _currentArrayIndex = allocateSlice();

    RenderStateBlock primitiveStateBlock;
    _boundingBoxPrimitive = GFX_DEVICE.newIMP();
    _boundingBoxPrimitive->name(Util::StringFormat("EnvironmentProbe_%d", getGUID()));
    _boundingBoxPrimitive->stateHash(primitiveStateBlock.getHash());

    _impostor = CreateResource<ImpostorSphere>(ResourceDescriptor(Util::StringFormat("EnvironmentProbeImpostor_%d", getGUID())));
    _impostor->setRadius(1.0f);

    _impostorShader = CreateResource<ShaderProgram>(ResourceDescriptor("ImmediateModeEmulation.EnvironmentProbe"));
}

EnvironmentProbe::~EnvironmentProbe()
{
    s_availableSlices[_currentArrayIndex] = false;
    _boundingBoxPrimitive->clear();}

void EnvironmentProbe::onStartup() {
    s_availableSlices.resize(g_maxEnvironmentProbes, false);
    // Reflection Targets
    SamplerDescriptor reflectionSampler;
    reflectionSampler.setFilters(TextureFilter::NEAREST);
    reflectionSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
    reflectionSampler.toggleMipMaps(false);
    TextureDescriptor environmentDescriptor(TextureType::TEXTURE_CUBE_MAP,
                                            GFXImageFormat::RGB8,
                                            GFXDataFormat::UNSIGNED_BYTE);
    environmentDescriptor.setSampler(reflectionSampler);
    environmentDescriptor.setLayerCount(g_maxEnvironmentProbes);


    TextureDescriptor depthDescriptor(TextureType::TEXTURE_CUBE_MAP,
                                      GFXImageFormat::DEPTH_COMPONENT,
                                      GFXDataFormat::UNSIGNED_INT);

    depthDescriptor.setSampler(reflectionSampler);

    RenderTargetHandle tempHandle;
    s_reflection = GFX_DEVICE.allocateRT(RenderTargetUsage::ENVIRONMENT, "EnviromentProbe");
    s_reflection._rt->addAttachment(environmentDescriptor, RTAttachment::Type::Colour, 0);
    s_reflection._rt->addAttachment(depthDescriptor, RTAttachment::Type::Depth, 0);
    s_reflection._rt->create(Config::REFLECTION_TARGET_RESOLUTION);
    s_reflection._rt->setClearColour(RTAttachment::Type::COUNT, 0, DefaultColours::WHITE());
}

void EnvironmentProbe::onShutdown() {
    GFX_DEVICE.deallocateRT(s_reflection);
}

U32 EnvironmentProbe::allocateSlice() {
    U32 i = 0;
    for (; i < g_maxEnvironmentProbes; ++i) {
        if (!s_availableSlices[i]) {
            s_availableSlices[i] = true;
            break;
        }
    }

    return i;
}

void EnvironmentProbe::refresh() {
    if (++_currentUpdateCall % _updateRate == 0) {
        GFX_DEVICE.generateCubeMap(*s_reflection._rt,
                                   _currentArrayIndex,
                                   _aabb.getCenter(),
                                   vec2<F32>(0.1f, (_aabb.getMax() - _aabb.getCenter()).length()),
                                   RenderStage::REFLECTION,
                                   getRTIndex());
        _currentUpdateCall = 0;
    }
    _boundingBoxPrimitive->paused(true);
}

void EnvironmentProbe::setBounds(const vec3<F32>& min, const vec3<F32>& max) {
    _aabb.set(min, max);
    updateInternal();
}

void EnvironmentProbe::setBounds(const vec3<F32>& center, F32 radius) {
    _aabb.createFromSphere(center, radius);
    updateInternal();
}

void EnvironmentProbe::setUpdateRate(U8 rate) {
    _updateRate = rate;
}

RenderTargetHandle EnvironmentProbe::reflectionTarget() {
    return s_reflection;
}

F32 EnvironmentProbe::distanceSqTo(const vec3<F32>& pos) const {
    return _aabb.getCenter().distanceSquared(pos);
}

U32 EnvironmentProbe::getRTIndex() const {
    return _currentArrayIndex;
}

void EnvironmentProbe::debugDraw(RenderSubPassCmds& subPassesInOut) {
    static RenderStateBlock stateBlock;

    _boundingBoxPrimitive->paused(false);

    const Texture_ptr& reflectTex = s_reflection._rt->getAttachment(RTAttachment::Type::Colour, 0).asTexture();

    VertexBuffer* vb = _impostor->getGeometryVB();
    GenericDrawCommand cmd(PrimitiveType::TRIANGLE_STRIP, 0, vb->getIndexCount());
    cmd.stateHash(stateBlock.getHash());
    cmd.shaderProgram(_impostorShader);
    cmd.sourceBuffer(vb);

    RenderSubPassCmd newSubPass;
    newSubPass._textures.addTexture(TextureData(reflectTex->getTextureType(),
                                                reflectTex->getHandle(),
                                                to_const_ubyte(ShaderProgram::TextureUsage::REFLECTION)));
    newSubPass._commands.push_back(cmd);
    newSubPass._commands.push_back(_boundingBoxPrimitive->toDrawCommand());
    subPassesInOut.push_back(newSubPass);
}

void EnvironmentProbe::updateInternal() {
    _boundingBoxPrimitive->fromBox(_aabb.getMin(), _aabb.getMax(), vec4<U8>(255, 255, 255, 255));
    const vec3<F32>& bbPos = _aabb.getCenter();
    _impostorShader->Uniform("dvd_WorldMatrixOverride", mat4<F32>(bbPos.x, bbPos.y, bbPos.z));
    _impostorShader->Uniform("dvd_LayerIndex", _currentArrayIndex);
}

}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/Rendering/EnvironmentProbe.cpp

Diff revisions: vs.
Revision Author Commited Message
788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

785 Diff Diff IonutCava picture IonutCava Tue 18 Oct, 2016 16:14:27 +0000

[IonutCava]
- Added better rendering debugging functionality (named render targets. Helps with GPU Profiling tools)
- Limit clip planes to the number of frustum planes (that should be enough and it also simplifies the code)

777 Diff Diff IonutCava picture IonutCava Fri 07 Oct, 2016 16:14:48 +0000

[IonutCava]
- Continue to implement CommandBuffer / RenderPass / RenderSubPass system.
— Rendering artefacts are still present.

776 Diff Diff IonutCava picture IonutCava Thu 06 Oct, 2016 15:57:56 +0000

[IonutCava]
- Improve the CommandBuffer system to include the notion of a RenderPass /RenderSubPass with output render target info stored in them (not used yet)

775 Diff Diff IonutCava picture IonutCava Wed 05 Oct, 2016 15:54:54 +0000

[IonutCava]
- Cleanup RenderTarget <-> RTAttachment relation
- Multisampled render target state is now deduced based on attachments used

774 Diff Diff IonutCava picture IonutCava Wed 05 Oct, 2016 13:42:22 +0000

[IonutCava]
- Removed glRTAttachment. Placed texture ptr in parent RTAttachment.
- Removed depth buffer auto generation for framebuffers. All attachments must now be explicit.

768 Diff Diff IonutCava picture IonutCava Fri 09 Sep, 2016 14:38:51 +0000

[IonutCava]
- Refactored framework entry points (init, step, shutdown) to allow the addition of hot code reloading in the future

764 Diff Diff IonutCava picture IonutCava Mon 22 Aug, 2016 15:38:11 +0000

[IonutCava]
- Add some explicit include for RenderStateBlock as it will be removed from the RenderAPIWrapper.h file

760 Diff Diff IonutCava picture IonutCava Tue 09 Aug, 2016 16:30:12 +0000

[IonutCava]
- Complete rewrite of RenderPass system:
— Allow every rendering pass (reflection, environment, etc) to use the same rendering system: occlusion culling, light culling, etc
— Define render passes by a param based token similar to modern API systems (DX12/Vulkan)
— W.I.P.: Depth rendering slightly wrong; Environment Mapping ,reflection and refraction rendering currently disabled
- Add compile time variables for DEBUG/PROFILE/RELEASE builds to validate all code for every build

749 IonutCava picture IonutCava Mon 04 Jul, 2016 16:01:34 +0000

[IonutCava]
- Added initial environment mapping code:
— Allow adding, per scene, of multiple environment probes that generate cube map reflections
— Support 2 types of probes: infinite (such as sky only probes) and local. Local probes use an AABB that will later be used for parallax correction
— Probes are held in a scene specific pool, can be updated at different rates and their results can be passed on to materials (if materials don’t need specific reflection systems such as for water or mirrors)
- ToDo:
— Blend between probes (currently, only the closes one is selected)
— Reduce VRAM usage
— Set as fallback for screen space reflections