Skip to content

Webgpu Support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions micropolis/dun-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

micropolis/src/lib/TileView.svelte
- webgpu / opengl fallback
if webgpu is not available, opengl is used
- support of tileSets and tileLayers
- tileLayer = Tile Image file
- a tileLayer can contain several tileSets
- mopData = tileset to use for given cell why ? in /src/lib/MicropolisSimulator.ts, all cells have same tileset
- In the meantime, quick implementation tilSet passed to uniforms with mopData[0] value
- support of space / rotate


- implement for gpu ctxGL.viewport(0, 0, canvasGL.width, canvasGL.height); ??
6 changes: 4 additions & 2 deletions micropolis/src/lib/MicropolisView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import { onMount, onDestroy } from 'svelte';
import { loadMicropolisEngine, MicropolisSimulator } from '$lib/MicropolisSimulator';
import { TileRenderer, WebGLTileRenderer } from '$lib/WebGLTileRenderer';
//import { TileRenderer, WebGLTileRenderer } from '$lib/WebGLTileRenderer';
//import { TileRenderer, WebGPUTileRenderer } from '$lib/WebGPUTileRenderer';

import initModule from "$lib/micropolisengine.js";
import { MicropolisCallbackLog } from "$lib/MicropolisCallbackLog";
import TileView from '$lib/TileView.svelte';
Expand Down Expand Up @@ -71,7 +73,7 @@
bind:this={tileView}
/>

<About showAbout={true}/>
<About showAbout={false}/>

<!--
<SnapView
Expand Down
58 changes: 34 additions & 24 deletions micropolis/src/lib/TileView.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">

import { onMount, onDestroy } from 'svelte';
import { TileRenderer, WebGLTileRenderer } from '$lib/WebGLTileRenderer';
import { TileRenderer as glTileRenderer, WebGLTileRenderer } from '$lib/WebGLTileRenderer';
import { TileRenderer as gpuTileRenderer, WebGPUTileRenderer } from '$lib/WebGPUTileRenderer';
import { pan, pinch } from 'svelte-gestures';
import { MicropolisSimulator } from '$lib/MicropolisSimulator';

Expand All @@ -18,9 +19,9 @@
let tileSetCount = 10;
let tileSet: number = 0;

let canvasGL: HTMLCanvasElement | null = null;
let ctxGL: WebGL2RenderingContext | null = null;
let tileRenderer: TileRenderer<any> | null = null;
let canvas: HTMLCanvasElement | null = null;
let ctx: GPUCanvasContext | WebGL2RenderingContext | null = null;
let tileRenderer: glTileRenderer<any> | gpuTileRenderer<any> | null = null;

let autoRepeatIntervalId: any | null = null;
let autoRepeatDelay = 1000 / 60; // 60 repeats per second
Expand Down Expand Up @@ -56,23 +57,31 @@

micropolisSimulator = micropolisSimulator_;

if (!micropolisSimulator || !canvasGL) return;
if (!micropolisSimulator || !canvas) return;

// Create 3d canvas drawing context and tileRenderer.
//console.log('TileView.svelte: onMount', 'canvasGL:', canvasGL);
if (canvasGL == null) {
if (canvas == null) {
console.log('TileView.svelte: initialize: canvasGL is null!');
return;
}

ctxGL = canvasGL.getContext('webgl2');
//console.log('TileView.svelte: initialize:', 'ctxGL:', ctxGL);
if (ctxGL == null) {
console.log('TileView.svelte: initialize: no ctxGL!');
return;
ctx = canvas.getContext('webgpu');
if (ctx == null) {
console.log('TileView.svelte: WebGPU not available. Fallback to webgl2.');
ctx = canvas.getContext('webgl2');
if (ctx==null) {
console.log('TileView.svelte: webgl2 not available');
return;
}
console.log('TileView.svelte: webgl2 rendering');
tileRenderer = new WebGLTileRenderer();
}
else {
console.log('TileView.svelte: WebGPU rendering');
tileRenderer = new WebGPUTileRenderer();
}

tileRenderer = new WebGLTileRenderer();

if (typeof window != "undefined") {
//window.tileRenderer = tileRenderer;
Expand All @@ -85,8 +94,8 @@
}

await tileRenderer.initialize(
canvasGL,
ctxGL,
canvas,
ctx,
micropolisSimulator.mapData!,
micropolisSimulator.mopData!,
micropolisSimulator.micropolisengine.WORLD_W,
Expand All @@ -104,7 +113,7 @@
tileRenderer.tileLayer = 0;

if (typeof window != "undefined") {
canvasGL.addEventListener('wheel', onwheel, {passive: false});
canvas.addEventListener('wheel', onwheel, {passive: false});
}

resizeCanvas();
Expand All @@ -119,13 +128,14 @@

// Function to resize the canvas to match the screen size.
function resizeCanvas() {
if (canvasGL) {
if (canvas) {
const ratio = window.devicePixelRatio || 1;
canvasGL.width = canvasGL.clientWidth * ratio;
canvasGL.height = canvasGL.clientHeight * ratio;
canvas.width = canvas.clientWidth * ratio;
canvas.height = canvas.clientHeight * ratio;
//console.log("TileView.svelte: resizeCanvas:", "ratio:", ratio, "canvasGL.width:", canvasGL.width, "canvasGL.height:", canvasGL.height);
if (ctxGL) {
ctxGL.viewport(0, 0, canvasGL.width, canvasGL.height);
if (ctx && "viewport" in ctx) {
// only applies to webgl2 renderer
ctx.viewport(0, 0, canvas.width, canvas.height);
}
}
}
Expand Down Expand Up @@ -434,9 +444,9 @@
}

export function refocusCanvas() {
if (canvasGL &&
(document.activeElement !== canvasGL)) {
canvasGL.focus();
if (canvas &&
(document.activeElement !== canvas)) {
canvas.focus();
}
}

Expand Down Expand Up @@ -513,7 +523,7 @@

<canvas
class="tileview-canvas"
bind:this={canvasGL}
bind:this={canvas}
tabindex="0"
onmousedown={onmousedown}
onmousemove={onmousemove}
Expand Down
Loading