Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Fix for lighting issue most obvious when changing view rapidly.
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhux-intel committed Dec 10, 2022
1 parent 88e0426 commit 5df1a26
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
66 changes: 39 additions & 27 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const FLOAT Scene::m_clearColor[4] = { 0, 0, 0.05f, 0 };

enum class DescriptorHeapOffsets
{
FRAME_CBV, // b0
FRAME_CBV0, // b0, one for each swap chain buffer
FRAME_CBV1,
FRAME_CBV2,
GUI,
SHARED_MIN_MIP_MAP,

Expand Down Expand Up @@ -234,7 +236,10 @@ Scene::~Scene()
}

::CloseHandle(m_renderFenceEvent);
m_frameConstantBuffer->Unmap(0, nullptr);
for (auto& b : m_frameConstantBuffers)
{
b->Unmap(0, nullptr);
}

delete m_pGpuTimer;
delete m_pGui;
Expand Down Expand Up @@ -1005,27 +1010,34 @@ void Scene::CreateConstantBuffers()

const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
const auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(bufferSize);
ThrowIfFailed(m_device->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&m_frameConstantBuffer)));

CD3DX12_RANGE readRange(0, bufferSize);
ThrowIfFailed(m_frameConstantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pFrameConstantData)));
m_frameConstantBuffers.resize(m_swapBufferCount);
m_pFrameConstantData.resize(m_swapBufferCount);

m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.538732767f, 0.787301660f, 0.299871892f, 0);
XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir)));
m_pFrameConstantData->g_lightColor = XMFLOAT4(1, 1, 1, 1);
m_pFrameConstantData->g_specularColor = XMFLOAT4(1, 1, 1, 50.f);

D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferView = {};
constantBufferView.SizeInBytes = bufferSize;
constantBufferView.BufferLocation = m_frameConstantBuffer->GetGPUVirtualAddress();
m_device->CreateConstantBufferView(&constantBufferView, CD3DX12_CPU_DESCRIPTOR_HANDLE(
m_srvHeap->GetCPUDescriptorHandleForHeapStart(), (UINT)DescriptorHeapOffsets::FRAME_CBV, m_srvUavCbvDescriptorSize));
for (UINT i = 0; i < m_swapBufferCount; i++)
{
ThrowIfFailed(m_device->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&m_frameConstantBuffers[i])));

CD3DX12_RANGE readRange(0, bufferSize);
ThrowIfFailed(m_frameConstantBuffers[i]->Map(0, &readRange, reinterpret_cast<void**>(&m_pFrameConstantData[i])));

m_pFrameConstantData[i]->g_lightDir = XMFLOAT4(-0.538732767f, 0.787301660f, 0.299871892f, 0);
XMStoreFloat4(&m_pFrameConstantData[i]->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData[i]->g_lightDir)));
m_pFrameConstantData[i]->g_lightColor = XMFLOAT4(1, 1, 1, 1);
m_pFrameConstantData[i]->g_specularColor = XMFLOAT4(1, 1, 1, 50.f);

D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferView = {};
constantBufferView.SizeInBytes = bufferSize;
constantBufferView.BufferLocation = m_frameConstantBuffers[i]->GetGPUVirtualAddress();
m_device->CreateConstantBufferView(&constantBufferView, CD3DX12_CPU_DESCRIPTOR_HANDLE(
m_srvHeap->GetCPUDescriptorHandleForHeapStart(), i + (UINT)DescriptorHeapOffsets::FRAME_CBV0, m_srvUavCbvDescriptorSize));
}
}
}

Expand Down Expand Up @@ -1148,7 +1160,7 @@ void Scene::DrawObjects()

SceneObjects::DrawParams drawParams;
drawParams.m_sharedMinMipMap = CD3DX12_GPU_DESCRIPTOR_HANDLE(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), (UINT)DescriptorHeapOffsets::SHARED_MIN_MIP_MAP, m_srvUavCbvDescriptorSize);
drawParams.m_constantBuffers = CD3DX12_GPU_DESCRIPTOR_HANDLE(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), (UINT)DescriptorHeapOffsets::FRAME_CBV, m_srvUavCbvDescriptorSize);
drawParams.m_constantBuffers = CD3DX12_GPU_DESCRIPTOR_HANDLE(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), m_frameIndex + (UINT)DescriptorHeapOffsets::FRAME_CBV0, m_srvUavCbvDescriptorSize);
drawParams.m_samplers = m_samplerHeap->GetGPUDescriptorHandleForHeapStart();
drawParams.m_projection = m_projection;
drawParams.m_view = m_viewMatrix;
Expand Down Expand Up @@ -1510,17 +1522,17 @@ void Scene::StartScene()
m_commandList->RSSetScissorRects(1, &m_scissorRect);

SetSampler();
DirectX::XMStoreFloat4(&m_pFrameConstantData->g_eyePos, m_viewMatrixInverse.r[3]);
m_pFrameConstantData->g_visualizeFeedback = m_args.m_visualizeMinMip;
DirectX::XMStoreFloat4(&m_pFrameConstantData[m_frameIndex]->g_eyePos, m_viewMatrixInverse.r[3]);
m_pFrameConstantData[m_frameIndex]->g_visualizeFeedback = m_args.m_visualizeMinMip;

if (m_args.m_lightFromView)
{
XMStoreFloat4(&m_pFrameConstantData->g_lightDir, m_viewMatrixInverse.r[2]);
XMStoreFloat4(&m_pFrameConstantData[m_frameIndex]->g_lightDir, m_viewMatrixInverse.r[2]);
}
else
{
m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.538732767f, -0.787301660f, -0.299871892f, 0);
XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir)));
m_pFrameConstantData[m_frameIndex]->g_lightDir = XMFLOAT4(-0.538732767f, -0.787301660f, -0.299871892f, 0);
XMStoreFloat4(&m_pFrameConstantData[m_frameIndex]->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData[m_frameIndex]->g_lightDir)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class Scene
int g_visualizeFeedback;
};

FrameConstantData* m_pFrameConstantData{ nullptr }; // left in the mapped state
ComPtr<ID3D12Resource> m_frameConstantBuffer;
std::vector<FrameConstantData*> m_pFrameConstantData{ nullptr }; // left in the mapped state
std::vector<ComPtr<ID3D12Resource>> m_frameConstantBuffers;

Gui* m_pGui{ nullptr };
Gui::ButtonChanges m_uiButtonChanges; // track changes in UI settings
Expand Down

0 comments on commit 5df1a26

Please sign in to comment.