Vulkan Samples Tutorial-02-Enumerate Physical Devices

in #vulkan8 years ago

source : 02-enumerate_devices.cpp
소스를 보면 기존에 작성한 샘플 코드들이 init_global_layer_properties(), init_instance() 등 함수화 되어 있는것을 알 수 있습니다. 눈으로 보면 쉬운데 가장 손에 익으려면 맨 처음 LunarG 설치하는 post에서 다루었듯이 빌드환경을 만들어서 소스를 한땀한땀 쓰는 것을 추천드립니다. 실제로 손으로 복붙함에도 불구하고 많은 에러가 난다는... ㅠㅠ

복습

앞에서 VkInstance object를 생성하였습니다.
한번더 복습을 하자면

  1. per-application에 VkInstance object가 있고
  2. 이 object는 application state를 관리합니다.
  3. 그런데 object만 있다고 뭔가를 할 순 없습니다.
  4. 그래서 GPU (physical devices, 무식하게 직역하면 물리 GPU...)를 찾아 application이 사용할 수 있게끔 해줘야 합니다.

Physical devices list 얻기

application은 VkInstance object만 만들었지, GPU를 어떻게 쓰는지 모릅니다.
그래서 그걸 알려주기 위해 physical devices list를 받아오는 과정을 거쳐야 합니다.

뭐, 임베디드 환경에 경우 대부분 GPU가 하나겠지만 요즘 desktop은 Intel CPU안에 내장된 GPU부터 외장 GPU등 multi GPU 환경을 가지고 있죠.

device list를 얻어오는 과정을 알면 앞으로 나올 다양한 object의 list를 얻어오는 과정이 똑같기 때문에
거침없이 코딩을 할 수 있습니다!!

// Get the number of devices (GPUs) available.
VkResult res = vkEnumeratePhysicalDevices(info.inst, &gpu_count, NULL);
// Allocate space and get the list of devices.
info.gpus.resize(gpu_count);
res = vkEnumeratePhysicalDevices(info.inst, &gpu_count, info.gpus.data());

코드를 보면 gpu_count pointer를 주고 pointer 부분을 NULL을 넘김으로써 physical devices가 몇 개인지를 얻어옵니다.
이제부터 사용하는 vk함수들은 VkIntance object handle을 필요로 하는 점도 눈여겨 보세요.

VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices(
    VkInstance                                  instance,
    uint32_t*                                   pPhysicalDeviceCount,
    VkPhysicalDevice*                            pPhysicalDevices);

LunarG API sample에선 VkPhysicalDevice* 로 넘기는 list를 vetcor에 저장합니다.

struct sample_info {
std::vector<VkPhysicalDevice> gpus;
...
}

그리고 gpu_count를 받아온 다음 info.gpu.resize(gpu_count);로 vector 크기를 resize합니다.
이걸 안해주면 이후 단계에서 crash가 발생하더군요 -_- count 만큼 resize하는 습관을 들여야 할 것 같습니다.

그래서 physical device list를 받아왔다면 이제는 진짜 쓸 GPU를 정해서 이를 쓸 수 있게 logical device object를 만들어야 합니다.

다음에는 logical device object를 만드는 과정을 보겠습니다. :)