74 lines
1.7 KiB
Markdown
74 lines
1.7 KiB
Markdown
# OpenGLDevice::CompileShader
|
||
|
||
```cpp
|
||
RHIShader* CompileShader(const ShaderCompileDesc& desc) override
|
||
```
|
||
|
||
编译着色器程序。
|
||
|
||
## 详细描述
|
||
|
||
从文件加载并编译着色器代码。
|
||
|
||
### 支持的着色器类型
|
||
|
||
通过 `profile` 参数指定:
|
||
- `"vs"` / `"vert"` - 顶点着色器
|
||
- `"fs"` / `"frag"` - 片段着色器
|
||
- `"gs"` - 几何着色器
|
||
- `"cs"` / `"compute"` - 计算着色器
|
||
- `"ts"` / `"tess"` - 曲面细分着色器
|
||
|
||
### 着色器编译流程
|
||
|
||
1. 从文件读取着色器源码
|
||
2. 根据 `profile` 选择着色器类型
|
||
3. 编译着色器源码
|
||
4. 链接程序(如果需要)
|
||
|
||
## 参数
|
||
|
||
- `desc` - 着色器编译描述符
|
||
|
||
### ShaderCompileDesc 字段
|
||
|
||
| 字段 | 描述 |
|
||
|------|------|
|
||
| `fileName` | 着色器文件路径(宽字符) |
|
||
| `entryPoint` | 入口点名称(默认 "main") |
|
||
| `profile` | 着色器配置文件(如 "vs", "fs") |
|
||
|
||
## 返回值
|
||
|
||
`RHIShader*` - 创建的着色器指针,编译失败返回包含错误的着色器对象
|
||
|
||
## 注意事项
|
||
|
||
- `fileName` 使用宽字符 (`std::wstring`)
|
||
- `entryPoint` 和 `profile` 从宽字符转换为窄字符
|
||
- 如果 `fileName` 为空,不执行编译但仍返回着色器对象
|
||
- 返回的着色器对象归调用者所有,需自行管理生命周期
|
||
|
||
## 示例
|
||
|
||
```cpp
|
||
ShaderCompileDesc vsDesc;
|
||
vsDesc.fileName = L"shaders/triangle.vert";
|
||
vsDesc.entryPoint = L"main";
|
||
vsDesc.profile = L"vs";
|
||
|
||
RHIShader* vertexShader = device.CompileShader(vsDesc);
|
||
|
||
ShaderCompileDesc fsDesc;
|
||
fsDesc.fileName = L"shaders/triangle.frag";
|
||
fsDesc.entryPoint = L"main";
|
||
fsDesc.profile = L"fs";
|
||
|
||
RHIShader* fragmentShader = device.CompileShader(fsDesc);
|
||
```
|
||
|
||
## 相关文档
|
||
|
||
- [OpenGLDevice](device.md) - 类总览
|
||
- [OpenGLShader](../opengl-shader.md) - OpenGL 着色器实现
|