Skip to content

Instantly share code, notes, and snippets.

@Mainvooid
Last active February 28, 2019 02:15
Show Gist options
  • Select an option

  • Save Mainvooid/3a752d7f64d73885efc8e97a35312c1e to your computer and use it in GitHub Desktop.

Select an option

Save Mainvooid/3a752d7f64d73885efc8e97a35312c1e to your computer and use it in GitHub Desktop.
D3D常用辅助函数 #DirectX #C++
#include <d3d11.h>
#include <d3dx11.h>
/*
*@brief 创建texture配置
*@param textureDesc 保存Texture配置的对象
*@param width 宽度
*@param height 高度
*/
void createTextureDesc(D3D11_TEXTURE2D_DESC &textureDesc,int width, int height) {
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = width;
textureDesc.Height = height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
}
/*
*@brief 创建D3D Device
*@param d3dDevice 保存ID3D11Device的对象
*/
void createDevice(ID3D11Device *&d3dDevice) {
UINT createDeviceFlags = 0;
//#ifdef _DEBUG
// createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG ;
//#endif
createDeviceFlags |= D3D11_CREATE_DEVICE_BGRA_SUPPORT;
D3D_FEATURE_LEVEL featureLevel;
ID3D11DeviceContext* md3dImmediateContext;//立即执行上下文
HRESULT hr = D3D11CreateDevice(
0, //IDXGIAdapter* 默认显示适配器
D3D_DRIVER_TYPE_HARDWARE,//D3D_DRIVER_TYPE 驱动类型
0, //HMODULE 不使用软件驱动
createDeviceFlags,
0, //若为nullptr则为默认特性等级,否则需要提供特性等级数组
0, //特性等级数组的元素数目
D3D11_SDK_VERSION, //SDK版本
&d3dDevice, //输出D3D设备
&featureLevel, //输出当前应用D3D特性等级
&md3dImmediateContext);//输出D3D设备上下文
if (FAILED(hr))
{
OutputDebugStringA("D3D11CreateDevice Failed.");
}
if (featureLevel != D3D_FEATURE_LEVEL_11_0)
{
OutputDebugStringA("Direct3D FeatureLevel 11 unsupported.");
}
}
/*
*@brief 保存Texture到文件
*@param d3dDevice Device对象
*@param pTexture2D Texture2D对象
*@param path 以.png结尾的路径
*/
void saveTextureToFile(ID3D11Device *d3dDevice, ID3D11Resource* pTexture2D,LPCSTR path) {
//应判断格式RGBA还是BGRA,并转换保存为RGBA
ID3D11DeviceContext *ctx = nullptr;
d3dDevice->GetImmediateContext(&ctx);
HRESULT hr = D3DX11SaveTextureToFile(ctx, pTexture2D, D3DX11_IFF_PNG, path);
if (FAILED(hr))
{
OutputDebugStringA("D3DX11SaveTextureToFile failed.");
}
}
/*
*@brief 读取Texture
*@param d3dDevice Device对象
*@param path 要读取的图像文件路径
*@param pTexture2D Texture2D对象
*/
void loadTextureFromFile(ID3D11Device *d3dDevice,LPCSTR path, ID3D11Texture2D *&pTexture2D) {
D3DX11_IMAGE_LOAD_INFO loadInfo;
ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO));
loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE;
loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
loadInfo.MipLevels = D3DX11_DEFAULT; //这时会产生最大的mipmaps层。
loadInfo.MipFilter = D3DX11_FILTER_LINEAR;
ID3DX11ThreadPump *pump = NULL;
HRESULT hr = D3DX11CreateTextureFromFile(d3dDevice, path, &loadInfo, pump, (ID3D11Resource**)&pTexture2D, NULL);
if (FAILED(hr)) {
OutputDebugStringA("D3DX11CreateTextureFromFile failed.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment