Просмотр исходного кода

Finalizing IS for point lights and optimizing the IS shader

Panagiotis Christopoulos Charitos 13 лет назад
Родитель
Сommit
4851cd5cc5
4 измененных файлов с 61 добавлено и 41 удалено
  1. 1 0
      include/anki/math/Vec4.h
  2. 9 0
      include/anki/math/Vec4.inl.h
  3. 38 26
      shaders/IsLpGeneric.glsl
  4. 13 15
      src/renderer/Is.cpp

+ 1 - 0
include/anki/math/Vec4.h

@@ -20,6 +20,7 @@ public:
 	explicit Vec4(const float f);
 	explicit Vec4(const float arr[]);
 	explicit Vec4(const Vec2& v2, const float z, const float w);
+	explicit Vec4(const Vec2& av2, const Vec2& bv2);
 	explicit Vec4(const Vec3& v3, const float w);
 	Vec4(const Vec4& b);
 	explicit Vec4(const Quat& q);

+ 9 - 0
include/anki/math/Vec4.inl.h

@@ -62,6 +62,15 @@ inline Vec4::Vec4(const Vec2& v2, const float z_, const float w_)
 	w() = w_;
 }
 
+// vec2, vec2
+inline Vec4::Vec4(const Vec2& av2, const Vec2& bv2)
+{
+	x() = av2.x();
+	y() = av2.y();
+	z() = bv2.x();
+	w() = bv2.y();
+}
+
 // vec3, float
 inline Vec4::Vec4(const Vec3& v3, const float w_)
 {

+ 38 - 26
shaders/IsLpGeneric.glsl

@@ -10,7 +10,7 @@
 
 #pragma anki include "shaders/Pack.glsl"
 
-#define DISCARD 1
+#define DISCARD 0
 
 /// @name Uniforms
 /// @{
@@ -18,20 +18,34 @@
 /// Watch the placement
 layout(std140) uniform uniforms
 {
-	uniform vec2 planes; ///< for the calculation of frag pos in view space
-	/// for the calculation of frag pos in view space
-	uniform vec2 limitsOfNearPlane; 
-	/// This is an optimization see PpsSsao.glsl and r403 for the clean one
-	uniform vec2 limitsOfNearPlane2; 
-	uniform float zNear; ///< for the calculation of frag pos in view space
-	uniform float lightRadius;
-	uniform float shadowMapSize;
-	uniform vec3 lightPos; ///< Light pos in eye space
-	uniform vec3 lightDiffuseCol;
-	uniform vec3 lightSpecularCol;
+	/// For the calculation of frag pos in view space. Only the .xy is used
+	uniform vec4 planes_;
+
+	/// For the calculation of frag pos in view space. The xy is the 
+	/// limitsOfNearPlane and the zw is an optimization see PpsSsao.glsl and 
+	/// r403 for the clean one
+	uniform vec4 limitsOfNearPlane_; 
+
+	/// Packs zNear in x that is used for the calculation of frag pos in view 
+	/// space. Also it packs the lightRadius (AKA distance for spot lights)
+	uniform vec4 zNearLightRadius; 
+
+	uniform vec4 lightPos_; ///< Light pos in eye space. w is always 0.0
+
+	uniform vec4 lightDiffuseCol_;
+	uniform vec4 lightSpecularCol_;
 	uniform mat4 texProjectionMat;
 };
 
+#define planes planes_.xy
+#define limitsOfNearPlane limitsOfNearPlane_.xy
+#define limitsOfNearPlane2 limitsOfNearPlane_.zw
+#define zNear zNearLightRadius.x
+#define lightRadius zNearLightRadius.y
+#define lightPos lightPos_.xyz
+#define lightDiffuseCol lightDiffuseCol_.xyz
+#define lightSpecularCol lightSpecularCol_.xyz
+
 uniform usampler2D msFai0;
 uniform sampler2D msDepthFai;
 uniform sampler2D lightTex;
@@ -85,25 +99,23 @@ float getAttenuation(in float fragLightDist)
 /// @return The blurred shadow
 float pcfLow(in vec3 shadowUv)
 {
-	float mapScale = 1.0 / shadowMapSize;
 	const int KERNEL_SIZE = 8;
-	const vec2 KERNEL[KERNEL_SIZE] = vec2[]
+	const ivec2 KERNEL[KERNEL_SIZE] = ivec2[]
 	(
-		vec2(1.0, 1.0),
-		vec2(1.0, -1.0),
-		vec2(-1.0, 1.0),
-		vec2(-1.0, -1.0),
-		vec2(0.0, 1.0),
-		vec2(0.0, -1.0),
-		vec2(1.0, 0.0),
-		vec2(-1.0, 0.0)
+		vec2(1, 1),
+		vec2(1, -1),
+		vec2(-1, 1),
+		vec2(-1, -1),
+		vec2(0, 1),
+		vec2(0, -1),
+		vec2(1, 0),
+		vec2(-1, 0)
 	);
 	
 	float shadowCol = texture(shadowMap, shadowUv);
 	for(int i = 0; i < KERNEL_SIZE; i++)
 	{
-		vec3 uv = vec3(shadowUv.xy + (KERNEL[i] * mapScale), shadowUv.z);
-		shadowCol += texture(shadowMap, uv);
+		shadowCol += textureOffset(shadowMap, shadowUv, KERNEL[i]);
 	}
 	
 	shadowCol *= (1.0 / 9.0);
@@ -232,8 +244,8 @@ void main()
 #	error "See file"
 #endif
 
-	
-	//fColor = vec3(texture(msDepthFai, vTexCoords).r);
+	//fColor = unpackNormal(unpackHalf2x16(texture(msFai0, vTexCoords).g));
+	//fColor = lightPos;
 
 	//gl_FragData[0] = gl_FragData[0] - gl_FragData[0] + vec4(1, 0, 1, 1);
 	/*#if defined(SPOT_LIGHT)

+ 13 - 15
src/renderer/Is.cpp

@@ -4,21 +4,19 @@
 #include "anki/scene/Camera.h"
 #include "anki/scene/Light.h"
 
-#define BLEND_ENABLE 0
+#define BLEND_ENABLE 1
 
 namespace anki {
 
 //==============================================================================
 
-/// Representation of the program's block
+/// Representation of the program's block. See shader for more info
 struct UniformBlockData
 {
-	Vec2 planes;
-	Vec2 limitsOfNearPlane;
-	Vec2 limitsOfNearPlane2;
-	float zNear; float lightRadius;
-	float shadowMapSize; float padding0;
-	Vec3 lightPos; float padding1;
+	Vec4 planes;
+	Vec4 limitsOfNearPlane;
+	Vec4 zNearLightRadius;
+	Vec4 lightPos;
 	Vec4 lightDiffuseCol;
 	Vec4 lightSpecularCol;
 	Mat4 texProjectionMat;
@@ -123,7 +121,6 @@ void Is::pointLightPass(PointLight& light)
 	const Camera& cam = r->getScene().getActiveCamera();
 
 	// XXX SMO
-	GlStateSingleton::get().disable(GL_DEPTH_TEST);
 
 	// shader prog
 	const ShaderProgram& shader = *pointLightSProg; // ensure the const-ness
@@ -134,14 +131,13 @@ void Is::pointLightPass(PointLight& light)
 		r->getMs().getDepthFai());
 
 	UniformBlockData data;
-	data.planes = r->getPlanes();
-	data.limitsOfNearPlane = r->getLimitsOfNearPlane();
-	data.limitsOfNearPlane2 = r->getLimitsOfNearPlane2();
-	data.zNear = cam.getNear();
+	data.planes = Vec4(r->getPlanes(), 0.0, 0.0);
+	data.limitsOfNearPlane = Vec4(r->getLimitsOfNearPlane(),
+		r->getLimitsOfNearPlane2());
+	data.zNearLightRadius = Vec4(cam.getNear(), light.getRadius(), 0.0, 0.0);
 	Vec3 lightPosEyeSpace = light.getWorldTransform().getOrigin().
 		getTransformed(cam.getViewMatrix());
-	data.lightPos = lightPosEyeSpace;
-	data.lightRadius = light.getRadius();
+	data.lightPos = Vec4(lightPosEyeSpace, 0.0);
 	data.lightDiffuseCol = light.getDiffuseColor();
 	data.lightSpecularCol = light.getSpecularColor();
 
@@ -166,6 +162,8 @@ void Is::run()
 #if BLEND_ENABLE
 	GlStateSingleton::get().enable(GL_BLEND);
 	glBlendFunc(GL_ONE, GL_ONE);
+#else
+	GlStateSingleton::get().disable(GL_BLEND);
 #endif
 
 	VisibilityInfo& vi = r->getScene().getVisibilityInfo();