Skip to content

Commit

Permalink
added CLContext.getSupportedImageFormats() methods and unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbien committed Jun 24, 2010
1 parent 735c9cb commit b6d6f75
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
48 changes: 47 additions & 1 deletion src/com/jogamp/opencl/CLContext.java
@@ -1,10 +1,10 @@
package com.jogamp.opencl;

import com.jogamp.opencl.CLDevice.Type;
import com.jogamp.opencl.CLMemory.Mem;
import com.jogamp.opencl.CLSampler.AddressingMode;
import com.jogamp.opencl.CLSampler.FilteringMode;
import com.jogamp.common.nio.PointerBuffer;
import com.jogamp.opencl.impl.CLImageFormatImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -27,6 +27,8 @@
import static com.jogamp.opencl.CLException.*;
import static com.jogamp.common.nio.Buffers.*;
import static com.jogamp.common.os.Platform.*;
import static com.jogamp.opencl.CL.*;
import static com.jogamp.opencl.CLBuffer.*;

/**
* CLContext is responsible for managing objects such as command-queues, memory,
Expand Down Expand Up @@ -414,6 +416,50 @@ protected void overrideContext(CLDevice device) {
device.setContext(this);
}

private CLImageFormat[] getSupportedImageFormats(int flags, int type) {

int[] entries = new int[1];
int ret = cl.clGetSupportedImageFormats(ID, flags, type, 0, null, entries, 0);
if(ret != CL_SUCCESS) {
throw newException(ret, "error calling clGetSupportedImageFormats");
}

int count = entries[0];
if(count == 0) {
return new CLImageFormat[0];
}

CLImageFormat[] formats = new CLImageFormat[count];
CLImageFormatImpl impl = CLImageFormatImpl.create(newDirectByteBuffer(count * CLImageFormatImpl.size()));
ret = cl.clGetSupportedImageFormats(ID, flags, type, count, impl, null, 0);
if(ret != CL_SUCCESS) {
throw newException(ret, "error calling clGetSupportedImageFormats");
}

ByteBuffer buffer = impl.getBuffer();
for (int i = 0; i < formats.length; i++) {
formats[i] = new CLImageFormat(CLImageFormatImpl.create(buffer.slice()));
buffer.position(i*CLImageFormatImpl.size());
}

return formats;

}

/**
* Returns all supported 2d image formats with the (optional) memory allocation flags.
*/
public CLImageFormat[] getSupportedImage2dFormats(Mem... flags) {
return getSupportedImageFormats(flags==null?0:Mem.flagsToInt(flags), CL_MEM_OBJECT_IMAGE2D);
}

/**
* Returns all supported 3d image formats with the (optional) memory allocation flags.
*/
public CLImageFormat[] getSupportedImage3dFormats(Mem... flags) {
return getSupportedImageFormats(flags==null?0:Mem.flagsToInt(flags), CL_MEM_OBJECT_IMAGE3D);
}

/**
* Returns the CLPlatform this context is running on.
*/
Expand Down
15 changes: 13 additions & 2 deletions src/com/jogamp/opencl/CLImageFormat.java
Expand Up @@ -5,17 +5,23 @@
import static com.jogamp.opencl.CL.*;

/**
*
* Represents the OpenCL image format with its channeltype and order.
* @author Michael Bien
*/
public final class CLImageFormat {

private final CLImageFormatImpl format = CLImageFormatImpl.create();
private final CLImageFormatImpl format;

CLImageFormat() {
format = CLImageFormatImpl.create();
}

CLImageFormat(CLImageFormatImpl format) {
this.format = format;
}

public CLImageFormat(ChannelOrder order, ChannelType type) {
format = CLImageFormatImpl.create();
setImageChannelOrder(order);
setImageChannelDataType(type);
}
Expand Down Expand Up @@ -45,6 +51,11 @@ public CLImageFormatImpl getFormatImpl() {
return format;
}

@Override
public String toString() {
return "CLImageFormat["+getImageChannelOrder()+" "+getImageChannelDataType()+"]";
}

/**
* Specifies the number of channels and the channel layout i.e. the memory
* layout in which channels are stored in the image.
Expand Down
29 changes: 29 additions & 0 deletions test/com/jogamp/opencl/HighLevelBindingTest.java
Expand Up @@ -321,5 +321,34 @@ public void vectorAddGMTest() throws IOException {
context.release();
}

@Test
public void supportedImageFormatsTest() {

CLDevice[] devices = CLPlatform.getDefault().listCLDevices();

CLDevice theChosenOne = null;
for (CLDevice device : devices) {
if(device.isImageSupportAvailable()) {
theChosenOne = device;
break;
}
}

if(theChosenOne == null) {
out.println("can not test image api.");
return;
}

CLContext context = CLContext.create(theChosenOne);

try{
CLImageFormat[] formats = context.getSupportedImage2dFormats();
assertTrue(formats.length > 0);
out.println("sample image format: "+formats[0]);
}finally{
context.release();
}

}

}

0 comments on commit b6d6f75

Please sign in to comment.