Search in sources :

Example 6 with PointerBuffer

use of org.lwjgl.PointerBuffer in project lwjgl by LWJGL.

the class HelloOpenCL method execute.

protected static void execute() {
    try {
        CL.create();
        final List<CLPlatform> platforms = CLPlatform.getPlatforms();
        if (platforms == null)
            throw new RuntimeException("No OpenCL platforms found.");
        for (CLPlatform platform : platforms) {
            System.out.println("\n-------------------------");
            System.out.println("NEW PLATFORM: " + platform.getPointer());
            System.out.println(CLCapabilities.getPlatformCapabilities(platform));
            System.out.println("-------------------------");
            printPlatformInfo(platform, "CL_PLATFORM_PROFILE", CL_PLATFORM_PROFILE);
            printPlatformInfo(platform, "CL_PLATFORM_VERSION", CL_PLATFORM_VERSION);
            printPlatformInfo(platform, "CL_PLATFORM_NAME", CL_PLATFORM_NAME);
            printPlatformInfo(platform, "CL_PLATFORM_VENDOR", CL_PLATFORM_VENDOR);
            printPlatformInfo(platform, "CL_PLATFORM_EXTENSIONS", CL_PLATFORM_EXTENSIONS);
            System.out.println("");
            final PointerBuffer ctxProps = BufferUtils.createPointerBuffer(3);
            ctxProps.put(CL_CONTEXT_PLATFORM).put(platform.getPointer()).put(0).flip();
            final List<CLDevice> devices = platform.getDevices(CL_DEVICE_TYPE_ALL);
            for (CLDevice device : devices) {
                final CLDeviceCapabilities caps = CLCapabilities.getDeviceCapabilities(device);
                System.out.println("\n\tNEW DEVICE: " + device.getPointer());
                System.out.println(caps);
                System.out.println("\t-------------------------");
                System.out.println("\tCL_DEVICE_TYPE = " + device.getInfoLong(CL_DEVICE_TYPE));
                System.out.println("\tCL_DEVICE_VENDOR_ID = " + device.getInfoInt(CL_DEVICE_VENDOR_ID));
                System.out.println("\tCL_DEVICE_MAX_COMPUTE_UNITS = " + device.getInfoInt(CL_DEVICE_MAX_COMPUTE_UNITS));
                System.out.println("\tCL_DEVICE_MAX_WORK_ITEM_DIMENSIONS = " + device.getInfoInt(CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS));
                //CL10.clGetDeviceInfo(device, CL10.CL_DEVICE_MAX_WORK_ITEM_SIZES, info, size_ret);
                //System.out.println("\tCL_DEVICE_MAX_WORK_ITEM_SIZES = " + info.getInt(0));
                System.out.println("\tCL_DEVICE_MAX_WORK_GROUP_SIZE = " + device.getInfoSize(CL_DEVICE_MAX_WORK_GROUP_SIZE));
                System.out.println("\tCL_DEVICE_MAX_CLOCK_FREQUENCY = " + device.getInfoInt(CL_DEVICE_MAX_CLOCK_FREQUENCY));
                System.out.println("\tCL_DEVICE_ADDRESS_BITS = " + device.getInfoInt(CL_DEVICE_ADDRESS_BITS));
                System.out.println("\tCL_DEVICE_AVAILABLE = " + device.getInfoBoolean(CL_DEVICE_AVAILABLE));
                System.out.println("\tCL_DEVICE_COMPILER_AVAILABLE = " + device.getInfoBoolean(CL_DEVICE_COMPILER_AVAILABLE));
                printDeviceInfo(device, "CL_DEVICE_NAME", CL_DEVICE_NAME);
                printDeviceInfo(device, "CL_DEVICE_VENDOR", CL_DEVICE_VENDOR);
                printDeviceInfo(device, "CL_DRIVER_VERSION", CL_DRIVER_VERSION);
                printDeviceInfo(device, "CL_DEVICE_PROFILE", CL_DEVICE_PROFILE);
                printDeviceInfo(device, "CL_DEVICE_VERSION", CL_DEVICE_VERSION);
                printDeviceInfo(device, "CL_DEVICE_EXTENSIONS", CL_DEVICE_EXTENSIONS);
                if (caps.OpenCL11)
                    printDeviceInfo(device, "CL_DEVICE_OPENCL_C_VERSION", CL_DEVICE_OPENCL_C_VERSION);
                CLContext context = clCreateContext(ctxProps, device, new CLContextCallback() {

                    protected void handleMessage(final String errinfo, final ByteBuffer private_info) {
                        System.out.println("IN CLContextCallback :: " + errinfo);
                    }
                }, null);
                CLMem buffer = clCreateBuffer(context, CL_MEM_READ_ONLY, 128, null);
                if (caps.OpenCL11) {
                    clSetMemObjectDestructorCallback(buffer, new CLMemObjectDestructorCallback() {

                        protected void handleMessage(final long memobj) {
                            System.out.println("FIRST Buffer destructed: " + memobj);
                        }
                    });
                    clSetMemObjectDestructorCallback(buffer, new CLMemObjectDestructorCallback() {

                        protected void handleMessage(final long memobj) {
                            System.out.println("SECOND Buffer destructed: " + memobj);
                        }
                    });
                }
                if (caps.OpenCL11) {
                    CLMem subbuffer = buffer.createSubBuffer(CL_MEM_READ_ONLY, CL_BUFFER_CREATE_TYPE_REGION, new CLBufferRegion(0, 64), null);
                    clSetMemObjectDestructorCallback(subbuffer, new CLMemObjectDestructorCallback() {

                        protected void handleMessage(final long memobj) {
                            System.out.println("Sub Buffer destructed: " + memobj);
                        }
                    });
                }
                clRetainMemObject(buffer);
                if (LWJGLUtil.getPlatform() != LWJGLUtil.PLATFORM_MACOSX) {
                    // TODO: Native kernels crash on MacOSX, disable this until we can debug properly.
                    final long exec_caps = device.getInfoLong(CL_DEVICE_EXECUTION_CAPABILITIES);
                    if ((exec_caps & CL_EXEC_NATIVE_KERNEL) == CL_EXEC_NATIVE_KERNEL) {
                        System.out.println("-TRYING TO EXEC NATIVE KERNEL-");
                        final CLCommandQueue queue = clCreateCommandQueue(context, device, 0, null);
                        final PointerBuffer ev = BufferUtils.createPointerBuffer(1);
                        clEnqueueNativeKernel(queue, new CLNativeKernel() {

                            protected void execute(final ByteBuffer[] memobjs) {
                                System.out.println("\tmemobjs.length = " + memobjs.length);
                                for (int k = 0; k < memobjs.length; k++) {
                                    System.out.println("\tmemobjs[" + k + "].remaining() = " + memobjs[k].remaining());
                                    for (int l = memobjs[k].position(); l < memobjs[k].limit(); l++) {
                                        memobjs[k].put(l, (byte) l);
                                    }
                                }
                                System.out.println("\tNative kernel done.");
                            }
                        }, new CLMem[] { buffer }, new long[] { 128 }, null, ev);
                        final CLEvent e = queue.getCLEvent(ev.get(0));
                        clSetEventCallback(e, CL_COMPLETE, new CLEventCallback() {

                            protected void handleMessage(final CLEvent event, final int event_command_exec_status) {
                                System.out.println("\t\tEvent callback status: " + getEventStatusName(event_command_exec_status));
                            }
                        });
                        int status = e.getInfoInt(CL_EVENT_COMMAND_EXECUTION_STATUS);
                        System.out.println("NATIVE KERNEL STATUS: " + getEventStatusName(status));
                        clFlush(queue);
                        do {
                            int newStatus = e.getInfoInt(CL_EVENT_COMMAND_EXECUTION_STATUS);
                            if (newStatus != status) {
                                status = newStatus;
                                System.out.println("NATIVE KERNEL STATUS: " + getEventStatusName(status));
                            }
                        } while (// Busy-spin until we're done
                        status != CL_SUCCESS);
                        clReleaseEvent(e);
                    }
                }
                clReleaseMemObject(buffer);
                clReleaseContext(context);
            }
        }
    } catch (LWJGLException le) {
        die("Init", le.getMessage());
    }
    CL.destroy();
}
Also used : CLBufferRegion(org.lwjgl.opencl.api.CLBufferRegion) PointerBuffer(org.lwjgl.PointerBuffer) ByteBuffer(java.nio.ByteBuffer) LWJGLException(org.lwjgl.LWJGLException)

Example 7 with PointerBuffer

use of org.lwjgl.PointerBuffer in project lwjgl by LWJGL.

the class EGL method getEGLConfig.

private static EGLConfig getEGLConfig(final EGLDisplay dpy, final IntBuffer attrib_list) throws LWJGLException {
    final int configID = attrib_list.get(0);
    // -- This fails on the emulator
    // Get EGL config used by the context
    attrib_list.put(0, EGL_CONFIG_ID).put(1, configID).put(2, EGL_NONE);
    final PointerBuffer configs_buffer = APIUtil.getBufferPointer(1);
    if (!neglChooseConfig(dpy.getPointer(), MemoryUtil.getAddress(attrib_list), MemoryUtil.getAddress0(configs_buffer), 1, MemoryUtil.getAddress(attrib_list, 3)))
        throwEGLError("Failed to choose EGL config.");
    return new EGLConfig(dpy, configs_buffer.get(0));
// -- Emulator workaround
/*
		EGLConfig config = null;

		final EGLConfig[] configs = eglGetConfigs(dpy, null, attrib_list);
		final int config_size = attrib_list.get(0);
		for ( int i = 0; i < config_size; i++ ) {
			if ( configs[i].getConfigID() == configID ) {
				config = configs[i];
				break;
			}
		}

		if ( config == null )
			throwEGLError("Failed to retrieve EGL config for current context.");

		return config;
		//*/
}
Also used : PointerBuffer(org.lwjgl.PointerBuffer)

Example 8 with PointerBuffer

use of org.lwjgl.PointerBuffer in project lwjgl by LWJGL.

the class EGL method eglChooseConfig.

/**
	 * Returns the available EGLConfigs on the speficied display that satisfy the specified list of attributes.
	 * The number of available EGLConfigs is returned in the num_config parameter. The configs array may be null.
	 * If it is null, a new array will be allocated, with size equal to the result of {@link #eglGetConfigsNum(EGLDisplay)}  eglGetConfigsNum}.
	 * If it is not null, no more than {@code configs.length} EGLConfigs will be returned. If the array is bigger
	 * than the number of available EGLConfigs, the remaining array elements will not be affected.
	 *
	 * @param dpy         the EGLDisplay
	 * @param attrib_list the attribute list (may be null)
	 * @param configs     the EGLConfigs array
	 * @param num_config  the number of available EGLConfigs returned
	 *
	 * @return the available EGLConfigs that satisfy the attribute list
	 *
	 * @throws org.lwjgl.LWJGLException if an EGL error occurs
	 */
static EGLConfig[] eglChooseConfig(EGLDisplay dpy, IntBuffer attrib_list, EGLConfig[] configs, IntBuffer num_config) throws LWJGLException {
    //LWJGLUtil.log("eglChooseConfig");
    checkAttribList(attrib_list);
    BufferChecks.checkBuffer(num_config, 1);
    int config_size;
    if (configs == null) {
        if (!neglChooseConfig(dpy.getPointer(), MemoryUtil.getAddressSafe(attrib_list), 0L, 0, MemoryUtil.getAddress(num_config)))
            throwEGLError("Failed to get number of available EGL configs.");
        config_size = num_config.get(num_config.position());
    } else
        config_size = configs.length;
    //LWJGLUtil.log("config_size = " + config_size);
    PointerBuffer configs_buffer = APIUtil.getBufferPointer(config_size);
    if (!neglChooseConfig(dpy.getPointer(), MemoryUtil.getAddressSafe(attrib_list), MemoryUtil.getAddress0(configs_buffer), config_size, MemoryUtil.getAddress(num_config)))
        throwEGLError("Failed to choose EGL config.");
    // Get the true number of configurations (the first neglChooseConfig call may return more than the second)
    config_size = num_config.get(num_config.position());
    if (configs == null)
        configs = new EGLConfig[config_size];
    for (int i = 0; i < config_size; i++) configs[i] = new EGLConfig(dpy, configs_buffer.get(i));
    return configs;
}
Also used : PointerBuffer(org.lwjgl.PointerBuffer)

Example 9 with PointerBuffer

use of org.lwjgl.PointerBuffer in project jmonkeyengine by jMonkeyEngine.

the class LwjglKernel method Run.

@Override
public Event Run(CommandQueue queue) {
    Utils.pointerBuffers[0].rewind();
    Utils.pointerBuffers[1].rewind();
    Utils.pointerBuffers[1].put(globalWorkSize.getSizes());
    Utils.pointerBuffers[1].position(0);
    PointerBuffer p2 = null;
    if (workGroupSize.getSizes()[0] > 0) {
        p2 = Utils.pointerBuffers[2].rewind();
        p2.put(workGroupSize.getSizes());
        p2.position(0);
    }
    long q = ((LwjglCommandQueue) queue).getQueue();
    int ret = CL10.clEnqueueNDRangeKernel(q, kernel, globalWorkSize.getDimension(), null, Utils.pointerBuffers[1], p2, null, Utils.pointerBuffers[0]);
    Utils.checkError(ret, "clEnqueueNDRangeKernel");
    return new LwjglEvent(Utils.pointerBuffers[0].get(0));
}
Also used : PointerBuffer(org.lwjgl.PointerBuffer)

Example 10 with PointerBuffer

use of org.lwjgl.PointerBuffer in project jmonkeyengine by jMonkeyEngine.

the class LwjglKernel method RunNoEvent.

@Override
public void RunNoEvent(CommandQueue queue) {
    Utils.pointerBuffers[1].rewind();
    Utils.pointerBuffers[1].put(globalWorkSize.getSizes());
    Utils.pointerBuffers[1].position(0);
    PointerBuffer p2 = null;
    if (workGroupSize.getSizes()[0] > 0) {
        p2 = Utils.pointerBuffers[2].rewind();
        p2.put(workGroupSize.getSizes());
        p2.position(0);
    }
    long q = ((LwjglCommandQueue) queue).getQueue();
    int ret = CL10.clEnqueueNDRangeKernel(q, kernel, globalWorkSize.getDimension(), null, Utils.pointerBuffers[1], p2, null, null);
    Utils.checkError(ret, "clEnqueueNDRangeKernel");
}
Also used : PointerBuffer(org.lwjgl.PointerBuffer)

Aggregations

PointerBuffer (org.lwjgl.PointerBuffer)20 IntBuffer (java.nio.IntBuffer)3 ByteBuffer (java.nio.ByteBuffer)2 Monitor (com.badlogic.gdx.Graphics.Monitor)1 Lwjgl3Monitor (com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.Lwjgl3Monitor)1 LWJGLException (org.lwjgl.LWJGLException)1 CLBufferRegion (org.lwjgl.opencl.api.CLBufferRegion)1