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 #include "zink_kopper.h"
27 #include "zink_query.h"
28 #include "zink_render_pass.h"
29 #include "zink_resource.h"
30 #include "zink_screen.h"
31
32 #include "util/u_memory.h"
33 #include "util/u_string.h"
34 #include "util/u_blitter.h"
35
36 static VkAttachmentLoadOp
get_rt_loadop(const struct zink_rt_attrib * rt,bool clear)37 get_rt_loadop(const struct zink_rt_attrib *rt, bool clear)
38 {
39 return clear ? VK_ATTACHMENT_LOAD_OP_CLEAR :
40 /* TODO: need replicate EXT */
41 //rt->resolve || rt->invalid ?
42 rt->invalid ?
43 VK_ATTACHMENT_LOAD_OP_DONT_CARE :
44 VK_ATTACHMENT_LOAD_OP_LOAD;
45 }
46
47 static VkImageLayout
get_color_rt_layout(const struct zink_rt_attrib * rt)48 get_color_rt_layout(const struct zink_rt_attrib *rt)
49 {
50 return rt->fbfetch ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
51 }
52
53 static VkImageLayout
get_zs_rt_layout(const struct zink_rt_attrib * rt)54 get_zs_rt_layout(const struct zink_rt_attrib *rt)
55 {
56 bool has_clear = rt->clear_color || rt->clear_stencil;
57 if (rt->mixed_zs)
58 return VK_IMAGE_LAYOUT_GENERAL;
59 return rt->needs_write || has_clear ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
60 }
61
62 static VkRenderPass
create_render_pass2(struct zink_screen * screen,struct zink_render_pass_state * state,struct zink_render_pass_pipeline_state * pstate)63 create_render_pass2(struct zink_screen *screen, struct zink_render_pass_state *state, struct zink_render_pass_pipeline_state *pstate)
64 {
65
66 VkAttachmentReference2 color_refs[PIPE_MAX_COLOR_BUFS], color_resolves[PIPE_MAX_COLOR_BUFS], zs_ref, zs_resolve;
67 VkAttachmentReference2 input_attachments[PIPE_MAX_COLOR_BUFS];
68 VkAttachmentDescription2 attachments[2 * (PIPE_MAX_COLOR_BUFS + 1)];
69 VkPipelineStageFlags dep_pipeline = 0;
70 VkAccessFlags dep_access = 0;
71 unsigned input_count = 0;
72 const unsigned cresolve_offset = state->num_cbufs + state->have_zsbuf;
73 const unsigned zsresolve_offset = cresolve_offset + state->num_cresolves;
74
75 pstate->num_attachments = state->num_cbufs;
76 pstate->num_cresolves = state->num_cresolves;
77 pstate->num_zsresolves = state->num_zsresolves;
78 pstate->fbfetch = 0;
79 for (int i = 0; i < state->num_cbufs; i++) {
80 struct zink_rt_attrib *rt = state->rts + i;
81 attachments[i].sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
82 attachments[i].pNext = NULL;
83 attachments[i].flags = 0;
84 pstate->attachments[i].format = attachments[i].format = rt->format;
85 pstate->attachments[i].samples = attachments[i].samples = rt->samples;
86 attachments[i].loadOp = get_rt_loadop(rt, rt->clear_color);
87
88 /* TODO: need replicate EXT */
89 //attachments[i].storeOp = rt->resolve ? VK_ATTACHMENT_STORE_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE;
90 attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
91 attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
92 attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
93 /* if layout changes are ever handled here, need VkAttachmentSampleLocationsEXT */
94 VkImageLayout layout = get_color_rt_layout(rt);
95 attachments[i].initialLayout = layout;
96 attachments[i].finalLayout = layout;
97 color_refs[i].sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2;
98 color_refs[i].pNext = NULL;
99 color_refs[i].attachment = i;
100 color_refs[i].layout = layout;
101 color_refs[i].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
102 dep_pipeline |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
103 if (rt->fbfetch) {
104 memcpy(&input_attachments[input_count++], &color_refs[i], sizeof(VkAttachmentReference2));
105 dep_pipeline |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
106 dep_access |= VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
107 pstate->fbfetch = 1;
108 }
109 dep_access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
110 if (attachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
111 dep_access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
112
113 if (rt->resolve) {
114 memcpy(&attachments[cresolve_offset + i], &attachments[i], sizeof(VkAttachmentDescription2));
115 attachments[cresolve_offset + i].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
116 attachments[cresolve_offset + i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
117 attachments[cresolve_offset + i].samples = 1;
118 memcpy(&color_resolves[i], &color_refs[i], sizeof(VkAttachmentReference2));
119 color_resolves[i].attachment = cresolve_offset + i;
120 if (attachments[cresolve_offset + i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
121 dep_access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
122 }
123 }
124
125 int num_attachments = state->num_cbufs;
126 if (state->have_zsbuf) {
127 struct zink_rt_attrib *rt = state->rts + state->num_cbufs;
128 VkImageLayout layout = get_zs_rt_layout(rt);
129 attachments[num_attachments].sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
130 attachments[num_attachments].pNext = NULL;
131 attachments[num_attachments].flags = 0;
132 pstate->attachments[num_attachments].format = attachments[num_attachments].format = rt->format;
133 pstate->attachments[num_attachments].samples = attachments[num_attachments].samples = rt->samples;
134 attachments[num_attachments].loadOp = rt->clear_color ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
135 attachments[num_attachments].stencilLoadOp = rt->clear_stencil ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
136 /* TODO: need replicate EXT */
137 //attachments[num_attachments].storeOp = rt->resolve ? VK_ATTACHMENT_LOAD_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE;
138 //attachments[num_attachments].stencilStoreOp = rt->resolve ? VK_ATTACHMENT_LOAD_OP_DONT_CARE : VK_ATTACHMENT_STORE_OP_STORE;
139 attachments[num_attachments].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
140 attachments[num_attachments].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
141 /* if layout changes are ever handled here, need VkAttachmentSampleLocationsEXT */
142 attachments[num_attachments].initialLayout = layout;
143 attachments[num_attachments].finalLayout = layout;
144
145 dep_pipeline |= VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
146 if (layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
147 dep_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
148 if (attachments[num_attachments].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
149 attachments[num_attachments].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
150 dep_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
151
152 zs_ref.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2;
153 zs_ref.pNext = NULL;
154 zs_ref.attachment = num_attachments++;
155 zs_ref.layout = layout;
156 if (rt->resolve) {
157 memcpy(&attachments[zsresolve_offset], &attachments[num_attachments], sizeof(VkAttachmentDescription2));
158 attachments[zsresolve_offset].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
159 attachments[zsresolve_offset].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
160 attachments[zsresolve_offset].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
161 attachments[zsresolve_offset].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
162 attachments[zsresolve_offset].samples = 1;
163 memcpy(&zs_resolve, &zs_ref, sizeof(VkAttachmentReference2));
164 zs_ref.attachment = zsresolve_offset;
165 if (attachments[zsresolve_offset].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
166 attachments[zsresolve_offset].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
167 dep_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
168 }
169 pstate->num_attachments++;
170 }
171 pstate->color_read = (dep_access & VK_ACCESS_COLOR_ATTACHMENT_READ_BIT) > 0;
172 pstate->depth_read = (dep_access & VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT) > 0;
173 pstate->depth_write = (dep_access & VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT) > 0;
174
175 if (!screen->info.have_KHR_synchronization2)
176 dep_pipeline = MAX2(dep_pipeline, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
177
178 VkDependencyFlags flag = screen->info.have_KHR_synchronization2 ? VK_DEPENDENCY_BY_REGION_BIT : 0;
179 VkSubpassDependency2 deps[] = {
180 {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2, NULL, VK_SUBPASS_EXTERNAL, 0, dep_pipeline, dep_pipeline, 0, dep_access, flag, 0},
181 {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2, NULL, 0, VK_SUBPASS_EXTERNAL, dep_pipeline, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, dep_access, 0, flag, 0}
182 };
183 VkPipelineStageFlags input_dep = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
184 //if (zs_fbfetch) input_dep |= VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
185 VkAccessFlags input_access = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
186 //if (zs_fbfetch) input_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
187 VkSubpassDependency2 fbfetch_deps[] = {
188 {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2, NULL, VK_SUBPASS_EXTERNAL, 0, dep_pipeline, dep_pipeline, 0, dep_access, flag, 0},
189 {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2, NULL, 0, 0, dep_pipeline, input_dep, dep_access, input_access, flag, 0},
190 {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2, NULL, 0, VK_SUBPASS_EXTERNAL, dep_pipeline, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, dep_access, 0, flag, 0}
191 };
192
193 VkSubpassDescription2 subpass = {0};
194 VkSubpassDescriptionDepthStencilResolve zsresolve;
195 subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2;
196 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
197 subpass.colorAttachmentCount = state->num_cbufs;
198 subpass.pColorAttachments = color_refs;
199 subpass.pDepthStencilAttachment = state->have_zsbuf ? &zs_ref : NULL;
200 subpass.inputAttachmentCount = input_count;
201 subpass.pInputAttachments = input_attachments;
202 if (state->num_cresolves)
203 subpass.pResolveAttachments = color_resolves;
204 if (state->num_zsresolves) {
205 subpass.pNext = &zsresolve;
206 zsresolve.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE;
207 zsresolve.pNext = NULL;
208 zsresolve.depthResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT;
209 zsresolve.stencilResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT;
210 zsresolve.pDepthStencilResolveAttachment = &zs_resolve;
211 } else
212 subpass.pNext = NULL;
213
214 VkRenderPassCreateInfo2 rpci = {0};
215 rpci.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2;
216 rpci.attachmentCount = num_attachments + state->num_cresolves + state->num_zsresolves;
217 rpci.pAttachments = attachments;
218 rpci.subpassCount = 1;
219 rpci.pSubpasses = &subpass;
220 rpci.dependencyCount = input_count ? 3 : 2;
221 rpci.pDependencies = input_count ? fbfetch_deps : deps;
222
223 VkRenderPass render_pass;
224 VkResult result = VKSCR(CreateRenderPass2)(screen->dev, &rpci, NULL, &render_pass);
225 if (result != VK_SUCCESS) {
226 mesa_loge("ZINK: vkCreateRenderPass2 failed (%s)", vk_Result_to_str(result));
227 return VK_NULL_HANDLE;
228 }
229
230 return render_pass;
231 }
232
233 struct zink_render_pass *
zink_create_render_pass(struct zink_screen * screen,struct zink_render_pass_state * state,struct zink_render_pass_pipeline_state * pstate)234 zink_create_render_pass(struct zink_screen *screen,
235 struct zink_render_pass_state *state,
236 struct zink_render_pass_pipeline_state *pstate)
237 {
238 struct zink_render_pass *rp = CALLOC_STRUCT(zink_render_pass);
239 if (!rp)
240 goto fail;
241
242 rp->render_pass = create_render_pass2(screen, state, pstate);
243 if (!rp->render_pass)
244 goto fail;
245 memcpy(&rp->state, state, sizeof(struct zink_render_pass_state));
246 return rp;
247
248 fail:
249 if (rp)
250 zink_destroy_render_pass(screen, rp);
251 return NULL;
252 }
253
254 void
zink_destroy_render_pass(struct zink_screen * screen,struct zink_render_pass * rp)255 zink_destroy_render_pass(struct zink_screen *screen,
256 struct zink_render_pass *rp)
257 {
258 VKSCR(DestroyRenderPass)(screen->dev, rp->render_pass, NULL);
259 FREE(rp);
260 }
261
262 VkImageLayout
zink_render_pass_attachment_get_barrier_info(const struct zink_rt_attrib * rt,bool color,VkPipelineStageFlags * pipeline,VkAccessFlags * access)263 zink_render_pass_attachment_get_barrier_info(const struct zink_rt_attrib *rt, bool color,
264 VkPipelineStageFlags *pipeline, VkAccessFlags *access)
265 {
266 *access = 0;
267 if (color) {
268 *pipeline = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
269 *access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
270 if (!rt->clear_color && !rt->invalid)
271 *access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
272 return get_color_rt_layout(rt);
273 }
274
275 *pipeline = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
276 if (rt->mixed_zs) {
277 *access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
278 } else {
279 if (!rt->clear_color && !rt->clear_stencil)
280 *access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
281 if (rt->clear_color || rt->clear_stencil || rt->needs_write)
282 *access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
283 }
284 return get_zs_rt_layout(rt);
285 }
286
287
288 static size_t
rp_state_size(const struct zink_render_pass_pipeline_state * pstate)289 rp_state_size(const struct zink_render_pass_pipeline_state *pstate)
290 {
291 return offsetof(struct zink_render_pass_pipeline_state, attachments) +
292 sizeof(pstate->attachments[0]) * pstate->num_attachments;
293 }
294
295 static uint32_t
hash_rp_state(const void * key)296 hash_rp_state(const void *key)
297 {
298 const struct zink_render_pass_pipeline_state *s = key;
299 return _mesa_hash_data(key, rp_state_size(s));
300 }
301
302 static bool
equals_rp_state(const void * a,const void * b)303 equals_rp_state(const void *a, const void *b)
304 {
305 return !memcmp(a, b, rp_state_size(a));
306 }
307
308 static uint32_t
hash_render_pass_state(const void * key)309 hash_render_pass_state(const void *key)
310 {
311 struct zink_render_pass_state* s = (struct zink_render_pass_state*)key;
312 return _mesa_hash_data(key, offsetof(struct zink_render_pass_state, rts) + sizeof(s->rts[0]) * s->num_rts);
313 }
314
315 static bool
equals_render_pass_state(const void * a,const void * b)316 equals_render_pass_state(const void *a, const void *b)
317 {
318 const struct zink_render_pass_state *s_a = a, *s_b = b;
319 if (s_a->num_rts != s_b->num_rts)
320 return false;
321 return memcmp(a, b, offsetof(struct zink_render_pass_state, rts) + sizeof(s_a->rts[0]) * s_a->num_rts) == 0;
322 }
323
324 void
zink_init_zs_attachment(struct zink_context * ctx,struct zink_rt_attrib * rt)325 zink_init_zs_attachment(struct zink_context *ctx, struct zink_rt_attrib *rt)
326 {
327 const struct pipe_framebuffer_state *fb = &ctx->fb_state;
328 struct zink_resource *zsbuf = zink_resource(fb->zsbuf->texture);
329 struct zink_framebuffer_clear *fb_clear = &ctx->fb_clears[PIPE_MAX_COLOR_BUFS];
330 struct zink_surface *transient = zink_transient_surface(fb->zsbuf);
331 rt->format = zsbuf->format;
332 rt->samples = MAX3(transient ? transient->base.nr_samples : 0, fb->zsbuf->texture->nr_samples, 1);
333 rt->clear_color = zink_fb_clear_enabled(ctx, PIPE_MAX_COLOR_BUFS) &&
334 !zink_fb_clear_first_needs_explicit(fb_clear) &&
335 (zink_fb_clear_element(fb_clear, 0)->zs.bits & PIPE_CLEAR_DEPTH);
336 rt->clear_stencil = zink_fb_clear_enabled(ctx, PIPE_MAX_COLOR_BUFS) &&
337 !zink_fb_clear_first_needs_explicit(fb_clear) &&
338 (zink_fb_clear_element(fb_clear, 0)->zs.bits & PIPE_CLEAR_STENCIL);
339 const uint64_t outputs_written = ctx->gfx_stages[PIPE_SHADER_FRAGMENT] ?
340 ctx->gfx_stages[PIPE_SHADER_FRAGMENT]->nir->info.outputs_written : 0;
341 bool needs_write_z = (ctx->dsa_state && ctx->dsa_state->hw_state.depth_write) ||
342 outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH);
343 needs_write_z |= transient || rt->clear_color ||
344 (zink_fb_clear_enabled(ctx, PIPE_MAX_COLOR_BUFS) && (zink_fb_clear_element(fb_clear, 0)->zs.bits & PIPE_CLEAR_DEPTH));
345
346 bool needs_write_s = rt->clear_stencil || (outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) ||
347 (zink_fb_clear_enabled(ctx, PIPE_MAX_COLOR_BUFS) && (zink_fb_clear_element(fb_clear, 0)->zs.bits & PIPE_CLEAR_STENCIL));
348 if (!needs_write_z && (!ctx->dsa_state || !ctx->dsa_state->base.depth_enabled))
349 /* depth sample, stencil write */
350 rt->mixed_zs = needs_write_s && zsbuf->bind_count[0];
351 else
352 /* depth write + sample */
353 rt->mixed_zs = needs_write_z && zsbuf->bind_count[0];
354 rt->needs_write = needs_write_z | needs_write_s;
355 }
356
357 void
zink_init_color_attachment(struct zink_context * ctx,unsigned i,struct zink_rt_attrib * rt)358 zink_init_color_attachment(struct zink_context *ctx, unsigned i, struct zink_rt_attrib *rt)
359 {
360 const struct pipe_framebuffer_state *fb = &ctx->fb_state;
361 struct pipe_surface *psurf = fb->cbufs[i];
362 if (psurf && !zink_use_dummy_attachments(ctx)) {
363 struct zink_surface *surf = zink_csurface(psurf);
364 struct zink_surface *transient = zink_transient_surface(psurf);
365 rt->format = surf->info.format[0];
366 rt->samples = MAX3(transient ? transient->base.nr_samples : 0, psurf->texture->nr_samples, 1);
367 rt->clear_color = zink_fb_clear_enabled(ctx, i) && !zink_fb_clear_first_needs_explicit(&ctx->fb_clears[i]);
368 rt->invalid = !zink_resource(psurf->texture)->valid || (ctx->new_swapchain && (psurf->texture->bind & PIPE_BIND_DISPLAY_TARGET));
369 rt->fbfetch = (ctx->fbfetch_outputs & BITFIELD_BIT(i)) > 0;
370 } else {
371 memset(rt, 0, sizeof(struct zink_rt_attrib));
372 rt->format = VK_FORMAT_R8G8B8A8_UNORM;
373 rt->samples = fb->samples;
374 }
375 }
376
377 static struct zink_render_pass *
get_render_pass(struct zink_context * ctx)378 get_render_pass(struct zink_context *ctx)
379 {
380 struct zink_screen *screen = zink_screen(ctx->base.screen);
381 const struct pipe_framebuffer_state *fb = &ctx->fb_state;
382 struct zink_render_pass_state state = {0};
383 uint32_t clears = 0;
384 state.samples = fb->samples > 0;
385
386 for (int i = 0; i < fb->nr_cbufs; i++) {
387 zink_init_color_attachment(ctx, i, &state.rts[i]);
388 struct pipe_surface *surf = fb->cbufs[i];
389 if (surf && !zink_use_dummy_attachments(ctx)) {
390 clears |= !!state.rts[i].clear_color ? PIPE_CLEAR_COLOR0 << i : 0;
391 struct zink_surface *transient = zink_transient_surface(surf);
392 if (transient) {
393 state.num_cresolves++;
394 state.rts[i].resolve = true;
395 if (!state.rts[i].clear_color)
396 state.msaa_expand_mask |= BITFIELD_BIT(i);
397 } else {
398 state.rts[i].resolve = false;
399 }
400 }
401 state.num_rts++;
402 }
403 state.num_cbufs = fb->nr_cbufs;
404 assert(!state.num_cresolves || state.num_cbufs == state.num_cresolves);
405
406 if (fb->zsbuf) {
407 zink_init_zs_attachment(ctx, &state.rts[fb->nr_cbufs]);
408 struct zink_surface *transient = zink_transient_surface(fb->zsbuf);
409 if (transient) {
410 state.num_zsresolves = 1;
411 state.rts[fb->nr_cbufs].resolve = true;
412 }
413 if (state.rts[fb->nr_cbufs].clear_color)
414 clears |= PIPE_CLEAR_DEPTH;
415 if (state.rts[fb->nr_cbufs].clear_stencil)
416 clears |= PIPE_CLEAR_STENCIL;
417 state.num_rts++;
418 }
419 state.have_zsbuf = fb->zsbuf != NULL;
420 if (zink_use_dummy_attachments(ctx))
421 assert(clears == (ctx->rp_clears_enabled & PIPE_CLEAR_DEPTHSTENCIL));
422 else
423 assert(clears == ctx->rp_clears_enabled);
424 state.clears = clears;
425 uint32_t hash = hash_render_pass_state(&state);
426 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ctx->render_pass_cache, hash,
427 &state);
428 struct zink_render_pass *rp;
429 if (entry) {
430 rp = entry->data;
431 assert(rp->state.clears == clears);
432 } else {
433 struct zink_render_pass_pipeline_state pstate;
434 pstate.samples = state.samples;
435 rp = zink_create_render_pass(screen, &state, &pstate);
436 if (!_mesa_hash_table_insert_pre_hashed(ctx->render_pass_cache, hash, &rp->state, rp))
437 return NULL;
438 bool found = false;
439 struct set_entry *entry = _mesa_set_search_or_add(&ctx->render_pass_state_cache, &pstate, &found);
440 struct zink_render_pass_pipeline_state *ppstate;
441 if (!found) {
442 entry->key = ralloc(ctx, struct zink_render_pass_pipeline_state);
443 ppstate = (void*)entry->key;
444 memcpy(ppstate, &pstate, rp_state_size(&pstate));
445 ppstate->id = ctx->render_pass_state_cache.entries;
446 }
447 ppstate = (void*)entry->key;
448 rp->pipeline_state = ppstate->id;
449 }
450 return rp;
451 }
452
453 /* check whether the active rp needs to be split to replace it with rp2 */
454 static bool
rp_must_change(const struct zink_render_pass * rp,const struct zink_render_pass * rp2,bool in_rp)455 rp_must_change(const struct zink_render_pass *rp, const struct zink_render_pass *rp2, bool in_rp)
456 {
457 if (rp == rp2)
458 return false;
459 unsigned num_cbufs = rp->state.num_cbufs;
460 if (rp->pipeline_state != rp2->pipeline_state) {
461 /* if any core attrib bits are different, must split */
462 if (rp->state.val != rp2->state.val)
463 return true;
464 for (unsigned i = 0; i < num_cbufs; i++) {
465 const struct zink_rt_attrib *rt = &rp->state.rts[i];
466 const struct zink_rt_attrib *rt2 = &rp2->state.rts[i];
467 /* if layout changed, must split */
468 if (get_color_rt_layout(rt) != get_color_rt_layout(rt2))
469 return true;
470 }
471 }
472 if (rp->state.have_zsbuf) {
473 const struct zink_rt_attrib *rt = &rp->state.rts[num_cbufs];
474 const struct zink_rt_attrib *rt2 = &rp2->state.rts[num_cbufs];
475 /* if zs layout has gone from read-only to read-write, split renderpass */
476 if (get_zs_rt_layout(rt) == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL &&
477 get_zs_rt_layout(rt2) == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
478 return true;
479 }
480 /* any other change doesn't require splitting a renderpass */
481 return !in_rp;
482 }
483
484 static void
setup_framebuffer(struct zink_context * ctx)485 setup_framebuffer(struct zink_context *ctx)
486 {
487 struct zink_screen *screen = zink_screen(ctx->base.screen);
488 struct zink_render_pass *rp = ctx->gfx_pipeline_state.render_pass;
489
490 zink_update_vk_sample_locations(ctx);
491
492 if (ctx->rp_changed || ctx->rp_layout_changed || ctx->rp_loadop_changed) {
493 /* 0. ensure no stale pointers are set */
494 ctx->gfx_pipeline_state.next_render_pass = NULL;
495 /* 1. calc new rp */
496 rp = get_render_pass(ctx);
497 /* 2. evaluate whether to use new rp */
498 if (ctx->gfx_pipeline_state.render_pass) {
499 /* 2a. if previous rp exists, check whether new rp MUST be used */
500 bool must_change = rp_must_change(ctx->gfx_pipeline_state.render_pass, rp, ctx->batch.in_rp);
501 ctx->fb_changed |= must_change;
502 if (!must_change)
503 /* 2b. if non-essential attribs have changed, store for later use and continue on */
504 ctx->gfx_pipeline_state.next_render_pass = rp;
505 } else {
506 /* 2c. no previous rp in use, use this one */
507 ctx->fb_changed = true;
508 }
509 } else if (ctx->gfx_pipeline_state.next_render_pass) {
510 /* previous rp was calculated but deferred: use it */
511 assert(!ctx->batch.in_rp);
512 rp = ctx->gfx_pipeline_state.next_render_pass;
513 ctx->gfx_pipeline_state.next_render_pass = NULL;
514 ctx->fb_changed = true;
515 }
516 if (rp->pipeline_state != ctx->gfx_pipeline_state.rp_state) {
517 ctx->gfx_pipeline_state.rp_state = rp->pipeline_state;
518 ctx->gfx_pipeline_state.dirty = true;
519 }
520
521 ctx->rp_loadop_changed = false;
522 ctx->rp_layout_changed = false;
523 ctx->rp_changed = false;
524 zink_render_update_swapchain(ctx);
525
526 if (!ctx->fb_changed)
527 return;
528
529 zink_update_framebuffer_state(ctx);
530 zink_init_framebuffer(screen, ctx->framebuffer, rp);
531 ctx->fb_changed = false;
532 ctx->gfx_pipeline_state.render_pass = rp;
533 zink_batch_no_rp(ctx);
534 }
535
536 static bool
prep_fb_attachments(struct zink_context * ctx,VkImageView * att)537 prep_fb_attachments(struct zink_context *ctx, VkImageView *att)
538 {
539 const unsigned cresolve_offset = ctx->fb_state.nr_cbufs + !!ctx->fb_state.zsbuf;
540 unsigned num_resolves = 0;
541 for (int i = 0; i < ctx->fb_state.nr_cbufs; i++) {
542 struct zink_surface *surf = zink_csurface(ctx->fb_state.cbufs[i]);
543 struct zink_surface *transient = zink_transient_surface(ctx->fb_state.cbufs[i]);
544 if (transient) {
545 att[i] = zink_prep_fb_attachment(ctx, transient, i);
546 att[i + cresolve_offset] = zink_prep_fb_attachment(ctx, surf, i);
547 num_resolves++;
548 } else {
549 att[i] = zink_prep_fb_attachment(ctx, surf, i);
550 if (!att[i])
551 /* dead swapchain */
552 return false;
553 }
554 }
555 if (ctx->fb_state.zsbuf) {
556 struct zink_surface *surf = zink_csurface(ctx->fb_state.zsbuf);
557 struct zink_surface *transient = zink_transient_surface(ctx->fb_state.zsbuf);
558 if (transient) {
559 att[ctx->fb_state.nr_cbufs] = zink_prep_fb_attachment(ctx, transient, ctx->fb_state.nr_cbufs);
560 att[cresolve_offset + num_resolves] = zink_prep_fb_attachment(ctx, surf, ctx->fb_state.nr_cbufs);
561 } else {
562 att[ctx->fb_state.nr_cbufs] = zink_prep_fb_attachment(ctx, surf, ctx->fb_state.nr_cbufs);
563 }
564 }
565 return true;
566 }
567
568 static unsigned
begin_render_pass(struct zink_context * ctx)569 begin_render_pass(struct zink_context *ctx)
570 {
571 struct zink_batch *batch = &ctx->batch;
572 struct pipe_framebuffer_state *fb_state = &ctx->fb_state;
573
574 VkRenderPassBeginInfo rpbi = {0};
575 rpbi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
576 rpbi.renderPass = ctx->gfx_pipeline_state.render_pass->render_pass;
577 rpbi.renderArea.offset.x = 0;
578 rpbi.renderArea.offset.y = 0;
579 rpbi.renderArea.extent.width = fb_state->width;
580 rpbi.renderArea.extent.height = fb_state->height;
581
582 VkClearValue clears[PIPE_MAX_COLOR_BUFS + 1] = {0};
583 unsigned clear_buffers = 0;
584 uint32_t clear_validate = 0;
585 for (int i = 0; i < fb_state->nr_cbufs; i++) {
586 /* these are no-ops */
587 if (!fb_state->cbufs[i] || !zink_fb_clear_enabled(ctx, i) || zink_use_dummy_attachments(ctx))
588 continue;
589 /* these need actual clear calls inside the rp */
590 struct zink_framebuffer_clear_data *clear = zink_fb_clear_element(&ctx->fb_clears[i], 0);
591 if (zink_fb_clear_needs_explicit(&ctx->fb_clears[i])) {
592 clear_buffers |= (PIPE_CLEAR_COLOR0 << i);
593 if (zink_fb_clear_count(&ctx->fb_clears[i]) < 2 ||
594 zink_fb_clear_element_needs_explicit(clear))
595 continue;
596 }
597 /* we now know there's one clear that can be done here */
598 memcpy(&clears[i].color, &clear->color, sizeof(float) * 4);
599 rpbi.clearValueCount = i + 1;
600 clear_validate |= PIPE_CLEAR_COLOR0 << i;
601 assert(ctx->framebuffer->rp->state.clears);
602 }
603 if (fb_state->zsbuf && zink_fb_clear_enabled(ctx, PIPE_MAX_COLOR_BUFS)) {
604 struct zink_framebuffer_clear *fb_clear = &ctx->fb_clears[PIPE_MAX_COLOR_BUFS];
605 struct zink_framebuffer_clear_data *clear = zink_fb_clear_element(fb_clear, 0);
606 if (!zink_fb_clear_element_needs_explicit(clear)) {
607 clears[fb_state->nr_cbufs].depthStencil.depth = clear->zs.depth;
608 clears[fb_state->nr_cbufs].depthStencil.stencil = clear->zs.stencil;
609 rpbi.clearValueCount = fb_state->nr_cbufs + 1;
610 clear_validate |= clear->zs.bits;
611 assert(ctx->framebuffer->rp->state.clears);
612 }
613 if (zink_fb_clear_needs_explicit(fb_clear)) {
614 for (int j = !zink_fb_clear_element_needs_explicit(clear);
615 (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL && j < zink_fb_clear_count(fb_clear);
616 j++)
617 clear_buffers |= zink_fb_clear_element(fb_clear, j)->zs.bits;
618 }
619 }
620 assert(clear_validate == ctx->framebuffer->rp->state.clears);
621 rpbi.pClearValues = &clears[0];
622 rpbi.framebuffer = ctx->framebuffer->fb;
623
624 assert(ctx->gfx_pipeline_state.render_pass && ctx->framebuffer);
625
626 VkRenderPassAttachmentBeginInfo infos;
627 VkImageView att[2 * (PIPE_MAX_COLOR_BUFS + 1)];
628 infos.sType = VK_STRUCTURE_TYPE_RENDER_PASS_ATTACHMENT_BEGIN_INFO;
629 infos.pNext = NULL;
630 infos.attachmentCount = ctx->framebuffer->state.num_attachments;
631 infos.pAttachments = att;
632 if (!prep_fb_attachments(ctx, att))
633 return 0;
634 /* this can be set if fbfetch is activated */
635 ctx->rp_changed = false;
636 #ifndef NDEBUG
637 const unsigned cresolve_offset = ctx->fb_state.nr_cbufs + !!ctx->fb_state.zsbuf;
638 for (int i = 0; i < ctx->fb_state.nr_cbufs; i++) {
639 if (ctx->fb_state.cbufs[i]) {
640 struct zink_surface *surf = zink_csurface(ctx->fb_state.cbufs[i]);
641 if (zink_use_dummy_attachments(ctx)) {
642 surf = zink_csurface(ctx->dummy_surface[util_logbase2_ceil(ctx->fb_state.samples)]);
643 assert(zink_resource(surf->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[i].usage);
644 } else {
645 struct zink_surface *transient = zink_transient_surface(ctx->fb_state.cbufs[i]);
646 if (surf->base.format == ctx->fb_state.cbufs[i]->format) {
647 if (transient) {
648 assert(zink_resource(transient->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[i].usage);
649 assert(zink_resource(surf->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[cresolve_offset].usage);
650 } else {
651 assert(zink_resource(surf->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[i].usage);
652 }
653 }
654 }
655 }
656 }
657 if (ctx->fb_state.zsbuf) {
658 struct zink_surface *surf = zink_csurface(ctx->fb_state.zsbuf);
659 struct zink_surface *transient = zink_transient_surface(ctx->fb_state.zsbuf);
660 if (transient) {
661 assert(zink_resource(transient->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[ctx->fb_state.nr_cbufs].usage);
662 assert(zink_resource(surf->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[cresolve_offset].usage);
663 } else {
664 assert(zink_resource(surf->base.texture)->obj->vkusage == ctx->framebuffer->state.infos[ctx->fb_state.nr_cbufs].usage);
665 }
666 }
667 #endif
668 rpbi.pNext = &infos;
669
670 VKCTX(CmdBeginRenderPass)(batch->state->cmdbuf, &rpbi, VK_SUBPASS_CONTENTS_INLINE);
671 batch->in_rp = true;
672 ctx->new_swapchain = false;
673 return clear_buffers;
674 }
675
676 unsigned
zink_begin_render_pass(struct zink_context * ctx)677 zink_begin_render_pass(struct zink_context *ctx)
678 {
679 setup_framebuffer(ctx);
680 if (ctx->batch.in_rp)
681 return 0;
682 /* TODO: need replicate EXT */
683 if (ctx->framebuffer->rp->state.msaa_expand_mask) {
684 uint32_t rp_state = ctx->gfx_pipeline_state.rp_state;
685 struct zink_render_pass *rp = ctx->gfx_pipeline_state.render_pass;
686
687 u_foreach_bit(i, ctx->framebuffer->rp->state.msaa_expand_mask) {
688 struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)ctx->fb_state.cbufs[i];
689 if (csurf->transient_init)
690 continue;
691 struct pipe_surface *dst_view = (struct pipe_surface*)csurf->transient;
692 assert(dst_view);
693 struct pipe_sampler_view src_templ, *src_view;
694 struct pipe_resource *src = ctx->fb_state.cbufs[i]->texture;
695 struct pipe_box dstbox;
696
697 u_box_3d(0, 0, 0, ctx->fb_state.width, ctx->fb_state.height,
698 1 + dst_view->u.tex.last_layer - dst_view->u.tex.first_layer, &dstbox);
699
700 util_blitter_default_src_texture(ctx->blitter, &src_templ, src, ctx->fb_state.cbufs[i]->u.tex.level);
701 src_view = ctx->base.create_sampler_view(&ctx->base, src, &src_templ);
702
703 zink_blit_begin(ctx, ZINK_BLIT_SAVE_FB | ZINK_BLIT_SAVE_FS | ZINK_BLIT_SAVE_TEXTURES);
704 util_blitter_blit_generic(ctx->blitter, dst_view, &dstbox,
705 src_view, &dstbox, ctx->fb_state.width, ctx->fb_state.height,
706 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
707 false, false, 0);
708
709 pipe_sampler_view_reference(&src_view, NULL);
710 csurf->transient_init = true;
711 }
712 ctx->rp_layout_changed = ctx->rp_loadop_changed = false;
713 ctx->fb_changed = ctx->rp_changed = false;
714 ctx->gfx_pipeline_state.rp_state = rp_state;
715 ctx->gfx_pipeline_state.render_pass = rp;
716 }
717 assert(ctx->gfx_pipeline_state.render_pass);
718 return begin_render_pass(ctx);
719 }
720
721 void
zink_end_render_pass(struct zink_context * ctx)722 zink_end_render_pass(struct zink_context *ctx)
723 {
724 if (ctx->batch.in_rp) {
725 VKCTX(CmdEndRenderPass)(ctx->batch.state->cmdbuf);
726 for (unsigned i = 0; i < ctx->fb_state.nr_cbufs; i++) {
727 struct zink_ctx_surface *csurf = (struct zink_ctx_surface*)ctx->fb_state.cbufs[i];
728 if (csurf)
729 csurf->transient_init = true;
730 }
731 }
732 ctx->batch.in_rp = false;
733 }
734
735 bool
zink_init_render_pass(struct zink_context * ctx)736 zink_init_render_pass(struct zink_context *ctx)
737 {
738 _mesa_set_init(&ctx->render_pass_state_cache, ctx, hash_rp_state, equals_rp_state);
739 ctx->render_pass_cache = _mesa_hash_table_create(NULL,
740 hash_render_pass_state,
741 equals_render_pass_state);
742 return !!ctx->render_pass_cache;
743 }
744
745 void
zink_render_update_swapchain(struct zink_context * ctx)746 zink_render_update_swapchain(struct zink_context *ctx)
747 {
748 bool has_swapchain = false;
749 for (unsigned i = 0; i < ctx->fb_state.nr_cbufs; i++) {
750 if (!ctx->fb_state.cbufs[i])
751 continue;
752 struct zink_resource *res = zink_resource(ctx->fb_state.cbufs[i]->texture);
753 if (zink_is_swapchain(res)) {
754 has_swapchain = true;
755 if (zink_kopper_acquire(ctx, res, UINT64_MAX))
756 zink_surface_swapchain_update(ctx, zink_csurface(ctx->fb_state.cbufs[i]));
757 }
758 }
759 if (has_swapchain && (ctx->swapchain_size.width || ctx->swapchain_size.height)) {
760 unsigned old_w = ctx->fb_state.width;
761 unsigned old_h = ctx->fb_state.height;
762 ctx->fb_state.width = ctx->swapchain_size.width;
763 ctx->fb_state.height = ctx->swapchain_size.height;
764 zink_kopper_fixup_depth_buffer(ctx);
765 if (ctx->fb_state.width != old_w || ctx->fb_state.height != old_h)
766 ctx->scissor_changed = true;
767 zink_update_framebuffer_state(ctx);
768 ctx->swapchain_size.width = ctx->swapchain_size.height = 0;
769 }
770 }
771