{"record":{"id":"7cd72243aee291de","repo":"stride3d/stride","slug":"can-t-cast-from-m1-to-v2","errorCode":null,"errorMessage":"Can't cast from {m1} to {v2}","messagePattern":"Can't cast from (.+?) to (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs","lineNumber":574,"sourceCode":"                throw new InvalidOperationException($\"Can't cast from {v1} to {v2} (more components)\");\n            case (VectorType v1, VectorType v2) when v1.Size > v2.Size:\n                {\n                    Span<int> valuesTemp = stackalloc int[v2.Size];\n                    for (int i = 0; i < v2.Size; ++i)\n                        Insert(new OpCompositeExtract(context.GetOrRegister(v1.BaseType), valuesTemp[i] = context.Bound++, valueId, [i]));\n                    valueType = new VectorType(v1.BaseType, v2.Size);\n                    values[0] = Insert(new OpCompositeConstruct(context.GetOrRegister(valueType), context.Bound++, new LiteralArray<int>(valuesTemp))).ResultId;\n                    break;\n                }\n            case (VectorType v1, MatrixType m2) when v1.Size != m2.Rows * m2.Columns:\n                throw new InvalidOperationException($\"Can't cast from {v1} to {m2}\");\n            case (VectorType v1, MatrixType m2) when v1.Size == m2.Rows * m2.Columns:\n                throw new NotImplementedException($\"Cast from {v1} to {m2} is not implemented (even though it should be valid since number of components is same\");\n            case (MatrixType m1, ScalarType s2):\n                values[0] = Insert(new OpCompositeExtract(context.GetOrRegister(m1.BaseType), context.Bound++, valueId, [0, 0])).ResultId;\n                break;\n            case (MatrixType m1, VectorType v2) when v2.Size != m1.Rows * m1.Columns:\n                throw new InvalidOperationException($\"Can't cast from {m1} to {v2}\");\n            case (MatrixType m1, VectorType v2) when v2.Size == m1.Rows * m1.Columns:\n                throw new NotImplementedException($\"Cast from {m1} to {v2} is not implemented (even though it should be valid since number of components is same\");\n            case (MatrixType m1, MatrixType m2) when m1.Rows < m2.Rows || m1.Columns < m2.Columns:\n                throw new InvalidOperationException($\"Can't cast from {m1} to {m2} (larger matrix)\");\n            case (MatrixType m1, MatrixType m2) when m1.Rows >= m2.Rows && m1.Columns >= m2.Columns:\n                {\n                    // Extract each of the m2.Columns leading matrix columns, and shorten it from\n                    // m1.Rows down to m2.Rows components when the column vectors also shrink.\n                    Span<int> shuffleIndices = stackalloc int[m2.Rows];\n                    for (int j = 0; j < m2.Rows; ++j)\n                        shuffleIndices[j] = j;\n                    for (int i = 0; i < m2.Columns; ++i)\n                    {\n                        values[i] = Insert(new OpCompositeExtract(context.GetOrRegister(new VectorType(m1.BaseType, m1.Rows)), context.Bound++, valueId, [i])).ResultId;\n                        if (m1.Rows != m2.Rows)\n                        {\n                            values[i] = Insert(new OpVectorShuffle(context.GetOrRegister(new VectorType(m1.BaseType, m2.Rows)), context.Bound++, values[i], values[i], new(shuffleIndices))).ResultId;\n                        }","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L556-L592","documentation":"Thrown by Builder.Expressions.Convert when casting a matrix to a vector whose Size does not equal the matrix's total component count (Rows*Columns). Only same-component-count matrix->vector casts are contemplated, so a mismatched shape is rejected with InvalidOperationException.","triggerScenarios":"Calling Convert with (MatrixType, VectorType) where v2.Size != m1.Rows * m1.Columns, e.g. casting a 3x3 matrix to a vec2; reached via ConvertTexCoord, ConvertOffset, constant0/1, CompileSign, MultiplyConstant.","commonSituations":"Shader code assigning a matrix into a smaller vector variable, or passing a matrix where a flattened vector of different length is expected, typically from HLSL/C# type inconsistencies after a refactor.","solutions":["Adjust the shader so the target vector's size equals the matrix's Rows*Columns.","Extract explicit rows/columns in the shader (e.g. `mul` results, m[0].xy) instead of casting the whole matrix.","If a truncating cast is intended, perform it manually in shader code."],"exampleFix":"// before\nfloat2 v = (float2)myMat3x3;\n// after\nfloat2 v = myMat3x3[0].xy; // or mul result","handlingStrategy":"validation","validationCode":"bool canCastMatToVec(MatrixType m, VectorType v) => v.Size == m.Rows * m.Columns;","typeGuard":"bool IsMatToVecCastable(SymbolType from, SymbolType to) => from is MatrixType m && to is VectorType v && v.Size == m.Rows * m.Columns;","tryCatchPattern":null,"preventionTips":["Match vector size to matrix Rows*Columns before casting.","Extract explicit rows/columns instead of casting whole matrices.","Review variable declarations when matrices and vectors interact."],"tags":["spirv","type-cast","shader-compilation","matrix-vector"],"backgroundTag":"incompatible-source-type","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}