Skip to content

Commit

Permalink
successfully add hq2x shader
Browse files Browse the repository at this point in the history
  • Loading branch information
duchuule committed Sep 24, 2015
1 parent a242de0 commit de888fc
Show file tree
Hide file tree
Showing 18 changed files with 490 additions and 96 deletions.
25 changes: 22 additions & 3 deletions DXSpriteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Engine
DXSpriteBatch::DXSpriteBatch(ID3D11Device1 *device, ID3D11DeviceContext1 *context, float width, float height)
: device(device), context(context),
batchedSprites(0), beginCalled(false),
customPS(nullptr), _useFilter(false)
customPS(nullptr), _useFilter(false), customSRV(nullptr)
{
this->queuedSprites.reserve(MAX_BATCH_SIZE);
this->LoadShaders();
Expand Down Expand Up @@ -269,6 +269,11 @@ namespace Engine
this->customPS = (ID3D11PixelShader *) customPS;
}

void DXSpriteBatch::SetCustomShaderResourceView(void *customSRV)
{
this->customSRV = (ID3D11ShaderResourceView *)customSRV;
}

void DXSpriteBatch::Begin(XMMATRIX &world, bool useFilter)
{
if (_useFilter != useFilter)
Expand Down Expand Up @@ -441,15 +446,18 @@ namespace Engine
this->beginCalled = false;
if(this->batchedSprites == 0)
{
this->SetCustomPixelShader(nullptr);
this->customPS = nullptr;
this->customSRV = nullptr;
return;
}

//sort_heap(this->queuedSprites.begin(), this->queuedSprites.end(), this->heapSort);

this->FlushBatch();

this->SetCustomPixelShader(nullptr);
this->customPS = nullptr;
this->customSRV = nullptr;

}

void DXSpriteBatch::FlushBatch(void)
Expand Down Expand Up @@ -624,6 +632,8 @@ namespace Engine
this->context->RSSetState(this->rasterizerState.Get());
this->context->PSSetSamplers(0, 1, this->samplerState.GetAddressOf());



// Shaders
this->context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
this->context->IASetInputLayout(this->inputLayout.Get());
Expand All @@ -636,6 +646,15 @@ namespace Engine
this->context->PSSetShader(this->ps.Get(), nullptr, 0);
}
this->context->PSSetShaderResources(0, 1, &srv);
if (this->customSRV) //custom shader resource
{
ID3D11ShaderResourceView *customsrv = nullptr;
customsrv = this->customSRV.Get();
this->context->PSSetShaderResources(1, 1, &customsrv);

//IMPORTANT: the following line does *NOT* work, even though it compile
//this->context->PSSetShaderResources(1, 1, &this->customSRV);
}

// Buffers
UINT stride = sizeof(SpriteVertex);
Expand Down
3 changes: 3 additions & 0 deletions DXSpriteBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace Engine
ComPtr<ID3D11RasterizerState> rasterizerState;
ComPtr<ID3D11SamplerState> samplerState;

ComPtr<ID3D11ShaderResourceView> customSRV; //use for look up table or anything

bool beginCalled;
UINT batchedSprites;
vector<SpriteInfo> queuedSprites;
Expand All @@ -82,6 +84,7 @@ namespace Engine

void OnResize(float width, float height);
void SetCustomPixelShader(void *customPS);
void SetCustomShaderResourceView(void *customSRV);

void Begin(XMMATRIX &world, bool filter);
void Draw(const Rectangle &targetArea, const Rectangle *sourceArea, ID3D11ShaderResourceView *textureSRV, ID3D11Texture2D *texture, float depth, float rotation, Color &color);
Expand Down
66 changes: 36 additions & 30 deletions EmulatorRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace VBA10
void EmulatorRenderer::CreateDeviceDependentResources()
{
//intialize the shader loader
this->shaderManager = new ShaderManager(m_deviceResources->GetD3DDevice());
this->shaderManager = new ShaderManager(m_deviceResources->GetD3DDevice(), m_deviceResources->GetD3DDeviceContext());
this->shaderManager->LoadShader(EmulatorSettings::Current->PixelShader);

//this does not seem neccessary
Expand Down Expand Up @@ -472,29 +472,30 @@ namespace VBA10

turboSkip = EmulatorSettings::Current->TurboFrameSkip;

//systemFrameSkip = GetPowerFrameSkip();
//float targetFPS = 60.0f;
//if(GetMonitorType() == 0)
//{
// systemFrameSkip = systemFrameSkip * 2 + 1;
// targetFPS = 30.0f;
//}
//if(GetFrameSkip() == -1 && GetPowerFrameSkip() == 0)
//{
// if(!lastSkipped && (this->lastElapsed * 1.0f) > (1.0f / targetFPS))
// {
// int skip = (int)((this->lastElapsed * 1.0f) / (1.0f / targetFPS));
// systemFrameSkip += (skip < 2) ? skip : 2;
// //systemFrameSkip++;
// lastSkipped = true;
// }else
// {
// lastSkipped = false;
// }
//}else if(GetFrameSkip() >= 0)
// {
// systemFrameSkip += GetFrameSkip();
// }
systemFrameSkip = 0; //GetPowerFrameSkip();
float targetFPS = 60.0f;
if(GetMonitorType() == 0)
{
systemFrameSkip = systemFrameSkip * 2 + 1;
targetFPS = 30.0f;
}
if(EmulatorSettings::Current->FrameSkip == -1 ) //&& GetPowerFrameSkip() == 0)
{
if(!lastSkipped && (this->lastElapsed * 1.0f) > (1.0f / targetFPS))
{
int skip = (int)((this->lastElapsed * 1.0f) / (1.0f / targetFPS));
systemFrameSkip += (skip < 2) ? skip : 2;
//systemFrameSkip++;
lastSkipped = true;
}else
{
lastSkipped = false;
}
}
else if (EmulatorSettings::Current->FrameSkip >= 0)
{
systemFrameSkip += EmulatorSettings::Current->FrameSkip;
}

this->Autosave();

Expand Down Expand Up @@ -704,26 +705,30 @@ namespace VBA10
//XMVECTOR colorv = XMLoadFloat4A(&colorf);
//XMVECTOR colorv2 = XMLoadFloat4A(&colorf2);

// Render last frame to screen
// Render current frame to screen
Color white(1.0f, 1.0f, 1.0f, 1.0f);

Color color(1.0f, 1.0f, 1.0f, 1.0f);
if (this->width > this->height) //landscape
color = Color(1.0f, 1.0f, 1.0f, GetControllerOpacity()/100.0f);





XMMATRIX x = XMLoadFloat4x4(&this->outputTransform);

//<-------begin drawing main picture

//force linear filter to disabled if using pixel shader
if (EmulatorSettings::Current->LinearFilterEnabled == false || EmulatorSettings::Current->PixelShader > 0 )
if (EmulatorSettings::Current->PixelShader > 0 )
this->dxSpriteBatch->Begin(x, false);
else
this->dxSpriteBatch->Begin(x, true);
this->dxSpriteBatch->Begin(x, EmulatorSettings::Current->LinearFilterEnabled);

if (EmulatorSettings::Current->PixelShader != 0)
this->dxSpriteBatch->SetCustomPixelShader(this->shaderManager->GetCurrentShader());

if (EmulatorSettings::Current->PixelShader == 1) //hq2x
this->dxSpriteBatch->SetCustomShaderResourceView(this->shaderManager->GetLookUpTable());
Engine::Rectangle sourceRect(source.left, source.top, source.right - source.left, source.bottom - source.top);
Engine::Rectangle targetRect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);

Expand All @@ -732,7 +737,8 @@ namespace VBA10



this->dxSpriteBatch->End(); //end of drawing main game picture
this->dxSpriteBatch->End();
//-------> end of drawing main game picture

//drawing virtual buttons
this->dxSpriteBatch->Begin(x, true);
Expand Down
18 changes: 17 additions & 1 deletion EmulatorSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ namespace VBA10
}
}


property int FrameSkip
{
int get()
{
return GetValueOrDefault<int>(FrameSkipKey, FrameSkipDefault);
}
void set(int value)
{
AddOrUpdateValue(FrameSkipKey, value);
}
}


property int TurboFrameSkip
{
int get()
Expand Down Expand Up @@ -635,6 +649,7 @@ namespace VBA10
Platform::String^ ControllerScaleKey = "ControllerScaleKey";
Platform::String^ ButtonScaleKey = "ButtonScaleKey";
Platform::String^ DPadStyleKey = "DPadStyleKey";
Platform::String^ FrameSkipKey = "FrameSkipKey";
Platform::String^ TurboFrameSkipKey = "TurboFrameSkipKey";
Platform::String^ EnableTurboKey = "EnableTurboKey";
Platform::String^ FullScreenKey = "FullScreenKey";
Expand Down Expand Up @@ -691,7 +706,8 @@ namespace VBA10
const int ControllerScaleDefault = 100;
const int ButtonScaleDefault = 100;
const int DPadStyleDefault = 0;
const int TurboFrameSkipDefault = 2;
const int FrameSkipDefault = 0; //skip 0 frame
const int TurboFrameSkipDefault = 2; //skip 2 frame
const bool EnableTurboDefault = false;
const bool FullScreenDefault = false;
const bool SignedInDefault = false;
Expand Down
80 changes: 63 additions & 17 deletions Filter/ShaderManager.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "ShaderManager.h"
#include "../TextureLoader.h"
#include "xBR2_PS.h"
#include "xBR5_PS.h"
#include "hq2x_PS.h"

using namespace VBA10;
using namespace Microsoft::WRL;

ShaderManager *ShaderManager::instance = nullptr;

ShaderManager::ShaderManager(ID3D11Device1 *device):device(device)
ShaderManager::ShaderManager(ID3D11Device1 *device, ID3D11DeviceContext1 *context):device(device), context(context)
{
ShaderManager::instance = this;
}
Expand All @@ -20,29 +22,73 @@ ShaderManager *ShaderManager::GetInstance()
void ShaderManager::LoadShader(int selection)
{
HRESULT test;
const BYTE* shaderByteCode;
size_t shaderSize;

//load the Look up table (texture)
LoadTextureFromFile(
this->device.Get(),
L"Filter/hq2x.dds",
this->lutResource.GetAddressOf(),
this->lutSRV.GetAddressOf()
);


//create sampler
//D3D11_SAMPLER_DESC samplerDesc;
//ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));

//samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;

//samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
//samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
//samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;

//samplerDesc.MaxLOD = FLT_MAX;
//samplerDesc.MaxAnisotropy = 1;
//samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;

//if (FAILED(this->device->CreateSamplerState(&samplerDesc, this->sampler.GetAddressOf())))
//{
// int test = 1;
//}

//this->context->PSSetSamplers(1, 1, this->sampler.GetAddressOf());


if (selection == 1)
{
shaderByteCode = XBR2_PS;
shaderSize = sizeof(XBR2_PS);
if (FAILED(test = this->device->CreatePixelShader(
HQ2X_PS,
sizeof(HQ2X_PS),
NULL,
&this->ps)))
{
}



}
else
else if (selection == 2) //2xbr
{
shaderByteCode = XBR5_PS;
shaderSize = sizeof(XBR5_PS);
if (FAILED(test = this->device->CreatePixelShader(
XBR2_PS,
sizeof(XBR2_PS),
NULL,
&this->ps)))
{
}
}


if (FAILED(test = this->device->CreatePixelShader(
shaderByteCode,
shaderSize,
NULL,
&this->ps)))
else if (selection == 3) //5xbr
{
int test = sizeof(*shaderByteCode);
test++;
if (FAILED(test = this->device->CreatePixelShader(
XBR5_PS,
sizeof(XBR5_PS),
NULL,
&this->ps)))
{
}
}




}
8 changes: 7 additions & 1 deletion Filter/ShaderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ namespace VBA10
public:


ShaderManager(ID3D11Device1 *device);
ShaderManager(ID3D11Device1 *device, ID3D11DeviceContext1 *context);
static ShaderManager *GetInstance();

ID3D11PixelShader* GetCurrentShader() {return this->ps.Get();};
ID3D11ShaderResourceView* GetLookUpTable() { return this->lutSRV.Get(); };
void LoadShader(int selection);
private:
Microsoft::WRL::ComPtr<ID3D11Device1> device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext1> context;
Microsoft::WRL::ComPtr<ID3D11PixelShader> ps;
Microsoft::WRL::ComPtr<ID3D11SamplerState> sampler;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> lutSRV;
Microsoft::WRL::ComPtr<ID3D11Resource> lutResource;

static ShaderManager *instance;
};
}
Binary file added Filter/hq2x.dds
Binary file not shown.
22 changes: 22 additions & 0 deletions Filter/hq2x_PS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2014 Jules Blok ( [email protected] )
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/


#define SCALE 2

#include "hqx.inc"
Binary file added Filter/hq3x.dds
Binary file not shown.
Loading

0 comments on commit de888fc

Please sign in to comment.