include custom vulkan shader in love source

This commit is contained in:
niki
2022-03-21 15:26:27 +01:00
parent 197dfb9738
commit 157c8f8a20
3 changed files with 58 additions and 42 deletions
+39 -2
View File
@@ -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];
+18 -40
View File
@@ -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<std::string> 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<std::string> 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<love::graphics::vulkan::Shader*>(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*>(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<uint32_t>(shaderStages.size());
// pipelineInfo.pStages = shaderStages.data();
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.stageCount = static_cast<uint32_t>(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() {
+1
View File
@@ -116,6 +116,7 @@ namespace love {
void createSwapChain();
void createImageViews();
void createRenderPass();
void createDefaultShaders();
void createGraphicsPipeline();
void createFramebuffers();
void createCommandPool();