Use impl_reprc macro

This commit is contained in:
hodasemi 2023-02-01 14:54:13 +01:00
parent f14737a2bb
commit 6a1075bf13
5 changed files with 39 additions and 16 deletions

View file

@ -16,7 +16,6 @@ rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git"
ui = { git = "https://gavania.de/hodasemi/ui.git" }
anyhow = { version = "1.0.68", features = ["backtrace"] }
safer-ffi = { version = "0.0.10", features = ["proc_macros"] }
paste = "1.0.11"
serde = "1.0.152"
serde_json = "1.0.91"

View file

@ -7,7 +7,7 @@ use std::{
time::Duration,
};
use crate::overlay::elements::PositionOnlyVertex;
use crate::overlay::elements::{ColorBuffer, PositionOnlyVertex};
pub struct BackgroundGenerator;
@ -107,7 +107,7 @@ impl BackgroundGenerator {
let color_buffer = Buffer::builder()
.set_usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.set_memory_usage(MemoryUsage::CpuOnly)
.set_data(&color)
.set_data(&ColorBuffer::from_array(color))
.build(device.clone())?;
let desc_pool = DescriptorPool::builder()

View file

@ -6,18 +6,36 @@ mod watermark;
pub use leaderboard::*;
pub use pedals::*;
pub use radar::*;
use utilities::impl_reprc;
pub use watermark::*;
use std::mem;
use vulkan_rs::prelude::*;
#[derive_ReprC]
#[repr(C)]
#[derive(Clone)]
pub struct PositionOnlyVertex {
pub position: cgmath::Vector4<f32>,
impl_reprc!(
pub struct ColorBuffer {
#[assume_reprc]
c: f32,
}
);
impl ColorBuffer {
pub fn from_array<const N: usize>(a: [f32; N]) -> [Self; N] {
a.into_iter()
.map(|c| Self { c })
.collect::<Vec<Self>>()
.try_into()
.unwrap_or_else(|_: Vec<Self>| unreachable!("create array from vec from an array"))
}
}
impl_reprc!(
pub struct PositionOnlyVertex {
#[assume_reprc]
position: cgmath::Vector4<f32>,
}
);
impl PositionOnlyVertex {
///
/// corners[0] - bottom left

View file

@ -10,6 +10,7 @@ use ui::prelude::*;
use vulkan_rs::prelude::*;
use crate::overlay::{
elements::ColorBuffer,
rfactor_data::{DataReceiver, GamePhase},
UiOverlay,
};
@ -96,10 +97,10 @@ impl Pedals {
.set_descriptor_set_count(2)
.build(device.clone())?;
let brake_color_buffer: Arc<Buffer<f32>> = Buffer::builder()
let brake_color_buffer = Buffer::builder()
.set_usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.set_memory_usage(MemoryUsage::CpuOnly)
.set_data(&[0.9, 0.0, 0.0, 1.0])
.set_data(&ColorBuffer::from_array([0.9, 0.0, 0.0, 1.0]))
.build(device.clone())?;
write_log!("allocate brake descriptor");
@ -107,10 +108,10 @@ impl Pedals {
let brake_descriptor = descriptor_pool.prepare_set().allocate()?;
brake_descriptor.update(&[DescriptorWrite::uniform_buffers(0, &[&brake_color_buffer])])?;
let throttle_color_buffer: Arc<Buffer<f32>> = Buffer::builder()
let throttle_color_buffer = Buffer::builder()
.set_usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.set_memory_usage(MemoryUsage::CpuOnly)
.set_data(&[0.0, 0.9, 0.0, 1.0])
.set_data(&ColorBuffer::from_array([0.0, 0.9, 0.0, 1.0]))
.build(device.clone())?;
write_log!("allocate throttle descriptor");

View file

@ -21,7 +21,7 @@ use crate::{
write_log,
};
use super::PositionOnlyVertex;
use super::{ColorBuffer, PositionOnlyVertex};
fn convert_vec(v: rF2Vec3) -> Vector3<f32> {
vec3(v.x as f32, v.y as f32, v.z as f32)
@ -385,7 +385,7 @@ struct RadarObject {
descriptor_set: Arc<DescriptorSet>,
// uniform buffer
color_buffer: Arc<Buffer<f32>>,
color_buffer: Arc<Buffer<ColorBuffer>>,
// vertex buffer
position_buffer: Arc<Buffer<PositionOnlyVertex>>,
@ -401,7 +401,7 @@ impl RadarObject {
let color_buffer = Buffer::builder()
.set_usage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
.set_memory_usage(MemoryUsage::CpuOnly)
.set_data(&color)
.set_data(&ColorBuffer::from_array(color))
.build(device.clone())?;
let position_buffer = Buffer::builder()
@ -445,7 +445,12 @@ impl RadarObject {
car_width,
car_height,
))?;
self.color_buffer.fill(&color)
self.color_buffer.fill(
&color
.iter()
.map(|&c| ColorBuffer { c })
.collect::<Vec<ColorBuffer>>(),
)
}
}