Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


bluepig.man

Member Since 16 Oct 2012
Offline Last Active Today, 09:35 AM

Topics I've Started

about keyword and include

01 October 2013 - 08:58 AM

Hey,everyone. Today, l try write some code but without include any header file, but this code run well. In my opinion, if we want use something in c++ we need to include it's header file into our program. But why these keyword no need to include any header file ?

If they aren't in header file, where they are? l tried to google ,but just can't find the answer. so please help. Thanks.

this the code:

class a
{
public:
	int d;
	int b;
private:
};

struct B
{
	double c;
};

int A( const int &a, const double &b )
{
	int justTest = 5;
	int aa = a;
	int bb = b;
	return bb;
}

union MyUnion
{
	int a;
	int b;
	struct c
	{
		double ad;
	};
};

int main()
{
	a in;
	in.d = 0;
	in.d -= 5;
	in.b = 6;
	in.d -= in.b;

	return 0;
}


D3D11 Shader Compile error x3116 :flags parameter is invalid.

09 July 2013 - 07:58 PM

Hi,l'm learning Direct 11 by <Introduction to 3D Game Programming with Direct 11>. 

An trouble error break out.

The Info is : error x3116:Flags parameter is invalid.This info break out when calling D3DX11CompileFromFile.

This happend may because the D3DX11CompileFromFile function's  wrong caling.l try to change the fx file and

 the code file,but the error still happend.

The file is here:

#include <windows.h>
#include <xnamath.h>
#include <cassert>
#include <d3dx11.h>
#include <dxerr.h>
#include "d3dx11Effect.h"

//---- Global Variable  ------------------------------------------------------------------------------------
	
	//Windows
	HINSTANCE				g_hinst				 = 0;
	HWND					g_hWnd				 = 0;
	LONG					g_ClientWidth		 = 800;
	LONG					g_ClientHeight		 = 600;

	//D3D
	ID3D11Device		   *g_D3dDevice			 = NULL;
	ID3D11DeviceContext	   *g_D3dDeviceContext   = NULL;
	IDXGISwapChain		   *g_SwapChain			 = NULL;
	ID3D11DepthStencilView *g_DepthStencilView   = NULL;
	ID3D11RenderTargetView *g_RenderTargetView   = NULL;
	ID3D11Texture2D        *g_DepthStencilBuffer = NULL;
	D3D11_VIEWPORT		   g_Vp;

	//Space
	XMMATRIX g_WorldViewProj;

	//Effect
	ID3DX11EffectTechnique *g_EffectTechnique	 = 0;
	ID3DX11Effect		   *g_Effect			 = 0;
	ID3D11InputLayout	   *g_InputLayout		 = 0;
	ID3D11Buffer		   *g_VertexBuffer		 = 0;
	ID3D11Buffer		   *g_IndexBuffer		 = 0;
	
	struct Vertex
	{
		XMFLOAT3 pos;
		XMFLOAT4 color;
	};


//---- Macro Define  ------------------------------------------------------------------------------------
	//HR define
	#if defined(DEBUG) | defined(_DEBUG)
	#ifndef HR
	#define HR(x)                                              \
	{                                                          \
		HRESULT hr = (x);                                      \
		if(FAILED(hr))                                         \
		{                                                      \
			DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true); \
		}                                                      \
	}
	#endif

	#else
	#ifndef HR
	#define HR(x) (x)
	#endif
	#endif

	#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
	#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
	#define ReleaseCOM(x)    { if(x){ x->Release(); x = 0;} }

//----Forward Declaration------------------------------------------------------------------------------------

	LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
	void onResizeAndInit();
	void BuildInputLayout();
	void BuildBuffer();
	void BuildFx();
	void DrawScreen();
	void ReleaseAll();
	bool Init_Window();
	bool Init_DX();
	bool Init();

//----Forward Implementation---------------------------------------------------------------------------------

	int WINAPI WinMain( _In_ HINSTANCE hInstance, 
						_In_opt_ HINSTANCE hPrevInstance, 
						_In_ LPSTR lpCmdLine, 
			 			_In_ int nShowCmd )
	{
		MSG msg = {0};
		g_hinst = hInstance;

		if( !Init() )
		{
			return FALSE;
		}

		while(msg.message != WM_QUIT)
		{
			// If there are Window messages then process them.
			if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			// Otherwise, do animation/game stuff.	
			else
			{
				if( KEYDOWN(VK_ESCAPE) )
				{
					SendMessage(g_hWnd,WM_CLOSE,0,0);
					break;
				}
				DrawScreen();
			}
		}

		return (msg.wParam);
	}

	bool Init_Window( )
	{
		WNDCLASS wc;
		wc.style = CS_HREDRAW | CS_VREDRAW;
		wc.lpfnWndProc = WndProc;
		wc.cbClsExtra = 0;
		wc.cbWndExtra = 0;
		wc.hInstance = g_hinst;
		wc.hIcon = LoadIcon(0, IDI_APPLICATION);
		wc.hCursor = LoadCursor(0,IDC_ARROW);
		wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
		wc.lpszMenuName = 0;
		wc.lpszClassName = L"D3DWndClassName";

		if( !RegisterClass(&wc) )
		{
			MessageBox( 0, L"ResiterClass Failed.", 0, 0 );
			return false;
		}

		RECT R = { 0, 0, g_ClientWidth, g_ClientHeight };
		AdjustWindowRect( &R, WS_OVERLAPPEDWINDOW, false );

		int width = R.right - R.left;
		int height = R.bottom - R.top;

		g_hWnd = CreateWindow( L"D3DWndClassName", L"createMyWindow", WS_OVERLAPPEDWINDOW, 
								  CW_USEDEFAULT, CW_USEDEFAULT, width, height, 
								  0, 0, g_hinst, 0);

		if( !g_hWnd )
		{
			MessageBox( 0, L"CreateWindow Failed! beijule", 0, 0 );
			return false;
		}

		ShowWindow( g_hWnd, SW_SHOW );
		UpdateWindow( g_hWnd );

		return true;
	}

	bool Init()
	{
		if( !Init_Window() )
		{
			return false;
		}
		if( !Init_DX() )
		{
			return false;
		}

		memset(&g_Vp, 0, sizeof(D3D11_VIEWPORT));
		g_WorldViewProj = XMMatrixIdentity();

		BuildFx();
		BuildBuffer();
		BuildInputLayout();
		return true;
	}

	bool Init_DX()
	{
		HRESULT hr;

		D3D_FEATURE_LEVEL FeatureLevelUsed;		
		HR( D3D11CreateDevice( 0, D3D_DRIVER_TYPE_HARDWARE, 0,
								D3D11_CREATE_DEVICE_DEBUG, 0,
								0, D3D11_SDK_VERSION,
								&g_D3dDevice, &FeatureLevelUsed ,&g_D3dDeviceContext));

		if( FeatureLevelUsed != D3D_FEATURE_LEVEL_11_0 )
		{
			MessageBox( NULL, L"Unsupport d3d11", NULL, NULL );
			return false;
		}

		//Now,prepare for create swapchain
		DXGI_SWAP_CHAIN_DESC sd;
		sd.BufferCount = 1;
		sd.BufferDesc.Height = g_ClientHeight;
		sd.BufferDesc.Width  = g_ClientWidth;
		sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		sd.BufferDesc.RefreshRate.Numerator = 60;
		sd.BufferDesc.RefreshRate.Denominator = 1;
		sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
		sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
		sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		sd.OutputWindow = g_hWnd;
		sd.Windowed = true;
		sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
		sd.SampleDesc.Count = 1;
		sd.SampleDesc.Quality = 0;
		sd.Flags = 0;

		IDXGIDevice *dxgiDevice = 0;
		hr = g_D3dDevice->QueryInterface( __uuidof( IDXGIDevice ), (void **)&dxgiDevice );
		if( FAILED(hr) )
		{
			MessageBox( NULL, L"Retrive DXGIDEVICE failed  ", NULL, NULL );
			return false;
		}

		IDXGIAdapter *dxgiAdapter = 0;
		hr = dxgiDevice->GetParent( __uuidof( IDXGIAdapter ), (void **)&dxgiAdapter );
		if( FAILED(hr) )
		{
			MessageBox( NULL, L"Retrive DXGIADAPTER failed  ", NULL, NULL );
			return false;
		}

		IDXGIFactory *dxgiFactory = 0;
		hr = dxgiAdapter->GetParent( __uuidof( IDXGIFactory ), (void **)&dxgiFactory );
		if( FAILED(hr) )
		{
			MessageBox( NULL, L"Retrive DXGIFACTORY failed  ", NULL, NULL );
			return false;
		}

		//Now it's the time to create swapchain
		hr = dxgiFactory->CreateSwapChain( g_D3dDevice, &sd, &g_SwapChain );
		dxgiFactory->MakeWindowAssociation( g_hWnd, DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_PRINT_SCREEN);
		if( FAILED(hr) )
		{
			MessageBox( NULL, L"Create SwapChain failed  ", NULL, NULL );
			return false;
		}

		//After Create SwapChain,we should release our acquired COM interfaces
		ReleaseCOM( dxgiDevice );
		ReleaseCOM( dxgiAdapter );
		ReleaseCOM( dxgiFactory );

		onResizeAndInit();
	}

	void onResizeAndInit()
	{
		assert( g_D3dDeviceContext );
		assert( g_D3dDevice ); 
		assert( g_SwapChain );

		ReleaseCOM(g_RenderTargetView);
		ReleaseCOM(g_DepthStencilView);
		ReleaseCOM(g_DepthStencilBuffer);

		HR( g_SwapChain->ResizeBuffers( 1, g_ClientWidth, g_ClientHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0) );
		ID3D11Texture2D *backBuffer = 0;
		HR( g_SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void **>(&backBuffer) )) ;
		HR( g_D3dDevice->CreateRenderTargetView( backBuffer, 0, &g_RenderTargetView ) );
		ReleaseCOM(backBuffer);

		D3D11_TEXTURE2D_DESC DepthStencilDesc;
		DepthStencilDesc.Width				= g_ClientWidth;
		DepthStencilDesc.Height				= g_ClientHeight;
		DepthStencilDesc.Format				= DXGI_FORMAT_D24_UNORM_S8_UINT;
		DepthStencilDesc.MipLevels			= 1;
		DepthStencilDesc.ArraySize			= 1;
		DepthStencilDesc.Usage				= D3D11_USAGE_DEFAULT;
		DepthStencilDesc.BindFlags			= D3D11_BIND_DEPTH_STENCIL;
		DepthStencilDesc.CPUAccessFlags		= 0;
		DepthStencilDesc.MiscFlags		    = 0;
		DepthStencilDesc.SampleDesc.Count   = 1;
		DepthStencilDesc.SampleDesc.Quality = 0;

		HR( g_D3dDevice->CreateTexture2D( &DepthStencilDesc, 0, &g_DepthStencilBuffer) );
		HR( g_D3dDevice->CreateDepthStencilView( g_DepthStencilBuffer, 0, &g_DepthStencilView ) );
		g_D3dDeviceContext->OMSetRenderTargets( 1, &g_RenderTargetView, g_DepthStencilView );

		g_Vp.TopLeftX = 0;
		g_Vp.TopLeftY = 0;
		g_Vp.Width	= static_cast<float>(g_ClientWidth);
		g_Vp.Height	= static_cast<float>(g_ClientHeight);
		g_Vp.MinDepth = 0;
		g_Vp.MaxDepth = 1.0f;

		g_D3dDeviceContext->RSSetViewports( 1, &g_Vp );
	}

	void ReleaseAll()
	{
		ReleaseCOM(g_RenderTargetView);
		ReleaseCOM(g_DepthStencilView);
		ReleaseCOM(g_InputLayout);
		ReleaseCOM(g_SwapChain);

		if( g_D3dDeviceContext )
		{
			g_D3dDeviceContext->ClearState();
		}
		ReleaseCOM( g_VertexBuffer );
		ReleaseCOM( g_IndexBuffer );
		ReleaseCOM( g_InputLayout );
		ReleaseCOM( g_Effect );
		ReleaseCOM(g_D3dDevice);
		ReleaseCOM(g_D3dDeviceContext);
	}

	void DrawScreen()
	{
		assert( g_D3dDeviceContext );
		assert( g_SwapChain );

		ID3DX11EffectMatrixVariable *fxWorldViewProj;
		fxWorldViewProj = g_Effect->GetVariableByName("gWorldViewProj")->AsMatrix();
		fxWorldViewProj->SetMatrix( (float *)&g_WorldViewProj );

		D3DX11_TECHNIQUE_DESC techDesc;
		g_EffectTechnique->GetDesc( &techDesc );
		g_EffectTechnique->GetPassByIndex(0)->Apply( 0, g_D3dDeviceContext );

		UINT stride = sizeof(Vertex);
		g_D3dDeviceContext->IASetInputLayout( g_InputLayout );
		g_D3dDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
		g_D3dDeviceContext->IASetVertexBuffers( 0, 1, &g_VertexBuffer, &stride,  0 );
		g_D3dDeviceContext->IASetIndexBuffer( g_IndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		XMVECTORF32 color = { 0.0f, 0.0f, 1.0f, 1.0f };
		g_D3dDeviceContext->ClearRenderTargetView( g_RenderTargetView, reinterpret_cast<const float*>(&color) );
		g_D3dDeviceContext->ClearDepthStencilView( g_DepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0.0f );
		g_D3dDeviceContext->DrawIndexed( 36, 0, 0 );

		HR(g_SwapChain->Present(0,0));
	}
	
	void BuildFx()
	{
		DWORD shaderFlag;
		#if  defined(DEBUG) || defined(_DEBUG)
		shaderFlag |= D3D10_SHADER_DEBUG;
		shaderFlag |= D3D10_SHADER_SKIP_OPTIMIZATION;
		#endif

		ID3D10Blob *CompiledShader;
		ID3D10Blob *CompiledError;

		 HR ( D3DX11CompileFromFile( L"FX/boxShader.fx", 0, 0, 0, "fx_5_0",
			shaderFlag, 0, 0, &CompiledShader, &CompiledError, 0 ) );

		if( CompiledError )
		{
			MessageBoxA( 0, (char *)CompiledError->GetBufferPointer(), 0, 0 );
			ReleaseCOM( CompiledError );
		}

		HR( D3DX11CreateEffectFromMemory( CompiledShader->GetBufferPointer(), CompiledShader->GetBufferSize(),
			0, g_D3dDevice, &g_Effect ) );

		ReleaseCOM(CompiledShader);		
	}

	void BuildBuffer()
	{
		Vertex BoxVertex[] = {
			{ XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT4(1.0f, 0.0f, 0.0f, 0.0f) },
			{ XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT4(0.0f, 1.0f, 0.0f, 0.0f) },
			{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT4(0.0f, 0.0f, 1.0f, 0.0f) },
			{ XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT4(1.0f, 1.0f, 0.0f, 0.0f) },
			{ XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT4(1.0f, 0.0f, 1.0f, 0.0f) },
			{ XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT4(0.0f, 1.0f, 1.0f, 0.0f) },
			{ XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT4(1.0f, 1.0f, 1.0f, 0.0f) },
			{ XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT4(0.5f, 0.5f, 0.5f, 0.0f) },
		};

		D3D11_BUFFER_DESC vDesc;
		//memset( &vDesc, 0, sizeof(D3D11_BUFFER_DESC) );
		vDesc.ByteWidth		 = sizeof(Vertex) * 8;
		vDesc.Usage			 = D3D11_USAGE_IMMUTABLE;
		vDesc.BindFlags		 = D3D11_BIND_VERTEX_BUFFER;
		vDesc.CPUAccessFlags = 0;
		vDesc.MiscFlags		 = 0;
		vDesc.StructureByteStride = 0;
		D3D11_SUBRESOURCE_DATA vInitData;
		vInitData.pSysMem	 = BoxVertex;
		HR( g_D3dDevice->CreateBuffer( &vDesc, &vInitData, &g_VertexBuffer) );

		UINT indices[] =
		{
			//FRONT
			0, 1, 2,
			2, 1, 3,

			//TOP
			0, 4, 5,
			0, 5, 1,

			//RIGHT
			1, 5, 3,
			3, 5, 7,

			//BACK
			4, 6, 5,
			5, 6, 7,

			//BOTTOM
			2, 7, 1,
			2, 3, 7,

			//LEFT
			0, 2, 4,
			2, 6, 4
		};

		D3D11_BUFFER_DESC bDesc;
		//memset( &bDesc, 0, sizeof( bDesc ) );
		bDesc.ByteWidth		= sizeof( UINT ) * 36;
		bDesc.Usage			= D3D11_USAGE_IMMUTABLE;
		bDesc.BindFlags		= D3D11_BIND_INDEX_BUFFER;
		bDesc.CPUAccessFlags = 0;
		bDesc.MiscFlags		= 0;
		bDesc.StructureByteStride = 0;
		D3D11_SUBRESOURCE_DATA iInitDate;
		iInitDate.pSysMem = indices;
		HR( g_D3dDevice->CreateBuffer( &bDesc, &iInitDate, &g_IndexBuffer ) );
	}
	
	void BuildInputLayout()
	{
		D3D11_INPUT_ELEMENT_DESC idesc[] = 
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
		};
		UINT numElements = ARRAYSIZE( idesc );

		g_EffectTechnique = g_Effect->GetTechniqueByName( "ColorTech" );
		D3DX11_PASS_DESC passDesc;
		g_EffectTechnique->GetPassByName("p0")->GetDesc(&passDesc);
		
		HR( g_D3dDevice->CreateInputLayout( idesc, numElements, passDesc.pIAInputSignature ,
					passDesc.IAInputSignatureSize, &g_InputLayout) );
	}

	LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
	{
		switch( msg )
		{
		case WM_SIZE:
			g_ClientWidth  = LOWORD( lParam );
			g_ClientHeight = HIWORD( lParam );

			if( g_D3dDevice )
			{
				if( wParam != SIZE_MINIMIZED )
				{
					onResizeAndInit();
				}
			}
			return 0;

		case WM_DESTROY:
			ReleaseAll();
			PostQuitMessage(0);
			return 0;

		default: 
			break;
		}
		return DefWindowProc(hWnd, msg, wParam, lParam); 
	}

you may need the BoxShader.fx file:

//
//		Effect for boxdemo
//

cbuffer cbPerObject
{
	float4x4 gWorldViewProj;
};

struct VertexIn
{
	float3 PosL : POSITION;
	float4 color : COLOR;
};

struct VertexOut
{
	float4 PosH : SV_POSITION;
	float4 color : COLOR;
};

VertexOut VS( VertexIn vin )
{
	VertexOut vout;
	vout.PosH = mul( float4( vin.PosL, 1.0f ), gWorldViewProj );
	
	vout.color = vin.color;
	
	return vout;
}

float4 PS( VertexOut pin ) : SV_Target
{
	return pin.color;
}

technique11 ColorTech
{
	pass p0
	{
		SetVertexShader( CompileShader( vs_5_0, VS() ) );
		SetGeometryShader( NULL );
		SetPixelShader( CompileShader( ps_5_0, PS() ) );
	}
}

Thanks for your help.


About constant reference

03 May 2013 - 06:09 AM

Today,l was met a strange problem.

 l have a buffer called mWavesVB:

ID3D11Buffer* mWavesVB;

 

And a class called wave:

 

Waves mWaves;

 

The declaration like this:

 

class Waves
{
public:
	Waves();
	~Waves();

	// Returns the solution at the ith grid point.
	const XMFLOAT3& operator[](int i)const { return mCurrSolution[i]; }

	// Returns the solution normal at the ith grid point.
	const XMFLOAT3& Normal(int i)const { return mNormals[i]; }

	// Returns the unit tangent vector at the ith grid point in the local x-axis direction.
	const XMFLOAT3 TangentX(int i)const { return mTangentX[i]; }

	void Init(UINT m, UINT n, float dx, float dt, float speed, float damping);
private:
	XMFLOAT3* mCurrSolution;
	XMFLOAT3* mNormals;
	XMFLOAT3* mTangentX;
};

 

The member variable all initialize and assgin the suitable value.

 

But when the following call happened,the project crashed:

 

D3D11_MAPPED_SUBRESOURCE mappedData;
md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);

Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData);
for(UINT i = 0; i < mWaves.VertexCount(); ++i)
{
	v[i].Pos    = mWaves[i];
	v[i].Normal = mWaves.Normal(i);
}

md3dImmediateContext->Unmap(mWavesVB, 0);

 

l know the problem is happened in this sentences:

v[i].Normal = mWaves.Normal(i);

Function Normal's value can't assignment to v[i].Normal.

 

but the things really bother me is,when the Normal's definition changed into:

 

const XMFLOAT3 Normal(int i)const { return mNormals[i]; }

 

Then the project suddenly work well. It‘s just like some magical power.it's So odd.

 


About input slot and vertex buffer

23 April 2013 - 10:40 AM

Hey,guys.

l am study Directx 11.And recently,l'm studying vertex buffer.But l got a problem about it.

The problem is, when l split position and color info into 2 vertex buffer and use 2 input slot(1 vertex buffer put in 1 input slot ),

but the cube's color is difference with the code use 1 input slot and 1 vertex buffer(The position and color info put in one vertex).

And then l also try using 1 input slot but using 2 vertex buffer,the cube's color difference with the first two cube.

The index buffer is same between this three cube.So why this happen?

Sorry if have any mistakes,my English level is poor.

Thanks!


program performance

16 December 2012 - 04:53 AM

how can l got a balance between performance and code standard.
for example, a varible shoule be in the class.But for the performance, it should be a global varible.
What should l do?

PARTNERS