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
// MIT License
// Copyright (c) 2022 BrindilleDeLaForet
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! ## Description
//!
//! wrld is a easy, fast, and more secure way of writing buffer descriptor for wgpu renderpipeline
//!
//! ## How it works ?
//!
//! Wrld take derived structure and crate a VertexBufferLayout according to the attribute passed.
//!
//! Wrld come with 3 macros :
//! - Desc
//! - DescInstance
//! - BufferData
//!
//! ### Desc
//!
//! Desc derive macro allow to create a VertexBufferLayout from a structure.
//!
//! #### Example
//! ```
//! #[repr(C)]
//! #[derive(wrld::Desc)]
//! struct Vertex {
//! #[f32x2(0)] position: [f32; 2],
//! #[f32x4(1)] color: [f32; 4]
//! }
//! ```
//!
//! #### Thing to know
//! - Desc will not handle data transformation. (bytemuck slice)
//! - Desc does not handle chaotic structure.
//!
//! ### DescInstance
//!
//! DescInstance is the same as the Desc macro. The only difference with DescInstance is that it change the vertex step mode but the result is the same.
//!
//! #### Example
//! ```
//! #[derive(wrld::DescInstance)]
//! struct Vertex {
//! #[f32x2(0)] position: [f32; 2],
//! #[f32x4(1)] color: [f32; 4]
//! }
//! ```
//!
//! ### Chaotic and ordered structure.
//!
//! Before aboarding the next macro. We need to know what is the difference between chaotic and ordered structure type.
//!
//! ### Chaotic structure
//!
//! A chaotic structure is a structure that have attributes put anywhere in the struct
//!
//! #### Example
//! ```
//! #[derive(wrld::Desc)]
//! struct Vertex {
//! #[f32x2(0)] position: [f32; 2],
//! data: String,
//! #[f32x4(1)] color: [f32; 4]
//! }
//! ```
//! If you try to cast slice with bytemuck on this structure. It will result in this.
//! ```
//! struct Vertex {
//! position: [f32; 2],
//! data: String
//! }
//! ```
//! And this is not good, because this is not the data that we are expecting to get from bytemuck.
//!
//! A simple fix to that is to create a ordered structure and have one or multiple function that convert this structure to a ordered one or to sort this one.
//!
//! ### Ordered structure
//!
//! A ordered structure is a structure that put the attribute field on top of the structure.
//!
//! #### Example
//! ```
//! #[derive(wrld::Desc)]
//! struct Vertex {
//! #[f32x2(0)] position: [f32; 2],
//! #[f32x4(1)] color: [f32; 4],
//! data: String
//! }
//! ```
//! If you try to cast slice with bytemuck on this structure. It will result in this.
//! ```
//! struct Vertex {
//! position: [f32; 2],
//! color: [f32; 4]
//! }
//! ```
//!
//! While this technique work. It could be annoying to rewrite thousand and thousand of structure just to fix this.
//! This is why the next macro was created for.
//!
//! ### BufferData
//!
//! BufferData create a ordered structure from a chaotic structure. It come with bytemuck derive macro out of the box.
//!
//! #### Example
//! ```
//! #[repr(C)]
//! #[derive(wrld::Desc, wrld::BufferData)]
//! struct Vertex {
//! uv: [f32; 2],
//! #[f32x2(0)] position: [f32; 2],
//! data: String,
//! #[f32x4(1)] color: [f32; 4]
//! }
//! ```
use ;
/// Desc is a proc derive macro that allow you to describe a structure as a description to pass to a renderpipeline.
///
/// ## Example
/// ```
/// use wrld::Desc;
///
/// #[repr(C)]
/// #[derive(Desc)]
/// struct Test {
/// #[f32x3(0)] position: Vector3
/// #[f32x4(1)] color: Vector4
/// }
/// ```
/// into
/// ```
/// impl Test {
/// pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
/// // let size_f32 = size_of::<f32>() = 4
/// // let f32x3 = size_f32 * 3 = 12;
/// // let f32x4 = size_f32 * 4 = 16;
/// // let array_stride = 12 + 16 = 28;
///
/// wgpu::VertexBufferLayout {
/// array_stride: 28 as wgpu::BufferAddress // array_stride variable,
/// step_mode: wgpu::VertexStepMode::Vertex,
/// attributes: &[
/// wgpu::VertexAttribute {
/// offset: 0u64,
/// format: wgpu::VertexFormat::Float32x3,
/// shader_location: 0u32,
/// },
/// wgpu::VertexAttribute {
/// offset: 12u64,
/// format: wgpu::VertexFormat::Float32x4,
/// shader_location: 1u32,
/// },
/// ],
/// }
/// }
/// }
/// ```
///
/// ## Matrice attributes
///
/// Matrices attributes are kind of special, because matrices are the only attributes that can take multiple location.
///
/// Matrices need two argument :
/// - The type of the matrice (u8, f32, f64, ect...)
/// - And the starting location
///
/// Matrices dimension start from 2x2 to 4x4
///
/// ### Example
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc)]
/// struct Actor {
/// #[mat4x2(u8, 0)] transform: [[f32; 4]; 4]
/// }
/// ```
/// Will result to
/// ```
/// impl Actor {
/// pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
/// wgpu::VertexBufferLayout {
/// array_stride: 8u64 as wgpu::BufferAddress,
/// step_mode: wgpu::VertexStepMode::Instance,
/// attributes: &[
/// wgpu::VertexAttribute {
/// offset: 0u64,
/// format: wgpu::VertexFormat::Uint8x2,
/// shader_location: 0u32,
/// },
/// wgpu::VertexAttribute {
/// offset: 2u64,
/// format: wgpu::VertexFormat::Uint8x2,
/// shader_location: 1u32,
/// },
/// wgpu::VertexAttribute {
/// offset: 4u64,
/// format: wgpu::VertexFormat::Uint8x2,
/// shader_location: 2u32,
/// },
/// wgpu::VertexAttribute {
/// offset: 6u64,
/// format: wgpu::VertexFormat::Uint8x2,
/// shader_location: 3u32,
/// },
/// ],
/// }
/// }
/// }
/// ```
/// So take care while using it.
///
/// Also matrix type handle only wgpu VertexFormat type for row.
/// That does mean that matrix like that.
/// ```
/// #[repr(C)]
/// #[derive(wrld::DescInstance)]
/// struct Vertex {
/// #[mat4x3(u8, 0)] transform: [[f32; 4]; 4]
/// }
/// ```
/// Will throw an error :
///
/// "Matrix mat4x3 cannot be use with u8 ! Available matrix are mat4x2 or mat4x4 for u8"
///
///
/// ## Thing to know
/// - Desc will not handle data transformation
/// - Desc does not handle chaotic structure
/// DescInstance is the same as Desc. The only difference is that it change the step mode to Instance instead of Vertex
///
/// ## Example
/// ```
/// use wrld::DescInstance;
///
/// #[repr(C)]
/// #[derive(DescInstance)]
/// struct Test {
/// #[f32x3(0)] position: Vector3
/// #[f32x4(1)] color: Vector4
/// }
/// ```
/// into
/// ```
/// impl Test {
/// pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
/// // let size_f32 = size_of::<f32>() = 4
/// // let f32x3 = size_f32 * 3 = 12;
/// // let f32x4 = size_f32 * 4 = 16;
/// // let array_stride = 12 + 16 = 28;
///
/// wgpu::VertexBufferLayout {
/// array_stride: 28 as wgpu::BufferAddress // array_stride variable,
/// step_mode: wgpu::VertexStepMode::Instance,
/// attributes: &[
/// wgpu::VertexAttribute {
/// offset: 0u64,
/// format: wgpu::VertexFormat::Float32x3,
/// shader_location: 0u32,
/// },
/// wgpu::VertexAttribute {
/// offset: 12u64,
/// format: wgpu::VertexFormat::Float32x4,
/// shader_location: 1u32,
/// },
/// ],
/// }
/// }
/// }
/// ```
/// A macro to handle any type of chaotic structure.
///
/// ## What is a chaotic structure ? And what are the structure different type ?
///
/// - Chaotic structure :
///
/// structure that have attribute but the fields are not ordered (basically put everywhere and not on the top of the structure)
///
/// for example
/// ```
/// #[repr(C)]
/// #[derive(wgpu::Desc)]
/// struct Vertex {
/// some_data: String,
/// #[f32x2(0)] position: [f32; 2],
/// some_other_data: TypeDefinedByUser,
/// #[f32x4(1)] color: [f32; 4]
/// }
/// ```
///
/// is a chaotic structure because crates like bytemuck will interpret this structure like this.
///
/// ```
/// struct Vertex {
/// some_data: String,
/// position: [f32; 2]
/// }
/// ```
///
/// - Ordered structure
///
/// is a structure that does put attribute field on the top of the structure.
///
/// for example
/// ```
/// #[repr(C)]
/// #[derive(wgpu::Desc)]
/// struct Vertex {
/// #[f32x2(0)] position: [f32; 2],
/// #[f32x4(1)] color: [f32; 4],
/// some_data: String,
/// some_other_data: TypeDefinedByUser
/// }
/// ```
///
/// is a ordered structure and bytemuck will interpret this structure like this.
///
/// ```
/// struct Vertex {
/// position: [f32; 2],
/// color: [f32; 4]
/// }
/// ```
///
/// before that macro, structure like this (chaotic structure)
/// ```
/// #[repr(C)]
/// #[derive(wgpu::Desc)]
/// struct Vertex {
/// uv: [f32; 2],
/// #[f32x2(0)] position: [f32; 2],
/// data: String,
/// #[f32x4(1)] color: [f32; 4]
/// }
/// ```
/// Where not very well handled by wrld, because bytemuck will not look for attribute data.
/// Which create undefined behaviour on structure data and will not correspond to what we expect to receive.
///
/// A solution to that was to reorder structure data fields (ordered structure)
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc)]
/// struct Vertex {
/// #[f32x2(0)] position: [f32; 2],
/// #[f32x4(1)] color: [f32; 4],
///
/// uv: [f32; 4],
/// data: String
/// }
/// ```
/// But now with BufferData this is not a problem anymore.
/// BufferData handle any type of chaotic structure so that does mean that this structure for example
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc)]
/// struct Vertex {
/// uv: [f32; 4],
/// #[f32x2(0)] position: [f32; 2],
/// data: String,
/// #[f32x4(1)] color: [f32; 4]
/// }
/// ```
/// Is handled via this macro and will have the result of what we expect it from.
///
/// ## How it's working ?
///
/// BufferData create a ordered structure from a chaotic structure.
/// It take any array or variable and transform it to is correponding ordered structure
/// it also provide function and trait converter accordingly.
///
/// ## Example
///
/// Take this structure
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc, wrld::BufferData)]
/// struct Vertex {
/// texture: SomeTextureType,
/// #[f32x3(0)] position: [f32; 3],
/// message: String,
/// #[f21x3(1)] scale: [f32; 3]
/// }
/// ```
///
/// This structure will result in this implementation
///
/// ```
/// #[repr(C)]
/// #[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
/// struct VertexBufferData {
/// position: [f32; 3],
/// scale: [f32; 3]
/// }
///
/// impl From<Vertex> for VertexBufferData {
/// fn from(other_data_from_ident_to_into: Vertex) -> Self {
/// Self {
/// position: other_data_from_ident_to_into.position,
/// scale: other_data_from_ident_to_into.scale
/// }
/// }
/// }
///
/// impl From<&'static Vertex> for VertexBufferData {
/// fn from(other_data_from_ident_to_into: &'static Vertex) -> Self {
/// Self {
/// position: other_data_from_ident_to_into.position,
/// scale: other_data_from_ident_to_into.scale
/// }
/// }
/// }
///
/// impl PartialEq<Vertex> for VertexBufferData {
/// fn eq(&self, other_ident_data_boolean_condition: &Vertex) -> bool {
/// position == other_ident_data_boolean_condition.position && scale: other_ident_data_boolean_condition.scale
/// }
/// }
///
/// impl FromIterator<Vertex> for Vec<VertexBufferData> {
/// fn from_iter<T: IntoIterator<Item = Vertex>>(iter: T) -> Self {
/// let mut vec_data_from_ident_from_iterator = Vec::new();
///
/// for c in iter {
/// vec_data_from_ident_from_iterator.push(c.into());
/// }
///
/// vec_data_from_ident_from_iterator
/// }
/// }
///
/// impl FromIterator<&'static Vertex> for Vec<VertexBufferData> {
/// fn from_iter<T: IntoIterator<Item = &'static Vertex>>(iter: T) -> Self {
/// let mut vec_data_from_ident_single_from_iterator : Vec<VertexBufferData> = Vec::new();
///
/// for c in iter {
/// vec_data_from_ident_single_from_iterator.push(c.into());
/// }
///
/// vec_data_from_ident_single_from_iterator
/// }
/// }
///
/// impl VertexBufferData {
/// pub const fn const_into(other_ident_data_to_into_const: &Vertex) -> Self {
/// Self {
/// position: other_ident_data_to_into_const.position,
/// scale: other_ident_data_to_into_const.scale
/// }
/// }
/// }
///
/// impl Vertex {
/// pub fn mutate<'a>(other_data_from_ident_to_mutate: &'a Vec<VertexBufferData>) -> &'a [u8] {
/// bytemuck::cast_slice(other_data_from_ident_to_mutate.as_slice())
/// }
///
/// pub fn transmute(other_data_from_ident_to_transmute: &'static [Self]) -> Vec<VertexBufferData> {
/// other_data_from_ident_to_transmute.into_iter().collect::<Vec<VertexBufferData>>()
/// }
/// }
///
/// macro_rules! vertex_const_into {
/// ($data: expr) => {
/// VertexBufferData::const_into(&$data)
/// };
/// }
///
/// macro_rules! mutate_vertex {
/// ($data: expr) => {
/// Vertex::mutate(&Vertex::transmute($data))
/// };
/// }
/// ```
/// Also bytemuck is used for converting structure data to wgpu
///
/// ## How to use it ?
///
/// When you create any chaotic structure for wrld. Just put wrld::BufferData derive macro at the top
///
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc, wrld::BufferData)]
/// struct Vertex {
/// texture: SomeTextureType,
/// #[f32x3(0)] position: [f32; 3],
/// message: String,
/// #[f21x3(1)] scale: [f32; 3]
/// }
/// ```
///
/// ### Single variable conversion.
///
/// If you only need to convert a single variable. You can do that.
///
/// ```
/// let data : VertexBufferData = Vertex {
/// texture: SomeTextureType::new(),
/// position: [0.0, 0.0, 0.0],
/// message: String::from("something"),
/// scale: [1.0, 1.0, 1.0]
/// }.into()
/// ```
///
/// If you however want to convert a constant vertex variable.
///
/// ```
/// const data : Vertex = Vertex {
/// texture: SomeTextureType::new(),
/// position: [0.0, 0.0, 0.0],
/// message: String::from("something"),
/// scale: [1.0, 1.0, 1.0]
/// }
/// const vertex_buffer_data = VertexBufferData::const_into(&data);
/// // or
/// const vertex_buffer_data_new = vertex_const_into!(data);
/// ```
///
/// ### Array conversion
///
/// Array conversion is a little bit more complex. We can't use the .into() because rust will not allow that.
/// This is why you will need to transmute the const array first and then mutate it.
///
/// ```
/// const data : [Vertex] = [Vertex {
/// texture: SomeTextureType::new(),
/// position: [0.0, 0.0, 0.0],
/// message: String::from("something"),
/// scale: [1.0, 1.0, 1.0]
/// }, Vertex {
/// texture: SomeTextureType::new(),
/// position: [0.0, 1.0, 0.0],
/// message: String::from("something 2"),
/// scale: [1.0, 1.0, 1.0]
/// }]
///
/// fn main() {
/// let arr : &[u8] = Vertex::mutate(&Vertex::transmute(data));
/// // or
/// let arr_new : &[u8] = mutate_vertex!(data);
///
/// // With wgpu create_buffer_init
/// let device = wgpu::Device::new()
///
/// let vertex_buffer = device.create_buffer_init(
/// &wgpu::utils::BufferInitDescriptor {
/// label: Some("Buffer init"),
/// contents: Vertex::mutate(&Vertex::transmute(data)),
/// usage: wgpu::BufferUsages::VERTEX
/// })
///
/// // or
///
/// let vertex_buffer_new = device.create_buffer_init(
/// &wgpu::utils::BufferInitDescriptor {
/// label: Some("Buffer init"),
/// contents: mutate_vertex!(data),
/// usage: wgpu::BufferUsages::VERTEX
/// })
/// }
/// ```
///
/// macro name are formated like this.
/// - struct name will be all lowercase
/// - struct that have uppercase letter in his name are prefix with _ and the letter in question except for the starting letter.
///
/// ### Example
/// ```
/// #[repr(C)]
/// #[derive(wrld::Desc, wrld::BufferData)]
/// struct VertexData {
/// #[f32x2(0)] position: [f32; 2]
/// #[f32x4(1)] color: [f32; 4]
/// }
///
/// // is equal to
///
/// macro_rules! vertex_data_const_into {
/// ($data: expr) => {
/// VertexDataBufferData::const_into(&$data)
/// };
/// }
/// macro_rules! mutate_vertex_data {
/// ($data: expr) => {
/// VertexData::mutate(&VertexData::transmute($data))
/// };
/// }
/// ```
///
/// ## Why you have created a another macro instead of putting it in wrld::Desc ?
///
/// 1. Prevent wrld to be too much invasive.
/// 2. BufferData is not always needed.
/// 3. BufferData is made to handle chaotic structure and not ordered one. (related to 2.)
///
/// There is also know problem about naming const variable the same as the quote generated code variable.
/// There is a simple workaround that is to name const variable all uppercase or just change name of the const variable.
/// However this problem only occurs on const variable