Browse Source

Refactoring the shaders a bit

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
f1f79d5cb2

+ 3 - 2
include/anki/gl/GlObject.h

@@ -155,8 +155,9 @@ public:
 	void checkNonSharable() const
 	void checkNonSharable() const
 	{
 	{
 #if ANKI_DEBUG
 #if ANKI_DEBUG
-		ANKI_ASSERT(creationThreadId == std::this_thread::get_id() && 
-				"Object is not context sharable");
+		ANKI_ASSERT((!isCreated() 
+			|| creationThreadId == std::this_thread::get_id())
+			&& "Object is not context sharable");
 #endif
 #endif
 	}
 	}
 
 

+ 1 - 1
shaders/BsCommonFrag.glsl

@@ -1,7 +1,7 @@
 // Common code for all fragment shaders of BS
 // Common code for all fragment shaders of BS
 #define DEFAULT_FLOAT_PRECISION mediump
 #define DEFAULT_FLOAT_PRECISION mediump
 
 
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 #pragma anki include "shaders/MsBsCommon.glsl"
 #pragma anki include "shaders/MsBsCommon.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 
 

+ 21 - 0
shaders/Common.glsl

@@ -0,0 +1,21 @@
+// This file contains common code for all shaders. It's optional but it's 
+// recomended to include it
+
+#ifndef ANKI_SHADERS_COMMON_GLSL
+#define ANKI_SHADERS_COMMON_GLSL
+
+#ifndef DEFAULT_FLOAT_PRECISION
+#define DEFAULT_FLOAT_PRECISION highp
+#endif
+
+#ifndef DEFAULT_INT_PRECISION
+#define DEFAULT_INT_PRECISION highp
+#endif
+
+precision DEFAULT_FLOAT_PRECISION float;
+precision DEFAULT_FLOAT_PRECISION int;
+
+// Read from a render target texture
+#define textureFai(tex_, texc_) texture(tex_, texc_)
+
+#endif

+ 0 - 15
shaders/CommonFrag.glsl

@@ -1,15 +0,0 @@
-#ifndef ANKI_SHADERS_COMMON_FRAG_GLSL
-#define ANKI_SHADERS_COMMON_FRAG_GLSL
-
-#ifndef DEFAULT_FLOAT_PRECISION
-#define DEFAULT_FLOAT_PRECISION highp
-#endif
-
-#ifndef DEFAULT_INT_PRECISION
-#define DEFAULT_INT_PRECISION highp
-#endif
-
-precision DEFAULT_FLOAT_PRECISION float;
-precision DEFAULT_FLOAT_PRECISION int;
-
-#endif

+ 2 - 1
shaders/Dbg.glsl

@@ -1,6 +1,7 @@
 // Debug stage shader program
 // Debug stage shader program
 
 
 #pragma anki start vertexShader
 #pragma anki start vertexShader
+#pragma anki include "shaders/Common.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 
 
 layout(location = 0) in vec3 position;
 layout(location = 0) in vec3 position;
@@ -16,7 +17,7 @@ void main()
 }
 }
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 
 
 in vec3 vColor;
 in vec3 vColor;
 
 

+ 6 - 5
shaders/Final.glsl

@@ -5,17 +5,18 @@
 #pragma anki include shaders/SimpleVert.glsl
 #pragma anki include shaders/SimpleVert.glsl
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include shaders/CommonFrag.glsl
+#define DEFAULT_FLOAT_PRECISION lowp
+#pragma anki include shaders/Common.glsl
 #pragma anki include shaders/Pack.glsl
 #pragma anki include shaders/Pack.glsl
 
 
-uniform lowp sampler2D rasterImage;
+uniform sampler2D rasterImage;
 
 
-in vec2 vTexCoords;
-layout(location = 0) out lowp vec3 fFragColor;
+in highp vec2 vTexCoords;
+layout(location = 0) out vec3 fFragColor;
 
 
 void main()
 void main()
 {
 {
-	lowp vec3 col = texture(rasterImage, vTexCoords).rgb;
+	vec3 col = textureFai(rasterImage, vTexCoords).rgb;
 	//lowp vec3 col = vec3(readAndUnpackNormal(rasterImage, vTexCoords) * 0.5 + 0.5);
 	//lowp vec3 col = vec3(readAndUnpackNormal(rasterImage, vTexCoords) * 0.5 + 0.5);
 	fFragColor = col;
 	fFragColor = col;
 }
 }

+ 2 - 2
shaders/IsLp.glsl

@@ -38,7 +38,7 @@ void main()
 }
 }
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/IsCommon.glsl"
 #pragma anki include "shaders/IsCommon.glsl"
@@ -117,7 +117,7 @@ out vec3 fColor;
 /// @return frag pos in view space
 /// @return frag pos in view space
 vec3 getFragPosVSpace()
 vec3 getFragPosVSpace()
 {
 {
-	float depth = texture(msDepthFai, vTexCoords).r;
+	float depth = textureFai(msDepthFai, vTexCoords).r;
 
 
 	vec3 fragPosVspace;
 	vec3 fragPosVspace;
 	fragPosVspace.z = planes.y / (planes.x + depth);
 	fragPosVspace.z = planes.y / (planes.x + depth);

+ 2 - 0
shaders/MsBsCommon.glsl

@@ -1,3 +1,5 @@
+#pragma anki include "shaders/Common.glsl"
+
 /// Generic add
 /// Generic add
 #define add_DEFINED
 #define add_DEFINED
 #define add(a, b) ((a) + (b))
 #define add(a, b) ((a) + (b))

+ 1 - 1
shaders/MsCommonFrag.glsl

@@ -1,5 +1,5 @@
 #define DEFAULT_FLOAT_PRECISION highp
 #define DEFAULT_FLOAT_PRECISION highp
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 
 
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/MsBsCommon.glsl"
 #pragma anki include "shaders/MsBsCommon.glsl"

+ 5 - 5
shaders/Pack.glsl

@@ -79,15 +79,15 @@ void readGBuffer(
 	out float specPower)
 	out float specPower)
 {
 {
 #if USE_MRT
 #if USE_MRT
-	vec4 comp = texture(fai0, texCoord);
+	vec4 comp = textureFai(fai0, texCoord);
 	diffColor = comp.rgb;
 	diffColor = comp.rgb;
 	specColor = comp.a;
 	specColor = comp.a;
 
 
-	comp = texture(fai1, texCoord);
+	comp = textureFai(fai1, texCoord);
 	normal = normalize(comp.xyz * 2.0 - 1.0);
 	normal = normalize(comp.xyz * 2.0 - 1.0);
 	specPower = comp.w * MAX_SPECULARITY;
 	specPower = comp.w * MAX_SPECULARITY;
 #else
 #else
-	highp uvec2 all_ = texture(fai0, texCoord).rg;
+	highp uvec2 all_ = textureFai(fai0, texCoord).rg;
 
 
 	vec4 v = unpackUnorm4x8(all_[0]);
 	vec4 v = unpackUnorm4x8(all_[0]);
 	diffColor = v.rgb;
 	diffColor = v.rgb;
@@ -110,9 +110,9 @@ void readNormalFromGBuffer(
 	out vec3 normal)
 	out vec3 normal)
 {
 {
 #if USE_MRT
 #if USE_MRT
-	normal = normalize(texture(fai1, texCoord).xyz);
+	normal = normalize(textureFai(fai1, texCoord).xyz);
 #else
 #else
-	vec4 v = unpackUnorm4x8(texture(fai0, texCoord).g);
+	vec4 v = unpackUnorm4x8(textureFai(fai0, texCoord).g);
 	normal = normalize(v.xyz * 2.0 - 1.0);
 	normal = normalize(v.xyz * 2.0 - 1.0);
 #endif
 #endif
 }
 }

+ 10 - 10
shaders/Pps.glsl

@@ -3,7 +3,7 @@
 #pragma anki include "shaders/SimpleVert.glsl"
 #pragma anki include "shaders/SimpleVert.glsl"
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 #pragma anki include "shaders/photoshop_filters.glsl"
 #pragma anki include "shaders/photoshop_filters.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 
 
@@ -62,12 +62,12 @@ vec3 sharpen(in sampler2D tex, in vec2 texCoords)
 {
 {
 	const float sharpenFactor = 0.25;
 	const float sharpenFactor = 0.25;
 
 
-	vec3 col = texture(tex, texCoords).rgb;
+	vec3 col = textureFai(tex, texCoords).rgb;
 
 
-	vec3 col2 = texture(tex, texCoords + KERNEL[0]).rgb;
+	vec3 col2 = textureFai(tex, texCoords + KERNEL[0]).rgb;
 	for(int i = 1; i < 8; i++)
 	for(int i = 1; i < 8; i++)
 	{
 	{
-		col2 += texture(tex, texCoords + KERNEL[i]).rgb;
+		col2 += textureFai(tex, texCoords + KERNEL[i]).rgb;
 	}
 	}
 
 
 	return col * (8.0 * sharpenFactor + 1.0) - sharpenFactor * col2;
 	return col * (8.0 * sharpenFactor + 1.0) - sharpenFactor * col2;
@@ -76,11 +76,11 @@ vec3 sharpen(in sampler2D tex, in vec2 texCoords)
 //==============================================================================
 //==============================================================================
 vec3 erosion(in sampler2D tex, in vec2 texCoords)
 vec3 erosion(in sampler2D tex, in vec2 texCoords)
 {
 {
-    vec3 minValue = texture(tex, texCoords).rgb;
+    vec3 minValue = textureFai(tex, texCoords).rgb;
 
 
     for (int i = 0; i < 8; i++)
     for (int i = 0; i < 8; i++)
     {
     {
-        vec3 tmpCol = textureLod(tex, texCoords + KERNEL[i], 0.0).rgb;
+        vec3 tmpCol = textureFai(tex, texCoords + KERNEL[i]).rgb;
         minValue = min(tmpCol, minValue);
         minValue = min(tmpCol, minValue);
     }
     }
 
 
@@ -93,22 +93,22 @@ void main(void)
 #if defined(SHARPEN_ENABLED)
 #if defined(SHARPEN_ENABLED)
 	fColor = sharpen(isFai, vTexCoords);
 	fColor = sharpen(isFai, vTexCoords);
 #else
 #else
-	fColor = texture(isFai, vTexCoords).rgb;
+	fColor = textureFai(isFai, vTexCoords).rgb;
 #endif
 #endif
 	//fColor = erosion(isFai, vTexCoords);
 	//fColor = erosion(isFai, vTexCoords);
 
 
 #if defined(HDR_ENABLED)
 #if defined(HDR_ENABLED)
-	vec3 hdr = texture(ppsHdrFai, vTexCoords).rgb;
+	vec3 hdr = textureFai(ppsHdrFai, vTexCoords).rgb;
 	fColor += hdr;
 	fColor += hdr;
 #endif
 #endif
 
 
 #if defined(SSAO_ENABLED)
 #if defined(SSAO_ENABLED)
-	float ssao = texture(ppsSsaoFai, vTexCoords).r;
+	float ssao = textureFai(ppsSsaoFai, vTexCoords).r;
 	fColor *= ssao;
 	fColor *= ssao;
 #endif
 #endif
 
 
 #if defined(LF_ENABLED)
 #if defined(LF_ENABLED)
-	vec3 lf = texture(ppsLfFai, vTexCoords).rgb;
+	vec3 lf = textureFai(ppsLfFai, vTexCoords).rgb;
 	fColor += lf;
 	fColor += lf;
 #endif
 #endif
 
 

+ 2 - 2
shaders/PpsHdr.glsl

@@ -3,7 +3,7 @@
 #pragma anki include "shaders/SimpleVert.glsl"
 #pragma anki include "shaders/SimpleVert.glsl"
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 
 
 uniform lowp sampler2D fai; ///< Its the IS FAI
 uniform lowp sampler2D fai; ///< Its the IS FAI
 
 
@@ -22,7 +22,7 @@ const float brightMax = 4.0;
 
 
 void main()
 void main()
 {
 {
-	vec3 color = texture(fai, vTexCoords).rgb;
+	vec3 color = textureFai(fai, vTexCoords).rgb;
 
 
 	float luminance = dot(vec3(0.30, 0.59, 0.11), color);
 	float luminance = dot(vec3(0.30, 0.59, 0.11), color);
 	float yd = exposure * (exposure / brightMax + 1.0) /
 	float yd = exposure * (exposure / brightMax + 1.0) /

+ 6 - 6
shaders/PpsLfPseudoPass.glsl

@@ -6,7 +6,7 @@
 #pragma anki include "shaders/SimpleVert.glsl"
 #pragma anki include "shaders/SimpleVert.glsl"
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 
 
 #define MAX_GHOSTS 4
 #define MAX_GHOSTS 4
 #define GHOST_DISPERSAL (0.7)
 #define GHOST_DISPERSAL (0.7)
@@ -30,17 +30,17 @@ vec3 textureDistorted(
 {
 {
 #if ENABLE_CHROMATIC_DISTORTION
 #if ENABLE_CHROMATIC_DISTORTION
 	return vec3(
 	return vec3(
-		texture(tex, texcoord + direction * distortion.r).r,
-		texture(tex, texcoord + direction * distortion.g).g,
-		texture(tex, texcoord + direction * distortion.b).b);
+		textureFai(tex, texcoord + direction * distortion.r).r,
+		textureFai(tex, texcoord + direction * distortion.g).g,
+		textureFai(tex, texcoord + direction * distortion.b).b);
 #else
 #else
-	return texture(tex, texcoord).rgb;
+	return textureFai(tex, texcoord).rgb;
 #endif
 #endif
 }
 }
 
 
 void main()
 void main()
 {
 {
-	vec2 texcoord = -vTexCoords + vec2(1.0);
+	vec2 texcoord = vec2(1.0) - vTexCoords;
 
 
 	vec2 imgSize = vec2(textureSize(tex, 0));
 	vec2 imgSize = vec2(textureSize(tex, 0));
 
 

+ 1 - 1
shaders/PpsLfSpritePass.glsl

@@ -34,7 +34,7 @@ void main()
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
 #define DEFAULT_FLOAT_PRECISION mediump
 #define DEFAULT_FLOAT_PRECISION mediump
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 
 
 #define SINGLE_FLARE 0
 #define SINGLE_FLARE 0
 
 

+ 3 - 3
shaders/PpsSsao.glsl

@@ -3,7 +3,7 @@
 #pragma anki include "shaders/SimpleVert.glsl"
 #pragma anki include "shaders/SimpleVert.glsl"
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include "shaders/CommonFrag.glsl"
+#pragma anki include "shaders/Common.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 
 
@@ -71,7 +71,7 @@ vec3 getRandom(in vec2 uv)
 // Get position in view space
 // Get position in view space
 vec3 getPosition(in vec2 uv)
 vec3 getPosition(in vec2 uv)
 {
 {
-	float depth = texture(msDepthFai, uv).r;
+	float depth = textureFai(msDepthFai, uv).r;
 
 
 	vec3 fragPosVspace;
 	vec3 fragPosVspace;
 	fragPosVspace.z = -planes.y / (planes.x + depth);
 	fragPosVspace.z = -planes.y / (planes.x + depth);
@@ -86,7 +86,7 @@ vec3 getPosition(in vec2 uv)
 
 
 float getZ(in vec2 uv)
 float getZ(in vec2 uv)
 {
 {
-	float depth = texture(msDepthFai, uv).r;
+	float depth = textureFai(msDepthFai, uv).r;
 	float z = -planes.y / (planes.x + depth);
 	float z = -planes.y / (planes.x + depth);
 	return z;
 	return z;
 }
 }

+ 1 - 1
shaders/TilerMinMax.glsl

@@ -3,7 +3,7 @@
 #pragma anki include "shaders/SimpleVert.glsl"
 #pragma anki include "shaders/SimpleVert.glsl"
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
-#pragma anki include shaders/CommonFrag.glsl
+#pragma anki include shaders/Common.glsl
 
 
 #if !defined(TILES_X_COUNT) || !defined(TILES_Y_COUNT) || !defined(RENDERER_WIDTH) || !defined(RENDERER_HEIGHT)
 #if !defined(TILES_X_COUNT) || !defined(TILES_Y_COUNT) || !defined(RENDERER_WIDTH) || !defined(RENDERER_HEIGHT)
 #error Forgot to define something
 #error Forgot to define something

+ 4 - 3
shaders/VariableSamplingBlurGeneric.glsl

@@ -18,7 +18,8 @@ void main()
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
 
 
-precision mediump float;
+#define DEFAULT_FLOAT_PRECISION mediump
+#pragma anki include "shaders/Common.glsl"
 
 
 // Preprocessor switches sanity checks
 // Preprocessor switches sanity checks
 #if !defined(VPASS) && !defined(HPASS)
 #if !defined(VPASS) && !defined(HPASS)
@@ -115,12 +116,12 @@ layout(location = 0) out COL_TYPE fFragColor;
 void main()
 void main()
 {
 {
 	// Get the first
 	// Get the first
-	COL_TYPE col = texture(img, vTexCoords + kernel[0]).TEX_FETCH;
+	COL_TYPE col = textureFai(img, vTexCoords + kernel[0]).TEX_FETCH;
 
 
 	// Get the rest of the samples
 	// Get the rest of the samples
 	for(int i = 1; i < SAMPLES; i++)
 	for(int i = 1; i < SAMPLES; i++)
 	{
 	{
-		col += texture(img, vTexCoords + kernel[i]).TEX_FETCH;
+		col += textureFai(img, vTexCoords + kernel[i]).TEX_FETCH;
 	}
 	}
 
 
 	fFragColor = col * (1.0 / float(SAMPLES));
 	fFragColor = col * (1.0 / float(SAMPLES));