Changeset c074f81 in opengl-game for vulkan-utils.hpp


Ignore:
Timestamp:
Jun 8, 2021, 2:29:12 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
8aa4888
Parents:
567fa88
Message:

Change VulkanUtils::copyDataToMemory() to always require a size and to optionally flush the memory. Also add a VulkanUtils::copyDataToMappedMemory function that does the same thing, but assumes the data is already mapped.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-utils.hpp

    r567fa88 rc074f81  
    103103
    104104      template<class DataType>
    105       static void copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
    106                                    VkDeviceSize offset);
     105      static void copyDataToMemory(VkDevice device, DataType* srcData, VkDeviceMemory bufferMemory,
     106                                   VkDeviceSize offset, VkDeviceSize size, bool flush);
    107107
    108108      template<class DataType>
    109       static void copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
    110                                    VkDeviceSize offset, VkDeviceSize size);
     109      static void copyDataToMappedMemory(VkDevice device, DataType* srcData, void* mappedData,
     110                                         VkDeviceMemory bufferMemory, VkDeviceSize size, bool flush);
    111111
    112112      static bool hasStencilComponent(VkFormat format);
     
    140140
    141141template<class DataType>
    142 void VulkanUtils::copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
    143                                    VkDeviceSize offset) {
    144    copyDataToMemory(device, srcData, bufferMemory, offset, sizeof(DataType));
    145 }
    146 
    147 // TODO: This would be used when the GPU memory is host-coherent. If it it not, I also need to use vkFlushMappedMemoryRanges
    148 // I should create a variant that supports non-coherent memory
    149 template<class DataType>
    150 void VulkanUtils::copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
    151                                    VkDeviceSize offset, VkDeviceSize size) {
     142void VulkanUtils::copyDataToMemory(VkDevice device, DataType* srcData, VkDeviceMemory bufferMemory,
     143                                   VkDeviceSize offset, VkDeviceSize size, bool flush) {
    152144   void* data;
    153145
    154146   vkMapMemory(device, bufferMemory, offset, size, 0, &data);
    155    memcpy(data, &srcData, size);
     147
     148   memcpy(data, srcData, size);
     149
     150   if (flush) {
     151      VkMappedMemoryRange memoryRange{};
     152      memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
     153      memoryRange.memory = bufferMemory;
     154      memoryRange.size = size;
     155
     156      vkFlushMappedMemoryRanges(device, 1, &memoryRange);
     157   }
     158
    156159   vkUnmapMemory(device, bufferMemory);
     160}
     161
     162template<class DataType>
     163void VulkanUtils::copyDataToMappedMemory(VkDevice device, DataType* srcData, void* mappedData,
     164                                         VkDeviceMemory bufferMemory, VkDeviceSize size, bool flush) {
     165   memcpy(mappedData, srcData, size);
     166
     167   if (flush) {
     168      VkMappedMemoryRange memoryRange{};
     169      memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
     170      memoryRange.memory = bufferMemory;
     171      memoryRange.size = size;
     172
     173      vkFlushMappedMemoryRanges(device, 1, &memoryRange);
     174   }
    157175}
    158176
Note: See TracChangeset for help on using the changeset viewer.