Skip to content

Commit

Permalink
Parse descriptor set attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 3, 2024
1 parent 7122493 commit 5c1a5c7
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 227 deletions.
38 changes: 19 additions & 19 deletions Sources/backends/glsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,25 @@ static void write_globals(char *glsl, size_t *offset, function *main) {
find_referenced_globals(main, globals, &globals_size);

for (size_t i = 0; i < globals_size; ++i) {
global g = get_global(globals[i]);
global *g = get_global(globals[i]);
int register_index = global_register_indices[globals[i]];

if (g.type == sampler_type_id) {
if (g->type == sampler_type_id) {
}
else if (g.type == tex2d_type_id) {
*offset += sprintf(&glsl[*offset], "uniform sampler2D _%" PRIu64 ";\n\n", g.var_index);
else if (g->type == tex2d_type_id) {
*offset += sprintf(&glsl[*offset], "uniform sampler2D _%" PRIu64 ";\n\n", g->var_index);
}
else if (g.type == texcube_type_id) {
*offset += sprintf(&glsl[*offset], "uniform samplerCube _%" PRIu64 ";\n\n", g.var_index);
else if (g->type == texcube_type_id) {
*offset += sprintf(&glsl[*offset], "uniform samplerCube _%" PRIu64 ";\n\n", g->var_index);
}
else if (g.type == float_id) {
else if (g->type == float_id) {
}
else {
*offset += sprintf(&glsl[*offset], "layout(std140) uniform _%" PRIu64 " {\n", g.var_index);
type *t = get_type(g.type);
*offset += sprintf(&glsl[*offset], "layout(std140) uniform _%" PRIu64 " {\n", g->var_index);
type *t = get_type(g->type);
for (size_t i = 0; i < t->members.size; ++i) {
*offset +=
sprintf(&glsl[*offset], "\t%s _%" PRIu64 "_%s;\n", type_string(t->members.m[i].type.type), g.var_index, get_name(t->members.m[i].name));
sprintf(&glsl[*offset], "\t%s _%" PRIu64 "_%s;\n", type_string(t->members.m[i].type.type), g->var_index, get_name(t->members.m[i].name));
}
*offset += sprintf(&glsl[*offset], "};\n\n");
}
Expand Down Expand Up @@ -311,10 +311,10 @@ static void write_functions(char *code, size_t *offset, shader_stage stage, type
}
case OPCODE_LOAD_MEMBER: {
uint64_t global_var_index = 0;
for (global_id j = 0; get_global(j).type != NO_TYPE; ++j) {
global g = get_global(j);
if (o->op_load_member.from.index == g.var_index) {
global_var_index = g.var_index;
for (global_id j = 0; get_global(j)->type != NO_TYPE; ++j) {
global *g = get_global(j);
if (o->op_load_member.from.index == g->var_index) {
global_var_index = g->var_index;
break;
}
}
Expand Down Expand Up @@ -518,17 +518,17 @@ void glsl_export(char *directory) {

memset(global_register_indices, 0, sizeof(global_register_indices));

for (global_id i = 0; get_global(i).type != NO_TYPE; ++i) {
global g = get_global(i);
if (g.type == sampler_type_id) {
for (global_id i = 0; get_global(i)->type != NO_TYPE; ++i) {
global *g = get_global(i);
if (g->type == sampler_type_id) {
global_register_indices[i] = sampler_index;
sampler_index += 1;
}
else if (g.type == tex2d_type_id || g.type == texcube_type_id) {
else if (g->type == tex2d_type_id || g->type == texcube_type_id) {
global_register_indices[i] = texture_index;
texture_index += 1;
}
else if (g.type == float_id) {
else if (g->type == float_id) {
}
else {
global_register_indices[i] = cbuffer_index;
Expand Down
75 changes: 38 additions & 37 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../errors.h"
#include "../functions.h"
#include "../parser.h"
#include "../sets.h"
#include "../shader_stage.h"
#include "../types.h"
#include "cstyle.h"
Expand Down Expand Up @@ -190,47 +191,47 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function *
}

for (size_t i = 0; i < globals_size; ++i) {
global g = get_global(globals[i]);
global *g = get_global(globals[i]);
int register_index = global_register_indices[globals[i]];

if (g.type == sampler_type_id) {
*offset += sprintf(&hlsl[*offset], "SamplerState _%" PRIu64 " : register(s%i);\n\n", g.var_index, register_index);
if (g->type == sampler_type_id) {
*offset += sprintf(&hlsl[*offset], "SamplerState _%" PRIu64 " : register(s%i);\n\n", g->var_index, register_index);
}
else if (g.type == tex2d_type_id) {
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 if (g->type == tex2d_type_id) {
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);
*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);
else if (g->type == texcube_type_id) {
*offset += sprintf(&hlsl[*offset], "TextureCube<float4> _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index);
}
else if (g.type == bvh_type_id) {
*offset += sprintf(&hlsl[*offset], "RaytracingAccelerationStructure _%" PRIu64 " : register(t%i);\n\n", g.var_index, register_index);
else if (g->type == bvh_type_id) {
*offset += sprintf(&hlsl[*offset], "RaytracingAccelerationStructure _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index);
}
else if (g.type == float_id) {
*offset += sprintf(&hlsl[*offset], "static const float _%" PRIu64 " = %f;\n\n", g.var_index, g.value.value.floats[0]);
else if (g->type == float_id) {
*offset += sprintf(&hlsl[*offset], "static const float _%" PRIu64 " = %f;\n\n", g->var_index, g->value.value.floats[0]);
}
else if (g.type == float2_id) {
*offset += sprintf(&hlsl[*offset], "static const float2 _%" PRIu64 " = float2(%f, %f);\n\n", g.var_index, g.value.value.floats[0],
g.value.value.floats[1]);
else if (g->type == float2_id) {
*offset += sprintf(&hlsl[*offset], "static const float2 _%" PRIu64 " = float2(%f, %f);\n\n", g->var_index, g->value.value.floats[0],
g->value.value.floats[1]);
}
else if (g.type == float3_id) {
*offset += sprintf(&hlsl[*offset], "static const float3 _%" PRIu64 " = float3(%f, %f, %f);\n\n", g.var_index, g.value.value.floats[0],
g.value.value.floats[1], g.value.value.floats[2]);
else if (g->type == float3_id) {
*offset += sprintf(&hlsl[*offset], "static const float3 _%" PRIu64 " = float3(%f, %f, %f);\n\n", g->var_index, g->value.value.floats[0],
g->value.value.floats[1], g->value.value.floats[2]);
}
else if (g.type == float4_id) {
*offset += sprintf(&hlsl[*offset], "static const float4 _%" PRIu64 " = float4(%f, %f, %f, %f);\n\n", g.var_index, g.value.value.floats[0],
g.value.value.floats[1], g.value.value.floats[2], g.value.value.floats[3]);
else if (g->type == float4_id) {
*offset += sprintf(&hlsl[*offset], "static const float4 _%" PRIu64 " = float4(%f, %f, %f, %f);\n\n", g->var_index, g->value.value.floats[0],
g->value.value.floats[1], g->value.value.floats[2], g->value.value.floats[3]);
}
else {
*offset += sprintf(&hlsl[*offset], "cbuffer _%" PRIu64 " : register(b%i) {\n", g.var_index, register_index);
type *t = get_type(g.type);
*offset += sprintf(&hlsl[*offset], "cbuffer _%" PRIu64 " : register(b%i) {\n", g->var_index, register_index);
type *t = get_type(g->type);
for (size_t i = 0; i < t->members.size; ++i) {
*offset +=
sprintf(&hlsl[*offset], "\t%s _%" PRIu64 "_%s;\n", type_string(t->members.m[i].type.type), g.var_index, get_name(t->members.m[i].name));
sprintf(&hlsl[*offset], "\t%s _%" PRIu64 "_%s;\n", type_string(t->members.m[i].type.type), g->var_index, get_name(t->members.m[i].name));
}
*offset += sprintf(&hlsl[*offset], "}\n\n");
}
Expand Down Expand Up @@ -601,11 +602,11 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
switch (o->type) {
case OPCODE_LOAD_MEMBER: {
uint64_t global_var_index = 0;
for (global_id j = 0; get_global(j).type != NO_TYPE; ++j) {
global g = get_global(j);
if (o->op_load_member.from.index == g.var_index) {
global_var_index = g.var_index;
if (get_type(g.type)->built_in) {
for (global_id j = 0; get_global(j) != NULL && get_global(j)->type != NO_TYPE; ++j) {
global *g = get_global(j);
if (o->op_load_member.from.index == g->var_index) {
global_var_index = g->var_index;
if (get_type(g->type)->built_in) {
global_var_index = 0;
}
break;
Expand Down Expand Up @@ -1028,14 +1029,14 @@ void hlsl_export(char *directory, api_kind d3d) {

memset(global_register_indices, 0, sizeof(global_register_indices));

for (global_id i = 0; get_global(i).type != NO_TYPE; ++i) {
global g = get_global(i);
if (g.type == sampler_type_id) {
for (global_id i = 0; get_global(i) != NULL && get_global(i)->type != NO_TYPE; ++i) {
global *g = get_global(i);
if (g->type == sampler_type_id) {
global_register_indices[i] = sampler_index;
sampler_index += 1;
}
else if (g.type == tex2d_type_id) {
if (has_attribute(&g.attributes, add_name("write"))) {
else if (g->type == tex2d_type_id) {
if (has_attribute(&g->attributes, add_name("write"))) {
global_register_indices[i] = uav_index;
uav_index += 1;
}
Expand All @@ -1044,11 +1045,11 @@ 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 == bvh_type_id) {
global_register_indices[i] = srv_index;
srv_index += 1;
}
else if (g.type == float_id) {
else if (g->type == float_id) {
}
else {
global_register_indices[i] = cbv_index;
Expand Down
44 changes: 22 additions & 22 deletions Sources/backends/metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ static void write_types(char *metal, size_t *offset) {
char name[256];

bool found = false;
for (global_id j = 0; get_global(j).type != NO_TYPE; ++j) {
global g = get_global(j);
if (g.type == i) {
sprintf(name, "_%" PRIu64, g.var_index);
for (global_id j = 0; get_global(j)->type != NO_TYPE; ++j) {
global *g = get_global(j);
if (g->type == i) {
sprintf(name, "_%" PRIu64, g->var_index);
found = true;
break;
}
Expand Down Expand Up @@ -187,22 +187,22 @@ static void write_functions(char *code, size_t *offset) {
size_t buffers_offset = 0;

for (size_t i = 0; i < globals_size; ++i) {
global g = get_global(globals[i]);
global *g = get_global(globals[i]);
int register_index = global_register_indices[globals[i]];

if (g.type == sampler_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", sampler _%" PRIu64 " [[sampler(%i)]]", g.var_index, register_index);
if (g->type == sampler_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", sampler _%" PRIu64 " [[sampler(%i)]]", g->var_index, register_index);
}
else if (g.type == tex2d_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", texture2d<float> _%" PRIu64 " [[texture(%i)]]", g.var_index, register_index);
else if (g->type == tex2d_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", texture2d<float> _%" PRIu64 " [[texture(%i)]]", g->var_index, register_index);
}
else if (g.type == texcube_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", texturecube<float> _%" PRIu64 " [[texture(%i)]]", g.var_index, register_index);
else if (g->type == texcube_type_id) {
buffers_offset += sprintf(&buffers[buffers_offset], ", texturecube<float> _%" PRIu64 " [[texture(%i)]]", g->var_index, register_index);
}
else if (g.type == float_id) {
else if (g->type == float_id) {
}
else {
buffers_offset += sprintf(&buffers[buffers_offset], ", constant _%" PRIu64 "_type& _%" PRIu64 " [[buffer(%i)]]", g.var_index, g.var_index,
buffers_offset += sprintf(&buffers[buffers_offset], ", constant _%" PRIu64 "_type& _%" PRIu64 " [[buffer(%i)]]", g->var_index, g->var_index,
register_index);
}
}
Expand Down Expand Up @@ -262,10 +262,10 @@ static void write_functions(char *code, size_t *offset) {
switch (o->type) {
case OPCODE_LOAD_MEMBER: {
uint64_t global_var_index = 0;
for (global_id j = 0; get_global(j).type != NO_TYPE; ++j) {
global g = get_global(j);
if (o->op_load_member.from.index == g.var_index) {
global_var_index = g.var_index;
for (global_id j = 0; get_global(j)->type != NO_TYPE; ++j) {
global *g = get_global(j);
if (o->op_load_member.from.index == g->var_index) {
global_var_index = g->var_index;
break;
}
}
Expand Down Expand Up @@ -390,17 +390,17 @@ void metal_export(char *directory) {

memset(global_register_indices, 0, sizeof(global_register_indices));

for (global_id i = 0; get_global(i).type != NO_TYPE; ++i) {
global g = get_global(i);
if (g.type == sampler_type_id) {
for (global_id i = 0; get_global(i)->type != NO_TYPE; ++i) {
global *g = get_global(i);
if (g->type == sampler_type_id) {
global_register_indices[i] = sampler_index;
sampler_index += 1;
}
else if (g.type == tex2d_type_id || g.type == texcube_type_id) {
else if (g->type == tex2d_type_id || g->type == texcube_type_id) {
global_register_indices[i] = texture_index;
texture_index += 1;
}
else if (g.type == float_id) {
else if (g->type == float_id) {
}
else {
global_register_indices[i] = cbuffer_index;
Expand Down
6 changes: 3 additions & 3 deletions Sources/backends/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ void indent(char *code, size_t *offset, int indentation) {
}

static void find_referenced_global_for_var(variable v, global_id *globals, size_t *globals_size) {
for (global_id j = 0; get_global(j).type != NO_TYPE; ++j) {
global g = get_global(j);
if (v.index == g.var_index) {
for (global_id j = 0; get_global(j) != NULL && get_global(j)->type != NO_TYPE; ++j) {
global *g = get_global(j);
if (v.index == g->var_index) {
bool found = false;
for (size_t k = 0; k < *globals_size; ++k) {
if (globals[k] == j) {
Expand Down
Loading

0 comments on commit 5c1a5c7

Please sign in to comment.