Skip to content

Commit c59318c

Browse files
committed
Implement kore_webgpu_command_list_copy_texture_to_buffer
1 parent 22eec72 commit c59318c

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

backends/gpu/webgpu/sources/commandlist.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,51 @@ void kore_webgpu_command_list_copy_buffer_to_texture(kore_gpu_command_list *list
9191
const kore_gpu_image_copy_texture *destination, uint32_t width, uint32_t height,
9292
uint32_t depth_or_array_layers) {}
9393

94+
static WGPUTextureAspect convert_texture_aspect(kore_gpu_texture_aspect aspect) {
95+
switch (aspect) {
96+
case KORE_GPU_IMAGE_COPY_ASPECT_ALL:
97+
return WGPUTextureAspect_All;
98+
case KORE_GPU_IMAGE_COPY_ASPECT_DEPTH_ONLY:
99+
return WGPUTextureAspect_DepthOnly;
100+
case KORE_GPU_IMAGE_COPY_ASPECT_STENCIL_ONLY:
101+
return WGPUTextureAspect_StencilOnly;
102+
}
103+
104+
assert(false);
105+
return WGPUTextureAspect_All;
106+
}
107+
94108
void kore_webgpu_command_list_copy_texture_to_buffer(kore_gpu_command_list *list, const kore_gpu_image_copy_texture *source,
95109
const kore_gpu_image_copy_buffer *destination, uint32_t width, uint32_t height,
96-
uint32_t depth_or_array_layers) {}
110+
uint32_t depth_or_array_layers) {
111+
WGPUImageCopyTexture copy_texture = {
112+
.texture = source->texture->webgpu.texture,
113+
.mipLevel = source->mip_level,
114+
.origin = {
115+
.x = source->origin_x,
116+
.y = source->origin_y,
117+
.z = source->origin_z,
118+
},
119+
.aspect = convert_texture_aspect(source->aspect),
120+
};
121+
122+
WGPUImageCopyBuffer copy_buffer = {
123+
.layout = {
124+
.offset = destination->offset,
125+
.bytesPerRow = destination->bytes_per_row,
126+
.rowsPerImage = destination->rows_per_image,
127+
},
128+
.buffer = destination->buffer->webgpu.buffer,
129+
};
130+
131+
WGPUExtent3D size = {
132+
.width = width,
133+
.height = height,
134+
.depthOrArrayLayers = depth_or_array_layers,
135+
};
136+
137+
wgpuCommandEncoderCopyTextureToBuffer(list->webgpu.command_encoder, &copy_texture, &copy_buffer, &size);
138+
}
97139

98140
void kore_webgpu_command_list_copy_texture_to_texture(kore_gpu_command_list *list, const kore_gpu_image_copy_texture *source,
99141
const kore_gpu_image_copy_texture *destination, uint32_t width, uint32_t height,

backends/gpu/webgpu/sources/device.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void kore_webgpu_device_create(kore_gpu_device *device, const kore_gpu_device_wi
5858
WGPUSurfaceConfiguration surface_configuration = {
5959
.device = wgpu_device,
6060
.format = capabilities.formats[0],
61-
.usage = WGPUTextureUsage_RenderAttachment,
61+
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc,
6262
.alphaMode = WGPUCompositeAlphaMode_Auto,
6363
.width = kore_window_width(0),
6464
.height = kore_window_height(0),
@@ -248,7 +248,7 @@ void kore_webgpu_device_create_raytracing_hierarchy(kore_gpu_device *device, kor
248248
void kore_webgpu_device_create_query_set(kore_gpu_device *device, const kore_gpu_query_set_parameters *parameters, kore_gpu_query_set *query_set) {}
249249

250250
uint32_t kore_webgpu_device_align_texture_row_bytes(kore_gpu_device *device, uint32_t row_bytes) {
251-
return row_bytes;
251+
return (uint32_t)align_pow2((int)row_bytes, 256);
252252
}
253253

254254
void kore_webgpu_device_create_fence(kore_gpu_device *device, kore_gpu_fence *fence) {}

0 commit comments

Comments
 (0)