]> git.cworth.org Git - apitrace/blob - wrappers/d3dcommonshader.cpp
2ae804b9223c6f16a8a6a9bc26c71fb38a9488df
[apitrace] / wrappers / d3dcommonshader.cpp
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 "d3dcommonshader.hpp"
31
32
33 struct ID3D10Blob : public IUnknown {
34 public:
35     virtual LPVOID STDMETHODCALLTYPE GetBufferPointer( void) = 0;
36     virtual SIZE_T STDMETHODCALLTYPE GetBufferSize( void) = 0;
37 };
38
39 typedef ID3D10Blob ID3DBlob;
40 typedef ID3DBlob* LPD3DBLOB;
41
42 #define D3D_DISASM_ENABLE_COLOR_CODE            0x00000001
43 #define D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS  0x00000002
44 #define D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING 0x00000004
45 #define D3D_DISASM_ENABLE_INSTRUCTION_CYCLE     0x00000008
46 #define D3D_DISASM_DISABLE_DEBUG_INFO           0x00000010
47 #define D3D_DISASM_ENABLE_INSTRUCTION_OFFSET    0x00000020
48 #define D3D_DISASM_INSTRUCTION_ONLY             0x00000040
49
50 typedef HRESULT
51 (WINAPI *PFND3DDISASSEMBLE)(
52     LPCVOID pSrcData,
53     SIZE_T SrcDataSize,
54     UINT Flags,
55     LPCSTR szComments,
56     ID3DBlob **ppDisassembly
57 );
58
59 static PFND3DDISASSEMBLE pfnD3DDisassemble = NULL;
60
61 typedef HRESULT
62 (WINAPI *PFND3D10DISASSEMBLESHADER)(
63     const void *pShader,
64     SIZE_T BytecodeLength,
65     BOOL EnableColorCode,
66     LPCSTR pComments,
67     ID3D10Blob **ppDisassembly
68 );
69
70 static PFND3D10DISASSEMBLESHADER pfnD3D10DisassembleShader = NULL;
71
72 void DumpShader(trace::Writer &writer, const void *pShaderBytecode, SIZE_T BytecodeLength)
73 {
74     static bool firsttime = true;
75
76     if (firsttime) {
77         char szFilename[MAX_PATH];
78         HMODULE hModule = NULL;
79
80         int version;
81         for (version = 44; version >= 33; --version) {
82             _snprintf(szFilename, sizeof(szFilename), "d3dcompiler_%i.dll", version);
83             hModule = LoadLibraryA(szFilename);
84             if (hModule) {
85                 pfnD3DDisassemble = (PFND3DDISASSEMBLE)
86                     GetProcAddress(hModule, "D3DDisassemble");
87                 if (pfnD3DDisassemble) {
88                     break;
89                 }
90             }
91         }
92
93         if (!pfnD3DDisassemble) {
94             /*
95              * Fallback to D3D10DisassembleShader, which should be always present.
96              */
97             if (GetSystemDirectoryA(szFilename, MAX_PATH)) {
98                 strcat(szFilename, "\\d3d10.dll");
99                 hModule = LoadLibraryA(szFilename);
100                 if (hModule) {
101                     pfnD3D10DisassembleShader = (PFND3D10DISASSEMBLESHADER)
102                         GetProcAddress(hModule, "D3D10DisassembleShader");
103                 }
104             }
105         }
106
107         firsttime = false;
108     }
109
110     LPD3DBLOB pDisassembly = NULL;
111     HRESULT hr = E_FAIL;
112
113     if (pfnD3DDisassemble) {
114         hr = pfnD3DDisassemble(pShaderBytecode, BytecodeLength, 0, NULL, &pDisassembly);
115     } else if (pfnD3D10DisassembleShader) {
116         hr = pfnD3D10DisassembleShader(pShaderBytecode, BytecodeLength, 0, NULL, &pDisassembly);
117     }
118
119     if (SUCCEEDED(hr)) {
120         writer.beginRepr();
121         writer.writeString((const char *)pDisassembly->GetBufferPointer(), pDisassembly->GetBufferSize());
122     }
123
124     writer.writeBlob(pShaderBytecode, BytecodeLength);
125
126     if (pDisassembly) {
127         pDisassembly->Release();
128     }
129     
130     if (SUCCEEDED(hr)) {
131         writer.endRepr();
132     }
133 }