Sign in to follow this  

Direct3D why Depth Buffer doesn't work.

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folk! I faced trouble: if far object will be rendered first and near object last then far object will be over near object. As far as i understand this problem appears when Depth buffer is disabled. I saw samples from DX SDK and compared them line by line to my code - there is no any success. Here is my code for initialization Graphics Device and rendering. Could you please see it and tell me what i forgot to do:

void GraphicsDevice::createGraphicsSystem(GRAPHICS_SETTINGS& settings)
{
	TRACE( "Creating GraphicsDevice..." );

	_backBufferHeight = settings.height;
	_backBufferWidth = settings.width;

	DXGI_SWAP_CHAIN_DESC desc;
	desc.BufferDesc.Width = settings.width;
	desc.BufferDesc.Height = settings.height;
	desc.BufferDesc.RefreshRate.Numerator = 60;
	desc.BufferDesc.RefreshRate.Denominator = 1;
	desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
	desc.SampleDesc.Count = settings.multisamplingCount;
	desc.SampleDesc.Quality = settings.multisamplingQuality;
	desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	desc.BufferCount = 1;
	desc.OutputWindow = settings.hwnd;
	desc.Windowed = settings.windowed;
	desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	desc.Flags = 0;
	UINT flags = 0;
#if defined(_DEBUG) || defined(DEBUG)
	flags |= D3D10_CREATE_DEVICE_DEBUG;
#endif
	dx_ptr<IDXGISwapChain> swapChain;
	dx_ptr<ID3D10Device> device;
	//HR( D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_SDK_VERSION, &desc, swapChain.pptr(), device.pptr() ) ); 
	HR( D3D10CreateDevice( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_SDK_VERSION, device.pptr() ) );
	dx_ptr<IDXGIFactory> dxgiFactory;
	HR( CreateDXGIFactory( __uuidof(IDXGIFactory), (void**)dxgiFactory.pptr() ) );
	HR( dxgiFactory->CreateSwapChain( device.ptr(), &desc, swapChain.pptr() ) );
	HR( dxgiFactory->MakeWindowAssociation( settings.hwnd, DXGI_MWA_NO_WINDOW_CHANGES ) );
	dxgiFactory.destroy();

	dx_ptr<ID3D10RenderTargetView> renderTargetView;
	dx_ptr<ID3D10Texture2D> backBuffer;
	HR( swapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D), reinterpret_cast<void**>( backBuffer.pptr() ) ) );
	HR( device->CreateRenderTargetView( backBuffer.ptr(), 0, renderTargetView.pptr() ) );
	backBuffer.destroy();

	D3D10_TEXTURE2D_DESC dsd;
	ZeroMemory( &dsd, sizeof( dsd ) );
	dsd.Width = settings.width;
	dsd.Height = settings.height;
	dsd.MipLevels = 1;
	dsd.ArraySize = 1;
	dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	dsd.SampleDesc.Count = 1;//settings.multisamplingCount;
	dsd.SampleDesc.Quality = 0;// settings.multisamplingQuality;
	dsd.Usage = D3D10_USAGE_DEFAULT;
	dsd.BindFlags = D3D10_BIND_DEPTH_STENCIL;
	dsd.CPUAccessFlags = 0;
	dsd.MiscFlags = 0;

	dx_ptr<ID3D10Texture2D> depthStencilBuffer;
	HR( device->CreateTexture2D( &dsd, NULL, depthStencilBuffer.pptr() ) );

	dx_ptr<ID3D10DepthStencilView> depthStencilView;
	D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
    descDSV.Format = dsd.Format;
    descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;

	HR( device->CreateDepthStencilView( depthStencilBuffer.ptr(), &descDSV, depthStencilView.pptr() ) );
	device->OMSetRenderTargets( 1, renderTargetView.pptr(), depthStencilView.ptr() );

	D3D10_VIEWPORT vp;
	ZeroMemory( &vp, sizeof( vp ) );
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	vp.Width = settings.width;
	vp.Height = settings.height;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	device->RSSetViewports( 1, &vp );

	_device = device.commit();
	_swapChain = swapChain.commit();
	_renderTargetView = renderTargetView.commit();
	_depthStencilBuffer = depthStencilBuffer.commit();
	_depthStencilView = depthStencilView.commit();
	_deviceCreated = true;

	TRACE( "GraphicsDevice created!" );
}
Rendering:

const float GraphicsDevice::DEPTH_CLEAR_VALUE = 1.0f;
const UINT8 GraphicsDevice::STENCIL_CLEAR_VALUE = 0;

inline void GraphicsDevice::beginRender(const D3DXCOLOR& backColor, UINT clearFlags = D3D10_CLEAR_DEPTH)
{
	_device->ClearRenderTargetView( _renderTargetView, (float*)&backColor );
	_device->ClearDepthStencilView( _depthStencilView, clearFlags, GraphicsDevice::DEPTH_CLEAR_VALUE, GraphicsDevice::STENCIL_CLEAR_VALUE );
}

inline void GraphicsDevice::endRender()
{
	_swapChain->Present( 0, 0 );
}


Share this post


Link to post
Share on other sites
I don't see you creating or using a ID3D10DepthStencilState, although the default one should have depth enabled, so this might not be the problem.

You can try using PIX to see what happens during your draw calls.

Share this post


Link to post
Share on other sites
I see you have commented out the multisampling/quality for the depth-stencil view. Do you use 1/0 multi-sampling for the render-target too?
If not, the depth-stencil won't cover the entire screen, which might cause your problem.

Share this post


Link to post
Share on other sites
Thanks a lot for your reply.
ET3D, i tried to use PIX for Windows. I found very interesting moment: there exists call of OMSetDepthStencilTest && OMSetBlendState. My program doesn't use this methods. How they can appear there? My program renders coordinate system (3 colored lines) and two rectangles with texture. Here is what i've got from PIX:

96 <0x02F83ED8> ID3D10Device::ClearRenderTargetView(0x02F858B0, 0x01458F78) 6530620697 173440
97 <0x02F83ED8> ID3D10Device::ClearDepthStencilView(0x02F861E8, 1, 1.000f, 0) 6807095073 2016
98 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST) 6807147421
102 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x0175C638 --> { 0x0305A2F0 }, 0x0175C644, 0x002CF93C) 6807157685
106 <0x02F83ED8> ID3D10Device::IASetIndexBuffer(0x0305AD28, DXGI_FORMAT_R16_UINT, 0) 6807176673
110 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x0305A290) 6807193096
114 <0x02F83ED8> ID3D10Device::UpdateSubresource(0x02F86510, 0, NULL, 0x014C8610, 192, 192) 6807214651
115 <0x02F83ED8> ID3D10Device::VSSetConstantBuffers(0, 1, 0x014C8860 --> { 0x02F86510 }) 6810108106
119 <0x02F83ED8> ID3D10Device::VSSetShader(0x02F867C8) 6810133767
123 <0x02F83ED8> ID3D10Device::PSSetShader(0x02F86820) 6810203563
124 <0x02F83ED8> ID3D10Device::GSSetShader(NULL) 6810217419
125 <0x02F83ED8> ID3D10Device::DrawIndexed(6, 0, 0) 6810226144 27072
126 <0x02F83ED8> ID3D10Device::OMSetBlendState(0x0305B350, 0x002CF888, -1) 6810347774
127 <0x02F83ED8> ID3D10Device::OMSetDepthStencilState(0x02F92A50, 0) 6810359577
131 <0x0305ADC0> ID3D10Texture2D::Map(0, D3D10_MAP_WRITE, 0, 0x002CF590) 6810497117
132 <0x0305ADC0> ID3D10Texture2D::Unmap(0) 6815603517
141 <0x02F92AD8> ID3D10ShaderResourceView::GetResource(0x002CF5E0 --> 0x02FF38D0) 6816014595
142 <0x02F83ED8> ID3D10Device::CopyResource(0x02FF38D0, 0x0305ADC0) 6819893407
143 <0x02FF38D0> ID3D10Texture2D::Release() 6819943701
147 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x02F83DE8) 6820042750
148 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST) 6820061738
152 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x014E3AE4 --> { 0x0305BE38 }, 0x002CF5EC, 0x002CF5E8) 6820070976
153 <0x02F83ED8> ID3D10Device::RSGetViewports(0x002CF8B0, 0x002CF660) 6822835104
154 <0x0305BDA0> ID3D10Buffer::Map(D3D10_MAP_WRITE_DISCARD, 0, 0x002CF614 --> 0x07055680) 6822853066
155 <0x0305BDA0> ID3D10Buffer::Unmap() 6822867436
156 <0x0305BE38> ID3D10Buffer::Map(D3D10_MAP_WRITE_NO_OVERWRITE, 0, 0x002CF604 --> 0x05AC1000) 6822877187
157 <0x02F83ED8> ID3D10Device::PSSetShaderResources(0, 1, 0x002CF600 --> { 0x02F92AD8 }) 6822899768
161 <0x02F83ED8> ID3D10Device::VSSetShader(0x030092C0) 6822911058
165 <0x02F83ED8> ID3D10Device::GSSetShader(0x02FF3970) 6822925941
166 <0x02F83ED8> ID3D10Device::GSSetConstantBuffers(0, 1, 0x014E3AE8 --> { 0x0305BDA0 }) 6822950575
170 <0x02F83ED8> ID3D10Device::PSSetSamplers(0, 1, 0x014E3AD8 --> { 0x02F878F0 }) 6822959813
171 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6822973156
175 <0x02F83ED8> ID3D10Device::PSSetShader(0x02F83450) 6822981368
176 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6822994198
177 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823001383
178 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823008054
179 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823014726
180 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823021911
181 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823028583
182 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823035254
183 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823041926
184 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823048598
185 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823055269
186 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823061941
187 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823073231
188 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823080416
189 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823087601
190 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823094273
191 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823100945
192 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823107616
193 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823114801
194 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823121473
195 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823128144
196 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823134303
197 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823140975
198 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823148160
199 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823154831
200 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823161503
201 <0x02F92AD8> ID3D10ShaderResourceView::GetDesc(0x002CF59C) 6823168175
202 <0x0305BE38> ID3D10Buffer::Unmap() 6823283646
203 <0x02F83ED8> ID3D10Device::Draw(27, 105) 6823321110 44768
204 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST) 6823477638
208 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x0175C258 --> { 0x02F877F8 }, 0x0175C264, 0x002CF938) 6823488928
212 <0x02F83ED8> ID3D10Device::IASetIndexBuffer(0x02F929B8, DXGI_FORMAT_R16_UINT, 0) 6823512536
213 <0x0305A238> ID3D10ShaderResourceView::AddRef() 6823529985
214 <0x0305A238> ID3D10ShaderResourceView::Release() 6823535630
215 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x03004768) 6823541789
216 <0x02F83ED8> ID3D10Device::UpdateSubresource(0x02F92BC8, 0, NULL, 0x014DA850, 192, 192) 6823558211
217 <0x02F83ED8> ID3D10Device::VSSetConstantBuffers(0, 1, 0x014DAB78 --> { 0x02F92BC8 }) 6823577713
218 <0x02F83ED8> ID3D10Device::VSSetShader(0x02F92C60) 6823586951
219 <0x02F83ED8> ID3D10Device::PSSetSamplers(0, 1, 0x014DABA0 --> { 0x02F87AF8 }) 6823595162
220 <0x02F83ED8> ID3D10Device::PSSetShaderResources(0, 1, 0x014DABA8 --> { 0x0305A238 }) 6823606453
221 <0x02F83ED8> ID3D10Device::PSSetShader(0x02F92CB8) 6823615177
222 <0x02F83ED8> ID3D10Device::GSSetShader(NULL) 6823623388
223 <0x02F83ED8> ID3D10Device::DrawIndexed(6, 0, 0) 6823631086 102656
224 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST) 6823709607
225 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x0175BAE0 --> { 0x02F87940 }, 0x0175BAEC, 0x002CF938) 6823719871
226 <0x02F83ED8> ID3D10Device::IASetIndexBuffer(0x02F92B30, DXGI_FORMAT_R16_UINT, 0) 6823730135
227 <0x0305A238> ID3D10ShaderResourceView::AddRef() 6823739373
228 <0x0305A238> ID3D10ShaderResourceView::Release() 6823745018
229 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x03004768) 6823750150
230 <0x02F83ED8> ID3D10Device::UpdateSubresource(0x02F92BC8, 0, NULL, 0x014DA850, 192, 192) 6823763494
231 <0x02F83ED8> ID3D10Device::VSSetConstantBuffers(0, 1, 0x014DAB78 --> { 0x02F92BC8 }) 6823779403
232 <0x02F83ED8> ID3D10Device::VSSetShader(0x02F92C60) 6823788128
233 <0x02F83ED8> ID3D10Device::PSSetSamplers(0, 1, 0x014DABA0 --> { 0x02F87AF8 }) 6823795826
234 <0x02F83ED8> ID3D10Device::PSSetShaderResources(0, 1, 0x014DABA8 --> { 0x0305A238 }) 6823803524
235 <0x02F83ED8> ID3D10Device::PSSetShader(0x02F92CB8) 6823811222
236 <0x02F83ED8> ID3D10Device::GSSetShader(NULL) 6823817893
237 <0x02F83ED8> ID3D10Device::DrawIndexed(6, 0, 0) 6823825078 2048
238 <0x02F85338> IDXGISwapChain::Present(0, 0) 6828953547

Share this post


Link to post
Share on other sites
I'm not sure what the deal is. Maybe it's the default states being set, though why at that particular point I can't say. In PIX if you do a full frame capture and then go the render panel on the right, PIX will simulate all the calls and then you'll be able to double click on the device to see its exact state (to check the depth state), as well as debug specific pixels. That might help you find out what's going on.

Share this post


Link to post
Share on other sites
Thanks a lot, ET3D for your advice about PIX. I never used it before and didn't know it's purpose. Through PIX i figured out that this calls cause the problem.


<0x02F83ED8> ID3D10Device::OMSetBlendState(0x0305B350, 0x002CF888, -1) 6810347774
127 <0x02F83ED8> ID3D10Device::OMSetDepthStencilState(0x02F92A50, 0) 6810359577
131 <0x0305ADC0> ID3D10Texture2D::Map(0, D3D10_MAP_WRITE, 0, 0x002CF590) 6810497117
132 <0x0305ADC0> ID3D10Texture2D::Unmap(0) 6815603517
141 <0x02F92AD8> ID3D10ShaderResourceView::GetResource(0x002CF5E0 --> 0x02FF38D0) 6816014595
142 <0x02F83ED8> ID3D10Device::CopyResource(0x02FF38D0, 0x0305ADC0) 6819893407
143 <0x02FF38D0> ID3D10Texture2D::Release() 6819943701
147 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x02F83DE8) 6820042750
148 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST) 6820061738
152 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x014E3AE4 --> { 0x0305BE38 }, 0x002CF5EC, 0x002CF5E8) 6820070976


I searched D3D10_PRIMITIVE_TOPOLOGY_POINTLIST and OMSetDepthStencilState in my code: there no such words. I was shocked and disappointed. Then one idea came to my head: besides cartesian system and textured rectangles i'm also displaying info about FPS through ID3DX10Font. I tried to run app without displaying this info. Success! Depth buffer is working! Problem was that ID3DX10Font during it's drawing changed depth buffer state. But now another question: why it is doing so?

Share this post


Link to post
Share on other sites
Quote:
Original post by Resharper
Thanks a lot, ET3D for your advice about PIX. I never used it before and didn't know it's purpose. Through PIX i figured out that this calls cause the problem.


<0x02F83ED8> ID3D10Device::OMSetBlendState(0x0305B350, 0x002CF888, -1) 6810347774
127 <0x02F83ED8> ID3D10Device::OMSetDepthStencilState(0x02F92A50, 0) 6810359577
131 <0x0305ADC0> ID3D10Texture2D::Map(0, D3D10_MAP_WRITE, 0, 0x002CF590) 6810497117
132 <0x0305ADC0> ID3D10Texture2D::Unmap(0) 6815603517
141 <0x02F92AD8> ID3D10ShaderResourceView::GetResource(0x002CF5E0 --> 0x02FF38D0) 6816014595
142 <0x02F83ED8> ID3D10Device::CopyResource(0x02FF38D0, 0x0305ADC0) 6819893407
143 <0x02FF38D0> ID3D10Texture2D::Release() 6819943701
147 <0x02F83ED8> ID3D10Device::IASetInputLayout(0x02F83DE8) 6820042750
148 <0x02F83ED8> ID3D10Device::IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST) 6820061738
152 <0x02F83ED8> ID3D10Device::IASetVertexBuffers(0, 1, 0x014E3AE4 --> { 0x0305BE38 }, 0x002CF5EC, 0x002CF5E8) 6820070976


I searched D3D10_PRIMITIVE_TOPOLOGY_POINTLIST and OMSetDepthStencilState in my code: there no such words. I was shocked and disappointed. Then one idea came to my head: besides cartesian system and textured rectangles i'm also displaying info about FPS through ID3DX10Font. I tried to run app without displaying this info. Success! Depth buffer is working! Problem was that ID3DX10Font during it's drawing changed depth buffer state. But now another question: why it is doing so?


Sprites/fonts tend to use a lot of alpha blending, which doesn't really play nice with depth tests. As such, it makes a lot of sense to disable them.

Share this post


Link to post
Share on other sites

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this