From 136e5365441dbed502ed0bcedc28b0bcd1516070 Mon Sep 17 00:00:00 2001 From: duarteapcoelho Date: Thu, 8 Dec 2022 15:44:53 +0000 Subject: [PATCH] Make depth buffer 8 bit --- src/main.cpp | 4 ++-- src/rasterizer.cpp | 25 ++++++++++++++++--------- src/rasterizer.h | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a0db20f..53df0ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,13 +28,13 @@ vec3 cameraSpeed = {0, 0, 0}; float cameraAngle = 0; #ifdef GINT -static GALIGNED(32) fp depthBuffer[RENDER_WIDTH*RENDER_HEIGHT]; +static GALIGNED(32) unsigned char depthBuffer[RENDER_WIDTH*RENDER_HEIGHT]; #include "models.h" #endif int main(){ #ifndef GINT - fp depthBuffer[RENDER_WIDTH*RENDER_HEIGHT]; + unsigned char depthBuffer[RENDER_WIDTH*RENDER_HEIGHT]; #include "models.h" #endif Rasterizer::depthBuffer = depthBuffer; diff --git a/src/rasterizer.cpp b/src/rasterizer.cpp index 19db397..9a0db52 100644 --- a/src/rasterizer.cpp +++ b/src/rasterizer.cpp @@ -37,7 +37,7 @@ inline int max(int a, int b){ namespace Rasterizer { Plane clippingPlanes[5]; - fp *depthBuffer; + unsigned char *depthBuffer; fp fov_d = 1; @@ -50,14 +50,21 @@ namespace Rasterizer { } void reset(){ + unsigned char value = -1; +#if GINT || PRIZM + long v = value | (value << 8) | (value << 16) | (value << 24); +#endif #if GINT && PIXEL_SIZE == 1 - fp v = -1; - fp *depthBuffer_P1 = (fp*) mmu_translate_uram(depthBuffer); - cache_ocbp(depthBuffer, RENDER_WIDTH*RENDER_HEIGHT*sizeof(fp)); - dma_memset(depthBuffer_P1, *((uint32_t*)&v), RENDER_WIDTH*RENDER_HEIGHT*sizeof(fp)); + unsigned char *depthBuffer_P1 = (unsigned char*) mmu_translate_uram(depthBuffer); + cache_ocbp(depthBuffer, RENDER_WIDTH*RENDER_HEIGHT*sizeof(unsigned char)); + dma_memset(depthBuffer_P1, *((uint32_t*)&v), RENDER_WIDTH*RENDER_HEIGHT*sizeof(unsigned char)); +#elif PRIZM + for(int i = 0; i < RENDER_WIDTH*RENDER_HEIGHT/4; i++){ + *(((long*)depthBuffer) + i) = v; + } #else for(int i = 0; i < RENDER_WIDTH*RENDER_HEIGHT; i++){ - depthBuffer[i] = -1; + depthBuffer[i] = value; } #endif } @@ -98,7 +105,7 @@ namespace Rasterizer { } // Draws a triangle which has a horizontal top or bottom - inline void _drawFlatSideTriangle(vec3 points[3], fp z, Color color, bool useDepth){ + inline void _drawFlatSideTriangle(vec3 points[3], unsigned char z, Color color, bool useDepth){ if(points[0].y == points[1].y && points[1].y == points[2].y && points[2].y == points[0].y){ return; } @@ -141,7 +148,7 @@ namespace Rasterizer { #endif for(int x = minX; x <= maxX; x++){ - if(z < depthBuffer[p] || depthBuffer[p] == fp(-1) || !useDepth){ + if(z < depthBuffer[p] || !useDepth){ if(useDepth){ depthBuffer[p] = z; } @@ -181,7 +188,7 @@ namespace Rasterizer { return; } - fp z = (points[0].z + points[1].z + points[2].z) / 3; + unsigned char z = (points[0].z + points[1].z + points[2].z) / 3; if(isShaded){ fp brightness = dot(mat4::toMat3(model->modelMatrix) * triangle.normal, vec3(I_SQRT_3, -I_SQRT_3, -I_SQRT_3)) * fp(0.6) + fp(0.4); diff --git a/src/rasterizer.h b/src/rasterizer.h index d6c5ce1..7be2d95 100644 --- a/src/rasterizer.h +++ b/src/rasterizer.h @@ -38,7 +38,7 @@ public: namespace Rasterizer { void init(); void reset(); - extern fp *depthBuffer; + extern unsigned char *depthBuffer; extern fp fov_d; void setFOV(int fov);