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_
16
#include "libGLESv2/Context.h" // Defines Index
22
// static const bool identity: true if this is an identity transform, false otherwise
23
// static U convert(T): convert a single element from the input type to the output type
24
// typedef ... OutputType: the type produced by this conversion
29
static const bool identity = true;
39
template <class FromT, class ToT>
42
static const bool identity = false;
44
typedef ToT OutputType;
46
static ToT convert(FromT x)
48
return static_cast<ToT>(x);
55
static const bool identity = true;
61
return static_cast<T>(x);
68
static const bool identity = false;
70
typedef float OutputType;
72
static float convert(T x)
74
typedef std::numeric_limits<T> NL;
75
float f = static_cast<float>(x);
79
// const float => VC2008 computes it at compile time
80
// static const float => VC2008 computes it the first time we get here, stores it to memory with static guard and all that.
81
const float divisor = 1.0f/(2*static_cast<float>(NL::max())+1);
82
return (2*f+1)*divisor;
91
template <class FromType, std::size_t ScaleBits>
94
static const bool identity = false;
96
typedef float OutputType;
98
static float convert(FromType x)
100
const float divisor = 1.0f / static_cast<float>(static_cast<FromType>(1) << ScaleBits);
101
return static_cast<float>(x) * divisor;
106
// static const unsigned int initialWidth: number of components before conversion
107
// static const unsigned int finalWidth: number of components after conversion
109
// Float is supported at any size.
110
template <std::size_t N>
113
static const std::size_t initialWidth = N;
114
static const std::size_t finalWidth = N;
117
// SHORT, norm-SHORT, norm-UNSIGNED_SHORT are supported but only with 2 or 4 components
118
template <std::size_t N>
121
static const std::size_t initialWidth = N;
122
static const std::size_t finalWidth = N+(N&1);
125
template <std::size_t N>
128
static const std::size_t initialWidth = N;
129
static const std::size_t finalWidth = 4;
132
// Most types have 0 and 1 that are just that.
134
struct SimpleDefaultValues
136
static T zero() { return static_cast<T>(0); }
137
static T one() { return static_cast<T>(1); }
140
// But normalised types only store [0,1] or [-1,1] so 1.0 is represented by the max value.
142
struct NormalizedDefaultValues
144
static T zero() { return static_cast<T>(0); }
145
static T one() { return std::numeric_limits<T>::max(); }
149
// static const bool identity: true if this is an identity transform (with no widening)
150
// static const std::size_t finalSize: number of bytes per output vertex
151
// 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.
153
template <class InT, class WidenRule, class Converter, class DefaultValueRule = SimpleDefaultValues<InT> >
154
struct VertexDataConverter
156
typedef typename Converter::OutputType OutputType;
157
typedef InT InputType;
159
static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity;
160
static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType);
162
static void convertArray(const InputType *in, std::size_t stride, std::size_t n, OutputType *out)
164
for (std::size_t i = 0; i < n; i++)
166
const InputType *ein = pointerAddBytes(in, i * stride);
168
copyComponent(out, ein, 0, static_cast<OutputType>(DefaultValueRule::zero()));
169
copyComponent(out, ein, 1, static_cast<OutputType>(DefaultValueRule::zero()));
170
copyComponent(out, ein, 2, static_cast<OutputType>(DefaultValueRule::zero()));
171
copyComponent(out, ein, 3, static_cast<OutputType>(DefaultValueRule::one()));
173
out += WidenRule::finalWidth;
177
static void convertArray(const void *in, std::size_t stride, std::size_t n, void *out)
179
return convertArray(static_cast<const InputType*>(in), stride, n, static_cast<OutputType*>(out));
183
// Advance the given pointer by a number of bytes (not pointed-to elements).
185
static T *pointerAddBytes(T *basePtr, std::size_t numBytes)
187
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(basePtr) + numBytes);
190
static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue)
192
if (WidenRule::finalWidth > elementindex)
194
if (WidenRule::initialWidth > elementindex)
196
out[elementindex] = Converter::convert(in[elementindex]);
200
out[elementindex] = defaultvalue;
208
#endif // LIBGLESV2_VERTEXCONVERSION_H_