{"record":{"id":"c4d686946f1016a5","repo":"stride3d/stride","slug":"can-t-cast-from-v1-to-m2","errorCode":null,"errorMessage":"Can't cast from {v1} to {m2}","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":567,"sourceCode":"                    valueType = v1.BaseType;\n                    break;\n                }\n            case (VectorType v1, VectorType v2) when v1.Size == v2.Size:\n                values[0] = valueId;\n                break;\n            case (VectorType v1, VectorType v2) when v1.Size < v2.Size:\n                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;","sourceCodeStart":549,"sourceCodeEnd":585,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L549-L585","documentation":"Thrown by Builder.Expressions.Convert when casting a SPIR-V vector value to a matrix type whose total component count (Rows*Columns) does not equal the vector's Size. The converter only supports casts where the element counts match exactly, so a shape mismatch is rejected with InvalidOperationException.","triggerScenarios":"Calling Convert (directly or via ConvertTexCoord, ConvertOffset, constant0/1, CompileSign, MultiplyConstant) with a vector-to-matrix cast where vector.Size != m2.Rows * m2.Columns, e.g. casting vec4 to a 2x3 matrix.","commonSituations":"Shader source performing assignments between vectors and matrices of differing component counts (e.g. HLSL `(float2x2)v` where v is a float3), often after refactoring a shader's data types or misreading HLSL matrix semantics.","solutions":["Fix the shader source so the vector length equals the target matrix Rows*Columns.","Construct the matrix explicitly in the shader (e.g. float2x3(v.x, v.y, v.z, ...) with explicit components) instead of relying on implicit cast.","If the sizes do match, upgrade: this branch currently throws NotImplementedException in Stride; check Stride version and patch Builder.Expressions.cs to implement the vector->matrix construct."],"exampleFix":"// before (shader)\nfloat2x2 m = (float2x2)myVec3;\n// after\nfloat2x2 m = float2x2(myVec3.xy, myVec3.xz); // explicit construction","handlingStrategy":"validation","validationCode":"// before Convert: check shape compatibility\nbool canCastVecToMat(VectorType v, MatrixType m) => v.Size == m.Rows * m.Columns;","typeGuard":"bool IsVecToMatCastable(SymbolType from, SymbolType to) => from is VectorType v && to is MatrixType m && v.Size == m.Rows * m.Columns;","tryCatchPattern":null,"preventionTips":["Keep vector lengths equal to target matrix component counts when casting.","Prefer explicit matrix construction over implicit casts in shaders.","Verify matrix dimensions after refactoring shader types."],"tags":["spirv","type-cast","shader-compilation","vector-matrix"],"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"}