iron_gpu.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #include <iron_array.h>
  3. #include <iron_global.h>
  4. #include <iron_gpu.h>
  5. #include <iron_math.h>
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include BACKEND_GPU_H
  10. #define GPU_MAX_VERTEX_ELEMENTS 16
  11. #define GPU_MAX_TEXTURES 16
  12. #define GPU_CONSTANT_BUFFER_SIZE 512
  13. #define GPU_CONSTANT_BUFFER_MULTIPLE 2048
  14. #ifdef IRON_ANDROID
  15. #define GPU_FRAMEBUFFER_COUNT 7
  16. #elif defined(IRON_WASM)
  17. #define GPU_FRAMEBUFFER_COUNT 1
  18. #else
  19. #define GPU_FRAMEBUFFER_COUNT 3
  20. #endif
  21. typedef enum {
  22. GPU_CLEAR_NONE = 0,
  23. GPU_CLEAR_COLOR = 1,
  24. GPU_CLEAR_DEPTH = 2,
  25. } gpu_clear_t;
  26. typedef enum {
  27. GPU_TEXTURE_STATE_SHADER_RESOURCE,
  28. GPU_TEXTURE_STATE_RENDER_TARGET,
  29. GPU_TEXTURE_STATE_RENDER_TARGET_DEPTH,
  30. GPU_TEXTURE_STATE_PRESENT
  31. } gpu_texture_state_t;
  32. typedef enum {
  33. GPU_TEXTURE_COMPRESSION_NONE,
  34. GPU_TEXTURE_COMPRESSION_DXT5,
  35. GPU_TEXTURE_COMPRESSION_ASTC
  36. } gpu_texture_compression_t;
  37. typedef enum {
  38. GPU_TEXTURE_FORMAT_RGBA32,
  39. GPU_TEXTURE_FORMAT_RGBA64,
  40. GPU_TEXTURE_FORMAT_RGBA128,
  41. GPU_TEXTURE_FORMAT_R8,
  42. GPU_TEXTURE_FORMAT_R16,
  43. GPU_TEXTURE_FORMAT_R32,
  44. GPU_TEXTURE_FORMAT_D32
  45. } gpu_texture_format_t;
  46. typedef enum {
  47. GPU_VERTEX_DATA_F32_1X,
  48. GPU_VERTEX_DATA_F32_2X,
  49. GPU_VERTEX_DATA_F32_3X,
  50. GPU_VERTEX_DATA_F32_4X,
  51. GPU_VERTEX_DATA_I16_2X_NORM,
  52. GPU_VERTEX_DATA_I16_4X_NORM
  53. } gpu_vertex_data_t;
  54. typedef enum {
  55. GPU_SHADER_TYPE_VERTEX,
  56. GPU_SHADER_TYPE_FRAGMENT,
  57. GPU_SHADER_TYPE_COUNT
  58. } gpu_shader_type_t;
  59. typedef enum {
  60. GPU_BLEND_ONE,
  61. GPU_BLEND_ZERO,
  62. GPU_BLEND_SOURCE_ALPHA,
  63. GPU_BLEND_DEST_ALPHA,
  64. GPU_BLEND_INV_SOURCE_ALPHA,
  65. GPU_BLEND_INV_DEST_ALPHA
  66. } gpu_blend_t;
  67. typedef enum {
  68. GPU_CULL_MODE_CLOCKWISE,
  69. GPU_CULL_MODE_COUNTER_CLOCKWISE,
  70. GPU_CULL_MODE_NONE
  71. } gpu_cull_mode_t;
  72. typedef enum {
  73. GPU_COMPARE_MODE_ALWAYS,
  74. GPU_COMPARE_MODE_NEVER,
  75. GPU_COMPARE_MODE_LESS,
  76. GPU_COMPARE_MODE_EQUAL
  77. } gpu_compare_mode_t;
  78. typedef struct gpu_texture {
  79. int width;
  80. int height;
  81. gpu_texture_format_t format;
  82. gpu_texture_compression_t compression;
  83. gpu_texture_state_t state;
  84. buffer_t *buffer;
  85. gpu_texture_impl_t impl;
  86. } gpu_texture_t;
  87. typedef struct gpu_buffer {
  88. int count;
  89. int stride;
  90. uint8_t *data; // constant buffer data
  91. gpu_buffer_impl_t impl;
  92. } gpu_buffer_t;
  93. typedef struct gpu_vertex_element {
  94. const char *name;
  95. gpu_vertex_data_t data;
  96. } gpu_vertex_element_t;
  97. typedef struct gpu_vertex_structure {
  98. gpu_vertex_element_t elements[GPU_MAX_VERTEX_ELEMENTS];
  99. int size;
  100. } gpu_vertex_structure_t;
  101. typedef struct gpu_shader {
  102. gpu_shader_impl_t impl;
  103. } gpu_shader_t;
  104. typedef struct gpu_pipeline {
  105. gpu_vertex_structure_t *input_layout;
  106. gpu_shader_t *vertex_shader;
  107. gpu_shader_t *fragment_shader;
  108. gpu_cull_mode_t cull_mode;
  109. bool depth_write;
  110. gpu_compare_mode_t depth_mode;
  111. gpu_blend_t blend_source;
  112. gpu_blend_t blend_destination;
  113. gpu_blend_t alpha_blend_source;
  114. gpu_blend_t alpha_blend_destination;
  115. bool color_write_mask_red[8];
  116. bool color_write_mask_green[8];
  117. bool color_write_mask_blue[8];
  118. bool color_write_mask_alpha[8];
  119. gpu_texture_format_t color_attachment[8];
  120. int color_attachment_count;
  121. int depth_attachment_bits;
  122. gpu_pipeline_impl_t impl;
  123. } gpu_pipeline_t;
  124. typedef struct gpu_raytrace_pipeline {
  125. gpu_buffer_t *constant_buffer;
  126. gpu_raytrace_pipeline_impl_t impl;
  127. } gpu_raytrace_pipeline_t;
  128. typedef struct gpu_raytrace_acceleration_structure {
  129. gpu_raytrace_acceleration_structure_impl_t impl;
  130. } gpu_raytrace_acceleration_structure_t;
  131. int gpu_max_bound_textures(void);
  132. void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, gpu_clear_t flags, unsigned color, float depth);
  133. void gpu_begin_internal(gpu_clear_t flags, unsigned color, float depth);
  134. void gpu_end(void);
  135. void gpu_end_internal(void);
  136. void gpu_execute_and_wait(void);
  137. void gpu_present(void);
  138. void gpu_present_internal(void);
  139. void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after);
  140. void gpu_create_framebuffers(int depth_buffer_bits);
  141. void gpu_init(int depth_buffer_bits, bool vsync);
  142. void gpu_init_internal(int depth_buffer_bits, bool vsync);
  143. void gpu_destroy(void);
  144. void gpu_draw(void);
  145. void gpu_resize(int width, int height);
  146. void gpu_resize_internal(int width, int height);
  147. void gpu_set_int(int location, int value);
  148. void gpu_set_int2(int location, int value1, int value2);
  149. void gpu_set_int3(int location, int value1, int value2, int value3);
  150. void gpu_set_int4(int location, int value1, int value2, int value3, int value4);
  151. void gpu_set_ints(int location, int *values, int count);
  152. void gpu_set_float(int location, float value);
  153. void gpu_set_float2(int location, float value1, float value2);
  154. void gpu_set_float3(int location, float value1, float value2, float value3);
  155. void gpu_set_float4(int location, float value1, float value2, float value3, float value4);
  156. void gpu_set_floats(int location, f32_array_t *values);
  157. void gpu_set_bool(int location, bool value);
  158. void gpu_set_matrix3(int location, iron_matrix3x3_t value);
  159. void gpu_set_matrix4(int location, iron_matrix4x4_t value);
  160. void gpu_vertex_structure_add(gpu_vertex_structure_t *structure, const char *name, gpu_vertex_data_t data);
  161. void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format);
  162. void gpu_texture_destroy(gpu_texture_t *texture);
  163. void gpu_texture_destroy_internal(gpu_texture_t *texture);
  164. void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format);
  165. void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int framebuffer_index);
  166. void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure);
  167. void *gpu_vertex_buffer_lock(gpu_buffer_t *buffer);
  168. void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer);
  169. void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size);
  170. void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count);
  171. void gpu_constant_buffer_unlock(gpu_buffer_t *buffer);
  172. void gpu_index_buffer_init(gpu_buffer_t *buffer, int count);
  173. void gpu_buffer_destroy(gpu_buffer_t *buffer);
  174. void gpu_buffer_destroy_internal(gpu_buffer_t *buffer);
  175. void *gpu_index_buffer_lock(gpu_buffer_t *buffer);
  176. void gpu_index_buffer_unlock(gpu_buffer_t *buffer);
  177. void gpu_pipeline_init(gpu_pipeline_t *pipeline);
  178. void gpu_pipeline_destroy(gpu_pipeline_t *pipeline);
  179. void gpu_pipeline_destroy_internal(gpu_pipeline_t *pipeline);
  180. void gpu_pipeline_compile(gpu_pipeline_t *pipeline);
  181. void gpu_shader_init(gpu_shader_t *shader, const void *source, size_t length, gpu_shader_type_t type);
  182. void gpu_shader_destroy(gpu_shader_t *shader);
  183. void gpu_draw_internal();
  184. void gpu_viewport(int x, int y, int width, int height);
  185. void gpu_scissor(int x, int y, int width, int height);
  186. void gpu_disable_scissor();
  187. void gpu_set_pipeline(gpu_pipeline_t *pipeline);
  188. void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline);
  189. void gpu_set_vertex_buffer(gpu_buffer_t *buffer);
  190. void gpu_set_index_buffer(gpu_buffer_t *buffer);
  191. void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size);
  192. void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data);
  193. void gpu_set_texture(int unit, gpu_texture_t *texture);
  194. void gpu_use_linear_sampling(bool b);
  195. char *gpu_device_name();
  196. bool gpu_raytrace_supported(void);
  197. void gpu_raytrace_pipeline_init(gpu_raytrace_pipeline_t *pipeline, void *ray_shader, int ray_shader_size, gpu_buffer_t *constant_buffer);
  198. void gpu_raytrace_pipeline_destroy(gpu_raytrace_pipeline_t *pipeline);
  199. void gpu_raytrace_acceleration_structure_init(gpu_raytrace_acceleration_structure_t *accel);
  200. void gpu_raytrace_acceleration_structure_add(gpu_raytrace_acceleration_structure_t *accel, gpu_buffer_t *vb, gpu_buffer_t *ib, iron_matrix4x4_t transform);
  201. void gpu_raytrace_acceleration_structure_build(gpu_raytrace_acceleration_structure_t *accel, gpu_buffer_t *_vb_full, gpu_buffer_t *_ib_full);
  202. void gpu_raytrace_acceleration_structure_destroy(gpu_raytrace_acceleration_structure_t *accel);
  203. 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,
  204. gpu_texture_t *texscramble, gpu_texture_t *texrank);
  205. void gpu_raytrace_set_acceleration_structure(gpu_raytrace_acceleration_structure_t *accel);
  206. void gpu_raytrace_set_pipeline(gpu_raytrace_pipeline_t *pipeline);
  207. void gpu_raytrace_set_target(gpu_texture_t *output);
  208. void gpu_raytrace_dispatch_rays();
  209. void _gpu_raytrace_init(buffer_t *shader);
  210. void _gpu_raytrace_as_init();
  211. void _gpu_raytrace_as_add(gpu_buffer_t *vb, gpu_buffer_t *ib, iron_matrix4x4_t transform);
  212. void _gpu_raytrace_as_build(gpu_buffer_t *vb_full, gpu_buffer_t *ib_full);
  213. void gpu_raytrace_set_textures(gpu_texture_t *tex0, gpu_texture_t *tex1, gpu_texture_t *tex2, gpu_texture_t *texenv, gpu_texture_t *texsobol,
  214. gpu_texture_t *texscramble, gpu_texture_t *texrank);
  215. void _gpu_raytrace_dispatch_rays(gpu_texture_t *render_target, buffer_t *buffer);
  216. int gpu_vertex_data_size(gpu_vertex_data_t data);
  217. int gpu_vertex_struct_size(gpu_vertex_structure_t *s);
  218. int gpu_texture_format_size(gpu_texture_format_t format);
  219. extern bool gpu_transpose_mat;
  220. extern bool gpu_in_use;
  221. extern gpu_texture_t *current_textures[GPU_MAX_TEXTURES];
  222. extern gpu_texture_t *current_render_targets[8];
  223. extern int current_render_targets_count;
  224. extern gpu_texture_t *current_depth_buffer;
  225. extern gpu_pipeline_t *current_pipeline;
  226. extern int constant_buffer_index;
  227. extern gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT];
  228. extern gpu_texture_t framebuffer_depth;
  229. extern int framebuffer_index;