Compiling shaders to SPIR-V 1.5

WIP Vulkan Pipeline initialization
This commit is contained in:
2021-08-10 20:13:32 -07:00
parent 8b59c5c382
commit ce7ecbdd9a
10 changed files with 104 additions and 35 deletions

View File

@@ -1162,6 +1162,8 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
} }
} }
m_pPipelineManager->createPipelines(info.logicalDevice); // TODO - Support multiple surfaces. Device needs to be passed in.
return KR_SUCCESS; return KR_SUCCESS;
#else #else
// Not implemented for this platform // Not implemented for this platform
@@ -1214,4 +1216,15 @@ void KRContext::presentationThreadFunc()
void KRContext::renderFrame() void KRContext::renderFrame()
{ {
}
KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(size_t index)
{
assert(index < m_surfaces.size());
return m_surfaces[index];
}
size_t KRContext::GetSurfaceCount() const
{
return m_surfaces.size();
} }

View File

@@ -128,6 +128,28 @@ public:
static void activateStreamerContext(); static void activateStreamerContext();
static void activateRenderContext(); static void activateRenderContext();
typedef struct {
KrSurfaceHandle surfaceHandle;
VkSurfaceKHR surface;
VkPhysicalDevice device;
VkDevice logicalDevice;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
std::vector<VkImageView> swapChainImageViews;
#ifdef WIN32
HWND hWnd;
#endif
} SurfaceInfo;
SurfaceInfo& GetSurfaceInfo(size_t index);
size_t GetSurfaceCount() const;
#if TARGET_OS_MAC #if TARGET_OS_MAC
static void attachToView(void *view); static void attachToView(void *view);
@@ -177,24 +199,7 @@ private:
void destroySurfaces(); void destroySurfaces();
unordered_multimap<std::string, KRResource*> m_resources; unordered_multimap<std::string, KRResource*> m_resources;
typedef struct {
KrSurfaceHandle surfaceHandle;
VkSurfaceKHR surface;
VkPhysicalDevice device;
VkDevice logicalDevice;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
std::vector<VkImageView> swapChainImageViews;
#ifdef WIN32
HWND hWnd;
#endif
} SurfaceInfo;
unordered_map<KrSurfaceHandle, SurfaceInfo> m_surfaces; unordered_map<KrSurfaceHandle, SurfaceInfo> m_surfaces;
std::thread m_presentationThread; std::thread m_presentationThread;
void presentationThreadFunc(); void presentationThreadFunc();

View File

@@ -108,6 +108,41 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = {
"fade_color", // KRENGINE_UNIFORM_FADE_COLOR "fade_color", // KRENGINE_UNIFORM_FADE_COLOR
}; };
KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, const std::vector<KRShader*>& shaders)
: KRContextObject(context)
, m_iProgram(0) // not used for Vulkan
{
strcpy(m_szKey, szKey);
const int kMaxStages = 4;
VkPipelineShaderStageCreateInfo stages[kMaxStages];
std::vector<std::string> stageNames;
memset(static_cast<void*>(stages), 0, sizeof(VkPipelineShaderStageCreateInfo) * kMaxStages);
size_t stage_count = 0;
for (KRShader* shader : shaders) {
VkShaderModule shaderModule;
if (!shader->createShaderModule(device, shaderModule)) {
// failed! TODO - Error handling
}
VkPipelineShaderStageCreateInfo& stageInfo = stages[stage_count++];
stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
if (shader->getSubExtension().compare("vert") == 0) {
stageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
} else if (shader->getSubExtension().compare("frag") == 0) {
stageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
} else {
// failed! TODO - Error handling
}
stageInfo.module = shaderModule;
const std::string& stageName = stageNames.emplace_back(shader->getName());
stageInfo.pName = stageName.c_str();
}
// TODO - WIP, need to complete
}
KRPipeline::KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) : KRContextObject(context) KRPipeline::KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) : KRContextObject(context)
{ {
strcpy(m_szKey, szKey); strcpy(m_szKey, szKey);

View File

@@ -40,8 +40,11 @@
#include "KRNode.h" #include "KRNode.h"
#include "KRViewport.h" #include "KRViewport.h"
class KRShader;
class KRPipeline : public KRContextObject { class KRPipeline : public KRContextObject {
public: public:
KRPipeline(KRContext& context, VkDevice& device, const char* szKey, const std::vector<KRShader*>& shaders);
KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource); KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource);
virtual ~KRPipeline(); virtual ~KRPipeline();
const char *getKey() const; const char *getKey() const;
@@ -119,18 +122,6 @@ public:
KRENGINE_UNIFORM_FADE_COLOR, KRENGINE_UNIFORM_FADE_COLOR,
KRENGINE_NUM_UNIFORMS KRENGINE_NUM_UNIFORMS
}; };
/*
typedef enum {
KRENGINE_UNIFORM_TYPE_UNKNOWN,
KRENGINE_UNIFORM_TYPE_FLOAT,
KRENGINE_UNIFORM_TYPE_INT,
KRENGINE_UNIFORM_TYPE_VECTOR2,
KRENGINE_UNIFORM_TYPE_VECTOR3,
KRENGINE_UNIFORM_TYPE_VECTOR4,
KRENGINE_UNIFORM_TYPE_MAT4
} uniform_type_t;
uniform_type_t m_uniform_type[KRENGINE_NUM_UNIFORMS];
*/
static const char *KRENGINE_UNIFORM_NAMES[]; static const char *KRENGINE_UNIFORM_NAMES[];
GLint m_uniforms[KRENGINE_NUM_UNIFORMS]; GLint m_uniforms[KRENGINE_NUM_UNIFORMS];

View File

@@ -62,6 +62,21 @@ KRPipelineManager::~KRPipelineManager() {
#endif // ANDROID #endif // ANDROID
} }
void KRPipelineManager::createPipelines(VkDevice& device)
{
{
// simple_blit
std::string pipeline_name = "simple_blit";
std::vector<KRShader*> shaders;
shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".vert", "spv"));
shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".frag", "spv"));
KRPipeline* pipeline = new KRPipeline(*m_pContext, device, pipeline_name.c_str(), shaders);
std::pair<std::string, std::vector<int> > key;
key.first = pipeline_name;
m_pipelines[key] = pipeline;
}
}
KRPipeline *KRPipelineManager::getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor) { KRPipeline *KRPipelineManager::getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor) {

View File

@@ -51,6 +51,7 @@ class KRPipelineManager : public KRContextObject {
public: public:
KRPipelineManager(KRContext &context); KRPipelineManager(KRContext &context);
virtual ~KRPipelineManager(); virtual ~KRPipelineManager();
void createPipelines(VkDevice& device);
KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false); KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false);

View File

@@ -35,6 +35,7 @@ KRShader::KRShader(KRContext &context, std::string name, std::string extension)
{ {
m_pData = new KRDataBlock(); m_pData = new KRDataBlock();
m_extension = extension; m_extension = extension;
m_subExtension = KRResource::GetFileExtension(name);
} }
KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name) KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name)
@@ -53,6 +54,11 @@ std::string KRShader::getExtension()
return m_extension; return m_extension;
} }
std::string& KRShader::getSubExtension()
{
return m_subExtension;
}
bool KRShader::save(KRDataBlock &data) bool KRShader::save(KRDataBlock &data)
{ {
data.append(*m_pData); data.append(*m_pData);
@@ -73,8 +79,9 @@ bool KRShader::createShaderModule(VkDevice& device, VkShaderModule& module)
createInfo.codeSize = m_pData->getSize(); createInfo.codeSize = m_pData->getSize();
createInfo.pCode = reinterpret_cast<const uint32_t*>(m_pData->getStart()); createInfo.pCode = reinterpret_cast<const uint32_t*>(m_pData->getStart());
VkShaderModule shaderModule; VkResult result = vkCreateShaderModule(device, &createInfo, nullptr, &module);
if (vkCreateShaderModule(device, &createInfo, nullptr, &module) != VK_SUCCESS) {
if(result != VK_SUCCESS) {
success = false; success = false;
} }
m_pData->unlock(); m_pData->unlock();

View File

@@ -45,6 +45,7 @@ public:
virtual ~KRShader(); virtual ~KRShader();
virtual std::string getExtension(); virtual std::string getExtension();
std::string& getSubExtension();
bool createShaderModule(VkDevice& device, VkShaderModule& module); bool createShaderModule(VkDevice& device, VkShaderModule& module);
@@ -55,6 +56,7 @@ public:
private: private:
std::string m_extension; std::string m_extension;
std::string m_subExtension;
KRDataBlock *m_pData; KRDataBlock *m_pData;
}; };

View File

@@ -257,6 +257,8 @@ bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource)
glslang::TShader::ForbidIncluder includer; glslang::TShader::ForbidIncluder includer;
glslang::TShader vertShader(EShLangVertex); glslang::TShader vertShader(EShLangVertex);
glslang::TShader fragShader(EShLangFragment); glslang::TShader fragShader(EShLangFragment);
vertShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_5);
fragShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_5);
glslang::TProgram program; // this must be declared after the TShader's to ensure it is deallocated before the TShader's glslang::TProgram program; // this must be declared after the TShader's to ensure it is deallocated before the TShader's
if (vertSource) { if (vertSource) {
vertSource->getData()->lock(); vertSource->getData()->lock();

View File

@@ -38,8 +38,6 @@ layout (binding = 1) uniform sampler2D diffuseTexture;
layout (location = 0) in vec2 textureCoordinate; layout (location = 0) in vec2 textureCoordinate;
layout (location = 0) out vec4 colorOut; layout (location = 0) out vec4 colorOut;
in mediump vec2 texCoord;
void main() { void main() {
colorOut = vec4(vec3(texture(diffuseTexture, texCoord)), 1.0) * material.alpha; colorOut = vec4(vec3(texture(diffuseTexture, textureCoordinate)), 1.0) * material.alpha;
} }