Disassemble D3D9 shaders.
authorJosé Fonseca <jfonseca@vmware.com>
Wed, 22 Jul 2009 17:14:52 +0000 (18:14 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Wed, 22 Jul 2009 17:14:52 +0000 (18:14 +0100)
SConstruct
d3d9.py
d3dshader.py [new file with mode: 0644]

index 60e956a..05ff800 100644 (file)
@@ -231,7 +231,7 @@ if has_d3d8:
 if has_d3d9:
     env.Command(
         target = 'd3d9.cpp', 
-        source = ['d3d9.py', 'd3d9types.py', 'd3d9caps.py', 'windows.py', 'base.py'],
+        source = ['d3d9.py', 'd3d9types.py', 'd3d9caps.py', 'd3dshader.py', 'windows.py', 'base.py'],
         action = 'python $SOURCE > $TARGET',
     )
         
diff --git a/d3d9.py b/d3d9.py
index d398d58..1b95014 100644 (file)
--- a/d3d9.py
+++ b/d3d9.py
 """d3d9.h"""
 
 from windows import *
+from d3dshader import *
 from d3d9types import *
 from d3d9caps import *
 
+D3DSHADER9 = D3DShader(9)
+
 HRESULT = Enum("HRESULT", [
     "D3D_OK",
     "D3DERR_WRONGTEXTUREFORMAT",
@@ -204,7 +207,7 @@ IDirect3DDevice9.methods += [
     Method(HRESULT, "GetVertexDeclaration", [(OutPointer(PDIRECT3DVERTEXDECLARATION9), "ppDecl")]),
     Method(HRESULT, "SetFVF", [(DWORD, "FVF")]),
     Method(HRESULT, "GetFVF", [(OutPointer(DWORD), "pFVF")]),
-    Method(HRESULT, "CreateVertexShader", [(ConstPointer(DWORD), "pFunction"), (OutPointer(PDIRECT3DVERTEXSHADER9), "ppShader")]),
+    Method(HRESULT, "CreateVertexShader", [(D3DSHADER9, "pFunction"), (OutPointer(PDIRECT3DVERTEXSHADER9), "ppShader")]),
     Method(HRESULT, "SetVertexShader", [(PDIRECT3DVERTEXSHADER9, "pShader")]),
     Method(HRESULT, "GetVertexShader", [(OutPointer(PDIRECT3DVERTEXSHADER9), "ppShader")]),
     Method(HRESULT, "SetVertexShaderConstantF", [(UINT, "StartRegister"), (ConstPointer(Float), "pConstantData"), (UINT, "Vector4fCount")]),
@@ -219,7 +222,7 @@ IDirect3DDevice9.methods += [
     Method(HRESULT, "GetStreamSourceFreq", [(UINT, "StreamNumber"), (OutPointer(UINT), "pSetting")]),
     Method(HRESULT, "SetIndices", [(PDIRECT3DINDEXBUFFER9, "pIndexData")]),
     Method(HRESULT, "GetIndices", [(OutPointer(PDIRECT3DINDEXBUFFER9), "ppIndexData")]),
-    Method(HRESULT, "CreatePixelShader", [(ConstPointer(DWORD), "pFunction"), (OutPointer(PDIRECT3DPIXELSHADER9), "ppShader")]),
+    Method(HRESULT, "CreatePixelShader", [(D3DSHADER9, "pFunction"), (OutPointer(PDIRECT3DPIXELSHADER9), "ppShader")]),
     Method(HRESULT, "SetPixelShader", [(PDIRECT3DPIXELSHADER9, "pShader")]),
     Method(HRESULT, "GetPixelShader", [(OutPointer(PDIRECT3DPIXELSHADER9), "ppShader")]),
     Method(HRESULT, "SetPixelShaderConstantF", [(UINT, "StartRegister"), (ConstPointer(Float), "pConstantData"), (UINT, "Vector4fCount")]),
diff --git a/d3dshader.py b/d3dshader.py
new file mode 100644 (file)
index 0000000..30a6519
--- /dev/null
@@ -0,0 +1,91 @@
+#############################################################################
+#
+# Copyright 2009 VMware, Inc.
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#############################################################################
+
+
+from base import Type
+
+
+class D3DShader(Type):
+
+    def __init__(self, version):
+        self.version = version
+        Type.__init__(self, "const DWORD *")
+
+    def decl(self):
+        print '#include <d3dx9.h>'
+        print '#include <stdio.h>'
+        print 
+        print 'void Dump%s(const DWORD *tokens);' % (self.id)
+    
+    def impl(self):
+        print '''
+typedef HRESULT 
+(WINAPI *PD3DXDISASSEMBLESHADER)(
+   CONST DWORD *pShader,
+   BOOL EnableColorCode,
+   LPCSTR pComments,
+   LPD3DXBUFFER *ppDisassembly
+);
+        '''
+        print 'void Dump%s(const DWORD *tokens)' % (self.id)
+        print '''{
+    static BOOL firsttime = TRUE;
+    static HMODULE hD3DXModule = NULL; 
+    static PD3DXDISASSEMBLESHADER pfnD3DXDisassembleShader = NULL;
+
+    if(firsttime) {
+        if(!hD3DXModule) {
+            unsigned release;
+            unsigned version;
+            for(release = 0; release <= 1; ++release) {
+                /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */ 
+                for(version = 41; version >= 24; --version) {
+                    char filename[256];
+                    _snprintf(filename, sizeof(filename), 
+                              "d3dx9%s_%u.dll", release ? "" : "d", version);
+                    hD3DXModule = LoadLibraryA(filename);
+                    if(hD3DXModule)
+                        goto found;
+                }
+            }
+found:
+            ;
+        }
+
+        if (hD3DXModule)
+            if(!pfnD3DXDisassembleShader)
+                pfnD3DXDisassembleShader = (PD3DXDISASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXDisassembleShader");
+
+        firsttime = FALSE;
+    }
+   
+    if(pfnD3DXDisassembleShader) {
+        LPD3DXBUFFER pDisassembly = NULL;
+   
+        if (pfnD3DXDisassembleShader( (DWORD *)tokens, FALSE, NULL, &pDisassembly) == D3D_OK)
+            Log::DumpString((char *)pDisassembly->GetBufferPointer());
+
+        if(pDisassembly)
+            pDisassembly->Release();
+    }
+}
+'''
+    
+    def dump(self, instance):
+        print '    Dump%s(%s);' % (self.id, instance)