Add support for matrix-to-vector conversions in SkSL.
GLSL supports casting vec4 into mat2 and vice versa, so SkSL should have equivalent support. This CL allows the Compound constructor to take a matrix as input, and fixes up backends to do the right thing when a matrix shows up in the compound-constructor path. Change-Id: I13289ad0a27ba59bddc3706093820594efebc693 Bug: skia:12067 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/426003 Auto-Submit: John Stiles <johnstiles@google.com> Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: John Stiles <johnstiles@google.com>
This commit is contained in:
parent
13d34497db
commit
6de2e1db03
@ -69,6 +69,8 @@ sksl_error_tests = [
|
||||
"/sksl/errors/LayoutInFunctions.sksl",
|
||||
"/sksl/errors/LayoutMultiplePrimitiveTypes.sksl",
|
||||
"/sksl/errors/LayoutRepeatedQualifiers.sksl",
|
||||
"/sksl/errors/MatrixToVectorCast3x3.sksl",
|
||||
"/sksl/errors/MatrixToVectorCastTooSmall.sksl",
|
||||
"/sksl/errors/MismatchedNumbers.sksl",
|
||||
"/sksl/errors/ModifiersInStruct.sksl",
|
||||
"/sksl/errors/OpaqueTypeAssignment.sksl",
|
||||
|
7
resources/sksl/errors/MatrixToVectorCast3x3.sksl
Normal file
7
resources/sksl/errors/MatrixToVectorCast3x3.sksl
Normal file
@ -0,0 +1,7 @@
|
||||
// Expect 4 errors
|
||||
|
||||
const half3x3 testMatrix3x3 = half3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
half testScalar = half (testMatrix3x3);
|
||||
half2 testVec2 = half2(testMatrix3x3);
|
||||
half3 testVec3 = half3(testMatrix3x3);
|
||||
half3 testVec4 = half4(testMatrix3x3);
|
7
resources/sksl/errors/MatrixToVectorCastTooSmall.sksl
Normal file
7
resources/sksl/errors/MatrixToVectorCastTooSmall.sksl
Normal file
@ -0,0 +1,7 @@
|
||||
// Expect 3 errors
|
||||
|
||||
const half2x2 testMatrix2x2 = half2x2(1, 2, 3, 4);
|
||||
half testScalar = half (testMatrix2x2);
|
||||
half2 testVec2 = half2(testMatrix2x2);
|
||||
half3 testVec3 = half3(testMatrix2x2);
|
||||
half4 testVec4 = half4(testMatrix2x2); // not an error
|
@ -1012,15 +1012,55 @@ void MetalCodeGenerator::writeConstructorMatrixResize(const ConstructorMatrixRes
|
||||
|
||||
void MetalCodeGenerator::writeConstructorCompound(const ConstructorCompound& c,
|
||||
Precedence parentPrecedence) {
|
||||
if (c.type().isMatrix()) {
|
||||
if (c.type().isVector()) {
|
||||
this->writeConstructorCompoundVector(c, parentPrecedence);
|
||||
} else if (c.type().isMatrix()) {
|
||||
this->writeConstructorCompoundMatrix(c, parentPrecedence);
|
||||
} else {
|
||||
this->writeAnyConstructor(c, "(", ")", parentPrecedence);
|
||||
fErrors.error(c.fOffset, "unsupported compound constructor");
|
||||
}
|
||||
}
|
||||
|
||||
void MetalCodeGenerator::writeVectorFromMat2x2ConstructorHelper() {
|
||||
static constexpr char kCode[] =
|
||||
R"(float4 float4_from_float2x2(float2x2 x) {
|
||||
return float4(x[0].xy, x[1].xy);
|
||||
}
|
||||
)";
|
||||
|
||||
String name = "matrixCompMult";
|
||||
if (fHelpers.find("float4_from_float2x2") == fHelpers.end()) {
|
||||
fHelpers.insert("float4_from_float2x2");
|
||||
fExtraFunctions.writeText(kCode);
|
||||
}
|
||||
}
|
||||
|
||||
void MetalCodeGenerator::writeConstructorCompoundVector(const ConstructorCompound& c,
|
||||
Precedence parentPrecedence) {
|
||||
SkASSERT(c.type().isVector());
|
||||
|
||||
// Metal supports constructing vectors from a mix of scalars and vectors, but not matrices.
|
||||
// GLSL supports vec4(mat2x2), so we detect that case here and emit a helper function.
|
||||
if (c.type().columns() == 4 && c.argumentSpan().size() == 1) {
|
||||
const Expression& expr = *c.argumentSpan().front();
|
||||
if (expr.type().isMatrix()) {
|
||||
SkASSERT(expr.type().rows() == 2);
|
||||
SkASSERT(expr.type().columns() == 2);
|
||||
this->writeVectorFromMat2x2ConstructorHelper();
|
||||
this->write("float4_from_float2x2(");
|
||||
this->writeExpression(expr, Precedence::kSequence);
|
||||
this->write(")");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this->writeAnyConstructor(c, "(", ")", parentPrecedence);
|
||||
}
|
||||
|
||||
void MetalCodeGenerator::writeConstructorCompoundMatrix(const ConstructorCompound& c,
|
||||
Precedence parentPrecedence) {
|
||||
SkASSERT(c.type().isMatrix());
|
||||
|
||||
// Emit and invoke a matrix-constructor helper method if one is necessary.
|
||||
if (this->matrixConstructHelperIsNeeded(c)) {
|
||||
this->write(this->getMatrixConstructHelper(c));
|
||||
|
@ -180,6 +180,8 @@ protected:
|
||||
|
||||
void writeMatrixEqualityHelpers(const Type& left, const Type& right);
|
||||
|
||||
void writeVectorFromMat2x2ConstructorHelper();
|
||||
|
||||
void writeArrayEqualityHelpers(const Type& type);
|
||||
|
||||
void writeStructEqualityHelpers(const Type& type);
|
||||
@ -196,6 +198,8 @@ protected:
|
||||
|
||||
void writeConstructorCompound(const ConstructorCompound& c, Precedence parentPrecedence);
|
||||
|
||||
void writeConstructorCompoundVector(const ConstructorCompound& c, Precedence parentPrecedence);
|
||||
|
||||
void writeConstructorCompoundMatrix(const ConstructorCompound& c, Precedence parentPrecedence);
|
||||
|
||||
void writeConstructorMatrixResize(const ConstructorMatrixResize& c,
|
||||
|
@ -1605,23 +1605,35 @@ SpvId SPIRVCodeGenerator::writeVectorConstructor(const ConstructorCompound& c, O
|
||||
}
|
||||
|
||||
std::vector<SpvId> arguments;
|
||||
arguments.reserve(c.arguments().size());
|
||||
for (size_t i = 0; i < c.arguments().size(); i++) {
|
||||
const Type& argType = c.arguments()[i]->type();
|
||||
SkASSERT(componentType == argType.componentType());
|
||||
|
||||
if (argType.isVector()) {
|
||||
SpvId arg = this->writeExpression(*c.arguments()[i], out);
|
||||
if (argType.isMatrix()) {
|
||||
// CompositeConstruct cannot take a 2x2 matrix as an input, so we need to extract out
|
||||
// each scalar separately.
|
||||
SkASSERT(argType.rows() == 2);
|
||||
SkASSERT(argType.columns() == 2);
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
SpvId componentId = this->nextId(&componentType);
|
||||
this->writeInstruction(SpvOpCompositeExtract, this->getType(componentType),
|
||||
componentId, arg, j / 2, j % 2, out);
|
||||
arguments.push_back(componentId);
|
||||
}
|
||||
} else if (argType.isVector()) {
|
||||
// There's a bug in the Intel Vulkan driver where OpCompositeConstruct doesn't handle
|
||||
// vector arguments at all, so we always extract each vector component and pass them
|
||||
// into OpCompositeConstruct individually.
|
||||
SpvId vec = this->writeExpression(*c.arguments()[i], out);
|
||||
for (int j = 0; j < argType.columns(); j++) {
|
||||
SpvId componentId = this->nextId(&componentType);
|
||||
this->writeInstruction(SpvOpCompositeExtract, this->getType(componentType),
|
||||
componentId, vec, j, out);
|
||||
componentId, arg, j, out);
|
||||
arguments.push_back(componentId);
|
||||
}
|
||||
} else {
|
||||
arguments.push_back(this->writeExpression(*c.arguments()[i], out));
|
||||
arguments.push_back(arg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,9 +51,9 @@ static std::unique_ptr<Expression> convert_compound_constructor(const Context& c
|
||||
return ConstructorCompoundCast::Make(context, offset, type, std::move(argument));
|
||||
}
|
||||
} else if (argument->type().isMatrix()) {
|
||||
// A matrix constructor containing a single matrix can be a resize, typecast, or both.
|
||||
// GLSL lumps these into one category, but internally SkSL keeps them distinct.
|
||||
if (type.isMatrix()) {
|
||||
// A vector or matrix constructor containing a single matrix can be a resize, typecast,
|
||||
// or both. GLSL lumps these into one category, but internally SkSL keeps them distinct.
|
||||
if (type.isVector() || type.isMatrix()) {
|
||||
// First, handle type conversion. If the component types differ, synthesize the
|
||||
// destination type with the argument's rows/columns. (This will be a no-op if it's
|
||||
// already the right type.)
|
||||
@ -61,12 +61,20 @@ static std::unique_ptr<Expression> convert_compound_constructor(const Context& c
|
||||
context,
|
||||
argument->type().columns(),
|
||||
argument->type().rows());
|
||||
std::unique_ptr<Expression> typecast = ConstructorCompoundCast::Make(
|
||||
context, offset, typecastType, std::move(argument));
|
||||
argument = ConstructorCompoundCast::Make(context, offset, typecastType,
|
||||
std::move(argument));
|
||||
|
||||
// Next, wrap the typecasted expression in a matrix-resize constructor if the
|
||||
// sizes differ. (This will be a no-op if it's already the right size.)
|
||||
return ConstructorMatrixResize::Make(context, offset, type, std::move(typecast));
|
||||
// Next, wrap the typecasted expression in another constructor depending on its
|
||||
// type.
|
||||
if (type.isMatrix()) {
|
||||
// Casting a matrix type into another matrix type is a resize.
|
||||
return ConstructorMatrixResize::Make(context, offset, type,
|
||||
std::move(argument));
|
||||
}
|
||||
if (type.isVector() && type.columns() == 4 && argument->type().slotCount() == 4) {
|
||||
// Casting a 2x2 matrix into a 4-slot vector is compound construction.
|
||||
return ConstructorCompound::Make(context, offset, type, std::move(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -252,8 +252,7 @@ SKSL_TEST(SkSLMatrices, "shared/Matrices.sksl")
|
||||
SKSL_TEST_ES3(SkSLMatricesNonsquare, "shared/MatricesNonsquare.sksl")
|
||||
SKSL_TEST(SkSLMatrixEquality, "shared/MatrixEquality.sksl")
|
||||
SKSL_TEST(SkSLMatrixScalarSplat, "shared/MatrixScalarSplat.sksl")
|
||||
// TODO(skia:12067): casting a mat2 into a vec4 does not yet parse as a valid constructor
|
||||
//SKSL_TEST(SkSLMatrixToVectorCast, "shared/MatrixToVectorCast.sksl")
|
||||
SKSL_TEST(SkSLMatrixToVectorCast, "shared/MatrixToVectorCast.sksl")
|
||||
SKSL_TEST(SkSLMultipleAssignments, "shared/MultipleAssignments.sksl")
|
||||
SKSL_TEST(SkSLNegatedVectorLiteral, "shared/NegatedVectorLiteral.sksl")
|
||||
SKSL_TEST(SkSLNumberCasts, "shared/NumberCasts.sksl")
|
||||
|
7
tests/sksl/errors/MatrixToVectorCast3x3.glsl
Normal file
7
tests/sksl/errors/MatrixToVectorCast3x3.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
### Compilation failed:
|
||||
|
||||
error: 4: invalid argument to 'half' constructor (expected a number or bool, but found 'half3x3')
|
||||
error: 5: 'half3x3' is not a valid parameter to 'half2' constructor
|
||||
error: 6: 'half3x3' is not a valid parameter to 'half3' constructor
|
||||
error: 7: 'half3x3' is not a valid parameter to 'half4' constructor
|
||||
4 errors
|
6
tests/sksl/errors/MatrixToVectorCastTooSmall.glsl
Normal file
6
tests/sksl/errors/MatrixToVectorCastTooSmall.glsl
Normal file
@ -0,0 +1,6 @@
|
||||
### Compilation failed:
|
||||
|
||||
error: 4: invalid argument to 'half' constructor (expected a number or bool, but found 'half2x2')
|
||||
error: 5: 'half2x2' is not a valid parameter to 'half2' constructor
|
||||
error: 6: 'half2x2' is not a valid parameter to 'half3' constructor
|
||||
3 errors
|
@ -1,4 +1,142 @@
|
||||
### Compilation failed:
|
||||
|
||||
error: 9: 'half2x2' is not a valid parameter to 'half4' constructor
|
||||
1 error
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint Fragment %_entrypoint_v "_entrypoint" %sk_FragColor %sk_Clockwise
|
||||
OpExecutionMode %_entrypoint_v OriginUpperLeft
|
||||
OpName %sk_FragColor "sk_FragColor"
|
||||
OpName %sk_Clockwise "sk_Clockwise"
|
||||
OpName %_UniformBuffer "_UniformBuffer"
|
||||
OpMemberName %_UniformBuffer 0 "colorGreen"
|
||||
OpMemberName %_UniformBuffer 1 "colorRed"
|
||||
OpMemberName %_UniformBuffer 2 "testMatrix2x2"
|
||||
OpName %_entrypoint_v "_entrypoint_v"
|
||||
OpName %main "main"
|
||||
OpName %ok "ok"
|
||||
OpDecorate %sk_FragColor RelaxedPrecision
|
||||
OpDecorate %sk_FragColor Location 0
|
||||
OpDecorate %sk_FragColor Index 0
|
||||
OpDecorate %sk_Clockwise BuiltIn FrontFacing
|
||||
OpMemberDecorate %_UniformBuffer 0 Offset 0
|
||||
OpMemberDecorate %_UniformBuffer 0 RelaxedPrecision
|
||||
OpMemberDecorate %_UniformBuffer 1 Offset 16
|
||||
OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision
|
||||
OpMemberDecorate %_UniformBuffer 2 Offset 32
|
||||
OpMemberDecorate %_UniformBuffer 2 ColMajor
|
||||
OpMemberDecorate %_UniformBuffer 2 MatrixStride 16
|
||||
OpMemberDecorate %_UniformBuffer 2 RelaxedPrecision
|
||||
OpDecorate %_UniformBuffer Block
|
||||
OpDecorate %10 Binding 0
|
||||
OpDecorate %10 DescriptorSet 0
|
||||
OpDecorate %31 RelaxedPrecision
|
||||
OpDecorate %38 RelaxedPrecision
|
||||
OpDecorate %39 RelaxedPrecision
|
||||
OpDecorate %40 RelaxedPrecision
|
||||
OpDecorate %41 RelaxedPrecision
|
||||
OpDecorate %42 RelaxedPrecision
|
||||
OpDecorate %43 RelaxedPrecision
|
||||
OpDecorate %48 RelaxedPrecision
|
||||
OpDecorate %53 RelaxedPrecision
|
||||
OpDecorate %57 RelaxedPrecision
|
||||
OpDecorate %66 RelaxedPrecision
|
||||
OpDecorate %75 RelaxedPrecision
|
||||
OpDecorate %78 RelaxedPrecision
|
||||
OpDecorate %79 RelaxedPrecision
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_ptr_Output_v4float = OpTypePointer Output %v4float
|
||||
%sk_FragColor = OpVariable %_ptr_Output_v4float Output
|
||||
%bool = OpTypeBool
|
||||
%_ptr_Input_bool = OpTypePointer Input %bool
|
||||
%sk_Clockwise = OpVariable %_ptr_Input_bool Input
|
||||
%v2float = OpTypeVector %float 2
|
||||
%mat2v2float = OpTypeMatrix %v2float 2
|
||||
%_UniformBuffer = OpTypeStruct %v4float %v4float %mat2v2float
|
||||
%_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer
|
||||
%10 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
|
||||
%void = OpTypeVoid
|
||||
%17 = OpTypeFunction %void
|
||||
%float_0 = OpConstant %float 0
|
||||
%20 = OpConstantComposite %v2float %float_0 %float_0
|
||||
%_ptr_Function_v2float = OpTypePointer Function %v2float
|
||||
%24 = OpTypeFunction %v4float %_ptr_Function_v2float
|
||||
%_ptr_Function_bool = OpTypePointer Function %bool
|
||||
%true = OpConstantTrue %bool
|
||||
%false = OpConstantFalse %bool
|
||||
%_ptr_Uniform_mat2v2float = OpTypePointer Uniform %mat2v2float
|
||||
%int = OpTypeInt 32 1
|
||||
%int_2 = OpConstant %int 2
|
||||
%float_1 = OpConstant %float 1
|
||||
%float_2 = OpConstant %float 2
|
||||
%float_3 = OpConstant %float 3
|
||||
%float_4 = OpConstant %float 4
|
||||
%48 = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
|
||||
%v4bool = OpTypeVector %bool 4
|
||||
%_ptr_Function_v4float = OpTypePointer Function %v4float
|
||||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
|
||||
%int_0 = OpConstant %int 0
|
||||
%int_1 = OpConstant %int 1
|
||||
%_entrypoint_v = OpFunction %void None %17
|
||||
%18 = OpLabel
|
||||
%21 = OpVariable %_ptr_Function_v2float Function
|
||||
OpStore %21 %20
|
||||
%23 = OpFunctionCall %v4float %main %21
|
||||
OpStore %sk_FragColor %23
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%main = OpFunction %v4float None %24
|
||||
%25 = OpFunctionParameter %_ptr_Function_v2float
|
||||
%26 = OpLabel
|
||||
%ok = OpVariable %_ptr_Function_bool Function
|
||||
%67 = OpVariable %_ptr_Function_v4float Function
|
||||
OpStore %ok %true
|
||||
%31 = OpLoad %bool %ok
|
||||
OpSelectionMerge %33 None
|
||||
OpBranchConditional %31 %32 %33
|
||||
%32 = OpLabel
|
||||
%34 = OpAccessChain %_ptr_Uniform_mat2v2float %10 %int_2
|
||||
%38 = OpLoad %mat2v2float %34
|
||||
%39 = OpCompositeExtract %float %38 0 0
|
||||
%40 = OpCompositeExtract %float %38 0 1
|
||||
%41 = OpCompositeExtract %float %38 1 0
|
||||
%42 = OpCompositeExtract %float %38 1 1
|
||||
%43 = OpCompositeConstruct %v4float %39 %40 %41 %42
|
||||
%49 = OpFOrdEqual %v4bool %43 %48
|
||||
%51 = OpAll %bool %49
|
||||
OpBranch %33
|
||||
%33 = OpLabel
|
||||
%52 = OpPhi %bool %false %26 %51 %32
|
||||
OpStore %ok %52
|
||||
%53 = OpLoad %bool %ok
|
||||
OpSelectionMerge %55 None
|
||||
OpBranchConditional %53 %54 %55
|
||||
%54 = OpLabel
|
||||
%56 = OpAccessChain %_ptr_Uniform_mat2v2float %10 %int_2
|
||||
%57 = OpLoad %mat2v2float %56
|
||||
%58 = OpCompositeExtract %float %57 0 0
|
||||
%59 = OpCompositeExtract %float %57 0 1
|
||||
%60 = OpCompositeExtract %float %57 1 0
|
||||
%61 = OpCompositeExtract %float %57 1 1
|
||||
%62 = OpCompositeConstruct %v4float %58 %59 %60 %61
|
||||
%63 = OpFOrdEqual %v4bool %62 %48
|
||||
%64 = OpAll %bool %63
|
||||
OpBranch %55
|
||||
%55 = OpLabel
|
||||
%65 = OpPhi %bool %false %33 %64 %54
|
||||
OpStore %ok %65
|
||||
%66 = OpLoad %bool %ok
|
||||
OpSelectionMerge %71 None
|
||||
OpBranchConditional %66 %69 %70
|
||||
%69 = OpLabel
|
||||
%72 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
|
||||
%75 = OpLoad %v4float %72
|
||||
OpStore %67 %75
|
||||
OpBranch %71
|
||||
%70 = OpLabel
|
||||
%76 = OpAccessChain %_ptr_Uniform_v4float %10 %int_1
|
||||
%78 = OpLoad %v4float %76
|
||||
OpStore %67 %78
|
||||
OpBranch %71
|
||||
%71 = OpLabel
|
||||
%79 = OpLoad %v4float %67
|
||||
OpReturnValue %79
|
||||
OpFunctionEnd
|
||||
|
@ -1,4 +1,11 @@
|
||||
### Compilation failed:
|
||||
|
||||
error: 9: 'half2x2' is not a valid parameter to 'half4' constructor
|
||||
1 error
|
||||
out vec4 sk_FragColor;
|
||||
uniform vec4 colorGreen;
|
||||
uniform vec4 colorRed;
|
||||
uniform mat2 testMatrix2x2;
|
||||
vec4 main() {
|
||||
bool ok = true;
|
||||
ok = ok && vec4(testMatrix2x2) == vec4(1.0, 2.0, 3.0, 4.0);
|
||||
ok = ok && vec4(testMatrix2x2) == vec4(1.0, 2.0, 3.0, 4.0);
|
||||
return ok ? colorGreen : colorRed;
|
||||
}
|
||||
|
@ -1,4 +1,25 @@
|
||||
### Compilation failed:
|
||||
|
||||
error: 9: 'half2x2' is not a valid parameter to 'half4' constructor
|
||||
1 error
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
using namespace metal;
|
||||
struct Uniforms {
|
||||
float4 colorGreen;
|
||||
float4 colorRed;
|
||||
float2x2 testMatrix2x2;
|
||||
};
|
||||
struct Inputs {
|
||||
};
|
||||
struct Outputs {
|
||||
float4 sk_FragColor [[color(0)]];
|
||||
};
|
||||
float4 float4_from_float2x2(float2x2 x) {
|
||||
return float4(x[0].xy, x[1].xy);
|
||||
}
|
||||
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
|
||||
Outputs _out;
|
||||
(void)_out;
|
||||
bool ok = true;
|
||||
ok = ok && all(float4_from_float2x2(_uniforms.testMatrix2x2) == float4(1.0, 2.0, 3.0, 4.0));
|
||||
ok = ok && all(float4_from_float2x2(_uniforms.testMatrix2x2) == float4(1.0, 2.0, 3.0, 4.0));
|
||||
_out.sk_FragColor = ok ? _uniforms.colorGreen : _uniforms.colorRed;
|
||||
return _out;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user