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
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
pub position: [f32; 3],
pub texture: [f32; 2],
}
impl Vertex {
pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::InputStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
],
}
}
}
pub mod uniform_type {
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Matrix {
pub data: [[f32; 4]; 4],
}
impl Matrix {
pub fn update(&mut self, uniform: Matrix) {
self.data = uniform.data;
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Array {
pub data: [f32; 4],
}
impl Array {
pub fn update(&mut self, uniform: Array) {
self.data = uniform.data;
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Float {
pub data: f32,
}
impl Float {
pub fn update(&mut self, uniform: Float) {
self.data = uniform.data;
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Pipeline {
pub shader_index: usize,
pub vertex_buffer_index: usize,
pub texture_index: usize,
pub uniform_index: Option<usize>,
}
pub struct VertexBuffers {
pub vertex_buffer: wgpu::Buffer,
pub index_buffer: wgpu::Buffer,
pub length: u32,
}
pub type Shaders = wgpu::RenderPipeline;
pub type UniformBuffers = wgpu::BindGroup;
pub type Textures = wgpu::BindGroup;
pub struct Renderer {
pub(crate) surface: wgpu::Surface,
pub(crate) device: wgpu::Device,
pub(crate) queue: wgpu::Queue,
pub(crate) sc_desc: wgpu::SwapChainDescriptor,
pub(crate) swap_chain: wgpu::SwapChain,
pub size: winit::dpi::PhysicalSize<u32>,
pub(crate) texture_bind_group_layout: wgpu::BindGroupLayout,
pub(crate) uniform_bind_group_layout: wgpu::BindGroupLayout,
pub(crate) shaders: Vec<Shaders>,
pub(crate) vertex_buffers: Vec<VertexBuffers>,
pub(crate) texture_bind_group: Vec<Textures>,
pub(crate) uniform_bind_group: Vec<UniformBuffers>,
pub(crate) render_pipelines: Vec<Pipeline>,
}
pub enum WindowCallbackEvents<'a> {
Before,
During(&'a winit_input_helper::WinitInputHelper),
After,
}
pub struct WindowDescriptor {
pub width: f64,
pub height: f64,
pub title: &'static str,
pub decorations: bool,
pub resizable: bool,
}
impl WindowDescriptor {
pub fn default() -> Result<Self, anyhow::Error> {
Ok(Self {
width: 800.0,
height: 600.0,
title: "Blue Engine by Blue Mazar",
decorations: true,
resizable: true,
})
}
}
pub use winit::event::MouseButton;
pub use winit::event::VirtualKeyCode as KeyboardKeys;
#[derive(Clone, Debug)]
pub enum UniformBuffer {
Matrix(&'static str, uniform_type::Matrix),
Array(&'static str, uniform_type::Array),
Float(&'static str, uniform_type::Float),
}
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0
);
pub fn normalize(value: f32, max: u32) -> f32 {
let mut result = value / max as f32;
if value == max as f32 {
result = 0.0;
} else if result < max as f32 / 2.0 {
}
if result > -1.0 {
return result as f32;
} else {
return -1.0;
}
}