Skip to content

Commit bbf81b6

Browse files
committed
Improve compatibility with GlTexture
- Implement clearDepthTexture method
1 parent 7a500e9 commit bbf81b6

7 files changed

Lines changed: 103 additions & 68 deletions

File tree

src/main/java/net/vulkanmod/mixin/render/target/MainTargetMixin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ public GpuTexture getColorTexture() {
6666
public GpuTextureView getColorTextureView() {
6767
return Renderer.getInstance().getMainPass().getColorAttachmentView();
6868
}
69+
70+
@Override
71+
public GpuTexture getDepthTexture() {
72+
return Renderer.getInstance().getMainPass().getDepthAttachment();
73+
}
6974
}

src/main/java/net/vulkanmod/render/engine/VkCommandEncoder.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.minecraft.client.Minecraft;
1818
import net.minecraft.util.ARGB;
1919
import net.vulkanmod.gl.VkGlFramebuffer;
20+
import net.vulkanmod.gl.VkGlTexture;
2021
import net.vulkanmod.interfaces.shader.ExtendedRenderPipeline;
2122
import net.vulkanmod.vulkan.Renderer;
2223
import net.vulkanmod.vulkan.Synchronization;
@@ -196,9 +197,8 @@ public void clearColorAndDepthTextures(GpuTexture colorAttachment, int clearColo
196197
else {
197198
VkFbo fbo = ((VkGpuTexture)colorAttachment).getFbo(depthAttachment);
198199

199-
fbo.clear = 0x4100;
200-
fbo.clearColor = clearColor;
201-
fbo.clearDepth = (float) clearDepth;
200+
((VkGpuTexture) colorAttachment).setClearColor(clearColor);
201+
((VkGpuTexture) depthAttachment).setDepthClearValue((float) clearDepth);
202202

203203
Framebuffer boundFramebuffer = Renderer.getInstance().getBoundFramebuffer();
204204
if (boundFramebuffer.getColorAttachment() == ((VkGpuTexture) colorAttachment).getVulkanImage()
@@ -240,9 +240,14 @@ public void clearDepthTexture(GpuTexture depthAttachment, double clearDepth) {
240240
throw new IllegalStateException("Close the existing render pass before creating a new one!");
241241
}
242242
else {
243-
// depthAttachment is not the target here
244-
VRenderSystem.clearDepth(clearDepth);
245-
Renderer.clearAttachments(256);
243+
Framebuffer boundFramebuffer = Renderer.getInstance().getBoundFramebuffer();
244+
if (boundFramebuffer.getDepthAttachment() == ((VkGpuTexture) depthAttachment).getVulkanImage()) {
245+
VRenderSystem.clearDepth(clearDepth);
246+
Renderer.clearAttachments(0x100);
247+
}
248+
else {
249+
((VkGpuTexture) depthAttachment).setDepthClearValue((float) clearDepth);
250+
}
246251
}
247252
}
248253

@@ -411,7 +416,9 @@ public void writeToTexture(GpuTexture gpuTexture, NativeImage nativeImage, int l
411416
throw new IllegalStateException("Destination texture is closed");
412417
} else {
413418
VTextureSelector.setActiveTexture(0);
414-
VTextureSelector.bindTexture(((VkGpuTexture) gpuTexture).getVulkanImage());
419+
var glTexture = VkGlTexture.getTexture(((GlTexture) gpuTexture).glId());
420+
// VTextureSelector.bindTexture(((VkGpuTexture) gpuTexture).getVulkanImage());
421+
VTextureSelector.bindTexture(glTexture.getVulkanImage());
415422
VTextureSelector.uploadSubTexture(level, arrayLayer, width, height, xOffset, yOffset, unpackSkipRows, unpackSkipPixels, nativeImage.getWidth(), nativeImage.getPointer());
416423
}
417424
} else {

src/main/java/net/vulkanmod/render/engine/VkFbo.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@
55
import net.vulkanmod.gl.VkGlFramebuffer;
66
import net.vulkanmod.vulkan.Renderer;
77
import net.vulkanmod.vulkan.VRenderSystem;
8-
import net.vulkanmod.vulkan.framebuffer.Framebuffer;
98
import org.lwjgl.opengl.GL33;
10-
import org.lwjgl.system.MemoryStack;
11-
12-
import static org.lwjgl.system.MemoryStack.stackPush;
139

1410
public class VkFbo {
1511
final int glId;
1612
final VkGpuTexture colorAttachment;
1713
final VkGpuTexture depthAttachment;
1814

19-
int clear = 0;
20-
int clearColor = 0;
21-
float clearDepth = 0.0f;
22-
2315
protected VkFbo(VkGpuTexture colorAttachment, VkGpuTexture depthAttachment) {
2416
this.glId = GlStateManager.glGenFramebuffers();
2517
this.colorAttachment = colorAttachment;
@@ -35,25 +27,35 @@ protected VkFbo(VkGpuTexture colorAttachment, VkGpuTexture depthAttachment) {
3527
}
3628

3729
protected void bind() {
38-
// VkGlFramebuffer glFramebuffer = VkGlFramebuffer.getFramebuffer(this.glId);
39-
// VkGlFramebuffer.beginRendering(glFramebuffer);
40-
//
41-
// Framebuffer framebuffer = glFramebuffer.getFramebuffer();
42-
// try (MemoryStack stack = stackPush()) {
43-
// framebuffer.beginRenderPass(currentCmdBuffer, renderPass, stack);
44-
// }
45-
4630
VkGlFramebuffer.bindFramebuffer(GL33.GL_FRAMEBUFFER, this.glId);
4731
clearAttachments();
4832
}
4933

5034
protected void clearAttachments() {
51-
if (clear != 0) {
52-
VRenderSystem.clearDepth(clearDepth);
35+
int clear = 0;
36+
float clearDepth;
37+
int clearColor;
38+
39+
if (colorAttachment.needsClear()) {
40+
clear |= 0x4000;
41+
clearColor = colorAttachment.clearColor;
42+
5343
VRenderSystem.setClearColor(ARGB.redFloat(clearColor), ARGB.greenFloat(clearColor), ARGB.blueFloat(clearColor), ARGB.alphaFloat(clearColor));
54-
Renderer.clearAttachments(clear);
5544

56-
clear = 0;
45+
colorAttachment.needsClear = false;
46+
}
47+
48+
if (depthAttachment != null && depthAttachment.needsClear()) {
49+
clear |= 0x100;
50+
clearDepth = depthAttachment.depthClearValue;
51+
52+
VRenderSystem.clearDepth(clearDepth);
53+
54+
depthAttachment.needsClear = false;
55+
}
56+
57+
if (clear != 0) {
58+
Renderer.clearAttachments(clear);
5759
}
5860
}
5961

@@ -62,6 +64,6 @@ protected void close() {
6264
}
6365

6466
public boolean needsClear() {
65-
return this.clear != 0;
67+
return this.colorAttachment.needsClear() || (this.depthAttachment != null && this.depthAttachment.needsClear());
6668
}
6769
}

src/main/java/net/vulkanmod/render/engine/VkGpuDevice.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public GpuTextureView createTextureView(GpuTexture gpuTexture, int startLevel, i
131131
if (gpuTexture.isClosed()) {
132132
throw new IllegalArgumentException("Can't create texture view with closed texture");
133133
} else if (startLevel >= 0 && startLevel + levels <= gpuTexture.getMipLevels()) {
134+
135+
// Try to convert gpuTexture to VkGpuTexture in case it's not
136+
if (gpuTexture.getClass() != VkGpuTexture.class) {
137+
gpuTexture = VkGpuTexture.fromGlTexture((GlTexture) gpuTexture);
138+
}
139+
134140
return new VkTextureView((VkGpuTexture) gpuTexture, startLevel, levels);
135141
} else {
136142
throw new IllegalArgumentException(

src/main/java/net/vulkanmod/render/engine/VkGpuTexture.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
package net.vulkanmod.render.engine;
22

3-
import com.mojang.blaze3d.opengl.GlConst;
43
import com.mojang.blaze3d.opengl.GlStateManager;
54
import com.mojang.blaze3d.opengl.GlTexture;
65
import com.mojang.blaze3d.textures.AddressMode;
76
import com.mojang.blaze3d.textures.FilterMode;
87
import com.mojang.blaze3d.textures.GpuTexture;
98
import com.mojang.blaze3d.textures.TextureFormat;
109
import it.unimi.dsi.fastutil.ints.*;
10+
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
1111
import net.fabricmc.api.EnvType;
1212
import net.fabricmc.api.Environment;
1313
import net.vulkanmod.gl.VkGlTexture;
1414
import net.vulkanmod.vulkan.texture.SamplerManager;
1515
import net.vulkanmod.vulkan.texture.VulkanImage;
1616
import org.jetbrains.annotations.Nullable;
17-
import org.lwjgl.opengl.GL11;
1817
import org.lwjgl.vulkan.VK10;
1918

2019
@Environment(EnvType.CLIENT)
21-
//public class VkGpuTexture extends GpuTexture {
2220
public class VkGpuTexture extends GlTexture {
21+
private static final Reference2ReferenceOpenHashMap<GlTexture, VkGpuTexture> glToVkMap = new Reference2ReferenceOpenHashMap<>();
22+
2323
protected VkGlTexture glTexture;
2424
protected final int id;
2525
private final Int2ReferenceMap<VkFbo> fboCache = new Int2ReferenceOpenHashMap<>();
2626
protected boolean closed;
2727
protected boolean modesDirty = true;
2828

29+
boolean needsClear = false;
30+
int clearColor = 0;
31+
float depthClearValue = 1.0f;
32+
2933
protected VkGpuTexture(int usage, String string, TextureFormat textureFormat, int width, int height, int layers, int mipLevel, int id, VkGlTexture glTexture) {
3034
super(usage, string, textureFormat, width, height, layers, mipLevel, id);
3135
this.id = id;
@@ -49,35 +53,8 @@ public boolean isClosed() {
4953
return this.closed;
5054
}
5155

52-
// public int getFbo(DirectStateAccess directStateAccess, @Nullable GpuTexture gpuTexture) {
53-
// int i = gpuTexture == null ? 0 : ((VkGpuTexture)gpuTexture).id;
54-
// return this.fboCache.computeIfAbsent(i, j -> {
55-
// int k = directStateAccess.createFrameBufferObject();
56-
// directStateAccess.bindFrameBufferTextures(k, this.id, i, 0, 0);
57-
// return k;
58-
// });
59-
// }
60-
6156
public void flushModeChanges() {
6257
if (this.modesDirty) {
63-
// GlStateManager._texParameter(3553, 10242, GlConst.toGl(this.addressModeU));
64-
// GlStateManager._texParameter(3553, 10243, GlConst.toGl(this.addressModeV));
65-
// switch (this.minFilter) {
66-
// case NEAREST:
67-
// GlStateManager._texParameter(3553, 10241, this.useMipmaps ? 9986 : 9728);
68-
// break;
69-
// case LINEAR:
70-
// GlStateManager._texParameter(3553, 10241, this.useMipmaps ? 9987 : 9729);
71-
// }
72-
//
73-
// switch (this.magFilter) {
74-
// case NEAREST:
75-
// GlStateManager._texParameter(3553, 10240, 9728);
76-
// break;
77-
// case LINEAR:
78-
// GlStateManager._texParameter(3553, 10240, 9729);
79-
// }
80-
8158
byte samplerFlags;
8259
samplerFlags = magFilter == FilterMode.LINEAR ? SamplerManager.LINEAR_FILTERING_BIT : 0;
8360

@@ -115,6 +92,20 @@ public void setUseMipmaps(boolean bl) {
11592
this.modesDirty = true;
11693
}
11794

95+
public void setClearColor(int clearColor) {
96+
this.needsClear = true;
97+
this.clearColor = clearColor;
98+
}
99+
100+
public void setDepthClearValue(float depthClearValue) {
101+
this.needsClear = true;
102+
this.depthClearValue = depthClearValue;
103+
}
104+
105+
public boolean needsClear() {
106+
return needsClear;
107+
}
108+
118109
public VkFbo getFbo(@Nullable GpuTexture depthAttachment) {
119110
int depthAttachmentId = depthAttachment == null ? 0 : ((VkGpuTexture)depthAttachment).id;
120111
return this.fboCache.computeIfAbsent(depthAttachmentId, j -> new VkFbo(this, (VkGpuTexture) depthAttachment));
@@ -124,6 +115,20 @@ public VulkanImage getVulkanImage() {
124115
return glTexture.getVulkanImage();
125116
}
126117

118+
public static VkGpuTexture fromGlTexture(GlTexture glTexture) {
119+
return glToVkMap.computeIfAbsent(glTexture, glTexture1 -> {
120+
var name = glTexture.getLabel();
121+
int id = glTexture.glId();
122+
VkGlTexture vglTexture = VkGlTexture.getTexture(id);
123+
VkGpuTexture gpuTexture = new VkGpuTexture(0, name, glTexture.getFormat(),
124+
glTexture.getWidth(0), glTexture.getHeight(0),
125+
1, glTexture.getMipLevels(),
126+
glTexture.glId(), vglTexture);
127+
128+
return gpuTexture;
129+
});
130+
}
131+
127132
public static TextureFormat textureFormat(int format) {
128133
return switch (format) {
129134
case VK10.VK_FORMAT_R8G8B8A8_UNORM, VK10.VK_FORMAT_B8G8R8A8_UNORM -> TextureFormat.RGBA8;

src/main/java/net/vulkanmod/vulkan/pass/DefaultMainPass.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package net.vulkanmod.vulkan.pass;
22

3-
import com.mojang.blaze3d.pipeline.RenderTarget;
43
import com.mojang.blaze3d.systems.RenderSystem;
54
import com.mojang.blaze3d.textures.GpuTexture;
65
import com.mojang.blaze3d.textures.GpuTextureView;
7-
import net.minecraft.client.Minecraft;
86
import net.vulkanmod.render.engine.VkGpuDevice;
97
import net.vulkanmod.render.engine.VkGpuTexture;
108
import net.vulkanmod.vulkan.Renderer;
@@ -16,7 +14,6 @@
1614
import org.lwjgl.system.MemoryStack;
1715
import org.lwjgl.vulkan.VkCommandBuffer;
1816
import org.lwjgl.vulkan.VkRect2D;
19-
import org.lwjgl.vulkan.VkViewport;
2017

2118
import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
2219
import static org.lwjgl.vulkan.VK10.*;
@@ -27,21 +24,20 @@ public static DefaultMainPass create() {
2724
return new DefaultMainPass();
2825
}
2926

30-
private RenderTarget mainTarget;
3127
private final Framebuffer mainFramebuffer;
3228

3329
private RenderPass mainRenderPass;
3430
private RenderPass auxRenderPass;
3531

3632
private GpuTexture[] colorAttachmentTextures;
3733
private GpuTextureView[] colorAttachmentTextureViews;
34+
private GpuTexture depthAttachmentTexture;
3835

3936
DefaultMainPass() {
40-
this.mainTarget = Minecraft.getInstance().getMainRenderTarget();
4137
this.mainFramebuffer = Renderer.getInstance().getSwapChain();
4238

4339
createRenderPasses();
44-
createSwapChainTextures();
40+
createAttachmentTextures();
4541
}
4642

4743
private void createRenderPasses() {
@@ -70,8 +66,6 @@ public void begin(VkCommandBuffer commandBuffer, MemoryStack stack) {
7066

7167
framebuffer.beginRenderPass(commandBuffer, this.mainRenderPass, stack);
7268

73-
// VkViewport.Buffer pViewport = framebuffer.viewport(stack);
74-
// vkCmdSetViewport(commandBuffer, 0, pViewport);
7569
Renderer.setViewport(0, 0, framebuffer.getWidth(), framebuffer.getHeight(), stack);
7670

7771
VkRect2D.Buffer pScissor = framebuffer.scissor(stack);
@@ -101,7 +95,7 @@ public void cleanUp() {
10195

10296
@Override
10397
public void onResize() {
104-
this.createSwapChainTextures();
98+
this.createAttachmentTextures();
10599
}
106100

107101
public void rebindMainTarget() {
@@ -148,11 +142,20 @@ public GpuTextureView getColorAttachmentView() {
148142
return this.colorAttachmentTextureViews[Renderer.getCurrentImage()];
149143
}
150144

151-
private void createSwapChainTextures() {
145+
@Override
146+
public GpuTexture getDepthAttachment() {
147+
return this.depthAttachmentTexture;
148+
}
149+
150+
private void createAttachmentTextures() {
152151
VkGpuDevice device = (VkGpuDevice) RenderSystem.getDevice();
153152

154153
SwapChain swapChain = Renderer.getInstance().getSwapChain();
155154
var swapChainImages = swapChain.getImages();
155+
156+
if (swapChain.getWidth() == 0 && swapChain.getHeight() == 0)
157+
return;
158+
156159
int imageCount = swapChainImages.size();
157160
this.colorAttachmentTextures = new GpuTexture[imageCount];
158161
this.colorAttachmentTextureViews = new GpuTextureView[imageCount];
@@ -163,5 +166,7 @@ private void createSwapChainTextures() {
163166
this.colorAttachmentTextures[i] = attachmentTexture;
164167
this.colorAttachmentTextureViews[i] = attachmentTextureView;
165168
}
169+
170+
this.depthAttachmentTexture = device.gpuTextureFromVulkanImage(swapChain.getDepthAttachment());
166171
}
167172
}

src/main/java/net/vulkanmod/vulkan/pass/MainPass.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ default GpuTexture getColorAttachment() {
3030
default GpuTextureView getColorAttachmentView() {
3131
return null;
3232
}
33+
34+
default GpuTexture getDepthAttachment() {
35+
return null;
36+
}
37+
3338
}

0 commit comments

Comments
 (0)