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
271
272
273
274
275
276
277
278
279
280
281
#include "stdafx.h"

#include "Headers/PostFX.h"
#include "Headers/PreRenderOperator.h"

#include "Core/Headers/ParamHandler.h"
#include "Core/Headers/StringHelper.h"
#include "Core/Time/Headers/ApplicationTimer.h"
#include "Core/Resources/Headers/ResourceCache.h"

#include "Managers/Headers/SceneManager.h"
#include "Rendering/Camera/Headers/Camera.h"
#include "Geometry/Shapes/Predefined/Headers/Quad3D.h"
#include "Platform/Video/Buffers/RenderTarget/Headers/RenderTarget.h"

namespace Divide {

PostFX::PostFX()
    : _preRenderBatch(nullptr),
      _screenBorder(nullptr),
      _noise(nullptr),
      _randomNoiseCoefficient(0.0f),
      _randomFlashCoefficient(0.0f),
      _noiseTimer(0.0),
      _tickInterval(1),
      _postProcessingShader(nullptr),
      _underwaterTexture(nullptr),
      _gfx(nullptr),
      _drawPipeline(nullptr),
      _filtersDirty(true),
      _currentFadeTimeMS(0.0),
      _targetFadeTimeMS(0.0),
      _fadeWaitDurationMS(0.0),
      _fadeOut(false),
      _fadeActive(false)
{
    ParamHandler::instance().setParam<bool>(_ID("postProcessing.enableVignette"), false);

    _postFXTarget.disableState(RTDrawDescriptor::State::CLEAR_DEPTH_BUFFER);
    _postFXTarget.drawMask().disableAll();
    _postFXTarget.drawMask().setEnabled(RTAttachmentType::Colour, 0, true);

    _filterStackCount.fill(0);
}

PostFX::~PostFX()
{
    if (_preRenderBatch) {
        _preRenderBatch->destroy();
        MemoryManager::SAFE_DELETE(_preRenderBatch);
    }
}

void PostFX::init(GFXDevice& context, ResourceCache& cache) {
    Console::printfn(Locale::get(_ID("START_POST_FX")));
    _gfx = &context;
    _preRenderBatch = MemoryManager_NEW PreRenderBatch(context, cache);

    ResourceDescriptor postFXShader("postProcessing");
    postFXShader.setThreadedLoading(false);
    postFXShader.setPropertyList(
    Util::StringFormat("TEX_BIND_POINT_SCREEN %d, "
                       "TEX_BIND_POINT_NOISE %d, "
                       "TEX_BIND_POINT_BORDER %d, "
                       "TEX_BIND_POINT_UNDERWATER %d",
                        to_base(TexOperatorBindPoint::TEX_BIND_POINT_SCREEN),
                        to_base(TexOperatorBindPoint::TEX_BIND_POINT_NOISE),
                        to_base(TexOperatorBindPoint::TEX_BIND_POINT_BORDER),
                        to_base(TexOperatorBindPoint::TEX_BIND_POINT_UNDERWATER)).c_str());

    _postProcessingShader = CreateResource<ShaderProgram>(cache, postFXShader);
    _drawConstants.set("_noiseTile", PushConstantType::FLOAT, 0.05f);
    _drawConstants.set("_noiseFactor", PushConstantType::FLOAT, 0.02f);
    _drawConstants.set("_fadeActive", PushConstantType::BOOL, false);
    
    _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(ShaderType::FRAGMENT, "Vignette"));  // 0
    _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(ShaderType::FRAGMENT, "Noise"));  // 1
    _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(ShaderType::FRAGMENT, "screenUnderwater"));  // 2
    _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(ShaderType::FRAGMENT, "screenNormal"));  // 3
    _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(ShaderType::FRAGMENT, "ColourPassThrough"));  // 4

    _shaderFunctionSelection.resize(_postProcessingShader->GetSubroutineUniformCount(ShaderType::FRAGMENT), 0);
    

    SamplerDescriptor defaultSampler;
    defaultSampler.setWrapMode(TextureWrap::REPEAT);
    
    TextureDescriptor texDescriptor(TextureType::TEXTURE_2D);
    texDescriptor.setSampler(defaultSampler);

    ResourceDescriptor textureWaterCaustics("Underwater Caustics");
    textureWaterCaustics.setResourceName("terrain_water_NM.jpg");
    textureWaterCaustics.setResourceLocation(Paths::g_assetsLocation + Paths::g_imagesLocation);
    textureWaterCaustics.setPropertyDescriptor(texDescriptor);
    _underwaterTexture = CreateResource<Texture>(cache, textureWaterCaustics);

     ResourceDescriptor noiseTexture("noiseTexture");
     noiseTexture.setResourceName("bruit_gaussien.jpg");
     noiseTexture.setResourceLocation(Paths::g_assetsLocation + Paths::g_imagesLocation);
     noiseTexture.setPropertyDescriptor(texDescriptor);
     _noise = CreateResource<Texture>(cache, noiseTexture);

     ResourceDescriptor borderTexture("borderTexture");
     borderTexture.setResourceName("vignette.jpeg");
     borderTexture.setResourceLocation(Paths::g_assetsLocation + Paths::g_imagesLocation);
     borderTexture.setPropertyDescriptor(texDescriptor);
     _screenBorder = CreateResource<Texture>(cache, borderTexture);

     PipelineDescriptor pipelineDescriptor;
     pipelineDescriptor._stateHash = context.get2DStateBlock();
     pipelineDescriptor._shaderProgram = _postProcessingShader;

     _drawCommand.primitiveType(PrimitiveType::TRIANGLES);
     _drawCommand.drawCount(1);
     _drawPipeline = &context.newPipeline(pipelineDescriptor);

     _preRenderBatch->init(RenderTargetID(RenderTargetUsage::SCREEN));

    _noiseTimer = 0.0;
    _tickInterval = 1.0f / 24.0f;
    _randomNoiseCoefficient = 0;
    _randomFlashCoefficient = 0;
}

void PostFX::updateResolution(U16 width, U16 height) {
    if ((_resolutionCache.width == width &&
         _resolutionCache.height == height)|| 
        width < 1 || height < 1)
    {
        return;
    }

    _resolutionCache.set(width, height);

    _preRenderBatch->reshape(width, height);
}

void PostFX::apply() {
    GFX::ScopedCommandBuffer sBuffer = GFX::allocateScopedCommandBuffer();

    GFX::CommandBuffer& buffer = sBuffer();

    if (_filtersDirty) {
        _shaderFunctionSelection[0] = _shaderFunctionList[getFilterState(FilterType::FILTER_VIGNETTE) ? 0 : 4];
        _shaderFunctionSelection[1] = _shaderFunctionList[getFilterState(FilterType::FILTER_NOISE) ? 1 : 4];
        _shaderFunctionSelection[2] = _shaderFunctionList[getFilterState(FilterType::FILTER_UNDERWATER) ? 2 : 3];

        PipelineDescriptor desc = _drawPipeline->descriptor();
        desc._shaderFunctions[ShaderType::FRAGMENT] = _shaderFunctionSelection;
        _drawPipeline = &_gfx->newPipeline(desc);
        _filtersDirty = false;
    }

    GFX::SetCameraCommand setCameraCommand;
    setCameraCommand._camera = Camera::utilityCamera(Camera::UtilityCamera::_2D);
    GFX::SetCamera(buffer, setCameraCommand);

    _preRenderBatch->execute(_filterStackCount, buffer);

    TextureData output = _preRenderBatch->getOutput();
    TextureData data0 = _underwaterTexture->getData();
    TextureData data1 = _noise->getData();
    TextureData data2 = _screenBorder->getData();

    RenderTarget& screenRT = _gfx->renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN));
    TextureData depthData = screenRT.getAttachment(RTAttachmentType::Depth, 0).texture()->getData();

    GFX::BeginRenderPassCommand beginRenderPassCmd;
    beginRenderPassCmd._target = RenderTargetID(RenderTargetUsage::SCREEN);
    beginRenderPassCmd._descriptor = _postFXTarget;
    beginRenderPassCmd._name = "DO_POSTFX_PASS";
    GFX::BeginRenderPass(buffer, beginRenderPassCmd);

    GFX::BindPipelineCommand bindPipelineCmd;
    bindPipelineCmd._pipeline = _drawPipeline;
    GFX::BindPipeline(buffer, bindPipelineCmd);

    GFX::SendPushConstantsCommand sendPushConstantsCmd;
    sendPushConstantsCmd._constants = _drawConstants;
    GFX::SendPushConstants(buffer, sendPushConstantsCmd);

    GFX::BindDescriptorSetsCommand bindDescriptorSetsCmd;
    bindDescriptorSetsCmd._set._textureData.addTexture(depthData, to_U8(ShaderProgram::TextureUsage::DEPTH));
    bindDescriptorSetsCmd._set._textureData.addTexture(output, to_U8(TexOperatorBindPoint::TEX_BIND_POINT_SCREEN));
    bindDescriptorSetsCmd._set._textureData.addTexture(data0, to_U8(TexOperatorBindPoint::TEX_BIND_POINT_UNDERWATER));
    bindDescriptorSetsCmd._set._textureData.addTexture(data1, to_U8(TexOperatorBindPoint::TEX_BIND_POINT_NOISE));
    bindDescriptorSetsCmd._set._textureData.addTexture(data2, to_U8(TexOperatorBindPoint::TEX_BIND_POINT_BORDER));
    GFX::BindDescriptorSets(buffer, bindDescriptorSetsCmd);

    GFX::DrawCommand drawCommand;
    drawCommand._drawCommands.push_back(_drawCommand);
    GFX::AddDrawCommands(buffer, drawCommand);

    GFX::EndRenderPassCommand endRenderPassCmd;
    GFX::EndRenderPass(buffer, endRenderPassCmd);

    _gfx->flushCommandBuffer(buffer);
}

void PostFX::idle(const Configuration& config) {
    // Update states
    if (getFilterState(FilterType::FILTER_NOISE)) {
        _noiseTimer += Time::ElapsedMilliseconds();
        if (_noiseTimer > _tickInterval) {
            _noiseTimer = 0.0;
            _randomNoiseCoefficient = Random(1000) * 0.001f;
            _randomFlashCoefficient = Random(1000) * 0.001f;
        }

        _drawConstants.set("randomCoeffNoise", PushConstantType::FLOAT, _randomNoiseCoefficient);
        _drawConstants.set("randomCoeffNoise", PushConstantType::FLOAT, _randomFlashCoefficient);
    }

    _preRenderBatch->idle(config);
}

void PostFX::update(const U64 deltaTimeUS) {
    if (_fadeActive) {
        _currentFadeTimeMS += Time::MicrosecondsToMilliseconds<D64>(deltaTimeUS);
        F32 fadeStrength = to_F32(std::min(_currentFadeTimeMS / _targetFadeTimeMS , 1.0));
        if (!_fadeOut) {
            fadeStrength = 1.0f - fadeStrength;
        }

        if (fadeStrength > 0.99) {
            if (_fadeWaitDurationMS < EPSILON_D64) {
                if (_fadeOutComplete) {
                    _fadeOutComplete();
                    _fadeOutComplete = DELEGATE_CBK<void>();
                }
            } else {
                _fadeWaitDurationMS -= Time::MicrosecondsToMilliseconds<D64>(deltaTimeUS);
            }
        }

        _drawConstants.set("_fadeStrength", PushConstantType::FLOAT, fadeStrength);
        
        _fadeActive = fadeStrength > EPSILON_D64;
        if (!_fadeActive) {
            _drawConstants.set("_fadeActive", PushConstantType::BOOL, false);
            if (_fadeInComplete) {
                _fadeInComplete();
                _fadeInComplete = DELEGATE_CBK<void>();
            }
        }
    }
}

void PostFX::setFadeOut(const UColour& targetColour, D64 durationMS, D64 waitDurationMS, DELEGATE_CBK<void> onComplete) {
    _drawConstants.set("_fadeColour", PushConstantType::VEC4, Util::ToFloatColour(targetColour));
    _drawConstants.set("_fadeActive", PushConstantType::BOOL, true);
    _targetFadeTimeMS = durationMS;
    _currentFadeTimeMS = 0.0;
    _fadeWaitDurationMS = waitDurationMS;
    _fadeOut = true;
    _fadeActive = true;
    _fadeOutComplete = onComplete;
}

// clear any fading effect currently active over the specified time interval
// set durationMS to instantly clear the fade effect
void PostFX::setFadeIn(D64 durationMS, DELEGATE_CBK<void> onComplete) {
    _targetFadeTimeMS = durationMS;
    _currentFadeTimeMS = 0.0;
    _fadeOut = false;
    _fadeActive = true;
    _drawConstants.set("_fadeActive", PushConstantType::BOOL, true);
    _fadeInComplete = onComplete;
}

void PostFX::setFadeOutIn(const UColour& targetColour, D64 durationFadeOutMS, D64 durationMS) {
    if (durationMS > 0.0) {
        setFadeOutIn(targetColour, durationMS * 0.5, durationMS * 0.5, durationFadeOutMS);
    }
}

void PostFX::setFadeOutIn(const UColour& targetColour, D64 durationFadeOutMS, D64 durationFadeInMS, D64 waitDurationMS) {
    setFadeOut(targetColour, durationFadeOutMS, waitDurationMS, [this, durationFadeInMS]() {setFadeIn(durationFadeInMS); });
}

};

Commits for Divide-Framework/trunk/Source Code/Rendering/PostFX/PostFX.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.

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

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

950 Diff Diff IonutCava picture IonutCava Sun 22 Oct, 2017 22:25:05 +0000

[Ionut]
- Revamp camera management
- More work on improving the command buffer system:
— Performance tweaks
— Validation
— Debug features
- Some code cleanup for Object3D stuff (preRender/onRender changes)

Still haven’t fixed random flashing issue

942 Diff Diff IonutCava picture IonutCava Wed 06 Sep, 2017 20:45:37 +0000

[Ionut]
- Add more DLLs required by CEGUI
- Move CommandBuffer allocation from GFXDevice to GFX namespace

941 Diff Diff IonutCava picture IonutCava Mon 04 Sep, 2017 20:55:02 +0000

[Ionut]
- Update CEGUI from static to dynamic config (some dlls might still be missing)
- Attempt to work around a weird string issue with the newest VS2017 update

940 Diff Diff IonutCava picture IonutCava Fri 18 Aug, 2017 16:18:44 +0000

[Ionut]
- Generate GFX::CommandBuffers only from GFX::CommandBufferPools.
— We shouldn’t be allocating any new GFX::CommandBuffers at runtime now.
— ToDo: Add a similar system for Pipelines
- Add 2 command buffer pools to GFXDevice: a primary one used for rendering and a large, secondary one used for command generation

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, ...)

934 Diff Diff IonutCava picture IonutCava Tue 15 Aug, 2017 16:15:55 +0000

[Ionut]
- Bug fixes for the new command buffer system

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