Changeset 69dccfe in opengl-game
- Timestamp:
- Aug 6, 2019, 7:01:45 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 5f3dba8
- Parents:
- bba12e7
- Files:
-
- 1 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
shaders/shader.frag
rbba12e7 r69dccfe 3 3 4 4 layout(binding = 1) uniform sampler2D texSampler; 5 layout(binding = 2) uniform sampler2D uiTexSampler; 5 6 6 7 layout(location = 0) in vec3 fragColor; 7 8 layout(location = 1) in vec2 fragTexCoord; 9 layout(location = 2) flat in uint isOverlay; 8 10 9 11 layout(location = 0) out vec4 outColor; 10 12 11 13 void main() { 12 outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb, 1.0); 14 if (isOverlay == 1) { 15 outColor = vec4(fragColor * texture(uiTexSampler, fragTexCoord).rgb, 0.3); 16 } else { 17 outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb, 1.0); 18 } 13 19 } -
shaders/shader.vert
rbba12e7 r69dccfe 14 14 layout(location = 0) out vec3 fragColor; 15 15 layout(location = 1) out vec2 fragTexCoord; 16 layout(location = 2) out uint isOverlay; 16 17 17 18 void main() { 18 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); 19 fragColor = inColor; 19 if (gl_VertexIndex < 8 ) { 20 gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); 21 fragColor = inColor; 22 isOverlay = 0; 23 } else { 24 gl_Position = vec4(inPosition, 1.0); 25 fragColor = vec3(0.0, 1.0, 1.0); 26 isOverlay = 1; 27 } 28 20 29 fragTexCoord = inTexCoord; 21 30 } -
vulkan-game.cpp
rbba12e7 r69dccfe 110 110 111 111 const vector<Vertex> vertices = { 112 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 113 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 114 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 115 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 116 112 117 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 113 118 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, … … 115 120 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 116 121 117 {{- 0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},118 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},119 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},120 {{- 0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}122 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 123 {{ 1.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 124 {{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 125 {{-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 121 126 }; 122 127 123 128 const vector<uint16_t> indices = { 124 0, 1, 2, 2, 3, 0, 125 4, 5, 6, 6, 7, 4 129 0, 1, 2, 2, 3, 0, 130 4, 5, 6, 6, 7, 4, 131 8, 9, 10, 10, 11, 8 126 132 }; 127 133 … … 198 204 VkDeviceMemory textureImageMemory; 199 205 VkImageView textureImageView; 206 207 VkImage overlayImage; 208 VkDeviceMemory overlayImageMemory; 209 VkImageView overlayImageView; 210 200 211 VkSampler textureSampler; 201 212 … … 249 260 createDepthResources(); 250 261 createFramebuffers(); 251 create TextureImage();252 create TextureImageView();262 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView); 263 createImageResources("textures/space.jpg", overlayImage, overlayImageMemory, overlayImageView); 253 264 createTextureSampler(); 254 265 createVertexBuffer(); … … 684 695 samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; 685 696 686 array<VkDescriptorSetLayoutBinding, 2> bindings = { uboLayoutBinding, samplerLayoutBinding }; 697 VkDescriptorSetLayoutBinding overlaySamplerLayoutBinding = {}; 698 overlaySamplerLayoutBinding.binding = 2; 699 overlaySamplerLayoutBinding.descriptorCount = 1; 700 overlaySamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 701 overlaySamplerLayoutBinding.pImmutableSamplers = nullptr; 702 overlaySamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; 703 704 array<VkDescriptorSetLayoutBinding, 3> bindings = { uboLayoutBinding, samplerLayoutBinding, overlaySamplerLayoutBinding }; 687 705 VkDescriptorSetLayoutCreateInfo layoutInfo = {}; 688 706 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; … … 768 786 VkPipelineColorBlendAttachmentState colorBlendAttachment = {}; 769 787 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; 770 colorBlendAttachment.blendEnable = VK_FALSE; 788 colorBlendAttachment.blendEnable = VK_TRUE; 789 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; 790 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; 791 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; 792 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; 793 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; 794 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; 771 795 772 796 VkPipelineColorBlendStateCreateInfo colorBlending = {}; … … 952 976 } 953 977 954 void create TextureImage() {978 void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) { 955 979 int texWidth, texHeight, texChannels; 956 980 957 stbi_uc* pixels = stbi_load( "textures/texture.jpg", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);981 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); 958 982 VkDeviceSize imageSize = texWidth * texHeight * 4; 959 983 … … 979 1003 createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL, 980 1004 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, 981 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);982 983 transitionImageLayout( textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);984 copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));985 transitionImageLayout( textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);1005 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory); 1006 1007 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); 1008 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)); 1009 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); 986 1010 987 1011 vkDestroyBuffer(device, stagingBuffer, nullptr); 988 1012 vkFreeMemory(device, stagingBufferMemory, nullptr); 1013 1014 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT); 989 1015 } 990 1016 … … 1114 1140 } 1115 1141 1116 void createTextureImageView() {1117 textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);1118 }1119 1120 1142 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) { 1121 1143 VkImageViewCreateInfo viewInfo = {}; … … 1312 1334 1313 1335 void createDescriptorPool() { 1314 array<VkDescriptorPoolSize, 2> poolSizes = {};1336 array<VkDescriptorPoolSize, 3> poolSizes = {}; 1315 1337 poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 1316 1338 poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1317 1339 poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1318 1340 poolSizes[1].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1341 poolSizes[2].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1342 poolSizes[2].descriptorCount = static_cast<uint32_t>(swapChainImages.size()); 1319 1343 1320 1344 VkDescriptorPoolCreateInfo poolInfo = {}; … … 1354 1378 imageInfo.sampler = textureSampler; 1355 1379 1356 array<VkWriteDescriptorSet, 2> descriptorWrites = {}; 1380 VkDescriptorImageInfo overlayImageInfo = {}; 1381 overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; 1382 overlayImageInfo.imageView = overlayImageView; 1383 overlayImageInfo.sampler = textureSampler; 1384 1385 array<VkWriteDescriptorSet, 3> descriptorWrites = {}; 1357 1386 1358 1387 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; … … 1375 1404 descriptorWrites[1].pImageInfo = &imageInfo; 1376 1405 descriptorWrites[1].pTexelBufferView = nullptr; 1406 1407 descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; 1408 descriptorWrites[2].dstSet = descriptorSets[i]; 1409 descriptorWrites[2].dstBinding = 2; 1410 descriptorWrites[2].dstArrayElement = 0; 1411 descriptorWrites[2].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 1412 descriptorWrites[2].descriptorCount = 1; 1413 descriptorWrites[2].pBufferInfo = nullptr; 1414 descriptorWrites[2].pImageInfo = &overlayImageInfo; 1415 descriptorWrites[2].pTexelBufferView = nullptr; 1377 1416 1378 1417 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); … … 1606 1645 1607 1646 vkDestroySampler(device, textureSampler, nullptr); 1647 1608 1648 vkDestroyImageView(device, textureImageView, nullptr); 1609 1649 vkDestroyImage(device, textureImage, nullptr); 1610 1650 vkFreeMemory(device, textureImageMemory, nullptr); 1651 1652 vkDestroyImageView(device, overlayImageView, nullptr); 1653 vkDestroyImage(device, overlayImage, nullptr); 1654 vkFreeMemory(device, overlayImageMemory, nullptr); 1611 1655 1612 1656 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.