Partage
  • Partager sur Facebook
  • Partager sur Twitter

[VULKAN] Validation layer erreur.

    23 novembre 2023 à 18:54:14

    Salut, je voudrais afficher des sommets avec ou sans texture, j'ai donc créer une variable dans le shader havetexture et j'ai modifié les descriptors.

    Malheureusement ça crash :

     RenderTarget::RenderTarget(window::VkSettup& vkSettup) : vkSettup(vkSettup), vertexBuffer(vkSettup), defaultShader(vkSettup),
            m_defaultView(), m_view() {
    
            }
            RenderTarget::~RenderTarget() {
                cleanup();
            }
            void RenderTarget::initialize() {
    
    
                m_defaultView = View (static_cast<float>(getSize().x), static_cast<float>(getSize().y), -static_cast<float>(getSize().y) - 200, static_cast<float>(getSize().y)+200);
                m_defaultView.reset(physic::BoundingBox(0, 0, -static_cast<float>(getSize().y) - 200,static_cast<float>(getSize().x), static_cast<float>(getSize().y),static_cast<float>(getSize().y)+200));
                m_view = m_defaultView;
                const std::string defaultVertexShader = R"(#version 450
                                                            layout(binding = 0) uniform UniformBufferObject {
                                                                mat4 model;
                                                                mat4 view;
                                                                mat4 proj;
                                                            } ubo;
                                                            layout(location = 0) in vec3 inPosition;
                                                            layout(location = 1) in vec4 inColor;
                                                            layout(location = 2) in vec2 inTexCoord;
    
                                                            layout(location = 0) out vec4 fragColor;
                                                            layout(location = 1) out vec2 fragTexCoord;
    
                                                            out gl_PerVertex {
                                                                vec4 gl_Position;
                                                            };
    
                                                            void main() {
                                                                gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
                                                                fragColor = inColor;
                                                                fragTexCoord = inTexCoord;
                                                            }
                                                            )";
                const std::string defaultFragmentShader = R"(#version 450
                                                              #extension GL_ARB_separate_shader_objects : enable
                                                              layout(binding = 2) uniform UniformBufferObject {
                                                                uint haveTexture;
                                                              } ubo;
                                                              layout(location = 0) in vec4 fragColor;
                                                              layout(location = 1) in vec2 fragTexCoord;
                                                              layout(location = 0) out vec4 outColor;
                                                              layout(binding = 1) uniform sampler2D texSampler;
                                                              void main() {
                                                                 if (ubo.haveTexture == 0) {
                                                                    outColor = fragColor;
                                                                 } else {
                                                                    outColor = texture(texSampler, fragTexCoord) * fragColor;
                                                                 }
                                                              })";
                 if (!defaultShader.loadFromMemory(defaultVertexShader, defaultFragmentShader)) {
                      throw core::Erreur (0, "Failed to load default shader", 1);
                 }
                 createRenderPass();
                 createCommandPool();
                 createUniformBuffers();
            }
            void RenderTarget::clear(const sf::Color& color) {
                cleanup();
                clearColor = color;
            }
            void RenderTarget::clearDepth() {
            }
            void RenderTarget::setView(View view)
            {
                m_view = view;
            }
            View& RenderTarget::getView() {
                return m_view;
            }
            View& RenderTarget::getDefaultView() {
                return m_defaultView;
            }
             math::Vec3f RenderTarget::mapPixelToCoords(const math::Vec3f& point)
            {
                return mapPixelToCoords(point, getView());
            }
    
    
            math::Vec3f RenderTarget::mapPixelToCoords(const math::Vec3f& point, View& view)
            {
                ViewportMatrix vpm;
                vpm.setViewport(math::Vec3f(view.getViewport().getPosition().x, view.getViewport().getPosition().y, 0)
                                            ,math::Vec3f(view.getViewport().getWidth(), view.getViewport().getHeight(), 1));
                math::Vec3f coords = vpm.toNormalizedCoordinates(point);
                coords = view.getProjMatrix().unProject(coords);
                coords = coords.normalizeToVec3();
                coords = view.getViewMatrix().inverseTransform(coords);
                return coords;
            }
    
            math::Vec3f RenderTarget::mapCoordsToPixel(const math::Vec3f& point)
            {
                return mapCoordsToPixel(point, getView());
            }
    
    
            math::Vec3f RenderTarget::mapCoordsToPixel(const math::Vec3f& point, View& view) {
                ViewportMatrix vpm;
                vpm.setViewport(math::Vec3f(view.getViewport().getPosition().x, view.getViewport().getPosition().y, 0),
                math::Vec3f(view.getViewport().getWidth(), view.getViewport().getHeight(), 1));
                math::Vec3f coords = view.getViewMatrix().transform(point);
                coords = view.getProjMatrix().project(coords);
                coords = coords.normalizeToVec3();
                coords = vpm.toViewportCoordinates(coords);
                return coords;
            }
            void RenderTarget::draw(Drawable& drawable, RenderStates states)
            {
                drawable.draw(*this, states);
            }
            void RenderTarget::draw(const Vertex* vertices, unsigned int vertexCount, sf::PrimitiveType type,
                          RenderStates states) {
    
                 createGraphicPipeline(vertices, vertexCount, type, states);
                 vertexBuffer.clear();
                 for (unsigned int i = 0; i < vertexCount; i++) {
                    vertexBuffer.append(vertices[i]);
                 }
                 UniformBufferObject ubo;
                 ubo.model = states.transform.getMatrix().transpose();
                 UniformBufferObject2 ubo2;
                 if (states.texture == nullptr)
                    ubo2.haveTexture = 0;
                 else
                    ubo2.haveTexture = 1;
                 updateUniformBuffer(getCurrentFrame(), ubo);
                 updateUniformBuffer(getCurrentFrame(), ubo2);
                 createDescriptorSetLayout(states.texture);
                 createDescriptorPool(states.texture);
                 createDescriptorSets(states.texture);
                 createCommandBuffers();
                 vertexBuffer.clearIndexes();
    
            }
            VertexBuffer& RenderTarget::getVertexBuffer () {
                return vertexBuffer;
            }
            void RenderTarget::createRenderPass() {
                VkAttachmentDescription colorAttachment{};
                colorAttachment.format =    getSwapchainImageFormat();
                colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
                colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
                colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
                colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
                colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
                colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
                colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    
                VkAttachmentReference colorAttachmentRef{};
                colorAttachmentRef.attachment = 0;
                colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    
                VkSubpassDescription subpass{};
                subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
                subpass.colorAttachmentCount = 1;
                subpass.pColorAttachments = &colorAttachmentRef;
    
                VkRenderPassCreateInfo renderPassInfo{};
                renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
                renderPassInfo.attachmentCount = 1;
                renderPassInfo.pAttachments = &colorAttachment;
                renderPassInfo.subpassCount = 1;
                renderPassInfo.pSubpasses = &subpass;
                VkSubpassDependency dependency{};
                dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
                dependency.dstSubpass = 0;
                dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
                dependency.srcAccessMask = 0;
                dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
                dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
                renderPassInfo.dependencyCount = 1;
                renderPassInfo.pDependencies = &dependency;
                if (vkCreateRenderPass(vkSettup.getDevice(), &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
                    throw core::Erreur(0, "failed to create render pass!", 1);
                }
    
            }
            void RenderTarget::createDescriptorSetLayout(const Texture* texture) {
                if (texture != nullptr) {
                    VkDescriptorSetLayoutBinding uboLayoutBinding{};
                    uboLayoutBinding.binding = 0;
                    uboLayoutBinding.descriptorCount = 1;
                    uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    uboLayoutBinding.pImmutableSamplers = nullptr;
                    uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
    
                    VkDescriptorSetLayoutBinding samplerLayoutBinding{};
                    samplerLayoutBinding.binding = 1;
                    samplerLayoutBinding.descriptorCount = 1;
                    samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
                    samplerLayoutBinding.pImmutableSamplers = nullptr;
                    samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    
                    VkDescriptorSetLayoutBinding ubo2LayoutBinding{};
                    ubo2LayoutBinding.binding = 2;
                    ubo2LayoutBinding.descriptorCount = 1;
                    ubo2LayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    ubo2LayoutBinding.pImmutableSamplers = nullptr;
                    ubo2LayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    
                    std::array<VkDescriptorSetLayoutBinding, 3> bindings = {uboLayoutBinding, samplerLayoutBinding, ubo2LayoutBinding};
    
                    VkDescriptorSetLayoutCreateInfo layoutInfo{};
                    layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
                    layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());;
                    layoutInfo.pBindings = bindings.data();
    
                    if (vkCreateDescriptorSetLayout(vkSettup.getDevice(), &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
                        throw std::runtime_error("failed to create descriptor set layout!");
                    }
                } else {
                    VkDescriptorSetLayoutBinding uboLayoutBinding{};
                    uboLayoutBinding.binding = 0;
                    uboLayoutBinding.descriptorCount = 1;
                    uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    uboLayoutBinding.pImmutableSamplers = nullptr;
                    uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
    
    
                    VkDescriptorSetLayoutBinding ubo2LayoutBinding{};
                    ubo2LayoutBinding.binding = 2;
                    ubo2LayoutBinding.descriptorCount = 1;
                    ubo2LayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    ubo2LayoutBinding.pImmutableSamplers = nullptr;
                    ubo2LayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    
                    std::array<VkDescriptorSetLayoutBinding, 2> bindings = {uboLayoutBinding, ubo2LayoutBinding};
    
                    VkDescriptorSetLayoutCreateInfo layoutInfo{};
                    layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
                    layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());;
                    layoutInfo.pBindings = bindings.data();
    
                    if (vkCreateDescriptorSetLayout(vkSettup.getDevice(), &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
                        throw std::runtime_error("failed to create descriptor set layout!");
                    }
                }
            }
            void RenderTarget::createDescriptorPool(const Texture* texture) {
                if (texture != nullptr) {
                    std::array<VkDescriptorPoolSize, 3> poolSizes{};
                    poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    poolSizes[0].descriptorCount = static_cast<uint32_t>(getMaxFramesInFlight());
                    poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
                    poolSizes[1].descriptorCount = static_cast<uint32_t>(getMaxFramesInFlight());
                    poolSizes[2].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    poolSizes[2].descriptorCount = static_cast<uint32_t>(getMaxFramesInFlight());
    
                    VkDescriptorPoolCreateInfo poolInfo{};
                    poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
                    poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
                    poolInfo.pPoolSizes = poolSizes.data();
                    poolInfo.maxSets = static_cast<uint32_t>(getMaxFramesInFlight());
                    if (vkCreateDescriptorPool(vkSettup.getDevice(), &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
                        throw std::runtime_error("echec de la creation de la pool de descripteurs!");
                    }
                } else {
                    std::array<VkDescriptorPoolSize, 2> poolSizes{};
                    poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    poolSizes[0].descriptorCount = static_cast<uint32_t>(getMaxFramesInFlight());
                    poolSizes[1].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                    poolSizes[1].descriptorCount = static_cast<uint32_t>(getMaxFramesInFlight());
    
                    VkDescriptorPoolCreateInfo poolInfo{};
                    poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
                    poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
                    poolInfo.pPoolSizes = poolSizes.data();
                    poolInfo.maxSets = static_cast<uint32_t>(getMaxFramesInFlight());
                    if (vkCreateDescriptorPool(vkSettup.getDevice(), &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
                        throw std::runtime_error("echec de la creation de la pool de descripteurs!");
                    }
                }
            }
            void RenderTarget::createDescriptorSets(const Texture* texture) {
                std::vector<VkDescriptorSetLayout> layouts(getMaxFramesInFlight(), descriptorSetLayout);
                VkDescriptorSetAllocateInfo allocInfo{};
                allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
                allocInfo.descriptorPool = descriptorPool;
                allocInfo.descriptorSetCount = static_cast<uint32_t>(getMaxFramesInFlight());
                allocInfo.pSetLayouts = layouts.data();
                descriptorSets.resize(getMaxFramesInFlight());
                if (vkAllocateDescriptorSets(vkSettup.getDevice(), &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
                    throw std::runtime_error("echec de l'allocation d'un set de descripteurs!");
                }
                for (size_t i = 0; i < getMaxFramesInFlight(); i++) {
                    VkDescriptorBufferInfo bufferInfo{};
                    bufferInfo.buffer = uniformBuffers[i];
                    bufferInfo.offset = 0;
                    bufferInfo.range = sizeof(UniformBufferObject);
                    VkDescriptorBufferInfo bufferInfo2{};
                    bufferInfo2.buffer = uniformBuffers2[i];
                    bufferInfo2.offset = 0;
                    bufferInfo2.range = sizeof(UniformBufferObject2);
                    if (texture != nullptr) {
                        VkDescriptorImageInfo imageInfo{};
                        imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
                        imageInfo.imageView = texture->getImageView();
                        imageInfo.sampler = texture->getSampler();
                        std::array<VkWriteDescriptorSet, 3> descriptorWrites{};
    
                        descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
                        descriptorWrites[0].dstSet = descriptorSets[i];
                        descriptorWrites[0].dstBinding = 0;
                        descriptorWrites[0].dstArrayElement = 0;
                        descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                        descriptorWrites[0].descriptorCount = 1;
                        descriptorWrites[0].pBufferInfo = &bufferInfo;
    
    
    
                        descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
                        descriptorWrites[1].dstSet = descriptorSets[i];
                        descriptorWrites[1].dstBinding = 1;
                        descriptorWrites[1].dstArrayElement = 0;
                        descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
                        descriptorWrites[1].descriptorCount = 1;
                        descriptorWrites[1].pImageInfo = &imageInfo;
    
                        descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
                        descriptorWrites[2].dstSet = descriptorSets[i];
                        descriptorWrites[2].dstBinding = 2;
                        descriptorWrites[2].dstArrayElement = 0;
                        descriptorWrites[2].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                        descriptorWrites[2].descriptorCount = 1;
                        descriptorWrites[2].pBufferInfo = &bufferInfo2;
                        vkUpdateDescriptorSets(vkSettup.getDevice(), static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
                    }  else {
                        std::array<VkWriteDescriptorSet, 2> descriptorWrites{};
    
                        descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
                        descriptorWrites[0].dstSet = descriptorSets[i];
                        descriptorWrites[0].dstBinding = 0;
                        descriptorWrites[0].dstArrayElement = 0;
                        descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                        descriptorWrites[0].descriptorCount = 1;
                        descriptorWrites[0].pBufferInfo = &bufferInfo;
    
                        descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
                        descriptorWrites[1].dstSet = descriptorSets[i];
                        descriptorWrites[1].dstBinding = 2;
                        descriptorWrites[1].dstArrayElement = 0;
                        descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
                        descriptorWrites[1].descriptorCount = 1;
                        descriptorWrites[1].pBufferInfo = &bufferInfo2;
                        vkUpdateDescriptorSets(vkSettup.getDevice(), static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
                    }
                }
            }
            void RenderTarget::createGraphicPipeline(const Vertex* vertices, unsigned int vertexCount, sf::PrimitiveType type,
                          RenderStates states = RenderStates::Default) {
                defaultShader.createShaderModules();
                VkShaderModule vertShaderModule = defaultShader.getVertexShaderModule();
                VkShaderModule fragShaderModule = defaultShader.getFragmentShaderModule();
                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};
    
                VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
                auto bindingDescription = Vertex::getBindingDescription();
                auto attributeDescriptions = Vertex::getAttributeDescriptions();
                vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
                vertexInputInfo.vertexBindingDescriptionCount = 1;
                vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
                vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
                vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
    
    
                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 = getSwapchainExtents().width;
                viewport.height = getSwapchainExtents().height;
                viewport.minDepth = 0.0f;
                viewport.maxDepth = 1.0f;
    
                VkRect2D scissor{};
                scissor.offset = {0, 0};
                scissor.extent = getSwapchainExtents();
    
                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 = 1;
                pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
    
                if (vkCreatePipelineLayout(vkSettup.getDevice(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
                    throw core::Erreur(0, "failed to create pipeline layout!", 1);
                }
                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(vkSettup.getDevice(), VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
                    throw core::Erreur(0, "failed to create graphics pipeline!", 1);
                }
                defaultShader.cleanupShaderModules();
            }
            void RenderTarget::createCommandPool() {
                window::VkSettup::QueueFamilyIndices queueFamilyIndices = vkSettup.findQueueFamilies(vkSettup.getPhysicalDevice(), getSurface());
    
                VkCommandPoolCreateInfo poolInfo{};
                poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
                poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
                poolInfo.flags = 0; // Optionel
                if (vkCreateCommandPool(vkSettup.getDevice(), &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
                    throw core::Erreur(0, "échec de la création d'une command pool!", 1);
                }
                vkSettup.setCommandPool(commandPool);
            }
            void RenderTarget::createUniformBuffers() {
                VkDeviceSize bufferSize = sizeof(UniformBufferObject);
    
                uniformBuffers.resize(getMaxFramesInFlight());
                uniformBuffersMemory.resize(getMaxFramesInFlight());
    
                for (size_t i = 0; i < getMaxFramesInFlight(); i++) {
                    createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBuffers[i], uniformBuffersMemory[i]);
                }
                VkDeviceSize bufferSize2 = sizeof(UniformBufferObject2);
                uniformBuffers2.resize(getMaxFramesInFlight());
                uniformBuffersMemory2.resize(getMaxFramesInFlight());
    
                for (size_t i = 0; i < getMaxFramesInFlight(); i++) {
                    createBuffer(bufferSize2, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBuffers2[i], uniformBuffersMemory2[i]);
                }
    
            }
            void RenderTarget::updateUniformBuffer(uint32_t currentImage, UniformBufferObject ubo) {
                ubo.proj = m_view.getProjMatrix().getMatrix().transpose();
                ubo.proj.m22 *= -1;
                ubo.view = m_view.getViewMatrix().getMatrix().transpose();
                void* data;
                vkMapMemory(vkSettup.getDevice(), uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
                    memcpy(data, &ubo, sizeof(ubo));
                vkUnmapMemory(vkSettup.getDevice(), uniformBuffersMemory[currentImage]);
    
            }
            void RenderTarget::updateUniformBuffer(uint32_t currentImage, UniformBufferObject2 ubo) {
                void* data;
                vkMapMemory(vkSettup.getDevice(), uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
                    memcpy(data, &ubo, sizeof(ubo));
                vkUnmapMemory(vkSettup.getDevice(), uniformBuffersMemory[currentImage]);
    
            }
            void RenderTarget::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
                VkBufferCreateInfo bufferInfo{};
                bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
                bufferInfo.size = size;
                bufferInfo.usage = usage;
                bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
    
                if (vkCreateBuffer(vkSettup.getDevice(), &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
                    throw std::runtime_error("failed to create buffer!");
                }
    
                VkMemoryRequirements memRequirements;
                vkGetBufferMemoryRequirements(vkSettup.getDevice(), buffer, &memRequirements);
    
                VkMemoryAllocateInfo allocInfo{};
                allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
                allocInfo.allocationSize = memRequirements.size;
                allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
    
                if (vkAllocateMemory(vkSettup.getDevice(), &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
                    throw std::runtime_error("failed to allocate buffer memory!");
                }
    
                vkBindBufferMemory(vkSettup.getDevice(), buffer, bufferMemory, 0);
            }
            uint32_t RenderTarget::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
                VkPhysicalDeviceMemoryProperties memProperties;
                vkGetPhysicalDeviceMemoryProperties(vkSettup.getPhysicalDevice(), &memProperties);
                for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
                    if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
                        return i;
                    }
                }
                throw std::runtime_error("aucun type de memoire ne satisfait le buffer!");
            }
            void RenderTarget::createCommandBuffers() {
                commandBuffers.resize(swapChainFramebuffers.size());
    
                VkCommandBufferAllocateInfo allocInfo{};
                allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
                allocInfo.commandPool = commandPool;
                allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
                allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
    
                if (vkAllocateCommandBuffers(vkSettup.getDevice(), &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
                    throw core::Erreur(0, "failed to allocate command buffers!", 1);
                }
    
                for (size_t i = 0; i < commandBuffers.size(); i++) {
                    VkCommandBufferBeginInfo beginInfo{};
                    beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    
                    if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
                        throw core::Erreur(0, "failed to begin recording command buffer!", 1);
                    }
                    VkRenderPassBeginInfo renderPassInfo{};
                    renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
                    renderPassInfo.renderPass = renderPass;
                    renderPassInfo.framebuffer = swapChainFramebuffers[i];
                    renderPassInfo.renderArea.offset = {0, 0};
                    renderPassInfo.renderArea.extent = getSwapchainExtents();
    
                    VkClearValue clrColor = {clearColor.r / 255.f,clearColor.g / 255.f, clearColor.b / 255.f, clearColor.a / 255.f};
                    renderPassInfo.clearValueCount = 1;
                    renderPassInfo.pClearValues = &clrColor;
    
                    vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
                    vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
                    VkBuffer vertexBuffers[] = {vertexBuffer.getVertexBuffer()};
                    VkDeviceSize offsets[] = {0};
                    vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
                    vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[getCurrentFrame()], 0, nullptr);
    
                    if(vertexBuffer.getIndicesSize() > 0) {
                        vkCmdBindIndexBuffer(commandBuffers[i], vertexBuffer.getIndexBuffer(), 0, VK_INDEX_TYPE_UINT16);
                    }
                    if(vertexBuffer.getIndicesSize() > 0) {
                        vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(vertexBuffer.getIndicesSize()), 1, 0, 0, 0);
                    } else {
                        vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertexBuffer.getSize()), 1, 0, 0);
                    }
    
                    vkCmdEndRenderPass(commandBuffers[i]);
    
                    if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
                        throw core::Erreur(0, "failed to record command buffer!", 1);
                    }
    
                }
            }
            void RenderTarget::cleanup() {
                vkDestroyCommandPool(vkSettup.getDevice(), commandPool, nullptr);
                vkDestroyPipeline(vkSettup.getDevice(), graphicsPipeline, nullptr);
                vkDestroyPipelineLayout(vkSettup.getDevice(), pipelineLayout, nullptr);
                vkDestroyRenderPass(vkSettup.getDevice(), renderPass, nullptr);
                vkDestroyDescriptorSetLayout(vkSettup.getDevice(), descriptorSetLayout, nullptr);
                for (size_t i = 0; i < getSwapchainImages().size(); i++) {
                    vkDestroyBuffer(vkSettup.getDevice(), uniformBuffers[i], nullptr);
                    vkFreeMemory(vkSettup.getDevice(), uniformBuffersMemory[i], nullptr);
                }
                 vkDestroyDescriptorPool(vkSettup.getDevice(), descriptorPool, nullptr);
                 vkDestroyDescriptorSetLayout(vkSettup.getDevice(), descriptorSetLayout, nullptr);
                 vkDestroyRenderPass(vkSettup.getDevice(), renderPass, nullptr);
            }
            std::vector<VkCommandBuffer> RenderTarget::getCommandBuffers() {
                return commandBuffers;
            }

    Voilà ce que m'affiche les validations layers :

    Je ne comprends pas pourquoi il me dit que pCommandBuffers n'est pas un pointeur valide.

    void RenderWindow::drawVulkanFrame() {
                if (getCommandBuffers().size() > 0) {
                    vkWaitForFences(vkSettup.getDevice(), 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
                    uint32_t imageIndex;
                    vkAcquireNextImageKHR(vkSettup.getDevice(), swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
                    // Vérifier si une frame précédente est en train d'utiliser cette image (il y a une fence à attendre)
                    if (imagesInFlight[imageIndex] != VK_NULL_HANDLE) {
                        vkWaitForFences(vkSettup.getDevice(), 1, &imagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
                    }
                     // Marque l'image comme étant à nouveau utilisée par cette frame
                    imagesInFlight[imageIndex] = inFlightFences[currentFrame];
    
                    VkSubmitInfo submitInfo{};
                    submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    
                    VkSemaphore waitSemaphores[] = {imageAvailableSemaphores[currentFrame]};
                    VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
                    submitInfo.waitSemaphoreCount = 1;
                    submitInfo.pWaitSemaphores = waitSemaphores;
                    submitInfo.pWaitDstStageMask = waitStages;
                    submitInfo.commandBufferCount = 1;
                    submitInfo.pCommandBuffers = &getCommandBuffers()[imageIndex];
                    VkSemaphore signalSemaphores[] = {renderFinishedSemaphores[currentFrame]};
                    submitInfo.signalSemaphoreCount = 1;
                    submitInfo.pSignalSemaphores = signalSemaphores;
                    vkResetFences(vkSettup.getDevice(), 1, &inFlightFences[currentFrame]);
                    if (vkQueueSubmit(vkSettup.getGraphicQueue(), 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
                        throw core::Erreur(0, "échec de l'envoi d'un command buffer!", 1);
                    }
                    VkPresentInfoKHR presentInfo{};
                    presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
    
                    presentInfo.waitSemaphoreCount = 1;
                    presentInfo.pWaitSemaphores = signalSemaphores;
                    VkSwapchainKHR swapChains[] = {swapChain};
                    presentInfo.swapchainCount = 1;
                    presentInfo.pSwapchains = swapChains;
                    presentInfo.pImageIndices = &imageIndex;
                    presentInfo.pResults = nullptr; // Optionnel
                    vkQueuePresentKHR(vkSettup.getPresentQueue(), &presentInfo);
                    currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
                    vkDeviceWaitIdle(vkSettup.getDevice());
                }
            }

    ça crash dans vkQueueSubmit.

    Et apparemment il faut lui indiquer toutes les variables dans les shaders même celles que je n'utilise pas pour les descriptor je trouve pas ça très pratique de la part de vulkan parce que la texture pourrait être null et dans ce cas inutile de récupérer l'imageview et le texture sampler.

    Merci.

    EDIT : ça ne crash plus il fallait retourner une référence ici :

    std::vector<VkCommandBuffer>& RenderTarget::getCommandBuffers() {
                return commandBuffers;
            }

    Par contre ça ne m'affiche rien je n'ai plus l'erreur avec pCommandBuffer mais j'ai toujours les autres erreurs.
    EDIT 2 : j'ai essayer avec la texture plus d'erreur mais ça ne m'affiche rien.

    -
    Edité par OmbreNoire 23 novembre 2023 à 19:33:50

    • Partager sur Facebook
    • Partager sur Twitter

    [VULKAN] Validation layer erreur.

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown