xref: /aosp_15_r20/external/skia/tests/graphite/RTEffectTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "tests/Test.h"
9 
10 #include "include/effects/SkRuntimeEffect.h"
11 #include "include/gpu/graphite/Context.h"
12 #include "src/core/SkRuntimeEffectPriv.h"
13 #include "src/gpu/graphite/ContextPriv.h"
14 #include "src/gpu/graphite/KeyHelpers.h"
15 #include "src/gpu/graphite/ShaderCodeDictionary.h"
16 
17 using namespace skgpu::graphite;
18 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(Shader_FindOrCreateSnippetForRuntimeEffect,reporter,context,CtsEnforcement::kApiLevel_V)19 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(Shader_FindOrCreateSnippetForRuntimeEffect, reporter, context,
20                                    CtsEnforcement::kApiLevel_V) {
21     ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
22 
23     std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
24         "half4 main(float2 coords) {"
25             "return half4(coords.xy01);"
26         "}"
27     ));
28 
29     // Create a new runtime-effect snippet.
30     int snippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
31     REPORTER_ASSERT(reporter, snippetID >= SkKnownRuntimeEffects::kUnknownRuntimeEffectIDStart);
32 
33     // Verify that it can be looked up and its name is 'RuntimeEffect'. (The name isn't meaningful,
34     // but this is an easy way to verify that we didn't get an unrelated snippet.)
35     const ShaderSnippet* snippet = dict->getEntry(snippetID);
36     REPORTER_ASSERT(reporter, snippet);
37     REPORTER_ASSERT(reporter, std::string_view(snippet->fName) == "RuntimeEffect");
38 
39     // If we pass the same effect again, we should get the same snippet ID as before.
40     int foundSnippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
41     REPORTER_ASSERT(reporter, foundSnippetID == snippetID);
42 }
43 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilter_FindOrCreateSnippetForRuntimeEffect,reporter,context,CtsEnforcement::kApiLevel_V)44 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilter_FindOrCreateSnippetForRuntimeEffect,
45                                    reporter,
46                                    context,
47                                    CtsEnforcement::kApiLevel_V) {
48     ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
49 
50     std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(
51             SkRuntimeEffect::MakeForColorFilter,
52                 "half4 main(half4 color) {"
53                     "return color.gbra;"
54                 "}"
55             ));
56 
57     // Create a new runtime-effect snippet.
58     int snippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
59     REPORTER_ASSERT(reporter, snippetID >= SkKnownRuntimeEffects::kUnknownRuntimeEffectIDStart);
60 
61     // Verify that it can be looked up and its name is 'RuntimeEffect'. (The name isn't meaningful,
62     // but this is an easy way to verify that we didn't get an unrelated snippet.)
63     const ShaderSnippet* snippet = dict->getEntry(snippetID);
64     REPORTER_ASSERT(reporter, snippet);
65     REPORTER_ASSERT(reporter, std::string_view(snippet->fName) == "RuntimeEffect");
66 
67     // If we pass the same effect again, we should get the same snippet ID as before.
68     int foundSnippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
69     REPORTER_ASSERT(reporter, foundSnippetID == snippetID);
70 }
71 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderUniforms_FindOrCreateSnippetForRuntimeEffect,reporter,context,CtsEnforcement::kApiLevel_V)72 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderUniforms_FindOrCreateSnippetForRuntimeEffect,
73                                    reporter, context, CtsEnforcement::kApiLevel_V) {
74     ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
75 
76     std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
77         "uniform float3x3 MyFloat3x3Uniform;"
78         "uniform int4 MyInt4ArrayUniform[1];"
79         "uniform half2 MyHalf2ArrayUniform[99];"
80         "half4 main(float2 coords) {"
81             "return half4(coords.xy01);"
82         "}"
83     ));
84 
85     // Create a new runtime-effect snippet.
86     int snippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
87     REPORTER_ASSERT(reporter, snippetID >= SkKnownRuntimeEffects::kUnknownRuntimeEffectIDStart);
88 
89     // Delete the test effect.
90     testEffect = nullptr;
91 
92     // Verify that it can be looked up by its snippet ID.
93     const ShaderSnippet* snippet = dict->getEntry(snippetID);
94     REPORTER_ASSERT(reporter, snippet);
95 
96     // The uniform span should match our expectations even though the runtime effect was deleted.
97     REPORTER_ASSERT(reporter, snippet->fUniforms.size() == 3);
98 
99     REPORTER_ASSERT(reporter,
100                     std::string_view(snippet->fUniforms[0].name()) == "MyFloat3x3Uniform");
101     REPORTER_ASSERT(reporter, snippet->fUniforms[0].type() == SkSLType::kFloat3x3);
102     REPORTER_ASSERT(reporter, snippet->fUniforms[0].count() == 0);
103 
104     REPORTER_ASSERT(reporter,
105                     std::string_view(snippet->fUniforms[1].name()) == "MyInt4ArrayUniform");
106     REPORTER_ASSERT(reporter, snippet->fUniforms[1].type() == SkSLType::kInt4);
107     REPORTER_ASSERT(reporter, snippet->fUniforms[1].count() == 1);
108 
109     REPORTER_ASSERT(reporter,
110                     std::string_view(snippet->fUniforms[2].name()) == "MyHalf2ArrayUniform");
111     REPORTER_ASSERT(reporter, snippet->fUniforms[2].type() == SkSLType::kHalf2);
112     REPORTER_ASSERT(reporter, snippet->fUniforms[2].count() == 99);
113 }
114 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilterUniforms_FindOrCreateSnippetForRuntimeEffect,reporter,context,CtsEnforcement::kApiLevel_V)115 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilterUniforms_FindOrCreateSnippetForRuntimeEffect,
116                                    reporter, context, CtsEnforcement::kApiLevel_V) {
117     ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
118 
119     std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(
120             SkRuntimeEffect::MakeForColorFilter,
121                 "uniform float3x3 MyFloat3x3Uniform;"
122                 "uniform int4 MyInt4ArrayUniform[1];"
123                 "uniform half2 MyHalf2ArrayUniform[99];"
124                 "half4 main(half4 color) {"
125                     "return color.gbra;"
126                 "}"
127             ));
128 
129     // Create a new runtime-effect snippet.
130     int snippetID = dict->findOrCreateRuntimeEffectSnippet(testEffect.get());
131     REPORTER_ASSERT(reporter, snippetID >= SkKnownRuntimeEffects::kUnknownRuntimeEffectIDStart);
132 
133     // Delete the test effect.
134     testEffect = nullptr;
135 
136     // Verify that it can be looked up by its snippet ID.
137     const ShaderSnippet* snippet = dict->getEntry(snippetID);
138     REPORTER_ASSERT(reporter, snippet);
139 
140     // The uniform span should match our expectations even though the runtime effect was deleted.
141     REPORTER_ASSERT(reporter, snippet->fUniforms.size() == 3);
142 
143     REPORTER_ASSERT(reporter,
144                     std::string_view(snippet->fUniforms[0].name()) == "MyFloat3x3Uniform");
145     REPORTER_ASSERT(reporter, snippet->fUniforms[0].type() == SkSLType::kFloat3x3);
146     REPORTER_ASSERT(reporter, snippet->fUniforms[0].count() == 0);
147 
148     REPORTER_ASSERT(reporter,
149                     std::string_view(snippet->fUniforms[1].name()) == "MyInt4ArrayUniform");
150     REPORTER_ASSERT(reporter, snippet->fUniforms[1].type() == SkSLType::kInt4);
151     REPORTER_ASSERT(reporter, snippet->fUniforms[1].count() == 1);
152 
153     REPORTER_ASSERT(reporter,
154                     std::string_view(snippet->fUniforms[2].name()) == "MyHalf2ArrayUniform");
155     REPORTER_ASSERT(reporter, snippet->fUniforms[2].type() == SkSLType::kHalf2);
156     REPORTER_ASSERT(reporter, snippet->fUniforms[2].count() == 99);
157 }
158