-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup WebGPU render pipeline with GPU buffers, bind groups, encoders,…
… vertex/fragment shaders, normalized data, etc.
- Loading branch information
Showing
13 changed files
with
286 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,90 @@ | ||
// src/main.ts | ||
|
||
// src/main.ts | ||
import { SceneGraph } from './scene-graph/scene-graph'; | ||
import { Rectangle } from './scene-graph/rectangle'; | ||
import { Circle } from './scene-graph/circle'; | ||
import { WebGPURenderer } from './renderer/webgpu-renderer'; | ||
import { WebGPURenderStrategy } from './renderer/webgpu-render-strategy'; | ||
import { CanvasRenderer } from './renderer/canvas-renderer'; | ||
import { CanvasRenderStrategy } from './renderer/canvas-render-strategy'; | ||
|
||
async function main() { | ||
// Set up the canvas and WebGPURenderer | ||
async function webGPURendering() { | ||
// Set up the canvas | ||
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement; | ||
canvas.width = 800; | ||
canvas.height = 600; | ||
|
||
// Create the WebGPU renderer | ||
const webgpuRenderer = new WebGPURenderer(canvas); | ||
|
||
// Initialize the WebGPU context and pipeline | ||
await webgpuRenderer.initialize(); | ||
|
||
// Get the device and pipeline from the WebGPU renderer | ||
const device = webgpuRenderer.getDevice(); | ||
const pipeline = webgpuRenderer.getPipeline(); | ||
|
||
// Create the WebGPU render strategy | ||
const webGPURenderStrategy = new WebGPURenderStrategy(device, pipeline); | ||
const webgpuRenderStrategy = new WebGPURenderStrategy(device, pipeline, canvas); | ||
|
||
// Create the scene graph | ||
const sceneGraph = new SceneGraph(webGPURenderStrategy); | ||
|
||
// Create a rectangle with the WebGPU render strategy | ||
const sceneGraph = new SceneGraph(webgpuRenderStrategy); | ||
|
||
const rect = new Rectangle(webGPURenderStrategy, 100, 50, 'red', 'black', 2); | ||
// Create a dynamic rectangle | ||
const rect = new Rectangle(webgpuRenderStrategy, 200, 100, { r: 0, g: 0, b: 1, a: 1 }, { r: 0, g: 0, b: 0, a: 1 }, 2); | ||
rect.x = 150; | ||
rect.y = 100; | ||
rect.y = 150; | ||
|
||
// Add the rectangle to the scene graph | ||
sceneGraph.root.addChild(rect); | ||
// Create a dynamic circle | ||
const circle = new Circle(webgpuRenderStrategy, 50, { r: 1, g: 0, b: 0, a: 1 }, { r: 0, g: 0, b: 0, a: 1 }, 2); | ||
circle.x = 400; | ||
circle.y = 300; | ||
|
||
// Add the shapes to the scene graph | ||
sceneGraph.root.addChild(rect); | ||
sceneGraph.root.addChild(circle); | ||
|
||
// Start the rendering loop | ||
function renderLoop() { | ||
webgpuRenderer.render(sceneGraph); | ||
requestAnimationFrame(renderLoop); | ||
} | ||
|
||
renderLoop(); | ||
} | ||
|
||
main(); | ||
|
||
/* | ||
/////////////////////////////////////////////////////////////////////////// | ||
CANVAS | ||
/////////////////////////////////////////////////////////////////////////// | ||
import { SceneGraph } from './scene-graph/scene-graph'; | ||
import { Rectangle } from './scene-graph/rectangle'; | ||
import { CanvasRenderer } from './renderer/canvas-renderer'; | ||
import { CanvasRenderStrategy } from './renderer/canvas-render-strategy'; | ||
// Set up the canvas | ||
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement; | ||
canvas.width = 800; | ||
canvas.height = 600; | ||
// Create the scene graph | ||
const sceneGraph = new SceneGraph(); | ||
// Create the canvas render strategy | ||
const canvasRenderStrategy = new CanvasRenderStrategy(); | ||
// Create a rectangle | ||
const rect = new Rectangle(canvasRenderStrategy, 100, 50, 'red', 'black', 2); | ||
rect.x = 150; | ||
rect.y = 100; | ||
// Add a click event to change the color of the rectangle | ||
rect.onClick = () => { | ||
rect.fillColor = rect.fillColor === 'red' ? 'green' : 'red'; | ||
console.log("Rectangle clicked! Color changed."); | ||
}; | ||
// Add the rectangle to the scene graph | ||
sceneGraph.root.addChild(rect); | ||
// Create the renderer | ||
const renderer = new CanvasRenderer(canvas, sceneGraph); | ||
// Start the rendering loop manually | ||
renderer.start(); | ||
///////////////////////////////////////////////////////////////////////////// | ||
WEBGPU | ||
///////////////////////////////////////////////////////////////////////////// | ||
// src/main.ts | ||
import { SceneGraph } from './scene-graph/scene-graph'; | ||
import { Rectangle } from './scene-graph/rectangle'; | ||
import { WebGPURenderer } from './renderer/webgpu-renderer'; | ||
import { WebGPURenderStrategy } from './rendering/webgpu-render-strategy'; | ||
async function main() { | ||
// Set up the canvas and WebGPURenderer | ||
async function canvasRendering() { | ||
// Set up the canvas | ||
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement; | ||
const webgpuRenderer = new WebGPURenderer(canvas); | ||
await webgpuRenderer.initialize(); | ||
canvas.width = 800; | ||
canvas.height = 600; | ||
|
||
const device = webgpuRenderer.getDevice(); | ||
const pipeline = webgpuRenderer.getPipeline(); | ||
// Create the canvas render strategy | ||
const canvasRenderStrategy = new CanvasRenderStrategy(); | ||
|
||
// Create the scene graph | ||
const sceneGraph = new SceneGraph(); | ||
const sceneGraph = new SceneGraph(canvasRenderStrategy); | ||
|
||
// Create the WebGPU render strategy | ||
const webGPURenderStrategy = new WebGPURenderStrategy(device, pipeline); | ||
var red = {r:1,g:0,b:0,a:1}; | ||
var black = {r:0,g:0,b:0,a:1}; | ||
|
||
// Create a rectangle with the WebGPU render strategy | ||
const rect = new Rectangle(webGPURenderStrategy, 100, 50, 'red', 'black', 2); | ||
// Create a rectangle | ||
const rect = new Rectangle(canvasRenderStrategy, 100, 50, red, black, 2); | ||
rect.x = 150; | ||
rect.y = 100; | ||
|
||
// Add a click event to change the color of the rectangle | ||
rect.onClick = () => { | ||
rect.fillColor = rect.fillColor === red ? black : red; | ||
console.log("Rectangle clicked! Color changed."); | ||
}; | ||
|
||
// Add the rectangle to the scene graph | ||
sceneGraph.root.addChild(rect); | ||
|
||
// Start the rendering loop | ||
function renderLoop() { | ||
webgpuRenderer.render(sceneGraph); | ||
requestAnimationFrame(renderLoop); | ||
} | ||
renderLoop(); | ||
} | ||
// Create the renderer | ||
const renderer = new CanvasRenderer(canvas, sceneGraph); | ||
|
||
main(); | ||
// Start the rendering loop manually | ||
renderer.start(); | ||
} | ||
|
||
*/ | ||
//canvasRendering(); | ||
webGPURendering(); |
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
Oops, something went wrong.