|
15 | 15 | #include <stb/stb_image.h> |
16 | 16 | #include <stb/stb_image_write.h> |
17 | 17 |
|
18 | | -#if defined(LVK_WITH_SLANG) && LVK_WITH_SLANG |
19 | | -#include <slang.h> |
20 | | -#include <slang-com-helper.h> |
21 | | -#include <slang-com-ptr.h> |
22 | | -#include <core/slang-basic.h> |
23 | | -#endif // defined(LVK_WITH_SLANG) && LVK_WITH_SLANG |
24 | | - |
25 | 18 | #if defined(ANDROID) |
26 | 19 | #include <android/asset_manager_jni.h> |
27 | 20 | #include <android/native_window_jni.h> |
@@ -412,124 +405,3 @@ void VulkanApp::drawFPS() { |
412 | 405 | } |
413 | 406 | ImGui::End(); |
414 | 407 | } |
415 | | - |
416 | | -#if defined(LVK_WITH_SLANG) && LVK_WITH_SLANG |
417 | | - |
418 | | -std::vector<uint8_t> compileSlangToSPIRV(const char* code, lvk::ShaderStage stage) { |
419 | | - using namespace Slang; |
420 | | - |
421 | | - ComPtr<slang::IGlobalSession> slangGlobalSession; |
422 | | - if (SLANG_FAILED(slang::createGlobalSession(slangGlobalSession.writeRef()))) { |
423 | | - return {}; |
424 | | - } |
425 | | - |
426 | | - slang::CompilerOptionEntry compilerOptions[] = { |
427 | | - {.name = slang::CompilerOptionName::Capability, |
428 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "SPV_GOOGLE_user_type"}}, |
429 | | - {.name = slang::CompilerOptionName::Capability, |
430 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvDerivativeControl"}}, |
431 | | - {.name = slang::CompilerOptionName::Capability, |
432 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvImageQuery"}}, |
433 | | - {.name = slang::CompilerOptionName::Capability, |
434 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvImageGatherExtended"}}, |
435 | | - {.name = slang::CompilerOptionName::Capability, |
436 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvSparseResidency"}}, |
437 | | - {.name = slang::CompilerOptionName::Capability, |
438 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvMinLod"}}, |
439 | | - {.name = slang::CompilerOptionName::Capability, |
440 | | - .value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvFragmentFullyCoveredEXT"}}, |
441 | | - }; |
442 | | - |
443 | | - const slang::TargetDesc targetDesc = { |
444 | | - .format = SLANG_SPIRV, |
445 | | - .profile = slangGlobalSession->findProfile("spirv_1_6"), |
446 | | - .flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY, |
447 | | - .forceGLSLScalarBufferLayout = true, |
448 | | - .compilerOptionEntries = &compilerOptions[0], |
449 | | - .compilerOptionEntryCount = LVK_ARRAY_NUM_ELEMENTS(compilerOptions), |
450 | | - }; |
451 | | - |
452 | | - const slang::SessionDesc sessionDesc = { |
453 | | - .targets = &targetDesc, |
454 | | - .targetCount = 1, |
455 | | - }; |
456 | | - |
457 | | - ComPtr<slang::ISession> session; |
458 | | - if (SLANG_FAILED(slangGlobalSession->createSession(sessionDesc, session.writeRef()))) { |
459 | | - return {}; |
460 | | - } |
461 | | - |
462 | | - slang::IModule* slangModule = nullptr; |
463 | | - { |
464 | | - ComPtr<slang::IBlob> diagnosticBlob; |
465 | | - slangModule = session->loadModuleFromSourceString("", "", code, diagnosticBlob.writeRef()); |
466 | | - if (diagnosticBlob) { |
467 | | - LLOGW("%s", (const char*)diagnosticBlob->getBufferPointer()); |
468 | | - } |
469 | | - if (!slangModule) { |
470 | | - return {}; |
471 | | - } |
472 | | - } |
473 | | - |
474 | | - ComPtr<slang::IEntryPoint> entryPointVert; |
475 | | - ComPtr<slang::IEntryPoint> entryPointFrag; |
476 | | - if (SLANG_FAILED(slangModule->findEntryPointByName("vertexMain", entryPointVert.writeRef()))) { |
477 | | - LVK_ASSERT_MSG(entryPointVert, "vertexMain() not found"); |
478 | | - return {}; |
479 | | - } |
480 | | - if (SLANG_FAILED(slangModule->findEntryPointByName("fragmentMain", entryPointFrag.writeRef()))) { |
481 | | - LVK_ASSERT_MSG(entryPointFrag, "fragmentMain() not found"); |
482 | | - return {}; |
483 | | - } |
484 | | - |
485 | | - Slang::List<slang::IComponentType*> componentTypes; |
486 | | - componentTypes.add(slangModule); |
487 | | - int entryPointCount = 0; |
488 | | - int vertexEntryPointIndex = entryPointCount++; |
489 | | - componentTypes.add(entryPointVert); |
490 | | - int fragmentEntryPointIndex = entryPointCount++; |
491 | | - componentTypes.add(entryPointFrag); |
492 | | - |
493 | | - ComPtr<slang::IComponentType> composedProgram; |
494 | | - { |
495 | | - ComPtr<slang::IBlob> diagnosticBlob; |
496 | | - SlangResult result = session->createCompositeComponentType( |
497 | | - componentTypes.getBuffer(), componentTypes.getCount(), composedProgram.writeRef(), diagnosticBlob.writeRef()); |
498 | | - if (diagnosticBlob) { |
499 | | - LLOGW("%s\n", (const char*)diagnosticBlob->getBufferPointer()); |
500 | | - } |
501 | | - if (SLANG_FAILED(result)) { |
502 | | - LVK_ASSERT_MSG(false, "Slang failed"); |
503 | | - return {}; |
504 | | - } |
505 | | - } |
506 | | - |
507 | | - ComPtr<slang::IBlob> spirvCode; |
508 | | - { |
509 | | - ComPtr<slang::IBlob> diagnosticBlob; |
510 | | - const int entryPoint = stage == lvk::Stage_Vert ? vertexEntryPointIndex : fragmentEntryPointIndex; |
511 | | - SlangResult result = composedProgram->getEntryPointCode(entryPoint, 0, spirvCode.writeRef(), diagnosticBlob.writeRef()); |
512 | | - if (diagnosticBlob) { |
513 | | - LLOGW("%s\n", (const char*)diagnosticBlob->getBufferPointer()); |
514 | | - } |
515 | | - if (SLANG_FAILED(result)) { |
516 | | - LVK_ASSERT_MSG(false, "Slang failed"); |
517 | | - return {}; |
518 | | - } |
519 | | - } |
520 | | - |
521 | | - const uint8_t* ptr = reinterpret_cast<const uint8_t*>(spirvCode->getBufferPointer()); |
522 | | - |
523 | | - return std::vector<uint8_t>(ptr, ptr + spirvCode->getBufferSize()); |
524 | | -} |
525 | | - |
526 | | -lvk::Holder<lvk::ShaderModuleHandle> slangCreateShaderModule(lvk::IContext* ctx, |
527 | | - const char* code, |
528 | | - lvk::ShaderStage stage, |
529 | | - const char* debugName) { |
530 | | - const std::vector<uint8_t> spirv = compileSlangToSPIRV(code, stage); |
531 | | - |
532 | | - return ctx ? ctx->createShaderModule({spirv.data(), spirv.size(), stage, debugName}) : lvk::Holder<lvk::ShaderModuleHandle>(); |
533 | | -} |
534 | | - |
535 | | -#endif // defined(LVK_WITH_SLANG) && LVK_WITH_SLANG |
0 commit comments