]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/common.glsl
Add instancing support to standard shaders and RenderMethod
[libs/gl.git] / shaderlib / common.glsl
1 import msp_interface;
2 import shadow;
3
4 layout(set=0) uniform EnvMap
5 {
6         mat3 env_world_matrix;
7 };
8
9 layout(set=1) uniform sampler2D normal_map;
10 layout(set=0) uniform samplerCube environment_map;
11 layout(set=0) uniform samplerCube irradiance_map;
12
13 layout(constant_id=auto) const bool use_instancing = false;
14 layout(constant_id=auto) const bool use_normal_map = false;
15
16 #pragma MSP stage(vertex)
17 virtual vec4 get_vertex_position()
18 {
19         return vertex;
20 }
21
22 virtual vec3 get_vertex_normal()
23 {
24         return normal;
25 }
26
27 virtual mat4 get_vertex_transform()
28 {
29         if(use_instancing)
30                 return transpose(mat4(instance_transform[0], instance_transform[1], instance_transform[2], vec4(0.0, 0.0, 0.0, 1.0)));
31         else
32                 return world_obj_matrix;
33 }
34
35 virtual mat3 get_normal_transform()
36 {
37         if(use_instancing)
38                 return transpose(mat3(instance_transform[0].xyz, instance_transform[1].xyz, instance_transform[2].xyz));
39         else
40                 return world_obj_normal_matrix;
41 }
42
43 void standard_transform()
44 {
45         mat4 vertex_tf = get_vertex_transform();
46         mat3 normal_tf = get_normal_transform();
47
48         out vec4 world_vertex = vertex_tf*get_vertex_position();
49         vec4 eye_vertex = eye_world_matrix*world_vertex;
50         gl_Position = clip_eye_matrix*eye_vertex;
51
52         out vec3 world_normal = normalize(normal_tf*get_vertex_normal());
53         if(use_normal_map)
54         {
55                 vec3 world_tangent = normalize(normal_tf*tangent);
56                 vec3 world_binormal = normalize(cross(world_normal, world_tangent));
57                 out mat3 world_tbn_matrix = mat3(world_tangent, world_binormal, world_normal);
58         }
59
60         vec3 eye_pos = world_eye_matrix[3].xyz;
61         out vec3 world_look_dir = normalize(world_vertex.xyz-eye_pos);
62
63         out float fog_coord = eye_vertex.z;
64 }
65
66 virtual void custom_transform()
67 {
68 }
69
70 void main()
71 {
72         standard_transform();
73         custom_transform();
74         passthrough;
75 }
76
77 #pragma MSP stage(fragment)
78 struct IncomingLight
79 {
80         vec3 direction;
81         vec3 color;
82 };
83
84 layout(location=0) out vec4 frag_color;
85
86 virtual vec3 get_fragment_normal()
87 {
88         vec3 normal;
89         float sgn = (gl_FrontFacing ? 1.0 : -1.0);
90         if(use_normal_map)
91                 return sgn*normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0));
92         else
93                 return sgn*normalize(world_normal);
94 }
95
96 virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
97 {
98         vec4 light_pos = light_sources[index].position;
99         vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
100         float d = length(rel_pos);
101         float attenuation = 1.0/dot(vec3(1.0, d, d*d), light_sources[index].attenuation);
102         return IncomingLight(rel_pos/d, light_sources[index].color*attenuation);
103 }
104
105 virtual vec3 get_environment_sample(vec3 direction, float roughness)
106 {
107         float lod = (2-roughness)*roughness*(textureQueryLevels(environment_map)-1);
108         return textureLod(environment_map, env_world_matrix*direction, lod).rgb;
109 }
110
111 virtual vec3 get_reflection(vec3 normal, vec3 look)
112 {
113         vec3 reflect_dir = reflect(look, normal);
114         return get_environment_sample(reflect_dir, 0.0);
115 }
116
117 virtual vec3 get_irradiance_sample(vec3 normal)
118 {
119         return texture(irradiance_map, env_world_matrix*normal).rgb;
120 }
121
122 vec3 apply_fog(vec3 color)
123 {
124         float fog_value = exp(fog_coord*fog_density);
125         return mix(fog_color.rgb, color, fog_value);
126 }