Skip to content

Commit

Permalink
Support const floats
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 24, 2024
1 parent 4556842 commit 5f2220d
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 40 deletions.
13 changes: 12 additions & 1 deletion Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function *
*offset += sprintf(&hlsl[*offset], "TextureCube<float4> _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index);
}
else if (g.type == float_id) {
*offset += sprintf(&hlsl[*offset], "static const float _%" PRIu64 " = %f;\n\n", g.var_index, g.value.value.floats[0]);
}
else if (g.type == float2_id) {
*offset += sprintf(&hlsl[*offset], "static const float2 _%" PRIu64 " = float2(%f, %f);\n\n", g.var_index, g.value.value.floats[0], g.value.value.floats[1]);
}
else if (g.type == float3_id) {
*offset += sprintf(&hlsl[*offset], "static const float3 _%" PRIu64 " = float3(%f, %f, %f);\n\n", g.var_index, g.value.value.floats[0],
g.value.value.floats[1], g.value.value.floats[2]);
}
else if (g.type == float4_id) {
*offset += sprintf(&hlsl[*offset], "static const float4 _%" PRIu64 " = float4(%f, %f, %f, %f);\n\n", g.var_index, g.value.value.floats[0],
g.value.value.floats[1], g.value.value.floats[2], g.value.value.floats[3]);
}
else {
*offset += sprintf(&hlsl[*offset], "cbuffer _%" PRIu64 " : register(b%i) {\n", g.var_index, register_index);
Expand Down Expand Up @@ -468,7 +480,6 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
}
else if (parameter_index == 1) {
*offset += sprintf(&hlsl[*offset], ", BuiltInTriangleIntersectionAttributes _kong_triangle_intersection_attributes");
//type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
else {
*offset += sprintf(&hlsl[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
Expand Down
2 changes: 1 addition & 1 deletion Sources/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) {
case EXPRESSION_CALL: {
type_ref t;
init_type_ref(&t, NO_NAME);
t.type = float4_id;
t.type = e->type.type;
variable v = allocate_variable(t, VARIABLE_LOCAL);

opcode o;
Expand Down
55 changes: 55 additions & 0 deletions Sources/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,52 @@ 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);
init_type_ref(&f->return_type, add_name("int"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameters_size = 0;
f->block = NULL;
}

static void add_func_float3_float_float_float(char *name) {
function_id func = add_function(add_name(name));
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);
f->parameter_names[0] = add_name("a");
f->parameter_names[1] = add_name("b");
f->parameter_names[2] = add_name("c");
for (int i = 0; i < 3; ++i) {
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 = 3;
f->block = NULL;
}

static void add_func_float3(char *name) {
function_id func = add_function(add_name(name));
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);
f->parameters_size = 0;
f->block = NULL;
}

static void add_func_float3_float3(char *name) {
function_id func = add_function(add_name(name));
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);
f->parameter_names[0] = add_name("a");
init_type_ref(&f->parameter_types[0], add_name("float3"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
f->parameters_size = 1;
f->block = NULL;
}

void functions_init(void) {
function *new_functions = realloc(functions, functions_size * sizeof(function));
debug_context context = {0};
Expand Down Expand Up @@ -81,6 +127,15 @@ void functions_init(void) {
f->parameters_size = 1;
f->block = NULL;
}

add_func_int("group_id");
add_func_int("group_thread_id");
add_func_int("dispatch_thread_id");
add_func_int("group_index");

add_func_float3_float_float_float("lerp");
add_func_float3("world_ray_direction");
add_func_float3_float3("normalize");
}

static void grow_if_needed(uint64_t size) {
Expand Down
82 changes: 55 additions & 27 deletions Sources/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,74 @@ static global globals[1024];
static global_id globals_size = 0;

void globals_init(void) {
add_global_with_value(float_id, add_name("COMPARE_ALWAYS"), 0);
add_global_with_value(float_id, add_name("COMPARE_NEVER"), 1);
add_global_with_value(float_id, add_name("COMPARE_EQUAL"), 2);
add_global_with_value(float_id, add_name("COMPARE_NOT_EQUAL"), 3);
add_global_with_value(float_id, add_name("COMPARE_LESS"), 4);
add_global_with_value(float_id, add_name("COMPARE_LESS_EQUAL"), 5);
add_global_with_value(float_id, add_name("COMPARE_GREATER"), 6);
add_global_with_value(float_id, add_name("COMPARE_GREATER_EQUAL"), 7);
global_value int_value;
int_value.kind = GLOBAL_VALUE_INT;

int_value.value.ints[0] = 0;
add_global_with_value(float_id, add_name("COMPARE_ALWAYS"), int_value);
int_value.value.ints[0] = 1;
add_global_with_value(float_id, add_name("COMPARE_NEVER"), int_value);
int_value.value.ints[0] = 2;
add_global_with_value(float_id, add_name("COMPARE_EQUAL"), int_value);
int_value.value.ints[0] = 3;
add_global_with_value(float_id, add_name("COMPARE_NOT_EQUAL"), int_value);
int_value.value.ints[0] = 4;
add_global_with_value(float_id, add_name("COMPARE_LESS"), int_value);
int_value.value.ints[0] = 5;
add_global_with_value(float_id, add_name("COMPARE_LESS_EQUAL"), int_value);
int_value.value.ints[0] = 6;
add_global_with_value(float_id, add_name("COMPARE_GREATER"), int_value);
int_value.value.ints[0] = 7;
add_global_with_value(float_id, add_name("COMPARE_GREATER_EQUAL"), int_value);

add_global_with_value(float_id, add_name("BLEND_ONE"), 0);
add_global_with_value(float_id, add_name("BLEND_ZERO"), 1);
add_global_with_value(float_id, add_name("BLEND_SOURCE_ALPHA"), 2);
add_global_with_value(float_id, add_name("BLEND_DEST_ALPHA"), 3);
add_global_with_value(float_id, add_name("BLEND_INV_SOURCE_ALPHA"), 4);
add_global_with_value(float_id, add_name("BLEND_INV_DEST_ALPHA"), 5);
add_global_with_value(float_id, add_name("BLEND_SOURCE_COLOR"), 6);
add_global_with_value(float_id, add_name("BLEND_DEST_COLOR"), 7);
add_global_with_value(float_id, add_name("BLEND_INV_SOURCE_COLOR"), 8);
add_global_with_value(float_id, add_name("BLEND_INV_DEST_COLOR"), 9);
add_global_with_value(float_id, add_name("BLEND_CONSTANT"), 10);
add_global_with_value(float_id, add_name("BLEND_INV_CONSTANT"), 11);
int_value.value.ints[0] = 0;
add_global_with_value(float_id, add_name("BLEND_ONE"), int_value);
int_value.value.ints[0] = 1;
add_global_with_value(float_id, add_name("BLEND_ZERO"), int_value);
int_value.value.ints[0] = 2;
add_global_with_value(float_id, add_name("BLEND_SOURCE_ALPHA"), int_value);
int_value.value.ints[0] = 3;
add_global_with_value(float_id, add_name("BLEND_DEST_ALPHA"), int_value);
int_value.value.ints[0] = 4;
add_global_with_value(float_id, add_name("BLEND_INV_SOURCE_ALPHA"), int_value);
int_value.value.ints[0] = 5;
add_global_with_value(float_id, add_name("BLEND_INV_DEST_ALPHA"), int_value);
int_value.value.ints[0] = 6;
add_global_with_value(float_id, add_name("BLEND_SOURCE_COLOR"), int_value);
int_value.value.ints[0] = 7;
add_global_with_value(float_id, add_name("BLEND_DEST_COLOR"), int_value);
int_value.value.ints[0] = 8;
add_global_with_value(float_id, add_name("BLEND_INV_SOURCE_COLOR"), int_value);
int_value.value.ints[0] = 9;
add_global_with_value(float_id, add_name("BLEND_INV_DEST_COLOR"), int_value);
int_value.value.ints[0] = 10;
add_global_with_value(float_id, add_name("BLEND_CONSTANT"), int_value);
int_value.value.ints[0] = 11;
add_global_with_value(float_id, add_name("BLEND_INV_CONSTANT"), int_value);

add_global_with_value(float_id, add_name("BLENDOP_ADD"), 0);
add_global_with_value(float_id, add_name("BLENDOP_SUBTRACT"), 1);
add_global_with_value(float_id, add_name("BLENDOP_REVERSE_SUBTRACT"), 2);
add_global_with_value(float_id, add_name("BLENDOP_MIN"), 3);
add_global_with_value(float_id, add_name("BLENDOP_MAX"), 4);
int_value.value.ints[0] = 0;
add_global_with_value(float_id, add_name("BLENDOP_ADD"), int_value);
int_value.value.ints[0] = 1;
add_global_with_value(float_id, add_name("BLENDOP_SUBTRACT"), int_value);
int_value.value.ints[0] = 2;
add_global_with_value(float_id, add_name("BLENDOP_REVERSE_SUBTRACT"), int_value);
int_value.value.ints[0] = 3;
add_global_with_value(float_id, add_name("BLENDOP_MIN"), int_value);
int_value.value.ints[0] = 4;
add_global_with_value(float_id, add_name("BLENDOP_MAX"), int_value);
}

global_id add_global(type_id type, name_id name) {
uint32_t index = globals_size;
globals[index].name = name;
globals[index].type = type;
globals[index].var_index = 0;
globals[index].value = 0;
globals[index].value.kind = GLOBAL_VALUE_NONE;
globals_size += 1;
return index;
}

global_id add_global_with_value(type_id type, name_id name, float value) {
global_id add_global_with_value(type_id type, name_id name, global_value value) {
uint32_t index = globals_size;
globals[index].name = name;
globals[index].type = type;
Expand Down
24 changes: 22 additions & 2 deletions Sources/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@

typedef uint32_t global_id;

typedef struct global_value {
enum {
GLOBAL_VALUE_FLOAT,
GLOBAL_VALUE_FLOAT2,
GLOBAL_VALUE_FLOAT3,
GLOBAL_VALUE_FLOAT4,
GLOBAL_VALUE_INT,
GLOBAL_VALUE_INT2,
GLOBAL_VALUE_INT3,
GLOBAL_VALUE_INT4,
GLOBAL_VALUE_BOOL,
GLOBAL_VALUE_NONE
} kind;
union {
float floats[4];
int ints[4];
bool b;
} value;
} global_value;

typedef struct global {
name_id name;
type_id type;
uint64_t var_index;
float value;
global_value value;
} global;

void globals_init(void);

global_id add_global(type_id type, name_id name);
global_id add_global_with_value(type_id type, name_id name, float value);
global_id add_global_with_value(type_id type, name_id name, global_value value);

global find_global(name_id name);

Expand Down
14 changes: 7 additions & 7 deletions Sources/integrations/kinc.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,43 +610,43 @@ void kinc_export(char *directory, api_kind api) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "depth_mode expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.depth_mode = %s;\n\n", get_name(t->name), convert_compare_mode((int)g.value));
fprintf(output, "\t%s.depth_mode = %s;\n\n", get_name(t->name), convert_compare_mode(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("blend_source")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "blend_source expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.blend_source = %s;\n\n", get_name(t->name), convert_blend_mode((int)g.value));
fprintf(output, "\t%s.blend_source = %s;\n\n", get_name(t->name), convert_blend_mode(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("blend_destination")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "blend_destination expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.blend_destination = %s;\n\n", get_name(t->name), convert_blend_mode((int)g.value));
fprintf(output, "\t%s.blend_destination = %s;\n\n", get_name(t->name), convert_blend_mode(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("blend_operation")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "blend_operation expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.blend_operation = %s;\n\n", get_name(t->name), convert_blend_op((int)g.value));
fprintf(output, "\t%s.blend_operation = %s;\n\n", get_name(t->name), convert_blend_op(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("alpha_blend_source")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "alpha_blend_source expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.alpha_blend_source = %s;\n\n", get_name(t->name), convert_blend_mode((int)g.value));
fprintf(output, "\t%s.alpha_blend_source = %s;\n\n", get_name(t->name), convert_blend_mode(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("alpha_blend_destination")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "alpha_blend_destination expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.alpha_blend_destination = %s;\n\n", get_name(t->name), convert_blend_mode((int)g.value));
fprintf(output, "\t%s.alpha_blend_destination = %s;\n\n", get_name(t->name), convert_blend_mode(g.value.value.ints[0]));
}
else if (t->members.m[j].name == add_name("alpha_blend_operation")) {
debug_context context = {0};
check(t->members.m[j].value.kind == TOKEN_IDENTIFIER, context, "alpha_blend_operation expects an identifier");
global g = find_global(t->members.m[j].value.identifier);
fprintf(output, "\t%s.alpha_blend_operation = %s;\n\n", get_name(t->name), convert_blend_op((int)g.value));
fprintf(output, "\t%s.alpha_blend_operation = %s;\n\n", get_name(t->name), convert_blend_op(g.value.value.ints[0]));
}
else {
debug_context context = {0};
Expand Down
6 changes: 6 additions & 0 deletions Sources/kong.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type_ref resolve_member_var_type(statement *parent_block, type_ref parent_type,
return left->type;
}
}
else if (left->kind == EXPRESSION_CALL) {
if (parent_block != NULL) {
resolve_types_in_expression(parent_block, left);
return left->type;
}
}
else if (left->kind == EXPRESSION_INDEX) {
if (parent_type.type != NO_TYPE) {
init_type_ref(&left->type, NO_NAME);
Expand Down
Loading

0 comments on commit 5f2220d

Please sign in to comment.