Drawing a Triangle with WebGPU-Native and SDL3 on Windows Отрисовка треугольника на WebGPU-Native и SDL3 под Windows

A step-by-step guide to setting up WebGPU-Native with SDL3 on Windows (MinGW) using C and CMake. Пошаговое руководство по настройке WebGPU-Native с SDL3 под Windows (MinGW) на языке C с помощью CMake.

Before proceeding, ensure you have set up the prerequisite libraries on your system: Перед продолжением убедитесь, что в вашей системе настроены необходимые библиотеки:

Step 1: Create Project Directory Шаг 1: Создание директории проекта

Create a workspace folder and a new directory for your WebGPU-Native and SDL3 project: Создайте рабочую папку и новую директорию для вашего проекта на WebGPU-Native и SDL3:

Windows (Command Prompt / PowerShell)

mkdir C:\projects\simple-triangle-webgpu-sdl3-mingw-c
cd C:\projects\simple-triangle-webgpu-sdl3-mingw-c

Step 2: Create Source Files (src/main.c & CMakeLists.txt) Шаг 2: Создание исходных файлов (src/main.c и CMakeLists.txt)

You can create these files using Notepad, Sublime Text, or VS Code. Вы можете создать эти файлы с помощью Блокнота, Sublime Text или VS Code.

Create a src directory and add main.c along with CMakeLists.txt in the project root: Создайте директорию src и добавьте main.c вместе с CMakeLists.txt в корень проекта:

Windows (Terminal / Notepad)

mkdir src
notepad src\main.c
notepad CMakeLists.txt

1. File Contents for src/main.c 1. Содержимое файла src/main.c

#define SDL_MAIN_USE_CALLBACKS 1

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <webgpu/webgpu.h>
#include <windows.h>

static SDL_Window *window = NULL;
static WGPUInstance instance = NULL;
static WGPUAdapter adapter = NULL;
static WGPUDevice device = NULL;
static WGPUQueue queue = NULL;
static WGPUSurface surface = NULL;
static WGPUSurfaceConfiguration config = { 0 };
static WGPURenderPipeline pipeline = NULL;

static bool is_configured = false;
static bool need_reconfigure = false;

static const int WIN_WIDTH = 280;
static const int WIN_HEIGHT = 280;

static const char *shader_code =
    "@vertex\n"
    "fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4f {\n"
    "    var pos = array<vec2f, 3>(\n"
    "        vec2f(0.0, 0.5),\n"
    "        vec2f(-0.5, -0.5),\n"
    "        vec2f(0.5, -0.5)\n"
    "    );\n"
    "    return vec4f(pos[in_vertex_index], 0.0, 1.0);\n"
    "}\n\n"
    "@fragment\n"
    "fn fs_main() -> @location(0) vec4f {\n"
    "    return vec4f(1.0, 0.5, 0.0, 1.0); // Orange color\n"
    "}\n";

static inline WGPUStringView WGPU_STR(const char *s)
{
    WGPUStringView view = {
        .data = s,
        .length = (s != NULL) ? SDL_strlen(s) : WGPU_STRLEN
    };
    return view;
}

static WGPUSurface CreateWGPUSurface(WGPUInstance inst, SDL_Window *win)
{
    SDL_PropertiesID props = SDL_GetWindowProperties(win);
    HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
    HINSTANCE hinstance = (HINSTANCE)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, NULL);

    WGPUSurfaceSourceWindowsHWND hwndSource = {
        .chain = { .sType = WGPUSType_SurfaceSourceWindowsHWND },
        .hinstance = hinstance,
        .hwnd = hwnd
    };
    WGPUSurfaceDescriptor desc = { .nextInChain = (WGPUChainedStruct *)&hwndSource };
    return wgpuInstanceCreateSurface(inst, &desc);
}

static void handle_device_request(
    WGPURequestDeviceStatus status,
    WGPUDevice res,
    WGPUStringView message,
    void *userdata1,
    void *userdata2)
{
    (void)userdata1;
    (void)userdata2;

    printf("DEVICE CALLBACK: status=%d\n", status);

    if (message.data && message.length > 0)
        printf("DEVICE MESSAGE: %.*s\n",
            (int)message.length,
            message.data);

    if (status == WGPURequestDeviceStatus_Success)
    {
        device = res;
        printf("DEVICE CREATED\n");
    }
    else
    {
        printf("DEVICE CREATION FAILED\n");
    }
}

static void handle_adapter_request(
    WGPURequestAdapterStatus status,
    WGPUAdapter res,
    WGPUStringView message,
    void *userdata1,
    void *userdata2)
{
    (void)userdata1;
    (void)userdata2;

    printf("ADAPTER CALLBACK: status=%d\n", status);

    if (message.data && message.length > 0)
        printf("ADAPTER MESSAGE: %.*s\n",
            (int)message.length,
            message.data);

    if (status == WGPURequestAdapterStatus_Success)
    {
        adapter = res;
        printf("ADAPTER CREATED\n");

        WGPURequestDeviceCallbackInfo deviceCallbackInfo = {
            .mode = WGPUCallbackMode_AllowSpontaneous,
            .callback = handle_device_request
        };

        wgpuAdapterRequestDevice(adapter, NULL, deviceCallbackInfo);
    }
    else
    {
        printf("ADAPTER CREATION FAILED\n");
    }
}

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
    (void)appstate;
    (void)argc;
    (void)argv;

    if (!SDL_Init(SDL_INIT_VIDEO))
        return SDL_APP_FAILURE;

    window = SDL_CreateWindow("WebGPU Triangle", WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_RESIZABLE);
    if (!window)
        return SDL_APP_FAILURE;

    instance = wgpuCreateInstance(NULL);
    if (!instance)
        return SDL_APP_FAILURE;

    surface = CreateWGPUSurface(instance, window);
    if (!surface)
        return SDL_APP_FAILURE;

    WGPURequestAdapterOptions opt = { .compatibleSurface = surface };

    WGPURequestAdapterCallbackInfo adapterCallbackInfo = {
        .mode = WGPUCallbackMode_AllowSpontaneous,
        .callback = handle_adapter_request
    };

    wgpuInstanceRequestAdapter(instance, &opt, adapterCallbackInfo);

    while (adapter == NULL || device == NULL)
    {
        SDL_Delay(1);
    }

    return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    (void)appstate;

    if (event->type == SDL_EVENT_QUIT)
    {
        return SDL_APP_SUCCESS;
    }

    if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED ||
        event->type == SDL_EVENT_WINDOW_RESIZED ||
        event->type == SDL_EVENT_WINDOW_RESTORED)
    {
        need_reconfigure = true;
    }

    return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppIterate(void *appstate)
{
    (void)appstate;

    if (!device)
    {
        return SDL_APP_CONTINUE;
    }

    if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
    {
        SDL_Delay(16);
        return SDL_APP_CONTINUE;
    }

    int w = 0, h = 0;
    SDL_GetWindowSizeInPixels(window, &w, &h);
    if (w <= 0 || h <= 0)
    {
        SDL_Delay(16);
        return SDL_APP_CONTINUE;
    }

    if (!is_configured)
    {
        queue = wgpuDeviceGetQueue(device);

        // Dynamically query supported surface capabilities
        WGPUSurfaceCapabilities caps = { 0 };
        wgpuSurfaceGetCapabilities(surface, adapter, &caps);

        WGPUTextureFormat surface_format = WGPUTextureFormat_Undefined;

        for (size_t i = 0; i < caps.formatCount; ++i)
        {
            if (caps.formats[i] == WGPUTextureFormat_BGRA8Unorm)
            {
                surface_format = caps.formats[i];
                break;
            }

            if (caps.formats[i] == WGPUTextureFormat_RGBA8Unorm)
            {
                surface_format = caps.formats[i];
            }
        }

        if (surface_format == WGPUTextureFormat_Undefined)
        {
            surface_format = (caps.formatCount > 0) ? caps.formats[0] : WGPUTextureFormat_BGRA8Unorm;
        }

        config.device = device;
        config.format = surface_format;
        config.usage = WGPUTextureUsage_RenderAttachment;
        config.width = (uint32_t)w;
        config.height = (uint32_t)h;
        config.presentMode = WGPUPresentMode_Fifo;
        wgpuSurfaceConfigure(surface, &config);

        wgpuSurfaceCapabilitiesFreeMembers(caps);

        WGPUShaderSourceWGSL wgsl_desc = { 0 };
        wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
        wgsl_desc.code = WGPU_STR(shader_code);

        WGPUShaderModuleDescriptor shader_desc = { 0 };
        shader_desc.nextInChain = &wgsl_desc.chain;
        WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc);

        WGPURenderPipelineDescriptor pipeline_desc = { 0 };
        pipeline_desc.vertex.module = shader_module;
        pipeline_desc.vertex.entryPoint = WGPU_STR("vs_main");

        WGPUColorTargetState color_target = { .format = config.format, .writeMask = WGPUColorWriteMask_All };
        WGPUFragmentState fragment_state = {
            .module = shader_module,
            .entryPoint = WGPU_STR("fs_main"),
            .targetCount = 1,
            .targets = &color_target
        };
        pipeline_desc.fragment = &fragment_state;
        pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
        pipeline_desc.multisample.count = 1;
        pipeline_desc.multisample.mask = 0xFFFFFFFF;

        pipeline = wgpuDeviceCreateRenderPipeline(device, &pipeline_desc);
        wgpuShaderModuleRelease(shader_module);

        is_configured = true;
        need_reconfigure = false;
        printf("WebGPU Device/Queue ready and Surface configured.\n");
    }
    else if (need_reconfigure || config.width != (uint32_t)w || config.height != (uint32_t)h)
    {
        config.width = (uint32_t)w;
        config.height = (uint32_t)h;
        wgpuSurfaceConfigure(surface, &config);
        need_reconfigure = false;
    }

    WGPUSurfaceTexture surfaceTexture = { 0 };
    wgpuSurfaceGetCurrentTexture(surface, &surfaceTexture);

    if (surfaceTexture.status == WGPUSurfaceGetCurrentTextureStatus_Outdated ||
        surfaceTexture.status == WGPUSurfaceGetCurrentTextureStatus_Lost)
    {
        if (surfaceTexture.texture)
        {
            wgpuTextureRelease(surfaceTexture.texture);
        }
        need_reconfigure = true;
        SDL_Delay(1);
        return SDL_APP_CONTINUE;
    }

    if (surfaceTexture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal &&
        surfaceTexture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal)
    {
        if (surfaceTexture.texture)
        {
            wgpuTextureRelease(surfaceTexture.texture);
        }
        SDL_Delay(1);
        return SDL_APP_CONTINUE;
    }

    if (surfaceTexture.status == WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal)
    {
        need_reconfigure = true;
    }

    WGPUTextureView view = wgpuTextureCreateView(surfaceTexture.texture, NULL);
    WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, NULL);

    WGPURenderPassColorAttachment colorAttachment = {
        .nextInChain = NULL,
        .view = view,
        .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED,
        .resolveTarget = NULL,
        .loadOp = WGPULoadOp_Clear,
        .storeOp = WGPUStoreOp_Store,
        .clearValue = { 0.2, 0.2, 0.2, 1.0 }
    };

    WGPURenderPassDescriptor renderPassDesc = {
        .colorAttachmentCount = 1,
        .colorAttachments = &colorAttachment
    };

    WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderPassDesc);
    wgpuRenderPassEncoderSetPipeline(pass, pipeline);
    wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
    wgpuRenderPassEncoderEnd(pass);

    WGPUCommandBuffer commandBuffer = wgpuCommandEncoderFinish(encoder, NULL);
    wgpuQueueSubmit(queue, 1, &commandBuffer);

    wgpuSurfacePresent(surface);

    if (commandBuffer)
    {
        wgpuCommandBufferRelease(commandBuffer);
    }
    if (pass)
    {
        wgpuRenderPassEncoderRelease(pass);
    }
    if (encoder)
    {
        wgpuCommandEncoderRelease(encoder);
    }
    if (view)
    {
        wgpuTextureViewRelease(view);
    }
    if (surfaceTexture.texture)
    {
        wgpuTextureRelease(surfaceTexture.texture);
    }

    return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
    (void)appstate;
    (void)result;

    if (pipeline)
        wgpuRenderPipelineRelease(pipeline);
    if (surface)
        wgpuSurfaceUnconfigure(surface);
    if (queue)
        wgpuQueueRelease(queue);
    if (device)
        wgpuDeviceRelease(device);
    if (adapter)
        wgpuAdapterRelease(adapter);
    if (surface)
        wgpuSurfaceRelease(surface);
    if (instance)
        wgpuInstanceRelease(instance);
    if (window)
        SDL_DestroyWindow(window);
    SDL_Quit();
}

2. File Contents for CMakeLists.txt 2. Содержимое файла CMakeLists.txt

cmake_minimum_required(VERSION 3.21)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug") # "Debug" or "Release"
endif()

project(simple-triangle-webgpu-sdl3-mingw-c LANGUAGES C)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)

# =============================================================================
# Paths (All library versions visible in one place)
# =============================================================================
set(LIBS_ROOT "C:/libs")
set(SDL3_PATH "${LIBS_ROOT}/sdl3-3.4.20-mingw")
set(WGPU_PATH "${LIBS_ROOT}/wgpu-native-29.0.1.1-mingw")

# =============================================================================
# Target
# =============================================================================
set(APP_TARGET app)

set(PROJECT_SOURCES
    src/main.c
)

add_executable(${APP_TARGET} ${PROJECT_SOURCES})

target_include_directories(${APP_TARGET} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# =============================================================================
# Packages & Imported Libraries
# =============================================================================

# --- SDL3 (Official Config -> handles winmm/imm32/version/setupapi/...) ------
find_package(SDL3 REQUIRED CONFIG
    PATHS "${SDL3_PATH}/lib/cmake/SDL3"
    NO_DEFAULT_PATH
    NO_CMAKE_FIND_ROOT_PATH
)

if(TARGET SDL3::SDL3-static)
    set(SDL3_TARGET SDL3::SDL3-static)
else()
    set(SDL3_TARGET SDL3::SDL3)
endif()

# --- wgpu-native (No CMake config -> manual IMPORTED target) -----------------
add_library(wgpu_native STATIC IMPORTED)
set_target_properties(wgpu_native PROPERTIES
    IMPORTED_LOCATION "${WGPU_PATH}/lib/libwgpu_native.a"
    INTERFACE_INCLUDE_DIRECTORIES "${WGPU_PATH}/include"
    INTERFACE_LINK_LIBRARIES "ws2_32;userenv;ntdll;bcrypt;d3d12;dxgi"
)

# =============================================================================
# Link Libraries, Flags & Stripping
# =============================================================================
target_link_libraries(${APP_TARGET} PRIVATE
    ${SDL3_TARGET}
    wgpu_native
)

target_link_options(${APP_TARGET} PRIVATE -static -static-libgcc)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_link_options(${APP_TARGET} PRIVATE -mconsole)
else()
    target_compile_options(${APP_TARGET} PRIVATE -Os)
    target_link_options(${APP_TARGET} PRIVATE -s -mwindows)
endif()

Step 3: Create Build & Execution Scripts Шаг 3: Создание скриптов сборки и запуска

Windows (MinGW / Batch Scripts)

config-win.bat

cmake -G Ninja -S . -B dist/win

build-win.bat

cmake --build dist/win

run-win.bat

@echo off
.\dist\win\app.exe

Run the scripts sequentially in Command Prompt or PowerShell: Последовательно запустите скрипты в командной строке или PowerShell:

config-win.bat
build-win.bat
run-win.bat

Complete unified source code and scripts are available on GitHub: Полный исходный код и скрипты сборки доступны на GitHub:

View on GitHub Смотреть на GitHub

Support My Work Поддержать проект

If these tutorials helped you, consider buying me a coffee! Если эти туториалы вам помогли, вы можете поддержать автора.

Sberbank (Russia only) Сбербанк (только для РФ)

Sberbank SBP QR Code

Direct transfer via phone number (Russia only) Перевод по номеру телефона (только для РФ)

+7 (917) 212-29-59

USDT TRC20

USDT TRC20 QR Code

Support via Cryptocurrency Поддержка криптовалютой

TMtY1YifNf6FKvgeFmqKGQR4NStKr3csGp