需要将着色器的统一变量放入顶点函数签名中吗?

huangapple go评论58阅读模式
英文:

Do I need to put shader uniforms into the Vertex function signature?

问题

顶点着色器应接受宽度、高度、U偏移、V偏移和颜色。

签名看起来像这样:

vertex VertexOut texturedVertex(constant packed_float3* pPosition [[ buffer(0) ]],
                                constant packed_float2* pTexCoords [[ buffer(2) ]],
                                constant float & pWidth [[ buffer(4) ]],
                                constant float & pHeight [[ buffer(5) ]],
                                uint vid [[ vertex_id ]],
                                constant packed_float4 & pColor [[ buffer(3) ]])

这看起来不太美观,也不太可扩展。
是否可能将这些值分别声明,类似于OpenGL的uniforms?

英文:

I have a shader that should accept width, height, u offset and v offset and a color.

the signature looks like this:

vertex VertexOut texturedVertex(constant packed_float3* pPosition   [[ buffer(0) ]],
                            constant packed_float2*        pTexCoords  [[ buffer(2) ]],
                            constant float&         pWidth      [[buffer(4)]],
                            constant float&         pHeight     [[buffer(5)]],
                            uint                    vid         [[ vertex_id ]],
                            constant packed_float4&        pColor      [[buffer(3)]])

This looks ugly and not scalable at all.
Is it possible to have the values be declared separately, something like in OpenGL's uniforms?

答案1

得分: 1

Argument Buffers - 参数缓冲区表示一组资源,可以集体分配为图形或计算函数的参数。您可以使用参数缓冲区来减少CPU开销,简化资源管理,并实现GPU驱动的流水线。

下面的示例展示了一个名为My_AB的参数缓冲区结构,该结构指定了用于名为my_kernel的核函数的资源:

struct My_AB {
    texture2d<float, access::write> a;
    depth2d<float> b;
    sampler c;
    texture2d<float> d;
    device float4* e;
    texture2d<float> f;
    int g;
};
kernel void my_kernel(constant My_AB & my_AB [[buffer(0)]])
{ ... }
英文:

Argument Buffers - an argument buffer represents a group of resources that can be collectively assigned as an argument to a graphics or compute function. You use argument buffers to reduce CPU overhead, simplify resource management, and implement GPU-driven pipelines.

The following example shows an argument buffer structure named My_AB that specifies resources for a kernel function named my_kernel:

struct My_AB {
    texture2d&lt;float, access::write&gt; a;
    depth2d&lt;float&gt; b;
    sampler c;
    texture2d&lt;float&gt; d;
    device float4* e;
    texture2d&lt;float&gt; f;
    int g;
};
kernel void my_kernel(constant My_AB &amp; my_AB [[buffer(0)]])
{ ... }

huangapple
  • 本文由 发表于 2020年1月3日 20:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定