-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtriangle.py
143 lines (116 loc) · 4.23 KB
/
triangle.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
Example use of webgpu API to draw a triangle. See the triangle_glfw.py
script (and related scripts) for actually running this.
Similar example in other languages / API's:
* Rust wgpu:
https://github.com/gfx-rs/wgpu-rs/blob/master/examples/hello-triangle/main.rs
* C wgpu:
https://github.com/gfx-rs/wgpu/blob/master/examples/triangle/main.c
* Python Vulkan:
https://github.com/realitix/vulkan/blob/master/example/contribs/example_glfw.py
"""
import wgpu
# %% Shaders
shader_source = """
struct VertexInput {
[[builtin(vertex_index)]] vertex_index : u32;
};
struct VertexOutput {
[[location(0)]] color : vec4<f32>;
[[builtin(position)]] pos: vec4<f32>;
};
[[stage(vertex)]]
fn vs_main(in: VertexInput) -> VertexOutput {
var positions = array<vec2<f32>, 3>(vec2<f32>(0.0, -0.5), vec2<f32>(0.5, 0.5), vec2<f32>(-0.5, 0.7));
let index = i32(in.vertex_index);
let p: vec2<f32> = positions[index];
var out: VertexOutput;
out.pos = vec4<f32>(p, 0.0, 1.0);
out.color = vec4<f32>(p, 0.5, 1.0);
return out;
}
[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
return in.color;
}
"""
# %% The wgpu calls
def main(canvas, power_preference="high-performance", limits=None):
"""Regular function to setup a viz on the given canvas."""
adapter = wgpu.request_adapter(canvas=canvas, power_preference=power_preference)
device = adapter.request_device(required_limits=limits)
return _main(canvas, device)
async def main_async(canvas):
"""Async function to setup a viz on the given canvas."""
adapter = await wgpu.request_adapter_async(
canvas=canvas, power_preference="high-performance"
)
device = await adapter.request_device_async(required_limits={})
return _main(canvas, device)
def _main(canvas, device):
shader = device.create_shader_module(code=shader_source)
# No bind group and layout, we should not create empty ones.
pipeline_layout = device.create_pipeline_layout(bind_group_layouts=[])
present_context = canvas.get_context()
render_texture_format = present_context.get_preferred_format(device.adapter)
present_context.configure(device=device, format=render_texture_format)
render_pipeline = device.create_render_pipeline(
layout=pipeline_layout,
vertex={
"module": shader,
"entry_point": "vs_main",
"buffers": [],
},
primitive={
"topology": wgpu.PrimitiveTopology.triangle_list,
"front_face": wgpu.FrontFace.ccw,
"cull_mode": wgpu.CullMode.none,
},
depth_stencil=None,
multisample={
"count": 1,
"mask": 0xFFFFFFFF,
"alpha_to_coverage_enabled": False,
},
fragment={
"module": shader,
"entry_point": "fs_main",
"targets": [
{
"format": render_texture_format,
"blend": {
"color": (
wgpu.BlendFactor.one,
wgpu.BlendFactor.zero,
wgpu.BlendOperation.add,
),
"alpha": (
wgpu.BlendFactor.one,
wgpu.BlendFactor.zero,
wgpu.BlendOperation.add,
),
},
},
],
},
)
def draw_frame():
current_texture_view = present_context.get_current_texture()
command_encoder = device.create_command_encoder()
render_pass = command_encoder.begin_render_pass(
color_attachments=[
{
"view": current_texture_view,
"resolve_target": None,
"load_value": (0, 0, 0, 1), # LoadOp.load or color
"store_op": wgpu.StoreOp.store,
}
],
)
render_pass.set_pipeline(render_pipeline)
# render_pass.set_bind_group(0, no_bind_group, [], 0, 1)
render_pass.draw(3, 1, 0, 0)
render_pass.end_pass()
device.queue.submit([command_encoder.finish()])
canvas.request_draw(draw_frame)
return device