José Fonseca | 3b18682 | 2012-11-27 15:25:21 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * Copyright 2008-2009 VMware, Inc. |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | * |
| 25 | **************************************************************************/ |
| 26 | |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include "d3dshader.hpp" |
| 31 | #include "os.hpp" |
| 32 | |
| 33 | |
| 34 | struct ID3DXBuffer : public IUnknown { |
| 35 | virtual LPVOID STDMETHODCALLTYPE GetBufferPointer(void) = 0; |
| 36 | virtual DWORD STDMETHODCALLTYPE GetBufferSize(void) = 0; |
| 37 | }; |
| 38 | |
| 39 | typedef HRESULT |
| 40 | (WINAPI *PD3DXDISASSEMBLESHADER)( |
| 41 | CONST DWORD *pShader, |
| 42 | BOOL EnableColorCode, |
| 43 | LPCSTR pComments, |
| 44 | ID3DXBuffer **ppDisassembly |
| 45 | ); |
| 46 | |
| 47 | |
| 48 | HRESULT |
| 49 | DisassembleShader(const DWORD *tokens, IDisassemblyBuffer **ppDisassembly) |
| 50 | { |
| 51 | static BOOL firsttime = TRUE; |
| 52 | |
| 53 | /* |
| 54 | * TODO: Consider using d3dcompile_xx.dll per |
| 55 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx |
| 56 | */ |
| 57 | |
| 58 | static HMODULE hD3DXModule = NULL; |
| 59 | static PD3DXDISASSEMBLESHADER pfnD3DXDisassembleShader = NULL; |
| 60 | |
| 61 | if (firsttime) { |
| 62 | if (!hD3DXModule) { |
| 63 | unsigned release; |
| 64 | int version; |
| 65 | for (release = 0; release <= 1; ++release) { |
| 66 | /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */ |
| 67 | for (version = 41; version >= 0; --version) { |
| 68 | char filename[256]; |
| 69 | _snprintf(filename, sizeof(filename), |
| 70 | "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version); |
| 71 | hD3DXModule = LoadLibraryA(filename); |
| 72 | if (hD3DXModule) |
| 73 | goto found; |
| 74 | } |
| 75 | } |
| 76 | found: |
| 77 | ; |
| 78 | } |
| 79 | |
| 80 | if (hD3DXModule) { |
| 81 | if (!pfnD3DXDisassembleShader) { |
| 82 | pfnD3DXDisassembleShader = (PD3DXDISASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXDisassembleShader"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | firsttime = FALSE; |
| 87 | } |
| 88 | |
| 89 | HRESULT hr = E_FAIL; |
| 90 | if (pfnD3DXDisassembleShader) { |
| 91 | hr = pfnD3DXDisassembleShader(tokens, FALSE, NULL, |
| 92 | reinterpret_cast<ID3DXBuffer **>(ppDisassembly)); |
| 93 | } |
| 94 | return hr; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | struct ID3D10Blob : public IUnknown { |
| 99 | virtual LPVOID STDMETHODCALLTYPE GetBufferPointer(void) = 0; |
| 100 | virtual SIZE_T STDMETHODCALLTYPE GetBufferSize(void) = 0; |
| 101 | }; |
| 102 | |
| 103 | typedef ID3D10Blob ID3DBlob; |
| 104 | |
| 105 | #define D3D_DISASM_ENABLE_COLOR_CODE 0x00000001 |
| 106 | #define D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS 0x00000002 |
| 107 | #define D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING 0x00000004 |
| 108 | #define D3D_DISASM_ENABLE_INSTRUCTION_CYCLE 0x00000008 |
| 109 | #define D3D_DISASM_DISABLE_DEBUG_INFO 0x00000010 |
| 110 | #define D3D_DISASM_ENABLE_INSTRUCTION_OFFSET 0x00000020 |
| 111 | #define D3D_DISASM_INSTRUCTION_ONLY 0x00000040 |
| 112 | |
| 113 | typedef HRESULT |
| 114 | (WINAPI *PFND3DDISASSEMBLE)( |
| 115 | LPCVOID pSrcData, |
| 116 | SIZE_T SrcDataSize, |
| 117 | UINT Flags, |
| 118 | LPCSTR szComments, |
| 119 | ID3DBlob **ppDisassembly |
| 120 | ); |
| 121 | |
| 122 | static PFND3DDISASSEMBLE pfnD3DDisassemble = NULL; |
| 123 | |
| 124 | typedef HRESULT |
| 125 | (WINAPI *PFND3D10DISASSEMBLESHADER)( |
| 126 | const void *pShader, |
| 127 | SIZE_T BytecodeLength, |
| 128 | BOOL EnableColorCode, |
| 129 | LPCSTR pComments, |
| 130 | ID3D10Blob **ppDisassembly |
| 131 | ); |
| 132 | |
| 133 | static PFND3D10DISASSEMBLESHADER pfnD3D10DisassembleShader = NULL; |
| 134 | |
| 135 | |
| 136 | HRESULT |
| 137 | DisassembleShader(const void *pShaderBytecode, SIZE_T BytecodeLength, IDisassemblyBuffer **ppDisassembly) |
| 138 | { |
| 139 | static bool firsttime = true; |
| 140 | |
| 141 | if (firsttime) { |
| 142 | char szFilename[MAX_PATH]; |
| 143 | HMODULE hModule = NULL; |
| 144 | int version; |
José Fonseca | 96f2963 | 2014-10-02 15:56:57 +0100 | [diff] [blame] | 145 | for (version = 47; version >= 33; --version) { |
José Fonseca | 3b18682 | 2012-11-27 15:25:21 +0000 | [diff] [blame] | 146 | _snprintf(szFilename, sizeof(szFilename), "d3dcompiler_%i.dll", version); |
| 147 | hModule = LoadLibraryA(szFilename); |
| 148 | if (hModule) { |
| 149 | pfnD3DDisassemble = (PFND3DDISASSEMBLE) |
| 150 | GetProcAddress(hModule, "D3DDisassemble"); |
| 151 | if (pfnD3DDisassemble) { |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | if (!pfnD3DDisassemble) { |
José Fonseca | 96f2963 | 2014-10-02 15:56:57 +0100 | [diff] [blame] | 157 | os::log("apitrace: warning: failed to load d3dcompiler_xx.dll\n"); |
| 158 | |
José Fonseca | 3b18682 | 2012-11-27 15:25:21 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Fallback to D3D10DisassembleShader, which should be always present. |
| 161 | */ |
| 162 | if (GetSystemDirectoryA(szFilename, MAX_PATH)) { |
| 163 | strcat(szFilename, "\\d3d10.dll"); |
| 164 | hModule = LoadLibraryA(szFilename); |
| 165 | if (hModule) { |
| 166 | pfnD3D10DisassembleShader = (PFND3D10DISASSEMBLESHADER) |
| 167 | GetProcAddress(hModule, "D3D10DisassembleShader"); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | firsttime = false; |
| 173 | } |
| 174 | |
| 175 | HRESULT hr = E_FAIL; |
| 176 | |
| 177 | if (pfnD3DDisassemble) { |
| 178 | hr = pfnD3DDisassemble(pShaderBytecode, BytecodeLength, 0, NULL, |
| 179 | reinterpret_cast<ID3DBlob **>(ppDisassembly)); |
| 180 | } else if (pfnD3D10DisassembleShader) { |
| 181 | hr = pfnD3D10DisassembleShader(pShaderBytecode, BytecodeLength, 0, NULL, |
| 182 | reinterpret_cast<ID3D10Blob **>(ppDisassembly)); |
| 183 | } |
| 184 | |
| 185 | return hr; |
| 186 | } |