Skip to content

Incorrect looping in shader on metal  #5067

Open
@pieper

Description

@pieper

Description
A triple-nested for loop behaves differently on a mac pro with AMD cards than it does on other platforms.

Repro steps

On most systems it runs normally (tested window and linux with nvidia GPUs and mac with M2 GPU).

On a mac pro with AMD Radeon Pro W5700X 16 GB, macOS 13.4, the error condition happen.

On the AMD mac, the max of neighborsVisited is 4 when it is 8 everywhere else. Also the value of ii is zero, not 2.

fn main(@builtin(global_invocation_id) id: vec3<u32>) {

    let idi32 : vec3<i32> = vec3<i32>(id);
    var dimensions : vec3<i32> = vec3<i32>(3,3,3);
    var kk : i32; var jj : i32; var ii : i32;
    var neighborsVisited : i32 = 0;
    for (kk = -1; kk < 2; kk += 1) {
        for (jj = -1; jj < 2; jj += 1) {
            for (ii = -1; ii < 2; ii += 1) {
                if ( (kk != 0 && jj != 0 && ii != 0)
                      && ((idi32.z + kk) >= 0 && (idi32.z + kk) < dimensions.z)
                      && ((idi32.y + jj) >= 0 && (idi32.y + jj) < dimensions.y)
                      && ((idi32.x + ii) >= 0 && (idi32.x + ii) < dimensions.x) ) {
                    neighborsVisited += 1;
                }
            }
        }
    }
    displacements[idi32.x + idi32.y * 3 + idi32.z * 9] = vec4<f32>(f32(kk), f32(jj), f32(ii), f32(neighborsVisited));
}

Expected vs observed behavior

The code should work the same everywhere : )

Extra materials

A full wgpu python script that demonstrates the issue is here.

Platform

AMD Radeon Pro W5700X 16 GB, macOS 13.4

wgpu '0.13.2'

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend: metalIssues with Metalexternal: driver-bugA driver is causing the bug, though we may still want to work around itlang: MetalMetal Shading Language

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions