2009-02-09 21:15:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2009-05-18 11:08:04 +00:00
|
|
|
#include "GSdx.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
#include "GSDevice9.h"
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
GSDevice9::GSDevice9()
|
2009-07-31 23:59:06 +00:00
|
|
|
: m_lost(false)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-06 16:35:06 +00:00
|
|
|
m_rbswapped = true;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
memset(&m_pp, 0, sizeof(m_pp));
|
|
|
|
memset(&m_ddcaps, 0, sizeof(m_ddcaps));
|
|
|
|
memset(&m_d3dcaps, 0, sizeof(m_d3dcaps));
|
2009-05-26 03:40:31 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
memset(&m_state, 0, sizeof(m_state));
|
|
|
|
|
|
|
|
m_state.bf = 0xffffffff;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSDevice9::~GSDevice9()
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs_cb) _aligned_free(m_state.vs_cb);
|
|
|
|
if(m_state.ps_cb) _aligned_free(m_state.ps_cb);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-06-03 12:09:04 +00:00
|
|
|
bool GSDevice9::Create(GSWnd* wnd, bool vsync)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-03 12:09:04 +00:00
|
|
|
if(!__super::Create(wnd, vsync))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
// dd
|
|
|
|
|
|
|
|
CComPtr<IDirectDraw7> dd;
|
|
|
|
|
|
|
|
hr = DirectDrawCreateEx(0, (void**)&dd, IID_IDirectDraw7, 0);
|
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
|
|
|
|
memset(&m_ddcaps, 0, sizeof(m_ddcaps));
|
|
|
|
|
|
|
|
m_ddcaps.dwSize = sizeof(DDCAPS);
|
|
|
|
|
|
|
|
hr = dd->GetCaps(&m_ddcaps, NULL);
|
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
|
|
|
|
dd = NULL;
|
|
|
|
|
|
|
|
// d3d
|
|
|
|
|
2009-07-04 22:54:57 +00:00
|
|
|
m_d3d.Attach(Direct3DCreate9(D3D_SDK_VERSION));
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(!m_d3d) return false;
|
|
|
|
|
|
|
|
hr = m_d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8);
|
2009-07-04 22:54:57 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
|
|
|
|
hr = m_d3d->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_D24S8);
|
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
|
|
|
|
memset(&m_d3dcaps, 0, sizeof(m_d3dcaps));
|
|
|
|
|
|
|
|
m_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_d3dcaps);
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
//
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
if(m_d3dcaps.VertexShaderVersion < (m_d3dcaps.PixelShaderVersion & ~0x10000))
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_d3dcaps.VertexShaderVersion > D3DVS_VERSION(0, 0))
|
|
|
|
{
|
|
|
|
ASSERT(0);
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else vertex shader should be emulated in software (gma950)
|
2009-07-22 03:55:28 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
m_d3dcaps.VertexShaderVersion = m_d3dcaps.PixelShaderVersion & ~0x10000;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
if(m_d3dcaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
|
|
|
|
{
|
|
|
|
SetFeatureLevel(D3D_FEATURE_LEVEL_9_3, false);
|
|
|
|
}
|
|
|
|
else if(m_d3dcaps.PixelShaderVersion >= D3DPS_VERSION(2, 0))
|
|
|
|
{
|
|
|
|
SetFeatureLevel(D3D_FEATURE_LEVEL_9_2, false);
|
|
|
|
}
|
|
|
|
else
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-03 12:09:04 +00:00
|
|
|
string s = format(
|
2009-06-12 19:09:17 +00:00
|
|
|
"Supported pixel shader version is too low!\n\nSupported: %d.%d\nNeeded: 2.0 or higher",
|
|
|
|
D3DSHADER_VERSION_MAJOR(m_d3dcaps.PixelShaderVersion), D3DSHADER_VERSION_MINOR(m_d3dcaps.PixelShaderVersion));
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-03 12:09:04 +00:00
|
|
|
MessageBox(NULL, s.c_str(), "GSdx", MB_OK);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
// msaa
|
|
|
|
|
|
|
|
for(uint32 i = 2; i <= 16; i++)
|
|
|
|
{
|
|
|
|
DWORD quality[2] = {0, 0};
|
|
|
|
|
|
|
|
if(SUCCEEDED(m_d3d->CheckDeviceMultiSampleType(m_d3dcaps.AdapterOrdinal, m_d3dcaps.DeviceType, D3DFMT_A8R8G8B8, TRUE, (D3DMULTISAMPLE_TYPE)i, &quality[0])) && quality[0] > 0
|
|
|
|
&& SUCCEEDED(m_d3d->CheckDeviceMultiSampleType(m_d3dcaps.AdapterOrdinal, m_d3dcaps.DeviceType, D3DFMT_D24S8, TRUE, (D3DMULTISAMPLE_TYPE)i, &quality[1])) && quality[1] > 0)
|
|
|
|
{
|
|
|
|
m_msaa_desc.Count = i;
|
|
|
|
m_msaa_desc.Quality = std::min<DWORD>(quality[0] - 1, quality[1] - 1);
|
|
|
|
|
|
|
|
if(i >= m_msaa) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
if(!Reset(1, 1, theApp.GetConfig("windowed", 1) ? Windowed : Fullscreen))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dev->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0);
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// convert
|
|
|
|
|
|
|
|
static const D3DVERTEXELEMENT9 il_convert[] =
|
|
|
|
{
|
|
|
|
{0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
|
|
|
|
{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
|
|
|
|
D3DDECL_END()
|
|
|
|
};
|
|
|
|
|
2009-06-19 20:24:18 +00:00
|
|
|
CompileShader(IDR_CONVERT_FX, "vs_main", NULL, &m_convert.vs, il_convert, countof(il_convert), &m_convert.il);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < countof(m_convert.ps); i++)
|
|
|
|
{
|
2009-06-19 20:24:18 +00:00
|
|
|
CompileShader(IDR_CONVERT_FX, format("ps_main%d", i), NULL, &m_convert.ps[i]);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_convert.dss.DepthEnable = false;
|
|
|
|
m_convert.dss.StencilEnable = false;
|
|
|
|
|
|
|
|
m_convert.bs.BlendEnable = false;
|
|
|
|
m_convert.bs.RenderTargetWriteMask = D3DCOLORWRITEENABLE_RGBA;
|
|
|
|
|
|
|
|
m_convert.ln.FilterMin[0] = D3DTEXF_LINEAR;
|
|
|
|
m_convert.ln.FilterMag[0] = D3DTEXF_LINEAR;
|
|
|
|
m_convert.ln.FilterMin[1] = D3DTEXF_LINEAR;
|
|
|
|
m_convert.ln.FilterMag[1] = D3DTEXF_LINEAR;
|
|
|
|
m_convert.ln.AddressU = D3DTADDRESS_CLAMP;
|
|
|
|
m_convert.ln.AddressV = D3DTADDRESS_CLAMP;
|
|
|
|
|
|
|
|
m_convert.pt.FilterMin[0] = D3DTEXF_POINT;
|
|
|
|
m_convert.pt.FilterMag[0] = D3DTEXF_POINT;
|
|
|
|
m_convert.pt.FilterMin[1] = D3DTEXF_POINT;
|
|
|
|
m_convert.pt.FilterMag[1] = D3DTEXF_POINT;
|
|
|
|
m_convert.pt.AddressU = D3DTADDRESS_CLAMP;
|
|
|
|
m_convert.pt.AddressV = D3DTADDRESS_CLAMP;
|
|
|
|
|
|
|
|
// merge
|
|
|
|
|
|
|
|
for(int i = 0; i < countof(m_merge.ps); i++)
|
|
|
|
{
|
2009-06-19 20:24:18 +00:00
|
|
|
CompileShader(IDR_MERGE_FX, format("ps_main%d", i), NULL, &m_merge.ps[i]);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_merge.bs.BlendEnable = true;
|
|
|
|
m_merge.bs.BlendOp = D3DBLENDOP_ADD;
|
|
|
|
m_merge.bs.SrcBlend = D3DBLEND_SRCALPHA;
|
|
|
|
m_merge.bs.DestBlend = D3DBLEND_INVSRCALPHA;
|
|
|
|
m_merge.bs.BlendOpAlpha = D3DBLENDOP_ADD;
|
|
|
|
m_merge.bs.SrcBlendAlpha = D3DBLEND_ONE;
|
|
|
|
m_merge.bs.DestBlendAlpha = D3DBLEND_ZERO;
|
|
|
|
m_merge.bs.RenderTargetWriteMask = D3DCOLORWRITEENABLE_RGBA;
|
|
|
|
|
|
|
|
// interlace
|
|
|
|
|
|
|
|
for(int i = 0; i < countof(m_interlace.ps); i++)
|
|
|
|
{
|
2009-06-19 20:24:18 +00:00
|
|
|
CompileShader(IDR_INTERLACE_FX, format("ps_main%d", i), NULL, &m_interlace.ps[i]);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
bool GSDevice9::Reset(int w, int h, int mode)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
if(!__super::Reset(w, h, mode))
|
2009-02-09 21:15:56 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(mode == DontCare)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
mode = m_pp.Windowed ? Windowed : Fullscreen;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(!m_lost)
|
|
|
|
{
|
|
|
|
if(m_swapchain && mode != Fullscreen && m_pp.Windowed)
|
|
|
|
{
|
|
|
|
m_swapchain = NULL;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
m_pp.BackBufferWidth = w;
|
|
|
|
m_pp.BackBufferHeight = h;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
hr = m_dev->CreateAdditionalSwapChain(&m_pp, &m_swapchain);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
|
|
|
|
CComPtr<IDirect3DSurface9> backbuffer;
|
|
|
|
hr = m_swapchain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
|
|
|
|
m_backbuffer = new GSTexture9(backbuffer);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_swapchain = NULL;
|
2009-07-04 15:14:04 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_vb = NULL;
|
|
|
|
m_vb_old = NULL;
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
m_vertices.start = 0;
|
|
|
|
m_vertices.count = 0;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs_cb) _aligned_free(m_state.vs_cb);
|
|
|
|
if(m_state.ps_cb) _aligned_free(m_state.ps_cb);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
memset(&m_state, 0, sizeof(m_state));
|
|
|
|
|
|
|
|
m_state.bf = 0xffffffff;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
memset(&m_pp, 0, sizeof(m_pp));
|
|
|
|
|
|
|
|
m_pp.Windowed = TRUE;
|
2009-06-03 12:09:04 +00:00
|
|
|
m_pp.hDeviceWindow = (HWND)m_wnd->GetHandle();
|
2009-02-09 21:15:56 +00:00
|
|
|
m_pp.SwapEffect = D3DSWAPEFFECT_FLIP;
|
|
|
|
m_pp.BackBufferFormat = D3DFMT_X8R8G8B8;
|
|
|
|
m_pp.BackBufferWidth = 1;
|
|
|
|
m_pp.BackBufferHeight = 1;
|
|
|
|
m_pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
|
|
|
|
|
|
|
|
if(m_vsync)
|
|
|
|
{
|
|
|
|
m_pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2009-04-18 09:51:42 +00:00
|
|
|
// m_pp.Flags |= D3DPRESENTFLAG_VIDEO; // enables tv-out (but I don't think anyone would still use a regular tv...)
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-18 11:08:04 +00:00
|
|
|
int mw = theApp.GetConfig("ModeWidth", 0);
|
|
|
|
int mh = theApp.GetConfig("ModeHeight", 0);
|
|
|
|
int mrr = theApp.GetConfig("ModeRefreshRate", 0);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
if(mode == Fullscreen && mw > 0 && mh > 0 && mrr >= 0)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
m_pp.Windowed = FALSE;
|
|
|
|
m_pp.BackBufferWidth = mw;
|
|
|
|
m_pp.BackBufferHeight = mh;
|
|
|
|
// m_pp.FullScreen_RefreshRateInHz = mrr;
|
|
|
|
|
2009-06-03 12:09:04 +00:00
|
|
|
m_wnd->HideFrame();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_dev)
|
|
|
|
{
|
2009-07-22 19:04:48 +00:00
|
|
|
uint32 flags = m_d3dcaps.VertexProcessingCaps ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
if(flags & D3DCREATE_HARDWARE_VERTEXPROCESSING)
|
|
|
|
{
|
|
|
|
flags |= D3DCREATE_PUREDEVICE;
|
|
|
|
}
|
|
|
|
|
2009-06-03 12:09:04 +00:00
|
|
|
hr = m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, (HWND)m_wnd->GetHandle(), flags, &m_pp, &m_dev);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = m_dev->Reset(&m_pp);
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
if(D3DERR_DEVICELOST == hr)
|
|
|
|
{
|
|
|
|
Sleep(1000);
|
|
|
|
|
|
|
|
hr = m_dev->Reset(&m_pp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_pp.Windowed)
|
|
|
|
{
|
|
|
|
m_pp.BackBufferWidth = 1;
|
|
|
|
m_pp.BackBufferHeight = 1;
|
|
|
|
|
|
|
|
hr = m_dev->CreateAdditionalSwapChain(&m_pp, &m_swapchain);
|
|
|
|
|
|
|
|
if(FAILED(hr)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CComPtr<IDirect3DSurface9> backbuffer;
|
|
|
|
|
|
|
|
if(m_swapchain)
|
|
|
|
{
|
|
|
|
hr = m_swapchain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = m_dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
m_backbuffer = new GSTexture9(backbuffer);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
m_dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
|
|
m_dev->SetRenderState(D3DRS_LIGHTING, FALSE);
|
|
|
|
m_dev->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
|
|
|
|
m_dev->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
bool GSDevice9::IsLost(bool update)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
if(!m_lost || update)
|
|
|
|
{
|
|
|
|
HRESULT hr = m_dev->TestCooperativeLevel();
|
|
|
|
|
|
|
|
m_lost = hr == D3DERR_DEVICELOST || hr == D3DERR_DEVICENOTRESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_lost;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
void GSDevice9::Flip(bool limit)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-03 22:45:28 +00:00
|
|
|
m_dev->EndScene();
|
|
|
|
|
2009-07-04 15:14:04 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(m_swapchain)
|
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
hr = m_swapchain->Present(NULL, NULL, NULL, NULL, 0);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-04 15:14:04 +00:00
|
|
|
hr = m_dev->Present(NULL, NULL, NULL, NULL);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-06-03 22:45:28 +00:00
|
|
|
|
|
|
|
m_dev->BeginScene();
|
2009-07-04 15:14:04 +00:00
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
m_lost = true;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::BeginScene()
|
|
|
|
{
|
2009-06-03 22:45:28 +00:00
|
|
|
// m_dev->BeginScene();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 03:40:31 +00:00
|
|
|
void GSDevice9::DrawPrimitive()
|
|
|
|
{
|
|
|
|
int prims = 0;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
switch(m_state.topology)
|
2009-05-26 03:40:31 +00:00
|
|
|
{
|
|
|
|
case D3DPT_TRIANGLELIST:
|
|
|
|
prims = m_vertices.count / 3;
|
|
|
|
break;
|
|
|
|
case D3DPT_LINELIST:
|
|
|
|
prims = m_vertices.count / 2;
|
|
|
|
break;
|
|
|
|
case D3DPT_POINTLIST:
|
|
|
|
prims = m_vertices.count;
|
|
|
|
break;
|
|
|
|
case D3DPT_TRIANGLESTRIP:
|
|
|
|
case D3DPT_TRIANGLEFAN:
|
|
|
|
prims = m_vertices.count - 2;
|
|
|
|
break;
|
|
|
|
case D3DPT_LINESTRIP:
|
|
|
|
prims = m_vertices.count - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_dev->DrawPrimitive(m_state.topology, m_vertices.start, prims);
|
2009-05-26 03:40:31 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void GSDevice9::EndScene()
|
|
|
|
{
|
2009-06-03 22:45:28 +00:00
|
|
|
// m_dev->EndScene();
|
2009-05-26 03:40:31 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
__super::EndScene();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
void GSDevice9::ClearRenderTarget(GSTexture* t, const GSVector4& c)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
ClearRenderTarget(t, (c * 255 + 0.5f).zyxw().rgba32());
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 06:26:46 +00:00
|
|
|
void GSDevice9::ClearRenderTarget(GSTexture* rt, uint32 c)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
CComPtr<IDirect3DSurface9> surface;
|
|
|
|
m_dev->GetRenderTarget(0, &surface);
|
2009-05-29 06:26:46 +00:00
|
|
|
m_dev->SetRenderTarget(0, *(GSTexture9*)rt);
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->Clear(0, NULL, D3DCLEAR_TARGET, c, 0, 0);
|
|
|
|
m_dev->SetRenderTarget(0, surface);
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
void GSDevice9::ClearDepth(GSTexture* t, float c)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* rt = CreateRenderTarget(t->GetWidth(), t->GetHeight(), t->IsMSAA());
|
2009-05-29 06:26:46 +00:00
|
|
|
|
|
|
|
CComPtr<IDirect3DSurface9> rtsurface;
|
|
|
|
CComPtr<IDirect3DSurface9> dssurface;
|
|
|
|
|
|
|
|
m_dev->GetRenderTarget(0, &rtsurface);
|
|
|
|
m_dev->GetDepthStencilSurface(&dssurface);
|
|
|
|
|
|
|
|
m_dev->SetRenderTarget(0, *(GSTexture9*)rt);
|
2009-05-22 01:22:52 +00:00
|
|
|
m_dev->SetDepthStencilSurface(*(GSTexture9*)t);
|
2009-05-29 06:26:46 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, c, 0);
|
2009-05-29 06:26:46 +00:00
|
|
|
|
|
|
|
m_dev->SetRenderTarget(0, rtsurface);
|
|
|
|
m_dev->SetDepthStencilSurface(dssurface);
|
|
|
|
|
|
|
|
Recycle(rt);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
void GSDevice9::ClearStencil(GSTexture* t, uint8 c)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* rt = CreateRenderTarget(t->GetWidth(), t->GetHeight(), t->IsMSAA());
|
2009-05-29 06:26:46 +00:00
|
|
|
|
|
|
|
CComPtr<IDirect3DSurface9> rtsurface;
|
|
|
|
CComPtr<IDirect3DSurface9> dssurface;
|
|
|
|
|
|
|
|
m_dev->GetRenderTarget(0, &rtsurface);
|
|
|
|
m_dev->GetDepthStencilSurface(&dssurface);
|
|
|
|
|
|
|
|
m_dev->SetRenderTarget(0, *(GSTexture9*)rt);
|
2009-05-22 01:22:52 +00:00
|
|
|
m_dev->SetDepthStencilSurface(*(GSTexture9*)t);
|
2009-05-29 06:26:46 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->Clear(0, NULL, D3DCLEAR_STENCIL, 0, 0, c);
|
2009-05-29 06:26:46 +00:00
|
|
|
|
|
|
|
m_dev->SetRenderTarget(0, rtsurface);
|
|
|
|
m_dev->SetDepthStencilSurface(dssurface);
|
|
|
|
|
|
|
|
Recycle(rt);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* GSDevice9::Create(int type, int w, int h, bool msaa, int format)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
CComPtr<IDirect3DTexture9> texture;
|
|
|
|
CComPtr<IDirect3DSurface9> surface;
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
// TODO: msaa
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case GSTexture::RenderTarget:
|
2009-08-02 23:07:30 +00:00
|
|
|
if(msaa) hr = m_dev->CreateRenderTarget(w, h, (D3DFORMAT)format, (D3DMULTISAMPLE_TYPE)m_msaa_desc.Count, m_msaa_desc.Quality, FALSE, &surface, NULL);
|
|
|
|
else hr = m_dev->CreateTexture(w, h, 1, D3DUSAGE_RENDERTARGET, (D3DFORMAT)format, D3DPOOL_DEFAULT, &texture, NULL);
|
2009-02-09 21:15:56 +00:00
|
|
|
break;
|
|
|
|
case GSTexture::DepthStencil:
|
2009-08-02 23:07:30 +00:00
|
|
|
hr = m_dev->CreateDepthStencilSurface(w, h, (D3DFORMAT)format, (D3DMULTISAMPLE_TYPE)m_msaa_desc.Count, m_msaa_desc.Quality, FALSE, &surface, NULL);
|
2009-02-09 21:15:56 +00:00
|
|
|
break;
|
|
|
|
case GSTexture::Texture:
|
|
|
|
hr = m_dev->CreateTexture(w, h, 1, 0, (D3DFORMAT)format, D3DPOOL_MANAGED, &texture, NULL);
|
|
|
|
break;
|
|
|
|
case GSTexture::Offscreen:
|
|
|
|
hr = m_dev->CreateOffscreenPlainSurface(w, h, (D3DFORMAT)format, D3DPOOL_SYSTEMMEM, &surface, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
GSTexture9* t = NULL;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(surface)
|
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
t = new GSTexture9(surface);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(texture)
|
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
t = new GSTexture9(texture);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(t)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case GSTexture::RenderTarget:
|
|
|
|
ClearRenderTarget(t, 0);
|
|
|
|
break;
|
|
|
|
case GSTexture::DepthStencil:
|
|
|
|
ClearDepth(t, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
return t;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* GSDevice9::CreateRenderTarget(int w, int h, bool msaa, int format)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-08-02 23:07:30 +00:00
|
|
|
return __super::CreateRenderTarget(w, h, msaa, format ? format : D3DFMT_A8R8G8B8);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* GSDevice9::CreateDepthStencil(int w, int h, bool msaa, int format)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-08-02 23:07:30 +00:00
|
|
|
return __super::CreateDepthStencil(w, h, msaa, format ? format : D3DFMT_D24S8);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
GSTexture* GSDevice9::CreateTexture(int w, int h, int format)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
return __super::CreateTexture(w, h, format ? format : D3DFMT_A8R8G8B8);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
GSTexture* GSDevice9::CreateOffscreen(int w, int h, int format)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
return __super::CreateOffscreen(w, h, format ? format : D3DFMT_A8R8G8B8);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
GSTexture* GSDevice9::Resolve(GSTexture* t)
|
|
|
|
{
|
|
|
|
ASSERT(t != NULL && t->IsMSAA());
|
|
|
|
|
|
|
|
if(GSTexture* dst = CreateRenderTarget(t->GetWidth(), t->GetHeight(), false, t->GetFormat()))
|
|
|
|
{
|
|
|
|
dst->SetScale(t->GetScale());
|
|
|
|
|
|
|
|
m_dev->StretchRect(*(GSTexture9*)t, NULL, *(GSTexture9*)dst, NULL, D3DTEXF_POINT);
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
GSTexture* GSDevice9::CopyOffscreen(GSTexture* src, const GSVector4& sr, int w, int h, int format)
|
|
|
|
{
|
|
|
|
GSTexture* dst = NULL;
|
|
|
|
|
|
|
|
if(format == 0)
|
|
|
|
{
|
|
|
|
format = D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(format != D3DFMT_A8R8G8B8)
|
|
|
|
{
|
|
|
|
ASSERT(0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
if(GSTexture* rt = CreateRenderTarget(w, h, false, format))
|
2009-05-22 01:22:52 +00:00
|
|
|
{
|
|
|
|
GSVector4 dr(0, 0, w, h);
|
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
if(GSTexture* src2 = src->IsMSAA() ? Resolve(src) : src)
|
|
|
|
{
|
|
|
|
StretchRect(src2, sr, rt, dr, m_convert.ps[1], NULL, 0);
|
|
|
|
|
|
|
|
if(src2 != src) Recycle(src2);
|
|
|
|
}
|
2009-05-22 01:22:52 +00:00
|
|
|
|
|
|
|
dst = CreateOffscreen(w, h, format);
|
|
|
|
|
|
|
|
if(dst)
|
|
|
|
{
|
|
|
|
m_dev->GetRenderTargetData(*(GSTexture9*)rt, *(GSTexture9*)dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Recycle(rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2009-07-06 16:35:06 +00:00
|
|
|
void GSDevice9::CopyRect(GSTexture* st, GSTexture* dt, const GSVector4i& r)
|
|
|
|
{
|
|
|
|
m_dev->StretchRect(*(GSTexture9*)st, r, *(GSTexture9*)dt, r, D3DTEXF_POINT);
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:23:38 +00:00
|
|
|
void GSDevice9::StretchRect(GSTexture* st, const GSVector4& sr, GSTexture* dt, const GSVector4& dr, int shader, bool linear)
|
2009-05-22 01:22:52 +00:00
|
|
|
{
|
2009-05-22 23:23:38 +00:00
|
|
|
StretchRect(st, sr, dt, dr, m_convert.ps[shader], NULL, 0, linear);
|
2009-05-22 01:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::StretchRect(GSTexture* st, const GSVector4& sr, GSTexture* dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, bool linear)
|
|
|
|
{
|
|
|
|
StretchRect(st, sr, dt, dr, ps, ps_cb, ps_cb_len, &m_convert.bs, linear);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::StretchRect(GSTexture* st, const GSVector4& sr, GSTexture* dt, const GSVector4& dr, IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len, Direct3DBlendState9* bs, bool linear)
|
|
|
|
{
|
|
|
|
BeginScene();
|
|
|
|
|
|
|
|
GSVector2i ds = dt->GetSize();
|
|
|
|
|
|
|
|
// om
|
|
|
|
|
2009-06-17 11:24:42 +00:00
|
|
|
OMSetDepthStencilState(&m_convert.dss);
|
2009-05-22 01:22:52 +00:00
|
|
|
OMSetBlendState(bs, 0);
|
|
|
|
OMSetRenderTargets(dt, NULL);
|
|
|
|
|
|
|
|
// ia
|
|
|
|
|
|
|
|
float left = dr.x * 2 / ds.x - 1.0f;
|
|
|
|
float top = 1.0f - dr.y * 2 / ds.y;
|
|
|
|
float right = dr.z * 2 / ds.x - 1.0f;
|
|
|
|
float bottom = 1.0f - dr.w * 2 / ds.y;
|
|
|
|
|
|
|
|
GSVertexPT1 vertices[] =
|
|
|
|
{
|
|
|
|
{GSVector4(left, top, 0.5f, 1.0f), GSVector2(sr.x, sr.y)},
|
|
|
|
{GSVector4(right, top, 0.5f, 1.0f), GSVector2(sr.z, sr.y)},
|
|
|
|
{GSVector4(left, bottom, 0.5f, 1.0f), GSVector2(sr.x, sr.w)},
|
|
|
|
{GSVector4(right, bottom, 0.5f, 1.0f), GSVector2(sr.z, sr.w)},
|
|
|
|
};
|
|
|
|
|
|
|
|
for(int i = 0; i < countof(vertices); i++)
|
|
|
|
{
|
|
|
|
vertices[i].p.x -= 1.0f / ds.x;
|
|
|
|
vertices[i].p.y += 1.0f / ds.y;
|
|
|
|
}
|
|
|
|
|
2009-05-26 03:40:31 +00:00
|
|
|
IASetVertexBuffer(vertices, sizeof(vertices[0]), countof(vertices));
|
2009-05-22 01:22:52 +00:00
|
|
|
IASetInputLayout(m_convert.il);
|
|
|
|
IASetPrimitiveTopology(D3DPT_TRIANGLESTRIP);
|
|
|
|
|
|
|
|
// vs
|
|
|
|
|
|
|
|
VSSetShader(m_convert.vs, NULL, 0);
|
|
|
|
|
|
|
|
// ps
|
|
|
|
|
|
|
|
PSSetShader(ps, ps_cb, ps_cb_len);
|
|
|
|
PSSetSamplerState(linear ? &m_convert.ln : &m_convert.pt);
|
|
|
|
PSSetShaderResources(st, NULL);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2009-05-26 03:40:31 +00:00
|
|
|
DrawPrimitive();
|
2009-05-22 01:22:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
EndScene();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::DoMerge(GSTexture* st[2], GSVector4* sr, GSVector4* dr, GSTexture* dt, bool slbg, bool mmod, const GSVector4& c)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
ClearRenderTarget(dt, c);
|
|
|
|
|
|
|
|
if(st[1] && !slbg)
|
|
|
|
{
|
|
|
|
StretchRect(st[1], sr[1], dt, dr[1], m_merge.ps[0], NULL, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(st[0])
|
|
|
|
{
|
|
|
|
MergeConstantBuffer cb;
|
|
|
|
|
|
|
|
cb.BGColor = c;
|
|
|
|
|
|
|
|
StretchRect(st[0], sr[0], dt, dr[0], m_merge.ps[mmod ? 1 : 0], (const float*)&cb, 1, &m_merge.bs, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
void GSDevice9::DoInterlace(GSTexture* st, GSTexture* dt, int shader, bool linear, float yoffset)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
GSVector4 s = GSVector4(dt->GetSize());
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
GSVector4 sr(0, 0, 1, 1);
|
2009-05-22 01:22:52 +00:00
|
|
|
GSVector4 dr(0.0f, yoffset, s.x, s.y + yoffset);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
InterlaceConstantBuffer cb;
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
cb.ZrH = GSVector2(0, 1.0f / s.y);
|
|
|
|
cb.hH = (float)s.y / 2;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
StretchRect(st, sr, dt, dr, m_interlace.ps[shader], (const float*)&cb, 1, linear);
|
|
|
|
}
|
|
|
|
|
2009-05-26 03:40:31 +00:00
|
|
|
void GSDevice9::IASetVertexBuffer(const void* vertices, size_t stride, size_t count)
|
|
|
|
{
|
|
|
|
ASSERT(m_vertices.count == 0);
|
|
|
|
|
2009-05-27 05:55:32 +00:00
|
|
|
if(count * stride > m_vertices.limit * m_vertices.stride)
|
2009-05-26 03:40:31 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_vb_old = m_vb;
|
|
|
|
m_vb = NULL;
|
|
|
|
|
2009-05-26 03:40:31 +00:00
|
|
|
m_vertices.start = 0;
|
|
|
|
m_vertices.count = 0;
|
2009-07-04 15:14:04 +00:00
|
|
|
m_vertices.limit = std::max<int>(count * 3 / 2, 10000);
|
2009-05-26 03:40:31 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_vb == NULL)
|
2009-05-26 03:40:31 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
hr = m_dev->CreateVertexBuffer(m_vertices.limit * stride, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &m_vb, NULL);
|
2009-05-26 03:40:31 +00:00
|
|
|
|
|
|
|
if(FAILED(hr)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 flags = D3DLOCK_NOOVERWRITE;
|
|
|
|
|
|
|
|
if(m_vertices.start + count > m_vertices.limit || stride != m_vertices.stride)
|
|
|
|
{
|
|
|
|
m_vertices.start = 0;
|
|
|
|
|
|
|
|
flags = D3DLOCK_DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* v = NULL;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(SUCCEEDED(m_vb->Lock(m_vertices.start * stride, count * stride, &v, flags)))
|
2009-05-26 03:40:31 +00:00
|
|
|
{
|
|
|
|
GSVector4i::storent(v, vertices, count * stride);
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_vb->Unlock();
|
2009-05-26 03:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_vertices.count = count;
|
|
|
|
m_vertices.stride = stride;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
IASetVertexBuffer(m_vb, stride);
|
2009-05-26 03:40:31 +00:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:52:53 +00:00
|
|
|
void GSDevice9::IASetVertexBuffer(IDirect3DVertexBuffer9* vb, size_t stride)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vb != vb || m_state.vb_stride != stride)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.vb = vb;
|
|
|
|
m_state.vb_stride = stride;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetStreamSource(0, vb, 0, stride);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::IASetInputLayout(IDirect3DVertexDeclaration9* layout)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.layout != layout)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.layout = layout;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetVertexDeclaration(layout);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::IASetPrimitiveTopology(D3DPRIMITIVETYPE topology)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.topology = topology;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::VSSetShader(IDirect3DVertexShader9* vs, const float* vs_cb, int vs_cb_len)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs != vs)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.vs = vs;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetVertexShader(vs);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(vs_cb && vs_cb_len > 0)
|
|
|
|
{
|
|
|
|
int size = vs_cb_len * sizeof(float) * 4;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs_cb_len != vs_cb_len || m_state.vs_cb == NULL || memcmp(m_state.vs_cb, vs_cb, size))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs_cb == NULL || m_state.vs_cb_len < vs_cb_len)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.vs_cb) _aligned_free(m_state.vs_cb);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.vs_cb = (float*)_aligned_malloc(size, 16);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.vs_cb_len = vs_cb_len;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
memcpy(m_state.vs_cb, vs_cb, size);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
m_dev->SetVertexShaderConstantF(0, vs_cb, vs_cb_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
void GSDevice9::PSSetShaderResources(GSTexture* sr0, GSTexture* sr1)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
IDirect3DTexture9* srv0 = NULL;
|
|
|
|
IDirect3DTexture9* srv1 = NULL;
|
|
|
|
|
|
|
|
if(sr0) srv0 = *(GSTexture9*)sr0;
|
|
|
|
if(sr1) srv1 = *(GSTexture9*)sr1;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps_srvs[0] != srv0)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps_srvs[0] = srv0;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetTexture(0, srv0);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps_srvs[1] != srv1)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps_srvs[1] = srv1;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetTexture(1, srv1);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::PSSetShader(IDirect3DPixelShader9* ps, const float* ps_cb, int ps_cb_len)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps != ps)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps = ps;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetPixelShader(ps);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ps_cb && ps_cb_len > 0)
|
|
|
|
{
|
|
|
|
int size = ps_cb_len * sizeof(float) * 4;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps_cb_len != ps_cb_len || m_state.ps_cb == NULL || memcmp(m_state.ps_cb, ps_cb, size))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps_cb == NULL || m_state.ps_cb_len < ps_cb_len)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.ps_cb) _aligned_free(m_state.ps_cb);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps_cb = (float*)_aligned_malloc(size, 16);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps_cb_len = ps_cb_len;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
memcpy(m_state.ps_cb, ps_cb, size);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
m_dev->SetPixelShaderConstantF(0, ps_cb, ps_cb_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDevice9::PSSetSamplerState(Direct3DSamplerState9* ss)
|
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(ss && m_state.ps_ss != ss)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.ps_ss = ss;
|
2009-06-17 11:24:42 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetSamplerState(0, D3DSAMP_ADDRESSU, ss->AddressU);
|
|
|
|
m_dev->SetSamplerState(0, D3DSAMP_ADDRESSV, ss->AddressV);
|
2009-07-07 10:57:04 +00:00
|
|
|
m_dev->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
|
|
|
|
m_dev->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetSamplerState(2, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
|
|
|
|
m_dev->SetSamplerState(2, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
|
|
|
|
m_dev->SetSamplerState(3, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
|
|
|
|
m_dev->SetSamplerState(3, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
|
|
|
|
m_dev->SetSamplerState(0, D3DSAMP_MINFILTER, ss->FilterMin[0]);
|
|
|
|
m_dev->SetSamplerState(0, D3DSAMP_MAGFILTER, ss->FilterMag[0]);
|
2009-07-07 10:57:04 +00:00
|
|
|
m_dev->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
m_dev->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
m_dev->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
m_dev->SetSamplerState(3, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
m_dev->SetSamplerState(3, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:42 +00:00
|
|
|
void GSDevice9::OMSetDepthStencilState(Direct3DDepthStencilState9* dss)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.dss != dss)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.dss = dss;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetRenderState(D3DRS_ZENABLE, dss->DepthEnable);
|
|
|
|
m_dev->SetRenderState(D3DRS_ZWRITEENABLE, dss->DepthWriteMask);
|
|
|
|
|
|
|
|
if(dss->DepthEnable)
|
|
|
|
{
|
|
|
|
m_dev->SetRenderState(D3DRS_ZFUNC, dss->DepthFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILENABLE, dss->StencilEnable);
|
|
|
|
|
|
|
|
if(dss->StencilEnable)
|
|
|
|
{
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILMASK, dss->StencilReadMask);
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILWRITEMASK, dss->StencilWriteMask);
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILFUNC, dss->StencilFunc);
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILPASS, dss->StencilPassOp);
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILFAIL, dss->StencilFailOp);
|
|
|
|
m_dev->SetRenderState(D3DRS_STENCILZFAIL, dss->StencilDepthFailOp);
|
2009-06-17 11:24:42 +00:00
|
|
|
m_dev->SetRenderState(D3DRS_STENCILREF, dss->StencilRef);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
void GSDevice9::OMSetBlendState(Direct3DBlendState9* bs, uint32 bf)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.bs != bs || m_state.bf != bf)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.bs = bs;
|
|
|
|
m_state.bf = bf;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetRenderState(D3DRS_ALPHABLENDENABLE, bs->BlendEnable);
|
|
|
|
|
|
|
|
if(bs->BlendEnable)
|
|
|
|
{
|
|
|
|
m_dev->SetRenderState(D3DRS_BLENDOP, bs->BlendOp);
|
|
|
|
m_dev->SetRenderState(D3DRS_SRCBLEND, bs->SrcBlend);
|
|
|
|
m_dev->SetRenderState(D3DRS_DESTBLEND, bs->DestBlend);
|
|
|
|
m_dev->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
|
|
|
|
m_dev->SetRenderState(D3DRS_BLENDOPALPHA, bs->BlendOpAlpha);
|
|
|
|
m_dev->SetRenderState(D3DRS_SRCBLENDALPHA, bs->SrcBlendAlpha);
|
|
|
|
m_dev->SetRenderState(D3DRS_DESTBLENDALPHA, bs->DestBlendAlpha);
|
|
|
|
m_dev->SetRenderState(D3DRS_BLENDFACTOR, bf);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dev->SetRenderState(D3DRS_COLORWRITEENABLE, bs->RenderTargetWriteMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
void GSDevice9::OMSetRenderTargets(GSTexture* rt, GSTexture* ds, const GSVector4i* scissor)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
IDirect3DSurface9* rtv = NULL;
|
|
|
|
IDirect3DSurface9* dsv = NULL;
|
|
|
|
|
|
|
|
if(rt) rtv = *(GSTexture9*)rt;
|
|
|
|
if(ds) dsv = *(GSTexture9*)ds;
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.rtv != rtv)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.rtv = rtv;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetRenderTarget(0, rtv);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(m_state.dsv != dsv)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.dsv = dsv;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
m_dev->SetDepthStencilSurface(dsv);
|
2009-07-22 03:55:28 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-08-02 23:07:30 +00:00
|
|
|
GSVector4i r = scissor ? *scissor : GSVector4i(rt->GetSize()).zwxy();
|
2009-07-22 03:55:28 +00:00
|
|
|
|
2009-07-31 23:59:06 +00:00
|
|
|
if(!m_state.scissor.eq(r))
|
2009-07-22 03:55:28 +00:00
|
|
|
{
|
2009-07-31 23:59:06 +00:00
|
|
|
m_state.scissor = r;
|
2009-07-22 03:55:28 +00:00
|
|
|
|
|
|
|
m_dev->SetScissorRect(r);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
HRESULT GSDevice9::CompileShader(uint32 id, const string& entry, const D3DXMACRO* macro, IDirect3DVertexShader9** vs, const D3DVERTEXELEMENT9* layout, int count, IDirect3DVertexDeclaration9** il)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-19 20:24:18 +00:00
|
|
|
vector<D3DXMACRO> m;
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
PrepareShaderMacro(m, macro);
|
2009-06-19 20:24:18 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
CComPtr<ID3DXBuffer> shader, error;
|
|
|
|
|
2009-07-26 15:30:25 +00:00
|
|
|
hr = D3DXCompileShaderFromResource(theApp.GetModuleHandle(), MAKEINTRESOURCE(id), &m[0], NULL, entry.c_str(), m_shader.vs.c_str(), 0, &shader, &error, NULL);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = m_dev->CreateVertexShader((DWORD*)shader->GetBufferPointer(), vs);
|
|
|
|
}
|
|
|
|
else if(error)
|
|
|
|
{
|
2009-05-26 03:40:31 +00:00
|
|
|
printf("%s\n", (const char*)error->GetBufferPointer());
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(SUCCEEDED(hr));
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = m_dev->CreateVertexDeclaration(layout, il);
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
HRESULT GSDevice9::CompileShader(uint32 id, const string& entry, const D3DXMACRO* macro, IDirect3DPixelShader9** ps)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32 flags = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
if(m_shader.level >= D3D_FEATURE_LEVEL_9_3)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
flags |= D3DXSHADER_AVOID_FLOW_CONTROL;
|
|
|
|
}
|
2009-07-26 15:30:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= D3DXSHADER_SKIPVALIDATION;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-19 20:24:18 +00:00
|
|
|
vector<D3DXMACRO> m;
|
|
|
|
|
2009-07-22 03:55:28 +00:00
|
|
|
PrepareShaderMacro(m, macro);
|
2009-06-19 20:24:18 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
CComPtr<ID3DXBuffer> shader, error;
|
|
|
|
|
2009-07-26 15:30:25 +00:00
|
|
|
hr = D3DXCompileShaderFromResource(theApp.GetModuleHandle(), MAKEINTRESOURCE(id), &m[0], NULL, entry.c_str(), m_shader.ps.c_str(), flags, &shader, &error, NULL);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = m_dev->CreatePixelShader((DWORD*)shader->GetBufferPointer(), ps);
|
|
|
|
}
|
|
|
|
else if(error)
|
|
|
|
{
|
2009-05-26 03:40:31 +00:00
|
|
|
printf("%s\n", (const char*)error->GetBufferPointer());
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(SUCCEEDED(hr));
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|