diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index cc936b2..52a2fd4 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -67,6 +67,8 @@ add_sources(KRResource.cpp) add_sources(KRReverbZone.cpp) add_sources(KRScene.cpp) add_sources(KRSceneManager.cpp) +add_sources(KRShader.cpp) +add_sources(KRShaderManager.cpp) add_sources(KRSource.cpp) add_sources(KRSourceManager.cpp) add_sources(KRPipeline.cpp) diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index d4cf4d7..a3617c0 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -83,6 +83,7 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) m_pAnimationCurveManager = new KRAnimationCurveManager(*this); m_pSoundManager = new KRAudioManager(*this); m_pUnknownManager = new KRUnknownManager(*this); + m_pShaderManager = new KRShaderManager(*this); m_pSourceManager = new KRSourceManager(*this); m_streamingEnabled = true; @@ -224,6 +225,9 @@ KRAnimationCurveManager *KRContext::getAnimationCurveManager() { KRAudioManager *KRContext::getAudioManager() { return m_pSoundManager; } +KRShaderManager *KRContext::getShaderManager() { + return m_pShaderManager; +} KRSourceManager *KRContext::getSourceManager() { return m_pSourceManager; } @@ -262,6 +266,13 @@ std::vector KRContext::getResources() resources.push_back((*itr2).second); } } + + unordered_map > shaders = m_pShaderManager->getShaders(); + for(unordered_map >::iterator itr = shaders.begin(); itr != shaders.end(); itr++) { + for(unordered_map::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) { + resources.push_back((*itr2).second); + } + } unordered_map > unknowns = m_pUnknownManager->getUnknowns(); for(unordered_map >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) { @@ -297,6 +308,9 @@ KRResource* KRContext::loadResource(const std::string &file_name, KRDataBlock *d resource = m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); } else if(extension.compare("tga") == 0) { resource = m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); + } else if(extension.compare("spv") == 0) { + // SPIR-V shader binary + resource = m_pShaderManager->load(name, extension, data); } else if(extension.compare("vert") == 0) { // vertex shader resource = m_pSourceManager->load(name, extension, data); diff --git a/kraken/KRContext.h b/kraken/KRContext.h index a768fe8..4908f2f 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -19,6 +19,7 @@ #include "KRAnimationManager.h" #include "KRAnimationCurveManager.h" #include "KRUnknownManager.h" +#include "KRShaderManager.h" #include "KRSourceManager.h" #include "KRStreamer.h" @@ -58,6 +59,7 @@ public: KRAnimationCurveManager *getAnimationCurveManager(); KRAudioManager *getAudioManager(); KRUnknownManager *getUnknownManager(); + KRShaderManager *getShaderManager(); KRSourceManager *getSourceManager(); KRCamera *createCamera(int width, int height); @@ -120,6 +122,7 @@ private: KRAnimationCurveManager *m_pAnimationCurveManager; KRAudioManager *m_pSoundManager; KRUnknownManager *m_pUnknownManager; + KRShaderManager *m_pShaderManager; KRSourceManager *m_pSourceManager; KRResource** m_resourceMap; diff --git a/kraken/KRShader.cpp b/kraken/KRShader.cpp new file mode 100644 index 0000000..620bf32 --- /dev/null +++ b/kraken/KRShader.cpp @@ -0,0 +1,65 @@ +// +// KRShader.cpp +// KREngine +// +// Copyright 2019 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRShader.h" + +KRShader::KRShader(KRContext &context, std::string name, std::string extension) : KRResource(context, name) +{ + m_pData = new KRDataBlock(); + m_extension = extension; +} + +KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name) +{ + m_pData = data; + m_extension = extension; +} + +KRShader::~KRShader() +{ + delete m_pData; +} + +std::string KRShader::getExtension() +{ + return m_extension; +} + +bool KRShader::save(KRDataBlock &data) +{ + data.append(*m_pData); + return true; +} + +KRDataBlock *KRShader::getData() +{ + return m_pData; +} diff --git a/kraken/KRShader.h b/kraken/KRShader.h new file mode 100644 index 0000000..fd52eca --- /dev/null +++ b/kraken/KRShader.h @@ -0,0 +1,59 @@ +// +// KRShader.h +// KREngine +// +// Copyright 2019 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRSHADER_H +#define KRSHADER_H + +#include "KREngine-common.h" +#include "KRContextObject.h" +#include "KRDataBlock.h" +#include "KRResource.h" + +class KRShader : public KRResource { + +public: + KRShader(KRContext &context, std::string name, std::string extension); + KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data); + virtual ~KRShader(); + + virtual std::string getExtension(); + + virtual bool save(KRDataBlock &data); + + KRDataBlock *getData(); + +private: + + std::string m_extension; + KRDataBlock *m_pData; +}; + +#endif /* defined(KRSHADER_H) */ diff --git a/kraken/KRShaderManager.cpp b/kraken/KRShaderManager.cpp new file mode 100644 index 0000000..068a427 --- /dev/null +++ b/kraken/KRShaderManager.cpp @@ -0,0 +1,102 @@ +// +// ShaderManager.cpp +// KREngine +// +// Copyright 2019 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRShaderManager.h" +#include "KREngine-common.h" + +KRShaderManager::KRShaderManager(KRContext &context) : KRContextObject(context) +{ + +} + +KRShaderManager::~KRShaderManager() +{ + for(unordered_map >::iterator extension_itr = m_shaders.begin(); extension_itr != m_shaders.end(); extension_itr++) { + for(unordered_map::iterator name_itr=(*extension_itr).second.begin(); name_itr != (*extension_itr).second.end(); name_itr++) { + delete (*name_itr).second; + } + } +} + +unordered_map > &KRShaderManager::getShaders() +{ + return m_shaders; +} + +void KRShaderManager::add(KRShader *shader) +{ + std::string lower_name = shader->getName(); + std::string lower_extension = shader->getExtension(); + + std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower); + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + + unordered_map >::iterator extension_itr = m_shaders.find(lower_extension); + if(extension_itr == m_shaders.end()) { + m_shaders[lower_extension] = unordered_map(); + extension_itr = m_shaders.find(lower_extension); + } + + unordered_map::iterator name_itr = (*extension_itr).second.find(lower_name); + if(name_itr != (*extension_itr).second.end()) { + delete (*name_itr).second; + (*name_itr).second = shader; + } else { + (*extension_itr).second[lower_name] = shader; + } +} + +KRShader *KRShaderManager::load(const std::string &name, const std::string &extension, KRDataBlock *data) +{ + KRShader *shader = new KRShader(getContext(), name, extension, data); + if(shader) add(shader); + return shader; +} + +KRShader *KRShaderManager::get(const std::string &name, const std::string &extension) +{ + std::string lower_name = name; + std::string lower_extension = extension; + + std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower); + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + + return m_shaders[lower_extension][lower_name]; +} + + +const unordered_map &KRShaderManager::get(const std::string &extension) +{ + std::string lower_extension = extension; + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + return m_shaders[lower_extension]; +} + diff --git a/kraken/KRShaderManager.h b/kraken/KRShaderManager.h new file mode 100644 index 0000000..f0117fa --- /dev/null +++ b/kraken/KRShaderManager.h @@ -0,0 +1,60 @@ +// +// ShaderManager.h +// KREngine +// +// Copyright 2019 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRSHADER_MANAGER_H +#define KRSHADER_MANAGER_H + +#include "KREngine-common.h" + +#include "KRShader.h" +#include "KRContextObject.h" +#include "KRDataBlock.h" + +class KRShaderManager : public KRContextObject { +public: + KRShaderManager(KRContext &context); + virtual ~KRShaderManager(); + + void add(KRShader *shader); + + KRShader *load(const std::string &name, const std::string &extension, KRDataBlock *data); + KRShader *get(const std::string &name, const std::string &extension); + + + const unordered_map &get(const std::string &extension); + + unordered_map > &getShaders(); + +private: + unordered_map > m_shaders; +}; + +#endif /* defined(KRUNKNOWN_MANAGER_H) */ diff --git a/kraken/KRSourceManager.cpp b/kraken/KRSourceManager.cpp index b7b3662..05628b7 100644 --- a/kraken/KRSourceManager.cpp +++ b/kraken/KRSourceManager.cpp @@ -2,7 +2,7 @@ // SourceManager.cpp // KREngine // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2019 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: diff --git a/kraken/KRSourceManager.h b/kraken/KRSourceManager.h index b3eeabe..390a1da 100644 --- a/kraken/KRSourceManager.h +++ b/kraken/KRSourceManager.h @@ -1,8 +1,8 @@ // -// FileManager.h +// SourceManager.h // KREngine // -// Copyright 2012 Kearwood Gilbert. All rights reserved. +// Copyright 2019 Kearwood Gilbert. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are // permitted provided that the following conditions are met: