Skip to content

Helpers, Pointers, Vectors, Structs

Bogdan edited this page Jun 21, 2026 · 2 revisions

@CCode

Use @CCode for reusable GPU-side helper logic or raw backend bodies.

Example:

@CCode(inline = true)
public static float lerp(float a, float b, float t) {
    return a + (b - a) * t;
}

Raw backend body example:

@CCode(code = """
        return (*ptr) * scale;
        """)
public static native float scaled(FloatPtr ptr, float scale);

Pointer Wrappers

Pointer wrappers are useful when helper code needs mutable scalar-by-reference semantics.

Available wrappers:

  • BytePtr
  • CharPtr
  • ShortPtr
  • IntPtr
  • LongPtr
  • FloatPtr
  • DoublePtr

Example:

@CCode
public static void fill(FloatPtr ptr) {
    ptr.value = 42.0f;
}

Vector Types

Vector wrappers mirror common OpenCL vector types.

Available vector types:

  • Float2, Float3, Float4
  • Int2, Int3, Int4
  • Long2, Long3, Long4
  • Double2, Double3, Double4

Example:

Float2 a = new Float2(input[id], input[id] * 2.0f);
Float2 b = new Float2(1.0f);
Float2 c = add(a, b);
output[id] = c.x + c.y;

@GPUStruct

Use @GPUStruct for OpenCL struct-like data.

Rules:

  • scalar fields, vector fields, or nested @GPUStruct fields only
  • avoid reserved local names like struct

Example:

@GPUStruct
public static class Vec2 {
    public double x;
    public double y;
}

@GPUStruct
public static class Sample {
    public Vec2 point;
    public double bias;
    public int count;
}

Typical kernel usage:

Vec2 point = new Vec2(input[id], input[id] * 2.0);
Sample sample = new Sample(point, 20.5, 10);
output[id] = sample.point.x + sample.point.y + sample.bias + sample.count;

Reusable Libraries

For reusable helper packs across compilations, JavaToGpu also supports library-style metadata export through:

  • @CCodeLibrary
  • @GPUIntrinsicLibrary

See Also

Clone this wiki locally