-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.go
1374 lines (1247 loc) · 51.5 KB
/
compat.go
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 (C) 2017 Google Inc.
//
// 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.
package gles
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/google/gapid/core/app/analytics"
"github.com/google/gapid/core/data/dictionary"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/math/u32"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/api/transform"
"github.com/google/gapid/gapis/memory"
"github.com/google/gapid/gapis/replay/builder"
"github.com/google/gapid/gapis/shadertools"
)
type support int
const (
unsupported support = iota
supported
required
)
var (
// We don't include tests directly in the gles package as it adds
// significantly to the test build time.
VisibleForTestingCompat = compat
)
// If the default vertex array object (id 0) is not allowed on
// the target platform, we remap the uses to this array.
const DefaultVertexArrayId = VertexArrayId(0xFFFF0001)
type extensions map[string]struct{}
func listToExtensions(list []string) extensions {
out := extensions{}
for _, s := range list {
out[s] = struct{}{}
}
return out
}
func translateExtensions(in U32ːstringᵐ) extensions {
out := make(extensions, in.Len())
for _, s := range in.All() {
out[s] = struct{}{}
}
return out
}
func (e extensions) get(name string) support {
if _, ok := e[name]; ok {
return supported
}
return unsupported
}
func (s support) String() string {
switch s {
case unsupported:
return "unsupported"
case supported:
return "supported"
case required:
return "required"
default:
return fmt.Sprintf("support<%d>", s)
}
}
type features struct {
vertexHalfFloatOES support // support for GL_OES_vertex_half_float
eglImageExternal support // support for GL_OES_EGL_image_external
textureMultisample support // support for ARB_texture_multisample
vertexArrayObjects support // support for VBOs
supportGenerateMipmapHint bool // support for GL_GENERATE_MIPMAP_HINT
compressedTextureFormats map[GLenum]struct{}
framebufferSrgb support // support for GL_FRAMEBUFFER_SRGB
}
func getFeatures(ctx context.Context, version string, ext extensions) (features, *Version, error) {
v, err := ParseVersion(version)
if err != nil {
return features{}, v, err
}
f := features{
vertexHalfFloatOES: ext.get("GL_OES_vertex_half_float"),
eglImageExternal: ext.get("GL_OES_EGL_image_external"),
textureMultisample: ext.get("ARB_texture_multisample"),
compressedTextureFormats: getSupportedCompressedTextureFormats(v, ext),
supportGenerateMipmapHint: v.IsES,
}
// TODO: Properly check the specifications for these flags.
switch {
case v.AtLeastES(3, 0):
f.vertexArrayObjects = supported
case v.AtLeastGL(3, 0):
f.vertexArrayObjects = required
}
if v.AtLeastES(3, 2) {
f.textureMultisample = required
}
// GLES defaults FRAMEBUFFER_SRGB to enabled and only allows changing it via
// an extension, while desktop GL defaults to disabled.
if v.IsES {
f.framebufferSrgb = ext.get("GL_EXT_sRGB_write_control")
} else {
f.framebufferSrgb = required
}
return f, v, nil
}
type onCompatError func(context.Context, api.CmdID, api.Cmd, error)
func compat(ctx context.Context, device *device.Instance, onError onCompatError) (transform.Transformer, error) {
ctx = log.Enter(ctx, "compat")
if device.Configuration.Drivers.Opengl == nil {
return nil, fmt.Errorf("Could not find OpenGL device on host")
}
glDev := device.Configuration.Drivers.Opengl
target, version, err := getFeatures(ctx, glDev.Version, listToExtensions(glDev.Extensions))
if err != nil {
return nil, fmt.Errorf(
"Error '%v' when getting feature list for version: '%s', extensions: '%s'",
err, glDev.Version, glDev.Extensions)
}
contexts := map[Contextʳ]features{}
bufferCompat := newBufferCompat(int(glDev.UniformBufferAlignment))
eglContextHandle := map[Contextʳ]EGLContext{}
nextTextureID := TextureId(0xffff0000)
newTexture := func(i api.CmdID, cb CommandBuilder, out transform.Writer) TextureId {
s := out.State()
id := nextTextureID
tmp := s.AllocDataOrPanic(ctx, id)
defer tmp.Free()
out.MutateAndWrite(ctx, i.Derived(), cb.GlGenTextures(1, tmp.Ptr()).AddWrite(tmp.Data()))
nextTextureID--
return id
}
// Definitions of Vertex Arrays backed by client memory.
// We postpone the write of the command until draw call.
clientVAs := map[VertexAttributeArrayʳ]*GlVertexAttribPointer{}
textureCompat := &textureCompat{
f: target,
v: version,
origSwizzle: map[GLenum]map[Textureʳ]GLenum{
GLenum_GL_TEXTURE_SWIZZLE_R: {},
GLenum_GL_TEXTURE_SWIZZLE_G: {},
GLenum_GL_TEXTURE_SWIZZLE_B: {},
GLenum_GL_TEXTURE_SWIZZLE_A: {},
},
compatSwizzle: map[Textureʳ]map[GLenum]GLenum{},
}
// TODO: Implement full support for external images.
convertTexTarget := func(tex interface {
Target() GLenum
SetTarget(GLenum)
}) {
t := tex.Target()
if t == GLenum_GL_TEXTURE_EXTERNAL_OES && target.eglImageExternal == unsupported {
// Remap external textures to plain 2D textures - this matches GLSL compat.
// TODO: This aliases GLenum_GL_TEXTURE_EXTERNAL_OES and GLenum_GL_TEXTURE_2D
tex.SetTarget(GLenum_GL_TEXTURE_2D)
}
}
var t transform.Transformer
t = transform.Transform("compat", func(ctx context.Context, id api.CmdID, cmd api.Cmd, out transform.Writer) error {
dID := id.Derived()
s := out.State()
cb := CommandBuilder{Thread: cmd.Thread(), Arena: s.Arena}
switch cmd := cmd.(type) {
case *EglMakeCurrent: // TODO: Check for GLX, CGL, WGL...
// The compatibility layer introduces calls to GL functions that are defined for desktop GL
// and for GLES 3.0+. If the trace originated on a GLES 2.0 device, these new commands' mutate
// functions will fail the minRequiredVersion checks (which look at the version coming from
// the original context from the trace).
// TODO(dsrbecky): This might make some commands valid for replay which were invalid on trace.
scs := FindStaticContextState(s.Arena, cmd.Extras())
if !scs.IsNil() && !version.IsES && scs.Constants().MajorVersion() < 3 {
clone := cmd.clone(s.Arena)
clone.Extras().MustClone(cmd.Extras().All()...)
for _, e := range clone.Extras().All() {
if cs, ok := e.(*StaticContextState); ok {
cs.Constants().SetMajorVersion(3)
cs.Constants().SetMinorVersion(0)
}
}
cmd = clone
}
// Mutate to set the context, Version and Extensions strings.
if err := out.MutateAndWrite(ctx, id, cmd); err != nil {
return err
}
c := GetContext(s, cmd.Thread())
if c.IsNil() || !c.Other().Initialized() {
return nil
}
if _, found := contexts[c]; found {
return nil
}
source, _, err := getFeatures(ctx, c.Constants().Version(), translateExtensions(c.Constants().Extensions()))
if err != nil {
log.E(log.V{
"version": c.Constants().Version(),
"extensions": c.Constants().Extensions(),
}.Bind(ctx), "Error getting feature list: %v", err)
return err
}
contexts[c] = source
eglContextHandle[c] = cmd.Context()
if target.vertexArrayObjects == required &&
source.vertexArrayObjects != required {
// Replay device requires VAO, but capture did not enforce it.
// Satisfy the target by creating and binding a single VAO
// which we will use instead of the default VAO (id 0).
tmp := s.AllocDataOrPanic(ctx, VertexArrayId(DefaultVertexArrayId))
defer tmp.Free()
if err := out.MutateAndWrite(ctx, dID, cb.GlGenVertexArrays(1, tmp.Ptr()).AddWrite(tmp.Data())); err != nil {
return err
}
if err := out.MutateAndWrite(ctx, dID, cb.GlBindVertexArray(DefaultVertexArrayId)); err != nil {
return err
}
}
return nil
case *EglCreateImageKHR:
if target.eglImageExternal == unsupported {
// Only mutate, don't write. EGL images are treated as regular textures.
return cmd.Mutate(ctx, id, s, nil /* builder */, nil /* watcher */)
}
if cmd.Target() == EGLenum_EGL_NATIVE_BUFFER_ANDROID {
// Need to create a texture, but probably don't have a context.
// This case is handled in glEGLImageTargetTexture2DOES below.
// Only mutate, don't write.
return cmd.Mutate(ctx, id, s, nil /* builder */, nil /* watcher */)
}
return out.MutateAndWrite(ctx, id, cmd)
}
c := GetContext(s, cmd.Thread())
if c.IsNil() || !c.Other().Initialized() {
// The compatibility translations below assume that we have a valid context.
return out.MutateAndWrite(ctx, id, cmd)
}
if cmd.CmdFlags(ctx, id, s).IsDrawCall() {
t := newTweaker(out, dID, cb)
disableUnusedAttribArrays(ctx, t)
defer t.revert(ctx)
}
switch cmd := cmd.(type) {
case *GlBindBuffer:
if cmd.Buffer() == 0 {
buf, err := subGetBoundBuffer(ctx, nil, api.CmdNoID, nil, s, GetState(s), cmd.Thread(), nil, nil, cmd.Target())
if err != nil {
log.E(ctx, "Can not get bound buffer: %v ", err)
}
if buf.IsNil() {
// The buffer is already unbound. Thus this command is a no-op.
// It is helpful to remove those no-ops in case the replay driver does not support the target.
return nil
}
} else if !c.Objects().GeneratedNames().Buffers().Get(cmd.Buffer()) {
// glGenBuffers() was not used to generate the buffer. Legal in GLES 2.
tmp := s.AllocDataOrPanic(ctx, cmd.Buffer())
defer tmp.Free()
out.MutateAndWrite(ctx, dID, cb.GlGenBuffers(1, tmp.Ptr()).AddRead(tmp.Data()))
}
case *GlBindTexture:
{
cmd := cmd.clone(s.Arena)
if cmd.Texture() != 0 && !c.Objects().GeneratedNames().Textures().Get(cmd.Texture()) {
// glGenTextures() was not used to generate the texture. Legal in GLES 2.
tmp := s.AllocDataOrPanic(ctx, cmd.Texture())
out.MutateAndWrite(ctx, dID, cb.GlGenTextures(1, tmp.Ptr()).AddRead(tmp.Data()))
defer tmp.Free()
}
convertTexTarget(cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlBindVertexArray:
if cmd.Array() == VertexArrayId(0) {
if target.vertexArrayObjects == required &&
contexts[c].vertexArrayObjects != required {
// NB: This leaks state change upstream.
// In particular, when the tweaker saves and then restores vertex array binding,
// it will restore it to DefaultVertexArrayId instead of 0. It is harmless.
return out.MutateAndWrite(ctx, id, cb.GlBindVertexArray(DefaultVertexArrayId))
}
}
case *GlBindVertexArrayOES:
if cmd.Array() == VertexArrayId(0) {
if target.vertexArrayObjects == required &&
contexts[c].vertexArrayObjects != required {
return out.MutateAndWrite(ctx, id, cb.GlBindVertexArray(DefaultVertexArrayId))
}
}
// Translate to non-OES call.
return out.MutateAndWrite(ctx, id, cb.GlBindVertexArray(cmd.Array()))
case *GlGenVertexArraysOES:
// Translate to non-OES call.
c := cb.GlGenVertexArrays(cmd.Count(), cmd.Arrays())
c.Extras().Add(cmd.Extras().All()...)
return out.MutateAndWrite(ctx, id, c)
case *GlBindBufferBase:
if cmd.Buffer() == 0 {
genBuf, err := subGetBoundBuffer(ctx, nil, api.CmdNoID, nil, s, GetState(s), cmd.Thread(), nil, nil, cmd.Target())
if err != nil {
onError(ctx, id, cmd, fmt.Errorf("Can not get bound buffer: %v", err))
}
idxBuf, err := subGetBoundBufferAtIndex(ctx, nil, api.CmdNoID, nil, s, GetState(s), cmd.Thread(), nil, nil, cmd.Target(), cmd.Index())
if err != nil {
onError(ctx, id, cmd, fmt.Errorf("Can not get bound buffer: %v", err))
}
if genBuf.IsNil() && idxBuf.IsNil() {
// Both of the binding points are already clear. Thus this command is a no-op.
// It is helpful to remove those no-ops in case the replay driver does not support the target.
return nil
}
}
case *GlBufferData:
bufferCompat.modifyBufferData(ctx, out, cb, c, id, cmd.Target(), func() { out.MutateAndWrite(ctx, id, cmd) })
return nil
case *GlBufferSubData:
bufferCompat.modifyBufferData(ctx, out, cb, c, id, cmd.Target(), func() { out.MutateAndWrite(ctx, id, cmd) })
return nil
case *GlCopyBufferSubData:
bufferCompat.modifyBufferData(ctx, out, cb, c, id, cmd.WriteTarget(), func() { out.MutateAndWrite(ctx, id, cmd) })
return nil
case *GlBindBufferRange:
bufferCompat.bindBufferRange(ctx, out, cb, c, id, cmd)
return nil
case *GlDisableVertexAttribArray:
if c.Bound().VertexArray().VertexAttributeArrays().Get(cmd.Location()).Enabled() == GLboolean_GL_FALSE {
// Ignore the call if it is redundant (i.e. it is already disabled).
// Some applications iterate over all arrays and explicitly disable them.
// This is a problem if the target supports fewer arrays than the capture.
return nil
}
case *GlVertexAttrib4fv:
if c.Vertex().Attributes().Contains(cmd.Location()) {
oldAttrib := c.Vertex().Attributes().Get(cmd.Location())
oldValue := oldAttrib.Value().MustRead(ctx, cmd, s, nil /* builder */)
cmd.Mutate(ctx, id, s, nil /* builder */, nil /* watcher */)
newAttrib := c.Vertex().Attributes().Get(cmd.Location())
newValue := newAttrib.Value().MustRead(ctx, cmd, s, nil /* builder */)
if reflect.DeepEqual(oldValue, newValue) {
// Ignore the call if it is redundant.
// Some applications iterate over all arrays and explicitly initialize them.
// This is a problem if the target supports fewer arrays than the capture.
return nil
}
}
return out.MutateAndWrite(ctx, id, cmd)
case *GlGetVertexAttribIiv,
*GlGetVertexAttribIuiv,
*GlGetVertexAttribPointerv,
*GlGetVertexAttribfv,
*GlGetVertexAttribiv:
// Some applications iterate over all arrays and query their state.
// This may fail if the target supports fewer arrays than the capture.
// As these should have no side-effects, just drop them.
return nil
case *GlShaderSource:
if version.IsES { // No compat required.
return out.MutateAndWrite(ctx, id, cmd)
}
// Apply the state mutation of the unmodified glShaderSource
// command.
// This is so we can grab the source string from the Shader object.
// We will actually provide the source to driver at compile time.
return cmd.Mutate(ctx, id, s, nil /* builder */, nil /* watcher */)
case *GlCompileShader:
if version.IsES { // No compat required.
return out.MutateAndWrite(ctx, id, cmd)
}
shader := c.Objects().Shaders().Get(cmd.Shader())
src := ""
st, err := shader.Type().ShaderType()
if err != nil {
onError(ctx, id, cmd, err)
}
opts := shadertools.ConvertOptions{
ShaderType: st,
Relaxed: true, // find_issues will still report bad GLSL.
StripOptimizations: true,
TargetGLSLVersion: version.MaxGLSL().AsInt(),
}
// Trim any prefix whitespace / newlines.
// This isn't legal if it comes before the #version, but this
// will be picked up by find_issues.go anyway.
src = strings.TrimLeft(shader.Source(), "\n\r\t ")
res, err := shadertools.ConvertGlsl(src, &opts)
if err != nil {
// Could not convert the shader.
onError(ctx, id, cmd, err)
return out.MutateAndWrite(ctx, id, cmd)
}
src = res.SourceCode
tmpSrc := s.AllocDataOrPanic(ctx, src)
tmpPtrToSrc := s.AllocDataOrPanic(ctx, tmpSrc.Ptr())
defer tmpSrc.Free()
defer tmpPtrToSrc.Free()
srcCmd := cb.GlShaderSource(cmd.Shader(), 1, tmpPtrToSrc.Ptr(), memory.Nullptr).
AddRead(tmpSrc.Data()).
AddRead(tmpPtrToSrc.Data())
return out.MutateAndWrite(ctx, id, srcCmd)
// TODO: glVertexAttribIPointer
case *GlVertexAttribPointer:
if cmd.Type() == GLenum_GL_HALF_FLOAT_OES && target.vertexHalfFloatOES == unsupported {
// Convert GL_HALF_FLOAT_OES to GL_HALF_FLOAT_ARB.
cmd = cb.GlVertexAttribPointer(cmd.Location(), cmd.Size(), GLenum_GL_HALF_FLOAT_ARB, cmd.Normalized(), cmd.Stride(), memory.Pointer(cmd.Data()))
}
vaa := c.Bound().VertexArray().VertexAttributeArrays().Get(cmd.Location())
if target.vertexArrayObjects == required && c.Bound().ArrayBuffer().IsNil() {
// Client-pointers are not supported, we need to copy this data to a buffer.
// However, we can't do this now as the observation only happens at the draw call.
clientVAs[vaa] = cmd
} else {
delete(clientVAs, vaa)
return out.MutateAndWrite(ctx, id, cmd)
}
return nil
case *GlDrawBuffers:
// Currently the default framebuffer for replay is single-buffered
// and so we need to transform any usage of GL_BACK to GL_FRONT.
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
bufs := cmd.Bufs().Slice(0, uint64(cmd.N()), s.MemoryLayout).MustRead(ctx, cmd, s, nil)
for i, buf := range bufs {
if buf == GLenum_GL_BACK {
bufs[i] = GLenum_GL_FRONT
}
}
tmp := s.AllocDataOrPanic(ctx, bufs)
defer tmp.Free()
return out.MutateAndWrite(ctx, id, cb.GlDrawBuffers(cmd.N(), tmp.Ptr()).AddRead(tmp.Data()))
case *GlDrawArrays:
if target.vertexArrayObjects == required {
if clientVAsBound(c, clientVAs) {
first := uint32(cmd.FirstIndex())
count := uint32(cmd.IndicesCount())
t := newTweaker(out, dID, cb)
defer t.revert(ctx)
moveClientVBsToVAs(ctx, t, clientVAs, first, count, id, cmd, s, c, out)
}
}
compatMultiviewDraw(ctx, id, cmd, out)
return nil
case drawElements:
if target.vertexArrayObjects != required {
compatMultiviewDraw(ctx, id, cmd, out)
} else {
t := newTweaker(out, dID, cb)
defer t.revert(ctx)
compatDrawElements(ctx, t, clientVAs, id, cmd, s, out)
}
return nil
case *GlCompressedTexImage2D:
if _, supported := target.compressedTextureFormats[cmd.Internalformat()]; !supported {
if err := decompressTexImage2D(ctx, id, cmd, s, out); err == nil {
return nil
}
onError(ctx, id, cmd, fmt.Errorf("Error decompressing texture: %v", err))
}
case *GlCompressedTexSubImage2D:
if _, supported := target.compressedTextureFormats[cmd.Internalformat()]; !supported {
if err := decompressTexSubImage2D(ctx, id, cmd, s, out); err == nil {
return nil
}
onError(ctx, id, cmd, fmt.Errorf("Error decompressing texture: %v", err))
}
case *GlTexBufferEXT:
if version.AtLeastGL(3, 1) { // Strip suffix on desktop.
cmd := cb.GlTexBuffer(cmd.Target(), cmd.Internalformat(), cmd.Buffer())
return out.MutateAndWrite(ctx, id, cmd)
}
// TODO: glTexStorage functions are not guaranteed to be supported. Consider replacing with glTexImage calls.
// TODO: Handle glTextureStorage family of functions - those use direct state access, not the bound texture.
case *GlTexStorage1DEXT:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
cmd := cb.GlTexStorage1D(cmd.Target(), cmd.Levels(), cmd.Internalformat(), cmd.Width())
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage2D:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage2DEXT:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
cmd := cb.GlTexStorage2D(cmd.Target(), cmd.Levels(), cmd.Internalformat(), cmd.Width(), cmd.Height())
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage2DMultisample:
if version.IsES || version.AtLeastGL(4, 3) {
// glTexStorage2DMultisample is supported by replay device.
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
} else {
// glTexStorage2DMultisample is not supported by replay device.
// Use glTexImage2DMultisample instead.
cmd := cb.GlTexImage2DMultisample(cmd.Target(), cmd.Samples(), cmd.Internalformat(), cmd.Width(), cmd.Height(), cmd.Fixedsamplelocations())
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage3D:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage3DEXT:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
cmd := cb.GlTexStorage3D(cmd.Target(), cmd.Levels(), cmd.Internalformat(), cmd.Width(), cmd.Height(), cmd.Depth())
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage3DMultisample:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexStorage3DMultisampleOES:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
cmd := cb.GlTexStorage3DMultisample(cmd.Target(), cmd.Samples(), cmd.Internalformat(), cmd.Width(), cmd.Height(), cmd.Depth(), cmd.Fixedsamplelocations())
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexImage2D:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{
func() GLenum { return GLenum(cmd.Internalformat()) },
func(fmt GLenum) { cmd.SetInternalformat(GLint(fmt)) },
}
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, fmt, ty, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexImage3D:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{
func() GLenum { return GLenum(cmd.Internalformat()) },
func(fmt GLenum) { cmd.SetInternalformat(GLint(fmt)) },
}
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, fmt, ty, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexImage3DOES:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, fmt, ty, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
extras := cmd.extras
cmd := cb.GlTexImage3D(cmd.Target(), cmd.Level(), GLint(cmd.Internalformat()), cmd.Width(), cmd.Height(), cmd.Depth(), cmd.Border(), cmd.Fmt(), cmd.Type(), memory.Pointer(cmd.Pixels()))
cmd.extras = extras
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexSubImage2D:
{
cmd := cmd.clone(s.Arena)
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), nil, fmt, ty, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexSubImage3D:
{
cmd := cmd.clone(s.Arena)
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), nil, fmt, ty, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexSubImage3DOES:
{
cmd := cmd.clone(s.Arena)
fmt := &glenumProperty{cmd.Fmt, cmd.SetFmt}
ty := &glenumProperty{cmd.Type, cmd.SetType}
textureCompat.convertFormat(ctx, cmd.Target(), nil, fmt, ty, out, id, cmd)
if !version.IsES { // Strip suffix on desktop.
extras := cmd.extras
cmd := cb.GlTexSubImage3D(cmd.Target(), cmd.Level(), cmd.Xoffset(), cmd.Yoffset(), cmd.Zoffset(), cmd.Width(), cmd.Height(), cmd.Depth(), cmd.Fmt(), cmd.Type(), memory.Pointer(cmd.Pixels()))
cmd.extras = extras
return out.MutateAndWrite(ctx, id, cmd)
}
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlCopyTexImage2D:
{
cmd := cmd.clone(s.Arena)
internalFormat := &glenumProperty{cmd.Internalformat, cmd.SetInternalformat}
textureCompat.convertFormat(ctx, cmd.Target(), internalFormat, nil, nil, out, id, cmd)
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlTexParameterIivOES:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterIuivOES:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterIiv:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterIuiv:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterf:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Parameter(), out, id, cmd)
return err
}
case *GlTexParameterfv:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameteri:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Parameter(), out, id, cmd)
return err
}
case *GlTexParameteriv:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterIivEXT:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlTexParameterIuivEXT:
{
cmd := cmd.clone(s.Arena)
convertTexTarget(cmd)
err := out.MutateAndWrite(ctx, id, cmd)
textureCompat.postTexParameter(ctx, cmd.Target(), cmd.Pname(), out, id, cmd)
return err
}
case *GlProgramBinary:
if !canUsePrecompiledShader(c, glDev) {
for _, cmd := range buildStubProgram(ctx, cmd.Thread(), cmd.Extras(), s, cmd.Program()) {
t.Transform(ctx, id, cmd, out)
}
return nil
}
case *GlProgramBinaryOES:
if !canUsePrecompiledShader(c, glDev) {
for _, cmd := range buildStubProgram(ctx, cmd.Thread(), cmd.Extras(), s, cmd.Program()) {
t.Transform(ctx, id, cmd, out)
}
return nil
}
case *GlHint:
if cmd.Target() == GLenum_GL_GENERATE_MIPMAP_HINT && !target.supportGenerateMipmapHint {
return nil // Not supported in the core profile of OpenGL.
}
case *GlDebugMessageCallback,
*GlDebugMessageControl,
*GlDebugMessageCallbackKHR,
*GlDebugMessageControlKHR:
// Ignore - the callback function address is invalid in replay.
return nil
case *GlGetBooleani_v,
*GlGetBooleanv,
*GlGetFloatv,
*GlGetInteger64i_v,
*GlGetInteger64v,
*GlGetIntegeri_v,
*GlGetIntegerv,
*GlGetInternalformativ,
*GlGetString,
*GlGetStringi:
// The acceptable values of these get functions vary between GL versions.
// As these should have no side-effects, just drop them.
return nil
case *GlGetActiveAttrib,
*GlGetActiveUniform:
// The number of active attributes and uniforms can vary between compilers
// depending on their ability to eliminate dead code. In particular,
// dead code in pixel shader can allow code removal in the vertex shader.
// As these should have no side-effects, just drop them.
return nil
case *GlGetProgramInterfaceiv:
// Introduced as core in 4.3, but macOS caps out at 4.1.
// As this should have no side-effects, just drop them.
return nil
case *GlLabelObjectEXT,
*GlGetObjectLabelEXT,
*GlObjectLabel,
*GlObjectLabelKHR,
*GlGetObjectLabel,
*GlObjectPtrLabel,
*GlGetObjectPtrLabel,
*GlGetObjectLabelKHR:
// These methods require non-trivial remapping for replay.
// As they do not affect rendering output, just drop them.
return nil
case *GlInsertEventMarkerEXT,
*GlPushGroupMarkerEXT,
*GlPopGroupMarkerEXT,
*GlPushDebugGroup,
*GlPopDebugGroup,
*GlPushDebugGroupKHR,
*GlPopDebugGroupKHR,
*GlDebugMessageInsertKHR:
// Debug markers may not be supported on the replay device.
// As they do not affect rendering output, just drop them.
return nil
case *GlGetProgramBinary,
*GlGetProgramBinaryOES:
// Program binaries are very driver specific. This command may fail on replay
// because one of the arguments must be GL_PROGRAM_BINARY_LENGTH.
// It has no side effects, so just drop it.
return nil
case *GlEnable:
if cmd.Capability() == GLenum_GL_FRAMEBUFFER_SRGB &&
target.framebufferSrgb == required && contexts[c].framebufferSrgb != required &&
c.Bound().DrawFramebuffer().GetID() == 0 {
// Ignore enabling of FRAMEBUFFER_SRGB if the capture device did not
// support an SRGB default framebuffer, but the replay device does. This
// is only done if the current bound draw framebuffer is the default
// framebuffer. The state is mutated so that when a non-default
// framebuffer is bound later on, FRAMEBUFFER_SRGB will be enabled.
// (see GlBindFramebuffer below)
return cmd.Mutate(ctx, id, s, nil /* builder */, nil /* watcher */)
}
case *GlDisable:
// GL_QCOM_alpha_test adds back GL_ALPHA_TEST from GLES 1.0 as extension.
// It seems that applications only disable it to make sure it is off, so
// we can safely ignore it. We should not ignore glEnable for it though.
if cmd.Capability() == GLenum_GL_ALPHA_TEST_QCOM {
return nil
}
case *GlGetGraphicsResetStatusEXT:
// From extension GL_EXT_robustness
// It may not be implemented by the replay driver.
// It has no effect on rendering so just drop it.
return nil
case *GlDiscardFramebufferEXT: // GL_EXT_discard_framebuffer
// It may not be implemented by the replay driver.
// It is only a hint so we can just drop it.
// TODO: It has performance impact so we should not ignore it when profiling.
return nil
case *GlInvalidateFramebuffer, *GlInvalidateSubFramebuffer:
if !version.AtLeastES(3, 0) || !version.AtLeastGL(4, 3) {
return nil // Not supported. Only a hint. Drop it.
}
case *GlMapBufferOES:
if !version.IsES { // Remove extension suffix on desktop.
cmd := cb.GlMapBuffer(cmd.Target(), cmd.Access(), memory.Pointer(cmd.Result()))
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlMapBufferRangeEXT:
if !version.IsES { // Remove extension suffix on desktop.
cmd := cb.GlMapBufferRange(cmd.Target(), cmd.Offset(), cmd.Length(), cmd.Access(), memory.Pointer(cmd.Result()))
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlFlushMappedBufferRangeEXT:
if !version.IsES { // Remove extension suffix on desktop.
extras := cmd.extras
cmd := cb.GlFlushMappedBufferRange(cmd.Target(), cmd.Offset(), cmd.Length())
cmd.extras = extras
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlUnmapBufferOES:
if !version.IsES { // Remove extension suffix on desktop.
extras := cmd.extras
cmd := cb.GlUnmapBuffer(cmd.Target(), cmd.Result())
cmd.extras = extras
return out.MutateAndWrite(ctx, id, cmd)
}
case *GlDeleteBuffers:
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
ids, err := cmd.Buffers().Slice(0, uint64(cmd.Count()), s.MemoryLayout).Read(ctx, cmd, s, nil)
if err == nil {
deleteCompat(ctx, ids, dID, dictionary.From(c.Objects().Buffers()), s, out,
func(cnt GLsizei, buf memory.Pointer) api.Cmd { return cb.GlDeleteBuffers(cnt, buf) })
return nil
}
case *GlDeleteFramebuffers:
// If you delete a framebuffer that is currently bound then the
// binding automatically reverts back to the default framebuffer
// (0). As we do compat for glBindFramebuffer(), scan the list of
// framebuffers that are being deleted, and forward them to a fake
// call to glBindFramebuffer(XXX, 0) if we find any.
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
fbs, err := cmd.Framebuffers().Slice(0, uint64(cmd.Count()), s.MemoryLayout).Read(ctx, cmd, s, nil)
if err == nil {
for _, fb := range fbs {
if fb == c.Bound().DrawFramebuffer().ID() {
t.Transform(ctx, dID, cb.GlBindFramebuffer(GLenum_GL_DRAW_FRAMEBUFFER, 0), out)
}
if fb == c.Bound().ReadFramebuffer().ID() {
t.Transform(ctx, dID, cb.GlBindFramebuffer(GLenum_GL_READ_FRAMEBUFFER, 0), out)
}
}
deleteCompat(ctx, fbs, dID, dictionary.From(c.Objects().Framebuffers()), s, out,
func(cnt GLsizei, buf memory.Pointer) api.Cmd { return cb.GlDeleteFramebuffers(cnt, buf) })
return nil
}
case *GlDeleteProgramPipelines:
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
ids, err := cmd.Pipelines().Slice(0, uint64(cmd.N()), s.MemoryLayout).Read(ctx, cmd, s, nil)
if err == nil {
deleteCompat(ctx, ids, dID, dictionary.From(c.Objects().Pipelines()), s, out,
func(cnt GLsizei, buf memory.Pointer) api.Cmd { return cb.GlDeleteProgramPipelines(cnt, buf) })
return nil
}
case *GlDeleteQueries:
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
ids, err := cmd.Queries().Slice(0, uint64(cmd.Count()), s.MemoryLayout).Read(ctx, cmd, s, nil)
if err == nil {
deleteCompat(ctx, ids, dID, dictionary.From(c.Objects().Queries()), s, out,
func(cnt GLsizei, buf memory.Pointer) api.Cmd { return cb.GlDeleteQueries(cnt, buf) })
return nil
}
case *GlDeleteRenderbuffers:
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
ids, err := cmd.Renderbuffers().Slice(0, uint64(cmd.Count()), s.MemoryLayout).Read(ctx, cmd, s, nil)
if err == nil {
deleteCompat(ctx, ids, dID, dictionary.From(c.Objects().Renderbuffers()), s, out,
func(cnt GLsizei, buf memory.Pointer) api.Cmd { return cb.GlDeleteRenderbuffers(cnt, buf) })
return nil
}
case *GlDeleteSamplers:
cmd.Extras().Observations().ApplyReads(s.Memory.ApplicationPool())
ids, err := cmd.Samplers().Slice(0, uint64(cmd.Count()), s.MemoryLayout).Read(ctx, cmd, s, nil)