Files
armorpaint/base/sources/iron_gpu.c
T

296 lines
10 KiB
C
Raw Normal View History

2025-02-26 08:47:09 +01:00
#include "iron_gpu.h"
2025-02-15 15:46:05 +01:00
#include <assert.h>
#include <string.h>
2025-02-26 08:47:09 +01:00
#include <stddef.h>
2025-03-06 22:21:05 +01:00
#include "iron_system.h"
#include "iron_math.h"
#include "iron_file.h"
2024-11-10 22:11:29 +01:00
2025-04-19 16:55:29 +02:00
#define CONSTANT_BUFFER_SIZE 256
2025-04-24 22:19:44 +02:00
#define CONSTANT_BUFFER_MULTIPLE 2048
2025-02-17 13:15:25 +01:00
#define FRAMEBUFFER_COUNT 2
2025-02-15 13:30:50 +01:00
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_t gpu_command_list;
static iron_gpu_buffer_t constant_buffer;
int constant_buffer_index = 0;
static int framebuffer_index;
static iron_gpu_texture_t framebuffers[FRAMEBUFFER_COUNT];
static iron_gpu_texture_t *render_targets[8];
2025-04-19 16:55:29 +02:00
static bool window_resized = false;
2025-02-15 15:46:05 +01:00
2025-04-24 22:19:44 +02:00
void gpu_internal_resize(int width, int height) {
2025-04-19 16:55:29 +02:00
window_resized = true;
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_internal_init_window(int depth_buffer_bits, bool vsync) {
iron_gpu_internal_init_window(depth_buffer_bits, vsync);
iron_gpu_command_list_init(&gpu_command_list);
framebuffer_index = -1;
2025-02-17 13:15:25 +01:00
for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) {
2025-04-24 22:19:44 +02:00
iron_gpu_render_target_init_framebuffer(&framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, depth_buffer_bits);
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
iron_gpu_constant_buffer_init(&constant_buffer, CONSTANT_BUFFER_SIZE * CONSTANT_BUFFER_MULTIPLE);
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_draw(void) {
iron_gpu_constant_buffer_unlock(&constant_buffer);
iron_gpu_command_list_set_constant_buffer(&gpu_command_list, &constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE);
iron_gpu_command_list_draw(&gpu_command_list);
++constant_buffer_index;
// if (constant_buffer_index == CONSTANT_BUFFER_MULTIPLE) {
// iron_gpu_command_list_end(&gpu_command_list);
// iron_gpu_command_list_wait(&gpu_command_list);
// iron_gpu_command_list_begin(&gpu_command_list);
// constant_buffer_index = 0;
// }
iron_gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE);
2024-11-10 22:11:29 +01:00
}
2025-03-10 20:38:53 +01:00
void gpu_begin() {
2025-04-24 22:19:44 +02:00
constant_buffer_index = 0;
iron_gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index, CONSTANT_BUFFER_SIZE);
framebuffer_index = (framebuffer_index + 1) % FRAMEBUFFER_COUNT;
2024-11-10 22:11:29 +01:00
2025-04-19 16:55:29 +02:00
// if (window_resized) {
// for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) {
2025-04-24 22:19:44 +02:00
// iron_gpu_texture_destroy(&framebuffers[i]);
2025-04-19 16:55:29 +02:00
// }
2025-04-24 22:19:44 +02:00
// framebuffer_index = 0;
2025-04-19 16:55:29 +02:00
// }
2024-11-10 22:11:29 +01:00
2025-04-24 22:19:44 +02:00
iron_gpu_begin(&framebuffers[framebuffer_index]);
2024-11-10 22:11:29 +01:00
2025-04-19 16:55:29 +02:00
// if (window_resized) {
// for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) {
2025-04-24 22:19:44 +02:00
// iron_gpu_render_target_init_framebuffer(&framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, 0);
2025-04-19 16:55:29 +02:00
// }
// window_resized = false;
// }
2024-11-10 22:11:29 +01:00
2025-04-24 22:19:44 +02:00
render_targets[0] = NULL;
iron_gpu_command_list_begin(&gpu_command_list);
iron_gpu_command_list_framebuffer_to_render_target_barrier(&gpu_command_list, &framebuffers[framebuffer_index]);
gpu_set_render_targets(NULL, 0, IRON_GPU_CLEAR_NONE, 0, 0);
2024-11-10 22:11:29 +01:00
}
2025-04-19 16:55:29 +02:00
void gpu_end() {
2025-04-24 22:19:44 +02:00
iron_gpu_constant_buffer_unlock(&constant_buffer);
iron_gpu_command_list_render_target_to_framebuffer_barrier(&gpu_command_list, &framebuffers[framebuffer_index]);
iron_gpu_command_list_end(&gpu_command_list);
iron_gpu_command_list_wait(&gpu_command_list);
2025-04-19 16:55:29 +02:00
iron_gpu_end();
}
2025-04-24 22:19:44 +02:00
void gpu_set_render_targets(iron_gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) {
iron_gpu_texture_t *_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
if (targets == NULL) {
_targets[0] = &framebuffers[framebuffer_index];
targets = _targets;
count = 1;
}
for (int i = 0; i < count; ++i) {
render_targets[i] = targets[i];
if (render_targets[i]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) {
iron_gpu_command_list_texture_to_render_target_barrier(&gpu_command_list, render_targets[i]);
render_targets[i]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}
}
iron_gpu_command_list_set_render_targets(&gpu_command_list, render_targets, count, flags, color, depth);
}
2025-03-10 20:38:53 +01:00
void gpu_viewport(int x, int y, int width, int height) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_viewport(&gpu_command_list, x, y, width, height);
2024-11-10 22:11:29 +01:00
}
2025-03-10 20:38:53 +01:00
void gpu_scissor(int x, int y, int width, int height) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_scissor(&gpu_command_list, x, y, width, height);
2024-11-10 22:11:29 +01:00
}
2025-03-10 20:38:53 +01:00
void gpu_disable_scissor(void) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_disable_scissor(&gpu_command_list);
2024-11-10 22:11:29 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_int(iron_gpu_constant_location_t *location, int value) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
ints[0] = value;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_int2(iron_gpu_constant_location_t *location, int value1, int value2) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
ints[0] = value1;
ints[1] = value2;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_int3(iron_gpu_constant_location_t *location, int value1, int value2, int value3) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
ints[0] = value1;
ints[1] = value2;
ints[2] = value3;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_int4(iron_gpu_constant_location_t *location, int value1, int value2, int value3, int value4) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
ints[0] = value1;
ints[1] = value2;
ints[2] = value3;
ints[3] = value4;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_ints(iron_gpu_constant_location_t *location, int *values, int count) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
for (int i = 0; i < count; ++i) {
ints[i] = values[i];
}
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_float(iron_gpu_constant_location_t *location, float value) {
2025-04-24 22:19:44 +02:00
float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
floats[0] = value;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_float2(iron_gpu_constant_location_t *location, float value1, float value2) {
2025-04-24 22:19:44 +02:00
float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
floats[0] = value1;
floats[1] = value2;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_float3(iron_gpu_constant_location_t *location, float value1, float value2, float value3) {
2025-04-24 22:19:44 +02:00
float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
floats[0] = value1;
floats[1] = value2;
floats[2] = value3;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_float4(iron_gpu_constant_location_t *location, float value1, float value2, float value3, float value4) {
2025-04-24 22:19:44 +02:00
float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
floats[0] = value1;
floats[1] = value2;
floats[2] = value3;
floats[3] = value4;
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_floats(iron_gpu_constant_location_t *location, f32_array_t *values) {
2025-04-24 22:19:44 +02:00
float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
for (int i = 0; i < values->length; ++i) {
floats[i] = values->buffer[i];
}
2025-02-17 13:15:25 +01:00
}
2025-03-18 12:14:21 +01:00
void gpu_set_bool(iron_gpu_constant_location_t *location, bool value) {
2025-04-24 22:19:44 +02:00
int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]);
2025-04-18 13:51:24 +02:00
ints[0] = value ? 1 : 0;
}
static void iron_internal_set_matrix3(uint8_t *constants, int offset, iron_matrix3x3_t *value) {
float *floats = (float *)(&constants[offset]);
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
floats[x + y * 4] = iron_matrix3x3_get(value, x, y);
}
}
}
static void iron_internal_set_matrix4(uint8_t *constants, int offset, iron_matrix4x4_t *value) {
float *floats = (float *)(&constants[offset]);
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 4; ++x) {
floats[x + y * 4] = iron_matrix4x4_get(value, x, y);
}
}
2025-02-17 13:15:25 +01:00
}
2025-04-19 16:55:29 +02:00
void gpu_set_matrix3(iron_gpu_constant_location_t *location, iron_matrix3x3_t value) {
2025-04-18 13:51:24 +02:00
if (iron_gpu_transpose_mat) {
2025-04-19 16:55:29 +02:00
iron_matrix3x3_t m = value;
iron_matrix3x3_transpose(&m);
2025-04-24 22:19:44 +02:00
iron_internal_set_matrix3(constant_buffer.data, location->impl.vertexOffset, &m);
2025-04-18 13:51:24 +02:00
}
else {
2025-04-24 22:19:44 +02:00
iron_internal_set_matrix3(constant_buffer.data, location->impl.vertexOffset, &value);
2025-04-18 13:51:24 +02:00
}
2025-02-17 13:15:25 +01:00
}
2025-04-19 16:55:29 +02:00
void gpu_set_matrix4(iron_gpu_constant_location_t *location, iron_matrix4x4_t value) {
2025-04-18 13:51:24 +02:00
if (iron_gpu_transpose_mat) {
2025-04-19 16:55:29 +02:00
iron_matrix4x4_t m = value;
iron_matrix4x4_transpose(&m);
2025-04-24 22:19:44 +02:00
iron_internal_set_matrix4(constant_buffer.data, location->impl.vertexOffset, &m);
2025-04-18 13:51:24 +02:00
}
else {
2025-04-24 22:19:44 +02:00
iron_internal_set_matrix4(constant_buffer.data, location->impl.vertexOffset, &value);
2024-11-10 22:11:29 +01:00
}
}
2025-03-18 10:39:23 +01:00
void gpu_set_vertex_buffer(iron_gpu_buffer_t *buffer) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_set_vertex_buffer(&gpu_command_list, buffer);
2024-11-10 22:11:29 +01:00
}
2025-03-18 10:39:23 +01:00
void gpu_set_index_buffer(iron_gpu_buffer_t *buffer) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_set_index_buffer(&gpu_command_list, buffer);
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_set_pipeline(iron_gpu_pipeline_t *pipeline) {
iron_gpu_command_list_set_pipeline(&gpu_command_list, pipeline);
2024-11-10 22:11:29 +01:00
}
2025-03-18 10:39:23 +01:00
void gpu_set_texture(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) {
2025-03-06 20:18:45 +01:00
if (!render_target->_uploaded) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_upload_texture(&gpu_command_list, render_target);
2025-03-06 20:18:45 +01:00
render_target->_uploaded = true;
}
2025-03-09 17:02:12 +01:00
if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_render_target_to_texture_barrier(&gpu_command_list, render_target);
2025-03-09 17:02:12 +01:00
render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE;
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_set_texture(&gpu_command_list, *unit, render_target);
2024-11-10 22:11:29 +01:00
}
2025-03-18 10:39:23 +01:00
void gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) {
2025-03-09 17:02:12 +01:00
if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) {
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_render_target_to_texture_barrier(&gpu_command_list, render_target);
2025-03-09 17:02:12 +01:00
render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE;
2024-11-10 22:11:29 +01:00
}
2025-04-24 22:19:44 +02:00
iron_gpu_command_list_set_texture_from_render_target_depth(&gpu_command_list, *unit, render_target);
2025-02-18 17:23:47 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data) {
iron_gpu_command_list_get_render_target_pixels(&gpu_command_list, render_target, data);
2025-02-18 17:23:47 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) {
iron_gpu_index_buffer_unlock(buffer);
iron_gpu_command_list_upload_index_buffer(&gpu_command_list, buffer);
2025-02-19 20:02:04 +01:00
}
2025-04-24 22:19:44 +02:00
void gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data) {
2025-03-03 22:50:40 +01:00
structure->elements[structure->size].name = name;
structure->elements[structure->size].data = data;
structure->size++;
2025-02-19 20:02:04 +01:00
}
2025-02-26 08:47:09 +01:00
2025-04-24 22:19:44 +02:00
void gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipe) {
2025-02-26 08:47:09 +01:00
pipe->input_layout = NULL;
pipe->vertex_shader = NULL;
pipe->fragment_shader = NULL;
2025-03-10 20:38:53 +01:00
pipe->cull_mode = IRON_GPU_CULL_MODE_NEVER;
2025-02-26 08:47:09 +01:00
pipe->depth_write = false;
2025-03-10 20:38:53 +01:00
pipe->depth_mode = IRON_GPU_COMPARE_MODE_ALWAYS;
pipe->blend_source = IRON_GPU_BLEND_ONE;
pipe->blend_destination = IRON_GPU_BLEND_ZERO;
pipe->blend_operation = IRON_GPU_BLENDOP_ADD;
pipe->alpha_blend_source = IRON_GPU_BLEND_ONE;
pipe->alpha_blend_destination = IRON_GPU_BLEND_ZERO;
pipe->alpha_blend_operation = IRON_GPU_BLENDOP_ADD;
2025-02-26 08:47:09 +01:00
for (int i = 0; i < 8; ++i) {
pipe->color_write_mask_red[i] = true;
pipe->color_write_mask_green[i] = true;
pipe->color_write_mask_blue[i] = true;
pipe->color_write_mask_alpha[i] = true;
2025-03-09 17:02:12 +01:00
pipe->color_attachment[i] = IRON_IMAGE_FORMAT_RGBA32;
2025-02-26 08:47:09 +01:00
}
pipe->color_attachment_count = 1;
pipe->depth_attachment_bits = 0;
}