From 6c92164a03e3620357070c25399bac0c59c404eb Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 22 Mar 2026 17:58:30 +0800 Subject: [PATCH] fix: D3D12 sphere rendering - correct matrix math, enable depth test, add texture sampling - Fix TranslationMatrix to use correct indices (m12/m13/m14 for translation) - Fix PerspectiveMatrix projection formula - Enable depth testing (was disabled) - Add texture sampling in pixel shader - Adjust sphere radius to 1.0 and position to z=5 - Fix D3D12Buffer copy size calculation --- engine/src/RHI/D3D12/D3D12Buffer.cpp | 4 +- .../integration/sphere/Res/Shader/sphere.hlsl | 14 +++++-- tests/RHI/D3D12/integration/sphere/main.cpp | 41 +++++++++++-------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/engine/src/RHI/D3D12/D3D12Buffer.cpp b/engine/src/RHI/D3D12/D3D12Buffer.cpp index 63521eb4..c55257c2 100644 --- a/engine/src/RHI/D3D12/D3D12Buffer.cpp +++ b/engine/src/RHI/D3D12/D3D12Buffer.cpp @@ -99,7 +99,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand D3D12_RESOURCE_DESC d3d12TempResourceDesc = {}; d3d12TempResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; d3d12TempResourceDesc.Alignment = 0; - d3d12TempResourceDesc.Width = memorySizeUsed; + d3d12TempResourceDesc.Width = size; d3d12TempResourceDesc.Height = 1; d3d12TempResourceDesc.DepthOrArraySize = 1; d3d12TempResourceDesc.MipLevels = 1; @@ -129,7 +129,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand memcpy(pDstTempBuffer, pSrcData, size); tempBufferObject->Unmap(0, nullptr); - commandList->CopyBufferRegion(m_resource.Get(), 0, tempBufferObject, 0, subresourceFootprint.Footprint.Width); + commandList->CopyBufferRegion(m_resource.Get(), 0, tempBufferObject, 0, size); D3D12_RESOURCE_BARRIER barrier = {}; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; diff --git a/tests/RHI/D3D12/integration/sphere/Res/Shader/sphere.hlsl b/tests/RHI/D3D12/integration/sphere/Res/Shader/sphere.hlsl index 04c36e6b..3cc02c4d 100644 --- a/tests/RHI/D3D12/integration/sphere/Res/Shader/sphere.hlsl +++ b/tests/RHI/D3D12/integration/sphere/Res/Shader/sphere.hlsl @@ -8,17 +8,25 @@ struct VSOut { float4 texcoord : TEXCOORD0; }; +Texture2D gDiffuseTexture : register(t0); +SamplerState gSampler : register(s0); + cbuffer MatrixBuffer : register(b0) { - float4x4 gMVP; + float4x4 gProjectionMatrix; + float4x4 gViewMatrix; + float4x4 gModelMatrix; + float4x4 gIT_ModelMatrix; }; VSOut MainVS(Vertex v) { VSOut o; - o.pos = mul(gMVP, v.pos); + float4 positionWS = mul(gModelMatrix, v.pos); + float4 positionVS = mul(gViewMatrix, positionWS); + o.pos = mul(gProjectionMatrix, positionVS); o.texcoord = v.texcoord; return o; } float4 MainPS(VSOut i) : SV_TARGET { - return float4(1.0f, 0.0f, 0.0f, 1.0f); + return gDiffuseTexture.Sample(gSampler, i.texcoord.xy); } \ No newline at end of file diff --git a/tests/RHI/D3D12/integration/sphere/main.cpp b/tests/RHI/D3D12/integration/sphere/main.cpp index 11f32e29..173f6e52 100644 --- a/tests/RHI/D3D12/integration/sphere/main.cpp +++ b/tests/RHI/D3D12/integration/sphere/main.cpp @@ -73,9 +73,6 @@ UINT gIndexCount = 0; float gProjectionMatrix[16]; float gViewMatrix[16]; float gModelMatrix[16]; -float gMVPMatrix[16]; -float gTempMatrix[16]; -float gTransposedMatrix[16]; HWND gHWND = nullptr; int gWidth = 1280; @@ -104,14 +101,23 @@ void IdentityMatrix(float* m) { m[0] = m[5] = m[10] = m[15] = 1.0f; } +void TranslationMatrix(float* m, float x, float y, float z) { + memset(m, 0, 16 * sizeof(float)); + m[0] = 1.0f; m[12] = x; + m[5] = 1.0f; m[13] = y; + m[10] = 1.0f; m[14] = z; + m[15] = 1.0f; +} + void PerspectiveMatrix(float* m, float fov, float aspect, float nearZ, float farZ) { memset(m, 0, 16 * sizeof(float)); float tanHalfFov = tanf(fov / 2.0f); m[0] = 1.0f / (aspect * tanHalfFov); m[5] = 1.0f / tanHalfFov; - m[10] = (farZ + nearZ) / (nearZ - farZ); - m[11] = -1.0f; - m[14] = (2.0f * farZ * nearZ) / (nearZ - farZ); + m[10] = farZ / (farZ - nearZ); + m[11] = 1.0f; + m[14] = -(farZ * nearZ) / (farZ - nearZ); + m[15] = 0.0f; } void LookAtMatrix(float* m, const float* eye, const float* target, const float* up) { @@ -360,7 +366,7 @@ bool InitD3D12() { psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; psoDesc.RasterizerState.FrontCounterClockwise = FALSE; psoDesc.RasterizerState.DepthClipEnable = TRUE; - psoDesc.DepthStencilState.DepthEnable = FALSE; + psoDesc.DepthStencilState.DepthEnable = TRUE; psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS; psoDesc.BlendState.RenderTarget[0].BlendEnable = FALSE; @@ -396,19 +402,18 @@ bool InitD3D12() { } gIndexBuffer.SetBufferType(BufferType::Index); - PerspectiveMatrix(gProjectionMatrix, 45.0f * 3.14159f / 180.0f, (float)gWidth / (float)gHeight, 0.1f, 100.0f); + float aspect = 1280.0f / 720.0f; + PerspectiveMatrix(gProjectionMatrix, 45.0f * 3.141592f / 180.0f, aspect, 0.1f, 1000.0f); + IdentityMatrix(gViewMatrix); + TranslationMatrix(gModelMatrix, 0.0f, 0.0f, 5.0f); - float eye[3] = { 0.0f, 0.0f, 3.0f }; - float target[3] = { 0.0f, 0.0f, 0.0f }; - float up[3] = { 0.0f, 1.0f, 0.0f }; - LookAtMatrix(gViewMatrix, eye, target, up); + float matrices[64]; + memcpy(matrices, gProjectionMatrix, 64); + memcpy(matrices + 16, gViewMatrix, 64); + memcpy(matrices + 32, gModelMatrix, 64); + memcpy(matrices + 48, gModelMatrix, 64); - IdentityMatrix(gModelMatrix); - MultiplyMatrix(gTempMatrix, gViewMatrix, gModelMatrix); - MultiplyMatrix(gMVPMatrix, gProjectionMatrix, gTempMatrix); - TransposeMatrix(gTransposedMatrix, gMVPMatrix); - - gMVPBuffer.InitializeWithData(device, gCommandList.GetCommandList(), gTransposedMatrix, sizeof(gTransposedMatrix), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER); + gMVPBuffer.InitializeWithData(device, gCommandList.GetCommandList(), matrices, sizeof(matrices), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER); if (!LoadTexture("Res/Image/earth.png", gDiffuseTexture, gDiffuseSRV, device, gSRVHeap, gCommandList.GetCommandList(), gCommandAllocator, gCommandQueue)) { Log("[ERROR] Failed to load texture");