Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDL3] [GPU] Allow using SDL_Renderer with custom GPU device #11829

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 143 additions & 92 deletions include/SDL3/SDL_render.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,8 @@ SDL3_0.0.0 {
SDL_GetTrayMenuParentEntry;
SDL_GetTrayMenuParentTray;
SDL_GetThreadState;
SDL_SetRenderGPUCommandBuffer;
SDL_RenderPresentToGPUTexture;
# extra symbols go here (don't modify this line)
local: *;
};
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -1255,3 +1255,5 @@
#define SDL_GetTrayMenuParentEntry SDL_GetTrayMenuParentEntry_REAL
#define SDL_GetTrayMenuParentTray SDL_GetTrayMenuParentTray_REAL
#define SDL_GetThreadState SDL_GetThreadState_REAL
#define SDL_SetRenderGPUCommandBuffer SDL_SetRenderGPUCommandBuffer_REAL
#define SDL_RenderPresentToGPUTexture SDL_RenderPresentToGPUTexture_REAL
2 changes: 2 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1263,3 +1263,5 @@ SDL_DYNAPI_PROC(SDL_TrayMenu*,SDL_GetTrayEntryParent,(SDL_TrayEntry *a),(a),retu
SDL_DYNAPI_PROC(SDL_TrayEntry*,SDL_GetTrayMenuParentEntry,(SDL_TrayMenu *a),(a),return)
SDL_DYNAPI_PROC(SDL_Tray*,SDL_GetTrayMenuParentTray,(SDL_TrayMenu *a),(a),return)
SDL_DYNAPI_PROC(SDL_ThreadState,SDL_GetThreadState,(SDL_Thread *a),(a),return)
SDL_DYNAPI_PROC(bool,SDL_SetRenderGPUCommandBuffer,(SDL_Renderer *a,SDL_GPUCommandBuffer *b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_RenderPresentToGPUTexture,(SDL_Renderer *a,SDL_GPUTexture *b,SDL_GPUTextureFormat c),(a,b,c),return)
386 changes: 210 additions & 176 deletions src/render/SDL_render.c

Large diffs are not rendered by default.

70 changes: 37 additions & 33 deletions src/render/SDL_sysrender.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,28 @@ typedef struct SDL_RenderViewState
SDL_FPoint scale;
SDL_FPoint logical_scale;
SDL_FPoint logical_offset;
SDL_FPoint current_scale; // this is just `scale * logical_scale`, precalculated, since we use it a lot.
SDL_FPoint current_scale; // this is just `scale * logical_scale`, precalculated, since we use it a lot.
} SDL_RenderViewState;

// Define the SDL texture structure
struct SDL_Texture
{
// Public API definition
SDL_PixelFormat format; /**< The format of the texture, read-only */
int w; /**< The width of the texture, read-only. */
int h; /**< The height of the texture, read-only. */
SDL_PixelFormat format; /**< The format of the texture, read-only */
int w; /**< The width of the texture, read-only. */
int h; /**< The height of the texture, read-only. */

int refcount; /**< Application reference count, used when freeing texture */
int refcount; /**< Application reference count, used when freeing texture */

// Private API definition
SDL_Colorspace colorspace; // The colorspace of the texture
float SDR_white_point; // The SDR white point for this content
float HDR_headroom; // The HDR headroom needed by this content
SDL_TextureAccess access; // The texture access mode
SDL_BlendMode blendMode; // The texture blend mode
SDL_ScaleMode scaleMode; // The texture scale mode
SDL_FColor color; // Texture modulation values
SDL_RenderViewState view; // Target texture view state
SDL_Colorspace colorspace; // The colorspace of the texture
float SDR_white_point; // The SDR white point for this content
float HDR_headroom; // The HDR headroom needed by this content
SDL_TextureAccess access; // The texture access mode
SDL_BlendMode blendMode; // The texture blend mode
SDL_ScaleMode scaleMode; // The texture scale mode
SDL_FColor color; // Texture modulation values
SDL_RenderViewState view; // Target texture view state

SDL_Renderer *renderer;

Expand All @@ -104,7 +104,7 @@ struct SDL_Texture

SDL_PropertiesID props;

void *internal; // Driver specific texture representation
void *internal; // Driver specific texture representation

SDL_Texture *prev;
SDL_Texture *next;
Expand Down Expand Up @@ -183,39 +183,39 @@ struct SDL_Renderer
bool (*QueueSetViewport)(SDL_Renderer *renderer, SDL_RenderCommand *cmd);
bool (*QueueSetDrawColor)(SDL_Renderer *renderer, SDL_RenderCommand *cmd);
bool (*QueueDrawPoints)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points,
int count);
int count);
bool (*QueueDrawLines)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points,
int count);
int count);
bool (*QueueFillRects)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FRect *rects,
int count);
int count);
bool (*QueueCopy)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_FRect *srcrect, const SDL_FRect *dstrect);
const SDL_FRect *srcrect, const SDL_FRect *dstrect);
bool (*QueueCopyEx)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const SDL_FRect *srcquad, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_FlipMode flip, float scale_x, float scale_y);
const SDL_FRect *srcquad, const SDL_FRect *dstrect,
const double angle, const SDL_FPoint *center, const SDL_FlipMode flip, float scale_x, float scale_y);
bool (*QueueGeometry)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
int num_vertices, const void *indices, int num_indices, int size_indices,
float scale_x, float scale_y);
const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
int num_vertices, const void *indices, int num_indices, int size_indices,
float scale_x, float scale_y);

void (*InvalidateCachedState)(SDL_Renderer *renderer);
bool (*RunCommandQueue)(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize);
bool (*UpdateTexture)(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, const void *pixels,
int pitch);
const SDL_Rect *rect, const void *pixels,
int pitch);
#ifdef SDL_HAVE_YUV
bool (*UpdateTextureYUV)(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch);
bool (*UpdateTextureNV)(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch);
bool (*UpdateTextureNV)(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *UVplane, int UVpitch);
const Uint8 *UVplane, int UVpitch);
#endif
bool (*LockTexture)(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect *rect, void **pixels, int *pitch);
const SDL_Rect *rect, void **pixels, int *pitch);
void (*UnlockTexture)(SDL_Renderer *renderer, SDL_Texture *texture);
void (*SetTextureScaleMode)(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode);
bool (*SetRenderTarget)(SDL_Renderer *renderer, SDL_Texture *texture);
Expand Down Expand Up @@ -307,7 +307,7 @@ struct SDL_Renderer

SDL_Texture *debug_char_texture_atlas;

bool destroyed; // already destroyed by SDL_DestroyWindow; just free this struct in SDL_DestroyRenderer.
bool destroyed; // already destroyed by SDL_DestroyWindow; just free this struct in SDL_DestroyRenderer.

void *internal;

Expand Down Expand Up @@ -366,6 +366,10 @@ extern void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, size_t numbytes,
// Let the video subsystem destroy a renderer without making its pointer invalid.
extern void SDL_DestroyRendererWithoutFreeing(SDL_Renderer *renderer);

// We must expose this function for manual control over renderer command buffer
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find a better way to expose some driver-specific functions aside from declaring them in sysrender.h

extern void GPU_SetCommandBuffer(SDL_Renderer *renderer, SDL_GPUCommandBuffer *cmd_buf);
extern bool GPU_PresentToUserTexture(SDL_Renderer *renderer, SDL_GPUTexture *textur, SDL_GPUTextureFormat format);

// Ends C function definitions when using C++
#ifdef __cplusplus
}
Expand Down
Loading