Skip to content

Commit

Permalink
fix totalSize computation in BindGroupFormat which crashes dynamic bu…
Browse files Browse the repository at this point in the history
…ffer allocation
  • Loading branch information
fuzhenn committed Mar 10, 2025
1 parent 5bdf412 commit 10932eb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
6 changes: 6 additions & 0 deletions packages/reshader.gl/src/common/WebGLConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const GL_NEAREST = 0x2600;
export const GL_LINEAR = 0x2601;
export const GL_LINEAR_MIPMAP_LINEAR = 0x2703;

export const MAG_FILTER = 0x2800;
export const MIN_FILTER = 0x2801;
1 change: 1 addition & 0 deletions packages/reshader.gl/src/shader/ImageShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ImageShader extends MeshShader {
config.vert = vert;
config.frag = frag;
super({
name: 'image',
vert,
frag,
wgslVert,
Expand Down
3 changes: 1 addition & 2 deletions packages/reshader.gl/src/shader/Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,7 @@ export default class GPUShader extends GLShader {
const passEncoder: GPURenderPassEncoder = this._getCurrentRenderPassEncoder(device);
passEncoder.setPipeline(command.pipeline);

const { key, bindGroupFormat, pipeline, vertexInfo } = command;
const layout = pipeline.getBindGroupLayout(0);
const { key, bindGroupFormat, vertexInfo, layout } = command;
// 1. 生成shader uniform 需要的dynamic buffer
if (!this._buffers) {
this._buffers = {};
Expand Down
9 changes: 4 additions & 5 deletions packages/reshader.gl/src/shader/wgsl/image_frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ struct Scene {

@fragment
fn main(
@location(0) vTexCoord : vec2f
@location(0) vTexCoord : vec2f,
) -> @location(0) vec4f {
var fragColor = textureSample(baseColorTexture, baseColorTextureSampler, vTexCoord);
// fragColor *= scene.baseColor;
fragColor *= scene.baseColor;
#if HAS_DEBUG
var debugColor = textureSample(debugTexture, debugTextureSampler, vTexCoord);
fragColor = vec4f(
Expand All @@ -27,8 +27,7 @@ fn main(
);
#endif
if (fragColor.a < scene.alphaTest) {
// discard;
discard;
}
return fragColor * 1.0;
// return vec4f(1.0, 0.0, 0.0, 1.0);
return fragColor * scene.opacity;
}
2 changes: 1 addition & 1 deletion packages/reshader.gl/src/shader/wgsl/image_vert.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct Uniforms {
projViewModelMatrix : mat4x4f,
projViewModelMatrix : mat4x4f
}

@group(0) @binding(0) var<uniform> uniforms : Uniforms;
Expand Down
7 changes: 4 additions & 3 deletions packages/reshader.gl/src/webgpu/BindGroupFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import GraphicsDevice from "./GraphicsDevice";
import { ShaderUniforms } from "../types/typings";
import AbstractTexture from "../AbstractTexture";
import GraphicsTexture from "./GraphicsTexture";
import { roundUp } from "./common/math";

let uuid = 0;

Expand Down Expand Up @@ -63,12 +64,12 @@ export default class BindGroupFormat {
let index = this._shaderUniforms.index;
this._shaderUniforms[index++] = uniform;
this._shaderUniforms.index = index;
this._shaderUniforms.totalSize += uniform.size;
this._shaderUniforms.totalSize += roundUp(uniform.size, this.alignment);;
} else {
let index = this._meshUniforms.index;
this._meshUniforms[index++] = uniform;
this._meshUniforms.index = index;
this._meshUniforms.totalSize += uniform.size;
this._meshUniforms.totalSize += roundUp(uniform.size, this.alignment);
}
}
}
Expand Down Expand Up @@ -122,7 +123,7 @@ export default class BindGroupFormat {
buffer: allocation.gpuBuffer,
// offset 永远设为0,在setBindGroup中设置dynamicOffsets
// offset: 0,
size: Math.max(group.size, this.alignment)
size: roundUp(group.size, this.alignment)
}
});
}
Expand Down
6 changes: 4 additions & 2 deletions packages/reshader.gl/src/webgpu/CommandBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ export default class CommandBuilder {
const pipeline = this._createPipeline(device, vert, vertexInfo, frag, layout, mesh, pipelineDesc, fbo);

const bindGroupMapping = this._createBindGroupMapping(vertGroups, fragGroups, mesh);
const bindGroupFormat = new BindGroupFormat(this.name, bindGroupMapping, device.wgpu.limits.minUniformBufferOffsetAlignment);
const alignment = device.wgpu.limits.minUniformBufferOffsetAlignment;
const bindGroupFormat = new BindGroupFormat(this.name, bindGroupMapping, alignment);
const activeAttributes = this._getActiveAttributes(vertexInfo);

return {
layout,
pipeline,
vertexInfo,
bindGroupMapping,
bindGroupFormat,
bindGroupMapping,
activeAttributes
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/reshader.gl/src/webgpu/DynamicBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ShaderUniformValue } from "../types/typings";
import DynamicBufferPool, { DynamicBufferAllocation } from "./DynamicBufferPool";
import { isArray, isFunction } from "../common/Util";
import DynamicOffsets from "./DynamicOffsets";
import { roundUp } from "./common/math";

export default class DynamicBuffer {
bindgroupMapping: any;
Expand Down Expand Up @@ -45,13 +46,13 @@ export default class DynamicBuffer {
const size = member.size;
this._fillValue(storage, offset, size, value);
}
dynamicOffset += Math.max(mapping[i].size, bufferAlignment);
dynamicOffset += roundUp(mapping[i].size, bufferAlignment);
} else if (uniform.resourceType === ResourceType.Uniform) {
dynamicOffsets.addItem({ binding: uniform.binding, offset: dynamicOffset });
const value = uniformValues[uniform.name];
const size = isFunction(uniform.size) ? uniform.size() : uniform.size;
this._fillValue(storage, dynamicOffset, size, value);
dynamicOffset += Math.max(size, bufferAlignment);
dynamicOffset += roundUp(size, bufferAlignment);
}
}

Expand Down

0 comments on commit 10932eb

Please sign in to comment.