![]() |
|
GLSL Shader and bit operations on a state (FHP LGCA Simulation) |
JaykopX
Member #19,997
April 2021
|
Hello, in my Shader, i would like to use an ALLEGRO_BITMAP* called "state" to store bit information. The following GLSL-Shader does "work", but it is not reliable, since i get wrong results in some cases, which i think is based on floating point inaccuracy. I think i could solve this by using usampler2D instead of sampler2D. How do i configurate this? Shader Code: 1#version 400
2uniform sampler2D state;
3uniform vec2 resolution;
4layout(location = 0) out vec4 Out_State;
5
6//Get State-Bit b
7uint isSet(uint b) {
8 uvec4 fourBytes = uvec4(255u * texture(state, (gl_FragCoord.xy / resolution)) );
9 uint oneUInt = (fourBytes.r << 24u) | (fourBytes.g << 16u) | (fourBytes.b << 8u);// | (fourBytes.a);
10 return ((oneUInt >> b) & 1u);
11}
12
13void main() {
14 //Particle present on this cell
15 uint act[6];
16 act[0] = isSet(8);
17 act[1] = isSet(9);
18 act[2] = isSet(10);
19 act[3] = isSet(11);
20 act[4] = isSet(12);
21 act[5] = isSet(13);
22
23 uint count = act[0] + act[1]+ act[2] + act[3] + act[4] + act[5];
24
25 //Assign Color if bitState is set
26 if(count == 0) {
27 Out_State = vec4(0.0, 0.0, 0.0, 1.0);
28 }
29 else if(count == 1) {
30 Out_State = vec4(0.0, 0.0, 1.0, 1.0);
31 }
32 else if(count == 2) {
33 Out_State = vec4(0.0, 1.0, 1.0, 1.0);
34 }
35 else if(count == 3) {
36 Out_State = vec4(0.0, 1.0, 0.0, 1.0);
37 }
38 else if(count == 4) {
39 Out_State = vec4(1.0, 1.0, 0.0, 1.0);
40 }
41 else if(count == 5) {
42 Out_State = vec4(1.0, 0.0, 0.0, 1.0);
43 }
44 else if(count == 6) {
45 Out_State = vec4(1.0, 1.0, 1.0, 1.0);
46 }
47 //Assign Color if bitState is not set
48 else {
49 Out_State = vec4(1.0, 0.0, 1.0, 1.0); //ERROR Color
50 }
51}
Init Code: 2 float uniform_resolution[2] = { (float)ScreenW, (float)ScreenH };
3 ALLEGRO_BITMAP* GoLCurrentBuffer = al_create_bitmap(uniform_resolution.x, uniform_resolution.y);
4 al_use_shader(shader_Simulation);
5 al_set_shader_float_vector("resolution", 2, uniform_resolution, 1);
6 al_set_shader_sampler("state", GoLCurrentBuffer, 0);
Draw Code: 3 al_set_target_bitmap(GoLCurrentBuffer);
4 al_use_shader(shader_Simulation);
5 al_draw_scaled_bitmap(GoLPreviousBuffer, 0, 0, sw, sh, 0, 0, ScreenW, ScreenH, ALLEGRO_FLIP_NONE);
Thanks! Edit: Solved it with rearanging al_use_shader. It seems i messed some buffers. I also switched to texelFetch instead of texture. Makes things easier. |
|