VulkanCompute
Package: Libraries.Compute.Hardware.Vulkan · Inherits from: Libraries.Compute.Hardware.HardwareCompute, Libraries.Language.Object
The VulkanCompute class is one computation on a VulkanComputeDevice (see HardwareCompute): its own program, data and commands on the device every computation in the program shares. VulkanComputeDevice:CreateCompute makes it, ready for SetProgram; Dispose releases what it made and leaves the device. It makes its own queue, command pool, command buffer and fence, so it never records into or waits on a game's frame, even on a game's device. Everything runs on the calling thread, as the game does, so sharing the game's queue needs no locking: a run finishes (it waits on its own fence) before the game records its next frame. Runs added between BeginRuns and FinishRuns go into one command buffer, with a memory barrier between each and the next, so each run sees what the one before wrote; FinishRuns submits them once and waits once. A run's integers (SetRunIntegerCount) are Vulkan push constants: the program reads them as a push_constant block of uints. The buffer is mapped as 32-bit numbers (Number32BitArray data, and DOUBLE_NUMBER_32) or as 64-bit numbers (NUMBER_64), and is made again when the kind changes.
Example Code
use Libraries.Game.GameStateManager
use Libraries.Compute.Hardware.HardwareComputeDevice
use Libraries.Compute.Hardware.HardwareCompute
use Libraries.Containers.Number64BitArray
GameStateManager manager
HardwareComputeDevice device = manager:GetComputeDevice()
if device not= undefined
// a VulkanCompute when the device is Vulkan
HardwareCompute compute = device:CreateCompute()
compute:SetProgram("Doubler", shaderSource)
compute:SetData(numbers)
compute:Run(numbers:GetSize(), 1, 1)
Number64BitArray answer
answer:SetSize(numbers:GetSize())
compute:GetValues(0, answer)
compute:Dispose()
end
Actions
AddRun(integer x, integer y, integer z, Libraries.Containers.Array<integer> runIntegers)
This action records one run: after the first, a memory barrier (the run before must finish writing before this one reads, and finish reading before this one writes), then the run's push constants, then the dispatch.
Parameters
xinteger: the number of work groups across.yinteger: the number of work groups down.zinteger: the number of work groups deep.runIntegersLibraries.Containers.Array<integer>: this run's integers: as many as SetRunIntegerCount said, or undefined when that is 0.
Example
See the same action in HardwareCompute.
BeginRuns()
This action starts recording a group of runs into the command buffer: it makes the pipeline and descriptor set if they are needed, then binds them.
Example
See the same action in HardwareCompute.
Dispose()
This action waits until the device is idle, then releases what this computation made: the pipeline, pipeline cache, pipeline layout, descriptor set layout, shader module, buffer, fence, push constant buffer, command buffer and command pool. The device is shared and stays. After Dispose the computation can't be used again.
Example
See the same action in HardwareCompute.
FinishRuns()
This action ends the recording with a barrier that makes what the runs wrote visible to reading it back, submits the command buffer once, and waits once on the compute's fence (made on the first run, reset after).
Example
See the same action in HardwareCompute.
ForgetProgram()
This action forgets the compiled program and its pipeline, so the next SetProgram compiles again even for the same source. For measuring.
Example
See the same action in HardwareCompute.
GetCompileTime()
This action returns how long the last compile took: GLSL to SPIR-V, and the shader module.
Returns
number: the time in milliseconds.
Example
See the same action in HardwareCompute.
GetDevice()
This action returns the device this computation runs on.
Returns
Libraries.Compute.Hardware.HardwareComputeDevice: the VulkanComputeDevice, as a HardwareComputeDevice.
Example
See the same action in HardwareCompute.
GetPipelineTime()
This action returns how long the last vkCreateComputePipelines took, where MoltenVK makes and compiles Metal on a Mac.
Returns
number: the time in milliseconds.
Example
See the same action in HardwareCompute.
GetValue(integer index)
This action returns one number from the buffer: a 32-bit number, or, with NUMBER_64 data, a 64-bit number.
Parameters
indexinteger: which number, from 0.
Returns
number: the number.
Example
See the same action in HardwareCompute.
GetValues(integer start, Libraries.Containers.Number32BitArray into)
This action copies 32-bit numbers back from the buffer in one copy (VulkanNumber32BitMappedMemory's bulk Get).
Parameters
startinteger: the first 32-bit number to copy, from 0.intoLibraries.Containers.Number32BitArray: where the numbers go; its size says how many.
Example
See the same action in HardwareCompute.
GetValues(integer start, Libraries.Containers.Number64BitArray into)
This action copies Quorum's 64-bit numbers back from the buffer, read as the number format says. With DOUBLE_NUMBER_32 each number is its high and low 32-bit numbers added, which is exact in a 64-bit number. The joining is a Quorum loop, as cheap as the split in SetData. With NUMBER_64 the numbers are copied as they are, in one native copy (VulkanNumber64BitMappedMemory's bulk Get).
Parameters
startinteger: the first number to copy, from 0, counted in 64-bit numbers.intoLibraries.Containers.Number64BitArray: where the numbers go; its size says how many.
Example
See the same action in HardwareCompute.
Initialize(Libraries.Compute.Hardware.Vulkan.VulkanComputeDevice owner)
This action sets the computation up on a device: a queue that can compute, and its own command pool and command buffer. VulkanComputeDevice:CreateCompute calls it; programs don't need to.
Parameters
ownerLibraries.Compute.Hardware.Vulkan.VulkanComputeDevice: the started device to compute on.
Returns
boolean: true if the device has a queue that can compute.
Example
use Libraries.Compute.Hardware.Vulkan.VulkanCompute
use Libraries.Compute.Hardware.Vulkan.VulkanComputeDevice
VulkanComputeDevice owner
if owner:Start()
VulkanCompute compute
boolean made = compute:Initialize(owner)
end
Run(integer x, integer y, integer z)
This action runs the program over x by y by z work groups and waits for it to finish: BeginRuns, one AddRun and FinishRuns.
Parameters
xinteger: the number of work groups across.yinteger: the number of work groups down.zinteger: the number of work groups deep.
Example
See the same action in HardwareCompute.
SetData(Libraries.Containers.Number64BitArray data)
This action copies Quorum's 64-bit numbers to the graphics card, held as the number format says. With DOUBLE_NUMBER_32 each number is split into a high 32-bit number (the number rounded) and a low one (the rest, rounded). The split is a Quorum loop; measured, it costs a few milliseconds at most on 2,048 x 2,048 matrices, so no native version is needed. With NUMBER_64 the numbers are copied as they are, in one native copy.
Parameters
dataLibraries.Containers.Number64BitArray: the numbers.
Example
See the same action in HardwareCompute.
SetData(Libraries.Containers.Number32BitArray data)
This action copies 32-bit numbers to the graphics card, as they are, in one copy. The buffer, and the descriptor set pointing at it, are kept while the numbers fit; more than fit grow it to at least twice its size.
Parameters
dataLibraries.Containers.Number32BitArray: the numbers.
Example
See the same action in HardwareCompute.
SetProgram(text name, text code)
This action sets the program, a GLSL compute shader, and compiles it to SPIR-V and a shader module. The same name and source as the last one keep the compiled program and pipeline, so setting it again costs nothing. The pipeline is built on the first run after it.
Parameters
nametext: a name for the program, used in error messages.codetext: the program's GLSL source.
Example
See the same action in HardwareCompute.
SetProgramCacheFile(text path)
This action sets a file for the Vulkan pipeline cache: each pipeline starts from it and saves to it once built. On a Mac this saved nothing (building a pipeline takes under a millisecond), so it is off unless set.
Parameters
pathtext: the file's path; "" for none.
Example
See the same action in HardwareCompute.
SetRunIntegerCount(integer count)
This action sets how many integers each run is given, as Vulkan push constants: count * 4 bytes, for the compute stage. Changing it makes the pipeline layout, and so the pipeline, again on the next run.
Parameters
countinteger: how many integers each run is given; 0 for none.
Example
See the same action in HardwareCompute.