Fix MeshAccessor.Stream count for items not aligned to buffer start

This commit is contained in:
2026-03-26 17:14:14 +01:00
parent b6789ee523
commit 09a0573932
3 changed files with 21 additions and 15 deletions
+10 -7
View File
@@ -18,13 +18,14 @@ namespace FlaxEngine
{
private Span<byte> _data;
private PixelFormat _format;
private int _stride;
private int _stride, _count;
private readonly PixelFormatSampler _sampler;
internal Stream(Span<byte> data, PixelFormat format, int stride)
internal Stream(Span<byte> data, PixelFormat format, int stride, int count)
{
_data = data;
_stride = stride;
_count = count;
if (PixelFormatSampler.Get(format, out _sampler))
{
_format = format;
@@ -53,7 +54,7 @@ namespace FlaxEngine
/// <summary>
/// Gets the count of the items in the stride.
/// </summary>
public int Count => _data.Length / _stride;
public int Count => _count;
/// <summary>
/// Returns true if stream is valid.
@@ -664,15 +665,16 @@ namespace FlaxEngine
{
Span<byte> data = new Span<byte>();
PixelFormat format = PixelFormat.Unknown;
int stride = 0;
int stride = 0, count = 0;
var ib = _data[(int)MeshBufferType.Index];
if (ib != null)
{
data = ib;
format = _formats[(int)MeshBufferType.Index];
stride = PixelFormatExtensions.SizeInBytes(format);
count = data.Length / stride;
}
return new Stream(data, format, stride);
return new Stream(data, format, stride, count);
}
/// <summary>
@@ -684,7 +686,7 @@ namespace FlaxEngine
{
Span<byte> data = new Span<byte>();
PixelFormat format = PixelFormat.Unknown;
int stride = 0;
int stride = 0, count = 0;
for (int vbIndex = 0; vbIndex < 3 && format == PixelFormat.Unknown; vbIndex++)
{
int idx = vbIndex + 1;
@@ -699,11 +701,12 @@ namespace FlaxEngine
data = new Span<byte>(vb).Slice(e.Offset);
format = e.Format;
stride = (int)layout.Stride;
count = vb.Length / stride;
break;
}
}
}
return new Stream(data, format, stride);
return new Stream(data, format, stride, count);
}
/// <summary>
+2 -2
View File
@@ -25,10 +25,10 @@ public:
private:
Span<byte> _data;
PixelFormat _format;
int32 _stride;
int32 _stride, _count;
PixelFormatSampler _sampler;
Stream(Span<byte> data, PixelFormat format, int32 stride);
Stream(Span<byte> data, PixelFormat format, int32 stride, int32 count);
public:
Span<byte> GetData() const;
+9 -6
View File
@@ -38,10 +38,11 @@ namespace
#endif
}
MeshAccessor::Stream::Stream(Span<byte> data, PixelFormat format, int32 stride)
MeshAccessor::Stream::Stream(Span<byte> data, PixelFormat format, int32 stride, int32 count)
: _data(data)
, _format(PixelFormat::Unknown)
, _stride(stride)
, _count(count)
{
auto sampler = PixelFormatSampler::Get(format);
if (sampler)
@@ -72,7 +73,7 @@ int32 MeshAccessor::Stream::GetStride() const
int32 MeshAccessor::Stream::GetCount() const
{
return _data.Length() / _stride;
return _count;
}
bool MeshAccessor::Stream::IsValid() const
@@ -368,22 +369,23 @@ MeshAccessor::Stream MeshAccessor::Index()
{
Span<byte> data;
PixelFormat format = PixelFormat::Unknown;
int32 stride = 0;
int32 stride = 0, count = 0;
auto& ib = _data[(int32)MeshBufferType::Index];
if (ib.IsValid())
{
data = ib;
format = _formats[(int32)MeshBufferType::Index];
stride = PixelFormatExtensions::SizeInBytes(format);
count = data.Length() / stride;
}
return Stream(data, format, stride);
return Stream(data, format, stride, count);
}
MeshAccessor::Stream MeshAccessor::Attribute(VertexElement::Types attribute)
{
Span<byte> data;
PixelFormat format = PixelFormat::Unknown;
int32 stride = 0;
int32 stride = 0, count = 0;
for (int32 vbIndex = 0; vbIndex < 3 && format == PixelFormat::Unknown; vbIndex++)
{
static_assert((int32)MeshBufferType::Vertex0 == 1, "Update code.");
@@ -399,11 +401,12 @@ MeshAccessor::Stream MeshAccessor::Attribute(VertexElement::Types attribute)
data = vb.Slice(e.Offset);
format = e.Format;
stride = layout->GetStride();
count = vb.Length() / stride;
break;
}
}
}
return Stream(data, format, stride);
return Stream(data, format, stride, count);
}
MeshBase::~MeshBase()