Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed May 13, 2024
1 parent 54d67aa commit 64a5abc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
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()
87 changes: 52 additions & 35 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,60 @@
#endif

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

wgpu::SwapChain swapChain;
wgpu::Surface surface;
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,
.width = kWidth,
.height = kHeight,
.presentMode = wgpu::PresentMode::Fifo};
swapChain = device.CreateSwapChain(surface, &scDesc);
void ConfigureSurface() {
wgpu::SurfaceCapabilities capabilities;
surface.GetCapabilities(adapter, &capabilities);

wgpu::SurfaceConfiguration config = {
.device = device,
.width = kWidth,
.height = kHeight,
.format = capabilities.formats[0],
.alphaMode = capabilities.alphaModes[0],
.presentMode = capabilities.presentModes[0],
};

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 (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::Device.
[](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;
},
userdata);
},
reinterpret_cast<void*>(callback));
nullptr);
reinterpret_cast<void (*)(wgpu::Device)>(userdata)(device);
}, reinterpret_cast<void*>(callback));
}

const char shaderCode[] = R"(
Expand Down Expand Up @@ -87,8 +98,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 +118,8 @@ void Render() {
device.GetQueue().Submit(1, &commands);
}

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

Expand All @@ -123,29 +137,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 64a5abc

Please sign in to comment.