1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
//
// RenderPass.cs
//
// Author:
// Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// Copyright (c) 2019 jp
//
// 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 the rights
// to use, copy, modify, merge, publish, distribute, sublicense, 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 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 NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS 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.
using System;
using System.Collections.Generic;
using Vulkan;
using static Vulkan.VulkanNative;
namespace VKE {
public class RenderPass : Activable {
internal VkRenderPass handle;
internal List<VkAttachmentDescription> attachments = new List<VkAttachmentDescription> ();
public List<VkClearValue> ClearValues = new List<VkClearValue> ();
internal List<SubPass> subpasses = new List<SubPass> ();
List<VkSubpassDependency> dependencies = new List<VkSubpassDependency> ();
#region CTORS
public RenderPass (Device device) : base(device) { }
/// <summary>
/// Create default renderpass with one color and one depth attachments
/// </summary>
public RenderPass (Device device, VkFormat colorFormat, VkFormat depthFormat, VkSampleCountFlags samples = VkSampleCountFlags.Count1)
: this (device){
AddAttachment (colorFormat, (samples == VkSampleCountFlags.Count1) ? VkImageLayout.PresentSrcKHR : VkImageLayout.ColorAttachmentOptimal, samples);
AddAttachment (depthFormat, VkImageLayout.DepthStencilAttachmentOptimal, samples);
ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) });
ClearValues.Add (new VkClearValue { depthStencil = new VkClearDepthStencilValue (1.0f, 0) });
SubPass subpass0 = new SubPass ();
subpass0.AddColorReference (0, VkImageLayout.ColorAttachmentOptimal);
subpass0.SetDepthReference (1, VkImageLayout.DepthStencilAttachmentOptimal);
if (samples != VkSampleCountFlags.Count1) {
AddAttachment (colorFormat, VkImageLayout.PresentSrcKHR, VkSampleCountFlags.Count1);
ClearValues.Add (new VkClearValue { color = new VkClearColorValue (0.0f, 0.0f, 0.2f) });
subpass0.AddResolveReference (2, VkImageLayout.ColorAttachmentOptimal);
}
AddSubpass (subpass0);
AddDependency (VulkanNative.SubpassExternal, 0,
VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput,
VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite);
AddDependency (0, 1,
VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe,
VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead);
}
#endregion
public override void Activate () {
if (state != ActivableState.Activated) {
List<VkSubpassDescription> spDescs = new List<VkSubpassDescription> ();
foreach (SubPass sp in subpasses)
spDescs.Add (sp.SubpassDescription);
VkRenderPassCreateInfo renderPassInfo = VkRenderPassCreateInfo.New ();
renderPassInfo.attachmentCount = (uint)attachments.Count;
renderPassInfo.pAttachments = attachments.Pin ();
renderPassInfo.subpassCount = (uint)spDescs.Count;
renderPassInfo.pSubpasses = spDescs.Pin ();
renderPassInfo.dependencyCount = (uint)dependencies.Count;
renderPassInfo.pDependencies = dependencies.Pin ();
handle = dev.CreateRenderPass (renderPassInfo);
attachments.Unpin ();
spDescs.Unpin ();
dependencies.Unpin ();
}
base.Activate ();
}
public void AddAttachment (VkFormat format,
VkImageLayout finalLayout, VkSampleCountFlags samples = VkSampleCountFlags.Count1,
VkAttachmentLoadOp loadOp = VkAttachmentLoadOp.Clear,
VkAttachmentStoreOp storeOp = VkAttachmentStoreOp.Store,
VkImageLayout initialLayout = VkImageLayout.Undefined) {
attachments.Add (new VkAttachmentDescription {
format = format,
samples = samples,
loadOp = loadOp,
storeOp = storeOp,
stencilLoadOp = VkAttachmentLoadOp.DontCare,
stencilStoreOp = VkAttachmentStoreOp.DontCare,
initialLayout = initialLayout,
finalLayout = finalLayout,
});
}
public void AddAttachment (VkFormat format, VkImageLayout finalLayout,
VkAttachmentLoadOp stencilLoadOp,
VkAttachmentStoreOp stencilStoreOp,
VkAttachmentLoadOp loadOp = VkAttachmentLoadOp.DontCare,
VkAttachmentStoreOp storeOp = VkAttachmentStoreOp.DontCare,
VkSampleCountFlags samples = VkSampleCountFlags.Count1,
VkImageLayout initialLayout = VkImageLayout.Undefined) {
attachments.Add (new VkAttachmentDescription {
format = format,
samples = samples,
loadOp = loadOp,
storeOp = storeOp,
stencilLoadOp = stencilLoadOp,
stencilStoreOp = stencilStoreOp,
initialLayout = initialLayout,
finalLayout = finalLayout,
});
}
public void AddDependency (uint srcSubpass, uint dstSubpass,
VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask,
VkDependencyFlags dependencyFlags = VkDependencyFlags.ByRegion) {
VkSubpassDependency dep = new VkSubpassDependency {
srcSubpass = srcSubpass,
dstSubpass = dstSubpass,
srcStageMask = srcStageMask,
dstStageMask = dstStageMask,
srcAccessMask = srcAccessMask,
dstAccessMask = dstAccessMask,
dependencyFlags = dependencyFlags
};
}
public void AddSubpass (SubPass subPass) {
subpasses.Add (subPass);
}
/// <summary>
/// Begin Render pass with framebuffer extent dimensions
/// </summary>
public void Begin (CommandBuffer cmd, Framebuffer frameBuffer) {
Begin (cmd, frameBuffer, frameBuffer.Width, frameBuffer.Height);
}
/// <summary>
/// Begin Render pass with custom render area
/// </summary>
public unsafe void Begin (CommandBuffer cmd, Framebuffer frameBuffer, uint width, uint height) {
VkRenderPassBeginInfo info = VkRenderPassBeginInfo.New ();
info.renderPass = handle;
info.renderArea.extent.width = width;
info.renderArea.extent.height = height;
info.clearValueCount = (uint)ClearValues.Count;
info.pClearValues = ClearValues.Pin ();
info.framebuffer = frameBuffer.handle;
vkCmdBeginRenderPass (cmd.Handle, ref info, VkSubpassContents.Inline);
ClearValues.Unpin ();
}
public void End (CommandBuffer cmd) {
vkCmdEndRenderPass (cmd.Handle);
}
#region IDisposable Support
protected override void Dispose (bool disposing) {
if (!disposing)
System.Diagnostics.Debug.WriteLine ("VKE Activable RenderPass disposed by finalizer");
if (state == ActivableState.Activated)
dev.DestroyRenderPass (handle);
base.Dispose (disposing);
}
#endregion
}
}
|