Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a701036dc8 | ||
|
|
7cde1b9218 |
@@ -1,7 +1,6 @@
|
|||||||
#version 450
|
#version 450
|
||||||
|
|
||||||
layout(location = 0) in vec3 fragColor;
|
layout(location = 0) in vec3 fragColor;
|
||||||
|
|
||||||
layout(location = 0) out vec4 outColor;
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|||||||
+18
-12
@@ -1,20 +1,26 @@
|
|||||||
#version 450
|
#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;
|
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() {
|
void main() {
|
||||||
|
<<<<<<< Updated upstream
|
||||||
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
|
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
|
||||||
fragColor = colors[gl_VertexIndex];
|
fragColor = colors[gl_VertexIndex];
|
||||||
|
=======
|
||||||
|
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
|
||||||
|
fragColor = inNormal * 0.5 + 0.5;
|
||||||
|
>>>>>>> Stashed changes
|
||||||
}
|
}
|
||||||
+323
-5
@@ -27,6 +27,7 @@ struct Vertex {
|
|||||||
glm::vec3 normal;
|
glm::vec3 normal;
|
||||||
glm::vec2 uv;
|
glm::vec2 uv;
|
||||||
};
|
};
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<uint32_t> indices;
|
std::vector<uint32_t> 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,
|
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
||||||
const VkAllocationCallbacks* pAllocator,
|
const VkAllocationCallbacks* pAllocator,
|
||||||
@@ -203,7 +211,7 @@ class HelloTriangleApplication {
|
|||||||
private:
|
private:
|
||||||
const int WIDTH = 800;
|
const int WIDTH = 800;
|
||||||
const int HEIGHT = 600;
|
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::string engineName = "The Best Engine";
|
||||||
const std::vector<const char*> deviceExtensions = {
|
const std::vector<const char*> deviceExtensions = {
|
||||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
||||||
@@ -214,10 +222,24 @@ private:
|
|||||||
};
|
};
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<uint32_t> indices;
|
std::vector<uint32_t> indices;
|
||||||
|
<<<<<<< Updated upstream
|
||||||
VkBuffer vertexBuffer;
|
VkBuffer vertexBuffer;
|
||||||
VkDeviceMemory vertexBufferMemory;
|
VkDeviceMemory vertexBufferMemory;
|
||||||
|
=======
|
||||||
|
VkBuffer vertexBuffer = VK_NULL_HANDLE;
|
||||||
|
VkDeviceMemory vertexBufferMemory = VK_NULL_HANDLE;
|
||||||
|
VkBuffer indexBuffer = VK_NULL_HANDLE;
|
||||||
|
VkDeviceMemory indexBufferMemory = VK_NULL_HANDLE;
|
||||||
|
>>>>>>> Stashed changes
|
||||||
Model model;
|
Model model;
|
||||||
|
// Â êëàññå HelloTriangleApplication
|
||||||
|
std::vector<VkBuffer> uniformBuffers;
|
||||||
|
std::vector<VkDeviceMemory> uniformBuffersMemory;
|
||||||
|
std::vector<void*> uniformBuffersMapped;
|
||||||
|
|
||||||
|
VkDescriptorSetLayout descriptorSetLayout;
|
||||||
|
VkDescriptorPool descriptorPool;
|
||||||
|
std::vector<VkDescriptorSet> descriptorSets;
|
||||||
|
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
const bool enableValidationLayers = false;
|
const bool enableValidationLayers = false;
|
||||||
@@ -277,13 +299,23 @@ private:
|
|||||||
createSurface();
|
createSurface();
|
||||||
pickPhysicalDevice();
|
pickPhysicalDevice();
|
||||||
createLogicalDevice();
|
createLogicalDevice();
|
||||||
|
<<<<<<< Updated upstream
|
||||||
loadModel();
|
loadModel();
|
||||||
|
=======
|
||||||
|
loadModel(); // Äîëæåí áûòü âûçâàí ïåðåä createVertexBuffer()
|
||||||
|
createIndexBuffer();
|
||||||
|
createUniformBuffers(); // Äîáàâèòü ýòó ñòðîêó
|
||||||
|
createDescriptorSetLayout();
|
||||||
|
createUniformBuffers();
|
||||||
|
createDescriptorPool();
|
||||||
|
createDescriptorSets();
|
||||||
|
createGraphicsPipeline();
|
||||||
|
>>>>>>> Stashed changes
|
||||||
createVertexBuffer();
|
createVertexBuffer();
|
||||||
createIndexBuffer();
|
createIndexBuffer();
|
||||||
createSwapChain();
|
createSwapChain();
|
||||||
createImageViews();
|
createImageViews();
|
||||||
createRenderPass();
|
createRenderPass();
|
||||||
createGraphicsPipeline();
|
|
||||||
createFramebuffers();
|
createFramebuffers();
|
||||||
createCommandPool();
|
createCommandPool();
|
||||||
createCommandBuffer();
|
createCommandBuffer();
|
||||||
@@ -311,6 +343,250 @@ private:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
=======
|
||||||
|
=======
|
||||||
|
void createDescriptorPool() {
|
||||||
|
VkDescriptorPoolSize poolSize{};
|
||||||
|
poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||||
|
poolSize.descriptorCount = static_cast<uint32_t>(swapChainImages.size());
|
||||||
|
|
||||||
|
VkDescriptorPoolCreateInfo poolInfo{};
|
||||||
|
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||||
|
poolInfo.poolSizeCount = 1;
|
||||||
|
poolInfo.pPoolSizes = &poolSize;
|
||||||
|
poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
|
||||||
|
|
||||||
|
if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("failed to create descriptor pool!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void createDescriptorSets() {
|
||||||
|
std::vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
|
||||||
|
|
||||||
|
VkDescriptorSetAllocateInfo allocInfo{};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||||
|
allocInfo.descriptorPool = descriptorPool;
|
||||||
|
allocInfo.descriptorSetCount = static_cast<uint32_t>(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;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, "C:/Users/emil2/OneDrive/Desktop/Coding/Study/CGDG/vulkan_tweaking/models/Models/BrainStem/glTF/BrainStem.gltf");
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "GLTF warning: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "GLTF error: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
throw std::runtime_error("Failed to load GLTF: " + err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Î÷èñòèì ïðåäûäóùèå äàííûå
|
||||||
|
vertices.clear();
|
||||||
|
indices.clear();
|
||||||
|
|
||||||
|
for (const auto& mesh : model.meshes) {
|
||||||
|
for (const auto& primitive : mesh.primitives) {
|
||||||
|
// Ñíà÷àëà îáðàáîòàåì POSITION, ÷òîáû ñîçäàòü vertices
|
||||||
|
for (const auto& attribute : primitive.attributes) {
|
||||||
|
if (attribute.first == "POSITION") {
|
||||||
|
const tinygltf::Accessor& accessor = model.accessors[attribute.second];
|
||||||
|
const tinygltf::BufferView& bufferView = model.bufferViews[accessor.bufferView];
|
||||||
|
const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer];
|
||||||
|
const float* vertexData = reinterpret_cast<const float*>(&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
|
||||||
|
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
// Çàïîëíÿåì vertices
|
||||||
|
vertices.resize(accessor.count); // Âàæíî: çàäàåì ðàçìåð çàðàíåå
|
||||||
|
for (size_t i = 0; i < accessor.count; ++i) {
|
||||||
|
vertices[i].pos = glm::vec3(
|
||||||
|
vertexData[i * 3 + 0],
|
||||||
|
vertexData[i * 3 + 1],
|
||||||
|
vertexData[i * 3 + 2]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break; // Âûõîäèì ïîñëå îáðàáîòêè POSITION
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Çàòåì îáðàáàòûâàåì îñòàëüíûå àòðèáóòû (NORMAL, TEXCOORD_0)
|
||||||
|
for (const auto& attribute : primitive.attributes) {
|
||||||
|
if (attribute.first == "NORMAL") {
|
||||||
|
const tinygltf::Accessor& accessor = model.accessors[attribute.second];
|
||||||
|
const tinygltf::BufferView& bufferView = model.bufferViews[accessor.bufferView];
|
||||||
|
const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer];
|
||||||
|
const float* normalData = reinterpret_cast<const float*>(&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
|
||||||
|
|
||||||
|
// Ïðîâåðÿåì, ÷òî vertices èìååò äîñòàòî÷íûé ðàçìåð
|
||||||
|
if (vertices.size() < accessor.count) {
|
||||||
|
throw std::runtime_error("Mismatch between POSITION and NORMAL counts");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < accessor.count; ++i) {
|
||||||
|
vertices[i].normal = glm::vec3(
|
||||||
|
normalData[i * 3 + 0],
|
||||||
|
normalData[i * 3 + 1],
|
||||||
|
normalData[i * 3 + 2]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (attribute.first == "TEXCOORD_0") {
|
||||||
|
const tinygltf::Accessor& accessor = model.accessors[attribute.second];
|
||||||
|
const tinygltf::BufferView& bufferView = model.bufferViews[accessor.bufferView];
|
||||||
|
const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer];
|
||||||
|
const float* uvData = reinterpret_cast<const float*>(&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < accessor.count; ++i) {
|
||||||
|
vertices[i].uv = glm::vec2(
|
||||||
|
uvData[i * 2 + 0],
|
||||||
|
uvData[i * 2 + 1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
=======
|
||||||
|
// Íîðìàëè (åñëè åñòü)
|
||||||
|
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<const float*>(&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<const uint16_t*>(&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<const uint16_t*>(&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<const uint32_t*>(&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() {
|
void createVertexBuffer() {
|
||||||
if (vertices.empty()) {
|
if (vertices.empty()) {
|
||||||
throw std::runtime_error("No vertices loaded!");
|
throw std::runtime_error("No vertices loaded!");
|
||||||
@@ -775,6 +1051,11 @@ private:
|
|||||||
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
||||||
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
||||||
|
|
||||||
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
||||||
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||||
|
pipelineLayoutInfo.setLayoutCount = 1;
|
||||||
|
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; // Äîáàâèòü
|
||||||
|
|
||||||
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
|
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
|
||||||
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||||
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||||
@@ -974,6 +1255,13 @@ private:
|
|||||||
throw std::runtime_error("failed to begin recording command buffer!");
|
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{};
|
VkRenderPassBeginInfo renderPassInfo{};
|
||||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||||
renderPassInfo.renderPass = renderPass;
|
renderPassInfo.renderPass = renderPass;
|
||||||
@@ -987,6 +1275,12 @@ private:
|
|||||||
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||||
|
|
||||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
|
||||||
|
|
||||||
|
VkBuffer vertexBuffers[] = { vertexBuffer };
|
||||||
|
VkDeviceSize offsets[] = { 0 };
|
||||||
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
|
||||||
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
||||||
|
|
||||||
VkViewport viewport{};
|
VkViewport viewport{};
|
||||||
viewport.x = 0.0f;
|
viewport.x = 0.0f;
|
||||||
viewport.y = 0.0f;
|
viewport.y = 0.0f;
|
||||||
@@ -1001,7 +1295,7 @@ private:
|
|||||||
scissor.extent = swapChainExtent;
|
scissor.extent = swapChainExtent;
|
||||||
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||||
|
|
||||||
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
|
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
|
||||||
|
|
||||||
vkCmdEndRenderPass(commandBuffer);
|
vkCmdEndRenderPass(commandBuffer);
|
||||||
|
|
||||||
@@ -1050,6 +1344,18 @@ private:
|
|||||||
presentInfo.pSwapchains = swapChains.data();
|
presentInfo.pSwapchains = swapChains.data();
|
||||||
presentInfo.pImageIndices = &imageIndex;
|
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<float>(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);
|
vkQueuePresentKHR(presentQueue, &presentInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1085,10 +1391,22 @@ private:
|
|||||||
if (enableValidationLayers) {
|
if (enableValidationLayers) {
|
||||||
DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
|
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);
|
vkDestroySurfaceKHR(instance, surface, nullptr);
|
||||||
vkDestroyInstance(instance, nullptr);
|
vkDestroyInstance(instance, nullptr);
|
||||||
|
if (vertexBuffer != VK_NULL_HANDLE) {
|
||||||
|
vkDestroyBuffer(device, vertexBuffer, nullptr);
|
||||||
|
vkFreeMemory(device, vertexBufferMemory, nullptr);
|
||||||
|
}
|
||||||
|
if (indexBuffer != VK_NULL_HANDLE) {
|
||||||
|
vkDestroyBuffer(device, indexBuffer, nullptr);
|
||||||
|
vkFreeMemory(device, indexBufferMemory, nullptr);
|
||||||
|
}
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|||||||
Reference in New Issue
Block a user