From 9d720593e9f4829e2bb81534ef54545a61b3df00 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:21 +0100 Subject: refactor(render): give some functions less arguments --- stockton-render/src/draw/context.rs | 43 +++++++----------------- stockton-render/src/draw/draw_buffers.rs | 54 ++++++++++++++++++++++++++++++ stockton-render/src/draw/mod.rs | 1 + stockton-render/src/draw/target.rs | 10 +++--- stockton-render/src/draw/texture/chunk.rs | 30 ++++++----------- stockton-render/src/draw/texture/loader.rs | 20 +++++------ 6 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 stockton-render/src/draw/draw_buffers.rs (limited to 'stockton-render') diff --git a/stockton-render/src/draw/context.rs b/stockton-render/src/draw/context.rs index 3fff042..895ae67 100644 --- a/stockton-render/src/draw/context.rs +++ b/stockton-render/src/draw/context.rs @@ -34,8 +34,9 @@ use stockton_levels::traits::faces::FaceType; use stockton_types::{Vector2, Vector3}; use super::{ - buffer::{ModifiableBuffer, StagedBuffer}, + buffer::ModifiableBuffer, camera::WorkingCamera, + draw_buffers::{DrawBuffers, INITIAL_INDEX_SIZE, INITIAL_VERT_SIZE}, target::{SwapchainProperties, TargetChain}, texture::TextureStore, }; @@ -44,12 +45,6 @@ use crate::{error, types::*}; /// Entry point name for shaders const ENTRY_NAME: &str = "main"; -/// Initial size of vertex buffer. TODO: Way of overriding this -const INITIAL_VERT_SIZE: u64 = 3 * 3000; - -/// Initial size of index buffer. TODO: Way of overriding this -const INITIAL_INDEX_SIZE: u64 = 3000; - /// Source for vertex shader. TODO const VERTEX_SOURCE: &str = include_str!("./data/stockton.vert"); @@ -101,11 +96,8 @@ pub struct RenderingContext<'a> { /// Texture store texture_store: ManuallyDrop, - /// (Staged) Vertex Buffer - pub vert_buffer: ManuallyDrop>, - - /// (Staged) Index Buffer - pub index_buffer: ManuallyDrop>, + /// Buffers used for drawing + draw_buffers: ManuallyDrop>, /// Our camera settings camera: WorkingCamera, @@ -259,14 +251,7 @@ impl<'a> RenderingContext<'a> { let camera = WorkingCamera::defaults(ratio); // Vertex and index buffers - let (vert_buffer, index_buffer) = { - use hal::buffer::Usage; - - let vert = StagedBuffer::new(&mut device, &adapter, Usage::VERTEX, INITIAL_VERT_SIZE)?; - let index = StagedBuffer::new(&mut device, &adapter, Usage::INDEX, INITIAL_INDEX_SIZE)?; - - (vert, index) - }; + let draw_buffers = DrawBuffers::new(&mut device, &adapter)?; // Texture store let texture_store = TextureStore::new( @@ -317,8 +302,7 @@ impl<'a> RenderingContext<'a> { texture_store: ManuallyDrop::new(texture_store), - vert_buffer: ManuallyDrop::new(vert_buffer), - index_buffer: ManuallyDrop::new(index_buffer), + draw_buffers: ManuallyDrop::new(draw_buffers), vs_module: ManuallyDrop::new(vs_module), fs_module: ManuallyDrop::new(fs_module), @@ -582,8 +566,7 @@ impl<'a> RenderingContext<'a> { // Prepare command buffer let cmd_buffer = self.target_chain.prep_next_target( &mut self.device, - self.vert_buffer.get_buffer(), - self.index_buffer.get_buffer(), + &mut self.draw_buffers, &self.renderpass, &self.pipeline, &self.pipeline_layout, @@ -633,12 +616,13 @@ impl<'a> RenderingContext<'a> { let uv = Vector2::new(vert.tex.u[0], vert.tex.v[0]); let uvp = UVPoint(vert.position, face.texture_idx.try_into().unwrap(), uv); - self.vert_buffer[curr_vert_idx] = uvp; + self.draw_buffers.vertex_buffer[curr_vert_idx] = uvp; curr_vert_idx += 1; } - self.index_buffer[curr_idx_idx] = (start_idx, start_idx + 1, start_idx + 2); + self.draw_buffers.index_buffer[curr_idx_idx] = + (start_idx, start_idx + 1, start_idx + 2); curr_idx_idx += 1; @@ -679,13 +663,13 @@ impl<'a> RenderingContext<'a> { } // Update our buffers before we actually start drawing - self.vert_buffer.commit( + self.draw_buffers.vertex_buffer.commit( &self.device, &mut self.queue_group.queues[0], &mut self.cmd_pool, ); - self.index_buffer.commit( + self.draw_buffers.index_buffer.commit( &self.device, &mut self.queue_group.queues[0], &mut self.cmd_pool, @@ -722,8 +706,7 @@ impl<'a> core::ops::Drop for RenderingContext<'a> { unsafe { use core::ptr::read; - ManuallyDrop::into_inner(read(&self.vert_buffer)).deactivate(&mut self.device); - ManuallyDrop::into_inner(read(&self.index_buffer)).deactivate(&mut self.device); + ManuallyDrop::into_inner(read(&self.draw_buffers)).deactivate(&mut self.device); ManuallyDrop::into_inner(read(&self.texture_store)).deactivate(&mut self.device); ManuallyDrop::into_inner(read(&self.target_chain)) diff --git a/stockton-render/src/draw/draw_buffers.rs b/stockton-render/src/draw/draw_buffers.rs new file mode 100644 index 0000000..daeea57 --- /dev/null +++ b/stockton-render/src/draw/draw_buffers.rs @@ -0,0 +1,54 @@ +// Copyright (C) Oscar Shrimpton 2019 + +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. + +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. + +// You should have received a copy of the GNU General Public License along +// with this program. If not, see . + +use crate::types::*; +use draw::buffer::StagedBuffer; +use draw::UVPoint; +use error::CreationError; +use hal::buffer::Usage; +use std::mem::ManuallyDrop; + +/// Initial size of vertex buffer. TODO: Way of overriding this +pub const INITIAL_VERT_SIZE: u64 = 3 * 3000; + +/// Initial size of index buffer. TODO: Way of overriding this +pub const INITIAL_INDEX_SIZE: u64 = 3000; + +/// The buffers used for drawing, ie index and vertex buffer +pub struct DrawBuffers<'a> { + pub vertex_buffer: ManuallyDrop>, + pub index_buffer: ManuallyDrop>, +} + +impl<'a> DrawBuffers<'a> { + pub fn new(device: &mut Device, adapter: &Adapter) -> Result, CreationError> { + let vert = StagedBuffer::new(device, &adapter, Usage::VERTEX, INITIAL_VERT_SIZE)?; + let index = StagedBuffer::new(device, &adapter, Usage::INDEX, INITIAL_INDEX_SIZE)?; + + Ok(DrawBuffers { + vertex_buffer: ManuallyDrop::new(vert), + index_buffer: ManuallyDrop::new(index), + }) + } + + pub fn deactivate(self, device: &mut Device) { + unsafe { + use core::ptr::read; + + ManuallyDrop::into_inner(read(&self.vertex_buffer)).deactivate(device); + ManuallyDrop::into_inner(read(&self.index_buffer)).deactivate(device); + } + } +} diff --git a/stockton-render/src/draw/mod.rs b/stockton-render/src/draw/mod.rs index 1151192..a58e8c8 100644 --- a/stockton-render/src/draw/mod.rs +++ b/stockton-render/src/draw/mod.rs @@ -22,6 +22,7 @@ mod macros; mod buffer; mod camera; mod context; +mod draw_buffers; mod texture; pub use self::context::RenderingContext; diff --git a/stockton-render/src/draw/target.rs b/stockton-render/src/draw/target.rs index 5e0ade0..1260c49 100644 --- a/stockton-render/src/draw/target.rs +++ b/stockton-render/src/draw/target.rs @@ -20,6 +20,8 @@ use crate::types::*; use core::{iter::once, mem::ManuallyDrop}; use arrayvec::ArrayVec; +use draw::buffer::ModifiableBuffer; +use draw::draw_buffers::DrawBuffers; use hal::{ format::{ChannelType, Format, Swizzle}, image::{Extent, Usage as ImgUsage, ViewKind}, @@ -258,8 +260,7 @@ impl TargetChain { pub fn prep_next_target<'a>( &'a mut self, device: &mut Device, - vert_buffer: &Buffer, - index_buffer: &Buffer, + draw_buffers: &mut DrawBuffers, renderpass: &RenderPass, pipeline: &GraphicsPipeline, pipeline_layout: &PipelineLayout, @@ -313,10 +314,11 @@ impl TargetChain { // Get references to our buffers let (vbufs, ibuf) = { - let vbufref: &::Buffer = vert_buffer; + let vbufref: &::Buffer = + draw_buffers.vertex_buffer.get_buffer(); let vbufs: ArrayVec<[_; 1]> = [(vbufref, SubRange::WHOLE)].into(); - let ibuf = index_buffer; + let ibuf = draw_buffers.index_buffer.get_buffer(); (vbufs, ibuf) }; diff --git a/stockton-render/src/draw/texture/chunk.rs b/stockton-render/src/draw/texture/chunk.rs index 02f2dc5..c2781de 100644 --- a/stockton-render/src/draw/texture/chunk.rs +++ b/stockton-render/src/draw/texture/chunk.rs @@ -20,14 +20,15 @@ use hal::prelude::*; use image::{Rgba, RgbaImage}; use core::mem::replace; -use std::ops::{Deref, Range}; +use std::ops::Deref; use crate::{error, types::*}; use super::image::SampledImage; use super::resolver::TextureResolver; use log::debug; -use stockton_levels::prelude::*; +use std::iter::Iterator; +use stockton_levels::traits::textures::Texture; /// The size of a chunk. Needs to match up with the fragment shader pub const CHUNK_SIZE: usize = 8; @@ -41,25 +42,18 @@ pub struct TextureChunk { impl TextureChunk { /// Create a new texture chunk and load in the textures specified by `range` from `file` using `resolver` /// Can error if the descriptor pool is too small or if a texture isn't found - pub fn new( + pub fn new<'a, I, R: TextureResolver>( device: &mut Device, adapter: &mut Adapter, command_queue: &mut CommandQueue, command_pool: &mut CommandPool, - pool: &mut DescriptorPool, - layout: &DescriptorSetLayout, - file: &T, - range: Range, + descriptor_set: DescriptorSet, + textures: I, resolver: &mut R, - ) -> Result { - // - let descriptor_set = unsafe { - pool.allocate_set(&layout).map_err(|e| { - println!("{:?}", e); - error::CreationError::OutOfMemoryError - })? - }; - + ) -> Result + where + I: 'a + Iterator, + { let mut store = TextureChunk { descriptor_set, sampled_images: Vec::with_capacity(CHUNK_SIZE), @@ -68,9 +62,7 @@ impl TextureChunk { let mut local_idx = 0; debug!("Created descriptor set"); - for tex_idx in range { - debug!("Loading tex {}", local_idx + 1); - let tex = file.get_texture(tex_idx); + for tex in textures { if let Some(img) = resolver.resolve(tex) { store .put_texture(img, local_idx, device, adapter, command_queue, command_pool) diff --git a/stockton-render/src/draw/texture/loader.rs b/stockton-render/src/draw/texture/loader.rs index 863e1a8..22a6212 100644 --- a/stockton-render/src/draw/texture/loader.rs +++ b/stockton-render/src/draw/texture/loader.rs @@ -129,24 +129,20 @@ impl TextureStore { debug!("Starting to load textures..."); let mut chunks = Vec::with_capacity(num_chunks); for i in 0..num_chunks { - let range = { - let mut r = (i * CHUNK_SIZE) as u32..((i + 1) * CHUNK_SIZE) as u32; - if r.end > size as u32 { - r.end = size as u32; - } - r - }; - debug!("Chunk {} / {} covering {:?}", i + 1, num_chunks, range); + debug!("Chunk {} / {}", i + 1, num_chunks); + let descriptor_set = unsafe { + descriptor_pool + .allocate_set(&descriptor_set_layout) + .map_err(|_| error::CreationError::OutOfMemoryError)? + }; chunks.push(TextureChunk::new( device, adapter, command_queue, command_pool, - &mut descriptor_pool, - &descriptor_set_layout, - file, - range, + descriptor_set, + file.textures_iter().skip(i * CHUNK_SIZE).take(CHUNK_SIZE), &mut resolver, )?); } -- cgit v1.2.3