Skip to content

Commit

Permalink
gx: Simplify handling of ub_SceneParams
Browse files Browse the repository at this point in the history
Rather than allocating in the RenderInstManager requiring us to do some weird juggling for multi-view scenarios, just let users allocate it like anywhere else.
  • Loading branch information
magcius committed Dec 27, 2024
1 parent f125293 commit 456dd20
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 73 deletions.
48 changes: 23 additions & 25 deletions src/PaperMario64/render.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@

//@ts-ignore
import program_glsl from './program.glsl';
import * as Viewer from '../viewer.js';
import * as Tex from './tex.js';
import { GfxBufferUsage, GfxDevice, GfxBindingLayoutDescriptor, GfxBlendMode, GfxBlendFactor, GfxFormat, GfxBuffer, GfxInputLayout, GfxVertexAttributeDescriptor, GfxVertexBufferFrequency, GfxTextureDimension, GfxSampler, GfxWrapMode, GfxTexFilterMode, GfxMipFilterMode, GfxCullMode, GfxProgram, GfxMegaStateDescriptor, GfxInputLayoutBufferDescriptor, makeTextureDescriptor2D, GfxVertexBufferDescriptor, GfxIndexBufferDescriptor } from "../gfx/platform/GfxPlatform.js";
import { mat4 } from "gl-matrix";
import { GfxRenderInstManager, makeSortKeyOpaque, GfxRendererLayer, setSortKeyDepth } from "../gfx/render/GfxRenderInstManager.js";
import { DeviceProgram } from "../Program.js";
import { fillMatrix4x4, fillMatrix4x3, fillMatrix4x2, fillVec4 } from "../gfx/helpers/UniformBufferHelpers.js";
import { ModelTreeNode, ModelTreeLeaf, ModelTreeGroup, PropertyType } from "./map_shape.js";
import { makeStaticDataBuffer } from "../gfx/helpers/BufferHelpers.js";
import { RSPOutput, Vertex } from "./f3dex2.js";
import { assert, nArray, assertExists, setBitFlagEnabled } from "../util.js";
import { TextureHolder, LoadedTexture, TextureMapping } from "../TextureHolder.js";
import { computeViewSpaceDepthFromWorldSpaceAABB } from "../Camera.js";
import { AABB } from "../Geometry.js";
import ArrayBufferSlice from '../ArrayBufferSlice.js';
import { getImageFormatString } from "../BanjoKazooie/f3dex.js";
import { computeViewSpaceDepthFromWorldSpaceAABB } from "../Camera.js";
import { TextFilt } from '../Common/N64/Image.js';
import { translateCM } from '../Common/N64/RDP.js';
import { calcTextureScaleForShift } from '../Common/N64/RSP.js';
import { AABB } from "../Geometry.js";
import { makeStaticDataBuffer } from "../gfx/helpers/BufferHelpers.js";
import { setAttachmentStateSimple } from '../gfx/helpers/GfxMegaStateDescriptorHelpers.js';
import { reverseDepthForDepthOffset } from '../gfx/helpers/ReversedDepthHelpers.js';
import { calcTextureScaleForShift } from '../Common/N64/RSP.js';
import { translateCM } from '../Common/N64/RDP.js';
import { convertToCanvas } from '../gfx/helpers/TextureConversionHelpers.js';
import ArrayBufferSlice from '../ArrayBufferSlice.js';
import { fillMatrix4x2, fillMatrix4x3, fillMatrix4x4, fillVec4 } from "../gfx/helpers/UniformBufferHelpers.js";
import { GfxBindingLayoutDescriptor, GfxBlendFactor, GfxBlendMode, GfxBuffer, GfxBufferUsage, GfxCullMode, GfxDevice, GfxFormat, GfxIndexBufferDescriptor, GfxInputLayout, GfxInputLayoutBufferDescriptor, GfxMegaStateDescriptor, GfxMipFilterMode, GfxProgram, GfxSampler, GfxTexFilterMode, GfxVertexAttributeDescriptor, GfxVertexBufferDescriptor, GfxVertexBufferFrequency, makeTextureDescriptor2D } from "../gfx/platform/GfxPlatform.js";
import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js';
import { GfxRendererLayer, GfxRenderInstManager, makeSortKeyOpaque, setSortKeyDepth } from "../gfx/render/GfxRenderInstManager.js";
import { DeviceProgram } from "../Program.js";
import { LoadedTexture, TextureHolder, TextureMapping } from "../TextureHolder.js";
import { assert, assertExists, nArray, setBitFlagEnabled } from "../util.js";
import * as Viewer from '../viewer.js';
import { RSPOutput, Vertex } from "./f3dex2.js";
import { ModelTreeGroup, ModelTreeLeaf, ModelTreeNode, PropertyType } from "./map_shape.js";
//@ts-ignore
import program_glsl from './program.glsl';
import * as Tex from './tex.js';

class PaperMario64Program extends DeviceProgram {
public static a_Position = 0;
Expand Down Expand Up @@ -195,26 +195,24 @@ export class BackgroundBillboardRenderer {
this.textureHolder.fillTextureMapping(this.textureMappings[0], this.textureName);
}

public prepareToRender(renderInstManager: GfxRenderInstManager, renderInput: Viewer.ViewerRenderInput): void {
public prepareToRender(renderInstManager: GfxRenderInstManager, viewerInput: Viewer.ViewerRenderInput): void {
const renderInst = renderInstManager.newRenderInst();
renderInst.setDrawCount(3);
renderInst.sortKey = makeSortKeyOpaque(GfxRendererLayer.BACKGROUND, this.gfxProgram.ResourceUniqueId);
renderInst.setVertexInput(null, null, null);
renderInst.setBindingLayouts(backgroundBillboardBindingLayouts);
renderInst.setGfxProgram(this.gfxProgram);
renderInst.allocateUniformBuffer(BackgroundBillboardProgram.ub_Params, 4);

// Set our texture bindings.
renderInst.setSamplerBindingsFromTextureMappings(this.textureMappings);

// Upload new buffer data.
let offs = renderInst.getUniformBufferOffset(BackgroundBillboardProgram.ub_Params);
const d = renderInst.mapUniformBufferF32(BackgroundBillboardProgram.ub_Params);
const d = renderInst.allocateUniformBufferF32(BackgroundBillboardProgram.ub_Params, 4);
let offs = 0;

// Extract yaw
const view = renderInput.camera.viewMatrix;
const view = viewerInput.camera.viewMatrix;
const o = Math.atan2(-view[2], view[0]) / (Math.PI * 2) * 4;
const aspect = renderInput.backbufferWidth / renderInput.backbufferHeight;
const aspect = viewerInput.backbufferWidth / viewerInput.backbufferHeight;

offs += fillVec4(d, offs, aspect, -1, o, 0);
renderInstManager.submitRenderInst(renderInst);
Expand Down
4 changes: 2 additions & 2 deletions src/PaperMarioTTYD/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class BackgroundBillboardRenderer {
renderInst.setGfxProgram(this.gfxProgram);
renderInst.setSamplerBindingsFromTextureMappings(this.textureMappings);

let offs = renderInst.allocateUniformBuffer(BackgroundBillboardProgram.ub_Params, 4);
const d = renderInst.mapUniformBufferF32(BackgroundBillboardProgram.ub_Params);
const d = renderInst.allocateUniformBufferF32(BackgroundBillboardProgram.ub_Params, 4);
let offs = 0;

const aspect = renderInput.backbufferWidth / renderInput.backbufferHeight;

Expand Down
23 changes: 10 additions & 13 deletions src/StarFoxAdventures/Sky.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

import { mat4, vec3 } from 'gl-matrix';
import { GfxClipSpaceNearZ, GfxDevice } from '../gfx/platform/GfxPlatform.js';
import { GfxRenderInstManager } from "../gfx/render/GfxRenderInstManager.js";
import { GfxrAttachmentSlot, GfxrGraphBuilder, GfxrRenderTargetDescription, GfxrRenderTargetID } from '../gfx/render/GfxRenderGraph.js';
import { TDDraw } from "../SuperMarioGalaxy/DDraw.js";
import { GfxRenderInstManager } from "../gfx/render/GfxRenderInstManager.js";
import * as GX from '../gx/gx_enum.js';
import * as GX_Material from '../gx/gx_material.js';
import { DrawParams, GXMaterialHelperGfx, GXRenderHelperGfx, MaterialParams, SceneParams, calcLODBias, fillSceneParamsData, fillSceneParamsDataOnTemplate, ub_SceneParamsBufferSize } from '../gx/gx_render.js';
import { GXMaterialBuilder } from '../gx/GXMaterialBuilder.js';
import { DrawParams, GXMaterialHelperGfx, MaterialParams, fillSceneParamsDataOnTemplate, SceneParams, fillSceneParamsData, GXRenderHelperGfx, calcLODBias } from '../gx/gx_render.js';
import { getMatrixAxisZ } from '../MathHelpers.js';
import { TDDraw } from "../SuperMarioGalaxy/DDraw.js";

import { projectionMatrixConvertClipSpaceNearZ } from '../gfx/helpers/ProjectionHelpers.js';
import { ObjectRenderContext } from './objects.js';
import { SceneRenderContext, SFARenderLists, setGXMaterialOnRenderInst } from './render.js';
import { vecPitch } from './util.js';
import { getCamPos } from './util.js';
import { SFARenderLists, SceneRenderContext, setGXMaterialOnRenderInst } from './render.js';
import { getCamPos, vecPitch } from './util.js';
import { World } from './world.js';
import { projectionMatrixConvertClipSpaceNearZ } from '../gfx/helpers/ProjectionHelpers.js';

const materialParams = new MaterialParams();
const drawParams = new DrawParams();
Expand Down Expand Up @@ -50,17 +50,14 @@ export class Sky {
if (tex === null || tex === undefined)
return;

// Call renderHelper.pushTemplateRenderInst (not renderInstManager.pushTemplateRenderInst)
// to obtain a local SceneParams buffer
const template = renderHelper.pushTemplateRenderInst();
const template = renderInstManager.pushTemplate();

// Setup to draw in clip space
mat4.identity(scratchSceneParams.u_Projection);
scratchSceneParams.u_SceneTextureLODBias = calcLODBias(sceneCtx.viewerInput.backbufferWidth, sceneCtx.viewerInput.backbufferHeight);
projectionMatrixConvertClipSpaceNearZ(scratchSceneParams.u_Projection, device.queryVendorInfo().clipSpaceNearZ, GfxClipSpaceNearZ.NegativeOne);
let offs = template.getUniformBufferOffset(GX_Material.GX_Program.ub_SceneParams);
const d = template.mapUniformBufferF32(GX_Material.GX_Program.ub_SceneParams);
fillSceneParamsData(d, offs, scratchSceneParams);
const d = template.allocateUniformBufferF32(GX_Material.GX_Program.ub_SceneParams, ub_SceneParamsBufferSize);
fillSceneParamsData(d, 0, scratchSceneParams);

materialParams.m_TextureMapping[0].gfxTexture = tex.gfxTexture;
materialParams.m_TextureMapping[0].gfxSampler = tex.gfxSampler;
Expand Down
12 changes: 5 additions & 7 deletions src/StarFoxAdventures/render.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { mat4, vec3 } from 'gl-matrix';
import { CameraController } from '../Camera.js';
import { colorNewFromRGBA8, White } from '../Color.js';
Expand All @@ -7,7 +8,7 @@ import { GfxrAttachmentSlot, GfxrGraphBuilder, GfxrPass, GfxrPassScope, GfxrRend
import { GfxRenderInst, GfxRenderInstList, GfxRenderInstManager } from "../gfx/render/GfxRenderInstManager.js";
import * as GX from '../gx/gx_enum.js';
import * as GX_Material from '../gx/gx_material.js';
import { DrawParams, fillSceneParamsData, fillSceneParamsDataOnTemplate, GXMaterialHelperGfx, GXRenderHelperGfx, MaterialParams, SceneParams } from '../gx/gx_render.js';
import { DrawParams, fillSceneParamsData, GXMaterialHelperGfx, GXRenderHelperGfx, MaterialParams, SceneParams, ub_SceneParamsBufferSize } from '../gx/gx_render.js';
import { TDDraw } from '../SuperMarioGalaxy/DDraw.js';
import { TextureMapping } from '../TextureHolder.js';
import { nArray } from '../util.js';
Expand Down Expand Up @@ -134,16 +135,13 @@ export class SFARenderer implements Viewer.SceneGfx {
protected addWorldRenderPassesInner(device: GfxDevice, builder: GfxrGraphBuilder, renderInstManager: GfxRenderInstManager, sceneCtx: SceneRenderContext) {}

private renderHeatShimmer(device: GfxDevice, builder: GfxrGraphBuilder, renderInstManager: GfxRenderInstManager, mainColorTargetID: GfxrRenderTargetID, sourceColorResolveTextureID: GfxrResolveTextureID, sourceDepthTargetID: GfxrRenderTargetID, sceneCtx: SceneRenderContext) {
// Call renderHelper.pushTemplateRenderInst (not renderInstManager)
// to obtain a local SceneParams buffer
const template = this.renderHelper.pushTemplateRenderInst();
const template = renderInstManager.pushTemplate();

// Setup to draw in screen space
mat4.ortho(scratchSceneParams.u_Projection, 0.0, 640.0, 0.0, 480.0, 1.0, 100.0);
scratchSceneParams.u_SceneTextureLODBias = 0;
let offs = template.getUniformBufferOffset(GX_Material.GX_Program.ub_SceneParams);
const d = template.mapUniformBufferF32(GX_Material.GX_Program.ub_SceneParams);
fillSceneParamsData(d, offs, scratchSceneParams);
const d = template.allocateUniformBufferF32(GX_Material.GX_Program.ub_SceneParams, ub_SceneParamsBufferSize);
fillSceneParamsData(d, 0, scratchSceneParams);

// Extract pitch
const cameraFwd = scratchVec0;
Expand Down
19 changes: 9 additions & 10 deletions src/ZeldaWindWaker/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@ import { J3DModelInstance } from '../Common/JSYSTEM/J3D/J3DGraphBase.js';
import * as JPA from '../Common/JSYSTEM/JPA.js';
import { BTIData } from '../Common/JSYSTEM/JUTTexture.js';
import { dfRange } from '../DebugFloaters.js';
import { Frustum } from '../Geometry.js';
import { MathConstants, getMatrixAxisZ, getMatrixTranslation, projectionMatrixForFrustum, range } from '../MathHelpers.js';
import { SceneContext } from '../SceneBase.js';
import { TextureMapping } from '../TextureHolder.js';
import { projectionMatrixConvertClipSpaceNearZ } from '../gfx/helpers/ProjectionHelpers.js';
import { setBackbufferDescSimple, standardFullClearRenderPassDescriptor } from '../gfx/helpers/RenderGraphHelpers.js';
import { projectionMatrixReverseDepth } from '../gfx/helpers/ReversedDepthHelpers.js';
import { GfxClipSpaceNearZ, GfxDevice, GfxFormat, GfxRenderPass, GfxTexture, makeTextureDescriptor2D } from '../gfx/platform/GfxPlatform.js';
import { GfxRenderCache } from '../gfx/render/GfxRenderCache.js';
import { GfxrAttachmentSlot, GfxrRenderTargetDescription } from '../gfx/render/GfxRenderGraph.js';
import { GfxRenderInstList, GfxRenderInstManager } from '../gfx/render/GfxRenderInstManager.js';
import { GXRenderHelperGfx, SceneParams, calcLODBias, fillSceneParamsData } from '../gx/gx_render.js';
import { GX_Program } from '../gx/gx_material.js';
import { GXRenderHelperGfx, SceneParams, calcLODBias, fillSceneParamsData, ub_SceneParamsBufferSize } from '../gx/gx_render.js';
import { FlowerPacket, GrassPacket, TreePacket } from './Grass.js';
import { LegacyActor__RegisterFallbackConstructor } from './LegacyActor.js';
import { dDlst_2DStatic_c, d_a__RegisterConstructors } from './d_a.js';
import { d_a_sea } from './d_a_sea.js';
import { dBgS } from './d_bg.js';
import { EDemoCamFlags, EDemoMode, dDemo_manager_c } from './d_demo.js';
import { dDlst_list_Set, dDlst_list_c } from './d_drawlist.js';
import { dKankyo_create, dKy__RegisterConstructors, dKy_setLight, dScnKy_env_light_c } from './d_kankyo.js';
import { dKyw__RegisterConstructors } from './d_kankyo_wether.js';
import { dPa_control_c } from './d_particle.js';
import { Placename, PlacenameState, dPn__update, d_pn__RegisterConstructors } from './d_place_name.js';
import { dProcName_e } from './d_procname.js';
import { ResType, dRes_control_c } from './d_resorce.js';
import { dStage_dt_c_roomLoader, dStage_dt_c_roomReLoader, dStage_dt_c_stageInitLoader, dStage_dt_c_stageLoader, dStage_roomControl_c, dStage_roomStatus_c, dStage_stageDt_c } from './d_stage.js';
import { WoodPacket } from './d_wood.js';
import { fopAcM_create, fopAcM_searchFromName, fopAc_ac_c } from './f_op_actor.js';
import { cPhs__Status, fGlobals, fopDw_Draw, fopScn, fpcCt_Handler, fpcLy_SetCurrentLayer, fpcM_Management, fpcPf__Register, fpcSCtRq_Request, fpc_pc__ProfileList } from './framework.js';
import { dDemo_manager_c, EDemoCamFlags, EDemoMode } from './d_demo.js';
import { d_pn__RegisterConstructors, Placename, PlacenameState, dPn__update } from './d_place_name.js';
import { GX_Program } from '../gx/gx_material.js';
import { Frustum } from '../Geometry.js';
import { projectionMatrixReverseDepth } from '../gfx/helpers/ReversedDepthHelpers.js';
import { projectionMatrixConvertClipSpaceNearZ } from '../gfx/helpers/ProjectionHelpers.js';

type SymbolData = { Filename: string, SymbolName: string, Data: ArrayBufferSlice };
type SymbolMapData = { SymbolData: SymbolData[] };
Expand Down Expand Up @@ -595,9 +595,8 @@ export class WindWakerRenderer implements Viewer.SceneGfx {

mat4.copy(sceneParams.u_Projection, globals.camera.clipFromViewMatrix);
sceneParams.u_SceneTextureLODBias = calcLODBias(viewerInput.backbufferWidth, viewerInput.backbufferHeight);
let offs = template.getUniformBufferOffset(GX_Program.ub_SceneParams);
const d = template.mapUniformBufferF32(GX_Program.ub_SceneParams);
fillSceneParamsData(d, offs, sceneParams);
const d = template.allocateUniformBufferF32(GX_Program.ub_SceneParams, ub_SceneParamsBufferSize);
fillSceneParamsData(d, 0, sceneParams);

this.extraTextures.prepareToRender(device);

Expand Down
14 changes: 2 additions & 12 deletions src/gfx/render/GfxRenderInstManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ export class GfxRenderInst {

/**
* Allocates {@param wordCount} words from the uniform buffer and assigns it to the buffer
* slot at index {@param bufferIndex}. As a convenience, this also directly returns the same
* offset into the uniform buffer, in words, that would be returned by a subsequent call to
* {@see getUniformBufferOffset}.
* slot at index {@param bufferIndex}, and returns the index into the buffer.
*/
public allocateUniformBuffer(bufferIndex: number, wordCount: number): number {
assert(this._bindingDescriptors[0].bindingLayout.numUniformBuffers <= this._dynamicUniformBufferByteOffsets.length);
Expand All @@ -331,7 +329,7 @@ export class GfxRenderInst {

const dst = this._bindingDescriptors[0].uniformBufferBindings[bufferIndex];
dst.wordCount = wordCount;
return this.getUniformBufferOffset(bufferIndex);
return this._dynamicUniformBufferByteOffsets[bufferIndex] >>> 2;
}

/**
Expand All @@ -343,14 +341,6 @@ export class GfxRenderInst {
return this._uniformBuffer.mapBufferF32().subarray(wordOffset);
}

/**
* Returns the offset into the uniform buffer, in words, that is assigned to the buffer slot
* at index {@param bufferIndex}, to be used with e.g. {@see mapUniformBufferF32}.
*/
public getUniformBufferOffset(bufferIndex: number) {
return this._dynamicUniformBufferByteOffsets[bufferIndex] >>> 2;
}

/**
* Directly sets the uniform buffer assigned to the buffer slot at index {@param bufferIndex}
* to be {@param wordOffset}. Use this if you have already allocated a uniform buffer chunk through
Expand Down
6 changes: 2 additions & 4 deletions src/gx/gx_render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,14 @@ const sceneParams = new SceneParams();
export function fillSceneParamsDataOnTemplate(renderInst: GfxRenderInst, viewerInput: Viewer.ViewerRenderInput, customLODBias: number | null = null, sceneParamsScratch = sceneParams): void {
mat4.copy(sceneParamsScratch.u_Projection, viewerInput.camera.projectionMatrix);
sceneParams.u_SceneTextureLODBias = customLODBias !== null ? customLODBias : calcLODBias(viewerInput.backbufferWidth, viewerInput.backbufferHeight);
let offs = renderInst.getUniformBufferOffset(GX_Material.GX_Program.ub_SceneParams);
const d = renderInst.mapUniformBufferF32(GX_Material.GX_Program.ub_SceneParams);
fillSceneParamsData(d, offs, sceneParamsScratch);
let d = renderInst.allocateUniformBufferF32(GX_Material.GX_Program.ub_SceneParams, ub_SceneParamsBufferSize);
fillSceneParamsData(d, 0, sceneParamsScratch);
}

export class GXRenderHelperGfx extends GfxRenderHelper {
public override pushTemplateRenderInst(): GfxRenderInst {
const template = super.pushTemplateRenderInst();
template.setBindingLayouts(gxBindingLayouts);
template.allocateUniformBuffer(GX_Material.GX_Program.ub_SceneParams, ub_SceneParamsBufferSize);
return template;
}
}
Expand Down

0 comments on commit 456dd20

Please sign in to comment.