Проблема с рисовкой вулканом. Какой бы цвет не задавал в фрагментном шейдере - либо ничего не выводит, либо выводит только красный треугольник. Как будто не поддерживает другие цвета.
#version 450
#extension GL_ARB_separate_shader_objects: enable
layout(location = 0) out vec4 outColor;
void main()
{
outColor = vec4(0.0, 1.0, 1.0, 1.0);
}
Код на С++
#define VK_USE_PLATFORM_WIN32_KHR
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <iostream>
#include <cassert>
#include <vector>
#include <fstream>
#define assert_vulkan(expr) (assert(!expr))
VkInstance vkInstance;
VkDevice vkDevice;
VkPhysicalDevice gpu;
VkSurfaceKHR surface;
VkSwapchainKHR swapChain;
GLFWwindow* window;
VkShaderModule shaderModuleVert;
VkShaderModule shaderModuleFrag;
VkPipelineLayout pipelineLayout;
VkRenderPass vkRenderPass;
VkPipeline pipeline;
VkQueue queue;
VkCommandPool commandPool;
std::vector<VkCommandBuffer> commandBuffers;
VkSemaphore semaphoreImageAvailable;
VkSemaphore semaphoreRenderFinished;
uint32_t currentQueueFamily = 0;
VkFormat currentFormat = VkFormat::VK_FORMAT_B8G8R8A8_UNORM;
std::vector<VkImageView> imageViews;
std::vector<VkFramebuffer> frameBuffers;
const int width = 400;
const int height = 300;
void printStates(VkPhysicalDevice& device);
std::string readFile(const std::string fileName) {
std::ifstream inputFile(fileName, std::ios::binary);
assert(inputFile.is_open());
return std::string((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
}
void startGlfw() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, "Vulkan", 0, 0);
}
void createShaderModule(std::string code, VkShaderModule* shaderModule) {
VkShaderModuleCreateInfo shaderModuleCreateInfo{};
shaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shaderModuleCreateInfo.pNext = 0;
shaderModuleCreateInfo.flags = 0;
shaderModuleCreateInfo.codeSize = code.length();
shaderModuleCreateInfo.pCode = (uint32_t*)code.data();
auto res = vkCreateShaderModule(vkDevice, &shaderModuleCreateInfo, 0, shaderModule);
assert_vulkan(res);
}
void startVulkan() {
VkApplicationInfo appInfo;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = 0;
appInfo.pApplicationName = "Vulkan";
appInfo.applicationVersion = VK_MAKE_VERSION(0, 0, 0);
appInfo.pEngineName = "Start engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_VERSION_1_0;
uint32_t amountOfLayers = 0;
vkEnumerateInstanceLayerProperties(&amountOfLayers, 0);
std::vector<VkLayerProperties> layouts(amountOfLayers);
vkEnumerateInstanceLayerProperties(&amountOfLayers, layouts.data());
std::cout << "Layouts count: " << amountOfLayers << std::endl;
int i = 0;
for (auto& layout : layouts) {
std::cout << "Layout #" << i + 1 << std::endl;
std::cout << "Name: " << layout.layerName << std::endl;
std::cout << "Spec version: " << layout.specVersion << std::endl;
std::cout << "Impl version: " << layout.implementationVersion << std::endl;
std::cout << "Description: " << layout.description << std::endl;
i++;
}
uint32_t nExtensions = 0;
vkEnumerateInstanceExtensionProperties(0, &nExtensions, 0);
std::vector<VkExtensionProperties> allExtensions(nExtensions);
vkEnumerateInstanceExtensionProperties(0, &nExtensions, allExtensions.data());
std::cout << "Extensions count: " << nExtensions << std::endl;
i = 0;
for (auto& ext : allExtensions) {
std::cout << "Extension #" << i + 1 << std::endl;
std::cout << "Name: " << ext.extensionName << std::endl;
std::cout << "Spec version: " << ext.specVersion << std::endl;
i++;
}
std::vector<const char*> validationLayers = {
"VK_LAYER_LUNARG_standard_validation"
};
uint32_t amountGlfwExtensions = 0;
auto glfwExtensions = glfwGetRequiredInstanceExtensions(&amountGlfwExtensions);
VkInstanceCreateInfo instanceCreateInfo{};
instanceCreateInfo.pNext = 0;
instanceCreateInfo.pApplicationInfo = &appInfo;
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.flags = 0;
instanceCreateInfo.enabledLayerCount = validationLayers.size();
instanceCreateInfo.ppEnabledLayerNames = validationLayers.data();
instanceCreateInfo.ppEnabledExtensionNames = glfwExtensions;
instanceCreateInfo.enabledExtensionCount = amountGlfwExtensions;
auto res = vkCreateInstance(&instanceCreateInfo, 0, &vkInstance);
assert_vulkan(res);
res = glfwCreateWindowSurface(vkInstance, window, 0, &surface);
assert_vulkan(res);
uint32_t pDevicesCount = 0;
vkEnumeratePhysicalDevices(vkInstance, &pDevicesCount, 0);
std::vector<VkPhysicalDevice> physicalDevices(pDevicesCount);
res = vkEnumeratePhysicalDevices(vkInstance, &pDevicesCount, physicalDevices.data());
assert_vulkan(res);
for (auto& pDevice : physicalDevices) {
printStates(pDevice);
}
VkDeviceQueueCreateInfo deviceQueueCreateInfo{};
deviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfo.pNext = 0;
deviceQueueCreateInfo.flags = 0;
deviceQueueCreateInfo.queueFamilyIndex = currentQueueFamily;
deviceQueueCreateInfo.queueCount = 4;
deviceQueueCreateInfo.pQueuePriorities = new float[4]{ 1.0f,1.0f,1.0f,1.0f };
VkPhysicalDeviceFeatures usedFeatures = {};
std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkDeviceCreateInfo deviceCreateInfo{};
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.pNext = 0;
deviceCreateInfo.flags = 0;
deviceCreateInfo.queueCreateInfoCount = 1;
deviceCreateInfo.pQueueCreateInfos = &deviceQueueCreateInfo;
deviceCreateInfo.enabledLayerCount = 0;
deviceCreateInfo.ppEnabledLayerNames = 0;
deviceCreateInfo.enabledExtensionCount = deviceExtensions.size();
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensions.data();
deviceCreateInfo.pEnabledFeatures = &usedFeatures;
res = vkCreateDevice(physicalDevices[0], &deviceCreateInfo, 0, &vkDevice);
assert_vulkan(res);
vkGetDeviceQueue(vkDevice, 0, 0, &queue);
VkBool32 isSupport = false;
res = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevices[0], 0, surface, &isSupport);
assert_vulkan(res);
if (!isSupport) {
std::cerr << "Surface not supported!\n";
__debugbreak();
}
VkSwapchainCreateInfoKHR swapchainCreateInfo{};
swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainCreateInfo.pNext = 0;
swapchainCreateInfo.flags = 0;
swapchainCreateInfo.surface = surface;
swapchainCreateInfo.minImageCount = 3;
swapchainCreateInfo.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
swapchainCreateInfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
swapchainCreateInfo.imageExtent = VkExtent2D{ width, height };
swapchainCreateInfo.imageArrayLayers = 1;
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainCreateInfo.queueFamilyIndexCount = 1;
swapchainCreateInfo.pQueueFamilyIndices = ¤tQueueFamily;
swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchainCreateInfo.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
swapchainCreateInfo.clipped = VK_TRUE;
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE;
res = vkCreateSwapchainKHR(vkDevice, &swapchainCreateInfo, 0, &swapChain);
assert_vulkan(res);
uint32_t nImagesInSwapChain = 0;
vkGetSwapchainImagesKHR(vkDevice, swapChain, &nImagesInSwapChain, 0);
std::vector<VkImage> vkImages(nImagesInSwapChain);
res = vkGetSwapchainImagesKHR(vkDevice, swapChain, &nImagesInSwapChain, vkImages.data());
assert_vulkan(res);
imageViews.resize(nImagesInSwapChain);
for (i = 0; i < nImagesInSwapChain; i++) {
VkImageViewCreateInfo imageViewCreateInfo{};
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.pNext = 0;
imageViewCreateInfo.flags = 0;
imageViewCreateInfo.image = vkImages[i];
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
imageViewCreateInfo.components = { VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY ,VK_COMPONENT_SWIZZLE_IDENTITY ,VK_COMPONENT_SWIZZLE_IDENTITY };
imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = 1;
res = vkCreateImageView(vkDevice, &imageViewCreateInfo, 0, &imageViews[i]);
assert_vulkan(res);
}
auto shaderCodeVert = readFile("vert.spv");
auto shaderCodeFrag = readFile("frag.spv");
std::cout << shaderCodeFrag.length() << std::endl;
std::cout << shaderCodeVert.length() << std::endl;
createShaderModule(shaderCodeFrag, &shaderModuleFrag);
createShaderModule(shaderCodeVert, &shaderModuleVert);
VkPipelineShaderStageCreateInfo piplineShaderCreateInfoVert{};
piplineShaderCreateInfoVert.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
piplineShaderCreateInfoVert.pNext = 0;
piplineShaderCreateInfoVert.flags = 0;
piplineShaderCreateInfoVert.stage = VK_SHADER_STAGE_VERTEX_BIT;
piplineShaderCreateInfoVert.module = shaderModuleVert;
piplineShaderCreateInfoVert.pName = "main";
piplineShaderCreateInfoVert.pSpecializationInfo = 0;
VkPipelineShaderStageCreateInfo piplineShaderCreateInfoFrag{};
piplineShaderCreateInfoFrag.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
piplineShaderCreateInfoFrag.pNext = 0;
piplineShaderCreateInfoFrag.flags = 0;
piplineShaderCreateInfoFrag.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
piplineShaderCreateInfoFrag.module = shaderModuleFrag;
piplineShaderCreateInfoFrag.pName = "main";
piplineShaderCreateInfoFrag.pSpecializationInfo = 0;
VkPipelineShaderStageCreateInfo shaderStages[]{ piplineShaderCreateInfoVert , piplineShaderCreateInfoFrag };
VkPipelineVertexInputStateCreateInfo inputVertexCreateInfo{};
inputVertexCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
inputVertexCreateInfo.pNext = 0;
inputVertexCreateInfo.flags = 0;
inputVertexCreateInfo.vertexBindingDescriptionCount = 0;
inputVertexCreateInfo.pVertexBindingDescriptions = 0;
inputVertexCreateInfo.vertexAttributeDescriptionCount = 0;
inputVertexCreateInfo.pVertexAttributeDescriptions = 0;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo{};
inputAssemblyCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssemblyCreateInfo.pNext = 0;
inputAssemblyCreateInfo.flags = 0;
inputAssemblyCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssemblyCreateInfo.primitiveRestartEnable = VK_FALSE;
VkViewport vkViewPort;
vkViewPort.x = 0;
vkViewPort.y = 0;
vkViewPort.width = width;
vkViewPort.height = height;
vkViewPort.minDepth = 0;
vkViewPort.maxDepth = 1;
VkRect2D scissor;
scissor.offset = { 0,0 };
scissor.extent = { width, height };
VkPipelineViewportStateCreateInfo viewportStateCreateInfo{};
viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportStateCreateInfo.pNext = 0;
viewportStateCreateInfo.flags = 0;
viewportStateCreateInfo.viewportCount = 1;
viewportStateCreateInfo.pViewports = &vkViewPort;
viewportStateCreateInfo.scissorCount = 1;
viewportStateCreateInfo.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo{};
pipelineRasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
pipelineRasterizationStateCreateInfo.pNext = 0;
pipelineRasterizationStateCreateInfo.flags = 0;
pipelineRasterizationStateCreateInfo.depthClampEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.rasterizerDiscardEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.polygonMode = VK_POLYGON_MODE_FILL;
pipelineRasterizationStateCreateInfo.cullMode = VK_CULL_MODE_BACK_BIT;
pipelineRasterizationStateCreateInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
pipelineRasterizationStateCreateInfo.depthBiasEnable = VK_FALSE;
pipelineRasterizationStateCreateInfo.depthBiasConstantFactor = 0;
pipelineRasterizationStateCreateInfo.depthBiasClamp = 0;
pipelineRasterizationStateCreateInfo.depthBiasSlopeFactor = 0;
pipelineRasterizationStateCreateInfo.lineWidth = 1;
VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo{};
pipelineMultisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
pipelineMultisampleStateCreateInfo.pNext = 0;
pipelineMultisampleStateCreateInfo.flags = 0;
pipelineMultisampleStateCreateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
pipelineMultisampleStateCreateInfo.sampleShadingEnable = VK_FALSE;
pipelineMultisampleStateCreateInfo.minSampleShading = 1.0f;
pipelineMultisampleStateCreateInfo.pSampleMask = 0;
pipelineMultisampleStateCreateInfo.alphaToCoverageEnable = VK_FALSE;
pipelineMultisampleStateCreateInfo.alphaToOneEnable = VK_FALSE;
VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState{};
pipelineColorBlendAttachmentState.blendEnable = VK_TRUE;
pipelineColorBlendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
pipelineColorBlendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
pipelineColorBlendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
pipelineColorBlendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
pipelineColorBlendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
pipelineColorBlendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
pipelineColorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT;
VkPipelineColorBlendStateCreateInfo colorBlendCreateInfo{};
colorBlendCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendCreateInfo.pNext = 0;
colorBlendCreateInfo.flags = 0;
colorBlendCreateInfo.logicOpEnable = VK_FALSE;
colorBlendCreateInfo.logicOp = VK_LOGIC_OP_NO_OP;
colorBlendCreateInfo.attachmentCount = 1;
colorBlendCreateInfo.pAttachments = &pipelineColorBlendAttachmentState;
colorBlendCreateInfo.blendConstants[0] = 0;
colorBlendCreateInfo.blendConstants[1] = 0;
colorBlendCreateInfo.blendConstants[2] = 0;
colorBlendCreateInfo.blendConstants[3] = 0;
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo{};
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.pNext = 0;
pipelineLayoutCreateInfo.flags = 0;
pipelineLayoutCreateInfo.setLayoutCount = 0;
pipelineLayoutCreateInfo.pSetLayouts = 0;
pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pipelineLayoutCreateInfo.pPushConstantRanges = 0;
res = vkCreatePipelineLayout(vkDevice, &pipelineLayoutCreateInfo, 0, &pipelineLayout);
assert_vulkan(res);
VkAttachmentDescription attachmentDescription{};
attachmentDescription.flags = 0;
attachmentDescription.format = currentFormat;
attachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
attachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentDescription.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachmentDescription.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference attachmenetReference{};
attachmenetReference.attachment = 0;
attachmenetReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpassDescription{};
subpassDescription.flags = 0;
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpassDescription.inputAttachmentCount = 0;
subpassDescription.pInputAttachments = 0;
subpassDescription.colorAttachmentCount = 1;
subpassDescription.pColorAttachments = &attachmenetReference;
subpassDescription.pResolveAttachments = 0;
subpassDescription.pDepthStencilAttachment = 0;
subpassDescription.preserveAttachmentCount = 0;
subpassDescription.pPreserveAttachments = 0;
VkSubpassDependency subpassDependency{};
subpassDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency.dstSubpass = 0;
subpassDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.srcAccessMask = 0;
subpassDependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
subpassDependency.dependencyFlags = 0;
VkRenderPassCreateInfo renderPassCreateInfo{};
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassCreateInfo.pNext = 0;
renderPassCreateInfo.flags = 0;
renderPassCreateInfo.attachmentCount = 1;
renderPassCreateInfo.pAttachments = &attachmentDescription;
renderPassCreateInfo.subpassCount = 1;
renderPassCreateInfo.pSubpasses = &subpassDescription;
renderPassCreateInfo.dependencyCount = 1;
renderPassCreateInfo.pDependencies = &subpassDependency;
res = vkCreateRenderPass(vkDevice, &renderPassCreateInfo, 0, &vkRenderPass);
assert_vulkan(res);
VkGraphicsPipelineCreateInfo pipelineCreateInfo{};
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineCreateInfo.pNext = 0;
pipelineCreateInfo.flags = 0;
pipelineCreateInfo.stageCount = 2;
pipelineCreateInfo.pStages = shaderStages;
pipelineCreateInfo.pVertexInputState = &inputVertexCreateInfo;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyCreateInfo;
pipelineCreateInfo.pTessellationState = 0;
pipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
pipelineCreateInfo.pRasterizationState = &pipelineRasterizationStateCreateInfo;
pipelineCreateInfo.pMultisampleState = &pipelineMultisampleStateCreateInfo;
pipelineCreateInfo.pDepthStencilState = 0;
pipelineCreateInfo.pColorBlendState = &colorBlendCreateInfo;
pipelineCreateInfo.pDynamicState = 0;
pipelineCreateInfo.layout = pipelineLayout;
pipelineCreateInfo.renderPass = vkRenderPass;
pipelineCreateInfo.subpass = 0;
pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
pipelineCreateInfo.basePipelineIndex = -1;
res = vkCreateGraphicsPipelines(vkDevice, 0, 1, &pipelineCreateInfo, 0, &pipeline);
assert_vulkan(res);
frameBuffers.resize(nImagesInSwapChain);
for (i = 0; i < nImagesInSwapChain; i++) {
VkFramebufferCreateInfo frameBufferCreateInfo{};
frameBufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
frameBufferCreateInfo.pNext = 0;
frameBufferCreateInfo.flags = 0;
frameBufferCreateInfo.renderPass = vkRenderPass;
frameBufferCreateInfo.attachmentCount = 1;
frameBufferCreateInfo.pAttachments = &imageViews[i];
frameBufferCreateInfo.width = width;
frameBufferCreateInfo.height = height;
frameBufferCreateInfo.layers = 1;
res = vkCreateFramebuffer(vkDevice, &frameBufferCreateInfo, 0, &frameBuffers[i]);
assert_vulkan(res);
}
VkCommandPoolCreateInfo commandPoolCreateInfo{};
commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
commandPoolCreateInfo.pNext = 0;
commandPoolCreateInfo.flags = 0;
commandPoolCreateInfo.queueFamilyIndex = currentQueueFamily;
res = vkCreateCommandPool(vkDevice, &commandPoolCreateInfo, 0, &commandPool);
assert_vulkan(res);
VkCommandBufferAllocateInfo commandBufferAllocateInfo{};
commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
commandBufferAllocateInfo.pNext = 0;
commandBufferAllocateInfo.commandPool = commandPool;
commandBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
commandBufferAllocateInfo.commandBufferCount = nImagesInSwapChain;
commandBuffers.resize(nImagesInSwapChain);
res = vkAllocateCommandBuffers(vkDevice, &commandBufferAllocateInfo, commandBuffers.data());
assert_vulkan(res);
VkCommandBufferBeginInfo commandBufferBeginInfo{};
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.pNext = 0;
commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
commandBufferBeginInfo.pInheritanceInfo = 0;
for (i = 0; i < nImagesInSwapChain; i++) {
res = vkBeginCommandBuffer(commandBuffers[i], &commandBufferBeginInfo);
assert_vulkan(res);
VkRenderPassBeginInfo renderPassBeginInfo{};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.pNext = 0;
renderPassBeginInfo.renderPass = vkRenderPass;
renderPassBeginInfo.framebuffer = frameBuffers[i];
renderPassBeginInfo.renderArea.offset = { 0,0 };
renderPassBeginInfo.renderArea.extent = { width, height };
VkClearValue clearValue = { 0,0,0,1 };
renderPassBeginInfo.clearValueCount = 1;
renderPassBeginInfo.pClearValues = &clearValue;
vkCmdBeginRenderPass(commandBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
vkCmdEndRenderPass(commandBuffers[i]);
res = vkEndCommandBuffer(commandBuffers[i]);
assert_vulkan(res);
}
VkSemaphoreCreateInfo semaphoreCreateInfo{};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
semaphoreCreateInfo.pNext = 0;
semaphoreCreateInfo.flags = 0;
res = vkCreateSemaphore(vkDevice, &semaphoreCreateInfo, 0, &semaphoreImageAvailable);
assert_vulkan(res);
res = vkCreateSemaphore(vkDevice, &semaphoreCreateInfo, 0, &semaphoreRenderFinished);
assert_vulkan(res);
}
void drawFrame() {
uint32_t imageIndex;
auto res = vkAcquireNextImageKHR(vkDevice, swapChain, UINT64_MAX, semaphoreImageAvailable, 0, &imageIndex);
assert_vulkan(res);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pNext = 0;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = &semaphoreImageAvailable;
VkPipelineStageFlags waitStageMask[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.pWaitDstStageMask = waitStageMask;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &semaphoreRenderFinished;
res = vkQueueSubmit(queue, 1, &submitInfo, 0);
assert_vulkan(res);
VkPresentInfoKHR presentInfo;
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = 0;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &semaphoreRenderFinished;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapChain;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = 0;
res = vkQueuePresentKHR(queue, &presentInfo);
assert_vulkan(res);
}
void gameLoop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
drawFrame();
}
}
Мне представляется, что проблема заключается в маске записи в экранный буфер, разрешающей вывод только красного канала:
pipelineColorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT;
вместо
pipelineColorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
bitor VK_COLOR_COMPONENT_G_BIT
bitor VK_COLOR_COMPONENT_B_BIT;
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
У меня возник вопрос по семантике перемещения в рамках POD типовОн сложный, поэтому я постараюсь разбить его на подпункты:
Подскажите, пожалуйста, почему вышеуказанный код показывает, что
Почему-то программа крашится на scanf'е, выдаёт ошибку malloc_consolidate() invalid chunk sizeНо если я поставлю функцию game_logic () в main после остальных, то всё будет...