Diligent Engine 开源项目教程
项目介绍
Diligent Engine 是一个现代的、跨平台的低级图形库,旨在为开发人员提供高效、灵活且易于使用的图形API。它支持多种平台,包括Windows、Linux、macOS、Android和iOS,并且兼容Direct3D11、Direct3D12、OpenGL/GLES和Vulkan。Diligent Engine 的设计理念是提供一个统一的接口,使得开发人员可以轻松地在不同平台上进行开发,同时保持高性能和低开销。
项目快速启动
环境搭建
首先,克隆Diligent Engine仓库到本地:
git clone https://github.com/DiligentGraphics/DiligentEngine.git
进入项目目录并构建项目:
cd DiligentEngine
mkdir build
cd build
cmake ..
make
示例代码
以下是一个简单的示例代码,展示如何在Diligent Engine中创建一个窗口并渲染一个三角形:
#include "EngineFactoryD3D11.h"
#include "EngineFactoryD3D12.h"
#include "EngineFactoryOpenGL.h"
#include "EngineFactoryVulkan.h"
#include "GraphicsEngine.h"
#include "SwapChain.h"
#include "DeviceContext.h"
#include "Shader.h"
#include "PipelineState.h"
#include "Buffer.h"
#include "RenderDevice.h"
#include "ShaderResourceBinding.h"
#include "MapHelper.h"
using namespace Diligent;
void CreateDeviceAndSwapChain()
{
// 初始化引擎工厂
auto* pFactoryD3D11 = GetEngineFactoryD3D11();
EngineD3D11CreateInfo EngineCI;
pFactoryD3D11->CreateDeviceAndContextsD3D11(EngineCI, &m_pDevice, &m_pContext);
// 创建交换链
SwapChainDesc SCDesc;
pFactoryD3D11->CreateSwapChainD3D11(m_pDevice, m_pContext, SCDesc, FullScreenModeDesc{}, nullptr, &m_pSwapChain);
}
void InitializePipelineState()
{
// 创建顶点着色器
ShaderCreateInfo ShaderCI;
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
ShaderCI.ShaderCompiler = SHADER_COMPILER_DEFAULT;
ShaderCI.UseCombinedTextureSamplers = true;
RefCntAutoPtr<IShader> pVS;
{
ShaderCI.Desc.ShaderType = SHADER_TYPE_VERTEX;
ShaderCI.EntryPoint = "main";
ShaderCI.Desc.Name = "Triangle Vertex Shader";
ShaderCI.Source = R"(
float4 main(float4 pos : POSITION) : SV_POSITION
{
return pos;
}
)";
m_pDevice->CreateShader(ShaderCI, &pVS);
}
// 创建像素着色器
RefCntAutoPtr<IShader> pPS;
{
ShaderCI.Desc.ShaderType = SHADER_TYPE_PIXEL;
ShaderCI.EntryPoint = "main";
ShaderCI.Desc.Name = "Triangle Pixel Shader";
ShaderCI.Source = R"(
float4 main() : SV_TARGET
{
return float4(1.0, 0.5, 0.2, 1.0);
}
)";
m_pDevice->CreateShader(ShaderCI, &pPS);
}
// 创建管线状态
GraphicsPipelineStateCreateInfo PSOCreateInfo;
PSOCreateInfo.PSODesc.Name = "Triangle PSO";
PSOCreateInfo.PSODesc.GraphicsPipeline.NumRenderTargets = 1;
PSOCreateInfo.PSODesc.GraphicsPipeline.RTVFormats[0] = m_pSwapChain->GetDesc().ColorBufferFormat;
PSOCreateInfo.PSODesc.GraphicsPipeline.DSVFormat = m_pSwapChain->GetDesc().DepthBufferFormat;
PSOCreateInfo.pVS = pVS;
PSOCreateInfo.pPS = pPS