Files
armorpaint/base/sources/iron_gpu.h
T

246 lines
8.9 KiB
C
Raw Normal View History

2025-02-26 08:47:09 +01:00
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <iron_global.h>
#include <iron_math.h>
#include <iron_gpu.h>
2025-03-06 20:18:45 +01:00
#include <iron_array.h>
2025-02-26 13:24:16 +01:00
#include BACKEND_GPU_H
2025-02-26 08:47:09 +01:00
2025-06-19 19:55:16 +02:00
#define GPU_MAX_VERTEX_ELEMENTS 16
2025-07-17 23:42:23 +02:00
#define GPU_MAX_TEXTURES 16
2025-08-22 17:15:49 +02:00
#define GPU_CONSTANT_BUFFER_SIZE 512
#define GPU_CONSTANT_BUFFER_MULTIPLE 2048
2025-08-04 17:41:10 +02:00
#ifdef IRON_ANDROID
#define GPU_FRAMEBUFFER_COUNT 7
#else
2025-07-24 15:15:00 +02:00
#define GPU_FRAMEBUFFER_COUNT 3
2025-08-04 17:41:10 +02:00
#endif
2025-02-26 08:47:09 +01:00
2025-09-26 10:48:18 +02:00
typedef enum gpu_clear {
GPU_CLEAR_NONE = 0,
GPU_CLEAR_COLOR = 1,
GPU_CLEAR_DEPTH = 2,
} gpu_clear_t;
2025-06-30 20:46:17 +02:00
typedef enum gpu_texture_state {
GPU_TEXTURE_STATE_SHADER_RESOURCE,
GPU_TEXTURE_STATE_RENDER_TARGET,
2025-07-11 12:54:08 +02:00
GPU_TEXTURE_STATE_RENDER_TARGET_DEPTH,
2025-06-30 20:46:17 +02:00
GPU_TEXTURE_STATE_PRESENT
} gpu_texture_state_t;
2025-03-09 17:02:12 +01:00
2025-06-30 20:46:17 +02:00
typedef enum gpu_texture_compression {
GPU_TEXTURE_COMPRESSION_NONE,
GPU_TEXTURE_COMPRESSION_DXT5,
GPU_TEXTURE_COMPRESSION_ASTC
} gpu_texture_compression_t;
typedef enum gpu_texture_format {
GPU_TEXTURE_FORMAT_RGBA32,
GPU_TEXTURE_FORMAT_RGBA64,
GPU_TEXTURE_FORMAT_RGBA128,
GPU_TEXTURE_FORMAT_R8,
GPU_TEXTURE_FORMAT_R16,
2025-07-01 14:46:40 +02:00
GPU_TEXTURE_FORMAT_R32,
GPU_TEXTURE_FORMAT_D32
2025-06-30 20:46:17 +02:00
} gpu_texture_format_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef enum gpu_vertex_data {
GPU_VERTEX_DATA_F32_1X,
GPU_VERTEX_DATA_F32_2X,
GPU_VERTEX_DATA_F32_3X,
GPU_VERTEX_DATA_F32_4X,
GPU_VERTEX_DATA_I16_2X_NORM,
2025-06-30 20:46:17 +02:00
GPU_VERTEX_DATA_I16_4X_NORM
2025-06-19 19:55:16 +02:00
} gpu_vertex_data_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef enum gpu_shader_type {
GPU_SHADER_TYPE_VERTEX,
GPU_SHADER_TYPE_FRAGMENT,
GPU_SHADER_TYPE_COUNT
} gpu_shader_type_t;
2025-04-19 16:55:29 +02:00
typedef enum {
2025-06-19 19:55:16 +02:00
GPU_BLEND_ONE,
GPU_BLEND_ZERO,
GPU_BLEND_SOURCE_ALPHA,
GPU_BLEND_DEST_ALPHA,
GPU_BLEND_INV_SOURCE_ALPHA,
GPU_BLEND_INV_DEST_ALPHA
} gpu_blending_factor_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef enum gpu_cull_mode {
GPU_CULL_MODE_CLOCKWISE,
GPU_CULL_MODE_COUNTERCLOCKWISE,
GPU_CULL_MODE_NEVER
} gpu_cull_mode_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef enum gpu_compare_mode {
GPU_COMPARE_MODE_ALWAYS,
GPU_COMPARE_MODE_NEVER,
2025-07-17 23:42:23 +02:00
GPU_COMPARE_MODE_LESS,
GPU_COMPARE_MODE_EQUAL
2025-06-19 19:55:16 +02:00
} gpu_compare_mode_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_texture {
2025-02-26 08:47:09 +01:00
int width;
int height;
2025-06-30 20:46:17 +02:00
gpu_texture_format_t format;
gpu_texture_compression_t compression;
gpu_texture_state_t state;
2025-03-06 20:18:45 +01:00
buffer_t *buffer;
2025-03-16 19:49:13 +01:00
gpu_texture_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_texture_t;
2025-02-26 08:47:09 +01:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_buffer {
2025-06-18 20:47:58 +02:00
int count;
2025-07-11 20:06:33 +02:00
int stride;
2025-08-12 23:26:06 +02:00
uint8_t *data; // constant buffer data
2025-03-18 10:39:23 +01:00
gpu_buffer_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_buffer_t;
2025-03-18 10:39:23 +01:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_vertex_element {
2025-04-19 16:55:29 +02:00
const char *name;
2025-06-19 19:55:16 +02:00
gpu_vertex_data_t data;
} gpu_vertex_element_t;
2025-02-26 08:47:09 +01:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_vertex_structure {
gpu_vertex_element_t elements[GPU_MAX_VERTEX_ELEMENTS];
2025-04-19 16:55:29 +02:00
int size;
2025-06-19 19:55:16 +02:00
} gpu_vertex_structure_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_shader {
2025-04-19 16:55:29 +02:00
gpu_shader_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_shader_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_pipeline {
gpu_vertex_structure_t *input_layout;
gpu_shader_t *vertex_shader;
gpu_shader_t *fragment_shader;
gpu_cull_mode_t cull_mode;
2025-04-19 16:55:29 +02:00
bool depth_write;
2025-06-19 19:55:16 +02:00
gpu_compare_mode_t depth_mode;
gpu_blending_factor_t blend_source;
gpu_blending_factor_t blend_destination;
gpu_blending_factor_t alpha_blend_source;
gpu_blending_factor_t alpha_blend_destination;
2025-06-18 20:47:58 +02:00
bool color_write_mask_red[8];
2025-04-19 16:55:29 +02:00
bool color_write_mask_green[8];
bool color_write_mask_blue[8];
bool color_write_mask_alpha[8];
2025-06-30 20:46:17 +02:00
gpu_texture_format_t color_attachment[8];
2025-04-19 16:55:29 +02:00
int color_attachment_count;
int depth_attachment_bits;
gpu_pipeline_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_pipeline_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_raytrace_pipeline {
2025-08-11 10:50:13 +02:00
gpu_buffer_t *constant_buffer;
2025-04-19 16:55:29 +02:00
gpu_raytrace_pipeline_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_raytrace_pipeline_t;
2025-04-19 16:55:29 +02:00
2025-06-19 19:55:16 +02:00
typedef struct gpu_raytrace_acceleration_structure {
2025-04-19 16:55:29 +02:00
gpu_raytrace_acceleration_structure_impl_t impl;
2025-06-19 19:55:16 +02:00
} gpu_raytrace_acceleration_structure_t;
2025-02-26 08:47:09 +01:00
2025-06-19 19:55:16 +02:00
int gpu_max_bound_textures(void);
2025-09-26 10:48:18 +02:00
void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, gpu_clear_t flags, unsigned color, float depth);
void gpu_begin_internal(gpu_clear_t flags, unsigned color, float depth);
2025-06-19 19:55:16 +02:00
void gpu_end(void);
2025-06-30 20:46:17 +02:00
void gpu_end_internal(void);
2025-07-12 10:58:05 +02:00
void gpu_execute_and_wait(void);
2025-06-19 19:55:16 +02:00
void gpu_present(void);
2025-07-11 20:06:33 +02:00
void gpu_present_internal(void);
2025-06-30 20:46:17 +02:00
void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after);
2025-07-01 14:46:40 +02:00
void gpu_create_framebuffers(int depth_buffer_bits);
2025-06-30 20:46:17 +02:00
void gpu_init(int depth_buffer_bits, bool vsync);
2025-06-19 19:55:16 +02:00
void gpu_init_internal(int depth_buffer_bits, bool vsync);
void gpu_destroy(void);
2025-04-24 22:19:44 +02:00
void gpu_draw(void);
2025-07-02 22:00:52 +02:00
void gpu_resize(int width, int height);
void gpu_resize_internal(int width, int height);
2025-06-19 09:20:00 +02:00
void gpu_set_int(int location, int value);
void gpu_set_int2(int location, int value1, int value2);
void gpu_set_int3(int location, int value1, int value2, int value3);
void gpu_set_int4(int location, int value1, int value2, int value3, int value4);
void gpu_set_ints(int location, int *values, int count);
void gpu_set_float(int location, float value);
void gpu_set_float2(int location, float value1, float value2);
void gpu_set_float3(int location, float value1, float value2, float value3);
void gpu_set_float4(int location, float value1, float value2, float value3, float value4);
void gpu_set_floats(int location, f32_array_t *values);
void gpu_set_bool(int location, bool value);
void gpu_set_matrix3(int location, iron_matrix3x3_t value);
void gpu_set_matrix4(int location, iron_matrix4x4_t value);
2025-03-10 20:38:53 +01:00
2025-06-19 19:55:16 +02:00
void gpu_vertex_structure_add(gpu_vertex_structure_t *structure, const char *name, gpu_vertex_data_t data);
2025-06-30 20:46:17 +02:00
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format);
2025-06-19 19:55:16 +02:00
void gpu_texture_destroy(gpu_texture_t *texture);
2025-08-14 00:25:01 +02:00
void gpu_texture_destroy_internal(gpu_texture_t *texture);
2025-07-01 14:46:40 +02:00
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format);
void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int framebuffer_index);
2025-06-19 19:55:16 +02:00
void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure);
2025-07-21 15:06:21 +02:00
void *gpu_vertex_buffer_lock(gpu_buffer_t *buffer);
2025-06-19 19:55:16 +02:00
void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer);
void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size);
void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count);
void gpu_constant_buffer_unlock(gpu_buffer_t *buffer);
void gpu_index_buffer_init(gpu_buffer_t *buffer, int count);
2025-06-19 23:30:32 +02:00
void gpu_buffer_destroy(gpu_buffer_t *buffer);
2025-08-14 00:25:01 +02:00
void gpu_buffer_destroy_internal(gpu_buffer_t *buffer);
2025-06-19 19:55:16 +02:00
void *gpu_index_buffer_lock(gpu_buffer_t *buffer);
void gpu_index_buffer_unlock(gpu_buffer_t *buffer);
2025-03-10 20:38:53 +01:00
2025-06-19 19:55:16 +02:00
void gpu_pipeline_init(gpu_pipeline_t *pipeline);
void gpu_pipeline_destroy(gpu_pipeline_t *pipeline);
2025-08-14 00:25:01 +02:00
void gpu_pipeline_destroy_internal(gpu_pipeline_t *pipeline);
2025-06-19 19:55:16 +02:00
void gpu_pipeline_compile(gpu_pipeline_t *pipeline);
void gpu_shader_init(gpu_shader_t *shader, const void *source, size_t length, gpu_shader_type_t type);
void gpu_shader_destroy(gpu_shader_t *shader);
2025-06-18 20:47:58 +02:00
2025-06-19 19:55:16 +02:00
void gpu_draw_internal();
void gpu_viewport(int x, int y, int width, int height);
void gpu_scissor(int x, int y, int width, int height);
void gpu_disable_scissor();
2025-06-19 23:30:32 +02:00
void gpu_set_pipeline(gpu_pipeline_t *pipeline);
2025-09-09 19:41:55 +02:00
void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline);
2025-06-19 19:55:16 +02:00
void gpu_set_vertex_buffer(gpu_buffer_t *buffer);
void gpu_set_index_buffer(gpu_buffer_t *buffer);
void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size);
2025-06-19 23:30:32 +02:00
void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data);
2025-06-19 19:55:16 +02:00
void gpu_set_texture(int unit, gpu_texture_t *texture);
2025-08-09 21:20:37 +02:00
void gpu_use_linear_sampling(bool b);
2025-08-24 10:30:41 +02:00
char *gpu_device_name();
2025-03-10 20:38:53 +01:00
2025-06-19 19:55:16 +02:00
bool gpu_raytrace_supported(void);
2025-06-19 23:30:32 +02:00
void gpu_raytrace_pipeline_init(gpu_raytrace_pipeline_t *pipeline, void *ray_shader, int ray_shader_size, gpu_buffer_t *constant_buffer);
2025-06-19 19:55:16 +02:00
void gpu_raytrace_pipeline_destroy(gpu_raytrace_pipeline_t *pipeline);
void gpu_raytrace_acceleration_structure_init(gpu_raytrace_acceleration_structure_t *accel);
2025-06-19 23:30:32 +02:00
void gpu_raytrace_acceleration_structure_add(gpu_raytrace_acceleration_structure_t *accel, gpu_buffer_t *vb, gpu_buffer_t *ib, iron_matrix4x4_t transform);
void gpu_raytrace_acceleration_structure_build(gpu_raytrace_acceleration_structure_t *accel, gpu_buffer_t *_vb_full, gpu_buffer_t *_ib_full);
2025-06-19 19:55:16 +02:00
void gpu_raytrace_acceleration_structure_destroy(gpu_raytrace_acceleration_structure_t *accel);
2025-06-19 23:30:32 +02:00
void gpu_raytrace_set_textures(gpu_texture_t *texpaint0, gpu_texture_t *texpaint1, gpu_texture_t *texpaint2, gpu_texture_t *texenv, gpu_texture_t *texsobol, gpu_texture_t *texscramble, gpu_texture_t *texrank);
2025-06-19 19:55:16 +02:00
void gpu_raytrace_set_acceleration_structure(gpu_raytrace_acceleration_structure_t *accel);
void gpu_raytrace_set_pipeline(gpu_raytrace_pipeline_t *pipeline);
2025-06-19 23:30:32 +02:00
void gpu_raytrace_set_target(gpu_texture_t *output);
2025-06-19 19:55:16 +02:00
void gpu_raytrace_dispatch_rays();
2025-04-19 16:55:29 +02:00
2025-07-21 15:36:06 +02:00
int gpu_vertex_data_size(gpu_vertex_data_t data);
int gpu_vertex_struct_size(gpu_vertex_structure_t *s);
int gpu_texture_format_size(gpu_texture_format_t format);
2025-06-30 20:46:17 +02:00
extern bool gpu_transpose_mat;
2025-06-30 20:50:53 +02:00
extern bool gpu_in_use;
2025-09-09 19:41:55 +02:00
extern gpu_texture_t *current_textures[GPU_MAX_TEXTURES];
2025-06-30 20:46:17 +02:00
extern gpu_texture_t *current_render_targets[8];
extern int current_render_targets_count;
2025-08-31 19:23:47 +02:00
extern gpu_texture_t *current_depth_buffer;
2025-09-09 19:41:55 +02:00
extern gpu_pipeline_t *current_pipeline;
2025-06-30 20:46:17 +02:00
extern int constant_buffer_index;
extern gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT];
2025-07-01 14:46:40 +02:00
extern gpu_texture_t framebuffer_depth;
2025-06-30 20:46:17 +02:00
extern int framebuffer_index;