7
7
8
8
#include "../SDL_sysgpu.h"
9
9
#include "SDL_internal.h"
10
- #include "stdalign.h"
11
- #include <SDL3/SDL_events.h>
12
10
#include <SDL3/SDL_gpu.h>
13
- #include <SDL3/SDL_hints.h>
14
- #include <SDL3/SDL_mutex.h>
15
- #include <SDL3/SDL_oldnames.h>
16
- #include <SDL3/SDL_pixels.h>
17
- #include <SDL3/SDL_properties.h>
18
- #include <SDL3/SDL_render.h>
19
- #include <SDL3/SDL_stdinc.h>
20
- #include <SDL3/SDL_video.h>
21
11
#include <emscripten/emscripten.h>
22
12
#include <emscripten/html5.h>
23
13
#include <regex.h>
24
- #include <stdbool.h>
25
- #include <stdint.h>
26
- #include <stdio.h>
27
14
#include <webgpu/webgpu.h>
28
15
29
16
#define MAX_UBO_SECTION_SIZE 4096 // 4 KiB
34
21
#define MAX_PIPELINE_BINDINGS 32
35
22
#define MAX_ENTRYPOINT_LENGTH 64
36
23
37
- #define EXPAND_ELEMENTS_IF_NEEDED (arr , initialValue , type ) \
38
- if (arr->count == arr->capacity) { \
39
- if (arr->capacity == 0) { \
40
- arr->capacity = initialValue; \
41
- } else { \
42
- arr->capacity *= 2; \
43
- } \
44
- arr->elements = (type *)SDL_realloc( \
45
- arr->elements, \
46
- arr->capacity * sizeof(type)); \
47
- }
48
-
49
- #define MOVE_ARRAY_CONTENTS_AND_RESET (i , dstArr , dstCount , srcArr , srcCount ) \
50
- for (i = 0; i < srcCount; i += 1) { \
51
- dstArr[i] = srcArr[i]; \
52
- } \
53
- dstCount = srcCount; \
54
- srcCount = 0;
55
-
56
24
#define TRACK_RESOURCE (resource , type , array , count , capacity ) \
57
25
Uint32 i; \
58
26
\
86
54
SDL_Log("%s", buffer); \
87
55
} while (0)
88
56
89
- // Enums
57
+ // Typedefs and Enums
58
+ // ---------------------------------------------------
59
+
90
60
typedef enum WebGPUBindingType
91
61
{
92
62
WGPUBindingType_Undefined = 0x00000000 ,
@@ -270,7 +240,7 @@ typedef struct SwapchainSupportDetails
270
240
typedef struct WebGPUSwapchainData
271
241
{
272
242
WGPUSurface surface ;
273
- WGPUSwapChain swapchain ;
243
+ WGPUSurfaceDescriptor surfaceDesc ;
274
244
WGPUTextureFormat format ;
275
245
WGPUPresentMode presentMode ;
276
246
@@ -2340,7 +2310,7 @@ bool WebGPU_INTERNAL_CreateSurface(WebGPURenderer *renderer, WindowData *windowD
2340
2310
surfaceDescriptor .nextInChain = & fromWindowsHWND .chain ;
2341
2311
surfaceDescriptor .label = NULL ;
2342
2312
}
2343
- #elif defined(__EMSCRIPTEN__ )
2313
+ #elif defined(SDL_PLATFORM_EMSCRIPTEN )
2344
2314
{
2345
2315
#ifdef WEBGPU_BACKEND_DAWN
2346
2316
WGPUSurfaceSourceCanvasHTMLSelector_Emscripten fromCanvasHTMLSelector ;
@@ -2360,13 +2330,20 @@ bool WebGPU_INTERNAL_CreateSurface(WebGPURenderer *renderer, WindowData *windowD
2360
2330
#error "Unsupported WGPU_TARGET"
2361
2331
#endif
2362
2332
windowData -> swapchainData .surface = wgpuInstanceCreateSurface (renderer -> instance , & surfaceDescriptor );
2333
+ windowData -> swapchainData .surfaceDesc = surfaceDescriptor ;
2363
2334
return windowData -> swapchainData .surface != NULL ;
2364
2335
}
2365
2336
2366
2337
static void WebGPU_CreateSwapchain (WebGPURenderer * renderer , WindowData * windowData )
2367
2338
{
2368
2339
SDL_assert (WebGPU_INTERNAL_CreateSurface (renderer , windowData ));
2340
+ SDL_assert (windowData -> swapchainData .surface );
2341
+
2342
+ WebGPUSwapchainData * swapchainData = & windowData -> swapchainData ;
2369
2343
2344
+ // This is done as a temporary workaround until I can figure out why the macros in WebGPU_INTERNAL_CreateSurface are not working.
2345
+ // Ideally we would just call WebGPU_INTERNAL_CreateSurface here and skip the lines between the dividers.
2346
+ // -----
2370
2347
WGPUSurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {
2371
2348
.chain .sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector ,
2372
2349
.selector = "#canvas" ,
@@ -2375,11 +2352,20 @@ static void WebGPU_CreateSwapchain(WebGPURenderer *renderer, WindowData *windowD
2375
2352
.nextInChain = & canvas_desc .chain ,
2376
2353
.label = "SDL_GPU Swapchain Surface" ,
2377
2354
};
2378
-
2379
- WebGPUSwapchainData * swapchainData = & windowData -> swapchainData ;
2380
2355
swapchainData -> surface = wgpuInstanceCreateSurface (renderer -> instance , & surf_desc );
2356
+ // -----
2357
+
2381
2358
swapchainData -> format = wgpuSurfaceGetPreferredFormat (swapchainData -> surface , renderer -> adapter );
2382
2359
swapchainData -> presentMode = SDLToWGPUPresentMode (windowData -> presentMode );
2360
+ wgpuSurfaceConfigure (swapchainData -> surface , & (WGPUSurfaceConfiguration ){
2361
+ .usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc | WGPUTextureUsage_CopyDst ,
2362
+ .format = swapchainData -> format ,
2363
+ .width = windowData -> window -> w ,
2364
+ .height = windowData -> window -> h ,
2365
+ .presentMode = swapchainData -> presentMode ,
2366
+ .alphaMode = WGPUCompositeAlphaMode_Opaque ,
2367
+ .device = renderer -> device ,
2368
+ });
2383
2369
2384
2370
// Swapchain should be the size of whatever SDL_Window it is attached to
2385
2371
swapchainData -> width = windowData -> window -> w ;
@@ -2390,17 +2376,6 @@ static void WebGPU_CreateSwapchain(WebGPURenderer *renderer, WindowData *windowD
2390
2376
swapchainData -> depthStencilView = NULL ;
2391
2377
swapchainData -> depthStencilTexture = NULL ;
2392
2378
2393
- // Emscripten WebGPU swapchain
2394
- WGPUSwapChainDescriptor swapchainDesc = {
2395
- .usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc | WGPUTextureUsage_CopyDst ,
2396
- .format = swapchainData -> format ,
2397
- .width = swapchainData -> width ,
2398
- .height = swapchainData -> height ,
2399
- .presentMode = swapchainData -> presentMode ,
2400
- };
2401
-
2402
- swapchainData -> swapchain = wgpuDeviceCreateSwapChain (renderer -> device , swapchainData -> surface , & swapchainDesc );
2403
-
2404
2379
// Depth/stencil texture for swapchain
2405
2380
WGPUTextureDescriptor depthDesc = {
2406
2381
.usage = WGPUTextureUsage_RenderAttachment ,
@@ -2444,7 +2419,7 @@ static void WebGPU_CreateSwapchain(WebGPURenderer *renderer, WindowData *windowD
2444
2419
swapchainData -> msaaView = wgpuTextureCreateView (swapchainData -> msaaTexture , NULL );
2445
2420
}
2446
2421
2447
- SDL_Log ("WebGPU: Created swapchain %p of size %dx%d" , swapchainData -> swapchain , swapchainData -> width , swapchainData -> height );
2422
+ SDL_Log ("WebGPU: Created swapchain surface %p of size %dx%d" , swapchainData -> surface , swapchainData -> width , swapchainData -> height );
2448
2423
}
2449
2424
2450
2425
static SDL_GPUTextureFormat WebGPU_GetSwapchainTextureFormat (
@@ -2475,10 +2450,6 @@ static void WebGPU_DestroySwapchain(WebGPUSwapchainData *swapchainData)
2475
2450
wgpuTextureRelease (swapchainData -> depthStencilTexture );
2476
2451
swapchainData -> depthStencilTexture = NULL ;
2477
2452
}
2478
- if (swapchainData -> swapchain ) {
2479
- wgpuSwapChainRelease (swapchainData -> swapchain );
2480
- swapchainData -> swapchain = NULL ;
2481
- }
2482
2453
if (swapchainData -> surface ) {
2483
2454
wgpuSurfaceRelease (swapchainData -> surface );
2484
2455
swapchainData -> surface = NULL ;
@@ -2492,6 +2463,32 @@ static void WebGPU_RecreateSwapchain(WebGPURenderer *renderer, WindowData *windo
2492
2463
windowData -> needsSwapchainRecreate = false;
2493
2464
}
2494
2465
2466
+ static WGPUTexture WebGPU_INTERNAL_AcquireSurfaceTexture (WebGPURenderer * renderer , WindowData * windowData )
2467
+ {
2468
+ WGPUSurfaceTexture surfaceTexture ;
2469
+ wgpuSurfaceGetCurrentTexture (windowData -> swapchainData .surface , & surfaceTexture );
2470
+
2471
+ switch (surfaceTexture .status ) {
2472
+ case WGPUSurfaceGetCurrentTextureStatus_Success :
2473
+ break ;
2474
+ case WGPUSurfaceGetCurrentTextureStatus_DeviceLost :
2475
+ SDL_LogError (SDL_LOG_CATEGORY_GPU , "GPU DEVICE LOST" );
2476
+ SDL_SetError ("GPU DEVICE LOST" );
2477
+ return NULL ;
2478
+ case WGPUSurfaceGetCurrentTextureStatus_OutOfMemory :
2479
+ SDL_OutOfMemory ();
2480
+ return NULL ;
2481
+ case WGPUSurfaceGetCurrentTextureStatus_Timeout :
2482
+ case WGPUSurfaceGetCurrentTextureStatus_Outdated :
2483
+ case WGPUSurfaceGetCurrentTextureStatus_Lost :
2484
+ default :
2485
+ WebGPU_RecreateSwapchain (renderer , windowData );
2486
+ return NULL ;
2487
+ }
2488
+
2489
+ return surfaceTexture .texture ;
2490
+ }
2491
+
2495
2492
static bool WebGPU_AcquireSwapchainTexture (
2496
2493
SDL_GPUCommandBuffer * commandBuffer ,
2497
2494
SDL_Window * window ,
@@ -2516,11 +2513,11 @@ static bool WebGPU_AcquireSwapchainTexture(
2516
2513
}
2517
2514
}
2518
2515
2519
- // Get the current texture view from the swapchain
2520
- WGPUTextureView currentView = wgpuSwapChainGetCurrentTextureView ( swapchainData -> swapchain );
2521
- if (currentView == NULL ) {
2522
- SDL_LogError (SDL_LOG_CATEGORY_GPU , "Failed to acquire texture view from swapchain" );
2523
- SDL_SetError ("Failed to acquire texture view from swapchain" );
2516
+ // Get the current texture from the swapchain
2517
+ WGPUTexture currentTexture = WebGPU_INTERNAL_AcquireSurfaceTexture ( renderer , windowData );
2518
+ if (currentTexture == NULL ) {
2519
+ SDL_LogError (SDL_LOG_CATEGORY_GPU , "Failed to acquire texture from swapchain" );
2520
+ SDL_SetError ("Failed to acquire texture from swapchain" );
2524
2521
return false;
2525
2522
}
2526
2523
@@ -2531,8 +2528,8 @@ static bool WebGPU_AcquireSwapchainTexture(
2531
2528
return false;
2532
2529
}
2533
2530
2534
- texture -> texture = wgpuSwapChainGetCurrentTexture ( swapchainData -> swapchain ) ;
2535
- texture -> fullView = currentView ;
2531
+ texture -> texture = currentTexture ;
2532
+ texture -> fullView = wgpuTextureCreateView ( currentTexture , NULL ) ;
2536
2533
texture -> dimensions = (WGPUExtent3D ){
2537
2534
.width = swapchainData -> width ,
2538
2535
.height = swapchainData -> height ,
0 commit comments