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-04-19 19:16:26 +00:00
|
|
|
static const uint32 g_vs_cb_index = 20;
|
|
|
|
static const uint32 g_ps_cb_index = 21;
|
2015-04-19 16:22:20 +00:00
|
|
|
static const uint32 g_gs_cb_index = 22;
|
2013-04-19 19:16:26 +00:00
|
|
|
|
2011-12-21 19:08:31 +00:00
|
|
|
void GSDeviceOGL::CreateTextureFX()
|
|
|
|
{
|
2015-05-12 10:36:34 +00:00
|
|
|
GL_PUSH("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-08-17 08:57:52 +00:00
|
|
|
// warning 1 sampler by image unit. So you cannot reuse m_ps_ss...
|
2013-06-26 20:09:07 +00:00
|
|
|
m_palette_ss = CreateSampler(false, false, false);
|
2013-08-02 16:38:12 +00:00
|
|
|
gl_BindSampler(1, m_palette_ss);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-06-09 16:41:58 +00:00
|
|
|
// Pre compile all Geometry & Vertex Shader
|
|
|
|
// It might cost a seconds at startup but it would reduce benchmark pollution
|
2013-08-14 10:18:38 +00:00
|
|
|
m_gs = CompileGS();
|
2013-06-26 20:09:07 +00:00
|
|
|
|
2015-04-19 16:01:42 +00:00
|
|
|
int logz = theApp.GetConfig("logz", 1);
|
2015-05-18 05:24:26 +00:00
|
|
|
// Don't do it in debug build, so it can still be tested
|
|
|
|
#ifndef _DEBUG
|
2015-05-01 13:01:08 +00:00
|
|
|
if (GLLoader::found_GL_ARB_clip_control && logz) {
|
|
|
|
fprintf(stderr, "Your driver supports advance depth. Logz will be disabled\n");
|
|
|
|
logz = 0;
|
|
|
|
} else if (!GLLoader::found_GL_ARB_clip_control && !logz) {
|
|
|
|
fprintf(stderr, "Your driver DOESN'T support advance depth (GL_ARB_clip_control)\n It is higly recommmended to enable logz\n");
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-19 16:01:42 +00:00
|
|
|
for (uint32 key = 0; key < VSSelector::size(); key++) {
|
|
|
|
// wildhack is only useful if both TME and FST are enabled.
|
|
|
|
VSSelector sel(key);
|
|
|
|
if (sel.wildhack && (!sel.tme || !sel.fst))
|
|
|
|
m_vs[key] = 0;
|
|
|
|
else
|
|
|
|
m_vs[key] = CompileVS(sel, logz);
|
|
|
|
}
|
2013-06-26 20:09:07 +00:00
|
|
|
|
2015-04-30 17:37:03 +00:00
|
|
|
// Enable all bits for stencil operations. Technically 1 bit is
|
|
|
|
// enough but buffer is polluted with noise. Clear will be limited
|
|
|
|
// to the mask.
|
|
|
|
glStencilMask(0xFF);
|
2013-06-26 20:09:07 +00:00
|
|
|
for (uint32 key = 0; key < OMDepthStencilSelector::size(); key++)
|
|
|
|
m_om_dss[key] = CreateDepthStencil(OMDepthStencilSelector(key));
|
2013-08-28 08:44:16 +00:00
|
|
|
|
|
|
|
// Help to debug FS in apitrace
|
|
|
|
m_apitrace = CompilePS(PSSelector());
|
2015-05-12 10:36:34 +00:00
|
|
|
|
|
|
|
GL_POP();
|
2013-06-26 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSDepthStencilOGL* GSDeviceOGL::CreateDepthStencil(OMDepthStencilSelector dssel)
|
|
|
|
{
|
|
|
|
GSDepthStencilOGL* dss = new GSDepthStencilOGL();
|
|
|
|
|
|
|
|
if (dssel.date)
|
|
|
|
{
|
|
|
|
dss->EnableStencil();
|
|
|
|
dss->SetStencil(GL_EQUAL, dssel.alpha_stencil ? GL_ZERO : GL_KEEP);
|
2013-06-09 16:41:58 +00:00
|
|
|
}
|
2013-06-26 20:09:07 +00:00
|
|
|
|
|
|
|
if(dssel.ztst != ZTST_ALWAYS || dssel.zwe)
|
|
|
|
{
|
|
|
|
static const GLenum ztst[] =
|
|
|
|
{
|
|
|
|
GL_NEVER,
|
|
|
|
GL_ALWAYS,
|
|
|
|
GL_GEQUAL,
|
|
|
|
GL_GREATER
|
|
|
|
};
|
|
|
|
dss->EnableDepth();
|
|
|
|
dss->SetDepth(ztst[dssel.ztst], dssel.zwe);
|
2013-06-09 16:41:58 +00:00
|
|
|
}
|
2013-06-26 20:09:07 +00:00
|
|
|
|
|
|
|
return dss;
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 13:36:48 +00:00
|
|
|
GSBlendStateOGL* GSDeviceOGL::CreateBlend(OMBlendSelector bsel, float afix)
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
2013-06-26 20:09:07 +00:00
|
|
|
GSBlendStateOGL* bs = new GSBlendStateOGL();
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
if(bsel.abe)
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
2013-06-26 20:09:07 +00:00
|
|
|
int i = ((bsel.a * 3 + bsel.b) * 3 + bsel.c) * 3 + bsel.d;
|
|
|
|
|
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, m_blendMapD3D9[i].src, m_blendMapD3D9[i].dst);
|
|
|
|
|
2015-05-19 22:12:52 +00:00
|
|
|
if (m_blendMapD3D9[i].bogus & A_MAX) {
|
2015-05-23 13:10:04 +00:00
|
|
|
if (!theApp.GetConfig("accurate_blend", 1)) {
|
2015-05-08 17:31:49 +00:00
|
|
|
bs->EnableBlend();
|
|
|
|
if (bsel.a == 0)
|
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, GL_ONE, m_blendMapD3D9[i].dst);
|
|
|
|
else
|
|
|
|
bs->SetRGB(m_blendMapD3D9[i].op, m_blendMapD3D9[i].src, GL_ONE);
|
|
|
|
}
|
2013-06-26 20:09:07 +00:00
|
|
|
|
2015-05-26 13:36:48 +00:00
|
|
|
const string afixstr = format("%f", afix);
|
2013-06-26 20:09:07 +00:00
|
|
|
const char *col[3] = {"Cs", "Cd", "0"};
|
|
|
|
const char *alpha[3] = {"As", "Ad", afixstr.c_str()};
|
|
|
|
fprintf(stderr, "Impossible blend for D3D: (%s - %s) * %s + %s\n", col[bsel.a], col[bsel.b], alpha[bsel.c], col[bsel.d]);
|
2015-05-08 17:31:49 +00:00
|
|
|
} else {
|
|
|
|
bs->EnableBlend();
|
2013-06-26 20:09:07 +00:00
|
|
|
}
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-06-26 20:09:07 +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
|
|
|
}
|
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
2015-05-06 06:44:27 +00:00
|
|
|
void GSDeviceOGL::SetupCB(const VSConstantBuffer* vs_cb, const PSConstantBuffer* ps_cb)
|
2013-06-26 20:09:07 +00:00
|
|
|
{
|
2013-08-10 19:43:59 +00:00
|
|
|
if(m_vs_cb_cache.Update(vs_cb)) {
|
|
|
|
m_vs_cb->upload(vs_cb);
|
|
|
|
}
|
2013-06-26 20:09:07 +00:00
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
if(m_ps_cb_cache.Update(ps_cb)) {
|
|
|
|
m_ps_cb->upload(ps_cb);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
2013-08-10 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSDeviceOGL::SetupVS(VSSelector sel)
|
|
|
|
{
|
2013-10-24 20:54:27 +00:00
|
|
|
if (GLLoader::found_GL_ARB_shader_subroutine) {
|
|
|
|
GLuint sub[1];
|
|
|
|
sub[0] = sel.tme ? 1 + (uint32)sel.fst : 0;
|
|
|
|
m_shader->VS_subroutine(sub);
|
|
|
|
// Handle by subroutine useless now
|
|
|
|
sel.tme = 0;
|
|
|
|
sel.fst = 0;
|
|
|
|
}
|
2013-08-10 19:43:59 +00:00
|
|
|
|
2013-10-24 20:54:27 +00:00
|
|
|
m_shader->VS(m_vs[sel], 1);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 10:18:38 +00:00
|
|
|
void GSDeviceOGL::SetupGS(bool enable)
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
2013-08-14 10:18:38 +00:00
|
|
|
if (enable)
|
|
|
|
m_shader->GS(m_gs);
|
|
|
|
else
|
|
|
|
m_shader->GS(0);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
void GSDeviceOGL::SetupPS(PSSelector sel)
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
2013-08-10 19:43:59 +00:00
|
|
|
if (GLLoader::found_GL_ARB_shader_subroutine) {
|
2013-10-24 20:54:27 +00:00
|
|
|
GLuint tfx = sel.tfx > 3 ? 19 : 11 + (uint32)sel.tfx + (uint32)sel.tcc*4;
|
|
|
|
|
|
|
|
GLuint colclip = 8 + (uint32)sel.colclip;
|
|
|
|
|
|
|
|
GLuint clamp =
|
|
|
|
(sel.wms == 2 && sel.wmt == 2) ? 20 :
|
|
|
|
(sel.wms == 2) ? 21 :
|
|
|
|
(sel.wmt == 2) ? 22 : 23;
|
|
|
|
|
|
|
|
GLuint wrap =
|
|
|
|
(sel.wms == 2 && sel.wmt == 2) ? 24 :
|
|
|
|
(sel.wms == 3 && sel.wmt == 3) ? 25 :
|
|
|
|
(sel.wms == 2 && sel.wmt == 3) ? 26 :
|
|
|
|
(sel.wms == 3 && sel.wmt == 2) ? 27 :
|
|
|
|
(sel.wms == 2) ? 28 :
|
|
|
|
(sel.wmt == 3) ? 29 :
|
|
|
|
(sel.wms == 3) ? 30 :
|
|
|
|
(sel.wmt == 2) ? 31 : 32;
|
|
|
|
|
|
|
|
GLuint sub[5] = {sel.atst, colclip, tfx, clamp, wrap};
|
|
|
|
|
2013-08-10 19:43:59 +00:00
|
|
|
m_shader->PS_subroutine(sub);
|
|
|
|
// Handle by subroutine useless now
|
|
|
|
sel.atst = 0;
|
|
|
|
sel.colclip = 0;
|
2013-10-24 20:54:27 +00:00
|
|
|
sel.tfx = 0;
|
|
|
|
sel.tcc = 0;
|
|
|
|
// sel.wms = 0;
|
|
|
|
// sel.wmt = 0;
|
2013-08-10 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 19:08:31 +00:00
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
GLuint ps;
|
|
|
|
auto i = m_ps.find(sel);
|
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
if (i == m_ps.end()) {
|
|
|
|
ps = CompilePS(sel);
|
2011-12-21 19:08:31 +00:00
|
|
|
m_ps[sel] = ps;
|
|
|
|
} else {
|
|
|
|
ps = i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
2013-10-24 20:54:27 +00:00
|
|
|
m_shader->PS(ps, 3);
|
2013-07-19 19:25:50 +00:00
|
|
|
}
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-08-17 08:57:52 +00:00
|
|
|
void GSDeviceOGL::SetupSampler(PSSamplerSelector ssel)
|
2013-07-19 19:25:50 +00:00
|
|
|
{
|
2013-08-02 16:38:12 +00:00
|
|
|
PSSetSamplerState(m_ps_ss[ssel]);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:57:52 +00:00
|
|
|
GLuint GSDeviceOGL::GetSamplerID(PSSamplerSelector ssel)
|
|
|
|
{
|
|
|
|
return m_ps_ss[ssel];
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GSDeviceOGL::GetPaletteSamplerID()
|
|
|
|
{
|
|
|
|
return m_palette_ss;
|
|
|
|
}
|
|
|
|
|
2015-05-26 13:36:48 +00:00
|
|
|
void GSDeviceOGL::SetupOM(OMDepthStencilSelector dssel, OMBlendSelector bsel, float afix, bool sw_blending)
|
2011-12-21 19:08:31 +00:00
|
|
|
{
|
2013-06-26 20:09:07 +00:00
|
|
|
GSDepthStencilOGL* dss = m_om_dss[dssel];
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2013-06-26 20:09:07 +00:00
|
|
|
OMSetDepthStencilState(dss, 1);
|
2011-12-21 19:08:31 +00:00
|
|
|
|
2015-05-19 22:51:37 +00:00
|
|
|
if (sw_blending) {
|
|
|
|
if (GLState::blend) {
|
|
|
|
GLState::blend = false;
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
// No hardware blending thank
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-21 19:08:31 +00:00
|
|
|
// *************************************************************
|
|
|
|
// Static
|
|
|
|
// *************************************************************
|
|
|
|
auto j = m_om_bs.find(bsel);
|
2013-06-26 20:09:07 +00:00
|
|
|
GSBlendStateOGL* bs;
|
2011-12-21 19:08:31 +00:00
|
|
|
|
|
|
|
if(j == m_om_bs.end())
|
|
|
|
{
|
2013-06-26 20:09:07 +00:00
|
|
|
bs = CreateBlend(bsel, afix);
|
2011-12-21 19:08:31 +00:00
|
|
|
m_om_bs[bsel] = bs;
|
2013-06-26 20:09:07 +00:00
|
|
|
} else {
|
|
|
|
bs = j->second;
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// *************************************************************
|
|
|
|
// Dynamic
|
|
|
|
// *************************************************************
|
2015-05-26 13:36:48 +00:00
|
|
|
OMSetBlendState(bs, afix);
|
2011-12-21 19:08:31 +00:00
|
|
|
}
|