Skip to content

Commit

Permalink
Support more function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 22, 2024
1 parent 9cfb85a commit 85401b9
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 120 deletions.
35 changes: 25 additions & 10 deletions Sources/backends/glsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,20 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
uint8_t *data = f->code.o;
size_t size = f->code.size;

uint64_t parameter_id = 0;
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_name == f->block->block.vars.v[i].name) {
parameter_id = f->block->block.vars.v[i].variable_id;
break;
uint64_t parameter_ids[256] = {0};
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_names[parameter_index] == f->block->block.vars.v[i].name) {
parameter_ids[parameter_index] = f->block->block.vars.v[i].variable_id;
break;
}
}
}

check(parameter_id != 0, context, "Parameter not found");
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
check(parameter_ids[parameter_index] != 0, context, "Parameter not found");
}

if (f == main) {
if (stage == SHADER_STAGE_VERTEX) {
*offset += sprintf(&code[*offset], "void main() {\n");
Expand All @@ -289,8 +294,16 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
}
}
else {
*offset += sprintf(&code[*offset], "%s %s(%s _%" PRIu64 ") {\n", type_string(f->return_type.type), get_name(f->name),
type_string(f->parameter_type.type), parameter_id);
*offset += sprintf(&code[*offset], "%s %s(", type_string(f->return_type.type), get_name(f->name));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset += sprintf(&code[*offset], "%s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
else {
*offset += sprintf(&code[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
}
*offset += sprintf(&code[*offset], ") {\n");
}

int indentation = 1;
Expand Down Expand Up @@ -456,7 +469,8 @@ static void glsl_export_vertex(char *directory, function *main) {

size_t offset = 0;

type_id vertex_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id vertex_input = main->parameter_types[0].type;
type_id vertex_output = main->return_type.type;

check(vertex_input != NO_TYPE, context, "vertex input missing");
Expand Down Expand Up @@ -488,7 +502,8 @@ static void glsl_export_fragment(char *directory, function *main) {

size_t offset = 0;

type_id pixel_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id pixel_input = main->parameter_types[0].type;

check(pixel_input != NO_TYPE, context, "fragment input missing");

Expand Down
77 changes: 62 additions & 15 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,34 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
uint8_t *data = f->code.o;
size_t size = f->code.size;

uint64_t parameter_id = 0;
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_name == f->block->block.vars.v[i].name) {
parameter_id = f->block->block.vars.v[i].variable_id;
break;
uint64_t parameter_ids[256] = {0};
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_names[parameter_index] == f->block->block.vars.v[i].name) {
parameter_ids[parameter_index] = f->block->block.vars.v[i].variable_id;
break;
}
}
}

check(parameter_id != 0, context, "Parameter not found");
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
check(parameter_ids[parameter_index] != 0, context, "Parameter not found");
}

if (f == main) {
if (stage == SHADER_STAGE_VERTEX) {
*offset += sprintf(&hlsl[*offset], "%s main(%s _%" PRIu64 ") {\n", type_string(f->return_type.type), type_string(f->parameter_type.type),
parameter_id);
*offset += sprintf(&hlsl[*offset], "%s main(", type_string(f->return_type.type));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset += sprintf(&hlsl[*offset], "%s _%" PRIu64 ") {\n", type_string(f->parameter_types[parameter_index].type),
parameter_ids[parameter_index]);
}
else {
*offset += sprintf(&hlsl[*offset], ", %s _%" PRIu64 ") {\n", type_string(f->parameter_types[parameter_index].type),
parameter_ids[parameter_index]);
}
}
*offset += sprintf(&hlsl[*offset], ") {\n");
}
else if (stage == SHADER_STAGE_FRAGMENT) {
if (f->return_type.array_size > 0) {
Expand All @@ -300,11 +315,33 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
*offset += sprintf(&hlsl[*offset], "\t%s _%i : SV_Target%i;\n", type_string(f->return_type.type), j, j);
}
*offset += sprintf(&hlsl[*offset], "};\n\n");
*offset += sprintf(&hlsl[*offset], "_kong_colors_out main(%s _%" PRIu64 ") {\n", type_string(f->parameter_type.type), parameter_id);

*offset += sprintf(&hlsl[*offset], "_kong_colors_out main(");
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset +=
sprintf(&hlsl[*offset], "%s _%" PRIu64, 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]);
}
}
*offset += sprintf(&hlsl[*offset], ") {\n");
}
else {
*offset += sprintf(&hlsl[*offset], "%s main(%s _%" PRIu64 ") : SV_Target0 {\n", type_string(f->return_type.type),
type_string(f->parameter_type.type), parameter_id);
*offset += sprintf(&hlsl[*offset], "%s main(", type_string(f->return_type.type));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset +=
sprintf(&hlsl[*offset], "%s _%" PRIu64, 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]);
}
}
*offset += sprintf(&hlsl[*offset], ") : SV_Target0 {\n");
}
}
else {
Expand All @@ -313,8 +350,16 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
}
}
else {
*offset += sprintf(&hlsl[*offset], "%s %s(%s _%" PRIu64 ") {\n", type_string(f->return_type.type), get_name(f->name),
type_string(f->parameter_type.type), parameter_id);
*offset += sprintf(&hlsl[*offset], "%s %s(", type_string(f->return_type.type), get_name(f->name));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset += sprintf(&hlsl[*offset], "%s _%" PRIu64, 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]);
}
}
*offset += sprintf(&hlsl[*offset], ") {\n");
}

int indentation = 1;
Expand Down Expand Up @@ -404,7 +449,8 @@ static void hlsl_export_vertex(char *directory, api_kind d3d, function *main) {
char *hlsl = (char *)calloc(1024 * 1024, 1);
size_t offset = 0;

type_id vertex_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id vertex_input = main->parameter_types[0].type;
type_id vertex_output = main->return_type.type;

debug_context context = {0};
Expand Down Expand Up @@ -450,7 +496,8 @@ static void hlsl_export_fragment(char *directory, api_kind d3d, function *main)
char *hlsl = (char *)calloc(1024 * 1024, 1);
size_t offset = 0;

type_id pixel_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id pixel_input = main->parameter_types[0].type;

debug_context context = {0};
check(pixel_input != NO_TYPE, context, "fragment input missing");
Expand Down
59 changes: 43 additions & 16 deletions Sources/backends/metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,20 @@ static void write_functions(char *code, size_t *offset) {
uint8_t *data = f->code.o;
size_t size = f->code.size;

uint64_t parameter_id = 0;
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_name == f->block->block.vars.v[i].name) {
parameter_id = f->block->block.vars.v[i].variable_id;
break;
uint64_t parameter_ids[256] = {0};
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_names[parameter_index] == f->block->block.vars.v[i].name) {
parameter_ids[parameter_index] = f->block->block.vars.v[i].variable_id;
break;
}
}
}

debug_context context = {0};
check(parameter_id != 0, context, "Parameter not found");
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
check(parameter_ids[parameter_index] != 0, context, "Parameter not found");
}

char buffers[1024];
strcpy(buffers, "");
Expand Down Expand Up @@ -278,8 +282,12 @@ static void write_functions(char *code, size_t *offset) {
}

if (is_vertex_function(i)) {
*offset += sprintf(&code[*offset], "vertex %s %s(%s _%" PRIu64 " [[stage_in]]%s) {\n", type_string(f->return_type.type), get_name(f->name),
type_string(f->parameter_type.type), parameter_id, buffers);
*offset += sprintf(&code[*offset], "vertex %s %s(%s _%" PRIu64 " [[stage_in]]) {\n", type_string(f->return_type.type), get_name(f->name),
type_string(f->parameter_types[0].type), parameter_ids[0]);
for (uint8_t parameter_index = 1; parameter_index < f->parameters_size; ++parameter_index) {
*offset += sprintf(&code[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[0].type), parameter_ids[0]);
}
*offset += sprintf(&code[*offset], "%s) {\n", buffers);
}
else if (is_fragment_function(i)) {
if (f->return_type.array_size > 0) {
Expand All @@ -288,18 +296,35 @@ static void write_functions(char *code, size_t *offset) {
*offset += sprintf(&code[*offset], "\t%s _%i [[color(%i)]];\n", type_string(f->return_type.type), j, j);
}
*offset += sprintf(&code[*offset], "};\n\n");
*offset += sprintf(&code[*offset], "fragment _kong_colors_out %s(%s _%" PRIu64 " [[stage_in]]%s) {\n", get_name(f->name),
type_string(f->parameter_type.type), parameter_id, buffers);

*offset += sprintf(&code[*offset], "fragment _kong_colors_out %s(%s _%" PRIu64 " [[stage_in]]", get_name(f->name),
type_string(f->parameter_types[0].type), parameter_ids[0]);
for (uint8_t parameter_index = 1; parameter_index < f->parameters_size; ++parameter_index) {
*offset += sprintf(&code[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
*offset += sprintf(&code[*offset], "%s) {\n", buffers);
}
else {
*offset += sprintf(&code[*offset], "fragment _kong_color_out %s(%s _%" PRIu64 " [[stage_in]]%s) {\n", get_name(f->name),
type_string(f->parameter_type.type), parameter_id, buffers);
*offset += sprintf(&code[*offset], "fragment _kong_color_out %s(%s _%" PRIu64 " [[stage_in]]) {\n", get_name(f->name),
type_string(f->parameter_types[0].type), parameter_ids[0]);
for (uint8_t parameter_index = 1; parameter_index < f->parameters_size; ++parameter_index) {
*offset += sprintf(&code[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
*offset += sprintf(&code[*offset], "%s) {\n", buffers);
}
}

else {
*offset += sprintf(&code[*offset], "%s %s(%s _%" PRIu64 ") {\n", type_string(f->return_type.type), get_name(f->name),
type_string(f->parameter_type.type), parameter_id);
*offset += sprintf(&code[*offset], "%s %s(", type_string(f->return_type.type), get_name(f->name));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset += sprintf(&code[*offset], "%s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
else {
*offset += sprintf(&code[*offset], ", %s _%" PRIu64, type_string(f->parameter_types[parameter_index].type), parameter_ids[parameter_index]);
}
}
*offset += sprintf(&code[*offset], ") {\n");
}

int indentation = 1;
Expand Down Expand Up @@ -481,14 +506,16 @@ void metal_export(char *directory) {
vertex_functions[vertex_functions_size] = i;
vertex_functions_size += 1;

vertex_inputs[vertex_inputs_size] = f->parameter_type.type;
assert(f->parameters_size > 0);
vertex_inputs[vertex_inputs_size] = f->parameter_types[0].type;
vertex_inputs_size += 1;
}
else if (f->name == fragment_shader_name) {
fragment_functions[fragment_functions_size] = i;
fragment_functions_size += 1;

fragment_inputs[fragment_inputs_size] = f->parameter_type.type;
assert(f->parameters_size > 0);
fragment_inputs[fragment_inputs_size] = f->parameter_types[0].type;
fragment_inputs_size += 1;
}
}
Expand Down
32 changes: 19 additions & 13 deletions Sources/backends/spirv.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,21 +739,25 @@ static void write_function(instructions_buffer *instructions, function *f, spirv
uint8_t *data = f->code.o;
size_t size = f->code.size;

uint64_t parameter_id = 0;
type_id parameter_type = 0;
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_name == f->block->block.vars.v[i].name) {
parameter_id = f->block->block.vars.v[i].variable_id;
parameter_type = f->block->block.vars.v[i].type.type;
break;
uint64_t parameter_ids[256] = {0};
type_id parameter_types[256] = {0};
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
for (size_t i = 0; i < f->block->block.vars.size; ++i) {
if (f->parameter_names[parameter_index] == f->block->block.vars.v[i].name) {
parameter_ids[parameter_index] = f->block->block.vars.v[i].variable_id;
parameter_types[parameter_index] = f->block->block.vars.v[i].type.type;
break;
}
}
}

check(parameter_id != 0, context, "Parameter not found");
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
check(parameter_ids[parameter_index] != 0, context, "Parameter not found");
}

// create variable for the input parameter
spirv_id spirv_parameter_id = convert_kong_index_to_spirv_id(parameter_id);
write_op_variable_preallocated(instructions, convert_pointer_type_to_spirv_id(parameter_type, STORAGE_CLASS_FUNCTION), spirv_parameter_id,
spirv_id spirv_parameter_id = convert_kong_index_to_spirv_id(parameter_ids[0]);
write_op_variable_preallocated(instructions, convert_pointer_type_to_spirv_id(parameter_types[0], STORAGE_CLASS_FUNCTION), spirv_parameter_id,
STORAGE_CLASS_FUNCTION);

// all vars have to go first
Expand Down Expand Up @@ -801,7 +805,7 @@ static void write_function(instructions_buffer *instructions, function *f, spirv
}

storage_class storage;
if (o->op_load_member.from.index == parameter_id) {
if (o->op_load_member.from.index == parameter_ids[0]) {
storage = STORAGE_CLASS_FUNCTION;
}
else {
Expand Down Expand Up @@ -1102,7 +1106,8 @@ static void spirv_export_vertex(char *directory, function *main) {
instructions_buffer constants = {0};
constants.instructions = (uint32_t *)calloc(1024 * 1024, 1);

type_id vertex_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id vertex_input = main->parameter_types[0].type;
type_id vertex_output = main->return_type.type;

debug_context context = {0};
Expand Down Expand Up @@ -1198,7 +1203,8 @@ static void spirv_export_fragment(char *directory, function *main) {
instructions_buffer constants = {0};
constants.instructions = (uint32_t *)calloc(1024 * 1024, 1);

type_id pixel_input = main->parameter_type.type;
assert(main->parameters_size > 0);
type_id pixel_input = main->parameter_types[0].type;

debug_context context = {0};
check(pixel_input != NO_TYPE, context, "fragment input missing");
Expand Down
14 changes: 8 additions & 6 deletions Sources/backends/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ void find_referenced_types(function *f, type_id *types, size_t *types_size) {

find_referenced_functions(f, functions, &functions_size);

for (size_t l = 0; l < functions_size; ++l) {
function *func = functions[l];
for (size_t function_index = 0; function_index < functions_size; ++function_index) {
function *func = functions[function_index];
debug_context context = {0};
check(func->parameter_type.type != NO_TYPE, context, "Function parameter type not found");
add_found_type(func->parameter_type.type, types, types_size);
for (uint8_t parameter_index = 0; parameter_index < func->parameters_size; ++parameter_index) {
check(func->parameter_types[parameter_index].type != NO_TYPE, context, "Function parameter type not found");
add_found_type(func->parameter_types[parameter_index].type, types, types_size);
}
check(func->return_type.type != NO_TYPE, context, "Function return type missing");
add_found_type(func->return_type.type, types, types_size);

uint8_t *data = functions[l]->code.o;
size_t size = functions[l]->code.size;
uint8_t *data = functions[function_index]->code.o;
size_t size = functions[function_index]->code.size;

size_t index = 0;
while (index < size) {
Expand Down
Loading

0 comments on commit 85401b9

Please sign in to comment.