OpenGL: rename simple shader to basic shader.
[blender-staging.git] / source / blender / gpu / intern / gpu_basic_shader.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/gpu/intern/gpu_basic_shader.c
29  *  \ingroup gpu
30  *
31  * GLSL shaders to replace fixed function OpenGL materials and lighting. These
32  * are deprecated in newer OpenGL versions and missing in OpenGL ES 2.0. Also,
33  * two sided lighting is no longer natively supported on NVidia cards which
34  * results in slow software fallback.
35  *
36  * Todo:
37  * - Replace glLight and glMaterial functions entirely with GLSL uniforms, to
38  *   make OpenGL ES 2.0 work.
39  * - Replace glTexCoord and glColor with generic attributes.
40  * - Optimize for case where fewer than 3 or 8 lights are used.
41  * - Optimize for case where specular is not used.
42  * - Optimize for case where no texture matrix is used.
43  */
44
45 #include "GPU_glew.h"
46
47 #include "BLI_math.h"
48 #include "BLI_utildefines.h"
49
50 #include "GPU_extensions.h"
51 #include "GPU_basic_shader.h"
52
53 /* State */
54
55 static const bool USE_GLSL = false;
56
57 static struct {
58         GPUShader *cached_shaders[GPU_SHADER_OPTION_COMBINATIONS];
59         bool failed_shaders[GPU_SHADER_OPTION_COMBINATIONS];
60
61         int bound_options;
62
63         int lights_enabled;
64         int lights_directional;
65 } GPU_MATERIAL_STATE;
66
67 /* Init / exit */
68
69 void GPU_basic_shaders_init(void)
70 {
71         memset(&GPU_MATERIAL_STATE, 0, sizeof(GPU_MATERIAL_STATE));
72 }
73
74 void GPU_basic_shaders_exit(void)
75 {
76         int i;
77         
78         for (i = 0; i < GPU_SHADER_OPTION_COMBINATIONS; i++)
79                 if (GPU_MATERIAL_STATE.cached_shaders[i])
80                         GPU_shader_free(GPU_MATERIAL_STATE.cached_shaders[i]);
81 }
82
83 /* Shader lookup / create */
84
85 static bool solid_compatible_lighting(void)
86 {
87         int enabled = GPU_MATERIAL_STATE.lights_enabled;
88         int directional = GPU_MATERIAL_STATE.lights_directional;
89
90         /* more than 3 lights? */
91         if (enabled >= (1 << 3))
92                 return false;
93
94         /* all directional? */
95         return ((directional & enabled) == enabled);
96 }
97
98 #if 0
99 static int detect_options()
100 {
101         GLint two_sided;
102         int options = 0;
103
104         if (glIsEnabled(GL_TEXTURE_2D))
105                 options |= GPU_SHADER_TEXTURE_2D;
106         if (glIsEnabled(GL_COLOR_MATERIAL))
107                 options |= GPU_SHADER_USE_COLOR;
108
109         if (glIsEnabled(GL_LIGHTING))
110                 options |= GPU_SHADER_LIGHTING;
111
112         glGetIntegerv(GL_LIGHT_MODEL_TWO_SIDE, &two_sided);
113         if (two_sided == GL_TRUE)
114                 options |= GPU_SHADER_TWO_SIDED;
115         
116         return options;
117 }
118 #endif
119
120 static GPUShader *gpu_basic_shader(int options)
121 {
122         /* glsl code */
123         extern char datatoc_gpu_shader_basic_vert_glsl[];
124         extern char datatoc_gpu_shader_basic_frag_glsl[];
125         GPUShader *shader;
126
127         /* detect if we can do faster lighting for solid draw mode */
128         if (options & GPU_SHADER_LIGHTING)
129                 if (solid_compatible_lighting())
130                         options |= GPU_SHADER_SOLID_LIGHTING;
131
132         /* cached shaders */
133         shader = GPU_MATERIAL_STATE.cached_shaders[options];
134
135         if (!shader && !GPU_MATERIAL_STATE.failed_shaders[options]) {
136                 /* create shader if it doesn't exist yet */
137                 char defines[64*GPU_SHADER_OPTIONS_NUM] = "";
138
139                 if (options & GPU_SHADER_USE_COLOR)
140                         strcat(defines, "#define USE_COLOR\n");
141                 if (options & GPU_SHADER_TWO_SIDED)
142                         strcat(defines, "#define USE_TWO_SIDED\n");
143                 if (options & GPU_SHADER_TEXTURE_2D)
144                         strcat(defines, "#define USE_TEXTURE\n");
145
146                 if (options & GPU_SHADER_SOLID_LIGHTING)
147                         strcat(defines, "#define USE_SOLID_LIGHTING\n");
148                 else if (options & GPU_SHADER_LIGHTING)
149                         strcat(defines, "#define USE_SCENE_LIGHTING\n");
150
151                 shader = GPU_shader_create(
152                         datatoc_gpu_shader_basic_vert_glsl,
153                         datatoc_gpu_shader_basic_frag_glsl,
154                         NULL,
155                         NULL,
156                         defines, 0, 0, 0);
157                 
158                 if (shader) {
159                         /* set texture map to first texture unit */
160                         if (options & GPU_SHADER_TEXTURE_2D)
161                                 glUniform1i(GPU_shader_get_uniform(shader, "texture_map"), 0);
162
163                         GPU_MATERIAL_STATE.cached_shaders[options] = shader;
164                 }
165                 else
166                         GPU_MATERIAL_STATE.failed_shaders[options] = true;
167         }
168
169         return shader;
170 }
171
172 /* Bind / unbind */
173
174 void GPU_basic_shader_bind(int options)
175 {
176         if (USE_GLSL) {
177                 if (options) {
178                         GPUShader *shader = gpu_basic_shader(options);
179
180                         if (shader)
181                                 GPU_shader_bind(shader);
182                 }
183                 else {
184                         GPU_shader_unbind();
185                 }
186         }
187         else {
188                 int bound_options = GPU_MATERIAL_STATE.bound_options;
189
190                 if (options & GPU_SHADER_LIGHTING) {
191                         glEnable(GL_LIGHTING);
192
193                         if (options & GPU_SHADER_USE_COLOR)
194                                 glEnable(GL_COLOR_MATERIAL);
195                         else
196                                 glDisable(GL_COLOR_MATERIAL);
197
198                         if (options & GPU_SHADER_TWO_SIDED)
199                                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
200                         else
201                                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
202                 }
203                 else if (bound_options & GPU_SHADER_LIGHTING) {
204                         glDisable(GL_LIGHTING);
205                         glDisable(GL_COLOR_MATERIAL);
206                         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
207                 }
208
209                 if (options & GPU_SHADER_TEXTURE_2D)
210                         glEnable(GL_TEXTURE_2D);
211                 else if (bound_options & GPU_SHADER_TEXTURE_2D)
212                         glDisable(GL_TEXTURE_2D);
213         }
214
215         GPU_MATERIAL_STATE.bound_options = options;
216 }
217
218 int GPU_basic_shader_bound_options(void)
219 {
220         /* ideally this should disappear, anything that uses this is making fragile
221          * assumptions that the basic shader is bound and not another shader */
222         return GPU_MATERIAL_STATE.bound_options;
223 }
224
225 /* Material Colors */
226
227 void GPU_basic_shader_colors(const float diffuse[3], const float specular[3],
228         int shininess, float alpha)
229 {
230         float gl_diffuse[4], gl_specular[4];
231
232         if (diffuse)
233                 copy_v3_v3(gl_diffuse, diffuse);
234         else
235                 zero_v3(gl_diffuse);
236         gl_diffuse[3] = alpha;
237
238         if (specular)
239                 copy_v3_v3(gl_specular, specular);
240         else
241                 zero_v3(gl_specular);
242         gl_specular[3] = 1.0f;
243
244         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gl_diffuse);
245         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, gl_specular);
246         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(shininess, 1, 128));
247 }
248
249 void GPU_basic_shader_light_set(int light_num, GPULightData *light)
250 {
251         int light_bit = (1 << light_num);
252
253         /* note that light position is affected by the current modelview matrix! */
254
255         GPU_MATERIAL_STATE.lights_enabled &= ~light_bit;
256         GPU_MATERIAL_STATE.lights_directional &= ~light_bit;
257
258         if (light) {
259                 float position[4], diffuse[4], specular[4];
260
261                 glEnable(GL_LIGHT0+light_num);
262
263                 /* position */
264                 if (light->type == GPU_LIGHT_SUN) {
265                         copy_v3_v3(position, light->direction);
266                         position[3] = 0.0f;
267                 }
268                 else {
269                         copy_v3_v3(position, light->position);
270                         position[3] = 1.0f;
271                 }
272                 glLightfv(GL_LIGHT0+light_num, GL_POSITION, position);
273
274                 /* energy */
275                 copy_v3_v3(diffuse, light->diffuse);
276                 copy_v3_v3(specular, light->specular);
277                 diffuse[3] = 1.0f;
278                 specular[3] = 1.0f;
279                 glLightfv(GL_LIGHT0+light_num, GL_DIFFUSE, diffuse);
280                 glLightfv(GL_LIGHT0+light_num, GL_SPECULAR, specular);
281
282                 /* attenuation */
283                 if (light->type == GPU_LIGHT_SUN) {
284                         glLightf(GL_LIGHT0+light_num, GL_CONSTANT_ATTENUATION, 1.0f);
285                         glLightf(GL_LIGHT0+light_num, GL_LINEAR_ATTENUATION, 0.0f);
286                         glLightf(GL_LIGHT0+light_num, GL_QUADRATIC_ATTENUATION, 0.0f);
287                 }
288                 else {
289                         glLightf(GL_LIGHT0+light_num, GL_CONSTANT_ATTENUATION, light->constant_attenuation);
290                         glLightf(GL_LIGHT0+light_num, GL_LINEAR_ATTENUATION, light->linear_attenuation);
291                         glLightf(GL_LIGHT0+light_num, GL_QUADRATIC_ATTENUATION, light->quadratic_attenuation);
292                 }
293
294                 /* spot */
295                 glLightfv(GL_LIGHT0+light_num, GL_SPOT_DIRECTION, light->direction);
296                 if (light->type == GPU_LIGHT_SPOT) {
297                         glLightf(GL_LIGHT0+light_num, GL_SPOT_CUTOFF, light->spot_cutoff);
298                         glLightf(GL_LIGHT0+light_num, GL_SPOT_EXPONENT, light->spot_exponent);
299                 }
300                 else {
301                         glLightf(GL_LIGHT0+light_num, GL_SPOT_CUTOFF, 180.0f);
302                         glLightf(GL_LIGHT0+light_num, GL_SPOT_EXPONENT, 0.0f);
303                 }
304
305                 GPU_MATERIAL_STATE.lights_enabled |= light_bit;
306                 if (position[3] == 0.0f)
307                         GPU_MATERIAL_STATE.lights_directional |= light_bit;
308         }
309         else {
310                 if (USE_GLSL) {
311                         /* glsl shader needs these zero to skip them */
312                         const float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
313
314                         glLightfv(GL_LIGHT0+light_num, GL_POSITION, zero);
315                         glLightfv(GL_LIGHT0+light_num, GL_DIFFUSE, zero);
316                         glLightfv(GL_LIGHT0+light_num, GL_SPECULAR, zero);
317                 }
318
319                 glDisable(GL_LIGHT0+light_num);
320         }
321 }
322
323 void GPU_basic_shader_light_set_viewer(bool local)
324 {
325         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (local)? GL_TRUE: GL_FALSE);
326 }
327