Skip to content

Commit

Permalink
added external memory
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed Nov 2, 2023
1 parent 0de90df commit e5981d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
48 changes: 22 additions & 26 deletions luisa_compute/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::any::Any;
use std::any::{Any, TypeId};
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::env;
Expand All @@ -9,6 +9,7 @@ use std::path::PathBuf;
use std::rc::Rc;
use std::sync::{Arc, Weak};

use libc::c_void;
use parking_lot::lock_api::RawMutex as RawMutexTrait;
use parking_lot::{Condvar, Mutex, RawMutex, RwLock};

Expand Down Expand Up @@ -177,31 +178,9 @@ impl Device {
}

/// Creates an **unintialized** buffer of `len` bytes.
/// Alias of [`Device::create_buffer::<u8>`].
pub fn create_byte_buffer(&self, len: usize) -> Buffer<u8> {
let name = self.name();
if name == "dx" {
assert!(
len < u32::MAX as usize,
"numer of bytes must be less than u32::MAX on dx"
);
}
let buffer = self.inner.create_buffer(&Type::void(), len);
let handle = Arc::new(BufferHandle {
device: self.clone(),
handle: api::Buffer(buffer.resource.handle),
native_handle: buffer.resource.native_handle,
});
let buffer = Buffer {
handle: handle.clone(),
full_view: BufferView {
device: self.clone(),
handle: Arc::downgrade(&handle),
offset: 0,
len,
_marker: PhantomData,
},
};
buffer
self.create_buffer(len)
}

/// Creates an **unintialized** buffer of `count` elements of type `T` in SOA layout.
Expand Down Expand Up @@ -233,6 +212,9 @@ impl Device {

/// Creates an **unintialized** buffer of `count` elements of type `T`.
pub fn create_buffer<T: Value>(&self, count: usize) -> Buffer<T> {
self._create_buffer(std::ptr::null_mut(), count)
}
fn _create_buffer<T: Value>(&self, ext_mem: *mut c_void, count: usize) -> Buffer<T> {
let name = self.name();
if name == "dx" {
assert!(
Expand All @@ -248,7 +230,12 @@ impl Device {
std::mem::size_of::<T>() > 0,
"size of T must be greater than 0"
);
let buffer = self.inner.create_buffer(&T::type_(), count);
let ty = if TypeId::of::<T>() == TypeId::of::<u8>() {
Type::void()
} else {
<T as TypeOf>::type_()
};
let buffer = self.inner.create_buffer(&ty, count, ext_mem);
let handle = Arc::new(BufferHandle {
device: self.clone(),
handle: api::Buffer(buffer.resource.handle),
Expand All @@ -266,6 +253,15 @@ impl Device {
};
buffer
}

/// Imports an external buffer of `count` elements of type `T`.
pub unsafe fn create_buffer_from_external_memory<T: Value>(
&self,
data: *mut T,
count: usize,
) -> Buffer<T> {
self._create_buffer(data as *mut c_void, count)
}
pub fn create_buffer_from_slice<T: Value>(&self, data: &[T]) -> Buffer<T> {
let buffer = self.create_buffer(data.len());
buffer.view(..).copy_from(data);
Expand Down
2 changes: 1 addition & 1 deletion luisa_compute_sys/LuisaCompute
Submodule LuisaCompute updated 71 files
+8 −8 .gitmodules
+1 −1 include/luisa/api/api.h
+4 −1 include/luisa/dsl/printer.h
+1 −1 include/luisa/runtime/buffer.h
+4 −2 include/luisa/runtime/device.h
+1 −1 include/luisa/runtime/dispatch_buffer.h
+20 −4 include/luisa/runtime/rhi/device_interface.h
+1 −1 include/luisa/rust/api_types.h
+1 −1 include/luisa/rust/api_types.hpp
+1 −1 pyproject.toml
+2 −2 src/api/runtime.cpp
+8 −4 src/backends/common/rust_device_common.cpp
+274 −0 src/backends/common/shader_print_formatter.h
+16 −28 src/backends/cuda/cuda_device.cpp
+2 −4 src/backends/cuda/cuda_device.h
+8 −1 src/backends/cuda/cuda_shader_metadata.cpp
+5 −247 src/backends/cuda/cuda_shader_printer.cpp
+5 −1 src/backends/cuda/cuda_shader_printer.h
+36 −19 src/backends/dx/DXApi/LCDevice.cpp
+3 −3 src/backends/dx/DXApi/LCDevice.h
+1 −1 src/backends/dx/DXRuntime/Device.cpp
+20 −2 src/backends/dx/Resource/DefaultBuffer.cpp
+6 −0 src/backends/dx/Resource/DefaultBuffer.h
+1 −0 src/backends/dx/Resource/DepthBuffer.cpp
+21 −3 src/backends/dx/Resource/GpuAllocator.cpp
+5 −3 src/backends/dx/Resource/GpuAllocator.h
+1 −1 src/backends/dx/Resource/ReadbackBuffer.cpp
+1 −0 src/backends/dx/Resource/RenderTexture.cpp
+1 −1 src/backends/dx/Resource/UploadBuffer.cpp
+1 −0 src/backends/metal/CMakeLists.txt
+10 −8 src/backends/metal/metal_builtin/metal_device_lib.metal
+67 −61 src/backends/metal/metal_builtin_embedded.cpp
+1 −1 src/backends/metal/metal_builtin_embedded.h
+37 −13 src/backends/metal/metal_codegen_ast.cpp
+1 −0 src/backends/metal/metal_codegen_ast.h
+1 −0 src/backends/metal/metal_compiler.cpp
+68 −68 src/backends/metal/metal_device.cpp
+2 −4 src/backends/metal/metal_device.h
+28 −2 src/backends/metal/metal_shader.cpp
+3 −0 src/backends/metal/metal_shader.h
+73 −7 src/backends/metal/metal_shader_metadata.cpp
+3 −2 src/backends/metal/metal_shader_metadata.h
+80 −0 src/backends/metal/metal_shader_printer.cpp
+48 −0 src/backends/metal/metal_shader_printer.h
+8 −4 src/backends/validation/device.cpp
+3 −3 src/backends/validation/device.h
+4 −0 src/py/export_gui.cpp
+4 −4 src/py/export_runtime.cpp
+4 −4 src/py/luisa/buffer.py
+9 −1 src/runtime/byte_buffer.cpp
+0 −8 src/runtime/rhi/device_interface.cpp
+2 −1 src/rust/luisa_compute_api_types/src/lib.rs
+8 −2 src/rust/luisa_compute_backend/src/lib.rs
+12 −2 src/rust/luisa_compute_backend/src/proxy.rs
+7 −1 src/rust/luisa_compute_backend_impl/src/cpu/mod.rs
+18 −8 src/rust/luisa_compute_backend_impl/src/cpu/resource.rs
+1 −1 src/rust/luisa_compute_ir/Cargo.toml
+17 −0 src/rust/luisa_compute_ir_v2/src/lib.rs
+1 −1 src/tests/next/Readme.md
+37 −0 src/tests/next/example/ext/test_supersampling.cpp
+6 −30 src/tests/next/example/gallary/render/path_tracer.cpp
+499 −0 src/tests/next/example/gallary/render/photon_mapping.cpp
+50 −0 src/tests/next/example/gallary/render/util.cpp
+19 −1 src/tests/next/example/gallary/render/util.h
+122 −0 src/tests/next/test/ext/dx/test_dml.cpp
+138 −0 src/tests/next/test/feat/ir/test_autodiff.cpp
+305 −0 src/tests/next/test/feat/ir/test_autodiff_full.cpp
+0 −1 src/tests/next/test/feat/runtime/test_buffer.cpp
+58 −4 src/tests/next/test/feat/runtime/test_buffer_view.cpp
+50 −0 src/tests/next/test/feat/runtime/test_matrix.cpp
+8 −3 src/tests/xmake.lua

0 comments on commit e5981d5

Please sign in to comment.