Skip to content

Commit

Permalink
Optimize pinning of values and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Sep 4, 2024
1 parent 95f8767 commit 678a405
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/Aardvark.Rendering.GL/Resources/Buffers/Buffer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module BufferExtensions =
member x.CreateBuffer(data : Array, [<Optional; DefaultParameterValue(BufferStorage.Device)>] storage : BufferStorage) =
let size = nativeint (data.GetType().GetElementType().GLSize) * nativeint data.Length

pinned data (fun src ->
data |> NativeInt.pin (fun src ->
x.CreateBuffer(src, size, storage)
)

Expand Down
2 changes: 1 addition & 1 deletion src/Aardvark.Rendering.GL/Resources/Pointers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module PointerContextExtensions =
| :? C4b as c -> c.ToArray()
| _ -> value

pinned value (fun pValue ->
value |> NativeInt.pin (fun pValue ->
let mutable pSrc = pValue

for r = 0 to dim.Y - 1 do
Expand Down
16 changes: 8 additions & 8 deletions src/Aardvark.Rendering.GL/Resources/Textures/TextureDownload.fs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ module internal TextureDownloadImplementation =
let pixelType = PixelType.ofType image.PixFormat.Type
let pixelFormat = PixelFormat.ofColFormat texture.Format.IsIntegerFormat image.Format

pinned image.Array (fun dst ->
image.Array |> NativeInt.pin (fun dst ->
let dstInfo = image.VolumeInfo.ToXYWTensor4'().AsBytes(elementSize)
downloadNative texture level slice offset size 0 elementSize pixelFormat pixelType dst dstInfo
)
Expand All @@ -182,22 +182,22 @@ module internal TextureDownloadImplementation =
let pixelType = PixelType.ofType volume.PixFormat.Type
let pixelFormat = PixelFormat.ofColFormat texture.Format.IsIntegerFormat volume.Format

pinned volume.Array (fun dst ->
volume.Array |> NativeInt.pin (fun dst ->
let dstInfo = volume.Tensor4Info.AsBytes(elementSize)
downloadNative texture level 0 offset volume.Size 0 elementSize pixelFormat pixelType dst dstInfo
)


let inline private copyUnsignedNormalizedDepth (matrix : Matrix<float32>) (shift : int) (maxValue : ^T)
(channels : int) (elementSize : int) (alignedLineSize : nativeint) (src : nativeint) =
pinned matrix.Array (fun dst ->
matrix.Data |> NativePtr.pinArr (fun dst ->
let src =
let info = Tensor4Info.deviceLayout true elementSize alignedLineSize channels matrix.Size.XYI
src |> NativeTensor4.ofNativeInt<uint8> info

let dst =
let info = matrix.AsVolume().Info.ToXYWTensor4'().AsBytes<float32>()
dst |> NativeTensor4.ofNativeInt<uint8> info
dst.Address |> NativeTensor4.ofNativeInt<uint8> info

(src, dst) ||> NativeTensor4.iterPtr2 (fun _ src dst ->
let src : nativeptr<'T> = NativePtr.cast src
Expand Down Expand Up @@ -246,9 +246,9 @@ module internal TextureDownloadImplementation =
downloadGeneralPixelData texture level slice offset size format typ copy

| FloatingPointDepth (format, typ) ->
pinned matrix.Array (fun dst ->
matrix.Data |> NativePtr.pinArr (fun dst ->
let dstInfo = matrix.AsVolume().Info.ToXYWTensor4'().AsBytes<float32>()
downloadNative texture level slice offset.XYO size 0 sizeof<float32> format typ dst dstInfo
downloadNative texture level slice offset.XYO size 0 sizeof<float32> format typ dst.Address dstInfo
)

| fmt -> failwithf "[GL] %A is not a supported depth format" fmt
Expand All @@ -265,9 +265,9 @@ module internal TextureDownloadImplementation =

match texture.Format with
| Stencil(format, typ, byteOffset) ->
pinned matrix.Array (fun dst ->
matrix.Data |> NativePtr.pinArr (fun dst ->
let dstInfo = matrix.AsVolume().Info.ToXYWTensor4'().AsBytes<int>()
downloadNative texture level slice offset.XYO size byteOffset sizeof<uint8> format typ dst dstInfo
downloadNative texture level slice offset.XYO size byteOffset sizeof<uint8> format typ dst.Address dstInfo
)

| fmt -> failwithf "[GL] %A is not a supported stencil format" fmt
Expand Down
28 changes: 14 additions & 14 deletions src/Aardvark.Rendering.Vulkan/Core/Device.fs
Original file line number Diff line number Diff line change
Expand Up @@ -768,14 +768,14 @@ and CopyEngine(family : DeviceQueueFamily) =
stream.Run cmd.Handle
cmd.End()

cmd.Handle |> pin (fun pCmd ->
cmd.Handle |> NativePtr.pin (fun pCmd ->
let submit =
VkSubmitInfo(
0u, NativePtr.zero, NativePtr.zero,
1u, pCmd,
0u, NativePtr.zero
)
submit |> pin (fun pSubmit ->
submit |> NativePtr.pin (fun pSubmit ->
VkRaw.vkQueueSubmit(queue.Handle, 1u, pSubmit, fence.Handle) |> ignore
)
)
Expand Down Expand Up @@ -1004,7 +1004,7 @@ and [<AbstractClass>] Resource<'T when 'T : unmanaged and 'T : equality> =
base.IsValid && x.handle <> Unchecked.defaultof<_>

new(device : Device, handle : 'T, refCount : int) =
handle |> pin device.Instance.RegisterDebugTrace
handle |> NativePtr.pin device.Instance.RegisterDebugTrace
{ inherit Resource(device, refCount); handle = handle }

new(device : Device, handle : 'T) =
Expand Down Expand Up @@ -1109,7 +1109,7 @@ and DeviceQueue internal(device : Device, deviceHandle : VkDevice, familyInfo :
uint32 cmds.Length, pCmdMasks,
signalCount, pSignalIndices
)
ext |> pin (fun pExt ->
ext |> NativePtr.pin (fun pExt ->

let submit =
VkSubmitInfo(
Expand All @@ -1118,7 +1118,7 @@ and DeviceQueue internal(device : Device, deviceHandle : VkDevice, familyInfo :
uint32 cmds.Length, pCmds,
uint32 signal.Length, pSignal
)
submit |> pin (fun pSubmit ->
submit |> NativePtr.pin (fun pSubmit ->
VkRaw.vkQueueSubmit(handle, 1u, pSubmit, fence)
|> check "could not submit command buffer"
)
Expand All @@ -1132,7 +1132,7 @@ and DeviceQueue internal(device : Device, deviceHandle : VkDevice, familyInfo :
uint32 signal.Length, pSignal
)

submit |> pin (fun pSubmit ->
submit |> NativePtr.pin (fun pSubmit ->
VkRaw.vkQueueSubmit(handle, 1u, pSubmit, fence)
|> check "could not submit command buffer"
)
Expand Down Expand Up @@ -1400,7 +1400,7 @@ and DeviceCommandPool internal(device : Device, index : int, queueFamily : Devic
uint32 index
)
let handle =
createInfo |> pin (fun pCreate ->
createInfo |> NativePtr.pin (fun pCreate ->
temporary<VkCommandPool,VkCommandPool> (fun pHandle ->
VkRaw.vkCreateCommandPool(device.Handle, pCreate, NativePtr.zero, pHandle)
|> check "could not create command pool"
Expand Down Expand Up @@ -1453,7 +1453,7 @@ and CommandPool internal(device : Device, familyIndex : int, queueFamily : Devic
flags |> int |> unbox,
uint32 familyIndex
)
createInfo |> pin (fun pCreate ->
createInfo |> NativePtr.pin (fun pCreate ->
temporary<VkCommandPool, VkCommandPool> (fun pHandle ->
VkRaw.vkCreateCommandPool(device.Handle, pCreate, NativePtr.zero, pHandle)
|> check "could not create command pool"
Expand Down Expand Up @@ -1685,7 +1685,7 @@ and CommandBuffer internal(device : Device, pool : VkCommandPool, queueFamily :
if handle <> 0n && device.Handle <> 0n then
releaseResources()

handle |> pin (fun pHandle -> VkRaw.vkFreeCommandBuffers(device.Handle, pool, 1u, pHandle))
handle |> NativePtr.pin (fun pHandle -> VkRaw.vkFreeCommandBuffers(device.Handle, pool, 1u, pHandle))
handle <- 0n

interface IDisposable with
Expand Down Expand Up @@ -1794,7 +1794,7 @@ and Semaphore internal(device : Device) =
let mutable handle =
let info = VkSemaphoreCreateInfo.Empty

info |> pin (fun pInfo ->
info |> NativePtr.pin (fun pInfo ->
temporary<VkSemaphore, VkSemaphore> (fun pHandle ->
VkRaw.vkCreateSemaphore(device.Handle, pInfo, NativePtr.zero, pHandle)
|> check "could not create semaphore"
Expand Down Expand Up @@ -1826,7 +1826,7 @@ and Event internal(device : Device) =
let mutable handle =
let info = VkEventCreateInfo.Empty

info |> pin (fun pInfo ->
info |> NativePtr.pin (fun pInfo ->
temporary<VkEvent, VkEvent> (fun pHandle ->
VkRaw.vkCreateEvent(device.Handle, pInfo, NativePtr.zero, pHandle)
|> check "could not create event"
Expand Down Expand Up @@ -1880,7 +1880,7 @@ and DeviceHeap internal(device : Device, physical : PhysicalDevice, memory : Mem
)

let mem =
info |> pin (fun pInfo ->
info |> NativePtr.pin (fun pInfo ->
temporary<VkDeviceMemory, VkDeviceMemory> (fun pHandle ->
VkRaw.vkAllocateMemory(device.Handle, pInfo, NativePtr.zero, pHandle)
|> check "could not 'allocate' null pointer for device heap"
Expand Down Expand Up @@ -2360,7 +2360,7 @@ and [<AllowNullLiteral>] DevicePtr internal(memory : DeviceMemory, offset : int6
finally
if not memory.Heap.IsHostCoherent then
let range = VkMappedMemoryRange(memory.Handle, uint64 x.Offset, uint64 x.Size)
range |> pin (fun pRange ->
range |> NativePtr.pin (fun pRange ->
VkRaw.vkFlushMappedMemoryRanges(device.Handle, 1u, pRange)
|> check "could not flush memory range"
)
Expand Down Expand Up @@ -2725,7 +2725,7 @@ and DeviceQueueThread(family : DeviceQueueFamily) =
uint32 imageBinds.Length, pImageBindInfos,
0u, NativePtr.zero
)
bindInfo |> pin (fun pInfo ->
bindInfo |> NativePtr.pin (fun pInfo ->
VkRaw.vkQueueBindSparse(queue.Handle, 1u, pInfo, fence.Handle)
|> check "could not bind sparse memory"
)
Expand Down
8 changes: 4 additions & 4 deletions src/Aardvark.Rendering.Vulkan/Core/Platform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,13 @@ and PhysicalDevice internal(instance : Instance, handle : VkPhysicalDevice) =
if apiVersion >= Version(1,1,0) || hasExtension KHRMaintenance3.Name then
let main3 =
VkPhysicalDeviceMaintenance3Properties(10u, 10UL)
main3 |> pin (fun pMain3 ->
main3 |> NativePtr.pin (fun pMain3 ->
let props =
VkPhysicalDeviceProperties2(
NativePtr.toNativeInt pMain3,
VkPhysicalDeviceProperties()
)
props |> pin (fun pProps ->
props |> NativePtr.pin (fun pProps ->
VkRaw.vkGetPhysicalDeviceProperties2(handle, pProps)
let props = NativePtr.read pProps
let main3 = NativePtr.read pMain3
Expand All @@ -572,13 +572,13 @@ and PhysicalDevice internal(instance : Instance, handle : VkPhysicalDevice) =
0u,
0u
)
id |> pin (fun pId ->
id |> NativePtr.pin (fun pId ->
let khrProps =
VkPhysicalDeviceProperties2KHR(
NativePtr.toNativeInt pId,
VkPhysicalDeviceProperties()
)
khrProps |> pin (fun pProps ->
khrProps |> NativePtr.pin (fun pProps ->
VkRaw.vkGetPhysicalDeviceProperties2(handle, pProps)
let id = NativePtr.read pId
let uid = sprintf "{ GUID = %A; Mask = %d }" id.deviceUUID id.deviceNodeMask
Expand Down
13 changes: 3 additions & 10 deletions src/Aardvark.Rendering.Vulkan/Core/Utilities.fs
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,9 @@ module private Utilities =
let inline (<!-) (ptr : nativeptr<'a>) (v : 'a) = NativePtr.write ptr v

let temporary<'a, 'r when 'a : unmanaged> (f : nativeptr<'a> -> 'r) =
native {
let! ptr = Unchecked.defaultof<'a>
return f ptr
}

let pin (f : nativeptr<'a> -> 'r) (v : 'a) =
native {
let! ptr = v
return f ptr
}
let value = Unchecked.defaultof<'a>
use ptr = fixed &value
f ptr

let check (str : string) (err : VkResult) =
if err <> VkResult.Success then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module IndirectBuffer =

let size = nativeint ab.Data.LongLength * nativeint sizeof<DrawCallInfo>

pinned ab.Data (fun src ->
ab.Data |> NativeInt.pin (fun src ->
device.DeviceMemory |> Buffer.ofWriter false flags size (fun dst ->
copy swap src dst ab.Data.Length
)
Expand Down
26 changes: 13 additions & 13 deletions src/Aardvark.Rendering.Vulkan/Resources/Image/Image.fs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ module ``Image Command Extensions`` =
img.VkImageSubresourceRange
)

imageMemoryBarrier |> pin (fun pBarrier ->
imageMemoryBarrier |> NativePtr.pin (fun pBarrier ->
VkRaw.vkCmdPipelineBarrier(
cmd.Handle,
srcStage, dstStage,
Expand Down Expand Up @@ -488,7 +488,7 @@ module ``Image Command Extensions`` =
)

cmd.AppendCommand()
copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyImage(cmd.Handle, src.Image.Handle, src.Image.Layout, dst.Image.Handle, dst.Image.Layout, 1u, pCopy)
)

Expand Down Expand Up @@ -535,7 +535,7 @@ module ``Image Command Extensions`` =
)

cmd.AppendCommand()
copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyBufferToImage(cmd.Handle, src.Handle, dst.Image.Handle, dst.Image.Layout, 1u, pCopy)
)

Expand All @@ -557,7 +557,7 @@ module ``Image Command Extensions`` =
)

cmd.AppendCommand()
copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyImageToBuffer(cmd.Handle, src.Image.Handle, src.Image.Layout, dst.Handle, 1u, pCopy)
)

Expand Down Expand Up @@ -587,7 +587,7 @@ module ``Image Command Extensions`` =
)

cmd.AppendCommand()
resolve |> pin (fun pResolve ->
resolve |> NativePtr.pin (fun pResolve ->
VkRaw.vkCmdResolveImage(cmd.Handle, src.Image.Handle, srcLayout, dst.Image.Handle, dstLayout, 1u, pResolve)
)

Expand Down Expand Up @@ -668,7 +668,7 @@ module ``Image Command Extensions`` =
)

cmd.AppendCommand()
blit |> pin (fun pBlit ->
blit |> NativePtr.pin (fun pBlit ->
VkRaw.vkCmdBlitImage(cmd.Handle, src.Image.Handle, srcLayout, dst.Image.Handle, dstLayout, 1u, pBlit, filter)
)

Expand Down Expand Up @@ -804,7 +804,7 @@ module ``Image Command Extensions`` =
let imgSize = int64 copy.extent.width * int64 copy.extent.height * int64 copy.extent.depth * 4L
totalSize <- totalSize + imgSize * int64 copy.srcSubresource.layerCount

copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyImage(
cmd.Handle,
baseImage.Handle, VkImageLayout.TransferSrcOptimal,
Expand All @@ -820,7 +820,7 @@ module ``Image Command Extensions`` =
VkAccessFlags.TransferWriteBit,
VkAccessFlags.TransferReadBit ||| VkAccessFlags.TransferWriteBit
)
mem |> pin (fun pMem ->
mem |> NativePtr.pin (fun pMem ->
VkRaw.vkCmdPipelineBarrier(
cmd.Handle,
VkPipelineStageFlags.TransferBit,
Expand Down Expand Up @@ -893,8 +893,8 @@ module ``Image Command Extensions`` =
VkClearColorValue(float32 = color.Float)

let range = img.VkImageSubresourceRange
clearValue |> pin (fun pClear ->
range |> pin (fun pRange ->
clearValue |> NativePtr.pin (fun pClear ->
range |> NativePtr.pin (fun pRange ->
cmd.AppendCommand()
VkRaw.vkCmdClearColorImage(cmd.Handle, img.Image.Handle, VkImageLayout.TransferDstOptimal, pClear, 1u, pRange)
)
Expand All @@ -919,8 +919,8 @@ module ``Image Command Extensions`` =

let mutable clearValue = VkClearDepthStencilValue(depth, stencil)
let mutable range = img.VkImageSubresourceRange
clearValue |> pin (fun pClear ->
range |> pin (fun pRange ->
clearValue |> NativePtr.pin (fun pClear ->
range |> NativePtr.pin (fun pRange ->
cmd.AppendCommand()
VkRaw.vkCmdClearDepthStencilImage(cmd.Handle, img.Image.Handle, VkImageLayout.TransferDstOptimal, pClear, 1u, pRange)
)
Expand Down Expand Up @@ -988,7 +988,7 @@ module Image =

let mutable handle =
temporary (fun pHandle ->
info |> pin (fun pInfo ->
info |> NativePtr.pin (fun pInfo ->
VkRaw.vkCreateImage(device.Handle, pInfo, NativePtr.zero, pHandle)
|> check "could not create image"
NativePtr.read pHandle
Expand Down
4 changes: 2 additions & 2 deletions src/Aardvark.Rendering.Vulkan/Resources/Image/TensorImage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ module TensorImageCommandExtensions =
)

cmd.AppendCommand()
copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyBufferToImage(cmd.Handle, src.Buffer.Handle, dst.Image.Handle, dst.Image.Layout, 1u, pCopy)
)

Expand Down Expand Up @@ -471,7 +471,7 @@ module TensorImageCommandExtensions =
)

cmd.AppendCommand()
copy |> pin (fun pCopy ->
copy |> NativePtr.pin (fun pCopy ->
VkRaw.vkCmdCopyImageToBuffer(cmd.Handle, src.Image.Handle, src.Image.Layout, dst.Buffer.Handle, 1u, pCopy)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ module private ShaderBindingTableUtilities =

let shaderHandles : uint8[] = Array.zeroCreate <| groupCount * (int size)

pinned shaderHandles (fun ptr ->
shaderHandles |> NativePtr.pinArr (fun ptr ->
VkRaw.vkGetRayTracingShaderGroupHandlesKHR(
pipeline.Device.Handle, pipeline.Handle, 0u, uint32 groupCount, uint64 shaderHandles.Length, ptr
pipeline.Device.Handle, pipeline.Handle, 0u, uint32 groupCount, uint64 shaderHandles.Length, ptr.Address
)
|> check "[Raytracing] Failed to get shader group handles"
)
Expand Down
Loading

0 comments on commit 678a405

Please sign in to comment.