2013-04-18 03:29:41 +00:00
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
2008-12-08 05:25:12 +00:00
# include <d3dx9.h>
# include <string>
2009-09-13 09:23:30 +00:00
# include "VideoConfig.h"
2008-12-08 05:25:12 +00:00
# include "D3DShader.h"
2011-01-29 20:16:51 +00:00
namespace DX9
{
2008-12-08 05:25:12 +00:00
namespace D3D
{
2008-12-26 17:02:46 +00:00
2010-09-28 02:15:02 +00:00
// bytecode->shader.
2010-01-17 17:44:09 +00:00
LPDIRECT3DVERTEXSHADER9 CreateVertexShaderFromByteCode ( const u8 * bytecode , int len )
{
LPDIRECT3DVERTEXSHADER9 v_shader ;
HRESULT hr = D3D : : dev - > CreateVertexShader ( ( DWORD * ) bytecode , & v_shader ) ;
if ( FAILED ( hr ) )
2011-07-29 22:18:11 +00:00
return NULL ;
2010-01-17 17:44:09 +00:00
return v_shader ;
}
2010-09-28 02:15:02 +00:00
// code->bytecode.
2010-01-17 17:44:09 +00:00
bool CompileVertexShader ( const char * code , int len , u8 * * bytecode , int * bytecodelen )
2008-12-26 17:02:46 +00:00
{
2010-09-28 02:15:02 +00:00
LPD3DXBUFFER shaderBuffer = NULL ;
LPD3DXBUFFER errorBuffer = NULL ;
2013-04-15 20:28:55 +00:00
HRESULT hr = PD3DXCompileShader ( code , len , 0 , 0 , " main " , D3D : : VertexShaderVersionString ( ) ,
2009-09-13 08:21:35 +00:00
0 , & shaderBuffer , & errorBuffer , 0 ) ;
2008-12-26 17:02:46 +00:00
if ( FAILED ( hr ) )
{
2011-07-29 22:18:11 +00:00
static int num_failures = 0 ;
char szTemp [ MAX_PATH ] ;
sprintf ( szTemp , " %sbad_vs_%04i.txt " , File : : GetUserPath ( D_DUMP_IDX ) . c_str ( ) , num_failures + + ) ;
2013-03-01 01:33:39 +00:00
std : : ofstream file ;
OpenFStream ( file , szTemp , std : : ios_base : : out ) ;
2011-07-29 22:18:11 +00:00
file < < code ;
file . close ( ) ;
PanicAlert ( " Failed to compile vertex shader! \n This usually happens when trying to use Dolphin with an outdated GPU or integrated GPU like the Intel GMA series. \n \n If you're sure this is Dolphin's error anyway, post the contents of %s along with this error message at the forums. \n \n Debug info (%s): \n %s " ,
szTemp ,
D3D : : VertexShaderVersionString ( ) ,
( char * ) errorBuffer - > GetBufferPointer ( ) ) ;
* bytecode = NULL ;
2010-01-17 17:44:09 +00:00
* bytecodelen = 0 ;
2008-12-26 17:02:46 +00:00
}
2011-07-29 22:18:11 +00:00
else
2008-12-26 17:02:46 +00:00
{
2010-01-17 17:44:09 +00:00
* bytecodelen = shaderBuffer - > GetBufferSize ( ) ;
* bytecode = new u8 [ * bytecodelen ] ;
memcpy ( * bytecode , shaderBuffer - > GetBufferPointer ( ) , * bytecodelen ) ;
2008-12-26 17:02:46 +00:00
}
2008-12-08 05:25:12 +00:00
2008-12-26 17:02:46 +00:00
//cleanup
if ( shaderBuffer )
shaderBuffer - > Release ( ) ;
if ( errorBuffer )
errorBuffer - > Release ( ) ;
2010-01-17 17:44:09 +00:00
return SUCCEEDED ( hr ) ? true : false ;
2008-12-26 17:02:46 +00:00
}
2008-12-08 05:25:12 +00:00
2010-09-28 02:15:02 +00:00
// bytecode->shader
2010-01-17 17:44:09 +00:00
LPDIRECT3DPIXELSHADER9 CreatePixelShaderFromByteCode ( const u8 * bytecode , int len )
{
LPDIRECT3DPIXELSHADER9 p_shader ;
HRESULT hr = D3D : : dev - > CreatePixelShader ( ( DWORD * ) bytecode , & p_shader ) ;
if ( FAILED ( hr ) )
2011-07-29 22:18:11 +00:00
return NULL ;
2010-01-17 17:44:09 +00:00
return p_shader ;
}
2010-09-28 02:15:02 +00:00
// code->bytecode
2010-01-17 17:44:09 +00:00
bool CompilePixelShader ( const char * code , int len , u8 * * bytecode , int * bytecodelen )
2008-12-26 17:02:46 +00:00
{
LPD3DXBUFFER shaderBuffer = 0 ;
LPD3DXBUFFER errorBuffer = 0 ;
2009-03-01 10:53:28 +00:00
2009-09-13 08:21:35 +00:00
// Someone:
// For some reason, I had this kind of errors : "Shader uses texture addressing operations
2009-09-02 04:10:40 +00:00
// in a dependency chain that is too complex for the target shader model (ps_2_0) to handle."
2010-07-17 11:42:28 +00:00
HRESULT hr = PD3DXCompileShader ( code , len , 0 , 0 , " main " , D3D : : PixelShaderVersionString ( ) ,
2009-09-13 08:21:35 +00:00
0 , & shaderBuffer , & errorBuffer , 0 ) ;
2008-12-08 05:25:12 +00:00
2008-12-26 17:02:46 +00:00
if ( FAILED ( hr ) )
{
2011-07-29 22:18:11 +00:00
static int num_failures = 0 ;
char szTemp [ MAX_PATH ] ;
sprintf ( szTemp , " %sbad_ps_%04i.txt " , File : : GetUserPath ( D_DUMP_IDX ) . c_str ( ) , num_failures + + ) ;
2013-03-01 01:33:39 +00:00
std : : ofstream file ;
OpenFStream ( file , szTemp , std : : ios_base : : out ) ;
2011-07-29 22:18:11 +00:00
file < < code ;
file . close ( ) ;
PanicAlert ( " Failed to compile pixel shader! \n This usually happens when trying to use Dolphin with an outdated GPU or integrated GPU like the Intel GMA series. \n \n If you're sure this is Dolphin's error anyway, post the contents of %s along with this error message at the forums. \n \n Debug info (%s): \n %s " ,
szTemp ,
D3D : : PixelShaderVersionString ( ) ,
( char * ) errorBuffer - > GetBufferPointer ( ) ) ;
* bytecode = NULL ;
2010-01-17 17:44:09 +00:00
* bytecodelen = 0 ;
2008-12-26 17:02:46 +00:00
}
2011-07-29 22:18:11 +00:00
else
2008-12-26 17:02:46 +00:00
{
2010-01-17 17:44:09 +00:00
* bytecodelen = shaderBuffer - > GetBufferSize ( ) ;
* bytecode = new u8 [ * bytecodelen ] ;
memcpy ( * bytecode , shaderBuffer - > GetBufferPointer ( ) , * bytecodelen ) ;
2008-12-26 17:02:46 +00:00
}
2008-12-08 05:25:12 +00:00
2008-12-26 17:02:46 +00:00
//cleanup
if ( shaderBuffer )
shaderBuffer - > Release ( ) ;
if ( errorBuffer )
errorBuffer - > Release ( ) ;
2010-01-17 17:44:09 +00:00
return SUCCEEDED ( hr ) ? true : false ;
}
2010-09-28 02:15:02 +00:00
LPDIRECT3DVERTEXSHADER9 CompileAndCreateVertexShader ( const char * code , int len )
{
2010-01-17 17:44:09 +00:00
u8 * bytecode ;
int bytecodelen ;
2010-09-28 02:15:02 +00:00
if ( CompileVertexShader ( code , len , & bytecode , & bytecodelen ) )
{
2010-01-17 17:44:09 +00:00
LPDIRECT3DVERTEXSHADER9 v_shader = CreateVertexShaderFromByteCode ( bytecode , len ) ;
delete [ ] bytecode ;
return v_shader ;
}
2010-09-28 02:15:02 +00:00
return NULL ;
2010-01-17 17:44:09 +00:00
}
2010-09-28 02:15:02 +00:00
LPDIRECT3DPIXELSHADER9 CompileAndCreatePixelShader ( const char * code , unsigned int len )
{
2010-01-17 17:44:09 +00:00
u8 * bytecode ;
int bytecodelen ;
2010-09-28 02:15:02 +00:00
if ( CompilePixelShader ( code , len , & bytecode , & bytecodelen ) )
{
2010-01-17 17:44:09 +00:00
LPDIRECT3DPIXELSHADER9 p_shader = CreatePixelShaderFromByteCode ( bytecode , len ) ;
delete [ ] bytecode ;
return p_shader ;
}
2010-09-28 02:15:02 +00:00
return NULL ;
2008-12-26 17:02:46 +00:00
}
2009-02-28 22:10:38 +00:00
} // namespace
2011-01-29 20:16:51 +00:00
} // namespace DX9