/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
set, make_check, exec_build, exec_unify, get_engine
} from "../../nova/core.mjs";
import {
GROUP_SIZE, get_pipeline
} from "./boot.mjs";
/******************************************************************/
/* Device Lifecycle */
/******************************************************************/
/**
* test_gpu_device_new(M, P): internal only
* The predicate succeeds in P with a promise that
* receives a GPU device into the map M.
*/
function test_gpu_device_new(args) {
let map = exec_build(args[0]);
return exec_unify(args[1],
gpu_device_promise(get_engine(), map));
}
async function gpu_device_promise(en, map) {
let adapter = await navigator.gpu.requestAdapter();
let device = await adapter.requestDevice();
map.set("device", device);
}
/**
* test_gpu_device_join(D, P): internal only
* The predicate succeeds in P with a new promise
* that waits for the GPU device D to finish.
*/
function test_gpu_device_join(args) {
let device = exec_build(args[0]);
return exec_unify(args[1],
gpu_join_promise(get_engine(), device));
}
async function gpu_join_promise(en, device) {
await device.queue.onSubmittedWorkDone();
}
/**
* test_gpu_device_free(D): internal only
* The predicate succeeds. As a side effect it frees
* the GPU device D.
*/
function test_gpu_device_free(args) {
let device = exec_build(args[0]);
device.destroy();
return true;
}
/******************************************************************/
/* Job Launch */
/******************************************************************/
/**
* test_gpu_job_new(D, C, O, T, S, W): internal only
* The predicate succeeds in W with a new GPU backed π-WAM
* for the GPU device D, the code buffer C, the offsets
* buffer O, the channel table T and the state buffer S.
*/
function test_gpu_job_new(args) {
let device = exec_build(args[0]);
let data = new Array(4);
for (let i = 0; i < 4; i++)
data[i] = exec_build(args[i+1]);
let entries = new Array(4);
for (let i = 0; i < 4; i++) {
let buf = device.createBuffer({
size: data[i].byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
entries[i] = {
binding: i,
resource: {buffer: buf}
};
}
for (let i = 0; i < 4; i++)
device.queue.writeBuffer(entries[i].resource.buffer, 0,
data[i].buffer, data[i].byteOffset, data[i].byteLength);
return exec_unify(args[5], entries);
}
/**
* test_gpu_job_start(D, W): internal only
* The predicate succeeds. As a side effect it starts
* the GPU backed π-WAM W on the GPU device D.
*/
function test_gpu_job_start(args) {
let device = exec_build(args[0]);
let entries = exec_build(args[1]);
let res = entries[1].resource.buffer.size / 4;
res = ((res+GROUP_SIZE-1)/GROUP_SIZE) | 0;
let pipeline = get_pipeline(device);
let bind = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: entries
});
let commandEncoder = device.createCommandEncoder();
let passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, bind);
passEncoder.dispatchWorkgroups(res);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
return true;
}
/******************************************************************/
/* Cmd Lib Init */
/******************************************************************/
export function main() {
set("gpu_device_new", 2, make_check(test_gpu_device_new));
set("gpu_device_join", 2, make_check(test_gpu_device_join));
set("gpu_device_free", 1, make_check(test_gpu_device_free));
set("gpu_job_new", 6, make_check(test_gpu_job_new));
set("gpu_job_start", 2, make_check(test_gpu_job_start));
}