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.
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.
10 #ifndef LIBGLESV2_VERTEXCONVERSION_H_
11 #define LIBGLESV2_VERTEXCONVERSION_H_
21 // static const bool identity: true if this is an identity transform, false otherwise
22 // static U convert(T): convert a single element from the input type to the output type
23 // typedef ... OutputType: the type produced by this conversion
28 static const bool identity = true;
38 template <class FromT, class ToT>
41 static const bool identity = false;
43 typedef ToT OutputType;
45 static ToT convert(FromT x)
47 return static_cast<ToT>(x);
54 static const bool identity = true;
60 return static_cast<T>(x);
67 static const bool identity = false;
69 typedef float OutputType;
71 static float convert(T x)
73 typedef std::numeric_limits<T> NL;
74 float f = static_cast<float>(x);
78 // const float => VC2008 computes it at compile time
79 // static const float => VC2008 computes it the first time we get here, stores it to memory with static guard and all that.
80 const float divisor = 1.0f/(2*static_cast<float>(NL::max())+1);
81 return (2*f+1)*divisor;
90 template <class FromType, std::size_t ScaleBits>
93 static const bool identity = false;
95 typedef float OutputType;
97 static float convert(FromType x)
99 const float divisor = 1.0f / static_cast<float>(static_cast<FromType>(1) << ScaleBits);
100 return static_cast<float>(x) * divisor;
105 // static const unsigned int initialWidth: number of components before conversion
106 // static const unsigned int finalWidth: number of components after conversion
108 // Float is supported at any size.
109 template <std::size_t N>
112 static const std::size_t initialWidth = N;
113 static const std::size_t finalWidth = N;
116 // SHORT, norm-SHORT, norm-UNSIGNED_SHORT are supported but only with 2 or 4 components
117 template <std::size_t N>
120 static const std::size_t initialWidth = N;
121 static const std::size_t finalWidth = N+(N&1);
124 template <std::size_t N>
127 static const std::size_t initialWidth = N;
128 static const std::size_t finalWidth = 4;
131 // Most types have 0 and 1 that are just that.
133 struct SimpleDefaultValues
135 static T zero() { return static_cast<T>(0); }
136 static T one() { return static_cast<T>(1); }
139 // But normalised types only store [0,1] or [-1,1] so 1.0 is represented by the max value.
141 struct NormalizedDefaultValues
143 static T zero() { return static_cast<T>(0); }
144 static T one() { return std::numeric_limits<T>::max(); }
148 // static const bool identity: true if this is an identity transform (with no widening)
149 // static const std::size_t finalSize: number of bytes per output vertex
150 // 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.
152 template <class InT, class WidenRule, class Converter, class DefaultValueRule = SimpleDefaultValues<InT> >
153 struct VertexDataConverter
155 typedef typename Converter::OutputType OutputType;
156 typedef InT InputType;
158 static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity;
159 static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType);
161 static void convertArray(const uint8_t *input, size_t stride, size_t n, uint8_t *output)
163 OutputType *out = reinterpret_cast<OutputType*>(output);
165 for (std::size_t i = 0; i < n; i++)
167 const InputType *ein = reinterpret_cast<const InputType*>(input + i * stride);
169 copyComponent(out, ein, 0, static_cast<OutputType>(DefaultValueRule::zero()));
170 copyComponent(out, ein, 1, static_cast<OutputType>(DefaultValueRule::zero()));
171 copyComponent(out, ein, 2, static_cast<OutputType>(DefaultValueRule::zero()));
172 copyComponent(out, ein, 3, static_cast<OutputType>(DefaultValueRule::one()));
174 out += WidenRule::finalWidth;
179 static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue)
181 if (WidenRule::finalWidth > elementindex)
183 if (WidenRule::initialWidth > elementindex)
185 out[elementindex] = Converter::convert(in[elementindex]);
189 out[elementindex] = defaultvalue;
197 #endif // LIBGLESV2_VERTEXCONVERSION_H_