diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index 462d2b8..14e3051 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -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(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; diff --git a/kraken/KREngine-common.h b/kraken/KREngine-common.h index 6e71c4b..a7a5c1c 100755 --- a/kraken/KREngine-common.h +++ b/kraken/KREngine-common.h @@ -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 diff --git a/kraken/KRShaderManager.cpp b/kraken/KRShaderManager.cpp index b499744..3808dd7 100644 --- a/kraken/KRShaderManager.cpp +++ b/kraken/KRShaderManager.cpp @@ -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(); @@ -302,6 +303,40 @@ bool KRShaderManager::compileAll(KRUnknown* logResource) logResource->getData()->append("\n"); success = false; } + + if (success) { + for (int stage = 0; stage < EShLangCount; ++stage) { + + if (program.getIntermediate((EShLanguage)stage)) { + std::vector 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(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(); diff --git a/kraken/KRShaderManager.h b/kraken/KRShaderManager.h index 8b31550..1bfea4e 100644 --- a/kraken/KRShaderManager.h +++ b/kraken/KRShaderManager.h @@ -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 &get(const std::string &extension); diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h index 93818fd..19d85d5 100644 --- a/kraken/public/kraken.h +++ b/kraken/public/kraken.h @@ -184,6 +184,7 @@ typedef struct { typedef struct { KrStructureType sType; + KrResourceMapIndex bundleHandle; KrResourceMapIndex logHandle; } KrCompileAllShadersInfo; diff --git a/tools/convert/main.cpp b/tools/convert/main.cpp index e681891..9edb5e4 100644 --- a/tools/convert/main.cpp +++ b/tools/convert/main.cpp @@ -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(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(result.data)[0] != '\0') { + printf("Shader compile log:\n%s\n", static_cast(result.data)); + } + } + }); } if (output_bundle && !failed) {