Merge the ProgramBinary class into Program.

BUG=angle:731

Change-Id: I2ee97155841dc62f04bb71c1f2035d210fd3883c
Reviewed-on: https://chromium-review.googlesource.com/232694
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/Program.cpp b/src/libANGLE/Program.cpp
index 6453454..0c086b4 100644
--- a/src/libANGLE/Program.cpp
+++ b/src/libANGLE/Program.cpp
@@ -8,14 +8,47 @@
 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
 
 #include "libANGLE/Program.h"
-#include "libANGLE/ProgramBinary.h"
 #include "libANGLE/ResourceManager.h"
+#include "libANGLE/Data.h"
+#include "libANGLE/features.h"
 #include "libANGLE/renderer/Renderer.h"
+#include "libANGLE/renderer/ProgramImpl.h"
+#include "libANGLE/renderer/d3d/ShaderD3D.h"
+#include "libANGLE/renderer/d3d/VertexDataManager.h"
+
+#include "common/debug.h"
+#include "common/version.h"
+#include "common/utilities.h"
+#include "common/platform.h"
+#include "common/blocklayout.h"
+
+#include <algorithm>
 
 namespace gl
 {
 const char * const g_fakepath = "C:\\fakepath";
 
+namespace
+{
+
+unsigned int ParseAndStripArrayIndex(std::string* name)
+{
+    unsigned int subscript = GL_INVALID_INDEX;
+
+    // Strip any trailing array operator and retrieve the subscript
+    size_t open = name->find_last_of('[');
+    size_t close = name->find_last_of(']');
+    if (open != std::string::npos && close == name->length() - 1)
+    {
+        subscript = atoi(name->substr(open + 1).c_str());
+        name->erase(open);
+    }
+
+    return subscript;
+}
+
+}
+
 AttributeBindings::AttributeBindings()
 {
 }
@@ -136,17 +169,41 @@
     }
 }
 
-Program::Program(rx::Renderer *renderer, ResourceManager *manager, GLuint handle) : mResourceManager(manager), mHandle(handle)
+VariableLocation::VariableLocation()
+    : name(), element(0), index(0)
 {
-    mFragmentShader = NULL;
-    mVertexShader = NULL;
-    mProgramBinary.set(NULL);
-    mDeleteStatus = false;
-    mLinked = false;
-    mRefCount = 0;
-    mRenderer = renderer;
+}
+
+VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
+    : name(name), element(element), index(index)
+{
+}
+
+LinkedVarying::LinkedVarying()
+{
+}
+
+LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
+                             unsigned int semanticIndex, unsigned int semanticIndexCount)
+    : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
+{
+}
+
+Program::Program(rx::ProgramImpl *impl, ResourceManager *manager, GLuint handle)
+    : mProgram(impl),
+      mValidated(false),
+      mFragmentShader(NULL),
+      mVertexShader(NULL),
+      mLinked(false),
+      mDeleteStatus(false),
+      mRefCount(0),
+      mResourceManager(manager),
+      mHandle(handle)
+{
+    ASSERT(mProgram);
 
     resetUniformBlockBindings();
+    unlink();
 }
 
 Program::~Program()
@@ -162,6 +219,8 @@
     {
         mFragmentShader->release();
     }
+
+    SafeDelete(mProgram);
 }
 
 bool Program::attachShader(Shader *shader)
@@ -251,15 +310,59 @@
     mInfoLog.reset();
     resetUniformBlockBindings();
 
-    mProgramBinary.set(new ProgramBinary(mRenderer->createProgram()));
-    LinkResult result = mProgramBinary->link(data, mInfoLog, mAttributeBindings, mFragmentShader, mVertexShader,
-                                             mTransformFeedbackVaryings, mTransformFeedbackBufferMode);
-    if (result.error.isError())
+    if (!mFragmentShader || !mFragmentShader->isCompiled())
+    {
+        return Error(GL_NO_ERROR);
+    }
+    ASSERT(mFragmentShader->getType() == GL_FRAGMENT_SHADER);
+
+    if (!mVertexShader || !mVertexShader->isCompiled())
+    {
+        return Error(GL_NO_ERROR);
+    }
+    ASSERT(mVertexShader->getType() == GL_VERTEX_SHADER);
+
+    int registers;
+    std::vector<LinkedVarying> linkedVaryings;
+    rx::LinkResult result = mProgram->link(data, mInfoLog, mFragmentShader, mVertexShader, mTransformFeedbackVaryings, mTransformFeedbackBufferMode,
+                                           &registers, &linkedVaryings, &mOutputVariables);
+    if (result.error.isError() || !result.linkSuccess)
     {
         return result.error;
     }
 
-    mLinked = result.linkSuccess;
+    if (!linkAttributes(mInfoLog, mAttributeBindings, mVertexShader))
+    {
+        return Error(GL_NO_ERROR);
+    }
+
+    if (!mProgram->linkUniforms(mInfoLog, *mVertexShader, *mFragmentShader, *data.caps))
+    {
+        return Error(GL_NO_ERROR);
+    }
+
+    if (!linkUniformBlocks(mInfoLog, *mVertexShader, *mFragmentShader, *data.caps))
+    {
+        return Error(GL_NO_ERROR);
+    }
+
+    if (!gatherTransformFeedbackLinkedVaryings(mInfoLog, linkedVaryings, mTransformFeedbackVaryings,
+                                               mTransformFeedbackBufferMode, &mProgram->getTransformFeedbackLinkedVaryings(), *data.caps))
+    {
+        return Error(GL_NO_ERROR);
+    }
+
+    // TODO: The concept of "executables" is D3D only, and as such this belongs in ProgramD3D. It must be called,
+    // however, last in this function, so it can't simply be moved to ProgramD3D::link without further shuffling.
+    result = mProgram->compileProgramExecutables(mInfoLog, mFragmentShader, mVertexShader, registers);
+    if (result.error.isError() || !result.linkSuccess)
+    {
+        mInfoLog.append("Failed to create D3D shaders.");
+        unlink(false);
+        return result.error;
+    }
+
+    mLinked = true;
     return gl::Error(GL_NO_ERROR);
 }
 
@@ -294,7 +397,15 @@
         }
     }
 
-    mProgramBinary.set(NULL);
+    std::fill(mLinkedAttribute, mLinkedAttribute + ArraySize(mLinkedAttribute), sh::Attribute());
+    std::fill(mSemanticIndex, mSemanticIndex + ArraySize(mSemanticIndex), -1);
+    std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
+    mOutputVariables.clear();
+
+    mProgram->reset();
+
+    mValidated = false;
+
     mLinked = false;
 }
 
@@ -303,27 +414,146 @@
     return mLinked;
 }
 
-ProgramBinary* Program::getProgramBinary() const
-{
-    return mProgramBinary.get();
-}
-
-Error Program::setProgramBinary(GLenum binaryFormat, const void *binary, GLsizei length)
+Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
 {
     unlink(false);
 
-    mInfoLog.reset();
+#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
+    return Error(GL_NO_ERROR);
+#else
+    ASSERT(binaryFormat == mProgram->getBinaryFormat());
 
-    mProgramBinary.set(new ProgramBinary(mRenderer->createProgram()));
-    LinkResult result = mProgramBinary->load(mInfoLog, binaryFormat, binary, length);
-    if (result.error.isError())
+    BinaryInputStream stream(binary, length);
+
+    GLenum format = stream.readInt<GLenum>();
+    if (format != mProgram->getBinaryFormat())
     {
-        mProgramBinary.set(NULL);
+        mInfoLog.append("Invalid program binary format.");
+        return Error(GL_NO_ERROR);
+    }
+
+    int majorVersion = stream.readInt<int>();
+    int minorVersion = stream.readInt<int>();
+    if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
+    {
+        mInfoLog.append("Invalid program binary version.");
+        return Error(GL_NO_ERROR);
+    }
+
+    unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
+    stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
+    if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
+    {
+        mInfoLog.append("Invalid program binary version.");
+        return Error(GL_NO_ERROR);
+    }
+
+    // TODO: move to ProgramImpl
+    int compileFlags = stream.readInt<int>();
+    if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
+    {
+        mInfoLog.append("Mismatched compilation flags.");
+        return Error(GL_NO_ERROR);
+    }
+
+    for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
+    {
+        stream.readInt(&mLinkedAttribute[i].type);
+        stream.readString(&mLinkedAttribute[i].name);
+        stream.readInt(&mProgram->getShaderAttributes()[i].type);
+        stream.readString(&mProgram->getShaderAttributes()[i].name);
+        stream.readInt(&mSemanticIndex[i]);
+    }
+
+    initAttributesByLayout();
+
+    rx::LinkResult result = mProgram->load(mInfoLog, &stream);
+    if (result.error.isError() || !result.linkSuccess)
+    {
         return result.error;
     }
 
-    mLinked = result.linkSuccess;
+    mLinked = true;
     return Error(GL_NO_ERROR);
+#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
+}
+
+Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
+{
+    if (binaryFormat)
+    {
+        *binaryFormat = mProgram->getBinaryFormat();
+    }
+
+    BinaryOutputStream stream;
+
+    stream.writeInt(mProgram->getBinaryFormat());
+    stream.writeInt(ANGLE_MAJOR_VERSION);
+    stream.writeInt(ANGLE_MINOR_VERSION);
+    stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
+
+    // TODO: move to ProgramImpl
+    stream.writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
+
+    for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
+    {
+        stream.writeInt(mLinkedAttribute[i].type);
+        stream.writeString(mLinkedAttribute[i].name);
+        stream.writeInt(mProgram->getShaderAttributes()[i].type);
+        stream.writeString(mProgram->getShaderAttributes()[i].name);
+        stream.writeInt(mSemanticIndex[i]);
+    }
+
+    gl::Error error = mProgram->save(&stream);
+    if (error.isError())
+    {
+        return error;
+    }
+
+    GLsizei streamLength = stream.length();
+    const void *streamData = stream.data();
+
+    if (streamLength > bufSize)
+    {
+        if (length)
+        {
+            *length = 0;
+        }
+
+        // TODO: This should be moved to the validation layer but computing the size of the binary before saving
+        // it causes the save to happen twice.  It may be possible to write the binary to a separate buffer, validate
+        // sizes and then copy it.
+        return Error(GL_INVALID_OPERATION);
+    }
+
+    if (binary)
+    {
+        char *ptr = reinterpret_cast<char*>(binary);
+
+        memcpy(ptr, streamData, streamLength);
+        ptr += streamLength;
+
+        ASSERT(ptr - streamLength == binary);
+    }
+
+    if (length)
+    {
+        *length = streamLength;
+    }
+
+    return Error(GL_NO_ERROR);
+}
+
+GLint Program::getBinaryLength() const
+{
+    GLint length;
+    Error error = saveBinary(NULL, NULL, std::numeric_limits<GLint>::max(), &length);
+    if (error.isError())
+    {
+        return 0;
+    }
+
+    return length;
 }
 
 void Program::release()
@@ -346,19 +576,6 @@
     return mRefCount;
 }
 
-GLint Program::getProgramBinaryLength() const
-{
-    ProgramBinary *programBinary = mProgramBinary.get();
-    if (programBinary)
-    {
-        return programBinary->getLength();
-    }
-    else
-    {
-        return 0;
-    }
-}
-
 int Program::getInfoLogLength() const
 {
     return mInfoLog.getLength();
@@ -399,12 +616,64 @@
     }
 }
 
+GLuint Program::getAttributeLocation(const std::string &name)
+{
+    for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
+    {
+        if (mLinkedAttribute[index].name == name)
+        {
+            return index;
+        }
+    }
+
+    return -1;
+}
+
+int Program::getSemanticIndex(int attributeIndex)
+{
+    ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
+
+    return mSemanticIndex[attributeIndex];
+}
+
 void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    if (mLinked)
     {
-        programBinary->getActiveAttribute(index, bufsize, length, size, type, name);
+        // Skip over inactive attributes
+        unsigned int activeAttribute = 0;
+        unsigned int attribute;
+        for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
+        {
+            if (mLinkedAttribute[attribute].name.empty())
+            {
+                continue;
+            }
+
+            if (activeAttribute == index)
+            {
+                break;
+            }
+
+            activeAttribute++;
+        }
+
+        if (bufsize > 0)
+        {
+            const char *string = mLinkedAttribute[attribute].name.c_str();
+
+            strncpy(name, string, bufsize);
+            name[bufsize - 1] = '\0';
+
+            if (length)
+            {
+                *length = strlen(name);
+            }
+        }
+
+        *size = 1;   // Always a single 'type' instance
+
+        *type = mLinkedAttribute[attribute].type;
     }
     else
     {
@@ -412,7 +681,7 @@
         {
             name[0] = '\0';
         }
-        
+
         if (length)
         {
             *length = 0;
@@ -425,36 +694,102 @@
 
 GLint Program::getActiveAttributeCount()
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    int count = 0;
+
+    if (mLinked)
     {
-        return programBinary->getActiveAttributeCount();
+        for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
+        {
+            if (!mLinkedAttribute[attributeIndex].name.empty())
+            {
+                count++;
+            }
+        }
     }
-    else
-    {
-        return 0;
-    }
+
+    return count;
 }
 
 GLint Program::getActiveAttributeMaxLength()
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    int maxLength = 0;
+
+    if (mLinked)
     {
-        return programBinary->getActiveAttributeMaxLength();
+        for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
+        {
+            if (!mLinkedAttribute[attributeIndex].name.empty())
+            {
+                maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
+            }
+        }
     }
-    else
+
+    return maxLength;
+}
+
+// Returns one more than the highest sampler index used.
+GLint Program::getUsedSamplerRange(SamplerType type)
+{
+    return mProgram->getUsedSamplerRange(type);
+}
+
+bool Program::usesPointSize() const
+{
+    return mProgram->usesPointSize();
+}
+
+GLint Program::getSamplerMapping(SamplerType type, unsigned int samplerIndex, const Caps &caps)
+{
+    return mProgram->getSamplerMapping(type, samplerIndex, caps);
+}
+
+GLenum Program::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
+{
+    return mProgram->getSamplerTextureType(type, samplerIndex);
+}
+
+GLint Program::getFragDataLocation(const std::string &name) const
+{
+    std::string baseName(name);
+    unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
+    for (auto locationIt = mOutputVariables.begin(); locationIt != mOutputVariables.end(); locationIt++)
     {
-        return 0;
+        const VariableLocation &outputVariable = locationIt->second;
+        if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
+        {
+            return static_cast<GLint>(locationIt->first);
+        }
     }
+    return -1;
 }
 
 void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    if (mLinked)
     {
-        return programBinary->getActiveUniform(index, bufsize, length, size, type, name);
+        ASSERT(index < mProgram->getUniforms().size());   // index must be smaller than getActiveUniformCount()
+        LinkedUniform *uniform = mProgram->getUniforms()[index];
+
+        if (bufsize > 0)
+        {
+            std::string string = uniform->name;
+            if (uniform->isArray())
+            {
+                string += "[0]";
+            }
+
+            strncpy(name, string.c_str(), bufsize);
+            name[bufsize - 1] = '\0';
+
+            if (length)
+            {
+                *length = strlen(name);
+            }
+        }
+
+        *size = uniform->elementCount();
+        *type = uniform->type;
     }
     else
     {
@@ -475,10 +810,9 @@
 
 GLint Program::getActiveUniformCount()
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    if (mLinked)
     {
-        return programBinary->getActiveUniformCount();
+        return mProgram->getUniforms().size();
     }
     else
     {
@@ -488,15 +822,203 @@
 
 GLint Program::getActiveUniformMaxLength()
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    int maxLength = 0;
+
+    if (mLinked)
     {
-        return programBinary->getActiveUniformMaxLength();
+        unsigned int numUniforms = mProgram->getUniforms().size();
+        for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
+        {
+            if (!mProgram->getUniforms()[uniformIndex]->name.empty())
+            {
+                int length = (int)(mProgram->getUniforms()[uniformIndex]->name.length() + 1);
+                if (mProgram->getUniforms()[uniformIndex]->isArray())
+                {
+                    length += 3;  // Counting in "[0]".
+                }
+                maxLength = std::max(length, maxLength);
+            }
+        }
     }
-    else
+
+    return maxLength;
+}
+
+GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
+{
+    const gl::LinkedUniform& uniform = *mProgram->getUniforms()[index];
+    switch (pname)
     {
-        return 0;
+      case GL_UNIFORM_TYPE:         return static_cast<GLint>(uniform.type);
+      case GL_UNIFORM_SIZE:         return static_cast<GLint>(uniform.elementCount());
+      case GL_UNIFORM_NAME_LENGTH:  return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
+      case GL_UNIFORM_BLOCK_INDEX:  return uniform.blockIndex;
+      case GL_UNIFORM_OFFSET:       return uniform.blockInfo.offset;
+      case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
+      case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
+      case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
+      default:
+        UNREACHABLE();
+        break;
     }
+    return 0;
+}
+
+bool Program::isValidUniformLocation(GLint location) const
+{
+    ASSERT(rx::IsIntegerCastSafe<GLint>(mProgram->getUniformIndices().size()));
+    return (location >= 0 && location < static_cast<GLint>(mProgram->getUniformIndices().size()));
+}
+
+LinkedUniform *Program::getUniformByLocation(GLint location) const
+{
+    return mProgram->getUniformByLocation(location);
+}
+
+LinkedUniform *Program::getUniformByName(const std::string &name) const
+{
+    return mProgram->getUniformByName(name);
+}
+
+GLint Program::getUniformLocation(const std::string &name)
+{
+    return mProgram->getUniformLocation(name);
+}
+
+GLuint Program::getUniformIndex(const std::string &name)
+{
+    return mProgram->getUniformIndex(name);
+}
+
+void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
+{
+    mProgram->setUniform1fv(location, count, v);
+}
+
+void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
+{
+    mProgram->setUniform2fv(location, count, v);
+}
+
+void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
+{
+    mProgram->setUniform3fv(location, count, v);
+}
+
+void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
+{
+    mProgram->setUniform4fv(location, count, v);
+}
+
+void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
+{
+    mProgram->setUniform1iv(location, count, v);
+}
+
+void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
+{
+    mProgram->setUniform2iv(location, count, v);
+}
+
+void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
+{
+    mProgram->setUniform3iv(location, count, v);
+}
+
+void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
+{
+    mProgram->setUniform4iv(location, count, v);
+}
+
+void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
+{
+    mProgram->setUniform1uiv(location, count, v);
+}
+
+void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
+{
+    mProgram->setUniform2uiv(location, count, v);
+}
+
+void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
+{
+    mProgram->setUniform3uiv(location, count, v);
+}
+
+void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
+{
+    mProgram->setUniform4uiv(location, count, v);
+}
+
+void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix2fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix3fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix4fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
+}
+
+void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
+{
+    mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
+}
+
+void Program::getUniformfv(GLint location, GLfloat *v)
+{
+    mProgram->getUniformfv(location, v);
+}
+
+void Program::getUniformiv(GLint location, GLint *v)
+{
+    mProgram->getUniformiv(location, v);
+}
+
+void Program::getUniformuiv(GLint location, GLuint *v)
+{
+    mProgram->getUniformuiv(location, v);
+}
+
+// Applies all the uniforms set for this program object to the renderer
+Error Program::applyUniforms()
+{
+    return mProgram->applyUniforms();
+}
+
+Error Program::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const Caps &caps)
+{
+    return mProgram->applyUniformBuffers(boundBuffers, caps);
 }
 
 void Program::flagForDeletion()
@@ -512,11 +1034,12 @@
 void Program::validate(const Caps &caps)
 {
     mInfoLog.reset();
+    mValidated = false;
 
-    ProgramBinary *programBinary = getProgramBinary();
-    if (isLinked() && programBinary)
+    if (mLinked)
     {
-        programBinary->validate(mInfoLog, caps);
+        applyUniforms();
+        mValidated = mProgram->validateSamplers(&mInfoLog, caps);
     }
     else
     {
@@ -524,43 +1047,114 @@
     }
 }
 
+bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
+{
+    return mProgram->validateSamplers(infoLog, caps);
+}
+
 bool Program::isValidated() const
 {
-    ProgramBinary *programBinary = mProgramBinary.get();
-    if (programBinary)
+    return mValidated;
+}
+
+void Program::updateSamplerMapping()
+{
+    return mProgram->updateSamplerMapping();
+}
+
+GLuint Program::getActiveUniformBlockCount()
+{
+    return mProgram->getUniformBlocks().size();
+}
+
+void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
+{
+    ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size());   // index must be smaller than getActiveUniformBlockCount()
+
+    const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
+
+    if (bufSize > 0)
     {
-        return programBinary->isValidated();
-    }
-    else
-    {
-        return false;
+        std::string string = uniformBlock.name;
+
+        if (uniformBlock.isArrayElement())
+        {
+            string += ArrayString(uniformBlock.elementIndex);
+        }
+
+        strncpy(uniformBlockName, string.c_str(), bufSize);
+        uniformBlockName[bufSize - 1] = '\0';
+
+        if (length)
+        {
+            *length = strlen(uniformBlockName);
+        }
     }
 }
 
-GLint Program::getActiveUniformBlockCount()
+void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size());   // index must be smaller than getActiveUniformBlockCount()
+
+    const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
+
+    switch (pname)
     {
-        return static_cast<GLint>(programBinary->getActiveUniformBlockCount());
-    }
-    else
-    {
-        return 0;
+      case GL_UNIFORM_BLOCK_DATA_SIZE:
+        *params = static_cast<GLint>(uniformBlock.dataSize);
+        break;
+      case GL_UNIFORM_BLOCK_NAME_LENGTH:
+        *params = static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArrayElement() ? 3 : 0));
+        break;
+      case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
+        *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
+        break;
+      case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
+        {
+            for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
+            {
+                params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
+            }
+        }
+        break;
+      case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
+        *params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader());
+        break;
+      case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
+        *params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader());
+        break;
+      default: UNREACHABLE();
     }
 }
 
 GLint Program::getActiveUniformBlockMaxLength()
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    int maxLength = 0;
+
+    if (mLinked)
     {
-        return static_cast<GLint>(programBinary->getActiveUniformBlockMaxLength());
+        unsigned int numUniformBlocks = mProgram->getUniformBlocks().size();
+        for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
+        {
+            const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
+            if (!uniformBlock.name.empty())
+            {
+                const int length = uniformBlock.name.length() + 1;
+
+                // Counting in "[0]".
+                const int arrayLength = (uniformBlock.isArrayElement() ? 3 : 0);
+
+                maxLength = std::max(length + arrayLength, maxLength);
+            }
+        }
     }
-    else
-    {
-        return 0;
-    }
+
+    return maxLength;
+}
+
+GLuint Program::getUniformBlockIndex(const std::string &name)
+{
+    return mProgram->getUniformBlockIndex(name);
 }
 
 void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
@@ -594,10 +1188,10 @@
 
 void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary && index < programBinary->getTransformFeedbackVaryingCount())
+    if (mLinked)
     {
-        const LinkedVarying &varying = programBinary->getTransformFeedbackVarying(index);
+        ASSERT(index < mProgram->getTransformFeedbackLinkedVaryings().size());
+        const LinkedVarying &varying = mProgram->getTransformFeedbackLinkedVaryings()[index];
         GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
         if (length)
         {
@@ -621,10 +1215,9 @@
 
 GLsizei Program::getTransformFeedbackVaryingCount() const
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    if (mLinked)
     {
-        return static_cast<GLsizei>(programBinary->getTransformFeedbackVaryingCount());
+        return static_cast<GLsizei>(mProgram->getTransformFeedbackLinkedVaryings().size());
     }
     else
     {
@@ -634,13 +1227,12 @@
 
 GLsizei Program::getTransformFeedbackVaryingMaxLength() const
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    if (mLinked)
     {
         GLsizei maxSize = 0;
-        for (size_t i = 0; i < programBinary->getTransformFeedbackVaryingCount(); i++)
+        for (size_t i = 0; i < mProgram->getTransformFeedbackLinkedVaryings().size(); i++)
         {
-            const LinkedVarying &varying = programBinary->getTransformFeedbackVarying(i);
+            const LinkedVarying &varying = mProgram->getTransformFeedbackLinkedVaryings()[i];
             maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
         }
 
@@ -654,15 +1246,430 @@
 
 GLenum Program::getTransformFeedbackBufferMode() const
 {
-    ProgramBinary *programBinary = getProgramBinary();
-    if (programBinary)
+    return mTransformFeedbackBufferMode;
+}
+
+struct AttributeSorter
+{
+    AttributeSorter(const int(&semanticIndices)[MAX_VERTEX_ATTRIBS])
+        : originalIndices(semanticIndices)
     {
-        return programBinary->getTransformFeedbackBufferMode();
     }
-    else
+
+    bool operator()(int a, int b)
     {
-        return mTransformFeedbackBufferMode;
+        if (originalIndices[a] == -1) return false;
+        if (originalIndices[b] == -1) return true;
+        return (originalIndices[a] < originalIndices[b]);
     }
+
+    const int(&originalIndices)[MAX_VERTEX_ATTRIBS];
+};
+
+void Program::initAttributesByLayout()
+{
+    for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
+    {
+        mAttributesByLayout[i] = i;
+    }
+
+    std::sort(&mAttributesByLayout[0], &mAttributesByLayout[MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
+}
+
+void Program::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
+{
+    rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
+
+    for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
+    {
+        oldTranslatedAttributes[i] = attributes[i];
+    }
+
+    for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
+    {
+        int oldIndex = mAttributesByLayout[i];
+        sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
+        attributes[i] = oldTranslatedAttributes[oldIndex];
+    }
+}
+
+bool Program::linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader)
+{
+    std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
+    std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
+
+    for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++)
+    {
+        PackedVarying *input = &fragmentVaryings[fragVaryingIndex];
+        bool matched = false;
+
+        // Built-in varyings obey special rules
+        if (input->isBuiltIn())
+        {
+            continue;
+        }
+
+        for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
+        {
+            PackedVarying *output = &vertexVaryings[vertVaryingIndex];
+            if (output->name == input->name)
+            {
+                if (!linkValidateVaryings(infoLog, output->name, *input, *output))
+                {
+                    return false;
+                }
+
+                output->registerIndex = input->registerIndex;
+                output->columnIndex = input->columnIndex;
+
+                matched = true;
+                break;
+            }
+        }
+
+        // We permit unmatched, unreferenced varyings
+        if (!matched && input->staticUse)
+        {
+            infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
+{
+    if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
+    {
+        return false;
+    }
+
+    if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
+    {
+        infoLog.append("Matrix packings for %s differ between vertex and fragment shaders", uniformName.c_str());
+        return false;
+    }
+
+    return true;
+}
+
+// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
+bool Program::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader)
+{
+    const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
+
+    unsigned int usedLocations = 0;
+    const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes();
+
+    // Link attributes that have a binding location
+    for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
+    {
+        const sh::Attribute &attribute = shaderAttributes[attributeIndex];
+
+        ASSERT(attribute.staticUse);
+
+        const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
+
+        mProgram->getShaderAttributes()[attributeIndex] = attribute;
+
+        if (location != -1)   // Set by glBindAttribLocation or by location layout qualifier
+        {
+            const int rows = VariableRegisterCount(attribute.type);
+
+            if (rows + location > MAX_VERTEX_ATTRIBS)
+            {
+                infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute.name.c_str(), location);
+
+                return false;
+            }
+
+            for (int row = 0; row < rows; row++)
+            {
+                const int rowLocation = location + row;
+                sh::ShaderVariable &linkedAttribute = mLinkedAttribute[rowLocation];
+
+                // In GLSL 3.00, attribute aliasing produces a link error
+                // In GLSL 1.00, attribute aliasing is allowed
+                if (mProgram->getShaderVersion() >= 300)
+                {
+                    if (!linkedAttribute.name.empty())
+                    {
+                        infoLog.append("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), linkedAttribute.name.c_str(), rowLocation);
+                        return false;
+                    }
+                }
+
+                linkedAttribute = attribute;
+                usedLocations |= 1 << rowLocation;
+            }
+        }
+    }
+
+    // Link attributes that don't have a binding location
+    for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
+    {
+        const sh::Attribute &attribute = shaderAttributes[attributeIndex];
+
+        ASSERT(attribute.staticUse);
+
+        const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
+
+        if (location == -1)   // Not set by glBindAttribLocation or by location layout qualifier
+        {
+            int rows = VariableRegisterCount(attribute.type);
+            int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
+
+            if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
+            {
+                infoLog.append("Too many active attributes (%s)", attribute.name.c_str());
+
+                return false;   // Fail to link
+            }
+
+            mLinkedAttribute[availableIndex] = attribute;
+        }
+    }
+
+    for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
+    {
+        int index = vertexShaderD3D->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
+        int rows = VariableRegisterCount(mLinkedAttribute[attributeIndex].type);
+
+        for (int r = 0; r < rows; r++)
+        {
+            mSemanticIndex[attributeIndex++] = index++;
+        }
+    }
+
+    initAttributesByLayout();
+
+    return true;
+}
+
+bool Program::linkUniformBlocks(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps)
+{
+    const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
+    const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
+    // Check that interface blocks defined in the vertex and fragment shaders are identical
+    typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
+    UniformBlockMap linkedUniformBlocks;
+    for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
+    {
+        const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex];
+        linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
+    }
+    for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
+    {
+        const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex];
+        UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
+        if (entry != linkedUniformBlocks.end())
+        {
+            const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
+            if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
+            {
+                return false;
+            }
+        }
+    }
+    for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
+    {
+        const sh::InterfaceBlock &interfaceBlock = vertexInterfaceBlocks[blockIndex];
+        // Note: shared and std140 layouts are always considered active
+        if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
+        {
+            if (!mProgram->defineUniformBlock(infoLog, vertexShader, interfaceBlock, caps))
+            {
+                return false;
+            }
+        }
+    }
+    for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
+    {
+        const sh::InterfaceBlock &interfaceBlock = fragmentInterfaceBlocks[blockIndex];
+        // Note: shared and std140 layouts are always considered active
+        if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
+        {
+            if (!mProgram->defineUniformBlock(infoLog, fragmentShader, interfaceBlock, caps))
+            {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
+                                         const sh::InterfaceBlock &fragmentInterfaceBlock)
+{
+    const char* blockName = vertexInterfaceBlock.name.c_str();
+    // validate blocks for the same member types
+    if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
+    {
+        infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName);
+        return false;
+    }
+    if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
+    {
+        infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName);
+        return false;
+    }
+    if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
+    {
+        infoLog.append("Layout qualifiers differ for interface block '%s' between vertex and fragment shaders", blockName);
+        return false;
+    }
+    const unsigned int numBlockMembers = vertexInterfaceBlock.fields.size();
+    for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
+    {
+        const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
+        const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
+        if (vertexMember.name != fragmentMember.name)
+        {
+            infoLog.append("Name mismatch for field %d of interface block '%s': (in vertex: '%s', in fragment: '%s')",
+                           blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str());
+            return false;
+        }
+        std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
+        if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
+        {
+            return false;
+        }
+    }
+    return true;
+}
+
+bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
+                                              const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
+{
+    if (vertexVariable.type != fragmentVariable.type)
+    {
+        infoLog.append("Types for %s differ between vertex and fragment shaders", variableName.c_str());
+        return false;
+    }
+    if (vertexVariable.arraySize != fragmentVariable.arraySize)
+    {
+        infoLog.append("Array sizes for %s differ between vertex and fragment shaders", variableName.c_str());
+        return false;
+    }
+    if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
+    {
+        infoLog.append("Precisions for %s differ between vertex and fragment shaders", variableName.c_str());
+        return false;
+    }
+
+    if (vertexVariable.fields.size() != fragmentVariable.fields.size())
+    {
+        infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", variableName.c_str());
+        return false;
+    }
+    const unsigned int numMembers = vertexVariable.fields.size();
+    for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
+    {
+        const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
+        const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
+
+        if (vertexMember.name != fragmentMember.name)
+        {
+            infoLog.append("Name mismatch for field '%d' of %s: (in vertex: '%s', in fragment: '%s')",
+                           memberIndex, variableName.c_str(),
+                           vertexMember.name.c_str(), fragmentMember.name.c_str());
+            return false;
+        }
+
+        const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
+                                       vertexMember.name + "'";
+
+        if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
+{
+    if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
+    {
+        return false;
+    }
+
+    return true;
+}
+
+bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
+{
+    if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
+    {
+        return false;
+    }
+
+    if (vertexVarying.interpolation != fragmentVarying.interpolation)
+    {
+        infoLog.append("Interpolation types for %s differ between vertex and fragment shaders", varyingName.c_str());
+        return false;
+    }
+
+    return true;
+}
+
+bool Program::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings,
+                                                    const std::vector<std::string> &transformFeedbackVaryingNames,
+                                                    GLenum transformFeedbackBufferMode,
+                                                    std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings,
+                                                    const Caps &caps) const
+{
+    size_t totalComponents = 0;
+
+    // Gather the linked varyings that are used for transform feedback, they should all exist.
+    outTransformFeedbackLinkedVaryings->clear();
+    for (size_t i = 0; i < transformFeedbackVaryingNames.size(); i++)
+    {
+        bool found = false;
+        for (size_t j = 0; j < linkedVaryings.size(); j++)
+        {
+            if (transformFeedbackVaryingNames[i] == linkedVaryings[j].name)
+            {
+                for (size_t k = 0; k < outTransformFeedbackLinkedVaryings->size(); k++)
+                {
+                    if (outTransformFeedbackLinkedVaryings->at(k).name == linkedVaryings[j].name)
+                    {
+                        infoLog.append("Two transform feedback varyings specify the same output variable (%s).", linkedVaryings[j].name.c_str());
+                        return false;
+                    }
+                }
+
+                size_t componentCount = linkedVaryings[j].semanticIndexCount * 4;
+                if (transformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
+                    componentCount > caps.maxTransformFeedbackSeparateComponents)
+                {
+                    infoLog.append("Transform feedback varying's %s components (%u) exceed the maximum separate components (%u).",
+                                   linkedVaryings[j].name.c_str(), componentCount, caps.maxTransformFeedbackSeparateComponents);
+                    return false;
+                }
+
+                totalComponents += componentCount;
+
+                outTransformFeedbackLinkedVaryings->push_back(linkedVaryings[j]);
+                found = true;
+                break;
+            }
+        }
+
+        // All transform feedback varyings are expected to exist since packVaryings checks for them.
+        ASSERT(found);
+    }
+
+    if (transformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && totalComponents > caps.maxTransformFeedbackInterleavedComponents)
+    {
+        infoLog.append("Transform feedback varying total components (%u) exceed the maximum interleaved components (%u).",
+                       totalComponents, caps.maxTransformFeedbackInterleavedComponents);
+        return false;
+    }
+
+    return true;
 }
 
 }