Changeset 5a0242e in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Nov 17, 2019, 6:56:16 PM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5ab1b20
Parents:
b8777b7
Message:

Refactor GraphicsPipeline_Vulkan to allow adding new data after creation:

  • Create the initial vertex and index buffers in the constructor
  • Add an addObject() method to add new vertex and index data and resize bufffers when needed
  • Remove the bindData() function, since addObject() can replace it
File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rb8777b7 r5a0242e  
    194194   createUniformBuffers();
    195195
    196    vector<ModelVertex> sceneVertices = {
    197       {{-0.5f, -0.5f, -2.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    198       {{ 0.5f, -0.5f, -2.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    199       {{ 0.5f,  0.5f, -2.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    200       {{-0.5f,  0.5f, -2.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
    201 
    202       {{-0.5f, -0.5f, -1.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    203       {{ 0.5f, -0.5f, -1.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    204       {{ 0.5f,  0.5f, -1.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    205       {{-0.5f,  0.5f, -1.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    206    };
    207    vector<uint16_t> sceneIndices = {
    208       0, 1, 2, 2, 3, 0,
    209       4, 5, 6, 6, 7, 4
    210    };
    211 
    212196   modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass,
    213       { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(ModelVertex));
    214 
    215    modelPipeline.bindData(sceneVertices, sceneIndices, commandPool, graphicsQueue);
     197      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24);
     198
     199   modelPipeline.addObject({
     200         {{-0.5f, -0.5f, -2.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     201         {{ 0.5f, -0.5f, -2.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     202         {{ 0.5f,  0.5f, -2.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     203         {{-0.5f,  0.5f, -2.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     204      }, {
     205         0, 1, 2, 2, 3, 0
     206      }, commandPool, graphicsQueue);
     207
     208   modelPipeline.addObject({
     209         {{-0.5f, -0.5f, -1.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     210         {{ 0.5f, -0.5f, -1.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     211         {{ 0.5f,  0.5f, -1.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     212         {{-0.5f,  0.5f, -1.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     213      }, {
     214         0, 1, 2, 2, 3, 0
     215      }, commandPool, graphicsQueue);
    216216
    217217   modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::pos));
     
    229229   modelPipeline.createDescriptorSets(swapChainImages);
    230230
    231    vector<OverlayVertex> overlayVertices = {
    232       {{-1.0f,  1.0f,  0.0f}, {0.0f, 1.0f}},
    233       {{ 1.0f,  1.0f,  0.0f}, {1.0f, 1.0f}},
    234       {{ 1.0f, -1.0f,  0.0f}, {1.0f, 0.0f}},
    235       {{-1.0f, -1.0f,  0.0f}, {0.0f, 0.0f}}
    236    };
    237    vector<uint16_t> overlayIndices = {
    238       0, 1, 2, 2, 3, 0
    239    };
    240 
    241231   overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass,
    242       { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex));
    243 
    244    overlayPipeline.bindData(overlayVertices, overlayIndices, commandPool, graphicsQueue);
     232      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
     233
     234   overlayPipeline.addObject({
     235         {{-1.0f,  1.0f,  0.0f}, {0.0f, 1.0f}},
     236         {{ 1.0f,  1.0f,  0.0f}, {1.0f, 1.0f}},
     237         {{ 1.0f, -1.0f,  0.0f}, {1.0f, 0.0f}},
     238         {{-1.0f, -1.0f,  0.0f}, {0.0f, 0.0f}}
     239      }, {
     240         0, 1, 2, 2, 3, 0
     241      }, commandPool, graphicsQueue);
    245242
    246243   overlayPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
     
    291288                  cout << "Adding a plane" << endl;
    292289                  float zOffset = -2.0f + (0.5f * numPlanes);
    293                   vector<ModelVertex> vertices = {
     290
     291                  vkDeviceWaitIdle(device);
     292                  vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     293
     294                  modelPipeline.addObject({
    294295                     {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    295296                     {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    296297                     {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    297298                     {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    298                   };
    299                   vector<uint16_t> indices = {
     299                  }, {
    300300                     0, 1, 2, 2, 3, 0
    301                   };
    302 
    303                   // TODO: Encapsulate these lines into an addObject() function in vulkan-game
    304 
    305                   vkDeviceWaitIdle(device);
    306                   vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    307 
    308                   modelPipeline.addObject(vertices, indices, commandPool, graphicsQueue);
     301                  }, commandPool, graphicsQueue);
    309302
    310303                  createCommandBuffers();
     
    314307                  cout << "Key event detected" << endl;
    315308               }
     309               break;
     310            case UI_EVENT_KEYUP:
    316311               break;
    317312            case UI_EVENT_MOUSEBUTTONDOWN:
Note: See TracChangeset for help on using the changeset viewer.