Beispiel #1
0
void MFRenderer_EndFramePlatformSpecific()
{
	MFCALLSTACK;

#if MF_DISPLAY == MF_DRIVER_X11
	glXSwapBuffers(xdisplay, glXWindow);
#elif MF_DISPLAY == MF_DRIVER_WIN32
	SwapBuffers(hDC);
#elif MF_DISPLAY == MF_DRIVER_SDL2
	MFDisplay *pDisplay = MFDisplay_GetCurrent();
	if(pDisplay)
		SDL_GL_SwapWindow((SDL_Window*)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow));
#elif MF_DISPLAY == MF_DRIVER_IPHONE
	MFRendererIPhone_SwapBuffers();
#endif

	MFCheckForOpenGLError(true);
}
Beispiel #2
0
void MFRenderer_DestroyDisplay(MFDisplay *pDisplay)
{
	if(gpDeviceRenderTarget)
		MFRenderTarget_Release(gpDeviceRenderTarget);

#if MF_DISPLAY == MF_DRIVER_X11
	if(fbConfigs != NULL)
	{
		XFree(fbConfigs);
		fbConfigs = NULL;
	}

	if(glXContext != NULL)
	{
		glXDestroyContext(xdisplay, glXContext);
		glXContext = NULL;
	}

	if(glXWindow != 0)
	{
		glXDestroyWindow(xdisplay, glXWindow);
		glXWindow = 0;
	}
#elif MF_DISPLAY == MF_DRIVER_WIN32
	HWND hWnd = (HWND)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow);

	if(hRC)
	{
		if(wglMakeCurrent(NULL, NULL))
		{
			wglDeleteContext(hRC);
		}
		hRC = NULL;
	}

	if(hDC)
	{
		ReleaseDC(hWnd, hDC);
		hDC = NULL;
	}
#elif MF_DISPLAY == MF_DRIVER_SDL2
	SDL_GL_DeleteContext(glContext);
#endif
}
Beispiel #3
0
void MFSound_InitModulePlatformSpecific(int *pSoundDataSize, int *pVoiceDataSize)
{
	MFCALLSTACK;

	void MFSound_InitWASAPI();
	MFSound_InitWASAPI();

#if defined(MF_XBOX)
	DirectSoundCreate(NULL, &pDirectSound, NULL);
#else
	HRESULT hr = DirectSoundCreate8(NULL, &pDirectSound, NULL);
	MFDebug_Assert(SUCCEEDED(hr), "Failed to create DirectSound instance.");
	if(FAILED(hr))
		return;

	// create the primary sound buffer
	// fill out DSBuffer creation data
	DSBUFFERDESC desc;

	desc.dwSize = sizeof(DSBUFFERDESC);
	desc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN;
	desc.dwBufferBytes = 0;
	desc.lpwfxFormat = NULL;
	desc.dwReserved = 0; 
	desc.guid3DAlgorithm = DS3DALG_DEFAULT; 

	// create the DSBuffer
	hr = pDirectSound->CreateSoundBuffer(&desc, &pDSPrimaryBuffer, NULL);
	MFDebug_Assert(SUCCEEDED(hr), "Failed to create the Primary Sound Buffer");

	HWND hWnd = (HWND)MFWindow_GetSystemWindowHandle(MFDisplay_GetCurrent()->settings.pWindow);
	hr = pDirectSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY);
	MFDebug_Assert(SUCCEEDED(hr), "Failed to set the DirectSound cooperative level");
#endif

	// we need to return the size of the internal structures so the platform independant
	// code can make the correct allocations..
	*pSoundDataSize = sizeof(MFSoundDataInternal);
	*pVoiceDataSize = sizeof(MFVoiceDataInternal);
}
Beispiel #4
0
int MFRenderer_CreateDisplay(MFDisplay *pDisplay)
{
#if MF_DISPLAY == MF_DRIVER_X11
	Window window = (Window)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow);

	glXWindow = glXCreateWindow(xdisplay, fbConfigs[0], window, NULL);
	if(!glXWindow)
	{
		MFDebug_Error("Unable to associate window with a GLXWindow");
		MFRenderer_DestroyDisplay(pDisplay);
		return 1;
	}

	glXContext = glXCreateNewContext(xdisplay, fbConfigs[0], GLX_RGBA_TYPE, NULL, true);
	if(!glXContext)
	{
		MFDebug_Error("Unable to create GLXContext");
		MFRenderer_DestroyDisplay(pDisplay);
		return 1;
	}

	XFree(fbConfigs);
	fbConfigs = NULL;

	if(!glXMakeContextCurrent(xdisplay, glXWindow, glXWindow, glXContext))
	{
		MFDebug_Error("glXMakeContextCurrent failed");
		MFRenderer_DestroyDisplay(pDisplay);
		return 1;
	}
#elif MF_DISPLAY == MF_DRIVER_WIN32
	HWND hWnd = (HWND)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow);

	GLuint pixelFormat;

	PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
		PFD_TYPE_RGBA,
		32, // colour depth
		0, 0, 0, 0, 0, 0,
		0, // No Alpha Buffer
		0, // Shift Bit Ignored
		0, // No Accumulation Buffer
		0, 0, 0, 0, // Accumulation Bits Ignored
		24, // 16Bit Z-Buffer (Depth Buffer)
		8, // No Stencil Buffer
		0, // No Auxiliary Buffer
		PFD_MAIN_PLANE, // Main Drawing Layer
		0, // Reserved
		0, 0, 0 // Layer Masks Ignored
	};

	pfd.dwFlags |= pDisplay->settings.numBuffers > 1 ? PFD_DOUBLEBUFFER : 0;

	hDC = GetDC(hWnd);
	if(!hDC)
	{
		MFRenderer_DestroyDisplay(pDisplay);
		MessageBoxA(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
		return 1;
	}

	pixelFormat = ChoosePixelFormat(hDC, &pfd);
	if(!pixelFormat)
	{
		MFRenderer_DestroyDisplay(pDisplay);
		MessageBoxA(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
		return 2;
	}

	if(!SetPixelFormat(hDC, pixelFormat, &pfd))
	{
		MFRenderer_DestroyDisplay(pDisplay);
		MessageBoxA(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
		return 3;
	}

	hRC = wglCreateContext(hDC);
	if(!hRC)
	{
		// *** driver bug ***
		// HACK: do it again...
		SetPixelFormat(hDC, pixelFormat, &pfd);
		hRC = wglCreateContext(hDC);
	}

	if(!hRC)
	{
		MessageBoxA(NULL, MFStr("Failed to create OpenGL context: %s", MFSystemPC_GetLastError()), "ERROR", MB_OK|MB_ICONEXCLAMATION);

		MFRenderer_DestroyDisplay(pDisplay);
		return 4;
	}

	if(!wglMakeCurrent(hDC, hRC))
	{
		MFRenderer_DestroyDisplay(pDisplay);
		MessageBoxA(NULL, "Can't Activate The GL Rendering Context.", "ERROR", MB_OK|MB_ICONEXCLAMATION);
		return 5;
	}
#elif MF_DISPLAY == MF_DRIVER_SDL2
	glContext = SDL_GL_CreateContext((SDL_Window*)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow));
#elif MF_DISPLAY == MF_DRIVER_IPHONE
	MFRendererIPhone_MakeCurrent();
#elif MF_DISPLAY == MF_DRIVER_NACL
	// do we need to do anything?
#endif

    // get the opengl version
	const char *pVersion = (const char *)glGetString(GL_VERSION);
	while(pVersion && *pVersion && !MFIsNumeric(*pVersion))
		++pVersion;
	float ver = MFString_AsciiToFloat(pVersion);
	gOpenGLVersion = (int)(ver * 100);

#if !defined(MF_OPENGL_ES)
	// glew wrangles all the horrid extensions...
	GLenum r = glewInit();
	MFDebug_Assert(r == GLEW_OK, "Error loading extensions!");
#endif

#if !defined(MF_OPENGL_ES)
	glEnable(GL_LINE_SMOOTH);

//	glFrontFace(GL_CW);
//	glCullFace(GL_BACK);

	glDisable(GL_LIGHTING);
#endif

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glEnable(GL_TEXTURE_2D);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

#if defined(MF_OPENGL_ES)
	// we need the EGL display apparently...
//	eglSwapInterval(, 1);
#else
#if MF_DISPLAY == MF_DRIVER_X11
//	GLXDrawable drawable = glXGetCurrentDrawable();
//	glXSwapIntervalEXT(xdisplay, drawable, 1);
#elif MF_DISPLAY == MF_DRIVER_WIN32
//	wglSwapInterval(1);
#endif
#endif

	glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&gDefaultRenderTarget);

	MFTextureDesc texDesc = { MFTexType_2D, ImgFmt_A8R8G8B8, pDisplay->settings.width, pDisplay->settings.height, 0, 0, 1, MFTCF_RenderTarget };
	gpDeviceColourTarget = MFTexture_InitTexture(&texDesc, MFRD_OpenGL, 0);
	gpDeviceColourTarget->pName = "Device Colour Target";
	gpDeviceColourTarget->pSurfaces[0].platformData = (uint64)gDefaultRenderTarget;

	texDesc.format = ImgFmt_D24S8;
	gpDeviceZTarget = MFTexture_InitTexture(&texDesc, MFRD_OpenGL, 0);
	gpDeviceZTarget->pName = "Device Depth Stencil";
	gpDeviceZTarget->pSurfaces[0].platformData = 0;

	MFRenderTargetDesc desc;
	desc.pName = "Device Render Target";
	desc.width = pDisplay->settings.width;
	desc.height = pDisplay->settings.height;
	desc.colourTargets[0].pSurface = gpDeviceColourTarget;
	desc.depthStencil.pSurface = gpDeviceZTarget;
	gpDeviceRenderTarget = MFRenderTarget_Create(&desc);

	gCurrentViewport.x = 0.0f;
	gCurrentViewport.y = 0.0f;
	gCurrentViewport.width = (float)pDisplay->settings.width;
	gCurrentViewport.height = (float)pDisplay->settings.height;
	glViewport(0, 0, pDisplay->settings.width, pDisplay->settings.height);

	return 0;
}
Beispiel #5
0
int MFRenderer_CreateDisplay(MFDisplay *pDisplay)
{
	HWND hWnd = (HWND)MFWindow_GetSystemWindowHandle(pDisplay->settings.pWindow);

	HRESULT hr = S_OK;

    RECT rc;
    GetClientRect( hWnd, &rc );
    UINT width = rc.right - rc.left;
    UINT height = rc.bottom - rc.top;

    UINT createDeviceFlags = 0;
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_DRIVER_TYPE driverTypes[] =
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };
    UINT 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,
    };
	UINT numFeatureLevels = ARRAYSIZE( featureLevels );

    DXGI_SWAP_CHAIN_DESC sd;
    MFZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;

    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
    {
        g_driverType = driverTypes[driverTypeIndex];
        hr = D3D11CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
                                            D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
        if( SUCCEEDED( hr ) )
            break;
    }
    if( FAILED( hr ) )
        return hr;

	MFRenderer_D3D11_SetDebugName(g_pImmediateContext, "MFRenderer global device context");

    // Create a render target view
    ID3D11Texture2D* pBackBuffer = NULL;
    hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
    if( FAILED( hr ) )
        return hr;

	MFRenderer_D3D11_SetDebugName(pBackBuffer, "MFRenderer back buffer");

    hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
    pBackBuffer->Release();
    if( FAILED( hr ) )
        return hr;

	MFRenderer_D3D11_SetDebugName(g_pRenderTargetView, "MFRenderer render target view");

	// Create depth stencil texture
    D3D11_TEXTURE2D_DESC descDepth;
    ZeroMemory( &descDepth, sizeof(descDepth) );
    descDepth.Width = width;
    descDepth.Height = height;
    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 = g_pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil );
    if( FAILED( hr ) )
        return hr;

	MFRenderer_D3D11_SetDebugName(g_pDepthStencil, "MFRenderer depth stencil buffer");

    // 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 = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView );
    if( FAILED( hr ) )
        return hr;

	MFRenderer_D3D11_SetDebugName(g_pDepthStencilView, "MFRenderer depth stencil view");

    g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );

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

	//--

	cbWorld.mWorldToScreen = MFMatrix::identity;
	cbWorld.mLocalToWorld = MFMatrix::identity;
	
	D3D11_BUFFER_DESC desc;
	MFZeroMemory(&desc, sizeof(desc));
	desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	desc.ByteWidth = sizeof(cbWorld);
	desc.Usage = D3D11_USAGE_DEFAULT;
	//desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

	D3D11_SUBRESOURCE_DATA data;
	MFZeroMemory(&data, sizeof(data));
	data.pSysMem = &cbWorld;

	g_pd3dDevice->CreateBuffer(&desc, &data, &g_pConstantBufferWorld);

	MFRenderer_D3D11_SetDebugName(g_pConstantBufferWorld, "MFRenderer world constant buffer");

	g_pImmediateContext->VSSetConstantBuffers(n_cbWorld, 1, &g_pConstantBufferWorld);
	g_pImmediateContext->PSSetConstantBuffers(n_cbWorld, 1, &g_pConstantBufferWorld);

	return 0;
}