~bl4ckb0ne/wxrc

33555fad816bad062f4596c9a99b9b2045e4df35 — Simon Zeni 1 year, 4 months ago f004ce0
example/cube: fix framerate
1 files changed, 76 insertions(+), 87 deletions(-)

M example/cube.c
M example/cube.c => example/cube.c +76 -87
@@ 25,6 25,8 @@
#endif

struct view {
	struct wl_list link; // state::views

	struct xr_view_v1 *xr_view;
	struct state *state;



@@ 36,7 38,7 @@ struct view {

	GLuint fbo, depth;

	struct wl_list link; // state::views
	mat4 vp;
};

struct controller {


@@ 94,8 96,6 @@ static void
controller_handedness(void *data, struct xr_controller_v1 *xr_controller_v1,
		uint32_t hand)
{
	printf("controller %p handedness %u\n", xr_controller_v1, hand);

	struct state *state = data;

	if (hand == XR_CONTROLLER_V1_HAND_LEFT) {


@@ 126,6 126,7 @@ controller_value(void *data, struct xr_controller_v1 *xr_controller_v1,
	double v = wl_fixed_to_double(value);

	if (v == 1.f && !controller->grabbing) {
		printf("xr_controller_v1 %p grabbing\n", xr_controller_v1);
		controller->grabbing = true;

		wl_fixed_t duration = wl_fixed_from_int(-1);


@@ 133,8 134,8 @@ controller_value(void *data, struct xr_controller_v1 *xr_controller_v1,
		wl_fixed_t amp = wl_fixed_from_double(1.f);

		xr_controller_v1_haptic(xr_controller_v1, duration, freq, amp);

	} else if (v < 1.f && controller->grabbing) {
		printf("xr_controller_v1 %p releasing\n", xr_controller_v1);
		controller->grabbing = false;
	}
}


@@ 156,18 157,12 @@ controller_position(void *data, struct xr_controller_v1 *xr_controller_v1,
	float delta = (controller->trackpad_y - value) * 0.1f;
	controller->trackpad_y = value;

	printf("touchpad %lf prev %lf delta %lf\n", wl_fixed_to_double(y),
		controller->trackpad_y, delta);

	if (!controller->grabbing) {
		return;
	}

	struct state *state = controller->state;
	float scale = state->scale + delta;
	if (scale >= 0.1f) {
		printf("scale %f\n", scale);
		state->scale = scale;
	if (controller->grabbing) {
		struct state *state = controller->state;
		float scale = state->scale + delta;
		if (scale >= 0.1f) {
			state->scale = scale;
		}
	}
}



@@ 223,8 218,6 @@ static const struct xr_controller_v1_listener controller_listener = {
static void
hand_handedness(void *data, struct xr_hand_v1 *xr_hand_v1, uint32_t hand)
{
	printf("hand %p handedness %u\n", xr_hand_v1, hand);

	struct state *state = data;

	if (hand == XR_HAND_V1_HAND_LEFT) {


@@ 246,10 239,10 @@ hand_gesture(void *data, struct xr_hand_v1 *xr_hand_v1, uint32_t state,

	if (state == XR_HAND_V1_GESTURE_STATE_FIST
			|| state == XR_HAND_V1_GESTURE_STATE_PINCH) {
		printf("hand %p grabbing\n", hand);
		printf("xr_hand_v1 %p grabbing\n", xr_hand_v1);
		hand->grabbing = true;
	} else {
		printf("hand %p not grabbing\n", hand);
		printf("xr_hand_v1 %p releasing\n", xr_hand_v1);
		hand->grabbing = false;
	}
}


@@ 380,70 373,10 @@ view_configure(void *data, struct xr_view_v1 *xr_view_v1, uint32_t width,
static void
view_frame(void *data, struct xr_view_v1 *xr_view_v1, struct wl_array *matrix)
{
	TRACE_BEGIN;
	struct view *view = data;

	char text[128] = {0};
	snprintf(text, sizeof(text), "xr_view_v1 %p", xr_view_v1);
	TRACE_TEXT(text, sizeof(text));

	struct state *state = view->state;
	struct xr_buffer *color = xr_buffer_get(&state->allocator, view->colors,
		view->width, view->height, view->color_fmt);
	struct xr_buffer *depth = xr_buffer_get(&state->allocator, view->depths,
		view->width, view->height, view->depth_fmt);

	if (color == NULL || depth == NULL) {
		TRACE_END;
		return;
	}

	mat4 vp;
	assert(matrix->size == sizeof(mat4));
	memcpy(vp, matrix->data, matrix->size);

	mat4 mvp = GLM_MAT4_IDENTITY_INIT;
	glm_mat4_mul(vp, state->transform, mvp);

	glBindFramebuffer(GL_FRAMEBUFFER, view->fbo);
	glViewport(0, 0, view->width, view->height);

	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
		GL_TEXTURE_2D, color->tex, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
		GL_TEXTURE_2D, depth->tex, 0);

	// Draw model to color attachment 0
	const GLenum attachment[] = {GL_COLOR_ATTACHMENT0, GL_NONE};
	glDrawBuffers(2, attachment);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	glClearColor(0.f, 0.f, 0.f, 0.f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glUseProgram(state->program);

	GLint mvp_loc = glGetUniformLocation(state->program, "mvp");
	glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, (GLfloat *)mvp);

	glBindVertexArray(state->vao);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
	glBindVertexArray(0);

	// Blit depth into a texture
	depth_draw(&state->depth, view->depth);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	xr_surface_v1_attach_color(state->xr_surface, view->xr_view,
		color->wl_buffer);

	xr_surface_v1_attach_depth(state->xr_surface, view->xr_view,
		depth->wl_buffer);

	TRACE_END;
	memcpy(view->vp, matrix->data, matrix->size);
}

static const struct xr_view_v1_listener view_listener = {


@@ 476,12 409,6 @@ static const struct xr_surface_v1_listener surface_listener = {

static void render(struct state *state, uint32_t time);

static void frame_done(void *data, struct wl_callback *wl_callback, uint32_t time);

static const struct wl_callback_listener frame_listener = {
	.done = frame_done,
};

static void
frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
{


@@ 491,11 418,71 @@ frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
	render(state, time);
}

static const struct wl_callback_listener frame_listener = {
	.done = frame_done,
};

static void
render(struct state *state, uint32_t time)
{
	TRACE_BEGIN;

	struct view *view;
	wl_list_for_each(view, &state->views, link) {
		struct xr_buffer *color = xr_buffer_get(&state->allocator, view->colors,
			view->width, view->height, view->color_fmt);
		struct xr_buffer *depth = xr_buffer_get(&state->allocator, view->depths,
			view->width, view->height, view->depth_fmt);

		if (color == NULL || depth == NULL) {
			printf("skipping frame");
			continue;
		}

		mat4 mvp = GLM_MAT4_IDENTITY_INIT;
		glm_mat4_mul(view->vp, state->transform, mvp);

		glBindFramebuffer(GL_FRAMEBUFFER, view->fbo);
		glViewport(0, 0, view->width, view->height);

		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			GL_TEXTURE_2D, color->tex, 0);
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
			GL_TEXTURE_2D, depth->tex, 0);

		// Draw model to color attachment 0
		const GLenum attachment[] = {GL_COLOR_ATTACHMENT0, GL_NONE};
		glDrawBuffers(2, attachment);

		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LESS);

		glClearColor(0.f, 0.f, 0.f, 0.f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glUseProgram(state->program);

		GLint mvp_loc = glGetUniformLocation(state->program, "mvp");
		glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, (GLfloat *)mvp);

		glBindVertexArray(state->vao);
		glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
		glBindVertexArray(0);

		// Blit depth into a texture
		depth_draw(&state->depth, view->depth);

		glFinish();

		glBindFramebuffer(GL_FRAMEBUFFER, 0);

		xr_surface_v1_attach_color(state->xr_surface, view->xr_view,
			color->wl_buffer);

		xr_surface_v1_attach_depth(state->xr_surface, view->xr_view,
			depth->wl_buffer);
	}

	struct wl_callback *wl_callback = wl_surface_frame(state->wl_surface);
	wl_callback_add_listener(wl_callback, &frame_listener, state);



@@ 505,6 492,7 @@ render(struct state *state, uint32_t time)

	TRACE_END;

/*
	static uint32_t benchmark_time = 0, frames = 0;
	static const uint32_t interval = 5;



@@ 518,6 506,7 @@ render(struct state *state, uint32_t time)
		frames = 0;
	}
	frames++;
*/
}

static bool