cleaned up new() function
This commit is contained in:
+89
-91
@@ -1,5 +1,6 @@
|
||||
use winit::{window::Window, dpi::PhysicalSize, event::WindowEvent};
|
||||
use wgpu::util::DeviceExt;
|
||||
use futures::executor::block_on;
|
||||
|
||||
use crate::vertex::Vertex;
|
||||
use crate::texture::Texture;
|
||||
@@ -31,35 +32,8 @@ pub struct App {
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
|
||||
// Select backend
|
||||
let backend = wgpu::BackendBit::PRIMARY;
|
||||
println!("Backend: {:?}", backend);
|
||||
|
||||
// Create surface and pick adapter (physical gpu)
|
||||
let instance = wgpu::Instance::new(backend);
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
let adapter = instance.request_adapter(
|
||||
&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
compatible_surface: Some(&surface),
|
||||
},
|
||||
).await.unwrap();
|
||||
println!("Adapter: {}", adapter.get_info().name);
|
||||
|
||||
// Pick device (logical gpu) from adapter
|
||||
let (device, queue) = adapter.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
features: wgpu::Features::empty(),
|
||||
limits: wgpu::Limits::default(),
|
||||
label: None,
|
||||
},
|
||||
None, // Trace path
|
||||
).await.unwrap();
|
||||
|
||||
// Build descriptor and create swap chain
|
||||
let size = window.inner_size();
|
||||
fn create_swapchain(adapter: &wgpu::Adapter, device: &wgpu::Device, surface: &wgpu::Surface, size: PhysicalSize<u32>) -> (wgpu::SwapChainDescriptor, wgpu::SwapChain) {
|
||||
let swapchain_desc = wgpu::SwapChainDescriptor {
|
||||
usage: wgpu::TextureUsage::RENDER_ATTACHMENT, // render to screen
|
||||
format: adapter.get_swap_chain_preferred_format(&surface),
|
||||
@@ -68,10 +42,57 @@ impl App {
|
||||
present_mode: wgpu::PresentMode::Fifo,
|
||||
};
|
||||
let swapchain = device.create_swap_chain(&surface, &swapchain_desc);
|
||||
(swapchain_desc, swapchain)
|
||||
}
|
||||
|
||||
// Load image
|
||||
let tex = Texture::from_file(&device, &queue, "./assets/rick.png");
|
||||
fn create_pipeline(device: &wgpu::Device, swapchain_desc: &wgpu::SwapChainDescriptor, bind_group_layouts: &[&wgpu::BindGroupLayout]) -> wgpu::RenderPipeline{
|
||||
// Pipeline stuff
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: bind_group_layouts,
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
let vert_state = wgpu::VertexState { //vert
|
||||
module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.vert.spv")),
|
||||
entry_point: "main",
|
||||
buffers: &[Vertex::desc()],
|
||||
};
|
||||
let frag_state = wgpu::FragmentState { // frag
|
||||
module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.frag.spv")),
|
||||
entry_point: "main",
|
||||
targets: &[wgpu::ColorTargetState {
|
||||
format: swapchain_desc.format,
|
||||
alpha_blend: wgpu::BlendState::REPLACE,
|
||||
color_blend: wgpu::BlendState::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
};
|
||||
let prim_state = wgpu::PrimitiveState { // primitive
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
cull_mode: wgpu::CullMode::Back,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
};
|
||||
let multisample_state = wgpu::MultisampleState { // multisample
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
};
|
||||
|
||||
// Create pipeline
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: vert_state,
|
||||
fragment: Some(frag_state),
|
||||
primitive: prim_state,
|
||||
depth_stencil: None,
|
||||
multisample: multisample_state,
|
||||
})
|
||||
}
|
||||
|
||||
fn create_bind_group(device: &wgpu::Device, tex: &Texture) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
|
||||
let bind_group_layout = device.create_bind_group_layout(
|
||||
&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
@@ -112,74 +133,51 @@ impl App {
|
||||
resource: wgpu::BindingResource::Sampler(&tex.sampler),
|
||||
}
|
||||
],
|
||||
label: Some("diffuse_bind_group"),
|
||||
label: None,
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Pipeline stuff
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Render Pipeline Layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
let vert_state = wgpu::VertexState { //vert
|
||||
module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.vert.spv")),
|
||||
entry_point: "main",
|
||||
buffers: &[Vertex::desc()],
|
||||
};
|
||||
let frag_state = wgpu::FragmentState { // frag
|
||||
module: &device.create_shader_module(&wgpu::include_spirv!(".bin\\shader.frag.spv")),
|
||||
entry_point: "main",
|
||||
targets: &[wgpu::ColorTargetState {
|
||||
format: swapchain_desc.format,
|
||||
alpha_blend: wgpu::BlendState::REPLACE,
|
||||
color_blend: wgpu::BlendState::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
};
|
||||
let prim_state = wgpu::PrimitiveState { // primitive
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
cull_mode: wgpu::CullMode::Back,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
};
|
||||
let multisample_state = wgpu::MultisampleState { // multisample
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
};
|
||||
(bind_group_layout, bind_group)
|
||||
}
|
||||
|
||||
// Create pipeline
|
||||
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render Pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: vert_state,
|
||||
fragment: Some(frag_state),
|
||||
primitive: prim_state,
|
||||
depth_stencil: None,
|
||||
multisample: multisample_state,
|
||||
});
|
||||
|
||||
// Vertex buffer
|
||||
let vertex_buffer = device.create_buffer_init(
|
||||
fn create_buffer(device: &wgpu::Device, usage: wgpu::BufferUsage, content: &[u8]) -> wgpu::Buffer {
|
||||
device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Vertex Buffer"),
|
||||
contents: bytemuck::cast_slice(VERTICES),
|
||||
usage: wgpu::BufferUsage::VERTEX,
|
||||
label: None,
|
||||
contents: content,
|
||||
usage: usage,
|
||||
}
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
// Index buffer
|
||||
let index_buffer = device.create_buffer_init(
|
||||
&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Index Buffer"),
|
||||
contents: bytemuck::cast_slice(INDICES),
|
||||
usage: wgpu::BufferUsage::INDEX,
|
||||
}
|
||||
);
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
|
||||
// Create surface and pick device
|
||||
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
|
||||
// Pick adapter (physical gpu)
|
||||
let adapter_options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, compatible_surface: Some(&surface) };
|
||||
let adapter = instance.request_adapter(&adapter_options).await.unwrap();
|
||||
println!("Adapter: {}", adapter.get_info().name);
|
||||
|
||||
// Pick device (logical gpu) from adapter
|
||||
let device_desc = wgpu::DeviceDescriptor { features: wgpu::Features::empty(), limits: wgpu::Limits::default(), label: None };
|
||||
let (device, queue) = adapter.request_device(&device_desc, None).await.unwrap();
|
||||
|
||||
// Create swapchain
|
||||
let (swapchain_desc, swapchain) = Self::create_swapchain(&adapter, &device, &surface, window.inner_size());
|
||||
|
||||
// Load image
|
||||
let tex = Texture::from_file(&device, &queue, "./assets/rick.png");
|
||||
let (bind_group_layout, bind_group) = Self::create_bind_group(&device, &tex);
|
||||
|
||||
// Pipeline
|
||||
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&bind_group_layout]);
|
||||
|
||||
// Buffers
|
||||
let vertex_buffer = Self::create_buffer(&device, wgpu::BufferUsage::VERTEX, bytemuck::cast_slice(VERTICES));
|
||||
let index_buffer = Self::create_buffer(&device, wgpu::BufferUsage::INDEX, bytemuck::cast_slice(INDICES));
|
||||
|
||||
// Save values in app
|
||||
println!("Initialized");
|
||||
|
||||
Reference in New Issue
Block a user