protected override void UnmapCore(MappableResource resource, uint subresource) { MappedResourceCacheKey key = new MappedResourceCacheKey(resource, subresource); bool commitUnmap; lock (_mappedResourceLock) { if (!_mappedResources.TryGetValue(key, out MappedResourceInfo info)) { throw new VeldridException($"The given resource ({resource}) is not mapped."); } info.RefCount -= 1; commitUnmap = info.RefCount == 0; if (commitUnmap) { lock (_immediateContextLock) { if (resource is D3D11Buffer buffer) { _immediateContext.UnmapSubresource(buffer.Buffer, 0); } else { D3D11Texture texture = Util.AssertSubtype <MappableResource, D3D11Texture>(resource); _immediateContext.UnmapSubresource(texture.DeviceTexture, (int)subresource); } bool result = _mappedResources.Remove(key); Debug.Assert(result); } } } }
/// <summary> /// Maps a <see cref="Buffer"/> or <see cref="Texture"/> into a CPU-accessible data region. /// </summary> /// <param name="resource">The <see cref="Buffer"/> or <see cref="Texture"/> resource to map.</param> /// <param name="mode">The <see cref="MapMode"/> to use.</param> /// <param name="subresource">The subresource to map. Subresources are indexed first by mip slice, then by array layer. /// For <see cref="Buffer"/> resources, this parameter must be 0.</param> /// <returns>A <see cref="MappedResource"/> structure describing the mapped data region.</returns> public MappedResource Map(MappableResource resource, MapMode mode, uint subresource) { #if VALIDATE_USAGE if (resource is Buffer buffer) { if ((buffer.Usage & BufferUsage.Dynamic) != BufferUsage.Dynamic && (buffer.Usage & BufferUsage.Staging) != BufferUsage.Staging) { throw new VeldridException("Buffers must have the Staging or Dynamic usage flag to be mapped."); } if (subresource != 0) { throw new VeldridException("Subresource must be 0 for Buffer resources."); } } else if (resource is Texture tex) { if ((tex.Usage & TextureUsage.Staging) == 0) { throw new VeldridException("Texture must have the Staging usage flag to be mapped."); } if (subresource >= tex.ArrayLayers * tex.MipLevels) { throw new VeldridException( "Subresource must be less than the number of subresources in the Texture being mapped."); } } #endif return(MapCore(resource, mode, subresource)); }
internal MappedResource(MappableResource resource, MapMode mode, IntPtr data, uint sizeInBytes) { Resource = resource; Mode = mode; Data = data; SizeInBytes = sizeInBytes; Subresource = 0; RowPitch = 0; DepthPitch = 0; }
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource) { if (resource is MTLBuffer buffer) { return(MapBuffer(buffer, mode)); } else { MTLTexture texture = Util.AssertSubtype <MappableResource, MTLTexture>(resource); return(MapTexture(texture, mode, subresource)); } }
internal MappedResource( MappableResource resource, MapMode mode, IntPtr data, uint sizeInBytes, uint subresource, uint rowPitch, uint depthPitch) { Resource = resource; Mode = mode; Data = data; SizeInBytes = sizeInBytes; Subresource = subresource; RowPitch = rowPitch; DepthPitch = depthPitch; }
public Vk3DMapProxy( FixedStagingBlock block, MappableResource resource, MapMode mode, uint subresource, uint rowPitch, uint depthPitch) { StagingBlock = block; MappedResource = new MappedResource( resource, mode, (IntPtr)block.Data, block.SizeInBytes, subresource, rowPitch, depthPitch); }
/// <summary> /// Maps a <see cref="DeviceBuffer"/> or <see cref="Texture"/> into a CPU-accessible data region. /// </summary> /// <param name="resource">The <see cref="DeviceBuffer"/> or <see cref="Texture"/> resource to map.</param> /// <param name="mode">The <see cref="MapMode"/> to use.</param> /// <param name="subresource">The subresource to map. Subresources are indexed first by mip slice, then by array layer. /// For <see cref="DeviceBuffer"/> resources, this parameter must be 0.</param> /// <returns>A <see cref="MappedResource"/> structure describing the mapped data region.</returns> public MappedResource Map(MappableResource resource, MapMode mode, uint subresource) { #if VALIDATE_USAGE if (resource is DeviceBuffer buffer) { if ((buffer.Usage & BufferUsage.Dynamic) != BufferUsage.Dynamic && (buffer.Usage & BufferUsage.Staging) != BufferUsage.Staging) { throw new VeldridException("Buffers must have the Staging or Dynamic usage flag to be mapped."); } if (subresource != 0) { throw new VeldridException("Subresource must be 0 for Buffer resources."); } if ((mode == MapMode.Read || mode == MapMode.ReadWrite) && (buffer.Usage & BufferUsage.Staging) == 0) { throw new VeldridException( $"{nameof(MapMode)}.{nameof(MapMode.Read)} and {nameof(MapMode)}.{nameof(MapMode.ReadWrite)} can only be used on buffers created with {nameof(BufferUsage)}.{nameof(BufferUsage.Staging)}."); } } else if (resource is Texture tex) { if ((tex.Usage & TextureUsage.Staging) == 0) { throw new VeldridException("Texture must have the Staging usage flag to be mapped."); } if (subresource >= tex.ArrayLayers * tex.MipLevels) { throw new VeldridException( "Subresource must be less than the number of subresources in the Texture being mapped."); } } #endif return(MapCore(resource, mode, subresource)); }
/// <summary> /// Invalidates a previously-mapped data region for the given <see cref="DeviceBuffer"/> or <see cref="Texture"/>. /// For <see cref="Texture"/> resources, this unmaps the first subresource. /// </summary> /// <param name="resource">The resource to unmap.</param> public void Unmap(MappableResource resource) => Unmap(resource, 0);
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource) { MappedResourceCacheKey key = new MappedResourceCacheKey(resource, subresource); lock (_mappedResourceLock) { if (_mappedResources.TryGetValue(key, out MappedResourceInfo info)) { if (info.Mode != mode) { throw new VeldridException("The given resource was already mapped with a different MapMode."); } info.RefCount += 1; _mappedResources[key] = info; } else { // No current mapping exists -- create one. if (resource is D3D11Buffer buffer) { lock (_immediateContextLock) { DataBox db = _immediateContext.MapSubresource( buffer.Buffer, 0, D3D11Formats.VdToD3D11MapMode((buffer.Usage & BufferUsage.Dynamic) == BufferUsage.Dynamic, mode), SharpDX.Direct3D11.MapFlags.None); info.MappedResource = new MappedResource(resource, mode, db.DataPointer, buffer.SizeInBytes); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } else { D3D11Texture texture = Util.AssertSubtype <MappableResource, D3D11Texture>(resource); lock (_immediateContextLock) { DataBox db = _immediateContext.MapSubresource( texture.DeviceTexture, (int)subresource, D3D11Formats.VdToD3D11MapMode(false, mode), SharpDX.Direct3D11.MapFlags.None, out DataStream ds); info.MappedResource = new MappedResource( resource, mode, db.DataPointer, (uint)ds.Length, subresource, (uint)db.RowPitch, (uint)db.SlicePitch); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } } return(info.MappedResource); } }
protected override void UnmapCore(MappableResource resource, uint subresource) { }
/// <summary> /// </summary> /// <param name="resource"></param> /// <param name="subresource"></param> protected abstract void UnmapCore(MappableResource resource, uint subresource);
/// <summary> /// Invalidates a previously-mapped data region for the given <see cref="DeviceBuffer"/> or <see cref="Texture"/>. /// </summary> /// <param name="resource">The resource to unmap.</param> /// <param name="subresource">The subresource to unmap. Subresources are indexed first by mip slice, then by array layer. /// For <see cref="DeviceBuffer"/> resources, this parameter must be 0.</param> public void Unmap(MappableResource resource, uint subresource) { UnmapCore(resource, subresource); }
/// <summary> /// Maps a <see cref="DeviceBuffer"/> or <see cref="Texture"/> into a CPU-accessible data region, and returns a structured /// view over that region. /// </summary> /// <param name="resource">The <see cref="DeviceBuffer"/> or <see cref="Texture"/> resource to map.</param> /// <param name="mode">The <see cref="MapMode"/> to use.</param> /// <param name="subresource">The subresource to map. Subresources are indexed first by mip slice, then by array layer.</param> /// <typeparam name="T">The blittable value type which mapped data is viewed as.</typeparam> /// <returns>A <see cref="MappedResource"/> structure describing the mapped data region.</returns> public MappedResourceView <T> Map <T>(MappableResource resource, MapMode mode, uint subresource) where T : struct { MappedResource mappedResource = Map(resource, mode, subresource); return(new MappedResourceView <T>(mappedResource)); }
/// <summary> /// Maps a <see cref="DeviceBuffer"/> or <see cref="Texture"/> into a CPU-accessible data region, and returns a structured /// view over that region. For Texture resources, this overload maps the first subresource. /// </summary> /// <param name="resource">The <see cref="DeviceBuffer"/> or <see cref="Texture"/> resource to map.</param> /// <param name="mode">The <see cref="MapMode"/> to use.</param> /// <typeparam name="T">The blittable value type which mapped data is viewed as.</typeparam> /// <returns>A <see cref="MappedResource"/> structure describing the mapped data region.</returns> public MappedResourceView <T> Map <T>(MappableResource resource, MapMode mode) where T : struct => Map <T>(resource, mode, 0);
/// <summary> /// </summary> /// <param name="resource"></param> /// <param name="mode"></param> /// <param name="subresource"></param> /// <returns></returns> protected abstract MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource);
/// <summary> /// Maps a <see cref="DeviceBuffer"/> or <see cref="Texture"/> into a CPU-accessible data region. For Texture resources, this /// overload maps the first subresource. /// </summary> /// <param name="resource">The <see cref="DeviceBuffer"/> or <see cref="Texture"/> resource to map.</param> /// <param name="mode">The <see cref="MapMode"/> to use.</param> /// <returns>A <see cref="MappedResource"/> structure describing the mapped data region.</returns> public MappedResource Map(MappableResource resource, MapMode mode) => Map(resource, mode, 0);
public MappedResourceCacheKey(MappableResource resource, uint subresource) { Resource = resource; Subresource = subresource; }
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource) { MappedResourceCacheKey key = new MappedResourceCacheKey(resource, subresource); lock (_mappedResourceLock) { if (_mappedResources.TryGetValue(key, out MappedResourceInfo info)) { if (info.Mode != mode) { throw new VeldridException("The given resource was already mapped with a different MapMode."); } info.RefCount += 1; _mappedResources[key] = info; } else { // No current mapping exists -- create one. if (resource is D3D11Buffer buffer) { lock (_immediateContextLock) { MappedSubresource msr = _immediateContext.Map( buffer.Buffer, 0, D3D11Formats.VdToD3D11MapMode((buffer.Usage & BufferUsage.Dynamic) == BufferUsage.Dynamic, mode), Vortice.Direct3D11.MapFlags.None); info.MappedResource = new MappedResource(resource, mode, msr.DataPointer, buffer.SizeInBytes); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } else { D3D11Texture texture = Util.AssertSubtype <MappableResource, D3D11Texture>(resource); lock (_immediateContextLock) { Util.GetMipLevelAndArrayLayer(texture, subresource, out uint mipLevel, out uint arrayLayer); MappedSubresource msr = _immediateContext.Map( texture.DeviceTexture, (int)mipLevel, (int)arrayLayer, D3D11Formats.VdToD3D11MapMode(false, mode), Vortice.Direct3D11.MapFlags.None, out int mipSize); info.MappedResource = new MappedResource( resource, mode, msr.DataPointer, texture.Height * (uint)msr.RowPitch, subresource, (uint)msr.RowPitch, (uint)msr.DepthPitch); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } } return(info.MappedResource); } }