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

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

PostFX::~PostFX()
{

    if(_postProcessingShader){
        RemoveResource(_postProcessingShader);
        if(_underwaterTexture)      RemoveResource(_underwaterTexture);
        if(_gfx->anaglyphEnabled()) RemoveResource(_anaglyphShader);
        if(_enableBloom)            SAFE_DELETE(_bloomFB);
        if(_enableSSAO)             SAFE_DELETE(_SSAO_FB);

        if(_enableNoise){
            RemoveResource(_noise);
            RemoveResource(_screenBorder);
        }
    }
    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);
        _postProcessingShader = CreateResource<ShaderProgram>(postFXShader);

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

        createOperators();

        _postProcessingShader->Uniform("_noiseTile", 0.05f);
        _postProcessingShader->Uniform("_noiseFactor", 0.02f);
        _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);
        _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);
    }

    if(_enableSSAO && !_SSAO_FB){
        _SSAO_FB = _gfx->newFB();
        PreRenderOperator* 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<std::string>("assetsLocation") + "/misc_images//bruit_gaussien.jpg");
        borderTexture.setResourceLocation(par.getParam<std::string>("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);
    _postProcessingShader->refresh();
    PreRenderStageBuilder::getInstance().getPreRenderBatch()->reshape(width,height);
}

void PostFX::displayScene(){
    if (!_gfx->postProcessingEnabled())
         return;


    _gfx->toggle2D(true);
    PreRenderStageBuilder::getInstance().getPreRenderBatch()->execute();

    if (_gfx->anaglyphEnabled()){
        _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{
        _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));
    _gfx->toggle2D(false);
}

void PostFX::idle(){
    if (!_gfx) return;

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

    if (!_gfx->postProcessingEnabled())
        return;

    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");
    _enableFXAA = _gfx->FXAAEnabled();

    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[0] : _shaderFunctionList[7];
}

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

Diff revisions: vs.
Revision Author Commited Message
286 Diff Diff IonutCava picture IonutCava Fri 18 Jul, 2014 16:32:03 +0000

[Ionut] [[BR]]

    • very buggy commit ** [[BR]]

- New rendering pipeline: [[BR]]
— Bumped OpenGL version to 4.4 [[BR]]
— Command based rendering. GFXDevice dispatches rendering commands to their respective buffers [[BR]]
— State changing is exclusively controlled by the GFXDevice class [[BR]]
— Material properties and transform matrices are stored in shader storage buffers that are updated after culling and uploaded once per RenderPass. Objects use drawIDs to index data in shaders [[BR]]
— Removed all custom matrices and other, per shader, bind data [[BR]]
— Removed per node light data. Light culling and shader buffers will replace the old light management system [[BR]]
— Framebuffers now use the Texture class for attachments [[BR]]
— Sampler objects now work like RenderStateBlocks. GL_API handles them and stores them in a hash indexed map [[BR]]
— Removed “prepareMaterial” calls. Pre-draw material setup is now only limited to texture binding [[BR]]
— Moved immediate mode emulation and primitive rendering from API level to GFXDevice level [[BR]]
— Framebuffer rendering must be completed with a call to end() to ensure proper MSAA resolve (asserts, otherwise) [[BR]]
- Fixed hash code generation for RenderStateBlock (and now, glSamplerObject) using boost::hash_combine [[BR]]
- Temporarily disabled XML caching for materials [[BR]]

277 Diff Diff IonutCava picture IonutCava Tue 17 Jun, 2014 19:27:29 +0000

[Ionut] [[BR]]
- Change PostFX shader to be subroutine-based (instead of using if-else statements) [[BR]]

274 Diff Diff IonutCava picture IonutCava Fri 13 Jun, 2014 12:56:03 +0000

[Ionut] [[BR]]
- Simplified buffer based rendering (VertexBuffer & GenericVertexData are now exclusively command based) [[BR]]
- Update bone buffer to persistent mapped-SSBO system with double-buffering [[BR]]
- Simplified sky rendering and (finally) reduced the sun’s radius [[BR]]

267 Diff Diff k1ngp1n picture k1ngp1n Wed 28 May, 2014 11:08:12 +0000

[Ionut] [[BR]]
- Improved texture handling: [[BR]]
— No more texture unbind (except when clearing the frame state) [[BR]]
— Moved texture bind checking to the GL_API class [[BR]]
- Fixed a crash related to vegetation loading due to compiler optimizations [[BR]]
- removed all “releaseMaterial” functions as they are no longer needed [[BR]]

264 Diff Diff k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]

263 Diff Diff k1ngp1n picture k1ngp1n Fri 09 May, 2014 16:43:53 +0000

[Ionut] [[BR]]
- Fullscreen quads are now rendered via Geometry Shaders [[BR]]
- Implemented shader subroutines [[BR]]
- Improved CVSM’s quality [[BR]]
- Separated shadow only light properties from general light properties in separate UBOs to improve update performance [[BR]]
- Removed shader mask and instead try to load all available shaders from a given file [[BR]]
- Added a method (hackish) of changing projection properties at runtime (e.g. z planes) [[BR]]
- Fixed a comparison bug in RenderStateBlock class (forgot to clean it at last commit) [[BR]]
- Fixed clipping plane upload to shader programs [[BR]]

260 Diff Diff k1ngp1n picture k1ngp1n Sat 03 May, 2014 19:33:41 +0000

[Ionut] [[BR]]
- More work on CSM. Reverted back to VSM to allow easier blurring and filtering [[BR]]
- Rewrote FrameBuffer classes to fix MSAA and memory leaks that occurred on resize (double creation of textures) [[BR]]
— Removed FBType. FrameBuffer types depend only on attachment types and if it’s MSAA enabled or not [[BR]]
- Rewrote RenderState classes to be API agnostic [[BR]]
- Bug fixes in math classes [[BR]]
- Changed “resolution change” logic [[BR]]
- Simplified some options in config.xml [[BR]]
- Improved per-light shadow options [[BR]]

259 Diff Diff k1ngp1n picture k1ngp1n Sun 27 Apr, 2014 18:24:52 +0000

[Ionut][[BR]]
- Implemented Exponential Stable Cascaded Shadow Maps for directional lights (still buggy, but basically working) [[BR]]
— CSM settings (resolution, number of splits, etc) are part of the scene render state [[BR]]
- SGN can skip rendering for the current frame if they are not ready (e.g. OnDraw can now return false or materials can fail to bind) [[BR]]
- Frustum class is now using the Plane class [[BR]]
- Removed Math SIMD implementation as the compiler generated code will always be faster and safer [[BR]]
- Added a “STUBBED” macro for outputing to the error stream of important messages (instead of using #pragma message) [[BR]]
- Added “freeze time” logic to pause/resume updates with or without freezing the GUI too [[BR]]
- Added “processGUI” method the every Scene class to separate time-sensitive GUI updates from game logic loop [[BR]]
- GLSL <-> Vertex Buffer bind locations are automatically set for every vertex shader [[BR]]
- Added basic support for compute shaders [[BR]]
- Removed most OS define checks to make porting to a new platform easier [[BR]]
- Texture transparency check per-pixel is now using OpenMP [[BR]]
- Texture samplers can now set a border color for CLAMP_TO_BORDER wrap mode [[BR]]
- Removed “Object” suffix from GFX Classes (pixel buffer, vertex buffer, frame buffer, etc) [[BR]]
- Stop processing scene input when application looses focus [[BR]]
- SceneGraphNode class is now responsible for updating SGNComponents on draw and calling its _node’s OnDraw method [[BR]]
- UBOs can now list all of the active uniforms inside a block with info such as type and offset [[BR]]
- Removed deprecated ‘GLCheck’ macro [[BR]]
- A lot of bug fixes and performance improvements in various places [[BR]]

249 Diff Diff k1ngp1n picture k1ngp1n Mon 31 Mar, 2014 20:42:31 +0000

[Ionut] [[BR]]
- Migrated to platform toolset v100 [[BR]]
- Updated boost to 1.55 [[BR]]
- Moved from “NULL” to “nullptr” [[BR]]
- DevIL is now linked dynamically [[BR]]

221 k1ngp1n picture k1ngp1n Wed 08 Jan, 2014 18:50:02 +0000

[Ionut] [[BR]]
- Merged branch “Shadows And Animations” back into trunk [[BR]]
- Added DoxygenGUI project file [[BR]]

  • Buggy *