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

Remove Material texture cache #2850

Merged
merged 2 commits into from
Jun 30, 2015
Merged
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 CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Change Log
* Fixed camera flights that wouldn't fly to the home view after zooming out past it. [#1400](https://github.com/AnalyticalGraphicsInc/cesium/issues/1400)
* Fixed flying to rectangles that cross the IDL in Columbus view and 2D. [#2093](https://github.com/AnalyticalGraphicsInc/cesium/issues/2093)
* Fixed flights with a pitch of -90 degrees. [#2468](https://github.com/AnalyticalGraphicsInc/cesium/issues/2468)
* Fixed several issues with material caching which resulted in the inability to use an image-based material multiple times. [#2821](https://github.com/AnalyticalGraphicsInc/cesium/issues/2821)

### 1.10 - 2015-06-01

Expand Down
118 changes: 42 additions & 76 deletions Source/Scene/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ define([

this._updateFunctions = [];

this._defaultTexture = undefined;

initializeMaterial(options, this);
defineProperties(this, {
type : {
Expand Down Expand Up @@ -411,13 +413,9 @@ define([
uniformId = loadedImage.id;
var image = loadedImage.image;

var texture = Material._textureCache.getTexture(this._texturePaths[uniformId]);
if (!defined(texture)) {
texture = context.createTexture2D({
source : image
});
Material._textureCache.addTexture(this._texturePaths[uniformId], texture);
}
var texture = context.createTexture2D({
source : image
});

this._textures[uniformId] = texture;

Expand All @@ -439,9 +437,7 @@ define([
uniformId = loadedCubeMap.id;
var images = loadedCubeMap.images;

var cubeMap = Material._textureCache.getTexture(this._texturePaths[uniformId]);
if (!defined(cubeMap)) {
cubeMap = context.createCubeMap({
var cubeMap = context.createCubeMap({
source : {
positiveX : images[0],
negativeX : images[1],
Expand All @@ -451,8 +447,6 @@ define([
negativeZ : images[5]
}
});
Material._textureCache.addTexture(this._texturePaths[uniformId], cubeMap);
}

this._textures[uniformId] = cubeMap;
}
Expand Down Expand Up @@ -505,14 +499,17 @@ define([
* material = material && material.destroy();
*/
Material.prototype.destroy = function() {
var materials = this.materials;
var uniforms = this.uniforms;
for ( var uniformId in uniforms) {
if (uniforms.hasOwnProperty(uniformId)) {
var path = this._texturePaths[uniformId];
Material._textureCache.releaseTexture(path);
var textures = this._textures;
for ( var texture in textures) {
if (textures.hasOwnProperty(texture)) {
var instance = textures[texture];
if (instance !== this._defaultTexture) {
instance.destroy();
}
}
}

var materials = this.materials;
for ( var material in materials) {
if (materials.hasOwnProperty(material)) {
materials[material].destroy();
Expand Down Expand Up @@ -667,8 +664,11 @@ define([
var uniformDimensions;

if (uniformValue instanceof Texture && uniformValue !== texture) {
Material._textureCache.releaseTexture(material._texturePaths[uniformId]);
material._texturePaths[uniformId] = undefined;
var tmp = material._textures[uniformId];
if (tmp !== material._defaultTexture) {
tmp.destroy();
}
material._textures[uniformId] = uniformValue;

uniformDimensionsName = uniformId + 'Dimensions';
Expand All @@ -683,7 +683,10 @@ define([

if (!defined(texture)) {
material._texturePaths[uniformId] = undefined;
texture = material._textures[uniformId] = context.defaultTexture;
if (!defined(material._defaultTexture)) {
material._defaultTexture = context.defaultTexture;
}
texture = material._textures[uniformId] = material._defaultTexture;

uniformDimensionsName = uniformId + 'Dimensions';
if (uniforms.hasOwnProperty(uniformDimensionsName)) {
Expand All @@ -698,11 +701,7 @@ define([
}

if (uniformValue !== material._texturePaths[uniformId]) {
var newTexture = Material._textureCache.getTexture(uniformValue);
if (defined(newTexture)) {
Material._textureCache.releaseTexture(material._texturePaths[uniformId]);
material._textures[uniformId] = newTexture;
} else if (typeof uniformValue === 'string') {
if (typeof uniformValue === 'string') {
when(loadImage(uniformValue), function(image) {
material._loadedImages.push({
id : uniformId,
Expand All @@ -726,7 +725,10 @@ define([
var uniformValue = material.uniforms[uniformId];

if (uniformValue instanceof CubeMap) {
Material._textureCache.releaseTexture(material._texturePaths[uniformId]);
var tmp = material._textures[uniformId];
if (tmp !== material._defaultTexture) {
tmp.destroy();
}
material._texturePaths[uniformId] = undefined;
material._textures[uniformId] = uniformValue;
return;
Expand All @@ -747,27 +749,21 @@ define([
uniformValue.positiveZ + uniformValue.negativeZ;

if (path !== material._texturePaths[uniformId]) {
var newTexture = Material._textureCache.getTexture(path);
if (defined(newTexture)) {
Material._textureCache.releaseTexture(material._texturePaths[uniformId]);
material._textures[uniformId] = newTexture;
} else {
var promises = [
loadImage(uniformValue.positiveX),
loadImage(uniformValue.negativeX),
loadImage(uniformValue.positiveY),
loadImage(uniformValue.negativeY),
loadImage(uniformValue.positiveZ),
loadImage(uniformValue.negativeZ)
];

when.all(promises).then(function(images) {
material._loadedCubeMaps.push({
id : uniformId,
images : images
});
var promises = [
loadImage(uniformValue.positiveX),
loadImage(uniformValue.negativeX),
loadImage(uniformValue.positiveY),
loadImage(uniformValue.negativeY),
loadImage(uniformValue.positiveZ),
loadImage(uniformValue.negativeZ)
];

when.all(promises).then(function(images) {
material._loadedCubeMaps.push({
id : uniformId,
images : images
});
}
});

material._texturePaths[uniformId] = path;
}
Expand Down Expand Up @@ -945,36 +941,6 @@ define([
return replaceToken(material, token, token, excludePeriod);
}

Material._textureCache = {
_textures : {},

addTexture : function(path, texture) {
this._textures[path] = {
texture : texture,
count : 1
};
},

getTexture : function(path) {
var entry = this._textures[path];

if (defined(entry)) {
entry.count++;
return entry.texture;
}

return undefined;
},

releaseTexture : function(path) {
var entry = this._textures[path];
if (defined(entry) && --entry.count === 0) {
entry.texture = entry.texture && entry.texture.destroy();
this._textures[path] = undefined;
}
}
};

Material._materialCache = {
_materials : {},
addMaterial : function(type, materialTemplate) {
Expand Down
36 changes: 27 additions & 9 deletions Specs/Scene/MaterialSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defineSuite([
'Specs/createCamera',
'Specs/createContext',
'Specs/createFrameState',
'Specs/pollToPromise',
'Specs/render'
], function(
Material,
Expand All @@ -24,6 +25,7 @@ defineSuite([
createCamera,
createContext,
createFrameState,
pollToPromise,
render) {
"use strict";
/*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn*/
Expand Down Expand Up @@ -741,10 +743,15 @@ defineSuite([
it('destroys material with texture', function() {
var material = Material.fromType(Material.DiffuseMapType);
material.uniforms.image = './Data/Images/Green.png';
var pixel = renderMaterial(material);
expect(pixel).not.toEqual([0, 0, 0, 0]);
material.destroy();
expect(material.isDestroyed()).toEqual(true);

pollToPromise(function() {
return material._loadedImages.length !== 0;
}).then(function() {
var pixel = renderMaterial(material);
expect(pixel).not.toEqual([0, 0, 0, 0]);
material.destroy();
expect(material.isDestroyed()).toEqual(true);
});
});

it('destroys sub-materials', function() {
Expand All @@ -770,12 +777,23 @@ defineSuite([
});
material.materials.diffuseMap.uniforms.image = './Data/Images/Green.png';

var pixel = renderMaterial(material);
expect(pixel).not.toEqual([0, 0, 0, 0]);
pollToPromise(function() {
return material.materials.diffuseMap._loadedImages.length !== 0;
}).then(function() {
var pixel = renderMaterial(material);
expect(pixel).not.toEqual([0, 0, 0, 0]);

var diffuseMap = material.materials.diffuseMap;
material.destroy();
expect(material.isDestroyed()).toEqual(true);
expect(diffuseMap.isDestroyed()).toEqual(true);
});
});

var diffuseMap = material.materials.diffuseMap;
it('does not destroy default material', function() {
var material = Material.fromType(Material.DiffuseMapType);
renderMaterial(material);
material.destroy();
expect(material.isDestroyed()).toEqual(true);
expect(diffuseMap.isDestroyed()).toEqual(true);
});

}, 'WebGL');
14 changes: 10 additions & 4 deletions Specs/Scene/ViewportQuadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defineSuite([
'Specs/createCamera',
'Specs/createContext',
'Specs/createFrameState',
'Specs/pollToPromise',
'Specs/render'
], function(
ViewportQuad,
Expand All @@ -20,6 +21,7 @@ defineSuite([
createCamera,
createContext,
createFrameState,
pollToPromise,
render) {
"use strict";
/*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn*/
Expand Down Expand Up @@ -114,11 +116,15 @@ defineSuite([
viewportQuad.material = Material.fromType(Material.ImageType);
viewportQuad.material.uniforms.image = texture;

ClearCommand.ALL.execute(context);
expect(context.readPixels()).toEqual([0, 0, 0, 0]);
pollToPromise(function() {
return viewportQuad.material._loadedImages.length !== 0;
}).then(function() {
ClearCommand.ALL.execute(context);
expect(context.readPixels()).toEqual([0, 0, 0, 0]);

render(context, frameState, viewportQuad);
expect(context.readPixels()).toEqual([255, 0, 0, 255]);
render(context, frameState, viewportQuad);
expect(context.readPixels()).toEqual([255, 0, 0, 255]);
});
});

it('isDestroyed', function() {
Expand Down