Skip to content

Commit e90b8aa

Browse files
committed
Fix hardcode constants option
1 parent 13923a9 commit e90b8aa

1 file changed

Lines changed: 48 additions & 66 deletions

File tree

src/backend/mode_gpu.js

Lines changed: 48 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,21 @@
210210
var paramDim = getDimensions(arguments[i], true);
211211
var paramSize = dimToTexSize(gl, paramDim);
212212

213-
paramStr += 'uniform sampler2D user_' + paramNames[i] + ';\n';
214-
paramStr += 'vec2 user_' + paramNames[i] + 'Size = vec2(' + paramSize[0] + ',' + paramSize[1] + ');\n';
215-
paramStr += 'vec3 user_' + paramNames[i] + 'Dim = vec3(' + paramDim[0] + ', ' + paramDim[1] + ', ' + paramDim[2] + ');\n';
213+
paramStr += 'uniform highp sampler2D user_' + paramNames[i] + ';\n';
214+
paramStr += 'highp vec2 user_' + paramNames[i] + 'Size = vec2(' + paramSize[0] + '.0, ' + paramSize[1] + '.0);\n';
215+
paramStr += 'highp vec3 user_' + paramNames[i] + 'Dim = vec3(' + paramDim[0] + '.0, ' + paramDim[1] + '.0, ' + paramDim[2] + '.0);\n';
216+
} else if (argType == "Number" && Number.isInteger(arguments[i])) {
217+
paramStr += 'highp float user_' + paramNames[i] + ' = ' + arguments[i] + '.0;\n';
216218
} else if (argType == "Number") {
217-
paramStr += 'float user_' + paramNames[i] + ' = ' + arguments[i] + ';\n';
219+
paramStr += 'highp float user_' + paramNames[i] + ' = ' + arguments[i] + ';\n';
218220
}
219221
} else {
220222
if (argType == "Array" || argType == "Texture") {
221-
paramStr += 'uniform sampler2D user_' + paramNames[i] + ';\n';
222-
paramStr += 'uniform vec2 user_' + paramNames[i] + 'Size;\n';
223-
paramStr += 'uniform vec3 user_' + paramNames[i] + 'Dim;\n';
223+
paramStr += 'uniform highp sampler2D user_' + paramNames[i] + ';\n';
224+
paramStr += 'uniform highp vec2 user_' + paramNames[i] + 'Size;\n';
225+
paramStr += 'uniform highp vec3 user_' + paramNames[i] + 'Dim;\n';
224226
} else if (argType == "Number") {
225-
paramStr += 'uniform float user_' + paramNames[i] + ';\n';
227+
paramStr += 'uniform highp float user_' + paramNames[i] + ';\n';
226228
}
227229
}
228230
}
@@ -238,10 +240,10 @@
238240
'precision highp int;',
239241
'precision highp sampler2D;',
240242
'',
241-
'attribute vec2 aPos;',
242-
'attribute vec2 aTexCoord;',
243+
'attribute highp vec2 aPos;',
244+
'attribute highp vec2 aTexCoord;',
243245
'',
244-
'varying vec2 vTexCoord;',
246+
'varying highp vec2 vTexCoord;',
245247
'',
246248
'void main(void) {',
247249
' gl_Position = vec4(aPos, 0, 1);',
@@ -252,21 +254,22 @@
252254
var fragShaderSrc = [
253255
'precision highp float;',
254256
'precision highp int;',
257+
'precision highp sampler2D;',
255258
'',
256259
'#define LOOP_MAX '+ (opt.loopMaxIterations ? parseInt(opt.loopMaxIterations)+'.0' : '100.0'),
257260
'#define EPSILON 0.0000001',
258261
'',
259-
opt.hardcodeConstants ? 'vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform vec3 uOutputDim;',
260-
opt.hardcodeConstants ? 'vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform vec2 uTexSize;',
261-
'varying vec2 vTexCoord;',
262+
opt.hardcodeConstants ? 'highp vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform highp vec3 uOutputDim;',
263+
opt.hardcodeConstants ? 'highp vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform highp vec2 uTexSize;',
264+
'varying highp vec2 vTexCoord;',
262265
'',
263-
'float integerMod(float x, float y) {',
264-
' float res = floor(x - y * floor(x/y));',
265-
' if (res > y - 0.5) res = 0.0;',
266+
'highp float integerMod(highp float x, highp float y) {',
267+
' highp float res = floor(mod(x, y));',
268+
' if (res > floor(y) - 1.0) res = 0.0;',
266269
' return res;',
267270
'}',
268271
'',
269-
'int integerMod(int x, int y) {',
272+
'highp int integerMod(highp int x, highp int y) {',
270273
' return int(integerMod(float(x), float(y)));',
271274
'}',
272275
'',
@@ -278,9 +281,9 @@
278281
(endianness == 'LE' ? '' : ' rgba.rgba = rgba.abgr;'),
279282
' rgba *= 255.0;',
280283
' rgba = floor(rgba+0.5);',
281-
' float sign = rgba.a > 127.0 ? -1.0 : 1.0;',
282-
' float exponent = 2.0 * integerMod(rgba.a, 128.0) + (rgba.b > 127.0 ? 1.0 : 0.0);',
283-
' float res;',
284+
' highp float sign = rgba.a > 127.0 ? -1.0 : 1.0;',
285+
' highp float exponent = 2.0 * integerMod(rgba.a, 128.0) + (rgba.b > 127.0 ? 1.0 : 0.0);',
286+
' highp float res;',
284287
' if (abs(exponent) < EPSILON) {',
285288
' res = sign * 0.0;',
286289
' } else {',
@@ -297,35 +300,14 @@
297300
'highp vec4 encode32(highp float f) {',
298301
' if (f == 0.0) return vec4(0.0);',
299302
' highp float F = abs(f);',
300-
' float sign = f < 0.0 ? 1.0 : 0.0;',
301-
' float log2F = log2(F);',
302-
' highp float exponentF = floor(log2F); ',
303-
' highp float mantissaF = (exp2(-exponentF) * F);',
304-
' exponentF = floor(log2F + 127.0) + floor(log2(mantissaF));',
305-
' float exponent;',
306-
' if (f > 1000.0) {',
307-
' exponent = log2F < 0.0 ? floor(log2F)-1.0 : floor(log2F);',
308-
' } else {',
309-
' exponent = exponentF - 127.0;',
310-
' }',
311-
' float mantissa_part1 = integerMod(F * exp2(23.0-exponent), 256.0);',
312-
' float mantissa_part2 = integerMod(F * exp2(15.0-exponent), 256.0);',
313-
' float mantissa_part3 = integerMod(F * exp2(7.0-exponent), 128.0);',
314-
' float test = exp2(exponent);',
315-
' test += mantissa_part3 * exp2(exponent-7.0);',
316-
' test += mantissa_part2 * exp2(exponent-15.0);',
317-
' test += mantissa_part1 * exp2(exponent-23.0);',
318-
' float error = log2(test) - log2F;',
319-
' if (abs(error) >= 1.0) {',
320-
' exponent -= floor(error);',
321-
' mantissa_part1 = integerMod(F * exp2(23.0-exponent), 256.0);',
322-
' mantissa_part2 = integerMod(F * exp2(15.0-exponent), 256.0);',
323-
' mantissa_part3 = integerMod(F * exp2(7.0-exponent), 128.0);',
324-
' } else if (abs(error) > 0.0) {',
325-
' mantissa_part1 = 0.0;',
326-
' mantissa_part2 = 0.0;',
327-
' mantissa_part3 = 0.0;',
328-
' }',
303+
' highp float sign = f < 0.0 ? 1.0 : 0.0;',
304+
' highp float log2F = log2(F);',
305+
' highp float exponent = floor(log2F);',
306+
' highp float mantissa = (exp2(-exponent) * F);',
307+
' exponent = floor(log2F) + floor(log2(mantissa));',
308+
' highp float mantissa_part1 = integerMod(F * exp2(23.0-exponent), 256.0);',
309+
' highp float mantissa_part2 = integerMod(F * exp2(15.0-exponent), 256.0);',
310+
' highp float mantissa_part3 = integerMod(F * exp2(7.0-exponent), 128.0);',
329311
' exponent += 127.0;',
330312
' vec4 rgba;',
331313
' rgba.a = 128.0 * sign + floor(exponent/2.0);',
@@ -338,40 +320,40 @@
338320
'}',
339321
'// Dragons end here',
340322
'',
341-
'float index;',
342-
'vec3 threadId;',
323+
'highp float index;',
324+
'highp vec3 threadId;',
343325
'',
344-
'vec3 indexTo3D(float idx, vec3 texDim) {',
326+
'highp vec3 indexTo3D(highp float idx, highp vec3 texDim) {',
345327
' idx = floor(idx + 0.5);',
346-
' float z = floor(idx / (texDim.x * texDim.y));',
328+
' highp float z = floor(idx / (texDim.x * texDim.y));',
347329
' idx -= z * texDim.x * texDim.y;',
348-
' float y = floor(idx / texDim.x);',
349-
' float x = integerMod(idx, texDim.x);',
330+
' highp float y = floor(idx / texDim.x);',
331+
' highp float x = integerMod(idx, texDim.x);',
350332
' return vec3(x, y, z);',
351333
'}',
352334
'',
353-
'float get(sampler2D tex, vec2 texSize, vec3 texDim, float z, float y, float x) {',
354-
' vec3 xyz = vec3(floor(x + 0.5), floor(y + 0.5), floor(z + 0.5));',
335+
'highp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float z, highp float y, highp float x) {',
336+
' highp vec3 xyz = vec3(floor(x + 0.5), floor(y + 0.5), floor(z + 0.5));',
355337
(opt.wraparound ? ' xyz = mod(xyz, texDim);' : ''),
356-
' float index = floor((xyz.z * texDim.x * texDim.y) + (xyz.y * texDim.x) + xyz.x + 0.5);',
357-
' float w = floor(texSize.x + 0.5);',
358-
' float s = integerMod(index, w);',
359-
' float t = float(int(index) / int(w));',
338+
' highp float index = floor((xyz.z * texDim.x * texDim.y) + (xyz.y * texDim.x) + xyz.x + 0.5);',
339+
' highp float w = floor(texSize.x + 0.5);',
340+
' highp float s = integerMod(index, w);',
341+
' highp float t = float(int(index) / int(w));',
360342
' s += 0.5;',
361343
' t += 0.5;',
362344
' return decode32(texture2D(tex, vec2(s / texSize.x, t / texSize.y)));',
363345
'}',
364346
'',
365-
'float get(sampler2D tex, vec2 texSize, vec3 texDim, float y, float x) {',
347+
'highp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float y, highp float x) {',
366348
' return get(tex, texSize, texDim, 0.0, y, x);',
367349
'}',
368350
'',
369-
'float get(sampler2D tex, vec2 texSize, vec3 texDim, float x) {',
351+
'highp float get(highp sampler2D tex, highp vec2 texSize, highp vec3 texDim, highp float x) {',
370352
' return get(tex, texSize, texDim, 0.0, 0.0, x);',
371353
'}',
372354
'',
373355
'const bool outputToColor = ' + (opt.graphical? 'true' : 'false') + ';',
374-
'vec4 actualColor;',
356+
'highp vec4 actualColor;',
375357
'void color(float r, float g, float b, float a) {',
376358
' actualColor = vec4(r,g,b,a);',
377359
'}',
@@ -380,7 +362,7 @@
380362
' color(r,g,b,1.0);',
381363
'}',
382364
'',
383-
'float kernelResult = 0.0;',
365+
'highp float kernelResult = 0.0;',
384366
paramStr,
385367
constantsStr,
386368
builder.webglString("kernel", opt),

0 commit comments

Comments
 (0)