{"id":"recipe--compute-image","name":"Example · Paint an image with compute","owner":"yeho","package":"recipes","kind":"recipe","signature":"start.yh · compute example","description":"Map a one-dimensional buffer index to a two-dimensional pixel.","context":"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.","parameters":[],"returns":"","members":[],"tags":["cpu","gpu","recipe"],"updated":"2026-09-09","source":{"repository":"demonhunterlabs","path":"app/lib/yeho-walkthrough.ts"},"status":"complete-program","notes":["Use red alone for the output, then green alone. Compare horizontal, vertical, and combined gradients."],"examples":[{"id":"program","title":"Example · Paint an image with compute","description":"Map a one-dimensional buffer index to a two-dimensional pixel.","code":"[auto]\ncompute Paint(buffer of int pixels)\n{\n    int index = compute.index\n    if index >= pixels.count { return }\n    int y = index / 32\n    int x = index - y * 32\n    float red = Math.IntToFloat(x) / 31.0\n    float green = Math.IntToFloat(y) / 31.0\n    pixels[index] = Math.RoundToInt((red + green) * 0.5 * 255.0)\n}\n\nbuffer of int pixels\npixels.Resize(1024)\ncomputeTask job = dispatch Paint(pixels)\nwait job\nif job.failed { Console.Error(job.error.message) Process.Exit(1) }\nfor index from 0 < pixels.count { Console.Log(Text.From(pixels[index])) }","manifest":"manifestVersion = \"2\"\npackage = \"api.example.recipe__compute_image\"\nversion = \"0.1.0\"\nlanguageEdition = \"yeho-core-2026.1\"\n\n[app]\nkind = \"headless\"\n","tags":["gpu","cpu"],"run":"metal","expected":null,"verification":"type checked","sha256":"7862c0d0708f1e7ea647673539ccfbe44f26b8cc6cc77929c7e50341c6ba559f","checked":"2026-09-09","diagnostic":"","observedOutput":null,"checkedBackend":null}],"searchTerms":[],"url":"https://demonhunterlabs.com/academy/api?item=recipe--compute-image","json":"https://demonhunterlabs.com/reference/items/recipe--compute-image.json","markdown":"https://demonhunterlabs.com/reference/text/recipe--compute-image.md"}
