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-09-18 00:16:52 +00:00
|
|
|
#include "GSDevice9.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
#include "resource.h"
|
2010-03-11 18:41:31 +00:00
|
|
|
#include "GSTables.h"
|
2009-07-04 22:54:57 +00:00
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
GSTexture* GSDevice9::CreateMskFix(uint32 size, uint32 msk, uint32 fix)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
GSTexture* t = NULL;
|
|
|
|
|
2009-05-14 16:41:52 +00:00
|
|
|
uint32 hash = (size << 20) | (msk << 10) | fix;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
hash_map<uint32, GSTexture*>::iterator i = m_mskfix.find(hash);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
if(i != m_mskfix.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-27 03:32:33 +00:00
|
|
|
t = i->second;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-18 00:16:52 +00:00
|
|
|
t = CreateTexture(size, 1, D3DFMT_R32F);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
if(t)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-12 19:09:17 +00:00
|
|
|
GSTexture::GSMap m;
|
|
|
|
|
|
|
|
if(t->Map(m))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-22 01:22:52 +00:00
|
|
|
for(uint32 i = 0; i < size; i++)
|
|
|
|
{
|
2009-06-12 19:09:17 +00:00
|
|
|
((float*)m.bits)[i] = (float)((i & msk) | fix) / size;
|
2009-05-22 01:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t->Unmap();
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
m_mskfix[hash] = t;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 01:22:52 +00:00
|
|
|
return t;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
void GSDevice9::SetupIA(const void* vertices, int count, int prim)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-18 00:16:52 +00:00
|
|
|
IASetVertexBuffer(vertices, sizeof(GSVertexHW9), count);
|
|
|
|
IASetInputLayout(m_il);
|
|
|
|
IASetPrimitiveTopology((D3DPRIMITIVETYPE)prim);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
void GSDevice9::SetupVS(VSSelector sel, const VSConstantBuffer* cb)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-18 00:16:52 +00:00
|
|
|
hash_map< uint32, CComPtr<IDirect3DVertexShader9> >::const_iterator i = m_vs.find(sel);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
if(i == m_vs.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
string str[4];
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
str[0] = format("%d", sel.bppz);
|
|
|
|
str[1] = format("%d", sel.tme);
|
|
|
|
str[2] = format("%d", sel.fst);
|
|
|
|
str[3] = format("%d", sel.logz);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
D3DXMACRO macro[] =
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
{"VS_BPPZ", str[0].c_str()},
|
|
|
|
{"VS_TME", str[1].c_str()},
|
|
|
|
{"VS_FST", str[2].c_str()},
|
|
|
|
{"VS_LOGZ", str[3].c_str()},
|
2009-02-09 21:15:56 +00:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const D3DVERTEXELEMENT9 layout[] =
|
|
|
|
{
|
|
|
|
{0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
|
|
|
|
{0, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
|
|
|
|
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1},
|
|
|
|
{0, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
|
|
|
|
D3DDECL_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
CComPtr<IDirect3DVertexDeclaration9> il;
|
2009-06-03 19:22:50 +00:00
|
|
|
CComPtr<IDirect3DVertexShader9> vs;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
CompileShader(IDR_TFX_FX, "vs_main", macro, &vs, layout, countof(layout), &il);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if(m_il == NULL)
|
|
|
|
{
|
|
|
|
m_il = il;
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_vs[sel] = vs;
|
2009-06-03 22:45:28 +00:00
|
|
|
|
|
|
|
i = m_vs.find(sel);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
VSSetShader(i->second, (const float*)cb, sizeof(*cb) / sizeof(GSVector4));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
void GSDevice9::SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
if(cb->WH.z > 0 && cb->WH.w > 0 && (sel.wms == 3 || sel.wmt == 3))
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
GSVector4i size(cb->WH);
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(sel.wms == 3)
|
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
if(GSTexture* t = CreateMskFix(size.z, cb->MskFix.x, cb->MskFix.z))
|
2009-05-22 01:22:52 +00:00
|
|
|
{
|
2009-09-18 00:16:52 +00:00
|
|
|
m_dev->SetTexture(2, *(GSTexture9*)t);
|
2009-05-22 01:22:52 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(sel.wmt == 3)
|
|
|
|
{
|
2009-07-22 03:55:28 +00:00
|
|
|
if(GSTexture* t = CreateMskFix(size.w, cb->MskFix.y, cb->MskFix.w))
|
2009-05-22 01:22:52 +00:00
|
|
|
{
|
2009-09-18 00:16:52 +00:00
|
|
|
m_dev->SetTexture(3, *(GSTexture9*)t);
|
2009-05-22 01:22:52 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
hash_map<uint32, CComPtr<IDirect3DPixelShader9> >::const_iterator i = m_ps.find(sel);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
if(i == m_ps.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-03-12 19:01:05 +00:00
|
|
|
string str[13];
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
str[0] = format("%d", sel.fst);
|
|
|
|
str[1] = format("%d", sel.wms);
|
|
|
|
str[2] = format("%d", sel.wmt);
|
2009-07-06 16:35:06 +00:00
|
|
|
str[3] = format("%d", sel.fmt);
|
2009-05-11 08:18:00 +00:00
|
|
|
str[4] = format("%d", sel.aem);
|
|
|
|
str[5] = format("%d", sel.tfx);
|
|
|
|
str[6] = format("%d", sel.tcc);
|
2009-07-26 15:30:25 +00:00
|
|
|
str[7] = format("%d", sel.atst);
|
|
|
|
str[8] = format("%d", sel.fog);
|
|
|
|
str[9] = format("%d", sel.clr1);
|
|
|
|
str[10] = format("%d", sel.rt);
|
|
|
|
str[11] = format("%d", sel.ltf);
|
2010-03-12 19:01:05 +00:00
|
|
|
str[12] = format("%d", sel.colclip);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
D3DXMACRO macro[] =
|
|
|
|
{
|
2009-06-19 20:43:08 +00:00
|
|
|
{"PS_FST", str[0].c_str()},
|
|
|
|
{"PS_WMS", str[1].c_str()},
|
|
|
|
{"PS_WMT", str[2].c_str()},
|
2009-07-06 16:35:06 +00:00
|
|
|
{"PS_FMT", str[3].c_str()},
|
2009-06-19 20:43:08 +00:00
|
|
|
{"PS_AEM", str[4].c_str()},
|
|
|
|
{"PS_TFX", str[5].c_str()},
|
|
|
|
{"PS_TCC", str[6].c_str()},
|
2009-07-26 15:30:25 +00:00
|
|
|
{"PS_ATST", str[7].c_str()},
|
|
|
|
{"PS_FOG", str[8].c_str()},
|
|
|
|
{"PS_CLR1", str[9].c_str()},
|
|
|
|
{"PS_RT", str[10].c_str()},
|
|
|
|
{"PS_LTF", str[11].c_str()},
|
2010-03-12 19:01:05 +00:00
|
|
|
{"PS_COLCLIP", str[12].c_str()},
|
2009-02-09 21:15:56 +00:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
CComPtr<IDirect3DPixelShader9> ps;
|
2009-06-03 22:45:28 +00:00
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
CompileShader(IDR_TFX_FX, "ps_main", macro, &ps);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_ps[sel] = ps;
|
2009-06-03 22:45:28 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
i = m_ps.find(sel);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
PSSetShader(i->second, (const float*)cb, sizeof(*cb) / sizeof(GSVector4));
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
Direct3DSamplerState9* ss = NULL;
|
|
|
|
|
|
|
|
if(sel.tfx != 4)
|
|
|
|
{
|
2009-07-06 16:35:06 +00:00
|
|
|
if(!(sel.fmt < 3 && sel.wms < 3 && sel.wmt < 3))
|
2009-06-09 10:13:28 +00:00
|
|
|
{
|
|
|
|
ssel.ltf = 0;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
hash_map<uint32, Direct3DSamplerState9* >::const_iterator i = m_ps_ss.find(ssel);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
if(i != m_ps_ss.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-27 03:32:33 +00:00
|
|
|
ss = i->second;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ss = new Direct3DSamplerState9();
|
|
|
|
|
|
|
|
memset(ss, 0, sizeof(*ss));
|
|
|
|
|
2009-06-09 10:13:28 +00:00
|
|
|
ss->FilterMin[0] = ssel.ltf ? D3DTEXF_LINEAR : D3DTEXF_POINT;
|
|
|
|
ss->FilterMag[0] = ssel.ltf ? D3DTEXF_LINEAR : D3DTEXF_POINT;
|
2009-02-09 21:15:56 +00:00
|
|
|
ss->FilterMin[1] = D3DTEXF_POINT;
|
|
|
|
ss->FilterMag[1] = D3DTEXF_POINT;
|
|
|
|
|
|
|
|
ss->AddressU = ssel.tau ? D3DTADDRESS_WRAP : D3DTADDRESS_CLAMP;
|
|
|
|
ss->AddressV = ssel.tav ? D3DTADDRESS_WRAP : D3DTADDRESS_CLAMP;
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_ps_ss[ssel] = ss;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
PSSetSamplerState(ss);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
void GSDevice9::SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, uint8 afix)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
Direct3DDepthStencilState9* dss = NULL;
|
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
hash_map<uint32, Direct3DDepthStencilState9*>::const_iterator i = m_om_dss.find(dssel);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
if(i == m_om_dss.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
dss = new Direct3DDepthStencilState9();
|
|
|
|
|
|
|
|
memset(dss, 0, sizeof(*dss));
|
|
|
|
|
|
|
|
if(dssel.date || dssel.fba)
|
|
|
|
{
|
|
|
|
dss->StencilEnable = true;
|
|
|
|
dss->StencilReadMask = 1;
|
|
|
|
dss->StencilWriteMask = 2;
|
|
|
|
dss->StencilFunc = dssel.date ? D3DCMP_EQUAL : D3DCMP_ALWAYS;
|
|
|
|
dss->StencilPassOp = dssel.fba ? D3DSTENCILOP_REPLACE : D3DSTENCILOP_KEEP;
|
|
|
|
dss->StencilFailOp = dssel.fba ? D3DSTENCILOP_ZERO : D3DSTENCILOP_KEEP;
|
|
|
|
dss->StencilDepthFailOp = dssel.fba ? D3DSTENCILOP_ZERO : D3DSTENCILOP_KEEP;
|
2009-06-17 11:24:42 +00:00
|
|
|
dss->StencilRef = 3;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
if(dssel.ztst != ZTST_ALWAYS || dssel.zwe)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
static const D3DCMPFUNC ztst[] =
|
|
|
|
{
|
|
|
|
D3DCMP_NEVER,
|
|
|
|
D3DCMP_ALWAYS,
|
|
|
|
D3DCMP_GREATEREQUAL,
|
|
|
|
D3DCMP_GREATER
|
|
|
|
};
|
|
|
|
|
2009-07-16 21:36:07 +00:00
|
|
|
dss->DepthEnable = true;
|
2009-02-09 21:15:56 +00:00
|
|
|
dss->DepthWriteMask = dssel.zwe;
|
|
|
|
dss->DepthFunc = ztst[dssel.ztst];
|
|
|
|
}
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_om_dss[dssel] = dss;
|
2009-06-03 22:45:28 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
i = m_om_dss.find(dssel);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
OMSetDepthStencilState(i->second);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
hash_map<uint32, Direct3DBlendState9*>::const_iterator j = m_om_bs.find(bsel);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
if(j == m_om_bs.end())
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-03 19:22:50 +00:00
|
|
|
Direct3DBlendState9* bs = new Direct3DBlendState9();
|
2009-06-03 22:45:28 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
memset(bs, 0, sizeof(*bs));
|
|
|
|
|
|
|
|
bs->BlendEnable = bsel.abe;
|
|
|
|
|
|
|
|
if(bsel.abe)
|
|
|
|
{
|
|
|
|
int i = ((bsel.a * 3 + bsel.b) * 3 + bsel.c) * 3 + bsel.d;
|
|
|
|
|
2010-03-11 18:41:31 +00:00
|
|
|
bs->BlendOp = blendMapD3D9[i].op;
|
|
|
|
bs->SrcBlend = blendMapD3D9[i].src;
|
|
|
|
bs->DestBlend = blendMapD3D9[i].dst;
|
2009-02-09 21:15:56 +00:00
|
|
|
bs->BlendOpAlpha = D3DBLENDOP_ADD;
|
|
|
|
bs->SrcBlendAlpha = D3DBLEND_ONE;
|
|
|
|
bs->DestBlendAlpha = D3DBLEND_ZERO;
|
|
|
|
|
2010-03-12 19:01:05 +00:00
|
|
|
// Not very good but I don't wanna write another 81 row table
|
|
|
|
if (bsel.negative) {
|
|
|
|
if (bs->BlendOp == D3DBLENDOP_ADD)
|
|
|
|
bs->BlendOp = D3DBLENDOP_REVSUBTRACT;
|
|
|
|
else if (bs->BlendOp == D3DBLENDOP_REVSUBTRACT)
|
|
|
|
bs->BlendOp = D3DBLENDOP_ADD;
|
|
|
|
else
|
|
|
|
; // god knows, best just not to mess with it for now
|
|
|
|
}
|
|
|
|
|
2010-03-11 18:41:31 +00:00
|
|
|
if(blendMapD3D9[i].bogus == 1)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
ASSERT(0);
|
|
|
|
|
|
|
|
(bsel.a == 0 ? bs->SrcBlend : bs->DestBlend) = D3DBLEND_ONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-14 11:33:12 +00:00
|
|
|
if(bsel.wr) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_BLUE;
|
2009-02-09 21:15:56 +00:00
|
|
|
if(bsel.wg) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_GREEN;
|
2010-03-14 11:33:12 +00:00
|
|
|
if(bsel.wb) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_RED;
|
2009-02-09 21:15:56 +00:00
|
|
|
if(bsel.wa) bs->RenderTargetWriteMask |= D3DCOLORWRITEENABLE_ALPHA;
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_om_bs[bsel] = bs;
|
2009-06-03 22:45:28 +00:00
|
|
|
|
2009-06-03 19:22:50 +00:00
|
|
|
j = m_om_bs.find(bsel);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 00:16:52 +00:00
|
|
|
OMSetBlendState(j->second, afix >= 0x80 ? 0xffffff : 0x020202 * afix);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|