mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-10 16:35:53 +01:00
Update for WebGPU latest
parent
5b9f07600e
commit
87e01f4fb2
@ -1139,54 +1139,31 @@ bool LoadTextureFromMemory(const void* data, size_t data_size, WGPUDevice device
|
||||
int image_width = 0;
|
||||
int image_height = 0;
|
||||
// Ask stbi to output 4 channels since WebGPU doesn't have 3-channels texture format
|
||||
const int output_channels = 4;
|
||||
void* const image_data = stbi_load_from_memory((const unsigned char *)data, (int)data_size, &image_width, &image_height, NULL, output_channels);
|
||||
const int image_channels = 4;
|
||||
void* const image_data = stbi_load_from_memory((const unsigned char *)data, (int)data_size, &image_width, &image_height, NULL, image_channels);
|
||||
if (image_data == NULL)
|
||||
return false;
|
||||
|
||||
// Create a WebGPU texture
|
||||
WGPUTextureDescriptor texture_desc;
|
||||
texture_desc.nextInChain = NULL;
|
||||
texture_desc.label = NULL;
|
||||
texture_desc.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding;
|
||||
texture_desc.dimension = WGPUTextureDimension_2D;
|
||||
texture_desc.size.width = (uint32_t)image_width;
|
||||
texture_desc.size.height = (uint32_t)image_height;
|
||||
texture_desc.size.depthOrArrayLayers = 1;
|
||||
texture_desc.format = WGPUTextureFormat_RGBA8Unorm;
|
||||
texture_desc.mipLevelCount = 1;
|
||||
texture_desc.sampleCount = 1;
|
||||
texture_desc.viewFormatCount = 0;
|
||||
texture_desc.viewFormats = NULL;
|
||||
WGPUTexture image_texture = wgpuDeviceCreateTexture(device, &texture_desc);
|
||||
|
||||
// Write loaded data to texture
|
||||
WGPUImageCopyTexture dest;
|
||||
dest.nextInChain = NULL;
|
||||
dest.texture = image_texture;
|
||||
dest.mipLevel = 0;
|
||||
dest.origin.x = 0;
|
||||
dest.origin.y = 0;
|
||||
dest.origin.z = 0;
|
||||
dest.aspect = WGPUTextureAspect_All;
|
||||
|
||||
WGPUTextureDataLayout src;
|
||||
src.nextInChain = NULL;
|
||||
src.offset = 0;
|
||||
src.bytesPerRow = (uint32_t)(output_channels * image_width);
|
||||
src.rowsPerImage = (uint32_t)image_height;
|
||||
|
||||
const size_t bytes = src.bytesPerRow * src.rowsPerImage;
|
||||
WGPUExtent3D write_size;
|
||||
write_size.width = (uint32_t)image_width;
|
||||
write_size.height = (uint32_t)image_height;
|
||||
write_size.depthOrArrayLayers = 1;
|
||||
wgpuQueueWriteTexture(queue, &dest, image_data, bytes, &src, &write_size);
|
||||
WGPUTextureDescriptor tex_desc;
|
||||
tex_desc.nextInChain = NULL;
|
||||
tex_desc.label = { "", WGPU_STRLEN };
|
||||
tex_desc.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding;
|
||||
tex_desc.dimension = WGPUTextureDimension_2D;
|
||||
tex_desc.size.width = (uint32_t)image_width;
|
||||
tex_desc.size.height = (uint32_t)image_height;
|
||||
tex_desc.size.depthOrArrayLayers = 1;
|
||||
tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
|
||||
tex_desc.mipLevelCount = 1;
|
||||
tex_desc.sampleCount = 1;
|
||||
tex_desc.viewFormatCount = 0;
|
||||
tex_desc.viewFormats = NULL;
|
||||
WGPUTexture image_texture = wgpuDeviceCreateTexture(device, &tex_desc);
|
||||
|
||||
// Create a WebGPU texture view
|
||||
WGPUTextureViewDescriptor view_desc;
|
||||
view_desc.nextInChain = NULL;
|
||||
view_desc.label = NULL;
|
||||
view_desc.label = { "", WGPU_STRLEN };
|
||||
view_desc.format = WGPUTextureFormat_RGBA8Unorm;
|
||||
view_desc.dimension = WGPUTextureViewDimension_2D;
|
||||
view_desc.baseMipLevel = 0;
|
||||
@ -1194,12 +1171,25 @@ bool LoadTextureFromMemory(const void* data, size_t data_size, WGPUDevice device
|
||||
view_desc.baseArrayLayer = 0;
|
||||
view_desc.arrayLayerCount = 1;
|
||||
view_desc.aspect = WGPUTextureAspect_All;
|
||||
WGPUTextureView view = wgpuTextureCreateView(image_texture, &view_desc);
|
||||
|
||||
// Write loaded data to texture
|
||||
WGPUTexelCopyTextureInfo dst_view = {};
|
||||
dst_view.texture = image_texture;
|
||||
dst_view.mipLevel = 0;
|
||||
dst_view.origin = { 0, 0, 0 };
|
||||
dst_view.aspect = WGPUTextureAspect_All;
|
||||
WGPUTexelCopyBufferLayout layout = {};
|
||||
layout.offset = 0;
|
||||
layout.bytesPerRow = image_width * image_channels;
|
||||
layout.rowsPerImage = image_height;
|
||||
WGPUExtent3D write_size = { (uint32_t)image_width, (uint32_t)image_height, 1 };
|
||||
wgpuQueueWriteTexture(queue, &dst_view, image_data, layout.bytesPerRow * layout.rowsPerImage, &layout, &write_size);
|
||||
|
||||
*out_texture = image_texture;
|
||||
*out_texture_view = wgpuTextureCreateView(image_texture, &view_desc);
|
||||
*out_texture_view = view;
|
||||
*out_width = image_width;
|
||||
*out_height = image_height;
|
||||
|
||||
*out_height = image_height;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user