mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 13:08:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
4 changed files with 784842 additions and 0 deletions
25
Brutal.Texture.Abstractions/Upload/StageUploadRegions.cs
Normal file
25
Brutal.Texture.Abstractions/Upload/StageUploadRegions.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
namespace Brutal.TextureApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Holds a collection of regions for copying texture data from a texture to a staging buffer,
|
||||
/// and from the staging buffer to an image.
|
||||
/// </summary>
|
||||
public class StageUploadRegions
|
||||
{
|
||||
/// <summary>
|
||||
/// The regions in the source texture's memory to copy
|
||||
/// </summary>
|
||||
public List<StagingRegion> StageBufferRegions = [];
|
||||
|
||||
/// <summary>
|
||||
/// The regions in a staging buffer copied using the StageBufferRegions
|
||||
/// that can be copied to the images on a GPU using a GPU API.
|
||||
/// </summary>
|
||||
public List<UploadRegion> CopyToImageRegions = [];
|
||||
|
||||
/// <summary>
|
||||
/// The minimum number of bytes that your staging buffer needs to be
|
||||
/// to accept the copied StageBufferRegions
|
||||
/// </summary>
|
||||
public ByteSize RequiredBufferSizeBytes;
|
||||
}
|
||||
44
Brutal.Texture.Abstractions/Upload/StagingRegion.cs
Normal file
44
Brutal.Texture.Abstractions/Upload/StagingRegion.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
namespace Brutal.TextureApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a region of texture data to be copied to a staging buffer.
|
||||
/// </summary>
|
||||
public class StagingRegion
|
||||
{
|
||||
/// <summary>
|
||||
/// Offset from the start of the texture data to start copying from.
|
||||
/// </summary>
|
||||
public ByteSize SourceStartOffset;
|
||||
|
||||
/// <summary>
|
||||
/// Offset within the destination staging buffer to start copying data to.
|
||||
/// </summary>
|
||||
public ByteSize DestinationOffset;
|
||||
|
||||
/// <summary>
|
||||
/// How many bytes of data to copy.
|
||||
/// </summary>
|
||||
public ByteSize SizeInBytes;
|
||||
|
||||
/// <summary>
|
||||
/// How many blocks wide is a single row of image data.
|
||||
/// </summary>
|
||||
public int RowWidthInBlocks;
|
||||
|
||||
/// <summary>
|
||||
/// How many bytes does a block take up. This is directly related
|
||||
/// to the <see cref="TextureFormat" /> of the texture data that's being staged.
|
||||
/// </summary>
|
||||
public ByteSize BlockSizeBytes;
|
||||
|
||||
/// <summary>
|
||||
/// How many bytes from the start of one one row of the texture to the start
|
||||
/// of the next row.
|
||||
///
|
||||
/// This can be > BlockSizeBytes * RowWidthInBlocks. Specifically this is the case
|
||||
/// for some KTX version 1 textures because they are padded to support OpenGL texture uploading.
|
||||
/// Almost all other formats will have RowPitch == RowwidthInBlocks * BlockSizeBytes but
|
||||
/// do not just assume that is the case and use this field for memory calculations on texture data.
|
||||
/// </summary>
|
||||
public required ByteSize RowPitch;
|
||||
}
|
||||
28
Brutal.Texture.Abstractions/Upload/UploadRegion.cs
Normal file
28
Brutal.Texture.Abstractions/Upload/UploadRegion.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
namespace Brutal.TextureApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// A region of data for uploading to a range of layers within a single level of an image.
|
||||
/// </summary>
|
||||
public class UploadRegion
|
||||
{
|
||||
/// <summary>
|
||||
/// Offset in the staging buffer where this region starts.
|
||||
/// </summary>
|
||||
public ByteSize BufferOffset;
|
||||
|
||||
/// <summary>
|
||||
/// The array (or faces if this is a non-array cube map) layer index this region starts at.
|
||||
/// </summary>
|
||||
public int BaseLayer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of array layers (or faces if this is a non-array cube map)
|
||||
/// this region encompasses.
|
||||
/// </summary>
|
||||
public int LayerCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The mip level that this UploadRegion starts at.
|
||||
/// </summary>
|
||||
public int BaseLevel { get; set; }
|
||||
}
|
||||
266
Brutal.Texture.Abstractions/Upload/UploadRegions.cs
Normal file
266
Brutal.Texture.Abstractions/Upload/UploadRegions.cs
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
using System.Diagnostics;
|
||||
using Brutal.Pointers.Extensions;
|
||||
using Brutal.TextureApi.Abstractions;
|
||||
|
||||
namespace Brutal.TextureApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Builds a set of information to describe how to stage an ITexture's data to a staging buffer
|
||||
/// and upload it to a GPU.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <list type="bullet">Removes row padding from KTX v1 textures.<br/></list>
|
||||
/// <list type="bullet">Aligning data to lcm(4, texel block size)<br/></list>
|
||||
/// <list type="bullet">Reducing number of copy commands where possible.<br/></list>
|
||||
/// <list type="bullet">Currently does NOT squash all mips into one copy for staging even if it would be valid.<br/></list>
|
||||
/// </remarks>
|
||||
public class UploadRegions
|
||||
{
|
||||
public enum Mode
|
||||
{
|
||||
BatchUploadRegions, // Try to put as many images into as few copy regions as possible
|
||||
UploadRegionPerImage, // Each image gets its own copy region
|
||||
}
|
||||
|
||||
private UploadRegions()
|
||||
{
|
||||
_currentStageRegion = null;
|
||||
_currentUploadRegion = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds optimal staging and upload regions for entire mip range
|
||||
/// of a texture.
|
||||
/// </summary>
|
||||
/// <param name="texture"></param>
|
||||
/// <returns></returns>
|
||||
public static StageUploadRegions Build(ITexture texture)
|
||||
{
|
||||
var textureUpload = new UploadRegions();
|
||||
return textureUpload.BuildInternal(texture, 0, texture.LevelCount, Mode.BatchUploadRegions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds optimal staging and upload regions for a selected mip range
|
||||
/// of a texture.
|
||||
/// </summary>
|
||||
/// <param name="texture">Texture to build regions for.</param>
|
||||
/// <param name="firstLevel">First level to build regions for. This level will become level 0 in the output.</param>
|
||||
/// <param name="maximumLevelCount">Maximum # of levels to build. Must be > 0.</param>
|
||||
/// <param name="mode">How to build the regions.</param>
|
||||
/// <returns>The <see cref="StageUploadRegions" /> that were built.</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if firstLevel >= the level count of texture or maximumLevelCount <= 0.</exception>
|
||||
public static StageUploadRegions Build(
|
||||
ITexture texture,
|
||||
int firstLevel,
|
||||
int maximumLevelCount,
|
||||
Mode mode = Mode.BatchUploadRegions
|
||||
)
|
||||
{
|
||||
if (firstLevel >= texture.LevelCount)
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(firstLevel),
|
||||
$"Level out of range. Expected 0-{texture.LevelCount - 1}, received {firstLevel}."
|
||||
);
|
||||
|
||||
if (maximumLevelCount <= 0)
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(maximumLevelCount),
|
||||
"Maximum level count must be greater than 0."
|
||||
);
|
||||
|
||||
var textureUpload = new UploadRegions();
|
||||
return textureUpload.BuildInternal(texture, firstLevel, maximumLevelCount, mode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a datastructure describing how to upload an ITexture's images to a buffer and then to
|
||||
/// a GPU.
|
||||
/// Goes through the texture by level, layer, face and slice order, building the minimal
|
||||
/// set of copies required to upload the texture, removing any row padding and also
|
||||
/// aligning as needed.
|
||||
/// </summary>
|
||||
private unsafe StageUploadRegions BuildInternal(ITexture texture, int firstLevel, int levelCount, Mode mode)
|
||||
{
|
||||
var formatDescriptor = texture.Format.Descriptor();
|
||||
|
||||
levelCount = Math.Min(levelCount, texture.LevelCount - firstLevel);
|
||||
|
||||
_uploadRegions = new List<UploadRegion>(texture.LevelCount);
|
||||
_copyRegions = new List<StagingRegion>(texture.LevelCount * texture.LayerCount * texture.FaceCount);
|
||||
_lcm = CalculateLcm4AndTexelBlockSize(formatDescriptor.BlockSizeInBytes);
|
||||
_stageBufferDestinationOffset = ByteSize.Zero;
|
||||
|
||||
var layouts = new TextureMemoryLayout(texture);
|
||||
var blockSizeBytes = formatDescriptor.BlockSizeInBytes;
|
||||
|
||||
for (var level = firstLevel; level < firstLevel + levelCount; ++level)
|
||||
{
|
||||
// Output levels are re-based so the requested firstLevel becomes level 0. Batch-mode's
|
||||
// "start a new region when the level changes" check must compare against this re-based
|
||||
// level, otherwise any firstLevel > 0 never matches BaseLevel and every layer is split
|
||||
// into its own region.
|
||||
var outputLevel = level - firstLevel;
|
||||
var layout = layouts.ImageLayout(level);
|
||||
var rowWidthInBlocks = layout.Extent.Blocks.X;
|
||||
var rowPitch = layout.RowPitch;
|
||||
var depthSlices = texture.Is3d ? layout.Extent.Texels.Z : 1;
|
||||
|
||||
for (var layer = 0; layer < texture.LayerCount; ++layer)
|
||||
{
|
||||
for (var face = 0; face < texture.FaceCount; ++face)
|
||||
{
|
||||
var sourceImageData = texture.ImageData(level, layer, face);
|
||||
var sourceImageOffset = ByteSize.Of(sourceImageData.AsPointer() - texture.Data.AsPointer());
|
||||
|
||||
if (
|
||||
_currentUploadRegion == null
|
||||
|| mode == Mode.UploadRegionPerImage
|
||||
|| (mode == Mode.BatchUploadRegions && outputLevel != _currentUploadRegion.BaseLevel)
|
||||
)
|
||||
NewUploadRegion(outputLevel, layer * texture.FaceCount + face);
|
||||
|
||||
for (var depthSlice = 0; depthSlice < depthSlices; ++depthSlice)
|
||||
{
|
||||
AddImageData(
|
||||
sourceImageOffset,
|
||||
ByteSize.Of(sourceImageData),
|
||||
rowWidthInBlocks,
|
||||
blockSizeBytes,
|
||||
rowPitch
|
||||
);
|
||||
sourceImageOffset += ByteSize.Of(sourceImageData);
|
||||
}
|
||||
++_currentUploadRegion!.LayerCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var minimumRequiredBytes =
|
||||
_copyRegions.Count == 0
|
||||
? ByteSize.Of(0)
|
||||
: _copyRegions[^1].DestinationOffset + _copyRegions[^1].SizeInBytes;
|
||||
|
||||
return new StageUploadRegions()
|
||||
{
|
||||
StageBufferRegions = _copyRegions,
|
||||
CopyToImageRegions = _uploadRegions,
|
||||
RequiredBufferSizeBytes = minimumRequiredBytes,
|
||||
};
|
||||
}
|
||||
|
||||
private void AddImageData(
|
||||
ByteSize sourceImageOffset,
|
||||
ByteSize length,
|
||||
int rowWidthInBlocks,
|
||||
ByteSize blockSizeBytes,
|
||||
ByteSize rowPitch
|
||||
)
|
||||
{
|
||||
UpdateStageRegion(sourceImageOffset, rowWidthInBlocks, blockSizeBytes, rowPitch);
|
||||
ExtendStageRegion(length);
|
||||
}
|
||||
|
||||
private bool ContinuesCurrentStageRegionSource(ByteSize sourceImageOffset)
|
||||
{
|
||||
return sourceImageOffset == _currentStageRegion!.SourceStartOffset + _currentStageRegion.SizeInBytes;
|
||||
}
|
||||
|
||||
private bool ContinuesCurrentStageRegionDestination(ByteSize destinationOffset)
|
||||
{
|
||||
return destinationOffset == _currentStageRegion!.DestinationOffset + _currentStageRegion.SizeInBytes;
|
||||
}
|
||||
|
||||
private void ExtendStageRegion(ByteSize sizeInBytes)
|
||||
{
|
||||
Debug.Assert(_currentStageRegion != null);
|
||||
|
||||
_currentStageRegion.SizeInBytes += sizeInBytes;
|
||||
_stageBufferDestinationOffset += sizeInBytes;
|
||||
}
|
||||
|
||||
private static uint CalculateLcm4AndTexelBlockSize(ByteSize texelBlocksize)
|
||||
{
|
||||
if (texelBlocksize == 0)
|
||||
return 0;
|
||||
|
||||
var num1 = ByteSize.Of(4u);
|
||||
var num2 = texelBlocksize;
|
||||
while (num1 != num2)
|
||||
{
|
||||
if (num1 > num2)
|
||||
{
|
||||
num1 -= num2;
|
||||
}
|
||||
else
|
||||
{
|
||||
num2 -= num1;
|
||||
}
|
||||
}
|
||||
|
||||
return (uint)(4 * texelBlocksize / num1);
|
||||
}
|
||||
|
||||
private void NewUploadRegion(int baseLevel, int baseLayer)
|
||||
{
|
||||
Debug.Assert(!_uploadRegions.Any(r => r.BaseLayer == baseLayer && r.BaseLevel == baseLevel));
|
||||
|
||||
_stageBufferDestinationOffset = AlignOffsetToNextLcm(_stageBufferDestinationOffset);
|
||||
|
||||
_currentUploadRegion = new UploadRegion()
|
||||
{
|
||||
BaseLevel = baseLevel,
|
||||
BaseLayer = baseLayer,
|
||||
LayerCount = 0,
|
||||
BufferOffset = _stageBufferDestinationOffset,
|
||||
};
|
||||
_uploadRegions.Add(_currentUploadRegion);
|
||||
}
|
||||
|
||||
private void UpdateStageRegion(
|
||||
ByteSize startOffset,
|
||||
int rowWidthInBlocks,
|
||||
ByteSize blockSizeBytes,
|
||||
ByteSize rowPitch
|
||||
)
|
||||
{
|
||||
Debug.Assert(
|
||||
_stageBufferDestinationOffset == 0
|
||||
|| _currentStageRegion == null
|
||||
|| _stageBufferDestinationOffset
|
||||
>= _currentStageRegion.DestinationOffset + _currentStageRegion.SizeInBytes
|
||||
);
|
||||
|
||||
if (_currentStageRegion != null)
|
||||
{
|
||||
if (
|
||||
ContinuesCurrentStageRegionSource(startOffset)
|
||||
&& ContinuesCurrentStageRegionDestination(_stageBufferDestinationOffset)
|
||||
)
|
||||
return;
|
||||
}
|
||||
|
||||
_currentStageRegion = new StagingRegion
|
||||
{
|
||||
DestinationOffset = _stageBufferDestinationOffset,
|
||||
SourceStartOffset = startOffset,
|
||||
SizeInBytes = ByteSize.Zero,
|
||||
RowWidthInBlocks = rowWidthInBlocks,
|
||||
BlockSizeBytes = blockSizeBytes,
|
||||
RowPitch = rowPitch,
|
||||
};
|
||||
_copyRegions.Add(_currentStageRegion);
|
||||
}
|
||||
|
||||
private ByteSize AlignOffsetToNextLcm(ByteSize offset)
|
||||
{
|
||||
return ByteSize.Of((offset.Bytes + _lcm - 1) / _lcm * _lcm);
|
||||
}
|
||||
|
||||
private ByteSize _stageBufferDestinationOffset;
|
||||
private uint _lcm;
|
||||
private StagingRegion? _currentStageRegion;
|
||||
private UploadRegion? _currentUploadRegion;
|
||||
private List<StagingRegion> _copyRegions = [];
|
||||
private List<UploadRegion> _uploadRegions = [];
|
||||
}
|
||||
Loading…
Reference in a new issue