2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2019-06-01 05:55:09 -06:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
|
|
|
|
|
|
|
#include <array>
|
2014-12-04 07:15:16 -07:00
|
|
|
#include <cmath>
|
2015-09-20 23:16:21 -06:00
|
|
|
#include <cstring>
|
2019-06-01 05:55:09 -06:00
|
|
|
#include <iterator>
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2014-10-21 18:42:55 -06:00
|
|
|
#include "Common/BitSet.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/ChunkFile.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-04-26 15:40:25 -06:00
|
|
|
#include "Common/Config/Config.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/Logging/Log.h"
|
2019-01-29 15:05:51 -07:00
|
|
|
#include "Common/Matrix.h"
|
2020-04-26 15:40:25 -06:00
|
|
|
#include "Core/Config/GraphicsSettings.h"
|
2015-07-22 23:39:44 -06:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/Core.h"
|
2018-01-21 05:04:15 -07:00
|
|
|
#include "VideoCommon/BPFunctions.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
#include "VideoCommon/CPMemory.h"
|
2020-04-26 15:40:25 -06:00
|
|
|
#include "VideoCommon/FreeLookCamera.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2014-02-18 18:27:20 -07:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/XFMemory.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2019-01-29 16:12:06 -07:00
|
|
|
alignas(16) static std::array<float, 16> g_fProjectionMatrix;
|
2008-12-07 22:25:12 -07:00
|
|
|
|
|
|
|
// track changes
|
2019-06-20 06:09:38 -06:00
|
|
|
static std::array<bool, 2> bTexMatricesChanged;
|
|
|
|
static bool bPosNormalMatrixChanged;
|
|
|
|
static bool bProjectionChanged;
|
|
|
|
static bool bViewportChanged;
|
|
|
|
static bool bTexMtxInfoChanged;
|
|
|
|
static bool bLightingConfigChanged;
|
2014-10-21 18:42:55 -06:00
|
|
|
static BitSet32 nMaterialsChanged;
|
2019-06-20 06:09:38 -06:00
|
|
|
static std::array<int, 2> nTransformMatricesChanged; // min,max
|
|
|
|
static std::array<int, 2> nNormalMatricesChanged; // min,max
|
|
|
|
static std::array<int, 2> nPostTransformMatricesChanged; // min,max
|
|
|
|
static std::array<int, 2> nLightsChanged; // min,max
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2019-01-29 16:12:06 -07:00
|
|
|
static Common::Matrix44 s_viewportCorrection;
|
2009-04-19 04:10:45 -06:00
|
|
|
|
2013-10-07 08:02:24 -06:00
|
|
|
VertexShaderConstants VertexShaderManager::constants;
|
|
|
|
bool VertexShaderManager::dirty;
|
|
|
|
|
2013-10-14 07:16:42 -06:00
|
|
|
// Viewport correction:
|
|
|
|
// In D3D, the viewport rectangle must fit within the render target.
|
|
|
|
// Say you want a viewport at (ix, iy) with size (iw, ih),
|
|
|
|
// but your viewport must be clamped at (ax, ay) with size (aw, ah).
|
|
|
|
// Just multiply the projection matrix with the following to get the same
|
|
|
|
// effect:
|
|
|
|
// [ (iw/aw) 0 0 ((iw - 2*(ax-ix)) / aw - 1) ]
|
|
|
|
// [ 0 (ih/ah) 0 ((-ih + 2*(ay-iy)) / ah + 1) ]
|
|
|
|
// [ 0 0 1 0 ]
|
|
|
|
// [ 0 0 0 1 ]
|
2019-01-29 16:12:06 -07:00
|
|
|
static void ViewportCorrectionMatrix(Common::Matrix44& result)
|
2013-10-14 07:16:42 -06:00
|
|
|
{
|
|
|
|
int scissorXOff = bpmem.scissorOffset.x * 2;
|
|
|
|
int scissorYOff = bpmem.scissorOffset.y * 2;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-14 07:16:42 -06:00
|
|
|
// TODO: ceil, floor or just cast to int?
|
|
|
|
// TODO: Directly use the floats instead of rounding them?
|
2014-04-27 12:59:04 -06:00
|
|
|
float intendedX = xfmem.viewport.xOrig - xfmem.viewport.wd - scissorXOff;
|
|
|
|
float intendedY = xfmem.viewport.yOrig + xfmem.viewport.ht - scissorYOff;
|
|
|
|
float intendedWd = 2.0f * xfmem.viewport.wd;
|
|
|
|
float intendedHt = -2.0f * xfmem.viewport.ht;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-02-16 13:30:18 -07:00
|
|
|
if (intendedWd < 0.f)
|
|
|
|
{
|
|
|
|
intendedX += intendedWd;
|
|
|
|
intendedWd = -intendedWd;
|
|
|
|
}
|
|
|
|
if (intendedHt < 0.f)
|
|
|
|
{
|
|
|
|
intendedY += intendedHt;
|
|
|
|
intendedHt = -intendedHt;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-14 07:16:42 -06:00
|
|
|
// fit to EFB size
|
2014-02-16 13:30:18 -07:00
|
|
|
float X = (intendedX >= 0.f) ? intendedX : 0.f;
|
|
|
|
float Y = (intendedY >= 0.f) ? intendedY : 0.f;
|
|
|
|
float Wd = (X + intendedWd <= EFB_WIDTH) ? intendedWd : (EFB_WIDTH - X);
|
|
|
|
float Ht = (Y + intendedHt <= EFB_HEIGHT) ? intendedHt : (EFB_HEIGHT - Y);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-01-29 16:12:06 -07:00
|
|
|
result = Common::Matrix44::Identity();
|
2013-10-14 07:16:42 -06:00
|
|
|
if (Wd == 0 || Ht == 0)
|
|
|
|
return;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
result.data[4 * 0 + 0] = intendedWd / Wd;
|
|
|
|
result.data[4 * 0 + 3] = (intendedWd - 2.f * (X - intendedX)) / Wd - 1.f;
|
|
|
|
result.data[4 * 1 + 1] = intendedHt / Ht;
|
|
|
|
result.data[4 * 1 + 3] = (-intendedHt + 2.f * (Y - intendedY)) / Ht + 1.f;
|
2013-10-14 07:16:42 -06:00
|
|
|
}
|
|
|
|
|
2008-12-26 03:43:18 -07:00
|
|
|
void VertexShaderManager::Init()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2015-01-15 09:29:39 -07:00
|
|
|
// Initialize state tracking variables
|
2019-06-20 06:09:38 -06:00
|
|
|
nTransformMatricesChanged.fill(-1);
|
|
|
|
nNormalMatricesChanged.fill(-1);
|
|
|
|
nPostTransformMatricesChanged.fill(-1);
|
|
|
|
nLightsChanged.fill(-1);
|
2015-01-15 09:29:39 -07:00
|
|
|
nMaterialsChanged = BitSet32(0);
|
2019-06-20 06:09:38 -06:00
|
|
|
bTexMatricesChanged.fill(false);
|
2015-01-15 09:29:39 -07:00
|
|
|
bPosNormalMatrixChanged = false;
|
|
|
|
bProjectionChanged = true;
|
|
|
|
bViewportChanged = false;
|
2017-07-19 23:25:27 -06:00
|
|
|
bTexMtxInfoChanged = false;
|
|
|
|
bLightingConfigChanged = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-10-14 16:09:17 -06:00
|
|
|
std::memset(static_cast<void*>(&xfmem), 0, sizeof(xfmem));
|
2016-12-10 15:07:28 -07:00
|
|
|
constants = {};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-01-02 03:20:22 -07:00
|
|
|
// TODO: should these go inside ResetView()?
|
2019-01-29 16:12:06 -07:00
|
|
|
s_viewportCorrection = Common::Matrix44::Identity();
|
|
|
|
g_fProjectionMatrix = Common::Matrix44::Identity().data;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-15 09:29:39 -07:00
|
|
|
dirty = true;
|
2008-12-26 03:43:18 -07:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2009-09-15 15:49:15 -06:00
|
|
|
void VertexShaderManager::Dirty()
|
|
|
|
{
|
2015-01-15 09:29:39 -07:00
|
|
|
// This function is called after a savestate is loaded.
|
|
|
|
// Any constants that can changed based on settings should be re-calculated
|
2009-09-15 15:49:15 -06:00
|
|
|
bProjectionChanged = true;
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2013-10-07 08:02:24 -06:00
|
|
|
dirty = true;
|
2009-09-15 15:49:15 -06:00
|
|
|
}
|
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
// Syncs the shader constant buffers with xfmem
|
2013-04-07 15:11:29 -06:00
|
|
|
// TODO: A cleaner way to control the matrices without making a mess in the parameters field
|
2009-08-15 01:20:37 -06:00
|
|
|
void VertexShaderManager::SetConstants()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2021-02-22 20:03:06 -07:00
|
|
|
if (constants.missing_color_hex != g_ActiveConfig.iMissingColorValue)
|
|
|
|
{
|
|
|
|
const float a = (g_ActiveConfig.iMissingColorValue) & 0xFF;
|
|
|
|
const float b = (g_ActiveConfig.iMissingColorValue >> 8) & 0xFF;
|
|
|
|
const float g = (g_ActiveConfig.iMissingColorValue >> 16) & 0xFF;
|
|
|
|
const float r = (g_ActiveConfig.iMissingColorValue >> 24) & 0xFF;
|
|
|
|
constants.missing_color_hex = g_ActiveConfig.iMissingColorValue;
|
|
|
|
constants.missing_color_value = {r / 255, g / 255, b / 255, a / 255};
|
|
|
|
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (nTransformMatricesChanged[0] >= 0)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int startn = nTransformMatricesChanged[0] / 4;
|
|
|
|
int endn = (nTransformMatricesChanged[1] + 3) / 4;
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.transformmatrices[startn].data(), &xfmem.posMatrices[startn * 4],
|
2015-09-20 23:53:10 -06:00
|
|
|
(endn - startn) * sizeof(float4));
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2010-06-04 19:38:22 -06:00
|
|
|
nTransformMatricesChanged[0] = nTransformMatricesChanged[1] = -1;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (nNormalMatricesChanged[0] >= 0)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int startn = nNormalMatricesChanged[0] / 3;
|
|
|
|
int endn = (nNormalMatricesChanged[1] + 2) / 3;
|
2015-02-15 12:43:31 -07:00
|
|
|
for (int i = startn; i < endn; i++)
|
2013-10-08 15:58:51 -06:00
|
|
|
{
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.normalmatrices[i].data(), &xfmem.normalMatrices[3 * i], 12);
|
2013-10-08 15:58:51 -06:00
|
|
|
}
|
|
|
|
dirty = true;
|
2009-09-19 04:46:25 -06:00
|
|
|
nNormalMatricesChanged[0] = nNormalMatricesChanged[1] = -1;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (nPostTransformMatricesChanged[0] >= 0)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int startn = nPostTransformMatricesChanged[0] / 4;
|
2015-02-15 12:43:31 -07:00
|
|
|
int endn = (nPostTransformMatricesChanged[1] + 3) / 4;
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.posttransformmatrices[startn].data(), &xfmem.postMatrices[startn * 4],
|
2015-09-20 23:53:10 -06:00
|
|
|
(endn - startn) * sizeof(float4));
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2012-12-01 06:23:24 -07:00
|
|
|
nPostTransformMatricesChanged[0] = nPostTransformMatricesChanged[1] = -1;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (nLightsChanged[0] >= 0)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2013-10-27 06:57:40 -06:00
|
|
|
// TODO: Outdated comment
|
2010-06-04 19:38:22 -06:00
|
|
|
// lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats
|
|
|
|
int istart = nLightsChanged[0] / 0x10;
|
|
|
|
int iend = (nLightsChanged[1] + 15) / 0x10;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
for (int i = istart; i < iend; ++i)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2014-05-30 06:54:16 -06:00
|
|
|
const Light& light = xfmem.lights[i];
|
2014-05-30 08:17:30 -06:00
|
|
|
VertexShaderConstants::Light& dstlight = constants.lights[i];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-05-30 06:54:16 -06:00
|
|
|
// xfmem.light.color is packed as abgr in u8[4], so we have to swap the order
|
2014-05-30 08:17:30 -06:00
|
|
|
dstlight.color[0] = light.color[3];
|
|
|
|
dstlight.color[1] = light.color[2];
|
|
|
|
dstlight.color[2] = light.color[1];
|
|
|
|
dstlight.color[3] = light.color[0];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-05-30 08:17:30 -06:00
|
|
|
dstlight.cosatt[0] = light.cosatt[0];
|
|
|
|
dstlight.cosatt[1] = light.cosatt[1];
|
|
|
|
dstlight.cosatt[2] = light.cosatt[2];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-05-30 08:17:30 -06:00
|
|
|
if (fabs(light.distatt[0]) < 0.00001f && fabs(light.distatt[1]) < 0.00001f &&
|
|
|
|
fabs(light.distatt[2]) < 0.00001f)
|
|
|
|
{
|
|
|
|
// dist attenuation, make sure not equal to 0!!!
|
|
|
|
dstlight.distatt[0] = .00001f;
|
|
|
|
}
|
|
|
|
else
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2014-05-30 08:17:30 -06:00
|
|
|
dstlight.distatt[0] = light.distatt[0];
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2014-05-30 08:17:30 -06:00
|
|
|
dstlight.distatt[1] = light.distatt[1];
|
|
|
|
dstlight.distatt[2] = light.distatt[2];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-05-30 08:17:30 -06:00
|
|
|
dstlight.pos[0] = light.dpos[0];
|
|
|
|
dstlight.pos[1] = light.dpos[1];
|
|
|
|
dstlight.pos[2] = light.dpos[2];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-06-27 02:40:45 -06:00
|
|
|
double norm = double(light.ddir[0]) * double(light.ddir[0]) +
|
|
|
|
double(light.ddir[1]) * double(light.ddir[1]) +
|
|
|
|
double(light.ddir[2]) * double(light.ddir[2]);
|
|
|
|
norm = 1.0 / sqrt(norm);
|
2014-08-19 21:16:50 -06:00
|
|
|
float norm_float = static_cast<float>(norm);
|
|
|
|
dstlight.dir[0] = light.ddir[0] * norm_float;
|
|
|
|
dstlight.dir[1] = light.ddir[1] * norm_float;
|
|
|
|
dstlight.dir[2] = light.ddir[2] * norm_float;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
nLightsChanged[0] = nLightsChanged[1] = -1;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-21 18:42:55 -06:00
|
|
|
for (int i : nMaterialsChanged)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2014-10-21 18:42:55 -06:00
|
|
|
u32 data = i >= 2 ? xfmem.matColor[i - 2] : xfmem.ambColor[i];
|
|
|
|
constants.materials[i][0] = (data >> 24) & 0xFF;
|
|
|
|
constants.materials[i][1] = (data >> 16) & 0xFF;
|
|
|
|
constants.materials[i][2] = (data >> 8) & 0xFF;
|
|
|
|
constants.materials[i][3] = data & 0xFF;
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2014-10-21 18:42:55 -06:00
|
|
|
nMaterialsChanged = BitSet32(0);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (bPosNormalMatrixChanged)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bPosNormalMatrixChanged = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-20 23:16:21 -06:00
|
|
|
const float* pos = &xfmem.posMatrices[g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4];
|
|
|
|
const float* norm =
|
|
|
|
&xfmem.normalMatrices[3 * (g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31)];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.posnormalmatrix.data(), pos, 3 * sizeof(float4));
|
|
|
|
memcpy(constants.posnormalmatrix[3].data(), norm, 3 * sizeof(float));
|
|
|
|
memcpy(constants.posnormalmatrix[4].data(), norm + 3, 3 * sizeof(float));
|
|
|
|
memcpy(constants.posnormalmatrix[5].data(), norm + 6, 3 * sizeof(float));
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2009-09-12 09:00:08 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (bTexMatricesChanged[0])
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bTexMatricesChanged[0] = false;
|
2019-06-01 05:55:09 -06:00
|
|
|
const std::array<const float*, 4> pos_matrix_ptrs{
|
2015-09-20 23:16:21 -06:00
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4],
|
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4],
|
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4],
|
2019-06-01 05:55:09 -06:00
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4],
|
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-06-01 05:55:09 -06:00
|
|
|
for (size_t i = 0; i < pos_matrix_ptrs.size(); ++i)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.texmatrices[3 * i].data(), pos_matrix_ptrs[i], 3 * sizeof(float4));
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (bTexMatricesChanged[1])
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bTexMatricesChanged[1] = false;
|
2019-06-01 05:55:09 -06:00
|
|
|
const std::array<const float*, 4> pos_matrix_ptrs{
|
2015-09-20 23:16:21 -06:00
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4],
|
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4],
|
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4],
|
2019-06-01 05:55:09 -06:00
|
|
|
&xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4],
|
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-06-01 05:55:09 -06:00
|
|
|
for (size_t i = 0; i < pos_matrix_ptrs.size(); ++i)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2017-08-27 11:56:16 -06:00
|
|
|
memcpy(constants.texmatrices[3 * i + 12].data(), pos_matrix_ptrs[i], 3 * sizeof(float4));
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2011-12-08 02:20:31 -07:00
|
|
|
if (bViewportChanged)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bViewportChanged = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-23 15:42:28 -06:00
|
|
|
// The console GPU places the pixel center at 7/12 unless antialiasing
|
|
|
|
// is enabled, while D3D and OpenGL place it at 0.5. See the comment
|
|
|
|
// in VertexShaderGen.cpp for details.
|
|
|
|
// NOTE: If we ever emulate antialiasing, the sample locations set by
|
|
|
|
// BP registers 0x01-0x04 need to be considered here.
|
|
|
|
const float pixel_center_correction = 7.0f / 12.0f - 0.5f;
|
2017-07-03 08:32:02 -06:00
|
|
|
const bool bUseVertexRounding = g_ActiveConfig.bVertexRounding && g_ActiveConfig.iEFBScale != 1;
|
2017-02-18 17:22:41 -07:00
|
|
|
const float viewport_width = bUseVertexRounding ?
|
|
|
|
(2.f * xfmem.viewport.wd) :
|
|
|
|
g_renderer->EFBToScaledXf(2.f * xfmem.viewport.wd);
|
|
|
|
const float viewport_height = bUseVertexRounding ?
|
|
|
|
(2.f * xfmem.viewport.ht) :
|
|
|
|
g_renderer->EFBToScaledXf(2.f * xfmem.viewport.ht);
|
|
|
|
const float pixel_size_x = 2.f / viewport_width;
|
|
|
|
const float pixel_size_y = 2.f / viewport_height;
|
2014-10-24 18:59:02 -06:00
|
|
|
constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x;
|
|
|
|
constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y;
|
2016-08-02 18:01:36 -06:00
|
|
|
|
2017-01-13 06:01:04 -07:00
|
|
|
// By default we don't change the depth value at all in the vertex shader.
|
|
|
|
constants.pixelcentercorrection[2] = 1.0f;
|
|
|
|
constants.pixelcentercorrection[3] = 0.0f;
|
|
|
|
|
2017-02-18 17:22:41 -07:00
|
|
|
constants.viewport[0] = (2.f * xfmem.viewport.wd);
|
|
|
|
constants.viewport[1] = (2.f * xfmem.viewport.ht);
|
|
|
|
|
2017-02-24 07:16:28 -07:00
|
|
|
if (g_renderer->UseVertexDepthRange())
|
2016-08-21 09:47:24 -06:00
|
|
|
{
|
2017-01-13 06:01:04 -07:00
|
|
|
// Oversized depth ranges are handled in the vertex shader. We need to reverse
|
2017-02-24 07:16:28 -07:00
|
|
|
// the far value to use the reversed-Z trick.
|
2017-01-05 06:40:37 -07:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
|
|
|
|
{
|
2017-02-24 07:16:28 -07:00
|
|
|
// Sometimes the console also tries to use the reversed-Z trick. We can only do
|
|
|
|
// that with the expected accuracy if the backend can reverse the depth range.
|
|
|
|
constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f;
|
|
|
|
if (xfmem.viewport.zRange < 0.0f)
|
|
|
|
constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f;
|
|
|
|
else
|
|
|
|
constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
|
2017-01-05 06:40:37 -07:00
|
|
|
}
|
2016-08-21 09:47:24 -06:00
|
|
|
else
|
2017-01-05 06:40:37 -07:00
|
|
|
{
|
2017-02-24 07:16:28 -07:00
|
|
|
// For backends that don't support reversing the depth range we can still render
|
|
|
|
// cases where the console uses the reversed-Z trick. But we simply can't provide
|
|
|
|
// the expected accuracy, which might result in z-fighting.
|
|
|
|
constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f;
|
|
|
|
constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f;
|
2017-01-05 06:40:37 -07:00
|
|
|
}
|
2016-08-21 09:47:24 -06:00
|
|
|
}
|
2016-08-02 18:01:36 -06:00
|
|
|
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2018-01-21 05:04:15 -07:00
|
|
|
BPFunctions::SetViewport();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-14 07:16:42 -06:00
|
|
|
// Update projection if the viewport isn't 1:1 useable
|
2014-03-10 05:30:55 -06:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsOversizedViewports)
|
2013-10-14 07:16:42 -06:00
|
|
|
{
|
|
|
|
ViewportCorrectionMatrix(s_viewportCorrection);
|
|
|
|
bProjectionChanged = true;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-10-14 07:16:42 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-04-24 21:59:35 -06:00
|
|
|
if (bProjectionChanged || g_freelook_camera.GetController()->IsDirty())
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bProjectionChanged = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-06-20 05:42:16 -06:00
|
|
|
const auto& rawProjection = xfmem.projection.rawProjection;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-04-27 12:59:04 -06:00
|
|
|
switch (xfmem.projection.type)
|
2010-06-04 19:38:22 -06:00
|
|
|
{
|
2021-02-10 17:01:42 -07:00
|
|
|
case ProjectionType::Perspective:
|
2020-05-18 16:22:41 -06:00
|
|
|
{
|
|
|
|
const Common::Vec2 fov =
|
2020-06-18 22:49:07 -06:00
|
|
|
g_freelook_camera.IsActive() ? g_freelook_camera.GetFieldOfView() : Common::Vec2{1, 1};
|
2020-05-18 16:22:41 -06:00
|
|
|
g_fProjectionMatrix[0] = rawProjection[0] * g_ActiveConfig.fAspectRatioHackW * fov.x;
|
2009-12-05 07:01:29 -07:00
|
|
|
g_fProjectionMatrix[1] = 0.0f;
|
2020-05-18 16:22:41 -06:00
|
|
|
g_fProjectionMatrix[2] = rawProjection[1] * g_ActiveConfig.fAspectRatioHackW * fov.x;
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[3] = 0.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[4] = 0.0f;
|
2020-05-18 16:22:41 -06:00
|
|
|
g_fProjectionMatrix[5] = rawProjection[2] * g_ActiveConfig.fAspectRatioHackH * fov.y;
|
|
|
|
g_fProjectionMatrix[6] = rawProjection[3] * g_ActiveConfig.fAspectRatioHackH * fov.y;
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[7] = 0.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[8] = 0.0f;
|
|
|
|
g_fProjectionMatrix[9] = 0.0f;
|
2013-01-24 05:10:13 -07:00
|
|
|
g_fProjectionMatrix[10] = rawProjection[4];
|
|
|
|
g_fProjectionMatrix[11] = rawProjection[5];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[12] = 0.0f;
|
|
|
|
g_fProjectionMatrix[13] = 0.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-08-30 17:54:05 -06:00
|
|
|
g_fProjectionMatrix[14] = -1.0f;
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[15] = 0.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-07-10 21:34:50 -06:00
|
|
|
g_stats.gproj = g_fProjectionMatrix;
|
2020-05-18 16:22:41 -06:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-02-10 17:01:42 -07:00
|
|
|
case ProjectionType::Orthographic:
|
2020-05-18 16:22:41 -06:00
|
|
|
{
|
2013-01-24 05:10:13 -07:00
|
|
|
g_fProjectionMatrix[0] = rawProjection[0];
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[1] = 0.0f;
|
|
|
|
g_fProjectionMatrix[2] = 0.0f;
|
2013-01-24 05:10:13 -07:00
|
|
|
g_fProjectionMatrix[3] = rawProjection[1];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[4] = 0.0f;
|
2013-01-24 05:10:13 -07:00
|
|
|
g_fProjectionMatrix[5] = rawProjection[2];
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[6] = 0.0f;
|
2013-01-24 05:10:13 -07:00
|
|
|
g_fProjectionMatrix[7] = rawProjection[3];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[8] = 0.0f;
|
|
|
|
g_fProjectionMatrix[9] = 0.0f;
|
2018-04-01 13:27:06 -06:00
|
|
|
g_fProjectionMatrix[10] = rawProjection[4];
|
|
|
|
g_fProjectionMatrix[11] = rawProjection[5];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
g_fProjectionMatrix[12] = 0.0f;
|
|
|
|
g_fProjectionMatrix[13] = 0.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-11-11 02:22:19 -07:00
|
|
|
g_fProjectionMatrix[14] = 0.0f;
|
2016-08-30 17:54:05 -06:00
|
|
|
g_fProjectionMatrix[15] = 1.0f;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-07-10 21:34:50 -06:00
|
|
|
g_stats.g2proj = g_fProjectionMatrix;
|
|
|
|
g_stats.proj = rawProjection;
|
2020-05-18 16:22:41 -06:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-01-24 05:10:13 -07:00
|
|
|
default:
|
2020-11-13 20:33:26 -07:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Unknown projection type: {}", xfmem.projection.type);
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-11-13 20:33:26 -07:00
|
|
|
PRIM_LOG("Projection: {} {} {} {} {} {}", rawProjection[0], rawProjection[1], rawProjection[2],
|
2016-11-01 19:19:00 -06:00
|
|
|
rawProjection[3], rawProjection[4], rawProjection[5]);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-03-09 09:25:41 -07:00
|
|
|
auto corrected_matrix = s_viewportCorrection * Common::Matrix44::FromArray(g_fProjectionMatrix);
|
|
|
|
|
2021-02-10 17:01:42 -07:00
|
|
|
if (g_freelook_camera.IsActive() && xfmem.projection.type == ProjectionType::Perspective)
|
2020-04-26 15:40:25 -06:00
|
|
|
corrected_matrix *= g_freelook_camera.GetView();
|
2019-01-29 16:12:06 -07:00
|
|
|
|
2019-03-09 09:25:41 -07:00
|
|
|
memcpy(constants.projection.data(), corrected_matrix.data.data(), 4 * sizeof(float4));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-04-24 21:59:35 -06:00
|
|
|
g_freelook_camera.GetController()->SetClean();
|
2020-06-17 11:07:40 -06:00
|
|
|
|
2013-10-08 15:58:51 -06:00
|
|
|
dirty = true;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2017-07-19 23:25:27 -06:00
|
|
|
|
|
|
|
if (bTexMtxInfoChanged)
|
|
|
|
{
|
|
|
|
bTexMtxInfoChanged = false;
|
|
|
|
constants.xfmem_dualTexInfo = xfmem.dualTexTrans.enabled;
|
2019-06-01 05:55:09 -06:00
|
|
|
for (size_t i = 0; i < std::size(xfmem.texMtxInfo); i++)
|
2017-07-19 23:25:27 -06:00
|
|
|
constants.xfmem_pack1[i][0] = xfmem.texMtxInfo[i].hex;
|
2019-06-01 05:55:09 -06:00
|
|
|
for (size_t i = 0; i < std::size(xfmem.postMtxInfo); i++)
|
2017-07-19 23:25:27 -06:00
|
|
|
constants.xfmem_pack1[i][1] = xfmem.postMtxInfo[i].hex;
|
|
|
|
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bLightingConfigChanged)
|
|
|
|
{
|
|
|
|
bLightingConfigChanged = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
constants.xfmem_pack1[i][2] = xfmem.color[i].hex;
|
|
|
|
constants.xfmem_pack1[i][3] = xfmem.alpha[i].hex;
|
|
|
|
}
|
|
|
|
constants.xfmem_numColorChans = xfmem.numChan.numColorChans;
|
|
|
|
dirty = true;
|
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2008-12-26 03:43:18 -07:00
|
|
|
void VertexShaderManager::InvalidateXFRange(int start, int end)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2014-08-27 11:26:06 -06:00
|
|
|
if (((u32)start >= (u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >=
|
|
|
|
XFMEM_NORMALMATRICES + ((u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31) * 3 &&
|
|
|
|
(u32)start < XFMEM_NORMALMATRICES +
|
|
|
|
((u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31) * 3 + 9))
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bPosNormalMatrixChanged = true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-27 11:26:06 -06:00
|
|
|
if (((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4 + 12))
|
2010-06-04 19:38:22 -06:00
|
|
|
{
|
|
|
|
bTexMatricesChanged[0] = true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-27 11:26:06 -06:00
|
|
|
if (((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4 + 12) ||
|
|
|
|
((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4 &&
|
|
|
|
(u32)start < (u32)g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4 + 12))
|
2010-06-04 19:38:22 -06:00
|
|
|
{
|
|
|
|
bTexMatricesChanged[1] = true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (start < XFMEM_POSMATRICES_END)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
if (nTransformMatricesChanged[0] == -1)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
nTransformMatricesChanged[0] = start;
|
2015-02-15 12:43:31 -07:00
|
|
|
nTransformMatricesChanged[1] = end > XFMEM_POSMATRICES_END ? XFMEM_POSMATRICES_END : end;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
|
|
|
else
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nTransformMatricesChanged[0] > start)
|
|
|
|
nTransformMatricesChanged[0] = start;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nTransformMatricesChanged[1] < end)
|
|
|
|
nTransformMatricesChanged[1] = end > XFMEM_POSMATRICES_END ? XFMEM_POSMATRICES_END : end;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (start < XFMEM_NORMALMATRICES_END && end > XFMEM_NORMALMATRICES)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int _start = start < XFMEM_NORMALMATRICES ? 0 : start - XFMEM_NORMALMATRICES;
|
|
|
|
int _end = end < XFMEM_NORMALMATRICES_END ? end - XFMEM_NORMALMATRICES :
|
|
|
|
XFMEM_NORMALMATRICES_END - XFMEM_NORMALMATRICES;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (nNormalMatricesChanged[0] == -1)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
nNormalMatricesChanged[0] = _start;
|
|
|
|
nNormalMatricesChanged[1] = _end;
|
|
|
|
}
|
|
|
|
else
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nNormalMatricesChanged[0] > _start)
|
|
|
|
nNormalMatricesChanged[0] = _start;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nNormalMatricesChanged[1] < _end)
|
|
|
|
nNormalMatricesChanged[1] = _end;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (start < XFMEM_POSTMATRICES_END && end > XFMEM_POSTMATRICES)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int _start = start < XFMEM_POSTMATRICES ? XFMEM_POSTMATRICES : start - XFMEM_POSTMATRICES;
|
|
|
|
int _end = end < XFMEM_POSTMATRICES_END ? end - XFMEM_POSTMATRICES :
|
|
|
|
XFMEM_POSTMATRICES_END - XFMEM_POSTMATRICES;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (nPostTransformMatricesChanged[0] == -1)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
nPostTransformMatricesChanged[0] = _start;
|
|
|
|
nPostTransformMatricesChanged[1] = _end;
|
|
|
|
}
|
|
|
|
else
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nPostTransformMatricesChanged[0] > _start)
|
|
|
|
nPostTransformMatricesChanged[0] = _start;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nPostTransformMatricesChanged[1] < _end)
|
|
|
|
nPostTransformMatricesChanged[1] = _end;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2010-06-04 19:38:22 -06:00
|
|
|
if (start < XFMEM_LIGHTS_END && end > XFMEM_LIGHTS)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
int _start = start < XFMEM_LIGHTS ? XFMEM_LIGHTS : start - XFMEM_LIGHTS;
|
|
|
|
int _end = end < XFMEM_LIGHTS_END ? end - XFMEM_LIGHTS : XFMEM_LIGHTS_END - XFMEM_LIGHTS;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nLightsChanged[0] == -1)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
nLightsChanged[0] = _start;
|
|
|
|
nLightsChanged[1] = _end;
|
|
|
|
}
|
|
|
|
else
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nLightsChanged[0] > _start)
|
|
|
|
nLightsChanged[0] = _start;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-15 12:43:31 -07:00
|
|
|
if (nLightsChanged[1] < _end)
|
|
|
|
nLightsChanged[1] = _end;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2008-12-26 03:43:18 -07:00
|
|
|
void VertexShaderManager::SetTexMatrixChangedA(u32 Value)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2014-08-27 11:26:06 -06:00
|
|
|
if (g_main_cp_state.matrix_index_a.Hex != Value)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2016-08-21 21:02:37 -06:00
|
|
|
g_vertex_manager->Flush();
|
2015-02-15 12:43:31 -07:00
|
|
|
if (g_main_cp_state.matrix_index_a.PosNormalMtxIdx != (Value & 0x3f))
|
2010-06-04 19:38:22 -06:00
|
|
|
bPosNormalMatrixChanged = true;
|
|
|
|
bTexMatricesChanged[0] = true;
|
2014-08-27 11:26:06 -06:00
|
|
|
g_main_cp_state.matrix_index_a.Hex = Value;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2008-12-26 03:43:18 -07:00
|
|
|
void VertexShaderManager::SetTexMatrixChangedB(u32 Value)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2014-08-27 11:26:06 -06:00
|
|
|
if (g_main_cp_state.matrix_index_b.Hex != Value)
|
2009-07-26 03:52:35 -06:00
|
|
|
{
|
2016-08-21 21:02:37 -06:00
|
|
|
g_vertex_manager->Flush();
|
2010-06-04 19:38:22 -06:00
|
|
|
bTexMatricesChanged[1] = true;
|
2014-08-27 11:26:06 -06:00
|
|
|
g_main_cp_state.matrix_index_b.Hex = Value;
|
2010-06-04 19:38:22 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2008-12-26 03:43:18 -07:00
|
|
|
void VertexShaderManager::SetViewportChanged()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bViewportChanged = true;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2011-02-05 11:25:34 -07:00
|
|
|
void VertexShaderManager::SetProjectionChanged()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2010-06-04 19:38:22 -06:00
|
|
|
bProjectionChanged = true;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2015-08-16 19:07:10 -06:00
|
|
|
void VertexShaderManager::SetMaterialColorChanged(int index)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2014-10-21 18:42:55 -06:00
|
|
|
nMaterialsChanged[index] = true;
|
2009-02-08 15:08:20 -07:00
|
|
|
}
|
2009-04-19 04:10:45 -06:00
|
|
|
|
2017-07-19 23:25:27 -06:00
|
|
|
void VertexShaderManager::SetVertexFormat(u32 components)
|
|
|
|
{
|
|
|
|
if (components != constants.components)
|
|
|
|
{
|
|
|
|
constants.components = components;
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderManager::SetTexMatrixInfoChanged(int index)
|
|
|
|
{
|
|
|
|
// TODO: Should we track this with more precision, like which indices changed?
|
|
|
|
// The whole vertex constants are probably going to be uploaded regardless.
|
|
|
|
bTexMtxInfoChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderManager::SetLightingConfigChanged()
|
|
|
|
{
|
|
|
|
bLightingConfigChanged = true;
|
|
|
|
}
|
|
|
|
|
2015-01-24 13:12:52 -07:00
|
|
|
void VertexShaderManager::TransformToClipSpace(const float* data, float* out, u32 MtxIdx)
|
2014-12-25 00:34:22 -07:00
|
|
|
{
|
2015-09-20 23:16:21 -06:00
|
|
|
const float* world_matrix = &xfmem.posMatrices[(MtxIdx & 0x3f) * 4];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-20 23:16:21 -06:00
|
|
|
// We use the projection matrix calculated by VertexShaderManager, because it
|
2015-01-23 07:15:09 -07:00
|
|
|
// includes any free look transformations.
|
2015-11-01 14:54:41 -07:00
|
|
|
// Make sure VertexShaderManager::SetConstants() has been called first.
|
2015-01-24 13:12:52 -07:00
|
|
|
const float* proj_matrix = &g_fProjectionMatrix[0];
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-20 23:16:21 -06:00
|
|
|
const float t[3] = {data[0] * world_matrix[0] + data[1] * world_matrix[1] +
|
|
|
|
data[2] * world_matrix[2] + world_matrix[3],
|
|
|
|
data[0] * world_matrix[4] + data[1] * world_matrix[5] +
|
|
|
|
data[2] * world_matrix[6] + world_matrix[7],
|
|
|
|
data[0] * world_matrix[8] + data[1] * world_matrix[9] +
|
|
|
|
data[2] * world_matrix[10] + world_matrix[11]};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-25 00:34:22 -07:00
|
|
|
out[0] = t[0] * proj_matrix[0] + t[1] * proj_matrix[1] + t[2] * proj_matrix[2] + proj_matrix[3];
|
|
|
|
out[1] = t[0] * proj_matrix[4] + t[1] * proj_matrix[5] + t[2] * proj_matrix[6] + proj_matrix[7];
|
|
|
|
out[2] = t[0] * proj_matrix[8] + t[1] * proj_matrix[9] + t[2] * proj_matrix[10] + proj_matrix[11];
|
|
|
|
out[3] =
|
|
|
|
t[0] * proj_matrix[12] + t[1] * proj_matrix[13] + t[2] * proj_matrix[14] + proj_matrix[15];
|
|
|
|
}
|
|
|
|
|
2012-01-01 13:46:02 -07:00
|
|
|
void VertexShaderManager::DoState(PointerWrap& p)
|
|
|
|
{
|
2019-06-20 06:09:38 -06:00
|
|
|
p.DoArray(g_fProjectionMatrix);
|
2012-01-01 13:46:02 -07:00
|
|
|
p.Do(s_viewportCorrection);
|
2020-04-26 15:40:25 -06:00
|
|
|
g_freelook_camera.DoState(p);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-06-20 06:09:38 -06:00
|
|
|
p.DoArray(nTransformMatricesChanged);
|
|
|
|
p.DoArray(nNormalMatricesChanged);
|
|
|
|
p.DoArray(nPostTransformMatricesChanged);
|
|
|
|
p.DoArray(nLightsChanged);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-01-15 09:29:39 -07:00
|
|
|
p.Do(nMaterialsChanged);
|
2019-06-20 06:09:38 -06:00
|
|
|
p.DoArray(bTexMatricesChanged);
|
2015-01-15 09:29:39 -07:00
|
|
|
p.Do(bPosNormalMatrixChanged);
|
|
|
|
p.Do(bProjectionChanged);
|
|
|
|
p.Do(bViewportChanged);
|
2017-07-19 23:25:27 -06:00
|
|
|
p.Do(bTexMtxInfoChanged);
|
|
|
|
p.Do(bLightingConfigChanged);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-07 08:02:24 -06:00
|
|
|
p.Do(constants);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-01-01 13:46:02 -07:00
|
|
|
if (p.GetMode() == PointerWrap::MODE_READ)
|
|
|
|
{
|
|
|
|
Dirty();
|
|
|
|
}
|
|
|
|
}
|