예제 #1
0
 /// <summary>
 ///     Creates a new memory buffer with the specified flags for the specified array. The size of memory 1allocated for the
 ///     memory buffer is determined by <see cref="T" /> and the number of elements in the array.
 /// </summary>
 /// <typeparam name="T">The size of the memory buffer will be determined by the structure specified in the type parameter.</typeparam>
 /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param>
 /// <param name="value">The value that is to be copied over to the device.</param>
 /// <exception cref="OpenClException">
 ///     If the memory buffer could not be created, then an <see cref="OpenClException" /> is
 ///     thrown.
 /// </exception>
 /// <returns>Returns the created memory buffer.</returns>
 public MemoryBuffer CreateBuffer <T>(MemoryFlag memoryFlags, T[] value, object handleIdentifier)
     where T : struct
 {
     return(CreateBuffer(
                memoryFlags,
                typeof(T),
                Array.ConvertAll(value, x => ( object )x),
                handleIdentifier
                ));
 }
예제 #2
0
        public MemoryBuffer CreateBuffer(MemoryFlag memoryFlags, Type t, object[] value, object handleIdentifier)
        {
            // Tries to create the memory buffer, if anything goes wrong, then it is crucial to free the allocated memory
            IntPtr hostBufferPointer = IntPtr.Zero;

            try
            {
                // Determines the size of the specified value and creates a pointer that points to the data inside the structure
                int size = Marshal.SizeOf(t) * value.Length;
                hostBufferPointer = Marshal.AllocHGlobal(size);

                for (int i = 0; i < value.Length; i++)
                {
                    Marshal.StructureToPtr(value[i], IntPtr.Add(hostBufferPointer, i * Marshal.SizeOf(t)), false);
                }

                // Creates a new memory buffer for the specified value
                IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(
                    Handle,
                    (Interop.Memory.MemoryFlag)memoryFlags,
                    new UIntPtr(( uint )size),
                    hostBufferPointer,
                    out Result result
                    );

                // Checks if the creation of the memory buffer was successful, if not, then an exception is thrown
                if (result != Result.Success)
                {
                    throw new OpenClException("The memory buffer could not be created.", result);
                }

                // Creates the memory buffer from the pointer to the memory buffer and returns it
                return(new MemoryBuffer(memoryBufferPointer, handleIdentifier));
            }
            finally
            {
                // Deallocates the host memory allocated for the value
                if (hostBufferPointer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(hostBufferPointer);
                }
            }
        }
예제 #3
0
파일: Context.cs 프로젝트: Open-FL/OpenCL
        /// <summary>
        ///     Creates a new memory buffer with the specified flags and of the specified size.
        /// </summary>
        /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param>
        /// <param name="size">The size of memory that should be allocated for the memory buffer.</param>
        /// <exception cref="OpenClException">
        ///     If the memory buffer could not be created, then an <see cref="OpenClException" /> is
        ///     thrown.
        /// </exception>
        /// <returns>Returns the created memory buffer.</returns>
        public MemoryBuffer CreateBuffer(MemoryFlag memoryFlags, int size, object handleIdentifier)
        {
            // Creates a new memory buffer of the specified size and with the specified memory flags
            IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(
                Handle,
                (Interop.Memory.MemoryFlag)memoryFlags,
                new UIntPtr((uint)size),
                IntPtr.Zero,
                out Result result
                );

            // Checks if the creation of the memory buffer was successful, if not, then an exception is thrown
            if (result != Result.Success)
            {
                throw new OpenClException("The memory buffer could not be created.", result);
            }

            // Creates the memory buffer from the pointer to the memory buffer and returns it
            return(new MemoryBuffer(memoryBufferPointer, handleIdentifier));
        }
예제 #4
0
파일: Context.cs 프로젝트: Open-FL/OpenCL
 /// <summary>
 ///     Creates a new memory buffer with the specified flags. The size of memory allocated for the memory buffer is
 ///     determined by <see cref="T" /> and the number of elements.
 /// </summary>
 /// <typeparam name="T">The size of the memory buffer will be determined by the structure specified in the type parameter.</typeparam>
 /// <param name="memoryFlags">The flags, that determines the how the memory buffer is created and how it can be accessed.</param>
 /// <exception cref="OpenClException">
 ///     If the memory buffer could not be created, then an <see cref="OpenClException" /> is
 ///     thrown.
 /// </exception>
 /// <returns>Returns the created memory buffer.</returns>
 public MemoryBuffer CreateBuffer <T>(MemoryFlag memoryFlags, int size, object handleIdentifier)
     where T : struct
 {
     return(CreateBuffer(memoryFlags, Marshal.SizeOf <T>() * size, handleIdentifier));
 }