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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "Headers/PostFX.h"
#include "Headers/PreRenderStage.h"
#include "Headers/PreRenderOperator.h"
#include "Headers/PreRenderStageBuilder.h"

#include "Core/Headers/ParamHandler.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 "Hardware/Video/Headers/RenderStateBlock.h"
#include "Hardware/Video/Buffers/Framebuffer/Headers/Framebuffer.h"

namespace Divide {

PostFX::PostFX(): _underwaterTexture(nullptr),
    _anaglyphShader(nullptr),
    _postProcessingShader(nullptr),
    _noise(nullptr),
    _screenBorder(nullptr),
    _bloomFB(nullptr),
    _SSAO_FB(nullptr),
    _fxaaOP(nullptr),
    _dofOP(nullptr),
    _bloomOP(nullptr),
    _underwater(false),
    _depthPreview(false),
    _gfx(nullptr)
{
    PreRenderStageBuilder::createInstance();
	ParamHandler::getInstance().setParam<bool>("postProcessing.enableVignette", false);
	ParamHandler::getInstance().setParam<bool>("postProcessing.fullScreenDepthBuffer", false);
}

PostFX::~PostFX()
{

    if (_postProcessingShader) {
        RemoveResource(_postProcessingShader);

        if (_gfx->anaglyphEnabled()) {
            RemoveResource(_anaglyphShader);
        }

        if (_underwaterTexture) {
            RemoveResource(_underwaterTexture);
        }

        if (_enableNoise) {
            RemoveResource(_noise);
            RemoveResource(_screenBorder);
        }

        MemoryManager::DELETE(_bloomFB);
        MemoryManager::DELETE(_SSAO_FB);
    }

    PreRenderStageBuilder::getInstance().destroyInstance();
}

void PostFX::init(const vec2<U16>& resolution) {
    _resolutionCache = resolution;

    PRINT_FN(Locale::get("START_POST_FX"));
    ParamHandler& par = ParamHandler::getInstance();
    _gfx = &GFX_DEVICE;
    _enableBloom = par.getParam<bool>("postProcessing.enableBloom");
    _enableSSAO = par.getParam<bool>("postProcessing.enableSSAO");
    _enableDOF = par.getParam<bool>("postProcessing.enableDepthOfField");
    _enableNoise = par.getParam<bool>("postProcessing.enableNoise");
    _enableVignette = par.getParam<bool>("postProcessing.enableVignette");
    _enableFXAA = _gfx->FXAAEnabled();

    if (_gfx->postProcessingEnabled()) {
        ResourceDescriptor postFXShader("postProcessing");
        postFXShader.setThreadedLoading(false);
        std::stringstream ss;
        ss << ("TEX_BIND_POINT_SCREEN "     + Util::toString((U32)TEX_BIND_POINT_SCREEN) + ", ");
        ss << ("TEX_BIND_POINT_BLOOM "      + Util::toString((U32)TEX_BIND_POINT_BLOOM) + ", ");
        ss << ("TEX_BIND_POINT_SSAO "       + Util::toString((U32)TEX_BIND_POINT_SSAO) + ", ");
        ss << ("TEX_BIND_POINT_NOISE "      + Util::toString((U32)TEX_BIND_POINT_NOISE) + ", ");
        ss << ("TEX_BIND_POINT_BORDER "     + Util::toString((U32)TEX_BIND_POINT_BORDER) + ", ");
        ss << ("TEX_BIND_POINT_UNDERWATER " + Util::toString((U32)TEX_BIND_POINT_UNDERWATER));
        postFXShader.setPropertyList(stringAlg::toBase(ss.str()));
        _postProcessingShader = CreateResource<ShaderProgram>(postFXShader);

        ResourceDescriptor textureWaterCaustics("Underwater Caustics");
		textureWaterCaustics.setResourceLocation(par.getParam<stringImpl>("assetsLocation") + "/misc_images/terrain_water_NM.jpg");
        _underwaterTexture = CreateResource<Texture>(textureWaterCaustics);

        createOperators();

        _postProcessingShader->Uniform("_noiseTile", 0.05f);
        _postProcessingShader->Uniform("_noiseFactor", 0.02f);
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "Vignette")); //0
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "Noise"));    //1
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "Bloom"));    //2
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "SSAO"));     //3
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "screenUnderwater")); //4
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "screenNormal"));     //5
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "ColorPassThrough")); //6
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "outputScreen"));     //7
        _shaderFunctionList.push_back(_postProcessingShader->GetSubroutineIndex(FRAGMENT_SHADER, "outputDepth"));      //8
        _shaderFunctionSelection.resize(_postProcessingShader->GetSubroutineUniformCount(FRAGMENT_SHADER), 0);
    
    }

    _timer = 0;
    _tickInterval = 1.0f/24.0f;
    _randomNoiseCoefficient = 0;
    _randomFlashCoefficient = 0;

    updateResolution(resolution.width, resolution.height);

    //par.setParam("postProcessing.enableDepthOfField", false); //enable using keyboard;
}

void PostFX::createOperators(){
    ParamHandler& par = ParamHandler::getInstance();

    PreRenderStageBuilder& stageBuilder = PreRenderStageBuilder::getInstance();
    Framebuffer* screenBuffer = _gfx->getRenderTarget(GFXDevice::RENDER_TARGET_SCREEN);
    Framebuffer* depthBuffer  = _gfx->getRenderTarget(GFXDevice::RENDER_TARGET_DEPTH);

    if (_gfx->anaglyphEnabled()) {
        ResourceDescriptor anaglyph("anaglyph");
        anaglyph.setThreadedLoading(false);
        _anaglyphShader = CreateResource<ShaderProgram>(anaglyph);
        _anaglyphShader->UniformTexture("texRightEye", TEX_BIND_POINT_RIGHT_EYE);
        _anaglyphShader->UniformTexture("texLeftEye", TEX_BIND_POINT_LEFT_EYE);
    }

    // Bloom and Ambient Occlusion generate textures that are applied in the PostFX shader
    if (_enableBloom && !_bloomFB) {
        _bloomFB = _gfx->newFB();
        _bloomOP = stageBuilder.addPreRenderOperator<BloomPreRenderOperator>(_enableBloom, _bloomFB, _resolutionCache);
        _bloomOP->addInputFB(screenBuffer);
        //_bloomOP->genericFlag(toneMapEnabled);
    }

    if (_enableSSAO && !_SSAO_FB) {
        _SSAO_FB = _gfx->newFB();
        PreRenderOperator* ssaoOP = nullptr;
        ssaoOP = stageBuilder.addPreRenderOperator<SSAOPreRenderOperator>(_enableSSAO, _SSAO_FB, _resolutionCache);
        ssaoOP->addInputFB(screenBuffer);
        ssaoOP->addInputFB(depthBuffer);
    }

    // DOF and FXAA modify the screen FB
    if (_enableDOF && !_dofOP) {
        _dofOP = stageBuilder.addPreRenderOperator<DoFPreRenderOperator>(_enableDOF, screenBuffer, _resolutionCache);
        _dofOP->addInputFB(screenBuffer);
        _dofOP->addInputFB(depthBuffer);
    }

    if (_enableFXAA && !_fxaaOP) {
        _fxaaOP = stageBuilder.addPreRenderOperator<FXAAPreRenderOperator>(_enableFXAA, screenBuffer, _resolutionCache);
        _fxaaOP->addInputFB(screenBuffer);
    }

    if (_enableNoise && !_noise) {
        ResourceDescriptor noiseTexture("noiseTexture");
        ResourceDescriptor borderTexture("borderTexture");
		noiseTexture.setResourceLocation(par.getParam<stringImpl>("assetsLocation") + "/misc_images//bruit_gaussien.jpg");
		borderTexture.setResourceLocation(par.getParam<stringImpl>("assetsLocation") + "/misc_images//vignette.jpeg");
        _noise = CreateResource<Texture>(noiseTexture);
        _screenBorder = CreateResource<Texture>(borderTexture);
    }
}

void PostFX::updateResolution(I32 width, I32 height){
    if (!_gfx || !_gfx->postProcessingEnabled()) {
        return;
    }

    if (vec2<U16>(width, height) == _resolutionCache || width < 1 || height < 1) {
        return;
    }

    _resolutionCache.set(width, height);

    PreRenderStageBuilder::getInstance().getPreRenderBatch()->reshape(width,height);
}

void PostFX::displayScene() {
    _gfx->toggle2D(true);
    PreRenderStageBuilder::getInstance().getPreRenderBatch()->execute();

    ShaderProgram* drawShader = nullptr;
    if (_gfx->anaglyphEnabled()) {
        drawShader = _anaglyphShader;
        _anaglyphShader->bind();
        _gfx->getRenderTarget(GFXDevice::RENDER_TARGET_SCREEN)->Bind(TEX_BIND_POINT_RIGHT_EYE); //right eye buffer
        _gfx->getRenderTarget(GFXDevice::RENDER_TARGET_ANAGLYPH)->Bind(TEX_BIND_POINT_LEFT_EYE); //left eye buffer
    } else {
        drawShader = _postProcessingShader;
        _postProcessingShader->bind();
        _postProcessingShader->SetSubroutines(FRAGMENT_SHADER, _shaderFunctionSelection);

    #ifdef _DEBUG
        _gfx->getRenderTarget(_depthPreview ? GFXDevice::RENDER_TARGET_DEPTH : 
                                              GFXDevice::RENDER_TARGET_SCREEN)->Bind(TEX_BIND_POINT_SCREEN,
                                                                                     _depthPreview ? TextureDescriptor::Depth :
                                                                                                     TextureDescriptor::Color0);
    #else
        _gfx->getRenderTarget(GFXDevice::RENDER_TARGET_SCREEN)->Bind(TEX_BIND_POINT_SCREEN);
    #endif

        if (_underwaterTexture) {
            _underwaterTexture->Bind(TEX_BIND_POINT_UNDERWATER);
        }

        if (_noise) {
            _noise->Bind(TEX_BIND_POINT_NOISE);
        }

        if (_screenBorder) {
            _screenBorder->Bind(TEX_BIND_POINT_BORDER);
        }

        if (_bloomFB) {
            _bloomFB->Bind(TEX_BIND_POINT_BLOOM);
        }

        if (_SSAO_FB) {
            _SSAO_FB->Bind(TEX_BIND_POINT_SSAO);
        }
    }    

    _gfx->drawPoints(1, _gfx->getDefaultStateBlock(true), drawShader);
    _gfx->toggle2D(false);
}

void PostFX::idle(){
    if (!_gfx || !_gfx->postProcessingEnabled()) {
        return;
    }

    ParamHandler& par = ParamHandler::getInstance();
    //Update states

    if (!_postProcessingShader) {
        init(GFX_DEVICE.getRenderTarget(GFXDevice::RENDER_TARGET_SCREEN)->getResolution());
    }

    _underwater = GET_ACTIVE_SCENE()->state()._cameraUnderwater;
    _enableDOF  = par.getParam<bool>("postProcessing.enableDepthOfField");
    _enableNoise = par.getParam<bool>("postProcessing.enableNoise");
    _enableVignette = par.getParam<bool>("postProcessing.enableVignette");
    _depthPreview = par.getParam<bool>("postProcessing.fullScreenDepthBuffer");
    _enableFXAA = _gfx->FXAAEnabled();

    bool recompileShader = false;
    if (_enableBloom != par.getParam<bool>("postProcessing.enableBloom")) {
        _enableBloom = !_enableBloom;
        if (_enableBloom) {
            _postProcessingShader->addShaderDefine("POSTFX_ENABLE_BLOOM");
        } else {
            _postProcessingShader->removeShaderDefine("POSTFX_ENABLE_BLOOM");
        }
        recompileShader = true;
    }

    if (_enableSSAO != par.getParam<bool>("postProcessing.enableSSAO")) {
        _enableSSAO = !_enableSSAO;
        if (_enableSSAO) {
            _postProcessingShader->addShaderDefine("POSTFX_ENABLE_SSAO");
        } else {
            _postProcessingShader->removeShaderDefine("POSTFX_ENABLE_SSAO");
        }
        recompileShader = true;
    }

    createOperators();

    //recreate only the fragment shader
    if (recompileShader) {
        _postProcessingShader->recompile(false,true);
        _postProcessingShader->UniformTexture("texScreen", TEX_BIND_POINT_SCREEN);
        _postProcessingShader->UniformTexture("texBloom", TEX_BIND_POINT_BLOOM);
        _postProcessingShader->UniformTexture("texSSAO", TEX_BIND_POINT_SSAO);
        _postProcessingShader->UniformTexture("texNoise", TEX_BIND_POINT_NOISE);
        _postProcessingShader->UniformTexture("texVignette", TEX_BIND_POINT_BORDER);
        _postProcessingShader->UniformTexture("texWaterNoiseNM", TEX_BIND_POINT_UNDERWATER);
    }

    if(_enableBloom) {
        _postProcessingShader->Uniform("bloomFactor", par.getParam<F32>("postProcessing.bloomFactor"));
    }

    if (_enableNoise) {
        _timer += GETMSTIME();

        if (_timer > _tickInterval) {
            _timer = 0.0;
            _randomNoiseCoefficient = (F32)random(1000) * 0.001f;
            _randomFlashCoefficient = (F32)random(1000) * 0.001f;
        }

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

    _shaderFunctionSelection[0] = _enableVignette ? _shaderFunctionList[0] : _shaderFunctionList[6];
    _shaderFunctionSelection[1] = _enableNoise    ? _shaderFunctionList[1] : _shaderFunctionList[6];
    _shaderFunctionSelection[2] = _enableBloom    ? _shaderFunctionList[2] : _shaderFunctionList[6];
    _shaderFunctionSelection[3] = _enableSSAO     ? _shaderFunctionList[3] : _shaderFunctionList[6];
    _shaderFunctionSelection[4] = _underwater     ? _shaderFunctionList[4] : _shaderFunctionList[5];
    _shaderFunctionSelection[5] = _depthPreview   ? _shaderFunctionList[8] : _shaderFunctionList[7];
}

};

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

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

311 Diff Diff IonutCava picture IonutCava Mon 18 Aug, 2014 21:40:52 +0000

[Ionut]
- Added a basic AESOP implementation
- Moved all AI related classes under the “AI” namespace

307 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 19:45:12 +0000

[Ionut]
- More editor work:
— Per object skeleton rendering, bounding box rendering and shadow map toggling
— Full screen depth preview toggle
— Text entry based transforms

301 Diff Diff IonutCava picture IonutCava Tue 05 Aug, 2014 20:55:30 +0000

[Ionut]
- Code cleanups and comments (ShaderManager, ShaderProgram, Texture)
- Fixed some singleton destruction order between ResourceCache and ShaderManager to properly unload remaining shader programs

299 Diff Diff IonutCava picture IonutCava Mon 04 Aug, 2014 22:09:22 +0000

[Ionut]
- Disabled shader binary dump/load on non-nVidia GPUs (caused issues)
- More code cleanup/comments/formating (GFXDevice.cpp)

298 Diff Diff k1ngp1n picture k1ngp1n Sun 03 Aug, 2014 17:46:29 +0000

[Ionut]
- Final nVidia compatibility commit

294 k1ngp1n picture k1ngp1n Wed 30 Jul, 2014 20:19:14 +0000

[Ionut]
- More nVidia compatibility fixes (lighting shaders with textures still fail to compile):
— NS_GLIM compatibility fix (set ELEMENT_ARRAY_BUFFER to 0 in glimBatchData’s VAO on creation when setting attrib pointers)

  • Could improve NS_GLIM performance a lot by caching some buffers instead of re-creating them every time a batch is generated, but it’s suppose to be used only for debugging, so low prio*

- Removed interpolation factor from FrameEvent struct. Only using interpolation factor stored in GFXDevice instance
- Fixed the close button (added GLFW close callback)
- More code style improvements (no new comments yet)