floating point - Howto best pack 2 texture coordinates for fast vertex buffer processing ? (WebGL GPU Float Packing) -
i wondering way smartes pack 2 texture coords fast usage in vertex shaders given following circumstances:
- both texture coords can either 1.0f or 0.0f => 1 bit each enough
- i have pack 2 coords glbyte (8 bit) attribute variable; best if 6 bits remain available other usages
my issues i'm not familiar bit-layout of floats myself, can imagine 1 bit in 0.0f needs flipped become 1.0f - dunno one; using bitwise ops should incredible fast (btw: gpu's faster bitwise stuff compared arithmetic, cpus ?).
it's done this, in vertex shader:
uv = fract(vertex.xy); normal = fract(vertex.z); gl_position = mvp * vec4(floor(vertex), 1.0)
obviously, vertex xyz must scaled before storing vbo maximum texture resolution, 256x256 in case:
vbo.push_back(vertex.mul_xyz_scalar(256.0).floor().add_xyz(uv.x, uv.y, normal));
so, if vertex.x .00390625, becomes 1. positions below 1/256 subnormals (lost, floor above truncates it). uv assumed in 0-1 range. if insist, can multiply 2 instead of 256, , 0 , 1 lost.
a word of warning. technique work only if you're bound memory bandwidth (many vertices, simple shaders, typically complex static meshes). otherwise additional floor() cost in shader not worth , you're better off storing uv , normals side.
finally, vertices must scaled 256 (if you're using different shaders without packed uv/n), or scaled down 256 via mvp matrix (this not incur additional overhead, unless naively done directly in shader without using existing matrix).
ninja edit: here fast general function "bytes" packed inside floats.
/* * packed with: (x*32768+y*128+z)/32768.0 x , y 0-255, z 0-127. * function returns x , y in 0-256, , z in 0-1 range, scale 128 needed. */ void unpack887(in float v, out float x, out float y, out float z) { x = floor(v); float rem = (v - x) * 256.0; y = floor(rem); z = rem - y; }
Comments
Post a Comment