diff --git a/.github/actions/setup-dawn/action.yml b/.github/actions/setup-dawn/action.yml new file mode 100644 index 0000000000..b64b114cd3 --- /dev/null +++ b/.github/actions/setup-dawn/action.yml @@ -0,0 +1,87 @@ +name: 'Setup Google Dawn' +description: 'Download Dawn GitHub release archive' +inputs: + platform: + description: 'Platform (macos-latest, ubuntu-latest, windows-latest)' + required: true + shell: + description: 'Shell' + required: true + dawn-release-name: + description: 'git tag of the Google Dawn release' + default: 'v20260819.212523' + dawn-git-tag: + description: 'git hash of dawn release (depends on dawn-release-name)' + default: 'e463578aa391834c1cf889ef0024e9978057f39a' + +runs: + using: 'composite' + steps: + - name: 'Calculate variables' + id: calc + shell: sh + run: | + case "${{ inputs.platform }}" in + "macos-latest" | "ubuntu-latest" | "windows-latest") + # https://github.com/google/dawn/releases/download/v20260819.212523/Dawn-e463578aa391834c1cf889ef0024e9978057f39a-macos-latest-Release.tar.gz + # https://github.com/google/dawn/releases/download/v20260819.212523/Dawn-e463578aa391834c1cf889ef0024e9978057f39a-ubuntu-latest-Release.tar.gz + # https://github.com/google/dawn/releases/download/v20260819.212523/Dawn-e463578aa391834c1cf889ef0024e9978057f39a-windows-latest-Release.tar.gz + dirname="Dawn-${{ inputs.dawn-git-tag }}-${{ inputs.platform }}-Release" + archive="${dirname}.tar.gz" + url="https://github.com/google/dawn/releases/download/${{ inputs.dawn-release-name }}/$archive" + ;; + *) + echo "Unsupported ${{ runner.os }}-${{ runner.inputs.platform }}" + exit 1; + ;; + esac + extract_dir="$(echo '${{ runner.temp }}' | sed -E 's#\\#/#g')" + extract_dir_unix="${extract_dir}" + if [ x${{ runner.os }} = xWindows ]; then + extract_dir_unix="$(cygpath -u "$extract_dir")" + fi + echo "extract-dir=${extract_dir}" >> ${GITHUB_OUTPUT} + echo "extract-dir-unix=${extract_dir_unix}" >> ${GITHUB_OUTPUT} + echo "archive=${archive}" >> ${GITHUB_OUTPUT} + echo "dirname=${dirname}" >> ${GITHUB_OUTPUT} + echo "url=${url}" >> ${GITHUB_OUTPUT} + echo "cache-key=${archive}-${{ inputs.platform }}" >> ${GITHUB_OUTPUT} + - name: 'Restore cached ${{ steps.calc.outputs.archive }}' + id: cache-restore + uses: actions/cache/restore@v5 + with: + path: '${{ steps.calc.outputs.extract-dir }}/${{ steps.calc.outputs.archive }}' + key: ${{ steps.calc.outputs.cache-key }} + - name: 'Download Dawn ${{ inputs.dawn-release-name }} for ${{ inputs.platform }}' + if: ${{ !steps.cache-restore.outputs.cache-hit || steps.cache-restore.outputs.cache-hit == 'false' }} + shell: pwsh + run: | + Invoke-WebRequest "${{ steps.calc.outputs.url }}" -OutFile "${{ steps.calc.outputs.extract-dir }}/${{ steps.calc.outputs.archive }}" + - name: 'Cache ${{ steps.calc.outputs.archive }}' + if: ${{ !steps.cache-restore.outputs.cache-hit || steps.cache-restore.outputs.cache-hit == 'false' }} + uses: actions/cache/save@v5 + with: + path: '${{ steps.calc.outputs.extract-dir }}/${{ steps.calc.outputs.archive }}' + key: ${{ steps.calc.outputs.cache-key }} + - name: 'Extract Dawn' + shell: sh + run: | + cd "${{ steps.calc.outputs.extract-dir-unix }}" + tar xf "${{ steps.calc.outputs.extract-dir-unix }}/${{ steps.calc.outputs.archive }}" + if [ ! -d "${{ steps.calc.outputs.extract-dir-unix }}/${{ steps.calc.outputs.dirname }}" ]; then + echo "Extraction failes: ${{ steps.calc.outputs.extract-dir }}/${{ steps.calc.outputs.dirname }} directory does not exist" + exit 1 + fi + - name: 'Set output variables' + id: final + shell: ${{ inputs.shell }} + run: | + dawn_prefix="${{ steps.calc.outputs.extract-dir }}/${{ steps.calc.outputs.dirname }}" + if [ -f "${dawn_prefix}/cmake/DawnConfig.cmake" ]; then + echo "Dawn_DIR=${dawn_prefix}/cmake" >> ${GITHUB_ENV} + elif [ -d "${dawn_prefix}/lib/cmake" ]; then + echo "Dawn_DIR=${dawn_prefix}/lib/cmake" >> ${GITHUB_ENV} + elif [ -d "${dawn_prefix}/lib64/cmake" ]; then + echo "Dawn_DIR=${dawn_prefix}/lib64/cmake" >> ${GITHUB_ENV} + fi + echo "Dawn_ROOT=${dawn_prefix}" >> ${GITHUB_ENV} diff --git a/.github/workflows/create-test-plan.py b/.github/workflows/create-test-plan.py index cb6116ba63..325bf37ab1 100755 --- a/.github/workflows/create-test-plan.py +++ b/.github/workflows/create-test-plan.py @@ -133,6 +133,7 @@ class JobSpec: gdk: bool = False vita_gles: Optional[VitaGLES] = None more_hard_deps: bool = False + dawn_webgpu: bool = False allow_failure: bool = False @@ -149,7 +150,7 @@ JOB_SPECS = { "msvc-clang-x86": JobSpec(name="Windows (MSVC, clang-cl x86)", priority=False, os=JobOs.WindowsLatest, platform=SdlPlatform.Msvc, artifact="SDL-clang-cl-x86", msvc_arch=MsvcArch.X86, clang_cl=True, ), "msvc-gdk-x64": JobSpec(name="GDK (MSVC, x64)", priority=False, os=JobOs.Windows2022, platform=SdlPlatform.Msvc, artifact="SDL-VC-GDK", msvc_arch=MsvcArch.X64, msvc_project="VisualC-GDK/SDL.sln", gdk=True, no_cmake=True, ), "ubuntu-22.04": JobSpec(name="Ubuntu 22.04", priority=False, os=JobOs.Ubuntu22_04, platform=SdlPlatform.Linux, artifact="SDL-ubuntu22.04", ), - "ubuntu-latest": JobSpec(name="Ubuntu (latest)", priority=False, os=JobOs.UbuntuLatest, platform=SdlPlatform.Linux, artifact="SDL-ubuntu-latest", ), + "ubuntu-latest": JobSpec(name="Ubuntu (latest)", priority=False, os=JobOs.UbuntuLatest, platform=SdlPlatform.Linux, artifact="SDL-ubuntu-latest", dawn_webgpu=True, ), "ubuntu-24.04-arm64": JobSpec(name="Ubuntu 24.04 (ARM64)", priority=False, os=JobOs.Ubuntu24_04_arm, platform=SdlPlatform.Linux, artifact="SDL-ubuntu24.04-arm64", ), "steamrt3": JobSpec(name="Steam Linux Runtime 3.0 (x86_64)", priority=False, os=JobOs.UbuntuLatest, platform=SdlPlatform.Linux, artifact="SDL-steamrt3", container="registry.gitlab.steamos.cloud/steamrt/sniper/sdk:latest" ), "steamrt4": JobSpec(name="Steam Linux Runtime 4.0 (x86_64)", priority=True, os=JobOs.UbuntuLatest, platform=SdlPlatform.Linux, artifact="SDL-steamrt4", container="registry.gitlab.steamos.cloud/steamrt/steamrt4/sdk:latest", more_hard_deps = True, ), @@ -274,6 +275,7 @@ class JobDetails: setup_gage_sdk_path: str = "" binutils_strings: str = "strings" ctest_args: str = "" + dawn_platform: bool = False allow_failure: bool = False def to_workflow(self, enable_artifacts: bool) -> dict[str, str|bool]: @@ -348,6 +350,7 @@ class JobDetails: "setup-ngage-sdk-path": self.setup_gage_sdk_path, "binutils-strings": self.binutils_strings, "ctest-args": self.ctest_args, + "dawn-platform": self.dawn_platform, "allow-failure": self.allow_failure, } return {k: v for k, v in data.items() if v != ""} @@ -544,6 +547,9 @@ def spec_to_job(spec: JobSpec, key: str, trackmem_symbol_names: bool, ctest_args job.apt_packages.extend(( "libunwind-dev", # For SDL_test memory tracking )) + if spec.dawn_webgpu: + job.cmake_arguments.append("-DDawn_DIR=${Dawn_DIR}") + job.dawn_platform = "ubuntu-latest" job.ccache = True if trackmem_symbol_names: # older libunwind is slow @@ -830,6 +836,9 @@ def spec_to_job(spec: JobSpec, key: str, trackmem_symbol_names: bool, ctest_args job.msys2_packages.append(f"{msys2_env}-ffmpeg") if job.ccache: job.msys2_packages.append(f"{msys2_env}-ccache") + if spec.dawn_webgpu: + job.cmake_arguments.append("-DDawn_DIR=${Dawn_DIR}") + job.dawn_platform = "windows-latest" case SdlPlatform.Cygwin: job.ccache = False # Missing evict-older-than option job.clang_tidy = False # error finding files [clang-diagnostic-error] cause might be space in command path diff --git a/.github/workflows/generic.yml b/.github/workflows/generic.yml index e33fdc9b37..57751be676 100644 --- a/.github/workflows/generic.yml +++ b/.github/workflows/generic.yml @@ -55,7 +55,7 @@ jobs: - uses: mymindstorm/setup-emsdk@v15 if: ${{ matrix.platform.platform == 'emscripten' }} with: - version: 3.1.35 + version: 4.0.10 - uses: browser-actions/setup-chrome@v2 id: setup-chrome if: ${{ matrix.platform.platform == 'emscripten' && matrix.platform.run-tests }} @@ -94,6 +94,12 @@ jobs: with: arch: ${{ matrix.platform.msvc-vcvars-arch }} sdk: ${{ matrix.platform.msvc-vcvars-sdk }} + - name: 'Set up Google Dawn SDK' + uses: ./.github/actions/setup-dawn + if: ${{ matrix.platform.dawn-platform != '' }} + with: + platform: ${{ matrix.platform.dawn-platform }} + shell: ${{ matrix.platform.shell }} - name: 'Set up Nokia N-Gage SDK' uses: ./.github/actions/setup-ngage-sdk if: ${{ matrix.platform.setup-ngage-sdk-path != '' }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 475a32afbf..8da0087d43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -288,6 +288,8 @@ endif() set(SDL_VULKAN_DEFAULT ON) +set(SDL_WGPU_DEFAULT ON) + set(SDL_X11_XRANDR_DEFAULT ON) if(SOLARIS) set(SDL_VULKAN_DEFAULT OFF) @@ -381,6 +383,8 @@ dep_option(SDL_RENDER_METAL "Enable the Metal render driver" ON "SDL_REND dep_option(SDL_RENDER_GPU "Enable the SDL_GPU render driver" ON "SDL_RENDER;SDL_GPU" OFF) dep_option(SDL_VIVANTE "Use Vivante EGL video driver" ON "${UNIX_SYS};SDL_CPU_ARM32" OFF) dep_option(SDL_VULKAN "Enable Vulkan support" "${SDL_VULKAN_DEFAULT}" "SDL_VIDEO;ANDROID OR APPLE OR LINUX OR FREEBSD OR OPENBSD OR WINDOWS OR CYGWIN OR OHOS" OFF) +dep_option(SDL_WEBGPU "Enable experimental WebGPU support" "${SDL_WGPU_DEFAULT}" "SDL_GPU;WINDOWS OR UNIX OR EMSCRIPTEN;NOT APPLE" OFF) +dep_option(SDL_WEBGPU_EMSCRIPTEN "Allow SDL to automatically link with Emscripten's WebGPU implementation" "TRUE" "SDL_WEBGPU;EMSCRIPTEN" OFF) dep_option(SDL_RENDER_VULKAN "Enable the Vulkan render driver" ON "SDL_RENDER;SDL_VULKAN" OFF) dep_option(SDL_METAL "Enable Metal support" ON "APPLE" OFF) set_option(SDL_OPENVR "Use OpenVR video driver" OFF) @@ -1891,6 +1895,7 @@ elseif(EMSCRIPTEN) # project. Uncomment at will for verbose cross-compiling -I/../ path info. sdl_compile_options(PRIVATE "-Wno-warn-absolute-paths") + CheckWebGPU() if(NOT SDL_EMSCRIPTEN_PERSISTENT_PATH STREQUAL "") set(SDL_EMSCRIPTEN_PERSISTENT_PATH_STRING "${SDL_EMSCRIPTEN_PERSISTENT_PATH}") sdl_link_dependency(idbfs LIBS idbfs.js) @@ -2088,6 +2093,7 @@ elseif(UNIX AND NOT (APPLE OR RISCOS OR HAIKU OR CYGWIN)) CheckVivante() CheckVulkan() CheckQNXScreen() + CheckWebGPU() endif() if(SDL_TRAY) @@ -2691,6 +2697,10 @@ elseif(WINDOWS OR CYGWIN) set(HAVE_RENDER_VULKAN TRUE) endif() endif() + + if(SDL_WEBGPU) + CheckWebGPU() + endif() endif() if(SDL_TRAY) @@ -3874,6 +3884,14 @@ if(SDL_GPU) set(SDL_GPU_VULKAN 1) set(HAVE_SDL_GPU TRUE) endif() + if(SDL_VIDEO_WEBGPU) + sdl_glob_sources( + "${SDL3_SOURCE_DIR}/src/gpu/webgpu/*.c" + "${SDL3_SOURCE_DIR}/src/gpu/webgpu/*.h" + ) + set(SDL_GPU_WEBGPU 1) + set(HAVE_SDL_GPU TRUE) + endif() if(SDL_VIDEO_METAL) sdl_glob_sources( "${SDL3_SOURCE_DIR}/src/gpu/metal/*.m" diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake index dddcefcba0..13ab397337 100644 --- a/cmake/sdlchecks.cmake +++ b/cmake/sdlchecks.cmake @@ -620,24 +620,24 @@ macro(CheckLibThai) endmacro() macro(WaylandProtocolGen _SCANNER _CODE_MODE _XML _PROTL) - set(_WAYLAND_PROT_C_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-protocol.c") - set(_WAYLAND_PROT_H_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-client-protocol.h") + set(_WAYLAND_PROT_C_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-protocol.c") + set(_WAYLAND_PROT_H_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-client-protocol.h") - add_custom_command( + add_custom_command( OUTPUT "${_WAYLAND_PROT_H_CODE}" DEPENDS "${_XML}" COMMAND "${_SCANNER}" ARGS client-header "${_XML}" "${_WAYLAND_PROT_H_CODE}" ) - add_custom_command( + add_custom_command( OUTPUT "${_WAYLAND_PROT_C_CODE}" DEPENDS "${_WAYLAND_PROT_H_CODE}" COMMAND "${_SCANNER}" ARGS "${_CODE_MODE}" "${_XML}" "${_WAYLAND_PROT_C_CODE}" ) - sdl_sources("${_WAYLAND_PROT_C_CODE}") + sdl_sources("${_WAYLAND_PROT_C_CODE}") endmacro() # Requires: @@ -949,6 +949,60 @@ macro(CheckVulkan) endif() endmacro() +macro(CheckWebGPU) + set(HAVE_WEBGPU FALSE) + if(SDL_WEBGPU) + if(SDL_WEBGPU_EMSCRIPTEN) + message(STATUS "Using Emdawnwebgpu (remote) as WebGPU implementation") + # NOTE: Since the Emdawnwebgpu port is the official Emscripten provided + # way to link WebGPU, I thought it would be okay to just have SDL do it. + # There's an option to disable this, and since the port is just a python script + # we could use our own if supply-chain attacks are an issue. + # (Although, if somebody's breached the Emscripten repo there's probably bigger issues.) + # https://github.com/emscripten-core/emscripten/blob/main/tools/ports/emdawnwebgpu.py + sdl_compile_options(PRIVATE "--use-port=emdawnwebgpu") + sdl_link_dependency(sdlgpu-emdawnwebgpu LINK_OPTIONS "--use-port=emdawnwebgpu") + set(HAVE_WEBGPU_EMSCRIPTEN TRUE) + set(HAVE_WEBGPU TRUE) + else() + check_language(CXX) + find_package(Dawn QUIET) + if(CMAKE_CXX_COMPILER AND Dawn_FOUND) + find_package(Threads) + set(HAVE_WEBGPU TRUE) + sdl_link_dependency(sdlgpu-dawn-threads CMAKE_MODULE Threads) + sdl_link_dependency(sdlgpu-dawn LIBS dawn::webgpu_dawn CMAKE_MODULE Dawn) + message("-- Using Dawn as WebGPU implementation") + endif() + endif() + if(HAVE_WEBGPU) + set(SDL_VIDEO_WEBGPU 1) + + # Dawn's a C++ library, so we'll have to enable C++ + enable_language(CXX) + + # Dawn includes + if(HAVE_WAYLAND) + sdl_sources("${SDL3_SOURCE_DIR}/src/video/wayland/SDL_waylandwgpu_dawn.cpp") + endif() + + if(HAVE_X11) + sdl_sources("${SDL3_SOURCE_DIR}/src/video/x11/SDL_x11wgpu_dawn.cpp") + endif() + + if(WINDOWS) + sdl_sources("${SDL3_SOURCE_DIR}/src/video/windows/SDL_windowswgpu_dawn.cpp") + endif() + + if(EMSCRIPTEN) + sdl_sources("${SDL3_SOURCE_DIR}/src/video/emscripten/SDL_emscriptenwgpu_dawn.cpp") + endif() + + set(WEBGPU_STATIC 1) + endif() + endif() +endmacro() + # Requires: # - EGL macro(CheckQNXScreen) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 682367197b..3ad4abd614 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -103,7 +103,7 @@ macro(add_sdl_example_executable TARGET) endif() elseif(EMSCRIPTEN) set_property(TARGET ${TARGET} PROPERTY SUFFIX ".html") - target_link_options(${TARGET} PRIVATE -sALLOW_MEMORY_GROWTH=1) + target_link_options(${TARGET} PRIVATE -sALLOW_MEMORY_GROWTH=1 -sASYNCIFY=1) # The WebGPU SDLGPU backend requires Asyncify. elseif(N3DS) set(ROMFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/romfs/${TARGET}") file(MAKE_DIRECTORY "${ROMFS_DIR}") @@ -165,6 +165,7 @@ add_sdl_example_executable(demo-snake SOURCES demo/01-snake/snake.c) add_sdl_example_executable(demo-woodeneye-008 SOURCES demo/02-woodeneye-008/woodeneye-008.c) add_sdl_example_executable(demo-infinite-monkeys SOURCES demo/03-infinite-monkeys/infinite-monkeys.c) add_sdl_example_executable(demo-bytepusher SOURCES demo/04-bytepusher/bytepusher.c) +add_sdl_example_executable(gpu-orbital-simulation SOURCES gpu/01-orbital-simulation/orbit.c gpu/01-orbital-simulation/math.c) # When you add an example, remember to add the Visual Studio project as well: # - Add a new example in examples/ diff --git a/examples/gpu/01-orbital-simulation/README.txt b/examples/gpu/01-orbital-simulation/README.txt new file mode 100644 index 0000000000..fb38532d53 --- /dev/null +++ b/examples/gpu/01-orbital-simulation/README.txt @@ -0,0 +1,4 @@ +This is a small SDLGPU application which simulates asteroids orbiting around Earth. +It uses compute shaders to calculate each asteroid's position and velocity, and instancing to render them. + +Press left/right to decrease/increase timestep, and up/down to increase/decrease the asteroid count. diff --git a/examples/gpu/01-orbital-simulation/math.c b/examples/gpu/01-orbital-simulation/math.c new file mode 100644 index 0000000000..3e7764ea13 --- /dev/null +++ b/examples/gpu/01-orbital-simulation/math.c @@ -0,0 +1,210 @@ +#include "math.h" + +// Matrix Math + +Matrix4x4 Matrix4x4_Multiply(Matrix4x4 matrix1, Matrix4x4 matrix2) { + Matrix4x4 result; + + result.m11 = ((matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21) + + (matrix1.m13 * matrix2.m31) + (matrix1.m14 * matrix2.m41)); + result.m12 = ((matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22) + + (matrix1.m13 * matrix2.m32) + (matrix1.m14 * matrix2.m42)); + result.m13 = ((matrix1.m11 * matrix2.m13) + (matrix1.m12 * matrix2.m23) + + (matrix1.m13 * matrix2.m33) + (matrix1.m14 * matrix2.m43)); + result.m14 = ((matrix1.m11 * matrix2.m14) + (matrix1.m12 * matrix2.m24) + + (matrix1.m13 * matrix2.m34) + (matrix1.m14 * matrix2.m44)); + result.m21 = ((matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21) + + (matrix1.m23 * matrix2.m31) + (matrix1.m24 * matrix2.m41)); + result.m22 = ((matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22) + + (matrix1.m23 * matrix2.m32) + (matrix1.m24 * matrix2.m42)); + result.m23 = ((matrix1.m21 * matrix2.m13) + (matrix1.m22 * matrix2.m23) + + (matrix1.m23 * matrix2.m33) + (matrix1.m24 * matrix2.m43)); + result.m24 = ((matrix1.m21 * matrix2.m14) + (matrix1.m22 * matrix2.m24) + + (matrix1.m23 * matrix2.m34) + (matrix1.m24 * matrix2.m44)); + result.m31 = ((matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + + (matrix1.m33 * matrix2.m31) + (matrix1.m34 * matrix2.m41)); + result.m32 = ((matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + + (matrix1.m33 * matrix2.m32) + (matrix1.m34 * matrix2.m42)); + result.m33 = ((matrix1.m31 * matrix2.m13) + (matrix1.m32 * matrix2.m23) + + (matrix1.m33 * matrix2.m33) + (matrix1.m34 * matrix2.m43)); + result.m34 = ((matrix1.m31 * matrix2.m14) + (matrix1.m32 * matrix2.m24) + + (matrix1.m33 * matrix2.m34) + (matrix1.m34 * matrix2.m44)); + result.m41 = ((matrix1.m41 * matrix2.m11) + (matrix1.m42 * matrix2.m21) + + (matrix1.m43 * matrix2.m31) + (matrix1.m44 * matrix2.m41)); + result.m42 = ((matrix1.m41 * matrix2.m12) + (matrix1.m42 * matrix2.m22) + + (matrix1.m43 * matrix2.m32) + (matrix1.m44 * matrix2.m42)); + result.m43 = ((matrix1.m41 * matrix2.m13) + (matrix1.m42 * matrix2.m23) + + (matrix1.m43 * matrix2.m33) + (matrix1.m44 * matrix2.m43)); + result.m44 = ((matrix1.m41 * matrix2.m14) + (matrix1.m42 * matrix2.m24) + + (matrix1.m43 * matrix2.m34) + (matrix1.m44 * matrix2.m44)); + + return result; +} + +Matrix4x4 Matrix4x4_TransposeToColumnMajor(Matrix4x4 rowMajorMat) { + Matrix4x4 newMatrix = rowMajorMat; + + newMatrix.m12 = rowMajorMat.m21; + newMatrix.m13 = rowMajorMat.m31; + newMatrix.m14 = rowMajorMat.m41; + newMatrix.m21 = rowMajorMat.m12; + newMatrix.m23 = rowMajorMat.m32; + newMatrix.m24 = rowMajorMat.m42; + newMatrix.m31 = rowMajorMat.m13; + newMatrix.m32 = rowMajorMat.m23; + newMatrix.m34 = rowMajorMat.m43; + newMatrix.m41 = rowMajorMat.m14; + newMatrix.m42 = rowMajorMat.m24; + newMatrix.m43 = rowMajorMat.m34; + + return newMatrix; +} + +Matrix4x4 Matrix4x4_CreateRotationZ(float radians) { + return Matrix4x4_TransposeToColumnMajor((Matrix4x4){ + SDL_cosf(radians), SDL_sinf(radians), 0, 0, -SDL_sinf(radians), + SDL_cosf(radians), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}); +} + +Matrix4x4 Matrix4x4_CreateTranslation(float x, float y, float z) { + return Matrix4x4_TransposeToColumnMajor( + (Matrix4x4){1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1}); +} + +Matrix4x4 Matrix4x4_CreateOrthographicOffCenter(float left, float right, + float bottom, float top, + float zNearPlane, + float zFarPlane) { + return Matrix4x4_TransposeToColumnMajor((Matrix4x4){ + 2.0f / (right - left), 0, 0, 0, 0, 2.0f / (top - bottom), 0, 0, 0, 0, + 1.0f / (zNearPlane - zFarPlane), 0, (left + right) / (left - right), + (top + bottom) / (bottom - top), zNearPlane / (zNearPlane - zFarPlane), + 1}); +} + +Matrix4x4 Matrix4x4_CreatePerspectiveFieldOfView(float fieldOfView, + float aspectRatio, + float nearPlaneDistance, + float farPlaneDistance) { + float num = 1.0f / (SDL_tanf(fieldOfView * 0.5f)); + return Matrix4x4_TransposeToColumnMajor((Matrix4x4){ + num / aspectRatio, 0, 0, 0, 0, num, 0, 0, 0, 0, + farPlaneDistance / (nearPlaneDistance - farPlaneDistance), -1, 0, 0, + (nearPlaneDistance * farPlaneDistance) / + (nearPlaneDistance - farPlaneDistance), + 0}); +} + +Matrix4x4 Matrix4x4_CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, + Vector3 cameraUpVector) { + Vector3 targetToPosition = { + cameraPosition.x - cameraTarget.x, + cameraPosition.y - cameraTarget.y, + cameraPosition.z - cameraTarget.z + }; + Vector3 vectorA = Vector3_Normalize(targetToPosition); + Vector3 vectorB = Vector3_Normalize(Vector3_Cross(cameraUpVector, vectorA)); + Vector3 vectorC = Vector3_Cross(vectorA, vectorB); + + return Matrix4x4_TransposeToColumnMajor((Matrix4x4) { + vectorB.x, vectorC.x, vectorA.x, 0, + vectorB.y, vectorC.y, vectorA.y, 0, + vectorB.z, vectorC.z, vectorA.z, 0, + -Vector3_Dot(vectorB, cameraPosition), -Vector3_Dot(vectorC, cameraPosition), -Vector3_Dot(vectorA, cameraPosition), 1 + }); +} + +Matrix4x4 Matrix4x4_Inverse(Matrix4x4 mat) { + float inv[16]; + + const float a[16] = {mat.m11, mat.m21, mat.m31, mat.m41, mat.m12, mat.m22, + mat.m32, mat.m42, mat.m13, mat.m23, mat.m33, mat.m43, + mat.m14, mat.m24, mat.m34, mat.m44}; + + inv[0] = a[5] * a[10] * a[15] - a[5] * a[11] * a[14] - a[9] * a[6] * a[15] + + a[9] * a[7] * a[14] + a[13] * a[6] * a[11] - a[13] * a[7] * a[10]; + + inv[4] = -a[4] * a[10] * a[15] + a[4] * a[11] * a[14] + a[8] * a[6] * a[15] - + a[8] * a[7] * a[14] - a[12] * a[6] * a[11] + a[12] * a[7] * a[10]; + + inv[8] = a[4] * a[9] * a[15] - a[4] * a[11] * a[13] - a[8] * a[5] * a[15] + + a[8] * a[7] * a[13] + a[12] * a[5] * a[11] - a[12] * a[7] * a[9]; + + inv[12] = -a[4] * a[9] * a[14] + a[4] * a[10] * a[13] + a[8] * a[5] * a[14] - + a[8] * a[6] * a[13] - a[12] * a[5] * a[10] + a[12] * a[6] * a[9]; + + inv[1] = -a[1] * a[10] * a[15] + a[1] * a[11] * a[14] + a[9] * a[2] * a[15] - + a[9] * a[3] * a[14] - a[13] * a[2] * a[11] + a[13] * a[3] * a[10]; + + inv[5] = a[0] * a[10] * a[15] - a[0] * a[11] * a[14] - a[8] * a[2] * a[15] + + a[8] * a[3] * a[14] + a[12] * a[2] * a[11] - a[12] * a[3] * a[10]; + + inv[9] = -a[0] * a[9] * a[15] + a[0] * a[11] * a[13] + a[8] * a[1] * a[15] - + a[8] * a[3] * a[13] - a[12] * a[1] * a[11] + a[12] * a[3] * a[9]; + + inv[13] = a[0] * a[9] * a[14] - a[0] * a[10] * a[13] - a[8] * a[1] * a[14] + + a[8] * a[2] * a[13] + a[12] * a[1] * a[10] - a[12] * a[2] * a[9]; + + inv[2] = a[1] * a[6] * a[15] - a[1] * a[7] * a[14] - a[5] * a[2] * a[15] + + a[5] * a[3] * a[14] + a[13] * a[2] * a[7] - a[13] * a[3] * a[6]; + + inv[6] = -a[0] * a[6] * a[15] + a[0] * a[7] * a[14] + a[4] * a[2] * a[15] - + a[4] * a[3] * a[14] - a[12] * a[2] * a[7] + a[12] * a[3] * a[6]; + + inv[10] = a[0] * a[5] * a[15] - a[0] * a[7] * a[13] - a[4] * a[1] * a[15] + + a[4] * a[3] * a[13] + a[12] * a[1] * a[7] - a[12] * a[3] * a[5]; + + inv[14] = -a[0] * a[5] * a[14] + a[0] * a[6] * a[13] + a[4] * a[1] * a[14] - + a[4] * a[2] * a[13] - a[12] * a[1] * a[6] + a[12] * a[2] * a[5]; + + inv[3] = -a[1] * a[6] * a[11] + a[1] * a[7] * a[10] + a[5] * a[2] * a[11] - + a[5] * a[3] * a[10] - a[9] * a[2] * a[7] + a[9] * a[3] * a[6]; + + inv[7] = a[0] * a[6] * a[11] - a[0] * a[7] * a[10] - a[4] * a[2] * a[11] + + a[4] * a[3] * a[10] + a[8] * a[2] * a[7] - a[8] * a[3] * a[6]; + + inv[11] = -a[0] * a[5] * a[11] + a[0] * a[7] * a[9] + a[4] * a[1] * a[11] - + a[4] * a[3] * a[9] - a[8] * a[1] * a[7] + a[8] * a[3] * a[5]; + + inv[15] = a[0] * a[5] * a[10] - a[0] * a[6] * a[9] - a[4] * a[1] * a[10] + + a[4] * a[2] * a[9] + a[8] * a[1] * a[6] - a[8] * a[2] * a[5]; + + float det = a[0] * inv[0] + a[1] * inv[4] + a[2] * inv[8] + a[3] * inv[12]; + + det = 1.0f / det; + + return (Matrix4x4){ + .m11 = inv[0] * det, + .m12 = inv[1] * det, + .m13 = inv[2] * det, + .m14 = inv[3] * det, + .m21 = inv[4] * det, + .m22 = inv[5] * det, + .m23 = inv[6] * det, + .m24 = inv[7] * det, + .m31 = inv[8] * det, + .m32 = inv[9] * det, + .m33 = inv[10] * det, + .m34 = inv[11] * det, + .m41 = inv[12] * det, + .m42 = inv[13] * det, + .m43 = inv[14] * det, + .m44 = inv[15] * det, + }; +} + +Vector3 Vector3_Normalize(Vector3 vec) { + float magnitude = + SDL_sqrtf((vec.x * vec.x) + (vec.y * vec.y) + (vec.z * vec.z)); + return (Vector3){vec.x / magnitude, vec.y / magnitude, vec.z / magnitude}; +} + +float Vector3_Dot(Vector3 vecA, Vector3 vecB) { + return (vecA.x * vecB.x) + (vecA.y * vecB.y) + (vecA.z * vecB.z); +} + +Vector3 Vector3_Cross(Vector3 vecA, Vector3 vecB) { + return (Vector3){vecA.y * vecB.z - vecB.y * vecA.z, + -(vecA.x * vecB.z - vecB.x * vecA.z), + vecA.x * vecB.y - vecB.x * vecA.y}; +} diff --git a/examples/gpu/01-orbital-simulation/math.h b/examples/gpu/01-orbital-simulation/math.h new file mode 100644 index 0000000000..0bc67064ce --- /dev/null +++ b/examples/gpu/01-orbital-simulation/math.h @@ -0,0 +1,37 @@ +// I stole this math library from TheSpydog's SDLGPU examples: +// https://github.com/TheSpydog/SDL_gpu_examples/blob/main/Examples/Common.c +// Sorry! +// TODO: Get more of the gpu_examples stuff into mainline SDL + +#include + +// Matrix Math +typedef struct Matrix4x4 { + float m11, m12, m13, m14; + float m21, m22, m23, m24; + float m31, m32, m33, m34; + float m41, m42, m43, m44; +} Matrix4x4; + +typedef struct Vector3 { + float x, y, z; +} Vector3; + +Matrix4x4 Matrix4x4_Multiply(Matrix4x4 matrix1, Matrix4x4 matrix2); +Matrix4x4 Matrix4x4_CreateRotationZ(float radians); +Matrix4x4 Matrix4x4_CreateTranslation(float x, float y, float z); +Matrix4x4 Matrix4x4_CreateOrthographicOffCenter(float left, float right, + float bottom, float top, + float zNearPlane, + float zFarPlane); +Matrix4x4 Matrix4x4_CreatePerspectiveFieldOfView(float fieldOfView, + float aspectRatio, + float nearPlaneDistance, + float farPlaneDistance); +Matrix4x4 Matrix4x4_CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, + Vector3 cameraUpVector); +Matrix4x4 Matrix4x4_Inverse(Matrix4x4 mat); + +Vector3 Vector3_Normalize(Vector3 vec); +float Vector3_Dot(Vector3 vecA, Vector3 vecB); +Vector3 Vector3_Cross(Vector3 vecA, Vector3 vecB); diff --git a/examples/gpu/01-orbital-simulation/onmouseover.webp b/examples/gpu/01-orbital-simulation/onmouseover.webp new file mode 100644 index 0000000000..6cb9aa4fc6 Binary files /dev/null and b/examples/gpu/01-orbital-simulation/onmouseover.webp differ diff --git a/examples/gpu/01-orbital-simulation/orbit.c b/examples/gpu/01-orbital-simulation/orbit.c new file mode 100644 index 0000000000..aedeaa05be --- /dev/null +++ b/examples/gpu/01-orbital-simulation/orbit.c @@ -0,0 +1,572 @@ +#include + +#define SDL_MAIN_USE_CALLBACKS +#include + +#include "math.h" +#include "shaders.h" + +typedef struct Asteroid +{ + Vector3 pos; + float padding1; + Vector3 vel; + float padding2; +} Asteroid; + +typedef struct UniformDataCompute +{ + float timescale; + float numAsteroids; + float deltaTime; +} UniformDataCompute; + +typedef struct Vertex +{ + float x, y, z; +} Vertex; + +typedef struct AppState +{ + bool shouldRecreateAsteroids; + bool leftClickDown; + + Uint32 numAsteroids; + Uint32 minDistance; + Uint32 maxDistance; + Uint32 cameraDistance; // KM + + float cameraRotation; + float cameraRotationVelocity; + + float deltaTime; + float timescale; + int timescaleIndex; + Uint64 timeLastIter; + + SDL_Window *window; + SDL_GPUDevice *device; + SDL_GPUShader *vs, *fs; + SDL_GPUGraphicsPipeline *graphicsPipeline; + SDL_GPUComputePipeline *computePipeline; + + SDL_GPUBuffer *vtxBuffer, *idxBuffer; + SDL_GPUBuffer *asteroidBuf; +} AppState; + +static AppState app = { 0 }; +#define ASTEROID_SIZE 15000.0f +#define MAX_ASTEROIDS 4000000.0f +#define EARTH_RADIUS 6371000.0f +#define RESOLUTION_X 800.0f +#define RESOLUTION_Y 600.0f + +// By pressing left or right arrow on your keyboard you can decrease/increase the time scale (how fast time goes) +const float timescales[6] = { 1.0f, 10.0f, 100.0f, 1000.0f, 2500.0f, 5000.0f }; + +void InitPipelines(void) +{ + app.vs = + SDL_CreateGPUShader(app.device, &(SDL_GPUShaderCreateInfo){ + .code = (const Uint8 *)vtxShader, + .code_size = SDL_strlen(vtxShader), + .entrypoint = "main", + .format = SDL_GPU_SHADERFORMAT_WGSL, + .num_samplers = 0, + .num_storage_buffers = 0, + .num_storage_textures = 0, + .num_uniform_buffers = 1, + .stage = SDL_GPU_SHADERSTAGE_VERTEX, + }); + + app.fs = + SDL_CreateGPUShader(app.device, &(SDL_GPUShaderCreateInfo){ + .code = (const Uint8 *)frgShader, + .code_size = SDL_strlen(frgShader), + .entrypoint = "main", + .format = SDL_GPU_SHADERFORMAT_WGSL, + .num_samplers = 0, + .num_storage_buffers = 0, + .num_storage_textures = 0, + .num_uniform_buffers = 0, + .stage = SDL_GPU_SHADERSTAGE_FRAGMENT, + }); + + app.graphicsPipeline = SDL_CreateGPUGraphicsPipeline( + app.device, + &(SDL_GPUGraphicsPipelineCreateInfo){ + .target_info = { + .num_color_targets = 1, + .color_target_descriptions = + (SDL_GPUColorTargetDescription[]){ + { + .format = SDL_GetGPUSwapchainTextureFormat( + app.device, app.window), + }, + }, // Note: I couldn't be bothered to do depth testing. Sorry! + }, + .vertex_input_state = (SDL_GPUVertexInputState){ + .num_vertex_buffers = 1, + .vertex_buffer_descriptions = (SDL_GPUVertexBufferDescription[]){ + { + .slot = 0, + .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX, + .instance_step_rate = 0, + .pitch = sizeof(Vertex), + }, + }, + .num_vertex_attributes = 1, + .vertex_attributes = (SDL_GPUVertexAttribute[]){ + { + .buffer_slot = 0, + .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, + .location = 0, + .offset = 0, + }, + }, + }, + .rasterizer_state = { + // We'll enable backface culling here. + // I'm not nearly good enough of a graphics programmer + // to explain what backface culling in a simple manner, + // but TLDR it's pretty much a free performance boost. + .cull_mode = SDL_GPU_CULLMODE_BACK, + .front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE, + }, + .primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .vertex_shader = app.vs, + .fragment_shader = app.fs, + }); + + app.computePipeline = SDL_CreateGPUComputePipeline(app.device, &(SDL_GPUComputePipelineCreateInfo){ + .code = (const Uint8 *)cmpShader, + .code_size = SDL_strlen(cmpShader), + .entrypoint = "main", + .format = SDL_GPU_SHADERFORMAT_WGSL, + .num_samplers = 0, + .num_readonly_storage_buffers = 0, + .num_readonly_storage_textures = 0, + .num_readwrite_storage_buffers = 1, + .num_readwrite_storage_textures = 0, + .num_uniform_buffers = 1, + .threadcount_x = 64, + .threadcount_y = 1, + .threadcount_z = 1, + }); +} + +void CreateAsteroids(SDL_GPUCopyPass *copyPass) +{ + Uint32 i; + + if (app.asteroidBuf != NULL) { + // If we're recreating the asteroids, we'll have to release the previous asteroid buffer + // since otherwise we'd waste a TON of GPU memory and eventually crash. + SDL_ReleaseGPUBuffer(app.device, app.asteroidBuf); + } + + // We'll create a storage buffer which'll store each asteroid's position and velocity. + // This will be written to in the compute shader, and then read from in the vertex shader. + app.asteroidBuf = SDL_CreateGPUBuffer(app.device, &(SDL_GPUBufferCreateInfo){ + .size = sizeof(Asteroid) * app.numAsteroids, + .usage = SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ | + SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE | + SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ, + }); + + SDL_GPUTransferBuffer *asteroidTransferBuffer = SDL_CreateGPUTransferBuffer(app.device, &(SDL_GPUTransferBufferCreateInfo){ + .size = sizeof(Asteroid) * app.numAsteroids, + .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, + }); + + Asteroid *asteroidData = SDL_MapGPUTransferBuffer(app.device, asteroidTransferBuffer, false); + + // This loop will create all the asteroids and randomly spread them out in Earth's orbit. + for (i = 0; i < app.numAsteroids; i++) { + Uint32 distance = SDL_max(SDL_rand(app.maxDistance), (Sint32)app.minDistance); + + float theta = (2.0f * SDL_PI_F * i) / app.numAsteroids; + float finalDist = EARTH_RADIUS + distance; + + asteroidData[i].pos.x = finalDist * SDL_sinf(theta); + asteroidData[i].pos.z = finalDist * SDL_cosf(theta); + + asteroidData[i].vel.x = (7700 * SDL_cosf(theta)) * (1 + SDL_randf() / 5); + asteroidData[i].vel.z = (7700 * -SDL_sinf(theta)) * (1 + SDL_randf() / 5); + // We give some initial random velocity to the Y axis to spread the asteroids out a little. + asteroidData[i].vel.y = 1500 * SDL_randf(); + } + + SDL_UnmapGPUTransferBuffer(app.device, asteroidTransferBuffer); + // We'll upload the transfer buffer's data to the storage buffer with this SDL_UploadToGPUBuffer call. + SDL_UploadToGPUBuffer( + copyPass, + &(SDL_GPUTransferBufferLocation){ + .transfer_buffer = asteroidTransferBuffer, + .offset = 0, + }, + &(SDL_GPUBufferRegion){ + .buffer = app.asteroidBuf, + .offset = 0, + .size = sizeof(Asteroid) * app.numAsteroids, + }, + false); + + // We don't need this transfer buffer any more, so we'll release it to free up GPU memory. + SDL_ReleaseGPUTransferBuffer(app.device, asteroidTransferBuffer); +} + +void InitBuffers(void) +{ + // We'll create our vertex & index buffers here. + // Since we're using instancing, we only need one cube mesh for all asteroids. + app.vtxBuffer = + SDL_CreateGPUBuffer(app.device, &(SDL_GPUBufferCreateInfo){ + .size = sizeof(Vertex) * 24, + .usage = SDL_GPU_BUFFERUSAGE_VERTEX, + }); + app.idxBuffer = + SDL_CreateGPUBuffer(app.device, &(SDL_GPUBufferCreateInfo){ + .size = sizeof(Uint16) * 36, + .usage = SDL_GPU_BUFFERUSAGE_INDEX, + }); + + // We'll have to create a transfer buffer to upload our mesh data. + // We'll be smart here and use the same buffer for both uploads, so we'll make it large enough to fit both the vertex & index data. + SDL_GPUTransferBuffer *meshTransferBuffer = SDL_CreateGPUTransferBuffer(app.device, &(SDL_GPUTransferBufferCreateInfo){ + .size = (sizeof(Uint16) * 36) + (sizeof(Vertex) * 24), + .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, + }); + + // We'll map the transfer buffer so we can upload our cube mesh to the GPU. + Vertex *transferData = SDL_MapGPUTransferBuffer(app.device, meshTransferBuffer, false); + Uint16 *indexData = (Uint16 *)&transferData[24]; + + // This is just a cube's raw data. + transferData[0] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[1] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[2] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[3] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[4] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[5] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[6] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[7] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[8] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[9] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[10] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[11] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[12] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[13] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[14] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[15] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[16] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[17] = (Vertex){ -ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[18] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[19] = (Vertex){ ASTEROID_SIZE, -ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[20] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + transferData[21] = (Vertex){ -ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[22] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, ASTEROID_SIZE }; + transferData[23] = (Vertex){ ASTEROID_SIZE, ASTEROID_SIZE, -ASTEROID_SIZE }; + Uint16 indices[] = { 0, 1, 2, 0, 2, 3, 6, 5, 4, 7, 6, 4, + 8, 9, 10, 8, 10, 11, 14, 13, 12, 15, 14, 12, + 16, 17, 18, 16, 18, 19, 22, 21, 20, 23, 22, 20 }; + SDL_memcpy(indexData, indices, sizeof(indices)); + + // Now that we're done copying the cube mesh data to the transfer buffer, + // we'll unmap the transfer buffer so we can upload to the vertex & index buffers from it. + SDL_UnmapGPUTransferBuffer(app.device, meshTransferBuffer); + + // We'll acquire a command buffer and begin a copy pass. + // In order to upload anything to the GPU in SDLGPU, you must first begin a "copy pass". + // Within this copy pass, you can... copy data. Once you're finished with the copy pass, + // you call SDL_EndGPUCopyPass and then continue on with whatever else you need to do. + SDL_GPUCommandBuffer *cmdBuf = SDL_AcquireGPUCommandBuffer(app.device); + SDL_GPUCopyPass *copyPass = SDL_BeginGPUCopyPass(cmdBuf); + + // While we're here, we'll also upload our initial asteroid data. + CreateAsteroids(copyPass); + + SDL_UploadToGPUBuffer(copyPass, + &(SDL_GPUTransferBufferLocation){ + .transfer_buffer = meshTransferBuffer, + .offset = 0, + }, + &(SDL_GPUBufferRegion){ + .buffer = app.vtxBuffer, + .offset = 0, + .size = sizeof(Vertex) * 24, + }, + false); + + SDL_UploadToGPUBuffer( + copyPass, + &(SDL_GPUTransferBufferLocation){ + .transfer_buffer = meshTransferBuffer, + // Remember that we're using the same transfer buffer here, + // so we'll add the vertex data size to the offset. + .offset = sizeof(Vertex) * 24, + }, + &(SDL_GPUBufferRegion){ + .buffer = app.idxBuffer, + .offset = 0, + .size = sizeof(Uint16) * 36, + }, + false); + + SDL_EndGPUCopyPass(copyPass); + SDL_SubmitGPUCommandBuffer(cmdBuf); + + SDL_ReleaseGPUTransferBuffer(app.device, meshTransferBuffer); +} + +// We're using the SDL callbacks here, since they generally play nicer with Emscripten. +// You can also run a traditional main loop if you want to, but you'll have to do a bit of screwing around to get it working with Emscripten. +SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) +{ + if (!SDL_Init(SDL_INIT_VIDEO)) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed (%s)", SDL_GetError()); + return SDL_APP_FAILURE; + } + + app.window = + SDL_CreateWindow("Orbit Simulation", (int)RESOLUTION_X, (int)RESOLUTION_Y, 0); + if (app.window == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow failed (%s)", SDL_GetError()); + return SDL_APP_FAILURE; + } + + // We'll create the SDL_GPUDevice here. This device will be used to interface with the GPU. + // This function takes in three arguments: The shader format(s) you support, whether or not debug mode is enabled, and additional properties. + // + // The shader format(s) argument allows you to say "Hey, I can give shaders in these formats!". + // This lets you support Vulkan, DX12, Metal, and WebGPU while still using the same SDLGPU code. + // + // However, in this example we are only supporting WGSL, which is WebGPU's shading language. + // (Quite frankly, I just didn't wanna rewrite the shader code in HLSL.) + app.device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_WGSL, true, NULL); + if (app.device == NULL) { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "This example only supports SDL_GPU through webgpu"); + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateGPUDevice failed (%s)", SDL_GetError()); + return SDL_APP_FAILURE; + } + + app.numAsteroids = 100000; + app.minDistance = 75000; + app.maxDistance = 78000; + app.cameraDistance = 10000; // cameraDistance is in kilometers + app.timescale = timescales[3]; + app.timescaleIndex = 3; + + // In order to render an image to the SDL window with SDLGPU, you must call SDL_ClaimWindowForGPUDevice. + // This creates the "swapchain", allowing you to later call SDL_(WaitAnd)AcquireGPUSwapchainTexture. + SDL_ClaimWindowForGPUDevice(app.device, app.window); + + InitPipelines(); + InitBuffers(); + + // We have to set this here, since this is part of the delta time calculations and dividing by 0 is not good. + app.timeLastIter = SDL_GetTicksNS(); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Press left or right to change timescale."); + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Press up or down to increase/decrease asteroid count."); + + // We'll return SDL_APP_CONTINUE, which tells SDL that the app initialization succeeded, and that the app should continue running. + return SDL_APP_CONTINUE; +} + +// This function runs once per frame, and therefore we'll put our GPU rendering code here. +SDL_AppResult SDL_AppIterate(void *appstate) +{ + Uint64 timeLastIter = app.timeLastIter; + + app.timeLastIter = SDL_GetTicksNS(); + app.deltaTime = (app.timeLastIter - timeLastIter) / (float)1e9; + + // This code changes the camera rotation by cameraRotationVelocity. It then also divides the velocity by 1.15f to apply some drag. + app.cameraRotation += app.cameraRotationVelocity; + app.cameraRotationVelocity /= 1.15f; + + // We'll acquire a command buffer here. + // In order to run anything on the GPU, you must first acquire a GPU command buffer. + // After you've acquired the buffer, you proceed to fill it with GPU commands. (e.g: draw this, upload that, clear this, etc...) + // Once you've filled the command buffer with all your GPU commands, you "submit" it. + // This sends the commands to the GPU where they'll be run. + SDL_GPUCommandBuffer *cmdBuf = SDL_AcquireGPUCommandBuffer(app.device); + + // If the asteroid count's changed we'll have to upload the new asteroids. + if (app.shouldRecreateAsteroids) { + // Important note with the WebGPU backend: There's an edge case in WebGPU which makes it so that any swapchain texture you've + // previously acquired will instantly be destroyed if you map a transfer buffer. I have no idea why it does that, but it does. + // Keep that in mind! + SDL_GPUCopyPass *copyPass = SDL_BeginGPUCopyPass(cmdBuf); + + CreateAsteroids(copyPass); + SDL_EndGPUCopyPass(copyPass); + + app.shouldRecreateAsteroids = false; + } + + // This code starts a "compute pass". A compute pass is a pass in which you execute compute shaders. + // This is unlike a copy pass, which copies data, and a render pass, which renders triangles. + // + // A compute shader is a special kind of shader which allows you to run non-graphics related code on a GPU. + // This example makes use of compute shaders to simulate each asteroid's orbit direcly on the GPU, without any input from the CPU. + // This not only simplifies the code, but this also runs much better than if we were to run the simulation on the CPU. + // Using this compute shader, I can simulate 4 million asteroids in less than a millisecond on my 6700XT, without ever uploading any data from the CPU. + // + // (Note that we're binding the asteroid buffer here, since SDLGPU requires R/W compute storage buffers to be bound in SDL_BeginGPUComputePass.) + SDL_GPUComputePass *computePass = SDL_BeginGPUComputePass(cmdBuf, NULL, 0, &(SDL_GPUStorageBufferReadWriteBinding){ .buffer = app.asteroidBuf, .cycle = false }, 1); + + // We'll bind our compute pipeline here. + SDL_BindGPUComputePipeline(computePass, app.computePipeline); + + // We'll push our uniform data here. This doesn't include all that much, just the deltaTime, amount of asteroids, and the timescale. + SDL_PushGPUComputeUniformData(cmdBuf, 0, &(UniformDataCompute){ + .deltaTime = app.deltaTime, + .numAsteroids = (float)app.numAsteroids, + .timescale = app.timescale, + }, + sizeof(UniformDataCompute)); + + // Finally, we'll dispatch our compute. This tells the GPU "hey, run this compute code please". + // Since we've previously set our X thread size to 64, we'll have to divide the asteroid count by 64 to get our thread dispatch size. + SDL_DispatchGPUCompute(computePass, app.numAsteroids / 64, 1, 1); + + // Compute is finished, let's move on to the rendering! + SDL_EndGPUComputePass(computePass); + + SDL_GPUTexture *swapchainTexture = NULL; + // As written about in the asteroid upload code above, WebGPU has a weird edge case where mapping a buffer will destroy any currently acquired swapchain textures. + // So, the swapchain acquisition is done as late as possible here, to prevent shenanigans. + SDL_WaitAndAcquireGPUSwapchainTexture(cmdBuf, app.window, &swapchainTexture, NULL, NULL); // We don't care about the texture's size since we already know it + + // If the swapchain texture's NULL, we won't bother doing anything. + // A NULL swapchain texture means that we can't render to the swapchain right now, for some reason. + // This is normal behaviour (for example: if the window's not visible this'll be NULL), so we'll just skip over the rendering code if that's the case. + if (swapchainTexture != NULL) { + // This is some basic matrix math that creates perspective and also allows us to move the camera around. + Matrix4x4 proj = Matrix4x4_CreatePerspectiveFieldOfView(75.0f * SDL_PI_F / 180.0f, RESOLUTION_X / RESOLUTION_Y, 0.001f, 200.0f); + Matrix4x4 view = Matrix4x4_CreateLookAt( + (Vector3){ + ((float)app.cameraDistance * 1000) * SDL_sinf(app.cameraRotation), + ((float)app.cameraDistance * 1000), + ((float)app.cameraDistance * 1000) * SDL_cosf(app.cameraRotation), + }, + (Vector3){ 0, 0, 0 }, + (Vector3){ 0, 1, 0 }); + Matrix4x4 viewproj = Matrix4x4_Multiply(proj, view); + + // In order to start a render pass in SDLGPU you have to provide your color targets. + // In this example there's only one color target (the swapchain), so that's all we care about. + SDL_GPUColorTargetInfo colorTargetInfo = { 0 }; + colorTargetInfo.texture = swapchainTexture; + colorTargetInfo.clear_color = (SDL_FColor){ 0.0f, 0.0f, 0.0f, 1.0f }; + colorTargetInfo.load_op = SDL_GPU_LOADOP_CLEAR; // We'll tell the GPU to clear the color target to pure black here. + colorTargetInfo.store_op = SDL_GPU_STOREOP_STORE; // We'll want to keep the finished image so that we can display it to the user. + + // Finally, we'll begin our render pass. + SDL_GPURenderPass *renderPass = + SDL_BeginGPURenderPass(cmdBuf, &colorTargetInfo, 1, NULL); + + // We'll first bind our pipeline, vertex & index buffers. + SDL_BindGPUGraphicsPipeline(renderPass, app.graphicsPipeline); + SDL_BindGPUVertexBuffers(renderPass, 0, &(SDL_GPUBufferBinding){ .buffer = app.vtxBuffer, .offset = 0 }, 1); + SDL_BindGPUIndexBuffer(renderPass, &(SDL_GPUBufferBinding){ .buffer = app.idxBuffer, .offset = 0 }, SDL_GPU_INDEXELEMENTSIZE_16BIT); + + // We'll bind our asteroid storage buffer here (which was previously updated by the compute pass) so that the vertex shader can access the asteroid position. + SDL_BindGPUVertexStorageBuffers(renderPass, 0, &app.asteroidBuf, 1); + // We'll push our combined view + projection matrix to the GPU here. + SDL_PushGPUVertexUniformData(cmdBuf, 0, &viewproj, sizeof(Matrix4x4)); + + // Time to draw all of the asteroids. I wasn't very fancy with it, so the "asteroids" are in reality just cubes. + // This example is mostly a showcase of WebGPU's compute abilities. + // You can see here that we pass in app.numAsteroids here as our instance count. + // This tells the GPU "hey, you'll be rendering the exact same thing a billion times, no need for multiple draw calls." + // This greatly speeds up rendering since there's only one draw call for the GPU. + SDL_DrawGPUIndexedPrimitives(renderPass, 36, app.numAsteroids, 0, 0, 0); + + // We're done with our render pass, so we'll end it here. + SDL_EndGPURenderPass(renderPass); + } + + // Finally, it's time to submit our command buffer. + // SDLGPU handles all of the swapchain stuff for you, so we don't need to bother with presentation and such. + SDL_SubmitGPUCommandBuffer(cmdBuf); + + return SDL_APP_CONTINUE; +} + +// This callback gets run whenever an event occurs (e.g keyboard input) +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) +{ + if (event->type == SDL_EVENT_QUIT) { + // If the "quit" event's happened we'll return SDL_APP_SUCCESS, which tells SDL that we're done with our app. + return SDL_APP_SUCCESS; + } + + if (event->type == SDL_EVENT_KEY_DOWN) { + if (event->key.scancode == SDL_SCANCODE_LEFT) { + if (app.timescaleIndex > 0) { + app.timescaleIndex -= 1; + app.timescale = timescales[app.timescaleIndex]; + } + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Timescale: %.3f", app.timescale); + } else if (event->key.scancode == SDL_SCANCODE_RIGHT) { + if (app.timescaleIndex < 5) { + app.timescaleIndex += 1; + app.timescale = timescales[app.timescaleIndex]; + } + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Timescale: %.3f", app.timescale); + } else if (event->key.scancode == SDL_SCANCODE_UP && (app.numAsteroids + 100000) <= MAX_ASTEROIDS) { + app.numAsteroids += 100000; + app.shouldRecreateAsteroids = true; + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Asteroid count: %i", (int)app.numAsteroids); + } else if (event->key.scancode == SDL_SCANCODE_DOWN && ((int)app.numAsteroids - 100000) >= 10000) { + app.numAsteroids -= 100000; + app.shouldRecreateAsteroids = true; + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Asteroid count: %i", (int)app.numAsteroids); + } + } + + if (event->type == SDL_EVENT_MOUSE_WHEEL) { + app.cameraDistance -= event->wheel.integer_y * 500; + } + + // If left click is pressed down on the mouse, we'll hide it and allow the user to rotate the camera view. + if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN || event->type == SDL_EVENT_MOUSE_BUTTON_UP) { + if (event->button.button == 1) { + app.leftClickDown = event->button.down; + SDL_SetWindowRelativeMouseMode(app.window, app.leftClickDown); + } + } + + if (event->type == SDL_EVENT_MOUSE_MOTION) { + if (app.leftClickDown) { + app.cameraRotationVelocity += event->motion.xrel / 2000.0f; + } + } + + return SDL_APP_CONTINUE; +} + +// This callback is called when the app is shutting down. +// We'll free our GPU resources here. +void SDL_AppQuit(void *appstate, SDL_AppResult result) +{ + SDL_ReleaseGPUBuffer(app.device, app.vtxBuffer); + SDL_ReleaseGPUBuffer(app.device, app.idxBuffer); + SDL_ReleaseGPUBuffer(app.device, app.asteroidBuf); + + SDL_ReleaseGPUGraphicsPipeline(app.device, app.graphicsPipeline); + SDL_ReleaseGPUShader(app.device, app.fs); + SDL_ReleaseGPUShader(app.device, app.vs); + + SDL_ReleaseWindowFromGPUDevice(app.device, app.window); + SDL_DestroyGPUDevice(app.device); + + SDL_DestroyWindow(app.window); + SDL_Quit(); +} diff --git a/examples/gpu/01-orbital-simulation/shaders.h b/examples/gpu/01-orbital-simulation/shaders.h new file mode 100644 index 0000000000..9468224587 --- /dev/null +++ b/examples/gpu/01-orbital-simulation/shaders.h @@ -0,0 +1,82 @@ +// These shaders are all in textual WGSL. +// This is for two reasons: +// - 1: This SDL3 example is only ever meant to run on the WebGPU backend, on the SDL website. +// - 2: I didn't want to rewrite these shaders. +// They're also formatted a little grossly since Clang & MSVC don't support R"" strings. + +/** + * This is the vertex shader for each asteroid. + * It reads the asteroid storage buffer (previously used in the compute shader), and displaces each asteroid by the buffer entry's "pos" field. + */ +const char *vtxShader = "\n\ +struct Asteroid {\n\ + pos: vec3,\n\ + vel: vec3,\n\ +};\n\ +@group(0u) @binding(0u) var posBuf: array;\n\ +@group(1u) @binding(0u) var camMatrix: mat4x4;\n\ +\n\ +@vertex\n\ +fn main(@location(0u) pos: vec3, @builtin(instance_index) index: u32) -> @builtin(position) vec4 {\n\ + return vec4(pos + posBuf[index].pos, 1.0f) * camMatrix;\n\ +}"; + +/** + * This is the asteroid fragment shader. + * Very simple, this just returns an almost-pure white. + */ +const char *frgShader = "\n\ +@fragment\n\ +fn main() -> @location(0) vec4 {\n\ + return vec4(0.9f, 0.9f, 0.9f, 1.0f);\n\ +}"; + +/** + * This is the compute shader for the asteroids. + * It uses a simplified version of Newton's law of universal gravitation + * to calculate the force applied to each asteroid. + * F = GM / r² + */ +const char *cmpShader = "\n\ +// This is the struct which stores each asteroid's data.\n\ +struct Asteroid {\n\ + pos: vec3,\n\ + vel: vec3,\n\ +};\n\ +\n\ +// This is the uniform data that's pushed to the compute shader.\n\ +// You don't *have* to organize your uniform data into a struct, but that's how I like to do it.\n\ +struct UniformData {\n\ + timescale: f32,\n\ + numAsteroids: f32,\n\ + deltaTime: f32,\n\ +};\n\ +\n\ +// G & M are merged into one since floating point numbers are a little weird at large sizes.\n\ +const GM = 3.986004418e+14;\n\ +\n\ +@group(1u) @binding(0u) var buf: array;\n\ +@group(2u) @binding(0u) var uniformData: UniformData;\n\ +\n\ +fn accel(pos: vec3) -> vec3{\n\ + // We don't bother calculating the force exerted upon the Earth by each asteroid since it's miniscule.\n\ + // This isn't meant to be a 100% accurate physics simulation, it's meant to run in your browser :P\n\ +\n\ + // r is the distance between the asteroid and the Earth.\n\ + // Since the earth's center of mass is at (0, 0, 0), we don't have to add anything to the asteroid's position.\n\ + let r2: f32 = pow(pos.x, 2) + pow(pos.y, 2) + pow(pos.z, 2);\n\ + let r: f32 = sqrt(r2);\n\ +\n\ + // Finally we'll calculate the force exerted upon the asteroid.\n\ + return pos * -(GM / (r2 * r));\n\ +}\n\ +\n\ +@compute @workgroup_size(64, 1, 1)\n\ +fn main(@builtin(global_invocation_id) id: vec3){\n\ + var ast: Asteroid = buf[id.x];\n\ + // We'll first calculate the updated velocity, and then the position.\n\ + ast.vel += (accel(ast.pos) * (uniformData.deltaTime)) * uniformData.timescale;\n\ + ast.pos += (ast.vel * uniformData.deltaTime) * uniformData.timescale;\n\ +\n\ + buf[id.x] = ast;\n\ +}"; diff --git a/examples/gpu/01-orbital-simulation/thumbnail.png b/examples/gpu/01-orbital-simulation/thumbnail.png new file mode 100644 index 0000000000..bfe33c1335 Binary files /dev/null and b/examples/gpu/01-orbital-simulation/thumbnail.png differ diff --git a/include/SDL3/SDL_gpu.h b/include/SDL3/SDL_gpu.h index 3b1ebd229e..fd3962a399 100644 --- a/include/SDL3/SDL_gpu.h +++ b/include/SDL3/SDL_gpu.h @@ -269,6 +269,41 @@ * - iOS/tvOS requires an A9 GPU or newer * - iOS Simulator and tvOS Simulator are unsupported * + * ### WebGPU + * + * SDL driver name: "webgpu" + * + * WebGPU support is complicated and rapidly updating, so there's no exact list. + * Generally speaking, if you're using a modern browser you can use WebGPU. + * + * The backend requires these WebGPU features to run: + * - `BGRA8UnormStorage` + * - `CoreFeaturesAndLimits` + * - `Depth32FloatStencil8` + * - `DepthClipControl` + * - `Float32Filterable` + * - `IndirectFirstInstance` + * - `RG11B10UfloatRenderable` + * + * The backend also optionally supports some features. These features are not required + * for the backend to run, but they are required for specific SDLGPU functionality: + * - `Float32Blendable` + * Required for blending Float32 textures. The backend will safely disable blending + * if this is unsupported. + * - `TextureCompressionASTC` + * - `TextureCompressionASTCSliced3D` + * Required for ASTC texture format support. + * - `TextureCompressionBC` + * - `TextureCompressionBCSliced3D` + * Required for BC texture format support + * - `TextureFormatsTier1` + * - `TextureFormatsTier2` + * Required for a lot of stuff, mostly related to texture sampling & access. It's + * too much to list here. + * + * Both all required and optional features will be requested automatically during + * device creation. If debug mode is enabled, any unsupported features will be logged. + * * ## Coordinate System * * The GPU API uses a left-handed coordinate system, following the convention @@ -371,6 +406,7 @@ * Debug Executable..., select your application, set "GPU Frame Capture" to * "Metal" in scheme "Options" window, run your app, and click the small * Metal icon on the bottom to capture a frame) + * * For WebGPU, check your browser's developer console. * * Aside from that, you may want to enable additional debug layers to receive * more detailed error messages, based on your GPU backend: @@ -383,6 +419,9 @@ * that should be installed. * * For Metal, it should be enough just to run the application from XCode to * receive detailed errors or warnings in the output. + * * For WebGPU, you usually don't need to do anything, as the browser will + * report detailed errors. However, some browsers may only provide error + * messages in beta / preview builds, so keep that in mind. * * Don't hesitate to use tools as RenderDoc when encountering runtime issues * or unexpected output on screen, quick GPU frame inspection can usually help @@ -906,6 +945,9 @@ typedef enum SDL_GPUTextureFormat * within a compute pass. Note that SIMULTANEOUS usage is only supported by a * limited number of texture formats. * + * Note that WebGPU does not distinguish between READ | WRITE and SIMULTANEOUS + * read-write. + * * \since This datatype is available since SDL 3.2.0. * * \sa SDL_CreateGPUTexture @@ -1049,6 +1091,7 @@ typedef Uint32 SDL_GPUShaderFormat; #define SDL_GPU_SHADERFORMAT_DXIL (1u << 3) /**< DXIL SM6_0 shaders for D3D12. */ #define SDL_GPU_SHADERFORMAT_MSL (1u << 4) /**< MSL shaders for Metal. */ #define SDL_GPU_SHADERFORMAT_METALLIB (1u << 5) /**< Precompiled metallib shaders for Metal. */ +#define SDL_GPU_SHADERFORMAT_WGSL (1u << 6) /**< WGSL shaders for WebGPU. */ /** * Specifies the format of a vertex attribute. @@ -2259,6 +2302,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_GPUSupportsProperties( * - "vulkan": [Vulkan](CategoryGPU#vulkan) * - "direct3d12": [D3D12](CategoryGPU#d3d12) * - "metal": [Metal](CategoryGPU#metal) + * - "webgpu": [WebGPU](CategoryGPU#webgpu) * - NULL: let SDL pick the optimal driver * * \param format_flags a bitflag indicating which shader formats the app is @@ -2411,6 +2455,7 @@ extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_CreateGPUDeviceWithProperties( #define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN "SDL.gpu.device.create.shaders.dxil" #define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN "SDL.gpu.device.create.shaders.msl" #define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN "SDL.gpu.device.create.shaders.metallib" +#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOLEAN "SDL.gpu.device.create.shaders.wgsl" #define SDL_PROP_GPU_DEVICE_CREATE_D3D12_ALLOW_FEWER_RESOURCE_SLOTS_BOOLEAN "SDL.gpu.device.create.d3d12.allowtier1resourcebinding" #define SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING "SDL.gpu.device.create.d3d12.semantic" #define SDL_PROP_GPU_DEVICE_CREATE_D3D12_AGILITY_SDK_VERSION_NUMBER "SDL.gpu.device.create.d3d12.agility_sdk_version" @@ -2490,8 +2535,8 @@ extern SDL_DECLSPEC int SDLCALL SDL_GetNumGPUDrivers(void); * checked during initialization. * * The names of drivers are all simple, low-ASCII identifiers, like "vulkan", - * "metal" or "direct3d12". These never have Unicode characters, and are not - * meant to be proper names. + * "metal", "direct3d12", or "webgpu". These never have Unicode characters, + * and are not meant to be proper names. * * \param index the index of a GPU driver. * \returns the name of the GPU driver with the given **index** or NULL when @@ -2833,6 +2878,50 @@ extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetGPUDeviceProperties(SDL_GPUD * * --- * + * **WGSL (WebGPU Shading Language)** + * + * For WGSL, use the following order: + * - 0: Sampled textures, followed by read-only storage textures, followed by + * read-only storage buffers + * - 1: Read-write storage textures, followed by read-write storage buffers + * - 2: Uniform buffers + * + * Note that WebGPU does not differentiate between simultaneous read-write and non-simultaneous + * read-write. This means that any storage bind with both READ & WRITE accesses will be treated + * as if it were simultaneous. If simultaneous RW is unsupported, the backend will fall back + * to write-only. + * + * **Example** + * + * For a compute shader binding 2 of each resource type, the binds should look + * like this: + * + * ```wgsl + * // Sampled textures come first, followed by the sampler, in SDL slot order. + * \@group(0) \@binding(0) var sampledTextureBoundToSlot0: ... + * \@group(0) \@binding(1) var samplerBoundToSlot0: sampler + * \@group(0) \@binding(2) var sampledTextureBoundToSlot1: ... + * \@group(0) \@binding(3) var samplerBoundToSlot1: sampler + * // Any read-only storage textures come next, in SDL slot order. + * \@group(0) \@binding(4) var readOnlyStorageTextureBoundToSlot0: ... + * \@group(0) \@binding(5) var readOnlyStorageTextureBoundToSlot1: ... + * // Any read-only storage buffers come next, in SDL slot order. + * \@group(0) \@binding(6) var readOnlyStorageBufferBoundToSlot0: ... + * \@group(0) \@binding(7) var readOnlyStorageBufferBoundToSlot1: ... + * // Read-write storage textures and buffers have their own bind group. + * // Read-write storage textures go first, in SDL slot order. + * \@group(1) \@binding(0) var readWriteStorageTextureBoundToSlot0: ... + * \@group(1) \@binding(1) var readWriteStorageTextureBoundToSlot1: ... + * // Any read-write storage textures come next, in SDL slot order. + * \@group(1) \@binding(2) var readWriteStorageBufferBoundToSlot0: ... + * \@group(1) \@binding(3) var readWriteStorageBufferBoundToSlot1: ... + * // Any uniform buffers are in their own set, in SDL slot order. + * \@group(2) \@binding(0) var uniformBufferBoundToSlot0: ... + * \@group(2) \@binding(1) var uniformBufferBoundToSlot1: ... + * ``` + * + * --- + * * There are optional properties that can be provided through `props`. These * are the supported properties: * @@ -3073,6 +3162,55 @@ extern SDL_DECLSPEC SDL_GPUSampler * SDLCALL SDL_CreateGPUSampler( * * --- * + * **WGSL (WebGPU Shading Language)** + * + * For vertex shaders, use: - Group 0 for samplers, storage textures, and + * storage buffers - Group 1 for uniform data + * + * For fragment shaders, use: - Group 2 for samplers, storage textures, and + * storage buffers - Group 3 for uniform data + * + * The first resource in a given set must have a `binding` of 0. Additional + * resources must appear at consecutive bindings (1, 2, etc), leaving no gaps + * in the set. + * + * All sampled textures must come first in the binding order, followed by their + * associated sampler, in order of how they are bound via `SDL_BindGPU*Samplers()`. + * + * All storage textures must come after all samplers and sampled textures + * in the binding order, in order of how they are bound via `SDL_Bind*StorageTextures()`. + * + * All storage buffers must come after all storage textures in the binding + * order, in order of how they are bound via `SDL_Bind*StorageBuffers()`. + * + * Note that WebGPU has... odd alignment rules. All types must be aligned to a power of + * two, but can be any size. + * Consult the [WebGPU spec](https://www.w3.org/TR/WGSL/#alignment-and-size) for more. + * + * **Example** + * + * If a vertex shader binds 2 samplers, 2 storage textures, 2 storage buffers, + * and 2 uniform buffers, its binding layout should look like this: + * + * ```wgsl + * // Sampled textures come first, followed by the sampler, in SDL slot order. + * \@group(0) \@binding(0) var sampledTextureBoundToSlot0: ... + * \@group(0) \@binding(1) var samplerBoundToSlot0: sampler + * \@group(0) \@binding(2) var sampledTextureBoundToSlot1: ... + * \@group(0) \@binding(3) var samplerBoundToSlot1: sampler + * // Any storage textures come next, in SDL slot order. + * \@group(0) \@binding(4) var storageTextureBoundToSlot0: ... + * \@group(0) \@binding(5) var storageTextureBoundToSlot1: ... + * // Any storage buffers come next, in SDL slot order. + * \@group(0) \@binding(6) var storageBufferBoundToSlot0: ... + * \@group(0) \@binding(7) var storageBufferBoundToSlot1: ... + * // Any uniform buffers are in their own set, in SDL slot order. + * \@group(1) \@binding(0) var uniformBufferBoundToSlot0: ... + * \@group(1) \@binding(1) var uniformBufferBoundToSlot1: ... + * ``` + * + * --- + * * Shader semantics other than system-value semantics do not matter in D3D12. * For ease of use, the SDL implementation assumes that non system-value * semantics will all be `TEXCOORD`. If you are using HLSL as the shader @@ -4217,6 +4355,10 @@ extern SDL_DECLSPEC void SDLCALL SDL_EndGPUComputePass( * SDL_UnmapGPUTransferBuffer. The memory is owned by the graphics driver - do * NOT call SDL_free() on the returned pointer. * + * Note that for the WebGPU backend, mapping download buffers is a very expensive + * procedure. Mapping download buffers with the WebGPU backend is strongly + * discouraged. + * * \param device a GPU context. * \param transfer_buffer a transfer buffer. * \param cycle if true, cycles the transfer buffer if it is already bound. @@ -4276,6 +4418,8 @@ extern SDL_DECLSPEC SDL_GPUCopyPass * SDLCALL SDL_BeginGPUCopyPass( * is properly aligned, but this adds overhead to the transfer process. Apps * can avoid this by aligning their data appropriately, or using a different * GPU backend than Direct3D 12. + * Note that the WebGPU backend inherits Direct3D's 256-byte BPR alignment + * rules, and therefore also presents the same behaviour. * * \param copy_pass a copy pass handle. * \param source the source transfer buffer with image layout information. @@ -4627,6 +4771,10 @@ extern SDL_DECLSPEC SDL_GPUTextureFormat SDLCALL SDL_GetGPUSwapchainTextureForma * freed by the user. You MUST NOT call this function from any thread other * than the one that created the window. * + * **WARNING**: WebGPU has an edge case in which mapping a transfer buffer + * destroys any currently acquired swapchain textures! Always remember to + * acquire your swapchain texture AFTER you've mapped your transfer buffers! + * * \param command_buffer a command buffer. * \param window a window that has been claimed. * \param swapchain_texture a pointer filled in with a swapchain texture @@ -4703,6 +4851,10 @@ extern SDL_DECLSPEC bool SDLCALL SDL_WaitForGPUSwapchain( * The swapchain texture is write-only and cannot be used as a sampler or for * another reading operation. * + * **WARNING**: WebGPU has an edge case in which mapping a transfer buffer + * destroys any currently acquired swapchain textures! Always remember to + * acquire your swapchain texture AFTER you've mapped your transfer buffers! + * * \param command_buffer a command buffer. * \param window a window that has been claimed. * \param swapchain_texture a pointer filled in with a swapchain texture diff --git a/include/SDL3/SDL_wgpu.h b/include/SDL3/SDL_wgpu.h new file mode 100644 index 0000000000..259354bfda --- /dev/null +++ b/include/SDL3/SDL_wgpu.h @@ -0,0 +1,61 @@ +/** + * # CategoryWGPU + * + * Experimental video support for WebGPU. + */ + +#ifndef SDL_wgpu_h_ +#define SDL_wgpu_h_ + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WEBGPU_H_ +#define WGPU_OBJECT_ATTRIBUTE(object) typedef struct object##Impl *object; + +WGPU_OBJECT_ATTRIBUTE(WGPUSurface) +WGPU_OBJECT_ATTRIBUTE(WGPUInstance) + +#undef WGPU_OBJECT_ATTRIBUTE +#endif + +/** + * Create a new WebGPU surface. + * This will not configure the surface. + * + * \param window the window which the surface will attach itself to. + * \param instance a WebGPU instance. + * \param surface a pointer to a WGPUSurface handle to output the newly + * created WebGPU surface. + * + * \returns true on success or false on failure; call SDL_GetError() for more + * information. + * + * \since This is part of SDL PR #16020, and is therefore not available in regular SDL. + * + * \sa SDL_WGPU_DestroySurface + */ +extern SDL_DECLSPEC bool SDLCALL SDL_WGPU_CreateSurface(SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); + +/** + * Destroy a WebGPU surface. + * + * \param surface the surface to destroy. + * + * \since This is part of SDL PR #16020, and is therefore not available in regular SDL. + * + * \sa SDL_WGPU_CreateSurface + */ +extern SDL_DECLSPEC void SDLCALL SDL_WGPU_DestroySurface(WGPUSurface surface); + +#ifdef __cplusplus +} +#endif +#include + +#endif // SDL_wgpu_h_ diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake index ac8050dbdd..1f1ad727c6 100644 --- a/include/build_config/SDL_build_config.h.cmake +++ b/include/build_config/SDL_build_config.h.cmake @@ -494,11 +494,18 @@ /* Enable Metal support */ #cmakedefine SDL_VIDEO_METAL 1 +/* Enable experimental WEBGPU support */ +#cmakedefine SDL_VIDEO_WEBGPU 1 +#cmakedefine WGPU_STATIC 1 +#cmakedefine WGPU_NATIVE 1 +#cmakedefine WGPU_DAWN 1 + /* Enable GPU support */ #cmakedefine SDL_GPU_D3D11 1 #cmakedefine SDL_GPU_D3D12 1 #cmakedefine SDL_GPU_VULKAN 1 #cmakedefine SDL_GPU_METAL 1 +#cmakedefine SDL_GPU_WEBGPU 1 #cmakedefine HAVE_GPU_OPENXR 1 #cmakedefine SDL_GPU_PRIVATE 1 diff --git a/src/dynapi/SDL_dynapi.c b/src/dynapi/SDL_dynapi.c index 0f9e767e6f..98b9a8607f 100644 --- a/src/dynapi/SDL_dynapi.c +++ b/src/dynapi/SDL_dynapi.c @@ -47,6 +47,7 @@ // These headers have system specific definitions, so aren't included above #include +#include #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) #ifndef WIN32_LEAN_AND_MEAN diff --git a/src/dynapi/SDL_dynapi.exports b/src/dynapi/SDL_dynapi.exports index f839f8bdf3..64205549be 100644 --- a/src/dynapi/SDL_dynapi.exports +++ b/src/dynapi/SDL_dynapi.exports @@ -1327,3 +1327,5 @@ _SDL_GetCachePath _SDL_SetRenderLogicalPresentationRect _SDL_IsGooglePlayOnPC _SDL_SetWindowMousePassthrough +_SDL_WGPU_CreateSurface +_SDL_WGPU_DestroySurface diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym index 8adc02fa4c..94868cd8c0 100644 --- a/src/dynapi/SDL_dynapi.sym +++ b/src/dynapi/SDL_dynapi.sym @@ -1328,6 +1328,8 @@ SDL3_0.0.0 { SDL_SetRenderLogicalPresentationRect; SDL_IsGooglePlayOnPC; SDL_SetWindowMousePassthrough; + SDL_WGPU_CreateSurface; + SDL_WGPU_DestroySurface; # extra symbols go here (don't modify this line) local: *; }; diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 188a5167b9..3c6103561b 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -1354,3 +1354,5 @@ #define SDL_SetRenderLogicalPresentationRect SDL_SetRenderLogicalPresentationRect_REAL #define SDL_IsGooglePlayOnPC SDL_IsGooglePlayOnPC_REAL #define SDL_SetWindowMousePassthrough SDL_SetWindowMousePassthrough_REAL +#define SDL_WGPU_CreateSurface SDL_WGPU_CreateSurface_REAL +#define SDL_WGPU_DestroySurface SDL_WGPU_DestroySurface_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 895aaf9305..618962fc93 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -1362,3 +1362,5 @@ SDL_DYNAPI_PROC(char*,SDL_GetCachePath,(const char *a,const char *b),(a,b),retur SDL_DYNAPI_PROC(bool,SDL_SetRenderLogicalPresentationRect,(SDL_Renderer *a,const SDL_FRect *b),(a,b),return) SDL_DYNAPI_PROC(bool,SDL_IsGooglePlayOnPC,(void),(),return) SDL_DYNAPI_PROC(bool,SDL_SetWindowMousePassthrough,(SDL_Window *a,bool b),(a,b),return) +SDL_DYNAPI_PROC(bool,SDL_WGPU_CreateSurface,(SDL_Window *a,WGPUInstance b,WGPUSurface *c),(a,b,c),return) +SDL_DYNAPI_PROC(void,SDL_WGPU_DestroySurface,(WGPUSurface a),(a),) diff --git a/src/gpu/SDL_gpu.c b/src/gpu/SDL_gpu.c index 62937ad778..c511e62219 100644 --- a/src/gpu/SDL_gpu.c +++ b/src/gpu/SDL_gpu.c @@ -328,6 +328,9 @@ static const SDL_GPUBootstrap *backends[] = { #endif #ifdef SDL_GPU_VULKAN &VulkanDriver, +#endif +#ifdef SDL_GPU_WEBGPU + &WebGPUDriver, #endif NULL }; @@ -672,6 +675,9 @@ static void SDL_GPU_FillProperties( if (format_flags & SDL_GPU_SHADERFORMAT_METALLIB) { SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, true); } + if (format_flags & SDL_GPU_SHADERFORMAT_WGSL) { + SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOLEAN, true); + } SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, debug_mode); SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name); } diff --git a/src/gpu/SDL_sysgpu.h b/src/gpu/SDL_sysgpu.h index 8ae50ff339..0e70123327 100644 --- a/src/gpu/SDL_sysgpu.h +++ b/src/gpu/SDL_sysgpu.h @@ -1233,6 +1233,7 @@ extern "C" { extern SDL_GPUBootstrap VulkanDriver; extern SDL_GPUBootstrap D3D12Driver; extern SDL_GPUBootstrap MetalDriver; +extern SDL_GPUBootstrap WebGPUDriver; extern SDL_GPUBootstrap PrivateGPUDriver; #ifdef __cplusplus diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c index ed51fe9be3..16a9c96768 100644 --- a/src/gpu/vulkan/SDL_gpu_vulkan.c +++ b/src/gpu/vulkan/SDL_gpu_vulkan.c @@ -151,7 +151,7 @@ static VkFormat SDLToVK_TextureFormat[] = { VK_FORMAT_R8G8B8A8_SRGB, // R8G8B8A8_UNORM_SRGB VK_FORMAT_B8G8R8A8_SRGB, // B8G8R8A8_UNORM_SRGB VK_FORMAT_BC1_RGBA_SRGB_BLOCK, // BC1_UNORM_SRGB - VK_FORMAT_BC2_SRGB_BLOCK, // BC3_UNORM_SRGB + VK_FORMAT_BC2_SRGB_BLOCK, // BC2_UNORM_SRGB VK_FORMAT_BC3_SRGB_BLOCK, // BC3_UNORM_SRGB VK_FORMAT_BC7_SRGB_BLOCK, // BC7_UNORM_SRGB VK_FORMAT_D16_UNORM, // D16_UNORM diff --git a/src/gpu/webgpu/SDL_gpu_webgpu.c b/src/gpu/webgpu/SDL_gpu_webgpu.c new file mode 100644 index 0000000000..ba1b0958b7 --- /dev/null +++ b/src/gpu/webgpu/SDL_gpu_webgpu.c @@ -0,0 +1,5968 @@ +// TODO: +// - Clean up this code base. +// - This code base is miserable to read through. I've placed functions in random places without care in the world. +// Searching for functions manually in this is a pain I wouldn't wish upon my worst enemy. +// - Documentation, documentation. +// - I have documented practically NOTHING, simply because documenting something means that I expect other people +// to use it, and I am afraid of commitment. +// - Allow pipelines to recreate themselves if bind layouts are invalid. + +// FIXME: I'm pretty sure 2D array mipmap generation is broken? +// FIXME: Uniform buffers are constant and therefore will break if the user pushes more than 1MiB of data +// to one within a single command buffer. Why would you ever do that? I have no idea. But it might happen. +// FIXME: We should seperate the swapchain fences from the submitted command buffer fences! +// FIXME: Frame pacing is screwed up on Firefox. Don't really know why, probably has to do with their stupid 100ms polling + +#include "SDL_internal.h" + +#ifdef SDL_GPU_WEBGPU + +#include "../SDL_sysgpu.h" +#include "webgpu.h" + +#include + +#define WINDOW_PROPERTY_DATA "SDL.internal.gpu.webgpu.data" +#define FORCIBLY_DESTROY_QUEUED_DESTROY_AFTER_N_FAILED 10000 +#define DEFAULT_BINDGROUP_EXPIRY 10000 +#define DEFAULT_PSEUDOMAP_SIZE_THRESHOLD (1 << 25) // 32MiB +#define DEFAULT_UNIFORM_BUFFER_SIZE (1 << 20) // 1MiB +#define WEBGPU_FEATURE_AT_END_OF_ENUM WGPUFeatureName_SubgroupSizeControl + +// previously public properties that were moved to be internal +#define SDL_PROP_GPU_DEVICE_CREATE_WEBGPU_BINDGROUP_EXPIRE_AFTER_N_SUBMITS "SDL.internal.gpu.device.create.webgpu.bindgroupexpiry" +#define SDL_PROP_GPU_DEVICE_CREATE_WEBGPU_PSEUDOMAP_BUFFERS_SMALLER_THAN "SDL.internal.gpu.device.create.webgpu.pseudomapsizethreshold" + +// I hate manual memory management so much I'm just reinventing the dynamic array but worse +#define WEBGPU_INTERNAL_InsertElementIntoArray(array, arrayCapacity, arrayElementCount, elementType, element) \ + do { \ + EXPAND_ARRAY_IF_NEEDED(array, elementType, arrayElementCount + 1, arrayCapacity, SDL_max(arrayElementCount + 1, arrayCapacity * 2)); \ + ((elementType *)array)[arrayElementCount++] = element; \ + } while (0) + +// Convert a regular char* to a WGPUStringView +#define WEBGPU_INTERNAL_CharPtrToWebGPUStringView(str) (str ? (WGPUStringView){ str, SDL_strlen(str) } : (WGPUStringView){ NULL, 0 }) + +/** + * This checks if a feature's enabled for this WebGPURenderer*. + * This also does some bounds checking, so you should use this instead of directly reading the features array. + */ +#define WEBGPU_INTERNAL_IsFeatureEnabled(renderer, feature) ((feature <= WEBGPU_FEATURE_AT_END_OF_ENUM) ? renderer->webgpuFeatureEnabled[feature] : false) + +/** + * Spin for 5000 nanoseconds. + * This allows Emscripten to catch up with browser events. + * + * Duration is fully arbitrary. + */ +#define WEBGPU_INTERNAL_Spin() SDL_DelayNS(5000) + +// WebGPU is bad and stinky +#define ALIGN_VALUE(value, alignment) (value % alignment != 0 ? value + (alignment - (value % alignment)) : value) + +#define TRACK_RESOURCE(resource, type, array, count, capacity, refcountvar) \ + for (Sint32 totallyUniqueVariableName = commandBuffer->submitted.count - 1; totallyUniqueVariableName >= 0; totallyUniqueVariableName -= 1) { \ + if (commandBuffer->submitted.array[totallyUniqueVariableName] == resource) { \ + return; \ + } \ + } \ + \ + WEBGPU_INTERNAL_InsertElementIntoArray(commandBuffer->submitted.array, \ + commandBuffer->submitted.capacity, \ + commandBuffer->submitted.count, \ + type, resource); \ + SDL_AtomicIncRef(&refcountvar) + +// Bltting shaders kindly borrowed (stolen) from klukaszek's SDLGPU WebGPU implementation. +// Thank you very much, I hate writing shaders. -- TheStickmahn +// https://github.com/klukaszek/SDL/blob/main/src/gpu/webgpu/SDL_gpu_webgpu.c + +const char *blitVert = "\n\ +struct VertexOutput {\n\ + @builtin(position) pos: vec4,\n\ + @location(0) tex: vec2\n\ +};\n\ +@vertex\n\ +fn main(@builtin(vertex_index) vertexIndex: u32) -> VertexOutput {\n\ + var output: VertexOutput;\n\ + let tex = vec2(\n\ + f32((vertexIndex << 1u) & 2u),\n\ + f32(vertexIndex & 2u)\n\ + );\n\ + output.tex = tex;\n\ + output.pos = vec4(\n\ + tex * vec2(2.0, -2.0) + vec2(-1.0, 1.0),\n\ + 0.0,\n\ + 1.0\n\ + );\n\ + return output;\n\ +}"; + +const char *blit2DShader = "\n\ +struct SourceRegionBuffer {\n\ + uvLeftTop: vec2,\n\ + uvDimensions: vec2,\n\ + mipLevel: u32,\n\ + layerOrDepth: f32\n\ +}\n\ +@group(2) @binding(0) var sourceTexture2D: texture_2d;\n\ +@group(2) @binding(1) var sourceSampler: sampler;\n\ +@group(3) @binding(0) var sourceRegion: SourceRegionBuffer;\n\ +@fragment\n\ +fn main(@location(0) tex: vec2) -> @location(0) vec4 {\n\ + let newCoord = sourceRegion.uvLeftTop + sourceRegion.uvDimensions * tex;\n\ + return textureSampleLevel(sourceTexture2D, sourceSampler, newCoord, f32(sourceRegion.mipLevel));\n\ +}"; + +const char *blit2DArrayShader = "\n\ +struct SourceRegionBuffer {\n\ + uvLeftTop: vec2,\n\ + uvDimensions: vec2,\n\ + mipLevel: u32,\n\ + layerOrDepth: f32\n\ +}\n\ +@group(2) @binding(0) var sourceTexture2DArray: texture_2d_array;\n\ +@group(2) @binding(1) var sourceSampler: sampler;\n\ +@group(3) @binding(0) var sourceRegion: SourceRegionBuffer;\n\ +@fragment\n\ +fn main(@location(0) tex: vec2) -> @location(0) vec4 {\n\ + let newCoord = vec2(\n\ + sourceRegion.uvLeftTop + sourceRegion.uvDimensions * tex\n\ + );\n\ + return textureSampleLevel(sourceTexture2DArray, sourceSampler, newCoord, u32(sourceRegion.layerOrDepth), f32(sourceRegion.mipLevel));\n\ +}"; + +const char *blit3DShader = "\n\ +struct SourceRegionBuffer {\n\ + uvLeftTop: vec2,\n\ + uvDimensions: vec2,\n\ + mipLevel: u32,\n\ + layerOrDepth: f32\n\ +}\n\ +@group(2) @binding(0) var sourceTexture3D: texture_3d;\n\ +@group(2) @binding(1) var sourceSampler: sampler;\n\ +@group(3) @binding(0) var sourceRegion: SourceRegionBuffer;\n\ +@fragment\n\ +fn main(@location(0) tex: vec2) -> @location(0) vec4 {\n\ + let newCoord = vec3(\n\ + sourceRegion.uvLeftTop + sourceRegion.uvDimensions * tex,\n\ + sourceRegion.layerOrDepth\n\ + );\n\ + return textureSampleLevel(sourceTexture3D, sourceSampler, newCoord, f32(sourceRegion.mipLevel));\n\ +}"; + +const char *blitCubeShader = "\n\ +struct SourceRegionBuffer {\n\ + uvLeftTop: vec2,\n\ + uvDimensions: vec2,\n\ + mipLevel: u32,\n\ + layerOrDepth: f32\n\ +}\n\ +@group(2) @binding(0) var sourceTextureCube: texture_cube;\n\ +@group(2) @binding(1) var sourceSampler: sampler;\n\ +@group(3) @binding(0) var sourceRegion: SourceRegionBuffer;\n\ +@fragment\n\ +fn main(@location(0) tex: vec2) -> @location(0) vec4 {\n\ + let scaledUV = sourceRegion.uvLeftTop + sourceRegion.uvDimensions * tex;\n\ + let u = 2.0 * scaledUV.x - 1.0;\n\ + let v = 2.0 * scaledUV.y - 1.0;\n\ + var newCoord: vec3;\n\ + switch(u32(sourceRegion.layerOrDepth)) {\n\ + case 0u: { newCoord = vec3(1.0, -v, -u); }\n\ + case 1u: { newCoord = vec3(-1.0, -v, u); }\n\ + case 2u: { newCoord = vec3(u, 1.0, -v); }\n\ + case 3u: { newCoord = vec3(u, -1.0, v); }\n\ + case 4u: { newCoord = vec3(u, -v, 1.0); }\n\ + case 5u: { newCoord = vec3(-u, -v, -1.0); }\n\ + default: { newCoord = vec3(0.0, 0.0, 0.0); }\n\ + }\n\ +\n\ + return textureSampleLevel(sourceTextureCube, sourceSampler, newCoord, f32(sourceRegion.mipLevel));\n\ +}"; + +const char *blitCubeArrayShader = "\n\ +struct SourceRegionBuffer {\n\ + uvLeftTop: vec2,\n\ + uvDimensions: vec2,\n\ + mipLevel: u32,\n\ + layerOrDepth: f32\n\ +}\n\ +@group(2) @binding(0) var sourceTextureCubeArray: texture_cube_array;\n\ +@group(2) @binding(1) var sourceSampler: sampler;\n\ +@group(3) @binding(0) var sourceRegion: SourceRegionBuffer;\n\ +@fragment\n\ +fn main(@location(0) tex: vec2) -> @location(0) vec4 {\n\ + let scaledUV = sourceRegion.uvLeftTop + sourceRegion.uvDimensions * tex;\n\ + let u = 2.0 * scaledUV.x - 1.0;\n\ + let v = 2.0 * scaledUV.y - 1.0;\n\ + let arrayIndex = u32(sourceRegion.layerOrDepth) / 6u;\n\ + var newCoord: vec3;\n\ + \n\ + switch(u32(sourceRegion.layerOrDepth) % 6u) {\n\ + case 0u: { newCoord = vec3(1.0, -v, -u); }\n\ + case 1u: { newCoord = vec3(-1.0, -v, u); }\n\ + case 2u: { newCoord = vec3(u, 1.0, -v); }\n\ + case 3u: { newCoord = vec3(u, -1.0, v); }\n\ + case 4u: { newCoord = vec3(u, -v, 1.0); }\n\ + case 5u: { newCoord = vec3(-u, -v, -1.0); }\n\ + default: { newCoord = vec3(0.0, 0.0, 0.0); }\n\ + }\n\ + \n\ + return textureSampleLevel(sourceTextureCubeArray, sourceSampler, newCoord, arrayIndex, f32(sourceRegion.mipLevel));\n\ +}"; + +// NOTE: All of these required features are subject to change as we test it on different browsers and operating systems. +// Also, note that while Firefox's documentation *claims* that a lot of features are unsupported, +// I've been able to use a lot of them just fine (on Linux nonetheless), so I'm not sure what's up with that. + +const WGPUFeatureName WEBGPU_INTERNAL_RequiredFeatures[7] = { + // These three all have 99.98% coverage on WebGPU devices. + WGPUFeatureName_Depth32FloatStencil8, + WGPUFeatureName_RG11B10UfloatRenderable, + WGPUFeatureName_IndirectFirstInstance, + // This one has 99.02%. + WGPUFeatureName_CoreFeaturesAndLimits, + // This one has 98%, and only because of Samsung's weird browser. + WGPUFeatureName_DepthClipControl, + // This one has 95%, blame Samsung. + WGPUFeatureName_BGRA8UnormStorage, + // This one has 90%. + WGPUFeatureName_Float32Filterable, +}; + +// These are features which we don't *need*, but they'd be really nice to have. +const WGPUFeatureName WEBGPU_INTERNAL_OptionalFeatures[7] = { + // This one has 85%. + WGPUFeatureName_Float32Blendable, + + // ASTC has 44%, while BC has 86%. + WGPUFeatureName_TextureCompressionASTC, + WGPUFeatureName_TextureCompressionASTCSliced3D, + WGPUFeatureName_TextureCompressionBC, + WGPUFeatureName_TextureCompressionBCSliced3D, + + // I have no idea what the support for these looks like. + WGPUFeatureName_TextureFormatsTier1, + WGPUFeatureName_TextureFormatsTier2, +}; + +static WGPUPresentMode SDLToWebGPU_PresentMode[] = { + [SDL_GPU_PRESENTMODE_VSYNC] = WGPUPresentMode_Fifo, + [SDL_GPU_PRESENTMODE_IMMEDIATE] = WGPUPresentMode_Immediate, + [SDL_GPU_PRESENTMODE_MAILBOX] = WGPUPresentMode_Mailbox, +}; + +static WGPUTextureFormat SDLToWebGPU_TextureFormat[] = { + [SDL_GPU_TEXTUREFORMAT_INVALID] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_A8_UNORM] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_R8_UNORM] = WGPUTextureFormat_R8Unorm, + [SDL_GPU_TEXTUREFORMAT_R8G8_UNORM] = WGPUTextureFormat_RG8Unorm, + [SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM] = WGPUTextureFormat_RGBA8Unorm, + [SDL_GPU_TEXTUREFORMAT_R16_UNORM] = WGPUTextureFormat_R16Unorm, + [SDL_GPU_TEXTUREFORMAT_R16G16_UNORM] = WGPUTextureFormat_RG16Unorm, + [SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UNORM] = WGPUTextureFormat_RGBA16Unorm, + [SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM] = WGPUTextureFormat_RGB10A2Unorm, + [SDL_GPU_TEXTUREFORMAT_B5G6R5_UNORM] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_B5G5R5A1_UNORM] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM] = WGPUTextureFormat_BGRA8Unorm, + [SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM] = WGPUTextureFormat_BC1RGBAUnorm, + [SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM] = WGPUTextureFormat_BC2RGBAUnorm, + [SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM] = WGPUTextureFormat_BC3RGBAUnorm, + [SDL_GPU_TEXTUREFORMAT_BC4_R_UNORM] = WGPUTextureFormat_BC4RUnorm, + [SDL_GPU_TEXTUREFORMAT_BC5_RG_UNORM] = WGPUTextureFormat_BC5RGUnorm, + [SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM] = WGPUTextureFormat_BC7RGBAUnorm, + [SDL_GPU_TEXTUREFORMAT_BC6H_RGB_FLOAT] = WGPUTextureFormat_BC6HRGBFloat, + [SDL_GPU_TEXTUREFORMAT_BC6H_RGB_UFLOAT] = WGPUTextureFormat_BC6HRGBUfloat, + [SDL_GPU_TEXTUREFORMAT_R8_SNORM] = WGPUTextureFormat_R8Snorm, + [SDL_GPU_TEXTUREFORMAT_R8G8_SNORM] = WGPUTextureFormat_RG8Snorm, + [SDL_GPU_TEXTUREFORMAT_R8G8B8A8_SNORM] = WGPUTextureFormat_RGBA8Snorm, + [SDL_GPU_TEXTUREFORMAT_R16_SNORM] = WGPUTextureFormat_R16Snorm, + [SDL_GPU_TEXTUREFORMAT_R16G16_SNORM] = WGPUTextureFormat_RG16Snorm, + [SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM] = WGPUTextureFormat_RGBA16Snorm, + [SDL_GPU_TEXTUREFORMAT_R16_FLOAT] = WGPUTextureFormat_R16Float, + [SDL_GPU_TEXTUREFORMAT_R16G16_FLOAT] = WGPUTextureFormat_RG16Float, + [SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT] = WGPUTextureFormat_RGBA16Float, + [SDL_GPU_TEXTUREFORMAT_R32_FLOAT] = WGPUTextureFormat_R32Float, + [SDL_GPU_TEXTUREFORMAT_R32G32_FLOAT] = WGPUTextureFormat_RG32Float, + [SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT] = WGPUTextureFormat_RGBA32Float, + [SDL_GPU_TEXTUREFORMAT_R11G11B10_UFLOAT] = WGPUTextureFormat_RG11B10Ufloat, + [SDL_GPU_TEXTUREFORMAT_R8_UINT] = WGPUTextureFormat_R8Uint, + [SDL_GPU_TEXTUREFORMAT_R8G8_UINT] = WGPUTextureFormat_RG8Uint, + [SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UINT] = WGPUTextureFormat_RGBA8Uint, + [SDL_GPU_TEXTUREFORMAT_R16_UINT] = WGPUTextureFormat_R16Uint, + [SDL_GPU_TEXTUREFORMAT_R16G16_UINT] = WGPUTextureFormat_RG16Uint, + [SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UINT] = WGPUTextureFormat_RGBA16Uint, + [SDL_GPU_TEXTUREFORMAT_R32_UINT] = WGPUTextureFormat_R32Uint, + [SDL_GPU_TEXTUREFORMAT_R32G32_UINT] = WGPUTextureFormat_RG32Uint, + [SDL_GPU_TEXTUREFORMAT_R32G32B32A32_UINT] = WGPUTextureFormat_RGBA32Uint, + [SDL_GPU_TEXTUREFORMAT_R8_INT] = WGPUTextureFormat_R8Sint, + [SDL_GPU_TEXTUREFORMAT_R8G8_INT] = WGPUTextureFormat_RG8Sint, + [SDL_GPU_TEXTUREFORMAT_R8G8B8A8_INT] = WGPUTextureFormat_RGBA8Sint, + [SDL_GPU_TEXTUREFORMAT_R16_INT] = WGPUTextureFormat_R16Sint, + [SDL_GPU_TEXTUREFORMAT_R16G16_INT] = WGPUTextureFormat_RG16Sint, + [SDL_GPU_TEXTUREFORMAT_R16G16B16A16_INT] = WGPUTextureFormat_RGBA16Sint, + [SDL_GPU_TEXTUREFORMAT_R32_INT] = WGPUTextureFormat_R32Sint, + [SDL_GPU_TEXTUREFORMAT_R32G32_INT] = WGPUTextureFormat_RG32Sint, + [SDL_GPU_TEXTUREFORMAT_R32G32B32A32_INT] = WGPUTextureFormat_RGBA32Sint, + [SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB] = WGPUTextureFormat_RGBA8UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB] = WGPUTextureFormat_BGRA8UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM_SRGB] = WGPUTextureFormat_BC1RGBAUnormSrgb, + [SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM_SRGB] = WGPUTextureFormat_BC2RGBAUnormSrgb, + [SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM_SRGB] = WGPUTextureFormat_BC3RGBAUnormSrgb, + [SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM_SRGB] = WGPUTextureFormat_BC7RGBAUnormSrgb, + [SDL_GPU_TEXTUREFORMAT_D16_UNORM] = WGPUTextureFormat_Depth16Unorm, + [SDL_GPU_TEXTUREFORMAT_D24_UNORM] = WGPUTextureFormat_Depth24Plus, + [SDL_GPU_TEXTUREFORMAT_D32_FLOAT] = WGPUTextureFormat_Depth32Float, + [SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT] = WGPUTextureFormat_Depth24PlusStencil8, + [SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT] = WGPUTextureFormat_Depth32FloatStencil8, + [SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM] = WGPUTextureFormat_ASTC4x4Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM] = WGPUTextureFormat_ASTC5x4Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM] = WGPUTextureFormat_ASTC5x5Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM] = WGPUTextureFormat_ASTC6x5Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM] = WGPUTextureFormat_ASTC6x6Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM] = WGPUTextureFormat_ASTC8x5Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM] = WGPUTextureFormat_ASTC8x6Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM] = WGPUTextureFormat_ASTC8x8Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM] = WGPUTextureFormat_ASTC10x5Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM] = WGPUTextureFormat_ASTC10x6Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM] = WGPUTextureFormat_ASTC10x8Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM] = WGPUTextureFormat_ASTC10x10Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM] = WGPUTextureFormat_ASTC12x10Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM] = WGPUTextureFormat_ASTC12x12Unorm, + [SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM_SRGB] = WGPUTextureFormat_ASTC4x4UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM_SRGB] = WGPUTextureFormat_ASTC5x4UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM_SRGB] = WGPUTextureFormat_ASTC5x5UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM_SRGB] = WGPUTextureFormat_ASTC6x5UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM_SRGB] = WGPUTextureFormat_ASTC6x6UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM_SRGB] = WGPUTextureFormat_ASTC8x5UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM_SRGB] = WGPUTextureFormat_ASTC8x6UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM_SRGB] = WGPUTextureFormat_ASTC8x8UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM_SRGB] = WGPUTextureFormat_ASTC10x5UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM_SRGB] = WGPUTextureFormat_ASTC10x6UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM_SRGB] = WGPUTextureFormat_ASTC10x8UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM_SRGB] = WGPUTextureFormat_ASTC10x10UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM_SRGB] = WGPUTextureFormat_ASTC12x10UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM_SRGB] = WGPUTextureFormat_ASTC12x12UnormSrgb, + [SDL_GPU_TEXTUREFORMAT_ASTC_4x4_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x4_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_5x5_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x5_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_6x6_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x5_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x6_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_8x8_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x5_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x6_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x8_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_10x10_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x10_FLOAT] = WGPUTextureFormat_Undefined, + [SDL_GPU_TEXTUREFORMAT_ASTC_12x12_FLOAT] = WGPUTextureFormat_Undefined, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_TextureFormat, SDL_arraysize(SDLToWebGPU_TextureFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE); + +static WGPUTextureFormat SwapchainCompositionToFormat[] = { + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR] = WGPUTextureFormat_RGBA8Unorm, + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR] = WGPUTextureFormat_RGBA8UnormSrgb, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR_EXTENDED_LINEAR] = WGPUTextureFormat_RGBA16Float, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR10_ST2084] = WGPUTextureFormat_RGB10A2Unorm +}; + +static WGPUTextureFormat SwapchainCompositionToFallbackFormat[] = { + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR] = WGPUTextureFormat_BGRA8Unorm, + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR] = WGPUTextureFormat_BGRA8UnormSrgb, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR_EXTENDED_LINEAR] = WGPUTextureFormat_RGBA16Float, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR10_ST2084] = WGPUTextureFormat_RGB10A2Unorm +}; + +static SDL_GPUTextureFormat SwapchainCompositionToSDLFormat[] = { + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR] = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR] = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR_EXTENDED_LINEAR] = SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR10_ST2084] = SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM, +}; + +static SDL_GPUTextureFormat SwapchainCompositionToFallbackSDLFormat[] = { + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR] = SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, + [SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR] = SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR_EXTENDED_LINEAR] = SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT, + [SDL_GPU_SWAPCHAINCOMPOSITION_HDR10_ST2084] = SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM, +}; + +static WGPUVertexFormat SDLToWebGPU_VertexFormat[] = { + [SDL_GPU_VERTEXELEMENTFORMAT_INVALID] = 0, + [SDL_GPU_VERTEXELEMENTFORMAT_INT] = WGPUVertexFormat_Sint32, + [SDL_GPU_VERTEXELEMENTFORMAT_INT2] = WGPUVertexFormat_Sint32x2, + [SDL_GPU_VERTEXELEMENTFORMAT_INT3] = WGPUVertexFormat_Sint32x3, + [SDL_GPU_VERTEXELEMENTFORMAT_INT4] = WGPUVertexFormat_Sint32x4, + [SDL_GPU_VERTEXELEMENTFORMAT_UINT] = WGPUVertexFormat_Uint32, + [SDL_GPU_VERTEXELEMENTFORMAT_UINT2] = WGPUVertexFormat_Uint32x2, + [SDL_GPU_VERTEXELEMENTFORMAT_UINT3] = WGPUVertexFormat_Uint32x3, + [SDL_GPU_VERTEXELEMENTFORMAT_UINT4] = WGPUVertexFormat_Uint32x4, + [SDL_GPU_VERTEXELEMENTFORMAT_FLOAT] = WGPUVertexFormat_Float32, + [SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2] = WGPUVertexFormat_Float32x2, + [SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3] = WGPUVertexFormat_Float32x3, + [SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4] = WGPUVertexFormat_Float32x4, + [SDL_GPU_VERTEXELEMENTFORMAT_BYTE2] = WGPUVertexFormat_Sint8x2, + [SDL_GPU_VERTEXELEMENTFORMAT_BYTE4] = WGPUVertexFormat_Sint8x4, + [SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2] = WGPUVertexFormat_Uint8x2, + [SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4] = WGPUVertexFormat_Uint8x4, + [SDL_GPU_VERTEXELEMENTFORMAT_BYTE2_NORM] = WGPUVertexFormat_Snorm8x2, + [SDL_GPU_VERTEXELEMENTFORMAT_BYTE4_NORM] = WGPUVertexFormat_Snorm8x4, + [SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2_NORM] = WGPUVertexFormat_Unorm8x2, + [SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM] = WGPUVertexFormat_Unorm8x4, + [SDL_GPU_VERTEXELEMENTFORMAT_SHORT2] = WGPUVertexFormat_Sint16x2, + [SDL_GPU_VERTEXELEMENTFORMAT_SHORT4] = WGPUVertexFormat_Sint16x4, + [SDL_GPU_VERTEXELEMENTFORMAT_USHORT2] = WGPUVertexFormat_Uint16x2, + [SDL_GPU_VERTEXELEMENTFORMAT_USHORT4] = WGPUVertexFormat_Uint16x4, + [SDL_GPU_VERTEXELEMENTFORMAT_SHORT2_NORM] = WGPUVertexFormat_Snorm16x2, + [SDL_GPU_VERTEXELEMENTFORMAT_SHORT4_NORM] = WGPUVertexFormat_Snorm16x4, + [SDL_GPU_VERTEXELEMENTFORMAT_USHORT2_NORM] = WGPUVertexFormat_Unorm16x2, + [SDL_GPU_VERTEXELEMENTFORMAT_USHORT4_NORM] = WGPUVertexFormat_Unorm16x4, + [SDL_GPU_VERTEXELEMENTFORMAT_HALF2] = WGPUVertexFormat_Float16x2, + [SDL_GPU_VERTEXELEMENTFORMAT_HALF4] = WGPUVertexFormat_Float16x4, + [SDL_GPU_VERTEXELEMENTFORMAT_R10G10B10A2_UNORM] = WGPUVertexFormat_Unorm10_10_10_2, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_VertexFormat, SDL_arraysize(SDLToWebGPU_VertexFormat) == SDL_GPU_VERTEXELEMENTFORMAT_MAX_ENUM_VALUE); + +static WGPUIndexFormat SDLToWebGPU_IndexFormat[] = { + [SDL_GPU_INDEXELEMENTSIZE_16BIT] = WGPUIndexFormat_Uint16, + [SDL_GPU_INDEXELEMENTSIZE_32BIT] = WGPUIndexFormat_Uint32, +}; + +static WGPUVertexStepMode SDLToWebGPU_VertexInputRate[] = { + [SDL_GPU_VERTEXINPUTRATE_VERTEX] = WGPUVertexStepMode_Vertex, + [SDL_GPU_VERTEXINPUTRATE_INSTANCE] = WGPUVertexStepMode_Instance, +}; + +static WGPUPrimitiveTopology SDLToWebGPU_PrimitiveType[] = { + [SDL_GPU_PRIMITIVETYPE_TRIANGLELIST] = WGPUPrimitiveTopology_TriangleList, + [SDL_GPU_PRIMITIVETYPE_TRIANGLESTRIP] = WGPUPrimitiveTopology_TriangleStrip, + [SDL_GPU_PRIMITIVETYPE_LINELIST] = WGPUPrimitiveTopology_LineList, + [SDL_GPU_PRIMITIVETYPE_LINESTRIP] = WGPUPrimitiveTopology_LineStrip, + [SDL_GPU_PRIMITIVETYPE_POINTLIST] = WGPUPrimitiveTopology_PointList, +}; + +static WGPUCullMode SDLToWebGPU_CullMode[] = { + [SDL_GPU_CULLMODE_NONE] = WGPUCullMode_None, + [SDL_GPU_CULLMODE_FRONT] = WGPUCullMode_Front, + [SDL_GPU_CULLMODE_BACK] = WGPUCullMode_Back, +}; + +static WGPUFrontFace SDLToWebGPU_FrontFace[] = { + [SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE] = WGPUFrontFace_CCW, + [SDL_GPU_FRONTFACE_CLOCKWISE] = WGPUFrontFace_CW +}; + +static WGPUBlendFactor SDLToWebGPU_BlendFactor[] = { + [SDL_GPU_BLENDFACTOR_INVALID] = WGPUBlendFactor_Undefined, + [SDL_GPU_BLENDFACTOR_ZERO] = WGPUBlendFactor_Zero, + [SDL_GPU_BLENDFACTOR_ONE] = WGPUBlendFactor_One, + [SDL_GPU_BLENDFACTOR_SRC_COLOR] = WGPUBlendFactor_Src, + [SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_COLOR] = WGPUBlendFactor_OneMinusSrc, + [SDL_GPU_BLENDFACTOR_DST_COLOR] = WGPUBlendFactor_Dst, + [SDL_GPU_BLENDFACTOR_ONE_MINUS_DST_COLOR] = WGPUBlendFactor_OneMinusDst, + [SDL_GPU_BLENDFACTOR_SRC_ALPHA] = WGPUBlendFactor_SrcAlpha, + [SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA] = WGPUBlendFactor_OneMinusSrcAlpha, + [SDL_GPU_BLENDFACTOR_DST_ALPHA] = WGPUBlendFactor_DstAlpha, + [SDL_GPU_BLENDFACTOR_ONE_MINUS_DST_ALPHA] = WGPUBlendFactor_OneMinusDstAlpha, + [SDL_GPU_BLENDFACTOR_CONSTANT_COLOR] = WGPUBlendFactor_Constant, + [SDL_GPU_BLENDFACTOR_ONE_MINUS_CONSTANT_COLOR] = WGPUBlendFactor_OneMinusConstant, + [SDL_GPU_BLENDFACTOR_SRC_ALPHA_SATURATE] = WGPUBlendFactor_SrcAlphaSaturated, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_BlendFactor, SDL_arraysize(SDLToWebGPU_BlendFactor) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE); + +static WGPUBlendOperation SDLToWebGPU_BlendOp[] = { + [SDL_GPU_BLENDOP_INVALID] = WGPUBlendOperation_Undefined, + [SDL_GPU_BLENDOP_ADD] = WGPUBlendOperation_Add, + [SDL_GPU_BLENDOP_SUBTRACT] = WGPUBlendOperation_Subtract, + [SDL_GPU_BLENDOP_REVERSE_SUBTRACT] = WGPUBlendOperation_ReverseSubtract, + [SDL_GPU_BLENDOP_MIN] = WGPUBlendOperation_Min, + [SDL_GPU_BLENDOP_MAX] = WGPUBlendOperation_Max, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_BlendOp, SDL_arraysize(SDLToWebGPU_BlendOp) == SDL_GPU_BLENDOP_MAX_ENUM_VALUE); + +static WGPUCompareFunction SDLToWebGPU_CompareFunc[] = { + [SDL_GPU_COMPAREOP_INVALID] = WGPUCompareFunction_Undefined, + [SDL_GPU_COMPAREOP_NEVER] = WGPUCompareFunction_Never, + [SDL_GPU_COMPAREOP_LESS] = WGPUCompareFunction_Less, + [SDL_GPU_COMPAREOP_EQUAL] = WGPUCompareFunction_Equal, + [SDL_GPU_COMPAREOP_LESS_OR_EQUAL] = WGPUCompareFunction_LessEqual, + [SDL_GPU_COMPAREOP_GREATER] = WGPUCompareFunction_Greater, + [SDL_GPU_COMPAREOP_NOT_EQUAL] = WGPUCompareFunction_NotEqual, + [SDL_GPU_COMPAREOP_GREATER_OR_EQUAL] = WGPUCompareFunction_GreaterEqual, + [SDL_GPU_COMPAREOP_ALWAYS] = WGPUCompareFunction_Always, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_CompareFunc, SDL_arraysize(SDLToWebGPU_CompareFunc) == SDL_GPU_COMPAREOP_MAX_ENUM_VALUE); + +static WGPUStencilOperation SDLToWebGPU_StencilOp[] = { + [SDL_GPU_STENCILOP_INVALID] = WGPUStencilOperation_Undefined, // INVALID + [SDL_GPU_STENCILOP_KEEP] = WGPUStencilOperation_Keep, + [SDL_GPU_STENCILOP_ZERO] = WGPUStencilOperation_Zero, + [SDL_GPU_STENCILOP_REPLACE] = WGPUStencilOperation_Replace, + [SDL_GPU_STENCILOP_INCREMENT_AND_CLAMP] = WGPUStencilOperation_IncrementClamp, + [SDL_GPU_STENCILOP_DECREMENT_AND_CLAMP] = WGPUStencilOperation_DecrementClamp, + [SDL_GPU_STENCILOP_INVERT] = WGPUStencilOperation_Invert, + [SDL_GPU_STENCILOP_INCREMENT_AND_WRAP] = WGPUStencilOperation_IncrementWrap, + [SDL_GPU_STENCILOP_DECREMENT_AND_WRAP] = WGPUStencilOperation_DecrementWrap, +}; +SDL_COMPILE_TIME_ASSERT(SDLToWebGPU_StencilOp, SDL_arraysize(SDLToWebGPU_StencilOp) == SDL_GPU_STENCILOP_MAX_ENUM_VALUE); + +static WGPULoadOp SDLToWebGPU_LoadOp[] = { + [SDL_GPU_LOADOP_LOAD] = WGPULoadOp_Load, + [SDL_GPU_LOADOP_CLEAR] = WGPULoadOp_Clear, + [SDL_GPU_LOADOP_DONT_CARE] = WGPULoadOp_Clear, +}; + +static WGPUStoreOp SDLToWebGPU_StoreOp[] = { + [SDL_GPU_STOREOP_STORE] = WGPUStoreOp_Store, + [SDL_GPU_STOREOP_DONT_CARE] = WGPUStoreOp_Discard, + [SDL_GPU_STOREOP_RESOLVE] = WGPUStoreOp_Discard, + [SDL_GPU_STOREOP_RESOLVE_AND_STORE] = WGPUStoreOp_Store, +}; + +static WGPUFilterMode SDLToWebGPU_FilterMode[] = { + [SDL_GPU_FILTER_NEAREST] = WGPUFilterMode_Nearest, + [SDL_GPU_FILTER_LINEAR] = WGPUFilterMode_Linear +}; + +static WGPUMipmapFilterMode SDLToWebGPU_MipmapFilterMode[] = { + [SDL_GPU_SAMPLERMIPMAPMODE_NEAREST] = WGPUMipmapFilterMode_Nearest, + [SDL_GPU_SAMPLERMIPMAPMODE_LINEAR] = WGPUMipmapFilterMode_Linear, +}; + +static WGPUAddressMode SDLToWebGPU_AddressMode[] = { + [SDL_GPU_SAMPLERADDRESSMODE_REPEAT] = WGPUAddressMode_Repeat, + [SDL_GPU_SAMPLERADDRESSMODE_MIRRORED_REPEAT] = WGPUAddressMode_MirrorRepeat, + [SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE] = WGPUAddressMode_ClampToEdge, +}; + +static WGPUTextureViewDimension SDLToWebGPU_TextureTypeToViewDimension[] = { + [SDL_GPU_TEXTURETYPE_2D] = WGPUTextureViewDimension_2D, + [SDL_GPU_TEXTURETYPE_2D_ARRAY] = WGPUTextureViewDimension_2DArray, + [SDL_GPU_TEXTURETYPE_3D] = WGPUTextureViewDimension_3D, + [SDL_GPU_TEXTURETYPE_CUBE] = WGPUTextureViewDimension_Cube, + [SDL_GPU_TEXTURETYPE_CUBE_ARRAY] = WGPUTextureViewDimension_CubeArray, +}; + +static const char *WEBGPU_FeatureNameToString(WGPUFeatureName name) +{ + switch (name) { + case WGPUFeatureName_CoreFeaturesAndLimits: + return "WGPUFeatureName_CoreFeaturesAndLimits"; + case WGPUFeatureName_DepthClipControl: + return "WGPUFeatureName_DepthClipControl"; + case WGPUFeatureName_Depth32FloatStencil8: + return "WGPUFeatureName_Depth32FloatStencil8"; + case WGPUFeatureName_TextureCompressionBC: + return "WGPUFeatureName_TextureCompressionBC"; + case WGPUFeatureName_TextureCompressionBCSliced3D: + return "WGPUFeatureName_TextureCompressionBCSliced3D"; + case WGPUFeatureName_TextureCompressionETC2: + return "WGPUFeatureName_TextureCompressionETC2"; + case WGPUFeatureName_TextureCompressionASTC: + return "WGPUFeatureName_TextureCompressionASTC"; + case WGPUFeatureName_TextureCompressionASTCSliced3D: + return "WGPUFeatureName_TextureCompressionASTCSliced3D"; + case WGPUFeatureName_TimestampQuery: + return "WGPUFeatureName_TimestampQuery"; + case WGPUFeatureName_IndirectFirstInstance: + return "WGPUFeatureName_IndirectFirstInstance"; + case WGPUFeatureName_ShaderF16: + return "WGPUFeatureName_ShaderF16"; + case WGPUFeatureName_RG11B10UfloatRenderable: + return "WGPUFeatureName_RG11B10UfloatRenderable"; + case WGPUFeatureName_BGRA8UnormStorage: + return "WGPUFeatureName_BGRA8UnormStorage"; + case WGPUFeatureName_Float32Filterable: + return "WGPUFeatureName_Float32Filterable"; + case WGPUFeatureName_Float32Blendable: + return "WGPUFeatureName_Float32Blendable"; + case WGPUFeatureName_ClipDistances: + return "WGPUFeatureName_ClipDistances"; + case WGPUFeatureName_DualSourceBlending: + return "WGPUFeatureName_DualSourceBlending"; + case WGPUFeatureName_Subgroups: + return "WGPUFeatureName_Subgroups"; + case WGPUFeatureName_TextureFormatsTier1: + return "WGPUFeatureName_TextureFormatsTier1"; + case WGPUFeatureName_TextureFormatsTier2: + return "WGPUFeatureName_TextureFormatsTier2"; + case WGPUFeatureName_PrimitiveIndex: + return "WGPUFeatureName_PrimitiveIndex"; + case WGPUFeatureName_TextureComponentSwizzle: + return "WGPUFeatureName_TextureComponentSwizzle"; + case WGPUFeatureName_SubgroupSizeControl: + return "WGPUFeatureName_SubgroupSizeControl"; + case WGPUFeatureName_Force32: + return "WGPUFeatureName_Force32"; + default: + SDL_assert(!"Unsupported WGPUFeatureName"); + return "Unknown WGPUFeatureName"; + } +} + +static WGPUTextureSampleType WebGPUTextureFormatToSampleType(WGPUTextureFormat format) +{ + switch (format) { + case WGPUTextureFormat_R8Uint: + case WGPUTextureFormat_RG8Uint: + case WGPUTextureFormat_RGBA8Uint: + case WGPUTextureFormat_R16Uint: + case WGPUTextureFormat_RG16Uint: + case WGPUTextureFormat_RGBA16Uint: + case WGPUTextureFormat_R32Uint: + case WGPUTextureFormat_RG32Uint: + case WGPUTextureFormat_RGBA32Uint: + return WGPUTextureSampleType_Uint; + + case WGPUTextureFormat_R8Sint: + case WGPUTextureFormat_RG8Sint: + case WGPUTextureFormat_RGBA8Sint: + case WGPUTextureFormat_R16Sint: + case WGPUTextureFormat_RG16Sint: + case WGPUTextureFormat_RGBA16Sint: + case WGPUTextureFormat_R32Sint: + case WGPUTextureFormat_RG32Sint: + case WGPUTextureFormat_RGBA32Sint: + return WGPUTextureSampleType_Sint; + + // While there *is* a depth sampling type, we don't use it at all since it kinda sucks. + // Unfilterable float's just better 99% of the time. + case WGPUTextureFormat_Depth16Unorm: + case WGPUTextureFormat_Depth24Plus: + case WGPUTextureFormat_Depth32Float: + // Note that since we moved Float32Filterable to the needed features array, we'll always assume that float32 is filterable. + return WGPUTextureSampleType_UnfilterableFloat; + default: + return WGPUTextureSampleType_Float; + } +} + +typedef struct WebGPUTexture WebGPUTexture; +typedef struct WebGPUTextureContainer WebGPUTextureContainer; +typedef struct WebGPUBuffer WebGPUBuffer; +typedef struct WebGPUBufferContainer WebGPUBufferContainer; +typedef struct WebGPUWindowData WebGPUWindowData; +typedef struct WebGPUFence WebGPUFence; +typedef struct WebGPUSubmittedCommandBuffer WebGPUSubmittedCommandBuffer; +typedef struct WebGPUQueuedDestroy WebGPUQueuedDestroy; + +typedef struct WebGPURenderer +{ + WGPUInstance instance; + WGPUAdapter adapter; + WGPUDevice device; + WGPUQueue queue; + + // 0-3: Vertex + // 4-7: Fragment + // 8-11: Compute + WebGPUBufferContainer *uniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE * 3]; + // Pre-made bind groups for the uniforms. + // 0: Vertex, 1: Fragment, 2: Compute + WGPUBindGroup uniformBufferBindGroups[3]; + // Pre-made bind group layouts for the uniforms. + // 0: Vertex, 1: Fragment, 2: Compute + WGPUBindGroupLayout uniformBufferBindGroupLayouts[3]; + + WebGPUSubmittedCommandBuffer **submittedCommandBuffers; + Uint32 submittedCommandBufferCapacity; + Uint32 submittedCommandBufferCount; + + WebGPUQueuedDestroy **queuedDestroys; + Uint32 queuedDestroyCount; + Uint32 queuedDestroyCapacity; + + BlitPipelineCacheEntry *blitPipelines; + + struct blitResources + { + SDL_GPUShader *blitVertexShader; + SDL_GPUShader *blit2DShader; + SDL_GPUShader *blit2DArrayShader; + SDL_GPUShader *blit3DShader; + SDL_GPUShader *blitCubeShader; + SDL_GPUShader *blitCubeArrayShader; + + SDL_GPUSampler *blitNearestSampler; + SDL_GPUSampler *blitLinearSampler; + } blitResources; + + SDL_PropertiesID props; + SDL_HashTable *bindGroupHashTable; // A cache of the renderer's bind groups. + + SDL_Mutex *destroyingSelfLock; + SDL_Mutex *submittingCommandBufferLock; + + Uint32 maxFramesInFlight; + Uint32 blitPipelineCount; + Uint32 blitPipelineCapacity; + + SDL_AtomicInt numSubmissions; + SDL_AtomicInt nextBindableResourceID; + + /** + * How big can a buffer get until pseudo-mapping is disallowed due to memory usage concerns? + */ + Uint64 pseudoMapBufferSizeThreshold; + + // For how many submissions can a bind group be unused until it's automatically freed? + // Set to -1 to disable pruning, and 0 to instantly free it. + int bindGroupsExpireAfter; + + // This is an array of booleans which state whether or not a feature is enabled for this WebGPU device. + // Do not access this directly! Use WEBGPU_INTERNAL_IsFeatureEnabled, which does bounds checking. + bool webgpuFeatureEnabled[WEBGPU_FEATURE_AT_END_OF_ENUM]; + + bool debugMode; + bool destroyingSelf; + bool preferLowPower; +} WebGPURenderer; + +// TODO: Ooooh, I hate the windowing stuff in this backend. +struct WebGPUWindowData +{ + SDL_Window *window; + WGPUSurface surface; + + SDL_GPUSwapchainComposition swapchainComposition; + SDL_GPUPresentMode presentMode; + + // The currently configured surface width + Uint32 surfaceWidth; + // The currently configured surface height + Uint32 surfaceHeight; + + bool surfaceDirty; + bool shouldUseFallbackFormat; +}; + +typedef enum WebGPUBindGroupType +{ + // Vertex stage, sampler, sampled textures, and storage textures / buffers + // Corresponds to group 0 in WGSL. + WEBGPU_BINDGROUP_VERTEXSAMPLERSTORAGE, + // Vertex stage, uniform buffers. + // Corresponds to group 1 in WGSL. + WEBGPU_BINDGROUP_VERTEXUNIFORMS, + // Fragment stage, samplers & storage textures / buffers + // Corresponds to group 2 in WGSL. + WEBGPU_BINDGROUP_FRAGMENTSAMPLERSTORAGE, + // Fragment stage, uniform buffers. + // Corresponds to group 3 in WGSL. + WEBGPU_BINDGROUP_FRAGMENTUNIFORMS, + // Compute stage, samplers, sampled textures, and readonly storage textures / buffers + // Corresponds to group 0 in WGSL. + WEBGPU_BINDGROUP_COMPUTESAMPLERSTORAGE, + // Compute stage, readwrite storage textures and buffers. + // Corresponds to group 1 in WGSL. + WEBGPU_BINDGROUP_COMPUTEREADWRITESTORAGE, + // Compute stage, uniform buffers. + // Corresponds to group 2 in WGSL. + WEBGPU_BINDGROUP_COMPUTEUNIFORMS, +} WebGPUBindGroupType; + +typedef enum WebGPUQueuedDestroyType +{ + WEBGPU_QUEUED_DESTROY_INVALID, + WEBGPU_QUEUED_DESTROY_TEXTURE_CONTAINER, + WEBGPU_QUEUED_DESTROY_TEXTURE, + WEBGPU_QUEUED_DESTROY_SAMPLER, + WEBGPU_QUEUED_DESTROY_BUFFER_CONTAINER, + WEBGPU_QUEUED_DESTROY_BUFFER, + WEBGPU_QUEUED_DESTROY_FENCE, + WEBGPU_QUEUED_DESTROY_SUBMITTED_COMMAND_BUFFER, + WEBGPU_QUEUED_DESTROY_BIND_GROUP, +} WebGPUQueuedDestroyType; + +typedef struct WebGPUBindGroupCacheKey +{ + WGPUShaderStage stage; + + Uint64 boundSamplersIDs[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + Uint64 boundTexturesIDs[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + + // Storage textures/buffers in a render pass are always read-only, so they don't need their own fields. + Uint64 boundReadWriteStorageTexturesIDs[MAX_STORAGE_TEXTURES_PER_STAGE]; + Uint64 boundReadOnlyStorageTexturesIDs[MAX_STORAGE_TEXTURES_PER_STAGE]; + + Uint64 boundReadWriteStorageBuffersIDs[MAX_STORAGE_BUFFERS_PER_STAGE]; + Uint64 boundReadOnlyStorageBuffersIDs[MAX_STORAGE_BUFFERS_PER_STAGE]; +} WebGPUBindGroupCacheKey; + +typedef enum WebGPUBindGroupEntryType +{ + WEBGPU_BIND_GROUP_ENTRY_TYPE_UNKNOWN, + WEBGPU_BIND_GROUP_ENTRY_TYPE_SAMPLER, + WEBGPU_BIND_GROUP_ENTRY_TYPE_TEXTURE, + WEBGPU_BIND_GROUP_ENTRY_TYPE_STORAGE_BUFFER, + WEBGPU_BIND_GROUP_ENTRY_TYPE_STORAGE_TEXTURE, +} WebGPUBindGroupEntryType; + +/** + * This is pretty much an extended version of WebGPUBindGroupEntryType which includes + * extra values for different variations of the same type. + */ +typedef enum WebGPUBindGroupLayoutEntryType +{ + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_UNKNOWN, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER_NONFILTERING, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE_UNFILTERABLE, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER_READWRITE, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE, + WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE_READWRITE, +} WebGPUBindGroupLayoutEntryType; + +typedef struct WebGPUInferredBindGroupLayoutEntry +{ + union + { + struct sampler + { + WGPUSamplerBindingType bindType; + } sampler; + + struct texture + { + WGPUTextureFormat format; + WGPUTextureViewDimension dimension; + + bool isDepth; // Is the texture a depth type? (E.g: texture_depth_2d) + bool isMultisampled; // Is the texture a multisampled type? (E.g: texture_multisampled_2d) + bool isForciblyUnfilterable; // Is this texture unfilterable? + } texture; + + struct storageTexture + { + WGPUTextureFormat format; + WGPUTextureViewDimension dimension; + WGPUStorageTextureAccess access; + } storageTexture; + + struct storageBuffer + { + bool canRead; + bool canWrite; + } storageBuffer; + }; + + WebGPUBindGroupEntryType type; + + Uint32 group; // The group this binding belongs to. + Uint32 binding; // The binding slot this binding.... binds to. (Take a shot each time I say "bind", and you'll be dead in a minute.) +} WebGPUInferredBindGroupLayoutEntry; + +typedef struct WebGPUBindGroup +{ + WGPUBindGroup bindGroup; + + WebGPUBindGroupCacheKey key; + + // The renderer's numSubmissions when this bind groups was last bound. + // This is used to automatically send old, redunant bind groups to the shadow realm. + SDL_AtomicInt lastUsedAtSubmission; + + // if this is true, nuke this bind group from orbit ASAP + bool invalid; +} WebGPUBindGroup; + +typedef enum WebGPUBufferType +{ + WEBGPU_BUFFER_TYPE_GPU, + WEBGPU_BUFFER_TYPE_UNIFORM, + WEBGPU_BUFFER_TYPE_TRANSFER_UPLOAD, + WEBGPU_BUFFER_TYPE_TRANSFER_DOWNLOAD, + // A transfer buffer which can only be accessed on the GPU. + // This is used for texture copies where the user has not properly padded the data. + // The GPUOnly transfer buffer acts as an intermediate. + WEBGPU_BUFFER_TYPE_TRANSFER_GPUONLY, +} WebGPUBufferType; + +struct WebGPUBuffer +{ + WebGPUBufferContainer *container; + WGPUBuffer buffer; + + WebGPUBufferType type; + SDL_GPUBufferUsageFlags usage; + + Uint64 size; + + // The resource's identifier. (Excellent documentation by yours truly, give me that pulitzer now) + Uint64 identifier; + SDL_AtomicInt referenceCount; +}; + +typedef enum WebGPUBufferMapState +{ + // Buffer is unmapped. + MAP_STATE_UNMAPPED, + // Buffer is mapped from GPU. + MAP_STATE_MAPPED_GPU, + // Buffer is pseudo-mapped. + MAP_STATE_MAPPED_CPU, +} WebGPUBufferMapState; + +struct WebGPUBufferContainer +{ + WebGPUBuffer *activeBuffer; + + WebGPUBuffer **buffers; + Uint32 bufferCapacity; + Uint32 bufferCount; + + SDL_GPUBufferUsageFlags usageFlags; + WebGPUBufferType bufferType; + + // Actually mapping stuff in WebGPU's really slow. So, we don't. Instead we allocate some memory, + // say that's the mapped buffer, and then when you use it to copy something we call wgpuQueueWriteBuffer. + // Now, this is only a thing for upload buffers. We obviously can't do this trick with download buffers. + // However, this does increase performance SIGNIFIGANTLY if you're uploading data regularly. + void *pseudoMappedRange; + + Uint32 size; + + // The map state of this buffer. + // 0 = Not mapped. + // 1 = Mapped from GPU (Accessing GPU memory directly) + // 2 = Mapped on CPU (Pseudo-mapping, see above) + Uint16 mapState; + + /** + * Should this buffer be pseudo-mapped? + */ + bool shouldPseudomap; +}; + +typedef struct WebGPUTextureView +{ + WGPUTextureView view; + WebGPUTexture *parent; + + Uint64 identifier; +} WebGPUTextureView; + +struct WebGPUTexture +{ + WebGPUTextureContainer *container; + + WGPUTexture texture; + WGPUTextureAspect aspect; + WebGPUTextureView *fullTextureView; + WebGPUTextureView **textureViews; + + SDL_GPUTextureType type; + SDL_GPUTextureFormat format; + SDL_GPUTextureUsageFlags usage; + SDL_GPUSampleCount sampleCount; + SDL_AtomicInt referenceCount; + + Uint32 textureViewCount; + Uint32 textureViewCapacity; +}; + +struct WebGPUTextureContainer +{ + TextureCommonHeader header; + + WebGPUTexture *activeTexture; + + Uint32 textureCapacity; + Uint32 textureCount; + WebGPUTexture **textures; +}; + +typedef struct WebGPUSampler +{ + WGPUSampler sampler; + + // This is a fallback sampler for non-filterable texture formats. + // This sampler always defaults to NEAREST, and does not support anisotropy. + WGPUSampler fallbackSampler; + Uint64 identifier; +} WebGPUSampler; + +typedef struct WebGPUFence +{ + WGPUFutureWaitInfo future; + bool signaled; +} WebGPUFence; + +typedef struct WebGPUQueuedResourceBindSampler +{ + WGPUShaderStage visibleTo; + WebGPUSampler *sampler; + WebGPUTextureView *textureView; + + Uint64 samplerIdentifier; + Uint64 textureIdentifier; + + Uint16 slot; +} WebGPUQueuedResourceBindSampler; + +typedef struct WebGPUQueuedResourceBindStorageTexture +{ + WGPUShaderStage visibleTo; + WebGPUTextureView *storageTextureView; + + Uint64 identifier; + Uint16 slot; + // if false, storage texture is read-only + bool canWrite; +} WebGPUQueuedResourceBindStorageTexture; + +typedef struct WebGPUQueuedResourceBindStorageBuffer +{ + WGPUShaderStage visibleTo; + WebGPUBuffer *storageBuffer; + + Uint64 identifier; + Uint16 slot; + // if false, storage buffer is read-only + bool canWrite; +} WebGPUQueuedResourceBindStorageBuffer; + +typedef struct WebGPUQueuedResourceBindUniformBuffer +{ + Uint32 length; + // Stage-relative + // (they're cousins) + Uint32 slot; +} WebGPUQueuedResourceBindUniformBuffer; + +typedef struct WebGPUQueuedResourceBindVertexBuffer +{ + WebGPUBuffer *buffer; + Uint32 offset; + + const char *identifier; + Uint16 slot; +} WebGPUQueuedResourceBindVertexBuffer; + +typedef struct WebGPUQueuedResourceBindIndexBuffer +{ + WebGPUBuffer *buffer; + + const char *identifier; + Uint32 offset; +} WebGPUQueuedResourceBindIndexBuffer; + +typedef struct WebGPUQueuedUniformDataUpload +{ + void *data; + + Uint64 offset; + Uint32 length; + // This doesn't care about stage visibility. It's the raw index of the uniform buffer it'll write to. + Uint32 slot; +} WebGPUQueuedUniformDataUpload; + +typedef struct WebGPUQueuedDestroy +{ + WebGPUQueuedDestroyType type; + union + { + WebGPUTextureContainer *textureContainer; + WebGPUTexture *texture; + WebGPUSampler *sampler; + + WebGPUBufferContainer *bufferContainer; + WebGPUBuffer *buffer; + + WebGPUFence *fence; + WebGPUSubmittedCommandBuffer *submittedCommandBuffer; + WebGPUBindGroup *bindGroup; + } resource; + + // Just used for debugging. If this is above 100 it'll log to the console that this resource is very stubborn. + Uint32 destroysAttempted; +} WebGPUQueuedDestroy; + +typedef struct WebGPUShaderBindGroupLayouts +{ + WGPUBindGroupLayout samplerStorageBindGroupLayout; + WGPUBindGroupLayout uniformBindGroupLayout; +} WebGPUShaderBindGroupLayouts; + +typedef struct WebGPUComputeShaderBindGroupLayouts +{ + WGPUBindGroupLayout samplerStorageBindGroupLayout; + WGPUBindGroupLayout readWriteStorageBindGroupLayout; + WGPUBindGroupLayout uniformBindGroupLayout; +} WebGPUComputeShaderBindGroupLayouts; + +typedef struct WebGPUShader +{ + WGPUShaderModule shader; + + char *entrypoint; + SDL_GPUShaderStage stage; + + SDL_AtomicInt referenceCount; + bool shouldFree; +} WebGPUShader; + +typedef struct WebGPUGraphicsPipeline +{ + // FIXME: This is never filled in. + GraphicsPipelineCommonHeader header; + + WGPURenderPipeline pipeline; + WebGPUShaderBindGroupLayouts vertexBindGroupLayouts; + WebGPUShaderBindGroupLayouts fragmentBindGroupLayouts; + + SDL_GPUGraphicsPipelineCreateInfo createInfo; + + // FIXME: This is never even used. It was intended to be used for the edge-case where pipelines + // have been given entirely different binds than it was created with but I never bothered adding this + // We'll do it later™ + Uint32 currentPipelineHash; + bool hasBeenCreated; +} WebGPUGraphicsPipeline; + +typedef struct WebGPUComputePipeline +{ + // FIXME: This is also never filled in. + ComputePipelineCommonHeader header; + + WGPUComputePipeline pipeline; + WebGPUComputeShaderBindGroupLayouts bindGroupLayouts; + + SDL_GPUComputePipelineCreateInfo createInfo; + + // FIXME: Same thing here, this is never used. + Uint32 currentPipelineHash; + bool hasBeenCreated; +} WebGPUComputePipeline; + +typedef struct WebGPUOffsetBufferBinding +{ + WebGPUBuffer *buffer; + Uint32 offset; +} WebGPUOffsetBufferBinding; + +struct WebGPUSubmittedCommandBuffer +{ + WebGPUFence *fence; + + WebGPUBuffer **usedBuffers; + Uint32 usedBufferCount; + Uint32 usedBufferCapacity; + + WebGPUTexture **usedTextures; + Uint32 usedTextureCount; + Uint32 usedTextureCapacity; +}; + +typedef struct WebGPUCommandBuffer +{ + CommandBufferCommonHeader common; + + WebGPURenderer *renderer; + + WGPUCommandEncoder encoder; + WGPUQueue queue; + WGPUDevice device; + WGPUInstance instance; + + WGPURenderPassEncoder renderPassEncoder; + WGPUComputePassEncoder computePassEncoder; + + WebGPUGraphicsPipeline *boundGraphicsPipeline; + WebGPUComputePipeline *boundComputePipeline; + + WebGPUSubmittedCommandBuffer submitted; + + // These'll run right before the command buffer is submitted. + WebGPUQueuedUniformDataUpload *queuedUniformUploads; + + Uint32 numQueuedUniformUploads; + Uint32 queuedUniformUploadCapacity; + + size_t totalUniformDataLen; + + bool hasBoundGraphicsPipeline; + bool hasBoundComputePipeline; + + struct vertexStageBinds + { + WebGPUSampler *boundSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE]; + WebGPUBuffer *boundStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE]; + WebGPUOffsetBufferBinding boundVertexBuffers[MAX_VERTEX_BUFFERS]; + WebGPUOffsetBufferBinding boundIndexBuffer; + + WGPUIndexFormat indexFormat; + + Uint32 currentUniformWriteOffsets[4]; + Uint32 currentUniformReadOffsets[4]; + + bool shouldBindVertexBuffers; + bool shouldBindIndexBuffer; + bool samplerStorageBindGroupOutdated; + bool uniformBindGroupOutdated; // The bind group itself can never become outdated, but the read offsets can. + } vertexStageBinds; + + struct fragmentStageBinds + { + WebGPUSampler *boundSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE]; + WebGPUBuffer *boundStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE]; + + Uint32 currentUniformWriteOffsets[4]; + Uint32 currentUniformReadOffsets[4]; + + bool samplerStorageBindGroupOutdated; + bool uniformBindGroupOutdated; // The bind group itself can never become outdated, but the read offsets can. + } fragmentStageBinds; + + struct computeStageBinds + { + WebGPUSampler *boundSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE]; + WebGPUTextureView *boundReadOnlyStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE]; + WebGPUTextureView *boundReadWriteStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE]; + WebGPUBuffer *boundReadOnlyStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE]; + WebGPUBuffer *boundReadWriteStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE]; + + Uint32 currentUniformWriteOffsets[4]; + Uint32 currentUniformReadOffsets[4]; + + bool samplerStorageBindGroupOutdated; + bool readWriteStorageBindGroupOutdated; + bool uniformBindGroupOutdated; + } computeStageBinds; + + Uint32 surfaceCount; + Uint32 surfaceCapacity; + WGPUSurface *surfaces; + + Uint32 swapchainTextureCount; + Uint32 swapchainTextureCapacity; + WebGPUTextureContainer **acquiredSwapchainTextures; + + // These are used to ensure that the end-user doesn't use a scissor + // that's larger than the smallest color target's, since WebGPU disallows + // that and will die a horrible, painful death from even considering it + Uint32 smallestColorTargetW; + Uint32 smallestColorTargetH; +} WebGPUCommandBuffer; + +static void +WEBGPU_INTERNAL_FenceCallback(WGPUQueueWorkDoneStatus status, WGPUStringView message, void *fence, void *unused) +{ + if (fence != NULL) { + // We're just gonna presume this never fails since that's easier + ((WebGPUFence *)fence)->signaled = true; + } +} + +static void +WEBGPU_INTERNAL_UncapturedErrorCallback(WGPUDevice const *device, WGPUErrorType type, WGPUStringView message, void *renderer, void *unused) +{ + SDL_LogError(SDL_LOG_CATEGORY_GPU, "WebGPU uncaptured error!\n%s", message.data); + + if (((WebGPURenderer *)renderer)->debugMode) { + SDL_assert_release(!"Uncaptured WebGPU error, message has been logged to SDL_LOG_CATEGORY_GPU"); + } +} + +static inline Uint32 WEBGPU_INTERNAL_GetTextureViewIndex(Uint32 layer, Uint32 level, Uint32 numMips) +{ + return (layer * numMips) + level; +} + +// oh my god these functions were so annoying to write out +static bool WEBGPU_INTERNAL_TextureFormatSupportsBlending(WGPUTextureFormat format, WebGPURenderer *renderer) +{ + switch (format) { + // always blendable + case WGPUTextureFormat_R8Unorm: + case WGPUTextureFormat_R16Unorm: + case WGPUTextureFormat_R16Snorm: + case WGPUTextureFormat_R16Float: + case WGPUTextureFormat_RG8Unorm: + case WGPUTextureFormat_RG16Unorm: + case WGPUTextureFormat_RG16Snorm: + case WGPUTextureFormat_RG16Float: + case WGPUTextureFormat_RGBA8Unorm: + case WGPUTextureFormat_BGRA8Unorm: + case WGPUTextureFormat_RGBA16Unorm: + case WGPUTextureFormat_RGBA16Snorm: + case WGPUTextureFormat_RGBA16Float: + case WGPUTextureFormat_RGBA8UnormSrgb: + case WGPUTextureFormat_BGRA8UnormSrgb: + case WGPUTextureFormat_RGB10A2Unorm: + return true; + + // requires tier 1 + case WGPUTextureFormat_R8Snorm: + case WGPUTextureFormat_RG8Snorm: + case WGPUTextureFormat_RGBA8Snorm: + return WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureFormatsTier1); + + // these require float32-blendable + case WGPUTextureFormat_R32Float: + case WGPUTextureFormat_RG32Float: + case WGPUTextureFormat_RGBA32Float: + return WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_Float32Blendable); + + // rg11b10ufloat is a special princess and requires its own feature + case WGPUTextureFormat_RG11B10Ufloat: + return WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_RG11B10UfloatRenderable); + + // never blendable + // NOTE: i struggled a bit wondering if I should explicitly define the unsupported ones, + // but I decided against it since it'd be a giant block of code (80 formats!) + // if the pros think it'd be better to have them explicit though, i'll add them back + default: + return false; + } +} + +// there might be some inaccuracies here i've just been copying directly from the spec with my meaty, flawed eyes +static bool WEBGPU_INTERNAL_TextureFormatSupportsStorageAccess(WebGPURenderer *renderer, WGPUTextureFormat format, WGPUStorageTextureAccess access) +{ + // Shortened names for convience + const bool t1 = WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureFormatsTier1); + const bool t2 = WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureFormatsTier2); + + switch (format) { + // always supports all accesses + case WGPUTextureFormat_R32Uint: + case WGPUTextureFormat_R32Sint: + case WGPUTextureFormat_R32Float: + return true; + + // always supports write-only & read-only, read-write is supported with tier 2 + case WGPUTextureFormat_RGBA8Uint: + case WGPUTextureFormat_RGBA8Sint: + case WGPUTextureFormat_RGBA8Unorm: + case WGPUTextureFormat_RGBA16Uint: + case WGPUTextureFormat_RGBA16Sint: + case WGPUTextureFormat_RGBA32Uint: + case WGPUTextureFormat_RGBA32Sint: + case WGPUTextureFormat_RGBA16Float: + case WGPUTextureFormat_RGBA32Float: + return access != WGPUStorageTextureAccess_ReadWrite || t2; + + // always supports write-only & read-only, read-write is not supported + case WGPUTextureFormat_R16Unorm: + case WGPUTextureFormat_R16Snorm: + // RG32 technically requires the "core-features-and-limits" feature so we could check for it, + // but since it's required for the WebGPU backend, we'll pretty safely assume that it's enabled. + case WGPUTextureFormat_RG32Uint: + case WGPUTextureFormat_RG32Sint: + case WGPUTextureFormat_RG32Float: + case WGPUTextureFormat_RG16Unorm: + case WGPUTextureFormat_RG16Snorm: + case WGPUTextureFormat_RGBA8Snorm: + case WGPUTextureFormat_RGBA16Unorm: + case WGPUTextureFormat_RGBA16Snorm: + return access != WGPUStorageTextureAccess_ReadWrite; + + // supports write-only & read-only with t1, read-write with t2 + case WGPUTextureFormat_R8Uint: + case WGPUTextureFormat_R8Sint: + case WGPUTextureFormat_R8Unorm: + case WGPUTextureFormat_R16Uint: + case WGPUTextureFormat_R16Sint: + case WGPUTextureFormat_R16Float: + return (access != WGPUStorageTextureAccess_ReadWrite && t1) || t2; + + // supports write-only & read-only wtih t1, read-write is not supported + case WGPUTextureFormat_R8Snorm: + case WGPUTextureFormat_RG8Uint: + case WGPUTextureFormat_RG8Sint: + case WGPUTextureFormat_RG8Snorm: + case WGPUTextureFormat_RG8Unorm: + case WGPUTextureFormat_RG16Uint: + case WGPUTextureFormat_RG16Sint: + case WGPUTextureFormat_RG16Float: + case WGPUTextureFormat_RGB10A2Uint: + case WGPUTextureFormat_RGB10A2Unorm: + case WGPUTextureFormat_RG11B10Ufloat: + return access != WGPUStorageTextureAccess_ReadWrite && t1; + + // supports write-only with bgra8unorm-storage feature + case WGPUTextureFormat_BGRA8Unorm: + return access == WGPUStorageTextureAccess_WriteOnly && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_BGRA8UnormStorage); + + // if not explicitly said to be fine, a texture format will be treated as unsupported + // see WEBGPU_INTERNAL_TextureFormatSupportsBlending for my reasoning behind this + default: + return false; + } +}; + +static SDL_PropertiesID WEBGPU_GetDeviceProperties( + SDL_GPUDevice *device) +{ + WebGPURenderer *renderer = (WebGPURenderer *)device; + return renderer->props; +} + +// forward decl +static void WEBGPU_INTERNAL_RequestDevice(WebGPURenderer *renderer, bool *success); + +static void WEBGPU_INTERNAL_DeviceLostCallback(WGPUDevice const *device, WGPUDeviceLostReason reason, WGPUStringView message, void *renderer, void *unused) +{ + if (!((WebGPURenderer *)renderer)->destroyingSelf && ((WebGPURenderer *)renderer)->debugMode) { + // TODO: I couldn't really figure out how to write a good error message here? + SDL_assert_release(!"WebGPU device has been lost."); + } +} + +static void WEBGPU_INTERNAL_RequestAdapterCallback(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, void *renderer, void *success) +{ + if (status == WGPURequestAdapterStatus_Success) { + ((WebGPURenderer *)renderer)->adapter = adapter; + if (((WebGPURenderer *)renderer)->debugMode) { + SDL_LogDebug(SDL_LOG_CATEGORY_GPU, "Acquired WebGPU adapter!"); + } + if (success != NULL) { + *((bool *)success) = true; + } + } else { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Requesting WebGPU adapter failed!\n'%s'", message.data); + if (success != NULL) { + *((bool *)success) = false; + } + } +} + +// Generate an FNV-1a hash for this data. +// +// This hash function is based on Landon Curt Noll's hash_32a.c FNV-1a implementation. +// https://github.com/lcn2/fnv/blob/master/hash_32a.c +static Uint32 WEBGPU_INTERNAL_GenFNV1aHash(Uint32 initialHash, const void *data, Uint32 size) +{ + Uint32 hash = initialHash; + + const unsigned char *slice = data; + const unsigned char *endOfData = data + size; + + while (slice < endOfData) { + hash ^= (Uint32)*slice++; + hash *= 0x01000193; + } + + return hash; +} + +static Uint32 WEBGPU_INTERNAL_HashBindGroupKey(void *unused, const void *key) +{ + Uint32 bufferSize = sizeof(WebGPUBindGroupCacheKey); + + return WEBGPU_INTERNAL_GenFNV1aHash(0, key, bufferSize); +} + +static void WEBGPU_INTERNAL_DestroyCachedBindGroupAndKey(void *unused, const void *key, const void *value) +{ + WebGPUBindGroup *bindGroup = (WebGPUBindGroup *)value; // "Bu-bu-but that's a const pointe-" shh my child, segmentation faults don't happen if you're pure of heart + if (bindGroup == NULL) { + return; + } + + wgpuBindGroupRelease(bindGroup->bindGroup); + SDL_free(bindGroup); +} + +static bool WEBGPU_INTERNAL_MatchHashedBindGroupKey(void *unused, const void *a, const void *b) +{ + return SDL_memcmp(a, b, sizeof(WebGPUBindGroupCacheKey)) == 0; +} + +static void +WEBGPU_INTERNAL_RequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void *renderer, void *success) +{ + if (status == WGPURequestDeviceStatus_Success) { + ((WebGPURenderer *)renderer)->device = device; + if (((WebGPURenderer *)renderer)->debugMode) { + SDL_LogDebug(SDL_LOG_CATEGORY_GPU, "Acquired WebGPU device!"); + } + if (success != NULL) { + *((bool *)success) = true; + } + } else { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Requesting WebGPU device failed!\n'%s'", message.data); + if (success != NULL) { + *((bool *)success) = false; + } + } +} + +static inline void WEBGPU_INTERNAL_RegisterQueuedDestroy(WebGPURenderer *renderer, WebGPUQueuedDestroy *destroy) +{ + WEBGPU_INTERNAL_InsertElementIntoArray(renderer->queuedDestroys, renderer->queuedDestroyCapacity, + renderer->queuedDestroyCount, WebGPUQueuedDestroy *, destroy); +} + +static void WEBGPU_INTERNAL_TrackBuffer(WebGPUCommandBuffer *commandBuffer, WebGPUBuffer *buffer) +{ + TRACK_RESOURCE(buffer, WebGPUBuffer *, usedBuffers, usedBufferCount, usedBufferCapacity, buffer->referenceCount); +} + +static void WEBGPU_INTERNAL_TrackTexture(WebGPUCommandBuffer *commandBuffer, WebGPUTexture *texture) +{ + TRACK_RESOURCE(texture, WebGPUTexture *, usedTextures, usedTextureCount, usedTextureCapacity, texture->referenceCount); +} + +static void WEBGPU_INTERNAL_QueueTextureContainerForRelease(WebGPURenderer *renderer, WebGPUTextureContainer *container) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (container == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_TEXTURE_CONTAINER; + destroy->resource.textureContainer = container; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_QueueSamplerForRelease(WebGPURenderer *renderer, WebGPUSampler *sampler) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (sampler == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_SAMPLER; + destroy->resource.sampler = sampler; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_QueueBufferContainerForRelease(WebGPURenderer *renderer, WebGPUBufferContainer *container) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (container == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_BUFFER_CONTAINER; + destroy->resource.bufferContainer = container; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_QueueFenceForRelease(WebGPURenderer *renderer, WebGPUFence *fence) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (fence == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_FENCE; + destroy->resource.fence = fence; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_QueueSubmittedCommandBufferForRelease(WebGPURenderer *renderer, WebGPUSubmittedCommandBuffer *commandBuffer) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (commandBuffer == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_SUBMITTED_COMMAND_BUFFER; + destroy->resource.submittedCommandBuffer = commandBuffer; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_QueueBindGroupForRelease(WebGPURenderer *renderer, WebGPUBindGroup *bindGroup) +{ + WebGPUQueuedDestroy *destroy = NULL; + if (bindGroup == NULL) { + return; + } + + destroy = SDL_calloc(1, sizeof(*destroy)); + destroy->type = WEBGPU_QUEUED_DESTROY_BIND_GROUP; + destroy->resource.bindGroup = bindGroup; + + WEBGPU_INTERNAL_RegisterQueuedDestroy(renderer, destroy); +} + +static void WEBGPU_INTERNAL_RequestAdapter(WebGPURenderer *renderer, bool *success) +{ + WGPURequestAdapterOptions adapterReqOptions = WGPU_REQUEST_ADAPTER_OPTIONS_INIT; + + adapterReqOptions.powerPreference = renderer->preferLowPower ? WGPUPowerPreference_LowPower : WGPUPowerPreference_HighPerformance; + + WGPUFuture future = wgpuInstanceRequestAdapter(renderer->instance, &adapterReqOptions, (WGPURequestAdapterCallbackInfo){ + .callback = WEBGPU_INTERNAL_RequestAdapterCallback, + .mode = WGPUCallbackMode_WaitAnyOnly, + .nextInChain = NULL, + .userdata1 = renderer, + .userdata2 = success, + }); + + WGPUFutureWaitInfo waitInfo = { + future, + false, + }; + + while (!waitInfo.completed) { + wgpuInstanceWaitAny(renderer->instance, 1, &waitInfo, 0); + WEBGPU_INTERNAL_Spin(); + } +} + +static void WEBGPU_INTERNAL_RequestDevice(WebGPURenderer *renderer, bool *success) +{ + WGPUDeviceDescriptor deviceDesc = WGPU_DEVICE_DESCRIPTOR_INIT; + + bool clipDistance = SDL_GetBooleanProperty(renderer->props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_CLIP_DISTANCE_BOOLEAN, false); + Uint32 numFeaturesEnabled = 0; + Uint32 featureCapacity = SDL_arraysize(WEBGPU_INTERNAL_RequiredFeatures) + SDL_arraysize(WEBGPU_INTERNAL_OptionalFeatures) + 1; + + WGPUFeatureName *features = SDL_calloc(featureCapacity, sizeof(WGPUFeatureName)); + WGPUSupportedFeatures supportedFeatures = { 0 }; + + wgpuAdapterGetFeatures(renderer->adapter, &supportedFeatures); + + if (clipDistance) { + WEBGPU_INTERNAL_InsertElementIntoArray(features, featureCapacity, numFeaturesEnabled, WGPUFeatureName, WGPUFeatureName_ClipDistances); + } + + // This code is horrible. + for (int i = 0; i < SDL_arraysize(WEBGPU_INTERNAL_RequiredFeatures); i++) { + bool supported = false; + for (int j = 0; j < supportedFeatures.featureCount; j++) { + if (WEBGPU_INTERNAL_RequiredFeatures[i] == supportedFeatures.features[j]) { + WEBGPU_INTERNAL_InsertElementIntoArray(features, featureCapacity, numFeaturesEnabled, WGPUFeatureName, WEBGPU_INTERNAL_RequiredFeatures[i]); + supported = true; + break; + } + } + if (!supported) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "WebGPU adapter does not support required feature \"%s\"!", WEBGPU_FeatureNameToString(WEBGPU_INTERNAL_RequiredFeatures[i])); + SDL_assert_release(!"WebGPU adapter does not support all required features!"); + } + + SDL_assert_release(WEBGPU_INTERNAL_RequiredFeatures[i] <= WEBGPU_FEATURE_AT_END_OF_ENUM); + renderer->webgpuFeatureEnabled[WEBGPU_INTERNAL_RequiredFeatures[i]] = supported; + } + + for (int i = 0; i < SDL_arraysize(WEBGPU_INTERNAL_OptionalFeatures); i++) { + bool supported = false; + + for (int j = 0; j < supportedFeatures.featureCount; j++) { + if (WEBGPU_INTERNAL_OptionalFeatures[i] == supportedFeatures.features[j]) { + WEBGPU_INTERNAL_InsertElementIntoArray(features, featureCapacity, numFeaturesEnabled, WGPUFeatureName, WEBGPU_INTERNAL_OptionalFeatures[i]); + supported = true; + break; + } + } + + if (!supported && renderer->debugMode) { + SDL_LogDebug(SDL_LOG_CATEGORY_GPU, "WebGPU adapter does not support optional feature \"%s\".", WEBGPU_FeatureNameToString(WEBGPU_INTERNAL_OptionalFeatures[i])); + } + + SDL_assert_release(WEBGPU_INTERNAL_OptionalFeatures[i] <= WEBGPU_FEATURE_AT_END_OF_ENUM); + renderer->webgpuFeatureEnabled[WEBGPU_INTERNAL_OptionalFeatures[i]] = supported; + } + + deviceDesc.deviceLostCallbackInfo = (WGPUDeviceLostCallbackInfo){ + .callback = WEBGPU_INTERNAL_DeviceLostCallback, + .mode = WGPUCallbackMode_AllowSpontaneous, + .nextInChain = NULL, + .userdata1 = renderer, + .userdata2 = NULL, + }; + + deviceDesc.uncapturedErrorCallbackInfo = (WGPUUncapturedErrorCallbackInfo){ + .callback = WEBGPU_INTERNAL_UncapturedErrorCallback, + .nextInChain = NULL, + .userdata1 = renderer, + .userdata2 = NULL, + }; + + deviceDesc.requiredFeatureCount = numFeaturesEnabled; + deviceDesc.requiredFeatures = features; + + WGPUFuture future = wgpuAdapterRequestDevice(renderer->adapter, &deviceDesc, (WGPURequestDeviceCallbackInfo){ + .callback = WEBGPU_INTERNAL_RequestDeviceCallback, + .mode = WGPUCallbackMode_WaitAnyOnly, + .nextInChain = NULL, + .userdata1 = renderer, + .userdata2 = success, + }); + + WGPUFutureWaitInfo waitInfo = { + future, + false, + }; + + while (!waitInfo.completed) { + wgpuInstanceWaitAny(renderer->instance, 1, &waitInfo, 0); + WEBGPU_INTERNAL_Spin(); + } + + wgpuSupportedFeaturesFreeMembers(supportedFeatures); + SDL_free(features); +} + +static void WEBGPU_BeginCopyPass( + SDL_GPUCommandBuffer *commandBuffer) +{ + // No-op. +} + +static void WEBGPU_EndCopyPass(SDL_GPUCommandBuffer *copyPass) +{ + // Unfortunately, this function will never be implemented. + // + // We simply do not, as a human race, have enough compute on this Earth + // to do the incredibly computationally expensive task, called "doing nothing" +} + +static SDL_GPUCommandBuffer *WEBGPU_AcquireCommandBuffer(SDL_GPURenderer *device) +{ + WebGPUCommandBuffer *wrapper; + wrapper = (WebGPUCommandBuffer *)SDL_calloc(1, sizeof(*wrapper)); + + wrapper->renderer = (WebGPURenderer *)device; + + wrapper->encoder = wgpuDeviceCreateCommandEncoder(((WebGPURenderer *)device)->device, NULL); + wrapper->queue = ((WebGPURenderer *)device)->queue; + wrapper->device = ((WebGPURenderer *)device)->device; + wrapper->instance = ((WebGPURenderer *)device)->instance; + + WGPUSurface *surfaces; + WebGPUTextureContainer **acquiredSwapchainTextures; + surfaces = SDL_calloc(2, sizeof(WGPUSurface)); + acquiredSwapchainTextures = SDL_calloc(2, sizeof(*acquiredSwapchainTextures)); + + // Allocating enough space for two since XR and stuff + // It'll automatically resize if needed but that's slow so eeeh + wrapper->surfaces = surfaces; + wrapper->surfaceCount = 0; + wrapper->surfaceCapacity = 2; + + wrapper->acquiredSwapchainTextures = acquiredSwapchainTextures; + wrapper->swapchainTextureCount = 0; + wrapper->swapchainTextureCapacity = 2; + + return (SDL_GPUCommandBuffer *)wrapper; +} + +/** + * Set all of the render pass's bound resources to NULL. + */ +static void WEBGPU_INTERNAL_ClearRenderPassBindings(WebGPUCommandBuffer *cmdBuf) +{ + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + cmdBuf->vertexStageBinds.boundTextures[i] = NULL; + cmdBuf->vertexStageBinds.boundSamplers[i] = NULL; + cmdBuf->fragmentStageBinds.boundTextures[i] = NULL; + cmdBuf->fragmentStageBinds.boundSamplers[i] = NULL; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + cmdBuf->vertexStageBinds.boundStorageTextures[i] = NULL; + cmdBuf->fragmentStageBinds.boundStorageTextures[i] = NULL; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + cmdBuf->vertexStageBinds.boundStorageBuffers[i] = NULL; + cmdBuf->fragmentStageBinds.boundStorageBuffers[i] = NULL; + } + + // for (int i = 0; i < MAX_VERTEX_BUFFERS; i++) { + // cmdBuf->vertexStageBinds.boundVertexBuffers[i].buffer = NULL; + // cmdBuf->vertexStageBinds.boundVertexBuffers[i].offset = 0; + // } + + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated = true; + cmdBuf->vertexStageBinds.uniformBindGroupOutdated = true; + + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated = true; + cmdBuf->fragmentStageBinds.uniformBindGroupOutdated = true; + + cmdBuf->vertexStageBinds.shouldBindVertexBuffers = true; + cmdBuf->vertexStageBinds.shouldBindIndexBuffer = true; +} + +/** + * Set all of the compute pass's bound resources to NULL. + * clearReadWriteBindings prevents... clearing read write bindings. Since they're set at the beginning of the compute pass, clearing them when the pipeline's bound is A Bad Thing! + */ +static void WEBGPU_INTERNAL_ClearComputePassBindings(WebGPUCommandBuffer *cmdBuf, bool clearReadWriteBindings) +{ + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + cmdBuf->computeStageBinds.boundSamplers[i] = NULL; + cmdBuf->computeStageBinds.boundTextures[i] = NULL; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i] = NULL; + if (clearReadWriteBindings) { + cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i] = NULL; + } + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i] = NULL; + if (clearReadWriteBindings) { + cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i] = NULL; + } + } + + cmdBuf->computeStageBinds.uniformBindGroupOutdated = true; + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated = true; + cmdBuf->computeStageBinds.readWriteStorageBindGroupOutdated = true; +} + +static void WEBGPU_INTERNAL_FreeCommandBuffer(WebGPUCommandBuffer *cmdBuf) +{ + SDL_free(cmdBuf->surfaces); + SDL_free(cmdBuf->acquiredSwapchainTextures); + SDL_free(cmdBuf->queuedUniformUploads); + SDL_free(cmdBuf); +} + +// Create a new fence which will be called by wgpuQueueOnSubmittedWorkDone +static WebGPUFence *WEBGPU_INTERNAL_CreateFence(WGPUQueue queue) +{ + WebGPUFence *fence = SDL_calloc(1, sizeof(*fence)); + + fence->future.future = wgpuQueueOnSubmittedWorkDone(queue, (WGPUQueueWorkDoneCallbackInfo){ + .callback = WEBGPU_INTERNAL_FenceCallback, + .mode = WGPUCallbackMode_WaitAnyOnly, + .nextInChain = NULL, + .userdata1 = fence, + .userdata2 = NULL, + }); + + return fence; +} + +static WebGPUFence *WEBGPU_INTERNAL_CreateFenceFromFuture(WGPUFuture future) +{ + WebGPUFence *fence = SDL_calloc(1, sizeof(*fence)); + + fence->future.future = future; + + return fence; +} + +static bool WEBGPU_INTERNAL_QueryFence(WebGPURenderer *renderer, WebGPUFence *fence) +{ + if (fence != NULL) { + if (!fence->signaled) { + // Despite its name, WaitAny isn't actually blocking unless the TimedWaitAny instance feature is enabled, + // and you give a value to the timeoutNS argument. + wgpuInstanceWaitAny(renderer->instance, 1, &fence->future, 0); + + fence->signaled = fence->future.completed; + } + return fence->signaled; + } else { + return true; + } +} + +static bool WEBGPU_QueryFence(SDL_GPURenderer *device, SDL_GPUFence *fence) +{ + return WEBGPU_INTERNAL_QueryFence((WebGPURenderer *)device, (WebGPUFence *)fence); +} + +static bool WEBGPU_INTERNAL_WaitForFences(WebGPURenderer *renderer, bool waitAll, WebGPUFence *const *fences, Uint32 num_fences) +{ + Uint32 triggeredFenceCount = 0; + Uint32 triggeredFenceThreshold = waitAll ? num_fences : 1; + + while (triggeredFenceCount < triggeredFenceThreshold) { + triggeredFenceCount = 0; + + for (int i = 0; i < num_fences; i++) { + if (WEBGPU_INTERNAL_QueryFence(renderer, fences[i])) { + triggeredFenceCount++; + } + + // Spin to appease Emscripten. + WEBGPU_INTERNAL_Spin(); + } + } + + return true; +} + +static bool WEBGPU_WaitForFences(SDL_GPURenderer *device, bool waitAll, SDL_GPUFence *const *fences, Uint32 num_fences) +{ + return WEBGPU_INTERNAL_WaitForFences((WebGPURenderer *)device, waitAll, (WebGPUFence *const *)fences, num_fences); +} + +static bool WEBGPU_Wait(SDL_GPURenderer *driverData) +{ + WebGPURenderer *renderer = (WebGPURenderer *)driverData; + // We'll wait until all the submitted command buffer fences have been called. + WebGPUFence **fences = NULL; + + Uint32 fenceCapacity = 0; + Uint32 fenceCount = 0; + + for (int i = 0; i < renderer->submittedCommandBufferCount; i++) { + WEBGPU_INTERNAL_InsertElementIntoArray(fences, fenceCapacity, fenceCount, WebGPUFence *, renderer->submittedCommandBuffers[i]->fence); + } + + if (fenceCount > 0) { + WEBGPU_INTERNAL_WaitForFences(renderer, true, fences, fenceCount); + } + + if (fences) { + SDL_free(fences); + } + + // I can't really think of a scenario in which this'd fail? + return true; +} + +static void WEBGPU_INTERNAL_GenerateBindGroupLayoutsForCurrentGraphicsBinds(WebGPUCommandBuffer *cmdBuf) +{ + WGPUBindGroupLayoutEntry *samplerEntriesVert = NULL; + WGPUBindGroupLayoutEntry *samplerEntriesFrag = NULL; + WGPUBindGroupLayoutDescriptor samplerLayoutVertDesc = { 0 }; + WGPUBindGroupLayoutDescriptor samplerLayoutFragDesc = { 0 }; + + Uint32 samplerEntryCountVertex = 0; + Uint32 samplerEntryCountFragment = 0; + Uint32 samplerEntryVertCapacity = 0; + Uint32 samplerEntryFragCapacity = 0; + + Uint32 offsetVert = 0; + Uint32 offsetFrag = 0; + + // 48 is enough to store all textures (16), samplers (16), storage textures (8), and storage buffers (8) + WebGPUBindGroupLayoutEntryType entriesVert[48] = { 0 }; + WebGPUBindGroupLayoutEntryType entriesFrag[48] = { 0 }; + Uint32 entriesVertOffset = 0; + Uint32 entriesFragOffset = 0; + + Uint32 hash = 0xAAAA1BEE; + + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + // We don't actually care about the sampler. + WebGPUTextureView *textureViewVert = cmdBuf->vertexStageBinds.boundTextures[i]; + WebGPUTextureView *textureViewFrag = cmdBuf->fragmentStageBinds.boundTextures[i]; + + if (textureViewVert) { + WGPUBindGroupLayoutEntry textureEntry = { + .binding = offsetVert++, + .texture = { + .multisampled = textureViewVert->parent->sampleCount != SDL_GPU_SAMPLECOUNT_1, + .nextInChain = NULL, + .sampleType = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureViewVert->parent->format]), + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewVert->parent->type], + }, + .visibility = WGPUShaderStage_Vertex, + }; + + WGPUBindGroupLayoutEntry samplerEntry = { + .binding = offsetVert++, + .sampler = { + .type = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WGPUSamplerBindingType_NonFiltering + : WGPUSamplerBindingType_Filtering, + }, + .visibility = WGPUShaderStage_Vertex, + }; + + entriesVert[entriesVertOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE_UNFILTERABLE + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE; + entriesVert[entriesVertOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER_NONFILTERING + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesVert, samplerEntryVertCapacity, samplerEntryCountVertex, WGPUBindGroupLayoutEntry, textureEntry); + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesVert, samplerEntryVertCapacity, samplerEntryCountVertex, WGPUBindGroupLayoutEntry, samplerEntry); + } + + if (textureViewFrag) { + WGPUBindGroupLayoutEntry textureEntry = { + .binding = offsetFrag++, + .texture = { + .multisampled = textureViewFrag->parent->sampleCount != SDL_GPU_SAMPLECOUNT_1, + .nextInChain = NULL, + .sampleType = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureViewFrag->parent->format]), + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewFrag->parent->type], + }, + .visibility = WGPUShaderStage_Fragment, + }; + + WGPUBindGroupLayoutEntry samplerEntry = { + .binding = offsetFrag++, + .sampler = { + .type = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WGPUSamplerBindingType_NonFiltering + : WGPUSamplerBindingType_Filtering, + }, + .visibility = WGPUShaderStage_Fragment, + }; + + entriesFrag[entriesFragOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE_UNFILTERABLE + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE; + entriesFrag[entriesFragOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER_NONFILTERING + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesFrag, samplerEntryFragCapacity, samplerEntryCountFragment, WGPUBindGroupLayoutEntry, textureEntry); + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesFrag, samplerEntryFragCapacity, samplerEntryCountFragment, WGPUBindGroupLayoutEntry, samplerEntry); + } + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureViewVert = cmdBuf->vertexStageBinds.boundStorageTextures[i]; + WebGPUTextureView *textureViewFrag = cmdBuf->fragmentStageBinds.boundStorageTextures[i]; + + if (textureViewVert) { + WGPUBindGroupLayoutEntry storageTextureEntry = { + .binding = offsetVert++, + .storageTexture = { + .access = WGPUStorageTextureAccess_ReadOnly, + .format = SDLToWebGPU_TextureFormat[textureViewVert->parent->format], + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewVert->parent->type], + }, + .visibility = WGPUShaderStage_Vertex, + }; + + entriesVert[entriesVertOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesVert, samplerEntryVertCapacity, samplerEntryCountVertex, WGPUBindGroupLayoutEntry, storageTextureEntry); + } + + if (textureViewFrag) { + WGPUBindGroupLayoutEntry storageTextureEntry = { + .binding = offsetFrag++, + .storageTexture = { + .access = WGPUStorageTextureAccess_ReadOnly, + .format = SDLToWebGPU_TextureFormat[textureViewFrag->parent->format], + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewFrag->parent->type], + }, + .visibility = WGPUShaderStage_Fragment, + }; + + entriesFrag[entriesFragOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesFrag, samplerEntryFragCapacity, samplerEntryCountFragment, WGPUBindGroupLayoutEntry, storageTextureEntry); + } + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *storageBufferVert = cmdBuf->vertexStageBinds.boundStorageBuffers[i]; + WebGPUBuffer *storageBufferFrag = cmdBuf->fragmentStageBinds.boundStorageBuffers[i]; + + if (storageBufferVert) { + WGPUBindGroupLayoutEntry storageBufferEntry = { + .binding = offsetVert++, + .buffer = { + .hasDynamicOffset = false, + .minBindingSize = 0, + .nextInChain = 0, + .type = WGPUBufferBindingType_ReadOnlyStorage, + }, + .visibility = WGPUShaderStage_Vertex, + }; + + entriesVert[entriesVertOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesVert, samplerEntryVertCapacity, samplerEntryCountVertex, WGPUBindGroupLayoutEntry, storageBufferEntry); + } + + if (storageBufferFrag) { + WGPUBindGroupLayoutEntry storageBufferEntry = { + .binding = offsetFrag++, + .buffer = { + .hasDynamicOffset = false, + .minBindingSize = 0, + .nextInChain = 0, + .type = WGPUBufferBindingType_ReadOnlyStorage, + }, + .visibility = WGPUShaderStage_Fragment, + }; + + entriesFrag[entriesFragOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER; + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntriesFrag, samplerEntryFragCapacity, samplerEntryCountFragment, WGPUBindGroupLayoutEntry, storageBufferEntry); + } + } + + hash = WEBGPU_INTERNAL_GenFNV1aHash(hash, (void *)&entriesVert, sizeof(WebGPUBindGroupLayoutEntryType) * 48); + hash = WEBGPU_INTERNAL_GenFNV1aHash(hash, (void *)&entriesFrag, sizeof(WebGPUBindGroupLayoutEntryType) * 48); + + if (cmdBuf->boundGraphicsPipeline->currentPipelineHash == hash && hash != 0xAAAA1BEE) { + goto finish; + } + + samplerLayoutVertDesc.entries = samplerEntriesVert; + samplerLayoutVertDesc.entryCount = samplerEntryCountVertex; + samplerLayoutVertDesc.label = (WGPUStringView){ NULL, 0 }; + samplerLayoutVertDesc.nextInChain = NULL; + + samplerLayoutFragDesc.entries = samplerEntriesFrag; + samplerLayoutFragDesc.entryCount = samplerEntryCountFragment; + samplerLayoutFragDesc.label = (WGPUStringView){ NULL, 0 }; + samplerLayoutFragDesc.nextInChain = NULL; + + if (cmdBuf->boundGraphicsPipeline->hasBeenCreated) { + // We'll need to free the previous layouts. + wgpuBindGroupLayoutRelease(cmdBuf->boundGraphicsPipeline->vertexBindGroupLayouts.samplerStorageBindGroupLayout); + wgpuBindGroupLayoutRelease(cmdBuf->boundGraphicsPipeline->fragmentBindGroupLayouts.samplerStorageBindGroupLayout); + } + + cmdBuf->boundGraphicsPipeline->vertexBindGroupLayouts.samplerStorageBindGroupLayout = wgpuDeviceCreateBindGroupLayout(cmdBuf->device, &samplerLayoutVertDesc); + cmdBuf->boundGraphicsPipeline->vertexBindGroupLayouts.uniformBindGroupLayout = cmdBuf->renderer->uniformBufferBindGroupLayouts[0]; // vtx + + cmdBuf->boundGraphicsPipeline->fragmentBindGroupLayouts.samplerStorageBindGroupLayout = wgpuDeviceCreateBindGroupLayout(cmdBuf->device, &samplerLayoutFragDesc); + cmdBuf->boundGraphicsPipeline->fragmentBindGroupLayouts.uniformBindGroupLayout = cmdBuf->renderer->uniformBufferBindGroupLayouts[1]; // frg + + cmdBuf->boundGraphicsPipeline->currentPipelineHash = hash; + + goto finish; +finish: + SDL_free(samplerEntriesVert); + SDL_free(samplerEntriesFrag); +} + +static void WEBGPU_INTERNAL_GenerateBindGroupLayoutsForCurrentComputeBinds(WebGPUCommandBuffer *cmdBuf) +{ + WGPUBindGroupLayoutEntry *samplerEntries = NULL; + WGPUBindGroupLayoutEntry *readWriteEntries = NULL; + + WGPUBindGroupLayoutDescriptor samplerLayoutDesc = { 0 }; + WGPUBindGroupLayoutDescriptor readWriteLayoutDesc = { 0 }; + + Uint32 samplerEntryCount = 0; + Uint32 samplerEntryCapacity = 0; + + Uint32 readWriteEntryCount = 0; + Uint32 readWriteEntryCapacity = 0; + + Uint32 offset = 0; + Uint32 offsetRW = 0; + Uint32 hash = 0xAAAA1BEE; + + // 64 is enough to fit all of them (probably) + WebGPUBindGroupLayoutEntryType entriesHashStuff[64] = { 0 }; + Uint32 entriesOffset = 0; + + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + WebGPUTextureView *textureView = cmdBuf->computeStageBinds.boundTextures[i]; + + if (textureView) { + WGPUBindGroupLayoutEntry textureEntry = { + .binding = offset++, + .texture = { + .multisampled = textureView->parent->sampleCount != SDL_GPU_SAMPLECOUNT_1, + .nextInChain = NULL, + .sampleType = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureView->parent->format]), + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureView->parent->type], + }, + .visibility = WGPUShaderStage_Compute, + }; + + WGPUBindGroupLayoutEntry samplerEntry = { + .binding = offset++, + .sampler = { + .nextInChain = NULL, + .type = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WGPUSamplerBindingType_NonFiltering + : WGPUSamplerBindingType_Filtering, + }, + .visibility = WGPUShaderStage_Compute, + }; + + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntries, samplerEntryCapacity, samplerEntryCount, WGPUBindGroupLayoutEntry, textureEntry); + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntries, samplerEntryCapacity, samplerEntryCount, WGPUBindGroupLayoutEntry, samplerEntry); + + entriesHashStuff[entriesOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE_UNFILTERABLE + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_TEXTURE; + entriesHashStuff[entriesOffset++] = textureEntry.texture.sampleType == + WGPUTextureSampleType_UnfilterableFloat + ? WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER_NONFILTERING + : WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_SAMPLER; + }; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureViewRO = cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i]; + WebGPUTextureView *textureViewRW = cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i]; + + if (textureViewRW) { + WGPUBindGroupLayoutEntry storageTextureEntry = { + .binding = offsetRW++, + .storageTexture = { + .access = WGPUStorageTextureAccess_Undefined, + .format = SDLToWebGPU_TextureFormat[textureViewRW->parent->format], + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewRW->parent->type], + }, + .visibility = WGPUShaderStage_Compute, + }; + + // if the texture format doesn't support simu read-write, we'll fall back to write-only + storageTextureEntry.storageTexture.access = + WEBGPU_INTERNAL_TextureFormatSupportsStorageAccess( + cmdBuf->renderer, storageTextureEntry.storageTexture.format, WGPUStorageTextureAccess_ReadWrite) + ? WGPUStorageTextureAccess_ReadWrite + : WGPUStorageTextureAccess_WriteOnly; + + WEBGPU_INTERNAL_InsertElementIntoArray(readWriteEntries, readWriteEntryCapacity, readWriteEntryCount, WGPUBindGroupLayoutEntry, storageTextureEntry); + entriesHashStuff[entriesOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE_READWRITE; + } + + if (textureViewRO) { + WGPUBindGroupLayoutEntry storageTextureEntry = { + .binding = offset++, + .storageTexture = { + .access = WGPUStorageTextureAccess_ReadOnly, + .format = SDLToWebGPU_TextureFormat[textureViewRO->parent->format], + .viewDimension = SDLToWebGPU_TextureTypeToViewDimension[textureViewRO->parent->type], + }, + .visibility = WGPUShaderStage_Compute, + }; + + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntries, samplerEntryCapacity, samplerEntryCount, WGPUBindGroupLayoutEntry, storageTextureEntry); + entriesHashStuff[entriesOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_TEXTURE; + } + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *storageBufferRO = cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i]; + WebGPUBuffer *storageBufferRW = cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i]; + + if (storageBufferRW) { + WGPUBindGroupLayoutEntry storageBufferEntry = { + .binding = offsetRW++, + .buffer = { + .hasDynamicOffset = false, + .minBindingSize = 0, + .nextInChain = 0, + .type = WGPUBufferBindingType_Storage, + }, + .visibility = WGPUShaderStage_Compute, + }; + + WEBGPU_INTERNAL_InsertElementIntoArray(readWriteEntries, readWriteEntryCapacity, readWriteEntryCount, WGPUBindGroupLayoutEntry, storageBufferEntry); + entriesHashStuff[entriesOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER; + } + + if (storageBufferRO) { + WGPUBindGroupLayoutEntry storageBufferEntry = { + .binding = offset++, + .buffer = { + .hasDynamicOffset = false, + .minBindingSize = 0, + .nextInChain = 0, + .type = WGPUBufferBindingType_ReadOnlyStorage, + }, + .visibility = WGPUShaderStage_Compute, + }; + + WEBGPU_INTERNAL_InsertElementIntoArray(samplerEntries, samplerEntryCapacity, samplerEntryCount, WGPUBindGroupLayoutEntry, storageBufferEntry); + entriesHashStuff[entriesOffset++] = WEBGPU_BIND_GROUP_LAYOUT_ENTRY_TYPE_STORAGE_BUFFER; + } + } + + hash = WEBGPU_INTERNAL_GenFNV1aHash(hash, (void *)&entriesHashStuff, sizeof(WebGPUBindGroupLayoutEntryType) * 64); + + if (cmdBuf->boundComputePipeline->currentPipelineHash == hash && hash != 0xAAA1ABEE) { + goto finish; + } + + samplerLayoutDesc.entries = samplerEntries; + samplerLayoutDesc.entryCount = samplerEntryCount; + samplerLayoutDesc.label = (WGPUStringView){ NULL, 0 }; + samplerLayoutDesc.nextInChain = NULL; + + readWriteLayoutDesc.entries = readWriteEntries; + readWriteLayoutDesc.entryCount = readWriteEntryCount; + readWriteLayoutDesc.label = (WGPUStringView){ NULL, 0 }; + readWriteLayoutDesc.nextInChain = NULL; + + cmdBuf->boundComputePipeline->bindGroupLayouts.samplerStorageBindGroupLayout = wgpuDeviceCreateBindGroupLayout(cmdBuf->device, &samplerLayoutDesc); + cmdBuf->boundComputePipeline->bindGroupLayouts.readWriteStorageBindGroupLayout = wgpuDeviceCreateBindGroupLayout(cmdBuf->device, &readWriteLayoutDesc); + cmdBuf->boundComputePipeline->bindGroupLayouts.uniformBindGroupLayout = cmdBuf->renderer->uniformBufferBindGroupLayouts[2]; + + cmdBuf->boundComputePipeline->currentPipelineHash = hash; + + goto finish; +finish: + SDL_free(samplerEntries); + SDL_free(readWriteEntries); +} + +static WebGPUBindGroup *WEBGPU_INTERNAL_GetBindGroupFromIdentifier(WebGPURenderer *renderer, WebGPUBindGroupCacheKey key) +{ + WebGPUBindGroup *result = NULL; + SDL_FindInHashTable(renderer->bindGroupHashTable, &key, (const void **)&result); + + return result; +} + +static bool WEBGPU_INTERNAL_BindGroupCacheIteratorCallback(void *userdata, const SDL_HashTable *table, const void *key, const void *value) +{ + WebGPURenderer *renderer = userdata; + WebGPUBindGroup *bindGroup = (WebGPUBindGroup *)value; + + if (bindGroup) { + if (SDL_GetAtomicInt(&renderer->numSubmissions) - SDL_GetAtomicInt(&bindGroup->lastUsedAtSubmission) > renderer->bindGroupsExpireAfter) { + WEBGPU_INTERNAL_QueueBindGroupForRelease(renderer, bindGroup); + bindGroup->invalid = true; + } + } + + return true; +} + +// This function checks all of the bind groups and releases any that haven't been used in a while. +static void WEBGPU_INTERNAL_TrimBindGroupCache(WebGPURenderer *renderer) +{ + SDL_IterateHashTable(renderer->bindGroupHashTable, WEBGPU_INTERNAL_BindGroupCacheIteratorCallback, renderer); +} + +static void WEBGPU_INTERNAL_CheckSubmittedCommandBuffers(WebGPURenderer *renderer) +{ + // We create a new buffer to store the non-completed command buffers. + // Unoptimized? Yes. + // Do I care? No. + WebGPUSubmittedCommandBuffer **notCompleted = NULL; + Uint32 notCompletedCapacity = 0; + Uint32 notCompletedCount = 0; + + if (renderer->submittedCommandBufferCount == 0) { + return; + } + + for (int i = renderer->submittedCommandBufferCount; i-- > 0;) { + WebGPUSubmittedCommandBuffer *current = renderer->submittedCommandBuffers[i]; + + if (current == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Submitted command buffer is NULL!"); + continue; + } + + if (!WEBGPU_INTERNAL_QueryFence(renderer, current->fence)) { + // Still not done, let's shove it into notCompleted. + WEBGPU_INTERNAL_InsertElementIntoArray(notCompleted, notCompletedCapacity, notCompletedCount, WebGPUSubmittedCommandBuffer *, current); + } else { + WEBGPU_INTERNAL_QueueSubmittedCommandBufferForRelease(renderer, current); + } + } + + // By now, all of the entries within submittedCommandBuffers should either have been queued for + // release, or moved to notCompleted. We'll free submittedCommandBuffers and replace it with notCompleted. + SDL_free(renderer->submittedCommandBuffers); + renderer->submittedCommandBufferCapacity = notCompletedCapacity; + renderer->submittedCommandBufferCount = notCompletedCount; + renderer->submittedCommandBuffers = notCompleted; +} + +static void WEBGPU_INTERNAL_ReleaseTextureContainer(WebGPURenderer *renderer, WebGPUTextureContainer *container); +static void WEBGPU_INTERNAL_ReleaseTexture(WebGPURenderer *renderer, WebGPUTexture *texture); +static void WEBGPU_INTERNAL_ReleaseSampler(WebGPURenderer *renderer, WebGPUSampler *sampler); +static void WEBGPU_INTERNAL_ReleaseBuffer(WebGPURenderer *renderer, WebGPUBuffer *buffer); +static void WEBGPU_INTERNAL_ReleaseBufferContainer(WebGPURenderer *renderer, WebGPUBufferContainer *container); + +static void WEBGPU_INTERNAL_HandlePendingDestroys(WebGPURenderer *renderer) +{ + WebGPUQueuedDestroy **newQueuedDestroys = NULL; + Uint32 newQueuedDestroysCapacity = 0; + Uint32 newQueuedDestroysCount = 0; + + // This needs to be run before the destruction loop since this function + // is what queues the submitted command buffers for freeing. + WEBGPU_INTERNAL_CheckSubmittedCommandBuffers(renderer); + WEBGPU_INTERNAL_TrimBindGroupCache(renderer); + + if (renderer->queuedDestroys != NULL && renderer->queuedDestroyCount > 0) { + for (int i = 0; i < renderer->queuedDestroyCount; i++) { + WebGPUQueuedDestroy *current = renderer->queuedDestroys[i]; + + int currentRefCount = 0; + bool forciblyDestroy = false; + bool wasReleased = false; + + if (current == NULL) { + continue; + } + + if (current->destroysAttempted >= FORCIBLY_DESTROY_QUEUED_DESTROY_AFTER_N_FAILED && renderer->debugMode) { + SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Queued destroy has been attempted %i times! Forcibly destroying.", current->destroysAttempted); + forciblyDestroy = true; + } + + switch (current->type) { + case WEBGPU_QUEUED_DESTROY_INVALID: + break; + case WEBGPU_QUEUED_DESTROY_TEXTURE_CONTAINER: + // Texture containers don't have any direct references, + // so we'll loop over all of the textures and add up their counts. + for (int j = 0; j < current->resource.textureContainer->textureCount; j++) { + int refCount = SDL_GetAtomicInt(¤t->resource.textureContainer->textures[j]->referenceCount); + currentRefCount += refCount; + + if (currentRefCount > 0) { + break; + } + } + + if (currentRefCount == 0 || forciblyDestroy) { + WEBGPU_INTERNAL_ReleaseTextureContainer(renderer, current->resource.textureContainer); + wasReleased = true; + } + break; + case WEBGPU_QUEUED_DESTROY_TEXTURE: + currentRefCount = SDL_GetAtomicInt(¤t->resource.texture->referenceCount); + if (currentRefCount == 0 || forciblyDestroy) { + WEBGPU_INTERNAL_ReleaseTexture(renderer, current->resource.texture); + wasReleased = true; + } + break; + case WEBGPU_QUEUED_DESTROY_SAMPLER: + WEBGPU_INTERNAL_ReleaseSampler(renderer, current->resource.sampler); + wasReleased = true; + break; + case WEBGPU_QUEUED_DESTROY_BUFFER_CONTAINER: + // Like with texture containers, the buffer container doesn't have any direct references. + for (int j = 0; j < current->resource.bufferContainer->bufferCount; j++) { + int refCount = SDL_GetAtomicInt(¤t->resource.bufferContainer->buffers[j]->referenceCount); + currentRefCount += refCount; + + if (currentRefCount > 0) { + break; + } + } + if (currentRefCount == 0 || forciblyDestroy) { + WEBGPU_INTERNAL_ReleaseBufferContainer(renderer, current->resource.bufferContainer); + wasReleased = true; + } + break; + case WEBGPU_QUEUED_DESTROY_BUFFER: + currentRefCount = SDL_GetAtomicInt(¤t->resource.buffer->referenceCount); + if (currentRefCount == 0 || forciblyDestroy) { + WEBGPU_INTERNAL_ReleaseBuffer(renderer, current->resource.buffer); + wasReleased = true; + } + break; + case WEBGPU_QUEUED_DESTROY_FENCE: + if (!WEBGPU_INTERNAL_QueryFence(renderer, current->resource.fence)) { + WEBGPU_INTERNAL_WaitForFences(renderer, true, ¤t->resource.fence, 1); + } + + SDL_free(current->resource.fence); + wasReleased = true; + + break; + case WEBGPU_QUEUED_DESTROY_SUBMITTED_COMMAND_BUFFER: + for (int j = 0; j < current->resource.submittedCommandBuffer->usedBufferCount; j++) { + (void)SDL_AtomicDecRef(¤t->resource.submittedCommandBuffer->usedBuffers[j]->referenceCount); + } + + for (int j = 0; j < current->resource.submittedCommandBuffer->usedTextureCount; j++) { + (void)SDL_AtomicDecRef(¤t->resource.submittedCommandBuffer->usedTextures[j]->referenceCount); + } + + SDL_free(current->resource.submittedCommandBuffer->usedTextures); + SDL_free(current->resource.submittedCommandBuffer->usedBuffers); + SDL_free(current->resource.submittedCommandBuffer->fence); + SDL_free(current->resource.submittedCommandBuffer); + + wasReleased = true; + break; + case WEBGPU_QUEUED_DESTROY_BIND_GROUP: + // FIXME: Bind groups don't have any reference counts, so there might be some issues with in-flight usages? + SDL_RemoveFromHashTable(renderer->bindGroupHashTable, ¤t->resource.bindGroup->key); // The key value destruction function also releases the bind group. + wasReleased = true; + + break; + } + + if (wasReleased) { + SDL_free(current); + } else { + WEBGPU_INTERNAL_InsertElementIntoArray(newQueuedDestroys, newQueuedDestroysCapacity, + newQueuedDestroysCount, WebGPUQueuedDestroy *, current); + current->destroysAttempted++; + } + } + + SDL_free(renderer->queuedDestroys); + + renderer->queuedDestroyCapacity = newQueuedDestroysCapacity; + renderer->queuedDestroyCount = newQueuedDestroysCount; + renderer->queuedDestroys = newQueuedDestroys; + } +} + +static WebGPUBindGroupCacheKey WEBGPU_INTERNAL_GenerateKeyForCurrentBinds(WebGPUCommandBuffer *cmdBuf, WebGPUBindGroupType desiredGroup) +{ + WebGPUBindGroupCacheKey result = { 0 }; + + switch (desiredGroup) { + case WEBGPU_BINDGROUP_VERTEXSAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + result.boundTexturesIDs[i] = cmdBuf->vertexStageBinds.boundTextures[i] ? cmdBuf->vertexStageBinds.boundTextures[i]->identifier : 0; + result.boundSamplersIDs[i] = cmdBuf->vertexStageBinds.boundSamplers[i] ? cmdBuf->vertexStageBinds.boundSamplers[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + result.boundReadOnlyStorageTexturesIDs[i] = cmdBuf->vertexStageBinds.boundStorageTextures[i] ? cmdBuf->vertexStageBinds.boundStorageTextures[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + result.boundReadOnlyStorageBuffersIDs[i] = cmdBuf->vertexStageBinds.boundStorageBuffers[i] ? cmdBuf->vertexStageBinds.boundStorageBuffers[i]->identifier : 0; + } + + result.stage = WGPUShaderStage_Vertex; + break; + } + case WEBGPU_BINDGROUP_VERTEXUNIFORMS: + { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempting to get hash of vertex uniform bind group! The uniform bind groups are currently pregenerated!"); + break; + } + case WEBGPU_BINDGROUP_FRAGMENTSAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + result.boundTexturesIDs[i] = cmdBuf->fragmentStageBinds.boundTextures[i] ? cmdBuf->fragmentStageBinds.boundTextures[i]->identifier : 0; + result.boundSamplersIDs[i] = cmdBuf->fragmentStageBinds.boundSamplers[i] ? cmdBuf->fragmentStageBinds.boundSamplers[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + result.boundReadOnlyStorageTexturesIDs[i] = cmdBuf->fragmentStageBinds.boundStorageTextures[i] ? cmdBuf->fragmentStageBinds.boundStorageTextures[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + result.boundReadOnlyStorageBuffersIDs[i] = cmdBuf->fragmentStageBinds.boundStorageBuffers[i] ? cmdBuf->fragmentStageBinds.boundStorageBuffers[i]->identifier : 0; + } + + result.stage = WGPUShaderStage_Fragment; + break; + } + case WEBGPU_BINDGROUP_FRAGMENTUNIFORMS: + { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempting to get hash of fragment uniform bind group! The uniform bind groups are currently pregenerated!"); + break; + } + case WEBGPU_BINDGROUP_COMPUTESAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + result.boundTexturesIDs[i] = cmdBuf->computeStageBinds.boundTextures[i] ? cmdBuf->computeStageBinds.boundTextures[i]->identifier : 0; + result.boundSamplersIDs[i] = cmdBuf->computeStageBinds.boundSamplers[i] ? cmdBuf->computeStageBinds.boundSamplers[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + result.boundReadOnlyStorageTexturesIDs[i] = cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i] ? cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + result.boundReadOnlyStorageBuffersIDs[i] = cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i] ? cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i]->identifier : 0; + } + + result.stage = WGPUShaderStage_Compute; + break; + } + case WEBGPU_BINDGROUP_COMPUTEREADWRITESTORAGE: + { + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + result.boundReadWriteStorageTexturesIDs[i] = cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i] ? cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i]->identifier : 0; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + result.boundReadWriteStorageBuffersIDs[i] = cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i] ? cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i]->identifier : 0; + } + + result.stage = WGPUShaderStage_Compute; + break; + } + case WEBGPU_BINDGROUP_COMPUTEUNIFORMS: + { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempting to get hash of compute uniform bind group! The uniform bind groups are currently pregenerated!"); + break; + } + } + + return result; +} + +static WebGPUBindGroup *WEBGPU_INTERNAL_CreateBindGroup(WebGPUCommandBuffer *cmdBuf, WebGPUBindGroupType type, WebGPUBindGroupCacheKey key) +{ + WebGPUBindGroup *result = NULL; + + WGPUBindGroupEntry entries[(MAX_TEXTURE_SAMPLERS_PER_STAGE * 2) + MAX_STORAGE_BUFFERS_PER_STAGE + MAX_STORAGE_TEXTURES_PER_STAGE] = { 0 }; + WGPUBindGroupDescriptor desc = { 0 }; + WGPUBindGroup bindGroup = NULL; + + Uint32 offset = 0; + + switch (type) { + case WEBGPU_BINDGROUP_VERTEXSAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + WebGPUSampler *samplerBind = cmdBuf->vertexStageBinds.boundSamplers[i]; + WebGPUTextureView *textureBind = cmdBuf->vertexStageBinds.boundTextures[i]; + + if (samplerBind == NULL && textureBind != NULL) { + SDL_assert_release(!"(VSS) Texture binding without corresponding sampler binding!"); + } else if (samplerBind != NULL && textureBind == NULL) { + SDL_assert_release(!"(VSS) Sampler binding without corresponding texture binding!"); + } else if (samplerBind == NULL && textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + + entries[offset].sampler = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureBind->parent->format]) == + WGPUTextureSampleType_Float + ? samplerBind->sampler + : samplerBind->fallbackSampler; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureBind = cmdBuf->vertexStageBinds.boundStorageTextures[i]; + if (textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *bufferBind = cmdBuf->vertexStageBinds.boundStorageBuffers[i]; + if (bufferBind == NULL) { + continue; + } + + entries[offset].buffer = bufferBind->buffer; + entries[offset].size = WGPU_WHOLE_SIZE; + entries[offset].binding = offset; + offset++; + } + + desc.entryCount = offset; + desc.layout = cmdBuf->boundGraphicsPipeline->vertexBindGroupLayouts.samplerStorageBindGroupLayout; + break; + } + case WEBGPU_BINDGROUP_VERTEXUNIFORMS: + { + SDL_assert_release(!"Attempting to create vertex uniform bind group! They're currently only pre-generated!"); + break; + } + case WEBGPU_BINDGROUP_FRAGMENTSAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + WebGPUSampler *samplerBind = cmdBuf->fragmentStageBinds.boundSamplers[i]; + WebGPUTextureView *textureBind = cmdBuf->fragmentStageBinds.boundTextures[i]; + + if (samplerBind == NULL && textureBind != NULL) { + SDL_assert_release(!"(VSS) Texture binding without corresponding sampler binding!"); + } else if (samplerBind != NULL && textureBind == NULL) { + SDL_assert_release(!"(VSS) Sampler binding without corresponding texture binding!"); + } else if (samplerBind == NULL && textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + + entries[offset].sampler = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureBind->parent->format]) == + WGPUTextureSampleType_Float + ? samplerBind->sampler + : samplerBind->fallbackSampler; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureBind = cmdBuf->fragmentStageBinds.boundStorageTextures[i]; + if (textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *bufferBind = cmdBuf->fragmentStageBinds.boundStorageBuffers[i]; + if (bufferBind == NULL) { + continue; + } + + entries[offset].buffer = bufferBind->buffer; + entries[offset].size = WGPU_WHOLE_SIZE; + entries[offset].binding = offset; + offset++; + } + + desc.entryCount = offset; + desc.layout = cmdBuf->boundGraphicsPipeline->fragmentBindGroupLayouts.samplerStorageBindGroupLayout; + + break; + } + case WEBGPU_BINDGROUP_FRAGMENTUNIFORMS: + { + SDL_assert_release(!"Attempting to create fragment uniform bind group! They're currently only pre-generated!"); + break; + } + case WEBGPU_BINDGROUP_COMPUTESAMPLERSTORAGE: + { + for (int i = 0; i < MAX_TEXTURE_SAMPLERS_PER_STAGE; i++) { + WebGPUSampler *samplerBind = cmdBuf->computeStageBinds.boundSamplers[i]; + WebGPUTextureView *textureBind = cmdBuf->computeStageBinds.boundTextures[i]; + + if (samplerBind == NULL && textureBind != NULL) { + SDL_assert_release(!"(CSS) Texture binding without corresponding sampler binding!"); + } else if (samplerBind != NULL && textureBind == NULL) { + SDL_assert_release(!"(CSS) Sampler binding without corresponding texture binding!"); + } else if (samplerBind == NULL && textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + + entries[offset].sampler = WebGPUTextureFormatToSampleType(SDLToWebGPU_TextureFormat[textureBind->parent->format]) == + WGPUTextureSampleType_Float + ? samplerBind->sampler + : samplerBind->fallbackSampler; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureBind = cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i]; + if (textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *bufferBind = cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i]; + if (bufferBind == NULL) { + continue; + } + + entries[offset].buffer = bufferBind->buffer; + entries[offset].size = WGPU_WHOLE_SIZE; + entries[offset].binding = offset; + offset++; + } + + desc.entryCount = offset; + desc.layout = cmdBuf->boundComputePipeline->bindGroupLayouts.samplerStorageBindGroupLayout; + break; + } + case WEBGPU_BINDGROUP_COMPUTEREADWRITESTORAGE: + { + for (int i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i++) { + WebGPUTextureView *textureBind = cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i]; + if (textureBind == NULL) { + continue; + } + + entries[offset].textureView = textureBind->view; + entries[offset].binding = offset; + offset++; + } + + for (int i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i++) { + WebGPUBuffer *bufferBind = cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i]; + if (bufferBind == NULL) { + continue; + } + + entries[offset].buffer = bufferBind->buffer; + entries[offset].size = WGPU_WHOLE_SIZE; + entries[offset].binding = offset; + offset++; + } + + desc.entryCount = offset; + desc.layout = cmdBuf->boundComputePipeline->bindGroupLayouts.readWriteStorageBindGroupLayout; + break; + } + case WEBGPU_BINDGROUP_COMPUTEUNIFORMS: + SDL_assert_release(!"Attempting to create compute uniform bind group! They're currently only pre-generated!"); + break; + } + + desc.entries = entries; + desc.label = (WGPUStringView){ NULL, 0 }; + desc.nextInChain = NULL; + + bindGroup = wgpuDeviceCreateBindGroup(cmdBuf->device, &desc); + + if (bindGroup == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create bind group!"); + } + + result = SDL_calloc(1, sizeof(*result)); + + result->bindGroup = bindGroup; + result->key = key; + result->invalid = false; + + SDL_InsertIntoHashTable(cmdBuf->renderer->bindGroupHashTable, &result->key, result, true); + + return result; +} + +// Get a bind group for the currently queued bind resources. +// Will create one if no bind group already exists. +static WGPUBindGroup WEBGPU_INTERNAL_GetBindGroup(WebGPUCommandBuffer *cmdBuf, WebGPUBindGroupType type) +{ + if (type == WEBGPU_BINDGROUP_VERTEXUNIFORMS) { + return cmdBuf->renderer->uniformBufferBindGroups[0]; + } else if (type == WEBGPU_BINDGROUP_FRAGMENTUNIFORMS) { + return cmdBuf->renderer->uniformBufferBindGroups[1]; + } else if (type == WEBGPU_BINDGROUP_COMPUTEUNIFORMS) { + return cmdBuf->renderer->uniformBufferBindGroups[2]; + } + + WebGPUBindGroupCacheKey key = WEBGPU_INTERNAL_GenerateKeyForCurrentBinds(cmdBuf, type); + WebGPUBindGroup *result = WEBGPU_INTERNAL_GetBindGroupFromIdentifier(cmdBuf->renderer, key); + + if (result == NULL) { + result = WEBGPU_INTERNAL_CreateBindGroup(cmdBuf, type, key); + } + + SDL_SetAtomicInt(&result->lastUsedAtSubmission, SDL_GetAtomicInt(&cmdBuf->renderer->numSubmissions) + 1); + + return result->bindGroup; +} + +static WebGPUTextureView *WEBGPU_INTERNAL_CreateTextureView(WebGPURenderer *renderer, WebGPUTexture *texture, int depth, int mipLevel) +{ + WebGPUTextureView *result = NULL; + + WGPUTextureFormat format = wgpuTextureGetFormat(texture->texture); + WGPUTextureUsage usages = wgpuTextureGetUsage(texture->texture); + WGPUTextureViewDimension dimension = WGPUTextureViewDimension_Undefined; + + Uint32 mipLevelCount = wgpuTextureGetMipLevelCount(texture->texture); + Uint32 arrayLayerCount = wgpuTextureGetDepthOrArrayLayers(texture->texture); + + switch (texture->type) { + case SDL_GPU_TEXTURETYPE_2D: + dimension = WGPUTextureViewDimension_2D; + break; + case SDL_GPU_TEXTURETYPE_2D_ARRAY: + dimension = WGPUTextureViewDimension_2DArray; + break; + case SDL_GPU_TEXTURETYPE_3D: + dimension = WGPUTextureViewDimension_3D; + break; + case SDL_GPU_TEXTURETYPE_CUBE: + // if we're only making a texture view slice, we can't use the cube argument + dimension = depth < 0 ? WGPUTextureViewDimension_Cube : WGPUTextureViewDimension_2D; + break; + case SDL_GPU_TEXTURETYPE_CUBE_ARRAY: + dimension = depth < 0 ? WGPUTextureViewDimension_CubeArray : WGPUTextureViewDimension_2D; + break; + } + + WGPUTextureViewDescriptor desc = { + .aspect = texture->aspect, + .dimension = dimension, + .format = format, + .label = { 0 }, + .usage = usages, + }; + + if (depth < 0) { + // create whole texture view + desc.arrayLayerCount = dimension == WGPUTextureViewDimension_2D || dimension == WGPUTextureViewDimension_3D ? 1 : arrayLayerCount; + desc.baseArrayLayer = 0; + desc.mipLevelCount = mipLevelCount; + desc.baseMipLevel = 0; + } else { + desc.arrayLayerCount = 1; + desc.baseArrayLayer = depth; + desc.mipLevelCount = 1; + desc.baseMipLevel = mipLevel; + } + + result = SDL_calloc(1, sizeof(*result)); + + result->identifier = SDL_AtomicIncRef(&renderer->nextBindableResourceID); + result->view = wgpuTextureCreateView(texture->texture, &desc); + result->parent = texture; + + return result; +} + +// Forward decl +static bool WEBGPU_SupportsTextureFormat(SDL_GPURenderer *driverData, SDL_GPUTextureFormat format, SDL_GPUTextureType type, SDL_GPUTextureUsageFlags usage); +static WebGPUTexture *WEBGPU_INTERNAL_CreateTexture(WebGPURenderer *renderer, const SDL_GPUTextureCreateInfo *createInfo) +{ + WebGPUTexture *texture; + + texture = (WebGPUTexture *)SDL_calloc(1, sizeof(*texture)); + WGPUTextureDescriptor desc = { 0 }; + + if (renderer->debugMode) { + if (!WEBGPU_SupportsTextureFormat((SDL_GPURenderer *)renderer, createInfo->format, createInfo->type, createInfo->usage)) { + SDL_assert_release(!"Texture format is not supported!"); + } + } + + switch (createInfo->type) { + case SDL_GPU_TEXTURETYPE_2D: + case SDL_GPU_TEXTURETYPE_2D_ARRAY: + case SDL_GPU_TEXTURETYPE_CUBE: + case SDL_GPU_TEXTURETYPE_CUBE_ARRAY: + desc.dimension = WGPUTextureDimension_2D; + break; + + case SDL_GPU_TEXTURETYPE_3D: + desc.dimension = WGPUTextureDimension_3D; + break; + } + + if (createInfo->usage & SDL_GPU_TEXTUREUSAGE_SAMPLER) { + desc.usage |= WGPUTextureUsage_TextureBinding; + } + if (createInfo->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) { + desc.usage |= WGPUTextureUsage_RenderAttachment; + } + if (createInfo->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) { + desc.usage |= WGPUTextureUsage_RenderAttachment; + } + if (createInfo->usage & SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ || createInfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ) { + desc.usage |= WGPUTextureUsage_StorageBinding; + desc.usage |= WGPUTextureUsage_TextureBinding; + } + if (createInfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE || createInfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) { + desc.usage |= WGPUTextureUsage_StorageBinding; + desc.usage |= WGPUTextureUsage_TextureBinding; + } + + desc.usage |= WGPUTextureUsage_CopyDst; + desc.usage |= WGPUTextureUsage_CopySrc; + + const char *debugName = SDL_GetStringProperty(createInfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, NULL); + + Uint32 blockWidth = Texture_GetBlockWidth(createInfo->format); + Uint32 blockHeight = Texture_GetBlockHeight(createInfo->format); + + desc.label = WEBGPU_INTERNAL_CharPtrToWebGPUStringView(debugName); + desc.format = SDLToWebGPU_TextureFormat[createInfo->format]; + desc.mipLevelCount = createInfo->num_levels; + desc.size = (WGPUExtent3D){ + .width = ALIGN_VALUE(createInfo->width, blockWidth), + .height = ALIGN_VALUE(createInfo->height, blockHeight), + .depthOrArrayLayers = createInfo->type == SDL_GPU_TEXTURETYPE_2D ? 1 : createInfo->layer_count_or_depth + }; + + switch (createInfo->sample_count) { + case SDL_GPU_SAMPLECOUNT_1: + desc.sampleCount = 1; + break; + case SDL_GPU_SAMPLECOUNT_2: + case SDL_GPU_SAMPLECOUNT_8: + case SDL_GPU_SAMPLECOUNT_4: + desc.sampleCount = 4; + break; + } + + texture->type = createInfo->type; + texture->format = createInfo->format; + texture->usage = createInfo->usage; + texture->sampleCount = createInfo->sample_count; + SDL_SetAtomicInt(&texture->referenceCount, 0); + texture->texture = wgpuDeviceCreateTexture(renderer->device, &desc); + // -1 signals that we want a texture view for all of the layers + texture->fullTextureView = WEBGPU_INTERNAL_CreateTextureView(renderer, texture, -1, 0); + + if (createInfo->type == SDL_GPU_TEXTURETYPE_3D) { + // manually create view + WebGPUTextureView *textureView = WEBGPU_INTERNAL_CreateTextureView(renderer, texture, 0, 0); + WEBGPU_INTERNAL_InsertElementIntoArray(texture->textureViews, texture->textureViewCapacity, + texture->textureViewCount, WebGPUTextureView *, textureView); + } else { + for (int i = 0; i < desc.size.depthOrArrayLayers; i++) { + for (int j = 0; j < desc.mipLevelCount; j++) { + WebGPUTextureView *textureView = WEBGPU_INTERNAL_CreateTextureView(renderer, texture, i, j); + + WEBGPU_INTERNAL_InsertElementIntoArray(texture->textureViews, texture->textureViewCapacity, + texture->textureViewCount, WebGPUTextureView *, textureView); + } + } + } + + return texture; +} + +static SDL_GPUTexture *WEBGPU_CreateTexture( + SDL_GPURenderer *driverData, + const SDL_GPUTextureCreateInfo *createInfo) +{ + WebGPURenderer *renderer = (WebGPURenderer *)driverData; + WebGPUTexture *texture; + WebGPUTextureContainer *container; + + texture = WEBGPU_INTERNAL_CreateTexture( + renderer, + createInfo); + + if (texture == NULL) { + return NULL; + } + + container = SDL_malloc(sizeof(WebGPUTextureContainer)); + + container->header.info = *createInfo; + container->header.info.props = SDL_CreateProperties(); + if (createInfo->props) { + SDL_CopyProperties(createInfo->props, container->header.info.props); + } + + container->activeTexture = texture; + container->textureCapacity = 1; + container->textureCount = 1; + container->textures = SDL_malloc( + container->textureCapacity * sizeof(WebGPUTexture *)); + container->textures[0] = container->activeTexture; + + texture->container = container; + + return (SDL_GPUTexture *)container; +} + +static WebGPUSampler *WEBGPU_INTERNAL_CreateSampler(WebGPURenderer *renderer, const SDL_GPUSamplerCreateInfo *createInfo) +{ + WebGPUSampler *sampler; + sampler = SDL_calloc(1, sizeof(*sampler)); + + WGPUSamplerDescriptor desc = { 0 }; + WGPUSamplerDescriptor descFallback = { 0 }; + const char *debugName = SDL_GetStringProperty(createInfo->props, SDL_PROP_GPU_SAMPLER_CREATE_NAME_STRING, NULL); + + desc = (WGPUSamplerDescriptor){ + .addressModeU = SDLToWebGPU_AddressMode[createInfo->address_mode_u], + .addressModeV = SDLToWebGPU_AddressMode[createInfo->address_mode_v], + .addressModeW = SDLToWebGPU_AddressMode[createInfo->address_mode_w], + .compare = createInfo->enable_compare ? SDLToWebGPU_CompareFunc[createInfo->compare_op] : WGPUCompareFunction_Undefined, + .label = WEBGPU_INTERNAL_CharPtrToWebGPUStringView(debugName), + .lodMaxClamp = createInfo->max_lod, + .lodMinClamp = createInfo->min_lod < 0 ? 0 : createInfo->min_lod, + .magFilter = SDLToWebGPU_FilterMode[createInfo->mag_filter], + .minFilter = SDLToWebGPU_FilterMode[createInfo->min_filter], + .mipmapFilter = SDLToWebGPU_MipmapFilterMode[createInfo->mipmap_mode], + // Again, gross float -> int cast, blame WebGPU. + .maxAnisotropy = (createInfo->enable_anisotropy && (createInfo->max_anisotropy >= 1)) ? (Uint16)createInfo->max_anisotropy : 1, + }; + + // HACK:: WebGPU doesn't support filtering on some texture formats. (looking at you, depth textures) + // As a workaround for this, the backend automatically disables filtering on unsupported textures. + // + // This works great, unless you try to filter an unfilterable texture! + // (note: you really shouldn't, it has very lackluster support even on Vulkan but alas) + // As you could probably guess, using a filtering sampler with an unfilterable texture... doesn't go well. + // + // Therefore, we have to create a second "fallback" sampler for those cases. + // This sampler always uses nearest-neighbor sampling and doesn't support any form of anisotropy. + // It looks like hot garbage, but it works™. Screw this API, it sucks. + descFallback = desc; + descFallback.maxAnisotropy = 1; + descFallback.magFilter = WGPUFilterMode_Nearest; // you'll get your filtering back once you learn to behave + descFallback.minFilter = WGPUFilterMode_Nearest; + descFallback.mipmapFilter = WGPUMipmapFilterMode_Nearest; + + sampler->sampler = wgpuDeviceCreateSampler(renderer->device, &desc); + sampler->fallbackSampler = wgpuDeviceCreateSampler(renderer->device, &descFallback); + sampler->identifier = SDL_GetAtomicInt(&renderer->nextBindableResourceID); + + SDL_AtomicIncRef(&renderer->nextBindableResourceID); + + return sampler; +} + +static SDL_GPUSampler *WEBGPU_CreateSampler(SDL_GPURenderer *device, const SDL_GPUSamplerCreateInfo *createInfo) +{ + return (SDL_GPUSampler *)WEBGPU_INTERNAL_CreateSampler((WebGPURenderer *)device, createInfo); +} + +static void WEBGPU_INTERNAL_ReleaseSampler(WebGPURenderer *renderer, WebGPUSampler *sampler) +{ + wgpuSamplerRelease(sampler->sampler); + wgpuSamplerRelease(sampler->fallbackSampler); + SDL_free(sampler); +} + +static void WEBGPU_ReleaseSampler(SDL_GPURenderer *device, SDL_GPUSampler *sampler) +{ + WEBGPU_INTERNAL_QueueSamplerForRelease((WebGPURenderer *)device, (WebGPUSampler *)sampler); +} + +// forward decl +static bool WEBGPU_SupportsSwapchainComposition(SDL_GPURenderer *driverData, SDL_Window *window, SDL_GPUSwapchainComposition swapchainComposition); +static SDL_GPUTextureFormat WEBGPU_GetSwapchainTextureFormat(SDL_GPURenderer *device, SDL_Window *window); + +static WGPUSurfaceConfiguration WEBGPU_INTERNAL_GetSurfaceConfigurationForWindow(WebGPURenderer *renderer, WebGPUWindowData *windowData) +{ + int windowWidth = 0; + int windowHeight = 0; + + SDL_GetWindowSizeInPixels(windowData->window, &windowWidth, &windowHeight); + + WGPUSurfaceConfiguration config = (WGPUSurfaceConfiguration){ + .alphaMode = WGPUCompositeAlphaMode_Opaque, // SDLGPU does not support transparent windows. (Yet.) + .presentMode = SDLToWebGPU_PresentMode[windowData->presentMode], + .format = windowData->shouldUseFallbackFormat ? SwapchainCompositionToFallbackFormat[windowData->swapchainComposition] + : SwapchainCompositionToFormat[windowData->swapchainComposition], + .width = windowWidth, + .height = windowHeight, + .usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopyDst, + .device = renderer->device, + .nextInChain = NULL, + }; + + if (windowWidth != windowData->surfaceWidth || windowHeight != windowData->surfaceHeight) { + // NOTE: + // I thought this made the WebGPU surface suboptimal, but turns out no it doesn't! + // I assume that we'd want to have the swapchain texture's size be the same as the window's size, + // so I made this mark the surface as "dirty" if the window dimensions don't match. + windowData->surfaceDirty = true; + } + + return config; +} + +static void WEBGPU_INTERNAL_ConfigureSurfaceIfNeeded(WebGPURenderer *renderer, WebGPUWindowData *windowData) +{ + WGPUSurfaceConfiguration config = WEBGPU_INTERNAL_GetSurfaceConfigurationForWindow(renderer, windowData); + + if (!windowData->surfaceDirty) { + return; + } + + wgpuSurfaceConfigure(windowData->surface, &config); + windowData->surfaceWidth = config.width; + windowData->surfaceHeight = config.height; + windowData->surfaceDirty = false; +} + +static bool WEBGPU_ClaimWindow(SDL_GPURenderer *renderer, SDL_Window *window) +{ + SDL_PropertiesID props = SDL_GetWindowProperties(window); + WebGPUWindowData *windowData = NULL; + + if (!props) { + SDL_SetError("Could not get window properties!"); + return false; + } + + windowData = SDL_calloc(1, sizeof(*windowData)); + + windowData->window = window; + windowData->presentMode = SDL_GPU_PRESENTMODE_VSYNC; // default + windowData->swapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // default + windowData->surfaceDirty = true; + + if (!SDL_WGPU_CreateSurface(window, ((WebGPURenderer *)renderer)->instance, &windowData->surface)) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Could not create WebGPU surface: %s", SDL_GetError()); + SDL_free(windowData); + + return false; + } + + SDL_SetPointerProperty(props, WINDOW_PROPERTY_DATA, windowData); + + // We'll verify that the surface supports SDR surface composition. + // This is also the only function that checks to see if a fallback format is needed. + // Bit jank since the function expects the window data to be assigned to the window but meh + if (!WEBGPU_SupportsSwapchainComposition(renderer, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR)) { + SDL_SetError("Failed to configure surface for SDR!"); + + wgpuSurfaceRelease(windowData->surface); + + SDL_SetPointerProperty(props, WINDOW_PROPERTY_DATA, NULL); + SDL_free(windowData); + + return false; + } + + // We'll configure the surface right away since creating a surface doesn't + // automatically configure it in WebGPU. (I think. Don't quote me on that.) + WEBGPU_INTERNAL_ConfigureSurfaceIfNeeded((WebGPURenderer *)renderer, windowData); + return true; +} + +static bool WEBGPU_AcquireSwapchainTexture(SDL_GPUCommandBuffer *commandBuffer, SDL_Window *window, SDL_GPUTexture **swapchainTexture, Uint32 *width, Uint32 *height) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WebGPUWindowData *windowData = SDL_GetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, NULL); + + WebGPUTexture *texture = NULL; + WebGPUTextureContainer *container = NULL; + + WGPUSurfaceTexture surfaceTexture = { 0 }; + + // HACK: + // We have to handle pending destroys here since it's also the function + // responsible for querying submitted command buffers. + // I should really remake the "fence" system. It sucks so much. + WEBGPU_INTERNAL_HandlePendingDestroys(cmdBuf->renderer); + + if (!windowData) { + SDL_SetError("WindowData is NULL! Did you call SDL_ClaimWindowForGPUDevice?"); + goto fail; + } + + if (cmdBuf->renderer->submittedCommandBufferCount >= cmdBuf->renderer->maxFramesInFlight) { + if (width) { + *width = 0; + } + if (height) { + *height = 0; + } + *swapchainTexture = NULL; + + return true; + } + + WEBGPU_INTERNAL_ConfigureSurfaceIfNeeded(cmdBuf->renderer, windowData); + wgpuSurfaceGetCurrentTexture(windowData->surface, &surfaceTexture); + + switch (surfaceTexture.status) { + case WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal: + break; + case WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal: + // We'll reconfigure the surface the next time AcquireSwapchainTexture is called. + windowData->surfaceDirty = true; + break; + case WGPUSurfaceGetCurrentTextureStatus_Timeout: + case WGPUSurfaceGetCurrentTextureStatus_Outdated: + case WGPUSurfaceGetCurrentTextureStatus_Lost: + case WGPUSurfaceGetCurrentTextureStatus_Error: + case WGPUSurfaceGetCurrentTextureStatus_Force32: + SDL_SetError("Failed to acquire swapchain texture!"); + goto fail; + } + + texture = SDL_calloc(1, sizeof(WebGPUTexture)); + container = SDL_calloc(1, sizeof(*container)); + + texture->container = container; + texture->texture = surfaceTexture.texture; + texture->aspect = WGPUTextureAspect_All; + texture->format = windowData->shouldUseFallbackFormat ? SwapchainCompositionToFallbackSDLFormat[windowData->swapchainComposition] + : SwapchainCompositionToSDLFormat[windowData->swapchainComposition]; + texture->type = SDL_GPU_TEXTURETYPE_2D; + texture->usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET; + texture->fullTextureView = WEBGPU_INTERNAL_CreateTextureView(cmdBuf->renderer, texture, -1, 0); + + SDL_SetAtomicInt(&texture->referenceCount, 0); + + WebGPUTextureView *textureView = WEBGPU_INTERNAL_CreateTextureView(cmdBuf->renderer, texture, 0, 0); + WEBGPU_INTERNAL_InsertElementIntoArray(texture->textureViews, texture->textureViewCapacity, texture->textureViewCount, WebGPUTextureView *, textureView); + WEBGPU_INTERNAL_InsertElementIntoArray(container->textures, container->textureCapacity, container->textureCount, WebGPUTexture *, texture); + + container->activeTexture = texture; + container->header.info.width = wgpuTextureGetWidth(texture->texture); + container->header.info.height = wgpuTextureGetHeight(texture->texture); + container->header.info.format = windowData->shouldUseFallbackFormat ? SwapchainCompositionToFallbackSDLFormat[windowData->swapchainComposition] + : SwapchainCompositionToSDLFormat[windowData->swapchainComposition]; + container->header.info.layer_count_or_depth = 1; + container->header.info.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET; + container->header.info.num_levels = 1; + container->header.info.props = 0; + + WEBGPU_INTERNAL_InsertElementIntoArray(cmdBuf->surfaces, cmdBuf->surfaceCapacity, cmdBuf->surfaceCount, WGPUSurface, windowData->surface); + WEBGPU_INTERNAL_InsertElementIntoArray(cmdBuf->acquiredSwapchainTextures, cmdBuf->swapchainTextureCapacity, + cmdBuf->swapchainTextureCount, WebGPUTextureContainer *, container); + + *swapchainTexture = (SDL_GPUTexture *)container; + + if (width) { + *width = wgpuTextureGetWidth(texture->texture); + } + if (height) { + *height = wgpuTextureGetHeight(texture->texture); + } + + return true; +fail: + if (width) { + *width = 0; + } + if (height) { + *height = 0; + } + *swapchainTexture = NULL; + + return false; +} + +static SDL_GPUShader *WEBGPU_CreateShader( + SDL_GPURenderer *driverData, + const SDL_GPUShaderCreateInfo *createinfo) +{ + WGPUShaderModuleDescriptor desc; + + WebGPUShader *shader; + shader = SDL_calloc(1, sizeof(*shader)); + + const char *debugName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_SHADER_CREATE_NAME_STRING, NULL); + desc.label = WEBGPU_INTERNAL_CharPtrToWebGPUStringView(debugName); + + WGPUShaderSourceWGSL source; + + source.code = (WGPUStringView){ .data = (char *)createinfo->code, .length = createinfo->code_size }; + source.chain.next = NULL; + source.chain.sType = WGPUSType_ShaderSourceWGSL; + + desc.nextInChain = &source.chain; + shader->shader = wgpuDeviceCreateShaderModule(((WebGPURenderer *)driverData)->device, &desc); + + shader->entrypoint = SDL_strdup(createinfo->entrypoint); + + return (SDL_GPUShader *)shader; +} + +static void WEBGPU_INTERNAL_ReleaseShader(WebGPUShader *shader) +{ + if (SDL_GetAtomicInt(&shader->referenceCount) == 0) { + wgpuShaderModuleRelease(shader->shader); + + SDL_free(shader->entrypoint); + SDL_free(shader); + } else { + shader->shouldFree = true; + } +} + +static void WEBGPU_ReleaseShader(SDL_GPURenderer *driverData, SDL_GPUShader *shader) +{ + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)shader); +} + +static void WEBGPU_INTERNAL_EnsureGraphicsPipelineExists(WebGPURenderer *renderer, WebGPUGraphicsPipeline *pipeline) +{ + if (pipeline->hasBeenCreated) { + return; + } + + WGPURenderPipelineDescriptor pipelineDesc; + + WebGPUShader *vertexShader = (WebGPUShader *)pipeline->createInfo.vertex_shader; + WebGPUShader *fragmentShader = (WebGPUShader *)pipeline->createInfo.fragment_shader; + + WGPUDepthStencilState depthStencilState = { + // yucky wucky + .depthBias = (Uint32)pipeline->createInfo.rasterizer_state.depth_bias_constant_factor, + .depthBiasClamp = pipeline->createInfo.rasterizer_state.depth_bias_clamp, + .depthBiasSlopeScale = pipeline->createInfo.rasterizer_state.depth_bias_slope_factor, + .depthCompare = SDLToWebGPU_CompareFunc[pipeline->createInfo.depth_stencil_state.compare_op], + .depthWriteEnabled = pipeline->createInfo.depth_stencil_state.enable_depth_write, + .stencilBack = pipeline->createInfo.depth_stencil_state.enable_stencil_test ? (WGPUStencilFaceState){ + .compare = SDLToWebGPU_CompareFunc[pipeline->createInfo.depth_stencil_state.back_stencil_state.compare_op], + .depthFailOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.back_stencil_state.depth_fail_op], + .failOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.back_stencil_state.fail_op], + .passOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.back_stencil_state.pass_op], + } + : (WGPUStencilFaceState){ 0 }, + .stencilFront = pipeline->createInfo.depth_stencil_state.enable_stencil_test ? (WGPUStencilFaceState){ + .compare = SDLToWebGPU_CompareFunc[pipeline->createInfo.depth_stencil_state.front_stencil_state.compare_op], + .depthFailOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.front_stencil_state.depth_fail_op], + .failOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.front_stencil_state.fail_op], + .passOp = SDLToWebGPU_StencilOp[pipeline->createInfo.depth_stencil_state.front_stencil_state.pass_op], + } + : (WGPUStencilFaceState){ 0 }, + .stencilReadMask = pipeline->createInfo.depth_stencil_state.enable_stencil_test ? pipeline->createInfo.depth_stencil_state.compare_mask : 0, + .stencilWriteMask = pipeline->createInfo.depth_stencil_state.enable_stencil_test ? pipeline->createInfo.depth_stencil_state.write_mask : 0, + .format = SDLToWebGPU_TextureFormat[pipeline->createInfo.target_info.depth_stencil_format], + .nextInChain = NULL, + }; + + WGPUColorTargetState *colorTargets = SDL_calloc(pipeline->createInfo.target_info.num_color_targets, sizeof(WGPUColorTargetState)); + WGPUBlendState *colorTargetBlendStates = SDL_calloc(pipeline->createInfo.target_info.num_color_targets, sizeof(WGPUBlendState)); + for (int i = 0; i < pipeline->createInfo.target_info.num_color_targets; i++) { + const SDL_GPUColorTargetDescription *targetDesc = &pipeline->createInfo.target_info.color_target_descriptions[i]; + + if (WEBGPU_INTERNAL_TextureFormatSupportsBlending(SDLToWebGPU_TextureFormat[targetDesc->format], renderer)) { + colorTargetBlendStates[i].color = (WGPUBlendComponent){ + .dstFactor = SDLToWebGPU_BlendFactor[targetDesc->blend_state.dst_color_blendfactor], + .srcFactor = SDLToWebGPU_BlendFactor[targetDesc->blend_state.src_color_blendfactor], + .operation = SDLToWebGPU_BlendOp[targetDesc->blend_state.color_blend_op], + }; + + colorTargetBlendStates[i].alpha = (WGPUBlendComponent){ + .dstFactor = SDLToWebGPU_BlendFactor[targetDesc->blend_state.dst_alpha_blendfactor], + .srcFactor = SDLToWebGPU_BlendFactor[targetDesc->blend_state.src_alpha_blendfactor], + .operation = SDLToWebGPU_BlendOp[targetDesc->blend_state.alpha_blend_op], + }; + + // WebGPU's strict about not allowing factors less than 1 for min & max so we'll set them here + if (colorTargetBlendStates[i].color.operation == WGPUBlendOperation_Min || colorTargetBlendStates[i].color.operation == WGPUBlendOperation_Max) { + colorTargetBlendStates[i].color.dstFactor = WGPUBlendFactor_One; + colorTargetBlendStates[i].color.srcFactor = WGPUBlendFactor_One; + } + + if (colorTargetBlendStates[i].alpha.operation == WGPUBlendOperation_Min || colorTargetBlendStates[i].alpha.operation == WGPUBlendOperation_Max) { + colorTargetBlendStates[i].alpha.dstFactor = WGPUBlendFactor_One; + colorTargetBlendStates[i].alpha.srcFactor = WGPUBlendFactor_One; + } + + colorTargets[i].blend = &colorTargetBlendStates[i]; + } else { + colorTargets[i].blend = NULL; + } + colorTargets[i].format = SDLToWebGPU_TextureFormat[targetDesc->format]; + colorTargets[i].nextInChain = NULL; + + if (targetDesc->blend_state.enable_color_write_mask) { + if (targetDesc->blend_state.color_write_mask & SDL_GPU_COLORCOMPONENT_A) { + colorTargets[i].writeMask |= WGPUColorWriteMask_Alpha; + } + if (targetDesc->blend_state.color_write_mask & SDL_GPU_COLORCOMPONENT_R) { + colorTargets[i].writeMask |= WGPUColorWriteMask_Red; + } + if (targetDesc->blend_state.color_write_mask & SDL_GPU_COLORCOMPONENT_G) { + colorTargets[i].writeMask |= WGPUColorWriteMask_Green; + } + if (targetDesc->blend_state.color_write_mask & SDL_GPU_COLORCOMPONENT_B) { + colorTargets[i].writeMask |= WGPUColorWriteMask_Blue; + } + } else { + colorTargets[i].writeMask = WGPUColorWriteMask_All; + } + } + + WGPUFragmentState fragmentState = { + .constantCount = 0, + .constants = NULL, + .targets = colorTargets, + .targetCount = pipeline->createInfo.target_info.num_color_targets, + .entryPoint = { fragmentShader->entrypoint, SDL_strlen(fragmentShader->entrypoint) }, + .module = fragmentShader->shader, + }; + + WGPUVertexBufferLayout *vertexBufferLayouts = SDL_calloc(pipeline->createInfo.vertex_input_state.num_vertex_buffers, sizeof(WGPUVertexBufferLayout)); + + for (int i = 0; i < pipeline->createInfo.vertex_input_state.num_vertex_buffers; i++) { + vertexBufferLayouts[i].stepMode = SDLToWebGPU_VertexInputRate[pipeline->createInfo.vertex_input_state.vertex_buffer_descriptions[i].input_rate]; + vertexBufferLayouts[i].arrayStride = pipeline->createInfo.vertex_input_state.vertex_buffer_descriptions[i].pitch; + vertexBufferLayouts[i].nextInChain = NULL; + + WGPUVertexAttribute *attributes = SDL_calloc(MAX_VERTEX_ATTRIBUTES, sizeof(WGPUVertexAttribute)); + Uint32 attributeCount = 0; + Uint32 currentOffset = 0; + + for (int j = 0; j < pipeline->createInfo.vertex_input_state.num_vertex_attributes; j++) { + const SDL_GPUVertexAttribute *attr = &pipeline->createInfo.vertex_input_state.vertex_attributes[j]; + + if (pipeline->createInfo.vertex_input_state.vertex_attributes[j].buffer_slot == i) { + attributes[currentOffset].offset = attr->offset; + attributes[currentOffset].format = SDLToWebGPU_VertexFormat[attr->format]; + attributes[currentOffset].nextInChain = NULL; + attributes[currentOffset].shaderLocation = attr->location; + attributeCount++; + currentOffset++; + } + } + + vertexBufferLayouts[i].attributeCount = attributeCount; + vertexBufferLayouts[i].attributes = attributes; + } + + WGPUVertexState vertexState = { + .bufferCount = pipeline->createInfo.vertex_input_state.num_vertex_buffers, + .buffers = vertexBufferLayouts, + .constantCount = 0, + .constants = NULL, + .entryPoint = { vertexShader->entrypoint, SDL_strlen(vertexShader->entrypoint) }, + .module = vertexShader->shader, + .nextInChain = NULL, + }; + + WGPUMultisampleState multisampleState = { + .alphaToCoverageEnabled = pipeline->createInfo.multisample_state.enable_alpha_to_coverage, + .mask = 0xFFFFFFFF, // Unimplemented in SDL_GPU + .nextInChain = NULL, + }; + + switch (pipeline->createInfo.multisample_state.sample_count) { + case SDL_GPU_SAMPLECOUNT_1: + multisampleState.count = 1; + break; + case SDL_GPU_SAMPLECOUNT_2: + case SDL_GPU_SAMPLECOUNT_8: + if (renderer->debugMode) { + SDL_LogDebug(SDL_LOG_CATEGORY_GPU, "WebGPU only supports 1x or 4x multisampling! Falling back to 4x."); + } + multisampleState.count = 4; + break; + case SDL_GPU_SAMPLECOUNT_4: + multisampleState.count = 4; + break; + } + + WGPUPrimitiveState primitiveState = { + .cullMode = SDLToWebGPU_CullMode[pipeline->createInfo.rasterizer_state.cull_mode], + .frontFace = SDLToWebGPU_FrontFace[pipeline->createInfo.rasterizer_state.front_face], + .stripIndexFormat = (pipeline->createInfo.primitive_type == SDL_GPU_PRIMITIVETYPE_TRIANGLESTRIP) ? WGPUIndexFormat_Uint32 : WGPUIndexFormat_Undefined, + .topology = SDLToWebGPU_PrimitiveType[pipeline->createInfo.primitive_type], + .unclippedDepth = !pipeline->createInfo.rasterizer_state.enable_depth_clip, + .nextInChain = NULL, + }; + + WGPUBindGroupLayout *bindGroupLayouts = SDL_calloc(4, sizeof(WGPUBindGroupLayout)); + bindGroupLayouts[0] = pipeline->vertexBindGroupLayouts.samplerStorageBindGroupLayout; + bindGroupLayouts[1] = pipeline->vertexBindGroupLayouts.uniformBindGroupLayout; + bindGroupLayouts[2] = pipeline->fragmentBindGroupLayouts.samplerStorageBindGroupLayout; + bindGroupLayouts[3] = pipeline->fragmentBindGroupLayouts.uniformBindGroupLayout; + + WGPUPipelineLayoutDescriptor pipelineLayoutDesc = { + .bindGroupLayoutCount = 4, + .bindGroupLayouts = bindGroupLayouts, + .immediateSize = 0, + .label = WGPU_STRING_VIEW_INIT, + .nextInChain = NULL, + }; + + WGPUPipelineLayout pipelineLayout = wgpuDeviceCreatePipelineLayout(renderer->device, &pipelineLayoutDesc); + const char *debugName = SDL_GetStringProperty(pipeline->createInfo.props, SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING, NULL); + + pipelineDesc.label = WEBGPU_INTERNAL_CharPtrToWebGPUStringView(debugName); + pipelineDesc.vertex = vertexState; + pipelineDesc.fragment = &fragmentState; + pipelineDesc.multisample = multisampleState; + pipelineDesc.depthStencil = pipeline->createInfo.target_info.has_depth_stencil_target ? &depthStencilState : NULL; + pipelineDesc.primitive = primitiveState; + pipelineDesc.layout = pipelineLayout; + pipelineDesc.nextInChain = NULL; + + pipeline->pipeline = wgpuDeviceCreateRenderPipeline(renderer->device, &pipelineDesc); + wgpuPipelineLayoutRelease(pipelineLayout); + + for (int i = 0; i < pipeline->createInfo.vertex_input_state.num_vertex_buffers; i++) { + SDL_free((void *)vertexBufferLayouts[i].attributes); + } + + SDL_free(colorTargetBlendStates); + + SDL_free(vertexBufferLayouts); + SDL_free(colorTargets); + + SDL_free(bindGroupLayouts); + + pipeline->hasBeenCreated = true; +} + +static SDL_GPUGraphicsPipeline *WEBGPU_CreateGraphicsPipeline(SDL_GPURenderer *driverData, const SDL_GPUGraphicsPipelineCreateInfo *createInfo) +{ + WebGPUGraphicsPipeline *pipeline; + pipeline = SDL_calloc(1, sizeof(*pipeline)); + + // Most of the time these pointers are given to memory in the stack, + // so we'll have to allocate heap memory and copy it over or else shit'll blow up + SDL_GPUColorTargetDescription *targets = NULL; + SDL_GPUVertexAttribute *vertexAttributes = NULL; + SDL_GPUVertexBufferDescription *vertexBufferDescriptions = NULL; + SDL_PropertiesID props = SDL_CreateProperties(); + + SDL_CopyProperties(createInfo->props, props); + + targets = SDL_calloc(createInfo->target_info.num_color_targets, sizeof(*targets)); + vertexAttributes = SDL_calloc(createInfo->vertex_input_state.num_vertex_attributes, sizeof(*vertexAttributes)); + vertexBufferDescriptions = SDL_calloc(createInfo->vertex_input_state.num_vertex_buffers, sizeof(*vertexBufferDescriptions)); + + for (int i = 0; i < createInfo->vertex_input_state.num_vertex_attributes; i++) { + vertexAttributes[i] = createInfo->vertex_input_state.vertex_attributes[i]; + } + + for (int i = 0; i < createInfo->vertex_input_state.num_vertex_buffers; i++) { + vertexBufferDescriptions[i] = createInfo->vertex_input_state.vertex_buffer_descriptions[i]; + } + + for (int i = 0; i < createInfo->target_info.num_color_targets; i++) { + targets[i] = createInfo->target_info.color_target_descriptions[i]; + } + + pipeline->createInfo = (SDL_GPUGraphicsPipelineCreateInfo){ + .vertex_shader = createInfo->vertex_shader, + .fragment_shader = createInfo->fragment_shader, + .vertex_input_state = { + .vertex_attributes = vertexAttributes, + .vertex_buffer_descriptions = vertexBufferDescriptions, + .num_vertex_buffers = createInfo->vertex_input_state.num_vertex_buffers, + .num_vertex_attributes = createInfo->vertex_input_state.num_vertex_attributes, + }, + .primitive_type = createInfo->primitive_type, + .rasterizer_state = createInfo->rasterizer_state, + .multisample_state = createInfo->multisample_state, + .depth_stencil_state = createInfo->depth_stencil_state, + .target_info = { + .color_target_descriptions = targets, + .num_color_targets = createInfo->target_info.num_color_targets, + .has_depth_stencil_target = createInfo->target_info.has_depth_stencil_target, + .depth_stencil_format = createInfo->target_info.depth_stencil_format, + }, + .props = props, + }; + + SDL_AtomicIncRef(&((WebGPUShader *)pipeline->createInfo.vertex_shader)->referenceCount); + SDL_AtomicIncRef(&((WebGPUShader *)pipeline->createInfo.fragment_shader)->referenceCount); + + return (SDL_GPUGraphicsPipeline *)pipeline; +} + +static void WEBGPU_ReleaseGraphicsPipeline(SDL_GPURenderer *device, SDL_GPUGraphicsPipeline *_pipeline) +{ + WebGPUGraphicsPipeline *pipeline = (WebGPUGraphicsPipeline *)_pipeline; + + if (pipeline->hasBeenCreated) { + wgpuRenderPipelineRelease(pipeline->pipeline); + + wgpuBindGroupLayoutRelease(pipeline->vertexBindGroupLayouts.samplerStorageBindGroupLayout); + wgpuBindGroupLayoutRelease(pipeline->fragmentBindGroupLayouts.samplerStorageBindGroupLayout); + } + + // We only free these when the pipeline's released, since it might be recreated. + + (void)SDL_AtomicDecRef(&((WebGPUShader *)pipeline->createInfo.vertex_shader)->referenceCount); + (void)SDL_AtomicDecRef(&((WebGPUShader *)pipeline->createInfo.fragment_shader)->referenceCount); + + if (((WebGPUShader *)pipeline->createInfo.vertex_shader)->shouldFree) { + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)pipeline->createInfo.vertex_shader); + } + if (((WebGPUShader *)pipeline->createInfo.fragment_shader)->shouldFree) { + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)pipeline->createInfo.fragment_shader); + } + + SDL_free((SDL_GPUColorTargetDescription *)pipeline->createInfo.target_info.color_target_descriptions); + SDL_free((SDL_GPUVertexAttribute *)pipeline->createInfo.vertex_input_state.vertex_attributes); + SDL_free((SDL_GPUVertexBufferDescription *)pipeline->createInfo.vertex_input_state.vertex_buffer_descriptions); + SDL_DestroyProperties(pipeline->createInfo.props); + + SDL_free(pipeline); +} + +static void WEBGPU_INTERNAL_EnsureComputePipelineExists(WebGPURenderer *renderer, WebGPUComputePipeline *pipeline) +{ + if (pipeline->hasBeenCreated) { + return; + } + + WGPUComputePipelineDescriptor pipelineDesc; + + WGPUPipelineLayout pipelineLayout; + WGPUPipelineLayoutDescriptor pipelineLayoutDesc; + WGPUBindGroupLayout *pipelineLayoutEntries = NULL; + + WGPUShaderModuleDescriptor shaderDesc; + WGPUShaderSourceWGSL shaderSource; + WGPUShaderModule shader; + + const char *pipelineDebugName = SDL_GetStringProperty(pipeline->createInfo.props, SDL_PROP_GPU_COMPUTEPIPELINE_CREATE_NAME_STRING, NULL); + + shaderSource.chain.next = NULL; + shaderSource.chain.sType = WGPUSType_ShaderSourceWGSL; + shaderSource.code = (WGPUStringView){ (char *)pipeline->createInfo.code, pipeline->createInfo.code_size }; + + shaderDesc.label = (WGPUStringView){ NULL, 0 }; + shaderDesc.nextInChain = &shaderSource.chain; + + shader = wgpuDeviceCreateShaderModule(renderer->device, &shaderDesc); + + if (!shader) { + SDL_assert_release(!"Failed to create shader module for compute pipeline!"); + return; + } + + pipelineLayoutEntries = SDL_calloc(3, sizeof(*pipelineLayoutEntries)); + pipelineLayoutEntries[0] = pipeline->bindGroupLayouts.samplerStorageBindGroupLayout; + pipelineLayoutEntries[1] = pipeline->bindGroupLayouts.readWriteStorageBindGroupLayout; + pipelineLayoutEntries[2] = pipeline->bindGroupLayouts.uniformBindGroupLayout; + + pipelineLayoutDesc.bindGroupLayoutCount = 3; + pipelineLayoutDesc.immediateSize = 0; + pipelineLayoutDesc.label = (WGPUStringView){ NULL, 0 }; + pipelineLayoutDesc.nextInChain = NULL; + pipelineLayoutDesc.bindGroupLayouts = pipelineLayoutEntries; + + pipelineLayout = wgpuDeviceCreatePipelineLayout(renderer->device, &pipelineLayoutDesc); + + if (!pipelineLayout) { + SDL_assert_release(!"Failed to create pipeline layout for compute pipeline!"); + } + + pipelineDesc.compute = (WGPUComputeState){ + .constantCount = 0, + .constants = NULL, + .entryPoint = (WGPUStringView){ pipeline->createInfo.entrypoint, SDL_strlen(pipeline->createInfo.entrypoint) }, + .module = shader, + .nextInChain = NULL, + }; + pipelineDesc.label = pipelineDebugName != NULL ? (WGPUStringView){ pipelineDebugName, SDL_strlen(pipelineDebugName) } : (WGPUStringView){ NULL, 0 }; + pipelineDesc.layout = pipelineLayout; + pipelineDesc.nextInChain = NULL; + + pipeline->pipeline = wgpuDeviceCreateComputePipeline(renderer->device, &pipelineDesc); + + SDL_free(pipelineLayoutEntries); + + wgpuShaderModuleRelease(shader); + wgpuPipelineLayoutRelease(pipelineLayout); + + if (pipeline->pipeline == NULL) { + SDL_assert_release(!"Failed to create compute pipeline!"); + } + + pipeline->hasBeenCreated = true; +} + +static SDL_GPUComputePipeline *WEBGPU_CreateComputePipeline(SDL_GPURenderer *device, const SDL_GPUComputePipelineCreateInfo *createInfo) +{ + WebGPUComputePipeline *pipeline = NULL; + pipeline = SDL_calloc(1, sizeof(*pipeline)); + + // Like with the render pipeline, we can't be sure that the pointers we've been given in createInfo won't be freed before pipeline creation. + // So, we'll copy their data and hopefully remember to free them when we're done :P + + char *code = NULL; + char *entrypoint = NULL; + SDL_PropertiesID props = SDL_CreateProperties(); + + code = SDL_strdup((char *)createInfo->code); + entrypoint = SDL_strdup((char *)createInfo->entrypoint); + + SDL_CopyProperties(createInfo->props, props); + + pipeline->createInfo = *createInfo; + pipeline->createInfo.code = (Uint8 *)code; + pipeline->createInfo.entrypoint = entrypoint; + pipeline->createInfo.props = props; + + return (SDL_GPUComputePipeline *)pipeline; +} + +static void WEBGPU_ReleaseComputePipeline(SDL_GPURenderer *driverData, SDL_GPUComputePipeline *computePipeline) +{ + WebGPUComputePipeline *pipeline = (WebGPUComputePipeline *)computePipeline; + + if (pipeline->hasBeenCreated) { + wgpuComputePipelineRelease(pipeline->pipeline); + + wgpuBindGroupLayoutRelease(pipeline->bindGroupLayouts.readWriteStorageBindGroupLayout); + wgpuBindGroupLayoutRelease(pipeline->bindGroupLayouts.samplerStorageBindGroupLayout); + } + + // const pointers aren't real wake up sheeple + SDL_free((char *)pipeline->createInfo.code); + SDL_free((char *)pipeline->createInfo.entrypoint); + SDL_DestroyProperties(pipeline->createInfo.props); + + SDL_free(computePipeline); +} + +static void WEBGPU_ReleaseFence(SDL_GPURenderer *device, SDL_GPUFence *fence) +{ + WEBGPU_INTERNAL_QueueFenceForRelease((WebGPURenderer *)device, (WebGPUFence *)fence); +} + +// Should out to klukaszek for writing the blit code because wow I hate blitting +static void WEBGPU_INTERNAL_InitBlitResources( + WebGPURenderer *renderer) +{ + SDL_GPUShaderCreateInfo shaderCreateInfo; + + renderer->blitPipelineCapacity = 2; + renderer->blitPipelineCount = 0; + renderer->blitPipelines = (BlitPipelineCacheEntry *)SDL_calloc( + renderer->blitPipelineCapacity, sizeof(BlitPipelineCacheEntry)); + + // Fullscreen vertex shader + SDL_zero(shaderCreateInfo); + shaderCreateInfo.code = (Uint8 *)blitVert; + shaderCreateInfo.code_size = SDL_strlen(blitVert); + shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX; + shaderCreateInfo.format = SDL_GPU_SHADERFORMAT_WGSL; + shaderCreateInfo.entrypoint = "main"; + + renderer->blitResources.blitVertexShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blitVertexShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile vertex shader for blit!"); + } + + shaderCreateInfo.code = (Uint8 *)blit2DShader; + shaderCreateInfo.code_size = SDL_strlen(blit2DShader); + shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT; + shaderCreateInfo.num_samplers = 1; + shaderCreateInfo.num_uniform_buffers = 1; + renderer->blitResources.blit2DShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blit2DShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile Blit2D pixel shader!"); + } + + shaderCreateInfo.code = (Uint8 *)blit2DArrayShader; + shaderCreateInfo.code_size = SDL_strlen(blit2DArrayShader); + shaderCreateInfo.entrypoint = "main"; + renderer->blitResources.blit2DArrayShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blit2DArrayShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile Blit2DArray pixel shader!"); + } + + shaderCreateInfo.code = (Uint8 *)blit3DShader; + shaderCreateInfo.code_size = SDL_strlen(blit3DShader); + renderer->blitResources.blit3DShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blit3DShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile Blit3D pixel shader!"); + } + + shaderCreateInfo.code = (Uint8 *)blitCubeShader; + shaderCreateInfo.code_size = SDL_strlen(blitCubeShader); + renderer->blitResources.blitCubeShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blitCubeShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitCube pixel shader!"); + } + + shaderCreateInfo.code = (Uint8 *)blitCubeArrayShader; + shaderCreateInfo.code_size = SDL_strlen(blitCubeArrayShader); + renderer->blitResources.blitCubeArrayShader = WEBGPU_CreateShader( + (SDL_GPURenderer *)renderer, + &shaderCreateInfo); + if (renderer->blitResources.blitCubeArrayShader == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitCubeArray pixel shader!"); + } + + SDL_GPUSamplerCreateInfo nearestCreateInfo = (SDL_GPUSamplerCreateInfo){ + .min_filter = SDL_GPU_FILTER_NEAREST, + .mag_filter = SDL_GPU_FILTER_NEAREST, + .mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST, + .address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + }; + + renderer->blitResources.blitNearestSampler = WEBGPU_CreateSampler( + (SDL_GPURenderer *)renderer, + &nearestCreateInfo); + + if (renderer->blitResources.blitNearestSampler == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create blit nearest sampler!"); + } + + SDL_GPUSamplerCreateInfo linearCreateInfo = (SDL_GPUSamplerCreateInfo){ + .min_filter = SDL_GPU_FILTER_LINEAR, + .mag_filter = SDL_GPU_FILTER_LINEAR, + .mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR, + .address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + }; + + renderer->blitResources.blitLinearSampler = WEBGPU_CreateSampler( + (SDL_GPURenderer *)renderer, + &linearCreateInfo); + + if (renderer->blitResources.blitLinearSampler == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create blit linear sampler!"); + } +} + +static void WEBGPU_INTERNAL_ReleaseUniformResources(WebGPURenderer *renderer) +{ + for (int i = 0; i < 12; i++) { + WEBGPU_INTERNAL_ReleaseBufferContainer(renderer, renderer->uniformBuffers[i]); + } + for (int i = 0; i < 3; i++) { + wgpuBindGroupRelease(renderer->uniformBufferBindGroups[i]); + wgpuBindGroupLayoutRelease(renderer->uniformBufferBindGroupLayouts[i]); + } +} + +static void WEBGPU_INTERNAL_ReleaseBlitResources(WebGPURenderer *renderer) +{ + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blit2DArrayShader); + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blit2DShader); + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blit3DShader); + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blitCubeShader); + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blitCubeArrayShader); + WEBGPU_INTERNAL_ReleaseShader((WebGPUShader *)renderer->blitResources.blitVertexShader); + WEBGPU_INTERNAL_ReleaseSampler(renderer, (WebGPUSampler *)renderer->blitResources.blitLinearSampler); + WEBGPU_INTERNAL_ReleaseSampler(renderer, (WebGPUSampler *)renderer->blitResources.blitNearestSampler); + + for (int i = 0; i < renderer->blitPipelineCount; i++) { + WEBGPU_ReleaseGraphicsPipeline((SDL_GPURenderer *)renderer, renderer->blitPipelines[i].pipeline); + } +} + +static WebGPUBuffer *WEBGPU_INTERNAL_CreateBuffer(WebGPURenderer *renderer, Uint64 size, SDL_GPUBufferUsageFlags usageFlags, WebGPUBufferType bufferType, const char *debugName) +{ + WGPUBufferUsage usages = 0; + + if (bufferType == WEBGPU_BUFFER_TYPE_GPU) { + if (usageFlags & SDL_GPU_BUFFERUSAGE_VERTEX) { + usages |= WGPUBufferUsage_Vertex; + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } + + if (usageFlags & SDL_GPU_BUFFERUSAGE_INDEX) { + usages |= WGPUBufferUsage_Index; + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } + + if (usageFlags & (SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ | + SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ | + SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE)) { + usages |= WGPUBufferUsage_Storage; + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } + + if (usageFlags & SDL_GPU_BUFFERUSAGE_INDIRECT) { + usages |= WGPUBufferUsage_Indirect; + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } + } else if (bufferType == WEBGPU_BUFFER_TYPE_TRANSFER_UPLOAD) { + // HACK: + // Anything with a MapWrite usage can't be copied to. This has the issue of making + // it impossible to upload pseudo-mapped data to a buffer which can also be mapped. + // God I hate this. Why do you do these things to me Firefox? + if (size <= renderer->pseudoMapBufferSizeThreshold) { + usages |= WGPUBufferUsage_CopyDst; + } else { + usages |= WGPUBufferUsage_MapWrite; + } + usages |= WGPUBufferUsage_CopySrc; + } else if (bufferType == WEBGPU_BUFFER_TYPE_TRANSFER_DOWNLOAD) { + usages |= WGPUBufferUsage_MapRead; + usages |= WGPUBufferUsage_CopyDst; + } else if (bufferType == WEBGPU_BUFFER_TYPE_TRANSFER_GPUONLY) { + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } else if (bufferType == WEBGPU_BUFFER_TYPE_UNIFORM) { + usages |= WGPUBufferUsage_Uniform; + usages |= WGPUBufferUsage_CopyDst; + usages |= WGPUBufferUsage_CopySrc; + } + + WebGPUBuffer *buf = SDL_calloc(1, sizeof(*buf)); + + buf->size = ALIGN_VALUE(size, 4); + buf->usage = usages; + buf->type = bufferType; + buf->identifier = SDL_GetAtomicInt(&renderer->nextBindableResourceID); + + SDL_AtomicIncRef(&renderer->nextBindableResourceID); + + WGPUBufferDescriptor desc = { + .label = debugName != NULL ? (WGPUStringView){ debugName, SDL_strlen(debugName) } : (WGPUStringView){ NULL, 0 }, + .size = ALIGN_VALUE(size, 4), + .usage = usages, + .mappedAtCreation = false, + .nextInChain = NULL, + }; + + buf->buffer = wgpuDeviceCreateBuffer(renderer->device, &desc); + SDL_SetAtomicInt(&buf->referenceCount, 0); + + return buf; +} + +static WebGPUBufferContainer *WEBGPU_INTERNAL_CreateBufferContainer( + WebGPURenderer *renderer, + Uint64 size, + SDL_GPUBufferUsageFlags usageFlags, + WebGPUBufferType bufferType, + bool dedicated, + const char *debugName) +{ + WebGPUBufferContainer *bufferContainer; + WebGPUBuffer *buffer; + + buffer = WEBGPU_INTERNAL_CreateBuffer( + renderer, + size, + usageFlags, + bufferType, + debugName); + + if (buffer == NULL) { + return NULL; + } + + bufferContainer = SDL_calloc(1, sizeof(WebGPUBufferContainer)); + + bufferContainer->activeBuffer = buffer; + buffer->container = bufferContainer; + + bufferContainer->bufferCapacity = 1; + bufferContainer->bufferCount = 1; + bufferContainer->buffers = SDL_calloc(bufferContainer->bufferCapacity, sizeof(WebGPUBuffer *)); + bufferContainer->buffers[0] = bufferContainer->activeBuffer; + bufferContainer->bufferType = bufferType; + bufferContainer->usageFlags = usageFlags; + bufferContainer->size = ALIGN_VALUE(size, 4); + + // The memory is allocated when the transfer buffer's first mapped. + bufferContainer->pseudoMappedRange = NULL; + bufferContainer->shouldPseudomap = size <= renderer->pseudoMapBufferSizeThreshold && bufferType == WEBGPU_BUFFER_TYPE_TRANSFER_UPLOAD; + + return bufferContainer; +} + +static void WEBGPU_INTERNAL_ReleaseBuffer(WebGPURenderer *renderer, WebGPUBuffer *buffer) +{ + wgpuBufferRelease(buffer->buffer); + SDL_free(buffer); + buffer = NULL; +} + +static void WEBGPU_INTERNAL_ReleaseTextureView(WebGPURenderer *renderer, WebGPUTextureView *view) +{ + wgpuTextureViewRelease(view->view); + SDL_free(view); + view = NULL; +} + +static void WEBGPU_INTERNAL_ReleaseTexture(WebGPURenderer *renderer, WebGPUTexture *texture) +{ + for (int i = 0; i < texture->textureViewCount; i++) { + WEBGPU_INTERNAL_ReleaseTextureView(renderer, texture->textureViews[i]); + } + + WEBGPU_INTERNAL_ReleaseTextureView(renderer, texture->fullTextureView); + + wgpuTextureRelease(texture->texture); + SDL_free(texture->textureViews); + SDL_free(texture); + texture = NULL; +} + +static void WEBGPU_INTERNAL_ReleaseTextureContainer(WebGPURenderer *renderer, WebGPUTextureContainer *container) +{ + for (int i = 0; i < container->textureCount; i++) { + WEBGPU_INTERNAL_ReleaseTexture(renderer, container->textures[i]); + } + SDL_free(container->textures); + SDL_free(container); + container = NULL; +} + +static void WEBGPU_INTERNAL_ReleaseBufferContainer(WebGPURenderer *renderer, WebGPUBufferContainer *container) +{ + if (container == NULL) { + return; + } + + for (int i = 0; i < container->bufferCount; i++) { + WEBGPU_INTERNAL_ReleaseBuffer(renderer, container->buffers[i]); + } + + SDL_free(container->pseudoMappedRange); + SDL_free(container->buffers); + SDL_free(container); + container = NULL; +} + +static void WEBGPU_INTERNAL_CycleBufferContainer(WebGPURenderer *renderer, WebGPUBufferContainer *container) +{ + if (SDL_GetAtomicInt(&container->activeBuffer->referenceCount) == 0) { + // The active buffer isn't being used by any command buffers, no need to cycle anything. + goto finish; + } + + if (container->bufferCount > 1) { + for (int i = 0; i < container->bufferCount; i++) { + WebGPUBuffer *current = container->buffers[i]; + SDL_assert_release(current != NULL); + + if (SDL_GetAtomicInt(¤t->referenceCount) != 0) { + continue; + } else { + container->activeBuffer = current; + goto finish; + } + } + } + + WebGPUBuffer *newBuf = + WEBGPU_INTERNAL_CreateBuffer(renderer, container->size, container->usageFlags, container->bufferType, NULL); + + WEBGPU_INTERNAL_InsertElementIntoArray(container->buffers, container->bufferCapacity, container->bufferCount, WebGPUBuffer *, newBuf); + container->activeBuffer = newBuf; + +finish: + return; +} + +static void WEBGPU_INTERNAL_CycleTextureContainer(WebGPURenderer *renderer, WebGPUTextureContainer *container) +{ + if (SDL_GetAtomicInt(&container->activeTexture->referenceCount) == 0) { + goto finish; + } + + if (container->textureCount > 1) { + for (int i = 0; i < container->textureCount; i++) { + WebGPUTexture *current = container->textures[i]; + SDL_assert_release(current != NULL); + + if (SDL_GetAtomicInt(¤t->referenceCount) != 0) { + continue; + } else { + container->activeTexture = current; + goto finish; + } + } + } + + WebGPUTexture *newTex = WEBGPU_INTERNAL_CreateTexture(renderer, &container->header.info); + WEBGPU_INTERNAL_InsertElementIntoArray(container->textures, container->textureCapacity, container->textureCount, WebGPUTexture *, newTex); + container->activeTexture = newTex; + +finish: + return; +} +static void WEBGPU_Blit(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUBlitInfo *info); +static void WEBGPU_CopyTextureToTexture(SDL_GPUCommandBuffer *copyPass, const SDL_GPUTextureLocation *source, const SDL_GPUTextureLocation *destination, Uint32 w, Uint32 h, Uint32 d, bool cycle); +static void WEBGPU_GenerateMipmaps(SDL_GPUCommandBuffer *commandBuffer, SDL_GPUTexture *_texture) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WebGPUTexture *texture = ((WebGPUTextureContainer *)_texture)->activeTexture; + + if (texture->type != SDL_GPU_TEXTURETYPE_2D) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "GenerateMipmaps currently only supports non-array 2D textures!"); + return; + } + + SDL_GPUTexture *renderTexture = WEBGPU_CreateTexture((SDL_GPURenderer *)cmdBuf->renderer, &(SDL_GPUTextureCreateInfo){ + .format = texture->format, + .height = wgpuTextureGetHeight(texture->texture), + .width = wgpuTextureGetWidth(texture->texture), + .layer_count_or_depth = wgpuTextureGetDepthOrArrayLayers(texture->texture), + .type = texture->type, + .num_levels = wgpuTextureGetMipLevelCount(texture->texture), + .usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, + }); + Uint32 currentSizeX = wgpuTextureGetWidth(texture->texture); + Uint32 currentSizeY = wgpuTextureGetHeight(texture->texture); + for (int i = 0; i < wgpuTextureGetDepthOrArrayLayers(texture->texture); i++) { + for (int j = 1; j < wgpuTextureGetMipLevelCount(texture->texture); j++) { + SDL_GPUBlitRegion source = { + .texture = _texture, + .layer_or_depth_plane = i, + .mip_level = j - 1, + .w = currentSizeX, + .h = currentSizeY, + }; + + currentSizeX /= 2; + currentSizeY /= 2; + + SDL_GPUBlitRegion destination = { + .texture = renderTexture, + .layer_or_depth_plane = i, + .mip_level = j, + .w = currentSizeX, + .h = currentSizeY, + }; + + WEBGPU_Blit(commandBuffer, &(SDL_GPUBlitInfo){ + .filter = SDL_GPU_FILTER_LINEAR, + .load_op = SDL_GPU_LOADOP_DONT_CARE, + .source = source, + .destination = destination, + }); + + WEBGPU_BeginCopyPass(commandBuffer); + WEBGPU_CopyTextureToTexture( + commandBuffer, + &(SDL_GPUTextureLocation){ + .texture = renderTexture, + .layer = i, + .mip_level = j, + }, + &(SDL_GPUTextureLocation){ + .texture = _texture, + .layer = i, + .mip_level = j, + }, + currentSizeX, currentSizeY, 1, false); + WEBGPU_EndCopyPass(commandBuffer); + } + } + + WEBGPU_INTERNAL_ReleaseTextureContainer(cmdBuf->renderer, (WebGPUTextureContainer *)renderTexture); +} + +static SDL_GPUBuffer *WEBGPU_CreateBuffer( + SDL_GPURenderer *driverData, + SDL_GPUBufferUsageFlags usageFlags, + Uint32 size, + const char *debugName) +{ + // #15981 + if ((usageFlags & SDL_GPU_BUFFERUSAGE_VERTEX) && (usageFlags & SDL_GPU_BUFFERUSAGE_INDEX) && ((WebGPURenderer *)driverData)->debugMode) { + SDL_assert_release(!"Buffer cannot be created with both VERTEX and INDEX flags!"); + return NULL; + } + + return (SDL_GPUBuffer *)WEBGPU_INTERNAL_CreateBufferContainer( + (WebGPURenderer *)driverData, + size, + usageFlags, + WEBGPU_BUFFER_TYPE_GPU, + false, + debugName); +} + +static SDL_GPUTransferBuffer *WEBGPU_CreateTransferBuffer(SDL_GPURenderer *device, SDL_GPUTransferBufferUsage usage, Uint32 size, const char *debugName) +{ + return (SDL_GPUTransferBuffer *)WEBGPU_INTERNAL_CreateBufferContainer( + (WebGPURenderer *)device, + size, + usage, + usage == SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD ? WEBGPU_BUFFER_TYPE_TRANSFER_UPLOAD : WEBGPU_BUFFER_TYPE_TRANSFER_DOWNLOAD, + false, + debugName); +} + +static void WEBGPU_ReleaseBuffer(SDL_GPURenderer *device, SDL_GPUBuffer *buffer) +{ + if (device != NULL && buffer != NULL) { + WEBGPU_INTERNAL_QueueBufferContainerForRelease((WebGPURenderer *)device, (WebGPUBufferContainer *)buffer); + } +} + +static void WEBGPU_ReleaseTransferBuffer(SDL_GPURenderer *device, SDL_GPUTransferBuffer *buffer) +{ + if (device != NULL && buffer != NULL) { + WEBGPU_INTERNAL_QueueBufferContainerForRelease((WebGPURenderer *)device, (WebGPUBufferContainer *)buffer); + } +} + +static void WEBGPU_INTERNAL_MapBufferCallback(WGPUMapAsyncStatus status, WGPUStringView message, void *unused0, void *unused1) +{ + // If the callback's NULL it'll segfault since the PC jumps to 0x0 so we need to have something here. +} + +static bool WEBGPU_INTERNAL_MapBuffer(WebGPURenderer *renderer, WebGPUBufferContainer *buffer) +{ + WGPUMapMode mapMode = WGPUMapMode_None; + + if (buffer->activeBuffer->type == WEBGPU_BUFFER_TYPE_TRANSFER_DOWNLOAD) { + mapMode = WGPUMapMode_Read; + } else if (buffer->activeBuffer->type == WEBGPU_BUFFER_TYPE_TRANSFER_UPLOAD) { + mapMode = WGPUMapMode_Write; + } else { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempting to map non-transfer buffer!"); + return false; + } + + WebGPUFence *bufferMapFence = + WEBGPU_INTERNAL_CreateFenceFromFuture(wgpuBufferMapAsync(buffer->activeBuffer->buffer, + mapMode, 0, + buffer->activeBuffer->size, + (WGPUBufferMapCallbackInfo){ + .callback = WEBGPU_INTERNAL_MapBufferCallback, + .mode = WGPUCallbackMode_WaitAnyOnly, + .nextInChain = NULL, + .userdata1 = NULL, + .userdata2 = NULL, + })); + + WEBGPU_WaitForFences((SDL_GPURenderer *)renderer, true, (SDL_GPUFence **)&bufferMapFence, 1); + + SDL_free(bufferMapFence); + return true; +} + +static void *WEBGPU_INTERNAL_MapBufferRange(WebGPURenderer *renderer, WebGPUBufferContainer *buffer, size_t offset, size_t size) +{ + // If "size" is -1, then bind the entire buffer. + size_t bindSize = (size == -1) ? buffer->activeBuffer->size : size; + + if (!WEBGPU_INTERNAL_MapBuffer(renderer, buffer)) { + SDL_SetError("Failed to map buffer!"); + return NULL; + } + + if (buffer->activeBuffer->type == WEBGPU_BUFFER_TYPE_TRANSFER_DOWNLOAD) { + return (void *)wgpuBufferGetConstMappedRange(buffer->activeBuffer->buffer, offset, bindSize); + } else { + return wgpuBufferGetMappedRange(buffer->activeBuffer->buffer, offset, bindSize); + } +} + +static void *WEBGPU_MapTransferBuffer(SDL_GPURenderer *device, SDL_GPUTransferBuffer *transferBuffer, bool cycle) +{ + if (cycle) { + WEBGPU_INTERNAL_CycleBufferContainer((WebGPURenderer *)device, (WebGPUBufferContainer *)transferBuffer); + } + + if (((WebGPUBufferContainer *)transferBuffer)->shouldPseudomap) { + // Time to do our magic tricks. + if (((WebGPUBufferContainer *)transferBuffer)->pseudoMappedRange == NULL) { + ((WebGPUBufferContainer *)transferBuffer)->pseudoMappedRange = SDL_calloc(((WebGPUBufferContainer *)transferBuffer)->size, 1); + } + + // Mapped on CPU. + ((WebGPUBufferContainer *)transferBuffer)->mapState = MAP_STATE_MAPPED_CPU; + + return ((WebGPUBufferContainer *)transferBuffer)->pseudoMappedRange; + } else { + // Mapped on GPU. + ((WebGPUBufferContainer *)transferBuffer)->mapState = MAP_STATE_MAPPED_GPU; + + return WEBGPU_INTERNAL_MapBufferRange((WebGPURenderer *)device, (WebGPUBufferContainer *)transferBuffer, 0, -1); + } +} + +static void WEBGPU_UnmapTransferBuffer(SDL_GPURenderer *device, SDL_GPUTransferBuffer *transferBuffer) +{ + if (((WebGPUBufferContainer *)transferBuffer)->mapState == MAP_STATE_MAPPED_GPU) { + wgpuBufferUnmap(((WebGPUBufferContainer *)transferBuffer)->activeBuffer->buffer); + } + + ((WebGPUBufferContainer *)transferBuffer)->mapState = MAP_STATE_UNMAPPED; +} + +static void WEBGPU_INTERNAL_CopyBufferToBuffer(WGPUCommandEncoder encoder, WebGPUBufferContainer *sourceBuf, + Uint32 sourceBufOffset, WebGPUBufferContainer *destBuf, + Uint32 destBufOffset, uint32_t size) +{ + wgpuCommandEncoderCopyBufferToBuffer(encoder, sourceBuf->activeBuffer->buffer, sourceBufOffset, destBuf->activeBuffer->buffer, destBufOffset, size); +} + +static void WEBGPU_CopyBufferToBuffer(SDL_GPUCommandBuffer *copyPass, const SDL_GPUBufferLocation *source, const SDL_GPUBufferLocation *destination, Uint32 size, bool cycle) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)copyPass; + + if (cycle) { + WEBGPU_INTERNAL_CycleBufferContainer(((WebGPUCommandBuffer *)copyPass)->renderer, (WebGPUBufferContainer *)destination->buffer); + } + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)source->buffer)->activeBuffer); + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)destination->buffer)->activeBuffer); + + WEBGPU_INTERNAL_CopyBufferToBuffer(((WebGPUCommandBuffer *)copyPass)->encoder, (WebGPUBufferContainer *)source->buffer, source->offset, (WebGPUBufferContainer *)destination->buffer, destination->offset, size); +} + +static void WEBGPU_CopyTextureToTexture(SDL_GPUCommandBuffer *copyPass, const SDL_GPUTextureLocation *source, const SDL_GPUTextureLocation *destination, Uint32 w, Uint32 h, Uint32 d, bool cycle) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)copyPass; + WGPUTexelCopyTextureInfo sourceInfo, destInfo; + + if (cycle) { + WEBGPU_INTERNAL_CycleTextureContainer(cmdBuf->renderer, (WebGPUTextureContainer *)destination->texture); + } + + // ASTC support. We only care about the destination texture being aligned though. + Uint32 blockWidthDest = Texture_GetBlockWidth(((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + Uint32 blockHeightDest = Texture_GetBlockHeight(((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + + sourceInfo.texture = ((WebGPUTextureContainer *)source->texture)->activeTexture->texture; + sourceInfo.aspect = ((WebGPUTextureContainer *)source->texture)->activeTexture->aspect; + sourceInfo.origin = (WGPUOrigin3D){ .x = source->x, .y = source->y, .z = source->z + source->layer }; + sourceInfo.mipLevel = source->mip_level; + + destInfo.texture = ((WebGPUTextureContainer *)destination->texture)->activeTexture->texture; + destInfo.aspect = ((WebGPUTextureContainer *)destination->texture)->activeTexture->aspect; + destInfo.origin = (WGPUOrigin3D){ .x = destination->x, .y = destination->y, .z = destination->z + destination->layer }; + destInfo.mipLevel = destination->mip_level; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)source->texture)->activeTexture); + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)destination->texture)->activeTexture); + + wgpuCommandEncoderCopyTextureToTexture(cmdBuf->encoder, &sourceInfo, &destInfo, &(WGPUExtent3D){ ALIGN_VALUE(w, blockWidthDest), ALIGN_VALUE(h, blockHeightDest), d }); +} + +// This was originally a full-blown public function but I decided against it, so that's why it has the opaque types as arguments. +static void WEBGPU_INTERNAL_UploadToBufferFromCPU(SDL_GPUCommandBuffer *copy_pass, const SDL_GPUBufferRegion *region, const void *data, bool cycle) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)copy_pass; + + if (cycle) { + WEBGPU_INTERNAL_CycleBufferContainer(cmdBuf->renderer, (WebGPUBufferContainer *)region->buffer); + } + + // FIXME: This function is not thread-safe! + wgpuQueueWriteBuffer(cmdBuf->queue, ((WebGPUBufferContainer *)region->buffer)->activeBuffer->buffer, + region->offset, data, ALIGN_VALUE(region->size, 4)); +} + +static void WEBGPU_UploadToBuffer(SDL_GPUCommandBuffer *copyPass, const SDL_GPUTransferBufferLocation *source, const SDL_GPUBufferRegion *destination, bool cycle) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)copyPass; + + if (cycle) { + WEBGPU_INTERNAL_CycleBufferContainer(((WebGPUCommandBuffer *)copyPass)->renderer, (WebGPUBufferContainer *)destination->buffer); + } + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)source->transfer_buffer)->activeBuffer); + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)destination->buffer)->activeBuffer); + + // Spaghetti code here. Opaque pointers are terrifying. + if (((WebGPUBufferContainer *)source->transfer_buffer)->shouldPseudomap) { + WEBGPU_INTERNAL_UploadToBufferFromCPU(copyPass, destination, ((WebGPUBufferContainer *)source->transfer_buffer)->pseudoMappedRange + source->offset, false); + } else { + WEBGPU_INTERNAL_CopyBufferToBuffer(((WebGPUCommandBuffer *)copyPass)->encoder, (WebGPUBufferContainer *)source->transfer_buffer, source->offset, + (WebGPUBufferContainer *)destination->buffer, destination->offset, ALIGN_VALUE(destination->size, 4)); + } +} + +// This'll eventually become a public function, but until PR #16249 is merged it'll be internal. +static void WEBGPU_INTERNAL_CopyBufferToTexture(SDL_GPUCommandBuffer *copyPass, const SDL_GPUBufferLocation *source, const SDL_GPUTextureRegion *destination, bool cycle) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)copyPass; + WGPUTexelCopyBufferInfo sourceInfo = { 0 }; + + // This should be NULL unless the user didn't pad their data correctly. + WebGPUBufferContainer *interimBuffer = NULL; + + Uint32 blockWidth = Texture_GetBlockWidth(((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + Uint32 blockHeight = Texture_GetBlockHeight(((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + Uint32 bytesPerRowNotAligned = BytesPerRow(destination->w, ((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + Uint32 bytesPerRow = BytesPerRow(ALIGN_VALUE(destination->w, blockWidth), ((WebGPUTextureContainer *)destination->texture)->activeTexture->format); + + if (cycle) { + WEBGPU_INTERNAL_CycleTextureContainer(cmdBuf->renderer, (WebGPUTextureContainer *)destination->texture); + } + + if (((WebGPUBufferContainer *)source->buffer)->shouldPseudomap) { + WebGPUBufferContainer *sourceBuffer = (WebGPUBufferContainer *)source->buffer; + + if (source->offset > sourceBuffer->size) { + SDL_assert_release(!"Offset is greater than buffer size!"); + } + + wgpuQueueWriteBuffer(cmdBuf->queue, sourceBuffer->activeBuffer->buffer, source->offset, + sourceBuffer->pseudoMappedRange + source->offset, SDL_max((Uint64)bytesPerRowNotAligned * ((destination->h + blockHeight - 1) / blockHeight), 4)); + } + + sourceInfo = (WGPUTexelCopyBufferInfo){ + .buffer = ((WebGPUBufferContainer *)source->buffer)->activeBuffer->buffer, + .layout = (WGPUTexelCopyBufferLayout){ + .bytesPerRow = bytesPerRow, + .rowsPerImage = (destination->h + blockHeight - 1) / blockHeight, + .offset = source->offset, + }, + }; + + if (bytesPerRow % 256 != 0) { + // BytesPerRow isn't a multiple of 256! This is a Bad Thing! + // WebGPU inherits D3D12's horrible 256-byte alignment requirement for BPR, so we'll need to do something about this. + // To do this, we'll create an interim buffer which'll be 256-byte aligned, and then we'll issue some GPU copies to put the user's data into said interim buffer. + + // NOTE: Since the D3D12 backend doesn't bother with caching the interim buffer, I decided not to either. + // However, I'm not sure if that's a good idea? Since the Web is just one giant attack vector WebGPU's pretty strict when it comes to GPU memory. + // This means that not only is it a royal PITA to do anything, it's also really slow to do anything. It wants to make sure 100% that you can't do anything bad. + // It's possible that this'll have a more noticeable performance impact than in D3D12. We should run some tests to see if this'd be a potential concern. + Uint32 paddedBPR = ALIGN_VALUE(bytesPerRow, 256); + + // HACK: We need to add blockWidth since for some reason 1x1 texture uploads require 260 bytes of data + // despite WebGPU's very strong insistance on 256-byte alignment? Am I tweaking? + interimBuffer = WEBGPU_INTERNAL_CreateBufferContainer(cmdBuf->renderer, (Uint64)paddedBPR * ALIGN_VALUE(destination->h, blockHeight) + blockWidth, + 0, WEBGPU_BUFFER_TYPE_TRANSFER_GPUONLY, false, + "SDLGPU Automatic Texture Padding Buffer"); + + for (int i = 0; i < (destination->h + blockHeight - 1) / blockHeight; i++) { + WEBGPU_INTERNAL_CopyBufferToBuffer(cmdBuf->encoder, (WebGPUBufferContainer *)source->buffer, + source->offset + bytesPerRow * i, interimBuffer, paddedBPR * i, SDL_max(bytesPerRow, 4)); + } + + sourceInfo = (WGPUTexelCopyBufferInfo){ + .buffer = interimBuffer->activeBuffer->buffer, + .layout = (WGPUTexelCopyBufferLayout){ + .bytesPerRow = paddedBPR, + .rowsPerImage = (destination->h + blockHeight - 1) / blockHeight, + .offset = 0, + }, + }; + } + + WGPUTexelCopyTextureInfo destInfo = { + .aspect = WGPUTextureAspect_All, + .texture = ((WebGPUTextureContainer *)destination->texture)->activeTexture->texture, + .mipLevel = destination->mip_level, + .origin = (WGPUOrigin3D){ destination->x, destination->y, destination->z + destination->layer }, + }; + + wgpuCommandEncoderCopyBufferToTexture(cmdBuf->encoder, + &sourceInfo, &destInfo, + &(WGPUExtent3D){ + ALIGN_VALUE(destination->w, blockWidth), + ALIGN_VALUE(destination->h, blockHeight), + destination->d, + }); + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)destination->texture)->activeTexture); + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)source->buffer)->activeBuffer); + + if (interimBuffer) { + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, interimBuffer->activeBuffer); + WEBGPU_INTERNAL_QueueBufferContainerForRelease(cmdBuf->renderer, interimBuffer); + } +} + +static void WEBGPU_UploadToTexture(SDL_GPUCommandBuffer *copyPass, const SDL_GPUTextureTransferInfo *source, const SDL_GPUTextureRegion *destination, bool cycle) +{ + // Since a transfer buffer in the WebGPU backend is actually just a normal buffer in a trenchcoat we can just use CopyBufferToTexture. + WEBGPU_INTERNAL_CopyBufferToTexture(copyPass, + &(SDL_GPUBufferLocation){ + .buffer = (SDL_GPUBuffer *)source->transfer_buffer, + .offset = source->offset, + }, + destination, cycle); +} + +static void WEBGPU_PushDebugGroup(SDL_GPUCommandBuffer *commandBuffer, const char *name) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + if (cmdBuf->common.render_pass.in_progress) { + wgpuRenderPassEncoderPushDebugGroup(cmdBuf->renderPassEncoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(name)); + } else if (cmdBuf->common.compute_pass.in_progress) { + wgpuComputePassEncoderPushDebugGroup(cmdBuf->computePassEncoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(name)); + } else { + // segfaults are lies made up by the woke left to distract you from the fact + // that memory access restrictions don't apply to the pure of heart + wgpuCommandEncoderPushDebugGroup(cmdBuf->encoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(name)); + } +} + +static void WEBGPU_PopDebugGroup(SDL_GPUCommandBuffer *commandBuffer) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + if (cmdBuf->common.render_pass.in_progress) { + wgpuRenderPassEncoderPopDebugGroup(cmdBuf->renderPassEncoder); + } else if (cmdBuf->common.compute_pass.in_progress) { + wgpuComputePassEncoderPopDebugGroup(cmdBuf->computePassEncoder); + } else { + wgpuCommandEncoderPopDebugGroup(cmdBuf->encoder); + } +} + +static void WEBGPU_InsertDebugLabel(SDL_GPUCommandBuffer *commandBuffer, const char *text) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + // Each type of encoder has its own debug group functions for some reason + if (cmdBuf->common.render_pass.in_progress) { + wgpuRenderPassEncoderInsertDebugMarker(cmdBuf->renderPassEncoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(text)); + } else if (cmdBuf->common.compute_pass.in_progress) { + wgpuComputePassEncoderInsertDebugMarker(cmdBuf->computePassEncoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(text)); + } else { + wgpuCommandEncoderInsertDebugMarker(cmdBuf->encoder, WEBGPU_INTERNAL_CharPtrToWebGPUStringView(text)); + } +} + +static void WEBGPU_BeginRenderPass(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUColorTargetInfo *colorTargetInfos, + Uint32 numColorTargets, const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo) +{ + WebGPUCommandBuffer *wrapper = (WebGPUCommandBuffer *)commandBuffer; + + WGPURenderPassDescriptor desc = { 0 }; + + WGPURenderPassColorAttachment *colorAttachments; + WGPURenderPassDepthStencilAttachment *depthStencilAttachment = NULL; + + colorAttachments = (WGPURenderPassColorAttachment *)SDL_calloc(numColorTargets, sizeof(*colorAttachments)); + + // these are by default 0, so SDL_min would always favour + // them over the texture's dimensions if we didn't do this + wrapper->smallestColorTargetW = SDL_MAX_UINT32; + wrapper->smallestColorTargetH = SDL_MAX_UINT32; + + for (int i = 0; i < numColorTargets; i++) { + WebGPUTexture *texture = ((WebGPUTextureContainer *)colorTargetInfos[i].texture)->activeTexture; + colorAttachments[i].clearValue.a = colorTargetInfos[i].clear_color.a; + colorAttachments[i].clearValue.r = colorTargetInfos[i].clear_color.r; + colorAttachments[i].clearValue.g = colorTargetInfos[i].clear_color.g; + colorAttachments[i].clearValue.b = colorTargetInfos[i].clear_color.b; + colorAttachments[i].loadOp = SDLToWebGPU_LoadOp[colorTargetInfos[i].load_op]; + colorAttachments[i].storeOp = SDLToWebGPU_StoreOp[colorTargetInfos[i].store_op]; + colorAttachments[i].depthSlice = texture->type == SDL_GPU_TEXTURETYPE_3D ? colorTargetInfos[i].layer_or_depth_plane : WGPU_DEPTH_SLICE_UNDEFINED; + + wrapper->smallestColorTargetW = SDL_min(wrapper->smallestColorTargetW, wgpuTextureGetWidth(((WebGPUTextureContainer *)colorTargetInfos[i].texture)->activeTexture->texture)); + wrapper->smallestColorTargetH = SDL_min(wrapper->smallestColorTargetH, wgpuTextureGetHeight(((WebGPUTextureContainer *)colorTargetInfos[i].texture)->activeTexture->texture)); + + switch (((WebGPUTextureContainer *)colorTargetInfos[i].texture)->activeTexture->type) { + case SDL_GPU_TEXTURETYPE_3D: + colorAttachments[i].view = texture->fullTextureView->view; + break; + case SDL_GPU_TEXTURETYPE_2D: + case SDL_GPU_TEXTURETYPE_2D_ARRAY: + case SDL_GPU_TEXTURETYPE_CUBE: + case SDL_GPU_TEXTURETYPE_CUBE_ARRAY: + colorAttachments[i].view = texture->textureViews[WEBGPU_INTERNAL_GetTextureViewIndex( + colorTargetInfos[i].layer_or_depth_plane, + colorTargetInfos[i].mip_level, + wgpuTextureGetMipLevelCount(texture->texture))] + ->view; + break; + } + + if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) { + WebGPUTexture *resolveTexture = ((WebGPUTextureContainer *)colorTargetInfos[i].resolve_texture)->activeTexture; + + colorAttachments[i].resolveTarget = resolveTexture->textureViews[WEBGPU_INTERNAL_GetTextureViewIndex( + colorTargetInfos[i].resolve_layer, + colorTargetInfos[i].resolve_mip_level, + wgpuTextureGetMipLevelCount(resolveTexture->texture))] + ->view; + } + } + + if (depthStencilTargetInfo != NULL) { + SDL_GPUTextureFormat textureFormat = ((WebGPUTextureContainer *)depthStencilTargetInfo->texture)->header.info.format; + depthStencilAttachment = (WGPURenderPassDepthStencilAttachment *)SDL_calloc(1, sizeof(*depthStencilAttachment)); + + depthStencilAttachment->depthClearValue = depthStencilTargetInfo->clear_depth; + depthStencilAttachment->depthLoadOp = SDLToWebGPU_LoadOp[depthStencilTargetInfo->load_op]; + depthStencilAttachment->depthStoreOp = SDLToWebGPU_StoreOp[depthStencilTargetInfo->store_op]; + depthStencilAttachment->stencilClearValue = IsStencilFormat(textureFormat) ? depthStencilTargetInfo->clear_stencil : 0; + depthStencilAttachment->stencilLoadOp = IsStencilFormat(textureFormat) ? SDLToWebGPU_LoadOp[depthStencilTargetInfo->stencil_load_op] : WGPULoadOp_Undefined; + depthStencilAttachment->stencilStoreOp = IsStencilFormat(textureFormat) ? SDLToWebGPU_StoreOp[depthStencilTargetInfo->stencil_store_op] : WGPUStoreOp_Undefined; + depthStencilAttachment->view = + ((WebGPUTextureContainer *)depthStencilTargetInfo->texture)->activeTexture->textureViews[WEBGPU_INTERNAL_GetTextureViewIndex(depthStencilTargetInfo->layer, depthStencilTargetInfo->mip_level, wgpuTextureGetMipLevelCount(((WebGPUTextureContainer *)depthStencilTargetInfo->texture)->activeTexture->texture))]->view; + } + + desc.colorAttachments = colorAttachments; + desc.colorAttachmentCount = numColorTargets; + desc.depthStencilAttachment = depthStencilTargetInfo != NULL ? depthStencilAttachment : NULL; + desc.occlusionQuerySet = NULL; // Unimplemented in SDLGPU + desc.timestampWrites = NULL; // Unimplemented in SDLGPU as of 2026-06-03, although, there is a proof of concept Query API in development right now. + desc.label = (WGPUStringView){ NULL, 0 }; + + wrapper->renderPassEncoder = wgpuCommandEncoderBeginRenderPass(wrapper->encoder, &desc); + wrapper->boundGraphicsPipeline = NULL; + wrapper->hasBoundGraphicsPipeline = false; + + SDL_free(colorAttachments); + SDL_free(depthStencilAttachment); +} + +static void WEBGPU_SetViewport(SDL_GPUCommandBuffer *renderPass, const SDL_GPUViewport *viewport) +{ + wgpuRenderPassEncoderSetViewport(((WebGPUCommandBuffer *)renderPass)->renderPassEncoder, viewport->x, viewport->y, viewport->w, viewport->h, viewport->min_depth, viewport->max_depth); +} + +static void WEBGPU_PushVertexUniformData(SDL_GPUCommandBuffer *commandBuffer, Uint32 slotIndex, const void *data, Uint32 length) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + if (cmdBuf->vertexStageBinds.currentUniformWriteOffsets[slotIndex] + ALIGN_VALUE(length, 256) > DEFAULT_UNIFORM_BUFFER_SIZE) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Cannot push vertex uniform data as UBO write offset + data length exceeds UBO size!"); + return; + } + + WebGPUQueuedUniformDataUpload upload = { + .data = SDL_malloc(length), + .length = length, + .offset = cmdBuf->vertexStageBinds.currentUniformWriteOffsets[slotIndex], + .slot = slotIndex, + }; + SDL_memcpy(upload.data, data, length); + + WEBGPU_INTERNAL_InsertElementIntoArray(cmdBuf->queuedUniformUploads, cmdBuf->queuedUniformUploadCapacity, + cmdBuf->numQueuedUniformUploads, WebGPUQueuedUniformDataUpload, upload); + + // jank and gross but it works + cmdBuf->vertexStageBinds.currentUniformReadOffsets[slotIndex] = cmdBuf->vertexStageBinds.currentUniformWriteOffsets[slotIndex]; + cmdBuf->vertexStageBinds.currentUniformWriteOffsets[slotIndex] += ALIGN_VALUE(length, 256); + cmdBuf->vertexStageBinds.uniformBindGroupOutdated = true; +} + +static void WEBGPU_PushFragmentUniformData(SDL_GPUCommandBuffer *commandBuffer, Uint32 slotIndex, const void *data, Uint32 length) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + if (cmdBuf->fragmentStageBinds.currentUniformWriteOffsets[slotIndex] + ALIGN_VALUE(length, 256) > DEFAULT_UNIFORM_BUFFER_SIZE) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Cannot push fragment uniform data as UBO write offset + data length exceeds UBO size!"); + return; + } + + WebGPUQueuedUniformDataUpload upload = { + .data = SDL_malloc(length), + .length = length, + .offset = cmdBuf->fragmentStageBinds.currentUniformWriteOffsets[slotIndex], + .slot = MAX_UNIFORM_BUFFERS_PER_STAGE + slotIndex, + }; + SDL_memcpy(upload.data, data, length); + + WEBGPU_INTERNAL_InsertElementIntoArray(cmdBuf->queuedUniformUploads, cmdBuf->queuedUniformUploadCapacity, + cmdBuf->numQueuedUniformUploads, WebGPUQueuedUniformDataUpload, upload); + + cmdBuf->fragmentStageBinds.currentUniformReadOffsets[slotIndex] = cmdBuf->fragmentStageBinds.currentUniformWriteOffsets[slotIndex]; + cmdBuf->fragmentStageBinds.currentUniformWriteOffsets[slotIndex] += ALIGN_VALUE(length, 256); + cmdBuf->fragmentStageBinds.uniformBindGroupOutdated = true; +} + +static void WEBGPU_PushComputeUniformData(SDL_GPUCommandBuffer *commandBuffer, Uint32 slotIndex, const void *data, Uint32 length) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + if (cmdBuf->computeStageBinds.currentUniformWriteOffsets[slotIndex] + ALIGN_VALUE(length, 256) > DEFAULT_UNIFORM_BUFFER_SIZE) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Cannot push compute uniform data as UBO write offset + data length exceeds UBO size!"); + return; + } + + WebGPUQueuedUniformDataUpload upload = { + .data = SDL_malloc(length), + .length = length, + .offset = cmdBuf->computeStageBinds.currentUniformWriteOffsets[slotIndex], + .slot = (MAX_UNIFORM_BUFFERS_PER_STAGE * 2) + slotIndex, + }; + + SDL_memcpy(upload.data, data, length); + + WEBGPU_INTERNAL_InsertElementIntoArray(cmdBuf->queuedUniformUploads, cmdBuf->queuedUniformUploadCapacity, + cmdBuf->numQueuedUniformUploads, WebGPUQueuedUniformDataUpload, upload); + + cmdBuf->computeStageBinds.currentUniformReadOffsets[slotIndex] = cmdBuf->computeStageBinds.currentUniformWriteOffsets[slotIndex]; + cmdBuf->computeStageBinds.currentUniformWriteOffsets[slotIndex] += ALIGN_VALUE(length, 256); + cmdBuf->computeStageBinds.uniformBindGroupOutdated = true; +} + +static void WEBGPU_SetBlendConstants(SDL_GPUCommandBuffer *commandBuffer, SDL_FColor blendConstants) +{ + wgpuRenderPassEncoderSetBlendConstant(((WebGPUCommandBuffer *)commandBuffer)->renderPassEncoder, &(WGPUColor){ blendConstants.a, blendConstants.r, blendConstants.g, blendConstants.b }); +} + +static void WEBGPU_SetScissor(SDL_GPUCommandBuffer *commandBuffer, const SDL_Rect *scissor) +{ + if ((scissor->w <= ((WebGPUCommandBuffer *)commandBuffer)->smallestColorTargetW) && + (scissor->h <= ((WebGPUCommandBuffer *)commandBuffer)->smallestColorTargetH)) { + wgpuRenderPassEncoderSetScissorRect(((WebGPUCommandBuffer *)commandBuffer)->renderPassEncoder, scissor->x, scissor->y, scissor->w, scissor->h); + } else if (((WebGPUCommandBuffer *)commandBuffer)->renderer->debugMode) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempting to use a scissor of size (%u, %u) with a " + "smaller render target of size (%u, %u)! This is disallowed by the WebGPU spec.", + scissor->w, scissor->h, ((WebGPUCommandBuffer *)commandBuffer)->smallestColorTargetW, ((WebGPUCommandBuffer *)commandBuffer)->smallestColorTargetH); + } +} + +static bool WEBGPU_WaitAndAcquireSwapchainTexture(SDL_GPUCommandBuffer *command_buffer, SDL_Window *window, SDL_GPUTexture **swapchain_texture, Uint32 *swapchain_texture_width, Uint32 *swapchain_texture_height) +{ + bool result = WEBGPU_AcquireSwapchainTexture(command_buffer, window, swapchain_texture, swapchain_texture_width, swapchain_texture_height); + + while (*swapchain_texture == NULL) { + WEBGPU_INTERNAL_Spin(); + + result = WEBGPU_AcquireSwapchainTexture(command_buffer, window, swapchain_texture, swapchain_texture_width, swapchain_texture_height); + + if (!result) { + break; + } + } + + return result; +} + +static void WEBGPU_ReleaseTexture(SDL_GPURenderer *renderer, SDL_GPUTexture *texture) +{ + WEBGPU_INTERNAL_QueueTextureContainerForRelease((WebGPURenderer *)renderer, (WebGPUTextureContainer *)texture); +} + +// Will bind a deferred graphics pipeline if it hasn't already been bound +static void WEBGPU_INTERNAL_EnsureGraphicsPipelineIsBound(WebGPUCommandBuffer *cmdBuf) +{ + if (cmdBuf->hasBoundGraphicsPipeline == false && cmdBuf->boundGraphicsPipeline) { + WEBGPU_INTERNAL_GenerateBindGroupLayoutsForCurrentGraphicsBinds(cmdBuf); + WEBGPU_INTERNAL_EnsureGraphicsPipelineExists(cmdBuf->renderer, cmdBuf->boundGraphicsPipeline); + + wgpuRenderPassEncoderSetPipeline(cmdBuf->renderPassEncoder, cmdBuf->boundGraphicsPipeline->pipeline); + + cmdBuf->hasBoundGraphicsPipeline = true; + } +} + +// Will bind a deferred compute pipeline if it hasn't already been bound +static void WEBGPU_INTERNAL_EnsureComputePipelineIsBound(WebGPUCommandBuffer *cmdBuf) +{ + if (cmdBuf->hasBoundComputePipeline == false && cmdBuf->boundComputePipeline) { + WEBGPU_INTERNAL_GenerateBindGroupLayoutsForCurrentComputeBinds(cmdBuf); + WEBGPU_INTERNAL_EnsureComputePipelineExists(cmdBuf->renderer, cmdBuf->boundComputePipeline); + + wgpuComputePassEncoderSetPipeline(cmdBuf->computePassEncoder, cmdBuf->boundComputePipeline->pipeline); + + cmdBuf->hasBoundComputePipeline = true; + } +} + +static void WEBGPU_BindGraphicsPipeline(SDL_GPUCommandBuffer *renderPass, SDL_GPUGraphicsPipeline *graphicsPipeline) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + cmdBuf->boundGraphicsPipeline = (WebGPUGraphicsPipeline *)graphicsPipeline; + cmdBuf->hasBoundGraphicsPipeline = false; + + WEBGPU_INTERNAL_ClearRenderPassBindings(cmdBuf); +} + +static void WEBGPU_BindComputePipeline(SDL_GPUCommandBuffer *commandBuffer, SDL_GPUComputePipeline *computePipeline) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + cmdBuf->boundComputePipeline = (WebGPUComputePipeline *)computePipeline; + cmdBuf->hasBoundComputePipeline = false; + + WEBGPU_INTERNAL_ClearComputePassBindings(cmdBuf, false); +} + +static void WEBGPU_BindVertexBuffers(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, const SDL_GPUBufferBinding *binding, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->vertexStageBinds.boundVertexBuffers[i + firstSlot].buffer = ((WebGPUBufferContainer *)binding[i].buffer)->activeBuffer; + cmdBuf->vertexStageBinds.boundVertexBuffers[i + firstSlot].offset = binding[i].offset; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)binding[i].buffer)->activeBuffer); + } + + cmdBuf->vertexStageBinds.shouldBindVertexBuffers = true; +} + +static void WEBGPU_BindIndexBuffer(SDL_GPUCommandBuffer *renderPass, const SDL_GPUBufferBinding *binding, SDL_GPUIndexElementSize elementSize) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + cmdBuf->vertexStageBinds.boundIndexBuffer.buffer = ((WebGPUBufferContainer *)binding->buffer)->activeBuffer; + cmdBuf->vertexStageBinds.boundIndexBuffer.offset = binding->offset; + + cmdBuf->vertexStageBinds.indexFormat = SDLToWebGPU_IndexFormat[elementSize]; + cmdBuf->vertexStageBinds.shouldBindIndexBuffer = true; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)binding->buffer)->activeBuffer); +} + +static void WEBGPU_BindVertexSamplers(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, const SDL_GPUTextureSamplerBinding *textureSamplerBindings, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->vertexStageBinds.boundSamplers[i + firstSlot] = (WebGPUSampler *)textureSamplerBindings[i].sampler; + cmdBuf->vertexStageBinds.boundTextures[i + firstSlot] = ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture); + } + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindVertexStorageBuffers(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, SDL_GPUBuffer *const *storageBuffers, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->vertexStageBinds.boundStorageBuffers[i + firstSlot] = ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer); + } + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindVertexStorageTextures(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, SDL_GPUTexture *const *storageTextures, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->vertexStageBinds.boundStorageTextures[i + firstSlot] = ((WebGPUTextureContainer *)storageTextures[i])->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)storageTextures[i])->activeTexture); + } + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindFragmentSamplers(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, const SDL_GPUTextureSamplerBinding *textureSamplerBindings, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + // Bounds-checking is for cowards too afraid to accept the inherent randomness of the universe, and the innate beauty that it creates. + cmdBuf->fragmentStageBinds.boundSamplers[i + firstSlot] = (WebGPUSampler *)textureSamplerBindings[i].sampler; + cmdBuf->fragmentStageBinds.boundTextures[i + firstSlot] = ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture); + } + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindFragmentStorageBuffers(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, SDL_GPUBuffer *const *storageBuffers, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->fragmentStageBinds.boundStorageBuffers[i + firstSlot] = ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer); + } + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindFragmentStorageTextures(SDL_GPUCommandBuffer *renderPass, Uint32 firstSlot, SDL_GPUTexture *const *storageTextures, uint32_t numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)renderPass; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->fragmentStageBinds.boundStorageTextures[i + firstSlot] = ((WebGPUTextureContainer *)storageTextures[i])->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)storageTextures[i])->activeTexture); + } + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindComputeSamplers(SDL_GPUCommandBuffer *commandBuffer, Uint32 firstSlot, const SDL_GPUTextureSamplerBinding *textureSamplerBindings, Uint32 numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->computeStageBinds.boundSamplers[i + firstSlot] = (WebGPUSampler *)textureSamplerBindings[i].sampler; + cmdBuf->computeStageBinds.boundTextures[i + firstSlot] = ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)textureSamplerBindings[i].texture)->activeTexture); + } + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindComputeStorageBuffers(SDL_GPUCommandBuffer *commandBuffer, Uint32 firstSlot, SDL_GPUBuffer *const *storageBuffers, Uint32 numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->computeStageBinds.boundReadOnlyStorageBuffers[i + firstSlot] = ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)storageBuffers[i])->activeBuffer); + } + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_BindComputeStorageTextures(SDL_GPUCommandBuffer *commandBuffer, Uint32 firstSlot, SDL_GPUTexture *const *storageTextures, Uint32 numBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + for (int i = 0; i < numBindings; i++) { + cmdBuf->computeStageBinds.boundReadOnlyStorageTextures[i + firstSlot] = ((WebGPUTextureContainer *)storageTextures[i])->activeTexture->fullTextureView; + + WEBGPU_INTERNAL_TrackTexture(cmdBuf, ((WebGPUTextureContainer *)storageTextures[i])->activeTexture); + } + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated = true; +} + +static void WEBGPU_INTERNAL_BindQueuedGraphicsResources(WebGPUCommandBuffer *cmdBuf) +{ + WGPUBindGroup bindGroups[4] = { + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_VERTEXSAMPLERSTORAGE) : NULL, + cmdBuf->vertexStageBinds.uniformBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_VERTEXUNIFORMS) : NULL, + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_FRAGMENTSAMPLERSTORAGE) : NULL, + cmdBuf->fragmentStageBinds.uniformBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_FRAGMENTUNIFORMS) : NULL, + }; + + for (int i = 0; i < 4; i++) { + if (bindGroups[i] != NULL) { + if (i == 1) { + // vert uniform + wgpuRenderPassEncoderSetBindGroup(cmdBuf->renderPassEncoder, i, bindGroups[i], 4, + cmdBuf->vertexStageBinds.currentUniformReadOffsets); + } else if (i == 3) { + // frag uniform + wgpuRenderPassEncoderSetBindGroup(cmdBuf->renderPassEncoder, i, bindGroups[i], 4, + cmdBuf->fragmentStageBinds.currentUniformReadOffsets); + } else { + wgpuRenderPassEncoderSetBindGroup(cmdBuf->renderPassEncoder, i, bindGroups[i], 0, NULL); + } + } + } + + if (cmdBuf->vertexStageBinds.shouldBindVertexBuffers) { + for (int i = 0; i < MAX_VERTEX_BUFFERS; i++) { + if (cmdBuf->vertexStageBinds.boundVertexBuffers[i].buffer == NULL) { + continue; + } + + wgpuRenderPassEncoderSetVertexBuffer(cmdBuf->renderPassEncoder, + i, cmdBuf->vertexStageBinds.boundVertexBuffers[i].buffer->buffer, + cmdBuf->vertexStageBinds.boundVertexBuffers[i].offset, WGPU_WHOLE_SIZE); + } + } + + if (cmdBuf->vertexStageBinds.shouldBindIndexBuffer) { + if (cmdBuf->vertexStageBinds.boundIndexBuffer.buffer) { + wgpuRenderPassEncoderSetIndexBuffer(cmdBuf->renderPassEncoder, + cmdBuf->vertexStageBinds.boundIndexBuffer.buffer->buffer, + cmdBuf->vertexStageBinds.indexFormat, + cmdBuf->vertexStageBinds.boundIndexBuffer.offset, + cmdBuf->vertexStageBinds.boundIndexBuffer.buffer->size); + } + } + + cmdBuf->vertexStageBinds.shouldBindIndexBuffer = false; + cmdBuf->vertexStageBinds.shouldBindVertexBuffers = false; + cmdBuf->vertexStageBinds.uniformBindGroupOutdated = false; + cmdBuf->fragmentStageBinds.uniformBindGroupOutdated = false; + cmdBuf->vertexStageBinds.samplerStorageBindGroupOutdated = false; + cmdBuf->fragmentStageBinds.samplerStorageBindGroupOutdated = false; +} + +static void WEBGPU_INTERNAL_BindQueuedComputeResources(WebGPUCommandBuffer *cmdBuf) +{ + WGPUBindGroup bindGroups[3] = { + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_COMPUTESAMPLERSTORAGE) : NULL, + cmdBuf->computeStageBinds.readWriteStorageBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_COMPUTEREADWRITESTORAGE) : NULL, + cmdBuf->computeStageBinds.uniformBindGroupOutdated ? WEBGPU_INTERNAL_GetBindGroup(cmdBuf, WEBGPU_BINDGROUP_COMPUTEUNIFORMS) : NULL, + }; + + for (int i = 0; i < 3; i++) { + if (bindGroups[i] != NULL) { + if (i == 2) { + wgpuComputePassEncoderSetBindGroup(cmdBuf->computePassEncoder, i, bindGroups[i], + 4, cmdBuf->computeStageBinds.currentUniformReadOffsets); + } else { + wgpuComputePassEncoderSetBindGroup(cmdBuf->computePassEncoder, i, bindGroups[i], 0, NULL); + } + } + } + + cmdBuf->computeStageBinds.uniformBindGroupOutdated = false; + cmdBuf->computeStageBinds.samplerStorageBindGroupOutdated = false; + cmdBuf->computeStageBinds.readWriteStorageBindGroupOutdated = false; +} + +static void WEBGPU_EndRenderPass(SDL_GPUCommandBuffer *commandBuffer) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + wgpuRenderPassEncoderEnd(cmdBuf->renderPassEncoder); + wgpuRenderPassEncoderRelease(cmdBuf->renderPassEncoder); +} + +static void WEBGPU_DrawPrimitives(SDL_GPUCommandBuffer *renderPass, Uint32 numVertices, uint32_t numInstances, uint32_t firstVertex, uint32_t firstInstance) +{ + WEBGPU_INTERNAL_EnsureGraphicsPipelineIsBound((WebGPUCommandBuffer *)renderPass); + + WEBGPU_INTERNAL_BindQueuedGraphicsResources((WebGPUCommandBuffer *)renderPass); + wgpuRenderPassEncoderDraw(((WebGPUCommandBuffer *)renderPass)->renderPassEncoder, numVertices, numInstances, firstVertex, firstInstance); +} + +static void WEBGPU_DrawPrimitivesIndirect(SDL_GPUCommandBuffer *renderPass, SDL_GPUBuffer *buffer, Uint32 offset, uint32_t drawCount) +{ + WEBGPU_INTERNAL_EnsureGraphicsPipelineIsBound((WebGPUCommandBuffer *)renderPass); + + WEBGPU_INTERNAL_BindQueuedGraphicsResources((WebGPUCommandBuffer *)renderPass); + + for (int i = 0; i < drawCount; i++) { + // ZEUS! + // WebGPU, unlike other (cough cough good) API's, doesn't support multi-draw indirect rendering. + // So, we have to do this. + // NOTE: Dawn actually supports multi-draw indirect. Maybe we should add conditional support for it? + wgpuRenderPassEncoderDrawIndirect(((WebGPUCommandBuffer *)renderPass)->renderPassEncoder, + ((WebGPUBufferContainer *)buffer)->activeBuffer->buffer, + (size_t)offset + (i * 16)); + } +} + +static void WEBGPU_DrawIndexedPrimitives(SDL_GPUCommandBuffer *renderPass, Uint32 numIndices, uint32_t numInstances, uint32_t firstIndex, Sint32 vertexOffset, uint32_t firstInstance) +{ + WEBGPU_INTERNAL_EnsureGraphicsPipelineIsBound((WebGPUCommandBuffer *)renderPass); + + WEBGPU_INTERNAL_BindQueuedGraphicsResources((WebGPUCommandBuffer *)renderPass); + wgpuRenderPassEncoderDrawIndexed(((WebGPUCommandBuffer *)renderPass)->renderPassEncoder, numIndices, numInstances, firstIndex, vertexOffset, firstInstance); +} + +static void WEBGPU_DrawIndexedPrimitivesIndirect(SDL_GPUCommandBuffer *renderPass, SDL_GPUBuffer *buffer, Uint32 offset, uint32_t drawCount) +{ + WEBGPU_INTERNAL_EnsureGraphicsPipelineIsBound((WebGPUCommandBuffer *)renderPass); + + WEBGPU_INTERNAL_BindQueuedGraphicsResources((WebGPUCommandBuffer *)renderPass); + + for (int i = 0; i < drawCount; i++) { + wgpuRenderPassEncoderDrawIndexedIndirect(((WebGPUCommandBuffer *)renderPass)->renderPassEncoder, + ((WebGPUBufferContainer *)buffer)->activeBuffer->buffer, + (size_t)offset + (i * 20)); + } +} + +static void WEBGPU_SetStencilReference(SDL_GPUCommandBuffer *commandBuffer, Uint8 reference) +{ + wgpuRenderPassEncoderSetStencilReference(((WebGPUCommandBuffer *)commandBuffer)->renderPassEncoder, reference); +} + +static void WEBGPU_INTERNAL_BindComputeReadWriteStorageTextures(WebGPUCommandBuffer *cmdBuf, + const SDL_GPUStorageTextureReadWriteBinding *bindings, Uint32 numBindings) +{ + for (int i = 0; i < numBindings; i++) { + if (bindings[i].cycle) { + WEBGPU_INTERNAL_CycleTextureContainer(cmdBuf->renderer, (WebGPUTextureContainer *)bindings[i].texture); + } + + WebGPUTextureContainer *texture = (WebGPUTextureContainer *)bindings[i].texture; + Uint32 index = WEBGPU_INTERNAL_GetTextureViewIndex(bindings[i].layer, bindings[i].mip_level, texture->header.info.num_levels); + + cmdBuf->computeStageBinds.boundReadWriteStorageTextures[i] = texture->activeTexture->textureViews[index]; + WEBGPU_INTERNAL_TrackTexture(cmdBuf, texture->activeTexture); + } + cmdBuf->computeStageBinds.readWriteStorageBindGroupOutdated = true; +} + +static void WEBGPU_INTERNAL_BindComputeReadWriteStorageBuffers(WebGPUCommandBuffer *cmdBuf, + const SDL_GPUStorageBufferReadWriteBinding *bindings, Uint32 numBindings) +{ + for (int i = 0; i < numBindings; i++) { + if (bindings[i].cycle) { + WEBGPU_INTERNAL_CycleBufferContainer(cmdBuf->renderer, (WebGPUBufferContainer *)bindings[i].buffer); + } + + cmdBuf->computeStageBinds.boundReadWriteStorageBuffers[i] = ((WebGPUBufferContainer *)bindings[i].buffer)->activeBuffer; + + WEBGPU_INTERNAL_TrackBuffer(cmdBuf, ((WebGPUBufferContainer *)bindings[i].buffer)->activeBuffer); + } + cmdBuf->computeStageBinds.readWriteStorageBindGroupOutdated = true; +} + +static void WEBGPU_BeginComputePass(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUStorageTextureReadWriteBinding *storageTextureBindings, Uint32 numStorageTextureBindings, + const SDL_GPUStorageBufferReadWriteBinding *storageBufferBindings, Uint32 numStorageBufferBindings) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WGPUComputePassDescriptor passDesc; + + passDesc.label = (WGPUStringView){ NULL, 0 }; + passDesc.nextInChain = NULL; + // Timestamps are unsupported in SDLGPU. + // 'erm acktually Cosmonaut made a PoC query API hghehgehg' Biden Blast!!! + passDesc.timestampWrites = NULL; + + cmdBuf->computePassEncoder = wgpuCommandEncoderBeginComputePass(cmdBuf->encoder, &passDesc); + if (cmdBuf->computePassEncoder == NULL) { + SDL_SetError("Could not begin compute pass!"); + return; + } + + WEBGPU_INTERNAL_ClearComputePassBindings(cmdBuf, true); + + WEBGPU_INTERNAL_BindComputeReadWriteStorageTextures(cmdBuf, storageTextureBindings, numStorageTextureBindings); + WEBGPU_INTERNAL_BindComputeReadWriteStorageBuffers(cmdBuf, storageBufferBindings, numStorageBufferBindings); + cmdBuf->hasBoundComputePipeline = false; +} + +static void WEBGPU_EndComputePass(SDL_GPUCommandBuffer *computePass) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)computePass; + wgpuComputePassEncoderEnd(cmdBuf->computePassEncoder); + + wgpuComputePassEncoderRelease(cmdBuf->computePassEncoder); +} + +static void WEBGPU_DispatchCompute(SDL_GPUCommandBuffer *commandBuffer, Uint32 groupcountX, Uint32 groupcountY, Uint32 groupcountZ) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + WEBGPU_INTERNAL_EnsureComputePipelineIsBound(cmdBuf); + + WEBGPU_INTERNAL_BindQueuedComputeResources(cmdBuf); + wgpuComputePassEncoderDispatchWorkgroups(cmdBuf->computePassEncoder, groupcountX, groupcountY, groupcountZ); +} + +static void WEBGPU_DispatchComputeIndirect(SDL_GPUCommandBuffer *commandBuffer, SDL_GPUBuffer *buffer, Uint32 offset) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + + WEBGPU_INTERNAL_EnsureComputePipelineIsBound(cmdBuf); + + WEBGPU_INTERNAL_BindQueuedComputeResources(cmdBuf); + wgpuComputePassEncoderDispatchWorkgroupsIndirect(cmdBuf->computePassEncoder, ((WebGPUBufferContainer *)buffer)->activeBuffer->buffer, offset); +} + +static void WEBGPU_INTERNAL_UploadQueuedUniformData(WebGPUCommandBuffer *cmdBuf) +{ + for (int i = 0; i < cmdBuf->numQueuedUniformUploads; i++) { + WebGPUQueuedUniformDataUpload upload = cmdBuf->queuedUniformUploads[i]; + + wgpuQueueWriteBuffer(cmdBuf->queue, cmdBuf->renderer->uniformBuffers[upload.slot]->activeBuffer->buffer, upload.offset, upload.data, upload.length); + SDL_free(upload.data); + } +} + +static bool WEBGPU_Submit(SDL_GPUCommandBuffer *commandBuffer) +{ + WebGPUCommandBuffer *wrapper = (WebGPUCommandBuffer *)commandBuffer; + WebGPUSubmittedCommandBuffer *submitted = NULL; + WebGPURenderer *renderer = wrapper->renderer; + + SDL_LockMutex(renderer->submittingCommandBufferLock); + + WEBGPU_INTERNAL_UploadQueuedUniformData(wrapper); + +#ifdef _MSC_VER + WGPUCommandBuffer cmdBuf = wgpuCommandEncoderFinish((((WebGPUCommandBuffer *)commandBuffer)->encoder), &(WGPUCommandBufferDescriptor)WGPU_COMMAND_BUFFER_DESCRIPTOR_INIT); +#else + WGPUCommandBuffer cmdBuf = wgpuCommandEncoderFinish((((WebGPUCommandBuffer *)commandBuffer)->encoder), &WGPU_COMMAND_BUFFER_DESCRIPTOR_INIT); +#endif + + if (!cmdBuf) { + SDL_free(commandBuffer); + SDL_UnlockMutex(renderer->submittingCommandBufferLock); + return false; + } + + wgpuQueueSubmit(wrapper->queue, 1, &cmdBuf); + + wrapper->submitted.fence = WEBGPU_INTERNAL_CreateFence(wrapper->queue); + + submitted = SDL_calloc(1, sizeof(*submitted)); + *submitted = wrapper->submitted; + + WEBGPU_INTERNAL_InsertElementIntoArray(wrapper->renderer->submittedCommandBuffers, wrapper->renderer->submittedCommandBufferCapacity, + wrapper->renderer->submittedCommandBufferCount, WebGPUSubmittedCommandBuffer *, submitted); + +#ifndef __EMSCRIPTEN__ + for (int i = 0; i < wrapper->surfaceCount; i++) { + wgpuSurfacePresent(wrapper->surfaces[i]); + } +#endif + for (int i = 0; i < wrapper->swapchainTextureCount; i++) { + WebGPUTextureContainer *container = wrapper->acquiredSwapchainTextures[i]; + + WEBGPU_INTERNAL_QueueTextureContainerForRelease(renderer, container); + } + wgpuCommandEncoderRelease(wrapper->encoder); + wgpuCommandBufferRelease(cmdBuf); + + SDL_AtomicIncRef(&renderer->numSubmissions); + + // We'll be freeing the "command buffer", so any usage of it will be undefined behaviour. + // Don't. The docs tell you not to. + + WEBGPU_INTERNAL_FreeCommandBuffer(wrapper); + + SDL_UnlockMutex(renderer->submittingCommandBufferLock); + + return true; +} + +static SDL_GPUFence *WEBGPU_SubmitAndAcquireFence(SDL_GPUCommandBuffer *commandBuffer) +{ + WGPUQueue queue = ((WebGPUCommandBuffer *)commandBuffer)->queue; + WEBGPU_Submit(commandBuffer); + + // I hate this. I'm not sure why I do, but I do. -- TheStickmahn + return (SDL_GPUFence *)WEBGPU_INTERNAL_CreateFence(queue); +} + +static void WEBGPU_DestroyDevice(SDL_GPUDevice *device) + +{ + WebGPURenderer *renderer = (WebGPURenderer *)device->driverData; + WEBGPU_Wait(device->driverData); + + renderer->destroyingSelf = true; + + SDL_LockMutex(renderer->destroyingSelfLock); + + WEBGPU_INTERNAL_ReleaseUniformResources(renderer); + WEBGPU_INTERNAL_ReleaseBlitResources(renderer); + + while (renderer->queuedDestroyCount > 0 || renderer->submittedCommandBufferCount > 0) { + WEBGPU_INTERNAL_HandlePendingDestroys(renderer); + WEBGPU_INTERNAL_Spin(); + } + + SDL_DestroyHashTable(renderer->bindGroupHashTable); + + // Destroying mutexes + SDL_DestroyMutex(renderer->submittingCommandBufferLock); + + wgpuQueueRelease(renderer->queue); + // FIXME: Releasing the device here causes memory leaks (according to ASan). + // Those memory leaks are also entirely illegible. I have NO idea why they happen since + // ASan straight up just blames them on random BS. We're releasing the device here since + // Emscripten doesn't like it if we don't, but I have NO way of knowing what's wrong here. + wgpuDeviceRelease(renderer->device); + wgpuAdapterRelease(renderer->adapter); + wgpuInstanceRelease(renderer->instance); + + SDL_DestroyProperties(renderer->props); + SDL_UnlockMutex(renderer->destroyingSelfLock); + SDL_DestroyMutex(renderer->destroyingSelfLock); + SDL_free(renderer->blitPipelines); + SDL_free(renderer->submittedCommandBuffers); + SDL_free(renderer); + SDL_free(device); +} + +static void WEBGPU_ReleaseWindow(SDL_GPURenderer *driverData, SDL_Window *window) +{ + WebGPUWindowData *windowData = SDL_GetPointerProperty(window->props, WINDOW_PROPERTY_DATA, NULL); + if (windowData == NULL) { + return; + } + + SDL_WGPU_DestroySurface(windowData->surface); + SDL_ClearProperty(window->props, WINDOW_PROPERTY_DATA); + + SDL_free(windowData); +} + +static void WEBGPU_DownloadFromTexture(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUTextureRegion *source, const SDL_GPUTextureTransferInfo *destination) +{ + WGPUTexelCopyTextureInfo sourceInfo; + WGPUTexelCopyBufferInfo destinationInfo; + + // If the user's given us an explicit pixels_per_row we'll use that + Uint32 unpaddedBPR = BytesPerRow(destination->pixels_per_row != 0 ? destination->pixels_per_row : source->w, ((WebGPUTextureContainer *)source->texture)->header.info.format); + Uint32 paddedBPR = ALIGN_VALUE(unpaddedBPR, 256); + bool bprMismatch = unpaddedBPR != paddedBPR; + + // This buffer is used to turn the stinky padded image data into non-stinky good image data + WebGPUBufferContainer *interimDownloadBuffer = NULL; + + if (bprMismatch) { + interimDownloadBuffer = WEBGPU_INTERNAL_CreateBufferContainer(((WebGPUCommandBuffer *)commandBuffer)->renderer, ((Uint64)paddedBPR * source->h) * SDL_min(source->d, 1), 0, WEBGPU_BUFFER_TYPE_TRANSFER_GPUONLY, false, "Interim Download Buffer"); + } + + sourceInfo.aspect = WGPUTextureAspect_All; + sourceInfo.texture = ((WebGPUTextureContainer *)source->texture)->activeTexture->texture; + sourceInfo.mipLevel = source->mip_level; + sourceInfo.origin = (WGPUOrigin3D){ .x = source->x, .y = source->y, .z = source->z + source->layer }; + + destinationInfo.buffer = bprMismatch ? interimDownloadBuffer->activeBuffer->buffer : ((WebGPUBufferContainer *)destination->transfer_buffer)->activeBuffer->buffer; + destinationInfo.layout = (WGPUTexelCopyBufferLayout){ + .bytesPerRow = paddedBPR, + .offset = bprMismatch ? 0 : destination->offset, + .rowsPerImage = destination->rows_per_layer != 0 ? destination->rows_per_layer : source->h, + }; + + Uint32 blockWidth = Texture_GetBlockWidth(((WebGPUTextureContainer *)source->texture)->activeTexture->format); + Uint32 blockHeight = Texture_GetBlockHeight(((WebGPUTextureContainer *)source->texture)->activeTexture->format); + + // FIXME: 3D texture layers will almost certainly also have to be unpadded! + // Kill me, please. + wgpuCommandEncoderCopyTextureToBuffer(((WebGPUCommandBuffer *)commandBuffer)->encoder, &sourceInfo, &destinationInfo, &(WGPUExtent3D){ ALIGN_VALUE(source->w, blockWidth), ALIGN_VALUE(source->h, blockHeight), source->d }); + + if (bprMismatch) { + // Since WebGPU sucks and inherits D3D12's horrid 256-byte BPR alignment rule we can't just copy the data 1 to 1. + // So, we need to do this unpadding nonsense. We first copy the texture to an interim buffer and then for each row + // we copy the unpadded data from it into the user-given transfer buffer. + // + // Why do you do these things to me WebGPU? + for (int i = 0; i < (destination->rows_per_layer != 0 ? destination->rows_per_layer : source->h); i++) { + wgpuCommandEncoderCopyBufferToBuffer(((WebGPUCommandBuffer *)commandBuffer)->encoder, interimDownloadBuffer->activeBuffer->buffer, (Uint64)i * paddedBPR, ((WebGPUBufferContainer *)destination->transfer_buffer)->activeBuffer->buffer, destination->offset + (Uint64)i * unpaddedBPR, unpaddedBPR); + } + + WEBGPU_INTERNAL_QueueBufferContainerForRelease(((WebGPUCommandBuffer *)commandBuffer)->renderer, interimDownloadBuffer); + } +} + +static void WEBGPU_Blit(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUBlitInfo *info) +{ + WebGPURenderer *renderer = ((WebGPUCommandBuffer *)commandBuffer)->renderer; + + SDL_GPU_BlitCommon( + commandBuffer, + info, + renderer->blitResources.blitLinearSampler, + renderer->blitResources.blitNearestSampler, + renderer->blitResources.blitVertexShader, + renderer->blitResources.blit2DShader, + renderer->blitResources.blit2DArrayShader, + renderer->blitResources.blit3DShader, + renderer->blitResources.blitCubeShader, + renderer->blitResources.blitCubeArrayShader, + &renderer->blitPipelines, + &renderer->blitPipelineCount, + &renderer->blitPipelineCapacity); +} + +static SDL_GPUTextureFormat WEBGPU_GetSwapchainTextureFormat(SDL_GPURenderer *device, SDL_Window *window) +{ + WebGPUWindowData *windowData = SDL_GetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, NULL); + + if (windowData != NULL) { + return windowData->shouldUseFallbackFormat ? SwapchainCompositionToFallbackSDLFormat[windowData->swapchainComposition] : SwapchainCompositionToSDLFormat[windowData->swapchainComposition]; + } else { + SDL_SetError("Window pointer is NULL!"); + return SDL_GPU_TEXTUREFORMAT_INVALID; + } +} + +static bool WEBGPU_SupportsSwapchainComposition(SDL_GPURenderer *driverData, SDL_Window *window, SDL_GPUSwapchainComposition swapchainComposition) +{ + WebGPUWindowData *windowData = SDL_GetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, NULL); + WGPUSurfaceCapabilities caps = { 0 }; + bool supportsComposition = false; + + if (windowData == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "WindowData pointer is NULL!"); + SDL_SetError("WindowData pointer is NULL! Did you call SDL_ClaimWindowForGPUDevice?"); + return false; + } + + WGPUStatus getCapsStatus = wgpuSurfaceGetCapabilities(windowData->surface, ((WebGPURenderer *)driverData)->adapter, &caps); + + if (getCapsStatus == WGPUStatus_Error) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to get surface capabilities!"); + SDL_SetError("Failed to get surface capabilities!"); + return false; + } + + for (int i = 0; i < caps.formatCount; i++) { + if (caps.formats[i] == SwapchainCompositionToFormat[swapchainComposition]) { + supportsComposition = true; + windowData->shouldUseFallbackFormat = false; + + break; + } else if (caps.formats[i] == SwapchainCompositionToFallbackFormat[swapchainComposition]) { + supportsComposition = true; + windowData->shouldUseFallbackFormat = true; + + break; + } + } + + wgpuSurfaceCapabilitiesFreeMembers(caps); + return supportsComposition; +} + +static bool WEBGPU_SupportsPresentMode(SDL_GPURenderer *driverData, SDL_Window *window, SDL_GPUPresentMode presentMode) +{ + WebGPUWindowData *windowData = SDL_GetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, NULL); + WGPUSurfaceCapabilities caps = { 0 }; + bool supportsPresentMode = false; + + if (windowData == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "WindowData pointer is NULL!"); + SDL_SetError("WindowData pointer is NULL! Did you call SDL_ClaimWindowForGPUDevice?"); + return false; + } + + WGPUStatus getCapsStatus = wgpuSurfaceGetCapabilities(windowData->surface, ((WebGPURenderer *)driverData)->adapter, &caps); + + if (getCapsStatus == WGPUStatus_Error) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to get surface capabilities!"); + SDL_SetError("Failed to get surface capabilities!"); + return false; + } + + for (int i = 0; i < caps.presentModeCount; i++) { + if (caps.presentModes[i] == SDLToWebGPU_PresentMode[presentMode]) { + supportsPresentMode = true; + goto finish; + } + } + + goto finish; + +finish: + wgpuSurfaceCapabilitiesFreeMembers(caps); + return supportsPresentMode; +} + +static bool WEBGPU_SetSwapchainParameters(SDL_GPURenderer *driverData, SDL_Window *window, + SDL_GPUSwapchainComposition swapchainComposition, SDL_GPUPresentMode presentMode) +{ + WebGPUWindowData *windowData = SDL_GetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, NULL); + + if (!WEBGPU_SupportsSwapchainComposition(driverData, window, swapchainComposition)) { + SDL_SetError("Window does not support swapchainComposition!"); + return false; + } + + if (!WEBGPU_SupportsPresentMode(driverData, window, presentMode)) { + SDL_SetError("Window does not support presentMode!"); + return false; + } + + windowData->presentMode = presentMode; + windowData->swapchainComposition = swapchainComposition; + windowData->surfaceDirty = true; + + return true; +} + +static bool WEBGPU_SupportsSampleCount(SDL_GPURenderer *driverData, SDL_GPUTextureFormat format, SDL_GPUSampleCount sampleCount) +{ + // TODO: Check format. I really don't wanna write another one of those damn functions 🥀 + if (sampleCount == SDL_GPU_SAMPLECOUNT_2 || sampleCount == SDL_GPU_SAMPLECOUNT_8) { + SDL_SetError("WebGPU only supports sample counts 1 and 4."); + return false; + } + + return true; +} + +static void WEBGPU_DownloadFromBuffer(SDL_GPUCommandBuffer *commandBuffer, const SDL_GPUBufferRegion *source, const SDL_GPUTransferBufferLocation *destination) +{ + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WEBGPU_INTERNAL_CopyBufferToBuffer(cmdBuf->encoder, (WebGPUBufferContainer *)source->buffer, source->offset, + (WebGPUBufferContainer *)destination->transfer_buffer, destination->offset, + ALIGN_VALUE(source->size, 4)); +} + +static bool WEBGPU_SetAllowedFramesInFlight(SDL_GPURenderer *driverData, Uint32 allowedFramesInFlight) +{ + // NOTE: Not entirely sure this is correct? Should we do something if + // allowedFramesInFlight < renderer->maxFramesInFlight & swapchain texture has been acquired? + + if (allowedFramesInFlight < 1 || allowedFramesInFlight > 3) { + SDL_SetError(allowedFramesInFlight < 1 ? "allowedFramesInFlight must be >= 1!" : "allowedFramesInFlight must be <= 3!"); + return false; + } + + ((WebGPURenderer *)driverData)->maxFramesInFlight = allowedFramesInFlight; + return true; +}; + +static void WEBGPU_INTERNAL_InitUniformResources(WebGPURenderer *renderer) +{ + for (int i = 0; i < (MAX_UNIFORM_BUFFERS_PER_STAGE * 3); i++) { + // 1048576 == 1MiB == 2²⁰ + renderer->uniformBuffers[i] = WEBGPU_INTERNAL_CreateBufferContainer(renderer, DEFAULT_UNIFORM_BUFFER_SIZE, 0, WEBGPU_BUFFER_TYPE_UNIFORM, false, NULL); + } + + for (int i = 0; i < 3; i++) { + WGPUBindGroupLayoutEntry *layoutEntries; + WGPUBindGroupEntry *entries; + + WGPUBindGroupLayoutDescriptor layoutDesc = { 0 }; + WGPUBindGroupDescriptor desc = { 0 }; + + WGPUBindGroupLayout layout; + + layoutEntries = SDL_calloc(MAX_UNIFORM_BUFFERS_PER_STAGE, sizeof(*layoutEntries)); + entries = SDL_calloc(MAX_UNIFORM_BUFFERS_PER_STAGE, sizeof(*entries)); + + for (int j = 0; j < MAX_UNIFORM_BUFFERS_PER_STAGE; j++) { + layoutEntries[j] = (WGPUBindGroupLayoutEntry){ 0 }; + layoutEntries[j].nextInChain = NULL; + layoutEntries[j].binding = j; + layoutEntries[j].buffer = (WGPUBufferBindingLayout){ + .minBindingSize = 0, + .nextInChain = NULL, + .hasDynamicOffset = true, + .type = WGPUBufferBindingType_Uniform, + }; + + if (i == 0) { + layoutEntries[j].visibility = WGPUShaderStage_Vertex; + } else if (i == 1) { + layoutEntries[j].visibility = WGPUShaderStage_Fragment; + } else if (i == 2) { + layoutEntries[j].visibility = WGPUShaderStage_Compute; + } + + entries[j] = (WGPUBindGroupEntry){ + .buffer = renderer->uniformBuffers[j + (i * MAX_UNIFORM_BUFFERS_PER_STAGE)]->activeBuffer->buffer, + .size = UNIFORM_BUFFER_SIZE, + .nextInChain = NULL, + .binding = j, + .offset = 0, + }; + } + + layoutDesc.entries = layoutEntries; + layoutDesc.entryCount = MAX_UNIFORM_BUFFERS_PER_STAGE; + + layout = wgpuDeviceCreateBindGroupLayout(renderer->device, &layoutDesc); + + desc.layout = layout; + desc.entries = entries; + desc.entryCount = MAX_UNIFORM_BUFFERS_PER_STAGE; + + renderer->uniformBufferBindGroups[i] = wgpuDeviceCreateBindGroup(renderer->device, &desc); + renderer->uniformBufferBindGroupLayouts[i] = layout; + + SDL_free(layoutEntries); + SDL_free(entries); + } +} + +static bool WEBGPU_Cancel(SDL_GPUCommandBuffer *commandBuffer) +{ + // this function sucks but it's better than nothing + + WebGPUCommandBuffer *cmdBuf = (WebGPUCommandBuffer *)commandBuffer; + WGPUCommandBuffer finishedCmdBuf = NULL; + + if (cmdBuf->common.render_pass.in_progress) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unresolved render pass, cannot cancel!"); + return false; + } else if (cmdBuf->common.compute_pass.in_progress) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unresolved compute pass, cannot cancel!"); + return false; + } else if (cmdBuf->common.copy_pass.in_progress) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unresolved copy pass, cannot cancel!"); + return false; + } + + // Since WebGPU sucks, we can't just "cancel" a command encoder. + // We'll have to finish the command encoder and then just drop it. + finishedCmdBuf = wgpuCommandEncoderFinish(cmdBuf->encoder, NULL); + + if (!finishedCmdBuf) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to finish command encoder, cannot cancel!"); + return false; + } + + wgpuCommandBufferRelease(finishedCmdBuf); + wgpuCommandEncoderRelease(cmdBuf->encoder); + + for (int i = 0; i < cmdBuf->numQueuedUniformUploads; i++) { + WebGPUQueuedUniformDataUpload upload = cmdBuf->queuedUniformUploads[i]; + SDL_free(upload.data); + } + + WEBGPU_INTERNAL_FreeCommandBuffer(cmdBuf); + + return true; +} + +// -- UNIMPLEMENTED FUNCTIONS -- +static bool WEBGPU_WaitForSwapchain(SDL_GPURenderer *driverData, SDL_Window *window) +{ + // WEBGPU_WaitForSwapchain is unimplemented since WebGPU abstracts away the swapchain from the user. + return true; +} + +// NOTE: A lot of this is guesswork at best. WebGPU really has no proper documentation. +static bool WEBGPU_SupportsTextureFormat(SDL_GPURenderer *driverData, SDL_GPUTextureFormat format, SDL_GPUTextureType type, SDL_GPUTextureUsageFlags usage) +{ + // this is horrible garbage code and I hate it so much + + bool hasStorageUsage = usage & (SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ | SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ | + SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE | SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE); + bool hasReadWriteStorageUsage = (usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ) || usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE; + bool hasDepthUsage = usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET; + bool hasColorTargetUsage = usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET; + bool hasRenderUsage = usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET; + bool hasSamplerUsage = usage & SDL_GPU_TEXTUREUSAGE_SAMPLER; + // TODO: Check the type + + WebGPURenderer *renderer = (WebGPURenderer *)driverData; + + if (SDLToWebGPU_TextureFormat[format] == WGPUTextureFormat_Undefined) { + return false; + } + + switch (format) { + case SDL_GPU_TEXTUREFORMAT_INVALID: + case SDL_GPU_TEXTUREFORMAT_A8_UNORM: + case SDL_GPU_TEXTUREFORMAT_B5G6R5_UNORM: + case SDL_GPU_TEXTUREFORMAT_B5G5R5A1_UNORM: + case SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_FLOAT: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_FLOAT: + return false; + case SDL_GPU_TEXTUREFORMAT_R16_UNORM: + case SDL_GPU_TEXTUREFORMAT_R16G16_UNORM: + case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UNORM: + case SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM: + return !hasDepthUsage && !hasReadWriteStorageUsage && !hasSamplerUsage && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureFormatsTier1); + case SDL_GPU_TEXTUREFORMAT_R8_UNORM: + case SDL_GPU_TEXTUREFORMAT_R8G8_UNORM: + case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM: + case SDL_GPU_TEXTUREFORMAT_R8_SNORM: + case SDL_GPU_TEXTUREFORMAT_R8G8_SNORM: + case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_SNORM: + case SDL_GPU_TEXTUREFORMAT_R16_SNORM: + case SDL_GPU_TEXTUREFORMAT_R16G16_SNORM: + case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM: + case SDL_GPU_TEXTUREFORMAT_R16_FLOAT: + case SDL_GPU_TEXTUREFORMAT_R16G16_FLOAT: + case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT: + case SDL_GPU_TEXTUREFORMAT_R8_UINT: + case SDL_GPU_TEXTUREFORMAT_R8G8_UINT: + case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UINT: + case SDL_GPU_TEXTUREFORMAT_R16_UINT: + case SDL_GPU_TEXTUREFORMAT_R16G16_UINT: + case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UINT: + case SDL_GPU_TEXTUREFORMAT_R8_INT: + case SDL_GPU_TEXTUREFORMAT_R8G8_INT: + case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_INT: + case SDL_GPU_TEXTUREFORMAT_R16_INT: + case SDL_GPU_TEXTUREFORMAT_R16G16_INT: + case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_INT: + case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_R32G32_FLOAT: + case SDL_GPU_TEXTUREFORMAT_R32G32_UINT: + case SDL_GPU_TEXTUREFORMAT_R32G32_INT: + case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_UINT: + case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_INT: + return !hasDepthUsage && !hasReadWriteStorageUsage; + case SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM: + case SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB: + if (hasStorageUsage && !hasReadWriteStorageUsage && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_BGRA8UnormStorage)) { + return true; + } else if (!hasDepthUsage && !hasReadWriteStorageUsage) { + return true; + } else { + return false; + } + case SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC4_R_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC5_RG_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM: + case SDL_GPU_TEXTUREFORMAT_BC6H_RGB_FLOAT: + case SDL_GPU_TEXTUREFORMAT_BC6H_RGB_UFLOAT: + case SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM_SRGB: + return !hasRenderUsage && !hasDepthUsage && !hasReadWriteStorageUsage && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureCompressionBC); + case SDL_GPU_TEXTUREFORMAT_R32_FLOAT: + case SDL_GPU_TEXTUREFORMAT_R32_UINT: + case SDL_GPU_TEXTUREFORMAT_R32_INT: + return !hasDepthUsage; + case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT: + if (!WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_Float32Filterable)) { + if (!WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_Float32Blendable)) { + SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "While Float32 textures are supported, " + "they're not filterable nor blendable as neither feature is supported on this device."); + } else { + SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "While Float32 textures are supported, " + "they're not filterable as the required WebGPU feature is not supported on this device."); + } + } else if (!WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_Float32Blendable)) { + SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "While Float32 textures are supported, " + "they're not blendable as the required WebGPU feature is not supported on this device."); + } + + return !hasDepthUsage && !hasReadWriteStorageUsage; + case SDL_GPU_TEXTUREFORMAT_R11G11B10_UFLOAT: + if (hasColorTargetUsage) { + return WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_RG11B10UfloatRenderable) && !hasDepthUsage && !hasReadWriteStorageUsage; + } else { + return false; + } + case SDL_GPU_TEXTUREFORMAT_D16_UNORM: + case SDL_GPU_TEXTUREFORMAT_D24_UNORM: + case SDL_GPU_TEXTUREFORMAT_D32_FLOAT: + case SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT: + return hasDepthUsage && !hasReadWriteStorageUsage; + case SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT: + return hasDepthUsage && !hasReadWriteStorageUsage && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_Depth32FloatStencil8); + case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM: + case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM_SRGB: + case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM_SRGB: + return !hasRenderUsage && !hasDepthUsage && !hasReadWriteStorageUsage && WEBGPU_INTERNAL_IsFeatureEnabled(renderer, WGPUFeatureName_TextureCompressionASTC); + default: + SDL_assert(!"Unsupported SDL_GPUTextureFormat"); + return false; + } +} + +// -- UNSUPPORTED FUNCTIONS -- +static void WEBGPU_SetBufferName(SDL_GPURenderer *device, SDL_GPUBuffer *buffer, const char *text) +{ + // No-op. +} + +static void WEBGPU_SetTextureName(SDL_GPURenderer *device, SDL_GPUTexture *texture, const char *text) +{ + // No-op. +} + +static XrResult WEBGPU_DestroyXRSwapchain( + SDL_GPURenderer *driverData, + XrSwapchain swapchain, + SDL_GPUTexture **swapchainImages) +{ + return XR_ERROR_FUNCTION_UNSUPPORTED; +} + +static SDL_GPUTextureFormat *WEBGPU_GetXRSwapchainFormats( + SDL_GPURenderer *driverData, + XrSession session, + int *num_formats) +{ + return NULL; +} + +static XrResult WEBGPU_CreateXRSwapchain( + SDL_GPURenderer *driverData, + XrSession session, + const XrSwapchainCreateInfo *oldCreateInfo, + SDL_GPUTextureFormat format, + XrSwapchain *swapchain, + SDL_GPUTexture ***textures) +{ + return XR_ERROR_FUNCTION_UNSUPPORTED; +} + +static XrResult WEBGPU_CreateXRSession( + SDL_GPURenderer *driverData, + const XrSessionCreateInfo *createinfo, + XrSession *session) +{ + return XR_ERROR_FUNCTION_UNSUPPORTED; +} + +static bool WEBGPU_PrepareDriver(SDL_VideoDevice *this, SDL_PropertiesID props) +{ + // TODO: This. + // There used to be code here, but it hadn't been updated in a while. + // (Quite frankly, I just got annoyed that it clogged the console with debug device creation information since it uses the same functions) + return true; +} + +static SDL_GPUDevice *WEBGPU_CreateDevice(bool debugMode, bool preferLowPower, SDL_PropertiesID props) +{ + WebGPURenderer *renderer; + SDL_GPUDevice *result; + + Sint64 bindGroupsExpireAfter = SDL_GetNumberProperty(props, SDL_PROP_GPU_DEVICE_CREATE_WEBGPU_BINDGROUP_EXPIRE_AFTER_N_SUBMITS, DEFAULT_BINDGROUP_EXPIRY); + Sint64 pseudoMapThreshold = SDL_GetNumberProperty(props, SDL_PROP_GPU_DEVICE_CREATE_WEBGPU_PSEUDOMAP_BUFFERS_SMALLER_THAN, DEFAULT_PSEUDOMAP_SIZE_THRESHOLD); + + bool getAdapterSucceeded = false; + bool getDeviceSucceeded = false; + + renderer = SDL_calloc(1, sizeof(*renderer)); + + if (renderer == NULL) { + return NULL; + } + + renderer->debugMode = debugMode; + renderer->preferLowPower = preferLowPower; + renderer->props = props; + renderer->bindGroupsExpireAfter = bindGroupsExpireAfter; + renderer->pseudoMapBufferSizeThreshold = pseudoMapThreshold; + renderer->maxFramesInFlight = 2; // Default + + renderer->bindGroupHashTable = SDL_CreateHashTable(512, true, WEBGPU_INTERNAL_HashBindGroupKey, WEBGPU_INTERNAL_MatchHashedBindGroupKey, WEBGPU_INTERNAL_DestroyCachedBindGroupAndKey, NULL); + + renderer->destroyingSelfLock = SDL_CreateMutex(); + renderer->submittingCommandBufferLock = SDL_CreateMutex(); + +// I do not like MSVC. +#ifdef _MSC_VER + renderer->instance = wgpuCreateInstance(&(WGPUInstanceDescriptor)WGPU_INSTANCE_DESCRIPTOR_INIT); +#else + renderer->instance = wgpuCreateInstance(&WGPU_INSTANCE_DESCRIPTOR_INIT); +#endif + + if (!renderer->instance) { + SDL_free(renderer); + return NULL; + } + + WEBGPU_INTERNAL_RequestAdapter(renderer, &getAdapterSucceeded); + + if (!renderer->adapter) { + wgpuInstanceRelease(renderer->instance); + + SDL_free(renderer); + return NULL; + } + + WEBGPU_INTERNAL_RequestDevice(renderer, &getDeviceSucceeded); + + if (!renderer->device) { + wgpuAdapterRelease(renderer->adapter); + wgpuInstanceRelease(renderer->instance); + + SDL_free(renderer); + return NULL; + } + + renderer->queue = wgpuDeviceGetQueue(renderer->device); + + WEBGPU_INTERNAL_InitUniformResources(renderer); + WEBGPU_INTERNAL_InitBlitResources(renderer); + + result = SDL_calloc(1, sizeof(SDL_GPUDevice)); + + result->driverData = (SDL_GPURenderer *)renderer; + result->shader_formats = SDL_GPU_SHADERFORMAT_WGSL; + + ASSIGN_DRIVER(WEBGPU) + return result; +} + +SDL_GPUBootstrap WebGPUDriver = { + "webgpu", + WEBGPU_PrepareDriver, + WEBGPU_CreateDevice +}; + +#endif diff --git a/src/gpu/webgpu/webgpu.h b/src/gpu/webgpu/webgpu.h new file mode 100644 index 0000000000..262d2fb543 --- /dev/null +++ b/src/gpu/webgpu/webgpu.h @@ -0,0 +1,6770 @@ +// FIXME: This is only here because I'm lazy. Remove this header and replace it with our own. + +/** + * Copyright 2019-2023 WebGPU-Native developers + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/** @file */ + +/** + * \mainpage + * + * **Important:** *This documentation is a Work In Progress.* + * + * This is the home of WebGPU C API specification. We define here the standard + * `webgpu.h` header that all implementations should provide. + * + * For all details where behavior is not otherwise specified, `webgpu.h` has + * the same behavior as the WebGPU specification for JavaScript on the Web. + * The WebIDL-based Web specification is mapped into C as faithfully (and + * bidirectionally) as practical/possible. + * The working draft of WebGPU can be found at . + * + * The standard include directive for this header is `#include ` + * (if it is provided in a system-wide or toolchain-wide include directory). + */ + +#ifndef WEBGPU_H_ +#define WEBGPU_H_ + +#if defined(WGPU_SHARED_LIBRARY) +# if defined(_WIN32) +# if defined(WGPU_IMPLEMENTATION) +# define WGPU_EXPORT __declspec(dllexport) +# else +# define WGPU_EXPORT __declspec(dllimport) +# endif +# else // defined(_WIN32) +# if defined(WGPU_IMPLEMENTATION) +# define WGPU_EXPORT __attribute__((visibility("default"))) +# else +# define WGPU_EXPORT +# endif +# endif // defined(_WIN32) +#else // defined(WGPU_SHARED_LIBRARY) +# define WGPU_EXPORT +#endif // defined(WGPU_SHARED_LIBRARY) + +#if !defined(WGPU_OBJECT_ATTRIBUTE) +#define WGPU_OBJECT_ATTRIBUTE +#endif +#if !defined(WGPU_ENUM_ATTRIBUTE) +#define WGPU_ENUM_ATTRIBUTE +#endif +#if !defined(WGPU_STRUCTURE_ATTRIBUTE) +#define WGPU_STRUCTURE_ATTRIBUTE +#endif +#if !defined(WGPU_FUNCTION_ATTRIBUTE) +#define WGPU_FUNCTION_ATTRIBUTE +#endif +#if !defined(WGPU_NULLABLE) +#define WGPU_NULLABLE +#endif + +#include +#include +#include + +#define _wgpu_COMMA , +#if defined(__cplusplus) +# define _wgpu_ENUM_ZERO_INIT(type) type(0) +# define _wgpu_STRUCT_ZERO_INIT {} +# if __cplusplus >= 201103L +# define _wgpu_MAKE_INIT_STRUCT(type, value) (type value) +# else +# define _wgpu_MAKE_INIT_STRUCT(type, value) value +# endif +#else +# define _wgpu_ENUM_ZERO_INIT(type) (type)0 +# define _wgpu_STRUCT_ZERO_INIT {0} +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +# define _wgpu_MAKE_INIT_STRUCT(type, value) ((type) value) +# else +# define _wgpu_MAKE_INIT_STRUCT(type, value) value +# endif +#endif + +/** + * \defgroup Constants Constants + * \brief Constants. + * + * @{ + */ + +/** + * 'True' value of @ref WGPUBool. + * + * @remark It's not usually necessary to use this, as `true` (from + * `stdbool.h` or C++) casts to the same value. + */ +#define WGPU_TRUE (UINT32_C(1)) +/** + * 'False' value of @ref WGPUBool. + * + * @remark It's not usually necessary to use this, as `false` (from + * `stdbool.h` or C++) casts to the same value. + */ +#define WGPU_FALSE (UINT32_C(0)) +/** + * Indicates no array layer count is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_ARRAY_LAYER_COUNT_UNDEFINED (UINT32_MAX) +/** + * Indicates no copy stride is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_COPY_STRIDE_UNDEFINED (UINT32_MAX) +/** + * Indicates no depth clear value is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_DEPTH_CLEAR_VALUE_UNDEFINED (NAN) +/** + * Indicates no depth slice is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_DEPTH_SLICE_UNDEFINED (UINT32_MAX) +/** + * For `uint32_t` limits, indicates no limit value is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_LIMIT_U32_UNDEFINED (UINT32_MAX) +/** + * For `uint64_t` limits, indicates no limit value is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_LIMIT_U64_UNDEFINED (UINT64_MAX) +/** + * Indicates no mip level count is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_MIP_LEVEL_COUNT_UNDEFINED (UINT32_MAX) +/** + * Indicates no query set index is specified. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_QUERY_SET_INDEX_UNDEFINED (UINT32_MAX) +/** + * Sentinel value used in @ref WGPUStringView to indicate that the pointer + * is to a null-terminated string, rather than an explicitly-sized string. + */ +#define WGPU_STRLEN (SIZE_MAX) +/** + * Indicates a size extending to the end of the buffer. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_WHOLE_MAP_SIZE (SIZE_MAX) +/** + * Indicates a size extending to the end of the buffer. For more info, + * see @ref SentinelValues and the places that use this sentinel value. + */ +#define WGPU_WHOLE_SIZE (UINT64_MAX) + +/** @} */ + +/** + * \defgroup UtilityTypes Utility Types + * + * @{ + */ + +/** + * Nullable value defining a pointer+length view into a UTF-8 encoded string. + * + * Values passed into the API may use the special length value @ref WGPU_STRLEN + * to indicate a null-terminated string. + * Non-null values passed out of the API (for example as callback arguments) + * always provide an explicit length and **may or may not be null-terminated**. + * + * Some inputs to the API accept null values. Those which do not accept null + * values "default" to the empty string when null values are passed. + * + * Values are encoded as follows: + * - `{NULL, WGPU_STRLEN}`: the null value. + * - `{non_null_pointer, WGPU_STRLEN}`: a null-terminated string view. + * - `{any, 0}`: the empty string. + * - `{NULL, non_zero_length}`: not allowed (null dereference). + * - `{non_null_pointer, non_zero_length}`: an explictly-sized string view with + * size `non_zero_length` (in bytes). + * + * For info on how this is used in various places, see \ref Strings. + */ +typedef struct WGPUStringView { + WGPU_NULLABLE char const * data; + size_t length; +} WGPUStringView WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUStringView. + */ +#define WGPU_STRING_VIEW_INIT _wgpu_MAKE_INIT_STRUCT(WGPUStringView, { \ + /*.data=*/NULL _wgpu_COMMA \ + /*.length=*/WGPU_STRLEN _wgpu_COMMA \ +}) + +typedef uint64_t WGPUFlags; +typedef uint32_t WGPUBool; + +/** @} */ + +/** + * \defgroup Objects Objects + * \brief Opaque, non-dispatchable handles to WebGPU objects. + * + * @{ + */ +typedef struct WGPUAdapterImpl* WGPUAdapter WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUBindGroupImpl* WGPUBindGroup WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUBindGroupLayoutImpl* WGPUBindGroupLayout WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUBufferImpl* WGPUBuffer WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUCommandBufferImpl* WGPUCommandBuffer WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUCommandEncoderImpl* WGPUCommandEncoder WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUComputePassEncoderImpl* WGPUComputePassEncoder WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUComputePipelineImpl* WGPUComputePipeline WGPU_OBJECT_ATTRIBUTE; +/** + * TODO + * + * Releasing the last ref to a `WGPUDevice` also calls @ref wgpuDeviceDestroy. + * For more info, see @ref DeviceRelease. + */ +typedef struct WGPUDeviceImpl* WGPUDevice WGPU_OBJECT_ATTRIBUTE; +/** + * A sampleable 2D texture that may perform 0-copy YUV sampling internally. Creation of @ref WGPUExternalTexture is extremely implementation-dependent and not defined in this header. + */ +typedef struct WGPUExternalTextureImpl* WGPUExternalTexture WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUInstanceImpl* WGPUInstance WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUPipelineLayoutImpl* WGPUPipelineLayout WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUQuerySetImpl* WGPUQuerySet WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUQueueImpl* WGPUQueue WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPURenderBundleImpl* WGPURenderBundle WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPURenderBundleEncoderImpl* WGPURenderBundleEncoder WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPURenderPassEncoderImpl* WGPURenderPassEncoder WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPURenderPipelineImpl* WGPURenderPipeline WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUSamplerImpl* WGPUSampler WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUShaderModuleImpl* WGPUShaderModule WGPU_OBJECT_ATTRIBUTE; +/** + * An object used to continuously present image data to the user, see @ref Surfaces for more details. + */ +typedef struct WGPUSurfaceImpl* WGPUSurface WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUTextureImpl* WGPUTexture WGPU_OBJECT_ATTRIBUTE; +typedef struct WGPUTextureViewImpl* WGPUTextureView WGPU_OBJECT_ATTRIBUTE; + +/** @} */ + +// Structure forward declarations +struct WGPUAdapterInfo; +struct WGPUBlendComponent; +struct WGPUBufferBindingLayout; +struct WGPUBufferDescriptor; +struct WGPUColor; +struct WGPUCommandBufferDescriptor; +struct WGPUCommandEncoderDescriptor; +struct WGPUCompatibilityModeLimits; +struct WGPUCompilationMessage; +struct WGPUConstantEntry; +struct WGPUExtent3D; +struct WGPUExternalTextureBindingEntry; +struct WGPUExternalTextureBindingLayout; +struct WGPUFuture; +struct WGPUInstanceLimits; +struct WGPUMultisampleState; +struct WGPUOrigin3D; +struct WGPUPassTimestampWrites; +struct WGPUPipelineLayoutDescriptor; +struct WGPUPrimitiveState; +struct WGPUQuerySetDescriptor; +struct WGPUQueueDescriptor; +struct WGPURenderBundleDescriptor; +struct WGPURenderBundleEncoderDescriptor; +struct WGPURenderPassDepthStencilAttachment; +struct WGPURenderPassMaxDrawCount; +struct WGPURequestAdapterWebXROptions; +struct WGPUSamplerBindingLayout; +struct WGPUSamplerDescriptor; +struct WGPUShaderSourceSPIRV; +struct WGPUShaderSourceWGSL; +struct WGPUStencilFaceState; +struct WGPUStorageTextureBindingLayout; +struct WGPUSupportedFeatures; +struct WGPUSupportedInstanceFeatures; +struct WGPUSupportedWGSLLanguageFeatures; +struct WGPUSurfaceCapabilities; +struct WGPUSurfaceColorManagement; +struct WGPUSurfaceConfiguration; +struct WGPUSurfaceSourceAndroidNativeWindow; +struct WGPUSurfaceSourceMetalLayer; +struct WGPUSurfaceSourceWaylandSurface; +struct WGPUSurfaceSourceWindowsHWND; +struct WGPUSurfaceSourceXCBWindow; +struct WGPUSurfaceSourceXlibWindow; +struct WGPUSurfaceTexture; +struct WGPUTexelCopyBufferLayout; +struct WGPUTextureBindingLayout; +struct WGPUTextureBindingViewDimension; +struct WGPUTextureComponentSwizzle; +struct WGPUVertexAttribute; +struct WGPUBindGroupEntry; +struct WGPUBindGroupLayoutEntry; +struct WGPUBlendState; +struct WGPUCompilationInfo; +struct WGPUComputePassDescriptor; +struct WGPUComputeState; +struct WGPUDepthStencilState; +struct WGPUFutureWaitInfo; +struct WGPUInstanceDescriptor; +struct WGPULimits; +struct WGPURenderPassColorAttachment; +struct WGPURequestAdapterOptions; +struct WGPUShaderModuleDescriptor; +struct WGPUSurfaceDescriptor; +struct WGPUTexelCopyBufferInfo; +struct WGPUTexelCopyTextureInfo; +struct WGPUTextureComponentSwizzleDescriptor; +struct WGPUTextureDescriptor; +struct WGPUVertexBufferLayout; +struct WGPUBindGroupDescriptor; +struct WGPUBindGroupLayoutDescriptor; +struct WGPUColorTargetState; +struct WGPUComputePipelineDescriptor; +struct WGPUDeviceDescriptor; +struct WGPURenderPassDescriptor; +struct WGPUTextureViewDescriptor; +struct WGPUVertexState; +struct WGPUFragmentState; +struct WGPURenderPipelineDescriptor; + +// Callback info structure forward declarations +struct WGPUBufferMapCallbackInfo; +struct WGPUCompilationInfoCallbackInfo; +struct WGPUCreateComputePipelineAsyncCallbackInfo; +struct WGPUCreateRenderPipelineAsyncCallbackInfo; +struct WGPUDeviceLostCallbackInfo; +struct WGPUPopErrorScopeCallbackInfo; +struct WGPUQueueWorkDoneCallbackInfo; +struct WGPURequestAdapterCallbackInfo; +struct WGPURequestDeviceCallbackInfo; +struct WGPUUncapturedErrorCallbackInfo; + +/** + * \defgroup Enumerations Enumerations + * \brief Enums. + * + * @{ + */ + +typedef enum WGPUAdapterType { + WGPUAdapterType_DiscreteGPU = 0x00000001, + WGPUAdapterType_IntegratedGPU = 0x00000002, + WGPUAdapterType_CPU = 0x00000003, + WGPUAdapterType_Unknown = 0x00000004, + WGPUAdapterType_Force32 = 0x7FFFFFFF +} WGPUAdapterType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUAddressMode { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUAddressMode_Undefined = 0x00000000, + WGPUAddressMode_ClampToEdge = 0x00000001, + WGPUAddressMode_Repeat = 0x00000002, + WGPUAddressMode_MirrorRepeat = 0x00000003, + WGPUAddressMode_Force32 = 0x7FFFFFFF +} WGPUAddressMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUBackendType { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUBackendType_Undefined = 0x00000000, + WGPUBackendType_Null = 0x00000001, + WGPUBackendType_WebGPU = 0x00000002, + WGPUBackendType_D3D11 = 0x00000003, + WGPUBackendType_D3D12 = 0x00000004, + WGPUBackendType_Metal = 0x00000005, + WGPUBackendType_Vulkan = 0x00000006, + WGPUBackendType_OpenGL = 0x00000007, + WGPUBackendType_OpenGLES = 0x00000008, + WGPUBackendType_Force32 = 0x7FFFFFFF +} WGPUBackendType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUBlendFactor { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUBlendFactor_Undefined = 0x00000000, + WGPUBlendFactor_Zero = 0x00000001, + WGPUBlendFactor_One = 0x00000002, + WGPUBlendFactor_Src = 0x00000003, + WGPUBlendFactor_OneMinusSrc = 0x00000004, + WGPUBlendFactor_SrcAlpha = 0x00000005, + WGPUBlendFactor_OneMinusSrcAlpha = 0x00000006, + WGPUBlendFactor_Dst = 0x00000007, + WGPUBlendFactor_OneMinusDst = 0x00000008, + WGPUBlendFactor_DstAlpha = 0x00000009, + WGPUBlendFactor_OneMinusDstAlpha = 0x0000000A, + WGPUBlendFactor_SrcAlphaSaturated = 0x0000000B, + WGPUBlendFactor_Constant = 0x0000000C, + WGPUBlendFactor_OneMinusConstant = 0x0000000D, + WGPUBlendFactor_Src1 = 0x0000000E, + WGPUBlendFactor_OneMinusSrc1 = 0x0000000F, + WGPUBlendFactor_Src1Alpha = 0x00000010, + WGPUBlendFactor_OneMinusSrc1Alpha = 0x00000011, + WGPUBlendFactor_Force32 = 0x7FFFFFFF +} WGPUBlendFactor WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUBlendOperation { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUBlendOperation_Undefined = 0x00000000, + WGPUBlendOperation_Add = 0x00000001, + WGPUBlendOperation_Subtract = 0x00000002, + WGPUBlendOperation_ReverseSubtract = 0x00000003, + WGPUBlendOperation_Min = 0x00000004, + WGPUBlendOperation_Max = 0x00000005, + WGPUBlendOperation_Force32 = 0x7FFFFFFF +} WGPUBlendOperation WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUBufferBindingType { + /** + * `0`. Indicates that this @ref WGPUBufferBindingLayout member of + * its parent @ref WGPUBindGroupLayoutEntry is not used. + * (See also @ref SentinelValues.) + */ + WGPUBufferBindingType_BindingNotUsed = 0x00000000, + /** + * `1`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUBufferBindingType_Undefined = 0x00000001, + WGPUBufferBindingType_Uniform = 0x00000002, + WGPUBufferBindingType_Storage = 0x00000003, + WGPUBufferBindingType_ReadOnlyStorage = 0x00000004, + WGPUBufferBindingType_Force32 = 0x7FFFFFFF +} WGPUBufferBindingType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUBufferMapState { + WGPUBufferMapState_Unmapped = 0x00000001, + WGPUBufferMapState_Pending = 0x00000002, + WGPUBufferMapState_Mapped = 0x00000003, + WGPUBufferMapState_Force32 = 0x7FFFFFFF +} WGPUBufferMapState WGPU_ENUM_ATTRIBUTE; + +/** + * The callback mode controls how a callback for an asynchronous operation may be fired. See @ref Asynchronous-Operations for how these are used. + */ +typedef enum WGPUCallbackMode { + /** + * Callbacks created with `WGPUCallbackMode_WaitAnyOnly`: + * - fire when the asynchronous operation's future is passed to a call to @ref wgpuInstanceWaitAny + * AND the operation has already completed or it completes inside the call to @ref wgpuInstanceWaitAny. + */ + WGPUCallbackMode_WaitAnyOnly = 0x00000001, + /** + * Callbacks created with `WGPUCallbackMode_AllowProcessEvents`: + * - fire for the same reasons as callbacks created with `WGPUCallbackMode_WaitAnyOnly` + * - fire inside a call to @ref wgpuInstanceProcessEvents if the asynchronous operation is complete. + */ + WGPUCallbackMode_AllowProcessEvents = 0x00000002, + /** + * Callbacks created with `WGPUCallbackMode_AllowSpontaneous`: + * - fire for the same reasons as callbacks created with `WGPUCallbackMode_AllowProcessEvents` + * - **may** fire spontaneously on an arbitrary or application thread, when the WebGPU implementations discovers that the asynchronous operation is complete. + * + * Implementations _should_ fire spontaneous callbacks as soon as possible. + * + * @note Because spontaneous callbacks may fire at an arbitrary time on an arbitrary thread, applications should take extra care when acquiring locks or mutating state inside the callback. It undefined behavior to re-entrantly call into the webgpu.h API if the callback fires while inside the callstack of another webgpu.h function that is not `wgpuInstanceWaitAny` or `wgpuInstanceProcessEvents`. + */ + WGPUCallbackMode_AllowSpontaneous = 0x00000003, + WGPUCallbackMode_Force32 = 0x7FFFFFFF +} WGPUCallbackMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUCompareFunction { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUCompareFunction_Undefined = 0x00000000, + WGPUCompareFunction_Never = 0x00000001, + WGPUCompareFunction_Less = 0x00000002, + WGPUCompareFunction_Equal = 0x00000003, + WGPUCompareFunction_LessEqual = 0x00000004, + WGPUCompareFunction_Greater = 0x00000005, + WGPUCompareFunction_NotEqual = 0x00000006, + WGPUCompareFunction_GreaterEqual = 0x00000007, + WGPUCompareFunction_Always = 0x00000008, + WGPUCompareFunction_Force32 = 0x7FFFFFFF +} WGPUCompareFunction WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUCompilationInfoRequestStatus { + WGPUCompilationInfoRequestStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPUCompilationInfoRequestStatus_CallbackCancelled = 0x00000002, + WGPUCompilationInfoRequestStatus_Force32 = 0x7FFFFFFF +} WGPUCompilationInfoRequestStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUCompilationMessageType { + WGPUCompilationMessageType_Error = 0x00000001, + WGPUCompilationMessageType_Warning = 0x00000002, + WGPUCompilationMessageType_Info = 0x00000003, + WGPUCompilationMessageType_Force32 = 0x7FFFFFFF +} WGPUCompilationMessageType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUComponentSwizzle { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUComponentSwizzle_Undefined = 0x00000000, + /** + * Force its value to 0. + */ + WGPUComponentSwizzle_Zero = 0x00000001, + /** + * Force its value to 1. + */ + WGPUComponentSwizzle_One = 0x00000002, + /** + * Take its value from the red channel of the texture. + */ + WGPUComponentSwizzle_R = 0x00000003, + /** + * Take its value from the green channel of the texture. + */ + WGPUComponentSwizzle_G = 0x00000004, + /** + * Take its value from the blue channel of the texture. + */ + WGPUComponentSwizzle_B = 0x00000005, + /** + * Take its value from the alpha channel of the texture. + */ + WGPUComponentSwizzle_A = 0x00000006, + WGPUComponentSwizzle_Force32 = 0x7FFFFFFF +} WGPUComponentSwizzle WGPU_ENUM_ATTRIBUTE; + +/** + * Describes how frames are composited with other contents on the screen when @ref wgpuSurfacePresent is called. + */ +typedef enum WGPUCompositeAlphaMode { + /** + * `0`. Lets the WebGPU implementation choose the best mode (supported, and with the best performance) between @ref WGPUCompositeAlphaMode_Opaque or @ref WGPUCompositeAlphaMode_Inherit. + */ + WGPUCompositeAlphaMode_Auto = 0x00000000, + /** + * The alpha component of the image is ignored and teated as if it is always 1.0. + */ + WGPUCompositeAlphaMode_Opaque = 0x00000001, + /** + * The alpha component is respected and non-alpha components are assumed to be already multiplied with the alpha component. For example, (0.5, 0, 0, 0.5) is semi-transparent bright red. + */ + WGPUCompositeAlphaMode_Premultiplied = 0x00000002, + /** + * The alpha component is respected and non-alpha components are assumed to NOT be already multiplied with the alpha component. For example, (1.0, 0, 0, 0.5) is semi-transparent bright red. + */ + WGPUCompositeAlphaMode_Unpremultiplied = 0x00000003, + /** + * The handling of the alpha component is unknown to WebGPU and should be handled by the application using system-specific APIs. This mode may be unavailable (for example on Wasm). + */ + WGPUCompositeAlphaMode_Inherit = 0x00000004, + WGPUCompositeAlphaMode_Force32 = 0x7FFFFFFF +} WGPUCompositeAlphaMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUCreatePipelineAsyncStatus { + WGPUCreatePipelineAsyncStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPUCreatePipelineAsyncStatus_CallbackCancelled = 0x00000002, + WGPUCreatePipelineAsyncStatus_ValidationError = 0x00000003, + WGPUCreatePipelineAsyncStatus_InternalError = 0x00000004, + WGPUCreatePipelineAsyncStatus_Force32 = 0x7FFFFFFF +} WGPUCreatePipelineAsyncStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUCullMode { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUCullMode_Undefined = 0x00000000, + WGPUCullMode_None = 0x00000001, + WGPUCullMode_Front = 0x00000002, + WGPUCullMode_Back = 0x00000003, + WGPUCullMode_Force32 = 0x7FFFFFFF +} WGPUCullMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUDeviceLostReason { + WGPUDeviceLostReason_Unknown = 0x00000001, + WGPUDeviceLostReason_Destroyed = 0x00000002, + /** + * See @ref CallbackStatuses. + */ + WGPUDeviceLostReason_CallbackCancelled = 0x00000003, + WGPUDeviceLostReason_FailedCreation = 0x00000004, + WGPUDeviceLostReason_Force32 = 0x7FFFFFFF +} WGPUDeviceLostReason WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUErrorFilter { + WGPUErrorFilter_Validation = 0x00000001, + WGPUErrorFilter_OutOfMemory = 0x00000002, + WGPUErrorFilter_Internal = 0x00000003, + WGPUErrorFilter_Force32 = 0x7FFFFFFF +} WGPUErrorFilter WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUErrorType { + WGPUErrorType_NoError = 0x00000001, + WGPUErrorType_Validation = 0x00000002, + WGPUErrorType_OutOfMemory = 0x00000003, + WGPUErrorType_Internal = 0x00000004, + WGPUErrorType_Unknown = 0x00000005, + WGPUErrorType_Force32 = 0x7FFFFFFF +} WGPUErrorType WGPU_ENUM_ATTRIBUTE; + +/** + * See @ref WGPURequestAdapterOptions::featureLevel. + */ +typedef enum WGPUFeatureLevel { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUFeatureLevel_Undefined = 0x00000000, + /** + * "Compatibility" profile which can be supported on OpenGL ES 3.1 and D3D11. + */ + WGPUFeatureLevel_Compatibility = 0x00000001, + /** + * "Core" profile which can be supported on Vulkan/Metal/D3D12 (at least). + */ + WGPUFeatureLevel_Core = 0x00000002, + WGPUFeatureLevel_Force32 = 0x7FFFFFFF +} WGPUFeatureLevel WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUFeatureName { + WGPUFeatureName_CoreFeaturesAndLimits = 0x00000001, + WGPUFeatureName_DepthClipControl = 0x00000002, + WGPUFeatureName_Depth32FloatStencil8 = 0x00000003, + WGPUFeatureName_TextureCompressionBC = 0x00000004, + WGPUFeatureName_TextureCompressionBCSliced3D = 0x00000005, + WGPUFeatureName_TextureCompressionETC2 = 0x00000006, + WGPUFeatureName_TextureCompressionASTC = 0x00000007, + WGPUFeatureName_TextureCompressionASTCSliced3D = 0x00000008, + WGPUFeatureName_TimestampQuery = 0x00000009, + WGPUFeatureName_IndirectFirstInstance = 0x0000000A, + WGPUFeatureName_ShaderF16 = 0x0000000B, + WGPUFeatureName_RG11B10UfloatRenderable = 0x0000000C, + WGPUFeatureName_BGRA8UnormStorage = 0x0000000D, + WGPUFeatureName_Float32Filterable = 0x0000000E, + WGPUFeatureName_Float32Blendable = 0x0000000F, + WGPUFeatureName_ClipDistances = 0x00000010, + WGPUFeatureName_DualSourceBlending = 0x00000011, + WGPUFeatureName_Subgroups = 0x00000012, + WGPUFeatureName_TextureFormatsTier1 = 0x00000013, + WGPUFeatureName_TextureFormatsTier2 = 0x00000014, + WGPUFeatureName_PrimitiveIndex = 0x00000015, + WGPUFeatureName_TextureComponentSwizzle = 0x00000016, + WGPUFeatureName_SubgroupSizeControl = 0x00000017, + WGPUFeatureName_Force32 = 0x7FFFFFFF +} WGPUFeatureName WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUFilterMode { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUFilterMode_Undefined = 0x00000000, + WGPUFilterMode_Nearest = 0x00000001, + WGPUFilterMode_Linear = 0x00000002, + WGPUFilterMode_Force32 = 0x7FFFFFFF +} WGPUFilterMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUFrontFace { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUFrontFace_Undefined = 0x00000000, + WGPUFrontFace_CCW = 0x00000001, + WGPUFrontFace_CW = 0x00000002, + WGPUFrontFace_Force32 = 0x7FFFFFFF +} WGPUFrontFace WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUIndexFormat { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUIndexFormat_Undefined = 0x00000000, + WGPUIndexFormat_Uint16 = 0x00000001, + WGPUIndexFormat_Uint32 = 0x00000002, + WGPUIndexFormat_Force32 = 0x7FFFFFFF +} WGPUIndexFormat WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUInstanceFeatureName { + /** + * Enable use of ::wgpuInstanceWaitAny with `timeoutNS > 0`. + */ + WGPUInstanceFeatureName_TimedWaitAny = 0x00000001, + /** + * Enable passing SPIR-V shaders to @ref wgpuDeviceCreateShaderModule, + * via @ref WGPUShaderSourceSPIRV. + */ + WGPUInstanceFeatureName_ShaderSourceSPIRV = 0x00000002, + /** + * Normally, a @ref WGPUAdapter can only create a single device. If this is + * available and enabled, then adapters won't immediately expire when they + * create a device, so can be reused to make multiple devices. They may + * still expire for other reasons. + */ + WGPUInstanceFeatureName_MultipleDevicesPerAdapter = 0x00000003, + WGPUInstanceFeatureName_Force32 = 0x7FFFFFFF +} WGPUInstanceFeatureName WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPULoadOp { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPULoadOp_Undefined = 0x00000000, + WGPULoadOp_Load = 0x00000001, + WGPULoadOp_Clear = 0x00000002, + WGPULoadOp_Force32 = 0x7FFFFFFF +} WGPULoadOp WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUMapAsyncStatus { + WGPUMapAsyncStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPUMapAsyncStatus_CallbackCancelled = 0x00000002, + WGPUMapAsyncStatus_Error = 0x00000003, + WGPUMapAsyncStatus_Aborted = 0x00000004, + WGPUMapAsyncStatus_Force32 = 0x7FFFFFFF +} WGPUMapAsyncStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUMipmapFilterMode { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUMipmapFilterMode_Undefined = 0x00000000, + WGPUMipmapFilterMode_Nearest = 0x00000001, + WGPUMipmapFilterMode_Linear = 0x00000002, + WGPUMipmapFilterMode_Force32 = 0x7FFFFFFF +} WGPUMipmapFilterMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUOptionalBool { + /** + * `0`. + */ + WGPUOptionalBool_False = 0x00000000, + WGPUOptionalBool_True = 0x00000001, + WGPUOptionalBool_Undefined = 0x00000002, + WGPUOptionalBool_Force32 = 0x7FFFFFFF +} WGPUOptionalBool WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUPopErrorScopeStatus { + /** + * The error scope stack was successfully popped and a result was reported. + */ + WGPUPopErrorScopeStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPUPopErrorScopeStatus_CallbackCancelled = 0x00000002, + /** + * The error scope stack could not be popped, because it was empty. + */ + WGPUPopErrorScopeStatus_Error = 0x00000003, + WGPUPopErrorScopeStatus_Force32 = 0x7FFFFFFF +} WGPUPopErrorScopeStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUPowerPreference { + /** + * `0`. No preference. (See also @ref SentinelValues.) + */ + WGPUPowerPreference_Undefined = 0x00000000, + WGPUPowerPreference_LowPower = 0x00000001, + WGPUPowerPreference_HighPerformance = 0x00000002, + WGPUPowerPreference_Force32 = 0x7FFFFFFF +} WGPUPowerPreference WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUPredefinedColorSpace { + WGPUPredefinedColorSpace_SRGB = 0x00000001, + WGPUPredefinedColorSpace_DisplayP3 = 0x00000002, + WGPUPredefinedColorSpace_Force32 = 0x7FFFFFFF +} WGPUPredefinedColorSpace WGPU_ENUM_ATTRIBUTE; + +/** + * Describes when and in which order frames are presented on the screen when @ref wgpuSurfacePresent is called. + */ +typedef enum WGPUPresentMode { + /** + * `0`. Present mode is not specified. Use the default. + */ + WGPUPresentMode_Undefined = 0x00000000, + /** + * The presentation of the image to the user waits for the next vertical blanking period to update in a first-in, first-out manner. + * Tearing cannot be observed and frame-loop will be limited to the display's refresh rate. + * This is the only mode that's always available. + */ + WGPUPresentMode_Fifo = 0x00000001, + /** + * The presentation of the image to the user tries to wait for the next vertical blanking period but may decide to not wait if a frame is presented late. + * Tearing can sometimes be observed but late-frame don't produce a full-frame stutter in the presentation. + * This is still a first-in, first-out mechanism so a frame-loop will be limited to the display's refresh rate. + */ + WGPUPresentMode_FifoRelaxed = 0x00000002, + /** + * The presentation of the image to the user is updated immediately without waiting for a vertical blank. + * Tearing can be observed but latency is minimized. + */ + WGPUPresentMode_Immediate = 0x00000003, + /** + * The presentation of the image to the user waits for the next vertical blanking period to update to the latest provided image. + * Tearing cannot be observed and a frame-loop is not limited to the display's refresh rate. + */ + WGPUPresentMode_Mailbox = 0x00000004, + WGPUPresentMode_Force32 = 0x7FFFFFFF +} WGPUPresentMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUPrimitiveTopology { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUPrimitiveTopology_Undefined = 0x00000000, + WGPUPrimitiveTopology_PointList = 0x00000001, + WGPUPrimitiveTopology_LineList = 0x00000002, + WGPUPrimitiveTopology_LineStrip = 0x00000003, + WGPUPrimitiveTopology_TriangleList = 0x00000004, + WGPUPrimitiveTopology_TriangleStrip = 0x00000005, + WGPUPrimitiveTopology_Force32 = 0x7FFFFFFF +} WGPUPrimitiveTopology WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUQueryType { + WGPUQueryType_Occlusion = 0x00000001, + WGPUQueryType_Timestamp = 0x00000002, + WGPUQueryType_Force32 = 0x7FFFFFFF +} WGPUQueryType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUQueueWorkDoneStatus { + WGPUQueueWorkDoneStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPUQueueWorkDoneStatus_CallbackCancelled = 0x00000002, + /** + * There was some deterministic error. (Note this is currently never used, + * but it will be relevant when it's possible to create a queue object.) + */ + WGPUQueueWorkDoneStatus_Error = 0x00000003, + WGPUQueueWorkDoneStatus_Force32 = 0x7FFFFFFF +} WGPUQueueWorkDoneStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPURequestAdapterStatus { + WGPURequestAdapterStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPURequestAdapterStatus_CallbackCancelled = 0x00000002, + WGPURequestAdapterStatus_Unavailable = 0x00000003, + WGPURequestAdapterStatus_Error = 0x00000004, + WGPURequestAdapterStatus_Force32 = 0x7FFFFFFF +} WGPURequestAdapterStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPURequestDeviceStatus { + WGPURequestDeviceStatus_Success = 0x00000001, + /** + * See @ref CallbackStatuses. + */ + WGPURequestDeviceStatus_CallbackCancelled = 0x00000002, + WGPURequestDeviceStatus_Error = 0x00000003, + WGPURequestDeviceStatus_Force32 = 0x7FFFFFFF +} WGPURequestDeviceStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUSamplerBindingType { + /** + * `0`. Indicates that this @ref WGPUSamplerBindingLayout member of + * its parent @ref WGPUBindGroupLayoutEntry is not used. + * (See also @ref SentinelValues.) + */ + WGPUSamplerBindingType_BindingNotUsed = 0x00000000, + /** + * `1`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUSamplerBindingType_Undefined = 0x00000001, + WGPUSamplerBindingType_Filtering = 0x00000002, + WGPUSamplerBindingType_NonFiltering = 0x00000003, + WGPUSamplerBindingType_Comparison = 0x00000004, + WGPUSamplerBindingType_Force32 = 0x7FFFFFFF +} WGPUSamplerBindingType WGPU_ENUM_ATTRIBUTE; + +/** + * Status code returned (synchronously) from many operations. Generally + * indicates an invalid input like an unknown enum value or @ref OutStructChainError. + * Read the function's documentation for specific error conditions. + */ +typedef enum WGPUStatus { + WGPUStatus_Success = 0x00000001, + WGPUStatus_Error = 0x00000002, + WGPUStatus_Force32 = 0x7FFFFFFF +} WGPUStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUStencilOperation { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUStencilOperation_Undefined = 0x00000000, + WGPUStencilOperation_Keep = 0x00000001, + WGPUStencilOperation_Zero = 0x00000002, + WGPUStencilOperation_Replace = 0x00000003, + WGPUStencilOperation_Invert = 0x00000004, + WGPUStencilOperation_IncrementClamp = 0x00000005, + WGPUStencilOperation_DecrementClamp = 0x00000006, + WGPUStencilOperation_IncrementWrap = 0x00000007, + WGPUStencilOperation_DecrementWrap = 0x00000008, + WGPUStencilOperation_Force32 = 0x7FFFFFFF +} WGPUStencilOperation WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUStorageTextureAccess { + /** + * `0`. Indicates that this @ref WGPUStorageTextureBindingLayout member of + * its parent @ref WGPUBindGroupLayoutEntry is not used. + * (See also @ref SentinelValues.) + */ + WGPUStorageTextureAccess_BindingNotUsed = 0x00000000, + /** + * `1`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUStorageTextureAccess_Undefined = 0x00000001, + WGPUStorageTextureAccess_WriteOnly = 0x00000002, + WGPUStorageTextureAccess_ReadOnly = 0x00000003, + WGPUStorageTextureAccess_ReadWrite = 0x00000004, + WGPUStorageTextureAccess_Force32 = 0x7FFFFFFF +} WGPUStorageTextureAccess WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUStoreOp { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUStoreOp_Undefined = 0x00000000, + WGPUStoreOp_Store = 0x00000001, + WGPUStoreOp_Discard = 0x00000002, + WGPUStoreOp_Force32 = 0x7FFFFFFF +} WGPUStoreOp WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUSType { + WGPUSType_ShaderSourceSPIRV = 0x00000001, + WGPUSType_ShaderSourceWGSL = 0x00000002, + WGPUSType_RenderPassMaxDrawCount = 0x00000003, + WGPUSType_SurfaceSourceMetalLayer = 0x00000004, + WGPUSType_SurfaceSourceWindowsHWND = 0x00000005, + WGPUSType_SurfaceSourceXlibWindow = 0x00000006, + WGPUSType_SurfaceSourceWaylandSurface = 0x00000007, + WGPUSType_SurfaceSourceAndroidNativeWindow = 0x00000008, + WGPUSType_SurfaceSourceXCBWindow = 0x00000009, + WGPUSType_SurfaceColorManagement = 0x0000000A, + WGPUSType_RequestAdapterWebXROptions = 0x0000000B, + WGPUSType_TextureComponentSwizzleDescriptor = 0x0000000C, + WGPUSType_ExternalTextureBindingLayout = 0x0000000D, + WGPUSType_ExternalTextureBindingEntry = 0x0000000E, + WGPUSType_CompatibilityModeLimits = 0x0000000F, + WGPUSType_TextureBindingViewDimension = 0x00000010, + WGPUSType_Force32 = 0x7FFFFFFF +} WGPUSType WGPU_ENUM_ATTRIBUTE; + +/** + * The status enum for @ref wgpuSurfaceGetCurrentTexture. + */ +typedef enum WGPUSurfaceGetCurrentTextureStatus { + /** + * Yay! Everything is good and we can render this frame. + */ + WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal = 0x00000001, + /** + * Still OK - the surface can present the frame, but in a suboptimal way. The surface may need reconfiguration. + */ + WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal = 0x00000002, + /** + * Some operation timed out while trying to acquire the frame. + */ + WGPUSurfaceGetCurrentTextureStatus_Timeout = 0x00000003, + /** + * The surface is too different to be used, compared to when it was originally created. + */ + WGPUSurfaceGetCurrentTextureStatus_Outdated = 0x00000004, + /** + * The connection to whatever owns the surface was lost, or generally needs to be fully reinitialized. + */ + WGPUSurfaceGetCurrentTextureStatus_Lost = 0x00000005, + /** + * There was some deterministic error (for example, the surface is not configured, or there was an @ref OutStructChainError). Should produce @ref ImplementationDefinedLogging containing details. + */ + WGPUSurfaceGetCurrentTextureStatus_Error = 0x00000006, + WGPUSurfaceGetCurrentTextureStatus_Force32 = 0x7FFFFFFF +} WGPUSurfaceGetCurrentTextureStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUTextureAspect { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUTextureAspect_Undefined = 0x00000000, + WGPUTextureAspect_All = 0x00000001, + WGPUTextureAspect_StencilOnly = 0x00000002, + WGPUTextureAspect_DepthOnly = 0x00000003, + WGPUTextureAspect_Force32 = 0x7FFFFFFF +} WGPUTextureAspect WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUTextureDimension { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUTextureDimension_Undefined = 0x00000000, + WGPUTextureDimension_1D = 0x00000001, + WGPUTextureDimension_2D = 0x00000002, + WGPUTextureDimension_3D = 0x00000003, + WGPUTextureDimension_Force32 = 0x7FFFFFFF +} WGPUTextureDimension WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUTextureFormat { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUTextureFormat_Undefined = 0x00000000, + WGPUTextureFormat_R8Unorm = 0x00000001, + WGPUTextureFormat_R8Snorm = 0x00000002, + WGPUTextureFormat_R8Uint = 0x00000003, + WGPUTextureFormat_R8Sint = 0x00000004, + WGPUTextureFormat_R16Unorm = 0x00000005, + WGPUTextureFormat_R16Snorm = 0x00000006, + WGPUTextureFormat_R16Uint = 0x00000007, + WGPUTextureFormat_R16Sint = 0x00000008, + WGPUTextureFormat_R16Float = 0x00000009, + WGPUTextureFormat_RG8Unorm = 0x0000000A, + WGPUTextureFormat_RG8Snorm = 0x0000000B, + WGPUTextureFormat_RG8Uint = 0x0000000C, + WGPUTextureFormat_RG8Sint = 0x0000000D, + WGPUTextureFormat_R32Float = 0x0000000E, + WGPUTextureFormat_R32Uint = 0x0000000F, + WGPUTextureFormat_R32Sint = 0x00000010, + WGPUTextureFormat_RG16Unorm = 0x00000011, + WGPUTextureFormat_RG16Snorm = 0x00000012, + WGPUTextureFormat_RG16Uint = 0x00000013, + WGPUTextureFormat_RG16Sint = 0x00000014, + WGPUTextureFormat_RG16Float = 0x00000015, + WGPUTextureFormat_RGBA8Unorm = 0x00000016, + WGPUTextureFormat_RGBA8UnormSrgb = 0x00000017, + WGPUTextureFormat_RGBA8Snorm = 0x00000018, + WGPUTextureFormat_RGBA8Uint = 0x00000019, + WGPUTextureFormat_RGBA8Sint = 0x0000001A, + WGPUTextureFormat_BGRA8Unorm = 0x0000001B, + WGPUTextureFormat_BGRA8UnormSrgb = 0x0000001C, + WGPUTextureFormat_RGB10A2Uint = 0x0000001D, + WGPUTextureFormat_RGB10A2Unorm = 0x0000001E, + WGPUTextureFormat_RG11B10Ufloat = 0x0000001F, + WGPUTextureFormat_RGB9E5Ufloat = 0x00000020, + WGPUTextureFormat_RG32Float = 0x00000021, + WGPUTextureFormat_RG32Uint = 0x00000022, + WGPUTextureFormat_RG32Sint = 0x00000023, + WGPUTextureFormat_RGBA16Unorm = 0x00000024, + WGPUTextureFormat_RGBA16Snorm = 0x00000025, + WGPUTextureFormat_RGBA16Uint = 0x00000026, + WGPUTextureFormat_RGBA16Sint = 0x00000027, + WGPUTextureFormat_RGBA16Float = 0x00000028, + WGPUTextureFormat_RGBA32Float = 0x00000029, + WGPUTextureFormat_RGBA32Uint = 0x0000002A, + WGPUTextureFormat_RGBA32Sint = 0x0000002B, + WGPUTextureFormat_Stencil8 = 0x0000002C, + WGPUTextureFormat_Depth16Unorm = 0x0000002D, + WGPUTextureFormat_Depth24Plus = 0x0000002E, + WGPUTextureFormat_Depth24PlusStencil8 = 0x0000002F, + WGPUTextureFormat_Depth32Float = 0x00000030, + WGPUTextureFormat_Depth32FloatStencil8 = 0x00000031, + WGPUTextureFormat_BC1RGBAUnorm = 0x00000032, + WGPUTextureFormat_BC1RGBAUnormSrgb = 0x00000033, + WGPUTextureFormat_BC2RGBAUnorm = 0x00000034, + WGPUTextureFormat_BC2RGBAUnormSrgb = 0x00000035, + WGPUTextureFormat_BC3RGBAUnorm = 0x00000036, + WGPUTextureFormat_BC3RGBAUnormSrgb = 0x00000037, + WGPUTextureFormat_BC4RUnorm = 0x00000038, + WGPUTextureFormat_BC4RSnorm = 0x00000039, + WGPUTextureFormat_BC5RGUnorm = 0x0000003A, + WGPUTextureFormat_BC5RGSnorm = 0x0000003B, + WGPUTextureFormat_BC6HRGBUfloat = 0x0000003C, + WGPUTextureFormat_BC6HRGBFloat = 0x0000003D, + WGPUTextureFormat_BC7RGBAUnorm = 0x0000003E, + WGPUTextureFormat_BC7RGBAUnormSrgb = 0x0000003F, + WGPUTextureFormat_ETC2RGB8Unorm = 0x00000040, + WGPUTextureFormat_ETC2RGB8UnormSrgb = 0x00000041, + WGPUTextureFormat_ETC2RGB8A1Unorm = 0x00000042, + WGPUTextureFormat_ETC2RGB8A1UnormSrgb = 0x00000043, + WGPUTextureFormat_ETC2RGBA8Unorm = 0x00000044, + WGPUTextureFormat_ETC2RGBA8UnormSrgb = 0x00000045, + WGPUTextureFormat_EACR11Unorm = 0x00000046, + WGPUTextureFormat_EACR11Snorm = 0x00000047, + WGPUTextureFormat_EACRG11Unorm = 0x00000048, + WGPUTextureFormat_EACRG11Snorm = 0x00000049, + WGPUTextureFormat_ASTC4x4Unorm = 0x0000004A, + WGPUTextureFormat_ASTC4x4UnormSrgb = 0x0000004B, + WGPUTextureFormat_ASTC5x4Unorm = 0x0000004C, + WGPUTextureFormat_ASTC5x4UnormSrgb = 0x0000004D, + WGPUTextureFormat_ASTC5x5Unorm = 0x0000004E, + WGPUTextureFormat_ASTC5x5UnormSrgb = 0x0000004F, + WGPUTextureFormat_ASTC6x5Unorm = 0x00000050, + WGPUTextureFormat_ASTC6x5UnormSrgb = 0x00000051, + WGPUTextureFormat_ASTC6x6Unorm = 0x00000052, + WGPUTextureFormat_ASTC6x6UnormSrgb = 0x00000053, + WGPUTextureFormat_ASTC8x5Unorm = 0x00000054, + WGPUTextureFormat_ASTC8x5UnormSrgb = 0x00000055, + WGPUTextureFormat_ASTC8x6Unorm = 0x00000056, + WGPUTextureFormat_ASTC8x6UnormSrgb = 0x00000057, + WGPUTextureFormat_ASTC8x8Unorm = 0x00000058, + WGPUTextureFormat_ASTC8x8UnormSrgb = 0x00000059, + WGPUTextureFormat_ASTC10x5Unorm = 0x0000005A, + WGPUTextureFormat_ASTC10x5UnormSrgb = 0x0000005B, + WGPUTextureFormat_ASTC10x6Unorm = 0x0000005C, + WGPUTextureFormat_ASTC10x6UnormSrgb = 0x0000005D, + WGPUTextureFormat_ASTC10x8Unorm = 0x0000005E, + WGPUTextureFormat_ASTC10x8UnormSrgb = 0x0000005F, + WGPUTextureFormat_ASTC10x10Unorm = 0x00000060, + WGPUTextureFormat_ASTC10x10UnormSrgb = 0x00000061, + WGPUTextureFormat_ASTC12x10Unorm = 0x00000062, + WGPUTextureFormat_ASTC12x10UnormSrgb = 0x00000063, + WGPUTextureFormat_ASTC12x12Unorm = 0x00000064, + WGPUTextureFormat_ASTC12x12UnormSrgb = 0x00000065, + WGPUTextureFormat_Force32 = 0x7FFFFFFF +} WGPUTextureFormat WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUTextureSampleType { + /** + * `0`. Indicates that this @ref WGPUTextureBindingLayout member of + * its parent @ref WGPUBindGroupLayoutEntry is not used. + * (See also @ref SentinelValues.) + */ + WGPUTextureSampleType_BindingNotUsed = 0x00000000, + /** + * `1`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUTextureSampleType_Undefined = 0x00000001, + WGPUTextureSampleType_Float = 0x00000002, + WGPUTextureSampleType_UnfilterableFloat = 0x00000003, + WGPUTextureSampleType_Depth = 0x00000004, + WGPUTextureSampleType_Sint = 0x00000005, + WGPUTextureSampleType_Uint = 0x00000006, + WGPUTextureSampleType_Force32 = 0x7FFFFFFF +} WGPUTextureSampleType WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUTextureViewDimension { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUTextureViewDimension_Undefined = 0x00000000, + WGPUTextureViewDimension_1D = 0x00000001, + WGPUTextureViewDimension_2D = 0x00000002, + WGPUTextureViewDimension_2DArray = 0x00000003, + WGPUTextureViewDimension_Cube = 0x00000004, + WGPUTextureViewDimension_CubeArray = 0x00000005, + WGPUTextureViewDimension_3D = 0x00000006, + WGPUTextureViewDimension_Force32 = 0x7FFFFFFF +} WGPUTextureViewDimension WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUToneMappingMode { + WGPUToneMappingMode_Standard = 0x00000001, + WGPUToneMappingMode_Extended = 0x00000002, + WGPUToneMappingMode_Force32 = 0x7FFFFFFF +} WGPUToneMappingMode WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUVertexFormat { + WGPUVertexFormat_Uint8 = 0x00000001, + WGPUVertexFormat_Uint8x2 = 0x00000002, + WGPUVertexFormat_Uint8x4 = 0x00000003, + WGPUVertexFormat_Sint8 = 0x00000004, + WGPUVertexFormat_Sint8x2 = 0x00000005, + WGPUVertexFormat_Sint8x4 = 0x00000006, + WGPUVertexFormat_Unorm8 = 0x00000007, + WGPUVertexFormat_Unorm8x2 = 0x00000008, + WGPUVertexFormat_Unorm8x4 = 0x00000009, + WGPUVertexFormat_Snorm8 = 0x0000000A, + WGPUVertexFormat_Snorm8x2 = 0x0000000B, + WGPUVertexFormat_Snorm8x4 = 0x0000000C, + WGPUVertexFormat_Uint16 = 0x0000000D, + WGPUVertexFormat_Uint16x2 = 0x0000000E, + WGPUVertexFormat_Uint16x4 = 0x0000000F, + WGPUVertexFormat_Sint16 = 0x00000010, + WGPUVertexFormat_Sint16x2 = 0x00000011, + WGPUVertexFormat_Sint16x4 = 0x00000012, + WGPUVertexFormat_Unorm16 = 0x00000013, + WGPUVertexFormat_Unorm16x2 = 0x00000014, + WGPUVertexFormat_Unorm16x4 = 0x00000015, + WGPUVertexFormat_Snorm16 = 0x00000016, + WGPUVertexFormat_Snorm16x2 = 0x00000017, + WGPUVertexFormat_Snorm16x4 = 0x00000018, + WGPUVertexFormat_Float16 = 0x00000019, + WGPUVertexFormat_Float16x2 = 0x0000001A, + WGPUVertexFormat_Float16x4 = 0x0000001B, + WGPUVertexFormat_Float32 = 0x0000001C, + WGPUVertexFormat_Float32x2 = 0x0000001D, + WGPUVertexFormat_Float32x3 = 0x0000001E, + WGPUVertexFormat_Float32x4 = 0x0000001F, + WGPUVertexFormat_Uint32 = 0x00000020, + WGPUVertexFormat_Uint32x2 = 0x00000021, + WGPUVertexFormat_Uint32x3 = 0x00000022, + WGPUVertexFormat_Uint32x4 = 0x00000023, + WGPUVertexFormat_Sint32 = 0x00000024, + WGPUVertexFormat_Sint32x2 = 0x00000025, + WGPUVertexFormat_Sint32x3 = 0x00000026, + WGPUVertexFormat_Sint32x4 = 0x00000027, + WGPUVertexFormat_Unorm10_10_10_2 = 0x00000028, + WGPUVertexFormat_Unorm8x4BGRA = 0x00000029, + WGPUVertexFormat_Force32 = 0x7FFFFFFF +} WGPUVertexFormat WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUVertexStepMode { + /** + * `0`. Indicates no value is passed for this argument. See @ref SentinelValues. + */ + WGPUVertexStepMode_Undefined = 0x00000000, + WGPUVertexStepMode_Vertex = 0x00000001, + WGPUVertexStepMode_Instance = 0x00000002, + WGPUVertexStepMode_Force32 = 0x7FFFFFFF +} WGPUVertexStepMode WGPU_ENUM_ATTRIBUTE; + +/** + * Status returned from a call to ::wgpuInstanceWaitAny. + */ +typedef enum WGPUWaitStatus { + /** + * At least one WGPUFuture completed successfully. + */ + WGPUWaitStatus_Success = 0x00000001, + /** + * The wait operation succeeded, but no WGPUFutures completed within the timeout. + */ + WGPUWaitStatus_TimedOut = 0x00000002, + /** + * The call was invalid for some reason (see @ref Wait-Any). + * Should produce @ref ImplementationDefinedLogging containing details. + */ + WGPUWaitStatus_Error = 0x00000003, + WGPUWaitStatus_Force32 = 0x7FFFFFFF +} WGPUWaitStatus WGPU_ENUM_ATTRIBUTE; + +typedef enum WGPUWGSLLanguageFeatureName { + WGPUWGSLLanguageFeatureName_ReadonlyAndReadwriteStorageTextures = 0x00000001, + WGPUWGSLLanguageFeatureName_Packed4x8IntegerDotProduct = 0x00000002, + WGPUWGSLLanguageFeatureName_UnrestrictedPointerParameters = 0x00000003, + WGPUWGSLLanguageFeatureName_PointerCompositeAccess = 0x00000004, + WGPUWGSLLanguageFeatureName_UniformBufferStandardLayout = 0x00000005, + WGPUWGSLLanguageFeatureName_SubgroupId = 0x00000006, + WGPUWGSLLanguageFeatureName_TextureAndSamplerLet = 0x00000007, + WGPUWGSLLanguageFeatureName_SubgroupUniformity = 0x00000008, + WGPUWGSLLanguageFeatureName_TextureFormatsTier1 = 0x00000009, + WGPUWGSLLanguageFeatureName_LinearIndexing = 0x0000000A, + WGPUWGSLLanguageFeatureName_ImmediateAddressSpace = 0x0000000B, + WGPUWGSLLanguageFeatureName_Force32 = 0x7FFFFFFF +} WGPUWGSLLanguageFeatureName WGPU_ENUM_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup Bitflags Bitflags + * \brief Type and constant definitions for bitflag types. + * + * @{ + */ + +/** + * For reserved non-standard bitflag values, see @ref BitflagRegistry. + */ +typedef WGPUFlags WGPUBufferUsage; +/** + * `0`. + */ +static const WGPUBufferUsage WGPUBufferUsage_None = 0x0000000000000000; +/** + * The buffer can be *mapped* on the CPU side in *read* mode (using @ref WGPUMapMode_Read). + */ +static const WGPUBufferUsage WGPUBufferUsage_MapRead = 0x0000000000000001; +/** + * The buffer can be *mapped* on the CPU side in *write* mode (using @ref WGPUMapMode_Write). + * + * @note This usage is **not** required to set `mappedAtCreation` to `true` in @ref WGPUBufferDescriptor. + */ +static const WGPUBufferUsage WGPUBufferUsage_MapWrite = 0x0000000000000002; +/** + * The buffer can be used as the *source* of a GPU-side copy operation. + */ +static const WGPUBufferUsage WGPUBufferUsage_CopySrc = 0x0000000000000004; +/** + * The buffer can be used as the *destination* of a GPU-side copy operation. + */ +static const WGPUBufferUsage WGPUBufferUsage_CopyDst = 0x0000000000000008; +/** + * The buffer can be used as an Index buffer when doing indexed drawing in a render pipeline. + */ +static const WGPUBufferUsage WGPUBufferUsage_Index = 0x0000000000000010; +/** + * The buffer can be used as a Vertex buffer when using a render pipeline. + */ +static const WGPUBufferUsage WGPUBufferUsage_Vertex = 0x0000000000000020; +/** + * The buffer can be bound to a shader as a uniform buffer. + */ +static const WGPUBufferUsage WGPUBufferUsage_Uniform = 0x0000000000000040; +/** + * The buffer can be bound to a shader as a storage buffer. + */ +static const WGPUBufferUsage WGPUBufferUsage_Storage = 0x0000000000000080; +/** + * The buffer can store arguments for an indirect draw call. + */ +static const WGPUBufferUsage WGPUBufferUsage_Indirect = 0x0000000000000100; +/** + * The buffer can store the result of a timestamp or occlusion query. + */ +static const WGPUBufferUsage WGPUBufferUsage_QueryResolve = 0x0000000000000200; + +/** + * For reserved non-standard bitflag values, see @ref BitflagRegistry. + */ +typedef WGPUFlags WGPUColorWriteMask; +/** + * `0`. + */ +static const WGPUColorWriteMask WGPUColorWriteMask_None = 0x0000000000000000; +static const WGPUColorWriteMask WGPUColorWriteMask_Red = 0x0000000000000001; +static const WGPUColorWriteMask WGPUColorWriteMask_Green = 0x0000000000000002; +static const WGPUColorWriteMask WGPUColorWriteMask_Blue = 0x0000000000000004; +static const WGPUColorWriteMask WGPUColorWriteMask_Alpha = 0x0000000000000008; +/** + * `Red | Green | Blue | Alpha`. + */ +static const WGPUColorWriteMask WGPUColorWriteMask_All = 0x000000000000000F; + +/** + * For reserved non-standard bitflag values, see @ref BitflagRegistry. + */ +typedef WGPUFlags WGPUMapMode; +/** + * `0`. + */ +static const WGPUMapMode WGPUMapMode_None = 0x0000000000000000; +static const WGPUMapMode WGPUMapMode_Read = 0x0000000000000001; +static const WGPUMapMode WGPUMapMode_Write = 0x0000000000000002; + +/** + * For reserved non-standard bitflag values, see @ref BitflagRegistry. + */ +typedef WGPUFlags WGPUShaderStage; +/** + * `0`. + */ +static const WGPUShaderStage WGPUShaderStage_None = 0x0000000000000000; +static const WGPUShaderStage WGPUShaderStage_Vertex = 0x0000000000000001; +static const WGPUShaderStage WGPUShaderStage_Fragment = 0x0000000000000002; +static const WGPUShaderStage WGPUShaderStage_Compute = 0x0000000000000004; + +/** + * For reserved non-standard bitflag values, see @ref BitflagRegistry. + */ +typedef WGPUFlags WGPUTextureUsage; +/** + * `0`. + */ +static const WGPUTextureUsage WGPUTextureUsage_None = 0x0000000000000000; +static const WGPUTextureUsage WGPUTextureUsage_CopySrc = 0x0000000000000001; +static const WGPUTextureUsage WGPUTextureUsage_CopyDst = 0x0000000000000002; +static const WGPUTextureUsage WGPUTextureUsage_TextureBinding = 0x0000000000000004; +static const WGPUTextureUsage WGPUTextureUsage_StorageBinding = 0x0000000000000008; +static const WGPUTextureUsage WGPUTextureUsage_RenderAttachment = 0x0000000000000010; +static const WGPUTextureUsage WGPUTextureUsage_TransientAttachment = 0x0000000000000020; + +/** @} */ + +typedef void (*WGPUProc)(void) WGPU_FUNCTION_ATTRIBUTE; + +/** + * \defgroup Callbacks Callbacks + * \brief Callbacks through which asynchronous functions return. + * + * @{ + */ + +/** + * See also @ref CallbackError. + * + * @param message + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUBufferMapCallback)(WGPUMapAsyncStatus status, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param compilationInfo + * This argument contains multiple @ref ImplementationAllocatedStructChain roots. + * Arbitrary chains must be handled gracefully by the application! + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUCompilationInfoCallback)(WGPUCompilationInfoRequestStatus status, struct WGPUCompilationInfo const * compilationInfo, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param pipeline + * This parameter is @ref PassedWithOwnership. + */ +typedef void (*WGPUCreateComputePipelineAsyncCallback)(WGPUCreatePipelineAsyncStatus status, WGPUComputePipeline pipeline, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param pipeline + * This parameter is @ref PassedWithOwnership. + */ +typedef void (*WGPUCreateRenderPipelineAsyncCallback)(WGPUCreatePipelineAsyncStatus status, WGPURenderPipeline pipeline, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param device + * Pointer to the device which was lost. This is always a non-null pointer. + * The pointed-to @ref WGPUDevice will be null if, and only if, either: + * (1) The `reason` is @ref WGPUDeviceLostReason_FailedCreation. + * (2) The last ref of the device has been (or is being) released: see @ref DeviceRelease. + * This parameter is @ref PassedWithoutOwnership. + * + * @param reason + * An error code explaining why the device was lost. + * + * @param message + * A @ref LocalizableHumanReadableMessageString describing why the device was lost. + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUDeviceLostCallback)(WGPUDevice const * device, WGPUDeviceLostReason reason, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param status + * See @ref WGPUPopErrorScopeStatus. + * + * @param type + * The type of the error caught by the scope, or @ref WGPUErrorType_NoError if there was none. + * If the `status` is not @ref WGPUPopErrorScopeStatus_Success, this is @ref WGPUErrorType_NoError. + * + * @param message + * If the `status` is not @ref WGPUPopErrorScopeStatus_Success **or** + * the `type` is not @ref WGPUErrorType_NoError, this is a non-empty + * @ref LocalizableHumanReadableMessageString; + * otherwise, this is an empty string. + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUPopErrorScopeCallback)(WGPUPopErrorScopeStatus status, WGPUErrorType type, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param status + * See @ref WGPUQueueWorkDoneStatus. + * + * @param message + * If the `status` is not @ref WGPUQueueWorkDoneStatus_Success, + * this is a non-empty @ref LocalizableHumanReadableMessageString; + * otherwise, this is an empty string. + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUQueueWorkDoneCallback)(WGPUQueueWorkDoneStatus status, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param adapter + * This parameter is @ref PassedWithOwnership. + * + * @param message + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPURequestAdapterCallback)(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param device + * This parameter is @ref PassedWithOwnership. + * + * @param message + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPURequestDeviceCallback)(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** + * See also @ref CallbackError. + * + * @param device + * This parameter is @ref PassedWithoutOwnership. + * + * @param message + * This parameter is @ref PassedWithoutOwnership. + */ +typedef void (*WGPUUncapturedErrorCallback)(WGPUDevice const * device, WGPUErrorType type, WGPUStringView message, WGPU_NULLABLE void* userdata1, WGPU_NULLABLE void* userdata2) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ +/** + * \defgroup ChainedStructures Chained Structures + * \brief Structures used to extend descriptors. + * + * @{ + */ + +typedef struct WGPUChainedStruct { + struct WGPUChainedStruct * next; + WGPUSType sType; +} WGPUChainedStruct WGPU_STRUCTURE_ATTRIBUTE; + +/** @} */ + + +/** + * \defgroup Structures Structures + * \brief Descriptors and other transparent structures. + * + * @{ + */ + +/** + * \defgroup CallbackInfoStructs Callback Info Structs + * \brief Callback info structures that are used in asynchronous functions. + * + * @{ + */ + +typedef struct WGPUBufferMapCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUBufferMapCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUBufferMapCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBufferMapCallbackInfo. + */ +#define WGPU_BUFFER_MAP_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBufferMapCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUCompilationInfoCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUCompilationInfoCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUCompilationInfoCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCompilationInfoCallbackInfo. + */ +#define WGPU_COMPILATION_INFO_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCompilationInfoCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUCreateComputePipelineAsyncCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUCreateComputePipelineAsyncCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUCreateComputePipelineAsyncCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCreateComputePipelineAsyncCallbackInfo. + */ +#define WGPU_CREATE_COMPUTE_PIPELINE_ASYNC_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCreateComputePipelineAsyncCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUCreateRenderPipelineAsyncCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUCreateRenderPipelineAsyncCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUCreateRenderPipelineAsyncCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCreateRenderPipelineAsyncCallbackInfo. + */ +#define WGPU_CREATE_RENDER_PIPELINE_ASYNC_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCreateRenderPipelineAsyncCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUDeviceLostCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUDeviceLostCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUDeviceLostCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUDeviceLostCallbackInfo. + */ +#define WGPU_DEVICE_LOST_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUDeviceLostCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUPopErrorScopeCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUPopErrorScopeCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUPopErrorScopeCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUPopErrorScopeCallbackInfo. + */ +#define WGPU_POP_ERROR_SCOPE_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUPopErrorScopeCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUQueueWorkDoneCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPUQueueWorkDoneCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUQueueWorkDoneCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUQueueWorkDoneCallbackInfo. + */ +#define WGPU_QUEUE_WORK_DONE_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUQueueWorkDoneCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPURequestAdapterCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPURequestAdapterCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPURequestAdapterCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURequestAdapterCallbackInfo. + */ +#define WGPU_REQUEST_ADAPTER_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPURequestAdapterCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPURequestDeviceCallbackInfo { + WGPUChainedStruct * nextInChain; + /** + * Controls when the callback may be called. + * + * Has no default. The `INIT` macro sets this to (@ref WGPUCallbackMode)0. + */ + WGPUCallbackMode mode; + WGPURequestDeviceCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPURequestDeviceCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURequestDeviceCallbackInfo. + */ +#define WGPU_REQUEST_DEVICE_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPURequestDeviceCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.mode=*/_wgpu_ENUM_ZERO_INIT(WGPUCallbackMode) _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +typedef struct WGPUUncapturedErrorCallbackInfo { + WGPUChainedStruct * nextInChain; + WGPUUncapturedErrorCallback callback; + WGPU_NULLABLE void* userdata1; + WGPU_NULLABLE void* userdata2; +} WGPUUncapturedErrorCallbackInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUUncapturedErrorCallbackInfo. + */ +#define WGPU_UNCAPTURED_ERROR_CALLBACK_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUUncapturedErrorCallbackInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.callback=*/NULL _wgpu_COMMA \ + /*.userdata1=*/NULL _wgpu_COMMA \ + /*.userdata2=*/NULL _wgpu_COMMA \ +}) + +/** @} */ + +/** + * Default values can be set using @ref WGPU_ADAPTER_INFO_INIT as initializer. + */ +typedef struct WGPUAdapterInfo { + WGPUChainedStruct * nextInChain; + /** + * This is an \ref OutputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView vendor; + /** + * This is an \ref OutputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView architecture; + /** + * This is an \ref OutputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView device; + /** + * This is an \ref OutputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView description; + /** + * The `INIT` macro sets this to @ref WGPUBackendType_Undefined. + */ + WGPUBackendType backendType; + /** + * The `INIT` macro sets this to (@ref WGPUAdapterType)0. + */ + WGPUAdapterType adapterType; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t vendorID; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t deviceID; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t subgroupMinSize; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t subgroupMaxSize; +} WGPUAdapterInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUAdapterInfo. + */ +#define WGPU_ADAPTER_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUAdapterInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.vendor=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.architecture=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.device=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.description=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.backendType=*/WGPUBackendType_Undefined _wgpu_COMMA \ + /*.adapterType=*/_wgpu_ENUM_ZERO_INIT(WGPUAdapterType) _wgpu_COMMA \ + /*.vendorID=*/0 _wgpu_COMMA \ + /*.deviceID=*/0 _wgpu_COMMA \ + /*.subgroupMinSize=*/0 _wgpu_COMMA \ + /*.subgroupMaxSize=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BLEND_COMPONENT_INIT as initializer. + */ +typedef struct WGPUBlendComponent { + /** + * If set to @ref WGPUBlendOperation_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUBlendOperation_Add. + * + * The `INIT` macro sets this to @ref WGPUBlendOperation_Undefined. + */ + WGPUBlendOperation operation; + /** + * If set to @ref WGPUBlendFactor_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUBlendFactor_One. + * + * The `INIT` macro sets this to @ref WGPUBlendFactor_Undefined. + */ + WGPUBlendFactor srcFactor; + /** + * If set to @ref WGPUBlendFactor_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUBlendFactor_Zero. + * + * The `INIT` macro sets this to @ref WGPUBlendFactor_Undefined. + */ + WGPUBlendFactor dstFactor; +} WGPUBlendComponent WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBlendComponent. + */ +#define WGPU_BLEND_COMPONENT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBlendComponent, { \ + /*.operation=*/WGPUBlendOperation_Undefined _wgpu_COMMA \ + /*.srcFactor=*/WGPUBlendFactor_Undefined _wgpu_COMMA \ + /*.dstFactor=*/WGPUBlendFactor_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BUFFER_BINDING_LAYOUT_INIT as initializer. + */ +typedef struct WGPUBufferBindingLayout { + WGPUChainedStruct * nextInChain; + /** + * If set to @ref WGPUBufferBindingType_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUBufferBindingType_Uniform. + * + * The `INIT` macro sets this to @ref WGPUBufferBindingType_Undefined. + */ + WGPUBufferBindingType type; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool hasDynamicOffset; + /** + * The `INIT` macro sets this to `0`. + */ + uint64_t minBindingSize; +} WGPUBufferBindingLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBufferBindingLayout. + */ +#define WGPU_BUFFER_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBufferBindingLayout, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.type=*/WGPUBufferBindingType_Undefined _wgpu_COMMA \ + /*.hasDynamicOffset=*/WGPU_FALSE _wgpu_COMMA \ + /*.minBindingSize=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BUFFER_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUBufferDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to @ref WGPUBufferUsage_None. + */ + WGPUBufferUsage usage; + /** + * The `INIT` macro sets this to `0`. + */ + uint64_t size; + /** + * When true, the buffer is mapped in write mode at creation. It should thus be unmapped once its initial data has been written. + * + * @note Mapping at creation does **not** require the usage @ref WGPUBufferUsage_MapWrite. + * + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool mappedAtCreation; +} WGPUBufferDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBufferDescriptor. + */ +#define WGPU_BUFFER_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBufferDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.usage=*/WGPUBufferUsage_None _wgpu_COMMA \ + /*.size=*/0 _wgpu_COMMA \ + /*.mappedAtCreation=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * An RGBA color. Represents a `f32`, `i32`, or `u32` color using @ref DoubleAsSupertype. + * + * If any channel is non-finite, produces a @ref NonFiniteFloatValueError. + * + * Default values can be set using @ref WGPU_COLOR_INIT as initializer. + */ +typedef struct WGPUColor { + /** + * The `INIT` macro sets this to `0.`. + */ + double r; + /** + * The `INIT` macro sets this to `0.`. + */ + double g; + /** + * The `INIT` macro sets this to `0.`. + */ + double b; + /** + * The `INIT` macro sets this to `0.`. + */ + double a; +} WGPUColor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUColor. + */ +#define WGPU_COLOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUColor, { \ + /*.r=*/0. _wgpu_COMMA \ + /*.g=*/0. _wgpu_COMMA \ + /*.b=*/0. _wgpu_COMMA \ + /*.a=*/0. _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COMMAND_BUFFER_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUCommandBufferDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPUCommandBufferDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCommandBufferDescriptor. + */ +#define WGPU_COMMAND_BUFFER_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCommandBufferDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COMMAND_ENCODER_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUCommandEncoderDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPUCommandEncoderDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCommandEncoderDescriptor. + */ +#define WGPU_COMMAND_ENCODER_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCommandEncoderDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Note: While Compatibility Mode is optional to implement, this extension struct + * is required to be supported (for both queries and requests) and behave as + * defined in the WebGPU spec. + * + * Default values can be set using @ref WGPU_COMPATIBILITY_MODE_LIMITS_INIT as initializer. + */ +typedef struct WGPUCompatibilityModeLimits { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageBuffersInVertexStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageTexturesInVertexStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageBuffersInFragmentStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageTexturesInFragmentStage; +} WGPUCompatibilityModeLimits WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCompatibilityModeLimits. + */ +#define WGPU_COMPATIBILITY_MODE_LIMITS_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCompatibilityModeLimits, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_CompatibilityModeLimits _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.maxStorageBuffersInVertexStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxStorageTexturesInVertexStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxStorageBuffersInFragmentStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxStorageTexturesInFragmentStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ +}) + +/** + * This is an @ref ImplementationAllocatedStructChain root. + * Arbitrary chains must be handled gracefully by the application! + * + * Default values can be set using @ref WGPU_COMPILATION_MESSAGE_INIT as initializer. + */ +typedef struct WGPUCompilationMessage { + WGPUChainedStruct * nextInChain; + /** + * A @ref LocalizableHumanReadableMessageString. + * + * This is an \ref OutputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView message; + /** + * Severity level of the message. + * + * The `INIT` macro sets this to (@ref WGPUCompilationMessageType)0. + */ + WGPUCompilationMessageType type; + /** + * Line number where the message is attached, starting at 1. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t lineNum; + /** + * Offset in UTF-8 code units (bytes) from the beginning of the line, starting at 1. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t linePos; + /** + * Offset in UTF-8 code units (bytes) from the beginning of the shader code, starting at 0. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t offset; + /** + * Length in UTF-8 code units (bytes) of the span the message corresponds to. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t length; +} WGPUCompilationMessage WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCompilationMessage. + */ +#define WGPU_COMPILATION_MESSAGE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCompilationMessage, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.message=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.type=*/_wgpu_ENUM_ZERO_INIT(WGPUCompilationMessageType) _wgpu_COMMA \ + /*.lineNum=*/0 _wgpu_COMMA \ + /*.linePos=*/0 _wgpu_COMMA \ + /*.offset=*/0 _wgpu_COMMA \ + /*.length=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_CONSTANT_ENTRY_INIT as initializer. + */ +typedef struct WGPUConstantEntry { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView key; + /** + * Represents a WGSL numeric or boolean value using @ref DoubleAsSupertype. + * + * If non-finite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to `0.`. + */ + double value; +} WGPUConstantEntry WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUConstantEntry. + */ +#define WGPU_CONSTANT_ENTRY_INIT _wgpu_MAKE_INIT_STRUCT(WGPUConstantEntry, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.key=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.value=*/0. _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_EXTENT_3D_INIT as initializer. + */ +typedef struct WGPUExtent3D { + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t width; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t height; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t depthOrArrayLayers; +} WGPUExtent3D WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUExtent3D. + */ +#define WGPU_EXTENT_3D_INIT _wgpu_MAKE_INIT_STRUCT(WGPUExtent3D, { \ + /*.width=*/0 _wgpu_COMMA \ + /*.height=*/1 _wgpu_COMMA \ + /*.depthOrArrayLayers=*/1 _wgpu_COMMA \ +}) + +/** + * Chained in an @ref WGPUBindGroupEntry to set it to an @ref WGPUExternalTexture. This must have a corresponding @ref WGPUExternalTextureBindingLayout in the @ref WGPUBindGroupLayout. + * + * Default values can be set using @ref WGPU_EXTERNAL_TEXTURE_BINDING_ENTRY_INIT as initializer. + */ +typedef struct WGPUExternalTextureBindingEntry { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUExternalTexture externalTexture; +} WGPUExternalTextureBindingEntry WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUExternalTextureBindingEntry. + */ +#define WGPU_EXTERNAL_TEXTURE_BINDING_ENTRY_INIT _wgpu_MAKE_INIT_STRUCT(WGPUExternalTextureBindingEntry, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_ExternalTextureBindingEntry _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.externalTexture=*/NULL _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUBindGroupLayoutEntry to specify that the corresponding entries in an @ref WGPUBindGroup will contain an @ref WGPUExternalTexture. + * + * Default values can be set using @ref WGPU_EXTERNAL_TEXTURE_BINDING_LAYOUT_INIT as initializer. + */ +typedef struct WGPUExternalTextureBindingLayout { + WGPUChainedStruct chain; +} WGPUExternalTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUExternalTextureBindingLayout. + */ +#define WGPU_EXTERNAL_TEXTURE_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUExternalTextureBindingLayout, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_ExternalTextureBindingLayout _wgpu_COMMA \ + }) _wgpu_COMMA \ +}) + +/** + * Opaque handle to an asynchronous operation. See @ref Asynchronous-Operations for more information. + * + * Default values can be set using @ref WGPU_FUTURE_INIT as initializer. + */ +typedef struct WGPUFuture { + /** + * Opaque id of the @ref WGPUFuture + * + * The `INIT` macro sets this to `0`. + */ + uint64_t id; +} WGPUFuture WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUFuture. + */ +#define WGPU_FUTURE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUFuture, { \ + /*.id=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_INSTANCE_LIMITS_INIT as initializer. + */ +typedef struct WGPUInstanceLimits { + WGPUChainedStruct * nextInChain; + /** + * The maximum number @ref WGPUFutureWaitInfo supported in a call to ::wgpuInstanceWaitAny with `timeoutNS > 0`. + * + * The `INIT` macro sets this to `0`. + */ + size_t timedWaitAnyMaxCount; +} WGPUInstanceLimits WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUInstanceLimits. + */ +#define WGPU_INSTANCE_LIMITS_INIT _wgpu_MAKE_INIT_STRUCT(WGPUInstanceLimits, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.timedWaitAnyMaxCount=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_MULTISAMPLE_STATE_INIT as initializer. + */ +typedef struct WGPUMultisampleState { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t count; + /** + * The `INIT` macro sets this to `0xFFFFFFFF`. + */ + uint32_t mask; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool alphaToCoverageEnabled; +} WGPUMultisampleState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUMultisampleState. + */ +#define WGPU_MULTISAMPLE_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUMultisampleState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.count=*/1 _wgpu_COMMA \ + /*.mask=*/0xFFFFFFFF _wgpu_COMMA \ + /*.alphaToCoverageEnabled=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_ORIGIN_3D_INIT as initializer. + */ +typedef struct WGPUOrigin3D { + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t x; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t y; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t z; +} WGPUOrigin3D WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUOrigin3D. + */ +#define WGPU_ORIGIN_3D_INIT _wgpu_MAKE_INIT_STRUCT(WGPUOrigin3D, { \ + /*.x=*/0 _wgpu_COMMA \ + /*.y=*/0 _wgpu_COMMA \ + /*.z=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_PASS_TIMESTAMP_WRITES_INIT as initializer. + */ +typedef struct WGPUPassTimestampWrites { + WGPUChainedStruct * nextInChain; + /** + * Query set to write timestamps to. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUQuerySet querySet; + /** + * The `INIT` macro sets this to @ref WGPU_QUERY_SET_INDEX_UNDEFINED. + */ + uint32_t beginningOfPassWriteIndex; + /** + * The `INIT` macro sets this to @ref WGPU_QUERY_SET_INDEX_UNDEFINED. + */ + uint32_t endOfPassWriteIndex; +} WGPUPassTimestampWrites WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUPassTimestampWrites. + */ +#define WGPU_PASS_TIMESTAMP_WRITES_INIT _wgpu_MAKE_INIT_STRUCT(WGPUPassTimestampWrites, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.querySet=*/NULL _wgpu_COMMA \ + /*.beginningOfPassWriteIndex=*/WGPU_QUERY_SET_INDEX_UNDEFINED _wgpu_COMMA \ + /*.endOfPassWriteIndex=*/WGPU_QUERY_SET_INDEX_UNDEFINED _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_PIPELINE_LAYOUT_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUPipelineLayoutDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * Array count for `bindGroupLayouts`. The `INIT` macro sets this to 0. + */ + size_t bindGroupLayoutCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUBindGroupLayout const * bindGroupLayouts; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t immediateSize; +} WGPUPipelineLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUPipelineLayoutDescriptor. + */ +#define WGPU_PIPELINE_LAYOUT_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUPipelineLayoutDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.bindGroupLayoutCount=*/0 _wgpu_COMMA \ + /*.bindGroupLayouts=*/NULL _wgpu_COMMA \ + /*.immediateSize=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_PRIMITIVE_STATE_INIT as initializer. + */ +typedef struct WGPUPrimitiveState { + WGPUChainedStruct * nextInChain; + /** + * If set to @ref WGPUPrimitiveTopology_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUPrimitiveTopology_TriangleList. + * + * The `INIT` macro sets this to @ref WGPUPrimitiveTopology_Undefined. + */ + WGPUPrimitiveTopology topology; + /** + * The `INIT` macro sets this to @ref WGPUIndexFormat_Undefined. + */ + WGPUIndexFormat stripIndexFormat; + /** + * If set to @ref WGPUFrontFace_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUFrontFace_CCW. + * + * The `INIT` macro sets this to @ref WGPUFrontFace_Undefined. + */ + WGPUFrontFace frontFace; + /** + * If set to @ref WGPUCullMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUCullMode_None. + * + * The `INIT` macro sets this to @ref WGPUCullMode_Undefined. + */ + WGPUCullMode cullMode; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool unclippedDepth; +} WGPUPrimitiveState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUPrimitiveState. + */ +#define WGPU_PRIMITIVE_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUPrimitiveState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.topology=*/WGPUPrimitiveTopology_Undefined _wgpu_COMMA \ + /*.stripIndexFormat=*/WGPUIndexFormat_Undefined _wgpu_COMMA \ + /*.frontFace=*/WGPUFrontFace_Undefined _wgpu_COMMA \ + /*.cullMode=*/WGPUCullMode_Undefined _wgpu_COMMA \ + /*.unclippedDepth=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_QUERY_SET_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUQuerySetDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to (@ref WGPUQueryType)0. + */ + WGPUQueryType type; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t count; +} WGPUQuerySetDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUQuerySetDescriptor. + */ +#define WGPU_QUERY_SET_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUQuerySetDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.type=*/_wgpu_ENUM_ZERO_INIT(WGPUQueryType) _wgpu_COMMA \ + /*.count=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_QUEUE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUQueueDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPUQueueDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUQueueDescriptor. + */ +#define WGPU_QUEUE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUQueueDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_BUNDLE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPURenderBundleDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPURenderBundleDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderBundleDescriptor. + */ +#define WGPU_RENDER_BUNDLE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderBundleDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_BUNDLE_ENCODER_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPURenderBundleEncoderDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * Array count for `colorFormats`. The `INIT` macro sets this to 0. + */ + size_t colorFormatCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUTextureFormat const * colorFormats; + /** + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat depthStencilFormat; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t sampleCount; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool depthReadOnly; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool stencilReadOnly; +} WGPURenderBundleEncoderDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderBundleEncoderDescriptor. + */ +#define WGPU_RENDER_BUNDLE_ENCODER_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderBundleEncoderDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.colorFormatCount=*/0 _wgpu_COMMA \ + /*.colorFormats=*/NULL _wgpu_COMMA \ + /*.depthStencilFormat=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.sampleCount=*/1 _wgpu_COMMA \ + /*.depthReadOnly=*/WGPU_FALSE _wgpu_COMMA \ + /*.stencilReadOnly=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_PASS_DEPTH_STENCIL_ATTACHMENT_INIT as initializer. + */ +typedef struct WGPURenderPassDepthStencilAttachment { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUTextureView view; + /** + * The `INIT` macro sets this to @ref WGPULoadOp_Undefined. + */ + WGPULoadOp depthLoadOp; + /** + * The `INIT` macro sets this to @ref WGPUStoreOp_Undefined. + */ + WGPUStoreOp depthStoreOp; + /** + * This is a @ref NullableFloatingPointType. + * + * If `NaN`, indicates an `undefined` value (as defined by the JS spec). + * Use @ref WGPU_DEPTH_CLEAR_VALUE_UNDEFINED to indicate this semantically. + * + * If infinite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to @ref WGPU_DEPTH_CLEAR_VALUE_UNDEFINED. + */ + float depthClearValue; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool depthReadOnly; + /** + * The `INIT` macro sets this to @ref WGPULoadOp_Undefined. + */ + WGPULoadOp stencilLoadOp; + /** + * The `INIT` macro sets this to @ref WGPUStoreOp_Undefined. + */ + WGPUStoreOp stencilStoreOp; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t stencilClearValue; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool stencilReadOnly; +} WGPURenderPassDepthStencilAttachment WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderPassDepthStencilAttachment. + */ +#define WGPU_RENDER_PASS_DEPTH_STENCIL_ATTACHMENT_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderPassDepthStencilAttachment, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.view=*/NULL _wgpu_COMMA \ + /*.depthLoadOp=*/WGPULoadOp_Undefined _wgpu_COMMA \ + /*.depthStoreOp=*/WGPUStoreOp_Undefined _wgpu_COMMA \ + /*.depthClearValue=*/WGPU_DEPTH_CLEAR_VALUE_UNDEFINED _wgpu_COMMA \ + /*.depthReadOnly=*/WGPU_FALSE _wgpu_COMMA \ + /*.stencilLoadOp=*/WGPULoadOp_Undefined _wgpu_COMMA \ + /*.stencilStoreOp=*/WGPUStoreOp_Undefined _wgpu_COMMA \ + /*.stencilClearValue=*/0 _wgpu_COMMA \ + /*.stencilReadOnly=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_PASS_MAX_DRAW_COUNT_INIT as initializer. + */ +typedef struct WGPURenderPassMaxDrawCount { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to `50000000`. + */ + uint64_t maxDrawCount; +} WGPURenderPassMaxDrawCount WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderPassMaxDrawCount. + */ +#define WGPU_RENDER_PASS_MAX_DRAW_COUNT_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderPassMaxDrawCount, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_RenderPassMaxDrawCount _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.maxDrawCount=*/50000000 _wgpu_COMMA \ +}) + +/** + * Extension providing requestAdapter options for implementations with WebXR interop (i.e. Wasm). + * + * Default values can be set using @ref WGPU_REQUEST_ADAPTER_WEBXR_OPTIONS_INIT as initializer. + */ +typedef struct WGPURequestAdapterWebXROptions { + WGPUChainedStruct chain; + /** + * Sets the `xrCompatible` option in the JS API. + * + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool xrCompatible; +} WGPURequestAdapterWebXROptions WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURequestAdapterWebXROptions. + */ +#define WGPU_REQUEST_ADAPTER_WEBXR_OPTIONS_INIT _wgpu_MAKE_INIT_STRUCT(WGPURequestAdapterWebXROptions, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_RequestAdapterWebXROptions _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.xrCompatible=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SAMPLER_BINDING_LAYOUT_INIT as initializer. + */ +typedef struct WGPUSamplerBindingLayout { + WGPUChainedStruct * nextInChain; + /** + * If set to @ref WGPUSamplerBindingType_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUSamplerBindingType_Filtering. + * + * The `INIT` macro sets this to @ref WGPUSamplerBindingType_Undefined. + */ + WGPUSamplerBindingType type; +} WGPUSamplerBindingLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSamplerBindingLayout. + */ +#define WGPU_SAMPLER_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSamplerBindingLayout, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.type=*/WGPUSamplerBindingType_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SAMPLER_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUSamplerDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * If set to @ref WGPUAddressMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUAddressMode_ClampToEdge. + * + * The `INIT` macro sets this to @ref WGPUAddressMode_Undefined. + */ + WGPUAddressMode addressModeU; + /** + * If set to @ref WGPUAddressMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUAddressMode_ClampToEdge. + * + * The `INIT` macro sets this to @ref WGPUAddressMode_Undefined. + */ + WGPUAddressMode addressModeV; + /** + * If set to @ref WGPUAddressMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUAddressMode_ClampToEdge. + * + * The `INIT` macro sets this to @ref WGPUAddressMode_Undefined. + */ + WGPUAddressMode addressModeW; + /** + * If set to @ref WGPUFilterMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUFilterMode_Nearest. + * + * The `INIT` macro sets this to @ref WGPUFilterMode_Undefined. + */ + WGPUFilterMode magFilter; + /** + * If set to @ref WGPUFilterMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUFilterMode_Nearest. + * + * The `INIT` macro sets this to @ref WGPUFilterMode_Undefined. + */ + WGPUFilterMode minFilter; + /** + * If set to @ref WGPUFilterMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUMipmapFilterMode_Nearest. + * + * The `INIT` macro sets this to @ref WGPUMipmapFilterMode_Undefined. + */ + WGPUMipmapFilterMode mipmapFilter; + /** + * TODO + * + * If non-finite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to `0.f`. + */ + float lodMinClamp; + /** + * TODO + * + * If non-finite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to `32.f`. + */ + float lodMaxClamp; + /** + * The `INIT` macro sets this to @ref WGPUCompareFunction_Undefined. + */ + WGPUCompareFunction compare; + /** + * The `INIT` macro sets this to `1`. + */ + uint16_t maxAnisotropy; +} WGPUSamplerDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSamplerDescriptor. + */ +#define WGPU_SAMPLER_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSamplerDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.addressModeU=*/WGPUAddressMode_Undefined _wgpu_COMMA \ + /*.addressModeV=*/WGPUAddressMode_Undefined _wgpu_COMMA \ + /*.addressModeW=*/WGPUAddressMode_Undefined _wgpu_COMMA \ + /*.magFilter=*/WGPUFilterMode_Undefined _wgpu_COMMA \ + /*.minFilter=*/WGPUFilterMode_Undefined _wgpu_COMMA \ + /*.mipmapFilter=*/WGPUMipmapFilterMode_Undefined _wgpu_COMMA \ + /*.lodMinClamp=*/0.f _wgpu_COMMA \ + /*.lodMaxClamp=*/32.f _wgpu_COMMA \ + /*.compare=*/WGPUCompareFunction_Undefined _wgpu_COMMA \ + /*.maxAnisotropy=*/1 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SHADER_SOURCE_SPIRV_INIT as initializer. + */ +typedef struct WGPUShaderSourceSPIRV { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t codeSize; + /** + * The `INIT` macro sets this to `NULL`. + */ + uint32_t const * code; +} WGPUShaderSourceSPIRV WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUShaderSourceSPIRV. + */ +#define WGPU_SHADER_SOURCE_SPIRV_INIT _wgpu_MAKE_INIT_STRUCT(WGPUShaderSourceSPIRV, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_ShaderSourceSPIRV _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.codeSize=*/0 _wgpu_COMMA \ + /*.code=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SHADER_SOURCE_WGSL_INIT as initializer. + */ +typedef struct WGPUShaderSourceWGSL { + WGPUChainedStruct chain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView code; +} WGPUShaderSourceWGSL WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUShaderSourceWGSL. + */ +#define WGPU_SHADER_SOURCE_WGSL_INIT _wgpu_MAKE_INIT_STRUCT(WGPUShaderSourceWGSL, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_ShaderSourceWGSL _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.code=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_STENCIL_FACE_STATE_INIT as initializer. + */ +typedef struct WGPUStencilFaceState { + /** + * If set to @ref WGPUCompareFunction_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUCompareFunction_Always. + * + * The `INIT` macro sets this to @ref WGPUCompareFunction_Undefined. + */ + WGPUCompareFunction compare; + /** + * If set to @ref WGPUStencilOperation_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUStencilOperation_Keep. + * + * The `INIT` macro sets this to @ref WGPUStencilOperation_Undefined. + */ + WGPUStencilOperation failOp; + /** + * If set to @ref WGPUStencilOperation_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUStencilOperation_Keep. + * + * The `INIT` macro sets this to @ref WGPUStencilOperation_Undefined. + */ + WGPUStencilOperation depthFailOp; + /** + * If set to @ref WGPUStencilOperation_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUStencilOperation_Keep. + * + * The `INIT` macro sets this to @ref WGPUStencilOperation_Undefined. + */ + WGPUStencilOperation passOp; +} WGPUStencilFaceState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUStencilFaceState. + */ +#define WGPU_STENCIL_FACE_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUStencilFaceState, { \ + /*.compare=*/WGPUCompareFunction_Undefined _wgpu_COMMA \ + /*.failOp=*/WGPUStencilOperation_Undefined _wgpu_COMMA \ + /*.depthFailOp=*/WGPUStencilOperation_Undefined _wgpu_COMMA \ + /*.passOp=*/WGPUStencilOperation_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT as initializer. + */ +typedef struct WGPUStorageTextureBindingLayout { + WGPUChainedStruct * nextInChain; + /** + * If set to @ref WGPUStorageTextureAccess_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUStorageTextureAccess_WriteOnly. + * + * The `INIT` macro sets this to @ref WGPUStorageTextureAccess_Undefined. + */ + WGPUStorageTextureAccess access; + /** + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * If set to @ref WGPUTextureViewDimension_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureViewDimension_2D. + * + * The `INIT` macro sets this to @ref WGPUTextureViewDimension_Undefined. + */ + WGPUTextureViewDimension viewDimension; +} WGPUStorageTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUStorageTextureBindingLayout. + */ +#define WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUStorageTextureBindingLayout, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.access=*/WGPUStorageTextureAccess_Undefined _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.viewDimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SUPPORTED_FEATURES_INIT as initializer. + */ +typedef struct WGPUSupportedFeatures { + /** + * Array count for `features`. The `INIT` macro sets this to 0. + */ + size_t featureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUFeatureName const * features; +} WGPUSupportedFeatures WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSupportedFeatures. + */ +#define WGPU_SUPPORTED_FEATURES_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSupportedFeatures, { \ + /*.featureCount=*/0 _wgpu_COMMA \ + /*.features=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SUPPORTED_INSTANCE_FEATURES_INIT as initializer. + */ +typedef struct WGPUSupportedInstanceFeatures { + /** + * Array count for `features`. The `INIT` macro sets this to 0. + */ + size_t featureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUInstanceFeatureName const * features; +} WGPUSupportedInstanceFeatures WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSupportedInstanceFeatures. + */ +#define WGPU_SUPPORTED_INSTANCE_FEATURES_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSupportedInstanceFeatures, { \ + /*.featureCount=*/0 _wgpu_COMMA \ + /*.features=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SUPPORTED_WGSL_LANGUAGE_FEATURES_INIT as initializer. + */ +typedef struct WGPUSupportedWGSLLanguageFeatures { + /** + * Array count for `features`. The `INIT` macro sets this to 0. + */ + size_t featureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUWGSLLanguageFeatureName const * features; +} WGPUSupportedWGSLLanguageFeatures WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSupportedWGSLLanguageFeatures. + */ +#define WGPU_SUPPORTED_WGSL_LANGUAGE_FEATURES_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSupportedWGSLLanguageFeatures, { \ + /*.featureCount=*/0 _wgpu_COMMA \ + /*.features=*/NULL _wgpu_COMMA \ +}) + +/** + * Filled by @ref wgpuSurfaceGetCapabilities with what's supported for @ref wgpuSurfaceConfigure for a pair of @ref WGPUSurface and @ref WGPUAdapter. + * + * Default values can be set using @ref WGPU_SURFACE_CAPABILITIES_INIT as initializer. + */ +typedef struct WGPUSurfaceCapabilities { + WGPUChainedStruct * nextInChain; + /** + * The bit set of supported @ref WGPUTextureUsage bits. + * Guaranteed to contain @ref WGPUTextureUsage_RenderAttachment. + * + * The `INIT` macro sets this to @ref WGPUTextureUsage_None. + */ + WGPUTextureUsage usages; + /** + * Array count for `formats`. The `INIT` macro sets this to 0. + */ + size_t formatCount; + /** + * A list of supported @ref WGPUTextureFormat values, in order of preference. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUTextureFormat const * formats; + /** + * Array count for `presentModes`. The `INIT` macro sets this to 0. + */ + size_t presentModeCount; + /** + * A list of supported @ref WGPUPresentMode values. + * Guaranteed to contain @ref WGPUPresentMode_Fifo. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUPresentMode const * presentModes; + /** + * Array count for `alphaModes`. The `INIT` macro sets this to 0. + */ + size_t alphaModeCount; + /** + * A list of supported @ref WGPUCompositeAlphaMode values. + * @ref WGPUCompositeAlphaMode_Auto will be an alias for the first element and will never be present in this array. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUCompositeAlphaMode const * alphaModes; +} WGPUSurfaceCapabilities WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceCapabilities. + */ +#define WGPU_SURFACE_CAPABILITIES_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceCapabilities, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.usages=*/WGPUTextureUsage_None _wgpu_COMMA \ + /*.formatCount=*/0 _wgpu_COMMA \ + /*.formats=*/NULL _wgpu_COMMA \ + /*.presentModeCount=*/0 _wgpu_COMMA \ + /*.presentModes=*/NULL _wgpu_COMMA \ + /*.alphaModeCount=*/0 _wgpu_COMMA \ + /*.alphaModes=*/NULL _wgpu_COMMA \ +}) + +/** + * Extension of @ref WGPUSurfaceConfiguration for color spaces and HDR. + * + * Default values can be set using @ref WGPU_SURFACE_COLOR_MANAGEMENT_INIT as initializer. + */ +typedef struct WGPUSurfaceColorManagement { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to (@ref WGPUPredefinedColorSpace)0. + */ + WGPUPredefinedColorSpace colorSpace; + /** + * The `INIT` macro sets this to (@ref WGPUToneMappingMode)0. + */ + WGPUToneMappingMode toneMappingMode; +} WGPUSurfaceColorManagement WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceColorManagement. + */ +#define WGPU_SURFACE_COLOR_MANAGEMENT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceColorManagement, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceColorManagement _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.colorSpace=*/_wgpu_ENUM_ZERO_INIT(WGPUPredefinedColorSpace) _wgpu_COMMA \ + /*.toneMappingMode=*/_wgpu_ENUM_ZERO_INIT(WGPUToneMappingMode) _wgpu_COMMA \ +}) + +/** + * Options to @ref wgpuSurfaceConfigure for defining how a @ref WGPUSurface will be rendered to and presented to the user. + * See @ref Surface-Configuration for more details. + * + * Default values can be set using @ref WGPU_SURFACE_CONFIGURATION_INIT as initializer. + */ +typedef struct WGPUSurfaceConfiguration { + WGPUChainedStruct * nextInChain; + /** + * The @ref WGPUDevice to use to render to surface's textures. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUDevice device; + /** + * The @ref WGPUTextureFormat of the surface's textures. + * + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * The @ref WGPUTextureUsage of the surface's textures. + * + * The `INIT` macro sets this to @ref WGPUTextureUsage_RenderAttachment. + */ + WGPUTextureUsage usage; + /** + * The width of the surface's textures. + * + * The `INIT` macro sets this to `0`. + */ + uint32_t width; + /** + * The height of the surface's textures. + * + * The `INIT` macro sets this to `0`. + */ + uint32_t height; + /** + * Array count for `viewFormats`. The `INIT` macro sets this to 0. + */ + size_t viewFormatCount; + /** + * The additional @ref WGPUTextureFormat for @ref WGPUTextureView format reinterpretation of the surface's textures. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUTextureFormat const * viewFormats; + /** + * How the surface's frames will be composited on the screen. + * + * If set to @ref WGPUCompositeAlphaMode_Auto, + * [defaults] to @ref WGPUCompositeAlphaMode_Inherit in native (allowing the mode + * to be configured externally), and to @ref WGPUCompositeAlphaMode_Opaque in Wasm. + * + * The `INIT` macro sets this to @ref WGPUCompositeAlphaMode_Auto. + */ + WGPUCompositeAlphaMode alphaMode; + /** + * When and in which order the surface's frames will be shown on the screen. + * + * If set to @ref WGPUPresentMode_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUPresentMode_Fifo. + * + * The `INIT` macro sets this to @ref WGPUPresentMode_Undefined. + */ + WGPUPresentMode presentMode; +} WGPUSurfaceConfiguration WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceConfiguration. + */ +#define WGPU_SURFACE_CONFIGURATION_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceConfiguration, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.device=*/NULL _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.usage=*/WGPUTextureUsage_RenderAttachment _wgpu_COMMA \ + /*.width=*/0 _wgpu_COMMA \ + /*.height=*/0 _wgpu_COMMA \ + /*.viewFormatCount=*/0 _wgpu_COMMA \ + /*.viewFormats=*/NULL _wgpu_COMMA \ + /*.alphaMode=*/WGPUCompositeAlphaMode_Auto _wgpu_COMMA \ + /*.presentMode=*/WGPUPresentMode_Undefined _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping an Android [`ANativeWindow`](https://developer.android.com/ndk/reference/group/a-native-window). + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_ANDROID_NATIVE_WINDOW_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceAndroidNativeWindow { + WGPUChainedStruct chain; + /** + * The pointer to the [`ANativeWindow`](https://developer.android.com/ndk/reference/group/a-native-window) that will be wrapped by the @ref WGPUSurface. + * + * The `INIT` macro sets this to `NULL`. + */ + void * window; +} WGPUSurfaceSourceAndroidNativeWindow WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceAndroidNativeWindow. + */ +#define WGPU_SURFACE_SOURCE_ANDROID_NATIVE_WINDOW_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceAndroidNativeWindow, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceAndroidNativeWindow _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.window=*/NULL _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping a [`CAMetalLayer`](https://developer.apple.com/documentation/quartzcore/cametallayer?language=objc). + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_METAL_LAYER_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceMetalLayer { + WGPUChainedStruct chain; + /** + * The pointer to the [`CAMetalLayer`](https://developer.apple.com/documentation/quartzcore/cametallayer?language=objc) that will be wrapped by the @ref WGPUSurface. + * + * The `INIT` macro sets this to `NULL`. + */ + void * layer; +} WGPUSurfaceSourceMetalLayer WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceMetalLayer. + */ +#define WGPU_SURFACE_SOURCE_METAL_LAYER_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceMetalLayer, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceMetalLayer _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.layer=*/NULL _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping a [Wayland](https://wayland.freedesktop.org/) [`wl_surface`](https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_surface). + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_WAYLAND_SURFACE_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceWaylandSurface { + WGPUChainedStruct chain; + /** + * A [`wl_display`](https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_display) for this Wayland instance. + * + * The `INIT` macro sets this to `NULL`. + */ + void * display; + /** + * A [`wl_surface`](https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_surface) that will be wrapped by the @ref WGPUSurface + * + * The `INIT` macro sets this to `NULL`. + */ + void * surface; +} WGPUSurfaceSourceWaylandSurface WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceWaylandSurface. + */ +#define WGPU_SURFACE_SOURCE_WAYLAND_SURFACE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceWaylandSurface, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceWaylandSurface _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.display=*/NULL _wgpu_COMMA \ + /*.surface=*/NULL _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping a Windows [`HWND`](https://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd). + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_WINDOWS_HWND_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceWindowsHWND { + WGPUChainedStruct chain; + /** + * The [`HINSTANCE`](https://learn.microsoft.com/en-us/windows/win32/learnwin32/winmain--the-application-entry-point) for this application. + * Most commonly `GetModuleHandle(nullptr)`. + * + * The `INIT` macro sets this to `NULL`. + */ + void * hinstance; + /** + * The [`HWND`](https://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd) that will be wrapped by the @ref WGPUSurface. + * + * The `INIT` macro sets this to `NULL`. + */ + void * hwnd; +} WGPUSurfaceSourceWindowsHWND WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceWindowsHWND. + */ +#define WGPU_SURFACE_SOURCE_WINDOWS_HWND_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceWindowsHWND, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceWindowsHWND _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.hinstance=*/NULL _wgpu_COMMA \ + /*.hwnd=*/NULL _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping an [XCB](https://xcb.freedesktop.org/) `xcb_window_t`. + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_XCB_WINDOW_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceXCBWindow { + WGPUChainedStruct chain; + /** + * The `xcb_connection_t` for the connection to the X server. + * + * The `INIT` macro sets this to `NULL`. + */ + void * connection; + /** + * The `xcb_window_t` for the window that will be wrapped by the @ref WGPUSurface. + * + * The `INIT` macro sets this to `0`. + */ + uint32_t window; +} WGPUSurfaceSourceXCBWindow WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceXCBWindow. + */ +#define WGPU_SURFACE_SOURCE_XCB_WINDOW_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceXCBWindow, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceXCBWindow _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.connection=*/NULL _wgpu_COMMA \ + /*.window=*/0 _wgpu_COMMA \ +}) + +/** + * Chained in @ref WGPUSurfaceDescriptor to make an @ref WGPUSurface wrapping an [Xlib](https://www.x.org/releases/current/doc/libX11/libX11/libX11.html) `Window`. + * + * Default values can be set using @ref WGPU_SURFACE_SOURCE_XLIB_WINDOW_INIT as initializer. + */ +typedef struct WGPUSurfaceSourceXlibWindow { + WGPUChainedStruct chain; + /** + * A pointer to the [`Display`](https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Opening_the_Display) connected to the X server. + * + * The `INIT` macro sets this to `NULL`. + */ + void * display; + /** + * The [`Window`](https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Creating_Windows) that will be wrapped by the @ref WGPUSurface. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t window; +} WGPUSurfaceSourceXlibWindow WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceSourceXlibWindow. + */ +#define WGPU_SURFACE_SOURCE_XLIB_WINDOW_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceSourceXlibWindow, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_SurfaceSourceXlibWindow _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.display=*/NULL _wgpu_COMMA \ + /*.window=*/0 _wgpu_COMMA \ +}) + +/** + * Queried each frame from a @ref WGPUSurface to get a @ref WGPUTexture to render to along with some metadata. + * See @ref Surface-Presenting for more details. + * + * Default values can be set using @ref WGPU_SURFACE_TEXTURE_INIT as initializer. + */ +typedef struct WGPUSurfaceTexture { + WGPUChainedStruct * nextInChain; + /** + * The @ref WGPUTexture representing the frame that will be shown on the surface. + * It is @ref ReturnedWithOwnership from @ref wgpuSurfaceGetCurrentTexture. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPUTexture texture; + /** + * Whether the call to @ref wgpuSurfaceGetCurrentTexture succeeded and a hint as to why it might not have. + * + * The `INIT` macro sets this to (@ref WGPUSurfaceGetCurrentTextureStatus)0. + */ + WGPUSurfaceGetCurrentTextureStatus status; +} WGPUSurfaceTexture WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceTexture. + */ +#define WGPU_SURFACE_TEXTURE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceTexture, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.texture=*/NULL _wgpu_COMMA \ + /*.status=*/_wgpu_ENUM_ZERO_INIT(WGPUSurfaceGetCurrentTextureStatus) _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXEL_COPY_BUFFER_LAYOUT_INIT as initializer. + */ +typedef struct WGPUTexelCopyBufferLayout { + /** + * The `INIT` macro sets this to `0`. + */ + uint64_t offset; + /** + * The `INIT` macro sets this to @ref WGPU_COPY_STRIDE_UNDEFINED. + */ + uint32_t bytesPerRow; + /** + * The `INIT` macro sets this to @ref WGPU_COPY_STRIDE_UNDEFINED. + */ + uint32_t rowsPerImage; +} WGPUTexelCopyBufferLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTexelCopyBufferLayout. + */ +#define WGPU_TEXEL_COPY_BUFFER_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTexelCopyBufferLayout, { \ + /*.offset=*/0 _wgpu_COMMA \ + /*.bytesPerRow=*/WGPU_COPY_STRIDE_UNDEFINED _wgpu_COMMA \ + /*.rowsPerImage=*/WGPU_COPY_STRIDE_UNDEFINED _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXTURE_BINDING_LAYOUT_INIT as initializer. + */ +typedef struct WGPUTextureBindingLayout { + WGPUChainedStruct * nextInChain; + /** + * If set to @ref WGPUTextureSampleType_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureSampleType_Float. + * + * The `INIT` macro sets this to @ref WGPUTextureSampleType_Undefined. + */ + WGPUTextureSampleType sampleType; + /** + * If set to @ref WGPUTextureViewDimension_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureViewDimension_2D. + * + * The `INIT` macro sets this to @ref WGPUTextureViewDimension_Undefined. + */ + WGPUTextureViewDimension viewDimension; + /** + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool multisampled; +} WGPUTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureBindingLayout. + */ +#define WGPU_TEXTURE_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureBindingLayout, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.sampleType=*/WGPUTextureSampleType_Undefined _wgpu_COMMA \ + /*.viewDimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \ + /*.multisampled=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Note: While Compatibility Mode is optional to implement, this extension struct + * is required to be accepted (but per the WebGPU spec, its contents are ignored + * on devices that have the @ref WGPUFeatureName_CoreFeaturesAndLimits feature). + * + * Default values can be set using @ref WGPU_TEXTURE_BINDING_VIEW_DIMENSION_INIT as initializer. + */ +typedef struct WGPUTextureBindingViewDimension { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to @ref WGPUTextureViewDimension_Undefined. + */ + WGPUTextureViewDimension textureBindingViewDimension; +} WGPUTextureBindingViewDimension WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureBindingViewDimension. + */ +#define WGPU_TEXTURE_BINDING_VIEW_DIMENSION_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureBindingViewDimension, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_TextureBindingViewDimension _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.textureBindingViewDimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \ +}) + +/** + * When accessed by a shader, the red/green/blue/alpha channels are replaced + * by the value corresponding to the component specified in r, g, b, and a, + * respectively unlike the JS API which uses a string of length four, with + * each character mapping to the texture view's red/green/blue/alpha channels. + * + * Default values can be set using @ref WGPU_TEXTURE_COMPONENT_SWIZZLE_INIT as initializer. + */ +typedef struct WGPUTextureComponentSwizzle { + /** + * The value that replaces the red channel in the shader. + * + * If set to @ref WGPUComponentSwizzle_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUComponentSwizzle_R. + * + * The `INIT` macro sets this to @ref WGPUComponentSwizzle_Undefined. + */ + WGPUComponentSwizzle r; + /** + * The value that replaces the green channel in the shader. + * + * If set to @ref WGPUComponentSwizzle_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUComponentSwizzle_G. + * + * The `INIT` macro sets this to @ref WGPUComponentSwizzle_Undefined. + */ + WGPUComponentSwizzle g; + /** + * The value that replaces the blue channel in the shader. + * + * If set to @ref WGPUComponentSwizzle_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUComponentSwizzle_B. + * + * The `INIT` macro sets this to @ref WGPUComponentSwizzle_Undefined. + */ + WGPUComponentSwizzle b; + /** + * The value that replaces the alpha channel in the shader. + * + * If set to @ref WGPUComponentSwizzle_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUComponentSwizzle_A. + * + * The `INIT` macro sets this to @ref WGPUComponentSwizzle_Undefined. + */ + WGPUComponentSwizzle a; +} WGPUTextureComponentSwizzle WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureComponentSwizzle. + */ +#define WGPU_TEXTURE_COMPONENT_SWIZZLE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureComponentSwizzle, { \ + /*.r=*/WGPUComponentSwizzle_Undefined _wgpu_COMMA \ + /*.g=*/WGPUComponentSwizzle_Undefined _wgpu_COMMA \ + /*.b=*/WGPUComponentSwizzle_Undefined _wgpu_COMMA \ + /*.a=*/WGPUComponentSwizzle_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_VERTEX_ATTRIBUTE_INIT as initializer. + */ +typedef struct WGPUVertexAttribute { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to (@ref WGPUVertexFormat)0. + */ + WGPUVertexFormat format; + /** + * The `INIT` macro sets this to `0`. + */ + uint64_t offset; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t shaderLocation; +} WGPUVertexAttribute WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUVertexAttribute. + */ +#define WGPU_VERTEX_ATTRIBUTE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUVertexAttribute, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.format=*/_wgpu_ENUM_ZERO_INIT(WGPUVertexFormat) _wgpu_COMMA \ + /*.offset=*/0 _wgpu_COMMA \ + /*.shaderLocation=*/0 _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BIND_GROUP_ENTRY_INIT as initializer. + */ +typedef struct WGPUBindGroupEntry { + WGPUChainedStruct * nextInChain; + /** + * Binding index in the bind group. + * + * The `INIT` macro sets this to `0`. + */ + uint32_t binding; + /** + * Set this if the binding is a buffer object. + * Otherwise must be null. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUBuffer buffer; + /** + * If the binding is a buffer, this is the byte offset of the binding range. + * Otherwise ignored. + * + * The `INIT` macro sets this to `0`. + */ + uint64_t offset; + /** + * If the binding is a buffer, this is the byte size of the binding range + * (@ref WGPU_WHOLE_SIZE means the binding ends at the end of the buffer). + * Otherwise ignored. + * + * The `INIT` macro sets this to @ref WGPU_WHOLE_SIZE. + */ + uint64_t size; + /** + * Set this if the binding is a sampler object. + * Otherwise must be null. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUSampler sampler; + /** + * Set this if the binding is a texture view object. + * Otherwise must be null. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUTextureView textureView; +} WGPUBindGroupEntry WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBindGroupEntry. + */ +#define WGPU_BIND_GROUP_ENTRY_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBindGroupEntry, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.binding=*/0 _wgpu_COMMA \ + /*.buffer=*/NULL _wgpu_COMMA \ + /*.offset=*/0 _wgpu_COMMA \ + /*.size=*/WGPU_WHOLE_SIZE _wgpu_COMMA \ + /*.sampler=*/NULL _wgpu_COMMA \ + /*.textureView=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BIND_GROUP_LAYOUT_ENTRY_INIT as initializer. + */ +typedef struct WGPUBindGroupLayoutEntry { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t binding; + /** + * The `INIT` macro sets this to @ref WGPUShaderStage_None. + */ + WGPUShaderStage visibility; + /** + * If non-zero, this entry defines a binding array with this size. + * + * The `INIT` macro sets this to `0`. + */ + uint32_t bindingArraySize; + /** + * The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`). + */ + WGPUBufferBindingLayout buffer; + /** + * The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`). + */ + WGPUSamplerBindingLayout sampler; + /** + * The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`). + */ + WGPUTextureBindingLayout texture; + /** + * The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`). + */ + WGPUStorageTextureBindingLayout storageTexture; +} WGPUBindGroupLayoutEntry WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBindGroupLayoutEntry. + */ +#define WGPU_BIND_GROUP_LAYOUT_ENTRY_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBindGroupLayoutEntry, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.binding=*/0 _wgpu_COMMA \ + /*.visibility=*/WGPUShaderStage_None _wgpu_COMMA \ + /*.bindingArraySize=*/0 _wgpu_COMMA \ + /*.buffer=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \ + /*.sampler=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \ + /*.texture=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \ + /*.storageTexture=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BLEND_STATE_INIT as initializer. + */ +typedef struct WGPUBlendState { + /** + * The `INIT` macro sets this to @ref WGPU_BLEND_COMPONENT_INIT. + */ + WGPUBlendComponent color; + /** + * The `INIT` macro sets this to @ref WGPU_BLEND_COMPONENT_INIT. + */ + WGPUBlendComponent alpha; +} WGPUBlendState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBlendState. + */ +#define WGPU_BLEND_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBlendState, { \ + /*.color=*/WGPU_BLEND_COMPONENT_INIT _wgpu_COMMA \ + /*.alpha=*/WGPU_BLEND_COMPONENT_INIT _wgpu_COMMA \ +}) + +/** + * This is an @ref ImplementationAllocatedStructChain root. + * Arbitrary chains must be handled gracefully by the application! + * + * Default values can be set using @ref WGPU_COMPILATION_INFO_INIT as initializer. + */ +typedef struct WGPUCompilationInfo { + WGPUChainedStruct * nextInChain; + /** + * Array count for `messages`. The `INIT` macro sets this to 0. + */ + size_t messageCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUCompilationMessage const * messages; +} WGPUCompilationInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUCompilationInfo. + */ +#define WGPU_COMPILATION_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUCompilationInfo, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.messageCount=*/0 _wgpu_COMMA \ + /*.messages=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COMPUTE_PASS_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUComputePassDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUPassTimestampWrites const * timestampWrites; +} WGPUComputePassDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUComputePassDescriptor. + */ +#define WGPU_COMPUTE_PASS_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUComputePassDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.timestampWrites=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COMPUTE_STATE_INIT as initializer. + */ +typedef struct WGPUComputeState { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUShaderModule module; + /** + * This is a \ref NullableInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView entryPoint; + /** + * Array count for `constants`. The `INIT` macro sets this to 0. + */ + size_t constantCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUConstantEntry const * constants; +} WGPUComputeState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUComputeState. + */ +#define WGPU_COMPUTE_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUComputeState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.module=*/NULL _wgpu_COMMA \ + /*.entryPoint=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.constantCount=*/0 _wgpu_COMMA \ + /*.constants=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_DEPTH_STENCIL_STATE_INIT as initializer. + */ +typedef struct WGPUDepthStencilState { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * The `INIT` macro sets this to @ref WGPUOptionalBool_Undefined. + */ + WGPUOptionalBool depthWriteEnabled; + /** + * The `INIT` macro sets this to @ref WGPUCompareFunction_Undefined. + */ + WGPUCompareFunction depthCompare; + /** + * The `INIT` macro sets this to @ref WGPU_STENCIL_FACE_STATE_INIT. + */ + WGPUStencilFaceState stencilFront; + /** + * The `INIT` macro sets this to @ref WGPU_STENCIL_FACE_STATE_INIT. + */ + WGPUStencilFaceState stencilBack; + /** + * The `INIT` macro sets this to `0xFFFFFFFF`. + */ + uint32_t stencilReadMask; + /** + * The `INIT` macro sets this to `0xFFFFFFFF`. + */ + uint32_t stencilWriteMask; + /** + * The `INIT` macro sets this to `0`. + */ + int32_t depthBias; + /** + * TODO + * + * If non-finite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to `0.f`. + */ + float depthBiasSlopeScale; + /** + * TODO + * + * If non-finite, produces a @ref NonFiniteFloatValueError. + * + * The `INIT` macro sets this to `0.f`. + */ + float depthBiasClamp; +} WGPUDepthStencilState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUDepthStencilState. + */ +#define WGPU_DEPTH_STENCIL_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUDepthStencilState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.depthWriteEnabled=*/WGPUOptionalBool_Undefined _wgpu_COMMA \ + /*.depthCompare=*/WGPUCompareFunction_Undefined _wgpu_COMMA \ + /*.stencilFront=*/WGPU_STENCIL_FACE_STATE_INIT _wgpu_COMMA \ + /*.stencilBack=*/WGPU_STENCIL_FACE_STATE_INIT _wgpu_COMMA \ + /*.stencilReadMask=*/0xFFFFFFFF _wgpu_COMMA \ + /*.stencilWriteMask=*/0xFFFFFFFF _wgpu_COMMA \ + /*.depthBias=*/0 _wgpu_COMMA \ + /*.depthBiasSlopeScale=*/0.f _wgpu_COMMA \ + /*.depthBiasClamp=*/0.f _wgpu_COMMA \ +}) + +/** + * Struct holding a future to wait on, and a `completed` boolean flag. + * + * Default values can be set using @ref WGPU_FUTURE_WAIT_INFO_INIT as initializer. + */ +typedef struct WGPUFutureWaitInfo { + /** + * The future to wait on. + * + * The `INIT` macro sets this to @ref WGPU_FUTURE_INIT. + */ + WGPUFuture future; + /** + * Whether or not the future completed. + * + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool completed; +} WGPUFutureWaitInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUFutureWaitInfo. + */ +#define WGPU_FUTURE_WAIT_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUFutureWaitInfo, { \ + /*.future=*/WGPU_FUTURE_INIT _wgpu_COMMA \ + /*.completed=*/WGPU_FALSE _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_INSTANCE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUInstanceDescriptor { + WGPUChainedStruct * nextInChain; + /** + * Array count for `requiredFeatures`. The `INIT` macro sets this to 0. + */ + size_t requiredFeatureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUInstanceFeatureName const * requiredFeatures; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUInstanceLimits const * requiredLimits; +} WGPUInstanceDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUInstanceDescriptor. + */ +#define WGPU_INSTANCE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUInstanceDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.requiredFeatureCount=*/0 _wgpu_COMMA \ + /*.requiredFeatures=*/NULL _wgpu_COMMA \ + /*.requiredLimits=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_LIMITS_INIT as initializer. + */ +typedef struct WGPULimits { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxTextureDimension1D; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxTextureDimension2D; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxTextureDimension3D; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxTextureArrayLayers; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxBindGroups; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxBindGroupsPlusVertexBuffers; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxBindingsPerBindGroup; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxDynamicUniformBuffersPerPipelineLayout; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxDynamicStorageBuffersPerPipelineLayout; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxSampledTexturesPerShaderStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxSamplersPerShaderStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageBuffersPerShaderStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxStorageTexturesPerShaderStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxUniformBuffersPerShaderStage; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U64_UNDEFINED. + */ + uint64_t maxUniformBufferBindingSize; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U64_UNDEFINED. + */ + uint64_t maxStorageBufferBindingSize; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t minUniformBufferOffsetAlignment; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t minStorageBufferOffsetAlignment; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxVertexBuffers; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U64_UNDEFINED. + */ + uint64_t maxBufferSize; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxVertexAttributes; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxVertexBufferArrayStride; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxInterStageShaderVariables; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxColorAttachments; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxColorAttachmentBytesPerSample; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeWorkgroupStorageSize; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeInvocationsPerWorkgroup; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeWorkgroupSizeX; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeWorkgroupSizeY; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeWorkgroupSizeZ; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxComputeWorkgroupsPerDimension; + /** + * The `INIT` macro sets this to @ref WGPU_LIMIT_U32_UNDEFINED. + */ + uint32_t maxImmediateSize; +} WGPULimits WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPULimits. + */ +#define WGPU_LIMITS_INIT _wgpu_MAKE_INIT_STRUCT(WGPULimits, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.maxTextureDimension1D=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxTextureDimension2D=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxTextureDimension3D=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxTextureArrayLayers=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxBindGroups=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxBindGroupsPlusVertexBuffers=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxBindingsPerBindGroup=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxDynamicUniformBuffersPerPipelineLayout=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxDynamicStorageBuffersPerPipelineLayout=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxSampledTexturesPerShaderStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxSamplersPerShaderStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxStorageBuffersPerShaderStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxStorageTexturesPerShaderStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxUniformBuffersPerShaderStage=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxUniformBufferBindingSize=*/WGPU_LIMIT_U64_UNDEFINED _wgpu_COMMA \ + /*.maxStorageBufferBindingSize=*/WGPU_LIMIT_U64_UNDEFINED _wgpu_COMMA \ + /*.minUniformBufferOffsetAlignment=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.minStorageBufferOffsetAlignment=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxVertexBuffers=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxBufferSize=*/WGPU_LIMIT_U64_UNDEFINED _wgpu_COMMA \ + /*.maxVertexAttributes=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxVertexBufferArrayStride=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxInterStageShaderVariables=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxColorAttachments=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxColorAttachmentBytesPerSample=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeWorkgroupStorageSize=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeInvocationsPerWorkgroup=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeWorkgroupSizeX=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeWorkgroupSizeY=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeWorkgroupSizeZ=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxComputeWorkgroupsPerDimension=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ + /*.maxImmediateSize=*/WGPU_LIMIT_U32_UNDEFINED _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_PASS_COLOR_ATTACHMENT_INIT as initializer. + */ +typedef struct WGPURenderPassColorAttachment { + WGPUChainedStruct * nextInChain; + /** + * If `NULL`, indicates a hole in the parent + * @ref WGPURenderPassDescriptor::colorAttachments array. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUTextureView view; + /** + * The `INIT` macro sets this to @ref WGPU_DEPTH_SLICE_UNDEFINED. + */ + uint32_t depthSlice; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUTextureView resolveTarget; + /** + * The `INIT` macro sets this to @ref WGPULoadOp_Undefined. + */ + WGPULoadOp loadOp; + /** + * The `INIT` macro sets this to @ref WGPUStoreOp_Undefined. + */ + WGPUStoreOp storeOp; + /** + * The `INIT` macro sets this to @ref WGPU_COLOR_INIT. + */ + WGPUColor clearValue; +} WGPURenderPassColorAttachment WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderPassColorAttachment. + */ +#define WGPU_RENDER_PASS_COLOR_ATTACHMENT_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderPassColorAttachment, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.view=*/NULL _wgpu_COMMA \ + /*.depthSlice=*/WGPU_DEPTH_SLICE_UNDEFINED _wgpu_COMMA \ + /*.resolveTarget=*/NULL _wgpu_COMMA \ + /*.loadOp=*/WGPULoadOp_Undefined _wgpu_COMMA \ + /*.storeOp=*/WGPUStoreOp_Undefined _wgpu_COMMA \ + /*.clearValue=*/WGPU_COLOR_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_REQUEST_ADAPTER_OPTIONS_INIT as initializer. + */ +typedef struct WGPURequestAdapterOptions { + WGPUChainedStruct * nextInChain; + /** + * "Feature level" for the adapter request. If an adapter is returned, it must support the features and limits in the requested feature level. + * + * If set to @ref WGPUFeatureLevel_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUFeatureLevel_Core. + * Additionally, implementations may ignore @ref WGPUFeatureLevel_Compatibility + * and provide @ref WGPUFeatureLevel_Core instead. + * + * The `INIT` macro sets this to @ref WGPUFeatureLevel_Undefined. + */ + WGPUFeatureLevel featureLevel; + /** + * The `INIT` macro sets this to @ref WGPUPowerPreference_Undefined. + */ + WGPUPowerPreference powerPreference; + /** + * If true, requires the adapter to be a "fallback" adapter as defined by the JS spec. + * If this is not possible, the request returns null. + * + * The `INIT` macro sets this to `WGPU_FALSE`. + */ + WGPUBool forceFallbackAdapter; + /** + * If set, requires the adapter to have a particular backend type. + * If this is not possible, the request returns null. + * + * The `INIT` macro sets this to @ref WGPUBackendType_Undefined. + */ + WGPUBackendType backendType; + /** + * If set, requires the adapter to be able to output to a particular surface. + * If this is not possible, the request returns null. + * + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUSurface compatibleSurface; +} WGPURequestAdapterOptions WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURequestAdapterOptions. + */ +#define WGPU_REQUEST_ADAPTER_OPTIONS_INIT _wgpu_MAKE_INIT_STRUCT(WGPURequestAdapterOptions, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.featureLevel=*/WGPUFeatureLevel_Undefined _wgpu_COMMA \ + /*.powerPreference=*/WGPUPowerPreference_Undefined _wgpu_COMMA \ + /*.forceFallbackAdapter=*/WGPU_FALSE _wgpu_COMMA \ + /*.backendType=*/WGPUBackendType_Undefined _wgpu_COMMA \ + /*.compatibleSurface=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_SHADER_MODULE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUShaderModuleDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPUShaderModuleDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUShaderModuleDescriptor. + */ +#define WGPU_SHADER_MODULE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUShaderModuleDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * The root descriptor for the creation of an @ref WGPUSurface with @ref wgpuInstanceCreateSurface. + * It isn't sufficient by itself and must have one of the `WGPUSurfaceSource*` in its chain. + * See @ref Surface-Creation for more details. + * + * Default values can be set using @ref WGPU_SURFACE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUSurfaceDescriptor { + WGPUChainedStruct * nextInChain; + /** + * Label used to refer to the object. + * + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; +} WGPUSurfaceDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUSurfaceDescriptor. + */ +#define WGPU_SURFACE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSurfaceDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXEL_COPY_BUFFER_INFO_INIT as initializer. + */ +typedef struct WGPUTexelCopyBufferInfo { + /** + * The `INIT` macro sets this to @ref WGPU_TEXEL_COPY_BUFFER_LAYOUT_INIT. + */ + WGPUTexelCopyBufferLayout layout; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUBuffer buffer; +} WGPUTexelCopyBufferInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTexelCopyBufferInfo. + */ +#define WGPU_TEXEL_COPY_BUFFER_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTexelCopyBufferInfo, { \ + /*.layout=*/WGPU_TEXEL_COPY_BUFFER_LAYOUT_INIT _wgpu_COMMA \ + /*.buffer=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXEL_COPY_TEXTURE_INFO_INIT as initializer. + */ +typedef struct WGPUTexelCopyTextureInfo { + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUTexture texture; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t mipLevel; + /** + * The `INIT` macro sets this to @ref WGPU_ORIGIN_3D_INIT. + */ + WGPUOrigin3D origin; + /** + * If set to @ref WGPUTextureAspect_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureAspect_All. + * + * The `INIT` macro sets this to @ref WGPUTextureAspect_Undefined. + */ + WGPUTextureAspect aspect; +} WGPUTexelCopyTextureInfo WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTexelCopyTextureInfo. + */ +#define WGPU_TEXEL_COPY_TEXTURE_INFO_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTexelCopyTextureInfo, { \ + /*.texture=*/NULL _wgpu_COMMA \ + /*.mipLevel=*/0 _wgpu_COMMA \ + /*.origin=*/WGPU_ORIGIN_3D_INIT _wgpu_COMMA \ + /*.aspect=*/WGPUTextureAspect_Undefined _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXTURE_COMPONENT_SWIZZLE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUTextureComponentSwizzleDescriptor { + WGPUChainedStruct chain; + /** + * The `INIT` macro sets this to @ref WGPU_TEXTURE_COMPONENT_SWIZZLE_INIT. + */ + WGPUTextureComponentSwizzle swizzle; +} WGPUTextureComponentSwizzleDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureComponentSwizzleDescriptor. + */ +#define WGPU_TEXTURE_COMPONENT_SWIZZLE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureComponentSwizzleDescriptor, { \ + /*.chain=*/_wgpu_MAKE_INIT_STRUCT(WGPUChainedStruct, { \ + /*.next=*/NULL _wgpu_COMMA \ + /*.sType=*/WGPUSType_TextureComponentSwizzleDescriptor _wgpu_COMMA \ + }) _wgpu_COMMA \ + /*.swizzle=*/WGPU_TEXTURE_COMPONENT_SWIZZLE_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXTURE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUTextureDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to @ref WGPUTextureUsage_None. + */ + WGPUTextureUsage usage; + /** + * If set to @ref WGPUTextureDimension_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureDimension_2D. + * + * The `INIT` macro sets this to @ref WGPUTextureDimension_Undefined. + */ + WGPUTextureDimension dimension; + /** + * The `INIT` macro sets this to @ref WGPU_EXTENT_3D_INIT. + */ + WGPUExtent3D size; + /** + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t mipLevelCount; + /** + * The `INIT` macro sets this to `1`. + */ + uint32_t sampleCount; + /** + * Array count for `viewFormats`. The `INIT` macro sets this to 0. + */ + size_t viewFormatCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUTextureFormat const * viewFormats; +} WGPUTextureDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureDescriptor. + */ +#define WGPU_TEXTURE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.usage=*/WGPUTextureUsage_None _wgpu_COMMA \ + /*.dimension=*/WGPUTextureDimension_Undefined _wgpu_COMMA \ + /*.size=*/WGPU_EXTENT_3D_INIT _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.mipLevelCount=*/1 _wgpu_COMMA \ + /*.sampleCount=*/1 _wgpu_COMMA \ + /*.viewFormatCount=*/0 _wgpu_COMMA \ + /*.viewFormats=*/NULL _wgpu_COMMA \ +}) + +/** + * If `attributes` is empty *and* `stepMode` is @ref WGPUVertexStepMode_Undefined, + * indicates a "hole" in the parent @ref WGPUVertexState `buffers` array, + * with behavior equivalent to `null` in the JS API. + * + * If `attributes` is empty but `stepMode` is *not* @ref WGPUVertexStepMode_Undefined, + * indicates a vertex buffer with no attributes, with behavior equivalent to + * `{ attributes: [] }` in the JS API. (TODO: If the JS API changes not to + * distinguish these cases, then this distinction doesn't matter and we can + * remove this documentation.) + * + * If `stepMode` is @ref WGPUVertexStepMode_Undefined but `attributes` is *not* empty, + * `stepMode` [defaults](@ref SentinelValues) to @ref WGPUVertexStepMode_Vertex. + * + * Default values can be set using @ref WGPU_VERTEX_BUFFER_LAYOUT_INIT as initializer. + */ +typedef struct WGPUVertexBufferLayout { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to @ref WGPUVertexStepMode_Undefined. + */ + WGPUVertexStepMode stepMode; + /** + * The `INIT` macro sets this to `0`. + */ + uint64_t arrayStride; + /** + * Array count for `attributes`. The `INIT` macro sets this to 0. + */ + size_t attributeCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUVertexAttribute const * attributes; +} WGPUVertexBufferLayout WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUVertexBufferLayout. + */ +#define WGPU_VERTEX_BUFFER_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUVertexBufferLayout, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.stepMode=*/WGPUVertexStepMode_Undefined _wgpu_COMMA \ + /*.arrayStride=*/0 _wgpu_COMMA \ + /*.attributeCount=*/0 _wgpu_COMMA \ + /*.attributes=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BIND_GROUP_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUBindGroupDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUBindGroupLayout layout; + /** + * Array count for `entries`. The `INIT` macro sets this to 0. + */ + size_t entryCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUBindGroupEntry const * entries; +} WGPUBindGroupDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBindGroupDescriptor. + */ +#define WGPU_BIND_GROUP_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBindGroupDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.layout=*/NULL _wgpu_COMMA \ + /*.entryCount=*/0 _wgpu_COMMA \ + /*.entries=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_BIND_GROUP_LAYOUT_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUBindGroupLayoutDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * Array count for `entries`. The `INIT` macro sets this to 0. + */ + size_t entryCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUBindGroupLayoutEntry const * entries; +} WGPUBindGroupLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUBindGroupLayoutDescriptor. + */ +#define WGPU_BIND_GROUP_LAYOUT_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBindGroupLayoutDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.entryCount=*/0 _wgpu_COMMA \ + /*.entries=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COLOR_TARGET_STATE_INIT as initializer. + */ +typedef struct WGPUColorTargetState { + WGPUChainedStruct * nextInChain; + /** + * The texture format of the target. If @ref WGPUTextureFormat_Undefined, + * indicates a "hole" in the parent @ref WGPUFragmentState `targets` array: + * the pipeline does not output a value at this `location`. + * + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUBlendState const * blend; + /** + * The `INIT` macro sets this to @ref WGPUColorWriteMask_All. + */ + WGPUColorWriteMask writeMask; +} WGPUColorTargetState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUColorTargetState. + */ +#define WGPU_COLOR_TARGET_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUColorTargetState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.blend=*/NULL _wgpu_COMMA \ + /*.writeMask=*/WGPUColorWriteMask_All _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_COMPUTE_PIPELINE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUComputePipelineDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUPipelineLayout layout; + /** + * The `INIT` macro sets this to @ref WGPU_COMPUTE_STATE_INIT. + */ + WGPUComputeState compute; +} WGPUComputePipelineDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUComputePipelineDescriptor. + */ +#define WGPU_COMPUTE_PIPELINE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUComputePipelineDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.layout=*/NULL _wgpu_COMMA \ + /*.compute=*/WGPU_COMPUTE_STATE_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_DEVICE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUDeviceDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * Array count for `requiredFeatures`. The `INIT` macro sets this to 0. + */ + size_t requiredFeatureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUFeatureName const * requiredFeatures; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPULimits const * requiredLimits; + /** + * The `INIT` macro sets this to @ref WGPU_QUEUE_DESCRIPTOR_INIT. + */ + WGPUQueueDescriptor defaultQueue; + /** + * The `INIT` macro sets this to @ref WGPU_DEVICE_LOST_CALLBACK_INFO_INIT. + */ + WGPUDeviceLostCallbackInfo deviceLostCallbackInfo; + /** + * Called when there is an uncaptured error on this device, from any thread. + * See @ref ErrorScopes. + * + * **Important:** This callback does not have a configurable @ref WGPUCallbackMode; it may be called at any time (like @ref WGPUCallbackMode_AllowSpontaneous). As such, calls into the `webgpu.h` API from this callback are unsafe. See @ref CallbackReentrancy. + * + * The `INIT` macro sets this to @ref WGPU_UNCAPTURED_ERROR_CALLBACK_INFO_INIT. + */ + WGPUUncapturedErrorCallbackInfo uncapturedErrorCallbackInfo; +} WGPUDeviceDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUDeviceDescriptor. + */ +#define WGPU_DEVICE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUDeviceDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.requiredFeatureCount=*/0 _wgpu_COMMA \ + /*.requiredFeatures=*/NULL _wgpu_COMMA \ + /*.requiredLimits=*/NULL _wgpu_COMMA \ + /*.defaultQueue=*/WGPU_QUEUE_DESCRIPTOR_INIT _wgpu_COMMA \ + /*.deviceLostCallbackInfo=*/WGPU_DEVICE_LOST_CALLBACK_INFO_INIT _wgpu_COMMA \ + /*.uncapturedErrorCallbackInfo=*/WGPU_UNCAPTURED_ERROR_CALLBACK_INFO_INIT _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_PASS_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPURenderPassDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * Array count for `colorAttachments`. The `INIT` macro sets this to 0. + */ + size_t colorAttachmentCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPURenderPassColorAttachment const * colorAttachments; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPURenderPassDepthStencilAttachment const * depthStencilAttachment; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUQuerySet occlusionQuerySet; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUPassTimestampWrites const * timestampWrites; +} WGPURenderPassDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderPassDescriptor. + */ +#define WGPU_RENDER_PASS_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderPassDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.colorAttachmentCount=*/0 _wgpu_COMMA \ + /*.colorAttachments=*/NULL _wgpu_COMMA \ + /*.depthStencilAttachment=*/NULL _wgpu_COMMA \ + /*.occlusionQuerySet=*/NULL _wgpu_COMMA \ + /*.timestampWrites=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPUTextureViewDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. + */ + WGPUTextureFormat format; + /** + * The `INIT` macro sets this to @ref WGPUTextureViewDimension_Undefined. + */ + WGPUTextureViewDimension dimension; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t baseMipLevel; + /** + * The `INIT` macro sets this to @ref WGPU_MIP_LEVEL_COUNT_UNDEFINED. + */ + uint32_t mipLevelCount; + /** + * The `INIT` macro sets this to `0`. + */ + uint32_t baseArrayLayer; + /** + * The `INIT` macro sets this to @ref WGPU_ARRAY_LAYER_COUNT_UNDEFINED. + */ + uint32_t arrayLayerCount; + /** + * If set to @ref WGPUTextureAspect_Undefined, + * [defaults](@ref SentinelValues) to @ref WGPUTextureAspect_All. + * + * The `INIT` macro sets this to @ref WGPUTextureAspect_Undefined. + */ + WGPUTextureAspect aspect; + /** + * The `INIT` macro sets this to @ref WGPUTextureUsage_None. + */ + WGPUTextureUsage usage; +} WGPUTextureViewDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUTextureViewDescriptor. + */ +#define WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureViewDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \ + /*.dimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \ + /*.baseMipLevel=*/0 _wgpu_COMMA \ + /*.mipLevelCount=*/WGPU_MIP_LEVEL_COUNT_UNDEFINED _wgpu_COMMA \ + /*.baseArrayLayer=*/0 _wgpu_COMMA \ + /*.arrayLayerCount=*/WGPU_ARRAY_LAYER_COUNT_UNDEFINED _wgpu_COMMA \ + /*.aspect=*/WGPUTextureAspect_Undefined _wgpu_COMMA \ + /*.usage=*/WGPUTextureUsage_None _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_VERTEX_STATE_INIT as initializer. + */ +typedef struct WGPUVertexState { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUShaderModule module; + /** + * This is a \ref NullableInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView entryPoint; + /** + * Array count for `constants`. The `INIT` macro sets this to 0. + */ + size_t constantCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUConstantEntry const * constants; + /** + * Array count for `buffers`. The `INIT` macro sets this to 0. + */ + size_t bufferCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUVertexBufferLayout const * buffers; +} WGPUVertexState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUVertexState. + */ +#define WGPU_VERTEX_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUVertexState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.module=*/NULL _wgpu_COMMA \ + /*.entryPoint=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.constantCount=*/0 _wgpu_COMMA \ + /*.constants=*/NULL _wgpu_COMMA \ + /*.bufferCount=*/0 _wgpu_COMMA \ + /*.buffers=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_FRAGMENT_STATE_INIT as initializer. + */ +typedef struct WGPUFragmentState { + WGPUChainedStruct * nextInChain; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUShaderModule module; + /** + * This is a \ref NullableInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView entryPoint; + /** + * Array count for `constants`. The `INIT` macro sets this to 0. + */ + size_t constantCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUConstantEntry const * constants; + /** + * Array count for `targets`. The `INIT` macro sets this to 0. + */ + size_t targetCount; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPUColorTargetState const * targets; +} WGPUFragmentState WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPUFragmentState. + */ +#define WGPU_FRAGMENT_STATE_INIT _wgpu_MAKE_INIT_STRUCT(WGPUFragmentState, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.module=*/NULL _wgpu_COMMA \ + /*.entryPoint=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.constantCount=*/0 _wgpu_COMMA \ + /*.constants=*/NULL _wgpu_COMMA \ + /*.targetCount=*/0 _wgpu_COMMA \ + /*.targets=*/NULL _wgpu_COMMA \ +}) + +/** + * Default values can be set using @ref WGPU_RENDER_PIPELINE_DESCRIPTOR_INIT as initializer. + */ +typedef struct WGPURenderPipelineDescriptor { + WGPUChainedStruct * nextInChain; + /** + * This is a \ref NonNullInputString. + * + * The `INIT` macro sets this to @ref WGPU_STRING_VIEW_INIT. + */ + WGPUStringView label; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUPipelineLayout layout; + /** + * The `INIT` macro sets this to @ref WGPU_VERTEX_STATE_INIT. + */ + WGPUVertexState vertex; + /** + * The `INIT` macro sets this to @ref WGPU_PRIMITIVE_STATE_INIT. + */ + WGPUPrimitiveState primitive; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUDepthStencilState const * depthStencil; + /** + * The `INIT` macro sets this to @ref WGPU_MULTISAMPLE_STATE_INIT. + */ + WGPUMultisampleState multisample; + /** + * The `INIT` macro sets this to `NULL`. + */ + WGPU_NULLABLE WGPUFragmentState const * fragment; +} WGPURenderPipelineDescriptor WGPU_STRUCTURE_ATTRIBUTE; + +/** + * Initializer for @ref WGPURenderPipelineDescriptor. + */ +#define WGPU_RENDER_PIPELINE_DESCRIPTOR_INIT _wgpu_MAKE_INIT_STRUCT(WGPURenderPipelineDescriptor, { \ + /*.nextInChain=*/NULL _wgpu_COMMA \ + /*.label=*/WGPU_STRING_VIEW_INIT _wgpu_COMMA \ + /*.layout=*/NULL _wgpu_COMMA \ + /*.vertex=*/WGPU_VERTEX_STATE_INIT _wgpu_COMMA \ + /*.primitive=*/WGPU_PRIMITIVE_STATE_INIT _wgpu_COMMA \ + /*.depthStencil=*/NULL _wgpu_COMMA \ + /*.multisample=*/WGPU_MULTISAMPLE_STATE_INIT _wgpu_COMMA \ + /*.fragment=*/NULL _wgpu_COMMA \ +}) + +/** @} */ + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(WGPU_SKIP_PROCS) +// Global procs +/** + * Proc pointer type for @ref wgpuCreateInstance: + * > @copydoc wgpuCreateInstance + */ +typedef WGPUInstance (*WGPUProcCreateInstance)(WGPU_NULLABLE WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuGetInstanceFeatures: + * > @copydoc wgpuGetInstanceFeatures + */ +typedef void (*WGPUProcGetInstanceFeatures)(WGPUSupportedInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuGetInstanceLimits: + * > @copydoc wgpuGetInstanceLimits + */ +typedef WGPUStatus (*WGPUProcGetInstanceLimits)(WGPUInstanceLimits * limits) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuHasInstanceFeature: + * > @copydoc wgpuHasInstanceFeature + */ +typedef WGPUBool (*WGPUProcHasInstanceFeature)(WGPUInstanceFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuGetProcAddress: + * > @copydoc wgpuGetProcAddress + */ +typedef WGPUProc (*WGPUProcGetProcAddress)(WGPUStringView procName) WGPU_FUNCTION_ATTRIBUTE; + + +// Procs of Adapter +/** + * Proc pointer type for @ref wgpuAdapterGetFeatures: + * > @copydoc wgpuAdapterGetFeatures + */ +typedef void (*WGPUProcAdapterGetFeatures)(WGPUAdapter adapter, WGPUSupportedFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterGetInfo: + * > @copydoc wgpuAdapterGetInfo + */ +typedef WGPUStatus (*WGPUProcAdapterGetInfo)(WGPUAdapter adapter, WGPUAdapterInfo * info) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterGetLimits: + * > @copydoc wgpuAdapterGetLimits + */ +typedef WGPUStatus (*WGPUProcAdapterGetLimits)(WGPUAdapter adapter, WGPULimits * limits) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterHasFeature: + * > @copydoc wgpuAdapterHasFeature + */ +typedef WGPUBool (*WGPUProcAdapterHasFeature)(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterRequestDevice: + * > @copydoc wgpuAdapterRequestDevice + */ +typedef WGPUFuture (*WGPUProcAdapterRequestDevice)(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterAddRef: + * > @copydoc wgpuAdapterAddRef + */ +typedef void (*WGPUProcAdapterAddRef)(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuAdapterRelease: + * > @copydoc wgpuAdapterRelease + */ +typedef void (*WGPUProcAdapterRelease)(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of AdapterInfo +/** + * Proc pointer type for @ref wgpuAdapterInfoFreeMembers: + * > @copydoc wgpuAdapterInfoFreeMembers + */ +typedef void (*WGPUProcAdapterInfoFreeMembers)(WGPUAdapterInfo adapterInfo) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of BindGroup +/** + * Proc pointer type for @ref wgpuBindGroupSetLabel: + * > @copydoc wgpuBindGroupSetLabel + */ +typedef void (*WGPUProcBindGroupSetLabel)(WGPUBindGroup bindGroup, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBindGroupAddRef: + * > @copydoc wgpuBindGroupAddRef + */ +typedef void (*WGPUProcBindGroupAddRef)(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBindGroupRelease: + * > @copydoc wgpuBindGroupRelease + */ +typedef void (*WGPUProcBindGroupRelease)(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of BindGroupLayout +/** + * Proc pointer type for @ref wgpuBindGroupLayoutSetLabel: + * > @copydoc wgpuBindGroupLayoutSetLabel + */ +typedef void (*WGPUProcBindGroupLayoutSetLabel)(WGPUBindGroupLayout bindGroupLayout, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBindGroupLayoutAddRef: + * > @copydoc wgpuBindGroupLayoutAddRef + */ +typedef void (*WGPUProcBindGroupLayoutAddRef)(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBindGroupLayoutRelease: + * > @copydoc wgpuBindGroupLayoutRelease + */ +typedef void (*WGPUProcBindGroupLayoutRelease)(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Buffer +/** + * Proc pointer type for @ref wgpuBufferDestroy: + * > @copydoc wgpuBufferDestroy + */ +typedef void (*WGPUProcBufferDestroy)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferGetConstMappedRange: + * > @copydoc wgpuBufferGetConstMappedRange + */ +typedef void const * (*WGPUProcBufferGetConstMappedRange)(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferGetMappedRange: + * > @copydoc wgpuBufferGetMappedRange + */ +typedef void * (*WGPUProcBufferGetMappedRange)(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferGetMapState: + * > @copydoc wgpuBufferGetMapState + */ +typedef WGPUBufferMapState (*WGPUProcBufferGetMapState)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferGetSize: + * > @copydoc wgpuBufferGetSize + */ +typedef uint64_t (*WGPUProcBufferGetSize)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferGetUsage: + * > @copydoc wgpuBufferGetUsage + */ +typedef WGPUBufferUsage (*WGPUProcBufferGetUsage)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferMapAsync: + * > @copydoc wgpuBufferMapAsync + */ +typedef WGPUFuture (*WGPUProcBufferMapAsync)(WGPUBuffer buffer, WGPUMapMode mode, size_t offset, size_t size, WGPUBufferMapCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferReadMappedRange: + * > @copydoc wgpuBufferReadMappedRange + */ +typedef WGPUStatus (*WGPUProcBufferReadMappedRange)(WGPUBuffer buffer, size_t offset, void * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferSetLabel: + * > @copydoc wgpuBufferSetLabel + */ +typedef void (*WGPUProcBufferSetLabel)(WGPUBuffer buffer, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferUnmap: + * > @copydoc wgpuBufferUnmap + */ +typedef void (*WGPUProcBufferUnmap)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferWriteMappedRange: + * > @copydoc wgpuBufferWriteMappedRange + */ +typedef WGPUStatus (*WGPUProcBufferWriteMappedRange)(WGPUBuffer buffer, size_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferAddRef: + * > @copydoc wgpuBufferAddRef + */ +typedef void (*WGPUProcBufferAddRef)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuBufferRelease: + * > @copydoc wgpuBufferRelease + */ +typedef void (*WGPUProcBufferRelease)(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of CommandBuffer +/** + * Proc pointer type for @ref wgpuCommandBufferSetLabel: + * > @copydoc wgpuCommandBufferSetLabel + */ +typedef void (*WGPUProcCommandBufferSetLabel)(WGPUCommandBuffer commandBuffer, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandBufferAddRef: + * > @copydoc wgpuCommandBufferAddRef + */ +typedef void (*WGPUProcCommandBufferAddRef)(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandBufferRelease: + * > @copydoc wgpuCommandBufferRelease + */ +typedef void (*WGPUProcCommandBufferRelease)(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of CommandEncoder +/** + * Proc pointer type for @ref wgpuCommandEncoderBeginComputePass: + * > @copydoc wgpuCommandEncoderBeginComputePass + */ +typedef WGPUComputePassEncoder (*WGPUProcCommandEncoderBeginComputePass)(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUComputePassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderBeginRenderPass: + * > @copydoc wgpuCommandEncoderBeginRenderPass + */ +typedef WGPURenderPassEncoder (*WGPUProcCommandEncoderBeginRenderPass)(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderClearBuffer: + * > @copydoc wgpuCommandEncoderClearBuffer + */ +typedef void (*WGPUProcCommandEncoderClearBuffer)(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderCopyBufferToBuffer: + * > @copydoc wgpuCommandEncoderCopyBufferToBuffer + */ +typedef void (*WGPUProcCommandEncoderCopyBufferToBuffer)(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderCopyBufferToTexture: + * > @copydoc wgpuCommandEncoderCopyBufferToTexture + */ +typedef void (*WGPUProcCommandEncoderCopyBufferToTexture)(WGPUCommandEncoder commandEncoder, WGPUTexelCopyBufferInfo const * source, WGPUTexelCopyTextureInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderCopyTextureToBuffer: + * > @copydoc wgpuCommandEncoderCopyTextureToBuffer + */ +typedef void (*WGPUProcCommandEncoderCopyTextureToBuffer)(WGPUCommandEncoder commandEncoder, WGPUTexelCopyTextureInfo const * source, WGPUTexelCopyBufferInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderCopyTextureToTexture: + * > @copydoc wgpuCommandEncoderCopyTextureToTexture + */ +typedef void (*WGPUProcCommandEncoderCopyTextureToTexture)(WGPUCommandEncoder commandEncoder, WGPUTexelCopyTextureInfo const * source, WGPUTexelCopyTextureInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderFinish: + * > @copydoc wgpuCommandEncoderFinish + */ +typedef WGPUCommandBuffer (*WGPUProcCommandEncoderFinish)(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUCommandBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderInsertDebugMarker: + * > @copydoc wgpuCommandEncoderInsertDebugMarker + */ +typedef void (*WGPUProcCommandEncoderInsertDebugMarker)(WGPUCommandEncoder commandEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderPopDebugGroup: + * > @copydoc wgpuCommandEncoderPopDebugGroup + */ +typedef void (*WGPUProcCommandEncoderPopDebugGroup)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderPushDebugGroup: + * > @copydoc wgpuCommandEncoderPushDebugGroup + */ +typedef void (*WGPUProcCommandEncoderPushDebugGroup)(WGPUCommandEncoder commandEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderResolveQuerySet: + * > @copydoc wgpuCommandEncoderResolveQuerySet + */ +typedef void (*WGPUProcCommandEncoderResolveQuerySet)(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, WGPUBuffer destination, uint64_t destinationOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderSetLabel: + * > @copydoc wgpuCommandEncoderSetLabel + */ +typedef void (*WGPUProcCommandEncoderSetLabel)(WGPUCommandEncoder commandEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderWriteTimestamp: + * > @copydoc wgpuCommandEncoderWriteTimestamp + */ +typedef void (*WGPUProcCommandEncoderWriteTimestamp)(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderAddRef: + * > @copydoc wgpuCommandEncoderAddRef + */ +typedef void (*WGPUProcCommandEncoderAddRef)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuCommandEncoderRelease: + * > @copydoc wgpuCommandEncoderRelease + */ +typedef void (*WGPUProcCommandEncoderRelease)(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of ComputePassEncoder +/** + * Proc pointer type for @ref wgpuComputePassEncoderDispatchWorkgroups: + * > @copydoc wgpuComputePassEncoderDispatchWorkgroups + */ +typedef void (*WGPUProcComputePassEncoderDispatchWorkgroups)(WGPUComputePassEncoder computePassEncoder, uint32_t workgroupCountX, uint32_t workgroupCountY, uint32_t workgroupCountZ) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderDispatchWorkgroupsIndirect: + * > @copydoc wgpuComputePassEncoderDispatchWorkgroupsIndirect + */ +typedef void (*WGPUProcComputePassEncoderDispatchWorkgroupsIndirect)(WGPUComputePassEncoder computePassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderEnd: + * > @copydoc wgpuComputePassEncoderEnd + */ +typedef void (*WGPUProcComputePassEncoderEnd)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderInsertDebugMarker: + * > @copydoc wgpuComputePassEncoderInsertDebugMarker + */ +typedef void (*WGPUProcComputePassEncoderInsertDebugMarker)(WGPUComputePassEncoder computePassEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderPopDebugGroup: + * > @copydoc wgpuComputePassEncoderPopDebugGroup + */ +typedef void (*WGPUProcComputePassEncoderPopDebugGroup)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderPushDebugGroup: + * > @copydoc wgpuComputePassEncoderPushDebugGroup + */ +typedef void (*WGPUProcComputePassEncoderPushDebugGroup)(WGPUComputePassEncoder computePassEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderSetBindGroup: + * > @copydoc wgpuComputePassEncoderSetBindGroup + */ +typedef void (*WGPUProcComputePassEncoderSetBindGroup)(WGPUComputePassEncoder computePassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderSetImmediates: + * > @copydoc wgpuComputePassEncoderSetImmediates + */ +typedef void (*WGPUProcComputePassEncoderSetImmediates)(WGPUComputePassEncoder computePassEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderSetLabel: + * > @copydoc wgpuComputePassEncoderSetLabel + */ +typedef void (*WGPUProcComputePassEncoderSetLabel)(WGPUComputePassEncoder computePassEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderSetPipeline: + * > @copydoc wgpuComputePassEncoderSetPipeline + */ +typedef void (*WGPUProcComputePassEncoderSetPipeline)(WGPUComputePassEncoder computePassEncoder, WGPUComputePipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderAddRef: + * > @copydoc wgpuComputePassEncoderAddRef + */ +typedef void (*WGPUProcComputePassEncoderAddRef)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePassEncoderRelease: + * > @copydoc wgpuComputePassEncoderRelease + */ +typedef void (*WGPUProcComputePassEncoderRelease)(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of ComputePipeline +/** + * Proc pointer type for @ref wgpuComputePipelineGetBindGroupLayout: + * > @copydoc wgpuComputePipelineGetBindGroupLayout + */ +typedef WGPUBindGroupLayout (*WGPUProcComputePipelineGetBindGroupLayout)(WGPUComputePipeline computePipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePipelineSetLabel: + * > @copydoc wgpuComputePipelineSetLabel + */ +typedef void (*WGPUProcComputePipelineSetLabel)(WGPUComputePipeline computePipeline, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePipelineAddRef: + * > @copydoc wgpuComputePipelineAddRef + */ +typedef void (*WGPUProcComputePipelineAddRef)(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuComputePipelineRelease: + * > @copydoc wgpuComputePipelineRelease + */ +typedef void (*WGPUProcComputePipelineRelease)(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Device +/** + * Proc pointer type for @ref wgpuDeviceCreateBindGroup: + * > @copydoc wgpuDeviceCreateBindGroup + */ +typedef WGPUBindGroup (*WGPUProcDeviceCreateBindGroup)(WGPUDevice device, WGPUBindGroupDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateBindGroupLayout: + * > @copydoc wgpuDeviceCreateBindGroupLayout + */ +typedef WGPUBindGroupLayout (*WGPUProcDeviceCreateBindGroupLayout)(WGPUDevice device, WGPUBindGroupLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateBuffer: + * > @copydoc wgpuDeviceCreateBuffer + */ +typedef WGPU_NULLABLE WGPUBuffer (*WGPUProcDeviceCreateBuffer)(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateCommandEncoder: + * > @copydoc wgpuDeviceCreateCommandEncoder + */ +typedef WGPUCommandEncoder (*WGPUProcDeviceCreateCommandEncoder)(WGPUDevice device, WGPU_NULLABLE WGPUCommandEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateComputePipeline: + * > @copydoc wgpuDeviceCreateComputePipeline + */ +typedef WGPUComputePipeline (*WGPUProcDeviceCreateComputePipeline)(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateComputePipelineAsync: + * > @copydoc wgpuDeviceCreateComputePipelineAsync + */ +typedef WGPUFuture (*WGPUProcDeviceCreateComputePipelineAsync)(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor, WGPUCreateComputePipelineAsyncCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreatePipelineLayout: + * > @copydoc wgpuDeviceCreatePipelineLayout + */ +typedef WGPUPipelineLayout (*WGPUProcDeviceCreatePipelineLayout)(WGPUDevice device, WGPUPipelineLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateQuerySet: + * > @copydoc wgpuDeviceCreateQuerySet + */ +typedef WGPUQuerySet (*WGPUProcDeviceCreateQuerySet)(WGPUDevice device, WGPUQuerySetDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateRenderBundleEncoder: + * > @copydoc wgpuDeviceCreateRenderBundleEncoder + */ +typedef WGPURenderBundleEncoder (*WGPUProcDeviceCreateRenderBundleEncoder)(WGPUDevice device, WGPURenderBundleEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateRenderPipeline: + * > @copydoc wgpuDeviceCreateRenderPipeline + */ +typedef WGPURenderPipeline (*WGPUProcDeviceCreateRenderPipeline)(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateRenderPipelineAsync: + * > @copydoc wgpuDeviceCreateRenderPipelineAsync + */ +typedef WGPUFuture (*WGPUProcDeviceCreateRenderPipelineAsync)(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateSampler: + * > @copydoc wgpuDeviceCreateSampler + */ +typedef WGPUSampler (*WGPUProcDeviceCreateSampler)(WGPUDevice device, WGPU_NULLABLE WGPUSamplerDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateShaderModule: + * > @copydoc wgpuDeviceCreateShaderModule + */ +typedef WGPUShaderModule (*WGPUProcDeviceCreateShaderModule)(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceCreateTexture: + * > @copydoc wgpuDeviceCreateTexture + */ +typedef WGPUTexture (*WGPUProcDeviceCreateTexture)(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceDestroy: + * > @copydoc wgpuDeviceDestroy + */ +typedef void (*WGPUProcDeviceDestroy)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceGetAdapterInfo: + * > @copydoc wgpuDeviceGetAdapterInfo + */ +typedef WGPUStatus (*WGPUProcDeviceGetAdapterInfo)(WGPUDevice device, WGPUAdapterInfo * adapterInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceGetFeatures: + * > @copydoc wgpuDeviceGetFeatures + */ +typedef void (*WGPUProcDeviceGetFeatures)(WGPUDevice device, WGPUSupportedFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceGetLimits: + * > @copydoc wgpuDeviceGetLimits + */ +typedef WGPUStatus (*WGPUProcDeviceGetLimits)(WGPUDevice device, WGPULimits * limits) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceGetLostFuture: + * > @copydoc wgpuDeviceGetLostFuture + */ +typedef WGPUFuture (*WGPUProcDeviceGetLostFuture)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceGetQueue: + * > @copydoc wgpuDeviceGetQueue + */ +typedef WGPUQueue (*WGPUProcDeviceGetQueue)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceHasFeature: + * > @copydoc wgpuDeviceHasFeature + */ +typedef WGPUBool (*WGPUProcDeviceHasFeature)(WGPUDevice device, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDevicePopErrorScope: + * > @copydoc wgpuDevicePopErrorScope + */ +typedef WGPUFuture (*WGPUProcDevicePopErrorScope)(WGPUDevice device, WGPUPopErrorScopeCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDevicePushErrorScope: + * > @copydoc wgpuDevicePushErrorScope + */ +typedef void (*WGPUProcDevicePushErrorScope)(WGPUDevice device, WGPUErrorFilter filter) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceSetLabel: + * > @copydoc wgpuDeviceSetLabel + */ +typedef void (*WGPUProcDeviceSetLabel)(WGPUDevice device, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceAddRef: + * > @copydoc wgpuDeviceAddRef + */ +typedef void (*WGPUProcDeviceAddRef)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuDeviceRelease: + * > @copydoc wgpuDeviceRelease + */ +typedef void (*WGPUProcDeviceRelease)(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of ExternalTexture +/** + * Proc pointer type for @ref wgpuExternalTextureSetLabel: + * > @copydoc wgpuExternalTextureSetLabel + */ +typedef void (*WGPUProcExternalTextureSetLabel)(WGPUExternalTexture externalTexture, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuExternalTextureAddRef: + * > @copydoc wgpuExternalTextureAddRef + */ +typedef void (*WGPUProcExternalTextureAddRef)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuExternalTextureRelease: + * > @copydoc wgpuExternalTextureRelease + */ +typedef void (*WGPUProcExternalTextureRelease)(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Instance +/** + * Proc pointer type for @ref wgpuInstanceCreateSurface: + * > @copydoc wgpuInstanceCreateSurface + */ +typedef WGPUSurface (*WGPUProcInstanceCreateSurface)(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceGetWGSLLanguageFeatures: + * > @copydoc wgpuInstanceGetWGSLLanguageFeatures + */ +typedef void (*WGPUProcInstanceGetWGSLLanguageFeatures)(WGPUInstance instance, WGPUSupportedWGSLLanguageFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceHasWGSLLanguageFeature: + * > @copydoc wgpuInstanceHasWGSLLanguageFeature + */ +typedef WGPUBool (*WGPUProcInstanceHasWGSLLanguageFeature)(WGPUInstance instance, WGPUWGSLLanguageFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceProcessEvents: + * > @copydoc wgpuInstanceProcessEvents + */ +typedef void (*WGPUProcInstanceProcessEvents)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceRequestAdapter: + * > @copydoc wgpuInstanceRequestAdapter + */ +typedef WGPUFuture (*WGPUProcInstanceRequestAdapter)(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const * options, WGPURequestAdapterCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceWaitAny: + * > @copydoc wgpuInstanceWaitAny + */ +typedef WGPUWaitStatus (*WGPUProcInstanceWaitAny)(WGPUInstance instance, size_t futureCount, WGPU_NULLABLE WGPUFutureWaitInfo * futures, uint64_t timeoutNS) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceAddRef: + * > @copydoc wgpuInstanceAddRef + */ +typedef void (*WGPUProcInstanceAddRef)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuInstanceRelease: + * > @copydoc wgpuInstanceRelease + */ +typedef void (*WGPUProcInstanceRelease)(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of PipelineLayout +/** + * Proc pointer type for @ref wgpuPipelineLayoutSetLabel: + * > @copydoc wgpuPipelineLayoutSetLabel + */ +typedef void (*WGPUProcPipelineLayoutSetLabel)(WGPUPipelineLayout pipelineLayout, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuPipelineLayoutAddRef: + * > @copydoc wgpuPipelineLayoutAddRef + */ +typedef void (*WGPUProcPipelineLayoutAddRef)(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuPipelineLayoutRelease: + * > @copydoc wgpuPipelineLayoutRelease + */ +typedef void (*WGPUProcPipelineLayoutRelease)(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of QuerySet +/** + * Proc pointer type for @ref wgpuQuerySetDestroy: + * > @copydoc wgpuQuerySetDestroy + */ +typedef void (*WGPUProcQuerySetDestroy)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQuerySetGetCount: + * > @copydoc wgpuQuerySetGetCount + */ +typedef uint32_t (*WGPUProcQuerySetGetCount)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQuerySetGetType: + * > @copydoc wgpuQuerySetGetType + */ +typedef WGPUQueryType (*WGPUProcQuerySetGetType)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQuerySetSetLabel: + * > @copydoc wgpuQuerySetSetLabel + */ +typedef void (*WGPUProcQuerySetSetLabel)(WGPUQuerySet querySet, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQuerySetAddRef: + * > @copydoc wgpuQuerySetAddRef + */ +typedef void (*WGPUProcQuerySetAddRef)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQuerySetRelease: + * > @copydoc wgpuQuerySetRelease + */ +typedef void (*WGPUProcQuerySetRelease)(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Queue +/** + * Proc pointer type for @ref wgpuQueueOnSubmittedWorkDone: + * > @copydoc wgpuQueueOnSubmittedWorkDone + */ +typedef WGPUFuture (*WGPUProcQueueOnSubmittedWorkDone)(WGPUQueue queue, WGPUQueueWorkDoneCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueSetLabel: + * > @copydoc wgpuQueueSetLabel + */ +typedef void (*WGPUProcQueueSetLabel)(WGPUQueue queue, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueSubmit: + * > @copydoc wgpuQueueSubmit + */ +typedef void (*WGPUProcQueueSubmit)(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueWriteBuffer: + * > @copydoc wgpuQueueWriteBuffer + */ +typedef void (*WGPUProcQueueWriteBuffer)(WGPUQueue queue, WGPUBuffer buffer, uint64_t bufferOffset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueWriteTexture: + * > @copydoc wgpuQueueWriteTexture + */ +typedef void (*WGPUProcQueueWriteTexture)(WGPUQueue queue, WGPUTexelCopyTextureInfo const * destination, void const * data, size_t dataSize, WGPUTexelCopyBufferLayout const * dataLayout, WGPUExtent3D const * writeSize) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueAddRef: + * > @copydoc wgpuQueueAddRef + */ +typedef void (*WGPUProcQueueAddRef)(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuQueueRelease: + * > @copydoc wgpuQueueRelease + */ +typedef void (*WGPUProcQueueRelease)(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of RenderBundle +/** + * Proc pointer type for @ref wgpuRenderBundleSetLabel: + * > @copydoc wgpuRenderBundleSetLabel + */ +typedef void (*WGPUProcRenderBundleSetLabel)(WGPURenderBundle renderBundle, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleAddRef: + * > @copydoc wgpuRenderBundleAddRef + */ +typedef void (*WGPUProcRenderBundleAddRef)(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleRelease: + * > @copydoc wgpuRenderBundleRelease + */ +typedef void (*WGPUProcRenderBundleRelease)(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of RenderBundleEncoder +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderDraw: + * > @copydoc wgpuRenderBundleEncoderDraw + */ +typedef void (*WGPUProcRenderBundleEncoderDraw)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderDrawIndexed: + * > @copydoc wgpuRenderBundleEncoderDrawIndexed + */ +typedef void (*WGPUProcRenderBundleEncoderDrawIndexed)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderDrawIndexedIndirect: + * > @copydoc wgpuRenderBundleEncoderDrawIndexedIndirect + */ +typedef void (*WGPUProcRenderBundleEncoderDrawIndexedIndirect)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderDrawIndirect: + * > @copydoc wgpuRenderBundleEncoderDrawIndirect + */ +typedef void (*WGPUProcRenderBundleEncoderDrawIndirect)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderFinish: + * > @copydoc wgpuRenderBundleEncoderFinish + */ +typedef WGPURenderBundle (*WGPUProcRenderBundleEncoderFinish)(WGPURenderBundleEncoder renderBundleEncoder, WGPU_NULLABLE WGPURenderBundleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderInsertDebugMarker: + * > @copydoc wgpuRenderBundleEncoderInsertDebugMarker + */ +typedef void (*WGPUProcRenderBundleEncoderInsertDebugMarker)(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderPopDebugGroup: + * > @copydoc wgpuRenderBundleEncoderPopDebugGroup + */ +typedef void (*WGPUProcRenderBundleEncoderPopDebugGroup)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderPushDebugGroup: + * > @copydoc wgpuRenderBundleEncoderPushDebugGroup + */ +typedef void (*WGPUProcRenderBundleEncoderPushDebugGroup)(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetBindGroup: + * > @copydoc wgpuRenderBundleEncoderSetBindGroup + */ +typedef void (*WGPUProcRenderBundleEncoderSetBindGroup)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetImmediates: + * > @copydoc wgpuRenderBundleEncoderSetImmediates + */ +typedef void (*WGPUProcRenderBundleEncoderSetImmediates)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetIndexBuffer: + * > @copydoc wgpuRenderBundleEncoderSetIndexBuffer + */ +typedef void (*WGPUProcRenderBundleEncoderSetIndexBuffer)(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetLabel: + * > @copydoc wgpuRenderBundleEncoderSetLabel + */ +typedef void (*WGPUProcRenderBundleEncoderSetLabel)(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetPipeline: + * > @copydoc wgpuRenderBundleEncoderSetPipeline + */ +typedef void (*WGPUProcRenderBundleEncoderSetPipeline)(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderSetVertexBuffer: + * > @copydoc wgpuRenderBundleEncoderSetVertexBuffer + */ +typedef void (*WGPUProcRenderBundleEncoderSetVertexBuffer)(WGPURenderBundleEncoder renderBundleEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderAddRef: + * > @copydoc wgpuRenderBundleEncoderAddRef + */ +typedef void (*WGPUProcRenderBundleEncoderAddRef)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderBundleEncoderRelease: + * > @copydoc wgpuRenderBundleEncoderRelease + */ +typedef void (*WGPUProcRenderBundleEncoderRelease)(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of RenderPassEncoder +/** + * Proc pointer type for @ref wgpuRenderPassEncoderBeginOcclusionQuery: + * > @copydoc wgpuRenderPassEncoderBeginOcclusionQuery + */ +typedef void (*WGPUProcRenderPassEncoderBeginOcclusionQuery)(WGPURenderPassEncoder renderPassEncoder, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderDraw: + * > @copydoc wgpuRenderPassEncoderDraw + */ +typedef void (*WGPUProcRenderPassEncoderDraw)(WGPURenderPassEncoder renderPassEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderDrawIndexed: + * > @copydoc wgpuRenderPassEncoderDrawIndexed + */ +typedef void (*WGPUProcRenderPassEncoderDrawIndexed)(WGPURenderPassEncoder renderPassEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderDrawIndexedIndirect: + * > @copydoc wgpuRenderPassEncoderDrawIndexedIndirect + */ +typedef void (*WGPUProcRenderPassEncoderDrawIndexedIndirect)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderDrawIndirect: + * > @copydoc wgpuRenderPassEncoderDrawIndirect + */ +typedef void (*WGPUProcRenderPassEncoderDrawIndirect)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderEnd: + * > @copydoc wgpuRenderPassEncoderEnd + */ +typedef void (*WGPUProcRenderPassEncoderEnd)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderEndOcclusionQuery: + * > @copydoc wgpuRenderPassEncoderEndOcclusionQuery + */ +typedef void (*WGPUProcRenderPassEncoderEndOcclusionQuery)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderExecuteBundles: + * > @copydoc wgpuRenderPassEncoderExecuteBundles + */ +typedef void (*WGPUProcRenderPassEncoderExecuteBundles)(WGPURenderPassEncoder renderPassEncoder, size_t bundleCount, WGPURenderBundle const * bundles) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderInsertDebugMarker: + * > @copydoc wgpuRenderPassEncoderInsertDebugMarker + */ +typedef void (*WGPUProcRenderPassEncoderInsertDebugMarker)(WGPURenderPassEncoder renderPassEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderPopDebugGroup: + * > @copydoc wgpuRenderPassEncoderPopDebugGroup + */ +typedef void (*WGPUProcRenderPassEncoderPopDebugGroup)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderPushDebugGroup: + * > @copydoc wgpuRenderPassEncoderPushDebugGroup + */ +typedef void (*WGPUProcRenderPassEncoderPushDebugGroup)(WGPURenderPassEncoder renderPassEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetBindGroup: + * > @copydoc wgpuRenderPassEncoderSetBindGroup + */ +typedef void (*WGPUProcRenderPassEncoderSetBindGroup)(WGPURenderPassEncoder renderPassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetBlendConstant: + * > @copydoc wgpuRenderPassEncoderSetBlendConstant + */ +typedef void (*WGPUProcRenderPassEncoderSetBlendConstant)(WGPURenderPassEncoder renderPassEncoder, WGPUColor const * color) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetImmediates: + * > @copydoc wgpuRenderPassEncoderSetImmediates + */ +typedef void (*WGPUProcRenderPassEncoderSetImmediates)(WGPURenderPassEncoder renderPassEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetIndexBuffer: + * > @copydoc wgpuRenderPassEncoderSetIndexBuffer + */ +typedef void (*WGPUProcRenderPassEncoderSetIndexBuffer)(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetLabel: + * > @copydoc wgpuRenderPassEncoderSetLabel + */ +typedef void (*WGPUProcRenderPassEncoderSetLabel)(WGPURenderPassEncoder renderPassEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetPipeline: + * > @copydoc wgpuRenderPassEncoderSetPipeline + */ +typedef void (*WGPUProcRenderPassEncoderSetPipeline)(WGPURenderPassEncoder renderPassEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetScissorRect: + * > @copydoc wgpuRenderPassEncoderSetScissorRect + */ +typedef void (*WGPUProcRenderPassEncoderSetScissorRect)(WGPURenderPassEncoder renderPassEncoder, uint32_t x, uint32_t y, uint32_t width, uint32_t height) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetStencilReference: + * > @copydoc wgpuRenderPassEncoderSetStencilReference + */ +typedef void (*WGPUProcRenderPassEncoderSetStencilReference)(WGPURenderPassEncoder renderPassEncoder, uint32_t reference) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetVertexBuffer: + * > @copydoc wgpuRenderPassEncoderSetVertexBuffer + */ +typedef void (*WGPUProcRenderPassEncoderSetVertexBuffer)(WGPURenderPassEncoder renderPassEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderSetViewport: + * > @copydoc wgpuRenderPassEncoderSetViewport + */ +typedef void (*WGPUProcRenderPassEncoderSetViewport)(WGPURenderPassEncoder renderPassEncoder, float x, float y, float width, float height, float minDepth, float maxDepth) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderAddRef: + * > @copydoc wgpuRenderPassEncoderAddRef + */ +typedef void (*WGPUProcRenderPassEncoderAddRef)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPassEncoderRelease: + * > @copydoc wgpuRenderPassEncoderRelease + */ +typedef void (*WGPUProcRenderPassEncoderRelease)(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of RenderPipeline +/** + * Proc pointer type for @ref wgpuRenderPipelineGetBindGroupLayout: + * > @copydoc wgpuRenderPipelineGetBindGroupLayout + */ +typedef WGPUBindGroupLayout (*WGPUProcRenderPipelineGetBindGroupLayout)(WGPURenderPipeline renderPipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPipelineSetLabel: + * > @copydoc wgpuRenderPipelineSetLabel + */ +typedef void (*WGPUProcRenderPipelineSetLabel)(WGPURenderPipeline renderPipeline, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPipelineAddRef: + * > @copydoc wgpuRenderPipelineAddRef + */ +typedef void (*WGPUProcRenderPipelineAddRef)(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuRenderPipelineRelease: + * > @copydoc wgpuRenderPipelineRelease + */ +typedef void (*WGPUProcRenderPipelineRelease)(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Sampler +/** + * Proc pointer type for @ref wgpuSamplerSetLabel: + * > @copydoc wgpuSamplerSetLabel + */ +typedef void (*WGPUProcSamplerSetLabel)(WGPUSampler sampler, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSamplerAddRef: + * > @copydoc wgpuSamplerAddRef + */ +typedef void (*WGPUProcSamplerAddRef)(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSamplerRelease: + * > @copydoc wgpuSamplerRelease + */ +typedef void (*WGPUProcSamplerRelease)(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of ShaderModule +/** + * Proc pointer type for @ref wgpuShaderModuleGetCompilationInfo: + * > @copydoc wgpuShaderModuleGetCompilationInfo + */ +typedef WGPUFuture (*WGPUProcShaderModuleGetCompilationInfo)(WGPUShaderModule shaderModule, WGPUCompilationInfoCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuShaderModuleSetLabel: + * > @copydoc wgpuShaderModuleSetLabel + */ +typedef void (*WGPUProcShaderModuleSetLabel)(WGPUShaderModule shaderModule, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuShaderModuleAddRef: + * > @copydoc wgpuShaderModuleAddRef + */ +typedef void (*WGPUProcShaderModuleAddRef)(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuShaderModuleRelease: + * > @copydoc wgpuShaderModuleRelease + */ +typedef void (*WGPUProcShaderModuleRelease)(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of SupportedFeatures +/** + * Proc pointer type for @ref wgpuSupportedFeaturesFreeMembers: + * > @copydoc wgpuSupportedFeaturesFreeMembers + */ +typedef void (*WGPUProcSupportedFeaturesFreeMembers)(WGPUSupportedFeatures supportedFeatures) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of SupportedInstanceFeatures +/** + * Proc pointer type for @ref wgpuSupportedInstanceFeaturesFreeMembers: + * > @copydoc wgpuSupportedInstanceFeaturesFreeMembers + */ +typedef void (*WGPUProcSupportedInstanceFeaturesFreeMembers)(WGPUSupportedInstanceFeatures supportedInstanceFeatures) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of SupportedWGSLLanguageFeatures +/** + * Proc pointer type for @ref wgpuSupportedWGSLLanguageFeaturesFreeMembers: + * > @copydoc wgpuSupportedWGSLLanguageFeaturesFreeMembers + */ +typedef void (*WGPUProcSupportedWGSLLanguageFeaturesFreeMembers)(WGPUSupportedWGSLLanguageFeatures supportedWGSLLanguageFeatures) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Surface +/** + * Proc pointer type for @ref wgpuSurfaceConfigure: + * > @copydoc wgpuSurfaceConfigure + */ +typedef void (*WGPUProcSurfaceConfigure)(WGPUSurface surface, WGPUSurfaceConfiguration const * config) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceGetCapabilities: + * > @copydoc wgpuSurfaceGetCapabilities + */ +typedef WGPUStatus (*WGPUProcSurfaceGetCapabilities)(WGPUSurface surface, WGPUAdapter adapter, WGPUSurfaceCapabilities * capabilities) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceGetCurrentTexture: + * > @copydoc wgpuSurfaceGetCurrentTexture + */ +typedef void (*WGPUProcSurfaceGetCurrentTexture)(WGPUSurface surface, WGPUSurfaceTexture * surfaceTexture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfacePresent: + * > @copydoc wgpuSurfacePresent + */ +typedef WGPUStatus (*WGPUProcSurfacePresent)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceSetLabel: + * > @copydoc wgpuSurfaceSetLabel + */ +typedef void (*WGPUProcSurfaceSetLabel)(WGPUSurface surface, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceUnconfigure: + * > @copydoc wgpuSurfaceUnconfigure + */ +typedef void (*WGPUProcSurfaceUnconfigure)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceAddRef: + * > @copydoc wgpuSurfaceAddRef + */ +typedef void (*WGPUProcSurfaceAddRef)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuSurfaceRelease: + * > @copydoc wgpuSurfaceRelease + */ +typedef void (*WGPUProcSurfaceRelease)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of SurfaceCapabilities +/** + * Proc pointer type for @ref wgpuSurfaceCapabilitiesFreeMembers: + * > @copydoc wgpuSurfaceCapabilitiesFreeMembers + */ +typedef void (*WGPUProcSurfaceCapabilitiesFreeMembers)(WGPUSurfaceCapabilities surfaceCapabilities) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of Texture +/** + * Proc pointer type for @ref wgpuTextureCreateView: + * > @copydoc wgpuTextureCreateView + */ +typedef WGPUTextureView (*WGPUProcTextureCreateView)(WGPUTexture texture, WGPU_NULLABLE WGPUTextureViewDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureDestroy: + * > @copydoc wgpuTextureDestroy + */ +typedef void (*WGPUProcTextureDestroy)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetDepthOrArrayLayers: + * > @copydoc wgpuTextureGetDepthOrArrayLayers + */ +typedef uint32_t (*WGPUProcTextureGetDepthOrArrayLayers)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetDimension: + * > @copydoc wgpuTextureGetDimension + */ +typedef WGPUTextureDimension (*WGPUProcTextureGetDimension)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetFormat: + * > @copydoc wgpuTextureGetFormat + */ +typedef WGPUTextureFormat (*WGPUProcTextureGetFormat)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetHeight: + * > @copydoc wgpuTextureGetHeight + */ +typedef uint32_t (*WGPUProcTextureGetHeight)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetMipLevelCount: + * > @copydoc wgpuTextureGetMipLevelCount + */ +typedef uint32_t (*WGPUProcTextureGetMipLevelCount)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetSampleCount: + * > @copydoc wgpuTextureGetSampleCount + */ +typedef uint32_t (*WGPUProcTextureGetSampleCount)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetTextureBindingViewDimension: + * > @copydoc wgpuTextureGetTextureBindingViewDimension + */ +typedef WGPUTextureViewDimension (*WGPUProcTextureGetTextureBindingViewDimension)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetUsage: + * > @copydoc wgpuTextureGetUsage + */ +typedef WGPUTextureUsage (*WGPUProcTextureGetUsage)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureGetWidth: + * > @copydoc wgpuTextureGetWidth + */ +typedef uint32_t (*WGPUProcTextureGetWidth)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureSetLabel: + * > @copydoc wgpuTextureSetLabel + */ +typedef void (*WGPUProcTextureSetLabel)(WGPUTexture texture, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureAddRef: + * > @copydoc wgpuTextureAddRef + */ +typedef void (*WGPUProcTextureAddRef)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureRelease: + * > @copydoc wgpuTextureRelease + */ +typedef void (*WGPUProcTextureRelease)(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; + +// Procs of TextureView +/** + * Proc pointer type for @ref wgpuTextureViewSetLabel: + * > @copydoc wgpuTextureViewSetLabel + */ +typedef void (*WGPUProcTextureViewSetLabel)(WGPUTextureView textureView, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureViewAddRef: + * > @copydoc wgpuTextureViewAddRef + */ +typedef void (*WGPUProcTextureViewAddRef)(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; +/** + * Proc pointer type for @ref wgpuTextureViewRelease: + * > @copydoc wgpuTextureViewRelease + */ +typedef void (*WGPUProcTextureViewRelease)(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; + +#endif // !defined(WGPU_SKIP_PROCS) + +#if !defined(WGPU_SKIP_DECLARATIONS) +/** + * \defgroup GlobalFunctions Global Functions + * \brief Functions that are not specific to an object. + * + * @{ + */ +/** + * Create a WGPUInstance + * + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPU_NULLABLE WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Get the list of @ref WGPUInstanceFeatureName values supported by the instance. + * + * @param features + * This parameter is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT void wgpuGetInstanceFeatures(WGPUSupportedInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * Get the limits supported by the instance. + * + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuGetInstanceLimits(WGPUInstanceLimits * limits) WGPU_FUNCTION_ATTRIBUTE; +/** + * Check whether a particular @ref WGPUInstanceFeatureName is supported by the instance. + */ +WGPU_EXPORT WGPUBool wgpuHasInstanceFeature(WGPUInstanceFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Returns the "procedure address" (function pointer) of the named function. + * The result must be cast to the appropriate proc pointer type. + */ +WGPU_EXPORT WGPUProc wgpuGetProcAddress(WGPUStringView procName) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup Methods Methods + * \brief Functions that are relative to a specific object. + * + * @{ + */ + +/** + * \defgroup WGPUAdapterMethods WGPUAdapter methods + * \brief Functions whose first argument has type WGPUAdapter. + * + * @{ + */ +/** + * Get the list of @ref WGPUFeatureName values supported by the adapter. + * + * @param features + * This parameter is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT void wgpuAdapterGetFeatures(WGPUAdapter adapter, WGPUSupportedFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * @param info + * This parameter is @ref ReturnedWithOwnership. + * + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuAdapterGetInfo(WGPUAdapter adapter, WGPUAdapterInfo * info) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuAdapterGetLimits(WGPUAdapter adapter, WGPULimits * limits) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUBool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUFuture wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPU_NULLABLE WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuAdapterAddRef(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuAdapterRelease(WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUAdapterInfoMethods WGPUAdapterInfo methods + * \brief Functions whose first argument has type WGPUAdapterInfo. + * + * @{ + */ +/** + * Frees members which were allocated by the API. + */ +WGPU_EXPORT void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUBindGroupMethods WGPUBindGroup methods + * \brief Functions whose first argument has type WGPUBindGroup. + * + * @{ + */ +WGPU_EXPORT void wgpuBindGroupSetLabel(WGPUBindGroup bindGroup, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBindGroupAddRef(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBindGroupRelease(WGPUBindGroup bindGroup) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUBindGroupLayoutMethods WGPUBindGroupLayout methods + * \brief Functions whose first argument has type WGPUBindGroupLayout. + * + * @{ + */ +WGPU_EXPORT void wgpuBindGroupLayoutSetLabel(WGPUBindGroupLayout bindGroupLayout, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBindGroupLayoutAddRef(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBindGroupLayoutRelease(WGPUBindGroupLayout bindGroupLayout) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUBufferMethods WGPUBuffer methods + * \brief Functions whose first argument has type WGPUBuffer. + * + * @{ + */ +WGPU_EXPORT void wgpuBufferDestroy(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Returns a const pointer to beginning of the mapped range. + * It must not be written; writing to this range causes undefined behavior. + * See @ref MappedRangeBehavior for error conditions and guarantees. + * This function is safe to call inside spontaneous callbacks (see @ref CallbackReentrancy). + * + * In Wasm, if `memcpy`ing from this range, prefer using @ref wgpuBufferReadMappedRange + * instead for better performance. + * + * @param offset + * Byte offset relative to the beginning of the buffer. + * + * @param size + * Byte size of the range to get. + * If this is @ref WGPU_WHOLE_MAP_SIZE, it defaults to `buffer.size - offset`. + * The returned pointer is valid for exactly this many bytes. + */ +WGPU_EXPORT void const * wgpuBufferGetConstMappedRange(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * Returns a mutable pointer to beginning of the mapped range. + * See @ref MappedRangeBehavior for error conditions and guarantees. + * This function is safe to call inside spontaneous callbacks (see @ref CallbackReentrancy). + * + * In Wasm, if `memcpy`ing into this range, prefer using @ref wgpuBufferWriteMappedRange + * instead for better performance. + * + * @param offset + * Byte offset relative to the beginning of the buffer. + * + * @param size + * Byte size of the range to get. + * If this is @ref WGPU_WHOLE_MAP_SIZE, it defaults to `buffer.size - offset`. + * The returned pointer is valid for exactly this many bytes. + */ +WGPU_EXPORT void * wgpuBufferGetMappedRange(WGPUBuffer buffer, size_t offset, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUBufferMapState wgpuBufferGetMapState(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint64_t wgpuBufferGetSize(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUBufferUsage wgpuBufferGetUsage(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * @param mode + * The mapping mode (read or write). + * + * @param offset + * Byte offset relative to beginning of the buffer. + * + * @param size + * Byte size of the region to map. + * If this is @ref WGPU_WHOLE_MAP_SIZE, it defaults to `buffer.size - offset`. + */ +WGPU_EXPORT WGPUFuture wgpuBufferMapAsync(WGPUBuffer buffer, WGPUMapMode mode, size_t offset, size_t size, WGPUBufferMapCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Copies a range of data from the buffer mapping into the provided destination pointer. + * See @ref MappedRangeBehavior for error conditions and guarantees. + * This function is safe to call inside spontaneous callbacks (see @ref CallbackReentrancy). + * + * In Wasm, this is more efficient than copying from a mapped range into a `malloc`'d range. + * + * @param offset + * Byte offset relative to the beginning of the buffer. + * + * @param data + * Destination, to read buffer data into. + * + * @param size + * Number of bytes of data to read from the buffer. + * (Note @ref WGPU_WHOLE_MAP_SIZE is *not* accepted here.) + * + * @returns + * @ref WGPUStatus_Error if the copy did not occur. + */ +WGPU_EXPORT WGPUStatus wgpuBufferReadMappedRange(WGPUBuffer buffer, size_t offset, void * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBufferSetLabel(WGPUBuffer buffer, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBufferUnmap(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +/** + * Copies a range of data from the provided source pointer into the buffer mapping. + * See @ref MappedRangeBehavior for error conditions and guarantees. + * This function is safe to call inside spontaneous callbacks (see @ref CallbackReentrancy). + * + * In Wasm, this is more efficient than copying from a `malloc`'d range into a mapped range. + * + * @param offset + * Byte offset relative to the beginning of the buffer. + * + * @param data + * Source, to write buffer data from. + * + * @param size + * Number of bytes of data to write to the buffer. + * (Note @ref WGPU_WHOLE_MAP_SIZE is *not* accepted here.) + * + * @returns + * @ref WGPUStatus_Error if the copy did not occur. + */ +WGPU_EXPORT WGPUStatus wgpuBufferWriteMappedRange(WGPUBuffer buffer, size_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBufferAddRef(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuBufferRelease(WGPUBuffer buffer) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUCommandBufferMethods WGPUCommandBuffer methods + * \brief Functions whose first argument has type WGPUCommandBuffer. + * + * @{ + */ +WGPU_EXPORT void wgpuCommandBufferSetLabel(WGPUCommandBuffer commandBuffer, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandBufferAddRef(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandBufferRelease(WGPUCommandBuffer commandBuffer) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUCommandEncoderMethods WGPUCommandEncoder methods + * \brief Functions whose first argument has type WGPUCommandEncoder. + * + * @{ + */ +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUComputePassEncoder wgpuCommandEncoderBeginComputePass(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUComputePassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPURenderPassEncoder wgpuCommandEncoderBeginRenderPass(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderClearBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderCopyBufferToBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderCopyBufferToTexture(WGPUCommandEncoder commandEncoder, WGPUTexelCopyBufferInfo const * source, WGPUTexelCopyTextureInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderCopyTextureToBuffer(WGPUCommandEncoder commandEncoder, WGPUTexelCopyTextureInfo const * source, WGPUTexelCopyBufferInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderCopyTextureToTexture(WGPUCommandEncoder commandEncoder, WGPUTexelCopyTextureInfo const * source, WGPUTexelCopyTextureInfo const * destination, WGPUExtent3D const * copySize) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUCommandBuffer wgpuCommandEncoderFinish(WGPUCommandEncoder commandEncoder, WGPU_NULLABLE WGPUCommandBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderInsertDebugMarker(WGPUCommandEncoder commandEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderPopDebugGroup(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderPushDebugGroup(WGPUCommandEncoder commandEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderResolveQuerySet(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount, WGPUBuffer destination, uint64_t destinationOffset) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderSetLabel(WGPUCommandEncoder commandEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderWriteTimestamp(WGPUCommandEncoder commandEncoder, WGPUQuerySet querySet, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderAddRef(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuCommandEncoderRelease(WGPUCommandEncoder commandEncoder) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUComputePassEncoderMethods WGPUComputePassEncoder methods + * \brief Functions whose first argument has type WGPUComputePassEncoder. + * + * @{ + */ +WGPU_EXPORT void wgpuComputePassEncoderDispatchWorkgroups(WGPUComputePassEncoder computePassEncoder, uint32_t workgroupCountX, uint32_t workgroupCountY, uint32_t workgroupCountZ) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderDispatchWorkgroupsIndirect(WGPUComputePassEncoder computePassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderEnd(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderInsertDebugMarker(WGPUComputePassEncoder computePassEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderPopDebugGroup(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderPushDebugGroup(WGPUComputePassEncoder computePassEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderSetBindGroup(WGPUComputePassEncoder computePassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderSetImmediates(WGPUComputePassEncoder computePassEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderSetLabel(WGPUComputePassEncoder computePassEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderSetPipeline(WGPUComputePassEncoder computePassEncoder, WGPUComputePipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderAddRef(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePassEncoderRelease(WGPUComputePassEncoder computePassEncoder) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUComputePipelineMethods WGPUComputePipeline methods + * \brief Functions whose first argument has type WGPUComputePipeline. + * + * @{ + */ +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUBindGroupLayout wgpuComputePipelineGetBindGroupLayout(WGPUComputePipeline computePipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePipelineSetLabel(WGPUComputePipeline computePipeline, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePipelineAddRef(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuComputePipelineRelease(WGPUComputePipeline computePipeline) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUDeviceMethods WGPUDevice methods + * \brief Functions whose first argument has type WGPUDevice. + * + * @{ + */ +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUBindGroup wgpuDeviceCreateBindGroup(WGPUDevice device, WGPUBindGroupDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUBindGroupLayout wgpuDeviceCreateBindGroupLayout(WGPUDevice device, WGPUBindGroupLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * TODO + * + * If @ref WGPUBufferDescriptor::mappedAtCreation is `true` and the mapping allocation fails, + * returns `NULL`. + * + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPU_NULLABLE WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device, WGPUBufferDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUCommandEncoder wgpuDeviceCreateCommandEncoder(WGPUDevice device, WGPU_NULLABLE WGPUCommandEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUComputePipeline wgpuDeviceCreateComputePipeline(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUFuture wgpuDeviceCreateComputePipelineAsync(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor, WGPUCreateComputePipelineAsyncCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUPipelineLayout wgpuDeviceCreatePipelineLayout(WGPUDevice device, WGPUPipelineLayoutDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUQuerySet wgpuDeviceCreateQuerySet(WGPUDevice device, WGPUQuerySetDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPURenderBundleEncoder wgpuDeviceCreateRenderBundleEncoder(WGPUDevice device, WGPURenderBundleEncoderDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPURenderPipeline wgpuDeviceCreateRenderPipeline(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUFuture wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor, WGPUCreateRenderPipelineAsyncCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUSampler wgpuDeviceCreateSampler(WGPUDevice device, WGPU_NULLABLE WGPUSamplerDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUShaderModule wgpuDeviceCreateShaderModule(WGPUDevice device, WGPUShaderModuleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUTexture wgpuDeviceCreateTexture(WGPUDevice device, WGPUTextureDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuDeviceDestroy(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * @param adapterInfo + * This parameter is @ref ReturnedWithOwnership. + * + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuDeviceGetAdapterInfo(WGPUDevice device, WGPUAdapterInfo * adapterInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Get the list of @ref WGPUFeatureName values supported by the device. + * + * @param features + * This parameter is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT void wgpuDeviceGetFeatures(WGPUDevice device, WGPUSupportedFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuDeviceGetLimits(WGPUDevice device, WGPULimits * limits) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * The @ref WGPUFuture for the device-lost event of the device. + */ +WGPU_EXPORT WGPUFuture wgpuDeviceGetLostFuture(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUQueue wgpuDeviceGetQueue(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUBool wgpuDeviceHasFeature(WGPUDevice device, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Pops an error scope to the current thread's error scope stack, + * asynchronously returning the result. See @ref ErrorScopes. + */ +WGPU_EXPORT WGPUFuture wgpuDevicePopErrorScope(WGPUDevice device, WGPUPopErrorScopeCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Pushes an error scope to the current thread's error scope stack. + * See @ref ErrorScopes. + */ +WGPU_EXPORT void wgpuDevicePushErrorScope(WGPUDevice device, WGPUErrorFilter filter) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuDeviceSetLabel(WGPUDevice device, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuDeviceAddRef(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuDeviceRelease(WGPUDevice device) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUExternalTextureMethods WGPUExternalTexture methods + * \brief Functions whose first argument has type WGPUExternalTexture. + * + * @{ + */ +WGPU_EXPORT void wgpuExternalTextureSetLabel(WGPUExternalTexture externalTexture, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuExternalTextureAddRef(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuExternalTextureRelease(WGPUExternalTexture externalTexture) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUInstanceMethods WGPUInstance methods + * \brief Functions whose first argument has type WGPUInstance. + * + * @{ + */ +/** + * Creates a @ref WGPUSurface, see @ref Surface-Creation for more details. + * + * @param descriptor + * The description of the @ref WGPUSurface to create. + * + * @returns + * A new @ref WGPUSurface for this descriptor (or an error @ref WGPUSurface). + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUSurface wgpuInstanceCreateSurface(WGPUInstance instance, WGPUSurfaceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +/** + * Get the list of @ref WGPUWGSLLanguageFeatureName values supported by the instance. + */ +WGPU_EXPORT void wgpuInstanceGetWGSLLanguageFeatures(WGPUInstance instance, WGPUSupportedWGSLLanguageFeatures * features) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUBool wgpuInstanceHasWGSLLanguageFeature(WGPUInstance instance, WGPUWGSLLanguageFeatureName feature) WGPU_FUNCTION_ATTRIBUTE; +/** + * Processes asynchronous events on this `WGPUInstance`, calling any callbacks for asynchronous operations created with @ref WGPUCallbackMode_AllowProcessEvents. + * + * See @ref Process-Events for more information. + */ +WGPU_EXPORT void wgpuInstanceProcessEvents(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUFuture wgpuInstanceRequestAdapter(WGPUInstance instance, WGPU_NULLABLE WGPURequestAdapterOptions const * options, WGPURequestAdapterCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +/** + * Wait for at least one WGPUFuture in `futures` to complete, and call callbacks of the respective completed asynchronous operations. + * + * See @ref Wait-Any for more information. + */ +WGPU_EXPORT WGPUWaitStatus wgpuInstanceWaitAny(WGPUInstance instance, size_t futureCount, WGPU_NULLABLE WGPUFutureWaitInfo * futures, uint64_t timeoutNS) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuInstanceAddRef(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuInstanceRelease(WGPUInstance instance) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUPipelineLayoutMethods WGPUPipelineLayout methods + * \brief Functions whose first argument has type WGPUPipelineLayout. + * + * @{ + */ +WGPU_EXPORT void wgpuPipelineLayoutSetLabel(WGPUPipelineLayout pipelineLayout, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuPipelineLayoutAddRef(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuPipelineLayoutRelease(WGPUPipelineLayout pipelineLayout) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUQuerySetMethods WGPUQuerySet methods + * \brief Functions whose first argument has type WGPUQuerySet. + * + * @{ + */ +WGPU_EXPORT void wgpuQuerySetDestroy(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuQuerySetGetCount(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUQueryType wgpuQuerySetGetType(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQuerySetSetLabel(WGPUQuerySet querySet, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQuerySetAddRef(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQuerySetRelease(WGPUQuerySet querySet) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUQueueMethods WGPUQueue methods + * \brief Functions whose first argument has type WGPUQueue. + * + * @{ + */ +WGPU_EXPORT WGPUFuture wgpuQueueOnSubmittedWorkDone(WGPUQueue queue, WGPUQueueWorkDoneCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQueueSetLabel(WGPUQueue queue, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQueueSubmit(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands) WGPU_FUNCTION_ATTRIBUTE; +/** + * Produces a @ref DeviceError both content-timeline (`size` alignment) and device-timeline + * errors defined by the WebGPU specification. + */ +WGPU_EXPORT void wgpuQueueWriteBuffer(WGPUQueue queue, WGPUBuffer buffer, uint64_t bufferOffset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQueueWriteTexture(WGPUQueue queue, WGPUTexelCopyTextureInfo const * destination, void const * data, size_t dataSize, WGPUTexelCopyBufferLayout const * dataLayout, WGPUExtent3D const * writeSize) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQueueAddRef(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuQueueRelease(WGPUQueue queue) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPURenderBundleMethods WGPURenderBundle methods + * \brief Functions whose first argument has type WGPURenderBundle. + * + * @{ + */ +WGPU_EXPORT void wgpuRenderBundleSetLabel(WGPURenderBundle renderBundle, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleAddRef(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleRelease(WGPURenderBundle renderBundle) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPURenderBundleEncoderMethods WGPURenderBundleEncoder methods + * \brief Functions whose first argument has type WGPURenderBundleEncoder. + * + * @{ + */ +WGPU_EXPORT void wgpuRenderBundleEncoderDraw(WGPURenderBundleEncoder renderBundleEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndexed(WGPURenderBundleEncoder renderBundleEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndexedIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderDrawIndirect(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPURenderBundle wgpuRenderBundleEncoderFinish(WGPURenderBundleEncoder renderBundleEncoder, WGPU_NULLABLE WGPURenderBundleDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderInsertDebugMarker(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderPopDebugGroup(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderPushDebugGroup(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetBindGroup(WGPURenderBundleEncoder renderBundleEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetImmediates(WGPURenderBundleEncoder renderBundleEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetIndexBuffer(WGPURenderBundleEncoder renderBundleEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetLabel(WGPURenderBundleEncoder renderBundleEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetPipeline(WGPURenderBundleEncoder renderBundleEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderSetVertexBuffer(WGPURenderBundleEncoder renderBundleEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderAddRef(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderBundleEncoderRelease(WGPURenderBundleEncoder renderBundleEncoder) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPURenderPassEncoderMethods WGPURenderPassEncoder methods + * \brief Functions whose first argument has type WGPURenderPassEncoder. + * + * @{ + */ +WGPU_EXPORT void wgpuRenderPassEncoderBeginOcclusionQuery(WGPURenderPassEncoder renderPassEncoder, uint32_t queryIndex) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderDraw(WGPURenderPassEncoder renderPassEncoder, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderDrawIndexed(WGPURenderPassEncoder renderPassEncoder, uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t baseVertex, uint32_t firstInstance) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderDrawIndexedIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderDrawIndirect(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer indirectBuffer, uint64_t indirectOffset) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderEnd(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderEndOcclusionQuery(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderExecuteBundles(WGPURenderPassEncoder renderPassEncoder, size_t bundleCount, WGPURenderBundle const * bundles) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderInsertDebugMarker(WGPURenderPassEncoder renderPassEncoder, WGPUStringView markerLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderPopDebugGroup(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderPushDebugGroup(WGPURenderPassEncoder renderPassEncoder, WGPUStringView groupLabel) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetBindGroup(WGPURenderPassEncoder renderPassEncoder, uint32_t groupIndex, WGPU_NULLABLE WGPUBindGroup group, size_t dynamicOffsetCount, uint32_t const * dynamicOffsets) WGPU_FUNCTION_ATTRIBUTE; +/** + * @param color + * The RGBA blend constant. Represents an `f32` color using @ref DoubleAsSupertype. + */ +WGPU_EXPORT void wgpuRenderPassEncoderSetBlendConstant(WGPURenderPassEncoder renderPassEncoder, WGPUColor const * color) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetImmediates(WGPURenderPassEncoder renderPassEncoder, uint32_t offset, void const * data, size_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetIndexBuffer(WGPURenderPassEncoder renderPassEncoder, WGPUBuffer buffer, WGPUIndexFormat format, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetLabel(WGPURenderPassEncoder renderPassEncoder, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetPipeline(WGPURenderPassEncoder renderPassEncoder, WGPURenderPipeline pipeline) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetScissorRect(WGPURenderPassEncoder renderPassEncoder, uint32_t x, uint32_t y, uint32_t width, uint32_t height) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetStencilReference(WGPURenderPassEncoder renderPassEncoder, uint32_t reference) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderSetVertexBuffer(WGPURenderPassEncoder renderPassEncoder, uint32_t slot, WGPU_NULLABLE WGPUBuffer buffer, uint64_t offset, uint64_t size) WGPU_FUNCTION_ATTRIBUTE; +/** + * TODO + * + * If any argument is non-finite, produces a @ref NonFiniteFloatValueError. + */ +WGPU_EXPORT void wgpuRenderPassEncoderSetViewport(WGPURenderPassEncoder renderPassEncoder, float x, float y, float width, float height, float minDepth, float maxDepth) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderAddRef(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPassEncoderRelease(WGPURenderPassEncoder renderPassEncoder) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPURenderPipelineMethods WGPURenderPipeline methods + * \brief Functions whose first argument has type WGPURenderPipeline. + * + * @{ + */ +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUBindGroupLayout wgpuRenderPipelineGetBindGroupLayout(WGPURenderPipeline renderPipeline, uint32_t groupIndex) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPipelineSetLabel(WGPURenderPipeline renderPipeline, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPipelineAddRef(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuRenderPipelineRelease(WGPURenderPipeline renderPipeline) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSamplerMethods WGPUSampler methods + * \brief Functions whose first argument has type WGPUSampler. + * + * @{ + */ +WGPU_EXPORT void wgpuSamplerSetLabel(WGPUSampler sampler, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuSamplerAddRef(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuSamplerRelease(WGPUSampler sampler) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUShaderModuleMethods WGPUShaderModule methods + * \brief Functions whose first argument has type WGPUShaderModule. + * + * @{ + */ +WGPU_EXPORT WGPUFuture wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, WGPUCompilationInfoCallbackInfo callbackInfo) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuShaderModuleAddRef(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuShaderModuleRelease(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSupportedFeaturesMethods WGPUSupportedFeatures methods + * \brief Functions whose first argument has type WGPUSupportedFeatures. + * + * @{ + */ +/** + * Frees members which were allocated by the API. + */ +WGPU_EXPORT void wgpuSupportedFeaturesFreeMembers(WGPUSupportedFeatures supportedFeatures) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSupportedInstanceFeaturesMethods WGPUSupportedInstanceFeatures methods + * \brief Functions whose first argument has type WGPUSupportedInstanceFeatures. + * + * @{ + */ +/** + * Frees members which were allocated by the API. + */ +WGPU_EXPORT void wgpuSupportedInstanceFeaturesFreeMembers(WGPUSupportedInstanceFeatures supportedInstanceFeatures) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSupportedWGSLLanguageFeaturesMethods WGPUSupportedWGSLLanguageFeatures methods + * \brief Functions whose first argument has type WGPUSupportedWGSLLanguageFeatures. + * + * @{ + */ +/** + * Frees members which were allocated by the API. + */ +WGPU_EXPORT void wgpuSupportedWGSLLanguageFeaturesFreeMembers(WGPUSupportedWGSLLanguageFeatures supportedWGSLLanguageFeatures) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSurfaceMethods WGPUSurface methods + * \brief Functions whose first argument has type WGPUSurface. + * + * @{ + */ +/** + * Configures parameters for rendering to `surface`. + * Produces a @ref DeviceError for all content-timeline errors defined by the WebGPU specification. + * + * See @ref Surface-Configuration for more details. + * + * @param config + * The new configuration to use. + */ +WGPU_EXPORT void wgpuSurfaceConfigure(WGPUSurface surface, WGPUSurfaceConfiguration const * config) WGPU_FUNCTION_ATTRIBUTE; +/** + * Provides information on how `adapter` is able to use `surface`. + * See @ref Surface-Capabilities for more details. + * + * @param adapter + * The @ref WGPUAdapter to get capabilities for presenting to this @ref WGPUSurface. + * + * @param capabilities + * The structure to fill capabilities in. + * It may contain memory allocations so @ref wgpuSurfaceCapabilitiesFreeMembers must be called to avoid memory leaks. + * This parameter is @ref ReturnedWithOwnership. + * + * @returns + * Indicates if there was an @ref OutStructChainError. + */ +WGPU_EXPORT WGPUStatus wgpuSurfaceGetCapabilities(WGPUSurface surface, WGPUAdapter adapter, WGPUSurfaceCapabilities * capabilities) WGPU_FUNCTION_ATTRIBUTE; +/** + * Returns the @ref WGPUTexture to render to `surface` this frame along with metadata on the frame. + * Returns `NULL` and @ref WGPUSurfaceGetCurrentTextureStatus_Error if the surface is not configured. + * + * See @ref Surface-Presenting for more details. + * + * @param surfaceTexture + * The structure to fill the @ref WGPUTexture and metadata in. + */ +WGPU_EXPORT void wgpuSurfaceGetCurrentTexture(WGPUSurface surface, WGPUSurfaceTexture * surfaceTexture) WGPU_FUNCTION_ATTRIBUTE; +/** + * Shows `surface`'s current texture to the user. + * See @ref Surface-Presenting for more details. + * + * @returns + * Returns @ref WGPUStatus_Error if the surface doesn't have a current texture. + */ +WGPU_EXPORT WGPUStatus wgpuSurfacePresent(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +/** + * Modifies the label used to refer to `surface`. + * + * @param label + * The new label. + */ +WGPU_EXPORT void wgpuSurfaceSetLabel(WGPUSurface surface, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +/** + * Removes the configuration for `surface`. + * See @ref Surface-Configuration for more details. + */ +WGPU_EXPORT void wgpuSurfaceUnconfigure(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuSurfaceAddRef(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuSurfaceRelease(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUSurfaceCapabilitiesMethods WGPUSurfaceCapabilities methods + * \brief Functions whose first argument has type WGPUSurfaceCapabilities. + * + * @{ + */ +/** + * Frees members which were allocated by the API. + */ +WGPU_EXPORT void wgpuSurfaceCapabilitiesFreeMembers(WGPUSurfaceCapabilities surfaceCapabilities) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUTextureMethods WGPUTexture methods + * \brief Functions whose first argument has type WGPUTexture. + * + * @{ + */ +/** + * @returns + * This value is @ref ReturnedWithOwnership. + */ +WGPU_EXPORT WGPUTextureView wgpuTextureCreateView(WGPUTexture texture, WGPU_NULLABLE WGPUTextureViewDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureDestroy(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuTextureGetDepthOrArrayLayers(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUTextureDimension wgpuTextureGetDimension(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUTextureFormat wgpuTextureGetFormat(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuTextureGetHeight(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuTextureGetMipLevelCount(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuTextureGetSampleCount(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUTextureViewDimension wgpuTextureGetTextureBindingViewDimension(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT WGPUTextureUsage wgpuTextureGetUsage(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT uint32_t wgpuTextureGetWidth(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureSetLabel(WGPUTexture texture, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureAddRef(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureRelease(WGPUTexture texture) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** + * \defgroup WGPUTextureViewMethods WGPUTextureView methods + * \brief Functions whose first argument has type WGPUTextureView. + * + * @{ + */ +WGPU_EXPORT void wgpuTextureViewSetLabel(WGPUTextureView textureView, WGPUStringView label) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureViewAddRef(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXPORT void wgpuTextureViewRelease(WGPUTextureView textureView) WGPU_FUNCTION_ATTRIBUTE; + +/** @} */ + +/** @} */ + +#endif // !defined(WGPU_SKIP_DECLARATIONS) + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // WEBGPU_H_ diff --git a/src/render/gpu/SDL_shaders_gpu.c b/src/render/gpu/SDL_shaders_gpu.c index 8e9caf813b..6bd19358e4 100644 --- a/src/render/gpu/SDL_shaders_gpu.c +++ b/src/render/gpu/SDL_shaders_gpu.c @@ -69,12 +69,22 @@ typedef struct GPU_ShaderModuleSource #define HAVE_METAL_SHADERS 0 #endif +#ifdef SDL_GPU_WEBGPU +#define IF_WEBGPU(...) __VA_ARGS__ +#define HAVE_WGSL_SHADERS 1 +#include "shaders/wgsl.h" +#else +#define IF_WEBGPU(...) +#define HAVE_WGSL_SHADERS 0 +#endif + typedef struct GPU_ShaderSources { IF_PRIVATE(GPU_ShaderModuleSource private;) IF_VULKAN(GPU_ShaderModuleSource spirv;) IF_D3D12(GPU_ShaderModuleSource dxil60;) IF_METAL(GPU_ShaderModuleSource msl;) + IF_WEBGPU(GPU_ShaderModuleSource wgsl;) unsigned int num_samplers; unsigned int num_uniform_buffers; } GPU_ShaderSources; @@ -91,6 +101,9 @@ typedef struct GPU_ShaderSources #define SHADER_METAL(code) \ IF_METAL(.msl = { code, sizeof(code), SDL_GPU_SHADERFORMAT_MSL }, ) +#define SHADER_WGSL(code) \ + IF_WEBGPU(.wgsl = { code, sizeof(code), SDL_GPU_SHADERFORMAT_WGSL }, ) + // clang-format off static const GPU_ShaderSources vert_shader_sources[NUM_VERT_SHADERS] = { [VERT_SHADER_LINEPOINT] = { @@ -100,6 +113,7 @@ static const GPU_ShaderSources vert_shader_sources[NUM_VERT_SHADERS] = { SHADER_SPIRV(linepoint_vert_spv) SHADER_DXIL60(linepoint_vert_dxil) SHADER_METAL(linepoint_vert_msl) + SHADER_WGSL(linepoint_vert_wgsl) }, [VERT_SHADER_TRI_COLOR] = { .num_samplers = 0, @@ -108,6 +122,7 @@ static const GPU_ShaderSources vert_shader_sources[NUM_VERT_SHADERS] = { SHADER_SPIRV(tri_color_vert_spv) SHADER_DXIL60(tri_color_vert_dxil) SHADER_METAL(tri_color_vert_msl) + SHADER_WGSL(tri_color_vert_wgsl) }, [VERT_SHADER_TRI_TEXTURE] = { .num_samplers = 0, @@ -116,6 +131,7 @@ static const GPU_ShaderSources vert_shader_sources[NUM_VERT_SHADERS] = { SHADER_SPIRV(tri_texture_vert_spv) SHADER_DXIL60(tri_texture_vert_dxil) SHADER_METAL(tri_texture_vert_msl) + SHADER_WGSL(tri_texture_vert_wgsl) }, }; @@ -127,6 +143,7 @@ static const GPU_ShaderSources frag_shader_sources[NUM_FRAG_SHADERS] = { SHADER_SPIRV(color_frag_spv) SHADER_DXIL60(color_frag_dxil) SHADER_METAL(color_frag_msl) + SHADER_WGSL(color_frag_wgsl) }, [FRAG_SHADER_TEXTURE_RGB] = { .num_samplers = 1, @@ -135,6 +152,7 @@ static const GPU_ShaderSources frag_shader_sources[NUM_FRAG_SHADERS] = { SHADER_SPIRV(texture_rgb_frag_spv) SHADER_DXIL60(texture_rgb_frag_dxil) SHADER_METAL(texture_rgb_frag_msl) + SHADER_WGSL(texture_rgb_frag_wgsl) }, [FRAG_SHADER_TEXTURE_RGBA] = { .num_samplers = 1, @@ -143,6 +161,7 @@ static const GPU_ShaderSources frag_shader_sources[NUM_FRAG_SHADERS] = { SHADER_SPIRV(texture_rgba_frag_spv) SHADER_DXIL60(texture_rgba_frag_dxil) SHADER_METAL(texture_rgba_frag_msl) + SHADER_WGSL(texture_rgba_frag_wgsl) }, [FRAG_SHADER_TEXTURE_ADVANCED] = { .num_samplers = 3, @@ -151,6 +170,7 @@ static const GPU_ShaderSources frag_shader_sources[NUM_FRAG_SHADERS] = { SHADER_SPIRV(texture_advanced_frag_spv) SHADER_DXIL60(texture_advanced_frag_dxil) SHADER_METAL(texture_advanced_frag_msl) + SHADER_WGSL(texture_advanced_frag_wgsl) }, [FRAG_SHADER_COLOR_PQ] = { .num_samplers = 0, @@ -187,6 +207,10 @@ static SDL_GPUShader *CompileShader(const GPU_ShaderSources *sources, SDL_GPUDev } else if (formats & SDL_GPU_SHADERFORMAT_MSL) { sms = &sources->msl; #endif // HAVE_METAL_SHADERS +#if HAVE_WGSL_SHADERS + } else if (formats & SDL_GPU_SHADERFORMAT_WGSL) { + sms = &sources->wgsl; +#endif // HAVE_WGSL_SHADERS } else { SDL_SetError("Unsupported GPU backend"); return NULL; diff --git a/src/render/gpu/shaders/color.frag.wgsl b/src/render/gpu/shaders/color.frag.wgsl new file mode 100644 index 0000000000..75fc69de96 --- /dev/null +++ b/src/render/gpu/shaders/color.frag.wgsl @@ -0,0 +1,25 @@ +struct Constants { + colorScale: f32, +}; + +struct PSInput { + @location(0u) vColor: vec4, +}; + +struct PSOutput { + @location(0u) oColor: vec4, +}; + +@group(3u) @binding(0u) var constants: Constants; + +fn getOutputColor(rgba: vec4, colorScale: f32) -> vec4 { + return vec4(rgba.rgb * colorScale, rgba.a); +} + +@fragment +fn main(input: PSInput) -> PSOutput { + var output: PSOutput; + output.oColor = getOutputColor(vec4(1.0f), constants.colorScale) * input.vColor; + + return output; +} diff --git a/src/render/gpu/shaders/color.frag.wgsl.h b/src/render/gpu/shaders/color.frag.wgsl.h new file mode 100644 index 0000000000..2dfcbf7d81 --- /dev/null +++ b/src/render/gpu/shaders/color.frag.wgsl.h @@ -0,0 +1,38 @@ +static const unsigned char color_frag_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, + 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, + 0x63, 0x74, 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x50, 0x53, 0x4f, + 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x6f, 0x43, 0x6f, 0x6c, 0x6f, 0x72, + 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, + 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x33, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, + 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, + 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x3a, + 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x3b, 0x0a, 0x0a, 0x66, 0x6e, 0x20, + 0x67, 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x72, + 0x67, 0x62, 0x61, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x29, + 0x20, 0x2d, 0x3e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x20, 0x7b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x28, 0x72, 0x67, 0x62, 0x61, 0x2e, 0x72, 0x67, 0x62, 0x20, 0x2a, 0x20, + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, 0x65, 0x2c, 0x20, 0x72, 0x67, 0x62, 0x61, + 0x2e, 0x61, 0x29, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x40, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, + 0x74, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, + 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x50, 0x53, 0x4f, + 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x50, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x6f, 0x43, 0x6f, + 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x67, 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x43, + 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, + 0x2e, 0x30, 0x66, 0x29, 0x2c, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x2e, + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, 0x65, 0x29, 0x20, 0x2a, 0x20, 0x69, 0x6e, + 0x70, 0x75, 0x74, 0x2e, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, + 0x7d, 0x0a +}; + +static const unsigned int color_frag_wgsl_len = 530; diff --git a/src/render/gpu/shaders/linepoint.vert.wgsl b/src/render/gpu/shaders/linepoint.vert.wgsl new file mode 100644 index 0000000000..9f7077ba3f --- /dev/null +++ b/src/render/gpu/shaders/linepoint.vert.wgsl @@ -0,0 +1,26 @@ +struct Context { + mvp: mat4x4, // Model-View-Projection matrix +}; + +struct VSInput { + @location(0u) aPosition: vec2, + @location(1u) aColor: vec4, +}; + +// The point size in WebGPU is constant since WebGPU +// hates puppies, rainbows, and the concept of love +struct VSOutput { + @location(0u) vColor: vec4, + @builtin(position) glPosition: vec4, +} + +@group(1u) @binding(0u) var context: Context; + +@vertex +fn main(input: VSInput) -> VSOutput { + var output: VSOutput; + output.glPosition = vec4(input.aPosition, 0.0f, 1.0f) * context.mvp; + output.vColor = input.aColor; + + return output; +} diff --git a/src/render/gpu/shaders/linepoint.vert.wgsl.h b/src/render/gpu/shaders/linepoint.vert.wgsl.h new file mode 100644 index 0000000000..a69719ed11 --- /dev/null +++ b/src/render/gpu/shaders/linepoint.vert.wgsl.h @@ -0,0 +1,45 @@ +static const unsigned char linepoint_vert_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6d, 0x76, 0x70, 0x3a, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x78, 0x34, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x2f, 0x2f, 0x20, 0x4d, 0x6f, 0x64, 0x65, 0x6c, 0x2d, + 0x56, 0x69, 0x65, 0x77, 0x2d, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, + 0x6d, 0x61, 0x74, 0x72, 0x69, 0x78, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, + 0x74, 0x20, 0x56, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x61, 0x50, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x2f, 0x2f, 0x20, + 0x54, 0x68, 0x65, 0x20, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x69, + 0x6e, 0x20, 0x57, 0x65, 0x62, 0x47, 0x50, 0x55, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x73, + 0x74, 0x61, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x20, 0x57, 0x65, 0x62, 0x47, 0x50, + 0x55, 0x20, 0x0a, 0x2f, 0x2f, 0x20, 0x68, 0x61, 0x74, 0x65, 0x73, 0x20, 0x70, 0x75, 0x70, 0x70, + 0x69, 0x65, 0x73, 0x2c, 0x20, 0x72, 0x61, 0x69, 0x6e, 0x62, 0x6f, 0x77, 0x73, 0x2c, 0x20, 0x61, + 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6e, 0x63, 0x65, 0x70, 0x74, 0x20, 0x6f, + 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x56, 0x53, + 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, + 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x43, 0x6f, 0x6c, 0x6f, + 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x40, 0x62, 0x75, 0x69, 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x70, 0x6f, 0x73, 0x69, 0x74, + 0x69, 0x6f, 0x6e, 0x29, 0x20, 0x67, 0x6c, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, + 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x0a, 0x0a, 0x40, + 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x31, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, + 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, 0x66, 0x6f, + 0x72, 0x6d, 0x3e, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x3a, 0x20, 0x43, 0x6f, 0x6e, + 0x74, 0x65, 0x78, 0x74, 0x3b, 0x0a, 0x0a, 0x40, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x0a, 0x66, + 0x6e, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x53, + 0x49, 0x6e, 0x70, 0x75, 0x74, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, + 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f, 0x75, 0x74, + 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x67, 0x6c, 0x50, 0x6f, 0x73, 0x69, + 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, + 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x2a, 0x20, + 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, 0x6d, 0x76, 0x70, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, + 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, + 0x74, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int linepoint_vert_wgsl_len = 645; diff --git a/src/render/gpu/shaders/texture_advanced.frag.wgsl b/src/render/gpu/shaders/texture_advanced.frag.wgsl new file mode 100644 index 0000000000..f8ee4c4c09 --- /dev/null +++ b/src/render/gpu/shaders/texture_advanced.frag.wgsl @@ -0,0 +1,312 @@ +// I couldn't be bothered with manually porting that entire shader so I used Tint + +struct Constants { + scRGB_output: f32, + texture_type: f32, + input_type: f32, + color_scale: f32, + texel_size: vec4, + tonemap_method: f32, + tonemap_factor1: f32, + tonemap_factor2: f32, + sdr_white_point: f32, + Yoffset: vec4, + Rcoeff: vec4, + Gcoeff: vec4, + Bcoeff: vec4, +} + +@group(3u) @binding(0u) var v: Constants; + +@group(2u) @binding(0u) var texture0: texture_2d; + +@group(2u) @binding(1u) var sampler0: sampler; + +@group(2u) @binding(2u) var texture1: texture_2d; + +@group(2u) @binding(3u) var sampler1: sampler; + +@group(2u) @binding(4u) var texture2: texture_2d; + +@group(2u) @binding(5u) var sampler2: sampler; + +var v_1: vec4; + +fn main_inner(v_2: vec4, v_3: vec2) { + var v_4: vec4; + var v_5: vec3; + var v_6: vec3; + var v_7: vec2; + var v_8: vec2; + var v_9: vec4; + var v_10: vec3; + var v_11: vec3; + var v_12: vec3; + var v_13: vec4; + var v_14: vec3; + var v_15: vec4; + var v_16: vec3; + var v_17: vec4; + if (v.texture_type == 0.0f) { + v_9 = vec4(1.0f); + } else if (v.texture_type == 3.0f) { + v_9 = textureSample(texture0, sampler0, v_3); + } else if (v.texture_type == 4.0f) { + let v_18 = clamp((fwidth(v_3) * v.texel_size.zw), vec2(0.00000999999974737875f), vec2(1.0f)); + let v_19 = ((v_3 * v.texel_size.zw) - (v_18 * 0.5f)); + let v_20 = smoothstep((vec2(1.0f) - v_18), vec2(1.0f), fract(v_19)); + let v_21 = (((floor(v_19) + vec2(0.5f)) + v_20) * v.texel_size.xy); + v_9 = textureSampleGrad(texture0, sampler0, v_21, dpdx(v_3), dpdy(v_3)); + } else if (v.texture_type == 1.0f) { + let v_22 = textureSample(texture0, sampler0, v_3); + v_9 = vec4(v_22.x, v_22.y, v_22.z, 1.0f); + } else if (v.texture_type == 2.0f) { + let v_23 = clamp((fwidth(v_3) * v.texel_size.zw), vec2(0.00000999999974737875f), vec2(1.0f)); + let v_24 = ((v_3 * v.texel_size.zw) - (v_23 * 0.5f)); + let v_25 = smoothstep((vec2(1.0f) - v_23), vec2(1.0f), fract(v_24)); + let v_26 = (((floor(v_24) + vec2(0.5f)) + v_25) * v.texel_size.xy); + let v_27 = textureSampleGrad(texture0, sampler0, v_26, dpdx(v_3), dpdy(v_3)); + v_9 = vec4(v_27.x, v_27.y, v_27.z, 1.0f); + } else if (v.texture_type == 5.0f) { + let v_28 = (textureSample(texture0, sampler0, v_3).x * 255.0f); + v_9 = textureSample(texture1, sampler1, vec2(((v_28 + 0.5f) * 0.00390625f), 0.5f)); + } else if (v.texture_type == 6.0f) { + let v_29 = ((v_3 * v.texel_size.zw) + vec2(0.5f)); + let v_30 = ((floor(v_29) - vec2(0.5f)) * v.texel_size.xy); + let v_31 = ((floor(v_29) + vec2(0.5f)) * v.texel_size.xy); + let v_32 = vec4(v_30.x, v_30.y, v_31.x, v_31.y); + v_8 = fract(v_29); + let v_33 = (textureSample(texture0, sampler0, v_32.xy).x * 255.0f); + let v_34 = textureSample(texture1, sampler1, vec2(((v_33 + 0.5f) * 0.00390625f), 0.5f)); + let v_35 = (textureSample(texture0, sampler0, v_32.xw).x * 255.0f); + let v_36 = textureSample(texture1, sampler1, vec2(((v_35 + 0.5f) * 0.00390625f), 0.5f)); + let v_37 = (textureSample(texture0, sampler0, v_32.zy).x * 255.0f); + let v_38 = textureSample(texture1, sampler1, vec2(((v_37 + 0.5f) * 0.00390625f), 0.5f)); + let v_39 = (textureSample(texture0, sampler0, v_32.zw).x * 255.0f); + let v_40 = textureSample(texture1, sampler1, vec2(((v_39 + 0.5f) * 0.00390625f), 0.5f)); + let v_41 = v_8.y; + let v_42 = mix(v_34, v_36, vec4(v_41, v_41, v_41, v_41)); + let v_43 = v_8.y; + let v_44 = mix(v_38, v_40, vec4(v_43, v_43, v_43, v_43)); + let v_45 = v_8.x; + v_9 = mix(v_42, v_44, vec4(v_45, v_45, v_45, v_45)); + } else if (v.texture_type == 7.0f) { + let v_46 = clamp((fwidth(v_3) * v.texel_size.zw), vec2(0.00000999999974737875f), vec2(1.0f)); + let v_47 = ((v_3 * v.texel_size.zw) - (v_46 * 0.5f)); + let v_48 = smoothstep((vec2(1.0f) - v_46), vec2(1.0f), fract(v_47)); + let v_49 = (((((floor(v_47) + vec2(0.5f)) + v_48) * v.texel_size.xy) * v.texel_size.zw) + vec2(0.5f)); + let v_50 = ((floor(v_49) - vec2(0.5f)) * v.texel_size.xy); + let v_51 = ((floor(v_49) + vec2(0.5f)) * v.texel_size.xy); + let v_52 = vec4(v_50.x, v_50.y, v_51.x, v_51.y); + v_7 = fract(v_49); + let v_53 = (textureSample(texture0, sampler0, v_52.xy).x * 255.0f); + let v_54 = textureSample(texture1, sampler1, vec2(((v_53 + 0.5f) * 0.00390625f), 0.5f)); + let v_55 = (textureSample(texture0, sampler0, v_52.xw).x * 255.0f); + let v_56 = textureSample(texture1, sampler1, vec2(((v_55 + 0.5f) * 0.00390625f), 0.5f)); + let v_57 = (textureSample(texture0, sampler0, v_52.zy).x * 255.0f); + let v_58 = textureSample(texture1, sampler1, vec2(((v_57 + 0.5f) * 0.00390625f), 0.5f)); + let v_59 = (textureSample(texture0, sampler0, v_52.zw).x * 255.0f); + let v_60 = textureSample(texture1, sampler1, vec2(((v_59 + 0.5f) * 0.00390625f), 0.5f)); + let v_61 = v_7.y; + let v_62 = mix(v_54, v_56, vec4(v_61, v_61, v_61, v_61)); + let v_63 = v_7.y; + let v_64 = mix(v_58, v_60, vec4(v_63, v_63, v_63, v_63)); + let v_65 = v_7.x; + v_9 = mix(v_62, v_64, vec4(v_65, v_65, v_65, v_65)); + } else if (v.texture_type == 8.0f) { + v_10.x = textureSample(texture0, sampler0, v_3).x; + let v_66 = textureSample(texture1, sampler0, v_3); + v_10.y = v_66.x; + v_10.z = v_66.y; + let v_67 = v.Yoffset.xyz; + let v_68 = (v_10 + v_67); + v_10 = v_68; + v_9.x = dot(v_68, v.Rcoeff.xyz); + v_9.y = dot(v_68, v.Gcoeff.xyz); + v_9.z = dot(v_68, v.Bcoeff.xyz); + v_9.w = 1.0f; + } else if (v.texture_type == 9.0f) { + v_11.x = textureSample(texture0, sampler0, v_3).x; + let v_69 = textureSample(texture1, sampler0, v_3); + v_11.y = v_69.y; + v_11.z = v_69.x; + let v_70 = v.Yoffset.xyz; + let v_71 = (v_11 + v_70); + v_11 = v_71; + v_9.x = dot(v_71, v.Rcoeff.xyz); + v_9.y = dot(v_71, v.Gcoeff.xyz); + v_9.z = dot(v_71, v.Bcoeff.xyz); + v_9.w = 1.0f; + } else if (v.texture_type == 10.0f) { + v_12.x = textureSample(texture0, sampler0, v_3).x; + v_12.y = textureSample(texture1, sampler0, v_3).x; + v_12.z = textureSample(texture2, sampler0, v_3).x; + let v_72 = v.Yoffset.xyz; + let v_73 = (v_12 + v_72); + v_12 = v_73; + v_9.x = dot(v_73, v.Rcoeff.xyz); + v_9.y = dot(v_73, v.Gcoeff.xyz); + v_9.z = dot(v_73, v.Bcoeff.xyz); + v_9.w = 1.0f; + } else { + v_9.x = 1.0f; + v_9.y = 0.0f; + v_9.z = 1.0f; + v_9.w = 1.0f; + } + v_13 = v_9; + if (v.input_type == 3.0f) { + let v_74 = v_13.xyz; + let v_75 = (pow(abs((max((pow(abs(v_74), vec3(0.01268331333994865417f)) - vec3(0.8359375f)), vec3()) / (vec3(18.8515625f) - (pow(abs(v_74), vec3(0.01268331333994865417f)) * 18.6875f)))), vec3(6.27739477157592773438f)) * 10000.0f); + let v_76 = v.sdr_white_point; + let v_77 = (v_75 / vec3(v_76, v_76, v_76)); + v_13.x = v_77.x; + v_13.y = v_77.y; + v_13.z = v_77.z; + } + if !((v.tonemap_method == 0.0f)) { + v_14 = v_13.xyz; + if (v.tonemap_method == 1.0f) { + let v_78 = v.tonemap_factor1; + v_14 = (v_14 * v_78); + } else if (v.tonemap_method == 2.0f) { + if (v.input_type == 2.0f) { + v_14 = (v_14 * mat3x3(vec3(0.62740397453308105469f, 0.32928299903869628906f, 0.04331300035119056702f), vec3(0.06909699738025665283f, 0.91954100131988525391f, 0.01136200036853551865f), vec3(0.01639099977910518646f, 0.08801300078630447388f, 0.89559501409530639648f))); + } + let v_79 = max(v_14.x, max(v_14.y, v_14.z)); + if (v_79 > 0.0f) { + let v_80 = ((1.0f + (v.tonemap_factor1 * v_79)) / (1.0f + (v.tonemap_factor2 * v_79))); + v_14 = (v_14 * v_80); + } + if (v.input_type == 2.0f) { + v_14 = (v_14 * mat3x3(vec3(1.66049599647521972656f, -0.5876560211181640625f, -0.07283999770879745483f), vec3(-0.12454699724912643433f, 1.13289499282836914062f, -0.00834800023585557938f), vec3(-0.01815400086343288422f, -0.10059700161218643188f, 1.11875104904174804688f))); + } + } + let v_81 = v_14; + v_13.x = v_81.x; + v_13.y = v_81.y; + v_13.z = v_81.z; + } + if (v.input_type == 1.0f) { + v_16 = v_13.xyz; + if !((v.scRGB_output == 0.0f)) { + let v_82 = v_16.x; + var v_83: f32; + if (v_82 <= 0.04044999927282333374f) { + v_83 = (v_82 * 0.07739938050508499146f); + } else { + v_83 = pow((abs((v_82 + 0.05499999970197677612f)) * 0.94786733388900756836f), 2.40000009536743164062f); + } + v_16.x = v_83; + let v_84 = v_16.y; + var v_85: f32; + if (v_84 <= 0.04044999927282333374f) { + v_85 = (v_84 * 0.07739938050508499146f); + } else { + v_85 = pow((abs((v_84 + 0.05499999970197677612f)) * 0.94786733388900756836f), 2.40000009536743164062f); + } + v_16.y = v_85; + let v_86 = v_16.z; + var v_87: f32; + if (v_86 <= 0.04044999927282333374f) { + v_87 = (v_86 * 0.07739938050508499146f); + } else { + v_87 = pow((abs((v_86 + 0.05499999970197677612f)) * 0.94786733388900756836f), 2.40000009536743164062f); + } + v_16.z = v_87; + } + let v_88 = (v_16 * v.color_scale); + v_15.x = v_88.x; + v_15.y = v_88.y; + v_15.z = v_88.z; + v_15.w = v_13.w; + } else if (v.input_type == 2.0f) { + v_6 = (v_13.xyz * v.color_scale); + if (v.scRGB_output == 0.0f) { + let v_89 = v_6.x; + var v_90: f32; + if (v_89 <= 0.00313080009073019028f) { + v_90 = (v_89 * 12.9200000762939453125f); + } else { + v_90 = ((pow(abs(v_89), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_6.x = v_90; + let v_91 = v_6.y; + var v_92: f32; + if (v_91 <= 0.00313080009073019028f) { + v_92 = (v_91 * 12.9200000762939453125f); + } else { + v_92 = ((pow(abs(v_91), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_6.y = v_92; + let v_93 = v_6.z; + var v_94: f32; + if (v_93 <= 0.00313080009073019028f) { + v_94 = (v_93 * 12.9200000762939453125f); + } else { + v_94 = ((pow(abs(v_93), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_6.z = v_94; + v_6 = clamp(v_6, vec3(), vec3(1.0f)); + } + let v_95 = v_6; + v_15.x = v_95.x; + v_15.y = v_95.y; + v_15.z = v_95.z; + v_15.w = v_13.w; + } else if (v.input_type == 3.0f) { + let v_96 = (v_13.xyz * mat3x3(vec3(1.66049599647521972656f, -0.5876560211181640625f, -0.07283999770879745483f), vec3(-0.12454699724912643433f, 1.13289499282836914062f, -0.00834800023585557938f), vec3(-0.01815400086343288422f, -0.10059700161218643188f, 1.11875104904174804688f))); + v_13.x = v_96.x; + v_13.y = v_96.y; + v_13.z = v_96.z; + v_5 = (v_13.xyz * v.color_scale); + if (v.scRGB_output == 0.0f) { + let v_97 = v_5.x; + var v_98: f32; + if (v_97 <= 0.00313080009073019028f) { + v_98 = (v_97 * 12.9200000762939453125f); + } else { + v_98 = ((pow(abs(v_97), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_5.x = v_98; + let v_99 = v_5.y; + var v_100: f32; + if (v_99 <= 0.00313080009073019028f) { + v_100 = (v_99 * 12.9200000762939453125f); + } else { + v_100 = ((pow(abs(v_99), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_5.y = v_100; + let v_101 = v_5.z; + var v_102: f32; + if (v_101 <= 0.00313080009073019028f) { + v_102 = (v_101 * 12.9200000762939453125f); + } else { + v_102 = ((pow(abs(v_101), 0.4166666567325592041f) * 1.05499994754791259766f) - 0.05499999970197677612f); + } + v_5.z = v_102; + v_5 = clamp(v_5, vec3(), vec3(1.0f)); + } + let v_103 = v_5; + v_15.x = v_103.x; + v_15.y = v_103.y; + v_15.z = v_103.z; + v_15.w = v_13.w; + } else { + let v_104 = v_13; + v_17 = v_104; + let v_105 = (v_104.xyz * v.color_scale); + v_4.x = v_105.x; + v_4.y = v_105.y; + v_4.z = v_105.z; + v_4.w = v_17.w; + v_15 = v_4; + } + v_1 = (v_15 * v_2); +} + +@fragment +fn main(@location(0u) v_106: vec4, @location(1u) v_107: vec2) -> @location(0u) vec4 { + main_inner(v_106, v_107); + return v_1; +} diff --git a/src/render/gpu/shaders/texture_advanced.frag.wgsl.h b/src/render/gpu/shaders/texture_advanced.frag.wgsl.h new file mode 100644 index 0000000000..40fee570cc --- /dev/null +++ b/src/render/gpu/shaders/texture_advanced.frag.wgsl.h @@ -0,0 +1,866 @@ +static const unsigned char texture_advanced_frag_wgsl[] = { + 0x2f, 0x2f, 0x20, 0x49, 0x20, 0x63, 0x6f, 0x75, 0x6c, 0x64, 0x6e, 0x27, 0x74, 0x20, 0x62, 0x65, + 0x20, 0x62, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6d, + 0x61, 0x6e, 0x75, 0x61, 0x6c, 0x6c, 0x79, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x69, 0x6e, 0x67, 0x20, + 0x74, 0x68, 0x61, 0x74, 0x20, 0x65, 0x6e, 0x74, 0x69, 0x72, 0x65, 0x20, 0x73, 0x68, 0x61, 0x64, + 0x65, 0x72, 0x20, 0x73, 0x6f, 0x20, 0x49, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x54, 0x69, 0x6e, + 0x74, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, + 0x6e, 0x74, 0x73, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x73, 0x63, 0x52, 0x47, 0x42, 0x5f, + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x66, + 0x33, 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, + 0x70, 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x63, 0x6f, 0x6c, + 0x6f, 0x72, 0x5f, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x3a, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x74, 0x6f, + 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x3a, 0x20, 0x66, 0x33, + 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x66, + 0x61, 0x63, 0x74, 0x6f, 0x72, 0x31, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x32, + 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x73, 0x64, 0x72, 0x5f, 0x77, + 0x68, 0x69, 0x74, 0x65, 0x5f, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x59, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x52, 0x63, 0x6f, + 0x65, 0x66, 0x66, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x47, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x42, 0x63, 0x6f, 0x65, 0x66, + 0x66, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x0a, + 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x33, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, + 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, + 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x76, 0x3a, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, + 0x74, 0x73, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, + 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x5f, 0x32, 0x64, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, + 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, + 0x28, 0x31, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, + 0x30, 0x3a, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, + 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, + 0x28, 0x32, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x31, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x32, 0x64, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, + 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x33, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, + 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x3a, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x72, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, + 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x34, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x32, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x5f, 0x32, 0x64, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, + 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, + 0x28, 0x35, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, + 0x32, 0x3a, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3b, 0x0a, 0x0a, 0x76, 0x61, 0x72, + 0x3c, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x3e, 0x20, 0x76, 0x5f, 0x31, 0x3a, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, + 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x28, 0x76, 0x5f, 0x32, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x61, 0x72, 0x20, 0x76, 0x5f, 0x34, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x35, 0x3a, 0x20, + 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x61, 0x72, 0x20, 0x76, 0x5f, 0x36, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x37, 0x3a, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x61, 0x72, 0x20, 0x76, 0x5f, 0x38, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x39, 0x3a, 0x20, + 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x31, + 0x3a, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x32, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, + 0x31, 0x33, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x34, 0x3a, 0x20, 0x76, 0x65, 0x63, + 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, + 0x76, 0x5f, 0x31, 0x35, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x36, 0x3a, 0x20, 0x76, + 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, + 0x72, 0x20, 0x76, 0x5f, 0x31, 0x37, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x2e, 0x30, + 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, + 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, + 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, + 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, + 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x33, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, + 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, + 0x20, 0x3d, 0x3d, 0x20, 0x34, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x38, 0x20, 0x3d, 0x20, 0x63, + 0x6c, 0x61, 0x6d, 0x70, 0x28, 0x28, 0x66, 0x77, 0x69, 0x64, 0x74, 0x68, 0x28, 0x76, 0x5f, 0x33, + 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, + 0x2e, 0x7a, 0x77, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, + 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x34, + 0x37, 0x33, 0x37, 0x38, 0x37, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, + 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x39, 0x20, 0x3d, 0x20, 0x28, + 0x28, 0x76, 0x5f, 0x33, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, + 0x69, 0x7a, 0x65, 0x2e, 0x7a, 0x77, 0x29, 0x20, 0x2d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x38, 0x20, + 0x2a, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x30, 0x20, 0x3d, 0x20, 0x73, 0x6d, 0x6f, + 0x6f, 0x74, 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, 0x28, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x2d, 0x20, 0x76, 0x5f, 0x31, 0x38, 0x29, + 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, + 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x28, 0x76, 0x5f, 0x31, 0x39, 0x29, 0x29, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, + 0x31, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, 0x76, 0x5f, 0x31, + 0x39, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, + 0x2e, 0x35, 0x66, 0x29, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x32, 0x30, 0x29, 0x20, 0x2a, 0x20, + 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x78, 0x79, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x20, 0x3d, 0x20, + 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x47, 0x72, 0x61, + 0x64, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x32, 0x31, 0x2c, 0x20, 0x64, 0x70, 0x64, 0x78, + 0x28, 0x76, 0x5f, 0x33, 0x29, 0x2c, 0x20, 0x64, 0x70, 0x64, 0x79, 0x28, 0x76, 0x5f, 0x33, 0x29, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, + 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, + 0x20, 0x3d, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x32, 0x20, 0x3d, 0x20, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, + 0x20, 0x76, 0x5f, 0x33, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x5f, 0x39, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, + 0x5f, 0x32, 0x32, 0x2e, 0x78, 0x2c, 0x20, 0x76, 0x5f, 0x32, 0x32, 0x2e, 0x79, 0x2c, 0x20, 0x76, + 0x5f, 0x32, 0x32, 0x2e, 0x7a, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x32, + 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x33, 0x20, 0x3d, 0x20, 0x63, 0x6c, 0x61, 0x6d, 0x70, 0x28, + 0x28, 0x66, 0x77, 0x69, 0x64, 0x74, 0x68, 0x28, 0x76, 0x5f, 0x33, 0x29, 0x20, 0x2a, 0x20, 0x76, + 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x7a, 0x77, 0x29, 0x2c, + 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x34, 0x37, 0x33, 0x37, 0x38, 0x37, + 0x35, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, + 0x2e, 0x30, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x34, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x76, 0x5f, 0x33, 0x20, + 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x7a, + 0x77, 0x29, 0x20, 0x2d, 0x20, 0x28, 0x76, 0x5f, 0x32, 0x33, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x35, + 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, + 0x20, 0x76, 0x5f, 0x32, 0x35, 0x20, 0x3d, 0x20, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x73, 0x74, + 0x65, 0x70, 0x28, 0x28, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, + 0x30, 0x66, 0x29, 0x20, 0x2d, 0x20, 0x76, 0x5f, 0x32, 0x33, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, + 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x2c, 0x20, 0x66, 0x72, + 0x61, 0x63, 0x74, 0x28, 0x76, 0x5f, 0x32, 0x34, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x36, 0x20, 0x3d, 0x20, 0x28, + 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, 0x76, 0x5f, 0x32, 0x34, 0x29, 0x20, 0x2b, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, + 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x32, 0x35, 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, + 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x78, 0x79, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x32, 0x37, 0x20, 0x3d, 0x20, + 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x47, 0x72, 0x61, + 0x64, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x32, 0x36, 0x2c, 0x20, 0x64, 0x70, 0x64, 0x78, + 0x28, 0x76, 0x5f, 0x33, 0x29, 0x2c, 0x20, 0x64, 0x70, 0x64, 0x79, 0x28, 0x76, 0x5f, 0x33, 0x29, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x20, 0x3d, + 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x32, 0x37, 0x2e, + 0x78, 0x2c, 0x20, 0x76, 0x5f, 0x32, 0x37, 0x2e, 0x79, 0x2c, 0x20, 0x76, 0x5f, 0x32, 0x37, 0x2e, + 0x7a, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, + 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x35, 0x2e, 0x30, 0x66, 0x29, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, + 0x5f, 0x32, 0x38, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x29, 0x2e, 0x78, 0x20, + 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, + 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, 0x5f, 0x32, 0x38, 0x20, 0x2b, 0x20, 0x30, 0x2e, + 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, + 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x36, 0x2e, 0x30, + 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, + 0x20, 0x76, 0x5f, 0x32, 0x39, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x76, 0x5f, 0x33, 0x20, 0x2a, 0x20, + 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x7a, 0x77, 0x29, + 0x20, 0x2b, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x35, + 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, + 0x20, 0x76, 0x5f, 0x33, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, + 0x76, 0x5f, 0x32, 0x39, 0x29, 0x20, 0x2d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, + 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x78, 0x79, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x31, 0x20, 0x3d, 0x20, + 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, 0x76, 0x5f, 0x32, 0x39, 0x29, 0x20, 0x2b, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, + 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, + 0x78, 0x79, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, + 0x20, 0x76, 0x5f, 0x33, 0x32, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x28, 0x76, 0x5f, 0x33, 0x30, 0x2e, 0x78, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x30, 0x2e, 0x79, + 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x31, 0x2e, 0x78, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x31, 0x2e, 0x79, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x38, 0x20, 0x3d, + 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x28, 0x76, 0x5f, 0x32, 0x39, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x33, 0x20, 0x3d, + 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, + 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, + 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x32, 0x2e, 0x78, 0x79, 0x29, 0x2e, 0x78, 0x20, 0x2a, + 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x34, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, + 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, 0x5f, 0x33, 0x33, 0x20, + 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, + 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x35, + 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x32, 0x2e, 0x78, 0x77, 0x29, 0x2e, 0x78, + 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x36, 0x20, 0x3d, 0x20, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, + 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, 0x5f, 0x33, + 0x35, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, + 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, + 0x33, 0x37, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x32, 0x2e, 0x7a, 0x79, 0x29, + 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x33, 0x38, 0x20, 0x3d, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, + 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, + 0x5f, 0x33, 0x37, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, + 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, + 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, + 0x76, 0x5f, 0x33, 0x39, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, + 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x32, 0x2e, 0x7a, + 0x77, 0x29, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x30, + 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, + 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, + 0x28, 0x76, 0x5f, 0x33, 0x39, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, + 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, + 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x34, 0x31, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x2e, 0x79, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x32, + 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, 0x33, 0x34, 0x2c, 0x20, 0x76, 0x5f, 0x33, + 0x36, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x34, + 0x31, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x31, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x31, 0x2c, 0x20, 0x76, + 0x5f, 0x34, 0x31, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x33, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x2e, 0x79, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, + 0x34, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, 0x33, 0x38, 0x2c, 0x20, 0x76, 0x5f, + 0x34, 0x30, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, + 0x34, 0x33, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x33, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x33, 0x2c, 0x20, + 0x76, 0x5f, 0x34, 0x33, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x35, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x2e, 0x78, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x20, 0x3d, 0x20, + 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, 0x34, 0x32, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x34, 0x2c, 0x20, + 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x34, 0x35, 0x2c, 0x20, + 0x76, 0x5f, 0x34, 0x35, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x35, 0x2c, 0x20, 0x76, 0x5f, 0x34, 0x35, + 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, + 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, + 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x37, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x36, 0x20, 0x3d, 0x20, + 0x63, 0x6c, 0x61, 0x6d, 0x70, 0x28, 0x28, 0x66, 0x77, 0x69, 0x64, 0x74, 0x68, 0x28, 0x76, 0x5f, + 0x33, 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, + 0x65, 0x2e, 0x7a, 0x77, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x28, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, + 0x34, 0x37, 0x33, 0x37, 0x38, 0x37, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x37, 0x20, 0x3d, 0x20, + 0x28, 0x28, 0x76, 0x5f, 0x33, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, + 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x7a, 0x77, 0x29, 0x20, 0x2d, 0x20, 0x28, 0x76, 0x5f, 0x34, 0x36, + 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x34, 0x38, 0x20, 0x3d, 0x20, 0x73, 0x6d, + 0x6f, 0x6f, 0x74, 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, 0x28, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, + 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x2d, 0x20, 0x76, 0x5f, 0x34, 0x36, + 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, + 0x66, 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x28, 0x76, 0x5f, 0x34, 0x37, 0x29, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, + 0x34, 0x39, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x28, 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, + 0x76, 0x5f, 0x34, 0x37, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x34, 0x38, 0x29, + 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, + 0x78, 0x79, 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, + 0x7a, 0x65, 0x2e, 0x7a, 0x77, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x35, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x28, + 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, 0x76, 0x5f, 0x34, 0x39, 0x29, 0x20, 0x2d, 0x20, 0x76, 0x65, + 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x20, 0x2a, + 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x78, 0x79, + 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, + 0x5f, 0x35, 0x31, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x66, 0x6c, 0x6f, 0x6f, 0x72, 0x28, 0x76, 0x5f, + 0x34, 0x39, 0x29, 0x20, 0x2b, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, + 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x65, 0x6c, + 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x2e, 0x78, 0x79, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x35, 0x32, 0x20, 0x3d, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x35, 0x30, 0x2e, 0x78, 0x2c, 0x20, + 0x76, 0x5f, 0x35, 0x30, 0x2e, 0x79, 0x2c, 0x20, 0x76, 0x5f, 0x35, 0x31, 0x2e, 0x78, 0x2c, 0x20, + 0x76, 0x5f, 0x35, 0x31, 0x2e, 0x79, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x5f, 0x37, 0x20, 0x3d, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x28, 0x76, 0x5f, 0x34, + 0x39, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, + 0x76, 0x5f, 0x35, 0x33, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, + 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x35, 0x32, 0x2e, 0x78, + 0x79, 0x29, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x35, 0x34, + 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, + 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, + 0x28, 0x76, 0x5f, 0x35, 0x33, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, + 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, + 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x35, 0x35, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, + 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, + 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x35, 0x32, + 0x2e, 0x78, 0x77, 0x29, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, 0x66, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, + 0x35, 0x36, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x28, 0x28, 0x28, 0x76, 0x5f, 0x35, 0x35, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x20, + 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, 0x2c, 0x20, + 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x35, 0x37, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, 0x78, 0x74, + 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, + 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, + 0x35, 0x32, 0x2e, 0x7a, 0x79, 0x29, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, 0x2e, 0x30, + 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, + 0x76, 0x5f, 0x35, 0x38, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, 0x5f, 0x35, 0x37, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x35, 0x66, + 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, 0x66, 0x29, + 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x35, 0x39, 0x20, 0x3d, 0x20, 0x28, 0x74, 0x65, + 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, + 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, + 0x76, 0x5f, 0x35, 0x32, 0x2e, 0x7a, 0x77, 0x29, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x35, 0x35, + 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x36, 0x30, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, + 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x31, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x28, 0x28, 0x28, 0x76, 0x5f, 0x35, 0x39, 0x20, 0x2b, 0x20, 0x30, 0x2e, + 0x35, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x39, 0x30, 0x36, 0x32, 0x35, + 0x66, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x35, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x31, 0x20, 0x3d, 0x20, 0x76, + 0x5f, 0x37, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x36, 0x32, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, 0x35, + 0x34, 0x2c, 0x20, 0x76, 0x5f, 0x35, 0x36, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x36, 0x31, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x31, 0x2c, 0x20, 0x76, + 0x5f, 0x36, 0x31, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x31, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x33, 0x20, 0x3d, 0x20, + 0x76, 0x5f, 0x37, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x34, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, + 0x35, 0x38, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x30, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, + 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x36, 0x33, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x33, 0x2c, 0x20, + 0x76, 0x5f, 0x36, 0x33, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x33, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x35, 0x20, 0x3d, + 0x20, 0x76, 0x5f, 0x37, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x39, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x5f, 0x36, 0x32, 0x2c, 0x20, + 0x76, 0x5f, 0x36, 0x34, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, + 0x76, 0x5f, 0x36, 0x35, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x35, 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x35, + 0x2c, 0x20, 0x76, 0x5f, 0x36, 0x35, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, + 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x38, 0x2e, 0x30, 0x66, 0x29, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x29, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x36, 0x20, 0x3d, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, + 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x5f, 0x31, 0x30, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x36, 0x36, 0x2e, 0x78, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x2e, 0x7a, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x36, 0x36, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x37, 0x20, 0x3d, 0x20, 0x76, 0x2e, + 0x59, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x38, 0x20, 0x3d, 0x20, + 0x28, 0x76, 0x5f, 0x31, 0x30, 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x36, 0x37, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x36, 0x38, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x36, 0x38, 0x2c, 0x20, 0x76, 0x2e, + 0x52, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, + 0x28, 0x76, 0x5f, 0x36, 0x38, 0x2c, 0x20, 0x76, 0x2e, 0x47, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, + 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, + 0x39, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x36, 0x38, 0x2c, 0x20, + 0x76, 0x2e, 0x42, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x31, + 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, + 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, + 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x39, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x31, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, + 0x20, 0x76, 0x5f, 0x33, 0x29, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x36, 0x39, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, + 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, + 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, + 0x33, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x31, + 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x36, 0x39, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x31, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x36, 0x39, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x37, 0x30, 0x20, 0x3d, 0x20, 0x76, 0x2e, 0x59, 0x6f, 0x66, 0x66, 0x73, + 0x65, 0x74, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x31, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x31, + 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x37, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x31, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x37, 0x31, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x64, + 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x37, 0x31, 0x2c, 0x20, 0x76, 0x2e, 0x52, 0x63, 0x6f, 0x65, 0x66, + 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x39, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x37, 0x31, + 0x2c, 0x20, 0x76, 0x2e, 0x47, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x7a, 0x20, 0x3d, + 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x37, 0x31, 0x2c, 0x20, 0x76, 0x2e, 0x42, 0x63, 0x6f, + 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, + 0x2e, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, + 0x20, 0x31, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x32, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x30, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, + 0x29, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, + 0x32, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x31, 0x2c, 0x20, 0x73, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x29, 0x2e, 0x78, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x32, 0x2e, 0x7a, 0x20, 0x3d, + 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x74, + 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x32, 0x2c, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, + 0x30, 0x2c, 0x20, 0x76, 0x5f, 0x33, 0x29, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x32, 0x20, 0x3d, 0x20, 0x76, 0x2e, + 0x59, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x33, 0x20, 0x3d, 0x20, + 0x28, 0x76, 0x5f, 0x31, 0x32, 0x20, 0x2b, 0x20, 0x76, 0x5f, 0x37, 0x32, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x32, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x37, 0x33, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x37, 0x33, 0x2c, 0x20, 0x76, 0x2e, + 0x52, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, + 0x28, 0x76, 0x5f, 0x37, 0x33, 0x2c, 0x20, 0x76, 0x2e, 0x47, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, + 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, + 0x39, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x64, 0x6f, 0x74, 0x28, 0x76, 0x5f, 0x37, 0x33, 0x2c, 0x20, + 0x76, 0x2e, 0x42, 0x63, 0x6f, 0x65, 0x66, 0x66, 0x2e, 0x78, 0x79, 0x7a, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x31, + 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x78, 0x20, + 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x39, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x31, 0x2e, + 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x2e, + 0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, + 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x33, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x34, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x35, 0x20, 0x3d, 0x20, + 0x28, 0x70, 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x28, 0x6d, 0x61, 0x78, 0x28, 0x28, 0x70, + 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x37, 0x34, 0x29, 0x2c, 0x20, 0x76, 0x65, + 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x30, 0x31, 0x32, 0x36, 0x38, 0x33, + 0x33, 0x31, 0x33, 0x33, 0x33, 0x39, 0x39, 0x34, 0x38, 0x36, 0x35, 0x34, 0x31, 0x37, 0x66, 0x29, + 0x29, 0x20, 0x2d, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, + 0x38, 0x33, 0x35, 0x39, 0x33, 0x37, 0x35, 0x66, 0x29, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x29, 0x29, 0x20, 0x2f, 0x20, 0x28, 0x76, 0x65, 0x63, 0x33, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x38, 0x2e, 0x38, 0x35, 0x31, 0x35, 0x36, 0x32, 0x35, + 0x66, 0x29, 0x20, 0x2d, 0x20, 0x28, 0x70, 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, + 0x37, 0x34, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, + 0x2e, 0x30, 0x31, 0x32, 0x36, 0x38, 0x33, 0x33, 0x31, 0x33, 0x33, 0x33, 0x39, 0x39, 0x34, 0x38, + 0x36, 0x35, 0x34, 0x31, 0x37, 0x66, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x38, 0x2e, 0x36, 0x38, + 0x37, 0x35, 0x66, 0x29, 0x29, 0x29, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x36, 0x2e, 0x32, 0x37, 0x37, 0x33, 0x39, 0x34, 0x37, 0x37, 0x31, 0x35, 0x37, + 0x35, 0x39, 0x32, 0x37, 0x37, 0x33, 0x34, 0x33, 0x38, 0x66, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x31, + 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x36, 0x20, 0x3d, 0x20, 0x76, 0x2e, 0x73, + 0x64, 0x72, 0x5f, 0x77, 0x68, 0x69, 0x74, 0x65, 0x5f, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x37, 0x37, + 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x37, 0x35, 0x20, 0x2f, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x5f, 0x37, 0x36, 0x2c, 0x20, 0x76, 0x5f, 0x37, 0x36, 0x2c, + 0x20, 0x76, 0x5f, 0x37, 0x36, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x37, 0x37, 0x2e, 0x78, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x79, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x37, 0x37, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x37, 0x37, + 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, + 0x20, 0x21, 0x28, 0x28, 0x76, 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x6d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x29, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x3d, 0x20, + 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, + 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x20, 0x3d, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x20, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x37, 0x38, 0x20, 0x3d, 0x20, 0x76, 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, + 0x61, 0x70, 0x5f, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x3d, 0x20, 0x28, + 0x76, 0x5f, 0x31, 0x34, 0x20, 0x2a, 0x20, 0x76, 0x5f, 0x37, 0x38, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, + 0x28, 0x76, 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x6d, 0x65, 0x74, 0x68, 0x6f, + 0x64, 0x20, 0x3d, 0x3d, 0x20, 0x32, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x69, + 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x32, 0x2e, 0x30, + 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, + 0x34, 0x20, 0x2a, 0x20, 0x6d, 0x61, 0x74, 0x33, 0x78, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, + 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x36, 0x32, 0x37, 0x34, + 0x30, 0x33, 0x39, 0x37, 0x34, 0x35, 0x33, 0x33, 0x30, 0x38, 0x31, 0x30, 0x35, 0x34, 0x36, 0x39, + 0x66, 0x2c, 0x20, 0x30, 0x2e, 0x33, 0x32, 0x39, 0x32, 0x38, 0x32, 0x39, 0x39, 0x39, 0x30, 0x33, + 0x38, 0x36, 0x39, 0x36, 0x32, 0x38, 0x39, 0x30, 0x36, 0x66, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x34, + 0x33, 0x33, 0x31, 0x33, 0x30, 0x30, 0x30, 0x33, 0x35, 0x31, 0x31, 0x39, 0x30, 0x35, 0x36, 0x37, + 0x30, 0x32, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, + 0x30, 0x2e, 0x30, 0x36, 0x39, 0x30, 0x39, 0x36, 0x39, 0x39, 0x37, 0x33, 0x38, 0x30, 0x32, 0x35, + 0x36, 0x36, 0x35, 0x32, 0x38, 0x33, 0x66, 0x2c, 0x20, 0x30, 0x2e, 0x39, 0x31, 0x39, 0x35, 0x34, + 0x31, 0x30, 0x30, 0x31, 0x33, 0x31, 0x39, 0x38, 0x38, 0x35, 0x32, 0x35, 0x33, 0x39, 0x31, 0x66, + 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x31, 0x31, 0x33, 0x36, 0x32, 0x30, 0x30, 0x30, 0x33, 0x36, 0x38, + 0x35, 0x33, 0x35, 0x35, 0x31, 0x38, 0x36, 0x35, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x30, 0x2e, 0x30, 0x31, 0x36, 0x33, 0x39, 0x30, 0x39, 0x39, + 0x39, 0x37, 0x37, 0x39, 0x31, 0x30, 0x35, 0x31, 0x38, 0x36, 0x34, 0x36, 0x66, 0x2c, 0x20, 0x30, + 0x2e, 0x30, 0x38, 0x38, 0x30, 0x31, 0x33, 0x30, 0x30, 0x30, 0x37, 0x38, 0x36, 0x33, 0x30, 0x34, + 0x34, 0x37, 0x33, 0x38, 0x38, 0x66, 0x2c, 0x20, 0x30, 0x2e, 0x38, 0x39, 0x35, 0x35, 0x39, 0x35, + 0x30, 0x31, 0x34, 0x30, 0x39, 0x35, 0x33, 0x30, 0x36, 0x33, 0x39, 0x36, 0x34, 0x38, 0x66, 0x29, + 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x37, 0x39, 0x20, 0x3d, 0x20, 0x6d, 0x61, 0x78, 0x28, 0x76, 0x5f, 0x31, + 0x34, 0x2e, 0x78, 0x2c, 0x20, 0x6d, 0x61, 0x78, 0x28, 0x76, 0x5f, 0x31, 0x34, 0x2e, 0x79, 0x2c, + 0x20, 0x76, 0x5f, 0x31, 0x34, 0x2e, 0x7a, 0x29, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x37, 0x39, 0x20, + 0x3e, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, + 0x38, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x20, 0x2b, 0x20, 0x28, 0x76, + 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x31, + 0x20, 0x2a, 0x20, 0x76, 0x5f, 0x37, 0x39, 0x29, 0x29, 0x20, 0x2f, 0x20, 0x28, 0x31, 0x2e, 0x30, + 0x66, 0x20, 0x2b, 0x20, 0x28, 0x76, 0x2e, 0x74, 0x6f, 0x6e, 0x65, 0x6d, 0x61, 0x70, 0x5f, 0x66, + 0x61, 0x63, 0x74, 0x6f, 0x72, 0x32, 0x20, 0x2a, 0x20, 0x76, 0x5f, 0x37, 0x39, 0x29, 0x29, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x2a, + 0x20, 0x76, 0x5f, 0x38, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, + 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x32, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, + 0x31, 0x34, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x34, 0x20, 0x2a, 0x20, 0x6d, 0x61, 0x74, + 0x33, 0x78, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x36, 0x36, 0x30, 0x34, 0x39, 0x35, 0x39, 0x39, 0x36, 0x34, 0x37, + 0x35, 0x32, 0x31, 0x39, 0x37, 0x32, 0x36, 0x35, 0x36, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x35, + 0x38, 0x37, 0x36, 0x35, 0x36, 0x30, 0x32, 0x31, 0x31, 0x31, 0x38, 0x31, 0x36, 0x34, 0x30, 0x36, + 0x32, 0x35, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x30, 0x37, 0x32, 0x38, 0x33, 0x39, 0x39, 0x39, + 0x37, 0x37, 0x30, 0x38, 0x37, 0x39, 0x37, 0x34, 0x35, 0x34, 0x38, 0x33, 0x66, 0x29, 0x2c, 0x20, + 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x2d, 0x30, 0x2e, 0x31, 0x32, 0x34, + 0x35, 0x34, 0x36, 0x39, 0x39, 0x37, 0x32, 0x34, 0x39, 0x31, 0x32, 0x36, 0x34, 0x33, 0x34, 0x33, + 0x33, 0x66, 0x2c, 0x20, 0x31, 0x2e, 0x31, 0x33, 0x32, 0x38, 0x39, 0x34, 0x39, 0x39, 0x32, 0x38, + 0x32, 0x38, 0x33, 0x36, 0x39, 0x31, 0x34, 0x30, 0x36, 0x32, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, + 0x30, 0x30, 0x38, 0x33, 0x34, 0x38, 0x30, 0x30, 0x30, 0x32, 0x33, 0x35, 0x38, 0x35, 0x35, 0x35, + 0x37, 0x39, 0x33, 0x38, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x28, 0x2d, 0x30, 0x2e, 0x30, 0x31, 0x38, 0x31, 0x35, 0x34, 0x30, 0x30, 0x30, 0x38, 0x36, + 0x33, 0x34, 0x33, 0x32, 0x38, 0x38, 0x34, 0x32, 0x32, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x31, + 0x30, 0x30, 0x35, 0x39, 0x37, 0x30, 0x30, 0x31, 0x36, 0x31, 0x32, 0x31, 0x38, 0x36, 0x34, 0x33, + 0x31, 0x38, 0x38, 0x66, 0x2c, 0x20, 0x31, 0x2e, 0x31, 0x31, 0x38, 0x37, 0x35, 0x31, 0x30, 0x34, + 0x39, 0x30, 0x34, 0x31, 0x37, 0x34, 0x38, 0x30, 0x34, 0x36, 0x38, 0x38, 0x66, 0x29, 0x29, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x38, 0x31, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, + 0x34, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x31, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, + 0x31, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, + 0x33, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x31, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x69, 0x6e, + 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, + 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x36, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x79, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x21, 0x28, 0x28, 0x76, 0x2e, 0x73, 0x63, 0x52, + 0x47, 0x42, 0x5f, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x2e, 0x30, + 0x66, 0x29, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x38, 0x32, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, + 0x36, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x38, 0x33, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, + 0x76, 0x5f, 0x38, 0x32, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x34, 0x30, 0x34, 0x34, 0x39, + 0x39, 0x39, 0x39, 0x32, 0x37, 0x32, 0x38, 0x32, 0x33, 0x33, 0x33, 0x33, 0x37, 0x34, 0x66, 0x29, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x38, 0x33, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x32, 0x20, + 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x37, 0x37, 0x33, 0x39, 0x39, 0x33, 0x38, 0x30, 0x35, 0x30, 0x35, + 0x30, 0x38, 0x34, 0x39, 0x39, 0x31, 0x34, 0x36, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x5f, 0x38, 0x33, 0x20, 0x3d, 0x20, 0x70, 0x6f, 0x77, 0x28, 0x28, 0x61, 0x62, 0x73, + 0x28, 0x28, 0x76, 0x5f, 0x38, 0x32, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, + 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, + 0x29, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x39, 0x34, 0x37, 0x38, 0x36, 0x37, 0x33, 0x33, 0x33, + 0x38, 0x38, 0x39, 0x30, 0x30, 0x37, 0x35, 0x36, 0x38, 0x33, 0x36, 0x66, 0x29, 0x2c, 0x20, 0x32, + 0x2e, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x39, 0x35, 0x33, 0x36, 0x37, 0x34, 0x33, 0x31, + 0x36, 0x34, 0x30, 0x36, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x36, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x33, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, + 0x74, 0x20, 0x76, 0x5f, 0x38, 0x34, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x36, 0x2e, 0x79, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, + 0x20, 0x76, 0x5f, 0x38, 0x35, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x34, + 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x34, 0x30, 0x34, 0x34, 0x39, 0x39, 0x39, 0x39, 0x32, + 0x37, 0x32, 0x38, 0x32, 0x33, 0x33, 0x33, 0x33, 0x37, 0x34, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x5f, 0x38, 0x35, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x34, 0x20, 0x2a, 0x20, 0x30, 0x2e, + 0x30, 0x37, 0x37, 0x33, 0x39, 0x39, 0x33, 0x38, 0x30, 0x35, 0x30, 0x35, 0x30, 0x38, 0x34, 0x39, + 0x39, 0x31, 0x34, 0x36, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x38, + 0x35, 0x20, 0x3d, 0x20, 0x70, 0x6f, 0x77, 0x28, 0x28, 0x61, 0x62, 0x73, 0x28, 0x28, 0x76, 0x5f, + 0x38, 0x34, 0x20, 0x2b, 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, + 0x37, 0x30, 0x31, 0x39, 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x29, 0x20, 0x2a, + 0x20, 0x30, 0x2e, 0x39, 0x34, 0x37, 0x38, 0x36, 0x37, 0x33, 0x33, 0x33, 0x38, 0x38, 0x39, 0x30, + 0x30, 0x37, 0x35, 0x36, 0x38, 0x33, 0x36, 0x66, 0x29, 0x2c, 0x20, 0x32, 0x2e, 0x34, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x39, 0x35, 0x33, 0x36, 0x37, 0x34, 0x33, 0x31, 0x36, 0x34, 0x30, 0x36, + 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x5f, 0x31, 0x36, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x35, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, + 0x38, 0x36, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x36, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x38, + 0x37, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x36, 0x20, 0x3c, 0x3d, 0x20, + 0x30, 0x2e, 0x30, 0x34, 0x30, 0x34, 0x34, 0x39, 0x39, 0x39, 0x39, 0x32, 0x37, 0x32, 0x38, 0x32, + 0x33, 0x33, 0x33, 0x33, 0x37, 0x34, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x38, 0x37, 0x20, + 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x36, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x30, 0x37, 0x37, 0x33, + 0x39, 0x39, 0x33, 0x38, 0x30, 0x35, 0x30, 0x35, 0x30, 0x38, 0x34, 0x39, 0x39, 0x31, 0x34, 0x36, + 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x38, 0x37, 0x20, 0x3d, 0x20, + 0x70, 0x6f, 0x77, 0x28, 0x28, 0x61, 0x62, 0x73, 0x28, 0x28, 0x76, 0x5f, 0x38, 0x36, 0x20, 0x2b, + 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, + 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x30, 0x2e, 0x39, + 0x34, 0x37, 0x38, 0x36, 0x37, 0x33, 0x33, 0x33, 0x38, 0x38, 0x39, 0x30, 0x30, 0x37, 0x35, 0x36, + 0x38, 0x33, 0x36, 0x66, 0x29, 0x2c, 0x20, 0x32, 0x2e, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x39, 0x35, 0x33, 0x36, 0x37, 0x34, 0x33, 0x31, 0x36, 0x34, 0x30, 0x36, 0x32, 0x66, 0x29, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x36, 0x2e, + 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x37, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, + 0x76, 0x5f, 0x38, 0x38, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x36, 0x20, 0x2a, 0x20, 0x76, + 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x5f, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x78, 0x20, 0x3d, 0x20, + 0x76, 0x5f, 0x38, 0x38, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x38, 0x2e, 0x79, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x7a, 0x20, + 0x3d, 0x20, 0x76, 0x5f, 0x38, 0x38, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, + 0x77, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, + 0x20, 0x28, 0x76, 0x2e, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x20, 0x3d, + 0x3d, 0x20, 0x32, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x36, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x79, + 0x7a, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x5f, 0x73, 0x63, 0x61, 0x6c, + 0x65, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, + 0x76, 0x2e, 0x73, 0x63, 0x52, 0x47, 0x42, 0x5f, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x3d, + 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x38, 0x39, 0x20, 0x3d, + 0x20, 0x76, 0x5f, 0x36, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x39, 0x30, 0x3a, 0x20, 0x66, 0x33, + 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, + 0x66, 0x20, 0x28, 0x76, 0x5f, 0x38, 0x39, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, + 0x31, 0x33, 0x30, 0x38, 0x30, 0x30, 0x30, 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, 0x39, 0x30, 0x32, + 0x38, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, + 0x38, 0x39, 0x20, 0x2a, 0x20, 0x31, 0x32, 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, + 0x36, 0x32, 0x39, 0x33, 0x39, 0x34, 0x35, 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, + 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x70, 0x6f, 0x77, + 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x38, 0x39, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x34, 0x31, + 0x36, 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, 0x32, 0x30, 0x34, + 0x31, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x34, + 0x37, 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, 0x29, 0x20, 0x2d, + 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, + 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x36, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, + 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, 0x31, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x36, 0x2e, 0x79, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, + 0x20, 0x76, 0x5f, 0x39, 0x32, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x39, 0x31, + 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x31, 0x33, 0x30, 0x38, 0x30, 0x30, 0x30, + 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, 0x39, 0x30, 0x32, 0x38, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x5f, 0x39, 0x32, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x39, 0x31, 0x20, 0x2a, 0x20, 0x31, 0x32, + 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x36, 0x32, 0x39, 0x33, 0x39, 0x34, 0x35, + 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, + 0x32, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x70, 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, + 0x39, 0x31, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x34, 0x31, 0x36, 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, + 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, 0x32, 0x30, 0x34, 0x31, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, + 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x34, 0x37, 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, + 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, 0x29, 0x20, 0x2d, 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, + 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, + 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, + 0x36, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, 0x33, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x36, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x39, 0x34, 0x3a, 0x20, + 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x39, 0x33, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, + 0x30, 0x33, 0x31, 0x33, 0x30, 0x38, 0x30, 0x30, 0x30, 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, 0x39, + 0x30, 0x32, 0x38, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x34, 0x20, 0x3d, 0x20, 0x28, + 0x76, 0x5f, 0x39, 0x33, 0x20, 0x2a, 0x20, 0x31, 0x32, 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x37, 0x36, 0x32, 0x39, 0x33, 0x39, 0x34, 0x35, 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, + 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x34, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x70, + 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x39, 0x33, 0x29, 0x2c, 0x20, 0x30, 0x2e, + 0x34, 0x31, 0x36, 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, 0x32, + 0x30, 0x34, 0x31, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, + 0x39, 0x34, 0x37, 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, 0x29, + 0x20, 0x2d, 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, + 0x31, 0x39, 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x36, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, + 0x5f, 0x39, 0x34, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x76, 0x5f, 0x36, 0x20, 0x3d, 0x20, 0x63, 0x6c, 0x61, 0x6d, 0x70, 0x28, 0x76, 0x5f, 0x36, + 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x29, 0x2c, 0x20, 0x76, + 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x29, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, 0x35, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x36, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x35, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, + 0x35, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, + 0x35, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x35, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x76, + 0x5f, 0x31, 0x33, 0x2e, 0x77, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, + 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x2e, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74, 0x79, + 0x70, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x33, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, 0x36, 0x20, 0x3d, + 0x20, 0x28, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x2a, 0x20, 0x6d, 0x61, 0x74, + 0x33, 0x78, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x36, 0x36, 0x30, 0x34, 0x39, 0x35, 0x39, 0x39, 0x36, 0x34, 0x37, + 0x35, 0x32, 0x31, 0x39, 0x37, 0x32, 0x36, 0x35, 0x36, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x35, + 0x38, 0x37, 0x36, 0x35, 0x36, 0x30, 0x32, 0x31, 0x31, 0x31, 0x38, 0x31, 0x36, 0x34, 0x30, 0x36, + 0x32, 0x35, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x30, 0x37, 0x32, 0x38, 0x33, 0x39, 0x39, 0x39, + 0x37, 0x37, 0x30, 0x38, 0x37, 0x39, 0x37, 0x34, 0x35, 0x34, 0x38, 0x33, 0x66, 0x29, 0x2c, 0x20, + 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x2d, 0x30, 0x2e, 0x31, 0x32, 0x34, + 0x35, 0x34, 0x36, 0x39, 0x39, 0x37, 0x32, 0x34, 0x39, 0x31, 0x32, 0x36, 0x34, 0x33, 0x34, 0x33, + 0x33, 0x66, 0x2c, 0x20, 0x31, 0x2e, 0x31, 0x33, 0x32, 0x38, 0x39, 0x34, 0x39, 0x39, 0x32, 0x38, + 0x32, 0x38, 0x33, 0x36, 0x39, 0x31, 0x34, 0x30, 0x36, 0x32, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, + 0x30, 0x30, 0x38, 0x33, 0x34, 0x38, 0x30, 0x30, 0x30, 0x32, 0x33, 0x35, 0x38, 0x35, 0x35, 0x35, + 0x37, 0x39, 0x33, 0x38, 0x66, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, + 0x3e, 0x28, 0x2d, 0x30, 0x2e, 0x30, 0x31, 0x38, 0x31, 0x35, 0x34, 0x30, 0x30, 0x30, 0x38, 0x36, + 0x33, 0x34, 0x33, 0x32, 0x38, 0x38, 0x34, 0x32, 0x32, 0x66, 0x2c, 0x20, 0x2d, 0x30, 0x2e, 0x31, + 0x30, 0x30, 0x35, 0x39, 0x37, 0x30, 0x30, 0x31, 0x36, 0x31, 0x32, 0x31, 0x38, 0x36, 0x34, 0x33, + 0x31, 0x38, 0x38, 0x66, 0x2c, 0x20, 0x31, 0x2e, 0x31, 0x31, 0x38, 0x37, 0x35, 0x31, 0x30, 0x34, + 0x39, 0x30, 0x34, 0x31, 0x37, 0x34, 0x38, 0x30, 0x34, 0x36, 0x38, 0x38, 0x66, 0x29, 0x29, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x78, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x36, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x36, + 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x33, + 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x39, 0x36, 0x2e, 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x35, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x33, + 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x5f, 0x73, + 0x63, 0x61, 0x6c, 0x65, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, + 0x66, 0x20, 0x28, 0x76, 0x2e, 0x73, 0x63, 0x52, 0x47, 0x42, 0x5f, 0x6f, 0x75, 0x74, 0x70, 0x75, + 0x74, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, + 0x37, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x35, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x39, 0x38, 0x3a, + 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, 0x39, 0x37, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, + 0x30, 0x30, 0x33, 0x31, 0x33, 0x30, 0x38, 0x30, 0x30, 0x30, 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, + 0x39, 0x30, 0x32, 0x38, 0x66, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x38, 0x20, 0x3d, 0x20, + 0x28, 0x76, 0x5f, 0x39, 0x37, 0x20, 0x2a, 0x20, 0x31, 0x32, 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x37, 0x36, 0x32, 0x39, 0x33, 0x39, 0x34, 0x35, 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, + 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x39, 0x38, 0x20, 0x3d, 0x20, 0x28, 0x28, + 0x70, 0x6f, 0x77, 0x28, 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x39, 0x37, 0x29, 0x2c, 0x20, 0x30, + 0x2e, 0x34, 0x31, 0x36, 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, + 0x32, 0x30, 0x34, 0x31, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, + 0x39, 0x39, 0x34, 0x37, 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, + 0x29, 0x20, 0x2d, 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, + 0x30, 0x31, 0x39, 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x35, 0x2e, 0x78, 0x20, 0x3d, 0x20, + 0x76, 0x5f, 0x39, 0x38, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x39, 0x39, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x35, + 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x61, 0x72, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x30, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, + 0x76, 0x5f, 0x39, 0x39, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x31, 0x33, 0x30, + 0x38, 0x30, 0x30, 0x30, 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, 0x39, 0x30, 0x32, 0x38, 0x66, 0x29, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x39, 0x39, + 0x20, 0x2a, 0x20, 0x31, 0x32, 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x36, 0x32, + 0x39, 0x33, 0x39, 0x34, 0x35, 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x30, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x70, 0x6f, 0x77, 0x28, + 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x39, 0x39, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x34, 0x31, 0x36, + 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, 0x32, 0x30, 0x34, 0x31, + 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x34, 0x37, + 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, 0x29, 0x20, 0x2d, 0x20, + 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, 0x37, + 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x35, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x30, + 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x31, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x35, 0x2e, 0x7a, + 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, + 0x72, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x32, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x3b, 0x0a, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x76, 0x5f, + 0x31, 0x30, 0x31, 0x20, 0x3c, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x30, 0x33, 0x31, 0x33, 0x30, 0x38, + 0x30, 0x30, 0x30, 0x39, 0x30, 0x37, 0x33, 0x30, 0x31, 0x39, 0x30, 0x32, 0x38, 0x66, 0x29, 0x20, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x32, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x30, 0x31, + 0x20, 0x2a, 0x20, 0x31, 0x32, 0x2e, 0x39, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x36, 0x32, + 0x39, 0x33, 0x39, 0x34, 0x35, 0x33, 0x31, 0x32, 0x35, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, + 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x32, 0x20, 0x3d, 0x20, 0x28, 0x28, 0x70, 0x6f, 0x77, 0x28, + 0x61, 0x62, 0x73, 0x28, 0x76, 0x5f, 0x31, 0x30, 0x31, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x34, 0x31, + 0x36, 0x36, 0x36, 0x36, 0x36, 0x35, 0x36, 0x37, 0x33, 0x32, 0x35, 0x35, 0x39, 0x32, 0x30, 0x34, + 0x31, 0x66, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x34, + 0x37, 0x35, 0x34, 0x37, 0x39, 0x31, 0x32, 0x35, 0x39, 0x37, 0x36, 0x36, 0x66, 0x29, 0x20, 0x2d, + 0x20, 0x30, 0x2e, 0x30, 0x35, 0x34, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x37, 0x30, 0x31, 0x39, + 0x37, 0x36, 0x37, 0x37, 0x36, 0x31, 0x32, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x35, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, + 0x30, 0x32, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x35, 0x20, 0x3d, 0x20, 0x63, 0x6c, 0x61, 0x6d, 0x70, 0x28, 0x76, 0x5f, 0x35, 0x2c, + 0x20, 0x76, 0x65, 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x29, 0x2c, 0x20, 0x76, 0x65, + 0x63, 0x33, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x29, 0x3b, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x33, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x35, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, + 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x33, 0x2e, 0x78, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x79, 0x20, 0x3d, 0x20, 0x76, 0x5f, + 0x31, 0x30, 0x33, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, + 0x5f, 0x31, 0x35, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x33, 0x2e, 0x7a, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x2e, 0x77, 0x20, + 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x2e, 0x77, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x20, + 0x65, 0x6c, 0x73, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x34, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x33, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x37, 0x20, 0x3d, 0x20, + 0x76, 0x5f, 0x31, 0x30, 0x34, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, + 0x65, 0x74, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x35, 0x20, 0x3d, 0x20, 0x28, 0x76, 0x5f, 0x31, 0x30, + 0x34, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x2a, 0x20, 0x76, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x5f, + 0x73, 0x63, 0x61, 0x6c, 0x65, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x5f, 0x34, 0x2e, 0x78, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x35, 0x2e, 0x78, 0x3b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x34, 0x2e, 0x79, 0x20, 0x3d, + 0x20, 0x76, 0x5f, 0x31, 0x30, 0x35, 0x2e, 0x79, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x76, 0x5f, 0x34, 0x2e, 0x7a, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x35, 0x2e, + 0x7a, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x34, 0x2e, 0x77, + 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x31, 0x37, 0x2e, 0x77, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x35, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x34, 0x3b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x5f, 0x31, 0x20, 0x3d, 0x20, 0x28, + 0x76, 0x5f, 0x31, 0x35, 0x20, 0x2a, 0x20, 0x76, 0x5f, 0x32, 0x29, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, + 0x40, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, 0x69, + 0x6e, 0x28, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, + 0x76, 0x5f, 0x31, 0x30, 0x36, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x2c, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, + 0x76, 0x5f, 0x31, 0x30, 0x37, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, + 0x75, 0x29, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x20, 0x7b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x28, 0x76, 0x5f, + 0x31, 0x30, 0x36, 0x2c, 0x20, 0x76, 0x5f, 0x31, 0x30, 0x37, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x76, 0x5f, 0x31, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int texture_advanced_frag_wgsl_len = 13791; diff --git a/src/render/gpu/shaders/texture_rgb.frag.wgsl b/src/render/gpu/shaders/texture_rgb.frag.wgsl new file mode 100644 index 0000000000..7c9cccce3f --- /dev/null +++ b/src/render/gpu/shaders/texture_rgb.frag.wgsl @@ -0,0 +1,29 @@ +struct Constants { + colorScale: f32, +}; + +struct PSInput { + @location(0u) vColor: vec4, + @location(1u) vUv: vec2, +}; + +struct PSOutput { + @location(0u) oColor: vec4, +}; + +@group(2u) @binding(0u) var uTexture: texture_2d; +@group(2u) @binding(1u) var uSampler: sampler; + +@group(3u) @binding(0u) var constants: Constants; + +fn getOutputColor(rgba: vec4, colorScale: f32) -> vec4 { + return vec4(rgba.rgb * colorScale, rgba.a); +} + +@fragment +fn main(input: PSInput) -> PSOutput { + var output: PSOutput; + output.oColor = getOutputColor(vec4(textureSample(uTexture, uSampler, input.vUv).rgb, 1.0f), constants.colorScale) * input.vColor; + + return output; +} diff --git a/src/render/gpu/shaders/texture_rgb.frag.wgsl.h b/src/render/gpu/shaders/texture_rgb.frag.wgsl.h new file mode 100644 index 0000000000..88a94d7604 --- /dev/null +++ b/src/render/gpu/shaders/texture_rgb.frag.wgsl.h @@ -0,0 +1,49 @@ +static const unsigned char texture_rgb_frag_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, + 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, + 0x63, 0x74, 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, + 0x31, 0x75, 0x29, 0x20, 0x76, 0x55, 0x76, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x50, + 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, + 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x6f, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, + 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, + 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x75, + 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x5f, 0x32, 0x64, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, + 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x31, 0x75, + 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x75, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3a, 0x20, + 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, + 0x28, 0x33, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, + 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x63, + 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x3a, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, + 0x6e, 0x74, 0x73, 0x3b, 0x0a, 0x0a, 0x66, 0x6e, 0x20, 0x67, 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, + 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x72, 0x67, 0x62, 0x61, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, + 0x61, 0x6c, 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x76, 0x65, 0x63, + 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, + 0x75, 0x72, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x72, 0x67, + 0x62, 0x61, 0x2e, 0x72, 0x67, 0x62, 0x20, 0x2a, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, + 0x61, 0x6c, 0x65, 0x2c, 0x20, 0x72, 0x67, 0x62, 0x61, 0x2e, 0x61, 0x29, 0x3b, 0x0a, 0x7d, 0x0a, + 0x0a, 0x40, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, + 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, + 0x74, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x50, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, + 0x20, 0x50, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, + 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x6f, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x67, + 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x75, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, + 0x75, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x2c, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, + 0x76, 0x55, 0x76, 0x29, 0x2e, 0x72, 0x67, 0x62, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x2c, + 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, + 0x53, 0x63, 0x61, 0x6c, 0x65, 0x29, 0x20, 0x2a, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x76, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, + 0x72, 0x6e, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int texture_rgb_frag_wgsl_len = 717; diff --git a/src/render/gpu/shaders/texture_rgba.frag.wgsl b/src/render/gpu/shaders/texture_rgba.frag.wgsl new file mode 100644 index 0000000000..63bd8f1c57 --- /dev/null +++ b/src/render/gpu/shaders/texture_rgba.frag.wgsl @@ -0,0 +1,29 @@ +struct Constants { + colorScale: f32, +}; + +struct PSInput { + @location(0u) vColor: vec4, + @location(1u) vUv: vec2, +}; + +struct PSOutput { + @location(0u) oColor: vec4, +}; + +@group(2u) @binding(0u) var uTexture: texture_2d; +@group(2u) @binding(1u) var uSampler: sampler; + +@group(3u) @binding(0u) var constants: Constants; + +fn getOutputColor(rgba: vec4, colorScale: f32) -> vec4 { + return vec4(rgba.rgb * colorScale, rgba.a); +} + +@fragment +fn main(input: PSInput) -> PSOutput { + var output: PSOutput; + output.oColor = getOutputColor(textureSample(uTexture, uSampler, input.vUv), constants.colorScale) * input.vColor; + + return output; +} diff --git a/src/render/gpu/shaders/texture_rgba.frag.wgsl.h b/src/render/gpu/shaders/texture_rgba.frag.wgsl.h new file mode 100644 index 0000000000..4d2757a662 --- /dev/null +++ b/src/render/gpu/shaders/texture_rgba.frag.wgsl.h @@ -0,0 +1,48 @@ +static const unsigned char texture_rgba_frag_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, + 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, + 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, + 0x63, 0x74, 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, + 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, + 0x31, 0x75, 0x29, 0x20, 0x76, 0x55, 0x76, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x50, + 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, + 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x6f, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, + 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, + 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x75, + 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, + 0x5f, 0x32, 0x64, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x3b, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, + 0x28, 0x32, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x31, 0x75, + 0x29, 0x20, 0x76, 0x61, 0x72, 0x20, 0x75, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3a, 0x20, + 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x3b, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, + 0x28, 0x33, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, + 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x63, + 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x73, 0x3a, 0x20, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, + 0x6e, 0x74, 0x73, 0x3b, 0x0a, 0x0a, 0x66, 0x6e, 0x20, 0x67, 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, + 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x72, 0x67, 0x62, 0x61, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, + 0x61, 0x6c, 0x65, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x76, 0x65, 0x63, + 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, + 0x75, 0x72, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x72, 0x67, + 0x62, 0x61, 0x2e, 0x72, 0x67, 0x62, 0x20, 0x2a, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, + 0x61, 0x6c, 0x65, 0x2c, 0x20, 0x72, 0x67, 0x62, 0x61, 0x2e, 0x61, 0x29, 0x3b, 0x0a, 0x7d, 0x0a, + 0x0a, 0x40, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, + 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x50, 0x53, 0x49, 0x6e, 0x70, 0x75, + 0x74, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x50, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, + 0x20, 0x50, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, + 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x6f, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x67, + 0x65, 0x74, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x74, 0x65, + 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x28, 0x75, 0x54, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x75, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x2c, 0x20, + 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x76, 0x55, 0x76, 0x29, 0x2c, 0x20, 0x63, 0x6f, 0x6e, 0x73, + 0x74, 0x61, 0x6e, 0x74, 0x73, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x53, 0x63, 0x61, 0x6c, 0x65, + 0x29, 0x20, 0x2a, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, + 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x75, + 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int texture_rgba_frag_wgsl_len = 696; diff --git a/src/render/gpu/shaders/tri_color.vert.wgsl b/src/render/gpu/shaders/tri_color.vert.wgsl new file mode 100644 index 0000000000..49dd8d00b9 --- /dev/null +++ b/src/render/gpu/shaders/tri_color.vert.wgsl @@ -0,0 +1,27 @@ +struct Context { + mvp: mat4x4, // Model-View-Projection matrix +}; + +struct VSInput { + @location(0u) aPosition: vec2, + @location(1u) aColor: vec4, + @location(2u) aUv: vec2, +}; + +struct VSOutput { + @location(0u) vColor: vec4, + @location(1u) vUv: vec2, + @builtin(position) glPosition: vec4, +} + +@group(1u) @binding(0u) var context: Context; + +@vertex +fn main(input: VSInput) -> VSOutput { + var output: VSOutput; + output.glPosition = context.mvp * vec4(input.aPosition, 0.0f, 1.0f); + output.vColor = input.aColor; + output.vUv = input.aUv; + + return output; +} diff --git a/src/render/gpu/shaders/tri_color.vert.wgsl.h b/src/render/gpu/shaders/tri_color.vert.wgsl.h new file mode 100644 index 0000000000..384c80abe0 --- /dev/null +++ b/src/render/gpu/shaders/tri_color.vert.wgsl.h @@ -0,0 +1,44 @@ +static const unsigned char tri_color_vert_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6d, 0x76, 0x70, 0x3a, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x78, 0x34, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x2f, 0x2f, 0x20, 0x4d, 0x6f, 0x64, 0x65, 0x6c, 0x2d, + 0x56, 0x69, 0x65, 0x77, 0x2d, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, + 0x6d, 0x61, 0x74, 0x72, 0x69, 0x78, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, + 0x74, 0x20, 0x56, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x61, 0x50, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, + 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x32, 0x75, 0x29, 0x20, 0x61, 0x55, 0x76, 0x3a, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, + 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, + 0x75, 0x29, 0x20, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, 0x76, 0x55, 0x76, 0x3a, 0x20, 0x76, 0x65, 0x63, + 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x62, 0x75, 0x69, + 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x29, 0x20, 0x67, + 0x6c, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, + 0x31, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, + 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x63, 0x6f, + 0x6e, 0x74, 0x65, 0x78, 0x74, 0x3a, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x3b, 0x0a, + 0x0a, 0x40, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, 0x69, 0x6e, + 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x29, + 0x20, 0x2d, 0x3e, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, + 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, + 0x70, 0x75, 0x74, 0x2e, 0x67, 0x6c, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, + 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, 0x6d, 0x76, 0x70, 0x20, 0x2a, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x61, + 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x2c, 0x20, + 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, + 0x74, 0x2e, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, + 0x2e, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, + 0x70, 0x75, 0x74, 0x2e, 0x76, 0x55, 0x76, 0x20, 0x3d, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, + 0x61, 0x55, 0x76, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, + 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int tri_color_vert_wgsl_len = 635; diff --git a/src/render/gpu/shaders/tri_texture.vert.wgsl b/src/render/gpu/shaders/tri_texture.vert.wgsl new file mode 100644 index 0000000000..49dd8d00b9 --- /dev/null +++ b/src/render/gpu/shaders/tri_texture.vert.wgsl @@ -0,0 +1,27 @@ +struct Context { + mvp: mat4x4, // Model-View-Projection matrix +}; + +struct VSInput { + @location(0u) aPosition: vec2, + @location(1u) aColor: vec4, + @location(2u) aUv: vec2, +}; + +struct VSOutput { + @location(0u) vColor: vec4, + @location(1u) vUv: vec2, + @builtin(position) glPosition: vec4, +} + +@group(1u) @binding(0u) var context: Context; + +@vertex +fn main(input: VSInput) -> VSOutput { + var output: VSOutput; + output.glPosition = context.mvp * vec4(input.aPosition, 0.0f, 1.0f); + output.vColor = input.aColor; + output.vUv = input.aUv; + + return output; +} diff --git a/src/render/gpu/shaders/tri_texture.vert.wgsl.h b/src/render/gpu/shaders/tri_texture.vert.wgsl.h new file mode 100644 index 0000000000..5d58dd8265 --- /dev/null +++ b/src/render/gpu/shaders/tri_texture.vert.wgsl.h @@ -0,0 +1,44 @@ +static const unsigned char tri_texture_vert_wgsl[] = { + 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6d, 0x76, 0x70, 0x3a, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x78, 0x34, + 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x20, 0x2f, 0x2f, 0x20, 0x4d, 0x6f, 0x64, 0x65, 0x6c, 0x2d, + 0x56, 0x69, 0x65, 0x77, 0x2d, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, + 0x6d, 0x61, 0x74, 0x72, 0x69, 0x78, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, + 0x74, 0x20, 0x56, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x75, 0x29, 0x20, 0x61, 0x50, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, + 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, + 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, + 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x32, 0x75, 0x29, 0x20, 0x61, 0x55, 0x76, 0x3a, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x73, + 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, + 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, + 0x75, 0x29, 0x20, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x28, 0x31, 0x75, 0x29, 0x20, 0x76, 0x55, 0x76, 0x3a, 0x20, 0x76, 0x65, 0x63, + 0x32, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x62, 0x75, 0x69, + 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x29, 0x20, 0x67, + 0x6c, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x3c, + 0x66, 0x33, 0x32, 0x3e, 0x2c, 0x0a, 0x7d, 0x0a, 0x0a, 0x40, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x28, + 0x31, 0x75, 0x29, 0x20, 0x40, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x75, 0x29, + 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x63, 0x6f, + 0x6e, 0x74, 0x65, 0x78, 0x74, 0x3a, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x3b, 0x0a, + 0x0a, 0x40, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x0a, 0x66, 0x6e, 0x20, 0x6d, 0x61, 0x69, 0x6e, + 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x53, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x29, + 0x20, 0x2d, 0x3e, 0x20, 0x56, 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x7b, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, + 0x53, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, + 0x70, 0x75, 0x74, 0x2e, 0x67, 0x6c, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, + 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x2e, 0x6d, 0x76, 0x70, 0x20, 0x2a, 0x20, 0x76, + 0x65, 0x63, 0x34, 0x3c, 0x66, 0x33, 0x32, 0x3e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x61, + 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x2c, 0x20, + 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, + 0x74, 0x2e, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, + 0x2e, 0x61, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, + 0x70, 0x75, 0x74, 0x2e, 0x76, 0x55, 0x76, 0x20, 0x3d, 0x20, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, + 0x61, 0x55, 0x76, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, + 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0a, 0x7d, 0x0a +}; + +static const unsigned int tri_texture_vert_wgsl_len = 635; diff --git a/src/render/gpu/shaders/wgsl.h b/src/render/gpu/shaders/wgsl.h new file mode 100644 index 0000000000..156ed8f5a9 --- /dev/null +++ b/src/render/gpu/shaders/wgsl.h @@ -0,0 +1,7 @@ +#include "color.frag.wgsl.h" +#include "linepoint.vert.wgsl.h" +#include "texture_advanced.frag.wgsl.h" +#include "texture_rgb.frag.wgsl.h" +#include "texture_rgba.frag.wgsl.h" +#include "tri_color.vert.wgsl.h" +#include "tri_texture.vert.wgsl.h" diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h index 393705a636..92a759127f 100644 --- a/src/video/SDL_sysvideo.h +++ b/src/video/SDL_sysvideo.h @@ -24,6 +24,7 @@ #define SDL_sysvideo_h_ #include +#include #include "SDL_surface_c.h" @@ -371,6 +372,13 @@ struct SDL_VideoDevice void (*Metal_DestroyView)(SDL_VideoDevice *_this, SDL_MetalView view); void *(*Metal_GetLayer)(SDL_VideoDevice *_this, SDL_MetalView view); + /* * * */ + /* + * Experimental WebGPU support + */ + bool (*WGPU_CreateSurface)(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); + void (*WGPU_DestroySurface)(WGPUSurface surface); + /* * * */ /* * Event manager functions diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 48faa86c3b..250d0339ea 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -6646,6 +6646,24 @@ void *SDL_Metal_GetLayer(SDL_MetalView view) } } +bool SDL_WGPU_CreateSurface(SDL_Window *window, WGPUInstance instance, WGPUSurface *surface) +{ + if (_this && _this->WGPU_CreateSurface) { + return _this->WGPU_CreateSurface(_this, window, instance, surface); + } else { + return SDL_SetError("WebGPU is not supported."); + } +} + +void SDL_WGPU_DestroySurface(WGPUSurface surface) +{ + if (_this && _this->WGPU_DestroySurface) { + _this->WGPU_DestroySurface(surface); + } else { + SDL_SetError("WebGPU is not supported."); + } +} + #if defined(SDL_VIDEO_DRIVER_X11) || defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_EMSCRIPTEN) const char *SDL_GetCSSCursorName(SDL_SystemCursor id, const char **fallback_name) { diff --git a/src/video/SDL_wgpu_defs.h b/src/video/SDL_wgpu_defs.h new file mode 100644 index 0000000000..11d8d23fe7 --- /dev/null +++ b/src/video/SDL_wgpu_defs.h @@ -0,0 +1,125 @@ +// BSD 3-Clause License +// +// Copyright (c) 2019, "WebGPU native" developers +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// TODO: We already have the entire webgpu header in the GPU backend, we should just use that instead of this. + +#include "SDL_internal.h" + +// define to prevent C++ name mangling on extern functions +#define WGPU_EXTERN extern "C" + +#ifndef WEBGPU_H_ +#define NO_WEBGPU_HEADER +#endif + +#ifdef NO_WEBGPU_HEADER + +#if !defined(WGPU_FUNCTION_ATTRIBUTE) +#define WGPU_FUNCTION_ATTRIBUTE +#endif + +#if !defined(WGPU_STRLEN) +#define WGPU_STRLEN (SIZE_MAX) +#endif + +typedef enum WGPUSType +{ + WGPUSType_ShaderSourceSPIRV = 0x00000001, + WGPUSType_ShaderSourceWGSL = 0x00000002, + WGPUSType_RenderPassMaxDrawCount = 0x00000003, + WGPUSType_SurfaceSourceMetalLayer = 0x00000004, + WGPUSType_SurfaceSourceWindowsHWND = 0x00000005, + WGPUSType_SurfaceSourceXlibWindow = 0x00000006, + WGPUSType_SurfaceSourceWaylandSurface = 0x00000007, + WGPUSType_SurfaceSourceAndroidNativeWindow = 0x00000008, + WGPUSType_SurfaceSourceXCBWindow = 0x00000009, + WGPUSType_SurfaceColorManagement = 0x0000000A, + WGPUSType_RequestAdapterWebXROptions = 0x0000000B, + WGPUSType_TextureComponentSwizzleDescriptor = 0x0000000C, + WGPUSType_ExternalTextureBindingLayout = 0x0000000D, + WGPUSType_ExternalTextureBindingEntry = 0x0000000E, + WGPUSType_CompatibilityModeLimits = 0x0000000F, + WGPUSType_TextureBindingViewDimension = 0x00000010, + WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector = 0x00040000, + WGPUSType_DawnCompilationMessageUtf16 = 0x0005003F, + WGPUSType_Force32 = 0x7FFFFFFF +} WGPUSType; + +typedef struct WGPUChainedStruct +{ + struct WGPUChainedStruct *next; + WGPUSType sType; +} WGPUChainedStruct; + +typedef struct WGPUStringView +{ + char const *data; + size_t length; +} WGPUStringView; + +typedef struct WGPUSurfaceDescriptor +{ + WGPUChainedStruct *nextInChain; + WGPUStringView label; +} WGPUSurfaceDescriptor; + +typedef struct WGPUSurfaceSourceWaylandSurface +{ + WGPUChainedStruct chain; + void *display; + void *surface; +} WGPUSurfaceSourceWaylandSurface; + +typedef struct WGPUSurfaceSourceXlibWindow +{ + WGPUChainedStruct chain; + void *display; + uint64_t window; +} WGPUSurfaceSourceXlibWindow; + +typedef struct WGPUSurfaceSourceWindowsHWND +{ + WGPUChainedStruct chain; + void *hinstance; + void *hwnd; +} WGPUSurfaceSourceWindowsHWND; + +typedef struct WGPUEmscriptenSurfaceSourceCanvasHTMLSelector +{ + WGPUChainedStruct chain; + WGPUStringView selector; +} WGPUEmscriptenSurfaceSourceCanvasHTMLSelector; + +typedef WGPUSurface (*WGPUProcInstanceCreateSurface)(WGPUInstance instance, WGPUSurfaceDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE; + +WGPU_EXTERN WGPUSurface wgpuInstanceCreateSurface(WGPUInstance instance, WGPUSurfaceDescriptor const *descriptor) WGPU_FUNCTION_ATTRIBUTE; +WGPU_EXTERN void wgpuSurfaceRelease(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE; + +#endif diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c index cd979af8bb..2cc1ebaee9 100644 --- a/src/video/emscripten/SDL_emscriptenvideo.c +++ b/src/video/emscripten/SDL_emscriptenvideo.c @@ -32,6 +32,7 @@ #include "SDL_emscriptenframebuffer.h" #include "SDL_emscriptenevents.h" #include "SDL_emscriptenmouse.h" +#include "SDL_emscriptenwgpu.h" #define EMSCRIPTENVID_DRIVER_NAME "emscripten" @@ -187,6 +188,11 @@ static SDL_VideoDevice *Emscripten_CreateDevice(void) device->GL_SwapWindow = Emscripten_GLES_SwapWindow; device->GL_DestroyContext = Emscripten_GLES_DestroyContext; +#ifdef SDL_VIDEO_WEBGPU + device->WGPU_CreateSurface = Emscripten_WGPU_CreateSurface; + device->WGPU_DestroySurface = Emscripten_WGPU_DestroySurface; +#endif + device->free = Emscripten_DeleteDevice; Emscripten_ListenSystemTheme(); diff --git a/src/video/emscripten/SDL_emscriptenwgpu.h b/src/video/emscripten/SDL_emscriptenwgpu.h new file mode 100644 index 0000000000..91260ddc29 --- /dev/null +++ b/src/video/emscripten/SDL_emscriptenwgpu.h @@ -0,0 +1,24 @@ +#ifndef SDL_emscriptenwgpu_h_ +#define SDL_emscriptenwgpu_h_ + +#include + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_EMSCRIPTEN) + +// Dawn's a C++ lib so we'll have to do this to prevent any name mangling. +#ifdef __cplusplus +extern "C" { +#else +extern +#endif + +bool Emscripten_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); +void Emscripten_WGPU_DestroySurface(WGPUSurface surface); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif // SDL_emscriptenwgpu_h_ diff --git a/src/video/emscripten/SDL_emscriptenwgpu_dawn.cpp b/src/video/emscripten/SDL_emscriptenwgpu_dawn.cpp new file mode 100644 index 0000000000..904d7530d8 --- /dev/null +++ b/src/video/emscripten/SDL_emscriptenwgpu_dawn.cpp @@ -0,0 +1,45 @@ +#include "SDL_internal.h" + +// TODO: Does this even need to be a C++ file? + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_EMSCRIPTEN) + +#include "../SDL_sysvideo.h" +#include "../SDL_wgpu_defs.h" + +#include "SDL_emscriptenwgpu.h" + +bool Emscripten_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface) +{ + if (!surface) { + SDL_SetError("Surface pointer is NULL!"); + return false; + } + + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(window); + + const char *canvas = SDL_GetStringProperty(windowProperties, SDL_PROP_WINDOW_EMSCRIPTEN_CANVAS_ID_STRING, 0); + + WGPUSurfaceDescriptor desc; + WGPUEmscriptenSurfaceSourceCanvasHTMLSelector source; + + source.selector.data = canvas; + source.chain.sType = WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector; + source.chain.next = NULL; + + desc.label = (WGPUStringView){ NULL, WGPU_STRLEN }; + desc.nextInChain = &source.chain; + + *surface = wgpuInstanceCreateSurface(instance, &desc); + + if (!surface) { + SDL_SetError("Failed to create WebGPU surface!"); + return false; + } + + return true; +} + +void Emscripten_WGPU_DestroySurface(WGPUSurface surface) { wgpuSurfaceRelease(surface); } + +#endif diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c index 24a8d7c2b7..e3e26d3086 100644 --- a/src/video/wayland/SDL_waylandvideo.c +++ b/src/video/wayland/SDL_waylandvideo.c @@ -38,6 +38,7 @@ #include "SDL_waylandframebuffer.h" #include "SDL_waylandvulkan.h" +#include "SDL_waylandwgpu.h" #include "SDL_waylandwindow.h" #include @@ -778,6 +779,11 @@ static SDL_VideoDevice *CreateWaylandDevice(bool require_preferred_protocols) device->Vulkan_GetPresentationSupport = Wayland_Vulkan_GetPresentationSupport; #endif +#ifdef SDL_VIDEO_WEBGPU + device->WGPU_CreateSurface = Wayland_WGPU_CreateSurface; + device->WGPU_DestroySurface = Wayland_WGPU_DestroySurface; +#endif + device->free = Wayland_DeleteDevice; device->device_caps = VIDEO_DEVICE_CAPS_MODE_SWITCHING_EMULATED | diff --git a/src/video/wayland/SDL_waylandwgpu.h b/src/video/wayland/SDL_waylandwgpu.h new file mode 100644 index 0000000000..d0f78ef020 --- /dev/null +++ b/src/video/wayland/SDL_waylandwgpu.h @@ -0,0 +1,24 @@ +#ifndef SDL_waylandwgpu_h_ +#define SDL_waylandwgpu_h_ + +#include + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_WAYLAND) + +// Dawn's a C++ lib so we'll have to do this to prevent any name mangling. +#ifdef __cplusplus +extern "C" { +#else +extern +#endif + +bool Wayland_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); +void Wayland_WGPU_DestroySurface(WGPUSurface surface); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif // SDL_waylandwgpu_h_ diff --git a/src/video/wayland/SDL_waylandwgpu_dawn.cpp b/src/video/wayland/SDL_waylandwgpu_dawn.cpp new file mode 100644 index 0000000000..d55dffbe27 --- /dev/null +++ b/src/video/wayland/SDL_waylandwgpu_dawn.cpp @@ -0,0 +1,57 @@ +// NOTE: +// Dawn's a C++ library. +// So, if you want to use C++, you have to have a .cpp file. +// A couple changes though: +// C++ isn't a fan of forward declaring enums, so we can't use SDL_waylandvideo.h, +// but that's fine; we're not using anything that isn't in SDL_sysvideo.h. +// I also had to add a cast for SDL_GetPointerProperty to wl_display/surface. +// UPDATE (2026-08-10): Yeah, I'll be perfectly honest. I probably just screwed up the build process. + +#include "SDL_internal.h" + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_WAYLAND) + +#include "../SDL_sysvideo.h" +#include "../SDL_wgpu_defs.h" + +#include "SDL_waylandwgpu.h" + +bool +Wayland_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *_surface) +{ + if(!_surface){ + SDL_SetError("Surface pointer is NULL!"); + return false; + } + + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(window); + struct wl_display *display = (wl_display *)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, 0); + struct wl_surface *surface = (wl_surface *)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, 0); + + WGPUSurfaceDescriptor desc; + WGPUSurfaceSourceWaylandSurface source; + + source.chain.sType = WGPUSType_SurfaceSourceWaylandSurface; + source.chain.next = NULL; + source.surface = surface; + source.display = display; + + // TODO: I don't know the convention on what label the surface should have? + // I'll just leave it NULL for now. - TheStickmahn + desc.label = (WGPUStringView){ NULL, WGPU_STRLEN }; + desc.nextInChain = &source.chain; + + // Dawn doesn't support dynamically loading, so we're just always using wgpuInstanceCreateSurface. + *_surface = wgpuInstanceCreateSurface(instance, &desc); + + if(!_surface){ + SDL_SetError("Failed to create WebGPU surface!"); + return false; + } + + return true; +} + +void Wayland_WGPU_DestroySurface(WGPUSurface surface) { wgpuSurfaceRelease(surface); } + +#endif diff --git a/src/video/windows/SDL_windowsvideo.c b/src/video/windows/SDL_windowsvideo.c index 09d4132c40..b8170198ff 100644 --- a/src/video/windows/SDL_windowsvideo.c +++ b/src/video/windows/SDL_windowsvideo.c @@ -35,6 +35,7 @@ #include "SDL_windowsmessagebox.h" #include "SDL_windowsrawinput.h" #include "SDL_windowsvulkan.h" +#include "SDL_windowswgpu.h" #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) #include @@ -425,6 +426,10 @@ static SDL_VideoDevice *WIN_CreateDevice(void) device->Vulkan_DestroySurface = WIN_Vulkan_DestroySurface; device->Vulkan_GetPresentationSupport = WIN_Vulkan_GetPresentationSupport; #endif +#ifdef SDL_VIDEO_WEBGPU + device->WGPU_CreateSurface = WIN_WGPU_CreateSurface; + device->WGPU_DestroySurface = WIN_WGPU_DestroySurface; +#endif #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) device->StartTextInput = WIN_StartTextInput; diff --git a/src/video/windows/SDL_windowswgpu.h b/src/video/windows/SDL_windowswgpu.h new file mode 100644 index 0000000000..4761680c50 --- /dev/null +++ b/src/video/windows/SDL_windowswgpu.h @@ -0,0 +1,24 @@ +#ifndef SDL_windowswgpu_h_ +#define SDL_windowswgpu_h_ + +#include + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_WINDOWS) + +// Dawn's a C++ lib so we'll have to do this to prevent any name mangling. +#ifdef __cplusplus +extern "C" { +#else +extern +#endif + +bool WIN_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); +void WIN_WGPU_DestroySurface(WGPUSurface surface); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif // SDL_windowswgpu_h_ diff --git a/src/video/windows/SDL_windowswgpu_dawn.cpp b/src/video/windows/SDL_windowswgpu_dawn.cpp new file mode 100644 index 0000000000..0b6969043e --- /dev/null +++ b/src/video/windows/SDL_windowswgpu_dawn.cpp @@ -0,0 +1,39 @@ +#include "SDL_internal.h" + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_WINDOWS) +#include "SDL_windowsvideo.h" + +// We have to add some random stuff here to appease Dawn. +// I don't know why, but we have to. +#if defined(_DEBUG) +#pragma comment(lib, "dxguid.lib") +#pragma comment(lib, "OneCore") +#endif + +#include "../SDL_wgpu_defs.h" +#include "SDL_windowswgpu.h" + +bool WIN_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface) +{ + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(window); + + void *hwnd = SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WIN32_HWND_POINTER, 0); + void *hinstance = SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, 0); + + WGPUSurfaceDescriptor desc; + WGPUSurfaceSourceWindowsHWND source; + + source.hwnd = hwnd; + source.hinstance = hinstance; + source.chain.sType = WGPUSType_SurfaceSourceWindowsHWND; + source.chain.next = NULL; + + desc.label = { NULL, WGPU_STRLEN }; + desc.nextInChain = &source.chain; + + return wgpuInstanceCreateSurface(instance, &desc); +} + +void WIN_WGPU_DestroySurface(WGPUSurface surface) { wgpuSurfaceRelease(surface); } + +#endif diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index 22e62574d7..2ddd7a51f5 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -35,6 +35,7 @@ #include "SDL_x11pen.h" #include "SDL_x11touch.h" #include "SDL_x11video.h" +#include "SDL_x11wgpu.h" #include "SDL_x11xfixes.h" #include "SDL_x11xinput2.h" #include "SDL_x11messagebox.h" @@ -270,6 +271,11 @@ static SDL_VideoDevice *X11_CreateDevice(void) device->Vulkan_GetPresentationSupport = X11_Vulkan_GetPresentationSupport; #endif +#ifdef SDL_VIDEO_WEBGPU + device->WGPU_CreateSurface = X11_WGPU_CreateSurface; + device->WGPU_DestroySurface = X11_WGPU_DestroySurface; +#endif + #ifdef SDL_USE_LIBDBUS if (SDL_SystemTheme_Init()) device->system_theme = SDL_SystemTheme_Get(); diff --git a/src/video/x11/SDL_x11wgpu.h b/src/video/x11/SDL_x11wgpu.h new file mode 100644 index 0000000000..f249badcb8 --- /dev/null +++ b/src/video/x11/SDL_x11wgpu.h @@ -0,0 +1,23 @@ +#ifndef SDL_x11wgpu_h_ +#define SDL_x11wgpu_h_ + +#include + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_X11) + +#ifdef __cplusplus +extern "C" { +#else +extern +#endif + +bool X11_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface); +void X11_WGPU_DestroySurface(WGPUSurface surface); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif // SDL_x11wgpu_h_ diff --git a/src/video/x11/SDL_x11wgpu_dawn.cpp b/src/video/x11/SDL_x11wgpu_dawn.cpp new file mode 100644 index 0000000000..682bdd985e --- /dev/null +++ b/src/video/x11/SDL_x11wgpu_dawn.cpp @@ -0,0 +1,49 @@ +// NOTE: We had to remove SDL_x11video.h since that uses the register keyword which was removed in C++17. +// That means we can't use Display, but that's not an issue since we're using a pointer to Display, which is a struct. +// We'll just make it a generic struct pointer. + +#include "SDL_internal.h" + +#if defined(SDL_VIDEO_WEBGPU) && defined(SDL_VIDEO_DRIVER_X11) + +#include "../SDL_sysvideo.h" +#include "../SDL_wgpu_defs.h" + +#include "SDL_x11wgpu.h" + +bool X11_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance, WGPUSurface *surface) +{ + if (!surface) { + SDL_SetError("Surface pointer is NULL!"); + return false; + } + + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(window); + + struct Display *x11_display = (Display *)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, 0); + int x11_window = SDL_GetNumberProperty(windowProperties, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); + + WGPUSurfaceDescriptor desc; + WGPUSurfaceSourceXlibWindow source; + + source.window = x11_window; + source.display = x11_display; + source.chain.sType = WGPUSType_SurfaceSourceXlibWindow; + source.chain.next = NULL; + + desc.label = (WGPUStringView){ NULL, WGPU_STRLEN }; + desc.nextInChain = &source.chain; + + *surface = wgpuInstanceCreateSurface(instance, &desc); + + if(!surface){ + SDL_SetError("Failed to create WebGPU surface!"); + return false; + } + + return true; +} + +void X11_WGPU_DestroySurface(WGPUSurface surface) { wgpuSurfaceRelease(surface); } + +#endif diff --git a/test/testsymbols.c b/test/testsymbols.c index c188547ea9..419fffbebb 100644 --- a/test/testsymbols.c +++ b/test/testsymbols.c @@ -17,6 +17,7 @@ #include #include #include +#include #if !defined(SDL_PLATFORM_ANDROID) extern SDL_DECLSPEC void SDLCALL SDL_GetAndroidActivity(void);