Ver Fonte

Improving tonemapping

Panagiotis Christopoulos Charitos há 10 anos atrás
pai
commit
7e3175cf00
3 ficheiros alterados com 19 adições e 14 exclusões
  1. 13 9
      shaders/PpsTmAverageLuminance.comp.glsl
  2. 5 4
      shaders/Tonemapping.glsl
  3. 1 1
      testapp/Main.cpp

+ 13 - 9
shaders/PpsTmAverageLuminance.comp.glsl

@@ -16,8 +16,8 @@ const uint WORKGROUP_SIZE_Y = 16u;
 const uint WORKGROUP_SIZE = WORKGROUP_SIZE_X * WORKGROUP_SIZE_Y;
 
 layout(
-	local_size_x = WORKGROUP_SIZE_X, 
-	local_size_y = WORKGROUP_SIZE_Y, 
+	local_size_x = WORKGROUP_SIZE_X,
+	local_size_y = WORKGROUP_SIZE_Y,
 	local_size_z = 1) in;
 
 const uint MIPMAP_WIDTH = ANKI_RENDERER_WIDTH / (2u << (IS_RT_MIPMAP - 1u));
@@ -42,17 +42,18 @@ void main()
 	uint yStart = gl_LocalInvocationID.y * PIXEL_READ_Y;
 	uint xStart = gl_LocalInvocationID.x * PIXEL_READ_X;
 	for(uint y = 0; y < PIXEL_READ_Y; ++y)
-	{		
+	{
 		for(uint x = 0; x < PIXEL_READ_X; ++x)
 		{
 			vec3 color = texelFetchOffset(
 				u_isRt, ivec2(xStart, yStart), IS_RT_MIPMAP, ivec2(x, y)).rgb;
 			float lum = computeLuminance(color);
-			avgLum += log(lum);
+			//avgLum += log(lum);
+			avgLum += lum / float(MIPMAP_WIDTH * MIPMAP_HEIGHT);
 		}
 	}
 
-	avgLum *= 1.0 / float(PIXEL_READ_X * PIXEL_READ_Y);
+	//avgLum *= 1.0 / float(PIXEL_READ_X * PIXEL_READ_Y);
 	g_avgLum[gl_LocalInvocationIndex] = avgLum;
 
 	memoryBarrierShared();
@@ -63,7 +64,7 @@ void main()
 	{
 		if(gl_LocalInvocationIndex < s)
 		{
-			g_avgLum[gl_LocalInvocationIndex] += 
+			g_avgLum[gl_LocalInvocationIndex] +=
 				g_avgLum[gl_LocalInvocationIndex + s];
 		}
 
@@ -74,13 +75,16 @@ void main()
 	// Write the result
 	if(gl_LocalInvocationIndex == 0)
 	{
-		float crntLum = exp(g_avgLum[0] / float(WORKGROUP_SIZE));
-#if 0
+		float crntLum = g_avgLum[0];
+		//crntLum = exp(crntLum / float(WORKGROUP_SIZE));
+		crntLum = max(crntLum, 0.04);
+
+#if 1
 		float prevLum = u_averageLuminancePad3.x;
 
 		// Lerp between previous and new L value
 		const float INTERPOLATION_FACTOR = 0.05;
-		u_averageLuminancePad3.x = prevLum * (1.0 - INTERPOLATION_FACTOR) 
+		u_averageLuminancePad3.x = prevLum * (1.0 - INTERPOLATION_FACTOR)
 			+ crntLum * INTERPOLATION_FACTOR;
 #else
 		u_averageLuminancePad3.x = crntLum;

+ 5 - 4
shaders/Tonemapping.glsl

@@ -23,7 +23,7 @@ vec3 computeExposedColor(in vec3 color, in float avgLum, in float threshold)
 {
 	float keyValue = 1.03 - (2.0 / (2.0 + log10(avgLum + 1.0)));
 	float linearExposure = (keyValue / avgLum);
-	float exposure = log2(max(EPSILON, linearExposure));
+	float exposure = log2(linearExposure);
 
 	exposure -= threshold;
 	return exp2(exposure) * color;
@@ -32,9 +32,9 @@ vec3 computeExposedColor(in vec3 color, in float avgLum, in float threshold)
 // Reinhard operator
 vec3 tonemapReinhard(in vec3 color, in float saturation)
 {
-	float lum = computeLuminance(color);   
+	float lum = computeLuminance(color);
 	float toneMappedLuminance = lum / (lum + 1.0);
-	return toneMappedLuminance * pow(color / lum, vec3(saturation));   
+	return toneMappedLuminance * pow(color / lum, vec3(saturation));
 }
 
 // Uncharted 2 operator
@@ -47,7 +47,7 @@ vec3 tonemapUncharted2(in vec3 color)
 	const float E = 0.02;
 	const float F = 0.30;
 
-	return ((color * (A * color + C * B) + D * E) 
+	return ((color * (A * color + C * B) + D * E)
 		/ (color * (A * color + B) + D * F)) - E / F;
 }
 
@@ -55,6 +55,7 @@ vec3 tonemap(in vec3 color, in float avgLum, in float threshold)
 {
 	vec3 c = computeExposedColor(color, avgLum, threshold);
 	return tonemapReinhard(c, 1.0);
+	//return tonemapUncharted2(c);
 }
 
 #endif

+ 1 - 1
testapp/Main.cpp

@@ -61,7 +61,7 @@ Error init()
 	MainRenderer& renderer = app->getMainRenderer();
 	ResourceManager& resources = app->getResourceManager();
 
-	scene.setAmbientColor(Vec4(1.0) * 0.1);
+	scene.setAmbientColor(Vec4(1.0) * 0.0001);
 
 	if(getenv("PROFILE"))
 	{