Skip to content

Commit beba5fe

Browse files
committed
added depth testing; added enums for image properties
1 parent 8e7ff36 commit beba5fe

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

jme3-examples/src/main/java/jme3test/vulkan/VulkanHelperTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.jme3.math.Quaternion;
77
import com.jme3.math.Transform;
88
import com.jme3.math.Vector3f;
9-
import com.jme3.opencl.CommandQueue;
109
import com.jme3.renderer.vulkan.VulkanUtils;
1110
import com.jme3.shaderc.ShaderType;
1211
import com.jme3.shaderc.ShadercLoader;
@@ -25,7 +24,6 @@
2524
import org.lwjgl.PointerBuffer;
2625
import org.lwjgl.glfw.GLFW;
2726
import org.lwjgl.system.MemoryStack;
28-
import org.lwjgl.system.MemoryUtil;
2927
import org.lwjgl.vulkan.*;
3028

3129
import java.nio.FloatBuffer;
@@ -35,9 +33,6 @@
3533
import java.util.logging.Level;
3634

3735
import static com.jme3.renderer.vulkan.VulkanUtils.*;
38-
import static org.lwjgl.system.MemoryUtil.NULL;
39-
import static org.lwjgl.vulkan.EXTDebugUtils.*;
40-
import static org.lwjgl.vulkan.EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
4136
import static org.lwjgl.vulkan.VK13.*;
4237

4338
public class VulkanHelperTest extends SimpleApplication implements SwapchainUpdater {
@@ -165,19 +160,7 @@ public void simpleInitApp() {
165160
CommandPool transferPool = new CommandPool(device, queues.getGraphicsQueue(), true, false);
166161

167162
// depth texture
168-
Image.Format depthFormat = device.getPhysicalDevice().findSupportedFormat(
169-
VK_IMAGE_TILING_OPTIMAL,
170-
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT,
171-
Image.Format.Depth32SFloat, Image.Format.Depth32SFloat_Stencil8UInt, Image.Format.Depth24UNorm_Stencil8UInt);
172-
GpuImage depthImage = new GpuImage(device, swapchain.getExtent().x, swapchain.getExtent().y, depthFormat,
173-
Image.Tiling.Optimal, new ImageUsageFlags().depthStencilAttachment(), new MemoryFlags().deviceLocal());
174-
depthView = depthImage.createView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1);
175-
CommandBuffer commands = transferPool.allocateOneTimeCommandBuffer();
176-
commands.begin();
177-
depthImage.transitionLayout(commands, Image.Layout.Undefined, Image.Layout.DepthStencilAttachmentOptimal);
178-
commands.end();
179-
commands.submit(null, null, null);
180-
commands.getPool().getQueue().waitIdle();
163+
depthView = createDepthAttachment(transferPool);
181164

182165
// pipeline
183166
pipelineLayout = new PipelineLayout(device, descriptorLayout);
@@ -196,7 +179,7 @@ public void simpleInitApp() {
196179
.initialLayout(Image.Layout.Undefined.getVkEnum())
197180
.finalLayout(Image.Layout.PresentSrc.getVkEnum()));
198181
int depth = pass.createAttachment(a -> a
199-
.format(depthFormat.getVkEnum())
182+
.format(depthView.getImage().getFormat().getVkEnum())
200183
.samples(VK_SAMPLE_COUNT_1_BIT)
201184
.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR)
202185
.storeOp(VK_ATTACHMENT_STORE_OP_DONT_CARE)
@@ -273,6 +256,7 @@ public boolean swapchainOutOfDate(Swapchain swapchain, int imageAcquireCode) {
273256
long window = ((LwjglVulkanContext)context).getWindowHandle();
274257
try (SimpleSwapchainSupport support = new SimpleSwapchainSupport(device.getPhysicalDevice(), surface, window)) {
275258
swapchain.reload(support);
259+
depthView = createDepthAttachment(new CommandPool(device, queues.getGraphicsQueue(), true, false));
276260
swapchain.createFrameBuffers(renderPass, depthView);
277261
}
278262
return true;
@@ -293,6 +277,23 @@ public void simpleUpdate(float tpf) {
293277
renderer.render(tpf);
294278
}
295279

280+
private ImageView createDepthAttachment(CommandPool pool) {
281+
Image.Format depthFormat = device.getPhysicalDevice().findSupportedFormat(
282+
VK_IMAGE_TILING_OPTIMAL,
283+
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT,
284+
Image.Format.Depth32SFloat, Image.Format.Depth32SFloat_Stencil8UInt, Image.Format.Depth24UNorm_Stencil8UInt);
285+
GpuImage image = new GpuImage(device, swapchain.getExtent().x, swapchain.getExtent().y, depthFormat,
286+
Image.Tiling.Optimal, new ImageUsageFlags().depthStencilAttachment(), new MemoryFlags().deviceLocal());
287+
ImageView view = image.createView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1);
288+
CommandBuffer commands = pool.allocateOneTimeCommandBuffer();
289+
commands.begin();
290+
image.transitionLayout(commands, Image.Layout.Undefined, Image.Layout.DepthStencilAttachmentOptimal);
291+
commands.end();
292+
commands.submit(null, null, null);
293+
commands.getPool().getQueue().waitIdle();
294+
return view;
295+
}
296+
296297
private GpuImage loadImage(String file, CommandPool transferPool) {
297298
try (MemoryStack stack = MemoryStack.stackPush()) {
298299
VulkanImageLoader.ImageData data = assetManager.loadAsset(VulkanImageLoader.key(file));

0 commit comments

Comments
 (0)