Skip to content

Commit

Permalink
WebGL1: removed supportsInstancing flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Valigursky committed Apr 29, 2024
1 parent 034b18d commit ddc6237
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 50 deletions.
74 changes: 36 additions & 38 deletions examples/src/examples/graphics/hardware-instancing/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,46 +81,44 @@ assetListLoader.load(() => {
// add the box entity to the hierarchy
app.root.addChild(cylinder);

if (app.graphicsDevice.supportsInstancing) {
// number of instances to render
const instanceCount = 1000;

// store matrices for individual instances into array
const matrices = new Float32Array(instanceCount * 16);
let matrixIndex = 0;

const radius = 5;
const pos = new pc.Vec3();
const rot = new pc.Quat();
const scl = new pc.Vec3();
const matrix = new pc.Mat4();

for (let i = 0; i < instanceCount; i++) {
// generate random positions / scales and rotations
pos.set(
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5
);
scl.set(0.1 + Math.random() * 0.1, 0.1 + Math.random() * 0.3, 0.1 + Math.random() * 0.1);
rot.setFromEulerAngles(i * 30, i * 50, i * 70);
matrix.setTRS(pos, rot, scl);

// copy matrix elements into array of floats
for (let m = 0; m < 16; m++) matrices[matrixIndex++] = matrix.data[m];
}

// create static vertex buffer containing the matrices
const vbFormat = pc.VertexFormat.getDefaultInstancingFormat(app.graphicsDevice);
const vertexBuffer = new pc.VertexBuffer(app.graphicsDevice, vbFormat, instanceCount, {
data: matrices
});

// initialize instancing using the vertex buffer on meshInstance of the created box
const cylinderMeshInst = cylinder.render.meshInstances[0];
cylinderMeshInst.setInstancing(vertexBuffer);
// number of instances to render
const instanceCount = 1000;

// store matrices for individual instances into array
const matrices = new Float32Array(instanceCount * 16);
let matrixIndex = 0;

const radius = 5;
const pos = new pc.Vec3();
const rot = new pc.Quat();
const scl = new pc.Vec3();
const matrix = new pc.Mat4();

for (let i = 0; i < instanceCount; i++) {
// generate random positions / scales and rotations
pos.set(
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5,
Math.random() * radius - radius * 0.5
);
scl.set(0.1 + Math.random() * 0.1, 0.1 + Math.random() * 0.3, 0.1 + Math.random() * 0.1);
rot.setFromEulerAngles(i * 30, i * 50, i * 70);
matrix.setTRS(pos, rot, scl);

// copy matrix elements into array of floats
for (let m = 0; m < 16; m++) matrices[matrixIndex++] = matrix.data[m];
}

// create static vertex buffer containing the matrices
const vbFormat = pc.VertexFormat.getDefaultInstancingFormat(app.graphicsDevice);
const vertexBuffer = new pc.VertexBuffer(app.graphicsDevice, vbFormat, instanceCount, {
data: matrices
});

// initialize instancing using the vertex buffer on meshInstance of the created box
const cylinderMeshInst = cylinder.render.meshInstances[0];
cylinderMeshInst.setInstancing(vertexBuffer);

// Set an update function on the app's update event
let angle = 0;
app.on('update', function (dt) {
Expand Down
7 changes: 7 additions & 0 deletions src/deprecated/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,13 @@ Object.defineProperty(GraphicsDevice.prototype, 'webgl2', {
}
});

Object.defineProperty(GraphicsDevice.prototype, 'supportsInstancing', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#supportsInstancing is deprecated as it is always true.');
return true;
}
});

Object.defineProperty(GraphicsDevice.prototype, 'textureHalfFloatUpdatable', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#textureHalfFloatUpdatable is deprecated as it is always true.');
Expand Down
8 changes: 0 additions & 8 deletions src/platform/graphics/graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,6 @@ class GraphicsDevice extends EventHandler {
/** @type {boolean} */
insideRenderPass = false;

/**
* True if hardware instancing is supported.
*
* @type {boolean}
* @readonly
*/
supportsInstancing;

/**
* True if the device supports uniform buffers.
*
Expand Down
1 change: 0 additions & 1 deletion src/platform/graphics/null/null-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class NullGraphicsDevice extends GraphicsDevice {
this.maxColorAttachments = 8;
this.maxPixelRatio = 1;
this.maxAnisotropy = 16;
this.supportsInstancing = true;
this.supportsUniformBuffers = false;
this.supportsVolumeTextures = true;
this.supportsBoneTextures = true;
Expand Down
2 changes: 0 additions & 2 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,6 @@ class WebglGraphicsDevice extends GraphicsDevice {
this.supportsMsaa = contextAttribs?.antialias ?? false;
this.supportsStencil = contextAttribs?.stencil ?? false;

this.supportsInstancing = !!this.extInstancing;

// Query parameter values from the WebGL context
this.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
this.maxCubeMapSize = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE);
Expand Down
1 change: 0 additions & 1 deletion src/platform/graphics/webgpu/webgpu-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
this.maxAnisotropy = 16;
this.fragmentUniformsCount = limits.maxUniformBufferBindingSize / 16;
this.vertexUniformsCount = limits.maxUniformBufferBindingSize / 16;
this.supportsInstancing = true;
this.supportsUniformBuffers = true;
this.supportsVolumeTextures = true;
this.supportsBoneTextures = true;
Expand Down

0 comments on commit ddc6237

Please sign in to comment.