#include "slang_handle.h" #include "renderer.h" #include "application/application.h" bool slang_handle::init_slang_module(const std::string& module_name, const std::string& entry_name) { spdlog::info("slang: init slang handle: module \"{}\", entry point \"{}\"", module_name.c_str(), entry_name.c_str()); const auto session = application::get()->get_renderer()->get_slang_session(); Slang::ComPtr diagnostics; *module.writeRef() = session->loadModule(module_name.c_str(), diagnostics.writeRef()); if (diagnostics) { spdlog::error("slang: load module \"{}\" with diagnostics: {}", module_name.c_str(), (const char*)diagnostics->getBufferPointer()); return false; } auto r = module->findEntryPointByName(entry_name.c_str(), entry_point.writeRef()); if (r != SLANG_OK) { spdlog::error("slang: can't find entry point \"{}\"", entry_name.c_str()); return false; } slang::IComponentType* components[] = { module, entry_point }; r = session->createCompositeComponentType(components, 2, program.writeRef()); if (r != SLANG_OK) { spdlog::error("slang: create composite component type failed"); return false; } slang::ProgramLayout* layout = program->getLayout(target_index); // get entry point index and shader type for (int i = 0; i < layout->getEntryPointCount(); ++i) { const auto entry_reflection = layout->getEntryPointByIndex(i); if (strcmp(entry_reflection->getName(), entry_name.c_str()) == 0) { entry_point_index_ = i; shader_type_ = entry_reflection->getStage(); break; } } #if _DEBUG spdlog::info("slang: shader type: {}", shader_type_); spdlog::info("================parameters================"); for (int i = 0; i < layout->getParameterCount(); ++i) { slang::VariableLayoutReflection* reflection = layout->getParameterByIndex(i); const auto type_reflection = reflection->getTypeLayout()->getType(); spdlog::info("{} {} {}; ", i, type_reflection->getName(), reflection->getName()); } spdlog::info("================parameters================"); #endif spdlog::info("slang: init slang handle successfully"); return true; } Slang::ComPtr slang_handle::get_entry_point_code() const { Slang::ComPtr diagnostics; Slang::ComPtr code_blob; program->getEntryPointCode( entry_point_index_, target_index, code_blob.writeRef(), diagnostics.writeRef()); if (diagnostics) { spdlog::error("slang: get entry point code failed: {}", (const char*)diagnostics->getBufferPointer()); return nullptr; } return code_blob; }