summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/camera.rs57
-rw-r--r--src/instance.rs7
-rw-r--r--src/renderer.rs121
3 files changed, 90 insertions, 95 deletions
diff --git a/src/camera.rs b/src/camera.rs
index edc3405..3ae33b4 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -19,31 +19,6 @@ fn inverse_aspect_ratio(width: u32, height: u32) -> f32 {
(height as f32) / (width as f32)
}
-fn create_buffer(device: &wgpu::Device) -> wgpu::Buffer {
- device.create_buffer(&wgpu::BufferDescriptor {
- label: Some("Camera Uniform"),
- size: size_of::<CameraUniform>() as wgpu::BufferAddress,
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- mapped_at_creation: false,
- })
-}
-
-fn create_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
- device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
- label: Some("Camera Bind Group Layout"),
- entries: &[wgpu::BindGroupLayoutEntry {
- binding: 0,
- visibility: wgpu::ShaderStages::VERTEX,
- ty: wgpu::BindingType::Buffer {
- ty: wgpu::BufferBindingType::Uniform,
- has_dynamic_offset: false,
- min_binding_size: None,
- },
- count: None,
- }],
- })
-}
-
fn create_bind_group(
device: &wgpu::Device,
buffer: &wgpu::Buffer,
@@ -66,9 +41,35 @@ impl Camera {
width: u32,
height: u32,
) -> (Self, wgpu::BindGroupLayout) {
- let buffer = create_buffer(device);
- let bind_group_layout = create_bind_group_layout(device);
- let bind_group = create_bind_group(device, &buffer, &bind_group_layout);
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Camera Uniform"),
+ size: size_of::<CameraUniform>() as wgpu::BufferAddress,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+
+ let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Camera Bind Group Layout"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ });
+
+ let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Camera Bind Group"),
+ layout: &bind_group_layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ }],
+ });
(
Self {
diff --git a/src/instance.rs b/src/instance.rs
index 3479b67..bacad56 100644
--- a/src/instance.rs
+++ b/src/instance.rs
@@ -78,12 +78,7 @@ impl InstanceBuffer {
pub(crate) fn new(device: &wgpu::Device, capacity: usize) -> Self {
let instances = Vec::with_capacity(capacity);
let instance_buffer_size = instances.capacity();
- let instance_buffer = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some("Sprite Instance Buffer"),
- size: (instance_buffer_size * size_of::<Instance>()) as wgpu::BufferAddress,
- usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
- mapped_at_creation: false,
- });
+ let instance_buffer = create_buffer(device, &instances);
Self {
instances,
diff --git a/src/renderer.rs b/src/renderer.rs
index 07c7b48..544b01b 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -55,67 +55,67 @@ pub struct Renderer {
window: Window,
}
-// TODO make this more complete
-impl Renderer {
- fn get_adapter(
- instance: &wgpu::Instance,
- surface: &wgpu::Surface,
- power_preference: wgpu::PowerPreference,
- ) -> Result<wgpu::Adapter, NoGpuError> {
- let adapter = instance
- .request_adapter(&wgpu::RequestAdapterOptions {
- power_preference,
- compatible_surface: Some(surface),
- force_fallback_adapter: false,
- })
- .block_on(); // TODO this takes too long
-
- let adapter = adapter.or_else(|| {
- instance
- .enumerate_adapters(wgpu::Backends::PRIMARY)
- .find(|adapter| !surface.get_supported_formats(adapter).is_empty())
- });
+fn get_adapter(
+ instance: &wgpu::Instance,
+ surface: &wgpu::Surface,
+ power_preference: wgpu::PowerPreference,
+) -> Result<wgpu::Adapter, NoGpuError> {
+ let adapter = instance
+ .request_adapter(&wgpu::RequestAdapterOptions {
+ power_preference,
+ compatible_surface: Some(surface),
+ force_fallback_adapter: false,
+ })
+ .block_on(); // TODO this takes too long
- adapter.ok_or(NoGpuError::new())
- }
+ let adapter = adapter.or_else(|| {
+ instance
+ .enumerate_adapters(wgpu::Backends::PRIMARY)
+ .find(|adapter| !surface.get_supported_formats(adapter).is_empty())
+ });
- fn sprite_render_pipeline(
- device: &wgpu::Device,
- texture_format: wgpu::TextureFormat,
- render_pipeline_layout: &wgpu::PipelineLayout,
- ) -> wgpu::RenderPipeline {
- let shader = device.create_shader_module(include_wgsl!("../shaders/sprite.wgsl"));
-
- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
- label: Some("Sprite Render Pipeline"),
- layout: Some(render_pipeline_layout),
- // information about the vertex shader
- vertex: wgpu::VertexState {
- module: &shader,
- entry_point: "vs_main",
- buffers: &[Vertex::desc(), Instance::desc()],
- },
- // information about the fragment shader
- fragment: Some(wgpu::FragmentState {
- module: &shader,
- entry_point: "fs_main",
- targets: &[Some(wgpu::ColorTargetState {
- format: texture_format,
- blend: Some(wgpu::BlendState::ALPHA_BLENDING),
- write_mask: wgpu::ColorWrites::ALL,
- })],
- }),
- primitive: wgpu::PrimitiveState {
- // don't render the back of a sprite
- cull_mode: Some(wgpu::Face::Back),
- ..Default::default()
- },
- depth_stencil: None,
- multisample: wgpu::MultisampleState::default(),
- multiview: None,
- })
- }
+ adapter.ok_or(NoGpuError::new())
+}
+
+fn sprite_render_pipeline(
+ device: &wgpu::Device,
+ texture_format: wgpu::TextureFormat,
+ render_pipeline_layout: &wgpu::PipelineLayout,
+) -> wgpu::RenderPipeline {
+ let shader = device.create_shader_module(include_wgsl!("../shaders/sprite.wgsl"));
+
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("Sprite Render Pipeline"),
+ layout: Some(render_pipeline_layout),
+ // information about the vertex shader
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: "vs_main",
+ buffers: &[Vertex::desc(), Instance::desc()],
+ },
+ // information about the fragment shader
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: "fs_main",
+ targets: &[Some(wgpu::ColorTargetState {
+ format: texture_format,
+ blend: Some(wgpu::BlendState::ALPHA_BLENDING),
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ primitive: wgpu::PrimitiveState {
+ // don't render the back of a sprite
+ cull_mode: Some(wgpu::Face::Back),
+ ..Default::default()
+ },
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ multiview: None,
+ })
+}
+// TODO make this more complete
+impl Renderer {
/// Initializes the renderer
///
/// # Errors
@@ -149,7 +149,7 @@ impl Renderer {
let power_preference = config.power_preference();
// the adapter is the handle to the GPU
- let adapter = Self::get_adapter(&instance, &surface, power_preference)?;
+ let adapter = get_adapter(&instance, &surface, power_preference)?;
// gets a connection to the device, as well as a handle to its command queue
// the options chosen here ensure that this is guaranteed to not panic
@@ -213,7 +213,7 @@ impl Renderer {
// set up a pipeline for sprite rendering
let render_pipeline =
- Self::sprite_render_pipeline(&device, surface_config.format, &render_pipeline_layout);
+ sprite_render_pipeline(&device, surface_config.format, &render_pipeline_layout);
Ok(Self {
surface,
@@ -302,7 +302,6 @@ impl Renderer {
/// A number of problems could occur here. A timeout could occur while
/// trying to acquire the next frame. There may also be no more memory left
/// that can be used for the new frame.
- // TODO this is too big
#[profiling::function]
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
// the new texture we can render to