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
#include "stdafx.h"

#include "Headers/EnvironmentProbe.h"

#include "Scenes/Headers/Scene.h"
#include "Dynamics/Entities/Headers/Impostor.h"
#include "Core/Headers/StringHelper.h"
#include "Core/Headers/PlatformContext.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 {
    U16 g_maxEnvironmentProbes = 32;
};

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

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

    RenderStateBlock primitiveStateBlock;

    PipelineDescriptor pipelineDescriptor;
    pipelineDescriptor._stateHash = primitiveStateBlock.getHash();

    _boundingBoxPrimitive = _context.newIMP();
    _boundingBoxPrimitive->name(Util::StringFormat("EnvironmentProbe_%d", getGUID()));
    _boundingBoxPrimitive->pipeline(_context.newPipeline(pipelineDescriptor));

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

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

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

void EnvironmentProbe::onStartup(GFXDevice& context) {
    s_availableSlices.resize(g_maxEnvironmentProbes, false);
    // Reflection Targets
    SamplerDescriptor reflectionSampler;
    reflectionSampler.setFilters(TextureFilter::NEAREST);
    reflectionSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);

    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);

    vectorImpl<RTAttachmentDescriptor> att = {
        { environmentDescriptor, RTAttachmentType::Colour, 0, DefaultColours::WHITE },
        { depthDescriptor, RTAttachmentType::Depth },
    };

    RenderTargetDescriptor desc = {};
    desc._name = "EnvironmentProbe";
    desc._resolution = vec2<U16>(Config::REFLECTION_TARGET_RESOLUTION_ENVIRONMENT_PROBE);
    desc._attachmentCount = to_U8(att.size());
    desc._attachments = att.data();

    s_reflection = context.renderTargetPool().allocateRT(RenderTargetUsage::ENVIRONMENT, desc);
}

void EnvironmentProbe::onShutdown(GFXDevice& context)
{
    context.renderTargetPool().deallocateRT(s_reflection);
}

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

    return i;
}

void EnvironmentProbe::refresh(GFX::CommandBuffer& bufferInOut) {
    if (++_currentUpdateCall % _updateRate == 0) {
        _context.generateCubeMap(s_reflection._targetID,
                                 _currentArrayIndex,
                                 _aabb.getCenter(),
                                 vec2<F32>(0.1f, (_aabb.getMax() - _aabb.getCenter()).length()),
                                 RenderStagePass(RenderStage::REFLECTION, RenderPassType::COLOUR_PASS),
                                 getRTIndex(),
                                 bufferInOut);
        _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(GFX::CommandBuffer& bufferInOut) {
    _boundingBoxPrimitive->paused(false);

    const Texture_ptr& reflectTex = s_reflection._rt->getAttachment(RTAttachmentType::Colour, 0).texture();

    VertexBuffer* vb = _impostor->getGeometryVB();

    PipelineDescriptor pipelineDescriptor;
    pipelineDescriptor._stateHash = _context.getDefaultStateBlock(false);
    pipelineDescriptor._shaderProgram = _impostorShader;

    GFX::BindPipelineCommand bindPipelineCmd;
    bindPipelineCmd._pipeline = &_context.newPipeline(pipelineDescriptor);
    GFX::BindPipeline(bufferInOut, bindPipelineCmd);

    GFX::BindDescriptorSetsCommand descriptorSetCmd;
    descriptorSetCmd._set._textureData.addTexture(TextureData(reflectTex->getTextureType(),
                                                              reflectTex->getHandle()),
                                                  to_U8(ShaderProgram::TextureUsage::REFLECTION_CUBE));
    GFX::BindDescriptorSets(bufferInOut, descriptorSetCmd);

    GFX::SendPushConstantsCommand pushConstants;
    PushConstants& constants = pushConstants._constants;
    const vec3<F32>& bbPos = _aabb.getCenter();
    constants.set("dvd_WorldMatrixOverride", PushConstantType::MAT4, mat4<F32>(bbPos.x, bbPos.y, bbPos.z));
    constants.set("dvd_LayerIndex", PushConstantType::UINT, to_U32(_currentArrayIndex));
    GFX::SendPushConstants(bufferInOut, pushConstants);

    GenericDrawCommand cmd(PrimitiveType::TRIANGLE_STRIP, 0, vb->getIndexCount());
    cmd.sourceBuffer(vb);

    GFX::DrawCommand drawCommand;
    drawCommand._drawCommands.push_back(cmd);
    GFX::AddDrawCommands(bufferInOut, drawCommand);

    bufferInOut.add(_boundingBoxPrimitive->toCommandBuffer());
}

void EnvironmentProbe::updateInternal() {
    _boundingBoxPrimitive->fromBox(_aabb.getMin(), _aabb.getMax(), UColour(255, 255, 255, 255));
}

}; //namespace Divide

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

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

953 Diff Diff IonutCava picture IonutCava Sat 28 Oct, 2017 21:57:10 +0000

[Ionut]
- Fix blinking issue by caching and hashing pipelines to that we don’t overwrite pipelines used by other elements

947 Diff Diff IonutCava picture IonutCava Sun 08 Oct, 2017 22:36:54 +0000

[Ionut]
- Refactor command buffer pooling
- Remove all static commands in an attempt to fix rendering issues. Profile and rework later.

938 Diff Diff IonutCava picture IonutCava Wed 16 Aug, 2017 21:35:55 +0000

[Ionut]
- Separate texture binding slot from TextureData
- Binding slot is now a property of the descriptor set
- Remove bind() calls from texture’s public interface
ToDo
- Add image layer binding options (addTextureLayer(data, binding, layer, ...)

933 Diff Diff IonutCava picture IonutCava Tue 15 Aug, 2017 00:02:05 +0000

[Ionut]
- CommandBuffer Part 2/3: code compiles and splash screen renders and application doesn’t crash (still has drawing issues)

929 Diff Diff IonutCava picture IonutCava Sat 12 Aug, 2017 17:34:44 +0000

[Ionut]
- More work on finishing up new PushConstant / Command buffer rendering system:
— Everything compiles and runs but with graphical and performance issues

924 Diff Diff IonutCava picture IonutCava Wed 02 Aug, 2017 20:29:19 +0000

[Ionut]
- Rework RenderTarget binding. Now access is only available via the GFXRTPool
- Split RenderPassManager::doCustomPass into multiple steps for easier debugging and cleaner code
- Other small code cleanups

912 Diff Diff IonutCava picture IonutCava Sun 02 Jul, 2017 23:42:39 +0000

[Ionut]
- Add experimental Weighted Blended Order Independed Transparency (ref: http://casual-effects.blogspot.co.uk/2015/03/implemented-weighted-blended-order.html)
— Add per drawbuffer blend
— All translucent renderbin items go via the new OIT 2-step rendering: accumulation and composition
- Make sure we have proper blend enabled for text rendering
- Add a primitive form of PushConstants (unused yet. Emulated via Uniform calls)
- Fix bug with XMLParser not using case insensitive key lookups

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 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