Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RTEffectTest.cpp
Go to the documentation of this file.
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
16
17using namespace skgpu::graphite;
18
19DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(Shader_FindOrCreateSnippetForRuntimeEffect, reporter, context,
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());
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
44DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilter_FindOrCreateSnippetForRuntimeEffect,
46 context,
48 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
49
50 std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(
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());
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
72DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ShaderUniforms_FindOrCreateSnippetForRuntimeEffect,
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());
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
100 std::string_view(snippet->fUniforms[0].name()) == "MyFloat3x3Uniform");
102 REPORTER_ASSERT(reporter, snippet->fUniforms[0].count() == 0);
103
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
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
115DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ColorFilterUniforms_FindOrCreateSnippetForRuntimeEffect,
117 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
118
119 std::unique_ptr<SkRuntimeEffect> testEffect(SkMakeRuntimeEffect(
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());
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
144 std::string_view(snippet->fUniforms[0].name()) == "MyFloat3x3Uniform");
146 REPORTER_ASSERT(reporter, snippet->fUniforms[0].count() == 0);
147
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
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}
reporter
SkRuntimeEffect * SkMakeRuntimeEffect(SkRuntimeEffect::Result(*make)(SkString, const SkRuntimeEffect::Options &), const char *sksl, SkRuntimeEffect::Options options=SkRuntimeEffect::Options{})
#define DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(name, reporter, graphite_ctx, ctsEnforcement)
Definition Test.h:373
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static Result MakeForColorFilter(SkString sksl, const Options &)
static Result MakeForShader(SkString sksl, const Options &)
int findOrCreateRuntimeEffectSnippet(const SkRuntimeEffect *effect)
const ShaderSnippet * getEntry(int codeSnippetID) const SK_EXCLUDES(fSpinLock)
static constexpr int kUnknownRuntimeEffectIDStart