Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2014 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 | // ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl. |
| 8 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 9 | #include "libANGLE/renderer/ProgramImpl.h" |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 10 | |
| 11 | #include "common/utilities.h" |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 12 | |
| 13 | namespace rx |
| 14 | { |
| 15 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 16 | LinkResult::LinkResult(bool linkSuccess, const gl::Error &error) |
| 17 | : linkSuccess(linkSuccess), |
| 18 | error(error) |
| 19 | { |
| 20 | } |
| 21 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 22 | ProgramImpl::~ProgramImpl() |
| 23 | { |
| 24 | // Ensure that reset was called by the inherited class during destruction |
| 25 | ASSERT(mUniformIndex.size() == 0); |
| 26 | } |
| 27 | |
| 28 | gl::LinkedUniform *ProgramImpl::getUniformByLocation(GLint location) const |
| 29 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 30 | ASSERT(location >= 0 && mUniformIndex.find(location) != mUniformIndex.end()); |
| 31 | return mUniforms[mUniformIndex.at(location).index]; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | gl::LinkedUniform *ProgramImpl::getUniformByName(const std::string &name) const |
| 35 | { |
| 36 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 37 | { |
| 38 | if (mUniforms[uniformIndex]->name == name) |
| 39 | { |
| 40 | return mUniforms[uniformIndex]; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | gl::UniformBlock *ProgramImpl::getUniformBlockByIndex(GLuint blockIndex) const |
| 48 | { |
| 49 | ASSERT(blockIndex < mUniformBlocks.size()); |
| 50 | return mUniformBlocks[blockIndex]; |
| 51 | } |
| 52 | |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 53 | GLint ProgramImpl::getUniformLocation(const std::string &name) const |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 54 | { |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 55 | size_t subscript = GL_INVALID_INDEX; |
| 56 | std::string baseName = gl::ParseUniformName(name, &subscript); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 57 | |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 58 | for (const auto &info : mUniformIndex) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 59 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 60 | GLuint location = info.first; |
| 61 | const gl::VariableLocation &uniform = info.second; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 62 | |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 63 | if (uniform.name == baseName) |
| 64 | { |
| 65 | const bool isArray = mUniforms[uniform.index]->isArray(); |
| 66 | |
| 67 | if ((isArray && uniform.element == subscript) || |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 68 | (subscript == GL_INVALID_INDEX)) |
| 69 | { |
| 70 | return location; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return -1; |
| 76 | } |
| 77 | |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 78 | GLuint ProgramImpl::getUniformIndex(const std::string &name) const |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 79 | { |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 80 | size_t subscript = GL_INVALID_INDEX; |
| 81 | std::string baseName = gl::ParseUniformName(name, &subscript); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 82 | |
| 83 | // The app is not allowed to specify array indices other than 0 for arrays of basic types |
| 84 | if (subscript != 0 && subscript != GL_INVALID_INDEX) |
| 85 | { |
| 86 | return GL_INVALID_INDEX; |
| 87 | } |
| 88 | |
Jamie Madill | b195643 | 2015-08-12 17:35:20 +0000 | [diff] [blame^] | 89 | unsigned int numUniforms = mUniforms.size(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 90 | for (unsigned int index = 0; index < numUniforms; index++) |
| 91 | { |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 92 | if (mUniforms[index]->name == baseName) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 93 | { |
| 94 | if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX) |
| 95 | { |
| 96 | return index; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return GL_INVALID_INDEX; |
| 102 | } |
| 103 | |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 104 | GLuint ProgramImpl::getUniformBlockIndex(const std::string &name) const |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 105 | { |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 106 | size_t subscript = GL_INVALID_INDEX; |
| 107 | std::string baseName = gl::ParseUniformName(name, &subscript); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 108 | |
Jamie Madill | b195643 | 2015-08-12 17:35:20 +0000 | [diff] [blame^] | 109 | unsigned int numUniformBlocks = mUniformBlocks.size(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 110 | for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++) |
| 111 | { |
| 112 | const gl::UniformBlock &uniformBlock = *mUniformBlocks[blockIndex]; |
Geoff Lang | cfaeaa9 | 2015-04-14 13:41:02 -0400 | [diff] [blame] | 113 | if (uniformBlock.name == baseName) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 114 | { |
| 115 | const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0); |
| 116 | if (subscript == uniformBlock.elementIndex || arrayElementZero) |
| 117 | { |
| 118 | return blockIndex; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return GL_INVALID_INDEX; |
| 124 | } |
| 125 | |
| 126 | void ProgramImpl::reset() |
| 127 | { |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 128 | std::fill(mSemanticIndex, mSemanticIndex + ArraySize(mSemanticIndex), -1); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 129 | SafeDeleteContainer(mUniforms); |
| 130 | mUniformIndex.clear(); |
| 131 | SafeDeleteContainer(mUniformBlocks); |
| 132 | mTransformFeedbackLinkedVaryings.clear(); |
| 133 | } |
| 134 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 135 | void ProgramImpl::setShaderAttribute(size_t index, const sh::Attribute &attrib) |
| 136 | { |
| 137 | if (mShaderAttributes.size() <= index) |
| 138 | { |
| 139 | mShaderAttributes.resize(index + 1); |
| 140 | } |
| 141 | mShaderAttributes[index] = attrib; |
| 142 | } |
| 143 | |
| 144 | void ProgramImpl::setShaderAttribute(size_t index, GLenum type, GLenum precision, const std::string &name, GLint size, int location) |
| 145 | { |
| 146 | if (mShaderAttributes.size() <= index) |
| 147 | { |
| 148 | mShaderAttributes.resize(index + 1); |
| 149 | } |
| 150 | mShaderAttributes[index].type = type; |
| 151 | mShaderAttributes[index].precision = precision; |
| 152 | mShaderAttributes[index].name = name; |
| 153 | mShaderAttributes[index].arraySize = size; |
| 154 | mShaderAttributes[index].location = location; |
| 155 | } |
| 156 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 157 | } |