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

SDL implementation is really inefficient #66

Open
vkoskiv opened this issue Feb 25, 2020 · 0 comments
Open

SDL implementation is really inefficient #66

vkoskiv opened this issue Feb 25, 2020 · 0 comments

Comments

@vkoskiv
Copy link
Owner

vkoskiv commented Feb 25, 2020

Ideally we wouldn't SDL_RenderCopy the entire image buffers for every frame, but instead update only the currently active tile rectangles.
Profiling reveals that a good portion of CPU time is indeed spent on the main thread where it copies the image buffers 60 times a second.

Another option would of course be to just reduce the update loop timing to 20 or 30fps, but I really prefer the smooth 60fps updating, and I think it can be manageable if we just implement this correctly.
Another thing is, we're not using SDL_LockTexture at all yet.

I've been prototyping it like this recently, but it's still not working quite right:

void drawWindow(struct renderer *r, struct texture *t) {
	if (aborted) {
		r->state.renderAborted = true;
	}
#ifdef UI_ENABLED
	//Render frames
	updateFrames(r);
	//Update image data
	
	for (int i = 0; i < r->prefs.threadCount; ++i) {
		SDL_Rect rect = fromTile(r->state.renderTiles[r->state.threads[i].currentTileNum]);
		//int offset = rect.x + r->prefs.imageHeight * r->prefs.imageWidth; // (pitch * rect.y) + rect.x
		//int offset = (rect.y * r->prefs.imageWidth) + rect.x * t->channels;
		int offset = (rect.x + (rect.y) * t->width) * t->channels;
		SDL_UpdateTexture(r->mainDisplay->texture, &rect, t->byte_data + offset, t->width * 3);
		SDL_UpdateTexture(r->mainDisplay->overlayTexture, &rect, r->state.uiBuffer->byte_data, t->width * 4);
	}
	
	//SDL_UpdateTexture(r->mainDisplay->texture, NULL, t->byte_data, t->width * 3);
	//SDL_UpdateTexture(r->mainDisplay->overlayTexture, NULL, r->state.uiBuffer->byte_data, t->width * 4);
	SDL_RenderCopy(r->mainDisplay->renderer, r->mainDisplay->texture, NULL, NULL);
	SDL_RenderCopy(r->mainDisplay->renderer, r->mainDisplay->overlayTexture, NULL, NULL);
	SDL_RenderPresent(r->mainDisplay->renderer);
#endif
}

Here it gathers all the actively rendered tiles, generates an SDL_Rect based on them and then tries to update only those regions, but I keep getting the offset wrong for some reason.

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

1 participant