-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyThread.js
More file actions
82 lines (71 loc) · 2.32 KB
/
Copy pathmyThread.js
File metadata and controls
82 lines (71 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {installWithThreadId} from 'react-native-webgpu';
import {installWithThreadId as installExperimentalWithThreadId} from 'react-native-webgpu-experimental';
import triangleVertWGSL from './src/WebGPUSamples/shaders/triangle.vert.wgsl';
import redFragWGSL from './src/WebGPUSamples/shaders/red.frag.wgsl';
global.reactNativeWebGPUThreadsInstance.onAttachSurface = ({uuid}) => {
const run = async ({context, navigator, requestAnimationFrame}) => {
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat,
alphaMode: 'auto',
});
const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
entryPoint: 'main',
module: device.createShaderModule({
code: triangleVertWGSL,
}),
},
fragment: {
entryPoint: 'main',
module: device.createShaderModule({
code: redFragWGSL,
}),
targets: [
{
format: presentationFormat,
},
],
},
primitive: {
topology: 'triangle-list',
},
});
function frame() {
const framebuffer = context.getCurrentTexture();
if (!framebuffer) {
requestAnimationFrame(frame);
return;
}
const commandEncoder = device.createCommandEncoder();
const textureView = framebuffer.createView();
const renderPassDescriptor = {
colorAttachments: [
{
view: textureView,
clearValue: [0, 0, 0, 1],
loadOp: 'clear',
storeOp: 'store',
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.draw(3);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
context.presentSurface();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
};
const webGPU = global.reactNativeWebGPU.getSurfaceBackedWebGPU(uuid);
run(webGPU);
};
const threadId = global.reactNativeWebGPUThreadsInstance.getContext().threadId;
installWithThreadId(threadId);
installExperimentalWithThreadId(threadId);