WIP adding directory structure
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
#include "KRAnimation.h"
|
||||
#include "KRAnimationManager.h"
|
||||
#include "KRContext.h"
|
||||
#include "KRNode.h"
|
||||
#include "nodes/KRNode.h"
|
||||
#include "resources/animation_curve/KRAnimationCurve.h"
|
||||
#include "KREngine-common.h"
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
#include "KRContextObject.h"
|
||||
#include "KREngine-common.h"
|
||||
#include "KRNode.h"
|
||||
#include "nodes/KRNode.h"
|
||||
#include "resources/animation_curve/KRAnimationCurve.h"
|
||||
|
||||
class KRAnimationAttribute : public KRContextObject
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
#include "block.h"
|
||||
#include "KRAudioBuffer.h"
|
||||
#include "KRContext.h"
|
||||
#include "KRCollider.h"
|
||||
#include "nodes/KRCollider.h"
|
||||
#include "siren.h"
|
||||
|
||||
using namespace mimir;
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
#include "KRContextObject.h"
|
||||
#include "block.h"
|
||||
#include "KRAudioSource.h"
|
||||
#include "nodes/KRAudioSource.h"
|
||||
#include "siren.h"
|
||||
|
||||
const int KRENGINE_AUDIO_MAX_POOL_SIZE = 60; //32;
|
||||
|
||||
@@ -38,10 +38,10 @@
|
||||
#include "resources/texture/KRTexture.h"
|
||||
#include "KRPipelineManager.h"
|
||||
#include "KRPipeline.h"
|
||||
#include "KRCamera.h"
|
||||
#include "nodes/KRCamera.h"
|
||||
#include "resources/KRResource.h"
|
||||
#include "resources/scene/KRScene.h"
|
||||
#include "KRBone.h"
|
||||
#include "nodes/KRBone.h"
|
||||
|
||||
enum class CullMode : __uint32_t;
|
||||
enum class ModelFormat : __uint8_t;
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
#include "KREngine-common.h"
|
||||
|
||||
#include "KRContext.h"
|
||||
#include "KRBone.h"
|
||||
#include "nodes/KRBone.h"
|
||||
#include "KRMeshManager.h"
|
||||
|
||||
#include "KREngine-common.h"
|
||||
@@ -51,7 +51,7 @@ using namespace kraken;
|
||||
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
||||
|
||||
#include "resources/material/KRMaterialManager.h"
|
||||
#include "KRCamera.h"
|
||||
#include "nodes/KRCamera.h"
|
||||
#include "KRViewport.h"
|
||||
|
||||
class KRMaterial;
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
#include "resources/KRResourceManager.h"
|
||||
#include "KRContextObject.h"
|
||||
#include "block.h"
|
||||
#include "KRNode.h"
|
||||
#include "nodes/KRNode.h"
|
||||
|
||||
class KRContext;
|
||||
class KRMesh;
|
||||
|
||||
@@ -31,13 +31,13 @@
|
||||
|
||||
#include "KREngine-common.h"
|
||||
|
||||
#include "KRLight.h"
|
||||
#include "nodes/KRLight.h"
|
||||
|
||||
#include "KRScene.h"
|
||||
#include "KRNode.h"
|
||||
#include "KRDirectionalLight.h"
|
||||
#include "KRSpotLight.h"
|
||||
#include "KRPointLight.h"
|
||||
#include "nodes/KRNode.h"
|
||||
#include "nodes/KRDirectionalLight.h"
|
||||
#include "nodes/KRSpotLight.h"
|
||||
#include "nodes/KRPointLight.h"
|
||||
#include "resources/audio/KRAudioManager.h"
|
||||
#include "KRRenderPass.h"
|
||||
|
||||
|
||||
@@ -33,14 +33,14 @@
|
||||
|
||||
#include "KREngine-common.h"
|
||||
|
||||
#include "KRModel.h"
|
||||
#include "nodes/KRModel.h"
|
||||
#include "resources/mesh/KRMesh.h"
|
||||
#include "KRCamera.h"
|
||||
#include "nodes/KRCamera.h"
|
||||
#include "resources/mesh/KRMeshManager.h"
|
||||
#include "KRNode.h"
|
||||
#include "KRLocator.h"
|
||||
#include "KRAmbientZone.h"
|
||||
#include "KRReverbZone.h"
|
||||
#include "nodes/KRNode.h"
|
||||
#include "nodes/KRLocator.h"
|
||||
#include "nodes/KRAmbientZone.h"
|
||||
#include "nodes/KRReverbZone.h"
|
||||
#include "KROctree.h"
|
||||
class KRModel;
|
||||
class KRLight;
|
||||
|
||||
235
kraken/resources/shader/KRShader.cpp
Normal file
235
kraken/resources/shader/KRShader.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
//
|
||||
// KRShader.cpp
|
||||
// Kraken Engine
|
||||
//
|
||||
// Copyright 2024 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"
|
||||
#include "spirv_reflect.h"
|
||||
|
||||
#include "mimir.h"
|
||||
|
||||
using namespace mimir;
|
||||
|
||||
ShaderStage getShaderStageFromExtension(const char* extension)
|
||||
{
|
||||
if (strcmp(extension, "vert") == 0) {
|
||||
return ShaderStage::vert;
|
||||
} else if (strcmp(extension, "frag") == 0) {
|
||||
return ShaderStage::frag;
|
||||
} else if (strcmp(extension, "tesc") == 0) {
|
||||
return ShaderStage::tesc;
|
||||
} else if (strcmp(extension, "tese") == 0) {
|
||||
return ShaderStage::tese;
|
||||
} else if (strcmp(extension, "geom") == 0) {
|
||||
return ShaderStage::geom;
|
||||
} else if (strcmp(extension, "comp") == 0) {
|
||||
return ShaderStage::comp;
|
||||
} else if (strcmp(extension, "mesh") == 0) {
|
||||
return ShaderStage::mesh;
|
||||
} else if (strcmp(extension, "task") == 0) {
|
||||
return ShaderStage::task;
|
||||
} else if (strcmp(extension, "rgen") == 0) {
|
||||
return ShaderStage::rgen;
|
||||
} else if (strcmp(extension, "rint") == 0) {
|
||||
return ShaderStage::rint;
|
||||
} else if (strcmp(extension, "rahit") == 0) {
|
||||
return ShaderStage::rahit;
|
||||
} else if (strcmp(extension, "rchit") == 0) {
|
||||
return ShaderStage::rchit;
|
||||
} else if (strcmp(extension, "rmiss") == 0) {
|
||||
return ShaderStage::rmiss;
|
||||
} else if (strcmp(extension, "rcall") == 0) {
|
||||
return ShaderStage::rcall;
|
||||
} else {
|
||||
return ShaderStage::Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
VkShaderStageFlagBits getShaderStageFlagBitsFromShaderStage(ShaderStage stage)
|
||||
{
|
||||
switch (stage) {
|
||||
case ShaderStage::vert:
|
||||
return VK_SHADER_STAGE_VERTEX_BIT;
|
||||
case ShaderStage::frag:
|
||||
return VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
case ShaderStage::tesc:
|
||||
return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
|
||||
case ShaderStage::tese:
|
||||
return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
|
||||
case ShaderStage::geom:
|
||||
return VK_SHADER_STAGE_GEOMETRY_BIT;
|
||||
case ShaderStage::comp:
|
||||
return VK_SHADER_STAGE_COMPUTE_BIT;
|
||||
case ShaderStage::mesh:
|
||||
return VK_SHADER_STAGE_MESH_BIT_NV;
|
||||
case ShaderStage::task:
|
||||
return VK_SHADER_STAGE_TASK_BIT_NV;
|
||||
case ShaderStage::rgen:
|
||||
return VK_SHADER_STAGE_RAYGEN_BIT_KHR;
|
||||
case ShaderStage::rint:
|
||||
return VK_SHADER_STAGE_INTERSECTION_BIT_KHR;
|
||||
case ShaderStage::rahit:
|
||||
return VK_SHADER_STAGE_ANY_HIT_BIT_KHR;
|
||||
case ShaderStage::rchit:
|
||||
return VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR;
|
||||
case ShaderStage::rmiss:
|
||||
return VK_SHADER_STAGE_MISS_BIT_KHR;
|
||||
case ShaderStage::rcall:
|
||||
return VK_SHADER_STAGE_CALLABLE_BIT_KHR;
|
||||
case ShaderStage::ShaderStageCount:
|
||||
case ShaderStage::Invalid:
|
||||
// Suppress warning
|
||||
break;
|
||||
}
|
||||
return (VkShaderStageFlagBits)0;
|
||||
}
|
||||
|
||||
KRShader::KRShader(KRContext& context, std::string name, std::string extension) : KRResource(context, name)
|
||||
{
|
||||
m_pData = new Block();
|
||||
m_extension = extension;
|
||||
m_subExtension = util::GetFileExtension(name);
|
||||
m_stage = getShaderStageFromExtension(m_subExtension.c_str());
|
||||
m_reflectionValid = false;
|
||||
|
||||
getReflection();
|
||||
}
|
||||
|
||||
KRShader::KRShader(KRContext& context, std::string name, std::string extension, Block* data) : KRResource(context, name)
|
||||
{
|
||||
m_pData = data;
|
||||
m_extension = extension;
|
||||
m_subExtension = util::GetFileExtension(name);
|
||||
m_stage = getShaderStageFromExtension(m_subExtension.c_str());
|
||||
m_reflectionValid = false;
|
||||
}
|
||||
|
||||
KRShader::~KRShader()
|
||||
{
|
||||
freeReflection();
|
||||
delete m_pData;
|
||||
}
|
||||
|
||||
std::string KRShader::getExtension()
|
||||
{
|
||||
return m_extension;
|
||||
}
|
||||
|
||||
std::string& KRShader::getSubExtension()
|
||||
{
|
||||
return m_subExtension;
|
||||
}
|
||||
|
||||
bool KRShader::save(Block& data)
|
||||
{
|
||||
data.append(*m_pData);
|
||||
return true;
|
||||
}
|
||||
|
||||
Block* KRShader::getData()
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
bool KRShader::createShaderModule(VkDevice& device, VkShaderModule& module)
|
||||
{
|
||||
bool success = true;
|
||||
VkShaderModuleCreateInfo createInfo{};
|
||||
m_pData->lock();
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
createInfo.codeSize = m_pData->getSize();
|
||||
createInfo.pCode = reinterpret_cast<const uint32_t*>(m_pData->getStart());
|
||||
|
||||
VkResult result = vkCreateShaderModule(device, &createInfo, nullptr, &module);
|
||||
|
||||
if (result != VK_SUCCESS) {
|
||||
success = false;
|
||||
}
|
||||
m_pData->unlock();
|
||||
|
||||
#if KRENGINE_DEBUG_GPU_LABELS
|
||||
if (success) {
|
||||
const std::string& name = getName();
|
||||
VkDebugUtilsObjectNameInfoEXT debugInfo{};
|
||||
debugInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
|
||||
debugInfo.objectHandle = (uint64_t)module;
|
||||
debugInfo.objectType = VK_OBJECT_TYPE_SHADER_MODULE;
|
||||
debugInfo.pObjectName = name.c_str();
|
||||
VkResult res = vkSetDebugUtilsObjectNameEXT(device, &debugInfo);
|
||||
}
|
||||
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||
return success;
|
||||
}
|
||||
|
||||
void KRShader::parseReflection()
|
||||
{
|
||||
if (m_reflectionValid) {
|
||||
return;
|
||||
}
|
||||
m_pData->lock();
|
||||
|
||||
// Generate reflection data for a shader
|
||||
SpvReflectResult result = spvReflectCreateShaderModule(m_pData->getSize(), m_pData->getStart(), &m_reflection);
|
||||
if (result != SPV_REFLECT_RESULT_SUCCESS) {
|
||||
// TODO - Log error
|
||||
return;
|
||||
}
|
||||
|
||||
m_reflectionValid = true;
|
||||
m_pData->unlock();
|
||||
}
|
||||
|
||||
void KRShader::freeReflection()
|
||||
{
|
||||
if (!m_reflectionValid) {
|
||||
return;
|
||||
}
|
||||
spvReflectDestroyShaderModule(&m_reflection);
|
||||
m_reflectionValid = false;
|
||||
}
|
||||
|
||||
|
||||
const SpvReflectShaderModule* KRShader::getReflection()
|
||||
{
|
||||
parseReflection();
|
||||
if (m_reflectionValid) {
|
||||
return &m_reflection;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ShaderStage KRShader::getShaderStage() const
|
||||
{
|
||||
return m_stage;
|
||||
}
|
||||
|
||||
VkShaderStageFlagBits KRShader::getShaderStageFlagBits() const
|
||||
{
|
||||
return getShaderStageFlagBitsFromShaderStage(m_stage);
|
||||
}
|
||||
97
kraken/resources/shader/KRShader.h
Normal file
97
kraken/resources/shader/KRShader.h
Normal file
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// KRShader.h
|
||||
// Kraken Engine
|
||||
//
|
||||
// Copyright 2024 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.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "KREngine-common.h"
|
||||
#include "KRContextObject.h"
|
||||
#include "block.h"
|
||||
#include "resources/KRResource.h"
|
||||
#include "spirv_reflect.h"
|
||||
|
||||
enum class ShaderStage : uint8_t
|
||||
{
|
||||
vert = 0,
|
||||
frag,
|
||||
tesc,
|
||||
tese,
|
||||
geom,
|
||||
comp,
|
||||
mesh,
|
||||
task,
|
||||
rgen,
|
||||
rint,
|
||||
rahit,
|
||||
rchit,
|
||||
rmiss,
|
||||
rcall,
|
||||
ShaderStageCount,
|
||||
Invalid = 0xff
|
||||
};
|
||||
|
||||
ShaderStage getShaderStageFromExtension(const char* extension);
|
||||
VkShaderStageFlagBits getShaderStageFlagBitsFromShaderStage(ShaderStage stage);
|
||||
|
||||
static const size_t kShaderStageCount = static_cast<size_t>(ShaderStage::ShaderStageCount);
|
||||
|
||||
class KRShader : public KRResource
|
||||
{
|
||||
public:
|
||||
KRShader(KRContext& context, std::string name, std::string extension);
|
||||
KRShader(KRContext& context, std::string name, std::string extension, mimir::Block* data);
|
||||
virtual ~KRShader();
|
||||
|
||||
virtual std::string getExtension();
|
||||
std::string& getSubExtension();
|
||||
|
||||
bool createShaderModule(VkDevice& device, VkShaderModule& module);
|
||||
|
||||
virtual bool save(mimir::Block& data);
|
||||
|
||||
mimir::Block* getData();
|
||||
const SpvReflectShaderModule* getReflection();
|
||||
ShaderStage getShaderStage() const;
|
||||
VkShaderStageFlagBits getShaderStageFlagBits() const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
std::string m_extension;
|
||||
std::string m_subExtension;
|
||||
mimir::Block* m_pData;
|
||||
SpvReflectShaderModule m_reflection;
|
||||
bool m_reflectionValid;
|
||||
|
||||
void parseReflection();
|
||||
void freeReflection();
|
||||
|
||||
ShaderStage m_stage;
|
||||
};
|
||||
410
kraken/resources/shader/KRShaderManager.cpp
Normal file
410
kraken/resources/shader/KRShaderManager.cpp
Normal file
@@ -0,0 +1,410 @@
|
||||
//
|
||||
// ShaderManager.cpp
|
||||
// Kraken Engine
|
||||
//
|
||||
// Copyright 2024 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"
|
||||
#include "KRContext.h"
|
||||
#include "resources/source/KRSourceManager.h"
|
||||
#include "resources/unknown/KRUnknownManager.h"
|
||||
#include "resources/unknown/KRUnknown.h"
|
||||
|
||||
#include "mimir.h"
|
||||
|
||||
using namespace mimir;
|
||||
|
||||
KRShaderManager::KRShaderManager(KRContext& context) : KRResourceManager(context)
|
||||
, m_initializedGlslang(false)
|
||||
, m_includer(&context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
KRShaderManager::~KRShaderManager()
|
||||
{
|
||||
for (unordered_map<std::string, unordered_map<std::string, KRShader*> >::iterator extension_itr = m_shaders.begin(); extension_itr != m_shaders.end(); extension_itr++) {
|
||||
for (unordered_map<std::string, KRShader*>::iterator name_itr = (*extension_itr).second.begin(); name_itr != (*extension_itr).second.end(); name_itr++) {
|
||||
delete (*name_itr).second;
|
||||
}
|
||||
}
|
||||
if (m_initializedGlslang) {
|
||||
glslang::FinalizeProcess();
|
||||
}
|
||||
}
|
||||
|
||||
KRResource* KRShaderManager::loadResource(const std::string& name, const std::string& extension, Block* data)
|
||||
{
|
||||
if (extension.compare("spv") == 0) {
|
||||
return load(name, extension, data);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
KRResource* KRShaderManager::getResource(const std::string& name, const std::string& extension)
|
||||
{
|
||||
if (extension.compare("spv") == 0) {
|
||||
return get(name, extension);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unordered_map<std::string, unordered_map<std::string, KRShader*> >& 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<std::string, unordered_map<std::string, KRShader*> >::iterator extension_itr = m_shaders.find(lower_extension);
|
||||
if (extension_itr == m_shaders.end()) {
|
||||
m_shaders[lower_extension] = unordered_map<std::string, KRShader*>();
|
||||
extension_itr = m_shaders.find(lower_extension);
|
||||
}
|
||||
|
||||
unordered_map<std::string, KRShader*>::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, Block* 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<std::string, KRShader*>& 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];
|
||||
}
|
||||
|
||||
// From glslang/StandAlone/ResourceLimits.cpp
|
||||
const TBuiltInResource DefaultTBuiltInResource = {
|
||||
/* .MaxLights = */ 32,
|
||||
/* .MaxClipPlanes = */ 6,
|
||||
/* .MaxTextureUnits = */ 32,
|
||||
/* .MaxTextureCoords = */ 32,
|
||||
/* .MaxVertexAttribs = */ 64,
|
||||
/* .MaxVertexUniformComponents = */ 4096,
|
||||
/* .MaxVaryingFloats = */ 64,
|
||||
/* .MaxVertexTextureImageUnits = */ 32,
|
||||
/* .MaxCombinedTextureImageUnits = */ 80,
|
||||
/* .MaxTextureImageUnits = */ 32,
|
||||
/* .MaxFragmentUniformComponents = */ 4096,
|
||||
/* .MaxDrawBuffers = */ 32,
|
||||
/* .MaxVertexUniformVectors = */ 128,
|
||||
/* .MaxVaryingVectors = */ 8,
|
||||
/* .MaxFragmentUniformVectors = */ 16,
|
||||
/* .MaxVertexOutputVectors = */ 16,
|
||||
/* .MaxFragmentInputVectors = */ 15,
|
||||
/* .MinProgramTexelOffset = */ -8,
|
||||
/* .MaxProgramTexelOffset = */ 7,
|
||||
/* .MaxClipDistances = */ 8,
|
||||
/* .MaxComputeWorkGroupCountX = */ 65535,
|
||||
/* .MaxComputeWorkGroupCountY = */ 65535,
|
||||
/* .MaxComputeWorkGroupCountZ = */ 65535,
|
||||
/* .MaxComputeWorkGroupSizeX = */ 1024,
|
||||
/* .MaxComputeWorkGroupSizeY = */ 1024,
|
||||
/* .MaxComputeWorkGroupSizeZ = */ 64,
|
||||
/* .MaxComputeUniformComponents = */ 1024,
|
||||
/* .MaxComputeTextureImageUnits = */ 16,
|
||||
/* .MaxComputeImageUniforms = */ 8,
|
||||
/* .MaxComputeAtomicCounters = */ 8,
|
||||
/* .MaxComputeAtomicCounterBuffers = */ 1,
|
||||
/* .MaxVaryingComponents = */ 60,
|
||||
/* .MaxVertexOutputComponents = */ 64,
|
||||
/* .MaxGeometryInputComponents = */ 64,
|
||||
/* .MaxGeometryOutputComponents = */ 128,
|
||||
/* .MaxFragmentInputComponents = */ 128,
|
||||
/* .MaxImageUnits = */ 8,
|
||||
/* .MaxCombinedImageUnitsAndFragmentOutputs = */ 8,
|
||||
/* .MaxCombinedShaderOutputResources = */ 8,
|
||||
/* .MaxImageSamples = */ 0,
|
||||
/* .MaxVertexImageUniforms = */ 0,
|
||||
/* .MaxTessControlImageUniforms = */ 0,
|
||||
/* .MaxTessEvaluationImageUniforms = */ 0,
|
||||
/* .MaxGeometryImageUniforms = */ 0,
|
||||
/* .MaxFragmentImageUniforms = */ 8,
|
||||
/* .MaxCombinedImageUniforms = */ 8,
|
||||
/* .MaxGeometryTextureImageUnits = */ 16,
|
||||
/* .MaxGeometryOutputVertices = */ 256,
|
||||
/* .MaxGeometryTotalOutputComponents = */ 1024,
|
||||
/* .MaxGeometryUniformComponents = */ 1024,
|
||||
/* .MaxGeometryVaryingComponents = */ 64,
|
||||
/* .MaxTessControlInputComponents = */ 128,
|
||||
/* .MaxTessControlOutputComponents = */ 128,
|
||||
/* .MaxTessControlTextureImageUnits = */ 16,
|
||||
/* .MaxTessControlUniformComponents = */ 1024,
|
||||
/* .MaxTessControlTotalOutputComponents = */ 4096,
|
||||
/* .MaxTessEvaluationInputComponents = */ 128,
|
||||
/* .MaxTessEvaluationOutputComponents = */ 128,
|
||||
/* .MaxTessEvaluationTextureImageUnits = */ 16,
|
||||
/* .MaxTessEvaluationUniformComponents = */ 1024,
|
||||
/* .MaxTessPatchComponents = */ 120,
|
||||
/* .MaxPatchVertices = */ 32,
|
||||
/* .MaxTessGenLevel = */ 64,
|
||||
/* .MaxViewports = */ 16,
|
||||
/* .MaxVertexAtomicCounters = */ 0,
|
||||
/* .MaxTessControlAtomicCounters = */ 0,
|
||||
/* .MaxTessEvaluationAtomicCounters = */ 0,
|
||||
/* .MaxGeometryAtomicCounters = */ 0,
|
||||
/* .MaxFragmentAtomicCounters = */ 8,
|
||||
/* .MaxCombinedAtomicCounters = */ 8,
|
||||
/* .MaxAtomicCounterBindings = */ 1,
|
||||
/* .MaxVertexAtomicCounterBuffers = */ 0,
|
||||
/* .MaxTessControlAtomicCounterBuffers = */ 0,
|
||||
/* .MaxTessEvaluationAtomicCounterBuffers = */ 0,
|
||||
/* .MaxGeometryAtomicCounterBuffers = */ 0,
|
||||
/* .MaxFragmentAtomicCounterBuffers = */ 1,
|
||||
/* .MaxCombinedAtomicCounterBuffers = */ 1,
|
||||
/* .MaxAtomicCounterBufferSize = */ 16384,
|
||||
/* .MaxTransformFeedbackBuffers = */ 4,
|
||||
/* .MaxTransformFeedbackInterleavedComponents = */ 64,
|
||||
/* .MaxCullDistances = */ 8,
|
||||
/* .MaxCombinedClipAndCullDistances = */ 8,
|
||||
/* .MaxSamples = */ 4,
|
||||
/* .maxMeshOutputVerticesNV = */ 256,
|
||||
/* .maxMeshOutputPrimitivesNV = */ 512,
|
||||
/* .maxMeshWorkGroupSizeX_NV = */ 32,
|
||||
/* .maxMeshWorkGroupSizeY_NV = */ 1,
|
||||
/* .maxMeshWorkGroupSizeZ_NV = */ 1,
|
||||
/* .maxTaskWorkGroupSizeX_NV = */ 32,
|
||||
/* .maxTaskWorkGroupSizeY_NV = */ 1,
|
||||
/* .maxTaskWorkGroupSizeZ_NV = */ 1,
|
||||
/* .maxMeshViewCountNV = */ 4,
|
||||
/* .maxMeshOutputVerticesEXT = */ 256,
|
||||
/* .maxMeshOutputPrimitivesEXT = */ 256,
|
||||
/* .maxMeshWorkGroupSizeX_EXT = */ 128,
|
||||
/* .maxMeshWorkGroupSizeY_EXT = */ 128,
|
||||
/* .maxMeshWorkGroupSizeZ_EXT = */ 128,
|
||||
/* .maxTaskWorkGroupSizeX_EXT = */ 128,
|
||||
/* .maxTaskWorkGroupSizeY_EXT = */ 128,
|
||||
/* .maxTaskWorkGroupSizeZ_EXT = */ 128,
|
||||
/* .maxMeshViewCountEXT = */ 4,
|
||||
/* .maxDualSourceDrawBuffersEXT = */ 1,
|
||||
|
||||
/* .limits = */ {
|
||||
/* .nonInductiveForLoops = */ 1,
|
||||
/* .whileLoops = */ 1,
|
||||
/* .doWhileLoops = */ 1,
|
||||
/* .generalUniformIndexing = */ 1,
|
||||
/* .generalAttributeMatrixVectorIndexing = */ 1,
|
||||
/* .generalVaryingIndexing = */ 1,
|
||||
/* .generalSamplerIndexing = */ 1,
|
||||
/* .generalVariableIndexing = */ 1,
|
||||
/* .generalConstantMatrixVectorIndexing = */ 1,
|
||||
} };
|
||||
|
||||
|
||||
bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource)
|
||||
{
|
||||
// TODO - Refactoring / cleanup needed once Vulkan shaders working...
|
||||
bool success = true;
|
||||
if (!m_initializedGlslang) {
|
||||
glslang::InitializeProcess();
|
||||
m_initializedGlslang = true;
|
||||
}
|
||||
|
||||
KRSourceManager* pSourceManager = getContext().getSourceManager();
|
||||
const unordered_map<std::string, KRSource*> vertSources = pSourceManager->get("vert");
|
||||
for (const std::pair<const std::string, KRSource*> vertSourceEntry : vertSources) {
|
||||
KRSource* vertSource = vertSourceEntry.second;
|
||||
KRSource* fragSource = pSourceManager->get(vertSourceEntry.first, "frag");
|
||||
const char* programName = vertSourceEntry.first.c_str();
|
||||
|
||||
TBuiltInResource resources;
|
||||
resources = DefaultTBuiltInResource;
|
||||
|
||||
EShMessages messages = EShMsgDefault;
|
||||
bool desktop = true;
|
||||
const int defaultVersion = desktop ? 110 : 100;
|
||||
|
||||
glslang::TShader::ForbidIncluder includer;
|
||||
glslang::TShader vertShader(EShLangVertex);
|
||||
glslang::TShader fragShader(EShLangFragment);
|
||||
vertShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
||||
vertShader.setEnvClient(glslang::EShClientVulkan, glslang::EshTargetClientVersion::EShTargetVulkan_1_1);
|
||||
fragShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
||||
fragShader.setEnvClient(glslang::EShClientVulkan, glslang::EshTargetClientVersion::EShTargetVulkan_1_1);
|
||||
glslang::TProgram program; // this must be declared after the TShader's to ensure it is deallocated before the TShader's
|
||||
if (vertSource) {
|
||||
vertSource->getData()->lock();
|
||||
}
|
||||
if (fragSource) {
|
||||
fragSource->getData()->lock();
|
||||
}
|
||||
const char* vertSourceText[1];
|
||||
const char* fragSourceText[1];
|
||||
int vertSourceLen[1];
|
||||
int fragSourceLen[1];
|
||||
const char* vertSourceNameStr[1];
|
||||
const char* fragSourceNameStr[1];
|
||||
std::string vertSourceName;
|
||||
std::string fragSourceName;
|
||||
|
||||
auto parse_shader = [&](KRSource* source, glslang::TShader& shader, const char** sourceText, int* sourceLen, const char** sourceNameStr, std::string& sourceName) {
|
||||
if (source == nullptr) {
|
||||
return;
|
||||
}
|
||||
sourceName = source->getName() + "." + source->getExtension();
|
||||
sourceText[0] = (char*)source->getData()->getStart();
|
||||
sourceLen[0] = source->getData()->getSize();
|
||||
sourceNameStr[0] = sourceName.c_str();
|
||||
shader.setStringsWithLengthsAndNames(sourceText, sourceLen, sourceNameStr, 1);
|
||||
//shader.setStrings(&sourceStr, 1);
|
||||
|
||||
if (shader.parse(&resources, defaultVersion, false, messages, m_includer)) {
|
||||
program.addShader(&shader);
|
||||
} else {
|
||||
const char* log = shader.getInfoLog();
|
||||
if (log[0] != '\0') {
|
||||
logResource->getData()->append(log);
|
||||
logResource->getData()->append("\n");
|
||||
}
|
||||
success = false;
|
||||
}
|
||||
};
|
||||
|
||||
parse_shader(vertSource, vertShader, vertSourceText, vertSourceLen, vertSourceNameStr, vertSourceName);
|
||||
parse_shader(fragSource, fragShader, fragSourceText, fragSourceLen, fragSourceNameStr, fragSourceName);
|
||||
|
||||
if (!program.link(messages)) {
|
||||
const char* log = program.getInfoLog();
|
||||
if (log[0] != '\0') {
|
||||
logResource->getData()->append(log);
|
||||
logResource->getData()->append("\n");
|
||||
}
|
||||
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);
|
||||
std::string messages = logger.getAllMessages();
|
||||
if (!messages.empty()) {
|
||||
logResource->getData()->append(messages.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()) {
|
||||
Block* data = new Block();
|
||||
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();
|
||||
}
|
||||
if (fragSource) {
|
||||
fragSource->getData()->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
KRShaderManager::Includer::Includer(KRContext* context)
|
||||
: m_context(context)
|
||||
{}
|
||||
|
||||
glslang::TShader::Includer::IncludeResult* KRShaderManager::Includer::includeSystem(
|
||||
const char* headerName,
|
||||
const char* includerName,
|
||||
size_t inclusionDepth)
|
||||
{
|
||||
fprintf(stderr, "includeSystem(%s, %s, %llu)\n", headerName, includerName, inclusionDepth);
|
||||
return nullptr;
|
||||
}
|
||||
glslang::TShader::Includer::IncludeResult* KRShaderManager::Includer::includeLocal(
|
||||
const char* headerName,
|
||||
const char* includerName,
|
||||
size_t inclusionDepth)
|
||||
{
|
||||
std::string name = util::GetFileBase(headerName);
|
||||
std::string extension = util::GetFileExtension(headerName);
|
||||
KRSource* source = m_context->getSourceManager()->get(name, extension);
|
||||
if (!source) {
|
||||
return nullptr;
|
||||
}
|
||||
Block* data = source->getData();
|
||||
data->lock();
|
||||
const char* sourceString = static_cast<const char*>(data->getStart());
|
||||
return new IncludeResult(std::string(headerName), sourceString, data->getSize(), static_cast<void*>(data));
|
||||
}
|
||||
|
||||
void KRShaderManager::Includer::releaseInclude(IncludeResult* includeResult)
|
||||
{
|
||||
Block* data = static_cast<Block*>(includeResult->userData);
|
||||
data->unlock();
|
||||
}
|
||||
84
kraken/resources/shader/KRShaderManager.h
Normal file
84
kraken/resources/shader/KRShaderManager.h
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// ShaderManager.h
|
||||
// Kraken Engine
|
||||
//
|
||||
// Copyright 2024 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.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "KREngine-common.h"
|
||||
|
||||
#include "resources/KRResourceManager.h"
|
||||
|
||||
#include "KRShader.h"
|
||||
#include "KRContextObject.h"
|
||||
#include "block.h"
|
||||
|
||||
class KRUnknown;
|
||||
|
||||
class KRShaderManager : public KRResourceManager
|
||||
{
|
||||
public:
|
||||
KRShaderManager(KRContext& context);
|
||||
virtual ~KRShaderManager();
|
||||
|
||||
virtual KRResource* loadResource(const std::string& name, const std::string& extension, mimir::Block* data) override;
|
||||
virtual KRResource* getResource(const std::string& name, const std::string& extension) override;
|
||||
|
||||
void add(KRShader* shader);
|
||||
|
||||
KRShader* load(const std::string& name, const std::string& extension, mimir::Block* data);
|
||||
KRShader* get(const std::string& name, const std::string& extension);
|
||||
|
||||
bool compileAll(KRBundle* outputBundle, KRUnknown* logResource);
|
||||
|
||||
const unordered_map<std::string, KRShader*>& get(const std::string& extension);
|
||||
|
||||
unordered_map<std::string, unordered_map<std::string, KRShader*> >& getShaders();
|
||||
|
||||
class Includer : public glslang::TShader::Includer
|
||||
{
|
||||
public:
|
||||
Includer() = delete;
|
||||
Includer(KRContext* context);
|
||||
IncludeResult* includeSystem(const char* headerName,
|
||||
const char* includerName,
|
||||
size_t inclusionDepth) override;
|
||||
IncludeResult* includeLocal(const char* headerName,
|
||||
const char* includerName,
|
||||
size_t inclusionDepth) override;
|
||||
void releaseInclude(IncludeResult* includeResult) override;
|
||||
private:
|
||||
KRContext* m_context;
|
||||
};
|
||||
|
||||
private:
|
||||
unordered_map<std::string, unordered_map<std::string, KRShader*> > m_shaders;
|
||||
bool m_initializedGlslang;
|
||||
Includer m_includer;
|
||||
};
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
#include "KRSourceManager.h"
|
||||
#include "KREngine-common.h"
|
||||
#include "KRShader.h"
|
||||
#include "resources/shader/KRShader.h"
|
||||
|
||||
using namespace mimir;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user