Skip to content

Commit

Permalink
Pull chunk.cpp into dx12helloworld for testing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
quadpixels committed Sep 11, 2023
1 parent 0d45be2 commit 0211842
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 9 deletions.
3 changes: 2 additions & 1 deletion chunkindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ void ChunkGrid::Init(unsigned _xlen, unsigned _ylen, unsigned _zlen) {
}

ChunkGrid::ChunkGrid(const char* vox_fn) {
FILE* f = fopen(vox_fn, "rb");
FILE* f;
fopen_s(&f, vox_fn, "rb");

long long file_size;
fseek(f, 0, SEEK_END);
Expand Down
130 changes: 130 additions & 0 deletions dx12helloworld/DX12ChunksScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "scene.hpp"

#include "d3dx12.h"
#include "utils.hpp"
#include <wrl/client.h>

using Microsoft::WRL::ComPtr;

void WaitForPreviousFrame();
extern ID3D12Device* g_device;

/*
cbuffer CBPerObject : register(b0) {
float4x4 M;
float4x4 V;
float4x4 P;
};
cbuffer CBPerScene : register(b1) {
float3 dir_light;
float4x4 lightPV;
float4 cam_pos;
}
*/

struct PerObjectCB {
DirectX::XMMATRIX M, V, P;
};

struct PerSceneCB {
DirectX::XMVECTOR dir_light;
DirectX::XMMATRIX lightPV;
DirectX::XMVECTOR cam_pos;
};

DX12ChunksScene::DX12ChunksScene() {
InitPipelineAndCommandList();
}

void DX12ChunksScene::InitPipelineAndCommandList() {
CE(g_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&command_allocator)));
CE(g_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT,
command_allocator, nullptr, IID_PPV_ARGS(&command_list)));
CE(command_list->Close());

{
ID3DBlob* error = nullptr;
unsigned compile_flags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
D3DCompileFromFile(L"shaders/default_palette.hlsl", nullptr, nullptr,
"VSMain", "vs_5_0", compile_flags, 0, &default_palette_VS, &error);
if (error) printf("Error compiling VS: %s\n", (char*)(error->GetBufferPointer()));

D3DCompileFromFile(L"shaders/default_palette.hlsl", nullptr, nullptr,
"PSMain", "ps_5_0", compile_flags, 0, &default_palette_PS, &error);
if (error) printf("Error compiling PS: %s\n", (char*)(error->GetBufferPointer()));
}

// Root signature
{
CD3DX12_DESCRIPTOR_RANGE1 ranges[1];

CD3DX12_ROOT_PARAMETER1 rootParameters[2];
rootParameters[0].InitAsConstantBufferView(0, 0,
D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY_VERTEX);
rootParameters[1].InitAsConstantBufferView(1, 0,
D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY_PIXEL);

CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC root_sig_desc;
root_sig_desc.Init_1_1(_countof(rootParameters), rootParameters, 0, NULL,
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
ComPtr<ID3DBlob> signature, error;

HRESULT hr = D3DX12SerializeVersionedRootSignature(&root_sig_desc,
D3D_ROOT_SIGNATURE_VERSION_1_1,
&signature, &error);
if (signature == nullptr) {
printf("Could not serialize root signature: %s\n",
(char*)(error->GetBufferPointer()));
}

CE(g_device->CreateRootSignature(0, signature->GetBufferPointer(),
signature->GetBufferSize(), IID_PPV_ARGS(&root_signature)));
root_signature->SetName(L"Root signature");
}

D3D12_INPUT_ELEMENT_DESC input_element_desc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR" , 0, DXGI_FORMAT_R32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR" , 1, DXGI_FORMAT_R32_FLOAT, 0, 16, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR" , 2, DXGI_FORMAT_R32_FLOAT, 0, 20, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
};

D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {
.pRootSignature = root_signature,
.VS = CD3DX12_SHADER_BYTECODE(default_palette_VS),
.PS = CD3DX12_SHADER_BYTECODE(default_palette_PS),
.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT),
.SampleMask = UINT_MAX,
.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT),
.DepthStencilState = {
.DepthEnable = false,
.StencilEnable = false,
},
.InputLayout = {
input_element_desc,
4
},
.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
.NumRenderTargets = 1,
.RTVFormats = {
DXGI_FORMAT_R8G8B8A8_UNORM,
},
.DSVFormat = DXGI_FORMAT_UNKNOWN,
.SampleDesc = {
.Count = 1,
},
};
CE(g_device->CreateGraphicsPipelineState(&pso_desc, IID_PPV_ARGS(&pipeline_state)));
}

void DX12ChunksScene::InitResources() {
}

void DX12ChunksScene::Render() {
}

void DX12ChunksScene::Update(float secs) {
}

2 changes: 1 addition & 1 deletion dx12helloworld/DX12ClearScreenScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Microsoft::WRL::ComPtr;

extern HWND g_hwnd;
extern unsigned WIN_W, WIN_H;
extern int WIN_W, WIN_H;

extern ID3D12Device* g_device;
extern IDXGIFactory4* g_factory;
Expand Down
2 changes: 1 addition & 1 deletion dx12helloworld/DX12HelloTriangleScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using Microsoft::WRL::ComPtr;

extern unsigned WIN_W, WIN_H;
extern int WIN_W, WIN_H;
extern ID3D12Device* g_device;
extern int g_frame_index;
extern ID3D12CommandQueue* g_command_queue;
Expand Down
15 changes: 13 additions & 2 deletions dx12helloworld/dx12helloworld.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(SolutionDir)\windows_lib\freetype-2.10.0\include;$(SolutionDir)\windows_lib\glm-0.9.9.5;$(SolutionDir)\windows_lib\glew-2.0.0\include;$(SolutionDir)\windows_lib\freeglut\include;$(SolutionDir)\windows_lib\glfw-3.2.1.bin.WIN64\include;$(SolutionDir)\windows_lib\DirectxTK\Inc</IncludePath>
<LibraryPath>$(SolutionDir)\windows_lib\freetype-2.10.0\win64;$(SolutionDir)\windows_lib\freeglut\lib\x64;$(SolutionDir)\windows_lib\glew-2.0.0\lib\Release\x64;$(SolutionDir)\windows_lib\glfw-3.2.1.bin.WIN64\lib-vc2015;$(SolutionDir)\windows_lib\DirectxTK\Bin\x64;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(SolutionDir)\windows_lib\freetype-2.10.0\include;$(SolutionDir)\windows_lib\glm-0.9.9.5;$(SolutionDir)\windows_lib\glew-2.0.0\include;$(SolutionDir)\windows_lib\freeglut\include;$(SolutionDir)\windows_lib\glfw-3.2.1.bin.WIN64\include;$(SolutionDir)\windows_lib\DirectxTK\Inc</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down Expand Up @@ -116,14 +119,15 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WIN32</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d12.lib;d3d11.lib;dxgi.lib;d3dcompiler.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;glfw3.lib;glew32.lib;freetype.lib;d3d11.lib;d3d12.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -135,6 +139,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -145,13 +150,19 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\chunk.cpp" />
<ClCompile Include="..\chunkindex.cpp" />
<ClCompile Include="..\util.cpp" />
<ClCompile Include="DX12ChunksScene.cpp" />
<ClCompile Include="DX12ClearScreenScene.cpp" />
<ClCompile Include="DX12HelloTriangleScene.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\chunk.hpp" />
<ClInclude Include="..\chunkindex.hpp" />
<ClInclude Include="..\util.hpp" />
<ClInclude Include="d3dx12.h" />
<ClInclude Include="scene.hpp" />
<ClInclude Include="utils.hpp" />
Expand Down
18 changes: 18 additions & 0 deletions dx12helloworld/dx12helloworld.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
<ClCompile Include="DX12ChunksScene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\chunk.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\chunkindex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="d3dx12.h">
Expand All @@ -41,5 +50,14 @@
<ClInclude Include="utils.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\chunk.hpp">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\chunkindex.hpp">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\util.hpp">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion dx12helloworld/dx12helloworld.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
17 changes: 14 additions & 3 deletions dx12helloworld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "scene.hpp"
#include "utils.hpp"

unsigned WIN_W = 800, WIN_H = 480;
int WIN_W = 800, WIN_H = 480;
const int FRAME_COUNT = 2;
HWND g_hwnd;

Expand All @@ -28,6 +28,17 @@ int g_frame_index;
static Scene* g_scenes[2];
static int g_scene_idx = 0;

// Override the following functions for DX12
bool IsGL() { return false; }
void UpdateGlobalPerObjectCB(const DirectX::XMMATRIX* M, const DirectX::XMMATRIX* V, const DirectX::XMMATRIX* P) {}
ID3D11Device* g_device11;
ID3D11DeviceContext* g_context11;
ID3D11VertexShader* g_vs_simpletexture;
ID3D11PixelShader* g_ps_simpletexture;
ID3DBlob* g_vs_textrender_blob, * g_ps_textrender_blob;
ID3D11BlendState* g_blendstate11;
ID3D11Buffer* g_simpletexture_cb;

// Shared across all scenes
void InitDeviceAndCommandQ() {
unsigned dxgi_factory_flags = 0;
Expand Down Expand Up @@ -78,8 +89,8 @@ void InitDeviceAndCommandQ() {

void InitSwapChain() {
DXGI_SWAP_CHAIN_DESC1 desc = {
.Width = WIN_W,
.Height = WIN_H,
.Width = (UINT)WIN_W,
.Height = (UINT)WIN_H,
.Format = DXGI_FORMAT_R8G8B8A8_UNORM,
.SampleDesc = {
.Count = 1,
Expand Down
18 changes: 18 additions & 0 deletions dx12helloworld/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <wrl/client.h>
#include <dxgi1_4.h>

#include "chunk.hpp"

class Scene {
public:
virtual void Render() = 0;
Expand Down Expand Up @@ -55,4 +57,20 @@ class DX12HelloTriangleScene : public Scene {
ID3D12Resource* cbvs; // CBV resource, N copies of the CBV for N triangles.
};

class DX12ChunksScene : public Scene {
public:
DX12ChunksScene();
void Render() override;
void Update(float secs) override;
static constexpr int FRAME_COUNT = 2;
private:
void InitPipelineAndCommandList();
void InitResources();
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
ID3DBlob* default_palette_VS, * default_palette_PS;
ID3D12RootSignature* root_signature;
ID3D12PipelineState* pipeline_state;
};

#endif

0 comments on commit 0211842

Please sign in to comment.