-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Add crunch compressed texture support #4814
Changes from all commits
cb01073
bf97394
68082c2
5875390
ee6856c
252bb8b
f6cf955
73fc589
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally add a member |
||
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; | ||
}); |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about We have some bigger work to do on parallelism, but, in the meantime, what is a reasonable default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only option because anytime we load an image we expect a promise that will resolve to an image. Otherwise, we have to change that behavior so when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, actually the request scheduler implicitly helps here. We'll flush this out later. |
||
|
||
/** | ||
* 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a lot of duplication among |
||
//>>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; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to CHANGES.md.