1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
31 #include "d3d9imports.hpp"
39 (WINAPI *PD3DXDISASSEMBLESHADER)(
43 LPD3DXBUFFER *ppDisassembly
48 disassembleShader(const DWORD *tokens, LPD3DXBUFFER *ppDisassembly)
50 static BOOL firsttime = TRUE;
53 * TODO: Consider using d3dcompile_xx.dll per
54 * http://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx
57 static HMODULE hD3DXModule = NULL;
58 static PD3DXDISASSEMBLESHADER pfnD3DXDisassembleShader = NULL;
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) {
68 _snprintf(filename, sizeof(filename),
69 "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
70 hD3DXModule = LoadLibraryA(filename);
80 if (!pfnD3DXDisassembleShader) {
81 pfnD3DXDisassembleShader = (PD3DXDISASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXDisassembleShader");
88 if (!pfnD3DXDisassembleShader) {
92 return pfnD3DXDisassembleShader(tokens, FALSE, NULL, ppDisassembly);
98 dumpShader(JSONWriter &json, const char *name, T *pShader) {
107 hr = pShader->GetFunction(NULL, &SizeOfData);
110 pData = malloc(SizeOfData);
112 hr = pShader->GetFunction(pData, &SizeOfData);
114 LPD3DXBUFFER pDisassembly;
116 hr = disassembleShader((const DWORD *)pData, &pDisassembly);
118 json.beginMember(name);
119 json.writeString((const char *)pDisassembly->GetBufferPointer() /*, pDisassembly->GetBufferSize() */);
121 pDisassembly->Release();
131 dumpShaders(JSONWriter &json, IDirect3DDevice9 *pDevice)
133 json.beginMember("shaders");
138 IDirect3DVertexShader9 *pVertexShader = NULL;
139 hr = pDevice->GetVertexShader(&pVertexShader);
141 dumpShader(json, "vertex", pVertexShader);
144 IDirect3DPixelShader9 *pPixelShader = NULL;
145 hr = pDevice->GetPixelShader(&pPixelShader);
147 dumpShader(json, "pixel", pPixelShader);
151 json.endMember(); // shaders
155 dumpDevice(std::ostream &os, IDirect3DDevice9 *pDevice)
159 dumpShaders(json, pDevice);
165 } /* namespace d3dstate */