Skip to content

wgpu 互操作 ​

cici 公开 wgpu 底层句柄。下面的示例使用全局函数。

rust
pub use wgpu;
pub use winit;
方法返回
wgpu_device()wgpu::Device(克隆句柄)
wgpu_queue()wgpu::Queue(克隆句柄)
surface_format()交换链格式
texture_from_wgpu(tex)包装成 Texture2D 供 draw_texture 使用
Texture2D::wgpu_texture()取得底层 wgpu::Texture
on_render_pass(f)当前帧主渲染之后插入自定义 wgpu pass

自定义离屏渲染 ​

可以创建自己的渲染目标,用自定义管线绘制,再作为 Texture2D 绘制:

rust
use cici::wgpu;

let device = wgpu_device();
let queue = wgpu_queue();

let size = wgpu::Extent3d {
    width: 512,
    height: 512,
    depth_or_array_layers: 1,
};

let texture = device.create_texture(&wgpu::TextureDescriptor {
    label: Some("custom target"),
    size,
    mip_level_count: 1,
    sample_count: 1,
    dimension: wgpu::TextureDimension::D2,
    format: wgpu::TextureFormat::Rgba8Unorm,
    usage: wgpu::TextureUsages::RENDER_ATTACHMENT
        | wgpu::TextureUsages::TEXTURE_BINDING,
    view_formats: &[],
});

let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());

// 用你自己的管线渲染到 view
{
    let _pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("custom pass"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: &view,
            depth_slice: None,
            resolve_target: None,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                store: wgpu::StoreOp::Store,
            },
        })],
        depth_stencil_attachment: None,
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    });
}

queue.submit(std::iter::once(encoder.finish()));

let tex = texture_from_wgpu(texture);
draw_texture(&tex, 100.0, 100.0, WHITE);

计算与自定义纹理 ​

  • wgpu_device() / wgpu_queue() 可以直接创建 buffer、bind group、compute pipeline,并提交计算。
  • texture_from_wgpu() 要求纹理是可过滤的 2D 颜色纹理(如 Rgba8Unorm),且带有 TEXTURE_BINDING usage。
  • Texture2D::wgpu_texture() 可用于自定义拷贝或写入;create_texture 创建的纹理已带 RENDER_ATTACHMENT | COPY_SRC。
  • on_render_pass(|device, queue, view, encoder| ...) 可以在当前帧、当前目标(窗口或 RenderTarget)上执行自己的 wgpu 管线,适合自定义 shader 与后处理。
  • 简单离屏可用 render_target(w, h) + set_render_target,不必手动管理纹理。

版本一致性 ​

通过 cici::wgpu 使用 wgpu,避免版本不一致。cici::winit 同理,可用于获取原始窗口、Android 入口类型等。

简单、强力、跨平台的 2D 游戏库