A Fast Bresenham Type Algorithm For Drawing Ellipses by John Kennedy
[xy_vsfilter.git] / src / apps / mplayerc / PixelShaderCompiler.cpp
blob6681c5ca28dc7482e49516801951a945785cd33e
1 /*
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)
8 * any later version.
9 *
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
22 #include "stdafx.h"
23 #include "PixelShaderCompiler.h"
25 CPixelShaderCompiler::CPixelShaderCompiler(IDirect3DDevice9* pD3DDev, bool fStaySilent)
26 : m_pD3DDev(pD3DDev)
27 , m_hDll(NULL)
28 , m_pD3DXCompileShader(NULL)
29 , m_pD3DXDisassembleShader(NULL)
31 CString d3dx9_dll;
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);
42 if(m_hDll) {
43 break;
48 if(m_hDll)
50 m_pD3DXCompileShader = (D3DXCompileShaderPtr)GetProcAddress(m_hDll, "D3DXCompileShader");
51 m_pD3DXDisassembleShader = (D3DXDisassembleShaderPtr)GetProcAddress(m_hDll, "D3DXDisassembleShader");
54 if(!fStaySilent)
56 if(!m_hDll)
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(
73 LPCSTR pSrcData,
74 LPCSTR pFunctionName,
75 LPCSTR pProfile,
76 DWORD Flags,
77 IDirect3DPixelShader9** ppPixelShader,
78 CString* disasm,
79 CString* errmsg)
81 if(!m_pD3DXCompileShader || !m_pD3DXDisassembleShader)
82 return E_FAIL;
84 HRESULT hr;
86 CComPtr<ID3DXBuffer> pShader, pDisAsm, pErrorMsgs;
87 hr = m_pD3DXCompileShader(pSrcData, strlen(pSrcData), NULL, NULL, pFunctionName, pProfile, Flags, &pShader, &pErrorMsgs, NULL);
89 if(FAILED(hr))
91 if(errmsg)
93 CStringA msg = "Unexpected compiler error";
95 if(pErrorMsgs)
97 int len = pErrorMsgs->GetBufferSize();
98 memcpy(msg.GetBufferSetLength(len), pErrorMsgs->GetBufferPointer(), len);
101 *errmsg = msg;
104 return hr;
107 if(ppPixelShader)
109 if(!m_pD3DDev) return E_FAIL;
110 hr = m_pD3DDev->CreatePixelShader((DWORD*)pShader->GetBufferPointer(), ppPixelShader);
111 if(FAILED(hr)) return hr;
114 if(disasm)
116 hr = m_pD3DXDisassembleShader((DWORD*)pShader->GetBufferPointer(), FALSE, NULL, &pDisAsm);
117 if(SUCCEEDED(hr) && pDisAsm) *disasm = CStringA((const char*)pDisAsm->GetBufferPointer());
120 return S_OK;