Skip to content

Commit 9ac87af

Browse files
* 修改shader入参
* C++对shader的输入矩阵提前进行转置 * 采用两个常量缓冲区绘制(向d3d12book靠拢)
1 parent 66e3c6d commit 9ac87af

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

Chapter 8 Lighting/Core/Shaders/defaultVS.hlsl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
cbuffer VSConstants : register(b0)
22
{
3-
float4x4 modelToProjection;
3+
float4x4 modelToWorld;
4+
};
5+
6+
cbuffer PassConstants : register(b1)
7+
{
8+
float4x4 gViewProj;
49
};
510

611
struct VertexIn
@@ -19,8 +24,11 @@ VertexOut main(VertexIn vin)
1924
{
2025
VertexOut vout;
2126

27+
// Transform to world space.
28+
float4 posW = mul(float4(vin.PosL, 1.0), modelToWorld);
29+
2230
// Transform to homogeneous clip space.
23-
vout.PosH = mul(modelToProjection, float4(vin.PosL, 1.0));
31+
vout.PosH = mul(posW, gViewProj);
2432

2533
// Just pass vertex color into the pixel shader.
2634
vout.Color = vin.Color;

Chapter 8 Lighting/GameApp.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ void GameApp::Startup(void)
3838
buildLandAndWaves();
3939

4040
// 根签名
41-
m_RootSignature.Reset(1, 0);
41+
m_RootSignature.Reset(2, 0);
4242
m_RootSignature[0].InitAsConstantBuffer(0, D3D12_SHADER_VISIBILITY_VERTEX);
43+
m_RootSignature[1].InitAsConstantBuffer(1, D3D12_SHADER_VISIBILITY_ALL);
4344
m_RootSignature.Finalize(L"box signature", D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
4445

4546
D3D12_INPUT_ELEMENT_DESC mInputLayout[] =
@@ -192,6 +193,19 @@ void GameApp::RenderScene(void)
192193
// 设置顶点拓扑结构
193194
gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
194195

196+
// 设置VS、PS通用的常量缓冲区
197+
__declspec(align(16)) struct
198+
{
199+
Matrix4 viewProj; // 从世界坐标转为投影坐标的矩阵
200+
}passConstants;
201+
202+
// https://www.cnblogs.com/X-Jun/p/9808727.html
203+
// C++代码端进行转置,HLSL中使用matrix(列矩阵)
204+
// mul函数让向量放在左边(行向量),实际运算是(行向量 X 行矩阵) 然后行矩阵为了使用dp4运算发生了转置成了列矩阵
205+
passConstants.viewProj = Transpose(m_ViewProjMatrix);
206+
gfxContext.SetDynamicConstantBufferView(1, sizeof(passConstants), &passConstants);
207+
208+
// 开始绘制
195209
if (m_bRenderShapes)
196210
renderShapes(gfxContext);
197211
else
@@ -269,26 +283,26 @@ void GameApp::buildShapesData()
269283

270284
renderItem item;
271285
// box
272-
item.modelToWorld = Matrix4(AffineTransform(Matrix3::MakeScale(2.0f, 2.0f, 2.0f), Vector3(0.0f, 0.5f, 0.0f)));
286+
item.modelToWorld = Transpose(Matrix4(AffineTransform(Matrix3::MakeScale(2.0f, 2.0f, 2.0f), Vector3(0.0f, 0.5f, 0.0f))));
273287
item.indexCount = (UINT)box.Indices32.size();
274288
item.startIndex = boxIndexOffset;
275289
item.baseVertex = boxVertexOffset;
276290
m_vecShapes.push_back(item);
277291

278292
// grid
279-
item.modelToWorld = Matrix4(kIdentity);
293+
item.modelToWorld = Transpose(Matrix4(kIdentity));
280294
item.indexCount = (UINT)grid.Indices32.size();
281295
item.startIndex = gridIndexOffset;
282296
item.baseVertex = gridVertexOffset;
283297
m_vecShapes.push_back(item);
284298

285299
for (int i = 0; i < 5; ++i)
286300
{
287-
Matrix4 leftCylWorld = Matrix4(AffineTransform(Vector3(-5.0f, 1.5f, -10.0f + i * 5.0f)));
288-
Matrix4 rightCylWorld = Matrix4(AffineTransform(Vector3(+5.0f, 1.5f, -10.0f + i * 5.0f)));
301+
Matrix4 leftCylWorld = Transpose(Matrix4(AffineTransform(Vector3(-5.0f, 1.5f, -10.0f + i * 5.0f))));
302+
Matrix4 rightCylWorld = Transpose(Matrix4(AffineTransform(Vector3(+5.0f, 1.5f, -10.0f + i * 5.0f))));
289303

290-
Matrix4 leftSphereWorld = Matrix4(AffineTransform(Vector3(-5.0f, 3.5f, -10.0f + i * 5.0f)));
291-
Matrix4 rightSphereWorld = Matrix4(AffineTransform(Vector3(+5.0f, 3.5f, -10.0f + i * 5.0f)));
304+
Matrix4 leftSphereWorld = Transpose(Matrix4(AffineTransform(Vector3(-5.0f, 3.5f, -10.0f + i * 5.0f))));
305+
Matrix4 rightSphereWorld = Transpose(Matrix4(AffineTransform(Vector3(+5.0f, 3.5f, -10.0f + i * 5.0f))));
292306

293307
// cylinder
294308
item.indexCount = (UINT)cylinder.Indices32.size();
@@ -319,9 +333,8 @@ void GameApp::renderShapes(GraphicsContext& gfxContext)
319333

320334
for (auto& item : m_vecShapes)
321335
{
322-
Matrix4 a = m_ViewProjMatrix * item.modelToWorld;
323336
// 设置常量缓冲区数据
324-
gfxContext.SetDynamicConstantBufferView(0, sizeof(a), &a);
337+
gfxContext.SetDynamicConstantBufferView(0, sizeof(item.modelToWorld), &item.modelToWorld);
325338
// 绘制
326339
gfxContext.DrawIndexedInstanced(item.indexCount, 1, item.startIndex, item.baseVertex, 0);
327340
}
@@ -413,7 +426,7 @@ float GameApp::GetHillsHeight(float x, float z) const
413426
void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
414427
{
415428
// 设置常量缓冲区数据
416-
gfxContext.SetDynamicConstantBufferView(0, sizeof(m_ViewProjMatrix), &m_ViewProjMatrix);
429+
gfxContext.SetDynamicConstantBufferView(0, sizeof(m_waveWorld), &m_waveWorld);
417430

418431
// land
419432
// 设置顶点视图

Chapter 8 Lighting/GameApp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class GameApp : public GameCore::IGameApp
6666
Waves m_waves{ 128, 128, 1.0f, 0.03f, 4.0f, 0.2f };
6767
ByteAddressBuffer m_IndexBufferWaves;
6868
std::vector<Vertex> m_verticesWaves;
69+
Matrix4 m_waveWorld = Transpose(Matrix4(kIdentity));
6970

7071
// 摄像机
7172
Camera m_Camera;

0 commit comments

Comments
 (0)