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
706
#include "config.h"

#include "Headers/glFramebuffer.h"

#include "Platform/Video/OpenGL/Headers/GLWrapper.h"
#include "Platform/Video/OpenGL/Headers/glResources.h"
#include "Platform/Video/OpenGL/Textures/Headers/glSamplerObject.h"

#include "Core/Headers/ParamHandler.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Textures/Headers/Texture.h"

namespace Divide {

namespace {
    const char* getAttachmentName(RTAttachment::Type type) {
        switch(type) {
            case RTAttachment::Type::Colour  : return "Colour";
            case RTAttachment::Type::Depth   : return "Depth";
            case RTAttachment::Type::Stencil : return "Stencil";
        };

        return "ERROR";
    };
};

#if defined(ENABLE_GPU_VALIDATION)
    bool glFramebuffer::_bufferBound = false;
#endif

bool glFramebuffer::_viewportChanged = false;

IMPLEMENT_CUSTOM_ALLOCATOR(glFramebuffer, 0, 0)
glFramebuffer::glFramebuffer(GFXDevice& context, bool useResolveBuffer)
    : RenderTarget(context, useResolveBuffer),
      _resolveBuffer(useResolveBuffer ? MemoryManager_NEW glFramebuffer(context)
                                      : nullptr),
      _resolved(false),
      _isLayeredDepth(false),
      _isCreated(false),
      _framebufferHandle(0)
{
    _previousMask.enableAll();

    I32 maxColourAttachments = ParamHandler::instance().getParam<I32>(_ID("rendering.maxRenderTargetOutputs"), 32);
    assert(maxColourAttachments > RenderTarget::g_maxColourAttachments);


    _attachments.init<glRTAttachment>(RenderTarget::g_maxColourAttachments);
}

glFramebuffer::~glFramebuffer() {
    destroy();
    MemoryManager::DELETE(_resolveBuffer);
}

void glFramebuffer::initAttachment(RTAttachment::Type type,
                                   U8 index,
                                   TextureDescriptor& texDescriptor,
                                   bool resize) {
    const RTAttachment_ptr& attachment = _attachments.get(type, index);
    // If it changed
    bool shouldResize = resize && attachment->used();
    if (!attachment->changed() && !shouldResize) {
        return;
    }

    DIVIDE_ASSERT(_width != 0 && _height != 0,
                  "glFramebuffer error: Invalid frame buffer dimensions!");

    if (_multisampled) {
        if (texDescriptor._type == TextureType::TEXTURE_2D) {
            texDescriptor._type = TextureType::TEXTURE_2D_MS;
        }
        if (texDescriptor._type == TextureType::TEXTURE_2D_ARRAY) {
            texDescriptor._type = TextureType::TEXTURE_2D_ARRAY_MS;
        }
    } else {
        if (texDescriptor._type == TextureType::TEXTURE_2D_MS) {
            texDescriptor._type = TextureType::TEXTURE_2D;
        }
        if (texDescriptor._type == TextureType::TEXTURE_2D_ARRAY_MS) {
            texDescriptor._type = TextureType::TEXTURE_2D_ARRAY;
        }
    }

    if (texDescriptor.getSampler().srgb()) {
        if (texDescriptor._internalFormat == GFXImageFormat::RGBA8) {
            texDescriptor._internalFormat = GFXImageFormat::SRGBA8;
        }
        if (texDescriptor._internalFormat == GFXImageFormat::RGB8) {
            texDescriptor._internalFormat = GFXImageFormat::SRGB8;
        }
    } else {
        if (texDescriptor._internalFormat == GFXImageFormat::SRGBA8) {
            texDescriptor._internalFormat = GFXImageFormat::RGBA8;
        }
        if (texDescriptor._internalFormat == GFXImageFormat::SRGB8) {
            texDescriptor._internalFormat = GFXImageFormat::RGB8;
        }
    }

    if (_multisampled) {
        texDescriptor.getSampler().toggleMipMaps(false);
    }

    glRTAttachment* att = static_cast<glRTAttachment*>(attachment.get());

    bool newTexture = false;
    Texture_ptr tex = nullptr;
    if (!att->asTexture()) {
        stringImpl attachmentName = Util::StringFormat(
            "Framebuffer_Att_%s_%d_%d", getAttachmentName(type), index, getGUID());

        ResourceDescriptor textureAttachment(attachmentName);
        textureAttachment.setThreadedLoading(false);
        textureAttachment.setPropertyDescriptor(texDescriptor.getSampler());
        textureAttachment.setEnumValue(to_uint(texDescriptor._type));
        tex = CreateResource<Texture>(textureAttachment);
        assert(tex);
        att->setTexture(tex);
        newTexture = true;
    }
    
    att->mipMapLevel(0,
                     texDescriptor.getSampler().generateMipMaps()
                         ? 1 + (I16)floorf(log2f(fmaxf(to_float(_width), to_float(_height))))
                         : 1);

    tex->setNumLayers(texDescriptor._layerCount);
    tex->lockAutomaticMipMapGeneration(!texDescriptor.automaticMipMapGeneration());

    if (resize) {
        tex->resize(NULL, vec2<U16>(_width, _height), att->mipMapLevel());
    } else {
        if (newTexture) {
            Texture::TextureLoadInfo info;
            info._type = texDescriptor._type;
            tex->loadData(info, texDescriptor, NULL, vec2<U16>(_width, _height), att->mipMapLevel());
        }
    }

    // Attach to frame buffer
    GLenum attachmentEnum;
    if (type == RTAttachment::Type::Depth) {
        attachmentEnum = GL_DEPTH_ATTACHMENT;
        _isLayeredDepth = (texDescriptor._type == TextureType::TEXTURE_2D_ARRAY ||
            texDescriptor._type == TextureType::TEXTURE_2D_ARRAY_MS ||
            texDescriptor._type == TextureType::TEXTURE_CUBE_MAP ||
            texDescriptor._type == TextureType::TEXTURE_CUBE_ARRAY ||
            texDescriptor._type == TextureType::TEXTURE_3D);
    } else {
        attachmentEnum = GLenum((U32)GL_COLOR_ATTACHMENT0 + index);
    }

    att->setInfo(attachmentEnum, tex->getHandle());
    att->clearRefreshFlag();
    att->flagDirty();
    att->enabled(true);
}

void glFramebuffer::toggleAttachment(RTAttachment::Type type, U8 index, bool state) {
    glRTAttachment* att = static_cast<glRTAttachment*>(_attachments.get(type, index).get());
    std::pair<GLenum, U32> info = att->getInfo();
    if (info.second != 0 && state != att->toggledState()) {
        glNamedFramebufferTexture(_framebufferHandle, info.first, state ? info.second : 0, 0);
        att->toggledState(state);
    }
}

void glFramebuffer::addDepthBuffer() {
    TextureDescriptor& desc = _attachments.get(RTAttachment::Type::Colour, 0)->descriptor();

    TextureType texType = desc._type;

    GFXDataFormat dataType = desc.dataType();
    bool fpDepth = (dataType == GFXDataFormat::FLOAT_16 ||
                    dataType == GFXDataFormat::FLOAT_32);

    const SamplerDescriptor& srcDesc = desc.getSampler();

    SamplerDescriptor screenSampler;
    screenSampler.setFilters(TextureFilter::NEAREST);
    screenSampler.setWrapMode(srcDesc.wrapU(), srcDesc.wrapV(), srcDesc.wrapW());
    
    TextureDescriptor depthDescriptor(
        texType,
        fpDepth ? GFXImageFormat::DEPTH_COMPONENT32F
                : GFXImageFormat::DEPTH_COMPONENT,
        fpDepth ? GFXDataFormat::FLOAT_32 
                : GFXDataFormat::UNSIGNED_INT);

    //screenSampler._useRefCompare = true;  //< Use compare function
    screenSampler._cmpFunc = ComparisonFunction::LEQUAL;  //< Use less or equal
    screenSampler.toggleMipMaps(srcDesc.generateMipMaps());

    depthDescriptor.setSampler(screenSampler);
    depthDescriptor.setLayerCount(desc._layerCount);
    _attachments.get(RTAttachment::Type::Depth, 0)->fromDescriptor(depthDescriptor);
    initAttachment(RTAttachment::Type::Depth, 0, _attachments.get(RTAttachment::Type::Depth, 0)->descriptor(), false);
}

bool glFramebuffer::create(U16 width, U16 height) {
    if (_resolveBuffer) {
        _resolveBuffer->_useDepthBuffer = _useDepthBuffer;

        for (U8 i = 0; i < to_const_ubyte(RTAttachment::Type::COUNT); ++i) {
            RTAttachment::Type type = static_cast<RTAttachment::Type>(i);
            for (U8 j = 0; j < _attachments.attachmentCount(type); ++j) {
                const RTAttachment_ptr& att = _attachments.get(type, j);
                if (att->changed()) {
                    _resolveBuffer->_attachments.get(type, j)->fromDescriptor(att->descriptor());
                }
                _resolveBuffer->_attachments.get(type, j)->clearColour(att->clearColour());
            }
        }

        _resolveBuffer->create(width, height);
    }

    bool shouldResize = false;
    if (Config::Profile::USE_2x2_TEXTURES) {
        _width = _height = 2;
    } else {
        shouldResize = (_width != width || _height != height);
        _width = width;
        _height = height;
    }

    _isCreated = _isCreated && !_shouldRebuild;

    if (!_isCreated) {
        assert(_framebufferHandle == 0);
        glCreateFramebuffers(1, &_framebufferHandle);
        shouldResize = false;
        _resolved = false;
        _isLayeredDepth = false;
    }


    vectorImpl<GLenum> colorBuffsers;
    // For every attachment, be it a colour or depth attachment ...
    for (U8 i = 0; i < to_const_ubyte(RTAttachment::Type::COUNT); ++i) {
        RTAttachment::Type type = static_cast<RTAttachment::Type>(i);
        for (U8 j = 0; j < _attachments.attachmentCount(type); ++j) {
            const RTAttachment_ptr& att = _attachments.get(type, j);
            initAttachment(static_cast<RTAttachment::Type>(i), j, att->descriptor(), shouldResize);
            if (type == RTAttachment::Type::Colour && att->enabled()) {
                colorBuffsers.push_back(static_cast<glRTAttachment*>(att.get())->getInfo().first);
            }
        }
    }
    
    // If we either specify a depth texture or request a depth buffer ...
    if (_useDepthBuffer && !hasDepth()) {
        addDepthBuffer();
    }
    
    if (!shouldResize) {
        setInitialAttachments();
    }

    // If colour writes are disabled, draw only depth info
    if (!colorBuffsers.empty()) {
        glNamedFramebufferDrawBuffers(_framebufferHandle, 
                                      static_cast<GLsizei>(colorBuffsers.size()),
                                      colorBuffsers.data());
    }

    _isCreated = checkStatus();
    if (_isCreated) {
        clear(RenderTarget::defaultPolicy());
    }
    _shouldRebuild = !_isCreated;
    return _isCreated;
}

void glFramebuffer::destroy() {
    if (_framebufferHandle > 0) {
        glDeleteFramebuffers(1, &_framebufferHandle);
        _framebufferHandle = 0;
    }

    _width = _height = 0;

    if (_resolveBuffer) {
        _resolveBuffer->destroy();
    }
}

void glFramebuffer::resolve() {
    if (_resolveBuffer && !_resolved) {
        _resolveBuffer->blitFrom(this, hasColour(), hasDepth());
        _resolved = true;
    }
}

void glFramebuffer::blitFrom(RenderTarget* inputFB,
                             bool blitColour,
                             bool blitDepth ) {
    if (!inputFB || (!blitColour && !blitDepth)) {
        return;
    }

    glFramebuffer* input = static_cast<glFramebuffer*>(inputFB);

    // prevent stack overflow
    if (_resolveBuffer && (inputFB->getGUID() != _resolveBuffer->getGUID())) {
        input->resolve();
    }

    GLuint previousFB = GL_API::getActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE);
    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_ONLY, input->_framebufferHandle);
    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_WRITE_ONLY, this->_framebufferHandle);

    if (blitColour && hasColour()) {
        RTAttachment::Type type = RTAttachment::Type::Colour;
        U8 colourCount = _attachments.attachmentCount(type);
        for (U8 j = 0; j < colourCount; ++j) {
            glRTAttachment* thisAtt = static_cast<glRTAttachment*>(_attachments.get(type, j).get());
            glRTAttachment* inputAtt = static_cast<glRTAttachment*>(input->_attachments.get(type, j).get());
            if (thisAtt->enabled()) {
                glDrawBuffer(thisAtt->getInfo().first);
                glReadBuffer(inputAtt->getInfo().first);
                glBlitFramebuffer(0,
                                  0,
                                  input->_width,
                                  input->_height,
                                  0,
                                  0,
                                  this->_width,
                                  this->_height,
                                  GL_COLOR_BUFFER_BIT,
                                  GL_NEAREST);
            }
            _context.registerDrawCall();
        }
    }

    if (blitDepth && hasDepth()) {
        glBlitFramebuffer(0,
                          0,
                          input->_width,
                          input->_height,
                          0,
                          0,
                          this->_width,
                          this->_height,
                          GL_DEPTH_BUFFER_BIT,
                          GL_NEAREST);
        _context.registerDrawCall();
    }

    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE, previousFB);
}

void glFramebuffer::blitFrom(RenderTarget* inputFB,
                             U8 index,
                             bool blitColour,
                             bool blitDepth) {
    if (!inputFB || (!blitColour && !blitDepth)) {
        return;
    }
    
    glFramebuffer* input = static_cast<glFramebuffer*>(inputFB);

    // prevent stack overflow
    if (_resolveBuffer && (inputFB->getGUID() != _resolveBuffer->getGUID())) {
        input->resolve();
    }

    GLuint previousFB = GL_API::getActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE);
    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_ONLY, input->_framebufferHandle);
    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_WRITE_ONLY, this->_framebufferHandle);

    if (blitColour && hasColour()) {
        glRTAttachment* thisAtt = static_cast<glRTAttachment*>(_attachments.get(RTAttachment::Type::Colour, index).get());
        glRTAttachment* inputAtt = static_cast<glRTAttachment*>(input->_attachments.get(RTAttachment::Type::Colour, index).get());
        if (thisAtt->enabled()) {
            glDrawBuffer(thisAtt->getInfo().first);
            glReadBuffer(inputAtt->getInfo().first);
            glBlitFramebuffer(0,
                                0,
                                input->_width,
                                input->_height,
                                0,
                                0,
                                this->_width,
                                this->_height,
                                GL_COLOR_BUFFER_BIT,
                                GL_NEAREST);
            _context.registerDrawCall();
        }
    }

    if (blitDepth && hasDepth()) {
        glBlitFramebuffer(0,
                          0,
                          input->_width,
                          input->_height,
                          0,
                          0,
                          this->_width,
                          this->_height,
                          GL_DEPTH_BUFFER_BIT,
                          GL_NEAREST);
        _context.registerDrawCall();
    }

    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE, previousFB);
}

const RTAttachment& glFramebuffer::getAttachment(RTAttachment::Type type, U8 index, bool flushStateOnRequest) {
    if (_resolveBuffer) {
        resolve();
        return _resolveBuffer->getAttachment(type, flushStateOnRequest);
    }

    const RTAttachment_ptr& att = _attachments.get(type, index);
    if (flushStateOnRequest) {
        att->flush();
    }

    return *att;
}

void glFramebuffer::bind(U8 unit, RTAttachment::Type type, U8 index, bool flushStateOnRequest) {
    const Texture_ptr& attachment = getAttachment(type, index, flushStateOnRequest).asTexture();
    if (attachment != nullptr) {
        attachment->bind(unit, false);
    }
}

void glFramebuffer::resetMipMaps(const RTDrawDescriptor& drawPolicy) {
    for (U8 i = 0; i < to_const_uint(RTAttachment::Type::COUNT); ++i) {
        RTAttachment::Type type = static_cast<RTAttachment::Type>(i);
        for (U8 j = 0; j < _attachments.attachmentCount(type); ++j)  {
            if (drawPolicy._drawMask.enabled(type, j) &&
                _attachments.get(type, j)->asTexture())
            {
                _attachments.get(type, j)->asTexture()->refreshMipMaps();
            }
        }
    }
}

void glFramebuffer::begin(const RTDrawDescriptor& drawPolicy) {
    static vectorImpl<GLenum> colourBuffers;

    DIVIDE_ASSERT(_framebufferHandle != 0,
                  "glFramebuffer error: "
                  "Tried to bind an invalid framebuffer!");

#   if defined(ENABLE_GPU_VALIDATION)
        DIVIDE_ASSERT(!glFramebuffer::_bufferBound,
                      "glFramebuffer error: "
                      "Begin() called without a call to the "
                      "previous bound buffer's End()");
#   endif

    if (drawPolicy._changeViewport) {
        _viewportChanged = true;
        _context.setViewport(vec4<GLint>(0, 0, _width, _height));
    }

    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE, _framebufferHandle);

    if (_resolveBuffer) {
        _resolved = false;
    }

    if (_previousMask != drawPolicy._drawMask) {
        // handle colour buffers first
        colourBuffers.resize(0);
        RTAttachment::Type type = RTAttachment::Type::Colour;
        U8 bufferCount = _attachments.attachmentCount(type);
        for (U8 j = 0; j < bufferCount; ++j) {
            glRTAttachment* thisAtt = static_cast<glRTAttachment*>(_attachments.get(type, j).get());
            thisAtt->enabled(drawPolicy._drawMask.enabled(type, j));
            colourBuffers.push_back(thisAtt->enabled() ? thisAtt->getInfo().first : GL_NONE);
        }
        glDrawBuffers(to_uint(bufferCount), colourBuffers.data());
        
        checkStatus();
        _previousMask = drawPolicy._drawMask;
    }

    clear(drawPolicy);

    resetMipMaps(drawPolicy);

#   if defined(ENABLE_GPU_VALIDATION)
        glFramebuffer::_bufferBound = true;
#   endif
}

void glFramebuffer::end() {
#   if defined(ENABLE_GPU_VALIDATION)
        DIVIDE_ASSERT(glFramebuffer::_bufferBound,
                      "glFramebuffer error: "
                      "End() called without a previous call to Begin()");
#   endif

    GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_WRITE, 0);
    if (_viewportChanged) {
        _context.restoreViewport();
        _viewportChanged = false;
    }

    setInitialAttachments();
    resolve();

#   if defined(ENABLE_GPU_VALIDATION)
        glFramebuffer::_bufferBound = false;
#   endif
}

void glFramebuffer::setInitialAttachments() {
    // Reset attachments if they changed (e.g. after layered rendering);
    for (U8 i = 0; i < to_const_ubyte(RTAttachment::Type::COUNT); ++i) {
        RTAttachment::Type type = static_cast<RTAttachment::Type>(i);
        for (U8 j = 0; j < _attachments.attachmentCount(type); ++j) {
            if (_attachments.get(type, j)->dirty()) {
                toggleAttachment(type, j, true);
                _attachments.get(type, j)->clean();
            }
        }
    }
}

void glFramebuffer::clear(const RTDrawDescriptor& drawPolicy) const {
    if (hasColour() && drawPolicy._clearColourBuffersOnBind) {
        for (U8 index = 0; index < _attachments.attachmentCount(RTAttachment::Type::Colour); ++index) {
            const RTAttachment_ptr& att = _attachments.get(RTAttachment::Type::Colour, index);
            if (att->enabled()) {
                GFXDataFormat dataType = att->descriptor().dataType();
                if(dataType == GFXDataFormat::FLOAT_16 ||dataType == GFXDataFormat::FLOAT_32) {
                    glClearNamedFramebufferfv(_framebufferHandle, GL_COLOR, index, att->clearColour()._v);
                } else {
                    glClearNamedFramebufferuiv(_framebufferHandle, GL_COLOR, index, Util::ToUIntColour(att->clearColour())._v);
                }
                _context.registerDrawCall();
            }
        }
    }

    if (hasDepth() && drawPolicy._clearDepthBufferOnBind) {
        glClearNamedFramebufferfv(_framebufferHandle, GL_DEPTH, 0, &_depthValue);
        _context.registerDrawCall();
    }
}

void glFramebuffer::drawToLayer(RTAttachment::Type type,
                                U8 index,
                                U32 layer,
                                bool includeDepth) {

    const RTAttachment_ptr& att = _attachments.get(type, index);

    GLenum textureType = GLUtil::glTextureTypeTable[to_uint(att->descriptor().type())];
    // only for array textures (it's better to simply ignore the command if the
    // format isn't supported (debugging reasons)
    if (textureType != GL_TEXTURE_2D_ARRAY &&
        textureType != GL_TEXTURE_CUBE_MAP_ARRAY &&
        textureType != GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
        return;
    }

    bool useDepthLayer =
        (hasDepth() && includeDepth) ||
        (hasDepth() && type == RTAttachment::Type::Depth);
    bool useColourLayer =
        (hasColour() && type < RTAttachment::Type::Depth);

    if (useDepthLayer && _isLayeredDepth) {
        const RTAttachment_ptr& attDepth = _attachments.get(RTAttachment::Type::Depth, 0);
        glFramebufferTextureLayer(GL_FRAMEBUFFER,
                                  GL_DEPTH_ATTACHMENT,
                                  attDepth->asTexture()->getHandle(),
                                  0,
                                  layer);
        attDepth->flagDirty();
    }

    if (useColourLayer) {
        glRTAttachment* glAtt = static_cast<glRTAttachment*>(att.get());

        glFramebufferTextureLayer(GL_FRAMEBUFFER,
                                  glAtt->getInfo().first,
                                  att->asTexture()->getHandle(),
                                  0,
                                  layer);
        att->flagDirty();
        att->asTexture()->refreshMipMaps();
    }

    checkStatus();
}

void glFramebuffer::setMipLevel(U16 mipMinLevel, U16 mipMaxLevel, U16 writeLevel, RTAttachment::Type type, U8 index) {
    glRTAttachment* glAtt = static_cast<glRTAttachment*>(_attachments.get(type, index).get());

    if (glAtt->used()) {
        // This is needed because certain drivers need all attachments to use the same mip level
        // This is also VERY SLOW so it might be worth optimising it per-driver version / IHV
        for (U8 i = 0; i < to_const_ubyte(RTAttachment::Type::COUNT); ++i) {
            RTAttachment::Type tempType = static_cast<RTAttachment::Type>(i);
            for (U8 j = 0; j < _attachments.attachmentCount(tempType); ++j) {
                if (type != tempType || j != index) {
                    toggleAttachment(tempType, j, false);
                    _attachments.get(tempType, j)->flagDirty();
                }
            }
        }

        glAtt->asTexture()->setMipMapRange(mipMinLevel, mipMaxLevel);

        glFramebufferTexture(GL_FRAMEBUFFER,
                             glAtt->getInfo().first,
                             glAtt->asTexture()->getHandle(),
                             writeLevel);
        checkStatus();
    }

}

void glFramebuffer::resetMipLevel(RTAttachment::Type type, U8 index) {
    glRTAttachment* glAtt = static_cast<glRTAttachment*>(_attachments.get(type, index).get());
    const vec2<U16>& mips = glAtt->mipMapLevel();

    setMipLevel(mips.x, mips.y, 0, type, index);
}

void glFramebuffer::readData(const vec4<U16>& rect,
                             GFXImageFormat imageFormat,
                             GFXDataFormat dataType,
                             void* outData) {
    if (_resolveBuffer) {
        resolve();
        _resolveBuffer->readData(rect, imageFormat, dataType, outData);
    } else {
        GL_API::setPixelPackUnpackAlignment();
        GL_API::setActiveFB(RenderTarget::RenderTargetUsage::RT_READ_ONLY, _framebufferHandle);
        glReadPixels(
            rect.x, rect.y, rect.z, rect.w,
            GLUtil::glImageFormatTable[to_uint(imageFormat)],
            GLUtil::glDataFormat[to_uint(dataType)], outData);
    }
}

bool glFramebuffer::checkStatus() const {
#if defined(ENABLE_GPU_VALIDATION)
    // check FB status
    switch (glCheckNamedFramebufferStatus(_framebufferHandle, GL_FRAMEBUFFER))
    {
        case GL_FRAMEBUFFER_COMPLETE: {
            return true;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_ATTACHMENT_INCOMPLETE")));
            return false;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_NO_IMAGE")));
            return false;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_INCOMPLETE_DRAW_BUFFER")));
            return false;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_INCOMPLETE_READ_BUFFER")));
            return false;
        }
        case GL_FRAMEBUFFER_UNSUPPORTED: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_UNSUPPORTED")));
            return false;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_INCOMPLETE_MULTISAMPLE")));
            return false;
        }
        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_INCOMPLETE_LAYER_TARGETS")));
            return false;
        }
        case gl::GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: {
            Console::errorfn(Locale::get(_ID("ERROR_RT_DIMENSIONS")));
            return false;
        }
        case gl::GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: {
             Console::errorfn(Locale::get(_ID("ERROR_RT_FORMAT")));
             return false;
        }
        default: {
            Console::errorfn(Locale::get(_ID("ERROR_UNKNOWN")));
            return false;
        }
    };
#else
    return true;
#endif
}

};  // namespace Divide

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Buffers/RenderTarget/glFramebuffer.cpp

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

726 Diff Diff IonutCava picture IonutCava Fri 10 Jun, 2016 16:13:52 +0000

[IonutCava]
- More static analysis based corrections
- Added initial stub code for a command pool system for GenericDrawCommands

722 Diff Diff IonutCava picture IonutCava Thu 09 Jun, 2016 16:15:33 +0000

[IonutCava]
- Restructure RenderTarget system:
— Separate attachments, use an attachment pool, use draw descriptors, require explicit information for RT calls, etc

721 Diff Diff IonutCava picture IonutCava Wed 08 Jun, 2016 15:47:49 +0000

[IonutCava]
- Spelling: change all references from our code of “color” to the PROPER British version of “colour” because it sounds 100x better

720 Diff Diff IonutCava picture IonutCava Mon 06 Jun, 2016 15:51:06 +0000

[IonutCava]
- PVS-Studio guided static analysis fixes and optimizations
- Added a flag to SceneState to bypass the Save/Load system not needed for certain types of scenes (menus, cinematics, etc)

718 IonutCava picture IonutCava Thu 02 Jun, 2016 16:02:48 +0000

[IonutCava]
- Code cleanup:
— Rename Framebuffer to RenderTarget. Only OpenGL uses the Framebuffer nomenclature.
— Remove base Shader class as only OpenGL will have separate shaders (and eventually move to pipeline objects) as Direct3D uses FX files and may end up with different structure
— Remove drawBox3D, drawSphere3D and drawLines from GFXDevice class and add them as member functions to the IMPrimitive class (renamed to fromXYZ)
— Pull some elements from SceneManager and move them to standalone classes (W.I.P.)