From 77f9dbee1721518e09f0beed10b3dbb78d893b08 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 13 Feb 2026 10:27:58 -0800 Subject: Ambient lighting. Defer texture filtering --- .gitignore | 1 + doc/lighting.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++ doc/pipeline.txt | 15 +++++++++++ doc/triangle-pipeline.txt | 27 ++++++++++++++++++++ include/swgfx.h | 1 + src/swgfx.c | 23 ++++++++++++++--- 6 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 doc/lighting.md create mode 100644 doc/pipeline.txt create mode 100644 doc/triangle-pipeline.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..871a6b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/*.png diff --git a/doc/lighting.md b/doc/lighting.md new file mode 100644 index 0000000..bc111b0 --- /dev/null +++ b/doc/lighting.md @@ -0,0 +1,64 @@ +# Lighting + +## Forward + +``` +for each triangle: + for each pixel: + for each light: + shade(pixel, light) +``` + +- Overdraw penalty. +- Client must specify all lights (for the object) up front. + +## Deferred + +### Forward Pass + +The forward pass generates: +- Depth +- Texture ID +- Texture coordinates + +Sampling of textures is deferred to avoid overdraw penalty. + +### Pixel-centric Shading + +``` +for each pixel: + for each light: + shade(pixel, light) +``` + +- Need a pixel-in-volume test. +- Inefficient if most pixels are not affected by a light. + - Ok: ambient, directional. + - Bad: point, area. + +### Light-centric Shading + +``` +for each light: + volume <- project light volume + for each pixel in volume: + shade(pixel, light) +``` + +- Need to project the light volume. +- Shades only the pixels that are covered by the volume. +- For ambient and directional, the volume is all space. + +## Clustered + +### Cluster Generation + +``` +for each light: + volume <- project light volume to NDC + for each cluster: + test(cluster, volume) +``` + +- Need a volume-AABB test (the cluster is an AABB in NDC space.) +- Lighting then proceeds as in deferred. diff --git a/doc/pipeline.txt b/doc/pipeline.txt new file mode 100644 index 0000000..75bd69e --- /dev/null +++ b/doc/pipeline.txt @@ -0,0 +1,15 @@ +@startuml +:G-buffer; +fork + :Depth; +fork again + :Normal; +fork again + :Tex coords; +fork again + :Tex ID; +end fork +:Lighting; +:Tone map; +:Gamma correct; +@enduml diff --git a/doc/triangle-pipeline.txt b/doc/triangle-pipeline.txt new file mode 100644 index 0000000..064cf5d --- /dev/null +++ b/doc/triangle-pipeline.txt @@ -0,0 +1,27 @@ +@startuml +:Triangles; +:Vertices; +if (Vertex cache?) then (hit) + :Load vertex; +else (miss) + :Transform & Store vertex; +endif +:Transformed triangle (clip space); +:Clip; +:2D triangles; +:Backface cull; +:Rasterize; +:Pixels; +if (Depth test?) then (pass) + if (Alpha mask?) then (opaque) + :Write pixel; + else (transparent) + :Discard; + stop + endif +else (fail) + :Discard; + stop +endif +stop +@enduml diff --git a/include/swgfx.h b/include/swgfx.h index 57ba472..b3f9150 100644 --- a/include/swgfx.h +++ b/include/swgfx.h @@ -112,6 +112,7 @@ void sgTrianglesIndexed(swgfx*, size_t numIndices, const sgIdx* indices, const s void sgTrianglesIndexedNonUniform(swgfx*, size_t numTris, const sgTriIdx* tris, const sgVec3* positions, const sgVec2* texcoords); void sgLighting(swgfx*); +void sgAmbient(swgfx*, sgVec3 ambient); void sgGamma (swgfx*, sgPixel*, int width, int height); void sgGammaInv(swgfx*, sgPixel*, int width, int height); diff --git a/src/swgfx.c b/src/swgfx.c index d41b69d..ddd64f7 100644 --- a/src/swgfx.c +++ b/src/swgfx.c @@ -45,6 +45,7 @@ typedef struct swgfx { R* depth; // Depth buffer. sgTextureId* texture; // Texture ID buffer. sgVec2* texcoords; // Texture coords buffer. + sgPixel* albedo; // Albedo buffer. sgViewport_t viewport; sgMat4 model; // Model matrix. sgMat4 view; // View matrix. @@ -89,6 +90,7 @@ static inline sgVec2 mod2(sgVec2 v, R m) { return (sgVec2){mod1(v.x, m), mod1(v static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; } static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } +static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; } static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};} @@ -739,6 +741,7 @@ size_t sgMem(int width, int height) { Align(N * sizeof(R)) + // Depth buffer. Align(N * sizeof(sgTextureId)) + // Texture ID buffer. Align(N * sizeof(sgVec2)) + // Texture coords buffer. + Align(N * sizeof(sgPixel)) + // Albedo buffer. Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register. (SG_ALIGN - 1); // To make room to align allocations within the buffer. } @@ -752,6 +755,7 @@ swgfx* sgNew(int width, int height, void* mem) { gfx->depth = SG_ALLOC(&aligned, N, R); gfx->texture = SG_ALLOC(&aligned, N, sgTextureId); gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2); + gfx->albedo = SG_ALLOC(&aligned, N, sgPixel); gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture); gfx->activeTexture = DefaultTextureId; gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; @@ -973,10 +977,21 @@ void sgLighting(swgfx* gfx) { const sgTextureId texid = gfx->texture[i]; const sgTexture* texture = &gfx->textureRegister[texid]; const sgVec2 uv = gfx->texcoords[i]; - sgPixel* colour = &gfx->colour[i]; - // TODO: Actual lighting. - const sgPixel albedo = Sample(texture->image, texture->filter, uv); - *colour = albedo; + sgPixel* albedo = &gfx->albedo[i]; + *albedo = Sample(texture->image, texture->filter, uv); + } + } +} + +void sgAmbient(swgfx* gfx, sgVec3 ambient) { + assert(gfx); + const int N = gfx->dims.x * gfx->dims.y; + for (int i = 0; i < N; ++i) { + const R depth = gfx->depth[i]; + if (depth != DepthClearValue) { + const sgPixel* albedo = &gfx->albedo[i]; + sgPixel* colour = &gfx->colour[i]; + *colour = Vec3ToPixel(mul3(PixelToVec3(*albedo), ambient), (R)albedo->a/255.f); } } } -- cgit v1.2.3