2014-07-27 18:02:35 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-07-27 18:02:35 +02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <array>
|
2015-09-11 13:20:02 +02:00
|
|
|
#include <cmath>
|
2017-05-29 21:03:41 +02:00
|
|
|
#include <tuple>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bit_field.h"
|
2015-05-24 21:20:31 +02:00
|
|
|
#include "common/color.h"
|
2014-07-27 18:02:35 +02:00
|
|
|
#include "common/common_types.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/logging/log.h"
|
2015-02-11 21:39:43 +01:00
|
|
|
#include "common/math_util.h"
|
2015-08-17 23:25:21 +02:00
|
|
|
#include "common/microprofile.h"
|
2017-06-09 20:14:55 +02:00
|
|
|
#include "common/quaternion.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/vector_math.h"
|
2015-09-11 13:20:02 +02:00
|
|
|
#include "core/hw/gpu.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/memory.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/debug_utils/debug_utils.h"
|
2016-03-03 04:16:38 +01:00
|
|
|
#include "video_core/pica_state.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/pica_types.h"
|
2017-01-29 00:12:09 +01:00
|
|
|
#include "video_core/regs_framebuffer.h"
|
|
|
|
#include "video_core/regs_rasterizer.h"
|
|
|
|
#include "video_core/regs_texturing.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/shader/shader.h"
|
2017-01-30 02:43:09 +01:00
|
|
|
#include "video_core/swrasterizer/framebuffer.h"
|
2017-08-03 11:01:31 +02:00
|
|
|
#include "video_core/swrasterizer/lighting.h"
|
2017-04-17 09:01:45 +02:00
|
|
|
#include "video_core/swrasterizer/proctex.h"
|
2017-01-29 00:59:36 +01:00
|
|
|
#include "video_core/swrasterizer/rasterizer.h"
|
2017-01-30 04:22:19 +01:00
|
|
|
#include "video_core/swrasterizer/texturing.h"
|
2017-01-05 23:11:23 +01:00
|
|
|
#include "video_core/texture/texture_decode.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "video_core/utils.h"
|
2014-08-17 12:31:19 +02:00
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
namespace Pica {
|
|
|
|
namespace Rasterizer {
|
|
|
|
|
2014-12-31 15:19:40 +01:00
|
|
|
// NOTE: Assuming that rasterizer coordinates are 12.4 fixed-point values
|
|
|
|
struct Fix12P4 {
|
2016-09-19 03:01:46 +02:00
|
|
|
Fix12P4() {}
|
|
|
|
Fix12P4(u16 val) : val(val) {}
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
static u16 FracMask() {
|
|
|
|
return 0xF;
|
|
|
|
}
|
|
|
|
static u16 IntMask() {
|
|
|
|
return (u16)~0xF;
|
|
|
|
}
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2014-12-31 15:19:40 +01:00
|
|
|
operator u16() const {
|
|
|
|
return val;
|
|
|
|
}
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
bool operator<(const Fix12P4& oth) const {
|
|
|
|
return (u16) * this < (u16)oth;
|
2014-12-31 15:19:40 +01:00
|
|
|
}
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2014-12-31 15:19:40 +01:00
|
|
|
private:
|
|
|
|
u16 val;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate signed area of the triangle spanned by the three argument vertices.
|
|
|
|
* The sign denotes an orientation.
|
|
|
|
*
|
|
|
|
* @todo define orientation concretely.
|
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
static int SignedArea(const Math::Vec2<Fix12P4>& vtx1, const Math::Vec2<Fix12P4>& vtx2,
|
|
|
|
const Math::Vec2<Fix12P4>& vtx3) {
|
2014-12-31 15:19:40 +01:00
|
|
|
const auto vec1 = Math::MakeVec(vtx2 - vtx1, 0);
|
|
|
|
const auto vec2 = Math::MakeVec(vtx3 - vtx1, 0);
|
|
|
|
// TODO: There is a very small chance this will overflow for sizeof(int) == 4
|
|
|
|
return Math::Cross(vec1, vec2).z;
|
|
|
|
};
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2017-05-29 21:03:41 +02:00
|
|
|
/// Convert a 3D vector for cube map coordinates to 2D texture coordinates along with the face name
|
|
|
|
static std::tuple<float24, float24, PAddr> ConvertCubeCoord(float24 u, float24 v, float24 w,
|
|
|
|
const TexturingRegs& regs) {
|
|
|
|
const float abs_u = std::abs(u.ToFloat32());
|
|
|
|
const float abs_v = std::abs(v.ToFloat32());
|
|
|
|
const float abs_w = std::abs(w.ToFloat32());
|
|
|
|
float24 x, y, z;
|
|
|
|
PAddr addr;
|
|
|
|
if (abs_u > abs_v && abs_u > abs_w) {
|
|
|
|
if (u > float24::FromFloat32(0)) {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveX);
|
|
|
|
y = -v;
|
|
|
|
} else {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeX);
|
|
|
|
y = v;
|
|
|
|
}
|
|
|
|
x = -w;
|
|
|
|
z = u;
|
|
|
|
} else if (abs_v > abs_w) {
|
|
|
|
if (v > float24::FromFloat32(0)) {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveY);
|
|
|
|
x = u;
|
|
|
|
} else {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeY);
|
|
|
|
x = -u;
|
|
|
|
}
|
|
|
|
y = w;
|
|
|
|
z = v;
|
|
|
|
} else {
|
|
|
|
if (w > float24::FromFloat32(0)) {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::PositiveZ);
|
|
|
|
y = -v;
|
|
|
|
} else {
|
|
|
|
addr = regs.GetCubePhysicalAddress(TexturingRegs::CubeFace::NegativeZ);
|
|
|
|
y = v;
|
|
|
|
}
|
|
|
|
x = u;
|
|
|
|
z = w;
|
|
|
|
}
|
|
|
|
const float24 half = float24::FromFloat32(0.5f);
|
|
|
|
return std::make_tuple(x / z * half + half, y / z * half + half, addr);
|
|
|
|
}
|
|
|
|
|
2015-08-17 23:25:21 +02:00
|
|
|
MICROPROFILE_DEFINE(GPU_Rasterization, "GPU", "Rasterization", MP_RGB(50, 50, 240));
|
2015-05-12 20:50:17 +02:00
|
|
|
|
2015-02-18 13:14:49 +01:00
|
|
|
/**
|
|
|
|
* Helper function for ProcessTriangle with the "reversed" flag to allow for implementing
|
|
|
|
* culling via recursion.
|
|
|
|
*/
|
2016-12-19 08:43:37 +01:00
|
|
|
static void ProcessTriangleInternal(const Vertex& v0, const Vertex& v1, const Vertex& v2,
|
|
|
|
bool reversed = false) {
|
2015-05-14 05:29:27 +02:00
|
|
|
const auto& regs = g_state.regs;
|
2015-08-17 23:25:21 +02:00
|
|
|
MICROPROFILE_SCOPE(GPU_Rasterization);
|
2015-05-12 20:50:17 +02:00
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
// vertex positions in rasterizer coordinates
|
2015-02-18 12:14:40 +01:00
|
|
|
static auto FloatToFix = [](float24 flt) {
|
|
|
|
// TODO: Rounding here is necessary to prevent garbage pixels at
|
|
|
|
// triangle borders. Is it that the correct solution, though?
|
|
|
|
return Fix12P4(static_cast<unsigned short>(round(flt.ToFloat32() * 16.0f)));
|
|
|
|
};
|
|
|
|
static auto ScreenToRasterizerCoordinates = [](const Math::Vec3<float24>& vec) {
|
|
|
|
return Math::Vec3<Fix12P4>{FloatToFix(vec.x), FloatToFix(vec.y), FloatToFix(vec.z)};
|
|
|
|
};
|
2014-12-13 21:39:42 +01:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
Math::Vec3<Fix12P4> vtxpos[3]{ScreenToRasterizerCoordinates(v0.screenpos),
|
|
|
|
ScreenToRasterizerCoordinates(v1.screenpos),
|
|
|
|
ScreenToRasterizerCoordinates(v2.screenpos)};
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2017-01-28 05:16:36 +01:00
|
|
|
if (regs.rasterizer.cull_mode == RasterizerRegs::CullMode::KeepAll) {
|
2015-02-18 13:14:49 +01:00
|
|
|
// Make sure we always end up with a triangle wound counter-clockwise
|
|
|
|
if (!reversed && SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) <= 0) {
|
|
|
|
ProcessTriangleInternal(v0, v2, v1, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-28 05:16:36 +01:00
|
|
|
if (!reversed && regs.rasterizer.cull_mode == RasterizerRegs::CullMode::KeepClockWise) {
|
2015-02-18 13:14:49 +01:00
|
|
|
// Reverse vertex order and use the CCW code path.
|
|
|
|
ProcessTriangleInternal(v0, v2, v1, true);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-13 21:39:42 +01:00
|
|
|
|
2015-02-18 13:14:49 +01:00
|
|
|
// Cull away triangles which are wound clockwise.
|
2014-12-31 15:19:40 +01:00
|
|
|
if (SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) <= 0)
|
2014-12-13 21:39:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
u16 min_x = std::min({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x});
|
|
|
|
u16 min_y = std::min({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y});
|
|
|
|
u16 max_x = std::max({vtxpos[0].x, vtxpos[1].x, vtxpos[2].x});
|
|
|
|
u16 max_y = std::max({vtxpos[0].y, vtxpos[1].y, vtxpos[2].y});
|
|
|
|
|
2015-12-02 19:23:51 +01:00
|
|
|
// Convert the scissor box coordinates to 12.4 fixed point
|
2017-01-28 05:16:36 +01:00
|
|
|
u16 scissor_x1 = (u16)(regs.rasterizer.scissor_test.x1 << 4);
|
|
|
|
u16 scissor_y1 = (u16)(regs.rasterizer.scissor_test.y1 << 4);
|
2016-01-25 07:59:16 +01:00
|
|
|
// x2,y2 have +1 added to cover the entire sub-pixel area
|
2017-01-28 05:16:36 +01:00
|
|
|
u16 scissor_x2 = (u16)((regs.rasterizer.scissor_test.x2 + 1) << 4);
|
|
|
|
u16 scissor_y2 = (u16)((regs.rasterizer.scissor_test.y2 + 1) << 4);
|
2015-12-02 19:23:51 +01:00
|
|
|
|
2017-01-28 05:16:36 +01:00
|
|
|
if (regs.rasterizer.scissor_test.mode == RasterizerRegs::ScissorMode::Include) {
|
2015-12-02 19:23:51 +01:00
|
|
|
// Calculate the new bounds
|
2016-01-25 07:59:16 +01:00
|
|
|
min_x = std::max(min_x, scissor_x1);
|
|
|
|
min_y = std::max(min_y, scissor_y1);
|
|
|
|
max_x = std::min(max_x, scissor_x2);
|
|
|
|
max_y = std::min(max_y, scissor_y2);
|
2015-12-02 19:23:51 +01:00
|
|
|
}
|
|
|
|
|
2014-08-12 20:04:28 +02:00
|
|
|
min_x &= Fix12P4::IntMask();
|
|
|
|
min_y &= Fix12P4::IntMask();
|
|
|
|
max_x = ((max_x + Fix12P4::FracMask()) & Fix12P4::IntMask());
|
|
|
|
max_y = ((max_y + Fix12P4::FracMask()) & Fix12P4::IntMask());
|
2014-07-27 18:02:35 +02:00
|
|
|
|
|
|
|
// Triangle filling rules: Pixels on the right-sided edge or on flat bottom edges are not
|
|
|
|
// drawn. Pixels on any other triangle border are drawn. This is implemented with three bias
|
|
|
|
// values which are added to the barycentric coordinates w0, w1 and w2, respectively.
|
|
|
|
// NOTE: These are the PSP filling rules. Not sure if the 3DS uses the same ones...
|
|
|
|
auto IsRightSideOrFlatBottomEdge = [](const Math::Vec2<Fix12P4>& vtx,
|
|
|
|
const Math::Vec2<Fix12P4>& line1,
|
2016-09-18 02:38:01 +02:00
|
|
|
const Math::Vec2<Fix12P4>& line2) {
|
2014-07-27 18:02:35 +02:00
|
|
|
if (line1.y == line2.y) {
|
|
|
|
// just check if vertex is above us => bottom line parallel to x-axis
|
|
|
|
return vtx.y < line1.y;
|
|
|
|
} else {
|
|
|
|
// check if vertex is on our left => right side
|
|
|
|
// TODO: Not sure how likely this is to overflow
|
2016-09-18 02:38:01 +02:00
|
|
|
return (int)vtx.x < (int)line1.x +
|
|
|
|
((int)line2.x - (int)line1.x) * ((int)vtx.y - (int)line1.y) /
|
|
|
|
((int)line2.y - (int)line1.y);
|
2014-07-27 18:02:35 +02:00
|
|
|
}
|
|
|
|
};
|
2016-09-18 02:38:01 +02:00
|
|
|
int bias0 =
|
|
|
|
IsRightSideOrFlatBottomEdge(vtxpos[0].xy(), vtxpos[1].xy(), vtxpos[2].xy()) ? -1 : 0;
|
|
|
|
int bias1 =
|
|
|
|
IsRightSideOrFlatBottomEdge(vtxpos[1].xy(), vtxpos[2].xy(), vtxpos[0].xy()) ? -1 : 0;
|
|
|
|
int bias2 =
|
|
|
|
IsRightSideOrFlatBottomEdge(vtxpos[2].xy(), vtxpos[0].xy(), vtxpos[1].xy()) ? -1 : 0;
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2014-12-23 16:05:51 +01:00
|
|
|
auto w_inverse = Math::MakeVec(v0.pos.w, v1.pos.w, v2.pos.w);
|
2014-12-23 13:59:07 +01:00
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
auto textures = regs.texturing.GetTextures();
|
|
|
|
auto tev_stages = regs.texturing.GetTevStages();
|
2014-12-23 13:59:07 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
bool stencil_action_enable =
|
|
|
|
g_state.regs.framebuffer.output_merger.stencil_test.enable &&
|
|
|
|
g_state.regs.framebuffer.framebuffer.depth_format == FramebufferRegs::DepthFormat::D24S8;
|
|
|
|
const auto stencil_test = g_state.regs.framebuffer.output_merger.stencil_test;
|
2015-05-21 16:51:31 +02:00
|
|
|
|
2015-01-02 01:02:18 +01:00
|
|
|
// Enter rasterization loop, starting at the center of the topleft bounding box corner.
|
2014-07-27 18:02:35 +02:00
|
|
|
// TODO: Not sure if looping through x first might be faster
|
2015-01-02 01:02:18 +01:00
|
|
|
for (u16 y = min_y + 8; y < max_y; y += 0x10) {
|
|
|
|
for (u16 x = min_x + 8; x < max_x; x += 0x10) {
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
// Do not process the pixel if it's inside the scissor box and the scissor mode is set
|
|
|
|
// to Exclude
|
2017-01-28 05:16:36 +01:00
|
|
|
if (regs.rasterizer.scissor_test.mode == RasterizerRegs::ScissorMode::Exclude) {
|
2016-09-18 02:38:01 +02:00
|
|
|
if (x >= scissor_x1 && x < scissor_x2 && y >= scissor_y1 && y < scissor_y2)
|
2016-01-25 07:59:16 +01:00
|
|
|
continue;
|
2015-12-02 19:23:51 +01:00
|
|
|
}
|
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
// Calculate the barycentric coordinates w0, w1 and w2
|
2014-12-31 15:19:40 +01:00
|
|
|
int w0 = bias0 + SignedArea(vtxpos[1].xy(), vtxpos[2].xy(), {x, y});
|
|
|
|
int w1 = bias1 + SignedArea(vtxpos[2].xy(), vtxpos[0].xy(), {x, y});
|
|
|
|
int w2 = bias2 + SignedArea(vtxpos[0].xy(), vtxpos[1].xy(), {x, y});
|
2014-07-27 18:02:35 +02:00
|
|
|
int wsum = w0 + w1 + w2;
|
|
|
|
|
|
|
|
// If current pixel is not covered by the current primitive
|
|
|
|
if (w0 < 0 || w1 < 0 || w2 < 0)
|
|
|
|
continue;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto baricentric_coordinates =
|
|
|
|
Math::MakeVec(float24::FromFloat32(static_cast<float>(w0)),
|
|
|
|
float24::FromFloat32(static_cast<float>(w1)),
|
|
|
|
float24::FromFloat32(static_cast<float>(w2)));
|
|
|
|
float24 interpolated_w_inverse =
|
|
|
|
float24::FromFloat32(1.0f) / Math::Dot(w_inverse, baricentric_coordinates);
|
2014-12-23 13:59:07 +01:00
|
|
|
|
2016-05-11 13:39:56 +02:00
|
|
|
// interpolated_z = z / w
|
2016-09-18 02:38:01 +02:00
|
|
|
float interpolated_z_over_w =
|
|
|
|
(v0.screenpos[2].ToFloat32() * w0 + v1.screenpos[2].ToFloat32() * w1 +
|
|
|
|
v2.screenpos[2].ToFloat32() * w2) /
|
|
|
|
wsum;
|
2016-05-11 13:39:56 +02:00
|
|
|
|
|
|
|
// Not fully accurate. About 3 bits in precision are missing.
|
|
|
|
// Z-Buffer (z / w * scale + offset)
|
2017-01-28 05:16:36 +01:00
|
|
|
float depth_scale = float24::FromRaw(regs.rasterizer.viewport_depth_range).ToFloat32();
|
|
|
|
float depth_offset =
|
|
|
|
float24::FromRaw(regs.rasterizer.viewport_depth_near_plane).ToFloat32();
|
2016-05-11 13:39:56 +02:00
|
|
|
float depth = interpolated_z_over_w * depth_scale + depth_offset;
|
|
|
|
|
|
|
|
// Potentially switch to W-Buffer
|
2017-01-28 05:16:36 +01:00
|
|
|
if (regs.rasterizer.depthmap_enable ==
|
|
|
|
Pica::RasterizerRegs::DepthBuffering::WBuffering) {
|
2016-05-11 13:39:56 +02:00
|
|
|
// W-Buffer (z * scale + w * offset = (z / w * scale + offset) * w)
|
|
|
|
depth *= interpolated_w_inverse.ToFloat32() * wsum;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp the result
|
|
|
|
depth = MathUtil::Clamp(depth, 0.0f, 1.0f);
|
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
// Perspective correct attribute interpolation:
|
|
|
|
// Attribute values cannot be calculated by simple linear interpolation since
|
|
|
|
// they are not linear in screen space. For example, when interpolating a
|
|
|
|
// texture coordinate across two vertices, something simple like
|
|
|
|
// u = (u0*w0 + u1*w1)/(w0+w1)
|
|
|
|
// will not work. However, the attribute value divided by the
|
|
|
|
// clipspace w-coordinate (u/w) and and the inverse w-coordinate (1/w) are linear
|
|
|
|
// in screenspace. Hence, we can linearly interpolate these two independently and
|
|
|
|
// calculate the interpolated attribute by dividing the results.
|
|
|
|
// I.e.
|
|
|
|
// u_over_w = ((u0/v0.pos.w)*w0 + (u1/v1.pos.w)*w1)/(w0+w1)
|
|
|
|
// one_over_w = (( 1/v0.pos.w)*w0 + ( 1/v1.pos.w)*w1)/(w0+w1)
|
|
|
|
// u = u_over_w / one_over_w
|
|
|
|
//
|
|
|
|
// The generalization to three vertices is straightforward in baricentric coordinates.
|
|
|
|
auto GetInterpolatedAttribute = [&](float24 attr0, float24 attr1, float24 attr2) {
|
2014-12-23 16:05:51 +01:00
|
|
|
auto attr_over_w = Math::MakeVec(attr0, attr1, attr2);
|
2014-07-27 18:02:35 +02:00
|
|
|
float24 interpolated_attr_over_w = Math::Dot(attr_over_w, baricentric_coordinates);
|
2014-12-23 13:59:07 +01:00
|
|
|
return interpolated_attr_over_w * interpolated_w_inverse;
|
2014-07-27 18:02:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Math::Vec4<u8> primary_color{
|
2017-11-09 02:42:25 +01:00
|
|
|
static_cast<u8>(round(
|
2016-09-18 02:38:01 +02:00
|
|
|
GetInterpolatedAttribute(v0.color.r(), v1.color.r(), v2.color.r()).ToFloat32() *
|
2017-11-09 02:42:25 +01:00
|
|
|
255)),
|
|
|
|
static_cast<u8>(round(
|
2016-09-18 02:38:01 +02:00
|
|
|
GetInterpolatedAttribute(v0.color.g(), v1.color.g(), v2.color.g()).ToFloat32() *
|
2017-11-09 02:42:25 +01:00
|
|
|
255)),
|
|
|
|
static_cast<u8>(round(
|
2016-09-18 02:38:01 +02:00
|
|
|
GetInterpolatedAttribute(v0.color.b(), v1.color.b(), v2.color.b()).ToFloat32() *
|
2017-11-09 02:42:25 +01:00
|
|
|
255)),
|
|
|
|
static_cast<u8>(round(
|
2016-09-18 02:38:01 +02:00
|
|
|
GetInterpolatedAttribute(v0.color.a(), v1.color.a(), v2.color.a()).ToFloat32() *
|
2017-11-09 02:42:25 +01:00
|
|
|
255)),
|
2016-09-19 03:01:46 +02:00
|
|
|
};
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2014-12-06 19:10:08 +01:00
|
|
|
Math::Vec2<float24> uv[3];
|
|
|
|
uv[0].u() = GetInterpolatedAttribute(v0.tc0.u(), v1.tc0.u(), v2.tc0.u());
|
|
|
|
uv[0].v() = GetInterpolatedAttribute(v0.tc0.v(), v1.tc0.v(), v2.tc0.v());
|
|
|
|
uv[1].u() = GetInterpolatedAttribute(v0.tc1.u(), v1.tc1.u(), v2.tc1.u());
|
|
|
|
uv[1].v() = GetInterpolatedAttribute(v0.tc1.v(), v1.tc1.v(), v2.tc1.v());
|
|
|
|
uv[2].u() = GetInterpolatedAttribute(v0.tc2.u(), v1.tc2.u(), v2.tc2.u());
|
|
|
|
uv[2].v() = GetInterpolatedAttribute(v0.tc2.v(), v1.tc2.v(), v2.tc2.v());
|
|
|
|
|
2017-04-17 09:01:45 +02:00
|
|
|
Math::Vec4<u8> texture_color[4]{};
|
2014-12-06 19:10:08 +01:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2014-12-23 13:59:07 +01:00
|
|
|
const auto& texture = textures[i];
|
2014-12-06 19:10:08 +01:00
|
|
|
if (!texture.enabled)
|
|
|
|
continue;
|
|
|
|
|
2015-01-21 02:16:47 +01:00
|
|
|
DEBUG_ASSERT(0 != texture.config.address);
|
2014-12-06 19:10:08 +01:00
|
|
|
|
2017-05-05 14:29:35 +02:00
|
|
|
int coordinate_i =
|
|
|
|
(i == 2 && regs.texturing.main_config.texture2_use_coord1) ? 1 : i;
|
2017-05-03 18:59:48 +02:00
|
|
|
float24 u = uv[coordinate_i].u();
|
|
|
|
float24 v = uv[coordinate_i].v();
|
2016-04-19 00:53:28 +02:00
|
|
|
|
|
|
|
// Only unit 0 respects the texturing type (according to 3DBrew)
|
|
|
|
// TODO: Refactor so cubemaps and shadowmaps can be handled
|
2017-05-29 21:03:41 +02:00
|
|
|
PAddr texture_address = texture.config.GetPhysicalAddress();
|
2016-04-19 00:53:28 +02:00
|
|
|
if (i == 0) {
|
2016-09-18 02:38:01 +02:00
|
|
|
switch (texture.config.type) {
|
2017-01-28 05:51:59 +01:00
|
|
|
case TexturingRegs::TextureConfig::Texture2D:
|
2016-04-19 00:53:28 +02:00
|
|
|
break;
|
2017-05-29 21:03:41 +02:00
|
|
|
case TexturingRegs::TextureConfig::TextureCube: {
|
|
|
|
auto w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w);
|
|
|
|
std::tie(u, v, texture_address) = ConvertCubeCoord(u, v, w, regs.texturing);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-28 05:51:59 +01:00
|
|
|
case TexturingRegs::TextureConfig::Projection2D: {
|
2016-04-19 00:53:28 +02:00
|
|
|
auto tc0_w = GetInterpolatedAttribute(v0.tc0_w, v1.tc0_w, v2.tc0_w);
|
|
|
|
u /= tc0_w;
|
|
|
|
v /= tc0_w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// TODO: Change to LOG_ERROR when more types are handled.
|
|
|
|
LOG_DEBUG(HW_GPU, "Unhandled texture type %x", (int)texture.config.type);
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
int s = (int)(u * float24::FromFloat32(static_cast<float>(texture.config.width)))
|
|
|
|
.ToFloat32();
|
|
|
|
int t = (int)(v * float24::FromFloat32(static_cast<float>(texture.config.height)))
|
|
|
|
.ToFloat32();
|
2016-04-19 00:53:28 +02:00
|
|
|
|
2017-06-01 15:56:46 +02:00
|
|
|
bool use_border_s = false;
|
|
|
|
bool use_border_t = false;
|
|
|
|
|
|
|
|
if (texture.config.wrap_s == TexturingRegs::TextureConfig::ClampToBorder) {
|
|
|
|
use_border_s = s < 0 || s >= static_cast<int>(texture.config.width);
|
|
|
|
} else if (texture.config.wrap_s == TexturingRegs::TextureConfig::ClampToBorder2) {
|
|
|
|
use_border_s = s >= static_cast<int>(texture.config.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture.config.wrap_t == TexturingRegs::TextureConfig::ClampToBorder) {
|
|
|
|
use_border_t = t < 0 || t >= static_cast<int>(texture.config.height);
|
|
|
|
} else if (texture.config.wrap_t == TexturingRegs::TextureConfig::ClampToBorder2) {
|
|
|
|
use_border_t = t >= static_cast<int>(texture.config.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_border_s || use_border_t) {
|
2015-07-07 23:36:06 +02:00
|
|
|
auto border_color = texture.config.border_color;
|
2017-10-25 16:03:21 +02:00
|
|
|
texture_color[i] = Math::MakeVec(border_color.r.Value(), border_color.g.Value(),
|
|
|
|
border_color.b.Value(), border_color.a.Value())
|
|
|
|
.Cast<u8>();
|
2015-07-07 23:36:06 +02:00
|
|
|
} else {
|
|
|
|
// Textures are laid out from bottom to top, hence we invert the t coordinate.
|
|
|
|
// NOTE: This may not be the right place for the inversion.
|
|
|
|
// TODO: Check if this applies to ETC textures, too.
|
|
|
|
s = GetWrappedTexCoord(texture.config.wrap_s, s, texture.config.width);
|
2016-09-18 02:38:01 +02:00
|
|
|
t = texture.config.height - 1 -
|
|
|
|
GetWrappedTexCoord(texture.config.wrap_t, t, texture.config.height);
|
2015-07-07 23:36:06 +02:00
|
|
|
|
2017-05-29 21:03:41 +02:00
|
|
|
const u8* texture_data = Memory::GetPhysicalPointer(texture_address);
|
2016-09-18 02:38:01 +02:00
|
|
|
auto info =
|
2017-01-05 23:11:23 +01:00
|
|
|
Texture::TextureInfo::FromPicaRegister(texture.config, texture.format);
|
2015-07-07 23:36:06 +02:00
|
|
|
|
2015-07-21 23:26:09 +02:00
|
|
|
// TODO: Apply the min and mag filters to the texture
|
2017-01-05 23:11:23 +01:00
|
|
|
texture_color[i] = Texture::LookupTexture(texture_data, s, t, info);
|
2015-07-26 11:55:47 +02:00
|
|
|
#if PICA_DUMP_TEXTURES
|
2015-07-07 23:36:06 +02:00
|
|
|
DebugUtils::DumpTexture(texture.config, texture_data);
|
2015-07-26 11:55:47 +02:00
|
|
|
#endif
|
2015-07-07 23:36:06 +02:00
|
|
|
}
|
2014-08-17 12:31:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 09:01:45 +02:00
|
|
|
// sample procedural texture
|
|
|
|
if (regs.texturing.main_config.texture3_enable) {
|
|
|
|
const auto& proctex_uv = uv[regs.texturing.main_config.texture3_coordinates];
|
|
|
|
texture_color[3] = ProcTex(proctex_uv.u().ToFloat32(), proctex_uv.v().ToFloat32(),
|
|
|
|
g_state.regs.texturing, g_state.proctex);
|
|
|
|
}
|
|
|
|
|
2014-08-15 16:33:17 +02:00
|
|
|
// Texture environment - consists of 6 stages of color and alpha combining.
|
|
|
|
//
|
|
|
|
// Color combiners take three input color values from some source (e.g. interpolated
|
|
|
|
// vertex color, texture color, previous stage, etc), perform some very simple
|
|
|
|
// operations on each of them (e.g. inversion) and then calculate the output color
|
|
|
|
// with some basic arithmetic. Alpha combiners can be configured separately but work
|
|
|
|
// analogously.
|
|
|
|
Math::Vec4<u8> combiner_output;
|
2015-12-01 07:33:38 +01:00
|
|
|
Math::Vec4<u8> combiner_buffer = {0, 0, 0, 0};
|
2017-10-25 16:03:21 +02:00
|
|
|
Math::Vec4<u8> next_combiner_buffer =
|
|
|
|
Math::MakeVec(regs.texturing.tev_combiner_buffer_color.r.Value(),
|
|
|
|
regs.texturing.tev_combiner_buffer_color.g.Value(),
|
|
|
|
regs.texturing.tev_combiner_buffer_color.b.Value(),
|
|
|
|
regs.texturing.tev_combiner_buffer_color.a.Value())
|
|
|
|
.Cast<u8>();
|
2015-05-07 04:18:11 +02:00
|
|
|
|
2017-06-28 19:37:14 +02:00
|
|
|
Math::Vec4<u8> primary_fragment_color = {0, 0, 0, 0};
|
|
|
|
Math::Vec4<u8> secondary_fragment_color = {0, 0, 0, 0};
|
2017-06-09 20:14:55 +02:00
|
|
|
|
2017-06-28 19:37:14 +02:00
|
|
|
if (!g_state.regs.lighting.disable) {
|
2017-08-02 21:05:53 +02:00
|
|
|
Math::Quaternion<float> normquat = Math::Quaternion<float>{
|
2017-07-11 19:15:23 +02:00
|
|
|
{GetInterpolatedAttribute(v0.quat.x, v1.quat.x, v2.quat.x).ToFloat32(),
|
|
|
|
GetInterpolatedAttribute(v0.quat.y, v1.quat.y, v2.quat.y).ToFloat32(),
|
|
|
|
GetInterpolatedAttribute(v0.quat.z, v1.quat.z, v2.quat.z).ToFloat32()},
|
|
|
|
GetInterpolatedAttribute(v0.quat.w, v1.quat.w, v2.quat.w).ToFloat32(),
|
2017-08-02 21:05:53 +02:00
|
|
|
}.Normalized();
|
2017-07-11 19:15:23 +02:00
|
|
|
|
|
|
|
Math::Vec3<float> view{
|
|
|
|
GetInterpolatedAttribute(v0.view.x, v1.view.x, v2.view.x).ToFloat32(),
|
|
|
|
GetInterpolatedAttribute(v0.view.y, v1.view.y, v2.view.y).ToFloat32(),
|
|
|
|
GetInterpolatedAttribute(v0.view.z, v1.view.z, v2.view.z).ToFloat32(),
|
|
|
|
};
|
2017-08-18 14:04:56 +02:00
|
|
|
std::tie(primary_fragment_color, secondary_fragment_color) = ComputeFragmentsColors(
|
|
|
|
g_state.regs.lighting, g_state.lighting, normquat, view, texture_color);
|
2017-06-28 19:37:14 +02:00
|
|
|
}
|
2017-06-09 20:14:55 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size();
|
|
|
|
++tev_stage_index) {
|
2015-05-07 04:18:11 +02:00
|
|
|
const auto& tev_stage = tev_stages[tev_stage_index];
|
2017-01-28 05:51:59 +01:00
|
|
|
using Source = TexturingRegs::TevStageConfig::Source;
|
2014-08-15 16:33:17 +02:00
|
|
|
|
2015-02-04 00:32:29 +01:00
|
|
|
auto GetSource = [&](Source source) -> Math::Vec4<u8> {
|
2014-08-15 16:33:17 +02:00
|
|
|
switch (source) {
|
|
|
|
case Source::PrimaryColor:
|
2017-06-09 20:14:55 +02:00
|
|
|
return primary_color;
|
2015-05-26 00:45:05 +02:00
|
|
|
|
2015-01-03 13:49:53 +01:00
|
|
|
case Source::PrimaryFragmentColor:
|
2017-06-09 20:14:55 +02:00
|
|
|
return primary_fragment_color;
|
2014-08-15 16:33:17 +02:00
|
|
|
|
2015-05-26 00:45:05 +02:00
|
|
|
case Source::SecondaryFragmentColor:
|
2017-06-09 20:14:55 +02:00
|
|
|
return secondary_fragment_color;
|
2015-05-26 00:45:05 +02:00
|
|
|
|
2014-08-17 12:31:19 +02:00
|
|
|
case Source::Texture0:
|
2014-10-24 00:58:04 +02:00
|
|
|
return texture_color[0];
|
2014-12-06 19:10:08 +01:00
|
|
|
|
|
|
|
case Source::Texture1:
|
2014-10-24 00:58:04 +02:00
|
|
|
return texture_color[1];
|
2014-12-06 19:10:08 +01:00
|
|
|
|
|
|
|
case Source::Texture2:
|
2014-10-24 00:58:04 +02:00
|
|
|
return texture_color[2];
|
2014-08-17 12:31:19 +02:00
|
|
|
|
2017-04-17 09:01:45 +02:00
|
|
|
case Source::Texture3:
|
|
|
|
return texture_color[3];
|
|
|
|
|
2015-05-07 04:18:11 +02:00
|
|
|
case Source::PreviousBuffer:
|
|
|
|
return combiner_buffer;
|
|
|
|
|
2014-08-15 16:33:17 +02:00
|
|
|
case Source::Constant:
|
2017-10-25 16:03:21 +02:00
|
|
|
return Math::MakeVec(tev_stage.const_r.Value(), tev_stage.const_g.Value(),
|
|
|
|
tev_stage.const_b.Value(), tev_stage.const_a.Value())
|
|
|
|
.Cast<u8>();
|
2014-08-15 16:33:17 +02:00
|
|
|
|
|
|
|
case Source::Previous:
|
2014-10-24 00:58:04 +02:00
|
|
|
return combiner_output;
|
2014-08-15 16:33:17 +02:00
|
|
|
|
|
|
|
default:
|
2015-10-09 23:13:54 +02:00
|
|
|
LOG_ERROR(HW_GPU, "Unknown color combiner source %d", (int)source);
|
2015-01-21 02:16:47 +01:00
|
|
|
UNIMPLEMENTED();
|
2015-05-07 03:45:10 +02:00
|
|
|
return {0, 0, 0, 0};
|
2014-08-15 16:33:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// color combiner
|
|
|
|
// NOTE: Not sure if the alpha combiner might use the color output of the previous
|
|
|
|
// stage as input. Hence, we currently don't directly write the result to
|
|
|
|
// combiner_output.rgb(), but instead store it in a temporary variable until
|
|
|
|
// alpha combining has been done.
|
|
|
|
Math::Vec3<u8> color_result[3] = {
|
2015-02-04 00:32:29 +01:00
|
|
|
GetColorModifier(tev_stage.color_modifier1, GetSource(tev_stage.color_source1)),
|
|
|
|
GetColorModifier(tev_stage.color_modifier2, GetSource(tev_stage.color_source2)),
|
2016-09-19 03:01:46 +02:00
|
|
|
GetColorModifier(tev_stage.color_modifier3, GetSource(tev_stage.color_source3)),
|
|
|
|
};
|
2014-08-15 16:33:17 +02:00
|
|
|
auto color_output = ColorCombine(tev_stage.color_op, color_result);
|
|
|
|
|
2017-04-19 22:48:10 +02:00
|
|
|
u8 alpha_output;
|
|
|
|
if (tev_stage.color_op == TexturingRegs::TevStageConfig::Operation::Dot3_RGBA) {
|
|
|
|
// result of Dot3_RGBA operation is also placed to the alpha component
|
|
|
|
alpha_output = color_output.x;
|
|
|
|
} else {
|
|
|
|
// alpha combiner
|
|
|
|
std::array<u8, 3> alpha_result = {{
|
|
|
|
GetAlphaModifier(tev_stage.alpha_modifier1,
|
|
|
|
GetSource(tev_stage.alpha_source1)),
|
|
|
|
GetAlphaModifier(tev_stage.alpha_modifier2,
|
|
|
|
GetSource(tev_stage.alpha_source2)),
|
|
|
|
GetAlphaModifier(tev_stage.alpha_modifier3,
|
|
|
|
GetSource(tev_stage.alpha_source3)),
|
|
|
|
}};
|
|
|
|
alpha_output = AlphaCombine(tev_stage.alpha_op, alpha_result);
|
|
|
|
}
|
2014-08-15 16:33:17 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
combiner_output[0] =
|
|
|
|
std::min((unsigned)255, color_output.r() * tev_stage.GetColorMultiplier());
|
|
|
|
combiner_output[1] =
|
|
|
|
std::min((unsigned)255, color_output.g() * tev_stage.GetColorMultiplier());
|
|
|
|
combiner_output[2] =
|
|
|
|
std::min((unsigned)255, color_output.b() * tev_stage.GetColorMultiplier());
|
|
|
|
combiner_output[3] =
|
|
|
|
std::min((unsigned)255, alpha_output * tev_stage.GetAlphaMultiplier());
|
2015-05-07 04:18:11 +02:00
|
|
|
|
2015-12-01 07:33:38 +01:00
|
|
|
combiner_buffer = next_combiner_buffer;
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
if (regs.texturing.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferColor(
|
2016-09-18 02:38:01 +02:00
|
|
|
tev_stage_index)) {
|
2015-12-01 07:33:38 +01:00
|
|
|
next_combiner_buffer.r() = combiner_output.r();
|
|
|
|
next_combiner_buffer.g() = combiner_output.g();
|
|
|
|
next_combiner_buffer.b() = combiner_output.b();
|
2015-05-07 04:18:11 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
if (regs.texturing.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferAlpha(
|
2016-09-18 02:38:01 +02:00
|
|
|
tev_stage_index)) {
|
2015-12-01 07:33:38 +01:00
|
|
|
next_combiner_buffer.a() = combiner_output.a();
|
2015-05-07 04:18:11 +02:00
|
|
|
}
|
2014-08-15 16:33:17 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
const auto& output_merger = regs.framebuffer.output_merger;
|
2015-05-21 16:51:31 +02:00
|
|
|
// TODO: Does alpha testing happen before or after stencil?
|
2015-05-14 05:29:27 +02:00
|
|
|
if (output_merger.alpha_test.enable) {
|
2015-01-27 03:10:09 +01:00
|
|
|
bool pass = false;
|
|
|
|
|
2015-05-14 05:29:27 +02:00
|
|
|
switch (output_merger.alpha_test.func) {
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Never:
|
2015-01-27 03:10:09 +01:00
|
|
|
pass = false;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Always:
|
2015-01-27 03:10:09 +01:00
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Equal:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() == output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::NotEqual:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() != output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThan:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() < output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThanOrEqual:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() <= output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThan:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() > output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThanOrEqual:
|
2015-05-14 05:29:27 +02:00
|
|
|
pass = combiner_output.a() >= output_merger.alpha_test.ref;
|
2015-01-27 03:10:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pass)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:39:56 +02:00
|
|
|
// Apply fog combiner
|
|
|
|
// Not fully accurate. We'd have to know what data type is used to
|
|
|
|
// store the depth etc. Using float for now until we know more
|
|
|
|
// about Pica datatypes
|
2017-01-28 05:51:59 +01:00
|
|
|
if (regs.texturing.fog_mode == TexturingRegs::FogMode::Fog) {
|
2017-10-25 16:03:21 +02:00
|
|
|
const Math::Vec3<u8> fog_color = Math::MakeVec(regs.texturing.fog_color.r.Value(),
|
|
|
|
regs.texturing.fog_color.g.Value(),
|
|
|
|
regs.texturing.fog_color.b.Value())
|
|
|
|
.Cast<u8>();
|
2016-05-11 13:39:56 +02:00
|
|
|
|
|
|
|
// Get index into fog LUT
|
|
|
|
float fog_index;
|
2017-01-28 05:51:59 +01:00
|
|
|
if (g_state.regs.texturing.fog_flip) {
|
2016-05-11 13:39:56 +02:00
|
|
|
fog_index = (1.0f - depth) * 128.0f;
|
|
|
|
} else {
|
|
|
|
fog_index = depth * 128.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate clamped fog factor from LUT for given fog index
|
|
|
|
float fog_i = MathUtil::Clamp(floorf(fog_index), 0.0f, 127.0f);
|
|
|
|
float fog_f = fog_index - fog_i;
|
|
|
|
const auto& fog_lut_entry = g_state.fog.lut[static_cast<unsigned int>(fog_i)];
|
2017-06-22 16:22:45 +02:00
|
|
|
float fog_factor = fog_lut_entry.ToFloat() + fog_lut_entry.DiffToFloat() * fog_f;
|
2016-05-11 13:39:56 +02:00
|
|
|
fog_factor = MathUtil::Clamp(fog_factor, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
// Blend the fog
|
|
|
|
for (unsigned i = 0; i < 3; i++) {
|
2016-11-30 15:37:37 +01:00
|
|
|
combiner_output[i] = static_cast<u8>(fog_factor * combiner_output[i] +
|
|
|
|
(1.0f - fog_factor) * fog_color[i]);
|
2016-05-11 13:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:51:31 +02:00
|
|
|
u8 old_stencil = 0;
|
2015-08-20 17:31:39 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto UpdateStencil = [stencil_test, x, y,
|
2017-01-28 06:47:34 +01:00
|
|
|
&old_stencil](Pica::FramebufferRegs::StencilAction action) {
|
2016-09-18 02:38:01 +02:00
|
|
|
u8 new_stencil =
|
|
|
|
PerformStencilAction(action, old_stencil, stencil_test.reference_value);
|
2017-01-28 06:47:34 +01:00
|
|
|
if (g_state.regs.framebuffer.framebuffer.allow_depth_stencil_write != 0)
|
2016-09-18 02:38:01 +02:00
|
|
|
SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) |
|
|
|
|
(old_stencil & ~stencil_test.write_mask));
|
2015-08-20 17:31:39 +02:00
|
|
|
};
|
|
|
|
|
2015-05-21 16:51:31 +02:00
|
|
|
if (stencil_action_enable) {
|
|
|
|
old_stencil = GetStencil(x >> 4, y >> 4);
|
2015-08-20 17:10:35 +02:00
|
|
|
u8 dest = old_stencil & stencil_test.input_mask;
|
|
|
|
u8 ref = stencil_test.reference_value & stencil_test.input_mask;
|
2015-05-21 16:51:31 +02:00
|
|
|
|
|
|
|
bool pass = false;
|
|
|
|
switch (stencil_test.func) {
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Never:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = false;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Always:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Equal:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref == dest);
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::NotEqual:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref != dest);
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThan:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref < dest);
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThanOrEqual:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref <= dest);
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThan:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref > dest);
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThanOrEqual:
|
2015-05-21 16:51:31 +02:00
|
|
|
pass = (ref >= dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pass) {
|
2015-08-20 17:31:39 +02:00
|
|
|
UpdateStencil(stencil_test.action_stencil_fail);
|
2015-05-21 16:51:31 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 01:53:49 +01:00
|
|
|
// Convert float to integer
|
2017-01-28 06:47:34 +01:00
|
|
|
unsigned num_bits =
|
|
|
|
FramebufferRegs::DepthBitsPerPixel(regs.framebuffer.framebuffer.depth_format);
|
2016-03-20 01:53:49 +01:00
|
|
|
u32 z = (u32)(depth * ((1 << num_bits) - 1));
|
2016-03-05 23:18:20 +01:00
|
|
|
|
2015-05-14 05:29:27 +02:00
|
|
|
if (output_merger.depth_test_enable) {
|
2015-03-08 18:05:17 +01:00
|
|
|
u32 ref_z = GetDepth(x >> 4, y >> 4);
|
2014-12-21 02:59:08 +01:00
|
|
|
|
|
|
|
bool pass = false;
|
|
|
|
|
2015-05-14 05:29:27 +02:00
|
|
|
switch (output_merger.depth_test_func) {
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Never:
|
2015-01-26 05:34:14 +01:00
|
|
|
pass = false;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Always:
|
2014-12-21 02:59:08 +01:00
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::Equal:
|
2015-01-26 05:34:14 +01:00
|
|
|
pass = z == ref_z;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::NotEqual:
|
2015-01-26 05:34:14 +01:00
|
|
|
pass = z != ref_z;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThan:
|
2014-12-21 02:59:08 +01:00
|
|
|
pass = z < ref_z;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::LessThanOrEqual:
|
2015-01-26 05:34:14 +01:00
|
|
|
pass = z <= ref_z;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThan:
|
2014-12-21 02:59:08 +01:00
|
|
|
pass = z > ref_z;
|
|
|
|
break;
|
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::CompareFunc::GreaterThanOrEqual:
|
2015-01-26 05:34:14 +01:00
|
|
|
pass = z >= ref_z;
|
|
|
|
break;
|
2014-12-21 02:59:08 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 16:51:31 +02:00
|
|
|
if (!pass) {
|
2015-08-21 16:48:43 +02:00
|
|
|
if (stencil_action_enable)
|
2015-08-20 17:31:39 +02:00
|
|
|
UpdateStencil(stencil_test.action_depth_fail);
|
2014-12-21 02:59:08 +01:00
|
|
|
continue;
|
2015-05-21 16:51:31 +02:00
|
|
|
}
|
2014-12-21 02:59:08 +01:00
|
|
|
}
|
2014-07-27 18:02:35 +02:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
if (regs.framebuffer.framebuffer.allow_depth_stencil_write != 0 &&
|
|
|
|
output_merger.depth_write_enable) {
|
|
|
|
|
2016-03-05 23:18:20 +01:00
|
|
|
SetDepth(x >> 4, y >> 4, z);
|
2017-01-28 06:47:34 +01:00
|
|
|
}
|
2016-03-05 23:18:20 +01:00
|
|
|
|
2015-08-21 16:48:43 +02:00
|
|
|
// The stencil depth_pass action is executed even if depth testing is disabled
|
|
|
|
if (stencil_action_enable)
|
|
|
|
UpdateStencil(stencil_test.action_depth_pass);
|
|
|
|
|
2014-12-21 03:00:25 +01:00
|
|
|
auto dest = GetPixel(x >> 4, y >> 4);
|
2015-02-11 21:39:43 +01:00
|
|
|
Math::Vec4<u8> blend_output = combiner_output;
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2015-05-14 05:29:27 +02:00
|
|
|
if (output_merger.alphablend_enable) {
|
|
|
|
auto params = output_merger.alpha_blending;
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
auto LookupFactor = [&](unsigned channel,
|
|
|
|
FramebufferRegs::BlendFactor factor) -> u8 {
|
2016-04-02 03:26:10 +02:00
|
|
|
DEBUG_ASSERT(channel < 4);
|
|
|
|
|
2017-10-25 16:03:21 +02:00
|
|
|
const Math::Vec4<u8> blend_const =
|
|
|
|
Math::MakeVec(output_merger.blend_const.r.Value(),
|
|
|
|
output_merger.blend_const.g.Value(),
|
|
|
|
output_merger.blend_const.b.Value(),
|
|
|
|
output_merger.blend_const.a.Value())
|
|
|
|
.Cast<u8>();
|
2016-04-02 03:26:10 +02:00
|
|
|
|
2015-01-31 07:40:05 +01:00
|
|
|
switch (factor) {
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::Zero:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 0;
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::One:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255;
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::SourceColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return combiner_output[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusSourceColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - combiner_output[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::DestColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return dest[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusDestColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - dest[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::SourceAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return combiner_output.a();
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusSourceAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - combiner_output.a();
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::DestAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return dest.a();
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusDestAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - dest.a();
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::ConstantColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return blend_const[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusConstantColor:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - blend_const[channel];
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::ConstantAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return blend_const.a();
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::OneMinusConstantAlpha:
|
2016-04-02 03:26:10 +02:00
|
|
|
return 255 - blend_const.a();
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
case FramebufferRegs::BlendFactor::SourceAlphaSaturate:
|
2016-04-02 03:26:10 +02:00
|
|
|
// Returns 1.0 for the alpha channel
|
|
|
|
if (channel == 3)
|
|
|
|
return 255;
|
|
|
|
return std::min(combiner_output.a(), static_cast<u8>(255 - dest.a()));
|
2015-01-31 07:40:05 +01:00
|
|
|
|
2014-12-21 03:00:25 +01:00
|
|
|
default:
|
2017-11-01 10:47:39 +01:00
|
|
|
LOG_CRITICAL(HW_GPU, "Unknown blend factor %x", static_cast<u32>(factor));
|
2015-02-18 13:50:25 +01:00
|
|
|
UNIMPLEMENTED();
|
2014-12-21 03:00:25 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-09-25 14:33:39 +02:00
|
|
|
|
2016-04-02 03:26:10 +02:00
|
|
|
return combiner_output[channel];
|
2014-12-21 03:00:25 +01:00
|
|
|
};
|
|
|
|
|
2016-04-02 03:26:10 +02:00
|
|
|
auto srcfactor = Math::MakeVec(LookupFactor(0, params.factor_source_rgb),
|
|
|
|
LookupFactor(1, params.factor_source_rgb),
|
|
|
|
LookupFactor(2, params.factor_source_rgb),
|
|
|
|
LookupFactor(3, params.factor_source_a));
|
|
|
|
|
|
|
|
auto dstfactor = Math::MakeVec(LookupFactor(0, params.factor_dest_rgb),
|
|
|
|
LookupFactor(1, params.factor_dest_rgb),
|
|
|
|
LookupFactor(2, params.factor_dest_rgb),
|
|
|
|
LookupFactor(3, params.factor_dest_a));
|
2014-12-21 03:00:25 +01:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
blend_output = EvaluateBlendEquation(combiner_output, srcfactor, dest, dstfactor,
|
|
|
|
params.blend_equation_rgb);
|
|
|
|
blend_output.a() = EvaluateBlendEquation(combiner_output, srcfactor, dest,
|
|
|
|
dstfactor, params.blend_equation_a)
|
|
|
|
.a();
|
2014-12-21 03:00:25 +01:00
|
|
|
} else {
|
2016-09-18 02:38:01 +02:00
|
|
|
blend_output =
|
|
|
|
Math::MakeVec(LogicOp(combiner_output.r(), dest.r(), output_merger.logic_op),
|
|
|
|
LogicOp(combiner_output.g(), dest.g(), output_merger.logic_op),
|
|
|
|
LogicOp(combiner_output.b(), dest.b(), output_merger.logic_op),
|
|
|
|
LogicOp(combiner_output.a(), dest.a(), output_merger.logic_op));
|
2014-12-21 03:00:25 +01:00
|
|
|
}
|
|
|
|
|
2016-09-19 03:01:46 +02:00
|
|
|
const Math::Vec4<u8> result = {
|
|
|
|
output_merger.red_enable ? blend_output.r() : dest.r(),
|
|
|
|
output_merger.green_enable ? blend_output.g() : dest.g(),
|
|
|
|
output_merger.blue_enable ? blend_output.b() : dest.b(),
|
|
|
|
output_merger.alpha_enable ? blend_output.a() : dest.a(),
|
|
|
|
};
|
2015-01-27 23:26:09 +01:00
|
|
|
|
2017-01-28 06:47:34 +01:00
|
|
|
if (regs.framebuffer.framebuffer.allow_color_write != 0)
|
2016-04-01 15:44:42 +02:00
|
|
|
DrawPixel(x >> 4, y >> 4, result);
|
2014-07-27 18:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 08:43:37 +01:00
|
|
|
void ProcessTriangle(const Vertex& v0, const Vertex& v1, const Vertex& v2) {
|
2015-02-18 13:14:49 +01:00
|
|
|
ProcessTriangleInternal(v0, v1, v2);
|
|
|
|
}
|
|
|
|
|
2014-07-27 18:02:35 +02:00
|
|
|
} // namespace Rasterizer
|
|
|
|
} // namespace Pica
|