blob: 06e723347150b7b5a619779727299e8b749493a8 [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
Jamie Madilla7d12dc2016-12-13 15:08:19 -050011#include "common/BitSetIterator.h"
Sami Väisänen46eaa942016-06-29 10:26:37 +030012#include "common/angleutils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050013#include "common/debug.h"
Sami Väisänen46eaa942016-06-29 10:26:37 +030014#include "common/string_utils.h"
Geoff Lang5ed74cf2015-04-14 13:57:07 -040015#include "common/utilities.h"
Jamie Madilla7d12dc2016-12-13 15:08:19 -050016#include "libANGLE/renderer/gl/ContextGL.h"
Geoff Langb1f435e2015-02-20 10:01:01 -050017#include "libANGLE/renderer/gl/FunctionsGL.h"
18#include "libANGLE/renderer/gl/ShaderGL.h"
19#include "libANGLE/renderer/gl/StateManagerGL.h"
Philippe Hamel40911192016-04-07 16:45:50 -040020#include "libANGLE/renderer/gl/WorkaroundsGL.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040021#include "libANGLE/Uniform.h"
unknownb4a3af22015-11-25 15:02:51 -050022#include "platform/Platform.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050023
24namespace rx
25{
26
Jamie Madill48ef11b2016-04-27 15:21:52 -040027ProgramGL::ProgramGL(const gl::ProgramState &data,
Jamie Madill5c6b7bf2015-08-17 12:53:35 -040028 const FunctionsGL *functions,
Philippe Hamel40911192016-04-07 16:45:50 -040029 const WorkaroundsGL &workarounds,
Sami Väisänen46eaa942016-06-29 10:26:37 +030030 StateManagerGL *stateManager,
31 bool enablePathRendering)
Philippe Hamel40911192016-04-07 16:45:50 -040032 : ProgramImpl(data),
33 mFunctions(functions),
34 mWorkarounds(workarounds),
35 mStateManager(stateManager),
Sami Väisänen46eaa942016-06-29 10:26:37 +030036 mEnablePathRendering(enablePathRendering),
Philippe Hamel40911192016-04-07 16:45:50 -040037 mProgramID(0)
Geoff Langb1f435e2015-02-20 10:01:01 -050038{
39 ASSERT(mFunctions);
40 ASSERT(mStateManager);
Geoff Lang0ca53782015-05-07 13:49:39 -040041
42 mProgramID = mFunctions->createProgram();
Geoff Langb1f435e2015-02-20 10:01:01 -050043}
Geoff Langf9a6f082015-01-22 13:32:49 -050044
45ProgramGL::~ProgramGL()
Geoff Langb1f435e2015-02-20 10:01:01 -050046{
Geoff Lang0ca53782015-05-07 13:49:39 -040047 mFunctions->deleteProgram(mProgramID);
48 mProgramID = 0;
Geoff Langb1f435e2015-02-20 10:01:01 -050049}
Geoff Langf9a6f082015-01-22 13:32:49 -050050
Jamie Madilla7d12dc2016-12-13 15:08:19 -050051LinkResult ProgramGL::load(const ContextImpl *contextImpl,
52 gl::InfoLog &infoLog,
53 gl::BinaryInputStream *stream)
Geoff Langf9a6f082015-01-22 13:32:49 -050054{
Geoff Lang65a0be92015-10-02 09:57:30 -040055 preLink();
56
57 // Read the binary format, size and blob
58 GLenum binaryFormat = stream->readInt<GLenum>();
59 GLint binaryLength = stream->readInt<GLint>();
60 const uint8_t *binary = stream->data() + stream->offset();
61 stream->skip(binaryLength);
62
63 // Load the binary
64 mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength);
65
66 // Verify that the program linked
67 if (!checkLinkStatus(infoLog))
68 {
Jamie Madillb0a838b2016-11-13 20:02:12 -050069 return false;
Geoff Lang65a0be92015-10-02 09:57:30 -040070 }
71
72 postLink();
73
Jamie Madilla7d12dc2016-12-13 15:08:19 -050074 // Re-apply UBO bindings to work around driver bugs.
75 const WorkaroundsGL &workaroundsGL = GetAs<ContextGL>(contextImpl)->getWorkaroundsGL();
76 if (workaroundsGL.reapplyUBOBindingsAfterLoadingBinaryProgram)
77 {
78 for (GLuint bindingIndex : angle::IterateBitSet(mState.getActiveUniformBlockBindingsMask()))
79 {
80 setUniformBlockBinding(bindingIndex, mState.getUniformBlockBinding(bindingIndex));
81 }
82 }
83
Jamie Madillb0a838b2016-11-13 20:02:12 -050084 return true;
Geoff Langf9a6f082015-01-22 13:32:49 -050085}
86
87gl::Error ProgramGL::save(gl::BinaryOutputStream *stream)
88{
Geoff Lang65a0be92015-10-02 09:57:30 -040089 GLint binaryLength = 0;
90 mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
91
92 std::vector<uint8_t> binary(binaryLength);
93 GLenum binaryFormat = GL_NONE;
94 mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat,
95 &binary[0]);
96
97 stream->writeInt(binaryFormat);
98 stream->writeInt(binaryLength);
99 stream->writeBytes(&binary[0], binaryLength);
100
101 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500102}
103
Geoff Langc5629752015-12-07 16:29:04 -0500104void ProgramGL::setBinaryRetrievableHint(bool retrievable)
105{
Geoff Lang65a0be92015-10-02 09:57:30 -0400106 // glProgramParameteri isn't always available on ES backends.
107 if (mFunctions->programParameteri)
108 {
109 mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT,
110 retrievable ? GL_TRUE : GL_FALSE);
111 }
Geoff Langc5629752015-12-07 16:29:04 -0500112}
113
Jamie Madill192745a2016-12-22 15:58:21 -0500114LinkResult ProgramGL::link(const gl::ContextState &data,
115 const gl::VaryingPacking &packing,
116 gl::InfoLog &infoLog)
Geoff Langf9a6f082015-01-22 13:32:49 -0500117{
Geoff Lang65a0be92015-10-02 09:57:30 -0400118 preLink();
Geoff Langb1f435e2015-02-20 10:01:01 -0500119
Martin Radev4c4c8e72016-08-04 12:25:34 +0300120 if (mState.getAttachedComputeShader())
Geoff Lang1a683462015-09-29 15:09:59 -0400121 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300122 const ShaderGL *computeShaderGL = GetImplAs<ShaderGL>(mState.getAttachedComputeShader());
Geoff Lang1a683462015-09-29 15:09:59 -0400123
Martin Radev4c4c8e72016-08-04 12:25:34 +0300124 mFunctions->attachShader(mProgramID, computeShaderGL->getShaderID());
125
126 // Link and verify
127 mFunctions->linkProgram(mProgramID);
128
129 // Detach the shaders
130 mFunctions->detachShader(mProgramID, computeShaderGL->getShaderID());
Geoff Lang1a683462015-09-29 15:09:59 -0400131 }
132 else
133 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300134 // Set the transform feedback state
135 std::vector<const GLchar *> transformFeedbackVaryings;
136 for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames())
Geoff Lang1528e562015-08-24 15:10:58 -0400137 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300138 transformFeedbackVaryings.push_back(tfVarying.c_str());
Geoff Lang1528e562015-08-24 15:10:58 -0400139 }
140
Martin Radev4c4c8e72016-08-04 12:25:34 +0300141 if (transformFeedbackVaryings.empty())
142 {
143 if (mFunctions->transformFeedbackVaryings)
144 {
145 mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr,
146 mState.getTransformFeedbackBufferMode());
147 }
148 }
149 else
150 {
151 ASSERT(mFunctions->transformFeedbackVaryings);
152 mFunctions->transformFeedbackVaryings(
153 mProgramID, static_cast<GLsizei>(transformFeedbackVaryings.size()),
154 &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode());
155 }
156
157 const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(mState.getAttachedVertexShader());
158 const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(mState.getAttachedFragmentShader());
159
160 // Attach the shaders
161 mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID());
162 mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID());
163
164 // Bind attribute locations to match the GL layer.
165 for (const sh::Attribute &attribute : mState.getAttributes())
166 {
Corentin Wallez6c1cbf52016-11-23 12:44:35 -0500167 if (!attribute.staticUse || attribute.isBuiltIn())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300168 {
169 continue;
170 }
171
172 mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str());
173 }
174
175 // Link and verify
176 mFunctions->linkProgram(mProgramID);
177
178 // Detach the shaders
179 mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID());
180 mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID());
Geoff Lang1528e562015-08-24 15:10:58 -0400181 }
182
Geoff Lang0ca53782015-05-07 13:49:39 -0400183 // Verify the link
Geoff Lang65a0be92015-10-02 09:57:30 -0400184 if (!checkLinkStatus(infoLog))
Geoff Langb1f435e2015-02-20 10:01:01 -0500185 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500186 return false;
Geoff Langb1f435e2015-02-20 10:01:01 -0500187 }
188
Philippe Hamel40911192016-04-07 16:45:50 -0400189 if (mWorkarounds.alwaysCallUseProgramAfterLink)
190 {
191 mStateManager->forceUseProgram(mProgramID);
192 }
193
Geoff Lang65a0be92015-10-02 09:57:30 -0400194 postLink();
Geoff Langb1f435e2015-02-20 10:01:01 -0500195
Jamie Madillb0a838b2016-11-13 20:02:12 -0500196 return true;
Geoff Langf9a6f082015-01-22 13:32:49 -0500197}
198
Jamie Madill36cfd6a2015-08-18 10:46:20 -0400199GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
200{
201 // TODO(jmadill): implement validate
202 return true;
203}
204
Geoff Langf9a6f082015-01-22 13:32:49 -0500205void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
206{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800207 if (mFunctions->programUniform1fv != nullptr)
208 {
209 mFunctions->programUniform1fv(mProgramID, uniLoc(location), count, v);
210 }
211 else
212 {
213 mStateManager->useProgram(mProgramID);
214 mFunctions->uniform1fv(uniLoc(location), count, v);
215 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500216}
217
218void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
219{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800220 if (mFunctions->programUniform2fv != nullptr)
221 {
222 mFunctions->programUniform2fv(mProgramID, uniLoc(location), count, v);
223 }
224 else
225 {
226 mStateManager->useProgram(mProgramID);
227 mFunctions->uniform2fv(uniLoc(location), count, v);
228 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500229}
230
231void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
232{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800233 if (mFunctions->programUniform3fv != nullptr)
234 {
235 mFunctions->programUniform3fv(mProgramID, uniLoc(location), count, v);
236 }
237 else
238 {
239 mStateManager->useProgram(mProgramID);
240 mFunctions->uniform3fv(uniLoc(location), count, v);
241 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500242}
243
244void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
245{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800246 if (mFunctions->programUniform4fv != nullptr)
247 {
248 mFunctions->programUniform4fv(mProgramID, uniLoc(location), count, v);
249 }
250 else
251 {
252 mStateManager->useProgram(mProgramID);
253 mFunctions->uniform4fv(uniLoc(location), count, v);
254 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500255}
256
257void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v)
258{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800259 if (mFunctions->programUniform1iv != nullptr)
260 {
261 mFunctions->programUniform1iv(mProgramID, uniLoc(location), count, v);
262 }
263 else
264 {
265 mStateManager->useProgram(mProgramID);
266 mFunctions->uniform1iv(uniLoc(location), count, v);
267 }
Geoff Langf51bc792015-05-04 14:57:03 -0400268
Jamie Madill48ef11b2016-04-27 15:21:52 -0400269 const gl::VariableLocation &locationEntry = mState.getUniformLocations()[location];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400270
271 size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index];
272 if (samplerIndex != GL_INVALID_INDEX)
Geoff Langf51bc792015-05-04 14:57:03 -0400273 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400274 std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits;
Geoff Langf51bc792015-05-04 14:57:03 -0400275
Jamie Madill62d31cb2015-09-11 13:25:51 -0400276 size_t copyCount =
Corentin Wallez4c5ff002016-10-25 09:32:17 -0400277 std::min<size_t>(count, boundTextureUnits.size() - locationEntry.element);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400278 std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element);
Geoff Langf51bc792015-05-04 14:57:03 -0400279 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500280}
281
282void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v)
283{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800284 if (mFunctions->programUniform2iv != nullptr)
285 {
286 mFunctions->programUniform2iv(mProgramID, uniLoc(location), count, v);
287 }
288 else
289 {
290 mStateManager->useProgram(mProgramID);
291 mFunctions->uniform2iv(uniLoc(location), count, v);
292 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500293}
294
295void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v)
296{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800297 if (mFunctions->programUniform3iv != nullptr)
298 {
299 mFunctions->programUniform3iv(mProgramID, uniLoc(location), count, v);
300 }
301 else
302 {
303 mStateManager->useProgram(mProgramID);
304 mFunctions->uniform3iv(uniLoc(location), count, v);
305 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500306}
307
308void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v)
309{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800310 if (mFunctions->programUniform4iv != nullptr)
311 {
312 mFunctions->programUniform4iv(mProgramID, uniLoc(location), count, v);
313 }
314 else
315 {
316 mStateManager->useProgram(mProgramID);
317 mFunctions->uniform4iv(uniLoc(location), count, v);
318 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500319}
320
321void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
322{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800323 if (mFunctions->programUniform1uiv != nullptr)
324 {
325 mFunctions->programUniform1uiv(mProgramID, uniLoc(location), count, v);
326 }
327 else
328 {
329 mStateManager->useProgram(mProgramID);
330 mFunctions->uniform1uiv(uniLoc(location), count, v);
331 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500332}
333
334void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
335{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800336 if (mFunctions->programUniform2uiv != nullptr)
337 {
338 mFunctions->programUniform2uiv(mProgramID, uniLoc(location), count, v);
339 }
340 else
341 {
342 mStateManager->useProgram(mProgramID);
343 mFunctions->uniform2uiv(uniLoc(location), count, v);
344 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500345}
346
347void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
348{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800349 if (mFunctions->programUniform3uiv != nullptr)
350 {
351 mFunctions->programUniform3uiv(mProgramID, uniLoc(location), count, v);
352 }
353 else
354 {
355 mStateManager->useProgram(mProgramID);
356 mFunctions->uniform3uiv(uniLoc(location), count, v);
357 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500358}
359
360void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
361{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800362 if (mFunctions->programUniform4uiv != nullptr)
363 {
364 mFunctions->programUniform4uiv(mProgramID, uniLoc(location), count, v);
365 }
366 else
367 {
368 mStateManager->useProgram(mProgramID);
369 mFunctions->uniform4uiv(uniLoc(location), count, v);
370 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500371}
372
373void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
374{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800375 if (mFunctions->programUniformMatrix2fv != nullptr)
376 {
377 mFunctions->programUniformMatrix2fv(mProgramID, uniLoc(location), count, transpose, value);
378 }
379 else
380 {
381 mStateManager->useProgram(mProgramID);
382 mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value);
383 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500384}
385
386void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
387{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800388 if (mFunctions->programUniformMatrix3fv != nullptr)
389 {
390 mFunctions->programUniformMatrix3fv(mProgramID, uniLoc(location), count, transpose, value);
391 }
392 else
393 {
394 mStateManager->useProgram(mProgramID);
395 mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value);
396 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500397}
398
399void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
400{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800401 if (mFunctions->programUniformMatrix4fv != nullptr)
402 {
403 mFunctions->programUniformMatrix4fv(mProgramID, uniLoc(location), count, transpose, value);
404 }
405 else
406 {
407 mStateManager->useProgram(mProgramID);
408 mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value);
409 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500410}
411
412void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
413{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800414 if (mFunctions->programUniformMatrix2x3fv != nullptr)
415 {
416 mFunctions->programUniformMatrix2x3fv(mProgramID, uniLoc(location), count, transpose,
417 value);
418 }
419 else
420 {
421 mStateManager->useProgram(mProgramID);
422 mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value);
423 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500424}
425
426void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
427{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800428 if (mFunctions->programUniformMatrix3x2fv != nullptr)
429 {
430 mFunctions->programUniformMatrix3x2fv(mProgramID, uniLoc(location), count, transpose,
431 value);
432 }
433 else
434 {
435 mStateManager->useProgram(mProgramID);
436 mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value);
437 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500438}
439
440void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
441{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800442 if (mFunctions->programUniformMatrix2x4fv != nullptr)
443 {
444 mFunctions->programUniformMatrix2x4fv(mProgramID, uniLoc(location), count, transpose,
445 value);
446 }
447 else
448 {
449 mStateManager->useProgram(mProgramID);
450 mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value);
451 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500452}
453
454void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
455{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800456 if (mFunctions->programUniformMatrix4x2fv != nullptr)
457 {
458 mFunctions->programUniformMatrix4x2fv(mProgramID, uniLoc(location), count, transpose,
459 value);
460 }
461 else
462 {
463 mStateManager->useProgram(mProgramID);
464 mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value);
465 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500466}
467
468void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
469{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800470 if (mFunctions->programUniformMatrix3x4fv != nullptr)
471 {
472 mFunctions->programUniformMatrix3x4fv(mProgramID, uniLoc(location), count, transpose,
473 value);
474 }
475 else
476 {
477 mStateManager->useProgram(mProgramID);
478 mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value);
479 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500480}
481
482void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
483{
Jiajia Qinee9f08c2016-11-16 10:06:10 +0800484 if (mFunctions->programUniformMatrix4x3fv != nullptr)
485 {
486 mFunctions->programUniformMatrix4x3fv(mProgramID, uniLoc(location), count, transpose,
487 value);
488 }
489 else
490 {
491 mStateManager->useProgram(mProgramID);
492 mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value);
493 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500494}
495
Geoff Lang5d124a62015-09-15 13:03:27 -0400496void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
497{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400498 // Lazy init
499 if (mUniformBlockRealLocationMap.empty())
500 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400501 mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size());
502 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill4a3c2342015-10-08 12:58:45 -0400503 {
504 const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex();
505 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str());
506 mUniformBlockRealLocationMap.push_back(blockIndex);
507 }
508 }
509
510 GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex];
511 if (realBlockIndex != GL_INVALID_INDEX)
512 {
513 mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding);
514 }
Geoff Lang5d124a62015-09-15 13:03:27 -0400515}
516
Geoff Langb1f435e2015-02-20 10:01:01 -0500517GLuint ProgramGL::getProgramID() const
518{
519 return mProgramID;
520}
521
Geoff Langf51bc792015-05-04 14:57:03 -0400522const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const
523{
524 return mSamplerBindings;
525}
526
Jamie Madill4a3c2342015-10-08 12:58:45 -0400527bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400528{
Jamie Madill4a3c2342015-10-08 12:58:45 -0400529 ASSERT(mProgramID != 0u);
Geoff Lang5d124a62015-09-15 13:03:27 -0400530
Jamie Madill4a3c2342015-10-08 12:58:45 -0400531 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str());
532 if (blockIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400533 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400534 *sizeOut = 0;
535 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400536 }
537
Jamie Madill4a3c2342015-10-08 12:58:45 -0400538 GLint dataSize = 0;
539 mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE,
540 &dataSize);
541 *sizeOut = static_cast<size_t>(dataSize);
542 return true;
543}
544
545bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName,
546 sh::BlockMemberInfo *memberInfoOut) const
547{
548 GLuint uniformIndex;
549 const GLchar *memberNameGLStr = memberUniformName.c_str();
550 mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
551
552 if (uniformIndex == GL_INVALID_INDEX)
Geoff Lang5d124a62015-09-15 13:03:27 -0400553 {
Jamie Madill4a3c2342015-10-08 12:58:45 -0400554 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
555 return false;
Geoff Lang5d124a62015-09-15 13:03:27 -0400556 }
Jamie Madill4a3c2342015-10-08 12:58:45 -0400557
558 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
559 &memberInfoOut->offset);
560 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
561 &memberInfoOut->arrayStride);
562 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
563 &memberInfoOut->matrixStride);
564
565 // TODO(jmadill): possibly determine this at the gl::Program level.
566 GLint isRowMajorMatrix = 0;
567 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
568 &isRowMajorMatrix);
569 memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE;
570 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400571}
Jamie Madill4a3c2342015-10-08 12:58:45 -0400572
Sami Väisänen46eaa942016-06-29 10:26:37 +0300573void ProgramGL::setPathFragmentInputGen(const std::string &inputName,
574 GLenum genMode,
575 GLint components,
576 const GLfloat *coeffs)
577{
578 ASSERT(mEnablePathRendering);
579
580 for (const auto &input : mPathRenderingFragmentInputs)
581 {
582 if (input.name == inputName)
583 {
584 mFunctions->programPathFragmentInputGenNV(mProgramID, input.location, genMode,
585 components, coeffs);
586 ASSERT(mFunctions->getError() == GL_NO_ERROR);
587 return;
588 }
589 }
590
591}
592
Geoff Lang65a0be92015-10-02 09:57:30 -0400593void ProgramGL::preLink()
594{
595 // Reset the program state
596 mUniformRealLocationMap.clear();
597 mUniformBlockRealLocationMap.clear();
598 mSamplerBindings.clear();
599 mUniformIndexToSamplerIndex.clear();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300600 mPathRenderingFragmentInputs.clear();
Geoff Lang65a0be92015-10-02 09:57:30 -0400601}
602
603bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog)
604{
605 GLint linkStatus = GL_FALSE;
606 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
607 if (linkStatus == GL_FALSE)
608 {
609 // Linking failed, put the error into the info log
610 GLint infoLogLength = 0;
611 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
612
613 std::string warning;
614
615 // Info log length includes the null terminator, so 1 means that the info log is an empty
616 // string.
617 if (infoLogLength > 1)
618 {
619 std::vector<char> buf(infoLogLength);
620 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
621
622 mFunctions->deleteProgram(mProgramID);
623 mProgramID = 0;
624
625 infoLog << buf.data();
626
627 warning = FormatString("Program link failed unexpectedly: %s", buf.data());
628 }
629 else
630 {
631 warning = "Program link failed unexpectedly with no info log.";
632 }
633 ANGLEPlatformCurrent()->logWarning(warning.c_str());
634 TRACE("\n%s", warning.c_str());
635
636 // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case
637 return false;
638 }
639
640 return true;
641}
642
643void ProgramGL::postLink()
644{
645 // Query the uniform information
646 ASSERT(mUniformRealLocationMap.empty());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400647 const auto &uniformLocations = mState.getUniformLocations();
648 const auto &uniforms = mState.getUniforms();
Geoff Lang65a0be92015-10-02 09:57:30 -0400649 mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX);
650 for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++)
651 {
652 const auto &entry = uniformLocations[uniformLocation];
653 if (!entry.used)
654 {
655 continue;
656 }
657
658 // From the spec:
659 // "Locations for sequential array indices are not required to be sequential."
660 const gl::LinkedUniform &uniform = uniforms[entry.index];
661 std::stringstream fullNameStr;
662 fullNameStr << uniform.name;
663 if (uniform.isArray())
664 {
665 fullNameStr << "[" << entry.element << "]";
666 }
667 const std::string &fullName = fullNameStr.str();
668
669 GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str());
670 mUniformRealLocationMap[uniformLocation] = realLocation;
671 }
672
Jamie Madill48ef11b2016-04-27 15:21:52 -0400673 mUniformIndexToSamplerIndex.resize(mState.getUniforms().size(), GL_INVALID_INDEX);
Geoff Lang65a0be92015-10-02 09:57:30 -0400674
675 for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId)
676 {
677 const gl::LinkedUniform &linkedUniform = uniforms[uniformId];
678
679 if (!linkedUniform.isSampler() || !linkedUniform.staticUse)
680 continue;
681
682 mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size();
683
684 // If uniform is a sampler type, insert it into the mSamplerBindings array
685 SamplerBindingGL samplerBinding;
686 samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type);
687 samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0);
688 mSamplerBindings.push_back(samplerBinding);
689 }
Sami Väisänen46eaa942016-06-29 10:26:37 +0300690
691 // Discover CHROMIUM_path_rendering fragment inputs if enabled.
692 if (!mEnablePathRendering)
693 return;
694
695 GLint numFragmentInputs = 0;
696 mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_ACTIVE_RESOURCES,
697 &numFragmentInputs);
698 if (numFragmentInputs <= 0)
699 return;
700
701 GLint maxNameLength = 0;
702 mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_MAX_NAME_LENGTH,
703 &maxNameLength);
704 ASSERT(maxNameLength);
705
706 for (GLint i = 0; i < numFragmentInputs; ++i)
707 {
708 std::string name;
709 name.resize(maxNameLength);
710
711 GLsizei nameLen = 0;
712 mFunctions->getProgramResourceName(mProgramID, GL_FRAGMENT_INPUT_NV, i, maxNameLength,
713 &nameLen, &name[0]);
714 name.resize(nameLen);
715
716 // Ignore built-ins
717 if (angle::BeginsWith(name, "gl_"))
718 continue;
719
720 const GLenum kQueryProperties[] = {GL_LOCATION, GL_ARRAY_SIZE};
721 GLint queryResults[ArraySize(kQueryProperties)];
722 GLsizei queryLength = 0;
723
Geoff Lang3f6a3982016-07-15 15:20:45 -0400724 mFunctions->getProgramResourceiv(
725 mProgramID, GL_FRAGMENT_INPUT_NV, i, static_cast<GLsizei>(ArraySize(kQueryProperties)),
726 kQueryProperties, static_cast<GLsizei>(ArraySize(queryResults)), &queryLength,
727 queryResults);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300728
Olli Etuahoe3191712016-07-18 16:01:10 +0300729 ASSERT(queryLength == static_cast<GLsizei>(ArraySize(kQueryProperties)));
Sami Väisänen46eaa942016-06-29 10:26:37 +0300730
Geoff Lang3f6a3982016-07-15 15:20:45 -0400731 PathRenderingFragmentInput baseElementInput;
732 baseElementInput.name = name;
733 baseElementInput.location = queryResults[0];
734 mPathRenderingFragmentInputs.push_back(std::move(baseElementInput));
Sami Väisänen46eaa942016-06-29 10:26:37 +0300735
736 // If the input is an array it's denoted by [0] suffix on the variable
737 // name. We'll then create an entry per each array index where index > 0
738 if (angle::EndsWith(name, "[0]"))
739 {
740 // drop the suffix
741 name.resize(name.size() - 3);
742
743 const auto arraySize = queryResults[1];
744 const auto baseLocation = queryResults[0];
745
Geoff Lang3f6a3982016-07-15 15:20:45 -0400746 for (GLint arrayIndex = 1; arrayIndex < arraySize; ++arrayIndex)
Sami Väisänen46eaa942016-06-29 10:26:37 +0300747 {
Geoff Lang3f6a3982016-07-15 15:20:45 -0400748 PathRenderingFragmentInput arrayElementInput;
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400749 arrayElementInput.name = name + "[" + ToString(arrayIndex) + "]";
Geoff Lang3f6a3982016-07-15 15:20:45 -0400750 arrayElementInput.location = baseLocation + arrayIndex;
751 mPathRenderingFragmentInputs.push_back(std::move(arrayElementInput));
Sami Väisänen46eaa942016-06-29 10:26:37 +0300752 }
753 }
754 }
Geoff Lang65a0be92015-10-02 09:57:30 -0400755}
756
Jamie Madill4a3c2342015-10-08 12:58:45 -0400757} // namespace rx