void QCLVectorBase::create (QCLContext *context, int size, QCLMemoryObject::Access access) { release(); d_ptr = new QCLVectorBasePrivate(); Q_CHECK_PTR(d_ptr); d_ptr->owners.append(this); cl_int error; cl_mem id = clCreateBuffer (context->contextId(), #ifndef QT_CL_COPY_VECTOR cl_mem_flags(access) | CL_MEM_ALLOC_HOST_PTR, #else cl_mem_flags(access), #endif size * m_elemSize, 0, &error); context->reportError("QCLVector<T>::create:", error); if (id) { d_ptr->context = context; d_ptr->id = id; d_ptr->state = State_Uninitialized; d_ptr->hostCopy = 0; m_size = size; } }
/*! Creates a 2D OpenCL image object from \a image with the specified \a access mode. The OpenCL image is initialized with a copy of the contents of \a image. The application's \a image can be discarded after the OpenCL image is created. Returns the new 2D OpenCL image object, or a null object if the image could not be created. If \a image has a zero size, this function will return a null QCLImage2D object. \sa createImage2DDevice(), createImage2DHost() */ QCLImage2D QCLContext::createImage2DCopy (const QImage &image, QCLMemoryObject::Access access) { Q_D(QCLContext); // Validate the image. if (image.width() < 1 || image.height() < 1) return QCLImage2D(); QCLImageFormat format(image.format()); if (format.isNull()) { qWarning("QCLContext::createImage2DCopy: QImage format %d " "does not have an OpenCL equivalent", int(image.format())); return QCLImage2D(); } // Create the image object. cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access) | CL_MEM_COPY_HOST_PTR; cl_mem mem = clCreateImage2D (d->id, flags, &(format.m_format), image.width(), image.height(), image.bytesPerLine(), const_cast<uchar *>(image.bits()), &error); reportError("QCLContext::createImage2DCopy:", error); if (mem) return QCLImage2D(this, mem); else return QCLImage2D(); }
/*! Creates a 2D OpenCL image object from \a image with the specified \a access mode. OpenCL kernels that access the image will read and write the QImage contents directly. Returns the new 2D OpenCL image object, or a null object if the image could not be created. If \a image is null or has a zero size, this function will return a null QCLImage2D object. \sa createImage2DDevice(), createImage2DCopy() */ QCLImage2D QCLContext::createImage2DHost(QImage *image, QCLMemoryObject::Access access) { Q_D(QCLContext); // Validate the image. if (!image || image->width() < 1 || image->height() < 1) return QCLImage2D(); QCLImageFormat format(image->format()); if (format.isNull()) { qWarning("QCLContext::createImage2DHost: QImage format %d " "does not have an OpenCL equivalent", int(image->format())); return QCLImage2D(); } // Create the image object. cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access) | CL_MEM_USE_HOST_PTR; cl_mem mem = clCreateImage2D (d->id, flags, &(format.m_format), image->width(), image->height(), image->bytesPerLine(), image->bits(), &error); reportError("QCLContext::createImage2DHost:", error); if (mem) return QCLImage2D(this, mem, format); else return QCLImage2D(); }
Buffer::Buffer(const Context& context, BufferKernelAccess kernelAccess, BufferStorage storage, void* data, size_t size) { cl_int errNum; cl_mem_flags flags = cl_mem_flags(kernelAccess) | cl_mem_flags(storage); _handle = clCreateBuffer(context.getHandle(), flags, size, data, &errNum); if (errNum != CL_SUCCESS) throw OpenCLException(errNum); }
/*! Creates an OpenCL memory buffer of \a size bytes in length, with the specified \a access mode. The memory is created on the device and will not be accessible to the host via a direct pointer. Use createBufferHost() to create a host-accessible buffer. Returns the new OpenCL memory buffer object, or a null object if the buffer could not be created. \sa createBufferHost(), createBufferCopy(), createVector() */ QCLBuffer QCLContext::createBufferDevice(size_t size, QCLMemoryObject::Access access) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); cl_mem mem = clCreateBuffer(d->id, flags, size, 0, &error); reportError("QCLContext::createBufferDevice:", error); if (mem) return QCLBuffer(this, mem); else return QCLBuffer(); }
/*! Creates a 2D OpenCL image object with the specified \a format, \a size, and \a access mode. The image memory is created on the device and will not be accessible to the host via a direct pointer. Use createImage2DHost() to create a host-accessible image. Returns the new 2D OpenCL image object, or a null object if the image could not be created. \sa createImage2DHost(), createImage2DCopy() */ QCLImage2D QCLContext::createImage2DDevice (const QCLImageFormat &format, const QSize &size, QCLMemoryObject::Access access) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); cl_mem mem = clCreateImage2D (d->id, flags, &(format.m_format), size.width(), size.height(), 0, 0, &error); reportError("QCLContext::createImage2DDevice:", error); if (mem) return QCLImage2D(this, mem, format); else return QCLImage2D(); }
/*! Creates a 3D OpenCL image object with the specified \a format, \a width, \a height, \a depth, and \a access mode. The image memory is created on the device and will not be accessible to the host via a direct pointer. Use createImage3DHost() to create a host-accessible image. Returns the new 3D OpenCL image object, or a null object if the image could not be created. \sa createImage3DHost(), createImage3DCopy() */ QCLImage3D QCLContext::createImage3DDevice (const QCLImageFormat &format, int width, int height, int depth, QCLMemoryObject::Access access) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); cl_mem mem = clCreateImage3D (d->id, flags, &(format.m_format), width, height, depth, 0, 0, 0, &error); reportError("QCLContext::createImage3DDevice:", error); if (mem) return QCLImage3D(this, mem); else return QCLImage3D(); }
/*! Creates an OpenCL memory buffer of \a size bytes in length, with the specified \a access mode. The buffer is initialized with a copy of the contents of \a data. The application's \a data can be discarded after the buffer is created. Returns the new OpenCL memory buffer object, or a null object if the buffer could not be created. \sa createBufferDevice(), createBufferHost(), createVector() */ QCLBuffer QCLContext::createBufferCopy (const void *data, size_t size, QCLMemoryObject::Access access) { Q_ASSERT(data); Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); flags |= CL_MEM_COPY_HOST_PTR; cl_mem mem = clCreateBuffer (d->id, flags, size, const_cast<void *>(data), &error); reportError("QCLContext::createBufferCopy:", error); if (mem) return QCLBuffer(this, mem); else return QCLBuffer(); }
Buffer::Buffer(const Context& context, BufferKernelAccess kernelAccess, size_t size) { cl_int errNum; cl_mem_flags flags = cl_mem_flags(kernelAccess); _handle = clCreateBuffer(context.getHandle(), flags, size, nullptr, &errNum); if (errNum != CL_SUCCESS) throw OpenCLException(errNum); }
/*! Creates a 2D OpenCL image object with the specified \a format, \a size, and \a access mode. If \a bytesPerLine is not zero, it indicates the number of bytes between lines in \a data. The image is initialized with a copy of the contents of \a data. The application's \a data can be discarded after the image is created. Returns the new 2D OpenCL image object, or a null object if the image could not be created. \sa createImage2DDevice(), createImage2DHost() */ QCLImage2D QCLContext::createImage2DCopy (const QCLImageFormat &format, const void *data, const QSize &size, QCLMemoryObject::Access access, int bytesPerLine) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access) | CL_MEM_COPY_HOST_PTR; cl_mem mem = clCreateImage2D (d->id, flags, &(format.m_format), size.width(), size.height(), bytesPerLine, const_cast<void *>(data), &error); reportError("QCLContext::createImage2DCopy:", error); if (mem) return QCLImage2D(this, mem); else return QCLImage2D(); }
/*! Creates an OpenCL memory buffer of \a size bytes in length, with the specified \a access mode. If \a data is not null, then it will be used as the storage for the buffer. If \a data is null, then a new block of host-accessible memory will be allocated. Returns the new OpenCL memory buffer object, or a null object if the buffer could not be created. \sa createBufferDevice(), createBufferCopy(), createVector() */ QCLBuffer QCLContext::createBufferHost (void *data, size_t size, QCLMemoryObject::Access access) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); if (data) flags |= CL_MEM_USE_HOST_PTR; else flags |= CL_MEM_ALLOC_HOST_PTR; cl_mem mem = clCreateBuffer(d->id, flags, size, data, &error); reportError("QCLContext::createBufferHost:", error); if (mem) return QCLBuffer(this, mem); else return QCLBuffer(); }
/*! Creates an OpenCL memory buffer from the OpenGL buffer object \a bufobj, with the specified \a access mode. This function will only work if supportsObjectSharing() is true. */ QCLBuffer QCLContextGL::createGLBuffer(GLuint bufobj, QCLMemoryObject::Access access) { #ifndef QT_NO_CL_OPENGL cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); cl_mem mem = clCreateFromGLBuffer (contextId(), flags, bufobj, &error); reportError("QCLContextGL::createGLBuffer:", error); if (mem) return QCLBuffer(this, mem); else return QCLBuffer(); #else Q_UNUSED(bufobj); Q_UNUSED(access); reportError("QCLContextGL::createGLBuffer:", CL_INVALID_VALUE); return QCLBuffer(); #endif }
/*! Creates a new buffer that refers to the \a size bytes, starting at \a offset within this buffer. The data in the new buffer will be accessed according to \a access. Sub-buffers are an OpenCL 1.1 feature. On OpenCL 1.0, this function will return a null buffer. \sa parentBuffer(), offset() */ QCLBuffer QCLBuffer::createSubBuffer (size_t offset, size_t size, QCLMemoryObject::Access access) { #ifdef QT_OPENCL_1_1 cl_int error; cl_buffer_region region; region.origin = offset; region.size = size; cl_mem mem = clCreateSubBuffer (memoryId(), cl_mem_flags(access), CL_BUFFER_CREATE_TYPE_REGION, ®ion, &error); context()->reportError("QCLBuffer::createSubBuffer:", error); return QCLBuffer(context(), mem); #else Q_UNUSED(offset); Q_UNUSED(size); Q_UNUSED(access); return QCLBuffer(); #endif }
/*! Creates a 2D OpenCL image object with the specified \a format, \a size, and \a access mode. If \a bytesPerLine is not zero, it indicates the number of bytes between lines in \a data. If \a data is not null, then it will be used as the storage for the image. If \a data is null, then a new block of host-accessible memory will be allocated. Returns the new 2D OpenCL image object, or a null object if the image could not be created. \sa createImage2DDevice(), createImage2DCopy() */ QCLImage2D QCLContext::createImage2DHost (const QCLImageFormat &format, void *data, const QSize &size, QCLMemoryObject::Access access, int bytesPerLine) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); if (data) flags |= CL_MEM_USE_HOST_PTR; else flags |= CL_MEM_ALLOC_HOST_PTR; cl_mem mem = clCreateImage2D (d->id, flags, &(format.m_format), size.width(), size.height(), bytesPerLine, data, &error); reportError("QCLContext::createImage2DHost:", error); if (mem) return QCLImage2D(this, mem, format); else return QCLImage2D(); }
/*! Creates a 3D OpenCL image object with the specified \a format, \a width, \a height, \a depth, and \a access mode. If \a bytesPerLine is not zero, it indicates the number of bytes between lines in \a data. If \a bytesPerSlice is not zero, it indicates the number of bytes between slices in \a data. If \a data is not null, then it will be used as the storage for the image. If \a data is null, then a new block of host-accessible memory will be allocated. Returns the new 3D OpenCL image object, or a null object if the image could not be created. \sa createImage3DDevice(), createImage3DCopy() */ QCLImage3D QCLContext::createImage3DHost (const QCLImageFormat &format, void *data, int width, int height, int depth, QCLMemoryObject::Access access, int bytesPerLine, int bytesPerSlice) { Q_D(QCLContext); cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); if (data) flags |= CL_MEM_USE_HOST_PTR; else flags |= CL_MEM_ALLOC_HOST_PTR; cl_mem mem = clCreateImage3D (d->id, flags, &(format.m_format), width, height, depth, bytesPerLine, bytesPerSlice, data, &error); reportError("QCLContext::createImage3DHost:", error); if (mem) return QCLImage3D(this, mem); else return QCLImage3D(); }
/*! Creates a 3D OpenCL image object from the specified \a mipmapLevel, OpenGL \a texture object, and \a access mode. The \a type must be \c{GL_TEXTURE_3D}. The \a texture does not need to be bound to an OpenGL texture target. This function will only work if supportsObjectSharing() is true. \sa createTexture2D() */ QCLImage3D QCLContextGL::createTexture3D (GLenum type, GLuint texture, GLint mipmapLevel, QCLMemoryObject::Access access) { #ifndef QT_NO_CL_OPENGL cl_int error = CL_INVALID_CONTEXT; cl_mem_flags flags = cl_mem_flags(access); cl_mem mem = clCreateFromGLTexture3D (contextId(), flags, type, mipmapLevel, texture, &error); reportError("QCLContextGL::createGLTexture3D:", error); if (mem) return QCLImage3D(this, mem); else return QCLImage3D(); #else Q_UNUSED(type); Q_UNUSED(texture); Q_UNUSED(mipmapLevel); Q_UNUSED(access); reportError("QCLContextGL::createGLTexture3D:", CL_INVALID_VALUE); return QCLImage3D(); #endif }