1 /*
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Solarus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SOLARUS_DEFAULTSHADERS_H
18 #define SOLARUS_DEFAULTSHADERS_H
19 
20 #include <string>
21 
22 namespace Solarus {
23 
24 namespace DefaultShaders {
25 
26 /**
27  * \brief Gets the default vertex shader compatibility source header.
28  * This should be included at the beginning of every vertex shader program
29  * source to ensure compatibility across different GLSL versions.
30  */
get_default_vertex_compat_header()31 inline const std::string& get_default_vertex_compat_header() {
32 
33   static const std::string source =
34 R"(
35 #if __VERSION__ >= 130
36 #define COMPAT_VARYING out
37 #define COMPAT_ATTRIBUTE in
38 #define COMPAT_TEXTURE texture
39 #else
40 #define COMPAT_VARYING varying
41 #define COMPAT_ATTRIBUTE attribute
42 #define COMPAT_TEXTURE texture2D
43 #endif
44 
45 #ifdef GL_ES
46 #define COMPAT_PRECISION mediump
47 #else
48 #define COMPAT_PRECISION
49 #endif
50 )";
51   return source;
52 }
53 
54 /**
55  * \brief Gets the Solarus default vertex shader program source.
56  */
get_default_vertex_source()57 inline const std::string& get_default_vertex_source() {
58 
59   static const std::string source = get_default_vertex_compat_header() +
60 R"(
61 uniform mat4 sol_mvp_matrix;
62 uniform mat3 sol_uv_matrix;
63 COMPAT_ATTRIBUTE vec2 sol_vertex;
64 COMPAT_ATTRIBUTE vec2 sol_tex_coord;
65 COMPAT_ATTRIBUTE vec4 sol_color;
66 COMPAT_VARYING vec2 sol_vtex_coord;
67 COMPAT_VARYING vec4 sol_vcolor;
68 
69 void main() {
70     gl_Position = sol_mvp_matrix * vec4(sol_vertex, 0.0, 1.0);
71     sol_vcolor = sol_color;
72     sol_vtex_coord = (sol_uv_matrix * vec3(sol_tex_coord, 1.0)).xy;
73 }
74 )";
75   return source;
76 }
77 
78 /**
79  * \brief Gets the default fragment shader compatibility source header.
80  * This should be included at the beginning of every fragment shader program
81  * source to ensure compatibility across different GLSL versions.
82  */
get_default_fragment_compat_header()83 inline const std::string& get_default_fragment_compat_header() {
84 
85   static const std::string source =
86 R"(
87 #ifdef GL_ES
88 #ifdef GL_FRAGMENT_PRECISION_HIGH
89 precision highp float;
90 #else
91 precision mediump float;
92 #endif
93 #define COMPAT_PRECISION mediump
94 #else
95 #define COMPAT_PRECISION
96 #endif
97 
98 #if __VERSION__ >= 130
99 #define COMPAT_VARYING in
100 #define COMPAT_TEXTURE texture
101 out COMPAT_PRECISION vec4 FragColor;
102 #else
103 #define COMPAT_VARYING varying
104 #define FragColor gl_FragColor
105 #define COMPAT_TEXTURE texture2D
106 #endif
107 )";
108   return source;
109 }
110 
111 /**
112  * \brief Gets the Solarus default fragment shader program source.
113  */
get_default_fragment_source()114 inline const std::string& get_default_fragment_source() {
115 
116 static const std::string source = get_default_fragment_compat_header() +
117 R"(
118 uniform sampler2D sol_texture;
119 uniform bool sol_vcolor_only;
120 uniform bool sol_alpha_mult;
121 COMPAT_VARYING vec2 sol_vtex_coord;
122 COMPAT_VARYING vec4 sol_vcolor;
123 
124 void main() {
125     if (!sol_vcolor_only) {
126         vec4 tex_color = COMPAT_TEXTURE(sol_texture, sol_vtex_coord);
127         FragColor = tex_color * sol_vcolor;
128         if (sol_alpha_mult) {
129             FragColor.rgb *= sol_vcolor.a; //Premultiply by opacity too
130         }
131     } else {
132         FragColor = sol_vcolor;
133     }
134 }
135 )";
136   return source;
137 }
138 
139 }  // namespace DefaultShaders
140 
141 }  // namespace Solarus
142 
143 #endif
144