Compare commits
No commits in common. "master" and "v0.1" have entirely different histories.
|
@ -21,5 +21,3 @@
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
# SPIR-V shaders
|
|
||||||
*.spv
|
|
|
@ -5,8 +5,7 @@
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"${workspaceFolder}/include",
|
"${workspaceFolder}/include",
|
||||||
"C:/VulkanSDK/1.2.189.2/Include",
|
"C:/VulkanSDK/1.2.189.2/Include",
|
||||||
"${workspaceFolder}/../dependencies/GLFW/include",
|
"${workspaceFolder}/../dependencies/GLFW/include"
|
||||||
"${workspaceFolder}/../dependencies/glm"
|
|
||||||
],
|
],
|
||||||
"compilerPath": "C:/MinGW/bin/g++.exe",
|
"compilerPath": "C:/MinGW/bin/g++.exe",
|
||||||
"cStandard": "c11",
|
"cStandard": "c11",
|
||||||
|
|
|
@ -41,10 +41,6 @@
|
||||||
"stdexcept": "cpp",
|
"stdexcept": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"typeinfo": "cpp",
|
"typeinfo": "cpp",
|
||||||
"cstring": "cpp",
|
"cstring": "cpp"
|
||||||
"list": "cpp",
|
|
||||||
"unordered_set": "cpp",
|
|
||||||
"map": "cpp",
|
|
||||||
"set": "cpp"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,7 +16,6 @@
|
||||||
"-LC:/VulkanSDK/1.2.189.2/Lib32",
|
"-LC:/VulkanSDK/1.2.189.2/Lib32",
|
||||||
|
|
||||||
"-I${workspaceRoot}/../dependencies/GLFW/include",
|
"-I${workspaceRoot}/../dependencies/GLFW/include",
|
||||||
"-I${workspaceRoot}/../dependencies/glm",
|
|
||||||
"-L${workspaceRoot}/../dependencies/GLFW/lib-mingw",
|
"-L${workspaceRoot}/../dependencies/GLFW/lib-mingw",
|
||||||
"-static",
|
"-static",
|
||||||
"-lvulkan-1",
|
"-lvulkan-1",
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
#ifndef VERTEX_H
|
|
||||||
#define VERTEX_H
|
|
||||||
|
|
||||||
#include <GLM/vec3.hpp>
|
|
||||||
|
|
||||||
typedef struct _Vertex
|
|
||||||
{
|
|
||||||
glm::vec3 position, color;
|
|
||||||
} Vertex;
|
|
||||||
|
|
||||||
#endif // VERTEX_H
|
|
|
@ -23,8 +23,6 @@ class Vulkan
|
||||||
std::vector<VkImage> swapChainImages; // Изображения из списка показа
|
std::vector<VkImage> swapChainImages; // Изображения из списка показа
|
||||||
std::vector<VkImageView> swapChainImageViews; // Информация об изображениях из списка показа
|
std::vector<VkImageView> swapChainImageViews; // Информация об изображениях из списка показа
|
||||||
VkRenderPass renderPass; // Проходы рендера
|
VkRenderPass renderPass; // Проходы рендера
|
||||||
VkPipelineLayout pipelineLayout; // Раскладка конвейера
|
|
||||||
VkPipeline graphicsPipeline; // Графический конвейер
|
|
||||||
|
|
||||||
// Структура для хранения флагов
|
// Структура для хранения флагов
|
||||||
struct
|
struct
|
||||||
|
@ -40,8 +38,6 @@ class Vulkan
|
||||||
void createWindowSurface(GLFWwindow* window); // Создание поверхности окна
|
void createWindowSurface(GLFWwindow* window); // Создание поверхности окна
|
||||||
void createSwapchain(GLFWwindow* window); // Создание цепочки показа
|
void createSwapchain(GLFWwindow* window); // Создание цепочки показа
|
||||||
void createRenderpass(); // Создание проходов рендера
|
void createRenderpass(); // Создание проходов рендера
|
||||||
VkShaderModule createShaderModule(const char * filename); // Создание шейдерного модуля
|
|
||||||
void createGraphicPipeline(); // Создание графического конвеера
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VK_H
|
#endif // VK_H
|
|
@ -1,9 +0,0 @@
|
||||||
#version 450
|
|
||||||
|
|
||||||
layout(location = 0) in vec3 fragColor;
|
|
||||||
|
|
||||||
layout(location = 0) out vec4 outColor;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
outColor = vec4(fragColor, 1.0);
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
#version 450
|
|
||||||
|
|
||||||
layout(location = 0) in vec2 inPosition;
|
|
||||||
layout(location = 1) in vec3 inColor;
|
|
||||||
|
|
||||||
layout(location = 0) out vec3 fragColor;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
gl_Position = vec4(inPosition, 0.0, 1.0);
|
|
||||||
fragColor = inColor;
|
|
||||||
}
|
|
203
src/vk.cpp
203
src/vk.cpp
|
@ -1,5 +1,4 @@
|
||||||
#include "vk.h"
|
#include "vk.h"
|
||||||
#include "Vertex.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -18,14 +17,11 @@ void Vulkan::init(GLFWwindow* window)
|
||||||
createLogicalDevice(deviceExtensions); // Создание физического устройства
|
createLogicalDevice(deviceExtensions); // Создание физического устройства
|
||||||
createSwapchain(window); // Создание списка показа
|
createSwapchain(window); // Создание списка показа
|
||||||
createRenderpass(); // Создание проходов рендера
|
createRenderpass(); // Создание проходов рендера
|
||||||
createGraphicPipeline(); // Создание графического конвейера
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// завершение работы
|
// завершение работы
|
||||||
void Vulkan::destroy()
|
void Vulkan::destroy()
|
||||||
{
|
{
|
||||||
vkDestroyPipeline(logicalDevice, graphicsPipeline, nullptr);
|
|
||||||
vkDestroyPipelineLayout(logicalDevice, pipelineLayout, nullptr);
|
|
||||||
vkDestroyRenderPass(logicalDevice, renderPass, nullptr);
|
vkDestroyRenderPass(logicalDevice, renderPass, nullptr);
|
||||||
|
|
||||||
// Уничтожение информации о изображениях списка показа
|
// Уничтожение информации о изображениях списка показа
|
||||||
|
@ -517,203 +513,4 @@ void Vulkan::createRenderpass()
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Unable to create render pass");
|
throw std::runtime_error("Unable to create render pass");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
// Считывание бинарного файла, содержащего шейдер
|
|
||||||
void readFile(const char * filename, std::vector<char>& buffer)
|
|
||||||
{
|
|
||||||
// откроем файл как бинарный и установим курсор в конец файла
|
|
||||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
|
||||||
// если файл не открыт - генерируем исключение
|
|
||||||
if (!file.is_open())
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Can't open file");
|
|
||||||
}
|
|
||||||
// определим размер файла
|
|
||||||
size_t fileSize = (size_t) file.tellg();
|
|
||||||
// создадим буфер
|
|
||||||
buffer.resize(fileSize);
|
|
||||||
|
|
||||||
// перенесем курсор в начало файла
|
|
||||||
file.seekg(0);
|
|
||||||
// считаем данные в буфер
|
|
||||||
file.read(buffer.data(), fileSize);
|
|
||||||
// закроем файл
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создание шейдерного модуля
|
|
||||||
VkShaderModule Vulkan::createShaderModule(const char * filename)
|
|
||||||
{
|
|
||||||
// буфер для чтения из файла
|
|
||||||
std::vector<char> buffer;
|
|
||||||
// считаем шейдер из файла
|
|
||||||
readFile(filename, buffer);
|
|
||||||
|
|
||||||
// Информация о создаваемом шейдерном модуле
|
|
||||||
VkShaderModuleCreateInfo createInfo{};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
||||||
createInfo.codeSize = buffer.size();
|
|
||||||
createInfo.pCode = reinterpret_cast<const uint32_t*>(buffer.data());
|
|
||||||
|
|
||||||
// Создание шейдерного модуля
|
|
||||||
VkShaderModule shaderModule;
|
|
||||||
if (vkCreateShaderModule(logicalDevice, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
|
||||||
throw std::runtime_error("Unable to create shader module");
|
|
||||||
}
|
|
||||||
|
|
||||||
return shaderModule;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создание графического конвеера
|
|
||||||
void Vulkan::createGraphicPipeline()
|
|
||||||
{
|
|
||||||
// Входные данные вершин
|
|
||||||
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
|
|
||||||
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
|
||||||
|
|
||||||
// Привязка
|
|
||||||
VkVertexInputBindingDescription bindingDescription{};
|
|
||||||
bindingDescription.binding = 0;
|
|
||||||
bindingDescription.stride = sizeof(Vertex);
|
|
||||||
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
|
||||||
|
|
||||||
// Описание атрибута
|
|
||||||
VkVertexInputAttributeDescription attributeDescriptions[2];
|
|
||||||
|
|
||||||
attributeDescriptions[0].binding = 0;
|
|
||||||
attributeDescriptions[0].location = 0;
|
|
||||||
attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
|
|
||||||
attributeDescriptions[0].offset = offsetof(Vertex, position);
|
|
||||||
|
|
||||||
attributeDescriptions[1].binding = 0;
|
|
||||||
attributeDescriptions[1].location = 1;
|
|
||||||
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
|
||||||
attributeDescriptions[1].offset = offsetof(Vertex, color);
|
|
||||||
|
|
||||||
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
|
||||||
vertexInputInfo.vertexAttributeDescriptionCount = 2;
|
|
||||||
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
|
|
||||||
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions;
|
|
||||||
|
|
||||||
// Входной сборщик
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
|
|
||||||
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
|
||||||
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
||||||
inputAssembly.primitiveRestartEnable = VK_FALSE;
|
|
||||||
|
|
||||||
// Область просмотра
|
|
||||||
VkViewport viewport{};
|
|
||||||
viewport.x = 0.0f;
|
|
||||||
viewport.y = 0.0f;
|
|
||||||
viewport.width = surface.selectedExtent.width;
|
|
||||||
viewport.height = surface.selectedExtent.height;
|
|
||||||
viewport.minDepth = 0.0f;
|
|
||||||
viewport.maxDepth = 1.0f;
|
|
||||||
|
|
||||||
// Прямоугольник отсечения
|
|
||||||
VkRect2D scissor{};
|
|
||||||
scissor.offset = {0, 0};
|
|
||||||
scissor.extent = surface.selectedExtent;
|
|
||||||
|
|
||||||
// Состояние области просмотра
|
|
||||||
VkPipelineViewportStateCreateInfo viewportState{};
|
|
||||||
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
|
||||||
viewportState.viewportCount = 1;
|
|
||||||
viewportState.pViewports = &viewport;
|
|
||||||
viewportState.scissorCount = 1;
|
|
||||||
viewportState.pScissors = &scissor;
|
|
||||||
|
|
||||||
// Растеризатор
|
|
||||||
VkPipelineRasterizationStateCreateInfo rasterizer{};
|
|
||||||
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
|
||||||
rasterizer.depthClampEnable = VK_FALSE;
|
|
||||||
rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
|
||||||
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
|
||||||
rasterizer.lineWidth = 1.0f;
|
|
||||||
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
|
||||||
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
|
||||||
rasterizer.depthBiasEnable = VK_FALSE;
|
|
||||||
|
|
||||||
// Мультисэмплинг
|
|
||||||
VkPipelineMultisampleStateCreateInfo multisampling{};
|
|
||||||
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
|
||||||
multisampling.sampleShadingEnable = VK_FALSE;
|
|
||||||
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
|
|
||||||
// Смешивание цветов для буфера
|
|
||||||
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
|
|
||||||
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
|
||||||
colorBlendAttachment.blendEnable = VK_FALSE;
|
|
||||||
|
|
||||||
// Глобальные настройки смешивания цветов
|
|
||||||
VkPipelineColorBlendStateCreateInfo colorBlending{};
|
|
||||||
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
|
||||||
colorBlending.logicOpEnable = VK_FALSE;
|
|
||||||
colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
|
||||||
colorBlending.attachmentCount = 1;
|
|
||||||
colorBlending.pAttachments = &colorBlendAttachment;
|
|
||||||
colorBlending.blendConstants[0] = 0.0f;
|
|
||||||
colorBlending.blendConstants[1] = 0.0f;
|
|
||||||
colorBlending.blendConstants[2] = 0.0f;
|
|
||||||
colorBlending.blendConstants[3] = 0.0f;
|
|
||||||
|
|
||||||
// раскладка конвейера
|
|
||||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
|
||||||
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
|
||||||
pipelineLayoutInfo.setLayoutCount = 0;
|
|
||||||
pipelineLayoutInfo.pushConstantRangeCount = 0;
|
|
||||||
|
|
||||||
if (vkCreatePipelineLayout(logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Unable to create pipeline layout");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создание шейдеров
|
|
||||||
VkShaderModule vertShaderModule = createShaderModule("shaders/vert.spv");
|
|
||||||
VkShaderModule fragShaderModule = createShaderModule("shaders/frag.spv");
|
|
||||||
|
|
||||||
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};
|
|
||||||
|
|
||||||
|
|
||||||
// Информация о создаваемом конвейере
|
|
||||||
VkGraphicsPipelineCreateInfo pipelineInfo{};
|
|
||||||
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
|
||||||
pipelineInfo.stageCount = 2;
|
|
||||||
pipelineInfo.pStages = shaderStages;
|
|
||||||
pipelineInfo.pVertexInputState = &vertexInputInfo;
|
|
||||||
pipelineInfo.pInputAssemblyState = &inputAssembly;
|
|
||||||
pipelineInfo.pViewportState = &viewportState;
|
|
||||||
pipelineInfo.pRasterizationState = &rasterizer;
|
|
||||||
pipelineInfo.pMultisampleState = &multisampling;
|
|
||||||
pipelineInfo.pColorBlendState = &colorBlending;
|
|
||||||
pipelineInfo.layout = pipelineLayout;
|
|
||||||
pipelineInfo.renderPass = renderPass;
|
|
||||||
pipelineInfo.subpass = 0;
|
|
||||||
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
|
||||||
|
|
||||||
// Создание графического конвейера
|
|
||||||
if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Unable to create graphics pipeline");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Удаление шейдерных модулей
|
|
||||||
vkDestroyShaderModule(logicalDevice, fragShaderModule, nullptr);
|
|
||||||
vkDestroyShaderModule(logicalDevice, vertShaderModule, nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue