Browse Source

Optimization on shaders (get view position from depth buffer)

Panagiotis Christopoulos Charitos 13 năm trước cách đây
mục cha
commit
55050eafb7
3 tập tin đã thay đổi với 12 bổ sung10 xóa
  1. 3 3
      shaders/IsLpGeneric.glsl
  2. 1 1
      shaders/PpsSsao.glsl
  3. 8 6
      src/renderer/Renderer.cpp

+ 3 - 3
shaders/IsLpGeneric.glsl

@@ -102,12 +102,12 @@ vec3 getFragPosVSpace()
 	const float depth = texture(msDepthFai, vTexCoords).r;
 
 	vec3 fragPosVspace;
+	/// XXX OPT: Why negative planes.y?
 	fragPosVspace.z = -planes.y / (planes.x + depth);
 
+	/// XXX OPT: Do that a varying
 	fragPosVspace.xy = (vTexCoords * limitsOfNearPlane2) - limitsOfNearPlane;
-
-	const float sc = -fragPosVspace.z / zNear;
-	fragPosVspace.xy *= sc;
+	fragPosVspace.xy *= -fragPosVspace.z;
 
 	return fragPosVspace;
 }

+ 1 - 1
shaders/PpsSsao.glsl

@@ -70,7 +70,7 @@ vec3 getPosition(in vec2 uv)
 	
 	fragPosVspace.xy = (uv * limitsOfNearPlane2) - limitsOfNearPlane;
 	
-	const float sc = -fragPosVspace.z / zNear;
+	const float sc = -fragPosVspace.z;
 	fragPosVspace.xy *= sc;
 
 	return fragPosVspace;

+ 8 - 6
src/renderer/Renderer.cpp

@@ -174,19 +174,21 @@ void Renderer::createFai(U32 width, U32 height, int internalFormat,
 //==============================================================================
 void Renderer::calcPlanes(const Vec2& cameraRange, Vec2& planes)
 {
-	float zNear = cameraRange.x();
-	float zFar = cameraRange.y();
+	F32 zNear = cameraRange.x();
+	F32 zFar = cameraRange.y();
 
-	planes.x() = zFar / (zNear - zFar);
-	planes.y() = (zFar * zNear) / (zNear -zFar);
+	F32 opt = zNear - zFar;
+
+	planes.x() = zFar / opt;
+	planes.y() = (zFar * zNear) / opt;
 }
 
 //==============================================================================
 void Renderer::calcLimitsOfNearPlane(const PerspectiveCamera& pcam,
 	Vec2& limitsOfNearPlane)
 {
-	limitsOfNearPlane.y() = pcam.getNear() * tan(0.5 * pcam.getFovY());
-	limitsOfNearPlane.x() = pcam.getNear() * tan(0.5 * pcam.getFovX());
+	limitsOfNearPlane.y() = tan(0.5 * pcam.getFovY());
+	limitsOfNearPlane.x() = tan(0.5 * pcam.getFovX());
 }