Skip to content

Commit

Permalink
Finally fixed Uniform Buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jan 7, 2024
1 parent 7c307fb commit 4d451f9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 36 deletions.
26 changes: 1 addition & 25 deletions crates/vent-rendering/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem::align_of;

use ash::vk;

use crate::{allocator::MemoryAllocator, debug, instance::VulkanInstance};
use crate::{allocator::MemoryAllocator, any_as_u8_slice, debug, instance::VulkanInstance};

Check failure on line 5 in crates/vent-rendering/src/buffer.rs

View workflow job for this annotation

GitHub Actions / Test `cargo check/test` on ubuntu-latest

unused import: `any_as_u8_slice`

Check failure on line 5 in crates/vent-rendering/src/buffer.rs

View workflow job for this annotation

GitHub Actions / Test `cargo check/test` on macos-latest

unused import: `any_as_u8_slice`

Check failure on line 5 in crates/vent-rendering/src/buffer.rs

View workflow job for this annotation

GitHub Actions / Test `cargo check/test` on windows-latest

unused import: `any_as_u8_slice`

pub struct VulkanBuffer {
pub buffer: vk::Buffer,
Expand Down Expand Up @@ -90,19 +90,6 @@ impl VulkanBuffer {
buffer
}

pub fn new_init_type<T>(
instance: &VulkanInstance,
allocator: &MemoryAllocator,
size: vk::DeviceSize,
usage: vk::BufferUsageFlags,
data: *const T,
name: Option<&str>,
) -> Self {
let buffer = Self::new(instance, allocator, size, usage, name);
buffer.upload_type(&instance.device, data, size);
buffer
}

pub fn upload_data<T: Copy>(&self, device: &ash::Device, data: &[T], size: vk::DeviceSize) {
unsafe {
let memory = device
Expand All @@ -114,17 +101,6 @@ impl VulkanBuffer {
}
}

pub fn upload_type<T>(&self, device: &ash::Device, data: *const T, size: vk::DeviceSize) {
unsafe {
let memory = device
.map_memory(self.buffer_memory, 0, size, vk::MemoryMapFlags::empty())
.unwrap();
let mut align = ash::util::Align::new(memory, align_of::<T>() as _, size);
align.copy_from_slice(&[data]);
device.unmap_memory(self.buffer_memory);
}
}

pub fn destroy(&mut self, device: &ash::Device) {
unsafe {
device.destroy_buffer(self.buffer, None);
Expand Down
4 changes: 2 additions & 2 deletions crates/vent-rendering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl<'a> Vertex<'a> for Vertex3D {
}
}

pub unsafe fn any_as_u8_slice<T: Sized>(any: &T) -> &[u8] {
pub fn any_as_u8_slice<T: Sized>(any: &T) -> &[u8] {
let ptr = (any as *const T) as *const u8;
std::slice::from_raw_parts(ptr, std::mem::size_of::<T>())
unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<T>()) }
}

pub fn begin_single_time_command(
Expand Down
2 changes: 1 addition & 1 deletion crates/vent-runtime/res/shaders/app/3D/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ layout (location = 0) out vec4 fragColor;
const float ambient_strength = 0.1;

void main() {
vec4 texture = texture(texture_diffuse, tex_coord) ;
vec4 texture = texture(texture_diffuse, tex_coord) * material.base_color;

// Calculate the ambient color
// vec3 ambient_color = light.color * ambient_strength;
Expand Down
Binary file modified crates/vent-runtime/res/shaders/app/3D/shader.frag.spv
Binary file not shown.
17 changes: 9 additions & 8 deletions crates/vent-runtime/src/render/d3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use glam::{Mat4, Vec3, Vec4};
use vent_assets::Mesh3D;

use vent_ecs::world::World;
use vent_rendering::{buffer::VulkanBuffer, instance::VulkanInstance, Vertex, Vertex3D};
use vent_rendering::{
any_as_u8_slice, buffer::VulkanBuffer, instance::VulkanInstance, Vertex, Vertex3D,
};
use winit::dpi::PhysicalSize;

use self::light_renderer::LightUBO;
Expand Down Expand Up @@ -50,7 +52,6 @@ pub struct Renderer3D {
// depth_view: wgpu::TextureView,
pipeline_layout: vk::PipelineLayout,
pipeline: vk::Pipeline,
// pipeline_wire: Option<wgpu::RenderPipeline>,
}

impl Renderer for Renderer3D {
Expand Down Expand Up @@ -106,26 +107,26 @@ impl Renderer for Renderer3D {
let mut light_buffers = vec![];

for _ in 0..instance.swapchain_images.len() {
let buffer = VulkanBuffer::new_init_type(
let buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<MaterialUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
&MaterialUBO {
any_as_u8_slice(&MaterialUBO {
base_color: Vec4::from_array(material.base_color),
},
}),
None,
);
material_buffers.push(buffer);
let buffer = VulkanBuffer::new_init_type(
let buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<LightUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
&LightUBO {
any_as_u8_slice(&LightUBO {
position: [2.0, 100.0, 2.0].into(),
color: [1.0, 1.0, 1.0].into(),
},
}),
None,
);
light_buffers.push(buffer)
Expand Down

0 comments on commit 4d451f9

Please sign in to comment.