Fix clipping shaders with some AMD/Intel drivers
[blender.git] / source / blender / draw / engines / workbench / shaders / workbench_prepass_vert.glsl
1 uniform mat4 ModelViewProjectionMatrix;
2 uniform mat4 ModelMatrix;
3 uniform mat4 ModelMatrixInverse;
4 uniform mat4 ProjectionMatrix;
5 uniform mat4 ViewProjectionMatrix;
6 uniform mat4 ViewMatrixInverse;
7 uniform mat3 NormalMatrix;
8
9 #ifndef HAIR_SHADER
10 in vec3 pos;
11 in vec3 nor;
12 in vec2 u; /* active texture layer */
13 #define uv u
14 #else /* HAIR_SHADER */
15 #  ifdef V3D_SHADING_TEXTURE_COLOR
16 uniform samplerBuffer u; /* active texture layer */
17 #  endif
18 flat out float hair_rand;
19 #endif /* HAIR_SHADER */
20
21 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
22 out vec3 normal_viewport;
23 #endif
24
25 #ifdef V3D_SHADING_TEXTURE_COLOR
26 out vec2 uv_interp;
27 #endif
28
29 /* From http://libnoise.sourceforge.net/noisegen/index.html */
30 float integer_noise(int n)
31 {
32         n = (n >> 13) ^ n;
33         int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
34         return (float(nn) / 1073741824.0);
35 }
36
37 void main()
38 {
39 #ifdef HAIR_SHADER
40 #  ifdef V3D_SHADING_TEXTURE_COLOR
41         vec2 uv = hair_get_customdata_vec2(u);
42 #  endif
43         float time, thick_time, thickness;
44         vec3 pos, tan, binor;
45         hair_get_pos_tan_binor_time(
46                 (ProjectionMatrix[3][3] == 0.0),
47                 ModelMatrixInverse,
48                 ViewMatrixInverse[3].xyz, ViewMatrixInverse[2].xyz,
49                 pos, tan, binor, time, thickness, thick_time);
50         /* To "simulate" anisotropic shading, randomize hair normal per strand. */
51         hair_rand = integer_noise(hair_get_strand_id());
52         tan = normalize(tan);
53         vec3 nor = normalize(cross(binor, tan));
54         nor = normalize(mix(nor, -tan, hair_rand * 0.10));
55         float cos_theta = (hair_rand*2.0 - 1.0) * 0.20;
56         float sin_theta = sqrt(max(0.0, 1.0f - cos_theta*cos_theta));
57         nor = nor * sin_theta + binor * cos_theta;
58         gl_Position = ViewProjectionMatrix * vec4(pos, 1.0);
59 #else
60         gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
61 #endif
62 #ifdef V3D_SHADING_TEXTURE_COLOR
63         uv_interp = uv;
64 #endif
65
66 #ifdef NORMAL_VIEWPORT_PASS_ENABLED
67         normal_viewport = NormalMatrix * nor;
68 #  ifndef HAIR_SHADER
69         normal_viewport = normalize(normal_viewport);
70 #  endif
71 #endif
72
73 #ifdef USE_WORLD_CLIP_PLANES
74         world_clip_planes_calc_clip_distance((ModelMatrix * vec4(pos, 1.0)).xyz);
75 #endif
76
77 }