1 /*
2 * Copyright 2018 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "zink_context.h"
25 #include "zink_framebuffer.h"
26
27 #include "zink_render_pass.h"
28 #include "zink_screen.h"
29 #include "zink_surface.h"
30
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33
34 static struct pipe_surface *
framebuffer_null_surface_init(struct zink_context * ctx,struct zink_framebuffer_state * state)35 framebuffer_null_surface_init(struct zink_context *ctx, struct zink_framebuffer_state *state)
36 {
37 struct pipe_surface surf_templ = {};
38 unsigned idx = util_logbase2_ceil(MAX2(state->samples, 1));
39
40 if (!ctx->null_buffers[idx]) {
41 struct pipe_resource *pres;
42 struct pipe_resource templ = {};
43 templ.width0 = state->width;
44 templ.height0 = state->height;
45 templ.depth0 = 1;
46 templ.format = PIPE_FORMAT_R8_UINT;
47 templ.target = PIPE_TEXTURE_2D;
48 templ.bind = PIPE_BIND_RENDER_TARGET;
49 templ.nr_samples = state->samples;
50
51 pres = ctx->base.screen->resource_create(ctx->base.screen, &templ);
52 if (!pres)
53 return NULL;
54
55 ctx->null_buffers[idx] = pres;
56 }
57 surf_templ.format = PIPE_FORMAT_R8_UINT;
58 surf_templ.nr_samples = state->samples;
59 return ctx->base.create_surface(&ctx->base, ctx->null_buffers[idx], &surf_templ);
60 }
61
62 void
zink_destroy_framebuffer(struct zink_screen * screen,struct zink_framebuffer * fbuf)63 zink_destroy_framebuffer(struct zink_screen *screen,
64 struct zink_framebuffer *fbuf)
65 {
66 vkDestroyFramebuffer(screen->dev, fbuf->fb, NULL);
67 for (int i = 0; i < ARRAY_SIZE(fbuf->surfaces); ++i)
68 pipe_surface_reference(fbuf->surfaces + i, NULL);
69
70 pipe_surface_reference(&fbuf->null_surface, NULL);
71
72 zink_render_pass_reference(screen, &fbuf->rp, NULL);
73
74 FREE(fbuf);
75 }
76
77 struct zink_framebuffer *
zink_create_framebuffer(struct zink_context * ctx,struct zink_screen * screen,struct zink_framebuffer_state * fb)78 zink_create_framebuffer(struct zink_context *ctx, struct zink_screen *screen,
79 struct zink_framebuffer_state *fb)
80 {
81 struct zink_framebuffer *fbuf = CALLOC_STRUCT(zink_framebuffer);
82 if (!fbuf)
83 return NULL;
84
85 pipe_reference_init(&fbuf->reference, 1);
86
87 if (fb->has_null_attachments)
88 fbuf->null_surface = framebuffer_null_surface_init(ctx, fb);
89
90 VkImageView attachments[ARRAY_SIZE(fb->attachments)] = {};
91 for (int i = 0; i < fb->num_attachments; i++) {
92 struct zink_surface *surf = fb->attachments[i];
93 if (!surf)
94 surf = zink_surface(fbuf->null_surface);
95 pipe_surface_reference(fbuf->surfaces + i, &surf->base);
96 attachments[i] = surf->image_view;
97 }
98
99 zink_render_pass_reference(screen, &fbuf->rp, fb->rp);
100
101 VkFramebufferCreateInfo fci = {};
102 fci.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
103 fci.renderPass = fbuf->rp->render_pass;
104 fci.attachmentCount = fb->num_attachments;
105 fci.pAttachments = attachments;
106 fci.width = fb->width;
107 fci.height = fb->height;
108 fci.layers = fb->layers;
109
110 if (vkCreateFramebuffer(screen->dev, &fci, NULL, &fbuf->fb) != VK_SUCCESS) {
111 zink_destroy_framebuffer(screen, fbuf);
112 return NULL;
113 }
114
115 return fbuf;
116 }
117
118 void
debug_describe_zink_framebuffer(char * buf,const struct zink_framebuffer * ptr)119 debug_describe_zink_framebuffer(char* buf, const struct zink_framebuffer *ptr)
120 {
121 sprintf(buf, "zink_framebuffer");
122 }
123