Spirv binaries are now compiled into the standard asset bundle
This commit is contained in:
@@ -521,6 +521,17 @@ KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo)
|
|||||||
|
|
||||||
KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo)
|
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) {
|
if (pCompileAllShadersInfo->logHandle < -1 || pCompileAllShadersInfo->logHandle >= m_resourceMapSize) {
|
||||||
return KR_ERROR_OUT_OF_BOUNDS;
|
return KR_ERROR_OUT_OF_BOUNDS;
|
||||||
}
|
}
|
||||||
@@ -538,9 +549,8 @@ KrResult KRContext::compileAllShaders(const KrCompileAllShadersInfo* pCompileAll
|
|||||||
m_resourceMap[pCompileAllShadersInfo->logHandle] = logResource;
|
m_resourceMap[pCompileAllShadersInfo->logHandle] = logResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = m_pShaderManager->compileAll(logResource);
|
bool success = m_pShaderManager->compileAll(bundle, logResource);
|
||||||
if (success) {
|
if (success) {
|
||||||
// TODO - Save log to a resource
|
|
||||||
return KR_SUCCESS;
|
return KR_SUCCESS;
|
||||||
}
|
}
|
||||||
return KR_ERROR_SHADER_COMPILE_FAILED;
|
return KR_ERROR_SHADER_COMPILE_FAILED;
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ using namespace kraken;
|
|||||||
|
|
||||||
#include "../3rdparty/tinyxml2/tinyxml2.h"
|
#include "../3rdparty/tinyxml2/tinyxml2.h"
|
||||||
#include "../3rdparty/glslang/glslang/Public/ShaderLang.h"
|
#include "../3rdparty/glslang/glslang/Public/ShaderLang.h"
|
||||||
|
#include "../3rdparty/glslang/SPIRV/GlslangToSpv.h"
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|||||||
@@ -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;
|
bool success = true;
|
||||||
if (!m_initializedGlslang) {
|
if (!m_initializedGlslang) {
|
||||||
glslang::InitializeProcess();
|
glslang::InitializeProcess();
|
||||||
@@ -303,6 +304,40 @@ bool KRShaderManager::compileAll(KRUnknown* logResource)
|
|||||||
success = false;
|
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) {
|
if (vertSource) {
|
||||||
vertSource->getData()->unlock();
|
vertSource->getData()->unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public:
|
|||||||
KRShader *load(const std::string &name, const std::string &extension, KRDataBlock *data);
|
KRShader *load(const std::string &name, const std::string &extension, KRDataBlock *data);
|
||||||
KRShader *get(const std::string &name, const std::string &extension);
|
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);
|
const unordered_map<std::string, KRShader *> &get(const std::string &extension);
|
||||||
|
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
KrStructureType sType;
|
KrStructureType sType;
|
||||||
|
KrResourceMapIndex bundleHandle;
|
||||||
KrResourceMapIndex logHandle;
|
KrResourceMapIndex logHandle;
|
||||||
} KrCompileAllShadersInfo;
|
} KrCompileAllShadersInfo;
|
||||||
|
|
||||||
|
|||||||
@@ -151,10 +151,15 @@ int main( int argc, char *argv[] )
|
|||||||
printf("Compiling Shaders... ");
|
printf("Compiling Shaders... ");
|
||||||
KrCompileAllShadersInfo compile_all_shaders_info = {};
|
KrCompileAllShadersInfo compile_all_shaders_info = {};
|
||||||
compile_all_shaders_info.sType = KR_STRUCTURE_TYPE_COMPILE_ALL_SHADERS;
|
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;
|
compile_all_shaders_info.logHandle = ResourceMapping::shader_compile_log;
|
||||||
res = KrCompileAllShaders(&compile_all_shaders_info);
|
res = KrCompileAllShaders(&compile_all_shaders_info);
|
||||||
if (res != KR_SUCCESS) {
|
if (res != KR_SUCCESS) {
|
||||||
printf("[FAIL] (Error %i)\n", res);
|
printf("[FAIL] (Error %i)\n", res);
|
||||||
|
failed = true;
|
||||||
|
} else {
|
||||||
|
printf("[GOOD]\n");
|
||||||
|
}
|
||||||
KrGetResourceDataInfo get_resource_data_info = {};
|
KrGetResourceDataInfo get_resource_data_info = {};
|
||||||
get_resource_data_info.sType = KR_STRUCTURE_TYPE_GET_RESOURCE_DATA;
|
get_resource_data_info.sType = KR_STRUCTURE_TYPE_GET_RESOURCE_DATA;
|
||||||
get_resource_data_info.resourceHandle = ResourceMapping::shader_compile_log;
|
get_resource_data_info.resourceHandle = ResourceMapping::shader_compile_log;
|
||||||
@@ -164,16 +169,11 @@ int main( int argc, char *argv[] )
|
|||||||
printf("Failed to get shader compile log. (Error %i)\n", result.result);
|
printf("Failed to get shader compile log. (Error %i)\n", result.result);
|
||||||
} else {
|
} else {
|
||||||
// result.data will be a null terminated string
|
// result.data will be a null terminated string
|
||||||
if (result.data != nullptr) {
|
if (result.data != nullptr && static_cast<char*>(result.data)[0] != '\0') {
|
||||||
printf("Shader compile log:\n%s\n", static_cast<char *>(result.data));
|
printf("Shader compile log:\n%s\n", static_cast<char *>(result.data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
failed = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("[GOOD]\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (output_bundle && !failed) {
|
if (output_bundle && !failed) {
|
||||||
|
|||||||
Reference in New Issue
Block a user