From 7ac0cace7d1eed574de31e0a8b28517e759034a7 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sun, 20 Nov 2016 02:07:55 +0100 Subject: [PATCH] Stop copying the matrix and remove r_triangle --- src/CMakeLists.txt | 1 - src/r_compiler/llvmdrawers.h | 2 - src/r_plane.cpp | 266 ------- src/r_poly_decal.cpp | 2 +- src/r_poly_decal.h | 2 +- src/r_poly_intersection.h | 2 +- src/r_poly_particle.cpp | 2 +- src/r_poly_plane.cpp | 4 +- src/r_poly_sky.cpp | 3 +- src/r_poly_sky.h | 1 + src/r_poly_sprite.cpp | 2 +- src/r_poly_triangle.cpp | 165 ++++- src/r_poly_triangle.h | 16 +- src/r_poly_wall.cpp | 2 +- src/r_poly_wallsprite.cpp | 2 +- src/r_poly_wallsprite.h | 2 +- src/r_triangle.cpp | 1338 ---------------------------------- src/r_triangle.h | 116 --- 18 files changed, 183 insertions(+), 1745 deletions(-) delete mode 100644 src/r_triangle.cpp delete mode 100644 src/r_triangle.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8c2e36220..987f23f2f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1092,7 +1092,6 @@ set( FASTMATH_PCH_SOURCES r_segs.cpp r_sky.cpp r_things.cpp - r_triangle.cpp s_advsound.cpp s_environment.cpp s_playlist.cpp diff --git a/src/r_compiler/llvmdrawers.h b/src/r_compiler/llvmdrawers.h index e42153743..1db2f5b2a 100644 --- a/src/r_compiler/llvmdrawers.h +++ b/src/r_compiler/llvmdrawers.h @@ -234,8 +234,6 @@ struct TriUniforms nearest_filter = 2, fixed_light = 4 }; - - TriMatrix objectToClip; }; struct TriDrawTriangleArgs diff --git a/src/r_plane.cpp b/src/r_plane.cpp index 17728e80c..ca8547003 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -59,7 +59,6 @@ #include "v_palette.h" #include "r_data/colormaps.h" #include "r_draw_rgba.h" -#include "r_triangle.h" #ifdef _MSC_VER #pragma warning(disable:4244) @@ -1015,261 +1014,6 @@ static const BYTE *R_GetTwoSkyColumns (FTexture *fronttex, int x) } } -static void R_DrawCubeSky(visplane_t *pl) -{ - int x1 = pl->left; - int x2 = pl->right; - short *uwal = (short *)pl->top; - short *dwal = (short *)pl->bottom; - - static TriVertex cube[6 * 6] = - { - // Top - { -1.0f, 1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - { 1.0f, 1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - { 1.0f, -1.0f, 0.6f, 1.0f, 0.0f, 0.1f }, - - { 1.0f, -1.0f, 0.6f, 1.0f, 0.0f, 0.1f }, - { -1.0f, -1.0f, 0.6f, 1.0f, 1.0f, 0.1f }, - { -1.0f, 1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - - // Bottom - { 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.9f }, - { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f }, - { -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f }, - - { -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f }, - { -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.9f }, - { 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.9f }, - - // Front - { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 2.0f }, - { 1.0f, 1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - { -1.0f, 1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - - { -1.0f, 1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - { -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 2.0f }, - - // Back - { -1.0f, -1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - { 1.0f, -1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - { 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - - { 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 2.0f }, - { -1.0f, -1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - - // Right - { 1.0f, -1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - { 1.0f, 1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - { 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - - { 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - { 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 2.0f }, - { 1.0f, -1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - - // Left - { -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 2.0f }, - { -1.0f, 1.0f, 0.6f, 1.0f, 1.0f, 0.0f }, - { -1.0f, -1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - - { -1.0f, -1.0f, 0.6f, 1.0f, 0.0f, 0.0f }, - { -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 2.0f }, - { -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 2.0f } - }; - - TriMatrix objectToWorld = TriMatrix::translate((float)ViewPos.X, (float)ViewPos.Y, (float)ViewPos.Z) * TriMatrix::scale(1000.0f, 1000.0f, 1000.0f); - TriMatrix objectToClip = TriMatrix::viewToClip() * TriMatrix::worldToView() * objectToWorld; - //TriMatrix objectToWorld = TriMatrix::scale(1000.0f, 1000.0f, 1000.0f); - //TriMatrix objectToClip = TriMatrix::viewToClip() * objectToWorld; - - uint32_t solid_top = frontskytex->GetSkyCapColor(false); - uint32_t solid_bottom = frontskytex->GetSkyCapColor(true); - - solid_top = RGB32k.RGB[(RPART(solid_top) >> 3)][(GPART(solid_top) >> 3)][(BPART(solid_top) >> 3)]; - solid_bottom = RGB32k.RGB[(RPART(solid_bottom) >> 3)][(GPART(solid_bottom) >> 3)][(BPART(solid_bottom) >> 3)]; - - TriUniforms uniforms; - uniforms.objectToClip = objectToClip; - uniforms.light = 256; - uniforms.flags = 0; - - TriangleDrawer::fill(uniforms, cube, 6, TriangleDrawMode::Normal, false, x1, x2 - 1, uwal, dwal, solid_top); - TriangleDrawer::fill(uniforms, cube + 6, 6, TriangleDrawMode::Normal, false, x1, x2 - 1, uwal, dwal, solid_bottom); - TriangleDrawer::draw(uniforms, cube + 2 * 6, 4 * 6, TriangleDrawMode::Normal, false, x1, x2 - 1, uwal, dwal, frontskytex); -} - -namespace -{ - class SkyDome - { - public: - SkyDome() { CreateDome(); } - void Render(visplane_t *pl); - - private: - TArray mVertices; - TArray mPrimStart; - int mRows, mColumns; - - void SkyVertex(int r, int c, bool yflip); - void CreateSkyHemisphere(bool zflip); - void CreateDome(); - void RenderRow(int row, visplane_t *pl); - void RenderCapColorRow(int row, bool bottomCap, visplane_t *pl); - - TriVertex SetVertex(float xx, float yy, float zz, float uu = 0, float vv = 0); - TriVertex SetVertexXYZ(float xx, float yy, float zz, float uu = 0, float vv = 0); - }; - - TriVertex SkyDome::SetVertex(float xx, float yy, float zz, float uu, float vv) - { - TriVertex v; - v.x = xx; - v.y = yy; - v.z = zz; - v.w = 1.0f; - v.varying[0] = uu; - v.varying[1] = vv; - return v; - } - - TriVertex SkyDome::SetVertexXYZ(float xx, float yy, float zz, float uu, float vv) - { - TriVertex v; - v.x = xx; - v.y = zz; - v.z = yy; - v.w = 1.0f; - v.varying[0] = uu; - v.varying[1] = vv; - return v; - } - - void SkyDome::SkyVertex(int r, int c, bool zflip) - { - static const FAngle maxSideAngle = 60.f; - static const float scale = 10000.; - - FAngle topAngle = (c / (float)mColumns * 360.f); - FAngle sideAngle = maxSideAngle * (mRows - r) / mRows; - float height = sideAngle.Sin(); - float realRadius = scale * sideAngle.Cos(); - FVector2 pos = topAngle.ToVector(realRadius); - float z = (!zflip) ? scale * height : -scale * height; - - float u, v; - //uint32_t color = r == 0 ? 0xffffff : 0xffffffff; - - // And the texture coordinates. - if (!zflip) // Flipped Y is for the lower hemisphere. - { - u = (-c / (float)mColumns); - v = (r / (float)mRows); - } - else - { - u = (-c / (float)mColumns); - v = 1.0f + ((mRows - r) / (float)mRows); - } - - if (r != 4) z += 300; - - // And finally the vertex. - TriVertex vert; - vert = SetVertexXYZ(-pos.X, z - 1.f, pos.Y, u * 4.0f, v + 0.5f/*, color*/); - mVertices.Push(vert); - } - - void SkyDome::CreateSkyHemisphere(bool zflip) - { - int r, c; - - mPrimStart.Push(mVertices.Size()); - - for (c = 0; c < mColumns; c++) - { - SkyVertex(1, c, zflip); - } - - // The total number of triangles per hemisphere can be calculated - // as follows: rows * columns * 2 + 2 (for the top cap). - for (r = 0; r < mRows; r++) - { - mPrimStart.Push(mVertices.Size()); - for (c = 0; c <= mColumns; c++) - { - SkyVertex(r + zflip, c, zflip); - SkyVertex(r + 1 - zflip, c, zflip); - } - } - } - - void SkyDome::CreateDome() - { - mColumns = 128; - mRows = 4; - CreateSkyHemisphere(false); - CreateSkyHemisphere(true); - mPrimStart.Push(mVertices.Size()); - } - - void SkyDome::RenderRow(int row, visplane_t *pl) - { - int x1 = pl->left; - int x2 = pl->right; - short *uwal = (short *)pl->top; - short *dwal = (short *)pl->bottom; - TriMatrix objectToWorld = TriMatrix::translate((float)ViewPos.X, (float)ViewPos.Y, (float)ViewPos.Z); - TriMatrix objectToClip = TriMatrix::viewToClip() * TriMatrix::worldToView() * objectToWorld; - TriUniforms uniforms; - uniforms.objectToClip = objectToClip; - uniforms.light = 256; - uniforms.flags = 0; - TriangleDrawer::draw(uniforms, &mVertices[mPrimStart[row]], mPrimStart[row + 1] - mPrimStart[row], TriangleDrawMode::Strip, false, x1, x2 - 1, uwal, dwal, frontskytex); - } - - void SkyDome::RenderCapColorRow(int row, bool bottomCap, visplane_t *pl) - { - uint32_t solid = frontskytex->GetSkyCapColor(bottomCap); - solid = RGB32k.RGB[(RPART(solid) >> 3)][(GPART(solid) >> 3)][(BPART(solid) >> 3)]; - - int x1 = pl->left; - int x2 = pl->right; - short *uwal = (short *)pl->top; - short *dwal = (short *)pl->bottom; - TriMatrix objectToWorld = TriMatrix::translate((float)ViewPos.X, (float)ViewPos.Y, (float)ViewPos.Z); - TriMatrix objectToClip = TriMatrix::viewToClip() * TriMatrix::worldToView() * objectToWorld; - TriUniforms uniforms; - uniforms.objectToClip = objectToClip; - uniforms.light = 256; - uniforms.flags = 0; - TriangleDrawer::fill(uniforms, &mVertices[mPrimStart[row]], mPrimStart[row + 1] - mPrimStart[row], TriangleDrawMode::Fan, bottomCap, x1, x2 - 1, uwal, dwal, solid); - } - - void SkyDome::Render(visplane_t *pl) - { - int rc = mRows + 1; - - // No need to draw this as the software renderer can't look that high anyway - //RenderCapColorRow(0, false, pl); - //RenderCapColorRow(rc, true, pl); - - for (int i = 1; i <= mRows; i++) - { - RenderRow(i, pl); - RenderRow(rc + i, pl); - } - } -} - -static void R_DrawDomeSky(visplane_t *pl) -{ - static SkyDome skydome; - skydome.Render(pl); -} - static void R_DrawSkyColumnStripe(int start_x, int y1, int y2, int columns, double scale, double texturemid, double yrepeat) { uint32_t height = frontskytex->GetHeight(); @@ -1467,16 +1211,6 @@ static void R_DrawSky (visplane_t *pl) R_DrawCapSky(pl); return; } - else if (r_skymode == 3) - { - R_DrawCubeSky(pl); - return; - } - else if (r_skymode == 4) - { - R_DrawDomeSky(pl); - return; - } int x; float swal; diff --git a/src/r_poly_decal.cpp b/src/r_poly_decal.cpp index 5641bfcd1..52caae7e2 100644 --- a/src/r_poly_decal.cpp +++ b/src/r_poly_decal.cpp @@ -129,7 +129,6 @@ void RenderPolyDecal::Render(const TriMatrix &worldToClip, DBaseDecal *decal, co bool fullbrightSprite = (decal->RenderFlags & RF_FULLBRIGHT) == RF_FULLBRIGHT; TriUniforms uniforms; - uniforms.objectToClip = worldToClip; if (fullbrightSprite || fixedlightlev >= 0 || fixedcolormap) { uniforms.light = 256; @@ -145,6 +144,7 @@ void RenderPolyDecal::Render(const TriMatrix &worldToClip, DBaseDecal *decal, co PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = 4; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_decal.h b/src/r_poly_decal.h index 39b250abb..61087a773 100644 --- a/src/r_poly_decal.h +++ b/src/r_poly_decal.h @@ -22,7 +22,7 @@ #pragma once -#include "r_triangle.h" +#include "r_poly_triangle.h" class RenderPolyDecal { diff --git a/src/r_poly_intersection.h b/src/r_poly_intersection.h index 48ebef031..b06bc1200 100644 --- a/src/r_poly_intersection.h +++ b/src/r_poly_intersection.h @@ -22,7 +22,7 @@ #pragma once -#include "r_triangle.h" +#include "r_poly_triangle.h" #include #include diff --git a/src/r_poly_particle.cpp b/src/r_poly_particle.cpp index e5797a108..8448c8f32 100644 --- a/src/r_poly_particle.cpp +++ b/src/r_poly_particle.cpp @@ -71,7 +71,6 @@ void RenderPolyParticle::Render(const TriMatrix &worldToClip, particle_t *partic bool fullbrightSprite = particle->bright != 0; TriUniforms uniforms; - uniforms.objectToClip = worldToClip; if (fullbrightSprite || fixedlightlev >= 0 || fixedcolormap) { uniforms.light = 256; @@ -88,6 +87,7 @@ void RenderPolyParticle::Render(const TriMatrix &worldToClip, particle_t *partic PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = 4; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_plane.cpp b/src/r_poly_plane.cpp index 9005e32f1..a68a4b568 100644 --- a/src/r_poly_plane.cpp +++ b/src/r_poly_plane.cpp @@ -103,7 +103,6 @@ void RenderPolyPlane::Render3DFloor(const TriMatrix &worldToClip, subsector_t *s } TriUniforms uniforms; - uniforms.objectToClip = worldToClip; uniforms.light = (uint32_t)(lightlevel / 255.0f * 256.0f); if (fixedlightlev >= 0 || fixedcolormap) uniforms.light = 256; @@ -133,6 +132,7 @@ void RenderPolyPlane::Render3DFloor(const TriMatrix &worldToClip, subsector_t *s PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = sub->numlines; args.mode = TriangleDrawMode::Fan; @@ -195,7 +195,6 @@ void RenderPolyPlane::Render(const TriMatrix &worldToClip, subsector_t *sub, uin bool isSky = picnum == skyflatnum; TriUniforms uniforms; - uniforms.objectToClip = worldToClip; uniforms.light = (uint32_t)(frontsector->lightlevel / 255.0f * 256.0f); if (fixedlightlev >= 0 || fixedcolormap) uniforms.light = 256; @@ -225,6 +224,7 @@ void RenderPolyPlane::Render(const TriMatrix &worldToClip, subsector_t *sub, uin PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = sub->numlines; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_sky.cpp b/src/r_poly_sky.cpp index b0b06f262..a60b3f460 100644 --- a/src/r_poly_sky.cpp +++ b/src/r_poly_sky.cpp @@ -48,9 +48,9 @@ void PolySkyDome::Render(const TriMatrix &worldToClip) backskytex = TexMan(sky2tex, true); TriMatrix objectToWorld = TriMatrix::translate((float)ViewPos.X, (float)ViewPos.Y, (float)ViewPos.Z); + objectToClip = worldToClip * objectToWorld; TriUniforms uniforms; - uniforms.objectToClip = worldToClip * objectToWorld; uniforms.light = 256; uniforms.flags = 0; uniforms.subsectorDepth = RenderPolyScene::SkySubsectorDepth; @@ -59,6 +59,7 @@ void PolySkyDome::Render(const TriMatrix &worldToClip) PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &objectToClip; args.stenciltestvalue = 255; args.stencilwritevalue = 1; args.SetTexture(frontskytex); diff --git a/src/r_poly_sky.h b/src/r_poly_sky.h index dd4bd29ca..95ec27ed6 100644 --- a/src/r_poly_sky.h +++ b/src/r_poly_sky.h @@ -33,6 +33,7 @@ private: TArray mVertices; TArray mPrimStart; int mRows, mColumns; + TriMatrix objectToClip; void SkyVertex(int r, int c, bool yflip); void CreateSkyHemisphere(bool zflip); diff --git a/src/r_poly_sprite.cpp b/src/r_poly_sprite.cpp index 8c74d0d1a..84ff8e221 100644 --- a/src/r_poly_sprite.cpp +++ b/src/r_poly_sprite.cpp @@ -114,7 +114,6 @@ void RenderPolySprite::Render(const TriMatrix &worldToClip, AActor *thing, subse bool fullbrightSprite = ((thing->renderflags & RF_FULLBRIGHT) || (thing->flags5 & MF5_BRIGHT)); TriUniforms uniforms; - uniforms.objectToClip = worldToClip; if (fullbrightSprite || fixedlightlev >= 0 || fixedcolormap) { uniforms.light = 256; @@ -129,6 +128,7 @@ void RenderPolySprite::Render(const TriMatrix &worldToClip, AActor *thing, subse PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = 4; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_triangle.cpp b/src/r_poly_triangle.cpp index ad9f7ff95..5574251e6 100644 --- a/src/r_poly_triangle.cpp +++ b/src/r_poly_triangle.cpp @@ -124,28 +124,28 @@ void PolyTriangleDrawer::draw_arrays(const PolyDrawArgs &drawargs, TriDrawVarian for (int i = 0; i < vcount / 3; i++) { for (int j = 0; j < 3; j++) - vert[j] = shade_vertex(drawargs.uniforms, *(vinput++)); + vert[j] = shade_vertex(*drawargs.objectToClip, *(vinput++)); draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); } } else if (drawargs.mode == TriangleDrawMode::Fan) { - vert[0] = shade_vertex(drawargs.uniforms, *(vinput++)); - vert[1] = shade_vertex(drawargs.uniforms, *(vinput++)); + vert[0] = shade_vertex(*drawargs.objectToClip, *(vinput++)); + vert[1] = shade_vertex(*drawargs.objectToClip, *(vinput++)); for (int i = 2; i < vcount; i++) { - vert[2] = shade_vertex(drawargs.uniforms, *(vinput++)); + vert[2] = shade_vertex(*drawargs.objectToClip, *(vinput++)); draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); vert[1] = vert[2]; } } else // TriangleDrawMode::Strip { - vert[0] = shade_vertex(drawargs.uniforms, *(vinput++)); - vert[1] = shade_vertex(drawargs.uniforms, *(vinput++)); + vert[0] = shade_vertex(*drawargs.objectToClip, *(vinput++)); + vert[1] = shade_vertex(*drawargs.objectToClip, *(vinput++)); for (int i = 2; i < vcount; i++) { - vert[2] = shade_vertex(drawargs.uniforms, *(vinput++)); + vert[2] = shade_vertex(*drawargs.objectToClip, *(vinput++)); draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); vert[0] = vert[1]; vert[1] = vert[2]; @@ -154,10 +154,10 @@ void PolyTriangleDrawer::draw_arrays(const PolyDrawArgs &drawargs, TriDrawVarian } } -TriVertex PolyTriangleDrawer::shade_vertex(const TriUniforms &uniforms, TriVertex v) +TriVertex PolyTriangleDrawer::shade_vertex(const TriMatrix &objectToClip, TriVertex v) { // Apply transform to get clip coordinates: - return uniforms.objectToClip * v; + return objectToClip * v; } void PolyTriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread, void(*drawfunc)(const TriDrawTriangleArgs *, WorkerThreadData *)) @@ -341,3 +341,150 @@ FString DrawPolyTrianglesCommand::DebugInfo() { return "DrawPolyTriangles"; } + +///////////////////////////////////////////////////////////////////////////// + +TriMatrix TriMatrix::null() +{ + TriMatrix m; + memset(m.matrix, 0, sizeof(m.matrix)); + return m; +} + +TriMatrix TriMatrix::identity() +{ + TriMatrix m = null(); + m.matrix[0] = 1.0f; + m.matrix[5] = 1.0f; + m.matrix[10] = 1.0f; + m.matrix[15] = 1.0f; + return m; +} + +TriMatrix TriMatrix::translate(float x, float y, float z) +{ + TriMatrix m = identity(); + m.matrix[0 + 3 * 4] = x; + m.matrix[1 + 3 * 4] = y; + m.matrix[2 + 3 * 4] = z; + return m; +} + +TriMatrix TriMatrix::scale(float x, float y, float z) +{ + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = x; + m.matrix[1 + 1 * 4] = y; + m.matrix[2 + 2 * 4] = z; + m.matrix[3 + 3 * 4] = 1; + return m; +} + +TriMatrix TriMatrix::rotate(float angle, float x, float y, float z) +{ + float c = cosf(angle); + float s = sinf(angle); + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = (x*x*(1.0f - c) + c); + m.matrix[0 + 1 * 4] = (x*y*(1.0f - c) - z*s); + m.matrix[0 + 2 * 4] = (x*z*(1.0f - c) + y*s); + m.matrix[1 + 0 * 4] = (y*x*(1.0f - c) + z*s); + m.matrix[1 + 1 * 4] = (y*y*(1.0f - c) + c); + m.matrix[1 + 2 * 4] = (y*z*(1.0f - c) - x*s); + m.matrix[2 + 0 * 4] = (x*z*(1.0f - c) - y*s); + m.matrix[2 + 1 * 4] = (y*z*(1.0f - c) + x*s); + m.matrix[2 + 2 * 4] = (z*z*(1.0f - c) + c); + m.matrix[3 + 3 * 4] = 1.0f; + return m; +} + +TriMatrix TriMatrix::swapYZ() +{ + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = 1.0f; + m.matrix[1 + 2 * 4] = 1.0f; + m.matrix[2 + 1 * 4] = -1.0f; + m.matrix[3 + 3 * 4] = 1.0f; + return m; +} + +TriMatrix TriMatrix::perspective(float fovy, float aspect, float z_near, float z_far) +{ + float f = (float)(1.0 / tan(fovy * M_PI / 360.0)); + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = f / aspect; + m.matrix[1 + 1 * 4] = f; + m.matrix[2 + 2 * 4] = (z_far + z_near) / (z_near - z_far); + m.matrix[2 + 3 * 4] = (2.0f * z_far * z_near) / (z_near - z_far); + m.matrix[3 + 2 * 4] = -1.0f; + return m; +} + +TriMatrix TriMatrix::frustum(float left, float right, float bottom, float top, float near, float far) +{ + float a = (right + left) / (right - left); + float b = (top + bottom) / (top - bottom); + float c = -(far + near) / (far - near); + float d = -(2.0f * far) / (far - near); + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = 2.0f * near / (right - left); + m.matrix[1 + 1 * 4] = 2.0f * near / (top - bottom); + m.matrix[0 + 2 * 4] = a; + m.matrix[1 + 2 * 4] = b; + m.matrix[2 + 2 * 4] = c; + m.matrix[2 + 3 * 4] = d; + m.matrix[3 + 2 * 4] = -1; + return m; +} + +TriMatrix TriMatrix::worldToView() +{ + TriMatrix m = null(); + m.matrix[0 + 0 * 4] = (float)ViewSin; + m.matrix[0 + 1 * 4] = (float)-ViewCos; + m.matrix[1 + 2 * 4] = 1.0f; + m.matrix[2 + 0 * 4] = (float)-ViewCos; + m.matrix[2 + 1 * 4] = (float)-ViewSin; + m.matrix[3 + 3 * 4] = 1.0f; + return m * translate((float)-ViewPos.X, (float)-ViewPos.Y, (float)-ViewPos.Z); +} + +TriMatrix TriMatrix::viewToClip() +{ + float near = 5.0f; + float far = 65536.0f; + float width = (float)(FocalTangent * near); + float top = (float)(CenterY / InvZtoScale * near); + float bottom = (float)(top - viewheight / InvZtoScale * near); + return frustum(-width, width, bottom, top, near, far); +} + +TriMatrix TriMatrix::operator*(const TriMatrix &mult) const +{ + TriMatrix result; + for (int x = 0; x < 4; x++) + { + for (int y = 0; y < 4; y++) + { + result.matrix[x + y * 4] = + matrix[0 * 4 + x] * mult.matrix[y * 4 + 0] + + matrix[1 * 4 + x] * mult.matrix[y * 4 + 1] + + matrix[2 * 4 + x] * mult.matrix[y * 4 + 2] + + matrix[3 * 4 + x] * mult.matrix[y * 4 + 3]; + } + } + return result; +} + +TriVertex TriMatrix::operator*(TriVertex v) const +{ + float vx = matrix[0 * 4 + 0] * v.x + matrix[1 * 4 + 0] * v.y + matrix[2 * 4 + 0] * v.z + matrix[3 * 4 + 0] * v.w; + float vy = matrix[0 * 4 + 1] * v.x + matrix[1 * 4 + 1] * v.y + matrix[2 * 4 + 1] * v.z + matrix[3 * 4 + 1] * v.w; + float vz = matrix[0 * 4 + 2] * v.x + matrix[1 * 4 + 2] * v.y + matrix[2 * 4 + 2] * v.z + matrix[3 * 4 + 2] * v.w; + float vw = matrix[0 * 4 + 3] * v.x + matrix[1 * 4 + 3] * v.y + matrix[2 * 4 + 3] * v.z + matrix[3 * 4 + 3] * v.w; + v.x = vx; + v.y = vy; + v.z = vz; + v.w = vw; + return v; +} diff --git a/src/r_poly_triangle.h b/src/r_poly_triangle.h index f0797460f..db6719de9 100644 --- a/src/r_poly_triangle.h +++ b/src/r_poly_triangle.h @@ -22,15 +22,27 @@ #pragma once -#include "r_triangle.h" +#include "r_draw.h" +#include "r_thread.h" +#include "r_compiler/llvmdrawers.h" #include "r_data/r_translate.h" +class FTexture; + +enum class TriangleDrawMode +{ + Normal, + Fan, + Strip +}; + struct TriDrawTriangleArgs; class PolyDrawArgs { public: TriUniforms uniforms; + const TriMatrix *objectToClip = nullptr; const TriVertex *vinput = nullptr; int vcount = 0; TriangleDrawMode mode = TriangleDrawMode::Normal; @@ -83,7 +95,7 @@ public: static void draw(const PolyDrawArgs &args, TriDrawVariant variant, TriBlendMode blendmode); private: - static TriVertex shade_vertex(const TriUniforms &uniforms, TriVertex v); + static TriVertex shade_vertex(const TriMatrix &objectToClip, TriVertex v); static void draw_arrays(const PolyDrawArgs &args, TriDrawVariant variant, TriBlendMode blendmode, WorkerThreadData *thread); static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread, void(*drawfunc)(const TriDrawTriangleArgs *, WorkerThreadData *)); static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2); diff --git a/src/r_poly_wall.cpp b/src/r_poly_wall.cpp index e8bad52a7..f0eb33c32 100644 --- a/src/r_poly_wall.cpp +++ b/src/r_poly_wall.cpp @@ -176,13 +176,13 @@ void RenderPolyWall::Render(const TriMatrix &worldToClip) } TriUniforms uniforms; - uniforms.objectToClip = worldToClip; uniforms.light = (uint32_t)(GetLightLevel() / 255.0f * 256.0f); uniforms.flags = 0; uniforms.subsectorDepth = SubsectorDepth; PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = 4; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_wallsprite.cpp b/src/r_poly_wallsprite.cpp index ccebe5f0e..cd331dcb5 100644 --- a/src/r_poly_wallsprite.cpp +++ b/src/r_poly_wallsprite.cpp @@ -99,7 +99,6 @@ void RenderPolyWallSprite::Render(const TriMatrix &worldToClip, AActor *thing, s bool fullbrightSprite = ((thing->renderflags & RF_FULLBRIGHT) || (thing->flags5 & MF5_BRIGHT)); TriUniforms uniforms; - uniforms.objectToClip = worldToClip; if (fullbrightSprite || fixedlightlev >= 0 || fixedcolormap) { uniforms.light = 256; @@ -114,6 +113,7 @@ void RenderPolyWallSprite::Render(const TriMatrix &worldToClip, AActor *thing, s PolyDrawArgs args; args.uniforms = uniforms; + args.objectToClip = &worldToClip; args.vinput = vertices; args.vcount = 4; args.mode = TriangleDrawMode::Fan; diff --git a/src/r_poly_wallsprite.h b/src/r_poly_wallsprite.h index dd4ba28da..66a92b033 100644 --- a/src/r_poly_wallsprite.h +++ b/src/r_poly_wallsprite.h @@ -22,7 +22,7 @@ #pragma once -#include "r_triangle.h" +#include "r_poly_triangle.h" class RenderPolyWallSprite { diff --git a/src/r_triangle.cpp b/src/r_triangle.cpp deleted file mode 100644 index 13a8ca292..000000000 --- a/src/r_triangle.cpp +++ /dev/null @@ -1,1338 +0,0 @@ -/* -** Triangle drawers -** Copyright (c) 2016 Magnus Norddahl -** -** This software is provided 'as-is', without any express or implied -** warranty. In no event will the authors be held liable for any damages -** arising from the use of this software. -** -** Permission is granted to anyone to use this software for any purpose, -** including commercial applications, and to alter it and redistribute it -** freely, subject to the following restrictions: -** -** 1. The origin of this software must not be misrepresented; you must not -** claim that you wrote the original software. If you use this software -** in a product, an acknowledgment in the product documentation would be -** appreciated but is not required. -** 2. Altered source versions must be plainly marked as such, and must not be -** misrepresented as being the original software. -** 3. This notice may not be removed or altered from any source distribution. -** -*/ - -#include -#include "templates.h" -#include "doomdef.h" -#include "i_system.h" -#include "w_wad.h" -#include "r_local.h" -#include "v_video.h" -#include "doomstat.h" -#include "st_stuff.h" -#include "g_game.h" -#include "g_level.h" -#include "r_data/r_translate.h" -#include "v_palette.h" -#include "r_data/colormaps.h" -#include "r_triangle.h" - -void TriangleDrawer::draw(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, FTexture *texture) -{ - if (r_swtruecolor) - queue_arrays(uniforms, vinput, vcount, mode, ccw, clipleft, clipright, cliptop, clipbottom, (const uint8_t*)texture->GetPixelsBgra(), texture->GetWidth(), texture->GetHeight(), 0); - else - draw_arrays(uniforms, vinput, vcount, mode, ccw, clipleft, clipright, cliptop, clipbottom, texture->GetPixels(), texture->GetWidth(), texture->GetHeight(), 0, nullptr, &ScreenTriangleDrawer::draw); -} - -void TriangleDrawer::fill(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, int solidcolor) -{ - if (r_swtruecolor) - queue_arrays(uniforms, vinput, vcount, mode, ccw, clipleft, clipright, cliptop, clipbottom, nullptr, 0, 0, solidcolor); - else - draw_arrays(uniforms, vinput, vcount, mode, ccw, clipleft, clipright, cliptop, clipbottom, nullptr, 0, 0, solidcolor, nullptr, &ScreenTriangleDrawer::fill); -} - -void TriangleDrawer::queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor) -{ - if (clipright < clipleft || clipleft < 0 || clipright > MAXWIDTH) - return; - - int cliplength = clipright - clipleft + 1; - short *clipdata = (short*)DrawerCommandQueue::AllocMemory(cliplength * 2 * sizeof(short)); - if (!clipdata) - { - DrawerCommandQueue::WaitForWorkers(); - clipdata = (short*)DrawerCommandQueue::AllocMemory(cliplength * 2 * sizeof(short)); - if (!clipdata) - return; - } - - for (int i = 0; i < cliplength; i++) - clipdata[i] = cliptop[clipleft + i]; - for (int i = 0; i < cliplength; i++) - clipdata[cliplength + i] = clipbottom[clipleft + i]; - - DrawerCommandQueue::QueueCommand(uniforms, vinput, vcount, mode, ccw, clipleft, clipright, clipdata, texturePixels, textureWidth, textureHeight, solidcolor); -} - -void TriangleDrawer::draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, WorkerThreadData *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, WorkerThreadData *)) -{ - if (vcount < 3) - return; - - ScreenTriangleDrawerArgs args; - args.dest = dc_destorg; - args.pitch = dc_pitch; - args.clipleft = clipleft; - args.clipright = clipright; - args.cliptop = cliptop; - args.clipbottom = clipbottom; - args.texturePixels = texturePixels; - args.textureWidth = textureWidth; - args.textureHeight = textureHeight; - args.solidcolor = solidcolor; - args.uniforms = &uniforms; - - TriVertex vert[3]; - if (mode == TriangleDrawMode::Normal) - { - for (int i = 0; i < vcount / 3; i++) - { - for (int j = 0; j < 3; j++) - vert[j] = shade_vertex(uniforms, *(vinput++)); - draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); - } - } - else if (mode == TriangleDrawMode::Fan) - { - vert[0] = shade_vertex(uniforms, *(vinput++)); - vert[1] = shade_vertex(uniforms, *(vinput++)); - for (int i = 2; i < vcount; i++) - { - vert[2] = shade_vertex(uniforms, *(vinput++)); - draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); - vert[1] = vert[2]; - } - } - else // TriangleDrawMode::Strip - { - vert[0] = shade_vertex(uniforms, *(vinput++)); - vert[1] = shade_vertex(uniforms, *(vinput++)); - for (int i = 2; i < vcount; i++) - { - vert[2] = shade_vertex(uniforms, *(vinput++)); - draw_shaded_triangle(vert, ccw, &args, thread, drawfunc); - vert[0] = vert[1]; - vert[1] = vert[2]; - ccw = !ccw; - } - } -} - -TriVertex TriangleDrawer::shade_vertex(const TriUniforms &uniforms, TriVertex v) -{ - // Apply transform to get clip coordinates: - return uniforms.objectToClip * v; -} - -void TriangleDrawer::draw_shaded_triangle(const TriVertex *vert, bool ccw, ScreenTriangleDrawerArgs *args, WorkerThreadData *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, WorkerThreadData *)) -{ - // Cull, clip and generate additional vertices as needed - TriVertex clippedvert[max_additional_vertices]; - int numclipvert; - clipedge(vert, clippedvert, numclipvert); - - // Map to 2D viewport: - for (int j = 0; j < numclipvert; j++) - { - auto &v = clippedvert[j]; - - // Calculate normalized device coordinates: - v.w = 1.0f / v.w; - v.x *= v.w; - v.y *= v.w; - v.z *= v.w; - - // Apply viewport scale to get screen coordinates: - v.x = viewwidth * (1.0f + v.x) * 0.5f; - v.y = viewheight * (1.0f - v.y) * 0.5f; - } - - // Draw screen triangles - if (ccw) - { - for (int i = numclipvert; i > 1; i--) - { - args->v1 = &clippedvert[numclipvert - 1]; - args->v2 = &clippedvert[i - 1]; - args->v3 = &clippedvert[i - 2]; - drawfunc(args, thread); - } - } - else - { - for (int i = 2; i < numclipvert; i++) - { - args->v1 = &clippedvert[0]; - args->v2 = &clippedvert[i - 1]; - args->v3 = &clippedvert[i]; - drawfunc(args, thread); - } - } -} - -bool TriangleDrawer::cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2) -{ - if (clipdistance1 < 0.0f && clipdistance2 < 0.0f) - return true; - - if (clipdistance1 < 0.0f) - t1 = MAX(-clipdistance1 / (clipdistance2 - clipdistance1), 0.0f); - else - t1 = 0.0f; - - if (clipdistance2 < 0.0f) - t2 = MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), 1.0f); - else - t2 = 1.0f; - - return false; -} - -void TriangleDrawer::clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert) -{ - // Clip and cull so that the following is true for all vertices: - // -v.w <= v.x <= v.w - // -v.w <= v.y <= v.w - // -v.w <= v.z <= v.w - - // use barycentric weights while clipping vertices - float weights[max_additional_vertices * 3 * 2]; - for (int i = 0; i < 3; i++) - { - weights[i * 3 + 0] = 0.0f; - weights[i * 3 + 1] = 0.0f; - weights[i * 3 + 2] = 0.0f; - weights[i * 3 + i] = 1.0f; - } - - // halfspace clip distances - float clipdistance[6 * 3]; - for (int i = 0; i < 3; i++) - { - const auto &v = verts[i]; - clipdistance[i * 6 + 0] = v.x + v.w; - clipdistance[i * 6 + 1] = v.w - v.x; - clipdistance[i * 6 + 2] = v.y + v.w; - clipdistance[i * 6 + 3] = v.w - v.y; - clipdistance[i * 6 + 4] = v.z + v.w; - clipdistance[i * 6 + 5] = v.w - v.z; - } - - // Clip against each halfspace - float *input = weights; - float *output = weights + max_additional_vertices * 3; - int inputverts = 3; - int outputverts = 0; - for (int p = 0; p < 6; p++) - { - // Clip each edge - outputverts = 0; - for (int i = 0; i < inputverts; i++) - { - int j = (i + 1) % inputverts; - float clipdistance1 = - clipdistance[0 * 6 + p] * input[i * 3 + 0] + - clipdistance[1 * 6 + p] * input[i * 3 + 1] + - clipdistance[2 * 6 + p] * input[i * 3 + 2]; - - float clipdistance2 = - clipdistance[0 * 6 + p] * input[j * 3 + 0] + - clipdistance[1 * 6 + p] * input[j * 3 + 1] + - clipdistance[2 * 6 + p] * input[j * 3 + 2]; - - float t1, t2; - if (!cullhalfspace(clipdistance1, clipdistance2, t1, t2) && outputverts + 1 < max_additional_vertices) - { - // add t1 vertex - for (int k = 0; k < 3; k++) - output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t1) + input[j * 3 + k] * t1; - outputverts++; - - if (t2 != 1.0f && t2 > t1) - { - // add t2 vertex - for (int k = 0; k < 3; k++) - output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t2) + input[j * 3 + k] * t2; - outputverts++; - } - } - } - std::swap(input, output); - std::swap(inputverts, outputverts); - if (inputverts == 0) - break; - } - - // Convert barycentric weights to actual vertices - numclipvert = inputverts; - for (int i = 0; i < numclipvert; i++) - { - auto &v = clippedvert[i]; - memset(&v, 0, sizeof(TriVertex)); - for (int w = 0; w < 3; w++) - { - float weight = input[i * 3 + w]; - v.x += verts[w].x * weight; - v.y += verts[w].y * weight; - v.z += verts[w].z * weight; - v.w += verts[w].w * weight; - for (int iv = 0; iv < TriVertex::NumVarying; iv++) - v.varying[iv] += verts[w].varying[iv] * weight; - } - } -} - -///////////////////////////////////////////////////////////////////////////// - -void ScreenTriangleDrawer::draw(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread) -{ - uint8_t *dest = args->dest; - int pitch = args->pitch; - const TriVertex &v1 = *args->v1; - const TriVertex &v2 = *args->v2; - const TriVertex &v3 = *args->v3; - int clipleft = args->clipleft; - int clipright = args->clipright; - const short *cliptop = args->cliptop; - const short *clipbottom = args->clipbottom; - const uint8_t *texturePixels = args->texturePixels; - int textureWidth = args->textureWidth; - int textureHeight = args->textureHeight; - - // 28.4 fixed-point coordinates - const int Y1 = (int)round(16.0f * v1.y); - const int Y2 = (int)round(16.0f * v2.y); - const int Y3 = (int)round(16.0f * v3.y); - - const int X1 = (int)round(16.0f * v1.x); - const int X2 = (int)round(16.0f * v2.x); - const int X3 = (int)round(16.0f * v3.x); - - // Deltas - const int DX12 = X1 - X2; - const int DX23 = X2 - X3; - const int DX31 = X3 - X1; - - const int DY12 = Y1 - Y2; - const int DY23 = Y2 - Y3; - const int DY31 = Y3 - Y1; - - // Fixed-point deltas - const int FDX12 = DX12 << 4; - const int FDX23 = DX23 << 4; - const int FDX31 = DX31 << 4; - - const int FDY12 = DY12 << 4; - const int FDY23 = DY23 << 4; - const int FDY31 = DY31 << 4; - - // Bounding rectangle - int clipymin = cliptop[clipleft]; - int clipymax = clipbottom[clipleft]; - for (int i = clipleft + 1; i <= clipright; i++) - { - clipymin = MIN(clipymin, (int)cliptop[i]); - clipymax = MAX(clipymax, (int)clipbottom[i]); - } - int minx = MAX((MIN(MIN(X1, X2), X3) + 0xF) >> 4, clipleft); - int maxx = MIN((MAX(MAX(X1, X2), X3) + 0xF) >> 4, clipright); - int miny = MAX((MIN(MIN(Y1, Y2), Y3) + 0xF) >> 4, clipymin); - int maxy = MIN((MAX(MAX(Y1, Y2), Y3) + 0xF) >> 4, clipymax - 1); - if (minx >= maxx || miny >= maxy) - return; - - // Block size, standard 8x8 (must be power of two) - const int q = 8; - - // Start in corner of 8x8 block - minx &= ~(q - 1); - miny &= ~(q - 1); - - dest += miny * pitch; - - // Half-edge constants - int C1 = DY12 * X1 - DX12 * Y1; - int C2 = DY23 * X2 - DX23 * Y2; - int C3 = DY31 * X3 - DX31 * Y3; - - // Correct for fill convention - if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++; - if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++; - if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++; - - // Gradients - float gradWX = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w); - float gradWY = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w); - float startW = v1.w + gradWX * (minx - v1.x) + gradWY * (miny - v1.y); - float gradVaryingX[TriVertex::NumVarying], gradVaryingY[TriVertex::NumVarying], startVarying[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - gradVaryingX[i] = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w); - gradVaryingY[i] = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w); - startVarying[i] = v1.varying[i] * v1.w + gradVaryingX[i] * (minx - v1.x) + gradVaryingY[i] * (miny - v1.y); - } - - // Loop through blocks - for (int y = miny; y < maxy; y += q) - { - for (int x = minx; x < maxx; x += q) - { - // Corners of block - int x0 = x << 4; - int x1 = (x + q - 1) << 4; - int y0 = y << 4; - int y1 = (y + q - 1) << 4; - - // Evaluate half-space functions - bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0; - bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0; - bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0; - bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0; - int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3); - - bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0; - bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0; - bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0; - bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0; - int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3); - - bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0; - bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0; - bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0; - bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0; - int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3); - - // Skip block when outside an edge - if (a == 0x0 || b == 0x0 || c == 0x0) continue; - - // Check if block needs clipping - int clipcount = 0; - for (int ix = x; ix < x + q; ix++) - { - clipcount += (clipleft > ix) || (clipright < ix) || (cliptop[ix] > y) || (clipbottom[ix] <= y + q - 1); - } - - // Calculate varying variables for affine block - float offx0 = (x - minx) + 0.5f; - float offy0 = (y - miny) + 0.5f; - float offx1 = offx0 + q; - float offy1 = offy0 + q; - float rcpWTL = 1.0f / (startW + offx0 * gradWX + offy0 * gradWY); - float rcpWTR = 1.0f / (startW + offx1 * gradWX + offy0 * gradWY); - float rcpWBL = 1.0f / (startW + offx0 * gradWX + offy1 * gradWY); - float rcpWBR = 1.0f / (startW + offx1 * gradWX + offy1 * gradWY); - float varyingTL[TriVertex::NumVarying]; - float varyingTR[TriVertex::NumVarying]; - float varyingBL[TriVertex::NumVarying]; - float varyingBR[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - varyingTL[i] = (startVarying[i] + offx0 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTL; - varyingTR[i] = (startVarying[i] + offx1 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTR; - varyingBL[i] = ((startVarying[i] + offx0 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBL - varyingTL[i]) * (1.0f / q); - varyingBR[i] = ((startVarying[i] + offx1 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBR - varyingTR[i]) * (1.0f / q); - } - - uint8_t *buffer = dest; - - // Accept whole block when totally covered - if (a == 0xF && b == 0xF && c == 0xF && clipcount == 0) - { - for (int iy = 0; iy < q; iy++) - { - uint32_t varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - float pos = varyingTL[i] + varyingBL[i] * iy; - float step = (varyingTR[i] + varyingBR[i] * iy - pos) * (1.0f / q); - - varying[i] = (uint32_t)((pos - floor(pos)) * 0x100000000LL); - varyingStep[i] = (uint32_t)(step * 0x100000000LL); - } - - for (int ix = x; ix < x + q; ix++) - { - uint32_t ufrac = varying[0]; - uint32_t vfrac = varying[1]; - - uint32_t upos = ((ufrac >> 16) * textureWidth) >> 16; - uint32_t vpos = ((vfrac >> 16) * textureHeight) >> 16; - uint32_t uvoffset = upos * textureHeight + vpos; - - buffer[ix] = texturePixels[uvoffset]; - - for (int i = 0; i < TriVertex::NumVarying; i++) - varying[i] += varyingStep[i]; - } - - buffer += pitch; - } - } - else // Partially covered block - { - int CY1 = C1 + DX12 * y0 - DY12 * x0; - int CY2 = C2 + DX23 * y0 - DY23 * x0; - int CY3 = C3 + DX31 * y0 - DY31 * x0; - - for (int iy = 0; iy < q; iy++) - { - int CX1 = CY1; - int CX2 = CY2; - int CX3 = CY3; - - uint32_t varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - float pos = varyingTL[i] + varyingBL[i] * iy; - float step = (varyingTR[i] + varyingBR[i] * iy - pos) * (1.0f / q); - - varying[i] = (uint32_t)((pos - floor(pos)) * 0x100000000LL); - varyingStep[i] = (uint32_t)(step * 0x100000000LL); - } - - for (int ix = x; ix < x + q; ix++) - { - bool visible = ix >= clipleft && ix <= clipright && (cliptop[ix] <= y + iy) && (clipbottom[ix] > y + iy); - - if (CX1 > 0 && CX2 > 0 && CX3 > 0 && visible) - { - uint32_t ufrac = varying[0]; - uint32_t vfrac = varying[1]; - - uint32_t upos = ((ufrac >> 16) * textureWidth) >> 16; - uint32_t vpos = ((vfrac >> 16) * textureHeight) >> 16; - uint32_t uvoffset = upos * textureHeight + vpos; - - buffer[ix] = texturePixels[uvoffset]; - } - - for (int i = 0; i < TriVertex::NumVarying; i++) - varying[i] += varyingStep[i]; - - CX1 -= FDY12; - CX2 -= FDY23; - CX3 -= FDY31; - } - - CY1 += FDX12; - CY2 += FDX23; - CY3 += FDX31; - - buffer += pitch; - } - } - } - - dest += q * pitch; - } -} - -void ScreenTriangleDrawer::fill(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread) -{ - uint8_t *dest = args->dest; - int pitch = args->pitch; - const TriVertex &v1 = *args->v1; - const TriVertex &v2 = *args->v2; - const TriVertex &v3 = *args->v3; - int clipleft = args->clipleft; - int clipright = args->clipright; - const short *cliptop = args->cliptop; - const short *clipbottom = args->clipbottom; - int solidcolor = args->solidcolor; - - // 28.4 fixed-point coordinates - const int Y1 = (int)round(16.0f * v1.y); - const int Y2 = (int)round(16.0f * v2.y); - const int Y3 = (int)round(16.0f * v3.y); - - const int X1 = (int)round(16.0f * v1.x); - const int X2 = (int)round(16.0f * v2.x); - const int X3 = (int)round(16.0f * v3.x); - - // Deltas - const int DX12 = X1 - X2; - const int DX23 = X2 - X3; - const int DX31 = X3 - X1; - - const int DY12 = Y1 - Y2; - const int DY23 = Y2 - Y3; - const int DY31 = Y3 - Y1; - - // Fixed-point deltas - const int FDX12 = DX12 << 4; - const int FDX23 = DX23 << 4; - const int FDX31 = DX31 << 4; - - const int FDY12 = DY12 << 4; - const int FDY23 = DY23 << 4; - const int FDY31 = DY31 << 4; - - // Bounding rectangle - int clipymin = cliptop[clipleft]; - int clipymax = clipbottom[clipleft]; - for (int i = clipleft + 1; i <= clipright; i++) - { - clipymin = MIN(clipymin, (int)cliptop[i]); - clipymax = MAX(clipymax, (int)clipbottom[i]); - } - int minx = MAX((MIN(MIN(X1, X2), X3) + 0xF) >> 4, clipleft); - int maxx = MIN((MAX(MAX(X1, X2), X3) + 0xF) >> 4, clipright); - int miny = MAX((MIN(MIN(Y1, Y2), Y3) + 0xF) >> 4, clipymin); - int maxy = MIN((MAX(MAX(Y1, Y2), Y3) + 0xF) >> 4, clipymax - 1); - if (minx >= maxx || miny >= maxy) - return; - - // Block size, standard 8x8 (must be power of two) - const int q = 8; - - // Start in corner of 8x8 block - minx &= ~(q - 1); - miny &= ~(q - 1); - - dest += miny * pitch; - - // Half-edge constants - int C1 = DY12 * X1 - DX12 * Y1; - int C2 = DY23 * X2 - DX23 * Y2; - int C3 = DY31 * X3 - DX31 * Y3; - - // Correct for fill convention - if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++; - if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++; - if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++; - - // Loop through blocks - for (int y = miny; y < maxy; y += q) - { - for (int x = minx; x < maxx; x += q) - { - // Corners of block - int x0 = x << 4; - int x1 = (x + q - 1) << 4; - int y0 = y << 4; - int y1 = (y + q - 1) << 4; - - // Evaluate half-space functions - bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0; - bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0; - bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0; - bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0; - int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3); - - bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0; - bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0; - bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0; - bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0; - int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3); - - bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0; - bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0; - bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0; - bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0; - int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3); - - // Skip block when outside an edge - if (a == 0x0 || b == 0x0 || c == 0x0) continue; - - // Check if block needs clipping - int clipcount = 0; - for (int ix = x; ix < x + q; ix++) - { - clipcount += (clipleft > ix) || (clipright < ix) || (cliptop[ix] > y) || (clipbottom[ix] <= y + q - 1); - } - - uint8_t *buffer = dest; - - // Accept whole block when totally covered - if (a == 0xF && b == 0xF && c == 0xF && clipcount == 0) - { - for (int iy = 0; iy < q; iy++) - { - for (int ix = x; ix < x + q; ix++) - { - buffer[ix] = solidcolor; - } - - buffer += pitch; - } - } - else // Partially covered block - { - int CY1 = C1 + DX12 * y0 - DY12 * x0; - int CY2 = C2 + DX23 * y0 - DY23 * x0; - int CY3 = C3 + DX31 * y0 - DY31 * x0; - - for (int iy = 0; iy < q; iy++) - { - int CX1 = CY1; - int CX2 = CY2; - int CX3 = CY3; - - for (int ix = x; ix < x + q; ix++) - { - bool visible = ix >= clipleft && ix <= clipright && (cliptop[ix] <= y + iy) && (clipbottom[ix] > y + iy); - - if (CX1 > 0 && CX2 > 0 && CX3 > 0 && visible) - { - buffer[ix] = solidcolor; - } - - CX1 -= FDY12; - CX2 -= FDY23; - CX3 -= FDY31; - } - - CY1 += FDX12; - CY2 += FDX23; - CY3 += FDX31; - - buffer += pitch; - } - } - } - - dest += q * pitch; - } -} - -void ScreenTriangleDrawer::draw32(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread) -{ - uint32_t *dest = (uint32_t *)args->dest; - int pitch = args->pitch; - const TriVertex &v1 = *args->v1; - const TriVertex &v2 = *args->v2; - const TriVertex &v3 = *args->v3; - int clipleft = args->clipleft; - int clipright = args->clipright; - const short *cliptop = args->cliptop; - const short *clipbottom = args->clipbottom; - const uint32_t *texturePixels = (const uint32_t *)args->texturePixels; - int textureWidth = args->textureWidth; - int textureHeight = args->textureHeight; - uint32_t light = args->uniforms->light; - - // 28.4 fixed-point coordinates - const int Y1 = (int)round(16.0f * v1.y); - const int Y2 = (int)round(16.0f * v2.y); - const int Y3 = (int)round(16.0f * v3.y); - - const int X1 = (int)round(16.0f * v1.x); - const int X2 = (int)round(16.0f * v2.x); - const int X3 = (int)round(16.0f * v3.x); - - // Deltas - const int DX12 = X1 - X2; - const int DX23 = X2 - X3; - const int DX31 = X3 - X1; - - const int DY12 = Y1 - Y2; - const int DY23 = Y2 - Y3; - const int DY31 = Y3 - Y1; - - // Fixed-point deltas - const int FDX12 = DX12 << 4; - const int FDX23 = DX23 << 4; - const int FDX31 = DX31 << 4; - - const int FDY12 = DY12 << 4; - const int FDY23 = DY23 << 4; - const int FDY31 = DY31 << 4; - - // Bounding rectangle - int clipymin = cliptop[clipleft]; - int clipymax = clipbottom[clipleft]; - for (int i = clipleft + 1; i <= clipright; i++) - { - clipymin = MIN(clipymin, (int)cliptop[i]); - clipymax = MAX(clipymax, (int)clipbottom[i]); - } - int minx = MAX((MIN(MIN(X1, X2), X3) + 0xF) >> 4, clipleft); - int maxx = MIN((MAX(MAX(X1, X2), X3) + 0xF) >> 4, clipright); - int miny = MAX((MIN(MIN(Y1, Y2), Y3) + 0xF) >> 4, clipymin); - int maxy = MIN((MAX(MAX(Y1, Y2), Y3) + 0xF) >> 4, clipymax - 1); - if (minx >= maxx || miny >= maxy) - return; - - // Block size, standard 8x8 (must be power of two) - const int q = 8; - - // Start in corner of 8x8 block - minx &= ~(q - 1); - miny &= ~(q - 1); - - dest += miny * pitch; - - // Half-edge constants - int C1 = DY12 * X1 - DX12 * Y1; - int C2 = DY23 * X2 - DX23 * Y2; - int C3 = DY31 * X3 - DX31 * Y3; - - // Correct for fill convention - if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++; - if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++; - if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++; - - // Gradients - float gradWX = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w); - float gradWY = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w); - float startW = v1.w + gradWX * (minx - v1.x) + gradWY * (miny - v1.y); - float gradVaryingX[TriVertex::NumVarying], gradVaryingY[TriVertex::NumVarying], startVarying[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - gradVaryingX[i] = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w); - gradVaryingY[i] = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w); - startVarying[i] = v1.varying[i] * v1.w + gradVaryingX[i] * (minx - v1.x) + gradVaryingY[i] * (miny - v1.y); - } - - // Loop through blocks - for (int y = miny; y < maxy; y += q) - { - for (int x = minx; x < maxx; x += q) - { - // Corners of block - int x0 = x << 4; - int x1 = (x + q - 1) << 4; - int y0 = y << 4; - int y1 = (y + q - 1) << 4; - - // Evaluate half-space functions - bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0; - bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0; - bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0; - bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0; - int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3); - - bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0; - bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0; - bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0; - bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0; - int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3); - - bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0; - bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0; - bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0; - bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0; - int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3); - - // Skip block when outside an edge - if (a == 0x0 || b == 0x0 || c == 0x0) continue; - - // Check if block needs clipping - int clipcount = 0; - for (int ix = x; ix < x + q; ix++) - { - clipcount += (clipleft > ix) || (clipright < ix) || (cliptop[ix] > y) || (clipbottom[ix] <= y + q - 1); - } - - // Calculate varying variables for affine block - float offx0 = (x - minx) + 0.5f; - float offy0 = (y - miny) + 0.5f; - float offx1 = offx0 + q; - float offy1 = offy0 + q; - float rcpWTL = 1.0f / (startW + offx0 * gradWX + offy0 * gradWY); - float rcpWTR = 1.0f / (startW + offx1 * gradWX + offy0 * gradWY); - float rcpWBL = 1.0f / (startW + offx0 * gradWX + offy1 * gradWY); - float rcpWBR = 1.0f / (startW + offx1 * gradWX + offy1 * gradWY); - float varyingTL[TriVertex::NumVarying]; - float varyingTR[TriVertex::NumVarying]; - float varyingBL[TriVertex::NumVarying]; - float varyingBR[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - varyingTL[i] = (startVarying[i] + offx0 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTL; - varyingTR[i] = (startVarying[i] + offx1 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTR; - varyingBL[i] = ((startVarying[i] + offx0 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBL - varyingTL[i]) * (1.0f / q); - varyingBR[i] = ((startVarying[i] + offx1 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBR - varyingTR[i]) * (1.0f / q); - } - - uint32_t *buffer = dest; - - // Accept whole block when totally covered - if (a == 0xF && b == 0xF && c == 0xF && clipcount == 0) - { - for (int iy = 0; iy < q; iy++) - { - uint32_t varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - float pos = varyingTL[i] + varyingBL[i] * iy; - float step = (varyingTR[i] + varyingBR[i] * iy - pos) * (1.0f / q); - - varying[i] = (uint32_t)((pos - floor(pos)) * 0x100000000LL); - varyingStep[i] = (uint32_t)(step * 0x100000000LL); - } - - if ((y + iy) % thread->num_cores == thread->core) - { - for (int ix = x; ix < x + q; ix++) - { - uint32_t ufrac = varying[0]; - uint32_t vfrac = varying[1]; - - uint32_t upos = ((ufrac >> 16) * textureWidth) >> 16; - uint32_t vpos = ((vfrac >> 16) * textureHeight) >> 16; - uint32_t uvoffset = upos * textureHeight + vpos; - - uint32_t fg = texturePixels[uvoffset]; - uint32_t fg_red = (RPART(fg) * light) >> 8; - uint32_t fg_green = (GPART(fg) * light) >> 8; - uint32_t fg_blue = (BPART(fg) * light) >> 8; - uint32_t fg_alpha = APART(fg); - - if (fg_alpha > 127) - buffer[ix] = 0xff000000 | (fg_red << 16) | (fg_green << 8) | fg_blue; - - for (int i = 0; i < TriVertex::NumVarying; i++) - varying[i] += varyingStep[i]; - } - } - - buffer += pitch; - } - } - else // Partially covered block - { - int CY1 = C1 + DX12 * y0 - DY12 * x0; - int CY2 = C2 + DX23 * y0 - DY23 * x0; - int CY3 = C3 + DX31 * y0 - DY31 * x0; - - for (int iy = 0; iy < q; iy++) - { - int CX1 = CY1; - int CX2 = CY2; - int CX3 = CY3; - - float varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying]; - for (int i = 0; i < TriVertex::NumVarying; i++) - { - varying[i] = varyingTL[i] + varyingBL[i] * iy; - varyingStep[i] = (varyingTR[i] + varyingBR[i] * iy - varying[i]) * (1.0f / q); - } - - if ((y + iy) % thread->num_cores == thread->core) - { - for (int ix = x; ix < x + q; ix++) - { - bool visible = ix >= clipleft && ix <= clipright && (cliptop[ix] <= y + iy) && (clipbottom[ix] > y + iy); - - if (CX1 > 0 && CX2 > 0 && CX3 > 0 && visible) - { - uint32_t ufrac = (uint32_t)((varying[0] - floor(varying[0])) * 0x100000000LL); - uint32_t vfrac = (uint32_t)((varying[1] - floor(varying[1])) * 0x100000000LL); - - uint32_t upos = ((ufrac >> 16) * textureWidth) >> 16; - uint32_t vpos = ((vfrac >> 16) * textureHeight) >> 16; - uint32_t uvoffset = upos * textureHeight + vpos; - - uint32_t fg = texturePixels[uvoffset]; - uint32_t fg_red = (RPART(fg) * light) >> 8; - uint32_t fg_green = (GPART(fg) * light) >> 8; - uint32_t fg_blue = (BPART(fg) * light) >> 8; - uint32_t fg_alpha = APART(fg); - - if (fg_alpha > 127) - buffer[ix] = 0xff000000 | (fg_red << 16) | (fg_green << 8) | fg_blue; - } - - for (int i = 0; i < TriVertex::NumVarying; i++) - varying[i] += varyingStep[i]; - - CX1 -= FDY12; - CX2 -= FDY23; - CX3 -= FDY31; - } - } - - CY1 += FDX12; - CY2 += FDX23; - CY3 += FDX31; - - buffer += pitch; - } - } - } - - dest += q * pitch; - } -} - -void ScreenTriangleDrawer::fill32(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread) -{ - uint32_t *dest = (uint32_t *)args->dest; - int pitch = args->pitch; - const TriVertex &v1 = *args->v1; - const TriVertex &v2 = *args->v2; - const TriVertex &v3 = *args->v3; - int clipleft = args->clipleft; - int clipright = args->clipright; - const short *cliptop = args->cliptop; - const short *clipbottom = args->clipbottom; - int solidcolor = args->solidcolor; - - // 28.4 fixed-point coordinates - const int Y1 = (int)round(16.0f * v1.y); - const int Y2 = (int)round(16.0f * v2.y); - const int Y3 = (int)round(16.0f * v3.y); - - const int X1 = (int)round(16.0f * v1.x); - const int X2 = (int)round(16.0f * v2.x); - const int X3 = (int)round(16.0f * v3.x); - - // Deltas - const int DX12 = X1 - X2; - const int DX23 = X2 - X3; - const int DX31 = X3 - X1; - - const int DY12 = Y1 - Y2; - const int DY23 = Y2 - Y3; - const int DY31 = Y3 - Y1; - - // Fixed-point deltas - const int FDX12 = DX12 << 4; - const int FDX23 = DX23 << 4; - const int FDX31 = DX31 << 4; - - const int FDY12 = DY12 << 4; - const int FDY23 = DY23 << 4; - const int FDY31 = DY31 << 4; - - // Bounding rectangle - int clipymin = cliptop[clipleft]; - int clipymax = clipbottom[clipleft]; - for (int i = clipleft + 1; i <= clipright; i++) - { - clipymin = MIN(clipymin, (int)cliptop[i]); - clipymax = MAX(clipymax, (int)clipbottom[i]); - } - int minx = MAX((MIN(MIN(X1, X2), X3) + 0xF) >> 4, clipleft); - int maxx = MIN((MAX(MAX(X1, X2), X3) + 0xF) >> 4, clipright); - int miny = MAX((MIN(MIN(Y1, Y2), Y3) + 0xF) >> 4, clipymin); - int maxy = MIN((MAX(MAX(Y1, Y2), Y3) + 0xF) >> 4, clipymax - 1); - if (minx >= maxx || miny >= maxy) - return; - - // Block size, standard 8x8 (must be power of two) - const int q = 8; - - // Start in corner of 8x8 block - minx &= ~(q - 1); - miny &= ~(q - 1); - - dest += miny * pitch; - - // Half-edge constants - int C1 = DY12 * X1 - DX12 * Y1; - int C2 = DY23 * X2 - DX23 * Y2; - int C3 = DY31 * X3 - DX31 * Y3; - - // Correct for fill convention - if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++; - if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++; - if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++; - - // Loop through blocks - for (int y = miny; y < maxy; y += q) - { - for (int x = minx; x < maxx; x += q) - { - // Corners of block - int x0 = x << 4; - int x1 = (x + q - 1) << 4; - int y0 = y << 4; - int y1 = (y + q - 1) << 4; - - // Evaluate half-space functions - bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0; - bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0; - bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0; - bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0; - int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3); - - bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0; - bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0; - bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0; - bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0; - int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3); - - bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0; - bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0; - bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0; - bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0; - int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3); - - // Skip block when outside an edge - if (a == 0x0 || b == 0x0 || c == 0x0) continue; - - // Check if block needs clipping - int clipcount = 0; - for (int ix = x; ix < x + q; ix++) - { - clipcount += (clipleft > ix) || (clipright < ix) || (cliptop[ix] > y) || (clipbottom[ix] <= y + q - 1); - } - - uint32_t *buffer = dest; - - // Accept whole block when totally covered - if (a == 0xF && b == 0xF && c == 0xF && clipcount == 0) - { - for (int iy = 0; iy < q; iy++) - { - if ((y + iy) % thread->num_cores == thread->core) - { - for (int ix = x; ix < x + q; ix++) - { - buffer[ix] = solidcolor; - } - } - - buffer += pitch; - } - } - else // Partially covered block - { - int CY1 = C1 + DX12 * y0 - DY12 * x0; - int CY2 = C2 + DX23 * y0 - DY23 * x0; - int CY3 = C3 + DX31 * y0 - DY31 * x0; - - for (int iy = 0; iy < q; iy++) - { - int CX1 = CY1; - int CX2 = CY2; - int CX3 = CY3; - - if ((y + iy) % thread->num_cores == thread->core) - { - for (int ix = x; ix < x + q; ix++) - { - bool visible = ix >= clipleft && ix <= clipright && (cliptop[ix] <= y + iy) && (clipbottom[ix] > y + iy); - - if (CX1 > 0 && CX2 > 0 && CX3 > 0 && visible) - { - buffer[ix] = solidcolor; - } - - CX1 -= FDY12; - CX2 -= FDY23; - CX3 -= FDY31; - } - } - - CY1 += FDX12; - CY2 += FDX23; - CY3 += FDX31; - - buffer += pitch; - } - } - } - - dest += q * pitch; - } -} - -float ScreenTriangleDrawer::gradx(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2) -{ - float top = (c1 - c2) * (y0 - y2) - (c0 - c2) * (y1 - y2); - float bottom = (x1 - x2) * (y0 - y2) - (x0 - x2) * (y1 - y2); - return top / bottom; -} - -float ScreenTriangleDrawer::grady(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2) -{ - float top = (c1 - c2) * (x0 - x2) - (c0 - c2) * (x1 - x2); - float bottom = -((x1 - x2) * (y0 - y2) - (x0 - x2) * (y1 - y2)); - return top / bottom; -} - -///////////////////////////////////////////////////////////////////////////// - -TriMatrix TriMatrix::null() -{ - TriMatrix m; - memset(m.matrix, 0, sizeof(m.matrix)); - return m; -} - -TriMatrix TriMatrix::identity() -{ - TriMatrix m = null(); - m.matrix[0] = 1.0f; - m.matrix[5] = 1.0f; - m.matrix[10] = 1.0f; - m.matrix[15] = 1.0f; - return m; -} - -TriMatrix TriMatrix::translate(float x, float y, float z) -{ - TriMatrix m = identity(); - m.matrix[0 + 3 * 4] = x; - m.matrix[1 + 3 * 4] = y; - m.matrix[2 + 3 * 4] = z; - return m; -} - -TriMatrix TriMatrix::scale(float x, float y, float z) -{ - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = x; - m.matrix[1 + 1 * 4] = y; - m.matrix[2 + 2 * 4] = z; - m.matrix[3 + 3 * 4] = 1; - return m; -} - -TriMatrix TriMatrix::rotate(float angle, float x, float y, float z) -{ - float c = cosf(angle); - float s = sinf(angle); - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = (x*x*(1.0f - c) + c); - m.matrix[0 + 1 * 4] = (x*y*(1.0f - c) - z*s); - m.matrix[0 + 2 * 4] = (x*z*(1.0f - c) + y*s); - m.matrix[1 + 0 * 4] = (y*x*(1.0f - c) + z*s); - m.matrix[1 + 1 * 4] = (y*y*(1.0f - c) + c); - m.matrix[1 + 2 * 4] = (y*z*(1.0f - c) - x*s); - m.matrix[2 + 0 * 4] = (x*z*(1.0f - c) - y*s); - m.matrix[2 + 1 * 4] = (y*z*(1.0f - c) + x*s); - m.matrix[2 + 2 * 4] = (z*z*(1.0f - c) + c); - m.matrix[3 + 3 * 4] = 1.0f; - return m; -} - -TriMatrix TriMatrix::swapYZ() -{ - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = 1.0f; - m.matrix[1 + 2 * 4] = 1.0f; - m.matrix[2 + 1 * 4] = -1.0f; - m.matrix[3 + 3 * 4] = 1.0f; - return m; -} - -TriMatrix TriMatrix::perspective(float fovy, float aspect, float z_near, float z_far) -{ - float f = (float)(1.0 / tan(fovy * M_PI / 360.0)); - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = f / aspect; - m.matrix[1 + 1 * 4] = f; - m.matrix[2 + 2 * 4] = (z_far + z_near) / (z_near - z_far); - m.matrix[2 + 3 * 4] = (2.0f * z_far * z_near) / (z_near - z_far); - m.matrix[3 + 2 * 4] = -1.0f; - return m; -} - -TriMatrix TriMatrix::frustum(float left, float right, float bottom, float top, float near, float far) -{ - float a = (right + left) / (right - left); - float b = (top + bottom) / (top - bottom); - float c = -(far + near) / (far - near); - float d = -(2.0f * far) / (far - near); - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = 2.0f * near / (right - left); - m.matrix[1 + 1 * 4] = 2.0f * near / (top - bottom); - m.matrix[0 + 2 * 4] = a; - m.matrix[1 + 2 * 4] = b; - m.matrix[2 + 2 * 4] = c; - m.matrix[2 + 3 * 4] = d; - m.matrix[3 + 2 * 4] = -1; - return m; -} - -TriMatrix TriMatrix::worldToView() -{ - TriMatrix m = null(); - m.matrix[0 + 0 * 4] = (float)ViewSin; - m.matrix[0 + 1 * 4] = (float)-ViewCos; - m.matrix[1 + 2 * 4] = 1.0f; - m.matrix[2 + 0 * 4] = (float)-ViewCos; - m.matrix[2 + 1 * 4] = (float)-ViewSin; - m.matrix[3 + 3 * 4] = 1.0f; - return m * translate((float)-ViewPos.X, (float)-ViewPos.Y, (float)-ViewPos.Z); -} - -TriMatrix TriMatrix::viewToClip() -{ - float near = 5.0f; - float far = 65536.0f; - float width = (float)(FocalTangent * near); - float top = (float)(CenterY / InvZtoScale * near); - float bottom = (float)(top - viewheight / InvZtoScale * near); - return frustum(-width, width, bottom, top, near, far); -} - -TriMatrix TriMatrix::operator*(const TriMatrix &mult) const -{ - TriMatrix result; - for (int x = 0; x < 4; x++) - { - for (int y = 0; y < 4; y++) - { - result.matrix[x + y * 4] = - matrix[0 * 4 + x] * mult.matrix[y * 4 + 0] + - matrix[1 * 4 + x] * mult.matrix[y * 4 + 1] + - matrix[2 * 4 + x] * mult.matrix[y * 4 + 2] + - matrix[3 * 4 + x] * mult.matrix[y * 4 + 3]; - } - } - return result; -} - -TriVertex TriMatrix::operator*(TriVertex v) const -{ - float vx = matrix[0 * 4 + 0] * v.x + matrix[1 * 4 + 0] * v.y + matrix[2 * 4 + 0] * v.z + matrix[3 * 4 + 0] * v.w; - float vy = matrix[0 * 4 + 1] * v.x + matrix[1 * 4 + 1] * v.y + matrix[2 * 4 + 1] * v.z + matrix[3 * 4 + 1] * v.w; - float vz = matrix[0 * 4 + 2] * v.x + matrix[1 * 4 + 2] * v.y + matrix[2 * 4 + 2] * v.z + matrix[3 * 4 + 2] * v.w; - float vw = matrix[0 * 4 + 3] * v.x + matrix[1 * 4 + 3] * v.y + matrix[2 * 4 + 3] * v.z + matrix[3 * 4 + 3] * v.w; - v.x = vx; - v.y = vy; - v.z = vz; - v.w = vw; - return v; -} - -///////////////////////////////////////////////////////////////////////////// - -DrawTrianglesCommand::DrawTrianglesCommand(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *clipdata, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor) - : uniforms(uniforms), vinput(vinput), vcount(vcount), mode(mode), ccw(ccw), clipleft(clipleft), clipright(clipright), clipdata(clipdata), texturePixels(texturePixels), textureWidth(textureWidth), textureHeight(textureHeight), solidcolor(solidcolor) -{ -} - -void DrawTrianglesCommand::Execute(DrawerThread *thread) -{ - int cliplength = clipright - clipleft + 1; - for (int i = 0; i < cliplength; i++) - { - thread->triangle_clip_top[clipleft + i] = clipdata[i]; - thread->triangle_clip_bottom[clipleft + i] = clipdata[cliplength + i]; - } - - WorkerThreadData thread_data; - thread_data.core = thread->core; - thread_data.num_cores = thread->num_cores; - thread_data.pass_start_y = thread->pass_start_y; - thread_data.pass_end_y = thread->pass_end_y; - thread_data.temp = thread->dc_temp_rgba; - - TriangleDrawer::draw_arrays( - uniforms, vinput, vcount, mode, ccw, - clipleft, clipright, thread->triangle_clip_top, thread->triangle_clip_bottom, - texturePixels, textureWidth, textureHeight, solidcolor, - &thread_data, texturePixels ? ScreenTriangleDrawer::draw32 : ScreenTriangleDrawer::fill32); -} - -FString DrawTrianglesCommand::DebugInfo() -{ - return "DrawTriangles"; -} diff --git a/src/r_triangle.h b/src/r_triangle.h deleted file mode 100644 index 6ec413d3f..000000000 --- a/src/r_triangle.h +++ /dev/null @@ -1,116 +0,0 @@ -/* -** Triangle drawers -** Copyright (c) 2016 Magnus Norddahl -** -** This software is provided 'as-is', without any express or implied -** warranty. In no event will the authors be held liable for any damages -** arising from the use of this software. -** -** Permission is granted to anyone to use this software for any purpose, -** including commercial applications, and to alter it and redistribute it -** freely, subject to the following restrictions: -** -** 1. The origin of this software must not be misrepresented; you must not -** claim that you wrote the original software. If you use this software -** in a product, an acknowledgment in the product documentation would be -** appreciated but is not required. -** 2. Altered source versions must be plainly marked as such, and must not be -** misrepresented as being the original software. -** 3. This notice may not be removed or altered from any source distribution. -** -*/ - - -#ifndef __R_TRIANGLE__ -#define __R_TRIANGLE__ - -#include "r_draw.h" -#include "r_thread.h" -#include "r_compiler/llvmdrawers.h" - -class FTexture; -struct ScreenTriangleDrawerArgs; - -enum class TriangleDrawMode -{ - Normal, - Fan, - Strip -}; - -class TriangleDrawer -{ -public: - static void draw(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, FTexture *texture); - static void fill(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, int solidcolor); - -private: - static TriVertex shade_vertex(const TriUniforms &uniforms, TriVertex v); - static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, WorkerThreadData *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, WorkerThreadData *)); - static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenTriangleDrawerArgs *args, WorkerThreadData *thread, void(*drawfunc)(const ScreenTriangleDrawerArgs *, WorkerThreadData *)); - static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2); - static void clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert); - - static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *cliptop, const short *clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor); - - enum { max_additional_vertices = 16 }; - - friend class DrawTrianglesCommand; -}; - -struct ScreenTriangleDrawerArgs -{ - uint8_t *dest; - int pitch; - TriVertex *v1; - TriVertex *v2; - TriVertex *v3; - int clipleft; - int clipright; - const short *cliptop; - const short *clipbottom; - const uint8_t *texturePixels; - int textureWidth; - int textureHeight; - int solidcolor; - const TriUniforms *uniforms; -}; - -class ScreenTriangleDrawer -{ -public: - static void draw(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread); - static void fill(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread); - - static void draw32(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread); - static void fill32(const ScreenTriangleDrawerArgs *args, WorkerThreadData *thread); - -private: - static float gradx(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2); - static float grady(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2); -}; - -class DrawTrianglesCommand : public DrawerCommand -{ -public: - DrawTrianglesCommand(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, const short *clipdata, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor); - - void Execute(DrawerThread *thread) override; - FString DebugInfo() override; - -private: - const TriUniforms uniforms; - const TriVertex *vinput; - int vcount; - TriangleDrawMode mode; - bool ccw; - int clipleft; - int clipright; - const short *clipdata; - const uint8_t *texturePixels; - int textureWidth; - int textureHeight; - int solidcolor; -}; - -#endif