Skip to content

Commit dd2dab3

Browse files
committed
Add immediate upload option to textures
1 parent 1806a15 commit dd2dab3

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

examples/src/examples/graphics/texture-array.example.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ assetListLoader.load(() => {
147147
assets.aerialRocks.resource.getSource(),
148148
assets.coastSand.resource.getSource()
149149
]
150-
]
150+
],
151+
immediate: true
151152
};
152153

153154
const textureArray = new pc.Texture(app.graphicsDevice, textureArrayOptions);
154-
textureArray.upload();
155155

156156
// generate mipmaps for visualization
157157
const mipmaps = generateMipmaps(textureArrayOptions.width, textureArrayOptions.height);

src/platform/graphics/texture.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class Texture {
185185
* of Uint8Array if options.arrayLength is defined and greater than zero.
186186
* @param {boolean} [options.storage] - Defines if texture can be used as a storage texture by
187187
* a compute shader. Defaults to false.
188+
* @param {boolean} [options.immediate] - If set and true, the texture will be uploaded to the GPU immediately.
188189
* @example
189190
* // Create a 8x8x24-bit texture
190191
* const texture = new pc.Texture(graphicsDevice, {
@@ -267,7 +268,7 @@ class Texture {
267268

268269
this._levels = options.levels;
269270
if (this._levels) {
270-
this.upload();
271+
this.upload(options.immediate ?? false);
271272
} else {
272273
this._levels = this._cubemap ? [[null, null, null, null, null, null]] : [null];
273274
}
@@ -878,8 +879,9 @@ class Texture {
878879
* @param {number} [mipLevel] - A non-negative integer specifying the image level of detail.
879880
* Defaults to 0, which represents the base image source. A level value of N, that is greater
880881
* than 0, represents the image source for the Nth mipmap reduction level.
882+
* @param {boolean} [immediate] - When set to true it forces an immediate upload upon assignment. Defaults to false.
881883
*/
882-
setSource(source, mipLevel = 0) {
884+
setSource(source, mipLevel = 0, immediate = false) {
883885
let invalid = false;
884886
let width, height;
885887

@@ -959,7 +961,7 @@ class Texture {
959961
this._invalid = invalid;
960962

961963
// reupload
962-
this.upload();
964+
this.upload(immediate);
963965
}
964966
}
965967

@@ -979,31 +981,33 @@ class Texture {
979981

980982
/**
981983
* Unlocks the currently locked mip level and uploads it to VRAM.
984+
* @param {boolean} [immediate] - When set to true it forces an immediate upload upon unlocking. Defaults to false.
982985
*/
983-
unlock() {
986+
unlock(immediate = false) {
984987
if (this._lockedMode === TEXTURELOCK_NONE) {
985988
Debug.warn("pc.Texture#unlock: Attempting to unlock a texture that is not locked.", this);
986989
}
987990

988991
// Upload the new pixel data if locked in write mode (default)
989992
if (this._lockedMode === TEXTURELOCK_WRITE) {
990-
this.upload();
993+
this.upload(immediate);
991994
}
992995
this._lockedLevel = -1;
993996
this._lockedMode = TEXTURELOCK_NONE;
994997
}
995998

996999
/**
9971000
* Forces a reupload of the textures pixel data to graphics memory. Ordinarily, this function
998-
* is called by internally by {@link Texture#setSource} and {@link Texture#unlock}. However, it
1001+
* is called internally by {@link Texture#setSource} and {@link Texture#unlock}. However, it
9991002
* still needs to be called explicitly in the case where an HTMLVideoElement is set as the
10001003
* source of the texture. Normally, this is done once every frame before video textured
10011004
* geometry is rendered.
1005+
* @param {boolean} [immediate] - When set to true it forces an immediate upload. Defaults to false.
10021006
*/
1003-
upload() {
1007+
upload(immediate = false) {
10041008
this._needsUpload = true;
10051009
this._needsMipmapsUpload = this._mipmaps;
1006-
this.impl.uploadImmediate?.(this.device, this);
1010+
this.impl.uploadImmediate(this.device, this, immediate);
10071011
}
10081012

10091013
/**

src/platform/graphics/webgl/webgl-texture.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,19 @@ class WebglTexture {
376376
this._glCreated = false;
377377
}
378378

379+
uploadImmediate(device, texture, immediate) {
380+
if (immediate) {
381+
if (!texture._glTexture) {
382+
this.initialize(device, texture);
383+
}
384+
385+
device.bindTexture(texture);
386+
this.upload(device, texture);
387+
texture._needsUpload = false;
388+
texture._needsMipmapsUpload = false;
389+
}
390+
}
391+
379392
/**
380393
* @param {import('./webgl-graphics-device.js').WebglGraphicsDevice} device - The device.
381394
* @param {import('../texture.js').Texture} texture - The texture to update.

0 commit comments

Comments
 (0)