2011-12-21 19:08:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2011 Gregory hainaut
|
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
*
|
|
|
|
* 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
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2011-12-21 19:08:31 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSDeviceOGL.h"
|
|
|
|
#include "GSTables.h"
|
|
|
|
|
2013-05-18 09:17:30 +00:00
|
|
|
#include "res/tfx.h"
|
|
|
|
|
2013-04-19 19:16:26 +00:00
|
|
|
static const uint32 g_vs_cb_index = 20;
|
|
|
|
static const uint32 g_ps_cb_index = 21;
|
|
|
|
|
2011-12-21 19:08:31 +00:00
|
|
|
void GSDeviceOGL::CreateTextureFX()
|
|
|
|
{
|
2013-04-19 19:16:26 +00:00
|
|
|
m_vs_cb = new GSUniformBufferOGL(g_vs_cb_index, sizeof(VSConstantBuffer));
|
|
|
|
m_ps_cb = new GSUniformBufferOGL(g_ps_cb_index, sizeof(PSConstantBuffer));
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_GenSamplers(1, &m_rt_ss);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME, seem to have no difference between sampler !!!
|
|
|
|
m_palette_ss = m_rt_ss;
|
|
|
|
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME which value for GL_TEXTURE_MIN_LOD
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameterf(m_rt_ss, GL_TEXTURE_MAX_LOD, FLT_MAX);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME: seems there is 2 possibility in opengl
|
|
|
|
// DX: sd.ComparisonFunc = D3D11_COMPARISON_NEVER;
|
2013-05-19 09:19:20 +00:00
|
|
|
// gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_MODE, GL_NONE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_FUNC, GL_NEVER);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME: need ogl extension sd.MaxAnisotropy = 16;
|
|
|
|
|
2012-02-11 10:22:02 +00:00
|
|
|
GSInputLayoutOGL vert_format[] =
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
|
|
|
// FIXME
|
2012-02-11 10:22:02 +00:00
|
|
|
{0 , 2 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(0) } ,
|
|
|
|
{1 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(8) } ,
|
|
|
|
{2 , 1 , GL_FLOAT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(12) } ,
|
|
|
|
{3 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(16) } ,
|
|
|
|
{4 , 1 , GL_UNSIGNED_INT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(20) } ,
|
2011-12-21 19:08:31 +00:00
|
|
|
// note: there is a 32 bits pad
|
2012-02-11 10:22:02 +00:00
|
|
|
{5 , 2 , GL_UNSIGNED_SHORT , GL_FALSE , sizeof(GSVertex) , (const GLvoid*)(24) } ,
|
|
|
|
{6 , 4 , GL_UNSIGNED_BYTE , GL_TRUE , sizeof(GSVertex) , (const GLvoid*)(28) } ,
|
2011-12-21 19:08:31 +00:00
|
|
|
};
|
2012-02-11 10:22:02 +00:00
|
|
|
m_vb = new GSVertexBufferStateOGL(sizeof(GSVertex), vert_format, countof(vert_format));
|
2012-08-08 17:49:23 +00:00
|
|
|
|
|
|
|
// Compile some dummy shaders to allow modification inside Apitrace for debug
|
2013-01-17 16:58:48 +00:00
|
|
|
#ifdef _LINUX
|
2012-08-08 17:49:23 +00:00
|
|
|
GLuint dummy;
|
2013-01-17 16:58:48 +00:00
|
|
|
std::string macro = "\n";
|
2013-05-18 09:17:30 +00:00
|
|
|
CompileShaderFromSource("tfx.glsl", "vs_main", GL_VERTEX_SHADER, &dummy, tfx_glsl, macro);
|
2013-05-27 16:53:38 +00:00
|
|
|
if (GLLoader::found_geometry_shader)
|
|
|
|
CompileShaderFromSource("tfx.glsl", "gs_main", GL_GEOMETRY_SHADER, &dummy, tfx_glsl, macro);
|
2013-05-18 09:17:30 +00:00
|
|
|
CompileShaderFromSource("tfx.glsl", "ps_main", GL_FRAGMENT_SHADER, &dummy, tfx_glsl, macro);
|
2013-01-17 16:58:48 +00:00
|
|
|
#endif
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSDeviceOGL::SetupVS(VSSelector sel, const VSConstantBuffer* cb)
|
|
|
|
{
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
auto i = m_vs.find(sel);
|
|
|
|
|
|
|
|
if(i == m_vs.end())
|
|
|
|
{
|
|
|
|
std::string macro = format("#define VS_BPPZ %d\n", sel.bppz)
|
2012-06-12 18:14:01 +00:00
|
|
|
+ format("#define VS_LOGZ %d\n", sel.logz)
|
2011-12-21 19:08:31 +00:00
|
|
|
+ format("#define VS_TME %d\n", sel.tme)
|
|
|
|
+ format("#define VS_FST %d\n", sel.fst)
|
|
|
|
+ format("#define VS_RTCOPY %d\n", sel.rtcopy);
|
|
|
|
|
|
|
|
GLuint vs;
|
2013-05-18 09:17:30 +00:00
|
|
|
CompileShaderFromSource("tfx.glsl", "vs_main", GL_VERTEX_SHADER, &vs, tfx_glsl, macro);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
m_vs[sel] = vs;
|
|
|
|
i = m_vs.find(sel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
2011-12-29 14:24:26 +00:00
|
|
|
if(m_vs_cb_cache.Update(cb)) {
|
2011-12-21 19:08:31 +00:00
|
|
|
SetUniformBuffer(m_vs_cb);
|
2011-12-29 14:24:26 +00:00
|
|
|
m_vs_cb->upload(cb);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VSSetShader(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDeviceOGL::SetupGS(GSSelector sel)
|
|
|
|
{
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
GLuint gs = 0;
|
2012-01-02 20:08:11 +00:00
|
|
|
if(sel.prim > 0 && (sel.iip == 0 || sel.prim == 3))
|
|
|
|
{
|
2011-12-21 19:08:31 +00:00
|
|
|
auto i = m_gs.find(sel);
|
|
|
|
|
|
|
|
if(i == m_gs.end()) {
|
|
|
|
std::string macro = format("#define GS_IIP %d\n", sel.iip)
|
|
|
|
+ format("#define GS_PRIM %d\n", sel.prim);
|
|
|
|
|
2013-05-18 09:17:30 +00:00
|
|
|
CompileShaderFromSource("tfx.glsl", "gs_main", GL_GEOMETRY_SHADER, &gs, tfx_glsl, macro);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
m_gs[sel] = gs;
|
|
|
|
} else {
|
|
|
|
gs = i->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
|
|
|
GSSetShader(gs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDeviceOGL::SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel)
|
|
|
|
{
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
GLuint ps;
|
|
|
|
auto i = m_ps.find(sel);
|
|
|
|
|
|
|
|
if (i == m_ps.end())
|
|
|
|
{
|
|
|
|
std::string macro = format("#define PS_FST %d\n", sel.fst)
|
|
|
|
+ format("#define PS_WMS %d\n", sel.wms)
|
|
|
|
+ format("#define PS_WMT %d\n", sel.wmt)
|
|
|
|
+ format("#define PS_FMT %d\n", sel.fmt)
|
|
|
|
+ format("#define PS_AEM %d\n", sel.aem)
|
|
|
|
+ format("#define PS_TFX %d\n", sel.tfx)
|
|
|
|
+ format("#define PS_TCC %d\n", sel.tcc)
|
|
|
|
+ format("#define PS_ATST %d\n", sel.atst)
|
|
|
|
+ format("#define PS_FOG %d\n", sel.fog)
|
|
|
|
+ format("#define PS_CLR1 %d\n", sel.clr1)
|
|
|
|
+ format("#define PS_FBA %d\n", sel.fba)
|
|
|
|
+ format("#define PS_AOUT %d\n", sel.aout)
|
|
|
|
+ format("#define PS_LTF %d\n", sel.ltf)
|
|
|
|
+ format("#define PS_COLCLIP %d\n", sel.colclip)
|
2012-04-26 21:42:16 +00:00
|
|
|
+ format("#define PS_DATE %d\n", sel.date)
|
2013-02-23 15:35:56 +00:00
|
|
|
+ format("#define PS_SPRITEHACK %d\n", sel.spritehack)
|
|
|
|
+ format("#define PS_TCOFFSETHACK %d\n", sel.tcoffsethack)
|
|
|
|
+ format("#define PS_POINT_SAMPLER %d\n", sel.point_sampler);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-05-18 09:17:30 +00:00
|
|
|
CompileShaderFromSource("tfx.glsl", "ps_main", GL_FRAGMENT_SHADER, &ps, tfx_glsl, macro);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
m_ps[sel] = ps;
|
|
|
|
i = m_ps.find(sel);
|
|
|
|
} else {
|
|
|
|
ps = i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
2011-12-29 14:24:26 +00:00
|
|
|
if(m_ps_cb_cache.Update(cb)) {
|
2011-12-21 19:08:31 +00:00
|
|
|
SetUniformBuffer(m_ps_cb);
|
2011-12-29 14:24:26 +00:00
|
|
|
m_ps_cb->upload(cb);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint ss0, ss1;
|
|
|
|
ss0 = ss1 = 0;
|
|
|
|
if(sel.tfx != 4)
|
|
|
|
{
|
|
|
|
if(!(sel.fmt < 3 && sel.wms < 3 && sel.wmt < 3))
|
|
|
|
{
|
|
|
|
ssel.ltf = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto i = m_ps_ss.find(ssel);
|
|
|
|
|
|
|
|
if(i != m_ps_ss.end())
|
|
|
|
{
|
|
|
|
ss0 = i->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_GenSamplers(1, &ss0);
|
2011-12-21 19:08:31 +00:00
|
|
|
if (ssel.ltf) {
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2011-12-21 19:08:31 +00:00
|
|
|
} else {
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME ensure U -> S, V -> T and W->R
|
|
|
|
if (ssel.tau)
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
2011-12-21 19:08:31 +00:00
|
|
|
else
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
if (ssel.tav)
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
2011-12-21 19:08:31 +00:00
|
|
|
else
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameteri(ss0, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
// FIXME which value for GL_TEXTURE_MIN_LOD
|
2013-05-19 09:19:20 +00:00
|
|
|
gl_SamplerParameterf(m_rt_ss, GL_TEXTURE_MAX_LOD, FLT_MAX);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME: seems there is 2 possibility in opengl
|
|
|
|
// DX: sd.ComparisonFunc = D3D11_COMPARISON_NEVER;
|
2013-05-19 09:19:20 +00:00
|
|
|
// gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_MODE, GL_NONE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
|
|
|
|
gl_SamplerParameteri(m_rt_ss, GL_TEXTURE_COMPARE_FUNC, GL_NEVER);
|
2011-12-21 19:08:31 +00:00
|
|
|
// FIXME: need ogl extension sd.MaxAnisotropy = 16;
|
|
|
|
|
|
|
|
m_ps_ss[ssel] = ss0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sel.fmt >= 3)
|
|
|
|
{
|
|
|
|
ss1 = m_palette_ss;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PSSetSamplerState(ss0, ss1, sel.date ? m_rt_ss : 0);
|
|
|
|
|
|
|
|
PSSetShader(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDeviceOGL::SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, uint8 afix)
|
|
|
|
{
|
|
|
|
auto i = m_om_dss.find(dssel);
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
if (i == m_om_dss.end())
|
|
|
|
{
|
|
|
|
GSDepthStencilOGL* dss = new GSDepthStencilOGL();
|
|
|
|
|
|
|
|
if (dssel.date)
|
|
|
|
{
|
2012-01-03 13:11:40 +00:00
|
|
|
dss->EnableStencil();
|
2012-08-15 10:14:13 +00:00
|
|
|
dss->SetStencil(GL_EQUAL, dssel.alpha_stencil ? GL_ZERO : GL_KEEP);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(dssel.ztst != ZTST_ALWAYS || dssel.zwe)
|
|
|
|
{
|
|
|
|
static const GLenum ztst[] =
|
|
|
|
{
|
|
|
|
GL_NEVER,
|
|
|
|
GL_ALWAYS,
|
|
|
|
GL_GEQUAL,
|
|
|
|
GL_GREATER
|
|
|
|
};
|
2012-01-03 13:11:40 +00:00
|
|
|
dss->EnableDepth();
|
|
|
|
dss->SetDepth(ztst[dssel.ztst], dssel.zwe);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_om_dss[dssel] = dss;
|
|
|
|
i = m_om_dss.find(dssel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
|
|
|
OMSetDepthStencilState(i->second, 1);
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
auto j = m_om_bs.find(bsel);
|
|
|
|
|
|
|
|
if(j == m_om_bs.end())
|
|
|
|
{
|
|
|
|
GSBlendStateOGL* bs = new GSBlendStateOGL();
|
|
|
|
|
|
|
|
if(bsel.abe)
|
|
|
|
{
|
|
|
|
int i = ((bsel.a * 3 + bsel.b) * 3 + bsel.c) * 3 + bsel.d;
|
|
|
|
|
2012-01-03 13:11:40 +00:00
|
|
|
bs->EnableBlend();
|
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, m_blendMapD3D9[i].src, m_blendMapD3D9[i].dst);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
if(m_blendMapD3D9[i].bogus == 1)
|
|
|
|
{
|
|
|
|
if (bsel.a == 0)
|
2012-01-03 13:11:40 +00:00
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, GL_ONE, m_blendMapD3D9[i].dst);
|
2011-12-21 19:08:31 +00:00
|
|
|
else
|
2012-01-03 13:11:40 +00:00
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, m_blendMapD3D9[i].src, GL_ONE);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
const string afixstr = format("%d >> 7", afix);
|
|
|
|
const char *col[3] = {"Cs", "Cd", "0"};
|
|
|
|
const char *alpha[3] = {"As", "Ad", afixstr.c_str()};
|
|
|
|
|
|
|
|
// FIXME, need to investigate OGL capabilities. Maybe for OGL5 ;)
|
|
|
|
fprintf(stderr, "Impossible blend for D3D: (%s - %s) * %s + %s\n", col[bsel.a], col[bsel.b], alpha[bsel.c], col[bsel.d]);
|
|
|
|
}
|
|
|
|
|
2012-01-03 13:11:40 +00:00
|
|
|
// Not very good but I don't wanna write another 81 row table
|
|
|
|
if(bsel.negative) bs->RevertOp();
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 13:11:40 +00:00
|
|
|
bs->SetMask(bsel.wr, bsel.wg, bsel.wb, bsel.wa);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
m_om_bs[bsel] = bs;
|
|
|
|
j = m_om_bs.find(bsel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
|
|
|
OMSetBlendState(j->second, (float)(int)afix / 0x80);
|
|
|
|
}
|