HALF_FLOAT in Compute shaders

105 views
Skip to first unread message

Evgeny Demidov

unread,
Feb 19, 2019, 10:48:10 AM2/19/19
to WebGL Dev List
I've seen https://esdiscuss.org/topic/float16array discussion.
May be you can help me before I sink in searching and experiments.
1. In https://www.ibiblio.org/e-notes/webgl/gpu/CSpetal.htm vertexes are generated by Compute Shader. All works with FLOAT data but I get broken picture with HALF_FLOAT data
   gl.bufferData(gl.SHADER_STORAGE_BUFFER, new Uint16Array(4*nr*nf), gl.STATIC_DRAW);
...
   gl.vertexAttribPointer(aPosLoc, 4, gl.HALF_FLOAT, gl.FALSE, 0, 0)
what shall I use instead Uint16Array or Float32Array?
2. can I convert f32 (Float32Array) into f16 buffer by simple Compute Shader for Matrix multiplications experiments?

Evgeny

Ken Russell

unread,
Feb 19, 2019, 4:08:28 PM2/19/19
to WebGL Dev List
On Tue, Feb 19, 2019 at 7:48 AM Evgeny Demidov <demidov...@gmail.com> wrote:
I've seen https://esdiscuss.org/topic/float16array discussion.
May be you can help me before I sink in searching and experiments.
1. In https://www.ibiblio.org/e-notes/webgl/gpu/CSpetal.htm vertexes are generated by Compute Shader. All works with FLOAT data but I get broken picture with HALF_FLOAT data
   gl.bufferData(gl.SHADER_STORAGE_BUFFER, new Uint16Array(4*nr*nf), gl.STATIC_DRAW);
...
   gl.vertexAttribPointer(aPosLoc, 4, gl.HALF_FLOAT, gl.FALSE, 0, 0)
what shall I use instead Uint16Array or Float32Array?

Uint16Array is correct. The only thing that does is change how the CPU-side code interprets the bits. If you are uploading half-float data from the CPU to the GPU then you'll need to pack it yourself using a library like https://github.com/petamoriken/float16 .
 
2. can I convert f32 (Float32Array) into f16 buffer by simple Compute Shader for Matrix multiplications experiments?

If you are uploading data from the CPU then you will need to convert it per above. If you are just writing half-float values from your compute shader into the buffer, the GPU does the conversion so you don't need to do anything. (If you then want to read the half-float values in JavaScript you'll need to convert them on the way back using the same float16 library.)

-Ken


 

Evgeny

--
You received this message because you are subscribed to the Google Groups "WebGL Dev List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webgl-dev-lis...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evgeny Demidov

unread,
Feb 19, 2019, 11:53:05 PM2/19/19
to WebGL Dev List
On Wednesday, February 20, 2019 at 12:08:28 AM UTC+3, Kenneth Russell wrote:
On Tue, Feb 19, 2019 at 7:48 AM Evgeny Demidov <demidov...@gmail.com> wrote:
I've seen https://esdiscuss.org/topic/float16array discussion.
May be you can help me before I sink in searching and experiments.
1. In https://www.ibiblio.org/e-notes/webgl/gpu/CSpetal.htm vertexes are generated by Compute Shader. All works with FLOAT data but I get broken picture with HALF_FLOAT data
   gl.bufferData(gl.SHADER_STORAGE_BUFFER, new Uint16Array(4*nr*nf), gl.STATIC_DRAW);
...
   gl.vertexAttribPointer(aPosLoc, 4, gl.HALF_FLOAT, gl.FALSE, 0, 0)
what shall I use instead Uint16Array or Float32Array?

Uint16Array is correct. The only thing that does is change how the CPU-side code interprets the bits. If you are uploading half-float data from the CPU to the GPU then you'll need to pack it yourself using a library like https://github.com/petamoriken/float16 .
no all data are generated by compute shader (CS)!
But CS writes wrongly vec3 data (only float, vec2 and vec4 work correctly). I got similar broken meshes with vec3 vertex arrays. From the neighboring topic

"for this simple shader
const CSs = `#version 310 es
  layout (local_size_x = 2, local_size_y = 2, local_size_z = 1) in;
  layout (std430, binding = 0) buffer SSBO {
    vec3 data[];
  } ssbo;
void main() {
  ssbo.data[gl_LocalInvocationIndex] = vec3(gl_LocalInvocationID);
}
`;
I get strange result - output: [0,0,0, 0,1,0, 0,0,0, 1,0,0] (I inserted spaces by hand)" me and

"I suppose this is because of memory layout. An array of vec3 is rounded up to 4N even though std430.
If you allocate and get result with new Float32Array(4 * 4) by bufferData()/getBufferSubData(), you will get correct result - output: [0,0,0,(0), 1,0,0,(0), 0,1,0,(0), 1,1,0,(0)], (0) means auto inserted padding. The result you showed is first 12 components of this." Kentaro Kawakatsu.

May be "this is because of memory layout" again but complicated by f16.

and I think we needn't any library now. Let simple CS convert f32 <-> f16 data (at least for experiments) meanwhile we get Float16Array.

I'll try to make simple tests for writing into f16 buffers (Uint16Array 8-)

Evgeny

Evgeny Demidov

unread,
Feb 20, 2019, 5:35:03 AM2/20/19
to WebGL Dev List
Malevich's CS 16f "black square" (is stolen) - f16 vertex array is written by CS

//   gl.bufferData(gl.SHADER_STORAGE_BUFFER, new Float32Array(8), gl.STATIC_DRAW);
     gl.bufferData(gl.SHADER_STORAGE_BUFFER, new Uint16Array(8), gl.STATIC_DRAW);

//   gl.vertexAttribPointer(aPosLoc, 2, gl.FLOAT, gl.FALSE, 0, 0)
     gl.vertexAttribPointer(aPosLoc, 2, gl.HALF_FLOAT, gl.FALSE, 0, 0)
 f32 vertex array is written by CS - works fine
no CS, just hand made Uint16Array - works fine

how long does it takes to compile ANGLE (Chromium, Firefox) on Core2Duo?  :)

Evgeny

Evgeny Demidov

unread,
Feb 20, 2019, 12:34:13 PM2/20/19
to WebGL Dev List
https://chromium.googlesource.com/angle/angle/+/c5af8ba6ebd2246b4c214f30bd50ba80fa8b1e3e/src/compiler/translator/blocklayout.cpp

72 void Std140BlockEncoder::getBlockLayoutInfo(GLenum type, ...
78 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)

no f16 am I correct?

Evgeny

Ken Russell

unread,
Feb 20, 2019, 5:03:09 PM2/20/19
to WebGL Dev List
Thanks for tracking that down Evgeny. It looks like a bug.



--

Ken Russell

unread,
Feb 21, 2019, 2:36:49 PM2/21/19
to WebGL Dev List
Hi Evgeny,

Actually some clarification is needed on https://bugs.chromium.org/p/angleproject/issues/detail?id=3160 . Could you please follow up on it? Thanks.

-Ken

Reply all
Reply to author
Forward
0 new messages