José Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 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 | |
José Fonseca | 1df5ed8 | 2014-06-13 10:45:46 +0100 | [diff] [blame^] | 31 | #include "json.hpp" |
| 32 | #include "com_ptr.hpp" |
José Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 33 | #include "d3d8imports.hpp" |
| 34 | #include "d3dshader.hpp" |
| 35 | #include "d3dstate.hpp" |
José Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | namespace d3dstate { |
| 39 | |
| 40 | |
| 41 | typedef HRESULT (STDMETHODCALLTYPE IDirect3DDevice8::*GetShaderFunctionMethod)(DWORD Handle, void* pData, DWORD* pSizeOfData); |
| 42 | |
| 43 | |
| 44 | struct 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 | |
| 54 | struct 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 | |
| 64 | template<class Getter> |
| 65 | inline void |
| 66 | dumpShader(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é Fonseca | 1df5ed8 | 2014-06-13 10:45:46 +0100 | [diff] [blame^] | 85 | com_ptr<IDisassemblyBuffer> pDisassembly; |
José Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 86 | 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é Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 91 | } |
José Fonseca | cbb8617 | 2012-12-12 07:38:42 +0000 | [diff] [blame] | 92 | } |
| 93 | free(pData); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | dumpShaders(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 | |
| 111 | void |
| 112 | dumpDevice(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 */ |