-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
3 changed files
with
85 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,82 @@ | ||
import { createWindow, mainloop } from "../mod.ts"; | ||
|
||
const adapter = await navigator.gpu.requestAdapter(); | ||
const device = await adapter!.requestDevice(); | ||
|
||
const window = createWindow({ | ||
title: "Deno Window Manager", | ||
width: 512, | ||
height: 512, | ||
resizable: true, | ||
}); | ||
|
||
const { width, height } = window.framebufferSize; | ||
|
||
const surface = window.windowSurface(); | ||
|
||
const context = surface.getContext("webgpu"); | ||
|
||
context.configure({ | ||
device, | ||
format: "bgra8unorm", | ||
width, | ||
height, | ||
}); | ||
|
||
const shaderCode = ` | ||
@vertex | ||
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> { | ||
let x = f32(i32(in_vertex_index) - 1); | ||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1); | ||
return vec4<f32>(x, y, 0.0, 1.0); | ||
} | ||
@fragment | ||
fn fs_main() -> @location(0) vec4<f32> { | ||
return vec4<f32>(1.0, 0.0, 0.0, 1.0); | ||
} | ||
`; | ||
|
||
const shaderModule = device.createShaderModule({ | ||
code: shaderCode, | ||
}); | ||
|
||
const pipelineLayout = device.createPipelineLayout({ | ||
bindGroupLayouts: [], | ||
}); | ||
|
||
const renderPipeline = device.createRenderPipeline({ | ||
layout: pipelineLayout, | ||
vertex: { | ||
module: shaderModule, | ||
entryPoint: "vs_main", | ||
}, | ||
fragment: { | ||
module: shaderModule, | ||
entryPoint: "fs_main", | ||
targets: [ | ||
{ | ||
format: "bgra8unorm", | ||
}, | ||
], | ||
}, | ||
}); | ||
mainloop(() => { | ||
const encoder = device.createCommandEncoder(); | ||
const texture = context.getCurrentTexture().createView(); | ||
const renderPass = encoder.beginRenderPass({ | ||
colorAttachments: [ | ||
{ | ||
view: texture, | ||
storeOp: "store", | ||
loadOp: "clear", | ||
clearValue: { r: 0, g: 1, b: 0, a: 1.0 }, | ||
}, | ||
], | ||
}); | ||
renderPass.setPipeline(renderPipeline); | ||
renderPass.draw(3, 1); | ||
renderPass.end(); | ||
device.queue.submit([encoder.finish()]); | ||
surface.present(); | ||
}, false); |
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 |
---|---|---|
|
@@ -47,4 +47,4 @@ await mainloop(() => { | |
|
||
device.queue.submit([commandEncoder.finish()]); | ||
surface.present(); | ||
}, false); | ||
}, false); |