From 3b0a620c0474093cc2564c54ca11d83973007e96 Mon Sep 17 00:00:00 2001 From: klukaszek Date: Sat, 26 Oct 2024 04:41:00 -0400 Subject: [PATCH] Got textures working but there seems to be some bugged pixels. --- src/gpu/webgpu/SDL_gpu_webgpu.c | 42 +++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/gpu/webgpu/SDL_gpu_webgpu.c b/src/gpu/webgpu/SDL_gpu_webgpu.c index 9f76de9405e3ca..1174d8578e4097 100644 --- a/src/gpu/webgpu/SDL_gpu_webgpu.c +++ b/src/gpu/webgpu/SDL_gpu_webgpu.c @@ -700,7 +700,7 @@ static WGPUTextureUsageFlags SDLToWGPUTextureUsageFlags(SDL_GPUTextureUsageFlags WGPUTextureUsageFlags wgpuFlags; switch (usageFlags) { case SDL_GPU_TEXTUREUSAGE_SAMPLER: - wgpuFlags = WGPUTextureUsage_TextureBinding; + wgpuFlags = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopyDst; break; case SDL_GPU_TEXTUREUSAGE_COLOR_TARGET: case SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET: @@ -2782,8 +2782,8 @@ static void WebGPU_SetTextureName( container->debugName = SDL_strdup(name); // Set the texture view name - wgpuTextureViewSetLabel(webgpuTexture->fullView, name); wgpuTextureSetLabel(webgpuTexture->texture, name); + wgpuTextureViewSetLabel(webgpuTexture->fullView, name); } static void WebGPU_UploadToTexture(SDL_GPUCommandBuffer *commandBuffer, @@ -2796,6 +2796,44 @@ static void WebGPU_UploadToTexture(SDL_GPUCommandBuffer *commandBuffer, SDL_LogError(SDL_LOG_CATEGORY_GPU, "Invalid parameters for uploading to texture"); return; } + + WebGPUCommandBuffer *wgpuCmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WebGPUTextureContainer *tex_container = (WebGPUTextureContainer *)destination->texture; + WebGPUTextureHandle *textureHandle = tex_container->activeTextureHandle; + WebGPUTexture *webgpuTexture = textureHandle->webgpuTexture; + + // Create the texture data + WGPUTextureDataLayout dataLayout = { + .offset = source->offset, + .bytesPerRow = destination->w * sizeof(Uint32), + .rowsPerImage = destination->h, + }; + + WGPUImageCopyTexture copyTexture = { + .texture = webgpuTexture->texture, + .mipLevel = destination->mip_level, + .origin = (WGPUOrigin3D){ + .x = destination->x, + .y = destination->y, + .z = destination->z, + }, + }; + + WebGPUBufferContainer *buf_container = (WebGPUBufferContainer *)source->transfer_buffer; + WebGPUBufferHandle *bufferHandle = buf_container->activeBufferHandle; + WebGPUBuffer *transfer_buffer = bufferHandle->webgpuBuffer; + + // Create the texture region + WGPUExtent3D extent = { + .width = destination->w, + .height = destination->h, + .depthOrArrayLayers = destination->d, + }; + + Uint32 size = transfer_buffer->size; + void *data = transfer_buffer->mappedData; + + wgpuQueueWriteTexture(wgpuCmdBuf->renderer->queue, ©Texture, data, size, &dataLayout, &extent); } static SDL_GPUSampler *WebGPU_CreateSampler(