]> git.cworth.org Git - apitrace/blob - wrappers/d3d10shader.cpp
dxva: Eliminate the globals hack.
[apitrace] / wrappers / d3d10shader.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 "d3d10shader.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
60 static HMODULE hD3DCompilerModule = NULL;
61 static PFND3DDISASSEMBLE pfnD3DDisassemble = NULL;
62
63
64 void DumpShader(trace::Writer &writer, const void *pShaderBytecode, SIZE_T BytecodeLength)
65 {
66     static BOOL firsttime = TRUE;
67
68     if (firsttime) {
69         if (!hD3DCompilerModule) {
70             int version;
71             for (version = 44; version >= 33; --version) {
72                 char filename[256];
73                 _snprintf(filename, sizeof(filename), "d3dcompiler_%u.dll", version);
74                 hD3DCompilerModule = LoadLibraryA(filename);
75                 if (hD3DCompilerModule) {
76                     break;
77                 }
78             }
79         }
80
81         if (hD3DCompilerModule) {
82             if (!pfnD3DDisassemble) {
83                 pfnD3DDisassemble = (PFND3DDISASSEMBLE)GetProcAddress(hD3DCompilerModule, "D3DDisassemble");
84             }
85         }
86
87         firsttime = FALSE;
88     }
89
90     /*
91      * TODO: Fallback to D3D10DisassembleShader, which should be always present.
92      */
93
94     LPD3DBLOB pDisassembly = NULL;
95     HRESULT hr = E_FAIL;
96
97     if (pfnD3DDisassemble) {
98         hr = pfnD3DDisassemble(pShaderBytecode, BytecodeLength, 0, NULL, &pDisassembly);
99     }
100
101     if (SUCCEEDED(hr)) {
102         writer.beginRepr();
103         writer.writeString((const char *)pDisassembly->GetBufferPointer(), pDisassembly->GetBufferSize());
104     }
105
106     writer.writeBlob(pShaderBytecode, BytecodeLength);
107
108     if (pDisassembly) {
109         pDisassembly->Release();
110     }
111     
112     if (SUCCEEDED(hr)) {
113         writer.endRepr();
114     }
115 }