Skip to content

Commit ffc5b41

Browse files
committed
More code
1 parent 7cf55e6 commit ffc5b41

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lib/mtl/command_enc/compute4.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ function dispatchThreads!(cce::MTL4ComputeCommandEncoder, threadsSize::MTLSize,
4444
end
4545

4646
# Copy Operations (Blit functionality integrated into compute encoder in Metal 4)
47-
# function append_copy!(cce::MTL4ComputeCommandEncoder, dst::MTLBuffer, dstOffset::Integer,
48-
# src::MTLBuffer, srcOffset::Integer, size::Integer)
49-
# @objc [cce::id{MTL4ComputeCommandEncoder} copyFromBuffer:src::id{MTLBuffer}
50-
# sourceOffset:srcOffset::NSUInteger
51-
# toBuffer:dst::id{MTLBuffer}
52-
# destinationOffset:dstOffset::NSUInteger
53-
# size:size::NSUInteger]::Nothing
54-
# end
47+
function append_copy!(cce::MTL4ComputeCommandEncoder, dst::MTLBuffer, dstOffset::Integer,
48+
src::MTLBuffer, srcOffset::Integer, size::Integer)
49+
@objc [cce::id{MTL4ComputeCommandEncoder} copyFromBuffer:src::id{MTLBuffer}
50+
sourceOffset:srcOffset::NSUInteger
51+
toBuffer:dst::id{MTLBuffer}
52+
destinationOffset:dstOffset::NSUInteger
53+
size:size::NSUInteger]::Nothing
54+
end
5555

5656
# function append_copy!(cce::MTL4ComputeCommandEncoder, dst::MTLTexture, dstSlice::Integer, dstLevel::Integer, dstOrigin::MTLOrigin,
5757
# src::MTLBuffer, srcOffset::Integer, srcBytesPerRow::Integer, srcBytesPerImage::Integer,

src/memory.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Base.convert(::Type{Ptr{T}}, ptr::MtlPtr) where {T} =
3131

3232
# CPU -> GPU
3333
function Base.unsafe_copyto!(dev::MTLDevice, dst::MtlPtr{T}, src::Ptr{T}, N::Integer;
34-
queue::MTLCommandQueue=global_queue(dev), async::Bool=false) where T
34+
queue=use_metal4() ? global_queue4(dev) : global_queue(dev), async::Bool=false) where T
3535
storage_type = dst.buffer.storageMode
3636
if storage_type == MTL.MTLStorageModePrivate
3737
# stage through a shared buffer
@@ -53,7 +53,7 @@ end
5353

5454
# GPU -> CPU
5555
function Base.unsafe_copyto!(dev::MTLDevice, dst::Ptr{T}, src::MtlPtr{T}, N::Integer;
56-
queue::MTLCommandQueue=global_queue(dev), async::Bool=false) where T
56+
queue=use_metal4() ? global_queue4(dev) : global_queue(dev), async::Bool=false) where T
5757
storage_type = src.buffer.storageMode
5858
if storage_type == MTL.MTLStorageModePrivate
5959
# stage through a shared buffer
@@ -88,15 +88,25 @@ end
8888
# GPU -> GPU
8989
@autoreleasepool function Base.unsafe_copyto!(dev::MTLDevice, dst::MtlPtr{T},
9090
src::MtlPtr{T}, N::Integer;
91-
queue::MTLCommandQueue=global_queue(dev),
91+
queue=use_metal4() ? global_queue4(dev) : global_queue(dev),
9292
async::Bool=false) where T
9393
if N > 0
94-
cmdbuf = MTLCommandBuffer(queue)
95-
MTLBlitCommandEncoder(cmdbuf) do enc
96-
append_copy!(enc, dst.buffer, dst.offset, src.buffer, src.offset, N * sizeof(T))
94+
if queue isa MTL4CommandQueue
95+
@info "MTL4"
96+
cmdbuf = MTL4CommandBuffer(dev; queue) do cmdbuf
97+
MTL4ComputeCommandEncoder(cmdbuf, !async) do enc
98+
append_copy!(enc, dst.buffer, dst.offset, src.buffer, src.offset, N * sizeof(T))
99+
end
100+
end
101+
else
102+
@info "MTL3"
103+
cmdbuf = MTLCommandBuffer(queue)
104+
MTLBlitCommandEncoder(cmdbuf) do enc
105+
append_copy!(enc, dst.buffer, dst.offset, src.buffer, src.offset, N * sizeof(T))
106+
end
107+
commit!(cmdbuf)
108+
async || wait_completed(cmdbuf)
97109
end
98-
commit!(cmdbuf)
99-
async || wait_completed(cmdbuf)
100110
end
101111
return dst
102112
end

src/state.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ end
100100
end
101101

102102
cmdbuf = commit!(cmdbuf, queue, allocator) do cmdbuf
103-
MTL4ComputeCommandEncoder(identity, cmdbuf, #=sync=#true)
103+
encoder = MTL4ComputeCommandEncoder(cmdbuf)
104+
MTL.barrierAfterQueueStages!(encoder)
105+
close(encoder)
104106
end
105107
return
106108
end

0 commit comments

Comments
 (0)