-
Notifications
You must be signed in to change notification settings - Fork 920
/
queue.rs
1881 lines (1825 loc) · 82.3 KB
/
queue.rs
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
use super::{conv::is_layered_target, Command as C, PrivateCapabilities};
use arrayvec::ArrayVec;
use glow::HasContext;
use std::{
mem::size_of,
slice,
sync::{atomic::Ordering, Arc},
};
const DEBUG_ID: u32 = 0;
fn extract_marker<'a>(data: &'a [u8], range: &std::ops::Range<u32>) -> &'a str {
std::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap()
}
fn get_2d_target(target: u32, array_layer: u32) -> u32 {
const CUBEMAP_FACES: [u32; 6] = [
glow::TEXTURE_CUBE_MAP_POSITIVE_X,
glow::TEXTURE_CUBE_MAP_NEGATIVE_X,
glow::TEXTURE_CUBE_MAP_POSITIVE_Y,
glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,
glow::TEXTURE_CUBE_MAP_POSITIVE_Z,
glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,
];
match target {
glow::TEXTURE_2D => target,
glow::TEXTURE_CUBE_MAP => CUBEMAP_FACES[array_layer as usize],
_ => unreachable!(),
}
}
fn get_z_offset(target: u32, base: &crate::TextureCopyBase) -> u32 {
match target {
glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY => base.array_layer,
glow::TEXTURE_3D => base.origin.z,
_ => unreachable!(),
}
}
impl super::Queue {
/// Performs a manual shader clear, used as a workaround for a clearing bug on mesa
unsafe fn perform_shader_clear(&self, gl: &glow::Context, draw_buffer: u32, color: [f32; 4]) {
let shader_clear = self
.shader_clear_program
.as_ref()
.expect("shader_clear_program should always be set if the workaround is enabled");
unsafe { gl.use_program(Some(shader_clear.program)) };
unsafe {
gl.uniform_4_f32(
Some(&shader_clear.color_uniform_location),
color[0],
color[1],
color[2],
color[3],
)
};
unsafe { gl.disable(glow::DEPTH_TEST) };
unsafe { gl.disable(glow::STENCIL_TEST) };
unsafe { gl.disable(glow::SCISSOR_TEST) };
unsafe { gl.disable(glow::BLEND) };
unsafe { gl.disable(glow::CULL_FACE) };
unsafe { gl.draw_buffers(&[glow::COLOR_ATTACHMENT0 + draw_buffer]) };
unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) };
let draw_buffer_count = self.draw_buffer_count.load(Ordering::Relaxed);
if draw_buffer_count != 0 {
// Reset the draw buffers to what they were before the clear
let indices = (0..draw_buffer_count as u32)
.map(|i| glow::COLOR_ATTACHMENT0 + i)
.collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>();
unsafe { gl.draw_buffers(&indices) };
}
}
unsafe fn reset_state(&self, gl: &glow::Context) {
unsafe { gl.use_program(None) };
unsafe { gl.bind_framebuffer(glow::FRAMEBUFFER, None) };
unsafe { gl.disable(glow::DEPTH_TEST) };
unsafe { gl.disable(glow::STENCIL_TEST) };
unsafe { gl.disable(glow::SCISSOR_TEST) };
unsafe { gl.disable(glow::BLEND) };
unsafe { gl.disable(glow::CULL_FACE) };
unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) };
unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) };
if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
unsafe { gl.disable(glow::DEPTH_CLAMP) };
}
unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None) };
let mut current_index_buffer = self.current_index_buffer.lock();
*current_index_buffer = None;
}
unsafe fn set_attachment(
&self,
gl: &glow::Context,
fbo_target: u32,
attachment: u32,
view: &super::TextureView,
) {
match view.inner {
super::TextureInner::Renderbuffer { raw } => {
unsafe {
gl.framebuffer_renderbuffer(
fbo_target,
attachment,
glow::RENDERBUFFER,
Some(raw),
)
};
}
super::TextureInner::DefaultRenderbuffer => panic!("Unexpected default RBO"),
super::TextureInner::Texture { raw, target } => {
let num_layers = view.array_layers.end - view.array_layers.start;
if num_layers > 1 {
#[cfg(webgl)]
unsafe {
gl.framebuffer_texture_multiview_ovr(
fbo_target,
attachment,
Some(raw),
view.mip_levels.start as i32,
view.array_layers.start as i32,
num_layers as i32,
)
};
} else if is_layered_target(target) {
unsafe {
gl.framebuffer_texture_layer(
fbo_target,
attachment,
Some(raw),
view.mip_levels.start as i32,
view.array_layers.start as i32,
)
};
} else {
unsafe {
assert_eq!(view.mip_levels.len(), 1);
gl.framebuffer_texture_2d(
fbo_target,
attachment,
get_2d_target(target, view.array_layers.start),
Some(raw),
view.mip_levels.start as i32,
)
};
}
}
#[cfg(webgl)]
super::TextureInner::ExternalFramebuffer { ref inner } => unsafe {
gl.bind_external_framebuffer(glow::FRAMEBUFFER, inner);
},
}
}
unsafe fn process(
&self,
gl: &glow::Context,
command: &C,
#[cfg_attr(target_arch = "wasm32", allow(unused))] data_bytes: &[u8],
queries: &[glow::Query],
) {
match *command {
C::Draw {
topology,
first_vertex,
vertex_count,
instance_count,
first_instance,
ref first_instance_location,
} => {
let supports_full_instancing = self
.shared
.private_caps
.contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);
if supports_full_instancing {
unsafe {
gl.draw_arrays_instanced_base_instance(
topology,
first_vertex as i32,
vertex_count as i32,
instance_count as i32,
first_instance,
)
}
} else {
unsafe {
gl.uniform_1_u32(first_instance_location.as_ref(), first_instance);
}
// Don't use `gl.draw_arrays` for `instance_count == 1`.
// Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `draw_arrays`.
// See https://github.com/gfx-rs/wgpu/issues/3578
unsafe {
gl.draw_arrays_instanced(
topology,
first_vertex as i32,
vertex_count as i32,
instance_count as i32,
)
}
};
}
C::DrawIndexed {
topology,
index_type,
index_count,
index_offset,
base_vertex,
first_instance,
instance_count,
ref first_instance_location,
} => {
let supports_full_instancing = self
.shared
.private_caps
.contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);
if supports_full_instancing {
unsafe {
gl.draw_elements_instanced_base_vertex_base_instance(
topology,
index_count as i32,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
first_instance,
)
}
} else {
unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), first_instance) };
if base_vertex == 0 {
unsafe {
// Don't use `gl.draw_elements`/`gl.draw_elements_base_vertex` for `instance_count == 1`.
// Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `gl.draw_elements`/`gl.draw_elements_base_vertex`.
// See https://github.com/gfx-rs/wgpu/issues/3578
gl.draw_elements_instanced(
topology,
index_count as i32,
index_type,
index_offset as i32,
instance_count as i32,
)
}
} else {
// If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature.
unsafe {
gl.draw_elements_instanced_base_vertex(
topology,
index_count as _,
index_type,
index_offset as i32,
instance_count as i32,
base_vertex,
)
}
}
}
}
C::DrawIndirect {
topology,
indirect_buf,
indirect_offset,
ref first_instance_location,
} => {
unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };
unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };
unsafe { gl.draw_arrays_indirect_offset(topology, indirect_offset as i32) };
}
C::DrawIndexedIndirect {
topology,
index_type,
indirect_buf,
indirect_offset,
ref first_instance_location,
} => {
unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };
unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };
unsafe {
gl.draw_elements_indirect_offset(topology, index_type, indirect_offset as i32)
};
}
C::Dispatch(group_counts) => {
unsafe { gl.dispatch_compute(group_counts[0], group_counts[1], group_counts[2]) };
}
C::DispatchIndirect {
indirect_buf,
indirect_offset,
} => {
unsafe { gl.bind_buffer(glow::DISPATCH_INDIRECT_BUFFER, Some(indirect_buf)) };
unsafe { gl.dispatch_compute_indirect(indirect_offset as i32) };
}
C::ClearBuffer {
ref dst,
dst_target,
ref range,
} => match dst.raw {
Some(buffer) => {
// When `INDEX_BUFFER_ROLE_CHANGE` isn't available, we can't copy into the
// index buffer from the zero buffer. This would fail in Chrome with the
// following message:
//
// > Cannot copy into an element buffer destination from a non-element buffer
// > source
//
// Instead, we'll upload zeroes into the buffer.
let can_use_zero_buffer = self
.shared
.private_caps
.contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)
|| dst_target != glow::ELEMENT_ARRAY_BUFFER;
if can_use_zero_buffer {
unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(self.zero_buffer)) };
unsafe { gl.bind_buffer(dst_target, Some(buffer)) };
let mut dst_offset = range.start;
while dst_offset < range.end {
let size = (range.end - dst_offset).min(super::ZERO_BUFFER_SIZE as u64);
unsafe {
gl.copy_buffer_sub_data(
glow::COPY_READ_BUFFER,
dst_target,
0,
dst_offset as i32,
size as i32,
)
};
dst_offset += size;
}
} else {
unsafe { gl.bind_buffer(dst_target, Some(buffer)) };
let zeroes = vec![0u8; (range.end - range.start) as usize];
unsafe {
gl.buffer_sub_data_u8_slice(dst_target, range.start as i32, &zeroes)
};
}
}
None => {
dst.data.as_ref().unwrap().lock().unwrap().as_mut_slice()
[range.start as usize..range.end as usize]
.fill(0);
}
},
C::CopyBufferToBuffer {
ref src,
src_target,
ref dst,
dst_target,
copy,
} => {
let copy_src_target = glow::COPY_READ_BUFFER;
let is_index_buffer_only_element_dst = !self
.shared
.private_caps
.contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)
&& dst_target == glow::ELEMENT_ARRAY_BUFFER
|| src_target == glow::ELEMENT_ARRAY_BUFFER;
// WebGL not allowed to copy data from other targets to element buffer and can't copy element data to other buffers
let copy_dst_target = if is_index_buffer_only_element_dst {
glow::ELEMENT_ARRAY_BUFFER
} else {
glow::COPY_WRITE_BUFFER
};
let size = copy.size.get() as usize;
match (src.raw, dst.raw) {
(Some(ref src), Some(ref dst)) => {
unsafe { gl.bind_buffer(copy_src_target, Some(*src)) };
unsafe { gl.bind_buffer(copy_dst_target, Some(*dst)) };
unsafe {
gl.copy_buffer_sub_data(
copy_src_target,
copy_dst_target,
copy.src_offset as _,
copy.dst_offset as _,
copy.size.get() as _,
)
};
}
(Some(src), None) => {
let mut data = dst.data.as_ref().unwrap().lock().unwrap();
let dst_data = &mut data.as_mut_slice()
[copy.dst_offset as usize..copy.dst_offset as usize + size];
unsafe { gl.bind_buffer(copy_src_target, Some(src)) };
unsafe {
self.shared.get_buffer_sub_data(
gl,
copy_src_target,
copy.src_offset as i32,
dst_data,
)
};
}
(None, Some(dst)) => {
let data = src.data.as_ref().unwrap().lock().unwrap();
let src_data = &data.as_slice()
[copy.src_offset as usize..copy.src_offset as usize + size];
unsafe { gl.bind_buffer(copy_dst_target, Some(dst)) };
unsafe {
gl.buffer_sub_data_u8_slice(
copy_dst_target,
copy.dst_offset as i32,
src_data,
)
};
}
(None, None) => {
todo!()
}
}
unsafe { gl.bind_buffer(copy_src_target, None) };
if is_index_buffer_only_element_dst {
unsafe {
gl.bind_buffer(
glow::ELEMENT_ARRAY_BUFFER,
*self.current_index_buffer.lock(),
)
};
} else {
unsafe { gl.bind_buffer(copy_dst_target, None) };
}
}
#[cfg(webgl)]
C::CopyExternalImageToTexture {
ref src,
dst,
dst_target,
dst_format,
dst_premultiplication,
ref copy,
} => {
const UNPACK_FLIP_Y_WEBGL: u32 =
web_sys::WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL;
const UNPACK_PREMULTIPLY_ALPHA_WEBGL: u32 =
web_sys::WebGl2RenderingContext::UNPACK_PREMULTIPLY_ALPHA_WEBGL;
unsafe {
if src.flip_y {
gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, true);
}
if dst_premultiplication {
gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
}
}
unsafe { gl.bind_texture(dst_target, Some(dst)) };
let format_desc = self.shared.describe_texture_format(dst_format);
if is_layered_target(dst_target) {
let z_offset = get_z_offset(dst_target, ©.dst_base);
match src.source {
wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {
gl.tex_sub_image_3d_with_image_bitmap(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
b,
);
},
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
gl.tex_sub_image_3d_with_html_image_element(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
gl.tex_sub_image_3d_with_html_video_element(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
v,
);
},
#[cfg(web_sys_unstable_apis)]
wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {
gl.tex_sub_image_3d_with_video_frame(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
v,
)
},
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
gl.tex_sub_image_3d_with_image_data(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
gl.tex_sub_image_3d_with_html_canvas_element(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
z_offset as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
c,
);
},
wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(),
}
} else {
let dst_target = get_2d_target(dst_target, copy.dst_base.array_layer);
match src.source {
wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {
gl.tex_sub_image_2d_with_image_bitmap_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
b,
);
},
wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
gl.tex_sub_image_2d_with_html_image_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
i,
)
},
wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
gl.tex_sub_image_2d_with_html_video_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
v,
)
},
#[cfg(web_sys_unstable_apis)]
wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {
gl.tex_sub_image_2d_with_video_frame_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
v,
)
},
wgt::ExternalImageSource::ImageData(ref i) => unsafe {
gl.tex_sub_image_2d_with_image_data_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
i,
);
},
wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
c,
)
},
wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(),
}
}
unsafe {
if src.flip_y {
gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, false);
}
if dst_premultiplication {
gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
}
}
}
C::CopyTextureToTexture {
src,
src_target,
dst,
dst_target,
ref copy,
} => {
//TODO: handle 3D copies
unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };
if is_layered_target(src_target) {
//TODO: handle GLES without framebuffer_texture_3d
unsafe {
gl.framebuffer_texture_layer(
glow::READ_FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
Some(src),
copy.src_base.mip_level as i32,
copy.src_base.array_layer as i32,
)
};
} else {
unsafe {
gl.framebuffer_texture_2d(
glow::READ_FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
src_target,
Some(src),
copy.src_base.mip_level as i32,
)
};
}
unsafe { gl.bind_texture(dst_target, Some(dst)) };
if is_layered_target(dst_target) {
unsafe {
gl.copy_tex_sub_image_3d(
dst_target,
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
get_z_offset(dst_target, ©.dst_base) as i32,
copy.src_base.origin.x as i32,
copy.src_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
)
};
} else {
unsafe {
gl.copy_tex_sub_image_2d(
get_2d_target(dst_target, copy.dst_base.array_layer),
copy.dst_base.mip_level as i32,
copy.dst_base.origin.x as i32,
copy.dst_base.origin.y as i32,
copy.src_base.origin.x as i32,
copy.src_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
)
};
}
}
C::CopyBufferToTexture {
ref src,
src_target: _,
dst,
dst_target,
dst_format,
ref copy,
} => {
let (block_width, block_height) = dst_format.block_dimensions();
let block_size = dst_format.block_copy_size(None).unwrap();
let format_desc = self.shared.describe_texture_format(dst_format);
let row_texels = copy
.buffer_layout
.bytes_per_row
.map_or(0, |bpr| block_width * bpr / block_size);
let column_texels = copy
.buffer_layout
.rows_per_image
.map_or(0, |rpi| block_height * rpi);
unsafe { gl.bind_texture(dst_target, Some(dst)) };
unsafe { gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, row_texels as i32) };
unsafe { gl.pixel_store_i32(glow::UNPACK_IMAGE_HEIGHT, column_texels as i32) };
let mut unbind_unpack_buffer = false;
if !dst_format.is_compressed() {
let buffer_data;
let unpack_data = match src.raw {
Some(buffer) => {
unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };
unbind_unpack_buffer = true;
glow::PixelUnpackData::BufferOffset(copy.buffer_layout.offset as u32)
}
None => {
buffer_data = src.data.as_ref().unwrap().lock().unwrap();
let src_data =
&buffer_data.as_slice()[copy.buffer_layout.offset as usize..];
glow::PixelUnpackData::Slice(src_data)
}
};
if is_layered_target(dst_target) {
unsafe {
gl.tex_sub_image_3d(
dst_target,
copy.texture_base.mip_level as i32,
copy.texture_base.origin.x as i32,
copy.texture_base.origin.y as i32,
get_z_offset(dst_target, ©.texture_base) as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.external,
format_desc.data_type,
unpack_data,
)
};
} else {
unsafe {
gl.tex_sub_image_2d(
get_2d_target(dst_target, copy.texture_base.array_layer),
copy.texture_base.mip_level as i32,
copy.texture_base.origin.x as i32,
copy.texture_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
unpack_data,
)
};
}
} else {
let bytes_per_row = copy
.buffer_layout
.bytes_per_row
.unwrap_or(copy.size.width * block_size);
let minimum_rows_per_image =
(copy.size.height + block_height - 1) / block_height;
let rows_per_image = copy
.buffer_layout
.rows_per_image
.unwrap_or(minimum_rows_per_image);
let bytes_per_image = bytes_per_row * rows_per_image;
let minimum_bytes_per_image = bytes_per_row * minimum_rows_per_image;
let bytes_in_upload =
(bytes_per_image * (copy.size.depth - 1)) + minimum_bytes_per_image;
let offset = copy.buffer_layout.offset as u32;
let buffer_data;
let unpack_data = match src.raw {
Some(buffer) => {
unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };
unbind_unpack_buffer = true;
glow::CompressedPixelUnpackData::BufferRange(
offset..offset + bytes_in_upload,
)
}
None => {
buffer_data = src.data.as_ref().unwrap().lock().unwrap();
let src_data = &buffer_data.as_slice()
[(offset as usize)..(offset + bytes_in_upload) as usize];
glow::CompressedPixelUnpackData::Slice(src_data)
}
};
if is_layered_target(dst_target) {
unsafe {
gl.compressed_tex_sub_image_3d(
dst_target,
copy.texture_base.mip_level as i32,
copy.texture_base.origin.x as i32,
copy.texture_base.origin.y as i32,
get_z_offset(dst_target, ©.texture_base) as i32,
copy.size.width as i32,
copy.size.height as i32,
copy.size.depth as i32,
format_desc.internal,
unpack_data,
)
};
} else {
unsafe {
gl.compressed_tex_sub_image_2d(
get_2d_target(dst_target, copy.texture_base.array_layer),
copy.texture_base.mip_level as i32,
copy.texture_base.origin.x as i32,
copy.texture_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.internal,
unpack_data,
)
};
}
}
if unbind_unpack_buffer {
unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None) };
}
}
C::CopyTextureToBuffer {
src,
src_target,
src_format,
ref dst,
dst_target: _,
ref copy,
} => {
let block_size = src_format.block_copy_size(None).unwrap();
if src_format.is_compressed() {
log::error!("Not implemented yet: compressed texture copy to buffer");
return;
}
if src_target == glow::TEXTURE_CUBE_MAP
|| src_target == glow::TEXTURE_CUBE_MAP_ARRAY
{
log::error!("Not implemented yet: cubemap texture copy to buffer");
return;
}
let format_desc = self.shared.describe_texture_format(src_format);
let row_texels = copy
.buffer_layout
.bytes_per_row
.map_or(copy.size.width, |bpr| bpr / block_size);
let column_texels = copy
.buffer_layout
.rows_per_image
.unwrap_or(copy.size.height);
unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };
let read_pixels = |offset| {
let mut buffer_data;
let unpack_data = match dst.raw {
Some(buffer) => {
unsafe { gl.pixel_store_i32(glow::PACK_ROW_LENGTH, row_texels as i32) };
unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(buffer)) };
glow::PixelPackData::BufferOffset(offset as u32)
}
None => {
buffer_data = dst.data.as_ref().unwrap().lock().unwrap();
let dst_data = &mut buffer_data.as_mut_slice()[offset as usize..];
glow::PixelPackData::Slice(dst_data)
}
};
unsafe {
gl.read_pixels(
copy.texture_base.origin.x as i32,
copy.texture_base.origin.y as i32,
copy.size.width as i32,
copy.size.height as i32,
format_desc.external,
format_desc.data_type,
unpack_data,
)
};
};
match src_target {
glow::TEXTURE_2D => {
unsafe {
gl.framebuffer_texture_2d(
glow::READ_FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
src_target,
Some(src),
copy.texture_base.mip_level as i32,
)
};
read_pixels(copy.buffer_layout.offset);
}
glow::TEXTURE_2D_ARRAY => {
unsafe {
gl.framebuffer_texture_layer(
glow::READ_FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
Some(src),
copy.texture_base.mip_level as i32,
copy.texture_base.array_layer as i32,
)
};
read_pixels(copy.buffer_layout.offset);
}
glow::TEXTURE_3D => {
for z in copy.texture_base.origin.z..copy.size.depth {
unsafe {
gl.framebuffer_texture_layer(
glow::READ_FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
Some(src),
copy.texture_base.mip_level as i32,
z as i32,
)
};
let offset = copy.buffer_layout.offset
+ (z * block_size * row_texels * column_texels) as u64;
read_pixels(offset);
}
}
glow::TEXTURE_CUBE_MAP | glow::TEXTURE_CUBE_MAP_ARRAY => unimplemented!(),
_ => unreachable!(),
}
}
C::SetIndexBuffer(buffer) => {
unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(buffer)) };
let mut current_index_buffer = self.current_index_buffer.lock();
*current_index_buffer = Some(buffer);
}
C::BeginQuery(query, target) => {
unsafe { gl.begin_query(target, query) };
}
C::EndQuery(target) => {
unsafe { gl.end_query(target) };
}
C::TimestampQuery(query) => {
unsafe { gl.query_counter(query, glow::TIMESTAMP) };
}
C::CopyQueryResults {
ref query_range,
ref dst,
dst_target,
dst_offset,
} => {
if self
.shared
.private_caps
.contains(PrivateCapabilities::QUERY_BUFFERS)
&& dst.raw.is_some()
{
unsafe {
// We're assuming that the only relevant queries are 8 byte timestamps or
// occlusion tests.
let query_size = 8;
let query_range_size = query_size * query_range.len();
let buffer = gl.create_buffer().ok();
gl.bind_buffer(glow::QUERY_BUFFER, buffer);
gl.buffer_data_size(
glow::QUERY_BUFFER,
query_range_size as _,
glow::STREAM_COPY,
);
for (i, &query) in queries
[query_range.start as usize..query_range.end as usize]
.iter()
.enumerate()
{
gl.get_query_parameter_u64_with_offset(
query,