Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of baseVertex support #6978

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/extras/mini-stats/render2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Render2d {
type: PRIMITIVE_TRIANGLES,
indexed: true,
base: 0,
baseVertex: 0,
count: 0
};
this.quads = 0;
Expand Down
1 change: 1 addition & 0 deletions src/platform/graphics/transform-feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class TransformFeedback {
device.draw({
type: PRIMITIVE_POINTS,
base: 0,
baseVertex: 0,
count: this._inputBuffer.numVertices,
indexed: false
});
Expand Down
1 change: 1 addition & 0 deletions src/platform/graphics/webgpu/webgpu-clear-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DepthState } from '../depth-state.js';
const primitive = {
type: PRIMITIVE_TRISTRIP,
base: 0,
baseVertex: 0,
count: 4,
indexed: false
};
Expand Down
39 changes: 38 additions & 1 deletion src/platform/graphics/webgpu/webgpu-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,43 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
_uniqueLocations.clear();
}

/**
* Submits a graphical primitive to the hardware for immediate rendering.
*
* @param {object} primitive - Primitive object describing how to submit current vertex/index
* buffers.
* @param {number} primitive.type - The type of primitive to render. Can be:
*
* - {@link PRIMITIVE_POINTS}
* - {@link PRIMITIVE_LINES}
* - {@link PRIMITIVE_LINELOOP}
* - {@link PRIMITIVE_LINESTRIP}
* - {@link PRIMITIVE_TRIANGLES}
* - {@link PRIMITIVE_TRISTRIP}
* - {@link PRIMITIVE_TRIFAN}
*
* @param {number} primitive.base - The offset of the first index or vertex to dispatch in the
* draw call.
* @param {number} primitive.baseVertex - The number added to each index value before indexing into
* the vertex buffers.
* @param {number} primitive.count - The number of indices or vertices to dispatch in the draw
* call.
* @param {boolean} [primitive.indexed] - True to interpret the primitive as indexed, thereby
* using the currently set index buffer and false otherwise.
* @param {number} [numInstances] - The number of instances to render when using instancing.
* Defaults to 1.
* @param {boolean} [keepBuffers] - Optionally keep the current set of vertex / index buffers /
* VAO. This is used when rendering of multiple views, for example under WebXR.
* @example
* // Render a single, unindexed triangle
* device.draw({
* type: pc.PRIMITIVE_TRIANGLES,
* base: 0,
* baseVertex: 0,
* count: 3,
* indexed: false
* });
*/
draw(primitive, numInstances = 1, keepBuffers) {

if (this.shader.ready && !this.shader.failed) {
Expand Down Expand Up @@ -574,7 +611,7 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
if (ib) {
this.indexBuffer = null;
passEncoder.setIndexBuffer(ib.impl.buffer, ib.impl.format);
passEncoder.drawIndexed(primitive.count, numInstances, primitive.base, 0, 0);
passEncoder.drawIndexed(primitive.count, numInstances, primitive.base, primitive.baseVertex || 0, 0);
} else {
passEncoder.draw(primitive.count, numInstances, primitive.base, 0);
}
Expand Down
7 changes: 5 additions & 2 deletions src/scene/batching/batch-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ class BatchManager {
batch = new Batch(meshInstances, dynamic, batchGroupId);
this._batchList.push(batch);

let indexBase, numIndices, indexData;
let indexBase, indexBaseVertex, numIndices, indexData;
let verticesOffset = 0;
let indexOffset = 0;
let transform;
Expand Down Expand Up @@ -738,6 +738,7 @@ class BatchManager {
// index buffer
if (mesh.primitive[0].indexed) {
indexBase = mesh.primitive[0].base;
indexBaseVertex = mesh.primitive[0].baseVertex || 0;
numIndices = mesh.primitive[0].count;

// source index buffer data mapped to its format
Expand All @@ -746,6 +747,8 @@ class BatchManager {

} else { // non-indexed

indexBaseVertex = 0;

const primitiveType = mesh.primitive[0].type;
if (primitiveType === PRIMITIVE_TRIFAN || primitiveType === PRIMITIVE_TRISTRIP) {
if (mesh.primitive[0].count === 4) {
Expand All @@ -760,7 +763,7 @@ class BatchManager {
}

for (let j = 0; j < numIndices; j++) {
indices[j + indexOffset] = indexData[indexBase + j] + verticesOffset;
indices[j + indexOffset] = indexData[indexBase + j] + indexBaseVertex + verticesOffset;
}

indexOffset += numIndices;
Expand Down
1 change: 1 addition & 0 deletions src/scene/graphics/quad-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { processShader } from '../shader-lib/utils.js';
const _quadPrimitive = {
type: PRIMITIVE_TRISTRIP,
base: 0,
baseVertex: 0,
count: 4,
indexed: false
};
Expand Down
5 changes: 4 additions & 1 deletion src/scene/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ class Mesh extends RefCountedObject {
*
* - `base` is the offset of the first index or vertex to dispatch in the draw call.
* - `count` is the number of indices or vertices to dispatch in the draw call.
* - `baseVertex` is the number added to each index value before indexing into the vertex buffers. (available only for WebGPU and Batching)
* - `indexed` specifies whether to interpret the primitive as indexed, thereby using the
* currently set index buffer.
*
* @type {{type: number, base: number, count: number, indexed?: boolean}[]}
* @type {{type: number, base: number, count: number, baseVertex: number, indexed?: boolean}[]}
*/
primitive = [{
type: 0,
base: 0,
baseVertex: 0,
count: 0
}];

Expand Down Expand Up @@ -1044,6 +1046,7 @@ class Mesh extends RefCountedObject {
this.primitive[RENDERSTYLE_POINTS] = {
type: PRIMITIVE_POINTS,
base: 0,
baseVertex: 0,
count: this.vertexBuffer ? this.vertexBuffer.numVertices : 0,
indexed: false
};
Expand Down