-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgraph_shader.metal
More file actions
56 lines (47 loc) · 1.61 KB
/
graph_shader.metal
File metadata and controls
56 lines (47 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// shaders.metal
// Metal Graph
//
// Created by Vegard Solheim Theriault on 13/01/2017.
// Copyright © 2017 Vegard Solheim Theriault. All rights reserved.
//
#include <metal_stdlib>
using namespace metal;
struct VertexIn {
float value;
};
struct VertexOut {
float4 position [[position]];
float pointSize [[point_size]];
float4 color;
};
struct Uniforms {
uint32_t offset;
uint32_t capacity;
float minValue;
float maxValue;
uint8_t pointSize;
float4 topColor;
float4 bottomColor;
};
vertex VertexOut vertexShader(device VertexIn *vertices [[buffer(0)]],
constant Uniforms *uniforms [[buffer(1)]],
uint vid [[vertex_id]])
{
// normalizedY is in the range [0, 1], where 0 is bottom, 1 is top
float normalizedY = (vertices[vid].value - uniforms->minValue) / (uniforms->maxValue - uniforms->minValue);
// transform normalizedY to the range [-1, 1]
float y = normalizedY * 2.0f - 1.0f;
// Use the offset and the vid to find the index. xIndex is in the range [0, uniforms->numSamples - 1]
float xIndex = float((vid + (uniforms->capacity - uniforms->offset)) % uniforms->capacity);
// Transforming xIndex to the range [-1, 1]
float x = (xIndex / float(uniforms->capacity - 1)) * 2.0f - 1.0f;
VertexOut vOut;
vOut.pointSize = uniforms->pointSize;
vOut.position = {x, y, 1.0f, 1.0f};
vOut.color = mix(uniforms->bottomColor, uniforms->topColor, normalizedY);
return vOut;
}
fragment float4 fragmentShader(VertexOut input [[stage_in]]) {
return input.color;
}