]> git.cworth.org Git - apitrace/blob - retrace/d3d9state.cpp
73746e7d14c9543ec4e2f91bcd0ad3b78579e876
[apitrace] / retrace / d3d9state.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <stdio.h>
28
29 #include <iostream>
30
31 #include "d3d9imports.hpp"
32 #include "json.hpp"
33
34
35 namespace d3dstate {
36
37
38 typedef HRESULT
39 (WINAPI *PD3DXDISASSEMBLESHADER)(
40     CONST DWORD *pShader,
41     BOOL EnableColorCode,
42     LPCSTR pComments,
43     LPD3DXBUFFER *ppDisassembly
44 );
45
46
47 HRESULT
48 disassembleShader(const DWORD *tokens, LPD3DXBUFFER *ppDisassembly)
49 {
50     static BOOL firsttime = TRUE;
51
52     /*
53      * TODO: Consider using d3dcompile_xx.dll per
54      * http://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx
55      */
56
57     static HMODULE hD3DXModule = NULL;
58     static PD3DXDISASSEMBLESHADER pfnD3DXDisassembleShader = NULL;
59
60     if (firsttime) {
61         if (!hD3DXModule) {
62             unsigned release;
63             int version;
64             for (release = 0; release <= 1; ++release) {
65                 /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */
66                 for (version = 41; version >= 0; --version) {
67                     char filename[256];
68                     _snprintf(filename, sizeof(filename),
69                               "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
70                     hD3DXModule = LoadLibraryA(filename);
71                     if (hD3DXModule)
72                         goto found;
73                 }
74             }
75 found:
76             ;
77         }
78
79         if (hD3DXModule) {
80             if (!pfnD3DXDisassembleShader) {
81                 pfnD3DXDisassembleShader = (PD3DXDISASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXDisassembleShader");
82             }
83         }
84
85         firsttime = FALSE;
86     }
87
88     if (!pfnD3DXDisassembleShader) {
89         return E_FAIL;
90     }
91
92     return pfnD3DXDisassembleShader(tokens, FALSE, NULL, ppDisassembly);
93 }
94
95
96 template< class T >
97 inline void
98 dumpShader(JSONWriter &json, const char *name, T *pShader) {
99     if (!pShader) {
100         return;
101     }
102
103     HRESULT hr;
104
105     UINT SizeOfData = 0;
106
107     hr = pShader->GetFunction(NULL, &SizeOfData);
108     if (SUCCEEDED(hr)) {
109         void *pData;
110         pData = malloc(SizeOfData);
111         if (pData) {
112             hr = pShader->GetFunction(pData, &SizeOfData);
113             if (SUCCEEDED(hr)) {
114                 LPD3DXBUFFER pDisassembly;
115
116                 hr = disassembleShader((const DWORD *)pData, &pDisassembly);
117                 if (SUCCEEDED(hr)) {
118                     json.beginMember(name);
119                     json.writeString((const char *)pDisassembly->GetBufferPointer() /*, pDisassembly->GetBufferSize() */);
120                     json.endMember();
121                     pDisassembly->Release();
122                 }
123
124             }
125             free(pData);
126         }
127     }
128 }
129
130 static void
131 dumpShaders(JSONWriter &json, IDirect3DDevice9 *pDevice)
132 {
133     json.beginMember("shaders");
134
135     HRESULT hr;
136     json.beginObject();
137
138     IDirect3DVertexShader9 *pVertexShader = NULL;
139     hr = pDevice->GetVertexShader(&pVertexShader);
140     if (SUCCEEDED(hr)) {
141         dumpShader(json, "vertex", pVertexShader);
142     }
143
144     IDirect3DPixelShader9 *pPixelShader = NULL;
145     hr = pDevice->GetPixelShader(&pPixelShader);
146     if (SUCCEEDED(hr)) {
147         dumpShader(json, "pixel", pPixelShader);
148     }
149
150     json.endObject();
151     json.endMember(); // shaders
152 }
153
154 void
155 dumpDevice(std::ostream &os, IDirect3DDevice9 *pDevice)
156 {
157     JSONWriter json(os);
158
159     dumpShaders(json, pDevice);
160
161     /* TODO */
162 }
163
164
165 } /* namespace d3dstate */