Changeset fba08f2 in opengl-game


Ignore:
Timestamp:
Aug 2, 2019, 4:15:02 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
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)
Message:

Update vulkan-game.cpp to support texturing in the shader

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • README.txt

    r621664a rfba08f2  
    8585
    8686make vulkangame && ./vulkangame
     87
     88REFERENCE
     89----------
     90
     91UV 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  
    22#extension GL_ARB_separate_shader_objects : enable
    33
     4layout(binding = 1) uniform sampler2D texSampler;
     5
    46layout(location = 0) in vec3 fragColor;
     7layout(location = 1) in vec2 fragTexCoord;
    58
    69layout(location = 0) out vec4 outColor;
    710
    811void 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);
    1015}
  • shaders/shader.vert

    r621664a rfba08f2  
    11#version 450
    22#extension GL_ARB_separate_shader_objects : enable
     3
     4vec2 positions[3] = vec2[](
     5    vec2( 0.0,  0.5),
     6    vec2(-0.5, -0.5),
     7    vec2( 0.5, -0.5)
     8);
    39
    410layout (binding = 0) uniform UniformBufferObject {
     
    1016layout(location = 0) in vec2 inPosition;
    1117layout(location = 1) in vec3 inColor;
     18layout(location = 2) in vec2 inTexCoord;
    1219
    1320layout(location = 0) out vec3 fragColor;
     21layout(location = 1) out vec2 fragTexCoord;
    1422
    1523void 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);
    1726   fragColor = inColor;
     27   fragTexCoord = inTexCoord;
    1828}
  • vulkan-game.cpp

    r621664a rfba08f2  
    1414
    1515#define GLM_FORCE_RADIANS
     16#define GLM_FORCE_DEPTH_ZERO_TO_ONE
    1617#include <glm/glm.hpp>
    1718#include <glm/gtc/matrix_transform.hpp>
     
    6869   glm::vec2 pos;
    6970   glm::vec3 color;
     71   glm::vec2 texCoord;
    7072
    7173   static VkVertexInputBindingDescription getBindingDescription() {
     
    7981   }
    8082
    81    static array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions() {
    82       array<VkVertexInputAttributeDescription, 2> attributeDescriptions = {};
     83   static array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
     84      array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {};
    8385
    8486      attributeDescriptions[0].binding = 0;
     
    9193      attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
    9294      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);
    93100
    94101      return attributeDescriptions;
     
    103110
    104111const 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}}
    109116};
    110117
     
    180187      VkImage textureImage;
    181188      VkDeviceMemory textureImageMemory;
     189      VkImageView textureImageView;
     190      VkSampler textureSampler;
    182191
    183192      VkBuffer vertexBuffer;
     
    230239         createCommandPool();
    231240         createTextureImage();
     241         createTextureImageView();
     242         createTextureSampler();
    232243         createVertexBuffer();
    233244         createIndexBuffer();
     
    377388      bool isDeviceSuitable(VkPhysicalDevice device) {
    378389         VkPhysicalDeviceProperties deviceProperties;
    379          VkPhysicalDeviceFeatures deviceFeatures;
    380 
    381390         vkGetPhysicalDeviceProperties(device, &deviceProperties);
    382          vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
    383391
    384392         cout << "Device: " << deviceProperties.deviceName << endl;
     
    393401         }
    394402
    395          return indices.isComplete() && extensionsSupported && swapChainAdequate;
     403         VkPhysicalDeviceFeatures supportedFeatures;
     404         vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
     405
     406         return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
    396407      }
    397408
     
    430441
    431442         VkPhysicalDeviceFeatures deviceFeatures = {};
     443         deviceFeatures.samplerAnisotropy = VK_TRUE;
    432444
    433445         VkDeviceCreateInfo createInfo = {};
     
    584596
    585597         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);
    606599         }
    607600      }
     
    652645         VkDescriptorSetLayoutBinding uboLayoutBinding = {};
    653646         uboLayoutBinding.binding = 0;
     647         uboLayoutBinding.descriptorCount = 1;
    654648         uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    655          uboLayoutBinding.descriptorCount = 1;
    656649         uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
    657650         uboLayoutBinding.pImmutableSamplers = nullptr;
    658651
     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 };
    659660         VkDescriptorSetLayoutCreateInfo layoutInfo = {};
    660661         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();
    663664
    664665         if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
     
    10171018      }
    10181019
     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
    10191076      void createVertexBuffer() {
    10201077         VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
     
    11591216
    11601217      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());
    11641223
    11651224         VkDescriptorPoolCreateInfo poolInfo = {};
    11661225         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();
    11691228         poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
    11701229
     
    11941253            bufferInfo.range = sizeof(UniformBufferObject);
    11951254
    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);
    12081283         }
    12091284      }
     
    13801455
    13811456         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     1457         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    13821458      }
    13831459
     
    13931469         ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
    13941470         ubo.proj[1][1] *= -1;
     1471
     1472         ubo.view = glm::mat4(1.0f);
    13951473
    13961474         void* data;
     
    14291507         cleanupSwapChain();
    14301508
     1509         vkDestroySampler(device, textureSampler, nullptr);
     1510         vkDestroyImageView(device, textureImageView, nullptr);
    14311511         vkDestroyImage(device, textureImage, nullptr);
    14321512         vkFreeMemory(device, textureImageMemory, nullptr);
Note: See TracChangeset for help on using the changeset viewer.