diff --git a/Sources/backends/hlsl.c b/Sources/backends/hlsl.c index ffb9a84..9b8989a 100644 --- a/Sources/backends/hlsl.c +++ b/Sources/backends/hlsl.c @@ -186,7 +186,12 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function * *offset += sprintf(&hlsl[*offset], "SamplerState _%" PRIu64 " : register(s%i);\n\n", g.var_index, register_index); } else if (g.type == tex2d_type_id) { - *offset += sprintf(&hlsl[*offset], "Texture2D _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index); + if (has_attribute(&g.attributes, add_name("write"))) { + *offset += sprintf(&hlsl[*offset], "RWTexture2D _%" PRIu64 " : register(u%i);\n\n", g.var_index, register_index); + } + else { + *offset += sprintf(&hlsl[*offset], "Texture2D _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index); + } } else if (g.type == texcube_type_id) { *offset += sprintf(&hlsl[*offset], "TextureCube _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index); @@ -770,8 +775,9 @@ static void hlsl_export_all_ray_shaders(char *directory) { } void hlsl_export(char *directory, api_kind d3d) { - int cbuffer_index = 0; - int texture_index = 0; + int cbv_index = 0; + int srv_index = 0; + int uav_index = 0; int sampler_index = 0; memset(global_register_indices, 0, sizeof(global_register_indices)); @@ -782,15 +788,25 @@ void hlsl_export(char *directory, api_kind d3d) { global_register_indices[i] = sampler_index; sampler_index += 1; } - else if (g.type == tex2d_type_id || g.type == texcube_type_id || g.type == bvh_type_id) { - global_register_indices[i] = texture_index; - texture_index += 1; + else if (g.type == tex2d_type_id) { + if (has_attribute(&g.attributes, add_name("write"))) { + global_register_indices[i] = uav_index; + uav_index += 1; + } + else { + global_register_indices[i] = srv_index; + srv_index += 1; + } + } + else if (g.type == texcube_type_id || g.type == bvh_type_id) { + global_register_indices[i] = srv_index; + srv_index += 1; } else if (g.type == float_id) { } else { - global_register_indices[i] = cbuffer_index; - cbuffer_index += 1; + global_register_indices[i] = cbv_index; + cbv_index += 1; } } diff --git a/Sources/globals.c b/Sources/globals.c index dc53c2a..0e0532c 100644 --- a/Sources/globals.c +++ b/Sources/globals.c @@ -10,77 +10,81 @@ static global_id globals_size = 0; void globals_init(void) { global_value int_value; int_value.kind = GLOBAL_VALUE_INT; - + + attribute_list attributes = {0}; + int_value.value.ints[0] = 0; - add_global_with_value(float_id, add_name("COMPARE_ALWAYS"), int_value); + add_global_with_value(float_id, attributes, add_name("COMPARE_ALWAYS"), int_value); int_value.value.ints[0] = 1; - add_global_with_value(float_id, add_name("COMPARE_NEVER"), int_value); + add_global_with_value(float_id, attributes, add_name("COMPARE_NEVER"), int_value); int_value.value.ints[0] = 2; - add_global_with_value(float_id, add_name("COMPARE_EQUAL"), int_value); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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, attributes, add_name("COMPARE_GREATER_EQUAL"), int_value); int_value.value.ints[0] = 0; - add_global_with_value(float_id, add_name("BLEND_ONE"), int_value); + add_global_with_value(float_id, attributes, add_name("BLEND_ONE"), int_value); int_value.value.ints[0] = 1; - add_global_with_value(float_id, add_name("BLEND_ZERO"), int_value); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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, attributes, add_name("BLEND_INV_CONSTANT"), int_value); int_value.value.ints[0] = 0; - add_global_with_value(float_id, add_name("BLENDOP_ADD"), int_value); + add_global_with_value(float_id, attributes, add_name("BLENDOP_ADD"), int_value); int_value.value.ints[0] = 1; - add_global_with_value(float_id, add_name("BLENDOP_SUBTRACT"), int_value); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, 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); + add_global_with_value(float_id, attributes, add_name("BLENDOP_MIN"), int_value); int_value.value.ints[0] = 4; - add_global_with_value(float_id, add_name("BLENDOP_MAX"), int_value); + add_global_with_value(float_id, attributes, add_name("BLENDOP_MAX"), int_value); } -global_id add_global(type_id type, name_id name) { +global_id add_global(type_id type, attribute_list attributes, name_id name) { uint32_t index = globals_size; globals[index].name = name; globals[index].type = type; globals[index].var_index = 0; globals[index].value.kind = GLOBAL_VALUE_NONE; + globals[index].attributes = attributes; globals_size += 1; return index; } -global_id add_global_with_value(type_id type, name_id name, global_value value) { +global_id add_global_with_value(type_id type, attribute_list attributes, name_id name, global_value value) { uint32_t index = globals_size; globals[index].name = name; globals[index].type = type; globals[index].var_index = 0; globals[index].value = value; + globals[index].attributes = attributes; globals_size += 1; return index; } diff --git a/Sources/globals.h b/Sources/globals.h index 9bf654a..95e21b9 100644 --- a/Sources/globals.h +++ b/Sources/globals.h @@ -30,12 +30,13 @@ typedef struct global { type_id type; uint64_t var_index; global_value value; + attribute_list attributes; } 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, global_value value); +global_id add_global(type_id type, attribute_list attributes, name_id name); +global_id add_global_with_value(type_id type, attribute_list attributes, name_id name, global_value value); global find_global(name_id name); diff --git a/Sources/parser.c b/Sources/parser.c index ea7a616..95b2c60 100644 --- a/Sources/parser.c +++ b/Sources/parser.c @@ -147,7 +147,7 @@ static void modifiers_add(modifiers_t *modifiers, modifier_t modifier) { static definition parse_struct(state_t *state); static definition parse_function(state_t *state); -static definition parse_const(state_t *state); +static definition parse_const(state_t *state, attribute_list attributes); static double attribute_parameter_to_number(name_id attribute_name, name_id parameter_name) { debug_context context = {0}; @@ -223,7 +223,7 @@ static definition parse_definition(state_t *state) { return d; } case TOKEN_CONST: { - definition d = parse_const(state); + definition d = parse_const(state, attributes); return d; } default: { @@ -1053,7 +1053,7 @@ static definition parse_function(state_t *state) { return d; } -static definition parse_const(state_t *state) { +static definition parse_const(state_t *state, attribute_list attributes) { advance_state(state); match_token(state, TOKEN_IDENTIFIER, "Expected an identifier"); @@ -1089,23 +1089,23 @@ static definition parse_const(state_t *state) { debug_context context = {0}; check(type != NO_TYPE, context, "Const has no type"); d.kind = DEFINITION_CONST_CUSTOM; - d.global = add_global(type, name.identifier); + d.global = add_global(type, attributes, name.identifier); } else if (type_name == add_name("tex2d")) { d.kind = DEFINITION_TEX2D; - d.global = add_global(tex2d_type_id, name.identifier); + d.global = add_global(tex2d_type_id, attributes, name.identifier); } else if (type_name == add_name("texcube")) { d.kind = DEFINITION_TEXCUBE; - d.global = add_global(texcube_type_id, name.identifier); + d.global = add_global(texcube_type_id, attributes, name.identifier); } else if (type_name == add_name("sampler")) { d.kind = DEFINITION_SAMPLER; - d.global = add_global(sampler_type_id, name.identifier); + d.global = add_global(sampler_type_id, attributes, name.identifier); } else if (type_name == add_name("bvh")) { d.kind = DEFINITION_BVH; - d.global = add_global(bvh_type_id, name.identifier); + d.global = add_global(bvh_type_id, attributes, name.identifier); } else if (type_name == add_name("float")) { debug_context context = {0}; @@ -1118,7 +1118,7 @@ static definition parse_const(state_t *state) { float_value.value.floats[0] = (float)value->number; d.kind = DEFINITION_CONST_BASIC; - d.global = add_global_with_value(float_id, name.identifier, float_value); + d.global = add_global_with_value(float_id, attributes, name.identifier, float_value); } else if (type_name == add_name("float2")) { debug_context context = {0}; @@ -1136,7 +1136,7 @@ static definition parse_const(state_t *state) { } d.kind = DEFINITION_CONST_BASIC; - d.global = add_global_with_value(float2_id, name.identifier, float2_value); + d.global = add_global_with_value(float2_id, attributes, name.identifier, float2_value); } else if (type_name == add_name("float3")) { debug_context context = {0}; @@ -1154,7 +1154,7 @@ static definition parse_const(state_t *state) { } d.kind = DEFINITION_CONST_BASIC; - d.global = add_global_with_value(float3_id, name.identifier, float3_value); + d.global = add_global_with_value(float3_id, attributes, name.identifier, float3_value); } else if (type_name == add_name("float4")) { debug_context context = {0}; @@ -1172,7 +1172,7 @@ static definition parse_const(state_t *state) { } d.kind = DEFINITION_CONST_BASIC; - d.global = add_global_with_value(float4_id, name.identifier, float4_value); + d.global = add_global_with_value(float4_id, attributes, name.identifier, float4_value); } else { debug_context context = {0};