Skip to content

Commit 8808d81

Browse files
committed
Add support for OES_texture_float inputs
1 parent 408beaf commit 8808d81

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/backend/mode_gpu.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
(function(GPU) {
2-
function dimToTexSize(gl, dimensions) {
2+
function dimToTexSize(gpu, dimensions, output) {
33
var numTexels = dimensions[0];
44
for (var i=1; i<dimensions.length; i++) {
55
numTexels *= dimensions[i];
66
}
7+
8+
if (gpu.OES_texture_float && !output) {
9+
numTexels = Math.ceil(numTexels / 4);
10+
}
711

812
// TODO: find out why this is broken in Safari
913
/*
14+
var gl = gpu.getGl();
1015
var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
1116
if (numTexels < maxSize) {
1217
return [numTexels, 1];
@@ -168,7 +173,7 @@
168173
}
169174
}
170175

171-
var texSize = dimToTexSize(gl, opt.dimensions);
176+
var texSize = dimToTexSize(gpu, opt.dimensions, true);
172177

173178
if (opt.graphical) {
174179
if (opt.dimensions.length != 2) {
@@ -208,7 +213,7 @@
208213
if (opt.hardcodeConstants) {
209214
if (argType == "Array" || argType == "Texture") {
210215
var paramDim = getDimensions(arguments[i], true);
211-
var paramSize = dimToTexSize(gl, paramDim);
216+
var paramSize = dimToTexSize(gpu, paramDim);
212217

213218
paramStr += 'uniform highp sampler2D user_' + paramNames[i] + ';\n';
214219
paramStr += 'highp vec2 user_' + paramNames[i] + 'Size = vec2(' + paramSize[0] + '.0, ' + paramSize[1] + '.0);\n';
@@ -335,12 +340,20 @@
335340
' highp vec3 xyz = vec3(floor(x + 0.5), floor(y + 0.5), floor(z + 0.5));',
336341
(opt.wraparound ? ' xyz = mod(xyz, texDim);' : ''),
337342
' highp float index = floor((xyz.z * texDim.x * texDim.y) + (xyz.y * texDim.x) + xyz.x + 0.5);',
343+
(gpu.OES_texture_float ? ' int channel = int(integerMod(index, 4.0));' : ''),
344+
(gpu.OES_texture_float ? ' index = float(int(index)/4);' : ''),
338345
' highp float w = floor(texSize.x + 0.5);',
339346
' highp float s = integerMod(index, w);',
340347
' highp float t = float(int(index) / int(w));',
341348
' s += 0.5;',
342349
' t += 0.5;',
343-
' return decode32(texture2D(tex, vec2(s / texSize.x, t / texSize.y)));',
350+
(gpu.OES_texture_float ? ' index = float(int(index)/4);' : ''),
351+
' highp vec4 texel = texture2D(tex, vec2(s / texSize.x, t / texSize.y));',
352+
(gpu.OES_texture_float ? ' if (channel == 0) return texel.r;' : ''),
353+
(gpu.OES_texture_float ? ' if (channel == 1) return texel.g;' : ''),
354+
(gpu.OES_texture_float ? ' if (channel == 2) return texel.b;' : ''),
355+
(gpu.OES_texture_float ? ' if (channel == 3) return texel.a;' : ''),
356+
' return decode32(texel);',
344357
'}',
345358
'',
346359
'highp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float y, highp float x) {',
@@ -449,7 +462,7 @@
449462
var paramDim, paramSize, texture;
450463
if (Array.isArray(arguments[textureCount])) {
451464
paramDim = getDimensions(arguments[textureCount], true);
452-
paramSize = dimToTexSize(gl, paramDim);
465+
paramSize = dimToTexSize(gpu, paramDim);
453466

454467
texture = gl.createTexture();
455468
gl.activeTexture(gl["TEXTURE"+textureCount]);
@@ -460,12 +473,22 @@
460473
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
461474

462475
var paramArray = flatten(arguments[textureCount]);
463-
while (paramArray.length < paramSize[0] * paramSize[1]) {
476+
var paramLength = paramSize[0] * paramSize[1];
477+
if (gpu.OES_texture_float) {
478+
paramLength *= 4;
479+
}
480+
while (paramArray.length < paramLength) {
464481
paramArray.push(0);
465482
}
466483

467-
var argBuffer = new Uint8Array((new Float32Array(paramArray)).buffer);
468-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, paramSize[0], paramSize[1], 0, gl.RGBA, gl.UNSIGNED_BYTE, argBuffer);
484+
var argBuffer;
485+
if (gpu.OES_texture_float) {
486+
argBuffer = new Float32Array(paramArray);
487+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, paramSize[0], paramSize[1], 0, gl.RGBA, gl.FLOAT, argBuffer);
488+
} else {
489+
argBuffer = new Uint8Array((new Float32Array(paramArray)).buffer);
490+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, paramSize[0], paramSize[1], 0, gl.RGBA, gl.UNSIGNED_BYTE, argBuffer);
491+
}
469492
textures[textureCount] = texture;
470493

471494
var paramLoc = gl.getUniformLocation(program, "user_" + paramNames[textureCount]);

src/gpu.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ var GPU = (function() {
2323
gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt);
2424
}
2525

26-
gl.getExtension('OES_texture_float');
27-
gl.getExtension('OES_texture_float_linear');
28-
gl.getExtension('OES_element_index_uint');
26+
this.OES_texture_float = gl.getExtension('OES_texture_float');
27+
this.OES_texture_float_linear = gl.getExtension('OES_texture_float_linear');
28+
this.OES_texture_index_uint = gl.getExtension('OES_element_index_uint');
2929

3030
this.gl = gl;
3131
this.canvas = canvas;

0 commit comments

Comments
 (0)