feat(RHI): 实现 RHIBuffer, RHITexture, RHIShader 抽象基类
- 新增 RHIBuffer, RHITexture, RHIShader 抽象基类 - D3D12Buffer/Texture/Shader 继承抽象基类 - OpenGLBuffer/Texture/Shader 继承抽象基类 - 添加 RHICapabilities, RHIDevice 头文件 - RHIEnums 添加 Fragment/TessControl/TessEvaluation - 文档更新差异处理策略
This commit is contained in:
@@ -253,7 +253,245 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
## 5. 核心差异处理方案
|
## 5. 核心差异处理方案
|
||||||
### 5.1 根签名 vs OpenGL 资源绑定
|
|
||||||
|
### 5.1 缓冲区(RHIBuffer)抽象设计
|
||||||
|
|
||||||
|
#### 5.1.1 现有实现对比
|
||||||
|
|
||||||
|
| 功能 | D3D12Buffer | OpenGLBuffer | 处理方案 |
|
||||||
|
|------|-------------|--------------|----------|
|
||||||
|
| 初始化参数 | 需要 device + heapType | 不需要 | 后端各自实现初始化逻辑 |
|
||||||
|
| 句柄类型 | `ID3D12Resource*` | `GLuint` | 统一返回 `void*`,底层逃逸 |
|
||||||
|
| Map/Unmap | `Map()` / `Unmap()` | `glMapBuffer` | 后端各自实现 |
|
||||||
|
| 上传数据 | `UpdateData()` | `SetData()` | 统一为 `SetData()` |
|
||||||
|
| 获取大小 | `GetSize()` | `GetSize()` | 通用 |
|
||||||
|
| Vertex Stride | 显式存储 `m_stride` | VAO 管理 | 基类提供接口 |
|
||||||
|
| 资源状态 | `ResourceStates` 显式管理 | 无 | D3D12 维护,OpenGL 透明 |
|
||||||
|
|
||||||
|
#### 5.1.2 抽象接口定义
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// engine/include/XCEngine/RHI/RHIBuffer.h
|
||||||
|
class RHIBuffer {
|
||||||
|
public:
|
||||||
|
virtual ~RHIBuffer() = default;
|
||||||
|
|
||||||
|
// 数据操作
|
||||||
|
virtual void* Map() = 0;
|
||||||
|
virtual void Unmap() = 0;
|
||||||
|
virtual void SetData(const void* data, size_t size, size_t offset = 0) = 0;
|
||||||
|
|
||||||
|
// 属性查询
|
||||||
|
virtual uint64_t GetSize() const = 0;
|
||||||
|
virtual BufferType GetBufferType() const = 0;
|
||||||
|
virtual void SetBufferType(BufferType type) = 0;
|
||||||
|
virtual uint32_t GetStride() const = 0;
|
||||||
|
virtual void SetStride(uint32_t stride) = 0;
|
||||||
|
|
||||||
|
// 底层逃逸
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
// 调试
|
||||||
|
virtual const std::string& GetName() const = 0;
|
||||||
|
virtual void SetName(const std::string& name) = 0;
|
||||||
|
|
||||||
|
// 生命周期
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.1.3 差异处理策略
|
||||||
|
|
||||||
|
1. **初始化参数差异**
|
||||||
|
- D3D12:在 `CreateBuffer()` 时由 Device 传入 device 参数,内部调用 `Initialize(device, size, ...)`
|
||||||
|
- OpenGL:直接调用 `Initialize(type, size, data, dynamic)`,不需要 device
|
||||||
|
|
||||||
|
2. **句柄类型差异**
|
||||||
|
- `GetNativeHandle()` 返回 `void*`
|
||||||
|
- D3D12 后端:`return m_resource.Get();`
|
||||||
|
- OpenGL 后端:`return (void*)(uintptr_t)m_buffer;`
|
||||||
|
|
||||||
|
3. **Vertex Stride**
|
||||||
|
- D3D12:显式存储在 `m_stride`
|
||||||
|
- OpenGL:在 VertexArray 中管理,可在基类中提供接口
|
||||||
|
|
||||||
|
4. **Device 创建接口**
|
||||||
|
```cpp
|
||||||
|
// RHIDevice 中
|
||||||
|
virtual RHIBuffer* CreateBuffer(const BufferDesc& desc) = 0;
|
||||||
|
|
||||||
|
// D3D12Device 实现
|
||||||
|
RHIBuffer* CreateBuffer(const BufferDesc& desc) override {
|
||||||
|
auto* buffer = new D3D12Buffer();
|
||||||
|
buffer->Initialize(m_device.Get(), desc.size, ...);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenGLDevice 实现
|
||||||
|
RHIBuffer* CreateBuffer(const BufferDesc& desc) override {
|
||||||
|
auto* buffer = new OpenGLBuffer();
|
||||||
|
buffer->Initialize(glType, desc.size, ...);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 纹理(RHITexture)抽象设计
|
||||||
|
|
||||||
|
#### 5.2.1 设计理念对应
|
||||||
|
|
||||||
|
| 差异点 | 设计理念 | 处理方案 |
|
||||||
|
|--------|---------|---------|
|
||||||
|
| 初始化参数差异 | 求同存异 | 后端各自实现初始化,基类定义统一创建接口 |
|
||||||
|
| 句柄类型差异 | 底层逃逸 | 统一返回 void*,通过 GetNativeHandle() 获取原生类型 |
|
||||||
|
| 资源状态管理 | 特性降级 | D3D12 显式管理 ResourceStates,OpenGL 无需管理(透明) |
|
||||||
|
| 格式枚举差异 | 求同存异 | 基类统一 Format 枚举,后端映射到各自格式 |
|
||||||
|
|
||||||
|
#### 5.2.2 现有实现对比
|
||||||
|
|
||||||
|
| 功能 | D3D12Texture | OpenGLTexture | 处理方案 |
|
||||||
|
|------|--------------|---------------|----------|
|
||||||
|
| 初始化参数 | device + resource desc | type + dimensions + format | 后端各自实现 |
|
||||||
|
| 句柄类型 | `ID3D12Resource*` | `GLuint` | 统一返回 `void*` |
|
||||||
|
| 尺寸获取 | GetWidth/Height/Depth | 同上 | 统一接口 |
|
||||||
|
| Mip Levels | GetMipLevels() | GetMipLevels() | 通用 |
|
||||||
|
| 格式 | DXGI_FORMAT | OpenGLFormat | 基类统一 Format |
|
||||||
|
| 纹理类型 | TextureType enum | OpenGLTextureType enum | 基类统一 TextureType |
|
||||||
|
| 绑定 | 通过 DescriptorHeap | glBindTexture | 后端实现 Bind() |
|
||||||
|
| Mipmap 生成 | 需手动计算 | glGenerateMipmap | 后端实现 |
|
||||||
|
| 资源状态 | ResourceStates | 无 | D3D12 维护,OpenGL 透明 |
|
||||||
|
| 深度纹理 | InitializeDepthStencil() | 普通纹理 | 后端实现 |
|
||||||
|
|
||||||
|
#### 5.2.3 抽象接口定义
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
class RHITexture {
|
||||||
|
public:
|
||||||
|
virtual ~RHITexture() = default;
|
||||||
|
|
||||||
|
// 属性(求同存异)
|
||||||
|
virtual uint32_t GetWidth() const = 0;
|
||||||
|
virtual uint32_t GetHeight() const = 0;
|
||||||
|
virtual uint32_t GetDepth() const = 0;
|
||||||
|
virtual uint32_t GetMipLevels() const = 0;
|
||||||
|
virtual Format GetFormat() const = 0;
|
||||||
|
virtual TextureType GetTextureType() const = 0;
|
||||||
|
|
||||||
|
// 资源状态(特性降级)
|
||||||
|
virtual ResourceStates GetState() const = 0;
|
||||||
|
virtual void SetState(ResourceStates state) = 0;
|
||||||
|
|
||||||
|
// 底层逃逸
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
// 调试
|
||||||
|
virtual const std::string& GetName() const = 0;
|
||||||
|
virtual void SetName(const std::string& name) = 0;
|
||||||
|
|
||||||
|
// 生命周期
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.2.4 差异处理策略
|
||||||
|
|
||||||
|
1. **初始化参数差异(求同存异)**
|
||||||
|
- D3D12:需要 device + resource desc
|
||||||
|
- OpenGL:需要 type + dimensions + format
|
||||||
|
- 解决:Device 的 CreateTexture() 统一接收 TextureDesc,后端各自实现 Initialize()
|
||||||
|
|
||||||
|
2. **句柄类型差异(底层逃逸)**
|
||||||
|
- `GetNativeHandle()` 返回 `void*`
|
||||||
|
- D3D12:`return m_resource.Get();`
|
||||||
|
- OpenGL:`return reinterpret_cast<void*>(static_cast<uintptr_t>(m_texture));`
|
||||||
|
|
||||||
|
3. **资源状态管理(特性降级)**
|
||||||
|
- D3D12:显式 ResourceStates 管理,需要转换 barrier
|
||||||
|
- OpenGL:无资源状态概念
|
||||||
|
- 解决:基类提供 GetState/SetState,D3D12 实现具体逻辑,OpenGL 空实现
|
||||||
|
|
||||||
|
4. **格式枚举差异(求同存异)**
|
||||||
|
- D3D12:DXGI_FORMAT
|
||||||
|
- OpenGL:OpenGLFormat
|
||||||
|
- 解决:基类统一使用 Format 枚举,后端内部映射
|
||||||
|
|
||||||
|
### 5.7 着色器(RHIShader)抽象设计
|
||||||
|
|
||||||
|
#### 5.7.1 设计理念对应
|
||||||
|
|
||||||
|
| 差异点 | 设计理念 | 处理方案 |
|
||||||
|
|--------|---------|---------|
|
||||||
|
| 编译方式差异 | 求同存异 | 统一 Compile 接口,后端各自实现 |
|
||||||
|
| 句柄类型差异 | 底层逃逸 | 统一返回 void* |
|
||||||
|
| Uniform 设置 | 特性降级 | OpenGL 实现,D3D12 空实现 |
|
||||||
|
| 绑定方式 | 求同存异 | 基类提供 Bind/Unbind,后端实现 |
|
||||||
|
|
||||||
|
#### 5.7.2 现有实现对比
|
||||||
|
|
||||||
|
| 功能 | D3D12Shader | OpenGLShader | 处理方案 |
|
||||||
|
|------|-------------|--------------|----------|
|
||||||
|
| 编译 | CompileFromFile/Compile | 同上 | 统一接口 |
|
||||||
|
| 字节码/Program | ID3DBlob | GLuint | 统一 void* |
|
||||||
|
| 类型 | ShaderType | 自有 enum | 基类统一 |
|
||||||
|
| Uniform 设置 | 无 | SetInt/SetFloat... | OpenGL 实现,D3D12 空 |
|
||||||
|
| 绑定 | 通过 CommandList | Use()/Bind() | 基类提供 Bind/Unbind |
|
||||||
|
|
||||||
|
#### 5.7.3 抽象接口定义
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
class RHIShader {
|
||||||
|
public:
|
||||||
|
virtual ~RHIShader() = default;
|
||||||
|
|
||||||
|
// 编译(求同存异)
|
||||||
|
virtual bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) = 0;
|
||||||
|
virtual bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) = 0;
|
||||||
|
|
||||||
|
// 属性
|
||||||
|
virtual ShaderType GetType() const = 0;
|
||||||
|
virtual bool IsValid() const = 0;
|
||||||
|
|
||||||
|
// 绑定(求同存异)
|
||||||
|
virtual void Bind() = 0;
|
||||||
|
virtual void Unbind() = 0;
|
||||||
|
|
||||||
|
// 底层逃逸
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
// Uniform 设置(特性降级)
|
||||||
|
virtual void SetInt(const char* name, int value) = 0;
|
||||||
|
virtual void SetFloat(const char* name, float value) = 0;
|
||||||
|
virtual void SetVec3(const char* name, float x, float y, float z) = 0;
|
||||||
|
virtual void SetVec4(const char* name, float x, float y, float z, float w) = 0;
|
||||||
|
virtual void SetMat4(const char* name, const float* value) = 0;
|
||||||
|
|
||||||
|
// 生命周期
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5.7.4 差异处理策略
|
||||||
|
|
||||||
|
1. **编译方式差异(求同存异)**
|
||||||
|
- D3D12:需要 entry point + target(如 "vs_6_0")
|
||||||
|
- OpenGL:根据 source 类型自动判断
|
||||||
|
- 解决:统一 Compile 接口,后端各自处理
|
||||||
|
|
||||||
|
2. **句柄类型差异(底层逃逸)**
|
||||||
|
- D3D12:ID3DBlob
|
||||||
|
- OpenGL:GLuint (program)
|
||||||
|
- 解决:GetNativeHandle() 返回 void*
|
||||||
|
|
||||||
|
3. **Uniform 设置(特性降级)**
|
||||||
|
- D3D12:通过 RootSignature/CBV 设置,不在 Shader 类
|
||||||
|
- OpenGL:通过 glUniform* 设置
|
||||||
|
- 解决:D3D12 实现为空,OpenGL 实现具体逻辑
|
||||||
|
|
||||||
|
4. **绑定方式**
|
||||||
|
- D3D12:通过 CommandList->SetPipelineState
|
||||||
|
- OpenGL:glUseProgram
|
||||||
|
- 解决:基类提供 Bind/Unbind 接口
|
||||||
|
|
||||||
|
### 5.8 根签名 vs OpenGL 资源绑定
|
||||||
- **D3D12**:显式 `RootSignature` 定义资源绑定规则
|
- **D3D12**:显式 `RootSignature` 定义资源绑定规则
|
||||||
- **OpenGL**:隐式通过 `glUniformLocation`、`glBindTextureUnit` 绑定
|
- **OpenGL**:隐式通过 `glUniformLocation`、`glBindTextureUnit` 绑定
|
||||||
- **解决方案**:
|
- **解决方案**:
|
||||||
@@ -261,7 +499,7 @@ public:
|
|||||||
- D3D12 后端:内部创建 `ID3D12RootSignature`
|
- D3D12 后端:内部创建 `ID3D12RootSignature`
|
||||||
- OpenGL 后端:存储绑定点数量,绘制时自动调用 `glBind*`
|
- OpenGL 后端:存储绑定点数量,绘制时自动调用 `glBind*`
|
||||||
|
|
||||||
### 5.2 描述符堆 vs OpenGL 纹理单元
|
### 5.4 描述符堆 vs OpenGL 纹理单元
|
||||||
- **D3D12**:显式 `DescriptorHeap` 管理资源视图
|
- **D3D12**:显式 `DescriptorHeap` 管理资源视图
|
||||||
- **OpenGL**:隐式通过 `glActiveTexture` 绑定
|
- **OpenGL**:隐式通过 `glActiveTexture` 绑定
|
||||||
- **解决方案**:
|
- **解决方案**:
|
||||||
@@ -270,7 +508,7 @@ public:
|
|||||||
- D3D12 后端:从堆分配槽位,更新描述符
|
- D3D12 后端:从堆分配槽位,更新描述符
|
||||||
- OpenGL 后端:维护绑定点计数器,绘制时自动绑定
|
- OpenGL 后端:维护绑定点计数器,绘制时自动绑定
|
||||||
|
|
||||||
### 5.3 多线程命令录制
|
### 5.5 多线程命令录制
|
||||||
- **D3D12**:原生支持多线程录制不同 `CommandList`
|
- **D3D12**:原生支持多线程录制不同 `CommandList`
|
||||||
- **OpenGL**:状态机模型,单线程上下文
|
- **OpenGL**:状态机模型,单线程上下文
|
||||||
- **解决方案**:
|
- **解决方案**:
|
||||||
@@ -278,7 +516,7 @@ public:
|
|||||||
- D3D12 后端:真·并行提交
|
- D3D12 后端:真·并行提交
|
||||||
- OpenGL 后端:命令缓冲队列,单线程回放
|
- OpenGL 后端:命令缓冲队列,单线程回放
|
||||||
|
|
||||||
### 5.4 高级特性(光线追踪等)
|
### 5.6 高级特性(光线追踪等)
|
||||||
- **解决方案**:
|
- **解决方案**:
|
||||||
1. **特性检测**:通过 `RHICapabilities` 查询支持情况
|
1. **特性检测**:通过 `RHICapabilities` 查询支持情况
|
||||||
2. **降级方案**:不支持时用传统路径替代
|
2. **降级方案**:不支持时用传统路径替代
|
||||||
|
|||||||
2448
docs/plan/第四阶段计划_资源系统.md
Normal file
2448
docs/plan/第四阶段计划_资源系统.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@
|
|||||||
#include <wrl/client.h>
|
#include <wrl/client.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "../RHIBuffer.h"
|
||||||
#include "D3D12Enum.h"
|
#include "D3D12Enum.h"
|
||||||
|
|
||||||
using Microsoft::WRL::ComPtr;
|
using Microsoft::WRL::ComPtr;
|
||||||
@@ -11,16 +12,16 @@ using Microsoft::WRL::ComPtr;
|
|||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
class D3D12Buffer {
|
class D3D12Buffer : public RHIBuffer {
|
||||||
public:
|
public:
|
||||||
D3D12Buffer();
|
D3D12Buffer();
|
||||||
~D3D12Buffer();
|
~D3D12Buffer() override;
|
||||||
|
|
||||||
bool Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_TYPE heapType = D3D12_HEAP_TYPE_DEFAULT);
|
bool Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_TYPE heapType = D3D12_HEAP_TYPE_DEFAULT);
|
||||||
bool InitializeFromExisting(ID3D12Resource* resource);
|
bool InitializeFromExisting(ID3D12Resource* resource);
|
||||||
bool InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
bool InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
||||||
const void* data, uint64_t size, D3D12_RESOURCE_STATES finalState);
|
const void* data, uint64_t size, D3D12_RESOURCE_STATES finalState);
|
||||||
void Shutdown();
|
void Shutdown() override;
|
||||||
|
|
||||||
ID3D12Resource* GetResource() const { return m_resource.Get(); }
|
ID3D12Resource* GetResource() const { return m_resource.Get(); }
|
||||||
D3D12_RESOURCE_DESC GetDesc() const { return m_resource->GetDesc(); }
|
D3D12_RESOURCE_DESC GetDesc() const { return m_resource->GetDesc(); }
|
||||||
@@ -29,21 +30,25 @@ public:
|
|||||||
|
|
||||||
void UpdateData(const void* data, uint64_t size);
|
void UpdateData(const void* data, uint64_t size);
|
||||||
|
|
||||||
void* GetNativeHandle() const { return m_resource.Get(); }
|
void* GetNativeHandle() override { return m_resource.Get(); }
|
||||||
ResourceStates GetState() const { return m_state; }
|
ResourceStates GetState() const { return m_state; }
|
||||||
void SetState(ResourceStates state) { m_state = state; }
|
void SetState(ResourceStates state) { m_state = state; }
|
||||||
|
|
||||||
uint64_t GetGPUAddress() const { return m_resource->GetGPUVirtualAddress(); }
|
uint64_t GetGPUAddress() const { return m_resource->GetGPUVirtualAddress(); }
|
||||||
size_t GetSize() const { return GetDesc().Width; }
|
uint64_t GetSize() const override { return GetDesc().Width; }
|
||||||
|
|
||||||
const std::string& GetName() const { return m_name; }
|
const std::string& GetName() const override { return m_name; }
|
||||||
void SetName(const std::string& name) { m_name = name; }
|
void SetName(const std::string& name) override { m_name = name; }
|
||||||
|
|
||||||
uint32_t GetStride() const { return m_stride; }
|
uint32_t GetStride() const override { return m_stride; }
|
||||||
BufferType GetBufferType() const { return m_bufferType; }
|
BufferType GetBufferType() const override { return m_bufferType; }
|
||||||
|
|
||||||
void SetStride(uint32_t stride) { m_stride = stride; }
|
void SetStride(uint32_t stride) override { m_stride = stride; }
|
||||||
void SetBufferType(BufferType type) { m_bufferType = type; }
|
void SetBufferType(BufferType type) override { m_bufferType = type; }
|
||||||
|
|
||||||
|
void* Map() override;
|
||||||
|
void Unmap() override;
|
||||||
|
void SetData(const void* data, size_t size, size_t offset = 0) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ComPtr<ID3D12Resource> m_resource;
|
ComPtr<ID3D12Resource> m_resource;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <wrl/client.h>
|
#include <wrl/client.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "../RHIShader.h"
|
||||||
#include "D3D12Enum.h"
|
#include "D3D12Enum.h"
|
||||||
#include "../RHITypes.h"
|
#include "../RHITypes.h"
|
||||||
|
|
||||||
@@ -13,21 +14,33 @@ using Microsoft::WRL::ComPtr;
|
|||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
class D3D12Shader {
|
class D3D12Shader : public RHIShader {
|
||||||
public:
|
public:
|
||||||
D3D12Shader();
|
D3D12Shader();
|
||||||
~D3D12Shader();
|
~D3D12Shader() override;
|
||||||
|
|
||||||
bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target);
|
bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) override;
|
||||||
bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target);
|
bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) override;
|
||||||
void Shutdown();
|
void Shutdown() override;
|
||||||
|
|
||||||
const D3D12_SHADER_BYTECODE GetD3D12Bytecode() const;
|
const D3D12_SHADER_BYTECODE GetD3D12Bytecode() const;
|
||||||
const void* GetBytecode() const;
|
const void* GetBytecode() const;
|
||||||
size_t GetBytecodeSize() const;
|
size_t GetBytecodeSize() const;
|
||||||
ShaderType GetType() const;
|
ShaderType GetType() const override;
|
||||||
const InputLayoutDesc& GetInputLayout() const;
|
const InputLayoutDesc& GetInputLayout() const;
|
||||||
|
|
||||||
|
void* GetNativeHandle() override { return m_bytecode.Get(); }
|
||||||
|
bool IsValid() const override { return m_bytecode != nullptr; }
|
||||||
|
|
||||||
|
void Bind() override { }
|
||||||
|
void Unbind() override { }
|
||||||
|
|
||||||
|
void SetInt(const char* name, int value) override { }
|
||||||
|
void SetFloat(const char* name, float value) override { }
|
||||||
|
void SetVec3(const char* name, float x, float y, float z) override { }
|
||||||
|
void SetVec4(const char* name, float x, float y, float z, float w) override { }
|
||||||
|
void SetMat4(const char* name, const float* value) override { }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ComPtr<ID3DBlob> m_bytecode;
|
ComPtr<ID3DBlob> m_bytecode;
|
||||||
ComPtr<ID3DBlob> m_error;
|
ComPtr<ID3DBlob> m_error;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <wrl/client.h>
|
#include <wrl/client.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "../RHITexture.h"
|
||||||
#include "D3D12Enum.h"
|
#include "D3D12Enum.h"
|
||||||
|
|
||||||
using Microsoft::WRL::ComPtr;
|
using Microsoft::WRL::ComPtr;
|
||||||
@@ -11,39 +12,39 @@ using Microsoft::WRL::ComPtr;
|
|||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
class D3D12Texture {
|
class D3D12Texture : public RHITexture {
|
||||||
public:
|
public:
|
||||||
D3D12Texture();
|
D3D12Texture();
|
||||||
~D3D12Texture();
|
~D3D12Texture() override;
|
||||||
|
|
||||||
bool Initialize(ID3D12Device* device, const D3D12_RESOURCE_DESC& desc, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON);
|
bool Initialize(ID3D12Device* device, const D3D12_RESOURCE_DESC& desc, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON);
|
||||||
bool InitializeFromExisting(ID3D12Resource* resource);
|
bool InitializeFromExisting(ID3D12Resource* resource);
|
||||||
bool InitializeFromData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
bool InitializeFromData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
||||||
const void* pixelData, uint32_t width, uint32_t height, DXGI_FORMAT format);
|
const void* pixelData, uint32_t width, uint32_t height, DXGI_FORMAT format);
|
||||||
bool InitializeDepthStencil(ID3D12Device* device, uint32_t width, uint32_t height, DXGI_FORMAT format = DXGI_FORMAT_D24_UNORM_S8_UINT);
|
bool InitializeDepthStencil(ID3D12Device* device, uint32_t width, uint32_t height, DXGI_FORMAT format = DXGI_FORMAT_D24_UNORM_S8_UINT);
|
||||||
void Shutdown();
|
void Shutdown() override;
|
||||||
|
|
||||||
ID3D12Resource* GetResource() const { return m_resource.Get(); }
|
ID3D12Resource* GetResource() const { return m_resource.Get(); }
|
||||||
D3D12_RESOURCE_DESC GetDesc() const { return m_resource->GetDesc(); }
|
D3D12_RESOURCE_DESC GetDesc() const { return m_resource->GetDesc(); }
|
||||||
|
|
||||||
uint32_t GetWidth() const { return static_cast<uint32_t>(GetDesc().Width); }
|
uint32_t GetWidth() const override { return static_cast<uint32_t>(GetDesc().Width); }
|
||||||
uint32_t GetHeight() const { return GetDesc().Height; }
|
uint32_t GetHeight() const override { return GetDesc().Height; }
|
||||||
uint32_t GetDepth() const { return GetDesc().DepthOrArraySize; }
|
uint32_t GetDepth() const override { return GetDesc().DepthOrArraySize; }
|
||||||
uint32_t GetMipLevels() const { return GetDesc().MipLevels; }
|
uint32_t GetMipLevels() const override { return GetDesc().MipLevels; }
|
||||||
|
|
||||||
void* GetNativeHandle() const { return m_resource.Get(); }
|
void* GetNativeHandle() override { return m_resource.Get(); }
|
||||||
ResourceStates GetState() const { return m_state; }
|
ResourceStates GetState() const override { return m_state; }
|
||||||
void SetState(ResourceStates state) { m_state = state; }
|
void SetState(ResourceStates state) override { m_state = state; }
|
||||||
|
|
||||||
uint64_t GetGPUAddress() const { return m_resource->GetGPUVirtualAddress(); }
|
uint64_t GetGPUAddress() const { return m_resource->GetGPUVirtualAddress(); }
|
||||||
size_t GetSize() const { return GetDesc().Width * GetDesc().Height * GetDesc().DepthOrArraySize; }
|
size_t GetSize() const { return GetDesc().Width * GetDesc().Height * GetDesc().DepthOrArraySize; }
|
||||||
|
|
||||||
const std::string& GetName() const { return m_name; }
|
const std::string& GetName() const override { return m_name; }
|
||||||
void SetName(const std::string& name) { m_name = name; }
|
void SetName(const std::string& name) override { m_name = name; }
|
||||||
|
|
||||||
uint32_t GetArraySize() const { return GetDesc().DepthOrArraySize; }
|
uint32_t GetArraySize() const { return GetDesc().DepthOrArraySize; }
|
||||||
Format GetFormat() const { return static_cast<Format>(GetDesc().Format); }
|
Format GetFormat() const override { return static_cast<Format>(GetDesc().Format); }
|
||||||
TextureType GetTextureType() const { return TextureType::Texture2D; }
|
TextureType GetTextureType() const override { return TextureType::Texture2D; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ComPtr<ID3D12Resource> m_resource;
|
ComPtr<ID3D12Resource> m_resource;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include "../RHIBuffer.h"
|
||||||
|
|
||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
@@ -18,35 +20,49 @@ enum class OpenGLBufferType {
|
|||||||
ShaderBindingTable
|
ShaderBindingTable
|
||||||
};
|
};
|
||||||
|
|
||||||
class OpenGLBuffer {
|
class OpenGLBuffer : public RHIBuffer {
|
||||||
public:
|
public:
|
||||||
OpenGLBuffer();
|
OpenGLBuffer();
|
||||||
~OpenGLBuffer();
|
~OpenGLBuffer() override;
|
||||||
|
|
||||||
bool Initialize(OpenGLBufferType type, size_t size, const void* data = nullptr, bool dynamic = false);
|
bool Initialize(OpenGLBufferType type, size_t size, const void* data = nullptr, bool dynamic = false);
|
||||||
bool InitializeVertexBuffer(const void* data, size_t size);
|
bool InitializeVertexBuffer(const void* data, size_t size);
|
||||||
bool InitializeIndexBuffer(const void* data, size_t size);
|
bool InitializeIndexBuffer(const void* data, size_t size);
|
||||||
void Shutdown();
|
void Shutdown() override;
|
||||||
|
|
||||||
void Bind() const;
|
void Bind() const;
|
||||||
void Unbind() const;
|
void Unbind() const;
|
||||||
void BindBase(unsigned int target, unsigned int index) const;
|
void BindBase(unsigned int target, unsigned int index) const;
|
||||||
|
|
||||||
void* Map();
|
void* Map() override;
|
||||||
void Unmap();
|
void Unmap() override;
|
||||||
void SetData(const void* data, size_t size, size_t offset = 0);
|
void SetData(const void* data, size_t size, size_t offset = 0) override;
|
||||||
|
|
||||||
unsigned int GetID() const { return m_buffer; }
|
unsigned int GetID() const { return m_buffer; }
|
||||||
size_t GetSize() const { return m_size; }
|
uint64_t GetSize() const override { return m_size; }
|
||||||
OpenGLBufferType GetType() const { return m_type; }
|
OpenGLBufferType GetType() const { return m_type; }
|
||||||
bool IsDynamic() const { return m_dynamic; }
|
bool IsDynamic() const { return m_dynamic; }
|
||||||
|
|
||||||
|
BufferType GetBufferType() const override { return m_bufferType; }
|
||||||
|
void SetBufferType(BufferType type) override { m_bufferType = type; }
|
||||||
|
|
||||||
|
uint32_t GetStride() const override { return m_stride; }
|
||||||
|
void SetStride(uint32_t stride) override { m_stride = stride; }
|
||||||
|
|
||||||
|
void* GetNativeHandle() override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_buffer)); }
|
||||||
|
|
||||||
|
const std::string& GetName() const override { return m_name; }
|
||||||
|
void SetName(const std::string& name) override { m_name = name; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int m_buffer;
|
unsigned int m_buffer;
|
||||||
size_t m_size;
|
size_t m_size;
|
||||||
bool m_isIndexBuffer;
|
bool m_isIndexBuffer;
|
||||||
bool m_dynamic;
|
bool m_dynamic;
|
||||||
OpenGLBufferType m_type;
|
OpenGLBufferType m_type;
|
||||||
|
BufferType m_bufferType = BufferType::Vertex;
|
||||||
|
uint32_t m_stride = 0;
|
||||||
|
std::string m_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace RHI
|
} // namespace RHI
|
||||||
|
|||||||
@@ -4,22 +4,19 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../RHIShader.h"
|
||||||
|
|
||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
enum class ShaderType {
|
class OpenGLShader : public RHIShader {
|
||||||
Vertex,
|
|
||||||
Fragment,
|
|
||||||
Geometry,
|
|
||||||
Compute,
|
|
||||||
TessControl,
|
|
||||||
TessEvaluation
|
|
||||||
};
|
|
||||||
|
|
||||||
class OpenGLShader {
|
|
||||||
public:
|
public:
|
||||||
OpenGLShader();
|
OpenGLShader();
|
||||||
~OpenGLShader();
|
~OpenGLShader() override;
|
||||||
|
|
||||||
|
bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) override;
|
||||||
|
bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) override;
|
||||||
|
void Shutdown() override;
|
||||||
|
|
||||||
bool CompileFromFile(const char* vertexPath, const char* fragmentPath);
|
bool CompileFromFile(const char* vertexPath, const char* fragmentPath);
|
||||||
bool CompileFromFile(const char* vertexPath, const char* fragmentPath, const char* geometryPath);
|
bool CompileFromFile(const char* vertexPath, const char* fragmentPath, const char* geometryPath);
|
||||||
@@ -27,33 +24,34 @@ public:
|
|||||||
bool Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource);
|
bool Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource);
|
||||||
bool CompileCompute(const char* computeSource);
|
bool CompileCompute(const char* computeSource);
|
||||||
bool Compile(const char* source, ShaderType type);
|
bool Compile(const char* source, ShaderType type);
|
||||||
void Shutdown();
|
|
||||||
|
|
||||||
void Use() const;
|
void Use() const;
|
||||||
void Bind() const { Use(); }
|
void Bind() override { Use(); }
|
||||||
void Unbind() const;
|
void Unbind() override;
|
||||||
|
|
||||||
void SetInt(const std::string& name, int value) const;
|
void SetInt(const char* name, int value) override;
|
||||||
void SetIntArray(const std::string& name, const int* values, unsigned int count) const;
|
void SetIntArray(const char* name, const int* values, unsigned int count);
|
||||||
void SetFloat(const std::string& name, float value) const;
|
void SetFloat(const char* name, float value) override;
|
||||||
void SetFloatArray(const std::string& name, const float* values, unsigned int count) const;
|
void SetFloatArray(const char* name, const float* values, unsigned int count);
|
||||||
void SetVec2(const std::string& name, float x, float y) const;
|
void SetVec3(const char* name, float x, float y, float z) override;
|
||||||
void SetVec2(const std::string& name, const float* values) const;
|
void SetVec3(const char* name, const float* values);
|
||||||
void SetVec3(const std::string& name, float x, float y, float z) const;
|
void SetVec4(const char* name, float x, float y, float z, float w) override;
|
||||||
void SetVec3(const std::string& name, const float* values) const;
|
void SetVec4(const char* name, const float* values);
|
||||||
void SetVec4(const std::string& name, float x, float y, float z, float w) const;
|
void SetMat2(const char* name, const float* value);
|
||||||
void SetVec4(const std::string& name, const float* values) const;
|
void SetMat3(const char* name, const float* value);
|
||||||
void SetMat2(const std::string& name, const float* value) const;
|
void SetMat4(const char* name, const float* value) override;
|
||||||
void SetMat3(const std::string& name, const float* value) const;
|
void SetMat4Array(const char* name, const float* values, unsigned int count);
|
||||||
void SetMat4(const std::string& name, const float* value) const;
|
|
||||||
void SetMat4Array(const std::string& name, const float* values, unsigned int count) const;
|
|
||||||
|
|
||||||
int GetUniformLocation(const std::string& name) const;
|
int GetUniformLocation(const char* name) const;
|
||||||
unsigned int GetID() const { return m_program; }
|
unsigned int GetID() const { return m_program; }
|
||||||
bool IsValid() const { return m_program != 0; }
|
void* GetNativeHandle() override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_program)); }
|
||||||
|
bool IsValid() const override { return m_program != 0; }
|
||||||
|
|
||||||
|
ShaderType GetType() const override { return m_type; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int m_program;
|
unsigned int m_program;
|
||||||
|
ShaderType m_type = ShaderType::Vertex;
|
||||||
bool CheckCompileErrors(unsigned int shader, const char* type);
|
bool CheckCompileErrors(unsigned int shader, const char* type);
|
||||||
bool CheckLinkErrors(unsigned int program);
|
bool CheckLinkErrors(unsigned int program);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../RHITexture.h"
|
||||||
|
|
||||||
namespace XCEngine {
|
namespace XCEngine {
|
||||||
namespace RHI {
|
namespace RHI {
|
||||||
|
|
||||||
@@ -40,16 +42,16 @@ enum class OpenGLInternalFormat {
|
|||||||
CompressedDXT5 = 22
|
CompressedDXT5 = 22
|
||||||
};
|
};
|
||||||
|
|
||||||
class OpenGLTexture {
|
class OpenGLTexture : public RHITexture {
|
||||||
public:
|
public:
|
||||||
OpenGLTexture();
|
OpenGLTexture();
|
||||||
~OpenGLTexture();
|
~OpenGLTexture() override;
|
||||||
|
|
||||||
bool Initialize(OpenGLTextureType type, int width, int height, int depth, int mipLevels, OpenGLFormat format, const void* data = nullptr);
|
bool Initialize(OpenGLTextureType type, int width, int height, int depth, int mipLevels, OpenGLFormat format, const void* data = nullptr);
|
||||||
bool Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap = true);
|
bool Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap = true);
|
||||||
bool InitializeCubeMap(int size, int mipLevels, OpenGLFormat format, const void* data = nullptr);
|
bool InitializeCubeMap(int size, int mipLevels, OpenGLFormat format, const void* data = nullptr);
|
||||||
bool LoadFromFile(const char* path, bool flipVertical = true);
|
bool LoadFromFile(const char* path, bool flipVertical = true);
|
||||||
void Shutdown();
|
void Shutdown() override;
|
||||||
|
|
||||||
void Bind(int slot = 0) const;
|
void Bind(int slot = 0) const;
|
||||||
void Unbind() const;
|
void Unbind() const;
|
||||||
@@ -60,11 +62,35 @@ public:
|
|||||||
void SetWrapping(int wrapS, int wrapT, int wrapR = -1);
|
void SetWrapping(int wrapS, int wrapT, int wrapR = -1);
|
||||||
|
|
||||||
unsigned int GetID() const { return m_texture; }
|
unsigned int GetID() const { return m_texture; }
|
||||||
OpenGLTextureType GetType() const { return m_type; }
|
OpenGLTextureType GetOpenGLType() const { return m_type; }
|
||||||
int GetWidth() const { return m_width; }
|
|
||||||
int GetHeight() const { return m_height; }
|
uint32_t GetWidth() const override { return static_cast<uint32_t>(m_width); }
|
||||||
int GetDepth() const { return m_depth; }
|
uint32_t GetHeight() const override { return static_cast<uint32_t>(m_height); }
|
||||||
int GetMipLevels() const { return m_mipLevels; }
|
uint32_t GetDepth() const override { return static_cast<uint32_t>(m_depth); }
|
||||||
|
uint32_t GetMipLevels() const override { return static_cast<uint32_t>(m_mipLevels); }
|
||||||
|
|
||||||
|
TextureType GetTextureType() const override {
|
||||||
|
switch (m_type) {
|
||||||
|
case OpenGLTextureType::Texture1D: return TextureType::Texture1D;
|
||||||
|
case OpenGLTextureType::Texture2D: return TextureType::Texture2D;
|
||||||
|
case OpenGLTextureType::Texture2DArray: return TextureType::Texture2DArray;
|
||||||
|
case OpenGLTextureType::Texture3D: return TextureType::Texture3D;
|
||||||
|
case OpenGLTextureType::TextureCube: return TextureType::TextureCube;
|
||||||
|
case OpenGLTextureType::TextureCubeArray: return TextureType::TextureCubeArray;
|
||||||
|
default: return TextureType::Texture2D;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void* GetNativeHandle() override { return reinterpret_cast<void*>(static_cast<uintptr_t>(m_texture)); }
|
||||||
|
|
||||||
|
ResourceStates GetState() const override { return ResourceStates::Common; }
|
||||||
|
void SetState(ResourceStates state) override { }
|
||||||
|
|
||||||
|
const std::string& GetName() const override { return m_name; }
|
||||||
|
void SetName(const std::string& name) override { m_name = name; }
|
||||||
|
|
||||||
|
Format GetFormat() const override { return m_format; }
|
||||||
|
void SetFormat(Format format) { m_format = format; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int m_texture;
|
unsigned int m_texture;
|
||||||
@@ -74,6 +100,8 @@ private:
|
|||||||
int m_depth;
|
int m_depth;
|
||||||
int m_mipLevels;
|
int m_mipLevels;
|
||||||
int m_channels;
|
int m_channels;
|
||||||
|
Format m_format = Format::Unknown;
|
||||||
|
std::string m_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace RHI
|
} // namespace RHI
|
||||||
|
|||||||
33
engine/include/XCEngine/RHI/RHIBuffer.h
Normal file
33
engine/include/XCEngine/RHI/RHIBuffer.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RHITypes.h"
|
||||||
|
#include "RHIEnums.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace XCEngine {
|
||||||
|
namespace RHI {
|
||||||
|
|
||||||
|
class RHIBuffer {
|
||||||
|
public:
|
||||||
|
virtual ~RHIBuffer() = default;
|
||||||
|
|
||||||
|
virtual void* Map() = 0;
|
||||||
|
virtual void Unmap() = 0;
|
||||||
|
virtual void SetData(const void* data, size_t size, size_t offset = 0) = 0;
|
||||||
|
|
||||||
|
virtual uint64_t GetSize() const = 0;
|
||||||
|
virtual BufferType GetBufferType() const = 0;
|
||||||
|
virtual void SetBufferType(BufferType type) = 0;
|
||||||
|
virtual uint32_t GetStride() const = 0;
|
||||||
|
virtual void SetStride(uint32_t stride) = 0;
|
||||||
|
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
virtual const std::string& GetName() const = 0;
|
||||||
|
virtual void SetName(const std::string& name) = 0;
|
||||||
|
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace RHI
|
||||||
|
} // namespace XCEngine
|
||||||
47
engine/include/XCEngine/RHI/RHICapabilities.h
Normal file
47
engine/include/XCEngine/RHI/RHICapabilities.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace XCEngine {
|
||||||
|
namespace RHI {
|
||||||
|
|
||||||
|
struct RHICapabilities {
|
||||||
|
bool bSupportsRayTracing = false;
|
||||||
|
bool bSupportsMeshShaders = false;
|
||||||
|
bool bSupportsExplicitMultiThreading = false;
|
||||||
|
bool bSupportsGeometryShaders = false;
|
||||||
|
bool bSupportsTessellation = false;
|
||||||
|
bool bSupportsComputeShaders = false;
|
||||||
|
bool bSupportsDepthBoundsTest = false;
|
||||||
|
bool bSupportsAlphaToCoverage = false;
|
||||||
|
bool bSupportsIndependentBlend = false;
|
||||||
|
bool bSupportsLogicOps = false;
|
||||||
|
bool bSupportsMultiViewport = false;
|
||||||
|
bool bSupportsConservativeRasterization = false;
|
||||||
|
bool bSupportsProgrammableSamplePositions = false;
|
||||||
|
|
||||||
|
uint32_t maxTexture2DSize = 0;
|
||||||
|
uint32_t maxTexture3DSize = 0;
|
||||||
|
uint32_t maxTextureCubeSize = 0;
|
||||||
|
uint32_t maxRenderTargets = 0;
|
||||||
|
uint32_t maxViewports = 0;
|
||||||
|
uint32_t maxVertexAttribs = 0;
|
||||||
|
uint32_t maxConstantBufferSize = 0;
|
||||||
|
uint32_t maxAnisotropy = 0;
|
||||||
|
uint32_t maxColorAttachments = 0;
|
||||||
|
|
||||||
|
float minSmoothedLineWidth = 1.0f;
|
||||||
|
float maxSmoothedLineWidth = 1.0f;
|
||||||
|
float minPointSize = 1.0f;
|
||||||
|
float maxPointSize = 1.0f;
|
||||||
|
float maxPointSizeAA = 1.0f;
|
||||||
|
float maxLineWidth = 1.0f;
|
||||||
|
float maxLineWidthAA = 1.0f;
|
||||||
|
|
||||||
|
int majorVersion = 0;
|
||||||
|
int minorVersion = 0;
|
||||||
|
std::string shaderModel;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace RHI
|
||||||
|
} // namespace XCEngine
|
||||||
49
engine/include/XCEngine/RHI/RHIDevice.h
Normal file
49
engine/include/XCEngine/RHI/RHIDevice.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RHITypes.h"
|
||||||
|
#include "RHICapabilities.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace XCEngine {
|
||||||
|
namespace RHI {
|
||||||
|
|
||||||
|
class RHIBuffer;
|
||||||
|
class RHITexture;
|
||||||
|
class RHISwapChain;
|
||||||
|
class RHICommandList;
|
||||||
|
class RHICommandQueue;
|
||||||
|
class RHIShader;
|
||||||
|
class RHIPipelineState;
|
||||||
|
class RHIFence;
|
||||||
|
class RHISampler;
|
||||||
|
|
||||||
|
class RHIDevice {
|
||||||
|
public:
|
||||||
|
virtual ~RHIDevice() = default;
|
||||||
|
|
||||||
|
virtual bool Initialize(const RHIDeviceDesc& desc) = 0;
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
|
||||||
|
virtual RHIBuffer* CreateBuffer(const BufferDesc& desc) = 0;
|
||||||
|
virtual RHITexture* CreateTexture(const TextureDesc& desc) = 0;
|
||||||
|
virtual RHISwapChain* CreateSwapChain(const SwapChainDesc& desc) = 0;
|
||||||
|
virtual RHICommandList* CreateCommandList(const CommandListDesc& desc) = 0;
|
||||||
|
virtual RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) = 0;
|
||||||
|
virtual RHIShader* CompileShader(const ShaderCompileDesc& desc) = 0;
|
||||||
|
virtual RHIPipelineState* CreatePipelineState(const PipelineStateDesc& desc) = 0;
|
||||||
|
virtual RHIFence* CreateFence(const FenceDesc& desc) = 0;
|
||||||
|
virtual RHISampler* CreateSampler(const SamplerDesc& desc) = 0;
|
||||||
|
|
||||||
|
virtual const RHICapabilities& GetCapabilities() const = 0;
|
||||||
|
virtual const RHIDeviceInfo& GetDeviceInfo() const = 0;
|
||||||
|
|
||||||
|
virtual void* GetNativeDevice() = 0;
|
||||||
|
|
||||||
|
virtual bool PollEvents() = 0;
|
||||||
|
virtual void SwapBuffers() = 0;
|
||||||
|
virtual bool ShouldClose() const = 0;
|
||||||
|
virtual void SetShouldClose(bool shouldClose) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace RHI
|
||||||
|
} // namespace XCEngine
|
||||||
@@ -7,11 +7,13 @@ namespace RHI {
|
|||||||
|
|
||||||
enum class ShaderType : uint8_t {
|
enum class ShaderType : uint8_t {
|
||||||
Vertex,
|
Vertex,
|
||||||
|
Fragment,
|
||||||
|
Geometry,
|
||||||
|
Compute,
|
||||||
|
TessControl,
|
||||||
|
TessEvaluation,
|
||||||
Hull,
|
Hull,
|
||||||
Domain,
|
Domain,
|
||||||
Geometry,
|
|
||||||
Pixel,
|
|
||||||
Compute,
|
|
||||||
Amplification,
|
Amplification,
|
||||||
Mesh,
|
Mesh,
|
||||||
Library
|
Library
|
||||||
@@ -291,5 +293,12 @@ enum class HeapType : uint8_t {
|
|||||||
Custom
|
Custom
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class RHIType : uint8_t {
|
||||||
|
D3D12,
|
||||||
|
OpenGL,
|
||||||
|
Vulkan,
|
||||||
|
Metal
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace RHI
|
} // namespace RHI
|
||||||
} // namespace XCEngine
|
} // namespace XCEngine
|
||||||
|
|||||||
35
engine/include/XCEngine/RHI/RHIShader.h
Normal file
35
engine/include/XCEngine/RHI/RHIShader.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RHITypes.h"
|
||||||
|
#include "RHIEnums.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace XCEngine {
|
||||||
|
namespace RHI {
|
||||||
|
|
||||||
|
class RHIShader {
|
||||||
|
public:
|
||||||
|
virtual ~RHIShader() = default;
|
||||||
|
|
||||||
|
virtual bool CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) = 0;
|
||||||
|
virtual bool Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) = 0;
|
||||||
|
|
||||||
|
virtual ShaderType GetType() const = 0;
|
||||||
|
virtual bool IsValid() const = 0;
|
||||||
|
|
||||||
|
virtual void Bind() = 0;
|
||||||
|
virtual void Unbind() = 0;
|
||||||
|
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
virtual void SetInt(const char* name, int value) = 0;
|
||||||
|
virtual void SetFloat(const char* name, float value) = 0;
|
||||||
|
virtual void SetVec3(const char* name, float x, float y, float z) = 0;
|
||||||
|
virtual void SetVec4(const char* name, float x, float y, float z, float w) = 0;
|
||||||
|
virtual void SetMat4(const char* name, const float* value) = 0;
|
||||||
|
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace RHI
|
||||||
|
} // namespace XCEngine
|
||||||
33
engine/include/XCEngine/RHI/RHITexture.h
Normal file
33
engine/include/XCEngine/RHI/RHITexture.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "RHITypes.h"
|
||||||
|
#include "RHIEnums.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace XCEngine {
|
||||||
|
namespace RHI {
|
||||||
|
|
||||||
|
class RHITexture {
|
||||||
|
public:
|
||||||
|
virtual ~RHITexture() = default;
|
||||||
|
|
||||||
|
virtual uint32_t GetWidth() const = 0;
|
||||||
|
virtual uint32_t GetHeight() const = 0;
|
||||||
|
virtual uint32_t GetDepth() const = 0;
|
||||||
|
virtual uint32_t GetMipLevels() const = 0;
|
||||||
|
virtual Format GetFormat() const = 0;
|
||||||
|
virtual TextureType GetTextureType() const = 0;
|
||||||
|
|
||||||
|
virtual ResourceStates GetState() const = 0;
|
||||||
|
virtual void SetState(ResourceStates state) = 0;
|
||||||
|
|
||||||
|
virtual void* GetNativeHandle() = 0;
|
||||||
|
|
||||||
|
virtual const std::string& GetName() const = 0;
|
||||||
|
virtual void SetName(const std::string& name) = 0;
|
||||||
|
|
||||||
|
virtual void Shutdown() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace RHI
|
||||||
|
} // namespace XCEngine
|
||||||
@@ -238,5 +238,54 @@ struct PipelineStateDesc {
|
|||||||
uint32_t size;
|
uint32_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct RHIDeviceDesc {
|
||||||
|
bool enableDebugLayer = false;
|
||||||
|
bool enableGPUValidation = false;
|
||||||
|
uint32_t adapterIndex = 0;
|
||||||
|
void* windowHandle = nullptr;
|
||||||
|
uint32_t width = 1280;
|
||||||
|
uint32_t height = 720;
|
||||||
|
std::wstring appName = L"XCEngine";
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RHIDeviceInfo {
|
||||||
|
std::wstring description;
|
||||||
|
std::wstring vendor;
|
||||||
|
std::wstring renderer;
|
||||||
|
std::wstring version;
|
||||||
|
uint64_t dedicatedVideoMemory = 0;
|
||||||
|
uint64_t dedicatedSystemMemory = 0;
|
||||||
|
uint64_t sharedSystemMemory = 0;
|
||||||
|
uint32_t vendorId = 0;
|
||||||
|
uint32_t deviceId = 0;
|
||||||
|
bool isSoftware = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RHIRenderPassDesc {
|
||||||
|
struct ColorAttachment {
|
||||||
|
void* texture = nullptr;
|
||||||
|
uint32_t loadAction = 0;
|
||||||
|
uint32_t storeAction = 0;
|
||||||
|
float clearColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
|
};
|
||||||
|
ColorAttachment colorAttachments[8];
|
||||||
|
uint32_t colorAttachmentCount = 0;
|
||||||
|
struct DepthStencilAttachment {
|
||||||
|
void* texture = nullptr;
|
||||||
|
uint32_t loadAction = 0;
|
||||||
|
uint32_t storeAction = 0;
|
||||||
|
float clearDepth = 1.0f;
|
||||||
|
uint8_t clearStencil = 0;
|
||||||
|
} depthStencilAttachment;
|
||||||
|
bool hasDepthStencil = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RHIPipelineLayoutDesc {
|
||||||
|
uint32_t constantBufferCount = 0;
|
||||||
|
uint32_t textureCount = 0;
|
||||||
|
uint32_t samplerCount = 0;
|
||||||
|
uint32_t uavCount = 0;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace RHI
|
} // namespace RHI
|
||||||
} // namespace XCEngine
|
} // namespace XCEngine
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ bool D3D12Shader::CompileFromFile(const wchar_t* filePath, const char* entryPoin
|
|||||||
if (strstr(target, "vs_")) {
|
if (strstr(target, "vs_")) {
|
||||||
m_type = ShaderType::Vertex;
|
m_type = ShaderType::Vertex;
|
||||||
} else if (strstr(target, "ps_")) {
|
} else if (strstr(target, "ps_")) {
|
||||||
m_type = ShaderType::Pixel;
|
m_type = ShaderType::Fragment;
|
||||||
} else if (strstr(target, "gs_")) {
|
} else if (strstr(target, "gs_")) {
|
||||||
m_type = ShaderType::Geometry;
|
m_type = ShaderType::Geometry;
|
||||||
} else if (strstr(target, "cs_")) {
|
} else if (strstr(target, "cs_")) {
|
||||||
|
|||||||
@@ -213,68 +213,60 @@ void OpenGLShader::Use() const {
|
|||||||
glUseProgram(m_program);
|
glUseProgram(m_program);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::Unbind() const {
|
void OpenGLShader::Unbind() {
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetInt(const std::string& name, int value) const {
|
void OpenGLShader::SetInt(const char* name, int value) {
|
||||||
glUniform1i(glGetUniformLocation(m_program, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(m_program, name), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetIntArray(const std::string& name, const int* values, unsigned int count) const {
|
void OpenGLShader::SetIntArray(const char* name, const int* values, unsigned int count) {
|
||||||
glUniform1iv(glGetUniformLocation(m_program, name.c_str()), count, values);
|
glUniform1iv(glGetUniformLocation(m_program, name), count, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetFloat(const std::string& name, float value) const {
|
void OpenGLShader::SetFloat(const char* name, float value) {
|
||||||
glUniform1f(glGetUniformLocation(m_program, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(m_program, name), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetFloatArray(const std::string& name, const float* values, unsigned int count) const {
|
void OpenGLShader::SetFloatArray(const char* name, const float* values, unsigned int count) {
|
||||||
glUniform1fv(glGetUniformLocation(m_program, name.c_str()), count, values);
|
glUniform1fv(glGetUniformLocation(m_program, name), count, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec2(const std::string& name, float x, float y) const {
|
void OpenGLShader::SetVec3(const char* name, float x, float y, float z) {
|
||||||
glUniform2f(glGetUniformLocation(m_program, name.c_str()), x, y);
|
glUniform3f(glGetUniformLocation(m_program, name), x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec2(const std::string& name, const float* values) const {
|
void OpenGLShader::SetVec3(const char* name, const float* values) {
|
||||||
glUniform2fv(glGetUniformLocation(m_program, name.c_str()), 1, values);
|
glUniform3fv(glGetUniformLocation(m_program, name), 1, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec3(const std::string& name, float x, float y, float z) const {
|
void OpenGLShader::SetVec4(const char* name, float x, float y, float z, float w) {
|
||||||
glUniform3f(glGetUniformLocation(m_program, name.c_str()), x, y, z);
|
glUniform4f(glGetUniformLocation(m_program, name), x, y, z, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec3(const std::string& name, const float* values) const {
|
void OpenGLShader::SetVec4(const char* name, const float* values) {
|
||||||
glUniform3fv(glGetUniformLocation(m_program, name.c_str()), 1, values);
|
glUniform4fv(glGetUniformLocation(m_program, name), 1, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec4(const std::string& name, float x, float y, float z, float w) const {
|
void OpenGLShader::SetMat2(const char* name, const float* value) {
|
||||||
glUniform4f(glGetUniformLocation(m_program, name.c_str()), x, y, z, w);
|
glUniformMatrix2fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetVec4(const std::string& name, const float* values) const {
|
void OpenGLShader::SetMat3(const char* name, const float* value) {
|
||||||
glUniform4fv(glGetUniformLocation(m_program, name.c_str()), 1, values);
|
glUniformMatrix3fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetMat2(const std::string& name, const float* value) const {
|
void OpenGLShader::SetMat4(const char* name, const float* value) {
|
||||||
glUniformMatrix2fv(glGetUniformLocation(m_program, name.c_str()), 1, GL_FALSE, value);
|
glUniformMatrix4fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetMat3(const std::string& name, const float* value) const {
|
void OpenGLShader::SetMat4Array(const char* name, const float* values, unsigned int count) {
|
||||||
glUniformMatrix3fv(glGetUniformLocation(m_program, name.c_str()), 1, GL_FALSE, value);
|
glUniformMatrix4fv(glGetUniformLocation(m_program, name), count, GL_FALSE, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShader::SetMat4(const std::string& name, const float* value) const {
|
int OpenGLShader::GetUniformLocation(const char* name) const {
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_program, name.c_str()), 1, GL_FALSE, value);
|
return glGetUniformLocation(m_program, name);
|
||||||
}
|
|
||||||
|
|
||||||
void OpenGLShader::SetMat4Array(const std::string& name, const float* values, unsigned int count) const {
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_program, name.c_str()), count, GL_FALSE, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
int OpenGLShader::GetUniformLocation(const std::string& name) const {
|
|
||||||
return glGetUniformLocation(m_program, name.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenGLShader::CheckCompileErrors(unsigned int shader, const char* type) {
|
bool OpenGLShader::CheckCompileErrors(unsigned int shader, const char* type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user