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