Upgrade CollisionCooking to use new MeshAccessor

This commit is contained in:
Wojtek Figat
2025-02-24 23:46:44 +01:00
parent 754d321ef0
commit 21047ac8ac
3 changed files with 107 additions and 19 deletions

View File

@@ -89,6 +89,16 @@ public:
// Check input data and stream type with IsLinear before calling.
void SetLinear(const void* data);
// Copies the contents of the input data span into the elements of this stream.
void Set(Span<Float2> src);
void Set(Span<Float3> src);
void Set(Span<Color> src);
// Copies the contents of this stream into a destination data span.
void CopyTo(Span<Float2> dst) const;
void CopyTo(Span<Float3> dst) const;
void CopyTo(Span<Color> dst) const;
};
private:

View File

@@ -89,6 +89,96 @@ void MeshAccessor::Stream::SetLinear(const void* data)
Platform::MemoryCopy(_data.Get(), data, _data.Length());
}
void MeshAccessor::Stream::Set(Span<Float2> src)
{
const int32 count = GetCount();
ASSERT(src.Length() >= count);
if (IsLinear(PixelFormat::R32G32_Float))
{
Platform::MemoryCopy(_data.Get(), src.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
_sampler.Write(_data.Get() + i * _stride, Float4(src.Get()[i], 0, 0));
}
}
void MeshAccessor::Stream::Set(Span<Float3> src)
{
const int32 count = GetCount();
ASSERT(src.Length() >= count);
if (IsLinear(PixelFormat::R32G32B32_Float))
{
Platform::MemoryCopy(_data.Get(), src.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
_sampler.Write(_data.Get() + i * _stride, Float4(src.Get()[i], 0));
}
}
inline void MeshAccessor::Stream::Set(Span<::Color> src)
{
const int32 count = GetCount();
ASSERT(src.Length() >= count);
if (IsLinear(PixelFormat::R32G32B32A32_Float))
{
Platform::MemoryCopy(_data.Get(), src.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
_sampler.Write(_data.Get() + i * _stride, Float4(src.Get()[i]));
}
}
void MeshAccessor::Stream::CopyTo(Span<Float2> dst) const
{
const int32 count = GetCount();
ASSERT(dst.Length() >= count);
if (IsLinear(PixelFormat::R32G32_Float))
{
Platform::MemoryCopy(dst.Get(), _data.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
dst.Get()[i] = Float2(_sampler.Read(_data.Get() + i * _stride));
}
}
void MeshAccessor::Stream::CopyTo(Span<Float3> dst) const
{
const int32 count = GetCount();
ASSERT(dst.Length() >= count);
if (IsLinear(PixelFormat::R32G32B32_Float))
{
Platform::MemoryCopy(dst.Get(), _data.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
dst.Get()[i] = Float3(_sampler.Read(_data.Get() + i * _stride));
}
}
void MeshAccessor::Stream::CopyTo(Span<::Color> dst) const
{
const int32 count = GetCount();
ASSERT(dst.Length() >= count);
if (IsLinear(PixelFormat::R32G32B32A32_Float))
{
Platform::MemoryCopy(dst.Get(), _data.Get(), _data.Length());
}
else
{
for (int32 i = 0; i < count; i++)
dst.Get()[i] = ::Color(_sampler.Read(_data.Get() + i * _stride));
}
}
bool MeshAccessor::LoadMesh(const MeshBase* mesh, bool forceGpu, Span<MeshBufferType> buffers)
{
CHECK_RETURN(mesh, true);

View File

@@ -4,8 +4,10 @@
#include "CollisionCooking.h"
#include "Engine/Threading/Task.h"
#include "Engine/Graphics/GPUBuffer.h"
#include "Engine/Graphics/Async/GPUTask.h"
#include "Engine/Graphics/Models/MeshBase.h"
#include "Engine/Graphics/Models/MeshAccessor.h"
#include "Engine/Threading/Threading.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Core/Log.h"
@@ -216,21 +218,11 @@ bool CollisionCooking::CookCollision(const Argument& arg, CollisionData::Seriali
const int32 vertexCount = vertexCounts[i];
if (vertexCount == 0)
continue;
const int32 vStride = vData.Length() / vertexCount;
if (vStride == sizeof(Float3))
Platform::MemoryCopy(finalVertexData.Get() + firstVertexIndex, vData.Get(), vertexCount * sizeof(Float3));
else
{
// This assumes that each vertex structure contains position as Float3 in the beginning
auto dst = finalVertexData.Get() + firstVertexIndex;
auto src = vData.Get();
for (int32 j = 0; j < vertexCount; j++)
{
*dst++ = *(Float3*)src;
src += vStride;
}
}
MeshAccessor accessor;
if (accessor.LoadBuffer(MeshBufferType::Vertex0, Span<byte>(vData), mesh.GetVertexBuffer(0)->GetVertexLayout()))
continue;
auto positionStream = accessor.Position();
positionStream.CopyTo(Span<Float3>(finalVertexData.Get() + firstVertexIndex, vertexCount));
vertexCounter += vertexCount;
if (needIndexBuffer)
@@ -242,9 +234,7 @@ bool CollisionCooking::CookCollision(const Argument& arg, CollisionData::Seriali
auto dst = finalIndexData.Get() + indexCounter;
auto src = iData.Get<uint16>();
for (int32 j = 0; j < indexCount; j++)
{
*dst++ = firstVertexIndex + *src++;
}
indexCounter += indexCount;
}
else
@@ -252,9 +242,7 @@ bool CollisionCooking::CookCollision(const Argument& arg, CollisionData::Seriali
auto dst = finalIndexData.Get() + indexCounter;
auto src = iData.Get<uint32>();
for (int32 j = 0; j < indexCount; j++)
{
*dst++ = firstVertexIndex + *src++;
}
indexCounter += indexCount;
}
}