Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug impls for Buffer; also extend methods for Vectors (eg for turning RGB to RGBA colors). #23

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions luisa_compute/examples/path_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ fn main() {
let path_tracer = Kernel::<fn(Tex2d<Float4>, Tex2d<u32>, Accel, Uint2)>::new_async(
&device,
&track!(|image: Tex2dVar<Float4>,
seed_image: Tex2dVar<u32>,
accel: AccelVar,
resolution: Expr<Uint2>| {
seed_image: Tex2dVar<u32>,
accel: AccelVar,
resolution: Expr<Uint2>| {
set_block_size([16u32, 16u32, 1u32]);
let cbox_materials = ([
Float3::new(0.725f32, 0.710f32, 0.680f32), // floor
Expand Down
6 changes: 3 additions & 3 deletions luisa_compute/examples/path_tracer_cutout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ fn main() {
let path_tracer = Kernel::<fn(Tex2d<Float4>, Tex2d<u32>, Accel, Uint2)>::new_async(
&device,
&track!(|image: Tex2dVar<Float4>,
seed_image: Tex2dVar<u32>,
accel: AccelVar,
resolution: Expr<Uint2>| {
seed_image: Tex2dVar<u32>,
accel: AccelVar,
resolution: Expr<Uint2>| {
set_block_size([16u32, 16u32, 1u32]);
let cbox_materials = [
Float3::new(0.725f32, 0.710f32, 0.680f32), // floor
Expand Down
18 changes: 18 additions & 0 deletions luisa_compute/src/lang/types/vector/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ pub trait VectorExprProxy {
}))
}
}
impl<T: VectorAlign<2>> VectorExprProxy2<T> {
#[tracked]
pub fn extend(self, z: impl AsExpr<Value = T>) -> Expr<Vec3<T>>
where
T: VectorAlign<3>,
{
Vec3::expr(self.x, self.y, z)
}
}
impl<T: VectorAlign<3>> VectorExprProxy3<T> {
#[tracked]
pub fn extend(self, w: impl AsExpr<Value = T>) -> Expr<Vec4<T>>
where
T: VectorAlign<4>,
{
Vec4::expr(self.x, self.y, self.z, w)
}
}
impl<const N: usize> SquareMatrix<N>
where
f32: VectorAlign<N>,
Expand Down
2 changes: 1 addition & 1 deletion luisa_compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod prelude {
pub use crate::runtime::api::StreamTag;
pub use crate::runtime::{
Callable, Command, Device, DynCallable, Kernel, KernelBuildOptions, KernelDef, Scope,
Stream,
Stream, Swapchain,
};
pub use crate::{cpu_dbg, if_, lc_assert, lc_unreachable, loop_, while_, Context};

Expand Down
62 changes: 60 additions & 2 deletions luisa_compute/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::{Cell, RefCell};
use std::fmt;
use std::ops::RangeBounds;
use std::process::abort;
use std::sync::Arc;
Expand Down Expand Up @@ -279,6 +280,33 @@ pub struct Buffer<T: Value> {
pub(crate) len: usize,
pub(crate) _marker: PhantomData<T>,
}
impl<T: Value + fmt::Debug> fmt::Debug for Buffer<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugEllipsis;
impl fmt::Debug for DebugEllipsis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("..")
}
}

write!(f, "Buffer<{}>({})", std::any::type_name::<T>(), self.len())?;
// if self.len() <= 16 || f.precision().is_some() {
// let count = f.precision().unwrap_or(16);
// if count >= self.len() {
// f.debug_list().entries(self.copy_to_vec().iter()).finish()?;
// } else {
// let values = self.view(0..count).copy_to_vec();
//
// f.debug_list()
// .entries(values.iter())
// .entry(&DebugEllipsis)
// .finish()?;
// }
// }
Ok(())
}
}

pub(crate) struct BufferHandle {
pub(crate) device: Device,
pub(crate) handle: api::Buffer,
Expand Down Expand Up @@ -406,7 +434,7 @@ impl<T: Value> Buffer<T> {
self.view(..).copy_from(data);
}
#[inline]
pub fn copy_from_async<'a>(&self, data: &[T]) -> Command<'_> {
pub fn copy_from_async<'a>(&self, data: &'a [T]) -> Command<'a> {
self.view(..).copy_from_async(data)
}
#[inline]
Expand Down Expand Up @@ -498,6 +526,12 @@ pub struct BufferHeap<T: Value> {
pub(crate) inner: BindlessArray,
pub(crate) _marker: PhantomData<T>,
}
impl<T: Value + fmt::Debug> fmt::Debug for BufferHeap<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BufferHeap<{}>", std::any::type_name::<T>(),)
}
}

pub struct BufferHeapVar<T: Value> {
inner: BindlessArrayVar,
_marker: PhantomData<T>,
Expand Down Expand Up @@ -849,7 +883,7 @@ unsafe impl Sync for BindlessArray {}
pub use api::{PixelFormat, PixelStorage, Sampler, SamplerAddress, SamplerFilter};
use luisa_compute_ir::context::type_hash;
use luisa_compute_ir::ir::{
new_node, Binding, BindlessArrayBinding, BufferBinding, Func, Instruction, Node, TextureBinding,
Binding, BindlessArrayBinding, BufferBinding, Func, Instruction, Node, TextureBinding,
};

pub(crate) struct TextureHandle {
Expand Down Expand Up @@ -1060,6 +1094,17 @@ pub struct Tex2d<T: IoTexel> {
pub(crate) handle: Arc<TextureHandle>,
pub(crate) marker: PhantomData<T>,
}
impl<T: IoTexel + fmt::Debug> fmt::Debug for Tex2d<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Tex2d<{}>({}, {})",
std::any::type_name::<T>(),
self.width(),
self.height(),
)
}
}

// `T` is the read out type of the texture, which is not necessarily the same as
// the storage type In fact, the texture can be stored in any format as long as
Expand All @@ -1074,6 +1119,19 @@ pub struct Tex3d<T: IoTexel> {
pub(crate) handle: Arc<TextureHandle>,
pub(crate) marker: PhantomData<T>,
}
impl<T: IoTexel + fmt::Debug> fmt::Debug for Tex3d<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Tex3d<{}>({}, {}, {})",
std::any::type_name::<T>(),
self.width(),
self.height(),
self.depth(),
)
}
}

#[derive(Clone, Copy)]
pub struct Tex2dView<'a, T: IoTexel> {
pub(crate) tex: &'a Tex2d<T>,
Expand Down
2 changes: 1 addition & 1 deletion luisa_compute/src/rtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::internal_prelude::*;
use crate::runtime::*;
use crate::{ResourceTracker, *};
use luisa_compute_ir::ir::{
new_node, AccelBinding, Binding, Func, Instruction, IrBuilder, Node, NodeRef, Type,
AccelBinding, Binding, Func, Instruction, IrBuilder, Node, NodeRef, Type,
};
use parking_lot::RwLock;
use std::ops::Deref;
Expand Down
1 change: 1 addition & 0 deletions luisa_compute/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ pub fn submit_default_stream_and_sync<'a, I: IntoIterator<Item = Command<'a>>>(
})
}

#[must_use]
pub struct Command<'a> {
#[allow(dead_code)]
pub(crate) inner: api::Command,
Expand Down