2013-06-25 14:15:09 +00:00
|
|
|
// Copyright (c) 2013- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#include "ppsspp_config.h"
|
2017-05-14 21:15:59 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
|
2021-05-01 14:15:04 +00:00
|
|
|
#include "Common/Data/Convert/ColorConv.h"
|
2020-10-04 08:04:01 +00:00
|
|
|
#include "Common/Profiler/Profiler.h"
|
2020-11-27 23:12:06 +00:00
|
|
|
#include "Common/Thread/ParallelLoop.h"
|
2020-10-05 18:58:33 +00:00
|
|
|
#include "Core/ThreadPools.h"
|
2014-01-19 00:39:46 +00:00
|
|
|
#include "Core/Config.h"
|
2013-09-01 08:10:12 +00:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/Reporting.h"
|
2020-11-27 23:12:06 +00:00
|
|
|
#include "Core/ThreadPools.h"
|
2013-09-01 08:10:12 +00:00
|
|
|
#include "GPU/GPUState.h"
|
2013-06-25 14:15:09 +00:00
|
|
|
|
2013-09-16 04:27:13 +00:00
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
2021-11-21 15:20:19 +00:00
|
|
|
#include "GPU/Software/DrawPixel.h"
|
2013-09-01 08:10:12 +00:00
|
|
|
#include "GPU/Software/Rasterizer.h"
|
2017-05-10 02:48:05 +00:00
|
|
|
#include "GPU/Software/Sampler.h"
|
2021-11-21 15:20:19 +00:00
|
|
|
#include "GPU/Software/SoftGpu.h"
|
2013-06-25 14:15:09 +00:00
|
|
|
|
2014-03-16 21:29:22 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
#include <emmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2013-06-25 14:15:09 +00:00
|
|
|
namespace Rasterizer {
|
|
|
|
|
2017-04-23 17:39:58 +00:00
|
|
|
// Only OK on x64 where our stack is aligned
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 17:39:58 +00:00
|
|
|
static inline __m128 Interpolate(const __m128 &c0, const __m128 &c1, const __m128 &c2, int w0, int w1, int w2, float wsum) {
|
|
|
|
__m128 v = _mm_mul_ps(c0, _mm_cvtepi32_ps(_mm_set1_epi32(w0)));
|
|
|
|
v = _mm_add_ps(v, _mm_mul_ps(c1, _mm_cvtepi32_ps(_mm_set1_epi32(w1))));
|
|
|
|
v = _mm_add_ps(v, _mm_mul_ps(c2, _mm_cvtepi32_ps(_mm_set1_epi32(w2))));
|
|
|
|
return _mm_mul_ps(v, _mm_set_ps1(wsum));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline __m128i Interpolate(const __m128i &c0, const __m128i &c1, const __m128i &c2, int w0, int w1, int w2, float wsum) {
|
|
|
|
return _mm_cvtps_epi32(Interpolate(_mm_cvtepi32_ps(c0), _mm_cvtepi32_ps(c1), _mm_cvtepi32_ps(c2), w0, w1, w2, wsum));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// NOTE: When not casting color0 and color1 to float vectors, this code suffers from severe overflow issues.
|
|
|
|
// Not sure if that should be regarded as a bug or if casting to float is a valid fix.
|
|
|
|
|
|
|
|
static inline Vec4<int> Interpolate(const Vec4<int> &c0, const Vec4<int> &c1, const Vec4<int> &c2, int w0, int w1, int w2, float wsum) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 17:39:58 +00:00
|
|
|
return Vec4<int>(Interpolate(c0.ivec, c1.ivec, c2.ivec, w0, w1, w2, wsum));
|
|
|
|
#else
|
|
|
|
return ((c0.Cast<float>() * w0 + c1.Cast<float>() * w1 + c2.Cast<float>() * w2) * wsum).Cast<int>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vec3<int> Interpolate(const Vec3<int> &c0, const Vec3<int> &c1, const Vec3<int> &c2, int w0, int w1, int w2, float wsum) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 17:39:58 +00:00
|
|
|
return Vec3<int>(Interpolate(c0.ivec, c1.ivec, c2.ivec, w0, w1, w2, wsum));
|
|
|
|
#else
|
|
|
|
return ((c0.Cast<float>() * w0 + c1.Cast<float>() * w1 + c2.Cast<float>() * w2) * wsum).Cast<int>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vec2<float> Interpolate(const Vec2<float> &c0, const Vec2<float> &c1, const Vec2<float> &c2, int w0, int w1, int w2, float wsum) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 17:39:58 +00:00
|
|
|
return Vec2<float>(Interpolate(c0.vec, c1.vec, c2.vec, w0, w1, w2, wsum));
|
|
|
|
#else
|
|
|
|
return (c0 * w0 + c1 * w1 + c2 * w2) * wsum;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vec4<float> Interpolate(const float &c0, const float &c1, const float &c2, const Vec4<float> &w0, const Vec4<float> &w1, const Vec4<float> &w2, const Vec4<float> &wsum_recip) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 17:39:58 +00:00
|
|
|
__m128 v = _mm_mul_ps(w0.vec, _mm_set1_ps(c0));
|
|
|
|
v = _mm_add_ps(v, _mm_mul_ps(w1.vec, _mm_set1_ps(c1)));
|
|
|
|
v = _mm_add_ps(v, _mm_mul_ps(w2.vec, _mm_set1_ps(c2)));
|
|
|
|
return _mm_mul_ps(v, wsum_recip.vec);
|
|
|
|
#else
|
|
|
|
return (w0 * c0 + w1 * c1 + w2 * c2) * wsum_recip;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vec4<float> Interpolate(const float &c0, const float &c1, const float &c2, const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<float> &wsum_recip) {
|
|
|
|
return Interpolate(c0, c1, c2, w0.Cast<float>(), w1.Cast<float>(), w2.Cast<float>(), wsum_recip);
|
|
|
|
}
|
|
|
|
|
2015-06-11 14:01:17 +00:00
|
|
|
static inline u8 ClampFogDepth(float fogdepth) {
|
2021-11-21 04:54:52 +00:00
|
|
|
union FloatBits {
|
|
|
|
float f;
|
|
|
|
u32 u;
|
|
|
|
};
|
|
|
|
FloatBits f;
|
|
|
|
f.f = fogdepth;
|
|
|
|
|
|
|
|
u32 exp = f.u >> 23;
|
|
|
|
if ((f.u & 0x80000000) != 0 || exp <= 126 - 8)
|
2015-06-11 14:01:17 +00:00
|
|
|
return 0;
|
2021-11-21 04:54:52 +00:00
|
|
|
if (exp > 126)
|
2015-06-11 14:01:17 +00:00
|
|
|
return 255;
|
2021-11-21 04:54:52 +00:00
|
|
|
|
|
|
|
u32 mantissa = (f.u & 0x007FFFFF) | 0x00800000;
|
|
|
|
return mantissa >> (16 + 126 - exp);
|
2015-06-11 14:01:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-07 21:14:07 +00:00
|
|
|
static inline int ClampUV(int v, int height) {
|
|
|
|
if (v >= height - 1)
|
|
|
|
return height - 1;
|
|
|
|
else if (v < 0)
|
|
|
|
return 0;
|
|
|
|
return v;
|
|
|
|
}
|
2013-12-09 00:44:23 +00:00
|
|
|
|
2017-05-07 21:14:07 +00:00
|
|
|
static inline int WrapUV(int v, int height) {
|
|
|
|
return v & (height - 1);
|
|
|
|
}
|
2013-07-24 10:40:57 +00:00
|
|
|
|
2017-05-07 21:14:07 +00:00
|
|
|
template <int N>
|
|
|
|
static inline void ApplyTexelClamp(int out_u[N], int out_v[N], const int u[N], const int v[N], int width, int height) {
|
2013-07-24 10:40:57 +00:00
|
|
|
if (gstate.isTexCoordClampedS()) {
|
2017-05-07 21:14:07 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
out_u[i] = ClampUV(u[i], width);
|
|
|
|
}
|
2013-07-24 10:40:57 +00:00
|
|
|
} else {
|
2017-05-07 21:14:07 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
out_u[i] = WrapUV(u[i], width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gstate.isTexCoordClampedT()) {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
out_v[i] = ClampUV(v[i], height);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
out_v[i] = WrapUV(v[i], height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int N>
|
|
|
|
static inline void ApplyTexelClampQuad(int out_u[N * 4], int out_v[N * 4], const int u[N], const int v[N], int width, int height) {
|
|
|
|
if (gstate.isTexCoordClampedS()) {
|
|
|
|
for (int i = 0; i < N * 4; ++i) {
|
|
|
|
out_u[i] = ClampUV(u[i >> 2] + (i & 1), width);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < N * 4; ++i) {
|
|
|
|
out_u[i] = WrapUV(u[i >> 2] + (i & 1), width);
|
|
|
|
}
|
2013-07-24 10:40:57 +00:00
|
|
|
}
|
|
|
|
if (gstate.isTexCoordClampedT()) {
|
2017-05-07 21:14:07 +00:00
|
|
|
for (int i = 0; i < N * 4; ++i) {
|
|
|
|
out_v[i] = ClampUV(v[i >> 2] + ((i >> 1) & 1), height);
|
|
|
|
}
|
2013-07-24 10:40:57 +00:00
|
|
|
} else {
|
2017-05-07 21:14:07 +00:00
|
|
|
for (int i = 0; i < N * 4; ++i) {
|
|
|
|
out_v[i] = WrapUV(v[i >> 2] + ((i >> 1) & 1), height);
|
|
|
|
}
|
2013-07-24 10:40:57 +00:00
|
|
|
}
|
2017-05-07 21:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void GetTexelCoordinates(int level, float s, float t, int& out_u, int& out_v)
|
|
|
|
{
|
|
|
|
int width = gstate.getTextureWidth(level);
|
|
|
|
int height = gstate.getTextureHeight(level);
|
|
|
|
|
2017-05-08 13:49:01 +00:00
|
|
|
int base_u = (int)(s * width * 256.0f + 0.375f);
|
|
|
|
int base_v = (int)(t * height * 256.0f + 0.375f);
|
2013-06-25 17:36:16 +00:00
|
|
|
|
2017-05-08 13:49:01 +00:00
|
|
|
base_u >>= 8;
|
|
|
|
base_v >>= 8;
|
|
|
|
|
|
|
|
ApplyTexelClamp<1>(&out_u, &out_v, &base_u, &base_v, width, height);
|
2013-12-09 00:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void GetTexelCoordinatesQuad(int level, float in_s, float in_t, int u[4], int v[4], int &frac_u, int &frac_v)
|
|
|
|
{
|
|
|
|
// 8 bits of fractional UV
|
2015-12-21 01:03:09 +00:00
|
|
|
int width = gstate.getTextureWidth(level);
|
|
|
|
int height = gstate.getTextureHeight(level);
|
2013-06-25 17:36:16 +00:00
|
|
|
|
2017-05-07 21:41:21 +00:00
|
|
|
int base_u = (int)(in_s * width * 256.0f + 0.375f) - 128;
|
|
|
|
int base_v = (int)(in_t * height * 256.0f + 0.375f) - 128;
|
2013-12-09 00:44:23 +00:00
|
|
|
|
|
|
|
frac_u = (int)(base_u) & 0xff;
|
|
|
|
frac_v = (int)(base_v) & 0xff;
|
2013-09-07 10:34:19 +00:00
|
|
|
|
2013-12-09 00:44:23 +00:00
|
|
|
base_u >>= 8;
|
|
|
|
base_v >>= 8;
|
|
|
|
|
|
|
|
// Need to generate and individually wrap/clamp the four sample coordinates. Ugh.
|
2017-05-07 21:14:07 +00:00
|
|
|
ApplyTexelClampQuad<1>(u, v, &base_u, &base_v, width, height);
|
2013-07-24 10:40:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 01:42:43 +00:00
|
|
|
static inline void GetTextureCoordinates(const VertexData& v0, const VertexData& v1, const float p, float &s, float &t) {
|
|
|
|
switch (gstate.getUVGenMode()) {
|
|
|
|
case GE_TEXMAP_TEXTURE_COORDS:
|
|
|
|
case GE_TEXMAP_UNKNOWN:
|
|
|
|
case GE_TEXMAP_ENVIRONMENT_MAP:
|
2018-11-26 03:19:11 +00:00
|
|
|
case GE_TEXMAP_TEXTURE_MATRIX:
|
2017-05-27 01:42:43 +00:00
|
|
|
{
|
|
|
|
// TODO: What happens if vertex has no texture coordinates?
|
|
|
|
// Note that for environment mapping, texture coordinates have been calculated during lighting
|
|
|
|
float q0 = 1.f / v0.clippos.w;
|
|
|
|
float q1 = 1.f / v1.clippos.w;
|
|
|
|
float wq0 = p * q0;
|
|
|
|
float wq1 = (1.0f - p) * q1;
|
|
|
|
|
|
|
|
float q_recip = 1.0f / (wq0 + wq1);
|
|
|
|
s = (v0.texturecoords.s() * wq0 + v1.texturecoords.s() * wq1) * q_recip;
|
|
|
|
t = (v0.texturecoords.t() * wq0 + v1.texturecoords.t() * wq1) * q_recip;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR_LOG_REPORT(G3D, "Software: Unsupported texture mapping mode %x!", gstate.getUVGenMode());
|
|
|
|
s = 0.0f;
|
|
|
|
t = 0.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:39:58 +00:00
|
|
|
static inline void GetTextureCoordinates(const VertexData& v0, const VertexData& v1, const VertexData& v2, const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<float> &wsum_recip, Vec4<float> &s, Vec4<float> &t)
|
2013-07-24 13:44:23 +00:00
|
|
|
{
|
2013-08-27 08:26:01 +00:00
|
|
|
switch (gstate.getUVGenMode()) {
|
2013-08-27 08:15:53 +00:00
|
|
|
case GE_TEXMAP_TEXTURE_COORDS:
|
|
|
|
case GE_TEXMAP_UNKNOWN:
|
|
|
|
case GE_TEXMAP_ENVIRONMENT_MAP:
|
2018-11-26 03:19:11 +00:00
|
|
|
case GE_TEXMAP_TEXTURE_MATRIX:
|
2013-08-27 08:15:53 +00:00
|
|
|
{
|
|
|
|
// TODO: What happens if vertex has no texture coordinates?
|
|
|
|
// Note that for environment mapping, texture coordinates have been calculated during lighting
|
|
|
|
float q0 = 1.f / v0.clippos.w;
|
|
|
|
float q1 = 1.f / v1.clippos.w;
|
|
|
|
float q2 = 1.f / v2.clippos.w;
|
2017-04-23 17:39:58 +00:00
|
|
|
Vec4<float> wq0 = w0.Cast<float>() * q0;
|
|
|
|
Vec4<float> wq1 = w1.Cast<float>() * q1;
|
|
|
|
Vec4<float> wq2 = w2.Cast<float>() * q2;
|
|
|
|
|
|
|
|
Vec4<float> q_recip = (wq0 + wq1 + wq2).Reciprocal();
|
|
|
|
s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), wq0, wq1, wq2, q_recip);
|
|
|
|
t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), wq0, wq1, wq2, q_recip);
|
2013-08-27 08:15:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-01 08:10:12 +00:00
|
|
|
ERROR_LOG_REPORT(G3D, "Software: Unsupported texture mapping mode %x!", gstate.getUVGenMode());
|
2017-04-23 17:39:58 +00:00
|
|
|
s = Vec4<float>::AssignToAll(0.0f);
|
|
|
|
t = Vec4<float>::AssignToAll(0.0f);
|
2013-08-27 08:15:53 +00:00
|
|
|
break;
|
2017-05-27 01:42:43 +00:00
|
|
|
}
|
2013-07-24 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 21:41:21 +00:00
|
|
|
static inline void SetPixelDepth(int x, int y, u16 value)
|
|
|
|
{
|
2013-09-14 18:36:56 +00:00
|
|
|
depthbuf.Set16(x, y, gstate.DepthBufStride(), value);
|
2013-06-29 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
static inline u8 GetPixelStencil(GEBufferFormat fmt, int x, int y) {
|
|
|
|
if (fmt == GE_FORMAT_565) {
|
2013-10-13 05:18:08 +00:00
|
|
|
// Always treated as 0 for comparison purposes.
|
2013-07-27 12:51:39 +00:00
|
|
|
return 0;
|
2021-11-20 22:22:55 +00:00
|
|
|
} else if (fmt == GE_FORMAT_5551) {
|
2013-09-14 18:36:56 +00:00
|
|
|
return ((fb.Get16(x, y, gstate.FrameBufStride()) & 0x8000) != 0) ? 0xFF : 0;
|
2021-11-20 22:22:55 +00:00
|
|
|
} else if (fmt == GE_FORMAT_4444) {
|
2013-11-10 10:26:10 +00:00
|
|
|
return Convert4To8(fb.Get16(x, y, gstate.FrameBufStride()) >> 12);
|
2013-07-27 12:51:39 +00:00
|
|
|
} else {
|
2013-10-07 08:00:43 +00:00
|
|
|
return fb.Get32(x, y, gstate.FrameBufStride()) >> 24;
|
2013-07-27 12:51:39 +00:00
|
|
|
}
|
2013-07-19 16:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 02:52:17 +00:00
|
|
|
static inline bool IsRightSideOrFlatBottomLine(const Vec2<int>& vertex, const Vec2<int>& line1, const Vec2<int>& line2)
|
2013-07-12 15:06:39 +00:00
|
|
|
{
|
|
|
|
if (line1.y == line2.y) {
|
|
|
|
// just check if vertex is above us => bottom line parallel to x-axis
|
|
|
|
return vertex.y < line1.y;
|
|
|
|
} else {
|
|
|
|
// check if vertex is on our left => right side
|
2017-04-16 02:52:17 +00:00
|
|
|
return vertex.x < line1.x + (line2.x - line1.x) * (vertex.y - line1.y) / (line2.y - line1.y);
|
2013-07-12 15:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 08:33:30 +00:00
|
|
|
Vec4<int> GetTextureFunctionOutput(const Vec4<int>& prim_color, const Vec4<int>& texcolor)
|
2013-07-21 05:06:55 +00:00
|
|
|
{
|
|
|
|
Vec3<int> out_rgb;
|
|
|
|
int out_a;
|
|
|
|
|
2013-08-24 18:10:56 +00:00
|
|
|
bool rgba = gstate.isTextureAlphaUsed();
|
2013-07-21 05:06:55 +00:00
|
|
|
|
|
|
|
switch (gstate.getTextureFunction()) {
|
|
|
|
case GE_TEXFUNC_MODULATE:
|
2014-03-17 01:28:06 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
// We can be accurate up to 24 bit integers, should be enough.
|
|
|
|
const __m128 p = _mm_cvtepi32_ps(prim_color.ivec);
|
|
|
|
const __m128 t = _mm_cvtepi32_ps(texcolor.ivec);
|
2018-09-10 03:09:48 +00:00
|
|
|
const __m128 b = _mm_mul_ps(p, t);
|
|
|
|
if (gstate.isColorDoublingEnabled()) {
|
|
|
|
// We double right here, only for modulate. Other tex funcs do not color double.
|
2019-03-17 02:40:33 +00:00
|
|
|
const __m128 doubleColor = _mm_setr_ps(2.0f / 255.0f, 2.0f / 255.0f, 2.0f / 255.0f, 1.0f / 255.0f);
|
|
|
|
out_rgb.ivec = _mm_cvtps_epi32(_mm_mul_ps(b, doubleColor));
|
2018-09-10 03:09:48 +00:00
|
|
|
} else {
|
|
|
|
out_rgb.ivec = _mm_cvtps_epi32(_mm_mul_ps(b, _mm_set_ps1(1.0f / 255.0f)));
|
|
|
|
}
|
2014-03-17 01:28:06 +00:00
|
|
|
|
|
|
|
if (rgba) {
|
|
|
|
return Vec4<int>(out_rgb.ivec);
|
|
|
|
} else {
|
|
|
|
out_a = prim_color.a();
|
|
|
|
}
|
|
|
|
#else
|
2018-09-10 03:09:48 +00:00
|
|
|
if (gstate.isColorDoublingEnabled()) {
|
|
|
|
out_rgb = (prim_color.rgb() * texcolor.rgb() * 2) / 255;
|
|
|
|
} else {
|
|
|
|
out_rgb = prim_color.rgb() * texcolor.rgb() / 255;
|
|
|
|
}
|
2014-03-16 22:04:41 +00:00
|
|
|
out_a = (rgba) ? (prim_color.a() * texcolor.a() / 255) : prim_color.a();
|
2014-03-17 01:28:06 +00:00
|
|
|
#endif
|
2013-07-21 05:06:55 +00:00
|
|
|
break;
|
2014-03-17 01:28:06 +00:00
|
|
|
}
|
2013-07-21 05:06:55 +00:00
|
|
|
|
|
|
|
case GE_TEXFUNC_DECAL:
|
|
|
|
{
|
|
|
|
int t = (rgba) ? texcolor.a() : 255;
|
|
|
|
int invt = (rgba) ? 255 - t : 0;
|
2014-03-16 22:04:41 +00:00
|
|
|
out_rgb = (prim_color.rgb() * invt + texcolor.rgb() * t) / 255;
|
|
|
|
out_a = prim_color.a();
|
2013-07-21 05:06:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GE_TEXFUNC_BLEND:
|
|
|
|
{
|
|
|
|
const Vec3<int> const255(255, 255, 255);
|
|
|
|
const Vec3<int> texenv(gstate.getTextureEnvColR(), gstate.getTextureEnvColG(), gstate.getTextureEnvColB());
|
2014-03-16 22:04:41 +00:00
|
|
|
out_rgb = ((const255 - texcolor.rgb()) * prim_color.rgb() + texcolor.rgb() * texenv) / 255;
|
|
|
|
out_a = prim_color.a() * ((rgba) ? texcolor.a() : 255) / 255;
|
2013-07-21 05:06:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GE_TEXFUNC_REPLACE:
|
|
|
|
out_rgb = texcolor.rgb();
|
2014-03-16 22:04:41 +00:00
|
|
|
out_a = (rgba) ? texcolor.a() : prim_color.a();
|
2013-07-21 05:06:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GE_TEXFUNC_ADD:
|
2014-03-16 22:04:41 +00:00
|
|
|
out_rgb = prim_color.rgb() + texcolor.rgb();
|
2013-07-21 05:06:55 +00:00
|
|
|
if (out_rgb.r() > 255) out_rgb.r() = 255;
|
|
|
|
if (out_rgb.g() > 255) out_rgb.g() = 255;
|
|
|
|
if (out_rgb.b() > 255) out_rgb.b() = 255;
|
2014-03-16 22:04:41 +00:00
|
|
|
out_a = prim_color.a() * ((rgba) ? texcolor.a() : 255) / 255;
|
2013-07-21 05:06:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-09-01 08:10:12 +00:00
|
|
|
ERROR_LOG_REPORT(G3D, "Software: Unknown texture function %x", gstate.getTextureFunction());
|
2013-09-01 08:05:35 +00:00
|
|
|
out_rgb = Vec3<int>::AssignToAll(0);
|
|
|
|
out_a = 0;
|
2013-07-21 05:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Vec4<int>(out_rgb.r(), out_rgb.g(), out_rgb.b(), out_a);
|
|
|
|
}
|
2013-07-24 08:42:04 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
static inline Vec3<int> GetSourceFactor(GEBlendSrcFactor factor, const Vec4<int> &source, const Vec4<int> &dst) {
|
|
|
|
switch (factor) {
|
2013-07-24 13:44:23 +00:00
|
|
|
case GE_SRCBLEND_DSTCOLOR:
|
|
|
|
return dst.rgb();
|
|
|
|
|
|
|
|
case GE_SRCBLEND_INVDSTCOLOR:
|
|
|
|
return Vec3<int>::AssignToAll(255) - dst.rgb();
|
|
|
|
|
|
|
|
case GE_SRCBLEND_SRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
return Vec3<int>(_mm_shuffle_epi32(source.ivec, _MM_SHUFFLE(3, 3, 3, 3)));
|
|
|
|
#else
|
|
|
|
return Vec3<int>::AssignToAll(source.a());
|
|
|
|
#endif
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_SRCBLEND_INVSRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
return Vec3<int>(_mm_sub_epi32(_mm_set1_epi32(255), _mm_shuffle_epi32(source.ivec, _MM_SHUFFLE(3, 3, 3, 3))));
|
|
|
|
#else
|
|
|
|
return Vec3<int>::AssignToAll(255 - source.a());
|
|
|
|
#endif
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_SRCBLEND_DSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(dst.a());
|
|
|
|
|
|
|
|
case GE_SRCBLEND_INVDSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(255 - dst.a());
|
|
|
|
|
|
|
|
case GE_SRCBLEND_DOUBLESRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
return Vec3<int>::AssignToAll(2 * source.a());
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_SRCBLEND_DOUBLEINVSRCALPHA:
|
2015-11-19 14:48:37 +00:00
|
|
|
return Vec3<int>::AssignToAll(255 - std::min(2 * source.a(), 255));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_SRCBLEND_DOUBLEDSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(2 * dst.a());
|
|
|
|
|
|
|
|
case GE_SRCBLEND_DOUBLEINVDSTALPHA:
|
2015-11-19 14:48:37 +00:00
|
|
|
return Vec3<int>::AssignToAll(255 - std::min(2 * dst.a(), 255));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_SRCBLEND_FIXA:
|
|
|
|
default:
|
2015-11-19 14:48:06 +00:00
|
|
|
// All other dest factors (> 10) are treated as FIXA.
|
|
|
|
return Vec3<int>::FromRGB(gstate.getFixA());
|
2013-07-24 13:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
static inline Vec3<int> GetDestFactor(GEBlendDstFactor factor, const Vec4<int> &source, const Vec4<int> &dst) {
|
|
|
|
switch (factor) {
|
2013-07-24 13:44:23 +00:00
|
|
|
case GE_DSTBLEND_SRCCOLOR:
|
2014-03-17 01:57:11 +00:00
|
|
|
return source.rgb();
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_INVSRCCOLOR:
|
2014-03-17 01:57:11 +00:00
|
|
|
return Vec3<int>::AssignToAll(255) - source.rgb();
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_SRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
return Vec3<int>(_mm_shuffle_epi32(source.ivec, _MM_SHUFFLE(3, 3, 3, 3)));
|
|
|
|
#else
|
|
|
|
return Vec3<int>::AssignToAll(source.a());
|
|
|
|
#endif
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_INVSRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
return Vec3<int>(_mm_sub_epi32(_mm_set1_epi32(255), _mm_shuffle_epi32(source.ivec, _MM_SHUFFLE(3, 3, 3, 3))));
|
|
|
|
#else
|
|
|
|
return Vec3<int>::AssignToAll(255 - source.a());
|
|
|
|
#endif
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_DSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(dst.a());
|
|
|
|
|
|
|
|
case GE_DSTBLEND_INVDSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(255 - dst.a());
|
|
|
|
|
|
|
|
case GE_DSTBLEND_DOUBLESRCALPHA:
|
2014-03-17 01:57:11 +00:00
|
|
|
return Vec3<int>::AssignToAll(2 * source.a());
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_DOUBLEINVSRCALPHA:
|
2015-11-19 14:48:37 +00:00
|
|
|
return Vec3<int>::AssignToAll(255 - std::min(2 * source.a(), 255));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_DOUBLEDSTALPHA:
|
|
|
|
return Vec3<int>::AssignToAll(2 * dst.a());
|
|
|
|
|
|
|
|
case GE_DSTBLEND_DOUBLEINVDSTALPHA:
|
2015-11-19 14:48:37 +00:00
|
|
|
return Vec3<int>::AssignToAll(255 - std::min(2 * dst.a(), 255));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_DSTBLEND_FIXB:
|
|
|
|
default:
|
2015-11-19 14:48:06 +00:00
|
|
|
// All other dest factors (> 10) are treated as FIXB.
|
|
|
|
return Vec3<int>::FromRGB(gstate.getFixB());
|
2013-07-24 13:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 08:33:30 +00:00
|
|
|
// Removed inline here - it was never chosen to be inlined by the compiler anyway, too complex.
|
2021-11-20 22:22:55 +00:00
|
|
|
Vec3<int> AlphaBlendingResult(const PixelFuncID &pixelID, const Vec4<int> &source, const Vec4<int> &dst)
|
2013-07-24 13:44:23 +00:00
|
|
|
{
|
2015-11-19 14:48:37 +00:00
|
|
|
// Note: These factors cannot go below 0, but they can go above 255 when doubling.
|
2021-11-20 22:22:55 +00:00
|
|
|
Vec3<int> srcfactor = GetSourceFactor(GEBlendSrcFactor(pixelID.alphaBlendSrc), source, dst);
|
|
|
|
Vec3<int> dstfactor = GetDestFactor(GEBlendDstFactor(pixelID.alphaBlendDst), source, dst);
|
2013-07-24 13:44:23 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
switch (GEBlendMode(pixelID.alphaBlendEq)) {
|
2013-07-24 13:44:23 +00:00
|
|
|
case GE_BLENDMODE_MUL_AND_ADD:
|
2014-03-17 01:57:11 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
const __m128 s = _mm_mul_ps(_mm_cvtepi32_ps(source.ivec), _mm_cvtepi32_ps(srcfactor.ivec));
|
|
|
|
const __m128 d = _mm_mul_ps(_mm_cvtepi32_ps(dst.ivec), _mm_cvtepi32_ps(dstfactor.ivec));
|
2014-11-17 20:20:22 +00:00
|
|
|
return Vec3<int>(_mm_cvtps_epi32(_mm_mul_ps(_mm_add_ps(s, d), _mm_set_ps1(1.0f / 255.0f))));
|
2014-03-17 01:57:11 +00:00
|
|
|
#else
|
2014-03-16 22:04:41 +00:00
|
|
|
return (source.rgb() * srcfactor + dst.rgb() * dstfactor) / 255;
|
2014-03-17 01:57:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_BLENDMODE_MUL_AND_SUBTRACT:
|
2014-03-17 01:57:11 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
const __m128 s = _mm_mul_ps(_mm_cvtepi32_ps(source.ivec), _mm_cvtepi32_ps(srcfactor.ivec));
|
|
|
|
const __m128 d = _mm_mul_ps(_mm_cvtepi32_ps(dst.ivec), _mm_cvtepi32_ps(dstfactor.ivec));
|
2014-11-17 20:20:22 +00:00
|
|
|
return Vec3<int>(_mm_cvtps_epi32(_mm_mul_ps(_mm_sub_ps(s, d), _mm_set_ps1(1.0f / 255.0f))));
|
2014-03-17 01:57:11 +00:00
|
|
|
#else
|
2014-03-16 22:04:41 +00:00
|
|
|
return (source.rgb() * srcfactor - dst.rgb() * dstfactor) / 255;
|
2014-03-17 01:57:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_BLENDMODE_MUL_AND_SUBTRACT_REVERSE:
|
2014-03-17 01:57:11 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
const __m128 s = _mm_mul_ps(_mm_cvtepi32_ps(source.ivec), _mm_cvtepi32_ps(srcfactor.ivec));
|
|
|
|
const __m128 d = _mm_mul_ps(_mm_cvtepi32_ps(dst.ivec), _mm_cvtepi32_ps(dstfactor.ivec));
|
2014-11-17 20:20:22 +00:00
|
|
|
return Vec3<int>(_mm_cvtps_epi32(_mm_mul_ps(_mm_sub_ps(d, s), _mm_set_ps1(1.0f / 255.0f))));
|
2014-03-17 01:57:11 +00:00
|
|
|
#else
|
2014-03-16 22:04:41 +00:00
|
|
|
return (dst.rgb() * dstfactor - source.rgb() * srcfactor) / 255;
|
2014-03-17 01:57:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_BLENDMODE_MIN:
|
2014-03-16 22:04:41 +00:00
|
|
|
return Vec3<int>(std::min(source.r(), dst.r()),
|
|
|
|
std::min(source.g(), dst.g()),
|
|
|
|
std::min(source.b(), dst.b()));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_BLENDMODE_MAX:
|
2014-03-16 22:04:41 +00:00
|
|
|
return Vec3<int>(std::max(source.r(), dst.r()),
|
|
|
|
std::max(source.g(), dst.g()),
|
|
|
|
std::max(source.b(), dst.b()));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
case GE_BLENDMODE_ABSDIFF:
|
2014-03-16 22:04:41 +00:00
|
|
|
return Vec3<int>(::abs(source.r() - dst.r()),
|
|
|
|
::abs(source.g() - dst.g()),
|
|
|
|
::abs(source.b() - dst.b()));
|
2013-07-24 13:44:23 +00:00
|
|
|
|
|
|
|
default:
|
2021-11-20 22:22:55 +00:00
|
|
|
ERROR_LOG_REPORT(G3D, "Software: Unknown blend function %x", pixelID.alphaBlendEq);
|
2013-07-24 13:44:23 +00:00
|
|
|
return Vec3<int>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
static inline void ApplyTexturing(Sampler::Funcs sampler, Vec4<int> &prim_color, float s, float t, int texlevel, int frac_texlevel, bool bilinear, u8 *texptr[], int texbufw[]) {
|
2017-05-08 13:49:01 +00:00
|
|
|
int u[8] = {0}, v[8] = {0}; // 1.23.8 fixed point
|
|
|
|
int frac_u[2], frac_v[2];
|
2014-01-19 04:10:42 +00:00
|
|
|
|
2017-05-08 13:49:01 +00:00
|
|
|
Vec4<int> texcolor0;
|
|
|
|
Vec4<int> texcolor1;
|
|
|
|
const u8 *tptr0 = texptr[texlevel];
|
2017-05-11 01:03:35 +00:00
|
|
|
int bufw0 = texbufw[texlevel];
|
2017-05-08 13:49:01 +00:00
|
|
|
const u8 *tptr1 = texptr[texlevel + 1];
|
2017-05-11 01:03:35 +00:00
|
|
|
int bufw1 = texbufw[texlevel + 1];
|
2014-01-19 04:10:42 +00:00
|
|
|
|
2017-05-08 13:49:01 +00:00
|
|
|
if (!bilinear) {
|
|
|
|
// Nearest filtering only. Round texcoords.
|
|
|
|
GetTexelCoordinates(texlevel, s, t, u[0], v[0]);
|
|
|
|
if (frac_texlevel) {
|
|
|
|
GetTexelCoordinates(texlevel + 1, s, t, u[1], v[1]);
|
|
|
|
}
|
2014-01-19 04:10:42 +00:00
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
texcolor0 = Vec4<int>::FromRGBA(sampler.nearest(u[0], v[0], tptr0, bufw0, texlevel));
|
2017-05-08 13:49:01 +00:00
|
|
|
if (frac_texlevel) {
|
2017-05-21 23:16:03 +00:00
|
|
|
texcolor1 = Vec4<int>::FromRGBA(sampler.nearest(u[1], v[1], tptr1, bufw1, texlevel + 1));
|
2014-01-19 04:10:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-08 13:49:01 +00:00
|
|
|
GetTexelCoordinatesQuad(texlevel, s, t, u, v, frac_u[0], frac_v[0]);
|
|
|
|
if (frac_texlevel) {
|
|
|
|
GetTexelCoordinatesQuad(texlevel + 1, s, t, u + 4, v + 4, frac_u[1], frac_v[1]);
|
|
|
|
}
|
2014-01-19 04:10:42 +00:00
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
texcolor0 = Vec4<int>::FromRGBA(sampler.linear(u, v, frac_u[0], frac_v[0], tptr0, bufw0, texlevel));
|
2017-05-08 13:49:01 +00:00
|
|
|
if (frac_texlevel) {
|
2017-05-21 23:16:03 +00:00
|
|
|
texcolor1 = Vec4<int>::FromRGBA(sampler.linear(u + 4, v + 4, frac_u[1], frac_v[1], tptr1, bufw1, texlevel + 1));
|
2014-01-19 04:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:49:01 +00:00
|
|
|
if (frac_texlevel) {
|
|
|
|
texcolor0 = (texcolor1 * frac_texlevel + texcolor0 * (256 - frac_texlevel)) / 256;
|
|
|
|
}
|
|
|
|
prim_color = GetTextureFunctionOutput(prim_color, texcolor0);
|
|
|
|
}
|
2014-03-16 21:33:42 +00:00
|
|
|
|
2017-05-09 03:43:02 +00:00
|
|
|
// Produces a signed 1.23.8 value.
|
|
|
|
static int TexLog2(float delta) {
|
|
|
|
union FloatBits {
|
|
|
|
float f;
|
|
|
|
u32 u;
|
|
|
|
};
|
|
|
|
FloatBits f;
|
|
|
|
f.f = delta;
|
|
|
|
// Use the exponent as the tex level, and the top mantissa bits for a frac.
|
|
|
|
// We can't support more than 8 bits of frac, so truncate.
|
|
|
|
int useful = (f.u >> 15) & 0xFFFF;
|
|
|
|
// Now offset so the exponent aligns with log2f (exp=127 is 0.)
|
|
|
|
return useful - 127 * 256;
|
|
|
|
}
|
|
|
|
|
2017-05-20 20:02:27 +00:00
|
|
|
static inline void CalculateSamplingParams(const float ds, const float dt, const int maxTexLevel, int &level, int &levelFrac, bool &filt) {
|
2017-05-14 22:22:34 +00:00
|
|
|
const int width = gstate.getTextureWidth(0);
|
|
|
|
const int height = gstate.getTextureHeight(0);
|
2017-05-13 14:53:39 +00:00
|
|
|
|
2017-05-09 03:43:02 +00:00
|
|
|
// With 8 bits of fraction (because texslope can be fairly precise.)
|
|
|
|
int detail;
|
2017-05-08 13:49:01 +00:00
|
|
|
switch (gstate.getTexLevelMode()) {
|
|
|
|
case GE_TEXLEVEL_MODE_AUTO:
|
2017-05-13 14:53:39 +00:00
|
|
|
detail = TexLog2(std::max(ds * width, dt * height));
|
2017-05-08 13:49:01 +00:00
|
|
|
break;
|
|
|
|
case GE_TEXLEVEL_MODE_SLOPE:
|
2017-05-09 03:43:02 +00:00
|
|
|
// This is always offset by an extra texlevel.
|
|
|
|
detail = 1 * 256 + TexLog2(gstate.getTextureLodSlope());
|
2017-05-08 13:49:01 +00:00
|
|
|
break;
|
|
|
|
case GE_TEXLEVEL_MODE_CONST:
|
|
|
|
default:
|
2017-05-13 03:01:08 +00:00
|
|
|
// Unused value 3 operates the same as CONST.
|
2017-05-09 03:43:02 +00:00
|
|
|
detail = 0;
|
2017-05-08 13:49:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-03-16 21:33:42 +00:00
|
|
|
|
2017-05-09 03:43:02 +00:00
|
|
|
// Add in the bias (used in all modes), expanding to 8 bits of fraction.
|
2017-05-20 20:02:27 +00:00
|
|
|
detail += gstate.getTexLevelOffset16() << 4;
|
2017-05-08 13:49:01 +00:00
|
|
|
|
2017-05-09 03:43:02 +00:00
|
|
|
if (detail > 0 && maxTexLevel > 0) {
|
2017-05-20 20:02:27 +00:00
|
|
|
bool mipFilt = gstate.isMipmapFilteringEnabled();
|
|
|
|
|
2017-05-09 03:43:02 +00:00
|
|
|
int level8 = std::min(detail, maxTexLevel * 256);
|
2017-05-08 13:49:01 +00:00
|
|
|
if (!mipFilt) {
|
|
|
|
// Round up at 1.5.
|
|
|
|
level8 += 128;
|
|
|
|
}
|
|
|
|
level = level8 >> 8;
|
|
|
|
levelFrac = mipFilt ? level8 & 0xFF : 0;
|
2017-05-14 22:22:34 +00:00
|
|
|
} else {
|
|
|
|
level = 0;
|
|
|
|
levelFrac = 0;
|
2017-05-08 13:49:01 +00:00
|
|
|
}
|
2017-05-14 22:22:34 +00:00
|
|
|
|
2020-09-13 13:41:51 +00:00
|
|
|
if (g_Config.iTexFiltering == TEX_FILTER_FORCE_LINEAR) {
|
2017-05-20 20:02:27 +00:00
|
|
|
filt = true;
|
2020-09-13 13:41:51 +00:00
|
|
|
} else if (g_Config.iTexFiltering == TEX_FILTER_FORCE_NEAREST) {
|
2017-05-20 20:02:27 +00:00
|
|
|
filt = false;
|
2017-05-14 22:22:34 +00:00
|
|
|
} else {
|
2017-05-20 20:02:27 +00:00
|
|
|
filt = detail > 0 ? gstate.isMinifyFilteringEnabled() : gstate.isMagnifyFilteringEnabled();
|
2017-05-08 13:49:01 +00:00
|
|
|
}
|
2017-05-14 22:22:34 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
static inline void ApplyTexturing(Sampler::Funcs sampler, Vec4<int> *prim_color, const Vec4<float> &s, const Vec4<float> &t, int maxTexLevel, u8 *texptr[], int texbufw[]) {
|
2017-05-14 22:22:34 +00:00
|
|
|
float ds = s[1] - s[0];
|
|
|
|
float dt = t[2] - t[0];
|
|
|
|
|
|
|
|
int level;
|
|
|
|
int levelFrac;
|
2017-05-20 20:02:27 +00:00
|
|
|
bool bilinear;
|
|
|
|
CalculateSamplingParams(ds, dt, maxTexLevel, level, levelFrac, bilinear);
|
2017-05-08 13:49:01 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2017-05-11 00:31:34 +00:00
|
|
|
ApplyTexturing(sampler, prim_color[i], s[i], t[i], level, levelFrac, bilinear, texptr, texbufw);
|
2014-01-19 04:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 14:18:32 +00:00
|
|
|
struct TriangleEdge {
|
|
|
|
Vec4<int> Start(const ScreenCoords &v0, const ScreenCoords &v1, const ScreenCoords &origin);
|
|
|
|
inline Vec4<int> StepX(const Vec4<int> &w);
|
|
|
|
inline Vec4<int> StepY(const Vec4<int> &w);
|
|
|
|
|
|
|
|
Vec4<int> stepX;
|
|
|
|
Vec4<int> stepY;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vec4<int> TriangleEdge::Start(const ScreenCoords &v0, const ScreenCoords &v1, const ScreenCoords &origin) {
|
2017-05-13 15:03:08 +00:00
|
|
|
// Start at pixel centers.
|
|
|
|
Vec4<int> initX = Vec4<int>::AssignToAll(origin.x) + Vec4<int>(7, 23, 7, 23);
|
|
|
|
Vec4<int> initY = Vec4<int>::AssignToAll(origin.y) + Vec4<int>(7, 7, 23, 23);
|
2017-04-23 14:18:32 +00:00
|
|
|
|
|
|
|
// orient2d refactored.
|
|
|
|
int xf = v0.y - v1.y;
|
|
|
|
int yf = v1.x - v0.x;
|
|
|
|
int c = v1.y * v0.x - v1.x * v0.y;
|
|
|
|
|
|
|
|
stepX = Vec4<int>::AssignToAll(xf * 16 * 2);
|
|
|
|
stepY = Vec4<int>::AssignToAll(yf * 16 * 2);
|
|
|
|
|
|
|
|
return Vec4<int>::AssignToAll(xf) * initX + Vec4<int>::AssignToAll(yf) * initY + Vec4<int>::AssignToAll(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Vec4<int> TriangleEdge::StepX(const Vec4<int> &w) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-04-23 14:19:50 +00:00
|
|
|
return _mm_add_epi32(w.ivec, stepX.ivec);
|
|
|
|
#else
|
2017-04-23 14:18:32 +00:00
|
|
|
return w + stepX;
|
2017-04-23 14:19:50 +00:00
|
|
|
#endif
|
2017-04-23 14:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Vec4<int> TriangleEdge::StepY(const Vec4<int> &w) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-05-07 21:58:15 +00:00
|
|
|
return _mm_add_epi32(w.ivec, stepY.ivec);
|
|
|
|
#else
|
2017-04-23 14:18:32 +00:00
|
|
|
return w + stepY;
|
2017-05-07 21:58:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:40:06 +00:00
|
|
|
static inline Vec4<int> MakeMask(const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<int> &bias0, const Vec4<int> &bias1, const Vec4<int> &bias2, const Vec4<int> &scissor) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-05-07 21:58:15 +00:00
|
|
|
__m128i biased0 = _mm_add_epi32(w0.ivec, bias0.ivec);
|
|
|
|
__m128i biased1 = _mm_add_epi32(w1.ivec, bias1.ivec);
|
|
|
|
__m128i biased2 = _mm_add_epi32(w2.ivec, bias2.ivec);
|
|
|
|
|
2017-05-14 14:40:06 +00:00
|
|
|
return _mm_or_si128(_mm_or_si128(biased0, _mm_or_si128(biased1, biased2)), scissor.ivec);
|
2017-05-07 21:58:15 +00:00
|
|
|
#else
|
2017-05-14 14:40:06 +00:00
|
|
|
return (w0 + bias0) | (w1 + bias1) | (w2 + bias2) | scissor;
|
2017-05-07 21:58:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:33 +00:00
|
|
|
static inline bool AnyMask(const Vec4<int> &mask) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-05-07 21:58:15 +00:00
|
|
|
// In other words: !(mask.x < 0 && mask.y < 0 && mask.z < 0 && mask.w < 0)
|
|
|
|
__m128i low2 = _mm_and_si128(mask.ivec, _mm_shuffle_epi32(mask.ivec, _MM_SHUFFLE(3, 2, 3, 2)));
|
|
|
|
__m128i low1 = _mm_and_si128(low2, _mm_shuffle_epi32(low2, _MM_SHUFFLE(1, 1, 1, 1)));
|
|
|
|
// Now we only need to check one sign bit.
|
|
|
|
return _mm_cvtsi128_si32(low1) >= 0;
|
|
|
|
#else
|
|
|
|
return mask.x >= 0 || mask.y >= 0 || mask.z >= 0 || mask.w >= 0;
|
|
|
|
#endif
|
2017-04-23 14:18:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 13:56:33 +00:00
|
|
|
static inline Vec4<float> EdgeRecip(const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2) {
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
|
2017-05-13 13:56:33 +00:00
|
|
|
__m128i wsum = _mm_add_epi32(w0.ivec, _mm_add_epi32(w1.ivec, w2.ivec));
|
2017-05-14 14:40:38 +00:00
|
|
|
// _mm_rcp_ps loses too much precision.
|
|
|
|
return _mm_div_ps(_mm_set1_ps(1.0f), _mm_cvtepi32_ps(wsum));
|
2017-05-13 13:56:33 +00:00
|
|
|
#else
|
|
|
|
return (w0 + w1 + w2).Cast<float>().Reciprocal();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:24:17 +00:00
|
|
|
template <bool clearMode>
|
2013-12-08 21:03:55 +00:00
|
|
|
void DrawTriangleSlice(
|
|
|
|
const VertexData& v0, const VertexData& v1, const VertexData& v2,
|
2021-09-06 06:24:08 +00:00
|
|
|
int x1, int y1, int x2, int y2,
|
2021-11-20 22:22:55 +00:00
|
|
|
bool byY, int h1, int h2,
|
2021-11-21 15:20:19 +00:00
|
|
|
const PixelFuncID &pixelID,
|
|
|
|
const Rasterizer::SingleFunc &drawPixel)
|
2013-06-25 17:36:16 +00:00
|
|
|
{
|
2017-04-23 14:18:32 +00:00
|
|
|
Vec4<int> bias0 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v0.screenpos.xy(), v1.screenpos.xy(), v2.screenpos.xy()) ? -1 : 0);
|
|
|
|
Vec4<int> bias1 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v1.screenpos.xy(), v2.screenpos.xy(), v0.screenpos.xy()) ? -1 : 0);
|
|
|
|
Vec4<int> bias2 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v2.screenpos.xy(), v0.screenpos.xy(), v1.screenpos.xy()) ? -1 : 0);
|
2013-07-28 22:30:47 +00:00
|
|
|
|
2017-05-11 01:03:35 +00:00
|
|
|
int texbufw[8] = {0};
|
2013-12-10 14:33:13 +00:00
|
|
|
|
2014-06-15 17:31:16 +00:00
|
|
|
int maxTexLevel = gstate.getTextureMaxLevel();
|
2013-12-10 14:33:13 +00:00
|
|
|
u8 *texptr[8] = {NULL};
|
|
|
|
|
2017-05-20 20:02:27 +00:00
|
|
|
if (!gstate.isMipmapEnabled()) {
|
2013-12-10 14:33:13 +00:00
|
|
|
// No mipmapping enabled
|
|
|
|
maxTexLevel = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:24:17 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !clearMode) {
|
2013-09-16 04:39:28 +00:00
|
|
|
GETextureFormat texfmt = gstate.getTextureFormat();
|
2013-12-10 14:33:13 +00:00
|
|
|
for (int i = 0; i <= maxTexLevel; i++) {
|
|
|
|
u32 texaddr = gstate.getTextureAddress(i);
|
2017-05-11 01:03:35 +00:00
|
|
|
texbufw[i] = GetTextureBufw(i, texaddr, texfmt);
|
2014-04-12 22:45:46 +00:00
|
|
|
if (Memory::IsValidAddress(texaddr))
|
2014-10-19 21:19:51 +00:00
|
|
|
texptr[i] = Memory::GetPointerUnchecked(texaddr);
|
2014-04-12 22:45:46 +00:00
|
|
|
else
|
|
|
|
texptr[i] = 0;
|
2013-12-10 14:33:13 +00:00
|
|
|
}
|
2013-09-16 04:39:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 14:18:32 +00:00
|
|
|
TriangleEdge e0;
|
|
|
|
TriangleEdge e1;
|
|
|
|
TriangleEdge e2;
|
|
|
|
|
2021-09-06 06:24:08 +00:00
|
|
|
int64_t minX = x1, maxX = x2, minY = y1, maxY = y2;
|
2017-11-12 02:16:55 +00:00
|
|
|
if (byY) {
|
2020-05-10 02:25:19 +00:00
|
|
|
maxY = std::min(maxY, minY + h2 * 16 * 2) - 1;
|
2017-11-12 02:16:55 +00:00
|
|
|
minY += h1 * 16 * 2;
|
|
|
|
} else {
|
2020-05-10 02:25:19 +00:00
|
|
|
maxX = std::min(maxX, minX + h2 * 16 * 2) - 1;
|
2017-11-12 02:16:55 +00:00
|
|
|
minX += h1 * 16 * 2;
|
|
|
|
}
|
|
|
|
|
2013-07-28 22:30:47 +00:00
|
|
|
ScreenCoords pprime(minX, minY, 0);
|
2017-04-23 14:18:32 +00:00
|
|
|
Vec4<int> w0_base = e0.Start(v1.screenpos, v2.screenpos, pprime);
|
|
|
|
Vec4<int> w1_base = e1.Start(v2.screenpos, v0.screenpos, pprime);
|
|
|
|
Vec4<int> w2_base = e2.Start(v0.screenpos, v1.screenpos, pprime);
|
2013-09-21 19:03:43 +00:00
|
|
|
|
2013-12-15 18:53:35 +00:00
|
|
|
// All the z values are the same, no interpolation required.
|
|
|
|
// This is common, and when we interpolate, we lose accuracy.
|
|
|
|
const bool flatZ = v0.screenpos.z == v1.screenpos.z && v0.screenpos.z == v2.screenpos.z;
|
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
Sampler::Funcs sampler = Sampler::GetFuncs();
|
2017-05-11 00:31:34 +00:00
|
|
|
|
2021-09-06 06:24:08 +00:00
|
|
|
for (int64_t curY = minY; curY <= maxY; curY += 32,
|
2017-04-23 14:18:32 +00:00
|
|
|
w0_base = e0.StepY(w0_base),
|
|
|
|
w1_base = e1.StepY(w1_base),
|
|
|
|
w2_base = e2.StepY(w2_base)) {
|
|
|
|
Vec4<int> w0 = w0_base;
|
|
|
|
Vec4<int> w1 = w1_base;
|
|
|
|
Vec4<int> w2 = w2_base;
|
2013-12-08 22:24:17 +00:00
|
|
|
|
2017-05-14 14:40:06 +00:00
|
|
|
// TODO: Maybe we can clip the edges instead?
|
2021-09-06 06:24:08 +00:00
|
|
|
int scissorYPlus1 = curY + 16 > maxY ? -1 : 0;
|
2020-05-10 02:25:19 +00:00
|
|
|
Vec4<int> scissor_mask = Vec4<int>(0, maxX - minX, scissorYPlus1, (maxX - minX) | scissorYPlus1);
|
2017-05-14 14:40:06 +00:00
|
|
|
Vec4<int> scissor_step = Vec4<int>(0, -32, 0, -32);
|
|
|
|
|
2021-09-06 06:24:08 +00:00
|
|
|
DrawingCoords p = TransformUnit::ScreenToDrawing(ScreenCoords(minX, curY, 0));
|
2013-12-09 06:38:09 +00:00
|
|
|
|
2021-09-06 06:24:08 +00:00
|
|
|
for (int64_t curX = minX; curX <= maxX; curX += 32,
|
2017-04-23 14:18:32 +00:00
|
|
|
w0 = e0.StepX(w0),
|
|
|
|
w1 = e1.StepX(w1),
|
|
|
|
w2 = e2.StepX(w2),
|
2017-05-14 14:40:06 +00:00
|
|
|
scissor_mask = scissor_mask + scissor_step,
|
2017-04-23 14:18:32 +00:00
|
|
|
p.x = (p.x + 2) & 0x3FF) {
|
2013-06-25 15:46:55 +00:00
|
|
|
|
|
|
|
// If p is on or inside all edges, render pixel
|
2017-05-14 14:40:06 +00:00
|
|
|
Vec4<int> mask = MakeMask(w0, w1, w2, bias0, bias1, bias2, scissor_mask);
|
2017-05-07 21:58:15 +00:00
|
|
|
if (AnyMask(mask)) {
|
2017-05-13 13:56:33 +00:00
|
|
|
Vec4<float> wsum_recip = EdgeRecip(w0, w1, w2);
|
2017-04-23 14:18:32 +00:00
|
|
|
|
|
|
|
Vec4<int> prim_color[4];
|
|
|
|
Vec3<int> sec_color[4];
|
2013-12-08 22:24:17 +00:00
|
|
|
if (gstate.getShadeMode() == GE_SHADE_GOURAUD && !clearMode) {
|
2015-06-11 14:01:17 +00:00
|
|
|
// Does the PSP do perspective-correct color interpolation? The GC doesn't.
|
2017-04-23 14:18:32 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
prim_color[i] = Interpolate(v0.color0, v1.color0, v2.color0, w0[i], w1[i], w2[i], wsum_recip[i]);
|
|
|
|
sec_color[i] = Interpolate(v0.color1, v1.color1, v2.color1, w0[i], w1[i], w2[i], wsum_recip[i]);
|
|
|
|
}
|
2013-06-29 22:32:25 +00:00
|
|
|
} else {
|
2017-04-23 14:18:32 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
prim_color[i] = v2.color0;
|
|
|
|
sec_color[i] = v2.color1;
|
|
|
|
}
|
2013-06-29 22:32:25 +00:00
|
|
|
}
|
2013-06-28 19:43:24 +00:00
|
|
|
|
2013-12-08 22:24:17 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !clearMode) {
|
2017-04-23 17:39:58 +00:00
|
|
|
Vec4<float> s, t;
|
2013-07-24 10:40:57 +00:00
|
|
|
if (gstate.isModeThrough()) {
|
2017-04-23 17:39:58 +00:00
|
|
|
s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), w0, w1, w2, wsum_recip);
|
|
|
|
t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), w0, w1, w2, wsum_recip);
|
2017-05-13 13:49:27 +00:00
|
|
|
|
|
|
|
// For levels > 0, mipmapping is always based on level 0. Simpler to scale first.
|
|
|
|
s *= 1.0f / (float)gstate.getTextureWidth(0);
|
|
|
|
t *= 1.0f / (float)gstate.getTextureHeight(0);
|
2013-07-24 10:40:57 +00:00
|
|
|
} else {
|
2015-06-11 18:23:53 +00:00
|
|
|
// Texture coordinate interpolation must definitely be perspective-correct.
|
2017-04-23 17:39:58 +00:00
|
|
|
GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t);
|
|
|
|
}
|
|
|
|
|
2017-05-11 00:31:34 +00:00
|
|
|
ApplyTexturing(sampler, prim_color, s, t, maxTexLevel, texptr, texbufw);
|
2013-06-29 22:32:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:21:12 +00:00
|
|
|
if (!clearMode) {
|
2017-04-23 14:18:32 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2014-03-16 23:21:12 +00:00
|
|
|
#if defined(_M_SSE)
|
2017-04-23 14:18:32 +00:00
|
|
|
// TODO: Tried making Vec4 do this, but things got slower.
|
|
|
|
const __m128i sec = _mm_and_si128(sec_color[i].ivec, _mm_set_epi32(0, -1, -1, -1));
|
|
|
|
prim_color[i].ivec = _mm_add_epi32(prim_color[i].ivec, sec);
|
2014-03-16 23:21:12 +00:00
|
|
|
#else
|
2017-04-23 14:18:32 +00:00
|
|
|
prim_color[i] += Vec4<int>(sec_color[i], 0);
|
2014-03-16 23:21:12 +00:00
|
|
|
#endif
|
2017-04-23 14:18:32 +00:00
|
|
|
}
|
2014-03-16 23:21:12 +00:00
|
|
|
}
|
2013-06-29 22:32:25 +00:00
|
|
|
|
2017-04-23 14:18:32 +00:00
|
|
|
Vec4<int> fog = Vec4<int>::AssignToAll(255);
|
2015-06-11 14:01:17 +00:00
|
|
|
if (gstate.isFogEnabled() && !clearMode) {
|
2017-04-23 14:18:32 +00:00
|
|
|
Vec4<float> fogdepths = w0.Cast<float>() * v0.fogdepth + w1.Cast<float>() * v1.fogdepth + w2.Cast<float>() * v2.fogdepth;
|
|
|
|
fogdepths = fogdepths * wsum_recip;
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
fog[i] = ClampFogDepth(fogdepths[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec4<int> z;
|
|
|
|
if (flatZ) {
|
|
|
|
z = Vec4<int>::AssignToAll(v2.screenpos.z);
|
|
|
|
} else {
|
|
|
|
// TODO: Is that the correct way to interpolate?
|
|
|
|
Vec4<float> zfloats = w0.Cast<float>() * v0.screenpos.z + w1.Cast<float>() * v1.screenpos.z + w2.Cast<float>() * v2.screenpos.z;
|
|
|
|
z = (zfloats * wsum_recip).Cast<int>();
|
2015-06-11 14:01:17 +00:00
|
|
|
}
|
2013-07-03 17:27:07 +00:00
|
|
|
|
2017-04-23 14:18:32 +00:00
|
|
|
DrawingCoords subp = p;
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (mask[i] < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
subp.x = p.x + (i & 1);
|
|
|
|
subp.y = p.y + (i / 2);
|
2013-07-23 19:04:27 +00:00
|
|
|
|
2021-11-21 15:20:19 +00:00
|
|
|
drawPixel(subp.x, subp.y, z[i], fog[i], prim_color[i], pixelID);
|
2017-04-23 14:18:32 +00:00
|
|
|
}
|
2013-06-25 15:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 14:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:03:55 +00:00
|
|
|
// Draws triangle, vertices specified in counter-clockwise direction
|
|
|
|
void DrawTriangle(const VertexData& v0, const VertexData& v1, const VertexData& v2)
|
|
|
|
{
|
2015-06-11 10:29:14 +00:00
|
|
|
PROFILE_THIS_SCOPE("draw_tri");
|
|
|
|
|
2013-12-08 21:03:55 +00:00
|
|
|
Vec2<int> d01((int)v0.screenpos.x - (int)v1.screenpos.x, (int)v0.screenpos.y - (int)v1.screenpos.y);
|
|
|
|
Vec2<int> d02((int)v0.screenpos.x - (int)v2.screenpos.x, (int)v0.screenpos.y - (int)v2.screenpos.y);
|
|
|
|
Vec2<int> d12((int)v1.screenpos.x - (int)v2.screenpos.x, (int)v1.screenpos.y - (int)v2.screenpos.y);
|
|
|
|
|
|
|
|
// Drop primitives which are not in CCW order by checking the cross product
|
|
|
|
if (d01.x * d02.y - d01.y * d02.x < 0)
|
|
|
|
return;
|
|
|
|
|
2015-06-11 14:01:17 +00:00
|
|
|
int minX = std::min(std::min(v0.screenpos.x, v1.screenpos.x), v2.screenpos.x) & ~0xF;
|
|
|
|
int minY = std::min(std::min(v0.screenpos.y, v1.screenpos.y), v2.screenpos.y) & ~0xF;
|
2017-11-12 02:16:55 +00:00
|
|
|
int maxX = (std::max(std::max(v0.screenpos.x, v1.screenpos.x), v2.screenpos.x) + 0xF) & ~0xF;
|
|
|
|
int maxY = (std::max(std::max(v0.screenpos.y, v1.screenpos.y), v2.screenpos.y) + 0xF) & ~0xF;
|
2013-12-08 21:03:55 +00:00
|
|
|
|
|
|
|
DrawingCoords scissorTL(gstate.getScissorX1(), gstate.getScissorY1(), 0);
|
|
|
|
DrawingCoords scissorBR(gstate.getScissorX2(), gstate.getScissorY2(), 0);
|
|
|
|
minX = std::max(minX, (int)TransformUnit::DrawingToScreen(scissorTL).x);
|
2021-11-07 19:19:41 +00:00
|
|
|
maxX = std::min(maxX, (int)TransformUnit::DrawingToScreen(scissorBR).x + 15);
|
2013-12-08 21:03:55 +00:00
|
|
|
minY = std::max(minY, (int)TransformUnit::DrawingToScreen(scissorTL).y);
|
2021-11-07 19:19:41 +00:00
|
|
|
maxY = std::min(maxY, (int)TransformUnit::DrawingToScreen(scissorBR).y + 15);
|
2013-12-08 21:03:55 +00:00
|
|
|
|
2017-04-23 14:18:32 +00:00
|
|
|
// 32 because we do two pixels at once, and we don't want overlap.
|
2017-11-12 02:16:55 +00:00
|
|
|
int rangeY = (maxY - minY) / 32 + 1;
|
|
|
|
int rangeX = (maxX - minX) / 32 + 1;
|
2021-06-12 19:21:28 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
PixelFuncID pixelID;
|
|
|
|
ComputePixelFuncID(&pixelID);
|
2021-11-21 15:20:19 +00:00
|
|
|
Rasterizer::SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID);
|
2021-11-20 22:22:55 +00:00
|
|
|
|
2021-06-12 19:21:28 +00:00
|
|
|
const int MIN_LINES_PER_THREAD = 4;
|
|
|
|
|
2017-11-12 02:16:55 +00:00
|
|
|
if (rangeY >= 12 && rangeX >= rangeY * 4) {
|
|
|
|
if (gstate.isModeClear()) {
|
2015-11-26 00:00:25 +00:00
|
|
|
auto bound = [&](int a, int b) -> void {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<true>(v0, v1, v2, minX, minY, maxX, maxY, false, a, b, pixelID, drawPixel);
|
2015-11-26 00:00:25 +00:00
|
|
|
};
|
2021-06-12 19:21:28 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, bound, 0, rangeX, MIN_LINES_PER_THREAD);
|
2015-11-26 00:00:25 +00:00
|
|
|
} else {
|
2017-11-12 02:16:55 +00:00
|
|
|
auto bound = [&](int a, int b) -> void {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<false>(v0, v1, v2, minX, minY, maxX, maxY, false, a, b, pixelID, drawPixel);
|
2017-11-12 02:16:55 +00:00
|
|
|
};
|
2021-06-12 19:21:28 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, bound, 0, rangeX, MIN_LINES_PER_THREAD);
|
2015-03-06 08:24:39 +00:00
|
|
|
}
|
2017-11-12 02:16:55 +00:00
|
|
|
} else if (rangeY >= 12 && rangeX >= 12) {
|
|
|
|
if (gstate.isModeClear()) {
|
|
|
|
auto bound = [&](int a, int b) -> void {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<true>(v0, v1, v2, minX, minY, maxX, maxY, true, a, b, pixelID, drawPixel);
|
2017-11-12 02:16:55 +00:00
|
|
|
};
|
2021-06-12 19:21:28 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, bound, 0, rangeY, MIN_LINES_PER_THREAD);
|
2017-11-12 02:16:55 +00:00
|
|
|
} else {
|
2015-11-26 00:00:25 +00:00
|
|
|
auto bound = [&](int a, int b) -> void {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<false>(v0, v1, v2, minX, minY, maxX, maxY, true, a, b, pixelID, drawPixel);
|
2015-11-26 00:00:25 +00:00
|
|
|
};
|
2021-06-12 19:21:28 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, bound, 0, rangeY, MIN_LINES_PER_THREAD);
|
2017-11-12 02:16:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gstate.isModeClear()) {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<true>(v0, v1, v2, minX, minY, maxX, maxY, true, 0, rangeY, pixelID, drawPixel);
|
2015-11-26 00:00:25 +00:00
|
|
|
} else {
|
2021-11-21 15:20:19 +00:00
|
|
|
DrawTriangleSlice<false>(v0, v1, v2, minX, minY, maxX, maxY, true, 0, rangeY, pixelID, drawPixel);
|
2015-03-06 08:24:39 +00:00
|
|
|
}
|
2014-03-16 21:49:49 +00:00
|
|
|
}
|
2013-12-08 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-19 04:50:38 +00:00
|
|
|
void DrawPoint(const VertexData &v0)
|
|
|
|
{
|
|
|
|
ScreenCoords pos = v0.screenpos;
|
2014-03-16 22:04:41 +00:00
|
|
|
Vec4<int> prim_color = v0.color0;
|
2014-01-19 04:50:38 +00:00
|
|
|
Vec3<int> sec_color = v0.color1;
|
|
|
|
|
2013-12-09 11:43:49 +00:00
|
|
|
ScreenCoords scissorTL(TransformUnit::DrawingToScreen(DrawingCoords(gstate.getScissorX1(), gstate.getScissorY1(), 0)));
|
|
|
|
ScreenCoords scissorBR(TransformUnit::DrawingToScreen(DrawingCoords(gstate.getScissorX2(), gstate.getScissorY2(), 0)));
|
2021-11-07 19:19:41 +00:00
|
|
|
// Allow drawing within a pixel's center.
|
|
|
|
scissorBR.x += 15;
|
|
|
|
scissorBR.y += 15;
|
2013-12-09 11:43:49 +00:00
|
|
|
|
2017-05-14 15:47:53 +00:00
|
|
|
if (pos.x < scissorTL.x || pos.y < scissorTL.y || pos.x > scissorBR.x || pos.y > scissorBR.y)
|
2013-12-09 11:43:49 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
Sampler::Funcs sampler = Sampler::GetFuncs();
|
2021-11-20 22:22:55 +00:00
|
|
|
PixelFuncID pixelID;
|
|
|
|
ComputePixelFuncID(&pixelID);
|
2021-11-21 15:20:19 +00:00
|
|
|
Rasterizer::SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID);
|
2017-05-11 00:31:34 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !pixelID.clearMode) {
|
2017-05-11 01:03:35 +00:00
|
|
|
int texbufw[8] = {0};
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2014-06-15 17:31:16 +00:00
|
|
|
int maxTexLevel = gstate.getTextureMaxLevel();
|
2014-01-19 04:42:08 +00:00
|
|
|
u8 *texptr[8] = {NULL};
|
|
|
|
|
2017-05-20 20:02:27 +00:00
|
|
|
if (!gstate.isMipmapEnabled()) {
|
2014-01-19 04:42:08 +00:00
|
|
|
// No mipmapping enabled
|
|
|
|
maxTexLevel = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !pixelID.clearMode) {
|
2014-01-19 04:42:08 +00:00
|
|
|
GETextureFormat texfmt = gstate.getTextureFormat();
|
|
|
|
for (int i = 0; i <= maxTexLevel; i++) {
|
|
|
|
u32 texaddr = gstate.getTextureAddress(i);
|
2017-05-11 01:03:35 +00:00
|
|
|
texbufw[i] = GetTextureBufw(i, texaddr, texfmt);
|
2017-05-27 01:02:55 +00:00
|
|
|
if (Memory::IsValidAddress(texaddr))
|
|
|
|
texptr[i] = Memory::GetPointerUnchecked(texaddr);
|
|
|
|
else
|
|
|
|
texptr[i] = 0;
|
2014-01-19 04:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 01:42:43 +00:00
|
|
|
float s = v0.texturecoords.s();
|
|
|
|
float t = v0.texturecoords.t();
|
2017-05-13 13:49:27 +00:00
|
|
|
if (gstate.isModeThrough()) {
|
|
|
|
s *= 1.0f / (float)gstate.getTextureWidth(0);
|
|
|
|
t *= 1.0f / (float)gstate.getTextureHeight(0);
|
2017-05-27 01:42:43 +00:00
|
|
|
} else {
|
|
|
|
// Texture coordinate interpolation must definitely be perspective-correct.
|
|
|
|
GetTextureCoordinates(v0, v0, 0.0f, s, t);
|
2017-05-13 13:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 01:02:55 +00:00
|
|
|
int texLevel;
|
|
|
|
int texLevelFrac;
|
2017-05-20 20:02:27 +00:00
|
|
|
bool bilinear;
|
|
|
|
CalculateSamplingParams(0.0f, 0.0f, maxTexLevel, texLevel, texLevelFrac, bilinear);
|
2017-05-11 00:31:34 +00:00
|
|
|
ApplyTexturing(sampler, prim_color, s, t, texLevel, texLevelFrac, bilinear, texptr, texbufw);
|
2014-01-19 04:42:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (!pixelID.clearMode)
|
2014-03-16 22:04:41 +00:00
|
|
|
prim_color += Vec4<int>(sec_color, 0);
|
2013-12-09 11:43:49 +00:00
|
|
|
|
|
|
|
ScreenCoords pprime = pos;
|
|
|
|
|
|
|
|
DrawingCoords p = TransformUnit::ScreenToDrawing(pprime);
|
|
|
|
u16 z = pos.z;
|
|
|
|
|
2015-11-26 00:03:15 +00:00
|
|
|
u8 fog = 255;
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isFogEnabled() && !pixelID.clearMode) {
|
2015-11-26 00:03:15 +00:00
|
|
|
fog = ClampFogDepth(v0.fogdepth);
|
|
|
|
}
|
2015-06-11 14:01:17 +00:00
|
|
|
|
2021-11-21 15:20:19 +00:00
|
|
|
drawPixel(p.x, p.y, z, fog, prim_color, pixelID);
|
2013-12-09 11:43:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 04:55:44 +00:00
|
|
|
void ClearRectangle(const VertexData &v0, const VertexData &v1)
|
|
|
|
{
|
|
|
|
int minX = std::min(v0.screenpos.x, v1.screenpos.x) & ~0xF;
|
|
|
|
int minY = std::min(v0.screenpos.y, v1.screenpos.y) & ~0xF;
|
|
|
|
int maxX = (std::max(v0.screenpos.x, v1.screenpos.x) + 0xF) & ~0xF;
|
|
|
|
int maxY = (std::max(v0.screenpos.y, v1.screenpos.y) + 0xF) & ~0xF;
|
|
|
|
|
|
|
|
DrawingCoords scissorTL(gstate.getScissorX1(), gstate.getScissorY1(), 0);
|
|
|
|
DrawingCoords scissorBR(gstate.getScissorX2(), gstate.getScissorY2(), 0);
|
|
|
|
minX = std::max(minX, (int)TransformUnit::DrawingToScreen(scissorTL).x);
|
2020-03-10 01:57:55 +00:00
|
|
|
maxX = std::max(0, std::min(maxX, (int)TransformUnit::DrawingToScreen(scissorBR).x + 16));
|
2017-11-12 04:55:44 +00:00
|
|
|
minY = std::max(minY, (int)TransformUnit::DrawingToScreen(scissorTL).y);
|
2020-03-10 01:57:55 +00:00
|
|
|
maxY = std::max(0, std::min(maxY, (int)TransformUnit::DrawingToScreen(scissorBR).y + 16));
|
2017-11-12 04:55:44 +00:00
|
|
|
|
2021-11-15 14:26:11 +00:00
|
|
|
DrawingCoords pprime = TransformUnit::ScreenToDrawing(ScreenCoords(minX, minY, 0));
|
|
|
|
DrawingCoords pend = TransformUnit::ScreenToDrawing(ScreenCoords(maxX, maxY, 0));
|
|
|
|
|
|
|
|
constexpr int MIN_LINES_PER_THREAD = 32;
|
|
|
|
// Min and max are in PSP fixed point screen coordinates, 16 here is for the 4 subpixel bits.
|
2017-11-12 04:55:44 +00:00
|
|
|
const int w = (maxX - minX) / 16;
|
|
|
|
if (w <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gstate.isClearModeDepthMask()) {
|
|
|
|
const u16 z = v1.screenpos.z;
|
|
|
|
const int stride = gstate.DepthBufStride();
|
|
|
|
|
2021-11-15 14:09:12 +00:00
|
|
|
// If both bytes of Z equal, we can just use memset directly which is faster.
|
2021-11-14 20:46:05 +00:00
|
|
|
if ((z & 0xFF) == (z >> 8)) {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
2021-11-15 14:26:11 +00:00
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-15 14:09:12 +00:00
|
|
|
u16 *row = depthbuf.Get16Ptr(p.x, p.y, stride);
|
2021-11-14 20:46:05 +00:00
|
|
|
memset(row, z, w * 2);
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
2021-11-14 20:46:05 +00:00
|
|
|
} else {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
2021-11-15 14:26:11 +00:00
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-14 20:46:05 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
SetPixelDepth(p.x + x, p.y, z);
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this stays 0xFFFFFFFF if keeping color and alpha, even for 16-bit.
|
|
|
|
u32 keepOldMask = 0xFFFFFFFF;
|
2018-11-23 14:10:14 +00:00
|
|
|
if (gstate.isClearModeColorMask())
|
|
|
|
keepOldMask &= 0xFF000000;
|
|
|
|
if (gstate.isClearModeAlphaMask())
|
|
|
|
keepOldMask &= 0x00FFFFFF;
|
|
|
|
|
|
|
|
// The pixel write masks are respected in clear mode.
|
|
|
|
keepOldMask |= gstate.getColorMask();
|
|
|
|
|
|
|
|
const u32 new_color = v1.color0.ToRGBA();
|
|
|
|
u16 new_color16;
|
2017-11-12 04:55:44 +00:00
|
|
|
switch (gstate.FrameBufFormat()) {
|
|
|
|
case GE_FORMAT_565:
|
|
|
|
new_color16 = RGBA8888ToRGB565(new_color);
|
2018-11-23 14:10:14 +00:00
|
|
|
keepOldMask = keepOldMask == 0 ? 0 : (0xFFFF0000 | RGBA8888ToRGB565(keepOldMask));
|
2017-11-12 04:55:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GE_FORMAT_5551:
|
|
|
|
new_color16 = RGBA8888ToRGBA5551(new_color);
|
2018-11-23 14:10:14 +00:00
|
|
|
keepOldMask = keepOldMask == 0 ? 0 : (0xFFFF0000 | RGBA8888ToRGBA5551(keepOldMask));
|
2017-11-12 04:55:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GE_FORMAT_4444:
|
|
|
|
new_color16 = RGBA8888ToRGBA4444(new_color);
|
2018-11-23 14:10:14 +00:00
|
|
|
keepOldMask = keepOldMask == 0 ? 0 : (0xFFFF0000 | RGBA8888ToRGBA4444(keepOldMask));
|
2017-11-12 04:55:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GE_FORMAT_8888:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GE_FORMAT_INVALID:
|
2020-08-20 04:18:44 +00:00
|
|
|
case GE_FORMAT_DEPTH16:
|
2020-07-19 15:47:02 +00:00
|
|
|
_dbg_assert_msg_(false, "Software: invalid framebuf format.");
|
2017-11-12 04:55:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keepOldMask == 0) {
|
|
|
|
const int stride = gstate.FrameBufStride();
|
|
|
|
|
|
|
|
if (gstate.FrameBufFormat() == GE_FORMAT_8888) {
|
2021-11-15 14:09:12 +00:00
|
|
|
const bool canMemsetColor = (new_color & 0xFF) == (new_color >> 8) && (new_color & 0xFFFF) == (new_color >> 16);
|
2021-11-15 14:26:11 +00:00
|
|
|
if (canMemsetColor) {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-15 14:09:12 +00:00
|
|
|
u32 *row = fb.Get32Ptr(p.x, p.y, stride);
|
2021-11-14 20:46:05 +00:00
|
|
|
memset(row, new_color, w * 4);
|
2021-11-15 14:26:11 +00:00
|
|
|
}
|
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
|
|
|
} else {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-14 20:46:05 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
fb.Set32(p.x + x, p.y, stride, new_color);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
} else {
|
2021-11-15 14:09:12 +00:00
|
|
|
const bool canMemsetColor = (new_color16 & 0xFF) == (new_color16 >> 8);
|
2021-11-15 14:26:11 +00:00
|
|
|
if (canMemsetColor) {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-15 14:09:12 +00:00
|
|
|
u16 *row = fb.Get16Ptr(p.x, p.y, stride);
|
2021-11-14 20:46:05 +00:00
|
|
|
memset(row, new_color16, w * 2);
|
2021-11-15 14:26:11 +00:00
|
|
|
}
|
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
|
|
|
} else {
|
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-14 20:46:05 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
fb.Set16(p.x + x, p.y, stride, new_color16);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
|
|
|
} else if (keepOldMask != 0xFFFFFFFF) {
|
|
|
|
const int stride = gstate.FrameBufStride();
|
|
|
|
|
|
|
|
if (gstate.FrameBufFormat() == GE_FORMAT_8888) {
|
2021-11-14 20:46:05 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
2021-11-15 14:26:11 +00:00
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-14 20:46:05 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
const u32 old_color = fb.Get32(p.x + x, p.y, stride);
|
|
|
|
const u32 c = (old_color & keepOldMask) | (new_color & ~keepOldMask);
|
|
|
|
fb.Set32(p.x + x, p.y, stride, c);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
2017-11-12 04:55:44 +00:00
|
|
|
} else {
|
2021-11-14 20:46:05 +00:00
|
|
|
ParallelRangeLoop(&g_threadManager, [=](int y1, int y2) {
|
2021-11-15 14:26:11 +00:00
|
|
|
DrawingCoords p = pprime;
|
|
|
|
for (p.y = y1; p.y < y2; ++p.y) {
|
2021-11-14 20:46:05 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
const u16 old_color = fb.Get16(p.x + x, p.y, stride);
|
|
|
|
const u16 c = (old_color & keepOldMask) | (new_color16 & ~keepOldMask);
|
|
|
|
fb.Set16(p.x + x, p.y, stride, c);
|
|
|
|
}
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
2021-11-15 14:26:11 +00:00
|
|
|
}, pprime.y, pend.y, MIN_LINES_PER_THREAD);
|
2017-11-12 04:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 11:43:49 +00:00
|
|
|
void DrawLine(const VertexData &v0, const VertexData &v1)
|
|
|
|
{
|
|
|
|
// TODO: Use a proper line drawing algorithm that handles fractional endpoints correctly.
|
|
|
|
Vec3<int> a(v0.screenpos.x, v0.screenpos.y, v0.screenpos.z);
|
|
|
|
Vec3<int> b(v1.screenpos.x, v1.screenpos.y, v0.screenpos.z);
|
|
|
|
|
|
|
|
int dx = b.x - a.x;
|
|
|
|
int dy = b.y - a.y;
|
|
|
|
int dz = b.z - a.z;
|
|
|
|
|
|
|
|
int steps;
|
|
|
|
if (abs(dx) < abs(dy))
|
2014-01-20 07:32:53 +00:00
|
|
|
steps = abs(dy) / 16;
|
2013-12-09 11:43:49 +00:00
|
|
|
else
|
2014-01-20 07:32:53 +00:00
|
|
|
steps = abs(dx) / 16;
|
2013-12-09 11:43:49 +00:00
|
|
|
|
2021-11-04 07:11:09 +00:00
|
|
|
// Avoid going too far since we typically don't start at the pixel center.
|
|
|
|
if (dx < 0 && dx >= -16)
|
|
|
|
dx++;
|
|
|
|
if (dy < 0 && dy >= -16)
|
|
|
|
dy++;
|
|
|
|
|
|
|
|
double xinc = (double)dx / steps;
|
|
|
|
double yinc = (double)dy / steps;
|
|
|
|
double zinc = (double)dz / steps;
|
2013-12-09 11:43:49 +00:00
|
|
|
|
2014-01-19 04:42:08 +00:00
|
|
|
ScreenCoords scissorTL(TransformUnit::DrawingToScreen(DrawingCoords(gstate.getScissorX1(), gstate.getScissorY1(), 0)));
|
|
|
|
ScreenCoords scissorBR(TransformUnit::DrawingToScreen(DrawingCoords(gstate.getScissorX2(), gstate.getScissorY2(), 0)));
|
2021-11-07 19:19:41 +00:00
|
|
|
// Allow drawing within a pixel's center.
|
|
|
|
scissorBR.x += 15;
|
|
|
|
scissorBR.y += 15;
|
2021-11-20 22:22:55 +00:00
|
|
|
|
|
|
|
PixelFuncID pixelID;
|
|
|
|
ComputePixelFuncID(&pixelID);
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2017-05-11 01:03:35 +00:00
|
|
|
int texbufw[8] = {0};
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2014-06-15 17:31:16 +00:00
|
|
|
int maxTexLevel = gstate.getTextureMaxLevel();
|
2014-01-19 04:42:08 +00:00
|
|
|
u8 *texptr[8] = {NULL};
|
|
|
|
|
2017-05-20 20:02:27 +00:00
|
|
|
if (!gstate.isMipmapEnabled()) {
|
2014-01-19 04:42:08 +00:00
|
|
|
// No mipmapping enabled
|
|
|
|
maxTexLevel = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !pixelID.clearMode) {
|
2014-01-19 04:42:08 +00:00
|
|
|
GETextureFormat texfmt = gstate.getTextureFormat();
|
|
|
|
for (int i = 0; i <= maxTexLevel; i++) {
|
|
|
|
u32 texaddr = gstate.getTextureAddress(i);
|
2017-05-11 01:03:35 +00:00
|
|
|
texbufw[i] = GetTextureBufw(i, texaddr, texfmt);
|
2014-01-19 04:42:08 +00:00
|
|
|
texptr[i] = Memory::GetPointer(texaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
Sampler::Funcs sampler = Sampler::GetFuncs();
|
2021-11-21 15:20:19 +00:00
|
|
|
Rasterizer::SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID);
|
2017-05-11 00:31:34 +00:00
|
|
|
|
2021-11-04 07:11:09 +00:00
|
|
|
double x = a.x > b.x ? a.x - 1 : a.x;
|
|
|
|
double y = a.y > b.y ? a.y - 1 : a.y;
|
|
|
|
double z = a.z;
|
2014-01-20 07:32:12 +00:00
|
|
|
const int steps1 = steps == 0 ? 1 : steps;
|
2017-05-14 21:15:59 +00:00
|
|
|
for (int i = 0; i < steps; i++) {
|
2017-05-14 15:47:53 +00:00
|
|
|
if (x >= scissorTL.x && y >= scissorTL.y && x <= scissorBR.x && y <= scissorBR.y) {
|
|
|
|
// Interpolate between the two points.
|
2017-05-14 15:55:50 +00:00
|
|
|
Vec4<int> prim_color;
|
|
|
|
Vec3<int> sec_color;
|
|
|
|
if (gstate.getShadeMode() == GE_SHADE_GOURAUD) {
|
|
|
|
prim_color = (v0.color0 * (steps - i) + v1.color0 * i) / steps1;
|
|
|
|
sec_color = (v0.color1 * (steps - i) + v1.color1 * i) / steps1;
|
|
|
|
} else {
|
|
|
|
prim_color = v1.color0;
|
|
|
|
sec_color = v1.color1;
|
|
|
|
}
|
2015-06-11 14:01:17 +00:00
|
|
|
|
2017-05-14 15:47:53 +00:00
|
|
|
u8 fog = 255;
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isFogEnabled() && !pixelID.clearMode) {
|
2017-05-14 15:47:53 +00:00
|
|
|
fog = ClampFogDepth((v0.fogdepth * (float)(steps - i) + v1.fogdepth * (float)i) / steps1);
|
|
|
|
}
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2017-05-14 22:49:36 +00:00
|
|
|
if (gstate.isAntiAliasEnabled()) {
|
|
|
|
// TODO: Clearmode?
|
|
|
|
// TODO: Calculate.
|
|
|
|
prim_color.a() = 0x7F;
|
2017-05-13 13:49:27 +00:00
|
|
|
}
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (gstate.isTextureMapEnabled() && !pixelID.clearMode) {
|
2017-05-27 01:42:43 +00:00
|
|
|
float s, s1;
|
|
|
|
float t, t1;
|
2017-05-14 15:47:53 +00:00
|
|
|
if (gstate.isModeThrough()) {
|
2017-05-27 01:42:43 +00:00
|
|
|
Vec2<float> tc = (v0.texturecoords * (float)(steps - i) + v1.texturecoords * (float)i) / steps1;
|
|
|
|
Vec2<float> tc1 = (v0.texturecoords * (float)(steps - i - 1) + v1.texturecoords * (float)(i + 1)) / steps1;
|
|
|
|
|
|
|
|
s = tc.s() * (1.0f / (float)gstate.getTextureWidth(0));
|
|
|
|
s1 = tc1.s() * (1.0f / (float)gstate.getTextureWidth(0));
|
|
|
|
t = tc.t() * (1.0f / (float)gstate.getTextureHeight(0));
|
|
|
|
t1 = tc1.t() * (1.0f / (float)gstate.getTextureHeight(0));
|
|
|
|
} else {
|
|
|
|
// Texture coordinate interpolation must definitely be perspective-correct.
|
|
|
|
GetTextureCoordinates(v0, v1, (float)(steps - i) / steps1, s, t);
|
|
|
|
GetTextureCoordinates(v0, v1, (float)(steps - i - 1) / steps1, s1, t1);
|
2017-05-14 15:47:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 22:22:34 +00:00
|
|
|
// If inc is 0, force the delta to zero.
|
2021-11-04 07:11:09 +00:00
|
|
|
float ds = xinc == 0.0 ? 0.0f : (s1 - s) * 16.0f * (1.0f / xinc);
|
|
|
|
float dt = yinc == 0.0 ? 0.0f : (t1 - t) * 16.0f * (1.0f / yinc);
|
2017-05-14 22:22:34 +00:00
|
|
|
|
|
|
|
int texLevel;
|
|
|
|
int texLevelFrac;
|
2017-05-20 20:02:27 +00:00
|
|
|
bool texBilinear;
|
|
|
|
CalculateSamplingParams(ds, dt, maxTexLevel, texLevel, texLevelFrac, texBilinear);
|
2017-05-14 22:22:34 +00:00
|
|
|
|
2017-05-14 22:49:36 +00:00
|
|
|
if (gstate.isAntiAliasEnabled()) {
|
|
|
|
// TODO: This is a niave and wrong implementation.
|
|
|
|
DrawingCoords p0 = TransformUnit::ScreenToDrawing(ScreenCoords((int)x, (int)y, (int)z));
|
|
|
|
s = ((float)p0.x + xinc / 32.0f) / 512.0f;
|
|
|
|
t = ((float)p0.y + yinc / 32.0f) / 512.0f;
|
|
|
|
|
2017-05-20 20:02:27 +00:00
|
|
|
texBilinear = true;
|
2017-05-14 22:49:36 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 00:31:34 +00:00
|
|
|
ApplyTexturing(sampler, prim_color, s, t, texLevel, texLevelFrac, texBilinear, texptr, texbufw);
|
2017-05-13 13:49:27 +00:00
|
|
|
}
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2021-11-20 22:22:55 +00:00
|
|
|
if (!pixelID.clearMode)
|
2017-05-14 15:47:53 +00:00
|
|
|
prim_color += Vec4<int>(sec_color, 0);
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2017-05-14 21:15:59 +00:00
|
|
|
ScreenCoords pprime = ScreenCoords((int)x, (int)y, (int)z);
|
2014-01-19 04:42:08 +00:00
|
|
|
|
2017-05-14 15:47:53 +00:00
|
|
|
DrawingCoords p = TransformUnit::ScreenToDrawing(pprime);
|
2021-11-21 15:20:19 +00:00
|
|
|
drawPixel(p.x, p.y, z, fog, prim_color, pixelID);
|
2014-01-19 04:42:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 15:47:53 +00:00
|
|
|
x += xinc;
|
|
|
|
y += yinc;
|
|
|
|
z += zinc;
|
2013-12-09 11:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 08:02:58 +00:00
|
|
|
bool GetCurrentStencilbuffer(GPUDebugBuffer &buffer)
|
|
|
|
{
|
2013-12-29 21:02:04 +00:00
|
|
|
int w = gstate.getRegionX2() - gstate.getRegionX1() + 1;
|
|
|
|
int h = gstate.getRegionY2() - gstate.getRegionY1() + 1;
|
|
|
|
buffer.Allocate(w, h, GPU_DBG_FORMAT_8BIT);
|
2013-10-07 08:02:58 +00:00
|
|
|
|
|
|
|
u8 *row = buffer.GetData();
|
2013-12-29 21:02:04 +00:00
|
|
|
for (int y = gstate.getRegionY1(); y <= gstate.getRegionY2(); ++y) {
|
|
|
|
for (int x = gstate.getRegionX1(); x <= gstate.getRegionX2(); ++x) {
|
2021-11-20 22:22:55 +00:00
|
|
|
row[x - gstate.getRegionX1()] = GetPixelStencil(gstate.FrameBufFormat(), x, y);
|
2013-10-07 08:02:58 +00:00
|
|
|
}
|
2013-12-29 21:02:04 +00:00
|
|
|
row += w;
|
2013-10-07 08:02:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-15 16:30:37 +00:00
|
|
|
bool GetCurrentTexture(GPUDebugBuffer &buffer, int level)
|
2013-10-07 08:26:48 +00:00
|
|
|
{
|
2014-05-11 17:57:04 +00:00
|
|
|
if (!gstate.isTextureMapEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-07 08:26:48 +00:00
|
|
|
GETextureFormat texfmt = gstate.getTextureFormat();
|
2014-06-15 16:30:37 +00:00
|
|
|
u32 texaddr = gstate.getTextureAddress(level);
|
2017-05-11 01:03:35 +00:00
|
|
|
int texbufw = GetTextureBufw(level, texaddr, texfmt);
|
2018-09-05 04:54:25 +00:00
|
|
|
int w = gstate.getTextureWidth(level);
|
|
|
|
int h = gstate.getTextureHeight(level);
|
|
|
|
|
|
|
|
if (!texaddr || !Memory::IsValidRange(texaddr, (textureBitsPerPixel[texfmt] * texbufw * h) / 8))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer.Allocate(w, h, GE_FORMAT_8888, false);
|
2013-10-07 08:26:48 +00:00
|
|
|
|
2017-05-21 23:16:03 +00:00
|
|
|
Sampler::Funcs sampler = Sampler::GetFuncs();
|
2017-05-11 00:31:34 +00:00
|
|
|
|
2018-09-05 04:54:25 +00:00
|
|
|
u8 *texptr = Memory::GetPointer(texaddr);
|
2013-10-07 08:26:48 +00:00
|
|
|
u32 *row = (u32 *)buffer.GetData();
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
2017-05-21 23:16:03 +00:00
|
|
|
row[x] = sampler.nearest(x, y, texptr, texbufw, level);
|
2013-10-07 08:26:48 +00:00
|
|
|
}
|
|
|
|
row += w;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-25 14:15:09 +00:00
|
|
|
} // namespace
|