From 500463c9cdf669de3b76f266781c0a34e52d305d Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 2 Feb 2025 20:58:21 +0100 Subject: [PATCH 1/3] Fix Xcode warnings --- Sources/backends/cstyle.c | 11 +-- Sources/backends/d3d11.c | 12 ++-- Sources/backends/d3d12.cpp | 2 + Sources/backends/glsl.c | 2 +- Sources/backends/hlsl.c | 18 +++++ Sources/backends/spirv.c | 38 +++++----- Sources/backends/util.c | 9 +++ Sources/backends/wgsl.c | 6 +- Sources/compiler.c | 16 +++-- Sources/integrations/kope.c | 136 ++++++++++++++++++++++++------------ Sources/kong.c | 2 +- Sources/parser.c | 26 +++---- Sources/types.c | 22 +++++- Sources/types.h | 20 +----- 14 files changed, 206 insertions(+), 114 deletions(-) diff --git a/Sources/backends/cstyle.c b/Sources/backends/cstyle.c index f6c621a..39894b7 100644 --- a/Sources/backends/cstyle.c +++ b/Sources/backends/cstyle.c @@ -8,9 +8,9 @@ #include #include -static char *function_string(name_id func) { - return get_name(func); -} +//static char *function_string(name_id func) { +// return get_name(func); +//} // HLSL for now static char *member_string(type *parent_type, name_id member_name) { @@ -84,7 +84,7 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func for (size_t i = 0; i < o->op_store_member.member_indices_size; ++i) { if (is_array) { if (o->op_store_member.dynamic_member[i]) { - *offset += sprintf(&code[*offset], "[_% " PRIu64 "]", o->op_store_member.dynamic_member_indices[i].index); + *offset += sprintf(&code[*offset], "[_%" PRIu64 "]", o->op_store_member.dynamic_member_indices[i].index); } else { *offset += sprintf(&code[*offset], "[%i]", o->op_store_member.static_member_indices[i]); @@ -116,6 +116,9 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func case OPCODE_MULTIPLY_AND_STORE_MEMBER: *offset += sprintf(&code[*offset], " *= _%" PRIu64 ";\n", o->op_store_member.from.index); break; + default: + assert(false); + break; } break; case OPCODE_LOAD_FLOAT_CONSTANT: diff --git a/Sources/backends/d3d11.c b/Sources/backends/d3d11.c index 2bdb096..db1e51c 100644 --- a/Sources/backends/d3d11.c +++ b/Sources/backends/d3d11.c @@ -17,6 +17,7 @@ #include #endif +#ifdef _WIN32 static const char *shaderString(shader_stage stage, int version) { if (version == 4) { switch (stage) { @@ -26,6 +27,9 @@ static const char *shaderString(shader_stage stage, int version) { return "ps_4_0"; case SHADER_STAGE_COMPUTE: return "cs_4_0"; + default: + assert(false); + return ""; } } else if (version == 5) { @@ -36,6 +40,9 @@ static const char *shaderString(shader_stage stage, int version) { return "ps_5_0"; case SHADER_STAGE_COMPUTE: return "cs_5_0"; + default: + assert(false); + return ""; } } @@ -43,10 +50,7 @@ static const char *shaderString(shader_stage stage, int version) { error(context, "Unsupported shader stage/version combination"); return "unsupported"; } - -struct map { - int nothing; -}; +#endif int compile_hlsl_to_d3d11(const char *source, uint8_t **output, size_t *outputlength, shader_stage stage, bool debug) { #ifdef _WIN32 diff --git a/Sources/backends/d3d12.cpp b/Sources/backends/d3d12.cpp index da0feba..1875c0a 100644 --- a/Sources/backends/d3d12.cpp +++ b/Sources/backends/d3d12.cpp @@ -15,6 +15,7 @@ #endif +#ifdef _WIN32 static const wchar_t *shader_string(shader_stage stage) { switch (stage) { case SHADER_STAGE_VERTEX: @@ -36,6 +37,7 @@ static const wchar_t *shader_string(shader_stage stage) { } } } +#endif int compile_hlsl_to_d3d12(const char *source, uint8_t **output, size_t *outputlength, shader_stage stage, bool debug) { #ifdef _WIN32 diff --git a/Sources/backends/glsl.c b/Sources/backends/glsl.c index efccb8c..5c76466 100644 --- a/Sources/backends/glsl.c +++ b/Sources/backends/glsl.c @@ -145,7 +145,7 @@ static void write_globals(char *glsl, size_t *offset, function *main) { for (size_t i = 0; i < globals_size; ++i) { global *g = get_global(globals[i]); - int register_index = global_register_indices[globals[i]]; + //int register_index = global_register_indices[globals[i]]; if (g->type == sampler_type_id) { } diff --git a/Sources/backends/hlsl.c b/Sources/backends/hlsl.c index cc82444..eb1ba6b 100644 --- a/Sources/backends/hlsl.c +++ b/Sources/backends/hlsl.c @@ -431,6 +431,9 @@ static void write_root_signature(char *hlsl, size_t *offset) { case DEFINITION_SAMPLER: has_sampler = true; break; + default: + assert(false); + break; } } @@ -475,6 +478,9 @@ static void write_root_signature(char *hlsl, size_t *offset) { srv_index += 1; } break; + default: + assert(false); + break; } } @@ -501,6 +507,9 @@ static void write_root_signature(char *hlsl, size_t *offset) { cbv_index += 1; } break; + default: + assert(false); + break; } } @@ -525,6 +534,9 @@ static void write_root_signature(char *hlsl, size_t *offset) { break; } + default: + assert(false); + break; } } } @@ -547,6 +559,9 @@ static void write_root_signature(char *hlsl, size_t *offset) { *offset += sprintf(&hlsl[*offset], "Sampler(s%i)", sampler_index); sampler_index += 1; break; + default: + assert(false); + break; } } @@ -618,6 +633,9 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func } } } + default: + assert(false); + break; } index += o->size; } diff --git a/Sources/backends/spirv.c b/Sources/backends/spirv.c index 4a943b5..87e13e4 100644 --- a/Sources/backends/spirv.c +++ b/Sources/backends/spirv.c @@ -349,13 +349,13 @@ static spirv_id write_type_float(instructions_buffer *instructions, uint32_t wid return float_type; } -static spirv_id write_type_vector(instructions_buffer *instructions, spirv_id component_type, uint32_t component_count) { - spirv_id vector_type = allocate_index(); - - uint32_t operands[] = {vector_type.id, component_type.id, component_count}; - write_instruction(instructions, WORD_COUNT(operands), SPIRV_OPCODE_TYPE_VECTOR, operands); - return vector_type; -} +//static spirv_id write_type_vector(instructions_buffer *instructions, spirv_id component_type, uint32_t component_count) { +// spirv_id vector_type = allocate_index(); +// +// uint32_t operands[] = {vector_type.id, component_type.id, component_count}; +// write_instruction(instructions, WORD_COUNT(operands), SPIRV_OPCODE_TYPE_VECTOR, operands); +// return vector_type; +//} static spirv_id write_type_vector_preallocated(instructions_buffer *instructions, spirv_id component_type, uint32_t component_count, spirv_id vector_type) { uint32_t operands[] = {vector_type.id, component_type.id, component_count}; @@ -569,11 +569,11 @@ static spirv_id write_op_function_preallocated(instructions_buffer *instructions return result; } -static spirv_id write_op_function(instructions_buffer *instructions, spirv_id result_type, function_control control, spirv_id function_type) { - spirv_id result = allocate_index(); - write_op_function_preallocated(instructions, result_type, control, function_type, result); - return result; -} +//static spirv_id write_op_function(instructions_buffer *instructions, spirv_id result_type, function_control control, spirv_id function_type) { +// spirv_id result = allocate_index(); +// write_op_function_preallocated(instructions, result_type, control, function_type, result); +// return result; +//} static spirv_id write_op_label(instructions_buffer *instructions) { spirv_id result = allocate_index(); @@ -721,13 +721,13 @@ static spirv_id write_op_variable_preallocated(instructions_buffer *instructions return result; } -static spirv_id write_op_variable_with_initializer(instructions_buffer *instructions, uint32_t result_type, storage_class storage, uint32_t initializer) { - spirv_id result = allocate_index(); - - uint32_t operands[] = {result_type, result.id, (uint32_t)storage, initializer}; - write_instruction(instructions, WORD_COUNT(operands), SPIRV_OPCODE_VARIABLE, operands); - return result; -} +//static spirv_id write_op_variable_with_initializer(instructions_buffer *instructions, uint32_t result_type, storage_class storage, uint32_t initializer) { +// spirv_id result = allocate_index(); +// +// uint32_t operands[] = {result_type, result.id, (uint32_t)storage, initializer}; +// write_instruction(instructions, WORD_COUNT(operands), SPIRV_OPCODE_VARIABLE, operands); +// return result; +//} static struct { uint64_t key; diff --git a/Sources/backends/util.c b/Sources/backends/util.c index 2a3001e..da255b7 100644 --- a/Sources/backends/util.c +++ b/Sources/backends/util.c @@ -86,6 +86,9 @@ void find_referenced_globals(function *f, global_id *globals, size_t *globals_si } break; } + default: + assert(false); + break; } index += o->size; @@ -132,6 +135,9 @@ void find_referenced_functions(function *f, function **functions, size_t *functi } break; } + default: + assert(false); + break; } index += o->size; @@ -183,6 +189,9 @@ void find_referenced_types(function *f, type_id *types, size_t *types_size) { case OPCODE_VAR: add_found_type(o->op_var.var.type.type, types, types_size); break; + default: + assert(false); + break; } index += o->size; diff --git a/Sources/backends/wgsl.c b/Sources/backends/wgsl.c index 5656427..9644498 100644 --- a/Sources/backends/wgsl.c +++ b/Sources/backends/wgsl.c @@ -36,9 +36,9 @@ static char *type_string(type_id type) { return get_name(get_type(type)->name); } -static char *function_string(name_id func) { - return get_name(func); -} +//static char *function_string(name_id func) { +// return get_name(func); +//} static void write_code(char *wgsl, char *directory, const char *filename) { char full_filename[512]; diff --git a/Sources/compiler.c b/Sources/compiler.c index d6dfe68..98fef23 100644 --- a/Sources/compiler.c +++ b/Sources/compiler.c @@ -161,7 +161,6 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { o.type = OPCODE_XOR; break; default: { - debug_context context = {0}; error(context, "Unexpected operator"); } } @@ -200,7 +199,6 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { o.type = OPCODE_MOD; break; default: { - debug_context context = {0}; error(context, "Unexpected operator"); } } @@ -213,7 +211,6 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { return result_var; } case OPERATOR_NOT: { - debug_context context = {0}; error(context, "! is not a binary operator"); } case OPERATOR_ASSIGN: @@ -243,7 +240,6 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { o.type = OPCODE_MULTIPLY_AND_STORE_VARIABLE; break; default: { - debug_context context = {0}; error(context, "Unexpected operator"); } } @@ -275,7 +271,6 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { o.type = OPCODE_MULTIPLY_AND_STORE_MEMBER; break; default: { - debug_context context = {0}; error(context, "Unexpected operator"); } } @@ -414,6 +409,11 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { error(context, "not implemented"); case OPERATOR_ASSIGN: error(context, "not implemented"); + case OPERATOR_PLUS_ASSIGN: + case OPERATOR_MINUS_ASSIGN: + case OPERATOR_MULTIPLY_ASSIGN: + case OPERATOR_DIVIDE_ASSIGN: + error(context, "not implemented"); } } case EXPRESSION_BOOLEAN: { @@ -592,6 +592,10 @@ variable emit_expression(opcodes *code, block *parent, expression *e) { debug_context context = {0}; error(context, "not implemented"); } + default: { + debug_context context = {0}; + error(context, "not implemented"); + } } { @@ -863,7 +867,7 @@ static block_ids emit_statement(opcodes *code, block *parent, statement *stateme o.type = OPCODE_VAR; o.size = OP_SIZE(o, op_var); - variable init_var; + variable init_var = {0}; if (statement->local_variable.init != NULL) { init_var = emit_expression(code, parent, statement->local_variable.init); } diff --git a/Sources/integrations/kope.c b/Sources/integrations/kope.c index ecb7819..4ac3c8a 100644 --- a/Sources/integrations/kope.c +++ b/Sources/integrations/kope.c @@ -313,6 +313,9 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se case DEFINITION_SAMPLER: has_sampler = true; break; + default: + assert(false); + break; } } @@ -346,6 +349,9 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se case DEFINITION_SAMPLER: has_sampler = true; break; + default: + assert(false); + break; } } @@ -362,10 +368,13 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se count += 1; break; } + default: + assert(false); + break; } } - fprintf(output, "\n\tD3D12_DESCRIPTOR_RANGE ranges%i[%" PRIu64 "] = {};\n", table_index, count); + fprintf(output, "\n\tD3D12_DESCRIPTOR_RANGE ranges%i[%zu] = {};\n", table_index, count); size_t range_index = 0; for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { @@ -373,10 +382,10 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se switch (def->kind) { case DEFINITION_CONST_CUSTOM: - fprintf(output, "\tranges%i[%" PRIu64 "].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].BaseShaderRegister = %i;\n", table_index, range_index, cbv_index); - fprintf(output, "\tranges%i[%" PRIu64 "].NumDescriptors = 1;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, + fprintf(output, "\tranges%i[%zu].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].BaseShaderRegister = %i;\n", table_index, range_index, cbv_index); + fprintf(output, "\tranges%i[%zu].NumDescriptors = 1;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, range_index); cbv_index += 1; @@ -388,19 +397,19 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se attribute *write_attribute = find_attribute(&get_global(def->global)->attributes, add_name("write")); if (write_attribute != NULL) { - fprintf(output, "\tranges%i[%" PRIu64 "].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].BaseShaderRegister = %i;\n", table_index, range_index, uav_index); - fprintf(output, "\tranges%i[%" PRIu64 "].NumDescriptors = 1;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, + fprintf(output, "\tranges%i[%zu].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].BaseShaderRegister = %i;\n", table_index, range_index, uav_index); + fprintf(output, "\tranges%i[%zu].NumDescriptors = 1;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, range_index); uav_index += 1; } else { - fprintf(output, "\tranges%i[%" PRIu64 "].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].BaseShaderRegister = %i;\n", table_index, range_index, srv_index); - fprintf(output, "\tranges%i[%" PRIu64 "].NumDescriptors = 1;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, + fprintf(output, "\tranges%i[%zu].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].BaseShaderRegister = %i;\n", table_index, range_index, srv_index); + fprintf(output, "\tranges%i[%zu].NumDescriptors = 1;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, range_index); srv_index += 1; @@ -408,13 +417,16 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se range_index += 1; + break; + default: + assert(false); break; } case DEFINITION_BVH: - fprintf(output, "\tranges%i[%" PRIu64 "].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].BaseShaderRegister = %i;\n", table_index, range_index, srv_index); - fprintf(output, "\tranges%i[%" PRIu64 "].NumDescriptors = 1;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, + fprintf(output, "\tranges%i[%zu].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].BaseShaderRegister = %i;\n", table_index, range_index, srv_index); + fprintf(output, "\tranges%i[%zu].NumDescriptors = 1;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, range_index); srv_index += 1; @@ -426,7 +438,7 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se } fprintf(output, "\n\tparams[%i].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;\n", table_index); - fprintf(output, "\tparams[%i].DescriptorTable.NumDescriptorRanges = %" PRIu64 ";\n", table_index, count); + fprintf(output, "\tparams[%i].DescriptorTable.NumDescriptorRanges = %zu;\n", table_index, count); fprintf(output, "\tparams[%i].DescriptorTable.pDescriptorRanges = ranges%i;\n", table_index, table_index); table_index += 1; @@ -443,10 +455,13 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se count += 1; break; } + default: + assert(false); + break; } } - fprintf(output, "\n\tD3D12_DESCRIPTOR_RANGE ranges%i[%" PRIu64 "] = {};\n", table_index, count); + fprintf(output, "\n\tD3D12_DESCRIPTOR_RANGE ranges%i[%zu] = {};\n", table_index, count); size_t range_index = 0; for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { @@ -454,18 +469,21 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se switch (def->kind) { case DEFINITION_SAMPLER: - fprintf(output, "\tranges%i[%" PRIu64 "].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].BaseShaderRegister = %i;\n", table_index, range_index, sampler_index); - fprintf(output, "\tranges%i[%" PRIu64 "].NumDescriptors = 1;\n", table_index, range_index); - fprintf(output, "\tranges%i[%" PRIu64 "].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, + fprintf(output, "\tranges%i[%zu].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].BaseShaderRegister = %i;\n", table_index, range_index, sampler_index); + fprintf(output, "\tranges%i[%zu].NumDescriptors = 1;\n", table_index, range_index); + fprintf(output, "\tranges%i[%zu].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;\n", table_index, range_index); sampler_index += 1; break; + default: + assert(false); + break; } } fprintf(output, "\n\tparams[%i].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;\n", table_index); - fprintf(output, "\tparams[%i].DescriptorTable.NumDescriptorRanges = %" PRIu64 ";\n", table_index, count); + fprintf(output, "\tparams[%i].DescriptorTable.NumDescriptorRanges = %zu;\n", table_index, count); fprintf(output, "\tparams[%i].DescriptorTable.pDescriptorRanges = ranges%i;\n", table_index, table_index); table_index += 1; @@ -542,6 +560,9 @@ void kope_export(char *directory, api_kind api) { api_long = "webgpu"; api_caps = "WEBGPU"; break; + default: + assert(false); + break; } if (api == API_WEBGPU) { @@ -682,7 +703,7 @@ void kope_export(char *directory, api_kind api) { for (size_t j = 0; j < t->members.size; ++j) { fprintf(output, "\t%s %s;\n", type_string(t->members.m[j].type.type), get_name(t->members.m[j].name)); if (t->members.m[j].type.type == float3x3_id) { - fprintf(output, "\tfloat pad%" PRIu64 "[3];\n", j); + fprintf(output, "\tfloat pad%zu[3];\n", j); } } fprintf(output, "} %s;\n\n", name); @@ -786,6 +807,9 @@ void kope_export(char *directory, api_kind api) { } break; } + default: + assert(false); + break; } } fprintf(output, "} %s_set;\n\n", get_name(set->name)); @@ -803,6 +827,9 @@ void kope_export(char *directory, api_kind api) { fprintf(output, ", uint32_t %s_index", get_name(get_global(d.global)->name)); } break; + default: + assert(false); + break; } } fprintf(output, ");\n\n"); @@ -825,10 +852,13 @@ void kope_export(char *directory, api_kind api) { fprintf(output, "\t\t%s_SET_UPDATE_%s,\n", upper_set_name, upper_definition_name); break; CASE_TEXTURE: { - type *t = get_type(get_global(d.global)->type); + //type *t = get_type(get_global(d.global)->type); fprintf(output, "\t\t%s_SET_UPDATE_%s,\n", upper_set_name, upper_definition_name); break; } + default: + assert(false); + break; } } fprintf(output, "\t} kind;\n"); @@ -857,6 +887,9 @@ void kope_export(char *directory, api_kind api) { } break; } + default: + assert(false); + break; } } fprintf(output, "\t};\n"); @@ -999,7 +1032,7 @@ void kope_export(char *directory, api_kind api) { fprintf(output, "}\n\n"); fprintf(output, "void kong_set_vertex_buffer_%s(kope_g5_command_list *list, %s_buffer *buffer) {\n", get_name(t->name), get_name(t->name)); - fprintf(output, "\tkope_%s_command_list_set_vertex_buffer(list, %" PRIu64 ", &buffer->buffer.%s, 0, buffer->count * sizeof(%s), sizeof(%s));\n", + fprintf(output, "\tkope_%s_command_list_set_vertex_buffer(list, %zu, &buffer->buffer.%s, 0, buffer->count * sizeof(%s), sizeof(%s));\n", api_short, vertex_input_slots[i], api_short, get_name(t->name), get_name(t->name)); fprintf(output, "}\n\n"); } @@ -1170,6 +1203,9 @@ void kope_export(char *directory, api_kind api) { other_count += 1; } break; + default: + assert(false); + break; } case DEFINITION_SAMPLER: sampler_count += 1; @@ -1177,7 +1213,7 @@ void kope_export(char *directory, api_kind api) { } } - fprintf(output, "\tkope_%s_device_create_descriptor_set(device, %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", &set->set);\n", api_short, + fprintf(output, "\tkope_%s_device_create_descriptor_set(device, %zu, %zu, %zu, %zu, &set->set);\n", api_short, other_count, dynamic_count, bindless_count, sampler_count); size_t other_index = 0; @@ -1189,14 +1225,14 @@ void kope_export(char *directory, api_kind api) { switch (d.kind) { case DEFINITION_CONST_CUSTOM: if (!has_attribute(&get_global(d.global)->attributes, add_name("indexed"))) { - fprintf(output, "\tkope_%s_descriptor_set_set_buffer_view_cbv(device, &set->set, parameters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_buffer_view_cbv(device, &set->set, parameters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), other_index); other_index += 1; } fprintf(output, "\tset->%s = parameters->%s;\n", get_name(get_global(d.global)->name), get_name(get_global(d.global)->name)); break; case DEFINITION_BVH: - fprintf(output, "\tkope_%s_descriptor_set_set_bvh_view_srv(device, &set->set, parameters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_bvh_view_srv(device, &set->set, parameters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), other_index); fprintf(output, "\tset->%s = parameters->%s;\n", get_name(get_global(d.global)->name), get_name(get_global(d.global)->name)); other_index += 1; @@ -1222,13 +1258,12 @@ void kope_export(char *directory, api_kind api) { else { attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write")); if (write_attribute != NULL) { - fprintf(output, "\tkope_%s_descriptor_set_set_texture_view_uav(device, &set->set, ¶meters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_texture_view_uav(device, &set->set, ¶meters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), other_index); } else { fprintf(output, - "\tkope_%s_descriptor_set_set_texture_view_srv(device, set->set.descriptor_allocation.offset + %" PRIu64 - ", ¶meters->%s);\n", + "\tkope_%s_descriptor_set_set_texture_view_srv(device, set->set.descriptor_allocation.offset + %zu, ¶meters->%s);\n", api_short, other_index, get_name(get_global(d.global)->name)); } @@ -1246,7 +1281,7 @@ void kope_export(char *directory, api_kind api) { error(context, "Texture arrays can not be writable"); } - fprintf(output, "\tkope_%s_descriptor_set_set_texture_array_view_srv(device, &set->set, ¶meters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_texture_array_view_srv(device, &set->set, ¶meters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), other_index); fprintf(output, "\tset->%s = parameters->%s;\n", get_name(get_global(d.global)->name), get_name(get_global(d.global)->name)); @@ -1259,7 +1294,7 @@ void kope_export(char *directory, api_kind api) { debug_context context = {0}; error(context, "Cube maps can not be writable"); } - fprintf(output, "\tkope_%s_descriptor_set_set_texture_cube_view_srv(device, &set->set, ¶meters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_texture_cube_view_srv(device, &set->set, ¶meters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), other_index); fprintf(output, "\tset->%s = parameters->%s;\n", get_name(get_global(d.global)->name), get_name(get_global(d.global)->name)); @@ -1267,10 +1302,13 @@ void kope_export(char *directory, api_kind api) { break; } case DEFINITION_SAMPLER: - fprintf(output, "\tkope_%s_descriptor_set_set_sampler(device, &set->set, parameters->%s, %" PRIu64 ");\n", api_short, + fprintf(output, "\tkope_%s_descriptor_set_set_sampler(device, &set->set, parameters->%s, %zu);\n", api_short, get_name(get_global(d.global)->name), sampler_index); sampler_index += 1; break; + default: + assert(false); + break; } } fprintf(output, "}\n\n"); @@ -1288,6 +1326,9 @@ void kope_export(char *directory, api_kind api) { fprintf(output, ", uint32_t %s_index", get_name(get_global(d.global)->name)); } break; + default: + assert(false); + break; } } fprintf(output, ") {\n"); @@ -1326,6 +1367,9 @@ void kope_export(char *directory, api_kind api) { } break; } + default: + assert(false); + break; } } @@ -1341,6 +1385,9 @@ void kope_export(char *directory, api_kind api) { dynamic_count += 1; } break; + default: + assert(false); + break; } } @@ -1364,6 +1411,9 @@ void kope_export(char *directory, api_kind api) { dynamic_index += 1; } break; + default: + assert(false); + break; } } } @@ -1570,20 +1620,20 @@ void kope_export(char *directory, api_kind api) { structure_type(vertex_type->members.m[j].type.type, api)); } else { - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].attributes[%" PRIu64 "].format = %s;\n", get_name(t->name), + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].attributes[%zu].format = %s;\n", get_name(t->name), input_index, j, structure_type(vertex_type->members.m[j].type.type, api)); - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].attributes[%" PRIu64 "].offset = %" PRIu64 ";\n", + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].attributes[%zu].offset = %zu;\n", get_name(t->name), input_index, j, offset); - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].attributes[%" PRIu64 "].shader_location = %" PRIu64 ";\n", + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].attributes[%zu].shader_location = %zu;\n", get_name(t->name), input_index, j, location); } offset += base_type_size(vertex_type->members.m[j].type.type); location += 1; } - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].attributes_count = %" PRIu64 ";\n", get_name(t->name), input_index, + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].attributes_count = %zu;\n", get_name(t->name), input_index, vertex_type->members.size); - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].array_stride = %" PRIu64 ";\n", get_name(t->name), input_index, offset); + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].array_stride = %zu;\n", get_name(t->name), input_index, offset); char step_mode[64]; if (instanced[input_index]) { @@ -1592,9 +1642,9 @@ void kope_export(char *directory, api_kind api) { else { sprintf(step_mode, "KOPE_%s_VERTEX_STEP_MODE_VERTEX", api_caps); } - fprintf(output, "\t%s_parameters.vertex.buffers[%" PRIu64 "].step_mode = %s;\n", get_name(t->name), input_index, step_mode); + fprintf(output, "\t%s_parameters.vertex.buffers[%zu].step_mode = %s;\n", get_name(t->name), input_index, step_mode); } - fprintf(output, "\t%s_parameters.vertex.buffers_count = %" PRIu64 ";\n\n", get_name(t->name), vertex_inputs_count); + fprintf(output, "\t%s_parameters.vertex.buffers_count = %zu;\n\n", get_name(t->name), vertex_inputs_count); } fprintf(output, "\t%s_parameters.primitive.topology = KOPE_%s_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;\n", get_name(t->name), api_caps); diff --git a/Sources/kong.c b/Sources/kong.c index 19d34c5..7e20f44 100644 --- a/Sources/kong.c +++ b/Sources/kong.c @@ -491,7 +491,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); + //const char *n = get_name(e->call.func_name); error(context, "Could not resolve type"); } } diff --git a/Sources/parser.c b/Sources/parser.c index b0b1ced..c022748 100644 --- a/Sources/parser.c +++ b/Sources/parser.c @@ -16,9 +16,9 @@ static statement *statement_allocate(void) { return s; } -static void statement_free(statement *statement) { - free(statement); -} +//static void statement_free(statement *statement) { +// free(statement); +//} static void statements_init(statements *statements) { statements->size = 0; @@ -37,9 +37,9 @@ static expression *expression_allocate(void) { return e; } -static void expression_free(expression *expression) { - free(expression); -} +//static void expression_free(expression *expression) { +// free(expression); +//} typedef struct state { tokens *tokens; @@ -137,14 +137,14 @@ typedef struct modifiers { size_t size; } modifiers_t; -static void modifiers_init(modifiers_t *modifiers) { - modifiers->size = 0; -} +//static void modifiers_init(modifiers_t *modifiers) { +// modifiers->size = 0; +//} -static void modifiers_add(modifiers_t *modifiers, modifier_t modifier) { - modifiers->m[modifiers->size] = modifier; - modifiers->size += 1; -} +//static void modifiers_add(modifiers_t *modifiers, modifier_t modifier) { +// modifiers->m[modifiers->size] = modifier; +// modifiers->size += 1; +//} static definition parse_struct(state_t *state); static definition parse_function(state_t *state); diff --git a/Sources/types.c b/Sources/types.c index 4c66b22..cec6695 100644 --- a/Sources/types.c +++ b/Sources/types.c @@ -670,9 +670,7 @@ void types_init(void) { get_type(ray_type_id)->built_in = true; type *t = get_type(ray_type_id); - - debug_context context = {0}; - + t->members.m[t->members.size].name = add_name("origin"); t->members.m[t->members.size].type.type = float3_id; t->members.m[t->members.size].type.array_size = 0; @@ -760,3 +758,21 @@ type *get_type(type_id s) { } return &types[s]; } + +bool has_attribute(attribute_list *attributes, name_id name) { + for (uint8_t index = 0; index < attributes->attributes_count; ++index) { + if (attributes->attributes[index].name == name) { + return true; + } + } + return false; +} + +attribute *find_attribute(attribute_list *attributes, name_id name) { + for (uint8_t index = 0; index < attributes->attributes_count; ++index) { + if (attributes->attributes[index].name == name) { + return &attributes->attributes[index]; + } + } + return NULL; +} diff --git a/Sources/types.h b/Sources/types.h index 7b3bc39..923a874 100644 --- a/Sources/types.h +++ b/Sources/types.h @@ -42,23 +42,9 @@ typedef struct attributes { uint8_t attributes_count; } attribute_list; -static bool has_attribute(attribute_list *attributes, name_id name) { - for (uint8_t index = 0; index < attributes->attributes_count; ++index) { - if (attributes->attributes[index].name == name) { - return true; - } - } - return false; -} - -static attribute *find_attribute(attribute_list *attributes, name_id name) { - for (uint8_t index = 0; index < attributes->attributes_count; ++index) { - if (attributes->attributes[index].name == name) { - return &attributes->attributes[index]; - } - } - return NULL; -} +bool has_attribute(attribute_list *attributes, name_id name); + +attribute *find_attribute(attribute_list *attributes, name_id name); typedef struct type { attribute_list attributes; From cb59f43db7cb35eac0ea1078e5b5fc7868942e72 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 2 Feb 2025 21:53:21 +0100 Subject: [PATCH 2/3] Fix command line parameters and some of the new asserts --- Sources/backends/util.c | 2 - Sources/integrations/kope.c | 22 +++++-- Sources/kong.c | 114 ++++++++++++++++++++---------------- 3 files changed, 78 insertions(+), 60 deletions(-) diff --git a/Sources/backends/util.c b/Sources/backends/util.c index da255b7..6cb3f5e 100644 --- a/Sources/backends/util.c +++ b/Sources/backends/util.c @@ -87,7 +87,6 @@ void find_referenced_globals(function *f, global_id *globals, size_t *globals_si break; } default: - assert(false); break; } @@ -136,7 +135,6 @@ void find_referenced_functions(function *f, function **functions, size_t *functi break; } default: - assert(false); break; } diff --git a/Sources/integrations/kope.c b/Sources/integrations/kope.c index 4ac3c8a..73bc5fa 100644 --- a/Sources/integrations/kope.c +++ b/Sources/integrations/kope.c @@ -1489,9 +1489,14 @@ void kope_export(char *directory, api_kind api) { for (size_t j = 0; j < t->members.size; ++j) { if (t->members.m[j].name == add_name("vertex")) { - fprintf(output, "\t%s_parameters.vertex.shader.data = %s_code;\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); - fprintf(output, "\t%s_parameters.vertex.shader.size = %s_code_size;\n\n", get_name(t->name), - get_name(t->members.m[j].value.identifier)); + if (api == API_METAL) { + fprintf(output, "\t%s_parameters.vertex.shader.function_name = \"%s\";\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); + } + else { + fprintf(output, "\t%s_parameters.vertex.shader.data = %s_code;\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); + fprintf(output, "\t%s_parameters.vertex.shader.size = %s_code_size;\n\n", get_name(t->name), + get_name(t->members.m[j].value.identifier)); + } vertex_shader_name = t->members.m[j].value.identifier; } else if (t->members.m[j].name == add_name("amplification")) { @@ -1501,9 +1506,14 @@ void kope_export(char *directory, api_kind api) { mesh_shader_name = t->members.m[j].value.identifier; } else if (t->members.m[j].name == add_name("fragment")) { - fprintf(output, "\t%s_parameters.fragment.shader.data = %s_code;\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); - fprintf(output, "\t%s_parameters.fragment.shader.size = %s_code_size;\n\n", get_name(t->name), - get_name(t->members.m[j].value.identifier)); + if (api == API_METAL) { + fprintf(output, "\t%s_parameters.fragment.shader.function_name = \"%s\";\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); + } + else { + fprintf(output, "\t%s_parameters.fragment.shader.data = %s_code;\n", get_name(t->name), get_name(t->members.m[j].value.identifier)); + fprintf(output, "\t%s_parameters.fragment.shader.size = %s_code_size;\n\n", get_name(t->name), + get_name(t->members.m[j].value.identifier)); + } fragment_shader_name = t->members.m[j].value.identifier; } // else if (t->members.m[j].name == add_name("depth_write")) { diff --git a/Sources/kong.c b/Sources/kong.c index 7e20f44..1104c48 100644 --- a/Sources/kong.c +++ b/Sources/kong.c @@ -663,128 +663,138 @@ int main(int argc, char **argv) { char *output = NULL; for (int i = 1; i < argc; ++i) { + char *arg = argv[i]; switch (mode) { case MODE_MODECHECK: { - if (argv[i][0] == '-') { - if (argv[i][1] == '-') { - if (strcmp(&argv[i][2], "in") == 0) { + if (arg[0] == '-') { + if (arg[1] == '-') { + if (strcmp(&arg[2], "in") == 0) { mode = MODE_INPUT; } - else if (strcmp(&argv[i][2], "out") == 0) { + else if (strcmp(&arg[2], "out") == 0) { mode = MODE_OUTPUT; } - else if (strcmp(&argv[i][2], "platform") == 0) { + else if (strcmp(&arg[2], "platform") == 0) { mode = MODE_PLATFORM; } - else if (strcmp(&argv[i][2], "api") == 0) { + else if (strcmp(&arg[2], "api") == 0) { mode = MODE_API; } - else if (strcmp(&argv[i][2], "integration")) { + else if (strcmp(&arg[2], "integration")) { mode = MODE_INTEGRATION; } - else if (strcmp(&argv[i][2], "help") == 0) { + else if (strcmp(&arg[2], "help") == 0) { help(); return 0; } else { - debug_context context = {0}; - error(context, "Unknown parameter %s", &argv[i][2]); + kong_log(LOG_LEVEL_WARNING, "Ignoring unknown parameter %s", arg); } } else { - debug_context context = {0}; - check(argv[i][1] != 0, context, "Borked parameter"); - check(argv[i][2] == 0, context, "Borked parameter"); - switch (argv[i][1]) { - case 'i': - mode = MODE_INPUT; - break; - case 'o': - mode = MODE_OUTPUT; - break; - case 'p': - mode = MODE_PLATFORM; - break; - case 'a': - mode = MODE_API; - break; - case 'n': - mode = MODE_INTEGRATION; - break; - case 'h': - help(); - return 0; - default: { - debug_context context = {0}; - error(context, "Unknown parameter %s", argv[i][1]); + bool ok = true; + + + if (arg[1] == 0) { + kong_log(LOG_LEVEL_WARNING, "Ignoring lonely -."); + ok = false; } + + if (arg[2] != 0) { + kong_log(LOG_LEVEL_WARNING, "Ignoring parameter %s because the format is not right (one - implies just one letter).", arg); + ok = false; + } + + if (ok) { + switch (arg[1]) { + case 'i': + mode = MODE_INPUT; + break; + case 'o': + mode = MODE_OUTPUT; + break; + case 'p': + mode = MODE_PLATFORM; + break; + case 'a': + mode = MODE_API; + break; + case 'n': + mode = MODE_INTEGRATION; + break; + case 'h': + help(); + return 0; + default: { + kong_log(LOG_LEVEL_WARNING, "Ignoring unknown parameter %s.", arg); + } + } } } } else { - debug_context context = {0}; - error(context, "Wrong parameter syntax"); + kong_log(LOG_LEVEL_WARNING, "Ignoring parameter %s because the syntax is off.", arg); } break; } case MODE_INPUT: { - inputs[inputs_size] = argv[i]; + inputs[inputs_size] = arg; inputs_size += 1; mode = MODE_MODECHECK; break; } case MODE_OUTPUT: { - output = argv[i]; + output = arg; mode = MODE_MODECHECK; break; } case MODE_PLATFORM: { - platform = argv[i]; + platform = arg; mode = MODE_MODECHECK; break; } case MODE_API: { - if (strcmp(argv[i], "direct3d9") == 0) { + if (strcmp(arg, "direct3d9") == 0) { api = API_DIRECT3D9; } - else if (strcmp(argv[i], "direct3d11") == 0) { + else if (strcmp(arg, "direct3d11") == 0) { api = API_DIRECT3D11; } - else if (strcmp(argv[i], "direct3d12") == 0) { + else if (strcmp(arg, "direct3d12") == 0) { api = API_DIRECT3D12; } - else if (strcmp(argv[i], "opengl") == 0) { + else if (strcmp(arg, "opengl") == 0) { api = API_OPENGL; } - else if (strcmp(argv[i], "metal") == 0) { + else if (strcmp(arg, "metal") == 0) { api = API_METAL; } - else if (strcmp(argv[i], "webgpu") == 0) { + else if (strcmp(arg, "webgpu") == 0) { api = API_WEBGPU; } - else if (strcmp(argv[i], "vulkan") == 0) { + else if (strcmp(arg, "vulkan") == 0) { api = API_VULKAN; } - else if (strcmp(argv[i], "default") == 0) { + else if (strcmp(arg, "default") == 0) { api = API_DEFAULT; } else { debug_context context = {0}; - error(context, "Unknown API %s", argv[i]); + error(context, "Unknown API %s", arg); } mode = MODE_MODECHECK; break; } case MODE_INTEGRATION: { - if (strcmp(argv[i], "kinc") == 0) { + if (strcmp(arg, "kinc") == 0) { integration = INTEGRATION_KINC; } - else if (strcmp(argv[i], "kope") == 0) { + else if (strcmp(arg, "kope") == 0) { integration = INTEGRATION_KOPE; } else { debug_context context = {0}; - error(context, "Unknown integration %s", argv[i]); + error(context, "Unknown integration %s", arg); } mode = MODE_MODECHECK; break; From 59746585a986a2b5fbcca06849551101b3f6f96e Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 2 Feb 2025 21:54:54 +0100 Subject: [PATCH 3/3] Ignore DS_Store --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 66630b6..fd9afad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /build /tests/out +.DS_Store +