2013-04-18 03:29:41 +00:00
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
2010-06-13 19:50:06 +00:00
# include <string>
2014-02-17 10:18:15 +00:00
# include "VideoBackends/D3D/D3DBase.h"
# include "VideoBackends/D3D/D3DShader.h"
# include "VideoCommon/VideoConfig.h"
2010-06-13 19:50:06 +00:00
2011-01-29 20:16:51 +00:00
namespace DX11
{
2010-06-13 19:50:06 +00:00
namespace D3D
{
// bytecode->shader
2011-06-11 19:37:21 +00:00
ID3D11VertexShader * CreateVertexShaderFromByteCode ( const void * bytecode , unsigned int len )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D11VertexShader * v_shader ;
HRESULT hr = D3D : : device - > CreateVertexShader ( bytecode , len , NULL , & v_shader ) ;
2010-06-13 19:50:06 +00:00
if ( FAILED ( hr ) )
2011-07-29 22:18:11 +00:00
return NULL ;
2011-06-11 19:37:21 +00:00
return v_shader ;
2010-06-13 19:50:06 +00:00
}
2011-06-11 19:37:21 +00:00
// code->bytecode
bool CompileVertexShader ( const char * code , unsigned int len , D3DBlob * * blob )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00: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 09:27:57 +00:00
HRESULT hr = PD3DCompile ( code , len , NULL , NULL , NULL , " main " , D3D : : VertexShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-06-11 19:37:21 +00:00
if ( errorBuffer )
{
INFO_LOG ( VIDEO , " Vertex shader compiler messages: \n %s \n " ,
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
}
2011-03-30 07:17:23 +00:00
2011-06-11 19:37:21 +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 ( ) ) ;
2011-06-11 19:37:21 +00:00
* blob = NULL ;
errorBuffer - > Release ( ) ;
}
else
{
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
}
return SUCCEEDED ( hr ) ;
2010-06-13 19:50:06 +00:00
}
2011-03-14 09:38:29 +00:00
// bytecode->shader
2011-06-11 19:37:21 +00:00
ID3D11GeometryShader * CreateGeometryShaderFromByteCode ( const void * bytecode , unsigned int len )
2011-03-14 09:38:29 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D11GeometryShader * g_shader ;
HRESULT hr = D3D : : device - > CreateGeometryShader ( bytecode , len , NULL , & g_shader ) ;
2011-03-14 09:38:29 +00:00
if ( FAILED ( hr ) )
2011-07-29 22:18:11 +00:00
return NULL ;
2011-06-11 19:37:21 +00:00
return g_shader ;
2011-03-14 09:38:29 +00:00
}
2011-06-11 19:37:21 +00:00
// code->bytecode
bool CompileGeometryShader ( const char * code , unsigned int len , D3DBlob * * blob ,
const D3D_SHADER_MACRO * pDefines )
2011-03-14 09:38:29 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D10Blob * shaderBuffer = NULL ;
ID3D10Blob * errorBuffer = NULL ;
2011-03-14 09:38:29 +00:00
# if defined(_DEBUG) || defined(DEBUGFAST)
2011-06-11 19:37:21 +00:00
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG | D3D10_SHADER_WARNINGS_ARE_ERRORS ;
2011-03-14 09:38:29 +00:00
# else
2011-06-11 19:37:21 +00:00
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_OPTIMIZATION_LEVEL3 | D3D10_SHADER_SKIP_VALIDATION ;
2011-03-14 09:38:29 +00:00
# endif
2013-10-19 09:27:57 +00:00
HRESULT hr = PD3DCompile ( code , len , NULL , pDefines , NULL , " main " , D3D : : GeometryShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-03-30 07:17:23 +00:00
if ( errorBuffer )
2011-03-14 09:38:29 +00:00
{
2011-06-11 19:37:21 +00:00
INFO_LOG ( VIDEO , " Geometry shader compiler messages: \n %s \n " ,
2011-03-30 07:17:23 +00:00
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
2011-06-11 19:37:21 +00:00
}
2011-03-30 07:17:23 +00:00
2011-06-11 19:37:21 +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_gs_%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 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 19:37:21 +00:00
* blob = NULL ;
2011-03-14 09:38:29 +00:00
errorBuffer - > Release ( ) ;
}
2011-06-11 19:37:21 +00:00
else
{
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
}
return SUCCEEDED ( hr ) ;
2011-03-14 09:38:29 +00:00
}
2011-06-11 19:37:21 +00:00
// bytecode->shader
ID3D11PixelShader * CreatePixelShaderFromByteCode ( const void * bytecode , unsigned int len )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00: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 19:50:06 +00:00
}
// code->bytecode
2011-06-11 19:37:21 +00:00
bool CompilePixelShader ( const char * code , unsigned int len , D3DBlob * * blob ,
2011-02-26 23:41:02 +00:00
const D3D_SHADER_MACRO * pDefines )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D10Blob * shaderBuffer = NULL ;
ID3D10Blob * errorBuffer = NULL ;
2010-06-13 19:50:06 +00:00
2011-06-11 19:37:21 +00: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 09:27:57 +00:00
HRESULT hr = PD3DCompile ( code , len , NULL , pDefines , NULL , " main " , D3D : : PixelShaderVersionString ( ) ,
flags , 0 , & shaderBuffer , & errorBuffer ) ;
2011-06-11 19:37:21 +00:00
if ( errorBuffer )
{
INFO_LOG ( VIDEO , " Pixel shader compiler messages: \n %s " ,
( const char * ) errorBuffer - > GetBufferPointer ( ) ) ;
}
2010-06-13 19:50:06 +00:00
2011-06-11 19:37:21 +00:00
if ( FAILED ( hr ) )
2010-06-13 19:50:06 +00:00
{
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 ( ) ) ;
2011-06-11 19:37:21 +00:00
* blob = NULL ;
errorBuffer - > Release ( ) ;
2010-06-16 23:25:19 +00:00
}
else
{
2011-06-11 19:37:21 +00:00
* blob = new D3DBlob ( shaderBuffer ) ;
shaderBuffer - > Release ( ) ;
2010-06-13 19:50:06 +00:00
}
2011-06-11 19:37:21 +00:00
return SUCCEEDED ( hr ) ;
2010-06-13 19:50:06 +00:00
}
2011-06-11 19:37:21 +00:00
ID3D11VertexShader * CompileAndCreateVertexShader ( const char * code ,
unsigned int len )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00:00
D3DBlob * blob = NULL ;
if ( CompileVertexShader ( code , len , & blob ) )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D11VertexShader * v_shader = CreateVertexShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return v_shader ;
2010-06-13 19:50:06 +00:00
}
2011-06-11 19:37:21 +00: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 09:38:29 +00:00
{
2011-06-11 19:37:21 +00:00
ID3D11GeometryShader * g_shader = CreateGeometryShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return g_shader ;
2011-03-14 09:38:29 +00:00
}
2011-06-11 19:37:21 +00:00
return NULL ;
2011-03-14 09:38:29 +00:00
}
2011-06-11 19:37:21 +00:00
ID3D11PixelShader * CompileAndCreatePixelShader ( const char * code ,
unsigned int len )
2010-06-13 19:50:06 +00:00
{
2011-06-11 19:37:21 +00:00
D3DBlob * blob = NULL ;
CompilePixelShader ( code , len , & blob ) ;
2010-06-13 19:50:06 +00:00
if ( blob )
{
2011-06-11 19:37:21 +00:00
ID3D11PixelShader * p_shader = CreatePixelShaderFromByteCode ( blob ) ;
blob - > Release ( ) ;
return p_shader ;
2010-06-13 19:50:06 +00:00
}
2011-06-11 19:37:21 +00:00
return NULL ;
2010-06-13 19:50:06 +00:00
}
} // namespace
2011-01-29 20:16:51 +00:00
2013-10-29 05:23:17 +00:00
} // namespace DX11