From 8cb39e3a26b809c591ce005bcb1d49ea68f612e1 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Wed, 30 Oct 2024 20:27:08 -0700 Subject: [PATCH] Dark Souls: WebGPU work --- src/DarkSouls/render.ts | 57 ++++++++++++++++++++++++----------------- src/Program.ts | 6 ++++- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/DarkSouls/render.ts b/src/DarkSouls/render.ts index 184205a0c..942d1a2d5 100644 --- a/src/DarkSouls/render.ts +++ b/src/DarkSouls/render.ts @@ -118,13 +118,14 @@ class BatchData { public primitiveIndexCounts: number[] = []; public primitiveIndexStarts: number[] = []; - constructor(device: GfxDevice, cache: GfxRenderCache, flverData: FLVERData, public batch: Batch, vertexBuffer: GfxCoalescedBuffer, indexBuffers: GfxCoalescedBuffer[], triangleIndexCounts: number[]) { + constructor(cache: GfxRenderCache, flverData: FLVERData, public batch: Batch, zeroBuffer: GfxCoalescedBuffer, vertexBuffer: GfxCoalescedBuffer, indexBuffers: GfxCoalescedBuffer[], triangleIndexCounts: number[]) { const flverInputState = flverData.flver.inputStates[batch.inputStateIndex]; const flverInputLayout = flverData.flver.inputLayouts[flverInputState.inputLayoutIndex]; - this.vertexBufferDescriptors = [vertexBuffer]; + this.vertexBufferDescriptors = [vertexBuffer, zeroBuffer]; const vertexAttributeDescriptors: GfxVertexAttributeDescriptor[] = []; + let attribBits = 0; for (let j = 0; j < flverInputLayout.vertexAttributes.length; j++) { const vertexAttributes = flverInputLayout.vertexAttributes[j]; const location = translateLocation(vertexAttributes); @@ -137,10 +138,26 @@ class BatchData { bufferByteOffset: vertexAttributes.offset, bufferIndex: 0, }); + + attribBits |= 1 << location; + } + + // Go through and fill zeroes with anything we missed. + for (let i = 0; i <= MaterialProgram_Base.a_AttribMax; i++) { + if (attribBits & (1 << i)) + continue; + + vertexAttributeDescriptors.push({ + location: i, + format: GfxFormat.F32_RGBA, + bufferByteOffset: 0, + bufferIndex: 1, + }); } const vertexBufferDescriptors: GfxInputLayoutBufferDescriptor[] = [ { byteStride: flverInputState.vertexSize, frequency: GfxVertexBufferFrequency.PerVertex, }, + { byteStride: 0x00, frequency: GfxVertexBufferFrequency.Constant, }, ]; this.inputLayout = cache.createInputLayout({ @@ -167,6 +184,7 @@ export class FLVERData { constructor(cache: GfxRenderCache, public flver: FLVER) { const vertexBufferDatas: ArrayBufferSlice[] = []; const indexBufferDatas: ArrayBufferSlice[] = []; + vertexBufferDatas.push(new ArrayBufferSlice(new ArrayBuffer(64))); for (let i = 0; i < flver.inputStates.length; i++) { vertexBufferDatas.push(flver.inputStates[i].vertexData); flver.inputStates[i].vertexData = null as unknown as ArrayBufferSlice; @@ -174,6 +192,8 @@ export class FLVERData { const vertexBuffers = coalesceBuffer(cache.device, GfxBufferUsage.Vertex, vertexBufferDatas); this.vertexBuffer = vertexBuffers[0].buffer; + const zeroBuffer = vertexBuffers.shift()!; + const triangleIndexCounts: number[] = []; for (let i = 0; i < flver.batches.length; i++) { @@ -194,7 +214,7 @@ export class FLVERData { for (let i = 0; i < flver.batches.length; i++) { const batch = flver.batches[i]; const coaVertexBuffer = vertexBuffers[batch.inputStateIndex]; - const batchData = new BatchData(cache.device, cache, this, batch, coaVertexBuffer, indexBuffers, triangleIndexCounts); + const batchData = new BatchData(cache, this, batch, zeroBuffer, coaVertexBuffer, indexBuffers, triangleIndexCounts); this.batchData.push(batchData); } } @@ -329,6 +349,7 @@ class MaterialProgram_Base extends DeviceProgram { public static a_Normal = 4; public static a_Tangent0 = 5; public static a_Tangent1 = 6; + public static a_AttribMax = 6; public static ub_SceneParams = 0; public static ub_MeshFragParams = 1; @@ -987,6 +1008,8 @@ precision mediump float; ${MaterialProgram_Base.BindingDefinitions} +layout(binding = 8) uniform samplerCube u_TextureDummy; + layout(std140) uniform ub_MeshFragParams { Mat4x3 u_WorldFromLocal[1]; DirectionalLight u_DirectionalLight; @@ -1051,6 +1074,8 @@ precision mediump float; ${MaterialProgram_Base.BindingDefinitions} +layout(binding = 8) uniform samplerCube u_TextureDummy; + layout(std140) uniform ub_MeshFragParams { Mat4x3 u_WorldFromLocal[1]; DirectionalLight u_DirectionalLight; @@ -1969,6 +1994,7 @@ class DepthOfFieldBlurProgram extends DeviceProgram { public static Common = ` uniform sampler2D u_TextureColor; +uniform sampler2D u_Texture2; layout(std140) uniform ub_Params { vec4 u_Misc[1]; @@ -2296,6 +2322,7 @@ void main() { class BloomBlur1Program extends DeviceProgram { public static Common = ` uniform sampler2D u_TextureColor; +uniform sampler2D u_Texture2; `; public override vert = ` @@ -2321,20 +2348,12 @@ void main() { // vec4 t_Color = vec4(0.0); -#if 0 - float t_TotalWeightInv = ${1.0 / Math.sqrt(MathConstants.TAU)}; - float t_Weight0 = exp(-0.5 * 0.0) * t_TotalWeightInv; - float t_Weight1 = exp(-0.5 * 1.0) * t_TotalWeightInv * 0.25; - float t_Weight2 = exp(-0.5 * 2.0) * t_TotalWeightInv * 0.25; - float t_Weight3 = exp(-0.5 * 4.0) * t_TotalWeightInv * 0.25; -#else const float t_Weight[4] = float[4]( 0.3989422804014327, 0.06049268112978584, 0.03669066579343498, 0.013497741628297016 ); -#endif // Ring 0 (center) t_Color += texture(SAMPLER_2D(u_TextureColor), v_TexCoord) * t_Weight[0]; @@ -2373,6 +2392,7 @@ class BloomBlur2Program extends DeviceProgram { public static Common = ` uniform sampler2D u_TextureColor; +uniform sampler2D u_Texture2; `; public override vert = ` @@ -2391,18 +2411,6 @@ void main() { // Bloom Blur 2 ("Bloom") appears to use a massive 15-tap per pass. // It also seems to use a standard deviation of 3 (hardcoded?) -#if 0 - float t_StdDevSq = pow(3.0, 2.0); - float t_Inv = 1.0 / sqrt(2.0f * M_PI * t_StdDevSq); - float t_TotalI = 0.0f; - - for (int i = 0; i < 8; i++) { - float t_Weight = exp(-0.5 * pow(i, 2.0) / t_StdDevSq) * t_Inv; - print("float t_Weight", i, " = ", t_Weight); - t_TotalI += t_Weight; - } - print("float t_TotalI = ", t_TotalI); -#else const float t_Weight[8] = float[8]( 0.1329807601338109, 0.12579440923099774, @@ -2413,7 +2421,6 @@ void main() { 0.017996988837729353, 0.008740629697903166 ); -#endif vec4 t_Color = vec4(0.0); t_Color += t_Weight[0] * texture(SAMPLER_2D(u_TextureColor), v_TexCoord); @@ -2447,6 +2454,7 @@ void main() { class BloomCombineProgram extends DeviceProgram { public static Common = ` uniform sampler2D u_TextureColor; +uniform sampler2D u_Texture2; layout(std140) uniform ub_Params { vec4 u_Misc[3]; @@ -2713,6 +2721,7 @@ class Bloom { class ToneCorrectProgram extends DeviceProgram { public static Common = ` uniform sampler2D u_TextureColor; +uniform sampler2D u_Texture2; layout(std140) uniform ub_Params { Mat4x3 u_ToneCorrectMatrix; diff --git a/src/Program.ts b/src/Program.ts index c8815e4a9..79b60cf31 100644 --- a/src/Program.ts +++ b/src/Program.ts @@ -5,7 +5,7 @@ import { GfxVendorInfo, GfxProgram, GfxDevice } from "./gfx/platform/GfxPlatform import { preprocessShader_GLSL } from "./gfx/shaderc/GfxShaderCompiler.js"; export class DeviceProgram { - public name: string = '(unnamed)'; + public name: string = '(unnamed DeviceProgram)'; // Compiled program. public preprocessedVert: string = ''; @@ -63,6 +63,10 @@ export class DeviceProgram { public associate(device: GfxDevice, program: GfxProgram): void { this._gfxDevice = device; this._gfxProgram = program; + + if (this.name === '(unnamed DeviceProgram)') + this.name = this.constructor.name; + this._gfxDevice.setResourceName(program, this.name); } private _editShader(n: 'vert' | 'frag' | 'both') {