// This file is part of the 64k demo project. // GPU rendering system interface. #pragma once #include "platform/platform.h" struct PlatformState; struct GpuContext { WGPUDevice device; WGPUQueue queue; WGPUTextureFormat format; }; struct GpuBuffer { WGPUBuffer buffer; size_t size; }; struct ComputePass { WGPUComputePipeline pipeline; WGPUBindGroup bind_group; uint32_t workgroup_size_x; uint32_t workgroup_size_y; uint32_t workgroup_size_z; }; struct RenderPass { WGPURenderPipeline pipeline; WGPUBindGroup bind_group; uint32_t vertex_count; uint32_t instance_count; }; void gpu_init(PlatformState* platform_state); void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat_time, float beat_phase); void gpu_resize(int width, int height); void gpu_shutdown(); WGPUSurface gpu_get_surface(); const GpuContext* gpu_get_context(); struct ResourceBinding { GpuBuffer buffer; WGPUBufferBindingType type; }; inline void gpu_init_color_attachment(WGPURenderPassColorAttachment& attachment, WGPUTextureView view) { attachment.view = view; attachment.loadOp = WGPULoadOp_Clear; attachment.storeOp = WGPUStoreOp_Store; attachment.clearValue = {0.0f, 0.0f, 0.0f, 1.0f}; #if !defined(DEMO_CROSS_COMPILE_WIN32) attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; #endif } struct TextureWithView { WGPUTexture texture; WGPUTextureView view; }; #if defined(DEMO_CROSS_COMPILE_WIN32) using GpuTextureCopyInfo = WGPUImageCopyTexture; using GpuTextureDataLayout = WGPUTextureDataLayout; #else using GpuTextureCopyInfo = WGPUTexelCopyTextureInfo; using GpuTextureDataLayout = WGPUTexelCopyBufferLayout; #endif GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, const void* data = nullptr); TextureWithView gpu_create_texture_2d(WGPUDevice device, uint32_t width, uint32_t height, WGPUTextureFormat format, WGPUTextureUsage usage, uint32_t mip_levels = 1); TextureWithView gpu_create_storage_texture_2d(WGPUDevice device, uint32_t width, uint32_t height, WGPUTextureFormat format); TextureWithView gpu_create_post_process_texture(WGPUDevice device, uint32_t width, uint32_t height, WGPUTextureFormat format); WGPUTextureView gpu_create_mip_view(WGPUTexture texture, WGPUTextureFormat format, uint32_t mip_level); WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture, WGPUTextureFormat format, uint32_t mip_levels = 1); ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, ResourceBinding* bindings, int num_bindings); RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, // Needed for render pipeline const char* shader_code, ResourceBinding* bindings, int num_bindings);