Howto:Using OpenCL in FlightGear

From FlightGear wiki
Jump to navigation Jump to search
This article or section contains out-of-date information

Please help improve this article by updating it. There may be additional information on the talk page.

Background

OpenCL is an emerging standard to formalize general purpose computing and offload it to dedicated hardware (CPU cores and GPU cores, but also hardware like FPGA boards) in a well-defined manner. OpenCL makes it possible to write so called "kernels" (like GLSL shaders) that offload specific computations, so that these are executed on idle CPU/GPU cores. Unlike GLSL shaders, OpenCL kernels are not rendering specific. For further information please see: http://en.wikipedia.org/wiki/OpenCL

Objective

Demonstrate how OpenCL can be used in FlightGear, to offload certain computations onto the GPU, or other cores.

Installing OpenCL Support

Status (08/2012)

I have a very simple proof of concept here, loading a "hello world" OpenCL kernel into FlightGear via Nasal extension functions. This doesn't yet have any real integration with the rest of FlightGear, i.e. compared to the FlightGear effects/shader interface it's really simple and crude, it's just intended to explore the possibility of using OpenCL in FlightGear. Currently, OpenCL kernels must be explicitly loaded via Nasal and a handful of new extension functions. Also, integration with the property tree is still missing, so that arguments must be manually passed from Nasal via getprop() and calling the Nasal extension function that's wrapping OpenCL's clSetKernelArg()

Todo

  • detect OpenCL support via cmake (finished)
  • add new C++ file to Nasal folder for wrapping the OpenCL APIs (finished)
  • Use the C++ bindings instead of the plain C bindings: http://www.khronos.org/registry/cl/
  • consider using clutil: http://code.google.com/p/clutil/wiki/UserGuide
  • expose additional OpenCL APIs (in progress)
  • There needs to be a wrapper to make FlightGear properties accessible to/from OpenCL kernels. Ideally, this should work like the existing effects/shader interface, so there's probably some code that could be borrowed/generalized to be useful for both, GLSL shaders and OpenCL kernels.

Nasal bindings

Minimal mapping of OpenCL API functions to their Nasal extension functions.

Nasal Wrapper

High level Nasal wrapper for the low-level APIs exposed via Nasal extension functions, these should be based on the C++ spec: http://www.khronos.org/registry/cl/specs/opencl-cplusplus-1.1.pdf

That means, there will be a main "opencl" module, with additional wrapper classes for:

  • memory objects
  • buffer objects
  • images
  • samplers
  • programs
  • kernels
  • events
  • user events
  • command queues


OpenCL Kernels

//VectorAdd.cl
__kernel void VectorAdd(__global const float* a, __global const float* b, __global float* c)
{
// get index into global data array
int iGID = get_global_id(0);
// add the vector elements
c[iGID] = a[iGID] + b[iGID];
}

Related

Resources