summaryrefslogtreecommitdiffstats
path: root/src/GalOgl.cpp
blob: e39f5f00f536e9de4b1dbfad76ec41d298ea6564 (plain) (blame)
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
#include "Gal.hpp"

#include <easylogging++.h>
#include <GL/glew.h>

#include "Utility.hpp"

using namespace Gal;

class ImplOgl;
class FramebufferDefaultOgl;
class ShaderOgl;

std::unique_ptr<ImplOgl> impl;
std::shared_ptr<FramebufferDefaultOgl> fbDefault;

size_t GalTypeGetComponents(Gal::Type type) {
    switch (type) {
    case Type::Float:
    case Type::Double:
    case Type::Uint8:
    case Type::Uint16:
    case Type::Uint32:
    case Type::Int8:
    case Type::Int16:
    case Type::Int32:
        return 1;
    case Type::Vec2:
    case Type::Vec2u8:
    case Type::Vec2u16:
    case Type::Vec2u32:
    case Type::Vec2i8:
    case Type::Vec2i16:
    case Type::Vec2i32:
        return 2;
    case Type::Vec3:
    case Type::Vec3u8:
    case Type::Vec3u16:
    case Type::Vec3u32:
    case Type::Vec3i8:
    case Type::Vec3i16:
    case Type::Vec3i32:
        return 3;
    case Type::Vec4:
    case Type::Vec4u8:
    case Type::Vec4u16:
    case Type::Vec4u32:
    case Type::Vec4i8:
    case Type::Vec4i16:
    case Type::Vec4i32:
    case Type::Mat2:
        return 4;
    case Type::Mat3:
        return 9;
    case Type::Mat4:
        return 16;
    default:
        return 0;
    }
    return 0;
}

size_t GalTypeGetComponentSize(Gal::Type type) {
    switch (type) {	
    case Type::Uint8:
    case Type::Int8:
    case Type::Vec2u8:
    case Type::Vec2i8:
    case Type::Vec3u8:
    case Type::Vec3i8:
    case Type::Vec4u8:
    case Type::Vec4i8:
        return 1;
    case Type::Uint16:
    case Type::Int16:
    case Type::Vec2u16:
    case Type::Vec2i16:
    case Type::Vec3u16:
    case Type::Vec3i16:
    case Type::Vec4u16:
    case Type::Vec4i16:
        return 2;
    case Type::Float:
    case Type::Uint32:
    case Type::Int32:
    case Type::Vec2:
    case Type::Vec2u32:
    case Type::Vec2i32:
    case Type::Vec3:
    case Type::Vec3u32:
    case Type::Vec3i32:
    case Type::Vec4:
    case Type::Vec4u32:
    case Type::Vec4i32:
    case Type::Mat2:
    case Type::Mat3:
    case Type::Mat4:
        return 4;
    case Type::Double:
        return 8;
    default:
        return 0;
    }
}

size_t GalTypeGetSize(Gal::Type type) {
    return GalTypeGetComponents(type) * GalTypeGetComponentSize(type);
}

GLenum GalTypeGetComponentGlType(Gal::Type type) {
    switch (type) {
    case Type::Float:
    case Type::Vec2:
    case Type::Vec3:
    case Type::Vec4:
    case Type::Mat2:
    case Type::Mat3:
    case Type::Mat4:
        return GL_FLOAT;
    case Type::Double:
        return GL_DOUBLE;
    case Type::Uint8:
    case Type::Vec2u8:
    case Type::Vec3u8:
    case Type::Vec4u8:
        return GL_UNSIGNED_BYTE;
    case Type::Uint16:
    case Type::Vec2u16:
    case Type::Vec3u16:
    case Type::Vec4u16:
        return GL_UNSIGNED_SHORT;
    case Type::Uint32:
    case Type::Vec2u32:
    case Type::Vec3u32:
    case Type::Vec4u32:
        return GL_UNSIGNED_INT;
    case Type::Int8:
    case Type::Vec2i8:
    case Type::Vec3i8:
    case Type::Vec4i8:
        return GL_BYTE;
    case Type::Int16:
    case Type::Vec2i16:
    case Type::Vec3i16:
    case Type::Vec4i16:
        return GL_SHORT;
    case Type::Int32:
    case Type::Vec2i32:
    case Type::Vec3i32:
    case Type::Vec4i32:
        return GL_INT;
    default:
        return 0;
    }
    return 0;
}

size_t GalFormatGetSize(Format format) {
    switch (format) {
    case Format::R8G8B8:
        return 3;
    case Format::R8G8B8A8:
        return 4;
    default:
        return 0;
    }
    return 0;
}

GLenum GalFormatGetGlInternalFormat(Format format) {
    switch (format) {
    case Format::R8G8B8:
        return GL_RGB8;
    case Format::R8G8B8A8:
        return GL_RGBA8;
    default:
        return 0;
    }
    return 0;
}

GLenum GalFormatGetGlFormat(Format format) {
    switch (format) {
    case Format::R8G8B8:
        return GL_RGB;
    case Format::R8G8B8A8:
        return GL_RGBA;
    default:
        return 0;
    }
    return 0;
}

GLenum GalFormatGetGlType(Format format) {
    switch (format) {
    case Format::R8G8B8:
        return GL_UNSIGNED_BYTE;
    case Format::R8G8B8A8:
        return GL_UNSIGNED_BYTE;
    default:
        return 0;
    }
    return 0;
}

GLenum GalFilteringGetGlType(Filtering filtering) {
    switch (filtering) {
    case Filtering::Nearest:
        return GL_NEAREST;
    case Filtering::Bilinear:
        return GL_LINEAR;
    case Filtering::Trilinear:
        return GL_LINEAR_MIPMAP_LINEAR;
    case Filtering::Anisotropy:
        return GL_LINEAR;
    default:
        return 0;
    }
    return 0;
}

GLenum GalWrappingGetGlType(Wrapping wrapping) {
    switch (wrapping) {
    case Wrapping::Repeat:
        return GL_REPEAT;
    case Wrapping::Clamp:
        return GL_CLAMP_TO_EDGE;
    case Wrapping::Mirror:
        return GL_MIRRORED_REPEAT;
    default:
        return 0;
    }
    return 0;
}


class ShaderOgl : public Shader {
public:
    bool isVertex = true;
    std::string code;
};

class BufferBindingOgl : public BufferBinding {
public:
    BufferBindingOgl(size_t id) : bufferId(id) {}

    const size_t bufferId;

    static constexpr size_t indexValue = (std::numeric_limits<size_t>::max)(); //parenthess for windows' max macro
};

class BufferOgl : public Buffer {
public:
    GLuint vbo;

    virtual void SetData(std::vector<std::byte>&& data) override {
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, data.size(), data.data(), GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glCheckError();
    }
};

class TextureConfigOgl : public TextureConfig {
public:

    Format format;
    size_t width = 1, height = 1, depth = 1;
    bool interpolateLayers = false;

    Filtering min = Filtering::Nearest, max = Filtering::Nearest;
    Wrapping wrap = Wrapping::Clamp;
    

    virtual void SetMinFilter(Filtering filter) override {
        min = filter;
    }

    virtual void SetMaxFilter(Filtering filter) override {
        max = filter;
    }

    virtual void SetWrapping(Wrapping wrapping) override {
        wrap = wrapping;
    }

};

class TextureOgl : public Texture {
public:

    GLenum type;
    GLuint texture;
    Format format;
    size_t width, height, depth;

    virtual void SetData(std::vector<std::byte>&& data, size_t mipLevel = 0) override {
        size_t expectedSize = width * height * depth * GalFormatGetSize(format);
        if (data.size() != expectedSize)
            throw std::logic_error("Size of data is not valid for this texture");

        glBindTexture(type, texture);
        glCheckError();

        switch (type) {
        case GL_TEXTURE_2D:
        case GL_PROXY_TEXTURE_2D:
        case GL_TEXTURE_1D_ARRAY:
        case GL_PROXY_TEXTURE_1D_ARRAY:
        case GL_TEXTURE_RECTANGLE:
        case GL_PROXY_TEXTURE_RECTANGLE:
        case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
        case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
        case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
        case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
        case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
        case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
        case GL_PROXY_TEXTURE_CUBE_MAP:
            glTexImage2D(type, mipLevel, GalFormatGetGlInternalFormat(format), width, height, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.data());
            break;
        case GL_TEXTURE_3D:
        case GL_PROXY_TEXTURE_3D:
        case GL_TEXTURE_2D_ARRAY:
        case GL_PROXY_TEXTURE_2D_ARRAY:
            glTexImage3D(type, mipLevel, GalFormatGetGlInternalFormat(format), width, height, depth, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.data());
            break;
        default:
            throw std::runtime_error("Unknown texture type");
        }

        glCheckError();

        glBindTexture(type, 0);
    }

};

class PipelineConfigOgl : public PipelineConfig {
public:
    std::shared_ptr<ShaderOgl> vertexShader, pixelShader;
    std::map<std::string, Type> shaderParameters;
    std::shared_ptr<Framebuffer> targetFb;
    std::vector<std::vector<VertexAttribute>> vertexBuffers;

public:
    virtual void SetVertexShader(std::shared_ptr<Shader> shader) override {
        vertexShader = std::static_pointer_cast<ShaderOgl,Shader>(shader);
    }

    virtual void SetPixelShader(std::shared_ptr<Shader> shader) override {
        pixelShader = std::static_pointer_cast<ShaderOgl, Shader>(shader);
    }

    virtual void AddShaderParameter(std::string_view name, Type type) override {
        shaderParameters.emplace(std::string(name), type);
    }

    virtual void SetTarget(std::shared_ptr<Framebuffer> target) override {
        targetFb = target;
    }

    virtual std::shared_ptr<BufferBinding> BindVertexBuffer(std::vector<VertexAttribute> &&bufferLayout) override {
        auto binding = std::make_shared<BufferBindingOgl>(vertexBuffers.size());
        vertexBuffers.push_back(bufferLayout);
        return std::static_pointer_cast<BufferBinding, BufferBindingOgl>(binding);
    }

    virtual std::shared_ptr<BufferBinding> BindIndexBuffer() override {
        auto binding = std::make_shared<BufferBindingOgl>(BufferBindingOgl::indexValue);
        return std::static_pointer_cast<BufferBinding, BufferBindingOgl>(binding);
    }
    
};

class PipelineInstanceOgl : public PipelineInstance {
public:
    GLuint vao;

    virtual void Activate() override {
        glBindVertexArray(vao);
        glCheckError();
    }

    virtual void Render(size_t offset = 0, size_t count = -1) override {
        glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
        glCheckError();
    }
};

class PipelineOgl : public Pipeline {
public:
    std::map<std::string, size_t> shaderParameters;
    GLuint program;
    struct VertexBindingCommand {
        size_t bufferId;
        size_t location;
        GLenum type;
        size_t count;
        size_t stride;
        size_t offset;
    };
    std::vector<VertexBindingCommand> vertexBindCmds;
    
    virtual void Activate() override {
        glUseProgram(program);
        glCheckError();
    }

    virtual void SetDynamicTexture(std::string_view name, std::shared_ptr<Texture> texture) override {
        Activate();
        glActiveTexture(GL_TEXTURE0);
        auto tex = std::static_pointer_cast<TextureOgl>(texture);
        glBindTexture(tex->type, tex->texture);
        SetShaderParameter(name, 0);
    }

    virtual std::shared_ptr<PipelineInstance> CreateInstance(std::vector<std::pair<std::shared_ptr<BufferBinding>, std::shared_ptr<Buffer>>>&& buffers) override {
        auto instance = std::make_shared<PipelineInstanceOgl>();

        size_t indexBuffer = BufferBindingOgl::indexValue;
        std::map<size_t, size_t> bufferBindingId;

        for (auto&& [binding, buffer] : buffers) {
            auto bind = std::static_pointer_cast<BufferBindingOgl, BufferBinding>(binding);
            auto buff = std::static_pointer_cast<BufferOgl, Buffer>(buffer);

            if (bind->bufferId == BufferBindingOgl::indexValue)
                indexBuffer = buff->vbo;
            else
                bufferBindingId.insert({ bind->bufferId,buff->vbo });
        }

        glGenVertexArrays(1, &instance->vao);
        glBindVertexArray(instance->vao);
        glCheckError();

        for (const auto& cmd : vertexBindCmds) {
            glBindBuffer(GL_ARRAY_BUFFER, bufferBindingId.find(cmd.bufferId)->second);
            glCheckError();
            switch (cmd.type) {
            case GL_FLOAT:
            case GL_DOUBLE:
                glVertexAttribPointer(cmd.location, cmd.count, cmd.type, GL_FALSE, cmd.offset, reinterpret_cast<void*>(cmd.stride));
                break;
            case GL_UNSIGNED_BYTE:
            case GL_BYTE:
            case GL_UNSIGNED_SHORT:
            case GL_SHORT:
            case GL_UNSIGNED_INT:
            case GL_INT:
                glVertexAttribIPointer(cmd.location, cmd.count, cmd.type, cmd.offset, reinterpret_cast<void*>(cmd.stride));
                break;
            }
            
            glCheckError();
            glEnableVertexAttribArray(cmd.location);
            glCheckError();
        }

        if (indexBuffer != BufferBindingOgl::indexValue) {
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
        }

        glBindVertexArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glCheckError();

        return instance;
    }

    virtual void SetShaderParameter(std::string_view name, float value) override {
        Activate();
        glUniform1f(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, double value) override {
        Activate();
        glUniform1d(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, int8_t value) override {
        Activate();
        glUniform1i(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, int16_t value) override {
        Activate();
        glUniform1i(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, int32_t value) override {
        Activate();
        glUniform1i(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, uint8_t value) override {
        Activate();
        glUniform1ui(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, uint16_t value) override {
        Activate();
        glUniform1ui(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, uint32_t value) override {
        Activate();
        glUniform1ui(shaderParameters.at(std::string(name)), value);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, glm::vec2 value) override {
        Activate();
        glUniform2f(shaderParameters.at(std::string(name)), value.x, value.y);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, glm::uvec2 value) override {
        Activate();
        glUniform2ui(shaderParameters.at(std::string(name)), value.x, value.y);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, glm::vec3 value) override {
        Activate();
        glUniform3f(shaderParameters.at(std::string(name)), value.x, value.y, value.z);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, glm::vec4 value) override {
        Activate();
        glUniform4f(shaderParameters.at(std::string(name)), value.x, value.y, value.z, value.w);
        glCheckError();
    }

    virtual void SetShaderParameter(std::string_view name, glm::mat4 value) override {
        Activate();
        glCheckError();
    }
};

class ImplOgl : public Impl {

public:

    virtual void Init() override {
        LOG(INFO) << "Initalizing Gal:OpenGL...";
        LOG(INFO) << "Initializing GLEW";
        glewExperimental = GL_TRUE;
        GLenum glewStatus = glewInit();
        glCheckError();
        if (glewStatus != GLEW_OK) {
            LOG(FATAL) << "Failed to initialize GLEW: " << glewGetErrorString(glewStatus);
        }
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);

        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glFrontFace(GL_CCW);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glCheckError();
        if (glActiveTexture == nullptr) {
            throw std::runtime_error("GLEW initialization failed with unknown reason");
        }
    }

    virtual void DeInit() override {
        LOG(INFO) << "Destroying Gal:OpenGL...";
    }

    virtual void Cleanup() override {

    }


    virtual std::shared_ptr<Buffer> CreateBuffer() override {
        auto buff = std::make_shared<BufferOgl>();
        glGenBuffers(1, &buff->vbo);
        buff->SetData({});
        glCheckError();
        return std::static_pointer_cast<Buffer, BufferOgl>(buff);
    }


    virtual std::shared_ptr<TextureConfig> CreateTexture2DConfig(size_t width, size_t height, Format format) override {
        auto config = std::make_shared<TextureConfigOgl>();

        config->width = width;
        config->height = height;
        config->depth = 1;
        config->format = format;

        return std::static_pointer_cast<TextureConfig, TextureConfigOgl>(config);
    }

    virtual std::shared_ptr<TextureConfig> CreateTexture3DConfig(size_t width, size_t height, size_t depth, bool interpolateLayers, Format format) override {
        auto config = std::make_shared<TextureConfigOgl>();

        config->width = width;
        config->height = height;
        config->depth = depth;
        config->interpolateLayers = interpolateLayers;
        config->format = format;

        return std::static_pointer_cast<TextureConfig, TextureConfigOgl>(config);
    }

    virtual std::shared_ptr<Texture> BuildTexture(std::shared_ptr<TextureConfig> config) override {
        auto texConfig = std::static_pointer_cast<TextureConfigOgl, TextureConfig>(config);
        auto texture = std::make_shared<TextureOgl>();

        texture->type = GL_TEXTURE_2D;
        texture->format = texConfig->format;
        texture->width = texConfig->width;
        texture->height = texConfig->height;
        texture->depth = texConfig->depth;

        glGenTextures(1, &texture->texture);
        glCheckError();
        glBindTexture(texture->type, texture->texture);

        glTexParameteri(texture->type, GL_TEXTURE_MIN_FILTER, GalFilteringGetGlType(texConfig->min));
        glTexParameteri(texture->type, GL_TEXTURE_MAG_FILTER, GalFilteringGetGlType(texConfig->max));
        glTexParameteri(texture->type, GL_TEXTURE_WRAP_S, GalWrappingGetGlType(texConfig->wrap));
        glTexParameteri(texture->type, GL_TEXTURE_WRAP_T, GalWrappingGetGlType(texConfig->wrap));
        glCheckError();

        glBindTexture(texture->type, 0);
        texture->SetData(std::vector<std::byte>(texture->width * texture->height * texture->depth * GalFormatGetSize(texture->format)));
        glCheckError();

        return std::static_pointer_cast<Texture, TextureOgl>(texture);
    }


    virtual std::shared_ptr<PipelineConfig> CreatePipelineConfig() override {
        auto pipelineConfig = std::make_shared<PipelineConfigOgl>();
        return std::static_pointer_cast<PipelineConfig, PipelineConfigOgl>(pipelineConfig);
    }

    virtual std::shared_ptr<Pipeline> BuildPipeline(std::shared_ptr<PipelineConfig> pipelineConfig) override {
        auto pipeline = std::make_shared<PipelineOgl>();
        auto config = std::static_pointer_cast<PipelineConfigOgl, PipelineConfig>(pipelineConfig);

        //Shader compilation

        bool vertexFailed = false, pixelFailed = false, linkFailed = false;

        const GLchar* vertexSourcePtr = config->vertexShader->code.c_str();
        const GLchar* pixelSourcePtr = config->pixelShader->code.c_str();

        GLuint vertex, pixel;
        GLint success;
        GLuint program;
        GLchar infoLog[512];

        vertex = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertex, 1, &vertexSourcePtr, NULL);
        glCompileShader(vertex);

        glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(vertex, 512, NULL, infoLog);
            LOG(ERROR) << "Vertex shader compilation failed: " << std::endl << infoLog;
            vertexFailed = true;
        };

        pixel = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(pixel, 1, &pixelSourcePtr, NULL);
        glCompileShader(pixel);

        glGetShaderiv(pixel, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(pixel, 512, NULL, infoLog);
            LOG(ERROR) << "Fragment shader compilation failed: " << std::endl << infoLog;
            pixelFailed = true;
        };

        if (vertexFailed || pixelFailed)
            throw std::runtime_error("Shaders not compiled");

        program = glCreateProgram();
        glAttachShader(program, vertex);
        glAttachShader(program, pixel);
        glLinkProgram(program);
        glGetProgramiv(program, GL_LINK_STATUS, &success);
        if (!success) {
            glGetProgramInfoLog(program, 512, NULL, infoLog);
            LOG(ERROR) << "Shader program not linked: " << std::endl << infoLog;
            linkFailed = true;
        }

        glDeleteShader(vertex);
        glDeleteShader(pixel);

        if (linkFailed)
            throw std::runtime_error("Shader not linked");

        glUseProgram(program);

        glCheckError();

        pipeline->program = program;

        //Shader parameters

        for (auto&& [name, type] : config->shaderParameters) {
            GLint location = glGetUniformLocation(program, name.c_str());
            if (location < 0) {
                glDeleteProgram(program);
                LOG(ERROR) << "Uniform name \"" << name << "\" not found in shader";
                throw std::runtime_error("Invalid uniform");
            }
            switch (type) {
            case Type::Vec2:
                glUniform2f(location, 0.0f, 0.0f);
                break;
            case Type::Vec2u8:
            case Type::Vec2u16:
            case Type::Vec2u32:
                glUniform2ui(location, 0, 0);
                break;
            case Type::Vec4u8:
                glUniform4ui(location, 0, 0, 0, 0);
                break;
            }
            pipeline->shaderParameters.insert({ name,location });
        }

        //Vertex attributes

        size_t bufferId = 0;
        for (const auto& buffer : config->vertexBuffers) {
            size_t vertexSize = 0;
            size_t cmdOffset = pipeline->vertexBindCmds.size();
            for (const auto& [name, type] : buffer) {
                if (name.empty()) {
                    vertexSize += GalTypeGetSize(type);
                    continue;
                }

                GLint location = glGetAttribLocation(program, name.c_str());
                if (location < 0) {
                    glDeleteProgram(program);
                    LOG(ERROR) << "Vertex attribute name \"" << name << "\" not found in shader";
                    throw std::runtime_error("Invalid attribute");
                }

                size_t attribSize = GalTypeGetSize(type);

                pipeline->vertexBindCmds.push_back({
                    bufferId,
                    static_cast<size_t>(location),
                    GalTypeGetComponentGlType(type),
                    GalTypeGetComponents(type),
                    vertexSize,
                    0
                    });

                vertexSize += attribSize;
            }

            for (size_t i = cmdOffset; i < pipeline->vertexBindCmds.size(); i++)
                pipeline->vertexBindCmds[i].offset = vertexSize;

            bufferId++;
        }

        glCheckError();

        return pipeline;
    }

    virtual std::shared_ptr<FramebufferConfig> CreateFramebufferConfig() override {
        return nullptr;
    }

    virtual std::shared_ptr<Framebuffer> BuildFramebuffer(std::shared_ptr<FramebufferConfig> config) override {
        return nullptr;
    }

    virtual std::shared_ptr<Framebuffer> GetDefaultFramebuffer() override {
        if (!fbDefault)
            fbDefault = std::make_shared<FramebufferDefaultOgl>();
        return std::static_pointer_cast<Framebuffer, FramebufferDefaultOgl>(fbDefault);
    }


    virtual std::shared_ptr<ShaderParameters> GetGlobalShaderParameters() override {
        return nullptr;
    }

    virtual std::shared_ptr<Shader> LoadVertexShader(std::string_view code) override {
        auto shader = std::make_shared<ShaderOgl>();
        shader->code = code;
        shader->isVertex = true;
        return std::static_pointer_cast<Shader, ShaderOgl>(shader);
    }

    virtual std::shared_ptr<Shader> LoadPixelShader(std::string_view code) override {
        auto shader = std::make_shared<ShaderOgl>();
        shader->code = code;
        shader->isVertex = false;
        return std::static_pointer_cast<Shader, ShaderOgl>(shader);
    }
};

class FramebufferDefaultOgl : public Framebuffer {
    size_t vpX, vpY, vpW, vpH;
public:

    virtual void Clear() override {
        GLbitfield clearBits = 0;
        clearBits |= GL_COLOR_BUFFER_BIT;
        clearBits |= GL_DEPTH_BUFFER_BIT;
        clearBits |= GL_STENCIL_BUFFER_BIT;
        glClear(clearBits);
    }

    virtual void SetViewport(size_t x, size_t y, size_t w, size_t h) override {
        vpX = x;
        vpY = y;
        vpW = w;
        vpH = h;
        glViewport(x, y, w, h);
    }
};

Impl* Gal::GetImplementation()
{
    if (!impl)
        impl = std::make_unique<ImplOgl>();

    return impl.get();
}