iron_gpu.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "iron_gpu.h"
  2. #include <iron_system.h>
  3. static gpu_buffer_t constant_buffer;
  4. static bool gpu_thrown = false;
  5. int constant_buffer_index = 0;
  6. int draw_calls = 0;
  7. int draw_calls_last = 0;
  8. bool gpu_in_use = false;
  9. gpu_texture_t *current_render_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  10. int current_render_targets_count = 0;
  11. gpu_texture_t *current_depth_buffer = NULL;
  12. gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT];
  13. gpu_texture_t framebuffer_depth;
  14. int framebuffer_index = 0;
  15. void gpu_init(int depth_buffer_bits, bool vsync) {
  16. gpu_init_internal(depth_buffer_bits, vsync);
  17. gpu_constant_buffer_init(&constant_buffer, GPU_CONSTANT_BUFFER_SIZE * GPU_CONSTANT_BUFFER_MULTIPLE);
  18. gpu_constant_buffer_lock(&constant_buffer, 0, GPU_CONSTANT_BUFFER_SIZE);
  19. }
  20. void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) {
  21. if (gpu_in_use && !gpu_thrown) {
  22. gpu_thrown = true;
  23. iron_log("End before you begin");
  24. }
  25. gpu_in_use = true;
  26. if (current_render_targets_count > 0 && current_render_targets[0] != &framebuffers[framebuffer_index]) {
  27. for (int i = 0; i < current_render_targets_count; ++i) {
  28. gpu_barrier(current_render_targets[i], GPU_TEXTURE_STATE_SHADER_RESOURCE);
  29. }
  30. }
  31. if (current_depth_buffer != NULL) {
  32. gpu_barrier(current_depth_buffer, GPU_TEXTURE_STATE_SHADER_RESOURCE);
  33. }
  34. if (targets == NULL) {
  35. current_render_targets[0] = &framebuffers[framebuffer_index];
  36. current_render_targets_count = 1;
  37. }
  38. else {
  39. for (int i = 0; i < count; ++i) {
  40. current_render_targets[i] = targets[i];
  41. }
  42. current_render_targets_count = count;
  43. }
  44. current_depth_buffer = depth_buffer;
  45. for (int i = 0; i < current_render_targets_count; ++i) {
  46. gpu_barrier(current_render_targets[i], GPU_TEXTURE_STATE_RENDER_TARGET);
  47. }
  48. if (current_depth_buffer != NULL) {
  49. gpu_barrier(current_depth_buffer, GPU_TEXTURE_STATE_RENDER_TARGET_DEPTH);
  50. }
  51. gpu_begin_internal(targets, count, depth_buffer, flags, color, depth);
  52. }
  53. void gpu_draw() {
  54. gpu_constant_buffer_unlock(&constant_buffer);
  55. gpu_set_constant_buffer(&constant_buffer, constant_buffer_index * GPU_CONSTANT_BUFFER_SIZE, GPU_CONSTANT_BUFFER_SIZE);
  56. gpu_draw_internal();
  57. constant_buffer_index++;
  58. if (constant_buffer_index >= GPU_CONSTANT_BUFFER_MULTIPLE) {
  59. constant_buffer_index = 0;
  60. }
  61. draw_calls++;
  62. if (draw_calls + draw_calls_last >= GPU_CONSTANT_BUFFER_MULTIPLE) {
  63. draw_calls = draw_calls_last = constant_buffer_index = 0;
  64. gpu_execute_and_wait();
  65. }
  66. gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index * GPU_CONSTANT_BUFFER_SIZE, GPU_CONSTANT_BUFFER_SIZE);
  67. }
  68. void gpu_end() {
  69. if (!gpu_in_use && !gpu_thrown) {
  70. gpu_thrown = true;
  71. iron_log("Begin before you end");
  72. }
  73. gpu_in_use = false;
  74. gpu_end_internal();
  75. }
  76. void gpu_present() {
  77. gpu_present_internal();
  78. draw_calls_last = draw_calls;
  79. draw_calls = 0;
  80. }
  81. void gpu_resize(int width, int height) {
  82. if (width == 0 || height == 0) {
  83. return;
  84. }
  85. if (width == framebuffers[0].width && height == framebuffers[0].height) {
  86. return;
  87. }
  88. gpu_resize_internal(width, height);
  89. }
  90. void gpu_set_int(int location, int value) {
  91. int *ints = (int *)(&constant_buffer.data[location]);
  92. ints[0] = value;
  93. }
  94. void gpu_set_int2(int location, int value1, int value2) {
  95. int *ints = (int *)(&constant_buffer.data[location]);
  96. ints[0] = value1;
  97. ints[1] = value2;
  98. }
  99. void gpu_set_int3(int location, int value1, int value2, int value3) {
  100. int *ints = (int *)(&constant_buffer.data[location]);
  101. ints[0] = value1;
  102. ints[1] = value2;
  103. ints[2] = value3;
  104. }
  105. void gpu_set_int4(int location, int value1, int value2, int value3, int value4) {
  106. int *ints = (int *)(&constant_buffer.data[location]);
  107. ints[0] = value1;
  108. ints[1] = value2;
  109. ints[2] = value3;
  110. ints[3] = value4;
  111. }
  112. void gpu_set_ints(int location, int *values, int count) {
  113. int *ints = (int *)(&constant_buffer.data[location]);
  114. for (int i = 0; i < count; ++i) {
  115. ints[i] = values[i];
  116. }
  117. }
  118. void gpu_set_float(int location, float value) {
  119. float *floats = (float *)(&constant_buffer.data[location]);
  120. floats[0] = value;
  121. }
  122. void gpu_set_float2(int location, float value1, float value2) {
  123. float *floats = (float *)(&constant_buffer.data[location]);
  124. floats[0] = value1;
  125. floats[1] = value2;
  126. }
  127. void gpu_set_float3(int location, float value1, float value2, float value3) {
  128. float *floats = (float *)(&constant_buffer.data[location]);
  129. floats[0] = value1;
  130. floats[1] = value2;
  131. floats[2] = value3;
  132. }
  133. void gpu_set_float4(int location, float value1, float value2, float value3, float value4) {
  134. float *floats = (float *)(&constant_buffer.data[location]);
  135. floats[0] = value1;
  136. floats[1] = value2;
  137. floats[2] = value3;
  138. floats[3] = value4;
  139. }
  140. void gpu_set_floats(int location, f32_array_t *values) {
  141. float *floats = (float *)(&constant_buffer.data[location]);
  142. for (int i = 0; i < values->length; ++i) {
  143. floats[i] = values->buffer[i];
  144. }
  145. }
  146. void gpu_set_bool(int location, bool value) {
  147. int *ints = (int *)(&constant_buffer.data[location]);
  148. ints[0] = value ? 1 : 0;
  149. }
  150. static void gpu_internal_set_matrix3(int offset, iron_matrix3x3_t *value) {
  151. float *floats = (float *)(&constant_buffer.data[offset]);
  152. for (int y = 0; y < 3; ++y) {
  153. for (int x = 0; x < 3; ++x) {
  154. floats[x + y * 4] = iron_matrix3x3_get(value, x, y);
  155. }
  156. }
  157. }
  158. static void gpu_internal_set_matrix4(int offset, iron_matrix4x4_t *value) {
  159. float *floats = (float *)(&constant_buffer.data[offset]);
  160. for (int y = 0; y < 4; ++y) {
  161. for (int x = 0; x < 4; ++x) {
  162. floats[x + y * 4] = iron_matrix4x4_get(value, x, y);
  163. }
  164. }
  165. }
  166. void gpu_set_matrix3(int location, iron_matrix3x3_t value) {
  167. if (gpu_transpose_mat) {
  168. iron_matrix3x3_t m = value;
  169. iron_matrix3x3_transpose(&m);
  170. gpu_internal_set_matrix3(location, &m);
  171. }
  172. else {
  173. gpu_internal_set_matrix3(location, &value);
  174. }
  175. }
  176. void gpu_set_matrix4(int location, iron_matrix4x4_t value) {
  177. if (gpu_transpose_mat) {
  178. iron_matrix4x4_t m = value;
  179. iron_matrix4x4_transpose(&m);
  180. gpu_internal_set_matrix4(location, &m);
  181. }
  182. else {
  183. gpu_internal_set_matrix4(location, &value);
  184. }
  185. }
  186. void gpu_vertex_structure_add(gpu_vertex_structure_t *structure, const char *name, gpu_vertex_data_t data) {
  187. structure->elements[structure->size].name = name;
  188. structure->elements[structure->size].data = data;
  189. structure->size++;
  190. }
  191. void gpu_pipeline_init(gpu_pipeline_t *pipe) {
  192. pipe->input_layout = NULL;
  193. pipe->vertex_shader = NULL;
  194. pipe->fragment_shader = NULL;
  195. pipe->cull_mode = GPU_CULL_MODE_NEVER;
  196. pipe->depth_write = false;
  197. pipe->depth_mode = GPU_COMPARE_MODE_ALWAYS;
  198. pipe->blend_source = GPU_BLEND_ONE;
  199. pipe->blend_destination = GPU_BLEND_ZERO;
  200. pipe->alpha_blend_source = GPU_BLEND_ONE;
  201. pipe->alpha_blend_destination = GPU_BLEND_ZERO;
  202. for (int i = 0; i < 8; ++i) {
  203. pipe->color_write_mask_red[i] = true;
  204. pipe->color_write_mask_green[i] = true;
  205. pipe->color_write_mask_blue[i] = true;
  206. pipe->color_write_mask_alpha[i] = true;
  207. pipe->color_attachment[i] = GPU_TEXTURE_FORMAT_RGBA32;
  208. }
  209. pipe->color_attachment_count = 1;
  210. pipe->depth_attachment_bits = 0;
  211. }
  212. void gpu_create_framebuffers(int depth_buffer_bits) {
  213. for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) {
  214. gpu_render_target_init2(&framebuffers[i], iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_RGBA32, i);
  215. }
  216. if (depth_buffer_bits > 0) {
  217. gpu_render_target_init(&framebuffer_depth, iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_D32);
  218. }
  219. else {
  220. framebuffer_depth.width = framebuffer_depth.height = 0;
  221. }
  222. }
  223. int gpu_vertex_data_size(gpu_vertex_data_t data) {
  224. switch (data) {
  225. case GPU_VERTEX_DATA_F32_1X:
  226. return 1 * 4;
  227. case GPU_VERTEX_DATA_F32_2X:
  228. return 2 * 4;
  229. case GPU_VERTEX_DATA_F32_3X:
  230. return 3 * 4;
  231. case GPU_VERTEX_DATA_F32_4X:
  232. return 4 * 4;
  233. case GPU_VERTEX_DATA_I16_2X_NORM:
  234. return 2 * 2;
  235. case GPU_VERTEX_DATA_I16_4X_NORM:
  236. return 4 * 2;
  237. }
  238. }
  239. int gpu_vertex_struct_size(gpu_vertex_structure_t *s) {
  240. int size = 0;
  241. for (int i = 0; i < s->size; ++i) {
  242. size += gpu_vertex_data_size(s->elements[i].data);
  243. }
  244. return size;
  245. }
  246. int gpu_texture_format_size(gpu_texture_format_t format) {
  247. switch (format) {
  248. case GPU_TEXTURE_FORMAT_RGBA128:
  249. return 16;
  250. case GPU_TEXTURE_FORMAT_RGBA64:
  251. return 8;
  252. case GPU_TEXTURE_FORMAT_R16:
  253. return 2;
  254. case GPU_TEXTURE_FORMAT_R8:
  255. return 1;
  256. default:
  257. return 4;
  258. }
  259. }