Files
dolphin/Source/Plugins/Plugin_VideoOGL/Src/main.cpp
T

497 lines
13 KiB
C++
Raw Normal View History

// Copyright (C) 2003-2009 Dolphin Project.
2008-12-08 05:25:12 +00:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
2009-06-06 13:36:33 +00:00
2009-06-06 13:36:33 +00:00
// OpenGL Plugin Documentation
/*
2009-06-06 13:36:33 +00:00
1.1 Display settings
2009-06-06 13:36:33 +00:00
Internal and fullscreen resolution: Since the only internal resolutions allowed are also
fullscreen resolution allowed by the system there is only need for one resolution setting
that applies to both the internal resolution and the fullscreen resolution.
Todo: Make the internal resolution option apply instantly, currently only the native and 2x option
applies instantly. To do this we need to enumerate all avaliable display resolutions before
Renderer:Init().
2009-06-06 13:36:33 +00:00
1.2 Screenshots
The screenshots should be taken from the internal representation of the picture regardless of
what the current window size is. Since AA and wireframe is applied together with the picture resizing
this rule is not currently applied to AA or wireframe pictures, they are instead taken from whatever
the window size is.
Todo: Render AA and wireframe to a separate picture used for the screenshot in addition to the one
for display.
1.3 AA
Make AA apply instantly during gameplay if possible
*/
2009-06-06 13:36:33 +00:00
2008-12-08 05:25:12 +00:00
#include "Globals.h"
2009-01-11 13:37:13 +00:00
#include <cstdarg>
2008-12-08 05:25:12 +00:00
#ifdef _WIN32
#include "OS/Win32.h"
#endif
#if defined(HAVE_WX) && HAVE_WX
#include "GUI/ConfigDlg.h"
#endif
#include "Config.h"
#include "LookUpTables.h"
#include "ImageWrite.h"
#include "Render.h"
#include "GLUtil.h"
#include "Fifo.h"
#include "OpcodeDecoding.h"
#include "TextureMngr.h"
2009-06-22 09:31:30 +00:00
#include "BPStructs.h"
2008-12-08 05:25:12 +00:00
#include "VertexLoader.h"
#include "VertexLoaderManager.h"
#include "VertexManager.h"
#include "PixelShaderCache.h"
2008-12-08 05:25:12 +00:00
#include "PixelShaderManager.h"
#include "VertexShaderCache.h"
2008-12-08 05:25:12 +00:00
#include "VertexShaderManager.h"
#include "XFB.h"
#include "XFBConvert.h"
#include "TextureConverter.h"
#include "PostProcessing.h"
#include "OnScreenDisplay.h"
#include "Setup.h"
2008-12-08 05:25:12 +00:00
#include "VideoState.h"
2008-12-14 23:52:01 +00:00
2008-12-08 05:25:12 +00:00
SVideoInitialize g_VideoInitialize;
PLUGIN_GLOBALS* globals = NULL;
2008-12-08 05:25:12 +00:00
// Logging
int GLScissorX, GLScissorY, GLScissorW, GLScissorH;
static bool s_PluginInitialized = false;
2008-12-08 05:25:12 +00:00
#if defined(HAVE_WX) && HAVE_WX
void DllDebugger(HWND _hParent, bool Show)
{
2009-02-21 02:42:35 +00:00
// TODO: Debugger needs recoding, right now its useless
2008-12-08 05:25:12 +00:00
}
2009-02-04 17:48:40 +00:00
void DoDllDebugger(){}
2008-12-08 05:25:12 +00:00
#else
void DllDebugger(HWND _hParent, bool Show) { }
2008-12-08 05:25:12 +00:00
void DoDllDebugger() { }
#endif
void GetDllInfo (PLUGIN_INFO* _PluginInfo)
2008-12-08 05:25:12 +00:00
{
_PluginInfo->Version = 0x0100;
_PluginInfo->Type = PLUGIN_TYPE_VIDEO;
#ifdef DEBUGFAST
2008-12-08 05:25:12 +00:00
sprintf(_PluginInfo->Name, "Dolphin OpenGL (DebugFast)");
#else
#ifndef _DEBUG
sprintf(_PluginInfo->Name, "Dolphin OpenGL");
#else
sprintf(_PluginInfo->Name, "Dolphin OpenGL (Debug)");
#endif
#endif
}
void SetDllGlobals(PLUGIN_GLOBALS* _pPluginGlobals)
{
globals = _pPluginGlobals;
LogManager::SetInstance((LogManager *)globals->logManager);
2009-01-08 12:12:15 +00:00
}
// This is used for the fuctions right below here, in DllConfig()
#if defined(HAVE_WX) && HAVE_WX && defined _WIN32
WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
extern HINSTANCE g_hInstance;
#endif
2008-12-08 05:25:12 +00:00
void DllConfig(HWND _hParent)
{
#if defined(HAVE_WX) && HAVE_WX
// This is needed because now we use wxEntryCleanup() when closing the configuration window
2009-02-23 21:38:06 +00:00
// if (!wxTheApp || !wxTheApp->CallOnInit())
// {
#ifdef _WIN32
wxSetInstance((HINSTANCE)g_hInstance);
#endif
2009-02-23 21:38:06 +00:00
// int argc = 0;
// char **argv = NULL;
// wxEntryStart(argc, argv);
2009-02-23 21:38:06 +00:00
// }
wxWindow *win = new wxWindow();
#ifdef _WIN32
win->SetHWND((WXHWND)_hParent);
win->AdoptAttributesFromHWND();
#endif
ConfigDialog *config_dialog = new ConfigDialog(win);
#endif
//Console::Open();
#if defined(_WIN32) && defined(HAVE_WX) && HAVE_WX
// Search for avaliable resolutions
2008-12-08 05:25:12 +00:00
DWORD iModeNum = 0;
DEVMODE dmi;
ZeroMemory(&dmi, sizeof(dmi));
dmi.dmSize = sizeof(dmi);
std::vector<std::string> resos;
resos.reserve(20);
2008-12-08 05:25:12 +00:00
int i = 0;
2008-12-08 05:25:12 +00:00
while (EnumDisplaySettings(NULL, iModeNum++, &dmi) != 0)
{
2008-12-08 05:25:12 +00:00
char szBuffer[100];
sprintf(szBuffer, "%dx%d", dmi.dmPelsWidth, dmi.dmPelsHeight);
2008-12-08 05:25:12 +00:00
std::string strBuffer(szBuffer);
// Create a check loop to check every pointer of resolutions to see if the res is added or not
2008-12-08 05:25:12 +00:00
int b = 0;
bool resFound = false;
while (b < i && !resFound)
{
// Is the res already added?
2008-12-08 05:25:12 +00:00
resFound = (resos[b] == strBuffer);
b++;
}
// Add the resolution
if (!resFound && i < 100) // don't want to overflow resos array. not likely to happen, but you never know.
2008-12-08 05:25:12 +00:00
{
resos.push_back(strBuffer);
2008-12-08 05:25:12 +00:00
i++;
config_dialog->AddFSReso(szBuffer);
config_dialog->AddWindowReso(szBuffer);
2008-12-08 05:25:12 +00:00
}
ZeroMemory(&dmi, sizeof(dmi));
}
#elif defined(HAVE_X11) && HAVE_X11 && defined(HAVE_XXF86VM) &&\
2008-12-14 23:52:01 +00:00
HAVE_XXF86VM && defined(HAVE_WX) && HAVE_WX
2008-12-08 05:25:12 +00:00
int glxMajorVersion, glxMinorVersion;
int vidModeMajorVersion, vidModeMinorVersion;
GLWin.dpy = XOpenDisplay(0);
glXQueryVersion(GLWin.dpy, &glxMajorVersion, &glxMinorVersion);
XF86VidModeQueryVersion(GLWin.dpy, &vidModeMajorVersion, &vidModeMinorVersion);
//Get all full screen resos for the config dialog
XF86VidModeModeInfo **modes = NULL;
int modeNum = 0;
int bestMode = 0;
//set best mode to current
bestMode = 0;
XF86VidModeGetAllModeLines(GLWin.dpy, GLWin.screen, &modeNum, &modes);
int px = 0, py = 0;
if (modeNum > 0 && modes != NULL)
2008-12-08 05:25:12 +00:00
{
for (int i = 0; i < modeNum; i++)
2008-12-08 05:25:12 +00:00
{
if (px != modes[i]->hdisplay && py != modes[i]->vdisplay)
2008-12-08 05:25:12 +00:00
{
char temp[32];
sprintf(temp,"%dx%d", modes[i]->hdisplay, modes[i]->vdisplay);
config_dialog->AddFSReso(temp);
config_dialog->AddWindowReso(temp);//Add same to Window ones, since they should be nearly all that's needed
2008-12-08 05:25:12 +00:00
px = modes[i]->hdisplay;//Used to remove repeating from different screen depths
py = modes[i]->vdisplay;
}
}
}
2008-12-08 05:25:12 +00:00
XFree(modes);
2009-05-15 08:55:46 +00:00
#elif defined(HAVE_COCOA) && HAVE_COCOA && defined(HAVE_WX) && HAVE_WX
CFArrayRef modes;
CFRange range;
CFDictionaryRef modesDict;
CFNumberRef modeValue;
int modeWidth;
int modeHeight;
int modeBpp;
int modeIndex;
int px = 0, py = 0;
modes = CGDisplayAvailableModes(CGMainDisplayID());
range.location = 0;
range.length = CFArrayGetCount(modes);
for (modeIndex=0; modeIndex<range.length; modeIndex++)
{
modesDict = (CFDictionaryRef)CFArrayGetValueAtIndex(modes, modeIndex);
modeValue = (CFNumberRef) CFDictionaryGetValue(modesDict, kCGDisplayWidth);
CFNumberGetValue(modeValue, kCFNumberLongType, &modeWidth);
modeValue = (CFNumberRef) CFDictionaryGetValue(modesDict, kCGDisplayHeight);
CFNumberGetValue(modeValue, kCFNumberLongType, &modeHeight);
modeValue = (CFNumberRef) CFDictionaryGetValue(modesDict, kCGDisplayBitsPerPixel);
CFNumberGetValue(modeValue, kCFNumberLongType, &modeBpp);
if (px != modeWidth && py != modeHeight)
{
char temp[32];
sprintf(temp,"%dx%d", modeWidth, modeHeight);
config_dialog->AddFSReso(temp);
config_dialog->AddWindowReso(temp);//Add same to Window ones, since they should be nearly all that's needed
px = modeWidth;
py = modeHeight;
}
}
2009-02-23 21:38:06 +00:00
#endif
// Check if at least one resolution was found. If we don't and the resolution array is empty
// CreateGUIControls() will crash because the array is empty.
2009-04-30 08:14:46 +00:00
#if defined(HAVE_WX) && HAVE_WX
2009-02-23 21:38:06 +00:00
if (config_dialog->arrayStringFor_FullscreenCB.size() == 0)
{
config_dialog->AddFSReso("<No resolutions found>");
config_dialog->AddWindowReso("<No resolutions found>");
}
config_dialog->CreateGUIControls();
// Hm, why does this code show it modally?
2009-02-23 21:38:06 +00:00
config_dialog->ShowModal();
2009-04-30 08:14:46 +00:00
#endif
2008-12-08 05:25:12 +00:00
}
void Initialize(void *init)
2008-12-08 05:25:12 +00:00
{
frameCount = 0;
SVideoInitialize *_pVideoInitialize = (SVideoInitialize*)init;
g_VideoInitialize = *(_pVideoInitialize); // Create a shortcut to _pVideoInitialize that can also update it
2008-12-08 05:25:12 +00:00
InitLUTs();
InitXFBConvTables();
g_Config.Load();
g_Config.GameIniLoad();
2009-06-03 23:38:31 +00:00
g_Config.UpdateProjectionHack();
if (!OpenGL_Create(g_VideoInitialize, 640, 480)) // 640x480 will be the default if all else fails
{
2008-12-08 05:25:12 +00:00
g_VideoInitialize.pLog("Renderer::Create failed\n", TRUE);
return;
}
_pVideoInitialize->pPeekMessages = g_VideoInitialize.pPeekMessages;
2008-12-08 05:25:12 +00:00
_pVideoInitialize->pUpdateFPSDisplay = g_VideoInitialize.pUpdateFPSDisplay;
// Now the window handle is written
2008-12-08 05:25:12 +00:00
_pVideoInitialize->pWindowHandle = g_VideoInitialize.pWindowHandle;
OSD::AddMessage("Dolphin OpenGL Video Plugin" ,5000);
2008-12-08 05:25:12 +00:00
}
void DoState(unsigned char **ptr, int mode) {
#ifndef _WIN32
// WHY is this here??
OpenGL_MakeCurrent();
#endif
2008-12-08 05:25:12 +00:00
// Clear all caches that touch RAM
TextureMngr::Invalidate(false);
2008-12-08 05:25:12 +00:00
VertexLoaderManager::MarkAllDirty();
PointerWrap p(ptr, mode);
VideoCommon_DoState(p);
// Refresh state.
if (mode == PointerWrap::MODE_READ)
{
2009-06-22 09:31:30 +00:00
BPReload();
RecomputeCachedArraybases();
}
2008-12-08 05:25:12 +00:00
}
// This is called after Video_Initialize() from the Core
void Video_Prepare(void)
{
OpenGL_MakeCurrent();
if (!Renderer::Init()) {
g_VideoInitialize.pLog("Renderer::Create failed\n", TRUE);
2008-12-08 05:25:12 +00:00
PanicAlert("Can't create opengl renderer. You might be missing some required opengl extensions, check the logs for more info");
exit(1);
}
TextureMngr::Init();
2009-06-22 09:31:30 +00:00
BPInit();
2008-12-08 05:25:12 +00:00
VertexManager::Init();
Fifo_Init(); // must be done before OpcodeDecoder_Init()
OpcodeDecoder_Init();
VertexShaderCache::Init();
VertexShaderManager::Init();
PixelShaderCache::Init();
PixelShaderManager::Init();
PostProcessing::Init();
2008-12-08 05:25:12 +00:00
GL_REPORT_ERRORD();
VertexLoaderManager::Init();
TextureConverter::Init();
s_PluginInitialized = true;
INFO_LOG(VIDEO, "Video plugin initialized.");
2008-12-08 05:25:12 +00:00
}
void Shutdown(void)
2008-12-08 05:25:12 +00:00
{
s_PluginInitialized = false;
Fifo_Shutdown();
PostProcessing::Shutdown();
TextureConverter::Shutdown();
VertexLoaderManager::Shutdown();
VertexShaderCache::Shutdown();
VertexShaderManager::Shutdown();
PixelShaderManager::Shutdown();
PixelShaderCache::Shutdown();
VertexManager::Shutdown();
TextureMngr::Shutdown();
OpcodeDecoder_Shutdown();
Renderer::Shutdown();
OpenGL_Shutdown();
2008-12-08 05:25:12 +00:00
}
// Enter and exit the video loop
2008-12-08 05:25:12 +00:00
void Video_EnterLoop()
{
Fifo_EnterLoop(g_VideoInitialize);
}
void Video_ExitLoop()
{
Fifo_ExitLoop();
}
// Screenshot and screen message
void Video_Screenshot(const char *_szFilename)
2008-12-08 05:25:12 +00:00
{
Renderer::SetScreenshot(_szFilename);
2008-12-08 05:25:12 +00:00
}
void Video_AddMessage(const char* pstr, u32 milliseconds)
{
OSD::AddMessage(pstr, milliseconds);
}
// TODO: Protect this structure with a mutex.
volatile struct
{
u32 xfbAddr;
u32 width;
u32 height;
s32 yOffset;
} tUpdateXFBArgs;
// Run from the CPU thread (from VideoInterface.cpp) for certain homebrew games only
void Video_UpdateXFB(u32 _dwXFBAddr, u32 _dwWidth, u32 _dwHeight, s32 _dwYOffset, bool scheduling)
2008-12-08 05:25:12 +00:00
{
if (s_PluginInitialized)
2008-12-08 05:25:12 +00:00
{
if (scheduling) // From CPU in DC mode
{
tUpdateXFBArgs.xfbAddr = _dwXFBAddr;
tUpdateXFBArgs.width = _dwWidth;
tUpdateXFBArgs.height = _dwHeight;
tUpdateXFBArgs.yOffset = _dwYOffset;
g_XFBUpdateRequested = TRUE;
}
else // From CPU in SC mode or graphics thread in DC mode
{
g_XFBUpdateRequested = FALSE;
if (!_dwXFBAddr)
{
// From graphics thread in DC mode
_dwXFBAddr = tUpdateXFBArgs.xfbAddr;
_dwWidth = tUpdateXFBArgs.width;
_dwHeight = tUpdateXFBArgs.height;
_dwYOffset = tUpdateXFBArgs.yOffset;
}
// TODO: Use real XFB source parameters based on VI settings
Renderer::Swap(_dwXFBAddr, _dwWidth, _dwHeight, g_Config.bUseXFB ? _dwYOffset : 0);
g_VideoInitialize.pCopiedToXFB();
}
2008-12-08 05:25:12 +00:00
}
}
u32 Video_AccessEFB(EFBAccessType type, u32 x, u32 y)
{
switch (type)
{
case PEEK_Z:
{
if (!g_VideoInitialize.bUseDualCore)
{
u32 z = 0;
TRectangle source, scaledTargetSource;
ComputeBackbufferRectangle(&source);
source.Scale(Renderer::GetTargetScaleX(), Renderer::GetTargetScaleY(), &scaledTargetSource);
GLuint depth_tex = Renderer::ResolveAndGetDepthTarget(scaledTargetSource);
glReadPixels(x, Renderer::GetTargetHeight()-y, 1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &z);
GL_REPORT_ERRORD();
// mask away the stencil bits
return z & 0xffffff;
}
}
break;
case POKE_Z:
break;
case PEEK_COLOR:
break;
case POKE_COLOR:
break;
default:
break;
}
return 0;
}