浏览代码

feat: 添加cpp文件 修改项目配置

nicetry12138 1 年之前
父节点
当前提交
56d2e0630b

+ 4 - 2
图形学/DirectX学习/src/D3DBox/D3DBox/D3DBox.vcxproj

@@ -102,8 +102,9 @@
     <ClCompile>
       <WarningLevel>Level3</WarningLevel>
       <SDLCheck>true</SDLCheck>
-      <PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <ConformanceMode>true</ConformanceMode>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ConformanceMode>Default</ConformanceMode>
+      <LanguageStandard>Default</LanguageStandard>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
@@ -140,6 +141,7 @@
   <ItemGroup>
     <ClCompile Include="D3dApp.cpp" />
     <ClCompile Include="D3DBox.cpp" />
+    <ClCompile Include="D3DUtil.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="D3DBox.rc" />

+ 6 - 3
图形学/DirectX学习/src/D3DBox/D3DBox/D3DBox.vcxproj.filters

@@ -33,15 +33,15 @@
     <ClInclude Include="DDSTextureLoader.h">
       <Filter>头文件</Filter>
     </ClInclude>
-    <ClInclude Include="D3DUtil.h">
-      <Filter>头文件</Filter>
-    </ClInclude>
     <ClInclude Include="MathHelper.h">
       <Filter>头文件</Filter>
     </ClInclude>
     <ClInclude Include="d3dx12.h">
       <Filter>头文件</Filter>
     </ClInclude>
+    <ClInclude Include="D3DUtil.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="D3DBox.cpp">
@@ -50,6 +50,9 @@
     <ClCompile Include="D3dApp.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="D3DUtil.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="D3DBox.rc">

+ 125 - 0
图形学/DirectX学习/src/D3DBox/D3DBox/D3DUtil.cpp

@@ -0,0 +1,125 @@
+
+#include "D3DUtil.h"
+#include <comdef.h>
+#include <fstream>
+
+using Microsoft::WRL::ComPtr;
+
+DxException::DxException(HRESULT hr, const std::wstring& functionName, const std::wstring& filename, int lineNumber) :
+	ErrorCode(hr),
+	FunctionName(functionName),
+	Filename(filename),
+	LineNumber(lineNumber)
+{
+}
+
+bool d3dUtil::IsKeyDown(int vkeyCode)
+{
+	return (GetAsyncKeyState(vkeyCode) & 0x8000) != 0;
+}
+
+ComPtr<ID3DBlob> d3dUtil::LoadBinary(const std::wstring& filename)
+{
+	std::ifstream fin(filename, std::ios::binary);
+
+	fin.seekg(0, std::ios_base::end);
+	std::ifstream::pos_type size = (int)fin.tellg();
+	fin.seekg(0, std::ios_base::beg);
+
+	ComPtr<ID3DBlob> blob;
+	ThrowIfFailed(D3DCreateBlob(size, blob.GetAddressOf()));
+
+	fin.read((char*)blob->GetBufferPointer(), size);
+	fin.close();
+
+	return blob;
+}
+
+Microsoft::WRL::ComPtr<ID3D12Resource> d3dUtil::CreateDefaultBuffer(
+	ID3D12Device* device,
+	ID3D12GraphicsCommandList* cmdList,
+	const void* initData,
+	UINT64 byteSize,
+	Microsoft::WRL::ComPtr<ID3D12Resource>& uploadBuffer)
+{
+	ComPtr<ID3D12Resource> defaultBuffer;
+
+	// Create the actual default buffer resource.
+	ThrowIfFailed(device->CreateCommittedResource(
+		&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
+		D3D12_HEAP_FLAG_NONE,
+		&CD3DX12_RESOURCE_DESC::Buffer(byteSize),
+		D3D12_RESOURCE_STATE_COMMON,
+		nullptr,
+		IID_PPV_ARGS(defaultBuffer.GetAddressOf())));
+
+	// In order to copy CPU memory data into our default buffer, we need to create
+	// an intermediate upload heap. 
+	ThrowIfFailed(device->CreateCommittedResource(
+		&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
+		D3D12_HEAP_FLAG_NONE,
+		&CD3DX12_RESOURCE_DESC::Buffer(byteSize),
+		D3D12_RESOURCE_STATE_GENERIC_READ,
+		nullptr,
+		IID_PPV_ARGS(uploadBuffer.GetAddressOf())));
+
+
+	// Describe the data we want to copy into the default buffer.
+	D3D12_SUBRESOURCE_DATA subResourceData = {};
+	subResourceData.pData = initData;
+	subResourceData.RowPitch = byteSize;
+	subResourceData.SlicePitch = subResourceData.RowPitch;
+
+	// Schedule to copy the data to the default buffer resource.  At a high level, the helper function UpdateSubresources
+	// will copy the CPU memory into the intermediate upload heap.  Then, using ID3D12CommandList::CopySubresourceRegion,
+	// the intermediate upload heap data will be copied to mBuffer.
+	cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(),
+		D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST));
+	UpdateSubresources<1>(cmdList, defaultBuffer.Get(), uploadBuffer.Get(), 0, 0, 1, &subResourceData);
+	cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(),
+		D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_GENERIC_READ));
+
+	// Note: uploadBuffer has to be kept alive after the above function calls because
+	// the command list has not been executed yet that performs the actual copy.
+	// The caller can Release the uploadBuffer after it knows the copy has been executed.
+
+
+	return defaultBuffer;
+}
+
+ComPtr<ID3DBlob> d3dUtil::CompileShader(
+	const std::wstring& filename,
+	const D3D_SHADER_MACRO* defines,
+	const std::string& entrypoint,
+	const std::string& target)
+{
+	UINT compileFlags = 0;
+#if defined(DEBUG) || defined(_DEBUG)  
+	compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
+#endif
+
+	HRESULT hr = S_OK;
+
+	ComPtr<ID3DBlob> byteCode = nullptr;
+	ComPtr<ID3DBlob> errors;
+	hr = D3DCompileFromFile(filename.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE,
+		entrypoint.c_str(), target.c_str(), compileFlags, 0, &byteCode, &errors);
+
+	if (errors != nullptr)
+		OutputDebugStringA((char*)errors->GetBufferPointer());
+
+	ThrowIfFailed(hr);
+
+	return byteCode;
+}
+
+std::wstring DxException::ToString()const
+{
+	// Get the string description of the error code.
+	_com_error err(ErrorCode);
+	std::wstring msg = err.ErrorMessage();
+
+	return FunctionName + L" failed in " + Filename + L"; line " + std::to_wstring(LineNumber) + L"; error: " + msg;
+}
+
+

+ 24 - 24
图形学/DirectX学习/src/D3DBox/D3DBox/D3dApp.cpp

@@ -7,7 +7,7 @@ using namespace DirectX;
 
 LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
-    // 转发窗口消息
+    // 杞�彂绐楀彛娑堟伅
 	return D3DApp::GetApp()->MsgProc(hwnd, msg, wParam, lParam);
 }
 
@@ -17,7 +17,7 @@ D3DApp::D3DApp(HINSTANCE hInstance)
 {
 	mhAppInst = hInstance;
 
-    // 单例模式 禁止各种方式创建第二个
+    // 鍗曚緥妯″紡 绂佹�鍚勭�鏂瑰紡鍒涘缓绗�簩涓�
 	assert(mApp == nullptr);
     mApp = this;
 }
@@ -25,7 +25,7 @@ D3DApp::D3DApp(HINSTANCE hInstance)
 D3DApp::~D3DApp()
 {
     if (md3dDevice != nullptr) {
-        FlushCommandQueue();    // 清空 GPU 命令
+        FlushCommandQueue();    // 娓呯┖ GPU 鍛戒护
     }
 }
 
@@ -87,11 +87,11 @@ bool D3DApp::Initialize()
 
 LRESULT D3DApp::MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
-    switch (msg)
-    {
-    default:
-        break;
-    }
+	switch (msg)
+	{
+	default:
+		break;
+	}
     return DefWindowProc(hWnd, msg, wParam, lParam);
 }
 
@@ -119,7 +119,7 @@ void D3DApp::OnResize()
 {
 }
 
-// 基本流程: 注册、创建、展示、更新
+// 鍩烘湰娴佺▼锛� 娉ㄥ唽銆佸垱寤恒€佸睍绀恒€佹洿鏂�
 bool D3DApp::InitMainWindow()
 {
 	WNDCLASS wc;
@@ -154,7 +154,7 @@ bool D3DApp::InitMainWindow()
 	}
 
 	ShowWindow(mhMainWnd, SW_SHOW);
-	UpdateWindow(mhMainWnd);	// 迫使系统立即处理那些已经在消息队列中的绘制消息,确保窗口首次创建之后,会被立即正确绘制
+	UpdateWindow(mhMainWnd);	// 杩�娇绯荤粺绔嬪嵆澶勭悊閭d簺宸茬粡鍦ㄦ秷鎭�槦鍒椾腑鐨勭粯鍒舵秷鎭�紝纭�繚绐楀彛棣栨�鍒涘缓涔嬪悗锛屼細琚�珛鍗虫�纭�粯鍒�
 
     return true;
 }
@@ -169,10 +169,10 @@ bool D3DApp::InitDirect3D()
 	}
 #endif
 
-	// 创建 DXGI 
+	// 鍒涘缓 DXGI 
 	ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mdxgiFactory)));
 
-	// 获得光栅器
+	// 鑾峰緱鍏夋爡鍣�
 	HRESULT hardwareResult = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&md3dDevice));
 	if (FAILED(hardwareResult)) {
 		ComPtr<IDXGIAdapter> pWarpAdapter;
@@ -180,15 +180,15 @@ bool D3DApp::InitDirect3D()
 		ThrowIfFailed(D3D12CreateDevice(pWarpAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&md3dDevice)));
 	}
 
-	// 创建围栏
+	// 鍒涘缓鍥存爮
 	ThrowIfFailed(md3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)));
 
-	// 存储描述符大小
+	// 瀛樺偍鎻忚堪绗﹀ぇ灏�
 	mRtvDescriptorSize = md3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
 	mDsvDescriptorSize = md3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
 	mCbvSrvUavDescriptorSize = md3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
 
-	// 设置 4x 多重采样 
+	// 璁剧疆 4x 澶氶噸閲囨牱 
 	D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS msQualityLevels;
 	msQualityLevels.Format = mBackBufferFormat;
 	msQualityLevels.SampleCount = 4;
@@ -253,21 +253,21 @@ void D3DApp::FlushCommandQueue()
 {
 	mCurrentFence++;
 
-	// 发送一个信号到命令队列。当GPU处理到这一点时,它会将围栏对象 mFence 的值设置为 mCurrentFence。这样可以确保所有先前提交的命令都已经被GPU处理完毕
+	// 鍙戦€佷竴涓�俊鍙峰埌鍛戒护闃熷垪銆傚綋GPU澶勭悊鍒拌繖涓€鐐规椂锛屽畠浼氬皢鍥存爮瀵硅薄 mFence 鐨勫€艰�缃�负 mCurrentFence銆傝繖鏍峰彲浠ョ‘淇濇墍鏈夊厛鍓嶆彁浜ょ殑鍛戒护閮藉凡缁忚�GPU澶勭悊瀹屾瘯
 	mCommandQueue->Signal(mFence.Get(), mCurrentFence);
 
 	if (mFence->GetCompletedValue() < mCurrentFence) {
-		// 创建一个事件对象,这个事件用于等待围栏信号的完成
-		HANDLE eventHandl = CreateEventEx(nullptr, false, false, EVENT_ALL_ACCESS);
+		// 鍒涘缓涓€涓�簨浠跺�璞★紝杩欎釜浜嬩欢鐢ㄤ簬绛夊緟鍥存爮淇″彿鐨勫畬鎴�
+		HANDLE eventHandle = CreateEventEx(nullptr, NULL, false, EVENT_ALL_ACCESS);
 
-		// 为围栏设置一个完成事件。当围栏达到指定的 mCurrentFence 值时,eventHandl 事件会被设置为信号状态
-		ThrowIfFailed(mFence->SetEventOnCompletion(mCurrentFence, eventHandl));
+		// 涓哄洿鏍忚�缃�竴涓�畬鎴愪簨浠躲€傚綋鍥存爮杈惧埌鎸囧畾鐨� mCurrentFence 鍊兼椂锛宔ventHandl 浜嬩欢浼氳�璁剧疆涓轰俊鍙风姸鎬�
+		ThrowIfFailed(mFence->SetEventOnCompletion(mCurrentFence, eventHandle));
 
-		// 等待事件对象变为信号状态
-		WaitForSingleObject(eventHandl, INFINITE);
+		// 绛夊緟浜嬩欢瀵硅薄鍙樹负淇″彿鐘舵€�
+		WaitForSingleObject(eventHandle, INFINITE);
 
-		// 关闭事件句柄,释放系统资源
-		CloseHandle(eventHandl);
+		// 鍏抽棴浜嬩欢鍙ユ焺锛岄噴鏀剧郴缁熻祫婧�
+		CloseHandle(eventHandle);
 	}
 }