Переглянути джерело

Improving HDR (maybe) (needs testing)

Panagiotis Christopoulos Charitos 15 роки тому
батько
коміт
212c87b235

+ 80 - 0
shaders/GaussianBlurGeneric.glsl

@@ -0,0 +1,80 @@
+/**
+ * @file
+ * Generic shader program for Gausian blur
+ * Switches: VPASS or HPASS, COL_RGBA or COL_RGB or COL_R
+ */
+
+#pragma anki vertShaderBegins
+
+#pragma anki attribute position 0
+attribute vec2 position;
+
+void main()
+{
+	gl_Position = vec4(position * 2.0 - 1.0, 0.0, 1.0);
+}
+
+#pragma anki fragShaderBegins
+
+
+/*
+ * Preprocessor switcher sanity checks
+ */
+#if !defined(VPASS) && !defined(HPASS)
+	#error "See file"
+#endif
+
+#if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
+	#error "See file"
+#endif
+
+
+uniform sampler2D img; ///< Input FAI
+uniform vec2 imgSize = vec2(100.0, 100.0);
+
+varying vec2 texCoords;
+
+
+/*
+ * Determin color type
+ */
+#if defined(COL_RGBA)
+	#define COL_TYPE vec4
+#elif defined(COL_RGB)
+	#define COL_TYPE vec3
+#elif defined(COL_R)
+	#define COL_TYPE float
+#endif
+
+/*
+ * Determin tex fetch
+ */
+#if defined(COL_RGBA)
+	#define TEX_FETCH rgba
+#elif defined(COL_RGB)
+	#define TEX_FETCH rgb
+#elif defined(COL_R)
+	#define TEX_FETCH r
+#endif
+
+
+void main()
+{
+	const float _offset_[3] = float[](0.0, 1.3846153846, 3.2307692308);
+	const float _weight_[3] = float[](0.2255859375, 0.314208984375, 0.06982421875);
+
+	COL_TYPE _col_ = texture2D(img, gl_FragCoord.xy / imgSize).TEX_FETCH * weight[0];
+
+	for(int i=1; i<3; i++)
+	{
+		#if defined(HPASS)
+			_col_ += texture2D(img, (gl_FragCoord.xy + vec2(0.0, offset[i])) / imgSize.x).TEX_FETCH * weight[i];
+			_col_ += texture2D(img, (gl_FragCoord.xy - vec2(0.0, offset[i])) / imgSize.x).TEX_FETCH * weight[i];
+		#elif defined(VPASS)
+			_col_ += texture2D(img, (gl_FragCoord.xy + vec2(offset[i], 0.0)) / imgSize.y).TEX_FETCH * weight[i];
+			_col_ += texture2D(img, (gl_FragCoord.xy - vec2(offset[i], 0.0)) / imgSize.y).TEX_FETCH * weight[i];
+		#endif
+	}
+
+	gl_FragColor[0].TEX_FETCH = _col_;
+}

+ 5 - 5
shaders/IsLpGeneric.glsl

@@ -122,7 +122,7 @@ float pcfLow(in vec3 _shadowUv_)
 		_shadowCol_ += shadow2D(shadowMap, _uvCoord_).r;
 	}
 	
-	_shadowCol_ *= 1.0/9.0;
+	_shadowCol_ *= (1.0/9.0);
 	return _shadowCol_;
 }
 
@@ -203,12 +203,12 @@ void main()
 	 */
 	#if defined(POINT_LIGHT_ENABLED)
 		/*
-		 * The func phong calculates the frag to light distance (_fragLightDist_) and be cause we need that distance
+		 * The func doPhong calculates the frag to light distance (_fragLightDist_) and be cause we need that distance
 		 * latter for other calculations we export it
 		 */
 		float _fragLightDist_;
 		vec3 _color_ = doPhong(fragPosVspace, _fragLightDist_);
-		gl_FragData[0] = vec4(_color_ * getAttenuation(_fragLightDist_), 1.0);
+		gl_FragData[0].rgb = _color_ * getAttenuation(_fragLightDist_);
 
 	/*
 	 * Spot light
@@ -245,9 +245,9 @@ void main()
 			float _att_ = getAttenuation(_fragLightDist_);
 
 			#if defined(SHADOW_ENABLED)
-				gl_FragData[0] = vec4(_lightTexCol_ * _color_ * (_shadowCol_ * _att_), 1.0);
+				gl_FragData[0].rgb = _lightTexCol_ * _color_ * (_shadowCol_ * _att_);
 			#else
-				gl_FragData[0] = vec4(_lightTexCol_ * _color_ * _att_, 1.0);
+				gl_FragData[0].rgb = _lightTexCol_ * _color_ * _att_;
 			#endif
 		}
 		else

+ 43 - 47
shaders/MsMpGeneric.glsl

@@ -124,8 +124,8 @@ void main()
 /**
  * Note: The process of calculating the diffuse color for the diffuse MSFAI is divided into two parts. The first happens
  * before the normal calculation and the other just after it. In the first part we read the texture (or the gl_Color)
- * and we set the _diff_color. In case of grass we discard. In the second part we calculate a SEM color and we combine
- * it with the _diff_color. We cannot put the second part before normal calculation because SEM needs the newNormal.
+ * and we set the _diffColl_. In case of grass we discard. In the second part we calculate a SEM color and we combine
+ * it with the _diffColl_. We cannot put the second part before normal calculation because SEM needs the _normal_.
  * Also we cannot put the first part after normal calculation because in case of grass we will waste calculations for
  * the normal. For that two reasons we split the diffuse calculations in two parts
  */
@@ -180,53 +180,53 @@ void main()
 		float _h = texture2D(heightMap, vTexCoords).r;
 		float _height = _scale * _h - _bias;
 
-		vec2 superTexCoords_v2f = _height * _norm_eye.xy + vTexCoords;*/
+		vec2 _superTexCoords__v2f = _height * _norm_eye.xy + vTexCoords;*/
 
-		vec2 superTexCoords = vTexCoords;
+		vec2 _superTexCoords_ = vTexCoords;
 		const float maxStepCount = 100.0;
-		float nSteps = maxStepCount * length(superTexCoords);
+		float nSteps = maxStepCount * length(_superTexCoords_);
 
 		vec3 dir = vVertPosViewSpace;
 		dir.xy /= 8.0;
 		dir /= -nSteps * dir.z;
 
-		float diff0, diff1 = 1.0 - texture2D(heightMap, superTexCoords).a;
+		float diff0, diff1 = 1.0 - texture2D(heightMap, _superTexCoords_).a;
 		if(diff1 > 0.0)
 		{
 			do 
 			{
-				superTexCoords += dir.xy;
+				_superTexCoords_ += dir.xy;
 
 				diff0 = diff1;
-				diff1 = texture2D(heightMap, superTexCoords).w;
+				diff1 = texture2D(heightMap, _superTexCoords_).w;
 			} while(diff1 > 0.0);
 
-			superTexCoords.xy += (diff1 / (diff0 - diff1)) * dir.xy;
+			_superTexCoords_.xy += (diff1 / (diff0 - diff1)) * dir.xy;
 		}
 	#else
-		#define superTexCoords vTexCoords
+		#define _superTexCoords_ vTexCoords
 	#endif
 
 
 	/*
 	 * Diffuse Calculations (Part I)
-	 * Get the color from the diffuse map and discard if alpha is zero
+	 * Get the color from the diffuse map and discard if alpha testing is on and alpha is zero
 	 */
-	vec3 _diff_color;
+	vec3 _diffColl_;
 	#if defined(DIFFUSE_MAPPING)
 
 		#if defined(ALPHA_TESTING)
-			vec4 _diff_color4 = texture2D(diffuseMap, superTexCoords);
-			if(_diff_color4.a == 0.0)
+			vec4 _diffCol4_ = texture2D(diffuseMap, _superTexCoords_);
+			if(_diffCol4_.a == 0.0)
 				discard;
-			_diff_color = _diff_color4.rgb;
-		#else
-			_diff_color = texture2D(diffuseMap, superTexCoords).rgb;
+			_diffColl_ = _diffCol4_.rgb;
+		#else // no alpha
+			_diffColl_ = texture2D(diffuseMap, _superTexCoords_).rgb;
 		#endif
 
-		_diff_color *= diffuseCol.rgb;
-	#else
-		_diff_color = diffuseCol.rgb;
+		_diffColl_ *= diffuseCol.rgb;
+	#else // no diff mapping
+		_diffColl_ = diffuseCol.rgb;
 	#endif
 
 
@@ -235,61 +235,57 @@ void main()
 	 * Either use a normap map and make some calculations or use the vertex normal
 	 */
 	#if defined(NORMAL_MAPPING)
-		vec3 _n = normalize(vNormal);
-		vec3 _t = normalize(vTangent);
-		vec3 _b = cross(_n, _t) * vTangentW;
+		vec3 _n_ = normalize(vNormal);
+		vec3 _t_ = normalize(vTangent);
+		vec3 _b_ = cross(_n_, _t_) * vTangentW;
 
-		mat3 tbnMat = mat3(_t,_b,_n);
+		mat3 _tbnMat_ = mat3(_t_, _b_, _n_);
 
-		vec3 nAtTangentspace = (texture2D(normalMap, superTexCoords).rgb - 0.5) * 2.0;
+		vec3 _nAtTangentspace_ = (texture2D(normalMap, _superTexCoords_).rgb - 0.5) * 2.0;
 
-		vec3 newNormal = normalize(tbnMat * nAtTangentspace);
+		vec3 _normal_ = normalize(_tbnMat_ * _nAtTangentspace_);
 	#else
-		vec3 newNormal = normalize(vNormal);
+		vec3 _normal_ = normalize(vNormal);
 	#endif
 
 
 	/*
 	 * Diffuse Calculations (Part II)
-	 * If SEM is enabled make some calculations and combine colors of SEM and the _diff_color
+	 * If SEM is enabled make some calculations (using the vVertPosViewSpace, environmentMap and the _normal_) and combine
+	 * colors of SEM and the _diffColl_
 	 */
-	// if SEM enabled make some aditional calculations using the vVertPosViewSpace, environmentMap and the newNormal
 	#if defined(ENVIRONMENT_MAPPING)
-		vec3 _u = normalize(vVertPosViewSpace);
-		
 		/*
 		 * In case of normal mapping I could play with vertex's normal but this gives better results and its allready
 		 * computed
 		 */
-		vec3 _r = reflect(_u, newNormal); 
-		
-		_r.z += 1.0;
-		float _m = 2.0 * length(_r);
-		vec2 _sem_texCoords = _r.xy/_m + 0.5;
-
-		vec3 _sem_col = texture2D(environmentMap, _sem_texCoords).rgb;
-		_diff_color = _diff_color + _sem_col; // blend existing color with the SEM texture map
+		vec3 _u_ = normalize(vVertPosViewSpace);
+		vec3 _r_ = reflect(_u_, _normal_);
+		_r_.z += 1.0;
+		float _m_ = 2.0 * length(_r_);
+		vec2 _semTexCoords_ = _r_.xy / _m_ + 0.5;
+
+		vec3 _semCol_ = texture2D(environmentMap, _semTexCoords_).rgb;
+		_diffColl_ += _semCol_; // blend existing color with the SEM texture map
 	#endif
 
 
 	/*
 	 * Specular Calculations
 	 */
-	// has specular map
 	#if defined(SPECULAR_MAPPING)
-		vec4 _specular = vec4(texture2D(specularMap, superTexCoords).rgb * specularCol, shininess);
-	// no specular map
-	#else
-		vec4 _specular = vec4(specularCol, shininess);
+		vec4 _specularCol_ = vec4(texture2D(specularMap, _superTexCoords_).rgb * specularCol, shininess);
+	#else // no specular map
+		vec4 _specularCol_ = vec4(specularCol, shininess);
 	#endif
 
 
 	/*
 	 * Final Stage. Write all data
 	 */
-	gl_FragData[0].rg = packNormal(newNormal);
-	gl_FragData[1].rgb = _diff_color;
-	gl_FragData[2] = _specular;
+	gl_FragData[0].rg = packNormal(_normal_);
+	gl_FragData[1].rgb = _diffColl_;
+	gl_FragData[2] = _specularCol_;
 
 	/*#if defined(HARDWARE_SKINNING)
 		gl_FragData[1] = gl_Color;

+ 8 - 52
shaders/PpsHdr.glsl

@@ -10,61 +10,17 @@
 #pragma anki fragShaderBegins
 
 uniform sampler2D fai; ///< Its the IS FAI
+
 varying vec2 texCoords;
 
 
 void main()
 {
-	//gl_FragData[0].rgb = texture2D(fai, texCoords).rgb;return;
-
-	/*if(texCoords.x*R_W > textureSize(fai,0).x/2)
-		gl_FragData[0].rgb = vec3(1, 0, 0);
-	else
-		gl_FragData[0].rgb = vec3(0, 1, 0);
-	return;*/
-
-	#if defined(_PPS_HDR_PASS_0_) || defined(_PPS_HDR_PASS_1_)
-		vec3 color = texture2D(fai, texCoords).rgb;
-		//float luminance = dot(vec3(0.30, 0.59, 0.11), color);
-
-		const float additionalOffset = 2.0;
-
-		#if defined(_PPS_HDR_PASS_0_)
-			const float offset = 1.0 / IS_FAI_WIDTH * additionalOffset;
-		#else
-			const float offset = 1.0 / PASS0_HEIGHT * additionalOffset;
-		#endif
-
-		const int SAMPLES_NUM = 6;
-		const float kernel[] = float[](-1.0 * offset, 1.0 * offset, -2.0 * offset, 2.0 * offset,
-				                            -3.0 * offset, 3.0 * offset, -4.0 * offset, 4.0 * offset,
-				                            -5.0 * offset, 5.0 * offset, -6.0 * offset, 6.0 * offset);
-
-
-		/*const float _offset_[3] = float[](0.0, 1.3846153846, 3.2307692308);
-		const float _weight_[3] = float[](0.2255859375, 0.314208984375, 0.06982421875);*/
-
-
-		for(int i=0; i<SAMPLES_NUM; i++)
-		{
-			#if defined(_PPS_HDR_PASS_0_)
-				color += texture2D(fai, texCoords + vec2(kernel[i], 0.0)).rgb;
-			#else // _PPS_HDR_PASS_1_
-				color += texture2D(fai, texCoords + vec2(0.0, kernel[i])).rgb;
-			#endif
-		}
-
-		const float denominator = 1.0 / (SAMPLES_NUM + 1); // Opt to make sure its const
-		gl_FragData[0].rgb = color * denominator;
-	#else // _PPS_HDR_PASS_2_
-		//vec3 color = MedianFilterRGB(fai, texCoords);
-		vec3 color = texture2D(fai, texCoords).rgb;
-
-		float Y = dot(vec3(0.30, 0.59, 0.11), color); // AKA luminance
-		const float exposure = 4.0;
-		const float brightMax = 4.0;
-		float YD = exposure * (exposure/brightMax + 1.0) / (exposure + 1.0) * Y;
-		color *= YD;
-		gl_FragData[0].rgb = color;
-	#endif
+	vec3 _color_ = texture2D(fai, texCoords).rgb;
+	float _luminance_ = dot(vec3(0.30, 0.59, 0.11), _color_); // AKA luminance
+	const float _exposure_ = 4.0;
+	const float _brightMax_ = 4.0;
+	float _yd_ = _exposure_ * (_exposure_/_brightMax_ + 1.0) / (_exposure_ + 1.0) * _luminance_;
+	_color_ *= _yd_;
+	gl_FragData[0].rgb = _color_;
 }

+ 12 - 15
src/Renderer/Hdr.cpp

@@ -46,7 +46,7 @@ void Renderer::Pps::Hdr::initFbos(Fbo& fbo, Texture& fai, int internalFormat)
 void Renderer::Pps::Hdr::init()
 {
 	//int width = renderingQuality * r.width;
-	int height = renderingQuality * r.height;
+	//int height = renderingQuality * r.height;
 
 	initFbos(pass0Fbo, pass0Fai, GL_RGB);
 	initFbos(pass1Fbo, pass1Fai, GL_RGB);
@@ -54,25 +54,22 @@ void Renderer::Pps::Hdr::init()
 
 	fai.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
-	// init shaders
-	const char* shaderFname = "shaders/PpsHdr.glsl";
-	string pps;
-	string prefix;
 
-	pps = "#define _PPS_HDR_PASS_0_\n#define IS_FAI_WIDTH " + lexical_cast<string>(r.width) + "\n";
-	prefix = "Pass0IsFaiWidth" + lexical_cast<string>(r.width);
-	pass0SProg.loadRsrc(ShaderProg::createSrcCodeToCache(shaderFname, pps.c_str(), prefix.c_str()).c_str());
+	// init shaders
+	pass0SProg.loadRsrc("shaders/PpsHdr.glsl");
 	pass0SProgFaiUniVar = pass0SProg->findUniVar("fai");
 
-	pps = "#define _PPS_HDR_PASS_1_\n#define PASS0_HEIGHT " + lexical_cast<string>(height) + "\n";
-	prefix = "Pass1Pass0Height" + lexical_cast<string>(height);
+	const char* shaderFname = "shaders/GaussianBlurGeneric.glsl";
+
+	string pps = "#define HPASS\n#define COL_RGB";
+	string prefix = "HRGB";
 	pass1SProg.loadRsrc(ShaderProg::createSrcCodeToCache(shaderFname, pps.c_str(), prefix.c_str()).c_str());
-	pass1SProgFaiUniVar = pass1SProg->findUniVar("fai");
+	pass1SProgFaiUniVar = pass1SProg->findUniVar("img");
 
-	pps = "#define _PPS_HDR_PASS_2_\n";
-	prefix = "Pass2";
-	pass2SProg.loadRsrc(ShaderProg::createSrcCodeToCache(shaderFname, pps.c_str(), prefix.c_str()).c_str());
-	pass2SProgFaiUniVar = pass2SProg->findUniVar("fai");
+	pps = "#define VPASS\n#define COL_RGB";
+	prefix = "VRGB";
+	pass1SProg.loadRsrc(ShaderProg::createSrcCodeToCache(shaderFname, pps.c_str(), prefix.c_str()).c_str());
+	pass1SProgFaiUniVar = pass1SProg->findUniVar("img");
 }