# "Flickering" geometry?

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

## Recommended Posts

To better understand the concepts of setting up a 3D rendering pipeline in D3D10, I decided to go through all the steps again from scratch.
And after some trial and error, I finally got a rotating cube to render... But it renders as if it were at an underground rave.
When I have D3D10_CULL_NONE set, it still does this.
So perhaps it's the ordering of my vertices?

Here's a video showing the problem (I manually switched to wireframe mode 1/2 way through):
[media]
[/media]

And here's my vertex setup (rendering it as a triangle strip):
struct BasicVertex { D3DXVECTOR3 pos; D3DXVECTOR4 color; BasicVertex( D3DXVECTOR3 p, D3DXVECTOR4 c ) : pos(p), color(c) {} }; // Front Face (1-2-3-4) m_vertices[0] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f) ); m_vertices[1] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.68f, 0.68f, 1.0f) ); m_vertices[2] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.68f, 0.68f, 1.0f) ); m_vertices[3] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f) ); // Right Face (2-6-4-8) m_vertices[4] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f) ); m_vertices[5] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR4(1.0f, 0.68f, 0.68f, 1.0f) ); m_vertices[6] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.68f, 0.68f, 1.0f) ); m_vertices[7] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f) ); // Top Face (5-6-1-2) m_vertices[8] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f) ); m_vertices[9] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR4(0.68f, 0.68f, 1.0f, 1.0f) ); m_vertices[10] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR4(0.68f, 0.68f, 1.0f, 1.0f) ); m_vertices[11] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f) ); // Back Face (6-5-8-7) m_vertices[12] = BasicVertex( D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f) ); m_vertices[13] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR4(1.0f, 1.0f, 0.68f, 1.0f) ); m_vertices[14] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR4(1.0f, 1.0f, 0.68f, 1.0f) ); m_vertices[15] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f) ); // Left Face (5-1-7-3) m_vertices[16] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR4(1.0f, 0.0f, 1.0f, 1.0f) ); m_vertices[17] = BasicVertex( D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.68f, 1.0f, 1.0f) ); m_vertices[18] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR4(1.0f, 0.68f, 1.0f, 1.0f) ); m_vertices[19] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR4(1.0f, 0.0f, 1.0f, 1.0f) ); // Bottom Face (3-4-7-8) m_vertices[20] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR4(0.0f, 1.0f, 1.0f, 1.0f) ); m_vertices[21] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR4(0.68f, 1.0f, 1.0f, 1.0f) ); m_vertices[22] = BasicVertex( D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR4(0.68f, 1.0f, 1.0f, 1.0f) ); m_vertices[23] = BasicVertex( D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR4(0.0f, 1.0f, 1.0f, 1.0f) );

... And here's my initialization code:
bool D3DClass::Initialize(int width, int height, HWND hwnd, const bool fullscreen, const float screenDepth, const float screenNear) { ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DEFINE SwapChain ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DXGI_SWAP_CHAIN_DESC sd; SecureZeroMemory( &sd, sizeof(DXGI_SWAP_CHAIN_DESC) ); sd.BufferDesc.Width = width; sd.BufferDesc.Height = height; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; // No multisampling. sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferCount = 1; sd.OutputWindow = hwnd; sd.Windowed = fullscreen; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.Flags = 0; UINT createDeviceFlags = 0; #if defined(DEBUG) || defined(_DEBUG) createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG; #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CREATE Device and Swap Chain ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(FAILED( D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE, 0, createDeviceFlags, D3D10_SDK_VERSION, &sd, &m_swapChain, &m_d3dDevice ))) { return false; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CREATE BackBuffer ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(FAILED( m_swapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), reinterpret_cast<void**>(&m_backBuffer)) )) { ReleaseCOM(m_swapChain); ReleaseCOM(m_d3dDevice); return false; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CREATE RenderTargetView ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(FAILED( m_d3dDevice->CreateRenderTargetView(m_backBuffer, 0, &m_renderTargetView) )) { ReleaseCOM(m_swapChain); ReleaseCOM(m_d3dDevice); return false; } ReleaseCOM(m_backBuffer); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DEFINE DepthStencil Buffer ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// D3D10_TEXTURE2D_DESC depthBufferDesc; SecureZeroMemory( &depthBufferDesc, sizeof(D3D10_TEXTURE2D_DESC) ); depthBufferDesc.Width = width; depthBufferDesc.Height = height; depthBufferDesc.MipLevels = 1; depthBufferDesc.ArraySize = 1; depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthBufferDesc.SampleDesc.Count = 1; // multisampling must match depthBufferDesc.SampleDesc.Quality = 0; // swap chain values. depthBufferDesc.Usage = D3D10_USAGE_DEFAULT; depthBufferDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL; depthBufferDesc.CPUAccessFlags = 0; depthBufferDesc.MiscFlags = 0; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DEFINE DepthStencil View ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// D3D10_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; SecureZeroMemory(&depthStencilViewDesc, sizeof(D3D10_DEPTH_STENCIL_VIEW_DESC)); depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthStencilViewDesc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D; depthStencilViewDesc.Texture2D.MipSlice = 0; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CREATE DepthStencil Buffer ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(FAILED( m_d3dDevice->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer)) ) { ReleaseCOM(m_renderTargetView); ReleaseCOM(m_swapChain); ReleaseCOM(m_d3dDevice); return false; } if(FAILED( m_d3dDevice->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView)) ) { ReleaseCOM(m_depthStencilBuffer); ReleaseCOM(m_renderTargetView); ReleaseCOM(m_swapChain); ReleaseCOM(m_d3dDevice); return false; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BIND Target View ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// m_d3dDevice->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DEFINE Rasterizer State ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// m_rasterDesc.AntialiasedLineEnable = true; m_rasterDesc.CullMode = D3D10_CULL_NONE; m_rasterDesc.DepthBias = 0; m_rasterDesc.DepthBiasClamp = 0.0f; m_rasterDesc.DepthClipEnable = true; m_rasterDesc.FillMode = D3D10_FILL_SOLID; m_rasterDesc.FrontCounterClockwise = false; m_rasterDesc.MultisampleEnable = false; m_rasterDesc.ScissorEnable = false; m_rasterDesc.SlopeScaledDepthBias = 0.0f; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CREATE Rasterizer State ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(FAILED( m_d3dDevice->CreateRasterizerState(&m_rasterDesc, &m_rasterState) )) { return false; } m_d3dDevice->RSSetState(m_rasterState); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // DEFINE Viewport ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// D3D10_VIEWPORT vp; vp.TopLeftX = 0; vp.TopLeftY = 0; vp.Width = width; vp.Height = height; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BIND Viewport ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// m_d3dDevice->RSSetViewports(1, &vp); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // SET Local Variables ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// m_screenWidth = width; m_screenHeight = height; float fieldOfView = (float)0.1f; float screenAspect = (float)m_screenWidth / (float)m_screenHeight; D3DXMatrixIdentity(&m_matrixWorld); D3DXMatrixPerspectiveFovLH(&m_matrixProjection, fieldOfView, screenAspect, screenNear, screenDepth); return true; }

I'm damn near getting this in the bag for good, but I can't figure out what I'm doing differently.
I just really want to make sure that I fully understand this and can do it properly every time.

EDIT:
I just sliced it down to the first 3 vertices (to only render the first triangle), and it still flickered.
And for some reason, it doesn't show at all unless it is actually moving. So perhaps it might be my window setup?
I'll keep tweaking around and checking back.

##### Share on other sites
Figured it out...

m_d3dDevice-&gt;OMSetRenderTargets(1, &amp;m_renderTargetView, m_depthStencilView);
...should've been:
m_d3dDevice->OMSetRenderTargets(1, &m_renderTargetView, NULL);

Can anybody tell me why though?
My book doesn't omit this value, so I don't understand why it works without it, unless it defaults to something that I don't have set up properly to begin with?

##### Share on other sites
Do you clear that depth buffer every frame?

##### Share on other sites
void D3DClass::BeginScene(float red, float green, float blue, float alpha) { // Clear the render target to the color specified by mClearColor. m_d3dDevice->ClearRenderTargetView(m_renderTargetView, D3DXCOLOR(red, green, blue, alpha)); // Clear the depth buffer to 1.0, and clear the stencil buffer to 0. m_d3dDevice->ClearDepthStencilView(m_depthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.0f, 0); }

...Oddly enough, after texturing my cube and reverting the OMSetRenderTargets call to use m_depthStencilView again, it works fine now... unless I comment out the call to ClearDepthStencilView.
But if I set the 3rd parameter in the OMSetRenderTargets call to NULL, it works irregardless if ClearDepthStencilView is commented out or not!?

I'm beginning to wonder if it was something wrong with my shader?
//-------------------------------------------------------------------------------------- // Constant Buffer Variables //-------------------------------------------------------------------------------------- matrix World; matrix View; matrix Projection; //-------------------------------------------------------------------------------------- struct VS_INPUT { float4 Pos : POSITION; float4 Color : COLOR; }; struct PS_INPUT { float4 Pos : SV_POSITION; float4 Color : COLOR0; }; //-------------------------------------------------------------------------------------- // Vertex Shader //-------------------------------------------------------------------------------------- PS_INPUT VS( VS_INPUT input ) { PS_INPUT output = (PS_INPUT)0; output.Pos = mul( input.Pos, World ); output.Pos = mul( output.Pos, View ); output.Pos = mul( output.Pos, Projection ); output.Color = input.Color; return output; } //-------------------------------------------------------------------------------------- // Pixel Shader //-------------------------------------------------------------------------------------- float4 PS( PS_INPUT input) : SV_Target { return input.Color; } //-------------------------------------------------------------------------------------- technique10 Render { pass P0 { SetVertexShader( CompileShader( vs_4_0, VS() ) ); SetGeometryShader( NULL ); SetPixelShader( CompileShader( ps_4_0, PS() ) ); } }

##### Share on other sites
Are you setting up and binding a depth-stencil state? You'll need to do this if you want to make use of the depth buffer.

##### Share on other sites
I've had this problem before. My issue was that the vertex format didn't match what the shader expected. I noticed you're position is a Vec3 in your CPU code but your shader has a Vec4. Could this be the issue?

##### Share on other sites

I've had this problem before. My issue was that the vertex format didn't match what the shader expected. I noticed you're position is a Vec3 in your CPU code but your shader has a Vec4. Could this be the issue?

No, it's okay to do that. The shader will put a 1.0 in the W component if you do this. Creating an input layout will just fail outright if you have an invalid mismatch between your vertex buffer and vertex shader inputs, and the debug layer will give you error messages about it.

##### Share on other sites

Are you setting up and binding a depth-stencil state? You'll need to do this if you want to make use of the depth buffer.

I believe so. But even if I'm not, it seems to be working just fine now.

I've had this problem before. My issue was that the vertex format didn't match what the shader expected. I noticed you're position is a Vec3 in your CPU code but your shader has a Vec4. Could this be the issue?

The issue was 2 things: Vertex order, and THIS (post #2).

... I've set things up to use only 8 vertices and 14 indices, in what seems to be the proper order.
Of course, this makes things rather complicated when trying to apply a texture to one of the cube's faces, since the texture coords are defined per vertex.

EDIT:
No, I wasn't binding a depth stencil state. I'll add that in now.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11