From 2a90de8796c7d1d4ae8aa7f2c48f7a8b38dac387 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:19 +0100 Subject: (wip) feat(render): seperate vertexbuffer logic --- stockton-render/src/draw/context.rs | 4 +- stockton-render/src/draw/mod.rs | 4 +- stockton-render/src/draw/vertexlump.rs | 113 +++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 stockton-render/src/draw/vertexlump.rs (limited to 'stockton-render/src/draw') diff --git a/stockton-render/src/draw/context.rs b/stockton-render/src/draw/context.rs index 07340c6..9d54cae 100644 --- a/stockton-render/src/draw/context.rs +++ b/stockton-render/src/draw/context.rs @@ -70,7 +70,7 @@ pub struct RenderingContext { pub events_loop: winit::EventsLoop, surface: ::Surface, - device: ManuallyDrop<::Device>, + pub (crate) device: ManuallyDrop<::Device>, swapchain: ManuallyDrop<::Swapchain>, @@ -99,7 +99,7 @@ pub struct RenderingContext { descriptor_set_layouts: ::DescriptorSetLayout, pipeline_layout: ManuallyDrop<::PipelineLayout>, pipeline: ManuallyDrop<::GraphicsPipeline>, - adapter: adapter::Adapter + pub (crate) adapter: adapter::Adapter } diff --git a/stockton-render/src/draw/mod.rs b/stockton-render/src/draw/mod.rs index bdfd3c8..53e2f40 100644 --- a/stockton-render/src/draw/mod.rs +++ b/stockton-render/src/draw/mod.rs @@ -16,6 +16,8 @@ //! Given 3D points and some camera information, renders to the screen. mod context; +mod vertexlump; pub use context::RenderingContext; -pub use context::Tri2; \ No newline at end of file +pub use context::Tri2; +pub use vertexlump::VertexLump; \ No newline at end of file diff --git a/stockton-render/src/draw/vertexlump.rs b/stockton-render/src/draw/vertexlump.rs new file mode 100644 index 0000000..534b3d9 --- /dev/null +++ b/stockton-render/src/draw/vertexlump.rs @@ -0,0 +1,113 @@ +// Copyright (C) 2019 Oscar Shrimpton + +// 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 std::marker::PhantomData; +use core::mem::{size_of}; +use hal::memory::{Pod, Properties, Requirements}; +use hal::buffer::Usage; +use hal::adapter::MemoryTypeId; +use hal::{Device, PhysicalDevice, mapping}; +use back::Backend; +use crate::error::CreationError; +use super::RenderingContext; + +pub struct VertexLump, X: Pod> { + buffer: ::Buffer, + memory: ::Memory, + requirements: Requirements, + + unit_size_bytes: u64, + batch_size: u64, + + active: bool, + + _t: PhantomData, + _x: PhantomData, +} + +const BATCH_SIZE: u64 = 3; + +impl, X: Pod> VertexLump { + pub(crate) fn new(ctx: &mut RenderingContext) -> Result, CreationError> { + let unit_size_bytes = size_of::() as u64; + + let mut buffer = unsafe { ctx.device + .create_buffer(BATCH_SIZE * unit_size_bytes, Usage::VERTEX) } + + .map_err(|e| CreationError::BufferError (e))?; + + let requirements = unsafe { ctx.device.get_buffer_requirements(&buffer) }; + let memory_type_id = ctx.adapter.physical_device + .memory_properties().memory_types + .iter().enumerate() + .find(|&(id, memory_type)| { + requirements.type_mask & (1 << id) != 0 && memory_type.properties.contains(Properties::CPU_VISIBLE) + }) + .map(|(id, _)| MemoryTypeId(id)) + .ok_or(CreationError::BufferNoMemory)?; + + let memory = unsafe {ctx.device + .allocate_memory(memory_type_id, requirements.size) } + .map_err(|_| CreationError::OutOfMemoryError)?; + + unsafe { ctx.device + .bind_buffer_memory(&memory, 0, &mut buffer) } + .map_err(|_| CreationError::BufferNoMemory)?; + + Ok(VertexLump { + buffer, memory, requirements, + + unit_size_bytes, + batch_size: BATCH_SIZE, // TODO + active: true, + _t: PhantomData, + _x: PhantomData + }) + } + + pub(crate) fn writer<'a>(&'a self, ctx: &'a mut RenderingContext) -> Result, ()> { + let mapping_writer = unsafe { ctx.device + .acquire_mapping_writer(&self.memory, 0..self.requirements.size) + .map_err(|_| ())? }; + + Ok(VertexWriter { + mapping_writer, + ctx + }) + } +} + +// TODO +pub struct VertexWriter<'a, X: Pod> { + mapping_writer: mapping::Writer<'a, Backend, X>, + ctx: &'a mut RenderingContext +} + +impl<'a, X: Pod> IndexMut for + +impl<'a, X: Pod> Drop for VertexWriter<'a, X> { + fn drop(&mut self) { + // TODO + } +} + + +impl, X: Pod> Drop for VertexLump { + fn drop(&mut self) { + if !self.active { + panic!("VertexLump dropped without being deactivated"); + } + } +} \ No newline at end of file -- cgit v1.2.3