Skip to content

Commit 687e068

Browse files
committed
Update Framebuffer
- add level param - fix bug with RenderPass
1 parent 0e2447f commit 687e068

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

src/main/java/net/vulkanmod/vulkan/framebuffer/Framebuffer.java

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
public class Framebuffer {
1919
public static final int DEFAULT_FORMAT = VK_FORMAT_R8G8B8A8_UNORM;
2020

21-
// private long id;
22-
21+
public final String name;
2322
protected int format;
2423
protected int depthFormat;
2524
protected int width, height;
@@ -33,12 +32,17 @@ public class Framebuffer {
3332
private VulkanImage colorAttachment;
3433
protected VulkanImage depthAttachment;
3534

35+
private int level;
36+
3637
private final Reference2LongArrayMap<RenderPass> renderpassToFramebufferMap = new Reference2LongArrayMap<>();
3738

38-
//SwapChain
39-
protected Framebuffer() {}
39+
// SwapChain
40+
protected Framebuffer() {
41+
this.name = null;
42+
}
4043

4144
public Framebuffer(Builder builder) {
45+
this.name = builder.name;
4246
this.format = builder.format;
4347
this.depthFormat = builder.depthFormat;
4448
this.width = builder.width;
@@ -54,22 +58,30 @@ public Framebuffer(Builder builder) {
5458
this.colorAttachment = builder.colorAttachment;
5559
this.depthAttachment = builder.depthAttachment;
5660
}
61+
62+
this.level = builder.level;
5763
}
5864

5965
public void createImages() {
6066
if (this.hasColorAttachment) {
61-
this.colorAttachment = VulkanImage.builder(this.width, this.height)
62-
.setFormat(format)
63-
.setUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
64-
.setLinearFiltering(linearFiltering)
65-
.setClamp(true)
66-
.createVulkanImage();
67+
this.colorAttachment =
68+
VulkanImage.builder(this.width, this.height)
69+
.setName(this.name != null ? String.format("%s Color", this.name) : null)
70+
.setFormat(format)
71+
.setUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
72+
.setLinearFiltering(linearFiltering)
73+
.setClamp(true)
74+
.createVulkanImage();
6775
}
6876

6977
if (this.hasDepthAttachment) {
70-
this.depthAttachment = VulkanImage.createDepthImage(depthFormat, this.width, this.height,
71-
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
72-
depthLinearFiltering, true);
78+
this.depthAttachment = VulkanImage.builder(width, height)
79+
.setName(this.name != null ? String.format("%s Depth", this.name) : null)
80+
.setFormat(depthFormat)
81+
.setUsage(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
82+
.setLinearFiltering(depthLinearFiltering)
83+
.setClamp(true)
84+
.createVulkanImage();
7385

7486
this.attachmentCount++;
7587
}
@@ -85,7 +97,6 @@ public void resize(int newWidth, int newHeight) {
8597
}
8698

8799
private long createFramebuffer(RenderPass renderPass) {
88-
89100
try (MemoryStack stack = MemoryStack.stackPush()) {
90101

91102
LongBuffer attachments;
@@ -116,10 +127,13 @@ private long createFramebuffer(RenderPass renderPass) {
116127
}
117128

118129
public void beginRenderPass(VkCommandBuffer commandBuffer, RenderPass renderPass, MemoryStack stack) {
130+
renderPass.setFramebuffer(this);
131+
119132
if (!DYNAMIC_RENDERING) {
120133
long framebufferId = this.getFramebufferId(renderPass);
121134
renderPass.beginRenderPass(commandBuffer, framebufferId, stack);
122-
} else {
135+
}
136+
else {
123137
renderPass.beginDynamicRendering(commandBuffer, stack);
124138
}
125139
}
@@ -173,6 +187,17 @@ public void cleanUp(boolean cleanImages) {
173187
renderpassToFramebufferMap.clear();
174188
}
175189

190+
public void setLevel(int level) {
191+
int maxLevel = this.colorAttachment.mipLevels - 1;
192+
if (level > maxLevel) {
193+
throw new IllegalStateException(
194+
"Requested mip level (%d) greater than color attachments max mip level (%d)"
195+
.formatted(level, maxLevel));
196+
}
197+
198+
this.level = level;
199+
}
200+
176201
public long getDepthImageView() {
177202
return depthAttachment.getImageView();
178203
}
@@ -185,6 +210,10 @@ public VulkanImage getColorAttachment() {
185210
return colorAttachment;
186211
}
187212

213+
public long getColorAttachmentView() {
214+
return colorAttachment.getLevelImageView(level);
215+
}
216+
188217
public int getWidth() {
189218
return this.width;
190219
}
@@ -210,26 +239,33 @@ public static Builder builder(VulkanImage colorAttachment, VulkanImage depthAtta
210239
}
211240

212241
public static class Builder {
242+
final String name;
213243
final boolean createImages;
214244
final int width, height;
215245
int format, depthFormat;
216246

217247
VulkanImage colorAttachment;
218248
VulkanImage depthAttachment;
219249

220-
// int colorAttachments;
221250
boolean hasColorAttachment;
222251
boolean hasDepthAttachment;
223252

224253
boolean linearFiltering;
225254
boolean depthLinearFiltering;
226255

256+
int level = 0;
257+
227258
public Builder(int width, int height, int colorAttachments, boolean hasDepthAttachment) {
259+
this(null, width, height, colorAttachments, hasDepthAttachment);
260+
}
261+
262+
public Builder(String name, int width, int height, int colorAttachments, boolean hasDepthAttachment) {
228263
Validate.isTrue(colorAttachments > 0 || hasDepthAttachment, "At least 1 attachment needed");
229264

230265
//TODO multi color attachments
231266
Validate.isTrue(colorAttachments <= 1, "Not supported");
232267

268+
this.name = name;
233269
this.createImages = true;
234270
this.format = DEFAULT_FORMAT;
235271
this.depthFormat = Vulkan.getDefaultDepthFormat();
@@ -243,6 +279,7 @@ public Builder(int width, int height, int colorAttachments, boolean hasDepthAtta
243279
}
244280

245281
public Builder(VulkanImage colorAttachment, VulkanImage depthAttachment) {
282+
this.name = null;
246283
this.createImages = false;
247284
this.colorAttachment = colorAttachment;
248285
this.depthAttachment = depthAttachment;
@@ -263,6 +300,12 @@ public Framebuffer build() {
263300
return new Framebuffer(this);
264301
}
265302

303+
public Builder setLevel(int level) {
304+
this.level = level;
305+
306+
return this;
307+
}
308+
266309
public Builder setFormat(int format) {
267310
this.format = format;
268311

src/main/java/net/vulkanmod/vulkan/framebuffer/RenderPass.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,16 @@ public Framebuffer getFramebuffer() {
263263
return framebuffer;
264264
}
265265

266-
public void cleanUp() {
267-
//TODO
268-
269-
if (!Vulkan.DYNAMIC_RENDERING)
270-
MemoryManager.getInstance().addFrameOp(
271-
() -> vkDestroyRenderPass(Vulkan.getVkDevice(), this.id, null));
266+
public void setFramebuffer(Framebuffer framebuffer) {
267+
this.framebuffer = framebuffer;
268+
}
272269

270+
public void cleanUp() {
271+
if (!Vulkan.DYNAMIC_RENDERING) {
272+
MemoryManager.getInstance()
273+
.addFrameOp(
274+
() -> vkDestroyRenderPass(Vulkan.getVkDevice(), this.id, null));
275+
}
273276
}
274277

275278
public long getId() {

0 commit comments

Comments
 (0)