I am trying to write a real time raytracer using a compute shader in openGl. in the compute shader I am using imageStore to write to a texture and then passing the texture to a vert/frag shader to render a quad. The problem is that my texture does not update for the subsequent frames. I have tried calling glMemoryBarrier
with both the GL_TEXTURE_FETCH_BARRIER_BIT
and the GL_TEXTURE_UPDATE_BARRIER_BIT
but had no luck with either. the code for my shaders is as below
compute shader
#version 430 compatibility
struct Sphere {
vec3 position;
float radius;
};
struct Ray{
vec3 origin;
vec3 dir;
};
uniform Sphere sphere;
uniform image2D destTex;
layout (std430, binding = 5) buffer Col
{
vec4 Colors [ ];
};
float hitSphere(Ray r, Sphere s){
float s_vv = dot(r.dir, r.dir);
float s_ov = dot(r.origin, r.dir);
float s_mv = dot(s.position, r.dir);
float s_mm = dot(s.position, s.position);
float s_mo = dot(s.position, r.origin);
float s_oo = dot(r.origin, r.origin);
float d = s_ov*s_ov-2*s_ov*s_mv+s_mv*s_mv-s_vv*(s_mm-2*s_mo*s_oo-s.radius*s.radius);
if(d < 0){
return -1.0f;
} else if(d == 0){
return (s_mv-s_ov)/s_vv;
} else {
float t1 = 0, t2 = 0;
t1 = s_mv-s_ov;
t2 = (t1-sqrt(d))/s_vv;
t1 = (t1+sqrt(d))/s_vv;
return t1>t2? t2 : t1 ;
}
}
layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
void main()
{
// const vec3 G = vec3 (0.0f, -9.8f, 0.0f);
uint g_id = gl_GlobalInvocationID.x;
uint x = gl_GlobalInvocationID.x;
uint y = gl_GlobalInvocationID.y;
if(x<640 && y<480) {
Ray r = {vec3(0,0,0), vec3(float(640)/2.0f-float(x), float(480)/2.0f-float(y), 200)};
float t = hitSphere(r, sphere);
if(t <= -0.001f){
imageStore(destTex, ivec2(x, y), vec4(0.0, 0.0, 0.0, 1.0));
} else {
imageStore(destTex, ivec2(x, y), vec4(0.0, 1.0, 0.0, 1.0));
}
}
}
Fragment shader
out vec4 FragColor;
uniform sampler2D srcTex;
in vec2 fragTexcoords;
in vec4 fragPos;
void main()
{
vec4 color = texture(srcTex, fragTexcoords);
FragColor = vec4(color.xyz, 1.0);
}
I am updating the position of the sphere every frame but the image does not get updated. However, if i change the position before the initial invocation it is reflected in the image