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

Commit 9998b3e

Browse files
Minor tweaks, including fix to not cull base "terrain" object textures.
1 parent 31ce7b6 commit 9998b3e

File tree

12 files changed

+82
-65
lines changed

12 files changed

+82
-65
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ ID3D12CommandList* pCommandLists[] = { commandLists.m_beforeDrawCommands, m_comm
275275
m_commandQueue->ExecuteCommandLists(_countof(pCommandLists), pCommandLists);
276276
```
277277
278+
## Log
279+
280+
- 2021-06-21: initial commit
281+
- 2021-07-23: use windows events to reduce cpu overhead
282+
- 2021-08-10: use WaitOnAddress to further reduce cpu overhead. some 16k x 16k textures (BC7 format) posted as "release 1".
283+
- 2021-08-28: proof-of-concept culling: textures for objects behind view are evicted
284+
- 2021-09-20: fixed race condition that could result in hang on exit
285+
- 2021-10-21: code refactor to improve sampler feedback streaming library API
286+
- 2021-12-03: added BC1 asset collection as "release 2." All texture assets (.xet files) can reside in the same directory despite format differences, and can co-exist in the same GPU heap. Also minor source tweaks, including fix to not cull base "terrain" object.
287+
278288
## License
279289
280290
Sample and its code provided under MIT license, please see [LICENSE](/LICENSE). All third-party source code provided under their own respective and MIT-compatible Open Source licenses.

TileUpdateManager/DataUploader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void Streaming::DataUploader::StopThreads()
190190
//-----------------------------------------------------------------------------
191191
void Streaming::DataUploader::FlushCommands()
192192
{
193-
DebugPrint(m_updateListFreeCount.load(), " DU flush\n");
193+
DebugPrint("DataUploader Flush ", m_updateListFreeCount.load(), "/", m_updateLists.size(), " batches freed\n");
194194
while (m_updateListFreeCount.load() < m_updateLists.size())
195195
{
196196
_mm_pause();

src/Gui.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// NOTE: this doesn't allocate any resources. it relies on calling function to set any heaps
4040
//-----------------------------------------------------------------------------
4141
Gui::Gui(HWND in_hWnd, ID3D12Device* in_pDevice,
42-
ID3D12DescriptorHeap* in_pSrvHeap, const UINT in_rootSigSlot,
42+
ID3D12DescriptorHeap* in_pSrvHeap, const UINT in_descriptorHeapOffset,
4343
const UINT in_swapChainBufferCount, const DXGI_FORMAT in_swapChainFormat,
4444
const std::wstring& in_adapterDescription, CommandLineArgs& in_args) :
4545
m_initialArgs(in_args)
@@ -50,16 +50,16 @@ Gui::Gui(HWND in_hWnd, ID3D12Device* in_pDevice,
5050
{
5151
IMGUI_CHECKVERSION();
5252
ImGui::CreateContext();
53-
ImGuiIO& io = ImGui::GetIO(); (void)io;
53+
ImGuiIO& io = ImGui::GetIO();
5454
io.ConfigFlags |= ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos; // Enable Keyboard Controls
5555

5656
ImGui::StyleColorsDark();
5757
ImGui_ImplWin32_Init(in_hWnd);
5858

5959
CD3DX12_CPU_DESCRIPTOR_HANDLE cpu(in_pSrvHeap->GetCPUDescriptorHandleForHeapStart(),
60-
in_rootSigSlot, in_pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
60+
in_descriptorHeapOffset, in_pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
6161
CD3DX12_GPU_DESCRIPTOR_HANDLE gpu(in_pSrvHeap->GetGPUDescriptorHandleForHeapStart(),
62-
in_rootSigSlot, in_pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
62+
in_descriptorHeapOffset, in_pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
6363

6464
ImGui_ImplDX12_Init(in_pDevice, in_swapChainBufferCount, in_swapChainFormat, in_pSrvHeap, cpu, gpu);
6565
ImGui_ImplDX12_CreateDeviceObjects();

src/Gui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Gui
3434
{
3535
public:
3636
Gui(HWND in_hWnd, ID3D12Device* in_pDevice,
37-
ID3D12DescriptorHeap* in_pSrvHeap, const UINT in_rootSigSlot,
37+
ID3D12DescriptorHeap* in_pSrvHeap, const UINT in_descriptorHeapOffset,
3838
const UINT in_swapChainBufferCount, const DXGI_FORMAT in_swapChainFormat,
3939
const std::wstring& in_adapterDescription, CommandLineArgs& in_args);
4040
~Gui();

src/Scene.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,30 +275,26 @@ void Scene::MoveView(int in_x, int in_y, int in_z)
275275
//-----------------------------------------------------------------------------
276276
void Scene::RotateView(float in_x, float in_y, float in_z)
277277
{
278-
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(in_x, 0, in_z);
278+
XMMATRIX rotation;
279279

280-
if (in_y)
280+
// NOTE: locking the "up" axis feels great when navigating the terrain
281+
// however, it breaks the controls when flying to other planets
282+
if (m_args.m_cameraUpLock)
281283
{
282-
// NOTE: locking the "up" axis feels great when navigating the terrain
283-
// however, it breaks the controls when flying to other planets
284-
XMMATRIX rotY = XMMatrixIdentity();
285-
if (m_args.m_cameraUpLock)
286-
{
287-
// this prevents spin while panning the terrain, but breaks if the user intentionally rotates in Z
288-
rotY = XMMatrixRotationAxis(XMVectorSet(0, 1, 0, 1), in_y);
289-
}
290-
else
291-
{
292-
// this rotates correctly with any z axis rotation, but "up" can drift:
293-
XMVECTOR yAxis = m_viewMatrixInverse.r[1];
294-
rotY = XMMatrixRotationNormal(yAxis, in_y);
295-
}
284+
XMVECTOR yAxis = XMVectorSet(0, 1, 0, 1);
285+
XMMATRIX rotY = XMMatrixRotationAxis(yAxis, in_y);
296286

297287
XMVECTOR xLate = XMVectorSetW(m_viewMatrixInverse.r[3], 0);
298288
rotY = XMMatrixMultiply(XMMatrixTranslationFromVector(-xLate), rotY);
299289
rotY = XMMatrixMultiply(rotY, XMMatrixTranslationFromVector(xLate));
300290

301291
m_viewMatrix = XMMatrixMultiply(rotY, m_viewMatrix);
292+
293+
rotation = XMMatrixRotationRollPitchYaw(in_x, 0, in_z);
294+
}
295+
else
296+
{
297+
rotation = XMMatrixRotationRollPitchYaw(in_x, in_y, in_z);
302298
}
303299

304300
m_viewMatrix = XMMatrixMultiply(m_viewMatrix, rotation);
@@ -989,7 +985,8 @@ void Scene::CreateConstantBuffers()
989985
CD3DX12_RANGE readRange(0, bufferSize);
990986
ThrowIfFailed(m_frameConstantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pFrameConstantData)));
991987

992-
m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.449135751f, 0.656364977f, 0.25f, 0);
988+
m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.538732767f, 0.787301660f, 0.299871892f, 0);
989+
XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir)));
993990
m_pFrameConstantData->g_lightColor = XMFLOAT4(1, 1, 1, 40.0f);
994991
m_pFrameConstantData->g_specColor = XMFLOAT4(1, 1, 1, 1);
995992

@@ -1153,7 +1150,9 @@ void Scene::DrawObjects()
11531150

11541151
// FIXME: want proper frustum culling here
11551152
float w = XMVectorGetW(o->GetCombinedMatrix().r[3]);
1156-
bool visible = (w > 0) || (o == m_pSky);
1153+
// never cull the sky
1154+
// also never cull the terrain object, or will see incorrect behavior when inspecting closely
1155+
bool visible = (w > 0) || (o == m_pSky) || (o == m_pTerrainSceneObject);
11571156

11581157
// get sampler feedback for this object?
11591158
bool queueFeedback = false;
@@ -1507,6 +1506,7 @@ void Scene::StartScene()
15071506
else
15081507
{
15091508
m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.449135751f, 0.656364977f, 0.25f, 0);
1509+
XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir)));
15101510
}
15111511
}
15121512

@@ -1531,7 +1531,7 @@ void Scene::DrawUI(float in_cpuProcessFeedbackTime)
15311531
UINT numMips = areaHeight / (UINT)minDim;
15321532
if (numMips > 1)
15331533
{
1534-
DirectX::XMFLOAT2 windowPos = DirectX::XMFLOAT2(m_viewport.Width - minDim, minDim);
1534+
DirectX::XMFLOAT2 windowPos = DirectX::XMFLOAT2(m_viewport.Width - minDim, 0);
15351535
m_pTextureViewer->Draw(m_commandList.Get(), windowPos, windowSize,
15361536
m_viewport,
15371537
m_args.m_visualizationBaseMip, numMips - 1,
@@ -1541,7 +1541,7 @@ void Scene::DrawUI(float in_cpuProcessFeedbackTime)
15411541
else
15421542
{
15431543
UINT numMips = UINT(m_viewport.Width) / (UINT)minDim;
1544-
DirectX::XMFLOAT2 windowPos = DirectX::XMFLOAT2(0, minDim);
1544+
DirectX::XMFLOAT2 windowPos = DirectX::XMFLOAT2(0, 0);
15451545
m_pTextureViewer->Draw(m_commandList.Get(), windowPos, windowSize,
15461546
m_viewport,
15471547
m_args.m_visualizationBaseMip, numMips,

src/SceneObject.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ void SceneObjects::BaseObject::SetModelConstants(ModelConstantData& out_modelCon
248248
{
249249
out_modelConstantData.g_combinedTransform = m_combinedMatrix;
250250

251-
DirectX::XMVECTOR pDet;
252-
DirectX::XMMATRIX worldInverse = XMMatrixInverse(&pDet, m_matrix);
253-
out_modelConstantData.g_worldTransform = worldInverse;
251+
out_modelConstantData.g_worldTransform = m_matrix;
254252

255-
DirectX::XMVECTOR vEyePt = XMVector4Transform(in_viewInverse.r[3], worldInverse);
253+
DirectX::XMVECTOR vEyePt = in_viewInverse.r[3];
256254

257255
DirectX::XMStoreFloat4(&(out_modelConstantData.g_eyePos), vEyePt);
258256

@@ -689,8 +687,6 @@ void SceneObjects::Sky::SetModelConstants(ModelConstantData& out_modelConstantDa
689687

690688
out_modelConstantData.g_combinedTransform = m_matrix * view * in_projection;
691689

692-
DirectX::XMVECTOR pDet;
693-
DirectX::XMMATRIX worldInverse = XMMatrixInverse(&pDet, m_matrix);
694690
out_modelConstantData.g_worldTransform = DirectX::XMMatrixIdentity();
695691

696692
out_modelConstantData.g_minmipmapWidth = m_pStreamingResource->GetMinMipMapWidth();

src/TextureViewer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void TextureViewer::DrawWindows(ID3D12GraphicsCommandList* in_pCL, D3D12_VIEWPOR
265265
}
266266

267267
//-----------------------------------------------------------------------------
268-
// note: screen space is -1,-1 to 1,1
268+
// draw the rectangle
269269
//-----------------------------------------------------------------------------
270270
void TextureViewer::Draw(ID3D12GraphicsCommandList* in_pCL,
271271
DirectX::XMFLOAT2 in_position, DirectX::XMFLOAT2 in_windowDim,
@@ -284,8 +284,8 @@ void TextureViewer::Draw(ID3D12GraphicsCommandList* in_pCL,
284284
}
285285

286286
ConstantBuffer* pConstants = (ConstantBuffer*)m_constants.data();
287-
pConstants->x = 2 * float(in_position.x) / in_viewPort.Width;
288-
pConstants->y = 2 * float(in_position.y) / in_viewPort.Height;
287+
pConstants->x = float(in_position.x) / in_viewPort.Width;
288+
pConstants->y = float(in_position.y) / in_viewPort.Height;
289289

290290
pConstants->width = in_windowDim.x / in_viewPort.Width;
291291
pConstants->height = in_windowDim.y / in_viewPort.Height;

src/TextureViewer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
#pragma once
2828

29-
// creates a number of windows showing mips of a provided texture
29+
// creates a windows per mip of a texture
30+
// screen coordinate system: (0, 0) is bottom-left. like normalized device space, (1, 1) is top-right
31+
// u,v coordinates: (0, 0) is top-left. like images, byte 0 is top left
3032

3133
class TextureViewer
3234
{

src/TextureViewer.hlsl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct VS_OUT
2828
{
2929
float4 pos : SV_POSITION;
3030
float2 uv : TEXCOORD0;
31-
nointerpolation float mipLevel: OUTPUT;
31+
nointerpolation float mipLevel : OUTPUT;
3232
};
3333

3434
cbuffer cb0
@@ -41,39 +41,46 @@ cbuffer cb0
4141

4242
VS_OUT vs(uint vertexID : SV_VertexID)
4343
{
44-
VS_OUT output;
44+
// remember: normalized screen space is [-1,-1] [1,1], so everything is doubled
4545

4646
float level = vertexID >> 2;
4747
vertexID &= 3;
48-
// normalized screen space is [-1,-1] [1,1], so everything is doubled
49-
// this forms a rect 0,0 2,0 0,2 2,2
50-
float2 grid = float2((vertexID & 1) << 1, vertexID & 2);
48+
49+
// this forms a rect 0,0 2,0 0,2 2,2 : tri-strip starting clockwise
50+
//float2 grid = float2((vertexID & 1) << 1, vertexID & 2);
51+
52+
// this forms a rect 0,0 0,2 2,0 2,2 : tri-strip starting counter-clockwise
53+
float2 grid = float2(vertexID & 2, (vertexID & 1) << 1);
5154

5255
float width = g_viewPosition.z;
53-
float height = -g_viewPosition.w;
56+
float height = g_viewPosition.w;
5457

55-
// scale and shift window to bottom left of screen
56-
output.pos = float4(grid * float2(width,height) + float2(-1.0f, -1.0), 0.0f, 1.0f);
58+
VS_OUT output;
59+
60+
// scale window and translate to bottom left of screen
61+
output.pos = float4((grid * float2(width, height)) + float2(-1.0f, -1.0), 0.0f, 1.0f);
5762

5863
// horizontal or vertical arrangement
5964
if (g_vertical)
6065
{
61-
height -= g_gap;
66+
height += g_gap;
6267

63-
output.pos.x += g_viewPosition.x;
64-
output.pos.y += g_viewPosition.y - (height * level * 2);
68+
output.pos.x += 2 * g_viewPosition.x;
69+
output.pos.y += 2 * (g_viewPosition.y + (height * level));
6570
}
6671
else
6772
{
6873
width += g_gap;
6974

70-
output.pos.x += g_viewPosition.x + (width * level * 2);
71-
output.pos.y += g_viewPosition.y;
75+
output.pos.x += 2 * (g_viewPosition.x + (width * level));
76+
output.pos.y += 2 * g_viewPosition.y;
7277
}
7378

7479
// uv from 0,0 to 1,1
7580
output.uv.xy = 0.5f * grid;
81+
output.uv.y = 1 - output.uv.y; // the window has 0,0 as bottom-left. the image u,v should have v = 0 in the top-left.
7682
output.mipLevel = g_visBaseMip + level;
83+
7784
return output;
7885
}
7986

@@ -84,5 +91,5 @@ float4 ps(VS_OUT input) : SV_Target
8491
{
8592
float4 diffuse = g_texture2D.SampleLevel(g_sampler, input.uv.xy, input.mipLevel);
8693

87-
return float4(diffuse.xyz, 1.0f);
94+
return float4(diffuse.xyz, 1.0f);
8895
}

src/shaders/terrainPS.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ SamplerState g_sampler : register(s0);
3838
float3 evaluateLight(in float3 normal, in float3 reflected)
3939
{
4040
// directional light
41-
float3 pointToLight = normalize(g_lightDir.xyz);
41+
float3 pointToLight = g_lightDir.xyz;
4242

43-
float diffuse = saturate(dot(g_lightDir.xyz, normal));
43+
float diffuse = saturate(dot(pointToLight, normal));
4444

4545
float specDot = saturate(dot(reflected, pointToLight));
4646
float specular = pow(specDot, 2 * g_lightColor.a);

0 commit comments

Comments
 (0)