From 78172b5abc1005232193c9ff5d970934461e5fb4 Mon Sep 17 00:00:00 2001 From: Kearwood Date: Thu, 6 Aug 2020 18:15:58 -0700 Subject: [PATCH] Added KrCompileAllShaders and stub implementation --- CMakeLists.txt | 2 +- kraken/KRContext.cpp | 11 ++++++++++ kraken/KRContext.h | 2 ++ kraken/KREngine-common.h | 1 + kraken/KRShaderManager.cpp | 5 +++++ kraken/KRShaderManager.h | 3 ++- kraken/kraken.cpp | 8 +++++++ kraken/public/kraken.h | 10 +++++++++ tools/convert/main.cpp | 45 +++++++++++++++++++++++++++++++------- 9 files changed, 77 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cb284a..1d10963 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,7 +237,7 @@ SET(STANDARD_ASSET_BUNDLE "${CMAKE_BINARY_DIR}/output/assets/standard_assets.krb add_custom_command( OUTPUT ${STANDARD_ASSET_BUNDLE} - COMMAND kraken_convert -o ${STANDARD_ASSET_BUNDLE} ${KRAKEN_STANDARD_ASSETS} + COMMAND kraken_convert -c -o ${STANDARD_ASSET_BUNDLE} ${KRAKEN_STANDARD_ASSETS} DEPENDS kraken_convert ${KRAKEN_STANDARD_ASSETS} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMENT "Creating Standard Assets" diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index b59ee93..184633a 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -106,6 +106,7 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) #endif createDeviceContexts(); + glslang::InitializeProcess(); } KRContext::~KRContext() { @@ -170,6 +171,7 @@ KRContext::~KRContext() { delete m_resourceMap; m_resourceMap = NULL; } + glslang::FinalizeProcess(); } void KRContext::SetLogCallback(log_callback *log_callback, void *user_data) @@ -493,6 +495,15 @@ KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo) return resource->moveToBundle(bundle); } +KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo) { + bool success = m_pShaderManager->compileAll(); + if (success) { + // TODO - Save log to a resource + return KR_SUCCESS; + } + return KR_ERROR_SHADER_COMPILE_FAILED; +} + KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo) { if (saveResourceInfo->resourceHandle < 0 || saveResourceInfo->resourceHandle >= m_resourceMapSize) { diff --git a/kraken/KRContext.h b/kraken/KRContext.h index 0585645..5dcccb6 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -51,6 +51,8 @@ public: KrResult unmapResource(const KrUnmapResourceInfo* unmapResourceInfo); KrResult saveResource(const KrSaveResourceInfo* saveResourceInfo); + KrResult compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo); + KrResult createScene(const KrCreateSceneInfo* createSceneInfo); KrResult findNodeByName(const KrFindNodeByNameInfo* pFindNodeByNameInfo); KrResult findAdjacentNodes(const KrFindAdjacentNodesInfo* pFindAdjacentNodesInfo); diff --git a/kraken/KREngine-common.h b/kraken/KREngine-common.h index c9ce754..6e71c4b 100755 --- a/kraken/KREngine-common.h +++ b/kraken/KREngine-common.h @@ -34,6 +34,7 @@ using namespace kraken; #include #include "../3rdparty/tinyxml2/tinyxml2.h" +#include "../3rdparty/glslang/glslang/Public/ShaderLang.h" #if defined(__APPLE__) #include diff --git a/kraken/KRShaderManager.cpp b/kraken/KRShaderManager.cpp index fbad9ee..d733200 100644 --- a/kraken/KRShaderManager.cpp +++ b/kraken/KRShaderManager.cpp @@ -115,3 +115,8 @@ const unordered_map &KRShaderManager::get(const std::st return m_shaders[lower_extension]; } + +bool KRShaderManager::compileAll() +{ + return true; +} diff --git a/kraken/KRShaderManager.h b/kraken/KRShaderManager.h index d05755c..7978d68 100644 --- a/kraken/KRShaderManager.h +++ b/kraken/KRShaderManager.h @@ -52,7 +52,8 @@ 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(); const unordered_map &get(const std::string &extension); diff --git a/kraken/kraken.cpp b/kraken/kraken.cpp index a648dbc..2f8d0e1 100644 --- a/kraken/kraken.cpp +++ b/kraken/kraken.cpp @@ -114,6 +114,14 @@ KrResult KrMoveToBundle(const KrMoveToBundleInfo* pMoveToBundleInfo) return sContext->moveToBundle(pMoveToBundleInfo); } +KrResult KrCompileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo) +{ + if (!sContext) { + return KR_ERROR_NOT_INITIALIZED; + } + return sContext->compileAllShaders(pCompileAllShadersInfo); +} + KrResult KrCreateScene(const KrCreateSceneInfo* pCreateSceneInfo) { if (!sContext) { diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h index 4669155..ad38ca5 100644 --- a/kraken/public/kraken.h +++ b/kraken/public/kraken.h @@ -50,6 +50,7 @@ typedef enum { KR_ERROR_VULKAN_REQUIRED, KR_ERROR_VULKAN_SWAP_CHAIN, KR_ERROR_NO_DEVICE, + KR_ERROR_SHADER_COMPILE_FAILED, KR_ERROR_UNEXPECTED = 0x10000000, KR_RESULT_MAX_ENUM = 0x7FFFFFFF } KrResult; @@ -69,6 +70,8 @@ typedef enum { KR_STRUCTURE_TYPE_CREATE_BUNDLE, KR_STRUCTURE_TYPE_MOVE_TO_BUNDLE, + KR_STRUCTURE_TYPE_COMPILE_ALL_SHADERS, + KR_STRUCTURE_TYPE_CREATE_SCENE = 0x00020000, KR_STRUCTURE_TYPE_FIND_NODE_BY_NAME = 0x00030000, @@ -165,6 +168,11 @@ typedef struct { KrResourceMapIndex bundleHandle; } KrMoveToBundleInfo; +typedef struct { + KrStructureType sType; + KrResourceMapIndex logHandle; +} KrCompileAllShadersInfo; + typedef struct { KrStructureType sType; const char* pSceneName; @@ -395,6 +403,8 @@ KrResult KrCreateBundle(const KrCreateBundleInfo* pCreateBundleInfo); KrResult KrMoveToBundle(const KrMoveToBundleInfo* pMoveToBundleInfo); KrResult KrInitNodeInfo(KrNodeInfo* pNodeInfo, KrStructureType nodeType); +KrResult KrCompileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo); + KrResult KrCreateScene(const KrCreateSceneInfo* pCreateSceneInfo); KrResult KrFindNodeByName(const KrFindNodeByNameInfo* pFindNodeByNameInfo); KrResult KrFindAdjacentNodes(const KrFindAdjacentNodesInfo* pFindAdjacentNodesInfo); diff --git a/tools/convert/main.cpp b/tools/convert/main.cpp index 8f2546a..794f00e 100644 --- a/tools/convert/main.cpp +++ b/tools/convert/main.cpp @@ -5,8 +5,15 @@ using namespace kraken; +enum ResourceMapping { + output_bundle = 0, + loaded_resource = 1, + shader_compile_log = 2, +}; + int main( int argc, char *argv[] ) { + bool failed = false; printf("Kraken Convert\n"); printf("Initializing Kraken...\n"); KrInitializeInfo init_info = {}; @@ -20,7 +27,7 @@ int main( int argc, char *argv[] ) KrCreateBundleInfo create_bundle_info = {}; create_bundle_info.sType = KR_STRUCTURE_TYPE_CREATE_BUNDLE; - create_bundle_info.resourceHandle = 0; + create_bundle_info.resourceHandle = ResourceMapping::output_bundle; create_bundle_info.pBundleName = "output"; res = KrCreateBundle(&create_bundle_info); if (res != KR_SUCCESS) { @@ -31,15 +38,16 @@ int main( int argc, char *argv[] ) KrLoadResourceInfo load_resource_info = {}; load_resource_info.sType = KR_STRUCTURE_TYPE_LOAD_RESOURCE; - load_resource_info.resourceHandle = 1; + load_resource_info.resourceHandle = ResourceMapping::loaded_resource; KrMoveToBundleInfo move_to_bundle_info = {}; move_to_bundle_info.sType = KR_STRUCTURE_TYPE_MOVE_TO_BUNDLE; - move_to_bundle_info.bundleHandle = 0; + move_to_bundle_info.bundleHandle = ResourceMapping::output_bundle; char* output_bundle = nullptr; + bool compile_shaders = false; - for (int i = 1; i < argc; i++) { + for (int i = 1; i < argc && !failed; i++) { char *arg = argv[i]; if (arg[0] == '-') { continue; @@ -50,6 +58,9 @@ int main( int argc, char *argv[] ) case 'o': output_bundle = arg; break; + case 'c': + compile_shaders = true; + break; default: printf("Unknown command: -%c\n", command); break; @@ -62,31 +73,49 @@ int main( int argc, char *argv[] ) res = KrLoadResource(&load_resource_info); if (res != KR_SUCCESS) { printf("[FAIL] (KrLoadResource)\n"); + failed = true; continue; } - move_to_bundle_info.resourceHandle = 1; + move_to_bundle_info.resourceHandle = ResourceMapping::loaded_resource; res = KrMoveToBundle(&move_to_bundle_info); if (res != KR_SUCCESS) { printf("[FAIL] (KrMoveToBundle)\n"); + failed = true; continue; } printf("[GOOD]\n"); } - if (output_bundle) { + if (compile_shaders && !failed) { + printf("Compiling Shaders...\n"); + KrCompileAllShadersInfo compile_all_shaders_info = {}; + compile_all_shaders_info.sType = KR_STRUCTURE_TYPE_COMPILE_ALL_SHADERS; + 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); + failed = true; + } + else { + printf("[GOOD]\n"); + } + } + + if (output_bundle && !failed) { printf("Bundling %s... ", output_bundle); KrSaveResourceInfo save_resource_info = {}; save_resource_info.sType = KR_STRUCTURE_TYPE_SAVE_RESOURCE; - save_resource_info.resourceHandle = 0; + save_resource_info.resourceHandle = ResourceMapping::output_bundle; save_resource_info.pResourcePath = output_bundle; res = KrSaveResource(&save_resource_info); if (res != KR_SUCCESS) { printf("[FAIL] (Error %i)\n", res); + failed = true; } else { printf("[GOOD]\n"); } } KrShutdown(); - return 0; + return failed ? 1 : 0; }