Skip to content

Commit

Permalink
update webgpu headers (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya authored Sep 19, 2023
1 parent 2773864 commit 19d2681
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 429 deletions.
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fn main() {
("WGPUSampler", "WGPUSamplerImpl"),
("WGPUShaderModule", "WGPUShaderModuleImpl"),
("WGPUSurface", "WGPUSurfaceImpl"),
("WGPUSwapChain", "WGPUSwapChainImpl"),
("WGPUTexture", "WGPUTextureImpl"),
("WGPUTextureView", "WGPUTextureViewImpl"),
];
Expand Down
3 changes: 2 additions & 1 deletion examples/capture/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "webgpu-headers/webgpu.h"
#include "wgpu.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down Expand Up @@ -85,7 +86,7 @@ int main(int argc, char *argv[]) {

frmwrk_setup_logging(WGPULogLevel_Warn);

instance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){0});
instance = wgpuCreateInstance(NULL);
ASSERT_CHECK(instance);

wgpuInstanceRequestAdapter(instance, NULL, handle_request_adapter,
Expand Down
3 changes: 2 additions & 1 deletion examples/compute/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "framework.h"
#include "webgpu-headers/webgpu.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down Expand Up @@ -59,7 +60,7 @@ int main(int argc, char *argv[]) {
uint32_t numbers_size = sizeof(numbers);
uint32_t numbers_length = numbers_size / sizeof(uint32_t);

instance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){0});
instance = wgpuCreateInstance(NULL);
ASSERT_CHECK(instance);

wgpuInstanceRequestAdapter(instance, NULL, handle_request_adapter,
Expand Down
3 changes: 1 addition & 2 deletions examples/enumerate_adapters/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
int main(int argc, char *argv[]) {
frmwrk_setup_logging(WGPULogLevel_Warn);

WGPUInstance instance =
wgpuCreateInstance(&(const WGPUInstanceDescriptor){0});
WGPUInstance instance = wgpuCreateInstance(NULL);
assert(instance);

const size_t adapter_count =
Expand Down
164 changes: 80 additions & 84 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "webgpu-headers/webgpu.h"
#include "wgpu.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -30,9 +31,7 @@ struct demo {
WGPUSurface surface;
WGPUAdapter adapter;
WGPUDevice device;
WGPUSwapChainDescriptor config;
WGPUSwapChain swapchain;
bool skip_curr_frame;
WGPUSurfaceConfiguration config;
};

static void handle_request_adapter(WGPURequestAdapterStatus status,
Expand Down Expand Up @@ -78,38 +77,13 @@ static void handle_glfw_framebuffer_size(GLFWwindow *window, int width,
}

struct demo *demo = glfwGetWindowUserPointer(window);
if (!demo || !demo->swapchain)
if (!demo)
return;

demo->config.width = width;
demo->config.height = height;

if (demo->swapchain)
wgpuSwapChainRelease(demo->swapchain);
demo->swapchain =
wgpuDeviceCreateSwapChain(demo->device, demo->surface, &demo->config);
assert(demo->swapchain);
}
static void handle_curr_texture_error(WGPUErrorType type, char const *message,
void *userdata) {
if (type == WGPUErrorType_NoError)
return;

printf(LOG_PREFIX " curr_texture_error type=%#.8x message=%s\n", type,
message);

struct demo *demo = userdata;

if (strstr(message, "Surface timed out") != NULL) {
demo->skip_curr_frame = true;
return;
} else if (strstr(message, "Surface is outdated") != NULL) {
demo->skip_curr_frame = true;
return;
} else if (strstr(message, "Surface was lost") != NULL) {
demo->skip_curr_frame = true;
return;
}
wgpuSurfaceConfigure(demo->surface, &demo->config);
}

int main(int argc, char *argv[]) {
Expand All @@ -121,6 +95,7 @@ int main(int argc, char *argv[]) {
WGPUShaderModule shader_module = NULL;
WGPUPipelineLayout pipeline_layout = NULL;
WGPURenderPipeline render_pipeline = NULL;
WGPUSurfaceCapabilities surface_capabilities = {0};
int ret = EXIT_SUCCESS;

#define ASSERT_CHECK(expr) \
Expand All @@ -141,7 +116,7 @@ int main(int argc, char *argv[]) {

ASSERT_CHECK(glfwInit());

demo.instance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){0});
demo.instance = wgpuCreateInstance(NULL);
ASSERT_CHECK(demo.instance);

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
Expand Down Expand Up @@ -262,48 +237,49 @@ int main(int argc, char *argv[]) {
});
ASSERT_CHECK(pipeline_layout);

WGPUTextureFormat surface_preferred_format =
wgpuSurfaceGetPreferredFormat(demo.surface, demo.adapter);
ASSERT_CHECK(surface_preferred_format != WGPUTextureFormat_Undefined);
wgpuSurfaceGetCapabilities(demo.surface, demo.adapter, &surface_capabilities);

render_pipeline = wgpuDeviceCreateRenderPipeline(
demo.device, &(const WGPURenderPipelineDescriptor){
.label = "render_pipeline",
.layout = pipeline_layout,
.vertex =
(const WGPUVertexState){
.module = shader_module,
.entryPoint = "vs_main",
},
.fragment =
&(const WGPUFragmentState){
.module = shader_module,
.entryPoint = "fs_main",
.targetCount = 1,
.targets =
(const WGPUColorTargetState[]){
(const WGPUColorTargetState){
.format = surface_preferred_format,
.writeMask = WGPUColorWriteMask_All,
},
},
},
.primitive =
(const WGPUPrimitiveState){
.topology = WGPUPrimitiveTopology_TriangleList,
},
.multisample =
(const WGPUMultisampleState){
.count = 1,
.mask = 0xFFFFFFFF,
},
});
demo.device,
&(const WGPURenderPipelineDescriptor){
.label = "render_pipeline",
.layout = pipeline_layout,
.vertex =
(const WGPUVertexState){
.module = shader_module,
.entryPoint = "vs_main",
},
.fragment =
&(const WGPUFragmentState){
.module = shader_module,
.entryPoint = "fs_main",
.targetCount = 1,
.targets =
(const WGPUColorTargetState[]){
(const WGPUColorTargetState){
.format = surface_capabilities.formats[0],
.writeMask = WGPUColorWriteMask_All,
},
},
},
.primitive =
(const WGPUPrimitiveState){
.topology = WGPUPrimitiveTopology_TriangleList,
},
.multisample =
(const WGPUMultisampleState){
.count = 1,
.mask = 0xFFFFFFFF,
},
});
ASSERT_CHECK(render_pipeline);

demo.config = (WGPUSwapChainDescriptor){
demo.config = (const WGPUSurfaceConfiguration){
.device = demo.device,
.usage = WGPUTextureUsage_RenderAttachment,
.format = surface_preferred_format,
.format = surface_capabilities.formats[0],
.presentMode = WGPUPresentMode_Fifo,
.alphaMode = surface_capabilities.alphaModes[0],
};

{
Expand All @@ -313,24 +289,44 @@ int main(int argc, char *argv[]) {
demo.config.height = height;
}

demo.swapchain =
wgpuDeviceCreateSwapChain(demo.device, demo.surface, &demo.config);
ASSERT_CHECK(demo.swapchain);
wgpuSurfaceConfigure(demo.surface, &demo.config);

while (!glfwWindowShouldClose(window)) {
demo.skip_curr_frame = false;
glfwPollEvents();

wgpuDevicePushErrorScope(demo.device, WGPUErrorFilter_Validation);
WGPUTextureView next_texture =
wgpuSwapChainGetCurrentTextureView(demo.swapchain);
wgpuDevicePopErrorScope(demo.device, handle_curr_texture_error, &demo);
if (demo.skip_curr_frame) {
if (next_texture)
wgpuTextureViewRelease(next_texture);
WGPUSurfaceTexture surface_texture;
wgpuSurfaceGetCurrentTexture(demo.surface, &surface_texture);
switch (surface_texture.status) {
case WGPUSurfaceGetCurrentTextureStatus_Success:
// All good, could check for `surface_texture.suboptimal` here.
break;
case WGPUSurfaceGetCurrentTextureStatus_Timeout:
case WGPUSurfaceGetCurrentTextureStatus_Outdated:
case WGPUSurfaceGetCurrentTextureStatus_Lost: {
// Skip this frame, and re-configure surface.
wgpuTextureRelease(surface_texture.texture);
int width, height;
glfwGetWindowSize(window, &width, &height);
if (width != 0 && height != 0) {
demo.config.width = width;
demo.config.height = height;
wgpuSurfaceConfigure(demo.surface, &demo.config);
}
continue;
}
assert(next_texture);
case WGPUSurfaceGetCurrentTextureStatus_OutOfMemory:
case WGPUSurfaceGetCurrentTextureStatus_DeviceLost:
case WGPUSurfaceGetCurrentTextureStatus_Force32:
// Fatal error
printf(LOG_PREFIX " get_current_texture status=%#.8x\n",
surface_texture.status);
abort();
}
assert(surface_texture.texture);

WGPUTextureView frame =
wgpuTextureCreateView(surface_texture.texture, NULL);
assert(frame);

WGPUCommandEncoder command_encoder = wgpuDeviceCreateCommandEncoder(
demo.device, &(const WGPUCommandEncoderDescriptor){
Expand All @@ -346,7 +342,7 @@ int main(int argc, char *argv[]) {
.colorAttachments =
(const WGPURenderPassColorAttachment[]){
(const WGPURenderPassColorAttachment){
.view = next_texture,
.view = frame,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue =
Expand All @@ -372,12 +368,13 @@ int main(int argc, char *argv[]) {
assert(command_buffer);

wgpuQueueSubmit(queue, 1, (const WGPUCommandBuffer[]){command_buffer});
wgpuSwapChainPresent(demo.swapchain);
wgpuSurfacePresent(demo.surface);

wgpuCommandBufferRelease(command_buffer);
wgpuRenderPassEncoderRelease(render_pass_encoder);
wgpuCommandEncoderRelease(command_encoder);
wgpuTextureViewRelease(next_texture);
wgpuTextureViewRelease(frame);
wgpuTextureRelease(surface_texture.texture);
}

cleanup_and_exit:
Expand All @@ -387,8 +384,7 @@ int main(int argc, char *argv[]) {
wgpuPipelineLayoutRelease(pipeline_layout);
if (shader_module)
wgpuShaderModuleRelease(shader_module);
if (demo.swapchain)
wgpuSwapChainRelease(demo.swapchain);
wgpuSurfaceCapabilitiesFreeMembers(surface_capabilities);
if (queue)
wgpuQueueRelease(queue);
if (demo.device)
Expand Down
2 changes: 1 addition & 1 deletion ffi/webgpu-headers
39 changes: 5 additions & 34 deletions ffi/wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
typedef enum WGPUNativeSType {
// Start at 6 to prevent collisions with webgpu STypes
WGPUSType_DeviceExtras = 0x60000001,
WGPUSType_AdapterExtras = 0x60000002,
WGPUSType_RequiredLimitsExtras = 0x60000003,
WGPUSType_PipelineLayoutExtras = 0x60000004,
WGPUSType_ShaderModuleGLSLDescriptor = 0x60000005,
WGPUSType_SupportedLimitsExtras = 0x60000003,
WGPUSType_RequiredLimitsExtras = 0x60000002,
WGPUSType_PipelineLayoutExtras = 0x60000003,
WGPUSType_ShaderModuleGLSLDescriptor = 0x60000004,
WGPUSType_SupportedLimitsExtras = 0x60000005,
WGPUSType_InstanceExtras = 0x60000006,
WGPUSType_SwapChainDescriptorExtras = 0x60000007,
WGPUNativeSType_Force32 = 0x7FFFFFFF
} WGPUNativeSType;

Expand Down Expand Up @@ -58,15 +56,6 @@ typedef enum WGPUDx12Compiler {
WGPUDx12Compiler_Force32 = 0x7FFFFFFF
} WGPUDx12Compiler;

typedef enum WGPUCompositeAlphaMode {
WGPUCompositeAlphaMode_Auto = 0x00000000,
WGPUCompositeAlphaMode_Opaque = 0x00000001,
WGPUCompositeAlphaMode_PreMultiplied = 0x00000002,
WGPUCompositeAlphaMode_PostMultiplied = 0x00000003,
WGPUCompositeAlphaMode_Inherit = 0x00000004,
WGPUCompositeAlphaMode_Force32 = 0x7FFFFFFF
} WGPUCompositeAlphaMode;

typedef struct WGPUInstanceExtras {
WGPUChainedStruct chain;
WGPUInstanceBackendFlags backends;
Expand Down Expand Up @@ -157,22 +146,6 @@ typedef struct WGPUGlobalReport {
WGPUHubReport gl;
} WGPUGlobalReport;

typedef struct WGPUSurfaceCapabilities {
size_t formatCount;
WGPUTextureFormat * formats;
size_t presentModeCount;
WGPUPresentMode * presentModes;
size_t alphaModeCount;
WGPUCompositeAlphaMode * alphaModes;
} WGPUSurfaceCapabilities;

typedef struct WGPUSwapChainDescriptorExtras {
WGPUChainedStruct chain;
WGPUCompositeAlphaMode alphaMode;
size_t viewFormatCount;
WGPUTextureFormat const * viewFormats;
} WGPUSwapChainDescriptorExtras;

typedef struct WGPUInstanceEnumerateAdapterOptions {
WGPUChainedStruct const * nextInChain;
WGPUInstanceBackendFlags backends;
Expand All @@ -190,16 +163,14 @@ size_t wgpuInstanceEnumerateAdapters(WGPUInstance instance, WGPUInstanceEnumerat
WGPUSubmissionIndex wgpuQueueSubmitForIndex(WGPUQueue queue, size_t commandCount, WGPUCommandBuffer const * commands);

// Returns true if the queue is empty, or false if there are more queue submissions still in flight.
bool wgpuDevicePoll(WGPUDevice device, bool wait, WGPUWrappedSubmissionIndex const * wrappedSubmissionIndex);
WGPUBool wgpuDevicePoll(WGPUDevice device, WGPUBool wait, WGPUWrappedSubmissionIndex const * wrappedSubmissionIndex);

void wgpuSetLogCallback(WGPULogCallback callback, void * userdata);

void wgpuSetLogLevel(WGPULogLevel level);

uint32_t wgpuGetVersion(void);

void wgpuSurfaceGetCapabilities(WGPUSurface surface, WGPUAdapter adapter, WGPUSurfaceCapabilities * capabilities);

void wgpuRenderPassEncoderSetPushConstants(WGPURenderPassEncoder encoder, WGPUShaderStageFlags stages, uint32_t offset, uint32_t sizeBytes, void* const data);

void wgpuRenderPassEncoderMultiDrawIndirect(WGPURenderPassEncoder encoder, WGPUBuffer buffer, uint64_t offset, uint32_t count);
Expand Down
Loading

0 comments on commit 19d2681

Please sign in to comment.