Initial Version of LayerManagementService added.
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Platform / X11GLESRenderer / src / ShaderProgramGLES.cpp
1 /***************************************************************************
2 *
3 * Copyright 2010 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *               http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19
20 #include "ShaderProgramGLES.h"
21 #include <GLES2/gl2ext.h>
22 #include <RenderUtil.h>
23 #include <Log.h>
24
25 static const char defaultVertShaderBinary[] = {
26 #include "renderer_vert.cghex"
27 };
28 static const char defaultFragShaderBinary[] = {
29 #include "renderer_frag.cghex"
30 };
31
32 ShaderProgram* ShaderProgramGLES::createProgram(const std::string& vertName, const std::string& fragName)
33 {
34         GLuint progHandle;
35         ShaderProgramGLES* program = 0;
36
37         if (vertName=="default" && fragName=="default")
38         {
39                 // load default shader from binary data
40                 progHandle = RenderUtilLoadShaderSources("renderer_vert.glslv", "renderer_frag.glslf", GL_TRUE);
41         }
42         else
43         {
44                 // load shader sources from file, compile and link them:
45                 progHandle = RenderUtilLoadShaderSources(vertName.c_str(), fragName.c_str(), GL_FALSE);
46         }
47
48         if (progHandle!=0)
49         {
50                 // bind attrib locations for vertex positions and texture coordinates
51                 glBindAttribLocation(progHandle, 0, "aPosition");
52                 glBindAttribLocation(progHandle, 1, "aTexCoords");
53
54                 // re-link the program as we have changed the attrib bindings
55                 glLinkProgram(progHandle);
56
57                 program = new ShaderProgramGLES(vertName, fragName, progHandle);
58         }
59         else
60         {
61                 LOG_ERROR("ShaderProgramGLES", "Failed to create and link shader program");
62         }
63
64         return program;
65 }
66
67 ShaderProgramGLES::ShaderProgramGLES(const std::string& vertName, const std::string& fragName, GLuint handle)
68         : ShaderProgram(vertName, fragName)
69         , _progHandle(handle)
70 {
71         // Update the uniform locations.
72         // Don't move this call to the base class constructor as we need
73         // to set the OpenGL program handle first.
74         updateCommonUniformLocations();
75 }
76
77 ShaderProgramGLES::~ShaderProgramGLES(void)
78 {
79         if (_progHandle)
80         {
81                 glDeleteProgram(_progHandle);
82         }
83 }
84
85 void ShaderProgramGLES::use(void) const
86 {
87         glUseProgram(_progHandle);
88 }
89