Direct3D + Cg progress, the plugin is still not working due to some missing features.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2494 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
XTra.KrazzY
2009-03-01 01:09:32 +00:00
parent e004800e68
commit 3234bca193
13 changed files with 719 additions and 26 deletions

View File

@ -27,6 +27,9 @@
#include "BPMemory.h"
#include "XFMemory.h"
#include <Cg/cg.h>
#include <Cg/cgD3D9.h>
PixelShaderCache::PSCache PixelShaderCache::PixelShaders;
void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4)
@ -58,7 +61,8 @@ void PixelShaderCache::SetShader()
if (D3D::GetShaderVersion() < 2)
return; // we are screwed
static LPDIRECT3DPIXELSHADER9 lastShader = 0;
//static LPDIRECT3DPIXELSHADER9 lastShader = 0;
static CGprogram lastShader = NULL;
DVSTARTPROFILE();
PIXELSHADERUID uid;
@ -73,14 +77,17 @@ void PixelShaderCache::SetShader()
PSCacheEntry &entry = iter->second;
if (!lastShader || entry.shader != lastShader)
{
D3D::dev->SetPixelShader(entry.shader);
//D3D::dev->SetPixelShader(entry.shader);
cgD3D9LoadProgram(entry.shader, false, 0);
cgD3D9BindProgram(entry.shader);
lastShader = entry.shader;
}
return;
}
const char *code = GeneratePixelShader(PixelShaderManager::GetTextureMask(), false, false);
LPDIRECT3DPIXELSHADER9 shader = D3D::CompilePixelShader(code, (int)(strlen(code)));
//LPDIRECT3DPIXELSHADER9 shader = D3D::CompilePixelShader(code, (int)(strlen(code)));
CGprogram shader = CompileCgShader(code);
if (shader)
{
//Make an entry in the table
@ -89,13 +96,48 @@ void PixelShaderCache::SetShader()
newentry.frameCount = frameCount;
PixelShaders[uid] = newentry;
// There seems to be an unknown Cg error here for some reason
///PanicAlert("Load pShader");
cgD3D9LoadProgram(shader, false, 0);
cgD3D9BindProgram(shader);
///PanicAlert("Loaded pShader");
INCSTAT(stats.numPixelShadersCreated);
SETSTAT(stats.numPixelShadersAlive, (int)PixelShaders.size());
} else
PanicAlert("Failed to compile Pixel Shader:\n\n%s", code);
//D3D::dev->SetPixelShader(shader);
}
CGprogram PixelShaderCache::CompileCgShader(const char *pstrprogram)
{
char stropt[64];
//sprintf(stropt, "MaxLocalParams=256,MaxInstructions=%d", s_nMaxVertexInstructions);
//const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL};
const char **opts = cgD3D9GetOptimalOptions(g_cgvProf);
CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgfProf, "main", opts);
if (!cgIsProgram(tempprog) || cgGetError() != CG_NO_ERROR) {
ERROR_LOG(VIDEO, "Failed to create ps %s:\n", cgGetLastListing(g_cgcontext));
ERROR_LOG(VIDEO, pstrprogram);
return false;
}
D3D::dev->SetPixelShader(shader);
// This looks evil - we modify the program through the const char * we got from cgGetProgramString!
// It SHOULD not have any nasty side effects though - but you never know...
char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM);
char *plocal = strstr(pcompiledprog, "program.local");
while (plocal != NULL) {
const char *penv = " program.env";
memcpy(plocal, penv, 13);
plocal = strstr(plocal+13, "program.local");
}
return tempprog;
}
void PixelShaderCache::Cleanup()
{
PSCache::iterator iter;

View File

@ -25,6 +25,9 @@
#include "PixelShaderGen.h"
#include "VertexShaderGen.h"
#include <Cg/cg.h>
#include <Cg/cgD3D9.h>
typedef u32 tevhash;
tevhash GetCurrentTEV();
@ -33,7 +36,8 @@ class PixelShaderCache
{
struct PSCacheEntry
{
LPDIRECT3DPIXELSHADER9 shader;
//LPDIRECT3DPIXELSHADER9 shader;
CGprogram shader;
int frameCount;
PSCacheEntry()
@ -43,8 +47,12 @@ class PixelShaderCache
}
void Destroy()
{
if (shader)
shader->Release();
if (shader) {
cgD3D9UnloadProgram(shader);
cgDestroyProgram(shader);
// shader->Release();
}
}
};
@ -57,6 +65,7 @@ public:
static void Cleanup();
static void Shutdown();
static void SetShader();
static CGprogram CompileCgShader(const char *pstrprogram);
};

View File

@ -38,6 +38,13 @@
#include "Utils.h"
#include "EmuWindow.h"
#include <Cg/cg.h>
#include <Cg/cgD3D9.h>
CGcontext g_cgcontext;
CGprofile g_cgvProf;
CGprofile g_cgfProf;
float Renderer::m_x;
float Renderer::m_y;
float Renderer::m_width;
@ -64,6 +71,15 @@ struct Message
static std::list<Message> s_listMsgs;
void HandleCgError(CGcontext ctx, CGerror err, void* appdata)
{
PanicAlert("Cg error: %s\n", cgGetErrorString(err));
const char* listing = cgGetLastListing(g_cgcontext);
if (listing != NULL) {
PanicAlert("last listing: %s\n", listing);
}
}
void Renderer::Init(SVideoInitialize &_VideoInitialize)
{
EmuWindow::SetSize(g_Res[g_Config.iWindowedRes][0], g_Res[g_Config.iWindowedRes][1]);
@ -73,6 +89,15 @@ void Renderer::Init(SVideoInitialize &_VideoInitialize)
D3DVIEWPORT9 vp;
D3D::dev->GetViewport(&vp);
g_cgcontext = cgCreateContext();
cgGetError();
cgSetErrorHandler(HandleCgError, NULL);
cgD3D9SetDevice(D3D::dev);
g_cgvProf = cgD3D9GetLatestVertexProfile();
g_cgfProf = cgD3D9GetLatestPixelProfile();
m_x = 0;
m_y = 0;
m_width = (float)vp.Width;
@ -104,6 +129,10 @@ void Renderer::Init(SVideoInitialize &_VideoInitialize)
void Renderer::Shutdown()
{
cgD3D9SetDevice(NULL);
cgDestroyContext(g_cgcontext);
g_cgcontext = NULL;
D3D::font.Shutdown();
D3D::EndFrame();
D3D::Close();

View File

@ -24,6 +24,11 @@
#include "pluginspecs_video.h"
#include "D3DBase.h"
#include <Cg/cg.h>
extern CGcontext g_cgcontext;
extern CGprofile g_cgvProf, g_cgfProf;
class Renderer
{
// screen offset

View File

@ -27,6 +27,9 @@
#include "BPMemory.h"
#include "XFMemory.h"
#include <Cg/cg.h>
#include <Cg/cgD3D9.h>
VertexShaderCache::VSCache VertexShaderCache::vshaders;
void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
@ -58,11 +61,11 @@ void VertexShaderCache::Shutdown()
void VertexShaderCache::SetShader(u32 components)
{
static LPDIRECT3DVERTEXSHADER9 shader = NULL;
static CGprogram shader = NULL;
if (D3D::GetShaderVersion() < 2)
return; // we are screwed
static LPDIRECT3DVERTEXSHADER9 lastShader = 0;
static CGprogram lastShader = NULL;
DVSTARTPROFILE();
VERTEXSHADERUID uid;
@ -77,14 +80,17 @@ void VertexShaderCache::SetShader(u32 components)
VSCacheEntry &entry = iter->second;
if (!lastShader || entry.shader != lastShader)
{
D3D::dev->SetVertexShader(entry.shader);
//D3D::dev->SetVertexShader(entry.shader);
cgD3D9LoadProgram(entry.shader, false, 0);
cgD3D9BindProgram(entry.shader);
lastShader = entry.shader;
}
return;
}
const char *code = GenerateVertexShader(components, false);
shader = D3D::CompileVertexShader(code, (int)strlen(code));
//shader = D3D::CompileVertexShader(code, (int)strlen(code));
shader = CompileCgShader(code);
if (shader)
{
//Make an entry in the table
@ -92,12 +98,45 @@ void VertexShaderCache::SetShader(u32 components)
entry.shader = shader;
entry.frameCount = frameCount;
vshaders[uid] = entry;
// There seems to be an unknown Cg error here for some reason
///PanicAlert("Load vShader");
cgD3D9LoadProgram(shader, false, 0);
cgD3D9BindProgram(shader);
///PanicAlert("Loaded vShader");
INCSTAT(stats.numVertexShadersCreated);
SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size());
} else
PanicAlert("Failed to compile Vertex Shader:\n\n%s", code);
//D3D::dev->SetVertexShader(shader);
}
CGprogram VertexShaderCache::CompileCgShader(const char *pstrprogram)
{
char stropt[64];
//sprintf(stropt, "MaxLocalParams=256,MaxInstructions=%d", s_nMaxVertexInstructions);
//const char *opts[] = {"-profileopts", stropt, "-O2", "-q", NULL};
const char **opts = cgD3D9GetOptimalOptions(g_cgvProf);
CGprogram tempprog = cgCreateProgram(g_cgcontext, CG_SOURCE, pstrprogram, g_cgvProf, "main", opts);
if (!cgIsProgram(tempprog) || cgGetError() != CG_NO_ERROR) {
ERROR_LOG(VIDEO, "Failed to load vs %s:\n", cgGetLastListing(g_cgcontext));
ERROR_LOG(VIDEO, pstrprogram);
return NULL;
}
D3D::dev->SetVertexShader(shader);
// This looks evil - we modify the program through the const char * we got from cgGetProgramString!
// It SHOULD not have any nasty side effects though - but you never know...
char *pcompiledprog = (char*)cgGetProgramString(tempprog, CG_COMPILED_PROGRAM);
char *plocal = strstr(pcompiledprog, "program.local");
while (plocal != NULL) {
const char* penv = " program.env";
memcpy(plocal, penv, 13);
plocal = strstr(plocal + 13, "program.local");
}
INCSTAT(stats.numVertexShadersCreated);
SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size());
return tempprog;
}
void VertexShaderCache::Cleanup()

View File

@ -25,11 +25,15 @@
#include "D3DBase.h"
#include "VertexShaderGen.h"
#include <Cg/cg.h>
#include <Cg/cgD3D9.h>
class VertexShaderCache
{
struct VSCacheEntry
{
LPDIRECT3DVERTEXSHADER9 shader;
//LPDIRECT3DVERTEXSHADER9 shader;
CGprogram shader;
int frameCount;
VSCacheEntry()
{
@ -38,8 +42,11 @@ class VertexShaderCache
}
void Destroy()
{
if (shader)
shader->Release();
if (shader) {
cgD3D9UnloadProgram(shader);
cgDestroyProgram(shader);
// shader->Release();
}
}
};
@ -52,6 +59,7 @@ public:
static void Cleanup();
static void Shutdown();
static void SetShader(u32 components);
static CGprogram CompileCgShader(const char *pstrprogram);
};
#endif // _VERTEXSHADERCACHE_H