Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Common/Math/lin/matrix4x4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void Matrix4x4::setViewFrame(const Vec3 &pos, const Vec3 &vRight, const Vec3 &vV
yx = vRight.y; yy = vUp.y; yz=vView.y; yw = 0.0f;
zx = vRight.z; zy = vUp.z; zz=vView.z; zw = 0.0f;

wx = -pos * vRight;
wy = -pos * vUp;
wz = -pos * vView;
wx = dot(-pos, vRight);
wy = dot(-pos, vUp);
wz = dot(-pos, vView);
ww = 1.0f;
}

Expand Down
1 change: 0 additions & 1 deletion Common/Math/lin/vec3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Vec3 Vec3::operator *(const Matrix4x4 &m) const {
x*m.xy + y*m.yy + z*m.zy + m.wy,
x*m.xz + y*m.yz + z*m.zz + m.wz);
}

Vec3 Vec3::rotatedBy(const Matrix4x4 &m) const {
return Vec3(x*m.xx + y*m.yx + z*m.zx,
x*m.xy + y*m.yy + z*m.zy,
Expand Down
21 changes: 8 additions & 13 deletions Common/Math/lin/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Vec3 {
x+=other.x; y+=other.y; z+=other.z;
}
Vec3 operator -(const Vec3 &v) const {
return Vec3(x-v.x,y-v.y,z-v.z);
return Vec3(x-v.x, y-v.y, z-v.z);
}
void operator -= (const Vec3 &other)
{
Expand All @@ -48,9 +48,8 @@ class Vec3 {
Vec3 operator -() const {
return Vec3(-x,-y,-z);
}

Vec3 operator * (const float f) const {
return Vec3(x*f,y*f,z*f);
Vec3 operator *(const float f) const {
return Vec3(x * f, y * f, z * f);
}
Vec3 operator / (const float f) const {
float invf = (1.0f/f);
Expand All @@ -60,9 +59,6 @@ class Vec3 {
{
*this = *this / f;
}
float operator * (const Vec3 &other) const {
return x*other.x + y*other.y + z*other.z;
}
void operator *= (const float f) {
*this = *this * f;
}
Expand All @@ -72,9 +68,6 @@ class Vec3 {
Vec3 scaledBy(const Vec3 &other) const {
return Vec3(x*other.x, y*other.y, z*other.z);
}
Vec3 scaledByInv(const Vec3 &other) const {
return Vec3(x/other.x, y/other.y, z/other.z);
}
Vec3 operator *(const Matrix4x4 &m) const;
void operator *=(const Matrix4x4 &m) {
*this = *this * m;
Expand All @@ -90,7 +83,7 @@ class Vec3 {
return sqrtf(length2());
}
void setLength(const float l) {
(*this) *= l/length();
(*this) *= l / length();
}
Vec3 withLength(const float l) const {
return (*this) * l / length();
Expand All @@ -116,11 +109,13 @@ class Vec3 {
return (*this)*(1-t) + other*t;
}
void setZero() {
memset((void *)this,0,sizeof(float)*3);
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
};

inline Vec3 operator * (const float f, const Vec3 &v) {return v * f;}
inline Vec3 operator * (const float f, const Vec3 &v) { return v * f; }

// In new code, prefer these to the operators.

Expand Down
14 changes: 4 additions & 10 deletions GPU/Common/ShaderUniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,10 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
}

if (dirtyUniforms & DIRTY_PROJTHROUGHMATRIX) {
Matrix4x4 proj_through;
proj_through.setOrthoVulkan(0.0f, gstate_c.curRTWidth, 0, gstate_c.curRTHeight, 0, 1);

// Negative RT offsets come from split framebuffers (Killzone)
if (gstate_c.curRTOffsetX < 0 || gstate_c.curRTOffsetY < 0) {
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;
}

CopyMatrix4x4(ub->proj_through, proj_through.getReadPtr());
ub->xywh[0] = (float)gstate_c.curRTOffsetX;
ub->xywh[1] = (float)gstate_c.curRTOffsetY;
ub->xywh[2] = (float)gstate_c.curRTWidth;
ub->xywh[3] = (float)gstate_c.curRTHeight;

ub->rotation = useBufferedRendering ? 0 : (float)g_display.rotation;
}
Expand Down
6 changes: 3 additions & 3 deletions GPU/Common/ShaderUniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum : uint64_t {
// Every line here is a 4-float.
struct alignas(16) UB_VS_FS_Base {
float proj[16];
float proj_through[16];
float xywh[4]; // later, we could invert w and h here to avoid division.
float view[12];
float world[12];
float tex[12];
Expand All @@ -43,11 +43,11 @@ struct alignas(16) UB_VS_FS_Base {
// VR stuff is to go here, later. For normal drawing, we can then get away
// with just uploading the first 448 bytes of the struct (up to and including fogCoef).
};
static_assert(sizeof(UB_VS_FS_Base) == 480, "UB_VS_FS_Base should be 480 bytes");
static_assert(sizeof(UB_VS_FS_Base) == 432, "UB_VS_FS_Base should be 432 bytes");

static const char * const ub_baseStr =
R"( mat4 u_proj;
mat4 u_proj_through;
vec4 u_xywh;
mat3x4 u_view;
mat3x4 u_world;
mat3x4 u_texmtx;
Expand Down
2 changes: 1 addition & 1 deletion GPU/Common/SoftwareTransformCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void SoftwareTransform::CalcCullParams(float &minZValue, float &maxZValue) const
maxZValue = 1.000030517578125f * gstate_c.vpDepthScale;
minZValue = -maxZValue;
// Scale and offset the Z appropriately, since we baked that into a projection transform.
if (params_.usesHalfZ) {
if (true) { // all backends are "use half z" now
maxZValue = maxZValue * 0.5f + 0.5f + gstate_c.vpZOffset * 0.5f;
minZValue = minZValue * 0.5f + 0.5f + gstate_c.vpZOffset * 0.5f;
} else {
Expand Down
2 changes: 0 additions & 2 deletions GPU/Common/SoftwareTransformCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ struct SoftwareTransformParams {
TextureCacheCommon *texCache;
bool allowClear;
bool allowSeparateAlphaClear;
bool flippedY;
bool usesHalfZ;
};

// Converts an index buffer to make the provoking vertex the last.
Expand Down
15 changes: 13 additions & 2 deletions GPU/Common/VertexShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
}

if (isModeThrough) {
WRITE(p, "uniform mat4 u_proj_through;\n");
WRITE(p, "uniform vec4 u_xywh;\n");
*uniformMask |= DIRTY_PROJTHROUGHMATRIX;
} else if (useHWTransform) {
if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) {
Expand Down Expand Up @@ -746,7 +746,12 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
WRITE(p, " %sv_fogdepth = fog;\n", compat.vsOutPrefix);
if (isModeThrough) {
// The proj_through matrix already has the rotation, if needed.
WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n");
// NOTE: In through mode, we can ignore W, it's always 1.0. However,
// this transform will later be applied in all modes.
WRITE(p, " vec4 outPos;\n");
WRITE(p, " outPos.xy = ((position.xy - u_xywh.xy * position.w) / u_xywh.zw) * 2.0 - 1.0;\n");
WRITE(p, " outPos.zw = position.zw;\n");
// WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n");
} else {
// The viewport is used in this case, so need to compensate for that.
if (gstate_c.Use(GPU_ROUND_DEPTH_TO_16BIT)) {
Expand Down Expand Up @@ -1294,6 +1299,12 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
WRITE(p, " %sgl_Position.y *= u_scaleY;\n", compat.vsOutPrefix);
}

if (compat.depthMinusOneToOne) {
// Convert from 0->1 to -1->1 depth range.
WRITE(p, " %sgl_Position.z = %sgl_Position.z * 2.0 - %sgl_Position.w;\n", compat.vsOutPrefix, compat.vsOutPrefix, compat.vsOutPrefix);
// The formula takes the z component of gl_Position, which is currently in the range [0, w] (where w is the homogeneous coordinate), and transforms it to the range [-w, w]. This is done by first multiplying by 2 to scale the range from [0, w] to [0, 2w], and then subtracting w to shift the range to [-w, w]. This effectively converts the depth range from 0->1 to -1->1 after perspective division (when gl_Position is divided by w).
}

if (needsZWHack) {
// See comment in thin3d_vulkan.cpp.
WRITE(p, " if (%sgl_Position.z == %sgl_Position.w) %sgl_Position.z *= 0.999999;\n",
Expand Down
2 changes: 0 additions & 2 deletions GPU/D3D11/DrawEngineD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,6 @@ void DrawEngineD3D11::Flush() {
params.texCache = textureCache_;
params.allowClear = true;
params.allowSeparateAlphaClear = false; // D3D11 doesn't support separate alpha clears
params.flippedY = false;
params.usesHalfZ = true;

if (gstate.getShadeMode() == GE_SHADE_FLAT) {
// We need to rotate the index buffer to simulate a different provoking vertex.
Expand Down
2 changes: 0 additions & 2 deletions GPU/GLES/DrawEngineGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ void DrawEngineGLES::Flush() {
params.texCache = textureCache_;
params.allowClear = true; // Clear in OpenGL respects scissor rects, so we'll use it.
params.allowSeparateAlphaClear = true;
params.flippedY = framebufferManager_->UseBufferedRendering();
params.usesHalfZ = false;

// We need correct viewport values in gstate_c already.
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
Expand Down
21 changes: 12 additions & 9 deletions GPU/GLES/ShaderManagerGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs,

queries.push_back({ &u_proj, "u_proj" });
queries.push_back({ &u_proj_lens, "u_proj_lens" });
queries.push_back({ &u_proj_through, "u_proj_through" });
queries.push_back({ &u_xywh, "u_xywh" });
queries.push_back({ &u_texenv, "u_texenv" });
queries.push_back({ &u_fogcolor, "u_fogcolor" });
queries.push_back({ &u_fogcoef, "u_fogcoef" });
Expand Down Expand Up @@ -308,9 +308,9 @@ static void SetMatrix4x3(GLRenderManager *render, GLint *uniform, const float *m
render->SetUniformM4x4(uniform, m4x4);
}

static inline void ConvertProjMatrixToGL(Matrix4x4 &in) {
const Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset);
const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale);
static void ConvertProjMatrixToZeroToOneDepth(Matrix4x4 &in) {
const Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
in.translateAndScale(trans, scale);
}

Expand Down Expand Up @@ -429,7 +429,7 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
UpdateVRParams(gstate.projMatrix);

FlipProjMatrix(vrProjection);
ConvertProjMatrixToGL(vrProjection);
ConvertProjMatrixToZeroToOneDepth(vrProjection);

render_->SetUniformM4x4(&u_proj_lens, vrProjection.m);
}
Expand All @@ -438,14 +438,17 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
memcpy(&flippedMatrix, gstate.projMatrix, 16 * sizeof(float));

FlipProjMatrix(flippedMatrix);
ConvertProjMatrixToGL(flippedMatrix);
ConvertProjMatrixToZeroToOneDepth(flippedMatrix);

render_->SetUniformM4x4(&u_proj, flippedMatrix.m);
}
if (dirty & DIRTY_PROJTHROUGHMATRIX) {
Matrix4x4 proj_through;
proj_through.setOrthoGL(0.0f, gstate_c.curRTWidth, 0.0f, gstate_c.curRTHeight, 0.0f, 1.0f);
render_->SetUniformM4x4(&u_proj_through, proj_through.getReadPtr());
float xywh[4];
xywh[0] = (float)gstate_c.curRTOffsetX;
xywh[1] = (float)gstate_c.curRTOffsetY;
xywh[2] = (float)gstate_c.curRTWidth;
xywh[3] = (float)gstate_c.curRTHeight;
SetFloatUniform4(render_, &u_xywh, xywh);
}
if (dirty & DIRTY_TEXENV) {
SetColorUniform3(render_, &u_texenv, gstate.texenvcolor);
Expand Down
2 changes: 1 addition & 1 deletion GPU/GLES/ShaderManagerGLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LinkedShader {
int u_tex;
int u_proj;
int u_proj_lens;
int u_proj_through;
int u_xywh;
int u_texenv;
int u_view;
int u_texmtx;
Expand Down
2 changes: 0 additions & 2 deletions GPU/Vulkan/DrawEngineVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,6 @@ void DrawEngineVulkan::Flush() {
IndexBufferProvokingLastToFirst(prim, inds, vertexCount);
}
}
params.flippedY = true;
params.usesHalfZ = true;

// We need to update the viewport early because it's checked for flipping in SoftwareTransform.
// We don't have a "DrawStateEarly" in vulkan, so...
Expand Down
Loading