diff --git a/src/main/java/net/halbear/Terrain4J/EngineCore/Vulkan/Rendering/Pipeline/Images/Texture.java b/src/main/java/net/halbear/Terrain4J/EngineCore/Vulkan/Rendering/Pipeline/Images/Texture.java index 6f894bc..f70b783 100644 --- a/src/main/java/net/halbear/Terrain4J/EngineCore/Vulkan/Rendering/Pipeline/Images/Texture.java +++ b/src/main/java/net/halbear/Terrain4J/EngineCore/Vulkan/Rendering/Pipeline/Images/Texture.java @@ -1,14 +1,24 @@ package net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.Pipeline.Images; import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext; +import net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DeviceLayers.CommandBuffer; import net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DisplayToScreen.ImageView; import net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanBuffer; +import net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanUtils; +import org.lwjgl.system.MemoryStack; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static org.lwjgl.vulkan.VK10.*; +import static org.lwjgl.vulkan.VK13.VK_ACCESS_2_NONE; public class Texture { private final String ID; private final int Height; private final int Width; + private final Image image; private final ImageView imageView; private boolean RecordedTransition; private VulkanBuffer stgBuffer; @@ -19,6 +29,53 @@ public class Texture { Width = imageSrc.Width(); Height = imageSrc.Height(); - //will continue later + CreateStgBuffer(VkCtx, imageSrc.data()); + var ImageData = new Image.ImageData().Width(Width).Height(Height) + .usage(VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT) + .format(ImageFormat); + image = new Image(VkCtx, ImageData); + var ImageViewData = new ImageView.ImageViewData().format(image.GetFormat()) + .aspectMask(VK_IMAGE_ASPECT_COLOR_BIT); + imageView = new ImageView(VkCtx.GetDevice(), image.getVulkanImage(), ImageViewData, false); } + + public void CreateStgBuffer(VulkanContext VkCtx, ByteBuffer data){ + int Size = data.remaining(); + stgBuffer = new VulkanBuffer(VkCtx, Size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + long MappedMemory = stgBuffer.MapMemory(VkCtx); + ByteBuffer buffer = MemoryUtil.memByteBuffer(MappedMemory, (int) stgBuffer.GetRequestedSize()); + buffer.put(data); + data.flip(); + stgBuffer.UnMapMemory(VkCtx); + } + + public void RecordTextureTransition(CommandBuffer commandBuffer){ + if(stgBuffer != null && !RecordedTransition){ + RecordedTransition = true; + try(var MemStack = MemoryStack.stackPush()){ + VulkanUtils.ImageBarrier(MemStack, commandBuffer.GetVulkanCommandBuffer(), image.getVulkanImage(), + VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_2_NONE, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_ASPECT_COLOR_BIT); + //finish this later + } + } + } + + public void CleanUp(VulkanContext VkCtx){ + CleanUpStgBuffer(VkCtx); + imageView.cleanup(VkCtx.GetDevice()); + image.CleanUp(VkCtx); + } + + public void CleanUpStgBuffer(VulkanContext VkCtx){ + if(stgBuffer != null){ + stgBuffer.cleanup(VkCtx); + stgBuffer = null; + } + } + + public int GetHeight(){return Height;} + public int GetWidth(){return Width;} + public String GetID(){return ID;} + public ImageView GetImageView(){return imageView;} }