Jump to content
  • Advertisement
Sign in to follow this  
kopilatis

DX11 Help in Directx11(June SDK)

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

I started learning directx a couple of days ago and as of today I can't even put a triangle on the screenangry.gif(with code I wrote that is). Anyway I have this code right here and it looks EXACTLY the same as the 3rd tutorial on the documentation. Problem is, when I run mine the only thing I see is a blue screen and when I run the documentation example it runs fine. Can someone point out the problem here ? It's realy driving me crazy.

P.S. I know it's horribly written and there is no clean up but I'm just trying to learn here.

#include <Windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#include <xnamath.h>
#include <fstream>
#include <D3DX10math.h>

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

/////////////////////////
//Forward Declarations//
///////////////////////

struct SimpleVertex
{
XMFLOAT3 Pos;
};

void Render();
HRESULT CompileShaderFromFile(WCHAR* fileName, LPCSTR entryPoint, LPCSTR shaderModel, ID3DBlob** blobOut, HWND hwnd);
IDXGISwapChain* g_swapChain;
ID3D11Device* g_device;
ID3D11DeviceContext* g_deviceContext;
ID3D11RenderTargetView* g_renderTargetView;
ID3D11VertexShader* gVertexShader;
ID3D11PixelShader* gPixelShader;
ID3D11InputLayout* gInputLayout;
ID3D11Buffer* gVertexBuffer;
ID3D11Buffer* gIndexBuffer;
ID3D11Buffer* gConstantBuffer;

LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(umsg)
{
case WM_QUIT:
{
//SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
}
case WM_PAINT:
hdc = BeginPaint( hwnd, &ps );
EndPaint( hwnd, &ps );
break;
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
{
SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
}
}
}
default:
return DefWindowProc(hwnd, umsg, wParam, lParam);
}

return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
///////////////////////////////////
//Windows Initialization//////////
/////////////////////////////////
HWND hwnd;
HRESULT hr;

WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"Engine";
wc.hIconSm = LoadIcon(wc.hInstance, IDI_WINLOGO);

if(!RegisterClassEx(&wc))
{
return false;
}

hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"Engine", L"Engine",
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP | WS_OVERLAPPEDWINDOW,
(GetSystemMetrics(SM_CXSCREEN) - 1200)/2,(GetSystemMetrics(SM_CYSCREEN) - 720)/2, 1280, 720, NULL, NULL, hInstance, NULL);

if(!hwnd)
{
return false;
}

ShowWindow(hwnd, iCmdshow);

////////////////////////////////
//DirectX Init/////////////////
//////////////////////////////

DXGI_SWAP_CHAIN_DESC swapDesc;
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
ID3D11Texture2D* backBufferPtr;

swapDesc.BufferDesc.Width = 1200;
swapDesc.BufferDesc.Height = 720;
swapDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapDesc.BufferDesc.RefreshRate.Numerator = 60;
swapDesc.BufferDesc.RefreshRate.Denominator = 1;
swapDesc.BufferCount = 1;
swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapDesc.OutputWindow = hwnd;
swapDesc.SampleDesc.Count = 1;
swapDesc.SampleDesc.Quality = 0;
swapDesc.Windowed = TRUE;
swapDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapDesc.Flags = 0;




if(FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1,
D3D11_SDK_VERSION, &swapDesc, &g_swapChain, &g_device, NULL, &g_deviceContext)))
{
return false;
}

if(FAILED(g_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr)))
{
return false;
}

if(FAILED(g_device->CreateRenderTargetView(backBufferPtr, NULL, &g_renderTargetView)))
{
return false;
}

backBufferPtr->Release();
backBufferPtr = NULL;

D3D11_VIEWPORT viewPort;
viewPort.Width = (float)1200;
viewPort.Height = (float)720;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
g_deviceContext->RSSetViewports(1, &viewPort);
//////////////////////////////////
//Vertex ////////////////////
////////////////////////////////
ID3DBlob* pVSBlob = NULL;

if(FAILED(CompileShaderFromFile(L"VertexShader.vs", "VS", "vs_5_0", &pVSBlob, hwnd)))
{
return false;
}

hr = g_device->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &gVertexShader);
if(FAILED(hr))
return false;

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

UINT numElements = ARRAYSIZE(layout);

hr = g_device->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &gInputLayout);
if(FAILED(hr))
return false;

pVSBlob->Release();

g_deviceContext->IASetInputLayout(gInputLayout);

ID3DBlob* pPSBlob = NULL;

if(FAILED(CompileShaderFromFile(L"PixelShader.ps", "PS", "ps_5_0", &pPSBlob, hwnd)))
{
return false;
}

hr = g_device->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &gPixelShader);
if(FAILED(hr))
return false;


pPSBlob->Release();

SimpleVertex vertices[3];

vertices[0].Pos = XMFLOAT3(0.0f, 0.5f, 0.5f);

vertices[1].Pos = XMFLOAT3(0.5f, -0.5f, 0.5f);

vertices[2].Pos = XMFLOAT3(-0.5f, -0.5f, 0.5f);

D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 3;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
bd.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
hr = g_device->CreateBuffer(&bd, &InitData, &gVertexBuffer);
if(FAILED(hr))
return false;

UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_deviceContext->IASetVertexBuffers(0, 1, &gVertexBuffer, &stride, &offset);
g_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


/////////////////////////////////
//Message Loop//////////////////
///////////////////////////////

MSG msg;
bool done = false;
ZeroMemory(&msg, sizeof(MSG));

while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
{
done = true;
}
}
else
{
Render();
}
}

return 0;
}

void Render()
{
float Color[4] = {0.0f, 0.125f, 0.6f, 1.0f};
g_deviceContext->ClearRenderTargetView(g_renderTargetView, Color);

g_deviceContext->VSSetShader(gVertexShader, NULL, 0);
g_deviceContext->PSSetShader(gPixelShader, NULL, 0);
g_deviceContext->Draw(3, 0);

g_swapChain->Present(0, 0);

}

HRESULT CompileShaderFromFile(WCHAR* fileName, LPCSTR entryPoint, LPCSTR shaderModel, ID3DBlob** blobOut, HWND hwnd)
{
HRESULT hr = S_OK;

DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

ID3DBlob* pErrorBlob;
hr = D3DX11CompileFromFile(fileName, NULL, NULL, entryPoint, shaderModel, dwShaderFlags, 0, NULL, blobOut, &pErrorBlob, NULL);
if(FAILED(hr))
{
char* compileErrors;
unsigned long bufferSize, i;
std::ofstream fout;

compileErrors = (char*)pErrorBlob->GetBufferPointer();
bufferSize = pErrorBlob->GetBufferSize();
fout.open("Shader-Errors.txt");

for(i = 0;i < bufferSize; i++)
{
fout << compileErrors;
}

fout.close();


MessageBox(hwnd, L"Shader Error, check the .txt", L"Error", MB_OK);
return hr;
}
if(pErrorBlob)
{
pErrorBlob->Release();
}

return S_OK;
}

Share this post


Link to post
Share on other sites
Advertisement
If forgot this line:

g_deviceContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

So the draw calls will draw the triangle in the render target

You should set D3D11_CREATE_DEVICE_DEBUG flag in the DXGI_SWAP_CHAIN_DESC and see if any you get any error/warning. It will help you in the future :wink:

Finally, run your program in PIX, find the draw call line and try see what happens before and after it...

Share this post


Link to post
Share on other sites
A million thanks to you, sir. That was indeed the line I forgot :D
As for the PIX, I've never used it before. What kind of information should I gather?

Share this post


Link to post
Share on other sites
OK I just ran it in PIX and it crashed. Without PIX though it runs fine. Here is the log:


Frame 000001 ....PRE: Frame(1)
Trigger 'Frame 1' fired
Created PIXRun file at: C:\Users\KOPILA~1\AppData\Local\Temp\PIXF45B.tmp
Frame 000001 ........PRE: D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, 0x0036FC78, 1, 7, 0x0036FC84, 0x00065398, 0x00065388, NULL, 0x0006538C)
Frame 000001 ............PRE: AddObject(D3D11 Device, 0x04AB1CB0, 0x01F74650)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device, 0x04AB1CB0, 0x01F74650)
Frame 000001 ............PRE: AddObject(DXGI Factory, 0x04AB1DA0, 0x00848EA8)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Factory, 0x04AB1DA0, 0x00848EA8)
Frame 000001 ............PRE: AddObject(DXGI Adapter, 0x04AB1DF0, 0x008498E8)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Adapter, 0x04AB1DF0, 0x008498E8)
Frame 000001 ............PRE: AddObject(DXGI Device, 0x04AB1E48, 0x01F74428)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Device, 0x04AB1E48, 0x01F74428)
Frame 000001 ............PRE: AddObject(DXGI Swap Chain, 0x04AB1EA8, 0x0084A128)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Swap Chain, 0x04AB1EA8, 0x0084A128)
Frame 000001 ............PRE: AddObject(DXGI Surface, 0x04AB1F40, 0x01F6829C)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Surface, 0x04AB1F40, 0x01F6829C)
Frame 000001 ............PRE: AddObject(D3D11 Texture2D, 0x04AB1FA0, 0x01F682D4)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Texture2D, 0x04AB1FA0, 0x01F682D4)
Frame 000001 ............PRE: AddObject(D3D11 Device Context, 0x04B28A38, 0x01F7BCF8)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device Context, 0x04B28A38, 0x01F7BCF8)
Frame 000001 ........POST: <S_OK> D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, 0x0036FC78, 1, 7, 0x0036FC84, 0x00065398, 0x00065388, NULL, 0x0006538C)
Frame 000001 ........PRE: <this=0x04ab1ea8>IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x0036FC6C)
Frame 000001 ........POST: <S_OK><this=0x04ab1ea8> IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x0036FC6C)
Frame 000001 ........PRE: <this=0x04ab1cb0>ID3D11Device::CreateRenderTargetView(0x04AB1FA0, NULL, 0x00065390)
Frame 000001 ............PRE: AddObject(D3D11 Render Target View, 0x04AB2440, 0x01F9E680)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Render Target View, 0x04AB2440, 0x01F9E680)
Frame 000001 ........POST: <S_OK><this=0x04ab1cb0> ID3D11Device::CreateRenderTargetView(0x04AB1FA0, NULL, 0x00065390)
Frame 000001 ........PRE: <this=0x04b28a38>ID3D11DeviceContext::OMSetRenderTargets(1, 0x00065390, NULL)
Frame 000001 ........POST: <><this=0x04b28a38> ID3D11DeviceContext::OMSetRenderTargets(1, 0x00065390, NULL)
Frame 000001 ........PRE: <this=0x04ab1fa0>ID3D11Texture2D::Release()
Frame 000001 ........POST: <0><this=0x04ab1fa0> ID3D11Texture2D::Release()
Frame 000001 ........PRE: <this=0x04b28a38>ID3D11DeviceContext::RSSetViewports(1, 0x0036FC4C)
Frame 000001 ........POST: <><this=0x04b28a38> ID3D11DeviceContext::RSSetViewports(1, 0x0036FC4C)
An unhandled exception occurred.
Closing Run File

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!