diff --git a/src/backends/fallback/fallback_accel.cpp b/src/backends/fallback/fallback_accel.cpp index 8101d8935..45b210485 100644 --- a/src/backends/fallback/fallback_accel.cpp +++ b/src/backends/fallback/fallback_accel.cpp @@ -211,5 +211,4 @@ void accel_transform_wrapper(void* accel, unsigned id, void* buffer) luisa::compute::fallback::detail::fill_transform( reinterpret_cast(accel), id, reinterpret_cast(buffer)); - auto g = reinterpret_cast(buffer); } diff --git a/src/backends/fallback/fallback_buffer.cpp b/src/backends/fallback/fallback_buffer.cpp index 5865c4e13..af3d59764 100644 --- a/src/backends/fallback/fallback_buffer.cpp +++ b/src/backends/fallback/fallback_buffer.cpp @@ -8,8 +8,19 @@ namespace luisa::compute::fallback { FallbackBufferView FallbackBuffer::view(size_t offset) noexcept { - LUISA_ASSERT(offset <= data.size(), "Buffer view out of range."); - return {static_cast(data.data() + offset), data.size() - offset}; + LUISA_ASSERT(offset <= size, "Buffer view out of range."); + return {static_cast(data + offset * elementStride), size-offset}; } + FallbackBuffer::FallbackBuffer(size_t size, unsigned elementStride):elementStride(elementStride), size(size) + { + data = luisa::allocate_with_allocator(size * elementStride); + + } + + FallbackBuffer::~FallbackBuffer() + { + luisa::deallocate_with_allocator(data); + } + }// namespace luisa::compute::fallback diff --git a/src/backends/fallback/fallback_buffer.h b/src/backends/fallback/fallback_buffer.h index dd6ca0fc5..1b747d84a 100644 --- a/src/backends/fallback/fallback_buffer.h +++ b/src/backends/fallback/fallback_buffer.h @@ -14,13 +14,15 @@ struct alignas(16) FallbackBufferView { class FallbackBuffer { public: - // FIXME: size - void *addr() { return data.data(); } + explicit FallbackBuffer(size_t size, unsigned elementStride); + void *addr() { return data; } [[nodiscard]] FallbackBufferView view(size_t offset) noexcept; - + ~FallbackBuffer(); private: - std::vector data{}; + unsigned elementStride; + unsigned size; + std::byte* data{}; }; }// namespace luisa::compute::fallback diff --git a/src/backends/fallback/fallback_device.cpp b/src/backends/fallback/fallback_device.cpp index cb555f2d7..e93579cfb 100644 --- a/src/backends/fallback/fallback_device.cpp +++ b/src/backends/fallback/fallback_device.cpp @@ -17,6 +17,7 @@ #include "fallback_accel.h" #include "fallback_bindless_array.h" #include "fallback_shader.h" +#include "fallback_buffer.h" //#include "llvm_event.h" //#include "llvm_shader.h" @@ -70,7 +71,7 @@ void *FallbackDevice::native_handle() const noexcept { } void FallbackDevice::destroy_buffer(uint64_t handle) noexcept { - luisa::deallocate_with_allocator(reinterpret_cast(handle)); + luisa::deallocate_with_allocator(reinterpret_cast(handle)); } void FallbackDevice::destroy_texture(uint64_t handle) noexcept { @@ -136,8 +137,8 @@ BufferCreationInfo FallbackDevice::create_buffer(const Type *element, size_t ele info.element_stride = element->size(); } info.total_size_bytes = info.element_stride * elem_count; - info.handle = reinterpret_cast( - luisa::allocate_with_allocator(info.total_size_bytes)); + auto buffer = luisa::new_with_allocator(elem_count, info.element_stride); + info.handle = reinterpret_cast(buffer); info.native_handle = reinterpret_cast(info.handle); return info; } diff --git a/src/backends/fallback/fallback_shader.cpp b/src/backends/fallback/fallback_shader.cpp index faa6cc7f5..1b5b55b89 100644 --- a/src/backends/fallback/fallback_shader.cpp +++ b/src/backends/fallback/fallback_shader.cpp @@ -211,21 +211,11 @@ void compute::fallback::FallbackShader::dispatch(ThreadPool &pool, const compute using Tag = ShaderDispatchCommand::Argument::Tag; switch (arg.tag) { case Tag::BUFFER: { - //What is indirect? - // if (reinterpret_cast(arg.buffer.handle)->is_indirect()) - // { - // auto buffer = reinterpret_cast(arg.buffer.handle); - // auto binding = buffer->binding(arg.buffer.offset, arg.buffer.size); - // auto ptr = allocate_argument(sizeof(binding)); - // std::memcpy(ptr, &binding, sizeof(binding)); - // } - // else { auto buffer = reinterpret_cast(arg.buffer.handle); auto buffer_view = buffer->view(arg.buffer.offset); - //auto binding = buffer->binding(arg.buffer.offset, arg.buffer.size); auto ptr = allocate_argument(sizeof(buffer_view)); - std::memcpy(ptr, &buffer, sizeof(buffer_view)); + std::memcpy(ptr, &buffer_view, sizeof(buffer_view)); } break; } @@ -282,23 +272,23 @@ void compute::fallback::FallbackShader::dispatch(ThreadPool &pool, const compute auto data = argument_buffer.data(); - // for (int i = 0; i < dispatch_counts.x; ++i) { - // for (int j = 0; j < dispatch_counts.y; ++j) { - // for (int k = 0; k < dispatch_counts.z; ++k) { - // auto c = config; - // c.block_id = make_uint3(i, j, k); - // (*_kernel_entry)(data, &c); - // } - // } - // } + for (int i = 0; i < dispatch_counts.x; ++i) { + for (int j = 0; j < dispatch_counts.y; ++j) { + for (int k = 0; k < dispatch_counts.z; ++k) { + auto c = config; + c.block_id = make_uint3(i, j, k); + (*_kernel_entry)(data, &c); + } + } + } - pool.parallel(dispatch_counts.x, dispatch_counts.y, dispatch_counts.z, - [this, config, data](auto bx, auto by, auto bz) noexcept { - auto c = config; - c.block_id = make_uint3(bx, by, bz); - (*_kernel_entry)(data, &c); - }); - pool.barrier(); +// pool.parallel(dispatch_counts.x, dispatch_counts.y, dispatch_counts.z, +// [this, config, data](auto bx, auto by, auto bz) noexcept { +// auto c = config; +// c.block_id = make_uint3(bx, by, bz); +// (*_kernel_entry)(data, &c); +// }); +// pool.barrier(); } void compute::fallback::FallbackShader::build_bound_arguments(compute::Function kernel) { _bound_arguments.reserve(kernel.bound_arguments().size()); diff --git a/src/backends/fallback/fallback_stream.cpp b/src/backends/fallback/fallback_stream.cpp index aced9e5f5..dd32552e8 100644 --- a/src/backends/fallback/fallback_stream.cpp +++ b/src/backends/fallback/fallback_stream.cpp @@ -9,6 +9,7 @@ #include "fallback_mesh.h" #include "fallback_texture.h" #include "fallback_shader.h" +#include "fallback_buffer.h" namespace luisa::compute::fallback { @@ -45,15 +46,15 @@ void FallbackStream::visit(const BufferUploadCommand *command) noexcept { std::memcpy(temp_buffer->data(), command->data(), command->size()); _pool.async([src = std::move(temp_buffer), buffer = command->handle(), offset = command->offset()] { - auto dst = reinterpret_cast(buffer + offset); + auto dst = reinterpret_cast(buffer)->view(offset).ptr; std::memcpy(dst, src->data(), src->size()); }); _pool.barrier(); } void FallbackStream::visit(const BufferDownloadCommand *command) noexcept { - _pool.async([cmd = *command] { - auto src = reinterpret_cast(cmd.handle() + cmd.offset()); + _pool.async([cmd = *command, buffer = command->handle(), offset = command->offset()] { + auto src = reinterpret_cast(buffer)->view(offset).ptr; std::memcpy(cmd.data(), src, cmd.size()); }); _pool.barrier(); @@ -61,8 +62,8 @@ void FallbackStream::visit(const BufferDownloadCommand *command) noexcept { void FallbackStream::visit(const BufferCopyCommand *command) noexcept { _pool.async([cmd = *command] { - auto src = reinterpret_cast(cmd.src_handle() + cmd.src_offset()); - auto dst = reinterpret_cast(cmd.dst_handle() + cmd.dst_offset()); + auto src = reinterpret_cast(cmd.src_handle())->view(cmd.src_offset()).ptr; + auto dst = reinterpret_cast(cmd.dst_handle())->view(cmd.dst_offset()).ptr; std::memcpy(dst, src, cmd.size()); }); _pool.barrier();