]> git.cworth.org Git - apitrace/blob - retrace/d3d9state.cpp
99cb6037655247d9825fb009a0cec9946bb8431d
[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 "d3dshader.hpp"
33 #include "json.hpp"
34
35
36 namespace d3dstate {
37
38
39 template< class T >
40 inline void
41 dumpShader(JSONWriter &json, const char *name, T *pShader) {
42     if (!pShader) {
43         return;
44     }
45
46     HRESULT hr;
47
48     UINT SizeOfData = 0;
49
50     hr = pShader->GetFunction(NULL, &SizeOfData);
51     if (SUCCEEDED(hr)) {
52         void *pData;
53         pData = malloc(SizeOfData);
54         if (pData) {
55             hr = pShader->GetFunction(pData, &SizeOfData);
56             if (SUCCEEDED(hr)) {
57                 IDisassemblyBuffer *pDisassembly = NULL;
58                 hr = DisassembleShader((const DWORD *)pData, &pDisassembly);
59                 if (SUCCEEDED(hr)) {
60                     json.beginMember(name);
61                     json.writeString((const char *)pDisassembly->GetBufferPointer() /*, pDisassembly->GetBufferSize() */);
62                     json.endMember();
63                     pDisassembly->Release();
64                 }
65
66             }
67             free(pData);
68         }
69     }
70
71     pShader->Release();
72 }
73
74 static void
75 dumpShaders(JSONWriter &json, IDirect3DDevice9 *pDevice)
76 {
77     json.beginMember("shaders");
78
79     HRESULT hr;
80     json.beginObject();
81
82     IDirect3DVertexShader9 *pVertexShader = NULL;
83     hr = pDevice->GetVertexShader(&pVertexShader);
84     if (SUCCEEDED(hr)) {
85         dumpShader(json, "vertex", pVertexShader);
86     }
87
88     IDirect3DPixelShader9 *pPixelShader = NULL;
89     hr = pDevice->GetPixelShader(&pPixelShader);
90     if (SUCCEEDED(hr)) {
91         dumpShader(json, "pixel", pPixelShader);
92     }
93
94     json.endObject();
95     json.endMember(); // shaders
96 }
97
98 void
99 dumpDevice(std::ostream &os, IDirect3DDevice9 *pDevice)
100 {
101     JSONWriter json(os);
102
103     dumpShaders(json, pDevice);
104
105     /* TODO */
106 }
107
108
109 } /* namespace d3dstate */