Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bgfx_init() second parameter needs to be casted #368

Closed
malobre opened this issue Jan 25, 2018 · 4 comments
Closed

bgfx_init() second parameter needs to be casted #368

malobre opened this issue Jan 25, 2018 · 4 comments

Comments

@malobre
Copy link

malobre commented Jan 25, 2018

Environment

  • LWJGL version: 3.1.5
  • LWJGL build #: 1
  • Java version: 9 (I'm programming in Kotlin 1.2.20)
  • Platform: Windows
  • Module: BGFX

Description

/** PCI */
public static final short
    BGFX_PCI_ID_NONE                = 0x0,
    BGFX_PCI_ID_SOFTWARE_RASTERIZER = 0x1,
    BGFX_PCI_ID_AMD                 = 0x1002,
    BGFX_PCI_ID_INTEL               = (short)0x8086,
    BGFX_PCI_ID_NVIDIA              = 0x10DE;
/**
 * Initializes bgfx library.
 *
 * @param _type      select rendering backend. When set to {@link #BGFX_RENDERER_TYPE_COUNT RENDERER_TYPE_COUNT}, default rendering backend will be selected. One of:<br><table><tr><td>{@link #BGFX_RENDERER_TYPE_NOOP RENDERER_TYPE_NOOP}</td><td>{@link #BGFX_RENDERER_TYPE_DIRECT3D9 RENDERER_TYPE_DIRECT3D9}</td><td>{@link #BGFX_RENDERER_TYPE_DIRECT3D11 RENDERER_TYPE_DIRECT3D11}</td></tr><tr><td>{@link #BGFX_RENDERER_TYPE_DIRECT3D12 RENDERER_TYPE_DIRECT3D12}</td><td>{@link #BGFX_RENDERER_TYPE_GNM RENDERER_TYPE_GNM}</td><td>{@link #BGFX_RENDERER_TYPE_METAL RENDERER_TYPE_METAL}</td></tr><tr><td>{@link #BGFX_RENDERER_TYPE_OPENGLES RENDERER_TYPE_OPENGLES}</td><td>{@link #BGFX_RENDERER_TYPE_OPENGL RENDERER_TYPE_OPENGL}</td><td>{@link #BGFX_RENDERER_TYPE_VULKAN RENDERER_TYPE_VULKAN}</td></tr><tr><td>{@link #BGFX_RENDERER_TYPE_COUNT RENDERER_TYPE_COUNT}</td></tr></table>
 * @param _vendorId  vendor PCI id. If set to {@link #BGFX_PCI_ID_NONE PCI_ID_NONE} it will select the first device.
 * @param _deviceId  device id. If set to 0 it will select first device, or device with matching id.
 * @param _callback  provide application specific callback interface
 * @param _allocator custom allocator. When custom allocator is not specified, library uses default CRT allocator. The library assumes custom allocator is thread safe.
 * 
 * @return `true` if initialization was successful
 */
@NativeType("bool")
public static boolean bgfx_init(@NativeType("bgfx_renderer_type_t") int _type, @NativeType("uint16_t") int _vendorId, @NativeType("uint16_t") int _deviceId, @NativeType("bgfx_callback_interface_t *") BGFXCallbackInterface _callback, @NativeType("bgfx_allocator_interface_t *") BGFXAllocatorInterface _allocator) {
    return nbgfx_init(_type, (short)_vendorId, (short)_deviceId, memAddressSafe(_callback), memAddressSafe(_allocator));
}

Second parameter of bgfx_init(), vendorId cannot be set to BGFX_PCI_ID_NONE or BGFX_PCI_ID_* without casting the constant to an Int.

@malobre
Copy link
Author

malobre commented Jan 25, 2018

Seems to be the same with bgfx_set_view_clear() and its second parameter, short constants needs to be casted to ints

@Spasi
Copy link
Member

Spasi commented Jan 25, 2018

Indeed, there is no reason to map _vendorId to an int. The same applies to the _side parameter of bgfx_update_texture_cube.

The situation with bgfx_set_view_clear and other functions with bitfield parameters (e.g. buffer creation flags) is more complicated. As soon as you need to || two or more flags together, mapping the parameter to a short becomes painful. In both Java and Kotlin. For example in Kotlin you'd need to do:

(BGFX_CLEAR_COLOR.toInt() or BGFX_CLEAR_DEPTH.toInt()).toShort()

There is an or that works on Short in kotlin.experimental, but it would still be painful in Java (and Java usability takes priority over Kotlin).

May I suggest a simple extension property:

val Short.i get() = this.toInt()

Then you can do:

BGFX_CLEAR_COLOR.i or BGFX_CLEAR_DEPTH.i // or
(BGFX_CLEAR_COLOR or BGFX_CLEAR_DEPTH).i // with kotlin.experimental.or

@malobre
Copy link
Author

malobre commented Jan 26, 2018

I'm currently using toInt() but isn't it possible to just declare the constants that are used in bitfield parameters as Ints insteads of Shorts ? Or am I missing something ?

@Spasi Spasi closed this as completed in 97e6cec Jan 27, 2018
@Spasi
Copy link
Member

Spasi commented Jan 27, 2018

@malobre Thanks!

If you have more suggestions, please post in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants