summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGL/Texture.cpp
blob: 300183d31408b479603b8b3fcea61a2fc3a3607d (plain)
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*
 * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
 * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
 * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Debug.h>
#include <LibGL/GLContext.h>
#include <LibGL/Image.h>
#include <LibGPU/ImageDataLayout.h>

namespace GL {

// Helper functions to handle type casting.
static u16 max_texture_size(GPU::DeviceInfo const& device_info)
{
    return static_cast<u16>(device_info.max_texture_size);
}

static u8 log2_max_texture_size(GPU::DeviceInfo const& device_info)
{
    return static_cast<u8>(AK::log2(device_info.max_texture_size));
}

void GLContext::gl_active_texture(GLenum texture)
{
    RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);

    m_active_texture_unit_index = texture - GL_TEXTURE0;
    m_active_texture_unit = &m_texture_units.at(m_active_texture_unit_index);

    if (m_current_matrix_mode == GL_TEXTURE) {
        m_current_matrix_stack = &m_active_texture_unit->texture_matrix_stack();
        m_current_matrix = &m_current_matrix_stack->last();
    }
}

void GLContext::gl_bind_texture(GLenum target, GLuint texture)
{
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
    RETURN_WITH_ERROR_IF(target != GL_TEXTURE_1D
            && target != GL_TEXTURE_2D
            && target != GL_TEXTURE_3D
            && target != GL_TEXTURE_1D_ARRAY
            && target != GL_TEXTURE_2D_ARRAY
            && target != GL_TEXTURE_CUBE_MAP,
        GL_INVALID_ENUM);

    // FIXME: We only support GL_TEXTURE_2D for now
    if (target != GL_TEXTURE_2D) {
        dbgln("gl_bind_texture(target = {:#x}): currently only GL_TEXTURE_2D is supported", target);
        return;
    }

    RefPtr<Texture2D> texture_2d;

    if (texture == 0) {
        // Texture name 0 refers to the default texture
        texture_2d = get_default_texture<Texture2D>(target);
    } else {
        // Find this texture name in our previously allocated textures
        auto it = m_allocated_textures.find(texture);
        if (it != m_allocated_textures.end()) {
            auto texture_object = it->value;
            if (!texture_object.is_null()) {
                // Texture must have been created with the same target
                RETURN_WITH_ERROR_IF(!texture_object->is_texture_2d(), GL_INVALID_OPERATION);
                texture_2d = static_cast<Texture2D*>(texture_object.ptr());
            }
        }

        // OpenGL 1.x supports binding texture names that were not previously generated by glGenTextures.
        // If there is not an allocated texture, meaning it was not previously generated by glGenTextures,
        // we can keep texture_object null to both allocate and bind the texture with the passed in texture name.
        // FIXME: Later OpenGL versions such as 4.x enforce that texture names being bound were previously generated
        //        by glGenTextures.
        if (!texture_2d) {
            texture_2d = adopt_ref(*new Texture2D());
            m_allocated_textures.set(texture, texture_2d);
        }
    }

    m_active_texture_unit->set_texture_2d_target_texture(texture_2d);
    m_sampler_config_is_dirty = true;
}

void GLContext::gl_client_active_texture(GLenum target)
{
    RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);

    m_client_active_texture = target - GL_TEXTURE0;
}

void GLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(internalformat == GL_NONE, GL_INVALID_ENUM);
    auto pixel_type_or_error = get_validated_pixel_type(target, internalformat, GL_NONE, GL_NONE);
    RETURN_WITH_ERROR_IF(pixel_type_or_error.is_error(), pixel_type_or_error.release_error().code());

    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + max_texture_size(m_device_info)) || height > (2 + max_texture_size(m_device_info)), GL_INVALID_VALUE);
    if (!m_device_info.supports_npot_textures)
        RETURN_WITH_ERROR_IF(!is_power_of_two(width) || !is_power_of_two(height), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE);

    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());

    auto internal_pixel_format = pixel_format_for_internal_format(internalformat);
    if (level == 0) {
        texture_2d->set_device_image(m_rasterizer->create_image(internal_pixel_format, width, height, 1, log2_max_texture_size(m_device_info)));
        m_sampler_config_is_dirty = true;
    }

    auto pixel_type = pixel_type_or_error.release_value();
    if (pixel_type.format == GPU::PixelFormat::DepthComponent) {
        m_rasterizer->blit_from_depth_buffer(
            *texture_2d->device_image(),
            level,
            { static_cast<u32>(width), static_cast<u32>(height) },
            { x, y },
            { 0, 0, 0 });
    } else if (pixel_type.format == GPU::PixelFormat::StencilIndex) {
        dbgln("{}: GL_STENCIL_INDEX is not yet supported", __FUNCTION__);
    } else {
        m_rasterizer->blit_from_color_buffer(
            *texture_2d->device_image(),
            level,
            { static_cast<u32>(width), static_cast<u32>(height) },
            { x, y },
            { 0, 0, 0 });
    }
}

void GLContext::gl_copy_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_sub_image_2d, target, level, xoffset, yoffset, x, y, width, height);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + max_texture_size(m_device_info)) || height > (2 + max_texture_size(m_device_info)), GL_INVALID_VALUE);

    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());
    RETURN_WITH_ERROR_IF(texture_2d->device_image().is_null(), GL_INVALID_OPERATION);

    m_rasterizer->blit_from_color_buffer(
        *texture_2d->device_image(),
        level,
        { static_cast<u32>(width), static_cast<u32>(height) },
        { x, y },
        { xoffset, yoffset, 0 });

    // FIXME: use GPU::PixelFormat for Texture2D's internal format
    if (texture_2d->internal_format() == GL_DEPTH_COMPONENT) {
        m_rasterizer->blit_from_depth_buffer(
            *texture_2d->device_image(),
            level,
            { static_cast<u32>(width), static_cast<u32>(height) },
            { x, y },
            { 0, 0, 0 });
    } else if (texture_2d->internal_format() == GL_STENCIL_INDEX) {
        dbgln("{}: GL_STENCIL_INDEX is not yet supported", __FUNCTION__);
    } else {
        m_rasterizer->blit_from_color_buffer(
            *texture_2d->device_image(),
            level,
            { static_cast<u32>(width), static_cast<u32>(height) },
            { x, y },
            { 0, 0, 0 });
    }
}

void GLContext::gl_delete_textures(GLsizei n, GLuint const* textures)
{
    RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    for (auto i = 0; i < n; i++) {
        GLuint name = textures[i];
        if (name == 0)
            continue;

        auto texture_object = m_allocated_textures.find(name);
        if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
            continue;

        m_name_allocator.free(name);

        auto texture = texture_object->value;

        // Check all texture units
        for (auto& texture_unit : m_texture_units) {
            if (texture->is_texture_2d() && texture_unit.texture_2d_target_texture() == texture) {
                // If a texture that is currently bound is deleted, the binding reverts to 0 (the default texture)
                texture_unit.set_texture_2d_target_texture(get_default_texture<Texture2D>(GL_TEXTURE_2D));
            }
        }

        m_allocated_textures.remove(name);
    }
}

void GLContext::gl_gen_textures(GLsizei n, GLuint* textures)
{
    RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    m_name_allocator.allocate(n, textures);

    // Initialize all texture names with a nullptr
    for (auto i = 0; i < n; ++i) {
        GLuint name = textures[i];
        m_allocated_textures.set(name, nullptr);
    }
}

void GLContext::gl_get_tex_image(GLenum target, GLint level, GLenum format, GLenum type, void* pixels)
{
    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(format == GL_NONE || type == GL_NONE, GL_INVALID_ENUM);
    auto pixel_type_or_error = get_validated_pixel_type(target, GL_NONE, format, type);
    RETURN_WITH_ERROR_IF(pixel_type_or_error.is_error(), pixel_type_or_error.release_error().code());

    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());

    u32 width = texture_2d->width_at_lod(level);
    u32 height = texture_2d->height_at_lod(level);

    GPU::ImageDataLayout output_layout = {
        .pixel_type = pixel_type_or_error.release_value(),
        .packing = get_packing_specification(PackingType::Pack),
        .dimensions = {
            .width = width,
            .height = height,
            .depth = 1,
        },
        .selection = {
            .width = width,
            .height = height,
            .depth = 1,
        },
    };

    texture_2d->download_texture_data(level, output_layout, pixels);
}

void GLContext::gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params)
{
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
    // FIXME: support targets other than GL_TEXTURE_2D
    RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
    // FIXME: support other parameter names
    RETURN_WITH_ERROR_IF(pname < GL_TEXTURE_WIDTH || pname > GL_TEXTURE_HEIGHT, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    // FIXME: GL_INVALID_VALUE is generated if target is GL_TEXTURE_BUFFER and level is not zero
    // FIXME: GL_INVALID_OPERATION is generated if GL_TEXTURE_COMPRESSED_IMAGE_SIZE is queried on texture images with an uncompressed internal format or on proxy targets

    VERIFY(!m_active_texture_unit->texture_2d_target_texture().is_null());
    auto const texture_2d = m_active_texture_unit->texture_2d_target_texture();

    switch (pname) {
    case GL_TEXTURE_HEIGHT:
        *params = texture_2d->height_at_lod(level);
        break;
    case GL_TEXTURE_WIDTH:
        *params = texture_2d->width_at_lod(level);
        break;
    }
}

GLboolean GLContext::gl_is_texture(GLuint texture)
{
    RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE);

    if (texture == 0)
        return GL_FALSE;

    auto it = m_allocated_textures.find(texture);
    if (it == m_allocated_textures.end())
        return GL_FALSE;

    return it->value.is_null() ? GL_FALSE : GL_TRUE;
}

void GLContext::gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_multi_tex_coord, target, s, t, r, q);

    RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);

    m_current_vertex_tex_coord[target - GL_TEXTURE0] = { s, t, r, q };
}

void GLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);

    m_current_vertex_tex_coord[0] = { s, t, r, q };
}

void GLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(target != GL_TEXTURE_ENV && target != GL_TEXTURE_FILTER_CONTROL, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(target == GL_TEXTURE_FILTER_CONTROL && pname != GL_TEXTURE_LOD_BIAS, GL_INVALID_ENUM);

    switch (target) {
    case GL_TEXTURE_ENV:
        switch (pname) {
        case GL_ALPHA_SCALE:
            RETURN_WITH_ERROR_IF(param != 1.f && param != 2.f && param != 4.f, GL_INVALID_VALUE);
            m_active_texture_unit->set_alpha_scale(param);
            break;
        case GL_COMBINE_ALPHA: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_ADD:
            case GL_ADD_SIGNED:
            case GL_INTERPOLATE:
            case GL_MODULATE:
            case GL_REPLACE:
            case GL_SUBTRACT:
                m_active_texture_unit->set_alpha_combinator(param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_COMBINE_RGB: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_ADD:
            case GL_ADD_SIGNED:
            case GL_DOT3_RGB:
            case GL_DOT3_RGBA:
            case GL_INTERPOLATE:
            case GL_MODULATE:
            case GL_REPLACE:
            case GL_SUBTRACT:
                m_active_texture_unit->set_rgb_combinator(param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_OPERAND0_ALPHA:
        case GL_OPERAND1_ALPHA:
        case GL_OPERAND2_ALPHA: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_ONE_MINUS_SRC_ALPHA:
            case GL_SRC_ALPHA:
                m_active_texture_unit->set_alpha_operand(pname - GL_OPERAND0_ALPHA, param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_OPERAND0_RGB:
        case GL_OPERAND1_RGB:
        case GL_OPERAND2_RGB: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_ONE_MINUS_SRC_ALPHA:
            case GL_ONE_MINUS_SRC_COLOR:
            case GL_SRC_ALPHA:
            case GL_SRC_COLOR:
                m_active_texture_unit->set_rgb_operand(pname - GL_OPERAND0_RGB, param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_RGB_SCALE:
            RETURN_WITH_ERROR_IF(param != 1.f && param != 2.f && param != 4.f, GL_INVALID_VALUE);
            m_active_texture_unit->set_rgb_scale(param);
            break;
        case GL_SRC0_ALPHA:
        case GL_SRC1_ALPHA:
        case GL_SRC2_ALPHA: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_CONSTANT:
            case GL_PREVIOUS:
            case GL_PRIMARY_COLOR:
            case GL_TEXTURE:
            case GL_TEXTURE0 ... GL_TEXTURE31:
                m_active_texture_unit->set_alpha_source(pname - GL_SRC0_ALPHA, param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_SRC0_RGB:
        case GL_SRC1_RGB:
        case GL_SRC2_RGB: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_CONSTANT:
            case GL_PREVIOUS:
            case GL_PRIMARY_COLOR:
            case GL_TEXTURE:
            case GL_TEXTURE0 ... GL_TEXTURE31:
                m_active_texture_unit->set_rgb_source(pname - GL_SRC0_RGB, param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        case GL_TEXTURE_ENV_MODE: {
            auto param_enum = static_cast<GLenum>(param);
            switch (param_enum) {
            case GL_ADD:
            case GL_BLEND:
            case GL_COMBINE:
            case GL_DECAL:
            case GL_MODULATE:
            case GL_REPLACE:
                m_active_texture_unit->set_env_mode(param_enum);
                break;
            default:
                RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
            }
            break;
        }
        default:
            RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
        }
        break;
    case GL_TEXTURE_FILTER_CONTROL:
        switch (pname) {
        case GL_TEXTURE_LOD_BIAS:
            m_active_texture_unit->set_level_of_detail_bias(param);
            break;
        default:
            VERIFY_NOT_REACHED();
        }
        break;
    default:
        VERIFY_NOT_REACHED();
    }

    m_sampler_config_is_dirty = true;
}

void GLContext::gl_tex_gen(GLenum coord, GLenum pname, GLint param)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen, coord, pname, param);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
            && param != GL_OBJECT_LINEAR
            && param != GL_SPHERE_MAP
            && param != GL_NORMAL_MAP
            && param != GL_REFLECTION_MAP,
        GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);

    GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);
    texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param;
    m_texture_units_dirty = true;
}

void GLContext::gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen_floatv, coord, pname, params);
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
    RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE
            && pname != GL_OBJECT_PLANE
            && pname != GL_EYE_PLANE,
        GL_INVALID_ENUM);

    GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);

    switch (pname) {
    case GL_TEXTURE_GEN_MODE: {
        auto param = static_cast<GLenum>(params[0]);
        RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
                && param != GL_OBJECT_LINEAR
                && param != GL_SPHERE_MAP
                && param != GL_NORMAL_MAP
                && param != GL_REFLECTION_MAP,
            GL_INVALID_ENUM);
        RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
        RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);

        texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param;
        break;
    }
    case GL_OBJECT_PLANE:
        texture_coordinate_generation(m_active_texture_unit_index, capability).object_plane_coefficients = { params[0], params[1], params[2], params[3] };
        break;
    case GL_EYE_PLANE: {
        auto const& inverse_model_view = model_view_matrix().inverse();
        auto input_coefficients = FloatVector4 { params[0], params[1], params[2], params[3] };

        // Note: we are allowed to store transformed coefficients here, according to the documentation on
        //       `glGetTexGen`:
        //
        // "The returned values are those maintained in eye coordinates. They are not equal to the values
        //  specified using glTexGen, unless the modelview matrix was identity when glTexGen was called."

        texture_coordinate_generation(m_active_texture_unit_index, capability).eye_plane_coefficients = inverse_model_view * input_coefficients;
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }

    m_texture_units_dirty = true;
}

void GLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLvoid const* data)
{
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(internal_format == GL_NONE || format == GL_NONE || type == GL_NONE, GL_INVALID_ENUM);
    auto pixel_type_or_error = get_validated_pixel_type(target, internal_format, format, type);
    RETURN_WITH_ERROR_IF(pixel_type_or_error.is_error(), pixel_type_or_error.release_error().code());

    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + max_texture_size(m_device_info)) || height > (2 + max_texture_size(m_device_info)), GL_INVALID_VALUE);
    if (!m_device_info.supports_npot_textures)
        RETURN_WITH_ERROR_IF(!is_power_of_two(width) || !is_power_of_two(height), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE);

    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());

    if (level == 0) {
        // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete.
        // Trying to render while an incomplete texture is bound will result in an error.
        // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect
        // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized.
        // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time.
        // All images that were attached before the device image was created need to be stored somewhere to be used to initialize the device image once complete.
        auto internal_pixel_format = pixel_format_for_internal_format(internal_format);
        texture_2d->set_device_image(m_rasterizer->create_image(internal_pixel_format, width, height, 1, log2_max_texture_size(m_device_info)));
        m_sampler_config_is_dirty = true;
    }

    GPU::ImageDataLayout input_layout = {
        .pixel_type = pixel_type_or_error.release_value(),
        .packing = get_packing_specification(PackingType::Unpack),
        .dimensions = {
            .width = static_cast<u32>(width),
            .height = static_cast<u32>(height),
            .depth = 1,
        },
        .selection = {
            .width = static_cast<u32>(width),
            .height = static_cast<u32>(height),
            .depth = 1,
        },
    };

    texture_2d->upload_texture_data(level, internal_format, input_layout, data);
}

void GLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);

    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
    RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);

    // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
    RETURN_WITH_ERROR_IF(pname != GL_GENERATE_MIPMAP
            && pname != GL_TEXTURE_LOD_BIAS
            && pname != GL_TEXTURE_MIN_FILTER
            && pname != GL_TEXTURE_MAG_FILTER
            && pname != GL_TEXTURE_WRAP_S
            && pname != GL_TEXTURE_WRAP_T,
        GL_INVALID_ENUM);

    // We assume GL_TEXTURE_2D (see above)
    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());

    switch (pname) {
    case GL_GENERATE_MIPMAP:
        RETURN_WITH_ERROR_IF(param != GL_TRUE && param != GL_FALSE, GL_INVALID_ENUM);
        texture_2d->set_generate_mipmaps(param == GL_TRUE);
        break;
    case GL_TEXTURE_LOD_BIAS:
        texture_2d->set_level_of_detail_bias(param);
        break;
    case GL_TEXTURE_MIN_FILTER:
        RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
                                 || param == GL_LINEAR
                                 || param == GL_NEAREST_MIPMAP_NEAREST
                                 || param == GL_LINEAR_MIPMAP_NEAREST
                                 || param == GL_NEAREST_MIPMAP_LINEAR
                                 || param == GL_LINEAR_MIPMAP_LINEAR),
            GL_INVALID_ENUM);

        texture_2d->sampler().set_min_filter(param);
        break;

    case GL_TEXTURE_MAG_FILTER:
        RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
                                 || param == GL_LINEAR),
            GL_INVALID_ENUM);

        texture_2d->sampler().set_mag_filter(param);
        break;

    case GL_TEXTURE_WRAP_S:
        RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
                                 || param == GL_CLAMP_TO_BORDER
                                 || param == GL_CLAMP_TO_EDGE
                                 || param == GL_MIRRORED_REPEAT
                                 || param == GL_REPEAT),
            GL_INVALID_ENUM);

        texture_2d->sampler().set_wrap_s_mode(param);
        break;

    case GL_TEXTURE_WRAP_T:
        RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
                                 || param == GL_CLAMP_TO_BORDER
                                 || param == GL_CLAMP_TO_EDGE
                                 || param == GL_MIRRORED_REPEAT
                                 || param == GL_REPEAT),
            GL_INVALID_ENUM);

        texture_2d->sampler().set_wrap_t_mode(param);
        break;

    default:
        VERIFY_NOT_REACHED();
    }

    m_sampler_config_is_dirty = true;
}

void GLContext::gl_tex_parameterfv(GLenum target, GLenum pname, GLfloat const* params)
{
    APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameterfv, target, pname, params);

    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
    RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);

    // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
    RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_BORDER_COLOR, GL_INVALID_ENUM);

    // We assume GL_TEXTURE_2D (see above)
    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    RETURN_WITH_ERROR_IF(texture_2d.is_null(), GL_INVALID_OPERATION);

    switch (pname) {
    case GL_TEXTURE_BORDER_COLOR:
        texture_2d->sampler().set_border_color(params[0], params[1], params[2], params[3]);
        break;
    default:
        VERIFY_NOT_REACHED();
    }

    m_sampler_config_is_dirty = true;
}

void GLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* data)
{
    RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);

    // We only support symbolic constants for now
    RETURN_WITH_ERROR_IF(level < 0 || level > log2_max_texture_size(m_device_info), GL_INVALID_VALUE);
    RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + max_texture_size(m_device_info)) || height > (2 + max_texture_size(m_device_info)), GL_INVALID_VALUE);

    // A 2D texture array must have been defined by a previous glTexImage2D operation
    auto texture_2d = m_active_texture_unit->texture_2d_target_texture();
    VERIFY(!texture_2d.is_null());
    RETURN_WITH_ERROR_IF(texture_2d->device_image().is_null(), GL_INVALID_OPERATION);

    RETURN_WITH_ERROR_IF(format == GL_NONE || type == GL_NONE, GL_INVALID_ENUM);
    auto pixel_type_or_error = get_validated_pixel_type(target, texture_2d->internal_format(), format, type);
    RETURN_WITH_ERROR_IF(pixel_type_or_error.is_error(), pixel_type_or_error.release_error().code());

    RETURN_WITH_ERROR_IF(xoffset < 0 || yoffset < 0 || xoffset + width > texture_2d->width_at_lod(level) || yoffset + height > texture_2d->height_at_lod(level), GL_INVALID_VALUE);

    GPU::ImageDataLayout input_layout = {
        .pixel_type = pixel_type_or_error.release_value(),
        .packing = get_packing_specification(PackingType::Unpack),
        .dimensions = {
            .width = static_cast<u32>(width),
            .height = static_cast<u32>(height),
            .depth = 1,
        },
        .selection = {
            .width = static_cast<u32>(width),
            .height = static_cast<u32>(height),
            .depth = 1,
        },
    };

    texture_2d->replace_sub_texture_data(level, input_layout, { xoffset, yoffset, 0 }, data);
}

void GLContext::sync_device_sampler_config()
{
    if (!m_sampler_config_is_dirty)
        return;

    m_sampler_config_is_dirty = false;

    for (unsigned i = 0; i < m_texture_units.size(); ++i) {
        auto const& texture_unit = m_texture_units[i];
        if (!texture_unit.texture_2d_enabled())
            continue;

        GPU::SamplerConfig config;

        auto texture_2d = texture_unit.texture_2d_target_texture();
        VERIFY(!texture_2d.is_null());
        config.bound_image = texture_2d->device_image();
        config.level_of_detail_bias = texture_2d->level_of_detail_bias() + texture_unit.level_of_detail_bias();

        auto const& sampler = texture_2d->sampler();

        switch (sampler.min_filter()) {
        case GL_NEAREST:
            config.texture_min_filter = GPU::TextureFilter::Nearest;
            config.mipmap_filter = GPU::MipMapFilter::None;
            break;
        case GL_LINEAR:
            config.texture_min_filter = GPU::TextureFilter::Linear;
            config.mipmap_filter = GPU::MipMapFilter::None;
            break;
        case GL_NEAREST_MIPMAP_NEAREST:
            config.texture_min_filter = GPU::TextureFilter::Nearest;
            config.mipmap_filter = GPU::MipMapFilter::Nearest;
            break;
        case GL_LINEAR_MIPMAP_NEAREST:
            config.texture_min_filter = GPU::TextureFilter::Linear;
            config.mipmap_filter = GPU::MipMapFilter::Nearest;
            break;
        case GL_NEAREST_MIPMAP_LINEAR:
            config.texture_min_filter = GPU::TextureFilter::Nearest;
            config.mipmap_filter = GPU::MipMapFilter::Linear;
            break;
        case GL_LINEAR_MIPMAP_LINEAR:
            config.texture_min_filter = GPU::TextureFilter::Linear;
            config.mipmap_filter = GPU::MipMapFilter::Linear;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        switch (sampler.mag_filter()) {
        case GL_NEAREST:
            config.texture_mag_filter = GPU::TextureFilter::Nearest;
            break;
        case GL_LINEAR:
            config.texture_mag_filter = GPU::TextureFilter::Linear;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        switch (sampler.wrap_s_mode()) {
        case GL_CLAMP:
            config.texture_wrap_u = GPU::TextureWrapMode::Clamp;
            break;
        case GL_CLAMP_TO_BORDER:
            config.texture_wrap_u = GPU::TextureWrapMode::ClampToBorder;
            break;
        case GL_CLAMP_TO_EDGE:
            config.texture_wrap_u = GPU::TextureWrapMode::ClampToEdge;
            break;
        case GL_REPEAT:
            config.texture_wrap_u = GPU::TextureWrapMode::Repeat;
            break;
        case GL_MIRRORED_REPEAT:
            config.texture_wrap_u = GPU::TextureWrapMode::MirroredRepeat;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        switch (sampler.wrap_t_mode()) {
        case GL_CLAMP:
            config.texture_wrap_v = GPU::TextureWrapMode::Clamp;
            break;
        case GL_CLAMP_TO_BORDER:
            config.texture_wrap_v = GPU::TextureWrapMode::ClampToBorder;
            break;
        case GL_CLAMP_TO_EDGE:
            config.texture_wrap_v = GPU::TextureWrapMode::ClampToEdge;
            break;
        case GL_REPEAT:
            config.texture_wrap_v = GPU::TextureWrapMode::Repeat;
            break;
        case GL_MIRRORED_REPEAT:
            config.texture_wrap_v = GPU::TextureWrapMode::MirroredRepeat;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        auto& fixed_function_env = config.fixed_function_texture_environment;

        auto get_env_mode = [](GLenum mode) {
            switch (mode) {
            case GL_ADD:
                return GPU::TextureEnvMode::Add;
            case GL_BLEND:
                return GPU::TextureEnvMode::Blend;
            case GL_COMBINE:
                return GPU::TextureEnvMode::Combine;
            case GL_DECAL:
                return GPU::TextureEnvMode::Decal;
            case GL_MODULATE:
                return GPU::TextureEnvMode::Modulate;
            case GL_REPLACE:
                return GPU::TextureEnvMode::Replace;
            default:
                VERIFY_NOT_REACHED();
            }
        };
        fixed_function_env.env_mode = get_env_mode(texture_unit.env_mode());

        fixed_function_env.alpha_scale = texture_unit.alpha_scale();
        fixed_function_env.rgb_scale = texture_unit.rgb_scale();

        auto get_combinator = [](GLenum combinator) {
            switch (combinator) {
            case GL_ADD:
                return GPU::TextureCombinator::Add;
            case GL_ADD_SIGNED:
                return GPU::TextureCombinator::AddSigned;
            case GL_DOT3_RGB:
                return GPU::TextureCombinator::Dot3RGB;
            case GL_DOT3_RGBA:
                return GPU::TextureCombinator::Dot3RGBA;
            case GL_INTERPOLATE:
                return GPU::TextureCombinator::Interpolate;
            case GL_MODULATE:
                return GPU::TextureCombinator::Modulate;
            case GL_REPLACE:
                return GPU::TextureCombinator::Replace;
            case GL_SUBTRACT:
                return GPU::TextureCombinator::Subtract;
            default:
                VERIFY_NOT_REACHED();
            }
        };
        fixed_function_env.alpha_combinator = get_combinator(texture_unit.alpha_combinator());
        fixed_function_env.rgb_combinator = get_combinator(texture_unit.rgb_combinator());

        auto get_operand = [](GLenum operand) {
            switch (operand) {
            case GL_ONE_MINUS_SRC_ALPHA:
                return GPU::TextureOperand::OneMinusSourceAlpha;
            case GL_ONE_MINUS_SRC_COLOR:
                return GPU::TextureOperand::OneMinusSourceColor;
            case GL_SRC_ALPHA:
                return GPU::TextureOperand::SourceAlpha;
            case GL_SRC_COLOR:
                return GPU::TextureOperand::SourceColor;
            default:
                VERIFY_NOT_REACHED();
            }
        };
        auto get_source = [](GLenum source) {
            switch (source) {
            case GL_CONSTANT:
                return GPU::TextureSource::Constant;
            case GL_PREVIOUS:
                return GPU::TextureSource::Previous;
            case GL_PRIMARY_COLOR:
                return GPU::TextureSource::PrimaryColor;
            case GL_TEXTURE:
                return GPU::TextureSource::Texture;
            case GL_TEXTURE0 ... GL_TEXTURE31:
                return GPU::TextureSource::TextureStage;
            default:
                VERIFY_NOT_REACHED();
            }
        };
        for (size_t j = 0; j < 3; ++j) {
            fixed_function_env.alpha_operand[j] = get_operand(texture_unit.alpha_operand(j));
            fixed_function_env.alpha_source[j] = get_source(texture_unit.alpha_source(j));
            if (fixed_function_env.alpha_source[j] == GPU::TextureSource::TextureStage)
                fixed_function_env.alpha_source_texture_stage = texture_unit.alpha_source(j) - GL_TEXTURE0;

            fixed_function_env.rgb_operand[j] = get_operand(texture_unit.rgb_operand(j));
            fixed_function_env.rgb_source[j] = get_source(texture_unit.rgb_source(j));
            if (fixed_function_env.rgb_source[j] == GPU::TextureSource::TextureStage)
                fixed_function_env.rgb_source_texture_stage = texture_unit.rgb_source(j) - GL_TEXTURE0;
        }

        config.border_color = sampler.border_color();
        m_rasterizer->set_sampler_config(i, config);
    }
}

void GLContext::sync_device_texture_units()
{
    if (!m_texture_units_dirty)
        return;
    m_texture_units_dirty = false;

    for (GPU::TextureUnitIndex i = 0; i < m_device_info.num_texture_units; ++i) {
        GPU::TextureUnitConfiguration texture_unit_configuration;
        texture_unit_configuration.enabled = m_texture_units[i].texture_2d_enabled();
        texture_unit_configuration.transformation_matrix = m_texture_units[i].texture_matrix();

        // Tex coord generation
        u8 enabled_coordinates = GPU::TexCoordGenerationCoordinate::None;
        for (GLenum capability = GL_TEXTURE_GEN_S; capability <= GL_TEXTURE_GEN_Q; ++capability) {
            auto const context_coordinate_config = texture_coordinate_generation(i, capability);
            if (!context_coordinate_config.enabled)
                continue;

            GPU::TexCoordGeneration* texcoord_generation;
            switch (capability) {
            case GL_TEXTURE_GEN_S:
                enabled_coordinates |= GPU::TexCoordGenerationCoordinate::S;
                texcoord_generation = &texture_unit_configuration.tex_coord_generation[0];
                break;
            case GL_TEXTURE_GEN_T:
                enabled_coordinates |= GPU::TexCoordGenerationCoordinate::T;
                texcoord_generation = &texture_unit_configuration.tex_coord_generation[1];
                break;
            case GL_TEXTURE_GEN_R:
                enabled_coordinates |= GPU::TexCoordGenerationCoordinate::R;
                texcoord_generation = &texture_unit_configuration.tex_coord_generation[2];
                break;
            case GL_TEXTURE_GEN_Q:
                enabled_coordinates |= GPU::TexCoordGenerationCoordinate::Q;
                texcoord_generation = &texture_unit_configuration.tex_coord_generation[3];
                break;
            default:
                VERIFY_NOT_REACHED();
            }

            switch (context_coordinate_config.generation_mode) {
            case GL_OBJECT_LINEAR:
                texcoord_generation->mode = GPU::TexCoordGenerationMode::ObjectLinear;
                texcoord_generation->coefficients = context_coordinate_config.object_plane_coefficients;
                break;
            case GL_EYE_LINEAR:
                texcoord_generation->mode = GPU::TexCoordGenerationMode::EyeLinear;
                texcoord_generation->coefficients = context_coordinate_config.eye_plane_coefficients;
                break;
            case GL_SPHERE_MAP:
                texcoord_generation->mode = GPU::TexCoordGenerationMode::SphereMap;
                break;
            case GL_REFLECTION_MAP:
                texcoord_generation->mode = GPU::TexCoordGenerationMode::ReflectionMap;
                break;
            case GL_NORMAL_MAP:
                texcoord_generation->mode = GPU::TexCoordGenerationMode::NormalMap;
                break;
            default:
                VERIFY_NOT_REACHED();
            }
        }
        texture_unit_configuration.tex_coord_generation_enabled = enabled_coordinates;

        m_rasterizer->set_texture_unit_configuration(i, texture_unit_configuration);
    }
}

}