Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / angletypes.cpp
1 //
2 // Copyright (c) 2013 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 // angletypes.h : Defines a variety of structures and enum types that are used throughout libGLESv2
8
9 #include "libGLESv2/angletypes.h"
10 #include "libGLESv2/ProgramBinary.h"
11 #include "libGLESv2/VertexAttribute.h"
12 #include "libGLESv2/State.h"
13 #include "libGLESv2/VertexArray.h"
14
15 namespace gl
16 {
17
18 SamplerState::SamplerState()
19     : minFilter(GL_NEAREST_MIPMAP_LINEAR),
20       magFilter(GL_LINEAR),
21       wrapS(GL_REPEAT),
22       wrapT(GL_REPEAT),
23       wrapR(GL_REPEAT),
24       maxAnisotropy(1.0f),
25       baseLevel(0),
26       maxLevel(1000),
27       minLod(-1000.0f),
28       maxLod(1000.0f),
29       compareMode(GL_NONE),
30       compareFunc(GL_LEQUAL),
31       swizzleRed(GL_RED),
32       swizzleGreen(GL_GREEN),
33       swizzleBlue(GL_BLUE),
34       swizzleAlpha(GL_ALPHA)
35 {}
36
37 bool SamplerState::swizzleRequired() const
38 {
39     return swizzleRed != GL_RED || swizzleGreen != GL_GREEN ||
40            swizzleBlue != GL_BLUE || swizzleAlpha != GL_ALPHA;
41 }
42
43 static void MinMax(int a, int b, int *minimum, int *maximum)
44 {
45     if (a < b)
46     {
47         *minimum = a;
48         *maximum = b;
49     }
50     else
51     {
52         *minimum = b;
53         *maximum = a;
54     }
55 }
56
57 bool ClipRectangle(const Rectangle &source, const Rectangle &clip, Rectangle *intersection)
58 {
59     int minSourceX, maxSourceX, minSourceY, maxSourceY;
60     MinMax(source.x, source.x + source.width, &minSourceX, &maxSourceX);
61     MinMax(source.y, source.y + source.height, &minSourceY, &maxSourceY);
62
63     int minClipX, maxClipX, minClipY, maxClipY;
64     MinMax(clip.x, clip.x + clip.width, &minClipX, &maxClipX);
65     MinMax(clip.y, clip.y + clip.height, &minClipY, &maxClipY);
66
67     if (minSourceX >= maxClipX || maxSourceX <= minClipX || minSourceY >= maxClipY || maxSourceY <= minClipY)
68     {
69         if (intersection)
70         {
71             intersection->x = minSourceX;
72             intersection->y = maxSourceY;
73             intersection->width = maxSourceX - minSourceX;
74             intersection->height = maxSourceY - minSourceY;
75         }
76
77         return false;
78     }
79     else
80     {
81         if (intersection)
82         {
83             intersection->x = std::max(minSourceX, minClipX);
84             intersection->y = std::max(minSourceY, minClipY);
85             intersection->width  = std::min(maxSourceX, maxClipX) - std::max(minSourceX, minClipX);
86             intersection->height = std::min(maxSourceY, maxClipY) - std::max(minSourceY, minClipY);
87         }
88
89         return true;
90     }
91 }
92
93 VertexFormat::VertexFormat()
94     : mType(GL_NONE),
95       mNormalized(GL_FALSE),
96       mComponents(0),
97       mPureInteger(false)
98 {}
99
100 VertexFormat::VertexFormat(GLenum type, GLboolean normalized, GLuint components, bool pureInteger)
101     : mType(type),
102       mNormalized(normalized),
103       mComponents(components),
104       mPureInteger(pureInteger)
105 {
106     // Float data can not be normalized, so ignore the user setting
107     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
108     {
109         mNormalized = GL_FALSE;
110     }
111 }
112
113 VertexFormat::VertexFormat(const VertexAttribute &attrib)
114     : mType(attrib.type),
115       mNormalized(attrib.normalized ? GL_TRUE : GL_FALSE),
116       mComponents(attrib.size),
117       mPureInteger(attrib.pureInteger)
118 {
119     // Ensure we aren't initializing a vertex format which should be using
120     // the current-value type
121     ASSERT(attrib.enabled);
122
123     // Float data can not be normalized, so ignore the user setting
124     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
125     {
126         mNormalized = GL_FALSE;
127     }
128 }
129
130 VertexFormat::VertexFormat(const VertexAttribute &attrib, GLenum currentValueType)
131     : mType(attrib.type),
132       mNormalized(attrib.normalized ? GL_TRUE : GL_FALSE),
133       mComponents(attrib.size),
134       mPureInteger(attrib.pureInteger)
135 {
136     if (!attrib.enabled)
137     {
138         mType = currentValueType;
139         mNormalized = GL_FALSE;
140         mComponents = 4;
141         mPureInteger = (currentValueType != GL_FLOAT);
142     }
143
144     // Float data can not be normalized, so ignore the user setting
145     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
146     {
147         mNormalized = GL_FALSE;
148     }
149 }
150
151 void VertexFormat::GetInputLayout(VertexFormat *inputLayout,
152                                   ProgramBinary *programBinary,
153                                   const State &state)
154 {
155     const VertexAttribute *vertexAttributes = state.getVertexArray()->getVertexAttributes();
156     for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
157     {
158         int semanticIndex = programBinary->getSemanticIndex(attributeIndex);
159
160         if (semanticIndex != -1)
161         {
162             inputLayout[semanticIndex] = VertexFormat(vertexAttributes[attributeIndex], state.getVertexAttribCurrentValue(attributeIndex).Type);
163         }
164     }
165 }
166
167 bool VertexFormat::operator==(const VertexFormat &other) const
168 {
169     return (mType == other.mType                &&
170             mComponents == other.mComponents    &&
171             mNormalized == other.mNormalized    &&
172             mPureInteger == other.mPureInteger  );
173 }
174
175 bool VertexFormat::operator!=(const VertexFormat &other) const
176 {
177     return !(*this == other);
178 }
179
180 bool VertexFormat::operator<(const VertexFormat& other) const
181 {
182     if (mType != other.mType)
183     {
184         return mType < other.mType;
185     }
186     if (mNormalized != other.mNormalized)
187     {
188         return mNormalized < other.mNormalized;
189     }
190     if (mComponents != other.mComponents)
191     {
192         return mComponents < other.mComponents;
193     }
194     return mPureInteger < other.mPureInteger;
195 }
196
197 bool Box::operator==(const Box &other) const
198 {
199     return (x == other.x && y == other.y && z == other.z &&
200             width == other.width && height == other.height && depth == other.depth);
201 }
202
203 bool Box::operator!=(const Box &other) const
204 {
205     return !(*this == other);
206 }
207
208 }