blob: 3a70ae93086cbc2e85e74aa683e0f6b535e2108e [file] [log] [blame]
Geoff Langf9a6f082015-01-22 13:32:49 -05001//
2// Copyright 2015 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// ProgramGL.cpp: Implements the class methods for ProgramGL.
8
9#include "libANGLE/renderer/gl/ProgramGL.h"
10
11#include "common/debug.h"
Geoff Lang5ed74cf2015-04-14 13:57:07 -040012#include "common/utilities.h"
Geoff Langb1f435e2015-02-20 10:01:01 -050013#include "libANGLE/renderer/gl/FunctionsGL.h"
14#include "libANGLE/renderer/gl/ShaderGL.h"
15#include "libANGLE/renderer/gl/StateManagerGL.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050016
17namespace rx
18{
19
Jamie Madill5c6b7bf2015-08-17 12:53:35 -040020ProgramGL::ProgramGL(const gl::Program::Data &data,
21 const FunctionsGL *functions,
22 StateManagerGL *stateManager)
23 : ProgramImpl(data), mFunctions(functions), mStateManager(stateManager), mProgramID(0)
Geoff Langb1f435e2015-02-20 10:01:01 -050024{
25 ASSERT(mFunctions);
26 ASSERT(mStateManager);
Geoff Lang0ca53782015-05-07 13:49:39 -040027
28 mProgramID = mFunctions->createProgram();
Geoff Langb1f435e2015-02-20 10:01:01 -050029}
Geoff Langf9a6f082015-01-22 13:32:49 -050030
31ProgramGL::~ProgramGL()
Geoff Langb1f435e2015-02-20 10:01:01 -050032{
Geoff Lang0ca53782015-05-07 13:49:39 -040033 mFunctions->deleteProgram(mProgramID);
34 mProgramID = 0;
Geoff Langb1f435e2015-02-20 10:01:01 -050035}
Geoff Langf9a6f082015-01-22 13:32:49 -050036
Geoff Langf9a6f082015-01-22 13:32:49 -050037int ProgramGL::getShaderVersion() const
38{
39 UNIMPLEMENTED();
40 return int();
41}
42
Geoff Langf9a6f082015-01-22 13:32:49 -050043GLenum ProgramGL::getBinaryFormat()
44{
45 UNIMPLEMENTED();
46 return GLenum();
47}
48
49LinkResult ProgramGL::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
50{
51 UNIMPLEMENTED();
52 return LinkResult(false, gl::Error(GL_INVALID_OPERATION));
53}
54
55gl::Error ProgramGL::save(gl::BinaryOutputStream *stream)
56{
57 UNIMPLEMENTED();
58 return gl::Error(GL_INVALID_OPERATION);
59}
60
Jamie Madillf5f4ad22015-09-02 18:32:38 +000061LinkResult ProgramGL::link(const gl::Data &data, gl::InfoLog &infoLog)
Geoff Langf9a6f082015-01-22 13:32:49 -050062{
Geoff Langb1f435e2015-02-20 10:01:01 -050063 // Reset the program state, delete the current program if one exists
64 reset();
65
Jamie Madillf5f4ad22015-09-02 18:32:38 +000066 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
67 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
68
Jamie Madillada9ecc2015-08-17 12:53:37 -040069 const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(vertexShader);
70 const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(fragmentShader);
Geoff Langb1f435e2015-02-20 10:01:01 -050071
Geoff Langb1f435e2015-02-20 10:01:01 -050072 // Attach the shaders
73 mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
74 mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
75
Geoff Lang1528e562015-08-24 15:10:58 -040076 // Bind attribute locations to match the GL layer.
77 for (const sh::Attribute &attribute : mData.getAttributes())
78 {
79 if (!attribute.staticUse)
80 {
81 continue;
82 }
83
84 mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str());
Geoff Lang1528e562015-08-24 15:10:58 -040085 }
86
Geoff Langb1f435e2015-02-20 10:01:01 -050087 // Link and verify
88 mFunctions->linkProgram(mProgramID);
89
Geoff Lang0ca53782015-05-07 13:49:39 -040090 // Detach the shaders
91 mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID());
92 mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID());
93
94 // Verify the link
Geoff Langb1f435e2015-02-20 10:01:01 -050095 GLint linkStatus = GL_FALSE;
96 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
97 ASSERT(linkStatus == GL_TRUE);
98 if (linkStatus == GL_FALSE)
99 {
100 // Linking failed, put the error into the info log
101 GLint infoLogLength = 0;
102 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
103
104 std::vector<char> buf(infoLogLength);
105 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
106
107 mFunctions->deleteProgram(mProgramID);
108 mProgramID = 0;
109
Jamie Madillf6113162015-05-07 11:49:21 -0400110 infoLog << &buf[0];
Geoff Langb1f435e2015-02-20 10:01:01 -0500111 TRACE("\n%s", &buf[0]);
112
113 // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
114 return LinkResult(false, gl::Error(GL_NO_ERROR));
115 }
116
117 // Query the uniform information
118 // TODO: A lot of this logic should be done at the gl::Program level
119 GLint activeUniformMaxLength = 0;
120 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformMaxLength);
121
122 std::vector<GLchar> uniformNameBuffer(activeUniformMaxLength);
123
124 GLint uniformCount = 0;
125 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORMS, &uniformCount);
126 for (GLint i = 0; i < uniformCount; i++)
127 {
128 GLsizei uniformNameLength = 0;
129 GLint uniformSize = 0;
130 GLenum uniformType = GL_NONE;
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700131 mFunctions->getActiveUniform(mProgramID, i, static_cast<GLsizei>(uniformNameBuffer.size()),
132 &uniformNameLength, &uniformSize, &uniformType,
133 &uniformNameBuffer[0]);
Geoff Langb1f435e2015-02-20 10:01:01 -0500134
Geoff Lang79d059f2015-07-28 15:03:28 -0400135 size_t subscript = 0;
136 std::string uniformName = gl::ParseUniformName(std::string(&uniformNameBuffer[0], uniformNameLength), &subscript);
137
138 bool isArray = uniformSize > 1 || subscript != GL_INVALID_INDEX;
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400139
140 for (size_t arrayIndex = 0; arrayIndex < static_cast<size_t>(uniformSize); arrayIndex++)
141 {
142 std::string locationName = uniformName;
Geoff Lang79d059f2015-07-28 15:03:28 -0400143 if (isArray)
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400144 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700145 locationName += "[" + Str(static_cast<int>(arrayIndex)) + "]";
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400146 }
147
148 GLint location = mFunctions->getUniformLocation(mProgramID, locationName.c_str());
149 if (location >= 0)
150 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700151 mUniformIndex[location] =
152 gl::VariableLocation(uniformName, static_cast<unsigned int>(arrayIndex),
153 static_cast<unsigned int>(mUniforms.size()));
Geoff Langf51bc792015-05-04 14:57:03 -0400154
155 // If the uniform is a sampler, track it in the sampler bindings array
156 if (gl::IsSamplerType(uniformType))
157 {
158 SamplerLocation samplerLoc;
159 samplerLoc.samplerIndex = mSamplerBindings.size();
160 samplerLoc.arrayIndex = arrayIndex;
161 mSamplerUniformMap[location] = samplerLoc;
162 }
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400163 }
164 }
165
166 // ANGLE uses 0 to identify an non-array uniform.
Geoff Lang79d059f2015-07-28 15:03:28 -0400167 unsigned int arraySize = isArray ? static_cast<unsigned int>(uniformSize) : 0;
Geoff Langb1f435e2015-02-20 10:01:01 -0500168
169 // TODO: determine uniform precision
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400170 mUniforms.push_back(new gl::LinkedUniform(uniformType, GL_NONE, uniformName, arraySize, -1, sh::BlockMemberInfo::getDefaultBlockInfo()));
Geoff Langf51bc792015-05-04 14:57:03 -0400171
172 // If uniform is a sampler type, insert it into the mSamplerBindings array
173 if (gl::IsSamplerType(uniformType))
174 {
175 SamplerBindingGL samplerBinding;
176 samplerBinding.textureType = gl::SamplerTypeToTextureType(uniformType);
177 samplerBinding.boundTextureUnits.resize(uniformSize, 0);
178 mSamplerBindings.push_back(samplerBinding);
179 }
Geoff Langb1f435e2015-02-20 10:01:01 -0500180 }
181
Geoff Langb1f435e2015-02-20 10:01:01 -0500182 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -0500183}
184
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400185GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
186{
187 // TODO(jmadill): implement validate
188 return true;
189}
190
Geoff Langf9a6f082015-01-22 13:32:49 -0500191void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
192{
Geoff Lang63cbace2015-02-26 10:03:12 -0500193 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500194 mFunctions->uniform1fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500195}
196
197void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
198{
Geoff Lang63cbace2015-02-26 10:03:12 -0500199 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500200 mFunctions->uniform2fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500201}
202
203void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
204{
Geoff Lang63cbace2015-02-26 10:03:12 -0500205 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500206 mFunctions->uniform3fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500207}
208
209void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
210{
Geoff Lang63cbace2015-02-26 10:03:12 -0500211 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500212 mFunctions->uniform4fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500213}
214
215void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
216{
Geoff Lang63cbace2015-02-26 10:03:12 -0500217 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500218 mFunctions->uniform1iv(location, count, v);
Geoff Langf51bc792015-05-04 14:57:03 -0400219
220 auto iter = mSamplerUniformMap.find(location);
221 if (iter != mSamplerUniformMap.end())
222 {
223 const SamplerLocation &samplerLoc = iter->second;
224 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerLoc.samplerIndex].boundTextureUnits;
225
226 size_t copyCount = std::max<size_t>(count, boundTextureUnits.size() - samplerLoc.arrayIndex);
227 std::copy(v, v + copyCount, boundTextureUnits.begin() + samplerLoc.arrayIndex);
228 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500229}
230
231void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
232{
Geoff Lang63cbace2015-02-26 10:03:12 -0500233 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500234 mFunctions->uniform2iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500235}
236
237void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
238{
Geoff Lang63cbace2015-02-26 10:03:12 -0500239 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500240 mFunctions->uniform3iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500241}
242
243void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
244{
Geoff Lang63cbace2015-02-26 10:03:12 -0500245 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500246 mFunctions->uniform4iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500247}
248
249void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
250{
Geoff Lang63cbace2015-02-26 10:03:12 -0500251 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500252 mFunctions->uniform1uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500253}
254
255void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
256{
Geoff Lang63cbace2015-02-26 10:03:12 -0500257 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500258 mFunctions->uniform2uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500259}
260
261void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
262{
Geoff Lang63cbace2015-02-26 10:03:12 -0500263 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500264 mFunctions->uniform3uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500265}
266
267void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
268{
Geoff Lang63cbace2015-02-26 10:03:12 -0500269 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500270 mFunctions->uniform4uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500271}
272
273void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
274{
Geoff Lang63cbace2015-02-26 10:03:12 -0500275 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500276 mFunctions->uniformMatrix2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500277}
278
279void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
280{
Geoff Lang63cbace2015-02-26 10:03:12 -0500281 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500282 mFunctions->uniformMatrix3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500283}
284
285void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
286{
Geoff Lang63cbace2015-02-26 10:03:12 -0500287 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500288 mFunctions->uniformMatrix4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500289}
290
291void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
292{
Geoff Lang63cbace2015-02-26 10:03:12 -0500293 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500294 mFunctions->uniformMatrix2x3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500295}
296
297void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
298{
Geoff Lang63cbace2015-02-26 10:03:12 -0500299 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500300 mFunctions->uniformMatrix3x2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500301}
302
303void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
304{
Geoff Lang63cbace2015-02-26 10:03:12 -0500305 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500306 mFunctions->uniformMatrix2x4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500307}
308
309void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
310{
Geoff Lang63cbace2015-02-26 10:03:12 -0500311 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500312 mFunctions->uniformMatrix4x2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500313}
314
315void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
316{
Geoff Lang63cbace2015-02-26 10:03:12 -0500317 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500318 mFunctions->uniformMatrix3x4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500319}
320
321void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
322{
Geoff Lang63cbace2015-02-26 10:03:12 -0500323 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500324 mFunctions->uniformMatrix4x3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500325}
326
327void ProgramGL::getUniformfv(GLint location, GLfloat *params)
328{
Geoff Langb1f435e2015-02-20 10:01:01 -0500329 mFunctions->getUniformfv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500330}
331
332void ProgramGL::getUniformiv(GLint location, GLint *params)
333{
Geoff Langb1f435e2015-02-20 10:01:01 -0500334 mFunctions->getUniformiv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500335}
336
337void ProgramGL::getUniformuiv(GLint location, GLuint *params)
338{
Geoff Langb1f435e2015-02-20 10:01:01 -0500339 mFunctions->getUniformuiv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500340}
341
Geoff Langf9a6f082015-01-22 13:32:49 -0500342bool ProgramGL::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
343{
Geoff Lang35d315c2015-03-31 12:48:54 -0400344 //UNIMPLEMENTED();
Geoff Langb1f435e2015-02-20 10:01:01 -0500345 return true;
Geoff Langf9a6f082015-01-22 13:32:49 -0500346}
347
Geoff Langb1f435e2015-02-20 10:01:01 -0500348void ProgramGL::reset()
349{
350 ProgramImpl::reset();
Geoff Langf51bc792015-05-04 14:57:03 -0400351
352 mSamplerUniformMap.clear();
353 mSamplerBindings.clear();
Geoff Langb1f435e2015-02-20 10:01:01 -0500354}
355
356GLuint ProgramGL::getProgramID() const
357{
358 return mProgramID;
359}
360
Geoff Langf51bc792015-05-04 14:57:03 -0400361const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
362{
363 return mSamplerBindings;
364}
365
Geoff Langf9a6f082015-01-22 13:32:49 -0500366}