Merge "CanvasRenderer: Fix changed update when buffer committed" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-shader.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "gles-graphics-shader.h"
20
21 // INTERNAL INCLUDES
22 #include "egl-graphics-controller.h"
23
24 namespace Dali::Graphics::GLES
25 {
26 struct Shader::Impl
27 {
28   Impl()  = default;
29   ~Impl() = default;
30
31   std::vector<char> source{};
32   uint32_t          glShader{};
33 };
34
35 Shader::Shader(const Graphics::ShaderCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
36 : ShaderResource(createInfo, controller)
37 {
38   // push shader to the create queue
39   mImpl = std::make_unique<Impl>();
40
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());
46
47   // Substitute pointer
48   mCreateInfo.sourceData = mImpl->source.data();
49 }
50
51 Shader::~Shader() = default;
52
53 bool Shader::Compile() const
54 {
55   auto& gl = *GetController().GetGL();
56   if(!mImpl->glShader)
57   {
58     GLenum pipelineStage{0u};
59     switch(GetCreateInfo().pipelineStage)
60     {
61       case Graphics::PipelineStage::TOP_OF_PIPELINE:
62       {
63         break;
64       }
65       case Graphics::PipelineStage::VERTEX_SHADER:
66       {
67         pipelineStage = GL_VERTEX_SHADER;
68         break;
69       }
70       case Graphics::PipelineStage::GEOMETRY_SHADER:
71       {
72         break;
73       }
74       case Graphics::PipelineStage::FRAGMENT_SHADER:
75       {
76         pipelineStage = GL_FRAGMENT_SHADER;
77         break;
78       }
79       case Graphics::PipelineStage::COMPUTE_SHADER:
80       {
81         break;
82       }
83       case Graphics::PipelineStage::TESSELATION_CONTROL:
84       {
85         break;
86       }
87       case Graphics::PipelineStage::TESSELATION_EVALUATION:
88       {
89         break;
90       }
91       case Graphics::PipelineStage::BOTTOM_OF_PIPELINE:
92       {
93         break;
94       }
95     }
96
97     if(pipelineStage)
98     {
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);
104
105       GLint status{0};
106       gl.GetShaderiv(shader, GL_COMPILE_STATUS, &status);
107       if(status != GL_TRUE)
108       {
109         char    output[4096];
110         GLsizei size{0u};
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);
115         return false;
116       }
117
118       // TODO: check error
119       mImpl->glShader = shader;
120     }
121     return true;
122   }
123   return true;
124 }
125
126 uint32_t Shader::GetGLShader() const
127 {
128   return mImpl->glShader;
129 }
130
131 void Shader::DestroyResource()
132 {
133   if(mImpl->glShader)
134   {
135     auto gl = GetController().GetGL();
136     if(!gl)
137     {
138       return;
139     }
140     gl->DeleteShader(mImpl->glShader);
141   }
142 }
143
144 void Shader::DiscardResource()
145 {
146   GetController().DiscardResource(this);
147 }
148
149 } // namespace Dali::Graphics::GLES