这个类是用来创建着色器并设置输入布局的。
这个类比较特殊,它创建的着色器与Effect文件有关,effect文件是用高级着色语言(hlsl)编写的。
shadersclass.h
#pragma once
#include <d3d11.h>
#include <d3dcompiler.h>
#include <D3DX11.h>
#include <xnamath.h> #pragma comment(lib,"d3dx11.lib")
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dcompiler.lib")
class shaderclass
{
public:
shaderclass();
~shaderclass();
bool Initialize(ID3D11Device *device, ID3D11DeviceContext* context);
void Shutdown();
private:
bool CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut);
private:
ID3DBlob* m_vsblob,*m_psblob;
ID3D11PixelShader *m_pixelshader;
ID3D11VertexShader *m_vertexshader;
ID3D11InputLayout *m_layout;
public:
void GetShaders(ID3D11VertexShader*& vs, ID3D11PixelShader*& ps);
};
这个着色器类比较简单,只创建了顶点着色器和像素着色器,还设置了一个简单的输入布局,这些工作都由Initialize()函数来做,私有函数CompileShaderFromFile()函数则是用来将effect文件编译成二进制文件的,GetShaders()则是用来获取创建好的着色器对象的。
shaderclass.cpp
#include "shaderclass.h" shaderclass::shaderclass()
{
} shaderclass::~shaderclass()
{
} bool shaderclass::Initialize(ID3D11Device *device,ID3D11DeviceContext* context)
{
HRESULT hr = S_OK;
bool result = CompileShaderFromFile(L"shader.fx", "VS", "vs_4_0", &m_vsblob);
if (!result)
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return false;
} hr = device->CreateVertexShader(m_vsblob->GetBufferPointer(), m_vsblob->GetBufferSize(), NULL, &m_vertexshader);
if (FAILED(hr))
{
return false;
} result = CompileShaderFromFile(L"shader.fx", "PS", "ps_4_0", &m_psblob);
if (!result)
{
MessageBox(NULL,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return false;
} hr = device->CreatePixelShader(m_psblob->GetBufferPointer(), m_psblob->GetBufferSize(), NULL, &m_pixelshader);
if (FAILED(hr))
{
return false;
}
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", , DXGI_FORMAT_R32G32B32_FLOAT, , , D3D11_INPUT_PER_VERTEX_DATA, },
{ "TEXCOORD", , DXGI_FORMAT_R32G32_FLOAT, , , D3D11_INPUT_PER_VERTEX_DATA, },
};/////////////////
UINT numElements = ARRAYSIZE(layout); hr = device->CreateInputLayout(layout, numElements, m_vsblob->GetBufferPointer(),m_vsblob->GetBufferSize(), &m_layout);
if (FAILED(hr))
{
return false;
}
context->IASetInputLayout(m_layout); return true;
}
void shaderclass::Shutdown()
{
if (m_layout)
{
m_layout->Release();
}
if (m_pixelshader)
{
m_pixelshader->Release();
}
if (m_psblob)
{
m_psblob->Release();
}
if (m_vertexshader)
{
m_vertexshader->Release();
}
if (m_vsblob)
{
m_vsblob->Release();
}
} bool shaderclass::CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut)
{
HRESULT hr = S_OK; DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif ID3DBlob* pErrorBlob=;
hr = D3DX11CompileFromFile(szFileName, NULL, NULL, szEntryPoint, szShaderModel,
dwShaderFlags, , NULL, ppBlobOut, &pErrorBlob, NULL); if (FAILED(hr))
{
if (pErrorBlob != NULL)
OutputDebugStringA((char*)pErrorBlob->GetBufferPointer());
if (pErrorBlob) pErrorBlob->Release();
return false;
}
if (pErrorBlob)
pErrorBlob->Release(); return true;
} void shaderclass::GetShaders(ID3D11VertexShader*& vs,ID3D11PixelShader*& ps)
{
vs = m_vertexshader;
ps = m_pixelshader;
}
Initialize():
- 调用CompileShaderFromFile()函数编译effect文件里的顶点处理函数,得到二进制文件,根据二进制文件创建顶点着色器,CompileShaderFromFile()函数的第一个参数“shader.fx”就是effect文件
- 编译effect文件里的像素处理函数,得到二进制文件并创建像素着色器
- 填充输入布局描述数据结构,并创建输入布局,成功后设置输入布局
CompileShaderFromFile(): 里面只是单纯的调用dx11API D3DX11CompileFromFile()函数
整个类的编写非常简单,这只是因为我很简单的创建了两个着色器。如果要创建更复杂的着色器,还要学习很多。
另附上effect文件
shader.fx
Texture2D txDiffuse : register(t0);
SamplerState samLinear : register(s0); cbuffer ConstantBuffer : register(b0)
{
matrix World;
matrix View;
matrix Projection;
} struct VS_INPUT
{
float4 Pos : POSITION;
float4 Tex : TEXCOORD0;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT);
output.Pos = mul(input.Pos, World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
output.Tex = input.Tex; return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(PS_INPUT input) : SV_Target
{
return txDiffuse.Sample(samLinear, input.Tex);
}