Health: added thread dead-lock detection
[profile/ivi/layer-management.git] / LayerManagerBase / include / Shader.h
1 /***************************************************************************
2 *
3 * Copyright 2010,2011 BMW Car IT GmbH
4 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *        http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ****************************************************************************/
20 #ifndef _SHADER_H_
21 #define _SHADER_H_
22
23 #include <string>
24 using std::string;
25
26 #include "UniformMap.h"
27 #include "ShaderUniform.h"
28 #include "ShaderProgram.h"
29
30 /**
31  * Represents a shader instance.
32  *
33  * It stores a set of uniform parameters and refers to an OpenGL program object.
34  * Some uniform variables are pre-defined, like surface position and size,
35  * opacity, etc... Additionally, there may be user-defined uniforms.
36  */
37 class Shader
38 {
39 public:
40     /**
41      * Creates a new shader instance by vertex and fragment shader name.
42      * @param vertFileName   File name of vertex shader.
43      * @param fragFileName   File name of fragment shader.
44      * @return new Shader instance, NULL if shader could not be loaded, compiled or linked.
45      */
46     static Shader* createShader(const string& vertFileName, const string& fragFileName);
47
48     /**
49      * Destructor
50      */
51     ~Shader();
52
53     /**
54      * @return Unique ID of this instance
55      */
56     int getId(void) const
57     {
58         return m_uniqueShaderId;
59     }
60
61     /**
62      * Start using this shader for rendering.
63      */
64     void use(void) const
65     {
66         m_program.use();
67     }
68
69     /**
70      * Set a uniform.
71      */
72     void setUniform(const ShaderUniform& uniform);
73
74     /**
75      * Update uniform values.
76      * Please note that this method doesn't update the standard uniforms
77      * used for position, size, etc... They need to be set separately
78      * by loadCommonUniforms().
79      */
80     void loadUniforms(void);
81
82     /**
83      * Load uniform values for common surface properties, like position,
84      * size, opacity, etc...
85      *
86      * @param uniforms   Uniform values
87      * @param texCount   texture count
88      */
89     void loadCommonUniforms(const ShaderProgram::CommonUniforms& uniforms, const int texCount) const
90     {
91         m_program.loadCommonUniforms(uniforms, texCount);
92     }
93
94 private:
95     /**
96      * Private constructor.
97      * New instances of this class are supposed to be created by createShader(...).
98      *
99      * @param program  Program object to be used
100      */
101     Shader(ShaderProgram& program);
102
103 private:
104     /// unique shader ID
105     const unsigned int m_uniqueShaderId;
106
107     /// reference to OpenGL program object used by this shader instance
108     ShaderProgram& m_program;
109
110     /// a map of user-defined uniforms
111     UniformMap m_uniformMap;
112
113     /// next unique ID
114     static unsigned int m_nextUniqueShaderId;
115 };
116
117 #endif /* _SHADER_H_ */
118