thenumbernine / lua-ffi-bindings

FFI bindings for LuaJIT
MIT License
43 stars 7 forks source link

question about imgui binding #1

Closed sonoro1234 closed 7 years ago

sonoro1234 commented 7 years ago

Hi,

I was making a luajit binding when I saw yours. I cant understand why cimgui dont return ImVec2 from functions. Nobody answered my question there, perhaps you would be so kind.

igGetCursorScreenPos for example expects an ImVec2 pointer instead of returning an ImVec2 ImGui expected behaviour can be provided as in code below (which is tedious for the binding writer)

function M.GetCursorScreenPos()
    local pos = ImVec2_p()
    lib.igGetCursorScreenPos(pos)
    return pos[0]
end

Is there any reason for this design?

thenumbernine commented 7 years ago

If you go back in time about a decade, during the 32bit days, passing a pointer would be faster than passing a struct of two floats. This is probably why the author decided to write it in this fashion. In fact, for larger structs, I'd still bet it is faster. But since back then, with 64bit, MMX, SSE, etc, passing a small struct of floats is actually faster. There are cases for each, and performance varies with use cases. In my case I chose to change his return-by-reference to a return-by-value because I thought it was an easier thing to do.

Thanks for the interest in my binding library. Tell me if you have any trouble getting it up and running. You need the ffi/imgui.lua https://github.com/thenumbernine/lua-ffi-bindings/blob/master/imgui.lua from my lua-ffi-bindings https://github.com/thenumbernine/lua-ffi-bindings repo, then you also need to build imgui https://github.com/ocornut/imgui and cimgui https://github.com/Extrawurst/cimgui together into the same dynamic library. I could provide my modified Makefile for it if you want.

Tell me if you have any other questions.

Thanks, Chris

On Sun, Dec 25, 2016 at 11:05 AM, Victor Bombi notifications@github.com wrote:

Hi,

I was making a luajit binding when I saw yours. I cant understand why cimgui dont return ImVec2 from functions. Nobody answered my question there, perhaps you would be so kind.

igGetCursorScreenPos for example expects an ImVec2 pointer instead of returning an ImVec2 ImGui expected behaviour can be provided as in code below (which is tedious for the binding writer)

function M.GetCursorScreenPos() local pos = ImVec2_p() lib.igGetCursorScreenPos(pos) return pos[0] end

Is there any reason for this design?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/thenumbernine/lua-ffi-bindings/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AEC3Tl-Et1XDwH8P7Kk-mwVZ2hIT0o15ks5rLjHfgaJpZM4LVXqq .

sonoro1234 commented 7 years ago

Hello Chris,

Thanks for answering. I have the feeling that when luajit does return-by-value itself as in:

pos = lib.GetMouseDragDeltaVersionImgui( 0, -1)

the work is the same that has to be done in:

function M.GetMouseDragDelta(button , lock_threshold)
    local pos = ImVec2_p()
    lib.igGetMouseDragDelta(pos, button or 0, lock_threshold or -1)
    return pos[0]
end

pos = M.GetMouseDragDleta(0, -1)

allocation and afterwards indirection and copy to the ImVec2 named pos.

But the first option would be easier to code and simpler.

victor

thenumbernine commented 7 years ago

Are you talking about adding a new C function to imgui/cimgui named " GetMouseDragDeltaVersionImgui" and then binding to it using ffi.cdef? Yeah, in that case, you will have a stack allocation of the ImVec2 within the c/imgui code until it gets to LuaJIT, then LuaJIT will heap allocate the ImVec2 once it is returned. In my "GetMouseDragDelta" wrapper, LuaJIT heap allocates it before calling "igGetMouseDragDelta" instead of after, but LuaJIT is still doing the heap allocation. No difference in memory usage. It's your vote whether you think a C binding function is easier to write than a LuaJIT binding function -- I'd rather write the LuaJIT function, hence my choice.

If you want to test this, you can use the LuaJIT 2.1 disassembler to check which is more optimal in terms of what code is produced within a use-case of the function. I haven't ventured into this realm because it didn't seem worth the effort for my use cases. I usually just stick to optimizing routines that are bottlenecks.

-Chris

On Sun, Dec 25, 2016 at 11:58 PM, Victor Bombi notifications@github.com wrote:

Hello Chris,

Thanks for answering. I have the feeling that when luajit does return-by-value itself as in:

pos = lib.GetMouseDragDeltaVersionImgui( 0, -1)

the work is the same that has to be done in:

function M.GetMouseDragDelta(button , lock_threshold) local pos = ImVec2_p() lib.igGetMouseDragDelta(pos, button or 0, lock_threshold or -1) return pos[0] end

pos = M.GetMouseDragDleta(0, -1)

allocation and afterwards indirection and copy to the ImVec2 named pos.

But the first option would be easier to code and simpler.

victor

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/thenumbernine/lua-ffi-bindings/issues/1#issuecomment-269138923, or mute the thread https://github.com/notifications/unsubscribe-auth/AEC3Tqv4RnBz8kKhWX7ftZhXcqDErCP_ks5rLucGgaJpZM4LVXqq .

sonoro1234 commented 7 years ago

Chris, thanks for kindly answering to this my, until now, solitary question.

victor