247 : options_(source_options) {
248 if (!source_mapping || source_mapping->GetMapping() == nullptr) {
250 << "Could not read shader source or shader source was empty.";
251 return;
252 }
253
255 COMPILER_ERROR(error_stream_) <<
"Target platform not specified.";
256 return;
257 }
258
259 SPIRVCompilerOptions spirv_options;
260
261
262
263 spirv_options.generate_debug_info = true;
264
267
268
269 spirv_options.source_langauge =
270 shaderc_source_language::shaderc_source_language_glsl;
271 spirv_options.source_profile = SPIRVCompilerSourceProfile{
272 shaderc_profile::shaderc_profile_core,
273 460,
274 };
275 break;
277 spirv_options.source_langauge =
278 shaderc_source_language::shaderc_source_language_hlsl;
279 break;
282 return;
283 }
284
285 switch (source_options.target_platform) {
288 SPIRVCompilerTargetEnv
target;
289
290 if (source_options.use_half_textures) {
291 target.env = shaderc_target_env::shaderc_target_env_opengl;
292 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
293 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
294 } else {
295 target.env = shaderc_target_env::shaderc_target_env_vulkan;
296 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
297 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
298 }
299
300 spirv_options.target =
target;
301 } break;
306 SPIRVCompilerTargetEnv
target;
307
308 target.env = shaderc_target_env::shaderc_target_env_vulkan;
309 target.version = shaderc_env_version::shaderc_env_version_vulkan_1_1;
310 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_3;
311
312 if (source_options.target_platform ==
314 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
315 spirv_options.relaxed_vulkan_rules = true;
316 }
317 spirv_options.target =
target;
318 } break;
321 SPIRVCompilerTargetEnv
target;
322
323 target.env = shaderc_target_env::shaderc_target_env_opengl;
324 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
325 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
326
327 spirv_options.target =
target;
328 spirv_options.macro_definitions.push_back("IMPELLER_GRAPHICS_BACKEND");
329 } break;
331 SPIRVCompilerTargetEnv
target;
332
333 target.env = shaderc_target_env::shaderc_target_env_opengl;
334 target.version = shaderc_env_version::shaderc_env_version_opengl_4_5;
335 target.spirv_version = shaderc_spirv_version::shaderc_spirv_version_1_0;
336
337
338
339
340
341 spirv_options.optimization_level =
342 shaderc_optimization_level::shaderc_optimization_level_zero;
343 spirv_options.target =
target;
344 spirv_options.macro_definitions.push_back("SKIA_GRAPHICS_BACKEND");
345 } break;
348 return;
349 }
350
351
352
353 spirv_options.macro_definitions.push_back("IMPELLER_DEVICE");
354 for (const auto& define : source_options.defines) {
355 spirv_options.macro_definitions.push_back(define);
356 }
357
358 std::vector<std::string> included_file_names;
359 spirv_options.includer = std::make_shared<Includer>(
361 [&included_file_names](auto included_name) {
362 included_file_names.emplace_back(std::move(included_name));
363 });
364
365
366 SPIRVCompiler spv_compiler(source_options, source_mapping);
367
368 spirv_assembly_ = spv_compiler.CompileToSPV(
369 error_stream_, spirv_options.BuildShadercOptions());
370
371 if (!spirv_assembly_) {
372 return;
373 } else {
374 included_file_names_ = std::move(included_file_names);
375 }
376
377
378 spirv_cross::Parser
parser(
379 reinterpret_cast<const uint32_t*>(spirv_assembly_->GetMapping()),
380 spirv_assembly_->GetSize() / sizeof(uint32_t));
381
382
384
385 const auto parsed_ir =
386 std::make_shared<spirv_cross::ParsedIR>(
parser.get_parsed_ir());
387
389
390 if (!sl_compiler) {
392 << "Could not create compiler for target platform.";
393 return;
394 }
395
396
397
398
399 auto sl_compilation_result =
401
402
403
404
407 auto stripped_spirv_options = spirv_options;
408 stripped_spirv_options.generate_debug_info = false;
409 sl_mapping_ = spv_compiler.CompileToSPV(
410 error_stream_, stripped_spirv_options.BuildShadercOptions());
411 } else {
412 sl_mapping_ = sl_compilation_result;
413 }
414
415 if (!sl_mapping_) {
416 COMPILER_ERROR(error_stream_) <<
"Could not generate SL from SPIRV";
417 return;
418 }
419
420 reflector_ = std::make_unique<Reflector>(std::move(reflector_options),
421 parsed_ir,
423 sl_compiler
424 );
425
426 if (!reflector_->IsValid()) {
428 << "Could not complete reflection on generated shader.";
429 return;
430 }
431
432 is_valid_ = true;
433}
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
#define COMPILER_ERROR(stream)
static CompilerBackend CreateCompiler(const spirv_cross::ParsedIR &ir, const SourceOptions &source_options)
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
SourceLanguage source_language
std::vector< IncludeDir > include_dirs
std::shared_ptr< fml::UniqueFD > working_directory