gpt4 book ai didi

ios - 在 Metal Shading Language 中创建全局可访问的常量缓冲区

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:14:01 24 4
gpt4 key购买 nike


我有一个关于 Metal 中常量缓冲区的问题。让我们假设,我有类似的东西:

...list of includes goes here...

using namespace metal;

struct ConstantBuffer {
float ANY_VALUE;
};

struct VS_INPUTS {
float4 i_pos_ms [ [ attribute ( 0 ) ] ] ;
} ;

struct V2P_STRUCT {
float4 v_pos_out [ [ position ] ] ;
} ;

float3 CalcSomething() {
return float3(ANY_VALUE, ANY_VALUE, ANY_VALUE); // !!!!!!!!
}

vertex V2P_STRUCT VertexFunc(VS_INPUTS vs_inputs [ [ stage_in ] ] ,
constant ConstantBuffer& cb [ [ buffer (1) ] ] )
{
V2P_STRUCT vs_outputs;

vs_outputs.v_pos_out.xyz = CalcSomething();
vs_outputs.v_pos_out.w = cb.ANY_VALUE; // that's OK

return vs_outputs;
}

是否可以在不传递 ANY_VALUE 作为输入参数的情况下调用 CalcSomething()?
例如,在 DX11 或 OpenGL 中,您创建常量缓冲区,可以从着色器代码中的每个位置访问它。

我考虑将“cb”的内容复制到临时全局对象,但我不知道该怎么做(因为地址空间不变)。

另一种想法是以某种方式在全局范围内声明“cb”(但不幸的是 [[buffer]] 仅为参数设计)。有什么技巧吗?

最佳答案

我的问题的解决方案:

#include <metal_stdlib> 
#include <metal_graphics>
#include <metal_texture>
#include <metal_matrix>
#include <metal_math>
#include <metal_geometric>
#include <metal_common>

using namespace metal;

constant float MyVariable = 4;

struct ConstantBuffer
{
float ANY_VALUE;
};

struct VS_INPUTS {
float4 i_pos_ms [ [ attribute ( 0 ) ] ] ;
};

struct V2P_STRUCT {
float4 v_pos_out [ [ position ] ] ;
};

struct VertexShader
{
thread VS_INPUTS& vs_inputs;
thread texture2d<float> img;

constant ConstantBuffer& cb;

VertexShader(thread VS_INPUTS& inputs, constant ConstantBuffer& b, thread texture2d<float>& texture)
: cb(b)
, vs_inputs(inputs)
, img(texture)
{}

float3 CalcSomething() {
return float3(cb.ANY_VALUE, cb.ANY_VALUE, cb.ANY_VALUE); // !!!!!!!!
}

V2P_STRUCT majn()
{
V2P_STRUCT vs_outputs;

vs_outputs.v_pos_out.xyz = CalcSomething();
vs_outputs.v_pos_out.w = cb.ANY_VALUE * vs_inputs.i_pos_ms.x * MyVariable; // that's OK

return vs_outputs;
}
};

vertex V2P_STRUCT VertexFunc(VS_INPUTS vs_inputs [ [ stage_in ] ] ,
constant ConstantBuffer& cb [ [ buffer (1) ] ] ,
texture2d<float> img [[ texture(0) ]]
)
{
VertexShader vs(vs_inputs, cb, img);
return vs.majn();
}

我创建了一个包含我整个原始着色器的结构。参数作为对构造函数的引用传递。任何函数都可以从常量缓冲区中读取而无需接收大量参数。要解决现在属于 cb 的 ANY_VALUE 的问题,我使用宏:

#define ANY_VALUE cb.ANY_VALUE.

关于ios - 在 Metal Shading Language 中创建全局可访问的常量缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32135788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com