Drop d3dcompiler_xx.dll dependence, using D3DX11CompileFromMemory instead.
[dolphin.git] / Source / Plugins / Plugin_VideoDX11 / Src / D3DShader.cpp
blob0a2b719e5d324487214e020d07476146b3086f36
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/
18 #include <d3dx11.h>
19 #include <d3dcompiler.h>
20 #include <string>
22 #include "VideoConfig.h"
23 #include "D3DShader.h"
25 namespace D3D
28 // bytecode->shader
29 ID3D11VertexShader* CreateVertexShaderFromByteCode(void* bytecode, unsigned int len)
31 ID3D11VertexShader* v_shader;
32 HRESULT hr = D3D::device->CreateVertexShader(bytecode, len, NULL, &v_shader);
33 if (FAILED(hr))
35 PanicAlert("CreateVertexShaderFromByteCode failed from %p (size %d) at %s %d\n", bytecode, len, __FILE__, __LINE__);
36 v_shader = NULL;
38 return v_shader;
41 // code->bytecode
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;
49 #else
50 UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY|D3D10_SHADER_OPTIMIZATION_LEVEL3|D3D10_SHADER_SKIP_VALIDATION;
51 #endif
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();
58 msg += "\n\n";
59 msg += code;
60 MessageBoxA(0, msg.c_str(), "Error compiling pixel shader", MB_ICONERROR);
62 *blob = NULL;
63 errorBuffer->Release();
65 else
67 *blob = new D3DBlob(shaderBuffer);
68 shaderBuffer->Release();
70 return SUCCEEDED(hr);
73 // bytecode->shader
74 ID3D11PixelShader* CreatePixelShaderFromByteCode(void* bytecode, unsigned int len)
76 ID3D11PixelShader* p_shader;
77 HRESULT hr = D3D::device->CreatePixelShader(bytecode, len, NULL, &p_shader);
78 if (FAILED(hr))
80 PanicAlert("CreatePixelShaderFromByteCode failed at %s %d\n", __FILE__, __LINE__);
81 p_shader = NULL;
83 return p_shader;
86 // code->bytecode
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;
94 #else
95 UINT flags = D3D10_SHADER_OPTIMIZATION_LEVEL3;
96 #endif
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();
103 msg += "\n\n";
104 msg += code;
105 MessageBoxA(0, msg.c_str(), "Error compiling pixel shader", MB_ICONERROR);
107 *blob = NULL;
108 errorBuffer->Release();
110 else
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);
124 blob->Release();
125 return v_shader;
127 PanicAlert("Failed to compile and create vertex shader from %p (size %d) at %s %d\n", code, len, __FILE__, __LINE__);
128 return NULL;
131 ID3D11PixelShader* CompileAndCreatePixelShader(const char* code, unsigned int len)
133 D3DBlob* blob = NULL;
134 CompilePixelShader(code, len, &blob);
135 if (blob)
137 ID3D11PixelShader* p_shader = CreatePixelShaderFromByteCode(blob);
138 blob->Release();
139 return p_shader;
141 PanicAlert("Failed to compile and create pixel shader, %s %d\n", __FILE__, __LINE__);
142 return NULL;
145 } // namespace