2 * Copyright (c) 2023 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 "gles-graphics-shader.h"
22 #include <dali/integration-api/debug.h>
23 #include "egl-graphics-controller.h"
25 namespace Dali::Graphics::GLES
32 std::vector<char> source{};
36 Shader::Shader(const Graphics::ShaderCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
37 : ShaderResource(createInfo, controller)
39 // push shader to the create queue
40 mImpl = std::make_unique<Impl>();
42 // Make a copy of source code
43 mImpl->source.resize(createInfo.sourceSize);
44 std::copy(reinterpret_cast<const char*>(mCreateInfo.sourceData),
45 reinterpret_cast<const char*>(mCreateInfo.sourceData) + mCreateInfo.sourceSize,
46 mImpl->source.begin());
49 mCreateInfo.sourceData = mImpl->source.data();
52 Shader::~Shader() = default;
54 bool Shader::Compile() const
56 auto gl = GetController().GetGL();
65 GLenum pipelineStage{0u};
66 switch(GetCreateInfo().pipelineStage)
68 case Graphics::PipelineStage::TOP_OF_PIPELINE:
72 case Graphics::PipelineStage::VERTEX_SHADER:
74 pipelineStage = GL_VERTEX_SHADER;
77 case Graphics::PipelineStage::GEOMETRY_SHADER:
81 case Graphics::PipelineStage::FRAGMENT_SHADER:
83 pipelineStage = GL_FRAGMENT_SHADER;
86 case Graphics::PipelineStage::COMPUTE_SHADER:
90 case Graphics::PipelineStage::TESSELATION_CONTROL:
94 case Graphics::PipelineStage::TESSELATION_EVALUATION:
98 case Graphics::PipelineStage::BOTTOM_OF_PIPELINE:
106 auto shader = gl->CreateShader(pipelineStage);
107 const auto src = reinterpret_cast<const char*>(GetCreateInfo().sourceData);
108 GLint size = GetCreateInfo().sourceSize;
109 gl->ShaderSource(shader, 1, const_cast<const char**>(&src), &size);
110 gl->CompileShader(shader);
113 gl->GetShaderiv(shader, GL_COMPILE_STATUS, &status);
114 if(status != GL_TRUE)
118 gl->GetShaderInfoLog(shader, 4096, &size, output);
119 DALI_LOG_RENDER_INFO("Code: %s\n", reinterpret_cast<const char*>(GetCreateInfo().sourceData));
120 DALI_LOG_RENDER_INFO("Log: %s\n", output);
121 gl->DeleteShader(shader);
126 mImpl->glShader = shader;
133 uint32_t Shader::GetGLShader() const
135 return mImpl->glShader;
138 void Shader::DestroyResource()
142 auto gl = GetController().GetGL();
147 gl->DeleteShader(mImpl->glShader);
151 void Shader::DiscardResource()
153 GetController().DiscardResource(this);
156 } // namespace Dali::Graphics::GLES