Skip to content

Commit 3cf0e5f

Browse files
author
Robert Plummer
committed
fix: #410, but on windows. Committing to try in linux and OSX.
1 parent 87871ec commit 3cf0e5f

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/backend/web-gl/fragment-shader.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,57 @@ float decode8(vec4 texel, int index) {
204204
return 0.0;
205205
}
206206
207-
vec4 encode32(float f) {
207+
vec4 oldEncode32(float f) {
208208
float F = abs(f);
209-
float sign = f < 0.0 ? 1.0 : 0.0;
210-
float exponent = floor(log2(F));
211-
float mantissa = (exp2(-exponent) * F);
209+
float sign = step(0.0, -f);
210+
float exponent = floor(log2(f));
211+
float mantissa = f * pow(2.0, -exponent) - 1.0;
212212
// exponent += floor(log2(mantissa));
213+
exponent = exponent + 127.0;
213214
vec4 texel = vec4(F * exp2(23.0-exponent)) * SCALE_FACTOR_INV;
214215
texel.rg = integerMod(texel.rg, 256.0);
215216
texel.b = integerMod(texel.b, 128.0);
216217
texel.a = exponent*0.5 + 63.5;
217-
texel.ba += vec2(integerMod(exponent+127.0, 2.0), sign) * 128.0;
218+
texel.ba += vec2(integerMod(exponent, 2.0), sign) * 128.0;
218219
texel = floor(texel);
219220
texel *= 0.003921569; // 1/255
220221
__ENCODE32_ENDIANNESS__;
221222
return texel;
222223
}
224+
225+
// Borrowed from exellect info here: http://www.vizitsolutions.com/portfolio/webgl/gpgpu/speedBumps.html
226+
// http://www.vizitsolutions.com/portfolio/webgl/gpgpu/js/ToUnsignedBytes.js
227+
vec4 encode32(float value) {
228+
if (value == 0.0) return vec4(0, 0, 0, 0);
229+
230+
float exponent;
231+
float mantissa;
232+
vec4 result;
233+
float sgn;
234+
235+
sgn = step(0.0, -value);
236+
value = abs(value);
237+
238+
exponent = floor(log2(value));
239+
240+
mantissa = value*pow(2.0, -exponent)-1.0;
241+
exponent = exponent+127.0;
242+
result = vec4(0,0,0,0);
243+
244+
result.a = floor(exponent/2.0);
245+
exponent = exponent - result.a*2.0;
246+
result.a = result.a + 128.0*sgn;
247+
248+
result.b = floor(mantissa * 128.0);
249+
mantissa = mantissa - result.b / 128.0;
250+
result.b = result.b + exponent*128.0;
251+
252+
result.g = floor(mantissa*32768.0);
253+
mantissa = mantissa - result.g/32768.0;
254+
255+
result.r = floor(mantissa*8388608.0);
256+
return result/255.0;
257+
}
223258
// Dragons end here
224259
225260
int index;

0 commit comments

Comments
 (0)