Browse Source

Adding support for animated textures

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
791f460ace

+ 21 - 2
shaders/FsCommonFrag.glsl

@@ -18,6 +18,8 @@ layout(TEX_BINDING(1, 0)) uniform sampler2D anki_u_msDepthRt;
 #undef LIGHT_SS_BINDING
 #undef LIGHT_TEX_BINDING
 
+#define anki_u_time u_lightingUniforms.groundLightDirTime.w
+
 layout(location = 0) in vec3 in_vertPosViewSpace;
 layout(location = 1) flat in float in_alpha;
 
@@ -152,10 +154,11 @@ vec3 computeLightColor(vec3 diffCol)
 		uint k = calcClusterSplit(fragPos.z);
 
 		vec2 tilef = gl_FragCoord.xy / float(TILE_SIZE);
-		uint tile = uint(tilef.y) * u_lightingUniforms.tileCount.x
+		uint tile = uint(tilef.y) * u_lightingUniforms.tileCountPad1.x
 			+ uint(tilef.x);
 
-		uint cluster = u_clusters[tile + k * u_lightingUniforms.tileCount.z];
+		uint cluster =
+			u_clusters[tile + k * u_lightingUniforms.tileCountPad1.z];
 
 		lightOffset = cluster >> 16u;
 		pointLightsCount = (cluster >> 8u) & 0xFFu;
@@ -233,6 +236,22 @@ void particleTextureAlphaLight(in sampler2D tex, in float alpha)
 }
 #endif
 
+//==============================================================================
+#if PASS == COLOR
+#	define particleAnimatedTextureAlphaLight_DEFINED
+void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha,
+	float layerCount, float period)
+{
+	vec4 color = readAnimatedTextureRgba(tex, layerCount, period, gl_PointCoord,
+		anki_u_time);
+	color.a *= alpha;
+
+	vec3 lightColor = computeLightColor(color.rgb);
+
+	writeGBuffer(vec4(lightColor, color.a));
+}
+#endif
+
 //==============================================================================
 #if PASS == COLOR
 #	define fog_DEFINED

+ 2 - 1
shaders/IsLp.frag.glsl

@@ -143,7 +143,8 @@ void main()
 	}
 
 #if GROUND_LIGHT
-	out_color += max(dot(normal, u_lightingUniforms.groundLightDir.xyz), 0.0)
+	out_color +=
+		max(dot(normal, u_lightingUniforms.groundLightDirTime.xyz), 0.0)
 		* vec3(0.01, 0.001, 0.001);
 #endif
 

+ 2 - 2
shaders/LightResources.glsl

@@ -13,10 +13,10 @@ struct LightingUniforms
 {
 	vec4 projectionParams;
 	vec4 sceneAmbientColor;
-	vec4 groundLightDir;
+	vec4 groundLightDirTime;
 	vec4 nearFarClustererDivisor;
 	mat4 viewMat;
-	uvec4 tileCount;
+	uvec4 tileCountPad1;
 };
 
 layout(std140, row_major, SS_BINDING(LIGHT_SET, LIGHT_SS_BINDING))

+ 12 - 3
shaders/MsFsCommon.glsl

@@ -5,6 +5,10 @@
 
 #pragma anki include "shaders/Common.glsl"
 
+// Misc
+#define COLOR 0
+#define DEPTH 1
+
 // Generic functions because materials cannot use operators
 #define add_DEFINED
 #define add(a, b) ((a) + (b))
@@ -27,6 +31,11 @@
 #define setW_DEFINED
 #define setW(a, b) ((a).w = (b))
 
-// Misc
-#define COLOR 0
-#define DEPTH 1
+// Read from animated texture
+#define readAnimatedTextureRgba_DEFINED
+vec4 readAnimatedTextureRgba(sampler2DArray tex, float layerCount, float period,
+	vec2 uv, float time)
+{
+	float layer = mod(time * layerCount / period, layerCount);
+	return texture(tex, vec3(uv, layer));
+}

+ 8 - 2
src/renderer/Is.cpp

@@ -53,7 +53,7 @@ struct ShaderCommonUniforms
 {
 	Vec4 m_projectionParams;
 	Vec4 m_sceneAmbientColor;
-	Vec4 m_groundLightDir;
+	Vec4 m_groundLightDirTime;
 	Vec4 m_nearFarClustererDivisor;
 	Mat4 m_viewMat;
 	UVec4 m_tileCount;
@@ -812,7 +812,13 @@ void Is::updateCommonBlock(CommandBufferPtr& cmdb, const FrustumComponent& fr)
 	{
 		const Mat4& viewMat =
 			m_cam->getComponent<FrustumComponent>().getViewMatrix();
-		blk->m_groundLightDir = Vec4(-viewMat.getColumn(1).xyz(), 1.0);
+		blk->m_groundLightDirTime =
+			Vec4(-viewMat.getColumn(1).xyz(), HighRezTimer::getCurrentTime());
+	}
+	else
+	{
+		blk->m_groundLightDirTime =
+			Vec4(Vec3(0.0), HighRezTimer::getCurrentTime());
 	}
 
 	blk->m_tileCount = UVec4(m_r->getTileCountXY(), m_r->getTileCount(), 0);

+ 1 - 1
src/resource/ImageLoader.cpp

@@ -311,7 +311,7 @@ static ANKI_USE_RESULT Error loadAnkiTexture(
 		return ErrorCode::USER_DATA;
 	}
 
-	if(header.m_depth < 1 || header.m_depth > 16)
+	if(header.m_depth < 1 || header.m_depth > 128)
 	{
 		ANKI_LOGE("Zero or too big depth");
 		return ErrorCode::USER_DATA;

+ 1 - 0
src/resource/Material.cpp

@@ -372,6 +372,7 @@ Error Material::populateVariables(const MaterialProgramCreator& loader)
 		{
 		// samplers
 		case ShaderVariableDataType::SAMPLER_2D:
+		case ShaderVariableDataType::SAMPLER_2D_ARRAY:
 		case ShaderVariableDataType::SAMPLER_CUBE:
 			{
 				TextureResourcePtr tp;

+ 5 - 1
src/resource/MaterialProgramCreator.cpp

@@ -99,6 +99,10 @@ ANKI_USE_RESULT Error computeShaderVariableDataType(
 	{
 		out = ShaderVariableDataType::SAMPLER_2D;
 	}
+	else if(str == "sampler2DArray")
+	{
+		out = ShaderVariableDataType::SAMPLER_2D_ARRAY;
+	}
 	else if(str == "samplerCube")
 	{
 		out = ShaderVariableDataType::SAMPLER_CUBE;
@@ -118,7 +122,7 @@ ANKI_USE_RESULT Error computeShaderVariableDataType(
 
 //==============================================================================
 MaterialProgramCreator::MaterialProgramCreator(TempResourceAllocator<U8> alloc)
-:	m_alloc(alloc)
+	: m_alloc(alloc)
 {}
 
 //==============================================================================