emoon / bgfx-rs Goto Github PK
View Code? Open in Web Editor NEWRust wrapper for BGFX
License: BSD 2-Clause "Simplified" License
Rust wrapper for BGFX
License: BSD 2-Clause "Simplified" License
It seems like macros like BGFX_STATE_BLEND_FUNC
are missing in both bgfx_sys
and bgfx_rs
while being mentioned in the documentation at StateBlendFlags e.g.
bgfx-rs = "0.13"
bgfx-sys = "0.9.0"
I encountered some crashes/invalid rendering when using multiple VertexLayoutBuilder
. I narrowed this down to VertexLayoutBuilder.end()
not being called by me, but in doing so I found numerous issues with VertexLayoutBuilder
:
begin
, add
and end
mutate the builder but don't take a mutable referenceVertexLayoutBuilder
is able to be passed to various APIs without end
being called. A new type safely encapsulating this requirement would be preferred.begin
and add
both return &Self
, meaning you can't assign their result to a variable. AFAIK Self
is idiomatic.I'm working on a PR for this.
I am getting this error compiling on Windows in debug:
libbgfx_sys-3a085ea111dd24ba.rlib(image.o) : error LNK2019: unresolved external symbol "bool __cdecl astc_codec::ASTCDecompressToRGBA(unsigned char const *,unsigned __int64,unsigned __int64,unsigned __int64,enum astc_codec::FootprintType,unsigned char *,unsigned __int64,unsigned __int64)" (?ASTCDecompressToRGBA@astc_codec@@YA_NPEBE_K11W4FootprintType@1@PEAE11@Z) referenced in function "void __cdecl bimg::imageDecodeToRgba8(struct bx::AllocatorI *,void *,void const *,unsigned int,unsigned int,unsigned int,enum bimg::TextureFormat::Enum)" (?imageDecodeToRgba8@bimg@@YAXPEAUAllocatorI@bx@@PEAXPEBXIIIW4Enum@TextureFormat@1@@Z)
I am able to compile and run in with --release
. I am using CLion, but the error also occurs when I try and build directly with Cargo. Steps I've taken to try and fix it:
bgfx documentation for the Limits structure does not match the definition in
Lines 1640 to 1649 in 49f8ea1
bgfx::get_caps()
Lines 1552 to 1596 in 49f8ea1
I think Caps::limits
should be changed from Limits
to CapsLimits
, as the CapsLimits struct reflects what the documentation says bgfx::get_caps()
should return.
Could be nice to provide examples for iOS/Android boilerplate code to get started.
I'm using the most recent stable build of Rust, with the MSVC compiler. When I try to compile my own project or one of the examples, it fails with error code 1120, and complains about several unresolved external symbols. Is there a special setup step I haven't noticed? This error occurs with all 3 examples, though 2 of them have a typo in the code that has to be fixed first.
If the last parameter (destroy_shaders) to bgfx::create_program(&vsh, &fsh, false)
is set to true
, I'm running into a segmentation fault; When set to false it is working correctly, but as far as I'm aware the shader objects are not needed anynore after the program is created.
On Windows: "(exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)"
On Linux: "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)"
The debugger seems to indicate the problem happens in GL_CHECK(glUniform1iv(loc, num, data) );
on line 4427 in renderer_gl.cpp which is called from bgfx_rs::static_lib::frame()
.
Sample repository: https://github.com/const-volatile/bgfx-imgui-glfw-rs-example
Toolchain: rustc 1.62.0-nightly (52ca603da 2022-04-12)
If changing line 60 here: https://github.com/const-volatile/bgfx-imgui-glfw-rs-example/blob/main/src/imgui_bgfx_renderer/mod.rs the segmentation fault can be observed.
Could this be an issue with bgfx-rs?
Hi,
I'm unable to build this crate on my MacOS setup. I attached the logs below but I believe it has to do with this issue:
bx/include/bx/bx.h:34:2: error: unknown type name 'constexpr'
. It seems like on MacOS you may need to specify support for c++11 -std=c++11
.
warning: ^
warning: bgfx/include/bgfx/bgfx.h:20:29: note: expanded from macro 'BGFX_INVALID_HANDLE'
warning: #define BGFX_INVALID_HANDLE { bgfx::kInvalidHandle }
warning: ^
warning: bgfx/include/bgfx/bgfx.h:1329:41: error: expected expression
warning: , VertexLayoutHandle _layoutHandle = BGFX_INVALID_HANDLE
warning: ^
warning: bgfx/include/bgfx/bgfx.h:20:29: note: expanded from macro 'BGFX_INVALID_HANDLE'
warning: #define BGFX_INVALID_HANDLE { bgfx::kInvalidHandle }
warning: ^
warning: bgfx/include/bgfx/bgfx.h:1969:32: error: expected expression
warning: bool init(const Init& _init = {});
warning: ^
warning: 3 warnings and 20 errors generated.
warning: bgfx/include/bgfx/bgfx.h:3718:40: error: expected expression
warning: , VertexLayoutHandle _layoutHandle = BGFX_INVALID_HANDLE
warning: ^
warning: bgfx/include/bgfx/bgfx.h:20:29: note: expanded from macro 'BGFX_INVALID_HANDLE'
warning: #define BGFX_INVALID_HANDLE { bgfx::kInvalidHandle }
warning: ^
warning: bgfx/include/bgfx/bgfx.h:3749:40: error: expected expression
warning: , VertexLayoutHandle _layoutHandle = BGFX_INVALID_HANDLE
warning: ^
warning: bgfx/include/bgfx/bgfx.h:20:29: note: expanded from macro 'BGFX_INVALID_HANDLE'
warning: #define BGFX_INVALID_HANDLE { bgfx::kInvalidHandle }
warning: ^
warning: bgfx/include/bgfx/bgfx.h:3780:40: error: expected expression
warning: , VertexLayoutHandle _layoutHandle = BGFX_INVALID_HANDLE
warning: ^
warning: bgfx/include/bgfx/bgfx.h:20:29: note: expanded from macro 'BGFX_INVALID_HANDLE'
warning: #define BGFX_INVALID_HANDLE { bgfx::kInvalidHandle }
warning: ^
warning: In file included from bgfx/src/shader.cpp:6:
warning: In file included from bgfx/src/bgfx_p.h:23:
warning: In file included from bgfx/src/config.h:9:
warning: bx/include/bx/bx.h:34:2: error: unknown type name 'constexpr'
warning: constexpr int32_t kExitSuccess = 0;
warning: ^
warning: bx/include/bx/bx.h:34:19: error: expected ';' after top level declarator
warning: constexpr int32_t kExitSuccess = 0;
warning: ^
warning: ;
warning: bx/include/bx/bx.h:35:2: error: unknown type name 'constexpr'
warning: constexpr int32_t kExitFailure = 1;
warning: ^
warning: bx/include/bx/bx.h:35:19: error: expected ';' after top level declarator
warning: constexpr int32_t kExitFailure = 1;
warning: ^
warning: ;
warning: bx/include/bx/bx.h:39:2: error: unknown type name 'constexpr'
warning: constexpr bool isTriviallyCopyable();
warning: ^
warning: bx/include/bx/bx.h:54:2: error: unknown type name 'constexpr'
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:54:12: warning: variable templates are a C++14 extension [-Wc++14-extensions]
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:54:14: error: expected ';' at end of declaration
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: ;
warning: bx/include/bx/bx.h:54:25: error: unknown type name 'Ty'
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:54:39: error: unknown type name 'Ty'
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:54:15: error: C++ requires a type specifier for all declarations
warning: constexpr Ty min(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:58:2: error: unknown type name 'constexpr'
warning: constexpr Ty max(const Ty& _a, const Ty& _b);
warning: ^
warning: bx/include/bx/bx.h:58:12: warning: variable templates are a C++14 extension [-Wc++14-extensions]
warning: constexpr Ty max(const Ty& _a, const Ty& _b);
warning: ^
warning: fatal error: too many errors emitted, stopping now [-ferror-limit=]
warning: 3 warnings and 20 errors generated.
warning: 3 warnings and 20 errors generated.
warning: 3 warnings and 20 errors generated.
warning: 3 warnings and 20 errors generated.
error: failed to run custom build command for `bgfx-sys v0.4.0`
Caused by:
process didn't exit successfully: `/Users/tino/Workspace/game/target/release/build/bgfx-sys-d765467cb1cd2222/build-script-build` (exit status: 1)
--- stdout
TARGET = Some("aarch64-apple-darwin")
OPT_LEVEL = Some("3")
HOST = Some("aarch64-apple-darwin")
CC_aarch64-apple-darwin = None
CC_aarch64_apple_darwin = None
HOST_CC = None
CC = None
CFLAGS_aarch64-apple-darwin = None
CFLAGS_aarch64_apple_darwin = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = None
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bx/src/amalgamated.o" "-c" "bx/src/amalgamated.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bgfx/src/topology.o" "-c" "bgfx/src/topology.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bgfx/src/bgfx.o" "-c" "bgfx/src/bgfx.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bimg/src/image_decode.o" "-c" "bimg/src/image_decode.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bimg/src/image_gnf.o" "-c" "bimg/src/image_gnf.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bgfx/src/debug_renderdoc.o" "-c" "bgfx/src/debug_renderdoc.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bimg/src/image_cubemap_filter.o" "-c" "bimg/src/image_cubemap_filter.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bimg/src/image.o" "-c" "bimg/src/image.cpp"
running: "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bgfx/src/vertexlayout.o" "-c" "bgfx/src/vertexlayout.cpp"
--- stderr
error occurred: Command "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "bx/include/compat/osx" "-I" "bgfx/3rdparty/khronos" "-I" "bgfx/3rdparty" "-I" "bgfx/include" "-I" "bx/include" "-I" "bx/3rdparty" "-I" "bimg/include" "-I" "bimg/3rdparty" "-I" "bimg/3rdparty/iqa/include" "-I" "bimg/3rdparty/astc-codec/include" "-I" "bimg/3rdparty/tinyexr/deps/miniz" "-Wall" "-Wextra" "-DBGFX_CONFIG_RENDERER_WEBGPU=0" "-DBGFX_CONFIG_RENDERER_GNM=0" "-DBIMG_DECODE_ASTC=0" "-DBGFX_CONFIG_RENDERER_VULKAN=0" "-DBGFX_CONFIG_RENDERER_METAL=1" "-o" "/Users/tino/Workspace/game/target/release/build/bgfx-sys-675d35d16d980e93/out/bx/src/amalgamated.o" "-c" "bx/src/amalgamated.cpp" with args "cc" did not execute successfully (status code exit status: 1).
First of all, Thank you very much for maintaining these rust bindings to bgfx - It's a great addition to the evolving rust ecosystem.
However, I'm facing one problem whenever I call bgfx:shutdown()
from my application there is an Segmentation fault happening. I was able to debug the root-cause up as following:
BX_ALIGNED_DELETE(g_allocator, ctx, Context::kAlignment);
in bgfx.cpp::shutdown()
is invoking the destructor of the Context class (bgfx_p.h). The Context class contains a list of TextureRefs m_textureHandle
in which the destructor for each element is invoked explicitly. Each TextureRef has an bx::String
m_name
for which the destructor is invoked explicitly. This destructor will call the clear()
method in bx::StringT
, which will call to BX_FREE(*AllocatorT, const_cast<char*>(m_ptr) );
.
This will call to bx::free
and from there to bgfx::AllocatorStub::realloc
. From there free
is called from the clib (bgfx.cpp, Line 192, ::free(_ptr);
).
Now here I do encounter the segmentation fault; even at this time my only texture is already released (automatically once its owner left the scope). There is still an fair chance, that the issue is due to an mistake in my application code - I double-checked multiple times and was not able to spot what could be the reason.
Below is a Minimal, Reproducible Example (the shaders are embedded, but there needs to be an file "test.png" in the working directory):
use bgfx::*;
use bgfx_rs::bgfx;
use glfw::{Action, Key, Window, WindowMode};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
const WIDTH: usize = 1280;
const HEIGHT: usize = 720;
fn load_image (filepath : &str) -> bgfx::Texture {
let mut file = std::fs::File::open(filepath).unwrap();
let img = stb::image::stbi_load_from_reader(&mut file, stb::image::Channels::RgbAlpha).unwrap();
return bgfx::create_texture_2d(img.0.width as u16, img.0.height as u16, false, 1, bgfx::TextureFormat::RGBA8, 0, &Memory::copy(&img.1.as_slice()));
}
fn load_shader() -> bgfx::Program {
let vsh_bytes: [u8;341] = [
0x56, 0x53, 0x48, 0x06, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x1e, 0x3e, 0x3c, 0x01, 0x00, 0x0f, 0x75, // VSH.....o.><...u
0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x04, 0x01, // _modelViewProj..
0x00, 0x00, 0x01, 0x00, 0x2c, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, // ....,...attribut
0x65, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x61, 0x5f, 0x70, // e highp vec3 a_p
0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, // osition;.attribu
0x74, 0x65, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, // te highp vec2 a_
0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, // texcoord0;.varyi
0x6e, 0x67, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x76, 0x5f, // ng highp vec2 v_
0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, // texcoord0;.unifo
0x72, 0x6d, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, // rm highp mat4 u_
0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x3b, 0x0a, 0x76, // modelViewProj;.v
0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, // oid main ().{.
0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // highp vec4 tmpva
0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, // r_1;. tmpvar_1.
0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // w = 1.0;. tmpva
0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, // r_1.xyz = a_posi
0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, // tion;. gl_Posit
0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, // ion = (u_modelVi
0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, // ewProj * tmpvar_
0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, // 1);. v_texcoord
0x30, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, // 0 = a_texcoord0;
0x0a, 0x7d, 0x0a, 0x0a, 0x00, // .}...
];
let fsh_bytes: [u8;215] = [
0x46, 0x53, 0x48, 0x06, 0x6f, 0x1e, 0x3e, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x73, // FSH.o.><.......s
0x5f, 0x74, 0x65, 0x78, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0xb3, // _texColor.......
0x00, 0x00, 0x00, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, // ...varying highp
0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, // vec2 v_texcoord
0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, // 0;.uniform sampl
0x65, 0x72, 0x32, 0x44, 0x20, 0x73, 0x5f, 0x74, 0x65, 0x78, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, // er2D s_texColor;
0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, // .void main ().{.
0x20, 0x20, 0x6c, 0x6f, 0x77, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, // lowp vec4 tmpv
0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, // ar_1;. tmpvar_1
0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x32, 0x44, 0x20, 0x28, 0x73, 0x5f, // = texture2D (s_
0x74, 0x65, 0x78, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, // texColor, v_texc
0x6f, 0x6f, 0x72, 0x64, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, // oord0);. gl_Fra
0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, // gColor = tmpvar_
0x31, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // 1;.}...
];
let vsh = bgfx::create_shader(&Memory::copy(&vsh_bytes));
let fsh = bgfx::create_shader(&Memory::copy(&fsh_bytes));
bgfx::create_program(&vsh, &fsh, true)
}
fn draw_image(shader : &bgfx::Program, texture: &bgfx::Texture){
let id : bgfx::ViewId = 0x00;
let builder = bgfx::VertexLayoutBuilder::new();
builder.begin(get_render_type());
builder.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float, AddArgs::default());
builder.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, AddArgs::default());
builder.end();
let mut tvb = bgfx::TransientVertexBuffer::new();
if bgfx::get_avail_transient_vertex_buffer(4, &builder) == 4 {
bgfx::alloc_transient_vertex_buffer(&mut tvb, 4, &builder);
let vertices : [f32; 16] = [
0.0f32, 0.0f32, 0.0f32, 0.0f32,
1024.0f32, 0.0f32, 1024.0f32, 0.0f32,
0.0f32, 1024.0f32, 0.0f32, 1024.0f32,
1024.0f32, 1024.0f32, 1024.0f32, 1024.0f32,
];
unsafe {
std::ptr::copy_nonoverlapping(vertices.as_ptr() as *const u8, tvb.data as *mut u8, std::mem::size_of::<[f32; 16]>()); //this line causes SIGSEGV
}
bgfx::set_transient_vertex_buffer(0, &tvb, 0, 4);
bgfx::set_state(StatePtFlags::TRISTRIP.bits() | StateWriteFlags::RGB.bits() | StateDepthTestFlags::LESS.bits() | StateBlendFlags::SRC_ALPHA.bits() | StateFlags::MSAA.bits(), 0);
let mvp = [
0.0f32, 0.0f32, 0.0f32, 0.0f32,
0.0f32, 0.0f32, 0.0f32, 0.0f32,
0.0f32, 0.0f32, 0.0f32, 0.0f32,
0.0f32, 0.0f32, 0.0f32, 0.0f32,
];
bgfx::set_uniform(&bgfx::Uniform::create("modelViewProj", bgfx::UniformType::Mat4, 1), &mvp.as_slice(), 1);
bgfx::set_texture(0, &bgfx::Uniform::create("texColor", bgfx::UniformType::Sampler, 1), &texture, u32::MAX);
bgfx::submit(id, &shader, bgfx_rs::static_lib::SubmitArgs{ depth: 1, flags: DiscardFlags::NONE.bits() });
}
}
#[cfg(target_os = "linux")]
fn update_platform_handle(pd: &mut PlatformData, window: &Window) {
match window.raw_window_handle() {
RawWindowHandle::Xlib(x_data) => {
pd.ndt = x_data.display;
pd.nwh = x_data.window as *mut core::ffi::c_void;
}
_ => panic!("Unsupported window type"),
}
}
#[cfg(target_os = "windows")]
fn update_platform_handle(pd: &mut PlatformData, window: &Window) {
match window.raw_window_handle() {
RawWindowHandle::Windows(data) => {
pd.nwh = data.hwnd as *mut core::ffi::c_void;
}
_ => panic!("Unsupported window type"),
}
}
#[cfg(target_os = "linux")]
fn get_render_type() -> RendererType {
RendererType::OpenGL
}
#[cfg(not(target_os = "linux"))]
fn get_render_type() -> RendererType {
RendererType::Count
}
fn main() {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi));
let (mut window, events) = glfw
.create_window(
WIDTH as _,
HEIGHT as _,
"App - ESC to close",
glfw::WindowMode::Windowed,
)
.expect("Failed to create GLFW window.");
window.set_key_polling(true);
let mut pd = bgfx::PlatformData::new();
update_platform_handle(&mut pd, &window);
bgfx::set_platform_data(&pd);
let mut init = Init::new();
init.type_r = get_render_type();
init.resolution.width = WIDTH as u32;
init.resolution.height = HEIGHT as u32;
init.resolution.reset = ResetFlags::VSYNC.bits();
init.platform_data = pd;
if !bgfx::init(&init) {
panic!("failed to init bgfx");
}
bgfx::set_debug(DebugFlags::TEXT.bits());
let mut old_size = (0, 0);
{
let shader = load_shader();
let texture = load_image("test.png");
while !window.should_close() {
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) {
if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
window.set_should_close(true)
}
}
let size = window.get_framebuffer_size();
if old_size != size {
bgfx::reset(size.0 as _, size.1 as _, ResetArgs::default());
old_size = size;
}
bgfx::set_view_rect(0, 0, 0, size.0 as _, size.1 as _);
bgfx::set_view_clear(
0,
ClearFlags::COLOR.bits() | ClearFlags::DEPTH.bits(),
SetViewClearArgs {
rgba: 0x103030ff,
..Default::default()
},
);
bgfx::touch(0);
draw_image(&shader, &texture);
bgfx::frame(false);
}
}
bgfx::shutdown();
}
The dependencies are:
[dependencies]
bgfx-rs = "0.8"
glfw = "0.41"
raw-window-handle = "0.3"
stb = "0.3.2"
Either closing the window by clicking the X or pressing ESC will result in the Segmentation Fault.
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Is there any problem in my code or could there be an problem with the bgfx rust bindings itself?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.