Search in sources :

Example 6 with Device

use of com.jme3.opencl.Device in project jmonkeyengine by jMonkeyEngine.

the class JoglContext method initOpenCL.

@SuppressWarnings("unchecked")
protected void initOpenCL() {
    logger.info("Initialize OpenCL with JOGL");
    //load platforms and devices
    StringBuilder platformInfos = new StringBuilder();
    ArrayList<JoclPlatform> platforms = new ArrayList<JoclPlatform>();
    for (CLPlatform p : CLPlatform.listCLPlatforms()) {
        platforms.add(new JoclPlatform(p));
    }
    platformInfos.append("Available OpenCL platforms:");
    for (int i = 0; i < platforms.size(); ++i) {
        JoclPlatform platform = platforms.get(i);
        platformInfos.append("\n * Platform ").append(i + 1);
        platformInfos.append("\n *   Name: ").append(platform.getName());
        platformInfos.append("\n *   Vendor: ").append(platform.getVendor());
        platformInfos.append("\n *   Version: ").append(platform.getVersion());
        platformInfos.append("\n *   Profile: ").append(platform.getProfile());
        platformInfos.append("\n *   Supports interop: ").append(platform.hasOpenGLInterop());
        List<JoclDevice> devices = platform.getDevices();
        platformInfos.append("\n *   Available devices:");
        for (int j = 0; j < devices.size(); ++j) {
            JoclDevice device = devices.get(j);
            platformInfos.append("\n *    * Device ").append(j + 1);
            platformInfos.append("\n *    *   Name: ").append(device.getName());
            platformInfos.append("\n *    *   Vendor: ").append(device.getVendor());
            platformInfos.append("\n *    *   Version: ").append(device.getVersion());
            platformInfos.append("\n *    *   Profile: ").append(device.getProfile());
            platformInfos.append("\n *    *   Compiler version: ").append(device.getCompilerVersion());
            platformInfos.append("\n *    *   Device type: ").append(device.getDeviceType());
            platformInfos.append("\n *    *   Compute units: ").append(device.getComputeUnits());
            platformInfos.append("\n *    *   Work group size: ").append(device.getMaxiumWorkItemsPerGroup());
            platformInfos.append("\n *    *   Global memory: ").append(device.getGlobalMemorySize()).append("B");
            platformInfos.append("\n *    *   Local memory: ").append(device.getLocalMemorySize()).append("B");
            platformInfos.append("\n *    *   Constant memory: ").append(device.getMaximumConstantBufferSize()).append("B");
            platformInfos.append("\n *    *   Supports double: ").append(device.hasDouble());
            platformInfos.append("\n *    *   Supports half floats: ").append(device.hasHalfFloat());
            platformInfos.append("\n *    *   Supports writable 3d images: ").append(device.hasWritableImage3D());
            platformInfos.append("\n *    *   Supports interop: ").append(device.hasOpenGLInterop());
        }
    }
    logger.info(platformInfos.toString());
    //choose devices
    PlatformChooser chooser = null;
    if (settings.getOpenCLPlatformChooser() != null) {
        try {
            chooser = (PlatformChooser) Class.forName(settings.getOpenCLPlatformChooser()).newInstance();
        } catch (Exception ex) {
            logger.log(Level.WARNING, "unable to instantiate custom PlatformChooser", ex);
        }
    }
    if (chooser == null) {
        chooser = new DefaultPlatformChooser();
    }
    List<? extends Device> choosenDevices = chooser.chooseDevices(platforms);
    List<CLDevice> devices = new ArrayList<>(choosenDevices.size());
    JoclPlatform platform = null;
    for (Device d : choosenDevices) {
        if (!(d instanceof JoclDevice)) {
            logger.log(Level.SEVERE, "attempt to return a custom Device implementation from PlatformChooser: {0}", d);
            return;
        }
        JoclDevice ld = (JoclDevice) d;
        if (platform == null) {
            platform = ld.getPlatform();
        } else if (platform != ld.getPlatform()) {
            logger.severe("attempt to use devices from different platforms");
            return;
        }
        devices.add(ld.getDevice());
    }
    if (devices.isEmpty()) {
        logger.warning("no devices specified, no OpenCL context created");
        return;
    }
    logger.log(Level.INFO, "chosen platform: {0}", platform.getName());
    logger.log(Level.INFO, "chosen devices: {0}", choosenDevices);
    //create context
    try {
        CLGLContext c = CLGLContext.create(GLContext.getCurrent(), devices.toArray(new CLDevice[devices.size()]));
        clContext = new com.jme3.opencl.jocl.JoclContext(c, (List<JoclDevice>) choosenDevices);
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Unable to create OpenCL context", ex);
        return;
    }
    logger.info("OpenCL context created");
}
Also used : JoclPlatform(com.jme3.opencl.jocl.JoclPlatform) JoclDevice(com.jme3.opencl.jocl.JoclDevice) CLDevice(com.jogamp.opencl.CLDevice) Device(com.jme3.opencl.Device) CLDevice(com.jogamp.opencl.CLDevice) ArrayList(java.util.ArrayList) JoclDevice(com.jme3.opencl.jocl.JoclDevice) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser) PlatformChooser(com.jme3.opencl.PlatformChooser) CLGLContext(com.jogamp.opencl.gl.CLGLContext) RendererException(com.jme3.renderer.RendererException) ArrayList(java.util.ArrayList) List(java.util.List) CLPlatform(com.jogamp.opencl.CLPlatform) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser)

Example 7 with Device

use of com.jme3.opencl.Device in project jmonkeyengine by jMonkeyEngine.

the class LwjglContext method initOpenCL.

protected void initOpenCL(long window) {
    logger.info("Initialize OpenCL with LWJGL3");
    //        try {
    //            CL.create();
    //        } catch (Exception ex) {
    //            logger.log(Level.SEVERE, "Unable to initialize OpenCL", ex);
    //            return;
    //        }
    //load platforms and devices
    StringBuilder platformInfos = new StringBuilder();
    ArrayList<LwjglPlatform> platforms = new ArrayList<>();
    for (long p : getPlatforms()) {
        platforms.add(new LwjglPlatform(p));
    }
    platformInfos.append("Available OpenCL platforms:");
    for (int i = 0; i < platforms.size(); ++i) {
        LwjglPlatform platform = platforms.get(i);
        platformInfos.append("\n * Platform ").append(i + 1);
        platformInfos.append("\n *   Name: ").append(platform.getName());
        platformInfos.append("\n *   Vendor: ").append(platform.getVendor());
        platformInfos.append("\n *   Version: ").append(platform.getVersion());
        platformInfos.append("\n *   Profile: ").append(platform.getProfile());
        platformInfos.append("\n *   Supports interop: ").append(platform.hasOpenGLInterop());
        List<LwjglDevice> devices = platform.getDevices();
        platformInfos.append("\n *   Available devices:");
        for (int j = 0; j < devices.size(); ++j) {
            LwjglDevice device = devices.get(j);
            platformInfos.append("\n *    * Device ").append(j + 1);
            platformInfos.append("\n *    *   Name: ").append(device.getName());
            platformInfos.append("\n *    *   Vendor: ").append(device.getVendor());
            platformInfos.append("\n *    *   Version: ").append(device.getVersion());
            platformInfos.append("\n *    *   Profile: ").append(device.getProfile());
            platformInfos.append("\n *    *   Compiler version: ").append(device.getCompilerVersion());
            platformInfos.append("\n *    *   Device type: ").append(device.getDeviceType());
            platformInfos.append("\n *    *   Compute units: ").append(device.getComputeUnits());
            platformInfos.append("\n *    *   Work group size: ").append(device.getMaxiumWorkItemsPerGroup());
            platformInfos.append("\n *    *   Global memory: ").append(device.getGlobalMemorySize()).append("B");
            platformInfos.append("\n *    *   Local memory: ").append(device.getLocalMemorySize()).append("B");
            platformInfos.append("\n *    *   Constant memory: ").append(device.getMaximumConstantBufferSize()).append("B");
            platformInfos.append("\n *    *   Supports double: ").append(device.hasDouble());
            platformInfos.append("\n *    *   Supports half floats: ").append(device.hasHalfFloat());
            platformInfos.append("\n *    *   Supports writable 3d images: ").append(device.hasWritableImage3D());
            platformInfos.append("\n *    *   Supports interop: ").append(device.hasOpenGLInterop());
        }
    }
    logger.info(platformInfos.toString());
    //choose devices
    PlatformChooser chooser = null;
    if (settings.getOpenCLPlatformChooser() != null) {
        try {
            chooser = (PlatformChooser) Class.forName(settings.getOpenCLPlatformChooser()).newInstance();
        } catch (Exception ex) {
            logger.log(Level.WARNING, "unable to instantiate custom PlatformChooser", ex);
        }
    }
    if (chooser == null) {
        chooser = new DefaultPlatformChooser();
    }
    List<? extends Device> choosenDevices = chooser.chooseDevices(platforms);
    List<Long> devices = new ArrayList<>(choosenDevices.size());
    LwjglPlatform platform = null;
    for (Device d : choosenDevices) {
        if (!(d instanceof LwjglDevice)) {
            logger.log(Level.SEVERE, "attempt to return a custom Device implementation from PlatformChooser: {0}", d);
            return;
        }
        LwjglDevice ld = (LwjglDevice) d;
        if (platform == null) {
            platform = ld.getPlatform();
        } else if (platform != ld.getPlatform()) {
            logger.severe("attempt to use devices from different platforms");
            return;
        }
        devices.add(ld.getDevice());
    }
    if (devices.isEmpty()) {
        logger.warning("no devices specified, no OpenCL context created");
        return;
    }
    logger.log(Level.INFO, "chosen platform: {0}", platform.getName());
    logger.log(Level.INFO, "chosen devices: {0}", choosenDevices);
    //create context
    try {
        long c = createContext(platform.getPlatform(), devices, window);
        clContext = new com.jme3.opencl.lwjgl.LwjglContext(c, (List<LwjglDevice>) choosenDevices);
    } catch (final Exception ex) {
        logger.log(Level.SEVERE, "Unable to create OpenCL context", ex);
        return;
    }
    logger.info("OpenCL context created");
}
Also used : Device(com.jme3.opencl.Device) LwjglDevice(com.jme3.opencl.lwjgl.LwjglDevice) ArrayList(java.util.ArrayList) PlatformChooser(com.jme3.opencl.PlatformChooser) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser) RendererException(com.jme3.renderer.RendererException) LwjglDevice(com.jme3.opencl.lwjgl.LwjglDevice) LwjglPlatform(com.jme3.opencl.lwjgl.LwjglPlatform) List(java.util.List) ArrayList(java.util.ArrayList) DefaultPlatformChooser(com.jme3.opencl.DefaultPlatformChooser)

Example 8 with Device

use of com.jme3.opencl.Device in project jmonkeyengine by jMonkeyEngine.

the class TestAndroidResources method simpleInitApp.

@Override
public void simpleInitApp() {
    // Create boxes with textures that are stored in the Android Resources Folders
    // Images are stored in multiple Drawable and Mipmap directories.  Android picks the ones that
    // match the device size and density.
    Box box1Mesh = new Box(1, 1, 1);
    Geometry box1 = new Geometry("Monkey Box 1", box1Mesh);
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setTexture("ColorMap", assetManager.loadTexture("drawable_monkey.png"));
    box1.setMaterial(mat1);
    box1.setLocalTranslation(-2, 0, 0);
    rootNode.attachChild(box1);
    Box box2Mesh = new Box(1, 1, 1);
    Geometry box2 = new Geometry("Monkey Box 2", box2Mesh);
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.setTexture("ColorMap", assetManager.loadTexture("mipmap_monkey.png"));
    box2.setMaterial(mat2);
    box2.setLocalTranslation(2, 0, 0);
    rootNode.attachChild(box2);
}
Also used : Geometry(com.jme3.scene.Geometry) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material)

Example 9 with Device

use of com.jme3.opencl.Device in project jmonkeyengine by jMonkeyEngine.

the class TestAndroidSensors method simpleInitApp.

@Override
public void simpleInitApp() {
    if (enableFlyByCameraRotation) {
        flyCam.setEnabled(true);
    } else {
        flyCam.setEnabled(false);
    }
    Mesh lineX = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_X.mult(3)));
    Mesh lineY = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Y.mult(3)));
    Mesh lineZ = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Z.mult(3)));
    Geometry geoX = new Geometry("X", lineX);
    Material matX = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matX.setColor("Color", ColorRGBA.Red);
    matX.getAdditionalRenderState().setLineWidth(30);
    geoX.setMaterial(matX);
    rootNode.attachChild(geoX);
    Geometry geoY = new Geometry("Y", lineY);
    Material matY = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matY.setColor("Color", ColorRGBA.Green);
    matY.getAdditionalRenderState().setLineWidth(30);
    geoY.setMaterial(matY);
    rootNode.attachChild(geoY);
    Geometry geoZ = new Geometry("Z", lineZ);
    Material matZ = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matZ.setColor("Color", ColorRGBA.Blue);
    matZ.getAdditionalRenderState().setLineWidth(30);
    geoZ.setMaterial(matZ);
    rootNode.attachChild(geoZ);
    Box b = new Box(1, 1, 1);
    geomZero = new Geometry("Box", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Yellow);
    Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    mat.setTexture("ColorMap", tex_ml);
    geomZero.setMaterial(mat);
    geomZero.setLocalTranslation(Vector3f.ZERO);
    geomZero.setLocalRotation(Quaternion.IDENTITY);
    rootNode.attachChild(geomZero);
    // Touch (aka MouseInput.BUTTON_LEFT) is used to record the starting
    // orientation when using absolute rotations
    inputManager.addMapping("MouseClick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(this, "MouseClick");
    Joystick[] joysticks = inputManager.getJoysticks();
    if (joysticks == null || joysticks.length < 1) {
        logger.log(Level.INFO, "Cannot find any joysticks!");
    } else {
        // Joysticks return a value of 0 to 1 based on how far the stick is
        // push on the axis. This value is then scaled based on how long
        // during the frame the joystick axis has been in that position.
        // If the joystick is push all the way for the whole frame,
        // then the value in onAnalog is equal to tpf.
        // If the joystick is push 1/2 way for the entire frame, then the
        // onAnalog value is 1/2 tpf.
        // Similarly, if the joystick is pushed to the maximum during a frame
        // the value in onAnalog will also be scaled.
        // For Android sensors, rotating the device 90deg is the same as
        // pushing an actual joystick axis to the maximum.
        logger.log(Level.INFO, "Number of joysticks: {0}", joysticks.length);
        JoystickAxis axis;
        for (Joystick joystick : joysticks) {
            // Get and display all axes in joystick.
            List<JoystickAxis> axes = joystick.getAxes();
            for (JoystickAxis joystickAxis : axes) {
                logger.log(Level.INFO, "{0} axis scan Name: {1}, LogicalId: {2}, AxisId: {3}", new Object[] { joystick.getName(), joystickAxis.getName(), joystickAxis.getLogicalId(), joystickAxis.getAxisId() });
            }
            // Get specific axis based on LogicalId of the JoystickAxis
            // If found, map axis
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_X);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
                inputManager.addListener(this, ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for X axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Y);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
                inputManager.addListener(this, ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Y axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Z);
            if (axis != null) {
                axis.assignAxis(ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
                inputManager.addListener(this, ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
                logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Z axis: {1}, with max value: {2}", new Object[] { joystick.toString(), axis.toString(), ((SensorJoystickAxis) axis).getMaxRawValue() });
            }
            joystickMap.put(joystick.getJoyId(), joystick);
        }
    }
}
Also used : Line(com.jme3.scene.shape.Line) Geometry(com.jme3.scene.Geometry) Joystick(com.jme3.input.Joystick) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box) MouseButtonTrigger(com.jme3.input.controls.MouseButtonTrigger) SensorJoystickAxis(com.jme3.input.SensorJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis) Texture(com.jme3.texture.Texture)

Example 10 with Device

use of com.jme3.opencl.Device in project jmonkeyengine by jMonkeyEngine.

the class ALAudioRenderer method updateInRenderThread.

public void updateInRenderThread(float tpf) {
    if (audioDisabled) {
        return;
    }
    for (int i = 0; i < channels.length; i++) {
        AudioSource src = chanSrcs[i];
        if (src == null) {
            continue;
        }
        int sourceId = channels[i];
        boolean boundSource = i == src.getChannel();
        boolean reclaimChannel = false;
        Status oalStatus = convertStatus(al.alGetSourcei(sourceId, AL_SOURCE_STATE));
        if (!boundSource) {
            // Handle it here.
            if (oalStatus == Status.Stopped) {
                // Instanced audio stopped playing. Reclaim channel.
                clearChannel(i);
                freeChannel(i);
            } else if (oalStatus == Status.Paused) {
                throw new AssertionError("Instanced audio cannot be paused");
            }
            continue;
        }
        Status jmeStatus = src.getStatus();
        // Check if we need to sync JME status with OAL status.
        if (oalStatus != jmeStatus) {
            if (oalStatus == Status.Stopped && jmeStatus == Status.Playing) {
                // Maybe we need to reclaim the channel.
                if (src.getAudioData() instanceof AudioStream) {
                    AudioStream stream = (AudioStream) src.getAudioData();
                    if (stream.isEOF() && !src.isLooping()) {
                        // Stream finished playing
                        reclaimChannel = true;
                    } else {
                    // Stream still has data. 
                    // Buffer starvation occured.
                    // Audio decoder thread will fill the data 
                    // and start the channel again.
                    }
                } else {
                    // Buffer finished playing.
                    if (src.isLooping()) {
                        // When a device is disconnected, all sources
                        // will enter the "stopped" state.
                        logger.warning("A looping sound has stopped playing");
                    }
                    reclaimChannel = true;
                }
                if (reclaimChannel) {
                    src.setStatus(Status.Stopped);
                    src.setChannel(-1);
                    clearChannel(i);
                    freeChannel(i);
                }
            } else {
                // This is only relevant for bound sources.
                throw new AssertionError("Unexpected sound status. " + "OAL: " + oalStatus + ", JME: " + jmeStatus);
            }
        } else {
            // Stopped channel was not cleared correctly.
            if (oalStatus == Status.Stopped) {
                throw new AssertionError("Channel " + i + " was not reclaimed");
            }
        }
    }
}
Also used : Status(com.jme3.audio.AudioSource.Status)

Aggregations

ArrayList (java.util.ArrayList)4 DefaultPlatformChooser (com.jme3.opencl.DefaultPlatformChooser)3 Device (com.jme3.opencl.Device)3 PlatformChooser (com.jme3.opencl.PlatformChooser)3 RendererException (com.jme3.renderer.RendererException)3 AppSettings (com.jme3.system.AppSettings)3 List (java.util.List)3 Joystick (com.jme3.input.Joystick)2 JoystickAxis (com.jme3.input.JoystickAxis)2 Material (com.jme3.material.Material)2 LwjglDevice (com.jme3.opencl.lwjgl.LwjglDevice)2 LwjglPlatform (com.jme3.opencl.lwjgl.LwjglPlatform)2 Geometry (com.jme3.scene.Geometry)2 Box (com.jme3.scene.shape.Box)2 GraphicsDevice (java.awt.GraphicsDevice)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileReader (java.io.FileReader)2 ActivityManager (android.app.ActivityManager)1 ConfigurationInfo (android.content.pm.ConfigurationInfo)1