{"record":{"id":"1df88b2b59dee129","repo":"stride3d/stride","slug":"invalid-swizzle-for-scalar-type","errorCode":null,"errorMessage":"Invalid swizzle for scalar type","messagePattern":"Invalid swizzle for scalar type","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs","lineNumber":46,"sourceCode":"    {\n        var valueType = context.ReverseTypes[value.TypeId];\n        return valueType switch\n        {\n            ScalarType s => ApplyScalarSwizzles(context, value, s, swizzleIndices),\n            VectorType v => ApplyVectorSwizzles(context, value, v, swizzleIndices),\n            _ => throw new NotSupportedException($\"Unsupported type for swizzle: {valueType}\"),\n        };\n    }\n\n    public (SpirvValue, SymbolType) ApplyScalarSwizzles(SpirvContext context, SpirvValue value, ScalarType s, Span<int> swizzleIndices)\n    {\n        var resultType = new VectorType(s, swizzleIndices.Length);\n\n        Span<int> constructIndices = stackalloc int[swizzleIndices.Length];\n        for (int j = 0; j < constructIndices.Length; ++j)\n        {\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","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/shaders/Stride.Shaders.Parsers/Spirv/Building/Builder.Expressions.cs#L28-L64","documentation":"ApplyScalarSwizzles builds a vector by repeating a scalar value for each swizzle index. HLSL semantics allow repeating a scalar (any swizzle like .xxx works), but this implementation requires every index to be exactly 0; if any swizzle index is non-zero on a scalar it throws InvalidOperationException.","triggerScenarios":"Swizzling a scalar value with an index other than 0 (e.g. s.y or s.zw) reaching ApplySwizzles -> ApplyScalarSwizzles; swizzleIndices[j] != 0 for some j.","commonSituations":"Shader code that treats a float as if it were a vector, e.g. float f = 1.0; float2 v = f.yz; or frontend mislabeling a vector as scalar.","solutions":["Fix the shader so the value is a vector before swizzling (construct a vector explicitly)","Use only the x component when replicating a scalar: s.xxxx instead of s.yyyy","Verify the symbol's inferred type is a vector, not a scalar","Update builder to treat scalar broadcast as always valid per HLSL rules"],"exampleFix":"// before (HLSL)\nfloat s = 1.0;\nfloat2 v = s.yx;\n// after\nfloat s = 1.0;\nfloat2 v = float2(s, s); // or s.xx","handlingStrategy":"type-guard","validationCode":"// Scalars can only be replicated via index 0 (x)\nif (targetType is ScalarType && swizzle.Indices.Any(i => i != 0))\n    throw new ShaderSemanticException(\"Scalar swizzle components must be 'x'\");","typeGuard":"bool IsValidScalarSwizzle(ReadOnlySpan<int> idx) { foreach (var i in idx) if (i != 0) return false; return idx.Length > 0; }","tryCatchPattern":"try { var r = ApplySwizzles(context, scalarValue, swizzle); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Invalid swizzle for scalar type\") { throw new ShaderSemanticException(\"Cannot swizzle scalar with non-x components\", ex); }","preventionTips":["Use s.xxxx (or construct a vector) to replicate scalars, never s.yyy","Fix the value's declared type to a vector if component letters are intended","Lint shader code for swizzles on scalar-valued symbols"],"tags":["shader","swizzle","scalar","spirv"],"backgroundTag":"invalid-argument-value","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"}