X-Git-Url: https://eleni.mutantstargoat.com/git/?p=demo;a=blobdiff_plain;f=src%2Fvulkan%2Fshader-vk.cc;fp=src%2Fvulkan%2Fshader-vk.cc;h=d53883d167ee5da93dd2ad296052a7241fa5d4a5;hp=eb22b97b1764fc193764d3af4fc61a3408afe947;hb=855c42d8e50fff743fd7b1be5e91cb0db18def77;hpb=ecb47604bb2b8ab3ac5a133e78ef6cb19a20d9cc diff --git a/src/vulkan/shader-vk.cc b/src/vulkan/shader-vk.cc index eb22b97..d53883d 100644 --- a/src/vulkan/shader-vk.cc +++ b/src/vulkan/shader-vk.cc @@ -2,8 +2,6 @@ #include "shader-vk.h" /* static variables */ -static vku_buffer ubo; - ShaderVK::ShaderVK() { } @@ -13,18 +11,50 @@ ShaderVK::~ShaderVK() destroy(); } -bool ShaderVK::create(char *buf, unsigned int bsz, const char *fname) +bool ShaderVK::load(const char *fname, SType type) { - return true; + char *vk_fname = new char[strlen(fname) + strlen(".spirv") + 1]; + strcpy(vk_fname, fname); + + char *suffix = strrchr(vk_fname, '.'); + + if(suffix) { + *suffix = 0; + } + + strcat(vk_fname, ".spirv"); + + bool res = Shader::load(vk_fname, type); + delete [] vk_fname; + + return res; } -bool ShaderVK::load(const char *fname, SType type) +bool ShaderVK::create(char *buf, unsigned int bsz, const char *fname) { + name = std::string(fname); + + VkShaderModuleCreateInfo sminf; + memset(&sminf, 0, sizeof sminf); + sminf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + + sminf.codeSize = bsz; + sminf.pCode = (uint32_t*)buf; + + if(vkCreateShaderModule(vk_device, &sminf, 0, &sm) != VK_SUCCESS) { + delete [] buf; + + fprintf(stderr, "Failed to create vertex shader module.\n"); + return false; + } + + delete [] buf; return true; } void ShaderVK::destroy() { + vkDestroyShaderModule(vk_device, sm, 0); } ShaderProgramVK::ShaderProgramVK() @@ -37,6 +67,44 @@ ShaderProgramVK::~ShaderProgramVK() bool ShaderProgramVK::create() { + /* pipeline cache */ + VkPipelineCacheCreateInfo pcinf; + memset(&pcinf, 0, sizeof pcinf); + pcinf.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; + + VkPipelineCache pcache; + if(!vkCreatePipelineCache(vk_device, &pcinf, 0, &pcache) != VK_SUCCESS) { + fprintf(stderr, "Failed to create pipeline cache.\n"); + return false; + } + + /* pipeline */ + VkGraphicsPipelineCreateInfo gpinf; + memset(&gpinf, 0, sizeof gpinf); + gpinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + gpinf.stageCount = 2; + + VkPipelineShaderStageCreateInfo ssinf[2]; + for(int i=0; i<2; i++) { + memset(&ssinf[i], 0, sizeof ssinf[i]); + ssinf[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + + switch(shaders[i]->get_type()) { + case SDR_VERTEX: + ssinf[i].stage = VK_SHADER_STAGE_VERTEX_BIT; + break; + case SDR_FRAGMENT: + ssinf[i].stage = VK_SHADER_STAGE_FRAGMENT_BIT; + break; + default: + fprintf(stderr, "Failed to create graphics pipeline: Invalid shader type.\n"); + return false; + } + ssinf[i].module = ((ShaderVK*)shaders[i])->sm; + } + + gpinf.pStages = ssinf; + return true; } @@ -98,4 +166,4 @@ void ShaderProgramVK::set_uniformf(int location, float x, float y, float z, floa void ShaderProgramVK::set_uniform_matrix(int location, const Mat4 &mat) { -} \ No newline at end of file +}