SDL_sysrender.h 13 KB

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