1
1
"""fxproc.py @ https://github.com/coderand/pyfxproc
2
2
Direct3D .fx file interface for GPU based data processing.
3
- Created by Dmitry "AND" Andreev 2013-2015 .
3
+ Created by Dmitry "AND" Andreev 2013-2021 .
4
4
License Creative Commons Zero v1.0 Universal.
5
5
"""
6
6
7
- __version__ = '0.1.8 '
7
+ __version__ = '0.1.9 '
8
8
__all__ = ["Effect" ]
9
9
10
10
import os
14
14
15
15
from ctypes import WINFUNCTYPE , Structure
16
16
from ctypes .wintypes import *
17
- from ctypes . wintypes import HRESULT
17
+ HRESULT = DWORD
18
18
19
19
# Direct3D9 constants
20
20
D3D_SDK_VERSION = 32
21
21
D3DADAPTER_DEFAULT = 0
22
22
D3DDEVTYPE_HAL = 1
23
23
D3DDEVTYPE_REF = 2
24
+ D3DCREATE_MULTITHREADED = 0x00000004
24
25
D3DCREATE_SOFTWARE_VERTEXPROCESSING = 0x00000020
25
26
D3DCREATE_HARDWARE_VERTEXPROCESSING = 0x00000040
26
27
D3DCREATE_MIXED_VERTEXPROCESSING = 0x00000080
@@ -291,6 +292,7 @@ class QUAD_VTX(Structure):
291
292
print ("WARNING: d3dx9_43.dll not found, falling back to lower version" )
292
293
293
294
Direct3DCreate9 = getattr (d3d9_dll , 'Direct3DCreate9' )
295
+ Direct3DCreate9 .restype = LPVOID
294
296
295
297
D3DXCreateEffectFromFile = getattr (d3dx9_43_dll , 'D3DXCreateEffectFromFileA' )
296
298
D3DXCreateEffectFromFile .argtypes = [LPVOID , LPCSTR , LPVOID , LPVOID , DWORD , LPVOID , LPVOID , LPVOID ]
@@ -322,7 +324,7 @@ class QUAD_VTX(Structure):
322
324
if not lpD3D9 :
323
325
raise Exception ("Failed to create D3D" )
324
326
325
- hWnd = CreateWindowEx (0 , "STATIC" , "fxproc_window" , WS_OVERLAPPEDWINDOW , 0 , 0 , 100 , 100 , 0 , 0 , 0 , 0 )
327
+ hWnd = CreateWindowEx (0 , "STATIC" . encode ( "ascii" ) , "fxproc_window" . encode ( "ascii" ) , WS_OVERLAPPEDWINDOW , 0 , 0 , 100 , 100 , 0 , 0 , 0 , 0 )
326
328
327
329
if hWnd == 0 :
328
330
raise Exception ("Failed to create window" )
@@ -332,7 +334,7 @@ class QUAD_VTX(Structure):
332
334
d3dpp = D3DPRESENT_PARAMETERS (Windowed = 1 , SwapEffect = D3DSWAPEFFECT_DISCARD )
333
335
334
336
try :
335
- D3D9_CreateDevice (lpD3D9 , D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , hWnd , D3DCREATE_HARDWARE_VERTEXPROCESSING , ctypes .byref (d3dpp ), ctypes .byref (lpDevice ))
337
+ D3D9_CreateDevice (lpD3D9 , D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , hWnd , D3DCREATE_MULTITHREADED | D3DCREATE_HARDWARE_VERTEXPROCESSING , ctypes .byref (d3dpp ), ctypes .byref (lpDevice ))
336
338
337
339
#:TODO: Try different configurations when one fails
338
340
#D3D9_CreateDevice(lpD3D9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, ctypes.byref(d3dpp), ctypes.byref(lpDevice))
@@ -372,6 +374,7 @@ def __init__(self, d3d_texture, name = ""):
372
374
slices = volume_desc .Depth
373
375
desc .Width = volume_desc .Width
374
376
desc .Height = volume_desc .Height
377
+ desc .Format = volume_desc .Format
375
378
376
379
else :
377
380
raise TypeError ("Unknown resource type" )
@@ -437,6 +440,7 @@ class Effect :
437
440
438
441
all_effects = []
439
442
curr_target_size = (0 , 0 )
443
+ begin_called = False
440
444
441
445
def __init__ (self , d3d_effect , name = "" ):
442
446
assert (d3d_effect )
@@ -457,7 +461,7 @@ def open(fx_name):
457
461
458
462
try :
459
463
D3DXCreateEffectFromFile (
460
- lpDevice , fx_name , NULL , NULL ,
464
+ lpDevice , fx_name . encode ( 'ascii' ) , NULL , NULL ,
461
465
D3DXFX_NOT_CLONEABLE | D3DXSHADER_SKIPOPTIMIZATION , NULL ,
462
466
ctypes .byref (d3d_effect ), ctypes .byref (errors )
463
467
)
@@ -474,7 +478,7 @@ def fromstring(text):
474
478
475
479
try :
476
480
D3DXCreateEffect (
477
- lpDevice , text , len (text ), NULL , NULL ,
481
+ lpDevice , text . encode ( 'ascii' ) , len (text ), NULL , NULL ,
478
482
D3DXFX_NOT_CLONEABLE | D3DXSHADER_SKIPOPTIMIZATION , NULL ,
479
483
ctypes .byref (d3d_effect ), ctypes .byref (errors )
480
484
)
@@ -501,18 +505,18 @@ def loadTexture(file_name, levels=0):
501
505
info = D3DXIMAGE_INFO ()
502
506
503
507
try :
504
- D3DXGetImageInfoFromFile (file_name , ctypes .byref (info ))
508
+ D3DXGetImageInfoFromFile (file_name . encode ( 'ascii' ) , ctypes .byref (info ))
505
509
506
510
if info .ResourceType == D3DRTYPE_CUBETEXTURE :
507
511
D3DXCreateCubeTextureFromFileEx (
508
- lpDevice , file_name , D3DX_DEFAULT_NONPOW2 ,
512
+ lpDevice , file_name . encode ( 'ascii' ) , D3DX_DEFAULT_NONPOW2 ,
509
513
int (levels ), 0 , info .Format , D3DPOOL_MANAGED , D3DX_DEFAULT , D3DX_DEFAULT ,
510
514
0 , NULL , NULL , ctypes .byref (texture )
511
515
)
512
516
513
517
elif info .ResourceType == D3DRTYPE_TEXTURE :
514
518
D3DXCreateTextureFromFileEx (
515
- lpDevice , file_name , D3DX_DEFAULT_NONPOW2 , D3DX_DEFAULT_NONPOW2 ,
519
+ lpDevice , file_name . encode ( 'ascii' ) , D3DX_DEFAULT_NONPOW2 , D3DX_DEFAULT_NONPOW2 ,
516
520
int (levels ), 0 , info .Format , D3DPOOL_MANAGED , D3DX_DEFAULT , D3DX_DEFAULT ,
517
521
0 , NULL , NULL , ctypes .byref (texture )
518
522
)
@@ -536,7 +540,7 @@ def saveTexture(pyobj, file_name):
536
540
format = D3DXIMAGE_FILEFORMAT .by_str [ext ]
537
541
538
542
try :
539
- D3DXSaveTextureToFile (file_name , format , pyobj .d3d_texture , NULL )
543
+ D3DXSaveTextureToFile (file_name . encode ( 'ascii' ) , format , pyobj .d3d_texture , NULL )
540
544
except :
541
545
raise IOError ("Can't save texture " '"%s"' % (file_name ))
542
546
@@ -627,16 +631,18 @@ def __beginScene(self, technique_name):
627
631
w = Effect .curr_target_size [0 ]
628
632
h = Effect .curr_target_size [1 ]
629
633
630
- IDirect3DDevice9_BeginScene (lpDevice )
634
+ if not Effect .begin_called :
635
+ IDirect3DDevice9_BeginScene (lpDevice )
636
+ Effect .begin_called = True
631
637
632
638
vec = D3DXVECTOR4 (w , h , 1.0 / w , 1.0 / h )
633
639
try :
634
- ID3DXEffect_SetVector (self .d3d_effect , "vTargetSize" , ctypes .byref (vec ))
640
+ ID3DXEffect_SetVector (self .d3d_effect , b "vTargetSize" , ctypes .byref (vec ))
635
641
except :
636
642
pass
637
643
638
644
try :
639
- ID3DXEffect_SetTechnique (self .d3d_effect , technique_name )
645
+ ID3DXEffect_SetTechnique (self .d3d_effect , technique_name . encode ( 'ascii' ) )
640
646
except WindowsError :
641
647
raise ValueError ('Can\' t set technique "%s"' % (technique_name ))
642
648
@@ -666,9 +672,11 @@ def drawQuad(self, technique_name, do_flush=True):
666
672
ID3DXEffect_EndPass (self .d3d_effect )
667
673
668
674
ID3DXEffect_End (self .d3d_effect )
669
- IDirect3DDevice9_EndScene (lpDevice )
670
675
671
- if do_flush : self .flush ()
676
+ if do_flush :
677
+ IDirect3DDevice9_EndScene (lpDevice )
678
+ Effect .begin_called = False
679
+ self .flush ()
672
680
673
681
@staticmethod
674
682
def createTris (tri_count ):
@@ -691,9 +699,11 @@ def drawTris(self, tri_list, technique_name, do_flush=True):
691
699
ID3DXEffect_EndPass (self .d3d_effect )
692
700
693
701
ID3DXEffect_End (self .d3d_effect )
694
- IDirect3DDevice9_EndScene (lpDevice )
695
702
696
- if do_flush : self .flush ()
703
+ if do_flush :
704
+ IDirect3DDevice9_EndScene (lpDevice )
705
+ Effect .begin_called = False
706
+ self .flush ()
697
707
698
708
@staticmethod
699
709
def flush ():
@@ -705,23 +715,23 @@ def flush():
705
715
706
716
def setFloat (self , name , x ):
707
717
try :
708
- ID3DXEffect_SetFloat (self .d3d_effect , name , x )
718
+ ID3DXEffect_SetFloat (self .d3d_effect , name . encode ( 'ascii' ) , x )
709
719
except WindowsError :
710
720
raise ValueError ('Can\' t set float "%s"' % (name ))
711
721
712
722
def setFloat4 (self , name , x , y = 0.0 , z = 0.0 , w = 0.0 ):
713
723
vec = D3DXVECTOR4 (x , y , z , w )
714
724
715
725
try :
716
- ID3DXEffect_SetVector (self .d3d_effect , name , ctypes .byref (vec ))
726
+ ID3DXEffect_SetVector (self .d3d_effect , name . encode ( 'ascii' ) , ctypes .byref (vec ))
717
727
except WindowsError :
718
728
raise ValueError ('Can\' t set vector "%s"' % (name ))
719
729
720
730
def setTexture (self , name , pyobj ):
721
731
Texture .check_type_of (pyobj )
722
732
723
733
try :
724
- ID3DXEffect_SetTexture (self .d3d_effect , name , pyobj .d3d_texture )
734
+ ID3DXEffect_SetTexture (self .d3d_effect , name . encode ( 'ascii' ) , pyobj .d3d_texture )
725
735
except WindowsError :
726
736
raise ValueError ('Can\' t set texture "%s"' % (name ))
727
737
0 commit comments