|
6 | 6 |
|
7 | 7 | #include <cstdint> |
8 | 8 | #include <memory> |
| 9 | +#include <string> |
| 10 | +#include <utility> |
9 | 11 | #include <vector> |
10 | 12 |
|
| 13 | +#include "flutter/lib/gpu/shader.h" |
11 | 14 | #include "fml/mapping.h" |
12 | 15 | #include "gtest/gtest.h" |
| 16 | +#include "impeller/core/shader_types.h" |
13 | 17 | #include "impeller/renderer/context.h" |
14 | 18 | // Pulls in flatbuffers/flatbuffers.h (FlatBufferBuilder, Verifier) and the |
15 | 19 | // generated impeller::fb::shaderbundle:: symbols. |
@@ -162,6 +166,85 @@ TEST(FlutterGpuShaderLibraryTest, |
162 | 166 | EXPECT_FALSE(library); |
163 | 167 | } |
164 | 168 |
|
| 169 | +// Serializes a single-fragment-shader bundle (Metal desktop variant) carrying |
| 170 | +// the given reflected textures and uniform structs, each as a |
| 171 | +// (name, ext_res_0) pair. A texture/struct whose ext_res_0 is the optimized-out |
| 172 | +// sentinel stands in for a resource the shader compiler dead-code-eliminated. |
| 173 | +static std::shared_ptr<std::vector<uint8_t>> BuildFragmentBundle( |
| 174 | + const std::vector<std::pair<std::string, uint64_t>>& textures, |
| 175 | + const std::vector<std::pair<std::string, uint64_t>>& structs) { |
| 176 | + namespace fbs = impeller::fb::shaderbundle; |
| 177 | + |
| 178 | + auto metal = std::make_unique<fbs::BackendShaderT>(); |
| 179 | + metal->stage = fbs::ShaderStage::kFragment; |
| 180 | + metal->entrypoint = "main"; |
| 181 | + // The bytes are ignored at parse time (no GPU compile), but the field must be |
| 182 | + // present and non-empty for the loader to build a code mapping. |
| 183 | + metal->shader = {0}; |
| 184 | + for (const auto& [name, ext_res_0] : textures) { |
| 185 | + auto texture = std::make_unique<fbs::ShaderUniformTextureT>(); |
| 186 | + texture->name = name; |
| 187 | + texture->ext_res_0 = ext_res_0; |
| 188 | + metal->uniform_textures.push_back(std::move(texture)); |
| 189 | + } |
| 190 | + for (const auto& [name, ext_res_0] : structs) { |
| 191 | + auto uniform = std::make_unique<fbs::ShaderUniformStructT>(); |
| 192 | + uniform->name = name; |
| 193 | + uniform->ext_res_0 = ext_res_0; |
| 194 | + uniform->size_in_bytes = 16; |
| 195 | + metal->uniform_structs.push_back(std::move(uniform)); |
| 196 | + } |
| 197 | + |
| 198 | + auto shader = std::make_unique<fbs::ShaderT>(); |
| 199 | + shader->name = "test"; |
| 200 | + shader->metal_desktop = std::move(metal); |
| 201 | + |
| 202 | + fbs::ShaderBundleT bundle; |
| 203 | + bundle.format_version = |
| 204 | + static_cast<uint32_t>(fbs::ShaderBundleFormatVersion::kVersion); |
| 205 | + bundle.shaders.push_back(std::move(shader)); |
| 206 | + |
| 207 | + flatbuffers::FlatBufferBuilder builder; |
| 208 | + builder.Finish(fbs::ShaderBundle::Pack(builder, &bundle), |
| 209 | + fbs::ShaderBundleIdentifier()); |
| 210 | + return std::make_shared<std::vector<uint8_t>>( |
| 211 | + builder.GetBufferPointer(), |
| 212 | + builder.GetBufferPointer() + builder.GetSize()); |
| 213 | +} |
| 214 | + |
| 215 | +// A sampler the shader compiler dead-code-eliminated is still listed in |
| 216 | +// reflection but carries the out-of-range binding sentinel. It must not be |
| 217 | +// registered as a bindable uniform texture (binding an out-of-range index |
| 218 | +// crashes the Metal backend), while a live sampler alongside it survives. |
| 219 | +TEST(FlutterGpuShaderLibraryTest, MakeFromFlatbufferSkipsOptimizedOutTexture) { |
| 220 | + const uint64_t sentinel = impeller::kOptimizedOutBinding; |
| 221 | + auto bundle = BuildFragmentBundle( |
| 222 | + /*textures=*/{{"u_live", 0}, {"u_dced", sentinel}}, /*structs=*/{}); |
| 223 | + auto library = ShaderLibrary::MakeFromFlatbuffer( |
| 224 | + impeller::Context::BackendType::kMetal, CreateMappingFromVector(bundle), |
| 225 | + "test_bundle"); |
| 226 | + ASSERT_TRUE(library); |
| 227 | + auto shader = library->FindShaderForTesting("test"); |
| 228 | + ASSERT_TRUE(shader); |
| 229 | + EXPECT_NE(shader->GetUniformTexture("u_live"), nullptr); |
| 230 | + EXPECT_EQ(shader->GetUniformTexture("u_dced"), nullptr); |
| 231 | +} |
| 232 | + |
| 233 | +// The same skip applies to a dead-code-eliminated uniform block. |
| 234 | +TEST(FlutterGpuShaderLibraryTest, MakeFromFlatbufferSkipsOptimizedOutStruct) { |
| 235 | + const uint64_t sentinel = impeller::kOptimizedOutBinding; |
| 236 | + auto bundle = BuildFragmentBundle( |
| 237 | + /*textures=*/{}, /*structs=*/{{"Live", 0}, {"Dced", sentinel}}); |
| 238 | + auto library = ShaderLibrary::MakeFromFlatbuffer( |
| 239 | + impeller::Context::BackendType::kMetal, CreateMappingFromVector(bundle), |
| 240 | + "test_bundle"); |
| 241 | + ASSERT_TRUE(library); |
| 242 | + auto shader = library->FindShaderForTesting("test"); |
| 243 | + ASSERT_TRUE(shader); |
| 244 | + EXPECT_NE(shader->GetUniformStruct("Live"), nullptr); |
| 245 | + EXPECT_EQ(shader->GetUniformStruct("Dced"), nullptr); |
| 246 | +} |
| 247 | + |
165 | 248 | } // namespace testing |
166 | 249 | } // namespace gpu |
167 | 250 | } // namespace flutter |
0 commit comments