From 157c8f8a2032b43549e3a40bc23a5cce601b3fdb Mon Sep 17 00:00:00 2001 From: niki Date: Mon, 21 Mar 2022 15:26:27 +0100 Subject: [PATCH] include custom vulkan shader in love source --- src/modules/graphics/Shader.cpp | 41 ++++++++++++++++- src/modules/graphics/vulkan/Graphics.cpp | 58 ++++++++---------------- src/modules/graphics/vulkan/Graphics.h | 1 + 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/modules/graphics/Shader.cpp b/src/modules/graphics/Shader.cpp index 6b89b26f7..d5b4cfaad 100644 --- a/src/modules/graphics/Shader.cpp +++ b/src/modules/graphics/Shader.cpp @@ -432,6 +432,37 @@ void main() { } )"; +static const char vulkan_vert[] = R"( +#version 450 + +layout(location = 0) in vec2 inPosition; + +layout(location = 0) out vec4 fragColor; + +float windowWidth = 800; +float windowHeight = 600; + +void main() { + gl_Position = vec4( + 2 * inPosition.x / windowWidth - 1, + 2 * inPosition.y / windowHeight - 1, + 0.0, 1.0); + fragColor = vec4(1, 1, 1, 1); +} +)"; + +static const char vulkan_pixel[] = R"( +#version 450 + +layout(location = 0) in vec4 fragColor; + +layout(location = 0) out vec4 outColor; + +void main() { + outColor = fragColor; +} +)"; + struct StageInfo { const char *name; @@ -576,8 +607,14 @@ std::string Shader::createShaderStageCode(Graphics *gfx, ShaderStageType stage, if (glsl1on3) lang = LANGUAGE_GLSL3; - if (info.vulkan) - lang = LANGUAGE_GLSL4; + if (info.vulkan) { + if (stage == SHADERSTAGE_VERTEX) { + return love::graphics::glsl::vulkan_vert; + } + if (stage == SHADERSTAGE_PIXEL) { + return love::graphics::glsl::vulkan_pixel; + } + } glsl::StageInfo stageinfo = glsl::stageInfo[stage]; diff --git a/src/modules/graphics/vulkan/Graphics.cpp b/src/modules/graphics/vulkan/Graphics.cpp index da3767c86..4bbc5a2bd 100644 --- a/src/modules/graphics/vulkan/Graphics.cpp +++ b/src/modules/graphics/vulkan/Graphics.cpp @@ -66,6 +66,7 @@ namespace love { createSwapChain(); createImageViews(); createRenderPass(); + createDefaultShaders(); createGraphicsPipeline(); createFramebuffers(); createCommandPool(); @@ -215,17 +216,6 @@ namespace love { batchedDrawState.indexBuffer = new StreamBuffer(device, physicalDevice, BUFFERUSAGE_INDEX, sizeof(uint16) * LOVE_UINT16_MAX); } - for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++) { - auto stype = (Shader::StandardShader)i; - - if (!Shader::standardShaders[i]) { - std::vector stages; - stages.push_back(Shader::getDefaultCode(stype, SHADERSTAGE_VERTEX)); - stages.push_back(Shader::getDefaultCode(stype, SHADERSTAGE_PIXEL)); - Shader::standardShaders[i] = newShader(stages, true); - } - } - return true; } @@ -696,29 +686,22 @@ namespace love { return shaderModule; } + void Graphics::createDefaultShaders() { + for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++) { + auto stype = (Shader::StandardShader)i; + + if (!Shader::standardShaders[i]) { + std::vector stages; + stages.push_back(Shader::getDefaultCode(stype, SHADERSTAGE_VERTEX)); + stages.push_back(Shader::getDefaultCode(stype, SHADERSTAGE_PIXEL)); + Shader::standardShaders[i] = newShader(stages, true); + } + } + } + void Graphics::createGraphicsPipeline() { - // love::graphics::vulkan::Shader* shader = dynamic_cast(getShader()); - // auto shaderStages = shader->getShaderStages(); - - auto vertShaderCode = readFile("vert.spv"); - auto fragShaderCode = readFile("frag.spv"); - - VkShaderModule vertShaderModule = createShaderModule(device, vertShaderCode); - VkShaderModule fragShaderModule = createShaderModule(device, fragShaderCode); - - VkPipelineShaderStageCreateInfo vertShaderStageInfo{}; - vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; - vertShaderStageInfo.module = vertShaderModule; - vertShaderStageInfo.pName = "main"; - - VkPipelineShaderStageCreateInfo fragShaderStageInfo{}; - fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; - fragShaderStageInfo.module = fragShaderModule; - fragShaderStageInfo.pName = "main"; - - VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo }; + auto shader = reinterpret_cast(love::graphics::vulkan::Shader::standardShaders[Shader::STANDARD_DEFAULT]); + auto shaderStages = shader->getShaderStages(); VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; @@ -812,10 +795,8 @@ namespace love { VkGraphicsPipelineCreateInfo pipelineInfo{}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; - // pipelineInfo.stageCount = static_cast(shaderStages.size()); - // pipelineInfo.pStages = shaderStages.data(); - pipelineInfo.stageCount = 2; - pipelineInfo.pStages = shaderStages; + pipelineInfo.stageCount = static_cast(shaderStages.size()); + pipelineInfo.pStages = shaderStages.data(); pipelineInfo.pVertexInputState = &vertexInputInfo; pipelineInfo.pInputAssemblyState = &inputAssembly; pipelineInfo.pViewportState = &viewportState; @@ -833,9 +814,6 @@ namespace love { if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) { throw love::Exception("failed to create graphics pipeline"); } - - vkDestroyShaderModule(device, vertShaderModule, nullptr); - vkDestroyShaderModule(device, fragShaderModule, nullptr); } void Graphics::createFramebuffers() { diff --git a/src/modules/graphics/vulkan/Graphics.h b/src/modules/graphics/vulkan/Graphics.h index 79f9f2334..e46d44a96 100644 --- a/src/modules/graphics/vulkan/Graphics.h +++ b/src/modules/graphics/vulkan/Graphics.h @@ -116,6 +116,7 @@ namespace love { void createSwapChain(); void createImageViews(); void createRenderPass(); + void createDefaultShaders(); void createGraphicsPipeline(); void createFramebuffers(); void createCommandPool();