internal void TagResource(GraphicsResourceLink resourceLink)
        {
            switch (resourceLink.Resource)
            {
            case Texture texture:
                if (texture.Usage == GraphicsResourceUsage.Dynamic)
                {
                    // Increase the reference count until GPU is done with the resource
                    resourceLink.ReferenceCount++;
                    graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
                }
                break;

            case Buffer buffer:
                if (buffer.Usage == GraphicsResourceUsage.Dynamic)
                {
                    // Increase the reference count until GPU is done with the resource
                    resourceLink.ReferenceCount++;
                    graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
                }
                break;

            case QueryPool _:
                resourceLink.ReferenceCount++;
                graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
                break;
            }
        }
예제 #2
0
 internal void TagResource(GraphicsResourceLink resourceLink)
 {
     if (resourceLink.Resource is GraphicsResource resource)
     {
         resource.DiscardNextMap = true;
     }
 }
예제 #3
0
        private void UpdateCounter(GraphicsResourceLink resourceLink, int deltaCount)
        {
            int newVal = Interlocked.Add(ref resourceLink.ReferenceCount, deltaCount);

            if (newVal == 0)
            {
                GraphicsDevice.TagResource(resourceLink);
            }

            resourceLink.AccessTotalCount++;
            resourceLink.AccessCountSinceLastRecycle++;
            resourceLink.LastAccessTime = DateTime.Now;
        }
예제 #4
0
        private TResource GetTemporaryResource <TResource, TKey>(Dictionary <TKey, List <GraphicsResourceLink> > cache, TKey description, Func <TKey, PixelFormat, TResource> creator, Func <TResource, TKey> getDefinition, PixelFormat viewFormat)
            where TResource : GraphicsResourceBase
            where TKey : struct
        {
            // For a specific description, get allocated textures
            List <GraphicsResourceLink> resourceLinks;

            if (!cache.TryGetValue(description, out resourceLinks))
            {
                resourceLinks = new List <GraphicsResourceLink>();
                cache.Add(description, resourceLinks);
            }

            // Find a texture available
            foreach (var resourceLink in resourceLinks)
            {
                if (resourceLink.ReferenceCount == 0)
                {
                    UpdateCounter(resourceLink, 1);
                    return((TResource)resourceLink.Resource);
                }
            }

            // If no texture available, then creates a new one
            var newResource = creator(description, viewFormat);

            newResource.Name = string.Format("{0}{1}-{2}", Name == null ? string.Empty : string.Format("{0}-", Name), newResource.Name == null ? newResource.GetType().Name : Name, resourceLinks.Count);

            // Description may be altered when creating a resource (based on HW limitations...etc.) so we get the actual description
            var realDescription = getDefinition(newResource);

            // For a specific description, get allocated textures
            if (!cache.TryGetValue(realDescription, out resourceLinks))
            {
                resourceLinks = new List <GraphicsResourceLink>();
                cache.Add(description, resourceLinks);
            }

            // Add the texture to the allocated textures
            // Start RefCount == 1, because we don't want this texture to be available if a post FxProcessor is calling
            // several times this GetTemporaryTexture method.
            var newResourceLink = new GraphicsResourceLink(newResource)
            {
                ReferenceCount = 1
            };

            resourceLinks.Add(newResourceLink);

            return(newResource);
        }
예제 #5
0
        private void UpdateCounter(GraphicsResourceLink resourceLink, int deltaCount)
        {
            if ((resourceLink.ReferenceCount + deltaCount) < 0)
            {
                throw new InvalidOperationException("Invalid decrement on reference count (must be >=0 after decrement). Current reference count: [{0}] Decrement: [{1}]".ToFormat(resourceLink.ReferenceCount, deltaCount));
            }

            resourceLink.ReferenceCount += deltaCount;
            resourceLink.AccessTotalCount++;
            resourceLink.AccessCountSinceLastRecycle++;
            resourceLink.LastAccessTime = DateTime.Now;

            if (resourceLink.ReferenceCount == 0)
            {
                GraphicsDevice.TagResource(resourceLink);
            }
        }
예제 #6
0
        internal void TagResource(GraphicsResourceLink resourceLink)
        {
            var texture = resourceLink.Resource as Texture;

            if (texture != null && texture.Usage == GraphicsResourceUsage.Dynamic)
            {
                // Increase the reference count until GPU is done with the resource
                resourceLink.ReferenceCount++;
                TemporaryResources.Enqueue(new KeyValuePair <long, object>(NextFenceValue, resourceLink));
            }

            var buffer = resourceLink.Resource as Buffer;

            if (buffer != null && buffer.Usage == GraphicsResourceUsage.Dynamic)
            {
                // Increase the reference count until GPU is done with the resource
                resourceLink.ReferenceCount++;
                TemporaryResources.Enqueue(new KeyValuePair <long, object>(NextFenceValue, resourceLink));
            }
        }
예제 #7
0
 /// <summary>
 /// The default recycle policy is always going to remove all allocated textures.
 /// </summary>
 /// <param name="resourceLink">The resource link.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 private static bool DefaultRecyclePolicy(GraphicsResourceLink resourceLink)
 {
     return(true);
 }
예제 #8
0
        private TResource GetTemporaryResource <TResource, TKey>(Dictionary <TKey, List <GraphicsResourceLink> > cache, TKey description, Func <TKey, PixelFormat, TResource> creator, Func <TResource, TKey> getDefinition, PixelFormat viewFormat)
            where TResource : GraphicsResourceBase
            where TKey : struct
        {
            // For a specific description, get allocated textures
            List <GraphicsResourceLink> resourceLinks;

            using (thisLock.UpgradableReadLock())
            {
                if (!cache.TryGetValue(description, out resourceLinks))
                {
                    resourceLinks = new List <GraphicsResourceLink>();
                    using (thisLock.WriteLock())
                    {
                        cache.Add(description, resourceLinks);
                    }
                }

                // Find a texture available without locking
                for (int i = 0; i < resourceLinks.Count; i++)
                {
                    int rc = Interlocked.CompareExchange(ref resourceLinks[i].ReferenceCount, 1, 0);
                    if (rc == 0)
                    {
                        resourceLinks[i].AccessTotalCount++;
                        resourceLinks[i].AccessCountSinceLastRecycle++;
                        resourceLinks[i].LastAccessTime = DateTime.Now;
                        return((TResource)resourceLinks[i].Resource);
                    }
                }
            }

            // If no texture available, then creates a new one
            var newResource = creator(description, viewFormat);

            newResource.Name = string.Format("{0}{1}-{2}", Name == null ? string.Empty : string.Format("{0}-", Name), newResource.Name == null ? newResource.GetType().Name : Name, resourceLinks.Count);

            // Description may be altered when creating a resource (based on HW limitations...etc.) so we get the actual description
            var realDescription = getDefinition(newResource);

            // For a specific description, get allocated textures
            using (thisLock.UpgradableReadLock())
            {
                if (!cache.TryGetValue(realDescription, out resourceLinks))
                {
                    resourceLinks = new List <GraphicsResourceLink>();
                    using (thisLock.WriteLock())
                    {
                        cache.Add(description, resourceLinks);
                    }
                }

                // Add the texture to the allocated textures
                // Start RefCount == 1, because we don't want this texture to be available if a post FxProcessor is calling
                // several times this GetTemporaryTexture method.
                var newResourceLink = new GraphicsResourceLink(newResource)
                {
                    ReferenceCount = 1
                };

                using (thisLock.WriteLock())
                {
                    resourceLinks.Add(newResourceLink);
                }
            }


            return(newResource);
        }