blob: bae83899706914ce5781f63982f0cf2e8e175bc0 [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
Jamie Madill62d31cb2015-09-11 13:25:51 -0400118 ASSERT(mUniformRealLocationMap.empty());
119 const auto &uniforms = mData.getUniforms();
120 for (const gl::VariableLocation &entry : mData.getUniformLocations())
Geoff Langb1f435e2015-02-20 10:01:01 -0500121 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400122 // From the spec:
123 // "Locations for sequential array indices are not required to be sequential."
124 const gl::LinkedUniform &uniform = uniforms[entry.index];
125 std::stringstream fullNameStr;
126 fullNameStr << uniform.name;
127 if (uniform.isArray())
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400128 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400129 fullNameStr << "[" << entry.element << "]";
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400130 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400131 const std::string &fullName = fullNameStr.str();
Geoff Lang5ed74cf2015-04-14 13:57:07 -0400132
Jamie Madill62d31cb2015-09-11 13:25:51 -0400133 GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str());
134 mUniformRealLocationMap.push_back(realLocation);
135 }
Geoff Langb1f435e2015-02-20 10:01:01 -0500136
Jamie Madill62d31cb2015-09-11 13:25:51 -0400137 mUniformIndexToSamplerIndex.resize(mData.getUniforms().size(), GL_INVALID_INDEX);
138
139 for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId)
140 {
141 const gl::LinkedUniform &linkedUniform = uniforms[uniformId];
142
143 if (!linkedUniform.isSampler() || !linkedUniform.staticUse)
144 continue;
145
146 mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size();
Geoff Langf51bc792015-05-04 14:57:03 -0400147
148 // If uniform is a sampler type, insert it into the mSamplerBindings array
Jamie Madill62d31cb2015-09-11 13:25:51 -0400149 SamplerBindingGL samplerBinding;
150 samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type);
151 samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0);
152 mSamplerBindings.push_back(samplerBinding);
Geoff Langb1f435e2015-02-20 10:01:01 -0500153 }
154
Geoff Langb1f435e2015-02-20 10:01:01 -0500155 return LinkResult(true, gl::Error(GL_NO_ERROR));
Geoff Langf9a6f082015-01-22 13:32:49 -0500156}
157
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400158GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
159{
160 // TODO(jmadill): implement validate
161 return true;
162}
163
Geoff Langf9a6f082015-01-22 13:32:49 -0500164void ProgramGL::setUniform1fv(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->uniform1fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500168}
169
170void ProgramGL::setUniform2fv(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->uniform2fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500174}
175
176void ProgramGL::setUniform3fv(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->uniform3fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500180}
181
182void ProgramGL::setUniform4fv(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->uniform4fv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500186}
187
188void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
189{
Geoff Lang63cbace2015-02-26 10:03:12 -0500190 mStateManager->useProgram(mProgramID);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400191 mFunctions->uniform1iv(uniLoc(location), count, v);
Geoff Langf51bc792015-05-04 14:57:03 -0400192
Jamie Madill62d31cb2015-09-11 13:25:51 -0400193 const gl::VariableLocation &locationEntry = mData.getUniformLocations()[location];
194
195 size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index];
196 if (samplerIndex != GL_INVALID_INDEX)
Geoff Langf51bc792015-05-04 14:57:03 -0400197 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400198 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits;
Geoff Langf51bc792015-05-04 14:57:03 -0400199
Jamie Madill62d31cb2015-09-11 13:25:51 -0400200 size_t copyCount =
201 std::max<size_t>(count, boundTextureUnits.size() - locationEntry.element);
202 std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element);
Geoff Langf51bc792015-05-04 14:57:03 -0400203 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500204}
205
206void ProgramGL::setUniform2iv(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->uniform2iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500210}
211
212void ProgramGL::setUniform3iv(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->uniform3iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500216}
217
218void ProgramGL::setUniform4iv(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->uniform4iv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500222}
223
224void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
225{
Geoff Lang63cbace2015-02-26 10:03:12 -0500226 mStateManager->useProgram(mProgramID);
Geoff Langb1f435e2015-02-20 10:01:01 -0500227 mFunctions->uniform1uiv(location, count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500228}
229
230void ProgramGL::setUniform2uiv(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->uniform2uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500234}
235
236void ProgramGL::setUniform3uiv(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->uniform3uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500240}
241
242void ProgramGL::setUniform4uiv(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->uniform4uiv(uniLoc(location), count, v);
Geoff Langf9a6f082015-01-22 13:32:49 -0500246}
247
248void ProgramGL::setUniformMatrix2fv(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->uniformMatrix2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500252}
253
254void ProgramGL::setUniformMatrix3fv(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->uniformMatrix3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500258}
259
260void ProgramGL::setUniformMatrix4fv(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->uniformMatrix4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500264}
265
266void ProgramGL::setUniformMatrix2x3fv(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->uniformMatrix2x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500270}
271
272void ProgramGL::setUniformMatrix3x2fv(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->uniformMatrix3x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500276}
277
278void ProgramGL::setUniformMatrix2x4fv(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->uniformMatrix2x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500282}
283
284void ProgramGL::setUniformMatrix4x2fv(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->uniformMatrix4x2fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500288}
289
290void ProgramGL::setUniformMatrix3x4fv(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->uniformMatrix3x4fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500294}
295
296void ProgramGL::setUniformMatrix4x3fv(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->uniformMatrix4x3fv(uniLoc(location), count, transpose, value);
Geoff Langf9a6f082015-01-22 13:32:49 -0500300}
301
Geoff Langf9a6f082015-01-22 13:32:49 -0500302bool ProgramGL::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
303{
Geoff Lang35d315c2015-03-31 12:48:54 -0400304 //UNIMPLEMENTED();
Geoff Langb1f435e2015-02-20 10:01:01 -0500305 return true;
Geoff Langf9a6f082015-01-22 13:32:49 -0500306}
307
Geoff Langb1f435e2015-02-20 10:01:01 -0500308void ProgramGL::reset()
309{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400310 mUniformRealLocationMap.clear();
Geoff Langf51bc792015-05-04 14:57:03 -0400311 mSamplerBindings.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400312 mUniformIndexToSamplerIndex.clear();
Geoff Langb1f435e2015-02-20 10:01:01 -0500313}
314
315GLuint ProgramGL::getProgramID() const
316{
317 return mProgramID;
318}
319
Geoff Langf51bc792015-05-04 14:57:03 -0400320const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
321{
322 return mSamplerBindings;
323}
324
Jamie Madill62d31cb2015-09-11 13:25:51 -0400325void ProgramGL::gatherUniformBlockInfo(std::vector<gl::UniformBlock> * /*uniformBlocks*/,
326 std::vector<gl::LinkedUniform> * /*uniforms*/)
327{
328 // TODO(jmadill): Gather uniform block layout info, and data sizes.
329}
Geoff Langf9a6f082015-01-22 13:32:49 -0500330}