From f597a66b8dd4ae41a62f58b6115282b9b0b38385 Mon Sep 17 00:00:00 2001 From: hrydgard Date: Sun, 1 Mar 2009 10:53:23 +0000 Subject: [PATCH] Get 2501 closer to "working". git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2502 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Plugin_VideoDX9/Plugin_VideoDX9.vcproj | 11 ++-- .../Plugin_VideoDX9/Src/PixelShaderCache.cpp | 42 ++++++++++----- .../Plugin_VideoDX9/Src/PixelShaderCache.h | 8 +-- Source/Plugins/Plugin_VideoDX9/Src/Render.cpp | 3 -- .../Plugin_VideoDX9/Src/VertexShaderCache.cpp | 52 ++++++++++++------- .../Plugin_VideoDX9/Src/VertexShaderCache.h | 6 +-- Source/Plugins/Plugin_VideoDX9/Src/main.cpp | 2 +- 7 files changed, 73 insertions(+), 51 deletions(-) diff --git a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj index 42225c1295..97305842af 100644 --- a/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj +++ b/Source/Plugins/Plugin_VideoDX9/Plugin_VideoDX9.vcproj @@ -1,7 +1,7 @@ - - + + diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp index 16074e5d5e..b4f0543bc5 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.cpp @@ -35,7 +35,7 @@ PixelShaderCache::PSCache PixelShaderCache::PixelShaders; void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4) { const float f[4] = {f1, f2, f3, f4}; - D3D::dev->SetPixelShaderConstantF(const_number, f, 1); + D3D::dev->SetPixelShaderConstantF(const_number, f, 1); } void SetPSConstant4fv(int const_number, const float *f) @@ -62,7 +62,8 @@ void PixelShaderCache::SetShader() return; // we are screwed static LPDIRECT3DPIXELSHADER9 lastShader = NULL; - DVSTARTPROFILE(); + + DVSTARTPROFILE(); PIXELSHADERUID uid; GetPixelShaderId(uid, PixelShaderManager::GetTextureMask(), false, false); @@ -83,9 +84,7 @@ void PixelShaderCache::SetShader() } const char *code = GeneratePixelShader(PixelShaderManager::GetTextureMask(), false, false); - //LPDIRECT3DPIXELSHADER9 shader = D3D::CompilePixelShader(code, (int)(strlen(code))); LPDIRECT3DPIXELSHADER9 shader = CompileCgShader(code); - if (shader) { //Make an entry in the table @@ -94,6 +93,7 @@ void PixelShaderCache::SetShader() newentry.frameCount = frameCount; PixelShaders[uid] = newentry; + D3D::dev->SetFVF(NULL); D3D::dev->SetPixelShader(shader); INCSTAT(stats.numPixelShadersCreated); @@ -119,18 +119,34 @@ LPDIRECT3DPIXELSHADER9 PixelShaderCache::CompileCgShader(const char *pstrprogram // 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"); - } - - LPDIRECT3DPIXELSHADER9 shader = D3D::CompilePixelShader(pcompiledprog, strlen(pcompiledprog)); + LPD3DXBUFFER shader_binary; + LPD3DXBUFFER error_msg; + // Step one - Assemble into binary code. This binary code could be cached. + if (FAILED(D3DXAssembleShader(pcompiledprog, (UINT)strlen(pcompiledprog), NULL, NULL, 0, &shader_binary, &error_msg))) + PanicAlert("Asm fail"); + // Destroy Cg program as early as possible - we want as little as possible to do with Cg due to + // our rather extreme performance requirements. cgDestroyProgram(tempprog); + tempprog = NULL; - return shader; + // Create pixel shader from the binary code. + LPDIRECT3DPIXELSHADER9 pixel_shader = NULL; + if (SUCCEEDED(D3D::dev->CreatePixelShader((const DWORD *)shader_binary->GetBufferPointer(), &pixel_shader))) { + // PanicAlert("Success Pixel!"); + } else { + if (error_msg) { + PanicAlert("failure pixel %s", error_msg->GetBufferPointer()); + MessageBox(0, pcompiledprog, 0, 0); + } + else + PanicAlert("failure pixel with no error message."); + } + if (shader_binary) + shader_binary->Release(); + if (error_msg) + error_msg->Release(); + return pixel_shader; } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.h b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.h index a4b9ad7719..28fd0b759c 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShaderCache.h @@ -34,13 +34,9 @@ class PixelShaderCache struct PSCacheEntry { LPDIRECT3DPIXELSHADER9 shader; - int frameCount; - PSCacheEntry() - { - shader = 0; - frameCount = 0; - } + + PSCacheEntry() : shader(NULL), frameCount(0) {} void Destroy() { if (shader) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp index 5bab126790..f9340c5e39 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/Render.cpp @@ -93,7 +93,6 @@ void Renderer::Init(SVideoInitialize &_VideoInitialize) cgGetError(); cgSetErrorHandler(HandleCgError, NULL); - cgD3D9SetDevice(D3D::dev); g_cgvProf = cgD3D9GetLatestVertexProfile(); g_cgfProf = cgD3D9GetLatestPixelProfile(); @@ -144,10 +143,8 @@ void Renderer::Initialize() m_Textures.reserve(MaxTextureStages); for (int i = 0; i < MaxTextureStages; i++) m_Textures.push_back(NULL); - for (int i = 0; i < 8; i++) D3D::dev->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 16); - Postprocess::Initialize(); Postprocess::BeginFrame(); D3D::BeginFrame(true, 0); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp index 6f2dda61b4..f013c850d5 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.cpp @@ -61,11 +61,11 @@ void VertexShaderCache::Shutdown() void VertexShaderCache::SetShader(u32 components) { - static LPDIRECT3DVERTEXSHADER9 shader = NULL; if (D3D::GetShaderVersion() < 2) return; // we are screwed static LPDIRECT3DVERTEXSHADER9 lastShader = NULL; + DVSTARTPROFILE(); VERTEXSHADERUID uid; @@ -73,7 +73,6 @@ void VertexShaderCache::SetShader(u32 components) VSCache::iterator iter; iter = vshaders.find(uid); - if (iter != vshaders.end()) { iter->second.frameCount = frameCount; @@ -87,11 +86,10 @@ void VertexShaderCache::SetShader(u32 components) } const char *code = GenerateVertexShader(components, false); - //shader = D3D::CompileVertexShader(code, (int)strlen(code)); - shader = CompileCgShader(code); + LPDIRECT3DVERTEXSHADER9 shader = CompileCgShader(code); if (shader) { - //Make an entry in the table + // Make an entry in the table VSCacheEntry entry; entry.shader = shader; entry.frameCount = frameCount; @@ -101,8 +99,12 @@ void VertexShaderCache::SetShader(u32 components) INCSTAT(stats.numVertexShadersCreated); SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size()); - } else + } else { PanicAlert("Failed to compile Vertex Shader:\n\n%s", code); + } + + D3D::dev->SetFVF(NULL); + D3D::dev->SetVertexShader(shader); } LPDIRECT3DVERTEXSHADER9 VertexShaderCache::CompileCgShader(const char *pstrprogram) @@ -117,22 +119,36 @@ LPDIRECT3DVERTEXSHADER9 VertexShaderCache::CompileCgShader(const char *pstrprogr ERROR_LOG(VIDEO, pstrprogram); return NULL; } + const char *pcompiledprog = cgGetProgramString(tempprog, CG_COMPILED_PROGRAM); - // 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"); - } - - LPDIRECT3DVERTEXSHADER9 shader = D3D::CompileVertexShader(pcompiledprog, strlen(pcompiledprog)); + LPD3DXBUFFER shader_binary; + LPD3DXBUFFER error_msg; + // Step one - Assemble into binary code. This binary code could be cached. + if (FAILED(D3DXAssembleShader(pcompiledprog, (UINT)strlen(pcompiledprog), NULL, NULL, 0, &shader_binary, &error_msg))) + PanicAlert("Asm fail"); + // Destroy Cg program as early as possible - we want as little as possible to do with Cg due to + // our rather extreme performance requirements. cgDestroyProgram(tempprog); + tempprog = NULL; - return shader; + // Create vertex shader from the binary code. + LPDIRECT3DVERTEXSHADER9 vertex_shader = NULL; + if (SUCCEEDED(D3D::dev->CreateVertexShader((const DWORD *)shader_binary->GetBufferPointer(), &vertex_shader))) { + // PanicAlert("Successvertex!"); + } else { + if (error_msg) { + PanicAlert("failure vertex %s", error_msg->GetBufferPointer()); + MessageBox(0, pcompiledprog, 0, 0); + } + else + PanicAlert("failure vertex with no error message."); + } + if (shader_binary) + shader_binary->Release(); + if (error_msg) + error_msg->Release(); + return vertex_shader; } void VertexShaderCache::Cleanup() diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.h b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.h index 0131d7947e..13b4cda9cf 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexShaderCache.h @@ -31,11 +31,7 @@ class VertexShaderCache { LPDIRECT3DVERTEXSHADER9 shader; int frameCount; - VSCacheEntry() - { - shader = 0; - frameCount = 0; - } + VSCacheEntry() : shader(NULL), frameCount(0) {} void Destroy() { if (shader) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp index 234afce7aa..f7190e250f 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp @@ -315,7 +315,7 @@ HRESULT ScreenShot(const char *File) void Video_Screenshot(const char *_szFilename) { - if(ScreenShot(_szFilename) != S_OK) + if (ScreenShot(_szFilename) != S_OK) PanicAlert("Error while capturing screen"); else { std::string message = "Saved ";