Fixes for texture and sampler codegen

- Moved textures and samplers out of property cbuffer for hlsl
- Fixed register indices for resources
This commit is contained in:
2024-07-01 07:58:13 +02:00
parent 274cb379b4
commit 92c1f593c2
3 changed files with 66 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
Output_Language :: enum {
HLSL;
GLSL;
GLSL; // @Incomplete
MLSL; // @Incomplete
}
Codegen_State :: struct {
@@ -28,10 +29,11 @@ Codegen_Result :: struct {
}
init_codegen_state :: (state : *Codegen_State, root : *AST_Node, checker_result : Semantic_Check_Result, output_language : Output_Language) {
state.root = root;
state.scope_stack = checker_result.scope_stack;
state.type_variables = checker_result.type_variables;
state.current_scope = cast(Scope_Handle)1;
state.root = root;
state.scope_stack = checker_result.scope_stack;
state.type_variables = checker_result.type_variables;
state.current_scope = cast(Scope_Handle)1;
state.output_language = output_language;
init_string_builder(*state.builder);
}
@@ -48,6 +50,14 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
print_to_builder(*state.builder, "% ", type_to_string(field));
print_to_builder(*state.builder, "%", node.name);
if field.type == .Sampler {
print_to_builder(*state.builder, " : register(s%)", field.resource_index);
}
if field.type == .Texture2D {
print_to_builder(*state.builder, " : register(t%)", field.resource_index);
}
for i :0..node.children.count - 1 {
child := node.children[i];
@@ -133,26 +143,40 @@ emit_properties :: (state : *Codegen_State, node : *AST_Node, indentation : int)
variable := h2tv(state.type_variables, find_result.type_variable);
print_to_builder(*state.builder, "cbuffer __PROPERTIES : register(b%) \n{\n", variable.buffer_index);
print_to_builder(*state.builder, "cbuffer __PROPERTIES : register(b%) \n{\n", variable.resource_index);
previous_scope := state.current_scope;
state.current_scope = variable.scope;
resources : Static_Array(*AST_Node, 8);
for child : node.children {
if child.kind == .FieldList {
for field : child.children {
tv := h2tv(state.type_variables, field.type_variable);
if tv.type == .Sampler || tv.type == .Texture2D {
array_add(*resources, field);
continue;
}
emit_node(state, field, 1);
append(*state.builder, ";\n");
}
}
}
state.current_scope = previous_scope;
append(*state.builder, "}\n\n");
for i : 0..resources.count - 1 {
resource := resources[i];
emit_node(state, resource, 0);
append(*state.builder, ";\n");
}
append(*state.builder, "\n");
state.current_scope = previous_scope;
}
emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, emit_body := true) {
@@ -372,7 +396,7 @@ emit_struct :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_cbuffer :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
variable := h2tv(state.type_variables, node.type_variable);
print_to_builder(*state.builder, "cbuffer % : register(b%)", variable.name, variable.buffer_index);
print_to_builder(*state.builder, "cbuffer % : register(b%)", variable.name, variable.resource_index);
current_scope := state.current_scope;
state.current_scope = h2tv(state.type_variables, node.type_variable).scope;