DX11 not drawing anything

Started by
12 comments, last by kauna 12 years ago
Traditionally I've worked with DX9, and I've just started experimenting with DX11 without DXUT. I can't get anything to draw, despite code being more or less copied from a project that does work. PiX shows that the triangle vertices I'm trying to draw are being transformed correctly, and that it's inside the viewport. The shader I'm using is hard-rigged to return white. What kind of problem could prevent something from drawing entirely?

I'd post code if there wasn't a ton of it split across several files, but here's a pix screenshot. happy.png

http://i.imgur.com/DlEZr.png
Advertisement
set render target? :D
Heheh.

Checklist of stuff I have done with descriptions, and valid checks removed for brevity.

1. Swap chain.

swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = screenWidth;
swapChainDesc.BufferDesc.Height = screenHeight;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = true;
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = 0;
featureLevel = D3D_FEATURE_LEVEL_11_0;
result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &mSwapChain, &mDevice, NULL, &mContext);


2. Create back buffer.

// Get the pointer to the back buffer.
result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
// Create the render target view with the back buffer pointer.
result = mDevice->CreateRenderTargetView(backBufferPtr, NULL, &mRenderTargetView);


3. Set up depth/stencil buffer and render target view

// Set up the description of the depth buffer.
depthBufferDesc.Width = screenWidth;
depthBufferDesc.Height = screenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;
// Create the texture for the depth buffer using the filled out description.
mDevice->CreateTexture2D(&depthBufferDesc, NULL, &mDepthStencilBuffer);
// Set up the description of the stencil state.
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
depthStencilDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
// Stencil operations if pixel is front-facing.
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing.
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Create the depth stencil state.
mDevice->CreateDepthStencilState(&depthStencilDesc, &mDepthStencilState);
// Set the depth stencil state.
mContext->OMSetDepthStencilState(mDepthStencilState, 1);
// Set up the depth stencil view description.
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
// Create the depth stencil view.
mDevice->CreateDepthStencilView(mDepthStencilBuffer, &depthStencilViewDesc, &mDepthStencilView);

// Bind the render target view and depth stencil view to the pipeline.
mContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView);


4. Create rasterizer state, (although I've tried removing this entirely to no effect)

// Setup the raster description which will determine how and what polygons will be drawn.
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_NONE;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;
// Create the rasterizer state from the description we just filled out.
mDevice->CreateRasterizerState(&rasterDesc, &mRasterState);
// Now set the rasterizer state.
mContext->RSSetState(mRasterState);



5. Created the viewport

// Setup the viewport for rendering.
viewport.Width = (float)screenWidth;
viewport.Height = (float)screenHeight;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
// Create the viewport.
mContext->RSSetViewports(1, &viewport);


6. Set the input layout.

D3DX11_PASS_SHADER_DESC effectVsDesc;
mPass->GetVertexShaderDesc(&effectVsDesc);
D3DX11_EFFECT_SHADER_DESC effectVsDesc2;
effectVsDesc.pShaderVariable->GetShaderDesc(effectVsDesc.ShaderIndex, &effectVsDesc2);
const void *vsCodePtr = effectVsDesc2.pBytecode;
unsigned vsCodeLen = effectVsDesc2.BytecodeLength;
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
polygonLayout[0].SemanticName = "POSITION";
polygonLayout[0].SemanticIndex = 0;
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[0].InputSlot = 0;
polygonLayout[0].AlignedByteOffset = 0;
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[0].InstanceDataStepRate = 0;
polygonLayout[1].SemanticName = "TEXCOORD";
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[1].InputSlot = 0;
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;
GraphicsMan->GetDevice()->CreateInputLayout(polygonLayout, sizeof(polygonLayout) / sizeof(polygonLayout[0]), vsCodePtr, vsCodeLen, &mLayout);


7. Set the active vertex/pixel shaders via apply() of the effect framework.

And right. I am using the D3DX11Effect framework, if there is some inherent flaw with it I don't know about.

Edit: Also I guess my topic name is ~misleading; it is showing my clear color at least.
You don't seem to be clearing your depth buffer.
I am, yeah. Here's my draw call.


float color[] = { 160.0 / 255.0, 69.0 / 255.0, 32.0 / 255.0, 0 };
// Clear the back buffer.
mContext->ClearRenderTargetView(mRenderTargetView, color);

// Clear the depth buffer.
mContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);

// Set vertex/index buffer.
mBuffer.ApplyBuffer();

// Update camera.
mCamera.Update();

// Set shader technique, pass, vars, and apply.
mEffect.SetTechnique(0);
mEffect.SetPass(0);
Matrix mx; D3DXMatrixIdentity(&mx);
mEffect.SetVariable("World", mx);
mEffect.SetVariable("View", mCamera.GetView());
mEffect.SetVariable("Projection", mCamera.GetProjection());
mEffect.Apply();

// Draw
mContext->DrawIndexed(mBuffer.GetIndexCount(), 0, 0);

// Present
mSwapChain->Present(0, 0);

Maybe triangle is backfacing?
I tried flipping the indices around, but also above there's the line:

rasterDesc.CullMode = D3D11_CULL_NONE;

on the rasterizer state description.

I'm positive whatever this is will be a stupid 'Doh mistake moment. >_>
Hm... It seems according to the PIX triangle is on the screen. Have you tried to debug pixel in that area? You'll see whether there's anything wrong with shader output, depth pass, or maybe it's not reaching pixel shader at all?
Debugging the pixel shows no change, except from the initial black frame buffer to the clear color.

What could prevent the pixel shader from working?



float4x4 World;
float4x4 View;
float4x4 Projection;
texture2D Texture;
SamplerState textureSampler {
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

struct VertexShaderInput
{
float4 Position : POSITION0;
//float4 Normal : NORMAL0;
float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TextureCoordinate : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

//output.Position = input.Position;
output.Position = mul(input.Position, World);
output.Position = mul(output.Position, View);
output.Position = mul(output.Position, Projection);

output.TextureCoordinate = input.TextureCoordinate;
return output;
};

float4 PixelShaderFunction(VertexShaderOutput input) : SV_TARGET
{
float3 color = float3(255, 255, 255);
color = normalize(color);
return float4(color, 1);
//return Texture.Sample(textureSampler, input.TextureCoordinate);
};

technique11 Textured
{
pass p0
{
SetVertexShader(CompileShader(vs_5_0, VertexShaderFunction()));
SetPixelShader(CompileShader(ps_5_0, PixelShaderFunction()));
}
};


float4 PixelShaderFunction(VertexShaderOutput input) : SV_TARGET
{
float3 color = float3(255, 255, 255);
color = normalize(color);
return float4(color, 1);
//return Texture.Sample(textureSampler, input.TextureCoordinate);
};



Shader colors range from 0.0 to 1.0. If you normalize (255, 255, 255) it'll be about (0.58, 0.58, 0.58) and that's just gray.
You could simply do return float4(1, 1, 1, 1) to return white color.

Try to remove blending/depth states, just to see if there's anything wrong with those. DirectX will use default values if no state is set.

mD3D11DeviceContext->RSSetState(0);
mD3D11DeviceContext->OMSetBlendState(0, 0, 0xFFFFFFFF);
mD3D11DeviceContext->OMSetDepthStencilState(0, 0);

This topic is closed to new replies.

Advertisement