From 89aa596b15f39a13b2e52b2d3ba99991fc729fe4 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Wed, 1 Jan 2025 12:34:11 -0800 Subject: [PATCH] Simplify more ArrayBuffer stuff --- src/Common/Unity/AssetManager.ts | 6 +- src/ZeldaWindWaker/Main.ts | 66 ------------------- src/byml.ts | 5 +- .../render/GfxRenderDynamicUniformBuffer.ts | 4 +- src/tools/bcsvtool.ts | 2 +- 5 files changed, 7 insertions(+), 76 deletions(-) diff --git a/src/Common/Unity/AssetManager.ts b/src/Common/Unity/AssetManager.ts index 7bdd9e13b..d2da7f542 100644 --- a/src/Common/Unity/AssetManager.ts +++ b/src/Common/Unity/AssetManager.ts @@ -15,9 +15,9 @@ import { GfxRenderCache } from '../../gfx/render/GfxRenderCache.js'; import { rust } from '../../rustlib.js'; import { assert, assertExists, fallbackUndefined } from '../../util.js'; -function concatBufs(a: Uint8Array, b: Uint8Array): Uint8Array { - let result = new Uint8Array(a.byteLength + b.byteLength); - result.set(a); +function concatBufs(a: Uint8Array, b: Uint8Array): Uint8Array { + const newBuffer = a.buffer.transfer(a.byteLength + b.byteLength); + const result = new Uint8Array(newBuffer); result.set(b, a.byteLength); return result; } diff --git a/src/ZeldaWindWaker/Main.ts b/src/ZeldaWindWaker/Main.ts index 4dd77efd8..91a6c22e5 100644 --- a/src/ZeldaWindWaker/Main.ts +++ b/src/ZeldaWindWaker/Main.ts @@ -202,71 +202,12 @@ export class dGlobals { } } -function gain(v: number, k: number): number { - const a = 0.5 * Math.pow(2*((v < 0.5) ? v : 1.0 - v), k); - return v < 0.5 ? a : 1.0 - a; -} - -class DynToonTex { - public gfxTexture: GfxTexture; - public desiredPower: number = 0; - private texPower: number = 0; - private textureData: Uint8Array[] = [new Uint8Array(256*1*2)]; - - constructor(device: GfxDevice) { - this.gfxTexture = device.createTexture(makeTextureDescriptor2D(GfxFormat.U8_RG_NORM, 256, 1, 1)); - device.setResourceName(this.gfxTexture, 'DynToonTex'); - } - - private fillTextureData(k: number): void { - let dstOffs = 0; - const dst = this.textureData[0]; - for (let i = 0; i < 256; i++) { - const t = i / 255; - dst[dstOffs++] = gain(t, k) * 255; - // TODO(jstpierre): Lantern - dst[dstOffs++] = 0; - } - } - - public prepareToRender(device: GfxDevice): void { - if (this.texPower !== this.desiredPower) { - this.texPower = this.desiredPower; - - // Recreate toon texture. - this.fillTextureData(this.texPower); - device.uploadTextureData(this.gfxTexture, 0, this.textureData); - } - } - - public destroy(device: GfxDevice): void { - device.destroyTexture(this.gfxTexture); - } -} - export class ZWWExtraTextures { public textureMapping: TextureMapping[] = nArray(2, () => new TextureMapping()); - public dynToonTex: DynToonTex; - - @dfRange(1, 15, 0.01) - public toonTexPower: number = 15; constructor(device: GfxDevice, ZAtoon: BTIData, ZBtoonEX: BTIData) { ZAtoon.fillTextureMapping(this.textureMapping[0]); ZBtoonEX.fillTextureMapping(this.textureMapping[1]); - this.dynToonTex = new DynToonTex(device); - } - - public powerPopup(): void { - this.textureMapping[0].gfxTexture = this.dynToonTex.gfxTexture; - this.textureMapping[1].gfxTexture = this.dynToonTex.gfxTexture; - - window.main.ui.debugFloaterHolder.bindPanel(this); - } - - public prepareToRender(device: GfxDevice): void { - this.dynToonTex.desiredPower = this.toonTexPower; - this.dynToonTex.prepareToRender(device); } public fillExtraTextures(modelInstance: J3DModelInstance): void { @@ -278,10 +219,6 @@ export class ZWWExtraTextures { if (ZBtoonEX_map !== null) ZBtoonEX_map.copy(this.textureMapping[1]); } - - public destroy(device: GfxDevice): void { - this.dynToonTex.destroy(device); - } } function fpcIsObject(n: dProcName_e): boolean { @@ -455,8 +392,6 @@ export class WindWakerRenderer implements Viewer.SceneGfx { if (globals.renderHacks.wireframe) template.setMegaStateFlags({ wireframe: true }); - this.extraTextures.prepareToRender(device); - fpcM_Management(globals.frameworkGlobals, globals, renderInstManager, viewerInput); const dlst = globals.dlst; @@ -578,7 +513,6 @@ export class WindWakerRenderer implements Viewer.SceneGfx { public destroy(device: GfxDevice): void { this.renderHelper.destroy(); - this.extraTextures.destroy(device); this.globals.destroy(device); this.globals.frameworkGlobals.delete(this.globals); } diff --git a/src/byml.ts b/src/byml.ts index a33e54eac..3cbc9137d 100644 --- a/src/byml.ts +++ b/src/byml.ts @@ -293,10 +293,7 @@ class GrowableBuffer { if (newBufferSize > this.bufferSize) { this.bufferSize = align(newBufferSize, this.growAmount); - const newBuffer = new ArrayBuffer(this.bufferSize); - // memcpy - new Uint8Array(newBuffer).set(new Uint8Array(this.buffer)); - this.buffer = newBuffer; + this.buffer = this.buffer.transfer(newBufferSize); this.view = new DataView(this.buffer); } } diff --git a/src/gfx/render/GfxRenderDynamicUniformBuffer.ts b/src/gfx/render/GfxRenderDynamicUniformBuffer.ts index aaec1bcb1..408db8f75 100644 --- a/src/gfx/render/GfxRenderDynamicUniformBuffer.ts +++ b/src/gfx/render/GfxRenderDynamicUniformBuffer.ts @@ -12,7 +12,7 @@ export class GfxRenderDynamicUniformBuffer { public gfxBuffer: GfxBuffer | null = null; private shadowBufferF32: Float32Array | null = null; - private shadowBufferU8: Uint8Array | null = null; + private shadowBufferU8: Uint8Array | null = null; constructor(private device: GfxDevice) { const limits = device.queryLimits(); @@ -51,7 +51,7 @@ export class GfxRenderDynamicUniformBuffer { // Grow logarithmically, aligned to page size. const newWordCount = alignNonPowerOfTwo(Math.max(this.currentWordOffset, this.shadowBufferF32!.length * 2), this.uniformBufferMaxPageWordSize); - const buffer = this.shadowBufferU8.buffer as ArrayBuffer; + const buffer = this.shadowBufferU8.buffer; const newBuffer = buffer.transfer(newWordCount << 2); this.shadowBufferU8 = new Uint8Array(newBuffer); diff --git a/src/tools/bcsvtool.ts b/src/tools/bcsvtool.ts index f41d33573..a731bb829 100644 --- a/src/tools/bcsvtool.ts +++ b/src/tools/bcsvtool.ts @@ -3,7 +3,7 @@ import { readFileSync, writeFileSync } from 'fs'; import ArrayBufferSlice from '../ArrayBufferSlice.js'; -import * as BCSV from '../luigis_mansion/bcsv.js'; +import * as BCSV from '../LuigisMansion/bcsv.js'; import { guessDebugName } from '../SuperMarioGalaxy/JMapInfo.js'; import { assert } from 'console';