diff --git a/README.md b/README.md index bb36fb5..ca166ae 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,11 @@ Auto-generated [D](https://dlang.org) bindings for the [sokol headers](https://g #### Status: Experimental +#### Targets + +- Native +- Wasm (`-Dtarget=wasm32-emscripten`) + ## BUILD **Required** @@ -65,13 +70,14 @@ zig build shaders # (re)generate D bindings from shaders.
-## License and attributinos +## License and attributions This code is released under the zlib license (see `LICENSE` for info). Parts of `gen_d.py` have been copied and modified from the zig-bindings (https://github.com/floooh/sokol-zig/) and rust-bindings (https://github.com/floooh/sokol-rust/) for sokol. + The sokol headers are created by Andre Weissflog (floooh) and sokol is released under its own license here: https://github.com/floooh/sokol/blob/master/LICENSE diff --git a/build.zig b/build.zig index c49b0c5..89a2117 100644 --- a/build.zig +++ b/build.zig @@ -166,7 +166,6 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep { csrc_root ++ "sokol_time.c", csrc_root ++ "sokol_audio.c", csrc_root ++ "sokol_gl.c", - csrc_root ++ "sokol_glue.c", csrc_root ++ "sokol_debugtext.c", csrc_root ++ "sokol_shape.c", }; @@ -418,6 +417,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep { // https://github.com/ldc-developers/ldc/issues/4501 try cmds.append("-L-w"); // hide linker warnings } + if (options.target.result.isWasm()) { try cmds.append("--d-version=CarelessAlocation"); try cmds.append("-L-allow-undefined"); @@ -434,6 +434,17 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep { try cmds.append("-link-defaultlib-shared"); } + // C include path + for (lib_sokol.root_module.include_dirs.items) |include_dir| { + const path = if (include_dir == .path) + include_dir.path.getPath(b) + else if (include_dir == .path_system) + include_dir.path_system.getPath(b) + else + include_dir.path_after.getPath(b); + try cmds.append(b.fmt("-P-I{s}", .{path})); + } + // library paths for (lib_sokol.root_module.lib_paths.items) |libpath| { if (libpath.path.len > 0) // skip empty paths @@ -458,7 +469,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep { // C defines for (lib_sokol.root_module.c_macros.items) |cdefine| { if (cdefine.len > 0) // skip empty cdefines - try cmds.append(b.fmt("-Xcc=-D{s}", .{cdefine})); + try cmds.append(b.fmt("-P-D{s}", .{cdefine})); break; } @@ -555,11 +566,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep { b.step("test", "Run all tests"); if (options.target.result.isWasm()) { - const artifact = addArtifact(options, b, .{ - .name = options.name, - .target = options.target, - .optimize = options.optimize, - }); + const artifact = addArtifact(b, options); artifact.addObjectFile(.{ .path = b.fmt("{s}/examples.{s}.o", .{ objpath, options.name }) }); artifact.linkLibrary(options.artifact.?); artifact.step.dependOn(&ldc_exec.step); @@ -604,28 +611,15 @@ pub const DCompileStep = struct { artifact: ?*Build.Step.Compile = null, emsdk: ?*Build.Dependency = null, }; -pub fn addArtifact(self: DCompileStep, b: *Build, options: Build.ObjectOptions) *Build.Step.Compile { +pub fn addArtifact(b: *Build, options: DCompileStep) *Build.Step.Compile { return Build.Step.Compile.create(b, .{ .name = options.name, .root_module = .{ - .target = self.target, - .optimize = self.optimize, - .link_libc = options.link_libc, - .single_threaded = options.single_threaded, - .pic = options.pic, - .strip = options.strip, - .unwind_tables = options.unwind_tables, - .omit_frame_pointer = options.omit_frame_pointer, - .sanitize_thread = options.sanitize_thread, - .error_tracing = options.error_tracing, - .code_model = options.code_model, + .target = options.target, + .optimize = options.optimize, }, - .linkage = self.linkage, - .kind = self.kind, - .max_rss = options.max_rss, - .use_llvm = options.use_llvm, - .use_lld = options.use_lld, - .zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir, + .linkage = options.linkage, + .kind = options.kind, }); } diff --git a/build.zig.zon b/build.zig.zon index 61a9a88..d61e2e1 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -22,8 +22,8 @@ .hash = "1220f1340cd871b444021c600661f921f96091ce0815fa43008528f4844cece7e245", }, .wasmd = .{ - .url = "git+https://github.com/kassane/webassembly#800870e68be1518eeffbb2c0814e23b9543a89cb", - .hash = "12203c2350903f04a07f746b448abb22824a452e8d81500b2fa11a0c3a5ea896b3f6", + .url = "git+https://github.com/kassane/wasmd#067e21e29aea24ec936d6c6b859f7c9a79894f78", + .hash = "122006f8e097d5bd627f26d2aa3b397d92b400c786f13396c47afed66fed1e8ef2c1", }, }, } diff --git a/src/sokol/app.d b/src/sokol/app.d index b5948cc..a7bdce3 100644 --- a/src/sokol/app.d +++ b/src/sokol/app.d @@ -164,7 +164,7 @@ enum AndroidTooltype { } extern(C) struct Touchpoint { - size_t identifier; + ulong identifier; float pos_x; float pos_y; AndroidTooltype android_tooltype; diff --git a/src/sokol/c/sokol_app.h b/src/sokol/c/sokol_app.h index ea8320a..50a1421 100644 --- a/src/sokol/c/sokol_app.h +++ b/src/sokol/c/sokol_app.h @@ -8249,8 +8249,8 @@ _SOKOL_PRIVATE bool _sapp_android_touch_event(const AInputEvent* e) { for (int32_t i = 0; i < _sapp.event.num_touches; i++) { sapp_touchpoint* dst = &_sapp.event.touches[i]; dst->identifier = (uintptr_t)AMotionEvent_getPointerId(e, (size_t)i); - dst->pos_x = (AMotionEvent_getRawX(e, (size_t)i) / _sapp.window_width) * _sapp.framebuffer_width; - dst->pos_y = (AMotionEvent_getRawY(e, (size_t)i) / _sapp.window_height) * _sapp.framebuffer_height; + dst->pos_x = (AMotionEvent_getX(e, (size_t)i) / _sapp.window_width) * _sapp.framebuffer_width; + dst->pos_y = (AMotionEvent_getY(e, (size_t)i) / _sapp.window_height) * _sapp.framebuffer_height; dst->android_tooltype = (sapp_android_tooltype) AMotionEvent_getToolType(e, (size_t)i); if (action == AMOTION_EVENT_ACTION_POINTER_DOWN || action == AMOTION_EVENT_ACTION_POINTER_UP) { diff --git a/src/sokol/c/sokol_gfx.h b/src/sokol/c/sokol_gfx.h index 740de5c..a8749d9 100644 --- a/src/sokol/c/sokol_gfx.h +++ b/src/sokol/c/sokol_gfx.h @@ -13291,9 +13291,6 @@ _SOKOL_PRIVATE void _sg_wgpu_init_caps(void) { _sg.features.mrt_independent_blend_state = true; _sg.features.mrt_independent_write_mask = true; - // FIXME: in webgpu.h, wgpuDeviceGetLimits() has a boolean return value, but - // the JS shim doesn't actually return anything - // (see: https://github.com/emscripten-core/emscripten/issues/20278) wgpuDeviceGetLimits(_sg.wgpu.dev, &_sg.wgpu.limits); const WGPULimits* l = &_sg.wgpu.limits.limits; diff --git a/src/sokol/c/sokol_glue.c b/src/sokol/c/sokol_glue.c deleted file mode 100644 index 95219dd..0000000 --- a/src/sokol/c/sokol_glue.c +++ /dev/null @@ -1,7 +0,0 @@ -#if defined(IMPL) -#define SOKOL_GLUE_IMPL -#endif -#include "sokol_defines.h" -#include "sokol_app.h" -#include "sokol_gfx.h" -#include "sokol_glue.h" \ No newline at end of file diff --git a/src/sokol/c/sokol_glue.h b/src/sokol/c/sokol_glue.h deleted file mode 100644 index 7216f6a..0000000 --- a/src/sokol/c/sokol_glue.h +++ /dev/null @@ -1,136 +0,0 @@ -#if defined(SOKOL_IMPL) && !defined(SOKOL_GLUE_IMPL) -#define SOKOL_GLUE_IMPL -#endif -#ifndef SOKOL_GLUE_INCLUDED -/* - sokol_glue.h -- glue helper functions for sokol headers - - Project URL: https://github.com/floooh/sokol - - Do this: - #define SOKOL_IMPL or - #define SOKOL_GLUE_IMPL - before you include this file in *one* C or C++ file to create the - implementation. - - ...optionally provide the following macros to override defaults: - - SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) - SOKOL_GLUE_API_DECL - public function declaration prefix (default: extern) - SOKOL_API_DECL - same as SOKOL_GLUE_API_DECL - SOKOL_API_IMPL - public function implementation prefix (default: -) - - If sokol_glue.h is compiled as a DLL, define the following before - including the declaration or implementation: - - SOKOL_DLL - - On Windows, SOKOL_DLL will define SOKOL_GLUE_API_DECL as __declspec(dllexport) - or __declspec(dllimport) as needed. - - OVERVIEW - ======== - The sokol core headers should not depend on each other, but sometimes - it's useful to have a set of helper functions as "glue" between - two or more sokol headers. - - This is what sokol_glue.h is for. Simply include the header after other - sokol headers (both for the implementation and declaration), and - depending on what headers have been included before, sokol_glue.h - will make available "glue functions". - - PROVIDED FUNCTIONS - ================== - - - if sokol_app.h and sokol_gfx.h is included: - - sg_context_desc sapp_sgcontext(void): - - Returns an initialized sg_context_desc function initialized - by calling sokol_app.h functions. - - LICENSE - ======= - zlib/libpng license - - Copyright (c) 2018 Andre Weissflog - - This software is provided 'as-is', without any express or implied warranty. - In no event will the authors be held liable for any damages arising from the - use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software in a - product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not - be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*/ -#define SOKOL_GLUE_INCLUDED - -#if defined(SOKOL_API_DECL) && !defined(SOKOL_GLUE_API_DECL) -#define SOKOL_GLUE_API_DECL SOKOL_API_DECL -#endif -#ifndef SOKOL_GLUE_API_DECL -#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GLUE_IMPL) -#define SOKOL_GLUE_API_DECL __declspec(dllexport) -#elif defined(_WIN32) && defined(SOKOL_DLL) -#define SOKOL_GLUE_API_DECL __declspec(dllimport) -#else -#define SOKOL_GLUE_API_DECL extern -#endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(SOKOL_GFX_INCLUDED) && defined(SOKOL_APP_INCLUDED) -SOKOL_GLUE_API_DECL sg_context_desc sapp_sgcontext(void); -#endif - -#ifdef __cplusplus -} /* extern "C" */ -#endif -#endif /* SOKOL_GLUE_INCLUDED */ - -/*-- IMPLEMENTATION ----------------------------------------------------------*/ -#ifdef SOKOL_GLUE_IMPL -#define SOKOL_GLUE_IMPL_INCLUDED (1) -#include /* memset */ - -#ifndef SOKOL_API_IMPL - #define SOKOL_API_IMPL -#endif - -#if defined(SOKOL_GFX_INCLUDED) && defined(SOKOL_APP_INCLUDED) -SOKOL_API_IMPL sg_context_desc sapp_sgcontext(void) { - sg_context_desc desc; - memset(&desc, 0, sizeof(desc)); - desc.color_format = (sg_pixel_format) sapp_color_format(); - desc.depth_format = (sg_pixel_format) sapp_depth_format(); - desc.sample_count = sapp_sample_count(); - desc.metal.device = sapp_metal_get_device(); - desc.metal.renderpass_descriptor_cb = sapp_metal_get_renderpass_descriptor; - desc.metal.drawable_cb = sapp_metal_get_drawable; - desc.d3d11.device = sapp_d3d11_get_device(); - desc.d3d11.device_context = sapp_d3d11_get_device_context(); - desc.d3d11.render_target_view_cb = sapp_d3d11_get_render_target_view; - desc.d3d11.depth_stencil_view_cb = sapp_d3d11_get_depth_stencil_view; - desc.wgpu.device = sapp_wgpu_get_device(); - desc.wgpu.render_view_cb = sapp_wgpu_get_render_view; - desc.wgpu.resolve_view_cb = sapp_wgpu_get_resolve_view; - desc.wgpu.depth_stencil_view_cb = sapp_wgpu_get_depth_stencil_view; - return desc; -} -#endif - -#endif /* SOKOL_GLUE_IMPL */ diff --git a/src/sokol/glue.d b/src/sokol/glue.d index 5adecde..e42a333 100644 --- a/src/sokol/glue.d +++ b/src/sokol/glue.d @@ -1,9 +1,30 @@ -// machine generated, do not edit - module sokol.glue; + import sg = sokol.gfx; +import sapp = sokol.app; -extern(C) sg.ContextDesc sapp_sgcontext() @system @nogc nothrow; -sg.ContextDesc context() @trusted @nogc nothrow { - return sapp_sgcontext(); +extern(C) sg.ContextDesc context() @trusted @nogc nothrow { + sg.ContextDesc ctx = { + color_format: sapp.colorFormat(), + depth_format: sapp.depthFormat(), + sample_count: sapp.sampleCount(), + metal: { + device: sapp.metalGetDevice(), + renderpass_descriptor_cb: &sapp.sapp_metal_get_renderpass_descriptor, + drawable_cb: &sapp.sapp_metal_get_drawable, + }, + d3d11: { + device: sapp.d3d11GetDevice(), + device_context: sapp.d3d11GetDeviceContext(), + render_target_view_cb: &sapp.sapp_d3d11_get_render_target_view, + depth_stencil_view_cb: &sapp.sapp_d3d11_get_depth_stencil_view, + }, + wgpu: { + device: sapp.wgpuGetDevice(), + render_view_cb: &sapp.sapp_wgpu_get_render_view, + resolve_view_cb: &sapp.sapp_wgpu_get_resolve_view, + depth_stencil_view_cb: &sapp.sapp_wgpu_get_depth_stencil_view, + }, + }; + return ctx; }