-
Notifications
You must be signed in to change notification settings - Fork 328
/
state_builder.go
1352 lines (1229 loc) · 54.1 KB
/
state_builder.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"
"time"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/data/compare"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/math/interval"
"github.com/google/gapid/core/memory/arena"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/config"
"github.com/google/gapid/gapis/memory"
"github.com/google/gapid/gapis/replay/value"
)
const (
maxDiffsLogged = 20
)
type stateBuilder struct {
oldState *api.GlobalState // The initial state which we are trying to recreate
newState *api.GlobalState // The recreated state generated by the commands
cmds []api.Cmd // The commands which recreate the initial state
cb CommandBuilder // Default command builder for thread 0
preCmd []func(api.Cmd) // Actions to be done on the next written command
tmpArena arena.Arena // The arena to use for temporary allocations
seen map[interface{}]bool
memoryIntervals interval.U64RangeList
cloneCtx api.CloneContext
}
// RebuildState returns a set of commands which, if executed on a new clean
// state, will reproduce the API's state in s.
// The segments of memory that were used to create these commands are returned
// in the rangeList.
func (API) RebuildState(ctx context.Context, oldState *api.GlobalState) ([]api.Cmd, interval.U64RangeList) {
start := time.Now()
s, hasState := oldState.APIs[ID].(*State)
if !hasState {
return nil, nil
}
newState := api.NewStateWithAllocator(memory.NewBasicAllocator(value.ValidMemoryRanges), oldState.MemoryLayout)
sb := &stateBuilder{
oldState: oldState,
newState: newState,
cb: CommandBuilder{Thread: 0, Arena: newState.Arena},
tmpArena: arena.New(),
seen: map[interface{}]bool{},
memoryIntervals: interval.U64RangeList{},
cloneCtx: api.CloneContext{},
}
defer sb.tmpArena.Dispose()
// Ensure that all pool IDs are distinct between the old state and new state.
// This helps with verification at the end by ensuring that diffing algorithm
// will not miss diffs just because the pool IDs happen to be same by chance.
sb.newState.Memory.NewAt(sb.oldState.Memory.NextPoolID())
// Create EGL contexts (possibly shared)
representative := map[ShareListʳ]EGLContext{}
for i := ContextID(0); i < s.NextContextID(); i++ {
for handle, c := range s.EGLContexts().All() {
// Don't recreate destroyed or uninitialized contexts.
if c.Other().Destroyed() || !c.Other().Initialized() {
continue
}
// TODO: We need to restore contexts in order without gaps, but this is messy.
if c.Identifier() == i {
sb.contextObject(ctx, handle, c, representative)
}
}
}
// Create EGL images (may depend on texture from any context)
for _, img := range s.EGLImages().All() {
sb.eglImage(ctx, img)
}
// Second pass over context which sets everything that may depend on EGLimage.
for handle, c := range s.EGLContexts().All() {
sb.contextObjectPostEGLImage(ctx, handle, c)
}
// Set the active context for each thread
sb.bindContexts(ctx, s)
log.I(ctx, "State reconstruction took %v", time.Since(start))
if config.CheckRebuiltStateMatches {
// Verify that the recreated state matches the original desired state.
diffs := 0
compare.Compare(s, GetState(sb.newState), func(d compare.Path) {
if l := len(d); l > 1 {
last := d[l-2] // l-1: is the pool, l-2 is the memory.Slice.
if oldSlice, ok := last.Reference.(memory.Slice); ok {
if newSlice, ok := last.Value.(memory.Slice); ok {
old := AsU8ˢ(sb.tmpArena, oldSlice, sb.oldState.MemoryLayout)
new := AsU8ˢ(sb.tmpArena, newSlice, sb.newState.MemoryLayout)
if old.ResourceID(ctx, sb.oldState) == new.ResourceID(ctx, sb.newState) {
return // The pool IDs are different, but the resource IDs match exactly.
}
oldData := old.MustRead(ctx, nil, sb.oldState, nil)
newData := new.MustRead(ctx, nil, sb.newState, nil)
if reflect.DeepEqual(oldData, newData) {
return // The pool IDs are different, but the actual data matches exactly.
}
}
}
}
if diffs++; diffs <= maxDiffsLogged {
log.W(ctx, "Initial state: %v", d)
}
})
if diffs > maxDiffsLogged {
log.W(ctx, "Initial state: found an additional %d differences", diffs-maxDiffsLogged)
}
}
return sb.cmds, sb.memoryIntervals
}
func (sb *stateBuilder) E(ctx context.Context, fmt string, args ...interface{}) {
log.E(ctx, "[GL state builder] "+fmt, args...)
}
func (sb *stateBuilder) readsData(ctx context.Context, v interface{}) memory.Pointer {
tmp := sb.newState.AllocDataOrPanic(ctx, v)
rng := tmp.Range()
interval.Merge(&sb.memoryIntervals, interval.U64Span{rng.Base, rng.Base + rng.Size}, true)
sb.preCmd = append(sb.preCmd, func(cmd api.Cmd) {
cmd.Extras().GetOrAppendObservations().AddRead(tmp.Data())
tmp.Free()
})
return tmp.Ptr()
}
func (sb *stateBuilder) readsSlice(ctx context.Context, v U8ˢ) memory.Pointer {
tmp := sb.newState.AllocOrPanic(ctx, v.Size())
rng := tmp.Range()
interval.Merge(&sb.memoryIntervals, interval.U64Span{rng.Base, rng.Base + rng.Size}, true)
id := v.ResourceID(ctx, sb.oldState)
sb.preCmd = append(sb.preCmd, func(cmd api.Cmd) {
cmd.Extras().GetOrAppendObservations().AddRead(tmp.Range(), id)
tmp.Free()
})
return tmp.Ptr()
}
func (sb *stateBuilder) writesData(ctx context.Context, v interface{}) memory.Pointer {
tmp := sb.newState.AllocDataOrPanic(ctx, v)
rng := tmp.Range()
interval.Merge(&sb.memoryIntervals, interval.U64Span{rng.Base, rng.Base + rng.Size}, true)
sb.preCmd = append(sb.preCmd, func(cmd api.Cmd) {
cmd.Extras().GetOrAppendObservations().AddWrite(tmp.Data())
tmp.Free()
})
return tmp.Ptr()
}
func (sb *stateBuilder) write(ctx context.Context, cmd api.Cmd) {
for _, fn := range sb.preCmd {
fn(cmd)
}
sb.preCmd = sb.preCmd[:0]
if config.CheckRebuiltStateMatches {
if err := cmd.Mutate(ctx, api.CmdNoID, sb.newState, nil, nil); err != nil {
log.W(ctx, "Initial cmd %v: %v - %v", len(sb.cmds), cmd, err)
}
}
sb.cmds = append(sb.cmds, cmd)
}
func (sb *stateBuilder) enable(ctx context.Context, cap GLenum, val GLboolean) {
write, cb := sb.write, sb.cb
if val == GLboolean_GL_TRUE {
write(ctx, cb.GlEnable(cap))
} else {
write(ctx, cb.GlDisable(cap))
}
}
func (sb *stateBuilder) enablei(ctx context.Context, cap GLenum, idx GLuint, val GLboolean) {
write, cb := sb.write, sb.cb
if val == GLboolean_GL_TRUE {
write(ctx, cb.GlEnablei(cap, idx))
} else {
write(ctx, cb.GlDisablei(cap, idx))
}
}
// once is used to ensure we create shared objects only once
func (sb *stateBuilder) once(key interface{}) (res bool) {
res = !sb.seen[key]
sb.seen[key] = true
return
}
func (sb *stateBuilder) contextExtras(ctx context.Context, c Contextʳ) []api.CmdExtra {
r := []api.CmdExtra{}
if se := c.Other().StaticStateExtra(); !se.IsNil() {
r = append(r, se.Get().Clone(sb.cb.Arena, sb.cloneCtx))
}
if de := c.Other().DynamicStateExtra(); !de.IsNil() {
r = append(r, de.Get().Clone(sb.cb.Arena, sb.cloneCtx))
}
return r
}
func (sb *stateBuilder) contextObject(ctx context.Context, handle EGLContext, c Contextʳ, representative map[ShareListʳ]EGLContext) {
ctx = status.Start(ctx, "contextObject %v", c.Identifier())
defer status.Finish(ctx)
write, cb := sb.write, sb.cb
// Check if we have already created any other context within this share-list.
sharedCtx := representative[c.Other().ShareList()] // Returns nullptr if we are the first.
representative[c.Other().ShareList()] = handle // Further contexts will reference us.
// TODO: Record the arguments in state.
write(ctx, cb.EglCreateContext(memory.Nullptr, memory.Nullptr, sharedCtx, memory.Nullptr, handle))
write(ctx, api.WithExtras(cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, handle, EGLBoolean(1)),
sb.contextExtras(ctx, c)...))
write(ctx, cb.GlPixelStorei(GLenum_GL_UNPACK_ALIGNMENT, 1))
if names := c.Objects().GeneratedNames().Buffers(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d buffers", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenBuffers(1, sb.writesData(ctx, id)))
if o := c.Objects().Buffers().Get(id); !o.IsNil() {
sb.bufferObject(ctx, o)
}
}
}
})
}
if names := c.Objects().GeneratedNames().Renderbuffers(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d renderbuffers", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenRenderbuffers(1, sb.writesData(ctx, id)))
if o := c.Objects().Renderbuffers().Get(id); !o.IsNil() {
sb.renderbufferObject(ctx, o)
}
}
}
})
}
for _, defaultTexture := range []Textureʳ{
c.Objects().Default().Texture2d(),
c.Objects().Default().Texture2dArray(),
c.Objects().Default().Texture2dMultisample(),
c.Objects().Default().Texture2dMultisampleArray(),
c.Objects().Default().Texture3d(),
c.Objects().Default().TextureBuffer(),
c.Objects().Default().TextureCubeMap(),
c.Objects().Default().TextureCubeMapArray(),
c.Objects().Default().TextureExternalOes(),
} {
sb.textureObject(ctx, defaultTexture)
}
if names := c.Objects().GeneratedNames().Textures(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d textures", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenTextures(1, sb.writesData(ctx, id)))
if o := c.Objects().Textures().Get(id); !o.IsNil() {
sb.textureObject(ctx, o)
}
}
}
})
}
if objs := c.Objects().ImageUnits(); sb.once(objs) && objs.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d image units", objs.Len()), func(ctx context.Context) {
for _, id := range objs.Keys() {
if o := c.Objects().ImageUnits().Get(id); !o.IsNil() {
sb.imageUnit(ctx, o)
}
}
})
}
var programs []ProgramId
if objs := c.Objects().Programs(); sb.once(objs) && objs.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d programs", objs.Len()), func(ctx context.Context) {
for _, id := range objs.Keys() {
if o := c.Objects().Programs().Get(id); !o.IsNil() {
sb.programObject(ctx, o)
programs = append(programs, id)
}
}
})
}
if objs := c.Objects().Shaders(); sb.once(objs) && objs.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d shaders", objs.Len()), func(ctx context.Context) {
for _, id := range objs.Keys() {
if o := c.Objects().Shaders().Get(id); !o.IsNil() {
sb.shaderObject(ctx, o)
}
}
})
}
if len(programs) > 0 {
status.Do(ctx, fmt.Sprintf("attaching shaders to %d programs", len(programs)), func(ctx context.Context) {
for _, id := range programs {
sb.attachShaders(ctx, c.Objects().Programs().Get(id))
}
})
}
if names := c.Objects().GeneratedNames().Pipelines(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d pipelines", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenProgramPipelines(1, sb.writesData(ctx, id)))
if o := c.Objects().Pipelines().Get(id); !o.IsNil() {
sb.pipelineObject(ctx, o)
}
}
}
})
}
if names := c.Objects().GeneratedNames().Samplers(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d samplers", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenSamplers(1, sb.writesData(ctx, id)))
if o := c.Objects().Samplers().Get(id); !o.IsNil() {
sb.samplerObject(ctx, o)
}
}
}
})
}
if names := c.Objects().GeneratedNames().Queries(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d queries", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenQueries(1, sb.writesData(ctx, id)))
if o := c.Objects().Queries().Get(id); !o.IsNil() {
sb.queryObject(ctx, o)
}
}
}
})
}
if objs := c.Objects().SyncObjects(); sb.once(objs) && objs.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d sync objects", objs.Len()), func(ctx context.Context) {
for _, id := range objs.Keys() {
if o := c.Objects().SyncObjects().Get(id); !o.IsNil() {
sb.syncObject(ctx, o)
}
}
})
}
sb.transformFeedbackObject(ctx, c.Objects().Default().TransformFeedback())
if names := c.Objects().GeneratedNames().TransformFeedbacks(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d tf feedback objects", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenTransformFeedbacks(1, sb.writesData(ctx, id)))
if o := c.Objects().TransformFeedbacks().Get(id); !o.IsNil() {
sb.transformFeedbackObject(ctx, o)
}
}
}
})
}
sb.vertexArrayObject(ctx, c.Objects().Default().VertexArray())
if names := c.Objects().GeneratedNames().VertexArrays(); sb.once(names) && names.Len() > 0 {
status.Do(ctx, fmt.Sprintf("rebuilding %d VAOs", names.Len()), func(ctx context.Context) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenVertexArrays(1, sb.writesData(ctx, id)))
if o := c.Objects().VertexArrays().Get(id); !o.IsNil() {
sb.vertexArrayObject(ctx, o)
}
}
}
})
}
sb.vertexState(ctx, c.Vertex())
sb.reasterizationState(ctx, c.Rasterization())
sb.pixelState(ctx, c.Pixel())
sb.otherState(ctx, c.Other())
sb.debugLabels(ctx, c)
sb.bindings(ctx, c)
sb.deleteMarkedObjects(ctx, c)
write(ctx, cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, memory.Nullptr, EGLBoolean(1)))
}
// eglImage creates an EGLImage object.
// It may reference a texture object in different context,
// so it must be called after all contexts have been created.
func (sb *stateBuilder) eglImage(ctx context.Context, img EGLImageʳ) {
write, cb := sb.write, sb.cb
// TODO: This might not work if the target texture object has been deleted.
attribs := img.AttribList().MustRead(ctx, nil, sb.oldState, nil)
cmd := cb.EglCreateImageKHR(img.Display(), img.Context(), img.Target(), img.Buffer(), sb.readsData(ctx, attribs), img.ID())
if extra := img.Extra(); !extra.IsNil() {
cmd.Extras().Add(extra.Get().Clone(cb.Arena, sb.cloneCtx))
}
write(ctx, cmd)
}
func (sb *stateBuilder) contextObjectPostEGLImage(ctx context.Context, handle EGLContext, c Contextʳ) {
write, cb := sb.write, sb.cb
if c.Other().Initialized() {
write(ctx, api.WithExtras(cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, handle, EGLBoolean(1)),
sb.contextExtras(ctx, c)...))
for _, t := range c.Objects().Textures().All() {
target := t.Kind()
if i := t.EGLImage(); !i.IsNil() {
write(ctx, cb.GlBindTexture(target, t.GetID()))
if target != GLenum_GL_TEXTURE_2D && target != GLenum_GL_TEXTURE_EXTERNAL_OES {
panic(fmt.Errorf("Unknown EGLImage target: %v", target))
}
img := t.Image()
extra := &EGLImageData{
ID: img.Data().ResourceID(ctx, sb.oldState),
Size: img.Data().Size(),
Width: img.Width(),
Height: img.Height(),
Format: img.DataFormat(),
Type: img.DataType(),
}
write(ctx, api.WithExtras(cb.GlEGLImageTargetTexture2DOES(target, i.ID()), extra))
}
}
// Create framebuffers
sb.framebufferObject(ctx, c, c.Objects().Default().Framebuffer())
if names := c.Objects().GeneratedNames().Framebuffers(); sb.once(names) {
for _, id := range names.Keys() {
if id != 0 && names.Get(id) {
write(ctx, cb.GlGenFramebuffers(1, sb.writesData(ctx, id)))
if o := c.Objects().Framebuffers().Get(id); !o.IsNil() {
sb.framebufferObject(ctx, c, o)
}
}
}
}
// Framebuffer bindings
write(ctx, cb.GlBindFramebuffer(GLenum_GL_READ_FRAMEBUFFER, c.Bound().ReadFramebuffer().GetID()))
write(ctx, cb.GlBindFramebuffer(GLenum_GL_DRAW_FRAMEBUFFER, c.Bound().DrawFramebuffer().GetID()))
write(ctx, cb.GlBindRenderbuffer(GLenum_GL_RENDERBUFFER, c.Bound().Renderbuffer().GetID()))
// Texture unit bindings
for unit, tu := range c.Objects().TextureUnits().All() {
bind := func(target GLenum, tex Textureʳ) {
if t := tex.GetID(); t != 0 || unit == 0 {
write(ctx, cb.GlActiveTexture(GLenum_GL_TEXTURE0+GLenum(unit)))
write(ctx, cb.GlBindTexture(target, t))
}
}
bind(GLenum_GL_TEXTURE_2D, tu.Binding2d())
bind(GLenum_GL_TEXTURE_2D_ARRAY, tu.Binding2dArray())
bind(GLenum_GL_TEXTURE_2D_MULTISAMPLE, tu.Binding2dMultisample())
bind(GLenum_GL_TEXTURE_2D_MULTISAMPLE_ARRAY, tu.Binding2dMultisampleArray())
bind(GLenum_GL_TEXTURE_3D, tu.Binding3d())
bind(GLenum_GL_TEXTURE_BUFFER, tu.BindingBuffer())
bind(GLenum_GL_TEXTURE_CUBE_MAP, tu.BindingCubeMap())
bind(GLenum_GL_TEXTURE_CUBE_MAP_ARRAY, tu.BindingCubeMapArray())
bind(GLenum_GL_TEXTURE_EXTERNAL_OES, tu.BindingExternalOes())
}
write(ctx, cb.GlActiveTexture(GLenum_GL_TEXTURE0+GLenum(c.Bound().TextureUnit().ID())))
write(ctx, cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, memory.Nullptr, EGLBoolean(1)))
}
}
// We have generally setup the whole state on single-thread.
// As a last step, call eglMakeCurrent for on all user-threads.
func (sb *stateBuilder) bindContexts(ctx context.Context, s *State) {
write, cb := sb.write, sb.cb
for handle, c := range s.EGLContexts().All() {
if thread := c.Other().BoundOnThread(); thread != 0 {
cb := CommandBuilder{Thread: thread, Arena: sb.cb.Arena}
write(ctx, api.WithExtras(cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, handle, EGLBoolean(1)),
sb.contextExtras(ctx, c)...))
}
}
write(ctx, cb.EglMakeCurrent(memory.Nullptr, memory.Nullptr, memory.Nullptr, memory.Nullptr, EGLBoolean(1)))
}
func (sb *stateBuilder) bufferObject(ctx context.Context, b Bufferʳ) {
write, cb, id := sb.write, sb.cb, b.GetID()
target := GLenum_GL_ARRAY_BUFFER // Any binding point will do.
write(ctx, cb.GlBindBuffer(target, id))
write(ctx, cb.GlBufferData(target, GLsizeiptr(b.Data().Size()), sb.readsSlice(ctx, b.Data()), b.Usage()))
if b.Mapped() == GLboolean_GL_TRUE {
write(ctx, cb.GlMapBufferRange(target, b.MapOffset(), b.MapLength(), b.AccessFlags(), b.MapPointer())) // GLES30
}
}
// Just to be on the safe side, this should be done before textures and framebuffers,
// since we generate some of them as temporary objects (to avoid ID collisions).
func (sb *stateBuilder) renderbufferObject(ctx context.Context, rb Renderbufferʳ) {
write, cb, id := sb.write, sb.cb, rb.GetID()
write(ctx, cb.GlBindRenderbuffer(GLenum_GL_RENDERBUFFER, id))
if img := rb.Image(); !img.IsNil() {
fmt, w, h := img.getCorrectInternalFormat(), img.Width(), img.Height()
write(ctx, cb.GlRenderbufferStorageMultisample(GLenum_GL_RENDERBUFFER, img.Samples(), fmt, w, h)) // GLES30
// Fill the renderbuffer with data using a framebuffer blit
if img.Data().Size() != 0 {
// Create temporary objects
tex := TextureId(0x10000001)
write(ctx, cb.GlGenTextures(1, sb.writesData(ctx, tex)))
write(ctx, cb.GlBindTexture(GLenum_GL_TEXTURE_2D, tex))
src := FramebufferId(0x10000002)
write(ctx, cb.GlGenFramebuffers(1, sb.writesData(ctx, src)))
write(ctx, cb.GlBindFramebuffer(GLenum_GL_READ_FRAMEBUFFER, src))
dst := FramebufferId(0x10000003)
write(ctx, cb.GlGenFramebuffers(1, sb.writesData(ctx, dst)))
write(ctx, cb.GlBindFramebuffer(GLenum_GL_DRAW_FRAMEBUFFER, dst))
// Upload data and blit it to our renderbuffer
att := GLenum_GL_COLOR_ATTACHMENT0 // TODO: Consider depth
dataFormat, dataType := img.getUnsizedFormatAndType()
write(ctx, cb.GlTexImage2D(GLenum_GL_TEXTURE_2D, 0, GLint(fmt), w, h, 0, dataFormat, dataType, sb.readsSlice(ctx, img.Data())))
write(ctx, cb.GlFramebufferTexture2D(GLenum_GL_READ_FRAMEBUFFER, att, GLenum_GL_TEXTURE_2D, tex, 0))
write(ctx, cb.GlFramebufferRenderbuffer(GLenum_GL_DRAW_FRAMEBUFFER, att, GLenum_GL_RENDERBUFFER, id))
write(ctx, cb.GlScissor(0, 0, w, h))
mask := GLbitfield_GL_COLOR_BUFFER_BIT | GLbitfield_GL_DEPTH_BUFFER_BIT | GLbitfield_GL_STENCIL_BUFFER_BIT
write(ctx, cb.GlBlitFramebuffer(0, 0, GLint(w), GLint(h), 0, 0, GLint(w), GLint(h), mask, GLenum_GL_NEAREST))
// Delete temporary objects
write(ctx, cb.GlBindTexture(GLenum_GL_TEXTURE_2D, 0))
write(ctx, cb.GlDeleteTextures(1, sb.readsData(ctx, tex)))
write(ctx, cb.GlBindFramebuffer(GLenum_GL_READ_FRAMEBUFFER, 0))
write(ctx, cb.GlDeleteFramebuffers(1, sb.readsData(ctx, src)))
write(ctx, cb.GlBindFramebuffer(GLenum_GL_DRAW_FRAMEBUFFER, 0))
write(ctx, cb.GlDeleteFramebuffers(1, sb.readsData(ctx, dst)))
}
}
}
func getTextureTargetInfo(target GLenum) (isMultisample, isArray, is3D bool) {
switch target {
case GLenum_GL_TEXTURE_2D:
break
case GLenum_GL_TEXTURE_2D_ARRAY:
isArray = true
case GLenum_GL_TEXTURE_2D_MULTISAMPLE:
isMultisample = true
case GLenum_GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
isMultisample, isArray = true, true
case GLenum_GL_TEXTURE_3D:
is3D = true
case GLenum_GL_TEXTURE_BUFFER:
break
case GLenum_GL_TEXTURE_CUBE_MAP:
break
case GLenum_GL_TEXTURE_CUBE_MAP_ARRAY:
isArray = true
case GLenum_GL_TEXTURE_EXTERNAL_OES:
break
default:
panic(fmt.Errorf("Unsupported texture type: %v", target))
}
return
}
// must be after renderbuffers and textures
func (sb *stateBuilder) framebufferObject(ctx context.Context, c Contextʳ, fb Framebufferʳ) {
write, cb, id := sb.write, sb.cb, fb.GetID()
target := GLenum_GL_FRAMEBUFFER
write(ctx, cb.GlBindFramebuffer(target, id))
if id == 0 {
if drawBuffer := fb.DrawBuffer().Get(0); drawBuffer != GLenum_GL_BACK {
write(ctx, cb.GlDrawBuffers(1, sb.readsData(ctx, drawBuffer)))
}
if fb.ReadBuffer() != GLenum_GL_BACK {
write(ctx, cb.GlReadBuffer(fb.ReadBuffer()))
}
} else {
// Attachments
attach := func(name GLenum, a FramebufferAttachment) {
switch a.Type() {
case GLenum_GL_RENDERBUFFER:
write(ctx, cb.GlFramebufferRenderbuffer(target, name, a.Type(), a.Renderbuffer().GetID()))
case GLenum_GL_TEXTURE:
ty, id, level, layer := a.Texture().Kind(), a.Texture().GetID(), a.TextureLevel(), a.TextureLayer()
_, isArray, is3D := getTextureTargetInfo(ty)
if a.NumViews() > 1 {
write(ctx, cb.GlFramebufferTextureMultiviewOVR(target, name, id, level, layer, a.NumViews()))
} else if ty == GLenum_GL_TEXTURE_CUBE_MAP {
if a.Layered() == GLboolean_GL_TRUE {
write(ctx, cb.GlFramebufferTexture(target, name, id, level)) // GLES32
} else {
ty = GLenum_GL_TEXTURE_CUBE_MAP_POSITIVE_X + GLenum(a.TextureLayer()%6)
write(ctx, cb.GlFramebufferTexture2D(target, name, ty, id, level))
}
} else if isArray || is3D {
if a.Layered() == GLboolean_GL_TRUE {
write(ctx, cb.GlFramebufferTexture(target, name, id, level)) // GLES32
} else if a.NumViews() == 1 {
write(ctx, cb.GlFramebufferTextureLayer(target, name, id, level, layer)) // GLES30
}
} else {
write(ctx, cb.GlFramebufferTexture2D(target, name, ty, id, level))
}
}
}
for i, a := range fb.ColorAttachments().All() {
attach(GLenum_GL_COLOR_ATTACHMENT0+GLenum(i), a)
}
attach(GLenum_GL_DEPTH_ATTACHMENT, fb.DepthAttachment())
attach(GLenum_GL_STENCIL_ATTACHMENT, fb.StencilAttachment())
// Active attachments
drawBuffers := []GLenum{}
for i := 0; i < fb.DrawBuffer().Len(); i++ {
drawBuffers = append(drawBuffers, fb.DrawBuffer().Get(GLint(i)))
}
write(ctx, cb.GlDrawBuffers(GLsizei(len(drawBuffers)), sb.readsData(ctx, drawBuffers)))
write(ctx, cb.GlReadBuffer(fb.ReadBuffer()))
// Parameters
param := func(name GLenum, value GLint) {
write(ctx, cb.GlFramebufferParameteri(target, name, value))
}
param(GLenum_GL_FRAMEBUFFER_DEFAULT_WIDTH, fb.DefaultWidth()) // GLES31
param(GLenum_GL_FRAMEBUFFER_DEFAULT_HEIGHT, fb.DefaultHeight()) // GLES31
param(GLenum_GL_FRAMEBUFFER_DEFAULT_LAYERS, fb.DefaultLayers()) // GLES32
param(GLenum_GL_FRAMEBUFFER_DEFAULT_SAMPLES, fb.DefaultSamples()) // GLES31
if fb.DefaultFixedSampleLocations() == GLboolean_GL_TRUE {
param(GLenum_GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, 1) // GLES31
}
}
}
func (sb *stateBuilder) imageUnit(ctx context.Context, i ImageUnitʳ) {
write, cb, id := sb.write, sb.cb, i.GetID()
if !i.Texture().IsNil() {
write(ctx, cb.GlBindImageTexture(id, i.Texture().GetID(), i.Level(), i.Layered(), i.Layer(), i.Access(), i.Fmt())) // GLES31
}
}
func (sb *stateBuilder) shaderObject(ctx context.Context, s Shaderʳ) {
write, cb, id := sb.write, sb.cb, s.GetID()
write(ctx, cb.GlCreateShader(s.Type(), id))
if e := s.CompileExtra(); !e.IsNil() {
if !e.Binary().IsNil() {
sb.E(ctx, "Precompiled shaders not suppored yet") // TODO
}
write(ctx, cb.GlShaderSource(id, 1, sb.readsData(ctx, sb.readsData(ctx, e.Source())), memory.Nullptr))
write(ctx, api.WithExtras(cb.GlCompileShader(id), e.Get().Clone(cb.Arena, sb.cloneCtx)))
}
write(ctx, cb.GlShaderSource(id, 1, sb.readsData(ctx, sb.readsData(ctx, s.Source())), memory.Nullptr))
}
func (sb *stateBuilder) programObject(ctx context.Context, p Programʳ) {
write, cb, id := sb.write, sb.cb, p.GetID()
write(ctx, cb.GlCreateProgram(id))
for name, location := range p.AttributeBindings().All() {
write(ctx, cb.GlBindAttribLocation(id, location, sb.readsData(ctx, name)))
}
if count := p.TransformFeedbackVaryings().Len(); count > 0 {
varyings := make([]memory.Pointer, count)
for i, varying := range p.TransformFeedbackVaryings().All() {
varyings[i] = sb.readsData(ctx, varying)
}
mode := p.TransformFeedbackBufferMode()
write(ctx, cb.GlTransformFeedbackVaryings(id, GLsizei(len(varyings)), sb.readsData(ctx, varyings), mode))
}
if p.Separable() {
write(ctx, cb.GlProgramParameteri(id, GLenum_GL_PROGRAM_SEPARABLE, 1))
}
if p.BinaryRetrievableHint() {
write(ctx, cb.GlProgramParameteri(id, GLenum_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, 1))
}
if !p.LinkExtra().IsNil() {
if p.SuccessfulLinkExtra() != p.LinkExtra() {
sb.E(ctx, "Stale program executable not suppored yet") // TODO
}
if !p.LinkExtra().Binary().IsNil() {
sb.E(ctx, "Precompiled programs not suppored yet") // TODO
}
// Create the shaders from the extra.
attachedShaders := []ShaderId{}
for t, s := range p.LinkExtra().Shaders().All() {
if !s.Binary().IsNil() {
sb.E(ctx, "Precompiled programs not suppored yet") // TODO
continue
}
write(ctx, cb.GlCreateShader(t, s.ID()))
write(ctx, cb.GlShaderSource(s.ID(), 1, sb.readsData(ctx, sb.readsData(ctx, s.Source())), memory.Nullptr))
write(ctx, api.WithExtras(cb.GlCompileShader(s.ID()), s.Get().Clone(cb.Arena, sb.cloneCtx)))
write(ctx, cb.GlAttachShader(id, s.ID()))
attachedShaders = append(attachedShaders, s.ID())
}
write(ctx, api.WithExtras(cb.GlLinkProgram(id), p.LinkExtra().Get().Clone(cb.Arena, sb.cloneCtx)))
write(ctx, cb.GlUseProgram(id))
for _, u := range p.ActiveResources().DefaultUniformBlock().All() {
if loc, ok := u.Locations().Lookup(0); ok {
sb.uniform(ctx, u.Type(), UniformLocation(loc), GLsizei(u.ArraySize()), sb.readsSlice(ctx, u.Value()))
}
}
for loc, b := range p.ActiveResources().UniformBlocks().All() {
if index := b.Binding(); index > 0 {
write(ctx, cb.GlUniformBlockBinding(id, UniformBlockIndex(loc), GLuint(index)))
}
}
write(ctx, cb.GlUseProgram(0))
// Detach and delete the linked shaders (we'll attach the shaders from the state later).
for _, shaderID := range attachedShaders {
write(ctx, cb.GlDetachShader(id, shaderID))
write(ctx, cb.GlDeleteShader(shaderID))
}
}
if !p.ValidateExtra().IsNil() {
write(ctx, api.WithExtras(cb.GlValidateProgram(id), p.ValidateExtra().Get().Clone(cb.Arena, sb.cloneCtx)))
}
}
func (sb *stateBuilder) attachShaders(ctx context.Context, p Programʳ) {
write, cb, id := sb.write, sb.cb, p.GetID()
for _, s := range p.Shaders().All() {
if s := s.GetID(); s != 0 {
write(ctx, cb.GlAttachShader(id, s))
}
}
}
func (sb *stateBuilder) uniform(ctx context.Context, ty GLenum, loc UniformLocation, n GLsizei, v memory.Pointer) {
write, cb := sb.write, sb.cb
switch ty {
case GLenum_GL_FLOAT:
write(ctx, cb.GlUniform1fv(loc, n, v))
case GLenum_GL_FLOAT_VEC2:
write(ctx, cb.GlUniform2fv(loc, n, v))
case GLenum_GL_FLOAT_VEC3:
write(ctx, cb.GlUniform3fv(loc, n, v))
case GLenum_GL_FLOAT_VEC4:
write(ctx, cb.GlUniform4fv(loc, n, v))
case GLenum_GL_BOOL:
case GLenum_GL_INT:
write(ctx, cb.GlUniform1iv(loc, n, v))
case GLenum_GL_BOOL_VEC2:
case GLenum_GL_INT_VEC2:
write(ctx, cb.GlUniform2iv(loc, n, v))
case GLenum_GL_BOOL_VEC3:
case GLenum_GL_INT_VEC3:
write(ctx, cb.GlUniform3iv(loc, n, v))
case GLenum_GL_BOOL_VEC4:
case GLenum_GL_INT_VEC4:
write(ctx, cb.GlUniform4iv(loc, n, v))
case GLenum_GL_UNSIGNED_INT:
write(ctx, cb.GlUniform1uiv(loc, n, v))
case GLenum_GL_UNSIGNED_INT_VEC2:
write(ctx, cb.GlUniform2uiv(loc, n, v))
case GLenum_GL_UNSIGNED_INT_VEC3:
write(ctx, cb.GlUniform3uiv(loc, n, v))
case GLenum_GL_UNSIGNED_INT_VEC4:
write(ctx, cb.GlUniform4uiv(loc, n, v))
case GLenum_GL_FLOAT_MAT2:
write(ctx, cb.GlUniformMatrix2fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT3:
write(ctx, cb.GlUniformMatrix3fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT4:
write(ctx, cb.GlUniformMatrix4fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT2x3:
write(ctx, cb.GlUniformMatrix2x3fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT2x4:
write(ctx, cb.GlUniformMatrix2x4fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT3x2:
write(ctx, cb.GlUniformMatrix3x2fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT3x4:
write(ctx, cb.GlUniformMatrix3x4fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT4x2:
write(ctx, cb.GlUniformMatrix4x2fv(loc, n, GLboolean_GL_FALSE, v))
case GLenum_GL_FLOAT_MAT4x3:
write(ctx, cb.GlUniformMatrix4x3fv(loc, n, GLboolean_GL_FALSE, v))
default:
write(ctx, cb.GlUniform1iv(loc, n, v))
}
}
func (sb *stateBuilder) pipelineObject(ctx context.Context, pipe Pipelineʳ) {
write, cb, id := sb.write, sb.cb, pipe.GetID()
write(ctx, cb.GlBindProgramPipeline(id))
write(ctx, cb.GlActiveShaderProgram(id, pipe.ActiveProgram().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_COMPUTE_SHADER_BIT, pipe.ComputeShader().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_FRAGMENT_SHADER_BIT, pipe.FragmentShader().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_VERTEX_SHADER_BIT, pipe.VertexShader().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_TESS_CONTROL_SHADER_BIT, pipe.TessControlShader().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_TESS_EVALUATION_SHADER_BIT, pipe.TessEvaluationShader().GetID()))
write(ctx, cb.GlUseProgramStages(id, GLbitfield_GL_GEOMETRY_SHADER_BIT, pipe.GeometryShader().GetID()))
if !pipe.ValidateExtra().IsNil() {
write(ctx, api.WithExtras(cb.GlValidateProgramPipeline(id), pipe.ValidateExtra().Get().Clone(cb.Arena, sb.cloneCtx)))
}
write(ctx, cb.GlBindProgramPipeline(0))
}
func (sb *stateBuilder) textureObject(ctx context.Context, t Textureʳ) {
write, cb, id := sb.write, sb.cb, t.GetID()
target := t.Kind()
isMultisample, isArray, is3D := getTextureTargetInfo(target)
isCompressed := func(img Imageʳ) bool {
return GetSizedFormatInfoOrPanic(img.SizedFormat()).Compression() != CompressionAlgorithm_Uncompressed
}
write(ctx, cb.GlBindTexture(target, id))
// Allocate space for texture data
if target == GLenum_GL_TEXTURE_BUFFER {
b := t.Buffer()
write(ctx, cb.GlTexBufferRange(target, b.InternalFormat(), b.Binding().GetID(), b.Offset(), b.Size()))
} else if target == GLenum_GL_TEXTURE_EXTERNAL_OES {
// The dimensions are fully specified by the EGLimage
} else if t.ImmutableFormat() == GLboolean_GL_TRUE {
img := t.Levels().Get(0).Layers().Get(0) // Must exist
lvl, fmt := GLsizei(t.Levels().Len()), img.getCorrectInternalFormat()
w, h, d := img.Width(), img.Height(), GLsizei(t.Levels().Get(0).Layers().Len())
samples, fixed := img.Samples(), img.FixedSampleLocations()
if isMultisample {
if isArray || is3D {
write(ctx, cb.GlTexStorage3DMultisample(target, samples, fmt, w, h, d, fixed))
} else {
write(ctx, cb.GlTexStorage2DMultisample(target, samples, fmt, w, h, fixed))
}
} else {
if isArray || is3D {
write(ctx, cb.GlTexStorage3D(target, lvl, fmt, w, h, d))
} else {
write(ctx, cb.GlTexStorage2D(target, lvl, fmt, w, h))
}
}
} else if isArray || is3D {
for lvl, levelObject := range t.Levels().All() {
img := levelObject.Layers().Get(0) // Must exist, all layers must be consistent.
fmt, w, h, d := img.getCorrectInternalFormat(), img.Width(), img.Height(), GLsizei(levelObject.Layers().Len())
dataFormat, dataType := img.getUnsizedFormatAndType()
if isCompressed(img) {
dataSize := GLsizei(img.Data().Size()) * d
write(ctx, cb.GlCompressedTexImage3D(target, lvl, fmt, w, h, d, 0, dataSize, memory.Nullptr))
} else {
write(ctx, cb.GlTexImage3D(target, lvl, GLint(fmt), w, h, d, 0, dataFormat, dataType, memory.Nullptr))
}
}
} else {
for lvl, levelObject := range t.Levels().All() {
for layer, img := range levelObject.Layers().All() {
// NB: Each face of cubemap faces can technically have different format and size.
fmt, w, h := img.getCorrectInternalFormat(), img.Width(), img.Height()
dataFormat, dataType := img.getUnsizedFormatAndType()
target := target
if target == GLenum_GL_TEXTURE_CUBE_MAP {
target = GLenum_GL_TEXTURE_CUBE_MAP_POSITIVE_X + GLenum(layer%6)
}
if isCompressed(img) {
write(ctx, cb.GlCompressedTexImage2D(target, lvl, fmt, w, h, 0, GLsizei(img.Data().Size()), memory.Nullptr))
} else {
write(ctx, cb.GlTexImage2D(target, lvl, GLint(fmt), w, h, 0, dataFormat, dataType, memory.Nullptr))
}
}
}
}
// Upload the layers one by one
for lvl, levelObject := range t.Levels().All() {
for layer, img := range levelObject.Layers().All() {
fmt, w, h, d := img.getCorrectInternalFormat(), img.Width(), img.Height(), GLsizei(1)
dataFormat, dataType := img.getUnsizedFormatAndType()
dataSize, data := GLsizei(img.Data().Size()), sb.readsSlice(ctx, img.Data())
if dataSize == 0 {
continue // The texture layer was not initialized.
} else if target == GLenum_GL_TEXTURE_BUFFER {
continue // There should be no images or layers.
} else if target == GLenum_GL_TEXTURE_EXTERNAL_OES {
continue // The content is fully specified by the EGLimage
} else if isArray || is3D {
if isCompressed(img) {
write(ctx, cb.GlCompressedTexSubImage3D(target, lvl, 0, 0, layer, w, h, d, fmt, dataSize, data))
} else {
write(ctx, cb.GlTexSubImage3D(target, lvl, 0, 0, layer, w, h, d, dataFormat, dataType, data))
}
} else {
target := target
if target == GLenum_GL_TEXTURE_CUBE_MAP {
target = GLenum_GL_TEXTURE_CUBE_MAP_POSITIVE_X + GLenum(layer%6)
}
if isCompressed(img) {
write(ctx, cb.GlCompressedTexSubImage2D(target, lvl, 0, 0, w, h, fmt, dataSize, data))
} else {
write(ctx, cb.GlTexSubImage2D(target, lvl, 0, 0, w, h, dataFormat, dataType, data))
}
}
}
}
defaults := MakeTexture(sb.tmpArena)
parami := func(name GLenum, value GLint, defaultValue GLint) {
if value != defaultValue || target == GLenum_GL_TEXTURE_EXTERNAL_OES {
write(ctx, cb.GlTexParameteri(target, name, value))
}
}
parami(GLenum_GL_TEXTURE_MAG_FILTER, GLint(t.MagFilter()), GLint(defaults.MagFilter()))
parami(GLenum_GL_TEXTURE_MIN_FILTER, GLint(t.MinFilter()), GLint(defaults.MinFilter()))
parami(GLenum_GL_TEXTURE_WRAP_S, GLint(t.WrapS()), GLint(defaults.WrapS()))
parami(GLenum_GL_TEXTURE_WRAP_T, GLint(t.WrapT()), GLint(defaults.WrapT()))
parami(GLenum_GL_TEXTURE_WRAP_R, GLint(t.WrapR()), GLint(defaults.WrapR()))
parami(GLenum_GL_TEXTURE_COMPARE_FUNC, GLint(t.CompareFunc()), GLint(defaults.CompareFunc()))
parami(GLenum_GL_TEXTURE_COMPARE_MODE, GLint(t.CompareMode()), GLint(defaults.CompareMode()))
parami(GLenum_GL_TEXTURE_BASE_LEVEL, t.BaseLevel(), defaults.BaseLevel())
parami(GLenum_GL_TEXTURE_MAX_LEVEL, t.MaxLevel(), defaults.MaxLevel())
parami(GLenum_GL_TEXTURE_SWIZZLE_A, GLint(t.SwizzleA()), GLint(defaults.SwizzleA()))
parami(GLenum_GL_TEXTURE_SWIZZLE_B, GLint(t.SwizzleB()), GLint(defaults.SwizzleB()))
parami(GLenum_GL_TEXTURE_SWIZZLE_G, GLint(t.SwizzleG()), GLint(defaults.SwizzleG()))
parami(GLenum_GL_TEXTURE_SWIZZLE_R, GLint(t.SwizzleR()), GLint(defaults.SwizzleR()))
parami(GLenum_GL_DEPTH_STENCIL_TEXTURE_MODE, GLint(t.DepthStencilTextureMode()), GLint(defaults.DepthStencilTextureMode()))
if t.MaxLod() != defaults.MaxLod() {
write(ctx, cb.GlTexParameterf(target, GLenum_GL_TEXTURE_MAX_LOD, t.MaxLod()))
}
if t.MinLod() != defaults.MinLod() {
write(ctx, cb.GlTexParameterf(target, GLenum_GL_TEXTURE_MIN_LOD, t.MinLod()))
}
if !t.BorderColor().EqualTo(0, 0, 0, 0) {
write(ctx, cb.GlTexParameterfv(target, GLenum_GL_TEXTURE_BORDER_COLOR, sb.readsData(ctx, t.BorderColor())))
} else if !t.BorderColorI().EqualTo(0, 0, 0, 0) {
write(ctx, cb.GlTexParameteriv(target, GLenum_GL_TEXTURE_BORDER_COLOR, sb.readsData(ctx, t.BorderColorI())))
}
if t.MaxAnisotropy() != 1.0 {
write(ctx, cb.GlTexParameterf(target, GLenum_GL_TEXTURE_MAX_ANISOTROPY_EXT, GLfloat(t.MaxAnisotropy())))
}
}
func (sb *stateBuilder) samplerObject(ctx context.Context, s Samplerʳ) {
write, cb, id := sb.write, sb.cb, s.GetID()
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_COMPARE_FUNC, GLint(s.CompareFunc())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_COMPARE_MODE, GLint(s.CompareMode())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_MIN_FILTER, GLint(s.MinFilter())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_MAG_FILTER, GLint(s.MagFilter())))
write(ctx, cb.GlSamplerParameterf(id, GLenum_GL_TEXTURE_MIN_LOD, GLfloat(s.MinLod())))
write(ctx, cb.GlSamplerParameterf(id, GLenum_GL_TEXTURE_MAX_LOD, GLfloat(s.MaxLod())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_WRAP_R, GLint(s.WrapR())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_WRAP_S, GLint(s.WrapS())))
write(ctx, cb.GlSamplerParameteri(id, GLenum_GL_TEXTURE_WRAP_T, GLint(s.WrapT())))
write(ctx, cb.GlSamplerParameterf(id, GLenum_GL_TEXTURE_MAX_ANISOTROPY_EXT, GLfloat(s.MaxAnisotropy())))
if !s.BorderColor().EqualTo(0, 0, 0, 0) {
write(ctx, cb.GlSamplerParameterfv(id, GLenum_GL_TEXTURE_BORDER_COLOR, sb.readsData(ctx, s.BorderColor()))) // GLES32
} else if !s.BorderColorI().EqualTo(0, 0, 0, 0) {
write(ctx, cb.GlSamplerParameterIiv(id, GLenum_GL_TEXTURE_BORDER_COLOR, sb.readsData(ctx, s.BorderColorI()))) // GLES32
}
}
func (sb *stateBuilder) queryObject(ctx context.Context, q Queryʳ) {
write, cb, id := sb.write, sb.cb, q.GetID()
target := q.Type()
write(ctx, cb.GlBeginQuery(target, id))
if !q.Active() {