Skip to content

Commit 3c4f8a4

Browse files
committed
move instance extras
1 parent d5ee586 commit 3c4f8a4

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

examples/cube.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import wgpu
1616
import numpy as np
17+
from wgpu.backends.wgpu_native.extras import set_instance_extras
18+
1719

1820
from rendercanvas.auto import RenderCanvas, loop
1921

@@ -455,6 +457,11 @@ async def draw_frame_async():
455457
uniform_data = np.zeros((), dtype=uniform_dtype)
456458

457459

460+
# TODO: remove testing code
461+
set_instance_extras(
462+
backends=1 << 3 # DX12 only
463+
)
464+
458465
print("Available adapters on this system:")
459466
for a in wgpu.gpu.enumerate_adapters_sync():
460467
print(a.summary)

wgpu/backends/wgpu_native/_api.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -315,30 +315,6 @@ def check_struct(struct_name, d):
315315
raise ValueError(f"Invalid keys in {struct_name}: {invalid_keys}")
316316

317317

318-
# forcing in the dynamic dx12 compiler to see if it works...
319-
dxil_path = r"C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll"
320-
dxc_path = r"C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxcompiler.dll"
321-
322-
# H: chain: WGPUChainedStruct, backends: WGPUInstanceBackend/int, flags: WGPUInstanceFlag/int, dx12ShaderCompiler: WGPUDx12Compiler, gles3MinorVersion: WGPUGles3MinorVersion, glFenceBehaviour: WGPUGLFenceBehaviour, dxilPath: WGPUStringView, dxcPath: WGPUStringView, dxcMaxShaderModel: WGPUDxcMaxShaderModel
323-
_instance_extras = new_struct_p(
324-
"WGPUInstanceExtras *",
325-
backends=8, # lib.WGPUInstanceBackend_DX12, #TODO: this isn't evaluated for importing or available in flags.py
326-
dx12ShaderCompiler=lib.WGPUDx12Compiler_Dxc,
327-
dxilPath=to_c_string_view(dxil_path),
328-
dxcPath=to_c_string_view(dxc_path),
329-
dxcMaxShaderModel=lib.WGPUDxcMaxShaderModel_V6_7,
330-
flags=3, # lib.WGPUInstanceFlag_Debug, # figure out if this works or not.
331-
# not used: chain
332-
# not used: gles3MinorVersion
333-
# not used: glFenceBehaviour
334-
)
335-
_instance_extras.chain.sType = lib.WGPUSType_InstanceExtras
336-
337-
338-
def get_wgpu_instance_dx12():
339-
return get_wgpu_instance(extras=_instance_extras)
340-
341-
342318
def _get_limits(id: int, device: bool = False, adapter: bool = False):
343319
"""Gets the limits for a device or an adapter"""
344320
assert device + adapter == 1 # exactly one is set
@@ -1290,6 +1266,7 @@ def device_lost_callback(c_device, c_reason, c_message, userdata1, userdata2):
12901266
def uncaptured_error_callback(
12911267
c_device, c_type, c_message, userdata1, userdata2
12921268
):
1269+
# TODO: does this always raise an exception? retest the loop cases!
12931270
error_type = enum_int2str["ErrorType"].get(c_type, "Unknown")
12941271
msg = from_c_string_view(c_message)
12951272
msg = "\n".join(line.rstrip() for line in msg.splitlines())

wgpu/backends/wgpu_native/extras.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
from typing import List
33

44
from . import GPUCommandEncoder, GPUComputePassEncoder, GPURenderPassEncoder
5-
from ._api import Dict, GPUBindGroupLayout, enums, logger, structs
5+
from ._api import (
6+
Dict,
7+
GPUBindGroupLayout,
8+
enums,
9+
logger,
10+
structs,
11+
new_struct_p,
12+
to_c_string_view,
13+
)
614
from ...enums import Enum
15+
from ._helpers import get_wgpu_instance
716

817

918
# NOTE: these functions represent backend-specific extra API.
@@ -172,3 +181,48 @@ def write_timestamp(encoder, query_set, query_index):
172181
encoder, (GPURenderPassEncoder, GPUComputePassEncoder, GPUCommandEncoder)
173182
)
174183
encoder._write_timestamp(query_set, query_index)
184+
185+
186+
def set_instance_extras(
187+
backends=0, # default all
188+
flags=0,
189+
dx12_compiler="fxc", # default, alternative "dxc"
190+
gles3_minor_version=0,
191+
fence_behavior=0,
192+
dxil_path: os.PathLike | None = None,
193+
dxc_path: os.PathLike | None = None,
194+
dxc_max_shader_model: float = 6.5,
195+
):
196+
"""
197+
Sets the global instance with extras.
198+
"""
199+
# TODO document and explain, find reference for defaults
200+
201+
# maybe include wgpu.h enums and flags in our codegen? (especially make sure to evaluate the flags)
202+
compiler_map = {
203+
"undefined": 0, # lib.WGPUDx12Compiler_Undefined
204+
"fxc": 1, # lib.WGPUDx12Compiler_Fxc
205+
"dxc": 2, # lib.WGPUDx12Compiler_Dxc
206+
}
207+
c_dx12_compiler = compiler_map.get(dx12_compiler, 0) # default to "undefined"?
208+
# the rust conv layer does all the checking, so fallbacks are handled there.
209+
210+
# TODO: translate to C enums/flags
211+
# H: chain: WGPUChainedStruct, backends: WGPUInstanceBackend/int, flags: WGPUInstanceFlag/int, dx12ShaderCompiler: WGPUDx12Compiler, gles3MinorVersion: WGPUGles3MinorVersion, glFenceBehaviour: WGPUGLFenceBehaviour, dxilPath: WGPUStringView, dxcPath: WGPUStringView, dxcMaxShaderModel: WGPUDxcMaxShaderModel
212+
c_extras = new_struct_p(
213+
"WGPUInstanceExtras *",
214+
# not used: chain
215+
backends=backends,
216+
flags=flags, # lib.WGPUInstanceFlag_Debug, # figure out if this works or not.
217+
dx12ShaderCompiler=c_dx12_compiler,
218+
gles3MinorVersion=gles3_minor_version,
219+
glFenceBehaviour=fence_behavior,
220+
dxilPath=to_c_string_view(dxil_path),
221+
dxcPath=to_c_string_view(dxc_path),
222+
# dxcMaxShaderModel=lib.WGPUDxcMaxShaderModel_V6_5,
223+
)
224+
225+
c_extras.chain.sType = (
226+
0x00030006 # lib.WGPUSType_InstanceExtras (but we don't import lib here?)
227+
)
228+
get_wgpu_instance(extras=c_extras) # this sets a global

0 commit comments

Comments
 (0)