Changeset c074f81 in opengl-game


Ignore:
Timestamp:
Jun 8, 2021, 2:29:12 PM (4 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.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.cpp

    r567fa88 rc074f81  
    475475   }
    476476
    477    VulkanUtils::copyDataToMemory(device, object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0);
     477   VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,
     478                                 sizeof(object_VP_mats), false);
    478479}
    479480
  • sdl-game.hpp

    r567fa88 rc074f81  
    383383void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
    384384   for (size_t i = 0; i < set.memory.size(); i++) {
    385       VulkanUtils::copyDataToMemory(device, ssbo, set.memory[i], objIndex * sizeof(SSBOType));
     385      VulkanUtils::copyDataToMemory(device, &ssbo, set.memory[i], objIndex * sizeof(SSBOType), sizeof(ssbo), false);
    386386   }
    387387}
  • vulkan-game.cpp

    r567fa88 rc074f81  
    11611161   explosion_UBO.cur_time = curTime;
    11621162
    1163    VulkanUtils::copyDataToMemory(device, object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0);
    1164 
    1165    VulkanUtils::copyDataToMemory(device, ship_VP_mats, uniformBuffers_shipPipeline.memory[imageIndex], 0);
    1166 
    1167    VulkanUtils::copyDataToMemory(device, asteroid_VP_mats, uniformBuffers_asteroidPipeline.memory[imageIndex], 0);
    1168 
    1169    VulkanUtils::copyDataToMemory(device, laser_VP_mats, uniformBuffers_laserPipeline.memory[imageIndex], 0);
    1170 
    1171    VulkanUtils::copyDataToMemory(device, explosion_UBO, uniformBuffers_explosionPipeline.memory[imageIndex], 0);
     1163   VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,
     1164                                 sizeof(object_VP_mats), false);
     1165
     1166   VulkanUtils::copyDataToMemory(device, &ship_VP_mats, uniformBuffers_shipPipeline.memory[imageIndex], 0,
     1167                                 sizeof(ship_VP_mats), false);
     1168
     1169   VulkanUtils::copyDataToMemory(device, &asteroid_VP_mats, uniformBuffers_asteroidPipeline.memory[imageIndex], 0,
     1170                                 sizeof(asteroid_VP_mats), false);
     1171
     1172   VulkanUtils::copyDataToMemory(device, &laser_VP_mats, uniformBuffers_laserPipeline.memory[imageIndex], 0,
     1173                                 sizeof(laser_VP_mats), false);
     1174
     1175   VulkanUtils::copyDataToMemory(device, &explosion_UBO, uniformBuffers_explosionPipeline.memory[imageIndex], 0,
     1176                                 sizeof(explosion_UBO), false);
    11721177}
    11731178
  • vulkan-game.hpp

    r567fa88 rc074f81  
    3939// TODO: Consider if there is a better way of dealing with all the vertex types and ssbo types, maybe
    4040// by consolidating some and trying to keep new ones to a minimum
    41 
    42 struct OverlayVertex {
    43    vec3 pos;
    44    vec2 texCoord;
    45 };
    4641
    4742struct ModelVertex {
     
    546541void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
    547542   for (size_t i = 0; i < set.memory.size(); i++) {
    548       VulkanUtils::copyDataToMemory(device, ssbo, set.memory[i], objIndex * sizeof(SSBOType));
     543      VulkanUtils::copyDataToMemory(device, &ssbo, set.memory[i], objIndex * sizeof(SSBOType), sizeof(ssbo), false);
    549544   }
    550545}
  • 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.