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
This commit is contained in:
@@ -99,7 +99,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand
|
|||||||
D3D12_RESOURCE_DESC d3d12TempResourceDesc = {};
|
D3D12_RESOURCE_DESC d3d12TempResourceDesc = {};
|
||||||
d3d12TempResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
d3d12TempResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||||
d3d12TempResourceDesc.Alignment = 0;
|
d3d12TempResourceDesc.Alignment = 0;
|
||||||
d3d12TempResourceDesc.Width = memorySizeUsed;
|
d3d12TempResourceDesc.Width = size;
|
||||||
d3d12TempResourceDesc.Height = 1;
|
d3d12TempResourceDesc.Height = 1;
|
||||||
d3d12TempResourceDesc.DepthOrArraySize = 1;
|
d3d12TempResourceDesc.DepthOrArraySize = 1;
|
||||||
d3d12TempResourceDesc.MipLevels = 1;
|
d3d12TempResourceDesc.MipLevels = 1;
|
||||||
@@ -129,7 +129,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand
|
|||||||
memcpy(pDstTempBuffer, pSrcData, size);
|
memcpy(pDstTempBuffer, pSrcData, size);
|
||||||
tempBufferObject->Unmap(0, nullptr);
|
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 = {};
|
D3D12_RESOURCE_BARRIER barrier = {};
|
||||||
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||||
|
|||||||
@@ -8,17 +8,25 @@ struct VSOut {
|
|||||||
float4 texcoord : TEXCOORD0;
|
float4 texcoord : TEXCOORD0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Texture2D gDiffuseTexture : register(t0);
|
||||||
|
SamplerState gSampler : register(s0);
|
||||||
|
|
||||||
cbuffer MatrixBuffer : register(b0) {
|
cbuffer MatrixBuffer : register(b0) {
|
||||||
float4x4 gMVP;
|
float4x4 gProjectionMatrix;
|
||||||
|
float4x4 gViewMatrix;
|
||||||
|
float4x4 gModelMatrix;
|
||||||
|
float4x4 gIT_ModelMatrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
VSOut MainVS(Vertex v) {
|
VSOut MainVS(Vertex v) {
|
||||||
VSOut o;
|
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;
|
o.texcoord = v.texcoord;
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
float4 MainPS(VSOut i) : SV_TARGET {
|
float4 MainPS(VSOut i) : SV_TARGET {
|
||||||
return float4(1.0f, 0.0f, 0.0f, 1.0f);
|
return gDiffuseTexture.Sample(gSampler, i.texcoord.xy);
|
||||||
}
|
}
|
||||||
@@ -73,9 +73,6 @@ UINT gIndexCount = 0;
|
|||||||
float gProjectionMatrix[16];
|
float gProjectionMatrix[16];
|
||||||
float gViewMatrix[16];
|
float gViewMatrix[16];
|
||||||
float gModelMatrix[16];
|
float gModelMatrix[16];
|
||||||
float gMVPMatrix[16];
|
|
||||||
float gTempMatrix[16];
|
|
||||||
float gTransposedMatrix[16];
|
|
||||||
|
|
||||||
HWND gHWND = nullptr;
|
HWND gHWND = nullptr;
|
||||||
int gWidth = 1280;
|
int gWidth = 1280;
|
||||||
@@ -104,14 +101,23 @@ void IdentityMatrix(float* m) {
|
|||||||
m[0] = m[5] = m[10] = m[15] = 1.0f;
|
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) {
|
void PerspectiveMatrix(float* m, float fov, float aspect, float nearZ, float farZ) {
|
||||||
memset(m, 0, 16 * sizeof(float));
|
memset(m, 0, 16 * sizeof(float));
|
||||||
float tanHalfFov = tanf(fov / 2.0f);
|
float tanHalfFov = tanf(fov / 2.0f);
|
||||||
m[0] = 1.0f / (aspect * tanHalfFov);
|
m[0] = 1.0f / (aspect * tanHalfFov);
|
||||||
m[5] = 1.0f / tanHalfFov;
|
m[5] = 1.0f / tanHalfFov;
|
||||||
m[10] = (farZ + nearZ) / (nearZ - farZ);
|
m[10] = farZ / (farZ - nearZ);
|
||||||
m[11] = -1.0f;
|
m[11] = 1.0f;
|
||||||
m[14] = (2.0f * farZ * nearZ) / (nearZ - farZ);
|
m[14] = -(farZ * nearZ) / (farZ - nearZ);
|
||||||
|
m[15] = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LookAtMatrix(float* m, const float* eye, const float* target, const float* up) {
|
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.CullMode = D3D12_CULL_MODE_NONE;
|
||||||
psoDesc.RasterizerState.FrontCounterClockwise = FALSE;
|
psoDesc.RasterizerState.FrontCounterClockwise = FALSE;
|
||||||
psoDesc.RasterizerState.DepthClipEnable = TRUE;
|
psoDesc.RasterizerState.DepthClipEnable = TRUE;
|
||||||
psoDesc.DepthStencilState.DepthEnable = FALSE;
|
psoDesc.DepthStencilState.DepthEnable = TRUE;
|
||||||
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
|
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
|
||||||
psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
||||||
psoDesc.BlendState.RenderTarget[0].BlendEnable = FALSE;
|
psoDesc.BlendState.RenderTarget[0].BlendEnable = FALSE;
|
||||||
@@ -396,19 +402,18 @@ bool InitD3D12() {
|
|||||||
}
|
}
|
||||||
gIndexBuffer.SetBufferType(BufferType::Index);
|
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 matrices[64];
|
||||||
float target[3] = { 0.0f, 0.0f, 0.0f };
|
memcpy(matrices, gProjectionMatrix, 64);
|
||||||
float up[3] = { 0.0f, 1.0f, 0.0f };
|
memcpy(matrices + 16, gViewMatrix, 64);
|
||||||
LookAtMatrix(gViewMatrix, eye, target, up);
|
memcpy(matrices + 32, gModelMatrix, 64);
|
||||||
|
memcpy(matrices + 48, gModelMatrix, 64);
|
||||||
|
|
||||||
IdentityMatrix(gModelMatrix);
|
gMVPBuffer.InitializeWithData(device, gCommandList.GetCommandList(), matrices, sizeof(matrices), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
||||||
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);
|
|
||||||
|
|
||||||
if (!LoadTexture("Res/Image/earth.png", gDiffuseTexture, gDiffuseSRV, device, gSRVHeap, gCommandList.GetCommandList(), gCommandAllocator, gCommandQueue)) {
|
if (!LoadTexture("Res/Image/earth.png", gDiffuseTexture, gDiffuseSRV, device, gSRVHeap, gCommandList.GetCommandList(), gCommandAllocator, gCommandQueue)) {
|
||||||
Log("[ERROR] Failed to load texture");
|
Log("[ERROR] Failed to load texture");
|
||||||
|
|||||||
Reference in New Issue
Block a user