diff options
| author | Micha White <botahamec@outlook.com> | 2022-10-13 21:07:54 -0400 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2022-10-13 21:07:54 -0400 |
| commit | 4574ec41259f42dceea4f770290d9975a8943db0 (patch) | |
| tree | c887840db5793f23d70e5452cb1cc56f6750e9b3 /src/renderer.rs | |
| parent | 7a7fab532c88c7ce7be5dc35f7cd70d0cb369a16 (diff) | |
Be less picky about function size
Diffstat (limited to 'src/renderer.rs')
| -rw-r--r-- | src/renderer.rs | 121 |
1 files changed, 60 insertions, 61 deletions
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 |
