wgpu_native/
command.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::GLOBAL;
6
7pub use core::command::{compute_ffi::*, render_ffi::*};
8
9use core::{gfx_select, id};
10
11#[no_mangle]
12pub extern "C" fn wgpu_command_encoder_finish(
13    encoder_id: id::CommandEncoderId,
14    desc: Option<&wgt::CommandBufferDescriptor>,
15) -> id::CommandBufferId {
16    let desc = &desc.cloned().unwrap_or_default();
17    gfx_select!(encoder_id => GLOBAL.command_encoder_finish(encoder_id, desc))
18}
19
20#[no_mangle]
21pub extern "C" fn wgpu_command_encoder_copy_buffer_to_buffer(
22    command_encoder_id: id::CommandEncoderId,
23    source: id::BufferId,
24    source_offset: wgt::BufferAddress,
25    destination: id::BufferId,
26    destination_offset: wgt::BufferAddress,
27    size: wgt::BufferAddress,
28) {
29    gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_buffer_to_buffer(
30        command_encoder_id,
31        source, source_offset,
32        destination,
33        destination_offset,
34        size))
35}
36
37#[no_mangle]
38pub extern "C" fn wgpu_command_encoder_copy_buffer_to_texture(
39    command_encoder_id: id::CommandEncoderId,
40    source: &core::command::BufferCopyView,
41    destination: &core::command::TextureCopyView,
42    copy_size: wgt::Extent3d,
43) {
44    gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_buffer_to_texture(
45        command_encoder_id,
46        source,
47        destination,
48        copy_size))
49}
50
51#[no_mangle]
52pub extern "C" fn wgpu_command_encoder_copy_texture_to_buffer(
53    command_encoder_id: id::CommandEncoderId,
54    source: &core::command::TextureCopyView,
55    destination: &core::command::BufferCopyView,
56    copy_size: wgt::Extent3d,
57) {
58    gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_texture_to_buffer(
59        command_encoder_id,
60        source,
61        destination,
62        copy_size))
63}
64
65#[no_mangle]
66pub extern "C" fn wgpu_command_encoder_copy_texture_to_texture(
67    command_encoder_id: id::CommandEncoderId,
68    source: &core::command::TextureCopyView,
69    destination: &core::command::TextureCopyView,
70    copy_size: wgt::Extent3d,
71) {
72    gfx_select!(command_encoder_id => GLOBAL.command_encoder_copy_texture_to_texture(
73        command_encoder_id,
74        source,
75        destination,
76        copy_size))
77}
78
79/// # Safety
80///
81/// This function is unsafe because improper use may lead to memory
82/// problems. For example, a double-free may occur if the function is called
83/// twice on the same raw pointer.
84#[no_mangle]
85pub unsafe extern "C" fn wgpu_command_encoder_begin_render_pass(
86    encoder_id: id::CommandEncoderId,
87    desc: &core::command::RenderPassDescriptor,
88) -> *mut core::command::RawPass {
89    let pass = core::command::RawPass::new_render(encoder_id, desc);
90    Box::into_raw(Box::new(pass))
91}
92
93/// # Safety
94///
95/// This function is unsafe because improper use may lead to memory
96/// problems. For example, a double-free may occur if the function is called
97/// twice on the same raw pointer.
98#[no_mangle]
99pub unsafe extern "C" fn wgpu_render_pass_end_pass(pass_id: id::RenderPassId) {
100    let (pass_data, encoder_id) = Box::from_raw(pass_id).finish_render();
101    gfx_select!(encoder_id => GLOBAL.command_encoder_run_render_pass(encoder_id, &pass_data))
102}
103
104#[no_mangle]
105pub unsafe extern "C" fn wgpu_render_pass_destroy(pass: *mut core::command::RawPass) {
106    let _ = Box::from_raw(pass).into_vec();
107}
108
109/// # Safety
110///
111/// This function is unsafe because improper use may lead to memory
112/// problems. For example, a double-free may occur if the function is called
113/// twice on the same raw pointer.
114#[no_mangle]
115pub unsafe extern "C" fn wgpu_command_encoder_begin_compute_pass(
116    encoder_id: id::CommandEncoderId,
117    _desc: Option<&core::command::ComputePassDescriptor>,
118) -> *mut core::command::RawPass {
119    let pass = core::command::RawPass::new_compute(encoder_id);
120    Box::into_raw(Box::new(pass))
121}
122
123#[no_mangle]
124pub unsafe extern "C" fn wgpu_compute_pass_end_pass(pass_id: id::ComputePassId) {
125    let (pass_data, encoder_id) = Box::from_raw(pass_id).finish_compute();
126    gfx_select!(encoder_id => GLOBAL.command_encoder_run_compute_pass(encoder_id, &pass_data))
127}
128
129#[no_mangle]
130pub unsafe extern "C" fn wgpu_compute_pass_destroy(pass: *mut core::command::RawPass) {
131    let _ = Box::from_raw(pass).into_vec();
132}
133
134#[no_mangle]
135pub unsafe extern "C" fn wgpu_command_encoder_push_debug_group(
136    command_encoder_id: id::CommandEncoderId,
137    label: core::RawString,
138    _color: u32,
139) {
140    let string = std::ffi::CStr::from_ptr(label).to_string_lossy();
141    gfx_select!(command_encoder_id => GLOBAL.command_encoder_push_debug_group(command_encoder_id, &string));
142}
143
144#[no_mangle]
145pub unsafe extern "C" fn wgpu_command_encoder_pop_debug_group(
146    command_encoder_id: id::CommandEncoderId,
147) {
148    gfx_select!(command_encoder_id => GLOBAL.command_encoder_pop_debug_group(command_encoder_id));
149}
150
151#[no_mangle]
152pub unsafe extern "C" fn wgpu_command_encoder_insert_debug_marker(
153    command_encoder_id: id::CommandEncoderId,
154    label: core::RawString,
155    _color: u32,
156) {
157    let string = std::ffi::CStr::from_ptr(label).to_string_lossy();
158    gfx_select!(command_encoder_id => GLOBAL.command_encoder_insert_debug_marker(command_encoder_id, &string));
159}