Skip to content

Commit

Permalink
Merge pull request #4814 from AnalyticalGraphicsInc/crunch
Browse files Browse the repository at this point in the history
Add crunch compressed texture support
  • Loading branch information
pjcozzi authored Jan 9, 2017
2 parents c67a2ca + 73fc589 commit 304d32a
Show file tree
Hide file tree
Showing 27 changed files with 1,471 additions and 75 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Change Log
### TODO

* Added compressed texture support.
* glTF models and imagery layers can now reference [KTX](https://www.khronos.org/opengles/sdk/tools/KTX/) textures.
* glTF models and imagery layers can now reference [KTX](https://www.khronos.org/opengles/sdk/tools/KTX/) textures and textures compressed with [crunch](https://github.com/BinomialLLC/crunch).
* Added `loadKTX` to load KTX textures.
* Added `loadCRN` to load crunch compressed textures.
* Added new `PixelFormat` and `WebGLConstants` enums from WebGL extensions `WEBGL_compressed_s3tc`, `WEBGL_compressed_texture_pvrtc`, and `WEBGL_compressed_texture_etc1`. [#4758](https://github.com/AnalyticalGraphicsInc/cesium/pull/4758)
* Added `CompressedTextureBuffer`.

### 1.30 - 2017-02-01

Expand Down
79 changes: 79 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,85 @@ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

### crunch

https://github.com/BinomialLLC/crunch

>crunch/crnlib uses the ZLIB license:
>http://opensource.org/licenses/Zlib
>
>Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC
>
>This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
>
>Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
>
>1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
>
>2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
>
>3. This notice may not be removed or altered from any source distribution.
### crunch_lib.cpp

https://github.com/Apress/html5-game-dev-insights/blob/master/jones_ch21/crunch_webgl/crunch_js/crunch_lib.cpp

>Copyright (c) 2013, Evan Parker, Brandon Jones. All rights reserved.
>
>Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
>
> * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
> * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
>
>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

### texture-tester

https://github.com/toji/texture-tester

>Copyright (c) 2014, Brandon Jones. All rights reserved.
>
>Redistribution and use in source and binary forms, with or without modification,
>are permitted provided that the following conditions are met:
>
>* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
>* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
>
>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Tests
=====

Expand Down
93 changes: 93 additions & 0 deletions Source/Core/CompressedTextureBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*global define*/
define([
'./defined',
'./defineProperties'
], function(
defined,
defineProperties
) {
'use strict';

/**
* Describes a compressed texture and contains a compressed texture buffer.
*
* @param {PixelFormat} internalFormat The pixel format of the compressed texture.
* @param {Number} width The width of the texture.
* @param {Number} height The height of the texture.
* @param {Uint8Array} buffer The compressed texture buffer.
*/
function CompressedTextureBuffer(internalFormat, width, height, buffer) {
this._format = internalFormat;
this._width = width;
this._height = height;
this._buffer = buffer;
}

defineProperties(CompressedTextureBuffer.prototype, {
/**
* The format of the compressed texture.
* @type PixelFormat
* @readonly
*/
internalFormat : {
get : function() {
return this._format;
}
},
/**
* The width of the texture.
* @type Number
* @readonly
*/
width : {
get : function() {
return this._width;
}
},
/**
* The height of the texture.
* @type Number
* @readonly
*/
height : {
get : function() {
return this._height;
}
},
/**
* The compressed texture buffer.
* @type Uint8Array
* @readonly
*/
bufferView : {
get : function() {
return this._buffer;
}
}
});

/**
* Creates a shallow clone of a compressed texture buffer.
*
* @param {CompressedTextureBuffer} object The compressed texture buffer to be cloned.
* @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer.
*/
CompressedTextureBuffer.clone = function(object) {
if (!defined(object)) {
return undefined;
}

return new CompressedTextureBuffer(object._format, object._width, object._height, object._buffer);
};

/**
* Creates a shallow clone of this compressed texture buffer.
*
* @return {CompressedTextureBuffer} A shallow clone of the compressed texture buffer.
*/
CompressedTextureBuffer.prototype.clone = function() {
return CompressedTextureBuffer.clone(this);
};

return CompressedTextureBuffer;
});
84 changes: 84 additions & 0 deletions Source/Core/loadCRN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*global define*/
define([
'./CompressedTextureBuffer',
'./defined',
'./DeveloperError',
'./loadArrayBuffer',
'./TaskProcessor',
'../ThirdParty/when'
], function(
CompressedTextureBuffer,
defined,
DeveloperError,
loadArrayBuffer,
TaskProcessor,
when) {
'use strict';

var transcodeTaskProcessor = new TaskProcessor('transcodeCRNToDXT', Number.POSITIVE_INFINITY);

/**
* Asynchronously loads and parses the given URL to a CRN file or parses the raw binary data of a CRN file.
* Returns a promise that will resolve to an object containing the image buffer, width, height and format once loaded,
* or reject if the URL failed to load or failed to parse the data. The data is loaded
* using XMLHttpRequest, which means that in order to make requests to another origin,
* the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
*
* @exports loadCRN
*
* @param {String|Promise.<String>|ArrayBuffer} urlOrBuffer The URL of the binary data, a promise for the URL, or an ArrayBuffer.
* @param {Object} [headers] HTTP headers to send with the requests.
* @returns {Promise.<CompressedTextureBuffer>} A promise that will resolve to the requested data when loaded.
*
* @exception {RuntimeError} Unsupported compressed format.
*
* @example
* // load a single URL asynchronously
* Cesium.loadCRN('some/url').then(function(textureData) {
* var width = textureData.width;
* var height = textureData.height;
* var format = textureData.internalFormat;
* var arrayBufferView = textureData.bufferView;
* // use the data to create a texture
* }).otherwise(function(error) {
* // an error occurred
* });
*
* @see {@link https://github.com/BinomialLLC/crunch|crunch DXTc texture compression and transcoding library}
* @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
* @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
*/
function loadCRN(urlOrBuffer, headers) {
//>>includeStart('debug', pragmas.debug);
if (!defined(urlOrBuffer)) {
throw new DeveloperError('urlOrBuffer is required.');
}
//>>includeEnd('debug');

var loadPromise;
if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
loadPromise = when.resolve(urlOrBuffer);
} else {
loadPromise = loadArrayBuffer(urlOrBuffer, headers);
}

return loadPromise.then(function(data) {
var transferrableObjects = [];
if (data instanceof ArrayBuffer) {
transferrableObjects.push(data);
} else if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
transferrableObjects.push(data.buffer);
} else {
// data is a view of an array buffer. need to copy so it is transferrable to web worker
data = data.slice(0, data.length);
transferrableObjects.push(data.buffer);
}

return transcodeTaskProcessor.scheduleTask(data, transferrableObjects);
}).then(function(compressedTextureBuffer) {
return CompressedTextureBuffer.clone(compressedTextureBuffer);
});
}

return loadCRN;
});
62 changes: 2 additions & 60 deletions Source/Core/loadKTX.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*global define*/
define([
'../ThirdParty/when',
'./CompressedTextureBuffer',
'./defined',
'./defineProperties',
'./DeveloperError',
'./loadArrayBuffer',
'./PixelFormat',
'./RuntimeError'
], function(
when,
CompressedTextureBuffer,
defined,
defineProperties,
DeveloperError,
loadArrayBuffer,
PixelFormat,
Expand Down Expand Up @@ -88,64 +88,6 @@ define([
});
}

/**
* Describes a compressed texture and contains a compressed texture buffer.
*
* @param {PixelFormat} internalFormat The pixel format of the compressed texture.
* @param {Number} width The width of the texture.
* @param {Number} height The height of the texture.
* @param {Uint8Array} buffer The compressed texture buffer.
*/
function CompressedTextureBuffer(internalFormat, width, height, buffer) {
this._format = internalFormat;
this._width = width;
this._height = height;
this._buffer = buffer;
}

defineProperties(CompressedTextureBuffer.prototype, {
/**
* The format of the compressed texture.
* @type PixelFormat
* @readonly
*/
internalFormat : {
get : function() {
return this._format;
}
},
/**
* The width of the texture.
* @type Number
* @readonly
*/
width : {
get : function() {
return this._width;
}
},
/**
* The height of the texture.
* @type Number
* @readonly
*/
height : {
get : function() {
return this._height;
}
},
/**
* The compressed texture buffer.
* @type Uint8Array
* @readonly
*/
bufferView : {
get : function() {
return this._buffer;
}
}
});

var fileIdentifier = [0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A];
var endiannessTest = 0x04030201;

Expand Down
7 changes: 6 additions & 1 deletion Source/Scene/ImageryProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define([
'../Core/defined',
'../Core/defineProperties',
'../Core/DeveloperError',
'../Core/loadCRN',
'../Core/loadImage',
'../Core/loadImageViaBlob',
'../Core/loadKTX',
Expand All @@ -11,6 +12,7 @@ define([
defined,
defineProperties,
DeveloperError,
loadCRN,
loadImage,
loadImageViaBlob,
loadKTX,
Expand Down Expand Up @@ -296,7 +298,8 @@ define([
*/
ImageryProvider.prototype.pickFeatures = DeveloperError.throwInstantiationError;

var ktxRegex = /(^data:image\/ktx)|(\.ktx$)/i;
var ktxRegex = /\.ktx$/i;
var crnRegex = /\.crn$/i;

/**
* Loads an image from a given URL. If the server referenced by the URL already has
Expand All @@ -312,6 +315,8 @@ define([
ImageryProvider.loadImage = function(imageryProvider, url) {
if (ktxRegex.test(url)) {
return throttleRequestByServer(url, loadKTX);
} else if (crnRegex.test(url)) {
return throttleRequestByServer(url, loadCRN);
} else if (defined(imageryProvider.tileDiscardPolicy)) {
return throttleRequestByServer(url, loadImageViaBlob);
}
Expand Down
Loading

0 comments on commit 304d32a

Please sign in to comment.