1 /*
2  * This software is licensed under the terms of the MIT License.
3  * See COPYING for further information.
4  * ---
5  * Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
6  * Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
7 */
8 
9 #include "taisei.h"
10 
11 #include "fbutil.h"
12 
attachment_name(FramebufferAttachment a)13 static const char* attachment_name(FramebufferAttachment a) {
14 	static const char *map[FRAMEBUFFER_MAX_ATTACHMENTS] = {
15 		[FRAMEBUFFER_ATTACH_DEPTH] = "depth",
16 		[FRAMEBUFFER_ATTACH_COLOR0] = "color0",
17 		[FRAMEBUFFER_ATTACH_COLOR1] = "color1",
18 		[FRAMEBUFFER_ATTACH_COLOR2] = "color2",
19 		[FRAMEBUFFER_ATTACH_COLOR3] = "color3",
20 	};
21 
22 	assert((uint)a < FRAMEBUFFER_MAX_ATTACHMENTS);
23 	return map[(uint)a];
24 }
25 
fbutil_create_attachments(Framebuffer * fb,uint num_attachments,FBAttachmentConfig attachments[num_attachments])26 void fbutil_create_attachments(Framebuffer *fb, uint num_attachments, FBAttachmentConfig attachments[num_attachments]) {
27 	char buf[128];
28 
29 	for(uint i = 0; i < num_attachments; ++i) {
30 		log_debug("%i %i", attachments[i].tex_params.width, attachments[i].tex_params.height);
31 		Texture *tex = r_texture_create(&attachments[i].tex_params);
32 		snprintf(buf, sizeof(buf), "%s [%s]", r_framebuffer_get_debug_label(fb), attachment_name(attachments[i].attachment));
33 		r_texture_set_debug_label(tex, buf);
34 		r_framebuffer_attach(fb, tex, 0, attachments[i].attachment);
35 	}
36 }
37 
fbutil_destroy_attachments(Framebuffer * fb)38 void fbutil_destroy_attachments(Framebuffer *fb) {
39 	for(uint i = 0; i < FRAMEBUFFER_MAX_ATTACHMENTS; ++i) {
40 		Texture *tex = r_framebuffer_get_attachment(fb, i);
41 
42 		if(tex != NULL) {
43 			r_texture_destroy(tex);
44 		}
45 	}
46 }
47 
fbutil_resize_attachment(Framebuffer * fb,FramebufferAttachment attachment,uint width,uint height)48 void fbutil_resize_attachment(Framebuffer *fb, FramebufferAttachment attachment, uint width, uint height) {
49 	Texture *tex = r_framebuffer_get_attachment(fb, attachment);
50 
51 	if(tex == NULL) {
52 		return;
53 	}
54 
55 	uint tw, th;
56 	r_texture_get_size(tex, 0, &tw, &th);
57 
58 	if(tw == width && th == height) {
59 		return;
60 	}
61 
62 	// TODO: We could render a rescaled version of the old texture contents here
63 
64 	TextureParams params;
65 	r_texture_get_params(tex, &params);
66 	r_texture_destroy(tex);
67 	params.width = width;
68 	params.height = height;
69 	params.mipmaps = 0; // FIXME
70 	r_framebuffer_attach(fb, r_texture_create(&params), 0, attachment);
71 }
72