This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibGLESv3.cpp
3841 lines (3196 loc) · 85.7 KB
/
libGLESv3.cpp
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
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// libGLESv3.cpp: Implements the exported OpenGL ES 3.0 functions.
#include "main.h"
#include "Buffer.h"
#include "Fence.h"
#include "Framebuffer.h"
#include "Program.h"
#include "Query.h"
#include "Sampler.h"
#include "Texture.h"
#include "mathutil.h"
#include "TransformFeedback.h"
#include "VertexArray.h"
#include "common/debug.h"
#include <GLES3/gl3.h>
#include <GLES2/gl2ext.h>
#include <limits.h>
using namespace es2;
static bool validImageSize(GLint level, GLsizei width, GLsizei height)
{
if(level < 0 || level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS || width < 0 || height < 0)
{
return false;
}
return true;
}
static bool ValidateQueryTarget(GLenum target)
{
switch(target)
{
case GL_ANY_SAMPLES_PASSED:
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
break;
default:
return false;
}
return true;
}
bool ValidateTexParamParameters(GLenum pname, GLint param)
{
switch(pname)
{
case GL_TEXTURE_WRAP_S:
case GL_TEXTURE_WRAP_T:
case GL_TEXTURE_WRAP_R:
switch(param)
{
case GL_REPEAT:
case GL_CLAMP_TO_EDGE:
case GL_MIRRORED_REPEAT:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
case GL_TEXTURE_MIN_FILTER:
switch(param)
{
case GL_NEAREST:
case GL_LINEAR:
case GL_NEAREST_MIPMAP_NEAREST:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_LINEAR:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_MAG_FILTER:
switch(param)
{
case GL_NEAREST:
case GL_LINEAR:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_USAGE_ANGLE:
switch(param)
{
case GL_NONE:
case GL_FRAMEBUFFER_ATTACHMENT_ANGLE:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_MAX_ANISOTROPY_EXT:
// we assume the parameter passed to this validation method is truncated, not rounded
if(param < 1)
{
return error(GL_INVALID_VALUE, false);
}
return true;
case GL_TEXTURE_MIN_LOD:
case GL_TEXTURE_MAX_LOD:
// any value is permissible
return true;
case GL_TEXTURE_COMPARE_MODE:
// Acceptable mode parameters from GLES 3.0.2 spec, table 3.17
switch(param)
{
case GL_NONE:
case GL_COMPARE_REF_TO_TEXTURE:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_COMPARE_FUNC:
// Acceptable function parameters from GLES 3.0.2 spec, table 3.17
switch(param)
{
case GL_LEQUAL:
case GL_GEQUAL:
case GL_LESS:
case GL_GREATER:
case GL_EQUAL:
case GL_NOTEQUAL:
case GL_ALWAYS:
case GL_NEVER:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_SWIZZLE_R:
case GL_TEXTURE_SWIZZLE_G:
case GL_TEXTURE_SWIZZLE_B:
case GL_TEXTURE_SWIZZLE_A:
switch(param)
{
case GL_RED:
case GL_GREEN:
case GL_BLUE:
case GL_ALPHA:
case GL_ZERO:
case GL_ONE:
return true;
default:
return error(GL_INVALID_ENUM, false);
}
break;
case GL_TEXTURE_BASE_LEVEL:
case GL_TEXTURE_MAX_LEVEL:
if(param < 0)
{
return error(GL_INVALID_VALUE, false);
}
return true;
default:
break;
}
return error(GL_INVALID_ENUM, false);
}
static bool ValidateSamplerObjectParameter(GLenum pname)
{
switch(pname)
{
case GL_TEXTURE_MIN_FILTER:
case GL_TEXTURE_MAG_FILTER:
case GL_TEXTURE_WRAP_S:
case GL_TEXTURE_WRAP_T:
case GL_TEXTURE_WRAP_R:
case GL_TEXTURE_MIN_LOD:
case GL_TEXTURE_MAX_LOD:
case GL_TEXTURE_COMPARE_MODE:
case GL_TEXTURE_COMPARE_FUNC:
case GL_TEXTURE_MAX_ANISOTROPY_EXT:
return true;
default:
return false;
}
}
namespace gl
{
void GL_APIENTRY ReadBuffer(GLenum src)
{
TRACE("(GLenum src = 0x%X)", src);
auto context = es2::getContext();
if(context)
{
GLuint readFramebufferName = context->getReadFramebufferName();
switch(src)
{
case GL_BACK:
if(readFramebufferName != 0)
{
return error(GL_INVALID_OPERATION);
}
context->setFramebufferReadBuffer(src);
break;
case GL_NONE:
context->setFramebufferReadBuffer(src);
break;
case GL_COLOR_ATTACHMENT0:
case GL_COLOR_ATTACHMENT1:
case GL_COLOR_ATTACHMENT2:
case GL_COLOR_ATTACHMENT3:
case GL_COLOR_ATTACHMENT4:
case GL_COLOR_ATTACHMENT5:
case GL_COLOR_ATTACHMENT6:
case GL_COLOR_ATTACHMENT7:
case GL_COLOR_ATTACHMENT8:
case GL_COLOR_ATTACHMENT9:
case GL_COLOR_ATTACHMENT10:
case GL_COLOR_ATTACHMENT11:
case GL_COLOR_ATTACHMENT12:
case GL_COLOR_ATTACHMENT13:
case GL_COLOR_ATTACHMENT14:
case GL_COLOR_ATTACHMENT15:
case GL_COLOR_ATTACHMENT16:
case GL_COLOR_ATTACHMENT17:
case GL_COLOR_ATTACHMENT18:
case GL_COLOR_ATTACHMENT19:
case GL_COLOR_ATTACHMENT20:
case GL_COLOR_ATTACHMENT21:
case GL_COLOR_ATTACHMENT22:
case GL_COLOR_ATTACHMENT23:
case GL_COLOR_ATTACHMENT24:
case GL_COLOR_ATTACHMENT25:
case GL_COLOR_ATTACHMENT26:
case GL_COLOR_ATTACHMENT27:
case GL_COLOR_ATTACHMENT28:
case GL_COLOR_ATTACHMENT29:
case GL_COLOR_ATTACHMENT30:
case GL_COLOR_ATTACHMENT31:
{
GLuint index = (src - GL_COLOR_ATTACHMENT0);
if(index >= MAX_COLOR_ATTACHMENTS)
{
return error(GL_INVALID_OPERATION);
}
if(readFramebufferName == 0)
{
return error(GL_INVALID_OPERATION);
}
context->setFramebufferReadBuffer(src);
}
break;
default:
return error(GL_INVALID_ENUM);
}
}
}
void GL_APIENTRY DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices)
{
TRACE("(GLenum mode = 0x%X, GLuint start = %d, GLuint end = %d, "
"GLsizei count = %d, GLenum type = 0x%x, const void* indices = %p)",
mode, start, end, count, type, indices);
switch(mode)
{
case GL_POINTS:
case GL_LINES:
case GL_LINE_LOOP:
case GL_LINE_STRIP:
case GL_TRIANGLES:
case GL_TRIANGLE_FAN:
case GL_TRIANGLE_STRIP:
break;
default:
return error(GL_INVALID_ENUM);
}
switch(type)
{
case GL_UNSIGNED_BYTE:
case GL_UNSIGNED_SHORT:
case GL_UNSIGNED_INT:
break;
default:
return error(GL_INVALID_ENUM);
}
if((count < 0) || (end < start))
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
es2::TransformFeedback* transformFeedback = context->getTransformFeedback();
if(transformFeedback && transformFeedback->isActive() && !transformFeedback->isPaused())
{
return error(GL_INVALID_OPERATION);
}
context->drawElements(mode, start, end, count, type, indices);
}
}
void GL_APIENTRY TexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *data)
{
TRACE("(GLenum target = 0x%X, GLint level = %d, GLenum internalformat = 0x%X, "
"GLsizei width = %d, GLsizei height = %d, GLsizei depth = %d, GLint border = %d, "
"GLenum format = 0x%X, GLenum type = 0x%x, const GLvoid* data = %p)",
target, level, internalformat, width, height, depth, border, format, type, data);
switch(target)
{
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
break;
default:
return error(GL_INVALID_ENUM);
}
if((level < 0) || (level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS))
{
return error(GL_INVALID_VALUE);
}
const GLsizei maxSize3D = es2::IMPLEMENTATION_MAX_3D_TEXTURE_SIZE >> level;
if((width < 0) || (height < 0) || (depth < 0) || (width > maxSize3D) || (height > maxSize3D) || (depth > maxSize3D))
{
return error(GL_INVALID_VALUE);
}
if(border != 0)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
GLenum validationError = ValidateTextureFormatType(format, type, internalformat, target);
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
es2::Texture3D *texture = (target == GL_TEXTURE_3D) ? context->getTexture3D() : context->getTexture2DArray();
if(!texture)
{
return error(GL_INVALID_OPERATION);
}
validationError = context->getPixels(&data, type, context->getRequiredBufferSize(width, height, depth, format, type));
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
GLint sizedInternalFormat = gl::GetSizedInternalFormat(internalformat, type);
texture->setImage(level, width, height, depth, sizedInternalFormat, format, type, context->getUnpackParameters(), data);
}
}
void GL_APIENTRY TexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *data)
{
TRACE("(GLenum target = 0x%X, GLint level = %d, GLint xoffset = %d, GLint yoffset = %d, "
"GLint zoffset = %d, GLsizei width = %d, GLsizei height = %d, GLsizei depth = %d, "
"GLenum format = 0x%X, GLenum type = 0x%x, const GLvoid* data = %p)",
target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, data);
switch(target)
{
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
break;
default:
return error(GL_INVALID_ENUM);
}
if((level < 0) || (level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS))
{
return error(GL_INVALID_VALUE);
}
if((width < 0) || (height < 0) || (depth < 0) || (xoffset < 0) || (yoffset < 0) || (zoffset < 0))
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
es2::Texture3D *texture = (target == GL_TEXTURE_3D) ? context->getTexture3D() : context->getTexture2DArray();
GLenum validationError = ValidateSubImageParams(false, false, target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, texture);
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
validationError = context->getPixels(&data, type, context->getRequiredBufferSize(width, height, depth, format, type));
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
texture->subImage(level, xoffset, yoffset, zoffset, width, height, depth, format, type, context->getUnpackParameters(), data);
}
}
void GL_APIENTRY CopyTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height)
{
TRACE("(GLenum target = 0x%X, GLint level = %d, GLint xoffset = %d, GLint yoffset = %d, "
"GLint zoffset = %d, GLint x = %d, GLint y = %d, GLsizei width = %d, GLsizei height = %d)",
target, level, xoffset, yoffset, zoffset, x, y, width, height);
switch(target)
{
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
break;
default:
return error(GL_INVALID_ENUM);
}
if((level < 0) || (level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS))
{
return error(GL_INVALID_VALUE);
}
if((width < 0) || (height < 0) || (xoffset < 0) || (yoffset < 0) || (zoffset < 0))
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
es2::Framebuffer *framebuffer = context->getReadFramebuffer();
if(!framebuffer || (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE))
{
return error(GL_INVALID_FRAMEBUFFER_OPERATION);
}
es2::Renderbuffer *source = framebuffer->getReadColorbuffer();
if(context->getReadFramebufferName() != 0 && (!source || source->getSamples() > 1))
{
return error(GL_INVALID_OPERATION);
}
GLenum colorbufferFormat = source->getFormat();
es2::Texture3D *texture = (target == GL_TEXTURE_3D) ? context->getTexture3D() : context->getTexture2DArray();
GLenum validationError = ValidateSubImageParams(false, true, target, level, xoffset, yoffset, zoffset, width, height, 1, GL_NONE, GL_NONE, texture);
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
GLenum textureFormat = texture->getFormat(target, level);
if(!ValidateCopyFormats(textureFormat, colorbufferFormat))
{
return;
}
texture->copySubImage(target, level, xoffset, yoffset, zoffset, x, y, width, height, source);
}
}
void GL_APIENTRY CompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data)
{
TRACE("(GLenum target = 0x%X, GLint level = %d, GLenum internalformat = 0x%X, GLsizei width = %d, "
"GLsizei height = %d, GLsizei depth = %d, GLint border = %d, GLsizei imageSize = %d, const GLvoid* data = %p)",
target, level, internalformat, width, height, depth, border, imageSize, data);
switch(target)
{
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
break;
default:
return error(GL_INVALID_ENUM);
}
if((level < 0) || (level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS))
{
return error(GL_INVALID_VALUE);
}
const GLsizei maxSize3D = es2::IMPLEMENTATION_MAX_3D_TEXTURE_SIZE >> level;
if((width < 0) || (height < 0) || (depth < 0) || (width > maxSize3D) || (height > maxSize3D) || (depth > maxSize3D) || (border != 0) || (imageSize < 0))
{
return error(GL_INVALID_VALUE);
}
if(!IsCompressed(internalformat))
{
return error(GL_INVALID_ENUM);
}
if(imageSize != gl::ComputeCompressedSize(width, height, internalformat) * depth)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
es2::Texture3D *texture = (target == GL_TEXTURE_3D) ? context->getTexture3D() : context->getTexture2DArray();
if(!texture)
{
return error(GL_INVALID_OPERATION);
}
GLenum validationError = context->getPixels(&data, GL_UNSIGNED_BYTE, imageSize);
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
texture->setCompressedImage(level, internalformat, width, height, depth, imageSize, data);
}
}
void GL_APIENTRY CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void *data)
{
TRACE("(GLenum target = 0x%X, GLint level = %d, GLint xoffset = %d, GLint yoffset = %d, "
"GLint zoffset = %d, GLsizei width = %d, GLsizei height = %d, GLsizei depth = %d, "
"GLenum format = 0x%X, GLsizei imageSize = %d, const void *data = %p)",
target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data);
switch(target)
{
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
break;
default:
return error(GL_INVALID_ENUM);
}
if(level < 0 || level >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
{
return error(GL_INVALID_VALUE);
}
if(xoffset < 0 || yoffset < 0 || zoffset < 0 || !validImageSize(level, width, height) || depth < 0 || imageSize < 0)
{
return error(GL_INVALID_VALUE);
}
if(!IsCompressed(format))
{
return error(GL_INVALID_ENUM);
}
if(imageSize != gl::ComputeCompressedSize(width, height, format) * depth)
{
return error(GL_INVALID_VALUE);
}
bool is_ETC2_EAC = false;
switch(format)
{
case GL_COMPRESSED_R11_EAC:
case GL_COMPRESSED_SIGNED_R11_EAC:
case GL_COMPRESSED_RG11_EAC:
case GL_COMPRESSED_SIGNED_RG11_EAC:
case GL_COMPRESSED_RGB8_ETC2:
case GL_COMPRESSED_SRGB8_ETC2:
case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
case GL_COMPRESSED_RGBA8_ETC2_EAC:
case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
if(target != GL_TEXTURE_2D_ARRAY)
{
return error(GL_INVALID_OPERATION);
}
if(((width % 4) != 0) || ((height % 4) != 0) ||
((xoffset % 4) != 0) || ((yoffset % 4) != 0))
{
return error(GL_INVALID_OPERATION);
}
is_ETC2_EAC = true;
break;
default:
break;
}
auto context = es2::getContext();
if(context)
{
es2::Texture3D *texture = (target == GL_TEXTURE_3D) ? context->getTexture3D() : context->getTexture2DArray();
if(!texture)
{
return error(GL_INVALID_OPERATION);
}
GLenum validationError = context->getPixels(&data, GL_UNSIGNED_BYTE, imageSize);
if(validationError != GL_NO_ERROR)
{
return error(validationError);
}
if(is_ETC2_EAC)
{
if(((width + xoffset) != texture->getWidth(target, level)) ||
((height + yoffset) != texture->getHeight(target, level)) ||
((depth + zoffset) != texture->getDepth(target, level)))
{
return error(GL_INVALID_OPERATION);
}
}
texture->subImageCompressed(level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data);
}
}
void GL_APIENTRY GenQueries(GLsizei n, GLuint *ids)
{
TRACE("(GLsizei n = %d, GLuint* ids = %p)", n, ids);
if(n < 0)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
for(int i = 0; i < n; i++)
{
ids[i] = context->createQuery();
}
}
}
void GL_APIENTRY DeleteQueries(GLsizei n, const GLuint *ids)
{
TRACE("(GLsizei n = %d, GLuint* ids = %p)", n, ids);
if(n < 0)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
for(int i = 0; i < n; i++)
{
context->deleteQuery(ids[i]);
}
}
}
GLboolean GL_APIENTRY IsQuery(GLuint id)
{
TRACE("(GLuint id = %d)", id);
if(id == 0)
{
return GL_FALSE;
}
auto context = es2::getContext();
if(context)
{
es2::Query *queryObject = context->getQuery(id);
if(queryObject)
{
return GL_TRUE;
}
}
return GL_FALSE;
}
void GL_APIENTRY BeginQuery(GLenum target, GLuint id)
{
TRACE("(GLenum target = 0x%X, GLuint id = %d)", target, id);
if(!ValidateQueryTarget(target))
{
return error(GL_INVALID_ENUM);
}
if(id == 0)
{
return error(GL_INVALID_OPERATION);
}
auto context = es2::getContext();
if(context)
{
context->beginQuery(target, id);
}
}
void GL_APIENTRY EndQuery(GLenum target)
{
TRACE("(GLenum target = 0x%X)", target);
if(!ValidateQueryTarget(target))
{
return error(GL_INVALID_ENUM);
}
auto context = es2::getContext();
if(context)
{
context->endQuery(target);
}
}
void GL_APIENTRY GetQueryiv(GLenum target, GLenum pname, GLint *params)
{
TRACE("(GLenum target = 0x%X, GLenum pname = 0x%X, GLint *params = %p)",
target, pname, params);
if(!ValidateQueryTarget(target) || (pname != GL_CURRENT_QUERY))
{
return error(GL_INVALID_ENUM);
}
auto context = es2::getContext();
if(context)
{
params[0] = context->getActiveQuery(target);
}
}
void GL_APIENTRY GetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params)
{
TRACE("(GLuint id = %d, GLenum pname = 0x%X, GLint *params = %p)",
id, pname, params);
switch(pname)
{
case GL_QUERY_RESULT:
case GL_QUERY_RESULT_AVAILABLE:
break;
default:
return error(GL_INVALID_ENUM);
}
auto context = es2::getContext();
if(context)
{
es2::Query *queryObject = context->getQuery(id);
if(!queryObject)
{
return error(GL_INVALID_OPERATION);
}
if(context->getActiveQuery(queryObject->getType()) == id)
{
return error(GL_INVALID_OPERATION);
}
switch(pname)
{
case GL_QUERY_RESULT:
params[0] = queryObject->getResult();
break;
case GL_QUERY_RESULT_AVAILABLE:
params[0] = queryObject->isResultAvailable();
break;
default:
ASSERT(false);
}
}
}
GLboolean GL_APIENTRY UnmapBuffer(GLenum target)
{
TRACE("(GLenum target = 0x%X)", target);
auto context = es2::getContext();
if(context)
{
es2::Buffer *buffer = nullptr;
if(!context->getBuffer(target, &buffer))
{
return error(GL_INVALID_ENUM, GL_TRUE);
}
if(!buffer)
{
// A null buffer means that "0" is bound to the requested buffer target
return error(GL_INVALID_OPERATION, GL_TRUE);
}
if(!buffer->isMapped())
{
// Already unmapped
return error(GL_INVALID_OPERATION, GL_TRUE);
}
return buffer->unmap() ? GL_TRUE : GL_FALSE;
}
return GL_TRUE;
}
void GL_APIENTRY GetBufferPointerv(GLenum target, GLenum pname, void **params)
{
TRACE("(GLenum target = 0x%X, GLenum pname = 0x%X, GLint *params = %p)",
target, pname, params);
if(pname != GL_BUFFER_MAP_POINTER)
{
return error(GL_INVALID_ENUM);
}
auto context = es2::getContext();
if(context)
{
es2::Buffer *buffer = nullptr;
if(!context->getBuffer(target, &buffer))
{
return error(GL_INVALID_ENUM);
}
if(!buffer)
{
// A null buffer means that "0" is bound to the requested buffer target
return error(GL_INVALID_OPERATION);
}
*params = buffer->isMapped() ? (void*)(((const char*)buffer->data()) + buffer->offset()) : nullptr;
}
}
void GL_APIENTRY DrawBuffers(GLsizei n, const GLenum *bufs)
{
TRACE("(GLsizei n = %d, const GLenum *bufs = %p)", n, bufs);
if(n < 0 || n > MAX_DRAW_BUFFERS)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
GLuint drawFramebufferName = context->getDrawFramebufferName();
if((drawFramebufferName == 0) && (n != 1))
{
return error(GL_INVALID_OPERATION);
}
for(unsigned int i = 0; i < (unsigned)n; i++)
{
switch(bufs[i])
{
case GL_BACK:
if(drawFramebufferName != 0)
{
return error(GL_INVALID_OPERATION);
}
break;
case GL_NONE:
break;
case GL_COLOR_ATTACHMENT0:
case GL_COLOR_ATTACHMENT1:
case GL_COLOR_ATTACHMENT2:
case GL_COLOR_ATTACHMENT3:
case GL_COLOR_ATTACHMENT4:
case GL_COLOR_ATTACHMENT5:
case GL_COLOR_ATTACHMENT6:
case GL_COLOR_ATTACHMENT7:
case GL_COLOR_ATTACHMENT8:
case GL_COLOR_ATTACHMENT9:
case GL_COLOR_ATTACHMENT10:
case GL_COLOR_ATTACHMENT11:
case GL_COLOR_ATTACHMENT12:
case GL_COLOR_ATTACHMENT13:
case GL_COLOR_ATTACHMENT14:
case GL_COLOR_ATTACHMENT15:
case GL_COLOR_ATTACHMENT16:
case GL_COLOR_ATTACHMENT17:
case GL_COLOR_ATTACHMENT18:
case GL_COLOR_ATTACHMENT19:
case GL_COLOR_ATTACHMENT20:
case GL_COLOR_ATTACHMENT21:
case GL_COLOR_ATTACHMENT22:
case GL_COLOR_ATTACHMENT23:
case GL_COLOR_ATTACHMENT24:
case GL_COLOR_ATTACHMENT25:
case GL_COLOR_ATTACHMENT26:
case GL_COLOR_ATTACHMENT27:
case GL_COLOR_ATTACHMENT28:
case GL_COLOR_ATTACHMENT29:
case GL_COLOR_ATTACHMENT30:
case GL_COLOR_ATTACHMENT31:
{
GLuint index = (bufs[i] - GL_COLOR_ATTACHMENT0);
if(index >= MAX_COLOR_ATTACHMENTS)
{
return error(GL_INVALID_OPERATION);
}
if(index != i)
{
return error(GL_INVALID_OPERATION);
}
if(drawFramebufferName == 0)
{
return error(GL_INVALID_OPERATION);
}
}
break;
default:
return error(GL_INVALID_ENUM);
}
}
context->setFramebufferDrawBuffers(n, bufs);
}
}
void GL_APIENTRY UniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
{
TRACE("(GLint location = %d, GLsizei count = %d, GLboolean transpose = %d, const GLfloat *value = %p)", location, count, transpose, value);
if(count < 0)
{
return error(GL_INVALID_VALUE);
}
auto context = es2::getContext();
if(context)
{
es2::Program *program = context->getCurrentProgram();
if(!program)
{
return error(GL_INVALID_OPERATION);
}