SDL_sysrender.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #ifndef SDL_sysrender_h_
  20. #define SDL_sysrender_h_
  21. #include "../video/SDL_surface_c.h"
  22. #include "SDL_yuv_sw_c.h"
  23. // Set up for C function definitions, even when using C++
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * A rectangle, with the origin at the upper left (double precision).
  29. */
  30. typedef struct SDL_DRect
  31. {
  32. double x;
  33. double y;
  34. double w;
  35. double h;
  36. } SDL_DRect;
  37. // The SDL 2D rendering system
  38. typedef struct SDL_RenderDriver SDL_RenderDriver;
  39. // Rendering view state
  40. typedef struct SDL_RenderViewState
  41. {
  42. int pixel_w;
  43. int pixel_h;
  44. SDL_Rect viewport;
  45. SDL_Rect pixel_viewport;
  46. SDL_Rect clip_rect;
  47. SDL_Rect pixel_clip_rect;
  48. bool clipping_enabled;
  49. SDL_FPoint scale;
  50. // Support for logical output coordinates
  51. SDL_RendererLogicalPresentation logical_presentation_mode;
  52. int logical_w, logical_h;
  53. SDL_FRect logical_src_rect;
  54. SDL_FRect logical_dst_rect;
  55. SDL_FPoint logical_scale;
  56. SDL_FPoint logical_offset;
  57. SDL_FPoint current_scale; // this is just `scale * logical_scale`, precalculated, since we use it a lot.
  58. } SDL_RenderViewState;
  59. // Define the SDL texture palette structure
  60. typedef struct SDL_TexturePalette
  61. {
  62. int refcount;
  63. Uint32 version;
  64. Uint32 last_command_generation; // last command queue generation this palette was in.
  65. void *internal; // Driver specific palette representation
  66. } SDL_TexturePalette;
  67. // Define the SDL texture structure
  68. struct SDL_Texture
  69. {
  70. // Public API definition
  71. SDL_PixelFormat format; /**< The format of the texture, read-only */
  72. int w; /**< The width of the texture, read-only. */
  73. int h; /**< The height of the texture, read-only. */
  74. int refcount; /**< Application reference count, used when freeing texture */
  75. // Private API definition
  76. SDL_Renderer *renderer;
  77. SDL_Colorspace colorspace; // The colorspace of the texture
  78. float SDR_white_point; // The SDR white point for this content
  79. float HDR_headroom; // The HDR headroom needed by this content
  80. SDL_TextureAccess access; // The texture access mode
  81. SDL_BlendMode blendMode; // The texture blend mode
  82. SDL_ScaleMode scaleMode; // The texture scale mode
  83. SDL_FColor color; // Texture modulation values
  84. SDL_RenderViewState view; // Target texture view state
  85. SDL_Palette *public_palette;
  86. SDL_TexturePalette *palette;
  87. Uint32 palette_version;
  88. SDL_Surface *palette_surface;
  89. // Support for formats not supported directly by the renderer
  90. SDL_Texture *native;
  91. SDL_SW_YUVTexture *yuv;
  92. void *pixels;
  93. int pitch;
  94. SDL_Rect locked_rect;
  95. SDL_Surface *locked_surface; // Locked region exposed as a SDL surface
  96. Uint32 last_command_generation; // last command queue generation this texture was in.
  97. SDL_PropertiesID props;
  98. void *internal; // Driver specific texture representation
  99. SDL_Texture *prev;
  100. SDL_Texture *next;
  101. };
  102. // Define the GPU render state structure
  103. typedef struct SDL_GPURenderStateUniformBuffer
  104. {
  105. Uint32 slot_index;
  106. void *data;
  107. Uint32 length;
  108. } SDL_GPURenderStateUniformBuffer;
  109. // Define the GPU render state structure
  110. struct SDL_GPURenderState
  111. {
  112. SDL_Renderer *renderer;
  113. Uint32 last_command_generation; // last command queue generation this state was in.
  114. SDL_GPUShader *fragment_shader;
  115. int num_sampler_bindings;
  116. SDL_GPUTextureSamplerBinding *sampler_bindings;
  117. int num_storage_textures;
  118. SDL_GPUTexture **storage_textures;
  119. int num_storage_buffers;
  120. SDL_GPUBuffer **storage_buffers;
  121. int num_uniform_buffers;
  122. SDL_GPURenderStateUniformBuffer *uniform_buffers;
  123. };
  124. typedef enum
  125. {
  126. SDL_RENDERCMD_NO_OP,
  127. SDL_RENDERCMD_SETVIEWPORT,
  128. SDL_RENDERCMD_SETCLIPRECT,
  129. SDL_RENDERCMD_SETDRAWCOLOR,
  130. SDL_RENDERCMD_CLEAR,
  131. SDL_RENDERCMD_DRAW_POINTS,
  132. SDL_RENDERCMD_DRAW_LINES,
  133. SDL_RENDERCMD_FILL_RECTS,
  134. SDL_RENDERCMD_COPY,
  135. SDL_RENDERCMD_COPY_EX,
  136. SDL_RENDERCMD_GEOMETRY
  137. } SDL_RenderCommandType;
  138. typedef struct SDL_RenderCommand
  139. {
  140. SDL_RenderCommandType command;
  141. union
  142. {
  143. struct
  144. {
  145. size_t first;
  146. SDL_Rect rect;
  147. } viewport;
  148. struct
  149. {
  150. bool enabled;
  151. SDL_Rect rect;
  152. } cliprect;
  153. struct
  154. {
  155. size_t first;
  156. size_t count;
  157. float color_scale;
  158. SDL_FColor color;
  159. SDL_BlendMode blend;
  160. SDL_Texture *texture;
  161. SDL_ScaleMode texture_scale_mode;
  162. SDL_TextureAddressMode texture_address_mode_u;
  163. SDL_TextureAddressMode texture_address_mode_v;
  164. SDL_GPURenderState *gpu_render_state;
  165. } draw;
  166. struct
  167. {
  168. size_t first;
  169. float color_scale;
  170. SDL_FColor color;
  171. } color;
  172. } data;
  173. struct SDL_RenderCommand *next;
  174. } SDL_RenderCommand;
  175. typedef struct SDL_VertexSolid
  176. {
  177. SDL_FPoint position;
  178. SDL_FColor color;
  179. } SDL_VertexSolid;
  180. typedef enum
  181. {
  182. SDL_RENDERLINEMETHOD_POINTS,
  183. SDL_RENDERLINEMETHOD_LINES,
  184. SDL_RENDERLINEMETHOD_GEOMETRY,
  185. } SDL_RenderLineMethod;
  186. // Define the SDL renderer structure
  187. struct SDL_Renderer
  188. {
  189. void (*WindowEvent)(SDL_Renderer *renderer, const SDL_WindowEvent *event);
  190. bool (*GetOutputSize)(SDL_Renderer *renderer, int *w, int *h);
  191. bool (*SupportsBlendMode)(SDL_Renderer *renderer, SDL_BlendMode blendMode);
  192. bool (*CreateTexture)(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props);
  193. bool (*QueueSetViewport)(SDL_Renderer *renderer, SDL_RenderCommand *cmd);
  194. bool (*QueueSetDrawColor)(SDL_Renderer *renderer, SDL_RenderCommand *cmd);
  195. bool (*QueueDrawPoints)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points,
  196. int count);
  197. bool (*QueueDrawLines)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points,
  198. int count);
  199. bool (*QueueFillRects)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FRect *rects,
  200. int count);
  201. bool (*QueueCopy)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  202. const SDL_FRect *srcrect, const SDL_FRect *dstrect);
  203. bool (*QueueCopyEx)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  204. const SDL_FRect *srcquad, const SDL_FRect *dstrect,
  205. const double angle, const SDL_FPoint *center, const SDL_FlipMode flip, float scale_x, float scale_y);
  206. bool (*QueueGeometry)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  207. const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
  208. int num_vertices, const void *indices, int num_indices, int size_indices,
  209. float scale_x, float scale_y);
  210. void (*InvalidateCachedState)(SDL_Renderer *renderer);
  211. bool (*RunCommandQueue)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize);
  212. bool (*CreatePalette)(SDL_Renderer *renderer, SDL_TexturePalette *palette);
  213. bool (*UpdatePalette)(SDL_Renderer *renderer, SDL_TexturePalette *palette, int ncolors, SDL_Color *colors);
  214. void (*DestroyPalette)(SDL_Renderer *renderer, SDL_TexturePalette *palette);
  215. bool (*ChangeTexturePalette)(SDL_Renderer *renderer, SDL_Texture *texture);
  216. bool (*UpdateTexture)(SDL_Renderer *renderer, SDL_Texture *texture,
  217. const SDL_Rect *rect, const void *pixels,
  218. int pitch);
  219. #ifdef SDL_HAVE_YUV
  220. bool (*UpdateTextureYUV)(SDL_Renderer *renderer, SDL_Texture *texture,
  221. const SDL_Rect *rect,
  222. const Uint8 *Yplane, int Ypitch,
  223. const Uint8 *Uplane, int Upitch,
  224. const Uint8 *Vplane, int Vpitch);
  225. bool (*UpdateTextureNV)(SDL_Renderer *renderer, SDL_Texture *texture,
  226. const SDL_Rect *rect,
  227. const Uint8 *Yplane, int Ypitch,
  228. const Uint8 *UVplane, int UVpitch);
  229. #endif
  230. bool (*LockTexture)(SDL_Renderer *renderer, SDL_Texture *texture,
  231. const SDL_Rect *rect, void **pixels, int *pitch);
  232. void (*UnlockTexture)(SDL_Renderer *renderer, SDL_Texture *texture);
  233. bool (*SetRenderTarget)(SDL_Renderer *renderer, SDL_Texture *texture);
  234. SDL_Surface *(*RenderReadPixels)(SDL_Renderer *renderer, const SDL_Rect *rect);
  235. bool (*RenderPresent)(SDL_Renderer *renderer);
  236. void (*DestroyTexture)(SDL_Renderer *renderer, SDL_Texture *texture);
  237. void (*DestroyRenderer)(SDL_Renderer *renderer);
  238. bool (*SetVSync)(SDL_Renderer *renderer, int vsync);
  239. void *(*GetMetalLayer)(SDL_Renderer *renderer);
  240. void *(*GetMetalCommandEncoder)(SDL_Renderer *renderer);
  241. bool (*AddVulkanRenderSemaphores)(SDL_Renderer *renderer, Uint32 wait_stage_mask, Sint64 wait_semaphore, Sint64 signal_semaphore);
  242. #ifdef SDL_PLATFORM_GDK
  243. void (*GDKSuspendRenderer)(SDL_Renderer *renderer);
  244. void (*GDKResumeRenderer)(SDL_Renderer *renderer);
  245. #endif
  246. // The current renderer info
  247. const char *name;
  248. SDL_PixelFormat *texture_formats;
  249. int num_texture_formats;
  250. bool software;
  251. bool npot_texture_wrap_unsupported;
  252. // The window associated with the renderer
  253. SDL_Window *window;
  254. bool hidden;
  255. // Whether we should simulate vsync
  256. bool wanted_vsync;
  257. bool simulate_vsync;
  258. Uint64 simulate_vsync_interval_ns;
  259. Uint64 last_present;
  260. SDL_RenderViewState *view;
  261. SDL_RenderViewState main_view;
  262. // The window pixel to point coordinate scale
  263. SDL_FPoint dpi_scale;
  264. // The method of drawing lines
  265. SDL_RenderLineMethod line_method;
  266. // Default scale mode for textures created with this renderer
  267. SDL_ScaleMode scale_mode;
  268. // The list of textures
  269. SDL_Texture *textures;
  270. SDL_Texture *target;
  271. SDL_Mutex *target_mutex;
  272. // The list of palettes
  273. SDL_HashTable *palettes;
  274. SDL_Colorspace output_colorspace;
  275. float SDR_white_point;
  276. float HDR_headroom;
  277. float desired_color_scale;
  278. float color_scale;
  279. SDL_FColor color; /**< Color for drawing operations values */
  280. SDL_BlendMode blendMode; /**< The drawing blend mode */
  281. SDL_TextureAddressMode texture_address_mode_u;
  282. SDL_TextureAddressMode texture_address_mode_v;
  283. SDL_GPURenderState *gpu_render_state;
  284. SDL_RenderCommand *render_commands;
  285. SDL_RenderCommand *render_commands_tail;
  286. SDL_RenderCommand *render_commands_pool;
  287. Uint32 render_command_generation;
  288. SDL_FColor last_queued_color;
  289. float last_queued_color_scale;
  290. SDL_Rect last_queued_viewport;
  291. SDL_Rect last_queued_cliprect;
  292. bool last_queued_cliprect_enabled;
  293. bool color_queued;
  294. bool viewport_queued;
  295. bool cliprect_queued;
  296. void *vertex_data;
  297. size_t vertex_data_used;
  298. size_t vertex_data_allocation;
  299. // Shaped window support
  300. bool transparent_window;
  301. SDL_Surface *shape_surface;
  302. SDL_Texture *shape_texture;
  303. SDL_PropertiesID props;
  304. SDL_Texture *debug_char_texture_atlas;
  305. bool destroyed; // already destroyed by SDL_DestroyWindow; just free this struct in SDL_DestroyRenderer.
  306. void *internal;
  307. SDL_Renderer *next;
  308. };
  309. // Define the SDL render driver structure
  310. struct SDL_RenderDriver
  311. {
  312. bool (*CreateRenderer)(SDL_Renderer *renderer, SDL_Window *window, SDL_PropertiesID props);
  313. const char *name;
  314. };
  315. // Not all of these are available in a given build. Use #ifdefs, etc.
  316. extern SDL_RenderDriver D3D_RenderDriver;
  317. extern SDL_RenderDriver D3D11_RenderDriver;
  318. extern SDL_RenderDriver D3D12_RenderDriver;
  319. extern SDL_RenderDriver GL_RenderDriver;
  320. extern SDL_RenderDriver GLES2_RenderDriver;
  321. extern SDL_RenderDriver METAL_RenderDriver;
  322. extern SDL_RenderDriver NGAGE_RenderDriver;
  323. extern SDL_RenderDriver VULKAN_RenderDriver;
  324. extern SDL_RenderDriver PS2_RenderDriver;
  325. extern SDL_RenderDriver PSP_RenderDriver;
  326. extern SDL_RenderDriver SW_RenderDriver;
  327. extern SDL_RenderDriver VITA_GXM_RenderDriver;
  328. extern SDL_RenderDriver GPU_RenderDriver;
  329. // Clean up any renderers at shutdown
  330. extern void SDL_QuitRender(void);
  331. #define RENDER_SAMPLER_HASHKEY(scale_mode, address_u, address_v) \
  332. (((scale_mode == SDL_SCALEMODE_NEAREST) << 0) | \
  333. ((address_u == SDL_TEXTURE_ADDRESS_WRAP) << 1) | \
  334. ((address_v == SDL_TEXTURE_ADDRESS_WRAP) << 2))
  335. #define RENDER_SAMPLER_COUNT (((1 << 0) | (1 << 1) | (1 << 2)) + 1)
  336. // Add a supported texture format to a renderer
  337. extern bool SDL_AddSupportedTextureFormat(SDL_Renderer *renderer, SDL_PixelFormat format);
  338. // Setup colorspace conversion
  339. extern void SDL_SetupRendererColorspace(SDL_Renderer *renderer, SDL_PropertiesID props);
  340. // Colorspace conversion functions
  341. extern bool SDL_RenderingLinearSpace(SDL_Renderer *renderer);
  342. extern void SDL_ConvertToLinear(SDL_FColor *color);
  343. extern void SDL_ConvertFromLinear(SDL_FColor *color);
  344. // Blend mode functions
  345. extern SDL_BlendFactor SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode);
  346. extern SDL_BlendFactor SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode);
  347. extern SDL_BlendOperation SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode);
  348. extern SDL_BlendFactor SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode);
  349. extern SDL_BlendFactor SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode);
  350. extern SDL_BlendOperation SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode);
  351. /* drivers call this during their Queue*() methods to make space in a array that are used
  352. for a vertex buffer during RunCommandQueue(). Pointers returned here are only valid until
  353. the next call, because it might be in an array that gets realloc()'d. */
  354. extern void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, size_t numbytes, size_t alignment, size_t *offset);
  355. // Let the video subsystem destroy a renderer without making its pointer invalid.
  356. extern void SDL_DestroyRendererWithoutFreeing(SDL_Renderer *renderer);
  357. // Ends C function definitions when using C++
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #endif // SDL_sysrender_h_