Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to neare…
Browse files Browse the repository at this point in the history
…st pixel sampling.
  • Loading branch information
slouken committed Mar 13, 2011
1 parent d4f488a commit 38ed0f2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
12 changes: 12 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -97,6 +97,18 @@ extern "C" {
*/
#define SDL_HINT_RENDER_OPENGL_SHADERS "SDL_RENDER_OPENGL_SHADERS"

/**
* \brief A variable controlling the scaling quality
*
* This variable can be set to the following values:
* "0" or "nearest" - Nearest pixel sampling
* "1" or "linear" - Linear filtering (supported by OpenGL and Direct3D)
* "2" or "best" - Anisotropic filtering (supported by Direct3D)
*
* By default nearest pixel sampling is used
*/
#define SDL_HINT_RENDER_SCALE_QUALITY "SDL_RENDER_SCALE_QUALITY"

/**
* \brief A variable controlling whether updates to the SDL 1.2 screen surface should be synchronized with the vertical refresh, to avoid tearing.
*
Expand Down
30 changes: 26 additions & 4 deletions src/render/direct3d/SDL_render_d3d.c
Expand Up @@ -26,6 +26,7 @@

#include "../../core/windows/SDL_windows.h"

#include "SDL_hints.h"
#include "SDL_loadso.h"
#include "SDL_syswm.h"
#include "../SDL_sysrender.h"
Expand Down Expand Up @@ -137,11 +138,13 @@ typedef struct
D3DPRESENT_PARAMETERS pparams;
SDL_bool updateSize;
SDL_bool beginScene;
D3DTEXTUREFILTERTYPE scaleMode;
} D3D_RenderData;

typedef struct
{
IDirect3DTexture9 *texture;
D3DTEXTUREFILTERTYPE scaleMode;
} D3D_TextureData;

typedef struct
Expand Down Expand Up @@ -451,6 +454,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
return NULL;
}
data->beginScene = SDL_TRUE;
data->scaleMode = D3DTEXF_FORCE_DWORD;

/* Get presentation parameters to fill info */
result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
Expand Down Expand Up @@ -537,6 +541,20 @@ D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
}
}

static D3DTEXTUREFILTERTYPE
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);

if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return D3DTEXF_POINT;
} else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
return D3DTEXF_LINEAR;
} else {
return D3DTEXF_ANISOTROPIC;
}
}

static int
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Expand All @@ -553,6 +571,7 @@ D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
SDL_OutOfMemory();
return -1;
}
data->scaleMode = GetScaleQuality();

texture->driverdata = data;

Expand Down Expand Up @@ -1018,10 +1037,13 @@ D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,

D3D_SetBlendMode(data, texture->blendMode);

IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
D3DTEXF_LINEAR);
IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
D3DTEXF_LINEAR);
if (texturedata->scaleMode != data->scaleMode) {
IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
texturedata->scaleMode);
IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
texturedata->scaleMode);
data->scaleMode = texturedata->scaleMode;
}

result =
IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
Expand Down
14 changes: 13 additions & 1 deletion src/render/opengl/SDL_render_gl.c
Expand Up @@ -393,6 +393,18 @@ convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
return SDL_TRUE;
}

static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);

if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}

static int
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Expand Down Expand Up @@ -455,7 +467,7 @@ GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)

data->format = format;
data->formattype = type;
data->scaleMode = GL_LINEAR;
data->scaleMode = GetScaleQuality();
renderdata->glEnable(data->type);
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
Expand Down
15 changes: 14 additions & 1 deletion src/render/opengles/SDL_render_gles.c
Expand Up @@ -23,6 +23,7 @@

#if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED

#include "SDL_hints.h"
#include "SDL_opengles.h"
#include "../SDL_sysrender.h"

Expand Down Expand Up @@ -292,6 +293,18 @@ power_of_2(int input)
return value;
}

static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);

if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}

static int
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Expand Down Expand Up @@ -345,7 +358,7 @@ GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)

data->format = format;
data->formattype = type;
data->scaleMode = GL_LINEAR;
data->scaleMode = GetScaleQuality();
glBindTexture(data->type, data->texture);
glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand Down
17 changes: 14 additions & 3 deletions src/render/opengles2/SDL_render_gles2.c
Expand Up @@ -24,6 +24,7 @@

#if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED

#include "SDL_hints.h"
#include "SDL_opengles2.h"
#include "../SDL_sysrender.h"
#include "SDL_shaders_gles2.h"
Expand Down Expand Up @@ -234,6 +235,18 @@ static void GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture);
static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
const void *pixels, int pitch);

static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);

if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}

static int
GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
Expand Down Expand Up @@ -266,7 +279,7 @@ GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
tdata->texture_type = GL_TEXTURE_2D;
tdata->pixel_format = format;
tdata->pixel_type = type;
tdata->scaleMode = GL_LINEAR;
tdata->scaleMode = GetScaleQuality();

/* Allocate a blob for image data */
if (texture->access == SDL_TEXTUREACCESS_STREAMING)
Expand All @@ -286,8 +299,6 @@ GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
glGenTextures(1, &tdata->texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(tdata->texture_type, tdata->texture);
glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
Expand Down

0 comments on commit 38ed0f2

Please sign in to comment.