Reaction-Diffusion Shaders: Prompting an AI Code Assistant and Repairing GLSL
Short answer. A useful AI-assisted reaction-diffusion shader begins with a precise description of the numerical model, its data textures and its rendering environment. The most important repairs usually concern texture feedback, coordinate handling, numerical stability and shader compatibility. Treat generated GLSL as a draft whose mathematical assumptions and rendering architecture must be made explicit.
- A reaction-diffusion shader needs an explicit numerical update rule.
- Two chemical concentration fields are commonly represented by two state variables.
- Ping-pong textures prevent a simulation pass from overwriting the state it still needs to read.
- Shader prompts should specify the host environment and texture conventions.
- Visual adjustments should follow numerical and architectural corrections.
Describe the simulation before requesting GLSL
Reaction-diffusion systems generate spatial patterns through the interaction of chemical-like quantities that spread and react over time. A common starting point is the Gray-Scott model, which tracks two concentrations, conventionally named U and V. Diffusion spreads each quantity through neighboring positions, while reaction terms describe how the concentrations interact. Feed and kill parameters regulate the system and influence the patterns that can emerge.
An AI code assistant needs this mathematical structure before it can produce useful shader code. A request for a beautiful organic shader leaves the essential implementation decisions unspecified. A stronger prompt identifies the model, state variables, update equations, boundary conditions, numerical parameters, texture format and target shader environment. It should also distinguish between the simulation pass and the image-rendering pass.
- Model: Gray-Scott reaction-diffusion.
- State: U and V concentration fields.
- Diffusion: neighboring texel sampling.
- Reaction: coupled concentration terms.
- Output: concentration-based color mapping.
The prompt as a technical specification
A useful prompt describes what the shader receives, what it returns and how state moves between frames. For example, specify a two-channel floating-point state texture, normalized texture coordinates, a separate previous-frame input and a target environment such as a GLSL fragment shader hosted by a visual programming application. Include the expected texture dimensions and whether coordinates require vertical inversion.
The prompt should also request clearly named uniforms for diffusion coefficients, feed rate, kill rate, time step and image resolution. Ask for the simulation equations to be written explicitly in the source comments. This makes it easier to distinguish a mathematical error from a host-application mismatch. If the environment supplies its own shader headers or output conventions, those requirements should be stated directly.
- Specify the exact shader language and host.
- Define input textures and output attachments.
- Name every simulation parameter.
- State the boundary condition.
- Request a separate simulation and display stage.
The Assistant Log: where generated code needs attention
The first repair target is usually the state update architecture. Reaction-diffusion is iterative: the current concentrations are calculated from the previous concentrations. Reading and writing the same texture during a rendering pass can create an undefined or unsupported feedback pattern. A safer architecture uses two state textures. The simulation reads texture A, writes the updated concentrations to texture B, and swaps their roles for the next frame. This is commonly called ping-pong rendering.
A second repair target is the neighborhood calculation. A Laplacian approximation combines samples from neighboring texels to estimate diffusion. The sampling offsets should be based on the actual texture resolution. Hard-coded offsets may behave differently at different dimensions. The assistant should expose inverse resolution as a uniform or calculate it consistently from the supplied texture size.
- Prompt: identify the model and update equations.
- Prompt: specify separate read and write textures.
- Repair: eliminate simultaneous state feedback.
- Repair: derive texel offsets from resolution.
- Repair: keep simulation and display logic separate.
The numerical core and its parameters
For the Gray-Scott model, a common formulation is dU/dt = Du times Laplacian(U) minus UV squared plus F times (1 minus U), and dV/dt = Dv times Laplacian(V) plus UV squared minus (F plus K) times V. Du and Dv are diffusion coefficients. F is the feed rate, and K is the kill rate. A shader approximates the derivatives over discrete time steps and stores the updated concentrations for the next frame.
The numerical time step matters. Large updates can cause unstable concentrations or values outside the expected range. Clamping may prevent extreme values from spreading through the texture, but it does not correct an unsuitable integration step. Start with conservative values, expose the time step as a parameter and keep the simulation update separate from frame-rate-dependent visual effects. Parameter ranges depend on the discretization and implementation.
- Du: 0.16 as an illustrative diffusion coefficient.
- Dv: 0.08 as an illustrative diffusion coefficient.
- F: 0.035 as an example feed rate.
- K: 0.060 as an example kill rate.
- Time step: begin with a conservative value such as 1.0.
From concentration data to a visual surface
The simulation texture contains numerical state, not a finished image. A display pass converts the concentrations into color, brightness or another visual property. For example, map V to a gradient, use U to control the background and add a threshold to emphasize regions where the second concentration becomes prominent. A smooth interpolation between colors can make the resulting structures easier to read without changing the simulation itself.
Keeping these stages separate protects the underlying model from decorative changes. Color grading, contrast, edge emphasis and coordinate distortion can be developed independently. This is also a useful way to evaluate suggestions from an AI code assistant: a request for more contrast should not silently change the reaction equations, while a request for more intricate pattern behavior should identify which numerical parameters are intended to change.
A disciplined repair sequence
When generated GLSL fails, address problems in dependency order. First establish that the shader compiles in its intended environment. Then confirm that the input textures contain valid concentration data and that the output texture receives updated values. Check the coordinate system, sampling offsets and texture swapping before adjusting the reaction parameters. A visually blank output may be a rendering problem, a state-initialization problem or a numerical problem.
Useful prompts ask the assistant to explain one specific failure and propose a minimal correction. Request the relevant code fragment, the assumption behind the change and any host-specific requirement. Keep the original source available and record the parameter values alongside the seed and initialization pattern. Begin the next iteration by changing one numerical parameter, such as F from 0.035 to 0.040, while keeping the rendering stage constant.
Questions readers ask
What makes reaction-diffusion useful for generative art?
Its local rules create complex spatial structures from relatively simple interactions. The resulting patterns can resemble spots, branching networks, cellular boundaries and other organic forms.
Why does a reaction-diffusion shader need two textures?
A conventional ping-pong arrangement separates the state being sampled from the state being written. This avoids relying on unsupported simultaneous texture reads and writes within the same rendering pass.
How should initialization be handled?
Start with a mostly uniform U field and a small localized disturbance in V. The size, shape and placement of that disturbance can be controlled with circles, rectangles, noise or procedural masks.
Can an assistant translate a shader between environments?
It can help translate syntax and identify equivalent operations, but host-specific inputs, uniforms, texture bindings and output conventions still require explicit attention. GLSL dialects and application interfaces are not interchangeable.