blob: 9b9aa1939a86a7ed227536d66d166642abf4676c [file] [log] [blame]
José Fonsecacbb86172012-12-12 07:38:42 +00001/**************************************************************************
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
José Fonseca1df5ed82014-06-13 10:45:46 +010031#include "json.hpp"
32#include "com_ptr.hpp"
José Fonsecacbb86172012-12-12 07:38:42 +000033#include "d3d8imports.hpp"
34#include "d3dshader.hpp"
35#include "d3dstate.hpp"
José Fonsecacbb86172012-12-12 07:38:42 +000036
37
38namespace d3dstate {
39
40
41typedef HRESULT (STDMETHODCALLTYPE IDirect3DDevice8::*GetShaderFunctionMethod)(DWORD Handle, void* pData, DWORD* pSizeOfData);
42
43
44struct VertexShaderGetter
45{
46 HRESULT GetShader(IDirect3DDevice8 *pDevice, DWORD *pHandle) {
47 return pDevice->GetVertexShader(pHandle);
48 }
49 HRESULT GetShaderFunction(IDirect3DDevice8 *pDevice, DWORD Handle, void* pData, DWORD* pSizeOfData) {
50 return pDevice->GetVertexShaderFunction(Handle, pData, pSizeOfData);
51 }
52};
53
54struct PixelShaderGetter
55{
56 HRESULT GetShader(IDirect3DDevice8 *pDevice, DWORD *pHandle) {
57 return pDevice->GetPixelShader(pHandle);
58 }
59 HRESULT GetShaderFunction(IDirect3DDevice8 *pDevice, DWORD Handle, void* pData, DWORD* pSizeOfData) {
60 return pDevice->GetPixelShaderFunction(Handle, pData, pSizeOfData);
61 }
62};
63
64template<class Getter>
65inline void
66dumpShader(JSONWriter &json, IDirect3DDevice8 *pDevice, const char *name) {
67 Getter getter;
68 HRESULT hr;
69
70 DWORD dwShader = 0;
71 hr = getter.GetShader(pDevice, &dwShader);
72 if (FAILED(hr) || !dwShader) {
73 return;
74 }
75
76 DWORD SizeOfData = 0;
77
78 hr = getter.GetShaderFunction(pDevice, dwShader, NULL, &SizeOfData);
79 if (SUCCEEDED(hr)) {
80 void *pData;
81 pData = malloc(SizeOfData);
82 if (pData) {
83 hr = getter.GetShaderFunction(pDevice, dwShader, pData, &SizeOfData);
84 if (SUCCEEDED(hr)) {
José Fonseca1df5ed82014-06-13 10:45:46 +010085 com_ptr<IDisassemblyBuffer> pDisassembly;
José Fonsecacbb86172012-12-12 07:38:42 +000086 hr = DisassembleShader((const DWORD *)pData, &pDisassembly);
87 if (SUCCEEDED(hr)) {
88 json.beginMember(name);
89 json.writeString((const char *)pDisassembly->GetBufferPointer() /*, pDisassembly->GetBufferSize() */);
90 json.endMember();
José Fonsecacbb86172012-12-12 07:38:42 +000091 }
José Fonsecacbb86172012-12-12 07:38:42 +000092 }
93 free(pData);
94 }
95 }
96}
97
98static void
99dumpShaders(JSONWriter &json, IDirect3DDevice8 *pDevice)
100{
101 json.beginMember("shaders");
102 json.beginObject();
103
104 dumpShader<VertexShaderGetter>(json, pDevice, "vertex");
105 dumpShader<PixelShaderGetter>(json, pDevice, "pixel");
106
107 json.endObject();
108 json.endMember(); // shaders
109}
110
111void
112dumpDevice(std::ostream &os, IDirect3DDevice8 *pDevice)
113{
114 JSONWriter json(os);
115
116 /* TODO */
117 json.beginMember("parameters");
118 json.beginObject();
119 json.endObject();
120 json.endMember(); // parameters
121
122 dumpShaders(json, pDevice);
123
124 json.beginMember("textures");
125 json.beginObject();
126 json.endObject();
127 json.endMember(); // textures
128
129 dumpFramebuffer(json, pDevice);
130}
131
132
133} /* namespace d3dstate */