Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Apr 10, 2024
1 parent f8a1329 commit ebc2b50
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
25 changes: 15 additions & 10 deletions modules/core/src/adapter/resources/render-pipeline.ts
Expand Up @@ -40,10 +40,12 @@ export type RenderPipelineProps = ResourceProps & {
/** Parameters that are controlled by pipeline */
parameters?: RenderPipelineParameters;

/** Number of vertices */
vertexCount?: number;
/** Number of instances */
instanceCount?: number;
// /** Use instanced rendering? */
// isInstanced?: boolean;
// /** Number of instances */
// instanceCount?: number;
// /** Number of vertices */
// vertexCount?: number;

/** Buffers, Textures, Samplers for the shader bindings */
bindings?: Record<string, Binding>;
Expand Down Expand Up @@ -71,8 +73,9 @@ export abstract class RenderPipeline extends Resource<RenderPipelineProps> {
topology: 'triangle-list',
parameters: {},

vertexCount: 0,
instanceCount: undefined,
// isInstanced: false,
// instanceCount: 0,
// vertexCount: 0,

bindings: {},
uniforms: {}
Expand Down Expand Up @@ -116,12 +119,14 @@ export abstract class RenderPipeline extends Resource<RenderPipelineProps> {
topology?: PrimitiveTopology;
/** vertex attributes */
vertexArray: VertexArray;
/** Number of "rows" in index buffer */
indexCount?: number;
/** Number of "rows" in 'vertex' buffers */
vertexCount?: number;
/** Use instanced rendering? */
isInstanced?: boolean;
/** Number of "rows" in 'instance' buffers */
instanceCount?: number;
/** Number of "rows" in 'vertex' buffers */
vertexCount?: number;
/** Number of "rows" in index buffer */
indexCount?: number;
/** First vertex to draw from */
firstVertex?: number;
/** First index to draw from */
Expand Down
52 changes: 37 additions & 15 deletions modules/engine/src/model/model.ts
Expand Up @@ -45,19 +45,20 @@ export type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs' | 'bindings'> & {

/** Shader inputs, used to generated uniform buffers and bindings */
shaderInputs?: ShaderInputs;

/** Bindings */
bindings?: Record<string, Binding | AsyncTexture>;

/** Parameters that are built into the pipeline */
parameters?: RenderPipelineParameters;

/** Geometry */
geometry?: GPUGeometry | Geometry | null;

/** Vertex count */
vertexCount?: number;
/** Use instanced rendering? */
isInstanced?: boolean;
/** instance count */
instanceCount?: number;
/** Vertex count */
vertexCount?: number;

indexBuffer?: Buffer | null;
/** @note this is really a map of buffers, not a map of attributes */
Expand Down Expand Up @@ -113,6 +114,10 @@ export class Model {
constantAttributes: {},
varyings: [],

isInstanced: false,
instanceCount: 0,
vertexCount: 0,

shaderInputs: undefined!,
pipelineFactory: undefined!,
shaderFactory: undefined!,
Expand Down Expand Up @@ -147,10 +152,12 @@ export class Model {

// Dynamic properties

/** Use instanced rendering */
isInstanced: boolean = false;
/** instance count. `undefined` means not instanced */
instanceCount: number = 0;
/** Vertex count */
vertexCount: number;
/** instance count. `undefined` means not instanced */
instanceCount: number | undefined = undefined;

/** Index buffer */
indexBuffer: Buffer | null = null;
Expand Down Expand Up @@ -277,12 +284,15 @@ export class Model {
}

// Apply any dynamic settings that will not trigger pipeline change
if (props.vertexCount) {
this.setVertexCount(props.vertexCount);
if (props.isInstanced) {
this.setInstanced(props.isInstanced);
}
if (props.instanceCount) {
this.setInstanceCount(props.instanceCount);
}
if (props.vertexCount) {
this.setVertexCount(props.vertexCount);
}
if (props.indexBuffer) {
this.setIndexBuffer(props.indexBuffer);
}
Expand Down Expand Up @@ -379,6 +389,7 @@ export class Model {
drawSuccess = this.pipeline.draw({
renderPass,
vertexArray: this.vertexArray,
isInstanced: this.isInstanced,
vertexCount: this.vertexCount,
instanceCount: this.instanceCount,
indexCount,
Expand Down Expand Up @@ -474,13 +485,10 @@ export class Model {

// Update dynamic fields

/**
* Updates the vertex count (used in draw calls)
* @note Any attributes with stepMode=vertex need to be at least this big
*/
setVertexCount(vertexCount: number): void {
this.vertexCount = vertexCount;
this.setNeedsRedraw('vertexCount');
/** Specify whether instanced rendering should be used */
setInstanced(isInstanced: boolean = true): void {
this.isInstanced = isInstanced;
this.setNeedsRedraw('instanced');
}

/**
Expand All @@ -489,9 +497,22 @@ export class Model {
*/
setInstanceCount(instanceCount: number): void {
this.instanceCount = instanceCount;
if (instanceCount > 0) {
this.isInstanced = true;
}
this.setNeedsRedraw('instanceCount');
}

/**
* Updates the vertex count (used in draw calls)
* @note Any attributes with stepMode=vertex need to be at least this big
*/
setVertexCount(vertexCount: number): void {
this.vertexCount = vertexCount;
this.setNeedsRedraw('vertexCount');
}

/** Set the shader inputs */
setShaderInputs(shaderInputs: ShaderInputs): void {
this.shaderInputs = shaderInputs;
this._uniformStore = new UniformStore(this.shaderInputs.modules);
Expand All @@ -503,6 +524,7 @@ export class Model {
this.setNeedsRedraw('shaderInputs');
}

/** Update uniform buffers from the model's shader inputs */
updateShaderInputs(): void {
this._uniformStore.setUniforms(this.shaderInputs.getUniformValues());
// TODO - this is already tracked through buffer/texture update times?
Expand Down
3 changes: 2 additions & 1 deletion modules/webgl/src/adapter/resources/webgl-render-pipeline.ts
Expand Up @@ -158,6 +158,7 @@ export class WEBGLRenderPipeline extends RenderPipeline {
parameters?: RenderPipelineParameters;
topology?: PrimitiveTopology;
vertexArray: VertexArray;
isInstanced?: boolean;
vertexCount?: number;
indexCount?: number;
instanceCount?: number;
Expand All @@ -175,6 +176,7 @@ export class WEBGLRenderPipeline extends RenderPipeline {
vertexCount,
// indexCount,
instanceCount,
isInstanced = false,
firstVertex = 0,
// firstIndex,
// firstInstance,
Expand All @@ -186,7 +188,6 @@ export class WEBGLRenderPipeline extends RenderPipeline {
const isIndexed: boolean = Boolean(vertexArray.indexBuffer);
const glIndexType = (vertexArray.indexBuffer as WEBGLBuffer)?.glIndexType;
// Note that we sometimes get called with 0 instances
const isInstanced: boolean = Number(instanceCount) >= 0;

// If we are using async linking, we need to wait until linking completes
if (this.linkStatus !== 'success') {
Expand Down
2 changes: 1 addition & 1 deletion test/apps/tree-shaking/app.js
Expand Up @@ -52,7 +52,7 @@ function makeInstancedCube(gl) {
const colors = new Float32Array(SIDE * SIDE * 3).map(() => Math.random() * 0.75 + 0.25);

return new Cube(gl, {
isInstanced: 1,
isInstanced: true,
instanceCount: SIDE * SIDE,
attributes: {
instanceOffsets: {value: offsets, size: 2, divisor: 1},
Expand Down

0 comments on commit ebc2b50

Please sign in to comment.