|
1 // |
|
2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
|
3 // Use of this source code is governed by a BSD-style license that can be |
|
4 // found in the LICENSE file. |
|
5 // |
|
6 |
|
7 // vertexconversion.h: A library of vertex conversion classes that can be used to build |
|
8 // the FormatConverter objects used by the buffer conversion system. |
|
9 |
|
10 #ifndef LIBGLESV2_VERTEXCONVERSION_H_ |
|
11 #define LIBGLESV2_VERTEXCONVERSION_H_ |
|
12 |
|
13 namespace rx |
|
14 { |
|
15 |
|
16 // Conversion types: |
|
17 // static const bool identity: true if this is an identity transform, false otherwise |
|
18 // static U convert(T): convert a single element from the input type to the output type |
|
19 // typedef ... OutputType: the type produced by this conversion |
|
20 |
|
21 template <class T> |
|
22 struct Identity |
|
23 { |
|
24 static const bool identity = true; |
|
25 |
|
26 typedef T OutputType; |
|
27 |
|
28 static T convert(T x) |
|
29 { |
|
30 return x; |
|
31 } |
|
32 }; |
|
33 |
|
34 template <class FromT, class ToT> |
|
35 struct Cast |
|
36 { |
|
37 static const bool identity = false; |
|
38 |
|
39 typedef ToT OutputType; |
|
40 |
|
41 static ToT convert(FromT x) |
|
42 { |
|
43 return static_cast<ToT>(x); |
|
44 } |
|
45 }; |
|
46 |
|
47 template <class T> |
|
48 struct Cast<T, T> |
|
49 { |
|
50 static const bool identity = true; |
|
51 |
|
52 typedef T OutputType; |
|
53 |
|
54 static T convert(T x) |
|
55 { |
|
56 return static_cast<T>(x); |
|
57 } |
|
58 }; |
|
59 |
|
60 template <class T> |
|
61 struct Normalize |
|
62 { |
|
63 static const bool identity = false; |
|
64 |
|
65 typedef float OutputType; |
|
66 |
|
67 static float convert(T x) |
|
68 { |
|
69 typedef std::numeric_limits<T> NL; |
|
70 float f = static_cast<float>(x); |
|
71 |
|
72 if (NL::is_signed) |
|
73 { |
|
74 // const float => VC2008 computes it at compile time |
|
75 // static const float => VC2008 computes it the first time we get here, stores it to memory with static guard and all that. |
|
76 const float divisor = 1.0f/(2*static_cast<float>(NL::max())+1); |
|
77 return (2*f+1)*divisor; |
|
78 } |
|
79 else |
|
80 { |
|
81 return f/NL::max(); |
|
82 } |
|
83 } |
|
84 }; |
|
85 |
|
86 template <class FromType, std::size_t ScaleBits> |
|
87 struct FixedToFloat |
|
88 { |
|
89 static const bool identity = false; |
|
90 |
|
91 typedef float OutputType; |
|
92 |
|
93 static float convert(FromType x) |
|
94 { |
|
95 const float divisor = 1.0f / static_cast<float>(static_cast<FromType>(1) << ScaleBits); |
|
96 return static_cast<float>(x) * divisor; |
|
97 } |
|
98 }; |
|
99 |
|
100 // Widen types: |
|
101 // static const unsigned int initialWidth: number of components before conversion |
|
102 // static const unsigned int finalWidth: number of components after conversion |
|
103 |
|
104 // Float is supported at any size. |
|
105 template <std::size_t N> |
|
106 struct NoWiden |
|
107 { |
|
108 static const std::size_t initialWidth = N; |
|
109 static const std::size_t finalWidth = N; |
|
110 }; |
|
111 |
|
112 // SHORT, norm-SHORT, norm-UNSIGNED_SHORT are supported but only with 2 or 4 components |
|
113 template <std::size_t N> |
|
114 struct WidenToEven |
|
115 { |
|
116 static const std::size_t initialWidth = N; |
|
117 static const std::size_t finalWidth = N+(N&1); |
|
118 }; |
|
119 |
|
120 template <std::size_t N> |
|
121 struct WidenToFour |
|
122 { |
|
123 static const std::size_t initialWidth = N; |
|
124 static const std::size_t finalWidth = 4; |
|
125 }; |
|
126 |
|
127 // Most types have 0 and 1 that are just that. |
|
128 template <class T> |
|
129 struct SimpleDefaultValues |
|
130 { |
|
131 static T zero() { return static_cast<T>(0); } |
|
132 static T one() { return static_cast<T>(1); } |
|
133 }; |
|
134 |
|
135 // But normalised types only store [0,1] or [-1,1] so 1.0 is represented by the max value. |
|
136 template <class T> |
|
137 struct NormalizedDefaultValues |
|
138 { |
|
139 static T zero() { return static_cast<T>(0); } |
|
140 static T one() { return std::numeric_limits<T>::max(); } |
|
141 }; |
|
142 |
|
143 // Converter: |
|
144 // static const bool identity: true if this is an identity transform (with no widening) |
|
145 // static const std::size_t finalSize: number of bytes per output vertex |
|
146 // static void convertArray(const void *in, std::size_t stride, std::size_t n, void *out): convert an array of vertices. Input may be strided, but output will be unstrided. |
|
147 |
|
148 template <class InT, class WidenRule, class Converter, class DefaultValueRule = SimpleDefaultValues<InT> > |
|
149 struct VertexDataConverter |
|
150 { |
|
151 typedef typename Converter::OutputType OutputType; |
|
152 typedef InT InputType; |
|
153 |
|
154 static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity; |
|
155 static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType); |
|
156 |
|
157 static void convertArray(const InputType *in, std::size_t stride, std::size_t n, OutputType *out) |
|
158 { |
|
159 for (std::size_t i = 0; i < n; i++) |
|
160 { |
|
161 const InputType *ein = pointerAddBytes(in, i * stride); |
|
162 |
|
163 copyComponent(out, ein, 0, static_cast<OutputType>(DefaultValueRule::zero())); |
|
164 copyComponent(out, ein, 1, static_cast<OutputType>(DefaultValueRule::zero())); |
|
165 copyComponent(out, ein, 2, static_cast<OutputType>(DefaultValueRule::zero())); |
|
166 copyComponent(out, ein, 3, static_cast<OutputType>(DefaultValueRule::one())); |
|
167 |
|
168 out += WidenRule::finalWidth; |
|
169 } |
|
170 } |
|
171 |
|
172 static void convertArray(const void *in, std::size_t stride, std::size_t n, void *out) |
|
173 { |
|
174 return convertArray(static_cast<const InputType*>(in), stride, n, static_cast<OutputType*>(out)); |
|
175 } |
|
176 |
|
177 private: |
|
178 // Advance the given pointer by a number of bytes (not pointed-to elements). |
|
179 template <class T> |
|
180 static T *pointerAddBytes(T *basePtr, std::size_t numBytes) |
|
181 { |
|
182 return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(basePtr) + numBytes); |
|
183 } |
|
184 |
|
185 static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue) |
|
186 { |
|
187 if (WidenRule::finalWidth > elementindex) |
|
188 { |
|
189 if (WidenRule::initialWidth > elementindex) |
|
190 { |
|
191 out[elementindex] = Converter::convert(in[elementindex]); |
|
192 } |
|
193 else |
|
194 { |
|
195 out[elementindex] = defaultvalue; |
|
196 } |
|
197 } |
|
198 } |
|
199 }; |
|
200 |
|
201 } |
|
202 |
|
203 #endif // LIBGLESV2_VERTEXCONVERSION_H_ |