1
0

SDL_pipeline_gpu.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_RENDER_GPU
  20. #include "SDL_gpu_util.h"
  21. #include "SDL_pipeline_gpu.h"
  22. #include "../SDL_sysrender.h"
  23. struct GPU_PipelineCacheKeyStruct
  24. {
  25. Uint64 blend_mode : 28;
  26. Uint64 frag_shader : 4;
  27. Uint64 vert_shader : 4;
  28. Uint64 attachment_format : 6;
  29. Uint64 primitive_type : 3;
  30. };
  31. typedef union GPU_PipelineCacheKey
  32. {
  33. struct GPU_PipelineCacheKeyStruct as_struct;
  34. Uint64 as_uint64;
  35. } GPU_PipelineCacheKey;
  36. SDL_COMPILE_TIME_ASSERT(GPU_PipelineCacheKey_Size, sizeof(GPU_PipelineCacheKey) <= sizeof(Uint64));
  37. typedef struct GPU_PipelineCacheEntry
  38. {
  39. GPU_PipelineCacheKey key;
  40. SDL_GPUGraphicsPipeline *pipeline;
  41. } GPU_PipelineCacheEntry;
  42. static Uint32 HashPipelineCacheKey(const GPU_PipelineCacheKey *key)
  43. {
  44. Uint64 x = key->as_uint64;
  45. // 64-bit uint hash function stolen from taisei (which stole it from somewhere else)
  46. x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
  47. x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
  48. x = x ^ (x >> 31);
  49. return (Uint32)(x & 0xffffffff);
  50. }
  51. static Uint32 HashPassthrough(const void *key, void *data)
  52. {
  53. // double-cast to silence a clang warning
  54. return (Uint32)(uintptr_t)key;
  55. }
  56. static bool MatchPipelineCacheKey(const void *a, const void *b, void *data)
  57. {
  58. return a == b;
  59. }
  60. static void NukePipelineCacheEntry(const void *key, const void *value, void *data)
  61. {
  62. GPU_PipelineCacheEntry *entry = (GPU_PipelineCacheEntry *)value;
  63. SDL_GPUDevice *device = data;
  64. SDL_ReleaseGPUGraphicsPipeline(device, entry->pipeline);
  65. SDL_free(entry);
  66. }
  67. bool GPU_InitPipelineCache(GPU_PipelineCache *cache, SDL_GPUDevice *device)
  68. {
  69. // FIXME how many buckets do we need?
  70. cache->table = SDL_CreateHashTable(device, 32, HashPassthrough, MatchPipelineCacheKey, NukePipelineCacheEntry, true);
  71. if (!cache->table) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. void GPU_DestroyPipelineCache(GPU_PipelineCache *cache)
  77. {
  78. SDL_DestroyHashTable(cache->table);
  79. }
  80. static SDL_GPUGraphicsPipeline *MakePipeline(SDL_GPUDevice *device, GPU_Shaders *shaders, const GPU_PipelineParameters *params)
  81. {
  82. SDL_GPUColorTargetDescription ad;
  83. SDL_zero(ad);
  84. ad.format = params->attachment_format;
  85. SDL_BlendMode blend = params->blend_mode;
  86. ad.blend_state.enable_blend = blend != 0;
  87. ad.blend_state.color_write_mask = 0xF;
  88. ad.blend_state.alpha_blend_op = GPU_ConvertBlendOperation(SDL_GetBlendModeAlphaOperation(blend));
  89. ad.blend_state.dst_alpha_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeDstAlphaFactor(blend));
  90. ad.blend_state.src_alpha_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blend));
  91. ad.blend_state.color_blend_op = GPU_ConvertBlendOperation(SDL_GetBlendModeColorOperation(blend));
  92. ad.blend_state.dst_color_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeDstColorFactor(blend));
  93. ad.blend_state.src_color_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeSrcColorFactor(blend));
  94. SDL_GPUGraphicsPipelineCreateInfo pci;
  95. SDL_zero(pci);
  96. pci.target_info.has_depth_stencil_target = false;
  97. pci.target_info.num_color_targets = 1;
  98. pci.target_info.color_target_descriptions = &ad;
  99. pci.vertex_shader = GPU_GetVertexShader(shaders, params->vert_shader);
  100. pci.fragment_shader = GPU_GetFragmentShader(shaders, params->frag_shader);
  101. pci.multisample_state.sample_count = SDL_GPU_SAMPLECOUNT_1;
  102. pci.multisample_state.enable_mask = SDL_FALSE;
  103. pci.primitive_type = params->primitive_type;
  104. pci.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
  105. pci.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
  106. pci.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
  107. SDL_GPUVertexBinding bind;
  108. SDL_zero(bind);
  109. Uint32 num_attribs = 0;
  110. SDL_GPUVertexAttribute attribs[4];
  111. SDL_zero(attribs);
  112. bool have_attr_color = false;
  113. bool have_attr_uv = false;
  114. switch (params->vert_shader) {
  115. case VERT_SHADER_TRI_TEXTURE:
  116. have_attr_uv = true;
  117. SDL_FALLTHROUGH;
  118. case VERT_SHADER_TRI_COLOR:
  119. have_attr_color = true;
  120. SDL_FALLTHROUGH;
  121. default:
  122. break;
  123. }
  124. // Position
  125. attribs[num_attribs].location = num_attribs;
  126. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
  127. attribs[num_attribs].offset = bind.pitch;
  128. bind.pitch += 2 * sizeof(float);
  129. num_attribs++;
  130. if (have_attr_color) {
  131. // Color
  132. attribs[num_attribs].location = num_attribs;
  133. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4;
  134. attribs[num_attribs].offset = bind.pitch;
  135. bind.pitch += 4 * sizeof(float);
  136. num_attribs++;
  137. }
  138. if (have_attr_uv) {
  139. // UVs
  140. attribs[num_attribs].location = num_attribs;
  141. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
  142. attribs[num_attribs].offset = bind.pitch;
  143. bind.pitch += 2 * sizeof(float);
  144. num_attribs++;
  145. }
  146. pci.vertex_input_state.num_vertex_attributes = num_attribs;
  147. pci.vertex_input_state.vertex_attributes = attribs;
  148. pci.vertex_input_state.num_vertex_bindings = 1;
  149. pci.vertex_input_state.vertex_bindings = &bind;
  150. return SDL_CreateGPUGraphicsPipeline(device, &pci);
  151. }
  152. static GPU_PipelineCacheKey MakePipelineCacheKey(const GPU_PipelineParameters *params)
  153. {
  154. GPU_PipelineCacheKey key;
  155. SDL_zero(key);
  156. key.as_struct.blend_mode = params->blend_mode;
  157. key.as_struct.frag_shader = params->frag_shader;
  158. key.as_struct.vert_shader = params->vert_shader;
  159. key.as_struct.attachment_format = params->attachment_format;
  160. key.as_struct.primitive_type = params->primitive_type;
  161. return key;
  162. }
  163. SDL_GPUGraphicsPipeline *GPU_GetPipeline(GPU_PipelineCache *cache, GPU_Shaders *shaders, SDL_GPUDevice *device, const GPU_PipelineParameters *params)
  164. {
  165. GPU_PipelineCacheKey key = MakePipelineCacheKey(params);
  166. void *keyval = (void *)(uintptr_t)HashPipelineCacheKey(&key);
  167. SDL_GPUGraphicsPipeline *pipeline = NULL;
  168. void *iter = NULL;
  169. GPU_PipelineCacheEntry *entry = NULL;
  170. while (SDL_IterateHashTableKey(cache->table, keyval, (const void **)&entry, &iter)) {
  171. if (entry->key.as_uint64 == key.as_uint64) {
  172. return entry->pipeline;
  173. }
  174. }
  175. pipeline = MakePipeline(device, shaders, params);
  176. if (pipeline == NULL) {
  177. return NULL;
  178. }
  179. entry = SDL_malloc(sizeof(*entry));
  180. entry->key = key;
  181. entry->pipeline = pipeline;
  182. SDL_InsertIntoHashTable(cache->table, keyval, entry);
  183. return pipeline;
  184. }
  185. #endif // SDL_VIDEO_RENDER_GPU