[Bf-blender-cvs] [2ea87253396] tmp-vulkan: Vulkan: Retrieve platform information.

Jeroen Bakker noreply at git.blender.org
Tue Jun 29 16:51:38 CEST 2021


Commit: 2ea87253396b48608f32893a67fd8f92668311bb
Author: Jeroen Bakker
Date:   Tue Jun 29 16:46:51 2021 +0200
Branches: tmp-vulkan
https://developer.blender.org/rB2ea87253396b48608f32893a67fd8f92668311bb

Vulkan: Retrieve platform information.

Retrieve device platform information for `GPUPlatformGlobal`.
As vulkan needs a physical device it was moved from the GPUBackend
constructor to an init method that is called when an initial context
is created.

Still need to find out how to extract the driver type.

===================================================================

M	source/blender/gpu/GPU_context.h
M	source/blender/gpu/intern/gpu_backend.hh
M	source/blender/gpu/intern/gpu_context.cc
M	source/blender/gpu/opengl/gl_backend.hh
M	source/blender/gpu/vulkan/vk_backend.cc
M	source/blender/gpu/vulkan/vk_backend.hh
M	source/blender/gpu/vulkan/vk_context.hh

===================================================================

diff --git a/source/blender/gpu/GPU_context.h b/source/blender/gpu/GPU_context.h
index 6ef8427b6f9..9939f844ff8 100644
--- a/source/blender/gpu/GPU_context.h
+++ b/source/blender/gpu/GPU_context.h
@@ -40,7 +40,7 @@ typedef enum eGPUBackendType {
 #endif
 } eGPUBackendType;
 
-void GPU_backend_init(eGPUBackendType backend);
+void GPU_backend_create(eGPUBackendType backend);
 void GPU_backend_exit(void);
 
 /** Opaque type hiding blender::gpu::Context. */
diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh
index 5750b4f81bb..c0774c3b61e 100644
--- a/source/blender/gpu/intern/gpu_backend.hh
+++ b/source/blender/gpu/intern/gpu_backend.hh
@@ -46,6 +46,8 @@ class GPUBackend {
 
   static GPUBackend *get(void);
 
+  virtual void init() = 0;
+
   virtual void samplers_update(void) = 0;
   virtual void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) = 0;
 
diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc
index 56232cbd7d3..4e2b2433ecd 100644
--- a/source/blender/gpu/intern/gpu_context.cc
+++ b/source/blender/gpu/intern/gpu_context.cc
@@ -105,6 +105,7 @@ Context *Context::get()
 
 GPUContext *GPU_context_create(void *ghost_window, void *ghost_context)
 {
+  bool backend_created = false;
   if (GPUBackend::get() == nullptr) {
     /* FIXME We should get the context type from ghost instead of guessing it. */
     eGPUBackendType type = GPU_BACKEND_OPENGL;
@@ -113,12 +114,16 @@ GPUContext *GPU_context_create(void *ghost_window, void *ghost_context)
       type = GPU_BACKEND_VULKAN;
     }
 #endif
-    GPU_backend_init(type);
+    backend_created = true;
+    GPU_backend_create(type);
   }
-
-  Context *ctx = GPUBackend::get()->context_alloc(ghost_window, ghost_context);
-
+  GPUBackend *backend = GPUBackend::get();
+  Context *ctx = backend->context_alloc(ghost_window, ghost_context);
   GPU_context_active_set(wrap(ctx));
+
+  if (backend_created) {
+    backend->init();
+  }
   return wrap(ctx);
 }
 
@@ -177,7 +182,7 @@ void GPU_context_main_unlock(void)
 
 static GPUBackend *g_backend;
 
-void GPU_backend_init(eGPUBackendType backend_type)
+void GPU_backend_create(eGPUBackendType backend_type)
 {
   BLI_assert(g_backend == nullptr);
 
diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh
index 88c7bf49f35..bb220a699ae 100644
--- a/source/blender/gpu/opengl/gl_backend.hh
+++ b/source/blender/gpu/opengl/gl_backend.hh
@@ -47,7 +47,14 @@ class GLBackend : public GPUBackend {
   GLSharedOrphanLists shared_orphan_list_;
 
  public:
-  GLBackend()
+  ~GLBackend()
+  {
+    GLTexture::samplers_free();
+
+    GLBackend::platform_exit();
+  }
+
+  void init() override
   {
     /* platform_init needs to go first. */
     GLBackend::platform_init();
@@ -55,12 +62,6 @@ class GLBackend : public GPUBackend {
     GLBackend::capabilities_init();
     GLTexture::samplers_init();
   }
-  ~GLBackend()
-  {
-    GLTexture::samplers_free();
-
-    GLBackend::platform_exit();
-  }
 
   static GLBackend *get(void)
   {
diff --git a/source/blender/gpu/vulkan/vk_backend.cc b/source/blender/gpu/vulkan/vk_backend.cc
index 0018cbd342c..1c75e0dc193 100644
--- a/source/blender/gpu/vulkan/vk_backend.cc
+++ b/source/blender/gpu/vulkan/vk_backend.cc
@@ -34,13 +34,57 @@ namespace blender::gpu {
 /** \name Platform
  * \{ */
 
+static std::string api_version_get(VkPhysicalDeviceProperties &properties)
+{
+  uint32_t major = VK_VERSION_MAJOR(properties.driverVersion);
+  uint32_t minor = VK_VERSION_MINOR(properties.driverVersion);
+  uint32_t patch = VK_VERSION_PATCH(properties.driverVersion);
+
+  std::stringstream version;
+  version << major << "." << minor << "." << patch;
+  return version.str();
+}
+
+enum class VendorID : uint32_t {
+  AMD = 0x1002,
+  Intel = 0x1F96,
+  NVIDIA = 0x10de,
+};
+static constexpr StringRef VENDOR_NAME_AMD = "Advanced Micro Devices";
+static constexpr StringRef VENDOR_NAME_INTEL = "Intel";
+static constexpr StringRef VENDOR_NAME_NVIDIA = "NVIDIA";
+static constexpr StringRef VENDOR_NAME_UNKNOWN = "Unknown";
+
+static constexpr StringRef vendor_name_get(VendorID vendor_id)
+{
+  switch (vendor_id) {
+    case VendorID::AMD:
+      return VENDOR_NAME_AMD;
+    case VendorID::Intel:
+      return VENDOR_NAME_INTEL;
+    case VendorID::NVIDIA:
+      return VENDOR_NAME_NVIDIA;
+  }
+  return VENDOR_NAME_UNKNOWN;
+}
+
+static VendorID vendor_id_get(VkPhysicalDeviceProperties &properties)
+{
+  return static_cast<VendorID>(properties.vendorID);
+}
+
 void VKBackend::platform_init(void)
 {
   BLI_assert(!GPG.initialized);
 
-  const char *vendor = "vendor";
-  const char *renderer = "renderer";
-  const char *version = "version";
+  VKContext *context = VKContext::get();
+  VkPhysicalDeviceProperties physical_device_properties;
+  VkPhysicalDevice physical_device = context->physical_device_get();
+  vkGetPhysicalDeviceProperties(physical_device, &physical_device_properties);
+  const VendorID vendor_id = vendor_id_get(physical_device_properties);
+  const std::string vendor = vendor_name_get(vendor_id);
+  const std::string renderer = physical_device_properties.deviceName;
+  const std::string version = api_version_get(physical_device_properties).c_str();
   eGPUDeviceType device = GPU_DEVICE_ANY;
   eGPUOSType os = GPU_OS_ANY;
   eGPUDriverType driver = GPU_DRIVER_ANY;
@@ -54,7 +98,18 @@ void VKBackend::platform_init(void)
   os = GPU_OS_UNIX;
 #endif
 
-  GPG.init(device, os, driver, support_level, vendor, renderer, version);
+  /* TODO(jbakker): extract the driver type. */
+  if (vendor_id == VendorID::AMD) {
+    device = GPU_DEVICE_ATI;
+  }
+  else if (vendor_id == VendorID::Intel) {
+    device = GPU_DEVICE_INTEL;
+  }
+  else if (vendor_id == VendorID::NVIDIA) {
+    device = GPU_DEVICE_NVIDIA;
+  }
+
+  GPG.init(device, os, driver, support_level, vendor.c_str(), renderer.c_str(), version.c_str());
 }
 
 void VKBackend::platform_exit(void)
diff --git a/source/blender/gpu/vulkan/vk_backend.hh b/source/blender/gpu/vulkan/vk_backend.hh
index a662d9ed782..d189f7158e7 100644
--- a/source/blender/gpu/vulkan/vk_backend.hh
+++ b/source/blender/gpu/vulkan/vk_backend.hh
@@ -41,17 +41,18 @@ namespace gpu {
 
 class VKBackend : public GPUBackend {
  public:
-  VKBackend()
+  ~VKBackend()
+  {
+    VKBackend::platform_exit();
+  }
+
+  void init() override
   {
     /* platform_init needs to go first. */
     VKBackend::platform_init();
 
     VKBackend::capabilities_init();
   }
-  ~VKBackend()
-  {
-    VKBackend::platform_exit();
-  }
 
   static VKBackend *get(void)
   {
diff --git a/source/blender/gpu/vulkan/vk_context.hh b/source/blender/gpu/vulkan/vk_context.hh
index a59faceb351..0a78ef356c6 100644
--- a/source/blender/gpu/vulkan/vk_context.hh
+++ b/source/blender/gpu/vulkan/vk_context.hh
@@ -78,6 +78,11 @@ class VKContext : public Context {
     return device_;
   }
 
+  VkPhysicalDevice physical_device_get() const
+  {
+    return physical_device_;
+  }
+
   VmaAllocator mem_allocator_get(void) const
   {
     return mem_allocator_;



More information about the Bf-blender-cvs mailing list