-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.jai
More file actions
202 lines (177 loc) · 5.77 KB
/
first.jai
File metadata and controls
202 lines (177 loc) · 5.77 KB
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
Targets :: struct {
tempest :: "src/main.jai";
sdl3_bindings :: "sdl3_bindings_gen.jai";
vulkan_bindings :: "vulkan_bindings_gen.jai";
cgltf_bindings :: "cgltf_bindings_gen.jai";
}
build :: () {
// The compiler warning does not have \n for some reason, so add it manually.
print("\n");
ld_lib_path := getenv("LD_LIBRARY_PATH");
if ld_lib_path {
s := to_string(ld_lib_path);
for split(s, ":") {
print("Adding library search path: %\n", it);
compiler_add_library_search_directory(it);
}
}
compiler_add_library_search_directory("modules");
w := compiler_create_workspace("Tempest workspace");
if !w {
print("Workspace creation failed.\n");
return;
}
targets : Type_Info_Struct = type_info(Targets);
need_to_compile_shaders := false;
target_options := get_build_options(w);
args := target_options.compile_time_command_line;
for arg: args {
for targets.members {
if arg == it.name {
if it.offset_into_constant_storage >= 0 {
path: *string = xx (targets.constant_storage.data + it.offset_into_constant_storage);
compile(it.name, path.*);
}
}
}
if arg == "shaders" need_to_compile_shaders = true;
}
// note(sc): need to do shaders step after main compile, since it may update shader
// types
if need_to_compile_shaders compile_shaders();;
set_build_options_dc(.{do_output=false});
}
compile :: (binary_name: string, path: string) {
workspace_name := tprint("Workspace: name: % path: %", binary_name, path);
w := compiler_create_workspace(workspace_name);
if !w {
print(tprint("Failed: %\n", workspace_name));
return;
}
workspace_options := get_build_options();
workspace_options.output_executable_name = binary_name;
set_build_options(workspace_options, w);
compiler_begin_intercept(w, .SKIP_EXPRESSIONS_WITHOUT_NOTES);
add_build_file(tprint("%/%", #filepath, path), w);
structs := capture_shader_structs();
compiler_end_intercept(w);
write_shader_structs(structs);
}
compile_shaders :: () {
if !make_directory_if_it_does_not_exist(SHADER_TARGET_DIR) {
print("Cannot create \"%\" directroy\n", SHADER_TARGET_DIR);
return;
}
for file_list("./shaders") if ends_with(it, SHADER_EXT) compile_shader(it);
}
Struct :: struct {
name: string;
code: *Code_Struct;
}
capture_shader_structs :: () -> [..]Struct {
structs: [..]Struct;
has_note :: (s: *Code_Declaration, note: string) -> bool {
for s.notes if it.text == note return true;
return false;
}
while true {
message := compiler_wait_for_message();
if message.kind == {
case .TYPECHECKED;
typechecked := cast(*Message_Typechecked)message;
for typechecked.declarations {
decl := it.expression;
expr := decl.expression;
if expr.kind == .STRUCT && has_note(decl, "ShaderType") {
s := array_add(*structs);
s.name = decl.name;
s.code = cast(*Code_Struct)expr;
}
}
case .COMPLETE;
break;
}
}
return structs;
}
write_shader_structs :: (structs: []Struct) {
builder: String_Builder;
print_to_builder(
*builder,
#string DONE
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#define VkDeviceAddress uint64_t
DONE
);
for structs print_struct(*builder, it);
code := builder_to_string(*builder);
file_name := "shaders/types.glsl";
file, success := file_open(file_name , true, false, true);
assert(success, "[glsl gen]: Unable to open '%' for writing\n", file_name);
defer file_close(*file);
success = file_write(*file, code);
assert(success, "[glsl gen]: Unable to write output into '%'\n", file_name);
}
print_struct :: (builder: *String_Builder, s: Struct) {
c := s.code;
if c.textual_flags & .UNION {
assert(false, "[glsl gen]: Cannot convert union: %", s.name);
return;
}
Field :: struct { type: string; name: string; };
fields: [..]Field;
Alias :: struct { from: string; to: string; };
aliases: [..]Alias;
if c.block {
for c.block.statements {
if it.node_flags & .CREATED_BY_DESUGARING continue;
if it.kind == it.kind.DECLARATION {
decl := cast(*Code_Declaration)it;
type: string;
if decl.type_inst {
if decl.type_inst.type_valued_expression.kind == .IDENT {
ident := cast(*Code_Ident)decl.type_inst.type_valued_expression;
if decl.notes{
type = ident.name;
glsl_type := type_to_glsl_type(decl.notes[0].text);
array_add(*aliases, .{ ident.name, glsl_type });
} else {
type = type_to_glsl_type(ident.name);
}
}
}
if type array_add(*fields, .{ type, decl.name });
}
}
}
for aliases print_to_builder(builder, "#define % %\n", it.from, it.to);
print_to_builder(builder, "struct % {\n", s.name);
for fields print_to_builder(builder, " % %;\n", it.type, it.name);
print_to_builder(builder, "};\n\n");
}
type_to_glsl_type :: (type: string) -> string {
if type == {
case "u32"; return "uint";
case "u64"; return "uint64_t";//#through;
// case "VkDeviceAddress"; return "uint64_t";
case "VkDeviceAddress"; return "VkDeviceAddress";
case "float"; #through;
case "float32"; return "float";
case "Vector2"; return "vec2";
case "Vector3"; return "vec3";
case "Vector4"; return "vec4";
case "Matrix3"; return "mat3";
case "Matrix4"; return "mat4";
}
assert(false, "[glsl gen]: Cannot convert type: % to glsl type", type);
return {};
}
#run build();
#import,file "src/shaders.jai";
#import "File";
#import "Basic";
#import "POSIX";
#import "String";
#import "Process";
#import "Compiler";
#import "File_Utilities";