stride3d/stride · error · InvalidOperationException

Invalid swizzle character

Error message

Invalid swizzle character '{c}'

What it means

Swizzle components must be one of x/y/z/w or r/g/b/a; this helper maps each character to its component index (0–3). Any other character in a swizzle accessor throws InvalidOperationException at Expression.cs:1532.

Solutions

  1. Correct the swizzle character to x/y/z/w (or the equivalent r/g/b/a).
  2. Remember only 4-component letters are valid; for larger types index components individually or use supported accessors.
  3. If the swizzle looks right, check for invisible/typo characters (e.g. digits or unicode lookalikes) in the shader source.

Example fix

// before
float4 v; float f = v.q;
// after
float4 v; float f = v.w;
Defensive patterns

Strategy: validation

Validate before calling

// validate swizzle characters before compiling
bool valid = swizzle.All(c => "xyzwrgba".Contains(c));
if (!valid) throw new ShaderCompileHint($"Bad swizzle: {swizzle}");

Type guard

static bool IsValidSwizzleChar(char c) => c is 'x' or 'y' or 'z' or 'w' or 'r' or 'g' or 'b' or 'a';

Try / catch

try { result = CompileExpression(expr, table, compiler); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Invalid swizzle character"))
{ diagnostics.Report(expr.Info, ex.Message); }

Prevention

When it happens

Trigger: Writing a swizzle like `v.q`, `v.0`, or any character outside {x,y,z,w,r,g,b,a} on a vector value in SDSL shader code.

Common situations: Typos in swizzle names, copying GLSL/HLSL code that uses unsupported swizzle letters, or mixing rgba and xyzw incorrectly (e.g. `v.xg` is fine but `v.q` is not).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/387decb2c2cb16ba. Report an issue: GitHub.

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Parsers/Parsing/SDSL/AST/Expression.cs:1532

        var textureCoordSize = textureType.CoordinateDimension + (hasLod ? 1 : 0);
        spirvValue = builder.Convert(context, spirvValue, baseType.GetVectorOrScalar(textureCoordSize));
        return spirvValue;
    }

    SpirvValue ConvertOffset(SpirvContext context, SpirvBuilder builder, TextureType textureType, SpirvValue spirvValue)
    {
        spirvValue = builder.Convert(context, spirvValue, ScalarType.Int.GetVectorOrScalar(textureType.BaseDimension));
        return spirvValue;
    }

    private static int ConvertSwizzle(char c)
        => c switch
        {
            'x' or 'r' => 0,
            'y' or 'g' => 1,
            'z' or 'b' => 2,
            'w' or 'a' => 3,
            _ => throw new InvalidOperationException($"Invalid swizzle character '{c}'"),
        };

    public override string ToString() => ToString(Accessors.Count);

    public string ToString(int accessorCount)
    {
        var builder = new StringBuilder().Append(Source);
        for (int i = 0; i < accessorCount; i++)
        {
            Expression? a = Accessors[i];
            if (a is IndexerExpression)
                builder.Append(a);
            else if (a is PostfixIncrement)
                builder.Append(a);
            else
                builder.Append('.').Append(a);
        }

View on GitHub (pinned to 96fad776d2)