iron_gpu.h 8.6 KB

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