Jump to content
  • Advertisement
Sign in to follow this  
Kyle Corver

DX11 Directx11: Draw Primitive and compile Release

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

Hey guys,

got two 'little' Problems with my current Project:

The first is: When I start my Project (simple Win32 Application) wihin Visual Studio in Release/Debug Mode it is showing me the normal Window I'd like to have. When I compile the Release Version and start the .exe in the Release Folder, the Window is closing immediately. I just have no Idea why that happens and I have no idea how I can create any output. But I identified the not-working function. It is CompileShader(...) in class Game.

 

The second thing is, that I'm trying to show a simple primitive like in the book i'm currently reading.Initialisation of DirectX is working proper without any exceptions, but the triangle is simply not showing :(

 

The Code is seperated in class Game and class MyGame, which inherits from Game and overwrites the Methods of Game.

Game:
void Game::Initialize(HWND _hwnd)
{
	hwnd = _hwnd;

	RECT dim;
	GetClientRect(hwnd, &dim);

	width = dim.right-dim.left;
	height = dim.bottom-dim.top;

	D3D_DRIVER_TYPE driverTypes[] = 
	{
		D3D_DRIVER_TYPE_HARDWARE,
		D3D_DRIVER_TYPE_WARP,
		D3D_DRIVER_TYPE_SOFTWARE
	};
	int numDriverTypes = ARRAYSIZE(driverTypes);

	D3D_FEATURE_LEVEL featureLevels [] = 
	{
		D3D_FEATURE_LEVEL_11_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_10_0,
		D3D_FEATURE_LEVEL_9_3,
		D3D_FEATURE_LEVEL_9_2,
		D3D_FEATURE_LEVEL_9_1
	};
	int numFeatureLevels = ARRAYSIZE(featureLevels);

	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = width;
	swapChainDesc.BufferDesc.Height = height;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow = hwnd;
	swapChainDesc.Windowed = true;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	HRESULT hr;
	D3D_FEATURE_LEVEL featureLevel;
	for(int i=0; i<numDriverTypes; i++)
	{
		hr = D3D11CreateDeviceAndSwapChain(0, driverTypes[i], 0, 0, featureLevels, numFeatureLevels, D3D11_SDK_VERSION,
			&swapChainDesc, &swapchain, &device, &featureLevel, &context);
		if(SUCCEEDED(hr))	break;
	}

	if(FAILED(hr))	throw BasicException(typeid(this).name(), "Could not create device and swapchain", hr);

	ID3D11Texture2D* backBufferTexture;
	hr = swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture);

	if(FAILED(hr))	throw BasicException(typeid(this).name(), "failed to get swapchain backbuffer", hr);

	hr = device->CreateRenderTargetView(backBufferTexture, 0, &backBufferTarget);

	if(backBufferTexture)	backBufferTexture->Release();

	if(FAILED(hr))	throw BasicException(typeid(this).name(), "failed to create render target view", hr);

	context->OMSetRenderTargets(1, &backBufferTarget, 0);

	viewport.Width		= static_cast<float>(width);
	viewport.Height		= static_cast<float>(height);
	viewport.MinDepth	= 0.0f;
	viewport.MaxDepth	= 1.0f;
	viewport.TopLeftX	= 0.0f;
	viewport.TopLeftY	= 0.0f;

	context->RSSetViewports(1, &viewport);

}
void Game::CompileShader(char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
	DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

	ID3DBlob* errorBuffer = 0;
	HRESULT hr;
	
	hr = D3DX11CompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, 0, buffer, &errorBuffer, 0);

	if(FAILED(hr))
	{
		if(errorBuffer!=0)				throw BasicException(typeid(this).name(), (char*)errorBuffer->GetBufferPointer(), hr);
		throw BasicException(typeid(this).name(), "failed loading shader", hr);
	}

	if(errorBuffer!=0)	errorBuffer->Release();
}
void Game::LoadContent() {}
void Game::Draw() {}
void Game::Begin()
{
	float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
	context->ClearRenderTargetView(backBufferTarget, color);
}
void Game::End()
{
	swapchain->Present(0, 0);
}
MyGame:
struct VertexPos
{
	XMFLOAT3 pos;
};
void MyGame::Initialize(HWND _hwnd)
{
	Game::Initialize(_hwnd);

	ID3DBlob* vsBuffer = 0;
	ID3DBlob* psBuffer = 0;
	HRESULT hr;

	try
	{
		CompileShader("VertexShader.hlsl", "main", "vs_4_0", &vsBuffer);  //here is the error, when i start the .exe outside of Visual Studio
	}
	catch(BasicException be)
	{
		throw be;
	}

	hr = device->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), 0, &vshader);

	if(FAILED(hr))
	{
		throw BasicException(typeid(this).name(), "failed creating vertexshader", hr);
		vsBuffer->Release();
	}

	D3D11_INPUT_ELEMENT_DESC colorLayout[] = 
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}
	};

	int numLayouts = ARRAYSIZE(colorLayout);

	hr = device->CreateInputLayout(colorLayout, numLayouts, vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &ilayout);

	vsBuffer->Release();

	if(FAILED(hr))					throw BasicException(typeid(this).name(), "failed creating input layout", hr);


	CompileShader("PixelShader.hlsl", "main", "ps_4_0", &psBuffer);

	hr = device->CreatePixelShader(psBuffer->GetBufferPointer(), psBuffer->GetBufferSize(), 0, &pshader);

	psBuffer->Release();

	if(FAILED(hr))					throw BasicException(typeid(this).name(), "failed creating pixelshader", hr);


}
void MyGame::LoadContent()
{
	Game::LoadContent();
	HRESULT hr;
	VertexPos vertices[] =
	{
		XMFLOAT3(0.5f, 0.5f, 0.5f),
		XMFLOAT3(0.5f, -0.5f, 0.5f),
		XMFLOAT3(-0.5f, -0.5f, 0.5f)
	};

	D3D11_BUFFER_DESC vertexDesc;
	ZeroMemory(&vertexDesc, sizeof(vertexDesc));
	vertexDesc.Usage		= D3D11_USAGE_DEFAULT;
	vertexDesc.BindFlags	= D3D11_BIND_VERTEX_BUFFER;
	vertexDesc.ByteWidth	= sizeof(VertexPos)*3;

	D3D11_SUBRESOURCE_DATA rcData;
	ZeroMemory(&rcData, sizeof(rcData));
	rcData.pSysMem = vertices;

	hr = device->CreateBuffer(&vertexDesc, &rcData, &vBuffer);

	if(FAILED(hr))				throw BasicException(typeid(this).name(), "failed creating vertexbuffer", hr);
}
void MyGame::Draw()
{
	Game::Draw();
	unsigned int stride = sizeof(VertexPos);
	unsigned int offset = 0;

	context->IASetInputLayout(ilayout);
	context->IASetVertexBuffers(0, 1, &vBuffer, &stride, &offset);
	context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	context->VSSetShader(vshader, 0, 0);
	context->PSSetShader(pshader, 0, 0);
	context->Draw(3, 0);
}
//are those shader codes correct?
VertexShader.hlsl
float4 main( float4 pos : POSITION ) : SV_POSITION
{
	return pos;
}



PixelShader.hlsl:
float4 main() : SV_TARGET
{
	return float4(0.0f, 1.0f, 1.0f, 1.0f);
}

I hope that Code is enough. Without drawing anything and just clearing the Backbuffer and presenting (Game::Begin(), Game::End()) I can see the whole window in color of the clearcolor, so I guess this is alright.

 

I hope I've got everything and anyone knows what to do.

 

Thanks 4 Help!

 

Kyle

Share this post


Link to post
Share on other sites
Advertisement

First things first, do you have the error messaging turned on in the DirectX Control Panel? If not go to it, (it should be in the bin folder of the SKD), and see if it has any issues in the output. Next thing is check for any uninitialized memory being used. That's where almost 80% of all release bugs come from. Last but not least, you should cout some text in your application and see how far in the app you get.

Share this post


Link to post
Share on other sites

If it only happens when you run the executable without the debugger, my guess is a file path issue. For example, I've got a project with the debugger's working directory set to ${ProjectDir}, the project directory. Meanwhile, the executable is sitting in the bin folder. In fact... wait while I dig into the folder.. yeah, my program crashes too. Try placing your executable and data into a folder together with the exe at the root of your working directory (otherwise known as deploying your game), and then running it.

 

For your triangle, why did you make a shader for 4-component positions when your array appears to only have 3 components? Are you setting w to 1? That's the best guess I have right now.

Edited by Pink Horror

Share this post


Link to post
Share on other sites


If it only happens when you run the executable without the debugger, my guess is a file path issue.

 

Your guess was right. I forgot to put the shader files into the release folder. That's why it always stopped working ... rolleyes.gif

 


For your triangle, why did you make a shader for 4-component positions when your array appears to only have 3 components? Are you setting w to 1? That's the best guess I have right now.

Yeah I just wondered. I copied nearly all the code exactly out of my tutorial book, so it should have worked. My code wasn't working but the exact program did work. Finally I found the error: I used the wrong format for the colorlayout. I fixed it and now my triangle is showing properly biggrin.png

 

So all my problems seem to have disappeared. Thanks ph34r.png

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!