2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoCommon/PixelShaderGen.h"
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
#include <cmath>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <cstdio>
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
2014-11-13 22:26:49 +00:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2014-12-03 06:29:50 +00:00
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/LightingShaderGen.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2017-10-26 05:44:39 +00:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2015-11-01 21:39:31 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2016-07-21 23:04:57 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/XFMemory.h" // for texture projection mode
|
2012-08-10 16:57:37 +00:00
|
|
|
|
2015-09-01 16:18:13 +00:00
|
|
|
// TODO: Get rid of these
|
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
C_COLORMATRIX = 0, // 0
|
|
|
|
C_COLORS = 0, // 0
|
|
|
|
C_KCOLORS = C_COLORS + 4, // 4
|
|
|
|
C_ALPHA = C_KCOLORS + 4, // 8
|
|
|
|
C_TEXDIMS = C_ALPHA + 1, // 9
|
|
|
|
C_ZBIAS = C_TEXDIMS + 8, // 17
|
|
|
|
C_INDTEXSCALE = C_ZBIAS + 2, // 19
|
|
|
|
C_INDTEXMTX = C_INDTEXSCALE + 2, // 21
|
|
|
|
C_FOGCOLOR = C_INDTEXMTX + 6, // 27
|
|
|
|
C_FOGI = C_FOGCOLOR + 1, // 28
|
|
|
|
C_FOGF = C_FOGI + 1, // 29
|
|
|
|
C_ZSLOPE = C_FOGF + 2, // 31
|
|
|
|
C_EFBSCALE = C_ZSLOPE + 1, // 32
|
|
|
|
C_PENVCONST_END = C_EFBSCALE + 1
|
|
|
|
};
|
2013-01-08 15:40:15 +00:00
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 32> tev_ksel_table_c{
|
2013-08-14 14:25:39 +00:00
|
|
|
"255,255,255", // 1 = 0x00
|
|
|
|
"223,223,223", // 7_8 = 0x01
|
|
|
|
"191,191,191", // 3_4 = 0x02
|
|
|
|
"159,159,159", // 5_8 = 0x03
|
2014-05-14 20:27:00 +00:00
|
|
|
"128,128,128", // 1_2 = 0x04
|
|
|
|
"96,96,96", // 3_8 = 0x05
|
|
|
|
"64,64,64", // 1_4 = 0x06
|
|
|
|
"32,32,32", // 1_8 = 0x07
|
|
|
|
"0,0,0", // INVALID = 0x08
|
|
|
|
"0,0,0", // INVALID = 0x09
|
|
|
|
"0,0,0", // INVALID = 0x0a
|
|
|
|
"0,0,0", // INVALID = 0x0b
|
2013-10-10 18:26:41 +00:00
|
|
|
I_KCOLORS "[0].rgb", // K0 = 0x0C
|
|
|
|
I_KCOLORS "[1].rgb", // K1 = 0x0D
|
|
|
|
I_KCOLORS "[2].rgb", // K2 = 0x0E
|
|
|
|
I_KCOLORS "[3].rgb", // K3 = 0x0F
|
|
|
|
I_KCOLORS "[0].rrr", // K0_R = 0x10
|
|
|
|
I_KCOLORS "[1].rrr", // K1_R = 0x11
|
|
|
|
I_KCOLORS "[2].rrr", // K2_R = 0x12
|
|
|
|
I_KCOLORS "[3].rrr", // K3_R = 0x13
|
|
|
|
I_KCOLORS "[0].ggg", // K0_G = 0x14
|
|
|
|
I_KCOLORS "[1].ggg", // K1_G = 0x15
|
|
|
|
I_KCOLORS "[2].ggg", // K2_G = 0x16
|
|
|
|
I_KCOLORS "[3].ggg", // K3_G = 0x17
|
|
|
|
I_KCOLORS "[0].bbb", // K0_B = 0x18
|
|
|
|
I_KCOLORS "[1].bbb", // K1_B = 0x19
|
|
|
|
I_KCOLORS "[2].bbb", // K2_B = 0x1A
|
|
|
|
I_KCOLORS "[3].bbb", // K3_B = 0x1B
|
|
|
|
I_KCOLORS "[0].aaa", // K0_A = 0x1C
|
|
|
|
I_KCOLORS "[1].aaa", // K1_A = 0x1D
|
|
|
|
I_KCOLORS "[2].aaa", // K2_A = 0x1E
|
|
|
|
I_KCOLORS "[3].aaa", // K3_A = 0x1F
|
2008-12-08 05:30:24 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 32> tev_ksel_table_a{
|
2013-08-14 14:25:39 +00:00
|
|
|
"255", // 1 = 0x00
|
|
|
|
"223", // 7_8 = 0x01
|
|
|
|
"191", // 3_4 = 0x02
|
|
|
|
"159", // 5_8 = 0x03
|
2014-05-14 20:27:00 +00:00
|
|
|
"128", // 1_2 = 0x04
|
|
|
|
"96", // 3_8 = 0x05
|
|
|
|
"64", // 1_4 = 0x06
|
|
|
|
"32", // 1_8 = 0x07
|
|
|
|
"0", // INVALID = 0x08
|
|
|
|
"0", // INVALID = 0x09
|
|
|
|
"0", // INVALID = 0x0a
|
|
|
|
"0", // INVALID = 0x0b
|
|
|
|
"0", // INVALID = 0x0c
|
|
|
|
"0", // INVALID = 0x0d
|
|
|
|
"0", // INVALID = 0x0e
|
|
|
|
"0", // INVALID = 0x0f
|
2013-10-10 18:26:41 +00:00
|
|
|
I_KCOLORS "[0].r", // K0_R = 0x10
|
|
|
|
I_KCOLORS "[1].r", // K1_R = 0x11
|
|
|
|
I_KCOLORS "[2].r", // K2_R = 0x12
|
|
|
|
I_KCOLORS "[3].r", // K3_R = 0x13
|
|
|
|
I_KCOLORS "[0].g", // K0_G = 0x14
|
|
|
|
I_KCOLORS "[1].g", // K1_G = 0x15
|
|
|
|
I_KCOLORS "[2].g", // K2_G = 0x16
|
|
|
|
I_KCOLORS "[3].g", // K3_G = 0x17
|
|
|
|
I_KCOLORS "[0].b", // K0_B = 0x18
|
|
|
|
I_KCOLORS "[1].b", // K1_B = 0x19
|
|
|
|
I_KCOLORS "[2].b", // K2_B = 0x1A
|
|
|
|
I_KCOLORS "[3].b", // K3_B = 0x1B
|
|
|
|
I_KCOLORS "[0].a", // K0_A = 0x1C
|
|
|
|
I_KCOLORS "[1].a", // K1_A = 0x1D
|
|
|
|
I_KCOLORS "[2].a", // K2_A = 0x1E
|
|
|
|
I_KCOLORS "[3].a", // K3_A = 0x1F
|
2008-12-08 05:30:24 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 16> tev_c_input_table{
|
2014-03-06 18:28:29 +00:00
|
|
|
"prev.rgb", // CPREV,
|
|
|
|
"prev.aaa", // APREV,
|
|
|
|
"c0.rgb", // C0,
|
|
|
|
"c0.aaa", // A0,
|
|
|
|
"c1.rgb", // C1,
|
|
|
|
"c1.aaa", // A1,
|
|
|
|
"c2.rgb", // C2,
|
|
|
|
"c2.aaa", // A2,
|
|
|
|
"textemp.rgb", // TEXC,
|
|
|
|
"textemp.aaa", // TEXA,
|
|
|
|
"rastemp.rgb", // RASC,
|
|
|
|
"rastemp.aaa", // RASA,
|
2013-08-14 15:26:15 +00:00
|
|
|
"int3(255,255,255)", // ONE
|
2014-05-14 20:27:00 +00:00
|
|
|
"int3(128,128,128)", // HALF
|
2014-03-06 18:28:29 +00:00
|
|
|
"konsttemp.rgb", // KONST
|
2013-08-14 15:26:15 +00:00
|
|
|
"int3(0,0,0)", // ZERO
|
2008-12-08 05:30:24 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 8> tev_a_input_table{
|
2014-03-24 13:41:56 +00:00
|
|
|
"prev.a", // APREV,
|
|
|
|
"c0.a", // A0,
|
|
|
|
"c1.a", // A1,
|
|
|
|
"c2.a", // A2,
|
|
|
|
"textemp.a", // TEXA,
|
|
|
|
"rastemp.a", // RASA,
|
|
|
|
"konsttemp.a", // KONST, (hw1 had quarter)
|
|
|
|
"0", // ZERO
|
2010-07-06 13:14:51 +00:00
|
|
|
};
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 8> tev_ras_table{
|
2014-12-25 23:56:12 +00:00
|
|
|
"iround(col0 * 255.0)",
|
|
|
|
"iround(col1 * 255.0)",
|
2013-01-11 14:47:38 +00:00
|
|
|
"ERROR13", // 2
|
|
|
|
"ERROR14", // 3
|
|
|
|
"ERROR15", // 4
|
2014-02-28 19:43:43 +00:00
|
|
|
"(int4(1, 1, 1, 1) * alphabump)", // bump alpha (0..248)
|
|
|
|
"(int4(1, 1, 1, 1) * (alphabump | (alphabump >> 5)))", // normalized bump alpha (0..255)
|
2013-08-14 14:16:06 +00:00
|
|
|
"int4(0, 0, 0, 0)", // zero
|
2008-12-08 05:30:24 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 4> tev_c_output_table{
|
|
|
|
"prev.rgb",
|
|
|
|
"c0.rgb",
|
|
|
|
"c1.rgb",
|
|
|
|
"c2.rgb",
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr std::array<const char*, 4> tev_a_output_table{
|
|
|
|
"prev.a",
|
|
|
|
"c0.a",
|
|
|
|
"c1.a",
|
|
|
|
"c2.a",
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 12:41:26 +00:00
|
|
|
// FIXME: Some of the video card's capabilities (BBox support, EarlyZ support, dstAlpha support)
|
2019-05-30 01:21:37 +00:00
|
|
|
// leak into this UID; This is really unhelpful if these UIDs ever move from one machine to
|
2016-01-16 12:41:26 +00:00
|
|
|
// another.
|
2016-12-28 00:37:41 +00:00
|
|
|
PixelShaderUid GetPixelShaderUid()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-01-16 12:41:26 +00:00
|
|
|
PixelShaderUid out;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-05-30 05:05:06 +00:00
|
|
|
pixel_shader_uid_data* const uid_data = out.GetUidData();
|
2016-12-28 00:37:41 +00:00
|
|
|
uid_data->useDstAlpha = bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate &&
|
2021-02-11 02:11:31 +00:00
|
|
|
bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24;
|
2016-12-28 00:37:41 +00:00
|
|
|
|
2014-10-21 06:52:45 +00:00
|
|
|
uid_data->genMode_numindstages = bpmem.genMode.numindstages;
|
|
|
|
uid_data->genMode_numtevstages = bpmem.genMode.numtevstages;
|
|
|
|
uid_data->genMode_numtexgens = bpmem.genMode.numtexgens;
|
2019-12-05 15:58:03 +00:00
|
|
|
uid_data->bounding_box = g_ActiveConfig.bBBoxEnable && BoundingBox::IsEnabled();
|
2016-09-08 00:43:34 +00:00
|
|
|
uid_data->rgba6_format =
|
2021-02-11 02:11:31 +00:00
|
|
|
bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24 && !g_ActiveConfig.bForceTrueColor;
|
2016-09-30 21:11:12 +00:00
|
|
|
uid_data->dither = bpmem.blendmode.dither && uid_data->rgba6_format;
|
2017-09-03 06:32:37 +00:00
|
|
|
uid_data->uint_output = bpmem.blendmode.UseLogicOp();
|
2016-01-16 13:25:16 +00:00
|
|
|
|
|
|
|
u32 numStages = uid_data->genMode_numtevstages + 1;
|
|
|
|
|
2016-01-16 12:41:26 +00:00
|
|
|
const bool forced_early_z =
|
2017-07-28 11:56:55 +00:00
|
|
|
bpmem.UseEarlyDepthTest() &&
|
2021-02-11 02:11:31 +00:00
|
|
|
(g_ActiveConfig.bFastDepthCalc ||
|
|
|
|
bpmem.alpha_test.TestResult() == AlphaTestResult::Undetermined)
|
2016-01-16 12:41:26 +00:00
|
|
|
// We can't allow early_ztest for zfreeze because depth is overridden per-pixel.
|
|
|
|
// This means it's impossible for zcomploc to be emulated on a zfrozen polygon.
|
|
|
|
&& !(bpmem.zmode.testenable && bpmem.genMode.zfreeze);
|
|
|
|
const bool per_pixel_depth =
|
2021-02-11 02:11:31 +00:00
|
|
|
(bpmem.ztex2.op != ZTexOp::Disabled && bpmem.UseLateDepthTest()) ||
|
2016-01-16 12:41:26 +00:00
|
|
|
(!g_ActiveConfig.bFastDepthCalc && bpmem.zmode.testenable && !forced_early_z) ||
|
|
|
|
(bpmem.zmode.testenable && bpmem.genMode.zfreeze);
|
|
|
|
|
|
|
|
uid_data->per_pixel_depth = per_pixel_depth;
|
|
|
|
uid_data->forced_early_z = forced_early_z;
|
|
|
|
|
2017-06-24 08:18:53 +00:00
|
|
|
if (g_ActiveConfig.bEnablePixelLighting)
|
2016-01-16 12:41:26 +00:00
|
|
|
{
|
2016-09-28 21:31:10 +00:00
|
|
|
uid_data->numColorChans = xfmem.numChan.numColorChans;
|
2016-01-16 12:41:26 +00:00
|
|
|
GetLightingShaderUid(uid_data->lighting);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid_data->genMode_numtexgens > 0)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < uid_data->genMode_numtexgens; ++i)
|
|
|
|
{
|
|
|
|
// optional perspective divides
|
2021-02-11 00:01:42 +00:00
|
|
|
uid_data->texMtxInfo_n_projection |= static_cast<u32>(xfmem.texMtxInfo[i].projection.Value())
|
|
|
|
<< i;
|
2016-01-16 12:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// indirect texture map lookup
|
|
|
|
int nIndirectStagesUsed = 0;
|
|
|
|
if (uid_data->genMode_numindstages > 0)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < numStages; ++i)
|
|
|
|
{
|
|
|
|
if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < uid_data->genMode_numindstages)
|
|
|
|
nIndirectStagesUsed |= 1 << bpmem.tevind[i].bt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_data->nIndirectStagesUsed = nIndirectStagesUsed;
|
|
|
|
for (u32 i = 0; i < uid_data->genMode_numindstages; ++i)
|
|
|
|
{
|
|
|
|
if (uid_data->nIndirectStagesUsed & (1 << i))
|
|
|
|
uid_data->SetTevindrefValues(i, bpmem.tevindref.getTexCoord(i), bpmem.tevindref.getTexMap(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int n = 0; n < numStages; n++)
|
|
|
|
{
|
|
|
|
int texcoord = bpmem.tevorders[n / 2].getTexCoord(n & 1);
|
|
|
|
bool bHasTexCoord = (u32)texcoord < bpmem.genMode.numtexgens;
|
|
|
|
// HACK to handle cases where the tex gen is not enabled
|
|
|
|
if (!bHasTexCoord)
|
|
|
|
texcoord = bpmem.genMode.numtexgens;
|
|
|
|
|
|
|
|
uid_data->stagehash[n].hasindstage = bpmem.tevind[n].bt < bpmem.genMode.numindstages;
|
|
|
|
uid_data->stagehash[n].tevorders_texcoord = texcoord;
|
|
|
|
if (uid_data->stagehash[n].hasindstage)
|
|
|
|
uid_data->stagehash[n].tevind = bpmem.tevind[n].hex;
|
|
|
|
|
|
|
|
TevStageCombiner::ColorCombiner& cc = bpmem.combiners[n].colorC;
|
|
|
|
TevStageCombiner::AlphaCombiner& ac = bpmem.combiners[n].alphaC;
|
|
|
|
uid_data->stagehash[n].cc = cc.hex & 0xFFFFFF;
|
|
|
|
uid_data->stagehash[n].ac = ac.hex & 0xFFFFF0; // Storing rswap and tswap later
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.a == TevColorArg::RasAlpha || cc.a == TevColorArg::RasColor ||
|
|
|
|
cc.b == TevColorArg::RasAlpha || cc.b == TevColorArg::RasColor ||
|
|
|
|
cc.c == TevColorArg::RasAlpha || cc.c == TevColorArg::RasColor ||
|
|
|
|
cc.d == TevColorArg::RasAlpha || cc.d == TevColorArg::RasColor ||
|
|
|
|
ac.a == TevAlphaArg::RasAlpha || ac.b == TevAlphaArg::RasAlpha ||
|
|
|
|
ac.c == TevAlphaArg::RasAlpha || ac.d == TevAlphaArg::RasAlpha)
|
2016-01-16 12:41:26 +00:00
|
|
|
{
|
|
|
|
const int i = bpmem.combiners[n].alphaC.rswap;
|
|
|
|
uid_data->stagehash[n].tevksel_swap1a = bpmem.tevksel[i * 2].swap1;
|
|
|
|
uid_data->stagehash[n].tevksel_swap2a = bpmem.tevksel[i * 2].swap2;
|
|
|
|
uid_data->stagehash[n].tevksel_swap1b = bpmem.tevksel[i * 2 + 1].swap1;
|
|
|
|
uid_data->stagehash[n].tevksel_swap2b = bpmem.tevksel[i * 2 + 1].swap2;
|
|
|
|
uid_data->stagehash[n].tevorders_colorchan = bpmem.tevorders[n / 2].getColorChan(n & 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_data->stagehash[n].tevorders_enable = bpmem.tevorders[n / 2].getEnable(n & 1);
|
|
|
|
if (uid_data->stagehash[n].tevorders_enable)
|
|
|
|
{
|
|
|
|
const int i = bpmem.combiners[n].alphaC.tswap;
|
|
|
|
uid_data->stagehash[n].tevksel_swap1c = bpmem.tevksel[i * 2].swap1;
|
|
|
|
uid_data->stagehash[n].tevksel_swap2c = bpmem.tevksel[i * 2].swap2;
|
|
|
|
uid_data->stagehash[n].tevksel_swap1d = bpmem.tevksel[i * 2 + 1].swap1;
|
|
|
|
uid_data->stagehash[n].tevksel_swap2d = bpmem.tevksel[i * 2 + 1].swap2;
|
|
|
|
uid_data->stagehash[n].tevorders_texmap = bpmem.tevorders[n / 2].getTexMap(n & 1);
|
|
|
|
}
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.a == TevColorArg::Konst || cc.b == TevColorArg::Konst || cc.c == TevColorArg::Konst ||
|
|
|
|
cc.d == TevColorArg::Konst || ac.a == TevAlphaArg::Konst || ac.b == TevAlphaArg::Konst ||
|
|
|
|
ac.c == TevAlphaArg::Konst || ac.d == TevAlphaArg::Konst)
|
2016-01-16 12:41:26 +00:00
|
|
|
{
|
|
|
|
uid_data->stagehash[n].tevksel_kc = bpmem.tevksel[n / 2].getKC(n & 1);
|
|
|
|
uid_data->stagehash[n].tevksel_ka = bpmem.tevksel[n / 2].getKA(n & 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MY_STRUCT_OFFSET(str, elem) ((u32)((u64) & (str).elem - (u64) & (str)))
|
2017-06-24 08:18:53 +00:00
|
|
|
uid_data->num_values = (g_ActiveConfig.bEnablePixelLighting) ?
|
2016-01-16 12:41:26 +00:00
|
|
|
sizeof(*uid_data) :
|
|
|
|
MY_STRUCT_OFFSET(*uid_data, stagehash[numStages]);
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
uid_data->Pretest = bpmem.alpha_test.TestResult();
|
2016-01-16 12:41:26 +00:00
|
|
|
uid_data->late_ztest = bpmem.UseLateDepthTest();
|
|
|
|
|
|
|
|
// NOTE: Fragment may not be discarded if alpha test always fails and early depth test is enabled
|
|
|
|
// (in this case we need to write a depth value if depth test passes regardless of the alpha
|
|
|
|
// testing result)
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->Pretest == AlphaTestResult::Undetermined ||
|
|
|
|
(uid_data->Pretest == AlphaTestResult::Fail && uid_data->late_ztest))
|
2016-01-16 12:41:26 +00:00
|
|
|
{
|
|
|
|
uid_data->alpha_test_comp0 = bpmem.alpha_test.comp0;
|
|
|
|
uid_data->alpha_test_comp1 = bpmem.alpha_test.comp1;
|
|
|
|
uid_data->alpha_test_logic = bpmem.alpha_test.logic;
|
|
|
|
|
|
|
|
// ZCOMPLOC HACK:
|
|
|
|
// The only way to emulate alpha test + early-z is to force early-z in the shader.
|
|
|
|
// As this isn't available on all drivers and as we can't emulate this feature otherwise,
|
|
|
|
// we are only able to choose which one we want to respect more.
|
|
|
|
// Tests seem to have proven that writing depth even when the alpha test fails is more
|
|
|
|
// important that a reliable alpha test, so we just force the alpha test to always succeed.
|
|
|
|
// At least this seems to be less buggy.
|
|
|
|
uid_data->alpha_test_use_zcomploc_hack =
|
|
|
|
bpmem.UseEarlyDepthTest() && bpmem.zmode.updateenable &&
|
|
|
|
!g_ActiveConfig.backend_info.bSupportsEarlyZ && !bpmem.genMode.zfreeze;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_data->zfreeze = bpmem.genMode.zfreeze;
|
|
|
|
uid_data->ztex_op = bpmem.ztex2.op;
|
|
|
|
uid_data->early_ztest = bpmem.UseEarlyDepthTest();
|
2021-02-11 02:11:31 +00:00
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
uid_data->fog_fsel = bpmem.fog.c_proj_fsel.fsel;
|
|
|
|
uid_data->fog_proj = bpmem.fog.c_proj_fsel.proj;
|
|
|
|
uid_data->fog_RangeBaseEnabled = bpmem.fogRange.Base.Enabled;
|
2016-01-16 12:41:26 +00:00
|
|
|
|
2017-10-26 05:44:39 +00:00
|
|
|
BlendingState state = {};
|
|
|
|
state.Generate(bpmem);
|
|
|
|
|
|
|
|
if (state.usedualsrc && state.dstalpha && g_ActiveConfig.backend_info.bSupportsFramebufferFetch &&
|
|
|
|
!g_ActiveConfig.backend_info.bSupportsDualSourceBlend)
|
|
|
|
{
|
|
|
|
uid_data->blend_enable = state.blendenable;
|
|
|
|
uid_data->blend_src_factor = state.srcfactor;
|
|
|
|
uid_data->blend_src_factor_alpha = state.srcfactoralpha;
|
|
|
|
uid_data->blend_dst_factor = state.dstfactor;
|
|
|
|
uid_data->blend_dst_factor_alpha = state.dstfactoralpha;
|
|
|
|
uid_data->blend_subtract = state.subtract;
|
|
|
|
uid_data->blend_subtract_alpha = state.subtractAlpha;
|
|
|
|
}
|
|
|
|
|
2016-01-16 12:41:26 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
void ClearUnusedPixelShaderUidBits(APIType api_type, const ShaderHostConfig& host_config,
|
2018-05-25 14:09:10 +00:00
|
|
|
PixelShaderUid* uid)
|
2017-09-05 13:33:15 +00:00
|
|
|
{
|
2019-05-30 05:05:06 +00:00
|
|
|
pixel_shader_uid_data* const uid_data = uid->GetUidData();
|
2017-09-05 13:33:15 +00:00
|
|
|
|
|
|
|
// OpenGL and Vulkan convert implicitly normalized color outputs to their uint representation.
|
2018-05-25 14:09:29 +00:00
|
|
|
// Therefore, it is not necessary to use a uint output on these backends. We also disable the
|
|
|
|
// uint output when logic op is not supported (i.e. driver/device does not support D3D11.1).
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type != APIType::D3D || !host_config.backend_logic_op)
|
2017-09-05 13:33:15 +00:00
|
|
|
uid_data->uint_output = 0;
|
2019-04-20 02:51:04 +00:00
|
|
|
|
|
|
|
// If bounding box is enabled when a UID cache is created, then later disabled, we shouldn't
|
|
|
|
// emit the bounding box portion of the shader.
|
2019-05-09 09:30:17 +00:00
|
|
|
uid_data->bounding_box &= host_config.bounding_box & host_config.backend_bbox;
|
2017-09-05 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 16:57:16 +00:00
|
|
|
void WritePixelShaderCommonHeader(ShaderCode& out, APIType api_type,
|
2019-01-23 08:11:17 +00:00
|
|
|
const ShaderHostConfig& host_config, bool bounding_box)
|
2016-01-16 12:41:26 +00:00
|
|
|
{
|
2013-10-08 01:04:41 +00:00
|
|
|
// dot product for integer vectors
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("int idot(int3 x, int3 y)\n"
|
|
|
|
"{{\n"
|
|
|
|
"\tint3 tmp = x * y;\n"
|
|
|
|
"\treturn tmp.x + tmp.y + tmp.z;\n"
|
|
|
|
"}}\n");
|
|
|
|
|
|
|
|
out.Write("int idot(int4 x, int4 y)\n"
|
|
|
|
"{{\n"
|
|
|
|
"\tint4 tmp = x * y;\n"
|
|
|
|
"\treturn tmp.x + tmp.y + tmp.z + tmp.w;\n"
|
|
|
|
"}}\n\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-17 16:11:27 +00:00
|
|
|
// rounding + casting to integer at once in a single function
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("int iround(float x) {{ return int (round(x)); }}\n"
|
|
|
|
"int2 iround(float2 x) {{ return int2(round(x)); }}\n"
|
|
|
|
"int3 iround(float3 x) {{ return int3(round(x)); }}\n"
|
|
|
|
"int4 iround(float4 x) {{ return int4(round(x)); }}\n\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
|
2010-08-29 19:34:54 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("SAMPLER_BINDING(0) uniform sampler2DArray samp[8];\n");
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2013-09-22 16:07:21 +00:00
|
|
|
else // D3D
|
2010-09-23 02:17:48 +00:00
|
|
|
{
|
2011-12-08 07:51:08 +00:00
|
|
|
// Declare samplers
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("SamplerState samp[8] : register(s0);\n"
|
|
|
|
"\n"
|
|
|
|
"Texture2DArray Tex[8] : register(t0);\n");
|
2010-09-23 02:17:48 +00:00
|
|
|
}
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("UBO_BINDING(std140, 1) uniform PSBlock {{\n");
|
2014-03-17 09:19:54 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("cbuffer PSBlock : register(b0) {{\n");
|
|
|
|
|
|
|
|
out.Write("\tint4 " I_COLORS "[4];\n"
|
|
|
|
"\tint4 " I_KCOLORS "[4];\n"
|
|
|
|
"\tint4 " I_ALPHA ";\n"
|
|
|
|
"\tfloat4 " I_TEXDIMS "[8];\n"
|
|
|
|
"\tint4 " I_ZBIAS "[2];\n"
|
|
|
|
"\tint4 " I_INDTEXSCALE "[2];\n"
|
|
|
|
"\tint4 " I_INDTEXMTX "[6];\n"
|
|
|
|
"\tint4 " I_FOGCOLOR ";\n"
|
|
|
|
"\tint4 " I_FOGI ";\n"
|
|
|
|
"\tfloat4 " I_FOGF ";\n"
|
|
|
|
"\tfloat4 " I_FOGRANGE "[3];\n"
|
|
|
|
"\tfloat4 " I_ZSLOPE ";\n"
|
|
|
|
"\tfloat2 " I_EFBSCALE ";\n"
|
|
|
|
"\tuint bpmem_genmode;\n"
|
|
|
|
"\tuint bpmem_alphaTest;\n"
|
|
|
|
"\tuint bpmem_fogParam3;\n"
|
|
|
|
"\tuint bpmem_fogRangeBase;\n"
|
|
|
|
"\tuint bpmem_dstalpha;\n"
|
|
|
|
"\tuint bpmem_ztex_op;\n"
|
|
|
|
"\tbool bpmem_late_ztest;\n"
|
|
|
|
"\tbool bpmem_rgba6_format;\n"
|
|
|
|
"\tbool bpmem_dither;\n"
|
|
|
|
"\tbool bpmem_bounding_box;\n"
|
|
|
|
"\tuint4 bpmem_pack1[16];\n" // .xy - combiners, .z - tevind
|
|
|
|
"\tuint4 bpmem_pack2[8];\n" // .x - tevorder, .y - tevksel
|
|
|
|
"\tint4 konstLookup[32];\n"
|
|
|
|
"\tbool blend_enable;\n"
|
|
|
|
"\tuint blend_src_factor;\n"
|
|
|
|
"\tuint blend_src_factor_alpha;\n"
|
|
|
|
"\tuint blend_dst_factor;\n"
|
|
|
|
"\tuint blend_dst_factor_alpha;\n"
|
|
|
|
"\tbool blend_subtract;\n"
|
|
|
|
"\tbool blend_subtract_alpha;\n"
|
|
|
|
"}};\n\n");
|
|
|
|
out.Write("#define bpmem_combiners(i) (bpmem_pack1[(i)].xy)\n"
|
|
|
|
"#define bpmem_tevind(i) (bpmem_pack1[(i)].z)\n"
|
|
|
|
"#define bpmem_iref(i) (bpmem_pack1[(i)].w)\n"
|
|
|
|
"#define bpmem_tevorder(i) (bpmem_pack2[(i)].x)\n"
|
|
|
|
"#define bpmem_tevksel(i) (bpmem_pack2[(i)].y)\n\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-23 08:11:17 +00:00
|
|
|
if (host_config.per_pixel_lighting)
|
2017-07-27 10:52:20 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("{}", s_lighting_struct);
|
2017-07-27 10:52:20 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("UBO_BINDING(std140, 2) uniform VSBlock {{\n");
|
2017-07-27 10:52:20 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("cbuffer VSBlock : register(b1) {{\n");
|
2017-07-27 10:52:20 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("{}", s_shader_uniforms);
|
|
|
|
out.Write("}};\n");
|
2017-07-27 10:52:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:15:07 +00:00
|
|
|
if (bounding_box)
|
2014-11-13 22:26:49 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(R"(
|
2019-03-20 12:14:38 +00:00
|
|
|
#ifdef API_D3D
|
|
|
|
globallycoherent RWBuffer<int> bbox_data : register(u2);
|
|
|
|
#define atomicMin InterlockedMin
|
|
|
|
#define atomicMax InterlockedMax
|
|
|
|
#define bbox_left bbox_data[0]
|
|
|
|
#define bbox_right bbox_data[1]
|
|
|
|
#define bbox_top bbox_data[2]
|
|
|
|
#define bbox_bottom bbox_data[3]
|
|
|
|
#else
|
2020-10-20 18:42:54 +00:00
|
|
|
SSBO_BINDING(0) buffer BBox {{
|
2019-03-20 12:14:38 +00:00
|
|
|
int bbox_left, bbox_right, bbox_top, bbox_bottom;
|
2020-10-20 18:42:54 +00:00
|
|
|
}};
|
2019-03-20 12:14:38 +00:00
|
|
|
#endif
|
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
void UpdateBoundingBoxBuffer(int2 min_pos, int2 max_pos) {{
|
2019-05-09 09:30:17 +00:00
|
|
|
if (bbox_left > min_pos.x)
|
|
|
|
atomicMin(bbox_left, min_pos.x);
|
|
|
|
if (bbox_right < max_pos.x)
|
|
|
|
atomicMax(bbox_right, max_pos.x);
|
|
|
|
if (bbox_top > min_pos.y)
|
|
|
|
atomicMin(bbox_top, min_pos.y);
|
|
|
|
if (bbox_bottom < max_pos.y)
|
|
|
|
atomicMax(bbox_bottom, max_pos.y);
|
2020-10-20 18:42:54 +00:00
|
|
|
}}
|
2019-03-22 10:39:54 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
void UpdateBoundingBox(float2 rawpos) {{
|
2019-03-20 12:14:38 +00:00
|
|
|
// The pixel center in the GameCube GPU is 7/12, not 0.5 (see VertexShaderGen.cpp)
|
|
|
|
// Adjust for this by unapplying the offset we added in the vertex shader.
|
|
|
|
const float PIXEL_CENTER_OFFSET = 7.0 / 12.0 - 0.5;
|
|
|
|
float2 offset = float2(PIXEL_CENTER_OFFSET, -PIXEL_CENTER_OFFSET);
|
|
|
|
|
|
|
|
#ifdef API_OPENGL
|
|
|
|
// OpenGL lower-left origin means that Y goes in the opposite direction.
|
|
|
|
offset.y = -offset.y;
|
|
|
|
#endif
|
|
|
|
|
2019-04-03 08:58:59 +00:00
|
|
|
// The rightmost shaded pixel is not included in the right bounding box register,
|
|
|
|
// such that width = right - left + 1. This has been verified on hardware.
|
|
|
|
int2 pos = iround(rawpos * cefbscale + offset);
|
2019-03-22 10:39:54 +00:00
|
|
|
|
|
|
|
#ifdef SUPPORTS_SUBGROUP_REDUCTION
|
2020-10-20 18:42:54 +00:00
|
|
|
if (CAN_USE_SUBGROUP_REDUCTION) {{
|
2019-04-03 08:58:59 +00:00
|
|
|
int2 min_pos = IS_HELPER_INVOCATION ? int2(2147483647, 2147483647) : pos;
|
|
|
|
int2 max_pos = IS_HELPER_INVOCATION ? int2(-2147483648, -2147483648) : pos;
|
2019-03-22 10:39:54 +00:00
|
|
|
SUBGROUP_MIN(min_pos);
|
|
|
|
SUBGROUP_MAX(max_pos);
|
|
|
|
if (IS_FIRST_ACTIVE_INVOCATION)
|
|
|
|
UpdateBoundingBoxBuffer(min_pos, max_pos);
|
2020-10-20 18:42:54 +00:00
|
|
|
}} else {{
|
2019-04-03 08:58:59 +00:00
|
|
|
UpdateBoundingBoxBuffer(pos, pos);
|
2020-10-20 18:42:54 +00:00
|
|
|
}}
|
2019-03-22 10:39:54 +00:00
|
|
|
#else
|
2019-04-03 08:58:59 +00:00
|
|
|
UpdateBoundingBoxBuffer(pos, pos);
|
2019-03-22 10:39:54 +00:00
|
|
|
#endif
|
2020-10-20 18:42:54 +00:00
|
|
|
}}
|
2019-03-22 10:39:54 +00:00
|
|
|
|
2019-03-20 12:14:38 +00:00
|
|
|
)");
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
2017-07-20 04:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void WriteStage(ShaderCode& out, const pixel_shader_uid_data* uid_data, int n,
|
2020-10-20 18:42:54 +00:00
|
|
|
APIType api_type, bool stereo);
|
2021-02-11 02:11:31 +00:00
|
|
|
static void WriteTevRegular(ShaderCode& out, std::string_view components, TevBias bias, TevOp op,
|
|
|
|
bool clamp, TevScale scale, bool alpha);
|
2020-10-20 18:42:54 +00:00
|
|
|
static void SampleTexture(ShaderCode& out, std::string_view texcoords, std::string_view texswap,
|
|
|
|
int texmap, bool stereo, APIType api_type);
|
|
|
|
static void WriteAlphaTest(ShaderCode& out, const pixel_shader_uid_data* uid_data, APIType api_type,
|
2017-07-20 04:15:07 +00:00
|
|
|
bool per_pixel_depth, bool use_dual_source);
|
|
|
|
static void WriteFog(ShaderCode& out, const pixel_shader_uid_data* uid_data);
|
2017-09-03 06:32:37 +00:00
|
|
|
static void WriteColor(ShaderCode& out, APIType api_type, const pixel_shader_uid_data* uid_data,
|
2017-07-20 04:15:07 +00:00
|
|
|
bool use_dual_source);
|
2017-10-26 05:44:39 +00:00
|
|
|
static void WriteBlend(ShaderCode& out, const pixel_shader_uid_data* uid_data);
|
2017-07-20 04:15:07 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& host_config,
|
2017-07-20 04:15:07 +00:00
|
|
|
const pixel_shader_uid_data* uid_data)
|
|
|
|
{
|
|
|
|
ShaderCode out;
|
|
|
|
|
|
|
|
const bool per_pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
|
|
|
|
const bool msaa = host_config.msaa;
|
|
|
|
const bool ssaa = host_config.ssaa;
|
|
|
|
const bool stereo = host_config.stereo;
|
|
|
|
const u32 numStages = uid_data->genMode_numtevstages + 1;
|
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("// Pixel Shader for TEV stages\n");
|
|
|
|
out.Write("// {} TEV stages, {} texgens, {} IND stages\n", numStages,
|
|
|
|
uid_data->genMode_numtexgens, uid_data->genMode_numindstages);
|
2017-07-20 04:15:07 +00:00
|
|
|
|
|
|
|
// Stuff that is shared between ubershaders and pixelgen.
|
2021-04-17 16:57:16 +00:00
|
|
|
WritePixelShaderCommonHeader(out, api_type, host_config, uid_data->bounding_box);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-28 11:56:55 +00:00
|
|
|
if (uid_data->forced_early_z && g_ActiveConfig.backend_info.bSupportsEarlyZ)
|
2014-03-17 08:02:45 +00:00
|
|
|
{
|
|
|
|
// Zcomploc (aka early_ztest) is a way to control whether depth test is done before
|
|
|
|
// or after texturing and alpha test. PC graphics APIs used to provide no way to emulate
|
|
|
|
// this feature properly until 2012: Depth tests were always done after alpha testing.
|
|
|
|
// Most importantly, it was not possible to write to the depth buffer without also writing
|
|
|
|
// a color value (unless color writing was disabled altogether).
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-10-17 11:43:22 +00:00
|
|
|
// OpenGL 4.2 actually provides two extensions which can force an early z test:
|
|
|
|
// * ARB_image_load_store has 'layout(early_fragment_tests)' which forces the driver to do z
|
|
|
|
// and stencil tests early.
|
|
|
|
// * ARB_conservative_depth has 'layout(depth_unchanged) which signals to the driver that it
|
|
|
|
// can make optimisations
|
|
|
|
// which assume the pixel shader won't update the depth buffer.
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-11-02 20:17:43 +00:00
|
|
|
// early_fragment_tests is the best option, as it requires the driver to do early-z and defines
|
|
|
|
// early-z exactly as
|
2015-10-17 11:43:22 +00:00
|
|
|
// we expect, with discard causing the shader to exit with only the depth buffer updated.
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-11-02 20:17:43 +00:00
|
|
|
// Conservative depth's 'depth_unchanged' only hints to the driver that an early-z optimisation
|
|
|
|
// can be made and
|
2015-10-17 11:43:22 +00:00
|
|
|
// doesn't define what will happen if we discard the fragment. But the way modern graphics
|
|
|
|
// hardware is implemented
|
2016-01-14 05:51:37 +00:00
|
|
|
// means it is not unreasonable to expect the same behaviour as early_fragment_tests.
|
2015-11-02 20:17:43 +00:00
|
|
|
// We can also assume that if a driver has gone out of its way to support conservative depth and
|
|
|
|
// not image_load_store
|
2015-10-17 11:43:22 +00:00
|
|
|
// as required by OpenGL 4.2 that it will be doing the optimisation.
|
|
|
|
// If the driver doesn't actually do an early z optimisation, ZCompLoc will be broken and depth
|
|
|
|
// will only be written
|
|
|
|
// if the alpha test passes.
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-10-17 11:43:22 +00:00
|
|
|
// We support Conservative as a fallback, because many drivers based on Mesa haven't implemented
|
|
|
|
// all of the
|
|
|
|
// ARB_image_load_store extension yet.
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-17 08:02:45 +00:00
|
|
|
// D3D11 also has a way to force the driver to enable early-z, so we're fine here.
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
|
2014-03-17 08:02:45 +00:00
|
|
|
{
|
2015-10-17 11:43:22 +00:00
|
|
|
// This is a #define which signals whatever early-z method the driver supports.
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FORCE_EARLY_Z; \n");
|
2014-03-17 08:02:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("[earlydepthstencil]\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-03-17 08:02:45 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-29 22:34:10 +00:00
|
|
|
// Only use dual-source blending when required on drivers that don't support it very well.
|
|
|
|
const bool use_dual_source =
|
2017-07-20 07:10:02 +00:00
|
|
|
host_config.backend_dual_source_blend &&
|
2016-10-29 22:34:10 +00:00
|
|
|
(!DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DUAL_SOURCE_BLENDING) ||
|
2016-12-28 00:37:41 +00:00
|
|
|
uid_data->useDstAlpha);
|
2017-10-26 05:44:39 +00:00
|
|
|
const bool use_shader_blend =
|
|
|
|
!use_dual_source && (uid_data->useDstAlpha && host_config.backend_shader_framebuffer_fetch);
|
2016-10-29 22:34:10 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
|
2010-10-21 05:22:18 +00:00
|
|
|
{
|
2016-10-29 22:34:10 +00:00
|
|
|
if (use_dual_source)
|
2016-08-12 14:40:18 +00:00
|
|
|
{
|
2018-09-01 03:32:56 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_FRAGMENT_SHADER_INDEX_DECORATION))
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n"
|
|
|
|
"FRAGMENT_OUTPUT_LOCATION(1) out vec4 ocol1;\n");
|
2018-09-01 03:32:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FRAGMENT_OUTPUT_LOCATION_INDEXED(0, 0) out vec4 ocol0;\n"
|
|
|
|
"FRAGMENT_OUTPUT_LOCATION_INDEXED(0, 1) out vec4 ocol1;\n");
|
2018-09-01 03:32:56 +00:00
|
|
|
}
|
2016-08-12 14:40:18 +00:00
|
|
|
}
|
2017-10-26 05:44:39 +00:00
|
|
|
else if (use_shader_blend)
|
|
|
|
{
|
2017-11-10 01:39:09 +00:00
|
|
|
// QComm's Adreno driver doesn't seem to like using the framebuffer_fetch value as an
|
|
|
|
// intermediate value with multiple reads & modifications, so pull out the "real" output value
|
|
|
|
// and use a temporary for calculations, then set the output value once at the end of the
|
|
|
|
// shader
|
2018-09-01 03:32:56 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_FRAGMENT_SHADER_INDEX_DECORATION))
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FRAGMENT_OUTPUT_LOCATION(0) FRAGMENT_INOUT vec4 real_ocol0;\n");
|
2018-09-01 03:32:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FRAGMENT_OUTPUT_LOCATION_INDEXED(0, 0) FRAGMENT_INOUT vec4 real_ocol0;\n");
|
2018-09-01 03:32:56 +00:00
|
|
|
}
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|
2016-08-12 14:40:18 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n");
|
2016-08-12 14:40:18 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->per_pixel_depth)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("#define depth gl_FragDepth\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-23 08:12:13 +00:00
|
|
|
if (host_config.backend_geometry_shaders)
|
2014-12-17 08:18:40 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("VARYING_LOCATION(0) in VertexData {{\n");
|
2020-10-20 18:42:54 +00:00
|
|
|
GenerateVSOutputMembers(out, api_type, uid_data->genMode_numtexgens, host_config,
|
2017-06-24 08:18:53 +00:00
|
|
|
GetInterpolationQualifier(msaa, ssaa, true, true));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-24 08:18:53 +00:00
|
|
|
if (stereo)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tflat int layer;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("}};\n");
|
2014-12-17 08:18:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-17 22:44:05 +00:00
|
|
|
// Let's set up attributes
|
2019-01-23 08:12:13 +00:00
|
|
|
u32 counter = 0;
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("VARYING_LOCATION({}) {} in float4 colors_0;\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa));
|
|
|
|
out.Write("VARYING_LOCATION({}) {} in float4 colors_1;\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa));
|
2020-10-20 18:42:54 +00:00
|
|
|
for (u32 i = 0; i < uid_data->genMode_numtexgens; ++i)
|
2014-12-17 22:44:05 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("VARYING_LOCATION({}) {} in float3 tex{};\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa), i);
|
2014-12-17 22:44:05 +00:00
|
|
|
}
|
2019-01-23 08:11:17 +00:00
|
|
|
if (!host_config.fast_depth_calc)
|
2020-10-20 18:42:54 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("VARYING_LOCATION({}) {} in float4 clipPos;\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa));
|
2020-10-20 18:42:54 +00:00
|
|
|
}
|
2017-06-24 08:18:53 +00:00
|
|
|
if (per_pixel_lighting)
|
2014-12-17 22:44:05 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("VARYING_LOCATION({}) {} in float3 Normal;\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa));
|
|
|
|
out.Write("VARYING_LOCATION({}) {} in float3 WorldPos;\n", counter++,
|
|
|
|
GetInterpolationQualifier(msaa, ssaa));
|
2014-12-17 22:44:05 +00:00
|
|
|
}
|
2014-12-17 08:18:40 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("void main()\n{{\n");
|
|
|
|
out.Write("\tfloat4 rawpos = gl_FragCoord;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
if (use_shader_blend)
|
|
|
|
{
|
|
|
|
// Store off a copy of the initial fb value for blending
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat4 initial_ocol0 = FB_FETCH_VALUE;\n"
|
|
|
|
"\tfloat4 ocol0;\n"
|
|
|
|
"\tfloat4 ocol1;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|
2010-10-21 05:22:18 +00:00
|
|
|
}
|
2013-09-22 16:07:21 +00:00
|
|
|
else // D3D
|
2010-10-21 05:22:18 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("void main(\n");
|
2017-09-03 06:32:37 +00:00
|
|
|
if (uid_data->uint_output)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(" out uint4 ocol0 : SV_Target,\n");
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2017-09-03 06:32:37 +00:00
|
|
|
else
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(" out float4 ocol0 : SV_Target0,\n"
|
|
|
|
" out float4 ocol1 : SV_Target1,\n");
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("{}"
|
|
|
|
" in float4 rawpos : SV_Position,\n",
|
|
|
|
uid_data->per_pixel_depth ? " out float depth : SV_Depth,\n" : "");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(" in {} float4 colors_0 : COLOR0,\n", GetInterpolationQualifier(msaa, ssaa));
|
|
|
|
out.Write(" in {} float4 colors_1 : COLOR1\n", GetInterpolationQualifier(msaa, ssaa));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-12-28 20:18:39 +00:00
|
|
|
// compute window position if needed because binding semantic WPOS is not widely supported
|
2020-10-20 18:42:54 +00:00
|
|
|
for (u32 i = 0; i < uid_data->genMode_numtexgens; ++i)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(",\n in {} float3 tex{} : TEXCOORD{}", GetInterpolationQualifier(msaa, ssaa), i,
|
|
|
|
i);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2019-01-23 08:11:17 +00:00
|
|
|
if (!host_config.fast_depth_calc)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(",\n in {} float4 clipPos : TEXCOORD{}", GetInterpolationQualifier(msaa, ssaa),
|
|
|
|
uid_data->genMode_numtexgens);
|
2019-01-23 08:11:17 +00:00
|
|
|
}
|
2017-06-24 08:18:53 +00:00
|
|
|
if (per_pixel_lighting)
|
2015-01-02 12:44:04 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(",\n in {} float3 Normal : TEXCOORD{}", GetInterpolationQualifier(msaa, ssaa),
|
|
|
|
uid_data->genMode_numtexgens + 1);
|
|
|
|
out.Write(",\n in {} float3 WorldPos : TEXCOORD{}", GetInterpolationQualifier(msaa, ssaa),
|
|
|
|
uid_data->genMode_numtexgens + 2);
|
2015-01-02 12:44:04 +00:00
|
|
|
}
|
2019-01-23 08:11:17 +00:00
|
|
|
if (host_config.backend_geometry_shaders)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(",\n in float clipDist0 : SV_ClipDistance0\n"
|
|
|
|
",\n in float clipDist1 : SV_ClipDistance1\n");
|
2019-01-23 08:11:17 +00:00
|
|
|
}
|
2017-06-24 08:18:53 +00:00
|
|
|
if (stereo)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(",\n in uint layer : SV_RenderTargetArrayIndex\n");
|
|
|
|
out.Write(" ) {{\n");
|
2009-02-19 04:41:58 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint4 c0 = " I_COLORS "[1], c1 = " I_COLORS "[2], c2 = " I_COLORS
|
|
|
|
"[3], prev = " I_COLORS "[0];\n"
|
|
|
|
"\tint4 rastemp = int4(0, 0, 0, 0), textemp = int4(0, 0, 0, 0), konsttemp = int4(0, 0, "
|
|
|
|
"0, 0);\n"
|
|
|
|
"\tint3 comp16 = int3(1, 256, 0), comp24 = int3(1, 256, 256*256);\n"
|
|
|
|
"\tint alphabump=0;\n"
|
|
|
|
"\tint3 tevcoord=int3(0, 0, 0);\n"
|
|
|
|
"\tint2 wrappedcoord=int2(0,0), tempcoord=int2(0,0);\n"
|
|
|
|
"\tint4 "
|
|
|
|
"tevin_a=int4(0,0,0,0),tevin_b=int4(0,0,0,0),tevin_c=int4(0,0,0,0),tevin_d=int4(0,0,0,"
|
|
|
|
"0);\n\n"); // tev combiner inputs
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-01-20 22:06:24 +00:00
|
|
|
// On GLSL, input variables must not be assigned to.
|
|
|
|
// This is why we declare these variables locally instead.
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat4 col0 = colors_0;\n"
|
|
|
|
"\tfloat4 col1 = colors_1;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-24 08:18:53 +00:00
|
|
|
if (per_pixel_lighting)
|
2010-09-23 02:17:48 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat3 _norm0 = normalize(Normal.xyz);\n\n"
|
|
|
|
"\tfloat3 pos = WorldPos;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint4 lacc;\n"
|
|
|
|
"\tfloat3 ldir, h, cosAttn, distAttn;\n"
|
|
|
|
"\tfloat dist, dist2, attn;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-05-30 14:17:30 +00:00
|
|
|
// TODO: Our current constant usage code isn't able to handle more than one buffer.
|
|
|
|
// So we can't mark the VS constant as used here. But keep them here as reference.
|
2014-05-30 13:59:29 +00:00
|
|
|
// out.SetConstantsUsed(C_PLIGHT_COLORS, C_PLIGHT_COLORS+7); // TODO: Can be optimized further
|
|
|
|
// out.SetConstantsUsed(C_PLIGHTS, C_PLIGHTS+31); // TODO: Can be optimized further
|
|
|
|
// out.SetConstantsUsed(C_PMATERIALS, C_PMATERIALS+3);
|
2017-01-29 12:38:48 +00:00
|
|
|
GenerateLightingShaderCode(out, uid_data->lighting, "colors_", "col");
|
|
|
|
if (uid_data->numColorChans == 0)
|
|
|
|
out.Write("col0 = float4(0.0, 0.0, 0.0, 0.0);\n");
|
|
|
|
if (uid_data->numColorChans <= 1)
|
|
|
|
out.Write("col1 = float4(0.0, 0.0, 0.0, 0.0);\n");
|
2014-12-25 23:56:12 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-01-12 03:39:14 +00:00
|
|
|
// HACK to handle cases where the tex gen is not enabled
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->genMode_numtexgens == 0)
|
2010-01-12 03:39:14 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 fixpoint_uv0 = int2(0, 0);\n\n");
|
2010-01-12 03:39:14 +00:00
|
|
|
}
|
|
|
|
else
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2016-01-16 13:25:16 +00:00
|
|
|
out.SetConstantsUsed(C_TEXDIMS, C_TEXDIMS + uid_data->genMode_numtexgens - 1);
|
2020-10-20 18:42:54 +00:00
|
|
|
for (u32 i = 0; i < uid_data->genMode_numtexgens; ++i)
|
2010-01-12 03:39:14 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 fixpoint_uv{} = int2(", i);
|
|
|
|
out.Write("(tex{}.z == 0.0 ? tex{}.xy : tex{}.xy / tex{}.z)", i, i, i, i);
|
|
|
|
out.Write(" * " I_TEXDIMS "[{}].zw);\n", i);
|
2014-01-29 14:28:22 +00:00
|
|
|
// TODO: S24 overflows here?
|
2010-01-12 03:39:14 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
for (u32 i = 0; i < uid_data->genMode_numindstages; ++i)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
if ((uid_data->nIndirectStagesUsed & (1U << i)) != 0)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
const u32 texcoord = uid_data->GetTevindirefCoord(i);
|
|
|
|
const u32 texmap = uid_data->GetTevindirefMap(i);
|
2016-01-16 13:25:16 +00:00
|
|
|
|
|
|
|
if (texcoord < uid_data->genMode_numtexgens)
|
2012-09-02 18:00:15 +00:00
|
|
|
{
|
|
|
|
out.SetConstantsUsed(C_INDTEXSCALE + i / 2, C_INDTEXSCALE + i / 2);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttempcoord = fixpoint_uv{} >> " I_INDTEXSCALE "[{}].{};\n", texcoord, i / 2,
|
|
|
|
(i & 1) != 0 ? "zw" : "xy");
|
2012-09-02 18:00:15 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttempcoord = int2(0, 0);\n");
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint3 iindtex{} = ", i);
|
2020-10-20 18:42:54 +00:00
|
|
|
SampleTexture(out, "float2(tempcoord)", "abg", texmap, stereo, api_type);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 00:16:38 +00:00
|
|
|
for (u32 i = 0; i < numStages; i++)
|
|
|
|
{
|
|
|
|
// Build the equation for this stage
|
2020-10-20 18:42:54 +00:00
|
|
|
WriteStage(out, uid_data, i, api_type, stereo);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-05-22 19:55:07 +00:00
|
|
|
{
|
|
|
|
// The results of the last texenv stage are put onto the screen,
|
|
|
|
// regardless of the used destination register
|
2016-01-16 13:25:16 +00:00
|
|
|
TevStageCombiner::ColorCombiner last_cc;
|
|
|
|
TevStageCombiner::AlphaCombiner last_ac;
|
|
|
|
last_cc.hex = uid_data->stagehash[uid_data->genMode_numtevstages].cc;
|
|
|
|
last_ac.hex = uid_data->stagehash[uid_data->genMode_numtevstages].ac;
|
2021-02-11 02:11:31 +00:00
|
|
|
if (last_cc.dest != TevOutput::Prev)
|
2012-08-11 14:54:46 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tprev.rgb = {};\n", tev_c_output_table[u32(last_cc.dest.Value())]);
|
2012-08-11 14:54:46 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
if (last_ac.dest != TevOutput::Prev)
|
2012-08-11 14:54:46 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tprev.a = {};\n", tev_a_output_table[u32(last_ac.dest.Value())]);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2012-08-11 14:54:46 +00:00
|
|
|
}
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tprev = prev & 255;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-19 19:27:54 +00:00
|
|
|
// NOTE: Fragment may not be discarded if alpha test always fails and early depth test is enabled
|
|
|
|
// (in this case we need to write a depth value if depth test passes regardless of the alpha
|
|
|
|
// testing result)
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->Pretest == AlphaTestResult::Undetermined ||
|
|
|
|
(uid_data->Pretest == AlphaTestResult::Fail && uid_data->late_ztest))
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
WriteAlphaTest(out, uid_data, api_type, uid_data->per_pixel_depth,
|
2017-10-26 05:44:39 +00:00
|
|
|
use_dual_source || use_shader_blend);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->zfreeze)
|
2013-03-26 21:16:29 +00:00
|
|
|
{
|
2015-05-07 20:48:49 +00:00
|
|
|
out.SetConstantsUsed(C_ZSLOPE, C_ZSLOPE);
|
|
|
|
out.SetConstantsUsed(C_EFBSCALE, C_EFBSCALE);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat2 screenpos = rawpos.xy * " I_EFBSCALE ".xy;\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-14 05:51:37 +00:00
|
|
|
// Opengl has reversed vertical screenspace coordinates
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::OpenGL)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tscreenpos.y = {}.0 - screenpos.y;\n", EFB_HEIGHT);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint zCoord = int(" I_ZSLOPE ".z + " I_ZSLOPE ".x * screenpos.x + " I_ZSLOPE
|
|
|
|
".y * screenpos.y);\n");
|
2015-05-07 20:48:49 +00:00
|
|
|
}
|
2017-07-20 07:10:02 +00:00
|
|
|
else if (!host_config.fast_depth_calc)
|
2015-05-07 20:48:49 +00:00
|
|
|
{
|
|
|
|
// FastDepth means to trust the depth generated in perspective division.
|
|
|
|
// It should be correct, but it seems not to be as accurate as required. TODO: Find out why!
|
|
|
|
// For disabled FastDepth we just calculate the depth value again.
|
|
|
|
// The performance impact of this additional calculation doesn't matter, but it prevents
|
|
|
|
// the host GPU driver from performing any early depth test optimizations.
|
2013-03-26 21:16:29 +00:00
|
|
|
out.SetConstantsUsed(C_ZBIAS + 1, C_ZBIAS + 1);
|
2013-05-09 15:48:48 +00:00
|
|
|
// the screen space depth value = far z + (clip z / clip w) * z range
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint zCoord = " I_ZBIAS "[1].x + int((clipPos.z / clipPos.w) * float(" I_ZBIAS
|
|
|
|
"[1].y));\n");
|
2013-03-26 21:16:29 +00:00
|
|
|
}
|
2015-05-07 20:48:49 +00:00
|
|
|
else
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!host_config.backend_reversed_depth_range)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint zCoord = int((1.0 - rawpos.z) * 16777216.0);\n");
|
2015-05-24 12:44:25 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint zCoord = int(rawpos.z * 16777216.0);\n");
|
2015-05-07 20:48:49 +00:00
|
|
|
}
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tzCoord = clamp(zCoord, 0, 0xFFFFFF);\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
// depth texture can safely be ignored if the result won't be written to the depth buffer
|
|
|
|
// (early_ztest) and isn't used for fog either
|
2021-02-11 02:11:31 +00:00
|
|
|
const bool skip_ztexture = !uid_data->per_pixel_depth && uid_data->fog_fsel == FogType::Off;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-05-10 10:51:06 +00:00
|
|
|
// Note: z-textures are not written to depth buffer if early depth test is used
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->per_pixel_depth && uid_data->early_ztest)
|
2014-12-25 07:34:22 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!host_config.backend_reversed_depth_range)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tdepth = 1.0 - float(zCoord) / 16777216.0;\n");
|
2015-05-24 12:44:25 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tdepth = float(zCoord) / 16777216.0;\n");
|
2014-12-25 07:34:22 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-06-29 21:14:08 +00:00
|
|
|
// Note: depth texture output is only written to depth buffer if late depth test is used
|
|
|
|
// theoretical final depth value is used for fog calculation, though, so we have to emulate
|
|
|
|
// ztextures anyway
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->ztex_op != ZTexOp::Disabled && !skip_ztexture)
|
2012-03-25 03:01:47 +00:00
|
|
|
{
|
2014-03-06 18:27:11 +00:00
|
|
|
// use the texture input of the last texture stage (textemp), hopefully this has been read and
|
|
|
|
// is in correct format...
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_ZBIAS, C_ZBIAS + 1);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tzCoord = idot(" I_ZBIAS "[0].xyzw, textemp.xyzw) + " I_ZBIAS "[1].w {};\n",
|
2021-02-11 02:11:31 +00:00
|
|
|
(uid_data->ztex_op == ZTexOp::Add) ? "+ zCoord" : "");
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tzCoord = zCoord & 0xFFFFFF;\n");
|
2012-08-10 16:57:37 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->per_pixel_depth && uid_data->late_ztest)
|
2014-12-25 07:34:22 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!host_config.backend_reversed_depth_range)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tdepth = 1.0 - float(zCoord) / 16777216.0;\n");
|
2015-05-24 12:44:25 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tdepth = float(zCoord) / 16777216.0;\n");
|
2014-12-25 07:34:22 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-09-08 00:43:05 +00:00
|
|
|
// No dithering for RGB8 mode
|
2016-09-30 21:11:12 +00:00
|
|
|
if (uid_data->dither)
|
2016-09-07 23:34:18 +00:00
|
|
|
{
|
2016-09-08 00:43:05 +00:00
|
|
|
// Flipper uses a standard 2x2 Bayer Matrix for 6 bit dithering
|
|
|
|
// Here the matrix is encoded into the two factor constants
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 dither = int2(rawpos.xy) & 1;\n");
|
|
|
|
out.Write("\tprev.rgb = (prev.rgb - (prev.rgb >> 6)) + abs(dither.y * 3 - dither.x * 2);\n");
|
2016-09-07 23:34:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
WriteFog(out, uid_data);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-10 13:34:55 +00:00
|
|
|
// Write the color and alpha values to the framebuffer
|
2017-10-26 05:44:39 +00:00
|
|
|
// If using shader blend, we still use the separate alpha
|
2020-10-20 18:42:54 +00:00
|
|
|
WriteColor(out, api_type, uid_data, use_dual_source || use_shader_blend);
|
2017-10-26 05:44:39 +00:00
|
|
|
|
|
|
|
if (use_shader_blend)
|
|
|
|
WriteBlend(out, uid_data);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->bounding_box)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tUpdateBoundingBox(rawpos.xy);\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("}}\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-11-03 02:47:05 +00:00
|
|
|
return out;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 12:41:26 +00:00
|
|
|
static void WriteStage(ShaderCode& out, const pixel_shader_uid_data* uid_data, int n,
|
2020-10-20 18:42:54 +00:00
|
|
|
APIType api_type, bool stereo)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
const auto& stage = uid_data->stagehash[n];
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\n\t// TEV stage {}\n", n);
|
2016-01-16 12:41:26 +00:00
|
|
|
|
2010-07-06 13:14:51 +00:00
|
|
|
// HACK to handle cases where the tex gen is not enabled
|
2016-01-16 12:41:26 +00:00
|
|
|
u32 texcoord = stage.tevorders_texcoord;
|
2020-10-20 18:42:54 +00:00
|
|
|
const bool has_tex_coord = texcoord < uid_data->genMode_numtexgens;
|
|
|
|
if (!has_tex_coord)
|
2010-07-06 13:14:51 +00:00
|
|
|
texcoord = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (stage.hasindstage)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2016-01-16 13:25:16 +00:00
|
|
|
TevStageIndirect tevind;
|
|
|
|
tevind.hex = stage.tevind;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t// indirect op\n");
|
2020-10-20 18:42:54 +00:00
|
|
|
|
|
|
|
// Perform the indirect op on the incoming regular coordinates
|
|
|
|
// using iindtex{} as the offset coords
|
2021-02-11 02:11:31 +00:00
|
|
|
if (tevind.bs != IndTexBumpAlpha::Off)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_ind_alpha_sel{
|
2019-05-30 00:50:17 +00:00
|
|
|
"",
|
|
|
|
"x",
|
|
|
|
"y",
|
|
|
|
"z",
|
|
|
|
};
|
|
|
|
|
|
|
|
// 0b11111000, 0b11100000, 0b11110000, 0b11111000
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_ind_alpha_mask{
|
2019-05-30 00:50:17 +00:00
|
|
|
"248",
|
|
|
|
"224",
|
|
|
|
"240",
|
|
|
|
"248",
|
|
|
|
};
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("alphabump = iindtex{}.{} & {};\n", tevind.bt.Value(),
|
|
|
|
tev_ind_alpha_sel[u32(tevind.bs.Value())],
|
|
|
|
tev_ind_alpha_mask[u32(tevind.fmt.Value())]);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2013-09-12 11:55:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: Should we reset alphabump to 0 here?
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-04-11 00:41:06 +00:00
|
|
|
if (tevind.matrix_index != IndMtxIndex::Off)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-05-05 04:36:09 +00:00
|
|
|
// format
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_ind_fmt_mask{
|
2019-05-30 00:50:17 +00:00
|
|
|
"255",
|
|
|
|
"31",
|
|
|
|
"15",
|
|
|
|
"7",
|
|
|
|
};
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint3 iindtevcrd{} = iindtex{} & {};\n", n, tevind.bt.Value(),
|
2021-02-11 02:11:31 +00:00
|
|
|
tev_ind_fmt_mask[u32(tevind.fmt.Value())]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-05-30 00:16:38 +00:00
|
|
|
// bias - TODO: Check if this needs to be this complicated...
|
|
|
|
// indexed by bias
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 8> tev_ind_bias_field{
|
2019-05-30 00:50:17 +00:00
|
|
|
"", "x", "y", "xy", "z", "xz", "yz", "xyz",
|
|
|
|
};
|
|
|
|
|
2019-05-30 00:16:38 +00:00
|
|
|
// indexed by fmt
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_ind_bias_add{
|
2019-05-30 00:50:17 +00:00
|
|
|
"-128",
|
|
|
|
"1",
|
|
|
|
"1",
|
|
|
|
"1",
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (tevind.bias == IndTexBias::S || tevind.bias == IndTexBias::T ||
|
|
|
|
tevind.bias == IndTexBias::U)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tiindtevcrd{}.{} += int({});\n", n,
|
|
|
|
tev_ind_bias_field[u32(tevind.bias.Value())],
|
|
|
|
tev_ind_bias_add[u32(tevind.fmt.Value())]);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
else if (tevind.bias == IndTexBias::ST || tevind.bias == IndTexBias::SU ||
|
|
|
|
tevind.bias == IndTexBias::TU_)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tiindtevcrd{0}.{1} += int2({2}, {2});\n", n,
|
|
|
|
tev_ind_bias_field[u32(tevind.bias.Value())],
|
|
|
|
tev_ind_bias_add[u32(tevind.fmt.Value())]);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
else if (tevind.bias == IndTexBias::STU)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tiindtevcrd{0}.{1} += int3({2}, {2}, {2});\n", n,
|
|
|
|
tev_ind_bias_field[u32(tevind.bias.Value())],
|
|
|
|
tev_ind_bias_add[u32(tevind.fmt.Value())]);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-04-11 00:41:06 +00:00
|
|
|
// Multiplied by 2 because each matrix has two rows.
|
|
|
|
// Note also that the 4th column of the matrix contains the scale factor.
|
|
|
|
const u32 mtxidx = 2 * (static_cast<u32>(tevind.matrix_index.Value()) - 1);
|
|
|
|
|
2014-05-05 04:36:09 +00:00
|
|
|
// multiply by offset matrix and scale - calculations are likely to overflow badly,
|
|
|
|
// yet it works out since we only care about the lower 23 bits (+1 sign bit) of the result
|
2021-04-11 00:41:06 +00:00
|
|
|
if (tevind.matrix_id == IndMtxId::Indirect)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_INDTEXMTX + mtxidx, C_INDTEXMTX + mtxidx);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 indtevtrans{} = int2(idot(" I_INDTEXMTX
|
|
|
|
"[{}].xyz, iindtevcrd{}), idot(" I_INDTEXMTX "[{}].xyz, iindtevcrd{})) >> 3;\n",
|
|
|
|
n, mtxidx, n, mtxidx + 1, n);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-09-05 04:45:35 +00:00
|
|
|
// TODO: should use a shader uid branch for this for better performance
|
2017-05-04 02:22:26 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BITWISE_OP_NEGATION))
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint indtexmtx_w_inverse_{} = -" I_INDTEXMTX "[{}].w;\n", n, mtxidx);
|
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= indtexmtx_w_inverse_{};\n", n, n);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= (-" I_INDTEXMTX "[{}].w);\n", n, mtxidx);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2021-04-11 00:41:06 +00:00
|
|
|
else if (tevind.matrix_id == IndMtxId::S)
|
|
|
|
{
|
|
|
|
ASSERT(has_tex_coord);
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_INDTEXMTX + mtxidx, C_INDTEXMTX + mtxidx);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 indtevtrans{} = int2(fixpoint_uv{} * iindtevcrd{}.xx) >> 8;\n", n,
|
|
|
|
texcoord, n);
|
2017-05-04 02:22:26 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BITWISE_OP_NEGATION))
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint indtexmtx_w_inverse_{} = -" I_INDTEXMTX "[{}].w;\n", n, mtxidx);
|
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= (indtexmtx_w_inverse_{});\n", n, n);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= (-" I_INDTEXMTX "[{}].w);\n", n, mtxidx);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2021-04-11 00:41:06 +00:00
|
|
|
else if (tevind.matrix_id == IndMtxId::T)
|
|
|
|
{
|
|
|
|
ASSERT(has_tex_coord);
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_INDTEXMTX + mtxidx, C_INDTEXMTX + mtxidx);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 indtevtrans{} = int2(fixpoint_uv{} * iindtevcrd{}.yy) >> 8;\n", n,
|
|
|
|
texcoord, n);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-05-04 02:22:26 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BITWISE_OP_NEGATION))
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint indtexmtx_w_inverse_{} = -" I_INDTEXMTX "[{}].w;\n", n, mtxidx);
|
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= (indtexmtx_w_inverse_{});\n", n, n);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tif (" I_INDTEXMTX "[{}].w >= 0) indtevtrans{} >>= " I_INDTEXMTX "[{}].w;\n",
|
|
|
|
mtxidx, n, mtxidx);
|
|
|
|
out.Write("\telse indtevtrans{} <<= (-" I_INDTEXMTX "[{}].w);\n", n, mtxidx);
|
2017-05-04 02:22:26 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-24 13:21:54 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 indtevtrans{} = int2(0, 0);\n", n);
|
2021-04-11 00:41:06 +00:00
|
|
|
ASSERT(false); // Unknown value for matrix_id
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2013-04-24 13:21:54 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint2 indtevtrans{} = int2(0, 0);\n", n);
|
2021-04-11 00:41:06 +00:00
|
|
|
// If matrix_index is Off (0), matrix_id should be Indirect (0)
|
|
|
|
ASSERT(tevind.matrix_id == IndMtxId::Indirect);
|
2013-04-24 13:21:54 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 13:14:51 +00:00
|
|
|
// ---------
|
2009-07-26 09:52:35 +00:00
|
|
|
// Wrapping
|
|
|
|
// ---------
|
2019-05-30 00:50:17 +00:00
|
|
|
|
|
|
|
// TODO: Should the last element be 1 or (1<<7)?
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 7> tev_ind_wrap_start{
|
2019-05-30 00:50:17 +00:00
|
|
|
"0", "(256<<7)", "(128<<7)", "(64<<7)", "(32<<7)", "(16<<7)", "1",
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 13:14:51 +00:00
|
|
|
// wrap S
|
2021-02-11 02:11:31 +00:00
|
|
|
if (tevind.sw == IndTexWrap::ITW_OFF)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.x = fixpoint_uv{}.x;\n", texcoord);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
else if (tevind.sw == IndTexWrap::ITW_0)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.x = 0;\n");
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.x = fixpoint_uv{}.x & ({} - 1);\n", texcoord,
|
2021-02-11 02:11:31 +00:00
|
|
|
tev_ind_wrap_start[u32(tevind.sw.Value())]);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 13:14:51 +00:00
|
|
|
// wrap T
|
2021-02-11 02:11:31 +00:00
|
|
|
if (tevind.tw == IndTexWrap::ITW_OFF)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.y = fixpoint_uv{}.y;\n", texcoord);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
else if (tevind.tw == IndTexWrap::ITW_0)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.y = 0;\n");
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\twrappedcoord.y = fixpoint_uv{}.y & ({} - 1);\n", texcoord,
|
2021-02-11 02:11:31 +00:00
|
|
|
tev_ind_wrap_start[u32(tevind.tw.Value())]);
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (tevind.fb_addprev) // add previous tevcoord
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttevcoord.xy += wrappedcoord + indtevtrans{};\n", n);
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttevcoord.xy = wrappedcoord + indtevtrans{};\n", n);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-09-12 11:55:38 +00:00
|
|
|
// Emulate s24 overflows
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttevcoord.xy = (tevcoord.xy << 8) >> 8;\n");
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
TevStageCombiner::ColorCombiner cc;
|
|
|
|
TevStageCombiner::AlphaCombiner ac;
|
|
|
|
cc.hex = stage.cc;
|
|
|
|
ac.hex = stage.ac;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.a == TevColorArg::RasAlpha || cc.a == TevColorArg::RasColor ||
|
|
|
|
cc.b == TevColorArg::RasAlpha || cc.b == TevColorArg::RasColor ||
|
|
|
|
cc.c == TevColorArg::RasAlpha || cc.c == TevColorArg::RasColor ||
|
|
|
|
cc.d == TevColorArg::RasAlpha || cc.d == TevColorArg::RasColor ||
|
|
|
|
ac.a == TevAlphaArg::RasAlpha || ac.b == TevAlphaArg::RasAlpha ||
|
|
|
|
ac.c == TevAlphaArg::RasAlpha || ac.d == TevAlphaArg::RasAlpha)
|
2010-08-16 22:37:04 +00:00
|
|
|
{
|
2016-01-16 12:41:26 +00:00
|
|
|
// Generate swizzle string to represent the Ras color channel swapping
|
2019-05-30 00:50:17 +00:00
|
|
|
const char rasswap[5] = {
|
|
|
|
"rgba"[stage.tevksel_swap1a],
|
|
|
|
"rgba"[stage.tevksel_swap2a],
|
|
|
|
"rgba"[stage.tevksel_swap1b],
|
|
|
|
"rgba"[stage.tevksel_swap2b],
|
|
|
|
'\0',
|
|
|
|
};
|
2016-01-16 13:25:16 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\trastemp = {}.{};\n", tev_ras_table[u32(stage.tevorders_colorchan)], rasswap);
|
2010-08-16 22:37:04 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 13:25:16 +00:00
|
|
|
if (stage.tevorders_enable)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2016-01-16 12:41:26 +00:00
|
|
|
// Generate swizzle string to represent the texture color channel swapping
|
2019-05-30 00:50:17 +00:00
|
|
|
const char texswap[5] = {
|
|
|
|
"rgba"[stage.tevksel_swap1c],
|
|
|
|
"rgba"[stage.tevksel_swap2c],
|
|
|
|
"rgba"[stage.tevksel_swap1d],
|
|
|
|
"rgba"[stage.tevksel_swap2d],
|
|
|
|
'\0',
|
|
|
|
};
|
2016-01-16 12:41:26 +00:00
|
|
|
|
|
|
|
if (!stage.hasindstage)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2010-07-06 13:14:51 +00:00
|
|
|
// calc tevcord
|
2020-10-20 18:42:54 +00:00
|
|
|
if (has_tex_coord)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttevcoord.xy = fixpoint_uv{};\n", texcoord);
|
2010-07-06 13:14:51 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttevcoord.xy = int2(0, 0);\n");
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttextemp = ");
|
2020-10-20 18:42:54 +00:00
|
|
|
SampleTexture(out, "float2(tevcoord.xy)", texswap, stage.tevorders_texmap, stereo, api_type);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-24 13:21:54 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\ttextemp = int4(255, 255, 255, 255);\n");
|
2013-04-24 13:21:54 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.a == TevColorArg::Konst || cc.b == TevColorArg::Konst || cc.c == TevColorArg::Konst ||
|
|
|
|
cc.d == TevColorArg::Konst || ac.a == TevAlphaArg::Konst || ac.b == TevAlphaArg::Konst ||
|
|
|
|
ac.c == TevAlphaArg::Konst || ac.d == TevAlphaArg::Konst)
|
2010-08-16 22:37:04 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tkonsttemp = int4({}, {});\n", tev_ksel_table_c[u32(stage.tevksel_kc)],
|
|
|
|
tev_ksel_table_a[u32(stage.tevksel_ka)]);
|
2016-01-16 13:25:16 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (u32(stage.tevksel_kc) > 7)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.SetConstantsUsed(C_KCOLORS + ((u32(stage.tevksel_kc) - 0xc) % 4),
|
|
|
|
C_KCOLORS + ((u32(stage.tevksel_kc) - 0xc) % 4));
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
if (u32(stage.tevksel_ka) > 7)
|
2019-05-30 00:16:38 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.SetConstantsUsed(C_KCOLORS + ((u32(stage.tevksel_ka) - 0xc) % 4),
|
|
|
|
C_KCOLORS + ((u32(stage.tevksel_ka) - 0xc) % 4));
|
2019-05-30 00:16:38 +00:00
|
|
|
}
|
2010-08-16 22:37:04 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.d == TevColorArg::Color0 || cc.d == TevColorArg::Alpha0 || ac.d == TevAlphaArg::Alpha0)
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_COLORS + 1, C_COLORS + 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.d == TevColorArg::Color1 || cc.d == TevColorArg::Alpha1 || ac.d == TevAlphaArg::Alpha1)
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_COLORS + 2, C_COLORS + 2);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.d == TevColorArg::Color2 || cc.d == TevColorArg::Alpha2 || ac.d == TevAlphaArg::Alpha2)
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_COLORS + 3, C_COLORS + 3);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (cc.dest >= TevOutput::Color0)
|
|
|
|
out.SetConstantsUsed(C_COLORS + u32(cc.dest.Value()), C_COLORS + u32(cc.dest.Value()));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (ac.dest >= TevOutput::Color0)
|
|
|
|
out.SetConstantsUsed(C_COLORS + u32(ac.dest.Value()), C_COLORS + u32(ac.dest.Value()));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-04-11 09:08:42 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_VECTOR_BITWISE_AND))
|
|
|
|
{
|
|
|
|
out.Write("\ttevin_a = int4({} & 255, {} & 255);\n", tev_c_input_table[u32(cc.a.Value())],
|
|
|
|
tev_a_input_table[u32(ac.a.Value())]);
|
|
|
|
out.Write("\ttevin_b = int4({} & 255, {} & 255);\n", tev_c_input_table[u32(cc.b.Value())],
|
|
|
|
tev_a_input_table[u32(ac.b.Value())]);
|
|
|
|
out.Write("\ttevin_c = int4({} & 255, {} & 255);\n", tev_c_input_table[u32(cc.c.Value())],
|
|
|
|
tev_a_input_table[u32(ac.c.Value())]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.Write("\ttevin_a = int4({}, {})&int4(255, 255, 255, 255);\n",
|
|
|
|
tev_c_input_table[u32(cc.a.Value())], tev_a_input_table[u32(ac.a.Value())]);
|
|
|
|
out.Write("\ttevin_b = int4({}, {})&int4(255, 255, 255, 255);\n",
|
|
|
|
tev_c_input_table[u32(cc.b.Value())], tev_a_input_table[u32(ac.b.Value())]);
|
|
|
|
out.Write("\ttevin_c = int4({}, {})&int4(255, 255, 255, 255);\n",
|
|
|
|
tev_c_input_table[u32(cc.c.Value())], tev_a_input_table[u32(ac.c.Value())]);
|
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\ttevin_d = int4({}, {});\n", tev_c_input_table[u32(cc.d.Value())],
|
|
|
|
tev_a_input_table[u32(ac.d.Value())]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t// color combine\n");
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\t{} = clamp(", tev_c_output_table[u32(cc.dest.Value())]);
|
|
|
|
if (cc.bias != TevBias::Compare)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
WriteTevRegular(out, "rgb", cc.bias, cc.op, cc.clamp, cc.scale, false);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
|
|
|
else
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 8> function_table{
|
2021-02-11 02:11:31 +00:00
|
|
|
"((tevin_a.r > tevin_b.r) ? tevin_c.rgb : int3(0,0,0))", // TevCompareMode::R8, GT
|
|
|
|
"((tevin_a.r == tevin_b.r) ? tevin_c.rgb : int3(0,0,0))", // R8, TevComparison::EQ
|
2016-06-24 10:16:10 +00:00
|
|
|
"((idot(tevin_a.rgb, comp16) > idot(tevin_b.rgb, comp16)) ? tevin_c.rgb : "
|
2021-02-11 02:11:31 +00:00
|
|
|
"int3(0,0,0))", // GR16, GT
|
2016-06-24 10:16:10 +00:00
|
|
|
"((idot(tevin_a.rgb, comp16) == idot(tevin_b.rgb, comp16)) ? tevin_c.rgb : "
|
2021-02-11 02:11:31 +00:00
|
|
|
"int3(0,0,0))", // GR16, EQ
|
2016-06-24 10:16:10 +00:00
|
|
|
"((idot(tevin_a.rgb, comp24) > idot(tevin_b.rgb, comp24)) ? tevin_c.rgb : "
|
2021-02-11 02:11:31 +00:00
|
|
|
"int3(0,0,0))", // BGR24, GT
|
2016-06-24 10:16:10 +00:00
|
|
|
"((idot(tevin_a.rgb, comp24) == idot(tevin_b.rgb, comp24)) ? tevin_c.rgb : "
|
2021-02-11 02:11:31 +00:00
|
|
|
"int3(0,0,0))", // BGR24, EQ
|
|
|
|
"(max(sign(tevin_a.rgb - tevin_b.rgb), int3(0,0,0)) * tevin_c.rgb)", // RGB8, GT
|
|
|
|
"((int3(1,1,1) - sign(abs(tevin_a.rgb - tevin_b.rgb))) * tevin_c.rgb)" // RGB8, EQ
|
2016-06-24 10:16:10 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
const u32 mode = (u32(cc.compare_mode.Value()) << 1) | u32(cc.comparison.Value());
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(" tevin_d.rgb + ");
|
|
|
|
out.Write("{}", function_table[mode]);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2010-01-12 03:39:14 +00:00
|
|
|
if (cc.clamp)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(", int3(0,0,0), int3(255,255,255))");
|
2013-08-14 16:54:43 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(", int3(-1024,-1024,-1024), int3(1023,1023,1023))");
|
|
|
|
out.Write(";\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t// alpha combine\n");
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\t{} = clamp(", tev_a_output_table[u32(ac.dest.Value())]);
|
|
|
|
if (ac.bias != TevBias::Compare)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
WriteTevRegular(out, "a", ac.bias, ac.op, ac.clamp, ac.scale, true);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
|
|
|
else
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 8> function_table{
|
2021-02-11 02:11:31 +00:00
|
|
|
"((tevin_a.r > tevin_b.r) ? tevin_c.a : 0)", // TevCompareMode::R8, GT
|
|
|
|
"((tevin_a.r == tevin_b.r) ? tevin_c.a : 0)", // R8, TevComparison::EQ
|
|
|
|
"((idot(tevin_a.rgb, comp16) > idot(tevin_b.rgb, comp16)) ? tevin_c.a : 0)", // GR16, GT
|
|
|
|
"((idot(tevin_a.rgb, comp16) == idot(tevin_b.rgb, comp16)) ? tevin_c.a : 0)", // GR16, EQ
|
|
|
|
"((idot(tevin_a.rgb, comp24) > idot(tevin_b.rgb, comp24)) ? tevin_c.a : 0)", // BGR24, GT
|
|
|
|
"((idot(tevin_a.rgb, comp24) == idot(tevin_b.rgb, comp24)) ? tevin_c.a : 0)", // BGR24, EQ
|
|
|
|
"((tevin_a.a > tevin_b.a) ? tevin_c.a : 0)", // A8, GT
|
|
|
|
"((tevin_a.a == tevin_b.a) ? tevin_c.a : 0)" // A8, EQ
|
2014-02-28 19:24:00 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
const u32 mode = (u32(ac.compare_mode.Value()) << 1) | u32(ac.comparison.Value());
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(" tevin_d.a + ");
|
|
|
|
out.Write("{}", function_table[mode]);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2010-01-12 03:39:14 +00:00
|
|
|
if (ac.clamp)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(", 0, 255)");
|
2013-08-14 16:54:43 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(", -1024, 1023)");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(";\n");
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
static void WriteTevRegular(ShaderCode& out, std::string_view components, TevBias bias, TevOp op,
|
|
|
|
bool clamp, TevScale scale, bool alpha)
|
2014-03-24 13:41:56 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_scale_table_left{
|
2021-02-11 02:11:31 +00:00
|
|
|
"", // Scale1
|
|
|
|
" << 1", // Scale2
|
|
|
|
" << 2", // Scale4
|
|
|
|
"", // Divide2
|
2014-03-24 13:41:56 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_scale_table_right{
|
2021-02-11 02:11:31 +00:00
|
|
|
"", // Scale1
|
|
|
|
"", // Scale2
|
|
|
|
"", // Scale4
|
|
|
|
" >> 1", // Divide2
|
2014-03-24 13:41:56 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
// indexed by 2*op+(scale==Divide2)
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_lerp_bias{
|
2019-05-30 00:50:17 +00:00
|
|
|
"",
|
|
|
|
" + 128",
|
|
|
|
"",
|
|
|
|
" + 127",
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<const char*, 4> tev_bias_table{
|
2021-02-11 02:11:31 +00:00
|
|
|
"", // Zero,
|
|
|
|
" + 128", // AddHalf,
|
|
|
|
" - 128", // SubHalf,
|
2014-03-24 13:41:56 +00:00
|
|
|
"",
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<char, 2> tev_op_table{
|
2021-02-11 02:11:31 +00:00
|
|
|
'+', // TevOp::Add = 0,
|
|
|
|
'-', // TevOp::Sub = 1,
|
2014-03-24 13:41:56 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-24 13:41:56 +00:00
|
|
|
// Regular TEV stage: (d + bias + lerp(a,b,c)) * scale
|
2015-01-11 05:17:29 +00:00
|
|
|
// The GameCube/Wii GPU uses a very sophisticated algorithm for scale-lerping:
|
2014-03-24 13:41:56 +00:00
|
|
|
// - c is scaled from 0..255 to 0..256, which allows dividing the result by 256 instead of 255
|
|
|
|
// - if scale is bigger than one, it is moved inside the lerp calculation for increased accuracy
|
|
|
|
// - a rounding bias is added before dividing by 256
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("(((tevin_d.{}{}){})", components, tev_bias_table[u32(bias)],
|
|
|
|
tev_scale_table_left[u32(scale)]);
|
|
|
|
out.Write(" {} ", tev_op_table[u32(op)]);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("(((((tevin_a.{}<<8) + (tevin_b.{}-tevin_a.{})*(tevin_c.{}+(tevin_c.{}>>7))){}){})>>8)",
|
2021-02-11 02:11:31 +00:00
|
|
|
components, components, components, components, components,
|
|
|
|
tev_scale_table_left[u32(scale)],
|
|
|
|
tev_lerp_bias[2 * u32(op) + ((scale == TevScale::Divide2) == alpha)]);
|
|
|
|
out.Write("){}", tev_scale_table_right[u32(scale)]);
|
2014-03-24 13:41:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
static void SampleTexture(ShaderCode& out, std::string_view texcoords, std::string_view texswap,
|
|
|
|
int texmap, bool stereo, APIType api_type)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_TEXDIMS + texmap, C_TEXDIMS + texmap);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::D3D)
|
2016-08-12 14:55:00 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("iround(255.0 * Tex[{}].Sample(samp[{}], float3({}.xy * " I_TEXDIMS
|
|
|
|
"[{}].xy, {}))).{};\n",
|
|
|
|
texmap, texmap, texcoords, texmap, stereo ? "layer" : "0.0", texswap);
|
2016-08-12 14:55:00 +00:00
|
|
|
}
|
2013-08-14 13:40:24 +00:00
|
|
|
else
|
2016-08-12 14:55:00 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("iround(255.0 * texture(samp[{}], float3({}.xy * " I_TEXDIMS "[{}].xy, {}))).{};\n",
|
|
|
|
texmap, texcoords, texmap, stereo ? "layer" : "0.0", texswap);
|
2016-08-12 14:55:00 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 8> tev_alpha_funcs_table{
|
2021-02-11 02:11:31 +00:00
|
|
|
"(false)", // CompareMode::Never
|
|
|
|
"(prev.a < {})", // CompareMode::Less
|
|
|
|
"(prev.a == {})", // CompareMode::Equal
|
|
|
|
"(prev.a <= {})", // CompareMode::LEqual
|
|
|
|
"(prev.a > {})", // CompareMode::Greater
|
|
|
|
"(prev.a != {})", // CompareMode::NEqual
|
|
|
|
"(prev.a >= {})", // CompareMode::GEqual
|
|
|
|
"(true)" // CompareMode::Always
|
2009-10-25 02:35:21 +00:00
|
|
|
};
|
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 4> tev_alpha_funclogic_table{
|
2009-10-25 02:35:21 +00:00
|
|
|
" && ", // and
|
2010-07-06 13:14:51 +00:00
|
|
|
" || ", // or
|
|
|
|
" != ", // xor
|
|
|
|
" == " // xnor
|
2009-10-25 02:35:21 +00:00
|
|
|
};
|
2013-01-08 15:40:15 +00:00
|
|
|
|
2020-10-20 18:42:54 +00:00
|
|
|
static void WriteAlphaTest(ShaderCode& out, const pixel_shader_uid_data* uid_data, APIType api_type,
|
2016-10-29 22:34:10 +00:00
|
|
|
bool per_pixel_depth, bool use_dual_source)
|
2010-10-10 14:35:31 +00:00
|
|
|
{
|
2020-10-20 18:42:54 +00:00
|
|
|
static constexpr std::array<std::string_view, 2> alpha_ref{
|
|
|
|
I_ALPHA ".r",
|
|
|
|
I_ALPHA ".g",
|
|
|
|
};
|
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
const auto write_alpha_func = [&out](CompareMode mode, std::string_view ref) {
|
|
|
|
const bool has_no_arguments = mode == CompareMode::Never || mode == CompareMode::Always;
|
2020-10-20 18:42:54 +00:00
|
|
|
if (has_no_arguments)
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("{}", tev_alpha_funcs_table[u32(mode)]);
|
2020-10-20 18:42:54 +00:00
|
|
|
else
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write(tev_alpha_funcs_table[u32(mode)], ref);
|
2020-10-20 18:42:54 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-09-02 18:00:15 +00:00
|
|
|
out.SetConstantsUsed(C_ALPHA, C_ALPHA);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-29 22:56:18 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_NEGATED_BOOLEAN))
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tif(( ");
|
2014-12-03 06:29:50 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tif(!( ");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-08-07 12:36:56 +00:00
|
|
|
// Lookup the first component from the alpha function table
|
2021-02-11 02:11:31 +00:00
|
|
|
write_alpha_func(uid_data->alpha_test_comp0, alpha_ref[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
// Lookup the logic op
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("{}", tev_alpha_funclogic_table[u32(uid_data->alpha_test_logic)]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2012-08-07 12:36:56 +00:00
|
|
|
// Lookup the second component from the alpha function table
|
2021-02-11 02:11:31 +00:00
|
|
|
write_alpha_func(uid_data->alpha_test_comp1, alpha_ref[1]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-29 22:56:18 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_NEGATED_BOOLEAN))
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(") == false) {{\n");
|
2014-12-03 06:29:50 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(")) {{\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t\tocol0 = float4(0.0, 0.0, 0.0, 0.0);\n");
|
2020-10-20 18:42:54 +00:00
|
|
|
if (use_dual_source && !(api_type == APIType::D3D && uid_data->uint_output))
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t\tocol1 = float4(0.0, 0.0, 0.0, 0.0);\n");
|
2014-03-10 11:30:55 +00:00
|
|
|
if (per_pixel_depth)
|
2016-08-12 14:55:00 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t\tdepth = {};\n",
|
|
|
|
!g_ActiveConfig.backend_info.bSupportsReversedDepthRange ? "0.0" : "1.0");
|
2016-08-12 14:55:00 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-17 08:02:45 +00:00
|
|
|
// ZCOMPLOC HACK:
|
2014-10-21 06:52:45 +00:00
|
|
|
if (!uid_data->alpha_test_use_zcomploc_hack)
|
2012-08-10 16:57:37 +00:00
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t\tdiscard;\n");
|
2020-10-20 18:42:54 +00:00
|
|
|
if (api_type == APIType::D3D)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t\treturn;\n");
|
2012-08-10 16:57:37 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\t}}\n");
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-19 04:41:58 +00:00
|
|
|
|
2019-05-30 00:50:17 +00:00
|
|
|
constexpr std::array<const char*, 8> tev_fog_funcs_table{
|
2014-02-16 20:30:18 +00:00
|
|
|
"", // No Fog
|
|
|
|
"", // ?
|
|
|
|
"", // Linear
|
|
|
|
"", // ?
|
|
|
|
"\tfog = 1.0 - exp2(-8.0 * fog);\n", // exp
|
|
|
|
"\tfog = 1.0 - exp2(-8.0 * fog * fog);\n", // exp2
|
|
|
|
"\tfog = exp2(-8.0 * (1.0 - fog));\n", // backward exp
|
|
|
|
"\tfog = 1.0 - fog;\n fog = exp2(-8.0 * fog * fog);\n" // backward exp2
|
2009-10-25 02:35:21 +00:00
|
|
|
};
|
|
|
|
|
2016-01-16 12:41:26 +00:00
|
|
|
static void WriteFog(ShaderCode& out, const pixel_shader_uid_data* uid_data)
|
2009-02-19 04:41:58 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->fog_fsel == FogType::Off)
|
2011-12-26 05:15:54 +00:00
|
|
|
return; // no Fog
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-10-27 12:10:00 +00:00
|
|
|
out.SetConstantsUsed(C_FOGCOLOR, C_FOGCOLOR);
|
2013-12-16 12:08:09 +00:00
|
|
|
out.SetConstantsUsed(C_FOGI, C_FOGI);
|
|
|
|
out.SetConstantsUsed(C_FOGF, C_FOGF + 1);
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->fog_proj == FogProjection::Perspective)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2010-07-06 13:14:51 +00:00
|
|
|
// perspective
|
2010-11-23 13:57:01 +00:00
|
|
|
// ze = A/(B - (Zs >> B_SHF)
|
2013-12-16 12:08:09 +00:00
|
|
|
// TODO: Verify that we want to drop lower bits here! (currently taken over from software
|
|
|
|
// renderer)
|
2014-02-28 19:53:31 +00:00
|
|
|
// Maybe we want to use "ze = (A << B_SHF)/((B << B_SHF) - Zs)" instead?
|
|
|
|
// That's equivalent, but keeps the lower bits of Zs.
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat ze = (" I_FOGF ".x * 16777216.0) / float(" I_FOGI ".y - (zCoord >> " I_FOGI
|
|
|
|
".w));\n");
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
|
|
|
else
|
2009-10-25 02:35:21 +00:00
|
|
|
{
|
2010-07-06 13:14:51 +00:00
|
|
|
// orthographic
|
2014-02-16 20:30:18 +00:00
|
|
|
// ze = a*Zs (here, no B_SHF)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat ze = " I_FOGF ".x * float(zCoord) / 16777216.0;\n");
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-11-23 13:57:01 +00:00
|
|
|
// x_adjust = sqrt((x-center)^2 + k^2)/k
|
|
|
|
// ze *= x_adjust
|
2016-01-16 13:25:16 +00:00
|
|
|
if (uid_data->fog_RangeBaseEnabled)
|
2011-01-29 04:31:56 +00:00
|
|
|
{
|
2013-12-16 12:08:09 +00:00
|
|
|
out.SetConstantsUsed(C_FOGF, C_FOGF);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat offset = (2.0 * (rawpos.x / " I_FOGF ".w)) - 1.0 - " I_FOGF ".z;\n"
|
|
|
|
"\tfloat floatindex = clamp(9.0 - abs(offset) * 9.0, 0.0, 9.0);\n"
|
|
|
|
"\tuint indexlower = uint(floatindex);\n"
|
|
|
|
"\tuint indexupper = indexlower + 1u;\n"
|
|
|
|
"\tfloat klower = " I_FOGRANGE "[indexlower >> 2u][indexlower & 3u];\n"
|
|
|
|
"\tfloat kupper = " I_FOGRANGE "[indexupper >> 2u][indexupper & 3u];\n"
|
|
|
|
"\tfloat k = lerp(klower, kupper, frac(floatindex));\n"
|
|
|
|
"\tfloat x_adjust = sqrt(offset * offset + k * k) / k;\n"
|
|
|
|
"\tze *= x_adjust;\n");
|
2011-01-29 04:31:56 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat fog = clamp(ze - " I_FOGF ".y, 0.0, 1.0);\n");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->fog_fsel >= FogType::Exp)
|
2009-10-25 02:35:21 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("{}", tev_fog_funcs_table[u32(uid_data->fog_fsel)]);
|
2009-10-25 02:35:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
if (uid_data->fog_fsel != FogType::Linear)
|
|
|
|
WARN_LOG_FMT(VIDEO, "Unknown Fog Type! {}", uid_data->fog_fsel);
|
2010-07-06 13:14:51 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tint ifog = iround(fog * 256.0);\n");
|
|
|
|
out.Write("\tprev.rgb = (prev.rgb * (256 - ifog) + " I_FOGCOLOR ".rgb * ifog) >> 8;\n");
|
2012-08-06 23:02:04 +00:00
|
|
|
}
|
2016-09-07 15:19:26 +00:00
|
|
|
|
2017-09-03 06:32:37 +00:00
|
|
|
static void WriteColor(ShaderCode& out, APIType api_type, const pixel_shader_uid_data* uid_data,
|
|
|
|
bool use_dual_source)
|
2016-09-07 15:19:26 +00:00
|
|
|
{
|
2017-09-03 06:32:37 +00:00
|
|
|
// D3D requires that the shader outputs be uint when writing to a uint render target for logic op.
|
|
|
|
if (api_type == APIType::D3D && uid_data->uint_output)
|
|
|
|
{
|
|
|
|
if (uid_data->rgba6_format)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0 = uint4(prev & 0xFC);\n");
|
2017-09-03 06:32:37 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0 = uint4(prev);\n");
|
2017-09-03 06:32:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:19:26 +00:00
|
|
|
if (uid_data->rgba6_format)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0.rgb = float3(prev.rgb >> 2) / 63.0;\n");
|
2016-09-07 15:19:26 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0.rgb = float3(prev.rgb) / 255.0;\n");
|
2016-09-08 00:43:05 +00:00
|
|
|
|
2016-10-10 13:34:55 +00:00
|
|
|
// Colors will be blended against the 8-bit alpha from ocol1 and
|
|
|
|
// the 6-bit alpha from ocol0 will be written to the framebuffer
|
2018-03-27 22:44:10 +00:00
|
|
|
if (uid_data->useDstAlpha)
|
2016-10-10 13:34:55 +00:00
|
|
|
{
|
2018-03-27 22:44:10 +00:00
|
|
|
out.SetConstantsUsed(C_ALPHA, C_ALPHA);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0.a = float(" I_ALPHA ".a >> 2) / 63.0;\n");
|
2018-03-27 22:44:10 +00:00
|
|
|
|
|
|
|
// Use dual-source color blending to perform dst alpha in a single pass
|
2016-10-29 22:34:10 +00:00
|
|
|
if (use_dual_source)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol1 = float4(0.0, 0.0, 0.0, float(prev.a) / 255.0);\n");
|
2016-10-10 13:34:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol0.a = float(prev.a >> 2) / 63.0;\n");
|
2016-10-29 22:34:10 +00:00
|
|
|
if (use_dual_source)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tocol1 = float4(0.0, 0.0, 0.0, float(prev.a) / 255.0);\n");
|
2016-10-10 13:34:55 +00:00
|
|
|
}
|
2016-09-07 15:19:26 +00:00
|
|
|
}
|
2017-10-26 05:44:39 +00:00
|
|
|
|
|
|
|
static void WriteBlend(ShaderCode& out, const pixel_shader_uid_data* uid_data)
|
|
|
|
{
|
|
|
|
if (uid_data->blend_enable)
|
|
|
|
{
|
2019-05-30 00:50:17 +00:00
|
|
|
static constexpr std::array<const char*, 8> blend_src_factor{
|
2017-10-26 05:44:39 +00:00
|
|
|
"float3(0,0,0);", // ZERO
|
|
|
|
"float3(1,1,1);", // ONE
|
|
|
|
"initial_ocol0.rgb;", // DSTCLR
|
|
|
|
"float3(1,1,1) - initial_ocol0.rgb;", // INVDSTCLR
|
|
|
|
"ocol1.aaa;", // SRCALPHA
|
|
|
|
"float3(1,1,1) - ocol1.aaa;", // INVSRCALPHA
|
|
|
|
"initial_ocol0.aaa;", // DSTALPHA
|
|
|
|
"float3(1,1,1) - initial_ocol0.aaa;", // INVDSTALPHA
|
2019-05-30 00:50:17 +00:00
|
|
|
};
|
|
|
|
static constexpr std::array<const char*, 8> blend_src_factor_alpha{
|
2017-10-26 05:44:39 +00:00
|
|
|
"0.0;", // ZERO
|
|
|
|
"1.0;", // ONE
|
|
|
|
"initial_ocol0.a;", // DSTCLR
|
|
|
|
"1.0 - initial_ocol0.a;", // INVDSTCLR
|
|
|
|
"ocol1.a;", // SRCALPHA
|
|
|
|
"1.0 - ocol1.a;", // INVSRCALPHA
|
|
|
|
"initial_ocol0.a;", // DSTALPHA
|
|
|
|
"1.0 - initial_ocol0.a;", // INVDSTALPHA
|
2019-05-30 00:50:17 +00:00
|
|
|
};
|
|
|
|
static constexpr std::array<const char*, 8> blend_dst_factor{
|
2017-10-26 05:44:39 +00:00
|
|
|
"float3(0,0,0);", // ZERO
|
|
|
|
"float3(1,1,1);", // ONE
|
|
|
|
"ocol0.rgb;", // SRCCLR
|
|
|
|
"float3(1,1,1) - ocol0.rgb;", // INVSRCCLR
|
|
|
|
"ocol1.aaa;", // SRCALHA
|
|
|
|
"float3(1,1,1) - ocol1.aaa;", // INVSRCALPHA
|
|
|
|
"initial_ocol0.aaa;", // DSTALPHA
|
|
|
|
"float3(1,1,1) - initial_ocol0.aaa;", // INVDSTALPHA
|
2019-05-30 00:50:17 +00:00
|
|
|
};
|
|
|
|
static constexpr std::array<const char*, 8> blend_dst_factor_alpha{
|
2017-10-26 05:44:39 +00:00
|
|
|
"0.0;", // ZERO
|
|
|
|
"1.0;", // ONE
|
|
|
|
"ocol0.a;", // SRCCLR
|
|
|
|
"1.0 - ocol0.a;", // INVSRCCLR
|
|
|
|
"ocol1.a;", // SRCALPHA
|
|
|
|
"1.0 - ocol1.a;", // INVSRCALPHA
|
|
|
|
"initial_ocol0.a;", // DSTALPHA
|
|
|
|
"1.0 - initial_ocol0.a;", // INVDSTALPHA
|
2019-05-30 00:50:17 +00:00
|
|
|
};
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat4 blend_src;\n");
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tblend_src.rgb = {}\n", blend_src_factor[u32(uid_data->blend_src_factor)]);
|
|
|
|
out.Write("\tblend_src.a = {}\n",
|
|
|
|
blend_src_factor_alpha[u32(uid_data->blend_src_factor_alpha)]);
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat4 blend_dst;\n");
|
2021-02-11 02:11:31 +00:00
|
|
|
out.Write("\tblend_dst.rgb = {}\n", blend_dst_factor[u32(uid_data->blend_dst_factor)]);
|
|
|
|
out.Write("\tblend_dst.a = {}\n",
|
|
|
|
blend_dst_factor_alpha[u32(uid_data->blend_dst_factor_alpha)]);
|
2020-11-09 07:23:32 +00:00
|
|
|
|
|
|
|
out.Write("\tfloat4 blend_result;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
if (uid_data->blend_subtract)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tblend_result.rgb = initial_ocol0.rgb * blend_dst.rgb - ocol0.rgb * "
|
|
|
|
"blend_src.rgb;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write(
|
2017-10-26 05:44:39 +00:00
|
|
|
"\tblend_result.rgb = initial_ocol0.rgb * blend_dst.rgb + ocol0.rgb * blend_src.rgb;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid_data->blend_subtract_alpha)
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tblend_result.a = initial_ocol0.a * blend_dst.a - ocol0.a * blend_src.a;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tblend_result.a = initial_ocol0.a * blend_dst.a + ocol0.a * blend_src.a;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\tfloat4 blend_result = ocol0;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
out.Write("\treal_ocol0 = blend_result;\n");
|
2017-10-26 05:44:39 +00:00
|
|
|
}
|