Skip to content

[SDL3] Vita: Fixed absence of clipping when viewport is set #13240

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

Merged
merged 2 commits into from
Jun 20, 2025
Merged
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
78 changes: 69 additions & 9 deletions src/render/vitagxm/SDL_render_vita_gxm.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,35 @@ static SceGxmTextureAddrMode TranslateAddressMode(SDL_TextureAddressMode mode)
}
}

static void ClampCliprectToViewport(SDL_Rect *clip, const SDL_Rect *viewport)
{
int max_x_v, max_y_v, max_x_c, max_y_c;

if (clip->x < 0) {
clip->w += clip->x;
clip->x = 0;
}

if (clip->y < 0) {
clip->h += clip->y;
clip->y = 0;
}

max_x_c = clip->x + clip->w;
max_y_c = clip->y + clip->h;

max_x_v = viewport->x + viewport->w;
max_y_v = viewport->y + viewport->h;

if (max_x_c > max_x_v) {
clip->w -= (max_x_v - max_x_c);
}

if (max_y_c > max_y_v) {
clip->h -= (max_y_v - max_y_c);
}
}

static bool SetDrawState(VITA_GXM_RenderData *data, const SDL_RenderCommand *cmd)
{
SDL_Texture *texture = cmd->data.draw.texture;
Expand Down Expand Up @@ -862,9 +891,13 @@ static bool SetDrawState(VITA_GXM_RenderData *data, const SDL_RenderCommand *cmd
data->drawstate.cliprect_enabled_dirty = false;
}

if (data->drawstate.cliprect_enabled && data->drawstate.cliprect_dirty) {
const SDL_Rect *rect = &data->drawstate.cliprect;
set_clip_rectangle(data, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
if ((data->drawstate.cliprect_enabled || data->drawstate.viewport_is_set) && data->drawstate.cliprect_dirty) {
SDL_Rect rect;
SDL_copyp(&rect, &data->drawstate.cliprect);
if (data->drawstate.viewport_is_set) {
ClampCliprectToViewport(&rect, &data->drawstate.viewport);
}
set_clip_rectangle(data, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h);
data->drawstate.cliprect_dirty = false;
}

Expand Down Expand Up @@ -952,20 +985,31 @@ static void VITA_GXM_InvalidateCachedState(SDL_Renderer *renderer)
static bool VITA_GXM_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
VITA_GXM_RenderData *data = (VITA_GXM_RenderData *)renderer->internal;
int w, h;

StartDrawing(renderer);

data->drawstate.target = renderer->target;
if (!data->drawstate.target) {
int w, h;
SDL_GetWindowSizeInPixels(renderer->window, &w, &h);
if ((w != data->drawstate.drawablew) || (h != data->drawstate.drawableh)) {
data->drawstate.viewport_dirty = true; // if the window dimensions changed, invalidate the current viewport, etc.
data->drawstate.cliprect_dirty = true;
data->drawstate.drawablew = w;
data->drawstate.drawableh = h;
} else {
float fw, fh;
if (!SDL_GetTextureSize(renderer->target, &fw, &fh)) {
w = data->drawstate.drawablew;
h = data->drawstate.drawableh;
} else {
w = (int)SDL_roundf(fw);
h = (int)SDL_roundf(fh);
}
}

if ((w != data->drawstate.drawablew) || (h != data->drawstate.drawableh)) {
data->drawstate.viewport_dirty = true; // if the window dimensions changed, invalidate the current viewport, etc.
data->drawstate.cliprect_dirty = true;
data->drawstate.drawablew = w;
data->drawstate.drawableh = h;
}

while (cmd) {
switch (cmd->command) {

Expand All @@ -976,16 +1020,32 @@ static bool VITA_GXM_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *
SDL_copyp(viewport, &cmd->data.viewport.rect);
data->drawstate.viewport_dirty = true;
data->drawstate.cliprect_dirty = true;
data->drawstate.viewport_is_set = viewport->x != 0 || viewport->y != 0 || viewport->w != data->drawstate.drawablew || viewport->h != data->drawstate.drawableh;
if (!data->drawstate.cliprect_enabled) {
if (data->drawstate.viewport_is_set) {
SDL_copyp(&data->drawstate.cliprect, viewport);
data->drawstate.cliprect.x = 0;
data->drawstate.cliprect.y = 0;
} else {
data->drawstate.cliprect_enabled_dirty = true;
}
}
}
break;
}

case SDL_RENDERCMD_SETCLIPRECT:
{
const SDL_Rect *rect = &cmd->data.cliprect.rect;
const SDL_Rect *viewport = &data->drawstate.viewport;
if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
data->drawstate.cliprect_enabled_dirty = true;
if (!data->drawstate.cliprect_enabled && data->drawstate.viewport_is_set) {
SDL_copyp(&data->drawstate.cliprect, viewport);
data->drawstate.cliprect.x = 0;
data->drawstate.cliprect.y = 0;
}
}

if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof(*rect)) != 0) {
Expand Down
1 change: 1 addition & 0 deletions src/render/vitagxm/SDL_render_vita_gxm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef struct
{
SDL_Rect viewport;
bool viewport_dirty;
bool viewport_is_set;
SDL_Texture *texture;
SDL_Texture *target;
SDL_FColor color;
Expand Down
Loading