diff options
| author | Andre Weissflog <floooh@gmail.com> | 2025-12-13 19:38:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-13 19:38:24 +0100 |
| commit | cda1eb5a32d472c74c805ee02243dbdd46bd7b01 (patch) | |
| tree | 5879499fffd1b7804938532a70539b36cbc65374 /sokol_app.h | |
| parent | b95c5245ba357967220c9a860c7578a7487937b0 (diff) | |
| parent | e857bcb682c9e1d55ad878a6317cfb902eeb84f5 (diff) | |
Merge pull request #1397 from floooh/issue1367/wgpu-cleanup
WebGPU cleanup round
Diffstat (limited to 'sokol_app.h')
| -rw-r--r-- | sokol_app.h | 139 |
1 files changed, 58 insertions, 81 deletions
diff --git a/sokol_app.h b/sokol_app.h index 84e639ce..99f25d11 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -3841,8 +3841,7 @@ _SOKOL_PRIVATE WGPUCallbackMode _sapp_wgpu_callbackmode(void) { _SOKOL_PRIVATE void _sapp_wgpu_await(WGPUFuture future) { #if defined(_SAPP_WGPU_HAS_WAIT) SOKOL_ASSERT(_sapp.wgpu.instance); - WGPUFutureWaitInfo wait_info; - _sapp_clear(&wait_info, sizeof(wait_info)); + _SAPP_STRUCT(WGPUFutureWaitInfo, wait_info); wait_info.future = future; WGPUWaitStatus res = wgpuInstanceWaitAny(_sapp.wgpu.instance, 1, &wait_info, UINT64_MAX); SOKOL_ASSERT(res == WGPUWaitStatus_Success); _SOKOL_UNUSED(res); @@ -3879,30 +3878,25 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_swapchain(bool called_from_resize) { if (!called_from_resize) { SOKOL_ASSERT(0 == _sapp.wgpu.surface); - WGPUSurfaceDescriptor surf_desc; - _sapp_clear(&surf_desc, sizeof(surf_desc)); + _SAPP_STRUCT(WGPUSurfaceDescriptor, surf_desc); #if defined (_SAPP_EMSCRIPTEN) - WGPUEmscriptenSurfaceSourceCanvasHTMLSelector html_canvas_desc; - _sapp_clear(&html_canvas_desc, sizeof(html_canvas_desc)); + _SAPP_STRUCT(WGPUEmscriptenSurfaceSourceCanvasHTMLSelector, html_canvas_desc); html_canvas_desc.chain.sType = WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector; html_canvas_desc.selector = _sapp_wgpu_stringview(_sapp.html5_canvas_selector); surf_desc.nextInChain = &html_canvas_desc.chain; #elif defined(_SAPP_MACOS) - WGPUSurfaceSourceMetalLayer from_metal_layer; - _sapp_clear(&from_metal_layer, sizeof(from_metal_layer)); + _SAPP_STRUCT(WGPUSurfaceSourceMetalLayer, from_metal_layer); from_metal_layer.chain.sType = WGPUSType_SurfaceSourceMetalLayer; from_metal_layer.layer = _sapp.macos.view.layer; surf_desc.nextInChain = &from_metal_layer.chain; #elif defined(_SAPP_WIN32) - WGPUSurfaceSourceWindowsHWND from_hwnd; - _sapp_clear(&from_hwnd, sizeof(from_hwnd)); + _SAPP_STRUCT(WGPUSurfaceSourceWindowsHWND, from_hwnd); from_hwnd.chain.sType = WGPUSType_SurfaceSourceWindowsHWND; from_hwnd.hinstance = GetModuleHandleW(NULL); from_hwnd.hwnd = _sapp.win32.hwnd; surf_desc.nextInChain = &from_hwnd.chain; #elif defined(_SAPP_LINUX) - WGPUSurfaceSourceXlibWindow from_xlib; - _sapp_clear(&from_xlib, sizeof(from_xlib)); + _SAPP_STRUCT(WGPUSurfaceSourceXlibWindow, from_xlib); from_xlib.chain.sType = WGPUSType_SurfaceSourceXlibWindow; from_xlib.display = _sapp.x11.display; from_xlib.window = _sapp.x11.window; @@ -3914,8 +3908,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_swapchain(bool called_from_resize) { if (0 == _sapp.wgpu.surface) { _SAPP_PANIC(WGPU_SWAPCHAIN_CREATE_SURFACE_FAILED); } - WGPUSurfaceCapabilities surf_caps; - _sapp_clear(&surf_caps, sizeof(surf_caps)); + _SAPP_STRUCT(WGPUSurfaceCapabilities, surf_caps); WGPUStatus caps_status = wgpuSurfaceGetCapabilities(_sapp.wgpu.surface, _sapp.wgpu.adapter, &surf_caps); if (caps_status != WGPUStatus_Success) { _SAPP_PANIC(WGPU_SWAPCHAIN_SURFACE_GET_CAPABILITIES_FAILED); @@ -3924,8 +3917,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_swapchain(bool called_from_resize) { } SOKOL_ASSERT(_sapp.wgpu.surface); - WGPUSurfaceConfiguration surf_conf; - _sapp_clear(&surf_conf, sizeof(surf_conf)); + _SAPP_STRUCT(WGPUSurfaceConfiguration, surf_conf); surf_conf.device = _sapp.wgpu.device; surf_conf.format = _sapp.wgpu.render_format; surf_conf.usage = WGPUTextureUsage_RenderAttachment; @@ -3941,8 +3933,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_swapchain(bool called_from_resize) { surf_conf.presentMode = WGPUPresentMode_Fifo; wgpuSurfaceConfigure(_sapp.wgpu.surface, &surf_conf); - WGPUTextureDescriptor ds_desc; - _sapp_clear(&ds_desc, sizeof(ds_desc)); + _SAPP_STRUCT(WGPUTextureDescriptor, ds_desc); ds_desc.usage = WGPUTextureUsage_RenderAttachment; ds_desc.dimension = WGPUTextureDimension_2D; ds_desc.size.width = (uint32_t)_sapp.framebuffer_width; @@ -3961,8 +3952,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_swapchain(bool called_from_resize) { } if (_sapp.sample_count > 1) { - WGPUTextureDescriptor msaa_desc; - _sapp_clear(&msaa_desc, sizeof(msaa_desc)); + _SAPP_STRUCT(WGPUTextureDescriptor, msaa_desc); msaa_desc.usage = WGPUTextureUsage_RenderAttachment; msaa_desc.dimension = WGPUTextureDimension_2D; msaa_desc.size.width = (uint32_t)_sapp.framebuffer_width; @@ -4009,8 +3999,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_discard_swapchain(bool called_from_resize) { _SOKOL_PRIVATE void _sapp_wgpu_swapchain_next(void) { SOKOL_ASSERT(0 == _sapp.wgpu.swapchain_view); - WGPUSurfaceTexture surf_tex; - _sapp_clear(&surf_tex, sizeof(surf_tex)); + _SAPP_STRUCT(WGPUSurfaceTexture, surf_tex); wgpuSurfaceGetCurrentTexture(_sapp.wgpu.surface, &surf_tex); switch (surf_tex.status) { case WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal: @@ -4101,8 +4090,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_request_device_cb(WGPURequestDeviceStatus status, SOKOL_ASSERT(device); _sapp.wgpu.device = device; #if !defined(_SAPP_EMSCRIPTEN) - WGPULoggingCallbackInfo cb_info; - _sapp_clear(&cb_info, sizeof(cb_info)); + _SAPP_STRUCT(WGPULoggingCallbackInfo, cb_info); cb_info.callback = _sapp_wgpu_device_logging_cb; wgpuDeviceSetLoggingCallback(_sapp.wgpu.device, cb_info); #endif @@ -4145,13 +4133,11 @@ _SOKOL_PRIVATE void _sapp_wgpu_create_device_and_swapchain(void) { requiredLimits.maxStorageBuffersPerShaderStage = adapterLimits.maxStorageBuffersPerShaderStage; requiredLimits.maxStorageTexturesPerShaderStage = adapterLimits.maxStorageTexturesPerShaderStage; - WGPURequestDeviceCallbackInfo cb_info; - _sapp_clear(&cb_info, sizeof(cb_info)); + _SAPP_STRUCT(WGPURequestDeviceCallbackInfo, cb_info); cb_info.mode = _sapp_wgpu_callbackmode(); cb_info.callback = _sapp_wgpu_request_device_cb; - WGPUDeviceDescriptor dev_desc; - _sapp_clear(&dev_desc, sizeof(dev_desc)); + _SAPP_STRUCT(WGPUDeviceDescriptor, dev_desc); dev_desc.requiredFeatureCount = cur_feature_index; dev_desc.requiredFeatures = requiredFeatures; dev_desc.requiredLimits = &requiredLimits; @@ -4188,8 +4174,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_request_adapter_cb(WGPURequestAdapterStatus statu _SOKOL_PRIVATE void _sapp_wgpu_create_adapter(void) { SOKOL_ASSERT(_sapp.wgpu.instance); // FIXME: power preference? - WGPURequestAdapterCallbackInfo cb_info; - _sapp_clear(&cb_info, sizeof(cb_info)); + _SAPP_STRUCT(WGPURequestAdapterCallbackInfo, cb_info); cb_info.mode = _sapp_wgpu_callbackmode(); cb_info.callback = _sapp_wgpu_request_adapter_cb; WGPUFuture future = wgpuInstanceRequestAdapter(_sapp.wgpu.instance, 0, cb_info); @@ -4204,8 +4189,7 @@ _SOKOL_PRIVATE void _sapp_wgpu_init(void) { SOKOL_ASSERT(0 == _sapp.wgpu.instance); SOKOL_ASSERT(!_sapp.wgpu.init_done); - WGPUInstanceDescriptor desc; - _sapp_clear(&desc, sizeof(desc)); + _SAPP_STRUCT(WGPUInstanceDescriptor, desc); #if defined(_SAPP_WGPU_HAS_WAIT) WGPUInstanceFeatureName inst_features[1] = { WGPUInstanceFeatureName_TimedWaitAny, @@ -5345,6 +5329,27 @@ _SOKOL_PRIVATE void _sapp_macos_app_event(sapp_event_type type) { } } +// called in applicationDidFinishedLaunching when no window size was provided +_SOKOL_PRIVATE void _sapp_macos_init_default_dimensions(void) { + if (_sapp.desc.high_dpi) { + _sapp.dpi_scale = NSScreen.mainScreen.backingScaleFactor; + } else { + _sapp.dpi_scale = 1.0f; + } + NSRect screen_rect = NSScreen.mainScreen.frame; + // use 4/5 of screen size as default size + const float default_widthf = (screen_rect.size.width * 4.0f) / 5.0f; + const float default_heightf = (screen_rect.size.height * 4.0f) / 5.0f; + if (_sapp.window_width == 0) { + _sapp.window_width = _sapp_roundf_gzero(default_widthf); + } + if (_sapp.window_height == 0) { + _sapp.window_height = _sapp_roundf_gzero(default_heightf); + } + _sapp.framebuffer_width = _sapp_roundf_gzero(default_widthf * _sapp.dpi_scale); + _sapp.framebuffer_height = _sapp_roundf_gzero(default_heightf * _sapp.dpi_scale); +} + /* NOTE: unlike the iOS version of this function, the macOS version can dynamically update the DPI scaling factor when a window is moved between HighDPI / LowDPI screens. @@ -5595,14 +5600,7 @@ _SOKOL_PRIVATE void _sapp_macos_frame(void) { _SOKOL_UNUSED(aNotification); _sapp_macos_init_cursors(); if ((_sapp.window_width == 0) || (_sapp.window_height == 0)) { - // use 4/5 of screen size as default size - NSRect screen_rect = NSScreen.mainScreen.frame; - if (_sapp.window_width == 0) { - _sapp.window_width = _sapp_roundf_gzero((screen_rect.size.width * 4.0f) / 5.0f); - } - if (_sapp.window_height == 0) { - _sapp.window_height = _sapp_roundf_gzero((screen_rect.size.height * 4.0f) / 5.0f); - } + _sapp_macos_init_default_dimensions(); } const NSUInteger style = NSWindowStyleMaskTitled | @@ -6556,8 +6554,7 @@ EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y, int mods) { } EMSCRIPTEN_KEEPALIVE void _sapp_emsc_invoke_fetch_cb(int index, int success, int error_code, _sapp_html5_fetch_callback callback, uint32_t fetched_size, void* buf_ptr, uint32_t buf_size, void* user_data) { - sapp_html5_fetch_response response; - _sapp_clear(&response, sizeof(response)); + _SAPP_STRUCT(sapp_html5_fetch_response, response); response.succeeded = (0 != success); response.error_code = (sapp_html5_fetch_error) error_code; response.file_index = index; @@ -8229,8 +8226,7 @@ _SOKOL_PRIVATE void _sapp_d3d11_create_default_render_target(void) { SOKOL_ASSERT(SUCCEEDED(hr) && _sapp.d3d11.rtv); /* common desc for MSAA and depth-stencil texture */ - D3D11_TEXTURE2D_DESC tex_desc; - _sapp_clear(&tex_desc, sizeof(tex_desc)); + _SAPP_STRUCT(D3D11_TEXTURE2D_DESC, tex_desc); tex_desc.Width = (UINT)_sapp.framebuffer_width; tex_desc.Height = (UINT)_sapp.framebuffer_height; tex_desc.MipLevels = 1; @@ -8381,8 +8377,7 @@ _SOKOL_PRIVATE bool _sapp_wgl_ext_supported(const char* ext) { _SOKOL_PRIVATE void _sapp_wgl_load_extensions(void) { SOKOL_ASSERT(_sapp.wgl.msg_dc); - PIXELFORMATDESCRIPTOR pfd; - _sapp_clear(&pfd, sizeof(pfd)); + _SAPP_STRUCT(PIXELFORMATDESCRIPTOR, pfd); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; @@ -8497,8 +8492,7 @@ _SOKOL_PRIVATE int _sapp_wgl_find_pixel_format(void) { continue; } - _sapp_gl_fbconfig u; - _sapp_clear(&u, sizeof(u)); + _SAPP_STRUCT(_sapp_gl_fbconfig, u); u.red_bits = query_results[result_red_bits_index]; u.green_bits = query_results[result_green_bits_index]; u.blue_bits = query_results[result_blue_bits_index]; @@ -8622,8 +8616,7 @@ _SOKOL_PRIVATE bool _sapp_win32_update_dimensions(void) { _SOKOL_PRIVATE void _sapp_win32_set_fullscreen(bool fullscreen, UINT swp_flags) { HMONITOR monitor = MonitorFromWindow(_sapp.win32.hwnd, MONITOR_DEFAULTTONEAREST); - MONITORINFO minfo; - _sapp_clear(&minfo, sizeof(minfo)); + _SAPP_STRUCT(MONITORINFO, minfo); minfo.cbSize = sizeof(MONITORINFO); GetMonitorInfo(monitor, &minfo); const RECT mr = minfo.rcMonitor; @@ -9057,8 +9050,7 @@ _SOKOL_PRIVATE void _sapp_win32_timing_measure(void) { #if defined(SOKOL_D3D11) // on D3D11, use the more precise DXGI timestamp if (_sapp.d3d11.use_dxgi_frame_stats) { - DXGI_FRAME_STATISTICS dxgi_stats; - _sapp_clear(&dxgi_stats, sizeof(dxgi_stats)); + _SAPP_STRUCT(DXGI_FRAME_STATISTICS, dxgi_stats); HRESULT hr = _sapp_dxgi_GetFrameStatistics(_sapp.d3d11.swap_chain, &dxgi_stats); if (SUCCEEDED(hr)) { if (dxgi_stats.SyncRefreshCount != _sapp.d3d11.sync_refresh_count) { @@ -9205,8 +9197,7 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM _sapp_win32_mouse_update(lParam); if (!_sapp.win32.mouse.tracked) { _sapp.win32.mouse.tracked = true; - TRACKMOUSEEVENT tme; - _sapp_clear(&tme, sizeof(tme)); + _SAPP_STRUCT(TRACKMOUSEEVENT, tme); tme.cbSize = sizeof(tme); tme.dwFlags = TME_LEAVE; tme.hwndTrack = _sapp.win32.hwnd; @@ -9332,8 +9323,7 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM } _SOKOL_PRIVATE void _sapp_win32_create_window(void) { - WNDCLASSW wndclassw; - _sapp_clear(&wndclassw, sizeof(wndclassw)); + _SAPP_STRUCT(WNDCLASSW, wndclassw); wndclassw.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wndclassw.lpfnWndProc = (WNDPROC) _sapp_win32_wndproc; wndclassw.hInstance = GetModuleHandleW(NULL); @@ -9594,8 +9584,7 @@ _SOKOL_PRIVATE void _sapp_win32_update_window_title(void) { } _SOKOL_PRIVATE HICON _sapp_win32_create_icon_from_image(const sapp_image_desc* desc, bool is_cursor) { - BITMAPV5HEADER bi; - _sapp_clear(&bi, sizeof(bi)); + _SAPP_STRUCT(BITMAPV5HEADER, bi); bi.bV5Size = sizeof(bi); bi.bV5Width = desc->width; bi.bV5Height = -desc->height; // NOTE the '-' here to indicate that origin is top-left @@ -9633,8 +9622,7 @@ _SOKOL_PRIVATE HICON _sapp_win32_create_icon_from_image(const sapp_image_desc* d source += 4; } - ICONINFO icon_info; - _sapp_clear(&icon_info, sizeof(icon_info)); + _SAPP_STRUCT(ICONINFO, icon_info); icon_info.fIcon = !is_cursor; icon_info.xHotspot = (DWORD) (is_cursor ? desc->cursor_hotspot_x : 0); icon_info.yHotspot = (DWORD) (is_cursor ? desc->cursor_hotspot_y : 0); @@ -12049,9 +12037,7 @@ _SOKOL_PRIVATE void _sapp_glx_swapinterval(int interval) { #endif // _SAPP_GLX _SOKOL_PRIVATE void _sapp_x11_send_event(Atom type, int a, int b, int c, int d, int e) { - XEvent event; - _sapp_clear(&event, sizeof(event)); - + _SAPP_STRUCT(XEvent, event); event.type = ClientMessage; event.xclient.window = _sapp.x11.window; event.xclient.format = 32; @@ -12415,8 +12401,7 @@ _SOKOL_PRIVATE void _sapp_x11_create_window(Visual* visual_or_null, int depth) { depth = DefaultDepth(_sapp.x11.display, _sapp.x11.screen); } _sapp.x11.colormap = XCreateColormap(_sapp.x11.display, _sapp.x11.root, visual, AllocNone); - XSetWindowAttributes wa; - _sapp_clear(&wa, sizeof(wa)); + _SAPP_STRUCT(XSetWindowAttributes, wa); const uint32_t wamask = CWBorderPixel | CWColormap | CWEventMask; wa.colormap = _sapp.x11.colormap; wa.border_pixel = 0; @@ -12963,8 +12948,7 @@ _SOKOL_PRIVATE void _sapp_x11_on_selectionnotify(XEvent* event) { } } if (_sapp.x11.xdnd.version >= 2) { - XEvent reply; - _sapp_clear(&reply, sizeof(reply)); + _SAPP_STRUCT(XEvent, reply); reply.type = ClientMessage; reply.xclient.window = _sapp.x11.xdnd.source; reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished; @@ -13031,8 +13015,7 @@ _SOKOL_PRIVATE void _sapp_x11_on_clientmessage(XEvent* event) { _sapp.x11.window, time); } else if (_sapp.x11.xdnd.version >= 2) { - XEvent reply; - _sapp_clear(&reply, sizeof(reply)); + _SAPP_STRUCT(XEvent, reply); reply.type = ClientMessage; reply.xclient.window = _sapp.x11.xdnd.source; reply.xclient.message_type = _sapp.x11.xdnd.XdndFinished; @@ -13050,8 +13033,7 @@ _SOKOL_PRIVATE void _sapp_x11_on_clientmessage(XEvent* event) { if (_sapp.x11.xdnd.version > _SAPP_X11_XDND_VERSION) { return; } - XEvent reply; - _sapp_clear(&reply, sizeof(reply)); + _SAPP_STRUCT(XEvent, reply); reply.type = ClientMessage; reply.xclient.window = _sapp.x11.xdnd.source; reply.xclient.message_type = _sapp.x11.xdnd.XdndStatus; @@ -13078,8 +13060,7 @@ _SOKOL_PRIVATE void _sapp_x11_on_selectionrequest(XEvent* event) { return; } SOKOL_ASSERT(_sapp.clipboard.buffer); - XSelectionEvent reply; - _sapp_clear(&reply, sizeof(reply)); + _SAPP_STRUCT(XSelectionEvent, reply); reply.type = SelectionNotify; reply.display = req->display; reply.requestor = req->requestor; @@ -13232,8 +13213,7 @@ _SOKOL_PRIVATE void _sapp_egl_init(void) { _SAPP_PANIC(LINUX_EGL_NO_NATIVE_VISUAL); } - XVisualInfo visual_info_template; - _sapp_clear(&visual_info_template, sizeof(visual_info_template)); + _SAPP_STRUCT(XVisualInfo, visual_info_template); visual_info_template.visualid = (VisualID)visual_id; int num_visuals; @@ -13435,8 +13415,7 @@ SOKOL_API_IMPL void sapp_run(const sapp_desc* desc) { sapp_desc sokol_main(int argc, char* argv[]) { _SOKOL_UNUSED(argc); _SOKOL_UNUSED(argv); - sapp_desc desc; - _sapp_clear(&desc, sizeof(desc)); + _SAPP_STRUCT(sapp_desc, desc); return desc; } #else @@ -13852,8 +13831,7 @@ SOKOL_API_IMPL void sapp_html5_fetch_dropped_file(const sapp_html5_fetch_request SOKOL_API_IMPL sapp_environment sapp_get_environment(void) { SOKOL_ASSERT(_sapp.valid); - sapp_environment res; - _sapp_clear(&res, sizeof(res)); + _SAPP_STRUCT(sapp_environment, res); res.defaults.color_format = sapp_color_format(); res.defaults.depth_format = sapp_depth_format(); res.defaults.sample_count = sapp_sample_count(); @@ -13882,8 +13860,7 @@ SOKOL_API_IMPL sapp_environment sapp_get_environment(void) { SOKOL_API_IMPL sapp_swapchain sapp_get_swapchain(void) { SOKOL_ASSERT(_sapp.valid); - sapp_swapchain res; - _sapp_clear(&res, sizeof(res)); + _SAPP_STRUCT(sapp_swapchain, res); res.width = sapp_width(); res.height = sapp_height(); res.color_format = sapp_color_format(); |