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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#include "Headers/GFXDevice.h"

#include "Core/Headers/ParamHandler.h"
#include "Utility/Headers/ImageTools.h"
#include "Managers/Headers/SceneManager.h"

#include "Rendering/Headers/Renderer.h"
#include "Rendering/PostFX/Headers/PostFX.h"
#include "Rendering/Camera/Headers/FreeFlyCamera.h"
#include "Rendering/RenderPass/Headers/RenderPass.h"

#include "Platform/Video/Shaders/Headers/ShaderManager.h"

namespace Divide {

std::array<VertexBuffer::AttribFlags, to_const_uint(RenderStage::COUNT)> VertexBuffer::_attribMaskPerStage;

namespace {
/// Used for anaglyph rendering
struct CameraFrustum {
    D32 leftfrustum;
    D32 rightfrustum;
    D32 bottomfrustum;
    D32 topfrustum;
    F32 modeltranslation;
} _leftCam, _rightCam;
F32 _anaglyphIOD = -0.01f;
};

GFXDevice::GFXDevice()
    : _api(nullptr), 
    _renderStage(RenderStage::COUNT),
    _prevRenderStage(RenderStage::COUNT)
{
    // Hash values
    _state2DRenderingHash = 0;
    _defaultStateBlockHash = 0;
    _currentStateBlockHash = 0;
    _previousStateBlockHash = 0;
    _defaultStateNoDepthHash = 0;
    _stateDepthOnlyRenderingHash = 0;
    // Pointers
    _axisGizmo = nullptr;
    _imShader = nullptr;
    _imShaderLines = nullptr;
    _gfxDataBuffer = nullptr;
    _HIZConstructProgram = nullptr;
    _HIZCullProgram = nullptr;
    _previewDepthMapShader = nullptr;
    _commandBuildTimer = nullptr;
    // Integers
    FRAME_COUNT = 0;
    FRAME_DRAW_CALLS = 0;
    FRAME_DRAW_CALLS_PREV = FRAME_DRAW_CALLS;
    _lastCommandCount = 0;
    _lastNodeCount = 0;
    // Floats
    _interpolationFactor = 1.0;
    // Booleans
    _2DRendering = false;
    _drawDebugAxis = false;
    _enableAnaglyph = false;
    _viewportUpdate = false;
    _rasterizationEnabled = true;
    _enablePostProcessing = false;
    // Enumerated Types
    _shadowDetailLevel = RenderDetailLevel::HIGH;
    _GPUVendor = GPUVendor::COUNT;
    _API_ID = RenderAPI::COUNT;
    // Utility cameras
    _2DCamera = MemoryManager_NEW FreeFlyCamera();
    _2DCamera->lockView(true);
    _cubeCamera = MemoryManager_NEW FreeFlyCamera();
    // Clipping planes
    _clippingPlanes.resize(Config::MAX_CLIP_PLANES, Plane<F32>(0, 0, 0, 0));
    // Render targets
    for (Framebuffer*& renderTarget : _renderTarget) {
        renderTarget = nullptr;
    }
    // To allow calls to "setBaseViewport"
    
    _viewport.push(vec4<I32>(-1));
    // Add our needed app-wide render passes. RenderPassManager is responsible
    // for deleting these!
    RenderPassManager::getInstance().addRenderPass("diffusePass", 1);
    // RenderPassManager::getInstance().addRenderPass("shadowPass",2);
    // Red X-axis
    _axisLines.push_back(
        Line(VECTOR3_ZERO, WORLD_X_AXIS * 2, vec4<U8>(255, 0, 0, 255), 3.0f));
    // Green Y-axis
    _axisLines.push_back(
        Line(VECTOR3_ZERO, WORLD_Y_AXIS * 2, vec4<U8>(0, 255, 0, 255), 3.0f));
    // Blue Z-axis
    _axisLines.push_back(
        Line(VECTOR3_ZERO, WORLD_Z_AXIS * 2, vec4<U8>(0, 0, 255, 255), 3.0f));

    VertexBuffer::AttribFlags flags;
    flags.fill(true);
    VertexBuffer::setAttribMasks(flags);

    // Don't (currently) need these for shadow passes
    flags[to_uint(VertexBuffer::VertexAttribute::ATTRIB_COLOR)] = false;
    flags[to_uint(VertexBuffer::VertexAttribute::ATTRIB_NORMAL)] = false;
    flags[to_uint(VertexBuffer::VertexAttribute::ATTRIB_TANGENT)] = false;

    VertexBuffer::setAttribMask(RenderStage::SHADOW, flags);
    VertexBuffer::setAttribMask(RenderStage::Z_PRE_PASS, flags);
}

GFXDevice::~GFXDevice()
{
}

/// Generate a cube texture and store it in the provided framebuffer
void GFXDevice::generateCubeMap(Framebuffer& cubeMap, const vec3<F32>& pos,
                                const DELEGATE_CBK<>& renderFunction,
                                const vec2<F32>& zPlanes,
                                RenderStage renderStage) {
    // Only the first color attachment or the depth attachment is used for now
    // and it must be a cube map texture
    Texture* colorAttachment =
        cubeMap.getAttachment(TextureDescriptor::AttachmentType::Color0);
    Texture* depthAttachment =
        cubeMap.getAttachment(TextureDescriptor::AttachmentType::Depth);
    // Color attachment takes precedent over depth attachment
    bool hasColor = (colorAttachment != nullptr);
    bool hasDepth = (depthAttachment != nullptr);
    // Everyone's innocent until prove guilty
    bool isValidFB = true;
    if (hasColor) {
        // We only need the color attachment
        isValidFB = (colorAttachment->getTextureType() ==
                     TextureType::TEXTURE_CUBE_MAP);
    } else {
        // We don't have a color attachment, so we require a cube map depth
        // attachment
        isValidFB = (hasDepth &&
                     depthAttachment->getTextureType() ==
                         TextureType::TEXTURE_CUBE_MAP);
    }
    // Make sure we have a proper render target to draw to
    if (!isValidFB) {
        // Future formats must be added later (e.g. cube map arrays)
        Console::errorfn(Locale::get("ERROR_GFX_DEVICE_INVALID_FB_CUBEMAP"));
        return;
    }
    // Calling this function without a callback is a programming error and
    // should never happen
    DIVIDE_ASSERT(renderFunction == false,
                  "GFXDevice error: tried to generate a cube map without a "
                  "valid render function!");
    // No dual-paraboloid rendering here. Just draw once for each face.
    static vec3<F32> TabUp[6] = {WORLD_Y_NEG_AXIS, WORLD_Y_NEG_AXIS,
                                 WORLD_Z_AXIS,     WORLD_Z_NEG_AXIS,
                                 WORLD_Y_NEG_AXIS, WORLD_Y_NEG_AXIS};
    // Get the center and up vectors for each cube face
    vec3<F32> TabCenter[6] = {vec3<F32>(pos.x + 1.0f, pos.y, pos.z),
                              vec3<F32>(pos.x - 1.0f, pos.y, pos.z),
                              vec3<F32>(pos.x, pos.y + 1.0f, pos.z),
                              vec3<F32>(pos.x, pos.y - 1.0f, pos.z),
                              vec3<F32>(pos.x, pos.y, pos.z + 1.0f),
                              vec3<F32>(pos.x, pos.y, pos.z - 1.0f)};

    Kernel& kernel = Application::getInstance().getKernel();
    // Set a 90 degree vertical FoV perspective projection
    _cubeCamera->setProjection(1.0f, 90.0f, zPlanes);
    // Set the cube camera as the currently active one
    kernel.getCameraMgr().pushActiveCamera(_cubeCamera, false);
    // Set the desired render stage, remembering the previous one
    RenderStage prevRenderStage = setRenderStage(renderStage);
    // Enable our render target
    cubeMap.begin(Framebuffer::defaultPolicy());
    // For each of the environment's faces (TOP, DOWN, NORTH, SOUTH, EAST, WEST)
    for (U8 i = 0; i < 6; ++i) {
        // Draw to the current cubemap face
        cubeMap.drawToFace(hasColor ? TextureDescriptor::AttachmentType::Color0
                                    : TextureDescriptor::AttachmentType::Depth,
                           i);
        // Point our camera to the correct face
        _cubeCamera->lookAt(pos, TabCenter[i], TabUp[i]);
        // And generated required matrices
        _cubeCamera->renderLookAt();
        // Pass our render function to the renderer
        getRenderer().render(renderFunction, GET_ACTIVE_SCENE().renderState());
    }
    // Resolve our render target
    cubeMap.end();
    // Return to our previous rendering stage
    setRenderStage(prevRenderStage);
    // Restore our previous camera
    kernel.getCameraMgr().popActiveCamera();
}

/// If the stateBlock doesn't exist in the state block map, add it for future reference
bool GFXDevice::registerRenderStateBlock(const RenderStateBlock& descriptor) {
    // Each combination of render states has a unique hash value
    U32 hashValue = descriptor.getHash();
    // Find the corresponding render state block
    // Create a new one if none are found. The GFXDevice class is
    // responsible for deleting these!
    std::pair<RenderStateMap::iterator, bool> result =
        hashAlg::emplace(_stateBlockMap, hashValue, descriptor);
    // Return true if registration was successful 
    return result.second;
}

/// Activate the render state block described by the specified hash value (0 == default state block)
U32 GFXDevice::setStateBlock(U32 stateBlockHash) {
    // Passing 0 is a perfectly acceptable way of enabling the default render state block
    if (stateBlockHash == 0) {
        stateBlockHash = _defaultStateBlockHash;
    }

    // If the new state hash is different from the previous one
    if (stateBlockHash != _currentStateBlockHash) {
        // Remember the previous state hash
        _previousStateBlockHash = _currentStateBlockHash;
        // Update the current state hash
        _currentStateBlockHash = stateBlockHash;
        RenderStateMap::const_iterator currentStateIt = _stateBlockMap.find(_currentStateBlockHash);
        RenderStateMap::const_iterator previousStateIt = _stateBlockMap.find(_previousStateBlockHash);

        DIVIDE_ASSERT(currentStateIt != previousStateIt &&
                      currentStateIt != std::cend(_stateBlockMap) &&
                      previousStateIt != std::cend(_stateBlockMap),
                      "GFXDevice error: Invalid state blocks detected on activation!");

        // Activate the new render state block in an rendering API dependent way
        activateStateBlock(currentStateIt->second, previousStateIt->second);
    }
    // Return the previous state hash
    return _previousStateBlockHash;
}

/// Return the the render state block defined by the specified hash value.
const RenderStateBlock& GFXDevice::getRenderStateBlock(U32 renderStateBlockHash) const {
    // Find the render state block associated with the received hash value
    RenderStateMap::const_iterator it = _stateBlockMap.find(renderStateBlockHash);
    // Assert if it doesn't exist. Avoids programming errors.
    DIVIDE_ASSERT(it != std::cend(_stateBlockMap),
                  "GFXDevice error: Invalid render state block hash specified "
                  "for getRenderStateBlock!");
    // Return the state block's descriptor
    return it->second;
}

void GFXDevice::increaseResolution() {
    const WindowManager& winManager = Application::getInstance().getWindowManager();
    const vec2<U16>& resolution = winManager.getResolution();
    const vectorImpl<GPUState::GPUVideoMode>& displayModes = _state.getDisplayModes(winManager.targetDisplay());

    vectorImpl<GPUState::GPUVideoMode>::const_reverse_iterator it;
    for (it = std::rbegin(displayModes); it != std::rend(displayModes); ++it) {
        const vec2<U16>& tempResolution = it->_resolution;

        if (resolution.width < tempResolution.width &&
            resolution.height < tempResolution.height) {
            changeResolution(tempResolution.width, tempResolution.height);
            return;
        }
    }
}

void GFXDevice::decreaseResolution() {
    const WindowManager& winManager = Application::getInstance().getWindowManager();
    const vec2<U16>& resolution = winManager.getResolution();
    const vectorImpl<GPUState::GPUVideoMode>& displayModes = _state.getDisplayModes(winManager.targetDisplay());
    
    vectorImpl<GPUState::GPUVideoMode>::const_iterator it;
    for (it = std::begin(displayModes); it != std::end(displayModes); ++it) {
        const vec2<U16>& tempResolution = it->_resolution;
        if (resolution.width > tempResolution.width &&
            resolution.height > tempResolution.height) {
            changeResolution(tempResolution.width, tempResolution.height);
            return;
        }
    }
}

void GFXDevice::toggleFullScreen() {
    WindowManager& winManager = Application::getInstance().getWindowManager();
    switch (winManager.mainWindowType()) {
        case WindowType::WINDOW:
        case WindowType::SPLASH:
            winManager.mainWindowType(WindowType::FULLSCREEN_WINDOWED);
            break;
        case WindowType::FULLSCREEN_WINDOWED:
            winManager.mainWindowType(WindowType::FULLSCREEN);
            break;
        case WindowType::FULLSCREEN:
            winManager.mainWindowType(WindowType::WINDOW);
            break;
    };
}

/// The main entry point for any resolution change request
void GFXDevice::changeResolution(U16 w, U16 h) {
    // Make sure we are in a valid state that allows resolution updates
    if (_renderTarget[to_uint(RenderTarget::SCREEN)] != nullptr) {
        // Update resolution only if it's different from the current one.
        // Avoid resolution change on minimize so we don't thrash render targets
        if (vec2<U16>(w, h) ==  _renderTarget[to_uint(RenderTarget::SCREEN)]
                    ->getResolution() ||
            !(w > 1 && h > 1)) {
            return;
        }
        // Update render targets with the new resolution
        for (Framebuffer* renderTarget : _renderTarget) {
            if (renderTarget) {
                renderTarget->create(w, h);
            }
        }
    }

    Application& app = Application::getInstance();
    // Update post-processing render targets and buffers
    PostFX::getInstance().updateResolution(w, h);
    app.getWindowManager().setResolution(vec2<U16>(w, h));
    // Refresh shader programs
    ShaderManager::getInstance().refreshShaderData();

    _api->changeResolution(w, h);
}

/// Update fog values
void GFXDevice::enableFog(F32 density, const vec3<F32>& color) {
    ParamHandler& par = ParamHandler::getInstance();
    par.setParam("rendering.sceneState.fogColor.r", color.r);
    par.setParam("rendering.sceneState.fogColor.g", color.g);
    par.setParam("rendering.sceneState.fogColor.b", color.b);
    par.setParam("rendering.sceneState.fogDensity", density);
    // Shader programs will pick up the new values on the next update call
    ShaderManager::getInstance().refreshSceneData();
}

/// Return a GFXDevice specific matrix or a derivative of it
void GFXDevice::getMatrix(const MATRIX_MODE& mode, mat4<F32>& mat) {
    // The matrix names are self-explanatory
    if (mode == MATRIX_MODE::VIEW_PROJECTION) {
        mat.set(_gpuBlock._data._ViewProjectionMatrix);
    } else if (mode == MATRIX_MODE::VIEW) {
        mat.set(_gpuBlock._data._ViewMatrix);
    } else if (mode == MATRIX_MODE::PROJECTION) {
        mat.set(_gpuBlock._data._ProjectionMatrix);
    } else if (mode == MATRIX_MODE::TEXTURE) {
        mat.identity();
        Console::errorfn(Locale::get("ERROR_TEXTURE_MATRIX_ACCESS"));
    } else if (mode == MATRIX_MODE::VIEW_INV) {
        _gpuBlock._data._ViewMatrix.getInverse(mat);
    } else if (mode == MATRIX_MODE::PROJECTION_INV) {
        _gpuBlock._data._ProjectionMatrix.getInverse(mat);
    } else if (mode == MATRIX_MODE::VIEW_PROJECTION_INV) {
        _gpuBlock._data._ViewProjectionMatrix.getInverse(mat);
    } else {
        DIVIDE_ASSERT(
            false,
            "GFXDevice error: attempted to query an invalid matrix target!");
    }
}

/// Update the internal GPU data buffer with the clip plane values
void GFXDevice::updateClipPlanes() {
    GPUBlock::GPUData& data = _gpuBlock._data;
    for (U8 i = 0; i < Config::MAX_CLIP_PLANES; ++i) {
        data._clipPlanes[i] = _clippingPlanes[i].getEquation();
    }
    _gpuBlock._updated = true;
}

/// Update the internal GPU data buffer with the updated viewport dimensions
void GFXDevice::updateViewportInternal(const vec4<I32>& viewport) {
    // Change the viewport on the Rendering API level
    changeViewport(viewport);
    // Update the buffer with the new value
    _gpuBlock._data._ViewPort.set(viewport.x, viewport.y, viewport.z, viewport.w);
    _gpuBlock._updated = true;
}

/// Update the virtual camera's matrices and upload them to the GPU
F32* GFXDevice::lookAt(const mat4<F32>& viewMatrix, const vec3<F32>& eyePos) {
    bool updated = false;

    GPUBlock::GPUData& data = _gpuBlock._data;

    if (eyePos != _gpuBlock._data._cameraPosition) {
        data._cameraPosition.set(eyePos);
        updated = true;
    }

    if (viewMatrix != _gpuBlock._data._ViewMatrix) {
        data._ViewMatrix.set(viewMatrix);
        updated = true;
    }

    if (updated) {
        data._ViewProjectionMatrix.set(data._ViewMatrix * data._ProjectionMatrix);
        _gpuBlock._updated = true;
    }

    return data._ViewMatrix.mat;
}

/// Enable an orthographic projection and upload the corresponding matrices to
/// the GPU
F32* GFXDevice::setProjection(const vec4<F32>& rect, const vec2<F32>& planes) {
    GPUBlock::GPUData& data = _gpuBlock._data;

    data._ProjectionMatrix.ortho(rect.x, rect.y, rect.z, rect.w,
                                 planes.x, planes.y);

    data._ZPlanesCombined.xy(planes);

    data._ViewProjectionMatrix.set(data._ViewMatrix * data._ProjectionMatrix);

    _gpuBlock._updated = true;

    return data._ProjectionMatrix.mat;
}

/// Enable a perspective projection and upload the corresponding matrices to the
/// GPU
F32* GFXDevice::setProjection(F32 FoV, F32 aspectRatio,
                              const vec2<F32>& planes) {
    GPUBlock::GPUData& data = _gpuBlock._data;

    data._ProjectionMatrix.perspective(Angle::DegreesToRadians(FoV),
                                       aspectRatio,
                                       planes.x, planes.y);

    data._ZPlanesCombined.xy(planes);

    data._ViewProjectionMatrix.set(data._ViewMatrix * data._ProjectionMatrix);

    _gpuBlock._updated = true;

    return data._ProjectionMatrix.mat;
}

/// Calculate a frustum for the requested eye (left-right frustum) for anaglyph
/// rendering
void GFXDevice::setAnaglyphFrustum(F32 camIOD, const vec2<F32>& zPlanes,
                                   F32 aspectRatio, F32 verticalFoV,
                                   bool rightFrustum) {
    // Only update frustum values if the interocular distance changed from the
    // previous request
    if (!COMPARE(_anaglyphIOD, camIOD)) {
        static const F32 DTR = 0.0174532925f;
        static const F32 screenZ = 10.0f;

        // Sets top of frustum based on FoV-Y and near clipping plane
        F32 top = zPlanes.x * std::tan(DTR * verticalFoV * 0.5f);
        F32 right = aspectRatio * top;
        // Sets right of frustum based on aspect ratio
        F32 frustumshift = (camIOD / 2) * zPlanes.x / screenZ;

        _leftCam.topfrustum = top;
        _leftCam.bottomfrustum = -top;
        _leftCam.leftfrustum = -right + frustumshift;
        _leftCam.rightfrustum = right + frustumshift;
        _leftCam.modeltranslation = camIOD / 2;

        _rightCam.topfrustum = top;
        _rightCam.bottomfrustum = -top;
        _rightCam.leftfrustum = -right - frustumshift;
        _rightCam.rightfrustum = right - frustumshift;
        _rightCam.modeltranslation = -camIOD / 2;

        _anaglyphIOD = camIOD;
    }
    // Set a camera for the requested eye's frustum
    CameraFrustum& tempCam = rightFrustum ? _rightCam : _leftCam;
    // Update the GPU data buffer with the proper projection data based on the
    // eye camera's frustum
    GPUBlock::GPUData& data = _gpuBlock._data;

    data._ProjectionMatrix.frustum(to_float(tempCam.leftfrustum),
                                   to_float(tempCam.rightfrustum),
                                   to_float(tempCam.bottomfrustum),
                                   to_float(tempCam.topfrustum),
                                   zPlanes.x,
                                   zPlanes.y);

    // Translate the matrix to cancel parallax
    data._ProjectionMatrix.translate(tempCam.modeltranslation, 0.0, 0.0);

    data._ZPlanesCombined.xy(zPlanes);

    data._ViewProjectionMatrix.set(data._ViewMatrix * data._ProjectionMatrix);

    _gpuBlock._updated = true;
}

/// Enable or disable 2D rendering mode 
/// (orthographic projection, no depth reads)
void GFXDevice::toggle2D(bool state) {
    // Remember the previous state hash
    static U32 previousStateBlockHash = 0;
    // Prevent double 2D toggle to the same state (e.g. in a loop)
    if (state == _2DRendering) {
        return;
    }
    Kernel& kernel = Application::getInstance().getKernel();
    _2DRendering = state;
    // If we need to enable 2D rendering
    if (state) {
        // Activate the 2D render state block
        previousStateBlockHash = setStateBlock(_state2DRenderingHash);
        // Push the 2D camera
        kernel.getCameraMgr().pushActiveCamera(_2DCamera);
        // Upload 2D camera matrices to the GPU
        _2DCamera->renderLookAt();
    } else {
        // Reverting to 3D implies popping the 2D camera
        kernel.getCameraMgr().popActiveCamera();
        // And restoring the previous state block
        setStateBlock(previousStateBlockHash);
    }
}

/// Update the rendering viewport
void GFXDevice::setViewport(const vec4<I32>& viewport) {
    // Avoid redundant changes
    _viewportUpdate = !viewport.compare(_viewport.top());

    if (_viewportUpdate) {
        // Push the new viewport
        _viewport.push(viewport);
        // Activate the new viewport
        updateViewportInternal(viewport);
    }
}

/// Restore the viewport to it's previous value
void GFXDevice::restoreViewport() {
    // If we didn't push a new viewport, there's nothing to pop
    if (!_viewportUpdate) {
        return;
    }
    // Restore the viewport
    _viewport.pop();
    // Activate the new top viewport
    updateViewportInternal(_viewport.top());
    _viewportUpdate = false;
}

/// Set a new viewport clearing the previous stack first
void GFXDevice::setBaseViewport(const vec4<I32>& viewport) {
    while (!_viewport.empty()) {
        _viewport.pop();
    }
    _viewport.push(viewport);

    // Set the new viewport
    updateViewportInternal(viewport);
    // The forced viewport can't be popped
    _viewportUpdate = false;
}
  
bool GFXDevice::postProcessingEnabled() const {
    return _enablePostProcessing && 
            //HACK: postprocessing not working with deferred rendering! -Ionut
            getRenderer().getType() != RendererType::RENDERER_DEFERRED_SHADING;
}

void GFXDevice::onCameraUpdate(Camera& camera) {
    if (drawDebugAxis()) {
        // We need to transform the gizmo so that it always remains axis aligned
        // Create a world matrix using a look at function with the eye position
        // backed up from the camera's view direction
        _axisGizmo->worldMatrix(
            mat4<F32>(-camera.getViewDir() * 2, VECTOR3_ZERO, camera.getUpDir()) *
            _gpuBlock._data._ViewMatrix.getInverse());
        _axisGizmo->paused(false);
    } else {
        _axisGizmo->paused(true);
    }
}

/// Depending on the context, either immediately call the function, or pass it
/// to the loading thread via a queue
bool GFXDevice::loadInContext(const CurrentContext& context,
                              const DELEGATE_CBK<>& callback) {
    // Skip invalid callbacks
    if (!callback) {
        return false;
    }
    // If we want and can call the function in the loading thread, add it to the
    // lock-free, single-producer, single-consumer queue
    if (context == CurrentContext::GFX_LOADING_CTX &&
        _state.loadingThreadAvailable()) {
        _state.addToLoadQueue(callback);
    } else {
        callback();
    }
    // The callback is valid and has been processed
    return true;
}

/// Transform our depth buffer to a HierarchicalZ buffer (for occlusion queries)
void GFXDevice::constructHIZ() {
    // We don't want to change the viewport or clear the buffer when starting to
    // render to the buffer
    Framebuffer::FramebufferTarget hizTarget;
    // We want to process the data already in the buffer
    hizTarget._clearBuffersOnBind = false;
    // And we calculate the target viewport for each loop
    hizTarget._changeViewport = false;
    hizTarget._drawMask = Framebuffer::FramebufferTarget::BufferMask::DEPTH;
    // The depth buffer's resolution should be equal to the screen's resolution
    vec2<U16> resolution =
        _renderTarget[to_uint(RenderTarget::SCREEN)]
            ->getResolution();
    // We use a special shader that downsamples the buffer
    // We will use a state block that disables color writes as we will render
    // only a depth image,
    // disables depth testing but allows depth writes
    // Set the depth buffer as the currently active render target
    _renderTarget[to_uint(RenderTarget::DEPTH)]->begin(hizTarget);
    // Bind the depth texture to the first texture unit
    _renderTarget[to_uint(RenderTarget::DEPTH)]->bind(to_ubyte(ShaderProgram::TextureUsage::UNIT0),
                                                      TextureDescriptor::AttachmentType::Depth);
    // Calculate the number of mipmap levels we need to generate
    U32 numLevels = 1 + to_uint(floorf(log2f(fmaxf(to_float(resolution.width),
                                                   to_float(resolution.height)))));
    // Store the current width and height of each mip
    U16 currentWidth = resolution.width;
    U16 currentHeight = resolution.height;
    vec4<I32> previousViewport(_viewport.top());
    // We skip the first level as that's our full resolution image
    for (U16 i = 1; i < numLevels; ++i) {
        // Inform the shader of the resolution we are downsampling from
        _HIZConstructProgram->Uniform("LastMipSize",
                                      vec2<I32>(currentWidth, currentHeight));
        // Calculate next viewport size
        currentWidth /= 2;
        currentHeight /= 2;
        // Ensure that the viewport size is always at least 1x1
        currentWidth = currentWidth > 0 ? currentWidth : 1;
        currentHeight = currentHeight > 0 ? currentHeight : 1;
        // Update the viewport with the new resolution
        updateViewportInternal(vec4<I32>(0, 0, currentWidth, currentHeight));
        // Bind next mip level for rendering but first restrict fetches only to previous level
        _renderTarget[to_uint(RenderTarget::DEPTH)]->setMipLevel(
            i - 1, i - 1, i, TextureDescriptor::AttachmentType::Depth);
        // Dummy draw command as the full screen quad is generated completely in the vertex shader
        drawTriangle(_stateDepthOnlyRenderingHash, _HIZConstructProgram);
    }
    updateViewportInternal(previousViewport);
    // Reset mipmap level range for the depth buffer
    _renderTarget[to_uint(RenderTarget::DEPTH)]
        ->resetMipLevel(TextureDescriptor::AttachmentType::Depth);
    // Unbind the render target
    _renderTarget[to_uint(RenderTarget::DEPTH)]->end();
    
}

/// Find an unused primitive object or create a new one and return it
IMPrimitive* GFXDevice::getOrCreatePrimitive(bool allowPrimitiveRecycle) {
    IMPrimitive* tempPriv = nullptr;
    // Find an available and unused primitive (a zombie primitive)
    vectorImpl<IMPrimitive*>::iterator it;
    it = std::find_if(std::begin(_imInterfaces), std::end(_imInterfaces),
                      [](IMPrimitive* const priv) { 
                            return (priv && !priv->inUse()); 
                      });
    // If we allow resurrected primitives check if we have one available
    if (allowPrimitiveRecycle && it != std::end(_imInterfaces)) {
        tempPriv = *it;
        // If we have a valid zombie, resurrect it
        tempPriv->clear();

    } else {
        // If we do not have a valid zombie, we create a new primitive
        tempPriv = newIMP();
        // And add it to our container. The GFXDevice class is responsible for
        // deleting these!
        _imInterfaces.push_back(tempPriv);
    }
    // Toggle zombification of the primitive on or off depending on our request
    tempPriv->_canZombify = allowPrimitiveRecycle;

    return tempPriv;
}

/// Extract the pixel data from the main render target's first color attachment
/// and save it as a TGA image
void GFXDevice::Screenshot(const stringImpl& filename) {
    // Get the screen's resolution
    const vec2<U16>& resolution =
        _renderTarget[to_uint(RenderTarget::SCREEN)]
            ->getResolution();
    // Allocate sufficiently large buffers to hold the pixel data
    U32 bufferSize = resolution.width * resolution.height * 4;
    U8* imageData = MemoryManager_NEW U8[bufferSize];
    // Read the pixels from the main render target (RGBA16F)
    _renderTarget[to_uint(RenderTarget::SCREEN)]->readData(
        GFXImageFormat::RGBA, GFXDataFormat::UNSIGNED_BYTE, imageData);
    // Save to file
    ImageTools::SaveSeries(filename,
                           vec2<U16>(resolution.width, resolution.height), 32,
                           imageData);
    // Delete local buffers
    MemoryManager::DELETE_ARRAY(imageData);
}
};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/GFXDevice.cpp

Diff revisions: vs.
Revision Author Commited Message
557 Diff Diff IonutCava picture IonutCava Tue 10 Nov, 2015 12:00:20 +0000

[IonutCava]
- Gather all rendering calls from glVertexArray and glGenericVertexData into a single function in glResources: submitRenderCommand.
— This allows both classes to interpret GenericDrawCommands in the same way
- Use unsigned ints for all hasehs instead of size_t to avoid negative number hashes (useful for debugging)

556 Diff Diff IonutCava picture IonutCava Mon 09 Nov, 2015 23:10:09 +0000

[IonutCava]
- Fixed faulty VB partitioning code
- Cleanup in state management system:
— Remove some useless clearing of buffers (e.g. in glimBatch code)
— Changing a VAO will invalidate the element buffer cache
-— ToDo: Investigate all states that need to invalidate cache on VAO change
- Don’t add RenderingComponent to the SceneRoot node

Known bug:
- If we are rendering only the skybox, it is not rendered properly until a different node comes into view. Needs investigation. Not state/shader or texture issue!

552 Diff Diff IonutCava picture IonutCava Fri 06 Nov, 2015 17:07:22 +0000

[Ionut]
- Use UNIT0 and UNIT1 during depth pass for transparency texture to reduce texture binds between calls
- Allow different VAOs with different attributes per render stage per glVertexArray instance
- Initial work on multiple glVertexArray instances per single VBO (attempt to get VBOs to the 4MB mark) - Currently crashes the build
- Fix a bug on exit if background tasks depended on scene elements. Wait for the main threadpool to clear before destroying the scene.
- Use a different node buffer per rendering stage to avoid waiting too much on fence syncs

549 Diff Diff IonutCava picture IonutCava Wed 04 Nov, 2015 22:19:15 +0000

[IonutCava]
- Axis gizmo on/off toggle fixes
- Removed the manual call to makeTextureResident in RenderingComponent. Used registerTextureDependency instead
- Small cleanup in Material class

546 Diff Diff IonutCava picture IonutCava Tue 03 Nov, 2015 23:40:35 +0000

[IonutCava]
- Single pass (layered rendering) cascaded shadowmaps including blur
- Naming convention fixes

543 Diff Diff IonutCava picture IonutCava Fri 30 Oct, 2015 17:18:18 +0000

[IonutCava]
- More profile guided optimizations
- Attempt to render all CSM splits in a single pass using geometry shader based instancing (unfinished)

542 Diff Diff IonutCava picture IonutCava Thu 29 Oct, 2015 23:44:59 +0000

[IonutCava]
- Improved multi-threaded culling
- Increased nodeBuffer size to avoid different render stages to override each-other’s data and wait for locks
- Added a indirect command buffer per render stage

535 Diff Diff IonutCava picture IonutCava Tue 27 Oct, 2015 17:03:35 +0000

[IonutCava]
- Improve occlusion culling
- Improve draw command management

532 Diff Diff IonutCava picture IonutCava Thu 22 Oct, 2015 15:40:26 +0000

[IonutCava]
- Some atomic counter improvements / fixes
- Some GL message parsing improvements

529 IonutCava picture IonutCava Mon 19 Oct, 2015 20:34:35 +0000

[IonutCava]
- Some alignment fixes
- Removed some redundant shader binds