Esempio n. 1
0
void CALL HGE_Impl::System_SetStateBool(hgeBoolState state, bool value)
{
	switch(state)
	{
		case HGE_WINDOWED:		if(VertArray || hwndParent) break;
								if(pD3DDevice && bWindowed != value)
								{
									if(d3dppW.BackBufferFormat==D3DFMT_UNKNOWN || d3dppFS.BackBufferFormat==D3DFMT_UNKNOWN) break;

									if(bWindowed) GetWindowRect(hwnd, &rectW);
									bWindowed=value;
									if(bWindowed) d3dpp=&d3dppW;
									else d3dpp=&d3dppFS;

									if(_format_id(d3dpp->BackBufferFormat) < 4) nScreenBPP=16;
									else nScreenBPP=32;

									_GfxRestore();
									_AdjustWindow();
								}
								else bWindowed=value;
								break;

		case HGE_ZBUFFER:		if(!pD3DDevice)	bZBuffer=value;
								break;

		case HGE_TEXTUREFILTER: bTextureFilter=value;
								if(pD3DDevice)
								{
									_render_batch();
									if(bTextureFilter)
									{
										pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
										pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
									}
									else
									{
										pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
										pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
									}
								}
								break;

		case HGE_USESOUND:		if(bUseSound!=value)
								{
									bUseSound=value;
									if(bUseSound && hwnd) _SoundInit();
									if(!bUseSound && hwnd) _SoundDone();
								}
								break;

		case HGE_HIDEMOUSE:		bHideMouse=value; break;

		case HGE_DONTSUSPEND:	bDontSuspend=value; break;

		#ifdef DEMO
		case HGE_SHOWSPLASH:	bDMO=value; break;
		#endif
	}
}
Esempio n. 2
0
bool HGE_Impl::_GfxInit()
{
//	static const char *szFormats[]={"UNKNOWN", "R5G6B5", "X1R5G5B5", "A1R5G5B5", "X8R8G8B8", "A8R8G8B8"};
//	D3DADAPTER_IDENTIFIER9 AdID;
//	D3DDISPLAYMODE Mode;
//	D3DFORMAT Format=D3DFMT_UNKNOWN;
//	UINT nModes, i;

// Init Open GL
    /*
    	HDC hDC;
    	HGLRC hRC;
    	long gl_error;


    	hDC = GetDC(pHGE->hwnd);
    	hRC = wglCreateContext(hDC);

    	if (hRC == NULL)
    	{
    		System_Log("Creating GL Context Failed with error code: %d", GetLastError());
    		return false;
    	}

    	System_Log("Initializing Graphix");

    	wglMakeCurrent(hDC, hRC);
    */
    long gl_error;



    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        System_Log("GLEW init Error: %s\n", glewGetErrorString(err));
    }
    else
        System_Log("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

    if (!GLEW_EXT_framebuffer_object)
    {
        System_Log("Status: Unavailable Extension GL_EXT_framebuffer_object\n");
    }
    else
        System_Log("Status: GL_EXT_framebuffer_object is supportet\n");



    glShadeModel(GL_SMOOTH);						// Enables Smooth Shading
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);      // 4-byte pixel alignment

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);


    glClearStencil(0);                          // clear stencil buffer
    glClearDepth(1.0f);								// Depth Buffer Setup


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
//	glCullFace(GL_BACK);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
//	glDisable(GL_DEPTH_TEST);


    glDepthFunc(GL_LEQUAL);							// The Type Of Depth Test To Do

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations



// Get adapter info

    System_Log("OpenGL version: %s", glGetString( GL_VERSION ));
//	System_Log("OpenGL version: %s", glGetString( GL_EXTENSIONS ));




// Set up Full Screen presentation parameters



    _AdjustWindow();

    System_Log("Mode: width = %d height = %d\n",nScreenWidth,nScreenHeight);

// Create vertex batch buffer

    textures=0;

// Init all stuff that can be lost

    _SetProjectionMatrix(nScreenWidth, nScreenHeight);

    if (GL_ERROR(gl_error))
    {
        System_Log("OpenGL error: %d", gl_error);
        return false;
    }

    if(!_init_lost()) return false;

    Gfx_Clear(0);

    System_Log("Graphix initialized");

    return true;
}
Esempio n. 3
0
bool HGE_Impl::_GfxInit()
{
	HRESULT hr = S_OK;

#pragma region
	const D3D_FEATURE_LEVEL featureLevels[] = {
		D3D_FEATURE_LEVEL_11_1,
		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 };

	UINT               numFeatureLevels = 7;
	D3D_FEATURE_LEVEL  selectedFeatureLevel = D3D_FEATURE_LEVEL_11_0;

	// create swap chain description
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
	swapChainDesc.BufferCount = 1;                                   // one back buffer
	swapChainDesc.BufferDesc.Width = nScreenWidth;                   // set the back buffer width
	swapChainDesc.BufferDesc.Height = nScreenHeight;                 // set the back buffer height
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;     // how swap chain is to be used
	swapChainDesc.OutputWindow = hwnd;                               // the window to be used
	swapChainDesc.SampleDesc.Count = 1;                              // how many multisamples
	swapChainDesc.SampleDesc.Quality = 0;
	swapChainDesc.Windowed = bWindowed;                              // windowed/full-screen mode
	swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;    // allow full-screen switching

	//Setup debuf flag
#ifdef _DEBUG
	UINT createDeviceFlags = D3D11_CREATE_DEVICE_DEBUG;
#else
	UINT createDeviceFlags = 0;
#endif
	//create device and swapchain
	hr = D3D11CreateDeviceAndSwapChain(
		NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		createDeviceFlags,
		featureLevels,
		numFeatureLevels,
		D3D11_SDK_VERSION,
		&swapChainDesc,
		&m_pSwapChain,
		&m_pD3DDevice,
		&selectedFeatureLevel,
		&m_pD3DDeviceContext);
	if (hr != S_OK)
	{
		_PostError("Can't initialize d3d11.");
		return false;
	}
#pragma endregion init swap chain

#pragma region 
	// Setup render target
	V_RETURN(m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&m_pBackBuffer));
	V_RETURN(m_pD3DDevice->CreateRenderTargetView(m_pBackBuffer, NULL, &pRTView));
	//m_pBackBuffer->Release();

	m_pD3DDeviceContext->OMSetRenderTargets(1, &pRTView, NULL);

	// Setup the viewport
	D3D11_VIEWPORT vp;
	vp.Width = (FLOAT)nScreenWidth;
	vp.Height = (FLOAT)nScreenHeight;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	m_pD3DDeviceContext->RSSetViewports(1, &vp);

	/*
	// Create depth stencil texture
	D3D11_TEXTURE2D_DESC descDepth;
	ZeroMemory(&descDepth, sizeof(descDepth));
	descDepth.Width = nScreenWidth;
	descDepth.Height = nScreenHeight;
	descDepth.MipLevels = 1;
	descDepth.ArraySize = 1;
	descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	descDepth.SampleDesc.Count = 1;
	descDepth.SampleDesc.Quality = 0;
	descDepth.Usage = D3D11_USAGE_DEFAULT;
	descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	descDepth.CPUAccessFlags = 0;
	descDepth.MiscFlags = 0;
	hr = m_pDevice->CreateTexture2D(&descDepth, NULL, &m_pDepthBuffer);
	if (FAILED(hr))
	return hr;

	// Create the depth stencil view
	D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
	ZeroMemory(&descDSV, sizeof(descDSV));
	descDSV.Format = descDepth.Format;
	descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
	descDSV.Texture2D.MipSlice = 0;
	hr = m_pDevice->CreateDepthStencilView(m_pDepthBuffer, &descDSV, &pDSView);
	if (FAILED(hr))
	return hr;
	*/
	//m_pDeviceContext->OMSetRenderTargets(1, &pRTView, pDSView);
#pragma endregion setup render targets & viewport

#pragma region
	std::string vertexProfile, pixelProfile;
	if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0)
	{
		vertexProfile = "vs_5_0";
		pixelProfile = "ps_5_0";
	}
	else if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_10_1)
	{
		vertexProfile = "vs_4_1";
		pixelProfile = "ps_4_1";
	}
	else if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_10_0)
	{
		vertexProfile = "vs_4_0";
		pixelProfile = "ps_4_0";
	}
	else if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_9_3)
	{
		vertexProfile = "vs_4_0_level_9_3";
		pixelProfile = "ps_4_0_level_9_3";
	}
	else if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_9_2)
	{
		vertexProfile = "vs_4_0_level_9_1";
		pixelProfile = "ps_4_0_level_9_1";
	}
	else if (m_pD3DDevice->GetFeatureLevel() == D3D_FEATURE_LEVEL_9_1)
	{
		vertexProfile = "vs_4_0_level_9_1";
		pixelProfile = "ps_4_0_level_9_1";
	}

	//D3DCOMPILE_OPTIMIZATION_LEVEL3 &
	//D3DCOMPILE_ENABLE_STRICTNESS
	UINT flags = (1 << 11) | (1 << 15);


	// Build Flat Vertex Shader
	ID3DBlob* binarycode = 0, *errors = 0, *signature0 = 0, *signature1 = 0;
	V_RETURN(D3DCompile(vsSource, strlen(vsSource), "VertexShader0", 0, 0, "main", vertexProfile.c_str(), flags, 0, &binarycode, &errors));

	V_RETURN(D3DGetInputSignatureBlob(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), &signature0));


	V_RETURN(m_pD3DDevice->CreateVertexShader(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), 0, &m_pVS0));

	if (binarycode) binarycode->Release();
	if (errors) errors->Release();

	// Build Textured Vertex Shader
	binarycode = 0;
	errors = 0;
	V_RETURN(D3DCompile(vsTexturedSource, strlen(vsTexturedSource), "VertexShader1", 0, 0, "main", vertexProfile.c_str(), flags, 0, &binarycode, &errors));

	V_RETURN(D3DGetInputSignatureBlob(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), &signature1));

	V_RETURN(m_pD3DDevice->CreateVertexShader(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), 0, &m_pVS1));

	if (binarycode) binarycode->Release();
	if (errors) errors->Release();

	// Build Flat Pixel Shader
	binarycode = 0;
	errors = 0;
	V_RETURN(D3DCompile(psSource, strlen(psSource), "PixelShader0", 0, 0, "main", pixelProfile.c_str(), flags, 0, &binarycode, &errors));

	V_RETURN(m_pD3DDevice->CreatePixelShader(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), 0, &m_pPS0));

	if (binarycode) binarycode->Release();
	if (errors) errors->Release();

	// Build Textured Pixel Shader
	binarycode = 0;
	errors = 0;
	V_RETURN(D3DCompile(psTexturedSource, strlen(psTexturedSource), "PixelShader1", 0, 0, "main", pixelProfile.c_str(), flags, 0, &binarycode, &errors));

	V_RETURN(m_pD3DDevice->CreatePixelShader(binarycode->GetBufferPointer(), binarycode->GetBufferSize(), 0, &m_pPS1));

	if (binarycode) binarycode->Release();
	if (errors) errors->Release();


	// Compile the shaders using the lowest possible profile for broadest feature level support
	// 	ID3DBlob* pVertexShaderBuffer = NULL;
	// 	V_RETURN(CompileShaderFromFile(TEXT("BasicHLSL11_VS.hlsl"), "VSMain", vertexProfile.c_str(), &pVertexShaderBuffer));
	// 
	// 	ID3DBlob* pPixelShaderBuffer = NULL;
	// 	V_RETURN(CompileShaderFromFile(TEXT("BasicHLSL11_PS.hlsl"), "PSMain", pixelProfile.c_str(), &pPixelShaderBuffer));
	// 
	// 	// Create the shaders
	// 	V_RETURN(m_pD3DDevice->CreateVertexShader(pVertexShaderBuffer->GetBufferPointer(),
	// 		pVertexShaderBuffer->GetBufferSize(), NULL, &pVS0));
	// 	V_RETURN(m_pD3DDevice->CreatePixelShader(pPixelShaderBuffer->GetBufferPointer(),
	// 		pPixelShaderBuffer->GetBufferSize(), NULL, &pPS0));

	// Create Input Layout
	V_RETURN(m_pD3DDevice->CreateInputLayout(vertexLayout, 3, signature0->GetBufferPointer(), signature0->GetBufferSize(), &m_pInputLayout0));
	signature0->Release();

	// Create Input Layout
	V_RETURN(m_pD3DDevice->CreateInputLayout(vertexLayout, 3, signature1->GetBufferPointer(), signature1->GetBufferSize(), &m_pInputLayout1));
	signature1->Release();

#pragma endregion create shaders and input layout

#pragma region
	// Setup constant buffers
	D3D11_BUFFER_DESC desc;
	desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	desc.MiscFlags = 0;
	desc.StructureByteStride = 0;
	desc.ByteWidth = sizeof(hgeVertex)* (VERTEX_BUFFER_SIZE);
	desc.Usage = D3D11_USAGE_DYNAMIC;
	V_RETURN(m_pD3DDevice->CreateBuffer(&desc, NULL, &m_pVertexBuf));

	// 	D3D11_BUFFER_DESC desc;
	// 	desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
	// 	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	// 	desc.MiscFlags = 0;
	// 	desc.StructureByteStride = 0;
	// 	desc.ByteWidth = 6 * (VERTEX_BUFFER_SIZE/4) * sizeof(uint16_t);
	// 	desc.Usage = D3D11_USAGE_DYNAMIC;
	// 	V_RETURN(m_pD3DDevice->CreateBuffer(&desc, NULL, &m_pIndexBuf));
	// 
	// 	for (int i = 0; i < VERTEX_BUFFER_SIZE / 4; i++)
	// 	{
	// 		*pIndices++ = n;
	// 		*pIndices++ = n + 1;
	// 		*pIndices++ = n + 2;
	// 		*pIndices++ = n + 2;
	// 		*pIndices++ = n + 3;
	// 		*pIndices++ = n;
	// 		n += 4;
	// 	}
	// 
	// 	m_pD3DDeviceContext->IASetIndexBuffer(m_pIndexBuf, DXGI_FORMAT_R16_UINT, 0);
	D3D11_BUFFER_DESC Desc;
	Desc.Usage = D3D11_USAGE_DYNAMIC;
	Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	Desc.MiscFlags = 0;
	Desc.ByteWidth = sizeof(CB_VS_PER_OBJECT);
	V_RETURN(m_pD3DDevice->CreateBuffer(&Desc, NULL, &g_pcbVSPerObject));
	Desc.ByteWidth = sizeof(CB_PS_PER_OBJECT);
	V_RETURN(m_pD3DDevice->CreateBuffer(&Desc, NULL, &g_pcbPSPerObject));
	Desc.ByteWidth = sizeof(CB_PS_PER_FRAME);
	V_RETURN(m_pD3DDevice->CreateBuffer(&Desc, NULL, &g_pcbPSPerFrame));
#pragma endregion setup constant buffers

	// Create Sampler State
	D3D11_SAMPLER_DESC samplerDesc;
	samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.BorderColor[0] = samplerDesc.BorderColor[1] = samplerDesc.BorderColor[2] = samplerDesc.BorderColor[3] = 0.0f;
	samplerDesc.ComparisonFunc = (D3D11_COMPARISON_FUNC)0;
	samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	samplerDesc.MaxAnisotropy = 1.0f;
	samplerDesc.MaxLOD = 0; //D3D11_FLOAT32_MAX
	samplerDesc.MinLOD = 0;
	samplerDesc.MipLODBias = 0.0f;
	V_RETURN(m_pD3DDevice->CreateSamplerState(&samplerDesc, &m_pSamplerState));

	// Create Blend State
	D3D11_BLEND_DESC blendDesc;
	blendDesc.AlphaToCoverageEnable = false;
	blendDesc.IndependentBlendEnable = false;
	blendDesc.RenderTarget[0].BlendEnable = true;
	blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
	blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
	blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
	blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
	blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
	blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	V_RETURN(m_pD3DDevice->CreateBlendState(&blendDesc, &m_pBlendState));

	// Create Depth Stencil State
	D3D11_DEPTH_STENCIL_DESC depthDesc;
	depthDesc.DepthEnable = false;
	depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
	depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
	depthDesc.StencilEnable = false;
	depthDesc.StencilReadMask = depthDesc.StencilWriteMask = 0;
	depthDesc.BackFace.StencilDepthFailOp = depthDesc.BackFace.StencilFailOp = depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
	depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
	depthDesc.FrontFace = depthDesc.BackFace;
	V_RETURN(m_pD3DDevice->CreateDepthStencilState(&depthDesc, &m_pDepthStencilState));

	// Create Rasterizer State
	D3D11_RASTERIZER_DESC rastDesc;
	rastDesc.FillMode = D3D11_FILL_SOLID;
	rastDesc.CullMode = D3D11_CULL_NONE;
	rastDesc.FrontCounterClockwise = FALSE;
	rastDesc.DepthBias = 0;
	rastDesc.DepthBiasClamp = 0;
	rastDesc.SlopeScaledDepthBias = 0;
	rastDesc.ScissorEnable = FALSE;
	rastDesc.MultisampleEnable = FALSE;
	rastDesc.AntialiasedLineEnable = FALSE;
	hr = m_pD3DDevice->CreateRasterizerState(&rastDesc, &m_pRasterizerState);

	// 	mUpdate = false;
	// 
	// 	mIsInitialise = true;

	// Setup the camera's view parameters
	// 	D3DXVECTOR3 vecEye(0.0f, 0.0f, -100.0f);
	// 	D3DXVECTOR3 vecAt(0.0f, 0.0f, -0.0f);
	// 	g_Camera.SetViewParams(&vecEye, &vecAt);
	// 	g_Camera.SetRadius(fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f);

	//return S_OK;

	// Get adapter info
	// 	auto adapterList = EnumerateAdapters();
	// 	for (auto it = adapterList.begin(); it != adapterList.end(); it++)
	// 	{
	// 		System_Log("D3D Driver: %s", "123123123123123");
	// 		DXGI_ADAPTER_DESC desc;
	// 		(*it)->GetDesc(&desc);
	// 		System_Log("Description: %s", desc.Description);
	// 
	// 	}

	m_prim_count = 0;
	m_cur_prim_type = HGEPRIM_QUADS;
	m_cur_blend_mode = BLEND_DEFAULT;

	//todo: test those functions
	_AdjustWindow();

	System_Log("Screen Resolution: %d x %d\n", nScreenWidth, nScreenHeight);

	_SetProjectionMatrix(nScreenWidth, nScreenHeight);

	if (!_init_lost()) return false;

	Gfx_Clear(0);

	return true;
}