使用新的iFloat替代原先的ifloat_t

This commit is contained in:
2025-06-28 18:32:37 +08:00
parent 4db0f654ed
commit 8388f448f3
9 changed files with 1089 additions and 142 deletions
+20
View File
@@ -0,0 +1,20 @@
#include "math/dvc_fix_point.h"
DV_CORE_BEGIN_NAMESPACE
const ifloat64 ifloat64::kMax{ ifloat64::kMax_s3132 };
const ifloat64 ifloat64::kMin{ ifloat64::kMin_s3132 };
const ifloat64 ifloat64::kZero{ 0 };
const ifloat64 ifloat64::kOne{ INT64_C(1) << ifloat64::kShift };
const ifloat64 ifloat64::kTwo{ INT64_C(2) << kShift };
const ifloat64 ifloat64::kThree{ INT64_C(3) << ifloat64::kShift };
const ifloat64 ifloat64::kFour{ INT64_C(4) << ifloat64::kShift };
const ifloat64 ifloat64::kHalf{ INT64_C(1) << (ifloat64::kShift-1) };
const ifloat64 ifloat64::kPi{ INT64_C(0x3243f6a89) }; //static_cast<int64_t>(std::round(M_PI * kFloatScale));
const ifloat64 ifloat64::kPi2{ INT64_C(0x6487ed511) };
const ifloat64 ifloat64::kPiHalf{ INT64_C(0x1921fb544) };
const ifloat64 ifloat64::kE{ INT64_C(0x2b7e15163) };
const ifloat64 ifloat64::kNegOne{ INT64_C(-1) << kShift };
DV_CORE_END_NAMESPACE
@@ -1,5 +1,5 @@
#include "pipe/dvc_rasterizer.h"
#include "math/dvc_fixmath.h"
#include "math/dvc_fix_point.h"
#include "math/dvc_math_util.h"
/*
@@ -21,7 +21,8 @@ struct DrawTriangleParam
float32_t bbox_max_y;
bool tlBorder[3];
float32_t area;
ifloat3_t edgesDX, edgesDY;
iFloat3 edgesDX, edgesDY;
Rasterizer::DebugParam debugParam;
};
//-------------------------------------------------------------------------------------
@@ -29,10 +30,15 @@ enum { TILE_WIDTH_IN_PIXELS = 64, COARSE_BLOCK_WIDTH_IN_PIXELS = 16, FINE_BLOCK_
//-------------------------------------------------------------------------------------
void _drawTriangle_Fine(int32_t tile_id, int32_t coarse_id, int32_t fine_id,
const DrawTriangleParam& param, const ifloat3_t& edges0, uint32_t testEdgeMask,
const DrawTriangleParam& param, const iFloat3& edges0, uint32_t testEdgeMask,
Rasterizer::DrawTriangleCallback callback)
{
ifloat3_t pixelEdges(edges0);
iFloat3 kHalfDX = param.edgesDX; kHalfDX.mul(iFloat::kHalf);
iFloat3 kHalfDY = param.edgesDY; kHalfDY.mul(iFloat::kHalf);
iFloat3 pixelEdges(edges0);
pixelEdges.sub(kHalfDY);
pixelEdges.add(kHalfDX);
const fVector2 v0(param.verts[0]), v1(param.verts[1]), v2(param.verts[2]);
const bool edgeMask[3] = { (testEdgeMask & 1) != 0 , (testEdgeMask & 2) != 0 , (testEdgeMask & 4) != 0 };
@@ -45,10 +51,39 @@ void _drawTriangle_Fine(int32_t tile_id, int32_t coarse_id, int32_t fine_id,
for (int32_t y_index=0, y= fine_start_y; y_index < 4; y_index++, y++)
{
ifloat3_t edgesRow(pixelEdges);
iFloat3 edgesRow(pixelEdges);
for (int32_t x_index = 0, x=fine_start_x; x_index < 4; x_index++, x++)
{
if (param.debugParam.debug)
{
if (x == param.debugParam.x && y == param.debugParam.y)
{
iFloat3 LBEdges(edges0);
LBEdges.sub(kHalfDY);
LBEdges.add(kHalfDX);
fprintf(param.debugParam.fpOutput, "======= Fine =======\n");
fprintf(param.debugParam.fpOutput, "LeftBottom=[%d,%d](%f,%f)\n", fine_start_x, fine_start_y, fine_start_x+0.5f, fine_start_y+0.5f);
fprintf(param.debugParam.fpOutput, "Edge0=(%f,%f,%f)[%lld,%lld,%lld]\n",
edges0.x.to_float(), edges0.y.to_float(), edges0.z.to_float(),
edges0.x.s3132, edges0.y.s3132, edges0.z.s3132
);
fprintf(param.debugParam.fpOutput, "Edge0+0.5=(%f,%f,%f)[%lld,%lld,%lld]\n",
LBEdges.x.to_float(), LBEdges.y.to_float(), LBEdges.z.to_float(),
LBEdges.x.s3132, LBEdges.y.s3132, LBEdges.z.s3132
);
fprintf(param.debugParam.fpOutput, "======= Pixel =======\n");
fprintf(param.debugParam.fpOutput, "Position=[%d,%d](%f,%f)\n", x, y, x+0.5f, y+0.5f);
fprintf(param.debugParam.fpOutput, "IndexInFind=[%d,%d]\n", x_index, y_index);
fprintf(param.debugParam.fpOutput, "Edge=(%f,%f,%f)[%lld,%lld,%lld]\n",
edgesRow.x.to_float(), edgesRow.y.to_float(), edgesRow.z.to_float(),
edgesRow.x.s3132, edgesRow.y.s3132, edgesRow.z.s3132
);
}
}
bool rejected =
(edgeMask[0] && (edgesRow.x < 0 || (!param.tlBorder[0] && edgesRow.x == 0))) ||
(edgeMask[1] && (edgesRow.y < 0 || (!param.tlBorder[1] && edgesRow.y == 0))) ||
@@ -74,24 +109,24 @@ void _drawTriangle_Fine(int32_t tile_id, int32_t coarse_id, int32_t fine_id,
//-------------------------------------------------------------------------------------
void _drawTriangle_Coarse(int32_t tile_id, int32_t coarse_id,
DrawTriangleParam& param, const ifloat3_t& edges0, uint32_t testEdgeMask,
DrawTriangleParam& param, const iFloat3& edges0, uint32_t testEdgeMask,
Rasterizer::DrawTriangleCallback callback)
{
const ifloat3_t blockEdgesDX(
const iFloat3 blockEdgesDX(
param.edgesDX.x * FINE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDX.y * FINE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDX.z * FINE_BLOCK_WIDTH_IN_PIXELS
);
const ifloat3_t blockEdgesDY(
const iFloat3 blockEdgesDY(
param.edgesDY.x * FINE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDY.y * FINE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDY.z * FINE_BLOCK_WIDTH_IN_PIXELS
);
const bool edgeMask[3] = { (testEdgeMask & 1) != 0 , (testEdgeMask & 2) != 0 , (testEdgeMask & 4) != 0 };
ifloat3_t blockReject(edges0);
ifloat3_t blockAccept(edges0);
ifloat3_t blockEdges(edges0);
iFloat3 blockReject(edges0);
iFloat3 blockAccept(edges0);
iFloat3 blockEdges(edges0);
for (size_t v = 0; v < 3; v++)
{
@@ -117,8 +152,8 @@ void _drawTriangle_Coarse(int32_t tile_id, int32_t coarse_id,
if (block_start_y + (y_index + 1)*FINE_BLOCK_WIDTH_IN_PIXELS >= param.bbox_min_y)
{
ifloat3_t edgesRow(blockEdges);
ifloat3_t edgesRowReject, edgesRowAccept;
iFloat3 edgesRow(blockEdges);
iFloat3 edgesRowReject, edgesRowAccept;
for (size_t v = 0; v < 3; v++)
{
@@ -182,25 +217,25 @@ void _drawTriangle_Coarse(int32_t tile_id, int32_t coarse_id,
//-------------------------------------------------------------------------------------
void _drawTriangle_Title(int32_t tile_id,
DrawTriangleParam& param, const ifloat3_t& edges0, uint32_t testEdgeMask,
DrawTriangleParam& param, const iFloat3& edges0, uint32_t testEdgeMask,
Rasterizer::DrawTriangleCallback callback)
{
const ifloat3_t blockEdgesDX(
const iFloat3 blockEdgesDX(
param.edgesDX[0] * COARSE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDX[1] * COARSE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDX[2] * COARSE_BLOCK_WIDTH_IN_PIXELS
);
const ifloat3_t blockEdgesDY(
const iFloat3 blockEdgesDY(
param.edgesDY[0] * COARSE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDY[1] * COARSE_BLOCK_WIDTH_IN_PIXELS,
param.edgesDY[2] * COARSE_BLOCK_WIDTH_IN_PIXELS
);
const bool edgeMask[3] = { (testEdgeMask & 1) != 0 , (testEdgeMask & 2) != 0 , (testEdgeMask & 4) != 0 };
ifloat3_t blockReject(edges0);
ifloat3_t blockAccept(edges0);
ifloat3_t blockEdges(edges0);
iFloat3 blockReject(edges0);
iFloat3 blockAccept(edges0);
iFloat3 blockEdges(edges0);
for (size_t v = 0; v < 3; v++)
{
@@ -226,8 +261,8 @@ void _drawTriangle_Title(int32_t tile_id,
if (block_start_y + (y_index + 1)*COARSE_BLOCK_WIDTH_IN_PIXELS >= param.bbox_min_y)
{
ifloat3_t edges(blockEdges);
ifloat3_t edgesRowReject, edgesRowAccept;
iFloat3 edges(blockEdges);
iFloat3 edgesRowReject, edgesRowAccept;
for (size_t v = 0; v < 3; v++)
{
@@ -298,7 +333,7 @@ void _drawTriangle_Title(int32_t tile_id,
//-------------------------------------------------------------------------------------
void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
const fVector2& v0, const fVector2& v1, const fVector2& v2,
DrawTriangleCallback callback, bool ccw)
DrawTriangleCallback callback, bool ccw, const DebugParam* debugParam)
{
assert(canvasWidth >= TILE_WIDTH_IN_PIXELS && canvasHeight >= TILE_WIDTH_IN_PIXELS);
assert(canvasWidth%TILE_WIDTH_IN_PIXELS == 0 && canvasHeight%TILE_WIDTH_IN_PIXELS == 0);
@@ -306,20 +341,23 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
DrawTriangleParam param;
param.widthInTiles = canvasWidth / TILE_WIDTH_IN_PIXELS;
//ccw
//Whether the three input vertices are in the agreed clockwise order or not,
// it indicates that the triangle is invisible
bool isCCW = (v1 - v0).crossProduct(v2 - v1)>0;
if (isCCW != ccw) return;
//By default, the order is counterclockwise. So if the input data is clockwise, a reversal is needed.
// Swap v1 and v2, and then reverse it back when the final callback is made
param.ccw = ccw;
param.verts[0] = v0;
param.verts[1] = ccw ? v1 : v2;
param.verts[2] = ccw ? v2 : v1;
param.area = (param.verts[1] - param.verts[0]).crossProduct(param.verts[2] - param.verts[1]);
ifloat2_t vertices[3];
iFloat2 vertices[3];
for (size_t i = 0; i < 3; i++)
{
vertices[i] = ifloat2_t(param.verts[i].x, param.verts[i].y);
vertices[i] = iFloat2(param.verts[i].x, param.verts[i].y);
}
//get window coordinates bounding box
@@ -329,8 +367,8 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
param.bbox_max_y = MathUtil::max3(v0.y, v1.y, v2.y);
//canvas size
ifloat_t iCanvasWidth = ifloat_init(canvasWidth);
ifloat_t iCanvasHeight = ifloat_init(canvasHeight);
iFloat iCanvasWidth(canvasWidth);
iFloat iCanvasHeight(canvasHeight);
// clip triangles that are fully outside the scissor rect (scissor rect = whole window)
if (param.bbox_max_x < 0 || param.bbox_max_y < 0 || param.bbox_min_x >= canvasWidth || param.bbox_min_y >= canvasHeight)
@@ -348,13 +386,13 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
int32_t last_tile_x = (int32_t)(param.bbox_max_x / TILE_WIDTH_IN_PIXELS);
int32_t last_tile_y = (int32_t)(param.bbox_max_y / TILE_WIDTH_IN_PIXELS);
// evaluate edge equation at the top left tile
ifloat_t firstTileX = first_tile_x * ifloat_init(TILE_WIDTH_IN_PIXELS);
ifloat_t firstTileY = first_tile_y * ifloat_init(TILE_WIDTH_IN_PIXELS);
//The first Title is the Title at the lower left corner within the bounding box range covered by the triangle
iFloat firstTileX = iFloat(first_tile_x * TILE_WIDTH_IN_PIXELS);
iFloat firstTileY = iFloat(first_tile_y * TILE_WIDTH_IN_PIXELS);
ifloat3_t edges0;
ifloat3_t tileEdgesDX, tileEdgesDY, edgesReject, edgesAccept;
const ifloat_t kZeroPointFive = ifloat_init(0.5f);
iFloat3 edgesLB; //The edge value at the lower left corner of the first title
iFloat3 tileEdgesDX, tileEdgesDY;
iFloat3 edgesReject, edgesAccept;
for (size_t v = 0; v < 3; v++)
{
@@ -366,8 +404,8 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
tileEdgesDY[v] = param.edgesDY[v] * TILE_WIDTH_IN_PIXELS;
//(V0,V1) X (V0, P) = (x1-x0)(py-y0)-(y1-y0)(px-x0)
edges0[v] = ifloat_multiply(param.edgesDX[v], (firstTileY + kZeroPointFive - vertices[v].y))
- ifloat_multiply(param.edgesDY[v], (firstTileX + kZeroPointFive - vertices[v].x));
edgesLB[v] = param.edgesDX[v] * (firstTileY - vertices[v].y)
- param.edgesDY[v] * (firstTileX - vertices[v].x);
// Top-left rule:
// shift top-left edges ever so slightly outward to make the top-left edges be
@@ -382,7 +420,7 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
param.tlBorder[v] = false;
}
edgesReject[v] = edgesAccept[v] = edges0[v];
edgesReject[v] = edgesAccept[v] = edgesLB[v];
if (tileEdgesDX[v] > 0) edgesReject[v] += tileEdgesDX[v];
else if (tileEdgesDX[v] < 0) edgesAccept[v] += tileEdgesDX[v];
@@ -391,18 +429,77 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
else if (tileEdgesDY[v] < 0) edgesReject[v] -= tileEdgesDY[v];
}
ifloat3_t rowEdges(edges0);
if (debugParam != nullptr)
{
param.debugParam.debug = debugParam->debug;
param.debugParam.szOutputFileName = debugParam->szOutputFileName;
param.debugParam.x = debugParam->x;
param.debugParam.y = debugParam->y;
if (param.debugParam.debug) {
param.debugParam.fpOutput = fopen(debugParam->szOutputFileName, "w");
fprintf(param.debugParam.fpOutput, "===== Triangle ====\n");
for (int32_t i = 0; i < 3; i++) {
fprintf(param.debugParam.fpOutput, "v%d=(%f,%f)[%lld,%lld]\n", i,
param.verts[i].x, param.verts[i].y, vertices[i].x.s3132, vertices[i].y.s3132);
}
fprintf(param.debugParam.fpOutput, "===== TitleRange ====\n");
fprintf(param.debugParam.fpOutput,
"tile_x={%d, %d}\ntile_y={%d, %d}\n",
first_tile_x, last_tile_x, first_tile_y, last_tile_y);
fprintf(param.debugParam.fpOutput,
"FirstTitle=(%f,%f)[%lld,%lld]\n", firstTileX.to_float(), firstTileY.to_float(),
firstTileX.s3132, firstTileY.s3132
);
fprintf(param.debugParam.fpOutput, "===== Edge ====\n");
for (int32_t i = 0; i < 3; i++) {
fprintf(param.debugParam.fpOutput, "DX%d=(%f)[%lld]\tDY%d=(%f)[%lld]\n",
i,
param.edgesDX[i].to_float(), param.edgesDX[i].s3132,
i,
param.edgesDY[i].to_float(), param.edgesDY[i].s3132);
}
fprintf(param.debugParam.fpOutput, "\n");
for (int32_t i = 0; i < 3; i++) {
fprintf(param.debugParam.fpOutput, "Edge%d=(%lld)*(y-%lld)-(%lld)*(x-%lld)\n\t=(%f)*(y-%f)-(%f)*(x-%f)\n", i,
param.edgesDX[i].s3132, vertices[i].y.s3132, param.edgesDY[i].s3132, vertices[i].x.s3132,
param.edgesDX[i].to_float(), vertices[i].y.to_float(), param.edgesDY[i].to_float(), vertices[i].x.to_float()
);
}
fprintf(param.debugParam.fpOutput, "EdgeLB=(%f,%f,%f)[%lld,%lld,%lld]\n",
edgesLB.x.to_float(), edgesLB.y.to_float(), edgesLB.z.to_float(),
edgesLB.x.s3132, edgesLB.y.s3132, edgesLB.z.s3132
);
for (int32_t i = 0; i < 3; i++)
{
fprintf(param.debugParam.fpOutput, "EdgeLB.%d=%lld*(%lld-%lld)-%lld*(%lld-%lld)\n", i,
param.edgesDX[i].s3132, firstTileY.s3132, vertices[i].y.s3132,
param.edgesDY[i].s3132, firstTileX.s3132, vertices[i].x.s3132
);
}
fprintf(param.debugParam.fpOutput, "TopLeft=%s,%s,%s\n",
param.tlBorder[0] ? "true" : "false", param.tlBorder[1] ? "true" : "false", param.tlBorder[2] ? "true" : "false"
);
}
}
iFloat3 rowEdges(edgesLB);
int32_t tile_row_start = first_tile_y * param.widthInTiles + first_tile_x;
for (int32_t tile_y = first_tile_y; tile_y <= last_tile_y; tile_y++)
{
ifloat3_t edges(rowEdges);
ifloat3_t tileEdgesReject(edgesReject);
ifloat3_t tileEdgesAccept(edgesAccept);
iFloat3 edges(rowEdges);
iFloat3 tileEdgesReject(edgesReject);
iFloat3 tileEdgesAccept(edgesAccept);
int32_t tile_i = tile_row_start;
for (int32_t tile_x = first_tile_x; tile_x <= last_tile_x; tile_x++)
{
//If the title is excluded by any edge,
// it indicates that the title is completely outside the triangle and does not require rendering
bool rejected =
((tileEdgesReject[0] < 0 || (!param.tlBorder[0] && tileEdgesReject[0] == 0))) ||
((tileEdgesReject[1] < 0 || (!param.tlBorder[1] && tileEdgesReject[1] == 0))) ||
@@ -416,7 +513,9 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
{
if (tileEdgesAccept[v] < 0 || (tileEdgesAccept[v] == 0 && !param.tlBorder[v]))
{
testEdgeMask += (1 << v);
//whether the accepting vertex of this title is on the "outside" of the three edges,
// that is, it may intersect or be completely outside
testEdgeMask += (1 << v);
}
}
@@ -436,6 +535,12 @@ void Rasterizer::drawTriangleLarrabee(int32_t canvasWidth, int32_t canvasHeight,
edgesAccept.add(tileEdgesDX);
tile_row_start += param.widthInTiles;
}
if (debugParam != nullptr && debugParam->debug)
{
fclose(param.debugParam.fpOutput);
param.debugParam.fpOutput = nullptr;
}
}
//-------------------------------------------------------------------------------------