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 Shader::~Shader() = default;
53 bool Shader::Compile() const
55 auto& gl = *GetController().GetGL();
58 GLenum pipelineStage{0u};
59 switch(GetCreateInfo().pipelineStage)
61 case Graphics::PipelineStage::TOP_OF_PIPELINE:
65 case Graphics::PipelineStage::VERTEX_SHADER:
67 pipelineStage = GL_VERTEX_SHADER;
70 case Graphics::PipelineStage::GEOMETRY_SHADER:
74 case Graphics::PipelineStage::FRAGMENT_SHADER:
76 pipelineStage = GL_FRAGMENT_SHADER;
79 case Graphics::PipelineStage::COMPUTE_SHADER:
83 case Graphics::PipelineStage::TESSELATION_CONTROL:
87 case Graphics::PipelineStage::TESSELATION_EVALUATION:
91 case Graphics::PipelineStage::BOTTOM_OF_PIPELINE:
99 auto shader = gl.CreateShader(pipelineStage);
100 const auto src = reinterpret_cast<const char*>(GetCreateInfo().sourceData);
101 GLint size = GetCreateInfo().sourceSize;
102 gl.ShaderSource(shader, 1, const_cast<const char**>(&src), &size);
103 gl.CompileShader(shader);
106 gl.GetShaderiv(shader, GL_COMPILE_STATUS, &status);
107 if(status != GL_TRUE)
111 gl.GetShaderInfoLog(shader, 4096, &size, output);
112 printf("Code: %s\n", reinterpret_cast<const char*>(GetCreateInfo().sourceData));
113 printf("Log: %s\n", output);
114 gl.DeleteShader(shader);
119 mImpl->glShader = shader;
126 uint32_t Shader::GetGLShader() const
128 return mImpl->glShader;
131 void Shader::DestroyResource()
135 auto gl = GetController().GetGL();
140 gl->DeleteShader(mImpl->glShader);
144 void Shader::DiscardResource()
146 GetController().DiscardResource(this);
149 } // namespace Dali::Graphics::GLES