forked from bullno1/cosmo-sokol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-sokol
executable file
·321 lines (292 loc) · 13.9 KB
/
gen-sokol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python
import textwrap
import os
import re
SOKOL_FUNCTIONS = [
# sokol_app
"bool sapp_isvalid()",
"int sapp_width()",
"float sapp_widthf()",
"int sapp_height()",
"float sapp_heightf()",
"int sapp_color_format()",
"int sapp_depth_format()",
"int sapp_sample_count()",
"bool sapp_high_dpi()",
"float sapp_dpi_scale()",
"void sapp_show_keyboard(bool show)",
"bool sapp_keyboard_shown()",
"bool sapp_is_fullscreen()",
"void sapp_toggle_fullscreen()",
"void sapp_show_mouse(bool show)",
"bool sapp_mouse_shown()",
"void sapp_lock_mouse(bool lock)",
"bool sapp_mouse_locked()",
"void sapp_set_mouse_cursor(sapp_mouse_cursor cursor)",
"sapp_mouse_cursor sapp_get_mouse_cursor()",
"void* sapp_userdata()",
"sapp_desc sapp_query_desc()",
"void sapp_request_quit()",
"void sapp_cancel_quit()",
"void sapp_quit()",
"void sapp_consume_event()",
"uint64_t sapp_frame_count()",
"double sapp_frame_duration()",
"void sapp_set_clipboard_string(const char* str)",
"const char* sapp_get_clipboard_string()",
"void sapp_set_window_title(const char* str)",
"void sapp_set_icon(const sapp_icon_desc* icon_desc)",
"int sapp_get_num_dropped_files()",
"const char* sapp_get_dropped_file_path(int index)",
"void sapp_run(const sapp_desc* desc)",
"const void* sapp_egl_get_display()",
"const void* sapp_egl_get_context()",
"void sapp_html5_ask_leave_site(bool ask)",
"uint32_t sapp_html5_get_dropped_file_size(int index)",
"void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request* request)",
"const void* sapp_metal_get_device()",
"const void* sapp_metal_get_current_drawable()",
"const void* sapp_metal_get_depth_stencil_texture()",
"const void* sapp_metal_get_msaa_color_texture()",
"const void* sapp_macos_get_window()",
"const void* sapp_ios_get_window()",
"const void* sapp_d3d11_get_device()",
"const void* sapp_d3d11_get_device_context()",
"const void* sapp_d3d11_get_swap_chain()",
"const void* sapp_d3d11_get_render_view()",
"const void* sapp_d3d11_get_resolve_view()",
"const void* sapp_d3d11_get_depth_stencil_view()",
"const void* sapp_win32_get_hwnd()",
"const void* sapp_wgpu_get_device()",
"const void* sapp_wgpu_get_render_view()",
"const void* sapp_wgpu_get_resolve_view()",
"const void* sapp_wgpu_get_depth_stencil_view()",
"uint32_t sapp_gl_get_framebuffer()",
"int sapp_gl_get_major_version()",
"int sapp_gl_get_minor_version()",
"const void* sapp_android_get_native_activity()",
# sokol_gfx
"void sg_setup(const sg_desc* desc)",
"void sg_shutdown()",
"bool sg_isvalid()",
"void sg_reset_state_cache()",
"sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks)",
"void sg_push_debug_group(const char* name)",
"void sg_pop_debug_group()",
"bool sg_add_commit_listener(sg_commit_listener listener)",
"bool sg_remove_commit_listener(sg_commit_listener listener)",
"sg_buffer sg_make_buffer(const sg_buffer_desc* desc)",
"sg_image sg_make_image(const sg_image_desc* desc)",
"sg_sampler sg_make_sampler(const sg_sampler_desc* desc)",
"sg_shader sg_make_shader(const sg_shader_desc* desc)",
"sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc)",
"sg_attachments sg_make_attachments(const sg_attachments_desc* desc)",
"void sg_destroy_buffer(sg_buffer buf)",
"void sg_destroy_image(sg_image img)",
"void sg_destroy_sampler(sg_sampler smp)",
"void sg_destroy_shader(sg_shader shd)",
"void sg_destroy_pipeline(sg_pipeline pip)",
"void sg_destroy_attachments(sg_attachments atts)",
"void sg_update_buffer(sg_buffer buf, const sg_range* data)",
"void sg_update_image(sg_image img, const sg_image_data* data)",
"int sg_append_buffer(sg_buffer buf, const sg_range* data)",
"bool sg_query_buffer_overflow(sg_buffer buf)",
"bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size)",
"void sg_begin_pass(const sg_pass* pass)",
"void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left)",
"void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left)",
"void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left)",
"void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left)",
"void sg_apply_pipeline(sg_pipeline pip)",
"void sg_apply_bindings(const sg_bindings* bindings)",
"void sg_apply_uniforms(int ub_slot, const sg_range* data)",
"void sg_draw(int base_element, int num_elements, int num_instances)",
"void sg_end_pass()",
"void sg_commit()",
"sg_desc sg_query_desc()",
"sg_backend sg_query_backend()",
"sg_features sg_query_features()",
"sg_limits sg_query_limits()",
"sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt)",
"int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes)",
"int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes)",
"sg_resource_state sg_query_buffer_state(sg_buffer buf)",
"sg_resource_state sg_query_image_state(sg_image img)",
"sg_resource_state sg_query_sampler_state(sg_sampler smp)",
"sg_resource_state sg_query_shader_state(sg_shader shd)",
"sg_resource_state sg_query_pipeline_state(sg_pipeline pip)",
"sg_resource_state sg_query_attachments_state(sg_attachments atts)",
"sg_buffer_info sg_query_buffer_info(sg_buffer buf)",
"sg_image_info sg_query_image_info(sg_image img)",
"sg_sampler_info sg_query_sampler_info(sg_sampler smp)",
"sg_shader_info sg_query_shader_info(sg_shader shd)",
"sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip)",
"sg_attachments_info sg_query_attachments_info(sg_attachments atts)",
"sg_buffer_desc sg_query_buffer_desc(sg_buffer buf)",
"sg_image_desc sg_query_image_desc(sg_image img)",
"sg_sampler_desc sg_query_sampler_desc(sg_sampler smp)",
"sg_shader_desc sg_query_shader_desc(sg_shader shd)",
"sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip)",
"sg_attachments_desc sg_query_attachments_desc(sg_attachments atts)",
"sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc)",
"sg_image_desc sg_query_image_defaults(const sg_image_desc* desc)",
"sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc)",
"sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc)",
"sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc)",
"sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc)",
"sg_buffer sg_alloc_buffer()",
"sg_image sg_alloc_image()",
"sg_sampler sg_alloc_sampler()",
"sg_shader sg_alloc_shader()",
"sg_pipeline sg_alloc_pipeline()",
"sg_attachments sg_alloc_attachments()",
"void sg_dealloc_buffer(sg_buffer buf)",
"void sg_dealloc_image(sg_image img)",
"void sg_dealloc_sampler(sg_sampler smp)",
"void sg_dealloc_shader(sg_shader shd)",
"void sg_dealloc_pipeline(sg_pipeline pip)",
"void sg_dealloc_attachments(sg_attachments attachments)",
"void sg_init_buffer(sg_buffer buf, const sg_buffer_desc* desc)",
"void sg_init_image(sg_image img, const sg_image_desc* desc)",
"void sg_init_sampler(sg_sampler smg, const sg_sampler_desc* desc)",
"void sg_init_shader(sg_shader shd, const sg_shader_desc* desc)",
"void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc* desc)",
"void sg_init_attachments(sg_attachments attachments, const sg_attachments_desc* desc)",
"void sg_uninit_buffer(sg_buffer buf)",
"void sg_uninit_image(sg_image img)",
"void sg_uninit_sampler(sg_sampler smp)",
"void sg_uninit_shader(sg_shader shd)",
"void sg_uninit_pipeline(sg_pipeline pip)",
"void sg_uninit_attachments(sg_attachments atts)",
"void sg_fail_buffer(sg_buffer buf)",
"void sg_fail_image(sg_image img)",
"void sg_fail_sampler(sg_sampler smp)",
"void sg_fail_shader(sg_shader shd)",
"void sg_fail_pipeline(sg_pipeline pip)",
"void sg_fail_attachments(sg_attachments atts)",
"void sg_enable_frame_stats()",
"void sg_disable_frame_stats()",
"bool sg_frame_stats_enabled()",
"sg_frame_stats sg_query_frame_stats()",
"const void* sg_d3d11_device()",
"const void* sg_d3d11_device_context()",
"sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf)",
"sg_d3d11_image_info sg_d3d11_query_image_info(sg_image img)",
"sg_d3d11_sampler_info sg_d3d11_query_sampler_info(sg_sampler smp)",
"sg_d3d11_shader_info sg_d3d11_query_shader_info(sg_shader shd)",
"sg_d3d11_pipeline_info sg_d3d11_query_pipeline_info(sg_pipeline pip)",
"sg_d3d11_attachments_info sg_d3d11_query_attachments_info(sg_attachments atts)",
"const void* sg_mtl_device()",
"const void* sg_mtl_render_command_encoder()",
"sg_mtl_buffer_info sg_mtl_query_buffer_info(sg_buffer buf)",
"sg_mtl_image_info sg_mtl_query_image_info(sg_image img)",
"sg_mtl_sampler_info sg_mtl_query_sampler_info(sg_sampler smp)",
"sg_mtl_shader_info sg_mtl_query_shader_info(sg_shader shd)",
"sg_mtl_pipeline_info sg_mtl_query_pipeline_info(sg_pipeline pip)",
"const void* sg_wgpu_device()",
"const void* sg_wgpu_queue()",
"const void* sg_wgpu_command_encoder()",
"const void* sg_wgpu_render_pass_encoder()",
"sg_wgpu_buffer_info sg_wgpu_query_buffer_info(sg_buffer buf)",
"sg_wgpu_image_info sg_wgpu_query_image_info(sg_image img)",
"sg_wgpu_sampler_info sg_wgpu_query_sampler_info(sg_sampler smp)",
"sg_wgpu_shader_info sg_wgpu_query_shader_info(sg_shader shd)",
"sg_wgpu_pipeline_info sg_wgpu_query_pipeline_info(sg_pipeline pip)",
"sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attachments atts)",
"sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf)",
"sg_gl_image_info sg_gl_query_image_info(sg_image img)",
"sg_gl_sampler_info sg_gl_query_sampler_info(sg_sampler smp)",
"sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd)",
"sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts)",
]
PLATFORMS = ["Linux", "Windows"]
def main():
os.chdir(os.path.dirname(os.path.abspath(__file__)))
for platform in PLATFORMS:
with open(f"sokol_{platform.lower()}.h", "w") as f:
f.write("#pragma once\n")
for function in SOKOL_FUNCTIONS:
sig = parse_c_signature(function)
name = sig['name']
f.write(f"#define {name} {make_platform_name(name, platform)}\n")
with open(f"sokol_cosmo.c", "w") as f:
f.write(textwrap.dedent("""
#include <sokol_app.h>
#include <sokol_gfx.h>
#include <cosmo.h>
#pragma GCC diagnostic ignored "-Wreturn-type"
""").lstrip())
for function in SOKOL_FUNCTIONS:
sig = parse_c_signature(function)
return_type = sig['return_type']
name = sig['name']
args = sig['args']
for platform in PLATFORMS:
platform_name = make_platform_name(name, platform)
f.write(f"extern {return_type} {platform_name}({arg_list(args)});\n")
f.write(f"{return_type} {name}({arg_list(args)}) {{\n")
for platform in PLATFORMS:
platform_name = make_platform_name(name, platform)
f.write(f" if (Is{platform}()) {{\n")
if return_type == "void":
f.write(f" {platform_name}({forward_list(args)});\n")
f.write( " return;\n")
else:
f.write(f" return {platform_name}({forward_list(args)});\n")
f.write(f" }}\n")
f.write("}\n\n")
def make_platform_name(name, platform):
return f'{platform.lower()}_{name}'
def parse_c_signature(signature):
# Define a regex pattern to match a C function signature
pattern = r"(?P<return_type>[a-zA-Z_][a-zA-Z0-9_\s\*]+\s+\**)(?P<name>[a-zA-Z_][a-zA-Z0-9_]*)\s*\((?P<args>.*)\)"
# Match the pattern with the provided signature
match = re.match(pattern, signature.strip())
assert match, "Could not parse signature"
# Extract components from named groups
return_type = match.group("return_type").strip()
name = match.group("name").strip()
args = match.group("args").strip()
# Process arguments into a list, handling empty argument lists
args_list = [arg.strip() for arg in args.split(",")] if args else []
# Further split each argument into type and name
processed_args = []
for arg in args_list:
if arg: # Only process non-empty arguments
# Separate type and name for each argument
parts = arg.rsplit(" ", 1) # Split from the right to handle cases like "int *ptr"
if len(parts) == 2:
arg_type, arg_name = parts[0].strip(), parts[1].strip()
else:
# Handle cases with unnamed parameters or pointer-only types
arg_type, arg_name = parts[0].strip(), None
# Pointer arg
if arg_name.startswith('*'):
new_arg_name = arg_name.lstrip('*')
num_stars = len(arg_name) - len(new_arg_name)
arg_name = new_arg_name
arg_type = arg_type + '*' * num_stars
processed_args.append({"type": arg_type, "name": arg_name})
return {
"return_type": return_type,
"name": name,
"args": processed_args
}
def arg_list(params):
if len(params) > 0:
return ', '.join([
f"{param['type']} {param['name']}"
for param in params
])
else:
return 'void'
def forward_list(params):
if len(params) > 0:
return ', '.join([
f"{param['name']}"
for param in params
])
else:
return ''
if __name__ == "__main__":
main()