Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occluder: added padding to fix a crash with low-poly occluders (#8) #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
27 changes: 17 additions & 10 deletions SoftwareRasterizer/Occluder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ std::unique_ptr<Occluder> Occluder::bake(const std::vector<__m128>& vertices, __

// Simple k-means clustering by normal direction to improve backface culling efficiency
std::vector<__m128> quadNormals;
quadNormals.reserve(vertices.size());
for (auto i = 0; i < vertices.size(); i += 4)
{
auto v0 = vertices[i + 0];
Expand All @@ -20,16 +21,15 @@ std::unique_ptr<Occluder> Occluder::bake(const std::vector<__m128>& vertices, __
quadNormals.push_back(normalize(_mm_add_ps(normal(v0, v1, v2), normal(v0, v2, v3))));
}

std::vector<__m128> centroids;
std::vector<uint32_t> centroidAssignment;
centroids.push_back(_mm_setr_ps(+1.0f, 0.0f, 0.0f, 0.0f));
centroids.push_back(_mm_setr_ps(0.0f, +1.0f, 0.0f, 0.0f));
centroids.push_back(_mm_setr_ps(0.0f, 0.0f, +1.0f, 0.0f));
centroids.push_back(_mm_setr_ps(0.0f, -1.0f, 0.0f, 0.0f));
centroids.push_back(_mm_setr_ps(0.0f, 0.0f, -1.0f, 0.0f));
centroids.push_back(_mm_setr_ps(-1.0f, 0.0f, 0.0f, 0.0f));

centroidAssignment.resize(vertices.size() / 4);
std::vector<__m128> centroids = {
_mm_setr_ps(+1.0f, 0.0f, 0.0f, 0.0f),
_mm_setr_ps(0.0f, +1.0f, 0.0f, 0.0f),
_mm_setr_ps(0.0f, 0.0f, +1.0f, 0.0f),
_mm_setr_ps(0.0f, -1.0f, 0.0f, 0.0f),
_mm_setr_ps(0.0f, 0.0f, -1.0f, 0.0f),
_mm_setr_ps(-1.0f, 0.0f, 0.0f, 0.0f),
};
std::vector<uint32_t> centroidAssignment(vertices.size() / 4);

bool anyChanged = true;
for (int iter = 0; iter < 10 && anyChanged; ++iter)
Expand Down Expand Up @@ -78,6 +78,7 @@ std::unique_ptr<Occluder> Occluder::bake(const std::vector<__m128>& vertices, __
}

std::vector<__m128> orderedVertices;
orderedVertices.reserve(centroids.size() * vertices.size()); // worst case
for (uint32_t k = 0; k < centroids.size(); ++k)
{
for (int j = 0; j < vertices.size() / 4; ++j)
Expand All @@ -91,6 +92,10 @@ std::unique_ptr<Occluder> Occluder::bake(const std::vector<__m128>& vertices, __
}
}
}
const uint32_t unpaddedSize = orderedVertices.size();
// pad to 32 elements so that the transformation loop does not break
while (orderedVertices.size() % 32 != 0)
orderedVertices.push_back(orderedVertices.back());

auto occluder = std::make_unique<Occluder>();

Expand Down Expand Up @@ -155,6 +160,8 @@ std::unique_ptr<Occluder> Occluder::bake(const std::vector<__m128>& vertices, __
occluder->m_vertexData[occluder->m_packetCount++] = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(v + 4));
occluder->m_vertexData[occluder->m_packetCount++] = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(v + 6));
}
// truncate to the original unpadded size
orderedVertices.resize(unpaddedSize);

occluder->m_refMin = refMin;
occluder->m_refMax = refMax;
Expand Down
5 changes: 3 additions & 2 deletions SoftwareRasterizer/QuadDecomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ namespace

std::vector<uint32_t> QuadDecomposition::decompose(const std::vector<uint32_t>& indices, const std::vector<__m128>& vertices)
{
std::vector<uint32_t> result;

size_t triangleCount = indices.size() / 3;

std::vector<uint32_t> result;
result.reserve(triangleCount * 4); // worst case

Graph candidateGraph;
candidateGraph.m_adjacencyList.resize(triangleCount);

Expand Down