I am trying to do some raytracing in OpenGL via the compute shader and I came across a weird problem. At the moment I just want to display a sphere without any shading. My compute shader launches a ray for every pixel and looks like this:
#version 430
struct Sphere{
vec4 position;
float radius;
};
struct Ray{
vec3 origin;
vec3 dir;
};
uniform image2D outputTexture;
uniform uint width;
uniform uint height;
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.xyz, r.dir);
float s_mm = dot(s.position.xyz, s.position.xyz);
float s_mo = dot(s.position.xyz, 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(){
uint x = gl_GlobalInvocationID.x;
uint y = gl_GlobalInvocationID.y;
if(x < 1024 && y < 768){
float t = 0.0f;
Ray r = {vec3(0,0,0), vec3(width/2-x, height/2-y, 1000)};
Sphere sp ={vec4(0, 0, 35, 1), 5.0f};
t = hitSphere(r, sp);
if(t <= -0.001f){
imageStore(outputTexture, ivec2(x, y), vec4(0.0, 0.0, 0.0, 1.0));
} else {
imageStore(outputTexture, ivec2(x, y), vec4(0.0, 1.0, 0.0, 1.0));
}
if(x == 550 && y == 390){
imageStore(outputTexture, ivec2(x, y), vec4(1.0, 0.0, 0.0, 1.0));
}
}
}
When I run the application I get the following image:
But when I run the same algorithm on the CPU I get the following more convincing image:
First I thought I didn't dispatch enough work-groups so that not every pixel gets its own compute shader invocation but that's not the case. As you can see in the GPU rendered image there is a red pixel in the middle which is caused by the last lines in the compute shader. This can be reproduced for every other pixel.
I use a resolution of 1024x768 at the moment and this is how I dispatch my compute shader:
#define WORK_GROUP_SIZE 16
void OpenGLRaytracer::renderScene(int width, int height){
glUseProgram(_progID);
glDispatchCompute(width/WORK_GROUP_SIZE, height/WORK_GROUP_SIZE,1);
glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);
}
Where is the mistake? Might there be a problem with the accuracy of the floating point calculations?
The mistake is in this line:
Ray r = {vec3(0,0,0), vec3(width/2-x, height/2-y, 1000)};
Since width, height, x and y are unsigned variables you will get problems when the term width/2-x becomes negative.
This solves the problem:
Ray r = {vec3(0,0,0), vec3(float(width)/2.0f-float(x), float(height)/2.0f-float(y), 1000)};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With