fix ubo
This commit is contained in:
@@ -4,7 +4,7 @@ use winit::event::Event;
|
||||
use crate::config::LogConfig;
|
||||
use crate::input::InputState;
|
||||
use crate::vulkan::{Game, GameObject, GameObjectHandle, LinePoint, MeshHandle, VulkanRenderer};
|
||||
use crate::vulkan::vs::ty::UniformBufferObject;
|
||||
use crate::vulkan::vs::ty::ObjectUniformData;
|
||||
|
||||
mod vulkan;
|
||||
mod input;
|
||||
@@ -25,7 +25,7 @@ impl Game for TestGame {
|
||||
self.input.on_window_event(event);
|
||||
}
|
||||
|
||||
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> UniformBufferObject {
|
||||
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> ObjectUniformData {
|
||||
self.input.frame_start();
|
||||
let time = (renderer.game_data.start_time.elapsed().unwrap().as_micros() as f64 / 1000000.0) as f32;
|
||||
|
||||
@@ -68,12 +68,10 @@ impl Game for TestGame {
|
||||
|
||||
renderer.game_data.line_push_constants.view = view.into();
|
||||
renderer.game_data.line_push_constants.projection = proj.into();
|
||||
renderer.game_data.push_constants.view = view.into();
|
||||
renderer.game_data.push_constants.projection = proj.into();
|
||||
|
||||
self.input.frame_end();
|
||||
|
||||
UniformBufferObject {
|
||||
ObjectUniformData {
|
||||
view: view.into(),
|
||||
projection: proj.into(),
|
||||
time,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::sync::{Arc};
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use cgmath::{Matrix4, SquareMatrix};
|
||||
@@ -6,7 +6,7 @@ use image::{ImageBuffer, ImageFormat, Rgb, Rgba};
|
||||
use image::buffer::ConvertBuffer;
|
||||
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
|
||||
use vulkano::command_buffer::{AutoCommandBuffer, AutoCommandBufferBuilder, DynamicState};
|
||||
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet};
|
||||
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
||||
use vulkano::descriptor::DescriptorSet;
|
||||
use vulkano::device::{Device, DeviceExtensions, Queue};
|
||||
use vulkano::format::{ClearValue, Format};
|
||||
@@ -24,7 +24,7 @@ use vulkano::sync::{FlushError, GpuFuture};
|
||||
use vulkano::sync;
|
||||
use vulkano_win::VkSurfaceBuild;
|
||||
use winit::event::{Event, WindowEvent};
|
||||
use winit::event_loop::{EventLoop, ControlFlow};
|
||||
use winit::event_loop::{ControlFlow, EventLoop};
|
||||
use winit::window::{Window, WindowBuilder};
|
||||
|
||||
use line_vs::ty::LinePushConstants;
|
||||
@@ -54,7 +54,7 @@ pub trait Game {
|
||||
/// Returns true if event should be ignored by the vulkan handler
|
||||
fn on_window_event(self: &mut Self, event: &Event<()>);
|
||||
|
||||
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> vs::ty::UniformBufferObject;
|
||||
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> vs::ty::ObjectUniformData;
|
||||
}
|
||||
|
||||
pub struct Mesh {
|
||||
@@ -84,7 +84,7 @@ pub struct GameData {
|
||||
pub textures: Vec<Arc<ImmutableImage<Format>>>,
|
||||
}
|
||||
|
||||
type Yeet = dyn DescriptorSet + Send + Sync;
|
||||
type RendererDescriptorSets = dyn DescriptorSet + Send + Sync;
|
||||
|
||||
pub struct VulkanRenderer {
|
||||
pub game_data: GameData,
|
||||
@@ -102,8 +102,8 @@ pub struct VulkanRenderer {
|
||||
pub recreate_swapchain: bool,
|
||||
pub debug_callback: Option<DebugCallback>,
|
||||
pub previous_frame_end: Option<Box<dyn GpuFuture>>,
|
||||
pub uniform_buffers: Vec<Arc<CpuAccessibleBuffer<vs::ty::UniformBufferObject>>>,
|
||||
pub descriptor_sets: Vec<Arc<Yeet>>,
|
||||
pub uniform_buffers: Vec<Arc<CpuAccessibleBuffer<vs::ty::ObjectUniformData>>>,
|
||||
pub descriptor_sets: Vec<Arc<RendererDescriptorSets>>,
|
||||
}
|
||||
|
||||
impl VulkanRenderer {
|
||||
@@ -111,8 +111,6 @@ impl VulkanRenderer {
|
||||
let mut data = GameData {
|
||||
push_constants: PushConstants {
|
||||
model: Matrix4::identity().into(),
|
||||
view: Matrix4::identity().into(),
|
||||
projection: Matrix4::identity().into(),
|
||||
},
|
||||
line_push_constants: LinePushConstants {
|
||||
model: Matrix4::identity().into(),
|
||||
@@ -284,7 +282,7 @@ impl VulkanRenderer {
|
||||
let framebuffers = window_size_dependent_setup(device.clone(), &images, render_pass.clone(), &mut dynamic_state);
|
||||
|
||||
let mut uniform_buffers = Vec::new();
|
||||
let uniform_buffer = vs::ty::UniformBufferObject { view: Matrix4::identity().into(), projection: Matrix4::identity().into(), time: 0.0 };
|
||||
let uniform_buffer = vs::ty::ObjectUniformData { view: Matrix4::identity().into(), projection: Matrix4::identity().into(), time: 0.0 };
|
||||
|
||||
for _ in 0..swapchain.num_images() {
|
||||
uniform_buffers.push(CpuAccessibleBuffer::from_data(
|
||||
@@ -299,7 +297,7 @@ impl VulkanRenderer {
|
||||
|
||||
let descriptor_sets = uniform_buffers.iter().map(|uniform_buffer| {
|
||||
let builder = PersistentDescriptorSet::start(descriptor_set_layout.clone());
|
||||
let result: Arc<Yeet> = Arc::new(builder
|
||||
let result: Arc<RendererDescriptorSets> = Arc::new(builder
|
||||
.add_buffer(uniform_buffer.clone()).unwrap()
|
||||
.add_sampled_image(default_tex.clone(), sampler.clone()).unwrap()
|
||||
.build().unwrap());
|
||||
@@ -322,9 +320,9 @@ impl VulkanRenderer {
|
||||
recreate_swapchain: false, debug_callback, previous_frame_end }, events_loop)
|
||||
}
|
||||
|
||||
fn create_command_buffer(self: &mut Self, fb_index: usize, ubo: vs::ty::UniformBufferObject) -> Arc<AutoCommandBuffer> {
|
||||
fn create_command_buffer(self: &mut Self, fb_index: usize, uniform_buffer_data: vs::ty::ObjectUniformData) -> Arc<AutoCommandBuffer> {
|
||||
let mut builder = AutoCommandBufferBuilder::primary_simultaneous_use(self.device.clone(), self.queue.family()).unwrap();
|
||||
builder.update_buffer(self.uniform_buffers[fb_index].clone(), ubo).unwrap();
|
||||
builder.update_buffer(self.uniform_buffers[fb_index].clone(), uniform_buffer_data).unwrap();
|
||||
builder.begin_render_pass(self.framebuffers[fb_index].clone(), false, vec![ClearValue::Float([0.0, 0.0, 0.0, 1.0]), ClearValue::Depth(1.0)]).unwrap();
|
||||
|
||||
for i in 0..self.game_data.game_objects.len() {
|
||||
@@ -347,13 +345,11 @@ impl VulkanRenderer {
|
||||
Arc::new(builder.build().unwrap())
|
||||
}
|
||||
|
||||
pub fn render_loop(self: &mut Self, new_ubo: vs::ty::UniformBufferObject) {
|
||||
// It is important to call this function from time to time, otherwise resources will keep
|
||||
// accumulating and you will eventually reach an out of memory error.
|
||||
// Calling this function polls various fences in order to determine what the GPU has
|
||||
// already processed, and frees the resources that are no longer needed.
|
||||
pub fn render_loop(self: &mut Self, new_ubo: vs::ty::ObjectUniformData) {
|
||||
// cleanup previous frame
|
||||
self.previous_frame_end.as_mut().unwrap().cleanup_finished();
|
||||
|
||||
// recreate swapchain if window size changed
|
||||
if self.recreate_swapchain {
|
||||
let window = self.surface.window();
|
||||
let inner_size = window.inner_size();
|
||||
@@ -378,6 +374,7 @@ impl VulkanRenderer {
|
||||
self.recreate_swapchain = false;
|
||||
}
|
||||
|
||||
// recreate pipeline if requested
|
||||
if self.game_data.recreate_pipeline {
|
||||
if let Some(pipeline_ok) = create_pipeline::<Vertex>(self.device.clone(), self.render_pass.clone(), false) {
|
||||
self.pipeline = pipeline_ok;
|
||||
@@ -468,7 +465,7 @@ impl VulkanRenderer {
|
||||
|
||||
pub fn start_event_loop(mut renderer: VulkanRenderer, mut game: Box<dyn Game>, event_loop: EventLoop<()>) {
|
||||
let mut recreate_swapchain = false;
|
||||
let ubo = game.update(&mut renderer);
|
||||
let mut ubo = game.update(&mut renderer);
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
game.on_window_event(&event);
|
||||
@@ -480,7 +477,7 @@ pub fn start_event_loop(mut renderer: VulkanRenderer, mut game: Box<dyn Game>, e
|
||||
recreate_swapchain = true;
|
||||
},
|
||||
Event::RedrawEventsCleared => {
|
||||
game.update(&mut renderer);
|
||||
ubo = game.update(&mut renderer);
|
||||
renderer.render_loop(ubo);
|
||||
},
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user