color.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "constants.h"
  5. #include "col3.h"
  6. #include "col4.h"
  7. #include "../simd/sse.h"
  8. namespace embree
  9. {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /// SSE RGBA Color Class
  12. ////////////////////////////////////////////////////////////////////////////////
  13. struct Color4
  14. {
  15. union {
  16. __m128 m128;
  17. struct { float r,g,b,a; };
  18. };
  19. ////////////////////////////////////////////////////////////////////////////////
  20. /// Construction
  21. ////////////////////////////////////////////////////////////////////////////////
  22. __forceinline Color4 () {}
  23. __forceinline Color4 ( const __m128 a ) : m128(a) {}
  24. __forceinline explicit Color4 (const float v) : m128(_mm_set1_ps(v)) {}
  25. __forceinline Color4 (const float r, const float g, const float b, const float a) : m128(_mm_set_ps(a,b,g,r)) {}
  26. __forceinline explicit Color4 ( const Col3uc& other ) { m128 = _mm_mul_ps(_mm_set_ps(255.0f,other.b,other.g,other.r),_mm_set1_ps(one_over_255)); }
  27. __forceinline explicit Color4 ( const Col3f& other ) { m128 = _mm_set_ps(1.0f,other.b,other.g,other.r); }
  28. __forceinline explicit Color4 ( const Col4uc& other ) { m128 = _mm_mul_ps(_mm_set_ps(other.a,other.b,other.g,other.r),_mm_set1_ps(one_over_255)); }
  29. __forceinline explicit Color4 ( const Col4f& other ) { m128 = _mm_set_ps(other.a,other.b,other.g,other.r); }
  30. __forceinline Color4 ( const Color4& other ) : m128(other.m128) {}
  31. __forceinline Color4& operator=( const Color4& other ) { m128 = other.m128; return *this; }
  32. __forceinline operator const __m128&() const { return m128; }
  33. __forceinline operator __m128&() { return m128; }
  34. ////////////////////////////////////////////////////////////////////////////////
  35. /// Set
  36. ////////////////////////////////////////////////////////////////////////////////
  37. __forceinline void set(Col3f& d) const { d.r = r; d.g = g; d.b = b; }
  38. __forceinline void set(Col4f& d) const { d.r = r; d.g = g; d.b = b; d.a = a; }
  39. __forceinline void set(Col3uc& d) const
  40. {
  41. vfloat4 s = clamp(vfloat4(m128))*255.0f;
  42. d.r = (unsigned char)(s[0]);
  43. d.g = (unsigned char)(s[1]);
  44. d.b = (unsigned char)(s[2]);
  45. }
  46. __forceinline void set(Col4uc& d) const
  47. {
  48. vfloat4 s = clamp(vfloat4(m128))*255.0f;
  49. d.r = (unsigned char)(s[0]);
  50. d.g = (unsigned char)(s[1]);
  51. d.b = (unsigned char)(s[2]);
  52. d.a = (unsigned char)(s[3]);
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. /// Constants
  56. ////////////////////////////////////////////////////////////////////////////////
  57. __forceinline Color4( ZeroTy ) : m128(_mm_set1_ps(0.0f)) {}
  58. __forceinline Color4( OneTy ) : m128(_mm_set1_ps(1.0f)) {}
  59. __forceinline Color4( PosInfTy ) : m128(_mm_set1_ps(pos_inf)) {}
  60. __forceinline Color4( NegInfTy ) : m128(_mm_set1_ps(neg_inf)) {}
  61. };
  62. ////////////////////////////////////////////////////////////////////////////////
  63. /// SSE RGB Color Class
  64. ////////////////////////////////////////////////////////////////////////////////
  65. struct Color
  66. {
  67. union {
  68. __m128 m128;
  69. struct { float r,g,b; };
  70. };
  71. ////////////////////////////////////////////////////////////////////////////////
  72. /// Construction
  73. ////////////////////////////////////////////////////////////////////////////////
  74. __forceinline Color () {}
  75. __forceinline Color ( const __m128 a ) : m128(a) {}
  76. __forceinline explicit Color (const float v) : m128(_mm_set1_ps(v)) {}
  77. __forceinline Color (const float r, const float g, const float b) : m128(_mm_set_ps(0.0f,b,g,r)) {}
  78. __forceinline Color ( const Color& other ) : m128(other.m128) {}
  79. __forceinline Color& operator=( const Color& other ) { m128 = other.m128; return *this; }
  80. __forceinline Color ( const Color4& other ) : m128(other.m128) {}
  81. __forceinline Color& operator=( const Color4& other ) { m128 = other.m128; return *this; }
  82. __forceinline operator const __m128&() const { return m128; }
  83. __forceinline operator __m128&() { return m128; }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. /// Set
  86. ////////////////////////////////////////////////////////////////////////////////
  87. __forceinline void set(Col3f& d) const { d.r = r; d.g = g; d.b = b; }
  88. __forceinline void set(Col4f& d) const { d.r = r; d.g = g; d.b = b; d.a = 1.0f; }
  89. __forceinline void set(Col3uc& d) const
  90. {
  91. vfloat4 s = clamp(vfloat4(m128))*255.0f;
  92. d.r = (unsigned char)(s[0]);
  93. d.g = (unsigned char)(s[1]);
  94. d.b = (unsigned char)(s[2]);
  95. }
  96. __forceinline void set(Col4uc& d) const
  97. {
  98. vfloat4 s = clamp(vfloat4(m128))*255.0f;
  99. d.r = (unsigned char)(s[0]);
  100. d.g = (unsigned char)(s[1]);
  101. d.b = (unsigned char)(s[2]);
  102. d.a = 255;
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. /// Constants
  106. ////////////////////////////////////////////////////////////////////////////////
  107. __forceinline Color( ZeroTy ) : m128(_mm_set1_ps(0.0f)) {}
  108. __forceinline Color( OneTy ) : m128(_mm_set1_ps(1.0f)) {}
  109. __forceinline Color( PosInfTy ) : m128(_mm_set1_ps(pos_inf)) {}
  110. __forceinline Color( NegInfTy ) : m128(_mm_set1_ps(neg_inf)) {}
  111. };
  112. ////////////////////////////////////////////////////////////////////////////////
  113. /// Unary Operators
  114. ////////////////////////////////////////////////////////////////////////////////
  115. __forceinline const Color operator +( const Color& a ) { return a; }
  116. __forceinline const Color operator -( const Color& a ) {
  117. const __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
  118. return _mm_xor_ps(a.m128, mask);
  119. }
  120. __forceinline const Color abs ( const Color& a ) {
  121. const __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
  122. return _mm_and_ps(a.m128, mask);
  123. }
  124. __forceinline const Color rcp ( const Color& a )
  125. {
  126. #if defined(__AVX512VL__)
  127. const Color r = _mm_rcp14_ps(a.m128);
  128. #else
  129. const Color r = _mm_rcp_ps(a.m128);
  130. #endif
  131. return _mm_sub_ps(_mm_add_ps(r, r), _mm_mul_ps(_mm_mul_ps(r, r), a));
  132. }
  133. __forceinline const Color rsqrt( const Color& a )
  134. {
  135. #if defined(__AVX512VL__)
  136. __m128 r = _mm_rsqrt14_ps(a.m128);
  137. #else
  138. __m128 r = _mm_rsqrt_ps(a.m128);
  139. #endif
  140. return _mm_add_ps(_mm_mul_ps(_mm_set1_ps(1.5f),r), _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set1_ps(-0.5f)), r), _mm_mul_ps(r, r)));
  141. }
  142. __forceinline const Color sqrt ( const Color& a ) { return _mm_sqrt_ps(a.m128); }
  143. ////////////////////////////////////////////////////////////////////////////////
  144. /// Binary Operators
  145. ////////////////////////////////////////////////////////////////////////////////
  146. __forceinline const Color operator +( const Color& a, const Color& b ) { return _mm_add_ps(a.m128, b.m128); }
  147. __forceinline const Color operator -( const Color& a, const Color& b ) { return _mm_sub_ps(a.m128, b.m128); }
  148. __forceinline const Color operator *( const Color& a, const Color& b ) { return _mm_mul_ps(a.m128, b.m128); }
  149. __forceinline const Color operator *( const Color& a, const float b ) { return a * Color(b); }
  150. __forceinline const Color operator *( const float a, const Color& b ) { return Color(a) * b; }
  151. __forceinline const Color operator /( const Color& a, const Color& b ) { return a * rcp(b); }
  152. __forceinline const Color operator /( const Color& a, const float b ) { return a * rcp(b); }
  153. __forceinline const Color min( const Color& a, const Color& b ) { return _mm_min_ps(a.m128,b.m128); }
  154. __forceinline const Color max( const Color& a, const Color& b ) { return _mm_max_ps(a.m128,b.m128); }
  155. ////////////////////////////////////////////////////////////////////////////////
  156. /// Assignment Operators
  157. ////////////////////////////////////////////////////////////////////////////////
  158. __forceinline const Color operator+=(Color& a, const Color& b) { return a = a + b; }
  159. __forceinline const Color operator-=(Color& a, const Color& b) { return a = a - b; }
  160. __forceinline const Color operator*=(Color& a, const Color& b) { return a = a * b; }
  161. __forceinline const Color operator/=(Color& a, const Color& b) { return a = a / b; }
  162. __forceinline const Color operator*=(Color& a, const float b ) { return a = a * b; }
  163. __forceinline const Color operator/=(Color& a, const float b ) { return a = a / b; }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. /// Reductions
  166. ////////////////////////////////////////////////////////////////////////////////
  167. __forceinline float reduce_add(const Color& v) { return v.r+v.g+v.b; }
  168. __forceinline float reduce_mul(const Color& v) { return v.r*v.g*v.b; }
  169. __forceinline float reduce_min(const Color& v) { return min(v.r,v.g,v.b); }
  170. __forceinline float reduce_max(const Color& v) { return max(v.r,v.g,v.b); }
  171. ////////////////////////////////////////////////////////////////////////////////
  172. /// Comparison Operators
  173. ////////////////////////////////////////////////////////////////////////////////
  174. __forceinline bool operator ==( const Color& a, const Color& b ) { return (_mm_movemask_ps(_mm_cmpeq_ps (a.m128, b.m128)) & 7) == 7; }
  175. __forceinline bool operator !=( const Color& a, const Color& b ) { return (_mm_movemask_ps(_mm_cmpneq_ps(a.m128, b.m128)) & 7) != 0; }
  176. __forceinline bool operator < ( const Color& a, const Color& b ) {
  177. if (a.r != b.r) return a.r < b.r;
  178. if (a.g != b.g) return a.g < b.g;
  179. if (a.b != b.b) return a.b < b.b;
  180. return false;
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////
  183. /// Select
  184. ////////////////////////////////////////////////////////////////////////////////
  185. __forceinline const Color select( bool s, const Color& t, const Color& f ) {
  186. __m128 mask = s ? _mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128())) : _mm_setzero_ps();
  187. return blendv_ps(f, t, mask);
  188. }
  189. ////////////////////////////////////////////////////////////////////////////////
  190. /// Special Operators
  191. ////////////////////////////////////////////////////////////////////////////////
  192. /*! computes luminance of a color */
  193. __forceinline float luminance (const Color& a) { return madd(0.212671f,a.r,madd(0.715160f,a.g,0.072169f*a.b)); }
  194. /*! output operator */
  195. __forceinline embree_ostream operator<<(embree_ostream cout, const Color& a) {
  196. return cout << "(" << a.r << ", " << a.g << ", " << a.b << ")";
  197. }
  198. }