mirror of
https://github.com/mikolalysenko/glsl-read-float.git
synced 2025-12-08 21:26:01 +00:00
adding files
This commit is contained in:
commit
3a70a43920
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
lib-cov
|
||||
*.seed
|
||||
*.log
|
||||
*.csv
|
||||
*.dat
|
||||
*.out
|
||||
*.pid
|
||||
*.gz
|
||||
|
||||
pids
|
||||
logs
|
||||
results
|
||||
|
||||
npm-debug.log
|
||||
node_modules/*
|
||||
*.DS_Store
|
||||
55
README.md
Normal file
55
README.md
Normal file
@ -0,0 +1,55 @@
|
||||
glsl-read-float
|
||||
===============
|
||||
Workaround for reading floating point values back from the GPU using GLSL.
|
||||
|
||||
## Example
|
||||
|
||||
```javascript
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
npm install glsl-read-float
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### GLSL
|
||||
|
||||
```glsl
|
||||
#pragma glslify: packFloat = require(glsl-read-float)
|
||||
```
|
||||
|
||||
#### `vec4 packed = packFloat(float f)`
|
||||
Packs a floating point number into an 8bit RGBA color vector, which can be written to the display using `gl_FragColor`, for example.
|
||||
|
||||
* `f` is a `float` number
|
||||
|
||||
**Returns** A packed `vec4` encoding the value of `f`
|
||||
|
||||
### JavaScript
|
||||
|
||||
```javascript
|
||||
var unpackFloat = require("glsl-read-float")
|
||||
```
|
||||
|
||||
#### `var f = readFloat(x, y, z, w)`
|
||||
Unpacks a packed `vec4` into a single floating point value.
|
||||
|
||||
* `x` is the first component of the packed float
|
||||
* `y` is the second component of the packed float
|
||||
* `z` is the third component of the packed float
|
||||
* `w` is the fourth component of the packed float
|
||||
|
||||
**Returns** A number which is the unpacked value of the floating point input.
|
||||
|
||||
## Credits
|
||||
|
||||
This routine was originally written by @ultraist. You can find his blog here: http://ultraist.hatenablog.com/
|
||||
|
||||
The post describing this code was published here:
|
||||
|
||||
[WebGL GPGPUでfloatの結果を得る](http://ultraist.hatenablog.com/entry/20110608/1307539319)
|
||||
|
||||
The npm entry and glslify packaging are currently maintained by Mikola Lysenko.
|
||||
41
example/example.js
Normal file
41
example/example.js
Normal file
@ -0,0 +1,41 @@
|
||||
"use strict"
|
||||
|
||||
var triangle = require('a-big-triangle')
|
||||
var fit = require('canvas-fit')
|
||||
var getContext = require('gl-context')
|
||||
var createShader = require('glslify')
|
||||
var unpackFloat = require("../index.js")
|
||||
|
||||
var canvas = container.appendChild(document.createElement('canvas'))
|
||||
var gl = getContext(canvas, render)
|
||||
|
||||
window.addEventListener('resize', fit(canvas), false)
|
||||
|
||||
var shader = glslify({
|
||||
vert: "\
|
||||
attribute vec2 position;\
|
||||
void main() {\
|
||||
gl_Position = vec4(position, 0, 1);\
|
||||
}",
|
||||
frag: "\
|
||||
uniform highp float f;\
|
||||
#pragma glslify: packFloat = require(../index.glsl)\
|
||||
void main() {\
|
||||
gl_FragColor = packFloat(f);\
|
||||
}"})(gl)
|
||||
|
||||
function render() {
|
||||
var num = Math.random()
|
||||
|
||||
//Draw shader
|
||||
shader.bind()
|
||||
shader.uniforms.f = num
|
||||
triangle(gl)
|
||||
|
||||
//Read back the float
|
||||
var buffer = new Uint8Array(4)
|
||||
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buffer)
|
||||
|
||||
//Log output to console
|
||||
console.log("expected:", num, "got:", )
|
||||
}
|
||||
28
index.glsl
Normal file
28
index.glsl
Normal file
@ -0,0 +1,28 @@
|
||||
vec4 nvjs_encode_float(float v) {
|
||||
vec4 c = vec4(0.0, 0.0, 0.0, 0.0);
|
||||
if (v < 0.0) {
|
||||
c[0] += 64.0;
|
||||
v = -v;
|
||||
}
|
||||
float f = 0.0;
|
||||
float e = ceil(log2(v));
|
||||
float m = v * exp2(-e);
|
||||
if (e < 0.0) {
|
||||
e = -e;
|
||||
c[0] += 128.0;
|
||||
}
|
||||
c[0] += e;
|
||||
m *= 255.0;
|
||||
f = floor(m);
|
||||
c[1] = f;
|
||||
m -= f;
|
||||
m *= 255.0;
|
||||
f = floor(m);
|
||||
c[2] = f;
|
||||
m -= f;
|
||||
m *= 255.0;
|
||||
c[3] = floor(m);
|
||||
return c * 3.921569E-03;
|
||||
}
|
||||
|
||||
#pragma glslify: export(nvjs_encode_float)
|
||||
56
index.js
Normal file
56
index.js
Normal file
@ -0,0 +1,56 @@
|
||||
//Adapted from here: http://ultraist.hatenablog.com/entry/20110608/1307539319
|
||||
module.exports = decodeFloat
|
||||
|
||||
var exp2_table = [
|
||||
2.168404E-19, 4.336809E-19, 8.673617E-19, 1.734723E-18,
|
||||
3.469447E-18, 6.938894E-18, 1.387779E-17, 2.775558E-17,
|
||||
5.551115E-17, 1.110223E-16, 2.220446E-16, 4.440892E-16,
|
||||
8.881784E-16, 1.776357E-15, 3.552714E-15, 7.105427E-15,
|
||||
1.421085E-14, 2.842171E-14, 5.684342E-14, 1.136868E-13,
|
||||
2.273737E-13, 4.547474E-13, 9.094947E-13, 1.818989E-12,
|
||||
3.637979E-12, 7.275958E-12, 1.455192E-11, 2.910383E-11,
|
||||
5.820766E-11, 1.164153E-10, 2.328306E-10, 4.656613E-10,
|
||||
9.313226E-10, 1.862645E-09, 3.725290E-09, 7.450581E-09,
|
||||
1.490116E-08, 2.980232E-08, 5.960464E-08, 1.192093E-07,
|
||||
2.384186E-07, 4.768372E-07, 9.536743E-07, 1.907349E-06,
|
||||
3.814697E-06, 7.629395E-06, 1.525879E-05, 3.051758E-05,
|
||||
6.103516E-05, 1.220703E-04, 2.441406E-04, 4.882812E-04,
|
||||
9.765625E-04, 1.953125E-03, 3.906250E-03, 7.812500E-03,
|
||||
1.562500E-02, 3.125000E-02, 6.250000E-02, 1.250000E-01,
|
||||
2.500000E-01, 5.000000E-01, 1.000000E+00, 2.000000E+00,
|
||||
4.000000E+00, 8.000000E+00, 1.600000E+01, 3.200000E+01,
|
||||
6.400000E+01, 1.280000E+02, 2.560000E+02, 5.120000E+02,
|
||||
1.024000E+03, 2.048000E+03, 4.096000E+03, 8.192000E+03,
|
||||
1.638400E+04, 3.276800E+04, 6.553600E+04, 1.310720E+05,
|
||||
2.621440E+05, 5.242880E+05, 1.048576E+06, 2.097152E+06,
|
||||
4.194304E+06, 8.388608E+06, 1.677722E+07, 3.355443E+07,
|
||||
6.710886E+07, 1.342177E+08, 2.684355E+08, 5.368709E+08,
|
||||
1.073742E+09, 2.147484E+09, 4.294967E+09, 8.589935E+09,
|
||||
1.717987E+10, 3.435974E+10, 6.871948E+10, 1.374390E+11,
|
||||
2.748779E+11, 5.497558E+11, 1.099512E+12, 2.199023E+12,
|
||||
4.398047E+12, 8.796093E+12, 1.759219E+13, 3.518437E+13,
|
||||
7.036874E+13, 1.407375E+14, 2.814750E+14, 5.629500E+14,
|
||||
1.125900E+15, 2.251800E+15, 4.503600E+15, 9.007199E+15,
|
||||
1.801440E+16, 3.602880E+16, 7.205759E+16, 1.441152E+17,
|
||||
2.882304E+17, 5.764608E+17, 1.152922E+18, 2.305843E+18
|
||||
]
|
||||
|
||||
function decodeFloat(x, y, z, w) {
|
||||
var m = y * 3.921569E-03
|
||||
+ z * 1.537870E-05
|
||||
+ w * 6.030863E-08
|
||||
var e = x
|
||||
var i_sign = 0
|
||||
if (e & 0x80) {
|
||||
i_sign = 1
|
||||
e &= ~0x80
|
||||
}
|
||||
if (e & 0x40) {
|
||||
m = -m
|
||||
e &= ~0x40
|
||||
}
|
||||
if (i_sign) {
|
||||
e = -e
|
||||
}
|
||||
return m * exp2_table[e + 62]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user