Skip to content

Commit

Permalink
Merge pull request #7 from beaufortfrancois/remove-swapchain
Browse files Browse the repository at this point in the history
Use new Surface API
  • Loading branch information
beaufortfrancois authored Jun 12, 2024
2 parents 54d67aa + 1ad2ebc commit 4003303
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
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: 56 additions & 36 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,64 @@
#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,
.width = kWidth,
.height = kHeight,
.presentMode = wgpu::PresentMode::Fifo};
swapChain = device.CreateSwapChain(surface, &scDesc);
.height = kHeight};
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);
}
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) {
if (message) {
printf("RequestDevice: %s\n", message);
}
wgpu::Device device = wgpu::Device::Acquire(cDevice);
device.SetUncapturedErrorCallback(
[](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 +88,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 +101,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 +121,8 @@ void Render() {
device.GetQueue().Submit(1, &commands);
}

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

Expand All @@ -123,29 +140,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();
});
});
}

0 comments on commit 4003303

Please sign in to comment.