66import com .jme3 .math .Quaternion ;
77import com .jme3 .math .Transform ;
88import com .jme3 .math .Vector3f ;
9+ import com .jme3 .opencl .CommandQueue ;
910import com .jme3 .renderer .vulkan .VulkanUtils ;
1011import com .jme3 .shaderc .ShaderType ;
1112import com .jme3 .shaderc .ShadercLoader ;
@@ -62,15 +63,31 @@ public class VulkanHelperTest extends SimpleApplication implements SwapchainUpda
6263
6364 private VulkanLogger logger ;
6465
66+ // mesh
6567 private final FloatBuffer vertexData = BufferUtils .createFloatBuffer (
66- -0.5f , -0.5f , 1f , 0f , 0f , 1f , 0f ,
67- 0.5f , -0.5f , 0f , 1f , 0f , 0f , 0f ,
68- 0.5f , 0.5f , 0f , 0f , 1f , 0f , 1f ,
69- -0.5f , 0.5f , 1f , 1f , 1f , 1f , 1f );
70- private final IntBuffer indexData = BufferUtils .createIntBuffer (0 , 1 , 2 , 2 , 3 , 0 );
68+ -0.5f , -0.5f , 0f , 1f , 0f , 0f , 1f , 0f ,
69+ 0.5f , -0.5f , 0f , 0f , 1f , 0f , 0f , 0f ,
70+ 0.5f , 0.5f , 0f , 0f , 0f , 1f , 0f , 1f ,
71+ -0.5f , 0.5f , 0f , 1f , 1f , 1f , 1f , 1f ,
72+
73+ -0.5f , -0.5f , -0.5f , 1f , 0f , 0f , 1f , 0f ,
74+ 0.5f , -0.5f , -0.5f , 0f , 1f , 0f , 0f , 0f ,
75+ 0.5f , 0.5f , -0.5f , 0f , 0f , 1f , 0f , 1f ,
76+ -0.5f , 0.5f , -0.5f , 1f , 1f , 1f , 1f , 1f
77+ );
78+ private final IntBuffer indexData = BufferUtils .createIntBuffer (
79+ 0 , 1 , 2 , 2 , 3 , 0 ,
80+ 4 , 5 , 6 , 6 , 7 , 4 );
81+
82+ // geometry
7183 private final Transform modelTransform = new Transform ();
84+
85+ // material
7286 private Texture texture ;
7387
88+ // framebuffer
89+ private ImageView depthView ;
90+
7491 public static void main (String [] args ) {
7592 VulkanHelperTest app = new VulkanHelperTest ();
7693 AppSettings settings = new AppSettings (true );
@@ -145,6 +162,23 @@ public void simpleInitApp() {
145162 descriptorPool = new DescriptorPool (device , 2 ,
146163 PoolSize .uniformBuffers (2 ), PoolSize .combinedImageSamplers (2 ));
147164
165+ CommandPool transferPool = new CommandPool (device , queues .getGraphicsQueue (), true , false );
166+
167+ // 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 ();
181+
148182 // pipeline
149183 pipelineLayout = new PipelineLayout (device , descriptorLayout );
150184 vertModule = new ShaderModule (device , assetManager .loadAsset (ShadercLoader .key (
@@ -153,37 +187,48 @@ public void simpleInitApp() {
153187 "Shaders/VulkanFragTest.glsl" , ShaderType .Fragment )), "main" );
154188 try (RenderPassBuilder pass = new RenderPassBuilder ()) {
155189 int color = pass .createAttachment (a -> a
156- .format (swapchain .getFormat ())
190+ .format (swapchain .getFormat (). getVkEnum () )
157191 .samples (VK_SAMPLE_COUNT_1_BIT )
158192 .loadOp (VK_ATTACHMENT_LOAD_OP_CLEAR )
159193 .storeOp (VK_ATTACHMENT_STORE_OP_STORE )
160194 .stencilLoadOp (VK_ATTACHMENT_LOAD_OP_DONT_CARE )
161195 .stencilStoreOp (VK_ATTACHMENT_STORE_OP_DONT_CARE )
162- .initialLayout (VK_IMAGE_LAYOUT_UNDEFINED )
163- .finalLayout (KHRSwapchain .VK_IMAGE_LAYOUT_PRESENT_SRC_KHR ));
196+ .initialLayout (Image .Layout .Undefined .getVkEnum ())
197+ .finalLayout (Image .Layout .PresentSrc .getVkEnum ()));
198+ int depth = pass .createAttachment (a -> a
199+ .format (depthFormat .getVkEnum ())
200+ .samples (VK_SAMPLE_COUNT_1_BIT )
201+ .loadOp (VK_ATTACHMENT_LOAD_OP_CLEAR )
202+ .storeOp (VK_ATTACHMENT_STORE_OP_DONT_CARE )
203+ .stencilLoadOp (VK_ATTACHMENT_LOAD_OP_DONT_CARE )
204+ .stencilStoreOp (VK_ATTACHMENT_STORE_OP_DONT_CARE )
205+ .initialLayout (Image .Layout .Undefined .getVkEnum ())
206+ .finalLayout (Image .Layout .DepthStencilAttachmentOptimal .getVkEnum ()));
164207 int subpass = pass .createSubpass (s -> {
165- VkAttachmentReference .Buffer ref = VkAttachmentReference .calloc (1 , pass .getStack ())
208+ VkAttachmentReference .Buffer colorRef = VkAttachmentReference .calloc (1 , pass .getStack ())
166209 .attachment (color )
167- .layout (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL );
210+ .layout (Image .Layout .ColorAttachmentOptimal .getVkEnum ());
211+ VkAttachmentReference depthRef = VkAttachmentReference .calloc (pass .getStack ())
212+ .attachment (depth )
213+ .layout (Image .Layout .DepthStencilAttachmentOptimal .getVkEnum ());
168214 s .pipelineBindPoint (VK_PIPELINE_BIND_POINT_GRAPHICS )
169215 .colorAttachmentCount (1 )
170- .pColorAttachments (ref );
216+ .pColorAttachments (colorRef )
217+ .pDepthStencilAttachment (depthRef );
171218 });
172219 pass .createDependency ()
173220 .srcSubpass (VK_SUBPASS_EXTERNAL )
174221 .dstSubpass (subpass )
175- .srcStageMask (VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT )
222+ .srcStageMask (VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT )
176223 .srcAccessMask (subpass )
177- .dstStageMask (VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT )
178- .dstAccessMask (VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT );
224+ .dstStageMask (VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT )
225+ .dstAccessMask (VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT );
179226 renderPass = pass .build (device );
180227 }
181- swapchain .createFrameBuffers (renderPass );
228+ swapchain .createFrameBuffers (renderPass , depthView );
182229 pipeline = new GraphicsPipeline (device , pipelineLayout , renderPass , new RenderState (), vertModule , fragModule , new MeshDescription ());
183230 graphicsPool = new CommandPool (device , queues .getGraphicsQueue (), false , true );
184231
185- CommandPool transferPool = new CommandPool (device , queues .getGraphicsQueue (), true , false );
186-
187232 // vertex buffers
188233 try (MemoryStack stack = MemoryStack .stackPush ()) {
189234 // cpu-accessible memory is not usually fast for the gpu to access, but
@@ -203,6 +248,7 @@ public void simpleInitApp() {
203248 indexBuffer .freeStagingBuffer ();
204249 }
205250
251+ // material color texture
206252 GpuImage image = loadImage ("Common/Textures/MissingTexture.png" , transferPool );
207253 texture = new Texture (device , image .createView (VK_IMAGE_VIEW_TYPE_2D , VK_IMAGE_ASPECT_COLOR_BIT , 0 , 1 , 0 , 1 ),
208254 VK_FILTER_LINEAR , VK_FILTER_LINEAR , VK_SAMPLER_ADDRESS_MODE_REPEAT , VK_SAMPLER_MIPMAP_MODE_LINEAR );
@@ -227,7 +273,7 @@ public boolean swapchainOutOfDate(Swapchain swapchain, int imageAcquireCode) {
227273 long window = ((LwjglVulkanContext )context ).getWindowHandle ();
228274 try (SimpleSwapchainSupport support = new SimpleSwapchainSupport (device .getPhysicalDevice (), surface , window )) {
229275 swapchain .reload (support );
230- swapchain .createFrameBuffers (renderPass );
276+ swapchain .createFrameBuffers (renderPass , depthView );
231277 }
232278 return true ;
233279 }
@@ -254,10 +300,11 @@ private GpuImage loadImage(String file, CommandPool transferPool) {
254300 new BufferUsageFlags ().transferSrc (), new MemoryFlags ().hostVisible ().hostCoherent (), false );
255301 staging .copy (stack , data .getBuffer ());
256302 GpuImage image = new GpuImage (device , data .getWidth (), data .getHeight (), data .getFormat (),
257- new ImageUsageFlags ().transferDst ().sampled (), new MemoryFlags ().deviceLocal ());
303+ Image .Tiling .Optimal , new ImageUsageFlags ().transferDst ().sampled (),
304+ new MemoryFlags ().deviceLocal ());
258305 CommandBuffer commands = transferPool .allocateOneTimeCommandBuffer ();
259306 commands .begin ();
260- image .transitionLayout (commands , VK_IMAGE_LAYOUT_UNDEFINED , VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL );
307+ image .transitionLayout (commands , Image . Layout . Undefined , Image . Layout . TransferDstOptimal );
261308 VkBufferImageCopy .Buffer region = VkBufferImageCopy .calloc (1 , stack )
262309 .bufferOffset (0 )
263310 .bufferRowLength (0 ) // padding bytes
@@ -270,7 +317,7 @@ private GpuImage loadImage(String file, CommandPool transferPool) {
270317 region .imageExtent ().set (data .getWidth (), data .getHeight (), 1 );
271318 vkCmdCopyBufferToImage (commands .getBuffer (), staging .getNativeObject (),
272319 image .getNativeObject (), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL , region );
273- image .transitionLayout (commands , VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL , VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL );
320+ image .transitionLayout (commands , Image . Layout . TransferDstOptimal , Image . Layout . ShaderReadOnlyOptimal );
274321 commands .end ();
275322 commands .submit (null , null , null );
276323 transferPool .getQueue ().waitIdle ();
0 commit comments