Skip to content

Commit

Permalink
Start supporting texture arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 22, 2024
1 parent 080d96a commit 47541dc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
7 changes: 6 additions & 1 deletion Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function *
*offset += sprintf(&hlsl[*offset], "Texture2D<float4> _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index);
}
}
else if (g->type == tex2darray_type_id) {
*offset += sprintf(&hlsl[*offset], "Texture2DArray<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 @@ -393,6 +396,7 @@ static void write_root_signature(char *hlsl, size_t *offset) {
switch (def->kind) {
case DEFINITION_CONST_CUSTOM:
case DEFINITION_TEX2D:
case DEFINITION_TEX2DARRAY:
case DEFINITION_TEXCUBE:
has_other = true;
break;
Expand Down Expand Up @@ -421,6 +425,7 @@ static void write_root_signature(char *hlsl, size_t *offset) {
cbv_index += 1;
break;
case DEFINITION_TEX2D:
case DEFINITION_TEX2DARRAY:
case DEFINITION_TEXCUBE:
if (first) {
first = false;
Expand Down Expand Up @@ -1377,7 +1382,7 @@ void hlsl_export(char *directory, api_kind d3d) {
srv_index += 1;
}
}
else if (g->type == texcube_type_id || g->type == bvh_type_id) {
else if (g->type == texcube_type_id || g->type == tex2darray_type_id || g->type == bvh_type_id) {
global_register_indices[i] = srv_index;
srv_index += 1;
}
Expand Down
55 changes: 34 additions & 21 deletions Sources/integrations/kope.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ static const char *convert_blend_mode(int mode) {
}
}

bool is_texture(type_id t) {
return t == tex2d_type_id || t == tex2darray_type_id || t == texcube_type_id;
}

#define CASE_TEXTURE \
case DEFINITION_TEX2D: \
case DEFINITION_TEX2DARRAY: \
case DEFINITION_TEXCUBE

static const char *convert_blend_op(int op) {
switch (op) {
case 0:
Expand Down Expand Up @@ -281,8 +290,7 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se

switch (def->kind) {
case DEFINITION_CONST_CUSTOM:
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
case DEFINITION_BVH:
has_other = true;
break;
Expand Down Expand Up @@ -315,8 +323,7 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se

switch (def->kind) {
case DEFINITION_CONST_CUSTOM:
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
case DEFINITION_BVH:
has_other = true;
break;
Expand All @@ -334,8 +341,7 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se

switch (def->kind) {
case DEFINITION_CONST_CUSTOM:
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
case DEFINITION_BVH: {
count += 1;
break;
Expand All @@ -362,8 +368,7 @@ static void write_root_signature(FILE *output, descriptor_set *all_descriptor_se
range_index += 1;

break;
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE: {
CASE_TEXTURE: {
attribute *write_attribute = find_attribute(&get_global(def->global)->attributes, add_name("write"));

if (write_attribute != NULL) {
Expand Down Expand Up @@ -478,7 +483,7 @@ void kope_export(char *directory, api_kind api) {
global_register_indices[i] = binding_index;
binding_index += 1;
}
else if (g->type == tex2d_type_id || g->type == texcube_type_id) {
else if (is_texture(g->type)) {
global_register_indices[i] = binding_index;
binding_index += 1;
}
Expand All @@ -501,7 +506,7 @@ void kope_export(char *directory, api_kind api) {
global_register_indices[i] = sampler_index;
sampler_index += 1;
}
else if (g->type == tex2d_type_id || g->type == texcube_type_id) {
else if (is_texture(g->type)) {
global_register_indices[i] = texture_index;
texture_index += 1;
}
Expand Down Expand Up @@ -586,7 +591,7 @@ void kope_export(char *directory, api_kind api) {

for (global_id i = 0; get_global(i) != NULL && get_global(i)->type != NO_TYPE; ++i) {
global *g = get_global(i);
if (g->type == tex2d_type_id || g->type == texcube_type_id || g->type == sampler_type_id) {
if (is_texture(g->type) || g->type == sampler_type_id) {
}
else if (!get_type(g->type)->built_in) {
type *t = get_type(g->type);
Expand Down Expand Up @@ -659,8 +664,7 @@ void kope_export(char *directory, api_kind api) {
case DEFINITION_CONST_CUSTOM:
fprintf(output, "\tkope_g5_buffer *%s;\n", get_name(get_global(d.global)->name));
break;
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
fprintf(output, "\tkope_g5_texture *%s;\n", get_name(get_global(d.global)->name));

attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write"));
Expand Down Expand Up @@ -698,8 +702,7 @@ void kope_export(char *directory, api_kind api) {
case DEFINITION_BVH:
fprintf(output, "\tkope_g5_raytracing_hierarchy *%s;\n", get_name(get_global(d.global)->name));
break;
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
fprintf(output, "\tkope_g5_texture *%s;\n", get_name(get_global(d.global)->name));

attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write"));
Expand Down Expand Up @@ -881,7 +884,7 @@ void kope_export(char *directory, api_kind api) {

for (global_id i = 0; get_global(i) != NULL && get_global(i)->type != NO_TYPE; ++i) {
global *g = get_global(i);
if (g->type != tex2d_type_id && g->type != texcube_type_id && g->type != sampler_type_id && !get_type(g->type)->built_in) {
if (!get_type(g->type)->built_in) {
type *t = get_type(g->type);

char type_name[256];
Expand Down Expand Up @@ -989,8 +992,7 @@ void kope_export(char *directory, api_kind api) {
case DEFINITION_CONST_CUSTOM:
other_count += 1;
break;
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE:
CASE_TEXTURE:
other_count += 1;
break;
case DEFINITION_SAMPLER:
Expand Down Expand Up @@ -1047,6 +1049,18 @@ void kope_export(char *directory, api_kind api) {
other_index += 1;
break;
}
case DEFINITION_TEX2DARRAY: {
attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write"));
if (write_attribute != NULL) {
debug_context context = {0};
error(context, "Texture arrays can not be writable");
}
fprintf(output, "\tkope_d3d12_descriptor_set_set_texture_array_view_srv(device, &set->set, parameters->%s, %" PRIu64 ");\n",
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;
break;
}
case DEFINITION_TEXCUBE: {
attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write"));
if (write_attribute != NULL) {
Expand Down Expand Up @@ -1076,8 +1090,7 @@ void kope_export(char *directory, api_kind api) {
case DEFINITION_CONST_CUSTOM:
fprintf(output, "\tkope_d3d12_descriptor_set_prepare_cbv_buffer(list, set->%s);\n", get_name(get_global(d.global)->name));
break;
case DEFINITION_TEX2D:
case DEFINITION_TEXCUBE: {
CASE_TEXTURE: {
attribute *write_attribute = find_attribute(&get_global(d.global)->attributes, add_name("write"));
if (write_attribute != NULL) {
fprintf(output, "\tkope_d3d12_descriptor_set_prepare_uav_texture(list, set->%s, set->%s_mip_level);\n",
Expand Down Expand Up @@ -1435,7 +1448,7 @@ void kope_export(char *directory, api_kind api) {
global *g = get_global(globals[i]);
if (g->type == sampler_type_id) {
}
else if (g->type == tex2d_type_id || g->type == texcube_type_id) {
else if (is_texture(g->type)) {
}
else if (g->type == float_id) {
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,10 @@ static definition parse_const(state_t *state, attribute_list attributes) {
d.kind = DEFINITION_TEX2D;
d.global = add_global(tex2d_type_id, attributes, name.identifier);
}
else if (type_name == add_name("tex2darray")) {
d.kind = DEFINITION_TEX2DARRAY;
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, attributes, name.identifier);
Expand Down
1 change: 1 addition & 0 deletions Sources/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ typedef struct definition {
DEFINITION_FUNCTION,
DEFINITION_STRUCT,
DEFINITION_TEX2D,
DEFINITION_TEX2DARRAY,
DEFINITION_TEXCUBE,
DEFINITION_SAMPLER,
DEFINITION_CONST_CUSTOM,
Expand Down
3 changes: 3 additions & 0 deletions Sources/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type_id bool3_id;
type_id bool4_id;
type_id function_type_id;
type_id tex2d_type_id;
type_id tex2darray_type_id;
type_id texcube_type_id;
type_id sampler_type_id;
type_id ray_type_id;
Expand Down Expand Up @@ -449,6 +450,8 @@ void types_init(void) {
get_type(sampler_type_id)->built_in = true;
tex2d_type_id = add_type(add_name("tex2d"));
get_type(tex2d_type_id)->built_in = true;
tex2darray_type_id = add_type(add_name("tex2darray"));
get_type(tex2darray_type_id)->built_in = true;
texcube_type_id = add_type(add_name("texcube"));
get_type(texcube_type_id)->built_in = true;

Expand Down
1 change: 1 addition & 0 deletions Sources/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ extern type_id uint3_id;
extern type_id uint4_id;
extern type_id bool_id;
extern type_id tex2d_type_id;
extern type_id tex2darray_type_id;
extern type_id texcube_type_id;
extern type_id sampler_type_id;
extern type_id ray_type_id;
Expand Down

0 comments on commit 47541dc

Please sign in to comment.