Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / intern / opensubdiv / shader / gpu_shader_opensubdiv_fragment.glsl
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) 2014 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 struct VertexData {
27         vec4 position;
28         vec3 normal;
29         vec2 uv;
30 };
31
32 #define MAX_LIGHTS 8
33 #define NUM_SOLID_LIGHTS 3
34
35 struct LightSource {
36         vec4 position;
37         vec4 ambient;
38         vec4 diffuse;
39         vec4 specular;
40         vec4 spotDirection;
41 #ifdef SUPPORT_COLOR_MATERIAL
42         float constantAttenuation;
43         float linearAttenuation;
44         float quadraticAttenuation;
45         float spotCutoff;
46         float spotExponent;
47         float spotCosCutoff;
48         float pad, pad2;
49 #endif
50 };
51
52 layout(std140) uniform Lighting {
53         LightSource lightSource[MAX_LIGHTS];
54         int num_enabled_lights;
55 };
56
57 uniform vec4 diffuse;
58 uniform vec4 specular;
59 uniform float shininess;
60
61 uniform sampler2D texture_buffer;
62
63 in block {
64         VertexData v;
65 } inpt;
66
67 void main()
68 {
69 #ifdef WIREFRAME
70         gl_FragColor = diffuse;
71 #else
72         vec3 N = inpt.v.normal;
73
74         if (!gl_FrontFacing)
75                 N = -N;
76
77         /* Compute diffuse and specular lighting. */
78         vec3 L_diffuse = vec3(0.0);
79         vec3 L_specular = vec3(0.0);
80
81 #ifdef USE_LIGHTING
82 #ifndef USE_COLOR_MATERIAL
83         /* Assume NUM_SOLID_LIGHTS directional lights. */
84         for (int i = 0; i < NUM_SOLID_LIGHTS; i++) {
85                 vec4 Plight = lightSource[i].position;
86 #ifdef USE_DIRECTIONAL_LIGHT
87                 vec3 l = (Plight.w == 0.0)
88                             ? normalize(Plight.xyz)
89                             : normalize(inpt.v.position.xyz);
90 #else  /* USE_DIRECTIONAL_LIGHT */
91                 /* TODO(sergey): We can normalize it outside of the shader. */
92                 vec3 l = normalize(Plight.xyz);
93 #endif  /* USE_DIRECTIONAL_LIGHT */
94                 vec3 h = normalize(l + vec3(0, 0, 1));
95                 float d = max(0.0, dot(N, l));
96                 float s = pow(max(0.0, dot(N, h)), shininess);
97                 L_diffuse += d * lightSource[i].diffuse.rgb;
98                 L_specular += s * lightSource[i].specular.rgb;
99         }
100 #else  /* USE_COLOR_MATERIAL */
101         vec3 varying_position = inpt.v.position.xyz;
102         vec3 V = (gl_ProjectionMatrix[3][3] == 0.0) ?
103                  normalize(varying_position) : vec3(0.0, 0.0, -1.0);
104         for (int i = 0; i < num_enabled_lights; i++) {
105                 /* todo: this is a slow check for disabled lights */
106                 if (lightSource[i].specular.a == 0.0)
107                         continue;
108
109                 float intensity = 1.0;
110                 vec3 light_direction;
111
112                 if (lightSource[i].position.w == 0.0) {
113                         /* directional light */
114                         light_direction = lightSource[i].position.xyz;
115                 }
116                 else {
117                         /* point light */
118                         vec3 d = lightSource[i].position.xyz - varying_position;
119                         light_direction = normalize(d);
120
121                         /* spot light cone */
122                         if (lightSource[i].spotCutoff < 90.0) {
123                                 float cosine = max(dot(light_direction,
124                                                        -lightSource[i].spotDirection.xyz),
125                                                    0.0);
126                                 intensity = pow(cosine, lightSource[i].spotExponent);
127                                 intensity *= step(lightSource[i].spotCosCutoff, cosine);
128                         }
129
130                         /* falloff */
131                         float distance = length(d);
132
133                         intensity /= lightSource[i].constantAttenuation +
134                                 lightSource[i].linearAttenuation * distance +
135                                 lightSource[i].quadraticAttenuation * distance * distance;
136                 }
137
138                 /* diffuse light */
139                 vec3 light_diffuse = lightSource[i].diffuse.rgb;
140                 float diffuse_bsdf = max(dot(N, light_direction), 0.0);
141                 L_diffuse += light_diffuse * diffuse_bsdf * intensity;
142
143                 /* specular light */
144                 vec3 light_specular = lightSource[i].specular.rgb;
145                 vec3 H = normalize(light_direction - V);
146
147                 float specular_bsdf = pow(max(dot(N, H), 0.0),
148                                           gl_FrontMaterial.shininess);
149                 L_specular += light_specular * specular_bsdf * intensity;
150         }
151 #endif  /* USE_COLOR_MATERIAL */
152 #else  /* USE_LIGHTING */
153         L_diffuse = vec3(1.0);
154 #endif
155
156         /* Compute diffuse color. */
157 #ifdef USE_TEXTURE_2D
158         L_diffuse *= texture2D(texture_buffer, inpt.v.uv).rgb;
159 #else
160         L_diffuse *= diffuse.rgb;
161 #endif
162
163         /* Sum lighting. */
164         vec3 L = L_diffuse;
165         if (shininess != 0) {
166                 L += L_specular * specular.rgb;
167         }
168
169         /* Write out fragment color. */
170         gl_FragColor = vec4(L, diffuse.a);
171 #endif
172 }