diff --git a/14-MultiThreadShadow/14-MultiThreadShadow.cpp b/14-MultiThreadShadow/14-MultiThreadShadow.cpp index c7c6bdff8ad42269a65b4a02e361c5b82901e190..b1476e7cc58198c253b6ffd8f08af0a582697a9b 100644 --- a/14-MultiThreadShadow/14-MultiThreadShadow.cpp +++ b/14-MultiThreadShadow/14-MultiThreadShadow.cpp @@ -269,46 +269,52 @@ struct ST_GRS_THREAD_PARAMS ID3D12Resource* m_pICBLights; }; -int g_iWndWidth = 1024; -int g_iWndHeight = 768; +// 窗口大小 +int g_iWndWidth = 1024; +int g_iWndHeight = 768; + +// 阴影用的深度缓冲的分辨率 +int g_iDepthWidth = 1024; +int g_iDepthHeight = 1024; D3D12_VIEWPORT g_stViewPort = { 0.0f, 0.0f, static_cast( g_iWndWidth ), static_cast( g_iWndHeight ) , D3D12_MIN_DEPTH, D3D12_MAX_DEPTH }; -D3D12_RECT g_stScissorRect = { 0, 0, static_cast( g_iWndWidth ), static_cast( g_iWndHeight ) }; +D3D12_VIEWPORT g_stDepthViewPort = { 0.0f, 0.0f, static_cast( g_iWndWidth ), static_cast( g_iWndHeight ) , D3D12_MIN_DEPTH, D3D12_MAX_DEPTH }; +D3D12_RECT g_stScissorRect = { 0, 0, static_cast( g_iWndWidth ), static_cast( g_iWndHeight ) }; //初始的默认摄像机的位置 -XMFLOAT3 g_f3EyePos = XMFLOAT3( 0.0f, 5.0f, -10.0f ); //眼睛位置 -XMFLOAT3 g_f3LockAt = XMFLOAT3( 0.0f, 0.0f, 0.0f ); //眼睛所盯的位置 -XMFLOAT3 g_f3HeapUp = XMFLOAT3( 0.0f, 1.0f, 0.0f ); //头部正上方位置 +XMFLOAT3 g_f3EyePos = XMFLOAT3( 0.0f, 5.0f, -10.0f ); //眼睛位置 +XMFLOAT3 g_f3HeapUp = XMFLOAT3( 0.0f, 1.0f, 0.0f ); //头部正上方位置 +XMFLOAT3 g_f3LockAt = XMFLOAT3( 0.0f, 0.0f, 0.0f ); //眼睛所盯的位置 -float g_fYaw = 0.0f; // 绕正Z轴的旋转量. -float g_fPitch = 0.0f; // 绕XZ平面的旋转量 +float g_fYaw = 0.0f; // 绕正Z轴的旋转量. +float g_fPitch = 0.0f; // 绕XZ平面的旋转量 -double g_fPalstance = 3.0f * XM_PI / 180.0f; //物体旋转的角速度,单位:弧度/秒 +double g_fPalstance = 3.0f * XM_PI / 180.0f; //物体旋转的角速度,单位:弧度/秒 -XMFLOAT4X4 g_mxWorld = {}; //World Matrix -XMFLOAT4X4 g_mxView = {}; //World Matrix -XMFLOAT4X4 g_mxProjection = {}; //World Matrix +XMFLOAT4X4 g_mxWorld = {}; //World Matrix +XMFLOAT4X4 g_mxView = {}; //World Matrix +XMFLOAT4X4 g_mxProjection = {}; //World Matrix // 全局线程参数 -const UINT g_nMaxThread = 3; -const UINT g_nThdSphere = 0; -const UINT g_nThdCube = 1; -const UINT g_nThdPlane = 2; -ST_GRS_THREAD_PARAMS g_stThreadParams[g_nMaxThread] = {}; +const UINT g_nMaxThread = 3; +const UINT g_nThdSphere = 0; +const UINT g_nThdCube = 1; +const UINT g_nThdPlane = 2; +ST_GRS_THREAD_PARAMS g_stThreadParams[g_nMaxThread] = {}; -ST_GRS_LIGHTBUFFER* g_pstLights = nullptr; +ST_GRS_LIGHTBUFFER* g_pstLights = nullptr; -const UINT g_nFrameBackBufCount = 3u; -UINT g_nRTVDescriptorSize = 0U; -UINT g_nSRVDescriptorSize = 0U; -UINT g_nDSVDescriptorSize = 0U; +const UINT g_nFrameBackBufCount = 3u; +UINT g_nRTVDescriptorSize = 0U; +UINT g_nSRVDescriptorSize = 0U; +UINT g_nDSVDescriptorSize = 0U; -ComPtr g_pIARenderTargets[g_nFrameBackBufCount]; +ComPtr g_pIARenderTargets[g_nFrameBackBufCount]; -ComPtr g_pIRTVHeap; -ComPtr g_pIDSVHeap; //深度缓冲描述符堆 +ComPtr g_pIRTVHeap; +ComPtr g_pIDSVHeap; //深度缓冲描述符堆 -TCHAR g_pszAppPath[MAX_PATH] = {}; +TCHAR g_pszAppPath[MAX_PATH] = {}; typedef CAtlArray CGRSMeshVertex; typedef CAtlArray CGRSMeshIndex; @@ -324,11 +330,10 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR { ::CoInitialize( nullptr ); //for WIC & COM - HWND hWnd = nullptr; - MSG msg = {}; - + HWND hWnd = nullptr; + MSG msg = {}; - const float faClearColor[] = { 0.2f, 0.5f, 1.0f, 1.0f }; + const float faClearColor[] = { 0.2f, 0.5f, 1.0f, 1.0f }; ComPtr pIDXGIFactory5; ComPtr pIDXGIFactory6; @@ -531,16 +536,14 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR ::SetWindowText( hWnd, pszWndTitle ); } - // 4、创建直接命令队列 + // 4、创建命令队列及命令列表 { D3D12_COMMAND_QUEUE_DESC stQueueDesc = {}; stQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; GRS_THROW_IF_FAILED( pID3D12Device4->CreateCommandQueue( &stQueueDesc, IID_PPV_ARGS( &pIMainCmdQueue ) ) ); GRS_SET_D3D12_DEBUGNAME_COMPTR( pIMainCmdQueue ); - } - // 5、创建直接命令列表 - { + // 创建直接命令列表 // 预处理命令列表 GRS_THROW_IF_FAILED( pID3D12Device4->CreateCommandAllocator( D3D12_COMMAND_LIST_TYPE_DIRECT , IID_PPV_ARGS( &pICmdAllocPre ) ) ); @@ -566,7 +569,7 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR GRS_SET_D3D12_DEBUGNAME_COMPTR( pICmdListPost ); } - // 6、创建围栏对象 + // 5、创建围栏对象 { GRS_THROW_IF_FAILED( pID3D12Device4->CreateFence( 0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS( &pIFence ) ) ); GRS_SET_D3D12_DEBUGNAME_COMPTR( pIFence ); @@ -579,7 +582,7 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR } } - // 7、创建交换链 + // 6、创建交换链 { DXGI_SWAP_CHAIN_DESC1 stSwapChainDesc = {}; stSwapChainDesc.BufferCount = g_nFrameBackBufCount; @@ -634,19 +637,20 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR GRS_THROW_IF_FAILED( pIDXGIFactory5->MakeWindowAssociation( hWnd, DXGI_MWA_NO_ALT_ENTER ) ); } - // 8、创建深度缓冲及深度缓冲描述符堆 + // 7、创建深度缓冲及深度缓冲描述符堆 { D3D12_CLEAR_VALUE stDepthOptimizedClearValue = {}; stDepthOptimizedClearValue.Format = emDepthShadowFormat; stDepthOptimizedClearValue.DepthStencil.Depth = 1.0f; stDepthOptimizedClearValue.DepthStencil.Stencil = 0; - + + D3D12_RESOURCE_DESC stDepthShadowResDesc = {}; stDepthShadowResDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; stDepthShadowResDesc.Alignment = 0; stDepthShadowResDesc.Format = emDepthShadowFormat; - stDepthShadowResDesc.Width = g_iWndWidth; - stDepthShadowResDesc.Height = g_iWndHeight; + stDepthShadowResDesc.Width = g_iDepthWidth; + stDepthShadowResDesc.Height = g_iDepthHeight; stDepthShadowResDesc.DepthOrArraySize = 1; stDepthShadowResDesc.MipLevels = 0; stDepthShadowResDesc.SampleDesc.Count = 1; @@ -716,7 +720,7 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR } - // 9、创建根签名 + // 8、创建根签名 {//这个例子中,所有物体使用相同的根签名,因为渲染过程中需要的参数是一样的 // 检测是否支持V1.1版本的根签名 stFeatureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1; @@ -821,7 +825,7 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR GRS_SET_D3D12_DEBUGNAME_COMPTR( pIRSShadowPass ); } - // 10、编译Shader创建渲染管线状态对象 + // 9、编译Shader创建渲染管线状态对象 if( TRUE ) { UINT nShaderCompileFlags = 0; @@ -921,7 +925,7 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR } - // 11、创建渲染UI矩形(渲染阴影视图)用的管线的根签名、管线状态对象、矩形框VB、CB、采样器(堆)、SRV Heap、CBV、SRV + // 10、创建渲染UI矩形(渲染阴影视图)用的管线的根签名、管线状态对象、矩形框VB、CB、采样器(堆)、SRV Heap、CBV、SRV { //-------------------------------------------------------------------------------------------------------------- //创建渲染矩形的根签名对象 @@ -1134,7 +1138,6 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR pID3D12Device4->CreateSampler( &stSamplerDesc, pISampleHeapQuad->GetCPUDescriptorHandleForHeapStart() ); //-------------------------------------------------------------------------------------------------------------- - // 计算正交投影矩阵 Orthographic // 基于左上角是坐标原点 X正方向向右 Y正方向向下 与窗口坐标系相同 // 这里的算法来自于古老的HGE引擎核心 @@ -1197,57 +1200,56 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR //-------------------------------------------------------------------------------------------------------------- } - // 12、创建Sample + // 11、创建Sample + if ( TRUE ) { - { - D3D12_DESCRIPTOR_HEAP_DESC stSamplerHeapDesc = {}; - stSamplerHeapDesc.NumDescriptors = 2; // 1 Wrap Sample + 1 Clamp Sample - stSamplerHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; - stSamplerHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - - GRS_THROW_IF_FAILED( pID3D12Device4->CreateDescriptorHeap( &stSamplerHeapDesc, IID_PPV_ARGS( &pISampleHeap ) ) ); - GRS_SET_D3D12_DEBUGNAME_COMPTR( pISampleHeap ); - - const UINT nSamplerDescriptorSize = pID3D12Device4->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER ); - D3D12_CPU_DESCRIPTOR_HANDLE stSamplerHandle = pISampleHeap->GetCPUDescriptorHandleForHeapStart() ; - - D3D12_SAMPLER_DESC stWrapSamplerDesc = {}; - stWrapSamplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; - stWrapSamplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; - stWrapSamplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; - stWrapSamplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; - stWrapSamplerDesc.MinLOD = 0; - stWrapSamplerDesc.MaxLOD = D3D12_FLOAT32_MAX; - stWrapSamplerDesc.MipLODBias = 0.0f; - stWrapSamplerDesc.MaxAnisotropy = 1; - stWrapSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; - stWrapSamplerDesc.BorderColor[0] - = stWrapSamplerDesc.BorderColor[1] - = stWrapSamplerDesc.BorderColor[2] - = stWrapSamplerDesc.BorderColor[3] = 0; - pID3D12Device4->CreateSampler( &stWrapSamplerDesc, stSamplerHandle ); - - stSamplerHandle.ptr += nSamplerDescriptorSize ; - - D3D12_SAMPLER_DESC stClampSamplerDesc = {}; - stClampSamplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; - stClampSamplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; - stClampSamplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; - stClampSamplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; - stClampSamplerDesc.MipLODBias = 0.0f; - stClampSamplerDesc.MaxAnisotropy = 1; - stClampSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; - stClampSamplerDesc.BorderColor[0] - = stClampSamplerDesc.BorderColor[1] - = stClampSamplerDesc.BorderColor[2] - = stClampSamplerDesc.BorderColor[3] = 0; - stClampSamplerDesc.MinLOD = 0; - stClampSamplerDesc.MaxLOD = D3D12_FLOAT32_MAX; - pID3D12Device4->CreateSampler( &stClampSamplerDesc, stSamplerHandle ); - } + D3D12_DESCRIPTOR_HEAP_DESC stSamplerHeapDesc = {}; + stSamplerHeapDesc.NumDescriptors = 2; // 1 Wrap Sample + 1 Clamp Sample + stSamplerHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; + stSamplerHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + + GRS_THROW_IF_FAILED( pID3D12Device4->CreateDescriptorHeap( &stSamplerHeapDesc, IID_PPV_ARGS( &pISampleHeap ) ) ); + GRS_SET_D3D12_DEBUGNAME_COMPTR( pISampleHeap ); + + const UINT nSamplerDescriptorSize = pID3D12Device4->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER ); + D3D12_CPU_DESCRIPTOR_HANDLE stSamplerHandle = pISampleHeap->GetCPUDescriptorHandleForHeapStart(); + + D3D12_SAMPLER_DESC stWrapSamplerDesc = {}; + stWrapSamplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; + stWrapSamplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + stWrapSamplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + stWrapSamplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + stWrapSamplerDesc.MinLOD = 0; + stWrapSamplerDesc.MaxLOD = D3D12_FLOAT32_MAX; + stWrapSamplerDesc.MipLODBias = 0.0f; + stWrapSamplerDesc.MaxAnisotropy = 1; + stWrapSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; + stWrapSamplerDesc.BorderColor[0] + = stWrapSamplerDesc.BorderColor[1] + = stWrapSamplerDesc.BorderColor[2] + = stWrapSamplerDesc.BorderColor[3] = 0; + pID3D12Device4->CreateSampler( &stWrapSamplerDesc, stSamplerHandle ); + + stSamplerHandle.ptr += nSamplerDescriptorSize; + + D3D12_SAMPLER_DESC stClampSamplerDesc = {}; + stClampSamplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT; + stClampSamplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; + stClampSamplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; + stClampSamplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; + stClampSamplerDesc.MipLODBias = 0.0f; + stClampSamplerDesc.MaxAnisotropy = 1; + stClampSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; + stClampSamplerDesc.BorderColor[0] + = stClampSamplerDesc.BorderColor[1] + = stClampSamplerDesc.BorderColor[2] + = stClampSamplerDesc.BorderColor[3] = 0; + stClampSamplerDesc.MinLOD = 0; + stClampSamplerDesc.MaxLOD = D3D12_FLOAT32_MAX; + pID3D12Device4->CreateSampler( &stClampSamplerDesc, stSamplerHandle ); } - // 13、创建全局灯光 + // 12、创建全局灯光 { // 计算透视矩阵(本例中光源 摄像机都使用同一个透视矩阵) XMStoreFloat4x4( &g_mxProjection, XMMatrixPerspectiveFovLH( XM_PIDIV4, (FLOAT) g_iWndWidth / (FLOAT) g_iWndHeight, fNearPlane, fFarPlane ) ); @@ -1270,8 +1272,8 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR for ( int i = 0; i < GRS_NUM_LIGHTS; i++ ) { - g_pstLights->m_stLights[i].m_v4Position = { 0.0f, 10.0f, -15.0f, 1.0f }; - g_pstLights->m_stLights[i].m_v4Direction = { 0.0, -1.0f, 1.0f, 0.0f }; + g_pstLights->m_stLights[i].m_v4Position = { 0.0f, 5.0f, -10.0f, 1.0f }; + g_pstLights->m_stLights[i].m_v4Direction = { 0.0, 0.0f, 1.0f, 0.0f }; g_pstLights->m_stLights[i].m_v4Falloff = { 80.0f, 1.0f, 0.0f, 1.0f }; g_pstLights->m_stLights[i].m_v4Color = { 0.9f, 0.9f, 0.9f, 1.0f }; @@ -1283,27 +1285,28 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR g_pstLights->m_stLights[i].m_mxProjection = g_mxProjection; } + g_pstLights->m_stLights[1].m_v4Color = { 0.0f, 0.0f, 0.0f, 1.0f }; g_pstLights->m_stLights[2].m_v4Color = { 0.0f, 0.0f, 0.0f, 1.0f }; } - // 14、准备参数并启动多个渲染线程 + // 13、准备参数并启动多个渲染线程 { USES_CONVERSION; // 球体个性参数 StringCchPrintf( g_stThreadParams[g_nThdSphere].m_pszDiffuseFile, MAX_PATH, _T( "%sAssets\\Earth_512.dds" ), g_pszAppPath ); StringCchPrintf( g_stThreadParams[g_nThdSphere].m_pszNormalFile, MAX_PATH, _T( "%sAssets\\Earth_512_Normal.dds" ), g_pszAppPath ); - g_stThreadParams[g_nThdSphere].m_v4ModelPos = XMFLOAT4( 2.0f, 2.0f, 0.0f, 1.0f ); + g_stThreadParams[g_nThdSphere].m_v4ModelPos = XMFLOAT4( 2.0f, 1.0f, 0.0f, 1.0f ); // 立方体个性参数 StringCchPrintf( g_stThreadParams[g_nThdCube].m_pszDiffuseFile, MAX_PATH, _T( "%sAssets\\Cube.dds" ), g_pszAppPath ); StringCchPrintf( g_stThreadParams[g_nThdCube].m_pszNormalFile, MAX_PATH, _T( "%sAssets\\Cube_NRM.dds" ), g_pszAppPath ); - g_stThreadParams[g_nThdCube].m_v4ModelPos = XMFLOAT4( -2.0f, 2.0f, 0.0f, 1.0f ); + g_stThreadParams[g_nThdCube].m_v4ModelPos = XMFLOAT4( -2.0f, 1.0f, 0.0f, 1.0f ); // 平板个性参数 StringCchPrintf( g_stThreadParams[g_nThdPlane].m_pszDiffuseFile, MAX_PATH, _T( "%sAssets\\Plane.dds" ), g_pszAppPath ); StringCchPrintf( g_stThreadParams[g_nThdPlane].m_pszNormalFile, MAX_PATH, _T( "%sAssets\\Plane_NRM.dds" ), g_pszAppPath ); - g_stThreadParams[g_nThdPlane].m_v4ModelPos = XMFLOAT4( 0.0f, 1.0f, 0.0f, 1.0f ); + g_stThreadParams[g_nThdPlane].m_v4ModelPos = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ); // 物体的共性参数,也就是各线程的共性参数 @@ -1407,8 +1410,8 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR { switch ( nStates ) { - case EM_GRS_RS_GPUCOPY://状态0,表示等到各子线程加载资源完毕,此时执行一次命令列表完成各子线程要求的资源上传的第二个Copy命令 - { + case EM_GRS_RS_GPUCOPY: + {//状态EM_GRS_RS_GPUCOPY:执行资源上传的第二个Copy命令 arCmdList.RemoveAll(); //执行命令列表 arCmdList.Add( g_stThreadParams[g_nThdSphere].m_pICmdList ); @@ -1430,8 +1433,8 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR arHWaited.Add( hEventFence ); } break; - case EM_GRS_RS_STARTSHADOW:// 状态1 等到命令队列执行结束(也即CPU等到GPU执行结束),开始新一轮渲染 - { + case EM_GRS_RS_STARTSHADOW: + {// 状态EM_GRS_RS_STARTSHADOW:渲染阴影 // 注意只在状态1 调用OnUpdate这个过程,只有阴影渲染,正常渲染结束了才再次更新,两遍渲染才是一个完整的周期 //OnUpdate() { @@ -1456,16 +1459,8 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR // 变换光源 for ( int i = 0; i < GRS_NUM_LIGHTS; i++ ) { - float fRotLight = (float) ( g_fPalstance * pow( -1.0, i ) ); - - //XMStoreFloat4(&g_pstLights->m_stLights[i].m_v4Position - // , XMVector4Transform(XMLoadFloat4(&g_pstLights->m_stLights[i].m_v4Position) - // , XMMatrixRotationY(fRotLight))); - XMVECTOR eye = XMLoadFloat4( &g_pstLights->m_stLights[i].m_v4Position ); - XMVECTOR at = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); - XMStoreFloat4( &g_pstLights->m_stLights[i].m_v4Direction - , XMVector3Normalize( XMVectorSubtract( at, eye ) ) ); + XMVECTOR at = { 0.0f,0.0f,0.0f,0.0f }; XMVECTOR up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); XMStoreFloat4x4( &g_pstLights->m_stLights[i].m_mxView @@ -1498,8 +1493,9 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR D3D12_CPU_DESCRIPTOR_HANDLE stDSVHandle = g_pIDSVHeap->GetCPUDescriptorHandleForHeapStart() ; //设置渲染目标(注意阴影Pass不要RTV) pICmdListPre->OMSetRenderTargets( 0, nullptr, FALSE, &stDSVHandle ); - - pICmdListPre->RSSetViewports( 1, &g_stViewPort ); + // 阴影贴图与窗口分辨率不同,需要改变视口(viewport)的参数以适应阴影贴图的尺寸。 + // 如果不更新视口,最后的深度贴图要么太小要么就不完整。 + pICmdListPre->RSSetViewports( 1, &g_stDepthViewPort ); pICmdListPre->RSSetScissorRects( 1, &g_stScissorRect ); pICmdListPre->ClearDepthStencilView( stDSVHandle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr ); @@ -1531,8 +1527,8 @@ int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR } } break; - case EM_GRS_RS_STARTRENDER:// 状态2 阴影渲染结束 开始正常渲染 - { + case EM_GRS_RS_STARTRENDER: + {// 状态EM_GRS_RS_STARTRENDER:阴影渲染结束 开始正常渲染 // OnUpdate() 恢复View Projection 矩阵到摄像机空间 { //计算 World 矩阵 这里是个旋转矩阵 diff --git a/14-MultiThreadShadow/Shader/PSMain.hlsl b/14-MultiThreadShadow/Shader/PSMain.hlsl index 59688b5b221230b08a04168179a451691c56912f..722416b6153477ffa17fd7f251b98c280f29e6e6 100644 --- a/14-MultiThreadShadow/Shader/PSMain.hlsl +++ b/14-MultiThreadShadow/Shader/PSMain.hlsl @@ -53,6 +53,7 @@ float3 CalcPerPixelNormal(float2 vTexcoord, float3 vVertNormal, float3 vVertTang float3 vBumpNormal = (float3)g_t2dNormalMap.Sample(g_SampleWrap, vTexcoord); // 符号化还原 vBumpNormal = 2.0f * vBumpNormal - 1.0f; + vBumpNormal.z = -vBumpNormal.z; return mul(vBumpNormal, mTangentSpaceToWorldSpace); } @@ -157,6 +158,7 @@ float4 PSMain(PSInput input) : SV_TARGET float4 fPixelColor = diffuseColor; float fDepthValue = g_t2dShadowMap.Sample(g_SampleClamp, v2LightCoord); vLightSpacePos.z -= 0.05f; + if ( vLightSpacePos.z < fDepthValue ) { fPixelColor += v4AmbientColor; @@ -171,7 +173,7 @@ float4 PSMain(PSInput input) : SV_TARGET { fPixelColor -= v4AmbientColor; } - return fPixelColor; + return fPixelColor; //return float4(vLightSpacePos.z, vLightSpacePos.z, vLightSpacePos.z, 1.0f); //return vLightSpacePos; diff --git a/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj b/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj index 6b324bf5e87f2b6632b6faf46aab0f671f79237f..12d60ca9cea78cd123e3303d8a420bd3c1f1c060 100644 --- a/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj +++ b/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj @@ -151,6 +151,10 @@ true + + + + diff --git a/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj.filters b/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj.filters index 6bb23dffbd99a36fa149bfd27061530a9fb969eb..60d454a5c5d60f1d37c2e41ebaab3febaab6a40a 100644 --- a/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj.filters +++ b/18-PBR-Base-Point-Lights/18-PBR-Base-Point-Lights.vcxproj.filters @@ -30,4 +30,12 @@ Shader + + + 澶存枃浠 + + + 澶存枃浠 + + \ No newline at end of file diff --git a/18-PBR-Base-Point-Lights/Shader/PS_PBR_Base_Point_Lights.hlsl b/18-PBR-Base-Point-Lights/Shader/PS_PBR_Base_Point_Lights.hlsl index aeb16d503fce2c1c5484ea156c3f5ab48dffa606..daf4dae2e08d25426071231f9a204409e05e919e 100644 --- a/18-PBR-Base-Point-Lights/Shader/PS_PBR_Base_Point_Lights.hlsl +++ b/18-PBR-Base-Point-Lights/Shader/PS_PBR_Base_Point_Lights.hlsl @@ -112,7 +112,11 @@ float4 PSMain( PSInput stPSInput ) : SV_TARGET // 中间向量(入射光与法线的角平分线) float3 H = normalize( V + L ); float distance = length( g_v4LightPos[i].xyz - stPSInput.g_v4PosWorld.xyz ); - float attenuation = 1.0 / ( distance * distance ); + + //float attenuation = 1.0 / ( distance * distance ); + // 已经Gamma矫正了,就不要二次反比衰减了,单次衰减就可以了 + float attenuation = 1.0 / distance ; + float3 radiance = g_v4LightClr[i].xyz * attenuation; // Cook-Torrance光照模型 BRDF diff --git a/README.assets/image-20210627151630910.png b/README.assets/image-20210627151630910.png new file mode 100644 index 0000000000000000000000000000000000000000..d5820a52a382cacf40cab3e0c59eb34a448154da Binary files /dev/null and b/README.assets/image-20210627151630910.png differ diff --git a/README.md b/README.md index bd2dd1267f70c095cb5142ca2a6a4e86f55725f6..8867c1e73f5ddf8733f6acfb5559bfa114f65f38 100644 --- a/README.md +++ b/README.md @@ -109,3 +109,10 @@   璇ョず渚嬪睍绀轰簡浣跨敤Assimp瀵煎叆鍔ㄧ敾鏂囦欢锛屽苟鐢ㄥ熀鏈殑D3D12娓叉煋妗嗘灦娓叉煋锛岃繕娌℃湁鍔犱换浣曠殑鍏夌収鏁堟灉锛屽彧鏄畝鍗曠殑灞曠ず3D楠ㄩ鍔ㄧ敾鐨勫熀鏈師鐞嗐 ![16233246465942021610193211](ScreenShot/16233246465942021610193211.gif) + +# 18-PBR-Base-Point-Lights + +  璇ョず渚嬪睍绀哄熀纭鐨凱BR娓叉煋杩囩▼锛岄噸鐐瑰湪PS_PBR_Base_Point_Lights.hlsl鏂囦欢涓傚熀浜庣墿鐞嗙殑娓叉煋锛圥BR锛夋槸鐜颁唬娓叉煋鐨勪富娴佹妧鏈紝涓庝紶缁熷厜鐓х函鍑犱綍寮忕殑鍏夌収娓叉煋鏂瑰紡宸茬粡澶х浉寰勫涵锛屾湰绯诲垪绀轰緥鍗崇洿鎺ヨ烦杩囦紶缁熷厜鐓э紝鐩存帴杩涘叆鐜颁唬鍖栫殑PBR娓叉煋鐨勫涔犱笌灞曠ず銆傛湰渚嬩腑鐢ㄤ簡8涓偣鍏夋簮锛岀収鏄庝簡涓涓悆浣擄紝浣跨敤浜哖BR涓瘮杈冩祦琛岀殑鈥滈噾灞炲伐浣滄祦鈥濇覆鏌撴ā鍨嬶紝鍗充富瑕佹潗璐ㄥ弬鏁版槸閲戝睘搴︺佺矖绯欏害銆佸弽灏勭巼绛夛紝鍏朵腑鍙嶅皠鐜囨ā鎷熶簡8绉嶉噾灞烇紝鍙互浣跨敤Tab閿垏鎹㈠弽灏勭巼锛岀敤A銆丵閿-鎺у埗閲戝睘搴︺乄銆丒閿-鎺у埗绮楃硻搴︺丒銆丏閿-鎺у埗鐜閬尅绯绘暟銆傜敱浜庝娇鐢ㄧ殑鏄偣鍏夋簮锛岃繖涓ず渚嬪湪绾噾灞炴渶浣庣矖绯欏害鏃讹紝浠呮湁楂樺厜鐐癸紝杩欏叾瀹炴槸涓嶅鐨勩備緥瀛愮殑鐪熷疄鐩殑鏄粰澶у灞曠ず鍩烘湰鐨凱BR鍏夌収鏂圭▼鍙樻垚浠g爜鐨勬牱瀛愩傛渶缁堥渶瑕佹敞鎰忕殑鏄紝瀛︿範D3D12鎺ュ彛缂栫▼鐨勭粓鏋佺洰鐨勬槸鎺屾彙鐜颁唬娓叉煋绠楁硶鍜屾妧鏈紝鍥犳鍚庣画鐨勪緥瀛愰兘甯屾湜澶у瀵瑰墠闈㈠悇涓緥瀛愪腑鐨勪竴浜汥3D12鍩烘湰鎿嶄綔閮芥湁浜嗙墷鍥虹殑鎺屾彙锛屾垨鑰呰D3D12鏈韩涓嶅簲鍐嶆垚涓哄ぇ瀹跺涔犵殑闅滅锛岃屾槸涓涓湁鍔涚殑宸ュ叿锛佸鏋滃墠搴忎緥瀛愯繕鏈夌枒闂垨鑰呮垜鍙兘娌℃湁浜や唬娓呮鐨勭粏鑺傦紝澶у閮藉彲浠ラ殢鏃跺埌鏈汉鍗氬锛歔Gamebaby Rock Sun鐨勫崥瀹(https://blog.csdn.net/u014038143?spm=1000.2115.3001.5343) 鐣欒█鎻愰棶銆傛湰绀轰緥杩愯鏁堟灉鎴浘濡備笅锛堜竴涓暐鏄剧矖绯欑殑閲戣壊閲戝睘鐞冿紝鍙互閫氳繃鎸夐敭璋冩暣鑷虫鏁堟灉锛夛細 + +![image-20210627151630910](ScreenShot/image-20210627151630910.png) + diff --git a/Revision.md b/Revision.md index fe31d9c8c95938994f724fed180b8bbadf965b37..dff8d6ac36505577bd9ab9ef07cf811ef333c9be 100644 --- a/Revision.md +++ b/Revision.md @@ -94,3 +94,9 @@ D3D12 ERROR: ID3D12CommandQueue::Present: Resource state (0x800: D3D12_RESOURCE_ 2021-6-22 1銆佹坊鍔犵18涓ず渚嬶紝18-PBR-Base-Point-Lights锛屽紑鍚珮绾у厜鐓т箣鏃咃紝鐩存帴鐣ヨ繃鏃犺亰鐨勪紶缁熷厜鐓э紱 + +2021-6-25 + +1銆佷慨姝d簡绗18鍙风ず渚婼hader涓殑BUG锛屼箣鍓嶇敤鐨勬槸浜屾琛板噺锛屽洜涓哄紑濮嬪凡缁廏amma鐭浜嗭紝浜屾琛板噺鐨勮瘽浼氫娇琛板噺杩囧害锛屾墍浠ユ敼鎴1娆¤“鍑忥紝杩欐牱Gamma鐭鐨2.2娆℃柟杩戜技绛変簬浜屾鏂逛簡锛岃繖鏍风浉褰撲簬宸茬粡浜屾琛板噺浜嗐 + +2銆14鍙风ず渚嬭繕鏄病鏈変慨姝eソ锛屽厛鎻愪氦锛屽ぇ瀹跺彲浠ュ厛鐣ヨ繃锛涢噸鐐规槸淇敼浜18鍙风ず渚嬩腑鍏夌嚎琛板噺璁$畻鐨勯敊璇紝骞朵笖鏇存柊浜18鍙风ず渚嬬殑璇存槑锛 diff --git a/ScreenShot/image-20210627151630910.png b/ScreenShot/image-20210627151630910.png new file mode 100644 index 0000000000000000000000000000000000000000..d5820a52a382cacf40cab3e0c59eb34a448154da Binary files /dev/null and b/ScreenShot/image-20210627151630910.png differ