在glsl和hlsl中,我可以定义如下函数:
float voronoi(vec2 x, out int2 cell) {
cell = ...
return ...
}然而,这似乎在wgsl中是不可能的。
这个的替代方案是什么?我想我可以定义一个VoronoiResult结构,但它似乎过于复杂:
struct VoronoiResult {
cell: vec2<i32>;
distance: f32;
};
fn voronoi(x: vec2<f32>) -> VoronoiResult {
// ...
var ret: VoronoiResult;
ret.distance = distance;
ret.cell = cell;
return ret;
}发布于 2022-07-20 01:39:24
等效的方法是使用指针参数:
fn voronoi(x: vec2<f32>, cell: ptr<function, vec2<i32>>) -> f32 {
*cell = vec2(1, 2);
return 1.f;
}
@compute @workgroup_size(1)
fn main() {
var a: vec2<i32>;
var f = voronoi(vec2(1.f, 1.f), &a);
}这就产生了HLSL:
float voronoi(float2 x, inout int2 cell) {
cell = int2(1, 2);
return 1.0f;
}
[numthreads(1, 1, 1)]
void main() {
int2 a = int2(0, 0);
float f = voronoi((1.0f).xx, a);
return;
}还可以使用struct初始化程序使struct版本更短:
struct Out {
cell: vec2<i32>,
val: f32,
}
fn voronoi(x: vec2<f32>) -> Out {
return Out(vec2(1, 2), 1.f);
}
@compute @workgroup_size(1)
fn main() {
var f = voronoi(vec2(1.f, 1.f));
}https://stackoverflow.com/questions/72967362
复制相似问题