Fix static array. Add constant buffers. Beginning of textures.

This commit is contained in:
2024-06-26 13:42:13 +02:00
parent 3bbbc1d556
commit 1bf829d42f
8 changed files with 217 additions and 59 deletions

View File

@@ -313,6 +313,7 @@ advance :: (parse_state : *Parse_State) {
err := tprint("unknown token \x1b[1;37m'%'\x1b[0m", parse_state.current.string_value);
unexpected_token(parse_state, parse_state.current, err);
return;
}
}
@@ -340,7 +341,9 @@ consume :: (parse_state : *Parse_State, kind : Token_Kind, message : string) {
return;
}
advance(parse_state);
unexpected_token(parse_state, parse_state.current, message);
consume(parse_state, kind, message);
}
////////////////////////////

View File

@@ -6,7 +6,7 @@
//~ nbr: Error reporting TODOs
//
// [x] Improve error reporting on mismatched overloads when types don't match, but arity does
// [ ] Improve error reporting for type mismatches in general. It seems like the expect node is no always correct.
// [x] Improve error reporting for type mismatches in general. It seems like the expect node is not always correct.
#load "static_array.jai";
#import "Hash_Table";
@@ -920,9 +920,7 @@ declare_struct :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variab
return declare_struct(checker, node, node.name);
}
PROPERTIES_BUFFER_INDEX : u32 : 0;
META_BUFFER_INDEX : u32 : PROPERTIES_BUFFER_INDEX + 1;
current_custom_buffer_index : u32 = META_BUFFER_INDEX + 1;
current_buffer_index : u32 = 0;
declare_properties :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_Handle {
name := ifx node.name.count == 0 then "properties" else node.name;
@@ -930,7 +928,8 @@ declare_properties :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Va
var := h2tv(checker, type_var);
var.type = .Properties;
var.typename = "properties";
var.buffer_index = PROPERTIES_BUFFER_INDEX;
var.buffer_index = current_buffer_index;
current_buffer_index += 1;
return type_var;
}
@@ -938,21 +937,12 @@ declare_cbuffer :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Varia
type_var := declare_struct(checker, node);
var := h2tv(checker, type_var);
var.type = .CBuffer;
current_custom_buffer_index += 1;
var.buffer_index = current_custom_buffer_index;
var.buffer_index = current_buffer_index;
current_buffer_index += 1;
array_add(*checker.result.constant_buffers, type_var);
return type_var;
}
declare_meta :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Variable_Handle {
name := ifx node.name.count == 0 then "meta" else node.name;
type_var := declare_struct(checker, node, name);
var := h2tv(checker, type_var);
var.typename = name;
var.buffer_index = META_BUFFER_INDEX;
return type_var;
}
get_actual_function_name :: (node : *AST_Node) -> string {
name_to_check := node.name;
if node.vertex_entry_point {
@@ -1501,8 +1491,6 @@ traverse :: (checker : *Semantic_Checker, root : *AST_Node) {
declare_properties(checker, declaration);
} else if declaration.kind == .Struct {
declare_struct(checker, declaration);
} else if declaration.kind == .Meta {
declare_meta(checker, declaration);
} else if declaration.kind == .CBuffer {
declare_cbuffer(checker, declaration);
}
@@ -1729,6 +1717,7 @@ type_check :: (checker : *Semantic_Checker, root : *AST_Node) {
}
check :: (checker : *Semantic_Checker, root : *AST_Node) -> Semantic_Check_Result {
current_buffer_index = 0;
array_reserve(*checker.result.messages, 16);
array_reserve(*checker.constraints, 1024);
add_hlsl_builtins(checker);
@@ -1745,18 +1734,6 @@ check :: (checker : *Semantic_Checker) -> Semantic_Check_Result {
// ===========================================================
// Pretty printing
pretty_print_children :: (checker : *Semantic_Checker, builder : *String_Builder, type : Type_Variable, indentation : int) {
indent(builder, indentation);
for 0..type.child_count - 1 {
child_handle := type.children[it];
child := h2tv(checker, child_handle);
// if child.kind != .Function {
// print_to_builder(builder, "% : %", child.name, type_to_string(child));
// } else {
// pretty_print_function(checker, builder, "", child, 0);
// }
}
}
type_to_string :: (type_variable : Type_Variable) -> string {
if type_variable.type == {
@@ -1842,7 +1819,6 @@ pretty_print_struct :: (checker : *Semantic_Checker, builder : *String_Builder,
}
}
pretty_print_children(checker, builder, struct_type, 0);
append(builder, "}\n");
}

View File

@@ -518,7 +518,7 @@ run_test_suite :: (using suite : *Test_Suite, output_type : Output_Type = 0) {
case .Golden_File_Not_Found; {
array_add(*failed_test_paths, .{ result.path, tprint("golden file not found for %", stage_to_string(result.stage)) });
}
}
}
evaluate_result(result);
} else {
break;

View File

@@ -116,13 +116,16 @@ Constant_Buffer :: struct {
name : string;
fields : Property_Field;
fields : Static_Array(Property_Field, 16);
buffer_index : u32;
}
Shader_Variant_Collection :: struct {
properties : Properties;
cbuffers : [..]Constant_Buffer;
max_constant_buffers :: 16;
cbuffers : Static_Array(Constant_Buffer, max_constant_buffers);
variants : [..]Shader_Variant;
}
@@ -357,14 +360,26 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
for child : field_list.children {
tv := h2tv(*checker, child.type_variable);
field := type_variable_to_field(*checker, tv);
print("%\n", pretty_print_field(*field));
array_add(*variant.vertex_entry_point.input, field);
}
}
}
}
for buffer_variable : to_array(*check_result.constant_buffers) {
variable := h2tv(check_result.type_variables, buffer_variable);
cb := array_add(*result.collection.cbuffers);
for i : 0..variable.child_count - 1 {
child := variable.children[i];
field : Property_Field;
field.base_field = type_variable_to_field(*checker, h2tv(*checker, child));;
array_add(*cb.fields, field);
}
cb.buffer_index = variable.buffer_index;
}
find_result := find_symbol(*check_result.scope_stack, "properties", xx 1);
if find_result {
@@ -411,7 +426,6 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
}
variant.pixel_entry_point.return_value = field;
print("%\n", pretty_print_field(*field));
}
array_add(*result.collection.variants, variant);

View File

@@ -1,29 +1,43 @@
Static_Array :: struct ($T : Type, $N : s64) {
Static_Array :: struct (T : Type, N : int) {
array : [N] T;
capacity : s64 = N;
count : s64;
count : int;
}
// operator [] :: (array : Static_Array($T, $N), index : int) -> T {
// assert(index < array.count);
// return array.array[index];
// }
// operator []= :: (array : *Static_Array($T, $N), index : int, value : T) {
// assert(index < array.count);
// array.array[index] = value;
// }
operator *[] :: (array : *Static_Array($T, $N), index : int) -> *T {
assert(index < array.count);
return *(array.array[index]);
operator *[] :: (sa : *Static_Array, index : int) -> *sa.T {
assert(index < sa.count);
return *sa.array[index];
}
array_add :: (array : *Static_Array($T, $N), item : T) {
assert(array.count + 1 < array.capacity);
array_add :: (sa : *Static_Array, item : sa.T) {
assert(sa.count + 1 < sa.N);
print("%\n", array[array.count]);
array[array.count] = item;
array.count += 1;
sa.array[sa.count] = item;
sa.count += 1;
}
array_add :: (sa : *Static_Array) -> *sa.T {
assert(sa.count + 1 < sa.N);
ptr := *sa.array[sa.count];
sa.count += 1;
return ptr;
}
pop :: (sa : *Static_Array) -> sa.T {
assert(sa.count > 0);
elem := sa.array[sa.count - 1];
sa.count -= 1;
return elem;
}
clear :: (sa : *Static_Array) {
sa.count = 0;
}
to_array :: (sa : *Static_Array) -> []sa.T {
array : []sa.T;
array.count = sa.count;
array.data = sa.array.data;
return array;
}

View File

@@ -0,0 +1,107 @@
{kind = TOKEN_IDENTIFIER; ; index = 0 ; length = 1 line = 1 ; column = 0 ; value ='p'; }
{kind = TOKEN_DOUBLECOLON; ; index = 2 ; length = 2 line = 1 ; column = 2 ; value ='::'; }
{kind = TOKEN_PROPERTIES; ; index = 5 ; length = 10 line = 1 ; column = 5 ; value ='properties'; }
{kind = TOKEN_DOUBLECOLON; ; index = 16 ; length = 2 line = 1 ; column = 16 ; value ='::'; }
{kind = TOKEN_LEFTBRACE; ; index = 19 ; length = 1 line = 1 ; column = 19 ; value ='{'; }
{kind = TOKEN_IDENTIFIER; ; index = 23 ; length = 7 line = 2 ; column = 0 ; value ='texture'; }
{kind = TOKEN_COLON; ; index = 31 ; length = 1 line = 2 ; column = 8 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 33 ; length = 9 line = 2 ; column = 10 ; value ='texture2D'; }
{kind = TOKEN_SEMICOLON; ; index = 42 ; length = 1 line = 2 ; column = 19 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 46 ; length = 7 line = 3 ; column = 0 ; value ='sampler'; }
{kind = TOKEN_COLON; ; index = 54 ; length = 1 line = 3 ; column = 8 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 56 ; length = 7 line = 3 ; column = 10 ; value ='sampler'; }
{kind = TOKEN_SEMICOLON; ; index = 63 ; length = 1 line = 3 ; column = 17 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 66 ; length = 1 line = 4 ; column = 0 ; value ='}'; }
{kind = TOKEN_IDENTIFIER; ; index = 71 ; length = 8 line = 6 ; column = 0 ; value ='PS_Input'; }
{kind = TOKEN_DOUBLECOLON; ; index = 80 ; length = 2 line = 6 ; column = 9 ; value ='::'; }
{kind = TOKEN_STRUCT; ; index = 83 ; length = 6 line = 6 ; column = 12 ; value ='struct'; }
{kind = TOKEN_LEFTBRACE; ; index = 90 ; length = 1 line = 6 ; column = 19 ; value ='{'; }
{kind = TOKEN_IDENTIFIER; ; index = 94 ; length = 2 line = 7 ; column = 0 ; value ='uv'; }
{kind = TOKEN_COLON; ; index = 98 ; length = 1 line = 7 ; column = 4 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 100 ; length = 6 line = 7 ; column = 6 ; value ='float2'; }
{kind = TOKEN_AT; ; index = 107 ; length = 1 line = 7 ; column = 13 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 108 ; length = 2 line = 7 ; column = 14 ; value ='uv'; }
{kind = TOKEN_SEMICOLON; ; index = 110 ; length = 1 line = 7 ; column = 16 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 114 ; length = 3 line = 8 ; column = 0 ; value ='pos'; }
{kind = TOKEN_COLON; ; index = 118 ; length = 1 line = 8 ; column = 4 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 120 ; length = 6 line = 8 ; column = 6 ; value ='float4'; }
{kind = TOKEN_AT; ; index = 127 ; length = 1 line = 8 ; column = 13 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 128 ; length = 8 line = 8 ; column = 14 ; value ='position'; }
{kind = TOKEN_SEMICOLON; ; index = 136 ; length = 1 line = 8 ; column = 22 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 139 ; length = 1 line = 9 ; column = 0 ; value ='}'; }
{kind = TOKEN_VERTEX; ; index = 144 ; length = 6 line = 11 ; column = 0 ; value ='vertex'; }
{kind = TOKEN_IDENTIFIER; ; index = 151 ; length = 4 line = 11 ; column = 7 ; value ='main'; }
{kind = TOKEN_DOUBLECOLON; ; index = 156 ; length = 2 line = 11 ; column = 12 ; value ='::'; }
{kind = TOKEN_LEFTPAREN; ; index = 159 ; length = 1 line = 11 ; column = 15 ; value ='('; }
{kind = TOKEN_IDENTIFIER; ; index = 160 ; length = 3 line = 11 ; column = 16 ; value ='pos'; }
{kind = TOKEN_COLON; ; index = 164 ; length = 1 line = 11 ; column = 20 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 166 ; length = 6 line = 11 ; column = 22 ; value ='float4'; }
{kind = TOKEN_AT; ; index = 173 ; length = 1 line = 11 ; column = 29 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 174 ; length = 8 line = 11 ; column = 30 ; value ='position'; }
{kind = TOKEN_COMMA; ; index = 182 ; length = 1 line = 11 ; column = 38 ; value =','; }
{kind = TOKEN_IDENTIFIER; ; index = 184 ; length = 2 line = 11 ; column = 40 ; value ='uv'; }
{kind = TOKEN_COLON; ; index = 187 ; length = 1 line = 11 ; column = 43 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 189 ; length = 6 line = 11 ; column = 45 ; value ='float2'; }
{kind = TOKEN_AT; ; index = 196 ; length = 1 line = 11 ; column = 52 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 197 ; length = 2 line = 11 ; column = 53 ; value ='uv'; }
{kind = TOKEN_RIGHTPAREN; ; index = 199 ; length = 1 line = 11 ; column = 55 ; value =')'; }
{kind = TOKEN_ARROW; ; index = 201 ; length = 2 line = 11 ; column = 57 ; value ='->'; }
{kind = TOKEN_IDENTIFIER; ; index = 204 ; length = 8 line = 11 ; column = 60 ; value ='PS_Input'; }
{kind = TOKEN_LEFTBRACE; ; index = 213 ; length = 1 line = 11 ; column = 69 ; value ='{'; }
{kind = TOKEN_IDENTIFIER; ; index = 217 ; length = 6 line = 12 ; column = 0 ; value ='result'; }
{kind = TOKEN_COLON; ; index = 224 ; length = 1 line = 12 ; column = 7 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 226 ; length = 8 line = 12 ; column = 9 ; value ='PS_Input'; }
{kind = TOKEN_SEMICOLON; ; index = 234 ; length = 1 line = 12 ; column = 17 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 240 ; length = 6 line = 14 ; column = 0 ; value ='result'; }
{kind = TOKEN_DOT; ; index = 246 ; length = 1 line = 14 ; column = 6 ; value ='.'; }
{kind = TOKEN_IDENTIFIER; ; index = 247 ; length = 2 line = 14 ; column = 7 ; value ='uv'; }
{kind = TOKEN_ASSIGN; ; index = 250 ; length = 1 line = 14 ; column = 10 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 252 ; length = 2 line = 14 ; column = 12 ; value ='uv'; }
{kind = TOKEN_SEMICOLON; ; index = 254 ; length = 1 line = 14 ; column = 14 ; value =';'; }
{kind = TOKEN_IDENTIFIER; ; index = 258 ; length = 6 line = 15 ; column = 0 ; value ='result'; }
{kind = TOKEN_DOT; ; index = 264 ; length = 1 line = 15 ; column = 6 ; value ='.'; }
{kind = TOKEN_IDENTIFIER; ; index = 265 ; length = 3 line = 15 ; column = 7 ; value ='pos'; }
{kind = TOKEN_ASSIGN; ; index = 269 ; length = 1 line = 15 ; column = 11 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 271 ; length = 3 line = 15 ; column = 13 ; value ='pos'; }
{kind = TOKEN_SEMICOLON; ; index = 274 ; length = 1 line = 15 ; column = 16 ; value =';'; }
{kind = TOKEN_RETURN; ; index = 280 ; length = 6 line = 17 ; column = 0 ; value ='return'; }
{kind = TOKEN_IDENTIFIER; ; index = 287 ; length = 6 line = 17 ; column = 7 ; value ='result'; }
{kind = TOKEN_SEMICOLON; ; index = 293 ; length = 1 line = 17 ; column = 13 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 296 ; length = 1 line = 18 ; column = 0 ; value ='}'; }
{kind = TOKEN_PIXEL; ; index = 301 ; length = 5 line = 20 ; column = 0 ; value ='pixel'; }
{kind = TOKEN_IDENTIFIER; ; index = 307 ; length = 4 line = 20 ; column = 6 ; value ='main'; }
{kind = TOKEN_DOUBLECOLON; ; index = 312 ; length = 2 line = 20 ; column = 11 ; value ='::'; }
{kind = TOKEN_LEFTPAREN; ; index = 315 ; length = 1 line = 20 ; column = 14 ; value ='('; }
{kind = TOKEN_IDENTIFIER; ; index = 316 ; length = 5 line = 20 ; column = 15 ; value ='input'; }
{kind = TOKEN_COLON; ; index = 322 ; length = 1 line = 20 ; column = 21 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 324 ; length = 8 line = 20 ; column = 23 ; value ='PS_Input'; }
{kind = TOKEN_RIGHTPAREN; ; index = 332 ; length = 1 line = 20 ; column = 31 ; value =')'; }
{kind = TOKEN_ARROW; ; index = 334 ; length = 2 line = 20 ; column = 33 ; value ='->'; }
{kind = TOKEN_IDENTIFIER; ; index = 337 ; length = 6 line = 20 ; column = 36 ; value ='float4'; }
{kind = TOKEN_AT; ; index = 344 ; length = 1 line = 20 ; column = 43 ; value ='@'; }
{kind = TOKEN_IDENTIFIER; ; index = 345 ; length = 6 line = 20 ; column = 44 ; value ='target'; }
{kind = TOKEN_LEFTBRACE; ; index = 352 ; length = 1 line = 20 ; column = 51 ; value ='{'; }
{kind = TOKEN_IDENTIFIER; ; index = 356 ; length = 5 line = 21 ; column = 0 ; value ='color'; }
{kind = TOKEN_COLON; ; index = 362 ; length = 1 line = 21 ; column = 6 ; value =':'; }
{kind = TOKEN_IDENTIFIER; ; index = 364 ; length = 6 line = 21 ; column = 8 ; value ='float4'; }
{kind = TOKEN_ASSIGN; ; index = 371 ; length = 1 line = 21 ; column = 15 ; value ='='; }
{kind = TOKEN_IDENTIFIER; ; index = 373 ; length = 6 line = 21 ; column = 17 ; value ='sample'; }
{kind = TOKEN_LEFTPAREN; ; index = 379 ; length = 1 line = 21 ; column = 23 ; value ='('; }
{kind = TOKEN_IDENTIFIER; ; index = 380 ; length = 1 line = 21 ; column = 24 ; value ='p'; }
{kind = TOKEN_DOT; ; index = 381 ; length = 1 line = 21 ; column = 25 ; value ='.'; }
{kind = TOKEN_IDENTIFIER; ; index = 382 ; length = 7 line = 21 ; column = 26 ; value ='texture'; }
{kind = TOKEN_COMMA; ; index = 389 ; length = 1 line = 21 ; column = 33 ; value =','; }
{kind = TOKEN_IDENTIFIER; ; index = 391 ; length = 5 line = 21 ; column = 35 ; value ='input'; }
{kind = TOKEN_DOT; ; index = 396 ; length = 1 line = 21 ; column = 40 ; value ='.'; }
{kind = TOKEN_IDENTIFIER; ; index = 397 ; length = 2 line = 21 ; column = 41 ; value ='uv'; }
{kind = TOKEN_COMMA; ; index = 399 ; length = 1 line = 21 ; column = 43 ; value =','; }
{kind = TOKEN_IDENTIFIER; ; index = 401 ; length = 1 line = 21 ; column = 45 ; value ='p'; }
{kind = TOKEN_DOT; ; index = 402 ; length = 1 line = 21 ; column = 46 ; value ='.'; }
{kind = TOKEN_IDENTIFIER; ; index = 403 ; length = 7 line = 21 ; column = 47 ; value ='sampler'; }
{kind = TOKEN_RIGHTPAREN; ; index = 410 ; length = 1 line = 21 ; column = 54 ; value =')'; }
{kind = TOKEN_SEMICOLON; ; index = 411 ; length = 1 line = 21 ; column = 55 ; value =';'; }
{kind = TOKEN_RETURN; ; index = 415 ; length = 6 line = 22 ; column = 0 ; value ='return'; }
{kind = TOKEN_IDENTIFIER; ; index = 422 ; length = 5 line = 22 ; column = 7 ; value ='color'; }
{kind = TOKEN_SEMICOLON; ; index = 427 ; length = 1 line = 22 ; column = 12 ; value =';'; }
{kind = TOKEN_RIGHTBRACE; ; index = 430 ; length = 1 line = 23 ; column = 0 ; value ='}'; }
{kind = TOKEN_EOF; ; index = 433 ; length = 0 line = 24 ; column = 0 ; value =''; }

View File

@@ -0,0 +1,21 @@
(program
(properties p
[(:= texture texture2D)
(:= sampler sampler)])
(struct PS_Input
[(:= uv float2 (@uv))
(:= pos float4 (@position))])
(fun vertex vs_main -> PS_Input
[(:= pos float4 (@position))
(:= uv float2 (@uv))]
(:= result PS_Input)
(= result.uv uv)
(= result.pos pos)
(return result))
(fun pixel ps_main -> float4 (@target)
[(:= input PS_Input)]
(:= color float4 (sample p.texture input.uv p.sampler))
(return color)))

23
test/texture_sample.shd Normal file
View File

@@ -0,0 +1,23 @@
p :: properties {
texture : texture2D;
sampler : sampler;
}
PS_Input :: struct {
uv : float2 @uv;
pos : float4 @position;
}
vertex main :: (pos : float4 @position, uv : float2 @uv) -> PS_Input {
result : PS_Input;
result.uv = uv;
result.pos = pos;
return result;
}
pixel main :: (input : PS_Input) -> float4 @target {
color : float4 = sample(p.texture, input.uv, p.sampler);
return color;
}