2018-08-31 16:50:20 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Collabora Ltd.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* on the rights to use, copy, modify, merge, publish, distribute, sub
|
|
|
|
* license, and/or sell copies of the Software, and to permit persons to whom
|
|
|
|
* the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zink_render_pass.h"
|
|
|
|
|
|
|
|
#include "zink_screen.h"
|
|
|
|
|
|
|
|
#include "util/u_memory.h"
|
|
|
|
#include "util/u_string.h"
|
|
|
|
|
|
|
|
static VkRenderPass
|
2021-06-17 15:51:27 -04:00
|
|
|
create_render_pass(VkDevice dev, struct zink_render_pass_state *state, struct zink_render_pass_pipeline_state *pstate)
|
2018-08-31 16:50:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
VkAttachmentReference color_refs[PIPE_MAX_COLOR_BUFS], zs_ref;
|
2021-07-27 14:04:14 -04:00
|
|
|
VkAttachmentReference input_attachments[PIPE_MAX_COLOR_BUFS];
|
2018-08-31 16:50:20 +02:00
|
|
|
VkAttachmentDescription attachments[PIPE_MAX_COLOR_BUFS + 1];
|
2021-03-03 14:54:05 -05:00
|
|
|
VkPipelineStageFlags dep_pipeline = 0;
|
|
|
|
VkAccessFlags dep_access = 0;
|
2021-07-27 14:04:14 -04:00
|
|
|
unsigned input_count = 0;
|
2018-08-31 16:50:20 +02:00
|
|
|
|
2021-06-17 15:51:27 -04:00
|
|
|
pstate->num_attachments = state->num_cbufs;
|
2018-08-31 16:50:20 +02:00
|
|
|
for (int i = 0; i < state->num_cbufs; i++) {
|
|
|
|
struct zink_rt_attrib *rt = state->rts + i;
|
|
|
|
attachments[i].flags = 0;
|
2021-06-17 15:51:27 -04:00
|
|
|
pstate->attachments[i].format = attachments[i].format = rt->format;
|
|
|
|
pstate->attachments[i].samples = attachments[i].samples = rt->samples;
|
2021-01-12 12:29:25 -05:00
|
|
|
attachments[i].loadOp = rt->clear_color ? VK_ATTACHMENT_LOAD_OP_CLEAR :
|
|
|
|
state->swapchain_init && rt->swapchain ?
|
|
|
|
VK_ATTACHMENT_LOAD_OP_DONT_CARE :
|
|
|
|
VK_ATTACHMENT_LOAD_OP_LOAD;
|
2018-08-31 16:50:20 +02:00
|
|
|
attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
|
|
|
attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
|
|
attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
2021-04-02 17:40:21 -04:00
|
|
|
/* if layout changes are ever handled here, need VkAttachmentSampleLocationsEXT */
|
2021-07-27 14:04:14 -04:00
|
|
|
VkImageLayout layout = rt->fbfetch ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
attachments[i].initialLayout = layout;
|
|
|
|
attachments[i].finalLayout = layout;
|
2018-08-31 16:50:20 +02:00
|
|
|
color_refs[i].attachment = i;
|
2021-07-27 14:04:14 -04:00
|
|
|
color_refs[i].layout = layout;
|
2021-03-03 14:54:05 -05:00
|
|
|
dep_pipeline |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
2021-07-27 14:04:14 -04:00
|
|
|
if (rt->fbfetch)
|
|
|
|
memcpy(&input_attachments[input_count++], &color_refs[i], sizeof(VkAttachmentReference));
|
2021-03-03 14:54:05 -05:00
|
|
|
dep_access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
if (attachments[i].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
|
|
|
|
dep_access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
|
2018-08-31 16:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int num_attachments = state->num_cbufs;
|
|
|
|
if (state->have_zsbuf) {
|
|
|
|
struct zink_rt_attrib *rt = state->rts + state->num_cbufs;
|
2021-03-03 15:26:02 -05:00
|
|
|
bool has_clear = rt->clear_color || rt->clear_stencil;
|
2021-07-27 14:04:14 -04:00
|
|
|
VkImageLayout write_layout = rt->fbfetch ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
VkImageLayout layout = rt->needs_write || has_clear ? write_layout : VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
|
2018-08-31 16:50:20 +02:00
|
|
|
attachments[num_attachments].flags = 0;
|
2021-06-17 15:51:27 -04:00
|
|
|
pstate->attachments[num_attachments].format = attachments[num_attachments].format = rt->format;
|
|
|
|
pstate->attachments[num_attachments].samples = attachments[num_attachments].samples = rt->samples;
|
2020-09-16 17:47:17 -04:00
|
|
|
attachments[num_attachments].loadOp = rt->clear_color ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
|
2018-08-31 16:50:20 +02:00
|
|
|
attachments[num_attachments].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
2020-09-16 17:47:17 -04:00
|
|
|
attachments[num_attachments].stencilLoadOp = rt->clear_stencil ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD;
|
2018-08-31 16:50:20 +02:00
|
|
|
attachments[num_attachments].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
|
2021-04-02 17:40:21 -04:00
|
|
|
/* if layout changes are ever handled here, need VkAttachmentSampleLocationsEXT */
|
2021-03-03 15:26:02 -05:00
|
|
|
attachments[num_attachments].initialLayout = layout;
|
|
|
|
attachments[num_attachments].finalLayout = layout;
|
2018-08-31 16:50:20 +02:00
|
|
|
|
2021-03-03 14:54:05 -05:00
|
|
|
dep_pipeline |= VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
|
2021-03-03 15:26:02 -05:00
|
|
|
if (layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)
|
|
|
|
dep_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
2021-03-03 14:54:05 -05:00
|
|
|
if (attachments[num_attachments].loadOp == VK_ATTACHMENT_LOAD_OP_LOAD ||
|
|
|
|
attachments[num_attachments].stencilLoadOp == VK_ATTACHMENT_LOAD_OP_LOAD)
|
|
|
|
dep_access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
|
|
|
|
|
2018-08-31 16:50:20 +02:00
|
|
|
zs_ref.attachment = num_attachments++;
|
2021-03-03 15:26:02 -05:00
|
|
|
zs_ref.layout = layout;
|
2021-06-17 15:51:27 -04:00
|
|
|
pstate->num_attachments++;
|
2018-08-31 16:50:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:54:05 -05:00
|
|
|
VkSubpassDependency deps[] = {
|
|
|
|
[0] = {VK_SUBPASS_EXTERNAL, 0, dep_pipeline, dep_pipeline, 0, dep_access, VK_DEPENDENCY_BY_REGION_BIT},
|
|
|
|
[1] = {0, VK_SUBPASS_EXTERNAL, dep_pipeline, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, dep_access, 0, VK_DEPENDENCY_BY_REGION_BIT}
|
|
|
|
};
|
|
|
|
|
2021-06-15 18:51:44 +08:00
|
|
|
VkSubpassDescription subpass = {0};
|
2018-08-31 16:50:20 +02:00
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
|
|
|
subpass.colorAttachmentCount = state->num_cbufs;
|
|
|
|
subpass.pColorAttachments = color_refs;
|
|
|
|
subpass.pDepthStencilAttachment = state->have_zsbuf ? &zs_ref : NULL;
|
2021-07-27 14:04:14 -04:00
|
|
|
subpass.inputAttachmentCount = input_count;
|
|
|
|
subpass.pInputAttachments = input_attachments;
|
2018-08-31 16:50:20 +02:00
|
|
|
|
2021-06-15 18:51:44 +08:00
|
|
|
VkRenderPassCreateInfo rpci = {0};
|
2018-08-31 16:50:20 +02:00
|
|
|
rpci.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
rpci.attachmentCount = num_attachments;
|
|
|
|
rpci.pAttachments = attachments;
|
|
|
|
rpci.subpassCount = 1;
|
|
|
|
rpci.pSubpasses = &subpass;
|
2021-03-03 14:54:05 -05:00
|
|
|
rpci.dependencyCount = 2;
|
|
|
|
rpci.pDependencies = deps;
|
2018-08-31 16:50:20 +02:00
|
|
|
|
|
|
|
VkRenderPass render_pass;
|
2020-12-18 09:47:23 -05:00
|
|
|
if (vkCreateRenderPass(dev, &rpci, NULL, &render_pass) != VK_SUCCESS) {
|
|
|
|
debug_printf("vkCreateRenderPass failed\n");
|
2018-08-31 16:50:20 +02:00
|
|
|
return VK_NULL_HANDLE;
|
2020-12-18 09:47:23 -05:00
|
|
|
}
|
2018-08-31 16:50:20 +02:00
|
|
|
|
|
|
|
return render_pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct zink_render_pass *
|
|
|
|
zink_create_render_pass(struct zink_screen *screen,
|
2021-06-17 15:51:27 -04:00
|
|
|
struct zink_render_pass_state *state,
|
|
|
|
struct zink_render_pass_pipeline_state *pstate)
|
2018-08-31 16:50:20 +02:00
|
|
|
{
|
|
|
|
struct zink_render_pass *rp = CALLOC_STRUCT(zink_render_pass);
|
|
|
|
if (!rp)
|
|
|
|
goto fail;
|
|
|
|
|
2021-06-17 15:51:27 -04:00
|
|
|
rp->render_pass = create_render_pass(screen->dev, state, pstate);
|
2018-08-31 16:50:20 +02:00
|
|
|
if (!rp->render_pass)
|
|
|
|
goto fail;
|
2020-12-09 15:26:30 -05:00
|
|
|
memcpy(&rp->state, state, sizeof(struct zink_render_pass_state));
|
2018-08-31 16:50:20 +02:00
|
|
|
return rp;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (rp)
|
|
|
|
zink_destroy_render_pass(screen, rp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zink_destroy_render_pass(struct zink_screen *screen,
|
|
|
|
struct zink_render_pass *rp)
|
|
|
|
{
|
|
|
|
vkDestroyRenderPass(screen->dev, rp->render_pass, NULL);
|
|
|
|
FREE(rp);
|
|
|
|
}
|
2021-03-03 15:26:02 -05:00
|
|
|
|
|
|
|
VkImageLayout
|
|
|
|
zink_render_pass_attachment_get_barrier_info(const struct zink_render_pass *rp, unsigned idx,
|
|
|
|
VkPipelineStageFlags *pipeline, VkAccessFlags *access)
|
|
|
|
{
|
|
|
|
*access = 0;
|
|
|
|
assert(idx < rp->state.num_rts);
|
|
|
|
const struct zink_rt_attrib *rt = &rp->state.rts[idx];
|
|
|
|
if (idx < rp->state.num_cbufs) {
|
|
|
|
*pipeline = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
*access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
if (!rt->clear_color && (!rp->state.swapchain_init || !rt->swapchain))
|
|
|
|
*access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
|
2021-07-27 14:04:14 -04:00
|
|
|
return rt->fbfetch ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
2021-03-03 15:26:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(rp->state.have_zsbuf);
|
|
|
|
*pipeline = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
|
|
|
|
if (!rp->state.rts[idx].clear_color && !rp->state.rts[idx].clear_stencil)
|
|
|
|
*access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
|
|
|
|
if (!rp->state.rts[idx].clear_color && !rp->state.rts[idx].clear_stencil && !rp->state.rts[idx].needs_write)
|
|
|
|
return VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
|
|
|
|
*access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
2021-07-27 14:04:14 -04:00
|
|
|
return rt->fbfetch ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
2021-03-03 15:26:02 -05:00
|
|
|
}
|