Skip to content

Commit

Permalink
Pimp attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 23, 2024
1 parent 7a623e2 commit f83edff
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 162 deletions.
6 changes: 3 additions & 3 deletions Sources/backends/glsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static void write_types(char *glsl, size_t *offset, shader_stage stage, type_id
for (size_t i = 0; i < types_size; ++i) {
type *t = get_type(types[i]);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
if (stage == SHADER_STAGE_VERTEX && types[i] == input) {
for (size_t j = 0; j < t->members.size; ++j) {
*offset += sprintf(&glsl[*offset], "layout(location = %zu) in %s %s_%s;\n", j, type_string(t->members.m[j].type.type), get_name(t->name),
Expand Down Expand Up @@ -197,7 +197,7 @@ static void write_types(char *glsl, size_t *offset, shader_stage stage, type_id
for (size_t i = 0; i < types_size; ++i) {
type *t = get_type(types[i]);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
*offset += sprintf(&glsl[*offset], "struct %s {\n", get_name(t->name));

for (size_t j = 0; j < t->members.size; ++j) {
Expand Down Expand Up @@ -559,7 +559,7 @@ void glsl_export(char *directory) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;
name_id fragment_shader_name = NO_NAME;

Expand Down
14 changes: 10 additions & 4 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static void write_types(char *hlsl, size_t *offset, shader_stage stage, type_id
for (size_t i = 0; i < types_size; ++i) {
type *t = get_type(types[i]);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
*offset += sprintf(&hlsl[*offset], "struct %s {\n", get_name(t->name));

if (stage == SHADER_STAGE_VERTEX && types[i] == input) {
Expand Down Expand Up @@ -345,7 +345,13 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
}
}
else if (stage == SHADER_STAGE_COMPUTE) {
*offset += sprintf(&hlsl[*offset], "[numthreads(64, 1, 1)] %s main(", type_string(f->return_type.type));
attribute *threads_attribute = find_attribute(&f->attributes, add_name("threads"));
if (threads_attribute == NULL || threads_attribute->paramters_count != 3) {
debug_context context = {0};
error(context, "Compute function requires a threads attribute with three parameters");
}
*offset += sprintf(&hlsl[*offset], "[numthreads(%i, %i, %i)] %s main(", (int)threads_attribute->parameters[0],
(int)threads_attribute->parameters[1], (int)threads_attribute->parameters[2], type_string(f->return_type.type));
for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) {
if (parameter_index == 0) {
*offset +=
Expand Down Expand Up @@ -625,7 +631,7 @@ void hlsl_export(char *directory, api_kind d3d) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;
name_id fragment_shader_name = NO_NAME;

Expand Down Expand Up @@ -661,7 +667,7 @@ void hlsl_export(char *directory, api_kind d3d) {

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (f->attribute == add_name("compute")) {
if (has_attribute(&f->attributes, add_name("compute"))) {
compute_shaders[compute_shaders_size] = f;
compute_shaders_size += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/backends/metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static void write_types(char *metal, size_t *offset) {
for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
if (t->name == NO_NAME) {
char name[256];

Expand Down Expand Up @@ -483,7 +483,7 @@ void metal_export(char *directory) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;
name_id fragment_shader_name = NO_NAME;

Expand Down
4 changes: 2 additions & 2 deletions Sources/backends/spirv.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ static void write_types(instructions_buffer *constants, function *main) {
for (size_t i = 0; i < types_size; ++i) {
type *t = get_type(types[i]);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
spirv_id member_types[256];
uint16_t member_types_size = 0;
for (size_t j = 0; j < t->members.size; ++j) {
Expand Down Expand Up @@ -1265,7 +1265,7 @@ void spirv_export(char *directory) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;
name_id fragment_shader_name = NO_NAME;

Expand Down
4 changes: 2 additions & 2 deletions Sources/backends/wgsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static void write_types(char *wgsl, size_t *offset) {
for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);

if (!t->built_in && t->attribute != add_name("pipe")) {
if (!t->built_in && !has_attribute(&t->attributes, add_name("pipe"))) {
if (t->name == NO_NAME) {
char name[256];

Expand Down Expand Up @@ -664,7 +664,7 @@ void wgsl_export(char *directory) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;
name_id fragment_shader_name = NO_NAME;

Expand Down
2 changes: 1 addition & 1 deletion Sources/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function_id add_function(name_id name) {
++next_function_index;

functions[f].name = name;
functions[f].attribute = NO_NAME;
functions[f].attributes.attributes_count = 0;
init_type_ref(&functions[f].return_type, NO_NAME);
functions[f].parameters_size = 0;
functions[f].block = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Sources/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef uint32_t function_id;
struct statement;

typedef struct function {
name_id attribute;
attribute_list attributes;
name_id name;
type_ref return_type;
name_id parameter_names[256];
Expand Down
20 changes: 10 additions & 10 deletions Sources/integrations/kinc.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void kinc_export(char *directory, api_kind api) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
name_id vertex_shader_name = NO_NAME;

for (size_t j = 0; j < t->members.size; ++j) {
Expand Down Expand Up @@ -404,14 +404,14 @@ void kinc_export(char *directory, api_kind api) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
fprintf(output, "extern kinc_g4_pipeline_t %s;\n\n", get_name(t->name));
}
}

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (f->attribute == add_name("compute")) {
if (has_attribute(&f->attributes, add_name("compute"))) {
fprintf(output, "extern kinc_g4_compute_shader %s;\n\n", get_name(f->name));
}
}
Expand All @@ -438,7 +438,7 @@ void kinc_export(char *directory, api_kind api) {
else {
for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
for (size_t j = 0; j < t->members.size; ++j) {
debug_context context = {0};
if (t->members.m[j].name == add_name("vertex")) {
Expand All @@ -455,7 +455,7 @@ void kinc_export(char *directory, api_kind api) {

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (f->attribute == add_name("compute")) {
if (has_attribute(&f->attributes, add_name("compute"))) {
fprintf(output, "#include \"kong_%s.h\"\n", get_name(f->name));
}
}
Expand All @@ -474,7 +474,7 @@ void kinc_export(char *directory, api_kind api) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
fprintf(output, "kinc_g4_pipeline_t %s;\n\n", get_name(t->name));
}
}
Expand Down Expand Up @@ -534,7 +534,7 @@ void kinc_export(char *directory, api_kind api) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
for (size_t j = 0; j < t->members.size; ++j) {
if (t->members.m[j].name == add_name("vertex") || t->members.m[j].name == add_name("fragment")) {
debug_context context = {0};
Expand All @@ -547,7 +547,7 @@ void kinc_export(char *directory, api_kind api) {

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (f->attribute == add_name("compute")) {
if (has_attribute(&f->attributes, add_name("compute"))) {
fprintf(output, "kinc_g4_compute_shader %s;\n", get_name(f->name));
}
}
Expand All @@ -568,7 +568,7 @@ void kinc_export(char *directory, api_kind api) {

for (type_id i = 0; get_type(i) != NULL; ++i) {
type *t = get_type(i);
if (!t->built_in && t->attribute == add_name("pipe")) {
if (!t->built_in && has_attribute(&t->attributes, add_name("pipe"))) {
fprintf(output, "\tkinc_g4_pipeline_init(&%s);\n\n", get_name(t->name));

name_id vertex_shader_name = NO_NAME;
Expand Down Expand Up @@ -747,7 +747,7 @@ void kinc_export(char *directory, api_kind api) {

for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (f->attribute == add_name("compute")) {
if (has_attribute(&f->attributes, add_name("compute"))) {
fprintf(output, "\tkinc_g4_compute_shader_init(&%s, %s_code, %s_code_size);\n", get_name(f->name), get_name(f->name), get_name(f->name));
}
}
Expand Down
Loading

0 comments on commit f83edff

Please sign in to comment.