Skip to content

Commit a5312f2

Browse files
committed
feat: pipeline handle rework
1 parent a196e37 commit a5312f2

File tree

13 files changed

+1038
-935
lines changed

13 files changed

+1038
-935
lines changed

src/main.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ pub mod macros;
3838
pub mod material;
3939
pub mod persistence;
4040
pub mod pipeline;
41+
pub mod pipelines;
4142
pub mod player;
4243
pub mod state;
4344
pub mod structures;
44-
pub mod ui;
4545
pub mod utils;
46-
mod water;
4746
pub mod world;
4847

4948
async fn run(event_loop: EventLoop<()>, window: Window) {
@@ -69,8 +68,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
6968

7069
event_loop
7170
.run(move |event, target| {
72-
// let _ = &(instance, &adapter, &shader, pipeline_layout);
73-
7471
if let Event::WindowEvent {
7572
window_id: _,
7673
event,
@@ -113,10 +110,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
113110
cursor_in = true;
114111
}
115112

116-
let delta = glam::vec2(
117-
prev_mouse_pos.x - position.x as f32,
118-
prev_mouse_pos.y - position.y as f32,
119-
);
120113
prev_mouse_pos.x = position.x as f32;
121114
prev_mouse_pos.y = position.y as f32;
122115

src/pipeline.rs

Lines changed: 0 additions & 286 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ use crate::{
1313
state::State,
1414
};
1515

16-
struct Matrices {
17-
view: glam::Mat4,
18-
projection: glam::Mat4,
19-
}
20-
2116
#[repr(C)]
2217
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
2318
pub struct Uniforms {
@@ -33,284 +28,3 @@ impl From<&Camera> for Uniforms {
3328
}
3429
}
3530
}
36-
37-
pub struct MainPipeline {
38-
pub projection_buffer: wgpu::Buffer,
39-
pub view_buffer: wgpu::Buffer,
40-
pub pipeline: wgpu::RenderPipeline,
41-
pub bind_group_0: wgpu::BindGroup,
42-
pub bind_group_0_layout: wgpu::BindGroupLayout,
43-
pub bind_group_1: wgpu::BindGroup,
44-
pub bind_group_1_layout: wgpu::BindGroupLayout,
45-
pub depth_texture: Texture,
46-
pub pipeline_type: PipelineType,
47-
}
48-
49-
impl MainPipeline {
50-
pub fn new(state: &State) -> Self {
51-
let swapchain_capabilities = state.surface.get_capabilities(&state.adapter);
52-
let swapchain_format = swapchain_capabilities.formats[0];
53-
54-
let shader_source = include_str!("./shaders/shader.wgsl");
55-
56-
let shader = state
57-
.device
58-
.create_shader_module(wgpu::ShaderModuleDescriptor {
59-
label: None,
60-
source: wgpu::ShaderSource::Wgsl(shader_source.into()),
61-
});
62-
let camera = &state.player.read().unwrap().camera;
63-
let uniforms = Uniforms::from(camera);
64-
65-
let projection_buffer =
66-
state
67-
.device
68-
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
69-
label: Some("projection_matrix"),
70-
contents: bytemuck::cast_slice(&[uniforms.projection]),
71-
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
72-
});
73-
74-
// View matrix
75-
let view_buffer = state
76-
.device
77-
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
78-
label: Some("projection_matrix"),
79-
contents: bytemuck::cast_slice(&[uniforms.view]),
80-
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
81-
});
82-
83-
// Constant bindgroup for chunks per row
84-
let world_chunk_per_row_buffer =
85-
state
86-
.device
87-
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
88-
contents: bytemuck::cast_slice(&[crate::world::CHUNKS_PER_ROW]),
89-
label: Some("world_chunk_per_row"),
90-
usage: wgpu::BufferUsages::UNIFORM,
91-
});
92-
93-
// Bind groups
94-
let bind_group_0_layout =
95-
state
96-
.device
97-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
98-
label: Some("bind_group_0"),
99-
entries: &[
100-
wgpu::BindGroupLayoutEntry {
101-
binding: 0,
102-
visibility: wgpu::ShaderStages::VERTEX,
103-
ty: wgpu::BindingType::Buffer {
104-
ty: wgpu::BufferBindingType::Uniform,
105-
has_dynamic_offset: false,
106-
min_binding_size: None,
107-
},
108-
count: None,
109-
},
110-
wgpu::BindGroupLayoutEntry {
111-
binding: 1,
112-
visibility: wgpu::ShaderStages::VERTEX,
113-
ty: wgpu::BindingType::Buffer {
114-
ty: wgpu::BufferBindingType::Uniform,
115-
has_dynamic_offset: false,
116-
min_binding_size: None,
117-
},
118-
count: None,
119-
},
120-
wgpu::BindGroupLayoutEntry {
121-
binding: 2,
122-
visibility: wgpu::ShaderStages::VERTEX,
123-
ty: wgpu::BindingType::Buffer {
124-
ty: wgpu::BufferBindingType::Uniform,
125-
has_dynamic_offset: false,
126-
min_binding_size: None,
127-
},
128-
count: None,
129-
},
130-
],
131-
});
132-
let bind_group_0 = state.device.create_bind_group(&wgpu::BindGroupDescriptor {
133-
layout: &bind_group_0_layout,
134-
label: None,
135-
entries: &[
136-
wgpu::BindGroupEntry {
137-
binding: 0,
138-
resource: projection_buffer.as_entire_binding(),
139-
},
140-
wgpu::BindGroupEntry {
141-
binding: 1,
142-
resource: view_buffer.as_entire_binding(),
143-
},
144-
wgpu::BindGroupEntry {
145-
binding: 2,
146-
resource: world_chunk_per_row_buffer.as_entire_binding(),
147-
},
148-
],
149-
});
150-
151-
let texture_atlas = Texture::from_path(
152-
"assets/tex_atlas.png",
153-
"tex_atlas".to_string(),
154-
&state.device,
155-
&state.queue,
156-
)
157-
.unwrap();
158-
159-
let bind_group_1_layout =
160-
state
161-
.device
162-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
163-
label: Some("bind_group_1"),
164-
entries: &[
165-
wgpu::BindGroupLayoutEntry {
166-
binding: 0,
167-
visibility: wgpu::ShaderStages::FRAGMENT,
168-
ty: wgpu::BindingType::Texture {
169-
sample_type: wgpu::TextureSampleType::Float { filterable: true },
170-
view_dimension: wgpu::TextureViewDimension::D2,
171-
multisampled: false,
172-
},
173-
count: None,
174-
},
175-
wgpu::BindGroupLayoutEntry {
176-
binding: 1,
177-
visibility: wgpu::ShaderStages::FRAGMENT,
178-
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
179-
count: None,
180-
},
181-
],
182-
});
183-
184-
let bind_group_1 = state.device.create_bind_group(&wgpu::BindGroupDescriptor {
185-
label: Some("bind_group_1"),
186-
layout: &bind_group_1_layout,
187-
entries: &[
188-
wgpu::BindGroupEntry {
189-
binding: 0,
190-
resource: wgpu::BindingResource::TextureView(&texture_atlas.view),
191-
},
192-
wgpu::BindGroupEntry {
193-
binding: 1,
194-
resource: wgpu::BindingResource::Sampler(&texture_atlas.sampler),
195-
},
196-
],
197-
});
198-
199-
// Textures
200-
let depth_texture = Texture::create_depth_texture(state);
201-
202-
// Pipeline layouts
203-
let pipeline_layout =
204-
state
205-
.device
206-
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
207-
label: None,
208-
bind_group_layouts: &[
209-
&bind_group_0_layout,
210-
&bind_group_1_layout,
211-
&state.world.chunk_data_layout,
212-
&state
213-
.player
214-
.read()
215-
.unwrap()
216-
.camera
217-
.position_bind_group_layout,
218-
],
219-
push_constant_ranges: &[],
220-
});
221-
222-
let render_pipeline =
223-
state
224-
.device
225-
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
226-
label: None,
227-
layout: Some(&pipeline_layout),
228-
vertex: wgpu::VertexState {
229-
module: &shader,
230-
entry_point: "vs_main",
231-
buffers: &[Block::get_vertex_data_layout()],
232-
},
233-
fragment: Some(wgpu::FragmentState {
234-
module: &shader,
235-
entry_point: "fs_main",
236-
targets: &[Some(swapchain_format.into())],
237-
}),
238-
239-
primitive: wgpu::PrimitiveState {
240-
polygon_mode: state.config.polygon_mode,
241-
cull_mode: Some(Face::Front),
242-
..Default::default()
243-
},
244-
depth_stencil: Some(wgpu::DepthStencilState {
245-
format: Texture::DEPTH_FORMAT,
246-
depth_write_enabled: true,
247-
depth_compare: wgpu::CompareFunction::Less,
248-
stencil: wgpu::StencilState::default(),
249-
bias: wgpu::DepthBiasState::default(),
250-
}),
251-
multisample: wgpu::MultisampleState::default(),
252-
multiview: None,
253-
});
254-
255-
Self {
256-
bind_group_0_layout,
257-
bind_group_1_layout,
258-
view_buffer,
259-
projection_buffer,
260-
pipeline_type: PipelineType::WORLD,
261-
depth_texture,
262-
bind_group_0,
263-
bind_group_1,
264-
pipeline: render_pipeline,
265-
}
266-
}
267-
}
268-
269-
impl PipelineTrait for MainPipeline {
270-
fn projection_buffer(&self) -> &Buffer {
271-
&self.projection_buffer
272-
}
273-
274-
fn pipeline(&self) -> &RenderPipeline {
275-
&self.pipeline
276-
}
277-
278-
fn view_buffer(&self) -> &Buffer {
279-
&self.view_buffer
280-
}
281-
282-
fn depth_texture(&self) -> &Texture {
283-
&self.depth_texture
284-
}
285-
fn set_depth_texture(&mut self, texture: Texture) {
286-
self.depth_texture = texture;
287-
}
288-
fn bind_group_0(&self) -> &BindGroup {
289-
&self.bind_group_0
290-
}
291-
fn bind_group_1(&self) -> &BindGroup {
292-
&self.bind_group_1
293-
}
294-
fn get_type(&self) -> PipelineType {
295-
self.pipeline_type
296-
}
297-
}
298-
299-
pub trait PipelineTrait {
300-
fn projection_buffer(&self) -> &wgpu::Buffer;
301-
fn pipeline(&self) -> &wgpu::RenderPipeline;
302-
fn view_buffer(&self) -> &wgpu::Buffer;
303-
fn bind_group_0(&self) -> &wgpu::BindGroup;
304-
fn bind_group_1(&self) -> &wgpu::BindGroup;
305-
fn depth_texture(&self) -> &Texture;
306-
fn set_depth_texture(&mut self, texture: Texture);
307-
308-
fn get_type(&self) -> PipelineType;
309-
}
310-
311-
#[derive(Debug, Clone, Copy, PartialEq)]
312-
pub enum PipelineType {
313-
WORLD,
314-
WATER,
315-
UI,
316-
}

0 commit comments

Comments
 (0)