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 PI = 3.14159;
const float E = 2.71828; const float E = 2.71828;
const int SAMPLES = 1; const int BOUNCES = 4;
const int BOUNCES = 8;

View File

@ -4,10 +4,13 @@ layout(local_size_x = 1, local_size_y = 1) in; // size of local
// gbuffer? // gbuffer?
layout(rgba32f, binding = 2) readonly uniform image2D _g0; layout(rgba32f, binding = 2) readonly uniform image2D _g0;
layout(rgba32f, binding = 3) readonly uniform image2D _g1;
// final output // final output
layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture 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: // TODO: some of these depend on each other!! need be be in this order for now c:
#include func.glsl #include func.glsl
#include constants.glsl #include constants.glsl
@ -30,7 +33,7 @@ vec3 shade(inout Ray ray, RayHit hit)
} }
// sky color // sky color
return vec3(0.68,0.85,0.9); return _skyColor;
} }
void main() void main()
@ -43,10 +46,15 @@ void main()
vec2 uv = pixelUv(pixelCoords, dims); vec2 uv = pixelUv(pixelCoords, dims);
vec4 g[2];
// load data from first pass // load data from first pass
vec4 d = imageLoad(_g0, ivec2(gl_GlobalInvocationID.xy)); g[0] = imageLoad(_g0, ivec2(gl_GlobalInvocationID.xy));
float depth = d.w; float depth = g[0].w;
vec3 normal = d.xyz*2.0-1.0; // unpack normal packaged into texture 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 // create a ray from the uv
Ray ray = createCameraRay(uv); Ray ray = createCameraRay(uv);
@ -54,13 +62,14 @@ void main()
firstHit.position = ray.origin+ray.direction*depth; firstHit.position = ray.origin+ray.direction*depth;
firstHit.distance = depth; firstHit.distance = depth;
firstHit.normal = normal; 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 int sky = depth >= INF ? 1 : 0;
//RayHit hit = trace(ray); int bounces = (1-sky) * BOUNCES;
pixel.xyz = mix(pixel.xyz, _skyColor, sky);
// trace the rays path around the scene // 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); RayHit hit = trace(ray);
@ -69,10 +78,11 @@ void main()
if (length(ray.energy) < 0.001) break; if (length(ray.energy) < 0.001) break;
} }
//pixel.xyz = mix(pixel.xyz, normal, 1.0-depth); //pixel.xyz = mix(pixel.xyz, normal, 1.0-depth);
pixel.xyz = mix(pixel.xyz, vec3(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.a = 1.0;
//pixel.xyz = texture(_g0, uv).xyz; //pixel.xyz = texture(_g0, uv).xyz;

View File

@ -5,7 +5,8 @@
// local work group // local work group
layout(local_size_x = 1, local_size_y = 1) in; 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 func.glsl
#include constants.glsl #include constants.glsl
@ -35,7 +36,7 @@ void main()
// n roughly correlates to steepness of log curve // n roughly correlates to steepness of log curve
// TODO: what does this mean in mathematical terms?? // TODO: what does this mean in mathematical terms??
float n = 1.0; float n = 4;
float f = INF; float f = INF;
float z = hit.distance; float z = hit.distance;
@ -44,11 +45,15 @@ void main()
//depth = z/f; //depth = z/f;
// logarithmic depth // logarithmic depth
depth = log(z*pow(E,n)/f)/n; depth = max(0,log(z*pow(E,n)/f)/n);
// pack normal into texture // pack normal into texture
pixel.xyz = hit.normal*0.5+0.5; pixel.xyz = hit.normal*0.5+0.5;
pixel.w = depth; pixel.w = depth;
imageStore(g0_output, pixelCoords, pixel); 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 // create a texture for the compute shader to write to
textures->target = createWriteOnlyTexture(width, height); textures->target = createWriteOnlyTexture(width, height);
textures->g0 = createTexture(width, height); textures->g0 = createTextureUnit(width, height, 2);
textures->g1 = createTextureUnit(width, height, 3);
return 0; return 0;
} }
@ -256,11 +257,10 @@ GLuint createWriteOnlyTexture(int width, int height)
} }
// creates an empty texture in GL_TEXTURE2 unit // creates an empty texture in GL_TEXTURE2 unit
GLuint createTexture(int width, int height) GLuint createTextureUnit(int width, int height, int unit)
{ {
GLuint texture; GLuint texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -268,7 +268,7 @@ GLuint createTexture(int width, int height)
// an empty image // an empty image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL); 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; return texture;
} }

View File

@ -25,13 +25,19 @@ struct Textures
// noise texture to help with randomness // noise texture to help with randomness
GLuint noise; GLuint noise;
// TODO: replace these with an actual array!
// additional view space information // additional view space information
// //
// x depth // x surface normal x
// y - // y surface normal y
// z - // z surface normal z
// w - // w depth
GLuint g0; GLuint g0;
// x albedo x
// y albedo y
// z albedo z
// w -
GLuint g1;
}; };
SDL_Window* gfxInit(int width, int height); 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); int createTextures(int width, int height, struct Shaders shaders, struct Textures* textures);
GLuint createNoiseTexture(int width, int height); 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 createWriteOnlyTexture(int width, int height);
GLuint compileShader(const char* path, GLenum type); GLuint compileShader(const char* path, GLenum type);

View File

@ -36,6 +36,7 @@ int main()
{ {
initialise(); initialise();
float start = now();
int frames; int frames;
for (frames = 0; !checkQuit(); frames++) for (frames = 0; !checkQuit(); frames++)
{ {
@ -77,8 +78,8 @@ int main()
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
} }
float elapsed = now(); float elapsed = now()-start;
printf("%d frames in %f seconds (avg: %f fps)\n", printf("%d frames in %fs [%f fps]\n",
frames, frames,
elapsed, elapsed,
(float)frames/elapsed); (float)frames/elapsed);