diff --git a/xbox/live/oskiewar.js b/xbox/live/oskiewar.js index 3e01018be6..d56f386ec1 100644 --- a/xbox/live/oskiewar.js +++ b/xbox/live/oskiewar.js @@ -2566,12 +2566,61 @@ function flushFrameProgram() { // The host's own drawing functions, taken before they are shadowed. The // shadows are the emitter: buffered they write an op, immediate they are // the old call. Every draw site in the piece keeps calling the old names. +let flushFaces = () => {}; { - const rawTriangle3d = typeof triangle3d === "function" ? triangle3d : null; - const rawTriangle = typeof triangle === "function" ? triangle : null; + let rawTriangle3d = typeof triangle3d === "function" ? triangle3d : null; + let rawTriangle = typeof triangle === "function" ? triangle : null; const rawDisc3d = typeof disc3d === "function" ? disc3d : null; const rawCapsule3d = typeof capsule3d === "function" ? capsule3d : null; - const rawWipe = wipe, rawBox = box, rawLine = line, rawWrite = write; + let rawWipe = wipe, rawBox = box, rawLine = line; + const rawWrite = write; + // A host that says `batchFaces` (the Mac: JavaScriptCore, where one call + // into Swift costs ~1.2 us and a Float32Array write a few ns) gets its + // faces gathered into one array and handed over by `triangles3d` when + // anything else touches its scene lane (a box, a line, a flat or + // translucent face, a wipe, a direct batch) and at the end of the paint, + // so the lane's order is exactly the per-call order. The console's QuickJS + // is the other way round (see emitTriangle) and keeps one call per face. + if (rawTriangle3d && typeof triangles3d === "function" && + typeof capabilities === "function" && capabilities().batchFaces === true) { + const send = triangles3d, room = 2048; + const faces = new Float32Array(room * 12); + let count = 0; + flushFaces = () => { + if (!count) return; + const n = count; + count = 0; + send(faces, n); + }; + rawTriangle3d = function (x1, y1, z1, x2, y2, z2, x3, y3, z3, r = 255, g = 255, b = 255) { + if (count === room) flushFaces(); + const at = count * 12; + faces[at] = x1; faces[at + 1] = y1; faces[at + 2] = z1; + faces[at + 3] = x2; faces[at + 4] = y2; faces[at + 5] = z2; + faces[at + 6] = x3; faces[at + 7] = y3; faces[at + 8] = z3; + faces[at + 9] = r; faces[at + 10] = g; faces[at + 11] = b; + count++; + }; + const wipeNow = rawWipe, boxNow = rawBox, lineNow = rawLine, triangleNow = rawTriangle; + rawWipe = function (r, g, b) { flushFaces(); return wipeNow(r, g, b); }; + rawBox = function (x, y, w, h, r, g, b, a) { + flushFaces(); + return arguments.length > 7 ? boxNow(x, y, w, h, r, g, b, a) : boxNow(x, y, w, h, r, g, b); + }; + rawLine = function (x1, y1, x2, y2, width, r, g, b) { flushFaces(); return lineNow(x1, y1, x2, y2, width, r, g, b); }; + if (triangleNow) rawTriangle = function (x1, y1, x2, y2, x3, y3, r, g, b) { + flushFaces(); + return triangleNow(x1, y1, x2, y2, x3, y3, r, g, b); + }; + if (typeof triangleAlpha === "function") { + const alphaNow = triangleAlpha; + triangleAlpha = function (x1, y1, z1, x2, y2, z2, x3, y3, z3, r, g, b, a) { + flushFaces(); + return alphaNow(x1, y1, z1, x2, y2, z2, x3, y3, z3, r, g, b, a); + }; + } + triangles3d = function (buffer, n) { flushFaces(); return send(buffer, n); }; + } const rawSystemWrite = systemWrite; const rawComicWrite = typeof comicWrite === "function" ? comicWrite : null; const rawYwftWrite = typeof ywftWrite === "function" ? ywftWrite : null; @@ -2709,7 +2758,7 @@ function beginFrameProgram() { worldDepthValue = 0; } function endFrameProgram() { - try { flushFrameProgram(); } finally { programBuffered = false; } + try { flushFaces(); flushFrameProgram(); } finally { programBuffered = false; } } function emitFrameCamera() { const doll = cameraDoll; diff --git a/xbox/live/tests/face-batch.test.mjs b/xbox/live/tests/face-batch.test.mjs new file mode 100644 index 0000000000..74c3fa1df9 --- /dev/null +++ b/xbox/live/tests/face-batch.test.mjs @@ -0,0 +1,45 @@ +// A host that says `batchFaces` (the Mac) receives the same scene lane as +// one that takes a call per face: the same faces, boxes, lines and +// translucent faces, in the same order, in a couple of crossings a paint. +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; + +const source = await readFile(new URL("../oskiewar.js", import.meta.url), "utf8"); + +function lane(batchFaces) { + let now = 0, crossings = 0; + const events = []; + const noOp = () => {}; + const face = (...v) => events.push(["face", ...v.slice(0, 9).map(Math.fround), ...[0, 1, 2].map(k => Math.fround(v[9 + k] ?? 255))]); + const fight = new Function("runtime", "gamepad", "capabilities", "telemetry", "gameSignal", + "saveReplay", "publishLive", "analytics", "drum", "wipe", "box", "line", "triangle", + "triangle3d", "triangles3d", "triangleAlpha", "write", "systemWrite", "gameView", + `${source}\nreturn { boot, sim, paint, enterGame: () => enterGame(runtime().monotonicUs), + startFight: () => { shellMode = "GAME"; selecting = false; players[1].npc = false; players[1].bot = false; applyRoster(players[1], 2); startReplay(runtime().monotonicUs); resetRound(runtime().monotonicUs, true); } };`)( + () => ({ monotonicUs: now, unixMs: 1785870000000, simCount: 0, paintCount: 0 }), + () => ({ connected: true, down: [], leftX: 0, leftY: 0 }), + () => ({ platform: "macos", inputFamily: "keyboard", batchFaces }), + noOp, noOp, () => Promise.resolve(true), noOp, noOp, noOp, + (...v) => events.push(["wipe", ...v]), (...v) => events.push(["box", ...v]), + (...v) => events.push(["line", ...v]), (...v) => events.push(["triangle", ...v]), face, + (buffer, count = buffer.length / 12) => { + crossings++; + for (let i = 0; i < count; i++) face(...buffer.subarray(i * 12, i * 12 + 12)); + }, + (...v) => events.push(["alpha", ...v]), noOp, noOp, () => ({ width: 1920, height: 1080 })); + globalThis.__oskiewarOpponent = "dummy"; + fight.boot(); fight.enterGame(); fight.startFight(); + delete globalThis.__oskiewarOpponent; + for (let i = 0; i < 120; i++) { now += 16667; fight.sim(); } + events.length = 0; crossings = 0; + fight.paint(); + return { events, crossings }; +} + +test("batched faces reach the host in the per-call order, in a few crossings", () => { + const perCall = lane(false), batched = lane(true); + assert.ok(perCall.events.filter(e => e[0] === "face").length > 500); + assert.ok(batched.crossings > 0 && batched.crossings < 20 && batched.crossings > perCall.crossings, `${batched.crossings} crossings`); + assert.deepEqual(batched.events, perCall.events); +}); diff --git a/xbox/macos-native/main.swift b/xbox/macos-native/main.swift index 223faa1eee..ef0f8c254a 100644 --- a/xbox/macos-native/main.swift +++ b/xbox/macos-native/main.swift @@ -75,6 +75,12 @@ private final class NativeRenderer { emit(x1, y1, z, x2, y2, z, x3, y3, z, red, green, blue) } + /// `count` faces of twelve floats, straight from the engine's Float32Array. + func triangles3d(_ values: UnsafePointer, count: Int) { + sawFace = true + _ = scene.triangleBatch(values, count: count) + } + func triangle3d(_ x1: Double, _ y1: Double, _ z1: Double, _ x2: Double, _ y2: Double, _ z2: Double, _ x3: Double, _ y3: Double, _ z3: Double, @@ -1236,7 +1242,7 @@ private final class NativeGameHost { "version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0", "graphics": "METAL NATIVE", "audio": "AVFOUNDATION", "displayScale": self?.view.window?.backingScaleFactor ?? 1, - "online": true, "liveLocalState": true, + "online": true, "liveLocalState": true, "batchFaces": true, "width": Int(self?.renderer.logicalSize.width ?? 1920), "height": Int(self?.renderer.logicalSize.height ?? 1080), ] as NSDictionary @@ -1293,6 +1299,19 @@ private final class NativeGameHost { javascript.setObject(4096, forKeyedSubscript: "decalSurfaceSize" as NSString) javascript.setObject(triangleAlpha, forKeyedSubscript: "triangleAlpha" as NSString) javascript.setObject(triangle3d, forKeyedSubscript: "triangle3d" as NSString) + // triangles3d(Float32Array, count?): a run of faces, twelve floats + // each, read in place. With `batchFaces` the engine gathers every + // triangle3d into one and crosses once per run instead of per face. + let triangles3d: @convention(block) (JSValue, JSValue) -> Void = { [weak self] buffer, count in + guard let self, let context = buffer.context?.jsGlobalContextRef, + let object = JSValueToObject(context, buffer.jsValueRef, nil) else { return } + let length = JSObjectGetTypedArrayLength(context, object, nil) / 12 + let wanted = count.isNumber ? Int(count.toInt32()) : length + let faces = min(max(0, wanted), length) + guard faces > 0, let values = Self.float32(buffer, minimum: faces * 12) else { return } + self.renderer.triangles3d(values, count: faces) + } + javascript.setObject(triangles3d, forKeyedSubscript: "triangles3d" as NSString) javascript.setObject(text("Comic Relief"), forKeyedSubscript: "comicWrite" as NSString) javascript.setObject(text("Comic Relief"), forKeyedSubscript: "ywftWrite" as NSString) javascript.setObject(text("Helvetica"), forKeyedSubscript: "systemWrite" as NSString)