1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
19 #include <d3dcompiler.h>
22 #include "VideoConfig.h"
23 #include "D3DShader.h"
29 ID3D11VertexShader
* CreateVertexShaderFromByteCode(void* bytecode
, unsigned int len
)
31 ID3D11VertexShader
* v_shader
;
32 HRESULT hr
= D3D::device
->CreateVertexShader(bytecode
, len
, NULL
, &v_shader
);
35 PanicAlert("CreateVertexShaderFromByteCode failed from %p (size %d) at %s %d\n", bytecode
, len
, __FILE__
, __LINE__
);
42 bool CompileVertexShader(const char* code
, unsigned int len
, D3DBlob
** blob
)
44 ID3D10Blob
* shaderBuffer
= NULL
;
45 ID3D10Blob
* errorBuffer
= NULL
;
47 #if defined(_DEBUG) || defined(DEBUGFAST)
48 UINT flags
= D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY
|D3D10_SHADER_DEBUG
|D3D10_SHADER_WARNINGS_ARE_ERRORS
;
50 UINT flags
= D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY
|D3D10_SHADER_OPTIMIZATION_LEVEL3
|D3D10_SHADER_SKIP_VALIDATION
;
52 HRESULT hr
= PD3DX11CompileFromMemory(code
, len
, NULL
, NULL
, NULL
, "main", D3D::VertexShaderVersionString(),
53 flags
, 0, NULL
, &shaderBuffer
, &errorBuffer
, NULL
);
55 if (FAILED(hr
) || errorBuffer
)
57 std::string msg
= (char*)errorBuffer
->GetBufferPointer();
60 MessageBoxA(0, msg
.c_str(), "Error compiling pixel shader", MB_ICONERROR
);
63 errorBuffer
->Release();
67 *blob
= new D3DBlob(shaderBuffer
);
68 shaderBuffer
->Release();
74 ID3D11PixelShader
* CreatePixelShaderFromByteCode(void* bytecode
, unsigned int len
)
76 ID3D11PixelShader
* p_shader
;
77 HRESULT hr
= D3D::device
->CreatePixelShader(bytecode
, len
, NULL
, &p_shader
);
80 PanicAlert("CreatePixelShaderFromByteCode failed at %s %d\n", __FILE__
, __LINE__
);
87 bool CompilePixelShader(const char* code
, unsigned int len
, D3DBlob
** blob
)
89 ID3D10Blob
* shaderBuffer
= NULL
;
90 ID3D10Blob
* errorBuffer
= NULL
;
92 #if defined(_DEBUG) || defined(DEBUGFAST)
93 UINT flags
= D3D10_SHADER_DEBUG
|D3D10_SHADER_WARNINGS_ARE_ERRORS
;
95 UINT flags
= D3D10_SHADER_OPTIMIZATION_LEVEL3
;
97 HRESULT hr
= PD3DX11CompileFromMemory(code
, len
, NULL
, NULL
, NULL
, "main", D3D::PixelShaderVersionString(),
98 flags
, 0, NULL
, &shaderBuffer
, &errorBuffer
, NULL
);
100 if (FAILED(hr
) || errorBuffer
)
102 std::string msg
= (char*)errorBuffer
->GetBufferPointer();
105 MessageBoxA(0, msg
.c_str(), "Error compiling pixel shader", MB_ICONERROR
);
108 errorBuffer
->Release();
112 *blob
= new D3DBlob(shaderBuffer
);
113 shaderBuffer
->Release();
115 return SUCCEEDED(hr
);
118 ID3D11VertexShader
* CompileAndCreateVertexShader(const char* code
, unsigned int len
)
120 D3DBlob
* blob
= NULL
;
121 if (CompileVertexShader(code
, len
, &blob
))
123 ID3D11VertexShader
* v_shader
= CreateVertexShaderFromByteCode(blob
);
127 PanicAlert("Failed to compile and create vertex shader from %p (size %d) at %s %d\n", code
, len
, __FILE__
, __LINE__
);
131 ID3D11PixelShader
* CompileAndCreatePixelShader(const char* code
, unsigned int len
)
133 D3DBlob
* blob
= NULL
;
134 CompilePixelShader(code
, len
, &blob
);
137 ID3D11PixelShader
* p_shader
= CreatePixelShaderFromByteCode(blob
);
141 PanicAlert("Failed to compile and create pixel shader, %s %d\n", __FILE__
, __LINE__
);