Files
ab_glyph_rasterizer
addr2line
adler
andrew
approx
arrayvec
ash
atom
backtrace
bitflags
byteorder
calloop
cfg_if
colorful
conrod_core
conrod_derive
conrod_example_shared
conrod_gfx
conrod_glium
conrod_piston
conrod_rendy
conrod_vulkano
conrod_wgpu
conrod_winit
copyless
copypasta
crossbeam
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_queue
crossbeam_utils
daggy
dlib
downcast_rs
draw_state
either
fixedbitset
float
fnv
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
fxhash
getrandom
gfx
gfx_backend_empty
gfx_backend_vulkan
gfx_core
gfx_descriptor
gfx_hal
gfx_memory
gimli
glium
glutin
glutin_egl_sys
glutin_glx_sys
graphics
half
hibitset
inplace_it
input
instant
interpolation
iovec
itoa
lazy_static
lazycell
libc
libloading
line_drawing
linked_hash_map
lock_api
log
maybe_uninit
memchr
memmap
memoffset
miniz_oxide
mio
mio_extras
naga
net2
nix
nom
num
num_bigint
num_complex
num_cpus
num_integer
num_iter
num_rational
num_traits
object
once_cell
ordered_float
ordermap
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
petgraph
pin_project
pin_project_internal
pin_project_lite
pin_utils
ppv_lite86
proc_macro2
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
raw_window_handle
read_color
relevant
rendy
rendy_chain
rendy_command
rendy_core
rendy_descriptor
rendy_factory
rendy_frame
rendy_graph
rendy_init
rendy_memory
rendy_mesh
rendy_resource
rendy_shader
rendy_texture
rendy_wsi
rustc_demangle
rustc_hash
rusttype
ryu
same_file
scoped_tls
scopeguard
serde
serde_derive
serde_json
shaderc
shaderc_sys
shared_library
slab
smallvec
smithay_client_toolkit
smithay_clipboard
spirv_headers
stb_truetype
syn
takeable_option
texture
thiserror
thiserror_impl
thread_profiler
time
tracing
tracing_core
ttf_parser
typed_arena
unicode_xid
vecmath
viewport
vk_sys
void
vulkano
buffer
command_buffer
descriptor
device
framebuffer
image
instance
memory
pipeline
query
swapchain
sync
vulkano_shaders
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
wgpu
wgpu_core
wgpu_types
winit
x11
x11_clipboard
x11_dl
xcb
xcursor
xdg
xml
  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
// Copyright 2016 The Gfx-rs Developers.
//
// 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.

//! Universal format specification.
//! Applicable to textures, views, and vertex buffers.

//TODO:
//  DXT 1-5, BC7
//  ETC2_RGB, // Use the EXT2 algorithm on 3 components.
//  ETC2_SRGB, // Use the EXT2 algorithm on 4 components (RGBA) in the sRGB color space.
//  ETC2_EAC_RGBA8, // Use the EXT2 EAC algorithm on 4 components.
use memory::Pod;

macro_rules! impl_channel_type {
    { $($name:ident = $shader_type:ident [ $($imp_trait:ident),* ] ,)* } => {
        /// Type of a surface channel. This is how we interpret the
        /// storage allocated with `SurfaceType`.
        #[allow(missing_docs)]
        #[repr(u8)]
        #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
        #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
        pub enum ChannelType {
            $( $name, )*
        }
        $(
            #[allow(missing_docs)]
            #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
            #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
            pub enum $name {}
            impl ChannelTyped for $name {
                type ShaderType = $shader_type;
                fn get_channel_type() -> ChannelType {
                    ChannelType::$name
                }
            }
            $(
                impl $imp_trait for $name {}
            )*
        )*
    }
}

impl_channel_type! {
    Int     = i32 [TextureChannel, RenderChannel],
    Uint    = u32 [TextureChannel, RenderChannel],
    Inorm   = f32 [TextureChannel, RenderChannel, BlendChannel],
    Unorm   = f32 [TextureChannel, RenderChannel, BlendChannel],
    Float   = f32 [TextureChannel, RenderChannel, BlendChannel],
    Srgb    = f32 [TextureChannel, RenderChannel, BlendChannel],
}

macro_rules! impl_formats {
    { $($name:ident : $container:ident < $($channel:ident),* > = $data_type:ty {$alpha_bits:expr} [ $($imp_trait:ident),* ], doc = $doc:expr ,)* } => {
        /// Type of the allocated texture surface. It is supposed to only
        /// carry information about the number of bits per each channel.
        /// The actual types are up to the views to decide and interpret.
        /// The actual components are up to the swizzle to define.
        #[repr(u8)]
        #[allow(missing_docs, non_camel_case_types)]
        #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
        #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
        pub enum SurfaceType {
            $( #[doc = $doc] $name, )*
        }
        impl SurfaceType {
            /// Return the total number of bits for this format.
            pub fn get_total_bits(&self) -> u8 {
                use std::mem::size_of;
                match *self {
                    $( SurfaceType::$name => (size_of::<$data_type>() * 8) as u8, )*
                }
            }
            /// Return the number of bits allocated for alpha and stencil.
            pub fn get_alpha_stencil_bits(&self) -> u8  {
                match *self {
                    $( SurfaceType::$name => $alpha_bits, )*
                }
            }
        }
        $(
            #[allow(missing_docs, non_camel_case_types)]
            #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
            #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
            #[doc = $doc]
            pub enum $name {}
            impl SurfaceTyped for $name {
                type DataType = $data_type;
                fn get_surface_type() -> SurfaceType {
                    SurfaceType::$name
                }
            }
            $(
                impl $imp_trait for $name {}
            )*
            $(
                impl Formatted for ($name, $channel) {
                    type Surface = $name;
                    type Channel = $channel;
                    type View = $container< <$channel as ChannelTyped>::ShaderType >;
                }
            )*
        )*
    }
}


impl_formats! {
    R4_G4           : Vec2<Unorm> = u8 {0}  [TextureSurface, RenderSurface], doc = "",
    R4_G4_B4_A4     : Vec4<Unorm> = u16 {4} [TextureSurface, RenderSurface], doc = "",
    R5_G5_B5_A1     : Vec4<Unorm> = u16 {1} [TextureSurface, RenderSurface], doc = "",
    R5_G6_B5        : Vec3<Unorm> = u16 {0} [TextureSurface, RenderSurface], doc = "",
    R8              : Vec1<Int, Uint, Inorm, Unorm> = u8 {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R8_G8           : Vec2<Int, Uint, Inorm, Unorm> = [u8; 2] {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R8_G8_B8_A8     : Vec4<Int, Uint, Inorm, Unorm, Srgb> = [u8; 4] {8}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R10_G10_B10_A2  : Vec4<Uint, Unorm> = u32 {2}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R11_G11_B10     : Vec4<Unorm, Float> = u32 {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R16             : Vec1<Int, Uint, Inorm, Unorm, Float> = u16 {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R16_G16         : Vec2<Int, Uint, Inorm, Unorm, Float> = [u16; 2] {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R16_G16_B16     : Vec3<Int, Uint, Inorm, Unorm, Float> = [u16; 3] {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R16_G16_B16_A16 : Vec4<Int, Uint, Inorm, Unorm, Float> = [u16; 4] {16}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R32             : Vec1<Int, Uint, Float> = u32 {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R32_G32         : Vec2<Int, Uint, Float> = [u32; 2] {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R32_G32_B32     : Vec3<Int, Uint, Float> = [u32; 3] {0}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    R32_G32_B32_A32 : Vec4<Int, Uint, Float> = [u32; 4] {32}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    B8_G8_R8_A8     : Vec4<Unorm, Srgb> = [u8; 4] {32}
        [BufferSurface, TextureSurface, RenderSurface], doc = "",
    D16             : Vec1<Unorm> = F16 {0} [TextureSurface, DepthSurface], doc = "",
    D24             : Vec1<Unorm> = f32 {8} [TextureSurface, DepthSurface], doc = "", //hacky stencil bits
    D24_S8          : Vec1<Unorm, Uint> = u32 {8} [TextureSurface, DepthSurface, StencilSurface], doc = "",
    D32             : Vec1<Float> = f32 {0} [TextureSurface, DepthSurface], doc = "",
    //D32_S8          : Vec1<Unorm, Float, Uint> = (f32, u32) {32} [TextureSurface, DepthSurface, StencilSurface],

    BC1_R8_G8_B8   : Vec4<Int, Uint, Inorm, Unorm, Srgb> = [u8; 3] {0} [TextureSurface],
        doc = "Block Compression 1 also known as DXT1, S3TC. See \
               [S3TC wiki](https://wikipedia.org/wiki/S3_Texture_Compression).\
               \n\nCurrently supported in the gfx_device_gl backend only.",
    BC3_R8_G8_B8_A8 : Vec4<Int, Uint, Inorm, Unorm, Srgb> = [u8; 4] {8} [TextureSurface],
        doc = "Block Compression 3 also known as DXT5, S3TC. See \
               [S3TC wiki](https://wikipedia.org/wiki/S3_Texture_Compression).\
               \n\nCurrently supported in the gfx_device_gl backend only.",
}


/// Source channel in a swizzle configuration. Some may redirect onto
/// different physical channels, some may be hardcoded to 0 or 1.
#[allow(missing_docs)]
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
pub enum ChannelSource {
    Zero,
    One,
    X,
    Y,
    Z,
    W,
}

/// Channel swizzle configuration for the resource views.
/// Note: It's not currently mirrored at compile-time,
/// thus providing less safety and convenience.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
pub struct Swizzle(pub ChannelSource, pub ChannelSource, pub ChannelSource, pub ChannelSource);

impl Swizzle {
    /// Create a new swizzle where each channel is unmapped.
    pub fn new() -> Swizzle {
        Swizzle(ChannelSource::X, ChannelSource::Y, ChannelSource::Z, ChannelSource::W)
    }
}

/// Complete run-time surface format.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
pub struct Format(pub SurfaceType, pub ChannelType);

/// Compile-time surface type trait.
pub trait SurfaceTyped {
    /// The corresponding data type to be passed from CPU.
    type DataType: Pod;
    /// Return the run-time value of the type.
    fn get_surface_type() -> SurfaceType;
}
/// An ability of a surface type to be used for vertex buffers.
pub trait BufferSurface: SurfaceTyped {}
/// An ability of a surface type to be used for textures.
pub trait TextureSurface: SurfaceTyped {}
/// An ability of a surface type to be used for render targets.
pub trait RenderSurface: SurfaceTyped {}
/// An ability of a surface type to be used for depth targets.
pub trait DepthSurface: SurfaceTyped {}
/// An ability of a surface type to be used for stencil targets.
pub trait StencilSurface: SurfaceTyped {}

/// Compile-time channel type trait.
pub trait ChannelTyped {
    /// Shader-visible type that corresponds to this channel.
    /// For example, normalized integers are visible as floats.
    type ShaderType;
    /// Return the run-time value of the type.
    fn get_channel_type() -> ChannelType;
}
/// An ability of a channel type to be used for textures.
pub trait TextureChannel: ChannelTyped {}
/// An ability of a channel type to be used for render targets.
pub trait RenderChannel: ChannelTyped {}
/// An ability of a channel type to be used for blended render targets.
pub trait BlendChannel: RenderChannel {}

/// Compile-time full format trait.
pub trait Formatted {
    /// Associated surface type.
    type Surface: SurfaceTyped;
    /// Associated channel type.
    type Channel: ChannelTyped;
    /// Shader view type of this format.
    type View;
    /// Return the run-time value of the type.
    fn get_format() -> Format {
        Format(
            Self::Surface::get_surface_type(),
            Self::Channel::get_channel_type())
    }
}
/// Ability to be used for vertex buffers.
pub trait BufferFormat: Formatted {}
/// Ability to be used for depth targets.
pub trait DepthFormat: Formatted {}
/// Ability to be used for vertex buffers.
pub trait StencilFormat: Formatted {}
/// Ability to be used for depth+stencil targets.
pub trait DepthStencilFormat: DepthFormat + StencilFormat {}
/// Ability to be used for textures.
pub trait TextureFormat: Formatted {}
/// Ability to be used for render targets.
pub trait RenderFormat: Formatted {}
/// Ability to be used for blended render targets.
pub trait BlendFormat: RenderFormat {}

impl<F> BufferFormat for F where
    F: Formatted,
    F::Surface: BufferSurface,
    F::Channel: ChannelTyped,
{}
impl<F> DepthFormat for F where
    F: Formatted,
    F::Surface: DepthSurface,
    F::Channel: RenderChannel,
{}
impl<F> StencilFormat for F where
    F: Formatted,
    F::Surface: StencilSurface,
    F::Channel: RenderChannel,
{}
impl<F> DepthStencilFormat for F where
    F: DepthFormat + StencilFormat
{}
impl<F> TextureFormat for F where
    F: Formatted,
    F::Surface: TextureSurface,
    F::Channel: TextureChannel,
{}
impl<F> RenderFormat for F where
    F: Formatted,
    F::Surface: RenderSurface,
    F::Channel: RenderChannel,
{}
impl<F> BlendFormat for F where
    F: Formatted,
    F::Surface: RenderSurface,
    F::Channel: BlendChannel,
{}

macro_rules! alias {
    { $( $name:ident = $ty:ty, )* } => {
        $(
            #[allow(missing_docs)]
            #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
            #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
            pub struct $name(pub $ty);
            impl From<$ty> for $name {
                fn from(v: $ty) -> $name {
                    $name(v)
                }
            }

            unsafe impl Pod for $name {}

            impl $name {
                /// Convert a 2-element slice.
                pub fn cast2(v: [$ty; 2]) -> [$name; 2] {
                    [$name(v[0]), $name(v[1])]
                }
                /// Convert a 3-element slice.
                pub fn cast3(v: [$ty; 3]) -> [$name; 3] {
                    [$name(v[0]), $name(v[1]), $name(v[2])]
                }
                /// Convert a 4-element slice.
                pub fn cast4(v: [$ty; 4]) -> [$name; 4] {
                    [$name(v[0]), $name(v[1]), $name(v[2]), $name(v[3])]
                }
                /// Convert a generic slice by transmutation.
                pub fn cast_slice(slice: &[$ty]) -> &[$name] {
                    use std::mem::transmute;
                    unsafe { transmute(slice) }
                }
            }
        )*
    }
}

alias! {
    U8Norm = u8,
    I8Norm = i8,
    U16Norm = u16,
    I16Norm = i16,
    F16 = u16, // half-float
}

/// Abstracted 1-element container for macro internal use
pub type Vec1<T> = T;
/// Abstracted 2-element container for macro internal use
pub type Vec2<T> = [T; 2];
/// Abstracted 3-element container for macro internal use
pub type Vec3<T> = [T; 3];
/// Abstracted 4-element container for macro internal use
pub type Vec4<T> = [T; 4];

/// Standard 8bits RGBA format.
pub type Rgba8 = (R8_G8_B8_A8, Unorm);
/// Standard 8bit gamma transforming RGB format.
pub type Srgba8 = (R8_G8_B8_A8, Srgb);
/// Standard HDR floating-point format with 10 bits for RGB components
/// and 2 bits for the alpha.
pub type Rgb10a2F = (R10_G10_B10_A2, Float);
/// Standard 16-bit floating-point RGBA format.
pub type Rgba16F = (R16_G16_B16_A16, Float);
/// Standard 32-bit floating-point RGBA format.
pub type Rgba32F = (R32_G32_B32_A32, Float);
/// Standard 8bits BGRA format.
pub type Bgra8 = (B8_G8_R8_A8, Unorm);
/// Standard 24-bit depth format.
pub type Depth = (D24, Unorm);
/// Standard 24-bit depth format with 8-bit stencil.
pub type DepthStencil = (D24_S8, Unorm);
/// Standard 32-bit floating-point depth format.
pub type Depth32F = (D32, Float);

macro_rules! impl_simple_formats {
    { $( $container:ident< $ty:ty > = $channel:ident $surface:ident, )* } => {
        $(
            impl Formatted for $container<$ty> {
                type Surface = $surface;
                type Channel = $channel;
                type View = $container<<$channel as ChannelTyped>::ShaderType>;
            }
        )*
    }
}

macro_rules! impl_formats_8bit {
    { $( $ty:ty = $channel:ident, )* } => {
        impl_simple_formats! {$(
            Vec1<$ty> = $channel R8,
            Vec2<$ty> = $channel R8_G8,
            Vec4<$ty> = $channel R8_G8_B8_A8,
        )*}
    }
}

macro_rules! impl_formats_16bit {
    { $( $ty:ty = $channel:ident, )* } => {
        impl_simple_formats! {$(
            Vec1<$ty> = $channel R16,
            Vec2<$ty> = $channel R16_G16,
            Vec3<$ty> = $channel R16_G16_B16,
            Vec4<$ty> = $channel R16_G16_B16_A16,
        )*}
    }
}

macro_rules! impl_formats_32bit {
    { $( $ty:ty = $channel:ident, )* } => {
        impl_simple_formats! {$(
            Vec1<$ty> = $channel R32,
            Vec2<$ty> = $channel R32_G32,
            Vec3<$ty> = $channel R32_G32_B32,
            Vec4<$ty> = $channel R32_G32_B32_A32,
        )*}
    }
}

impl_formats_8bit! {
    u8 = Uint,
    i8 = Int,
    U8Norm = Unorm,
    I8Norm = Inorm,
}

impl_formats_16bit! {
    u16 = Uint,
    i16 = Int,
    U16Norm = Unorm,
    I16Norm = Inorm,
    F16 = Float,
}

impl_formats_32bit! {
    u32 = Uint,
    i32 = Int,
    f32 = Float,
}