SDL_GPU minimal, single-header, high-performance 2D graphics painting library

Jul 30, 2026 09:36 PM - 4 weeks ago 793

A minimal, high-performance 2D (g)raphics (p)ainter for SDL3.

This is simply a larboard of sokol_gp to SDL3, pinch 1 main difference:

sokol_gp relies connected sokol, which manages resources internally. SDL_gpu is lower-level and does not; So SDL_gp provides a elemental assets guidance strategy very akin to sokol's.

Primitive:

Screenshot of primite

Rectangle:

Screenshot of rectangles

Sprite:

Screenshot of sprite

Blendmode:

Screenshot of blendmode

If you want to jump successful the correct way, cheque retired the samples folder.

here is simply a elemental illustration to tie a reddish rectangle:

// Acquire a bid buffer for the existent frame SDL_GPUCommandBuffer *cmd_buffer = SDL_AcquireGPUCommandBuffer(device); // Begin a caller frame SDL_GPBegin(WINDOW_WIDTH, WINDOW_HEIGHT); { // Clear the surface to black. SDL_GPSetColor((SDL_Color){ 0, 0, 0, 255 }); SDL_GPClear(); // Draw a reddish filled rectangle. SDL_GPSetColor((SDL_Color){ 255, 0, 0, 255 }); { SDL_GPDrawRectFilled((SDL_GPRect){ 10, 10, 100, 100 }); } // The caller tin render to a swapchain aliases to a texture. Here we render to // the swapchain. SDL_GPUTexture *swapchain_texture = NULL; SDL_WaitAndAcquireGPUSwapchainTexture( cmd_buffer, window, &swapchain_texture, NULL, NULL); SDL_GPFlush(cmd_buffer, swapchain_texture); } SDL_GPEnd(); SDL_SubmitGPUCommandBuffer(cmd_buffer); SDL_Delay(DELTA_TIME_MS);

Error API:

// Get the past correction that occurred successful SDL_gp. Returns SDL_GP_ERROR_NONE if no // correction has occurred. SDL_GP_Error SDL_GPGetLastError(void); // Get a human-readable drawstring describing an SDL_GP_Error value. Returns // "Unknown error" if the correction worth is not recognized. const char *SDL_GPGetErrorMessage(SDL_GP_Error error);

Image API:

// Create an image from an SDL_Surface. Returns an invalid image if creation // failed, usage SDL_GPGetLastError() to get much accusation astir the error. SDL_GPImage SDL_GPCreateImage(SDL_Surface *surface); // Destroy an image and free its resources. void SDL_GPDestroyImage(SDL_GPImage image); // Get the GPU texture associated pinch an image. Returns NULL if the image is // invalid. SDL_GPUTexture *SDL_GPGetImageGPUTexture(SDL_GPImage image); // Get the width of an image successful pixels. Returns 0 if the image is invalid. int SDL_GPGetImageWidth(SDL_GPImage image); // Get the tallness of an image successful pixels. Returns 0 if the image is invalid. int SDL_GPGetImageHeight(SDL_GPImage image);

Shader API:

// Create a shader from vertex and part shader descriptions. Returns an // invalid shader if creation failed, Use SDL_GPGetLastError() to get more // accusation astir the error. SDL_GPShader SDL_GPCreateShader(SDL_GPShaderDesc *desc); // Get the SDL shader associated pinch a SDL_gp shader. Returns NULL if the // shader is invalid. SDL_GPUShader *SDL_GPGetGPUShader(SDL_GPShader shader); // Destroy a shader and free its resources. void SDL_GPDestroyShader(SDL_GPShader shader);

Pipeline API:

// Create a graphics pipeline, Returns an invalid pipeline if creation failed, // Use SDL_GPGetLastError() to get much accusation astir the error. SDL_GPPipeline SDL_GPCreatePipeline(SDL_GPShader shader_vert, SDL_GPShader shader_frag, SDL_GPPrimitiveType primitive_type, SDL_GPBlendMode blend_mode); // Destroy a graphics pipeline and free its resources. void SDL_GPDestroyPipeline(SDL_GPPipeline pipeline); // Get the GPU graphics pipeline associated pinch a SDL_gp pipeline. Returns // NULL if the pipeline is invalid. SDL_GPUGraphicsPipeline *SDL_GPGetGPUPipeline(SDL_GPPipeline pipeline);

Painter API:

// Setup SDL_GP context. Returns mendacious if setup failed, use // SDL_GPGetLastError() to get much accusation astir the error. bool SDL_GPSetup(SDL_GPDesc *desc); // Shutdown SDL_GP context. void SDL_GPShutdown(void); // Begin recoarding tie calls for the existent frame. This should beryllium called // aft mounting up SDL_gp and acquiring a swapchain texture and command // buffer for the existent frame. // If return mendacious past an correction occurred and the framework should beryllium skipped, // usage SDL_GPGetLastError() to get much accusation astir the error. bool SDL_GPBegin(int width, int height); // Flush the recorded tie calls to the GPU. Returns mendacious if an error // occurred, usage SDL_GPGetLastError() to get much accusation astir the error. bool SDL_GPFlush(SDL_GPUCommandBuffer *cmd_buffer, SDL_GPUTexture *texture); // End signaling tie calls for the existent frame. void SDL_GPEnd(void); // Set the coordinate abstraction boundaries successful the existent viewport. void SDL_GPSetProjection(float left, float right, float bottom, float top); // Reset the projection to the default coordinate space, which is the // coordinate of the existent viewport. void SDL_GPResetProjection(void); // Save the existent toggle shape matrix connected the toggle shape stack. To beryllium popular later // pinch SDL_GPPopTransform. void SDL_GPPushTransform(void); // Restore the toggle shape matrix from the apical of the toggle shape stack. void SDL_GPPopTransform(void); // Set the existent toggle shape matrix to personality (no transformation). void SDL_GPResetTransform(void); // Translates the 2D coordinates space. void SDL_GPTranslate(float x, float y); // Rotates the 2D coordinate abstraction astir the origin. void SDL_GPRotate(float angle); // Rotates the 2D coordinate abstraction astir a point. void SDL_GPRotateAt(float angle, float ax, float ay); // Scales the 2D coordinate abstraction astir the origin. void SDL_GPScale(float sx, float sy); // Scales the 2D coordinate abstraction astir a point. void SDL_GPScaleAt(float sx, float sy, float ax, float ay); // Set the existent graphics pipeline. void SDL_GPSetPipeline(SDL_GPPipeline pipeline); // Reset the graphics pipeline to the default pipeline builtin pipeline. void SDL_GPResetPipeline(void); // Set azygous information for the existent pipeline. void SDL_GPSetUniform(const void *vs_data, size_t vs_size, const void *fs_data, size_t fs_size); // Reset azygous information to the default authorities (current authorities color). void SDL_GPResetUniform(void); // Set the existent blend mode. void SDL_GPSetBlendMode(SDL_GPBlendMode blend_mode); // Reset the existent blend mode to the default blend mode (no blending). void SDL_GPResetBlendMode(void); // Sets existent color. void SDL_GPSetColor(SDL_Color color); // Gets existent color. SDL_Color SDL_GPGetColor(void); // Reset existent colour to the default colour (white). void SDL_GPResetColor(void); // Sets existent bound image successful a texture channel. void SDL_GPSetImage(int channel, SDL_GPImage image); // Remove existent bound image from a texture transmission (no texture). void SDL_GPUnsetImage(int channel); // Reset existent bound image successful a texture transmission to the default (white // texture). void SDL_GPResetImage(int channel); // Set existent bound sampler successful a texture channel. void SDL_GPSetSampler(int channel, SDL_GPUSampler *sampler); // Remove existent bound sampler from a texture transmission (no sampler). void SDL_GPUnsetSampler(int channel); // Reset existent bound sampler successful a texture transmission to default (nearest // sampler). void SDL_GPResetSampler(int channel); // Set the surface are to tie to. void SDL_GPViewport(int x, int y, int w, int h); // Reset the viewport to default (0, 0, width, height). void SDL_GPResetViewport(void); // Set the clipping rectangle successful the viewport. void SDL_GPScissor(int x, int y, int w, int h); // Reset the clipping rectangle to default (viewport bounds). void SDL_GPResetScissor(void); // Reset each authorities to default. void SDL_GPResetState(void); // Clear the existent viewport pinch the existent color. void SDL_GPClear(void); // Draw immoderate primitive. void SDL_GPDraw(SDL_GPPrimitiveType primitive_type, const SDL_GPVertex *vertices, Uint32 vertices_count); // Draw points successful batch. void SDL_GPDrawPoints(const SDL_GPPoint *points, Uint32 count); // Draw a azygous point. void SDL_GPDrawPoint(SDL_GPPoint point); // Draw lines successful batch. void SDL_GPDrawLines(const SDL_GPLine *lines, Uint32 count); // Draw a azygous line. void SDL_GPDrawLine(SDL_GPLine line); // Draw a stip of lines. void SDL_GPDrawLinesStrip(const SDL_GPVec2 *points, Uint32 count); // Draw triangles successful batch. void SDL_GPDrawFilledTriangles(const SDL_GPTriangle *triangles, Uint32 count); // Draw a azygous triangle. void SDL_GPDrawFilledTriangle(SDL_GPTriangle triangle); // Draw a portion of triangles. void SDL_GPDrawFilledTrianglesStrip(const SDL_GPVec2 *points, Uint32 count); // Draw rectangles successful batch. void SDL_GPDrawFilledRects(const SDL_GPRect *rects, Uint32 count); // Draw a azygous rectangle. void SDL_GPDrawFilledRect(SDL_GPRect rect); // Draw textured rectangles successful batch. void SDL_GPDrawTexturedRects(int channel, const SDL_GPTexturedRect *rects, Uint32 count); // Draw a azygous textured rectangle. void SDL_GPDrawTexturedRect(int channel, SDL_GPTexturedRect rect);

Hi everyone, I'm nsix, an indie crippled developer and unfastened root contributor trying to make a surviving from my work.

If you bask what I create and want to support me, see becoming a sponsor connected GitHub Sponsors. Every spot of support intends a batch and is greatly appreciated!

You tin besides cheque retired my blog astatine www.nsix.blog for much updates and insights into my work.

  • Edubart - Creator of the original sokol_gp that encouraged maine to create SDL_gp.
  • The SDL team - For processing and maintaining SDL3.

This task is licensed nether the MIT License - spot the LICENSE record for details.

More