{"record":{"id":"22dc132758a8826e","repo":"stride3d/stride","slug":"invalid-swizzle-for-vector-type","errorCode":null,"errorMessage":"Invalid swizzle for vector type","messagePattern":"Invalid swizzle for vector type","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs","lineNumber":62,"sourceCode":"        {\n            if (swizzleIndices[j] != 0)\n                throw new InvalidOperationException(\"Invalid swizzle for scalar type\");\n\n            constructIndices[j] = value.Id;\n        }\n\n        SpirvValue result;\n        var construct = InsertData(new OpCompositeConstruct(context.GetOrRegister(resultType), context.Bound++, new(constructIndices)));\n        result = new(construct);\n        return (result, resultType);\n    }\n\n    public (SpirvValue, SymbolType) ApplyVectorSwizzles(SpirvContext context, SpirvValue value, VectorType v, Span<int> swizzleIndices)\n    {\n        for (int j = 0; j < swizzleIndices.Length; ++j)\n        {\n            if (swizzleIndices[j] >= v.Size)\n                throw new InvalidOperationException(\"Invalid swizzle for vector type\");\n        }\n\n        if (swizzleIndices.Length > 1)\n        {\n            // Apply swizzle\n            var resultType = new VectorType(v.BaseType, swizzleIndices.Length);\n            var shuffle = InsertData(new OpVectorShuffle(context.GetOrRegister(resultType), context.Bound++, value.Id, value.Id, new(swizzleIndices)));\n            value = new(shuffle);\n\n            return (value, resultType);\n        }\n        else if (swizzleIndices.Length == 1)\n        {\n            // Apply swizzle\n            var resultType = v.BaseType;\n            var extract = InsertData(new OpCompositeExtract(context.GetOrRegister(resultType), context.Bound++, value.Id, [swizzleIndices[0]]));\n            value = new(extract);\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L44-L80","documentation":"ApplyVectorSwizzles validates each swizzle index against the vector's component count. If any index is >= the vector size (e.g. .zw on a float2), the builder throws InvalidOperationException because SPIR-V OpCompositeExtract/Construct would reference a nonexistent component.","triggerScenarios":"Swizzling a vector with component letters beyond its length, e.g. float2 v; v.z or v.xyz on a vec2; swizzleIndices[j] >= v.Size.","commonSituations":"Typos in shader code (using .xyz on vec2); refactoring that changed a vec4 to vec2 without updating swizzles; code generator emitting wrong swizzle letters.","solutions":["Fix the swizzle in the shader to stay within the vector's component count","Change the variable's declared type to a vector large enough for the swizzle","Check generated/frontend swizzle index computation for off-by-one errors","Add shader-side validation or tests for swizzles on small vectors"],"exampleFix":"// before (HLSL)\nfloat2 v = ...;\nfloat4 w = v.xyzw;\n// after\nfloat4 v4 = float4(v, 0.0, 0.0);\nfloat4 w = v4.xyzw;","handlingStrategy":"validation","validationCode":"// Validate swizzle indices against vector size before applying\nif (targetType is VectorType v && swizzle.Indices.Any(i => i >= v.Size))\n    throw new ShaderSemanticException($\"Swizzle component out of range for {v.Size}-component vector\");","typeGuard":"bool IsValidVectorSwizzle(VectorType v, ReadOnlySpan<int> idx) { foreach (var i in idx) if (i < 0 || i >= v.Size) return false; return idx.Length > 0; }","tryCatchPattern":"try { var r = ApplySwizzles(context, vecValue, swizzle); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Invalid swizzle for vector type\") { throw new ShaderSemanticException(\"Swizzle exceeds vector component count\", ex); }","preventionTips":["Match swizzle length to the vector size (no .zw on vec2)","Re-check swizzles after changing vector declarations/fields","Add semantic analysis tests covering swizzles on vec2/vec3/vec4"],"tags":["shader","swizzle","vector","spirv"],"backgroundTag":"value-out-of-range","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"}