Changeset 3e8cc8b in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Jan 16, 2020, 4:20:37 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
4ece3bf
Parents:
0fe8433
Message:

In VulkanGame, add the asteroid shader pipeline and start implementing asteroids flying at the player from the top of the screen

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r0fe8433 r3e8cc8b  
    2525   object_VP_mats = {};
    2626   ship_VP_mats = {};
     27   asteroid_VP_mats = {};
    2728}
    2829
     
    196197   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    197198      uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
     199   // TODO: Calculate the size of this buffer (and all the other SSBOs) based on the number of objects
    198200   createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    199201      storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
     
    274276      addObjectIndex<ShipVertex>(shipObjects.size(),
    275277      addVertexNormals<ShipVertex>({
     278
    276279         //back
    277280         {{ -0.5f,   0.3f,   0.0f}, {0.0f, 0.0f, 0.3f}},
     
    506509   shipPipeline.createDescriptorSets(swapChainImages);
    507510
     511   asteroidPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&AsteroidVertex::pos));
     512   asteroidPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&AsteroidVertex::color));
     513   asteroidPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&AsteroidVertex::normal));
     514   asteroidPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&AsteroidVertex::objIndex));
     515
     516   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     517      uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline);
     518   createBufferSet(10 * sizeof(SBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     519      storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline);
     520
     521   asteroidPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
     522      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_asteroidPipeline);
     523   asteroidPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
     524      VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_asteroidPipeline);
     525
     526   addObject(asteroidObjects, asteroidPipeline,
     527      centerObject<AsteroidVertex>(
     528      addObjectIndex<AsteroidVertex>(asteroidObjects.size(),
     529      addVertexNormals<AsteroidVertex>({
     530
     531         // front
     532         {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     533         {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     534         {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     535         {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     536         {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     537         {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     538
     539         // top
     540         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     541         {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     542         {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     543         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     544         {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     545         {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     546
     547         // bottom
     548         {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     549         {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     550         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     551         {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     552         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     553         {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}},
     554
     555         // back
     556         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     557         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     558         {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     559         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     560         {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     561         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     562
     563         // right
     564         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     565         {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     566         {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     567         {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     568         {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     569         {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     570
     571         // left
     572         {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     573         {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     574         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     575         {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     576         {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     577         {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     578      }))), {
     579          0,  1,  2,  3,  4,  5,
     580          6,  7,  8,  9, 10, 11,
     581         12, 13, 14, 15, 16, 17,
     582         18, 19, 20, 21, 22, 23,
     583         24, 25, 26, 27, 28, 29,
     584         30, 31, 32, 33, 34, 35,
     585      });
     586
     587   asteroidPipeline.createDescriptorSetLayout();
     588   asteroidPipeline.createPipeline("shaders/asteroid-vert.spv", "shaders/asteroid-frag.spv");
     589   asteroidPipeline.createDescriptorPool(swapChainImages);
     590   asteroidPipeline.createDescriptorSets(swapChainImages);
     591
    508592   cout << "Created all the graphics pipelines" << endl;
    509593
     
    515599      translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
    516600      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     601
     602   //vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f));
     603   vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f);
     604
     605   asteroidObjects[0].model_base =
     606      translate(mat4(1.0f), pos) *
     607      rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *
     608      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     609   asteroidObjects[0].model_transform = mat4(1.0); // Might not be needed
    517610}
    518611
     
    526619   shipPipeline = GraphicsPipeline_Vulkan<ShipVertex>(physicalDevice, device, renderPass,
    527620      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 138, 138);
     621
     622   asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex>(physicalDevice, device, renderPass,
     623      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 24, 36);
    528624}
    529625
     
    550646   ship_VP_mats.view = view;
    551647   ship_VP_mats.proj = proj;
     648
     649   asteroid_VP_mats.view = view;
     650   asteroid_VP_mats.proj = proj;
    552651}
    553652
     
    624723      }
    625724
     725      // this code moves the asteroids
     726      transformObject(asteroidObjects[0], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)));
     727
    626728      renderUI();
    627729      renderScene();
     
    647749   so_Ship.model = shipObjects[0].model_transform * shipObjects[0].model_base;
    648750
     751   so_Asteroid.model = asteroidObjects[0].model_transform * asteroidObjects[0].model_base;
     752   so_Asteroid.hp = 10.0;
     753
    649754   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats);
    650755
     
    654759
    655760   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship);
     761
     762   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], asteroid_VP_mats);
     763
     764   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_asteroidPipeline[currentImage], so_Asteroid);
    656765}
    657766
     
    757866   overlayPipeline.cleanupBuffers();
    758867   shipPipeline.cleanupBuffers();
     868   asteroidPipeline.cleanupBuffers();
    759869
    760870   for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
     
    12371347      modelPipeline.createRenderCommands(commandBuffers[i], i);
    12381348      shipPipeline.createRenderCommands(commandBuffers[i], i);
     1349      asteroidPipeline.createRenderCommands(commandBuffers[i], i);
    12391350
    12401351      // Always render this pipeline last
     
    13351446   shipPipeline.createDescriptorSets(swapChainImages);
    13361447
     1448   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     1449      uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline);
     1450   createBufferSet(10 * sizeof(SBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1451      storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline);
     1452
     1453   asteroidPipeline.updateRenderPass(renderPass);
     1454   asteroidPipeline.createPipeline("shaders/asteroid-vert.spv", "shaders/asteroid-frag.spv");
     1455   asteroidPipeline.createDescriptorPool(swapChainImages);
     1456   asteroidPipeline.createDescriptorSets(swapChainImages);
     1457
    13371458   createCommandBuffers();
    13381459}
     
    13501471   overlayPipeline.cleanup();
    13511472   shipPipeline.cleanup();
     1473   asteroidPipeline.cleanup();
    13521474
    13531475   vkDestroyRenderPass(device, renderPass, nullptr);
     
    13781500      vkFreeMemory(device, storageBuffersMemory_shipPipeline[i], nullptr);
    13791501   }
    1380 }
     1502
     1503   for (size_t i = 0; i < uniformBuffers_asteroidPipeline.size(); i++) {
     1504      vkDestroyBuffer(device, uniformBuffers_asteroidPipeline[i], nullptr);
     1505      vkFreeMemory(device, uniformBuffersMemory_asteroidPipeline[i], nullptr);
     1506   }
     1507
     1508   for (size_t i = 0; i < storageBuffers_asteroidPipeline.size(); i++) {
     1509      vkDestroyBuffer(device, storageBuffers_asteroidPipeline[i], nullptr);
     1510      vkFreeMemory(device, storageBuffersMemory_asteroidPipeline[i], nullptr);
     1511   }
     1512}
Note: See TracChangeset for help on using the changeset viewer.