diff --git a/Sources/backends/cstyle.c b/Sources/backends/cstyle.c index 08d1d3d..6b7d347 100644 --- a/Sources/backends/cstyle.c +++ b/Sources/backends/cstyle.c @@ -123,6 +123,11 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func *offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %f;\n", type_string(o->op_load_float_constant.to.type.type), o->op_load_float_constant.to.index, o->op_load_float_constant.number); break; + case OPCODE_LOAD_INT_CONSTANT: + indent(code, offset, *indentation); + *offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %i;\n", type_string(o->op_load_int_constant.to.type.type), o->op_load_int_constant.to.index, + o->op_load_int_constant.number); + break; case OPCODE_LOAD_BOOL_CONSTANT: indent(code, offset, *indentation); *offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %s;\n", type_string(o->op_load_bool_constant.to.type.type), o->op_load_bool_constant.to.index, diff --git a/Sources/backends/hlsl.c b/Sources/backends/hlsl.c index 0bf6c7e..dece118 100644 --- a/Sources/backends/hlsl.c +++ b/Sources/backends/hlsl.c @@ -751,7 +751,7 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func break; } case OPCODE_MULTIPLY: { - if (o->op_binary.left.type.type == float4x4_id) { + if (o->op_binary.left.type.type == float4x4_id || o->op_binary.left.type.type == float3x3_id) { indent(hlsl, offset, indentation); *offset += sprintf(&hlsl[*offset], "%s _%" PRIu64 " = mul(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_binary.result.type.type), o->op_binary.result.index, o->op_binary.right.index, o->op_binary.left.index); diff --git a/Sources/compiler.c b/Sources/compiler.c index 39f9fd5..7d2f6df 100644 --- a/Sources/compiler.c +++ b/Sources/compiler.c @@ -175,7 +175,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { case OPERATOR_MULTIPLY: { variable right_var = emit_expression(code, parent, right); variable left_var = emit_expression(code, parent, left); - variable result_var = allocate_variable(right_var.type, VARIABLE_LOCAL); + variable result_var = allocate_variable(e->type, VARIABLE_LOCAL); opcode o; switch (e->binary.op) { @@ -443,7 +443,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { case EXPRESSION_INT: { type_ref t; init_type_ref(&t, NO_NAME); - t.type = float_id; + t.type = int_id; variable v = allocate_variable(t, VARIABLE_LOCAL); opcode o; diff --git a/Sources/functions.c b/Sources/functions.c index ef75a55..d4e759f 100644 --- a/Sources/functions.c +++ b/Sources/functions.c @@ -10,11 +10,6 @@ static function *functions = NULL; static function_id functions_size = 1024; static function_id next_function_index = 0; -function_id sample_id; -function_id float2_constructor_id; -function_id float3_constructor_id; -function_id float4_constructor_id; - static void add_func_int(char *name) { function_id func = add_function(add_name(name)); function *f = get_function(func); @@ -70,6 +65,18 @@ static void add_func_float_float(char *name) { f->block = NULL; } +static void add_func_float_float2(char *name) { + function_id func = add_function(add_name(name)); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("float")); + f->return_type.type = find_type_by_ref(&f->return_type); + f->parameter_names[0] = add_name("a"); + init_type_ref(&f->parameter_types[0], add_name("float2")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + f->parameters_size = 1; + f->block = NULL; +} + static void add_func_float3_float3(char *name) { function_id func = add_function(add_name(name)); function *f = get_function(func); @@ -108,8 +115,8 @@ void functions_init(void) { next_function_index = 0; { - sample_id = add_function(add_name("sample")); - function *f = get_function(sample_id); + function_id func = add_function(add_name("sample")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("float4")); f->return_type.type = find_type_by_ref(&f->return_type); f->parameter_names[0] = add_name("tex_coord"); @@ -120,8 +127,8 @@ void functions_init(void) { } { - sample_id = add_function(add_name("sample_lod")); - function *f = get_function(sample_id); + function_id func = add_function(add_name("sample_lod")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("float4")); f->return_type.type = find_type_by_ref(&f->return_type); f->parameter_names[0] = add_name("tex_coord"); @@ -132,8 +139,22 @@ void functions_init(void) { } { - float2_constructor_id = add_function(add_name("float2")); - function *f = get_function(float2_constructor_id); + function_id func = add_function(add_name("float")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("float")); + f->return_type.type = find_type_by_ref(&f->return_type); + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("float")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameters_size = 1; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("float2")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("float2")); f->return_type.type = find_type_by_ref(&f->return_type); f->parameter_names[0] = add_name("x"); @@ -150,8 +171,8 @@ void functions_init(void) { } { - float3_constructor_id = add_function(add_name("float3")); - function *f = get_function(float3_constructor_id); + function_id func = add_function(add_name("float3")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("float3")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -173,8 +194,8 @@ void functions_init(void) { } { - float4_constructor_id = add_function(add_name("float4")); - function *f = get_function(float4_constructor_id); + function_id func = add_function(add_name("float4")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("float4")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -200,8 +221,23 @@ void functions_init(void) { } { - float2_constructor_id = add_function(add_name("int2")); - function *f = get_function(float2_constructor_id); + function_id func = add_function(add_name("int")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("int")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("int")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameters_size = 1; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("int2")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("int2")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -219,8 +255,8 @@ void functions_init(void) { } { - float3_constructor_id = add_function(add_name("int3")); - function *f = get_function(float3_constructor_id); + function_id func = add_function(add_name("int3")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("int3")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -242,8 +278,8 @@ void functions_init(void) { } { - float4_constructor_id = add_function(add_name("int4")); - function *f = get_function(float4_constructor_id); + function_id func = add_function(add_name("int4")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("int4")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -269,8 +305,23 @@ void functions_init(void) { } { - float2_constructor_id = add_function(add_name("uint2")); - function *f = get_function(float2_constructor_id); + function_id func = add_function(add_name("uint")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("uint")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("uint")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameters_size = 1; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("uint2")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("uint2")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -288,8 +339,8 @@ void functions_init(void) { } { - float3_constructor_id = add_function(add_name("uint3")); - function *f = get_function(float3_constructor_id); + function_id func = add_function(add_name("uint3")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("uint3")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -311,8 +362,8 @@ void functions_init(void) { } { - float4_constructor_id = add_function(add_name("uint4")); - function *f = get_function(float4_constructor_id); + function_id func = add_function(add_name("uint4")); + function *f = get_function(func); init_type_ref(&f->return_type, add_name("uint4")); f->return_type.type = find_type_by_ref(&f->return_type); @@ -337,6 +388,90 @@ void functions_init(void) { f->block = NULL; } + { + function_id func = add_function(add_name("bool")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("bool")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("bool")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameters_size = 1; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("bool2")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("bool2")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("bool")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameter_names[1] = add_name("y"); + init_type_ref(&f->parameter_types[1], add_name("bool")); + f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]); + + f->parameters_size = 2; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("bool3")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("bool3")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("bool")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameter_names[1] = add_name("y"); + init_type_ref(&f->parameter_types[1], add_name("bool")); + f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]); + + f->parameter_names[2] = add_name("z"); + init_type_ref(&f->parameter_types[2], add_name("bool")); + f->parameter_types[2].type = find_type_by_ref(&f->parameter_types[2]); + + f->parameters_size = 3; + + f->block = NULL; + } + + { + function_id func = add_function(add_name("bool4")); + function *f = get_function(func); + init_type_ref(&f->return_type, add_name("bool4")); + f->return_type.type = find_type_by_ref(&f->return_type); + + f->parameter_names[0] = add_name("x"); + init_type_ref(&f->parameter_types[0], add_name("bool")); + f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]); + + f->parameter_names[1] = add_name("y"); + init_type_ref(&f->parameter_types[1], add_name("bool")); + f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]); + + f->parameter_names[2] = add_name("z"); + init_type_ref(&f->parameter_types[2], add_name("bool")); + f->parameter_types[2].type = find_type_by_ref(&f->parameter_types[2]); + + f->parameter_names[3] = add_name("w"); + init_type_ref(&f->parameter_types[3], add_name("bool")); + f->parameter_types[3].type = find_type_by_ref(&f->parameter_types[3]); + + f->parameters_size = 4; + + f->block = NULL; + } + { function_id func = add_function(add_name("trace_ray")); function *f = get_function(func); @@ -401,6 +536,9 @@ void functions_init(void) { add_func_float3("world_ray_direction"); add_func_float3_float3("normalize"); add_func_float_float("saturate"); + add_func_float_float("sin"); + add_func_float_float("cos"); + add_func_float_float2("length"); add_func_uint3("ray_index"); add_func_float3("ray_dimensions"); diff --git a/Sources/functions.h b/Sources/functions.h index 8e24c6f..1b48485 100644 --- a/Sources/functions.h +++ b/Sources/functions.h @@ -29,5 +29,3 @@ function_id add_function(name_id name); function_id find_function(name_id name); function *get_function(function_id function); - -extern function_id sample_id; diff --git a/Sources/integrations/kope.c b/Sources/integrations/kope.c index 60ccb1c..c6006cf 100644 --- a/Sources/integrations/kope.c +++ b/Sources/integrations/kope.c @@ -67,9 +67,12 @@ static uint32_t base_type_size(type_id type) { if (type == float4x4_id) { return 4 * 4 * 4; } + if (type == float3x3_id) { + return 3 * 4 * 4; + } debug_context context = {0}; - error(context, "Unknown type for structure"); + error(context, "Unknown type %s for structure", get_name(get_type(type)->name)); return 1; } diff --git a/Sources/kong.c b/Sources/kong.c index 52ab154..0b289a2 100644 --- a/Sources/kong.c +++ b/Sources/kong.c @@ -445,6 +445,7 @@ void resolve_types_in_expression(statement *parent, expression *e) { if (e->type.type == NO_TYPE) { debug_context context = {0}; + const char *n = get_name(e->call.func_name); error(context, "Could not resolve type"); } }