Skip to content

Commit

Permalink
Simplify more ArrayBuffer stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Jan 1, 2025
1 parent 976bfe0 commit 89aa596
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/Common/Unity/AssetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayBuffer> {
let result = new Uint8Array(a.byteLength + b.byteLength);
result.set(a);
function concatBufs(a: Uint8Array<ArrayBuffer>, b: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer> {
const newBuffer = a.buffer.transfer(a.byteLength + b.byteLength);
const result = new Uint8Array(newBuffer);
result.set(b, a.byteLength);
return result;
}
Expand Down
66 changes: 0 additions & 66 deletions src/ZeldaWindWaker/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions src/byml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/render/GfxRenderDynamicUniformBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayBuffer> | null = null;

constructor(private device: GfxDevice) {
const limits = device.queryLimits();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/bcsvtool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 89aa596

Please sign in to comment.