Skip to content

Commit

Permalink
refactor(mesh): refactor model and renderstate
Browse files Browse the repository at this point in the history
  • Loading branch information
junwei.gu committed Jul 26, 2023
1 parent 563c909 commit f8b4eea
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 186 deletions.
24 changes: 10 additions & 14 deletions example/material/shaderMaterial.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@
}
}
],
vert: (defines) => {
return `
vert: `
struct MaterialUniform {
modelMatrix: mat4x4<f32>,
color:vec3<f32>,
}
struct VertexInput {
//auto Fill
@location(${defines.positionLocation}) position: vec3<f32>,
@location(${defines.normalLocation}) normal: vec3<f32>,
@location(${defines.uvLocation}) uv: vec2<f32>,
@location(positionLocation) position: vec3<f32>,
@location(normalLocation) normal: vec3<f32>,
@location(uvLocation) uv: vec2<f32>,
}
struct VertexOutput {
@builtin(position) position : vec4<f32>,
Expand All @@ -100,19 +99,17 @@
cameraPosition: vec3<f32>,
};
// Guaranteed to be consistent with the ShaderMaterial type
@binding(${defines.customBinding}) @group(0) var<uniform> matUniform : MaterialUniform;
@binding(${defines.cameraBinding}) @group(1) var<uniform> cameraUniform : CameraUniform;
@binding(customBinding) @group(0) var<uniform> matUniform : MaterialUniform;
@binding(cameraBinding) @group(1) var<uniform> cameraUniform : CameraUniform;
@vertex
fn main(input:VertexInput) -> VertexOutput {
var output : VertexOutput;
output.position = cameraUniform.projectionMatrix * cameraUniform.viewMatrix *matUniform.modelMatrix*vec4<f32>(input.position,1.0);
output.fragPosition = vec4(input.uv,input.uv.x,1.0);
return output;
}
`;
},
frag: (defines) => {
return `
`,
frag: `
struct VertexOutput {
@builtin(position) position : vec4<f32>,
@location(0) fragPosition: vec4<f32>,
Expand All @@ -121,13 +118,12 @@
modelMatrix: mat4x4<f32>,
color:vec3<f32>,
}
@binding(${defines.customBinding}) @group(0) var<uniform> matUniform : MaterialUniform;
@binding(customBinding) @group(0) var<uniform> matUniform : MaterialUniform;
@fragment
fn main(input:VertexOutput) -> @location(0) vec4<f32> {
return vec4(input.fragPosition.xyz*matUniform.color,1.0);
}
`;
}
`
});
const boxPrimitive = new Mesh(boxGeometry, boxMaterial);
scene.add(boxPrimitive);
Expand Down
6 changes: 4 additions & 2 deletions example/native/cube1.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@
format: "depth24plus"
}
},
count: 36,
instances: 1
draw: {
count: 36,
instanceCount: 1
}
});

function animate() {
Expand Down
6 changes: 4 additions & 2 deletions example/native/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@
format: "depth24plus"
}
},
count: 36,
instances: 1
draw: {
count: 36,
instanceCount: 1
}
});

function animate() {
Expand Down
6 changes: 4 additions & 2 deletions example/native/particles.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@
format: "depth24plus"
}
},
count: 6,
instances: numParticles
draw: {
count: 6,
instanceCount: numParticles
}
});
const simulateModel = new Model({
shaderId: "simulate",
Expand Down
12 changes: 8 additions & 4 deletions example/native/textureCube.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@
format: "depth24plus"
}
},
count: 36,
instances: 1
draw: {
count: 36,
instanceCount: 1
}
});
const model1 = new Model({
shaderId: "model",
Expand Down Expand Up @@ -289,8 +291,10 @@
format: "depth24plus"
}
},
count: 36,
instances: 1
draw: {
count: 36,
instanceCount: 1
}
});

function animate() {
Expand Down
111 changes: 36 additions & 75 deletions src/core/WebGPUTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BindGroupEntity from "../render/BindGroupEntity";
import BindGroupLayout from "../render/BindGroupLayout";
import Buffer from "../render/Buffer";
import IndexBuffer from "../render/IndexBuffer";
import QuerySet from "../render/QuerySet";
import { RenderState } from "../render/RenderState";
import RenderTarget from "../render/RenderTarget";
import Sampler from "../render/Sampler";
Expand Down Expand Up @@ -187,7 +188,7 @@ export type bufferLayoutType = {
minBindingSize?: number;
};
// renderstate
export type DepthStencil = {
export type DepthStencilProps = {
format: TextureFormat;
depthWriteEnabled: boolean;
depthCompare: CompareFunction;
Expand All @@ -209,19 +210,19 @@ export type DepthStencil = {
depthBiasSlopeScale?: number;
depthBiasClamp?: number;
};
export type PrimitiveState = {
export type PrimitiveStateProps = {
frontFace?: FrontFace;
cullMode?: CullMode;
unclippedDepth?: boolean;
topology?: PrimitiveTopology;
stripIndexFormat?: IndexFormat;
};
export type MultiSample = {
export type MultiSampleProps = {
count?: number;
mask?: number;
alphaToCoverageEnabled?: boolean;
};
export type Target = {
export type TargetProps = {
format: TextureFormat;
blend?: {
color: {
Expand All @@ -237,36 +238,38 @@ export type Target = {
};
writeMask: ColorWriteFlags;
};
export type BlendConstant = {
export type BlendConstantProp = {
r: number;
g: number;
b: number;
a: number;
};
export type RenderStateProps = {
depthStencil?: DepthStencil;
primitive?: PrimitiveState;
multisample?: MultiSample;
depthStencil?: DepthStencilProps;
primitive?: PrimitiveStateProps;
multisample?: MultiSampleProps;
stencilReference?: number;
targets?: Array<Target>;
viewport?: { x: number; y: number; width: number; height: number };
blendConstant?: BlendConstant;
targets?: Array<TargetProps>;
viewport?: ViewPortProps;
blendConstant?: BlendConstantProp;
scissorTestEnabled?: boolean;
stencilEnabled?: boolean;
};
export type ViewPort = {
export type ViewPortProps = {
x?: number;
y?: number;
width?: number;
height?: number;
minDepth?: number;
maxDepth?: number;
variable?: boolean;
};
export type ScissorTest = {
export type ScissorTestProps = {
x: number;
y: number;
width: number;
height: number;
variable?: boolean;
};
export type ShaderMaterialParms = {
type?: string;
Expand Down Expand Up @@ -368,6 +371,8 @@ export type DrawCommandParams = {
lightShaderData?: ShaderData;

useLight?: boolean;

drawParams?: DrawParmas;
};

export type ModelParams = {
Expand All @@ -381,8 +386,6 @@ export type ModelParams = {
};
uniformBuffers?: Array<UniformBufferProp>;
renderState?: RenderStateProp;
count?: number;
instances?: number;
indices?: Array<number>;
draw?: DrawParmas;
dispatch?: { x?: number; y?: number; z?: number };
Expand Down Expand Up @@ -413,66 +416,13 @@ export type AttributeProp = {
};
export type RenderStateProp = {
stencilReference: number;
blendConstant?: {
r: number;
g: number;
b: number;
a: number;
};
multiSample?: {
count: number;
mask: number;
alphaToCoverageEnabled: boolean;
};
scissorTest?: {
x: number;
y: number;
width: number;
height: number;
};
viewPort?: {
x: number;
y: number;
width: number;
height: number;
minDepth: number;
maxDepth: number;
};
primitive?: {
frontFace: string;
cullMode: string;
unclippedDepth: boolean;
topology: string;
};
depthStencil?: {
format: string;
depthWriteEnabled: boolean;
depthCompare: string;
stencilReadMask: number;
stencilWriteMask: number;
stencilFrontCompare: string;
stencilFrontFailOp: string;
stencilFrontDepthFailOp: string;
stencilFrontPassOp: string;

stencilBackCompare: string;
stencilBackFailOp: string;
stencilBackDepthFailOp: string;
stencilBackPassOp: string;
depthBias: number;
depthBiasSlopeScale: number;
depthBiasClamp: number;
};
targets?: Array<{
format?: string;
blendColorOperation?: string;
blendColorSrcFactor?: string;
blendColorDstFactor?: string;
blendAlphaOperation?: string;
blendAlphaSrcFactor?: string;
blendAlphaDstFactor?: string;
writeMask: GPUColorWrite;
}>;
blendConstant?: BlendConstantProp;
multiSample?: MultiSampleProps;
scissorTest?: ScissorTestProps;
viewPort?: ViewPortProps;
primitive?: PrimitiveStateProps;
depthStencil?: DepthStencilProps;
targets?: Array<TargetProps>;
};
export type DrawParmas = {
count?: number; // The number of indices to draw./The number of vertices to draw.
Expand Down Expand Up @@ -524,3 +474,14 @@ export enum ShaderMainStage {
FRAG = "fragment",
COMPUTE = "compute"
}
export type RenderModelParams = {
device: GPUDevice;
passEncoder: GPURenderPassEncoder;
querySet?: QuerySet;
viewPort?: ViewPortProps;
scissorTest?: ScissorTestProps;
};
export type ComputeModelParams = {
device: GPUDevice;
passEncoder: GPUComputePassEncoder;
};
6 changes: 4 additions & 2 deletions src/mesh/Mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export class Mesh extends RenderObject {
vertexBuffers: this.geometry.vertexBuffers,
indexBuffer: this.geometry.indexBuffer,
shaderData: this.material.shaderData,
instances: this.instanceCount,
count: this.geometry.count,
drawParams: {
count: this.geometry.count,
instanceCount: this.instanceCount
},
renderState: this.material.renderState,
shaderSource: this.material.shaderSource,
lightShaderData: this.material.light ? lightManger?.lightShaderData : undefined,
Expand Down
Loading

0 comments on commit f8b4eea

Please sign in to comment.