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 #include <limits> 14 #include <cstdint> 15 #include <cstddef> 16 17 namespace rx 18 { 19 20 // Conversion types: 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 24 25 template <class T> 26 struct Identity 27 { 28 static const bool identity = true; 29 30 typedef T OutputType; 31 32 static T convert(T x) 33 { 34 return x; 35 } 36 }; 37 38 template <class FromT, class ToT> 39 struct Cast 40 { 41 static const bool identity = false; 42 43 typedef ToT OutputType; 44 45 static ToT convert(FromT x) 46 { 47 return static_cast<ToT>(x); 48 } 49 }; 50 51 template <class T> 52 struct Cast<T, T> 53 { 54 static const bool identity = true; 55 56 typedef T OutputType; 57 58 static T convert(T x) 59 { 60 return static_cast<T>(x); 61 } 62 }; 63 64 template <class T> 65 struct Normalize 66 { 67 static const bool identity = false; 68 69 typedef float OutputType; 70 71 static float convert(T x) 72 { 73 typedef std::numeric_limits<T> NL; 74 float f = static_cast<float>(x); 75 76 if (NL::is_signed) 77 { 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; 82 } 83 else 84 { 85 return f/NL::max(); 86 } 87 } 88 }; 89 90 template <class FromType, std::size_t ScaleBits> 91 struct FixedToFloat 92 { 93 static const bool identity = false; 94 95 typedef float OutputType; 96 97 static float convert(FromType x) 98 { 99 const float divisor = 1.0f / static_cast<float>(static_cast<FromType>(1) << ScaleBits); 100 return static_cast<float>(x) * divisor; 101 } 102 }; 103 104 // Widen types: 105 // static const unsigned int initialWidth: number of components before conversion 106 // static const unsigned int finalWidth: number of components after conversion 107 108 // Float is supported at any size. 109 template <std::size_t N> 110 struct NoWiden 111 { 112 static const std::size_t initialWidth = N; 113 static const std::size_t finalWidth = N; 114 }; 115 116 // SHORT, norm-SHORT, norm-UNSIGNED_SHORT are supported but only with 2 or 4 components 117 template <std::size_t N> 118 struct WidenToEven 119 { 120 static const std::size_t initialWidth = N; 121 static const std::size_t finalWidth = N+(N&1); 122 }; 123 124 template <std::size_t N> 125 struct WidenToFour 126 { 127 static const std::size_t initialWidth = N; 128 static const std::size_t finalWidth = 4; 129 }; 130 131 // Most types have 0 and 1 that are just that. 132 template <class T> 133 struct SimpleDefaultValues 134 { 135 static T zero() { return static_cast<T>(0); } 136 static T one() { return static_cast<T>(1); } 137 }; 138 139 // But normalised types only store [0,1] or [-1,1] so 1.0 is represented by the max value. 140 template <class T> 141 struct NormalizedDefaultValues 142 { 143 static T zero() { return static_cast<T>(0); } 144 static T one() { return std::numeric_limits<T>::max(); } 145 }; 146 147 // Converter: 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. 151 152 template <class InT, class WidenRule, class Converter, class DefaultValueRule = SimpleDefaultValues<InT> > 153 struct VertexDataConverter 154 { 155 typedef typename Converter::OutputType OutputType; 156 typedef InT InputType; 157 158 static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity; 159 static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType); 160 161 static void convertArray(const uint8_t *input, size_t stride, size_t n, uint8_t *output) 162 { 163 OutputType *out = reinterpret_cast<OutputType*>(output); 164 165 for (std::size_t i = 0; i < n; i++) 166 { 167 const InputType *ein = reinterpret_cast<const InputType*>(input + i * stride); 168 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())); 173 174 out += WidenRule::finalWidth; 175 } 176 } 177 178 private: 179 static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue) 180 { 181 if (WidenRule::finalWidth > elementindex) 182 { 183 if (WidenRule::initialWidth > elementindex) 184 { 185 out[elementindex] = Converter::convert(in[elementindex]); 186 } 187 else 188 { 189 out[elementindex] = defaultvalue; 190 } 191 } 192 } 193 }; 194 195 } 196 197 #endif // LIBGLESV2_VERTEXCONVERSION_H_ 198