{"record":{"id":"6604bd2ca044df8b","repo":"stride3d/stride","slug":"cast-from-m1-to-v2-is-not-implemented-even-though-it-should","errorCode":null,"errorMessage":"Cast from {m1} to {v2} is not implemented (even though it should be valid since number of components is same","messagePattern":"Cast from (.+?) to (.+?) is not implemented \\(even though it should be valid since number of components is same","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs","lineNumber":576,"sourceCode":"                {\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                        }\n                    }\n                    valueType = new VectorType(m1.BaseType, m2.Rows);","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L558-L594","documentation":"Thrown by Builder.Expressions.Convert when casting a matrix to a vector of the same total component count. The shape is valid, but the converter has no SPIR-V emission for this case, so it throws NotImplementedException noting it 'should be valid'.","triggerScenarios":"Calling Convert with (MatrixType, VectorType) where v2.Size == m1.Rows * m1.Columns (e.g. 2x2 matrix -> vec4); reached through ConvertTexCoord, ConvertOffset, CompileSign, MultiplyConstant, etc.","commonSituations":"HLSL-style flattening casts such as `(float4)myMat2x2` in shader source; an upstream Stride limitation rather than caller error.","solutions":["Flatten manually in shader code (e.g. float4(m[0], m[1])).","Implement the branch in Builder.Expressions.cs using OpCompositeExtract per column plus OpCompositeConstruct.","Upgrade Stride to a version implementing this cast."],"exampleFix":"// before\nfloat4 v = (float4)m2x2;\n// after\nfloat4 v = float4(m2x2[0], m2x2[1]);","handlingStrategy":"fallback","validationCode":"// flatten manually instead of casting\n// float4 v = float4(m[0], m[1]);","typeGuard":"bool IsUnsupportedMatToVec(SymbolType from, SymbolType to) => from is MatrixType m && to is VectorType v && v.Size == m.Rows * m.Columns;","tryCatchPattern":"try { Compile(shader); }\ncatch (NotImplementedException e) { Log.Warn(e.Message); /* replace cast in shader source */ }","preventionTips":["Flatten matrices with explicit component expressions in shaders.","Avoid (vector)matrix casts; they are unimplemented in Stride's SPIR-V backend.","Keep a list of supported casts when writing shaders for this pipeline."],"tags":["spirv","not-implemented","type-cast","shader-compilation"],"backgroundTag":"method-not-implemented","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"}