From 4c90d37ea90eca51873fa33ee75e1b9971e7c4ba Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Thu, 26 Dec 2024 21:29:14 +0800 Subject: [PATCH 1/2] allow null-size update buffer --- src/backends/metal/metal_accel.cpp | 14 +++++++------- src/backends/metal/metal_primitive.cpp | 13 +++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/backends/metal/metal_accel.cpp b/src/backends/metal/metal_accel.cpp index c49fde0c5..9be62c2b4 100644 --- a/src/backends/metal/metal_accel.cpp +++ b/src/backends/metal/metal_accel.cpp @@ -146,11 +146,10 @@ void MetalAccel::_do_update(MetalCommandEncoder &encoder) noexcept { LUISA_ASSERT(_handle != nullptr, "Acceleration structure is not built."); LUISA_ASSERT(_descriptor != nullptr, "Descriptor is not allocated."); LUISA_ASSERT(_instance_buffer != nullptr, "Instance buffer is not allocated."); - LUISA_ASSERT(_update_buffer != nullptr, "Update buffer is not allocated."); auto command_encoder = encoder.command_buffer()->accelerationStructureCommandEncoder(); _descriptor->retain(); _handle->retain(); - _update_buffer->retain(); + if (_update_buffer != nullptr) { _update_buffer->retain(); } command_encoder->refitAccelerationStructure(_handle, _descriptor, _handle, _update_buffer, 0u); command_encoder->endEncoding(); encoder.add_callback(FunctionCallbackContext::create([descriptor = _descriptor, @@ -158,7 +157,7 @@ void MetalAccel::_do_update(MetalCommandEncoder &encoder) noexcept { update_buffer = _update_buffer] { descriptor->release(); handle->release(); - update_buffer->release(); + if (update_buffer != nullptr) { update_buffer->release(); } })); } @@ -171,9 +170,11 @@ void MetalAccel::_do_build(MetalCommandEncoder &encoder) noexcept { if (_update_buffer == nullptr || _update_buffer->length() < sizes.refitScratchBufferSize) { if (_update_buffer != nullptr) { _update_buffer->release(); } - _update_buffer = device->newBuffer(sizes.refitScratchBufferSize, - MTL::ResourceStorageModePrivate | - MTL::ResourceHazardTrackingModeTracked); + if (sizes.refitScratchBufferSize != 0) { + _update_buffer = device->newBuffer(sizes.refitScratchBufferSize, + MTL::ResourceStorageModePrivate | + MTL::ResourceHazardTrackingModeTracked); + } } } if (_handle != nullptr) { _handle->release(); } @@ -269,4 +270,3 @@ void MetalAccel::mark_resource_usages(MetalCommandEncoder &encoder, } }// namespace luisa::compute::metal - diff --git a/src/backends/metal/metal_primitive.cpp b/src/backends/metal/metal_primitive.cpp index 86a35138f..452b3b451 100644 --- a/src/backends/metal/metal_primitive.cpp +++ b/src/backends/metal/metal_primitive.cpp @@ -42,9 +42,11 @@ void MetalPrimitive::_do_build(MetalCommandEncoder &encoder, if (_update_buffer == nullptr || _update_buffer->length() < sizes.refitScratchBufferSize) { if (_update_buffer != nullptr) { _update_buffer->release(); } - _update_buffer = device->newBuffer(sizes.refitScratchBufferSize, - MTL::ResourceHazardTrackingModeTracked | - MTL::ResourceStorageModePrivate); + if (sizes.refitScratchBufferSize != 0) { + _update_buffer = device->newBuffer(sizes.refitScratchBufferSize, + MTL::ResourceHazardTrackingModeTracked | + MTL::ResourceStorageModePrivate); + } } } if (_handle != nullptr) { _handle->release(); } @@ -99,12 +101,11 @@ void MetalPrimitive::_do_update(MetalCommandEncoder &encoder, MTL::PrimitiveAccelerationStructureDescriptor *descriptor) noexcept { LUISA_ASSERT(_handle != nullptr, "Acceleration structure not built yet."); - LUISA_ASSERT(_update_buffer != nullptr, "Invalid acceleration structure update buffer."); LUISA_ASSERT(descriptor != nullptr, "Invalid acceleration structure descriptor."); auto refit_encoder = encoder.command_buffer()->accelerationStructureCommandEncoder(); _handle->retain(); - _update_buffer->retain(); + if (_update_buffer != nullptr) { _update_buffer->retain(); } descriptor->retain(); refit_encoder->refitAccelerationStructure(_handle, descriptor, _handle, _update_buffer, 0u); refit_encoder->endEncoding(); @@ -112,8 +113,8 @@ void MetalPrimitive::_do_update(MetalCommandEncoder &encoder, update_buffer = _update_buffer, descriptor] { handle->release(); - update_buffer->release(); descriptor->release(); + if (update_buffer != nullptr) { update_buffer->release(); } })); } From 6137bf4e2f72dfa7f4416636ff2dbe944ea51309 Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Fri, 27 Dec 2024 14:15:13 +0800 Subject: [PATCH 2/2] fix bindless buffer on metal --- src/backends/metal/metal_codegen_ast.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/backends/metal/metal_codegen_ast.cpp b/src/backends/metal/metal_codegen_ast.cpp index 0998a7620..1757a1357 100644 --- a/src/backends/metal/metal_codegen_ast.cpp +++ b/src/backends/metal/metal_codegen_ast.cpp @@ -231,7 +231,7 @@ void MetalCodegenAST::_emit_type_decls(Function kernel) noexcept { // process types in topological order types.clear(); auto emit = [&](auto &&self, auto type) noexcept -> void { - if (types.emplace(type).second) { + if (type != nullptr && types.emplace(type).second) { if (type->is_array() || type->is_buffer()) { self(self, type->element()); } else if (type->is_structure()) { @@ -960,7 +960,12 @@ void MetalCodegenAST::visit(const CallExpr *expr) noexcept { case CallOp::BUFFER_READ: _scratch << "buffer_read"; break; case CallOp::BUFFER_WRITE: _scratch << "buffer_write"; break; case CallOp::BUFFER_SIZE: _scratch << "buffer_size"; break; - case CallOp::BYTE_BUFFER_READ: _scratch << "byte_buffer_read"; break; + case CallOp::BYTE_BUFFER_READ: { + _scratch << "byte_buffer_read<"; + _emit_type_name(expr->type()); + _scratch << ">"; + break; + } case CallOp::BYTE_BUFFER_WRITE: _scratch << "byte_buffer_write"; break; case CallOp::BYTE_BUFFER_SIZE: _scratch << "byte_buffer_size"; break; case CallOp::TEXTURE_READ: _scratch << "texture_read"; break; @@ -1137,6 +1142,15 @@ void MetalCodegenAST::visit(const CallExpr *expr) noexcept { case CallOp::TEXTURE3D_SAMPLE_LEVEL: LUISA_NOT_IMPLEMENTED(); case CallOp::TEXTURE3D_SAMPLE_GRAD: LUISA_NOT_IMPLEMENTED(); case CallOp::TEXTURE3D_SAMPLE_GRAD_LEVEL: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE2D_SAMPLE_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE2D_SAMPLE_LEVEL_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE2D_SAMPLE_GRAD_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE2D_SAMPLE_GRAD_LEVEL_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE3D_SAMPLE_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE3D_SAMPLE_LEVEL_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE3D_SAMPLE_GRAD_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::BINDLESS_TEXTURE3D_SAMPLE_GRAD_LEVEL_SAMPLER: LUISA_NOT_IMPLEMENTED(); + case CallOp::CLOCK: LUISA_NOT_IMPLEMENTED(); } _scratch << "("; if (auto op = expr->op(); is_atomic_operation(op)) {