Exemple #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>(Memory.MemoryFlag memoryFlags, T[] value) where T : struct
        {
            return(CreateBuffer(memoryFlags, typeof(T), Array.ConvertAll(value, x => (object)x)));
            //// 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
            //    Result result;
            //    IntPtr memoryBufferPointer = MemoryNativeApi.CreateBuffer(this.Handle, (Interop.Memory.MemoryFlag)memoryFlags, new UIntPtr((uint)size), hostBufferPointer, out 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);
            //}
            //finally
            //{
            //    // Deallocates the host memory allocated for the value
            //    if (hostBufferPointer != IntPtr.Zero)
            //        Marshal.FreeHGlobal(hostBufferPointer);
            //}
        }
Exemple #2
0
        /// <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(Memory.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));
        }
Exemple #3
0
        public MemoryBuffer CreateBuffer(Memory.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);
                }
            }
        }
Exemple #4
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>(Memory.MemoryFlag memoryFlags, T[] value, object handleIdentifier)
     where T : struct
 {
     return(CreateBuffer(memoryFlags, typeof(T), Array.ConvertAll(value, x => (object)x), handleIdentifier));
 }
Exemple #5
0
 /// <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>(Memory.MemoryFlag memoryFlags, int size, object handleIdentifier)
     where T : struct
 {
     return(CreateBuffer(memoryFlags, Marshal.SizeOf <T>() * size, handleIdentifier));
 }
Exemple #6
0
        //public MemoryBuffer CreateFromGLTexture2D(Memory.MemoryFlag memoryFlags,uint gltex_target, int miplevel, uint gltex)
        //{
        //    // Creates a new memory buffer of the specified size and with the specified memory flags
        //    Result result;
        //    IntPtr memoryBufferPointer = ContextsNativeApi.CreateFromGLTexture2D(this.Handle, (Interop.Memory.MemoryFlag)memoryFlags, gltex_target, miplevel, gltex, out 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);
        //}

        /// <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>(Memory.MemoryFlag memoryFlags, int size) where T : struct
        {
            return(CreateBuffer(memoryFlags, Marshal.SizeOf <T>() * size));
        }