This commit is contained in:
2026-06-14 19:09:18 +01:00
parent 14bd1a9271
commit 13fa90a0e9
3958 changed files with 999286 additions and 4 deletions
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
+242
View File
@@ -0,0 +1,242 @@
// basis_wrappers.cpp - Simple C-style wrappers to the C++ transcoder for WebGL use.
#include "basisu_transcoder.h"
using namespace basist;
typedef unsigned int uint;
extern "C"
{
void basis_init();
void *basis_open(void *src, uint src_size);
void basis_close(void *h);
uint basis_get_has_alpha(void *h);
uint basis_get_num_images(void *h);
uint basis_get_num_levels(void *h, uint image_index);
uint basis_get_image_width(void *h, uint image_index, uint level_index);
uint basis_get_image_height(void *h, uint image_index, uint level_index);
uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint level_index, uint format);
uint basis_start_transcoding(void *h);
uint basis_stop_transcoding(void *h);
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
uint image_index, uint level_index, uint format,
uint unused, uint get_alpha_for_opaque_formats);
void basis_set_debug_flags(uint f) { basist::set_debug_flags(f); }
uint basis_get_debug_flags() { return basist::get_debug_flags(); }
}
void basis_init()
{
basisu_transcoder_init();
}
#define MAGIC 0xDEADBEE1
struct basis_file
{
int m_magic;
basisu_transcoder m_transcoder;
void *m_pFile;
uint m_file_size;
basis_file() :
m_transcoder()
{
}
};
void *basis_open(void *src, uint src_size)
{
if ((!src) || (!src_size))
return nullptr;
basis_file *f = new basis_file;
f->m_pFile = src;
f->m_file_size = src_size;
if (!f->m_transcoder.validate_header(f->m_pFile, f->m_file_size))
{
delete f;
return nullptr;
}
f->m_magic = MAGIC;
return f;
}
void basis_close(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return;
delete f;
}
uint basis_get_has_alpha(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
basisu_image_level_info li;
if (!f->m_transcoder.get_image_level_info(f->m_pFile, f->m_file_size, li, 0, 0))
return 0;
return li.m_alpha_flag;
}
uint basis_get_num_images(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
return f->m_transcoder.get_total_images(f->m_pFile, f->m_file_size);
}
uint basis_get_num_levels(void *h, uint image_index)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
basisu_image_info ii;
if (!f->m_transcoder.get_image_info(f->m_pFile, f->m_file_size, ii, image_index))
return 0;
return ii.m_total_levels;
}
uint basis_get_image_width(void *h, uint image_index, uint level_index)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
uint orig_width, orig_height, total_blocks;
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
return orig_width;
}
uint basis_get_image_height(void *h, uint image_index, uint level_index)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
uint orig_width, orig_height, total_blocks;
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
return orig_height;
}
uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint level_index, uint format)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
uint orig_width, orig_height, total_blocks;
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
uint bytes_per_block_or_pixel = basis_get_bytes_per_block_or_pixel((transcoder_texture_format)format);
if (basis_transcoder_format_is_uncompressed((transcoder_texture_format)format))
return orig_width * orig_height * bytes_per_block_or_pixel;
return total_blocks * bytes_per_block_or_pixel;
}
uint basis_start_transcoding(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
return f->m_transcoder.start_transcoding(f->m_pFile, f->m_file_size);
}
uint basis_stop_transcoding(void *h)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
return f->m_transcoder.stop_transcoding();
}
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
uint image_index, uint level_index, uint format,
uint unused, uint get_alpha_for_opaque_formats)
{
basis_file *f = static_cast<basis_file *>(h);
if (!f)
return 0;
assert(f->m_magic == MAGIC);
if (f->m_magic != MAGIC)
return 0;
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
uint bytes_per_block = basis_get_bytes_per_block_or_pixel((transcoder_texture_format)format);
return f->m_transcoder.transcode_image_level(f->m_pFile, f->m_file_size, image_index, level_index,
dst, dst_size_in_bytes / bytes_per_block,
(basist::transcoder_texture_format)format,
(get_alpha_for_opaque_formats ? (uint32_t)cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0));
}
+4
View File
@@ -0,0 +1,4 @@
@REM -O0 -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1
@REM -O2 -s ASSERTIONS=0
emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_stop_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -o basis.js -DBASISD_ENABLE_DEBUG_FLAGS=1 ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=749993984 -std=c++11 -O2 -s ASSERTIONS=0 -I ../transcoder -D BASISD_SUPPORT_KTX2=0
rem emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_stop_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -o basis.js -DBASISD_ENABLE_DEBUG_FLAGS=1 ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=499974144 -std=c++11 -O0 -s ASSERTIONS=0 -I ../transcoder -D BASISD_SUPPORT_KTX2=0
+3
View File
@@ -0,0 +1,3 @@
# rg - I haven't tested this shell script yet (I use build.bat on Windows)
emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding','_basis_get_debug_flags','_basis_set_debug_flags']" -s TOTAL_MEMORY=749993984 -O2 -s ASSERTIONS=0 -I ../transcoder -o basis.js ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -D BASISD_SUPPORT_KTX2=0 -D BASISD_ENABLE_DEBUG_FLAGS=1
# chmod -R a+rX .
+128
View File
@@ -0,0 +1,128 @@
/**
* Transcodes DXT into RGB565.
* This is an optimized version of dxtToRgb565Unoptimized() below.
* Optimizations:
* 1. Use integer math to compute c2 and c3 instead of floating point
* math. Specifically:
* c2 = 5/8 * c0 + 3/8 * c1
* c3 = 3/8 * c0 + 5/8 * c1
* This is about a 40% performance improvement. It also appears to
* match what hardware DXT decoders do, as the colors produced
* by this integer math match what hardware produces, while the
* floating point in dxtToRgb565Unoptimized() produce slightly
* different colors (for one GPU this was tested on).
* 2. Unroll the inner loop. Another ~10% improvement.
* 3. Compute r0, g0, b0, r1, g1, b1 only once instead of twice.
* Another 10% improvement.
* 4. Use a Uint16Array instead of a Uint8Array. Another 10% improvement.
* @param {Uint16Array} src The src DXT bits as a Uint16Array.
* @param {number} srcByteOffset
* @param {number} width
* @param {number} height
* @return {Uint16Array} dst
*/
function dxtToRgb565(src, src16Offset, width, height) {
var c = new Uint16Array(4);
var dst = new Uint16Array(width * height);
var nWords = (width * height) / 4;
var m = 0;
var dstI = 0;
var i = 0;
var r0 = 0, g0 = 0, b0 = 0, r1 = 0, g1 = 0, b1 = 0;
var blockWidth = width / 4;
var blockHeight = height / 4;
for (var blockY = 0; blockY < blockHeight; blockY++) {
for (var blockX = 0; blockX < blockWidth; blockX++) {
i = src16Offset + 4 * (blockY * blockWidth + blockX);
c[0] = src[i];
c[1] = src[i + 1];
r0 = c[0] & 0x1f;
g0 = c[0] & 0x7e0;
b0 = c[0] & 0xf800;
r1 = c[1] & 0x1f;
g1 = c[1] & 0x7e0;
b1 = c[1] & 0xf800;
// Interpolate between c0 and c1 to get c2 and c3.
// Note that we approximate 1/3 as 3/8 and 2/3 as 5/8 for
// speed. This also appears to be what the hardware DXT
// decoder in many GPUs does :)
// rg FIXME: This is most likely leading to wrong results vs. a GPU
c[2] = ((5 * r0 + 3 * r1) >> 3)
| (((5 * g0 + 3 * g1) >> 3) & 0x7e0)
| (((5 * b0 + 3 * b1) >> 3) & 0xf800);
c[3] = ((5 * r1 + 3 * r0) >> 3)
| (((5 * g1 + 3 * g0) >> 3) & 0x7e0)
| (((5 * b1 + 3 * b0) >> 3) & 0xf800);
m = src[i + 2];
dstI = (blockY * 4) * width + blockX * 4;
dst[dstI] = c[m & 0x3];
dst[dstI + 1] = c[(m >> 2) & 0x3];
dst[dstI + 2] = c[(m >> 4) & 0x3];
dst[dstI + 3] = c[(m >> 6) & 0x3];
dstI += width;
dst[dstI] = c[(m >> 8) & 0x3];
dst[dstI + 1] = c[(m >> 10) & 0x3];
dst[dstI + 2] = c[(m >> 12) & 0x3];
dst[dstI + 3] = c[(m >> 14)];
m = src[i + 3];
dstI += width;
dst[dstI] = c[m & 0x3];
dst[dstI + 1] = c[(m >> 2) & 0x3];
dst[dstI + 2] = c[(m >> 4) & 0x3];
dst[dstI + 3] = c[(m >> 6) & 0x3];
dstI += width;
dst[dstI] = c[(m >> 8) & 0x3];
dst[dstI + 1] = c[(m >> 10) & 0x3];
dst[dstI + 2] = c[(m >> 12) & 0x3];
dst[dstI + 3] = c[(m >> 14)];
}
}
return dst;
}
/**
* An unoptimized version of dxtToRgb565. Also, the floating
* point math used to compute the colors actually results in
* slightly different colors compared to hardware DXT decoders.
* @param {Uint8Array} src
* @param {number} srcByteOffset
* @param {number} width
* @param {number} height
* @return {Uint16Array} dst
*/
function dxtToRgb565Unoptimized(src, srcByteOffset, width, height) {
var c = new Uint16Array(4);
var dst = new Uint16Array(width * height);
var nWords = (width * height) / 4;
var blockWidth = width / 4;
var blockHeight = height / 4;
for (var blockY = 0; blockY < blockHeight; blockY++) {
for (var blockX = 0; blockX < blockWidth; blockX++) {
var i = srcByteOffset + 8 * (blockY * blockWidth + blockX);
c[0] = src[i] | (src[i + 1] << 8);
c[1] = src[i + 2] | (src[i + 3] << 8);
c[2] = (2 * (c[0] & 0x1f) + 1 * (c[1] & 0x1f)) / 3
| (((2 * (c[0] & 0x7e0) + 1 * (c[1] & 0x7e0)) / 3) & 0x7e0)
| (((2 * (c[0] & 0xf800) + 1 * (c[1] & 0xf800)) / 3) & 0xf800);
c[3] = (2 * (c[1] & 0x1f) + 1 * (c[0] & 0x1f)) / 3
| (((2 * (c[1] & 0x7e0) + 1 * (c[0] & 0x7e0)) / 3) & 0x7e0)
| (((2 * (c[1] & 0xf800) + 1 * (c[0] & 0xf800)) / 3) & 0xf800);
for (var row = 0; row < 4; row++) {
var m = src[i + 4 + row];
var dstI = (blockY * 4 + row) * width + blockX * 4;
dst[dstI++] = c[m & 0x3];
dst[dstI++] = c[(m >> 2) & 0x3];
dst[dstI++] = c[(m >> 4) & 0x3];
dst[dstI++] = c[(m >> 6) & 0x3];
}
}
}
return dst;
}
+545
View File
@@ -0,0 +1,545 @@
<html>
<head>
<script src="basis.js"></script>
<script src="renderer.js"></script>
<script src="dxt-to-rgb565.js"></script>
<script type="text/javascript">
function log(s) {
var div = document.createElement('div');
div.innerHTML = s;
document.getElementById('logger').appendChild(div);
}
function logTime(desc, t) {
log(t + 'ms ' + desc);
}
function isDef(v) {
return typeof v != 'undefined';
}
function elem(id) {
return document.getElementById(id);
}
formatTable = function(rows) {
var colLengths = [];
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
for (var j = 0; j < row.length; j++) {
if (colLengths.length <= j) colLengths.push(0);
if (colLengths[j] < row[j].length) colLengths[j] = row[j].length;
}
}
function formatRow(row) {
var parts = [];
for (var i = 0; i < colLengths.length; i++) {
var s = row.length > i ? row[i] : '';
var padding = (new Array(1 + colLengths[i] - s.length)).join(' ');
if (s && s[0] >= '0' && s[0] <= '9') {
// Right-align numbers.
parts.push(padding + s);
} else {
parts.push(s + padding);
}
}
return parts.join(' | ');
}
var width = 0;
for (var i = 0; i < colLengths.length; i++) {
width += colLengths[i];
// Add another 3 for the separator.
if (i != 0) width += 3;
}
var lines = [];
lines.push(formatRow(rows[0]));
lines.push((new Array(width + 1)).join('-'));
for (var i = 1; i < rows.length; i++) {
lines.push(formatRow(rows[i]));
}
return lines.join('\n');
};
function loadArrayBuffer(uri, callback) {
log('Loading ' + uri + '...');
var xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.open('GET', uri, true);
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.response);
}
}
xhr.send(null);
}
// DXT formats, from:
// http://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc/
COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C;
BASIS_FORMAT = {
cTFETC1: 0,
cTFETC2: 1,
cTFBC1: 2,
cTFBC3: 3,
cTFBC4: 4,
cTFBC5: 5,
cTFBC7: 6,
cTFPVRTC1_4_RGB: 8,
cTFPVRTC1_4_RGBA: 9,
cTFASTC_4x4: 10,
cTFATC_RGB: 11,
cTFATC_RGBA_INTERPOLATED_ALPHA: 12,
cTFRGBA32: 13,
cTFRGB565: 14,
cTFBGR565: 15,
cTFRGBA4444: 16
};
BASIS_DEBUG_FLAGS = {
DEBUG_FLAG_VIS_CR: 1,
DEBUG_FLAG_VIS_SELS: 2,
DEBUG_FLAG_VIS_ENDPOINTS: 4
};
BASIS_FORMAT_NAMES = {};
for (var name in BASIS_FORMAT) {
BASIS_FORMAT_NAMES[BASIS_FORMAT[name]] = name;
}
DXT_FORMAT_MAP = {};
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC1] = COMPRESSED_RGB_S3TC_DXT1_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC3] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC7] = COMPRESSED_RGBA_BPTC_UNORM;
var dxtSupported = true;
var bc7Supported = false;
var drawMode = 0;
var basisHandle = 0, src = 0, srcSize = 0;
var tex = 0, width = 0, height = 0, images = 0, levels = 0, have_alpha = false, alignedWidth = 0, alignedHeight = 0, format;
var curImageIndex = 0;
function redraw()
{
if ((!width) || (!tex))
return;
if (dxtSupported)
{
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
}
else if (format == BASIS_FORMAT.cTFBC1)
{
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
}
else
{
log('TODO: Implement BC3->RGBA conversion for browsers that don\'t support BC3');
}
}
function transcodeFrame()
{
if (basisHandle == 0)
return;
var dstSize = Module._basis_get_image_transcoded_size_in_bytes(basisHandle, 0, 0, format);
var dst = Module._malloc(dstSize);
if (!Module._basis_transcode_image(basisHandle, dst, dstSize, curImageIndex, 0, format, 1, 0))
{
Module._basis_close(basisHandle); basisHandle = 0;
Module._free(src); src = 0;
Module._free(dst);
width = 0;
log('basis_transcode_image() failed!');
return;
}
var dxtData = new Uint8Array(Module.HEAPU8.buffer, dst, dstSize);
if (tex != 0)
{
renderer.destroyTexture(tex);
tex = 0;
}
if (dxtSupported)
{
tex = renderer.createDxtTexture(dxtData, alignedWidth, alignedHeight, DXT_FORMAT_MAP[format]);
}
else if (format == BASIS_FORMAT.cTFBC1)
{
var rgb565Data = dxtToRgb565(Module.HEAPU16, dst / 2, alignedWidth, alignedHeight);
tex = renderer.createRgb565Texture(rgb565Data, alignedWidth, alignedHeight);
}
Module._free(dst);
}
var then = 0;
var timeSinceLastFrame = 0;
var g_framerateLimit = true;
var g_paused = false;
var g_Framerate = 24.0;
var g_targetFrame = 0.0;
var g_dataLoadedCountDown;
var g_receivedData;
var forceBC1 = 0;
function frame(now)
{
if (g_dataLoadedCountDown > 0)
{
g_dataLoadedCountDown = g_dataLoadedCountDown - 1;
if (g_dataLoadedCountDown == 0)
{
if (g_receivedData)
{
transcode(g_receivedData);
g_receivedData = 0;
}
}
}
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
if (basisHandle != 0)
{
transcodeFrame();
redraw()
if (g_targetFrame >= 0)
{
timeSinceLastFrame = 0;
if (curImageIndex >= g_targetFrame)
g_targetFrame = -1;
else
curImageIndex++;
}
else if (g_paused)
{
timeSinceLastFrame = 0;
}
else if (!g_framerateLimit)
{
timeSinceLastFrame = 0;
curImageIndex++;
}
else
{
timeSinceLastFrame += deltaTime;
if (timeSinceLastFrame > 1.0/g_Framerate)
{
timeSinceLastFrame -= 1.0/g_Framerate;
curImageIndex++;
}
}
if (curImageIndex == images)
curImageIndex = 0;
}
requestAnimationFrame(frame);
// log('Here!');
}
function transcode(data)
{
Module._basis_init();
if (basisHandle != 0)
{
Module._basis_close(basisHandle);
basisHandle = 0;
}
if (src != 0)
{
src = 0;
Module._free(src);
}
srcSize = data.byteLength;
var bytes = new Uint8Array(data);
src = Module._malloc(srcSize);
arrayBufferCopy(bytes, Module.HEAPU8, src, srcSize);
basisHandle = Module._basis_open(src, srcSize);
if (!basisHandle)
{
log('Failed opening basis file!');
Module._free(src); src = 0;
width = 0;
return;
}
width = Module._basis_get_image_width(basisHandle, 0, 0);
height = Module._basis_get_image_height(basisHandle, 0, 0);
images = Module._basis_get_num_images(basisHandle, 0);
levels = Module._basis_get_num_levels(basisHandle, 0);
has_alpha = Module._basis_get_has_alpha(basisHandle);
if (!width || !height || !images || !levels)
{
Module._basis_close(basisHandle); basisHandle = 0;
Module._free(src); src = 0;
width = 0;
log('Failed getting info from basis file!');
return;
}
if ((bc7Supported) && (forceBC1 == 0))
{
format = BASIS_FORMAT.cTFBC7;
log('Decoding .basis data to BC7/BPTC');
}
else
{
format = BASIS_FORMAT.cTFBC1;
if (has_alpha)
{
format = BASIS_FORMAT.cTFBC3;
log('Decoding .basis data to BC3');
}
else
{
log('Decoding .basis data to BC1');
}
}
log('.basis file size: ' + srcSize);
if (levels == 1)
log('average bits/texel: ' + ((srcSize * 8.0) / (width * height * images)));
log('width: ' + width);
log('height: ' + height);
log('images: ' + images);
log('first image mipmap levels: ' + levels);
log('has_alpha: ' + has_alpha);
alignedWidth = (width + 3) & ~3;
alignedHeight = (height + 3) & ~3;
var canvas = elem('canvas');
canvas.width = alignedWidth;
canvas.height = alignedHeight;
var status = Module._basis_start_transcoding(basisHandle);
if (!status)
{
Module._basis_close(basisHandle); basisHandle = 0;
Module._free(src); src = 0;
width = 0;
log('basis_start_transcoding() failed!');
return;
}
curImageIndex = 0;
}
function dataLoaded(data)
{
log('Done loading .basis file');
if (bc7Supported)
log('EXT_texture_compression_bptc is supported!')
else
log('EXT_texture_compression_bptc is NOT supported!')
g_dataLoadedCountDown=50;
g_receivedData=data;
}
/**
* @param {Uint8Array} src
* @param {Uint8Array} dst
*/
function arrayBufferCopy(src, dst, dstByteOffset, numBytes) {
dst32Offset = dstByteOffset / 4;
var tail = (numBytes % 4);
var src32 = new Uint32Array(src.buffer, 0, (numBytes - tail) / 4);
var dst32 = new Uint32Array(dst.buffer);
for (var i = 0; i < src32.length; i++) {
dst32[dst32Offset + i] = src32[i];
}
for (var i = numBytes - tail; i < numBytes; i++) {
dst[dstByteOffset + i] = src[i];
}
}
function run() {
elem('logger').innerHTML = '';
loadArrayBuffer(elem('file').value, dataLoaded);
requestAnimationFrame(frame);
}
function initialize() {
var gl = elem('canvas').getContext('experimental-webgl');
// Load the DXT extension, and verify it exists.
if (!gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc') && !gl.getExtension('WEBGL_compressed_texture_s3tc')) {
dxtSupported = false;
elem('nodxt').style.display = 'block';
}
if (gl.getExtension('EXT_texture_compression_bptc'))
bc7Supported = true;
else
bc7Supported = false;
window.renderer = new Renderer(gl);
elem('file').addEventListener('keydown', function(e) {
if (e.keyCode == 13) {
run();
}
}, false);
run();
}
function alphaBlend() { drawMode = 0; redraw(); }
function viewRGB() { drawMode = 1; redraw(); }
function viewAlpha() { drawMode = 2; redraw(); }
function forceBC1Func() { forceBC1 = 1 - forceBC1; run(); }
function visCR()
{
var i = Module._basis_get_debug_flags();
i = i ^ BASIS_DEBUG_FLAGS.DEBUG_FLAG_VIS_CR;
Module._basis_set_debug_flags(i);
}
function visSels()
{
var i = Module._basis_get_debug_flags();
i = i ^ BASIS_DEBUG_FLAGS.DEBUG_FLAG_VIS_SELS;
Module._basis_set_debug_flags(i);
}
function visEndpoints()
{
var i = Module._basis_get_debug_flags();
i ^= BASIS_DEBUG_FLAGS.DEBUG_FLAG_VIS_ENDPOINTS;
Module._basis_set_debug_flags(i);
}
function togglePause()
{
g_paused = !g_paused;
}
function toggleFramerateLimit()
{
g_framerateLimit = !g_framerateLimit;
}
function restart()
{
curImageIndex = 0;
}
function slow()
{
if (g_Framerate >= 20.0)
g_Framerate = 5.0;
else
g_Framerate = 20.0;
}
function rewind()
{
if (g_targetFrame < 0)
{
g_targetFrame = curImageIndex;
if (g_targetFrame > 10)
g_targetFrame -= 10;
else
g_targetFrame = 0;
curImageIndex = 0;
}
}
</script>
</head>
<body onload="initialize()">
<br>
<div style="font-size: 24pt; font-weight: bold">
Basis Universal GPU Texture Video UASTC->BC7 Transcoding Test
</div>
<div style="font-size: 8pt;">
<br>This demo uses the <a href="https://github.com/BinomialLLC/basis_universal">Basis C++ transcoder</a> (compiled to Javascript using Emscripten) to transcode a .basis Universal Texture Video file directly to GPU texture data, with no intermediate recompression step.
<br>.basis universal GPU texture files can be quickly transcoded directly to any other GPU texture format with little to no quality loss.
<br>Thanks to Evan Parker for providing <a href="https://github.com/toji/webgl-texture-utils">webgl-texture-utils</a> and this test bed.
</div>
<br>
<br>
.basis file:
<input id="file" type="text" size=30 value="6.1.01.basis"></input>
<input type="button" value="Run!" onclick="run()"></input>
<br>
<br>
<input type="button" value="Alpha blend" onclick="alphaBlend()"></input>
<input type="button" value="View RGB" onclick="viewRGB()"></input>
<input type="button" value="View Alpha" onclick="viewAlpha()"></input>
<br>
<br>
<input type="button" value="Force BC1" onclick="forceBC1Func()"></input>
<br>
<br>
<input type="button" value="Visualize CR's" onclick="visCR()"></input>
<input type="button" value="Visualize Selectors" onclick="visSels()"></input>
<input type="button" value="Visualize Endpoints" onclick="visEndpoints()"></input>
<br>
<br>
<input type="button" value="No Framerate Limit" onclick="toggleFramerateLimit()"></input>
<input type="button" value="Pause" onclick="togglePause()"></input>
<input type="button" value="Restart" onclick="restart()"></input>
<input type="button" value="Toggle Slow Speed" onclick="slow()"></input>
<input type="button" value="Rewind" onclick="rewind()"></input>
<div style="position:absolute; left: 525px; top:130px; font-size: 20pt; font-weight: bold; color: red">
<div id="nodxt" style="display: none; width: 768px; font-size: 20pt; font-weight: bold; color: red">
NOTE: Your browser does not support DXT, so using RGB565
for the texture below. To get DXT, try Chrome 19+
(beta channel as of 2012-04-20; graphics card DXT support also required).
</div>
<canvas id='canvas'></canvas>
</div>
<br><br>
<div id='logger'></div>
</body>
</html>
Binary file not shown.
Binary file not shown.
+231
View File
@@ -0,0 +1,231 @@
/**
* Constructs a renderer object.
* @param {WebGLRenderingContext} gl The GL context.
* @constructor
*/
var Renderer = function(gl) {
/**
* The GL context.
* @type {WebGLRenderingContext}
* @private
*/
this.gl_ = gl;
/**
* The WebGLProgram.
* @type {WebGLProgram}
* @private
*/
this.program_ = gl.createProgram();
/**
* @type {WebGLShader}
* @private
*/
this.vertexShader_ = this.compileShader_(
Renderer.vertexShaderSource_, gl.VERTEX_SHADER);
/**
* @type {WebGLShader}
* @private
*/
this.fragmentShader_ = this.compileShader_(
Renderer.fragmentShaderSource_, gl.FRAGMENT_SHADER);
/**
* Cached uniform locations.
* @type {Object.<string, WebGLUniformLocation>}
* @private
*/
this.uniformLocations_ = {};
/**
* Cached attribute locations.
* @type {Object.<string, WebGLActiveInfo>}
* @private
*/
this.attribLocations_ = {};
/**
* A vertex buffer containing a single quad with xy coordinates from [-1,-1]
* to [1,1] and uv coordinates from [0,0] to [1,1].
* @private
*/
this.quadVertexBuffer_ = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);
var vertices = new Float32Array(
[-1.0, -1.0, 0.0, 1.0,
+1.0, -1.0, 1.0, 1.0,
-1.0, +1.0, 0.0, 0.0,
1.0, +1.0, 1.0, 0.0]);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
// init shaders
gl.attachShader(this.program_, this.vertexShader_);
gl.attachShader(this.program_, this.fragmentShader_);
gl.bindAttribLocation(this.program_, 0, 'vert');
gl.linkProgram(this.program_);
gl.useProgram(this.program_);
gl.enableVertexAttribArray(0);
gl.enable(gl.DEPTH_TEST);
gl.disable(gl.CULL_FACE);
var count = gl.getProgramParameter(this.program_, gl.ACTIVE_UNIFORMS);
for (var i = 0; i < /** @type {number} */(count); i++) {
var info = gl.getActiveUniform(this.program_, i);
var result = gl.getUniformLocation(this.program_, info.name);
this.uniformLocations_[info.name] = result;
}
count = gl.getProgramParameter(this.program_, gl.ACTIVE_ATTRIBUTES);
for (var i = 0; i < /** @type {number} */(count); i++) {
var info = gl.getActiveAttrib(this.program_, i);
var result = gl.getAttribLocation(this.program_, info.name);
this.attribLocations_[info.name] = result;
}
};
Renderer.prototype.finishInit = function() {
this.draw();
};
Renderer.prototype.createDxtTexture = function(dxtData, width, height, format) {
var gl = this.gl_;
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.compressedTexImage2D(
gl.TEXTURE_2D,
0,
format,
width,
height,
0,
dxtData);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
//gl.generateMipmap(gl.TEXTURE_2D)
gl.bindTexture(gl.TEXTURE_2D, null);
return tex;
};
Renderer.prototype.destroyTexture = function(tex)
{
var gl = this.gl_;
//gl.bindTexture(gl.TEXTURE_2D, 0);
gl.deleteTexture(tex);
}
Renderer.prototype.createRgb565Texture = function(rgb565Data, width, height) {
var gl = this.gl_;
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGB,
width,
height,
0,
gl.RGB,
gl.UNSIGNED_SHORT_5_6_5,
rgb565Data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
//gl.generateMipmap(gl.TEXTURE_2D)
gl.bindTexture(gl.TEXTURE_2D, null);
return tex;
};
Renderer.prototype.drawTexture = function(texture, width, height, mode) {
var gl = this.gl_;
// draw scene
gl.clearColor(0, 0, 0, 1);
gl.clearDepth(1.0);
gl.viewport(0, 0, width, height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(this.uniformLocations_.texSampler, 0);
var x = 0.0;
var y = 0.0;
if (mode == 1)
x = 1.0;
else if (mode == 2)
y = 1.0;
gl.uniform4f(this.uniformLocations_.control, x, y, 0.0, 0.0);
gl.enableVertexAttribArray(this.attribLocations_.vert);
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);
gl.vertexAttribPointer(this.attribLocations_.vert, 4, gl.FLOAT,
false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
/**
* Compiles a GLSL shader and returns a WebGLShader.
* @param {string} shaderSource The shader source code string.
* @param {number} type Either VERTEX_SHADER or FRAGMENT_SHADER.
* @return {WebGLShader} The new WebGLShader.
* @private
*/
Renderer.prototype.compileShader_ = function(shaderSource, type) {
var gl = this.gl_;
var shader = gl.createShader(type);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
return shader;
};
/**
* @type {string}
* @private
*/
Renderer.vertexShaderSource_ = [
'attribute vec4 vert;',
'varying vec2 v_texCoord;',
'void main() {',
' gl_Position = vec4(vert.xy, 0.0, 1.0);',
' v_texCoord = vert.zw;',
'}'
].join('\n');
/**
* @type {string}
* @private ' gl_FragColor = texture2D(texSampler, v_texCoord);',
*/
Renderer.fragmentShaderSource_ = [
'precision highp float;',
'uniform sampler2D texSampler;',
'uniform vec4 control;',
'varying vec2 v_texCoord;',
'void main() {',
' vec4 c;',
' c = texture2D(texSampler, v_texCoord);',
' if (control.x > 0.0)',
' {',
' c.w = 1.0;',
' }',
' else if (control.y > 0.0)',
' {',
' c.rgb = c.aaa; c.w = 1.0;',
' }',
' gl_FragColor = c;',
'}'
].join('\n');