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

Memory usage stats for Models #5049

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions Source/Core/PixelFormat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*global define*/
define([
'../Renderer/PixelDatatype',
'./freezeObject',
'./WebGLConstants'
], function(
PixelDatatype,
freezeObject,
WebGLConstants) {
'use strict';
Expand Down Expand Up @@ -141,6 +143,28 @@ define([
*/
RGB_ETC1 : WebGLConstants.COMPRESSED_RGB_ETC1_WEBGL,

/**
* @private
*/
componentLength : function(pixelFormat) {
switch (pixelFormat) {
case PixelFormat.RGB:
// Many GPUs store RGB as RGBA internally
// https://devtalk.nvidia.com/default/topic/699479/general-graphics-programming/rgb-auto-converted-to-rgba/post/4142379/#4142379
return 4;
case PixelFormat.RGBA:
return 4;
case PixelFormat.ALPHA:
return 1;
case PixelFormat.LUMINANCE:
return 1;
case PixelFormat.LUMINANCE_ALPHA:
return 2;
default:
return 1;
}
},

/**
* @private
*/
Expand Down Expand Up @@ -249,6 +273,17 @@ define([
default:
return 0;
}
},

/**
* @private
*/
textureSize : function(pixelFormat, pixelDatatype, width, height) {
var componentLength = PixelFormat.componentLength(pixelFormat);
if (PixelDatatype.isPackedDatatype(pixelDatatype)) {
componentLength = 1;
}
return componentLength * PixelDatatype.sizeInBytes(pixelDatatype) * width * height;
}
};

Expand Down
13 changes: 12 additions & 1 deletion Source/Renderer/CubeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ define([
}
//>>includeEnd('debug');

var sizeInBytes = PixelFormat.textureSize(pixelFormat, pixelDatatype, size, size) * 6;

// Use premultiplied alpha for opaque textures should perform better on Chrome:
// http://media.tojicode.com/webglCamp4/#20
var preMultiplyAlpha = options.preMultiplyAlpha || ((pixelFormat === PixelFormat.RGB) || (pixelFormat === PixelFormat.LUMINANCE));
Expand Down Expand Up @@ -156,6 +158,7 @@ define([
this._pixelFormat = pixelFormat;
this._pixelDatatype = pixelDatatype;
this._size = size;
this._sizeInBytes = sizeInBytes;
this._preMultiplyAlpha = preMultiplyAlpha;
this._flipY = flipY;
this._sampler = undefined;
Expand Down Expand Up @@ -253,11 +256,16 @@ define([
return this._size;
}
},
height: {
height : {
get : function() {
return this._size;
}
},
sizeInBytes : {
get : function() {
return this._sizeInBytes;
}
},
preMultiplyAlpha : {
get : function() {
return this._preMultiplyAlpha;
Expand Down Expand Up @@ -313,6 +321,9 @@ define([
gl.bindTexture(target, this._texture);
gl.generateMipmap(target);
gl.bindTexture(target, null);

// The mipmap adds approximately 1/3 of the original texture size
this._sizeInBytes = Math.floor(this._sizeInBytes * 4 / 3);
};

CubeMap.prototype.isDestroyed = function() {
Expand Down
23 changes: 23 additions & 0 deletions Source/Renderer/PixelDatatype.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ define([
UNSIGNED_SHORT_5_5_5_1 : WebGLConstants.UNSIGNED_SHORT_5_5_5_1,
UNSIGNED_SHORT_5_6_5 : WebGLConstants.UNSIGNED_SHORT_5_6_5,

isPackedDatatype : function(pixelDatatype) {
return pixelDatatype === PixelDatatype.UNSIGNED_INT_24_8 ||
pixelDatatype === PixelDatatype.UNSIGNED_SHORT_4_4_4_4 ||
pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_5_5_1 ||
pixelDatatype === PixelDatatype.UNSIGNED_SHORT_5_6_5;
},

sizeInBytes : function(pixelDatatype) {
switch (pixelDatatype) {
case PixelDatatype.UNSIGNED_BYTE:
return 1;
case PixelDatatype.UNSIGNED_SHORT:
case PixelDatatype.UNSIGNED_SHORT_4_4_4_4:
case PixelDatatype.UNSIGNED_SHORT_5_5_5_1:
case PixelDatatype.UNSIGNED_SHORT_5_6_5:
return 2;
case PixelDatatype.UNSIGNED_INT:
case PixelDatatype.FLOAT:
case PixelDatatype.UNSIGNED_INT_24_8:
return 4;
}
},

validate : function(pixelDatatype) {
return ((pixelDatatype === PixelDatatype.UNSIGNED_BYTE) ||
(pixelDatatype === PixelDatatype.UNSIGNED_SHORT) ||
Expand Down
17 changes: 17 additions & 0 deletions Source/Renderer/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ define([
}
gl.bindTexture(textureTarget, null);

var sizeInBytes;
if (isCompressed) {
sizeInBytes = PixelFormat.compressedTextureSize(pixelFormat, width, height);
} else {
sizeInBytes = PixelFormat.textureSize(pixelFormat, pixelDatatype, width, height);
}

this._context = context;
this._textureFilterAnisotropic = context._textureFilterAnisotropic;
this._textureTarget = textureTarget;
Expand All @@ -198,6 +205,7 @@ define([
this._width = width;
this._height = height;
this._dimensions = new Cartesian2(width, height);
this._sizeInBytes = sizeInBytes;
this._preMultiplyAlpha = preMultiplyAlpha;
this._flipY = flipY;
this._sampler = undefined;
Expand Down Expand Up @@ -380,6 +388,11 @@ define([
return this._height;
}
},
sizeInBytes : {
get : function() {
return this._sizeInBytes;
}
},
_target : {
get : function() {
return this._textureTarget;
Expand Down Expand Up @@ -527,6 +540,7 @@ define([
* @param {MipmapHint} [hint=MipmapHint.DONT_CARE] optional.
*
* @exception {DeveloperError} Cannot call generateMipmap when the texture pixel format is DEPTH_COMPONENT or DEPTH_STENCIL.
* @exception {DeveloperError} Cannot call generateMipmap when the texture pixel format is a compressed format.
* @exception {DeveloperError} hint is invalid.
* @exception {DeveloperError} This texture's width must be a power of two to call generateMipmap().
* @exception {DeveloperError} This texture's height must be a power of two to call generateMipmap().
Expand Down Expand Up @@ -561,6 +575,9 @@ define([
gl.bindTexture(target, this._texture);
gl.generateMipmap(target);
gl.bindTexture(target, null);

// The mipmap adds approximately 1/3 of the original texture size
this._sizeInBytes = Math.floor(this._sizeInBytes * 4 / 3);
};

Texture.prototype.isDestroyed = function() {
Expand Down
36 changes: 36 additions & 0 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,42 @@ define([
get : function() {
return this._upAxis;
}
},

/**
* Gets the model's vertex memory in bytes. This includes all vertex and index buffers.
*
* @private
*/
vertexMemoryInBytes : {
get : function() {
var memory = 0;
var buffers = this._rendererResources.buffers;
for (var id in buffers) {
if (buffers.hasOwnProperty(id)) {
memory += buffers[id].sizeInBytes;
}
}
return memory;
}
},

/**
* Gets the model's texture memory in bytes.
*
* @private
*/
textureMemoryInBytes : {
get : function() {
var memory = 0;
var textures = this._rendererResources.textures;
for (var id in textures) {
if (textures.hasOwnProperty(id)) {
memory += textures[id].sizeInBytes;
}
}
return memory;
}
}
});

Expand Down
32 changes: 32 additions & 0 deletions Specs/Renderer/CubeMapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ defineSuite([
var blueImage;
var blueAlphaImage;
var blueOverRedImage;
var red16x16Image;

beforeAll(function() {
context = createContext();
Expand All @@ -109,6 +110,9 @@ defineSuite([
promises.push(loadImage('./Data/Images/BlueOverRed.png').then(function(result) {
blueOverRedImage = result;
}));
promises.push(loadImage('./Data/Images/Red16x16.png').then(function(result) {
red16x16Image = result;
}));

return when.all(promises);
});
Expand Down Expand Up @@ -186,6 +190,16 @@ defineSuite([
expect(cubeMap.height).toEqual(16);
});

it('gets size in bytes', function() {
cubeMap = new CubeMap({
context : context,
width : 16,
height : 16
});

expect(cubeMap.sizeInBytes).toEqual(256 * 4 * 6);
});

it('gets flip Y', function() {
cubeMap = new CubeMap({
context : context,
Expand Down Expand Up @@ -625,6 +639,24 @@ defineSuite([
}).contextToRender([0, 0, 255, 255]);
});

it('gets size in bytes for mipmap', function() {
cubeMap = new CubeMap({
context : context,
source : {
positiveX : red16x16Image,
negativeX : red16x16Image,
positiveY : red16x16Image,
negativeY : red16x16Image,
positiveZ : red16x16Image,
negativeZ : red16x16Image
}
});
cubeMap.generateMipmap();

// Allow for some leniency with the sizeInBytes approximation
expect(cubeMap.sizeInBytes).toEqualEpsilon((16*16 + 8*8 + 4*4 + 2*2 + 1) * 4 * 6, 10);
});

it('destroys', function() {
var c = new CubeMap({
context : context,
Expand Down
Loading