SDL_pipeline_gpu.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. static Uint32 SDLCALL HashPipelineCacheKey(void *userdata, const void *key)
  24. {
  25. const GPU_PipelineParameters *params = (const GPU_PipelineParameters *) key;
  26. return SDL_murmur3_32(params, sizeof(*params), 0);
  27. }
  28. static bool SDLCALL MatchPipelineCacheKey(void *userdata, const void *a, const void *b)
  29. {
  30. return (SDL_memcmp(a, b, sizeof (GPU_PipelineParameters)) == 0);
  31. }
  32. static void SDLCALL DestroyPipelineCacheHashItem(void *userdata, const void *key, const void *value)
  33. {
  34. SDL_GPUGraphicsPipeline *pipeline = (SDL_GPUGraphicsPipeline *) value;
  35. SDL_GPUDevice *device = (SDL_GPUDevice *) userdata;
  36. SDL_ReleaseGPUGraphicsPipeline(device, pipeline);
  37. SDL_free((GPU_PipelineParameters *) key);
  38. }
  39. bool GPU_InitPipelineCache(GPU_PipelineCache *cache, SDL_GPUDevice *device)
  40. {
  41. cache->table = SDL_CreateHashTable(0, false, HashPipelineCacheKey, MatchPipelineCacheKey, DestroyPipelineCacheHashItem, device);
  42. return (cache->table != NULL);
  43. }
  44. void GPU_DestroyPipelineCache(GPU_PipelineCache *cache)
  45. {
  46. SDL_DestroyHashTable(cache->table);
  47. }
  48. static SDL_GPUGraphicsPipeline *MakePipeline(SDL_GPUDevice *device, GPU_Shaders *shaders, const GPU_PipelineParameters *params)
  49. {
  50. SDL_GPUColorTargetDescription ad;
  51. SDL_zero(ad);
  52. ad.format = params->attachment_format;
  53. SDL_BlendMode blend = params->blend_mode;
  54. ad.blend_state.enable_blend = blend != 0;
  55. ad.blend_state.color_write_mask = 0xF;
  56. ad.blend_state.alpha_blend_op = GPU_ConvertBlendOperation(SDL_GetBlendModeAlphaOperation(blend));
  57. ad.blend_state.dst_alpha_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeDstAlphaFactor(blend));
  58. ad.blend_state.src_alpha_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blend));
  59. ad.blend_state.color_blend_op = GPU_ConvertBlendOperation(SDL_GetBlendModeColorOperation(blend));
  60. ad.blend_state.dst_color_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeDstColorFactor(blend));
  61. ad.blend_state.src_color_blendfactor = GPU_ConvertBlendFactor(SDL_GetBlendModeSrcColorFactor(blend));
  62. SDL_GPUGraphicsPipelineCreateInfo pci;
  63. SDL_zero(pci);
  64. pci.target_info.has_depth_stencil_target = false;
  65. pci.target_info.num_color_targets = 1;
  66. pci.target_info.color_target_descriptions = &ad;
  67. pci.vertex_shader = GPU_GetVertexShader(shaders, params->vert_shader);
  68. pci.fragment_shader = GPU_GetFragmentShader(shaders, params->frag_shader);
  69. pci.multisample_state.sample_count = SDL_GPU_SAMPLECOUNT_1;
  70. pci.multisample_state.enable_mask = false;
  71. pci.primitive_type = params->primitive_type;
  72. pci.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
  73. pci.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
  74. pci.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
  75. pci.rasterizer_state.enable_depth_clip = true;
  76. SDL_GPUVertexBufferDescription vertex_buffer_desc;
  77. SDL_zero(vertex_buffer_desc);
  78. Uint32 num_attribs = 0;
  79. SDL_GPUVertexAttribute attribs[4];
  80. SDL_zero(attribs);
  81. bool have_attr_uv = false;
  82. switch (params->vert_shader) {
  83. case VERT_SHADER_TRI_COLOR:
  84. have_attr_uv = true;
  85. break;
  86. case VERT_SHADER_TRI_TEXTURE:
  87. have_attr_uv = true;
  88. break;
  89. default:
  90. break;
  91. }
  92. // Position
  93. attribs[num_attribs].location = num_attribs;
  94. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
  95. attribs[num_attribs].offset = vertex_buffer_desc.pitch;
  96. vertex_buffer_desc.pitch += 2 * sizeof(float);
  97. num_attribs++;
  98. // Color
  99. attribs[num_attribs].location = num_attribs;
  100. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4;
  101. attribs[num_attribs].offset = vertex_buffer_desc.pitch;
  102. vertex_buffer_desc.pitch += 4 * sizeof(float);
  103. num_attribs++;
  104. if (have_attr_uv) {
  105. // UVs
  106. attribs[num_attribs].location = num_attribs;
  107. attribs[num_attribs].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
  108. attribs[num_attribs].offset = vertex_buffer_desc.pitch;
  109. vertex_buffer_desc.pitch += 2 * sizeof(float);
  110. num_attribs++;
  111. }
  112. pci.vertex_input_state.num_vertex_attributes = num_attribs;
  113. pci.vertex_input_state.vertex_attributes = attribs;
  114. pci.vertex_input_state.num_vertex_buffers = 1;
  115. pci.vertex_input_state.vertex_buffer_descriptions = &vertex_buffer_desc;
  116. return SDL_CreateGPUGraphicsPipeline(device, &pci);
  117. }
  118. SDL_GPUGraphicsPipeline *GPU_GetPipeline(GPU_PipelineCache *cache, GPU_Shaders *shaders, SDL_GPUDevice *device, const GPU_PipelineParameters *params)
  119. {
  120. SDL_GPUGraphicsPipeline *pipeline = NULL;
  121. if (!SDL_FindInHashTable(cache->table, params, (const void **) &pipeline)) {
  122. bool inserted = false;
  123. // !!! FIXME: why don't we have an SDL_alloc_copy function/macro?
  124. GPU_PipelineParameters *paramscpy = (GPU_PipelineParameters *) SDL_malloc(sizeof (*paramscpy));
  125. if (paramscpy) {
  126. SDL_copyp(paramscpy, params);
  127. pipeline = MakePipeline(device, shaders, params);
  128. if (pipeline) {
  129. inserted = SDL_InsertIntoHashTable(cache->table, paramscpy, pipeline, false);
  130. }
  131. }
  132. if (!inserted) {
  133. SDL_free(paramscpy);
  134. if (pipeline) {
  135. SDL_ReleaseGPUGraphicsPipeline(device, pipeline);
  136. pipeline = NULL;
  137. }
  138. }
  139. }
  140. return pipeline;
  141. }
  142. #endif // SDL_VIDEO_RENDER_GPU