1 /* Copyright (c) 2015-2018 The Khronos Group Inc.
2  * Copyright (c) 2015-2018 Valve Corporation
3  * Copyright (c) 2015-2018 LunarG, Inc.
4  * Copyright (C) 2015-2018 Google Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Tobias Hector <@tobski>
19  */
20 
21 #include <string.h>
22 
23 #include "convert_to_renderpass2.h"
24 #include "vk_typemap_helper.h"
25 #include "vk_format_utils.h"
26 
ConvertVkAttachmentReferenceToV2KHR(const VkAttachmentReference * in_struct,safe_VkAttachmentReference2KHR * out_struct)27 static void ConvertVkAttachmentReferenceToV2KHR(const VkAttachmentReference* in_struct,
28                                                 safe_VkAttachmentReference2KHR* out_struct) {
29     out_struct->sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2_KHR;
30     out_struct->pNext = nullptr;
31     out_struct->attachment = in_struct->attachment;
32     out_struct->layout = in_struct->layout;
33     out_struct->aspectMask =
34         VK_IMAGE_ASPECT_FLAG_BITS_MAX_ENUM;  // Uninitialized - must be filled in by top level struct for input attachments
35 }
36 
ConvertVkSubpassDependencyToV2KHR(const VkSubpassDependency * in_struct,safe_VkSubpassDependency2KHR * out_struct)37 static void ConvertVkSubpassDependencyToV2KHR(const VkSubpassDependency* in_struct, safe_VkSubpassDependency2KHR* out_struct) {
38     out_struct->sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2_KHR;
39     out_struct->pNext = nullptr;
40     out_struct->srcSubpass = in_struct->srcSubpass;
41     out_struct->dstSubpass = in_struct->dstSubpass;
42     out_struct->srcStageMask = in_struct->srcStageMask;
43     out_struct->dstStageMask = in_struct->dstStageMask;
44     out_struct->srcAccessMask = in_struct->srcAccessMask;
45     out_struct->dstAccessMask = in_struct->dstAccessMask;
46     out_struct->dependencyFlags = in_struct->dependencyFlags;
47     out_struct->viewOffset = 0;
48 }
49 
ConvertVkSubpassDescriptionToV2KHR(const VkSubpassDescription * in_struct,safe_VkSubpassDescription2KHR * out_struct)50 static void ConvertVkSubpassDescriptionToV2KHR(const VkSubpassDescription* in_struct, safe_VkSubpassDescription2KHR* out_struct) {
51     out_struct->sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2_KHR;
52     out_struct->pNext = nullptr;
53     out_struct->flags = in_struct->flags;
54     out_struct->pipelineBindPoint = in_struct->pipelineBindPoint;
55     out_struct->viewMask = 0;
56     out_struct->inputAttachmentCount = in_struct->inputAttachmentCount;
57     out_struct->pInputAttachments = nullptr;
58     out_struct->colorAttachmentCount = in_struct->colorAttachmentCount;
59     out_struct->pColorAttachments = nullptr;
60     out_struct->pResolveAttachments = nullptr;
61     out_struct->preserveAttachmentCount = in_struct->preserveAttachmentCount;
62     out_struct->pPreserveAttachments = nullptr;
63 
64     if (out_struct->inputAttachmentCount && in_struct->pInputAttachments) {
65         out_struct->pInputAttachments = new safe_VkAttachmentReference2KHR[out_struct->inputAttachmentCount];
66         for (uint32_t i = 0; i < out_struct->inputAttachmentCount; ++i) {
67             ConvertVkAttachmentReferenceToV2KHR(&in_struct->pInputAttachments[i], &out_struct->pInputAttachments[i]);
68         }
69     }
70     if (out_struct->colorAttachmentCount && in_struct->pColorAttachments) {
71         out_struct->pColorAttachments = new safe_VkAttachmentReference2KHR[out_struct->colorAttachmentCount];
72         for (uint32_t i = 0; i < out_struct->colorAttachmentCount; ++i) {
73             ConvertVkAttachmentReferenceToV2KHR(&in_struct->pColorAttachments[i], &out_struct->pColorAttachments[i]);
74         }
75     }
76     if (out_struct->colorAttachmentCount && in_struct->pResolveAttachments) {
77         out_struct->pResolveAttachments = new safe_VkAttachmentReference2KHR[out_struct->colorAttachmentCount];
78         for (uint32_t i = 0; i < out_struct->colorAttachmentCount; ++i) {
79             ConvertVkAttachmentReferenceToV2KHR(&in_struct->pResolveAttachments[i], &out_struct->pResolveAttachments[i]);
80         }
81     }
82     if (in_struct->pDepthStencilAttachment) {
83         out_struct->pDepthStencilAttachment = new safe_VkAttachmentReference2KHR();
84         ConvertVkAttachmentReferenceToV2KHR(in_struct->pDepthStencilAttachment, out_struct->pDepthStencilAttachment);
85     } else {
86         out_struct->pDepthStencilAttachment = NULL;
87     }
88     if (in_struct->pPreserveAttachments) {
89         out_struct->pPreserveAttachments = new uint32_t[in_struct->preserveAttachmentCount];
90         memcpy((void*)out_struct->pPreserveAttachments, (void*)in_struct->pPreserveAttachments,
91                sizeof(uint32_t) * in_struct->preserveAttachmentCount);
92     }
93 }
94 
ConvertVkAttachmentDescriptionToV2KHR(const VkAttachmentDescription * in_struct,safe_VkAttachmentDescription2KHR * out_struct)95 static void ConvertVkAttachmentDescriptionToV2KHR(const VkAttachmentDescription* in_struct,
96                                                   safe_VkAttachmentDescription2KHR* out_struct) {
97     out_struct->sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2_KHR;
98     out_struct->pNext = nullptr;
99     out_struct->flags = in_struct->flags;
100     out_struct->format = in_struct->format;
101     out_struct->samples = in_struct->samples;
102     out_struct->loadOp = in_struct->loadOp;
103     out_struct->storeOp = in_struct->storeOp;
104     out_struct->stencilLoadOp = in_struct->stencilLoadOp;
105     out_struct->stencilStoreOp = in_struct->stencilStoreOp;
106     out_struct->initialLayout = in_struct->initialLayout;
107     out_struct->finalLayout = in_struct->finalLayout;
108 }
109 
ConvertVkRenderPassCreateInfoToV2KHR(const VkRenderPassCreateInfo * in_struct,safe_VkRenderPassCreateInfo2KHR * out_struct)110 void ConvertVkRenderPassCreateInfoToV2KHR(const VkRenderPassCreateInfo* in_struct, safe_VkRenderPassCreateInfo2KHR* out_struct) {
111     out_struct->sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2_KHR;
112     out_struct->pNext = nullptr;
113     out_struct->flags = in_struct->flags;
114     out_struct->attachmentCount = in_struct->attachmentCount;
115     out_struct->pAttachments = nullptr;
116     out_struct->subpassCount = in_struct->subpassCount;
117     out_struct->pSubpasses = nullptr;
118     out_struct->dependencyCount = in_struct->dependencyCount;
119     out_struct->pDependencies = nullptr;
120     out_struct->correlatedViewMaskCount = 0;
121     out_struct->pCorrelatedViewMasks = nullptr;
122     if (out_struct->attachmentCount && in_struct->pAttachments) {
123         out_struct->pAttachments = new safe_VkAttachmentDescription2KHR[out_struct->attachmentCount];
124         for (uint32_t i = 0; i < out_struct->attachmentCount; ++i) {
125             ConvertVkAttachmentDescriptionToV2KHR(&in_struct->pAttachments[i], &out_struct->pAttachments[i]);
126         }
127     }
128     if (out_struct->subpassCount && in_struct->pSubpasses) {
129         out_struct->pSubpasses = new safe_VkSubpassDescription2KHR[out_struct->subpassCount];
130         for (uint32_t i = 0; i < out_struct->subpassCount; ++i) {
131             ConvertVkSubpassDescriptionToV2KHR(&in_struct->pSubpasses[i], &out_struct->pSubpasses[i]);
132         }
133     }
134     if (out_struct->dependencyCount && in_struct->pDependencies) {
135         out_struct->pDependencies = new safe_VkSubpassDependency2KHR[out_struct->dependencyCount];
136         for (uint32_t i = 0; i < out_struct->dependencyCount; ++i) {
137             ConvertVkSubpassDependencyToV2KHR(&in_struct->pDependencies[i], &out_struct->pDependencies[i]);
138         }
139     }
140 
141     // Handle extension structs from KHR_multiview and KHR_maintenance2 to fill out the "filled in" bits.
142     if (in_struct->pNext) {
143         const VkRenderPassMultiviewCreateInfo* pMultiviewInfo =
144             lvl_find_in_chain<VkRenderPassMultiviewCreateInfo>(in_struct->pNext);
145         if (pMultiviewInfo) {
146             for (uint32_t subpass = 0; subpass < pMultiviewInfo->subpassCount; ++subpass) {
147                 if (subpass < in_struct->subpassCount) {
148                     out_struct->pSubpasses[subpass].viewMask = pMultiviewInfo->pViewMasks[subpass];
149                 }
150             }
151             for (uint32_t dependency = 0; dependency < pMultiviewInfo->dependencyCount; ++dependency) {
152                 if (dependency < in_struct->dependencyCount) {
153                     out_struct->pDependencies[dependency].viewOffset = pMultiviewInfo->pViewOffsets[dependency];
154                 }
155             }
156             if (pMultiviewInfo->correlationMaskCount) {
157                 out_struct->correlatedViewMaskCount = pMultiviewInfo->correlationMaskCount;
158                 uint32_t* pCorrelatedViewMasks = new uint32_t[out_struct->correlatedViewMaskCount];
159                 for (uint32_t correlationMask = 0; correlationMask < pMultiviewInfo->correlationMaskCount; ++correlationMask) {
160                     pCorrelatedViewMasks[correlationMask] = pMultiviewInfo->pCorrelationMasks[correlationMask];
161                 }
162                 out_struct->pCorrelatedViewMasks = pCorrelatedViewMasks;
163             }
164         }
165         const VkRenderPassInputAttachmentAspectCreateInfo* pInputAttachmentAspectInfo =
166             lvl_find_in_chain<VkRenderPassInputAttachmentAspectCreateInfo>(in_struct->pNext);
167         if (pInputAttachmentAspectInfo) {
168             for (uint32_t i = 0; i < pInputAttachmentAspectInfo->aspectReferenceCount; ++i) {
169                 uint32_t subpass = pInputAttachmentAspectInfo->pAspectReferences[i].subpass;
170                 uint32_t attachment = pInputAttachmentAspectInfo->pAspectReferences[i].inputAttachmentIndex;
171                 VkImageAspectFlags aspectMask = pInputAttachmentAspectInfo->pAspectReferences[i].aspectMask;
172                 if (subpass < in_struct->subpassCount && attachment < in_struct->pSubpasses[subpass].inputAttachmentCount) {
173                     out_struct->pSubpasses[subpass].pInputAttachments[attachment].aspectMask = aspectMask;
174                 }
175             }
176         }
177     }
178 
179     if (out_struct->subpassCount && out_struct->pSubpasses) {
180         for (uint32_t i = 0; i < out_struct->subpassCount; ++i) {
181             if (out_struct->pSubpasses[i].inputAttachmentCount && out_struct->pSubpasses[i].pInputAttachments) {
182                 for (uint32_t j = 0; j < out_struct->pSubpasses[i].inputAttachmentCount; ++j) {
183                     safe_VkAttachmentReference2KHR& attachment_ref = out_struct->pSubpasses[i].pInputAttachments[j];
184                     if (attachment_ref.aspectMask == VK_IMAGE_ASPECT_FLAG_BITS_MAX_ENUM &&
185                         attachment_ref.attachment < out_struct->attachmentCount && out_struct->pAttachments) {
186                         attachment_ref.aspectMask = 0;
187                         VkFormat attachmentFormat = out_struct->pAttachments[attachment_ref.attachment].format;
188                         if (FormatIsColor(attachmentFormat)) {
189                             attachment_ref.aspectMask |= VK_IMAGE_ASPECT_COLOR_BIT;
190                         }
191                         if (FormatHasDepth(attachmentFormat)) {
192                             attachment_ref.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
193                         }
194                         if (FormatHasStencil(attachmentFormat)) {
195                             attachment_ref.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
196                         }
197                     }
198                 }
199             }
200         }
201     }
202 }
203