Browse Source

Math & SRGB (so ugly) & multisampling

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
b82d42c239

+ 4 - 1
include/anki/gl/Texture.h

@@ -180,6 +180,7 @@ public:
 		Bool repeat = false;
 		I anisotropyLevel = 0;
 		Bool genMipmaps = false;
+		U samples = 1;
 
 		Array<Array<Data, 256>, 256> data; ///< Array of data in: [mip][layer]
 
@@ -268,7 +269,8 @@ public:
 
 	/// Create a FAI
 	void create2dFai(
-		U w, U h, GLenum internalFormat, GLenum format, GLenum type);
+		U w, U h, GLenum internalFormat, GLenum format, GLenum type,
+		U samples = 1);
 
 	/// Bind the texture to a unit that the texture unit system will decide
 	/// @return The texture init
@@ -311,6 +313,7 @@ private:
 	GLenum type = GL_NONE; ///< GL_UNSIGNED_BYTE, GL_BYTE etc
 	GLuint width = 0, height = 0, depth = 0;
 	TextureFilteringType filtering;
+	U8 samples = 1;
 
 	Bool isCreated() const
 	{

+ 31 - 23
include/anki/math/F16.h

@@ -11,6 +11,14 @@ namespace anki {
 /// Half float
 class F16
 {
+	/// @name Friends
+	/// @{
+	friend F32 operator+(const F32 f, const F16 h);
+	friend F32 operator-(const F32 f, const F16 h);
+	friend F32 operator*(const F32 f, const F16 h);
+	friend F32 operator/(const F32 f, const F16 h);
+	/// @}
+
 public:
 	/// @name Constructors
 	/// @{
@@ -172,35 +180,35 @@ public:
 	}
 	/// @}
 
-	/// @name Friends
-	/// @{
-	friend F32 operator+(const F32 f, const F16 h)
-	{
-		return f + h.toF32();
-	}
-
-	friend F32 operator-(const F32 f, const F16 h)
-	{
-		return f - h.toF32();
-	}
-
-	friend F32 operator*(const F32 f, const F16 h)
-	{
-		return f * h.toF32();
-	}
-
-	friend F32 operator/(const F32 f, const F16 h)
-	{
-		return f / h.toF32();
-	}
-	/// @}
-
 private:
 	U16 data;
 
 	static F32 toF32(F16 h);
 	static F16 toF16(F32 f);
 };
+
+/// @name F16 friends
+/// @{
+inline F32 operator+(const F32 f, const F16 h)
+{
+	return f + h.toF32();
+}
+
+inline F32 operator-(const F32 f, const F16 h)
+{
+	return f - h.toF32();
+}
+
+inline F32 operator*(const F32 f, const F16 h)
+{
+	return f * h.toF32();
+}
+
+inline F32 operator/(const F32 f, const F16 h)
+{
+	return f / h.toF32();
+}
+/// @}
 /// @}
 
 static_assert(sizeof(F16) == 2, "Incorrect size");

+ 49 - 35
include/anki/math/Mat3.h

@@ -13,6 +13,18 @@ namespace anki {
 template<typename T>
 class TMat3
 {
+	/// @name Friends
+	/// @{
+	template<typename Y>
+	friend TMat3<Y> operator+(Y f, const TMat3<Y>& m3);
+	template<typename Y>
+	friend TMat3<Y> operator-(Y f, const TMat3<Y>& m3);
+	template<typename Y>
+	friend TMat3<Y> operator*(Y f, const TMat3<Y>& m3);
+	template<typename Y>
+	friend TMat3<Y> operator/(Y f, const TMat3<Y>& m3);
+	/// @}
+
 public:
 	/// @name Constructors
 	/// @{
@@ -699,39 +711,6 @@ public:
 	}
 	/// @}
 
-	/// @name Friends
-	/// @{
-	friend TMat3 operator+(T f, const TMat3& m3)
-	{
-		return m3 + f;
-	}
-
-	friend TMat3 operator-(T f, const TMat3& m3)
-	{
-		TMat3 out;
-		for(U i = 0; i < 9; i++)
-		{
-			out[i] = f - m3[i];
-		}
-		return out;
-	}
-
-	friend TMat3 operator*(T f, const TMat3& m3)
-	{
-		return m3 * f;
-	}
-
-	friend TMat3 operator/(T f, const TMat3& m3)
-	{
-		TMat3 out;
-		for(U i = 0; i < 9; i++)
-		{
-			out[i] = f / m3[i];
-		}
-		return out;
-	}
-	/// @}
-
 private:
 	/// @name Data members
 	/// @{
@@ -745,11 +724,46 @@ private:
 	/// @}
 };
 
+/// @name TMat3 friends
+/// @{
+template<typename T>
+TMat3<T> operator+(T f, const TMat3<T>& m3)
+{
+	return m3 + f;
+}
+
+template<typename T>
+TMat3<T> operator-(T f, const TMat3<T>& m3)
+{
+	TMat3<T> out;
+	for(U i = 0; i < 9; i++)
+	{
+		out[i] = f - m3[i];
+	}
+	return out;
+}
+
+template<typename T>
+TMat3<T> operator*(T f, const TMat3<T>& m3)
+{
+	return m3 * f;
+}
+
+template<typename T>
+TMat3<T> operator/(T f, const TMat3<T>& m3)
+{
+	TMat3<T> out;
+	for(U i = 0; i < 9; i++)
+	{
+		out[i] = f / m3[i];
+	}
+	return out;
+}
+/// @}
+
 /// F32 3x3 matrix
 typedef TMat3<F32> Mat3;
-
 static_assert(sizeof(Mat3) == sizeof(F32) * 3 * 3, "Incorrect size");
-
 /// @}
 
 } // end namespace anki

+ 12 - 17
include/anki/math/Mat4.h

@@ -29,6 +29,18 @@ struct TMat4Simd<F32>
 template<typename T>
 ANKI_ATTRIBUTE_ALIGNED(class, 16) TMat4
 {
+	/// @name Friends
+	/// @{
+	template<typename Y>
+	friend TMat4<Y> operator+(const Y f, const TMat4<Y>& m4);
+	template<typename Y>
+	friend TMat4<Y> operator-(const Y f, const TMat4<Y>& m4);
+	template<typename Y>
+	friend TMat4<Y> operator*(const Y f, const TMat4<Y>& m4);
+	template<typename Y>
+	friend TMat4<Y> operator/(const Y f, const TMat4<Y>& m4);
+	/// @}
+
 public:
 	typedef typename TMat4Simd<T>::Type Simd;
 
@@ -773,21 +785,6 @@ public:
 	}
 	/// @}
 
-	/// @name Friends
-	/// @{
-	template<typename Y>
-	friend TMat4<Y> operator+(const Y f, const TMat4<Y>& m4);
-
-	template<typename Y>
-	friend TMat4<Y> operator-(const Y f, const TMat4<Y>& m4);
-
-	template<typename Y>
-	friend TMat4<Y> operator*(const Y f, const TMat4<Y>& m4);
-
-	template<typename Y>
-	friend TMat4<Y> operator/(const Y f, const TMat4<Y>& m4);
-	/// @}
-
 private:
 	/// @name Data
 	/// @{
@@ -877,9 +874,7 @@ TMat4<F32> operator/(const F32 f, const TMat4<F32>& m4);
 
 /// F32 4x4 matrix
 typedef TMat4<F32> Mat4;
-
 static_assert(sizeof(Mat4) == sizeof(F32) * 4 * 4, "Incorrect size");
-
 /// @}
 
 } // end namespace anki

+ 15 - 11
include/anki/math/Mat4.inl.h

@@ -139,20 +139,24 @@ inline TMat4<F32>& TMat4<F32>::operator-=(const TMat4<F32>& b)
 template<>
 inline TMat4<F32> TMat4<F32>::operator*(const TMat4<F32>& b) const
 {
-	TMat4<F32> c;
-	TMat4<F32> t(b);
-	t.transpose();
-	
-	// XXX See if this is optimal
+	TMat4<F32> out;
+	const TMat4<F32>& m = *this;
 	for(U i = 0; i < 4; i++)
 	{
-		for(U j = 0; j < 4; j++)
-		{
-			_mm_store_ss(&c(i, j), _mm_dp_ps(simd[i], t.simd[j], 0xF1));
-		}
+		__m128 t1, t2;
+
+		t1 = _mm_set1_ps(m(i, 0));
+		t2 = _mm_mul_ps(b.simd[0], t1);
+		t1 =_mm_set1_ps(m(i, 1));
+		t2 = _mm_add_ps(_mm_mul_ps(b.simd[1], t1), t2);
+		t1 =_mm_set1_ps(m(i, 2));
+		t2 = _mm_add_ps(_mm_mul_ps(b.simd[2], t1), t2);
+		t1 =_mm_set1_ps(m(i, 3));
+		t2 = _mm_add_ps(_mm_mul_ps(b.simd[3], t1), t2);
+
+		out.simd[i] = t2;
 	}
-
-	return c;
+	return out;
 }
 
 //==============================================================================

+ 12 - 16
include/anki/math/Vec4.h

@@ -35,6 +35,18 @@ struct TVec4Simd<F32>
 template<typename T>
 ANKI_ATTRIBUTE_ALIGNED(class, 16) TVec4
 {
+	/// @name Friends
+	/// @{
+	template<typename Y>
+	friend TVec4<Y> operator+(const Y f, const TVec4<Y>& v4);
+	template<typename Y>
+	friend TVec4<Y> operator-(const Y f, const TVec4<Y>& v4);
+	template<typename Y>
+	friend TVec4<Y> operator*(const Y f, const TVec4<Y>& v4);
+	template<typename Y>
+	friend TVec4<Y> operator/(const Y f, const TVec4<Y>& v4);
+	/// @}
+
 public:
 	typedef typename TVec4Simd<T>::Type Simd;
 
@@ -374,21 +386,6 @@ public:
 	}
 	/// @}
 
-	/// @name Friends
-	/// @{
-	template<typename Y>
-	friend TVec4<Y> operator+(const Y f, const TVec4<Y>& v4);
-
-	template<typename Y>
-	friend TVec4<Y> operator-(const Y f, const TVec4<Y>& v4);
-
-	template<typename Y>
-	friend TVec4<Y> operator*(const Y f, const TVec4<Y>& v4);
-
-	template<typename Y>
-	friend TVec4<Y> operator/(const Y f, const TVec4<Y>& v4);
-	/// @}
-
 private:
 	/// @name Data
 	/// @{
@@ -516,7 +513,6 @@ typedef TVec4<I32> IVec4;
 
 /// 32bit unsigned integer 4D vector
 typedef TVec4<U32> UVec4;
-
 /// @}
 
 } // end namespace anki

+ 1 - 1
shaders/IsLp.glsl

@@ -326,7 +326,7 @@ void main()
 
 #if GROUND_LIGHT
 	fColor += max(dot(normal, groundLightDir.xyz), 0.0) 
-		* vec3(0.1, 0.1, 0.0);
+		* vec3(0.05, 0.05, 0.01);
 #endif
 
 #if 0

+ 1 - 0
shaders/Pps.glsl

@@ -112,6 +112,7 @@ void main(void)
 	fColor += lf;
 #endif
 
+	//fColor = BlendHardLight(vec3(0.7, 0.72, 0.4), fColor);
 	fColor = gammaCorrectionRgb(vec3(0.9, 0.92, 0.75), fColor);
 
 #if 0

+ 3 - 1
shaders/PpsSsao.glsl

@@ -43,6 +43,8 @@ uniform sampler2D noiseMap;
 /// @}
 
 #define RADIUS 0.5
+#define DARKNESS_MULTIPLIER 2.0 // Initial is 1.0 but the bigger it is the more
+                                // darker the SSAO factor gets
 
 // Get normal
 vec3 getNormal(in vec2 uv)
@@ -120,7 +122,7 @@ void main(void)
 		float sampleDepth = getZ(offset.xy);
 
 		// range check & accumulate:
-		const float ADVANCE = 1.0 / float(KERNEL_SIZE);
+		const float ADVANCE = DARKNESS_MULTIPLIER / float(KERNEL_SIZE);
 
 #if 1
 		float rangeCheck = 

+ 41 - 27
src/gl/Texture.cpp

@@ -225,6 +225,7 @@ void Texture::create(const Initializer& init)
 	internalFormat = init.internalFormat;
 	format = init.format;
 	type = init.type;
+	samples = init.samples;
 
 	// Bind
 	TextureUnitsSingleton::get().bindTextureAndActivateUnit(*this);
@@ -355,6 +356,15 @@ void Texture::create(const Initializer& init)
 				}
 			}
 			break;
+		case GL_TEXTURE_2D_MULTISAMPLE:
+			glTexImage2DMultisample(
+				target,
+				samples,
+				internalFormat,
+				w,
+				h,
+				GL_TRUE);
+			break;
 		default:
 			ANKI_ASSERT(0);
 		}
@@ -366,51 +376,54 @@ void Texture::create(const Initializer& init)
 	ANKI_CHECK_GL_ERROR();
 
 	// Set parameters
-	if(init.repeat)
-	{
-		glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
-		glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
-	}
-	else
+	if(samples == 1)
 	{
-		glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-		glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-	}
+		if(init.repeat)
+		{
+			glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
+			glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
+		}
+		else
+		{
+			glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+			glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+		}
 
-	if(init.genMipmaps)
-	{
-		glGenerateMipmap(target);
-	}
-	else
-	{
-		// Make sure that the texture is complete
-		glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, init.mipmapsCount - 1);
-	}
+		if(init.genMipmaps)
+		{
+			glGenerateMipmap(target);
+		}
+		else
+		{
+			// Make sure that the texture is complete
+			glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, init.mipmapsCount - 1);
+		}
 
-	// Set filtering type
-	setFilteringNoBind(init.filteringType);
+		// Set filtering type
+		setFilteringNoBind(init.filteringType);
 
 #if ANKI_GL == ANKI_GL_DESKTOP
-	if(init.anisotropyLevel > 1)
-	{
-		glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 
-			GLint(init.anisotropyLevel));
-	}
+		if(init.anisotropyLevel > 1)
+		{
+			glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 
+				GLint(init.anisotropyLevel));
+		}
 #endif
+	}
 
 	ANKI_CHECK_GL_ERROR();
 }
 
 //==============================================================================
 void Texture::create2dFai(U w, U h, 
-	GLenum internalFormat_, GLenum format_, GLenum type_)
+	GLenum internalFormat_, GLenum format_, GLenum type_, U samples_)
 {
 	Initializer init;
 
 	init.width = w;
 	init.height = h;
 	init.depth = 0;
-	init.target = GL_TEXTURE_2D;
+	init.target = (samples_ == 1) ? GL_TEXTURE_2D : GL_TEXTURE_2D_MULTISAMPLE;
 	init.internalFormat = internalFormat_;
 	init.format = format_;
 	init.type = type_;
@@ -419,6 +432,7 @@ void Texture::create2dFai(U w, U h,
 	init.repeat = false;
 	init.anisotropyLevel = 0;
 	init.genMipmaps = false;
+	init.samples = samples_;
 
 	create(init);
 }

+ 1 - 1
src/renderer/Hdr.cpp

@@ -10,7 +10,7 @@ Hdr::~Hdr()
 //==============================================================================
 void Hdr::initFbo(Fbo& fbo, Texture& fai)
 {
-	fai.create2dFai(width, height, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE);
+	fai.create2dFai(width, height, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE);
 
 	// Set to bilinear because the blurring techniques take advantage of that
 	fai.setFiltering(Texture::TFT_LINEAR);

+ 1 - 1
src/renderer/Is.cpp

@@ -484,7 +484,7 @@ void Is::initInternal(const RendererInitializer& initializer)
 	//
 
 	// IS FBO
-	fai.create2dFai(r->getWidth(), r->getHeight(), GL_RGB8,
+	fai.create2dFai(r->getWidth(), r->getHeight(), GL_SRGB8,
 		GL_RGB, GL_UNSIGNED_BYTE);
 	fbo.create();
 	fbo.setColorAttachments({&fai});

+ 1 - 1
src/renderer/Lf.cpp

@@ -102,7 +102,7 @@ void Lf::initInternal(const RendererInitializer& initializer)
 	// Create the FAI
 	fai.create2dFai(r->getPps().getHdr().getFai().getWidth(), 
 		r->getPps().getHdr().getFai().getHeight(), 
-		GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE);
+		GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE);
 
 	fbo.create();
 	fbo.setColorAttachments({&fai});

+ 2 - 0
src/renderer/MainRenderer.cpp

@@ -67,6 +67,8 @@ void MainRenderer::initGl()
 	GlStateSingleton::get().setDepthMaskEnabled(true);
 	GlStateSingleton::get().setDepthFunc(GL_LESS);
 
+	glEnable(GL_FRAMEBUFFER_SRGB);
+
 	glDisable(GL_DITHER);
 
 	// Check for error

+ 2 - 2
src/renderer/Ms.cpp

@@ -18,8 +18,8 @@ void Ms::init(const RendererInitializer& initializer)
 	try
 	{
 #if ANKI_RENDERER_USE_MRT
-		fai0.create2dFai(r->getWidth(), r->getHeight(), GL_RGBA8,
-			GL_RGBA, GL_UNSIGNED_BYTE);
+		fai0.create2dFai(r->getWidth(), r->getHeight(), GL_SRGB8_ALPHA8,
+			GL_RGBA, GL_UNSIGNED_BYTE, 16);
 		fai1.create2dFai(r->getWidth(), r->getHeight(), GL_RG16F,
 			GL_RG, GL_FLOAT);
 #else

+ 1 - 1
src/renderer/Pps.cpp

@@ -34,7 +34,7 @@ void Pps::initInternal(const RendererInitializer& initializer)
 	height = initializer.height / initializer.renderingQuality;
 
 	// FBO
-	fai.create2dFai(r->getWidth(), r->getHeight(), GL_RGB, GL_RGB,
+	fai.create2dFai(r->getWidth(), r->getHeight(), GL_SRGB8, GL_RGB,
 		GL_UNSIGNED_BYTE);
 
 	fbo.create();

+ 3 - 1
src/resource/Image.cpp

@@ -745,7 +745,9 @@ void Image::load(const char* filename)
 		}
 		else if(strcmp(ext, "ankitex") == 0)
 		{
-#if ANKI_GL == ANKI_GL_DESKTOP
+#if 0
+			compression = Image::DC_RAW;
+#elif ANKI_GL == ANKI_GL_DESKTOP
 			compression = Image::DC_S3TC;
 #else
 			compression = Image::DC_ETC;

+ 2 - 1
testapp/Main.cpp

@@ -119,6 +119,7 @@ void init()
 	ANKI_LOGI("Other init...");
 
 	SceneGraph& scene = SceneGraphSingleton::get();
+	scene.setAmbientColor(Vec3(0.04));
 
 #if 0
 	painter = new UiPainter(Vec2(AppSingleton::get().getWindowWidth(),
@@ -526,7 +527,7 @@ void initSubsystems(int argc, char* argv[])
 	nwinit.depthBits = 0;
 	nwinit.stencilBits = 0;
 	nwinit.fullscreenDesktopRez = true;
-	nwinit.debugContext = false;
+	nwinit.debugContext = true;
 	win = new NativeWindow;	
 	win->create(nwinit);