Commit 942244ec authored by Administrator's avatar Administrator
Browse files

add depth buffer

parent 680c085b
...@@ -25,6 +25,11 @@ void App::DoFrame() ...@@ -25,6 +25,11 @@ void App::DoFrame()
{ {
const float c = sin(timer.Peek()) / 2.0f + 0.5f; const float c = sin(timer.Peek()) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer(c, c, 1.0f); wnd.Gfx().ClearBuffer(c, c, 1.0f);
wnd.Gfx().DrawTestTriangle(
-timer.Peek(),
0.0f,
0.0f
);
wnd.Gfx().DrawTestTriangle( wnd.Gfx().DrawTestTriangle(
timer.Peek(), timer.Peek(),
wnd.mouse.GetPosX() / 400.0f - 1.0f, wnd.mouse.GetPosX() / 400.0f - 1.0f,
......
...@@ -41,10 +41,10 @@ Graphics::Graphics( HWND hWnd ) ...@@ -41,10 +41,10 @@ Graphics::Graphics( HWND hWnd )
sd.SampleDesc.Count = 1; sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0; sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 2; sd.BufferCount = 1;
sd.OutputWindow = hWnd; sd.OutputWindow = hWnd;
sd.Windowed = TRUE; sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0; sd.Flags = 0;
UINT swapCreateFlags = 0u; UINT swapCreateFlags = 0u;
...@@ -72,11 +72,46 @@ Graphics::Graphics( HWND hWnd ) ...@@ -72,11 +72,46 @@ Graphics::Graphics( HWND hWnd )
&pContext &pContext
)); ));
// gain access to texture subresource in swap chain (back buffer) // gain access to texture subresource in swap chain (back buffer)
wrl::ComPtr<ID3D11Resource> pBackBuffer; wrl::ComPtr<ID3D11Resource> pBackBuffer;
GFX_THROW_INFO(pSwap->GetBuffer(0, __uuidof(ID3D11Resource), &pBackBuffer)); GFX_THROW_INFO(pSwap->GetBuffer(0, __uuidof(ID3D11Resource), &pBackBuffer));
GFX_THROW_INFO(pDevice->CreateRenderTargetView(pBackBuffer.Get(), nullptr, &pTarget)); GFX_THROW_INFO(pDevice->CreateRenderTargetView(pBackBuffer.Get(), nullptr, &pTarget));
// Create depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
wrl::ComPtr<ID3D11DepthStencilState> pDepthStencilState;
// Create depth stencil state
GFX_THROW_INFO(pDevice->CreateDepthStencilState(&depthStencilDesc, &pDepthStencilState));
// bind depth state
pContext->OMSetDepthStencilState(pDepthStencilState.Get(), 1u);
// Create the depth stencil texture
wrl::ComPtr<ID3D11Texture2D> depthStencilTexture;
D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {};
depthStencilTextureDesc.Width = 800;
depthStencilTextureDesc.Height = 600;
depthStencilTextureDesc.MipLevels = 1u;
depthStencilTextureDesc.ArraySize = 1u;
depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilTextureDesc.SampleDesc.Count = 1u;
depthStencilTextureDesc.SampleDesc.Quality = 0u;
depthStencilTextureDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
GFX_THROW_INFO(pDevice->CreateTexture2D(&depthStencilTextureDesc, nullptr, &depthStencilTexture));
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc = {};
depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0u;
GFX_THROW_INFO(pDevice->CreateDepthStencilView(depthStencilTexture.Get(), &depthStencilViewDesc, &pDSView));
// Bind depth stencil view to out put merger
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSView.Get());
} }
void Graphics::EndFrame() void Graphics::EndFrame()
...@@ -103,6 +138,7 @@ void Graphics::ClearBuffer(float red, float green, float blue) noexcept ...@@ -103,6 +138,7 @@ void Graphics::ClearBuffer(float red, float green, float blue) noexcept
{ {
const float color[] = { red,green,blue,1.0f }; const float color[] = { red,green,blue,1.0f };
pContext->ClearRenderTargetView(pTarget.Get(), color); pContext->ClearRenderTargetView(pTarget.Get(), color);
pContext->ClearDepthStencilView(pDSView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
} }
void Graphics::DrawTestTriangle(float angle, float x, float y) void Graphics::DrawTestTriangle(float angle, float x, float y)
...@@ -209,7 +245,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y) ...@@ -209,7 +245,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
// rotate Y axis // rotate Y axis
* dx::XMMatrixRotationY(-angle) * dx::XMMatrixRotationY(-angle)
// attach the image to mouse // attach the image to mouse
* dx::XMMatrixTranslation( x, y, 4.0f ) * dx::XMMatrixTranslation( x, 0.0f, 4.0f + y )
// projection // projection
* dx::XMMatrixPerspectiveLH( 1.0f, heightWidthRatio, 0.5f, 10.0f) * dx::XMMatrixPerspectiveLH( 1.0f, heightWidthRatio, 0.5f, 10.0f)
) )
...@@ -229,8 +265,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y) ...@@ -229,8 +265,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
// Bind constant buffer to vertex // Bind constant buffer to vertex
pContext->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf()); pContext->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
// Create constant buffer for color // Create constant buffer for color
struct ColorConstantBuffer struct ColorConstantBuffer
{ {
...@@ -269,7 +303,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y) ...@@ -269,7 +303,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
// Bind constant buffer to vertex // Bind constant buffer to vertex
pContext->PSSetConstantBuffers(0u, 1u, pColorConstantBuffer.GetAddressOf()); pContext->PSSetConstantBuffers(0u, 1u, pColorConstantBuffer.GetAddressOf());
// create pixel shader // create pixel shader
wrl::ComPtr<ID3D11PixelShader> pPixelShader; wrl::ComPtr<ID3D11PixelShader> pPixelShader;
wrl::ComPtr<ID3DBlob> pBlob; wrl::ComPtr<ID3DBlob> pBlob;
...@@ -306,7 +339,8 @@ void Graphics::DrawTestTriangle(float angle, float x, float y) ...@@ -306,7 +339,8 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
pContext->IASetInputLayout(pInputLayout.Get()); pContext->IASetInputLayout(pInputLayout.Get());
// bind render target // bind render target
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), nullptr); // After apply depth stencil view to Output Merger, we remove this one
//pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), nullptr);
// Set primitive topology to triangle list (groups of 3 vertices) // Set primitive topology to triangle list (groups of 3 vertices)
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
...@@ -321,7 +355,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y) ...@@ -321,7 +355,6 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
vp.TopLeftY = 0; vp.TopLeftY = 0;
pContext->RSSetViewports(1u, &vp); pContext->RSSetViewports(1u, &vp);
//GFX_THROW_INFO_ONLY(pContext->Draw((UINT)std::size(vertices), 0u)); //GFX_THROW_INFO_ONLY(pContext->Draw((UINT)std::size(vertices), 0u));
GFX_THROW_INFO_ONLY(pContext->DrawIndexed((UINT)std::size(indices), 0u, 0u)); GFX_THROW_INFO_ONLY(pContext->DrawIndexed((UINT)std::size(indices), 0u, 0u));
} }
......
...@@ -66,4 +66,6 @@ private: ...@@ -66,4 +66,6 @@ private:
Microsoft::WRL::ComPtr<ID3D11DeviceContext> pContext; Microsoft::WRL::ComPtr<ID3D11DeviceContext> pContext;
// 指向View的指针 // 指向View的指针
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> pTarget; Microsoft::WRL::ComPtr<ID3D11RenderTargetView> pTarget;
// 指向Depth Buffer View的指针
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> pDSView;
}; };
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment