Compiling shaders to SPIR-V 1.5
WIP Vulkan Pipeline initialization
This commit is contained in:
@@ -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;
|
||||
#else
|
||||
// Not implemented for this platform
|
||||
@@ -1214,4 +1216,15 @@ void KRContext::presentationThreadFunc()
|
||||
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();
|
||||
}
|
||||
@@ -128,6 +128,28 @@ public:
|
||||
|
||||
static void activateStreamerContext();
|
||||
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
|
||||
static void attachToView(void *view);
|
||||
@@ -177,24 +199,7 @@ private:
|
||||
void destroySurfaces();
|
||||
|
||||
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;
|
||||
std::thread m_presentationThread;
|
||||
void presentationThreadFunc();
|
||||
|
||||
@@ -108,6 +108,41 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = {
|
||||
"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)
|
||||
{
|
||||
strcpy(m_szKey, szKey);
|
||||
|
||||
@@ -40,8 +40,11 @@
|
||||
#include "KRNode.h"
|
||||
#include "KRViewport.h"
|
||||
|
||||
class KRShader;
|
||||
|
||||
class KRPipeline : public KRContextObject {
|
||||
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);
|
||||
virtual ~KRPipeline();
|
||||
const char *getKey() const;
|
||||
@@ -119,18 +122,6 @@ public:
|
||||
KRENGINE_UNIFORM_FADE_COLOR,
|
||||
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[];
|
||||
GLint m_uniforms[KRENGINE_NUM_UNIFORMS];
|
||||
|
||||
@@ -62,6 +62,21 @@ KRPipelineManager::~KRPipelineManager() {
|
||||
#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) {
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ class KRPipelineManager : public KRContextObject {
|
||||
public:
|
||||
KRPipelineManager(KRContext &context);
|
||||
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);
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ KRShader::KRShader(KRContext &context, std::string name, std::string extension)
|
||||
{
|
||||
m_pData = new KRDataBlock();
|
||||
m_extension = extension;
|
||||
m_subExtension = KRResource::GetFileExtension(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;
|
||||
}
|
||||
|
||||
std::string& KRShader::getSubExtension()
|
||||
{
|
||||
return m_subExtension;
|
||||
}
|
||||
|
||||
bool KRShader::save(KRDataBlock &data)
|
||||
{
|
||||
data.append(*m_pData);
|
||||
@@ -73,8 +79,9 @@ bool KRShader::createShaderModule(VkDevice& device, VkShaderModule& module)
|
||||
createInfo.codeSize = m_pData->getSize();
|
||||
createInfo.pCode = reinterpret_cast<const uint32_t*>(m_pData->getStart());
|
||||
|
||||
VkShaderModule shaderModule;
|
||||
if (vkCreateShaderModule(device, &createInfo, nullptr, &module) != VK_SUCCESS) {
|
||||
VkResult result = vkCreateShaderModule(device, &createInfo, nullptr, &module);
|
||||
|
||||
if(result != VK_SUCCESS) {
|
||||
success = false;
|
||||
}
|
||||
m_pData->unlock();
|
||||
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
virtual ~KRShader();
|
||||
|
||||
virtual std::string getExtension();
|
||||
std::string& getSubExtension();
|
||||
|
||||
bool createShaderModule(VkDevice& device, VkShaderModule& module);
|
||||
|
||||
@@ -55,6 +56,7 @@ public:
|
||||
private:
|
||||
|
||||
std::string m_extension;
|
||||
std::string m_subExtension;
|
||||
KRDataBlock *m_pData;
|
||||
};
|
||||
|
||||
|
||||
@@ -257,6 +257,8 @@ bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource)
|
||||
glslang::TShader::ForbidIncluder includer;
|
||||
glslang::TShader vertShader(EShLangVertex);
|
||||
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
|
||||
if (vertSource) {
|
||||
vertSource->getData()->lock();
|
||||
|
||||
@@ -38,8 +38,6 @@ layout (binding = 1) uniform sampler2D diffuseTexture;
|
||||
layout (location = 0) in vec2 textureCoordinate;
|
||||
layout (location = 0) out vec4 colorOut;
|
||||
|
||||
in mediump vec2 texCoord;
|
||||
|
||||
void main() {
|
||||
colorOut = vec4(vec3(texture(diffuseTexture, texCoord)), 1.0) * material.alpha;
|
||||
colorOut = vec4(vec3(texture(diffuseTexture, textureCoordinate)), 1.0) * material.alpha;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user