Skip to content

Commit

Permalink
Start merging the Emscripten code
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 25, 2025
1 parent 7ca0b75 commit 37e4d2e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ typedef struct kore_webgpu_buffer_access {
typedef struct kore_webgpu_command_list {
WGPUCommandEncoder command_encoder;
WGPURenderPassEncoder render_pass_encoder;
WGPUDevice device;
WGPUSwapChain swap_chain;
} kore_webgpu_command_list;

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion backends/gpu/webgpu/includes/kore3/webgpu/device_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
typedef struct kore_webgpu_device {
WGPUDevice device;
WGPUQueue queue;
WGPUSwapChain swap_chain;
WGPUSurface surface;
WGPUShaderModule shader_module;
} kore_webgpu_device;

Expand Down
7 changes: 1 addition & 6 deletions backends/gpu/webgpu/sources/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@
void kore_webgpu_command_list_destroy(kore_gpu_command_list *list) {}

void kore_webgpu_command_list_begin_render_pass(kore_gpu_command_list *list, const kore_gpu_render_pass_parameters *parameters) {
WGPUCommandEncoderDescriptor command_encoder_descriptor = {0};
list->webgpu.command_encoder = wgpuDeviceCreateCommandEncoder(list->webgpu.device, &command_encoder_descriptor);

WGPUTextureViewDescriptor texture_view_descriptor = {
.format = WGPUTextureFormat_BGRA8Unorm,
.dimension = WGPUTextureViewDimension_2D,
.arrayLayerCount = 1,
};
//WGPUTextureView texture_view = wgpuTextureCreateView(parameters->color_attachments[0].texture.texture->webgpu.texture, &texture_view_descriptor);

WGPUTextureView texture_view = wgpuSwapChainGetCurrentTextureView(list->webgpu.swap_chain);
WGPUTextureView texture_view = wgpuTextureCreateView(parameters->color_attachments[0].texture.texture->webgpu.texture, &texture_view_descriptor);

WGPURenderPassColorAttachment color_attachment = {
.view = texture_view,
Expand Down
67 changes: 35 additions & 32 deletions backends/gpu/webgpu/sources/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,45 @@
#endif

#include <assert.h>
#include <stdio.h>

extern WGPUDevice wgpu_device;
extern WGPUInstance wgpu_instance;
extern WGPUAdapter wgpu_adapter;

static void error_callback(WGPUErrorType errorType, const char* message, void* userdata) {
printf("%d: %s\n", errorType, message);
}

void kore_webgpu_device_create(kore_gpu_device *device, const kore_gpu_device_wishlist *wishlist) {
#ifdef KORE_EMSCRIPTEN
device->webgpu.device = emscripten_webgpu_get_device();
device->webgpu.device = wgpu_device;
#endif
wgpuDevicePushErrorScope(device->webgpu.device, WGPUErrorFilter_Validation);

wgpuDeviceSetUncapturedErrorCallback(wgpu_device, error_callback, NULL);

device->webgpu.queue = wgpuDeviceGetQueue(device->webgpu.device);

WGPUSurfaceDescriptorFromCanvasHTMLSelector canvas_selector = {
.chain = {
.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector,
},
.selector = "#canvas",
};
WGPUSurfaceDescriptorFromCanvasHTMLSelector canvasDesc = {0};
canvasDesc.selector = "#canvas";
canvasDesc.chain.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector;

WGPUSurfaceDescriptor surface_descriptor = {
.nextInChain = (WGPUChainedStruct *)&canvas_selector,
};
WGPUSurfaceDescriptor surfDesc = {0};
surfDesc.nextInChain = (WGPUChainedStruct *)&canvasDesc;
device->webgpu.surface = wgpuInstanceCreateSurface(wgpu_instance, &surfDesc);

WGPUInstance instance = wgpuCreateInstance(NULL);
WGPUSurface surface = wgpuInstanceCreateSurface(instance, &surface_descriptor);
WGPUSurfaceCapabilities capabilities = {0};
wgpuSurfaceGetCapabilities(device->webgpu.surface, wgpu_adapter, &capabilities);

WGPUSwapChainDescriptor swap_chain_descriptor = {
WGPUSurfaceConfiguration config = {
.device = wgpu_device,
.format = capabilities.formats[0],
.usage = WGPUTextureUsage_RenderAttachment,
.format = WGPUTextureFormat_BGRA8Unorm,
.width = 800,// kore_window_width(0),
.height = 600,//kore_window_height(0),
.presentMode = WGPUPresentMode_Fifo,
};
device->webgpu.swap_chain = wgpuDeviceCreateSwapChain(device->webgpu.device, surface, &swap_chain_descriptor);
.alphaMode = WGPUCompositeAlphaMode_Auto,
.width = kore_window_width(0),
.height = kore_window_height(0),
.presentMode = WGPUPresentMode_Fifo};
wgpuSurfaceConfigure(device->webgpu.surface, &config);

WGPUShaderModuleWGSLDescriptor shader_module_wgsl_descriptor = {
.code = wgsl,
Expand Down Expand Up @@ -116,9 +124,6 @@ void kore_webgpu_device_create_buffer(kore_gpu_device *device, const kore_gpu_bu
void kore_webgpu_device_create_command_list(kore_gpu_device *device, kore_gpu_command_list_type type, kore_gpu_command_list *list) {
WGPUCommandEncoderDescriptor command_encoder_descriptor = {0};
list->webgpu.command_encoder = wgpuDeviceCreateCommandEncoder(device->webgpu.device, &command_encoder_descriptor);

list->webgpu.device = device->webgpu.device;
list->webgpu.swap_chain = device->webgpu.swap_chain;
}

void kore_webgpu_device_create_texture(kore_gpu_device *device, const kore_gpu_texture_parameters *parameters, kore_gpu_texture *texture) {
Expand All @@ -143,7 +148,9 @@ void kore_webgpu_device_create_texture(kore_gpu_device *device, const kore_gpu_t
static kore_gpu_texture framebuffer;

kore_gpu_texture *kore_webgpu_device_get_framebuffer(kore_gpu_device *device) {
framebuffer.webgpu.texture = wgpuSwapChainGetCurrentTexture(device->webgpu.swap_chain);
WGPUSurfaceTexture surfaceTexture;
wgpuSurfaceGetCurrentTexture(device->webgpu.surface, &surfaceTexture);
framebuffer.webgpu.texture = surfaceTexture.texture;
return &framebuffer;
}

Expand All @@ -155,15 +162,11 @@ void kore_webgpu_device_execute_command_list(kore_gpu_device *device, kore_gpu_c
WGPUCommandBufferDescriptor command_buffer_descriptor = {0};

WGPUCommandBuffer command_buffer = wgpuCommandEncoderFinish(list->webgpu.command_encoder, &command_buffer_descriptor);
wgpuCommandEncoderRelease(list->webgpu.command_encoder);


wgpuQueueSubmit(device->webgpu.queue, 1, &command_buffer);
wgpuCommandBufferRelease(command_buffer);

//emscripten_sleep(100);

//WGPUCommandEncoderDescriptor command_encoder_descriptor = {0};
//list->webgpu.command_encoder = wgpuDeviceCreateCommandEncoder(device->webgpu.device, &command_encoder_descriptor);

WGPUCommandEncoderDescriptor command_encoder_descriptor = {0};
list->webgpu.command_encoder = wgpuDeviceCreateCommandEncoder(device->webgpu.device, &command_encoder_descriptor);
}

void kore_webgpu_device_wait_until_idle(kore_gpu_device *device) {}
Expand Down
75 changes: 28 additions & 47 deletions backends/system/emscripten/sources/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

static int html5_argc;
static char **html5_argv;
static bool initialized = false;

static void drawfunc() {
if (!initialized)
return;
kore_internal_update_callback();
kore_audio_update();
#ifdef KORE_OPENGL
Expand Down Expand Up @@ -316,40 +313,10 @@ int kore_hardware_threads(void) {
return 4;
}

extern int kickstart(int argc, char **argv);

#ifdef KORE_WEBGPU
EMSCRIPTEN_KEEPALIVE void kore_internal_webgpu_initialized() {
kickstart(html5_argc, html5_argv);
initialized = true;
}
#endif

int main_(int argc, char **argv) {
html5_argc = argc;
html5_argv = argv;
#ifdef KORE_WEBGPU
char *code = "(async () => {\
const adapter = await navigator.gpu.requestAdapter();\
const device = await adapter.requestDevice();\
Module.preinitializedWebGPUDevice = device;\
_kore_internal_webgpu_initialized();\
})();";
emscripten_run_script(code);
#else
kickstart(argc, argv);
initialized = true;
#endif
emscripten_set_main_loop(drawfunc, 0, 1);

return 0;
}

#include <webgpu/webgpu.h>
#include <emscripten/html5_webgpu.h>

static WGPUInstance instance;

static const char shaderCode[] = "\
@vertex\n\
fn main_v(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {\n\
Expand All @@ -364,16 +331,19 @@ static const char shaderCode[] = "\
}\n\
";

static WGPUAdapter adapter;
static WGPUDevice wgpu_device;
WGPUInstance wgpu_instance;
WGPUAdapter wgpu_adapter;
WGPUDevice wgpu_device;
static WGPUQueue queue;
static WGPURenderPipeline pipeline;
static int testsCompleted = 0;

void run();

void main3(void) {
run();
//run();
kickstart(html5_argc, html5_argv);
emscripten_set_main_loop(drawfunc, 0, false);
}

void device_callback(WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * userdata) {
Expand All @@ -388,12 +358,12 @@ void device_callback(WGPURequestDeviceStatus status, WGPUDevice device, char con
}

void GetDevice() {
wgpuAdapterRequestDevice(adapter, NULL, device_callback, NULL);
wgpuAdapterRequestDevice(wgpu_adapter, NULL, device_callback, NULL);
}

void main2(void) {
WGPUAdapterInfo info;
wgpuAdapterGetInfo(adapter, &info);
wgpuAdapterGetInfo(wgpu_adapter, &info);
printf("adapter vendor: %s\n", info.vendor);
printf("adapter architecture: %s\n", info.architecture);
printf("adapter device: %s\n", info.device);
Expand All @@ -402,20 +372,20 @@ void main2(void) {
GetDevice();
}

void adapter_callback(WGPURequestAdapterStatus status, WGPUAdapter _adapter, char const * message, void * userdata) {
void adapter_callback(WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * userdata) {
if (message) {
printf("RequestAdapter: %s\n", message);
}
assert(status == WGPURequestAdapterStatus_Success);

adapter = _adapter;
wgpu_adapter = adapter;

main2();
}

void GetAdapter() {
instance = wgpuCreateInstance(NULL);
wgpuInstanceRequestAdapter(instance, NULL, adapter_callback, NULL);
wgpu_instance = wgpuCreateInstance(NULL);
wgpuInstanceRequestAdapter(wgpu_instance, NULL, adapter_callback, NULL);
}

void error_callback(WGPUErrorType errorType, const char* message, void* userdata) {
Expand Down Expand Up @@ -529,10 +499,10 @@ void run() {

WGPUSurfaceDescriptor surfDesc = {0};
surfDesc.nextInChain = (WGPUChainedStruct *)&canvasDesc;
surface = wgpuInstanceCreateSurface(instance, &surfDesc);
surface = wgpuInstanceCreateSurface(wgpu_instance, &surfDesc);

WGPUSurfaceCapabilities capabilities;
wgpuSurfaceGetCapabilities(surface, adapter, &capabilities);
wgpuSurfaceGetCapabilities(surface, wgpu_adapter, &capabilities);

WGPUSurfaceConfiguration config = {
.device = wgpu_device,
Expand All @@ -547,7 +517,18 @@ void run() {
emscripten_set_main_loop(frame, 0, false);
}

int main() {
GetAdapter();
return 0;
extern int kickstart(int argc, char **argv);

int main(int argc, char **argv) {
html5_argc = argc;
html5_argv = argv;

#ifdef KORE_WEBGPU
GetAdapter();
#else
kickstart(argc, argv);
emscripten_set_main_loop(drawfunc, 0, 1);
#endif

return 0;
}

0 comments on commit 37e4d2e

Please sign in to comment.