blob: b3e36b0dc3d0eacc122ad8b110dfb3e89f80cabe [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 -050037LinkResult ProgramGL::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
38{
Geoff Lang01306fc2015-10-05 16:53:10 +000039 UNIMPLEMENTED();
40 return LinkResult(false, gl::Error(GL_INVALID_OPERATION));
Geoff Langf9a6f082015-01-22 13:32:49 -050041}
42
43gl::Error ProgramGL::save(gl::BinaryOutputStream *stream)
44{
Geoff Lang01306fc2015-10-05 16:53:10 +000045 UNIMPLEMENTED();
46 return gl::Error(GL_INVALID_OPERATION);
Geoff Langf9a6f082015-01-22 13:32:49 -050047}
48
Jamie Madillf5f4ad22015-09-02 18:32:38 +000049LinkResult ProgramGL::link(const gl::Data &data, gl::InfoLog &infoLog)
Geoff Langf9a6f082015-01-22 13:32:49 -050050{
Geoff Lang01306fc2015-10-05 16:53:10 +000051 // Reset the program state, delete the current program if one exists
52 reset();
Geoff Langb1f435e2015-02-20 10:01:01 -050053
Geoff Lang01306fc2015-10-05 16:53:10 +000054 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
55 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
56
57 const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(vertexShader);
58 const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(fragmentShader);
Geoff Langb1f435e2015-02-20 10:01:01 -050059
Geoff Langb1f435e2015-02-20 10:01:01 -050060 // Attach the shaders
61 mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
62 mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
63
Geoff Lang1528e562015-08-24 15:10:58 -040064 // Bind attribute locations to match the GL layer.
65 for (const sh::Attribute &attribute : mData.getAttributes())
66 {
67 if (!attribute.staticUse)
68 {
69 continue;
70 }
71
72 mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str());
Geoff Lang1528e562015-08-24 15:10:58 -040073 }
74
Geoff Langb1f435e2015-02-20 10:01:01 -050075 // Link and verify
76 mFunctions->linkProgram(mProgramID);
77
Geoff Lang0ca53782015-05-07 13:49:39 -040078 // Detach the shaders
79 mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID());
80 mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID());
81
82 // Verify the link
Geoff Lang01306fc2015-10-05 16:53:10 +000083 GLint linkStatus = GL_FALSE;
84 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
85 ASSERT(linkStatus == GL_TRUE);
86 if (linkStatus == GL_FALSE)
Geoff Langb1f435e2015-02-20 10:01:01 -050087 {
Geoff Lang01306fc2015-10-05 16:53:10 +000088 // Linking failed, put the error into the info log
89 GLint infoLogLength = 0;
90 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
91
92 std::vector<char> buf(infoLogLength);
93 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
94
95 mFunctions->deleteProgram(mProgramID);
96 mProgramID = 0;
97
98 infoLog << &buf[0];
99 TRACE("\n%s", &buf[0]);
100
101 // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
Geoff Langb1f435e2015-02-20 10:01:01 -0500102 return LinkResult(false, gl::Error(GL_NO_ERROR));
103 }
104
Geoff Lang01306fc2015-10-05 16:53:10 +0000105 // Query the uniform information
106 ASSERT(mUniformRealLocationMap.empty());
107 const auto &uniforms = mData.getUniforms();
108 for (const gl::VariableLocation &entry : mData.getUniformLocations())
109 {
110 // From the spec:
111 // "Locations for sequential array indices are not required to be sequential."
112 const gl::LinkedUniform &uniform = uniforms[entry.index];
113 std::stringstream fullNameStr;
114 fullNameStr << uniform.name;
115 if (uniform.isArray())
116 {
117 fullNameStr << "[" << entry.element << "]";
118 }
119 const std::string &fullName = fullNameStr.str();
120
121 GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str());
122 mUniformRealLocationMap.push_back(realLocation);
123 }
124
125 mUniformIndexToSamplerIndex.resize(mData.getUniforms().size(), GL_INVALID_INDEX);
126
127 for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId)
128 {
129 const gl::LinkedUniform &linkedUniform = uniforms[uniformId];
130
131 if (!linkedUniform.isSampler() || !linkedUniform.staticUse)
132 continue;
133
134 mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size();
135
136 // If uniform is a sampler type, insert it into the mSamplerBindings array
137 SamplerBindingGL samplerBinding;
138 samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type);
139 samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0);
140 mSamplerBindings.push_back(samplerBinding);
141 }
Geoff Langb1f435e2015-02-20 10:01:01 -0500142
Geoff Langb1f435e2015-02-20 10:01:01 -0500143 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -0500144}
145
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400146GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
147{
148 // TODO(jmadill): implement validate
149 return true;
150}
151
Geoff Langf9a6f082015-01-22 13:32:49 -0500152void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
153{
Geoff Lang63cbace2015-02-26 10:03:12 -0500154 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400155 mFunctions->uniform1fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500156}
157
158void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
159{
Geoff Lang63cbace2015-02-26 10:03:12 -0500160 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400161 mFunctions->uniform2fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500162}
163
164void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
165{
Geoff Lang63cbace2015-02-26 10:03:12 -0500166 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400167 mFunctions->uniform3fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500168}
169
170void ProgramGL::setUniform4fv(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->uniform4fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500174}
175
176void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
177{
Geoff Lang63cbace2015-02-26 10:03:12 -0500178 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400179 mFunctions->uniform1iv(uniLoc(location), count, v);
Geoff Langf51bc792015-05-04 14:57:03 -0400180
Jamie Madill62d31cb2015-09-11 13:25:51 -0400181 const gl::VariableLocation &locationEntry = mData.getUniformLocations()[location];
182
183 size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index];
184 if (samplerIndex != GL_INVALID_INDEX)
Geoff Langf51bc792015-05-04 14:57:03 -0400185 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400186 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits;
Geoff Langf51bc792015-05-04 14:57:03 -0400187
Jamie Madill62d31cb2015-09-11 13:25:51 -0400188 size_t copyCount =
189 std::max<size_t>(count, boundTextureUnits.size() - locationEntry.element);
190 std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element);
Geoff Langf51bc792015-05-04 14:57:03 -0400191 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500192}
193
194void ProgramGL::setUniform2iv(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->uniform2iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500198}
199
200void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
201{
Geoff Lang63cbace2015-02-26 10:03:12 -0500202 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400203 mFunctions->uniform3iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500204}
205
206void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
207{
Geoff Lang63cbace2015-02-26 10:03:12 -0500208 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400209 mFunctions->uniform4iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500210}
211
212void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
213{
Geoff Lang63cbace2015-02-26 10:03:12 -0500214 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500215 mFunctions->uniform1uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500216}
217
218void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
219{
Geoff Lang63cbace2015-02-26 10:03:12 -0500220 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400221 mFunctions->uniform2uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500222}
223
224void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
225{
Geoff Lang63cbace2015-02-26 10:03:12 -0500226 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400227 mFunctions->uniform3uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500228}
229
230void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
231{
Geoff Lang63cbace2015-02-26 10:03:12 -0500232 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400233 mFunctions->uniform4uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500234}
235
236void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
237{
Geoff Lang63cbace2015-02-26 10:03:12 -0500238 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400239 mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500240}
241
242void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
243{
Geoff Lang63cbace2015-02-26 10:03:12 -0500244 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400245 mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500246}
247
248void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
249{
Geoff Lang63cbace2015-02-26 10:03:12 -0500250 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251 mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500252}
253
254void ProgramGL::setUniformMatrix2x3fv(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->uniformMatrix2x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500258}
259
260void ProgramGL::setUniformMatrix3x2fv(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->uniformMatrix3x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500264}
265
266void ProgramGL::setUniformMatrix2x4fv(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->uniformMatrix2x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500270}
271
272void ProgramGL::setUniformMatrix4x2fv(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->uniformMatrix4x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500276}
277
278void ProgramGL::setUniformMatrix3x4fv(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->uniformMatrix3x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500282}
283
284void ProgramGL::setUniformMatrix4x3fv(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->uniformMatrix4x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500288}
289
Geoff Lang5d124a62015-09-15 13:03:27 -0400290void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
291{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400292 // Lazy init
293 if (mUniformBlockRealLocationMap.empty())
294 {
295 mUniformBlockRealLocationMap.reserve(mData.getUniformBlocks().size());
296 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
297 {
298 const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex();
299 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str());
300 mUniformBlockRealLocationMap.push_back(blockIndex);
301 }
302 }
303
304 GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex];
305 if (realBlockIndex != GL_INVALID_INDEX)
306 {
307 mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding);
308 }
Geoff Lang5d124a62015-09-15 13:03:27 -0400309}
310
Geoff Lang01306fc2015-10-05 16:53:10 +0000311void ProgramGL::reset()
312{
313 mUniformRealLocationMap.clear();
Jamie Madill4a3c2342015-10-08 12:58:45 -0400314 mUniformBlockRealLocationMap.clear();
Geoff Lang01306fc2015-10-05 16:53:10 +0000315 mSamplerBindings.clear();
316 mUniformIndexToSamplerIndex.clear();
317}
318
Geoff Langb1f435e2015-02-20 10:01:01 -0500319GLuint ProgramGL::getProgramID() const
320{
321 return mProgramID;
322}
323
Geoff Langf51bc792015-05-04 14:57:03 -0400324const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
325{
326 return mSamplerBindings;
327}
328
Jamie Madill4a3c2342015-10-08 12:58:45 -0400329bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400330{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400331 ASSERT(mProgramID != 0u);
Geoff Lang5d124a62015-09-15 13:03:27 -0400332
Jamie Madill4a3c2342015-10-08 12:58:45 -0400333 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str());
334 if (blockIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400335 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400336 *sizeOut = 0;
337 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400338 }
339
Jamie Madill4a3c2342015-10-08 12:58:45 -0400340 GLint dataSize = 0;
341 mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
342 &dataSize);
343 *sizeOut = static_cast<size_t>(dataSize);
344 return true;
345}
346
347bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName,
348 sh::BlockMemberInfo *memberInfoOut) const
349{
350 GLuint uniformIndex;
351 const GLchar *memberNameGLStr = memberUniformName.c_str();
352 mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
353
354 if (uniformIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400355 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400356 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
357 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400358 }
Jamie Madill4a3c2342015-10-08 12:58:45 -0400359
360 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
361 &memberInfoOut->offset);
362 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
363 &memberInfoOut->arrayStride);
364 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
365 &memberInfoOut->matrixStride);
366
367 // TODO(jmadill): possibly determine this at the gl::Program level.
368 GLint isRowMajorMatrix = 0;
369 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
370 &isRowMajorMatrix);
371 memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE;
372 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400373}
Jamie Madill4a3c2342015-10-08 12:58:45 -0400374
375} // namespace rx