Skip to content

Commit

Permalink
rt(gl): use array_init instead of manually doing it with MaybeUninit
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Sep 19, 2024
1 parent ad3ae06 commit e68aa94
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 77 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions librashader-reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ rspirv = { version = "0.12.0", optional = true }
spirv = { version = "0.3.0", optional = true}

serde = { version = "1.0", features = ["derive"], optional = true }

indexmap = { version = "2.1.0", features = [] }
matches = { version = "0.1.10", features = [] }
rustc-hash = "2.0.0"

[target.'cfg(windows)'.dependencies.spirv-to-dxil]
Expand Down
1 change: 1 addition & 0 deletions librashader-runtime-gl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ glow = { workspace = true}
bytemuck = { version = "1.12.3", features = ["derive"] }
thiserror = "1.0.37"
rayon = "1.6.1"
array-init = "2.1.0"

[features]
stable = ["librashader-reflect/stable"]
Expand Down
49 changes: 14 additions & 35 deletions librashader-runtime-gl/src/gl/gl3/ubo_ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,29 @@ use librashader_reflect::reflect::semantics::BufferReflection;
use librashader_runtime::ringbuffer::InlineRingBuffer;
use librashader_runtime::ringbuffer::RingBuffer;
use librashader_runtime::uniforms::UniformStorageAccess;
use std::mem::MaybeUninit;

pub struct Gl3UboRing<const SIZE: usize> {
ring: InlineRingBuffer<glow::Buffer, SIZE>,
}

impl<const SIZE: usize> Gl3UboRing<SIZE> {
const _ASSERT_TRANSMUTABLE: () = assert!(
std::mem::size_of::<[glow::Buffer; SIZE]>()
== std::mem::size_of::<[MaybeUninit<glow::Buffer>; SIZE]>()
);
}

impl<const SIZE: usize> UboRing<SIZE> for Gl3UboRing<SIZE> {
fn new(ctx: &glow::Context, buffer_size: u32) -> error::Result<Self> {
// TODO: array::try_from_fn whenever that gets stabilized
// this is basically blocking on try_trait_v2
let mut items: [MaybeUninit<glow::Buffer>; SIZE] = [MaybeUninit::zeroed(); SIZE];
for items in items.iter_mut() {
unsafe {
let buffer = ctx
.create_buffer()
.map(|buffer| {
ctx.bind_buffer(glow::UNIFORM_BUFFER, Some(buffer));
ctx.buffer_data_size(
glow::UNIFORM_BUFFER,
buffer_size as i32,
glow::STREAM_DRAW,
);
ctx.bind_buffer(glow::UNIFORM_BUFFER, None);
buffer
})
.map_err(FilterChainError::GlError)?;

*items = MaybeUninit::new(buffer)
}
}

// SAFETY: everything was initialized above.
// MaybeUninit<glow::Buffer> and glow::Buffer have the same size.
let items: [glow::Buffer; SIZE] = unsafe { std::mem::transmute_copy(&items) };
let items: [glow::Buffer; SIZE] = array_init::try_array_init(|_| unsafe {
ctx
.create_buffer()
.map(|buffer| {
ctx.bind_buffer(glow::UNIFORM_BUFFER, Some(buffer));
ctx.buffer_data_size(
glow::UNIFORM_BUFFER,
buffer_size as i32,
glow::STREAM_DRAW,
);
ctx.bind_buffer(glow::UNIFORM_BUFFER, None);
buffer
})
}).map_err(FilterChainError::GlError)?;

let ring: InlineRingBuffer<glow::Buffer, SIZE> = InlineRingBuffer::from_array(items);

Ok(Gl3UboRing { ring })
}

Expand Down
43 changes: 12 additions & 31 deletions librashader-runtime-gl/src/gl/gl46/ubo_ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,27 @@ use librashader_reflect::reflect::semantics::BufferReflection;
use librashader_runtime::ringbuffer::InlineRingBuffer;
use librashader_runtime::ringbuffer::RingBuffer;
use librashader_runtime::uniforms::UniformStorageAccess;
use std::mem::MaybeUninit;

pub struct Gl46UboRing<const SIZE: usize> {
ring: InlineRingBuffer<glow::Buffer, SIZE>,
}

impl<const SIZE: usize> Gl46UboRing<SIZE> {
const _ASSERT_TRANSMUTABLE: () = assert!(
std::mem::size_of::<[glow::Buffer; SIZE]>()
== std::mem::size_of::<[MaybeUninit<glow::Buffer>; SIZE]>()
);
}

impl<const SIZE: usize> UboRing<SIZE> for Gl46UboRing<SIZE> {
fn new(context: &glow::Context, buffer_size: u32) -> error::Result<Self> {
// TODO: array::try_from_fn whenever that gets stabilized
// this is basically blocking on try_trait_v2
let mut items: [MaybeUninit<glow::Buffer>; SIZE] = [MaybeUninit::zeroed(); SIZE];
for items in items.iter_mut() {
unsafe {
let buffer = context
.create_named_buffer()
.map(|buffer| {
context.named_buffer_data_size(
buffer,
buffer_size as i32,
glow::STREAM_DRAW,
);
buffer
})
.map_err(FilterChainError::GlError)?;

*items = MaybeUninit::new(buffer)
}
}
let items: [glow::Buffer; SIZE] = array_init::try_array_init(|_| unsafe {
context
.create_named_buffer()
.map(|buffer| {
context.named_buffer_data_size(
buffer,
buffer_size as i32,
glow::STREAM_DRAW,
);
buffer
})
}).map_err(FilterChainError::GlError)?;

// SAFETY: everything was initialized above.
let items: [glow::Buffer; SIZE] = unsafe { std::mem::transmute_copy(&items) };
let ring: InlineRingBuffer<glow::Buffer, SIZE> = InlineRingBuffer::from_array(items);

Ok(Gl46UboRing { ring })
}

Expand Down

0 comments on commit e68aa94

Please sign in to comment.