Add Slice to Span and sue it to make code cleaner

This commit is contained in:
Wojtek Figat
2025-01-05 17:41:27 +01:00
parent 78cf1a4948
commit 933fac6c13
12 changed files with 37 additions and 13 deletions

View File

@@ -85,6 +85,30 @@ public:
return (U*)_data;
}
public:
/// <summary>
/// Constructs a slice out of the current span that begins at a specified index.
/// </summary>
/// <param name="start">The zero-based index at which to begin the slice.</param>
/// <returns>A span that consists of all elements of the current span from <paramref name="start" /> to the end of the span.</returns>
Span<T> Slice(int32 start)
{
ASSERT(start <= _length);
return Span(_data + start, _length - start);
}
/// <summary>
/// Constructs a slice out of the current span starting at a specified index for a specified length.
/// </summary>
/// <param name="start">The zero-based index at which to begin this slice.</param>
/// <param name="length">The length for the result slice.</param>
/// <returns>A span that consists of <paramref name="length" /> elements from the current span starting at <paramref name="start" />.</returns>
Span<T> Slice(int32 start, int32 length)
{
ASSERT(start + length <= _length);
return Span(_data + start, length);
}
public:
/// <summary>
/// Gets or sets the element by index

View File

@@ -33,7 +33,7 @@ void DecalMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(DecalMaterialShaderData));
auto materialData = reinterpret_cast<DecalMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(DecalMaterialShaderData), cb.Length() - sizeof(DecalMaterialShaderData));
cb = cb.Slice(sizeof(DecalMaterialShaderData));
const bool isCameraInside = OrientedBoundingBox(Vector3::Half, drawCall.World).Contains(view.Position) == ContainmentType::Contains;
// Setup parameters

View File

@@ -41,7 +41,7 @@ void DeformableMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(DeformableMaterialShaderData));
auto materialData = reinterpret_cast<DeformableMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(DeformableMaterialShaderData), cb.Length() - sizeof(DeformableMaterialShaderData));
cb = cb.Slice(sizeof(DeformableMaterialShaderData));
int32 srv = 1;
// Setup features

View File

@@ -32,7 +32,7 @@ void GUIMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(GUIMaterialShaderData));
auto materialData = reinterpret_cast<GUIMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(GUIMaterialShaderData), cb.Length() - sizeof(GUIMaterialShaderData));
cb = cb.Slice(sizeof(GUIMaterialShaderData));
int32 srv = 0;
const auto ps = context->IsDepthBufferBinded() ? _cache.Depth : _cache.NoDepth;
auto customData = (Render2D::CustomData*)params.CustomData;

View File

@@ -113,7 +113,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
}
}
cb = Span<byte>(cb.Get() + sizeof(Data), cb.Length() - sizeof(Data));
cb = cb.Slice(sizeof(Data));
srv += SRVs;
}
@@ -184,7 +184,7 @@ bool GlobalIlluminationFeature::Bind(MaterialShader::BindParameters& params, Spa
params.GPUContext->UnBindSR(srv + 2);
}
cb = Span<byte>(cb.Get() + sizeof(Data), cb.Length() - sizeof(Data));
cb = cb.Slice(sizeof(Data));
srv += SRVs;
return useGI;
}
@@ -236,7 +236,7 @@ bool SDFReflectionsFeature::Bind(MaterialShader::BindParameters& params, Span<by
params.GPUContext->UnBindSR(srv + 6);
}
cb = Span<byte>(cb.Get() + sizeof(Data), cb.Length() - sizeof(Data));
cb = cb.Slice(sizeof(Data));
srv += SRVs;
return useSDFReflections;
}

View File

@@ -53,7 +53,7 @@ void ParticleMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(ParticleMaterialShaderData));
auto materialData = reinterpret_cast<ParticleMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(ParticleMaterialShaderData), cb.Length() - sizeof(ParticleMaterialShaderData));
cb = cb.Slice(sizeof(ParticleMaterialShaderData));
int32 srv = 2;
// Setup features

View File

@@ -31,7 +31,7 @@ void PostFxMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(PostFxMaterialShaderData));
auto materialData = reinterpret_cast<PostFxMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(PostFxMaterialShaderData), cb.Length() - sizeof(PostFxMaterialShaderData));
cb = cb.Slice(sizeof(PostFxMaterialShaderData));
int32 srv = 0;
// Setup parameters

View File

@@ -50,7 +50,7 @@ void TerrainMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(TerrainMaterialShaderData));
auto materialData = reinterpret_cast<TerrainMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(TerrainMaterialShaderData), cb.Length() - sizeof(TerrainMaterialShaderData));
cb = cb.Slice(sizeof(TerrainMaterialShaderData));
int32 srv = 3;
// Setup features

View File

@@ -41,7 +41,7 @@ void VolumeParticleMaterialShader::Bind(BindParameters& params)
Span<byte> cb(_cbData.Get(), _cbData.Count());
ASSERT_LOW_LAYER(cb.Length() >= sizeof(VolumeParticleMaterialShaderData));
auto materialData = reinterpret_cast<VolumeParticleMaterialShaderData*>(cb.Get());
cb = Span<byte>(cb.Get() + sizeof(VolumeParticleMaterialShaderData), cb.Length() - sizeof(VolumeParticleMaterialShaderData));
cb = cb.Slice(sizeof(VolumeParticleMaterialShaderData));
int32 srv = 1;
auto customData = (VolumetricFogPass::CustomData*)params.CustomData;

View File

@@ -12,7 +12,7 @@ GPUShaderProgram* GPUShaderDX12::CreateGPUShaderProgram(ShaderStage type, const
{
// Extract the DX shader header from the cache
DxShaderHeader* header = (DxShaderHeader*)bytecode.Get();
bytecode = Span<byte>(bytecode.Get() + sizeof(DxShaderHeader), bytecode.Length() - sizeof(DxShaderHeader));
bytecode = bytecode.Slice(sizeof(DxShaderHeader));
GPUShaderProgram* shader = nullptr;
switch (type)

View File

@@ -102,7 +102,7 @@ GPUShaderProgram* GPUShaderVulkan::CreateGPUShaderProgram(ShaderStage type, cons
{
// Extract the SPIR-V shader header from the cache
SpirvShaderHeader* header = (SpirvShaderHeader*)bytecode.Get();
bytecode = Span<byte>(bytecode.Get() + sizeof(SpirvShaderHeader), bytecode.Length() - sizeof(SpirvShaderHeader));
bytecode = bytecode.Slice(sizeof(SpirvShaderHeader));
// Extract the SPIR-V bytecode
BytesContainer spirv;

View File

@@ -166,7 +166,7 @@ void GPUParticles::Execute(GPUContext* context, ParticleEmitter* emitter, Partic
// Setup parameters
MaterialParameter::BindMeta bindMeta;
bindMeta.Context = context;
bindMeta.Constants = hasCB ? Span<byte>(_cbData.Get() + sizeof(GPUParticlesData), _cbData.Count() - sizeof(GPUParticlesData)) : Span<byte>(nullptr, 0);
bindMeta.Constants = hasCB ? ToSpan(_cbData).Slice(sizeof(GPUParticlesData)) : Span<byte>(nullptr, 0);
bindMeta.Input = nullptr;
if (viewTask)
{