How to map this wgsl code to rust-gpu code? #551
-
|
Hi, I am new to shaders and I have been following along Learn WGPU tutorial, and am currently at Buffers and Indices. The current wgsl code that I am trying to port over is: struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) color: vec3<f32>,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};
@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = vec4<f32>(model.position, 1.0);
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color, 1.0);
}I cannot seem to link the content in the vertex buffer to #![no_std]
use spirv_std::glam::{Vec3, Vec4};
use spirv_std::spirv;
/*#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
position: [f32; 3],
color: [f32; 3]
}*/
#[spirv(vertex)]
pub fn vs_main(
#[spirv(descriptor_set = 0, binding = 0)] position: &[f32],
#[spirv(descriptor_set = 0, binding = 1)] color: &[f32],
#[spirv(position, invariant)] out_pos: &mut Vec4,
out_color: &mut Vec3
) {
let color: [f32; 3] = color.try_into().unwrap();
*out_color = color.into();
let pos: [f32; 3] = position.try_into().unwrap();
let pos: Vec3 = pos.into();
*out_pos = pos.extend(1.0);
}
#[spirv(fragment)]
pub fn fs_main(
pos: Vec4,
color: Vec3,
out: &mut Vec4
) {
*out = color.extend(1.0);
}which gives I had previously done the example in the earlier chapter 'The Pipeline', and successfully converted the code over. code#![no_std]
use spirv_std::glam::{Vec3, Vec4, Vec4Swizzles};
use spirv_std::spirv;
#[spirv(vertex)]
pub fn vs_main(
#[spirv(vertex_index)] vtx_idx: i32,
#[spirv(position)] clip_pos: &mut Vec4,
vert_pos: &mut Vec3
) {
let x = (1 - vtx_idx) as f32 * 0.5;
let y = ((vtx_idx & 1) * 2 - 1) as f32 * 0.5;
*clip_pos = Vec4::new(x, y, 0., 1.);
*vert_pos = clip_pos.xyz();
}
#[spirv(fragment)]
pub fn fs_main(
_clip_pos: Vec4,
_vert_pos: Vec3,
out: &mut Vec4
) {
*out = Vec4::new(0.3, 0.2, 0.1, 1.);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Essentially, with current restrictions of rust-gpu you'll not be able to convert between slices and arrays in either direction. This is supposed to change at some point, but not yet. Similarly, you'll have a hard time using iterator trait for anything moderately complex, so will need to do manual indexing like in C/C++ quite often. |
Beta Was this translation helpful? Give feedback.
Yep, for now you'll just have to
[position[i], position[i+1], position[i+2]],core::array::from_fnalso doesn't work