Fix a vertex shader generation bug that crashed Super Monkey Ball. Better shader error handling. Random warning fixes.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4170 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard
2009-09-02 21:19:35 +00:00
parent 50593397f3
commit 1e016dd522
8 changed files with 47 additions and 61 deletions

View File

@ -110,19 +110,19 @@ void ConsoleListener::BufferWidthHeight(int BufferWidth, int BufferHeight, int S
{ {
// Change screen buffer size // Change screen buffer size
COORD Co = {BufferWidth, BufferHeight}; COORD Co = {BufferWidth, BufferHeight};
SB = SetConsoleScreenBufferSize(hConsole, Co); SB = (bool)SetConsoleScreenBufferSize(hConsole, Co);
// Change the screen buffer window size // Change the screen buffer window size
SMALL_RECT coo = {0,0,ScreenWidth, ScreenHeight}; // top, left, right, bottom SMALL_RECT coo = {0,0,ScreenWidth, ScreenHeight}; // top, left, right, bottom
SW = SetConsoleWindowInfo(hConsole, TRUE, &coo); SW = (bool)SetConsoleWindowInfo(hConsole, TRUE, &coo);
} }
else else
{ {
// Change the screen buffer window size // Change the screen buffer window size
SMALL_RECT coo = {0,0, ScreenWidth, ScreenHeight}; // top, left, right, bottom SMALL_RECT coo = {0,0, ScreenWidth, ScreenHeight}; // top, left, right, bottom
SW = SetConsoleWindowInfo(hConsole, TRUE, &coo); SW = (bool)SetConsoleWindowInfo(hConsole, TRUE, &coo);
// Change screen buffer size // Change screen buffer size
COORD Co = {BufferWidth, BufferHeight}; COORD Co = {BufferWidth, BufferHeight};
SB = SetConsoleScreenBufferSize(hConsole, Co); SB = (bool)SetConsoleScreenBufferSize(hConsole, Co);
} }
#endif #endif
} }
@ -198,7 +198,7 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool
int BytesRead = 0; int BytesRead = 0;
int i = 0; int i = 0;
int LastAttrRead = 0; int LastAttrRead = 0;
while(BytesRead < BufferSize) while (BytesRead < BufferSize)
{ {
Str.push_back(new char[MAX_BYTES]); Str.push_back(new char[MAX_BYTES]);
if (!ReadConsoleOutputCharacter(hConsole, Str[i], ReadBufferSize, coordScreen, &cCharsRead)) if (!ReadConsoleOutputCharacter(hConsole, Str[i], ReadBufferSize, coordScreen, &cCharsRead))
@ -215,8 +215,8 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool
LastAttrRead = cAttrRead; LastAttrRead = cAttrRead;
} }
// Letter space // Letter space
int LWidth = floor((float)Width / 8.0) - 1.0; int LWidth = (int)floor((float)Width / 8.0) - 1.0;
int LHeight = floor((float)Height / 12.0) - 1.0; int LHeight = (int)floor((float)Height / 12.0) - 1.0;
int LBufWidth = LWidth + 1; int LBufWidth = LWidth + 1;
int LBufHeight = floor((float)BufferSize / (float)LBufWidth); int LBufHeight = floor((float)BufferSize / (float)LBufWidth);
// Change screen buffer size // Change screen buffer size

View File

@ -660,13 +660,13 @@ void Callback_VideoCopiedToXFB(bool video_update)
double wait_frametime = (1000.0 / VideoInterface::TargetRefreshRate); double wait_frametime = (1000.0 / VideoInterface::TargetRefreshRate);
if (Timer.GetTimeDifference() >= wait_frametime * frames) if (Timer.GetTimeDifference() >= wait_frametime * frames)
no_framelimit=Timer.GetTimeDifference(); no_framelimit = (u32)Timer.GetTimeDifference();
while (Timer.GetTimeDifference() < wait_frametime * videoupd) while (Timer.GetTimeDifference() < wait_frametime * videoupd)
{ {
// TODO : This is wrong, the sleep shouldn't be there but rather in cputhread // TODO : This is wrong, the sleep shouldn't be there but rather in cputhread
// as it's not based on the fps but on the refresh rate... // as it's not based on the fps but on the refresh rate...
if (no_framelimit==0) if (no_framelimit == 0)
Common::SleepCurrentThread(1); Common::SleepCurrentThread(1);
} }
} }

View File

@ -336,7 +336,7 @@ const char *GenerateVertexShader(u32 components, bool D3D)
_assert_( texinfo.inputform == XF_TEXINPUT_ABC1 ); _assert_( texinfo.inputform == XF_TEXINPUT_ABC1 );
WRITE(p, "float4 coord = float4(rawnorm0.xyz, 1.0);\n"); WRITE(p, "float4 coord = float4(rawnorm0.xyz, 1.0);\n");
} }
else WRITE(p, "float4 coord = 0;\n"); else WRITE(p, "float4 coord = float4(0.0f, 0.0f, 1.0f, 1.0f);\n"); // avoid errors
break; break;
case XF_SRCCOLORS_INROW: case XF_SRCCOLORS_INROW:
_assert_( texinfo.texgentype == XF_TEXGEN_COLOR_STRGBC0 || texinfo.texgentype == XF_TEXGEN_COLOR_STRGBC1 ); _assert_( texinfo.texgentype == XF_TEXGEN_COLOR_STRGBC0 || texinfo.texgentype == XF_TEXGEN_COLOR_STRGBC1 );
@ -346,21 +346,21 @@ const char *GenerateVertexShader(u32 components, bool D3D)
_assert_( texinfo.inputform == XF_TEXINPUT_ABC1 ); _assert_( texinfo.inputform == XF_TEXINPUT_ABC1 );
WRITE(p, "float4 coord = float4(rawnorm1.xyz, 1.0);\n"); WRITE(p, "float4 coord = float4(rawnorm1.xyz, 1.0);\n");
} }
else WRITE(p, "float4 coord = 0;\n"); else WRITE(p, "float4 coord = float4(0.0f, 0.0f, 1.0f, 1.0f);\n"); // avoid errors
break; break;
case XF_SRCBINORMAL_B_INROW: case XF_SRCBINORMAL_B_INROW:
if (components & VB_HAS_NRM2) { if (components & VB_HAS_NRM2) {
_assert_( texinfo.inputform == XF_TEXINPUT_ABC1 ); _assert_( texinfo.inputform == XF_TEXINPUT_ABC1 );
WRITE(p, "float4 coord = float4(rawnorm2.xyz, 1.0);\n"); WRITE(p, "float4 coord = float4(rawnorm2.xyz, 1.0);\n");
} }
else WRITE(p, "float4 coord = 0;\n"); else WRITE(p, "float4 coord = float4(0.0f, 0.0f, 1.0f, 1.0f);\n"); // avoid errors
break; break;
default: default:
_assert_(texinfo.sourcerow <= XF_SRCTEX7_INROW); _assert_(texinfo.sourcerow <= XF_SRCTEX7_INROW);
if (components & (VB_HAS_UV0<<(texinfo.sourcerow - XF_SRCTEX0_INROW)) ) if (components & (VB_HAS_UV0<<(texinfo.sourcerow - XF_SRCTEX0_INROW)) )
WRITE(p, "float4 coord = float4(tex%d.x, tex%d.y, 1.0f, 1.0f);\n", texinfo.sourcerow - XF_SRCTEX0_INROW, texinfo.sourcerow - XF_SRCTEX0_INROW); WRITE(p, "float4 coord = float4(tex%d.x, tex%d.y, 1.0f, 1.0f);\n", texinfo.sourcerow - XF_SRCTEX0_INROW, texinfo.sourcerow - XF_SRCTEX0_INROW);
else else
WRITE(p, "float4 coord = float4(0.0f, 0.0f, 1.0f, 1.0f);\n"); WRITE(p, "float4 coord = float4(0.0f, 0.0f, 1.0f, 1.0f);\n"); // avoid errors
break; break;
} }

View File

@ -57,10 +57,8 @@ void PixelShaderCache::Shutdown()
PixelShaders.clear(); PixelShaders.clear();
} }
void PixelShaderCache::SetShader(bool dstAlpha) bool PixelShaderCache::SetShader(bool dstAlpha)
{ {
static LPDIRECT3DPIXELSHADER9 last_shader = NULL;
DVSTARTPROFILE(); DVSTARTPROFILE();
PIXELSHADERUID uid; PIXELSHADERUID uid;
@ -68,19 +66,14 @@ void PixelShaderCache::SetShader(bool dstAlpha)
PSCache::iterator iter; PSCache::iterator iter;
iter = PixelShaders.find(uid); iter = PixelShaders.find(uid);
if (iter != PixelShaders.end()) if (iter != PixelShaders.end())
{ {
iter->second.frameCount = frameCount; iter->second.frameCount = frameCount;
const PSCacheEntry &entry = iter->second; const PSCacheEntry &entry = iter->second;
D3D::dev->SetPixelShader(entry.shader);
last_entry = &entry; last_entry = &entry;
if (!last_shader || entry.shader != last_shader) DEBUGGER_PAUSE_COUNT_N(NEXT_PIXEL_SHADER_CHANGE);
{ return true;
D3D::dev->SetPixelShader(entry.shader);
last_shader = entry.shader;
DEBUGGER_PAUSE_COUNT_N(NEXT_PIXEL_SHADER_CHANGE);
}
return;
} }
const char *code = GeneratePixelShader(PixelShaderManager::GetTextureMask(), dstAlpha, true); const char *code = GeneratePixelShader(PixelShaderManager::GetTextureMask(), dstAlpha, true);
@ -88,26 +81,27 @@ void PixelShaderCache::SetShader(bool dstAlpha)
if (shader) if (shader)
{ {
//Make an entry in the table // Make an entry in the table
PSCacheEntry newentry; PSCacheEntry newentry;
newentry.shader = shader; newentry.shader = shader;
newentry.frameCount = frameCount; newentry.frameCount = frameCount;
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
newentry.code = code; newentry.code = code;
#endif #endif
PixelShaders[uid] = newentry; PixelShaders[uid] = newentry;
last_entry = &PixelShaders[uid]; last_entry = &PixelShaders[uid];
D3D::dev->SetPixelShader(shader); D3D::dev->SetPixelShader(shader);
last_shader = shader;
INCSTAT(stats.numPixelShadersCreated); INCSTAT(stats.numPixelShadersCreated);
SETSTAT(stats.numPixelShadersAlive, (int)PixelShaders.size()); SETSTAT(stats.numPixelShadersAlive, (int)PixelShaders.size());
return true;
} }
else if (g_Config.bShowShaderErrors) else if (g_Config.bShowShaderErrors)
{ {
PanicAlert("Failed to compile Pixel Shader:\n\n%s", code); PanicAlert("Failed to compile Pixel Shader:\n\n%s", code);
} }
return false;
} }
@ -131,7 +125,7 @@ void PixelShaderCache::Cleanup()
SETSTAT(stats.numPixelShadersAlive, (int)PixelShaders.size()); SETSTAT(stats.numPixelShadersAlive, (int)PixelShaders.size());
} }
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string PixelShaderCache::GetCurrentShaderCode() std::string PixelShaderCache::GetCurrentShaderCode()
{ {
if (last_entry) if (last_entry)

View File

@ -36,7 +36,7 @@ private:
{ {
LPDIRECT3DPIXELSHADER9 shader; LPDIRECT3DPIXELSHADER9 shader;
int frameCount; int frameCount;
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string code; std::string code;
#endif #endif
PSCacheEntry() : shader(NULL), frameCount(0) {} PSCacheEntry() : shader(NULL), frameCount(0) {}
@ -56,8 +56,8 @@ public:
static void Init(); static void Init();
static void Cleanup(); static void Cleanup();
static void Shutdown(); static void Shutdown();
static void SetShader(bool dstAlpha); static bool SetShader(bool dstAlpha);
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
static std::string GetCurrentShaderCode(); static std::string GetCurrentShaderCode();
#endif #endif
static LPDIRECT3DPIXELSHADER9 CompileCgShader(const char *pstrprogram); static LPDIRECT3DPIXELSHADER9 CompileCgShader(const char *pstrprogram);

View File

@ -185,10 +185,6 @@ void Flush()
DVSTARTPROFILE(); DVSTARTPROFILE();
if (collection != C_NOTHING) if (collection != C_NOTHING)
{ {
// setup the pointers
if (g_nativeVertexFmt)
g_nativeVertexFmt->SetupVertexPointers();
u32 usedtextures = 0; u32 usedtextures = 0;
for (u32 i = 0; i < (u32)bpmem.genMode.numtevstages + 1; ++i) { for (u32 i = 0; i < (u32)bpmem.genMode.numtevstages + 1; ++i) {
if (bpmem.tevorders[i/2].getEnable(i & 1)) if (bpmem.tevorders[i/2].getEnable(i & 1))
@ -230,8 +226,10 @@ void Flush()
VertexShaderManager::SetConstants(); VertexShaderManager::SetConstants();
PixelShaderManager::SetConstants(); PixelShaderManager::SetConstants();
PixelShaderCache::SetShader(false); if (!PixelShaderCache::SetShader(false))
VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); goto shader_fail;
if (!VertexShaderCache::SetShader(g_nativeVertexFmt->m_components))
goto shader_fail;
int stride = g_nativeVertexFmt->GetVertexStride(); int stride = g_nativeVertexFmt->GetVertexStride();
g_nativeVertexFmt->SetupVertexPointers(); g_nativeVertexFmt->SetupVertexPointers();
@ -245,12 +243,12 @@ void Flush()
D3DFMT_INDEX16, D3DFMT_INDEX16,
fakeVBuffer, fakeVBuffer,
stride))) { stride))) {
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string error_shaders; std::string error_shaders;
error_shaders.append(VertexShaderCache::GetCurrentShaderCode()); error_shaders.append(VertexShaderCache::GetCurrentShaderCode());
error_shaders.append(PixelShaderCache::GetCurrentShaderCode()); error_shaders.append(PixelShaderCache::GetCurrentShaderCode());
File::WriteStringToFile(true, error_shaders, "bad_shader_combo.txt"); File::WriteStringToFile(true, error_shaders, "bad_shader_combo.txt");
PanicAlert("DrawIndexedPrimitiveUP failed. Shaders written to shaders.txt."); PanicAlert("DrawIndexedPrimitiveUP failed. Shaders written to bad_shader_combo.txt.");
#endif #endif
} }
} }
@ -263,7 +261,8 @@ void Flush()
if (bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate) if (bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate)
{ {
DWORD write = 0; DWORD write = 0;
PixelShaderCache::SetShader(true); if (!PixelShaderCache::SetShader(true))
goto shader_fail;
// update alpha only // update alpha only
Renderer::SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA); Renderer::SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA);
@ -280,12 +279,12 @@ void Flush()
D3DFMT_INDEX16, D3DFMT_INDEX16,
fakeVBuffer, fakeVBuffer,
stride))) { stride))) {
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string error_shaders; std::string error_shaders;
error_shaders.append(VertexShaderCache::GetCurrentShaderCode()); error_shaders.append(VertexShaderCache::GetCurrentShaderCode());
error_shaders.append(PixelShaderCache::GetCurrentShaderCode()); error_shaders.append(PixelShaderCache::GetCurrentShaderCode());
File::WriteStringToFile(true, error_shaders, "bad_shader_combo.txt"); File::WriteStringToFile(true, error_shaders, "bad_shader_combo.txt");
PanicAlert("DrawIndexedPrimitiveUP failed. Shaders written to shaders.txt."); PanicAlert("DrawIndexedPrimitiveUP failed (dstalpha). Shaders written to bad_shader_combo.txt.");
#endif #endif
} }
} }
@ -309,7 +308,7 @@ void Flush()
INCSTAT(stats.thisFrame.numDrawCalls); INCSTAT(stats.thisFrame.numDrawCalls);
} }
shader_fail:
collection = C_NOTHING; collection = C_NOTHING;
VertexManager::s_pCurBufferPointer = fakeVBuffer; VertexManager::s_pCurBufferPointer = fakeVBuffer;
DEBUGGER_PAUSE_COUNT_N(NEXT_FLUSH); DEBUGGER_PAUSE_COUNT_N(NEXT_FLUSH);

View File

@ -57,11 +57,8 @@ void VertexShaderCache::Shutdown()
vshaders.clear(); vshaders.clear();
} }
bool VertexShaderCache::SetShader(u32 components)
void VertexShaderCache::SetShader(u32 components)
{ {
static LPDIRECT3DVERTEXSHADER9 last_shader = NULL;
DVSTARTPROFILE(); DVSTARTPROFILE();
VERTEXSHADERUID uid; VERTEXSHADERUID uid;
@ -73,15 +70,10 @@ void VertexShaderCache::SetShader(u32 components)
{ {
iter->second.frameCount = frameCount; iter->second.frameCount = frameCount;
const VSCacheEntry &entry = iter->second; const VSCacheEntry &entry = iter->second;
D3D::dev->SetVertexShader(entry.shader);
last_entry = &entry; last_entry = &entry;
if (!last_shader || entry.shader != last_shader) DEBUGGER_PAUSE_COUNT_N(NEXT_VERTEX_SHADER_CHANGE);
{ return true;
D3D::dev->SetVertexShader(entry.shader);
last_shader = entry.shader;
DEBUGGER_PAUSE_COUNT_N(NEXT_VERTEX_SHADER_CHANGE);
}
return;
} }
const char *code = GenerateVertexShader(components, true); const char *code = GenerateVertexShader(components, true);
@ -93,22 +85,23 @@ void VertexShaderCache::SetShader(u32 components)
VSCacheEntry entry; VSCacheEntry entry;
entry.shader = shader; entry.shader = shader;
entry.frameCount = frameCount; entry.frameCount = frameCount;
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
entry.code = code; entry.code = code;
#endif #endif
vshaders[uid] = entry; vshaders[uid] = entry;
last_entry = &vshaders[uid]; last_entry = &vshaders[uid];
last_shader = entry.shader;
D3D::dev->SetVertexShader(shader); D3D::dev->SetVertexShader(shader);
INCSTAT(stats.numVertexShadersCreated); INCSTAT(stats.numVertexShadersCreated);
SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size()); SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size());
return true;
} }
else if (g_Config.bShowShaderErrors) else if (g_Config.bShowShaderErrors)
{ {
PanicAlert("Failed to compile Vertex Shader:\n\n%s", code); PanicAlert("Failed to compile Vertex Shader:\n\n%s", code);
} }
return false;
} }
void VertexShaderCache::Cleanup() void VertexShaderCache::Cleanup()
@ -129,7 +122,7 @@ void VertexShaderCache::Cleanup()
SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size()); SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size());
} }
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string VertexShaderCache::GetCurrentShaderCode() std::string VertexShaderCache::GetCurrentShaderCode()
{ {
if (last_entry) if (last_entry)

View File

@ -33,7 +33,7 @@ private:
{ {
LPDIRECT3DVERTEXSHADER9 shader; LPDIRECT3DVERTEXSHADER9 shader;
int frameCount; int frameCount;
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
std::string code; std::string code;
#endif #endif
VSCacheEntry() : shader(NULL), frameCount(0) {} VSCacheEntry() : shader(NULL), frameCount(0) {}
@ -53,8 +53,8 @@ public:
static void Init(); static void Init();
static void Cleanup(); static void Cleanup();
static void Shutdown(); static void Shutdown();
static void SetShader(u32 components); static bool SetShader(u32 components);
#ifdef _DEBUG #if defined(_DEBUG) || defined(DEBUGFAST)
static std::string GetCurrentShaderCode(); static std::string GetCurrentShaderCode();
#endif #endif
static LPDIRECT3DVERTEXSHADER9 CompileCgShader(const char *pstrprogram); static LPDIRECT3DVERTEXSHADER9 CompileCgShader(const char *pstrprogram);