Files
XCEngine/docs/api/rhi/shader/compile-from-file.md
2026-03-20 02:35:45 +08:00

40 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# RHIShader::CompileFromFile
```cpp
virtual bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) = 0;
```
从文件加载并编译着色器。
从指定的文件路径读取着色器代码并编译为 GPU 可执行的着色器程序。该方法支持各种着色器目标平台(如 D3D12 的 `vs_5_0``ps_5_0` 等,或 Vulkan 的 SPIR-V
**参数:**
- `filePath` - 着色器文件路径(宽字符字符串,支持 Unicode 路径)
- `entryPoint` - 着色器入口点函数名称(如 `"VSMain"``"PSMain"`
- `target` - 着色器目标配置文件(如 `"vs_5_0"``"ps_5_0"``"spv"`
**返回:** 编译成功返回 `true`,失败返回 `false`
**线程安全:** ❌(多线程同时编译同一着色器可能导致未定义行为)
**复杂度:** O(n),其中 n 为着色器代码行数
**示例:**
```cpp
RHIShader* vertexShader = device->CreateShader();
if (vertexShader->CompileFromFile(L"shaders/vertex.cso", "VSMain", "vs_5_0")) {
vertexShader->Bind();
// 使用着色器...
} else {
printf("Vertex shader compilation failed!\n");
}
vertexShader->Shutdown();
device->DestroyShader(vertexShader);
```
## 相关文档
- [RHIShader](shader.md) - 返回类总览
- [`Compile`](compile.md) - 从内存数据编译着色器