Start maybe working on a native app
This commit is contained in:
+30
-11
@@ -7,8 +7,20 @@ import type { RenderLayer } from "@/common/everything_registry.ts";
|
||||
import { TERRAIN_VERTEX_BYTES } from "../workers/chunk_messages.ts";
|
||||
import { GpuTimer } from "./gpu_timing.ts";
|
||||
|
||||
// what frames are drawn on: the page's canvas, or the desktop app's window (deno's UnsafeWindowSurface). setting
|
||||
// width and height resizes it, like a canvas
|
||||
export interface DrawSurface {
|
||||
width: number;
|
||||
height: number;
|
||||
getContext(id: "webgpu"): unknown;
|
||||
// shows what was drawn, the window's needs it after every frame, a canvas does it by itself
|
||||
present?(): void;
|
||||
// a canvas is shown scaled when its size is fixed, see set_canvas_size
|
||||
style?: { width: string; height: string };
|
||||
}
|
||||
|
||||
export let device: GPUDevice;
|
||||
export let canvas: HTMLCanvasElement;
|
||||
export let canvas: DrawSurface;
|
||||
|
||||
const MAX_SPRITES = 100000;
|
||||
const VERTS_PER_SPRITE = 6;
|
||||
@@ -120,6 +132,8 @@ export let gpu_timer: GpuTimer | undefined;
|
||||
export let adapter_info: GPUAdapterInfo | undefined;
|
||||
// see set_canvas_size
|
||||
let fixed_canvas_size: { width: number; height: number } | undefined;
|
||||
// the space frames are shown in, the page's or the window's, see resize_canvas
|
||||
let view_size = { width: 1800, height: 900 };
|
||||
let resize_pending = false;
|
||||
// what the passes starting now are timed as
|
||||
let gpu_label = "other";
|
||||
@@ -287,10 +301,10 @@ fn fs_translucent(in: VertexOut) -> @location(0) vec4<f32> {
|
||||
}
|
||||
`;
|
||||
|
||||
export async function init_window(canvas_element: HTMLCanvasElement) {
|
||||
canvas = canvas_element;
|
||||
canvas.width = 1800;
|
||||
canvas.height = 900;
|
||||
export async function init_window(surface: DrawSurface) {
|
||||
canvas = surface;
|
||||
canvas.width = view_size.width;
|
||||
canvas.height = view_size.height;
|
||||
|
||||
if (!navigator.gpu) {
|
||||
throw new Error("WebGPU not supported");
|
||||
@@ -413,6 +427,7 @@ export function end_drawing() {
|
||||
gpu_timer?.resolve(encoder!);
|
||||
device.queue.submit([encoder!.finish()]);
|
||||
encoder = undefined;
|
||||
canvas.present?.();
|
||||
gpu_timer?.read();
|
||||
|
||||
for (const buffer of pending_destroy) {
|
||||
@@ -695,19 +710,23 @@ export function destroy_buffer(buffer: GPUBuffer) {
|
||||
}
|
||||
}
|
||||
|
||||
export function resize_canvas() {
|
||||
const width = fixed_canvas_size?.width ?? self.innerWidth;
|
||||
const height = fixed_canvas_size?.height ?? self.innerHeight;
|
||||
// the space frames are shown in changed to view_width × view_height, or the fixed size did
|
||||
export function resize_canvas(view_width = view_size.width, view_height = view_size.height) {
|
||||
view_size = { width: view_width, height: view_height };
|
||||
const width = fixed_canvas_size?.width ?? view_width;
|
||||
const height = fixed_canvas_size?.height ?? view_height;
|
||||
// a fixed size is shown as big as it fits in the window
|
||||
const scale = Math.min(self.innerWidth / width, self.innerHeight / height);
|
||||
const scale = Math.min(view_width / width, view_height / height);
|
||||
|
||||
if (canvas.width !== width || canvas.height !== height) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
// render targets get recreated at the start of the next frame
|
||||
}
|
||||
canvas.style.width = width * scale + "px";
|
||||
canvas.style.height = height * scale + "px";
|
||||
if (canvas.style) {
|
||||
canvas.style.width = width * scale + "px";
|
||||
canvas.style.height = height * scale + "px";
|
||||
}
|
||||
}
|
||||
|
||||
// frames are drawn at this size whatever the window's, like the benchmark's, or at the window's again when it's
|
||||
|
||||
Reference in New Issue
Block a user