recipes · recipe ·

Example · Paint an image with compute

Map a one-dimensional buffer index to a two-dimensional pixel.

cpugpurecipe

An image is a grid, but a buffer is a sequence. We can store rows one after another. For a width of 32, index / 32 gives the row. Subtract row * 32 from index to find the column. This kernel fills a 32 by 32 grayscale image. The pixel value combines horizontal and vertical ramps, producing a diagonal gradient. The host prints the integer pixels; the displayed image was saved from the actual Metal output.

start.yh · compute example

Status: complete-program

Example · Paint an image with compute

Map a one-dimensional buffer index to a two-dimensional pixel.

gpu · cpu · type checked

[auto]
compute Paint(buffer of int pixels)
{
    int index = compute.index
    if index >= pixels.count { return }
    int y = index / 32
    int x = index - y * 32
    float red = Math.IntToFloat(x) / 31.0
    float green = Math.IntToFloat(y) / 31.0
    pixels[index] = Math.RoundToInt((red + green) * 0.5 * 255.0)
}

buffer of int pixels
pixels.Resize(1024)
computeTask job = dispatch Paint(pixels)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }
for index from 0 < pixels.count { Console.Log(Text.From(pixels[index])) }
project.mech
manifestVersion = "2"
package = "api.example.recipe__compute_image"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"
Notes and source

Use red alone for the output, then green alone. Compare horizontal, vertical, and combined gradients.

demonhunterlabs/app/lib/yeho-walkthrough.ts