Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Mar 2011 11:18:35 -0700
changeset 5484e20c93bc9122
parent 5483 ccec742c9b7d
child 5485 4cf1ad60ad47
Added the SDL_HINT_RENDER_SCALE_QUALITY hint, which defaults to nearest pixel sampling.
include/SDL_hints.h
src/render/direct3d/SDL_render_d3d.c
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/include/SDL_hints.h	Sun Mar 13 11:17:11 2011 -0700
     1.2 +++ b/include/SDL_hints.h	Sun Mar 13 11:18:35 2011 -0700
     1.3 @@ -98,6 +98,18 @@
     1.4  #define SDL_HINT_RENDER_OPENGL_SHADERS      "SDL_RENDER_OPENGL_SHADERS"
     1.5  
     1.6  /**
     1.7 + *  \brief  A variable controlling the scaling quality
     1.8 + *
     1.9 + *  This variable can be set to the following values:
    1.10 + *    "0" or "nearest" - Nearest pixel sampling
    1.11 + *    "1" or "linear"  - Linear filtering (supported by OpenGL and Direct3D)
    1.12 + *    "2" or "best"    - Anisotropic filtering (supported by Direct3D)
    1.13 + *
    1.14 + *  By default nearest pixel sampling is used
    1.15 + */
    1.16 +#define SDL_HINT_RENDER_SCALE_QUALITY       "SDL_RENDER_SCALE_QUALITY"
    1.17 +
    1.18 +/**
    1.19   *  \brief  A variable controlling whether updates to the SDL 1.2 screen surface should be synchronized with the vertical refresh, to avoid tearing.
    1.20   *
    1.21   *  This variable can be set to the following values:
     2.1 --- a/src/render/direct3d/SDL_render_d3d.c	Sun Mar 13 11:17:11 2011 -0700
     2.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Sun Mar 13 11:18:35 2011 -0700
     2.3 @@ -26,6 +26,7 @@
     2.4  
     2.5  #include "../../core/windows/SDL_windows.h"
     2.6  
     2.7 +#include "SDL_hints.h"
     2.8  #include "SDL_loadso.h"
     2.9  #include "SDL_syswm.h"
    2.10  #include "../SDL_sysrender.h"
    2.11 @@ -137,11 +138,13 @@
    2.12      D3DPRESENT_PARAMETERS pparams;
    2.13      SDL_bool updateSize;
    2.14      SDL_bool beginScene;
    2.15 +    D3DTEXTUREFILTERTYPE scaleMode;
    2.16  } D3D_RenderData;
    2.17  
    2.18  typedef struct
    2.19  {
    2.20      IDirect3DTexture9 *texture;
    2.21 +    D3DTEXTUREFILTERTYPE scaleMode;
    2.22  } D3D_TextureData;
    2.23  
    2.24  typedef struct
    2.25 @@ -451,6 +454,7 @@
    2.26          return NULL;
    2.27      }
    2.28      data->beginScene = SDL_TRUE;
    2.29 +    data->scaleMode = D3DTEXF_FORCE_DWORD;
    2.30  
    2.31      /* Get presentation parameters to fill info */
    2.32      result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
    2.33 @@ -537,6 +541,20 @@
    2.34      }
    2.35  }
    2.36  
    2.37 +static D3DTEXTUREFILTERTYPE
    2.38 +GetScaleQuality(void)
    2.39 +{
    2.40 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
    2.41 +
    2.42 +    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
    2.43 +        return D3DTEXF_POINT;
    2.44 +    } else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
    2.45 +        return D3DTEXF_LINEAR;
    2.46 +    } else {
    2.47 +        return D3DTEXF_ANISOTROPIC;
    2.48 +    }
    2.49 +}
    2.50 +
    2.51  static int
    2.52  D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    2.53  {
    2.54 @@ -553,6 +571,7 @@
    2.55          SDL_OutOfMemory();
    2.56          return -1;
    2.57      }
    2.58 +    data->scaleMode = GetScaleQuality();
    2.59  
    2.60      texture->driverdata = data;
    2.61  
    2.62 @@ -1018,10 +1037,13 @@
    2.63  
    2.64      D3D_SetBlendMode(data, texture->blendMode);
    2.65  
    2.66 -    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
    2.67 -                                     D3DTEXF_LINEAR);
    2.68 -    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
    2.69 -                                     D3DTEXF_LINEAR);
    2.70 +    if (texturedata->scaleMode != data->scaleMode) {
    2.71 +        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
    2.72 +                                         texturedata->scaleMode);
    2.73 +        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
    2.74 +                                         texturedata->scaleMode);
    2.75 +        data->scaleMode = texturedata->scaleMode;
    2.76 +    }
    2.77  
    2.78      result =
    2.79          IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
     3.1 --- a/src/render/opengl/SDL_render_gl.c	Sun Mar 13 11:17:11 2011 -0700
     3.2 +++ b/src/render/opengl/SDL_render_gl.c	Sun Mar 13 11:18:35 2011 -0700
     3.3 @@ -393,6 +393,18 @@
     3.4      return SDL_TRUE;
     3.5  }
     3.6  
     3.7 +static GLenum
     3.8 +GetScaleQuality(void)
     3.9 +{
    3.10 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
    3.11 +
    3.12 +    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
    3.13 +        return GL_NEAREST;
    3.14 +    } else {
    3.15 +        return GL_LINEAR;
    3.16 +    }
    3.17 +}
    3.18 +
    3.19  static int
    3.20  GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    3.21  {
    3.22 @@ -455,7 +467,7 @@
    3.23  
    3.24      data->format = format;
    3.25      data->formattype = type;
    3.26 -    data->scaleMode = GL_LINEAR;
    3.27 +    data->scaleMode = GetScaleQuality();
    3.28      renderdata->glEnable(data->type);
    3.29      renderdata->glBindTexture(data->type, data->texture);
    3.30      renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
     4.1 --- a/src/render/opengles/SDL_render_gles.c	Sun Mar 13 11:17:11 2011 -0700
     4.2 +++ b/src/render/opengles/SDL_render_gles.c	Sun Mar 13 11:18:35 2011 -0700
     4.3 @@ -23,6 +23,7 @@
     4.4  
     4.5  #if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED
     4.6  
     4.7 +#include "SDL_hints.h"
     4.8  #include "SDL_opengles.h"
     4.9  #include "../SDL_sysrender.h"
    4.10  
    4.11 @@ -292,6 +293,18 @@
    4.12      return value;
    4.13  }
    4.14  
    4.15 +static GLenum
    4.16 +GetScaleQuality(void)
    4.17 +{
    4.18 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
    4.19 +
    4.20 +    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
    4.21 +        return GL_NEAREST;
    4.22 +    } else {
    4.23 +        return GL_LINEAR;
    4.24 +    }
    4.25 +}
    4.26 +
    4.27  static int
    4.28  GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    4.29  {
    4.30 @@ -345,7 +358,7 @@
    4.31  
    4.32      data->format = format;
    4.33      data->formattype = type;
    4.34 -    data->scaleMode = GL_LINEAR;
    4.35 +    data->scaleMode = GetScaleQuality();
    4.36      glBindTexture(data->type, data->texture);
    4.37      glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    4.38      glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     5.1 --- a/src/render/opengles2/SDL_render_gles2.c	Sun Mar 13 11:17:11 2011 -0700
     5.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Sun Mar 13 11:18:35 2011 -0700
     5.3 @@ -24,6 +24,7 @@
     5.4  
     5.5  #if SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED
     5.6  
     5.7 +#include "SDL_hints.h"
     5.8  #include "SDL_opengles2.h"
     5.9  #include "../SDL_sysrender.h"
    5.10  #include "SDL_shaders_gles2.h"
    5.11 @@ -234,6 +235,18 @@
    5.12  static int GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
    5.13                                 const void *pixels, int pitch);
    5.14  
    5.15 +static GLenum
    5.16 +GetScaleQuality(void)
    5.17 +{
    5.18 +    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
    5.19 +
    5.20 +    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
    5.21 +        return GL_NEAREST;
    5.22 +    } else {
    5.23 +        return GL_LINEAR;
    5.24 +    }
    5.25 +}
    5.26 +
    5.27  static int
    5.28  GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
    5.29  {
    5.30 @@ -266,7 +279,7 @@
    5.31      tdata->texture_type = GL_TEXTURE_2D;
    5.32      tdata->pixel_format = format;
    5.33      tdata->pixel_type = type;
    5.34 -    tdata->scaleMode = GL_LINEAR;
    5.35 +    tdata->scaleMode = GetScaleQuality();
    5.36  
    5.37      /* Allocate a blob for image data */
    5.38      if (texture->access == SDL_TEXTUREACCESS_STREAMING)
    5.39 @@ -286,8 +299,6 @@
    5.40      glGenTextures(1, &tdata->texture);
    5.41      glActiveTexture(GL_TEXTURE0);
    5.42      glBindTexture(tdata->texture_type, tdata->texture);
    5.43 -    glTexParameteri(tdata->texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    5.44 -    glTexParameteri(tdata->texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    5.45      glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    5.46      glTexParameteri(tdata->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    5.47      glTexImage2D(tdata->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);