We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2269

Summary: SDL_RenderSetClipRect doesn't work properly when use SDL_RenderSetLogicalSize
Product: SDL Reporter: neo <neo333_email>
Component: renderAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: leonardo.guilherme, raincomplex, valentin.soudier, vii
Version: 2.0.1Keywords: target-2.0.4
Hardware: x86_64   
OS: Windows 8   
Attachments: An example from code.
Uses viewport x and y to set cliprect x/y
add viewport x/y to clipping rectangle
Adds viewport x/y to cliprect in UpdateClipRect + D3D

Description neo 2013-11-24 16:35:06 UTC
Created attachment 1473 [details]
An example from code.

-------------Info machine:---------
OS: Win8 (64bit)
SDL Version: 2.0.1
HW Video: AMD Radeon HD 7600M series
------------------------------------

--------------Code example:-----------------------------

//Now i'm window 1280x800 [for example]
SDL_Window* win = SDL_CreateWindow("title",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1280,800,SDL_WINDOW_RESIZABLE);

... //[create renderer with SDL_RENDERER_ACCELERATED]

//My application is designed for 800x600
SDL_RenderSetLogicalSize(renderer,800,600);


SDL_Texture* texture = ...	//load all


//main loop
while(true){
	 SDL_RenderClear(renderer);
     SDL_RenderSetClipRect(renderer, Rect(0,0,800,600)); //Rect = easy SDL_Rect wrapper class.
     SDL_RenderCopy(renderer,texture,NULL,NULL);
	 SDL_RenderSetClipRect(renderer,NULL);
	 SDL_RenderPresent(renderer);
}

-------------------------------------------------------

As you can see from the attached 'SDL_RenderSetClipRect'  does not work well 
because the area of the clip does not coincide with the area set on 'SDL_RenderSetLogicalSize'.
Comment 1 valentin.soudier 2014-01-06 15:19:57 UTC
*** Bug 2336 has been marked as a duplicate of this bug. ***
Comment 2 Leonardo 2014-06-07 20:15:50 UTC
Created attachment 1674 [details]
Uses viewport x and y to set cliprect x/y

Hello. This bug happens because the viewport x/y is not being taken in consideration when setting the clipping rectangle (and render logical size is implemented as a combination of viewport and scale)

With the proposed patch, I've fixed this problem for the OpenGL[ES[2]] renderers but I have no idea if such problems occur in the DirectX renderer (couldn't be sure after superficially analyzing its code). Also I havent tested it in more odd combinations of scaling, viewport, target textures, etc.

As a workaround until this is fixed, one can get the viewport and add its x/y coordinates to the clipping rectangle:

SDL_Rect clip;
SDL_RenderGetViewport(renderer, &clip);
clip.x += 100, clip.y += 100, clip.w = 100, clip.w = 100;
SDL_RenderSetClipRect(renderer, &clip);
Comment 3 raincomplex 2014-09-26 02:21:40 UTC
Created attachment 1882 [details]
add viewport x/y to clipping rectangle

Patch fixes the issue.

Workaround for unpatched SDL:

    SDL_Rect c = {...};  // clipping rectangle
    SDL_Rect vp;
    SDL_RenderGetViewport(renderer, &vp);
    c.x += vp.x;
    c.y -= vp.y;
    SDL_RenderSetClipRect(renderer, &c);
Comment 4 Vii 2015-01-30 13:07:38 UTC
Created attachment 2010 [details]
Adds viewport x/y to cliprect in UpdateClipRect + D3D

Hello. I was affected by this bug and found Leonardo's patch to work better than raincomplex's. I guess it's better to add the viewport coordinates when the driver updates the scissor rect than to do it beforehand.
Attached patch is the same as Leonardo's but with fix for D3D added.
Comment 5 Ryan C. Gordon 2015-02-18 21:44:21 UTC
*** Bug 2336 has been marked as a duplicate of this bug. ***
Comment 6 Sam Lantinga 2015-05-29 02:01:54 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/1d13a878b066