Skip to content

An extension for the GLFW library for using WebGPU native.

Notifications You must be signed in to change notification settings

JackieMaHatesDogEyes/glfw3webgpu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMake Badge

GLFW WebGPU Extension

This is an extension for the great GLFW library for using it with WebGPU native. It was written as part of the Learn WebGPU for native C++ tutorial series.

Table of Contents

Overview

This extension simply provides the following function:

WGPUSurface glfwGetWGPUSurface(WGPUInstance instance, GLFWwindow* window);

Given a GLFW window, glfwGetWGPUSurface returns a WebGPU surface that corresponds to the window's back-end. This is a process that is highly platform-specific, which is why I believe it belongs to GLFW.

Usage

Your project must link to an implementation of WebGPU (providing webgpu.h) and of course to GLFW. Then:

Option A If you use CMake, you can simply include this project as a subdirectory with add_subdirectory(glfw3webgpu) (see the content of CMakeLists.txt).

Option B Just copy glfw3webgpu.h and glfw3webgpu.c to your project's source tree. On MacOS, you must add the compile option -x objective-c and the link libraries -framework Cocoa, -framework CoreVideo, -framework IOKit, and -framework QuartzCore.

Example

Thanks to this extension it is possible to simply write a fully cross-platform WebGPU hello world:

#include "glfw3webgpu.h"

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <webgpu/webgpu.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
	// Init WebGPU
	WGPUInstanceDescriptor desc;
	desc.nextInChain = NULL;
	WGPUInstance instance = wgpuCreateInstance(&desc);

	// Init GLFW
	glfwInit();
	glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
	GLFWwindow* window = glfwCreateWindow(640, 480, "Learn WebGPU", NULL, NULL);

	// Here we create our WebGPU surface from the window!
	WGPUSurface surface = glfwGetWGPUSurface(instance, window);
	printf("surface = %p", surface);

	// Terminate GLFW
	while (!glfwWindowShouldClose(window)) glfwPollEvents();
	glfwDestroyWindow(window);
	glfwTerminate();

	return 0;
}

NB The linking process depends on the implementation of WebGPU that you are using. You can find detailed instructions for the wgpu-native implementation in this Hello WebGPU chapter. You may also check out examples/CMakeLists.txt.

License

MIT License
Copyright (c) 2022-2023 Élie Michel and the wgpu-native authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

An extension for the GLFW library for using WebGPU native.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 92.5%
  • CMake 7.5%