diff options
| author | NeoBrainX <NeoBrainX@googlemail.com> | 2011-06-11 19:37:21 +0000 |
|---|---|---|
| committer | NeoBrainX <NeoBrainX@googlemail.com> | 2011-06-11 19:37:21 +0000 |
| commit | 8244efcc02b1aed06214bc58e5503ca5d68b4e63 (patch) | |
| tree | 39539ff2188a4fb9a6a870842496db496a1201ae /Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp | |
| parent | e5210de9d5da0cdbe5b8a8171b21d9ab9c5a8d5c (diff) | |
Revert r7421 and r7422.
Should fix issue 4413.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7592 8ced0084-cf51-0410-be5f-012b33b47a6e
Diffstat (limited to 'Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp')
| -rw-r--r-- | Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp | 168 |
1 files changed, 77 insertions, 91 deletions
diff --git a/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp index d1b20cd3a4..5418923bf8 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp @@ -44,14 +44,13 @@ const PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry; LinearDiskCache<PIXELSHADERUID, u8> g_ps_disk_cache; -SharedPtr<ID3D11PixelShader> s_ColorMatrixProgram[2]; -SharedPtr<ID3D11PixelShader> s_ColorCopyProgram[2]; -SharedPtr<ID3D11PixelShader> s_DepthMatrixProgram[2]; -SharedPtr<ID3D11PixelShader> s_ClearProgram; -SharedPtr<ID3D11PixelShader> s_rgba6_to_rgb8[2]; -SharedPtr<ID3D11PixelShader> s_rgb8_to_rgba6[2]; - -SharedPtr<ID3D11Buffer> pscbuf; +ID3D11PixelShader* s_ColorMatrixProgram[2] = {NULL}; +ID3D11PixelShader* s_ColorCopyProgram[2] = {NULL}; +ID3D11PixelShader* s_DepthMatrixProgram[2] = {NULL}; +ID3D11PixelShader* s_ClearProgram = NULL; +ID3D11PixelShader* s_rgba6_to_rgb8[2] = {NULL}; +ID3D11PixelShader* s_rgb8_to_rgba6[2] = {NULL}; +ID3D11Buffer* pscbuf = NULL; const char clear_program_code[] = { "void main(\n" @@ -243,7 +242,6 @@ const char reint_rgb8_to_rgba6_msaa[] = { "}\n" }; - ID3D11PixelShader* PixelShaderCache::ReinterpRGBA6ToRGB8(bool multisampled) { if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) @@ -298,65 +296,50 @@ ID3D11PixelShader* PixelShaderCache::ReinterpRGB8ToRGBA6(bool multisampled) ID3D11PixelShader* PixelShaderCache::GetColorCopyProgram(bool multisampled) { - if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) - { - return s_ColorCopyProgram[0]; - } - else if (!s_ColorCopyProgram[1]) + if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_ColorCopyProgram[0]; + else if (s_ColorCopyProgram[1]) return s_ColorCopyProgram[1]; + else { // create MSAA shader for current AA mode char buf[1024]; - const int l = sprintf_s(buf, 1024, color_copy_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); - + int l = sprintf_s(buf, 1024, color_copy_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); s_ColorCopyProgram[1] = D3D::CompileAndCreatePixelShader(buf, l); - - CHECK(s_ColorCopyProgram[1], "Create color copy MSAA pixel shader"); - D3D::SetDebugObjectName(s_ColorCopyProgram[1], "color copy MSAA pixel shader"); + CHECK(s_ColorCopyProgram[1]!=NULL, "Create color copy MSAA pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorCopyProgram[1], "color copy MSAA pixel shader"); + return s_ColorCopyProgram[1]; } - - return s_ColorCopyProgram[1]; } ID3D11PixelShader* PixelShaderCache::GetColorMatrixProgram(bool multisampled) { - if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) - { - return s_ColorMatrixProgram[0]; - } - else if (!s_ColorMatrixProgram[1]) + if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_ColorMatrixProgram[0]; + else if (s_ColorMatrixProgram[1]) return s_ColorMatrixProgram[1]; + else { // create MSAA shader for current AA mode char buf[1024]; - const int l = sprintf_s(buf, 1024, color_matrix_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); - + int l = sprintf_s(buf, 1024, color_matrix_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); s_ColorMatrixProgram[1] = D3D::CompileAndCreatePixelShader(buf, l); - - CHECK(s_ColorMatrixProgram[1], "Create color matrix MSAA pixel shader"); - D3D::SetDebugObjectName(s_ColorMatrixProgram[1], "color matrix MSAA pixel shader"); + CHECK(s_ColorMatrixProgram[1]!=NULL, "Create color matrix MSAA pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorMatrixProgram[1], "color matrix MSAA pixel shader"); + return s_ColorMatrixProgram[1]; } - - return s_ColorMatrixProgram[1]; } ID3D11PixelShader* PixelShaderCache::GetDepthMatrixProgram(bool multisampled) { - if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) - { - return s_DepthMatrixProgram[0]; - } - else if (!s_DepthMatrixProgram[1]) + if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_DepthMatrixProgram[0]; + else if (s_DepthMatrixProgram[1]) return s_DepthMatrixProgram[1]; + else { // create MSAA shader for current AA mode char buf[1024]; - const int l = sprintf_s(buf, 1024, depth_matrix_program_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); - + int l = sprintf_s(buf, 1024, depth_matrix_program_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode)); s_DepthMatrixProgram[1] = D3D::CompileAndCreatePixelShader(buf, l); - - CHECK(s_DepthMatrixProgram[1], "Create depth matrix MSAA pixel shader"); - D3D::SetDebugObjectName(s_DepthMatrixProgram[1], "depth matrix MSAA pixel shader"); + CHECK(s_DepthMatrixProgram[1]!=NULL, "Create depth matrix MSAA pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_DepthMatrixProgram[1], "depth matrix MSAA pixel shader"); + return s_DepthMatrixProgram[1]; } - - return s_DepthMatrixProgram[1]; } ID3D11PixelShader* PixelShaderCache::GetClearProgram() @@ -364,31 +347,27 @@ ID3D11PixelShader* PixelShaderCache::GetClearProgram() return s_ClearProgram; } -ID3D11Buffer*const& PixelShaderCache::GetConstantBuffer() +ID3D11Buffer* &PixelShaderCache::GetConstantBuffer() { // TODO: divide the global variables of the generated shaders into about 5 constant buffers to speed this up if (pscbufchanged) { D3D11_MAPPED_SUBRESOURCE map; - D3D::g_context->Map(pscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); + D3D::context->Map(pscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); memcpy(map.pData, psconstants, sizeof(psconstants)); - D3D::g_context->Unmap(pscbuf, 0); + D3D::context->Unmap(pscbuf, 0); pscbufchanged = false; } return pscbuf; } // this class will load the precompiled shaders into our cache -class PixelShaderCacheInserter +class PixelShaderCacheInserter : public LinearDiskCacheReader<PIXELSHADERUID, u8> { public: - template <typename F> - void operator()(const PIXELSHADERUID& key, u32 value_size, F get_data) const + void Read(const PIXELSHADERUID &key, const u8 *value, u32 value_size) { - std::unique_ptr<u8[]> value(new u8[value_size]); - get_data(value.get()); - - PixelShaderCache::InsertByteCode(key, value.get(), value_size); + PixelShaderCache::InsertByteCode(key, value, value_size); } }; @@ -396,29 +375,29 @@ void PixelShaderCache::Init() { unsigned int cbsize = ((sizeof(psconstants))&(~0xf))+0x10; // must be a multiple of 16 D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); - pscbuf = CreateBufferShared(&cbdesc, NULL); - CHECK(pscbuf, "Create pixel shader constant buffer"); - D3D::SetDebugObjectName(pscbuf, "pixel shader constant buffer used to emulate the GX pipeline"); + D3D::device->CreateBuffer(&cbdesc, NULL, &pscbuf); + CHECK(pscbuf!=NULL, "Create pixel shader constant buffer"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)pscbuf, "pixel shader constant buffer used to emulate the GX pipeline"); // used when drawing clear quads s_ClearProgram = D3D::CompileAndCreatePixelShader(clear_program_code, sizeof(clear_program_code)); - CHECK(s_ClearProgram, "Create clear pixel shader"); - D3D::SetDebugObjectName(s_ClearProgram, "clear pixel shader"); + CHECK(s_ClearProgram!=NULL, "Create clear pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ClearProgram, "clear pixel shader"); // used when copying/resolving the color buffer s_ColorCopyProgram[0] = D3D::CompileAndCreatePixelShader(color_copy_program_code, sizeof(color_copy_program_code)); - CHECK(s_ColorCopyProgram[0], "Create color copy pixel shader"); - D3D::SetDebugObjectName(s_ColorCopyProgram[0], "color copy pixel shader"); + CHECK(s_ColorCopyProgram[0]!=NULL, "Create color copy pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorCopyProgram[0], "color copy pixel shader"); // used for color conversion s_ColorMatrixProgram[0] = D3D::CompileAndCreatePixelShader(color_matrix_program_code, sizeof(color_matrix_program_code)); - CHECK(s_ColorMatrixProgram[0], "Create color matrix pixel shader"); - D3D::SetDebugObjectName(s_ColorMatrixProgram[0], "color matrix pixel shader"); + CHECK(s_ColorMatrixProgram[0]!=NULL, "Create color matrix pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorMatrixProgram[0], "color matrix pixel shader"); // used for depth copy s_DepthMatrixProgram[0] = D3D::CompileAndCreatePixelShader(depth_matrix_program, sizeof(depth_matrix_program)); - CHECK(s_DepthMatrixProgram[0], "Create depth matrix pixel shader"); - D3D::SetDebugObjectName(s_DepthMatrixProgram[0], "depth matrix pixel shader"); + CHECK(s_DepthMatrixProgram[0]!=NULL, "Create depth matrix pixel shader"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)s_DepthMatrixProgram[0], "depth matrix pixel shader"); Clear(); @@ -430,39 +409,41 @@ void PixelShaderCache::Init() char cache_filename[MAX_PATH]; sprintf(cache_filename, "%sdx11-%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), - SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); - - g_ps_disk_cache.OpenAndRead(cache_filename, PixelShaderCacheInserter()); + SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); + PixelShaderCacheInserter inserter; + g_ps_disk_cache.OpenAndRead(cache_filename, inserter); } // ONLY to be used during shutdown. void PixelShaderCache::Clear() { + for (PSCache::iterator iter = PixelShaders.begin(); iter != PixelShaders.end(); iter++) + iter->second.Destroy(); PixelShaders.clear(); } // Used in Swap() when AA mode has changed void PixelShaderCache::InvalidateMSAAShaders() { - s_ColorCopyProgram[1].reset(); - s_ColorMatrixProgram[1].reset(); - s_DepthMatrixProgram[1].reset(); - s_rgb8_to_rgba6[1].reset(); - s_rgba6_to_rgb8[1].reset(); + SAFE_RELEASE(s_ColorCopyProgram[1]); + SAFE_RELEASE(s_ColorMatrixProgram[1]); + SAFE_RELEASE(s_DepthMatrixProgram[1]); + SAFE_RELEASE(s_rgb8_to_rgba6[1]); + SAFE_RELEASE(s_rgba6_to_rgb8[1]); } void PixelShaderCache::Shutdown() { - pscbuf.reset(); + SAFE_RELEASE(pscbuf); - s_ClearProgram.reset(); + SAFE_RELEASE(s_ClearProgram); for (int i = 0; i < 2; ++i) { - s_ColorCopyProgram[i].reset(); - s_ColorMatrixProgram[i].reset(); - s_DepthMatrixProgram[i].reset(); - s_rgba6_to_rgb8[i].reset(); - s_rgb8_to_rgba6[i].reset(); + SAFE_RELEASE(s_ColorCopyProgram[i]); + SAFE_RELEASE(s_ColorMatrixProgram[i]); + SAFE_RELEASE(s_DepthMatrixProgram[i]); + SAFE_RELEASE(s_rgba6_to_rgb8[i]); + SAFE_RELEASE(s_rgb8_to_rgba6[i]); } Clear(); @@ -470,7 +451,7 @@ void PixelShaderCache::Shutdown() g_ps_disk_cache.Close(); } -bool PixelShaderCache::LoadShader(DSTALPHA_MODE dstAlphaMode, u32 components) +bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) { PIXELSHADERUID uid; GetPixelShaderId(&uid, dstAlphaMode); @@ -495,14 +476,14 @@ bool PixelShaderCache::LoadShader(DSTALPHA_MODE dstAlphaMode, u32 components) last_entry = &entry; GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE,true); - return (!!entry.shader); + return (entry.shader != NULL); } // Need to compile a new shader const char* code = GeneratePixelShaderCode(dstAlphaMode, API_D3D11, components); - auto const pbytecode = D3D::CompilePixelShader(code, (unsigned int)strlen(code)); - if (!pbytecode) + D3DBlob* pbytecode; + if (!D3D::CompilePixelShader(code, (unsigned int)strlen(code), &pbytecode)) { PanicAlert("Failed to compile Pixel Shader:\n\n%s", code); GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true); @@ -510,25 +491,25 @@ bool PixelShaderCache::LoadShader(DSTALPHA_MODE dstAlphaMode, u32 components) } // Insert the bytecode into the caches - g_ps_disk_cache.Append(uid, (u8*)pbytecode->GetBufferPointer(), (u32)pbytecode->GetBufferSize()); + g_ps_disk_cache.Append(uid, pbytecode->Data(), pbytecode->Size()); g_ps_disk_cache.Sync(); - bool result = InsertByteCode(uid, pbytecode->GetBufferPointer(), (u32)pbytecode->GetBufferSize()); + bool result = InsertByteCode(uid, pbytecode->Data(), pbytecode->Size()); + pbytecode->Release(); GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true); return result; } bool PixelShaderCache::InsertByteCode(const PIXELSHADERUID &uid, const void* bytecode, unsigned int bytecodelen) { - auto const shader = D3D::CreatePixelShaderFromByteCode(bytecode, bytecodelen); - if (!shader) + ID3D11PixelShader* shader = D3D::CreatePixelShaderFromByteCode(bytecode, bytecodelen); + if (shader == NULL) { PanicAlert("Failed to create pixel shader at %s %d\n", __FILE__, __LINE__); - // INCSTAT(stats.numPixelShadersFailed); return false; } // TODO: Somehow make the debug name a bit more specific - D3D::SetDebugObjectName(shader, "a pixel shader of PixelShaderCache"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)shader, "a pixel shader of PixelShaderCache"); // Make an entry in the table PSCacheEntry newentry; @@ -537,6 +518,11 @@ bool PixelShaderCache::InsertByteCode(const PIXELSHADERUID &uid, const void* byt PixelShaders[uid] = newentry; last_entry = &PixelShaders[uid]; + if (!shader) { + // INCSTAT(stats.numPixelShadersFailed); + return false; + } + INCSTAT(stats.numPixelShadersCreated); SETSTAT(stats.numPixelShadersAlive, PixelShaders.size()); return true; |
