@@ -1996,7 +1996,156 @@ function renderer3D(p5, fn) {
19961996 p5 . Renderer3D = Renderer3D ;
19971997
19981998 /**
1999- * Creates a storage buffer for use in compute shaders.
1999+ * Creates a <a href="#/p5/p5.StorageBuffer">`p5.StorageBuffer`</a>, which is
2000+ * a block of data that shaders can read from, and compute shaders
2001+ * can also write to. This is only available in WebGPU mode.
2002+ *
2003+ * To read or write the data inside a shader, use
2004+ * <a href="#/p5/uniformStorage">`uniformStorage()`</a>. To update its contents
2005+ * from JavaScript, call <a href="#/p5.StorageBuffer/update">`.update()`</a>
2006+ * on the result with new data.
2007+ *
2008+ * Pass an array of objects to store a list of items, each with named
2009+ * properties. The properties can be numbers, arrays of numbers, vectors
2010+ * created with <a href="#/p5/createVector">`createVector()`</a>, or colors
2011+ * created with <a href="#/p5/color">`color()`</a>. Inside the shader, each
2012+ * item is accessed by index, and its properties are available by name.
2013+ *
2014+ * ```js example
2015+ * let instanceData;
2016+ * let instancesShader;
2017+ * let instance;
2018+ * let count = 5;
2019+ *
2020+ * async function setup() {
2021+ * await createCanvas(200, 200, WEBGPU);
2022+ *
2023+ * let data = [];
2024+ * for (let i = 0; i < count; i++) {
2025+ * data.push({
2026+ * position: createVector(
2027+ * random(-1, 1) * width / 2,
2028+ * random(-1, 1) * height / 2,
2029+ * 0,
2030+ * ),
2031+ * color: color(
2032+ * random(255),
2033+ * random(255),
2034+ * random(255)
2035+ * )
2036+ * });
2037+ * }
2038+ * instanceData = createStorage(data);
2039+ * instance = buildGeometry(drawInstance);
2040+ * instancesShader = buildMaterialShader(drawInstances);
2041+ * }
2042+ *
2043+ * function drawInstance() {
2044+ * sphere(15);
2045+ * }
2046+ *
2047+ * function drawInstances() {
2048+ * let data = uniformStorage(instanceData);
2049+ * let itemColor = sharedVec4();
2050+ *
2051+ * worldInputs.begin();
2052+ * let item = data[instanceID()];
2053+ * itemColor = item.color;
2054+ * worldInputs.position += item.position;
2055+ * worldInputs.end();
2056+ *
2057+ * finalColor.begin();
2058+ * finalColor.set(itemColor);
2059+ * finalColor.end();
2060+ * }
2061+ *
2062+ * function draw() {
2063+ * background(220);
2064+ * lights();
2065+ * noStroke();
2066+ * shader(instancesShader);
2067+ * model(instance, count);
2068+ * }
2069+ * ```
2070+ *
2071+ * You can also store a plain list of numbers by passing an array of numbers.
2072+ * Inside the shader, each number is accessed by index directly. To create an
2073+ * empty list to be filled in by a compute shader, pass a count instead.
2074+ *
2075+ * ```js example
2076+ * let cells;
2077+ * let nextCells;
2078+ * let gameShader;
2079+ * let displayShader;
2080+ * const W = 100;
2081+ * const H = 100;
2082+ *
2083+ * async function setup() {
2084+ * await createCanvas(100, 100, WEBGPU);
2085+ *
2086+ * let initial = new Float32Array(W * H);
2087+ * for (let i = 0; i < initial.length; i++) {
2088+ * initial[i] = random() > 0.7 ? 1 : 0;
2089+ * }
2090+ * cells = createStorage(initial);
2091+ * nextCells = createStorage(W * H);
2092+ *
2093+ * gameShader = buildComputeShader(simulate);
2094+ * displayShader = buildFilterShader(display);
2095+ * }
2096+ *
2097+ * function simulate() {
2098+ * let current = uniformStorage(() => cells);
2099+ * let next = uniformStorage(() => nextCells);
2100+ * let w = uniformInt(() => W);
2101+ * let h = uniformInt(() => H);
2102+ * let x = index.x;
2103+ * let y = index.y;
2104+ *
2105+ * let n = 0;
2106+ * for (let dy = -1; dy <= 1; dy++) {
2107+ * for (let dx = -1; dx <= 1; dx++) {
2108+ * if (dx != 0 || dy != 0) {
2109+ * let nx = (x + dx + w) % w;
2110+ * let ny = (y + dy + h) % h;
2111+ * n += current[ny * w + nx];
2112+ * }
2113+ * }
2114+ * }
2115+ *
2116+ * let alive = current[y * w + x];
2117+ * let nextOutput = 0;
2118+ * if (alive == 1) {
2119+ * if (abs(n - 2) < 0.1 || abs(n - 3) < 0.1) {
2120+ * nextOutput = 1;
2121+ * }
2122+ * } else {
2123+ * if (abs(n - 3) < 0.1) {
2124+ * nextOutput = 1;
2125+ * }
2126+ * }
2127+ * next[y * w + x] = nextOutput;
2128+ * }
2129+ *
2130+ * function display() {
2131+ * let data = uniformStorage(() => cells);
2132+ * let w = uniformInt(() => W);
2133+ * let h = uniformInt(() => H);
2134+ *
2135+ * filterColor.begin();
2136+ * let x = floor(filterColor.texCoord.x * w);
2137+ * let y = floor(filterColor.texCoord.y * h);
2138+ * let alive = data[y * w + x];
2139+ * filterColor.set([alive, alive, alive, 1]);
2140+ * filterColor.end();
2141+ * }
2142+ *
2143+ * function draw() {
2144+ * compute(gameShader, W, H);
2145+ * [nextCells, cells] = [cells, nextCells];
2146+ * filter(displayShader);
2147+ * }
2148+ * ```
20002149 *
20012150 * @method createStorage
20022151 * @submodule p5.strands
0 commit comments