Home | History | Annotate | Download | only in libGLESv2
      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 bool SamplerState::swizzleRequired() const
     18 {
     19     return swizzleRed != GL_RED || swizzleGreen != GL_GREEN ||
     20            swizzleBlue != GL_BLUE || swizzleAlpha != GL_ALPHA;
     21 }
     22 
     23 static void MinMax(int a, int b, int *minimum, int *maximum)
     24 {
     25     if (a < b)
     26     {
     27         *minimum = a;
     28         *maximum = b;
     29     }
     30     else
     31     {
     32         *minimum = b;
     33         *maximum = a;
     34     }
     35 }
     36 
     37 bool ClipRectangle(const Rectangle &source, const Rectangle &clip, Rectangle *intersection)
     38 {
     39     int minSourceX, maxSourceX, minSourceY, maxSourceY;
     40     MinMax(source.x, source.x + source.width, &minSourceX, &maxSourceX);
     41     MinMax(source.y, source.y + source.height, &minSourceY, &maxSourceY);
     42 
     43     int minClipX, maxClipX, minClipY, maxClipY;
     44     MinMax(clip.x, clip.x + clip.width, &minClipX, &maxClipX);
     45     MinMax(clip.y, clip.y + clip.height, &minClipY, &maxClipY);
     46 
     47     if (minSourceX >= maxClipX || maxSourceX <= minClipX || minSourceY >= maxClipY || maxSourceY <= minClipY)
     48     {
     49         if (intersection)
     50         {
     51             intersection->x = minSourceX;
     52             intersection->y = maxSourceY;
     53             intersection->width = maxSourceX - minSourceX;
     54             intersection->height = maxSourceY - minSourceY;
     55         }
     56 
     57         return false;
     58     }
     59     else
     60     {
     61         if (intersection)
     62         {
     63             intersection->x = std::max(minSourceX, minClipX);
     64             intersection->y = std::max(minSourceY, minClipY);
     65             intersection->width  = std::min(maxSourceX, maxClipX) - std::max(minSourceX, minClipX);
     66             intersection->height = std::min(maxSourceY, maxClipY) - std::max(minSourceY, minClipY);
     67         }
     68 
     69         return true;
     70     }
     71 }
     72 
     73 VertexFormat::VertexFormat()
     74     : mType(GL_NONE),
     75       mNormalized(GL_FALSE),
     76       mComponents(0),
     77       mPureInteger(false)
     78 {}
     79 
     80 VertexFormat::VertexFormat(GLenum type, GLboolean normalized, GLuint components, bool pureInteger)
     81     : mType(type),
     82       mNormalized(normalized),
     83       mComponents(components),
     84       mPureInteger(pureInteger)
     85 {
     86     // Float data can not be normalized, so ignore the user setting
     87     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
     88     {
     89         mNormalized = GL_FALSE;
     90     }
     91 }
     92 
     93 VertexFormat::VertexFormat(const VertexAttribute &attribute)
     94     : mType(attribute.mType),
     95       mNormalized(attribute.mNormalized ? GL_TRUE : GL_FALSE),
     96       mComponents(attribute.mSize),
     97       mPureInteger(attribute.mPureInteger)
     98 {
     99     // Ensure we aren't initializing a vertex format which should be using
    100     // the current-value type
    101     ASSERT(attribute.mArrayEnabled);
    102 
    103     // Float data can not be normalized, so ignore the user setting
    104     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
    105     {
    106         mNormalized = GL_FALSE;
    107     }
    108 }
    109 
    110 VertexFormat::VertexFormat(const VertexAttribute &attribute, GLenum currentValueType)
    111     : mType(attribute.mType),
    112       mNormalized(attribute.mNormalized ? GL_TRUE : GL_FALSE),
    113       mComponents(attribute.mSize),
    114       mPureInteger(attribute.mPureInteger)
    115 {
    116     if (!attribute.mArrayEnabled)
    117     {
    118         mType = currentValueType;
    119         mNormalized = GL_FALSE;
    120         mComponents = 4;
    121         mPureInteger = (currentValueType != GL_FLOAT);
    122     }
    123 
    124     // Float data can not be normalized, so ignore the user setting
    125     if (mType == GL_FLOAT || mType == GL_HALF_FLOAT || mType == GL_FIXED)
    126     {
    127         mNormalized = GL_FALSE;
    128     }
    129 }
    130 
    131 void VertexFormat::GetInputLayout(VertexFormat *inputLayout,
    132                                   ProgramBinary *programBinary,
    133                                   const VertexAttribute *attributes,
    134                                   const gl::VertexAttribCurrentValueData *currentValues)
    135 {
    136     for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
    137     {
    138         int semanticIndex = programBinary->getSemanticIndex(attributeIndex);
    139 
    140         if (semanticIndex != -1)
    141         {
    142             inputLayout[semanticIndex] = VertexFormat(attributes[attributeIndex], currentValues[attributeIndex].Type);
    143         }
    144     }
    145 }
    146 
    147 bool VertexFormat::operator==(const VertexFormat &other) const
    148 {
    149     return (mType == other.mType                &&
    150             mComponents == other.mComponents    &&
    151             mNormalized == other.mNormalized    &&
    152             mPureInteger == other.mPureInteger  );
    153 }
    154 
    155 bool VertexFormat::operator!=(const VertexFormat &other) const
    156 {
    157     return !(*this == other);
    158 }
    159 
    160 bool VertexFormat::operator<(const VertexFormat& other) const
    161 {
    162     if (mType != other.mType)
    163     {
    164         return mType < other.mType;
    165     }
    166     if (mNormalized != other.mNormalized)
    167     {
    168         return mNormalized < other.mNormalized;
    169     }
    170     if (mComponents != other.mComponents)
    171     {
    172         return mComponents < other.mComponents;
    173     }
    174     return mPureInteger < other.mPureInteger;
    175 }
    176 
    177 }
    178