我正在尝试在 rustgpu 中声明一个原子输入变量,它可以在 glsl 中完成。
我尝试查看 rustgpu 代码库 和 spirv 板条箱文档。
从他的文档中我认为这会起作用:
#![cfg_attr(target_arch = "spirv", no_std, feature(lang_items))]
#![allow(internal_features)]
extern crate bytemuck;
extern crate spirv_std;
use core::sync::atomic::AtomicU32;
use shader_utils::ray_tracing::*;
use shader_utils::voxel_oct_tree::*;
use spirv_std::{arch::IndexUnchecked, glam::*, image::*, spirv, RuntimeArray};
type Texture2D = Image!(2D, type=f32, sampled);
type SampledTexture2D = SampledImage<Texture2D>;
// useful:
// https://github.com/EmbarkStudios/rust-gpu/blob/54f6978c25b7e168ded04e720b996625b3654ebd/crates/rustc_codegen_spirv/src/symbols.rs#L42
#[spirv(fragment)]
pub fn main_fs(
#[spirv(uniform, descriptor_set = 1, binding = 0)] volume_data: &VoxelVolumeMeta,
#[spirv(uniform, descriptor_set = 1, binding = 1)] window_data: &WindowData,
#[spirv(uniform, descriptor_set = 1, binding = 2)] voctree_levels: &u32,
#[spirv(descriptor_set = 1, binding = 3)] volume: &Storage3D,
#[spirv(atomic_counter, descriptor_set = 1, binding = 4)] node_count: &AtomicU32,
// #[spirv(uniform, descriptor_set = 1, binding = 5)] nodes:
// &RuntimeArray<Node<u32>>,
#[spirv(frag_coord)] screen_pos: Vec4,
out_color: &mut Vec4,
)
{}
但是我收到此错误:
error: Operand 2 of TypePointer requires one of these capabilities: AtomicStorage
%_ptr_AtomicCounter__struct_16 = OpTypePointer AtomicCounter %_struct_16
所以我不确定正确的语法是什么。
基于此讨论,似乎没有专门的类型,这只是将原子操作正确应用于存储输入的问题?
我不确定,但我尝试使用单个 u32 声明 SSBO,然后在前面的链接中复制粘贴带有原子的代码片段。它无法编译。
这似乎是正确的做法。您将原子声明为可变存储缓冲区,然后使用原子语义。
#[spirv(fragment)]
pub fn main_fs(
#[spirv(uniform, descriptor_set = 1, binding = 0)] volume_data: &VoxelVolumeMeta,
#[spirv(uniform, descriptor_set = 1, binding = 1)] window_data: &WindowData,
#[spirv(uniform, descriptor_set = 1, binding = 2)] voctree_levels: &u32,
#[spirv(descriptor_set = 1, binding = 3)] volume: &Storage3D,
#[spirv(storage_buffer, descriptor_set = 1, binding = 4)] buffer: &mut [u32; 1],
#[spirv(storage_buffer, descriptor_set = 1, binding = 5)] nodes: &mut RuntimeArray<
Node<u32>,
>,
#[spirv(frag_coord)] screen_pos: Vec4,
out_color: &mut Vec4,
)
{
let old = unsafe {
spirv_std::arch::atomic_i_add::<
_,
{ Scope::Invocation as u32 },
{ Semantics::UNIFORM_MEMORY.bits() },
>(buffer.index_unchecked_mut(0), 1)
};
}