Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2025-03-10 - [PPM Framebuffer IO Optimization]
**Learning:** `std::fwrite` has overhead for every call. Writing 3 bytes per pixel individually for an 800x600 image causes 480,000 library calls per frame. Buffering an entire row of pixels (or the whole image) and making one `fwrite` call per row (or per image) provides a massive performance boost (nearly 2x speedup for the offline demo application).
**Action:** Always batch I/O operations. When writing image files or any large binary data, buffer the data in memory and write in large chunks rather than making many small `fwrite` calls.

## 2025-03-17 - [Renderer Dynamic Vector Allocation Avoidance]
**Learning:** `std::vector` allocations in `Renderer::drawMesh` per draw call cause large memory overhead when called thousands of times per frame. Using class members instead and just calling `.resize()` helps reuse their capacity.
**Action:** Always allocate vectors as members in render loops and prefer `.resize()` instead of local instantiations when the vector sizes match vertex or index counts.
2 changes: 2 additions & 0 deletions include/soft_render/render/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Renderer {
pipeline::FragmentShader fs_;
pipeline::Uniforms uniforms_;
pipeline::SceneLighting lighting_;
std::vector<pipeline::ClipVertex> transformedBuffer_;
std::vector<pipeline::Triangle> triangleBuffer_;
};

}
Expand Down
14 changes: 7 additions & 7 deletions src/render/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ void Renderer::drawMesh(const pipeline::Vertex* verts, int vCount,
const uint32_t* indices, int iCount,
const pipeline::Material& mat) {
// Transform all vertices
std::vector<pipeline::ClipVertex> transformed(vCount);
vp_.processBatch(verts, transformed.data(), vCount, uniforms_);
transformedBuffer_.resize(vCount);
vp_.processBatch(verts, transformedBuffer_.data(), vCount, uniforms_);

// Build triangle list
int triCount = iCount / 3;
std::vector<pipeline::Triangle> tris(triCount);
triangleBuffer_.resize(triCount);
for (int i = 0; i < triCount; ++i) {
tris[i].v[0] = transformed[indices[i*3 + 0]];
tris[i].v[1] = transformed[indices[i*3 + 1]];
tris[i].v[2] = transformed[indices[i*3 + 2]];
triangleBuffer_[i].v[0] = transformedBuffer_[indices[i*3 + 0]];
triangleBuffer_[i].v[1] = transformedBuffer_[indices[i*3 + 1]];
triangleBuffer_[i].v[2] = transformedBuffer_[indices[i*3 + 2]];
}

// Fragment shader
Expand All @@ -76,7 +76,7 @@ void Renderer::drawMesh(const pipeline::Vertex* verts, int vCount,

// Rasterize
pipeline::Rasterizer rast(fb_);
rast.rasterizeBatch(tris.data(), triCount, fragCb);
rast.rasterizeBatch(triangleBuffer_.data(), triCount, fragCb);
}

void Renderer::draw(const DrawCall& call) {
Expand Down
Loading