]> git.cworth.org Git - apitrace/blob - retrace/d3d8state.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / retrace / d3d8state.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 VMware, Inc.
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 "d3d8imports.hpp"
32 #include "d3dshader.hpp"
33 #include "d3dstate.hpp"
34 #include "json.hpp"
35
36
37 namespace d3dstate {
38
39
40 typedef HRESULT (STDMETHODCALLTYPE IDirect3DDevice8::*GetShaderFunctionMethod)(DWORD Handle, void* pData, DWORD* pSizeOfData);
41
42
43 struct VertexShaderGetter
44 {
45     HRESULT GetShader(IDirect3DDevice8 *pDevice, DWORD *pHandle) {
46         return pDevice->GetVertexShader(pHandle);
47     }
48     HRESULT GetShaderFunction(IDirect3DDevice8 *pDevice, DWORD Handle, void* pData, DWORD* pSizeOfData) {
49         return pDevice->GetVertexShaderFunction(Handle, pData, pSizeOfData);
50     }
51 };
52
53 struct PixelShaderGetter
54 {
55     HRESULT GetShader(IDirect3DDevice8 *pDevice, DWORD *pHandle) {
56         return pDevice->GetPixelShader(pHandle);
57     }
58     HRESULT GetShaderFunction(IDirect3DDevice8 *pDevice, DWORD Handle, void* pData, DWORD* pSizeOfData) {
59         return pDevice->GetPixelShaderFunction(Handle, pData, pSizeOfData);
60     }
61 };
62
63 template<class Getter>
64 inline void
65 dumpShader(JSONWriter &json, IDirect3DDevice8 *pDevice, const char *name) {
66     Getter getter;
67     HRESULT hr;
68
69     DWORD dwShader = 0;
70     hr = getter.GetShader(pDevice, &dwShader);
71     if (FAILED(hr) || !dwShader) {
72         return;
73     }
74
75     DWORD SizeOfData = 0;
76
77     hr = getter.GetShaderFunction(pDevice, dwShader, NULL, &SizeOfData);
78     if (SUCCEEDED(hr)) {
79         void *pData;
80         pData = malloc(SizeOfData);
81         if (pData) {
82             hr = getter.GetShaderFunction(pDevice, dwShader, pData, &SizeOfData);
83             if (SUCCEEDED(hr)) {
84                 IDisassemblyBuffer *pDisassembly = NULL;
85                 hr = DisassembleShader((const DWORD *)pData, &pDisassembly);
86                 if (SUCCEEDED(hr)) {
87                     json.beginMember(name);
88                     json.writeString((const char *)pDisassembly->GetBufferPointer() /*, pDisassembly->GetBufferSize() */);
89                     json.endMember();
90                     pDisassembly->Release();
91                 }
92
93             }
94             free(pData);
95         }
96     }
97 }
98
99 static void
100 dumpShaders(JSONWriter &json, IDirect3DDevice8 *pDevice)
101 {
102     json.beginMember("shaders");
103     json.beginObject();
104
105     dumpShader<VertexShaderGetter>(json, pDevice, "vertex");
106     dumpShader<PixelShaderGetter>(json, pDevice, "pixel");
107
108     json.endObject();
109     json.endMember(); // shaders
110 }
111
112 void
113 dumpDevice(std::ostream &os, IDirect3DDevice8 *pDevice)
114 {
115     JSONWriter json(os);
116
117     /* TODO */
118     json.beginMember("parameters");
119     json.beginObject();
120     json.endObject();
121     json.endMember(); // parameters
122
123     dumpShaders(json, pDevice);
124
125     json.beginMember("textures");
126     json.beginObject();
127     json.endObject();
128     json.endMember(); // textures
129
130     dumpFramebuffer(json, pDevice);
131 }
132
133
134 } /* namespace d3dstate */