Texture's edges are jagged when being rendered with rotation

I create a 200x20 texture full of white pixel and write a simple demo to draw this texture. If I use SDL_RenderCopyExF and specify the rotation angle, the renderer seems to be using nearest neighbor interpolation to render the texture, results in jagged edges. Neither setting the hint SDL_HINT_RENDER_SCALE_QUALITY to 'linear' nor setting the texture’s scale mode to SDL_ScaleModeLinear helps. Is this made by design? Or is this a bug? What should I do if I want the rotated texture being rendered smoothly?

#define SDL_MAIN_HANDLED

#include <SDL2/SDL.h>
#include <SDL2/SDL_main.h>
#include <SDL2/SDL_image.h>

#include <Windows.h>

int main()
{
	SetProcessDPIAware();

	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");

	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Window* window_ptr;
	SDL_Renderer* renderer_ptr;
	SDL_CreateWindowAndRenderer(800, 600, 0, &window_ptr, &renderer_ptr);

	SDL_Texture* rectange_ptr = IMG_LoadTexture(renderer_ptr, "rect.bmp");
	SDL_SetTextureScaleMode(rectange_ptr, SDL_ScaleModeLinear);

	SDL_Rect src = { 0,0,0,0 };
	SDL_QueryTexture(rectange_ptr, nullptr, nullptr, &src.w, &src.h);

	SDL_FRect dst = { 400 - src.w / 2.0f, 300 - src.h / 2.0f, src.w, src.h };

	while (true)
	{
		SDL_PumpEvents();

		SDL_SetRenderDrawColor(renderer_ptr, 0, 0, 0, 255);
		SDL_RenderClear(renderer_ptr);

		SDL_RenderCopyExF(renderer_ptr, rectange_ptr,
						  &src, &dst, 30, nullptr, SDL_FLIP_NONE);

		SDL_RenderPresent(renderer_ptr);
	}

	SDL_Quit();
}

It’s because the white rectangle in your texture goes all the way to the texture’s edge.

SDL is just feeding textured polygons to your GPU; if your texture’s contents go all the way to the edge of it, when rotating it you’re gonna see jaggies at the polygon edges, just like in a 3D game with anti-aliasing turned off.

To get rid of this, make the texture 2 pixels bigger in each dimension than the white rectangle, with scale mode set to linear.

1 Like

Thank you. However, the problem I met is that I’m using a UI library that draws lines by transforming a 1x1 white texture and I do not want to modify its source code. I’ve done some research, and found that multi-samping can help. But as far as I know, SDL only offers functions to set OpenGL’s multi-sampling. Is there a way to set DirectX’s or Metal’s multi-sampling by SDL instead of directly call their own APIs?