blob: a13aa56f5379dbe6919d14681f54bab5844ca772 [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"
Philippe Hamel40911192016-04-07 16:45:50 -040016#include "libANGLE/renderer/gl/WorkaroundsGL.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040017#include "libANGLE/Uniform.h"
unknownb4a3af22015-11-25 15:02:51 -050018#include "platform/Platform.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050019
20namespace rx
21{
22
Jamie Madill48ef11b2016-04-27 15:21:52 -040023ProgramGL::ProgramGL(const gl::ProgramState &data,
Jamie Madill5c6b7bf2015-08-17 12:53:35 -040024 const FunctionsGL *functions,
Philippe Hamel40911192016-04-07 16:45:50 -040025 const WorkaroundsGL &workarounds,
Jamie Madill5c6b7bf2015-08-17 12:53:35 -040026 StateManagerGL *stateManager)
Philippe Hamel40911192016-04-07 16:45:50 -040027 : ProgramImpl(data),
28 mFunctions(functions),
29 mWorkarounds(workarounds),
30 mStateManager(stateManager),
31 mProgramID(0)
Geoff Langb1f435e2015-02-20 10:01:01 -050032{
33 ASSERT(mFunctions);
34 ASSERT(mStateManager);
Geoff Lang0ca53782015-05-07 13:49:39 -040035
36 mProgramID = mFunctions->createProgram();
Geoff Langb1f435e2015-02-20 10:01:01 -050037}
Geoff Langf9a6f082015-01-22 13:32:49 -050038
39ProgramGL::~ProgramGL()
Geoff Langb1f435e2015-02-20 10:01:01 -050040{
Geoff Lang0ca53782015-05-07 13:49:39 -040041 mFunctions->deleteProgram(mProgramID);
42 mProgramID = 0;
Geoff Langb1f435e2015-02-20 10:01:01 -050043}
Geoff Langf9a6f082015-01-22 13:32:49 -050044
Geoff Langf9a6f082015-01-22 13:32:49 -050045LinkResult ProgramGL::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
46{
Geoff Lang65a0be92015-10-02 09:57:30 -040047 preLink();
48
49 // Read the binary format, size and blob
50 GLenum binaryFormat = stream->readInt<GLenum>();
51 GLint binaryLength = stream->readInt<GLint>();
52 const uint8_t *binary = stream->data() + stream->offset();
53 stream->skip(binaryLength);
54
55 // Load the binary
56 mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength);
57
58 // Verify that the program linked
59 if (!checkLinkStatus(infoLog))
60 {
61 return LinkResult(false, gl::Error(GL_NO_ERROR));
62 }
63
64 postLink();
65
66 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -050067}
68
69gl::Error ProgramGL::save(gl::BinaryOutputStream *stream)
70{
Geoff Lang65a0be92015-10-02 09:57:30 -040071 GLint binaryLength = 0;
72 mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
73
74 std::vector<uint8_t> binary(binaryLength);
75 GLenum binaryFormat = GL_NONE;
76 mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat,
77 &binary[0]);
78
79 stream->writeInt(binaryFormat);
80 stream->writeInt(binaryLength);
81 stream->writeBytes(&binary[0], binaryLength);
82
83 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -050084}
85
Geoff Langc5629752015-12-07 16:29:04 -050086void ProgramGL::setBinaryRetrievableHint(bool retrievable)
87{
Geoff Lang65a0be92015-10-02 09:57:30 -040088 // glProgramParameteri isn't always available on ES backends.
89 if (mFunctions->programParameteri)
90 {
91 mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT,
92 retrievable ? GL_TRUE : GL_FALSE);
93 }
Geoff Langc5629752015-12-07 16:29:04 -050094}
95
Jamie Madill9082b982016-04-27 15:21:51 -040096LinkResult ProgramGL::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Geoff Langf9a6f082015-01-22 13:32:49 -050097{
Geoff Lang65a0be92015-10-02 09:57:30 -040098 preLink();
Geoff Langb1f435e2015-02-20 10:01:01 -050099
Geoff Lang1a683462015-09-29 15:09:59 -0400100 // Set the transform feedback state
101 std::vector<const GLchar *> transformFeedbackVaryings;
Jamie Madill48ef11b2016-04-27 15:21:52 -0400102 for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames())
Geoff Lang1a683462015-09-29 15:09:59 -0400103 {
104 transformFeedbackVaryings.push_back(tfVarying.c_str());
105 }
106
107 if (transformFeedbackVaryings.empty())
108 {
109 if (mFunctions->transformFeedbackVaryings)
110 {
111 mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400112 mState.getTransformFeedbackBufferMode());
Geoff Lang1a683462015-09-29 15:09:59 -0400113 }
114 }
115 else
116 {
117 ASSERT(mFunctions->transformFeedbackVaryings);
118 mFunctions->transformFeedbackVaryings(
119 mProgramID, static_cast<GLsizei>(transformFeedbackVaryings.size()),
Jamie Madill48ef11b2016-04-27 15:21:52 -0400120 &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode());
Geoff Lang1a683462015-09-29 15:09:59 -0400121 }
122
Jamie Madill48ef11b2016-04-27 15:21:52 -0400123 const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(mState.getAttachedVertexShader());
124 const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(mState.getAttachedFragmentShader());
Geoff Langb1f435e2015-02-20 10:01:01 -0500125
Geoff Langb1f435e2015-02-20 10:01:01 -0500126 // Attach the shaders
127 mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
128 mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
129
Geoff Lang1528e562015-08-24 15:10:58 -0400130 // Bind attribute locations to match the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400131 for (const sh::Attribute &attribute : mState.getAttributes())
Geoff Lang1528e562015-08-24 15:10:58 -0400132 {
133 if (!attribute.staticUse)
134 {
135 continue;
136 }
137
138 mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str());
Geoff Lang1528e562015-08-24 15:10:58 -0400139 }
140
Geoff Langb1f435e2015-02-20 10:01:01 -0500141 // Link and verify
142 mFunctions->linkProgram(mProgramID);
143
Geoff Lang0ca53782015-05-07 13:49:39 -0400144 // Detach the shaders
145 mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID());
146 mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID());
147
148 // Verify the link
Geoff Lang65a0be92015-10-02 09:57:30 -0400149 if (!checkLinkStatus(infoLog))
Geoff Langb1f435e2015-02-20 10:01:01 -0500150 {
Geoff Langb1f435e2015-02-20 10:01:01 -0500151 return LinkResult(false, gl::Error(GL_NO_ERROR));
152 }
153
Philippe Hamel40911192016-04-07 16:45:50 -0400154 if (mWorkarounds.alwaysCallUseProgramAfterLink)
155 {
156 mStateManager->forceUseProgram(mProgramID);
157 }
158
Geoff Lang65a0be92015-10-02 09:57:30 -0400159 postLink();
Geoff Langb1f435e2015-02-20 10:01:01 -0500160
Geoff Langb1f435e2015-02-20 10:01:01 -0500161 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -0500162}
163
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400164GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
165{
166 // TODO(jmadill): implement validate
167 return true;
168}
169
Geoff Langf9a6f082015-01-22 13:32:49 -0500170void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
171{
Geoff Lang63cbace2015-02-26 10:03:12 -0500172 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400173 mFunctions->uniform1fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500174}
175
176void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
177{
Geoff Lang63cbace2015-02-26 10:03:12 -0500178 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400179 mFunctions->uniform2fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500180}
181
182void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
183{
Geoff Lang63cbace2015-02-26 10:03:12 -0500184 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400185 mFunctions->uniform3fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500186}
187
188void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
189{
Geoff Lang63cbace2015-02-26 10:03:12 -0500190 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400191 mFunctions->uniform4fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500192}
193
194void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
195{
Geoff Lang63cbace2015-02-26 10:03:12 -0500196 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400197 mFunctions->uniform1iv(uniLoc(location), count, v);
Geoff Langf51bc792015-05-04 14:57:03 -0400198
Jamie Madill48ef11b2016-04-27 15:21:52 -0400199 const gl::VariableLocation &locationEntry = mState.getUniformLocations()[location];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400200
201 size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index];
202 if (samplerIndex != GL_INVALID_INDEX)
Geoff Langf51bc792015-05-04 14:57:03 -0400203 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400204 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits;
Geoff Langf51bc792015-05-04 14:57:03 -0400205
Jamie Madill62d31cb2015-09-11 13:25:51 -0400206 size_t copyCount =
207 std::max<size_t>(count, boundTextureUnits.size() - locationEntry.element);
208 std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element);
Geoff Langf51bc792015-05-04 14:57:03 -0400209 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500210}
211
212void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
213{
Geoff Lang63cbace2015-02-26 10:03:12 -0500214 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400215 mFunctions->uniform2iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500216}
217
218void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
219{
Geoff Lang63cbace2015-02-26 10:03:12 -0500220 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400221 mFunctions->uniform3iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500222}
223
224void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
225{
Geoff Lang63cbace2015-02-26 10:03:12 -0500226 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400227 mFunctions->uniform4iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500228}
229
230void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
231{
Geoff Lang63cbace2015-02-26 10:03:12 -0500232 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500233 mFunctions->uniform1uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500234}
235
236void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
237{
Geoff Lang63cbace2015-02-26 10:03:12 -0500238 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400239 mFunctions->uniform2uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500240}
241
242void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
243{
Geoff Lang63cbace2015-02-26 10:03:12 -0500244 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400245 mFunctions->uniform3uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500246}
247
248void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
249{
Geoff Lang63cbace2015-02-26 10:03:12 -0500250 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251 mFunctions->uniform4uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500252}
253
254void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
255{
Geoff Lang63cbace2015-02-26 10:03:12 -0500256 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400257 mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500258}
259
260void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
261{
Geoff Lang63cbace2015-02-26 10:03:12 -0500262 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400263 mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500264}
265
266void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
267{
Geoff Lang63cbace2015-02-26 10:03:12 -0500268 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400269 mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500270}
271
272void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
273{
Geoff Lang63cbace2015-02-26 10:03:12 -0500274 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400275 mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500276}
277
278void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
279{
Geoff Lang63cbace2015-02-26 10:03:12 -0500280 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400281 mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500282}
283
284void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
285{
Geoff Lang63cbace2015-02-26 10:03:12 -0500286 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400287 mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500288}
289
290void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
291{
Geoff Lang63cbace2015-02-26 10:03:12 -0500292 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400293 mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500294}
295
296void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
297{
Geoff Lang63cbace2015-02-26 10:03:12 -0500298 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400299 mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500300}
301
302void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
303{
Geoff Lang63cbace2015-02-26 10:03:12 -0500304 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400305 mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500306}
307
Geoff Lang5d124a62015-09-15 13:03:27 -0400308void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
309{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400310 // Lazy init
311 if (mUniformBlockRealLocationMap.empty())
312 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400313 mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size());
314 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill4a3c2342015-10-08 12:58:45 -0400315 {
316 const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex();
317 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str());
318 mUniformBlockRealLocationMap.push_back(blockIndex);
319 }
320 }
321
322 GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex];
323 if (realBlockIndex != GL_INVALID_INDEX)
324 {
325 mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding);
326 }
Geoff Lang5d124a62015-09-15 13:03:27 -0400327}
328
Geoff Langb1f435e2015-02-20 10:01:01 -0500329GLuint ProgramGL::getProgramID() const
330{
331 return mProgramID;
332}
333
Geoff Langf51bc792015-05-04 14:57:03 -0400334const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
335{
336 return mSamplerBindings;
337}
338
Jamie Madill4a3c2342015-10-08 12:58:45 -0400339bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400340{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400341 ASSERT(mProgramID != 0u);
Geoff Lang5d124a62015-09-15 13:03:27 -0400342
Jamie Madill4a3c2342015-10-08 12:58:45 -0400343 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str());
344 if (blockIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400345 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400346 *sizeOut = 0;
347 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400348 }
349
Jamie Madill4a3c2342015-10-08 12:58:45 -0400350 GLint dataSize = 0;
351 mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
352 &dataSize);
353 *sizeOut = static_cast<size_t>(dataSize);
354 return true;
355}
356
357bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName,
358 sh::BlockMemberInfo *memberInfoOut) const
359{
360 GLuint uniformIndex;
361 const GLchar *memberNameGLStr = memberUniformName.c_str();
362 mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
363
364 if (uniformIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400365 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400366 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
367 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400368 }
Jamie Madill4a3c2342015-10-08 12:58:45 -0400369
370 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
371 &memberInfoOut->offset);
372 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
373 &memberInfoOut->arrayStride);
374 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
375 &memberInfoOut->matrixStride);
376
377 // TODO(jmadill): possibly determine this at the gl::Program level.
378 GLint isRowMajorMatrix = 0;
379 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
380 &isRowMajorMatrix);
381 memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE;
382 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400383}
Jamie Madill4a3c2342015-10-08 12:58:45 -0400384
Geoff Lang65a0be92015-10-02 09:57:30 -0400385void ProgramGL::preLink()
386{
387 // Reset the program state
388 mUniformRealLocationMap.clear();
389 mUniformBlockRealLocationMap.clear();
390 mSamplerBindings.clear();
391 mUniformIndexToSamplerIndex.clear();
392}
393
394bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog)
395{
396 GLint linkStatus = GL_FALSE;
397 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
398 if (linkStatus == GL_FALSE)
399 {
400 // Linking failed, put the error into the info log
401 GLint infoLogLength = 0;
402 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
403
404 std::string warning;
405
406 // Info log length includes the null terminator, so 1 means that the info log is an empty
407 // string.
408 if (infoLogLength > 1)
409 {
410 std::vector<char> buf(infoLogLength);
411 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
412
413 mFunctions->deleteProgram(mProgramID);
414 mProgramID = 0;
415
416 infoLog << buf.data();
417
418 warning = FormatString("Program link failed unexpectedly: %s", buf.data());
419 }
420 else
421 {
422 warning = "Program link failed unexpectedly with no info log.";
423 }
424 ANGLEPlatformCurrent()->logWarning(warning.c_str());
425 TRACE("\n%s", warning.c_str());
426
427 // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
428 return false;
429 }
430
431 return true;
432}
433
434void ProgramGL::postLink()
435{
436 // Query the uniform information
437 ASSERT(mUniformRealLocationMap.empty());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400438 const auto &uniformLocations = mState.getUniformLocations();
439 const auto &uniforms = mState.getUniforms();
Geoff Lang65a0be92015-10-02 09:57:30 -0400440 mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX);
441 for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++)
442 {
443 const auto &entry = uniformLocations[uniformLocation];
444 if (!entry.used)
445 {
446 continue;
447 }
448
449 // From the spec:
450 // "Locations for sequential array indices are not required to be sequential."
451 const gl::LinkedUniform &uniform = uniforms[entry.index];
452 std::stringstream fullNameStr;
453 fullNameStr << uniform.name;
454 if (uniform.isArray())
455 {
456 fullNameStr << "[" << entry.element << "]";
457 }
458 const std::string &fullName = fullNameStr.str();
459
460 GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str());
461 mUniformRealLocationMap[uniformLocation] = realLocation;
462 }
463
Jamie Madill48ef11b2016-04-27 15:21:52 -0400464 mUniformIndexToSamplerIndex.resize(mState.getUniforms().size(), GL_INVALID_INDEX);
Geoff Lang65a0be92015-10-02 09:57:30 -0400465
466 for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId)
467 {
468 const gl::LinkedUniform &linkedUniform = uniforms[uniformId];
469
470 if (!linkedUniform.isSampler() || !linkedUniform.staticUse)
471 continue;
472
473 mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size();
474
475 // If uniform is a sampler type, insert it into the mSamplerBindings array
476 SamplerBindingGL samplerBinding;
477 samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type);
478 samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0);
479 mSamplerBindings.push_back(samplerBinding);
480 }
481}
482
Jamie Madill4a3c2342015-10-08 12:58:45 -0400483} // namespace rx