@@ -812,7 +812,30 @@ def _release(self):
812812
813813
814814class GPUObjectBase (classes .GPUObjectBase ):
815- pass
815+ def _release (self ):
816+ if self ._internal is not None and libf is not None :
817+ self ._internal , internal = None , self ._internal
818+ # H: void wgpuDeviceRelease(WGPUDevice device)
819+ # H: void wgpuBufferRelease(WGPUBuffer buffer)
820+ # H: void wgpuTextureRelease(WGPUTexture texture)
821+ # H: void wgpuTextureViewRelease(WGPUTextureView textureView)
822+ # H: void wgpuSamplerRelease(WGPUSampler sampler)
823+ # H: void wgpuBindGroupLayoutRelease(WGPUBindGroupLayout bindGroupLayout)
824+ # H: void wgpuBindGroupRelease(WGPUBindGroup bindGroup)
825+ # H: void wgpuPipelineLayoutRelease(WGPUPipelineLayout pipelineLayout)
826+ # H: void wgpuShaderModuleRelease(WGPUShaderModule shaderModule)
827+ # H: void wgpuComputePipelineRelease(WGPUComputePipeline computePipeline)
828+ # H: void wgpuRenderPipelineRelease(WGPURenderPipeline renderPipeline)
829+ # H: void wgpuCommandBufferRelease(WGPUCommandBuffer commandBuffer)
830+ # H: void wgpuCommandEncoderRelease(WGPUCommandEncoder commandEncoder)
831+ # H: void wgpuComputePassEncoderRelease(WGPUComputePassEncoder computePassEncoder)
832+ # H: void wgpuRenderPassEncoderRelease(WGPURenderPassEncoder renderPassEncoder)
833+ # H: void wgpuRenderBundleEncoderRelease(WGPURenderBundleEncoder renderBundleEncoder)
834+ # H: void wgpuQueueRelease(WGPUQueue queue)
835+ # H: void wgpuRenderBundleRelease(WGPURenderBundle renderBundle)
836+ # H: void wgpuQuerySetRelease(WGPUQuerySet querySet)
837+ function = type (self )._release_function
838+ function (internal )
816839
817840
818841class GPUAdapterInfo (classes .GPUAdapterInfo ):
@@ -1042,6 +1065,9 @@ def _release(self):
10421065
10431066
10441067class GPUDevice (classes .GPUDevice , GPUObjectBase ):
1068+ # GPUObjectBaseMixin
1069+ _release_function = libf .wgpuDeviceRelease
1070+
10451071 def __init__ (self , label , internal , adapter , features , limits , queue ):
10461072 super ().__init__ (label , internal , adapter , features , limits , queue )
10471073
@@ -1856,15 +1882,15 @@ def destroy(self):
18561882
18571883 def _release (self ):
18581884 if self ._queue is not None :
1859- self ._queue ._release ()
1860- self ._queue = None
1861- if self ._internal is not None and libf is not None :
1862- self ._internal , internal = None , self ._internal
1863- # H: void f(WGPUDevice device)
1864- libf .wgpuDeviceRelease (internal )
1885+ queue , self ._queue = self ._queue , None
1886+ queue ._release ()
1887+ super ()._release ()
18651888
18661889
18671890class GPUBuffer (classes .GPUBuffer , GPUObjectBase ):
1891+ # GPUObjectBaseMixin
1892+ _release_function = libf .wgpuBufferRelease
1893+
18681894 def __init__ (self , label , internal , device , size , usage , map_state ):
18691895 super ().__init__ (label , internal , device , size , usage , map_state )
18701896
@@ -2086,13 +2112,13 @@ def destroy(self):
20862112
20872113 def _release (self ):
20882114 self ._release_memoryviews ()
2089- if self ._internal is not None and libf is not None :
2090- self ._internal , internal = None , self ._internal
2091- # H: void f(WGPUBuffer buffer)
2092- libf .wgpuBufferRelease (internal )
2115+ super ()._release ()
20932116
20942117
20952118class GPUTexture (classes .GPUTexture , GPUObjectBase ):
2119+ # GPUObjectBaseMixin
2120+ _release_function = libf .wgpuTextureRelease
2121+
20962122 def create_view (
20972123 self ,
20982124 * ,
@@ -2150,54 +2176,36 @@ def destroy(self):
21502176 # H: void f(WGPUTexture texture)
21512177 libf .wgpuTextureDestroy (internal )
21522178
2153- def _release (self ):
2154- if self ._internal is not None and libf is not None :
2155- self ._internal , internal = None , self ._internal
2156- # H: void f(WGPUTexture texture)
2157- libf .wgpuTextureRelease (internal )
2158-
21592179
21602180class GPUTextureView (classes .GPUTextureView , GPUObjectBase ):
2161- def _release (self ):
2162- if self ._internal is not None and libf is not None :
2163- self ._internal , internal = None , self ._internal
2164- # H: void f(WGPUTextureView textureView)
2165- libf .wgpuTextureViewRelease (internal )
2181+ # GPUObjectBaseMixin
2182+ _release_function = libf .wgpuTextureViewRelease
21662183
21672184
21682185class GPUSampler (classes .GPUSampler , GPUObjectBase ):
2169- def _release (self ):
2170- if self ._internal is not None and libf is not None :
2171- self ._internal , internal = None , self ._internal
2172- # H: void f(WGPUSampler sampler)
2173- libf .wgpuSamplerRelease (internal )
2186+ # GPUObjectBaseMixin
2187+ _release_function = libf .wgpuSamplerRelease
21742188
21752189
21762190class GPUBindGroupLayout (classes .GPUBindGroupLayout , GPUObjectBase ):
2177- def _release (self ):
2178- if self ._internal is not None and libf is not None :
2179- self ._internal , internal = None , self ._internal
2180- # H: void f(WGPUBindGroupLayout bindGroupLayout)
2181- libf .wgpuBindGroupLayoutRelease (internal )
2191+ # GPUObjectBaseMixin
2192+ _release_function = libf .wgpuBindGroupLayoutRelease
21822193
21832194
21842195class GPUBindGroup (classes .GPUBindGroup , GPUObjectBase ):
2185- def _release (self ):
2186- if self ._internal is not None and libf is not None :
2187- self ._internal , internal = None , self ._internal
2188- # H: void f(WGPUBindGroup bindGroup)
2189- libf .wgpuBindGroupRelease (internal )
2196+ # GPUObjectBaseMixin
2197+ _release_function = libf .wgpuBindGroupRelease
21902198
21912199
21922200class GPUPipelineLayout (classes .GPUPipelineLayout , GPUObjectBase ):
2193- def _release (self ):
2194- if self ._internal is not None and libf is not None :
2195- self ._internal , internal = None , self ._internal
2196- # H: void f(WGPUPipelineLayout pipelineLayout)
2197- libf .wgpuPipelineLayoutRelease (internal )
2201+ # GPUObjectBaseMixin
2202+ _release_function = libf .wgpuPipelineLayoutRelease
21982203
21992204
22002205class GPUShaderModule (classes .GPUShaderModule , GPUObjectBase ):
2206+ # GPUObjectBaseMixin
2207+ _release_function = libf .wgpuShaderModuleRelease
2208+
22012209 def get_compilation_info (self ):
22022210 # Here's a little setup to implement this method. Unfortunately,
22032211 # this is not yet implemented in wgpu-native. Another problem
@@ -2229,12 +2237,6 @@ def get_compilation_info(self):
22292237
22302238 return []
22312239
2232- def _release (self ):
2233- if self ._internal is not None and libf is not None :
2234- self ._internal , internal = None , self ._internal
2235- # H: void f(WGPUShaderModule shaderModule)
2236- libf .wgpuShaderModuleRelease (internal )
2237-
22382240
22392241class GPUPipelineBase (classes .GPUPipelineBase ):
22402242 def get_bind_group_layout (self , index ):
@@ -2251,30 +2253,18 @@ def get_bind_group_layout(self, index):
22512253
22522254
22532255class GPUComputePipeline (classes .GPUComputePipeline , GPUPipelineBase , GPUObjectBase ):
2254- def _release (self ):
2255- if self ._internal is not None and libf is not None :
2256- self ._internal , internal = None , self ._internal
2257- # H: void f(WGPUComputePipeline computePipeline)
2258- libf .wgpuComputePipelineRelease (internal )
2256+ # GPUObjectBaseMixin
2257+ _release_function = libf .wgpuComputePipelineRelease
22592258
22602259
22612260class GPURenderPipeline (classes .GPURenderPipeline , GPUPipelineBase , GPUObjectBase ):
2262- def _release (self ):
2263- if self ._internal is not None and libf is not None :
2264- self ._internal , internal = None , self ._internal
2265- # H: void f(WGPURenderPipeline renderPipeline)
2266- libf .wgpuRenderPipelineRelease (internal )
2261+ # GPUObjectBaseMixin
2262+ _release_function = libf .wgpuRenderPipelineRelease
22672263
22682264
22692265class GPUCommandBuffer (classes .GPUCommandBuffer , GPUObjectBase ):
2270- def _release (self ):
2271- # Note that command buffers get destroyed when they are submitted.
2272- # In earlier versions we had to take this into account by setting
2273- # _internal to None. That seems not necessary anymore.
2274- if self ._internal is not None and libf is not None :
2275- self ._internal , internal = None , self ._internal
2276- # H: void f(WGPUCommandBuffer commandBuffer)
2277- libf .wgpuCommandBufferRelease (internal )
2266+ # GPUObjectBaseMixin
2267+ _release_function = libf .wgpuCommandBufferRelease
22782268
22792269
22802270class GPUCommandsMixin (classes .GPUCommandsMixin ):
@@ -2430,6 +2420,9 @@ class GPUCommandEncoder(
24302420 _pop_debug_group_function = libf .wgpuCommandEncoderPopDebugGroup
24312421 _insert_debug_marker_function = libf .wgpuCommandEncoderInsertDebugMarker
24322422
2423+ # GPUObjectBaseMixin
2424+ _release_function = libf .wgpuCommandEncoderRelease
2425+
24332426 def begin_compute_pass (
24342427 self , * , label = "" , timestamp_writes : "structs.ComputePassTimestampWrites" = None
24352428 ):
@@ -2822,14 +2815,6 @@ def resolve_query_set(
28222815 int (destination_offset ),
28232816 )
28242817
2825- def _release (self ):
2826- # Note that the native object gets destroyed on finish.
2827- # Also see GPUCommandBuffer._release()
2828- if self ._internal is not None and libf is not None :
2829- self ._internal , internal = None , self ._internal
2830- # H: void f(WGPUCommandEncoder commandEncoder)
2831- libf .wgpuCommandEncoderRelease (internal )
2832-
28332818
28342819class GPUComputePassEncoder (
28352820 classes .GPUComputePassEncoder ,
@@ -2847,6 +2832,9 @@ class GPUComputePassEncoder(
28472832 # GPUBindingCommandsMixin
28482833 _set_bind_group_function = libf .wgpuComputePassEncoderSetBindGroup
28492834
2835+ # GPUObjectBaseMixin
2836+ _release_function = libf .wgpuComputePassEncoderRelease
2837+
28502838 _ended = False
28512839
28522840 def set_pipeline (self , pipeline ):
@@ -2874,14 +2862,9 @@ def end(self):
28742862 libf .wgpuComputePassEncoderEnd (self ._internal )
28752863 self ._ended = True
28762864 # Need to release, see https://github.com/gfx-rs/wgpu-native/issues/412
2865+ # As of wgpu-native v22.1.0.5, this bug is still present.
28772866 self ._release ()
28782867
2879- def _release (self ):
2880- if self ._internal is not None and libf is not None :
2881- self ._internal , internal = None , self ._internal
2882- # H: void f(WGPUComputePassEncoder computePassEncoder)
2883- libf .wgpuComputePassEncoderRelease (internal )
2884-
28852868
28862869class GPURenderPassEncoder (
28872870 classes .GPURenderPassEncoder ,
@@ -2908,6 +2891,9 @@ class GPURenderPassEncoder(
29082891 _draw_indexed_function = libf .wgpuRenderPassEncoderDrawIndexed
29092892 _draw_indexed_indirect_function = libf .wgpuRenderPassEncoderDrawIndexedIndirect
29102893
2894+ # GPUObjectBaseMixin
2895+ _release_function = libf .wgpuRenderPassEncoderRelease
2896+
29112897 _ended = False
29122898
29132899 def set_viewport (self , x , y , width , height , min_depth , max_depth ):
@@ -2950,6 +2936,7 @@ def end(self):
29502936 libf .wgpuRenderPassEncoderEnd (self ._internal )
29512937 self ._ended = True
29522938 # Need to release, see https://github.com/gfx-rs/wgpu-native/issues/412
2939+ # As of wgpu-native v22.1.0.5, this bug is still present.
29532940 self ._release ()
29542941
29552942 def execute_bundles (self , bundles ):
@@ -2997,12 +2984,6 @@ def _set_push_constants(self, visibility, offset, size_in_bytes, data, data_offs
29972984 self ._internal , int (visibility ), offset , size , c_data + data_offset
29982985 )
29992986
3000- def _release (self ):
3001- if self ._internal is not None and libf is not None :
3002- self ._internal , internal = None , self ._internal
3003- # H: void f(WGPURenderPassEncoder renderPassEncoder)
3004- libf .wgpuRenderPassEncoderRelease (internal )
3005-
30062987
30072988class GPURenderBundleEncoder (
30082989 classes .GPURenderBundleEncoder ,
@@ -3029,6 +3010,9 @@ class GPURenderBundleEncoder(
30293010 _draw_indexed_function = libf .wgpuRenderBundleEncoderDrawIndexed
30303011 _draw_indexed_indirect_function = libf .wgpuRenderBundleEncoderDrawIndexedIndirect
30313012
3013+ # GPUObjectBaseMixin
3014+ _release_function = libf .wgpuRenderBundleEncoderRelease
3015+
30323016 def finish (self , * , label = "" ):
30333017 # H: nextInChain: WGPUChainedStruct *, label: char *
30343018 struct = new_struct_p (
@@ -3038,16 +3022,15 @@ def finish(self, *, label=""):
30383022 )
30393023 # H: WGPURenderBundle f(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderBundleDescriptor const * descriptor)
30403024 id = libf .wgpuRenderBundleEncoderFinish (self ._internal , struct )
3025+ # The other encoders require that we call self._release() when
3026+ # we're done with it. But that doesn't seem to be an issue here.
30413027 return GPURenderBundle (label , id , self ._device )
30423028
3043- def _release (self ):
3044- if self ._internal is not None and libf is not None :
3045- self ._internal , internal = None , self ._internal
3046- # H: void f(WGPURenderBundleEncoder renderBundleEncoder)
3047- libf .wgpuRenderBundleEncoderRelease (internal )
3048-
30493029
30503030class GPUQueue (classes .GPUQueue , GPUObjectBase ):
3031+ # GPUObjectBaseMixin
3032+ _release_function = libf .wgpuQueueRelease
3033+
30513034 def submit (self , command_buffers ):
30523035 command_buffer_ids = [cb ._internal for cb in command_buffers ]
30533036 c_command_buffers = ffi .new ("WGPUCommandBuffer []" , command_buffer_ids )
@@ -3266,35 +3249,23 @@ def callback(status_, user_data_p):
32663249 if status != 0 :
32673250 raise RuntimeError (f"Queue work done status: { status } " )
32683251
3269- def _release (self ):
3270- if self ._internal is not None and libf is not None :
3271- self ._internal , internal = None , self ._internal
3272- # H: void f(WGPUQueue queue)
3273- libf .wgpuQueueRelease (internal )
3274-
32753252
32763253class GPURenderBundle (classes .GPURenderBundle , GPUObjectBase ):
3277- def _release (self ):
3278- if self ._internal is not None and libf is not None :
3279- self ._internal , internal = None , self ._internal
3280- # H: void f(WGPURenderBundle renderBundle)
3281- libf .wgpuRenderBundleRelease (internal )
3254+ # GPUObjectBaseMixin
3255+ _release_function = libf .wgpuRenderBundleRelease
32823256
32833257
32843258class GPUQuerySet (classes .GPUQuerySet , GPUObjectBase ):
3259+ # GPUObjectBaseMixin
3260+ _release_function = libf .wgpuQuerySetRelease
3261+
32853262 def destroy (self ):
32863263 # Note: not yet implemented in wgpu-core, the wgpu-native func is a noop
32873264 internal = self ._internal
32883265 if internal is not None :
32893266 # H: void f(WGPUQuerySet querySet)
32903267 libf .wgpuQuerySetDestroy (internal )
32913268
3292- def _release (self ):
3293- if self ._internal is not None and libf is not None :
3294- self ._internal , internal = None , self ._internal
3295- # H: void f(WGPUQuerySet querySet)
3296- libf .wgpuQuerySetRelease (internal )
3297-
32983269
32993270# %% Subclasses that don't need anything else
33003271
0 commit comments