public override MTLRenderPassDescriptor CreateRenderPassDescriptor() { MTLRenderPassDescriptor ret = MTLRenderPassDescriptor.New(); for (int i = 0; i < ColorTargets.Count; i++) { FramebufferAttachment colorTarget = ColorTargets[i]; MTLTexture mtlTarget = Util.AssertSubtype <Texture, MTLTexture>(colorTarget.Target); MTLRenderPassColorAttachmentDescriptor colorDescriptor = ret.colorAttachments[(uint)i]; colorDescriptor.texture = mtlTarget.DeviceTexture; colorDescriptor.loadAction = MTLLoadAction.Load; colorDescriptor.slice = (UIntPtr)colorTarget.ArrayLayer; } if (DepthTarget != null) { MTLTexture mtlDepthTarget = Util.AssertSubtype <Texture, MTLTexture>(DepthTarget.Value.Target); MTLRenderPassDepthAttachmentDescriptor depthDescriptor = ret.depthAttachment; depthDescriptor.loadAction = MTLLoadAction.Load; depthDescriptor.storeAction = MTLStoreAction.Store; depthDescriptor.texture = mtlDepthTarget.DeviceTexture; depthDescriptor.slice = (UIntPtr)DepthTarget.Value.ArrayLayer; if (FormatHelpers.IsStencilFormat(mtlDepthTarget.Format)) { MTLRenderPassStencilAttachmentDescriptor stencilDescriptor = ret.stencilAttachment; stencilDescriptor.loadAction = MTLLoadAction.Load; stencilDescriptor.storeAction = MTLStoreAction.Store; stencilDescriptor.texture = mtlDepthTarget.DeviceTexture; stencilDescriptor.slice = (UIntPtr)DepthTarget.Value.ArrayLayer; } } return(ret); }
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)); } }
private void RecreateDepthTexture(uint width, uint height) { Debug.Assert(_depthFormat.HasValue); if (_depthTexture != null) { _depthTexture.Dispose(); } _depthTexture = Util.AssertSubtype <Texture, MTLTexture>( _gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D( width, height, 1, 1, _depthFormat.Value, TextureUsage.DepthStencil))); }
private protected override void UpdateTextureCore( Texture texture, IntPtr source, uint sizeInBytes, uint x, uint y, uint z, uint width, uint height, uint depth, uint mipLevel, uint arrayLayer) { MTLTexture mtlTex = Util.AssertSubtype <Texture, MTLTexture>(texture); if (mtlTex.StagingBuffer.IsNull) { Texture stagingTex = ResourceFactory.CreateTexture(new TextureDescription( width, height, depth, 1, 1, texture.Format, TextureUsage.Staging, texture.Type)); UpdateTexture(stagingTex, source, sizeInBytes, 0, 0, 0, width, height, depth, 0, 0); CommandList cl = ResourceFactory.CreateCommandList(); cl.Begin(); cl.CopyTexture( stagingTex, 0, 0, 0, 0, 0, texture, x, y, z, mipLevel, arrayLayer, width, height, depth, 1); cl.End(); SubmitCommands(cl); cl.Dispose(); stagingTex.Dispose(); } else { mtlTex.GetSubresourceLayout(mipLevel, arrayLayer, out uint dstRowPitch, out uint dstDepthPitch); ulong dstOffset = Util.ComputeSubresourceOffset(mtlTex, mipLevel, arrayLayer); uint srcRowPitch = FormatHelpers.GetRowPitch(width, texture.Format); uint srcDepthPitch = FormatHelpers.GetDepthPitch(srcRowPitch, height, texture.Format); Util.CopyTextureRegion( source.ToPointer(), 0, 0, 0, srcRowPitch, srcDepthPitch, (byte *)mtlTex.StagingBuffer.contents() + dstOffset, x, y, z, dstRowPitch, dstDepthPitch, width, height, depth, texture.Format); } }
private MappedResource MapTexture(MTLTexture texture, MapMode mode, uint subresource) { Debug.Assert(!texture.StagingBuffer.IsNull); void *data = texture.StagingBuffer.contents(); Util.GetMipLevelAndArrayLayer(texture, subresource, out uint mipLevel, out uint arrayLayer); Util.GetMipDimensions(texture, mipLevel, out uint width, out uint height, out uint depth); uint subresourceSize = texture.GetSubresourceSize(mipLevel, arrayLayer); texture.GetSubresourceLayout(mipLevel, arrayLayer, out uint rowPitch, out uint depthPitch); ulong offset = Util.ComputeSubresourceOffset(texture, mipLevel, arrayLayer); byte *offsetPtr = (byte *)data + offset; return(new MappedResource(texture, mode, (IntPtr)offsetPtr, subresourceSize, subresource, rowPitch, depthPitch)); }
public MTLTextureView(ref TextureViewDescription description, MTLGraphicsDevice gd) : base(ref description) { MTLTexture targetMTLTexture = Util.AssertSubtype <Texture, MTLTexture>(description.Target); if (BaseMipLevel != 0 || MipLevels != Target.MipLevels || BaseArrayLayer != 0 || ArrayLayers != Target.ArrayLayers) { _hasTextureView = true; TargetDeviceTexture = targetMTLTexture.DeviceTexture.newTextureView( targetMTLTexture.MTLPixelFormat, targetMTLTexture.MTLTextureType, new NSRange(BaseMipLevel, MipLevels), new NSRange(BaseArrayLayer, ArrayLayers)); } else { TargetDeviceTexture = targetMTLTexture.DeviceTexture; } }