Spirv binaries are now compiled into the standard asset bundle

This commit is contained in:
2020-12-30 17:03:18 -08:00
parent 893092e0ad
commit 75d3f019ae
6 changed files with 67 additions and 20 deletions

View File

@@ -521,6 +521,17 @@ KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo)
KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo)
{
if (pCompileAllShadersInfo->bundleHandle < 0 || pCompileAllShadersInfo->bundleHandle >= m_resourceMapSize) {
return KR_ERROR_OUT_OF_BOUNDS;
}
KRResource* bundleResource = m_resourceMap[pCompileAllShadersInfo->bundleHandle];
if (bundleResource == nullptr) {
return KR_ERROR_NOT_MAPPED;
}
KRBundle* bundle = dynamic_cast<KRBundle*>(bundleResource);
if (bundle == nullptr) {
return KR_ERROR_INCORRECT_TYPE;
}
if (pCompileAllShadersInfo->logHandle < -1 || pCompileAllShadersInfo->logHandle >= m_resourceMapSize) {
return KR_ERROR_OUT_OF_BOUNDS;
}
@@ -538,9 +549,8 @@ KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAll
m_resourceMap[pCompileAllShadersInfo->logHandle] = logResource;
}
bool success = m_pShaderManager->compileAll(logResource);
bool success = m_pShaderManager->compileAll(bundle, logResource);
if (success) {
// TODO - Save log to a resource
return KR_SUCCESS;
}
return KR_ERROR_SHADER_COMPILE_FAILED;

View File

@@ -35,6 +35,7 @@ using namespace kraken;
#include "../3rdparty/tinyxml2/tinyxml2.h"
#include "../3rdparty/glslang/glslang/Public/ShaderLang.h"
#include "../3rdparty/glslang/SPIRV/GlslangToSpv.h"
#if defined(__APPLE__)
#include <sys/mman.h>

View File

@@ -231,8 +231,9 @@ const TBuiltInResource DefaultTBuiltInResource = {
} };
bool KRShaderManager::compileAll(KRUnknown* logResource)
bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource)
{
// TODO - Refactoring / cleanup needed once Vulkan shaders working...
bool success = true;
if (!m_initializedGlslang) {
glslang::InitializeProcess();
@@ -303,6 +304,40 @@ bool KRShaderManager::compileAll(KRUnknown* logResource)
success = false;
}
if (success) {
for (int stage = 0; stage < EShLangCount; ++stage) {
if (program.getIntermediate((EShLanguage)stage)) {
std::vector<unsigned int> spirv;
spv::SpvBuildLogger logger;
glslang::SpvOptions spvOptions;
glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
logResource->getData()->append(logger.getAllMessages().c_str());
logResource->getData()->append("\n");
std::string shader_name;
switch (stage)
{
case EShLangVertex:
shader_name = vertSourceName;
break;
case EShLangFragment:
shader_name = fragSourceName;
break;
}
if (!shader_name.empty()) {
KRDataBlock* data = new KRDataBlock();
data->append(static_cast<void*>(spirv.data()), spirv.size() * sizeof(unsigned int));
KRShader* shader = new KRShader(getContext(), shader_name, "spv", data);
add(shader);
if (outputBundle) {
shader->moveToBundle(outputBundle);
}
}
}
}
}
if (vertSource) {
vertSource->getData()->unlock();
}

View File

@@ -55,7 +55,7 @@ public:
KRShader *load(const std::string &name, const std::string &extension, KRDataBlock *data);
KRShader *get(const std::string &name, const std::string &extension);
bool compileAll(KRUnknown* logResource);
bool compileAll(KRBundle* outputBundle, KRUnknown* logResource);
const unordered_map<std::string, KRShader *> &get(const std::string &extension);

View File

@@ -184,6 +184,7 @@ typedef struct {
typedef struct {
KrStructureType sType;
KrResourceMapIndex bundleHandle;
KrResourceMapIndex logHandle;
} KrCompileAllShadersInfo;

View File

@@ -151,29 +151,29 @@ int main( int argc, char *argv[] )
printf("Compiling Shaders... ");
KrCompileAllShadersInfo compile_all_shaders_info = {};
compile_all_shaders_info.sType = KR_STRUCTURE_TYPE_COMPILE_ALL_SHADERS;
compile_all_shaders_info.bundleHandle = ResourceMapping::output_bundle;
compile_all_shaders_info.logHandle = ResourceMapping::shader_compile_log;
res = KrCompileAllShaders(&compile_all_shaders_info);
if (res != KR_SUCCESS) {
printf("[FAIL] (Error %i)\n", res);
KrGetResourceDataInfo get_resource_data_info = {};
get_resource_data_info.sType = KR_STRUCTURE_TYPE_GET_RESOURCE_DATA;
get_resource_data_info.resourceHandle = ResourceMapping::shader_compile_log;
res = KrGetResourceData(&get_resource_data_info, [](const KrGetResourceDataResult& result) {
// TODO - This will later be asynchronous... Will need to block rest of execution until returned
if (result.result != KR_SUCCESS) {
printf("Failed to get shader compile log. (Error %i)\n", result.result);
} else {
// result.data will be a null terminated string
if (result.data != nullptr) {
printf("Shader compile log:\n%s\n", static_cast<char *>(result.data));
}
}
});
failed = true;
}
else {
} else {
printf("[GOOD]\n");
}
KrGetResourceDataInfo get_resource_data_info = {};
get_resource_data_info.sType = KR_STRUCTURE_TYPE_GET_RESOURCE_DATA;
get_resource_data_info.resourceHandle = ResourceMapping::shader_compile_log;
res = KrGetResourceData(&get_resource_data_info, [](const KrGetResourceDataResult& result) {
// TODO - This will later be asynchronous... Will need to block rest of execution until returned
if (result.result != KR_SUCCESS) {
printf("Failed to get shader compile log. (Error %i)\n", result.result);
} else {
// result.data will be a null terminated string
if (result.data != nullptr && static_cast<char*>(result.data)[0] != '\0') {
printf("Shader compile log:\n%s\n", static_cast<char *>(result.data));
}
}
});
}
if (output_bundle && !failed) {