Jump to content
  • Advertisement
Sign in to follow this  
dr4cula

DirectX11 entry point

This topic is 2105 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

Hello,

 

I've been trying to set up my own DirectX entry point but I've run into a dead end: I get a black screen and absolutely no errors (even though I'm checking every possible HRESULT returned by functions). Since the DirectX debugger (forced through the dx control panel) doesn't report any errors, I'm thinking that my DirectX should be initialised correctly, in which case the problem is with my shader usage.

 

Here's my main testing:

HLSLEffect testEffect(sysD3D.getDevice(), sysD3D.getDeviceContext());
 
testEffect.CompileShader(L"test.vs", HLSLEffect::VertexShader);
testEffect.CompileShader(L"test.ps", HLSLEffect::PixelShader);
 
//float vertices[] = {0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
D3DXVECTOR3 vertices[] = {D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR3(0.5f, 1.0f, 0.0f), D3DXVECTOR3(1.0f, 0.0f, 0.0f)};
unsigned long int indices[] = {0, 1, 2};
 
D3D11_SUBRESOURCE_DATA indexData;
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
 
testEffect.AddIndexBuffer(3 * sizeof(unsigned long int), &indexData);
 
D3D11_SUBRESOURCE_DATA vertData;
vertData.pSysMem = vertices;
vertData.SysMemPitch = 0;
vertData.SysMemSlicePitch = 0;
 
testEffect.AddVertexBuffer(3 * sizeof(D3DXVECTOR3), &vertData);
 
D3D11_INPUT_ELEMENT_DESC descs[1];
descs[0].SemanticName = "POSITION";
descs[0].SemanticIndex = 0;
descs[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
descs[0].InputSlot = 0;
descs[0].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
descs[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
descs[0].InstanceDataStepRate = 0;
 
testEffect.SetInputLayout(descs, 1, HLSLEffect::VertexShader);
 
struct Matrices {
D3DXMATRIX worldMatrix;
D3DXMATRIX viewMatrix;
D3DXMATRIX projectionMatrix;
};
 
Matrices matrices;
D3DXMatrixIdentity(&matrices.worldMatrix);
D3DXMatrixPerspectiveFovLH(&matrices.projectionMatrix, D3DX_PI / 4.0f, static_cast<float>(WINDOW_WIDTH) / static_cast<float>(WINDOW_HEIGHT), 0.1f, 1000.0f);
 
D3DXVECTOR3 eye(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&matrices.viewMatrix, &eye, &at, &up);
 
D3DXMatrixTranspose(&matrices.worldMatrix, &matrices.worldMatrix);
D3DXMatrixTranspose(&matrices.viewMatrix, &matrices.viewMatrix);
D3DXMatrixTranspose(&matrices.projectionMatrix, &matrices.projectionMatrix);
 
D3D11_SUBRESOURCE_DATA cBuffer;
cBuffer.pSysMem = &matrices;
cBuffer.SysMemPitch = 0;
cBuffer.SysMemSlicePitch = 0;
 
testEffect.AddConstantBuffer(sizeof(Matrices), &cBuffer, HLSLEffect::VertexShader);
 
testEffect.Use();
 
And the loop:
float clearColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
 
sysD3D.Clear(clearColor);
 
sysD3D.getDeviceContext()->DrawIndexed(3, 0, 0);
 
sysD3D.Present();
 
Relevant (imo) effect class code:
 
void HLSLEffect::AddIndexBuffer(UINT size, D3D11_SUBRESOURCE_DATA* data) {
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = size;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 0;
 
HRESULT bufferCreation = p_device_->CreateBuffer(&bufferDesc, data, &p_indexBuffer_);
if(FAILED(bufferCreation)) {
MessageBox(NULL, L"Failed creating the index buffer.", L"Error", 0);
return;
}
 
p_deviceContext_->IASetIndexBuffer(p_indexBuffer_, DXGI_FORMAT_R32_UINT, 0);
 
}
 
void HLSLEffect::AddVertexBuffer(UINT size, D3D11_SUBRESOURCE_DATA* data) {
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = size;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 0;
 
HRESULT bufferCreation = p_device_->CreateBuffer(&bufferDesc, data, &p_vertexBuffer_);
if(FAILED(bufferCreation)) {
MessageBox(NULL, L"Failed creating the vertex buffer.", L"Error", 0);
return;
}
 
ID3D11Buffer* buffers[] = {p_vertexBuffer_};
UINT strides[] = {sizeof(D3DXVECTOR3)};
UINT offsets[] = {0};
 
p_deviceContext_->IASetVertexBuffers(0, 1, buffers, strides, offsets);
}
 
void HLSLEffect::AddConstantBuffer(UINT size, D3D11_SUBRESOURCE_DATA* data, ShaderType type) {
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = size;
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 0;
 
 
 
HRESULT bufferCreation = p_device_->CreateBuffer(&bufferDesc, data, &cBuffer);
if(FAILED(bufferCreation)) {
MessageBox(NULL, L"Failed creating the constant buffer.", L"Error", 0);
return;
}
 
switch(type) {
case VertexShader:
p_deviceContext_->VSSetConstantBuffers(0, 1, &cBuffer);
break;
 
case PixelShader:
p_deviceContext_->PSSetConstantBuffers(0, 1, &cBuffer);
break;
}
}
 
void HLSLEffect::SetInputLayout(D3D11_INPUT_ELEMENT_DESC* descs, UINT count, ShaderType sigFrom) {
void* byteCode;
size_t byteCodeLength;
 
switch(sigFrom) {
case VertexShader:
byteCode = p_vertexShaderBlob_->GetBufferPointer();
byteCodeLength = p_vertexShaderBlob_->GetBufferSize();
break;
 
case PixelShader:
byteCode = p_pixelShaderBlob_->GetBufferPointer();
byteCodeLength = p_pixelShaderBlob_->GetBufferSize();
break;
}
 
HRESULT layoutCreation = p_device_->CreateInputLayout(descs, count, byteCode, byteCodeLength, &p_inputLayout_);
 
if(FAILED(layoutCreation)) {
MessageBox(NULL, L"Failed creating the input layout.", L"Error", 0);
}
 
p_deviceContext_->IASetInputLayout(p_inputLayout_);
}
 
void HLSLEffect::Use() {
p_deviceContext_->IASetInputLayout(p_inputLayout_);
p_deviceContext_->VSSetShader(p_vertexShader_, NULL, 0);
p_deviceContext_->PSSetShader(p_pixelShader_, NULL, 0);
}
 
And finally shaders:
vertex:
struct vsInput {
float3 position : POSITION;
};
 
struct vsOutput {
float4 position : SV_POSITION;
float4 color : COLOR;
};
 
cbuffer Matrices {
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
 
vsOutput vsMain(vsInput input) {
vsOutput output;
output.position = float4(input.position, 1.0f);
 
output.position = mul(output.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
 
output.color = float4(1.0f, 1.0f, 1.0f, 1.0f);
 
return output;
}
 
pixel:
struct psIn {
float4 position : SV_POSITION;
float4 color : COLOR;
};
 
float4 psMain(psIn vertIn) : SV_TARGET {
return vertIn.color;
}
 
This results in a black screen.
DirectX debugger:
 
D3D11 INFO: Create ID3D11Context: Name="unnamed", Addr=0x003DC010, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097225: CREATE_CONTEXT]
D3D11 INFO: Create ID3DDeviceContextState: Name="unnamed", Addr=0x003CFCC8, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #3145735: CREATE_DEVICECONTEXTSTATE]
D3D11 INFO: Create ID3D11BlendState: Name="unnamed", Addr=0x003D2D74, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097270: CREATE_BLENDSTATE]
D3D11 INFO: Create ID3D11DepthStencilState: Name="unnamed", Addr=0x003D2F54, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097273: CREATE_DEPTHSTENCILSTATE]
D3D11 INFO: Create ID3D11RasterizerState: Name="unnamed", Addr=0x003D309C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097276: CREATE_RASTERIZERSTATE]
D3D11 INFO: Create ID3D11Sampler: Name="unnamed", Addr=0x003D323C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097267: CREATE_SAMPLER]
D3D11 INFO: Create ID3D11Query: Name="unnamed", Addr=0x003D33F4, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097279: CREATE_QUERY]
D3D11 INFO: Create ID3D11Texture2D: Name="unnamed", Addr=0x003D3AA4, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097234: CREATE_TEXTURE2D]
D3D11 INFO: Create ID3D11RenderTargetView: Name="unnamed", Addr=0x003D3D4C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097243: CREATE_RENDERTARGETVIEW]
D3D11 INFO: Create ID3D11Texture2D: Name="unnamed", Addr=0x003D3FD4, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097234: CREATE_TEXTURE2D]
D3D11 INFO: Create ID3D11DepthStencilView: Name="unnamed", Addr=0x003D423C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097246: CREATE_DEPTHSTENCILVIEW]
'DirectX 11.exe': Loaded 'C:\Windows\SysWOW64\D3DCompiler_43.dll', Cannot find or open the PDB file
D3D11 INFO: Create ID3D11VertexShader: Name="unnamed", Addr=0x003D4584, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097249: CREATE_VERTEXSHADER]
D3D11 INFO: Create ID3D11PixelShader: Name="unnamed", Addr=0x00401AEC, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097261: CREATE_PIXELSHADER]
D3D11 INFO: Create ID3D11Buffer: Name="unnamed", Addr=0x00401D5C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097228: CREATE_BUFFER]
D3D11 INFO: Create ID3D11Buffer: Name="unnamed", Addr=0x00401FE4, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097228: CREATE_BUFFER]
D3D11 INFO: Create ID3D11InputLayout: Name="unnamed", Addr=0x0040226C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097264: CREATE_INPUTLAYOUT]
D3D11 INFO: Create ID3D11Buffer: Name="unnamed", Addr=0x00402C14, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097228: CREATE_BUFFER]
 
Any help would be greatly appreciated.
 
Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

The console debug mode function doesn't exist/work for DX11 anymore, instead you have create your device with the appropriate flag:

				UINT createDeviceFlags = 0;
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

				if(FAILED(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, nullptr, 0, D3D11_SDK_VERSION, &description, &m_pSwapChain, &m_pDevice, &m_featureLevel, &m_pContext)))
					throw d3dException();

Still no errors shown? In that case, use a graphics debugger like nsight, amd perf studio, or the one integrated in VS2012 (PIX is also an option, but AFAIK currently broken badly at least on Win8). Inspect your draw calls, and that states that are set to the device at that point, and see if there is anything suspicious.

Share this post


Link to post
Share on other sites

Tried the debug flag and called this right before my draw call:

p_device_->QueryInterface(__uuidof(ID3D11Debug), reinterpret_cast<void**>(&p_debugger_));
HRESULT contextValidation = p_debugger_->ValidateContext(p_deviceContext_);
if(FAILED(contextValidation)) {
MessageBox(NULL, L"Context validation failed.", L"Error", 0);
}

And still nothing...

 

Perf studio frame debugger shows me this: http://i44.tinypic.com/2wfn3ty.png

I tried changing the winding of the triangle's vertices but that didn't change much - the triangle doesn't appear in the window but it does appear in the frame debugger again, though it's a slightly different than the other one.

But the actual frame window is still showing black.

 

EDIT: So, I managed to fix it.I started comparing my configuration to the ones found online and the problem was the following line:

 

p_deviceContext_->ClearDepthStencilView(p_depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0);

 

I had 0.0f instead of 1.0f for the clear value, which I assume didn't match with the OP function set for the depth buffer. The depth buffer has screwed me over in OpenGL and I guess it keeps haunting me in DirectX as well :P Though, Perf studio output still is odd, considering the actual result is this: http://i43.tinypic.com/dxctva.png

 

Anyways, Juliean, thanks for the great tool suggestions! Couldn't try out nvidia's one because I needed an account (need to wait for verification) but definitely will try it out since I have an nvidia card anyways.

Edited by dr4cula

Share this post


Link to post
Share on other sites

I don't normally use the effect framework, so I can't help there, but are you sure that your geometry is going to be within the view frustum?  Do you have access to the Graphics Debugger in Visual Studio (for VS2012 it requires one of the paid versions - i.e. Pro or better)?  That would help you rather quickly get access to the pipeline state and help you figure out what is going wrong.

Share this post


Link to post
Share on other sites


The console debug mode function doesn't exist/work for DX11 anymore, instead you have create your device with the appropriate flag:

You might be interested to check out this article over on AltDevBlog: http://www.altdevblogaday.com/2013/09/30/fixing-the-directx-d3d-debug-layer/.  I haven't tried this myself, but it seems that the control panel has simply moved due to the inclusion of the DX components in the Windows SDK.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!