Skip to content

Commit

Permalink
Merge in change from DirectXTK branch to fix threading bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
bo3b committed Mar 24, 2015
1 parent 3ad8a41 commit 1a107cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions DirectX11/Direct3D11Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ STDMETHODIMP_(ULONG) D3D11Wrapper::ID3D11Device::Release(THIS)

for (PreloadPixelShaderMap::iterator i = G->mPreloadedPixelShaders.begin(); i != G->mPreloadedPixelShaders.end(); ++i)
i->second->Release();
G->mPreloadedPixelShaders.clear();
G->mPreloadedPixelShaders.clear(); // No critical wrap for exiting.
}
if (!G->mPreloadedVertexShaders.empty())
{
LogInfo(" releasing preloaded vertex shaders\n");

for (PreloadVertexShaderMap::iterator i = G->mPreloadedVertexShaders.begin(); i != G->mPreloadedVertexShaders.end(); ++i)
i->second->Release();
G->mPreloadedVertexShaders.clear();
G->mPreloadedVertexShaders.clear(); // No critical wrap for exiting.
}
delete this;
return 0L;
Expand Down Expand Up @@ -252,6 +252,8 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreateTexture1D(THIS_
// ShaderModel is usually something like "vs_5_0", but "bin" is a valid ShaderModel string, and tells the
// reloader to disassemble the .bin file to determine the shader model.

// Currently, critical lock must be taken BEFORE this is called.

static void RegisterForReload(D3D11Base::ID3D11DeviceChild* ppShader,
UINT64 hash, wstring shaderType, string shaderModel, D3D11Base::ID3D11ClassLinkage* pClassLinkage, D3D11Base::ID3DBlob* byteCode, FILETIME timeStamp)
{
Expand Down Expand Up @@ -1312,12 +1314,8 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreateVertexShader(THIS_
if (hr != S_OK && ppVertexShader && pShaderBytecode)
{
D3D11Base::ID3D11VertexShader *zeroShader = 0;
// Not sure why, but blocking the Decompiler from multi-threading prevents a crash.
// This is just a patch for now.
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
char *replaceShader = ReplaceShader(GetD3D11Device(), hash, L"vs", pShaderBytecode, BytecodeLength, replaceShaderSize,
shaderModel, ftWrite, (void **)&zeroShader);
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
if (replaceShader)
{
// Create the new shader.
Expand All @@ -1334,7 +1332,9 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreateVertexShader(THIS_
D3D11Base::ID3DBlob* blob;
D3DCreateBlob(replaceShaderSize, &blob);
memcpy(blob->GetBufferPointer(), replaceShader, replaceShaderSize);
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
RegisterForReload(*ppVertexShader, hash, L"vs", shaderModel, pClassLinkage, blob, ftWrite);
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
}
KeepOriginalShader(this, hash, *ppVertexShader, NULL, pShaderBytecode, BytecodeLength, pClassLinkage);
}
Expand All @@ -1353,6 +1353,7 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreateVertexShader(THIS_
// have a copy for every shader seen.
if (G->hunting)
{
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
D3D11Base::ID3DBlob* blob;
D3DCreateBlob(BytecodeLength, &blob);
memcpy(blob->GetBufferPointer(), pShaderBytecode, blob->GetBufferSize());
Expand All @@ -1363,6 +1364,7 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreateVertexShader(THIS_
// original and depth buffer filtering will work:
if (G->mOriginalVertexShaders.count(*ppVertexShader) == 0)
G->mOriginalVertexShaders[*ppVertexShader] = *ppVertexShader;
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
}
}
if (hr == S_OK && ppVertexShader && pShaderBytecode)
Expand Down Expand Up @@ -1530,11 +1532,8 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreatePixelShader(THIS_
}
if (hr != S_OK && ppPixelShader && pShaderBytecode)
{
// TODO: shouldn't require critical section
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
char *replaceShader = ReplaceShader(GetD3D11Device(), hash, L"ps", pShaderBytecode, BytecodeLength, replaceShaderSize,
shaderModel, ftWrite, (void **)&zeroShader);
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
if (replaceShader)
{
// Create the new shader.
Expand All @@ -1551,7 +1550,9 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreatePixelShader(THIS_
D3D11Base::ID3DBlob* blob;
D3DCreateBlob(replaceShaderSize, &blob);
memcpy(blob->GetBufferPointer(), replaceShader, replaceShaderSize);
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
RegisterForReload(*ppPixelShader, hash, L"ps", shaderModel, pClassLinkage, blob, ftWrite);
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
}
KeepOriginalShader(this, hash, NULL, *ppPixelShader, pShaderBytecode, BytecodeLength, pClassLinkage);
}
Expand All @@ -1570,6 +1571,7 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreatePixelShader(THIS_
// have a copy for every shader seen.
if (G->hunting)
{
if (G->ENABLE_CRITICAL_SECTION) EnterCriticalSection(&G->mCriticalSection);
D3D11Base::ID3DBlob* blob;
D3DCreateBlob(BytecodeLength, &blob);
memcpy(blob->GetBufferPointer(), pShaderBytecode, blob->GetBufferSize());
Expand All @@ -1580,6 +1582,7 @@ STDMETHODIMP D3D11Wrapper::ID3D11Device::CreatePixelShader(THIS_
// original and depth buffer filtering will work:
if (G->mOriginalPixelShaders.count(*ppPixelShader) == 0)
G->mOriginalPixelShaders[*ppPixelShader] = *ppPixelShader;
if (G->ENABLE_CRITICAL_SECTION) LeaveCriticalSection(&G->mCriticalSection);
}
}
if (hr == S_OK && ppPixelShader && pShaderBytecode)
Expand Down
2 changes: 1 addition & 1 deletion HLSLDecompiler/DecompileHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ class Decompiler
int ReadStatement(const char *pos)
{
// Kill newline.
static char lineBuffer[256];
char lineBuffer[256];
strncpy(lineBuffer, pos, 255); lineBuffer[255] = 0;
char *newlinePos = strchr(lineBuffer, '\n'); if (newlinePos) *newlinePos = 0;
op1[0] = 0; op2[0] = 0; op3[0] = 0; op4[0] = 0; op5[0] = 0; op6[0] = 0; op7[0] = 0; op8[0] = 0;
Expand Down

0 comments on commit 1a107cf

Please sign in to comment.