MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / init

Function init

gpu-pipeline/webgpu/index.js:18–154  ·  view source on GitHub ↗

* @license * Copyright 2022 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unles

()

Source from the content-addressed store, hash-verified

16 */
17
18async function init() {
19 const customBackendName = 'custom-webgpu';
20
21 const kernels = tf.getKernelsForBackend('webgpu');
22 kernels.forEach(kernelConfig => {
23 const newKernelConfig = { ...kernelConfig, backendName: customBackendName };
24 tf.registerKernel(newKernelConfig);
25 });
26
27 adapter = await navigator.gpu.requestAdapter();
28 device = await adapter.requestDevice();
29
30 tf.registerBackend(customBackendName, async () => {
31 return new tf.WebGPUBackend(device);
32 });
33 await tf.setBackend(customBackendName);
34
35 const context = canvasEl.getContext('webgpu');
36 const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
37 const presentationSize = [
38 canvasEl.width,
39 canvasEl.height,
40 ];
41
42 context.configure({
43 device,
44 size: presentationSize,
45 format: presentationFormat,
46 alphaMode: 'opaque',
47 });
48
49 const pipeline = device.createRenderPipeline({
50 layout: 'auto',
51 vertex: {
52 module: device.createShaderModule({
53 code: VERTEX_SHADER,
54 }),
55 entryPoint: 'main',
56 },
57 fragment: {
58 module: device.createShaderModule({
59 code: PIXEL_SHADER,
60 }),
61 entryPoint: 'main',
62 targets: [
63 {
64 format: presentationFormat,
65 },
66 ],
67 },
68 primitive: {
69 topology: 'triangle-list',
70 },
71 });
72
73 const sampler = device.createSampler({
74 magFilter: 'linear',
75 minFilter: 'linear',

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected