Max - Built-in functions - Shader Learning

Built-in functions

Max

Task

Using the max and step functions, paint only those pixels whose normalized x-coordinate is less than 0.25 or greater than 0.75.


Requirements


The shader should avoid using branching or conditional statements in its code, and instead rely on the step and max functions to determine the color of each pixel.

Theory

The max function in GLSL/HLSL is used to return the maximum value between two input values. It takes two parameters and returns the greater of the two values. Here's a detailed explanation of how the max function works:


Function Signature

float max(float x, float y);

How It Works


Here's a breakdown of how the max function works:

  • If x is greater than or equal to y, the function returns x.
  • If y is greater than x, the function returns y.

Example Usage

float a = 5.0;
float b = 3.0;
float result = max(a, b); // will be 5.0

The max function is widely used for tasks such as clamping values and simplifying conditional logic.