目前新版DXSDK 2008.6以后的BorderColor是使用float4类型,而在360里仍然使用DWORD,如果不做转换则通过词法分析后会导致unhandled exception.
另外360上编译shader速度相当慢,最好读取编译过的shader,但是不能在PC上编,因为POWERPC和X86字节序不同。
编译函数如下
1
ID3DXBuffer* CompileEffectOutput( const char* InFileName )
2
{
3
assert( InFileName && "Input file name could not be empty!" );
4
5
//Create output file name
6
std::string OutFileName = std::string( InFileName ) + "o";
7
8
//Check output file exist
9
ID3DXBuffer *pCompiledData = NULL;
10
std::ifstream fin;
11
fin.open( OutFileName.c_str(), std::ios_base::in | std::ios_base::binary );
12
if( !fin )
13
{
14
//First time to create binary data.
15
ID3DXBuffer *pErrorMsg = NULL;
16
ID3DXEffectCompiler *pCompiler = NULL;
17
HRESULT hr = S_OK;
18
try
19
{
20
//Load file
21
fin.clear();
22
fin.open( InFileName, std::ios_base::in | std::ios_base::binary );
23
if( !fin )
24
{
25
assert( false && "Could not open input file, do you forget prefix : [Game:\\\\] ?" );
26
throw std::runtime_error( "Open input file failed" );
27
}
28
fin.seekg( 0,std::ios_base::end );
29
std::streamsize fileSize = fin.tellg();
30
fin.seekg( 0,std::ios_base::beg );
31
char *fileBuf = new char[fileSize];
32
fin.read( fileBuf, fileSize );
33
fin.close();
34
35
36
//Create compiler
37
hr = D3DXCreateEffectCompiler( fileBuf, fileSize, NULL, NULL, 0, &pCompiler, &pErrorMsg );
38
if( FAILED(hr) )
39
{
40
if( pErrorMsg )
41
OutputDebugStringA( static_cast<char*>( pErrorMsg->GetBufferPointer() ) );
42
assert( false && "Create Effect Compiler failed!" );
43
throw std::runtime_error( "Create Effect Compiler failed" );
44
}
45
46
47
//Compile Effect to create compiled binary data
48
hr = pCompiler->CompileEffect( 0, &pCompiledData, &pErrorMsg );
49
if( FAILED(hr) )
50
{
51
if( pErrorMsg )
52
OutputDebugStringA( static_cast<char*>( pErrorMsg->GetBufferPointer() ) );
53
assert( false && "Compile Effect failed!" );
54
throw std::runtime_error( "Compile Effect failed" );
55
}
56
}
57
catch( std::runtime_error& )
58
{
59
URelease( pCompiler );
60
URelease( pCompiledData );
61
URelease( pErrorMsg );
62
throw;
63
}
64
65
66
//Save compiled effect data to file
67
std::ofstream fout;
68
fout.open( OutFileName.c_str(), std::ios_base::out | std::ios_base::binary );
69
if( !fout )
70
{
71
assert( false && "Open output failed!" );
72
throw std::runtime_error( "Open output failed" );
73
}
74
fout.write( static_cast<char*>( pCompiledData->GetBufferPointer() ), pCompiledData->GetBufferSize() );
75
fout.close();
76
}
77
else
78
{
79
//Load compiled data
80
fin.seekg( 0,std::ios_base::end );
81
std::streamsize fileSize = fin.tellg();
82
fin.seekg( 0,std::ios_base::beg );
83
char *fileBuf = new char[fileSize];
84
fin.read( fileBuf, fileSize );
85
fin.close();
86
87
HRESULT hr = D3DXCreateBuffer( fileSize, &pCompiledData );
88
if( FAILED( hr ) )
89
{
90
URelease( pCompiledData );
91
assert( false && "Could not create buffer." );
92
throw std::runtime_error( "Create buffer failed" );
93
}
94
memcpy( pCompiledData->GetBufferPointer(), fileBuf, fileSize );
95
delete [] fileBuf;
96
}
97
98
//Return compiled data
99
assert( pCompiledData );
100
return pCompiledData;
101
}
ID3DXBuffer* CompileEffectOutput( const char* InFileName )2
{3
assert( InFileName && "Input file name could not be empty!" );4

5
//Create output file name6
std::string OutFileName = std::string( InFileName ) + "o";7

8
//Check output file exist9
ID3DXBuffer *pCompiledData = NULL;10
std::ifstream fin;11
fin.open( OutFileName.c_str(), std::ios_base::in | std::ios_base::binary );12
if( !fin )13
{14
//First time to create binary data.15
ID3DXBuffer *pErrorMsg = NULL;16
ID3DXEffectCompiler *pCompiler = NULL;17
HRESULT hr = S_OK;18
try19
{20
//Load file21
fin.clear();22
fin.open( InFileName, std::ios_base::in | std::ios_base::binary );23
if( !fin )24
{25
assert( false && "Could not open input file, do you forget prefix : [Game:\\\\] ?" );26
throw std::runtime_error( "Open input file failed" );27
}28
fin.seekg( 0,std::ios_base::end );29
std::streamsize fileSize = fin.tellg();30
fin.seekg( 0,std::ios_base::beg );31
char *fileBuf = new char[fileSize];32
fin.read( fileBuf, fileSize );33
fin.close();34

35

36
//Create compiler37
hr = D3DXCreateEffectCompiler( fileBuf, fileSize, NULL, NULL, 0, &pCompiler, &pErrorMsg );38
if( FAILED(hr) )39
{40
if( pErrorMsg )41
OutputDebugStringA( static_cast<char*>( pErrorMsg->GetBufferPointer() ) );42
assert( false && "Create Effect Compiler failed!" ); 43
throw std::runtime_error( "Create Effect Compiler failed" );44
}45

46

47
//Compile Effect to create compiled binary data 48
hr = pCompiler->CompileEffect( 0, &pCompiledData, &pErrorMsg ); 49
if( FAILED(hr) )50
{51
if( pErrorMsg )52
OutputDebugStringA( static_cast<char*>( pErrorMsg->GetBufferPointer() ) ); 53
assert( false && "Compile Effect failed!" );54
throw std::runtime_error( "Compile Effect failed" );55
}56
}57
catch( std::runtime_error& )58
{59
URelease( pCompiler );60
URelease( pCompiledData );61
URelease( pErrorMsg );62
throw;63
}64
65

66
//Save compiled effect data to file67
std::ofstream fout;68
fout.open( OutFileName.c_str(), std::ios_base::out | std::ios_base::binary );69
if( !fout )70
{71
assert( false && "Open output failed!" );72
throw std::runtime_error( "Open output failed" );73
}74
fout.write( static_cast<char*>( pCompiledData->GetBufferPointer() ), pCompiledData->GetBufferSize() );75
fout.close();76
}77
else78
{79
//Load compiled data80
fin.seekg( 0,std::ios_base::end );81
std::streamsize fileSize = fin.tellg();82
fin.seekg( 0,std::ios_base::beg );83
char *fileBuf = new char[fileSize];84
fin.read( fileBuf, fileSize );85
fin.close();86

87
HRESULT hr = D3DXCreateBuffer( fileSize, &pCompiledData );88
if( FAILED( hr ) )89
{90
URelease( pCompiledData );91
assert( false && "Could not create buffer." ); 92
throw std::runtime_error( "Create buffer failed" );93
}94
memcpy( pCompiledData->GetBufferPointer(), fileBuf, fileSize );95
delete [] fileBuf;96
}97

98
//Return compiled data99
assert( pCompiledData );100
return pCompiledData;101
}
-----------------------------------------------------------
每天进步一点


浙公网安备 33010602011771号