-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkyEffect.cpp
187 lines (146 loc) · 4.77 KB
/
SkyEffect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "Effects.h"
#include "d3dUtil.h"
#include "EffectHelper.h" // 必须晚于Effects.h和d3dUtil.h包含
#include "DXTrace.h"
#include "Vertex.h"
using namespace DirectX;
//
// SkyEffect::Impl 需要先于SkyEffect的定义
//
class SkyEffect::Impl : public AlignedType<SkyEffect::Impl>
{
public:
//
// 这些结构体对应HLSL的结构体。需要按16字节对齐
//
struct CBChangesEveryFrame
{
DirectX::XMMATRIX worldViewProj;
};
public:
// 必须显式指定
Impl() : m_IsDirty() {}
~Impl() = default;
public:
CBufferObject<0, CBChangesEveryFrame> m_CBFrame; // 每帧绘制的常量缓冲区
BOOL m_IsDirty; // 是否有值变更
std::vector<CBufferBase*> m_pCBuffers; // 统一管理上面所有的常量缓冲区
ComPtr<ID3D11VertexShader> m_pSkyVS;
ComPtr<ID3D11PixelShader> m_pSkyPS;
ComPtr<ID3D11InputLayout> m_pVertexPosLayout;
ComPtr<ID3D11ShaderResourceView> m_pTextureCube; // 天空盒纹理
};
//
// SkyEffect
//
namespace
{
// SkyEffect单例
static SkyEffect * g_pInstance = nullptr;
}
SkyEffect::SkyEffect()
{
if (g_pInstance)
throw std::exception("SkyEffect is a singleton!");
g_pInstance = this;
pImpl = std::make_unique<SkyEffect::Impl>();
}
SkyEffect::~SkyEffect()
{
}
SkyEffect::SkyEffect(SkyEffect && moveFrom) noexcept
{
pImpl.swap(moveFrom.pImpl);
}
SkyEffect & SkyEffect::operator=(SkyEffect && moveFrom) noexcept
{
pImpl.swap(moveFrom.pImpl);
return *this;
}
SkyEffect & SkyEffect::Get()
{
if (!g_pInstance)
throw std::exception("SkyEffect needs an instance!");
return *g_pInstance;
}
bool SkyEffect::InitAll(ID3D11Device * device)
{
if (!device)
return false;
if (!pImpl->m_pCBuffers.empty())
return true;
if (!RenderStates::IsInit())
throw std::exception("RenderStates need to be initialized first!");
ComPtr<ID3DBlob> blob;
// ******************
// 创建顶点着色器
//
HR(CreateShaderFromFile(L"HLSL\\Sky_VS.cso", L"HLSL\\Sky_VS.hlsl", "VS", "vs_5_0", blob.ReleaseAndGetAddressOf()));
HR(device->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, pImpl->m_pSkyVS.GetAddressOf()));
// 创建顶点布局
HR(device->CreateInputLayout(VertexPos::inputLayout, ARRAYSIZE(VertexPos::inputLayout),
blob->GetBufferPointer(), blob->GetBufferSize(), pImpl->m_pVertexPosLayout.GetAddressOf()));
// ******************
// 创建像素着色器
//
HR(CreateShaderFromFile(L"HLSL\\Sky_PS.cso", L"HLSL\\Sky_PS.hlsl", "PS", "ps_5_0", blob.ReleaseAndGetAddressOf()));
HR(device->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, pImpl->m_pSkyPS.GetAddressOf()));
pImpl->m_pCBuffers.assign({
&pImpl->m_CBFrame,
});
// 创建常量缓冲区
for (auto& pBuffer : pImpl->m_pCBuffers)
{
HR(pBuffer->CreateBuffer(device));
}
// 设置调试对象名
D3D11SetDebugObjectName(pImpl->m_pVertexPosLayout.Get(), "SkyEffect.VertexPosLayout");
D3D11SetDebugObjectName(pImpl->m_pCBuffers[0]->cBuffer.Get(), "SkyEffect.CBFrame");
D3D11SetDebugObjectName(pImpl->m_pSkyVS.Get(), "SkyEffect.Sky_VS");
D3D11SetDebugObjectName(pImpl->m_pSkyPS.Get(), "SkyEffect.Sky_PS");
return true;
}
void SkyEffect::SetRenderDefault(ID3D11DeviceContext * deviceContext)
{
deviceContext->IASetInputLayout(pImpl->m_pVertexPosLayout.Get());
deviceContext->VSSetShader(pImpl->m_pSkyVS.Get(), nullptr, 0);
deviceContext->PSSetShader(pImpl->m_pSkyPS.Get(), nullptr, 0);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
deviceContext->GSSetShader(nullptr, nullptr, 0);
deviceContext->RSSetState(RenderStates::RSNoCull.Get());
deviceContext->PSSetSamplers(0, 1, RenderStates::SSLinearWrap.GetAddressOf());
deviceContext->OMSetDepthStencilState(RenderStates::DSSLessEqual.Get(), 0);
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFFF);
}
void XM_CALLCONV SkyEffect::SetWorldViewProjMatrix(DirectX::FXMMATRIX W, DirectX::CXMMATRIX V, DirectX::CXMMATRIX P)
{
auto& cBuffer = pImpl->m_CBFrame;
cBuffer.data.worldViewProj = XMMatrixTranspose(W * V * P);
pImpl->m_IsDirty = cBuffer.isDirty = true;
}
void XM_CALLCONV SkyEffect::SetWorldViewProjMatrix(DirectX::FXMMATRIX WVP)
{
auto& cBuffer = pImpl->m_CBFrame;
cBuffer.data.worldViewProj = XMMatrixTranspose(WVP);
pImpl->m_IsDirty = cBuffer.isDirty = true;
}
void SkyEffect::SetTextureCube(ID3D11ShaderResourceView * m_pTextureCube)
{
pImpl->m_pTextureCube = m_pTextureCube;
}
void SkyEffect::Apply(ID3D11DeviceContext * deviceContext)
{
auto& pCBuffers = pImpl->m_pCBuffers;
// 将缓冲区绑定到渲染管线上
pCBuffers[0]->BindVS(deviceContext);
// 设置SRV
deviceContext->PSSetShaderResources(0, 1, pImpl->m_pTextureCube.GetAddressOf());
if (pImpl->m_IsDirty)
{
pImpl->m_IsDirty = false;
for (auto& pCBuffer : pCBuffers)
{
pCBuffer->UpdateBuffer(deviceContext);
}
}
}