webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
vertexconversion.h
Go to the documentation of this file.
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 LIBANGLE_RENDERER_D3D_D3D9_VERTEXCONVERSION_H_
11 #define LIBANGLE_RENDERER_D3D_D3D9_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>
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>
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>
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>
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>
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,
153  class WidenRule,
154  class Converter,
155  class DefaultValueRule = SimpleDefaultValues<InT>>
157 {
158  typedef typename Converter::OutputType OutputType;
159  typedef InT InputType;
160 
161  static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity;
162  static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType);
163 
164  static void convertArray(const uint8_t *input, size_t stride, size_t n, uint8_t *output)
165  {
166  OutputType *out = reinterpret_cast<OutputType*>(output);
167 
168  for (std::size_t i = 0; i < n; i++)
169  {
170  const InputType *ein = reinterpret_cast<const InputType*>(input + i * stride);
171 
172  copyComponent(out, ein, 0, static_cast<OutputType>(DefaultValueRule::zero()));
173  copyComponent(out, ein, 1, static_cast<OutputType>(DefaultValueRule::zero()));
174  copyComponent(out, ein, 2, static_cast<OutputType>(DefaultValueRule::zero()));
175  copyComponent(out, ein, 3, static_cast<OutputType>(DefaultValueRule::one()));
176 
177  out += WidenRule::finalWidth;
178  }
179  }
180 
181  private:
182  static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue)
183  {
184  if (WidenRule::finalWidth > elementindex)
185  {
186  if (WidenRule::initialWidth > elementindex)
187  {
188  out[elementindex] = Converter::convert(in[elementindex]);
189  }
190  else
191  {
192  out[elementindex] = defaultvalue;
193  }
194  }
195  }
196 };
197 
198 }
199 
200 #endif // LIBANGLE_RENDERER_D3D_D3D9_VERTEXCONVERSION_H_
static const bool identity
Definition: vertexconversion.h:28
static T convert(T x)
Definition: vertexconversion.h:32
static T zero()
Definition: vertexconversion.h:143
Definition: vertexconversion.h:156
static T zero()
Definition: vertexconversion.h:135
static T one()
Definition: vertexconversion.h:136
static float convert(T x)
Definition: vertexconversion.h:71
Definition: vertexconversion.h:141
Definition: vertexconversion.h:39
ToT OutputType
Definition: vertexconversion.h:43
GLuint divisor
Definition: gl2ext.h:1105
Definition: vertexconversion.h:118
std::integral_constant< std::size_t, V > size_t
Definition: Brigand.h:447
static T one()
Definition: vertexconversion.h:144
#define output
Definition: wire_format_lite.h:418
T OutputType
Definition: vertexconversion.h:30
EGLStreamKHR EGLint n
Definition: eglext.h:984
float OutputType
Definition: vertexconversion.h:69
static T convert(T x)
Definition: vertexconversion.h:58
Definition: mathutil.h:804
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
EGLSurface EGLint x
Definition: eglext.h:950
int int * out
Definition: gcc-loops.cpp:206
unsigned char uint8_t
Definition: ptypes.h:89
constexpr U convert(U v)
Definition: Optional.h:278
#define N
Definition: gcc-loops.cpp:14
GLfloat f
Definition: gl2.h:417
static ToT convert(FromT x)
Definition: vertexconversion.h:45
Converter::OutputType OutputType
Definition: vertexconversion.h:158
float OutputType
Definition: vertexconversion.h:95
double max
Definition: DeviceProximityEvent.idl:32
for i
Definition: complexityMeasures.m:24
T OutputType
Definition: vertexconversion.h:56
Definition: vertexconversion.h:125
string input
Definition: tokenizer_unittest.cc:198
Definition: vertexconversion.h:91
static void convertArray(const uint8_t *input, size_t stride, size_t n, uint8_t *output)
Definition: vertexconversion.h:164
static float convert(FromType x)
Definition: vertexconversion.h:97
Definition: vertexconversion.h:26
Definition: vertexconversion.h:133
EGLImageKHR EGLint EGLint EGLint * stride
Definition: eglext.h:851
InT InputType
Definition: vertexconversion.h:159
Definition: vertexconversion.h:65
Definition: vertexconversion.h:110