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

Draw only if anything changed #52

Open
Oneiros90 opened this issue Jun 10, 2018 · 1 comment
Open

Draw only if anything changed #52

Oneiros90 opened this issue Jun 10, 2018 · 1 comment

Comments

@Oneiros90
Copy link

Oneiros90 commented Jun 10, 2018

Thanks for this Go wrapper, it is awesome.
I am trying to change the drawing logic in order to achieve better performance in a simple desktop application. I would like to limit the GL draw calls to actual user interactions or render buffer changes. In the c nuklear documentation I see that it is possible by defining NK_ZERO_COMMAND_MEMORY and by comparing the last draw commands with the new ones, using memcmp and memcpy.

Here's my attempt at it, by modifying impl_glfw_gl3.go:

NK_ZERO_COMMAND_MEMORY definition:

#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#define NK_ZERO_COMMAND_MEMORY

#include "nuklear.h"
*/
import "C"

memcmp and memcpy wrappers:

func memcmp(arg0 *Buffer, arg1 *Buffer, csize _Ctype_ulonglong) int32 {
	carg0, _ := (unsafe.Pointer(arg0)), cgoAllocsUnknown
	carg1, _ := (unsafe.Pointer(arg1)), cgoAllocsUnknown
	cresu, _ := C.memcmp(carg0, carg1, csize)
	return int32(cresu)
}

func memcpy(arg0 *Buffer, arg1 *Buffer, csize _Ctype_ulonglong) {
	carg0, _ := (unsafe.Pointer(arg0)), cgoAllocsUnknown
	carg1, _ := (unsafe.Pointer(arg1)), cgoAllocsUnknown
	C.memcpy(carg0, carg1, csize)
}

Definition of a buffer for the last frame commands:

type platformDevice struct {
	cmds     *Buffer
	lastCmds *Buffer
...

Initialization:

func deviceCreate() {
	dev := state.ogl
	dev.cmds = NewBuffer()
	NkBufferInitDefault(dev.cmds)
	dev.lastCmds = NewBuffer()
	NkBufferInitDefault(dev.lastCmds)
...

And finally, the comparison and copy in the NkPlatformRender function:

gl.UnmapBuffer(gl.ARRAY_BUFFER)
gl.UnmapBuffer(gl.ARRAY_BUFFER)
gl.UnmapBuffer(gl.ELEMENT_ARRAY_BUFFER)

if memcmp(dev.cmds, dev.lastCmds, state.ctx.memory.allocated) != 0 {

	memcpy(dev.lastCmds, dev.cmds, state.ctx.memory.allocated)

	var offset uintptr

	// iterate over and execute each draw command
	NkDrawForeach(state.ctx, dev.cmds, func(cmd *DrawCommand) {
....

Unfortunately this doesn't look to work on the demo application... all I see is a blue background (the default color). I tried to debug and memcmp returns -1 only the first time, then always 0... weird.

Any thought about this attempt?

@xlab
Copy link
Member

xlab commented Jun 10, 2018

Good attempt, should help to save some CPU on mobile devices. I'll look into it using your code when I have some time, can't promise at this point.

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

No branches or pull requests

2 participants