Merge branch 'devel/master' into devel/graphics
[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 bool Shader::Compile() const
52 {
53   auto& gl = *GetController().GetGL();
54   if(!mImpl->glShader)
55   {
56     GLenum pipelineStage{0u};
57     switch(GetCreateInfo().pipelineStage)
58     {
59       case Graphics::PipelineStage::TOP_OF_PIPELINE:
60       {
61         break;
62       }
63       case Graphics::PipelineStage::VERTEX_SHADER:
64       {
65         pipelineStage = GL_VERTEX_SHADER;
66         break;
67       }
68       case Graphics::PipelineStage::GEOMETRY_SHADER:
69       {
70         break;
71       }
72       case Graphics::PipelineStage::FRAGMENT_SHADER:
73       {
74         pipelineStage = GL_FRAGMENT_SHADER;
75         break;
76       }
77       case Graphics::PipelineStage::COMPUTE_SHADER:
78       {
79         break;
80       }
81       case Graphics::PipelineStage::TESSELATION_CONTROL:
82       {
83         break;
84       }
85       case Graphics::PipelineStage::TESSELATION_EVALUATION:
86       {
87         break;
88       }
89       case Graphics::PipelineStage::BOTTOM_OF_PIPELINE:
90       {
91         break;
92       }
93     }
94
95     if(pipelineStage)
96     {
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);
102
103       GLint status{0};
104       gl.GetShaderiv(shader, GL_COMPILE_STATUS, &status);
105       if(status != GL_TRUE)
106       {
107         char    output[4096];
108         GLsizei size{0u};
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);
113         return false;
114       }
115
116       // TODO: check error
117       mImpl->glShader = shader;
118     }
119     return true;
120   }
121   return true;
122 }
123
124 uint32_t Shader::GetGLShader() const
125 {
126   return mImpl->glShader;
127 }
128
129 } // namespace Dali::Graphics::GLES