1 //
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/WorkerThread.h"
12 #include "common/angleutils.h"
13 #include "common/bitset_utils.h"
14 #include "common/debug.h"
15 #include "common/string_utils.h"
16 #include "common/utilities.h"
17 #include "libANGLE/Context.h"
18 #include "libANGLE/ProgramLinkedResources.h"
19 #include "libANGLE/Uniform.h"
20 #include "libANGLE/queryconversions.h"
21 #include "libANGLE/renderer/gl/ContextGL.h"
22 #include "libANGLE/renderer/gl/FunctionsGL.h"
23 #include "libANGLE/renderer/gl/RendererGL.h"
24 #include "libANGLE/renderer/gl/ShaderGL.h"
25 #include "libANGLE/renderer/gl/StateManagerGL.h"
26 #include "libANGLE/trace.h"
27 #include "platform/PlatformMethods.h"
28 #include "platform/autogen/FeaturesGL_autogen.h"
29
30 namespace rx
31 {
32 namespace
33 {
34
35 // Returns mapped name of a transform feedback varying. The original name may contain array
36 // brackets with an index inside, which will get copied to the mapped name. The varying must be
37 // known to be declared in the shader.
GetTransformFeedbackVaryingMappedName(const gl::SharedCompiledShaderState & shaderState,const std::string & tfVaryingName)38 std::string GetTransformFeedbackVaryingMappedName(const gl::SharedCompiledShaderState &shaderState,
39 const std::string &tfVaryingName)
40 {
41 ASSERT(shaderState->shaderType != gl::ShaderType::Fragment &&
42 shaderState->shaderType != gl::ShaderType::Compute);
43 const auto &varyings = shaderState->outputVaryings;
44 auto bracketPos = tfVaryingName.find("[");
45 if (bracketPos != std::string::npos)
46 {
47 auto tfVaryingBaseName = tfVaryingName.substr(0, bracketPos);
48 for (const auto &varying : varyings)
49 {
50 if (varying.name == tfVaryingBaseName)
51 {
52 std::string mappedNameWithArrayIndex =
53 varying.mappedName + tfVaryingName.substr(bracketPos);
54 return mappedNameWithArrayIndex;
55 }
56 }
57 }
58 else
59 {
60 for (const auto &varying : varyings)
61 {
62 if (varying.name == tfVaryingName)
63 {
64 return varying.mappedName;
65 }
66 else if (varying.isStruct())
67 {
68 GLuint fieldIndex = 0;
69 const auto *field = varying.findField(tfVaryingName, &fieldIndex);
70 if (field == nullptr)
71 {
72 continue;
73 }
74 ASSERT(field != nullptr && !field->isStruct() &&
75 (!field->isArray() || varying.isShaderIOBlock));
76 std::string mappedName;
77 // If it's an I/O block without an instance name, don't include the block name.
78 if (!varying.isShaderIOBlock || !varying.name.empty())
79 {
80 mappedName = varying.isShaderIOBlock ? varying.mappedStructOrBlockName
81 : varying.mappedName;
82 mappedName += '.';
83 }
84 return mappedName + field->mappedName;
85 }
86 }
87 }
88 UNREACHABLE();
89 return std::string();
90 }
91
92 } // anonymous namespace
93
94 class ProgramGL::LinkTaskGL final : public LinkTask
95 {
96 public:
LinkTaskGL(ProgramGL * program,bool hasNativeParallelCompile,const FunctionsGL * functions,const gl::Extensions & extensions,GLuint programID)97 LinkTaskGL(ProgramGL *program,
98 bool hasNativeParallelCompile,
99 const FunctionsGL *functions,
100 const gl::Extensions &extensions,
101 GLuint programID)
102 : mProgram(program),
103 mHasNativeParallelCompile(hasNativeParallelCompile),
104 mFunctions(functions),
105 mExtensions(extensions),
106 mProgramID(programID)
107 {}
108 ~LinkTaskGL() override = default;
109
link(const gl::ProgramLinkedResources & resources,const gl::ProgramMergedVaryings & mergedVaryings,std::vector<std::shared_ptr<LinkSubTask>> * linkSubTasksOut,std::vector<std::shared_ptr<LinkSubTask>> * postLinkSubTasksOut)110 void link(const gl::ProgramLinkedResources &resources,
111 const gl::ProgramMergedVaryings &mergedVaryings,
112 std::vector<std::shared_ptr<LinkSubTask>> *linkSubTasksOut,
113 std::vector<std::shared_ptr<LinkSubTask>> *postLinkSubTasksOut) override
114 {
115 ASSERT(linkSubTasksOut && linkSubTasksOut->empty());
116 ASSERT(postLinkSubTasksOut && postLinkSubTasksOut->empty());
117
118 mResult = mProgram->linkJobImpl(mExtensions);
119
120 // If there is no native parallel compile, do the post-link right away.
121 if (mResult == angle::Result::Continue && !mHasNativeParallelCompile)
122 {
123 mResult = mProgram->postLinkJobImpl(resources);
124 }
125
126 // See comment on mResources
127 mResources = &resources;
128 return;
129 }
130
getResult(const gl::Context * context,gl::InfoLog & infoLog)131 angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
132 {
133 ANGLE_TRACE_EVENT0("gpu.angle", "LinkTaskGL::getResult");
134
135 if (mResult == angle::Result::Continue && mHasNativeParallelCompile)
136 {
137 mResult = mProgram->postLinkJobImpl(*mResources);
138 }
139
140 return mResult;
141 }
142
isLinkingInternally()143 bool isLinkingInternally() override
144 {
145 GLint completionStatus = GL_TRUE;
146 if (mHasNativeParallelCompile)
147 {
148 mFunctions->getProgramiv(mProgramID, GL_COMPLETION_STATUS, &completionStatus);
149 }
150 return completionStatus == GL_FALSE;
151 }
152
153 private:
154 ProgramGL *mProgram;
155 const bool mHasNativeParallelCompile;
156 const FunctionsGL *mFunctions;
157 const gl::Extensions &mExtensions;
158 const GLuint mProgramID;
159
160 angle::Result mResult = angle::Result::Continue;
161
162 // Note: resources are kept alive by the front-end for the entire duration of the link,
163 // including during resolve when getResult() and postLink() are called.
164 const gl::ProgramLinkedResources *mResources = nullptr;
165 };
166
ProgramGL(const gl::ProgramState & data,const FunctionsGL * functions,const angle::FeaturesGL & features,StateManagerGL * stateManager,const std::shared_ptr<RendererGL> & renderer)167 ProgramGL::ProgramGL(const gl::ProgramState &data,
168 const FunctionsGL *functions,
169 const angle::FeaturesGL &features,
170 StateManagerGL *stateManager,
171 const std::shared_ptr<RendererGL> &renderer)
172 : ProgramImpl(data),
173 mFunctions(functions),
174 mFeatures(features),
175 mStateManager(stateManager),
176 mProgramID(0),
177 mRenderer(renderer)
178 {
179 ASSERT(mFunctions);
180 ASSERT(mStateManager);
181
182 mProgramID = mFunctions->createProgram();
183 }
184
185 ProgramGL::~ProgramGL() = default;
186
destroy(const gl::Context * context)187 void ProgramGL::destroy(const gl::Context *context)
188 {
189 mFunctions->deleteProgram(mProgramID);
190 mProgramID = 0;
191 }
192
load(const gl::Context * context,gl::BinaryInputStream * stream,std::shared_ptr<LinkTask> * loadTaskOut,egl::CacheGetResult * resultOut)193 angle::Result ProgramGL::load(const gl::Context *context,
194 gl::BinaryInputStream *stream,
195 std::shared_ptr<LinkTask> *loadTaskOut,
196 egl::CacheGetResult *resultOut)
197 {
198 ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::load");
199 ProgramExecutableGL *executableGL = getExecutable();
200
201 // Read the binary format, size and blob
202 GLenum binaryFormat = stream->readInt<GLenum>();
203 GLint binaryLength = stream->readInt<GLint>();
204 const uint8_t *binary = stream->data() + stream->offset();
205 stream->skip(binaryLength);
206
207 // Load the binary
208 mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength);
209
210 // Verify that the program linked. Ensure failure if program binary is intentionally corrupted,
211 // even if the corruption didn't really cause a failure.
212 if (!checkLinkStatus() ||
213 GetImplAs<ContextGL>(context)->getFeaturesGL().corruptProgramBinaryForTesting.enabled)
214 {
215 return angle::Result::Continue;
216 }
217
218 executableGL->postLink(mFunctions, mStateManager, mFeatures, mProgramID);
219 executableGL->reapplyUBOBindings();
220
221 *loadTaskOut = {};
222 *resultOut = egl::CacheGetResult::Success;
223
224 return angle::Result::Continue;
225 }
226
save(const gl::Context * context,gl::BinaryOutputStream * stream)227 void ProgramGL::save(const gl::Context *context, gl::BinaryOutputStream *stream)
228 {
229 GLint binaryLength = 0;
230 mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
231
232 std::vector<uint8_t> binary(std::max(binaryLength, 1));
233 GLenum binaryFormat = GL_NONE;
234 mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat,
235 binary.data());
236
237 stream->writeInt(binaryFormat);
238 stream->writeInt(binaryLength);
239
240 const angle::FeaturesGL &features = GetImplAs<ContextGL>(context)->getFeaturesGL();
241 if (features.corruptProgramBinaryForTesting.enabled)
242 {
243 // Random corruption of the binary data. Corrupting the first byte has proven to be enough
244 // to later cause the binary load to fail on most platforms.
245 ++binary[0];
246 }
247
248 stream->writeBytes(binary.data(), binaryLength);
249
250 // Re-apply UBO bindings to work around driver bugs.
251 if (features.reapplyUBOBindingsAfterUsingBinaryProgram.enabled)
252 {
253 getExecutable()->reapplyUBOBindings();
254 }
255 }
256
setBinaryRetrievableHint(bool retrievable)257 void ProgramGL::setBinaryRetrievableHint(bool retrievable)
258 {
259 // glProgramParameteri isn't always available on ES backends.
260 if (mFunctions->programParameteri)
261 {
262 mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT,
263 retrievable ? GL_TRUE : GL_FALSE);
264 }
265 }
266
setSeparable(bool separable)267 void ProgramGL::setSeparable(bool separable)
268 {
269 mFunctions->programParameteri(mProgramID, GL_PROGRAM_SEPARABLE, separable ? GL_TRUE : GL_FALSE);
270 }
271
prepareForLink(const gl::ShaderMap<ShaderImpl * > & shaders)272 void ProgramGL::prepareForLink(const gl::ShaderMap<ShaderImpl *> &shaders)
273 {
274 for (gl::ShaderType shaderType : gl::AllShaderTypes())
275 {
276 mAttachedShaders[shaderType] = 0;
277
278 if (shaders[shaderType] != nullptr)
279 {
280 const ShaderGL *shaderGL = GetAs<ShaderGL>(shaders[shaderType]);
281 mAttachedShaders[shaderType] = shaderGL->getShaderID();
282 }
283 }
284 }
285
link(const gl::Context * context,std::shared_ptr<LinkTask> * linkTaskOut)286 angle::Result ProgramGL::link(const gl::Context *context, std::shared_ptr<LinkTask> *linkTaskOut)
287 {
288 ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::link");
289
290 *linkTaskOut = std::make_shared<LinkTaskGL>(this, mRenderer->hasNativeParallelCompile(),
291 mFunctions, context->getExtensions(), mProgramID);
292
293 return angle::Result::Continue;
294 }
295
linkJobImpl(const gl::Extensions & extensions)296 angle::Result ProgramGL::linkJobImpl(const gl::Extensions &extensions)
297 {
298 ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::linkJobImpl");
299 const gl::ProgramExecutable &executable = mState.getExecutable();
300 ProgramExecutableGL *executableGL = getExecutable();
301
302 if (mAttachedShaders[gl::ShaderType::Compute] != 0)
303 {
304 mFunctions->attachShader(mProgramID, mAttachedShaders[gl::ShaderType::Compute]);
305 }
306 else
307 {
308 // Set the transform feedback state
309 std::vector<std::string> transformFeedbackVaryingMappedNames;
310 const gl::ShaderType tfShaderType =
311 executable.hasLinkedShaderStage(gl::ShaderType::Geometry) ? gl::ShaderType::Geometry
312 : gl::ShaderType::Vertex;
313 const gl::SharedCompiledShaderState &tfShaderState = mState.getAttachedShader(tfShaderType);
314 for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames())
315 {
316 std::string tfVaryingMappedName =
317 GetTransformFeedbackVaryingMappedName(tfShaderState, tfVarying);
318 transformFeedbackVaryingMappedNames.push_back(tfVaryingMappedName);
319 }
320
321 if (transformFeedbackVaryingMappedNames.empty())
322 {
323 // Only clear the transform feedback state if transform feedback varyings have already
324 // been set.
325 if (executableGL->mHasAppliedTransformFeedbackVaryings)
326 {
327 ASSERT(mFunctions->transformFeedbackVaryings);
328 mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr,
329 mState.getTransformFeedbackBufferMode());
330 executableGL->mHasAppliedTransformFeedbackVaryings = false;
331 }
332 }
333 else
334 {
335 ASSERT(mFunctions->transformFeedbackVaryings);
336 std::vector<const GLchar *> transformFeedbackVaryings;
337 for (const auto &varying : transformFeedbackVaryingMappedNames)
338 {
339 transformFeedbackVaryings.push_back(varying.c_str());
340 }
341 mFunctions->transformFeedbackVaryings(
342 mProgramID, static_cast<GLsizei>(transformFeedbackVaryingMappedNames.size()),
343 &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode());
344 executableGL->mHasAppliedTransformFeedbackVaryings = true;
345 }
346
347 for (const gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
348 {
349 if (mAttachedShaders[shaderType] != 0)
350 {
351 mFunctions->attachShader(mProgramID, mAttachedShaders[shaderType]);
352 }
353 }
354
355 // Bind attribute locations to match the GL layer.
356 for (const gl::ProgramInput &attribute : executable.getProgramInputs())
357 {
358 if (!attribute.isActive() || attribute.isBuiltIn())
359 {
360 continue;
361 }
362
363 mFunctions->bindAttribLocation(mProgramID, attribute.getLocation(),
364 attribute.mappedName.c_str());
365 }
366
367 // Bind the secondary fragment color outputs defined in EXT_blend_func_extended. We only use
368 // the API to bind fragment output locations in case EXT_blend_func_extended is enabled.
369 // Otherwise shader-assigned locations will work.
370 if (extensions.blendFuncExtendedEXT)
371 {
372 const gl::SharedCompiledShaderState &fragmentShader =
373 mState.getAttachedShader(gl::ShaderType::Fragment);
374 if (fragmentShader && fragmentShader->shaderVersion == 100 &&
375 mFunctions->standard == STANDARD_GL_DESKTOP)
376 {
377 ASSERT(!mFeatures.avoidBindFragDataLocation.enabled);
378
379 const auto &shaderOutputs = fragmentShader->activeOutputVariables;
380 for (const auto &output : shaderOutputs)
381 {
382 // TODO(http://anglebug.com/40644593) This could be cleaner if the transformed
383 // names would be set correctly in ShaderVariable::mappedName. This would
384 // require some refactoring in the translator. Adding a mapped name dictionary
385 // for builtins into the symbol table would be one fairly clean way to do it.
386 if (output.name == "gl_SecondaryFragColorEXT")
387 {
388 mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 0,
389 "webgl_FragColor");
390 mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 1,
391 "webgl_SecondaryFragColor");
392 }
393 else if (output.name == "gl_SecondaryFragDataEXT")
394 {
395 // Basically we should have a loop here going over the output
396 // array binding "webgl_FragData[i]" and "webgl_SecondaryFragData[i]" array
397 // indices to the correct color buffers and color indices.
398 // However I'm not sure if this construct is legal or not, neither ARB or
399 // EXT version of the spec mention this. They only mention that
400 // automatically assigned array locations for ESSL 3.00 output arrays need
401 // to have contiguous locations.
402 //
403 // In practice it seems that binding array members works on some drivers and
404 // fails on others. One option could be to modify the shader translator to
405 // expand the arrays into individual output variables instead of using an
406 // array.
407 //
408 // For now we're going to have a limitation of assuming that
409 // GL_MAX_DUAL_SOURCE_DRAW_BUFFERS is *always* 1 and then only bind the
410 // basename of the variable ignoring any indices. This appears to work
411 // uniformly.
412 ASSERT(output.isArray() && output.getOutermostArraySize() == 1);
413
414 mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 0, "webgl_FragData");
415 mFunctions->bindFragDataLocationIndexed(mProgramID, 0, 1,
416 "webgl_SecondaryFragData");
417 }
418 }
419 }
420 else if (fragmentShader && fragmentShader->shaderVersion >= 300)
421 {
422 // ESSL 3.00 and up.
423 auto assignOutputLocations =
424 [this](const std::vector<gl::VariableLocation> &locations) {
425 const gl::ProgramExecutable &executable = mState.getExecutable();
426 for (size_t outputLocationIndex = 0u;
427 outputLocationIndex < locations.size(); ++outputLocationIndex)
428 {
429 const gl::VariableLocation &outputLocation =
430 locations[outputLocationIndex];
431 if (outputLocation.arrayIndex != 0 || !outputLocation.used() ||
432 outputLocation.ignored)
433 {
434 continue;
435 }
436
437 const gl::ProgramOutput &outputVar =
438 executable.getOutputVariables()[outputLocation.index];
439 if (outputVar.pod.hasShaderAssignedLocation)
440 {
441 continue;
442 }
443
444 // We only need to assign the location and index via the API if the
445 // variable doesn't have a shader-assigned location.
446 ASSERT(outputVar.pod.index != -1);
447
448 // Avoid calling glBindFragDataLocationIndexed unless the application
449 // did it explicitly to avoid Qualcomm driver bugs with multiple render
450 // targets.
451 if (mFeatures.avoidBindFragDataLocation.enabled &&
452 !outputVar.pod.hasApiAssignedLocation)
453 {
454 continue;
455 }
456
457 mFunctions->bindFragDataLocationIndexed(
458 mProgramID, static_cast<int>(outputLocationIndex),
459 outputVar.pod.index, outputVar.mappedName.c_str());
460 }
461 };
462
463 ANGLE_GL_CLEAR_ERRORS(mFunctions);
464
465 assignOutputLocations(executable.getOutputLocations());
466 assignOutputLocations(executable.getSecondaryOutputLocations());
467
468 GLenum error = mFunctions->getError();
469 if (error != GL_NO_ERROR)
470 {
471 executable.getInfoLog()
472 << "Failed to bind frag data locations. See http://anglebug.com/42267082";
473 return angle::Result::Stop;
474 }
475 }
476 }
477 }
478
479 mFunctions->linkProgram(mProgramID);
480 return angle::Result::Continue;
481 }
482
postLinkJobImpl(const gl::ProgramLinkedResources & resources)483 angle::Result ProgramGL::postLinkJobImpl(const gl::ProgramLinkedResources &resources)
484 {
485 ANGLE_TRACE_EVENT0("gpu.angle", "ProgramGL::postLinkJobImpl");
486
487 if (mAttachedShaders[gl::ShaderType::Compute] != 0)
488 {
489 mFunctions->detachShader(mProgramID, mAttachedShaders[gl::ShaderType::Compute]);
490 }
491 else
492 {
493 for (const gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
494 {
495 if (mAttachedShaders[shaderType] != 0)
496 {
497 mFunctions->detachShader(mProgramID, mAttachedShaders[shaderType]);
498 }
499 }
500 }
501
502 // Verify the link
503 if (!checkLinkStatus())
504 {
505 return angle::Result::Stop;
506 }
507
508 if (mFeatures.alwaysCallUseProgramAfterLink.enabled)
509 {
510 mStateManager->forceUseProgram(mProgramID);
511 }
512
513 linkResources(resources);
514 getExecutable()->postLink(mFunctions, mStateManager, mFeatures, mProgramID);
515
516 return angle::Result::Continue;
517 }
518
validate(const gl::Caps &)519 GLboolean ProgramGL::validate(const gl::Caps & /*caps*/)
520 {
521 // TODO(jmadill): implement validate
522 return true;
523 }
524
getUniformBlockSize(const std::string &,const std::string & blockMappedName,size_t * sizeOut) const525 bool ProgramGL::getUniformBlockSize(const std::string & /* blockName */,
526 const std::string &blockMappedName,
527 size_t *sizeOut) const
528 {
529 ASSERT(mProgramID != 0u);
530
531 GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockMappedName.c_str());
532 if (blockIndex == GL_INVALID_INDEX)
533 {
534 *sizeOut = 0;
535 return false;
536 }
537
538 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
getUniformBlockMemberInfo(const std::string &,const std::string & memberUniformMappedName,sh::BlockMemberInfo * memberInfoOut) const545 bool ProgramGL::getUniformBlockMemberInfo(const std::string & /* memberUniformName */,
546 const std::string &memberUniformMappedName,
547 sh::BlockMemberInfo *memberInfoOut) const
548 {
549 GLuint uniformIndex;
550 const GLchar *memberNameGLStr = memberUniformMappedName.c_str();
551 mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
552
553 if (uniformIndex == GL_INVALID_INDEX)
554 {
555 *memberInfoOut = sh::kDefaultBlockMemberInfo;
556 return false;
557 }
558
559 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET,
560 &memberInfoOut->offset);
561 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE,
562 &memberInfoOut->arrayStride);
563 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE,
564 &memberInfoOut->matrixStride);
565
566 // TODO(jmadill): possibly determine this at the gl::Program level.
567 GLint isRowMajorMatrix = 0;
568 mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR,
569 &isRowMajorMatrix);
570 memberInfoOut->isRowMajorMatrix = gl::ConvertToBool(isRowMajorMatrix);
571 return true;
572 }
573
getShaderStorageBlockMemberInfo(const std::string &,const std::string & memberUniformMappedName,sh::BlockMemberInfo * memberInfoOut) const574 bool ProgramGL::getShaderStorageBlockMemberInfo(const std::string & /* memberName */,
575 const std::string &memberUniformMappedName,
576 sh::BlockMemberInfo *memberInfoOut) const
577 {
578 const GLchar *memberNameGLStr = memberUniformMappedName.c_str();
579 GLuint index =
580 mFunctions->getProgramResourceIndex(mProgramID, GL_BUFFER_VARIABLE, memberNameGLStr);
581
582 if (index == GL_INVALID_INDEX)
583 {
584 *memberInfoOut = sh::kDefaultBlockMemberInfo;
585 return false;
586 }
587
588 constexpr int kPropCount = 5;
589 std::array<GLenum, kPropCount> props = {
590 {GL_ARRAY_STRIDE, GL_IS_ROW_MAJOR, GL_MATRIX_STRIDE, GL_OFFSET, GL_TOP_LEVEL_ARRAY_STRIDE}};
591 std::array<GLint, kPropCount> params;
592 GLsizei length;
593 mFunctions->getProgramResourceiv(mProgramID, GL_BUFFER_VARIABLE, index, kPropCount,
594 props.data(), kPropCount, &length, params.data());
595 ASSERT(kPropCount == length);
596 memberInfoOut->arrayStride = params[0];
597 memberInfoOut->isRowMajorMatrix = params[1] != 0;
598 memberInfoOut->matrixStride = params[2];
599 memberInfoOut->offset = params[3];
600 memberInfoOut->topLevelArrayStride = params[4];
601
602 return true;
603 }
604
getShaderStorageBlockSize(const std::string & name,const std::string & mappedName,size_t * sizeOut) const605 bool ProgramGL::getShaderStorageBlockSize(const std::string &name,
606 const std::string &mappedName,
607 size_t *sizeOut) const
608 {
609 const GLchar *nameGLStr = mappedName.c_str();
610 GLuint index =
611 mFunctions->getProgramResourceIndex(mProgramID, GL_SHADER_STORAGE_BLOCK, nameGLStr);
612
613 if (index == GL_INVALID_INDEX)
614 {
615 *sizeOut = 0;
616 return false;
617 }
618
619 GLenum prop = GL_BUFFER_DATA_SIZE;
620 GLsizei length = 0;
621 GLint dataSize = 0;
622 mFunctions->getProgramResourceiv(mProgramID, GL_SHADER_STORAGE_BLOCK, index, 1, &prop, 1,
623 &length, &dataSize);
624 *sizeOut = static_cast<size_t>(dataSize);
625 return true;
626 }
627
getAtomicCounterBufferSizeMap(std::map<int,unsigned int> * sizeMapOut) const628 void ProgramGL::getAtomicCounterBufferSizeMap(std::map<int, unsigned int> *sizeMapOut) const
629 {
630 if (mFunctions->getProgramInterfaceiv == nullptr)
631 {
632 return;
633 }
634
635 int resourceCount = 0;
636 mFunctions->getProgramInterfaceiv(mProgramID, GL_ATOMIC_COUNTER_BUFFER, GL_ACTIVE_RESOURCES,
637 &resourceCount);
638
639 for (int index = 0; index < resourceCount; index++)
640 {
641 constexpr int kPropCount = 2;
642 std::array<GLenum, kPropCount> props = {{GL_BUFFER_BINDING, GL_BUFFER_DATA_SIZE}};
643 std::array<GLint, kPropCount> params;
644 GLsizei length;
645 mFunctions->getProgramResourceiv(mProgramID, GL_ATOMIC_COUNTER_BUFFER, index, kPropCount,
646 props.data(), kPropCount, &length, params.data());
647 ASSERT(kPropCount == length);
648 int bufferBinding = params[0];
649 unsigned int bufferDataSize = params[1];
650 sizeMapOut->insert(std::pair<int, unsigned int>(bufferBinding, bufferDataSize));
651 }
652 }
653
checkLinkStatus()654 bool ProgramGL::checkLinkStatus()
655 {
656 GLint linkStatus = GL_FALSE;
657 mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus);
658 if (linkStatus == GL_FALSE)
659 {
660 // Linking or program binary loading failed, put the error into the info log.
661 GLint infoLogLength = 0;
662 mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
663
664 // Info log length includes the null terminator, so 1 means that the info log is an empty
665 // string.
666 if (infoLogLength > 1)
667 {
668 std::vector<char> buf(infoLogLength);
669 mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]);
670
671 mState.getExecutable().getInfoLog() << buf.data();
672
673 WARN() << "Program link or binary loading failed: " << buf.data();
674 }
675 else
676 {
677 WARN() << "Program link or binary loading failed with no info log.";
678 }
679
680 // This may happen under normal circumstances if we're loading program binaries and the
681 // driver or hardware has changed.
682 ASSERT(mProgramID != 0);
683 return false;
684 }
685
686 return true;
687 }
688
markUnusedUniformLocations(std::vector<gl::VariableLocation> * uniformLocations,std::vector<gl::SamplerBinding> * samplerBindings,std::vector<gl::ImageBinding> * imageBindings)689 void ProgramGL::markUnusedUniformLocations(std::vector<gl::VariableLocation> *uniformLocations,
690 std::vector<gl::SamplerBinding> *samplerBindings,
691 std::vector<gl::ImageBinding> *imageBindings)
692 {
693 const gl::ProgramExecutable &executable = mState.getExecutable();
694 const ProgramExecutableGL *executableGL = getExecutable();
695
696 GLint maxLocation = static_cast<GLint>(uniformLocations->size());
697 for (GLint location = 0; location < maxLocation; ++location)
698 {
699 if (executableGL->mUniformRealLocationMap[location] == -1)
700 {
701 auto &locationRef = (*uniformLocations)[location];
702 if (executable.isSamplerUniformIndex(locationRef.index))
703 {
704 GLuint samplerIndex = executable.getSamplerIndexFromUniformIndex(locationRef.index);
705 gl::SamplerBinding &samplerBinding = (*samplerBindings)[samplerIndex];
706 if (locationRef.arrayIndex <
707 static_cast<unsigned int>(samplerBinding.textureUnitsCount))
708 {
709 // Crop unused sampler bindings in the sampler array.
710 SetBitField(samplerBinding.textureUnitsCount, locationRef.arrayIndex);
711 }
712 }
713 else if (executable.isImageUniformIndex(locationRef.index))
714 {
715 GLuint imageIndex = executable.getImageIndexFromUniformIndex(locationRef.index);
716 gl::ImageBinding &imageBinding = (*imageBindings)[imageIndex];
717 if (locationRef.arrayIndex < imageBinding.boundImageUnits.size())
718 {
719 // Crop unused image bindings in the image array.
720 imageBinding.boundImageUnits.resize(locationRef.arrayIndex);
721 }
722 }
723 // If the location has been previously bound by a glBindUniformLocation call, it should
724 // be marked as ignored. Otherwise it's unused.
725 if (mState.getUniformLocationBindings().getBindingByLocation(location) != -1)
726 {
727 locationRef.markIgnored();
728 }
729 else
730 {
731 locationRef.markUnused();
732 }
733 }
734 }
735 }
736
linkResources(const gl::ProgramLinkedResources & resources)737 void ProgramGL::linkResources(const gl::ProgramLinkedResources &resources)
738 {
739 // Gather interface block info.
740 auto getUniformBlockSize = [this](const std::string &name, const std::string &mappedName,
741 size_t *sizeOut) {
742 return this->getUniformBlockSize(name, mappedName, sizeOut);
743 };
744
745 auto getUniformBlockMemberInfo = [this](const std::string &name, const std::string &mappedName,
746 sh::BlockMemberInfo *infoOut) {
747 return this->getUniformBlockMemberInfo(name, mappedName, infoOut);
748 };
749
750 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
751
752 auto getShaderStorageBlockSize = [this](const std::string &name, const std::string &mappedName,
753 size_t *sizeOut) {
754 return this->getShaderStorageBlockSize(name, mappedName, sizeOut);
755 };
756
757 auto getShaderStorageBlockMemberInfo = [this](const std::string &name,
758 const std::string &mappedName,
759 sh::BlockMemberInfo *infoOut) {
760 return this->getShaderStorageBlockMemberInfo(name, mappedName, infoOut);
761 };
762 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
763 getShaderStorageBlockMemberInfo);
764
765 // Gather atomic counter buffer info.
766 std::map<int, unsigned int> sizeMap;
767 getAtomicCounterBufferSizeMap(&sizeMap);
768 resources.atomicCounterBufferLinker.link(sizeMap);
769
770 const gl::SharedCompiledShaderState &fragmentShader =
771 mState.getAttachedShader(gl::ShaderType::Fragment);
772 if (fragmentShader != nullptr)
773 {
774 resources.pixelLocalStorageLinker.link(fragmentShader->pixelLocalStorageFormats);
775 }
776 }
777
onUniformBlockBinding(gl::UniformBlockIndex uniformBlockIndex)778 void ProgramGL::onUniformBlockBinding(gl::UniformBlockIndex uniformBlockIndex)
779 {
780 getExecutable()->mDirtyUniformBlockBindings.set(uniformBlockIndex.value);
781 }
782 } // namespace rx
783