Changeset 6486ba8 in opengl-game for vulkan-buffer.hpp


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

Rewrite some parts of SDLGame and VulkanGame to store per-object buffer object
data contiguously and copied to the GPU in one call

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-buffer.hpp

    rc1ec4f6 r6486ba8  
    3333      void resize();
    3434
     35      T& get(uint32_t index);
    3536      void add(T obj);
    3637
     
    4142      size_t alignment;
    4243      size_t range;
     44      //size_t capacity;
    4345      size_t numObjects;
    4446
    45       T* srcData; // TODO: Rename this to something else probably and rename rawData to data
    46       vector<T>* vData;
    47 
    48       // Remember that this is a pointer to the mapped video memory
    49       // Maybe rename it to mappedData or something to make that clearer
    50       void* rawData;
     47      T* rawData;
     48      vector<void*> mappedData;
    5149
    5250      size_t memRequirement(size_t capacity);
     51      size_t memOffset(uint32_t index);
    5352};
    5453
     
    7372                              , numObjects(0)
    7473                              , resized(false)
    75                               , srcData(nullptr)
    7674                              , rawData(nullptr)
    77                               , vData(nullptr) {
     75                              , mappedData() {
    7876}
    7977
     
    8583                              , numObjects(0)
    8684                              , resized(false)
    87                               , srcData(nullptr)
    8885                              , rawData(nullptr)
    89                               , vData(nullptr) {
     86                              , mappedData() {
    9087   if (minOffsetAlignment > 0) {
    9188      alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
    9289   }
    9390
    94    srcData = (T*)malloc(memRequirement(capacity));
     91   rawData = (T*)malloc(memRequirement(capacity));
    9592}
    9693
     
    10299template<class T>
    103100VulkanBuffer<T>::~VulkanBuffer() {
    104    if (srcData != nullptr) {
    105       free(srcData);
     101   if (rawData != nullptr) {
     102      free(rawData);
    106103   }
    107104}
     
    117114      range = other.range;
    118115
    119       if (srcData != nullptr) {
    120          free(srcData);
     116      if (rawData != nullptr) {
     117         free(rawData);
    121118      }
    122119
    123       srcData = other.srcData;
     120      rawData = other.rawData;
    124121
    125122      other.capacity = 0;
     
    127124      other.range = 0;
    128125
    129       other.srcData = nullptr;
     126      other.rawData = nullptr;
    130127   }
    131128
     
    139136
    140137template<class T>
     138T& VulkanBuffer<T>::get(uint32_t index) {
     139   // TODO: Check that index < numObjects
     140
     141   T* obj = (T*)((size_t)rawData + memOffset(index));
     142   return *obj;
     143}
     144
     145template<class T>
    141146void VulkanBuffer<T>::add(T obj) {
    142147   if (numObjects == capacity) {
     
    144149      resized = true;
    145150
     151      size_t oldMemReq = memRequirement(capacity);
     152
    146153      capacity *= 2;
     154
     155      size_t newMemReq = memRequirement(capacity);
     156
     157      T* newData = (T*)malloc(newMemReq);
     158      // TODO: Check for failure
     159
     160      memcpy(newData, rawData, oldMemReq);
     161
     162      free(rawData);
     163      rawData = newData;
    147164   }
     165
     166   T* ptr = (T*)((size_t)rawData + memOffset(numObjects));
     167   *ptr = obj;
    148168
    149169   numObjects++;
     
    160180}
    161181
     182template<class T>
     183size_t VulkanBuffer<T>::memOffset(uint32_t index) {
     184   return (index / range) * alignment + (index % range) * sizeof(T);
     185}
     186
    162187#endif // _VULKAN_BUFFER_H
Note: See TracChangeset for help on using the changeset viewer.