13 个版本 (6 个破坏性更新)

0.7.3 2024 年 3 月 2 日
0.7.2 2024 年 2 月 16 日
0.6.0 2024 年 1 月 17 日
0.5.0 2024 年 1 月 17 日
0.1.2 2023 年 9 月 29 日

#1213 in 游戏开发

Download history 2/week @ 2024-04-08 1/week @ 2024-06-03 2/week @ 2024-06-10 1/week @ 2024-06-17 19/week @ 2024-07-01 47/week @ 2024-07-22

66 每月下载次数

MIT 许可证

105KB
2K SLoC

LucentFlux 图形库

这个工具箱提供了一系列编写 wgpu 程序时常用的实用工具。这个库是公开的,因为提供的工具可能很有用,但它不是一个通用库,并且功能 将会 随着我们的需求而添加和删除。

功能

仅片段的全屏四边形着色器管道

在运行于全屏四边形的片段着色器中减少了样板代码

use lf_gfx::{LfDeviceExt, LfCommandEncoderExt};
# let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
# let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).unwrap();
# let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default(), None)).unwrap();

// Standard pipeline creation pattern, but ending with `create_fragment_only_render_pipeline`:
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { 
    label: Some("layout"), 
    bind_group_layouts: &[/* .. */], 
    push_constant_ranges:  &[/* .. */] 
});
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
    label: Some("module"),
    source: wgpu::ShaderSource::Wgsl(
        /* .. */
#       "".into()
    ),
});
let pipeline = device.create_fragment_only_render_pipeline(&lf_gfx::FragmentOnlyRenderPipelineDescriptor {
    label: Some("pipeline"),
    layout: Some(&layout),
    multisample: wgpu::MultisampleState::default(),
    fragment: wgpu::FragmentState { 
        module: &module, 
        entry_point: "main", 
        targets: &[/* .. */] 
    },
    multiview: None,
});

let mut cmd = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
    label: Some("command encoder"),
});
let mut rp = cmd.begin_fragment_only_render_pass(&lf_gfx::FragmentOnlyRenderPassDescriptor {
    label: Some("renderpass"),
    color_attachments: &[/* .. */],
    stencil_attachment: None,
    timestamp_writes: None,
});
// No need to bind vertex buffers or even a unit vertex shader
rp.set_pipeline(&pipeline);
rp.draw();

本地存储

use lf_gfx::local_storage;

local_storage::store("my_key", "a value").unwrap();

// Persists between runs
let stored = local_storage::load("my_key");
assert_eq!(stored, Some("a value".to_owned()));

游戏 API

struct MyGameCfg { /* .. */ }
#[derive(serde::Serialize, serde::Deserialize)]
enum MyGameLinearInputs { 
    Forward,
    Jump,
    /* .. */ 
}
#[derive(serde::Serialize, serde::Deserialize)]
enum MyGameVectorInputs { 
    Look,
    /* .. */ 
}

struct MyGame { /* .. */ }

impl lf_gfx::Game for MyGame {
    type InitData = MyGameCfg;
    type LinearInputType = MyGameLinearInputs;
    type VectorInputType = MyGameVectorInputs;

    fn title() -> &'static str {
        "My Game"
    }

    fn default_inputs(&self) -> lf_gfx::input::InputMap<MyGameLinearInputs, MyGameVectorInputs> {
        let mut inputs = lf_gfx::input::InputMap::empty();

        inputs.assign_linear(lf_gfx::input::KeyCode::KeyW, MyGameLinearInputs::Forward);
        inputs.assign_linear(lf_gfx::input::MouseInputType::ScrollUp, MyGameLinearInputs::Jump);

        inputs.assign_vector(lf_gfx::input::VectorInputType::MouseMove, MyGameVectorInputs::Look);

        return inputs;
    }

    fn init(data: &lf_gfx::GameData, init: Self::InitData) -> anyhow::Result<Self> {
        Ok(Self { /* .. */ })
    }

    fn window_resize(&mut self, data: &lf_gfx::GameData, new_size: winit::dpi::PhysicalSize<u32>) {
        /* .. */
    }

    fn handle_linear_input(
        &mut self,
        data: &lf_gfx::GameData,
        input: &Self::LinearInputType,
        activation: lf_gfx::input::LinearInputActivation,
    ) {
        /* .. */
    }

    fn handle_vector_input(
        &mut self,
        data: &lf_gfx::GameData,
        input: &Self::VectorInputType,
        activation: lf_gfx::input::VectorInputActivation,
    ) {
        /* .. */
    }

    fn render_to(&mut self, data: &lf_gfx::GameData, view: wgpu::TextureView) {
        /* .. */
    }
}

fn main() {
    use lf_gfx::LfGameExt;
    MyGame::run(MyGameCfg { /* .. */ });
}

更快的非加密哈希算法

let mut hs = lf_gfx::FastHashSet::default();
let mut hm = lf_gfx::FastHashMap::default();

hs.insert(10);
hm.insert(20, 30);

依赖项

~6–42MB
~686K SLoC