store first hit albedo

This commit is contained in:
ktyl 2021-08-13 20:03:19 +01:00
parent 9b61093de4
commit c6ccf6f14b
6 changed files with 47 additions and 26 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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);