100 lines
3.1 KiB
C++
100 lines
3.1 KiB
C++
#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<slang::IBlob> 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(), r);
|
|
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_);
|
|
#endif
|
|
|
|
spdlog::info("slang: init slang handle successfully");
|
|
return true;
|
|
}
|
|
|
|
Slang::ComPtr<slang::IBlob> slang_handle::get_entry_point_code() const
|
|
{
|
|
Slang::ComPtr<slang::IBlob> diagnostics;
|
|
Slang::ComPtr<slang::IBlob> 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;
|
|
}
|
|
|
|
std::map<std::string, slang::BindingType> slang_handle::get_binding_types() const
|
|
{
|
|
std::map<std::string, slang::BindingType> out;
|
|
slang::ProgramLayout* layout = program->getLayout(target_index);
|
|
layout->getGlobalConstantBufferBinding();
|
|
return out;
|
|
}
|
|
|
|
unsigned int slang_handle::get_bind_index(const char* param_name) const
|
|
{
|
|
slang::ProgramLayout* layout = program->getLayout(target_index);
|
|
// const auto entry_reflection = layout->getEntryPointByIndex(entry_point_index_);
|
|
const auto param_reflection = layout->getParameterCount();
|
|
for (unsigned int i = 0; i < param_reflection; ++i)
|
|
{
|
|
const auto param = layout->getParameterByIndex(i);
|
|
if (strcmp(param->getName(), param_name) == 0)
|
|
{
|
|
return param->getBindingIndex();
|
|
}
|
|
}
|
|
return -1;
|
|
}
|