|
1 | 1 | (function(GPU) { |
2 | | - function dimToTexSize(gl, dimensions) { |
| 2 | + function dimToTexSize(gpu, dimensions, output) { |
3 | 3 | var numTexels = dimensions[0]; |
4 | 4 | for (var i=1; i<dimensions.length; i++) { |
5 | 5 | numTexels *= dimensions[i]; |
6 | 6 | } |
| 7 | + |
| 8 | + if (gpu.OES_texture_float && !output) { |
| 9 | + numTexels = Math.ceil(numTexels / 4); |
| 10 | + } |
7 | 11 |
|
8 | 12 | // TODO: find out why this is broken in Safari |
9 | 13 | /* |
| 14 | + var gl = gpu.getGl(); |
10 | 15 | var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); |
11 | 16 | if (numTexels < maxSize) { |
12 | 17 | return [numTexels, 1]; |
|
168 | 173 | } |
169 | 174 | } |
170 | 175 |
|
171 | | - var texSize = dimToTexSize(gl, opt.dimensions); |
| 176 | + var texSize = dimToTexSize(gpu, opt.dimensions, true); |
172 | 177 |
|
173 | 178 | if (opt.graphical) { |
174 | 179 | if (opt.dimensions.length != 2) { |
|
208 | 213 | if (opt.hardcodeConstants) { |
209 | 214 | if (argType == "Array" || argType == "Texture") { |
210 | 215 | var paramDim = getDimensions(arguments[i], true); |
211 | | - var paramSize = dimToTexSize(gl, paramDim); |
| 216 | + var paramSize = dimToTexSize(gpu, paramDim); |
212 | 217 |
|
213 | 218 | paramStr += 'uniform highp sampler2D user_' + paramNames[i] + ';\n'; |
214 | 219 | paramStr += 'highp vec2 user_' + paramNames[i] + 'Size = vec2(' + paramSize[0] + '.0, ' + paramSize[1] + '.0);\n'; |
|
335 | 340 | ' highp vec3 xyz = vec3(floor(x + 0.5), floor(y + 0.5), floor(z + 0.5));', |
336 | 341 | (opt.wraparound ? ' xyz = mod(xyz, texDim);' : ''), |
337 | 342 | ' 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);' : ''), |
338 | 345 | ' highp float w = floor(texSize.x + 0.5);', |
339 | 346 | ' highp float s = integerMod(index, w);', |
340 | 347 | ' highp float t = float(int(index) / int(w));', |
341 | 348 | ' s += 0.5;', |
342 | 349 | ' 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);', |
344 | 357 | '}', |
345 | 358 | '', |
346 | 359 | 'highp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float y, highp float x) {', |
|
449 | 462 | var paramDim, paramSize, texture; |
450 | 463 | if (Array.isArray(arguments[textureCount])) { |
451 | 464 | paramDim = getDimensions(arguments[textureCount], true); |
452 | | - paramSize = dimToTexSize(gl, paramDim); |
| 465 | + paramSize = dimToTexSize(gpu, paramDim); |
453 | 466 |
|
454 | 467 | texture = gl.createTexture(); |
455 | 468 | gl.activeTexture(gl["TEXTURE"+textureCount]); |
|
460 | 473 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
461 | 474 |
|
462 | 475 | 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) { |
464 | 481 | paramArray.push(0); |
465 | 482 | } |
466 | 483 |
|
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 | + } |
469 | 492 | textures[textureCount] = texture; |
470 | 493 |
|
471 | 494 | var paramLoc = gl.getUniformLocation(program, "user_" + paramNames[textureCount]); |
|
0 commit comments