Skip to content

Commit

Permalink
Optimize texture upload of morph targets (#5037)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky authored Feb 3, 2023
1 parent 1b3c554 commit 1aed0a0
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/scene/morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ class Morph extends RefCountedObject {
}

// build texture for each delta array, all textures are the same size
const arraySize = this.morphTextureWidth * this.morphTextureHeight * numComponents;
const packedDeltas = halfFloat ? new Uint16Array(arraySize) : new Float32Array(arraySize);
for (let i = 0; i < deltaArrays.length; i++) {
const data = deltaArrays[i];

const texture = this._createTexture('MorphTarget', this._textureFormat);
const packedDeltas = texture.lock();

// copy full arrays into sparse arrays and convert format (skip 0th pixel - used by non-morphed vertices)
for (let v = 0; v < usedDataIndices.length; v++) {
const index = usedDataIndices[v];
Expand All @@ -197,9 +198,9 @@ class Morph extends RefCountedObject {
}

// create texture and assign it to target
texture.unlock();
const target = deltaInfos[i].target;
const format = this._textureFormat;
target._setTexture(deltaInfos[i].name, this._createTexture('MorphTarget', format, packedDeltas));
target._setTexture(deltaInfos[i].name, texture);
}

// create vertex stream with vertex_id used to map vertex to texture
Expand Down Expand Up @@ -266,9 +267,8 @@ class Morph extends RefCountedObject {
}

// creates texture. Used to create both source morph target data, as well as render target used to morph these into, positions and normals
_createTexture(name, format, pixelData) {

const texture = new Texture(this.device, {
_createTexture(name, format) {
return new Texture(this.device, {
width: this.morphTextureWidth,
height: this.morphTextureHeight,
format: format,
Expand All @@ -280,13 +280,6 @@ class Morph extends RefCountedObject {
addressV: ADDRESS_CLAMP_TO_EDGE,
name: name
});

if (pixelData) {
texture.lock().set(pixelData);
texture.unlock();
}

return texture;
}
}

Expand Down

0 comments on commit 1aed0a0

Please sign in to comment.