DonkeyKong64 grind crash
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_font.h
1 #ifndef SHADER_model_font_H
2 #define SHADER_model_font_H
3 static void shader_model_font_link(void);
4 static void shader_model_font_register(void);
5 static struct vg_shader _shader_model_font = {
6 .name = "model_font",
7 .link = shader_model_font_link,
8 .vs =
9 {
10 .orig_file = "shaders/model_font.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "\n"
16 "#line 1 1 \n"
17 "const float k_motion_lerp_amount = 0.01;\n"
18 "\n"
19 "#line 2 0 \n"
20 "\n"
21 "out vec3 aMotionVec0;\n"
22 "out vec3 aMotionVec1;\n"
23 "\n"
24 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
25 "{\n"
26 " // This magically solves some artifacting errors!\n"
27 " //\n"
28 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
29 "\n"
30 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
31 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
32 "}\n"
33 "\n"
34 "#line 6 0 \n"
35 "\n"
36 "uniform mat4x3 uMdl;\n"
37 "uniform mat4 uPv;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "uniform vec4 uOffset;\n"
40 "\n"
41 "out vec2 aUv;\n"
42 "out vec4 aNorm;\n"
43 "out vec3 aCo;\n"
44 "out vec3 aWorldCo;\n"
45 "\n"
46 "void main()\n"
47 "{\n"
48 " vec3 co = a_co*uOffset.w+uOffset.xyz;\n"
49 " vec3 world_pos0 = uMdl * vec4( co, 1.0 );\n"
50 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
51 " vec4 vproj1 = uPvmPrev * vec4( co, 1.0 );\n"
52 "\n"
53 " vs_motion_out( vproj0, vproj1 );\n"
54 "\n"
55 " gl_Position = vproj0;\n"
56 "\n"
57 " aUv = a_uv;\n"
58 " aNorm = vec4( mat3(uMdl) * a_norm, 0.0 );\n"
59 " aCo = co;\n"
60 " aWorldCo = world_pos0;\n"
61 "}\n"
62 ""},
63 .fs =
64 {
65 .orig_file = "shaders/model_font.fs",
66 .static_src =
67 "layout (location = 0) out vec4 oColour;\n"
68 "\n"
69 "uniform sampler2D uTexMain;\n"
70 "uniform vec4 uColour;\n"
71 "\n"
72 "in vec2 aUv;\n"
73 "in vec4 aNorm;\n"
74 "in vec3 aCo;\n"
75 "\n"
76 "#line 1 1 \n"
77 "const float k_motion_lerp_amount = 0.01;\n"
78 "\n"
79 "#line 2 0 \n"
80 "\n"
81 "layout (location = 1) out vec2 oMotionVec;\n"
82 "\n"
83 "in vec3 aMotionVec0;\n"
84 "in vec3 aMotionVec1;\n"
85 "\n"
86 "void compute_motion_vectors()\n"
87 "{\n"
88 " // Write motion vectors\n"
89 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
90 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
91 "\n"
92 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
93 "}\n"
94 "\n"
95 "#line 11 0 \n"
96 "#line 1 2 \n"
97 "uniform sampler2D uTexSceneDepth;\n"
98 "uniform vec3 uInverseRatioDepth;\n"
99 "uniform vec3 uInverseRatioMain;\n"
100 "uniform bool uDepthCompare;\n"
101 "\n"
102 "float linear_depth( float depth, float near, float far ) {\n"
103 " float z = depth * 2.0 - 1.0;\n"
104 " return (2.0 * near * far) / (far + near - z * (far - near)); \n"
105 "}\n"
106 "\n"
107 "void depth_compare_dither(){\n"
108 " if( uDepthCompare ){\n"
109 " vec2 back_coord = gl_FragCoord.xy * uInverseRatioMain.xy \n"
110 " * uInverseRatioDepth.xy;\n"
111 " float back_depth = texture( uTexSceneDepth, back_coord ).r;\n"
112 " float front_depth = gl_FragCoord.z/gl_FragCoord.w;\n"
113 "\n"
114 " back_depth = linear_depth( back_depth, 0.1, 2100.0 );\n"
115 " float diff = back_depth - front_depth;\n"
116 "\n"
117 " vec2 ssuv = gl_FragCoord.xy;\n"
118 " vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );\n"
119 " float dither = fract( vDither.g / 71.0 ) - 0.5;\n"
120 "\n"
121 " if( step(0.0,diff)+dither<0.3 )\n"
122 " discard;\n"
123 " }\n"
124 "}\n"
125 "\n"
126 "#line 12 0 \n"
127 "\n"
128 "void main(){\n"
129 " depth_compare_dither();\n"
130 " compute_motion_vectors();\n"
131 " oColour = texture( uTexMain, aUv ) * uColour;\n"
132 "}\n"
133 ""},
134 };
135
136 static GLuint _uniform_model_font_uMdl;
137 static GLuint _uniform_model_font_uPv;
138 static GLuint _uniform_model_font_uPvmPrev;
139 static GLuint _uniform_model_font_uOffset;
140 static GLuint _uniform_model_font_uTexMain;
141 static GLuint _uniform_model_font_uColour;
142 static GLuint _uniform_model_font_uTexSceneDepth;
143 static GLuint _uniform_model_font_uInverseRatioDepth;
144 static GLuint _uniform_model_font_uInverseRatioMain;
145 static GLuint _uniform_model_font_uDepthCompare;
146 static void shader_model_font_uMdl(m4x3f m){
147 glUniformMatrix4x3fv(_uniform_model_font_uMdl,1,GL_FALSE,(float*)m);
148 }
149 static void shader_model_font_uPv(m4x4f m){
150 glUniformMatrix4fv(_uniform_model_font_uPv,1,GL_FALSE,(float*)m);
151 }
152 static void shader_model_font_uPvmPrev(m4x4f m){
153 glUniformMatrix4fv(_uniform_model_font_uPvmPrev,1,GL_FALSE,(float*)m);
154 }
155 static void shader_model_font_uOffset(v4f v){
156 glUniform4fv(_uniform_model_font_uOffset,1,v);
157 }
158 static void shader_model_font_uTexMain(int i){
159 glUniform1i(_uniform_model_font_uTexMain,i);
160 }
161 static void shader_model_font_uColour(v4f v){
162 glUniform4fv(_uniform_model_font_uColour,1,v);
163 }
164 static void shader_model_font_uTexSceneDepth(int i){
165 glUniform1i(_uniform_model_font_uTexSceneDepth,i);
166 }
167 static void shader_model_font_uInverseRatioDepth(v3f v){
168 glUniform3fv(_uniform_model_font_uInverseRatioDepth,1,v);
169 }
170 static void shader_model_font_uInverseRatioMain(v3f v){
171 glUniform3fv(_uniform_model_font_uInverseRatioMain,1,v);
172 }
173 static void shader_model_font_uDepthCompare(int b){
174 glUniform1i(_uniform_model_font_uDepthCompare,b);
175 }
176 static void shader_model_font_register(void){
177 vg_shader_register( &_shader_model_font );
178 }
179 static void shader_model_font_use(void){ glUseProgram(_shader_model_font.id); }
180 static void shader_model_font_link(void){
181 _uniform_model_font_uMdl = glGetUniformLocation( _shader_model_font.id, "uMdl" );
182 _uniform_model_font_uPv = glGetUniformLocation( _shader_model_font.id, "uPv" );
183 _uniform_model_font_uPvmPrev = glGetUniformLocation( _shader_model_font.id, "uPvmPrev" );
184 _uniform_model_font_uOffset = glGetUniformLocation( _shader_model_font.id, "uOffset" );
185 _uniform_model_font_uTexMain = glGetUniformLocation( _shader_model_font.id, "uTexMain" );
186 _uniform_model_font_uColour = glGetUniformLocation( _shader_model_font.id, "uColour" );
187 _uniform_model_font_uTexSceneDepth = glGetUniformLocation( _shader_model_font.id, "uTexSceneDepth" );
188 _uniform_model_font_uInverseRatioDepth = glGetUniformLocation( _shader_model_font.id, "uInverseRatioDepth" );
189 _uniform_model_font_uInverseRatioMain = glGetUniformLocation( _shader_model_font.id, "uInverseRatioMain" );
190 _uniform_model_font_uDepthCompare = glGetUniformLocation( _shader_model_font.id, "uDepthCompare" );
191 }
192 #endif /* SHADER_model_font_H */