# Flip On the Pipeline

*Task 1 of 5 · [Pipelines & Textures](https://gpu.rocks/learn/pipelines-and-textures-9f4aeaa5.md) · GPU.js Learn*

Until now, every kernel call ended the same way: the GPU finished computing,
then the whole result was **downloaded back to JavaScript** as a typed array.
That download is the expensive part — for a 512×512 grid it's a megabyte crossing the
bus on every single call.

`pipeline: true` changes the ending. The kernel still runs the same, but the
result *stays in GPU memory*, and what you get back is a **texture** —
a lightweight handle to data that never left the card. Log one and you'll see an object,
not numbers. When you actually want the values, you ask for the download explicitly with
`.toArray()` — and since the download is a real trip across the bus, it is
asynchronous like the kernel call itself: `await result.toArray()`.

One backend wrinkle to know: the CPU backend has no textures, so there a pipeline
kernel hands back a plain array — which has no `.toArray` at all. Mode-safe code
uses the same guard gpu.js uses internally, with the await in front of the call it guards:
`result.toArray ? await result.toArray() : result`. (Awaiting a plain array is a
no-op, so that one line is correct on every backend.)

## Goal

**Goal:** make the `boost` kernel a pipeline kernel, then
download its result explicitly and log the first sample.

## Requirements

- Add `pipeline: true` to the kernel settings
- Log the raw result — see what a texture looks like in the console
- Download the values with `await .toArray()`, using the mode-safe guard
- Log the first value as `console.log('first sample:', values[0])`

## Hint 1 — where does the flag go?

`pipeline: true` sits in the settings object, right next to
`output`. Nothing about the kernel function itself changes.

## Hint 2 — the mode-safe download

```js
const values = result.toArray ? await result.toArray() : result;
```

On a GPU backend this awaits `toArray()`; on the CPU backend
`result` is already an array and passes through untouched.

## Same idea elsewhere

A gpu.js texture is the same idea as a `GPUBuffer` you never map in
WebGPU, or device memory behind a pointer in CUDA and ROCm: the data has an address on
the card, and JavaScript only holds the ticket stub. `.toArray()` is the
explicit "map it back to the host" step.

## Starter code

```js
// Run this as-is first: the kernel returns plain numbers, which means
// every call ships the whole result back to JavaScript. Let's stop that.
const gpu = new GPU({ mode });

const boost = gpu.createKernel(function (signal) {
  return Math.min(signal[this.thread.x] * 1.5, 1);
}, {
  output: [256],
  // TODO: keep the result on the GPU
});

const result = await boost(signal);
console.log(result);

// TODO: `result` is about to become a texture. Download the values
// explicitly (mode-safe: result.toArray ? await result.toArray() : result)
// and log the first one as:  console.log('first sample:', values[0]);
```

---

Interactive version: https://gpu.rocks/learn/pipelines-and-textures-9f4aeaa5/1

[Next task](https://gpu.rocks/learn/pipelines-and-textures-9f4aeaa5/2.md)
