2 * Copyright 2018 Collabora Ltd.
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:
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
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.
24 #include "zink_context.h"
25 #include "zink_framebuffer.h"
27 #include "zink_render_pass.h"
28 #include "zink_screen.h"
29 #include "zink_surface.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
34 static struct pipe_surface
*
35 framebuffer_null_surface_init(struct zink_context
*ctx
, struct zink_framebuffer_state
*state
)
37 struct pipe_surface surf_templ
= {};
38 unsigned idx
= util_logbase2_ceil(MAX2(state
->samples
, 1));
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
;
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
;
51 pres
= ctx
->base
.screen
->resource_create(ctx
->base
.screen
, &templ
);
55 ctx
->null_buffers
[idx
] = pres
;
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
);
63 zink_destroy_framebuffer(struct zink_screen
*screen
,
64 struct zink_framebuffer
*fbuf
)
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
);
70 pipe_surface_reference(&fbuf
->null_surface
, NULL
);
72 zink_render_pass_reference(screen
, &fbuf
->rp
, NULL
);
77 struct zink_framebuffer
*
78 zink_create_framebuffer(struct zink_context
*ctx
, struct zink_screen
*screen
,
79 struct zink_framebuffer_state
*fb
)
81 struct zink_framebuffer
*fbuf
= CALLOC_STRUCT(zink_framebuffer
);
85 pipe_reference_init(&fbuf
->reference
, 1);
87 if (fb
->has_null_attachments
)
88 fbuf
->null_surface
= framebuffer_null_surface_init(ctx
, fb
);
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
];
94 surf
= zink_surface(fbuf
->null_surface
);
95 pipe_surface_reference(fbuf
->surfaces
+ i
, &surf
->base
);
96 attachments
[i
] = surf
->image_view
;
99 zink_render_pass_reference(screen
, &fbuf
->rp
, fb
->rp
);
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
;
110 if (vkCreateFramebuffer(screen
->dev
, &fci
, NULL
, &fbuf
->fb
) != VK_SUCCESS
) {
111 zink_destroy_framebuffer(screen
, fbuf
);
119 debug_describe_zink_framebuffer(char* buf
, const struct zink_framebuffer
*ptr
)
121 sprintf(buf
, "zink_framebuffer");