// First check if the WebCL extension is installed at all
if (window.webcl == undefined){
alert("Unfortunately your system does not support WebCL." +
"Make sure that you have both the OpenCL dirver" +
"and the WebCL browser extension installed.");
}
// Get a list of available CL platforms, and another list of the
// available devices on each platform. If there are no platforms
// or no available devices on any platform, then we can conclude
// that WebCL is not available
webcl = window.webcl
try{
var platforms = webcl.getPlatforms();
var devices = [];
for (var i in platforms){
var p = platforms[i];
devices[i] = p.getDevices();
}
alert("Excellent! Your system does support WebCL");
} catch(e){
alert("Unfortunately platform or device inquiry failed.");
}
// Setup WebCL context using the default device
var ctx = webcl.createContext();
// Find appropriate device
for (var j = 0, jl = device.length; j < jl; ++j){
var d = devices[j];
var devExts = d.getInfo(cl.DEVICE_EXTENSIONS);
var devGMem = d.getInfo(cl.DEVICE_GLOBAL_MEM_SIZE);
var devLMem = d.getInfo(cl.DEVICE_LOCAL_MEM_SIZE);
var devCompUnits = d.getInfo(cl.DEVICE_MAX_COMPUTE_UNITS);
var devHasImage = d.getInfo(cl.DEVICE_IMAGE_SUPPORT);
// select device that matches your requirements
platform = ...
device = ...
}
// assuming we found the best device, we can create the context
var context = webcl.createContext(platform, device);
// Create the compute program from the source strings
program = context.createProgram(source);
// Build the program executable with relaxed math flag
try{
program.build(device, "-cl-fast-relaxed-math");
} catch(err) {
throw 'Error building program:' + err + program.getBuildInfo(device, cl.PROGRAM_BUILD_LOG);
}
// Create a 1D buffer
var buffer = webcl.createBuffer(flags, sizeInBytes, optional srcBuffer);
// flags:
// webcl.MEM_READ_WRITE Default. Memory object is read and written by kernel
// webcl.MEM_WRITE_ONLY Memory object only writeten by kernel
// webcl.MEM_READ_ONLY Memory object only read by kernel
// webcl.MEM_USE_HOST_PTR Implementation requests OpenCL to allocate host memory
// webcl.MEM_COPY_HOST_PTR Implementation requests OpenCL to allocate host memory and copy data from srcBuffer memory. srcBuffer must be specified
// create a 32-bit RGBA WebCLImage object
// first, we define the format of the image
var imageFormat = {
// memory layout in which pixel data channels are stored in the image
'channelOrder':webcl.RGBA,
// type of the channel data
'channelType':webcl.UNSIGNED_INT8,
// image size
'width':image_width,
'height':image_height,
// scan-line pitch in bytes.
// If imageBuffer is null, which is the default if rowPitch is not specified.
'rowPitch':image_pitch
};
// Image on device
// imageBuffer is a typed array that contain the image data already allocated by the application
// imageBuffer.byteLength >= rowPitch * image_height. The size of each element in bytes must be a power of 2.
var image = context.createImage(webcl.MEM_READ_ONLY | webcl.MEM_USE_HOST_PTR, imageFormat, imageBuffer);
// create a smpler object
var sampler = context.createSampler(normalizedCoords, addressingMode, filterMode);
// normalizedCoods indicates if image coordinates specified are normalized.
// addressingMode indicated how out-of-range image coordinations are handled when reading an image.
// This can be set to webcl.ADDRESS_MIRRORED_REPEAT
// webcl.ADDRESS_REPEAT, webcl.ADDRESS_CLAMP_TO_EDGE,
// webcl.ADDRESS_CLAMP and webcl.ADDRESS_NONE.
// filterMode specifies the type of filter to apply when reading an image. This can be webcl.FILTER_NEAREST or webcl.FILTER_LINEAR
// Sets value of kernel argument idx with value as memory object or sampler
kernel.setArg(idx.a_buffer);
kernel.setArg(idx.a_image);
kernel.setArg(idx.a_sampler);
// Sets value of argument 0 to the integer value 5
kernel.setArg(0, new Int32Array([5]));
// Sets value of argument 1 to the float value 1.34
kernel.setArg(1, new Float32Array([1.34]));
// Sets value of argument 2 as a 3-float vector
// buffer should be a Float32Array with 3 floats
kernel.setArg(2, new Float32Array([1.0, 2.0, 3.0]));
// Allocate 4096 bytes of local memory for argument 4
kernel.setArg(3, new Int32Array([4096]));
// Create an in-order command-queue(default)
var queue = context.createCommandQueue(device);
// Create an in-order command-queue with profiling of commands enabled
var queue = context.createCommandQueue(device, webcl.QUEUE_PROFILING_ENABLE);
// Create an out-of-order command-queue
var queue = context.createCommandQueue(device, webcl.QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE);