Skip to content

Commit dfa775f

Browse files
committed
add Imgui, fix bugs
1 parent 77224c2 commit dfa775f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+54906
-310
lines changed

Client/Client.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<ClCompile>
124124
<WarningLevel>Level3</WarningLevel>
125125
<SDLCheck>true</SDLCheck>
126-
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
126+
<PreprocessorDefinitions>_DEBUG;_WINDOWS;_GAMING_DESKTOP;__WRL_NO_DEFAULT_LIB__;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127127
<ConformanceMode>true</ConformanceMode>
128128
<AdditionalIncludeDirectories>../Renderer;../Tracy;../ModelView;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
129129
<LanguageStandard>stdcpp17</LanguageStandard>
@@ -147,7 +147,7 @@
147147
<FunctionLevelLinking>true</FunctionLevelLinking>
148148
<IntrinsicFunctions>true</IntrinsicFunctions>
149149
<SDLCheck>true</SDLCheck>
150-
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
150+
<PreprocessorDefinitions>NDEBUG;_DEBUG;_WINDOWS;_GAMING_DESKTOP;__WRL_NO_DEFAULT_LIB__;%(PreprocessorDefinitions)</PreprocessorDefinitions>
151151
<ConformanceMode>true</ConformanceMode>
152152
<AdditionalIncludeDirectories>../Renderer;../Tracy;../ModelView;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
153153
<LanguageStandard>stdcpp17</LanguageStandard>

Client/Client.vcxproj.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<ShowAllFiles>false</ShowAllFiles>
4+
<ShowAllFiles>true</ShowAllFiles>
55
</PropertyGroup>
66
</Project>

Client/Main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Mesh.h"
77
#include "MeshRenderer.h"
88
#include "Texture.h"
9+
#include "ImGui/imgui.h"
910

1011
static glTF::Asset sAsset;
1112
static Scene* sScenePtr;
@@ -54,6 +55,25 @@ namespace GameApp
5455
MaterialManager::GetInstance()->Update();
5556

5657
sScenePtr->Update(deltaTime);
58+
59+
static unsigned counter = 0;
60+
ImGui::ShowDemoWindow();
61+
62+
//ImGui::Begin("Hello, world!");
63+
//ImGui::Text("This is some useful text.");
64+
//ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
65+
//ImGui::Checkbox("Another Window", &show_another_window);
66+
67+
//ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
68+
//ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
69+
70+
//if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
71+
// counter++;
72+
//ImGui::SameLine();
73+
//ImGui::Text("counter = %d", counter);
74+
75+
//ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ImGui::GetIO().DeltaTime, ImGui::GetIO().Framerate);
76+
//ImGui::End();
5777
}
5878

5979
void SceneGameApp::Cleanup()

ModelView/ConstantBuffer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ struct PBRMaterialConstants
2222
struct
2323
{
2424
// UV0 or UV1 for each texture
25-
uint32_t baseColorUV : 2;
26-
uint32_t metallicRoughnessUV : 2;
27-
uint32_t occlusionUV : 2;
28-
uint32_t emissiveUV : 2;
29-
uint32_t normalUV : 2;
25+
uint32_t baseColorUV : 1;
26+
uint32_t metallicRoughnessUV : 1;
27+
uint32_t emissiveUV : 1;
28+
uint32_t normalUV : 1;
3029

3130
// Three special modes
3231
uint32_t twoSided : 1;
3332
uint32_t alphaTest : 1;
3433
uint32_t alphaBlend : 1;
3534

36-
uint32_t _pad : 3;
35+
uint32_t _pad : 9;
3736

3837
uint32_t alphaRef : 16; // half float
3938
};

ModelView/Material.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class PBRMaterial : public Material
7979
{
8080
kBaseColor,
8181
kMetallicRoughness,
82-
kOcclusion,
8382
kEmissive,
8483
kNormal,
8584
kNumTextures

ModelView/MeshRenderer.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ModelRenderer
2929

3030
SamplerDesc CubeMapSamplerDesc = DefaultSamplerDesc;
3131

32-
sForwardRootSig = GET_RSO(L"MeshRenderer RSO");
32+
sForwardRootSig = GET_RSO(L"MeshRendererForward RSO");
3333
sForwardRootSig->Reset(kNumRootBindings, 3);
3434
sForwardRootSig->InitStaticSampler(10, DefaultSamplerDesc, D3D12_SHADER_VISIBILITY_PIXEL);
3535
sForwardRootSig->InitStaticSampler(11, Graphics::SamplerShadowDesc, D3D12_SHADER_VISIBILITY_PIXEL);
@@ -49,9 +49,9 @@ namespace ModelRenderer
4949
ADD_SHADER("SkyBoxVS", L"MeshRender/SkyBoxVS.hlsl", kVS);
5050
ADD_SHADER("SkyBoxPS", L"MeshRender/SkyBoxPS.hlsl", kPS);
5151
ADD_SHADER("ForwardVS", L"MeshRender/ForwardVS.hlsl", kVS);
52+
ADD_SHADER("ForwardPS", L"MeshRender/ForwardPS.hlsl", kPS, { "REVERSED_Z", "" });
5253
ADD_SHADER("ForwardVSNoSecondUV", L"MeshRender/ForwardVS.hlsl", kVS, { "NO_SECOND_UV", "" });
53-
ADD_SHADER("ForwardPS", L"MeshRender/ForwardPS.hlsl", kPS);
54-
ADD_SHADER("ForwardPSNoSecondUV", L"MeshRender/ForwardPS.hlsl", kPS, { "NO_SECOND_UV", "" });
54+
ADD_SHADER("ForwardPSNoSecondUV", L"MeshRender/ForwardPS.hlsl", kPS, { "NO_SECOND_UV", "", "REVERSED_Z", "" });
5555
ADD_SHADER("DepthOnlyVS", L"MeshRender/DepthOnlyVS.hlsl", kVS);
5656
ADD_SHADER("DepthOnlyPS", L"MeshRender/DepthOnlyPS.hlsl", kPS);
5757
ADD_SHADER("DepthOnlyVSCutOff", L"MeshRender/DepthOnlyVS.hlsl", kVS, { "ENABLE_ALPHATEST", "" });
@@ -258,7 +258,7 @@ void MeshRenderer::AddMesh(const SubMesh& subMesh, const Model* model, float dis
258258
mPassCounts[kOpaque]++;
259259
}*/
260260

261-
SortObject object = { model, meshCBV };
261+
SortObject object = { model, &subMesh, meshCBV };
262262
mSortObjects.push_back(object);
263263
}
264264

@@ -332,31 +332,26 @@ void MeshRenderer::RenderMeshes(GraphicsCommandList& context, GlobalConstants& g
332332
{
333333
SortKey key = mSortKeys[mCurrentDraw];
334334
const SortObject& object = mSortObjects[key.objectIdx];
335-
const Mesh& mesh = *object.model->mMesh;
335+
const Mesh& mesh = *object.model->GetMesh();
336+
const SubMesh& subMesh = *object.subMesh;
337+
const Material& material = *GET_MATERIAL(subMesh.materialIdx);
336338

337-
context.SetConstantBuffer(ModelRenderer::kMeshConstants, object.meshCBV);
338339
context.SetPipelineState(*ModelRenderer::sAllPSOs[key.psoIdx]);
340+
context.SetConstantBuffer(ModelRenderer::kMeshConstants, object.meshCBV);
341+
context.SetConstantBuffer(ModelRenderer::kMaterialConstants, GET_MAT_VPTR(subMesh.materialIdx));
342+
context.SetDescriptorTable(ModelRenderer::kModelTextures, material.GetTextureGpuHandles());
343+
context.SetDescriptorTable(ModelRenderer::kModelTextureSamplers, material.GetSamplerGpuHandles());
344+
context.SetDescriptorTable(ModelRenderer::kSceneTextures, mScene->GetSceneTextureHandles());
339345

340-
for (uint32_t i = 0; i < mesh.subMeshCount; ++i)
341-
{
342-
const SubMesh& subMesh = mesh.subMeshes[i];
343-
const Material& material = *GET_MATERIAL(subMesh.materialIdx);
344-
345-
context.SetConstantBuffer(ModelRenderer::kMaterialConstants, GET_MAT_VPTR(subMesh.materialIdx));
346-
context.SetDescriptorTable(ModelRenderer::kModelTextures, material.GetTextureGpuHandles());
347-
context.SetDescriptorTable(ModelRenderer::kModelTextureSamplers, material.GetSamplerGpuHandles());
348-
context.SetDescriptorTable(ModelRenderer::kSceneTextures, mScene->GetSceneTextureHandles());
349-
350-
if (mCurrentPass == kZPass)
351-
context.SetVertexBuffer(0, { GET_MESH_DepthVB + mesh.vbDepthOffset, mesh.sizeDepthVB, mesh.depthVertexStride });
352-
else
353-
context.SetVertexBuffer(0, { GET_MESH_VB + mesh.vbOffset, mesh.sizeVB, mesh.vertexStride });
346+
if (mCurrentPass == kZPass)
347+
context.SetVertexBuffer(0, { GET_MESH_DepthVB + mesh.vbDepthOffset, mesh.sizeDepthVB, mesh.depthVertexStride });
348+
else
349+
context.SetVertexBuffer(0, { GET_MESH_VB + mesh.vbOffset, mesh.sizeVB, mesh.vertexStride });
354350

355-
DXGI_FORMAT indexFormat = subMesh.index32 ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
356-
context.SetIndexBuffer({ GET_MESH_IB + mesh.ibOffset, mesh.sizeIB, indexFormat });
351+
DXGI_FORMAT indexFormat = subMesh.index32 ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
352+
context.SetIndexBuffer({ GET_MESH_IB + mesh.ibOffset, mesh.sizeIB, indexFormat });
357353

358-
context.DrawIndexed(subMesh.indexCount, subMesh.startIndex, subMesh.baseVertex);
359-
}
354+
context.DrawIndexed(subMesh.indexCount, subMesh.startIndex, subMesh.baseVertex);
360355

361356
++mCurrentDraw;
362357
}

ModelView/MeshRenderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class MeshRenderer
116116
struct SortObject
117117
{
118118
const Model* model;
119+
const SubMesh* subMesh;
119120
D3D12_GPU_VIRTUAL_ADDRESS meshCBV;
120121
};
121122

ModelView/Model.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
#include "Model.h"
22
#include "MeshRenderer.h"
33
#include "Mesh.h"
4+
#include "Scene.h"
45

5-
void Model::Render(MeshRenderer& sorter, const Math::AffineTransform& transform,
6+
void Model::Render(MeshRenderer& renderer, const Math::AffineTransform& transform,
67
D3D12_GPU_VIRTUAL_ADDRESS meshCBV) const
78
{
8-
const Math::Frustum& frustum = sorter.GetViewFrustum();
9-
const Math::AffineTransform& viewMat = (const Math::AffineTransform&)sorter.GetViewMatrix();
9+
const Math::Frustum& frustum = renderer.GetViewFrustum();
10+
const Math::AffineTransform& viewMat = (const Math::AffineTransform&)renderer.GetViewMatrix();
1011

1112
for (uint32_t i = 0; i < mMesh->subMeshCount; ++i)
1213
{
1314
const SubMesh& subMesh = mMesh->subMeshes[i];
1415

1516
Math::BoundingSphere sphereLS((const XMFLOAT4*)subMesh.bounds);
16-
Math::BoundingSphere sphereWS = Math::BoundingSphere(transform * sphereLS.GetCenter(),
17-
sphereWS.GetRadius() * transform.GetUniformScale());
17+
Math::BoundingSphere sphereWS(transform * sphereLS.GetCenter(), sphereLS.GetRadius() * transform.GetUniformScale());
1818
Math::BoundingSphere sphereVS = Math::BoundingSphere(viewMat * sphereWS.GetCenter(), sphereWS.GetRadius());
1919

20-
//if (frustum.IntersectSphere(sphereVS))
2120
//if (subMesh.psoFlags & ePSOFlags::kAlphaTest)
21+
if (frustum.IntersectSphere(sphereVS))
2222
{
2323
float distance = -sphereVS.GetCenter().GetZ() - sphereVS.GetRadius();
24-
sorter.AddMesh(subMesh, this, distance, meshCBV);
24+
renderer.AddMesh(subMesh, this, distance, meshCBV);
2525
}
2626
}
2727
}
28+
29+
Math::BoundingSphere Model::GetWorldBoundingSphere() const
30+
{
31+
const Math::AffineTransform& trans = mScene->GetModelTranform(mCurIndex);
32+
return Math::BoundingSphere(trans * m_BSLS.GetCenter(), trans.GetUniformScale() * m_BSLS.GetRadius());
33+
}

ModelView/Model.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,38 @@
55

66
struct Mesh;
77
class MeshRenderer;
8+
class Scene;
9+
810

911
// as gltf Node
1012
class Model
1113
{
14+
friend class Scene;
1215
public:
1316
Model() {}
1417
~Model() {}
1518

16-
void Render(MeshRenderer& sorter, const Math::AffineTransform& transform,
19+
void Render(MeshRenderer& renderer, const Math::AffineTransform& transform,
1720
D3D12_GPU_VIRTUAL_ADDRESS meshCBV) const;
1821

19-
Math::UniformTransform mLocalTrans;
20-
//Math::XMFLOAT3 position;
21-
//Math::Quaternion rotation;
22-
//Math::XMFLOAT3 scale;
22+
const Mesh* GetMesh() const { return mMesh; }
23+
Math::BoundingSphere GetWorldBoundingSphere() const;
24+
private:
25+
Math::XMFLOAT3 mPosition;
26+
Math::XMFLOAT4 mRotation;
27+
Math::XMFLOAT3 mScale;
2328

2429
uint32_t mParentIndex;
2530
uint32_t mCurIndex;
2631

27-
//Math::BoundingSphere m_BSLS; // local space bounds
28-
Math::BoundingSphere m_BSOS; // object space bounds
29-
//Math::AxisAlignedBox m_BBoxLS; // local space AABB
30-
Math::AxisAlignedBox m_BBoxOS; // object space AABB
32+
Math::BoundingSphere m_BSLS; // local space bounds
33+
//Math::BoundingSphere m_BSOS; // object space bounds
34+
Math::AxisAlignedBox m_BBoxLS; // local space AABB
35+
//Math::AxisAlignedBox m_BBoxOS; // object space AABB
3136

37+
// These variables can be present by index
3238
const Mesh* mMesh;
39+
const Scene* mScene;
3340
bool mHasChildren;
3441
bool mHasSiblings;
3542
};

ModelView/ModelConverter.cpp

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ static uint16_t GetTextureFlag(uint32_t type, bool alpha = false)
2424
return kSRGB | kDefaultBC | kGenerateMipMaps | (alpha ? kPreserveAlpha : 0);
2525
case PBRMaterial::kMetallicRoughness:
2626
return kNoneTextureFlag;
27-
case PBRMaterial::kOcclusion:
28-
return kNoneTextureFlag;
2927
case PBRMaterial::kEmissive:
3028
return kSRGB;
3129
case PBRMaterial::kNormal: // Use BC5 Compression
@@ -327,7 +325,7 @@ namespace ModelConverter
327325
// Local space bounds
328326
Vector3 sphereCenterLS = (Vector3(*(XMFLOAT3*)primitive.minPos) + Vector3(*(XMFLOAT3*)primitive.maxPos)) * 0.5f;
329327
Scalar maxRadiusLSSq(kZero);
330-
AxisAlignedBox aabbLS = AxisAlignedBox(kZero);
328+
AxisAlignedBox aabbLS(kZero);
331329

332330
for (uint32_t v = 0; v < vertexCount/*maxIndex*/; ++v)
333331
{
@@ -339,7 +337,7 @@ namespace ModelConverter
339337
}
340338

341339
XMStoreFloat3((XMFLOAT3*)subMesh.bounds, sphereCenterLS);
342-
subMesh.bounds[3] = maxRadiusLSSq;
340+
subMesh.bounds[3] = Sqrt(maxRadiusLSSq);
343341
XMStoreFloat3(&subMesh.minPos, aabbLS.GetMin());
344342
XMStoreFloat3(&subMesh.maxPos, aabbLS.GetMax());
345343
}
@@ -601,7 +599,7 @@ namespace ModelConverter
601599
GeometryData& geoData = allGeoData.emplace_back(BuildSubMesh(
602600
gltfMesh.primitives[pi], mesh.subMeshes[pi], (ePSOFlags) meshflags));
603601

604-
Utility::PrintMessage("%d %d", mesh.depthVertexStride, geoData.depthVertexStride);
602+
//Utility::PrintMessage("%d %d", mesh.depthVertexStride, geoData.depthVertexStride);
605603
ASSERT(mesh.vertexStride == 0 || mesh.vertexStride == geoData.vertexStride);
606604
ASSERT(mesh.depthVertexStride == 0 || mesh.depthVertexStride == geoData.depthVertexStride);
607605
mesh.vertexStride = geoData.vertexStride;
@@ -666,84 +664,12 @@ namespace ModelConverter
666664
MeshManager::GetInstance()->UpdateMeshes();
667665
}
668666

669-
void WalkGraph(
670-
std::vector<Model>& sceneModels,
671-
const std::vector<glTF::Node*>& siblings,
672-
uint32_t curIndex,
673-
const Math::Matrix4& xform
674-
)
675-
{
676-
using namespace Math;
677-
678-
size_t numSiblings = siblings.size();
679-
for (size_t i = 0; i < numSiblings; ++i)
680-
{
681-
glTF::Node* curNode = siblings[i];
682-
Model& model = sceneModels[curNode->linearIdx];
683-
model.mHasChildren = false;
684-
model.mCurIndex = curNode->linearIdx;
685-
model.mParentIndex = curIndex;
686-
687-
Math::Matrix4 modelXForm;
688-
if (curNode->hasMatrix)
689-
{
690-
CopyMemory((float*)&modelXForm, curNode->matrix, sizeof(curNode->matrix));
691-
}
692-
else
693-
{
694-
Quaternion rot;
695-
XMFLOAT3 scale;
696-
CopyMemory((float*)&rot, curNode->rotation, sizeof(curNode->rotation));
697-
CopyMemory((float*)&scale, curNode->scale, sizeof(curNode->scale));
698-
modelXForm = Matrix4(
699-
Matrix3(rot) * Matrix3::MakeScale(scale),
700-
Vector3(*(const XMFLOAT3*)curNode->translation)
701-
);
702-
}
703-
const AffineTransform& affineTrans = (const AffineTransform&)modelXForm;
704-
Math::Quaternion q(modelXForm);
705-
Math::UniformTransform localTrans(q, affineTrans.GetUniformScale(), affineTrans.GetTranslation());
706-
model.mLocalTrans = localTrans;
707-
708-
const Matrix4 LocalXform = xform * modelXForm;
709-
710-
if (!curNode->pointsToCamera && curNode->mesh != nullptr)
711-
{
712-
model.mMesh = GET_MESH(curNode->mesh->index);
713-
714-
Scalar scaleXSqr = LengthSquare((Vector3)LocalXform.GetX());
715-
Scalar scaleYSqr = LengthSquare((Vector3)LocalXform.GetY());
716-
Scalar scaleZSqr = LengthSquare((Vector3)LocalXform.GetZ());
717-
Scalar sphereScale = Sqrt(Max(Max(scaleXSqr, scaleYSqr), scaleZSqr));
718-
719-
Vector3 sphereCenter(*(Math::XMFLOAT3*)model.mMesh->bounds);
720-
sphereCenter = (Vector3)(LocalXform * sphereCenter);
721-
Scalar sphereRadius = sphereScale * model.mMesh->bounds[3];
722-
model.m_BSOS = Math::BoundingSphere(sphereCenter, sphereRadius);
723-
model.m_BBoxOS = AxisAlignedBox::CreateFromSphere(model.m_BSOS);
724-
}
725-
726-
if (curNode->children.size() > 0)
727-
{
728-
model.mHasChildren = true;
729-
WalkGraph(sceneModels, curNode->children, model.mCurIndex, LocalXform);
730-
}
731-
732-
// Are there more siblings?
733-
if (i + 1 < numSiblings)
734-
{
735-
model.mHasSiblings = true;
736-
}
737-
}
738-
}
739-
740667
void BuildScene(Scene* scene, const glTF::Asset& asset)
741668
{
742-
scene->GetModels().resize(asset.m_nodes.size());
743-
scene->GetModelTranforms().resize(asset.m_nodes.size());
669+
scene->ResizeModels(asset.m_nodes.size());
744670

745671
const glTF::Scene* gltfScene = asset.m_scene;
746672
// only one scene
747-
WalkGraph(scene->GetModels(), gltfScene->nodes, -1, Math::Matrix4(Math::kIdentity));
673+
scene->WalkGraph(gltfScene->nodes, -1, Math::Matrix4(Math::kIdentity));
748674
}
749675
};

0 commit comments

Comments
 (0)