Files
XCEngine/docs/api/rhi/factory/create-rhi-device-string.md

60 lines
1.5 KiB
Markdown
Raw Normal View History

2026-03-20 02:35:45 +08:00
# RHIFactory::CreateRHIDevice
```cpp
static RHIDevice* CreateRHIDevice(const std::string& typeName);
```
2026-03-20 02:35:45 +08:00
根据指定的 RHI 类型名称创建一个新的 RHI 设备实例。
**参数:**
2026-03-20 02:35:45 +08:00
- `typeName` - RHI 类型的字符串名称,支持的值包括:
- `"D3D12"` - DirectX 12
- `"OpenGL"` - OpenGL
- `"Vulkan"` - Vulkan
- `"Metal"` - Metal
2026-03-20 02:35:45 +08:00
**返回:** 新创建的 RHIDevice 指针,如果创建失败则返回 nullptr。
2026-03-20 02:35:45 +08:00
**异常:** 无
2026-03-20 02:35:45 +08:00
**线程安全:** ❌ 该方法本身是线程安全的,但返回的 RHIDevice 实例通常不是线程安全的。
**复杂度:** O(n),其中 n 为 typeName 字符串长度
**示例:**
```cpp
2026-03-20 02:35:45 +08:00
#include "XCEngine/RHI/RHIFactory.h"
#include "XCEngine/RHI/RHIDevice.h"
#include <iostream>
using namespace XCEngine::RHI;
int main() {
RHIDevice* device = RHIFactory::CreateRHIDevice("Vulkan");
if (!device) {
std::cerr << "Failed to create Vulkan device" << std::endl;
return 1;
}
RHIDeviceDesc desc;
if (!device->Initialize(desc)) {
std::cerr << "Failed to initialize device" << std::endl;
delete device;
return 1;
}
const RHIDeviceInfo& info = device->GetDeviceInfo();
std::cout << "Device created: " << info.deviceName << std::endl;
device->Shutdown();
delete device;
return 0;
}
```
## 相关文档
2026-03-20 02:35:45 +08:00
- [RHIFactory](factory.md) - 返回类总览
- [`CreateRHIDevice(RHIType)`](create-rhi-device-type.md) - 根据枚举类型创建设备