diff --git a/shader/include/constants.glsl b/shader/include/constants.glsl index 774a06e..4720c02 100644 --- a/shader/include/constants.glsl +++ b/shader/include/constants.glsl @@ -1,5 +1,4 @@ -const float INF = 17.0; +const float INF = 50.0; const float PI = 3.14159; const float E = 2.71828; -const int SAMPLES = 1; -const int BOUNCES = 8; +const int BOUNCES = 4; diff --git a/shader/root/rt.glsl b/shader/root/rt.glsl index f84c496..191a2d8 100644 --- a/shader/root/rt.glsl +++ b/shader/root/rt.glsl @@ -4,10 +4,13 @@ layout(local_size_x = 1, local_size_y = 1) in; // size of local // gbuffer? layout(rgba32f, binding = 2) readonly uniform image2D _g0; +layout(rgba32f, binding = 3) readonly uniform image2D _g1; // final output layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture +uniform vec3 _skyColor = vec3(0.68,0.85,0.9); + // TODO: some of these depend on each other!! need be be in this order for now c: #include func.glsl #include constants.glsl @@ -30,7 +33,7 @@ vec3 shade(inout Ray ray, RayHit hit) } // sky color - return vec3(0.68,0.85,0.9); + return _skyColor; } void main() @@ -43,10 +46,15 @@ void main() vec2 uv = pixelUv(pixelCoords, dims); + vec4 g[2]; + // load data from first pass - vec4 d = imageLoad(_g0, ivec2(gl_GlobalInvocationID.xy)); - float depth = d.w; - vec3 normal = d.xyz*2.0-1.0; // unpack normal packaged into texture + g[0] = imageLoad(_g0, ivec2(gl_GlobalInvocationID.xy)); + float depth = g[0].w; + vec3 normal = g[0].xyz*2.0-1.0; // unpack normal packaged into texture + + g[1] = imageLoad(_g1, ivec2(gl_GlobalInvocationID.xy)); + vec3 albedo = g[1].xyz; // create a ray from the uv Ray ray = createCameraRay(uv); @@ -54,13 +62,14 @@ void main() firstHit.position = ray.origin+ray.direction*depth; firstHit.distance = depth; firstHit.normal = normal; - firstHit.albedo = vec3(1.0,1.0,1.0); + firstHit.albedo = albedo; - // do a trace using precomputed depth and surface normal values - //RayHit hit = trace(ray); + int sky = depth >= INF ? 1 : 0; + int bounces = (1-sky) * BOUNCES; + pixel.xyz = mix(pixel.xyz, _skyColor, sky); // trace the rays path around the scene - for (int j = 1; j < BOUNCES; j++) + for (int j = 0; j < bounces; j++) { RayHit hit = trace(ray); @@ -69,10 +78,11 @@ void main() if (length(ray.energy) < 0.001) break; } - //pixel.xyz = mix(pixel.xyz, normal, 1.0-depth); pixel.xyz = mix(pixel.xyz, vec3(1.0), depth); + //pixel.xyz = mix(firstHit.albedo, pixel.xyz, depth); + //pixel.a = 1.0; //pixel.xyz = texture(_g0, uv).xyz; diff --git a/shader/root/rtpre.glsl b/shader/root/rtpre.glsl index dd003f9..53fb86d 100644 --- a/shader/root/rtpre.glsl +++ b/shader/root/rtpre.glsl @@ -5,7 +5,8 @@ // local work group layout(local_size_x = 1, local_size_y = 1) in; -layout (rgba32f, binding = 2) uniform image2D g0_output; +layout (rgba32f, binding = 2) writeonly uniform image2D g0_output; +layout (rgba32f, binding = 3) writeonly uniform image2D g1_output; #include func.glsl #include constants.glsl @@ -35,7 +36,7 @@ void main() // n roughly correlates to steepness of log curve // TODO: what does this mean in mathematical terms?? - float n = 1.0; + float n = 4; float f = INF; float z = hit.distance; @@ -44,11 +45,15 @@ void main() //depth = z/f; // logarithmic depth - depth = log(z*pow(E,n)/f)/n; + depth = max(0,log(z*pow(E,n)/f)/n); // pack normal into texture pixel.xyz = hit.normal*0.5+0.5; pixel.w = depth; imageStore(g0_output, pixelCoords, pixel); + + pixel.xyz = hit.albedo; + pixel.w = 0; + imageStore(g1_output, pixelCoords, pixel); } diff --git a/src/gfx.c b/src/gfx.c index 3346542..a376fa6 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -200,7 +200,8 @@ int createTextures(int width, int height, struct Shaders shaders, struct Texture // create a texture for the compute shader to write to textures->target = createWriteOnlyTexture(width, height); - textures->g0 = createTexture(width, height); + textures->g0 = createTextureUnit(width, height, 2); + textures->g1 = createTextureUnit(width, height, 3); return 0; } @@ -256,11 +257,10 @@ GLuint createWriteOnlyTexture(int width, int height) } // creates an empty texture in GL_TEXTURE2 unit -GLuint createTexture(int width, int height) +GLuint createTextureUnit(int width, int height, int unit) { GLuint texture; glGenTextures(1, &texture); - //glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -268,7 +268,7 @@ GLuint createTexture(int width, int height) // an empty image glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL); - glBindImageTexture(2, texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); + glBindImageTexture(unit, texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); return texture; } diff --git a/src/gfx.h b/src/gfx.h index 78e2827..db364e7 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -25,13 +25,19 @@ struct Textures // noise texture to help with randomness GLuint noise; + // TODO: replace these with an actual array! // additional view space information // - // x depth - // y - - // z - - // w - + // x surface normal x + // y surface normal y + // z surface normal z + // w depth GLuint g0; + // x albedo x + // y albedo y + // z albedo z + // w - + GLuint g1; }; SDL_Window* gfxInit(int width, int height); @@ -42,7 +48,7 @@ GLuint compileComputeShaderProgram(const char* csPath); int createTextures(int width, int height, struct Shaders shaders, struct Textures* textures); GLuint createNoiseTexture(int width, int height); -GLuint createTexture(int width, int height); +GLuint createTextureUnit(int width, int height, int unit); GLuint createWriteOnlyTexture(int width, int height); GLuint compileShader(const char* path, GLenum type); diff --git a/src/main.c b/src/main.c index 948666a..14140a1 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ int main() { initialise(); + float start = now(); int frames; for (frames = 0; !checkQuit(); frames++) { @@ -77,8 +78,8 @@ int main() SDL_GL_SwapWindow(window); } - float elapsed = now(); - printf("%d frames in %f seconds (avg: %f fps)\n", + float elapsed = now()-start; + printf("%d frames in %fs [%f fps]\n", frames, elapsed, (float)frames/elapsed);