blob: 56fed09eca6840a215e503275edd218028a3b4f5 [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 Madillccdf74b2015-08-18 10:46:12 -040061LinkResult ProgramGL::link(const gl::Data &data,
62 gl::InfoLog &infoLog,
63 gl::Shader *fragmentShader,
64 gl::Shader *vertexShader,
Geoff Langf9a6f082015-01-22 13:32:49 -050065 std::map<int, gl::VariableLocation> *outputVariables)
66{
Geoff Langb1f435e2015-02-20 10:01:01 -050067 // Reset the program state, delete the current program if one exists
68 reset();
69
Jamie Madillada9ecc2015-08-17 12:53:37 -040070 const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(vertexShader);
71 const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(fragmentShader);
Geoff Langb1f435e2015-02-20 10:01:01 -050072
Geoff Langb1f435e2015-02-20 10:01:01 -050073 // Attach the shaders
74 mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
75 mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
76
Geoff Langb1f435e2015-02-20 10:01:01 -050077 // Link and verify
78 mFunctions->linkProgram(mProgramID);
79
Geoff Lang0ca53782015-05-07 13:49:39 -040080 // Detach the shaders
81 mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID());
82 mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID());
83
84 // Verify the link
Geoff Langb1f435e2015-02-20 10:01:01 -050085 GLint linkStatus = GL_FALSE;
86 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
87 ASSERT(linkStatus == GL_TRUE);
88 if (linkStatus == GL_FALSE)
89 {
90 // Linking failed, put the error into the info log
91 GLint infoLogLength = 0;
92 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
93
94 std::vector<char> buf(infoLogLength);
95 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
96
97 mFunctions->deleteProgram(mProgramID);
98 mProgramID = 0;
99
Jamie Madillf6113162015-05-07 11:49:21 -0400100 infoLog << &buf[0];
Geoff Langb1f435e2015-02-20 10:01:01 -0500101 TRACE("\n%s", &buf[0]);
102
103 // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
104 return LinkResult(false, gl::Error(GL_NO_ERROR));
105 }
106
107 // Query the uniform information
108 // TODO: A lot of this logic should be done at the gl::Program level
109 GLint activeUniformMaxLength = 0;
110 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformMaxLength);
111
112 std::vector<GLchar> uniformNameBuffer(activeUniformMaxLength);
113
114 GLint uniformCount = 0;
115 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_UNIFORMS, &uniformCount);
116 for (GLint i = 0; i < uniformCount; i++)
117 {
118 GLsizei uniformNameLength = 0;
119 GLint uniformSize = 0;
120 GLenum uniformType = GL_NONE;
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700121 mFunctions->getActiveUniform(mProgramID, i, static_cast<GLsizei>(uniformNameBuffer.size()),
122 &uniformNameLength, &uniformSize, &uniformType,
123 &uniformNameBuffer[0]);
Geoff Langb1f435e2015-02-20 10:01:01 -0500124
Geoff Lang79d059f2015-07-28 15:03:28 -0400125 size_t subscript = 0;
126 std::string uniformName = gl::ParseUniformName(std::string(&uniformNameBuffer[0], uniformNameLength), &subscript);
127
128 bool isArray = uniformSize > 1 || subscript != GL_INVALID_INDEX;
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400129
130 for (size_t arrayIndex = 0; arrayIndex < static_cast<size_t>(uniformSize); arrayIndex++)
131 {
132 std::string locationName = uniformName;
Geoff Lang79d059f2015-07-28 15:03:28 -0400133 if (isArray)
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400134 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700135 locationName += "[" + Str(static_cast<int>(arrayIndex)) + "]";
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400136 }
137
138 GLint location = mFunctions->getUniformLocation(mProgramID, locationName.c_str());
139 if (location >= 0)
140 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700141 mUniformIndex[location] =
142 gl::VariableLocation(uniformName, static_cast<unsigned int>(arrayIndex),
143 static_cast<unsigned int>(mUniforms.size()));
Geoff Langf51bc792015-05-04 14:57:03 -0400144
145 // If the uniform is a sampler, track it in the sampler bindings array
146 if (gl::IsSamplerType(uniformType))
147 {
148 SamplerLocation samplerLoc;
149 samplerLoc.samplerIndex = mSamplerBindings.size();
150 samplerLoc.arrayIndex = arrayIndex;
151 mSamplerUniformMap[location] = samplerLoc;
152 }
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400153 }
154 }
155
156 // ANGLE uses 0 to identify an non-array uniform.
Geoff Lang79d059f2015-07-28 15:03:28 -0400157 unsigned int arraySize = isArray ? static_cast<unsigned int>(uniformSize) : 0;
Geoff Langb1f435e2015-02-20 10:01:01 -0500158
159 // TODO: determine uniform precision
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400160 mUniforms.push_back(new gl::LinkedUniform(uniformType, GL_NONE, uniformName, arraySize, -1, sh::BlockMemberInfo::getDefaultBlockInfo()));
Geoff Langf51bc792015-05-04 14:57:03 -0400161
162 // If uniform is a sampler type, insert it into the mSamplerBindings array
163 if (gl::IsSamplerType(uniformType))
164 {
165 SamplerBindingGL samplerBinding;
166 samplerBinding.textureType = gl::SamplerTypeToTextureType(uniformType);
167 samplerBinding.boundTextureUnits.resize(uniformSize, 0);
168 mSamplerBindings.push_back(samplerBinding);
169 }
Geoff Langb1f435e2015-02-20 10:01:01 -0500170 }
171
172 // Query the attribute information
173 GLint activeAttributeMaxLength = 0;
174 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttributeMaxLength);
175
176 std::vector<GLchar> attributeNameBuffer(activeAttributeMaxLength);
177
178 GLint attributeCount = 0;
179 mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTES, &attributeCount);
180 for (GLint i = 0; i < attributeCount; i++)
181 {
182 GLsizei attributeNameLength = 0;
183 GLint attributeSize = 0;
184 GLenum attributeType = GL_NONE;
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700185 mFunctions->getActiveAttrib(mProgramID, i, static_cast<GLsizei>(attributeNameBuffer.size()),
186 &attributeNameLength, &attributeSize, &attributeType,
187 &attributeNameBuffer[0]);
Geoff Langb1f435e2015-02-20 10:01:01 -0500188
189 std::string attributeName(&attributeNameBuffer[0], attributeNameLength);
190
Geoff Lang0ca53782015-05-07 13:49:39 -0400191 GLint location = mFunctions->getAttribLocation(mProgramID, attributeName.c_str());
192
Geoff Langb1f435e2015-02-20 10:01:01 -0500193 // TODO: determine attribute precision
Geoff Lang0ca53782015-05-07 13:49:39 -0400194 setShaderAttribute(static_cast<size_t>(i), attributeType, GL_NONE, attributeName, attributeSize, location);
Geoff Langb61e1732015-06-05 11:49:55 -0400195
Corentin Wallez1661fde2015-08-18 11:29:46 -0400196 int attributeRegisterCount = gl::VariableRegisterCount(attributeType);
197 for (int offset = 0; offset < attributeRegisterCount; offset++)
198 {
199 mActiveAttributesMask.set(location + offset);
200 }
Geoff Langb1f435e2015-02-20 10:01:01 -0500201 }
202
203 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -0500204}
205
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400206GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
207{
208 // TODO(jmadill): implement validate
209 return true;
210}
211
Geoff Lang0ca53782015-05-07 13:49:39 -0400212void ProgramGL::bindAttributeLocation(GLuint index, const std::string &name)
213{
214 mFunctions->bindAttribLocation(mProgramID, index, name.c_str());
215}
216
Geoff Langf9a6f082015-01-22 13:32:49 -0500217void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
218{
Geoff Lang63cbace2015-02-26 10:03:12 -0500219 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500220 mFunctions->uniform1fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500221}
222
223void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
224{
Geoff Lang63cbace2015-02-26 10:03:12 -0500225 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500226 mFunctions->uniform2fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500227}
228
229void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
230{
Geoff Lang63cbace2015-02-26 10:03:12 -0500231 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500232 mFunctions->uniform3fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500233}
234
235void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
236{
Geoff Lang63cbace2015-02-26 10:03:12 -0500237 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500238 mFunctions->uniform4fv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500239}
240
241void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
242{
Geoff Lang63cbace2015-02-26 10:03:12 -0500243 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500244 mFunctions->uniform1iv(location, count, v);
Geoff Langf51bc792015-05-04 14:57:03 -0400245
246 auto iter = mSamplerUniformMap.find(location);
247 if (iter != mSamplerUniformMap.end())
248 {
249 const SamplerLocation &samplerLoc = iter->second;
250 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerLoc.samplerIndex].boundTextureUnits;
251
252 size_t copyCount = std::max<size_t>(count, boundTextureUnits.size() - samplerLoc.arrayIndex);
253 std::copy(v, v + copyCount, boundTextureUnits.begin() + samplerLoc.arrayIndex);
254 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500255}
256
257void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
258{
Geoff Lang63cbace2015-02-26 10:03:12 -0500259 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500260 mFunctions->uniform2iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500261}
262
263void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
264{
Geoff Lang63cbace2015-02-26 10:03:12 -0500265 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500266 mFunctions->uniform3iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500267}
268
269void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
270{
Geoff Lang63cbace2015-02-26 10:03:12 -0500271 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500272 mFunctions->uniform4iv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500273}
274
275void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
276{
Geoff Lang63cbace2015-02-26 10:03:12 -0500277 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500278 mFunctions->uniform1uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500279}
280
281void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
282{
Geoff Lang63cbace2015-02-26 10:03:12 -0500283 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500284 mFunctions->uniform2uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500285}
286
287void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
288{
Geoff Lang63cbace2015-02-26 10:03:12 -0500289 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500290 mFunctions->uniform3uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500291}
292
293void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
294{
Geoff Lang63cbace2015-02-26 10:03:12 -0500295 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500296 mFunctions->uniform4uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500297}
298
299void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
300{
Geoff Lang63cbace2015-02-26 10:03:12 -0500301 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500302 mFunctions->uniformMatrix2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500303}
304
305void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
306{
Geoff Lang63cbace2015-02-26 10:03:12 -0500307 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500308 mFunctions->uniformMatrix3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500309}
310
311void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
312{
Geoff Lang63cbace2015-02-26 10:03:12 -0500313 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500314 mFunctions->uniformMatrix4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500315}
316
317void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
318{
Geoff Lang63cbace2015-02-26 10:03:12 -0500319 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500320 mFunctions->uniformMatrix2x3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500321}
322
323void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
324{
Geoff Lang63cbace2015-02-26 10:03:12 -0500325 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500326 mFunctions->uniformMatrix3x2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500327}
328
329void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
330{
Geoff Lang63cbace2015-02-26 10:03:12 -0500331 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500332 mFunctions->uniformMatrix2x4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500333}
334
335void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
336{
Geoff Lang63cbace2015-02-26 10:03:12 -0500337 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500338 mFunctions->uniformMatrix4x2fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500339}
340
341void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
342{
Geoff Lang63cbace2015-02-26 10:03:12 -0500343 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500344 mFunctions->uniformMatrix3x4fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500345}
346
347void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
348{
Geoff Lang63cbace2015-02-26 10:03:12 -0500349 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500350 mFunctions->uniformMatrix4x3fv(location, count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500351}
352
353void ProgramGL::getUniformfv(GLint location, GLfloat *params)
354{
Geoff Langb1f435e2015-02-20 10:01:01 -0500355 mFunctions->getUniformfv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500356}
357
358void ProgramGL::getUniformiv(GLint location, GLint *params)
359{
Geoff Langb1f435e2015-02-20 10:01:01 -0500360 mFunctions->getUniformiv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500361}
362
363void ProgramGL::getUniformuiv(GLint location, GLuint *params)
364{
Geoff Langb1f435e2015-02-20 10:01:01 -0500365 mFunctions->getUniformuiv(mProgramID, location, params);
Geoff Langf9a6f082015-01-22 13:32:49 -0500366}
367
Geoff Langf9a6f082015-01-22 13:32:49 -0500368bool ProgramGL::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
369{
Geoff Lang35d315c2015-03-31 12:48:54 -0400370 //UNIMPLEMENTED();
Geoff Langb1f435e2015-02-20 10:01:01 -0500371 return true;
Geoff Langf9a6f082015-01-22 13:32:49 -0500372}
373
Geoff Langb1f435e2015-02-20 10:01:01 -0500374void ProgramGL::reset()
375{
376 ProgramImpl::reset();
Geoff Langf51bc792015-05-04 14:57:03 -0400377
378 mSamplerUniformMap.clear();
379 mSamplerBindings.clear();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000380 mActiveAttributesMask.reset();
Geoff Langb1f435e2015-02-20 10:01:01 -0500381}
382
383GLuint ProgramGL::getProgramID() const
384{
385 return mProgramID;
386}
387
Geoff Langf51bc792015-05-04 14:57:03 -0400388const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
389{
390 return mSamplerBindings;
391}
392
Jamie Madill0b9e9032015-08-17 11:51:52 +0000393const gl::AttributesMask &ProgramGL::getActiveAttributesMask() const
Geoff Langb61e1732015-06-05 11:49:55 -0400394{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000395 return mActiveAttributesMask;
Geoff Langb61e1732015-06-05 11:49:55 -0400396}
397
Geoff Langf9a6f082015-01-22 13:32:49 -0500398}