forked from juj/wasm_webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.c
209 lines (168 loc) · 7.17 KB
/
texture.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <assert.h>
#include <stdio.h>
#include "lib_webgpu.h"
#include "lib_demo.h"
#include <miniprintf.h>
WGpuAdapter adapter;
WGpuCanvasContext canvasContext;
WGpuDevice device;
WGpuQueue queue;
WGpuRenderPipeline renderPipeline;
WGpuBuffer buffer = 0;
WGpuTexture texture;
int imageWidth, imageHeight;
WGpuSampler sampler;
WGpuBindGroup bindGroup;
float v[] = {
// x, y, u, v
-1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f,
-1.f, 1.f, 0.f, 1.f,
-1.f, 1.f, 0.f, 1.f,
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f
};
void CreateGeometryAndRender()
{
WGpuBufferDescriptor bufferDesc = {};
bufferDesc.size = sizeof(v);
bufferDesc.usage = WGPU_BUFFER_USAGE_VERTEX;
bufferDesc.mappedAtCreation = EM_TRUE;
int canvasWidth, canvasHeight;
emscripten_get_canvas_element_size("canvas", &canvasWidth, &canvasHeight);
float scaleX = (float)canvasWidth/imageWidth;
float scaleY = (float)canvasHeight/imageHeight;
float scale = scaleY < scaleX ? scaleY : scaleX;
float letterbox = imageWidth * scale / canvasWidth;
float pillarbox = imageHeight * scale / canvasHeight;
v[0] = v[8] = v[12] = -letterbox;
v[4] = v[16] = v[20] = letterbox;
v[1] = v[5] = v[17] = -pillarbox;
v[9] = v[13] = v[21] = pillarbox;
wgpu_object_destroy(buffer);
buffer = wgpu_device_create_buffer(device, &bufferDesc);
wgpu_buffer_get_mapped_range(buffer, 0, WGPU_MAX_SIZE);
wgpu_buffer_write_mapped_range(buffer, 0, 0, v, sizeof(v));
wgpu_buffer_unmap(buffer);
WGpuCommandEncoder encoder = wgpu_device_create_command_encoder(device, 0);
WGpuRenderPassColorAttachment colorAttachment = WGPU_RENDER_PASS_COLOR_ATTACHMENT_DEFAULT_INITIALIZER;
colorAttachment.view = wgpu_texture_create_view(wgpu_canvas_context_get_current_texture(canvasContext), 0);
colorAttachment.loadOp = WGPU_LOAD_OP_CLEAR;
colorAttachment.clearValue.r = colorAttachment.clearValue.g =
colorAttachment.clearValue.b = colorAttachment.clearValue.a = 1.0;
WGpuRenderPassDescriptor passDesc = {};
passDesc.numColorAttachments = 1;
passDesc.colorAttachments = &colorAttachment;
WGpuRenderPassEncoder pass = wgpu_command_encoder_begin_render_pass(encoder, &passDesc);
wgpu_render_pass_encoder_set_pipeline(pass, renderPipeline);
wgpu_render_pass_encoder_set_vertex_buffer(pass, 0, buffer, 0, sizeof(v));
wgpu_render_pass_encoder_set_bind_group(pass, 0, bindGroup, 0, 0);
wgpu_render_pass_encoder_draw(pass, 6, 1, 0, 0);
wgpu_render_pass_encoder_end(pass);
wgpu_queue_submit_one_and_destroy(queue, wgpu_command_encoder_finish(encoder));
assert(wgpu_get_num_live_objects() < 100); // Check against programming errors from Wasm<->JS WebGPU object leaks
}
void DownloadedImage(WGpuImageBitmap bitmap, int width, int height, void *userData)
{
imageWidth = width;
imageHeight = height;
emscripten_mini_stdio_printf("DownloadedImage: width: %d, height: %d\n", width, height);
if (!width)
return;
queue = wgpu_device_get_queue(device);
canvasContext = wgpu_canvas_get_webgpu_context("canvas");
WGpuCanvasConfiguration config = WGPU_CANVAS_CONFIGURATION_DEFAULT_INITIALIZER;
config.device = device;
config.format = navigator_gpu_get_preferred_canvas_format();
wgpu_canvas_context_configure(canvasContext, &config);
const char *vertexShader =
"struct In {\n"
" @location(0) pos : vec2<f32>,\n"
" @location(1) uv : vec2<f32>\n"
"};\n"
"struct Out {\n"
" @builtin(position) pos : vec4<f32>,\n"
" @location(0) uv : vec2<f32>\n"
"};\n"
"@vertex\n"
"fn main(in: In) -> Out {\n"
"var out: Out;\n"
"out.pos = vec4<f32>(in.pos, 0.0, 1.0);\n"
"out.uv = in.uv;\n"
"return out;\n"
"}\n";
const char *fragmentShader =
"@group(0) @binding(0) var myTexture : texture_2d<f32>;\n"
"@group(0) @binding(1) var mySampler : sampler;\n"
"@fragment\n"
"fn main(@location(0) uv : vec2<f32>) -> @location(0) vec4<f32> {\n"
"return textureSample(myTexture, mySampler, uv);\n"
"}\n";
WGpuRenderPipelineDescriptor renderPipelineDesc = WGPU_RENDER_PIPELINE_DESCRIPTOR_DEFAULT_INITIALIZER;
WGpuVertexAttribute vertexAttr[2] = {};
vertexAttr[0].format = WGPU_VERTEX_FORMAT_FLOAT32X2;
vertexAttr[0].offset = 0;
vertexAttr[0].shaderLocation = 0;
vertexAttr[1].format = WGPU_VERTEX_FORMAT_FLOAT32X2;
vertexAttr[1].offset = 8;
vertexAttr[1].shaderLocation = 1;
WGpuVertexBufferLayout vbLayout = {};
vbLayout.numAttributes = 2;
vbLayout.attributes = vertexAttr;
vbLayout.arrayStride = 16;
renderPipelineDesc.vertex.numBuffers = 1;
renderPipelineDesc.vertex.buffers = &vbLayout;
WGpuShaderModuleDescriptor shaderModuleDesc = {};
shaderModuleDesc.code = vertexShader;
renderPipelineDesc.vertex.module = wgpu_device_create_shader_module(device, &shaderModuleDesc);
renderPipelineDesc.vertex.entryPoint = "main";
shaderModuleDesc.code = fragmentShader;
renderPipelineDesc.fragment.module = wgpu_device_create_shader_module(device, &shaderModuleDesc);
renderPipelineDesc.fragment.entryPoint = "main";
WGpuColorTargetState colorTarget = WGPU_COLOR_TARGET_STATE_DEFAULT_INITIALIZER;
colorTarget.format = config.format;
colorTarget.blend.color.operation = WGPU_BLEND_OPERATION_ADD;
colorTarget.blend.color.srcFactor = WGPU_BLEND_FACTOR_SRC_ALPHA;
colorTarget.blend.color.dstFactor = WGPU_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
renderPipelineDesc.fragment.numTargets = 1;
renderPipelineDesc.fragment.targets = &colorTarget;
renderPipeline = wgpu_device_create_render_pipeline(device, &renderPipelineDesc);
WGpuTextureDescriptor textureDesc = WGPU_TEXTURE_DESCRIPTOR_DEFAULT_INITIALIZER;
textureDesc.width = width;
textureDesc.height = height;
textureDesc.format = WGPU_TEXTURE_FORMAT_RGBA8UNORM;
textureDesc.usage = WGPU_TEXTURE_USAGE_COPY_DST | WGPU_TEXTURE_USAGE_TEXTURE_BINDING | WGPU_TEXTURE_USAGE_RENDER_ATTACHMENT;
texture = wgpu_device_create_texture(device, &textureDesc);
WGpuImageCopyExternalImage src = {};
src.source = bitmap;
WGpuImageCopyTextureTagged dst = {};
dst.texture = texture;
wgpu_queue_copy_external_image_to_texture(queue, &src, &dst, width, height, 1);
WGpuSamplerDescriptor samplerDesc = WGPU_SAMPLER_DESCRIPTOR_DEFAULT_INITIALIZER;
sampler = wgpu_device_create_sampler(device, &samplerDesc);
WGpuBindGroupEntry bindGroupEntries[2] = {};
bindGroupEntries[0].binding = 0;
bindGroupEntries[0].resource = wgpu_texture_create_view(texture, 0);
bindGroupEntries[1].binding = 1;
bindGroupEntries[1].resource = sampler;
bindGroup = wgpu_device_create_bind_group(device, wgpu_pipeline_get_bind_group_layout(renderPipeline, 0), bindGroupEntries, 2);
window_resized_callback(CreateGeometryAndRender);
CreateGeometryAndRender();
}
void ObtainedWebGpuDevice(WGpuDevice result, void *userData)
{
device = result;
wgpu_load_image_bitmap_from_url_async("fish.png", EM_TRUE, DownloadedImage, 0);
}
void ObtainedWebGpuAdapter(WGpuAdapter result, void *userData)
{
adapter = result;
WGpuDeviceDescriptor deviceDesc = {};
wgpu_adapter_request_device_async(adapter, &deviceDesc, ObtainedWebGpuDevice, 0);
}
int main()
{
WGpuRequestAdapterOptions options = {};
options.powerPreference = WGPU_POWER_PREFERENCE_LOW_POWER;
navigator_gpu_request_adapter_async(&options, ObtainedWebGpuAdapter, 0);
}