Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 4, 2023
1 parent b991132 commit 4a301c1
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 239 deletions.
14 changes: 9 additions & 5 deletions Sources/backends/cstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
is_array = false;
}
else {
check(o->op_store_member.member_indices[i] < s->members.size, 0, 0, "Member index out of bounds");
debug_context context = {0};
check(o->op_store_member.member_indices[i] < s->members.size, context, "Member index out of bounds");
*offset += sprintf(&code[*offset], ".%s", get_name(s->members.m[o->op_store_member.member_indices[i]].name));
is_array = s->members.m[o->op_store_member.member_indices[i]].type.array_size > 0;
s = get_type(s->members.m[o->op_store_member.member_indices[i]].type.type);
Expand All @@ -51,13 +52,14 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
o->op_load_constant.number);
break;
case OPCODE_CALL: {
debug_context context = {0};
if (o->op_call.func == add_name("sample")) {
check(o->op_call.parameters_size == 3, 0, 0, "sample requires three parameters");
check(o->op_call.parameters_size == 3, context, "sample requires three parameters");
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".Sample(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_call.var.type.type),
o->op_call.var.index, o->op_call.parameters[0].index, o->op_call.parameters[1].index, o->op_call.parameters[2].index);
}
else if (o->op_call.func == add_name("sample_lod")) {
check(o->op_call.parameters_size == 4, 0, 0, "sample_lod requires four parameters");
check(o->op_call.parameters_size == 4, context, "sample_lod requires four parameters");
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".SampleLevel(_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
type_string(o->op_call.var.type.type), o->op_call.var.index, o->op_call.parameters[0].index, o->op_call.parameters[1].index,
o->op_call.parameters[2].index, o->op_call.parameters[3].index);
Expand All @@ -80,8 +82,10 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
o->op_multiply.result.index, o->op_multiply.left.index, o->op_multiply.right.index);
break;
}
default:
error(0, 0, "Unknown opcode");
default: {
debug_context context = {0};
error(context, "Unknown opcode");
break;
}
}
}
8 changes: 5 additions & 3 deletions Sources/backends/d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static const char *shaderString(shader_stage stage, int version) {
}
}

error(0, 0, "Unsupported shader stage/version combination");
debug_context context = {0};
error(context, "Unsupported shader stage/version combination");
return "unsupported";
}

Expand Down Expand Up @@ -167,10 +168,11 @@ int compile_hlsl_to_d3d11(const char *source, uint8_t **output, size_t *outputle
return 0;
}
else {
check(errorMessage != NULL, 0, 0, "Error message missing");
debug_context context = {0};
check(errorMessage != NULL, context, "Error message missing");
SIZE_T size = errorMessage->lpVtbl->GetBufferSize(errorMessage);
char *error = malloc(size + 1);
check(error != NULL, 0, 0, "Could not allocate error string");
check(error != NULL, context, "Could not allocate error string");
memcpy(error, errorMessage->lpVtbl->GetBufferPointer(errorMessage), size);
error[size] = 0;
kong_log(LOG_LEVEL_ERROR, error);
Expand Down
36 changes: 22 additions & 14 deletions Sources/backends/glsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ static void find_referenced_types(function *f, type_id *types, size_t *types_siz

for (size_t l = 0; l < functions_size; ++l) {
function *func = functions[l];
check(func->parameter_type.type != NO_TYPE, 0, 0, "Function parameter type not found");
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);
check(func->return_type.type != NO_TYPE, 0, 0, "Function return type missing");
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;
Expand Down Expand Up @@ -343,7 +344,8 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
for (size_t i = 0; i < functions_size; ++i) {
function *f = functions[i];

check(f->block != NULL, 0, 0, "Function has no block");
debug_context context = {0};
check(f->block != NULL, context, "Function has no block");

uint8_t *data = f->code.o;
size_t size = f->code.size;
Expand All @@ -356,7 +358,7 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
}
}

check(parameter_id != 0, 0, 0, "Parameter not found");
check(parameter_id != 0, context, "Parameter not found");
if (f == main) {
if (stage == SHADER_STAGE_VERTEX) {
*offset += sprintf(&code[*offset], "void main() {\n");
Expand All @@ -375,7 +377,8 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
}
}
else {
error(0, 0, "Unsupported shader stage");
debug_context context = {0};
error(context, "Unsupported shader stage");
}
}
else {
Expand All @@ -389,13 +392,15 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
switch (o->type) {
case OPCODE_CALL: {
if (o->op_call.func == add_name("sample")) {
check(o->op_call.parameters_size == 3, 0, 0, "sample requires three parameters");
debug_context context = {0};
check(o->op_call.parameters_size == 3, context, "sample requires three parameters");
*offset +=
sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".Sample(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_call.var.type.type),
o->op_call.var.index, o->op_call.parameters[0].index, o->op_call.parameters[1].index, o->op_call.parameters[2].index);
}
else if (o->op_call.func == add_name("sample_lod")) {
check(o->op_call.parameters_size == 4, 0, 0, "sample_lod requires four parameters");
debug_context context = {0};
check(o->op_call.parameters_size == 4, context, "sample_lod requires four parameters");
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".SampleLevel(_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
type_string(o->op_call.var.type.type), o->op_call.var.index, o->op_call.parameters[0].index,
o->op_call.parameters[1].index, o->op_call.parameters[2].index, o->op_call.parameters[3].index);
Expand Down Expand Up @@ -522,15 +527,16 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type

static void glsl_export_vertex(char *directory, function *main) {
char *glsl = (char *)calloc(1024 * 1024, 1);
check(glsl != NULL, 0, 0, "Could not allocate glsl string");
debug_context context = {0};
check(glsl != NULL, context, "Could not allocate glsl string");

size_t offset = 0;

type_id vertex_input = main->parameter_type.type;
type_id vertex_output = main->return_type.type;

check(vertex_input != NO_TYPE, 0, 0, "vertex input missing");
check(vertex_output != NO_TYPE, 0, 0, "vertex output missing");
check(vertex_input != NO_TYPE, context, "vertex input missing");
check(vertex_output != NO_TYPE, context, "vertex output missing");

offset += sprintf(&glsl[offset], "#version 330\n\n");

Expand All @@ -553,13 +559,14 @@ static void glsl_export_vertex(char *directory, function *main) {

static void glsl_export_fragment(char *directory, function *main) {
char *glsl = (char *)calloc(1024 * 1024, 1);
check(glsl != NULL, 0, 0, "Could not allocate glsl string");
debug_context context = {0};
check(glsl != NULL, context, "Could not allocate glsl string");

size_t offset = 0;

type_id pixel_input = main->parameter_type.type;

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

offset += sprintf(&glsl[offset], "#version 330\n\n");

Expand Down Expand Up @@ -626,8 +633,9 @@ void glsl_export(char *directory) {
}
}

check(vertex_shader_name != NO_NAME, 0, 0, "vertex shader missing");
check(fragment_shader_name != NO_NAME, 0, 0, "fragment shader missing");
debug_context context = {0};
check(vertex_shader_name != NO_NAME, context, "vertex shader missing");
check(fragment_shader_name != NO_NAME, context, "fragment shader missing");

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
Expand Down
30 changes: 18 additions & 12 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ static void find_referenced_types(function *f, type_id *types, size_t *types_siz

for (size_t l = 0; l < functions_size; ++l) {
function *func = functions[l];
check(func->parameter_type.type != NO_TYPE, 0, 0, "Parameter type missing");
debug_context context = {0};
check(func->parameter_type.type != NO_TYPE, context, "Parameter type missing");
add_found_type(func->parameter_type.type, types, types_size);
check(func->return_type.type != NO_TYPE, 0, 0, "Return type missing");
check(func->return_type.type != NO_TYPE, context, "Return type missing");
add_found_type(func->return_type.type, types, types_size);

uint8_t *data = functions[l]->code.o;
Expand Down Expand Up @@ -355,7 +356,8 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
for (size_t i = 0; i < functions_size; ++i) {
function *f = functions[i];

check(f->block != NULL, 0, 0, "Function block missing");
debug_context context = {0};
check(f->block != NULL, context, "Function block missing");

uint8_t *data = f->code.o;
size_t size = f->code.size;
Expand All @@ -368,7 +370,7 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
}
}

check(parameter_id != 0, 0, 0, "Parameter not found");
check(parameter_id != 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),
Expand All @@ -389,7 +391,8 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
}
}
else {
error(0, 0, "Unsupported shader stage");
debug_context context = {0};
error(context, "Unsupported shader stage");
}
}
else {
Expand Down Expand Up @@ -476,8 +479,9 @@ static void hlsl_export_vertex(char *directory, function *main) {
type_id vertex_input = main->parameter_type.type;
type_id vertex_output = main->return_type.type;

check(vertex_input != NO_TYPE, 0, 0, "vertex input missing");
check(vertex_output != NO_TYPE, 0, 0, "vertex output missing");
debug_context context = {0};
check(vertex_input != NO_TYPE, context, "vertex input missing");
check(vertex_output != NO_TYPE, context, "vertex output missing");

write_types(hlsl, &offset, SHADER_STAGE_VERTEX, vertex_input, vertex_output, main);

Expand All @@ -488,7 +492,7 @@ static void hlsl_export_vertex(char *directory, function *main) {
char *output;
size_t output_size;
int result = compile_hlsl_to_d3d11(hlsl, &output, &output_size, SHADER_STAGE_VERTEX, false);
check(result == 0, 0, 0, "HLSL compilation failed");
check(result == 0, context, "HLSL compilation failed");

char *name = get_name(main->name);

Expand All @@ -507,7 +511,8 @@ static void hlsl_export_fragment(char *directory, function *main) {

type_id pixel_input = main->parameter_type.type;

check(pixel_input != NO_TYPE, 0, 0, "fragment input missing");
debug_context context = {0};
check(pixel_input != NO_TYPE, context, "fragment input missing");

write_types(hlsl, &offset, SHADER_STAGE_FRAGMENT, pixel_input, NO_TYPE, main);

Expand All @@ -518,7 +523,7 @@ static void hlsl_export_fragment(char *directory, function *main) {
uint8_t *output;
size_t output_size;
int result = compile_hlsl_to_d3d11(hlsl, &output, &output_size, SHADER_STAGE_FRAGMENT, false);
check(result == 0, 0, 0, "HLSL compilation failed");
check(result == 0, context, "HLSL compilation failed");

char *name = get_name(main->name);

Expand Down Expand Up @@ -577,8 +582,9 @@ void hlsl_export(char *directory) {
}
}

check(vertex_shader_name != NO_NAME, 0, 0, "vertex shader missing");
check(fragment_shader_name != NO_NAME, 0, 0, "fragment shader missing");
debug_context context = {0};
check(vertex_shader_name != NO_NAME, context, "vertex shader missing");
check(fragment_shader_name != NO_NAME, context, "fragment shader missing");

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
Expand Down
16 changes: 10 additions & 6 deletions Sources/backends/metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ static void find_referenced_types(function *f, type_id *types, size_t *types_siz

for (size_t l = 0; l < functions_size; ++l) {
function *func = functions[l];
check(func->parameter_type.type != NO_TYPE, 0, 0, "Function parameter type missing");
debug_context context = {0};
check(func->parameter_type.type != NO_TYPE, context, "Function parameter type missing");
add_found_type(func->parameter_type.type, types, types_size);
check(func->return_type.type != NO_TYPE, 0, 0, "Function return type missing");
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;
Expand Down Expand Up @@ -345,7 +346,8 @@ static void write_functions(char *metal, size_t *offset) {
}
}

check(parameter_id != 0, 0, 0, "Parameter not found");
debug_context context = {0};
check(parameter_id != 0, context, "Parameter not found");

char buffers[1024];
strcpy(buffers, "");
Expand Down Expand Up @@ -462,7 +464,8 @@ static void write_functions(char *metal, size_t *offset) {

static void metal_export_everything(char *directory) {
char *metal = (char *)calloc(1024 * 1024, 1);
check(metal != NULL, 0, 0, "Could not allocate Metal string");
debug_context context = {0};
check(metal != NULL, context, "Could not allocate Metal string");
size_t offset = 0;

offset += sprintf(&metal[offset], "#include <metal_stdlib>\n");
Expand Down Expand Up @@ -518,8 +521,9 @@ void metal_export(char *directory) {
}
}

check(vertex_shader_name != NO_NAME, 0, 0, "vertex shader missing");
check(fragment_shader_name != NO_NAME, 0, 0, "fragment shader missing");
debug_context context = {0};
check(vertex_shader_name != NO_NAME, context, "vertex shader missing");
check(fragment_shader_name != NO_NAME, context, "fragment shader missing");

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
Expand Down
21 changes: 13 additions & 8 deletions Sources/backends/wgsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ static void find_referenced_types(function *f, type_id *types, size_t *types_siz

for (size_t l = 0; l < functions_size; ++l) {
function *func = functions[l];
check(func->parameter_type.type != NO_TYPE, 0, 0, "Function parameter has no type");
debug_context context = {0};
check(func->parameter_type.type != NO_TYPE, context, "Function parameter has no type");
add_found_type(func->parameter_type.type, types, types_size);
check(func->return_type.type != NO_TYPE, 0, 0, "Function return type missing");
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;
Expand Down Expand Up @@ -359,7 +360,8 @@ static void write_functions(char *wgsl, size_t *offset) {
}
}

check(parameter_id != 0, 0, 0, "Parameter not found");
debug_context context = {0};
check(parameter_id != 0, context, "Parameter not found");

if (is_vertex_function(i)) {
*offset += sprintf(&wgsl[*offset], "@vertex fn %s(_%" PRIu64 ": %s) -> %s {\n", get_name(f->name), parameter_id,
Expand Down Expand Up @@ -448,14 +450,15 @@ static void write_functions(char *wgsl, size_t *offset) {
type_string(o->op_load_constant.to.type.type), o->op_load_constant.number);
break;
case OPCODE_CALL: {
debug_context context = {0};
if (o->op_call.func == add_name("sample")) {
check(o->op_call.parameters_size == 3, 0, 0, "sample requires three arguments");
check(o->op_call.parameters_size == 3, context, "sample requires three arguments");
*offset += sprintf(&wgsl[*offset], "\tvar _%" PRIu64 ": %s = textureSample(_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
o->op_call.var.index, type_string(o->op_call.var.type.type), o->op_call.parameters[0].index,
o->op_call.parameters[1].index, o->op_call.parameters[2].index);
}
else if (o->op_call.func == add_name("sample_lod")) {
check(o->op_call.parameters_size == 4, 0, 0, "sample_lod requires four arguments");
check(o->op_call.parameters_size == 4, context, "sample_lod requires four arguments");
*offset += sprintf(&wgsl[*offset], "\tvar _%" PRIu64 ": %s = textureSample(_%" PRIu64 ",_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
o->op_call.var.index, type_string(o->op_call.var.type.type), o->op_call.parameters[0].index,
o->op_call.parameters[1].index, o->op_call.parameters[2].index, o->op_call.parameters[3].index);
Expand Down Expand Up @@ -487,7 +490,8 @@ static void write_functions(char *wgsl, size_t *offset) {

static void wgsl_export_everything(char *directory) {
char *wgsl = (char *)calloc(1024 * 1024, 1);
check(wgsl != NULL, 0, 0, "Could not allocate the wgsl string");
debug_context context = {0};
check(wgsl != NULL, context, "Could not allocate the wgsl string");
size_t offset = 0;

write_types(wgsl, &offset);
Expand Down Expand Up @@ -539,8 +543,9 @@ void wgsl_export(char *directory) {
}
}

check(vertex_shader_name != NO_NAME, 0, 0, "vertex shader not found");
check(fragment_shader_name != NO_NAME, 0, 0, "fragment shader not found");
debug_context context = {0};
check(vertex_shader_name != NO_NAME, context, "vertex shader not found");
check(fragment_shader_name != NO_NAME, context, "fragment shader not found");

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
Expand Down
Loading

0 comments on commit 4a301c1

Please sign in to comment.