diff --git a/src/graphics/Software.cpp b/src/graphics/Software.cpp index 21a99912..e92a72d1 100644 --- a/src/graphics/Software.cpp +++ b/src/graphics/Software.cpp @@ -369,7 +369,14 @@ inline ZunVec3 Software::ProjectToNDC(ZunVec3 vertex, ZunMatrix mv, ZunMatrix p, viewZ = clip.z; clip = p * clip; ZunVec3 ndc = {clip.x, clip.y, clip.z}; - if (clip.w != 0) + + // this is probably incorrect but at least it prevents background on stage 2 from breaking without adding complexity + if (clip.w <= 0) + { + W = 1.0f; + return ndc; + } + else { ndc /= clip.w; W = 1.0f / clip.w; @@ -568,7 +575,8 @@ void Software::Draw(PrimitiveType type, i32 start, i32 count) f32 depth; if (useDepthTest) { - depth = ((ndcZ * clipW) * 0.5f + 0.5f) * depthDif + depthNear; + depth = ZUN_MAX(ZUN_MIN(((ndcZ * clipW) * 0.5f + 0.5f) * depthDif + depthNear, depthFar), + depthNear); if (depthFunc == DEPTH_FUNC_LEQUAL && depth > depthBuffer[pixelCoord]) { continue;