naev 0.10.4
shaders_c_gen.py
1#!/usr/bin/env python3
2
3class Shader:
4 def __init__(self, name, vs_path, fs_path, attributes, uniforms, subroutines):
5 self.name = name
6 self.vs_path = vs_path
7 self.fs_path = fs_path
8 self.attributes = attributes
9 self.uniforms = uniforms
10 self.subroutines= subroutines
11
12 def header_chunks(self):
13 yield " struct {\n"
14 yield " GLuint program;\n"
15 for attribute in self.attributes:
16 yield f" GLuint {attribute};\n"
17 for uniform in self.uniforms:
18 yield f" GLuint {uniform};\n"
19 for subroutine, routines in self.subroutines.items():
20 yield " struct {\n"
21 yield " GLuint uniform;\n"
22 for r in routines:
23 yield f" GLuint {r};\n"
24 yield f" }} {subroutine};\n"
25 yield f" }} {self.name};\n"
26
27 def source_chunks(self):
28 yield f" shaders.{self.name}.program = gl_program_vert_frag(\"{self.vs_path}\", \"{self.fs_path}\");\n"
29 for attribute in self.attributes:
30 yield f" shaders.{self.name}.{attribute} = glGetAttribLocation(shaders.{self.name}.program, \"{attribute}\");\n"
31 for uniform in self.uniforms:
32 yield f" shaders.{self.name}.{uniform} = glGetUniformLocation(shaders.{self.name}.program, \"{uniform}\");\n"
33 if len(self.subroutines) > 0:
34 yield " if (gl_has( OPENGL_SUBROUTINES )) {\n"
35 for subroutine, routines in self.subroutines.items():
36 yield f" shaders.{self.name}.{subroutine}.uniform = glGetSubroutineUniformLocation( shaders.{self.name}.program, GL_FRAGMENT_SHADER, \"{subroutine}\" );\n"
37 for r in routines:
38 yield f" shaders.{self.name}.{subroutine}.{r} = glGetSubroutineIndex( shaders.{self.name}.program, GL_FRAGMENT_SHADER, \"{r}\" );\n"
39 yield " }\n"
40
41num_simpleshaders = 0
43 def __init__(self, name, fs_path):
44 super().__init__( name=name, vs_path="project_pos.vert", fs_path=fs_path, attributes=["vertex"], uniforms=["projection","color","dimensions","dt","paramf","parami","paramv"], subroutines={} )
45 global num_simpleshaders
46 num_simpleshaders += 1
47 def header_chunks(self):
48 yield f" SimpleShader {self.name};\n"
49 def source_chunks(self):
50 yield f" shaders_loadSimple( \"{self.name}\", &shaders.{self.name}, \"{self.fs_path}\" );"
51
52SHADERS = [
53 Shader(
54 name = "solid",
55 vs_path = "project.vert",
56 fs_path = "solid.frag",
57 attributes = ["vertex"],
58 uniforms = ["projection", "color"],
59 subroutines = {},
60 ),
61 Shader(
62 name = "trail",
63 vs_path = "project_pos.vert",
64 fs_path = "trail.frag",
65 attributes = ["vertex"],
66 uniforms = ["projection", "c1", "c2", "t1", "t2", "dt", "pos1", "pos2", "r", "nebu_col" ],
67 subroutines = {
68 "trail_func" : [
69 "trail_default",
70 "trail_pulse",
71 "trail_wave",
72 "trail_flame",
73 "trail_nebula",
74 "trail_arc",
75 "trail_bubbles",
76 ]
77 }
78 ),
79 Shader(
80 name = "smooth",
81 vs_path = "smooth.vert",
82 fs_path = "smooth.frag",
83 attributes = ["vertex", "vertex_color"],
84 uniforms = ["projection"],
85 subroutines = {},
86 ),
87 Shader(
88 name = "texture",
89 vs_path = "texture.vert",
90 fs_path = "texture.frag",
91 attributes = ["vertex"],
92 uniforms = ["projection", "color", "tex_mat", "sampler"],
93 subroutines = {},
94 ),
95 Shader(
96 name = "texture_interpolate",
97 vs_path = "texture.vert",
98 fs_path = "texture_interpolate.frag",
99 attributes = ["vertex"],
100 uniforms = ["projection", "color", "tex_mat", "sampler1", "sampler2", "inter"],
101 subroutines = {},
102 ),
103 Shader(
104 name = "stealthoverlay",
105 vs_path = "texture.vert",
106 fs_path = "stealthoverlay.frag",
107 attributes = ["vertex"],
108 uniforms = ["projection", "color", "tex_mat"],
109 subroutines = {},
110 ),
111 Shader(
112 name = "nebula",
113 vs_path = "nebula.vert",
114 fs_path = "nebula_overlay.frag",
115 attributes = ["vertex"],
116 uniforms = ["projection", "hue", "nonuniformity", "horizon", "eddy_scale", "time"],
117 subroutines = {},
118 ),
119 Shader(
120 name = "nebula_background",
121 vs_path = "nebula.vert",
122 fs_path = "nebula_background.frag",
123 attributes = ["vertex"],
124 uniforms = ["projection", "hue", "nonuniformity", "eddy_scale", "time", "volatility"],
125 subroutines = {},
126 ),
127 Shader(
128 name = "nebula_puff",
129 vs_path = "project_pos.vert",
130 fs_path = "nebula_puff.frag",
131 attributes = ["vertex"],
132 uniforms = ["projection", "nebu_col", "time", "r" ],
133 subroutines = {},
134 ),
135 Shader(
136 name = "nebula_map",
137 vs_path = "system_map.vert",
138 fs_path = "nebula_map.frag",
139 attributes = ["vertex"],
140 uniforms = ["projection", "hue", "time", "globalpos", "alpha", "volatility"],
141 subroutines = {},
142 ),
143 Shader(
144 name = "points",
145 vs_path = "smooth.vert",
146 fs_path = "points.frag",
147 attributes = ["vertex", "vertex_color"],
148 uniforms = ["projection"],
149 subroutines = {},
150 ),
151 Shader(
152 name = "stars",
153 vs_path = "stars.vert",
154 fs_path = "stars.frag",
155 attributes = ["vertex", "brightness"],
156 uniforms = ["projection", "star_xy", "dims", "xy", "use_lines", "dim"],
157 subroutines = {},
158 ),
159 Shader(
160 name = "lines",
161 vs_path = "lines.vert",
162 fs_path = "lines.frag",
163 attributes = ["vertex"],
164 uniforms = ["projection", "colour"],
165 subroutines = {},
166 ),
167 Shader(
168 name = "font",
169 vs_path = "font.vert",
170 fs_path = "font.frag",
171 attributes = ["vertex", "tex_coord"],
172 uniforms = ["projection", "m", "color", "outline_color"],
173 subroutines = {},
174 ),
175 Shader(
176 name = "beam",
177 vs_path = "project_pos.vert",
178 fs_path = "beam.frag",
179 attributes = ["vertex"],
180 uniforms = ["projection", "color", "dt", "r", "dimensions" ],
181 subroutines = {
182 "beam_func" : [
183 "beam_default",
184 "beam_wave",
185 "beam_arc",
186 "beam_helix",
187 "beam_organic",
188 "beam_unstable",
189 "beam_fuzzy",
190 ]
191 }
192 ),
193 Shader(
194 name = "jump",
195 vs_path = "project_pos.vert",
196 fs_path = "jump.frag",
197 attributes = ["vertex"],
198 uniforms = ["projection", "progress", "direction", "dimensions", "brightness"],
199 subroutines = {
200 "jump_func" : [
201 "jump_default",
202 "jump_nebula",
203 "jump_organic",
204 "jump_circular",
205 "jump_wind",
206 ]
207 }
208 ),
209 Shader(
210 name = "material",
211 vs_path = "material.vert",
212 fs_path = "material.frag",
213 attributes = ["vertex", "vertex_normal", "vertex_tex"],
214 uniforms = ["projection", "model", "map_Kd", "map_Ks", "map_Ke", "map_Bump", "Ns", "Ka", "Kd", "Ks", "Ke", "Ni", "d", "bm"],
215 subroutines = {},
216 ),
217 Shader(
218 name = "colorblind",
219 vs_path = "postprocess.vert",
220 fs_path = "colorblind.frag",
221 attributes = ["VertexPosition"],
222 uniforms = ["ClipSpaceFromLocal", "MainTex"],
223 subroutines = {},
224 ),
225 Shader(
226 name = "shake",
227 vs_path = "postprocess.vert",
228 fs_path = "shake.frag",
229 attributes = ["VertexPosition"],
230 uniforms = ["ClipSpaceFromLocal", "MainTex", "shake_pos", "shake_vel", "shake_force"],
231 subroutines = {},
232 ),
233 Shader(
234 name = "damage",
235 vs_path = "postprocess.vert",
236 fs_path = "damage.frag",
237 attributes = ["VertexPosition"],
238 uniforms = ["ClipSpaceFromLocal", "MainTex", "damage_strength", "love_ScreenSize"],
239 subroutines = {},
240 ),
241 Shader(
242 name = "gamma_correction",
243 vs_path = "postprocess.vert",
244 fs_path = "gamma_correction.frag",
245 attributes = ["VertexPosition"],
246 uniforms = ["ClipSpaceFromLocal", "MainTex", "gamma"],
247 subroutines = {},
248 ),
250 name = "status",
251 fs_path = "status.frag",
252 ),
254 name = "factiondisk",
255 fs_path = "factiondisk.frag",
256 ),
258 name = "stealthaura",
259 fs_path = "stealthaura.frag",
260 ),
262 name = "spobmarker_empty",
263 fs_path = "spobmarker_empty.frag",
264 ),
266 name = "spobmarker_earth",
267 fs_path = "spobmarker_earth.frag",
268 ),
270 name = "spobmarker_rhombus",
271 fs_path = "spobmarker_rhombus.frag",
272 ),
274 name = "spobmarker_triangle",
275 fs_path = "spobmarker_triangle.frag",
276 ),
278 name = "spobmarker_wormhole",
279 fs_path = "spobmarker_wormhole.frag",
280 ),
282 name = "jumpmarker",
283 fs_path = "jumpmarker.frag",
284 ),
286 name = "pilotmarker",
287 fs_path = "pilotmarker.frag",
288 ),
290 name = "playermarker",
291 fs_path = "playermarker.frag",
292 ),
294 name = "blinkmarker",
295 fs_path = "blinkmarker.frag",
296 ),
298 name = "sysmarker",
299 fs_path = "sysmarker.frag",
300 ),
302 name = "notemarker",
303 fs_path = "notemarker.frag",
304 ),
306 name = "asteroidmarker",
307 fs_path = "asteroidmarker.frag",
308 ),
310 name = "targetship",
311 fs_path = "targetship.frag",
312 ),
314 name = "targetspob",
315 fs_path = "targetspob.frag",
316 ),
318 name = "jumplane",
319 fs_path = "jumplane.frag",
320 ),
322 name = "jumplanegoto",
323 fs_path = "jumplanegoto.frag",
324 ),
326 name = "safelane",
327 fs_path = "safelane.frag",
328 ),
330 name = "iflockon",
331 fs_path = "iflockon.frag",
332 ),
334 name = "gear",
335 fs_path = "gear.frag",
336 ),
338 name = "selectspob",
339 fs_path = "selectspob.frag",
340 ),
342 name = "selectposition",
343 fs_path = "selectposition.frag",
344 ),
346 name = "sdfsolid",
347 fs_path = "sdfsolid.frag",
348 ),
350 name = "circle",
351 fs_path = "circle.frag",
352 ),
354 name = "crosshairs",
355 fs_path = "crosshairs.frag",
356 ),
358 name = "astaura",
359 fs_path = "astaura.frag",
360 ),
362 name = "hilight",
363 fs_path = "hilight.frag",
364 ),
366 name = "hilight_circle",
367 fs_path = "hilight_circle.frag",
368 ),
370 name = "stealthmarker",
371 fs_path = "stealthmarker.frag",
372 ),
374 name = "healthbar",
375 fs_path = "healthbar.frag",
376 ),
377]
378
379def header_chunks():
380 yield f"/* FILE GENERATED BY {__file__} */"
381
382def generate_h_file():
383 yield from header_chunks()
384
385 yield f"""
386#pragma once
387
388#include "glad.h"
389
390#define NUM_SIMPLE_SHADERS {num_simpleshaders}
391
392typedef struct SimpleShader_ {{
393 const char *name;
394 GLuint program;
395 GLuint vertex;
396 GLuint projection;
397 GLuint color;
398 GLuint dimensions;
399 GLuint dt;
400 GLuint parami;
401 GLuint paramf;
402 GLuint paramv;
403}} SimpleShader;
404
405typedef struct Shaders_ {{
406"""
407
408 for shader in SHADERS:
409 yield from shader.header_chunks()
410
411 yield f""" SimpleShader *simple_shaders[ NUM_SIMPLE_SHADERS ];
412}} Shaders;
413
414extern Shaders shaders;
415
416void shaders_load (void);
417void shaders_unload (void);
418const SimpleShader *shaders_getSimple( const char *name );
419"""
420
421def generate_c_file():
422 yield from header_chunks()
423
424 yield """
425#include <string.h>
426#include "shaders.gen.h"
427#include "opengl_shader.h"
428
429Shaders shaders;
430
431static int nsimpleshaders = 0;
432
433static int shaders_cmp( const void *p1, const void *p2 )
434{
435 const SimpleShader **s1 = (const SimpleShader**) p1;
436 const SimpleShader **s2 = (const SimpleShader**) p2;
437 return strcmp( (*s1)->name, (*s2)->name );
438}
439
440static int shaders_loadSimple( const char *name, SimpleShader *shd, const char *fs_path )
441{
442 shd->name = name;
443 shd->program = gl_program_vert_frag( "project_pos.vert", fs_path );
444 shd->vertex = glGetAttribLocation( shd->program, "vertex" );
445 shd->projection = glGetUniformLocation( shd->program, "projection" );
446 shd->color = glGetUniformLocation( shd->program, "color" );
447 shd->dimensions = glGetUniformLocation( shd->program, "dimensions" );
448 shd->dt = glGetUniformLocation( shd->program, "dt" );
449 shd->paramf = glGetUniformLocation( shd->program, "paramf" );
450 shd->parami = glGetUniformLocation( shd->program, "parami" );
451 shd->paramv = glGetUniformLocation( shd->program, "paramv" );
452
453 /* Add to list. */
454 shaders.simple_shaders[ nsimpleshaders++ ] = shd;
455
456 return 0;
457}
458
459const SimpleShader *shaders_getSimple( const char *name )
460{
461 const SimpleShader shd = { .name=name };
462 const SimpleShader *shdptr = &shd;
463 const SimpleShader **found = bsearch( &shdptr, shaders.simple_shaders, nsimpleshaders, sizeof(SimpleShader*), shaders_cmp );
464 if (found!=NULL)
465 return *found;
466 return NULL;
467}
468
469void shaders_load (void) {
470"""
471 for i, shader in enumerate(SHADERS):
472 yield from shader.source_chunks()
473 if i != len(SHADERS) - 1:
474 yield "\n"
475 yield """
476 qsort( shaders.simple_shaders, nsimpleshaders, sizeof(SimpleShader*), shaders_cmp );
477}
478
479void shaders_unload (void) {
480"""
481 for shader in SHADERS:
482 yield f" glDeleteProgram(shaders.{shader.name}.program);\n"
483
484 yield """ memset(&shaders, 0, sizeof(shaders));
485 nsimpleshaders = 0;
486}"""
487
488with open("shaders.gen.h", "w") as shaders_gen_h:
489 shaders_gen_h.writelines(generate_h_file())
490
491with open("shaders.gen.c", "w") as shaders_gen_c:
492 shaders_gen_c.writelines(generate_c_file())