forked from apitrace/apitrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglstate_images.cpp
1895 lines (1567 loc) · 58.7 KB
/
glstate_images.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 2011-2014 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include "image.hpp"
#include "state_writer.hpp"
#include "retrace.hpp"
#include "glproc.hpp"
#include "glsize.hpp"
#include "glstate.hpp"
#include "glstate_internal.hpp"
#ifdef __linux__
#include <dlfcn.h>
#endif
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#ifdef __cplusplus
extern "C" {
#endif
OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
#ifdef __cplusplus
}
#endif
#endif /* __APPLE__ */
namespace retrace {
extern bool resolveMSAA;
}
namespace glstate {
struct ImageDesc
{
GLint width;
GLint height;
GLint depth;
GLint samples;
GLint internalFormat;
inline
ImageDesc() :
width(0),
height(0),
depth(0),
samples(0),
internalFormat(GL_NONE)
{}
inline bool
operator == (const ImageDesc &other) const {
return width == other.width &&
height == other.height &&
depth == other.depth &&
samples == other.samples &&
internalFormat == other.internalFormat;
}
inline bool
valid(void) const {
return width > 0 && height > 0 && depth > 0;
}
};
static GLenum
getTextureTarget(Context &context, GLuint texture);
/**
* OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
* probe whether a texture has a given size by crafting a dummy glTexSubImage()
* call.
*/
static bool
probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3],
GLenum internalFormat, GLenum type)
{
flushErrors();
GLint dummy = 0;
switch (target) {
case GL_TEXTURE_2D:
case GL_TEXTURE_CUBE_MAP:
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:
glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
break;
case GL_TEXTURE_3D_OES:
glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
break;
default:
assert(0);
return false;
}
GLenum error = glGetError();
if (0) {
std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
}
if (error == GL_NO_ERROR) {
return true;
}
flushErrors();
return false;
}
static bool
probeTextureFormatOES(GLenum target, GLint level,
GLenum *internalFormat, GLenum *type)
{
static const struct {
GLenum internalFormat;
GLenum type;
} info[] = {
/* internalFormat */ /* type */
{ GL_RGBA, GL_UNSIGNED_BYTE },
{ GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV },
{ GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 },
{ GL_DEPTH_COMPONENT, GL_FLOAT },
{ GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT },
{ GL_STENCIL_INDEX, GL_UNSIGNED_BYTE },
/* others? */
{ 0, 0 },
};
static const GLint size[3] = {1, 1, 1};
for (int i = 0; info[i].internalFormat; i++) {
if (probeTextureLevelSizeOES(target, level, size,
info[i].internalFormat,
info[i].type)) {
*internalFormat = info[i].internalFormat;
*type = info[i].type;
return true;
}
}
return false;
}
/**
* Bisect the texture size along an axis.
*
* It is assumed that the texture exists.
*/
static GLint
bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max,
GLenum internalFormat, GLenum type)
{
GLint size[3] = {0, 0, 0};
assert(axis < 3);
assert(max >= 0);
GLint min = 0;
while (true) {
GLint test = (min + max) / 2;
if (test == min) {
return min;
}
size[axis] = test;
if (probeTextureLevelSizeOES(target, level, size, internalFormat, type)) {
min = test;
} else {
max = test;
}
}
}
/**
* Special path to obtain texture size on OpenGL ES, that does not rely on
* glGetTexLevelParameteriv
*/
static bool
getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
{
if (target == GL_TEXTURE_1D) {
// OpenGL ES does not support 1D textures
return false;
}
GLenum internalFormat, type;
if (!probeTextureFormatOES(target, level, &internalFormat, &type)) {
return false;
}
desc.internalFormat = internalFormat;
GLint maxSize = 0;
switch (target) {
case GL_TEXTURE_2D:
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize, internalFormat, type);
desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize, internalFormat, type);
desc.depth = 1;
break;
case GL_TEXTURE_CUBE_MAP:
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:
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize, internalFormat, type);
desc.height = desc.width;
desc.depth = 1;
break;
case GL_TEXTURE_3D_OES:
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize, internalFormat, type);
desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize, internalFormat, type);
desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize, internalFormat, type);
break;
default:
return false;
}
if (0) {
std::cerr
<< enumToString(target) << " "
<< level << " "
<< desc.width << "x" << desc.height << "x" << desc.depth
<< "\n";
}
return desc.valid();
}
static inline bool
getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
{
assert(target != GL_TEXTURE_CUBE_MAP);
if (context.ES) {
return getActiveTextureLevelDescOES(context, target, level, desc);
}
if (target == GL_TEXTURE_BUFFER) {
assert(level == 0);
GLint buffer = 0;
glGetIntegerv(GL_TEXTURE_BUFFER_DATA_STORE_BINDING, &buffer);
if (!buffer) {
return false;
}
// This is the general binding point, not the texture's
GLint active_buffer = 0;
glGetIntegerv(GL_TEXTURE_BUFFER, &active_buffer);
glBindBuffer(GL_TEXTURE_BUFFER, buffer);
GLint buffer_size = 0;
glGetBufferParameteriv(GL_TEXTURE_BUFFER, GL_BUFFER_SIZE, &buffer_size);
glBindBuffer(GL_TEXTURE_BUFFER, active_buffer);
glGetIntegerv(GL_TEXTURE_BUFFER_FORMAT_ARB, &desc.internalFormat);
const InternalFormatDesc &formatDesc = getInternalFormatDesc(desc.internalFormat);
if (formatDesc.type == GL_NONE) {
std::cerr << "error: unexpected GL_TEXTURE_BUFFER internal format "
<< enumToString(desc.internalFormat)
<< " (https://git.io/JOMRy)\n";
return false;
}
unsigned bits_per_pixel = _gl_format_size(formatDesc.format, formatDesc.type);
desc.width = buffer_size * 8 / bits_per_pixel;
desc.height = 1;
desc.depth = 1;
return desc.valid();
}
glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
desc.width = 0;
glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
if (target == GL_TEXTURE_BUFFER ||
target == GL_TEXTURE_1D) {
desc.height = 1;
desc.depth = 1;
} else {
desc.height = 0;
glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
if (target != GL_TEXTURE_3D &&
target != GL_TEXTURE_2D_ARRAY &&
target != GL_TEXTURE_2D_MULTISAMPLE_ARRAY &&
target != GL_TEXTURE_CUBE_MAP_ARRAY) {
desc.depth = 1;
} else {
desc.depth = 0;
glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
}
}
glGetTexLevelParameteriv(target, level, GL_TEXTURE_SAMPLES, &desc.samples);
return desc.valid();
}
const GLenum
textureTargets[] = {
GL_TEXTURE_2D,
GL_TEXTURE_1D,
GL_TEXTURE_RECTANGLE,
GL_TEXTURE_CUBE_MAP,
GL_TEXTURE_3D,
GL_TEXTURE_2D_MULTISAMPLE,
GL_TEXTURE_2D_ARRAY,
GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
GL_TEXTURE_1D_ARRAY,
GL_TEXTURE_CUBE_MAP_ARRAY,
GL_TEXTURE_BUFFER,
};
const unsigned
numTextureTargets = ARRAYSIZE(textureTargets);
GLenum
getTextureBinding(GLenum target)
{
switch (target) {
case GL_TEXTURE_1D:
return GL_TEXTURE_BINDING_1D;
case GL_TEXTURE_1D_ARRAY:
return GL_TEXTURE_BINDING_1D_ARRAY;
case GL_TEXTURE_2D:
return GL_TEXTURE_BINDING_2D;
case GL_TEXTURE_2D_ARRAY:
return GL_TEXTURE_BINDING_2D_ARRAY;
case GL_TEXTURE_2D_MULTISAMPLE:
return GL_TEXTURE_BINDING_2D_MULTISAMPLE;
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
return GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY;
case GL_TEXTURE_RECTANGLE:
return GL_TEXTURE_BINDING_RECTANGLE;
case GL_TEXTURE_CUBE_MAP:
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:
return GL_TEXTURE_BINDING_CUBE_MAP;
case GL_TEXTURE_CUBE_MAP_ARRAY:
return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY;
case GL_TEXTURE_3D:
return GL_TEXTURE_BINDING_3D;
case GL_TEXTURE_BUFFER:
return GL_TEXTURE_BINDING_BUFFER;
default:
assert(false);
return GL_NONE;
}
}
/**
* OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
* texture to a framebuffer.
*/
static inline void
getTexImageOES(GLenum target, GLint level, GLenum format, GLenum type,
ImageDesc &desc, GLubyte *pixels)
{
memset(pixels, 0x80, desc.height * desc.width * 4);
GLenum texture_binding = getTextureBinding(target);
if (texture_binding == GL_NONE) {
return;
}
GLint texture = 0;
glGetIntegerv(texture_binding, &texture);
if (!texture) {
return;
}
GLint prev_fbo = 0;
GLuint fbo = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLenum status;
switch (target) {
case GL_TEXTURE_2D:
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:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
}
glReadPixels(0, 0, desc.width, desc.height, format, type, pixels);
break;
case GL_TEXTURE_3D_OES:
for (int i = 0; i < desc.depth; i++) {
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
glReadPixels(0, 0, desc.width, desc.height, format, type, pixels + 4 * i * desc.width * desc.height);
}
break;
}
glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
glDeleteFramebuffers(1, &fbo);
}
/**
* Simple Shader compile
*/
static inline int
compileShader(const GLchar* shaderSource, GLenum shaderType )
{
GLuint id;
GLint result;
int logLength;
char logInfo[1000];
id = glCreateShader(shaderType);
glShaderSource(id, 1, (const GLchar**)&shaderSource, NULL);
glCompileShader(id);
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &logLength);
glGetShaderInfoLog(id, logLength, NULL, &logInfo[0]);
std::cerr << std::endl << logInfo << std::endl;
return -1;
}
return id;
}
/**
* Program Creation / Linking.
*/
static inline void
createProgram(GLuint program_id, const GLchar* vShaderSource, const GLchar* pShaderSource)
{
GLint result;
int logLength;
char logInfo[1000];
GLint vshaderID = compileShader(vShaderSource, GL_VERTEX_SHADER);
GLint pshaderID = compileShader(pShaderSource, GL_FRAGMENT_SHADER);
glAttachShader(program_id, vshaderID);
glAttachShader(program_id, pshaderID);
glLinkProgram(program_id);
glGetProgramiv(program_id, GL_LINK_STATUS, &result);
if (result == GL_FALSE) {
glGetShaderiv(program_id, GL_INFO_LOG_LENGTH, &logLength);
glGetShaderInfoLog(program_id, logLength, NULL, &logInfo[0]);
std::cerr << std::endl << logInfo << std::endl;
}
glDeleteShader(vshaderID);
glDeleteShader(pshaderID);
return;
}
/**
* Obtain unresolved/resolved MSAA surface by attaching the
* texture to a framebuffer and using a multisample texel fetch inside custom shader.
*/
static inline void
getTexImageMSAA(GLenum target, GLenum format, GLenum type,
ImageDesc &desc, GLubyte *pixels, bool resolve)
{
TempId prog = TempId(GL_PROGRAM);
TempId vao = TempId(GL_VERTEX_ARRAY);
TempId vbo = TempId(GL_ARRAY_BUFFER);
/*
* Vertices for 2 tri strip
*/
const GLint channels = 4;
const GLint vertices = 4;
static const float vertArray[vertices][channels] = {
{ 1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f, 1.0f },
{ -1.0f, -1.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, 0.0f, 1.0f },
};
/*
* Create an empty texture + fbo of correct size.
*/
TempId tex = TempId(GL_TEXTURE);
TextureBinding bt = TextureBinding(GL_TEXTURE_2D, tex.ID());
TempId fbo = TempId(GL_FRAMEBUFFER);
BufferBinding fb = BufferBinding(GL_FRAMEBUFFER, fbo.ID());
BufferBinding Db = BufferBinding(GL_DRAW_BUFFER, GL_COLOR_ATTACHMENT0);
BufferBinding Rb = BufferBinding(GL_READ_BUFFER, GL_COLOR_ATTACHMENT0);
GLint savedProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, &savedProgram);
GLint savedViewport[4];
glGetIntegerv(GL_VIEWPORT, savedViewport);
GLuint viewport_height = desc.height;
if (resolve){
/*
* Create blitting program to perform resolve.
*/
const GLchar* const vShaderSource =
"in vec4 Position;\n "
"void main() {\n "
" gl_Position = Position;\n "
"}\n ";
const GLchar* const pShaderSource =
"#version 430\n; "
"uniform ivec3 texDim; // Width, Height, samples\n "
"uniform sampler2DMS Sampler;\n "
"layout(location = 0) out vec4 FragColor;\n "
"void main() {\n "
" ivec2 coord = ivec2(gl_FragCoord.xy);\n "
" coord.x = texDim.x - coord.x - 1;\n "
" vec4 outColor = { 0, 0, 0, 0 }; \n "
" for (int i=0; i<int(texDim.z); i++) { "
" outColor += texelFetch(Sampler, coord, i);\n "
" } \n "
" FragColor = outColor / float(texDim.z); \n "
"}\n ";
createProgram(prog.ID(), vShaderSource, pShaderSource);
glUseProgram(prog.ID());
/*
* Pass uniform for texture dimensions and number of samples
*/
GLint myLoc = glGetUniformLocation(prog.ID(), "texDim");
glProgramUniform3i(prog.ID(), myLoc, desc.width, desc.height, desc.samples);
} else { /* no resolve */
/*
* Read out each individual sample surface with border for MSAA surface
* Create blitting program to copy unresolved samples into tall texture.
*/
const GLchar* const vShaderSource =
"in vec4 Position;\n "
"void main() {\n "
" gl_Position = Position;\n "
"}\n ";
const GLchar* const pShaderSource =
"#version 430\n; "
"uniform ivec2 texDim; // Width, Height\n "
"uniform sampler2DMS Sampler;\n "
"layout(location = 0) out vec4 FragColor;\n "
"void main() {\n "
" ivec2 coord = ivec2(gl_FragCoord.xy);\n "
" int height = int(gl_FragCoord.y / texDim.y);\n "
" coord.y = (coord.y % texDim.y);\n "
" coord.x = texDim.x - coord.x - 1;\n "
" FragColor = texelFetch(Sampler, coord, height);\n "
"}\n ";
createProgram(prog.ID(), vShaderSource, pShaderSource);
glUseProgram(prog.ID());
/*
* Pass uniform for texture dimensions and viewport height
* Add bottom border to each sample window only (in-between samples)
*/
viewport_height = desc.height * desc.samples;
GLint myLoc = glGetUniformLocation(prog.ID(), "texDim");
glProgramUniform2i(prog.ID(), myLoc, desc.width, desc.height);
}
glViewport(0, 0, desc.width, viewport_height);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertArray), vertArray, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glTexImage2D(GL_TEXTURE_2D, 0, desc.internalFormat, desc.width, viewport_height, 0, format, type, NULL);
GLuint clearBufferBit;
switch(format)
{
case GL_DEPTH_COMPONENT:
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, bt.ID(), 0);
clearBufferBit = GL_DEPTH_BUFFER_BIT;
break;
case GL_STENCIL_INDEX:
glFramebufferTexture(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, bt.ID(), 0);
clearBufferBit = GL_STENCIL_BUFFER_BIT;
break;
default:
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bt.ID(), 0);
clearBufferBit = GL_COLOR_BUFFER_BIT;
break;
}
GLuint result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (result != GL_FRAMEBUFFER_COMPLETE)
{
std::cerr << "Framebuffer not done..." << std::endl;
goto exit_clean;
}
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear( clearBufferBit );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glReadPixels(0, 0, desc.width, viewport_height, format, type, pixels);
exit_clean:
// Put back state
glUseProgram(savedProgram);
glDisableVertexAttribArray(0);
glViewport(savedViewport[0],savedViewport[1],savedViewport[2],savedViewport[3]);
return;
}
static inline void
dumpActiveTextureLevel(StateWriter &writer, Context &context,
GLenum target, GLint level,
const std::string & label,
const char *userLabel)
{
bool multiSample = (target == GL_TEXTURE_2D_MULTISAMPLE
|| target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) ? true : false;
ImageDesc desc;
if (!getActiveTextureLevelDesc(context, target, level, desc)) {
return;
}
const InternalFormatDesc &formatDesc = getInternalFormatDesc(desc.internalFormat);
GLenum format;
GLenum type;
const PixelFormat *pixelFormat = nullptr;
if (target == GL_TEXTURE_BUFFER) {
pixelFormat = getPixelFormat(desc.internalFormat);
if (!pixelFormat) {
std::cerr << "warning: unsupported texture buffer internal format " << formatToString(desc.internalFormat) << "\n";
return;
}
format = GL_RGBA;
type = GL_FLOAT;
} else {
chooseReadBackFormat(formatDesc, format, type);
}
writer.beginMember(label);
if (context.ES && format == GL_DEPTH_COMPONENT) {
format = GL_RED;
}
GLuint channels;
image::ChannelType channelType;
getImageFormat(format, type, channels, channelType);
if (0) {
std::cerr << std::endl << std::endl;
std::cerr << label << ", " << userLabel << std::endl;
std::cerr << enumToString(desc.internalFormat) << ", " << enumToString(format) << ", " << enumToString(type) << std::endl;
std::cerr << "desc (w,h,d) (" << desc.width << ", " << desc.height << ", " << desc.depth << "), "
<< "channels: " << channels << ", channelType: " << channelType << std::endl;
}
image::Image *image;
PixelPackState pps(context);
if (target == GL_TEXTURE_BUFFER) {
assert(desc.height == 1);
assert(desc.depth == 1);
assert(pixelFormat);
assert(format == GL_RGBA);
assert(type == GL_FLOAT);
image = new image::Image(desc.width, desc.height * desc.depth, channels, true, channelType);
assert(image->bytesPerPixel == sizeof(float[4]));
GLint buffer = 0;
glGetIntegerv(GL_TEXTURE_BUFFER_DATA_STORE_BINDING, &buffer);
assert(buffer);
BufferMapping bm;
const GLvoid *map = bm.map(GL_TEXTURE_BUFFER, buffer);
if (map) {
pixelFormat->unpackSpan(static_cast<const uint8_t *>(map),
reinterpret_cast<float *>(image->pixels), image->width);
}
} else if (multiSample) {
// Perform multisample retrieval here.
if (retrace::resolveMSAA){
// For resolved MSAA...
image = new image::Image(desc.width, desc.height, channels, true, channelType);
memset(image->pixels, 0x0, image->sizeInBytes());
getTexImageMSAA(target, format, type, desc, image->pixels, true);
}
else {
// For unresolved MSAA...
GLuint samples = std::max(desc.samples, 1);
GLuint total_height = desc.height * samples;
image = new image::Image(desc.width, total_height, channels, true, channelType);
memset(image->pixels, 0x0, image->sizeInBytes());
getTexImageMSAA(target, format, type, desc, image->pixels, false);
}
}
else {
// Create a simple image single sample size.
image = new image::Image(desc.width, desc.height * desc.depth, channels, true, channelType);
if (context.ES) {
getTexImageOES(target, level, format, type, desc, image->pixels);
} else {
glGetTexImage(target, level, format, type, image->pixels);
}
}
if (userLabel) {
image->label = userLabel;
}
StateWriter::ImageDesc imageDesc;
imageDesc.depth = desc.depth;
imageDesc.format = formatToString(desc.internalFormat);
writer.writeImage(image, imageDesc);
delete image;
writer.endMember(); // label
}
static inline void
dumpActiveTexture(StateWriter &writer, Context &context, GLenum target, GLuint texture)
{
char *object_label = getObjectLabel(context, GL_TEXTURE, texture);
GLint active_texture = GL_TEXTURE0;
glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
assert(active_texture >= GL_TEXTURE0);
GLenum start_subtarget;
GLenum stop_subtarget;
if (target == GL_TEXTURE_CUBE_MAP) {
start_subtarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
stop_subtarget = start_subtarget + 6;
} else {
start_subtarget = target;
stop_subtarget = start_subtarget + 1;
}
GLboolean allowMipmaps = target != GL_TEXTURE_RECTANGLE &&
target != GL_TEXTURE_BUFFER &&
target != GL_TEXTURE_2D_MULTISAMPLE &&
target != GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
GLint level = 0;
do {
ImageDesc desc;
for (GLenum subtarget = start_subtarget; subtarget < stop_subtarget; ++subtarget) {
std::stringstream label;
label << "GL_TEXTURE" << (active_texture - GL_TEXTURE0) << ", "
<< enumToString(subtarget);
if (allowMipmaps) {
label << ", level = " << level;
}
if (!getActiveTextureLevelDesc(context, subtarget, level, desc)) {
goto finished;
}
dumpActiveTextureLevel(writer, context, subtarget, level, label.str(), object_label);
}
if (!allowMipmaps) {
// no mipmaps
break;
}
++level;
} while(true);
finished:
free(object_label);
}
static void
dumpTextureImages(StateWriter &writer, Context &context)
{
if (!context.ARB_shader_image_load_store) {
return;
}
GLint maxImageUnits = 0;
glGetIntegerv(GL_MAX_IMAGE_UNITS, &maxImageUnits);
glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
for (GLint imageUnit = 0; imageUnit < maxImageUnits; ++imageUnit) {
GLint texture = 0;
glGetIntegeri_v(GL_IMAGE_BINDING_NAME, imageUnit, &texture);
if (texture) {
GLint level = 0;
glGetIntegeri_v(GL_IMAGE_BINDING_LEVEL, imageUnit, &level);
GLint isLayered = 0;
glGetIntegeri_v(GL_IMAGE_BINDING_LAYERED, imageUnit, &isLayered);
GLint layer = 0;
glGetIntegeri_v(GL_IMAGE_BINDING_LAYER, imageUnit, &layer);
std::stringstream label;
label << "Image Unit " << imageUnit;
if (level) {
label << ", level = " << level;
}
if (isLayered) {
label << ", layer = " << layer;
}
GLenum target = getTextureTarget(context, texture);
GLint previousTexture = 0;
glGetIntegerv(getTextureBinding(target), &previousTexture);
glBindTexture(target, texture);
char *object_label = getObjectLabel(context, GL_TEXTURE, texture);
dumpActiveTextureLevel(writer, context, target, level, label.str(),
object_label);
free(object_label);
glBindTexture(target, previousTexture);
}
}
}
void
dumpTextures(StateWriter &writer, Context &context)
{
writer.beginMember("textures");
writer.beginObject();
GLint max_texture_coords = 0;
if (!context.core) {
glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
}
GLint max_combined_texture_image_units = 0;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
/*
* At least the Android software GL implementation doesn't return the
* proper value for this, but rather returns 0. The GL(ES) specification
* mandates a minimum value of 2, so use this as a fall-back value.
*/
max_combined_texture_image_units = std::max(max_combined_texture_image_units, 2);
GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
GLint active_texture = GL_TEXTURE0;
glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
for (GLint unit = 0; unit < max_units; ++unit) {
GLenum texture = GL_TEXTURE0 + unit;
glActiveTexture(texture);
for (auto target : textureTargets) {
// Whether this fixed-function stage is enabled
GLboolean enabled = GL_FALSE;
if (unit < max_texture_coords &&
(target == GL_TEXTURE_1D ||
target == GL_TEXTURE_2D ||
target == GL_TEXTURE_3D ||
target == GL_TEXTURE_CUBE_MAP ||
target == GL_TEXTURE_RECTANGLE)) {
glGetBooleanv(target, &enabled);
}
// Whether a texture object is bound
GLint texture = 0;
if (unit < max_combined_texture_image_units) {
GLenum binding = getTextureBinding(target);
glGetIntegerv(binding, &texture);
}
if (enabled || texture) {
dumpActiveTexture(writer, context, target, texture);
}
}
}
glActiveTexture(active_texture);
dumpTextureImages(writer, context);
writer.endObject();
writer.endMember(); // textures
}
bool
getDrawableBounds(GLint *width, GLint *height) {
#if defined(__linux__)
if (_getPublicProcAddress("eglGetCurrentContext")) {
EGLContext currentContext = eglGetCurrentContext();
if (currentContext != EGL_NO_CONTEXT) {
EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
if (currentSurface == EGL_NO_SURFACE) {
return false;
}
EGLDisplay currentDisplay = eglGetCurrentDisplay();
if (currentDisplay == EGL_NO_DISPLAY) {
return false;
}
if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
!eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
return false;
}
return true;
}
}
#endif