Jump to content
  • Advertisement
Sign in to follow this  
Chris_F

Problems getting D3D11 test working

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

So I thought I'd give a stab at making a quick D3D11 example, based off some examples I have read. When I run it, all I get is a black, mostly unresponsive window and I can't manage to find the culprit. sad.png

main.cpp
[source lang=cpp]
#include <windows.h>

#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10math.h>

#include <fstream>

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")

const int Width = 1024;
const int Height = 768;

struct Vertex
{
D3DXVECTOR3 Position;
D3DXVECTOR3 Normal;
D3DXVECTOR2 Texcoord;
};

struct ConstBuffer
{
D3DXMATRIX World;
D3DXMATRIX View;
D3DXMATRIX Projection;
};

ID3D11Device* pDevice;
ID3D11DeviceContext* pDeviceCon;
IDXGISwapChain* pSwapChain;
ID3D11RenderTargetView* pRenderTargetView;
ID3D11Texture2D* pDepthStencil;
ID3D11DepthStencilView* pDepthStencilView;
D3D11_VIEWPORT viewPort;
ID3D11RasterizerState* pRS;

ID3D11Buffer* pVertexBuffer;
ID3D11Buffer* pIndexBuffer;
ID3D11InputLayout* pVertexLayout;

ID3D11VertexShader* pVertexShader;
ID3D11PixelShader* pPixelShader;

ID3D11Buffer* pCBuffer;
ConstBuffer cBuffer;

ID3D11ShaderResourceView* pTextureRV1;
ID3D11ShaderResourceView* pTextureRV2;

UINT IndexCount;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

void InitD3D(HWND hWnd)
{
//// SET UP DEVICE ////

DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));

swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc.Width = Width;
swapChainDesc.BufferDesc.Height = Height;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;

swapChainDesc.SampleDesc.Count = 1;

swapChainDesc.OutputWindow = hWnd;
swapChainDesc.Windowed = true;

D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
0,
0,
NULL,
0,
D3D11_SDK_VERSION,
&swapChainDesc,
&pSwapChain,
&pDevice,
NULL,
&pDeviceCon
);

//// INPUT ASSEMBLY STAGE ////

ID3DBlob *VS, *PS;
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS, 0, 0);

pDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVertexShader);
pDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPixelShader);

pDeviceCon->VSSetShader(pVertexShader, 0, 0);
pDeviceCon->PSSetShader(pPixelShader, 0, 0);

D3D11_INPUT_ELEMENT_DESC ied[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

pDevice->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pVertexLayout);
pDeviceCon->IASetInputLayout(pVertexLayout);

//// RASTERIZER STAGE SETUP ////

viewPort.Width = Width;
viewPort.Height = Height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;

pDeviceCon->RSSetViewports(1, &viewPort);

D3D11_RASTERIZER_DESC rasterizerState;
rasterizerState.CullMode = D3D11_CULL_NONE;
rasterizerState.FillMode = D3D11_FILL_SOLID;
rasterizerState.FrontCounterClockwise = true;
rasterizerState.DepthBias = false;
rasterizerState.DepthBiasClamp = 0;
rasterizerState.SlopeScaledDepthBias = 0;
rasterizerState.DepthClipEnable = true;
rasterizerState.ScissorEnable = false;
rasterizerState.MultisampleEnable = false;
rasterizerState.AntialiasedLineEnable = true;

pDevice->CreateRasterizerState( &rasterizerState, &pRS);
pDeviceCon->RSSetState(pRS);

//// OUTPUT-MERGER STAGE ////

ID3D11Texture2D* pBackBuffer;
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*) &pBackBuffer);

pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);

pBackBuffer->Release();

D3D11_TEXTURE2D_DESC descDepth;
descDepth.Width = Width;
descDepth.Height = Height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

pDevice->CreateTexture2D( &descDepth, NULL, &pDepthStencil );

D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;

pDevice->CreateDepthStencilView( pDepthStencil, &descDSV, &pDepthStencilView );

pDeviceCon->OMSetRenderTargets( 1, &pRenderTargetView, pDepthStencilView );

//// SET UP SCENE VARIABLES ////

int VertexBufferSize;
int IndexBufferSize;

std::fstream FileIn("face.mesh", std::fstream::in | std::fstream::binary);

FileIn.read((char*)&VertexBufferSize, sizeof(int));
FileIn.read((char*)&IndexBufferSize, sizeof(int));

Vertex* VertexBuffer = new Vertex[VertexBufferSize / sizeof(Vertex)];
UINT* IndexBuffer = new unsigned int[IndexBufferSize / sizeof(UINT)];

FileIn.read((char*)VertexBuffer, VertexBufferSize);
FileIn.read((char*)IndexBuffer, IndexBufferSize);

IndexCount = IndexBufferSize / sizeof(int);

D3D11_BUFFER_DESC bd;
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = VertexBufferSize;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

pDevice->CreateBuffer( &bd, NULL, &pVertexBuffer );

bd.ByteWidth = IndexBufferSize;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;

pDevice->CreateBuffer( &bd, NULL, &pIndexBuffer );

bd.ByteWidth = sizeof(ConstBuffer);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

pDevice->CreateBuffer( &bd, NULL, &pCBuffer );

D3D11_MAPPED_SUBRESOURCE ms;

pDeviceCon->Map(pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, 0, &ms);
memcpy(ms.pData, VertexBuffer, VertexBufferSize);
pDeviceCon->Unmap(pVertexBuffer, 0);
delete[] VertexBuffer;

pDeviceCon->Map(pIndexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, 0, &ms);
memcpy(ms.pData, IndexBuffer, IndexBufferSize);
pDeviceCon->Unmap(pIndexBuffer, 0);
delete[] IndexBuffer;

D3DX11CreateShaderResourceViewFromFile(pDevice, "texture1.bmp", NULL, NULL, &pTextureRV1, NULL);
D3DX11CreateShaderResourceViewFromFile(pDevice, "texture2.bmp", NULL, NULL, &pTextureRV2, NULL);

const D3DXVECTOR3 camera[] =
{
D3DXVECTOR3(0.0f, -0.25f, 4.0f),
D3DXVECTOR3(0.0f, -0.5f, 0.0f),
D3DXVECTOR3(0.0f, 1.0f, 0.0f)
};

D3DXMatrixIdentity(&cBuffer.World);
D3DXMatrixLookAtLH(&cBuffer.View, &camera[0], &camera[1], &camera[2] );
D3DXMatrixPerspectiveFovLH(&cBuffer.Projection, D3DXToRadian(45), (float)Width/(float)Height, 0.1f, 100.0f);

pDeviceCon->Map(pCBuffer, NULL, D3D11_MAP_WRITE_DISCARD, 0, &ms);
memcpy(ms.pData, &cBuffer, sizeof(ConstBuffer));
pDeviceCon->Unmap(pCBuffer, 0);

UINT stride = sizeof(Vertex);
UINT offset = 0;

pDeviceCon->IASetVertexBuffers(0, 1, &pVertexBuffer, &stride, &offset);
pDeviceCon->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
pDeviceCon->VSSetConstantBuffers(0, 1, &pCBuffer);
}

void CleanUp()
{
if (pDevice) pDevice->Release();
if (pDeviceCon) pDeviceCon->Release();
if (pSwapChain) pSwapChain->Release();
if (pRenderTargetView) pRenderTargetView->Release();
if (pDepthStencil) pDepthStencil->Release();
if (pDepthStencilView) pDepthStencilView->Release();
if (pRS) pRS->Release();
if (pVertexBuffer) pVertexBuffer->Release();
if (pIndexBuffer) pIndexBuffer->Release();
if (pCBuffer) pCBuffer->Release();
if (pTextureRV1) pTextureRV1->Release();
if (pTextureRV2) pTextureRV2->Release();
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wc.lpszClassName = "WindowClass1";

RegisterClassEx(&wc);

RECT wndSize = {0, 0, Width, Height};
AdjustWindowRect(&wndSize, WS_OVERLAPPEDWINDOW, FALSE);
hWnd = CreateWindowEx(WS_EX_LTRREADING,
"WindowClass1",
"DirectX",
WS_OVERLAPPEDWINDOW,
0, 0,
wndSize.right - wndSize.left,
wndSize.bottom - wndSize.top,
NULL, NULL,
hInstance, NULL);

ShowWindow(hWnd, nCmdShow);

InitD3D(hWnd);

MSG msg = {0};
while (WM_QUIT != msg.message)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

//RENDER

pDeviceCon->ClearRenderTargetView( pRenderTargetView, D3DXCOLOR(1,0,0,1) );
pDeviceCon->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );

pDeviceCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
pDeviceCon->DrawIndexed( IndexCount, 0, 0 );
}

CleanUp();
return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}
[/source]

shaders.hlsl
[source lang=cpp]
cbuffer ConstBuffer
{
matrix World;
matrix View;
matrix Projection;
};

struct PS_INPUT
{
float4 position : SV_POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD;
};

struct VS_INPUT
{
float4 position : POSITION;
float4 normal : NORMAL;
float2 texcoord : TEXCOORD;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output;

output.position = mul( input.position, World );
output.position = mul( output.position, View );
output.position = mul( output.position, Projection );
output.color = input.normal;
output.texcoord = input.texcoord;
return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input ) : SV_Target
{
return input.color;
}
[/source]

Share this post


Link to post
Share on other sites
Advertisement
You should try using the D3D11_CREATE_DEVICE_DEBUG flag when creating the device, it will usually tell you when you screw something up.

Share this post


Link to post
Share on other sites
It seems it is failing to create a depth stencil view for some reason.

EDIT:

Ok, I forgot to zero out my D3D11_DEPTH_STENCIL_VIEW_DESC. Fixing that means no more failed HRESULTS, but I still only get a black screen and the window's response to events is very slow.

EDIT2:

I take that back. Every window lags when I'm running this. It's like it's murdering my graphics card or something.

Final Edit:

I forgot to present with my swap chain. rolleyes.gif

But unfortunately I still see nothing rendered. sad.png

Share this post


Link to post
Share on other sites
Thanks! That did it. But why?

I have almost identical code that uses D3D10 instead, and it works flawlessly without ever calling D3DXMatrixTranspose.

EDIT: Hmm. My D3D10 code makes use of EffectMatrixVariable for setting matrices. I assume this automatically transposes the matrix?

Share this post


Link to post
Share on other sites
I'm going to guess that shaders like matrices column major and your matrices in C++ are row major. Thus, D3DXMatrixTranspose() will transpose the matrix before sending it off to the shader.

Share this post


Link to post
Share on other sites
By default shaders expect column-major matrices, since a multiply can be expressed a bit more efficiently in assembly using dot products. You can change that by declaring matrices with the row_major modifier, or by passing D3D10_SHADER_PACK_MATRIX_ROW_MAJOR when compiling the shader. The effects framework handles automatically transposing for you, which is why you wouldn't have to with that framework.

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!