stride3d/stride · error · InvalidOperationException

Can't cast from to (larger matrix)

Error message

Can't cast from {m1} to {m2} (larger matrix)

What it means

Thrown by Builder.Expressions.Convert when casting between two matrix types where the source matrix has fewer rows or columns than the target (an upcast to a 'larger matrix'). Conversion only supports shrinking or same-shape matrices, so growing a matrix is rejected with InvalidOperationException.

Solutions

  1. Fix the shader to use same-size or smaller target matrices.
  2. Construct the larger matrix explicitly from the smaller one's rows/identity fill in shader code.
  3. Verify the variable declarations in the shader match the intended math.

Example fix

// before
float3x3 m3 = (float3x3)m2;
// after
float3x3 m3 = float3x3(m2[0].xy0, m2[1].xy0, float3(0,0,1)); // explicit construction
Defensive patterns

Strategy: validation

Validate before calling

bool canCastMatToMat(MatrixType from, MatrixType to) => from.Rows >= to.Rows && from.Columns >= to.Columns;

Type guard

bool IsMatUpcast(SymbolType from, SymbolType to) => from is MatrixType a && to is MatrixType b && (a.Rows < b.Rows || a.Columns < b.Columns);

Prevention

When it happens

Trigger: Calling Convert with (MatrixType, MatrixType) where m1.Rows < m2.Rows || m1.Columns < m2.Columns, e.g. casting a 2x2 matrix to 3x3; reached via ConvertTexCoord, ConvertOffset, constant0/1, CompileSign, MultiplyConstant.

Common situations: Shader code widening matrices implicitly (e.g. assigning float2x2 into a float3x3 variable), unsupported in both HLSL semantics and this converter; usually indicates a shader type mistake.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs:578

                    for (int i = 0; i < v2.Size; ++i)
                        Insert(new OpCompositeExtract(context.GetOrRegister(v1.BaseType), valuesTemp[i] = context.Bound++, valueId, [i]));
                    valueType = new VectorType(v1.BaseType, v2.Size);
                    values[0] = Insert(new OpCompositeConstruct(context.GetOrRegister(valueType), context.Bound++, new LiteralArray<int>(valuesTemp))).ResultId;
                    break;
                }
            case (VectorType v1, MatrixType m2) when v1.Size != m2.Rows * m2.Columns:
                throw new InvalidOperationException($"Can't cast from {v1} to {m2}");
            case (VectorType v1, MatrixType m2) when v1.Size == m2.Rows * m2.Columns:
                throw new NotImplementedException($"Cast from {v1} to {m2} is not implemented (even though it should be valid since number of components is same");
            case (MatrixType m1, ScalarType s2):
                values[0] = Insert(new OpCompositeExtract(context.GetOrRegister(m1.BaseType), context.Bound++, valueId, [0, 0])).ResultId;
                break;
            case (MatrixType m1, VectorType v2) when v2.Size != m1.Rows * m1.Columns:
                throw new InvalidOperationException($"Can't cast from {m1} to {v2}");
            case (MatrixType m1, VectorType v2) when v2.Size == m1.Rows * m1.Columns:
                throw new NotImplementedException($"Cast from {m1} to {v2} is not implemented (even though it should be valid since number of components is same");
            case (MatrixType m1, MatrixType m2) when m1.Rows < m2.Rows || m1.Columns < m2.Columns:
                throw new InvalidOperationException($"Can't cast from {m1} to {m2} (larger matrix)");
            case (MatrixType m1, MatrixType m2) when m1.Rows >= m2.Rows && m1.Columns >= m2.Columns:
                {
                    // Extract each of the m2.Columns leading matrix columns, and shorten it from
                    // m1.Rows down to m2.Rows components when the column vectors also shrink.
                    Span<int> shuffleIndices = stackalloc int[m2.Rows];
                    for (int j = 0; j < m2.Rows; ++j)
                        shuffleIndices[j] = j;
                    for (int i = 0; i < m2.Columns; ++i)
                    {
                        values[i] = Insert(new OpCompositeExtract(context.GetOrRegister(new VectorType(m1.BaseType, m1.Rows)), context.Bound++, valueId, [i])).ResultId;
                        if (m1.Rows != m2.Rows)
                        {
                            values[i] = Insert(new OpVectorShuffle(context.GetOrRegister(new VectorType(m1.BaseType, m2.Rows)), context.Bound++, values[i], values[i], new(shuffleIndices))).ResultId;
                        }
                    }
                    valueType = new VectorType(m1.BaseType, m2.Rows);
                    valueCount = m2.Columns;
                    break;

View on GitHub (pinned to 96fad776d2)