Changeset fba08f2 in opengl-game
- Timestamp:
- Aug 2, 2019, 4:15:02 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 4f63fa8
- Parents:
- 621664a
- git-author:
- Dmitry Portnoy <dmp1488@…> (08/02/19 04:01:00)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (08/02/19 04:15:02)
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
README.txt
r621664a rfba08f2 85 85 86 86 make vulkangame && ./vulkangame 87 88 REFERENCE 89 ---------- 90 91 UV coords 0 u 1 x, y, z -1 (untransformed z ranges from 0 to 1, glm::perspective seems to make the z range negative) 92 0 -------> ^ 93 | | 94 v | -1 | 0 1 95 | <-----|-----> 96 1 V /| 97 / | 98 1 v 99 1 -
shaders/shader.frag
r621664a rfba08f2 2 2 #extension GL_ARB_separate_shader_objects : enable 3 3 4 layout(binding = 1) uniform sampler2D texSampler; 5 4 6 layout(location = 0) in vec3 fragColor; 7 layout(location = 1) in vec2 fragTexCoord; 5 8 6 9 layout(location = 0) out vec4 outColor; 7 10 8 11 void main() { 9 outColor = vec4(fragColor, 1.0); 12 // outColor = vec4(fragColor, 1.0); 13 // outColor = texture(texSampler, fragTexCoord); 14 outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb, 1.0); 10 15 } -
shaders/shader.vert
r621664a rfba08f2 1 1 #version 450 2 2 #extension GL_ARB_separate_shader_objects : enable 3 4 vec2 positions[3] = vec2[]( 5 vec2( 0.0, 0.5), 6 vec2(-0.5, -0.5), 7 vec2( 0.5, -0.5) 8 ); 3 9 4 10 layout (binding = 0) uniform UniformBufferObject { … … 10 16 layout(location = 0) in vec2 inPosition; 11 17 layout(location = 1) in vec3 inColor; 18 layout(location = 2) in vec2 inTexCoord; 12 19 13 20 layout(location = 0) out vec3 fragColor; 21 layout(location = 1) out vec2 fragTexCoord; 14 22 15 23 void main() { 16 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); 24 // gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); 25 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(positions[gl_VertexIndex], -5.0, 1.0); 17 26 fragColor = inColor; 27 fragTexCoord = inTexCoord; 18 28 } -
vulkan-game.cpp
r621664a rfba08f2 14 14 15 15 #define GLM_FORCE_RADIANS 16 #define GLM_FORCE_DEPTH_ZERO_TO_ONE 16 17 #include <glm/glm.hpp> 17 18 #include <glm/gtc/matrix_transform.hpp> … … 68 69 glm::vec2 pos; 69 70 glm::vec3 color; 71 glm::vec2 texCoord; 70 72 71 73 static VkVertexInputBindingDescription getBindingDescription() { … … 79 81 } 80 82 81 static array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions() {82 array<VkVertexInputAttributeDescription, 2> attributeDescriptions = {};83 static array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() { 84 array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {}; 83 85 84 86 attributeDescriptions[0].binding = 0; … … 91 93 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; 92 94 attributeDescriptions[1].offset = offsetof(Vertex, color); 95 96 attributeDescriptions[2].binding = 0; 97 attributeDescriptions[2].location = 2; 98 attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; 99 attributeDescriptions[2].offset = offsetof(Vertex, texCoord); 93 100 94 101 return attributeDescriptions; … … 103 110 104 111 const vector<Vertex> vertices = { 105 {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f} },106 {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f} },107 {{ 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f} },108 {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f} }112 {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f}}, 113 {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}, 114 {{ 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}}, 115 {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}} 109 116 }; 110 117 … … 180 187 VkImage textureImage; 181 188 VkDeviceMemory textureImageMemory; 189 VkImageView textureImageView; 190 VkSampler textureSampler; 182 191 183 192 VkBuffer vertexBuffer; … … 230 239 createCommandPool(); 231 240 createTextureImage(); 241 createTextureImageView(); 242 createTextureSampler(); 232 243 createVertexBuffer(); 233 244 createIndexBuffer(); … … 377 388 bool isDeviceSuitable(VkPhysicalDevice device) { 378 389 VkPhysicalDeviceProperties deviceProperties; 379 VkPhysicalDeviceFeatures deviceFeatures;380 381 390 vkGetPhysicalDeviceProperties(device, &deviceProperties); 382 vkGetPhysicalDeviceFeatures(device, &deviceFeatures);383 391 384 392 cout << "Device: " << deviceProperties.deviceName << endl; … … 393 401 } 394 402 395 return indices.isComplete() && extensionsSupported && swapChainAdequate; 403 VkPhysicalDeviceFeatures supportedFeatures; 404 vkGetPhysicalDeviceFeatures(device, &supportedFeatures); 405 406 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy; 396 407 } 397 408 … … 430 441 431 442 VkPhysicalDeviceFeatures deviceFeatures = {}; 443 deviceFeatures.samplerAnisotropy = VK_TRUE; 432 444 433 445 VkDeviceCreateInfo createInfo = {}; … … 584 596 585 597 for (size_t i = 0; i < swapChainImages.size(); i++) { 586 VkImageViewCreateInfo createInfo = {}; 587 createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; 588 createInfo.image = swapChainImages[i]; 589 createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; 590 createInfo.format = swapChainImageFormat; 591 592 createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; 593 createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; 594 createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; 595 createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; 596 597 createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 598 createInfo.subresourceRange.baseMipLevel = 0; 599 createInfo.subresourceRange.levelCount = 1; 600 createInfo.subresourceRange.baseArrayLayer = 0; 601 createInfo.subresourceRange.layerCount = 1; 602 603 if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) { 604 throw runtime_error("failed to create image views!"); 605 } 598 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat); 606 599 } 607 600 } … … 652 645 VkDescriptorSetLayoutBinding uboLayoutBinding = {}; 653 646 uboLayoutBinding.binding = 0; 647 uboLayoutBinding.descriptorCount = 1; 654 648 uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 655 uboLayoutBinding.descriptorCount = 1;656 649 uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; 657 650 uboLayoutBinding.pImmutableSamplers = nullptr; 658 651 652 VkDescriptorSetLayoutBinding samplerLayoutBinding = {}; 653 samplerLayoutBinding.binding = 1; 654 samplerLayoutBinding.descriptorCount = 1; 655 samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 656 samplerLayoutBinding.pImmutableSamplers = nullptr; 657 samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; 658 659 array<VkDescriptorSetLayoutBinding, 2> bindings = { uboLayoutBinding, samplerLayoutBinding }; 659 660 VkDescriptorSetLayoutCreateInfo layoutInfo = {}; 660 661 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; 661 layoutInfo.bindingCount = 1;662 layoutInfo.pBindings = &uboLayoutBinding;662 layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size()); 663 layoutInfo.pBindings = bindings.data(); 663 664 664 665 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) { … … 1017 1018 } 1018 1019 1020 void createTextureImageView() { 1021 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM); 1022 } 1023 1024 VkImageView createImageView(VkImage image, VkFormat format) { 1025 VkImageViewCreateInfo viewInfo = {}; 1026 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; 1027 viewInfo.image = image; 1028 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; 1029 viewInfo.format = format; 1030 1031 viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; 1032 viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; 1033 viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; 1034 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; 1035 1036 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; 1037 viewInfo.subresourceRange.baseMipLevel = 0; 1038 viewInfo.subresourceRange.levelCount = 1; 1039 viewInfo.subresourceRange.baseArrayLayer = 0; 1040 viewInfo.subresourceRange.layerCount = 1; 1041 1042 VkImageView imageView; 1043 if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) { 1044 throw runtime_error("failed to create texture image view!"); 1045 } 1046 1047 return imageView; 1048 } 1049 1050 void createTextureSampler() { 1051 VkSamplerCreateInfo samplerInfo = {}; 1052 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; 1053 samplerInfo.magFilter = VK_FILTER_LINEAR; 1054 samplerInfo.minFilter = VK_FILTER_LINEAR; 1055 1056 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; 1057 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; 1058 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; 1059 1060 samplerInfo.anisotropyEnable = VK_TRUE; 1061 samplerInfo.maxAnisotropy = 16; 1062 samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK; 1063 samplerInfo.unnormalizedCoordinates = VK_FALSE; 1064 samplerInfo.compareEnable = VK_FALSE; 1065 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; 1066 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; 1067 samplerInfo.mipLodBias = 0.0f; 1068 samplerInfo.minLod = 0.0f; 1069 samplerInfo.maxLod = 0.0f; 1070 1071 if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) { 1072 throw runtime_error("failed to create texture sampler!"); 1073 } 1074 } 1075 1019 1076 void createVertexBuffer() { 1020 1077 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size(); … … 1159 1216 1160 1217 void createDescriptorPool() { 1161 VkDescriptorPoolSize poolSize = {}; 1162 poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 1163 poolSize.descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1218 array<VkDescriptorPoolSize, 2> poolSizes = {}; 1219 poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 1220 poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1221 poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1222 poolSizes[1].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1164 1223 1165 1224 VkDescriptorPoolCreateInfo poolInfo = {}; 1166 1225 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; 1167 poolInfo.poolSizeCount = 1;1168 poolInfo.pPoolSizes = &poolSize;1226 poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size()); 1227 poolInfo.pPoolSizes = poolSizes.data(); 1169 1228 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size()); 1170 1229 … … 1194 1253 bufferInfo.range = sizeof(UniformBufferObject); 1195 1254 1196 VkWriteDescriptorSet descriptorWrite = {}; 1197 descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1198 descriptorWrite.dstSet = descriptorSets[i]; 1199 descriptorWrite.dstBinding = 0; 1200 descriptorWrite.dstArrayElement = 0; 1201 descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 1202 descriptorWrite.descriptorCount = 1; 1203 descriptorWrite.pBufferInfo = &bufferInfo; 1204 descriptorWrite.pImageInfo = nullptr; 1205 descriptorWrite.pTexelBufferView = nullptr; 1206 1207 vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr); 1255 VkDescriptorImageInfo imageInfo = {}; 1256 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; 1257 imageInfo.imageView = textureImageView; 1258 imageInfo.sampler = textureSampler; 1259 1260 array<VkWriteDescriptorSet, 2> descriptorWrites = {}; 1261 1262 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1263 descriptorWrites[0].dstSet = descriptorSets[i]; 1264 descriptorWrites[0].dstBinding = 0; 1265 descriptorWrites[0].dstArrayElement = 0; 1266 descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 1267 descriptorWrites[0].descriptorCount = 1; 1268 descriptorWrites[0].pBufferInfo = &bufferInfo; 1269 descriptorWrites[0].pImageInfo = nullptr; 1270 descriptorWrites[0].pTexelBufferView = nullptr; 1271 1272 descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1273 descriptorWrites[1].dstSet = descriptorSets[i]; 1274 descriptorWrites[1].dstBinding = 1; 1275 descriptorWrites[1].dstArrayElement = 0; 1276 descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1277 descriptorWrites[1].descriptorCount = 1; 1278 descriptorWrites[1].pBufferInfo = nullptr; 1279 descriptorWrites[1].pImageInfo = &imageInfo; 1280 descriptorWrites[1].pTexelBufferView = nullptr; 1281 1282 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); 1208 1283 } 1209 1284 } … … 1380 1455 1381 1456 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1457 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1382 1458 } 1383 1459 … … 1393 1469 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f); 1394 1470 ubo.proj[1][1] *= -1; 1471 1472 ubo.view = glm::mat4(1.0f); 1395 1473 1396 1474 void* data; … … 1429 1507 cleanupSwapChain(); 1430 1508 1509 vkDestroySampler(device, textureSampler, nullptr); 1510 vkDestroyImageView(device, textureImageView, nullptr); 1431 1511 vkDestroyImage(device, textureImage, nullptr); 1432 1512 vkFreeMemory(device, textureImageMemory, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.