Add SkSL test for uniform arrays.

These weren't used anywhere in our test suite.

Change-Id: I35e8607ad2dbddf8f403668bd2b2636a8964d304
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/455777
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:
John Stiles 2021-10-04 17:02:16 -04:00 committed by SkCQ
parent 211b336da0
commit e5d4c43561
5 changed files with 130 additions and 19 deletions

View File

@ -1,3 +1,11 @@
uniform float arr[3];
uniform float testArray[5];
uniform half4 colorGreen, colorRed;
void main() {}
half4 main(float2 coords) {
for (int index=0; index<5; ++index) {
if (testArray[index] != float(index + 1)) {
return colorRed;
}
}
return colorGreen;
}

View File

@ -37,6 +37,14 @@ static void set_uniform(SkRuntimeShaderBuilder* builder, const char* name, const
}
}
template <typename T>
static void set_uniform_array(SkRuntimeShaderBuilder* builder, const char* name, SkSpan<T> values) {
SkRuntimeShaderBuilder::BuilderUniform uniform = builder->uniform(name);
if (uniform.fVar) {
uniform.set(values.data(), values.size());
}
}
static void test_one_permutation(skiatest::Reporter* r,
SkSurface* surface,
const char* testFile,
@ -56,6 +64,8 @@ static void test_one_permutation(skiatest::Reporter* r,
return;
}
static constexpr float kArray[5] = {1, 2, 3, 4, 5};
SkRuntimeShaderBuilder builder(result.effect);
set_uniform(&builder, "colorBlack", SkV4{0, 0, 0, 1});
set_uniform(&builder, "colorRed", SkV4{1, 0, 0, 1});
@ -74,6 +84,7 @@ static void test_one_permutation(skiatest::Reporter* r,
set_uniform(&builder, "testMatrix3x3", std::array<float,9>{1, 2, 3,
4, 5, 6,
7, 8, 9});
set_uniform_array(&builder, "testArray", SkMakeSpan(kArray));
sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
if (!shader) {
@ -341,6 +352,7 @@ SKSL_TEST(SkSLTernaryAsLValueEntirelyFoldable, "shared/TernaryAsLValueEntirelyFo
SKSL_TEST(SkSLTernaryAsLValueFoldableTest, "shared/TernaryAsLValueFoldableTest.sksl")
SKSL_TEST(SkSLTernaryExpression, "shared/TernaryExpression.sksl")
SKSL_TEST(SkSLUnaryPositiveNegative, "shared/UnaryPositiveNegative.sksl")
SKSL_TEST(SkSLUniformArray, "shared/UniformArray.sksl")
SKSL_TEST(SkSLUnusedVariables, "shared/UnusedVariables.sksl")
SKSL_TEST(SkSLVectorConstructors, "shared/VectorConstructors.sksl")
SKSL_TEST(SkSLVectorToMatrixCast, "shared/VectorToMatrixCast.sksl")

View File

@ -1,31 +1,104 @@
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %sk_Clockwise
OpExecutionMode %main OriginUpperLeft
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 "arr"
OpMemberName %_UniformBuffer 0 "testArray"
OpMemberName %_UniformBuffer 1 "colorGreen"
OpMemberName %_UniformBuffer 2 "colorRed"
OpName %_entrypoint_v "_entrypoint_v"
OpName %main "main"
OpName %index "index"
OpDecorate %sk_FragColor RelaxedPrecision
OpDecorate %sk_FragColor Location 0
OpDecorate %sk_FragColor Index 0
OpDecorate %sk_Clockwise BuiltIn FrontFacing
OpDecorate %_arr_float_int_3 ArrayStride 16
OpDecorate %_arr_float_int_5 ArrayStride 16
OpMemberDecorate %_UniformBuffer 0 Offset 0
OpMemberDecorate %_UniformBuffer 1 Offset 80
OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision
OpMemberDecorate %_UniformBuffer 2 Offset 96
OpMemberDecorate %_UniformBuffer 2 RelaxedPrecision
OpDecorate %_UniformBuffer Block
OpDecorate %6 Binding 0
OpDecorate %6 DescriptorSet 0
OpDecorate %10 Binding 0
OpDecorate %10 DescriptorSet 0
OpDecorate %55 RelaxedPrecision
OpDecorate %59 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
%float = OpTypeFloat 32
%int = OpTypeInt 32 1
%int_3 = OpConstant %int 3
%_arr_float_int_3 = OpTypeArray %float %int_3
%_UniformBuffer = OpTypeStruct %_arr_float_int_3
%int_5 = OpConstant %int 5
%_arr_float_int_5 = OpTypeArray %float %int_5
%_UniformBuffer = OpTypeStruct %_arr_float_int_5 %v4float %v4float
%_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer
%6 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
%10 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
%void = OpTypeVoid
%14 = OpTypeFunction %void
%main = OpFunction %void None %14
%15 = OpLabel
%18 = OpTypeFunction %void
%v2float = OpTypeVector %float 2
%float_0 = OpConstant %float 0
%22 = OpConstantComposite %v2float %float_0 %float_0
%_ptr_Function_v2float = OpTypePointer Function %v2float
%26 = OpTypeFunction %v4float %_ptr_Function_v2float
%_ptr_Function_int = OpTypePointer Function %int
%int_0 = OpConstant %int 0
%_ptr_Uniform__arr_float_int_5 = OpTypePointer Uniform %_arr_float_int_5
%_ptr_Uniform_float = OpTypePointer Uniform %float
%int_1 = OpConstant %int 1
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
%int_2 = OpConstant %int 2
%_entrypoint_v = OpFunction %void None %18
%19 = OpLabel
%23 = OpVariable %_ptr_Function_v2float Function
OpStore %23 %22
%25 = OpFunctionCall %v4float %main %23
OpStore %sk_FragColor %25
OpReturn
OpFunctionEnd
%main = OpFunction %v4float None %26
%27 = OpFunctionParameter %_ptr_Function_v2float
%28 = OpLabel
%index = OpVariable %_ptr_Function_int Function
OpStore %index %int_0
OpBranch %32
%32 = OpLabel
OpLoopMerge %36 %35 None
OpBranch %33
%33 = OpLabel
%37 = OpLoad %int %index
%38 = OpSLessThan %bool %37 %int_5
OpBranchConditional %38 %34 %36
%34 = OpLabel
%39 = OpAccessChain %_ptr_Uniform__arr_float_int_5 %10 %int_0
%41 = OpLoad %int %index
%42 = OpAccessChain %_ptr_Uniform_float %39 %41
%44 = OpLoad %float %42
%45 = OpLoad %int %index
%47 = OpIAdd %int %45 %int_1
%48 = OpConvertSToF %float %47
%49 = OpFOrdNotEqual %bool %44 %48
OpSelectionMerge %51 None
OpBranchConditional %49 %50 %51
%50 = OpLabel
%52 = OpAccessChain %_ptr_Uniform_v4float %10 %int_2
%55 = OpLoad %v4float %52
OpReturnValue %55
%51 = OpLabel
OpBranch %35
%35 = OpLabel
%56 = OpLoad %int %index
%57 = OpIAdd %int %56 %int_1
OpStore %index %57
OpBranch %32
%36 = OpLabel
%58 = OpAccessChain %_ptr_Uniform_v4float %10 %int_1
%59 = OpLoad %v4float %58
OpReturnValue %59
OpFunctionEnd

View File

@ -1,4 +1,13 @@
uniform float arr[3];
void main() {
out vec4 sk_FragColor;
uniform float testArray[5];
uniform vec4 colorGreen;
uniform vec4 colorRed;
vec4 main() {
for (int index = 0;index < 5; ++index) {
if (testArray[index] != float(index + 1)) {
return colorRed;
}
}
return colorGreen;
}

View File

@ -2,7 +2,9 @@
#include <simd/simd.h>
using namespace metal;
struct Uniforms {
array<float, 3> arr;
array<float, 5> testArray;
float4 colorGreen;
float4 colorRed;
};
struct Inputs {
};
@ -12,5 +14,12 @@ struct Outputs {
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Outputs _out;
(void)_out;
for (int index = 0;index < 5; ++index) {
if (_uniforms.testArray[index] != float(index + 1)) {
_out.sk_FragColor = _uniforms.colorRed;
return _out;
}
}
_out.sk_FragColor = _uniforms.colorGreen;
return _out;
}