From b77a3040c999d14b97fd978936bdeb7c9f2ff454 Mon Sep 17 00:00:00 2001 From: knokko Date: Thu, 10 Oct 2024 22:36:16 +0200 Subject: [PATCH] wip: ktx --- build.gradle | 2 + .../knokko/boiler/samples/HelloKtx.java | 142 ++++++++++++++++++ .../knokko/boiler/samples/graphics/ktx.frag | 7 + .../boiler/samples/graphics/ktx.frag.spv | Bin 0 -> 440 bytes .../knokko/boiler/samples/graphics/ktx.vert | 9 ++ .../boiler/samples/graphics/ktx.vert.spv | Bin 0 -> 1196 bytes test1-bc1.ktx2 | Bin 0 -> 344 bytes test1-bc3.ktx2 | Bin 0 -> 496 bytes test1-bc7.ktx2 | Bin 0 -> 480 bytes test1.png | Bin 0 -> 184 bytes test2-1.png | Bin 0 -> 305 bytes test2-7.png | Bin 0 -> 438 bytes test2-bc1.ktx2 | Bin 0 -> 344 bytes test2-bc3.ktx2 | Bin 0 -> 496 bytes test2-bc7.ktx2 | Bin 0 -> 480 bytes test2.png | Bin 0 -> 506 bytes 16 files changed, 160 insertions(+) create mode 100644 samples/src/main/java/com/github/knokko/boiler/samples/HelloKtx.java create mode 100644 samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag create mode 100644 samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag.spv create mode 100644 samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert create mode 100644 samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert.spv create mode 100644 test1-bc1.ktx2 create mode 100644 test1-bc3.ktx2 create mode 100644 test1-bc7.ktx2 create mode 100644 test1.png create mode 100644 test2-1.png create mode 100644 test2-7.png create mode 100644 test2-bc1.ktx2 create mode 100644 test2-bc3.ktx2 create mode 100644 test2-bc7.ktx2 create mode 100644 test2.png diff --git a/build.gradle b/build.gradle index 6842319..f73e325 100644 --- a/build.gradle +++ b/build.gradle @@ -81,6 +81,7 @@ project(":samples") { implementation "org.lwjgl:lwjgl" implementation "org.lwjgl:lwjgl-vma" implementation "org.lwjgl:lwjgl-vulkan" + implementation "org.lwjgl:lwjgl-ktx" implementation "org.lwjgl:lwjgl-glfw" implementation "org.lwjgl:lwjgl-openxr" implementation "org.joml:joml:${jomlVersion}" @@ -89,6 +90,7 @@ project(":samples") { runtimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives" runtimeOnly "org.lwjgl:lwjgl-vma::$lwjglNatives" runtimeOnly "org.lwjgl:lwjgl-openxr::$lwjglNatives" + runtimeOnly "org.lwjgl:lwjgl-ktx::$lwjglNatives" if (lwjglNatives == "natives-macos") runtimeOnly "org.lwjgl:lwjgl-vulkan::$lwjglNatives" } shadowJar { diff --git a/samples/src/main/java/com/github/knokko/boiler/samples/HelloKtx.java b/samples/src/main/java/com/github/knokko/boiler/samples/HelloKtx.java new file mode 100644 index 0000000..7d281e9 --- /dev/null +++ b/samples/src/main/java/com/github/knokko/boiler/samples/HelloKtx.java @@ -0,0 +1,142 @@ +package com.github.knokko.boiler.samples; + +import com.github.knokko.boiler.BoilerInstance; +import com.github.knokko.boiler.builders.BoilerBuilder; +import com.github.knokko.boiler.builders.WindowBuilder; +import com.github.knokko.boiler.commands.CommandRecorder; +import com.github.knokko.boiler.pipelines.GraphicsPipelineBuilder; +import com.github.knokko.boiler.synchronization.ResourceUsage; +import com.github.knokko.boiler.utilities.ReflectionHelper; +import com.github.knokko.boiler.window.AcquiredImage; +import com.github.knokko.boiler.window.SimpleWindowRenderLoop; +import com.github.knokko.boiler.window.VkbWindow; +import com.github.knokko.boiler.window.WindowEventLoop; +import org.lwjgl.system.MemoryStack; +import org.lwjgl.util.ktx.*; +import org.lwjgl.vulkan.VK10; +import org.lwjgl.vulkan.VK13; +import org.lwjgl.vulkan.VkPhysicalDeviceFeatures; +import org.lwjgl.vulkan.VkRenderingAttachmentInfo; + +import static com.github.knokko.boiler.exceptions.VulkanFailureException.assertVkSuccess; +import static org.lwjgl.system.MemoryStack.stackPush; +import static org.lwjgl.util.ktx.KTXVulkan.ktxVulkanDeviceInfo_Construct; +import static org.lwjgl.vulkan.KHRSurface.VK_PRESENT_MODE_FIFO_KHR; +import static org.lwjgl.vulkan.VK10.*; +import static org.lwjgl.vulkan.VK12.VK_API_VERSION_1_2; + +public class HelloKtx extends SimpleWindowRenderLoop { + + private static void assertKtxSuccess(int result) { + if (result != KTX.KTX_SUCCESS) throw new RuntimeException("returned " + result + KTX.ktxErrorString(result)); + } + + @SuppressWarnings("resource") + public static void main(String[] args) { + var boiler = new BoilerBuilder( + VK_API_VERSION_1_2, "HelloKtx", 1 + ) + .validation() + .enableDynamicRendering() + .addWindow(new WindowBuilder(800, 500, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) + .requiredFeatures10(VkPhysicalDeviceFeatures::textureCompressionBC) + .featurePicker10(((stack, supportedFeatures, toEnable) -> toEnable.textureCompressionBC(true))) + .build(); + + try (var stack = stackPush()) { + var pTexture = stack.callocPointer(1); + assertKtxSuccess(KTX.ktxTexture_CreateFromNamedFile(stack.ASCII("test2-bc1.ktx2"), 1, pTexture)); + var ktxTexture = ktxTexture2.create(pTexture.get(0)); + + // TODO Using AMD compressonator CLI: .\compressonatorcli.exe -fd bc7 test1.png test1-bc7.ktx2 + int format = KTXVulkan.ktxTexture2_GetVkFormat(ktxTexture); + System.out.println("format is " + format + ": " + ReflectionHelper.getIntConstantName(VK13.class, format, "VK_FORMAT_", "", "unknown")); + + var uploadCommandPool = boiler.commands.createPool(0, boiler.queueFamilies().graphics().index(), "KtxPool"); + + var deviceInfo = ktxVulkanDeviceInfo.calloc(stack); + assertKtxSuccess(ktxVulkanDeviceInfo_Construct( + deviceInfo, boiler.vkPhysicalDevice(), boiler.vkDevice(), + boiler.queueFamilies().graphics().first().vkQueue(), + uploadCommandPool, null + )); + + var ktxVkTexture = ktxVulkanTexture.calloc(stack); + + assertKtxSuccess(KTXVulkan.ktxTexture2_VkUpload(ktxTexture, deviceInfo, ktxVkTexture)); + + System.out.println("size is " + ktxVkTexture.width() + " x " + ktxVkTexture.height()); + KTXVulkan.ktxVulkanTexture_Destruct(ktxVkTexture, boiler.vkDevice(), null); + KTXVulkan.ktxVulkanDeviceInfo_Destruct(deviceInfo); + + vkDestroyCommandPool(boiler.vkDevice(), uploadCommandPool, null); + } + +// var eventLoop = new WindowEventLoop(); +// eventLoop.addWindow(new HelloKtx(boiler.window())); +// eventLoop.runMain(); + + boiler.destroyInitialObjects(); + } + + public HelloKtx(VkbWindow window) { + super( + window, 1, true, VK_PRESENT_MODE_FIFO_KHR, + ResourceUsage.COLOR_ATTACHMENT_WRITE, ResourceUsage.COLOR_ATTACHMENT_WRITE + ); + } + + private long pipelineLayout, graphicsPipeline; + + @Override + protected void setup(BoilerInstance boiler, MemoryStack stack) { + super.setup(boiler, stack); + this.pipelineLayout = boiler.pipelines.createLayout(null, "CompressedLayout"); + + var builder = new GraphicsPipelineBuilder(boiler, stack); + builder.simpleShaderStages( + "CompressedPipeline", + "com/github/knokko/boiler/samples/graphics/ktx.vert.spv", + "com/github/knokko/boiler/samples/graphics/ktx.frag.spv" + ); + builder.noVertexInput(); + builder.simpleInputAssembly(); + builder.dynamicViewports(1); + builder.simpleRasterization(VK_CULL_MODE_NONE); + builder.noMultisampling(); + builder.noDepthStencil(); + builder.noColorBlending(1); + builder.dynamicStates(VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_VIEWPORT); + builder.ciPipeline.layout(pipelineLayout); + builder.dynamicRendering(0, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, window.surfaceFormat); + this.graphicsPipeline = builder.build("CompressedPipeline"); + } + + @Override + protected void recordFrame( + MemoryStack stack, int frameIndex, CommandRecorder recorder, + AcquiredImage acquiredImage, BoilerInstance instance + ) { + var colorAttachments = VkRenderingAttachmentInfo.calloc(1, stack); + recorder.simpleColorRenderingAttachment( + colorAttachments.get(0), acquiredImage.image().vkImageView(), + VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE, + 1f, 0f, 0f, 1f + ); + recorder.beginSimpleDynamicRendering( + acquiredImage.width(), acquiredImage.height(), + colorAttachments, null, null + ); + vkCmdBindPipeline(recorder.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); + recorder.dynamicViewportAndScissor(acquiredImage.width(), acquiredImage.height()); + vkCmdDraw(recorder.commandBuffer, 6, 1, 0, 0); + recorder.endDynamicRendering(); + } + + @Override + protected void cleanUp(BoilerInstance boiler) { + super.cleanUp(boiler); + vkDestroyPipeline(boiler.vkDevice(), graphicsPipeline, null); + vkDestroyPipelineLayout(boiler.vkDevice(), pipelineLayout, null); + } +} diff --git a/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag new file mode 100644 index 0000000..4cd6d9b --- /dev/null +++ b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag @@ -0,0 +1,7 @@ +#version 450 + +layout(location = 0) out vec4 outColor; + +void main() { + outColor = vec4(0.0, 1.0, 0.5, 1.0); +} diff --git a/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag.spv b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..62d44aa5f1cc92089c9ccf2fc35fad5f27ad92f3 GIT binary patch literal 440 zcmYjO!D_-l6r38PR;yJC?X6gPY>^&Rgn}MK_7d>W4_Hc)!$O*f#zJrXHowY?(3!15 z9=yDr$?Uud32M(x;0hgd(ZhcYP$L6!4L?ov5sh|bG8#|br05~=LTPTPsdK8oK3%RC z*X%wu;%LrOKu6%f3w5+%B5T%b6Rm8#+1OqAlchFud1@24P2q zDylm}gnnM5xJ literal 0 HcmV?d00001 diff --git a/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert new file mode 100644 index 0000000..412f1aa --- /dev/null +++ b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert @@ -0,0 +1,9 @@ +#version 450 + +vec2 positions[] = { + vec2(-0.8, -0.8), vec2(-0.3, -0.8), vec2(-0.3, -0.3), vec2(-0.3, -0.3), vec2(-0.8, -0.3), vec2(-0.8, -0.8) +}; + +void main() { + gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); +} diff --git a/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert.spv b/samples/src/main/resources/com/github/knokko/boiler/samples/graphics/ktx.vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..b2329a5fea046ad43dfee78978b169866a21185c GIT binary patch literal 1196 zcmYk3TT2{45QW=a$NM$j6QiT%YD9c6K?pHO5+;EG6~Wg*b_Z#gjLXg#qEGo7`ePLG z9RH6mg6Er=-fS;ttLoILsp{%_ZLQy!9y4r4&0ABRNmCPJ#Pt<62#>hkQDW0LYT9<( z$nCEf-9VSlrj6vh*)8y-=nyXmJxeqBp5~sDJx!v6xP2VA^Z2(yu_cdB zt|}4RU+B3{!P_)HvX`2Mnli^dl>3s{)>oV5Cut+@anB3-M{d5L-(b2tm5FE zW?cUD)zyvDOels!g%!#T--O=)pIr0qEek^Rz5;u`_qa8Tss*JI>0I zhg*`z8@U>CfV3v&^pf6y0pgh$IDUts_M literal 0 HcmV?d00001 diff --git a/test1-bc1.ktx2 b/test1-bc1.ktx2 new file mode 100644 index 0000000000000000000000000000000000000000..877cc55f1e085d259f2bc4a56045c76ba544a8d0 GIT binary patch literal 344 zcmZ4O9TK5nWU!l;ONy(Rfq{V$hy|b+NPz)J41_@-1Bi8icnT0Z05Lka0TpOKqd|H= zfQdnap@ET+fti_sfde80B<|3F|3OddpwLir-Bj)5S)D^LN5 J4h;wi1prYiPyGM@ literal 0 HcmV?d00001 diff --git a/test1-bc3.ktx2 b/test1-bc3.ktx2 new file mode 100644 index 0000000000000000000000000000000000000000..f027bc6c0c21a8c1bd258fbd24d3028bf9607e13 GIT binary patch literal 496 zcmZ4O9TK5nWU!l;ONy(Lfq{V$hy|b+NPz)J41_@-1Bh*acnJ_Y05LlF026>|fYKm6 zAi%_6!O+CW$iU1Du?He*&kvy)82$r+1A{$`3+7t`c|d2D7iE^D7BQsfgt#d<=jRp_ zr4|?G=Ovcp7b%n(=@}^KE97J*WtUVal$q!m=o#oHrIsX`)EU8zf-wGjPErA){Ven6 zhla8`GC=wBS?YoO5D^$3A|D!p%6CBJi;9NAL9zp@h|3F|3q=C*VFUl-QEn-N|32{?!&d)62At|B?Sc(DnTP;r8=0fK7n&fn0u|4FJQXlk)%o literal 0 HcmV?d00001 diff --git a/test1.png b/test1.png new file mode 100644 index 0000000000000000000000000000000000000000..3a3deb1d5f2c51cf1f543d6901849b7bc38f5d5c GIT binary patch literal 184 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFLlCcWHFcd{kq(hhPu7ZoBU*S{~ojVkYgTLUPNWh_M z6-Dqj99{eXH-`?o)xjpUNm?picn}^hx2HEx$h;`{Lp@hB2?R+XIvIHSQd%BX)x{(b z$h;1y=KvX%1ga?`2Tf}CWK_)xGXoR7i9i|DGHCj56Z@Og8KMihNu3PZl+CM|U*ENxu9&{~~ zMbP$K%0!itwTYJ(+>Sc{hSLRO-!|#ANWci)6gT00000NkvXXu0mjf D4&r&< literal 0 HcmV?d00001 diff --git a/test2-7.png b/test2-7.png new file mode 100644 index 0000000000000000000000000000000000000000..947b914527ba4bd2d5a93d9c64da2c8dc06cb675 GIT binary patch literal 438 zcmV;n0ZIOeP)quLd!*bAoC)VvG#Yjd2R--a4>_T_(QlX%bE-z&d$vAY7GVn>`j?V;}?}GB>M~@?!vMF z5k7#;xf(3!J2N-|2Eg8ubbtk2=;nHoRHPCfVtbPCs%kqT;eKHN04u{fj*GyH@B}U?NL1o3zw-TXI_2>tKWvy>jpqPNK3ad gCdWwq-)aN=03L^us0sFQ#Q*>R07*qoM6N<$f;ir`EC2ui literal 0 HcmV?d00001 diff --git a/test2-bc1.ktx2 b/test2-bc1.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