Sign in to follow this  

Problems with creating layout

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

Hi everyone. I'm new here as you can see ;) I have a problem with a source code from a book I own. The window shows up just for a second and than closes itself. Here is the code:
#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>

#pragma comment (lib, "d3d10.lib")
#pragma comment (lib, "d3dx10.lib")

#define WINDOW_NAME "D3D Primitive Drawing"
#define WINDOW_HEIGHT 600
#define WINDOW_WIDTH 800

ID3D10Device *g_d3dDevice = NULL;
IDXGISwapChain *g_swapChain = NULL;
ID3D10RenderTargetView *g_renderTargetView = NULL;

ID3D10Effect *g_shader = NULL;
ID3D10EffectTechnique *g_technique = NULL;

ID3D10InputLayout *g_layout = NULL;
ID3D10Buffer *g_vertexBuffer = NULL;

struct DX10_Vertex
{
	D3DXVECTOR3 pos;
};

bool InitializeDemo()
{
	DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;

#if defined (DEBUG) || defined (_DEBUG)
	shaderFlags |= D3D10_SHADER_DEBUG;
#endif

	HRESULT hr = D3DX10CreateEffectFromFile("shader.fx", NULL, NULL, "fx_4_0", shaderFlags, 0, g_d3dDevice, NULL, NULL, &g_shader, NULL, NULL);

	if(FAILED(hr))
		return false;

	g_technique = g_shader->GetTechniqueByName("PassThorugh");

	D3D10_INPUT_ELEMENT_DESC layout[]=
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
	};

	unsigned int numElements = sizeof(layout) / sizeof(layout[0]);
	D3D10_PASS_DESC passDesc;

	g_technique->GetPassByIndex(0)->GetDesc(&passDesc);


	hr = g_d3dDevice->CreateInputLayout(layout, numElements, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &g_layout);

	if(FAILED(hr))
		return false;

	DX10_Vertex vertices[]=
	{
		{D3DXVECTOR3(0.5f, 0.5f, 0.5f) },
		{D3DXVECTOR3(0.5f, -0.5f, 0.5f) },
		{D3DXVECTOR3(-0.5f, -0.5f, 0.5f) },

		{D3DXVECTOR3(-0.5f, -0.5f, 0.5f) },
		{D3DXVECTOR3(-0.5f, 0.5f, 0.5f) },
		{D3DXVECTOR3(0.5f, 0.5f, 0.5f) },
	};

	D3D10_BUFFER_DESC buffDesc;

	buffDesc.Usage = D3D10_USAGE_DEFAULT;
	buffDesc.ByteWidth = sizeof(DX10_Vertex) * 6;
	buffDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	buffDesc.CPUAccessFlags = 0;
	buffDesc.MiscFlags = 0;

	D3D10_SUBRESOURCE_DATA resData;
	resData.pSysMem = vertices;

	hr = g_d3dDevice->CreateBuffer(&buffDesc, &resData, &g_vertexBuffer);

	if(FAILED(hr))
		return false;

	return true;
}

void RenderScene()
{
	float col[4]={0, 0, 0, 0};

	g_d3dDevice->ClearRenderTargetView(g_renderTargetView, col);

	ID3D10RasterizerState *rsState;
	D3D10_RASTERIZER_DESC rsStateDesc;

	rsStateDesc.FillMode = D3D10_FILL_WIREFRAME;
	rsStateDesc.CullMode = D3D10_CULL_NONE;
	rsStateDesc.FrontCounterClockwise = true;
	rsStateDesc.DepthBias = false;
	rsStateDesc.DepthBiasClamp = 0;
	rsStateDesc.SlopeScaledDepthBias = 0;
	rsStateDesc.DepthClipEnable = true;
	rsStateDesc.ScissorEnable = false;
	rsStateDesc.MultisampleEnable = false;
	rsStateDesc.AntialiasedLineEnable = false;

	g_d3dDevice->CreateRasterizerState(&rsStateDesc, &rsState);
	g_d3dDevice->RSSetState(rsState);

	unsigned int stride = sizeof(DX10_Vertex);
	unsigned int offset = 0;

	g_d3dDevice->IASetInputLayout(g_layout);
	g_d3dDevice->IASetVertexBuffers(0, 1, &g_vertexBuffer, &stride, &offset);
	g_d3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	D3D10_TECHNIQUE_DESC techDesc;
	g_technique->GetDesc(&techDesc);

	for(unsigned int i=0; i < techDesc.Passes; i++)
	{
		g_technique->GetPassByIndex(i)->Apply(0);
		g_d3dDevice->Draw(6, 0);
	}

	g_swapChain->Present(0, 0);
}

void Shutdown()
{

	if(g_d3dDevice) g_d3dDevice->ClearState();
	if(g_swapChain) g_swapChain->Release();
	if(g_renderTargetView) g_renderTargetView->Release();
	if(g_shader) g_shader->Release();
	if(g_layout) g_layout->Release();
	if(g_vertexBuffer) g_vertexBuffer->Release();
	if(g_d3dDevice) g_d3dDevice->Release();
}
void Update()
{

}

void ResizeD3D10Window(int width, int height)
{
	if(g_d3dDevice == NULL)
		return;

	D3D10_VIEWPORT vp;

	vp.Width = width;
	vp.Height = height;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;

	g_d3dDevice->RSSetViewports(1, &vp);
}

bool InitializeD3D10(HWND hwnd)
{
	DXGI_SWAP_CHAIN_DESC swapDesc;
	ZeroMemory(&swapDesc, sizeof(swapDesc));

	swapDesc.BufferCount = 2;
	swapDesc.BufferDesc.Width = WINDOW_WIDTH;
	swapDesc.BufferDesc.Height = WINDOW_HEIGHT;
	swapDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapDesc.OutputWindow = hwnd;
	swapDesc.SampleDesc.Count = 1;
	swapDesc.SampleDesc.Quality = 0;
	swapDesc.Windowed = TRUE;

	HRESULT hr = S_OK;
	unsigned int flags = 0;

#ifdef _DEBUG
	flags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

	D3D10_DRIVER_TYPE driverType = D3D10_DRIVER_TYPE_NULL;

	D3D10_DRIVER_TYPE driverTypes[]=
	{
		D3D10_DRIVER_TYPE_HARDWARE,
		D3D10_DRIVER_TYPE_REFERENCE,
	};
	
	unsigned int numDriverTypes = sizeof(driverTypes) / sizeof(driverTypes[0]);

	for(unsigned int i=0; i < numDriverTypes; i++)
	{
		driverType = driverTypes[i];

		hr = D3D10CreateDeviceAndSwapChain(NULL, driverType, NULL, flags, D3D10_SDK_VERSION, &swapDesc, &g_swapChain, &g_d3dDevice);

		if(SUCCEEDED(hr))
			break;
	}

	if(FAILED(hr))
		return false;

	ID3D10Texture2D *buffer = NULL;
	hr = g_swapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &buffer);

	if(FAILED(hr))
		return false;

	hr = g_d3dDevice->CreateRenderTargetView(buffer, NULL, &g_renderTargetView);

	buffer->Release();

	if(FAILED(hr))
		return false;

	g_d3dDevice->OMSetRenderTargets(1, &g_renderTargetView, NULL);

	ResizeD3D10Window(WINDOW_WIDTH, WINDOW_HEIGHT);

	return true;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT m, WPARAM wp, LPARAM lp)
{

	int width, height;

	switch(m)
	{
		case WM_CLOSE:
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
			break;

		case WM_SIZE:
			height = HIWORD(lp);
			width = LOWORD(lp);
			if(height == 0)
				height = 1;

			ResizeD3D10Window(width, height);
			return 0;
			break;

		case WM_KEYDOWN:
			switch(wp)
			{ 
				case VK_ESCAPE:
					PostQuitMessage(0);
					break;

				default:
					break;
			}
			break;

		default:
			break;
	}

	return (DefWindowProc(hwnd, m, wp, lp));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmd, int show)
{
	MSG msg;

	WNDCLASSEX windowClass;
	memset(&windowClass, 0, sizeof(WNDCLASSEX));
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	windowClass.lpfnWndProc = WndProc;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.lpszClassName = "DX10CLASS";
	windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&windowClass))
		return 0;

	HWND hwnd = CreateWindowEx(NULL, "DX10CLASS", WINDOW_NAME, WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, hInstance, NULL);

	if(!hwnd)
		return 0;

	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);

	if(InitializeD3D10(hwnd) == true)
	{
		if(InitializeDemo() == true)
		{
			while(1)
			{
				if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
				{
					if(msg.message == WM_QUIT) break;
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
				else
				{
					Update();
					RenderScene();
				}
			}
		}
	}
	
	Shutdown();
	UnregisterClass("DX10CLASS", windowClass.hInstance);

	return 0;
}

I'm not sure where is the mistake but I think it may be in this line:
hr = g_d3dDevice->CreateInputLayout(layout, numElements, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &g_layout);

Debugger also say so. What should I change? I would be grateful if someone can help me.

Share this post


Link to post
Share on other sites
Output console says that the third parameter in CreateInputLayout is corrupt:
D3D10: CORRUPTION: ID3D10Device::CreateInputLayout: Third parameter is corrupt. [ MISCELLANEOUS CORRUPTION #15: CORRUPTED_PARAMETER3 ]




Could someone check what's wrong?

EDIT: I checked one more thing. The HRESULT of this line:
g_technique->GetPassByIndex(0)->GetDesc(&passDesc);


is E_FAIL.

Share this post


Link to post
Share on other sites
Shader.fx content:

struct VS_INPUT
{
float4 Pos : POSITION;
};


struct PS_INPUT
{
float4 Pos : SV_POSITION;
};


PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;

output.Pos = input.Pos;

return output;
}


float4 PS(PS_INPUT input) : SV_Target
{
return float4(1, 0, 1, 1);
}


technique10 PassThrough
{
pass P0
{
SetVertexShader(CompileShader(vs_4_0, VS()));

SetGeometryShader(NULL);

SetPixelShader(CompileShader(ps_4_0, PS()));
}
}

Share this post


Link to post
Share on other sites

This topic is 2836 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.

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