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 "gles-graphics-shader.h"
22 #include "egl-graphics-controller.h"
24 namespace Dali::Graphics::GLES
31 std::vector<char> source{};
35 Shader::Shader(const Graphics::ShaderCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
36 : ShaderResource(createInfo, controller)
38 // push shader to the create queue
39 mImpl = std::make_unique<Impl>();
41 // Make a copy of source code
42 mImpl->source.resize(createInfo.sourceSize);
43 std::copy(reinterpret_cast<const char*>(mCreateInfo.sourceData),
44 reinterpret_cast<const char*>(mCreateInfo.sourceData) + mCreateInfo.sourceSize,
45 mImpl->source.begin());
48 mCreateInfo.sourceData = mImpl->source.data();
51 bool Shader::Compile() const
53 auto& gl = *GetController().GetGL();
56 GLenum pipelineStage{0u};
57 switch(GetCreateInfo().pipelineStage)
59 case Graphics::PipelineStage::TOP_OF_PIPELINE:
63 case Graphics::PipelineStage::VERTEX_SHADER:
65 pipelineStage = GL_VERTEX_SHADER;
68 case Graphics::PipelineStage::GEOMETRY_SHADER:
72 case Graphics::PipelineStage::FRAGMENT_SHADER:
74 pipelineStage = GL_FRAGMENT_SHADER;
77 case Graphics::PipelineStage::COMPUTE_SHADER:
81 case Graphics::PipelineStage::TESSELATION_CONTROL:
85 case Graphics::PipelineStage::TESSELATION_EVALUATION:
89 case Graphics::PipelineStage::BOTTOM_OF_PIPELINE:
97 auto shader = gl.CreateShader(pipelineStage);
98 const auto src = reinterpret_cast<const char*>(GetCreateInfo().sourceData);
99 GLint size = GetCreateInfo().sourceSize;
100 gl.ShaderSource(shader, 1, const_cast<const char**>(&src), &size);
101 gl.CompileShader(shader);
104 gl.GetShaderiv(shader, GL_COMPILE_STATUS, &status);
105 if(status != GL_TRUE)
109 gl.GetShaderInfoLog(shader, 4096, &size, output);
110 printf("Code: %s\n", reinterpret_cast<const char*>(GetCreateInfo().sourceData));
111 printf("Log: %s\n", output);
112 gl.DeleteShader(shader);
117 mImpl->glShader = shader;
124 uint32_t Shader::GetGLShader() const
126 return mImpl->glShader;
129 } // namespace Dali::Graphics::GLES