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
#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/Headers/Predefined/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),
      _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(RTAttachment::Type::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_const_uint(TexOperatorBindPoint::TEX_BIND_POINT_SCREEN),
                        to_const_uint(TexOperatorBindPoint::TEX_BIND_POINT_NOISE),
                        to_const_uint(TexOperatorBindPoint::TEX_BIND_POINT_BORDER),
                        to_const_uint(TexOperatorBindPoint::TEX_BIND_POINT_UNDERWATER)).c_str());

    _postProcessingShader = CreateResource<ShaderProgram>(cache, postFXShader);
    _postProcessingShader->Uniform("_noiseTile", 0.05f);
    _postProcessingShader->Uniform("_noiseFactor", 0.02f);
    _postProcessingShader->Uniform("_fadeActive", 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);
    
    ResourceDescriptor textureWaterCaustics("Underwater Caustics");
    textureWaterCaustics.setResourceName("terrain_water_NM.jpg");
    textureWaterCaustics.setResourceLocation(Paths::g_assetsLocation + Paths::g_imagesLocation);
    textureWaterCaustics.setEnumValue(to_const_uint(TextureType::TEXTURE_2D));
    _underwaterTexture = CreateResource<Texture>(cache, textureWaterCaustics);

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

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

     _drawCommand.primitiveType(PrimitiveType::TRIANGLES);
     _drawCommand.drawCount(1);
     _drawCommand.shaderProgram(_postProcessingShader);

     _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(GFXDevice& context) {
    _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];

    GFX::Scoped2DRendering scoped2D(context);
    _preRenderBatch->execute(_filterStackCount);
    _postProcessingShader->bind();
    _postProcessingShader->SetSubroutines(ShaderType::FRAGMENT, _shaderFunctionSelection);

    _preRenderBatch->bindOutput(to_const_ubyte(TexOperatorBindPoint::TEX_BIND_POINT_SCREEN));
    _underwaterTexture->bind(to_const_ubyte(TexOperatorBindPoint::TEX_BIND_POINT_UNDERWATER));
    _noise->bind(to_const_ubyte(TexOperatorBindPoint::TEX_BIND_POINT_NOISE));
    _screenBorder->bind(to_const_ubyte(TexOperatorBindPoint::TEX_BIND_POINT_BORDER));

    RenderTarget& screenRT = _gfx->renderTarget(RenderTargetID(RenderTargetUsage::SCREEN));

    screenRT.begin(_postFXTarget);
        _drawCommand.stateHash(_gfx->getDefaultStateBlock(true));
        _gfx->draw(_drawCommand);
    screenRT.end();
}

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

        _postProcessingShader->Uniform("randomCoeffNoise", _randomNoiseCoefficient);
        _postProcessingShader->Uniform("randomCoeffFlash", _randomFlashCoefficient);
    }

    _preRenderBatch->idle(config);
}

void PostFX::update(const U64 deltaTime) {
    if (_fadeActive) {
        _currentFadeTimeMS += Time::MicrosecondsToMilliseconds<D64>(deltaTime);
        F32 fadeStrength = to_float(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>(deltaTime);
            }
        }

        _postProcessingShader->Uniform("_fadeStrength", fadeStrength);
        
        _fadeActive = fadeStrength > EPSILON_D64;
        if (!_fadeActive) {
            _postProcessingShader->Uniform("_fadeActive", false);
            if (_fadeInComplete) {
                _fadeInComplete();
                _fadeInComplete = DELEGATE_CBK<void>();
            }
        }
    }
}

void PostFX::setFadeOut(const vec4<U8>& targetColour, D64 durationMS, D64 waitDurationMS, DELEGATE_CBK<void> onComplete) {
    _postProcessingShader->Uniform("_fadeColour", Util::ToFloatColour(targetColour));
    _targetFadeTimeMS = durationMS;
    _currentFadeTimeMS = 0.0;
    _fadeWaitDurationMS = waitDurationMS;
    _fadeOut = true;
    _fadeActive = true;
    _postProcessingShader->Uniform("_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;
    _postProcessingShader->Uniform("_fadeActive", true);
    _fadeInComplete = onComplete;
}

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

void PostFX::setFadeOutIn(const vec4<U8>& 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
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

831 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 23:52:34 +0000

[IonutCava]
- More path related updates

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

829 Diff Diff IonutCava picture IonutCava Tue 24 Jan, 2017 17:28:39 +0000

[IonutCava]
- Separate resourceLocation for resources into resourceLocation and resourceName.
- More XML config loading refactoring

822 Diff Diff IonutCava picture IonutCava Tue 17 Jan, 2017 22:47:06 +0000

[IonutCava]
- Fix postFX per player rendering
- Fix player remove code (wasn’t removing nodes)

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

791 Diff Diff IonutCava picture IonutCava Thu 17 Nov, 2016 16:46:19 +0000

[IonutCava]
- Reworked PostFX filter list to allow multiple enable calls to the same effect (stack based)
- Added noise, vignette and underwater effects to the filter list available for PostFX
- Added better underwater camera detection

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)

765 IonutCava picture IonutCava Mon 22 Aug, 2016 15:48:12 +0000

[IonutCava]
- Descriptor/State cleanup part 1/3: RTDrawDescriptor refactoring