Skip to content

Commit

Permalink
Support UAV textures
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 27, 2024
1 parent bf0c62e commit f030ecf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 50 deletions.
32 changes: 24 additions & 8 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<float4> _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index);
if (has_attribute(&g.attributes, add_name("write"))) {
*offset += sprintf(&hlsl[*offset], "RWTexture2D<float4> _%" PRIu64 " : register(u%i);\n\n", g.var_index, register_index);
}
else {
*offset += sprintf(&hlsl[*offset], "Texture2D<float4> _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index);
}
}
else if (g.type == texcube_type_id) {
*offset += sprintf(&hlsl[*offset], "TextureCube<float4> _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index);
Expand Down Expand Up @@ -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));
Expand All @@ -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;
}
}

Expand Down
60 changes: 32 additions & 28 deletions Sources/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 12 additions & 12 deletions Sources/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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};
Expand All @@ -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};
Expand All @@ -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};
Expand All @@ -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};
Expand All @@ -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};
Expand Down

0 comments on commit f030ecf

Please sign in to comment.