Changeset 69dccfe in opengl-game


Ignore:
Timestamp:
Aug 6, 2019, 7:01:45 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5f3dba8
Parents:
bba12e7
Message:

Implement a translucent, fullscreen overlay that shows a loaded texture. This will later be used to show a UI rendered by SDL.

Files:
1 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • shaders/shader.frag

    rbba12e7 r69dccfe  
    33
    44layout(binding = 1) uniform sampler2D texSampler;
     5layout(binding = 2) uniform sampler2D uiTexSampler;
    56
    67layout(location = 0) in vec3 fragColor;
    78layout(location = 1) in vec2 fragTexCoord;
     9layout(location = 2) flat in uint isOverlay;
    810
    911layout(location = 0) out vec4 outColor;
    1012
    1113void 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   }
    1319}
  • shaders/shader.vert

    rbba12e7 r69dccfe  
    1414layout(location = 0) out vec3 fragColor;
    1515layout(location = 1) out vec2 fragTexCoord;
     16layout(location = 2) out uint isOverlay;
    1617
    1718void 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
    2029   fragTexCoord = inTexCoord;
    2130}
  • vulkan-game.cpp

    rbba12e7 r69dccfe  
    110110
    111111const 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
    112117   {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    113118   {{ 0.5f, -0.5f,  0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     
    115120   {{-0.5f,  0.5f,  0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
    116121
    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}}
    121126};
    122127
    123128const 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
    126132};
    127133
     
    198204      VkDeviceMemory textureImageMemory;
    199205      VkImageView textureImageView;
     206
     207      VkImage overlayImage;
     208      VkDeviceMemory overlayImageMemory;
     209      VkImageView overlayImageView;
     210
    200211      VkSampler textureSampler;
    201212
     
    249260         createDepthResources();
    250261         createFramebuffers();
    251          createTextureImage();
    252          createTextureImageView();
     262         createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
     263         createImageResources("textures/space.jpg", overlayImage, overlayImageMemory, overlayImageView);
    253264         createTextureSampler();
    254265         createVertexBuffer();
     
    684695         samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    685696
    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 };
    687705         VkDescriptorSetLayoutCreateInfo layoutInfo = {};
    688706         layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
     
    768786         VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
    769787         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;
    771795
    772796         VkPipelineColorBlendStateCreateInfo colorBlending = {};
     
    952976      }
    953977
    954       void createTextureImage() {
     978      void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
    955979         int texWidth, texHeight, texChannels;
    956980
    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);
    958982         VkDeviceSize imageSize = texWidth * texHeight * 4;
    959983
     
    9791003         createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
    9801004            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);
    9861010
    9871011         vkDestroyBuffer(device, stagingBuffer, nullptr);
    9881012         vkFreeMemory(device, stagingBufferMemory, nullptr);
     1013
     1014         view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
    9891015      }
    9901016
     
    11141140      }
    11151141
    1116       void createTextureImageView() {
    1117          textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
    1118       }
    1119 
    11201142      VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
    11211143         VkImageViewCreateInfo viewInfo = {};
     
    13121334
    13131335      void createDescriptorPool() {
    1314          array<VkDescriptorPoolSize, 2> poolSizes = {};
     1336         array<VkDescriptorPoolSize, 3> poolSizes = {};
    13151337         poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    13161338         poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
    13171339         poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    13181340         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());
    13191343
    13201344         VkDescriptorPoolCreateInfo poolInfo = {};
     
    13541378            imageInfo.sampler = textureSampler;
    13551379
    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 = {};
    13571386
    13581387            descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
     
    13751404            descriptorWrites[1].pImageInfo = &imageInfo;
    13761405            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;
    13771416
    13781417            vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
     
    16061645
    16071646         vkDestroySampler(device, textureSampler, nullptr);
     1647
    16081648         vkDestroyImageView(device, textureImageView, nullptr);
    16091649         vkDestroyImage(device, textureImage, nullptr);
    16101650         vkFreeMemory(device, textureImageMemory, nullptr);
     1651
     1652         vkDestroyImageView(device, overlayImageView, nullptr);
     1653         vkDestroyImage(device, overlayImage, nullptr);
     1654         vkFreeMemory(device, overlayImageMemory, nullptr);
    16111655
    16121656         vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
Note: See TracChangeset for help on using the changeset viewer.