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.
18 #include "gles-graphics-program.h"
21 #include "egl-graphics-controller.h"
22 #include "gles-graphics-reflection.h"
23 #include "gles-graphics-shader.h"
26 #include <dali/integration-api/gl-abstraction.h>
27 #include <dali/integration-api/gl-defines.h>
29 namespace Dali::Graphics::GLES
31 struct ProgramImpl::Impl
33 explicit Impl(EglGraphicsController& _controller, const ProgramCreateInfo& info)
34 : controller(_controller)
39 createInfo.shaderState = new std::vector<ShaderState>(*info.shaderState);
45 delete createInfo.shaderState;
48 EglGraphicsController& controller;
49 ProgramCreateInfo createInfo;
51 uint32_t refCount{0u};
53 std::unique_ptr<GLES::Reflection> reflection{nullptr};
56 ProgramImpl::ProgramImpl(const Graphics::ProgramCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
58 // Create implementation
59 mImpl = std::make_unique<Impl>(controller, createInfo);
62 mImpl->reflection = std::make_unique<GLES::Reflection>(*this, controller);
65 ProgramImpl::~ProgramImpl() = default;
67 bool ProgramImpl::Destroy()
71 auto& gl = *mImpl->controller.GetGL();
72 gl.DeleteProgram(mImpl->glProgram);
78 bool ProgramImpl::Create()
80 // Create and link new program
81 auto& gl = *mImpl->controller.GetGL();
82 auto program = gl.CreateProgram();
84 const auto& info = mImpl->createInfo;
85 for(const auto& state : *info.shaderState)
87 const auto* shader = static_cast<const GLES::Shader*>(state.shader);
89 // Compile shader first (ignored when compiled)
92 gl.AttachShader(program, shader->GetGLShader());
94 gl.LinkProgram(program);
97 gl.GetProgramiv(program, GL_LINK_STATUS, &status);
102 gl.GetProgramInfoLog(program, 4096, &size, output);
105 // TODO: un-printf-it
106 printf("Log: %s\n", output);
107 gl.DeleteProgram(program);
111 mImpl->glProgram = program;
113 // Initialize reflection
114 mImpl->reflection->BuildUniformReflection();
115 mImpl->reflection->BuildVertexAttributeReflection();
120 uint32_t ProgramImpl::GetGlProgram() const
122 return mImpl->glProgram;
125 uint32_t ProgramImpl::Retain()
127 return ++mImpl->refCount;
130 uint32_t ProgramImpl::Release()
132 return --mImpl->refCount;
135 const GLES::Reflection& ProgramImpl::GetReflection() const
137 return *mImpl->reflection;
140 bool ProgramImpl::GetParameter(uint32_t parameterId, void* out)
142 if(parameterId == 1) // a magic number to access program id
144 *reinterpret_cast<decltype(&mImpl->glProgram)>(out) = mImpl->glProgram;
150 EglGraphicsController& ProgramImpl::GetController() const
152 return mImpl->controller;
155 const ProgramCreateInfo& ProgramImpl::GetCreateInfo() const
157 return mImpl->createInfo;
162 // Destroy GL resources of implementation. This should happen
163 // only if there's no more pipelines using this program so
164 // it is safe to do it in the destructor
165 if(!mProgram->Release())
171 const GLES::Reflection& Program::GetReflection() const
173 return mProgram->GetReflection();
176 EglGraphicsController& Program::GetController() const
178 return GetImplementation()->GetController();
181 const ProgramCreateInfo& Program::GetCreateInfo() const
183 return GetImplementation()->GetCreateInfo();
186 void Program::DiscardResource()
188 GetController().DiscardResource(this);
191 }; // namespace Dali::Graphics::GLES