DoubleNumber32ShaderCode
Package: Libraries.Compute.Hardware.Shaders · Inherits from: Libraries.Language.Object
The DoubleNumber32ShaderCode class gives the GLSL a compute shader needs to work with Quorum's 64-bit numbers on any graphics card, in HardwareCompute's DOUBLE_NUMBER_32 format: each number as two 32-bit numbers, high and low (a vec2 in GLSL), about 48 bits. Graphics cards without 64-bit numbers, such as Apple's, get close to 64-bit precision this way. GetBufferDeclaration the storage buffer, "buff", as 32-bit numbers GetLoadAndStore loadNumber(i) and storeNumber(i, value): number i of the buffer, 32-bit numbers 2i and 2i + 1 GetArithmetic multiplyNumbers(a, b) and addNumbers(a, b), and the error-free steps they are made of The arithmetic keeps each operation's rounding error: twoProduct uses fma, which gives a product's error exactly, and every sum is renormalized so the low part stays small. Every 32-bit number in it is marked precise (SPIR-V NoContraction), so a compiler running with fast math, as MoltenVK does for Metal, may not reorder, simplify or fuse it; without that, b - (s - a) can be "simplified" to 0 and the shader quietly loses half its precision.
Example Code
use Libraries.Compute.Hardware.Shaders.DoubleNumber32ShaderCode
// squares every number in the buffer
DoubleNumber32ShaderCode pairs
text code = "#version 450
" + pairs:GetBufferDeclaration(0) + "
layout (local_size_x = 64) in;
" + pairs:GetLoadAndStore() + pairs:GetArithmetic() + "
void main()
{
uint i = gl_GlobalInvocationID.x;
vec2 value = loadNumber(i);
storeNumber(i, multiplyNumbers(value, value));
}
"
Actions
GetArithmetic()
This action returns the GLSL functions for arithmetic on numbers held as two 32-bit numbers: multiplyNumbers(a, b) and addNumbers(a, b), each taking and returning a vec2 (high, low), and the error-free steps they are made of (quickTwoSum, twoSum, twoProduct). Each keeps its rounding error, and every step is marked precise.
Returns
text: the GLSL functions.
Example
use Libraries.Compute.Hardware.Shaders.DoubleNumber32ShaderCode
DoubleNumber32ShaderCode pairs
text code = pairs:GetArithmetic()
GetBufferDeclaration(integer binding)
This action returns the GLSL for the storage buffer the other actions' code uses: named buff, read and written as 32-bit numbers (buff.data).
Parameters
bindinginteger: the buffer's binding in descriptor set 0.
Returns
text: the GLSL declaration.
Example
use Libraries.Compute.Hardware.Shaders.DoubleNumber32ShaderCode
DoubleNumber32ShaderCode pairs
text declaration = pairs:GetBufferDeclaration(0)
GetLoadAndStore()
This action returns the GLSL functions loadNumber(i), which reads number i of the buffer as a vec2 (32-bit numbers 2i and 2i + 1, high and low), and storeNumber(i, value), which writes one. It needs the buffer from GetBufferDeclaration.
Returns
text: the GLSL functions.
Example
use Libraries.Compute.Hardware.Shaders.DoubleNumber32ShaderCode
DoubleNumber32ShaderCode pairs
text code = pairs:GetBufferDeclaration(0) + pairs:GetLoadAndStore()