2013-04-17 21:29:41 -06:00
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
2010-06-13 13:50:06 -06:00
# include <string>
# include "VideoConfig.h"
2011-01-25 09:43:08 -07:00
2011-06-11 13:37:21 -06:00
# include "D3DBase.h"
2010-06-13 13:50:06 -06:00
# include "D3DShader.h"
2011-01-29 13:16:51 -07:00
namespace DX11
{
2010-06-13 13:50:06 -06:00
namespace D3D
{
// bytecode->shader
2011-06-11 13:37:21 -06:00
ID3D11VertexShader * CreateVertexShaderFromByteCode ( const void * bytecode , unsigned int len )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D11VertexShader * v_shader ;
HRESULT hr = D3D : : device - > CreateVertexShader ( bytecode , len , NULL , & v_shader ) ;
2010-06-13 13:50:06 -06:00
if ( FAILED ( hr ) )
2011-07-29 16:18:11 -06:00
return NULL ;
2011-06-11 13:37:21 -06:00
return v_shader ;
2010-06-13 13:50:06 -06:00
}
2011-06-11 13:37:21 -06:00
// code->bytecode
bool CompileVertexShader ( const char * code , unsigned int len , D3DBlob * * blob )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D10Blob * shaderBuffer = NULL ;
ID3D10Blob * errorBuffer = NULL ;
# if defined(_DEBUG) || defined(DEBUGFAST)
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG | D3D10_SHADER_WARNINGS_ARE_ERRORS ;
# else
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_OPTIMIZATION_LEVEL3 | D3D10_SHADER_SKIP_VALIDATION ;
# endif
2013-10-19 03:27:57 -06:00
HRESULT hr = PD3DCompile ( code , len , NULL , NULL , NULL , " main " , D3D : : VertexShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-06-11 13:37:21 -06:00
if ( errorBuffer )
{
INFO_LOG ( VIDEO , " Vertex shader compiler messages: \n %s \n " ,
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
}
2011-03-30 01:17:23 -06:00
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) )
{
2011-07-29 16:18:11 -06: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-02-28 18:33:39 -07:00
std : : ofstream file ;
OpenFStream ( file , szTemp , std : : ios_base : : out ) ;
2011-07-29 16:18:11 -06: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 ( ) ) ;
2011-06-11 13:37:21 -06:00
* blob = NULL ;
errorBuffer - > Release ( ) ;
}
else
{
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
}
return SUCCEEDED ( hr ) ;
2010-06-13 13:50:06 -06:00
}
2011-03-14 03:38:29 -06:00
// bytecode->shader
2011-06-11 13:37:21 -06:00
ID3D11GeometryShader * CreateGeometryShaderFromByteCode ( const void * bytecode , unsigned int len )
2011-03-14 03:38:29 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D11GeometryShader * g_shader ;
HRESULT hr = D3D : : device - > CreateGeometryShader ( bytecode , len , NULL , & g_shader ) ;
2011-03-14 03:38:29 -06:00
if ( FAILED ( hr ) )
2011-07-29 16:18:11 -06:00
return NULL ;
2011-06-11 13:37:21 -06:00
return g_shader ;
2011-03-14 03:38:29 -06:00
}
2011-06-11 13:37:21 -06:00
// code->bytecode
bool CompileGeometryShader ( const char * code , unsigned int len , D3DBlob * * blob ,
const D3D_SHADER_MACRO * pDefines )
2011-03-14 03:38:29 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D10Blob * shaderBuffer = NULL ;
ID3D10Blob * errorBuffer = NULL ;
2011-03-14 03:38:29 -06:00
# if defined(_DEBUG) || defined(DEBUGFAST)
2011-06-11 13:37:21 -06:00
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG | D3D10_SHADER_WARNINGS_ARE_ERRORS ;
2011-03-14 03:38:29 -06:00
# else
2011-06-11 13:37:21 -06:00
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_OPTIMIZATION_LEVEL3 | D3D10_SHADER_SKIP_VALIDATION ;
2011-03-14 03:38:29 -06:00
# endif
2013-10-19 03:27:57 -06:00
HRESULT hr = PD3DCompile ( code , len , NULL , pDefines , NULL , " main " , D3D : : GeometryShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-03-30 01:17:23 -06:00
if ( errorBuffer )
2011-03-14 03:38:29 -06:00
{
2011-06-11 13:37:21 -06:00
INFO_LOG ( VIDEO , " Geometry shader compiler messages: \n %s \n " ,
2011-03-30 01:17:23 -06:00
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
2011-06-11 13:37:21 -06:00
}
2011-03-30 01:17:23 -06:00
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) )
{
2011-07-29 16:18:11 -06:00
static int num_failures = 0 ;
char szTemp [ MAX_PATH ] ;
sprintf ( szTemp , " %sbad_gs_%04i.txt " , File : : GetUserPath ( D_DUMP_IDX ) . c_str ( ) , num_failures + + ) ;
2013-02-28 18:33:39 -07:00
std : : ofstream file ;
OpenFStream ( file , szTemp , std : : ios_base : : out ) ;
2011-07-29 16:18:11 -06:00
file < < code ;
file . close ( ) ;
PanicAlert ( " Failed to compile geometry 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 : : GeometryShaderVersionString ( ) ,
( char * ) errorBuffer - > GetBufferPointer ( ) ) ;
2011-06-11 13:37:21 -06:00
* blob = NULL ;
2011-03-14 03:38:29 -06:00
errorBuffer - > Release ( ) ;
}
2011-06-11 13:37:21 -06:00
else
{
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
}
return SUCCEEDED ( hr ) ;
2011-03-14 03:38:29 -06:00
}
2011-06-11 13:37:21 -06:00
// bytecode->shader
ID3D11PixelShader * CreatePixelShaderFromByteCode ( const void * bytecode , unsigned int len )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D11PixelShader * p_shader ;
HRESULT hr = D3D : : device - > CreatePixelShader ( bytecode , len , NULL , & p_shader ) ;
if ( FAILED ( hr ) )
{
PanicAlert ( " CreatePixelShaderFromByteCode failed at %s %d \n " , __FILE__ , __LINE__ ) ;
p_shader = NULL ;
}
return p_shader ;
2010-06-13 13:50:06 -06:00
}
// code->bytecode
2011-06-11 13:37:21 -06:00
bool CompilePixelShader ( const char * code , unsigned int len , D3DBlob * * blob ,
2011-02-26 16:41:02 -07:00
const D3D_SHADER_MACRO * pDefines )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D10Blob * shaderBuffer = NULL ;
ID3D10Blob * errorBuffer = NULL ;
2010-06-13 13:50:06 -06:00
2011-06-11 13:37:21 -06:00
# if defined(_DEBUG) || defined(DEBUGFAST)
UINT flags = D3D10_SHADER_DEBUG | D3D10_SHADER_WARNINGS_ARE_ERRORS ;
# else
UINT flags = D3D10_SHADER_OPTIMIZATION_LEVEL3 ;
# endif
2013-10-19 03:27:57 -06:00
HRESULT hr = PD3DCompile ( code , len , NULL , pDefines , NULL , " main " , D3D : : PixelShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-06-11 13:37:21 -06:00
if ( errorBuffer )
{
INFO_LOG ( VIDEO , " Pixel shader compiler messages: \n %s " ,
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
}
2010-06-13 13:50:06 -06:00
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) )
2010-06-13 13:50:06 -06:00
{
2011-07-29 16:18:11 -06: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-02-28 18:33:39 -07:00
std : : ofstream file ;
OpenFStream ( file , szTemp , std : : ios_base : : out ) ;
2011-07-29 16:18:11 -06: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 ( ) ) ;
2011-06-11 13:37:21 -06:00
* blob = NULL ;
errorBuffer - > Release ( ) ;
2010-06-16 17:25:19 -06:00
}
else
{
2011-06-11 13:37:21 -06:00
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
2010-06-13 13:50:06 -06:00
}
2011-06-11 13:37:21 -06:00
return SUCCEEDED ( hr ) ;
2010-06-13 13:50:06 -06:00
}
2011-06-11 13:37:21 -06:00
ID3D11VertexShader * CompileAndCreateVertexShader ( const char * code ,
unsigned int len )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
D3DBlob * blob = NULL ;
if ( CompileVertexShader ( code , len , & blob ) )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D11VertexShader * v_shader = CreateVertexShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return v_shader ;
2010-06-13 13:50:06 -06:00
}
2011-06-11 13:37:21 -06:00
return NULL ;
}
ID3D11GeometryShader * CompileAndCreateGeometryShader ( const char * code ,
unsigned int len , const D3D_SHADER_MACRO * pDefines )
{
D3DBlob * blob = NULL ;
if ( CompileGeometryShader ( code , len , & blob , pDefines ) )
2011-03-14 03:38:29 -06:00
{
2011-06-11 13:37:21 -06:00
ID3D11GeometryShader * g_shader = CreateGeometryShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return g_shader ;
2011-03-14 03:38:29 -06:00
}
2011-06-11 13:37:21 -06:00
return NULL ;
2011-03-14 03:38:29 -06:00
}
2011-06-11 13:37:21 -06:00
ID3D11PixelShader * CompileAndCreatePixelShader ( const char * code ,
unsigned int len )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
D3DBlob * blob = NULL ;
CompilePixelShader ( code , len , & blob ) ;
2010-06-13 13:50:06 -06:00
if ( blob )
{
2011-06-11 13:37:21 -06:00
ID3D11PixelShader * p_shader = CreatePixelShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return p_shader ;
2010-06-13 13:50:06 -06:00
}
2011-06-11 13:37:21 -06:00
return NULL ;
2010-06-13 13:50:06 -06:00
}
} // namespace
2011-01-29 13:16:51 -07:00
} // namespace DX11