Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Color.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Convert float RGB(A) colors to unsigned byte
20  */
21 
22 #include "precompiled.h"
23 
24 #include "graphics/Color.h"
25 
26 #include "maths/MathUtil.h"
27 #include "graphics/SColor.h"
28 
29 #if HAVE_SSE
30 # include <xmmintrin.h>
32 #endif
33 
35 {
36  SColor4ub result;
37  result.R=clamp(int(src.X*255),0,255);
38  result.G=clamp(int(src.Y*255),0,255);
39  result.B=clamp(int(src.Z*255),0,255);
40  result.A=0xff;
41  return result;
42 }
43 
44 // on IA32, this is replaced by an SSE assembly version in ia32.cpp
46 
47 
48 // Assembler-optimized function for color conversion
49 #if HAVE_SSE
50 static SColor4ub sse_ConvertRGBColorTo4ub(const RGBColor& src)
51 {
52  const __m128 zero = _mm_setzero_ps();
53  const __m128 _255 = _mm_set_ss(255.0f);
54  __m128 r = _mm_load_ss(&src.X);
55  __m128 g = _mm_load_ss(&src.Y);
56  __m128 b = _mm_load_ss(&src.Z);
57 
58  // C = min(255, 255*max(C, 0)) ( == clamp(255*C, 0, 255) )
59  r = _mm_max_ss(r, zero);
60  g = _mm_max_ss(g, zero);
61  b = _mm_max_ss(b, zero);
62 
63  r = _mm_mul_ss(r, _255);
64  g = _mm_mul_ss(g, _255);
65  b = _mm_mul_ss(b, _255);
66 
67  r = _mm_min_ss(r, _255);
68  g = _mm_min_ss(g, _255);
69  b = _mm_min_ss(b, _255);
70 
71  // convert to integer and combine channels using bit logic
72  int ri = _mm_cvtss_si32(r);
73  int gi = _mm_cvtss_si32(g);
74  int bi = _mm_cvtss_si32(b);
75 
76  return SColor4ub(ri, gi, bi, 0xFF);
77 }
78 #endif
79 
81 {
82  if(0)
83  {
84  }
85 #if HAVE_SSE
87  {
88  ConvertRGBColorTo4ub = sse_ConvertRGBColorTo4ub;
89  }
90 #endif
91  else
92  {
93  debug_printf(L"No SSE available. Slow fallback routines will be used.\n");
94  }
95 }
u8 G
Definition: SColor.h:33
static SColor4ub fallback_ConvertRGBColorTo4ub(const RGBColor &src)
Definition: Color.cpp:34
u8 B
Definition: SColor.h:34
u8 A
Definition: SColor.h:35
float X
Definition: Vector3D.h:31
float Y
Definition: Vector3D.h:31
SColor4ub(* ConvertRGBColorTo4ub)(const RGBColor &src)
Definition: Color.cpp:45
u8 R
Definition: SColor.h:32
bool Cap(Caps cap)
Definition: x86_x64.cpp:142
float Z
Definition: Vector3D.h:31
T clamp(T value, T min, T max)
Definition: MathUtil.h:32
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142
void ColorActivateFastImpl()
Definition: Color.cpp:80