/// <summary cref="DirectXBuffer{T}.Dispose(bool)"/> protected override void Dispose(bool disposing) { base.Dispose(disposing); if (cudaGraphicsResource == IntPtr.Zero) { return; } CudaException.ThrowIfFailed( CudaNativeMethods.cuGraphicsUnregisterResource( cudaGraphicsResource)); cudaGraphicsResource = IntPtr.Zero; }
/// <summary> /// Registers the resource with the given flags in the scope of the Cuda runtime system. /// </summary> /// <param name="resource">The resource to register.</param> /// <param name="viewFlags">The view flags to use.</param> /// <param name="cudaGraphicsResource">The resulting graphics resource.</param> internal static void RegisterResource( Resource resource, DirectXViewFlags viewFlags, out IntPtr cudaGraphicsResource) { CudaException.ThrowIfFailed( CudaNativeMethods.cuGraphicsD3D11RegisterResource( out cudaGraphicsResource, resource.NativePointer, CudaGraphicsRegisterFlags.None)); CudaException.ThrowIfFailed( CudaNativeMethods.cuGraphicsResourceSetMapFlags( cudaGraphicsResource, (CudaGraphicsMapFlags)viewFlags)); }
/// <summary cref="DirectXBuffer.OnMap(DeviceContext)"/> protected override unsafe IntPtr OnMap(DeviceContext context) { Debug.Assert(cudaArray == IntPtr.Zero); CudaException.ThrowIfFailed( CudaNativeMethods.cuGraphicsSubResourceGetMappedArray( out cudaArray, cudaGraphicsResource, 0, 0)); Debug.Assert(cudaArray != IntPtr.Zero); if (buffer == null) { CudaException.ThrowIfFailed( CudaNativeMethods.cuArrayGetDescriptor(out desc, cudaArray)); pixelByteSize = CudaNativeMethods.GetByteSize(desc.arrayFormat) * desc.numChannels; buffer = Accelerator.Allocate <byte>( desc.width.ToInt32() * desc.height.ToInt32() * pixelByteSize); } Debug.Assert(pixelByteSize > 0); if (ViewFlags != DirectXViewFlags.WriteDiscard) { // Copy texture data to buffer var args = new CudaMemcpy2DArgs() { dstDevice = buffer.NativePtr, dstMemoryType = CudaMemoryType.Device, srcArray = cudaArray, srcMemoryType = CudaMemoryType.Array, WidthInBytes = new IntPtr(desc.width.ToInt32() * pixelByteSize), Height = desc.height, }; CudaException.ThrowIfFailed( CudaNativeMethods.cuMemcpy2D(ref args)); } return(buffer.NativePtr); }
/// <summary cref="DirectXInteropAccelerator.UnmapBuffers(DeviceContext, DirectXBuffer[])"/> internal protected override unsafe void UnmapBuffers( DeviceContext context, DirectXBuffer[] buffers) { IntPtr *cudaResources = stackalloc IntPtr[buffers.Length]; for (int i = 0, e = buffers.Length; i < e; ++i) { var cudaBuffer = buffers[i] as ICudaDirectXBuffer; Debug.Assert(cudaBuffer != null, "Invalid Cuda buffer"); cudaResources[i] = cudaBuffer.CudaGraphicsResource; } CudaException.ThrowIfFailed( CudaNativeMethods.cuGraphicsUnmapResources( buffers.Length, cudaResources, IntPtr.Zero)); }
/// <summary cref="DirectXBuffer.OnUnmap(DeviceContext)"/> protected override unsafe void OnUnmap(DeviceContext context) { Debug.Assert(pixelByteSize > 0); if (ViewFlags != DirectXViewFlags.ReadOnly) { // Copy buffer data to texture var args = new CudaMemcpy2DArgs() { srcDevice = buffer.NativePtr, srcMemoryType = CudaMemoryType.Device, dstArray = cudaArray, dstMemoryType = CudaMemoryType.Array, WidthInBytes = new IntPtr(desc.width.ToInt32() * pixelByteSize), Height = desc.height, }; CudaException.ThrowIfFailed( CudaNativeMethods.cuMemcpy2D(ref args)); } cudaArray = IntPtr.Zero; }