BVH accelaration support for scene intersections (#704)
This commit is contained in:
@@ -928,6 +928,36 @@ namespace wi::scene
|
||||
device->SetName(&BLASes[lod], std::string("MeshComponent::BLAS[LOD" + std::to_string(lod) + "]").c_str());
|
||||
}
|
||||
}
|
||||
void MeshComponent::BuildBVH()
|
||||
{
|
||||
bvh_leaf_aabbs.clear();
|
||||
uint32_t first_subset = 0;
|
||||
uint32_t last_subset = 0;
|
||||
GetLODSubsetRange(0, first_subset, last_subset);
|
||||
for (uint32_t subsetIndex = first_subset; subsetIndex < last_subset; ++subsetIndex)
|
||||
{
|
||||
assert(subsetIndex <= 0xFF); // must fit into 8 bits userdata packing
|
||||
const MeshComponent::MeshSubset& subset = subsets[subsetIndex];
|
||||
if (subset.indexCount == 0)
|
||||
continue;
|
||||
const uint32_t indexOffset = subset.indexOffset;
|
||||
const uint32_t triangleCount = subset.indexCount / 3;
|
||||
for (uint32_t triangleIndex = 0; triangleIndex < triangleCount; ++triangleIndex)
|
||||
{
|
||||
assert(triangleIndex <= 0xFFFFFF); // must fit into 24 bits userdata packing
|
||||
const uint32_t i0 = indices[indexOffset + triangleIndex * 3 + 0];
|
||||
const uint32_t i1 = indices[indexOffset + triangleIndex * 3 + 1];
|
||||
const uint32_t i2 = indices[indexOffset + triangleIndex * 3 + 2];
|
||||
const XMFLOAT3& p0 = vertex_positions[i0];
|
||||
const XMFLOAT3& p1 = vertex_positions[i1];
|
||||
const XMFLOAT3& p2 = vertex_positions[i2];
|
||||
AABB aabb = wi::primitive::AABB(wi::math::Min(p0, wi::math::Min(p1, p2)), wi::math::Max(p0, wi::math::Max(p1, p2)));
|
||||
aabb.userdata = (triangleIndex & 0xFFFFFF) | ((subsetIndex & 0xFF) << 24u);
|
||||
bvh_leaf_aabbs.push_back(aabb);
|
||||
}
|
||||
}
|
||||
bvh.Build(bvh_leaf_aabbs.data(), (uint32_t)bvh_leaf_aabbs.size());
|
||||
}
|
||||
void MeshComponent::ComputeNormals(COMPUTE_NORMALS compute)
|
||||
{
|
||||
// Start recalculating normals:
|
||||
@@ -1300,6 +1330,44 @@ namespace wi::scene
|
||||
sphere.radius = aabb.getRadius();
|
||||
return sphere;
|
||||
}
|
||||
size_t MeshComponent::GetMemoryUsageCPU() const
|
||||
{
|
||||
size_t size =
|
||||
vertex_positions.size() * sizeof(XMFLOAT3) +
|
||||
vertex_normals.size() * sizeof(XMFLOAT3) +
|
||||
vertex_tangents.size() * sizeof(XMFLOAT4) +
|
||||
vertex_uvset_0.size() * sizeof(XMFLOAT2) +
|
||||
vertex_uvset_1.size() * sizeof(XMFLOAT2) +
|
||||
vertex_boneindices.size() * sizeof(XMUINT4) +
|
||||
vertex_boneweights.size() * sizeof(XMFLOAT4) +
|
||||
vertex_atlas.size() * sizeof(XMFLOAT2) +
|
||||
vertex_colors.size() * sizeof(uint32_t) +
|
||||
vertex_windweights.size() * sizeof(uint8_t) +
|
||||
indices.size() * sizeof(uint32_t);
|
||||
|
||||
for (const MorphTarget& morph : morph_targets)
|
||||
{
|
||||
size +=
|
||||
morph.vertex_positions.size() * sizeof(XMFLOAT3) +
|
||||
morph.vertex_normals.size() * sizeof(XMFLOAT3) +
|
||||
morph.sparse_indices_positions.size() * sizeof(uint32_t) +
|
||||
morph.sparse_indices_normals.size() * sizeof(uint32_t);
|
||||
}
|
||||
|
||||
size += GetMemoryUsageBVH();
|
||||
|
||||
return size;
|
||||
}
|
||||
size_t MeshComponent::GetMemoryUsageGPU() const
|
||||
{
|
||||
return generalBuffer.desc.size + streamoutBuffer.desc.size;
|
||||
}
|
||||
size_t MeshComponent::GetMemoryUsageBVH() const
|
||||
{
|
||||
return
|
||||
bvh.allocation.capacity() +
|
||||
bvh_leaf_aabbs.size() * sizeof(wi::primitive::AABB);
|
||||
}
|
||||
|
||||
void ObjectComponent::ClearLightmap()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user