diff options
| author | 3gg <3gg@shellblade.net> | 2026-02-13 10:27:58 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2026-02-13 10:27:58 -0800 |
| commit | 77f9dbee1721518e09f0beed10b3dbb78d893b08 (patch) | |
| tree | b9b26485ef1cff497867e120ae27428c6f82c029 | |
| parent | 5f7855b6aa0e3338625eb7d30e8ccc5b74050bbf (diff) | |
Ambient lighting. Defer texture filtering
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | doc/lighting.md | 64 | ||||
| -rw-r--r-- | doc/pipeline.txt | 15 | ||||
| -rw-r--r-- | doc/triangle-pipeline.txt | 27 | ||||
| -rw-r--r-- | include/swgfx.h | 1 | ||||
| -rw-r--r-- | src/swgfx.c | 23 |
6 files changed, 127 insertions, 4 deletions
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 @@ | |||
| 1 | # Lighting | ||
| 2 | |||
| 3 | ## Forward | ||
| 4 | |||
| 5 | ``` | ||
| 6 | for each triangle: | ||
| 7 | for each pixel: | ||
| 8 | for each light: | ||
| 9 | shade(pixel, light) | ||
| 10 | ``` | ||
| 11 | |||
| 12 | - Overdraw penalty. | ||
| 13 | - Client must specify all lights (for the object) up front. | ||
| 14 | |||
| 15 | ## Deferred | ||
| 16 | |||
| 17 | ### Forward Pass | ||
| 18 | |||
| 19 | The forward pass generates: | ||
| 20 | - Depth | ||
| 21 | - Texture ID | ||
| 22 | - Texture coordinates | ||
| 23 | |||
| 24 | Sampling of textures is deferred to avoid overdraw penalty. | ||
| 25 | |||
| 26 | ### Pixel-centric Shading | ||
| 27 | |||
| 28 | ``` | ||
| 29 | for each pixel: | ||
| 30 | for each light: | ||
| 31 | shade(pixel, light) | ||
| 32 | ``` | ||
| 33 | |||
| 34 | - Need a pixel-in-volume test. | ||
| 35 | - Inefficient if most pixels are not affected by a light. | ||
| 36 | - Ok: ambient, directional. | ||
| 37 | - Bad: point, area. | ||
| 38 | |||
| 39 | ### Light-centric Shading | ||
| 40 | |||
| 41 | ``` | ||
| 42 | for each light: | ||
| 43 | volume <- project light volume | ||
| 44 | for each pixel in volume: | ||
| 45 | shade(pixel, light) | ||
| 46 | ``` | ||
| 47 | |||
| 48 | - Need to project the light volume. | ||
| 49 | - Shades only the pixels that are covered by the volume. | ||
| 50 | - For ambient and directional, the volume is all space. | ||
| 51 | |||
| 52 | ## Clustered | ||
| 53 | |||
| 54 | ### Cluster Generation | ||
| 55 | |||
| 56 | ``` | ||
| 57 | for each light: | ||
| 58 | volume <- project light volume to NDC | ||
| 59 | for each cluster: | ||
| 60 | test(cluster, volume) | ||
| 61 | ``` | ||
| 62 | |||
| 63 | - Need a volume-AABB test (the cluster is an AABB in NDC space.) | ||
| 64 | - 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 @@ | |||
| 1 | @startuml | ||
| 2 | :G-buffer; | ||
| 3 | fork | ||
| 4 | :Depth; | ||
| 5 | fork again | ||
| 6 | :Normal; | ||
| 7 | fork again | ||
| 8 | :Tex coords; | ||
| 9 | fork again | ||
| 10 | :Tex ID; | ||
| 11 | end fork | ||
| 12 | :Lighting; | ||
| 13 | :Tone map; | ||
| 14 | :Gamma correct; | ||
| 15 | @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 @@ | |||
| 1 | @startuml | ||
| 2 | :Triangles; | ||
| 3 | :Vertices; | ||
| 4 | if (Vertex cache?) then (hit) | ||
| 5 | :Load vertex; | ||
| 6 | else (miss) | ||
| 7 | :Transform & Store vertex; | ||
| 8 | endif | ||
| 9 | :Transformed triangle (clip space); | ||
| 10 | :Clip; | ||
| 11 | :2D triangles; | ||
| 12 | :Backface cull; | ||
| 13 | :Rasterize; | ||
| 14 | :Pixels; | ||
| 15 | if (Depth test?) then (pass) | ||
| 16 | if (Alpha mask?) then (opaque) | ||
| 17 | :Write pixel; | ||
| 18 | else (transparent) | ||
| 19 | :Discard; | ||
| 20 | stop | ||
| 21 | endif | ||
| 22 | else (fail) | ||
| 23 | :Discard; | ||
| 24 | stop | ||
| 25 | endif | ||
| 26 | stop | ||
| 27 | @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 | |||
| 112 | void sgTrianglesIndexedNonUniform(swgfx*, size_t numTris, const sgTriIdx* tris, const sgVec3* positions, const sgVec2* texcoords); | 112 | void sgTrianglesIndexedNonUniform(swgfx*, size_t numTris, const sgTriIdx* tris, const sgVec3* positions, const sgVec2* texcoords); |
| 113 | 113 | ||
| 114 | void sgLighting(swgfx*); | 114 | void sgLighting(swgfx*); |
| 115 | void sgAmbient(swgfx*, sgVec3 ambient); | ||
| 115 | 116 | ||
| 116 | void sgGamma (swgfx*, sgPixel*, int width, int height); | 117 | void sgGamma (swgfx*, sgPixel*, int width, int height); |
| 117 | void sgGammaInv(swgfx*, sgPixel*, int width, int height); | 118 | 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 { | |||
| 45 | R* depth; // Depth buffer. | 45 | R* depth; // Depth buffer. |
| 46 | sgTextureId* texture; // Texture ID buffer. | 46 | sgTextureId* texture; // Texture ID buffer. |
| 47 | sgVec2* texcoords; // Texture coords buffer. | 47 | sgVec2* texcoords; // Texture coords buffer. |
| 48 | sgPixel* albedo; // Albedo buffer. | ||
| 48 | sgViewport_t viewport; | 49 | sgViewport_t viewport; |
| 49 | sgMat4 model; // Model matrix. | 50 | sgMat4 model; // Model matrix. |
| 50 | sgMat4 view; // View matrix. | 51 | sgMat4 view; // View matrix. |
| @@ -89,6 +90,7 @@ static inline sgVec2 mod2(sgVec2 v, R m) { return (sgVec2){mod1(v.x, m), mod1(v | |||
| 89 | static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; } | 90 | static inline sgVec3 add3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x + b.x, a.y + b.y, a.z + b.z}; } |
| 90 | static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } | 91 | static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } |
| 91 | static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } | 92 | static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } |
| 93 | static inline sgVec3 mul3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x * b.x, a.y * b.y, a.z * b.z}; } | ||
| 92 | static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } | 94 | static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } |
| 93 | static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } | 95 | static inline sgVec3 scale3(sgVec3 v, R s) { return (sgVec3){v.x * s, v.y * s, v.z * s}; } |
| 94 | static inline sgVec3 exp3(sgVec3 v, R exp) { return (sgVec3){powf(v.x, exp), powf(v.y, exp), powf(v.z, exp)};} | 96 | 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) { | |||
| 739 | Align(N * sizeof(R)) + // Depth buffer. | 741 | Align(N * sizeof(R)) + // Depth buffer. |
| 740 | Align(N * sizeof(sgTextureId)) + // Texture ID buffer. | 742 | Align(N * sizeof(sgTextureId)) + // Texture ID buffer. |
| 741 | Align(N * sizeof(sgVec2)) + // Texture coords buffer. | 743 | Align(N * sizeof(sgVec2)) + // Texture coords buffer. |
| 744 | Align(N * sizeof(sgPixel)) + // Albedo buffer. | ||
| 742 | Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register. | 745 | Align(SWGFX_TEXTURE_REGISTER_SIZE * sizeof(sgTexture)) + // Texture register. |
| 743 | (SG_ALIGN - 1); // To make room to align allocations within the buffer. | 746 | (SG_ALIGN - 1); // To make room to align allocations within the buffer. |
| 744 | } | 747 | } |
| @@ -752,6 +755,7 @@ swgfx* sgNew(int width, int height, void* mem) { | |||
| 752 | gfx->depth = SG_ALLOC(&aligned, N, R); | 755 | gfx->depth = SG_ALLOC(&aligned, N, R); |
| 753 | gfx->texture = SG_ALLOC(&aligned, N, sgTextureId); | 756 | gfx->texture = SG_ALLOC(&aligned, N, sgTextureId); |
| 754 | gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2); | 757 | gfx->texcoords = SG_ALLOC(&aligned, N, sgVec2); |
| 758 | gfx->albedo = SG_ALLOC(&aligned, N, sgPixel); | ||
| 755 | gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture); | 759 | gfx->textureRegister = SG_ALLOC(&aligned, SWGFX_TEXTURE_REGISTER_SIZE, sgTexture); |
| 756 | gfx->activeTexture = DefaultTextureId; | 760 | gfx->activeTexture = DefaultTextureId; |
| 757 | gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; | 761 | gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; |
| @@ -973,10 +977,21 @@ void sgLighting(swgfx* gfx) { | |||
| 973 | const sgTextureId texid = gfx->texture[i]; | 977 | const sgTextureId texid = gfx->texture[i]; |
| 974 | const sgTexture* texture = &gfx->textureRegister[texid]; | 978 | const sgTexture* texture = &gfx->textureRegister[texid]; |
| 975 | const sgVec2 uv = gfx->texcoords[i]; | 979 | const sgVec2 uv = gfx->texcoords[i]; |
| 976 | sgPixel* colour = &gfx->colour[i]; | 980 | sgPixel* albedo = &gfx->albedo[i]; |
| 977 | // TODO: Actual lighting. | 981 | *albedo = Sample(texture->image, texture->filter, uv); |
| 978 | const sgPixel albedo = Sample(texture->image, texture->filter, uv); | 982 | } |
| 979 | *colour = albedo; | 983 | } |
| 984 | } | ||
| 985 | |||
| 986 | void sgAmbient(swgfx* gfx, sgVec3 ambient) { | ||
| 987 | assert(gfx); | ||
| 988 | const int N = gfx->dims.x * gfx->dims.y; | ||
| 989 | for (int i = 0; i < N; ++i) { | ||
| 990 | const R depth = gfx->depth[i]; | ||
| 991 | if (depth != DepthClearValue) { | ||
| 992 | const sgPixel* albedo = &gfx->albedo[i]; | ||
| 993 | sgPixel* colour = &gfx->colour[i]; | ||
| 994 | *colour = Vec3ToPixel(mul3(PixelToVec3(*albedo), ambient), (R)albedo->a/255.f); | ||
| 980 | } | 995 | } |
| 981 | } | 996 | } |
| 982 | } | 997 | } |
