Skip to content

Commit

Permalink
Merge branch 'dev-fallback-backend' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Dec 27, 2024
2 parents 23af935 + 6137bf4 commit 30410d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/backends/metal/metal_accel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,18 @@ 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,
handle = _handle,
update_buffer = _update_buffer] {
descriptor->release();
handle->release();
update_buffer->release();
if (update_buffer != nullptr) { update_buffer->release(); }
}));
}

Expand All @@ -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(); }
Expand Down Expand Up @@ -269,4 +270,3 @@ void MetalAccel::mark_resource_usages(MetalCommandEncoder &encoder,
}

}// namespace luisa::compute::metal

18 changes: 16 additions & 2 deletions src/backends/metal/metal_codegen_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 7 additions & 6 deletions src/backends/metal/metal_primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -99,21 +101,20 @@ 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();
encoder.add_callback(FunctionCallbackContext::create([handle = _handle,
update_buffer = _update_buffer,
descriptor] {
handle->release();
update_buffer->release();
descriptor->release();
if (update_buffer != nullptr) { update_buffer->release(); }
}));
}

Expand Down

0 comments on commit 30410d0

Please sign in to comment.