From ae080953f7e09a7990a31cde89b7321cf7587762 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 30 Jan 2023 17:29:10 +0100 Subject: [PATCH] Rewrote the other face drawing functions to ditch the vertex copying stage too. --- draw.h | 90 +++++++++++++++++++++++++++++++++++++++++++++------------ world.c | 36 +++-------------------- 2 files changed, 76 insertions(+), 50 deletions(-) diff --git a/draw.h b/draw.h index 2c30cec..bd18ba0 100644 --- a/draw.h +++ b/draw.h @@ -73,28 +73,33 @@ static INLINE void draw_trianglestrip_lit(SVECTOR *verts, u_char numVerts, u_lon } } -static INLINE void draw_quadstrip_lit(SVECTOR *verts, u_char numVerts, u_long *ot) +static INLINE void draw_quadstrip_lit(const ps1bsp_vertex_t *vertices, const ps1bsp_polyvertex_t *polyVerts, u_char numVerts, u_long *ot) { - // Draw the face as a quad strip - const SVECTOR *v0, *v1, *v2, *v3; - const SVECTOR *head = verts; - const SVECTOR *tail = verts + numVerts; + const ps1bsp_polyvertex_t *v0, *v1, *v2, *v3; + u_char i0, i1, i2, i3; + u_char head = 0; + u_char tail = numVerts; // Initialize the first two vertices - v2 = --tail; - v3 = head++; + i2 = --tail; + i3 = head++; // Normally a quad strip would have (N-2)/2 quads, but we might end up with a sole triangle at the end which will be drawn as a collapsed quad u_char numQuads = (numVerts - 1) >> 1; for (u_char quadIdx = 0; quadIdx < numQuads; ++quadIdx) { - v0 = v2; - v1 = v3; - v2 = --tail; - v3 = head++; + i0 = i2; + i1 = i3; + i2 = --tail; + i3 = head++; - // Naively draw the quad with GTE, nothing special or optimized about this - gte_ldv3(v0, v1, v2); + v0 = &polyVerts[i0]; + v1 = &polyVerts[i1]; + v2 = &polyVerts[i2]; + v3 = &polyVerts[i3]; + + // Transform the first three vertices + gte_ldv3(&vertices[v0->index], &vertices[v1->index], &vertices[v2->index]); gte_rtpt(); // Rotation, translation, perspective projection // Draw a flat-shaded untextured colored quad @@ -103,14 +108,63 @@ static INLINE void draw_quadstrip_lit(SVECTOR *verts, u_char numVerts, u_long *o gte_stsxy3_g3(poly); // Transform the fourth vertex to complete the quad - gte_ldv0(v3); + gte_ldv0(&vertices[v3->index]); gte_rtps(); gte_stsxy(&poly->x3); - poly->r0 = poly->g0 = poly->b0 = (uint8_t)v0->pad; - poly->r1 = poly->g1 = poly->b1 = (uint8_t)v1->pad; - poly->r2 = poly->g2 = poly->b2 = (uint8_t)v2->pad; - poly->r3 = poly->g3 = poly->b3 = (uint8_t)v3->pad; + poly->r0 = poly->g0 = poly->b0 = (uint8_t)v0->light; + poly->r1 = poly->g1 = poly->b1 = (uint8_t)v1->light; + poly->r2 = poly->g2 = poly->b2 = (uint8_t)v2->light; + poly->r3 = poly->g3 = poly->b3 = (uint8_t)v3->light; + + addPrim(ot, poly); + ++polyCount; + } +} + +static INLINE void draw_quadstrip_colored(const ps1bsp_vertex_t *vertices, const ps1bsp_facevertex_t *faceVerts, u_char numVerts, u_long *ot) +{ + const ps1bsp_facevertex_t *v0, *v1, *v2, *v3; + u_char i0, i1, i2, i3; + u_char head = 0; + u_char tail = numVerts; + + // Initialize the first two vertices + i2 = --tail; + i3 = head++; + + // Normally a quad strip would have (N-2)/2 quads, but we might end up with a sole triangle at the end which will be drawn as a collapsed quad + u_char numQuads = (numVerts - 1) >> 1; + for (u_char quadIdx = 0; quadIdx < numQuads; ++quadIdx) + { + i0 = i2; + i1 = i3; + i2 = --tail; + i3 = head++; + + v0 = &faceVerts[i0]; + v1 = &faceVerts[i1]; + v2 = &faceVerts[i2]; + v3 = &faceVerts[i3]; + + // Transform the first three vertices + gte_ldv3(&vertices[v0->index], &vertices[v1->index], &vertices[v2->index]); + gte_rtpt(); // Rotation, translation, perspective projection + + // Draw a flat-shaded untextured colored quad + POLY_G4 *poly = (POLY_G4*)mem_prim(sizeof(POLY_G4)); + setPolyG4(poly); + gte_stsxy3_g3(poly); + + // Transform the fourth vertex to complete the quad + gte_ldv0(&vertices[v3->index]); + gte_rtps(); + gte_stsxy(&poly->x3); + + poly->r0 = v0->r << 3; poly->g0 = v0->g << 3; poly->b0 = v0->b << 3; + poly->r1 = v1->r << 3; poly->g1 = v1->g << 3; poly->b1 = v1->b << 3; + poly->r2 = v2->r << 3; poly->g2 = v2->g << 3; poly->b2 = v2->b << 3; + poly->r3 = v3->r << 3; poly->g3 = v3->g << 3; poly->b3 = v3->b << 3; addPrim(ot, poly); ++polyCount; diff --git a/world.c b/world.c index 536f247..0bce96a 100644 --- a/world.c +++ b/world.c @@ -103,22 +103,8 @@ static void world_drawface_fast(const world_t *world, const ps1bsp_face_t *face, return; // Draw untextured, vertex colored faces, skipping the entire polygon tessellation step - SVECTOR *verts = (SVECTOR*)(scratchpad + 256); - SVECTOR *curVert = verts; - const ps1bsp_facevertex_t *faceVertex = &world->faceVertices[face->firstFaceVertex]; - for (u_char vertIdx = 0; vertIdx < face->numFaceVertices; ++vertIdx, ++faceVertex, ++curVert) - { - const ps1bsp_vertex_t *vert = &world->vertices[faceVertex->index]; - curVert->vx = vert->x; - curVert->vy = vert->y; - curVert->vz = vert->z; - curVert->pad = (unsigned short)(size_t)face & 0xFF; // TODO: apply averaged light * color value - } - - if (face->numFaceVertices == 3) - draw_triangle_lit(verts, ot); - else - draw_quadstrip_lit(verts, face->numFaceVertices, ot); + const ps1bsp_facevertex_t *faceVertices = &world->faceVertices[face->firstFaceVertex]; + draw_quadstrip_colored(world->vertices, faceVertices, face->numFaceVertices, ot); } static void world_drawface_lit(const world_t *world, const ps1bsp_face_t *face, u_long *ot) @@ -132,25 +118,11 @@ static void world_drawface_lit(const world_t *world, const ps1bsp_face_t *face, return; // Draw untextured, vertex colored polygons - SVECTOR *verts = (SVECTOR*)(scratchpad + 256); const ps1bsp_polygon_t* poly = &world->polygons[face->firstPolygon]; for (u_char polyIdx = 0; polyIdx < face->numPolygons; ++polyIdx, ++poly) { - ps1bsp_polyvertex_t *polyVertex = &world->polyVertices[poly->firstPolyVertex]; - SVECTOR *curVert = verts; - for (u_char vertIdx = 0; vertIdx < poly->numPolyVertices; ++vertIdx, ++polyVertex, ++curVert) - { - const ps1bsp_vertex_t *vert = &world->vertices[polyVertex->index]; - curVert->vx = vert->x; - curVert->vy = vert->y; - curVert->vz = vert->z; - curVert->pad = polyVertex->light; - } - - if (poly->numPolyVertices == 3) - draw_triangle_lit(verts, ot); - else - draw_quadstrip_lit(verts, poly->numPolyVertices, ot); + ps1bsp_polyvertex_t *polyVertices = &world->polyVertices[poly->firstPolyVertex]; + draw_quadstrip_lit(world->vertices, polyVertices, poly->numPolyVertices, ot); } }