Change result to context for clarity. Fix a bunch of stuff in builtin functions and structs.

This commit is contained in:
2025-09-10 23:21:34 +02:00
parent ceafd197f5
commit 9461fe626f
7 changed files with 374 additions and 361 deletions

View File

@@ -28,10 +28,6 @@ Environment :: struct {
defines : [..]string;
}
Shader_Compiler :: struct {
environment : Environment;
}
Field_Kind :: enum {
Int :: 0;
Half :: 1;
@@ -141,7 +137,7 @@ Input_File :: struct {
path : string;
}
Compile_Result :: struct {
Compiler_Context :: struct {
file : Input_File;
environment : Environment;
@@ -174,14 +170,13 @@ Compile_Result :: struct {
properties : Properties;
max_constant_buffers :: 16;
cbuffers : Static_Array(Constant_Buffer, max_constant_buffers);
cbuffers : Static_Array(Constant_Buffer, max_constant_buffers);
had_error : bool;
messages : [..]Compiler_Message;
allocator : Allocator;
// string_allocator : Allocator;
}
#add_context scratch_allocators : [2]Allocator;
@@ -211,7 +206,7 @@ get_scratch :: (conflict : Allocator = .{}) -> Scratch {
return scratch_begin(*context.scratch_allocators[0]);
}
record_error :: (result : *Compile_Result, format : string, args : .. Any) {
record_error :: (result : *Compiler_Context, format : string, args : .. Any) {
error : Compiler_Message;
error.message_kind = .Error;
error.message = sprint(format, args);
@@ -219,7 +214,7 @@ record_error :: (result : *Compile_Result, format : string, args : .. Any) {
array_add(*result.messages, error);
}
make_file :: (result : *Compile_Result, path : string) -> Input_File {
make_file :: (result : *Compiler_Context, path : string) -> Input_File {
if !file_exists(path) {
record_error(result, "Unable to load file: %", path);
return .{};
@@ -407,18 +402,18 @@ type_variable_to_field :: (type_variables : []Type_Variable, scope_stack : Scope
}
type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variable) -> Field {
return type_variable_to_field(checker.result.type_variables, checker.result.scope_stack, variable);
return type_variable_to_field(checker.ctx.type_variables, checker.ctx.scope_stack, variable);
}
generate_output_data :: (result : *Compile_Result) {
if result.had_error {
generate_output_data :: (ctx : *Compiler_Context) {
if ctx.had_error {
return;
}
if result.vertex_entry_point.node {
result.vertex_entry_point.name = result.vertex_entry_point.node.name;
if ctx.vertex_entry_point.node {
ctx.vertex_entry_point.name = ctx.vertex_entry_point.node.name;
type_variable := from_handle(result.type_variables, result.vertex_entry_point.node.type_variable);
type_variable := from_handle(ctx.type_variables, ctx.vertex_entry_point.node.type_variable);
assert(type_variable.type == .Function);
node := type_variable.source_node;
@@ -426,23 +421,23 @@ generate_output_data :: (result : *Compile_Result) {
if node.children[0].kind == .FieldList {
field_list := node.children[0];
for child : field_list.children {
tv := from_handle(result.type_variables, child.type_variable);
field := type_variable_to_field(result.type_variables, result.scope_stack, tv);
array_add(*result.vertex_entry_point.input, field);
tv := from_handle(ctx.type_variables, child.type_variable);
field := type_variable_to_field(ctx.type_variables, ctx.scope_stack, tv);
array_add(*ctx.vertex_entry_point.input, field);
}
}
}
}
for buffer_variable : result.constant_buffers {
variable := from_handle(result.type_variables, buffer_variable);
for buffer_variable : ctx.constant_buffers {
variable := from_handle(ctx.type_variables, buffer_variable);
cb := array_add(*result.cbuffers);
cb := array_add(*ctx.cbuffers);
for i : 0..variable.children.count - 1 {
child := variable.children[i];
field : Property_Field;
field.base_field = type_variable_to_field(result.type_variables, result.scope_stack, from_handle(result.type_variables, child));
field.base_field = type_variable_to_field(ctx.type_variables, ctx.scope_stack, from_handle(ctx.type_variables, child));
array_add(*cb.fields, field);
}
@@ -456,28 +451,28 @@ generate_output_data :: (result : *Compile_Result) {
}
}
find_result := find_symbol(*result.scope_stack, result.property_name, xx 1);
find_result := find_symbol(*ctx.scope_stack, ctx.property_name, xx 1);
if find_result {
property_variable := from_handle(result.type_variables, find_result.type_variable);
property_variable := from_handle(ctx.type_variables, find_result.type_variable);
for i : 0..property_variable.children.count - 1 {
child := property_variable.children[i];
field := type_variable_to_field(result.type_variables, result.scope_stack, from_handle(result.type_variables, child));
field := type_variable_to_field(ctx.type_variables, ctx.scope_stack, from_handle(ctx.type_variables, child));
prop_field : Property_Field;
prop_field.base_field = field;
array_add(*result.properties.fields, prop_field);
array_add(*ctx.properties.fields, prop_field);
}
result.properties.buffer_index = property_variable.resource_index;
ctx.properties.buffer_index = property_variable.resource_index;
}
if result.pixel_entry_point.node {
result.pixel_entry_point.name = result.pixel_entry_point.node.name;
if ctx.pixel_entry_point.node {
ctx.pixel_entry_point.name = ctx.pixel_entry_point.node.name;
type_variable := from_handle(result.type_variables, result.pixel_entry_point.node.type_variable);
type_variable := from_handle(ctx.type_variables, ctx.pixel_entry_point.node.type_variable);
assert(type_variable.type == .Function);
field := type_variable_to_field(result.type_variables, result.scope_stack, type_variable.return_type_variable);
field := type_variable_to_field(ctx.type_variables, ctx.scope_stack, type_variable.return_type_variable);
for hint : type_variable.source_node.hint_tokens {
field_hint : Field_Hint;
@@ -501,33 +496,25 @@ generate_output_data :: (result : *Compile_Result) {
array_add(*field.hints, field_hint);
}
result.pixel_entry_point.return_value = field;
ctx.pixel_entry_point.return_value = field;
}
}
compile_file :: (compiler : *Shader_Compiler, path : string, allocator : Allocator = temp) -> Compile_Result {
result : Compile_Result;
compile_file :: (ctx : *Compiler_Context, path : string, allocator : Allocator = temp) {
new_context := context;
new_context.allocator = allocator;
push_context new_context {
init_context_allocators();
defer clear_context_allocators();
ctx.allocator = make_arena(Megabytes(128));
ctx.file = make_file(ctx, path);
result.allocator = make_arena(Megabytes(128));
result.file = make_file(*result, path);
result.environment = compiler.environment;
lex(*result);
parse(*result);
check(*result);
codegen(*result);
generate_output_data(*result);
lex(ctx);
parse(ctx);
check(ctx);
codegen(ctx);
generate_output_data(ctx);
}
return result;
}