From 1b7d95519569639135a68e7ebda5148f3263217c Mon Sep 17 00:00:00 2001
From: gdk <gab.dark.100@gmail.com>
Date: Thu, 17 Oct 2019 23:41:18 -0300
Subject: Initial support for image stores, support texture sample on compute

---
 Ryujinx.Graphics.Gpu/Image/TextureManager.cs | 173 ++++++++-------------------
 1 file changed, 52 insertions(+), 121 deletions(-)

(limited to 'Ryujinx.Graphics.Gpu/Image/TextureManager.cs')

diff --git a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs
index 23416ddd..91a9cfd1 100644
--- a/Ryujinx.Graphics.Gpu/Image/TextureManager.cs
+++ b/Ryujinx.Graphics.Gpu/Image/TextureManager.cs
@@ -4,7 +4,6 @@ using Ryujinx.Graphics.GAL.Texture;
 using Ryujinx.Graphics.Gpu.Image;
 using Ryujinx.Graphics.Gpu.Memory;
 using Ryujinx.Graphics.Gpu.State;
-using Ryujinx.Graphics.Shader;
 using Ryujinx.Graphics.Texture;
 using System;
 
@@ -12,15 +11,10 @@ namespace Ryujinx.Graphics.Gpu.Image
 {
     class TextureManager
     {
-        private GpuContext    _context;
-        private BufferManager _bufferManager;
+        private GpuContext _context;
 
-        private SamplerPool _samplerPool;
-
-        private ulong _texturePoolAddress;
-        private int   _texturePoolMaximumId;
-
-        private TexturePoolCache _texturePoolCache;
+        private TextureBindingsManager _cpBindingsManager;
+        private TextureBindingsManager _gpBindingsManager;
 
         private Texture[] _rtColors;
         private Texture   _rtColor3D;
@@ -35,24 +29,12 @@ namespace Ryujinx.Graphics.Gpu.Image
 
         private AutoDeleteCache _cache;
 
-        private TextureBindingInfo[][] _bindings;
-
-        private struct TextureStatePerStage
-        {
-            public ITexture Texture;
-            public ISampler Sampler;
-        }
-
-        private TextureStatePerStage[][] _textureState;
-
-        private int _textureBufferIndex;
-
-        public TextureManager(GpuContext context, BufferManager bufferManager)
+        public TextureManager(GpuContext context)
         {
-            _context       = context;
-            _bufferManager = bufferManager;
+            _context = context;
 
-            _texturePoolCache = new TexturePoolCache(context, this);
+            _cpBindingsManager = new TextureBindingsManager(context, isCompute: true);
+            _gpBindingsManager = new TextureBindingsManager(context, isCompute: false);
 
             _rtColors = new Texture[Constants.TotalRenderTargets];
 
@@ -61,47 +43,56 @@ namespace Ryujinx.Graphics.Gpu.Image
             _textures = new RangeList<Texture>();
 
             _cache = new AutoDeleteCache();
+        }
 
-            _bindings = new TextureBindingInfo[Constants.TotalShaderStages][];
-
-            _textureState = new TextureStatePerStage[Constants.TotalShaderStages][];
+        public void SetComputeTextures(TextureBindingInfo[] bindings)
+        {
+            _cpBindingsManager.SetTextures(0, bindings);
         }
 
-        public void BindTextures(int stage, TextureBindingInfo[] bindings)
+        public void SetGraphicsTextures(int stage, TextureBindingInfo[] bindings)
         {
-            _bindings[stage] = bindings;
+            _gpBindingsManager.SetTextures(stage, bindings);
+        }
 
-            _textureState[stage] = new TextureStatePerStage[bindings.Length];
+        public void SetComputeImages(TextureBindingInfo[] bindings)
+        {
+            _cpBindingsManager.SetImages(0, bindings);
         }
 
-        public void SetTextureBufferIndex(int index)
+        public void SetGraphicsImages(int stage, TextureBindingInfo[] bindings)
         {
-            _textureBufferIndex = index;
+            _gpBindingsManager.SetImages(stage, bindings);
         }
 
-        public void SetSamplerPool(ulong gpuVa, int maximumId)
+        public void SetComputeTextureBufferIndex(int index)
         {
-            ulong address = _context.MemoryManager.Translate(gpuVa);
+            _cpBindingsManager.SetTextureBufferIndex(index);
+        }
 
-            if (_samplerPool != null)
-            {
-                if (_samplerPool.Address == address)
-                {
-                    return;
-                }
+        public void SetGraphicsTextureBufferIndex(int index)
+        {
+            _gpBindingsManager.SetTextureBufferIndex(index);
+        }
 
-                _samplerPool.Dispose();
-            }
+        public void SetComputeSamplerPool(ulong gpuVa, int maximumId)
+        {
+            _cpBindingsManager.SetSamplerPool(gpuVa, maximumId);
+        }
 
-            _samplerPool = new SamplerPool(_context, address, maximumId);
+        public void SetGraphicsSamplerPool(ulong gpuVa, int maximumId)
+        {
+            _gpBindingsManager.SetSamplerPool(gpuVa, maximumId);
         }
 
-        public void SetTexturePool(ulong gpuVa, int maximumId)
+        public void SetComputeTexturePool(ulong gpuVa, int maximumId)
         {
-            ulong address = _context.MemoryManager.Translate(gpuVa);
+            _cpBindingsManager.SetTexturePool(gpuVa, maximumId);
+        }
 
-            _texturePoolAddress   = address;
-            _texturePoolMaximumId = maximumId;
+        public void SetGraphicsTexturePool(ulong gpuVa, int maximumId)
+        {
+            _gpBindingsManager.SetTexturePool(gpuVa, maximumId);
         }
 
         public void SetRenderTargetColor(int index, Texture color)
@@ -121,59 +112,22 @@ namespace Ryujinx.Graphics.Gpu.Image
             _rtDepthStencil = depthStencil;
         }
 
-        public void CommitBindings()
+        public void CommitComputeBindings()
         {
-            UpdateTextures();
-            UpdateRenderTargets();
+            // Evert time we switch between graphics and compute work,
+            // 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();
         }
 
-        private void UpdateTextures()
+        public void CommitGraphicsBindings()
         {
-            TexturePool texturePool = _texturePoolCache.FindOrCreate(
-                _texturePoolAddress,
-                _texturePoolMaximumId);
-
-            for (ShaderStage stage = ShaderStage.Vertex; stage <= ShaderStage.Fragment; stage++)
-            {
-                int stageIndex = (int)stage - 1;
+            _gpBindingsManager.CommitBindings();
 
-                if (_bindings[stageIndex] == null)
-                {
-                    continue;
-                }
-
-                for (int index = 0; index < _bindings[stageIndex].Length; index++)
-                {
-                    TextureBindingInfo binding = _bindings[stageIndex][index];
-
-                    int packedId = ReadPackedId(stageIndex, binding.Handle);
-
-                    int textureId = (packedId >> 0)  & 0xfffff;
-                    int samplerId = (packedId >> 20) & 0xfff;
-
-                    Texture texture = texturePool.Get(textureId);
-
-                    ITexture hostTexture = texture?.GetTargetTexture(binding.Target);
-
-                    if (_textureState[stageIndex][index].Texture != hostTexture)
-                    {
-                        _textureState[stageIndex][index].Texture = hostTexture;
-
-                        _context.Renderer.GraphicsPipeline.BindTexture(index, stage, hostTexture);
-                    }
-
-                    Sampler sampler = _samplerPool.Get(samplerId);
-
-                    ISampler hostSampler = sampler?.HostSampler;
-
-                    if (_textureState[stageIndex][index].Sampler != hostSampler)
-                    {
-                        _textureState[stageIndex][index].Sampler = hostSampler;
-
-                        _context.Renderer.GraphicsPipeline.BindSampler(index, stage, hostSampler);
-                    }
-                }
-            }
+            UpdateRenderTargets();
         }
 
         private void UpdateRenderTargets()
@@ -203,7 +157,7 @@ namespace Ryujinx.Graphics.Gpu.Image
 
                 if (anyChanged)
                 {
-                    _context.Renderer.GraphicsPipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
+                    _context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
                 }
             }
             else
@@ -217,20 +171,11 @@ namespace Ryujinx.Graphics.Gpu.Image
 
                 if (anyChanged)
                 {
-                    _context.Renderer.GraphicsPipeline.SetRenderTargets(_rtColor3D.HostTexture, _rtHostDs);
+                    _context.Renderer.Pipeline.SetRenderTargets(_rtColor3D.HostTexture, _rtHostDs);
                 }
             }
         }
 
-        private int ReadPackedId(int stage, int wordOffset)
-        {
-            ulong address = _bufferManager.GetGraphicsUniformBufferAddress(stage, _textureBufferIndex);
-
-            address += (uint)wordOffset * 4;
-
-            return BitConverter.ToInt32(_context.PhysicalMemory.Read(address, 4));
-        }
-
         public Texture FindOrCreateTexture(CopyTexture copyTexture)
         {
             ulong address = _context.MemoryManager.Translate(copyTexture.Address.Pack());
@@ -645,20 +590,6 @@ namespace Ryujinx.Graphics.Gpu.Image
             return ts[0];
         }
 
-        public void InvalidateRange(ulong address, ulong size)
-        {
-            Texture[] overlaps = _textures.FindOverlaps(address, size);
-
-            foreach (Texture overlap in overlaps)
-            {
-                overlap.Invalidate();
-            }
-
-            _samplerPool?.InvalidateRange(address, size);
-
-            _texturePoolCache.InvalidateRange(address, size);
-        }
-
         public void Flush()
         {
             foreach (Texture texture in _cache)
-- 
cgit v1.2.3-70-g09d2