2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
|
|
|
using System;
|
2020-02-06 22:49:26 +01:00
|
|
|
using System.Collections.Generic;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Image
|
|
|
|
{
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Texture manager.
|
|
|
|
/// </summary>
|
2019-12-31 23:09:49 +01:00
|
|
|
class TextureManager : IDisposable
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
private const int OverlapsBufferInitialCapacity = 10;
|
|
|
|
private const int OverlapsBufferMaxCapacity = 10000;
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private readonly GpuContext _context;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private readonly TextureBindingsManager _cpBindingsManager;
|
|
|
|
private readonly TextureBindingsManager _gpBindingsManager;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private readonly Texture[] _rtColors;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private Texture _rtDepthStencil;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private readonly ITexture[] _rtHostColors;
|
|
|
|
|
|
|
|
private ITexture _rtHostDs;
|
|
|
|
|
|
|
|
private readonly RangeList<Texture> _textures;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
private Texture[] _textureOverlaps;
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
private readonly AutoDeleteCache _cache;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-02-06 22:49:26 +01:00
|
|
|
private readonly HashSet<Texture> _modified;
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new instance of the texture manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">The GPU context that the texture manager belongs to</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public TextureManager(GpuContext context)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_context = context;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-22 18:17:06 +01:00
|
|
|
TexturePoolCache texturePoolCache = new TexturePoolCache(context);
|
|
|
|
|
|
|
|
_cpBindingsManager = new TextureBindingsManager(context, texturePoolCache, isCompute: true);
|
|
|
|
_gpBindingsManager = new TextureBindingsManager(context, texturePoolCache, isCompute: false);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
_rtColors = new Texture[Constants.TotalRenderTargets];
|
|
|
|
|
|
|
|
_rtHostColors = new ITexture[Constants.TotalRenderTargets];
|
|
|
|
|
2019-11-24 03:24:03 +01:00
|
|
|
_textures = new RangeList<Texture>();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
_textureOverlaps = new Texture[OverlapsBufferInitialCapacity];
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
_cache = new AutoDeleteCache();
|
2020-02-06 22:49:26 +01:00
|
|
|
|
|
|
|
_modified = new HashSet<Texture>(new ReferenceEqualityComparer<Texture>());
|
2019-10-18 04:41:18 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets texture bindings on the compute pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="bindings">The texture bindings</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetComputeTextures(TextureBindingInfo[] bindings)
|
|
|
|
{
|
|
|
|
_cpBindingsManager.SetTextures(0, bindings);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets texture bindings on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stage">The index of the shader stage to bind the textures</param>
|
|
|
|
/// <param name="bindings">The texture bindings</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetGraphicsTextures(int stage, TextureBindingInfo[] bindings)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_gpBindingsManager.SetTextures(stage, bindings);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets image bindings on the compute pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="bindings">The image bindings</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetComputeImages(TextureBindingInfo[] bindings)
|
|
|
|
{
|
|
|
|
_cpBindingsManager.SetImages(0, bindings);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets image bindings on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stage">The index of the shader stage to bind the images</param>
|
|
|
|
/// <param name="bindings">The image bindings</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetGraphicsImages(int stage, TextureBindingInfo[] bindings)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_gpBindingsManager.SetImages(stage, bindings);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the texture constant buffer index on the compute pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The texture constant buffer index</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetComputeTextureBufferIndex(int index)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_cpBindingsManager.SetTextureBufferIndex(index);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the texture constant buffer index on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The texture constant buffer index</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetGraphicsTextureBufferIndex(int index)
|
|
|
|
{
|
|
|
|
_gpBindingsManager.SetTextureBufferIndex(index);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current sampler pool on the compute pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">The start GPU virtual address of the sampler pool</param>
|
|
|
|
/// <param name="maximumId">The maximum ID of the sampler pool</param>
|
2019-12-30 18:44:22 +01:00
|
|
|
/// <param name="samplerIndex">The indexing type of the sampler pool</param>
|
2019-12-05 21:34:47 +01:00
|
|
|
public void SetComputeSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2019-12-05 21:34:47 +01:00
|
|
|
_cpBindingsManager.SetSamplerPool(gpuVa, maximumId, samplerIndex);
|
2019-10-18 04:41:18 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current sampler pool on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">The start GPU virtual address of the sampler pool</param>
|
|
|
|
/// <param name="maximumId">The maximum ID of the sampler pool</param>
|
2019-12-30 18:44:22 +01:00
|
|
|
/// <param name="samplerIndex">The indexing type of the sampler pool</param>
|
2019-12-05 21:34:47 +01:00
|
|
|
public void SetGraphicsSamplerPool(ulong gpuVa, int maximumId, SamplerIndex samplerIndex)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2019-12-05 21:34:47 +01:00
|
|
|
_gpBindingsManager.SetSamplerPool(gpuVa, maximumId, samplerIndex);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current texture pool on the compute pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">The start GPU virtual address of the texture pool</param>
|
|
|
|
/// <param name="maximumId">The maximum ID of the texture pool</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetComputeTexturePool(ulong gpuVa, int maximumId)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_cpBindingsManager.SetTexturePool(gpuVa, maximumId);
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current texture pool on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gpuVa">The start GPU virtual address of the texture pool</param>
|
|
|
|
/// <param name="maximumId">The maximum ID of the texture pool</param>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void SetGraphicsTexturePool(ulong gpuVa, int maximumId)
|
|
|
|
{
|
|
|
|
_gpBindingsManager.SetTexturePool(gpuVa, maximumId);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the render target color buffer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The index of the color buffer to set (up to 8)</param>
|
|
|
|
/// <param name="color">The color buffer texture</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void SetRenderTargetColor(int index, Texture color)
|
|
|
|
{
|
|
|
|
_rtColors[index] = color;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the render target depth-stencil buffer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="depthStencil">The depth-stencil buffer texture</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void SetRenderTargetDepthStencil(Texture depthStencil)
|
|
|
|
{
|
|
|
|
_rtDepthStencil = depthStencil;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Commits bindings on the compute pipeline.
|
|
|
|
/// </summary>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void CommitComputeBindings()
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-26 19:50:52 +02:00
|
|
|
// Every time we switch between graphics and compute work,
|
2019-10-18 04:41:18 +02:00
|
|
|
// we must rebind everything.
|
|
|
|
// Since compute work happens less often, we always do that
|
|
|
|
// before and after the compute dispatch.
|
|
|
|
_cpBindingsManager.Rebind();
|
|
|
|
_cpBindingsManager.CommitBindings();
|
|
|
|
_gpBindingsManager.Rebind();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Commits bindings on the graphics pipeline.
|
|
|
|
/// </summary>
|
2019-10-18 04:41:18 +02:00
|
|
|
public void CommitGraphicsBindings()
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-18 04:41:18 +02:00
|
|
|
_gpBindingsManager.CommitBindings();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
UpdateRenderTargets();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a texture descriptor used on the graphics pipeline.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="stageIndex">Index of the shader stage where the texture is bound</param>
|
|
|
|
/// <param name="handle">Shader "fake" handle of the texture</param>
|
|
|
|
/// <returns>The texture descriptor</returns>
|
2019-12-16 05:59:46 +01:00
|
|
|
public TextureDescriptor GetGraphicsTextureDescriptor(GpuState state, int stageIndex, int handle)
|
|
|
|
{
|
|
|
|
return _gpBindingsManager.GetTextureDescriptor(state, stageIndex, handle);
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Update host framebuffer attachments based on currently bound render target buffers.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
private void UpdateRenderTargets()
|
|
|
|
{
|
|
|
|
bool anyChanged = false;
|
|
|
|
|
|
|
|
if (_rtHostDs != _rtDepthStencil?.HostTexture)
|
|
|
|
{
|
|
|
|
_rtHostDs = _rtDepthStencil?.HostTexture;
|
|
|
|
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
for (int index = 0; index < _rtColors.Length; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-31 00:45:01 +01:00
|
|
|
ITexture hostTexture = _rtColors[index]?.HostTexture;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
if (_rtHostColors[index] != hostTexture)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-10-31 00:45:01 +01:00
|
|
|
_rtHostColors[index] = hostTexture;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
anyChanged = true;
|
|
|
|
}
|
2019-10-31 00:45:01 +01:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
if (anyChanged)
|
|
|
|
{
|
|
|
|
_context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Tries to find an existing texture, or create a new one if not found.
|
2019-12-30 00:26:37 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="copyTexture">Copy texture to find or create</param>
|
|
|
|
/// <returns>The texture</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public Texture FindOrCreateTexture(CopyTexture copyTexture)
|
|
|
|
{
|
|
|
|
ulong address = _context.MemoryManager.Translate(copyTexture.Address.Pack());
|
|
|
|
|
|
|
|
if (address == MemoryManager.BadAddress)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gobBlocksInY = copyTexture.MemoryLayout.UnpackGobBlocksInY();
|
|
|
|
int gobBlocksInZ = copyTexture.MemoryLayout.UnpackGobBlocksInZ();
|
|
|
|
|
|
|
|
FormatInfo formatInfo = copyTexture.Format.Convert();
|
|
|
|
|
2019-10-14 03:48:09 +02:00
|
|
|
int width;
|
|
|
|
|
|
|
|
if (copyTexture.LinearLayout)
|
|
|
|
{
|
|
|
|
width = copyTexture.Stride / formatInfo.BytesPerPixel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = copyTexture.Width;
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
TextureInfo info = new TextureInfo(
|
|
|
|
address,
|
2019-10-14 03:48:09 +02:00
|
|
|
width,
|
2019-10-13 08:02:07 +02:00
|
|
|
copyTexture.Height,
|
|
|
|
copyTexture.Depth,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
copyTexture.Stride,
|
|
|
|
copyTexture.LinearLayout,
|
|
|
|
gobBlocksInY,
|
|
|
|
gobBlocksInZ,
|
|
|
|
1,
|
|
|
|
Target.Texture2D,
|
|
|
|
formatInfo);
|
|
|
|
|
|
|
|
Texture texture = FindOrCreateTexture(info, TextureSearchFlags.IgnoreMs);
|
|
|
|
|
|
|
|
texture.SynchronizeMemory();
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Tries to find an existing texture, or create a new one if not found.
|
2019-12-30 00:26:37 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="colorState">Color buffer texture to find or create</param>
|
|
|
|
/// <param name="samplesInX">Number of samples in the X direction, for MSAA</param>
|
|
|
|
/// <param name="samplesInY">Number of samples in the Y direction, for MSAA</param>
|
|
|
|
/// <returns>The texture</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public Texture FindOrCreateTexture(RtColorState colorState, int samplesInX, int samplesInY)
|
|
|
|
{
|
|
|
|
ulong address = _context.MemoryManager.Translate(colorState.Address.Pack());
|
|
|
|
|
|
|
|
if (address == MemoryManager.BadAddress)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isLinear = colorState.MemoryLayout.UnpackIsLinear();
|
|
|
|
|
|
|
|
int gobBlocksInY = colorState.MemoryLayout.UnpackGobBlocksInY();
|
|
|
|
int gobBlocksInZ = colorState.MemoryLayout.UnpackGobBlocksInZ();
|
|
|
|
|
|
|
|
Target target;
|
|
|
|
|
|
|
|
if (colorState.MemoryLayout.UnpackIsTarget3D())
|
|
|
|
{
|
|
|
|
target = Target.Texture3D;
|
|
|
|
}
|
|
|
|
else if ((samplesInX | samplesInY) != 1)
|
|
|
|
{
|
|
|
|
target = colorState.Depth > 1
|
|
|
|
? Target.Texture2DMultisampleArray
|
|
|
|
: Target.Texture2DMultisample;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target = colorState.Depth > 1
|
|
|
|
? Target.Texture2DArray
|
|
|
|
: Target.Texture2D;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatInfo formatInfo = colorState.Format.Convert();
|
|
|
|
|
|
|
|
int width, stride;
|
|
|
|
|
|
|
|
// For linear textures, the width value is actually the stride.
|
|
|
|
// We can easily get the width by dividing the stride by the bpp,
|
|
|
|
// since the stride is the total number of bytes occupied by a
|
|
|
|
// line. The stride should also meet alignment constraints however,
|
|
|
|
// so the width we get here is the aligned width.
|
|
|
|
if (isLinear)
|
|
|
|
{
|
|
|
|
width = colorState.WidthOrStride / formatInfo.BytesPerPixel;
|
|
|
|
stride = colorState.WidthOrStride;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = colorState.WidthOrStride;
|
|
|
|
stride = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureInfo info = new TextureInfo(
|
|
|
|
address,
|
|
|
|
width,
|
|
|
|
colorState.Height,
|
|
|
|
colorState.Depth,
|
|
|
|
1,
|
|
|
|
samplesInX,
|
|
|
|
samplesInY,
|
|
|
|
stride,
|
|
|
|
isLinear,
|
|
|
|
gobBlocksInY,
|
|
|
|
gobBlocksInZ,
|
|
|
|
1,
|
|
|
|
target,
|
|
|
|
formatInfo);
|
|
|
|
|
|
|
|
Texture texture = FindOrCreateTexture(info);
|
|
|
|
|
|
|
|
texture.SynchronizeMemory();
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Tries to find an existing texture, or create a new one if not found.
|
2019-12-30 00:26:37 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="dsState">Depth-stencil buffer texture to find or create</param>
|
|
|
|
/// <param name="size">Size of the depth-stencil texture</param>
|
|
|
|
/// <param name="samplesInX">Number of samples in the X direction, for MSAA</param>
|
|
|
|
/// <param name="samplesInY">Number of samples in the Y direction, for MSAA</param>
|
|
|
|
/// <returns>The texture</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public Texture FindOrCreateTexture(RtDepthStencilState dsState, Size3D size, int samplesInX, int samplesInY)
|
|
|
|
{
|
|
|
|
ulong address = _context.MemoryManager.Translate(dsState.Address.Pack());
|
|
|
|
|
|
|
|
if (address == MemoryManager.BadAddress)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gobBlocksInY = dsState.MemoryLayout.UnpackGobBlocksInY();
|
|
|
|
int gobBlocksInZ = dsState.MemoryLayout.UnpackGobBlocksInZ();
|
|
|
|
|
|
|
|
Target target = (samplesInX | samplesInY) != 1
|
|
|
|
? Target.Texture2DMultisample
|
|
|
|
: Target.Texture2D;
|
|
|
|
|
|
|
|
FormatInfo formatInfo = dsState.Format.Convert();
|
|
|
|
|
|
|
|
TextureInfo info = new TextureInfo(
|
|
|
|
address,
|
|
|
|
size.Width,
|
|
|
|
size.Height,
|
|
|
|
size.Depth,
|
|
|
|
1,
|
|
|
|
samplesInX,
|
|
|
|
samplesInY,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
gobBlocksInY,
|
|
|
|
gobBlocksInZ,
|
|
|
|
1,
|
|
|
|
target,
|
|
|
|
formatInfo);
|
|
|
|
|
|
|
|
Texture texture = FindOrCreateTexture(info);
|
|
|
|
|
|
|
|
texture.SynchronizeMemory();
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Tries to find an existing texture, or create a new one if not found.
|
2019-12-30 00:26:37 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="info">Texture information of the texture to be found or created</param>
|
|
|
|
/// <param name="flags">The texture search flags, defines texture comparison rules</param>
|
|
|
|
/// <returns>The texture</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public Texture FindOrCreateTexture(TextureInfo info, TextureSearchFlags flags = TextureSearchFlags.None)
|
|
|
|
{
|
|
|
|
bool isSamplerTexture = (flags & TextureSearchFlags.Sampler) != 0;
|
|
|
|
|
|
|
|
// Try to find a perfect texture match, with the same address and parameters.
|
2019-11-25 01:29:37 +01:00
|
|
|
int sameAddressOverlapsCount = _textures.FindOverlaps(info.Address, ref _textureOverlaps);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
for (int index = 0; index < sameAddressOverlapsCount; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
Texture overlap = _textureOverlaps[index];
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (overlap.IsPerfectMatch(info, flags))
|
|
|
|
{
|
|
|
|
if (!isSamplerTexture)
|
|
|
|
{
|
|
|
|
// If not a sampler texture, it is managed by the auto delete
|
|
|
|
// cache, ensure that it is on the "top" of the list to avoid
|
|
|
|
// deletion.
|
|
|
|
_cache.Lift(overlap);
|
|
|
|
}
|
|
|
|
else if (!overlap.SizeMatches(info))
|
|
|
|
{
|
|
|
|
// If this is used for sampling, the size must match,
|
|
|
|
// otherwise the shader would sample garbage data.
|
|
|
|
// To fix that, we create a new texture with the correct
|
|
|
|
// size, and copy the data from the old one to the new one.
|
|
|
|
overlap.ChangeSize(info.Width, info.Height, info.DepthOrLayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
return overlap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate texture sizes, used to find all overlapping textures.
|
|
|
|
SizeInfo sizeInfo;
|
|
|
|
|
|
|
|
if (info.IsLinear)
|
|
|
|
{
|
|
|
|
sizeInfo = SizeCalculator.GetLinearTextureSize(
|
|
|
|
info.Stride,
|
|
|
|
info.Height,
|
|
|
|
info.FormatInfo.BlockHeight);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sizeInfo = SizeCalculator.GetBlockLinearTextureSize(
|
|
|
|
info.Width,
|
|
|
|
info.Height,
|
|
|
|
info.GetDepth(),
|
|
|
|
info.Levels,
|
|
|
|
info.GetLayers(),
|
|
|
|
info.FormatInfo.BlockWidth,
|
|
|
|
info.FormatInfo.BlockHeight,
|
|
|
|
info.FormatInfo.BytesPerPixel,
|
|
|
|
info.GobBlocksInY,
|
|
|
|
info.GobBlocksInZ,
|
|
|
|
info.GobBlocksInTileX);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find view compatible matches.
|
|
|
|
ulong size = (ulong)sizeInfo.TotalSize;
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
int overlapsCount = _textures.FindOverlaps(info.Address, size, ref _textureOverlaps);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
Texture texture = null;
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
for (int index = 0; index < overlapsCount; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
Texture overlap = _textureOverlaps[index];
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (overlap.IsViewCompatible(info, size, out int firstLayer, out int firstLevel))
|
|
|
|
{
|
|
|
|
if (!isSamplerTexture)
|
|
|
|
{
|
|
|
|
info = AdjustSizes(overlap, info, firstLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
texture = overlap.CreateView(info, sizeInfo, firstLayer, firstLevel);
|
|
|
|
|
|
|
|
// The size only matters (and is only really reliable) when the
|
|
|
|
// texture is used on a sampler, because otherwise the size will be
|
|
|
|
// aligned.
|
|
|
|
if (!overlap.SizeMatches(info, firstLevel) && isSamplerTexture)
|
|
|
|
{
|
|
|
|
texture.ChangeSize(info.Width, info.Height, info.DepthOrLayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match, create a new texture.
|
|
|
|
if (texture == null)
|
|
|
|
{
|
|
|
|
texture = new Texture(_context, info, sizeInfo);
|
|
|
|
|
|
|
|
// We need to synchronize before copying the old view data to the texture,
|
|
|
|
// otherwise the copied data would be overwritten by a future synchronization.
|
|
|
|
texture.SynchronizeMemory();
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
for (int index = 0; index < overlapsCount; index++)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
Texture overlap = _textureOverlaps[index];
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (texture.IsViewCompatible(overlap.Info, overlap.Size, out int firstLayer, out int firstLevel))
|
|
|
|
{
|
|
|
|
TextureInfo overlapInfo = AdjustSizes(texture, overlap.Info, firstLevel);
|
|
|
|
|
|
|
|
TextureCreateInfo createInfo = GetCreateInfo(overlapInfo, _context.Capabilities);
|
|
|
|
|
|
|
|
ITexture newView = texture.HostTexture.CreateView(createInfo, firstLayer, firstLevel);
|
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
overlap.HostTexture.CopyTo(newView, 0, 0);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
overlap.ReplaceView(texture, overlapInfo, newView);
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 00:45:01 +01:00
|
|
|
|
|
|
|
// If the texture is a 3D texture, we need to additionally copy any slice
|
|
|
|
// of the 3D texture to the newly created 3D texture.
|
|
|
|
if (info.Target == Target.Texture3D)
|
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
for (int index = 0; index < overlapsCount; index++)
|
2019-10-31 00:45:01 +01:00
|
|
|
{
|
2019-11-25 01:29:37 +01:00
|
|
|
Texture overlap = _textureOverlaps[index];
|
|
|
|
|
2019-10-31 00:45:01 +01:00
|
|
|
if (texture.IsViewCompatible(
|
|
|
|
overlap.Info,
|
|
|
|
overlap.Size,
|
|
|
|
isCopy: true,
|
|
|
|
out int firstLayer,
|
|
|
|
out int firstLevel))
|
|
|
|
{
|
|
|
|
overlap.HostTexture.CopyTo(texture.HostTexture, firstLayer, firstLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sampler textures are managed by the texture pool, all other textures
|
|
|
|
// are managed by the auto delete cache.
|
|
|
|
if (!isSamplerTexture)
|
|
|
|
{
|
|
|
|
_cache.Add(texture);
|
2020-02-06 22:49:26 +01:00
|
|
|
texture.Modified += CacheTextureModified;
|
|
|
|
texture.Disposed += CacheTextureDisposed;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_textures.Add(texture);
|
|
|
|
|
2019-11-25 01:29:37 +01:00
|
|
|
ShrinkOverlapsBufferIfNeeded();
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:49:26 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Signaled when a cache texture is modified, and adds it to a set to be enumerated when flushing textures.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="texture">The texture that was modified.</param>
|
|
|
|
private void CacheTextureModified(Texture texture)
|
|
|
|
{
|
|
|
|
_modified.Add(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Signaled when a cache texture is disposed, so it can be removed from the set of modified textures if present.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="texture">The texture that was diosposed.</param>
|
|
|
|
private void CacheTextureDisposed(Texture texture)
|
|
|
|
{
|
|
|
|
_modified.Remove(texture);
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Resizes the temporary buffer used for range list intersection results, if it has grown too much.
|
|
|
|
/// </summary>
|
2019-11-25 01:29:37 +01:00
|
|
|
private void ShrinkOverlapsBufferIfNeeded()
|
|
|
|
{
|
|
|
|
if (_textureOverlaps.Length > OverlapsBufferMaxCapacity)
|
|
|
|
{
|
|
|
|
Array.Resize(ref _textureOverlaps, OverlapsBufferMaxCapacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Adjusts the size of the texture information for a given mipmap level,
|
|
|
|
/// based on the size of a parent texture.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="parent">The parent texture</param>
|
|
|
|
/// <param name="info">The texture information to be adjusted</param>
|
|
|
|
/// <param name="firstLevel">The first level of the texture view</param>
|
|
|
|
/// <returns>The adjusted texture information with the new size</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
private static TextureInfo AdjustSizes(Texture parent, TextureInfo info, int firstLevel)
|
|
|
|
{
|
|
|
|
// When the texture is used as view of another texture, we must
|
|
|
|
// ensure that the sizes are valid, otherwise data uploads would fail
|
|
|
|
// (and the size wouldn't match the real size used on the host API).
|
|
|
|
// Given a parent texture from where the view is created, we have the
|
|
|
|
// following rules:
|
|
|
|
// - The view size must be equal to the parent size, divided by (2 ^ l),
|
|
|
|
// where l is the first mipmap level of the view. The division result must
|
|
|
|
// be rounded down, and the result must be clamped to 1.
|
|
|
|
// - If the parent format is compressed, and the view format isn't, the
|
|
|
|
// view size is calculated as above, but the width and height of the
|
|
|
|
// view must be also divided by the compressed format block width and height.
|
|
|
|
// - If the parent format is not compressed, and the view is, the view
|
|
|
|
// size is calculated as described on the first point, but the width and height
|
|
|
|
// of the view must be also multiplied by the block width and height.
|
|
|
|
int width = Math.Max(1, parent.Info.Width >> firstLevel);
|
|
|
|
int height = Math.Max(1, parent.Info.Height >> firstLevel);
|
|
|
|
|
|
|
|
if (parent.Info.FormatInfo.IsCompressed && !info.FormatInfo.IsCompressed)
|
|
|
|
{
|
|
|
|
width = BitUtils.DivRoundUp(width, parent.Info.FormatInfo.BlockWidth);
|
|
|
|
height = BitUtils.DivRoundUp(height, parent.Info.FormatInfo.BlockHeight);
|
|
|
|
}
|
|
|
|
else if (!parent.Info.FormatInfo.IsCompressed && info.FormatInfo.IsCompressed)
|
|
|
|
{
|
|
|
|
width *= info.FormatInfo.BlockWidth;
|
|
|
|
height *= info.FormatInfo.BlockHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int depthOrLayers;
|
|
|
|
|
|
|
|
if (info.Target == Target.Texture3D)
|
|
|
|
{
|
|
|
|
depthOrLayers = Math.Max(1, parent.Info.DepthOrLayers >> firstLevel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
depthOrLayers = info.DepthOrLayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TextureInfo(
|
|
|
|
info.Address,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
depthOrLayers,
|
|
|
|
info.Levels,
|
|
|
|
info.SamplesInX,
|
|
|
|
info.SamplesInY,
|
|
|
|
info.Stride,
|
|
|
|
info.IsLinear,
|
|
|
|
info.GobBlocksInY,
|
|
|
|
info.GobBlocksInZ,
|
|
|
|
info.GobBlocksInTileX,
|
|
|
|
info.Target,
|
|
|
|
info.FormatInfo,
|
|
|
|
info.DepthStencilMode,
|
|
|
|
info.SwizzleR,
|
|
|
|
info.SwizzleG,
|
|
|
|
info.SwizzleB,
|
|
|
|
info.SwizzleA);
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a texture creation information from texture information.
|
|
|
|
/// This can be used to create new host textures.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="info">Texture information</param>
|
|
|
|
/// <param name="caps">GPU capabilities</param>
|
|
|
|
/// <returns>The texture creation information</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public static TextureCreateInfo GetCreateInfo(TextureInfo info, Capabilities caps)
|
|
|
|
{
|
|
|
|
FormatInfo formatInfo = info.FormatInfo;
|
|
|
|
|
|
|
|
if (!caps.SupportsAstcCompression)
|
|
|
|
{
|
|
|
|
if (formatInfo.Format.IsAstcUnorm())
|
|
|
|
{
|
|
|
|
formatInfo = new FormatInfo(Format.R8G8B8A8Unorm, 1, 1, 4);
|
|
|
|
}
|
|
|
|
else if (formatInfo.Format.IsAstcSrgb())
|
|
|
|
{
|
|
|
|
formatInfo = new FormatInfo(Format.R8G8B8A8Srgb, 1, 1, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = info.Width / info.SamplesInX;
|
|
|
|
int height = info.Height / info.SamplesInY;
|
|
|
|
|
|
|
|
int depth = info.GetDepth() * info.GetLayers();
|
|
|
|
|
|
|
|
return new TextureCreateInfo(
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
depth,
|
|
|
|
info.Levels,
|
|
|
|
info.Samples,
|
|
|
|
formatInfo.BlockWidth,
|
|
|
|
formatInfo.BlockHeight,
|
|
|
|
formatInfo.BytesPerPixel,
|
|
|
|
formatInfo.Format,
|
|
|
|
info.DepthStencilMode,
|
|
|
|
info.Target,
|
|
|
|
info.SwizzleR,
|
|
|
|
info.SwizzleG,
|
|
|
|
info.SwizzleB,
|
|
|
|
info.SwizzleA);
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Flushes all the textures in the cache that have been modified since the last call.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Flush()
|
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
foreach (Texture texture in _modified)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
if (texture.Info.IsLinear)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
texture.Flush();
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 22:49:26 +01:00
|
|
|
_modified.Clear();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
2019-12-30 18:44:22 +01:00
|
|
|
/// Flushes the textures in the cache inside a given range that have been modified since the last call.
|
2019-12-30 00:26:37 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="address">The range start address</param>
|
|
|
|
/// <param name="size">The range size</param>
|
2019-12-05 21:34:47 +01:00
|
|
|
public void Flush(ulong address, ulong size)
|
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
foreach (Texture texture in _modified)
|
2019-12-05 21:34:47 +01:00
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
if (texture.OverlapsWith(address, size))
|
2019-12-05 21:34:47 +01:00
|
|
|
{
|
|
|
|
texture.Flush();
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 22:49:26 +01:00
|
|
|
_modified.Clear();
|
2019-12-05 21:34:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Removes a texture from the cache.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2019-12-30 00:26:37 +01:00
|
|
|
/// This only removes the texture from the internal list, not from the auto-deletion cache.
|
|
|
|
/// It may still have live references after the removal.
|
2020-01-01 16:39:09 +01:00
|
|
|
/// </remarks>
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <param name="texture">The texture to be removed</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void RemoveTextureFromCache(Texture texture)
|
|
|
|
{
|
|
|
|
_textures.Remove(texture);
|
|
|
|
}
|
2019-12-31 23:09:49 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes all textures in the cache.
|
|
|
|
/// It's an error to use the texture manager after disposal.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
foreach (Texture texture in _textures)
|
|
|
|
{
|
2020-02-06 22:49:26 +01:00
|
|
|
_modified.Remove(texture);
|
2019-12-31 23:09:49 +01:00
|
|
|
texture.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|