aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:20 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:20 +0100
commit7cdb4bb159a7df88390e63c59f0d4c5538d7411b (patch)
tree87867a506fbc0ac0a641080042ee95130209d17d
parent38ec1aa6b22e7e02c4b25c8fbf376b7cf5885009 (diff)
feat(render): add size parameter during stagedbuffer creation
-rw-r--r--examples/render-quad/src/main.rs10
-rw-r--r--stockton-render/src/draw/buffer.rs29
-rw-r--r--stockton-render/src/draw/context.rs24
3 files changed, 36 insertions, 27 deletions
diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs
index d0993ea..9cc7310 100644
--- a/examples/render-quad/src/main.rs
+++ b/examples/render-quad/src/main.rs
@@ -41,10 +41,10 @@ fn main() {
let window = WindowBuilder::new().build(&event_loop).unwrap();
let mut ctx = RenderingContext::new(&window).unwrap();
- ctx.vert_buffer[0] = UVPoint(Vector2::new(-0.5, -0.5), Vector3::new(1.0, 0.0, 0.0));
- ctx.vert_buffer[1] = UVPoint(Vector2::new(0.5, -0.5), Vector3::new(0.0, 1.0, 0.0));
- ctx.vert_buffer[2] = UVPoint(Vector2::new(0.5, 0.5), Vector3::new(0.0, 0.0, 1.0));
- ctx.vert_buffer[3] = UVPoint(Vector2::new(-0.5, 0.5), Vector3::new(1.0, 0.0, 1.0));
+ ctx.vert_buffer[0] = UVPoint(Vector2::new(-0.5, -0.5), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0));
+ ctx.vert_buffer[1] = UVPoint(Vector2::new(0.5, -0.5), Vector3::new(0.0, 1.0, 0.0), Vector2::new(1.0, 0.0));
+ ctx.vert_buffer[2] = UVPoint(Vector2::new(0.5, 0.5), Vector3::new(0.0, 0.0, 1.0), Vector2::new(1.0, 1.0));
+ ctx.vert_buffer[3] = UVPoint(Vector2::new(-0.5, 0.5), Vector3::new(1.0, 0.0, 1.0), Vector2::new(0.0, 1.0));
ctx.index_buffer[0] = (0, 1, 2);
ctx.index_buffer[1] = (0, 2, 3);
@@ -72,7 +72,7 @@ fn main() {
let mouse_x: f32 = ((position.x / win_size.width as f64) * 2.0 - 1.0) as f32;
let mouse_y: f32 = ((position.y / win_size.height as f64) * 2.0 - 1.0) as f32;
- ctx.vert_buffer[0] = UVPoint(Vector2::new(mouse_x, mouse_y), Vector3::new(1.0, 0.0, 0.0));
+ ctx.vert_buffer[0] = UVPoint(Vector2::new(mouse_x, mouse_y), Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 0.0));
}
Event::MainEventsCleared => {
diff --git a/stockton-render/src/draw/buffer.rs b/stockton-render/src/draw/buffer.rs
index f420fc7..8ec31e4 100644
--- a/stockton-render/src/draw/buffer.rs
+++ b/stockton-render/src/draw/buffer.rs
@@ -28,15 +28,13 @@ use hal::{
use crate::error::CreationError;
use crate::types::*;
-// TODO: Proper sizing of buffers
-const BUF_SIZE: u64 = 4 * 15 * 2;
-
fn create_buffer(device: &mut Device,
adapter: &Adapter,
usage: Usage,
- properties: Properties) -> Result<(Buffer, Memory), CreationError> {
+ properties: Properties,
+ size: u64) -> Result<(Buffer, Memory), CreationError> {
let mut buffer = unsafe { device
- .create_buffer(BUF_SIZE, usage) }
+ .create_buffer(size, usage) }
.map_err(|e| CreationError::BufferError (e))?;
let requirements = unsafe { device.get_buffer_requirements(&buffer) };
@@ -74,22 +72,23 @@ pub struct StagedBuffer<'a, T: Sized> {
memory: ManuallyDrop<Memory>,
staged_mapped_memory: &'a mut [T],
staged_is_dirty: bool,
+ pub highest_used: usize
}
impl<'a, T: Sized> StagedBuffer<'a, T> {
- pub fn new(device: &mut Device, adapter: &Adapter, usage: Usage) -> Result<Self, CreationError> {
+ /// size is the size in T
+ pub fn new(device: &mut Device, adapter: &Adapter, usage: Usage, size: u64) -> Result<Self, CreationError> {
- let (staged_buffer, staged_memory) = create_buffer(device, adapter, Usage::TRANSFER_SRC, Properties::CPU_VISIBLE)?;
- let (buffer, memory) = create_buffer(device, adapter, Usage::TRANSFER_DST | usage, Properties::DEVICE_LOCAL)?;
+ let size_bytes = size * size_of::<T>() as u64;
+ let (staged_buffer, staged_memory) = create_buffer(device, adapter, Usage::TRANSFER_SRC, Properties::CPU_VISIBLE, size_bytes)?;
+ let (buffer, memory) = create_buffer(device, adapter, Usage::TRANSFER_DST | usage, Properties::DEVICE_LOCAL, size_bytes)?;
// Map it somewhere and get a slice to that memory
let staged_mapped_memory = unsafe {
let ptr = device.map_memory(&staged_memory, Segment::ALL).unwrap();
-
- let slice_size: usize = (BUF_SIZE / size_of::<T>() as u64).try_into().unwrap(); // size in f32s
- std::slice::from_raw_parts_mut(ptr as *mut T, slice_size)
+ std::slice::from_raw_parts_mut(ptr as *mut T, size.try_into().unwrap())
};
Ok(StagedBuffer {
@@ -98,7 +97,8 @@ impl<'a, T: Sized> StagedBuffer<'a, T> {
buffer: ManuallyDrop::new(buffer),
memory: ManuallyDrop::new(memory),
staged_mapped_memory,
- staged_is_dirty: false
+ staged_is_dirty: false,
+ highest_used: 0
})
}
@@ -133,7 +133,7 @@ impl <'a, T: Sized> ModifiableBuffer for StagedBuffer<'a, T> {
BufferCopy {
src: 0,
dst: 0,
- size: BUF_SIZE // TODO
+ size: (self.staged_mapped_memory.len() * size_of::<T>()) as u64
}
]);
buf.finish();
@@ -175,6 +175,9 @@ impl<'a, T: Sized> Index<usize> for StagedBuffer<'a, T> {
impl<'a, T: Sized> IndexMut<usize> for StagedBuffer<'a, T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.staged_is_dirty = true;
+ if index > self.highest_used {
+ self.highest_used = index;
+ }
&mut self.staged_mapped_memory[index]
}
}
diff --git a/stockton-render/src/draw/context.rs b/stockton-render/src/draw/context.rs
index fced5e8..882db44 100644
--- a/stockton-render/src/draw/context.rs
+++ b/stockton-render/src/draw/context.rs
@@ -48,9 +48,9 @@ const VERTEX_SOURCE: &str = include_str!("./data/stockton.vert");
/// Source for fragment shader. TODO
const FRAGMENT_SOURCE: &str = include_str!("./data/stockton.frag");
-/// Represents a point of a vertices, including U/V information.
+/// Represents a point of a vertices, including RGB and UV information.
#[derive(Debug, Clone, Copy)]
-pub struct UVPoint (pub Vector2, pub Vector3);
+pub struct UVPoint (pub Vector2, pub Vector3, pub Vector2);
/// Contains all the hal related stuff.
/// In the end, this takes some 3D points and puts it on the screen.
@@ -257,8 +257,8 @@ impl<'a> RenderingContext<'a> {
let (vert_buffer, index_buffer) = {
use hal::buffer::Usage;
- let vert = StagedBuffer::new(&mut device, &adapter, Usage::VERTEX | Usage::TRANSFER_DST)?;
- let index = StagedBuffer::new(&mut device, &adapter, Usage::TRANSFER_SRC)?;
+ let vert = StagedBuffer::new(&mut device, &adapter, Usage::VERTEX, 4)?;
+ let index = StagedBuffer::new(&mut device, &adapter, Usage::INDEX, 2)?;
(vert, index)
};
@@ -404,24 +404,31 @@ impl<'a> RenderingContext<'a> {
// Vertex buffers
let vertex_buffers: Vec<VertexBufferDesc> = vec![VertexBufferDesc {
binding: 0,
- stride: (size_of::<f32>() * 5) as u32,
+ stride: (size_of::<f32>() * 7) as u32,
rate: VertexInputRate::Vertex,
}];
- let attributes: Vec<AttributeDesc> = vec![AttributeDesc {
+ let attributes: Vec<AttributeDesc> = vec![AttributeDesc { // XY Attribute
location: 0,
binding: 0,
element: Element {
format: Format::Rg32Sfloat,
offset: 0,
},
- }, AttributeDesc {
+ }, AttributeDesc { // RGB Attribute
location: 1,
binding: 0,
element: Element {
format: Format::Rgb32Sfloat,
offset: (size_of::<f32>() * 2) as ElemOffset,
}
+ }, AttributeDesc { // UV Attribute
+ location: 2,
+ binding: 0,
+ element: Element {
+ format: Format::Rg32Sfloat,
+ offset: (size_of::<f32>() * 5) as ElemOffset,
+ }
}];
// Rasterizer
@@ -663,8 +670,7 @@ impl<'a> RenderingContext<'a> {
range: SubRange::WHOLE,
index_type: hal::IndexType::U16
});
-
- buffer.draw_indexed(0..6, 0, 0..1);
+ buffer.draw_indexed(0..((self.index_buffer.highest_used as u32 + 1) * 3), 0, 0..1);
buffer.end_render_pass();
}
buffer.finish();