-
Notifications
You must be signed in to change notification settings - Fork 397
/
textures.rs
1515 lines (1362 loc) · 68.3 KB
/
textures.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 std::io::Write;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum TextureType {
Regular,
Compressed,
Srgb,
CompressedSrgb,
Integral,
Unsigned,
Depth,
Stencil,
DepthStencil,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum TextureDimensions {
Texture1d,
Texture2d,
Texture2dMultisample,
Texture3d,
Texture1dArray,
Texture2dArray,
Texture2dMultisampleArray,
Cubemap,
CubemapArray,
}
impl TextureDimensions {
fn is_array(&self) -> bool {
match self {
TextureDimensions::Texture1dArray => true,
TextureDimensions::Texture2dArray => true,
TextureDimensions::Texture2dMultisampleArray => true,
TextureDimensions::CubemapArray => true,
_ => false
}
}
fn is_multisample(&self) -> bool {
match self {
TextureDimensions::Texture2dMultisample => true,
TextureDimensions::Texture2dMultisampleArray => true,
_ => false
}
}
fn is_cube(&self) -> bool {
match self {
TextureDimensions::Cubemap => true,
TextureDimensions::CubemapArray => true,
_ => false
}
}
}
pub fn build_texture_file<W: Write>(dest: &mut W) {
build_texture(dest, TextureType::Regular, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Compressed, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture1d);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture1d);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture1d);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Compressed, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture2d);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture2d);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture2d);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture2dMultisample);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Compressed, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture3d);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture3d);
//build_texture(dest, TextureType::Stencil, TextureDimensions::Texture3d); // forbidden
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture3d);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Compressed, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture1dArray);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Compressed, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture2dArray);
build_texture(dest, TextureType::Regular, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Srgb, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Integral, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Depth, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Stencil, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Texture2dMultisampleArray);
build_texture(dest, TextureType::Regular, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Compressed, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Srgb, TextureDimensions::Cubemap);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Integral, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Unsigned, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Depth, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Stencil, TextureDimensions::Cubemap);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::Cubemap);
build_texture(dest, TextureType::Regular, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Compressed, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Srgb, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::CompressedSrgb, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Integral, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Unsigned, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Depth, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::Stencil, TextureDimensions::CubemapArray);
build_texture(dest, TextureType::DepthStencil, TextureDimensions::CubemapArray);
}
fn build_texture<W: Write>(dest: &mut W, ty: TextureType, dimensions: TextureDimensions) {
// building the name of the module
let module_name: String = {
let prefix = match ty {
TextureType::Regular => "",
TextureType::Compressed => "compressed_",
TextureType::Srgb => "srgb_",
TextureType::CompressedSrgb => "compressed_srgb_",
TextureType::Integral => "integral_",
TextureType::Unsigned => "unsigned_",
TextureType::Depth => "depth_",
TextureType::Stencil => "stencil_",
TextureType::DepthStencil => "depth_stencil_",
};
let suffix = match dimensions {
TextureDimensions::Texture1d => "texture1d",
TextureDimensions::Texture2d => "texture2d",
TextureDimensions::Texture2dMultisample => "texture2d_multisample",
TextureDimensions::Texture3d => "texture3d",
TextureDimensions::Texture1dArray => "texture1d_array",
TextureDimensions::Texture2dArray => "texture2d_array",
TextureDimensions::Texture2dMultisampleArray => "texture2d_multisample_array",
TextureDimensions::Cubemap => "cubemap",
TextureDimensions::CubemapArray => "cubemap_array",
};
format!("{}{}", prefix, suffix)
};
// building the name of the texture type
let name: String = {
let prefix = match ty {
TextureType::Regular => "",
TextureType::Compressed => "Compressed",
TextureType::Srgb => "Srgb",
TextureType::CompressedSrgb => "CompressedSrgb",
TextureType::Integral => "Integral",
TextureType::Unsigned => "Unsigned",
TextureType::Depth => "Depth",
TextureType::Stencil => "Stencil",
TextureType::DepthStencil => "DepthStencil",
};
let suffix = match dimensions {
TextureDimensions::Texture1d => "Texture1d",
TextureDimensions::Texture2d => "Texture2d",
TextureDimensions::Texture2dMultisample => "Texture2dMultisample",
TextureDimensions::Texture3d => "Texture3d",
TextureDimensions::Texture1dArray => "Texture1dArray",
TextureDimensions::Texture2dArray => "Texture2dArray",
TextureDimensions::Texture2dMultisampleArray => "Texture2dMultisampleArray",
TextureDimensions::Cubemap => "Cubemap",
TextureDimensions::CubemapArray => "CubemapArray",
};
format!("{}{}", prefix, suffix)
};
// the trait corresponding to the data source
let data_source_trait = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture1dArray => "Texture1dDataSource",
TextureDimensions::Texture2d | TextureDimensions::Texture2dArray => "Texture2dDataSource",
TextureDimensions::Texture3d => "Texture3dDataSource",
TextureDimensions::Texture2dMultisample | TextureDimensions::Texture2dMultisampleArray |
TextureDimensions::Cubemap | TextureDimensions::CubemapArray => {
"unreachable"
},
};
// the format enum corresponding to this texture
let relevant_format = match ty {
TextureType::Regular => "UncompressedFloatFormat",
TextureType::Compressed => "CompressedFormat",
TextureType::Srgb => "SrgbFormat",
TextureType::CompressedSrgb => "CompressedSrgbFormat",
TextureType::Integral => "UncompressedIntFormat",
TextureType::Unsigned => "UncompressedUintFormat",
TextureType::Depth => "DepthFormat",
TextureType::Stencil => "StencilFormat",
TextureType::DepthStencil => "DepthStencilFormat",
};
// the default format to use when none is specified
let default_format = match ty {
TextureType::Compressed => "TextureFormatRequest::AnyCompressed",
TextureType::Regular => "TextureFormatRequest::AnyFloatingPoint",
TextureType::CompressedSrgb => "TextureFormatRequest::AnyCompressedSrgb",
TextureType::Srgb => "TextureFormatRequest::AnySrgb",
TextureType::Integral => "TextureFormatRequest::AnyIntegral",
TextureType::Unsigned => "TextureFormatRequest::AnyUnsigned",
TextureType::Depth => "TextureFormatRequest::AnyDepth",
TextureType::Stencil => "TextureFormatRequest::AnyStencil",
TextureType::DepthStencil => "TextureFormatRequest::AnyDepthStencil",
};
// whether this is a internally compressed texture object
let is_compressed = matches!(ty, TextureType::Compressed |
TextureType::CompressedSrgb);
let client_format_any_ty = match ty {
TextureType::Compressed => "ClientFormatAny::CompressedFormat",
TextureType::CompressedSrgb => "ClientFormatAny::CompressedSrgbFormat",
_ => "ClientFormatAny::ClientFormat",
};
let mipmaps_option_ty = match ty {
TextureType::Compressed | TextureType::CompressedSrgb => "CompressedMipmapsOption",
_ => "MipmapsOption",
};
let mipmap_default = match ty {
TextureType::Compressed | TextureType::CompressedSrgb => "CompressedMipmapsOption::NoMipmap",
TextureType::Unsigned | TextureType::Integral => "MipmapsOption::NoMipmap",
_ => "MipmapsOption::AutoGeneratedMipmaps",
};
//
let dimensions_parameters_input = match dimensions {
TextureDimensions::Texture1d => "width: u32",
TextureDimensions::Texture2d => "width: u32, height: u32",
TextureDimensions::Texture2dMultisample => "width: u32, height: u32, samples: u32",
TextureDimensions::Texture3d => "width: u32, height: u32, depth: u32",
TextureDimensions::Texture1dArray => "width: u32, array_size: u32",
TextureDimensions::Texture2dArray => "width: u32, height: u32, array_size: u32",
TextureDimensions::Texture2dMultisampleArray => "width: u32, height: u32, array_size: u32, samples: u32",
TextureDimensions::Cubemap => "dimension: u32",
TextureDimensions::CubemapArray => "dimension: u32, array_size: u32",
};
let dimensions_parameters_passing = match dimensions {
TextureDimensions::Texture1d => {
"Dimensions::Texture1d { width: width }"
},
TextureDimensions::Texture2d => {
"Dimensions::Texture2d { width: width, height: height }"
},
TextureDimensions::Texture2dMultisample => {
"Dimensions::Texture2dMultisample { width: width, height: height, samples: samples }"
},
TextureDimensions::Texture3d => {
"Dimensions::Texture3d { width: width, height: height, depth: depth }"
},
TextureDimensions::Texture1dArray => {
"Dimensions::Texture1dArray { width: width, array_size: array_size }"
},
TextureDimensions::Texture2dArray => {
"Dimensions::Texture2dArray { width: width, height: height, array_size: array_size }"
},
TextureDimensions::Texture2dMultisampleArray => {
"Dimensions::Texture2dMultisampleArray { width: width, height: height, array_size: array_size, samples: samples }"
},
TextureDimensions::Cubemap => {
"Dimensions::Cubemap { dimension: dimension }"
},
TextureDimensions::CubemapArray => {
"Dimensions::CubemapArray { dimension: dimension, array_size: array_size }"
},
};
// writing the `use module::*;` statement
writeln!(dest, "pub use self::{}::{};", module_name, name).unwrap();
// opening `mod module {`
writeln!(dest, "
/// Contains the implementation of `{}`.
pub mod {} {{\
// the list of imports we need depends on the texture type, don't bother with this
#![allow(unused_imports)]
use std::borrow::Cow;
use crate::texture::any::{{self, TextureAny, TextureAnyLayer, TextureAnyMipmap}};
use crate::texture::any::{{TextureAnyLayerMipmap, TextureAnyImage, Dimensions}};
use crate::texture::bindless::{{ResidentTexture, BindlessTexturesNotSupportedError}};
use crate::texture::get_format::{{InternalFormat, InternalFormatType, GetFormatError}};
use crate::texture::pixel_buffer::PixelBuffer;
use crate::texture::{{TextureCreationError, Texture1dDataSource, Texture2dDataSource}};
use crate::texture::{{Texture3dDataSource, Texture2dDataSink, MipmapsOption, CompressedMipmapsOption}};
use crate::texture::{{RawImage1d, RawImage2d, RawImage3d, CubeLayer}};
use crate::texture::pixel::PixelValue;
use crate::image_format::{{ClientFormatAny, TextureFormatRequest}};
use crate::image_format::{{UncompressedFloatFormat, UncompressedIntFormat}};
use crate::image_format::{{CompressedFormat, DepthFormat, DepthStencilFormat, StencilFormat}};
use crate::image_format::{{CompressedSrgbFormat, SrgbFormat, UncompressedUintFormat}};
use crate::backend::Facade;
use crate::uniforms::{{UniformValue, AsUniformValue, Sampler}};
use crate::framebuffer;
use crate::Rect;
use crate::GlObject;
use crate::TextureExt;
use crate::TextureMipmapExt;
use crate::gl;
", name, module_name).unwrap();
// writing the struct with doc-comment
(write!(dest, "/// ")).unwrap();
(write!(dest, "{}", match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture2dMultisample | TextureDimensions::Texture3d |
TextureDimensions::Cubemap => "A ",
TextureDimensions::Texture1dArray | TextureDimensions::Texture2dArray |
TextureDimensions::Texture2dMultisampleArray |
TextureDimensions::CubemapArray => "An array of ",
})).unwrap();
if is_compressed {
(write!(dest, "compressed ")).unwrap();
}
(write!(dest, "{}", match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture1dArray => "one-dimensional ",
TextureDimensions::Texture2d | TextureDimensions::Texture2dArray |
TextureDimensions::Texture2dMultisample | TextureDimensions::Texture2dMultisampleArray => {
"two-dimensional "
},
TextureDimensions::Texture3d => "three-dimensional ",
TextureDimensions::Cubemap | TextureDimensions::CubemapArray => "cube ",
})).unwrap();
(write!(dest, "{}", match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture2dMultisample | TextureDimensions::Texture3d |
TextureDimensions::Cubemap => "texture ",
TextureDimensions::Texture1dArray | TextureDimensions::Texture2dArray |
TextureDimensions::Texture2dMultisampleArray |
TextureDimensions::CubemapArray => "textures ",
})).unwrap();
(write!(dest, "{}", match ty {
TextureType::Regular | TextureType::Compressed => " containing floating-point data",
TextureType::Srgb | TextureType::CompressedSrgb => " containing sRGB floating-point data",
TextureType::Integral => " containing signed integral data",
TextureType::Unsigned => " containing unsigned integral data",
TextureType::Depth => " containing depth data",
TextureType::Stencil => " containing stencil data",
TextureType::DepthStencil => " containing both depth and stencil data",
})).unwrap();
(writeln!(dest, ".")).unwrap();
(writeln!(dest, "pub struct {}(TextureAny);", name)).unwrap();
// `GlObject` trait impl
(writeln!(dest, "
impl GlObject for {} {{
type Id = gl::types::GLuint;
#[inline]
fn get_id(&self) -> gl::types::GLuint {{
self.0.get_id()
}}
}}
", name)).unwrap();
// `Debug` trait impl
(writeln!(dest, "
impl ::std::fmt::Debug for {} {{
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error>
{{
self.0.fmt(f)
}}
}}
", name)).unwrap();
// 'Deref' impl to common type.
(writeln!(dest, "
impl ::std::ops::Deref for {} {{
type Target = TextureAny;
#[inline]
fn deref<'a>(&'a self) -> &'a TextureAny {{
&self.0
}}
}}
", name)).unwrap();
// `UniformValue` trait impl
{
match ty {
TextureType::Regular | TextureType::Compressed |
TextureType::Srgb | TextureType::CompressedSrgb |
TextureType::Integral | TextureType::Unsigned | TextureType::Depth => {
(writeln!(dest, "
impl<'a> AsUniformValue for &'a {myname} {{
#[inline]
fn as_uniform_value(&self) -> UniformValue {{
UniformValue::{myname}(*self, None)
}}
}}
impl AsUniformValue for {myname} {{
#[inline]
fn as_uniform_value(&self) -> UniformValue {{
UniformValue::{myname}(self, None)
}}
}}
impl<'a> AsUniformValue for Sampler<'a, {myname}> {{
#[inline]
fn as_uniform_value(&self) -> UniformValue {{
UniformValue::{myname}(self.0, Some(self.1))
}}
}}
impl {myname} {{
/// Builds a `Sampler` marker object that allows you to indicate
/// how the texture should be sampled from inside a shader.
///
/// # Example
///
/// ```no_run
/// # #[macro_use] extern crate glium;
/// # fn example(texture: glium::texture::Texture2d) {{
/// let uniforms = uniform! {{
/// color_texture: texture.sampled().magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest)
/// }};
/// # }}
/// ```
#[inline]
pub fn sampled(&self) -> Sampler<{myname}> {{
Sampler(self, Default::default())
}}
}}
", myname = name)).unwrap();
},
_ => ()
}
}
// `ToXXXAttachment` trait impl
if dimensions == TextureDimensions::Texture2d || dimensions == TextureDimensions::Texture2dMultisample ||
dimensions == TextureDimensions::Texture1d
{
match ty {
TextureType::Regular | TextureType::Srgb | TextureType::Integral | TextureType::Unsigned => {
(writeln!(dest, "
impl<'t> crate::framebuffer::ToColorAttachment<'t> for &'t {name} {{
#[inline]
fn to_color_attachment(self) -> crate::framebuffer::ColorAttachment<'t> {{
crate::framebuffer::ColorAttachment::Texture(self.0.main_level().first_layer().into_image(None).unwrap())
}}
}}
", name = name)).unwrap();
},
TextureType::Depth => {
(writeln!(dest, "
impl<'t> crate::framebuffer::ToDepthAttachment<'t> for &'t {name} {{
#[inline]
fn to_depth_attachment(self) -> crate::framebuffer::DepthAttachment<'t> {{
crate::framebuffer::DepthAttachment::Texture(self.0.main_level().first_layer().into_image(None).unwrap())
}}
}}
", name = name)).unwrap();
},
TextureType::Stencil => {
(writeln!(dest, "
impl<'t> crate::framebuffer::ToStencilAttachment<'t> for &'t {name} {{
#[inline]
fn to_stencil_attachment(self) -> crate::framebuffer::StencilAttachment<'t> {{
crate::framebuffer::StencilAttachment::Texture(self.0.main_level().first_layer().into_image(None).unwrap())
}}
}}
", name = name)).unwrap();
},
TextureType::DepthStencil => {
(writeln!(dest, "
impl<'t> crate::framebuffer::ToDepthStencilAttachment<'t> for &'t {name} {{
#[inline]
fn to_depth_stencil_attachment(self) -> crate::framebuffer::DepthStencilAttachment<'t> {{
crate::framebuffer::DepthStencilAttachment::Texture(self.0.main_level().first_layer().into_image(None).unwrap())
}}
}}
", name = name)).unwrap();
},
_ => ()
}
}
// opening `impl Texture` block
(writeln!(dest, "impl {} {{", name)).unwrap();
// writing `get_internal_format`
writeln!(dest, "
/// Determines the internal format of this texture.
///
/// The backend may not support querying the actual format, in which case an error
/// is returned.
#[inline]
pub fn get_internal_format(&self) -> Result<InternalFormat, GetFormatError> {{
self.0.get_internal_format()
}}
").unwrap();
// writing the `new` function
if !dimensions.is_multisample() && !dimensions.is_cube() {
let param = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture3d => "T",
TextureDimensions::Texture1dArray |
TextureDimensions::Texture2dArray => "Vec<T>",
_ => unreachable!()
};
let gen_doc = if is_compressed {
"/// No mipmap level (except for the main level) will be allocator nor generated."
} else {
"/// This function will automatically generate all mipmaps of the texture."
};
(writeln!(dest, "
/// Builds a new texture by uploading data.
///
{gen_doc}
#[inline]
pub fn new<'a, F: ?Sized, T>(facade: &F, data: {param})
-> Result<{name}, TextureCreationError>
where T: {data_source_trait}<'a>, F: Facade
{{
{name}::new_impl(facade, data, None, {mipmap_default})
}}
", data_source_trait = data_source_trait, param = param, name = name,
mipmap_default = mipmap_default, gen_doc = gen_doc)).unwrap();
}
// writing the `with_mipmaps` function
if !dimensions.is_multisample() && !dimensions.is_cube() {
let param = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture3d => "T",
TextureDimensions::Texture1dArray |
TextureDimensions::Texture2dArray => "Vec<T>",
_ => unreachable!()
};
(writeln!(dest, "
/// Builds a new texture by uploading data.
#[inline]
pub fn with_mipmaps<'a, F: ?Sized, T>(facade: &F, data: {param}, mipmaps: {mipmaps})
-> Result<{name}, TextureCreationError>
where T: {data_source_trait}<'a>, F: Facade
{{
{name}::new_impl(facade, data, None, mipmaps)
}}
", data_source_trait = data_source_trait, param = param, name = name,
mipmaps = mipmaps_option_ty)).unwrap();
}
// writing the `with_compressed_data` function
if is_compressed && !dimensions.is_multisample() && !dimensions.is_cube() {
let param = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture3d => "&[u8]",
TextureDimensions::Texture1dArray |
TextureDimensions::Texture2dArray => "Vec<&[u8]>",
_ => unreachable!()
};
(writeln!(dest, "
/// Builds a new texture with a specific format. The input data must also be of the
/// specified compressed format.
#[inline]
pub fn with_compressed_data<F: ?Sized>(facade: &F, data: {param}, {dim_params},
format: {format}, mipmaps: {mipmaps})
-> Result<{name}, TextureCreationError>
where F: Facade
{{
let data = Cow::Borrowed(data.as_ref());
let client_format = {client_format_any}(format);
Ok({name}(any::new_texture(facade, {default_format}, Some((client_format, data)),
mipmaps.into(), {dim_params_passing})?))
}}
", dim_params = dimensions_parameters_input, dim_params_passing = dimensions_parameters_passing,
param = param, client_format_any = client_format_any_ty,
name = name, format = relevant_format, default_format = default_format,
mipmaps = mipmaps_option_ty).unwrap());
}
// writing the `with_format` function
if !dimensions.is_multisample() && !dimensions.is_cube() {
let param = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture3d => "T",
TextureDimensions::Texture1dArray |
TextureDimensions::Texture2dArray => "Vec<T>",
_ => unreachable!()
};
(writeln!(dest, "
/// Builds a new texture with a specific format.
#[inline]
pub fn with_format<'a, F: ?Sized, T>(facade: &F, data: {param},
format: {format}, mipmaps: {mipmaps})
-> Result<{name}, TextureCreationError>
where T: {data_source_trait}<'a>, F: Facade
{{
{name}::new_impl(facade, data, Some(format), mipmaps)
}}
", data_source_trait = data_source_trait, param = param,
format = relevant_format, name = name,
mipmaps = mipmaps_option_ty)).unwrap();
}
// writing the `new_impl` function
if !dimensions.is_multisample() && !dimensions.is_cube() {
let param = match dimensions {
TextureDimensions::Texture1d | TextureDimensions::Texture2d |
TextureDimensions::Texture3d => "T",
TextureDimensions::Texture1dArray |
TextureDimensions::Texture2dArray => "Vec<T>",
_ => unreachable!()
};
(writeln!(dest, "
#[inline]
fn new_impl<'a, F: ?Sized, T>(facade: &F, data: {param},
format: Option<{relevant_format}>, mipmaps: {mipmaps})
-> Result<{name}, TextureCreationError>
where T: {data_source_trait}<'a>, F: Facade
{{
", data_source_trait = data_source_trait,
param = param, name = name,
relevant_format = relevant_format,
mipmaps = mipmaps_option_ty)).unwrap();
// writing the `let format = ...` line
(write!(dest, "let format = format.map(|f| {{
TextureFormatRequest::Specific(f.to_texture_format())
}}).unwrap_or({});", default_format)).unwrap();
match dimensions {
TextureDimensions::Texture1d => (write!(dest, "
let RawImage1d {{ data, width, format: client_format }} = data.into_raw();
")).unwrap(),
TextureDimensions::Texture2d => (write!(dest, "
let RawImage2d {{ data, width, height, format: client_format }} =
data.into_raw();
")).unwrap(),
TextureDimensions::Texture3d => (write!(dest, "
let RawImage3d {{ data, width, height, depth, format: client_format }} =
data.into_raw();
")).unwrap(),
TextureDimensions::Texture1dArray => (write!(dest, "
let vec_raw = data.into_iter().map(|e| e.into_raw()).collect();
let RawImage2d {{data, width, height: array_size, format: client_format }} = RawImage2d::from_vec_raw1d(&vec_raw);
")).unwrap(), // TODO: panic if dimensions are inconsistent
TextureDimensions::Texture2dArray => (write!(dest, "
let vec_raw = data.into_iter().map(|e| e.into_raw()).collect();
let RawImage3d {{data, width, height, depth: array_size, format: client_format }} = RawImage3d::from_vec_raw2d(&vec_raw);
")).unwrap(), // TODO: panic if dimensions are inconsistent
_ => unreachable!()
}
(write!(dest, "let client_format = ClientFormatAny::ClientFormat(client_format);")).unwrap();
// writing the constructor
(write!(dest, "Ok({}(any::new_texture(facade, format, \
Some((client_format, data)), mipmaps.into(), {}", name, dimensions_parameters_passing)).unwrap();
(writeln!(dest, ")?))")).unwrap();
// end of "new" function block
(writeln!(dest, "}}")).unwrap();
}
// writing the `empty` function
if !is_compressed {
// opening function
(writeln!(dest, "
/// Creates an empty texture.
///
/// No mipmap level (except for the main level) will be allocated or generated.
///
/// The texture will contain undefined data.
#[inline]
pub fn empty<F: ?Sized>(facade: &F, {dim_params})
-> Result<{name}, TextureCreationError>
where F: Facade
{{
let format = {format};
", format = default_format, dim_params = dimensions_parameters_input, name = name,
)).unwrap();
// writing the constructor
(write!(dest, "any::new_texture::<_, u8>(facade, format, None, {mipmap}::NoMipmap.into(), {}).map(|t| {}(t))",
dimensions_parameters_passing, name, mipmap = mipmaps_option_ty)).unwrap();
// closing function
(writeln!(dest, "}}")).unwrap();
}
// writing the `empty_with_format` function
if true {
// opening function
(writeln!(dest, "
/// Creates an empty texture with a specific format.
///
/// The texture (and its mipmaps) will contain undefined data.
#[inline]
pub fn empty_with_format<F: ?Sized>(facade: &F, format: {format}, mipmaps: {mipmaps}, {dim_params}) -> Result<{name}, TextureCreationError> where F: Facade {{
let format = format.to_texture_format();
let format = TextureFormatRequest::Specific(format);
", format = relevant_format, dim_params = dimensions_parameters_input, name = name,
mipmaps = mipmaps_option_ty)).unwrap();
// writing the constructor
(write!(dest, "let t = any::new_texture::<_, u8>(facade, format, None, mipmaps.into(), {});", dimensions_parameters_passing)).unwrap();
(writeln!(dest, "
t.map(|t| {}(t))", name)).unwrap();
// closing function
(writeln!(dest, "}}")).unwrap();
}
// writing the `empty_with_mipmaps` function
if !is_compressed {
// opening function
(writeln!(dest, "
/// Creates an empty texture. Specifies whether is has mipmaps.
///
/// The texture (and its mipmaps) will contain undefined data.
#[inline]
pub fn empty_with_mipmaps<F: ?Sized>(facade: &F, mipmaps: {mipmaps}, {dim_params}) -> Result<{name}, TextureCreationError> where F: Facade {{
let format = {format};
", format = default_format, dim_params = dimensions_parameters_input, name = name,
mipmaps = mipmaps_option_ty)).unwrap();
// writing the constructor
(write!(dest, "any::new_texture::<_, u8>(facade, format, None, mipmaps.into(), {})", dimensions_parameters_passing)).unwrap();
(writeln!(dest, ".map(|t| {}(t))", name)).unwrap();
// closing function
(writeln!(dest, "}}")).unwrap();
}
// writing the 'from_id' function
(writeln!(dest, "
/// Builds a new texture reference from an existing, externally created OpenGL texture.
/// If `owned` is true, this reference will take ownership of the texture and be responsible
/// for cleaning it up. Otherwise, the texture must be cleaned up externally, but only
/// after this reference's lifetime has ended.
pub unsafe fn from_id<F: Facade + ?Sized>(facade: &F,
format: {format},
id: gl::types::GLuint,
owned: bool,
mipmap: MipmapsOption,
ty: Dimensions)
-> {name} {{
let format = format.to_texture_format();
let format = TextureFormatRequest::Specific(format);
{name}(any::from_id(facade, format, id, owned, mipmap, ty))
}}
", format = relevant_format, name = name)).unwrap();
// dimensions getters
write_dimensions_getters(dest, dimensions, "self.0", true);
// writing the `as_surface` function
if (dimensions == TextureDimensions::Texture2d ||
dimensions == TextureDimensions::Texture2dMultisample) &&
(ty == TextureType::Regular ||
ty == TextureType::Integral ||
ty == TextureType::Unsigned)
{
(write!(dest, "
/// Starts drawing on the texture.
///
/// All the function calls to the framebuffer will draw on the texture instead
/// of the screen.
///
/// ## Low-level information
///
/// The first time that this function is called, a FrameBuffer Object will be
/// created and cached. The following calls to `as_surface` will load the existing
/// FBO and re-use it. When the texture is destroyed, the FBO is destroyed too.
///
#[inline]
pub fn as_surface<'a>(&'a self) -> framebuffer::SimpleFrameBuffer<'a> {{
framebuffer::SimpleFrameBuffer::new(self.0.get_context(), self).unwrap()
}}
")).unwrap();
}
// writing the `get_mipmap_levels` function
(write!(dest, "
/// Returns the number of mipmap levels of the texture.
///
/// The minimum value is 1, since there is always a main texture.
#[inline]
pub fn get_mipmap_levels(&self) -> u32 {{
self.0.get_mipmap_levels()
}}
")).unwrap();
// writing the `read` functions
// TODO: implement for other types too
if dimensions == TextureDimensions::Texture2d &&
(ty == TextureType::Regular || ty == TextureType::Srgb || is_compressed)
{
(write!(dest, r#"
/// Reads the content of the texture to RAM. This method may only read `U8U8U8U8`
/// data, as it is the only format guaranteed to be supported across all OpenGL
/// versions.
///
/// You should avoid doing this at all cost during performance-critical
/// operations (for example, while you're drawing).
/// Use `read_to_pixel_buffer` instead.
#[inline]
pub fn read<T>(&self) -> T where T: Texture2dDataSink<(u8, u8, u8, u8)> {{
unsafe {{ self.unchecked_read() }}
}}
"#)).unwrap();
(write!(dest, r#"
/// Reads the content of the texture into a buffer in video memory. This method may
/// only read `U8U8U8U8` data, as it is the only format guaranteed to be supported
/// across all OpenGL versions.
///
/// This operation copies the texture's data into a buffer in video memory
/// (a pixel buffer). Contrary to the `read` function, this operation is
/// done asynchronously and doesn't need a synchronization.
#[inline]
pub fn read_to_pixel_buffer(&self) -> PixelBuffer<(u8, u8, u8, u8)> {{
unsafe {{ self.unchecked_read_to_pixel_buffer() }}
}}
"#)).unwrap();
(write!(dest, r#"
/// Unsafely reads the content of the texture to RAM in the specified pixel format.
/// It is possible that the current OpenGL context does not support the given
/// format, in which case the returned data will be invalid.
///
/// You should avoid doing this at all cost during performance-critical
/// operations (for example, while you're drawing).
/// Use `read_to_pixel_buffer` instead.
#[inline]
pub unsafe fn unchecked_read<T, P>(&self) -> T where T: Texture2dDataSink<P>, P: PixelValue {{
let rect = Rect {{ left: 0, bottom: 0, width: self.get_width(),
height: self.get_height().unwrap_or(1) }};
self.0.main_level().first_layer().into_image(None).unwrap().raw_read(&rect)
}}
"#)).unwrap();
(write!(dest, r#"
/// Unsafely reads the content of the texture into a buffer in video memory. It is
/// possible that the current OpenGL context does not support the given format, in
/// which case the returned data will be invalid.
///
/// This operation copies the texture's data into a buffer in video memory
/// (a pixel buffer). Contrary to the `read` function, this operation is
/// done asynchronously and doesn't need a synchronization.
#[inline]
pub unsafe fn unchecked_read_to_pixel_buffer<P>(&self) -> PixelBuffer<P> where P: PixelValue {{
let rect = Rect {{ left: 0, bottom: 0, width: self.get_width(),
height: self.get_height().unwrap_or(1) }};
let pb = PixelBuffer::new_empty(self.0.get_context(),
rect.width as usize * rect.height as usize);
self.0.main_level().first_layer().into_image(None).unwrap()
.raw_read_to_pixel_buffer(&rect, &pb);
pb
}}
"#)).unwrap();
}
// writing the `read_compressed_data` function
if is_compressed && !dimensions.is_array() {
(write!(dest, r#"
/// Reads the content of the texture to RAM without decompressing it before.
///
/// You should avoid doing this at all cost during performance-critical
/// operations (for example, while you're drawing).
///
/// Returns the compressed format of the texture and the compressed data, gives
/// `None` when the internal compression format is generic or unknown.
#[inline]
pub fn read_compressed_data(&self) -> Option<({format}, Vec<u8>)> {{
self.main_level().read_compressed_data()
}}
"#, format = relevant_format)).unwrap();
}
// writing the `write` function
// TODO: implement for other types too
if dimensions == TextureDimensions::Texture2d &&
(ty == TextureType::Regular || ty == TextureType::Srgb || is_compressed)
{
let compressed_restrictions = if is_compressed {
r#" ///
/// Calling this for compressed textures will result in a panic of type INVALID_OPERATION
/// if `Rect::bottom` or `Rect::width` is not equal to 0 (border). In addition, the contents
/// of any texel outside the region modified by such a call are undefined. These
/// restrictions may be relaxed for specific compressed internal formats whose images
/// are easily edited.
"#
} else {
""
};
(write!(dest, r#"
/// Uploads some data in the texture.
///
/// Note that this may cause a synchronization if you use the texture right before
/// or right after this call. Prefer creating a whole new texture if you change a
/// huge part of it.
///
/// ## Panic
///
/// Panics if the the dimensions of `data` don't match the `Rect`.
{compressed_restrictions}
#[inline]
pub fn write<'a, T>(&self, rect: Rect, data: T) where T: {data_source_trait}<'a> {{
self.main_level().write(rect, data)
}}
"#, data_source_trait = data_source_trait,
compressed_restrictions = compressed_restrictions)).unwrap();
}
// writing the `write_compressed_data` function
// TODO: implement for other types too
if dimensions == TextureDimensions::Texture2d && is_compressed
{
(write!(dest, r#"
/// Uploads some data in the texture by using a compressed format as input.
///
/// Note that this may cause a synchronization if you use the texture right before
/// or right after this call. Prefer creating a whole new texture if you change a
/// huge part of it.
///
/// ## Panic
///
/// Panics if the the dimensions of `data` don't match the `Rect`.
///
/// Calling this will result in a panic of type INVALID_OPERATION error if `Rect::width`
/// or `Rect::height` is not equal to 0 (border), or if the written dimensions do not match
/// the original texture dimensions. The contents of any texel outside the region modified
/// by the call are undefined. These restrictions may be relaxed for specific compressed
/// internal formats whose images are easily edited.
#[inline]
pub fn write_compressed_data(&self, rect: Rect, data: &[u8],
width: u32, height: u32, format: {format})
-> Result<(), ()>
{{
// FIXME is having width and height as parameter redundant as rect kinda of
// already provides them?
self.main_level().write_compressed_data(rect, data, width, height, format)
}}
"#, format = relevant_format)).unwrap();
}