diff --git a/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm b/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm index 1e6d164e267be..eaa053585e06a 100644 --- a/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm +++ b/GPU/GPUTracking/Base/metal/GPUReconstructionMetal.mm @@ -203,6 +203,7 @@ uint32_t metalGPUCoreCount() } else { auto* master = dynamic_cast(mMaster); mWarpSize = master->mWarpSize; + mMultiprocessorCount = master->mMultiprocessorCount; mMaxBackendThreads = master->mMaxBackendThreads; mDeviceName = master->mDeviceName; mDeviceConstantMem = master->mDeviceConstantMem; @@ -282,21 +283,25 @@ uint32_t metalGPUCoreCount() { // Where a pointer lives as far as the backend is concerned. mDeviceMemoryBase is // a GPU address, so the CPU-visible address of owned memory has to be taken from -// the buffer rather than from the pointer itself. +// the buffer rather than from the pointer itself. The offsets are into the buffers, +// not from the instance's bases: a slave's bases start after the master's +// permanent memory, but it shares the master's buffers. struct MetalMemRef { id buffer; // nil for memory the backend does not own size_t offset; char* cpu; }; -MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const void* p, const void* hostBase, size_t hostSize, const void* devBase, size_t devSize) +MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const void* p) { - if (p >= hostBase && p < ((const char*)hostBase + hostSize)) { - const size_t o = (const char*)p - (const char*)hostBase; + const char* hostBase = (const char*)internals->mem_host.contents; + const char* devBase = (const char*)(uintptr_t)internals->mem_gpu.gpuAddress; + if (p >= hostBase && p < hostBase + internals->mem_host.allocatedSize) { + const size_t o = (const char*)p - hostBase; return {internals->mem_host, o, (char*)internals->mem_host.contents + o}; } - if (p >= devBase && p < ((const char*)devBase + devSize)) { - const size_t o = (const char*)p - (const char*)devBase; + if (p >= devBase && p < devBase + internals->mem_gpu.allocatedSize) { + const size_t o = (const char*)p - devBase; return {internals->mem_gpu, o, (char*)internals->mem_gpu.contents + o}; } return {nil, 0, (char*)p}; @@ -320,8 +325,8 @@ MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const if (nEvents) { StreamWaitForEvents(realStream, evList, nEvents); } - const MetalMemRef s = metalResolve(mInternals, src, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); - const MetalMemRef d = metalResolve(mInternals, dst, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); + const MetalMemRef s = metalResolve(mInternals, src); + const MetalMemRef d = metalResolve(mInternals, dst); if (s.buffer == nil || d.buffer == nil) { // A blit moves data between MTLBuffers only, and the calibration objects @@ -361,7 +366,7 @@ MetalMemRef metalResolve(const GPUReconstructionMetalInternals* internals, const } auto realStream = stream == -1 ? 0 : stream; - const MetalMemRef s = metalResolve(mInternals, src, mHostMemoryBase, mHostMemorySize, mDeviceMemoryBase, mDeviceMemorySize); + const MetalMemRef s = metalResolve(mInternals, src); if (s.buffer == nil) { DrainStream(realStream); diff --git a/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc b/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc index 5f456a7ce06bb..26779dcc2805a 100644 --- a/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc +++ b/GPU/GPUTracking/Base/metal/GPUReconstructionMetalKernelsSpecialize.inc @@ -14,7 +14,8 @@ template <> inline void GPUReconstructionMetal::runKernelBackend(const krnlSetupTime& _xyz, void* const& ptr, uint64_t const& size) { - const uint64_t offset = static_cast(ptr) - static_cast(mDeviceMemoryBase); + // Into mem_gpu, not from mDeviceMemoryBase: a slave shares the buffer, but its base is the end of the master's permanent memory + const uint64_t offset = (uint64_t)(uintptr_t)ptr - mInternals->mem_gpu.gpuAddress; const uint64_t length = (size + 15ull) & ~15ull; if (_xyz.z.evList) { StreamWaitForEvents(_xyz.x.stream, _xyz.z.evList, _xyz.z.nEvents);