2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/render/shaders/program.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/gl-defines.h>
28 #include <dali/internal/common/shader-data.h>
29 #include <dali/internal/render/common/performance-monitor.h>
30 #include <dali/internal/render/gl-resources/gl-call-debug.h>
31 #include <dali/internal/render/shaders/program-cache.h>
32 #include <dali/public-api/common/constants.h>
33 #include <dali/public-api/common/dali-common.h>
34 #include <dali/public-api/common/dali-vector.h>
35 #include <dali/graphics-api/graphics-program.h>
36 #include <dali/graphics-api/graphics-controller.h>
39 void LogWithLineNumbers(const char* source)
41 uint32_t lineNumber = 0u;
42 const char* prev = source;
43 const char* ptr = prev;
51 // seek the next end of line or end of text
52 while(*ptr != '\n' && *ptr != '\0')
57 std::string line(prev, ptr - prev);
58 Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%4d %s\n", lineNumber, line.c_str());
78 const char* const gStdAttribs[Program::ATTRIB_TYPE_LAST] =
80 "aPosition", // ATTRIB_POSITION
81 "aTexCoord", // ATTRIB_TEXCOORD
84 const char* const gStdUniforms[Program::UNIFORM_TYPE_LAST] =
86 "uMvpMatrix", // UNIFORM_MVP_MATRIX
87 "uModelView", // UNIFORM_MODELVIEW_MATRIX
88 "uProjection", // UNIFORM_PROJECTION_MATRIX
89 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
90 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
91 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
92 "uColor", // UNIFORM_COLOR
93 "sTexture", // UNIFORM_SAMPLER
94 "sTextureRect", // UNIFORM_SAMPLER_RECT
95 "sEffect", // UNIFORM_EFFECT_SAMPLER
96 "uSize" // UNIFORM_SIZE
103 Program* Program::New(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& gfxController, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
105 uint32_t programId{0u};
107 // Get program id and use it as hash for the cache
108 // in order to maintain current functionality as long as needed
109 gfxController.GetProgramParameter( *gfxProgram, 1, &programId );
111 size_t shaderHash = programId;
113 Program* program = cache.GetProgram(shaderHash);
115 if(nullptr == program)
117 // program not found so create it
118 program = new Program(cache, shaderData, gfxController, std::move(gfxProgram), modifiesGeometry);
120 cache.AddProgram(shaderHash, program);
130 if(this != mCache.GetCurrentProgram())
132 LOG_GL("UseProgram(%d)\n", mProgramId);
133 CHECK_GL(mGlAbstraction, mGlAbstraction.UseProgram(mProgramId));
135 mCache.SetCurrentProgram(this);
140 bool Program::IsUsed()
142 return (this == mCache.GetCurrentProgram());
145 GLint Program::GetAttribLocation(AttribType type)
147 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
149 return GetCustomAttributeLocation(type);
152 uint32_t Program::RegisterCustomAttribute(ConstString name)
155 // find the value from cache
156 for(; index < static_cast<uint32_t>(mAttributeLocations.size()); ++index)
158 if(mAttributeLocations[index].first == name)
160 // name found so return index
164 // if we get here, index is one past end so push back the new name
165 mAttributeLocations.push_back(std::make_pair(name, ATTRIB_UNKNOWN));
169 GLint Program::GetCustomAttributeLocation(uint32_t attributeIndex)
171 // debug check that index is within name cache
172 DALI_ASSERT_DEBUG(mAttributeLocations.size() > attributeIndex);
174 // check if we have already queried the location of the attribute
175 GLint location = mAttributeLocations[attributeIndex].second;
177 if(location == ATTRIB_UNKNOWN)
179 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetAttribLocation(mProgramId, mAttributeLocations[attributeIndex].first.GetCString()));
181 mAttributeLocations[attributeIndex].second = location;
182 LOG_GL("GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[attributeIndex].first.GetCString(), mAttributeLocations[attributeIndex].second);
188 uint32_t Program::RegisterUniform(ConstString name)
191 // find the value from cache
192 for(; index < static_cast<uint32_t>(mUniformLocations.size()); ++index)
194 if(mUniformLocations[index].first == name)
196 // name found so return index
200 // if we get here, index is one past end so push back the new name
201 mUniformLocations.push_back(std::make_pair(name, UNIFORM_NOT_QUERIED));
205 GLint Program::GetUniformLocation(uint32_t uniformIndex)
207 // debug check that index is within name cache
208 DALI_ASSERT_DEBUG(mUniformLocations.size() > uniformIndex);
210 // check if we have already queried the location of the uniform
211 GLint location = mUniformLocations[uniformIndex].second;
213 if(location == UNIFORM_NOT_QUERIED)
215 location = CHECK_GL(mGlAbstraction, mGlAbstraction.GetUniformLocation(mProgramId, mUniformLocations[uniformIndex].first.GetCString()));
217 mUniformLocations[uniformIndex].second = location;
218 LOG_GL("GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[uniformIndex].first.GetCString(), mUniformLocations[uniformIndex].second);
227 * This struct is used to record the position of a uniform declaration
228 * within the fragment shader source code.
230 struct LocationPosition
232 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
233 int32_t position; ///< the position of the uniform declaration
234 LocationPosition(GLint uniformLocation, int32_t position)
235 : uniformLocation(uniformLocation),
241 bool sortByPosition(LocationPosition a, LocationPosition b)
243 return a.position < b.position;
246 const char* const DELIMITERS = " \t\n";
250 const char* const mString;
251 const uint32_t mLength;
253 template<uint32_t kLength>
254 constexpr StringSize(const char (&string)[kLength])
256 mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
260 operator const char*() const
266 bool operator==(const StringSize& lhs, const char* rhs)
268 return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
271 constexpr StringSize UNIFORM{"uniform"};
272 constexpr StringSize SAMPLER_PREFIX{"sampler"};
273 constexpr StringSize SAMPLER_TYPES[] = {
278 constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
282 void Program::GetActiveSamplerUniforms()
284 GLint numberOfActiveUniforms = -1;
285 GLint uniformMaxNameLength = -1;
287 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms);
288 mGlAbstraction.GetProgramiv(mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength);
290 std::vector<std::string> samplerNames;
291 std::vector<char> name(uniformMaxNameLength + 1); // Allow for null terminator
292 std::vector<LocationPosition> samplerUniformLocations;
297 GLenum type = GL_ZERO;
299 for(int i = 0; i < numberOfActiveUniforms; ++i)
301 mGlAbstraction.GetActiveUniform(mProgramId, static_cast<GLuint>(i), uniformMaxNameLength, &nameLength, &number, &type, name.data());
303 if(type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES)
305 GLuint location = mGlAbstraction.GetUniformLocation(mProgramId, name.data());
306 samplerNames.push_back(name.data());
307 samplerUniformLocations.push_back(LocationPosition(location, -1));
312 //Determine declaration order of each sampler
313 char* fragShader = strdup(mProgramData->GetFragmentShader());
314 char* uniform = strstr(fragShader, UNIFORM);
315 int samplerPosition = 0;
318 char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
320 char* nextPtr = nullptr;
321 char* token = strtok_r(outerToken, DELIMITERS, &nextPtr);
324 if(SAMPLER_PREFIX == token)
326 token += SAMPLER_PREFIX.mLength;
327 if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
330 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
331 for(uint32_t i = 0; i < static_cast<uint32_t>(samplerUniformLocations.size()); ++i)
333 if(samplerUniformLocations[i].position == -1 &&
334 strncmp(token, samplerNames[i].c_str(), samplerNames[i].size()) == 0)
336 samplerUniformLocations[i].position = samplerPosition++;
344 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token);
350 token = strtok_r(nullptr, DELIMITERS, &nextPtr);
353 uniform = strstr(uniform, UNIFORM);
358 // Re-order according to declaration order in the fragment source.
359 uint32_t samplerUniformCount = static_cast<uint32_t>(samplerUniformLocations.size());
360 if(samplerUniformCount > 1)
362 std::sort(samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
365 mSamplerUniformLocations.resize(samplerUniformCount);
366 for(uint32_t i = 0; i < samplerUniformCount; ++i)
368 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
372 bool Program::GetSamplerUniformLocation(uint32_t index, GLint& location)
375 if(index < mSamplerUniformLocations.size())
377 location = mSamplerUniformLocations[index];
383 uint32_t Program::GetActiveSamplerCount() const
385 return static_cast<uint32_t>(mSamplerUniformLocations.size());
388 void Program::SetUniform1i(GLint location, GLint value0)
390 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
392 if(UNIFORM_UNKNOWN == location)
394 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
395 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
396 // specified uniform variable will not be changed.following opengl silently do nothing
400 // check if uniform location fits the cache
401 if(location >= MAX_UNIFORM_CACHE_SIZE)
403 // not cached, make the gl call
404 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
405 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
409 // check if the value is different from what's already been set
410 if(value0 != mUniformCacheInt[location])
413 LOG_GL("Uniform1i(%d,%d)\n", location, value0);
414 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1i(location, value0));
416 mUniformCacheInt[location] = value0;
421 void Program::SetUniform4i(GLint location, GLint value0, GLint value1, GLint value2, GLint value3)
423 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
425 if(UNIFORM_UNKNOWN == location)
427 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
428 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
429 // specified uniform variable will not be changed.following opengl silently do nothing
433 // Not caching these as based on current analysis this is not called that often by our shaders
434 LOG_GL("Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3);
435 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4i(location, value0, value1, value2, value3));
438 void Program::SetUniform1f(GLint location, GLfloat value0)
440 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
442 if(UNIFORM_UNKNOWN == location)
444 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
445 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
446 // specified uniform variable will not be changed.following opengl silently do nothing
450 // check if uniform location fits the cache
451 if(location >= MAX_UNIFORM_CACHE_SIZE)
453 // not cached, make the gl call
454 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
455 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
459 // check if the same value has already been set, reset if it is different
460 if((fabsf(value0 - mUniformCacheFloat[location]) >= Math::MACHINE_EPSILON_1))
463 LOG_GL("Uniform1f(%d,%f)\n", location, value0);
464 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform1f(location, value0));
467 mUniformCacheFloat[location] = value0;
472 void Program::SetUniform2f(GLint location, GLfloat value0, GLfloat value1)
474 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
476 if(UNIFORM_UNKNOWN == location)
478 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
479 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
480 // specified uniform variable will not be changed.following opengl silently do nothing
484 // check if uniform location fits the cache
485 if(location >= MAX_UNIFORM_CACHE_SIZE)
487 // not cached, make the gl call
488 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
489 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
493 // check if the same value has already been set, reset if it is different
494 if((fabsf(value0 - mUniformCacheFloat2[location][0]) >= Math::MACHINE_EPSILON_1) ||
495 (fabsf(value1 - mUniformCacheFloat2[location][1]) >= Math::MACHINE_EPSILON_1))
498 LOG_GL("Uniform2f(%d,%f,%f)\n", location, value0, value1);
499 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform2f(location, value0, value1));
502 mUniformCacheFloat2[location][0] = value0;
503 mUniformCacheFloat2[location][1] = value1;
508 void Program::SetSizeUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
510 if((fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1) ||
511 (fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1) ||
512 (fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1))
514 mSizeUniformCache.x = value0;
515 mSizeUniformCache.y = value1;
516 mSizeUniformCache.z = value2;
517 SetUniform3f(location, value0, value1, value2);
521 void Program::SetUniform3f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2)
523 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
525 if(UNIFORM_UNKNOWN == location)
527 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
528 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
529 // specified uniform variable will not be changed.following opengl silently do nothing
533 // Not caching these as based on current analysis this is not called that often by our shaders
534 LOG_GL("Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2);
535 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform3f(location, value0, value1, value2));
538 void Program::SetUniform4f(GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3)
540 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
542 if(UNIFORM_UNKNOWN == location)
544 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
545 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
546 // specified uniform variable will not be changed.following opengl silently do nothing
550 // check if uniform location fits the cache
551 if(location >= MAX_UNIFORM_CACHE_SIZE)
553 // not cached, make the gl call
554 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
555 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
559 // check if the same value has already been set, reset if any component is different
560 // checking index 3 first because we're often animating alpha (rgba)
561 if((fabsf(value3 - mUniformCacheFloat4[location][3]) >= Math::MACHINE_EPSILON_1) ||
562 (fabsf(value0 - mUniformCacheFloat4[location][0]) >= Math::MACHINE_EPSILON_1) ||
563 (fabsf(value1 - mUniformCacheFloat4[location][1]) >= Math::MACHINE_EPSILON_1) ||
564 (fabsf(value2 - mUniformCacheFloat4[location][2]) >= Math::MACHINE_EPSILON_1))
567 LOG_GL("Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3);
568 CHECK_GL(mGlAbstraction, mGlAbstraction.Uniform4f(location, value0, value1, value2, value3));
570 mUniformCacheFloat4[location][0] = value0;
571 mUniformCacheFloat4[location][1] = value1;
572 mUniformCacheFloat4[location][2] = value2;
573 mUniformCacheFloat4[location][3] = value3;
578 void Program::SetUniformMatrix4fv(GLint location, GLsizei count, const GLfloat* value)
580 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
582 if(UNIFORM_UNKNOWN == location)
584 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
585 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
586 // specified uniform variable will not be changed.following opengl silently do nothing
590 // Not caching these calls. Based on current analysis this is called very often
591 // but with different values (we're using this for MVP matrices)
592 // NOTE! we never want driver or GPU to transpose
593 LOG_GL("UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
594 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix4fv(location, count, GL_FALSE, value));
597 void Program::SetUniformMatrix3fv(GLint location, GLsizei count, const GLfloat* value)
599 DALI_ASSERT_DEBUG(IsUsed()); // should not call this if this program is not used
601 if(UNIFORM_UNKNOWN == location)
603 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
604 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
605 // specified uniform variable will not be changed.following opengl silently do nothing
609 // Not caching these calls. Based on current analysis this is called very often
610 // but with different values (we're using this for MVP matrices)
611 // NOTE! we never want driver or GPU to transpose
612 LOG_GL("UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value);
613 CHECK_GL(mGlAbstraction, mGlAbstraction.UniformMatrix3fv(location, count, GL_FALSE, value));
616 void Program::GlContextCreated()
620 void Program::GlContextDestroyed()
624 mFragmentShaderId = 0;
627 ResetAttribsUniformCache();
630 bool Program::ModifiesGeometry()
632 return mModifiesGeometry;
635 Program::Program(ProgramCache& cache, Internal::ShaderDataPtr shaderData, Graphics::Controller& controller, Graphics::UniquePtr<Graphics::Program>&& gfxProgram, bool modifiesGeometry)
637 mGlAbstraction(mCache.GetGlAbstraction()),
638 mProjectionMatrix(nullptr),
639 mViewMatrix(nullptr),
642 mFragmentShaderId(0),
644 mGfxProgram( std::move(gfxProgram)),
645 mGfxController( controller ),
646 mProgramData(shaderData),
647 mModifiesGeometry(modifiesGeometry)
649 // reserve space for standard attributes
650 mAttributeLocations.reserve(ATTRIB_TYPE_LAST);
651 for(uint32_t i = 0; i < ATTRIB_TYPE_LAST; ++i)
653 RegisterCustomAttribute(ConstString(gStdAttribs[i]));
656 // reserve space for standard uniforms
657 mUniformLocations.reserve(UNIFORM_TYPE_LAST);
658 // reset built in uniform names in cache
659 for(uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i)
661 RegisterUniform(ConstString(gStdUniforms[i]));
665 ResetAttribsUniformCache();
667 // Get program id and use it as hash for the cache
668 // in order to maintain current functionality as long as needed
669 mGfxController.GetProgramParameter( *mGfxProgram, 1, &mProgramId );
680 // Temporary, exist if program id is already set
683 GetActiveSamplerUniforms();
687 DALI_ASSERT_ALWAYS(nullptr != mProgramData.Get() && "Program data is not initialized");
688 DALI_ASSERT_DEBUG(mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it.");
690 LOG_GL("CreateProgram()\n");
691 mProgramId = CHECK_GL(mGlAbstraction, mGlAbstraction.CreateProgram());
693 GLint linked = GL_FALSE;
695 const bool binariesSupported = mCache.IsBinarySupported();
697 // if shader binaries are supported and ShaderData contains compiled bytecode?
698 if(binariesSupported && mProgramData->HasBinary())
700 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
702 CHECK_GL(mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), static_cast<GLsizei>(mProgramData->GetBufferSize()))); // truncated
704 CHECK_GL(mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId));
707 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_VALIDATE_STATUS, &success));
709 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
711 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_LINK_STATUS, &linked));
713 if(GL_FALSE == linked)
715 DALI_LOG_ERROR("Failed to load program binary \n");
718 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &nLength));
721 Dali::Vector<char> szLog;
722 szLog.Reserve(nLength); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
723 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramInfoLog(mProgramId, nLength, &nLength, szLog.Begin()));
724 DALI_LOG_ERROR("Program Link Error: %s\n", szLog.Begin());
730 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Reused binary.\n");
734 // Fall back to compiling and linking the vertex and fragment sources
735 if(GL_FALSE == linked)
737 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
738 if(CompileShader(GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader()))
740 if(CompileShader(GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader()))
744 if(binariesSupported && mLinked)
746 GLint binaryLength = 0;
748 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader());
750 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength));
751 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
754 // Allocate space for the bytecode in ShaderData
755 mProgramData->AllocateBuffer(binaryLength);
756 // Copy the bytecode to ShaderData
757 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, nullptr, &binaryFormat, mProgramData->GetBufferData()));
758 mCache.StoreBinary(mProgramData);
759 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Saved binary.\n");
766 GetActiveSamplerUniforms();
772 void Program::Unload()
774 // Bypass when using gfx program
775 if(!mProgramData && mProgramId)
781 if(this == mCache.GetCurrentProgram())
783 CHECK_GL(mGlAbstraction, mGlAbstraction.UseProgram(0));
785 mCache.SetCurrentProgram(nullptr);
790 LOG_GL("DeleteProgram(%d)\n", mProgramId);
791 CHECK_GL(mGlAbstraction, mGlAbstraction.DeleteProgram(mProgramId));
798 bool Program::CompileShader(GLenum shaderType, GLuint& shaderId, const char* src)
800 // Bypass when using gfx program
801 if(!mProgramData && mProgramId)
807 LOG_GL("CreateShader(%d)\n", shaderType);
808 shaderId = CHECK_GL(mGlAbstraction, mGlAbstraction.CreateShader(shaderType));
809 LOG_GL("AttachShader(%d,%d)\n", mProgramId, shaderId);
810 CHECK_GL(mGlAbstraction, mGlAbstraction.AttachShader(mProgramId, shaderId));
813 LOG_GL("ShaderSource(%d)\n", shaderId);
814 CHECK_GL(mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, nullptr));
816 LOG_GL("CompileShader(%d)\n", shaderId);
817 CHECK_GL(mGlAbstraction, mGlAbstraction.CompileShader(shaderId));
820 LOG_GL("GetShaderiv(%d)\n", shaderId);
821 CHECK_GL(mGlAbstraction, mGlAbstraction.GetShaderiv(shaderId, GL_COMPILE_STATUS, &compiled));
823 if(compiled == GL_FALSE)
825 DALI_LOG_ERROR("Failed to compile shader\n");
826 LogWithLineNumbers(src);
829 mGlAbstraction.GetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &nLength);
832 Dali::Vector<char> szLog;
833 szLog.Reserve(nLength); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
834 mGlAbstraction.GetShaderInfoLog(shaderId, nLength, &nLength, szLog.Begin());
835 DALI_LOG_ERROR("Shader Compiler Error: %s\n", szLog.Begin());
838 DALI_ASSERT_ALWAYS(0 && "Shader compilation failure");
841 return compiled != 0;
846 // Bypass when using gfx program
847 if(!mProgramData && mProgramId)
851 LOG_GL("LinkProgram(%d)\n", mProgramId);
852 CHECK_GL(mGlAbstraction, mGlAbstraction.LinkProgram(mProgramId));
855 LOG_GL("GetProgramiv(%d)\n", mProgramId);
856 CHECK_GL(mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_LINK_STATUS, &linked));
858 if(linked == GL_FALSE)
860 DALI_LOG_ERROR("Shader failed to link \n");
863 mGlAbstraction.GetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &nLength);
866 Dali::Vector<char> szLog;
867 szLog.Reserve(nLength); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
868 mGlAbstraction.GetProgramInfoLog(mProgramId, nLength, &nLength, szLog.Begin());
869 DALI_LOG_ERROR("Shader Link Error: %s\n", szLog.Begin());
872 DALI_ASSERT_ALWAYS(0 && "Shader linking failure");
875 mLinked = linked != GL_FALSE;
878 void Program::FreeShaders()
880 // Bypass when using gfx program
881 if(!mProgramData && mProgramId)
887 LOG_GL("DeleteShader(%d)\n", mVertexShaderId);
888 CHECK_GL(mGlAbstraction, mGlAbstraction.DetachShader(mProgramId, mVertexShaderId));
889 CHECK_GL(mGlAbstraction, mGlAbstraction.DeleteShader(mVertexShaderId));
893 if(mFragmentShaderId)
895 LOG_GL("DeleteShader(%d)\n", mFragmentShaderId);
896 CHECK_GL(mGlAbstraction, mGlAbstraction.DetachShader(mProgramId, mFragmentShaderId));
897 CHECK_GL(mGlAbstraction, mGlAbstraction.DeleteShader(mFragmentShaderId));
898 mFragmentShaderId = 0;
902 void Program::ResetAttribsUniformCache()
904 // reset attribute locations
905 for(uint32_t i = 0; i < mAttributeLocations.size(); ++i)
907 mAttributeLocations[i].second = ATTRIB_UNKNOWN;
910 // reset all gl uniform locations
911 for(uint32_t i = 0; i < mUniformLocations.size(); ++i)
913 // reset gl program locations and names
914 mUniformLocations[i].second = UNIFORM_NOT_QUERIED;
917 mSamplerUniformLocations.clear();
919 // reset uniform caches
920 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
922 for(uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i)
924 // GL initializes uniforms to 0
925 mUniformCacheInt[i] = 0;
926 mUniformCacheFloat[i] = 0.0f;
927 mUniformCacheFloat2[i][0] = 0.0f;
928 mUniformCacheFloat2[i][1] = 0.0f;
929 mUniformCacheFloat4[i][0] = 0.0f;
930 mUniformCacheFloat4[i][1] = 0.0f;
931 mUniformCacheFloat4[i][2] = 0.0f;
932 mUniformCacheFloat4[i][3] = 0.0f;
936 } // namespace Internal