/* * Copyright (c) 2009-2016 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.opencl.jocl; import com.jme3.opencl.*; import com.jme3.opencl.Context; import com.jme3.opencl.Image.ImageDescriptor; import com.jme3.opencl.Image.ImageFormat; import com.jme3.scene.VertexBuffer; import com.jme3.texture.FrameBuffer; import com.jme3.texture.Texture; import com.jogamp.opencl.CLContext; import com.jogamp.opencl.CLImageFormat; import com.jogamp.opencl.CLMemory.Mem; import com.jogamp.opencl.CLPlatform; import com.jogamp.opencl.llb.CL; import com.jogamp.opencl.llb.gl.CLGL; import com.jogamp.opencl.llb.impl.CLImageFormatImpl; import com.jogamp.opengl.GL; import java.nio.ByteBuffer; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author shaman */ public class JoclContext extends Context { private static final Logger LOG = Logger.getLogger(JoclContext.class.getName()); final CLContext context; final long id; final CL cl; private final List devices; public JoclContext(CLContext context, List devices) { super(new ReleaserImpl(context.ID, devices)); this.context = context; this.id = context.ID; this.cl = context.getCL(); this.devices = devices; } public CLContext getContext() { return context; } @Override public List getDevices() { return devices; } @Override @SuppressWarnings("element-type-mismatch") public CommandQueue createQueue(Device device) { assert (devices.contains(device)); //this also ensures that device is a JoclDevice long d = ((JoclDevice) device).id; long properties = 0; long q = cl.clCreateCommandQueue(id, d, properties, Utils.errorBuffer); Utils.checkError(Utils.errorBuffer, "clCreateCommandQueue"); return new JoclCommandQueue(q); } @Override public Buffer createBuffer(long size, MemoryAccess access) { long flags = Utils.getMemoryAccessFlags(access); long mem = cl.clCreateBuffer(id, flags, size, null, Utils.errorBuffer); Utils.checkError(Utils.errorBuffer, "clCreateBuffer"); return new JoclBuffer(mem); } @Override public Buffer createBufferFromHost(ByteBuffer data, MemoryAccess access) { long flags = Utils.getMemoryAccessFlags(access); flags |= CL.CL_MEM_USE_HOST_PTR; long mem = cl.clCreateBuffer(id, flags, data.capacity(), data, Utils.errorBuffer); Utils.checkError(Utils.errorBuffer, "clCreateBuffer"); return new JoclBuffer(mem); } @Override public Image createImage(MemoryAccess access, ImageFormat format, ImageDescriptor descr) { if (descr.type != Image.ImageType.IMAGE_2D && descr.type != Image.ImageType.IMAGE_3D) { throw new UnsupportedOperationException("Jocl only supports 2D and 3D images"); } long memFlags = Utils.getMemoryAccessFlags(access); Utils.errorBuffer.rewind(); //fill image format CLImageFormatImpl f = CLImageFormatImpl.create(); f.setImageChannelOrder(JoclImage.decodeImageChannelOrder(format.channelOrder)); f.setImageChannelDataType(JoclImage.decodeImageChannelType(format.channelType)); //create image long mem; if (descr.type == Image.ImageType.IMAGE_2D) { mem = cl.clCreateImage2D(id, memFlags, f, descr.width, descr.height, descr.hostPtr==null ? 0 : descr.rowPitch, descr.hostPtr, Utils.errorBuffer); Utils.checkError(Utils.errorBuffer, "clCreateImage2D"); } else { mem = cl.clCreateImage3D(id, memFlags, f, descr.width, descr.height, descr.depth, descr.hostPtr==null ? 0 : descr.rowPitch, descr.hostPtr==null ? 0 : descr.slicePitch, descr.hostPtr, Utils.errorBuffer); Utils.checkError(Utils.errorBuffer, "clCreateImage3D"); } return new JoclImage(mem); } @Override public ImageFormat[] querySupportedFormats(MemoryAccess access, Image.ImageType type) { if (type != Image.ImageType.IMAGE_2D && type != Image.ImageType.IMAGE_3D) { throw new UnsupportedOperationException("Jocl only supports 2D and 3D images"); } long memFlags = Utils.getMemoryAccessFlags(access); CLImageFormat[] fx; if (type == Image.ImageType.IMAGE_2D) { fx = context.getSupportedImage2dFormats(Mem.valueOf((int) memFlags)); } else { fx = context.getSupportedImage3dFormats(Mem.valueOf((int) memFlags)); } //convert formats ImageFormat[] formats = new ImageFormat[fx.length]; for (int i=0; i devices; private ReleaserImpl(long id, List devices) { this.id = id; this.devices = devices; } @Override public void release() { if (id != 0) { int ret = CLPlatform.getLowLevelCLInterface().clReleaseContext(id); id = 0; devices.clear(); Utils.reportError(ret, "clReleaseContext"); } } } }