Files
XCEngine/docs/api/rhi/rhi-shader.md

3.2 KiB

RHIShader

命名空间: XCEngine::RHI

类型: class (abstract)

描述: GPU 着色器资源抽象接口,用于管理着色器代码的编译和绑定。

概述

RHIShader 封装了着色器的编译、参数设置和绑定操作。着色器是 GPU 可编程管线的核心组件。

公共方法

生命周期

方法 描述
virtual void Shutdown() 释放着色器资源

编译

方法 描述
virtual bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) 从文件编译着色器
virtual bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) 从内存编译着色器

属性访问

方法 描述
virtual ShaderType GetType() const 获取着色器类型
virtual bool IsValid() const 检查着色器是否有效

绑定/解绑

方法 描述
virtual void Bind() 绑定着色器到管线
virtual void Unbind() 解绑着色器

Uniform 设置

方法 描述
virtual void SetInt(const char* name, int value) 设置整数 uniform
virtual void SetFloat(const char* name, float value) 设置浮点 uniform
virtual void SetVec3(const char* name, float x, float y, float z) 设置三维向量 uniform
virtual void SetVec4(const char* name, float x, float y, float z, float w) 设置四维向量 uniform
virtual void SetMat4(const char* name, const float* value) 设置 4x4 矩阵 uniform

其他

方法 描述
virtual void* GetNativeHandle() 获取原生 API 句柄

着色器类型 (ShaderType)

枚举值 描述
Vertex 顶点着色器
Fragment 片元着色器
Geometry 几何着色器
Compute 计算着色器
TessControl 曲面细分控制着色器
TessEvaluation 曲面细分评估着色器
Hull Hull 着色器 (D3D)
Domain Domain 着色器 (D3D)
Amplification 放大着色器 (D3D12.9+)
Mesh Mesh 着色器 (D3D12.9+)
Library 着色器库

编译目标格式

API 目标格式
D3D11 vs_5_0, ps_5_0, gs_5_0, cs_5_0
D3D12 vs_6_0, ps_6_0, gs_6_0, cs_6_0, ds_6_0, hs_6_0
OpenGL GLSL (GLSL 源码)

使用示例

// 编译顶点着色器
RHIShader* vs = device->CompileShader({});
vs->CompileFromFile(L"shaders/vertex.hlsl", "main", "vs_6_0");

// 编译片元着色器
RHIShader* ps = device->CompileShader({});
ps->CompileFromFile(L"shaders/fragment.hlsl", "main", "ps_6_0");

// 设置 uniform
vs->SetMat4("modelMatrix", modelMatrix);
vs->SetMat4("viewMatrix", viewMatrix);
vs->SetMat4("projectionMatrix", projectionMatrix);

ps->SetVec3("lightDir", 0.5f, 0.8f, 0.3f);
ps->SetFloat("roughness", 0.5f);
ps->SetInt("albedoMap", 0);

// 绑定着色器
vs->Bind();
ps->Bind();

// 使用完毕后关闭
vs->Shutdown();
ps->Shutdown();

相关文档