Changeset d2d9286 in opengl-game for vulkan-utils.cpp


Ignore:
Timestamp:
Nov 7, 2019, 2:05:17 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
f985231
Parents:
87c8f1a
Message:

In vulkangame, implement the renderScene function to draw a frame in Vulkan and implement a test UI overlay

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-utils.cpp

    r87c8f1a rd2d9286  
    269269   VkDeviceMemory stagingBufferMemory;
    270270
    271    createBuffer(device, physicalDevice, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     271   createBuffer(device, physicalDevice, imageSize,
     272      VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
    272273      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    273274      stagingBuffer, stagingBufferMemory);
     
    308309
    309310   image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
     311}
     312
     313void VulkanUtils::populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
     314      VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image,
     315      VkQueue graphicsQueue) {
     316   int a, w, h;
     317
     318   SDL_QueryTexture(texture, nullptr, &a, &w, &h);
     319
     320   VkDeviceSize imageSize = w * h * 4;
     321   unsigned char* pixels = new unsigned char[imageSize];
     322
     323   SDL_RenderReadPixels(renderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4);
     324
     325   VkBuffer stagingBuffer;
     326   VkDeviceMemory stagingBufferMemory;
     327
     328   createBuffer(device, physicalDevice, imageSize,
     329      VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     330      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
     331      stagingBuffer, stagingBufferMemory);
     332
     333   void* data;
     334
     335   vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
     336   memcpy(data, pixels, static_cast<size_t>(imageSize));
     337
     338   VkMappedMemoryRange mappedMemoryRange = {};
     339   mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
     340   mappedMemoryRange.memory = stagingBufferMemory;
     341   mappedMemoryRange.offset = 0;
     342   mappedMemoryRange.size = VK_WHOLE_SIZE;
     343
     344   // TODO: Should probably check that the function succeeded
     345   vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange);
     346   vkUnmapMemory(device, stagingBufferMemory);
     347
     348   delete[] pixels;
     349
     350   transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
     351      VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, graphicsQueue);
     352   copyBufferToImage(device, commandPool, stagingBuffer, image.image,
     353      static_cast<uint32_t>(w), static_cast<uint32_t>(h), graphicsQueue);
     354   transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
     355      VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, graphicsQueue);
     356
     357   vkDestroyBuffer(device, stagingBuffer, nullptr);
     358   vkFreeMemory(device, stagingBufferMemory, nullptr);
    310359}
    311360
Note: See TracChangeset for help on using the changeset viewer.