2017-02-13 01:07:27 +00:00
|
|
|
#include <algorithm>
|
2018-09-10 07:27:16 +00:00
|
|
|
#include <cmath>
|
2017-02-13 01:07:27 +00:00
|
|
|
|
2017-02-08 16:35:41 +00:00
|
|
|
#include "ShaderUniforms.h"
|
2020-10-04 08:10:55 +00:00
|
|
|
#include "Common/System/Display.h"
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Data/Convert/SmallDataConvert.h"
|
|
|
|
#include "Common/Math/lin/matrix4x4.h"
|
|
|
|
#include "Common/Math/math_util.h"
|
|
|
|
#include "Common/Math/lin/vec3.h"
|
2017-02-08 16:35:41 +00:00
|
|
|
#include "GPU/GPUState.h"
|
2020-08-03 21:17:22 +00:00
|
|
|
#include "GPU/Common/FramebufferManagerCommon.h"
|
2017-11-11 10:36:26 +00:00
|
|
|
#include "GPU/Common/GPUStateUtils.h"
|
2017-02-08 16:35:41 +00:00
|
|
|
#include "GPU/Math3D.h"
|
|
|
|
|
2019-10-22 20:58:10 +00:00
|
|
|
using namespace Lin;
|
|
|
|
|
2017-02-10 10:25:24 +00:00
|
|
|
static void ConvertProjMatrixToVulkan(Matrix4x4 &in) {
|
2020-08-05 08:54:07 +00:00
|
|
|
const Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
|
2017-02-08 16:35:41 +00:00
|
|
|
const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
|
|
|
|
in.translateAndScale(trans, scale);
|
|
|
|
}
|
|
|
|
|
2017-02-10 10:25:24 +00:00
|
|
|
static void ConvertProjMatrixToD3D11(Matrix4x4 &in) {
|
2020-08-06 07:35:26 +00:00
|
|
|
const Vec3 trans(gstate_c.vpXOffset, -gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
|
2017-02-12 11:02:13 +00:00
|
|
|
const Vec3 scale(gstate_c.vpWidthScale, -gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
|
2017-02-10 10:25:24 +00:00
|
|
|
in.translateAndScale(trans, scale);
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:27:25 +00:00
|
|
|
void CalcCullRange(float minValues[4], float maxValues[4], bool flipViewport, bool hasNegZ) {
|
|
|
|
// Account for the projection viewport adjustment when viewport is too large.
|
|
|
|
auto reverseViewportX = [](float x) {
|
|
|
|
float pspViewport = (x - gstate.getViewportXCenter()) * (1.0f / gstate.getViewportXScale());
|
2019-03-02 18:48:01 +00:00
|
|
|
return (pspViewport * gstate_c.vpWidthScale) - gstate_c.vpXOffset;
|
2018-09-18 05:27:25 +00:00
|
|
|
};
|
|
|
|
auto reverseViewportY = [flipViewport](float y) {
|
|
|
|
float heightScale = gstate_c.vpHeightScale;
|
2019-03-02 18:57:49 +00:00
|
|
|
float yOffset = gstate_c.vpYOffset;
|
2018-09-18 05:27:25 +00:00
|
|
|
if (flipViewport) {
|
|
|
|
// For D3D11 and GLES non-buffered.
|
|
|
|
heightScale = -heightScale;
|
2019-03-02 18:57:49 +00:00
|
|
|
yOffset = -yOffset;
|
2018-09-18 05:27:25 +00:00
|
|
|
}
|
|
|
|
float pspViewport = (y - gstate.getViewportYCenter()) * (1.0f / gstate.getViewportYScale());
|
2019-03-02 18:57:49 +00:00
|
|
|
return (pspViewport * heightScale) - yOffset;
|
2018-09-18 05:27:25 +00:00
|
|
|
};
|
2021-09-11 23:54:25 +00:00
|
|
|
auto transformZ = [hasNegZ](float z) {
|
|
|
|
// Z culling ignores the viewport, so we just redo the projection matrix adjustments.
|
|
|
|
if (hasNegZ) {
|
|
|
|
return (z * gstate_c.vpDepthScale) + gstate_c.vpZOffset;
|
2019-02-10 17:58:01 +00:00
|
|
|
}
|
2021-09-11 23:54:25 +00:00
|
|
|
return (z * gstate_c.vpDepthScale * 0.5f) + gstate_c.vpZOffset * 0.5f + 0.5f;
|
2018-09-18 05:27:25 +00:00
|
|
|
};
|
|
|
|
auto sortPair = [](float a, float b) {
|
|
|
|
return a > b ? std::make_pair(b, a) : std::make_pair(a, b);
|
|
|
|
};
|
|
|
|
|
|
|
|
// The PSP seems to use 0.12.4 for X and Y, and 0.16.0 for Z.
|
|
|
|
// Any vertex outside this range (unless depth clamp enabled) is discarded.
|
|
|
|
auto x = sortPair(reverseViewportX(0.0f), reverseViewportX(4096.0f));
|
|
|
|
auto y = sortPair(reverseViewportY(0.0f), reverseViewportY(4096.0f));
|
2021-09-11 23:54:25 +00:00
|
|
|
auto z = sortPair(transformZ(-1.000030517578125f), transformZ(1.000030517578125f));
|
2018-09-18 05:27:25 +00:00
|
|
|
// Since we have space in w, use it to pass the depth clamp flag. We also pass NAN for w "discard".
|
|
|
|
float clampEnable = gstate.isDepthClampEnabled() ? 1.0f : 0.0f;
|
|
|
|
|
2019-03-13 10:39:08 +00:00
|
|
|
minValues[0] = x.first;
|
|
|
|
minValues[1] = y.first;
|
|
|
|
minValues[2] = z.first;
|
|
|
|
minValues[3] = clampEnable;
|
|
|
|
maxValues[0] = x.second;
|
|
|
|
maxValues[1] = y.second;
|
|
|
|
maxValues[2] = z.second;
|
|
|
|
maxValues[3] = NAN;
|
2018-09-18 05:27:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 18:03:07 +00:00
|
|
|
void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport, bool useBufferedRendering) {
|
2017-02-08 16:35:41 +00:00
|
|
|
if (dirtyUniforms & DIRTY_TEXENV) {
|
2022-10-06 08:40:58 +00:00
|
|
|
Uint8x3ToFloat3(ub->texEnvColor, gstate.texenvcolor);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_ALPHACOLORREF) {
|
2022-10-10 15:16:52 +00:00
|
|
|
ub->alphaColorRef = gstate.getColorTestRef() | ((gstate.getAlphaTestRef() & gstate.getAlphaTestMask()) << 24);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_ALPHACOLORMASK) {
|
2022-09-25 08:33:14 +00:00
|
|
|
ub->colorTestMask = gstate.getColorTestMask() | (gstate.getAlphaTestMask() << 24);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_FOGCOLOR) {
|
2022-10-10 15:16:52 +00:00
|
|
|
Uint8x3ToFloat3(ub->fogColor, gstate.fogcolor);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_SHADERBLEND) {
|
2022-10-06 08:40:58 +00:00
|
|
|
Uint8x3ToFloat3(ub->blendFixA, gstate.getFixA());
|
|
|
|
Uint8x3ToFloat3(ub->blendFixB, gstate.getFixB());
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_TEXCLAMP) {
|
|
|
|
const float invW = 1.0f / (float)gstate_c.curTextureWidth;
|
|
|
|
const float invH = 1.0f / (float)gstate_c.curTextureHeight;
|
|
|
|
const int w = gstate.getTextureWidth(0);
|
|
|
|
const int h = gstate.getTextureHeight(0);
|
|
|
|
const float widthFactor = (float)w * invW;
|
|
|
|
const float heightFactor = (float)h * invH;
|
|
|
|
|
|
|
|
// First wrap xy, then half texel xy (for clamp.)
|
|
|
|
ub->texClamp[0] = widthFactor;
|
|
|
|
ub->texClamp[1] = heightFactor;
|
|
|
|
ub->texClamp[2] = invW * 0.5f;
|
|
|
|
ub->texClamp[3] = invH * 0.5f;
|
|
|
|
ub->texClampOffset[0] = gstate_c.curTextureXOffset * invW;
|
|
|
|
ub->texClampOffset[1] = gstate_c.curTextureYOffset * invH;
|
2022-07-31 08:43:12 +00:00
|
|
|
}
|
2022-07-25 16:51:08 +00:00
|
|
|
|
2022-07-31 08:43:12 +00:00
|
|
|
if (dirtyUniforms & DIRTY_MIPBIAS) {
|
2022-07-25 19:13:56 +00:00
|
|
|
float mipBias = (float)gstate.getTexLevelOffset16() * (1.0 / 16.0f);
|
|
|
|
ub->mipBias = (mipBias + 0.5f) / (float)(gstate.getTextureMaxLevel() + 1);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirtyUniforms & DIRTY_PROJMATRIX) {
|
|
|
|
Matrix4x4 flippedMatrix;
|
|
|
|
memcpy(&flippedMatrix, gstate.projMatrix, 16 * sizeof(float));
|
|
|
|
|
|
|
|
const bool invertedY = gstate_c.vpHeight < 0;
|
|
|
|
if (invertedY) {
|
|
|
|
flippedMatrix[1] = -flippedMatrix[1];
|
|
|
|
flippedMatrix[5] = -flippedMatrix[5];
|
|
|
|
flippedMatrix[9] = -flippedMatrix[9];
|
|
|
|
flippedMatrix[13] = -flippedMatrix[13];
|
|
|
|
}
|
|
|
|
const bool invertedX = gstate_c.vpWidth < 0;
|
|
|
|
if (invertedX) {
|
|
|
|
flippedMatrix[0] = -flippedMatrix[0];
|
|
|
|
flippedMatrix[4] = -flippedMatrix[4];
|
|
|
|
flippedMatrix[8] = -flippedMatrix[8];
|
|
|
|
flippedMatrix[12] = -flippedMatrix[12];
|
|
|
|
}
|
2017-02-10 10:25:24 +00:00
|
|
|
if (flipViewport) {
|
|
|
|
ConvertProjMatrixToD3D11(flippedMatrix);
|
|
|
|
} else {
|
|
|
|
ConvertProjMatrixToVulkan(flippedMatrix);
|
|
|
|
}
|
2017-03-07 12:23:35 +00:00
|
|
|
|
2023-02-25 12:09:44 +00:00
|
|
|
if (!useBufferedRendering && g_display.rotation != DisplayRotation::ROTATE_0) {
|
|
|
|
flippedMatrix = flippedMatrix * g_display.rot_matrix;
|
2017-03-07 12:23:35 +00:00
|
|
|
}
|
2017-02-08 16:35:41 +00:00
|
|
|
CopyMatrix4x4(ub->proj, flippedMatrix.getReadPtr());
|
2022-12-16 12:03:44 +00:00
|
|
|
|
2023-02-25 12:09:44 +00:00
|
|
|
ub->rotation = useBufferedRendering ? 0 : (float)g_display.rotation;
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirtyUniforms & DIRTY_PROJTHROUGHMATRIX) {
|
|
|
|
Matrix4x4 proj_through;
|
2017-02-10 10:25:24 +00:00
|
|
|
if (flipViewport) {
|
|
|
|
proj_through.setOrthoD3D(0.0f, gstate_c.curRTWidth, gstate_c.curRTHeight, 0, 0, 1);
|
|
|
|
} else {
|
|
|
|
proj_through.setOrthoVulkan(0.0f, gstate_c.curRTWidth, 0, gstate_c.curRTHeight, 0, 1);
|
|
|
|
}
|
2023-02-25 12:09:44 +00:00
|
|
|
if (!useBufferedRendering && g_display.rotation != DisplayRotation::ROTATE_0) {
|
|
|
|
proj_through = proj_through * g_display.rot_matrix;
|
2017-03-07 12:23:35 +00:00
|
|
|
}
|
2022-08-31 06:44:15 +00:00
|
|
|
|
|
|
|
// Negative RT offsets come from split framebuffers (Killzone)
|
2022-08-30 23:40:53 +00:00
|
|
|
if (gstate_c.curRTOffsetX < 0 || gstate_c.curRTOffsetY < 0) {
|
2022-08-31 06:44:15 +00:00
|
|
|
proj_through.wx += 2.0f * (float)gstate_c.curRTOffsetX / (float)gstate_c.curRTWidth;
|
|
|
|
proj_through.wy += 2.0f * (float)gstate_c.curRTOffsetY / (float)gstate_c.curRTHeight;
|
2022-08-30 23:40:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:35:41 +00:00
|
|
|
CopyMatrix4x4(ub->proj_through, proj_through.getReadPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform
|
|
|
|
if (dirtyUniforms & DIRTY_WORLDMATRIX) {
|
2017-02-14 10:53:46 +00:00
|
|
|
ConvertMatrix4x3To3x4Transposed(ub->world, gstate.worldMatrix);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_VIEWMATRIX) {
|
2017-02-14 10:53:46 +00:00
|
|
|
ConvertMatrix4x3To3x4Transposed(ub->view, gstate.viewMatrix);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_TEXMATRIX) {
|
2017-02-14 10:53:46 +00:00
|
|
|
ConvertMatrix4x3To3x4Transposed(ub->tex, gstate.tgenMatrix);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 20:01:14 +00:00
|
|
|
if (dirtyUniforms & DIRTY_FOGCOEF) {
|
|
|
|
float fogcoef[2] = {
|
|
|
|
getFloat24(gstate.fog1),
|
|
|
|
getFloat24(gstate.fog2),
|
|
|
|
};
|
|
|
|
// The PSP just ignores infnan here (ignoring IEEE), so take it down to a valid float.
|
|
|
|
// Workaround for https://github.com/hrydgard/ppsspp/issues/5384#issuecomment-38365988
|
|
|
|
if (my_isnanorinf(fogcoef[0])) {
|
|
|
|
// Not really sure what a sensible value might be, but let's try 64k.
|
|
|
|
fogcoef[0] = std::signbit(fogcoef[0]) ? -65535.0f : 65535.0f;
|
|
|
|
}
|
|
|
|
if (my_isnanorinf(fogcoef[1])) {
|
|
|
|
fogcoef[1] = std::signbit(fogcoef[1]) ? -65535.0f : 65535.0f;
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
2023-05-08 20:01:14 +00:00
|
|
|
CopyFloat2(ub->fogCoef, fogcoef);
|
2017-12-07 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:05:42 +00:00
|
|
|
if (dirtyUniforms & DIRTY_TEX_ALPHA_MUL) {
|
2023-02-02 15:12:28 +00:00
|
|
|
bool doTextureAlpha = gstate.isTextureAlphaUsed();
|
|
|
|
if (gstate_c.textureFullAlpha && gstate.getTextureFunction() != GE_TEXFUNC_REPLACE) {
|
|
|
|
doTextureAlpha = false;
|
|
|
|
}
|
|
|
|
ub->texNoAlpha = doTextureAlpha ? 0.0f : 1.0f;
|
2023-01-05 16:05:42 +00:00
|
|
|
ub->texMul = gstate.isColorDoublingEnabled() ? 2.0f : 1.0f;
|
2023-01-09 10:19:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 20:08:34 +00:00
|
|
|
if (dirtyUniforms & DIRTY_STENCILREPLACEVALUE) {
|
2023-01-09 10:19:50 +00:00
|
|
|
ub->stencilReplaceValue = (float)gstate.getStencilTestRef() * (1.0 / 255.0);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note - this one is not in lighting but in transformCommon as it has uses beyond lighting
|
|
|
|
if (dirtyUniforms & DIRTY_MATAMBIENTALPHA) {
|
|
|
|
Uint8x3ToFloat4_AlphaUint8(ub->matAmbient, gstate.materialambient, gstate.getMaterialAmbientA());
|
|
|
|
}
|
|
|
|
|
2020-11-08 22:17:06 +00:00
|
|
|
if (dirtyUniforms & DIRTY_COLORWRITEMASK) {
|
|
|
|
ub->colorWriteMask = ~((gstate.pmska << 24) | (gstate.pmskc & 0xFFFFFF));
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:35:41 +00:00
|
|
|
// Texturing
|
|
|
|
if (dirtyUniforms & DIRTY_UVSCALEOFFSET) {
|
2023-06-15 09:57:30 +00:00
|
|
|
float widthFactor = 1.0f;
|
|
|
|
float heightFactor = 1.0f;
|
|
|
|
if (gstate_c.textureIsFramebuffer) {
|
|
|
|
const float invW = 1.0f / (float)gstate_c.curTextureWidth;
|
|
|
|
const float invH = 1.0f / (float)gstate_c.curTextureHeight;
|
|
|
|
const int w = gstate.getTextureWidth(0);
|
|
|
|
const int h = gstate.getTextureHeight(0);
|
|
|
|
widthFactor = (float)w * invW;
|
|
|
|
heightFactor = (float)h * invH;
|
|
|
|
}
|
2020-12-13 15:04:16 +00:00
|
|
|
if (gstate_c.submitType == SubmitType::HW_BEZIER || gstate_c.submitType == SubmitType::HW_SPLINE) {
|
2017-02-25 09:32:39 +00:00
|
|
|
// When we are generating UV coordinates through the bezier/spline, we need to apply the scaling.
|
|
|
|
// However, this is missing a check that we're not getting our UV:s supplied for us in the vertices.
|
|
|
|
ub->uvScaleOffset[0] = gstate_c.uv.uScale * widthFactor;
|
|
|
|
ub->uvScaleOffset[1] = gstate_c.uv.vScale * heightFactor;
|
|
|
|
ub->uvScaleOffset[2] = gstate_c.uv.uOff * widthFactor;
|
|
|
|
ub->uvScaleOffset[3] = gstate_c.uv.vOff * heightFactor;
|
|
|
|
} else {
|
|
|
|
ub->uvScaleOffset[0] = widthFactor;
|
|
|
|
ub->uvScaleOffset[1] = heightFactor;
|
|
|
|
ub->uvScaleOffset[2] = 0.0f;
|
|
|
|
ub->uvScaleOffset[3] = 0.0f;
|
|
|
|
}
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirtyUniforms & DIRTY_DEPTHRANGE) {
|
2017-11-11 10:36:26 +00:00
|
|
|
// Same formulas as D3D9 now. Should work for both Vulkan and D3D11.
|
|
|
|
|
|
|
|
// Depth is [0, 1] mapping to [minz, maxz], not too hard.
|
|
|
|
float vpZScale = gstate.getViewportZScale();
|
|
|
|
float vpZCenter = gstate.getViewportZCenter();
|
|
|
|
|
|
|
|
// These are just the reverse of the formulas in GPUStateUtils.
|
2023-12-12 17:48:22 +00:00
|
|
|
float halfActualZRange = InfToZero(gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f);
|
|
|
|
float inverseDepthScale = InfToZero(gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f);
|
|
|
|
|
2017-11-11 10:36:26 +00:00
|
|
|
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
|
|
|
|
float viewZScale = halfActualZRange * 2.0f;
|
2023-02-11 13:41:25 +00:00
|
|
|
float viewZCenter = minz;
|
2017-02-08 16:35:41 +00:00
|
|
|
|
|
|
|
ub->depthRange[0] = viewZScale;
|
|
|
|
ub->depthRange[1] = viewZCenter;
|
2021-09-11 23:54:25 +00:00
|
|
|
ub->depthRange[2] = gstate_c.vpZOffset * 0.5f + 0.5f;
|
2023-03-05 16:41:44 +00:00
|
|
|
ub->depthRange[3] = 2.0f * inverseDepthScale;
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
2017-02-25 09:32:39 +00:00
|
|
|
|
2018-09-17 06:57:20 +00:00
|
|
|
if (dirtyUniforms & DIRTY_CULLRANGE) {
|
2018-09-18 05:27:25 +00:00
|
|
|
CalcCullRange(ub->cullRangeMin, ub->cullRangeMax, flipViewport, false);
|
2018-09-17 06:57:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 02:51:44 +00:00
|
|
|
if (dirtyUniforms & DIRTY_BEZIERSPLINE) {
|
2018-09-22 10:20:30 +00:00
|
|
|
ub->spline_counts = gstate_c.spline_num_points_u;
|
2017-02-25 09:32:39 +00:00
|
|
|
}
|
2018-04-13 10:25:57 +00:00
|
|
|
|
|
|
|
if (dirtyUniforms & DIRTY_DEPAL) {
|
|
|
|
int indexMask = gstate.getClutIndexMask();
|
|
|
|
int indexShift = gstate.getClutIndexShift();
|
|
|
|
int indexOffset = gstate.getClutIndexStartPos() >> 4;
|
|
|
|
int format = gstate_c.depalFramebufferFormat;
|
|
|
|
uint32_t val = BytesToUint32(indexMask, indexShift, indexOffset, format);
|
|
|
|
// Poke in a bilinear filter flag in the top bit.
|
2023-08-17 18:46:43 +00:00
|
|
|
if (gstate.isMagnifyFilteringEnabled())
|
|
|
|
val |= 0x80000000;
|
2018-04-13 10:25:57 +00:00
|
|
|
ub->depal_mask_shift_off_fmt = val;
|
|
|
|
}
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 21:39:35 +00:00
|
|
|
// For "light ubershader" bits.
|
2024-07-24 17:45:42 +00:00
|
|
|
// TODO: We pack these bits even when not using ubershader lighting. Maybe not bother.
|
2022-09-25 16:30:27 +00:00
|
|
|
uint32_t PackLightControlBits() {
|
|
|
|
// Bit organization
|
|
|
|
// Bottom 4 bits are enable bits for each light.
|
|
|
|
// Then, for each light, comes 2 bits for "comp" and 2 bits for "type".
|
2022-09-26 09:20:17 +00:00
|
|
|
// At the end, at bit 20, we put the three material update bits.
|
|
|
|
|
2022-09-25 16:30:27 +00:00
|
|
|
uint32_t lightControl = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (gstate.isLightChanEnabled(i)) {
|
|
|
|
lightControl |= 1 << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 computation = (u32)gstate.getLightComputation(i); // 2 bits
|
|
|
|
u32 type = (u32)gstate.getLightType(i); // 2 bits
|
2023-01-11 17:07:06 +00:00
|
|
|
if (type == 3) { type = 0; } // Don't want to handle this degenerate case in the shader.
|
2022-09-25 16:30:27 +00:00
|
|
|
lightControl |= computation << (4 + i * 4);
|
|
|
|
lightControl |= type << (4 + i * 4 + 2);
|
|
|
|
}
|
2022-09-26 09:20:17 +00:00
|
|
|
|
2022-12-30 21:39:35 +00:00
|
|
|
// Material update is 3 bits.
|
2022-09-26 09:20:17 +00:00
|
|
|
lightControl |= gstate.getMaterialUpdate() << 20;
|
2022-09-25 16:30:27 +00:00
|
|
|
return lightControl;
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:35:41 +00:00
|
|
|
void LightUpdateUniforms(UB_VS_Lights *ub, uint64_t dirtyUniforms) {
|
|
|
|
// Lighting
|
|
|
|
if (dirtyUniforms & DIRTY_AMBIENT) {
|
|
|
|
Uint8x3ToFloat4_AlphaUint8(ub->ambientColor, gstate.ambientcolor, gstate.getAmbientA());
|
|
|
|
}
|
|
|
|
if (dirtyUniforms & DIRTY_MATDIFFUSE) {
|
|
|
|
Uint8x3ToFloat4(ub->materialDiffuse, gstate.materialdiffuse);
|
|
|
|
}
|
2017-02-13 01:07:27 +00:00
|
|
|
if (dirtyUniforms & DIRTY_MATSPECULAR) {
|
|
|
|
Uint8x3ToFloat4_Alpha(ub->materialSpecular, gstate.materialspecular, std::max(0.0f, getFloat24(gstate.materialspecularcoef)));
|
|
|
|
}
|
2017-02-08 16:35:41 +00:00
|
|
|
if (dirtyUniforms & DIRTY_MATEMISSIVE) {
|
2022-09-25 11:59:52 +00:00
|
|
|
// We're not touching the fourth f32 here, because we store an u32 of control bits in it.
|
2022-10-06 08:40:58 +00:00
|
|
|
Uint8x3ToFloat3(ub->materialEmissive, gstate.materialemissive);
|
2017-02-08 16:35:41 +00:00
|
|
|
}
|
2022-09-25 11:59:52 +00:00
|
|
|
if (dirtyUniforms & DIRTY_LIGHT_CONTROL) {
|
2022-09-25 16:30:27 +00:00
|
|
|
ub->lightControl = PackLightControlBits();
|
2022-09-25 11:59:52 +00:00
|
|
|
}
|
2017-02-08 16:35:41 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (dirtyUniforms & (DIRTY_LIGHT0 << i)) {
|
|
|
|
if (gstate.isDirectionalLight(i)) {
|
|
|
|
// Prenormalize
|
2023-01-11 16:45:55 +00:00
|
|
|
ExpandFloat24x3ToFloat4AndNormalize(ub->lpos[i], &gstate.lpos[i * 3]);
|
2017-02-08 16:35:41 +00:00
|
|
|
} else {
|
|
|
|
ExpandFloat24x3ToFloat4(ub->lpos[i], &gstate.lpos[i * 3]);
|
|
|
|
}
|
2023-01-11 16:45:55 +00:00
|
|
|
// ldir is only used for spotlights. Prenormalize it.
|
|
|
|
ExpandFloat24x3ToFloat4AndNormalize(ub->ldir[i], &gstate.ldir[i * 3]);
|
2017-02-08 16:35:41 +00:00
|
|
|
ExpandFloat24x3ToFloat4(ub->latt[i], &gstate.latt[i * 3]);
|
2018-03-12 10:07:51 +00:00
|
|
|
float lightAngle_spotCoef[2] = { getFloat24(gstate.lcutoff[i]), getFloat24(gstate.lconv[i]) };
|
|
|
|
CopyFloat2To4(ub->lightAngle_SpotCoef[i], lightAngle_spotCoef);
|
2017-02-08 16:35:41 +00:00
|
|
|
Uint8x3ToFloat4(ub->lightAmbient[i], gstate.lcolor[i * 3]);
|
|
|
|
Uint8x3ToFloat4(ub->lightDiffuse[i], gstate.lcolor[i * 3 + 1]);
|
|
|
|
Uint8x3ToFloat4(ub->lightSpecular[i], gstate.lcolor[i * 3 + 2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 10:22:02 +00:00
|
|
|
|
|
|
|
void BoneUpdateUniforms(UB_VS_Bones *ub, uint64_t dirtyUniforms) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (dirtyUniforms & (DIRTY_BONEMATRIX0 << i)) {
|
|
|
|
ConvertMatrix4x3To3x4Transposed(ub->bones[i], gstate.boneMatrix + 12 * i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|