WinRT: added support for SDL_HINT_RENDER_SCALE_QUALITY

This commit is contained in:
David Ludwig 2013-11-01 22:54:39 -04:00
parent ea12ff9faf
commit 7cc0951637
2 changed files with 52 additions and 6 deletions

View File

@ -60,6 +60,10 @@ using namespace Windows::Graphics::Display;
using namespace Windows::UI::Core; using namespace Windows::UI::Core;
#endif #endif
/* Texture sampling types */
static const D3D11_FILTER SDL_D3D11_NEAREST_PIXEL_FILTER = D3D11_FILTER_MIN_MAG_MIP_POINT;
static const D3D11_FILTER SDL_D3D11_LINEAR_FILTER = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
/* Direct3D 11.1 renderer implementation */ /* Direct3D 11.1 renderer implementation */
static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags); static SDL_Renderer *D3D11_CreateRenderer(SDL_Window * window, Uint32 flags);
@ -487,10 +491,10 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
data->vertexBuffer = nullptr; data->vertexBuffer = nullptr;
// //
// Create a sampler to use when drawing textures: // Create samplers to use when drawing textures:
// //
D3D11_SAMPLER_DESC samplerDesc; D3D11_SAMPLER_DESC samplerDesc;
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.Filter = SDL_D3D11_NEAREST_PIXEL_FILTER;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
@ -505,7 +509,17 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
result = data->d3dDevice->CreateSamplerState( result = data->d3dDevice->CreateSamplerState(
&samplerDesc, &samplerDesc,
&data->mainSampler &data->nearestPixelSampler
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
samplerDesc.Filter = SDL_D3D11_LINEAR_FILTER;
result = data->d3dDevice->CreateSamplerState(
&samplerDesc,
&data->linearSampler
); );
if (FAILED(result)) { if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result); WIN_SetErrorFromHRESULT(__FUNCTION__, result);
@ -928,6 +942,17 @@ D3D11_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
} }
} }
static D3D11_FILTER
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return SDL_D3D11_NEAREST_PIXEL_FILTER;
} else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
return SDL_D3D11_LINEAR_FILTER;
}
}
static int static int
D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ {
@ -948,6 +973,7 @@ D3D11_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
} }
textureData->pixelFormat = SDL_AllocFormat(texture->format); textureData->pixelFormat = SDL_AllocFormat(texture->format);
textureData->lockedTexturePosition = XMINT2(0, 0); textureData->lockedTexturePosition = XMINT2(0, 0);
textureData->scaleMode = GetScaleQuality();
texture->driverdata = textureData; texture->driverdata = textureData;
@ -1621,6 +1647,22 @@ D3D11_RenderFillRects(SDL_Renderer * renderer,
return 0; return 0;
} }
static ID3D11SamplerState *
D3D11_RenderGetSampler(SDL_Renderer * renderer, SDL_Texture * texture)
{
D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
switch (textureData->scaleMode) {
case SDL_D3D11_NEAREST_PIXEL_FILTER:
return rendererData->nearestPixelSampler.Get();
case SDL_D3D11_LINEAR_FILTER:
return rendererData->linearSampler.Get();
default:
return NULL;
}
}
static int static int
D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect) const SDL_Rect * srcrect, const SDL_FRect * dstrect)
@ -1659,11 +1701,12 @@ D3D11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
return -1; return -1;
} }
ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
D3D11_SetPixelShader( D3D11_SetPixelShader(
renderer, renderer,
rendererData->texturePixelShader.Get(), rendererData->texturePixelShader.Get(),
textureData->mainTextureResourceView.Get(), textureData->mainTextureResourceView.Get(),
rendererData->mainSampler.Get()); textureSampler);
D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor)); D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));
@ -1733,11 +1776,12 @@ D3D11_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
return -1; return -1;
} }
ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
D3D11_SetPixelShader( D3D11_SetPixelShader(
renderer, renderer,
rendererData->texturePixelShader.Get(), rendererData->texturePixelShader.Get(),
textureData->mainTextureResourceView.Get(), textureData->mainTextureResourceView.Get(),
rendererData->mainSampler.Get()); textureSampler);
D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor)); D3D11_RenderFinishDrawOp(renderer, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, sizeof(vertices) / sizeof(VertexPositionColor));

View File

@ -47,7 +47,8 @@ typedef struct
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend; Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend;
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd; Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd;
Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod; Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod;
Microsoft::WRL::ComPtr<ID3D11SamplerState> mainSampler; Microsoft::WRL::ComPtr<ID3D11SamplerState> nearestPixelSampler;
Microsoft::WRL::ComPtr<ID3D11SamplerState> linearSampler;
Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer; Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
D3D_FEATURE_LEVEL featureLevel; D3D_FEATURE_LEVEL featureLevel;
@ -72,6 +73,7 @@ typedef struct
SDL_PixelFormat * pixelFormat; SDL_PixelFormat * pixelFormat;
Microsoft::WRL::ComPtr<ID3D11Texture2D> stagingTexture; Microsoft::WRL::ComPtr<ID3D11Texture2D> stagingTexture;
DirectX::XMINT2 lockedTexturePosition; DirectX::XMINT2 lockedTexturePosition;
D3D11_FILTER scaleMode;
} D3D11_TextureData; } D3D11_TextureData;
struct VertexPositionColor struct VertexPositionColor