Intrinsics1 min read

load

Loads a texel directly without filtering.

Reading Time
1 min
Word Count
178
Sections
12
Try It Live

Test load in a live shader

Open the playground, start from a visual preset, and wire load into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo Pending

This intrinsic does not yet have a self-contained interactive preview. Compute-only and external-resource intrinsics need a different demo path than the current fragment and 3D showcases.

The load function reads a single texel from a texture at integer coordinates without any filtering or interpolation.

Signature

bwsl
load :: (texture2D tex, int2 coord, int mip) -> float4 {...}
load :: (texture3D tex, int3 coord, int mip) -> float4 {...}

Parameters

ParameterTypeDescription
textexture*The texture to read from
coordint2/3Integer texel coordinates
mipintMip level to read from

Return Value

Returns the exact texel value without filtering.

Example

bwsl
pipeline ImageProcessing {
fragment {
// Read exact pixel values for processing
int2 pixelCoord = int2(input.position.xy);
float4 center = load(inputTex, pixelCoord, 0);
float4 right = load(inputTex, pixelCoord + int2(1, 0), 0);
float4 down = load(inputTex, pixelCoord + int2(0, 1), 0);
// Edge detection
float4 dx = right - center;
float4 dy = down - center;
float edge = length(dx.rgb) + length(dy.rgb);
output.color = float4(float3(edge), 1.0);
}
}

Common Use Cases

Pixel-Perfect Rendering

bwsl
// Read exact pixel for UI/sprites
int2 texelPos = int2(floor(uv * textureSize));
float4 pixel = load(uiTex, texelPos, 0);

Image Processing

bwsl
// Convolution kernel
float4 sum = float4(0.0);
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
float4 texel = load(tex, coord + int2(x, y), 0);
sum += texel * kernel[y+1][x+1];
}
}

Compute Shader Access

bwsl
pass "Main" {
compute "Main" [8, 8, 1] {
// Read input texture
int2 gid = int2(input.global_id.xy);
float4 input = load(inputTex, gid, 0);
// Process and write output
store(outputTex, gid, processedValue);
}
}

Mipmap Chain Reading

bwsl
// Read from specific mip levels
float4 mip0 = load(tex, coord, 0);
float4 mip1 = load(tex, coord / 2, 1);
float4 mip2 = load(tex, coord / 4, 2);

G-Buffer Access

bwsl
// Read G-buffer at pixel location
int2 pixel = int2(screenPos);
float4 albedo = load(gBufferAlbedo, pixel, 0);
float4 normal = load(gBufferNormal, pixel, 0);
float depth = load(gBufferDepth, pixel, 0).r;

No Filtering

Unlike sample, load performs no filtering or interpolation. It returns the exact texel value at the given coordinates.

Bounds Checking

Accessing coordinates outside the texture bounds produces undefined results. Ensure coordinates are within valid range.

When to Use

Use load when:

  • You need exact pixel values (no filtering)
  • Implementing custom filtering algorithms
  • Working in compute shaders on textures
  • Reading from integer-format textures

Compiled Output

When compiled to GLSL:

glsl
texelFetch(tex, coord, mip)

When compiled to HLSL:

hlsl
tex.Load(int3(coord, mip))

When compiled to Metal:

metal
tex.read(coord, mip)

See Also

  • sample - Filtered sampling
  • store - Write to storage texture
  • gather - Gather 2x2 texels