Skip to content

Commit

Permalink
Handle OPCODE_LOAD_CONSTANT in SPIR-V
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 15, 2023
1 parent b42e2a4 commit 60fed56
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Sources/backends/spirv.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static void write_vertex_input_decorations(instructions_buffer *instructions, ui
}
}

uint32_t write_op_function(instructions_buffer *instructions, uint32_t result_type, function_control control, uint32_t function_type) {
static uint32_t write_op_function(instructions_buffer *instructions, uint32_t result_type, function_control control, uint32_t function_type) {
uint32_t result = allocate_index();

uint32_t operands[4];
Expand All @@ -413,7 +413,7 @@ uint32_t write_op_function(instructions_buffer *instructions, uint32_t result_ty
return result;
}

uint32_t write_label(instructions_buffer *instructions) {
static uint32_t write_label(instructions_buffer *instructions) {
uint32_t result = allocate_index();

uint32_t operands[1];
Expand Down Expand Up @@ -444,6 +444,20 @@ static uint32_t get_int_constant(int value) {
return index;
}

static struct {
float key;
uint32_t value;
} *float_constants = NULL;

static uint32_t get_float_constant(float value) {
uint32_t index = hmget(float_constants, value);
if (index == 0) {
index = allocate_index();
hmput(float_constants, value, index);
}
return index;
}

static uint32_t write_op_access_chain(instructions_buffer *instructions, uint32_t result_type, uint32_t base, int *indices, uint16_t indices_size) {
uint32_t pointer = allocate_index();

Expand Down Expand Up @@ -525,6 +539,8 @@ static void write_function(instructions_buffer *instructions, function *f) {
break;
}
case OPCODE_LOAD_CONSTANT: {
uint32_t spirv_id = get_float_constant(o->op_load_constant.number);
hmput(index_map, o->op_load_constant.to.index, spirv_id);
break;
}
case OPCODE_CALL: {
Expand Down Expand Up @@ -663,6 +679,7 @@ static void spirv_export_fragment(char *directory, function *main) {
void spirv_export(char *directory) {
hmdefault(index_map, 0);
hmdefault(int_constants, 0);
hmdefault(float_constants, 0);

function *vertex_shaders[256];
size_t vertex_shaders_size = 0;
Expand Down

0 comments on commit 60fed56

Please sign in to comment.