Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new Surface API #7

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[submodule "dawn"]
path = dawn
url = https://dawn.googlesource.com/dawn
branch = chromium/6167 # Chrome 121
branch = chromium/6478 # Chrome 126
shallow = true
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ if(EMSCRIPTEN)
else()
set(DAWN_FETCH_DEPENDENCIES ON)
add_subdirectory("dawn" EXCLUDE_FROM_ALL)
target_link_libraries(app PRIVATE webgpu_cpp webgpu_dawn webgpu_glfw)
target_link_libraries(app PRIVATE webgpu_cpp webgpu_dawn glfw webgpu_glfw)
endif()
2 changes: 1 addition & 1 deletion dawn
Submodule dawn updated from 12b225 to fda42b
92 changes: 57 additions & 35 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,66 @@
#endif

wgpu::Instance instance;
wgpu::Adapter adapter;
wgpu::Device device;
wgpu::RenderPipeline pipeline;

wgpu::SwapChain swapChain;
wgpu::Surface surface;
wgpu::TextureFormat format;
const uint32_t kWidth = 512;
const uint32_t kHeight = 512;

void SetupSwapChain(wgpu::Surface surface) {
wgpu::SwapChainDescriptor scDesc{
.usage = wgpu::TextureUsage::RenderAttachment,
.format = wgpu::TextureFormat::BGRA8Unorm,
void ConfigureSurface() {
wgpu::SurfaceCapabilities capabilities;
surface.GetCapabilities(adapter, &capabilities);
format = capabilities.formats[0];

wgpu::SurfaceConfiguration config{
.device = device,
.format = format,
.alphaMode = capabilities.alphaModes[0],
beaufortfrancois marked this conversation as resolved.
Show resolved Hide resolved
.width = kWidth,
.height = kHeight,
.presentMode = wgpu::PresentMode::Fifo};
swapChain = device.CreateSwapChain(surface, &scDesc);
.presentMode = capabilities.presentModes[0]};
beaufortfrancois marked this conversation as resolved.
Show resolved Hide resolved
surface.Configure(&config);
}

void GetDevice(void (*callback)(wgpu::Device)) {
void GetAdapter(void (*callback)(wgpu::Adapter)) {
instance.RequestAdapter(
nullptr,
// TODO(https://bugs.chromium.org/p/dawn/issues/detail?id=1892): Use
// wgpu::RequestAdapterStatus, wgpu::Adapter, and wgpu::Device.
// wgpu::RequestAdapterStatus and wgpu::Adapter.
[](WGPURequestAdapterStatus status, WGPUAdapter cAdapter,
const char* message, void* userdata) {
if (message) {
printf("RequestAdapter: %s\n", message);
}
if (status != WGPURequestAdapterStatus_Success) {
exit(0);
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
}
wgpu::Adapter adapter = wgpu::Adapter::Acquire(cAdapter);
adapter.RequestDevice(
nullptr,
[](WGPURequestDeviceStatus status, WGPUDevice cDevice,
const char* message, void* userdata) {
wgpu::Device device = wgpu::Device::Acquire(cDevice);
device.SetUncapturedErrorCallback(
[](WGPUErrorType type, const char* message, void* userdata) {
std::cout << "Error: " << type << " - message: " << message;
},
nullptr);
reinterpret_cast<void (*)(wgpu::Device)>(userdata)(device);
reinterpret_cast<void (*)(wgpu::Adapter)>(userdata)(adapter);
}, reinterpret_cast<void*>(callback));
}

void GetDevice(void (*callback)(wgpu::Device)) {
adapter.RequestDevice(
nullptr,
// TODO(https://bugs.chromium.org/p/dawn/issues/detail?id=1892): Use
// wgpu::RequestDeviceStatus and wgpu::Device.
[](WGPURequestDeviceStatus status, WGPUDevice cDevice,
const char* message, void* userdata) {
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
if (message) {
printf("RequestDevice: %s\n", message);
}
wgpu::Device device = wgpu::Device::Acquire(cDevice);
device.SetUncapturedErrorCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will be part of the descriptor soon.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(It is already there in Dawn, but Emscripten doesn't implement it.)

[](WGPUErrorType type, const char* message, void* userdata) {
std::cout << "Error: " << type << " - message: " << message;
},
userdata);
},
reinterpret_cast<void*>(callback));
nullptr);
reinterpret_cast<void (*)(wgpu::Device)>(userdata)(device);
}, reinterpret_cast<void*>(callback));
}

const char shaderCode[] = R"(
Expand All @@ -73,8 +90,7 @@ void CreateRenderPipeline() {
wgpu::ShaderModule shaderModule =
device.CreateShaderModule(&shaderModuleDescriptor);

wgpu::ColorTargetState colorTargetState{
.format = wgpu::TextureFormat::BGRA8Unorm};
wgpu::ColorTargetState colorTargetState{.format = format};

wgpu::FragmentState fragmentState{.module = shaderModule,
.targetCount = 1,
Expand All @@ -87,8 +103,11 @@ void CreateRenderPipeline() {
}

void Render() {
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);

wgpu::RenderPassColorAttachment attachment{
.view = swapChain.GetCurrentTextureView(),
.view = surfaceTexture.texture.CreateView(),
.loadOp = wgpu::LoadOp::Clear,
.storeOp = wgpu::StoreOp::Store};

Expand All @@ -104,8 +123,8 @@ void Render() {
device.GetQueue().Submit(1, &commands);
}

void InitGraphics(wgpu::Surface surface) {
SetupSwapChain(surface);
void InitGraphics() {
ConfigureSurface();
CreateRenderPipeline();
}

Expand All @@ -123,29 +142,32 @@ void Start() {
canvasDesc.selector = "#canvas";

wgpu::SurfaceDescriptor surfaceDesc{.nextInChain = &canvasDesc};
wgpu::Surface surface = instance.CreateSurface(&surfaceDesc);
surface = instance.CreateSurface(&surfaceDesc);
#else
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
#endif

InitGraphics(surface);
InitGraphics();

#if defined(__EMSCRIPTEN__)
emscripten_set_main_loop(Render, 0, false);
#else
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
Render();
swapChain.Present();
surface.Present();
instance.ProcessEvents();
}
#endif
}

int main() {
instance = wgpu::CreateInstance();
GetDevice([](wgpu::Device dev) {
device = dev;
Start();
GetAdapter([](wgpu::Adapter a) {
adapter = a;
GetDevice([](wgpu::Device d) {
device = d;
Start();
});
});
}