OpenSubdiv: Commit of OpenSubdiv integration into Blender
[blender.git] / source / blender / gpu / intern / gpu_simple_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_simple_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_simple_shader.h"
52
53 /* State */
54
55 // #define NUM_OPENGL_LIGHTS 8
56
57 static struct {
58         GPUShader *cached_shaders[GPU_SHADER_OPTION_COMBINATIONS];
59         bool failed_shaders[GPU_SHADER_OPTION_COMBINATIONS];
60
61         bool need_normals;
62
63         int lights_enabled;
64         int lights_directional;
65 } GPU_MATERIAL_STATE;
66
67 /* Init / exit */
68
69 void GPU_simple_shaders_init(void)
70 {
71         memset(&GPU_MATERIAL_STATE, 0, sizeof(GPU_MATERIAL_STATE));
72 }
73
74 void GPU_simple_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_OVERRIDE_DIFFUSE;
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_simple_shader(int options)
121 {
122         /* glsl code */
123         extern char datatoc_gpu_shader_simple_vert_glsl[];
124         extern char datatoc_gpu_shader_simple_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_OVERRIDE_DIFFUSE)
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_simple_vert_glsl,
153                         datatoc_gpu_shader_simple_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_simple_shader_bind(int options)
175 {
176         if (GPU_glsl_support()) {
177                 GPUShader *shader = gpu_simple_shader(options);
178
179                 if (shader)
180                         GPU_shader_bind(shader);
181         }
182         else {
183                 // XXX where does this fit, depends on ortho/persp?
184
185                 if (options & GPU_SHADER_LIGHTING)
186                         glEnable(GL_LIGHTING);
187
188                 if (options & GPU_SHADER_TWO_SIDED)
189                         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
190
191                 if (options & GPU_SHADER_OVERRIDE_DIFFUSE) {
192                         glEnable(GL_COLOR_MATERIAL);
193                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
194                 }
195
196                 if (options & GPU_SHADER_TEXTURE_2D)
197                         glEnable(GL_TEXTURE_2D);
198         }
199
200         /* temporary hack, should be solved outside of this file */
201         GPU_MATERIAL_STATE.need_normals = (options & GPU_SHADER_LIGHTING);
202 }
203
204 void GPU_simple_shader_unbind(void)
205 {
206         if (GPU_glsl_support()) {
207                 GPU_shader_unbind();
208         }
209         else {
210                 glDisable(GL_LIGHTING);
211                 glDisable(GL_COLOR_MATERIAL);
212                 glDisable(GL_TEXTURE_2D);
213                 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
214         }
215 }
216
217 /* Material Colors */
218
219 void GPU_simple_shader_colors(const float diffuse[3], const float specular[3],
220         int shininess, float alpha)
221 {
222         float gl_diffuse[4], gl_specular[4];
223
224         copy_v3_v3(gl_diffuse, diffuse);
225         gl_diffuse[3] = alpha;
226
227         copy_v3_v3(gl_specular, specular);
228         gl_specular[3] = 1.0f;
229
230         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gl_diffuse);
231         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, gl_specular);
232         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(shininess, 1, 128));
233 }
234
235 bool GPU_simple_shader_need_normals(void)
236 {
237         return GPU_MATERIAL_STATE.need_normals;
238 }
239
240 void GPU_simple_shader_light_set(int light_num, GPULightData *light)
241 {
242         int light_bit = (1 << light_num);
243
244         GPU_MATERIAL_STATE.lights_enabled &= ~light_bit;
245         GPU_MATERIAL_STATE.lights_directional &= ~light_bit;
246
247         if (light) {
248                 glEnable(GL_LIGHT0+light_num);
249
250                 glLightfv(GL_LIGHT0+light_num, GL_POSITION, light->position); 
251                 glLightfv(GL_LIGHT0+light_num, GL_DIFFUSE, light->diffuse);
252                 glLightfv(GL_LIGHT0+light_num, GL_SPECULAR, light->specular);
253
254                 glLightf(GL_LIGHT0+light_num, GL_CONSTANT_ATTENUATION, light->constant_attenuation);
255                 glLightf(GL_LIGHT0+light_num, GL_LINEAR_ATTENUATION, light->linear_attenuation);
256                 glLightf(GL_LIGHT0+light_num, GL_QUADRATIC_ATTENUATION, light->quadratic_attenuation);
257
258                 glLightfv(GL_LIGHT0+light_num, GL_SPOT_DIRECTION, light->spot_direction);
259                 glLightf(GL_LIGHT0+light_num, GL_SPOT_CUTOFF, light->spot_cutoff);
260                 glLightf(GL_LIGHT0+light_num, GL_SPOT_EXPONENT, light->spot_exponent);
261
262                 GPU_MATERIAL_STATE.lights_enabled |= light_bit;
263                 if (light->position[3] == 0.0f)
264                         GPU_MATERIAL_STATE.lights_directional |= light_bit;
265         }
266         else {
267                 const float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
268
269                 glLightfv(GL_LIGHT0+light_num, GL_POSITION, zero); 
270                 glLightfv(GL_LIGHT0+light_num, GL_DIFFUSE, zero); 
271                 glLightfv(GL_LIGHT0+light_num, GL_SPECULAR, zero);
272
273                 glDisable(GL_LIGHT0+light_num);
274         }
275 }
276
277 void GPU_simple_shader_light_set_viewer(bool local)
278 {
279         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (local)? GL_TRUE: GL_FALSE);
280 }
281