Jump to content
  • Advertisement
Sign in to follow this  
RoundPotato

Direct3D11 dev->CreateVertexShader() crashes the program.

This topic is 1866 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 there,

 

I've pinned the crash down to its position and it turned out to be:

dev->CreateVertexShader(vBlob->GetBufferPointer(),vBlob->GetBufferSize(),NULL,&vs);

 

It is inside "void initPipeline()"...

 

From:

#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dcompiler.h>

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

#define SCREEN_WIDTH 1366
#define SCREEN_HEIGHT 786

// COM pointers
ID3D11Device *dev;
ID3D11DeviceContext *devCon;
IDXGISwapChain *sc;
ID3D11RenderTargetView *backBuffer;
ID3D11VertexShader *vs;
ID3D11PixelShader *ps;
ID3D11Buffer *vBuffer;
ID3D11InputLayout *il;

// VERTEX struct
struct VERTEX
{
	FLOAT x,y,z;
	FLOAT color[4];
};

void renderFrame()
{
	float color[4] = {0.0,0.2,0.4,1.0};
	devCon->ClearRenderTargetView(backBuffer,color);
	/*
	// set Vertex buffers
	UINT size = sizeof(VERTEX);
	UINT offset = 0;
	devCon->IASetVertexBuffers(0,1,&vBuffer,&size,&offset);
	
	// set primitives rendering style
	devCon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	// draw vertex onto back buffer
	devCon->Draw(3,0);
	*/
	// swap buffers
	sc->Present(0,0);
}

void initPipeline()
{
	// load Shaders
	ID3D10Blob *vBlob,*pBlob;
	D3DCompileFromFile(L"Shaders.hlsl",NULL,NULL,"PShader","ps_5_0",NULL,NULL,&pBlob,NULL);
	D3DCompileFromFile(L"Shaders.hlsl",NULL,NULL,"VShader","vs_5_0",NULL,NULL,&vBlob,NULL);
	
	// create Shader objects
	//dev->CreatePixelShader(pBlob->GetBufferPointer(),pBlob->GetBufferSize(),NULL,&ps);
	//dev->CreatePixelShader(pBlob->GetBufferPointer(),pBlob->GetBufferSize(),NULL,&ps);
	dev->CreateVertexShader(vBlob->GetBufferPointer(),vBlob->GetBufferSize(),NULL,&vs);
	/*
	// set Shaders
	devCon->VSSetShader(vs,NULL,NULL);
	devCon->PSSetShader(ps,NULL,NULL);
	*/
	/*
	// Describe Input Layout
	D3D11_INPUT_ELEMENT_DESC ied[] = 
	{
		{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
		{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0}
	};

	// Create Input Layout
	dev->CreateInputLayout(ied,2,vBlob->GetBufferPointer(),vBlob->GetBufferSize(),&il);

	// set Input Layout
	devCon->IASetInputLayout(il);
	*/
}

void initGraphics()
{
	// create a triangle using the VERTEX struct
	VERTEX verts[] =
	{
		{0.0f, 0.5f, 0.0f, (1.0f, 0.0f, 0.0f, 1.0f)},
		{0.45f, -0.5, 0.0f, (0.0f, 1.0f, 0.0f, 1.0f)},
		{-0.45f, -0.5f, 0.0f, (0.0f, 0.0f, 1.0f, 1.0f)}
	};

	// create buffer
	D3D11_BUFFER_DESC bd;
	ZeroMemory(&bd,sizeof(D3D11_BUFFER_DESC));

	bd.Usage = D3D11_USAGE_DYNAMIC;
	bd.ByteWidth = sizeof(VERTEX)*3;
	bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;

	dev->CreateBuffer(&bd,NULL,&vBuffer);

	D3D11_MAPPED_SUBRESOURCE msr;
	devCon->Map(vBuffer,NULL,D3D11_MAP_WRITE_DISCARD,NULL,&msr);
	memcpy(msr.pData,verts,sizeof(verts));
	devCon->Unmap(vBuffer,NULL);
}

void initD3D(HWND hWnd)
{
	DXGI_SWAP_CHAIN_DESC scd;
	ZeroMemory(&scd,sizeof(DXGI_SWAP_CHAIN_DESC));

	scd.BufferCount = 1;
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	scd.BufferDesc.Width = SCREEN_WIDTH;
	scd.BufferDesc.Height = SCREEN_HEIGHT;
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow = hWnd;
	scd.SampleDesc.Count = 4;
	scd.Windowed = TRUE;
	scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

	D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,NULL,NULL,NULL,D3D11_SDK_VERSION,&scd,&sc,&dev,NULL,&devCon);

	ID3D11Texture2D *t2D;
	sc->GetBuffer(0,__uuidof(ID3D11Texture2D),(LPVOID*)&t2D);
	dev->CreateRenderTargetView(t2D,NULL,&backBuffer);
	t2D->Release();
	devCon->OMSetRenderTargets(1,&backBuffer,NULL);

	/* crappy viewport
	D3D11_VIEWPORT vp;
	ZeroMemory(&vp,sizeof(D3D11_VIEWPORT));

	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	vp.Width = SCREEN_WIDTH;
	vp.Height = SCREEN_HEIGHT;
	devCon->RSSetViewports(1,&vp);
	*/
}

void cleanD3D()
{
	// turn off FullScreen
	sc->SetFullscreenState(FALSE,NULL);

	// release COM objects' resources
	il->Release();
	vBuffer->Release();
	vs->Release();
	ps->Release();
	dev->Release();
	devCon->Release();
	backBuffer->Release();
	sc->Release();
}

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

	// return default-managed values
	return DefWindowProc(hWnd,msg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lCmdLIne,int nCmdShow)
{
	WNDCLASSEX wc;
	ZeroMemory(&wc,sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	//wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.lpszClassName = L"WindowClass1";

	RegisterClassEx(&wc);

	RECT wRect = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
	AdjustWindowRect(&wRect,WS_OVERLAPPEDWINDOW,FALSE);
	HWND hWnd = CreateWindowEx(NULL,L"WindowClass1",L"MY MEGA WINDOW",WS_OVERLAPPEDWINDOW,0,0,wRect.right - wRect.left,wRect.bottom - wRect.top,NULL,NULL,hInstance,NULL);
	ShowWindow(hWnd,SW_SHOWMAXIMIZED);

	initD3D(hWnd);
	initPipeline();
	//initGraphics();

	MSG msg = {0};

	while(true)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			if(msg.message == WM_QUIT) break;
		}
		else
		{
			renderFrame();
			// 50 FPS  (1000/20)
			Sleep(20);
		}
	}

	cleanD3D();

	return msg.wParam;
}

 

 

Both Pixel and Vertex Shader creation crashed the program, I don't even know what to think. Anyone has any ideas ? sad.png

Share this post


Link to post
Share on other sites
Advertisement
First read here!

And yeah, like I said earlier, directxtutorial.com lacks error checking. Check the HRESULT from the compile call with the check macros (e.g. SUCCEEDED).

Also: Make sure the shader file is where your app is looking for. The working directory of Visual Studio is usually not where your exe is.

Share this post


Link to post
Share on other sites

[attachment=15502:Desktop.zip][attachment=15502:Desktop.zip]I don't know what do you mean by Shaders compiling, but the statements where there are "compiled into blobs" is fine, it is the very creation I mentioned.

 

Yes the shader file is in the projects and I have copied over the necessary x86 .dll to where the .exe is (will probably ask how to get rid of it later).

 

I don't know how to check for those error codes you mentioned, I don't think it will be useful if it just returns true or false since it crashes on that statement anyway.

 

Please see attached videos of the crash and debug.

Share this post


Link to post
Share on other sites

Those videos show nothing we could use. it would crash only if shader didn't compile successfully (assuming your code is like the one you posted - without error checking).

Share this post


Link to post
Share on other sites

Try 

ID3D10Blob *VS, *PS;
D3DCompileFromFile(L"shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, &VS, 0);
D3DCompileFromFile(L"shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, &PS, 0);

Instead of your 

ID3D10Blob *vBlob,*pBlob;
D3DCompileFromFile(L"Shaders.hlsl",NULL,NULL,"PShader","ps_5_0",NULL,NULL,&pBlob,NULL);
D3DCompileFromFile(L"Shaders.hlsl",NULL,NULL,"VShader","vs_5_0",NULL,NULL,&vBlob,NULL);

See if that works.

?Actually nevermind that just retried your code and it worked fine for me, although it didn't first time around and as of yet i can't see why.

Edited by RyanMm

Share this post


Link to post
Share on other sites

Ugh... I read about the "macros" and put that shader compilation call into:

	if( SUCCEEDED( dev->CreateVertexShader(vBlob->GetBufferPointer(),vBlob->GetBufferSize(),NULL,&vs) ) )
		MessageBox(NULL,L"Vertex Shader Compiled!",L"Shader Compilation Result",MB_OK | MB_ICONEXCLAMATION);
	else
		MessageBox(NULL,L"Vertex SHader FAILED!",L"Shader Compilation Result",MB_OK | MB_ICONEXCLAMATION);

 

Still crashes the same way, which I assumed the checking is useless since the call itself crashes for no reason...

Any more ideas ?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!