Skip to content

Commit f78b128

Browse files
author
Dmitry Andreev
committed
Update fxproc.py
Upgrade to v0.1.9 - Making it Python3 and multi-threading friendly
1 parent f3eeb75 commit f78b128

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

fxproc.py

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""fxproc.py @ https://github.com/coderand/pyfxproc
22
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.
44
License Creative Commons Zero v1.0 Universal.
55
"""
66

7-
__version__ = '0.1.8'
7+
__version__ = '0.1.9'
88
__all__ = ["Effect"]
99

1010
import os
@@ -14,13 +14,14 @@
1414

1515
from ctypes import WINFUNCTYPE, Structure
1616
from ctypes.wintypes import *
17-
from ctypes.wintypes import HRESULT
17+
HRESULT = DWORD
1818

1919
# Direct3D9 constants
2020
D3D_SDK_VERSION = 32
2121
D3DADAPTER_DEFAULT = 0
2222
D3DDEVTYPE_HAL = 1
2323
D3DDEVTYPE_REF = 2
24+
D3DCREATE_MULTITHREADED = 0x00000004
2425
D3DCREATE_SOFTWARE_VERTEXPROCESSING = 0x00000020
2526
D3DCREATE_HARDWARE_VERTEXPROCESSING = 0x00000040
2627
D3DCREATE_MIXED_VERTEXPROCESSING = 0x00000080
@@ -291,6 +292,7 @@ class QUAD_VTX(Structure):
291292
print("WARNING: d3dx9_43.dll not found, falling back to lower version")
292293

293294
Direct3DCreate9 = getattr(d3d9_dll, 'Direct3DCreate9')
295+
Direct3DCreate9.restype = LPVOID
294296

295297
D3DXCreateEffectFromFile = getattr(d3dx9_43_dll, 'D3DXCreateEffectFromFileA')
296298
D3DXCreateEffectFromFile.argtypes = [LPVOID, LPCSTR, LPVOID, LPVOID, DWORD, LPVOID, LPVOID, LPVOID]
@@ -322,7 +324,7 @@ class QUAD_VTX(Structure):
322324
if not lpD3D9:
323325
raise Exception("Failed to create D3D")
324326

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)
326328

327329
if hWnd == 0:
328330
raise Exception("Failed to create window")
@@ -332,7 +334,7 @@ class QUAD_VTX(Structure):
332334
d3dpp = D3DPRESENT_PARAMETERS(Windowed=1, SwapEffect=D3DSWAPEFFECT_DISCARD)
333335

334336
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))
336338

337339
#:TODO: Try different configurations when one fails
338340
#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 = ""):
372374
slices = volume_desc.Depth
373375
desc.Width = volume_desc.Width
374376
desc.Height = volume_desc.Height
377+
desc.Format = volume_desc.Format
375378

376379
else:
377380
raise TypeError("Unknown resource type")
@@ -437,6 +440,7 @@ class Effect :
437440

438441
all_effects = []
439442
curr_target_size = (0, 0)
443+
begin_called = False
440444

441445
def __init__(self, d3d_effect, name = ""):
442446
assert(d3d_effect)
@@ -457,7 +461,7 @@ def open(fx_name):
457461

458462
try:
459463
D3DXCreateEffectFromFile(
460-
lpDevice, fx_name, NULL, NULL,
464+
lpDevice, fx_name.encode('ascii'), NULL, NULL,
461465
D3DXFX_NOT_CLONEABLE | D3DXSHADER_SKIPOPTIMIZATION, NULL,
462466
ctypes.byref(d3d_effect), ctypes.byref(errors)
463467
)
@@ -474,7 +478,7 @@ def fromstring(text):
474478

475479
try:
476480
D3DXCreateEffect(
477-
lpDevice, text, len(text), NULL, NULL,
481+
lpDevice, text.encode('ascii'), len(text), NULL, NULL,
478482
D3DXFX_NOT_CLONEABLE | D3DXSHADER_SKIPOPTIMIZATION, NULL,
479483
ctypes.byref(d3d_effect), ctypes.byref(errors)
480484
)
@@ -501,18 +505,18 @@ def loadTexture(file_name, levels=0):
501505
info = D3DXIMAGE_INFO()
502506

503507
try:
504-
D3DXGetImageInfoFromFile(file_name, ctypes.byref(info))
508+
D3DXGetImageInfoFromFile(file_name.encode('ascii'), ctypes.byref(info))
505509

506510
if info.ResourceType == D3DRTYPE_CUBETEXTURE:
507511
D3DXCreateCubeTextureFromFileEx(
508-
lpDevice, file_name, D3DX_DEFAULT_NONPOW2,
512+
lpDevice, file_name.encode('ascii'), D3DX_DEFAULT_NONPOW2,
509513
int(levels), 0, info.Format, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
510514
0, NULL, NULL, ctypes.byref(texture)
511515
)
512516

513517
elif info.ResourceType == D3DRTYPE_TEXTURE:
514518
D3DXCreateTextureFromFileEx(
515-
lpDevice, file_name, D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
519+
lpDevice, file_name.encode('ascii'), D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
516520
int(levels), 0, info.Format, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
517521
0, NULL, NULL, ctypes.byref(texture)
518522
)
@@ -536,7 +540,7 @@ def saveTexture(pyobj, file_name):
536540
format = D3DXIMAGE_FILEFORMAT.by_str[ext]
537541

538542
try:
539-
D3DXSaveTextureToFile(file_name, format, pyobj.d3d_texture, NULL)
543+
D3DXSaveTextureToFile(file_name.encode('ascii'), format, pyobj.d3d_texture, NULL)
540544
except:
541545
raise IOError("Can't save texture " '"%s"' % (file_name))
542546

@@ -627,16 +631,18 @@ def __beginScene(self, technique_name):
627631
w = Effect.curr_target_size[0]
628632
h = Effect.curr_target_size[1]
629633

630-
IDirect3DDevice9_BeginScene(lpDevice)
634+
if not Effect.begin_called:
635+
IDirect3DDevice9_BeginScene(lpDevice)
636+
Effect.begin_called = True
631637

632638
vec = D3DXVECTOR4(w, h, 1.0 / w, 1.0 / h)
633639
try:
634-
ID3DXEffect_SetVector(self.d3d_effect, "vTargetSize", ctypes.byref(vec))
640+
ID3DXEffect_SetVector(self.d3d_effect, b"vTargetSize", ctypes.byref(vec))
635641
except:
636642
pass
637643

638644
try:
639-
ID3DXEffect_SetTechnique(self.d3d_effect, technique_name)
645+
ID3DXEffect_SetTechnique(self.d3d_effect, technique_name.encode('ascii'))
640646
except WindowsError:
641647
raise ValueError('Can\'t set technique "%s"' % (technique_name))
642648

@@ -666,9 +672,11 @@ def drawQuad(self, technique_name, do_flush=True):
666672
ID3DXEffect_EndPass(self.d3d_effect)
667673

668674
ID3DXEffect_End(self.d3d_effect)
669-
IDirect3DDevice9_EndScene(lpDevice)
670675

671-
if do_flush: self.flush()
676+
if do_flush:
677+
IDirect3DDevice9_EndScene(lpDevice)
678+
Effect.begin_called = False
679+
self.flush()
672680

673681
@staticmethod
674682
def createTris(tri_count):
@@ -691,9 +699,11 @@ def drawTris(self, tri_list, technique_name, do_flush=True):
691699
ID3DXEffect_EndPass(self.d3d_effect)
692700

693701
ID3DXEffect_End(self.d3d_effect)
694-
IDirect3DDevice9_EndScene(lpDevice)
695702

696-
if do_flush: self.flush()
703+
if do_flush:
704+
IDirect3DDevice9_EndScene(lpDevice)
705+
Effect.begin_called = False
706+
self.flush()
697707

698708
@staticmethod
699709
def flush():
@@ -705,23 +715,23 @@ def flush():
705715

706716
def setFloat(self, name, x):
707717
try:
708-
ID3DXEffect_SetFloat(self.d3d_effect, name, x)
718+
ID3DXEffect_SetFloat(self.d3d_effect, name.encode('ascii'), x)
709719
except WindowsError:
710720
raise ValueError('Can\'t set float "%s"' % (name))
711721

712722
def setFloat4(self, name, x, y=0.0, z=0.0, w=0.0):
713723
vec = D3DXVECTOR4(x, y, z, w)
714724

715725
try:
716-
ID3DXEffect_SetVector(self.d3d_effect, name, ctypes.byref(vec))
726+
ID3DXEffect_SetVector(self.d3d_effect, name.encode('ascii'), ctypes.byref(vec))
717727
except WindowsError:
718728
raise ValueError('Can\'t set vector "%s"' % (name))
719729

720730
def setTexture(self, name, pyobj):
721731
Texture.check_type_of(pyobj)
722732

723733
try:
724-
ID3DXEffect_SetTexture(self.d3d_effect, name, pyobj.d3d_texture)
734+
ID3DXEffect_SetTexture(self.d3d_effect, name.encode('ascii'), pyobj.d3d_texture)
725735
except WindowsError:
726736
raise ValueError('Can\'t set texture "%s"' % (name))
727737

0 commit comments

Comments
 (0)