From a701036dc87b01713ecfe2779ea6eef53d1669df Mon Sep 17 00:00:00 2001 From: Emil Shanaty Date: Thu, 27 Mar 2025 15:07:08 +0300 Subject: [PATCH] does not work --- shaders/shader.frag | 1 - shaders/shader.vert | 30 +++--- src/main.cpp | 216 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 231 insertions(+), 16 deletions(-) diff --git a/shaders/shader.frag b/shaders/shader.frag index 13009da..e948fd6 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,7 +1,6 @@ #version 450 layout(location = 0) in vec3 fragColor; - layout(location = 0) out vec4 outColor; void main() { diff --git a/shaders/shader.vert b/shaders/shader.vert index f4f4c7d..8f83563 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -1,20 +1,26 @@ #version 450 +<<<<<<< Updated upstream +======= +layout(binding = 0) uniform UniformBufferObject { + mat4 model; + mat4 view; + mat4 proj; +} ubo; + +layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inNormal; +layout(location = 2) in vec2 inUV; + +>>>>>>> Stashed changes layout(location = 0) out vec3 fragColor; -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[] ( - vec3(1.0, 0.0, 0.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) -); - void main() { +<<<<<<< Updated upstream gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); fragColor = colors[gl_VertexIndex]; +======= + gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); + fragColor = inNormal * 0.5 + 0.5; +>>>>>>> Stashed changes } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f120b10..02047bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ struct Vertex { glm::vec3 normal; glm::vec2 uv; }; +<<<<<<< Updated upstream std::vector vertices; std::vector indices; @@ -92,6 +93,13 @@ void loadModel() { } } +======= +struct UniformBufferObject { + alignas(16) glm::mat4 model; + alignas(16) glm::mat4 view; + alignas(16) glm::mat4 proj; +}; +>>>>>>> Stashed changes VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, @@ -203,7 +211,7 @@ class HelloTriangleApplication { private: const int WIDTH = 800; const int HEIGHT = 600; - const std::string appName = "Vulkan on MacOS"; + const std::string appName = "Vulkan on Windows"; const std::string engineName = "The Best Engine"; const std::vector deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME, @@ -224,7 +232,14 @@ private: VkDeviceMemory indexBufferMemory = VK_NULL_HANDLE; >>>>>>> Stashed changes Model model; + // В классе HelloTriangleApplication + std::vector uniformBuffers; + std::vector uniformBuffersMemory; + std::vector uniformBuffersMapped; + VkDescriptorSetLayout descriptorSetLayout; + VkDescriptorPool descriptorPool; + std::vector descriptorSets; #ifdef NDEBUG const bool enableValidationLayers = false; @@ -284,13 +299,23 @@ private: createSurface(); pickPhysicalDevice(); createLogicalDevice(); +<<<<<<< Updated upstream loadModel(); +======= + loadModel(); // Должен быть вызван перед createVertexBuffer() + createIndexBuffer(); + createUniformBuffers(); // Добавить эту строку + createDescriptorSetLayout(); + createUniformBuffers(); + createDescriptorPool(); + createDescriptorSets(); + createGraphicsPipeline(); +>>>>>>> Stashed changes createVertexBuffer(); createIndexBuffer(); createSwapChain(); createImageViews(); createRenderPass(); - createGraphicsPipeline(); createFramebuffers(); createCommandPool(); createCommandBuffer(); @@ -319,7 +344,92 @@ private: } private: <<<<<<< Updated upstream +<<<<<<< Updated upstream ======= +======= + void createDescriptorPool() { + VkDescriptorPoolSize poolSize{}; + poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + poolSize.descriptorCount = static_cast(swapChainImages.size()); + + VkDescriptorPoolCreateInfo poolInfo{}; + poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + poolInfo.poolSizeCount = 1; + poolInfo.pPoolSizes = &poolSize; + poolInfo.maxSets = static_cast(swapChainImages.size()); + + if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) { + throw std::runtime_error("failed to create descriptor pool!"); + } + } + + void createDescriptorSets() { + std::vector layouts(swapChainImages.size(), descriptorSetLayout); + + VkDescriptorSetAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = descriptorPool; + allocInfo.descriptorSetCount = static_cast(swapChainImages.size()); + allocInfo.pSetLayouts = layouts.data(); + + descriptorSets.resize(swapChainImages.size()); + if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) { + throw std::runtime_error("failed to allocate descriptor sets!"); + } + + for (size_t i = 0; i < swapChainImages.size(); i++) { + VkDescriptorBufferInfo bufferInfo{}; + bufferInfo.buffer = uniformBuffers[i]; + bufferInfo.offset = 0; + bufferInfo.range = sizeof(UniformBufferObject); + + VkWriteDescriptorSet descriptorWrite{}; + descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrite.dstSet = descriptorSets[i]; + descriptorWrite.dstBinding = 0; + descriptorWrite.dstArrayElement = 0; + descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrite.descriptorCount = 1; + descriptorWrite.pBufferInfo = &bufferInfo; + + vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr); + } + } + void createDescriptorSetLayout() { + VkDescriptorSetLayoutBinding uboLayoutBinding{}; + uboLayoutBinding.binding = 0; + uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + uboLayoutBinding.descriptorCount = 1; + uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + + VkDescriptorSetLayoutCreateInfo layoutInfo{}; + layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + layoutInfo.bindingCount = 1; + layoutInfo.pBindings = &uboLayoutBinding; + + if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) { + throw std::runtime_error("failed to create descriptor set layout!"); + } + } + void createUniformBuffers() { + VkDeviceSize bufferSize = sizeof(UniformBufferObject); + uniformBuffers.resize(swapChainImages.size()); + uniformBuffersMemory.resize(swapChainImages.size()); + uniformBuffersMapped.resize(swapChainImages.size()); + + for (size_t i = 0; i < swapChainImages.size(); 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] + ); + + vkMapMemory(device, uniformBuffersMemory[i], 0, bufferSize, 0, &uniformBuffersMapped[i]); + } + } +>>>>>>> Stashed changes void loadModel() { TinyGLTF loader; std::string err; @@ -353,6 +463,7 @@ private: const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer]; const float* vertexData = reinterpret_cast(&buffer.data[bufferView.byteOffset + accessor.byteOffset]); +<<<<<<< Updated upstream // Заполняем vertices vertices.resize(accessor.count); // Важно: задаем размер заранее for (size_t i = 0; i < accessor.count; ++i) { @@ -400,10 +511,80 @@ private: ); } } +======= + // Нормали (если есть) + const auto& normalAccessor = model.accessors[primitive.attributes.at("NORMAL")]; + const auto& normalView = model.bufferViews[normalAccessor.bufferView]; + const auto& normalBuffer = model.buffers[normalView.buffer]; + const float* normals = reinterpret_cast(&normalBuffer.data[normalView.byteOffset + normalAccessor.byteOffset]); + + // Индексы + /* + const auto& indexAccessor = model.accessors[primitive.indices]; + const auto& indexView = model.bufferViews[indexAccessor.bufferView]; + const auto& indexBuffer = model.buffers[indexView.buffer]; + const uint16_t* indicesSrc = reinterpret_cast(&indexBuffer.data[indexView.byteOffset + indexAccessor.byteOffset]); + */ + const auto& indexAccessor = model.accessors[primitive.indices]; + const auto& indexView = model.bufferViews[indexAccessor.bufferView]; + const auto& indexBuffer = model.buffers[indexView.buffer]; + + // Check component type + if (indexAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) { + const uint16_t* indicesSrc = reinterpret_cast(&indexBuffer.data[indexView.byteOffset + indexAccessor.byteOffset]); + for (size_t i = 0; i < indexAccessor.count; i++) { + indices.push_back(indicesSrc[i]); + } } + else if (indexAccessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) { + const uint32_t* indicesSrc = reinterpret_cast(&indexBuffer.data[indexView.byteOffset + indexAccessor.byteOffset]); + for (size_t i = 0; i < indexAccessor.count; i++) { + indices.push_back(indicesSrc[i]); + } + } + else { + throw std::runtime_error("Unsupported index type"); + } + + // Заполнение векторов + for (size_t i = 0; i < positionAccessor.count; i++) { + Vertex vertex{}; + vertex.pos = glm::vec3(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]); + vertex.normal = glm::vec3(normals[i * 3], normals[i * 3 + 1], normals[i * 3 + 2]); + vertices.push_back(vertex); + } + /* + for (size_t i = 0; i < indexAccessor.count; i++) { + indices.push_back(indicesSrc[i]); +>>>>>>> Stashed changes + } + */ + + + + // Check if "NORMAL" exists + if (primitive.attributes.find("NORMAL") == primitive.attributes.end()) { + throw std::runtime_error("Model missing NORMAL attribute"); + } + + // Similarly for "TEXCOORD_0" if using UVs + // Determine index type + VkIndexType indexType = (model.accessors[model.meshes[0].primitives[0].indices].componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) + ? VK_INDEX_TYPE_UINT16 + : VK_INDEX_TYPE_UINT32; + + // Update the binding call + //vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, indexType); } + + } +<<<<<<< Updated upstream +======= + std::cout << "Loaded " << vertices.size() << " vertices, " + << indices.size() << " indices" << std::endl; +>>>>>>> Stashed changes } >>>>>>> Stashed changes void createVertexBuffer() { @@ -870,6 +1051,11 @@ private: VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); + VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; + pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pipelineLayoutInfo.setLayoutCount = 1; + pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; // Добавить + VkPipelineShaderStageCreateInfo vertShaderStageInfo{}; vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; @@ -1069,6 +1255,13 @@ private: throw std::runtime_error("failed to begin recording command buffer!"); } + vkCmdBindDescriptorSets( + commandBuffer, + VK_PIPELINE_BIND_POINT_GRAPHICS, + pipelineLayout, + 0, 1, &descriptorSets[imageIndex], 0, nullptr + ); + VkRenderPassBeginInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassInfo.renderPass = renderPass; @@ -1151,6 +1344,18 @@ private: presentInfo.pSwapchains = swapChains.data(); presentInfo.pImageIndices = &imageIndex; + static auto startTime = std::chrono::high_resolution_clock::now(); + auto currentTime = std::chrono::high_resolution_clock::now(); + float time = std::chrono::duration(currentTime - startTime).count(); + + UniformBufferObject ubo{}; + ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f); + ubo.proj[1][1] *= -1; // GLM uses OpenGL's clip space, so flip Y + + memcpy(uniformBuffersMapped[imageIndex], &ubo, sizeof(ubo)); + vkQueuePresentKHR(presentQueue, &presentInfo); } @@ -1186,7 +1391,12 @@ private: if (enableValidationLayers) { DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); } - + vkDestroyDescriptorPool(device, descriptorPool, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); + for (size_t i = 0; i < swapChainImages.size(); i++) { + vkDestroyBuffer(device, uniformBuffers[i], nullptr); + vkFreeMemory(device, uniformBuffersMemory[i], nullptr); + } vkDestroySurfaceKHR(instance, surface, nullptr); vkDestroyInstance(instance, nullptr); if (vertexBuffer != VK_NULL_HANDLE) {