这个金属着色器基于此处的教程。
这个着色器的结果通过一个MTKView的子类在MacOS Swift Playground中可见。
着色器代码:
最终图像的尺寸被硬编码为400x400。是否有一种方法可以动态获取渲染目标的尺寸?
http://metalkit.org/2016/10/01/using-metalkit-part-2-3-2.html
它绘制了页面上第三张图中看到的黄蓝渐变。 我的目标是使用片段/顶点对而不是计算着色器来绘制此着色器。这个着色器的结果通过一个MTKView的子类在MacOS Swift Playground中可见。
着色器代码:
#include <metal_stdlib>
using namespace metal;
struct Vertex {
float4 position [[position]];
float4 color;
};
vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]],
uint vid [[vertex_id]]) {
Vertex in = vertices[vid];
Vertex out;
out.position = float4(in.position);
out.color = in.color;
return out;
}
fragment float4 fragment_func(Vertex vert [[stage_in]],
constant float &timer [[buffer(0)]]) {
float4 fragColor;
int width = 400;
int height = 400;
float2 resolution = float2(width,height);
float2 uv = vert.position.xy * 0.5 / resolution;
float3 color = mix(float3(1.0, 0.6, 0.1), float3(0.5, 0.8, 1.0), sqrt(1 - uv.y));
fragColor = float4(color,1);
return(fragColor);
}
Swift顶点和索引代码:
let vertexData = [
Vertex(pos: [-1.0, -1.0, 0.0, 1.0], col: [1, 0, 0, 1]),
Vertex(pos: [ 1.0, -1.0, 0.0, 1.0], col: [0, 1, 0, 1]),
Vertex(pos: [ 1.0, 1.0, 0.0, 1.0], col: [0, 0, 1, 1]),
Vertex(pos: [-1.0, 1.0, 0.0, 1.0], col: [1, 1, 1, 1])
]
let indexData: [UInt16] = [
0, 1, 2, 2, 3, 0
]
最终图像的尺寸被硬编码为400x400。是否有一种方法可以动态获取渲染目标的尺寸?