Browse Source

- Added lowp variant of GTC_colorspace convertLinearToSRGB #419

Christophe Riccio 9 years ago
parent
commit
a2684a8fe5
4 changed files with 31 additions and 2 deletions
  1. 11 1
      glm/gtc/color_space.inl
  2. 1 1
      glm/gtc/packing.inl
  3. 1 0
      readme.md
  4. 18 0
      test/gtc/gtc_color_space.cpp

+ 11 - 1
glm/gtc/color_space.inl

@@ -55,6 +55,16 @@ namespace detail
 		return detail::compute_rgbToSrgb<T, P, vecType>::call(ColorLinear, static_cast<T>(0.41666));
 	}
 
+	// Based on Ian Taylor http://chilliant.blogspot.fr/2012/08/srgb-approximations-for-hlsl.html
+	template <>
+	GLM_FUNC_QUALIFIER tvec3<float, lowp> convertLinearToSRGB(tvec3<float, lowp> const& ColorLinear)
+	{
+		tvec3<float, lowp> S1 = sqrt(ColorLinear);
+		tvec3<float, lowp> S2 = sqrt(S1);
+		tvec3<float, lowp> S3 = sqrt(S2);
+		return 0.662002687f * S1 + 0.684122060f * S2 - 0.323583601f * S3 - 0.0225411470f * ColorLinear;
+	}
+
 	template <typename T, precision P, template <typename, precision> class vecType>
 	GLM_FUNC_QUALIFIER vecType<T, P> convertLinearToSRGB(vecType<T, P> const& ColorLinear, T Gamma)
 	{
@@ -66,7 +76,7 @@ namespace detail
 	{
 		return detail::compute_srgbToRgb<T, P, vecType>::call(ColorSRGB, static_cast<T>(2.4));
 	}
-	
+
 	template <typename T, precision P, template <typename, precision> class vecType>
 	GLM_FUNC_QUALIFIER vecType<T, P> convertSRGBToLinear(vecType<T, P> const& ColorSRGB, T Gamma)
 	{

+ 1 - 1
glm/gtc/packing.inl

@@ -639,7 +639,7 @@ namespace detail
 		return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * pow(2.0f, Unpack.data.w - 15.f - 9.f);
 	}
 
-	// From http://graphicrants.blogspot.fr/2009/04/rgbm-color-encoding.html
+	// Based on Brian Karis http://graphicrants.blogspot.fr/2009/04/rgbm-color-encoding.html
 	template <typename T, precision P>
 	GLM_FUNC_QUALIFIER tvec4<T, P> packRGBM(tvec3<T, P> const & rgb)
 	{

+ 1 - 0
readme.md

@@ -57,6 +57,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate)
 - Added GTC_color_encoding extension
 
 ##### Improvements:
+- Added lowp variant of GTC_colorspace convertLinearToSRGB #419
 
 ##### Fixes:
 

+ 18 - 0
test/gtc/gtc_color_space.cpp

@@ -40,11 +40,29 @@ namespace srgb
 	}
 }//namespace srgb
 
+namespace srgb_lowp
+{
+	int test()
+	{
+		int Error(0);
+
+		for(float Color = 0.0f; Color < 1.0f; Color += 0.01f)
+		{
+			glm::highp_vec3 const HighpSRGB = glm::convertLinearToSRGB(glm::highp_vec3(Color));
+			glm::lowp_vec3 const LowpSRGB = glm::convertLinearToSRGB(glm::lowp_vec3(Color));
+			Error += glm::all(glm::epsilonEqual(glm::abs(HighpSRGB - glm::highp_vec3(LowpSRGB)), glm::highp_vec3(0), 0.1f)) ? 0 : 1;
+		}
+
+		return Error;
+	}
+}//namespace srgb_lowp
+
 int main()
 {
 	int Error(0);
 
 	Error += srgb::test();
+	Error += srgb_lowp::test();
 
 	return Error;
 }