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
#include "Headers/GFXDevice.h"

#include "Core/Headers/Kernel.h"
#include "Core/Headers/ParamHandler.h"
#include "Managers/Headers/ShaderManager.h"
#include "Rendering/PostFX/Headers/PostFX.h"
#include "Core/Resources/Headers/ResourceCache.h"

//Max number of frames before an unused primitive is deleted (default: 180 - 3 seconds at 60 fps)
const I32 IM_MAX_FRAMES_ZOMBIE_COUNT = 180;

I8 GFXDevice::initHardware(const vec2<U16>& resolution, I32 argc, char **argv) {
    I8 hardwareState = _api.initHardware(resolution,argc,argv);

    if(hardwareState != NO_ERR)
        return hardwareState;

    //Create an immediate mode shader
    ShaderManager::getInstance().init(_kernel);
    _imShader = ShaderManager::getInstance().getDefaultShader();
    DIVIDE_ASSERT(_imShader != nullptr, "GFXDevice error: No immediate mode emulation shader available!");
    _imShader->Uniform("tex",0);

    //View, Projection, ViewProjection, Camera Position, Viewport, 
    //zPlanes and ClipPlanes[MAX_CLIP_PLANES] 3 x 16 + 4 + 4 + 4 + 4 * MAX_CLIP_PLANES float values
    _gfxDataBuffer = newSB(false, false);
    _gfxDataBuffer->Create((3 * 16) + 4 + 4 + 4 + (4 * Config::MAX_CLIP_PLANES), sizeof(F32)); 
    _gfxDataBuffer->Bind(Divide::SHADER_BUFFER_GPU_BLOCK);

    _nodeMatricesBuffer = newSB(true);
    _nodeMatricesBuffer->Create(Config::MAX_VISIBLE_NODES, sizeof(NodeData2));
    _nodeMatricesBuffer->Bind(Divide::SHADER_BUFFER_NODE_TRANSFORMS);

    _nodeMaterialsBuffer = newSB(true);
    _nodeMaterialsBuffer->Create(Config::MAX_VISIBLE_NODES, sizeof(NodeData4)); 
    _nodeMaterialsBuffer->BindRange(Divide::SHADER_BUFFER_NODE_MATERIAL, 0, Config::MAX_VISIBLE_NODES);

    changeResolution(resolution);

    RenderStateBlockDescriptor defaultStateDescriptor;
    _defaultStateBlockHash = getOrCreateStateBlock(defaultStateDescriptor);
    RenderStateBlockDescriptor defaultStateDescriptorNoDepth;
    defaultStateDescriptorNoDepth.setZReadWrite(false, true);
    _defaultStateNoDepthHash = getOrCreateStateBlock(defaultStateDescriptorNoDepth);
    RenderStateBlockDescriptor state2DRenderingDesc;
    state2DRenderingDesc.setCullMode(CULL_MODE_NONE);
    state2DRenderingDesc.setZReadWrite(false, true);
    _state2DRenderingHash = getOrCreateStateBlock(state2DRenderingDesc);
    RenderStateBlockDescriptor stateDepthOnlyRendering;
    stateDepthOnlyRendering.setColorWrites(false, false, false, false);
    stateDepthOnlyRendering.setZFunc(CMP_FUNC_ALWAYS);
    _stateDepthOnlyRenderingHash = getOrCreateStateBlock(stateDepthOnlyRendering);
    _stateBlockMap[0] = nullptr;

    DIVIDE_ASSERT(_stateDepthOnlyRenderingHash != _state2DRenderingHash,    "GFXDevice error: Invalid default state hash detected!");
    DIVIDE_ASSERT(_state2DRenderingHash        != _defaultStateNoDepthHash, "GFXDevice error: Invalid default state hash detected!");
    DIVIDE_ASSERT(_defaultStateNoDepthHash     != _defaultStateBlockHash,   "GFXDevice error: Invalid default state hash detected!");

    setStateBlock(_defaultStateBlockHash);

    //Screen FB should use MSAA if available, else fallback to normal color FB (no AA or FXAA)
    _renderTarget[RENDER_TARGET_SCREEN]       = newFB(true);
    _renderTarget[RENDER_TARGET_DEPTH]        = newFB(false);
    _renderTarget[RENDER_TARGET_DEPTH_RANGES] = newFB(false);

    TextureDescriptor depthRangesDescriptor(TEXTURE_2D, RGBA32F, FLOAT_32);
    SamplerDescriptor depthRangesSampler;
    depthRangesSampler.setFilters(TEXTURE_FILTER_NEAREST);
    depthRangesSampler.setWrapMode(TEXTURE_CLAMP_TO_EDGE);
    depthRangesSampler.toggleMipMaps(false);
    depthRangesDescriptor.setSampler(depthRangesSampler);
    vec2<U16> tileSize(Config::Lighting::LIGHT_GRID_TILE_DIM_X, Config::Lighting::LIGHT_GRID_TILE_DIM_Y);
    vec2<U16> resTemp(resolution + tileSize);

    TextureDescriptor screenDescriptor(TEXTURE_2D_MS, RGBA16F, FLOAT_16);
    SamplerDescriptor screenSampler;
    screenSampler.setFilters(TEXTURE_FILTER_NEAREST, TEXTURE_FILTER_NEAREST);
    screenSampler.setWrapMode(TEXTURE_CLAMP_TO_EDGE);
    screenSampler.toggleMipMaps(false);
    screenDescriptor.setSampler(screenSampler);

    TextureDescriptor depthDescriptorHiZ(TEXTURE_2D_MS, DEPTH_COMPONENT32F, FLOAT_32);
    SamplerDescriptor depthSamplerHiZ;
    depthSamplerHiZ.setFilters(TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST, TEXTURE_FILTER_NEAREST);
    depthSamplerHiZ.setWrapMode(TEXTURE_CLAMP_TO_EDGE);
    depthSamplerHiZ.toggleMipMaps(true);
    depthSamplerHiZ._useRefCompare = true; //< Use compare function
    depthSamplerHiZ._cmpFunc = CMP_FUNC_GEQUAL; //< Use greater or equal
    depthDescriptorHiZ.setSampler(depthSamplerHiZ);

    TextureDescriptor depthDescriptor(TEXTURE_2D_MS, DEPTH_COMPONENT32F, FLOAT_32);
    SamplerDescriptor depthSampler;
    depthSampler.setFilters(TEXTURE_FILTER_NEAREST);
    depthSampler.setWrapMode(TEXTURE_CLAMP_TO_EDGE);
    depthSampler.toggleMipMaps(false);
    depthSampler._useRefCompare = true; //< Use compare function
    depthSampler._cmpFunc = CMP_FUNC_GEQUAL; //< Use greater or equal
    depthDescriptor.setSampler(depthSampler);

    _renderTarget[RENDER_TARGET_DEPTH_RANGES]->AddAttachment(depthRangesDescriptor, TextureDescriptor::Color0);
    _renderTarget[RENDER_TARGET_DEPTH_RANGES]->toggleDepthBuffer(false);
    _renderTarget[RENDER_TARGET_DEPTH_RANGES]->setClearColor(vec4<F32>(0.0f, 1.0f, 0.0f, 1.0f));
    _renderTarget[RENDER_TARGET_DEPTH_RANGES]->Create(resTemp.x / tileSize.x - 1, resTemp.y / tileSize.y - 1);

    _renderTarget[RENDER_TARGET_DEPTH]->AddAttachment(depthDescriptorHiZ, TextureDescriptor::Depth);
    _renderTarget[RENDER_TARGET_DEPTH]->toggleColorWrites(false);
    _renderTarget[RENDER_TARGET_DEPTH]->Create(resolution.width, resolution.height);

    _renderTarget[RENDER_TARGET_SCREEN]->AddAttachment(screenDescriptor, TextureDescriptor::Color0);
    _renderTarget[RENDER_TARGET_SCREEN]->AddAttachment(depthDescriptor,  TextureDescriptor::Depth);
    _renderTarget[RENDER_TARGET_SCREEN]->Create(resolution.width, resolution.height);

    if(_enableAnaglyph){
        _renderTarget[RENDER_TARGET_ANAGLYPH] = newFB(true);
        _renderTarget[RENDER_TARGET_ANAGLYPH]->AddAttachment(screenDescriptor, TextureDescriptor::Color0);
        _renderTarget[RENDER_TARGET_ANAGLYPH]->AddAttachment(depthDescriptor,  TextureDescriptor::Depth);
        _renderTarget[RENDER_TARGET_ANAGLYPH]->Create(resolution.width, resolution.height);
    }

    _postFX.init(resolution);
    add2DRenderFunction(DELEGATE_BIND(&GFXDevice::previewDepthBuffer, this), 0);
    _kernel->getCameraMgr().addNewCamera("2DRenderCamera", _2DCamera);
    _kernel->getCameraMgr().addNewCamera("_gfxCubeCamera", _cubeCamera);
    _HIZConstructProgram = CreateResource<ShaderProgram>(ResourceDescriptor("HiZConstruct"));
    _HIZConstructProgram->UniformTexture("LastMip", 0);

    ResourceDescriptor rangesDesc("DepthRangesConstruct");
    rangesDesc.setPropertyList("LIGHT_GRID_TILE_DIM_X " + Util::toString(Config::Lighting::LIGHT_GRID_TILE_DIM_X) + "," + "LIGHT_GRID_TILE_DIM_Y " + Util::toString(Config::Lighting::LIGHT_GRID_TILE_DIM_Y));
    _depthRangesConstructProgram = CreateResource<ShaderProgram>(rangesDesc);
    _depthRangesConstructProgram->UniformTexture("depthTex", 0);

    _cachedSceneZPlanes.set(ParamHandler::getInstance().getParam<F32>("rendering.zNear"), ParamHandler::getInstance().getParam<F32>("rendering.zFar"));
    
    return NO_ERR;
}

void GFXDevice::closeRenderingApi(){
    _shaderManager.destroyInstance();
    _api.closeRenderingApi();
    _loaderThread->join();
    SAFE_DELETE(_loaderThread);

    FOR_EACH(RenderStateMap::value_type& it, _stateBlockMap)
        SAFE_DELETE(it.second);
    
    _stateBlockMap.clear();
    for(IMPrimitive*& priv : _imInterfaces)
        SAFE_DELETE(priv);
    
    _imInterfaces.clear(); //<Should call all destructors

    //Destroy all rendering Passes
    RenderPassManager::getInstance().destroyInstance();

    for (Framebuffer*& renderTarget : _renderTarget)
        SAFE_DELETE(renderTarget);

    SAFE_DELETE(_gfxDataBuffer);
    SAFE_DELETE(_nodeMaterialsBuffer);
    SAFE_DELETE(_nodeMatricesBuffer);
}

void GFXDevice::closeRenderer(){
    RemoveResource(_HIZConstructProgram);
    RemoveResource(_depthRangesConstructProgram);
    PRINT_FN(Locale::get("STOP_POST_FX"));
    _postFX.destroyInstance();
    _shaderManager.Destroy();
    PRINT_FN(Locale::get("CLOSING_RENDERER"));
    SAFE_DELETE(_renderer);
}

void GFXDevice::idle() {
    if (!_renderer) return;

    _cachedSceneZPlanes.set(ParamHandler::getInstance().getParam<F32>("rendering.zNear"), 
                            ParamHandler::getInstance().getParam<F32>("rendering.zFar"));

    _postFX.idle();
    _shaderManager.idle();
    _api.idle();
}

void GFXDevice::beginFrame()    {
    _api.beginFrame();
    setStateBlock(_defaultStateBlockHash);
}

void GFXDevice::endFrame()      { 
    //Remove dead primitives in 3 steps (or we could automate this with shared_ptr?):
    //1) Partition the vector in 2 parts: valid objects first, zombie objects second
    vectorImpl<IMPrimitive* >::iterator zombie = std::partition(_imInterfaces.begin(),
                                                                _imInterfaces.end(),
                                                                [](IMPrimitive* const priv){
                                                                    if(!priv->_canZombify) return true;
                                                                    return priv->zombieCounter() < IM_MAX_FRAMES_ZOMBIE_COUNT;
                                                                });
    //2) For every zombie object, free the memory it's using
    for( vectorImpl<IMPrimitive *>::iterator i = zombie ; i != _imInterfaces.end() ; ++i )
        SAFE_DELETE(*i);

    //3) Remove all the zombie objects once the memory is freed
    _imInterfaces.erase(zombie, _imInterfaces.end());

    FRAME_COUNT++;
    FRAME_DRAW_CALLS_PREV = FRAME_DRAW_CALLS;
    FRAME_DRAW_CALLS = 0;

    _api.endFrame();  
}

Commits for Divide-Framework/trunk/Source Code/Hardware/Video/GFXDeviceState.cpp

Diff revisions: vs.
Revision Author Commited Message
286 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]]