blob: b05d7f5b50910e8e9d55b0ab4f76a9aadb5fa2cc [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{
292 mFunctions->uniformBlockBinding(mProgramID, mUniformBlockRealLocationMap[uniformBlockIndex],
293 uniformBlockBinding);
294}
295
Geoff Lang01306fc2015-10-05 16:53:10 +0000296void ProgramGL::reset()
297{
298 mUniformRealLocationMap.clear();
299 mSamplerBindings.clear();
300 mUniformIndexToSamplerIndex.clear();
301}
302
Geoff Langb1f435e2015-02-20 10:01:01 -0500303GLuint ProgramGL::getProgramID() const
304{
305 return mProgramID;
306}
307
Geoff Langf51bc792015-05-04 14:57:03 -0400308const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
309{
310 return mSamplerBindings;
311}
312
Geoff Lang5d124a62015-09-15 13:03:27 -0400313void ProgramGL::gatherUniformBlockInfo(std::vector<gl::UniformBlock> *uniformBlocks,
314 std::vector<gl::LinkedUniform> *uniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400315{
Geoff Lang5d124a62015-09-15 13:03:27 -0400316 mUniformBlockRealLocationMap.resize(uniformBlocks->size(), 0);
317
318 for (int i = 0; i < static_cast<int>(uniformBlocks->size()); i++)
319 {
320 auto &uniformBlock = uniformBlocks->at(i);
321
322 std::stringstream fullNameStr;
323 fullNameStr << uniformBlock.name;
324 if (uniformBlock.isArray)
325 {
326 fullNameStr << "[" << uniformBlock.arrayElement << "]";
327 }
328
329 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, fullNameStr.str().c_str());
330 if (blockIndex != GL_INVALID_INDEX)
331 {
332 mUniformBlockRealLocationMap[i] = blockIndex;
333
334 GLint dataSize = 0;
335 mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
336 &dataSize);
337 uniformBlock.dataSize = dataSize;
338 }
339 else
340 {
341 // Remove this uniform block
342 uniformBlocks->erase(uniformBlocks->begin() + i);
343 i--;
344 }
345 }
346
347 for (int uniformIdx = 0; uniformIdx < static_cast<int>(uniforms->size()); uniformIdx++)
348 {
349 auto &uniform = uniforms->at(uniformIdx);
350 if (uniform.isInDefaultBlock())
351 {
352 continue;
353 }
354
355 const GLchar *uniformName = uniform.name.c_str();
356 GLuint uniformIndex = 0;
357 mFunctions->getUniformIndices(mProgramID, 1, &uniformName, &uniformIndex);
358
Corentin Wallez4fbec002015-10-01 11:47:47 -0700359 if (uniformIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400360 {
361 // Uniform member has been optimized out, remove it from the list
362 // TODO: Clean this up by using a class to wrap around the uniforms so manual removal is
363 // not needed.
364 for (size_t uniformBlockIdx = 0; uniformBlockIdx < uniformBlocks->size();
365 uniformBlockIdx++)
366 {
367 auto &uniformBlock = uniformBlocks->at(uniformBlockIdx);
368 for (int memberIndex = 0;
369 memberIndex < static_cast<int>(uniformBlock.memberUniformIndexes.size());
370 memberIndex++)
371 {
372 if (uniformBlock.memberUniformIndexes[memberIndex] ==
373 static_cast<unsigned int>(uniformIdx))
374 {
375 uniformBlock.memberUniformIndexes.erase(
376 uniformBlock.memberUniformIndexes.begin() + memberIndex);
377 memberIndex--;
378 }
379 else if (uniformBlock.memberUniformIndexes[memberIndex] >
380 static_cast<unsigned int>(uniformIdx))
381 {
382 uniformBlock.memberUniformIndexes[memberIndex]--;
383 }
384 }
385 }
386 uniforms->erase(uniforms->begin() + uniformIdx);
387 uniformIdx--;
388 }
389 else
390 {
391 GLint offset = 0;
392 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
393 &offset);
394 uniform.blockInfo.offset = offset;
395
396 GLint arrayStride = 0;
397 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
398 &arrayStride);
399 uniform.blockInfo.arrayStride = arrayStride;
400
401 GLint matrixStride = 0;
402 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
403 &matrixStride);
404 uniform.blockInfo.matrixStride = matrixStride;
405
406 // TODO: determine this at the gl::Program level.
407 GLint isRowMajorMatrix = 0;
408 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
409 &isRowMajorMatrix);
410 uniform.blockInfo.isRowMajorMatrix = isRowMajorMatrix != GL_FALSE;
411 }
412 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400413}
Geoff Langf9a6f082015-01-22 13:32:49 -0500414}