• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Chris_F

Problems getting D3D11 test working

7 posts in this topic

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. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

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]
0

Share this post


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

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. [i]Every[/i] 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. [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

But unfortunately I still see nothing rendered. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
0

Share this post


Link to post
Share on other sites
If I comment out the application of the View and Projection matrix in the shader, I get this:

[img]http://s7.postimage.org/ymzo867ex/Untitled_1.jpg[/img]
[url]http://s7.postimage.org/ymzo867ex/Untitled_1.jpg[/url]

But with them applied, I get a black screen. Does anyone have any idea what is causing this?
0

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?
0

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.
0

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.
0

Share this post


Link to post
Share on other sites

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  
Followers 0