-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import gpu from '@kmamal/gpu' | ||
|
||
const instance = gpu.create([]) | ||
const adapter = await instance.requestAdapter() | ||
const device = await adapter.requestDevice() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"version": "0.0.0", | ||
"name": "gpu-example-compute", | ||
"main": "index.js", | ||
"type": "module", | ||
"dependencies": { | ||
"@kmamal/gpu": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import gpu from '@kmamal/gpu' | ||
import sdl from '@kmamal/sdl' | ||
|
||
const window = sdl.video.createWindow({ accelerated: false }) | ||
const { width, height } = window | ||
|
||
const instance = gpu.create([]) | ||
const adapter = await instance.requestAdapter() | ||
const device = await adapter.requestDevice() | ||
|
||
const colorTexture = device.createTexture({ | ||
size: [ width, height, 1 ], | ||
dimension: '2d', | ||
format: 'rgba8unorm', | ||
usage: gpu.TextureUsage.RENDER_ATTACHMENT | gpu.TextureUsage.COPY_SRC, | ||
}) | ||
const colorTextureView = colorTexture.createView() | ||
|
||
const bufferSize = width * height * 4 | ||
const readBuffer = device.createBuffer({ | ||
size: bufferSize, | ||
usage: gpu.BufferUsage.COPY_DST | gpu.BufferUsage.MAP_READ, | ||
}) | ||
|
||
const positions = new Float32Array([ | ||
...[ 1.0, -1.0, 0.0 ], | ||
...[ -1.0, -1.0, 0.0 ], | ||
...[ 0.0, 1.0, 0.0 ], | ||
]) | ||
|
||
const colors = new Float32Array([ | ||
...[ 1.0, 0.0, 0.0 ], | ||
...[ 0.0, 1.0, 0.0 ], | ||
...[ 0.0, 0.0, 1.0 ], | ||
]) | ||
|
||
const indices = new Uint16Array([ 0, 1, 2 ]) | ||
|
||
const createBuffer = (arr, usage) => { | ||
const buffer = device.createBuffer({ | ||
size: (arr.byteLength + 3) & ~3, | ||
usage, | ||
mappedAtCreation: true, | ||
}) | ||
|
||
const writeArray = arr instanceof Uint16Array | ||
? new Uint16Array(buffer.getMappedRange()) | ||
: new Float32Array(buffer.getMappedRange()) | ||
writeArray.set(arr) | ||
buffer.unmap() | ||
return buffer | ||
} | ||
|
||
const positionBuffer = createBuffer(positions, gpu.BufferUsage.VERTEX) | ||
const colorBuffer = createBuffer(colors, gpu.BufferUsage.VERTEX) | ||
const indexBuffer = createBuffer(indices, gpu.BufferUsage.INDEX) | ||
|
||
const vertexShader = ` | ||
struct VSOut { | ||
@builtin(position) Position: vec4<f32>, | ||
@location(0) color: vec3<f32>, | ||
}; | ||
@vertex | ||
fn main(@location(0) inPos: vec3<f32>, @location(1) inColor: vec3<f32> ) -> VSOut { | ||
var vsOut: VSOut; | ||
vsOut.Position = vec4<f32>(inPos, 1.0); | ||
vsOut.color = inColor; | ||
return vsOut; | ||
} | ||
` | ||
|
||
const fragmentShader = ` | ||
@fragment | ||
fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> { | ||
return vec4<f32>(inColor, 1.0); | ||
} | ||
` | ||
|
||
const pipeline = device.createRenderPipeline({ | ||
layout: 'auto', | ||
vertex: { | ||
module: device.createShaderModule({ code: vertexShader }), | ||
entryPoint: 'main', | ||
buffers: [ | ||
{ | ||
attributes: [ | ||
{ | ||
shaderLocation: 0, | ||
offset: 0, | ||
format: 'float32x3', | ||
}, | ||
], | ||
arrayStride: 3 * Float32Array.BYTES_PER_ELEMENT, | ||
stepMode: 'vertex', | ||
}, | ||
{ | ||
attributes: [ | ||
{ | ||
shaderLocation: 1, | ||
offset: 0, | ||
format: 'float32x3', | ||
}, | ||
], | ||
arrayStride: 3 * Float32Array.BYTES_PER_ELEMENT, | ||
stepMode: 'vertex', | ||
}, | ||
], | ||
}, | ||
fragment: { | ||
module: device.createShaderModule({ code: fragmentShader }), | ||
entryPoint: 'main', | ||
targets: [ { format: 'rgba8unorm' } ], | ||
}, | ||
primitive: { | ||
topology: 'triangle-list', | ||
}, | ||
}) | ||
|
||
const commandEncoder = device.createCommandEncoder() | ||
|
||
const renderPass = commandEncoder.beginRenderPass({ | ||
colorAttachments: [ | ||
{ | ||
view: colorTextureView, | ||
clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }, | ||
loadOp: 'clear', | ||
storeOp: 'store', | ||
}, | ||
], | ||
}) | ||
renderPass.setPipeline(pipeline) | ||
renderPass.setViewport(0, 0, width, height, 0, 1) | ||
renderPass.setScissorRect(0, 0, width, height) | ||
renderPass.setVertexBuffer(0, positionBuffer) | ||
renderPass.setVertexBuffer(1, colorBuffer) | ||
renderPass.setIndexBuffer(indexBuffer, 'uint16') | ||
renderPass.drawIndexed(3) | ||
renderPass.end() | ||
|
||
commandEncoder.copyTextureToBuffer( | ||
{ texture: colorTexture }, | ||
{ buffer: readBuffer, bytesPerRow: width * 4 }, | ||
{ width, height }, | ||
) | ||
|
||
device.queue.submit([ commandEncoder.finish() ]) | ||
|
||
await readBuffer.mapAsync(gpu.MapMode.READ) | ||
const resultBuffer = new Uint8Array(readBuffer.getMappedRange()) | ||
window.render(width, height, width * 4, 'rgba32', Buffer.from(resultBuffer)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": "0.0.0", | ||
"name": "gpu-example-render", | ||
"main": "index.js", | ||
"type": "module", | ||
"dependencies": { | ||
"@kmamal/gpu": "*", | ||
"@kmamal/sdl": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Examples | ||
|
||
## [0. Compute Pipeline](https://github.com/kmamal/node-sdl/tree/master/examples/00-compute) | ||
|
||
Multiplies two matrices together and prints the result. | ||
|
||
## [1. Render Pipeline](https://github.com/kmamal/node-sdl/tree/master/examples/01-render) | ||
|
||
Renders a triagle and displays it in a window. | ||
|
||
|
||
// TODO: more |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters