diff --git a/GPU/Software/Lighting.cpp b/GPU/Software/Lighting.cpp index 35a1dde62a..0f11c2c24a 100644 --- a/GPU/Software/Lighting.cpp +++ b/GPU/Software/Lighting.cpp @@ -62,12 +62,16 @@ void GenerateLightST(VertexData &vertex) { void Process(VertexData& vertex, bool hasColor) { const int materialupdate = gstate.materialupdate & (hasColor ? 7 : 0); - Vec3 vcol0 = vertex.color0.rgb().Cast() * Vec3::AssignToAll(1.0f / 255.0f); - Vec3 mec = Vec3::FromRGB(gstate.getMaterialEmissive()); + Vec4 mec = Vec4::FromRGBA(gstate.getMaterialEmissive()); - Vec3 mac = (materialupdate & 1) ? vcol0 : Vec3::FromRGB(gstate.getMaterialAmbientRGBA()); - Vec3 final_color = mec + mac * Vec3::FromRGB(gstate.getAmbientRGBA()); - Vec3 specular_color(0.0f, 0.0f, 0.0f); + Vec4 mac = (materialupdate & 1) ? vertex.color0 : Vec4::FromRGBA(gstate.getMaterialAmbientRGBA()); + Vec4 ac = Vec4::FromRGBA(gstate.getAmbientRGBA()); + // Ambient (whether vertex or material) rounds using the half offset method (like alpha blend.) + const Vec4 ones = Vec4::AssignToAll(1); + Vec4 ambient = ((mac * 2 + ones) * (ac * 2 + ones)) / 1024; + + Vec4 final_color = mec + ambient; + Vec4 specular_color = Vec4::AssignToAll(0); for (unsigned int light = 0; light < 4; ++light) { if (!gstate.isLightChanEnabled(light)) @@ -103,13 +107,14 @@ void Process(VertexData& vertex, bool hasColor) { } // ambient lighting - Vec3 lac = Vec3::FromRGB(gstate.getLightAmbientColor(light)); - final_color += lac * mac * att * spot; + int attspot = (int)ceilf(256 * 2 * att * spot + 1); + if (attspot > 512) + attspot = 512; + Vec4 lac = Vec4::FromRGBA(gstate.getLightAmbientColor(light)); + Vec4 lambient = ((mac * 2 + ones) * (lac * 2 + ones) * attspot) / (1024 * 512); + final_color += lambient; // diffuse lighting - Vec3 ldc = Vec3::FromRGB(gstate.getDiffuseColor(light)); - Vec3 mdc = (materialupdate & 2) ? vcol0 : Vec3::FromRGB(gstate.getMaterialDiffuse()); - float diffuse_factor = Dot(L, vertex.worldnormal); if (gstate.isUsingPoweredDiffuseLight(light)) { float k = gstate.getMaterialSpecularCoef(); @@ -117,35 +122,39 @@ void Process(VertexData& vertex, bool hasColor) { } if (diffuse_factor > 0.f) { - final_color += ldc * mdc * diffuse_factor * att * spot; + int diffuse_attspot = (int)ceilf(attspot * diffuse_factor + 1); + if (diffuse_attspot > 512) + diffuse_attspot = 512; + Vec4 ldc = Vec4::FromRGBA(gstate.getDiffuseColor(light)); + Vec4 mdc = (materialupdate & 2) ? vertex.color0 : Vec4::FromRGBA(gstate.getMaterialDiffuse()); + Vec4 ldiffuse = ((ldc * 2 + ones) * (mdc * 2 + ones) * diffuse_attspot) / (1024 * 512); + final_color += ldiffuse; } if (gstate.isUsingSpecularLight(light) && diffuse_factor >= 0.0f) { Vec3 H = L + Vec3(0.f, 0.f, 1.f); - Vec3 lsc = Vec3::FromRGB(gstate.getSpecularColor(light)); - Vec3 msc = (materialupdate & 4) ? vcol0 : Vec3::FromRGB(gstate.getMaterialSpecular()); - float specular_factor = Dot(H.NormalizedOr001(cpu_info.bSSE4_1), vertex.worldnormal); float k = gstate.getMaterialSpecularCoef(); specular_factor = pspLightPow(specular_factor, k); if (specular_factor > 0.f) { - specular_color += lsc * msc * specular_factor * att * spot; + int specular_attspot = (int)ceilf(attspot * specular_factor + 1); + if (specular_attspot > 512) + specular_attspot = 512; + Vec4 lsc = Vec4::FromRGBA(gstate.getSpecularColor(light)); + Vec4 msc = (materialupdate & 4) ? vertex.color0 : Vec4::FromRGBA(gstate.getMaterialSpecular()); + Vec4 lspecular = ((lsc * 2 + ones) * (msc * 2 + ones) * specular_attspot) / (1024 * 512); + specular_color += lspecular; } } } - int maa = (materialupdate & 1) ? vertex.color0.a() : gstate.getMaterialAmbientA(); - int final_alpha = (gstate.getAmbientA() * maa) / 255; - if (gstate.isUsingSecondaryColor()) { - Vec3 final_color_int = (final_color.Clamp(0.0f, 1.0f) * 255.0f).Cast(); - vertex.color0 = Vec4(final_color_int, final_alpha); - vertex.color1 = (specular_color.Clamp(0.0f, 1.0f) * 255.0f).Cast(); + vertex.color0 = final_color.Clamp(0, 255); + vertex.color1 = specular_color.Clamp(0, 255).rgb(); } else { - Vec3 final_color_int = ((final_color + specular_color).Clamp(0.0f, 1.0f) * 255.0f).Cast(); - vertex.color0 = Vec4(final_color_int, final_alpha); + vertex.color0 = (final_color + specular_color).Clamp(0, 255); } }