2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 #include "PixelShaderCompiler.h"
25 CPixelShaderCompiler::CPixelShaderCompiler(IDirect3DDevice9
* pD3DDev
, bool fStaySilent
)
28 , m_pD3DXCompileShader(NULL
)
29 , m_pD3DXDisassembleShader(NULL
)
33 // Load first available dll
34 // Older versions are preffered because those still have PS1.X support
35 for (int i
=24; i
<=D3DX_SDK_VERSION
; i
++)
37 if (i
!=33) // Don't use DXSDK April 2007 (crashes sometimes during shader compilation)
39 d3dx9_dll
.Format(_T("d3dx9_%d.dll"), i
);
40 m_hDll
= LoadLibrary(d3dx9_dll
);
50 m_pD3DXCompileShader
= (D3DXCompileShaderPtr
)GetProcAddress(m_hDll
, "D3DXCompileShader");
51 m_pD3DXDisassembleShader
= (D3DXDisassembleShaderPtr
)GetProcAddress(m_hDll
, "D3DXDisassembleShader");
58 AfxMessageBox(_T("Cannot load ") + d3dx9_dll
+ _T(", pixel shaders will not work."), MB_OK
);
60 else if(!m_pD3DXCompileShader
|| !m_pD3DXDisassembleShader
)
62 AfxMessageBox(_T("Cannot find necessary function entry points in ") + d3dx9_dll
+ _T(", pixel shaders will not work."), MB_OK
);
67 CPixelShaderCompiler::~CPixelShaderCompiler()
69 if(m_hDll
) FreeLibrary(m_hDll
);
72 HRESULT
CPixelShaderCompiler::CompileShader(
77 IDirect3DPixelShader9
** ppPixelShader
,
81 if(!m_pD3DXCompileShader
|| !m_pD3DXDisassembleShader
)
86 CComPtr
<ID3DXBuffer
> pShader
, pDisAsm
, pErrorMsgs
;
87 hr
= m_pD3DXCompileShader(pSrcData
, strlen(pSrcData
), NULL
, NULL
, pFunctionName
, pProfile
, Flags
, &pShader
, &pErrorMsgs
, NULL
);
93 CStringA msg
= "Unexpected compiler error";
97 int len
= pErrorMsgs
->GetBufferSize();
98 memcpy(msg
.GetBufferSetLength(len
), pErrorMsgs
->GetBufferPointer(), len
);
109 if(!m_pD3DDev
) return E_FAIL
;
110 hr
= m_pD3DDev
->CreatePixelShader((DWORD
*)pShader
->GetBufferPointer(), ppPixelShader
);
111 if(FAILED(hr
)) return hr
;
116 hr
= m_pD3DXDisassembleShader((DWORD
*)pShader
->GetBufferPointer(), FALSE
, NULL
, &pDisAsm
);
117 if(SUCCEEDED(hr
) && pDisAsm
) *disasm
= CStringA((const char*)pDisAsm
->GetBufferPointer());