blob: 9da0c42ae8309a3ef7c65b401c0d863cff9e2c85 [file] [log] [blame]
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001//
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 Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/ProgramImpl.h"
Brandon Jones1a8a7e32014-10-01 12:49:30 -070010
11#include "common/utilities.h"
Brandon Jones1a8a7e32014-10-01 12:49:30 -070012
13namespace rx
14{
15
Geoff Lang7dd2e102014-11-10 15:19:26 -050016LinkResult::LinkResult(bool linkSuccess, const gl::Error &error)
17 : linkSuccess(linkSuccess),
18 error(error)
19{
20}
21
Brandon Jones1a8a7e32014-10-01 12:49:30 -070022ProgramImpl::~ProgramImpl()
23{
24 // Ensure that reset was called by the inherited class during destruction
25 ASSERT(mUniformIndex.size() == 0);
26}
27
28gl::LinkedUniform *ProgramImpl::getUniformByLocation(GLint location) const
29{
Geoff Lang95137842015-06-02 15:38:43 -040030 ASSERT(location >= 0 && mUniformIndex.find(location) != mUniformIndex.end());
31 return mUniforms[mUniformIndex.at(location).index];
Brandon Jones1a8a7e32014-10-01 12:49:30 -070032}
33
34gl::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
47gl::UniformBlock *ProgramImpl::getUniformBlockByIndex(GLuint blockIndex) const
48{
49 ASSERT(blockIndex < mUniformBlocks.size());
50 return mUniformBlocks[blockIndex];
51}
52
Geoff Langcfaeaa92015-04-14 13:41:02 -040053GLint ProgramImpl::getUniformLocation(const std::string &name) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -070054{
Geoff Langcfaeaa92015-04-14 13:41:02 -040055 size_t subscript = GL_INVALID_INDEX;
56 std::string baseName = gl::ParseUniformName(name, &subscript);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070057
Geoff Lang95137842015-06-02 15:38:43 -040058 for (const auto &info : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -070059 {
Geoff Lang95137842015-06-02 15:38:43 -040060 GLuint location = info.first;
61 const gl::VariableLocation &uniform = info.second;
Brandon Jones1a8a7e32014-10-01 12:49:30 -070062
Geoff Lang95137842015-06-02 15:38:43 -040063 if (uniform.name == baseName)
64 {
65 const bool isArray = mUniforms[uniform.index]->isArray();
66
67 if ((isArray && uniform.element == subscript) ||
Brandon Jones1a8a7e32014-10-01 12:49:30 -070068 (subscript == GL_INVALID_INDEX))
69 {
70 return location;
71 }
72 }
73 }
74
75 return -1;
76}
77
Geoff Langcfaeaa92015-04-14 13:41:02 -040078GLuint ProgramImpl::getUniformIndex(const std::string &name) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -070079{
Geoff Langcfaeaa92015-04-14 13:41:02 -040080 size_t subscript = GL_INVALID_INDEX;
81 std::string baseName = gl::ParseUniformName(name, &subscript);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070082
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 Madillb1956432015-08-12 17:35:20 +000089 unsigned int numUniforms = mUniforms.size();
Brandon Jones1a8a7e32014-10-01 12:49:30 -070090 for (unsigned int index = 0; index < numUniforms; index++)
91 {
Geoff Langcfaeaa92015-04-14 13:41:02 -040092 if (mUniforms[index]->name == baseName)
Brandon Jones1a8a7e32014-10-01 12:49:30 -070093 {
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 Langcfaeaa92015-04-14 13:41:02 -0400104GLuint ProgramImpl::getUniformBlockIndex(const std::string &name) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700105{
Geoff Langcfaeaa92015-04-14 13:41:02 -0400106 size_t subscript = GL_INVALID_INDEX;
107 std::string baseName = gl::ParseUniformName(name, &subscript);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700108
Jamie Madillb1956432015-08-12 17:35:20 +0000109 unsigned int numUniformBlocks = mUniformBlocks.size();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700110 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
111 {
112 const gl::UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
Geoff Langcfaeaa92015-04-14 13:41:02 -0400113 if (uniformBlock.name == baseName)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700114 {
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
126void ProgramImpl::reset()
127{
Jamie Madill437d2662014-12-05 14:23:35 -0500128 std::fill(mSemanticIndex, mSemanticIndex + ArraySize(mSemanticIndex), -1);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700129 SafeDeleteContainer(mUniforms);
130 mUniformIndex.clear();
131 SafeDeleteContainer(mUniformBlocks);
132 mTransformFeedbackLinkedVaryings.clear();
133}
134
Jamie Madill3da79b72015-04-27 11:09:17 -0400135void 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
144void 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 Jones1a8a7e32014-10-01 12:49:30 -0700157}