From ab1c577b6fbcdf67b79ffd2e7a1c4f9753715977 Mon Sep 17 00:00:00 2001 From: Max Thiele Date: Tue, 7 Jan 2025 20:52:32 +0100 Subject: [PATCH] generate ksl vk shader code --- .../kool/pipeline/backend/gl/GlslGenerator.kt | 12 +++- .../pipeline/backend/gl/RenderBackendGl.kt | 6 -- .../pipeline/backend/wgsl/WgslGenerator.kt | 12 +++- .../kotlin/de/fabmax/kool/util/LongHash.kt | 1 + .../backend/vk/{util => }/KoolVkExtensions.kt | 2 +- .../pipeline/backend/vk/OnScreenRenderPass.kt | 10 +-- .../pipeline/backend/vk/RenderBackendVk.kt | 28 ++------ .../pipeline/backend/vk/ShaderCodeImplVk.kt | 66 ------------------- .../kool/pipeline/backend/vk/ShaderCodeVk.kt | 59 +++++++++++++++++ .../pipeline/backend/vk/{util => }/Shaderc.kt | 11 ++-- .../backend/vk/{SwapChain.kt => Swapchain.kt} | 6 +- .../kool/pipeline/backend/vk/TextureLoader.kt | 2 - .../pipeline/backend/vk/VkOffscreenPass2d.kt | 1 - .../backend/vk/VkOffscreenPassCube.kt | 1 - .../kool/pipeline/backend/vk/VkScene.kt | 4 +- .../kool/pipeline/backend/vk/VkSystem.kt | 2 +- .../backend/vk/pipeline/GraphicsPipeline.kt | 3 +- .../backend/vk/pipeline/PipelineManager.kt | 6 +- .../backend/vk/pipeline/ShaderStage.kt | 8 +-- .../backend/webgpu/RenderBackendWebGpu.kt | 6 -- 20 files changed, 113 insertions(+), 133 deletions(-) rename kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/{util => }/KoolVkExtensions.kt (98%) delete mode 100644 kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeImplVk.kt create mode 100644 kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeVk.kt rename kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/{util => }/Shaderc.kt (80%) rename kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/{SwapChain.kt => Swapchain.kt} (97%) diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/GlslGenerator.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/GlslGenerator.kt index ba2ac84c5..40ea6dfa2 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/GlslGenerator.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/GlslGenerator.kt @@ -24,14 +24,22 @@ open class GlslGenerator(val hints: Hints) : KslGenerator() { return GlslGeneratorOutput.shaderOutput( generateVertexSrc(vertexStage, pipeline), generateFragmentSrc(fragmentStage, pipeline) - ) + ).also { + if (program.dumpCode) { + it.dump() + } + } } override fun generateComputeProgram(program: KslProgram, pipeline: ComputePipeline): GlslGeneratorOutput { val computeStage = checkNotNull(program.computeStage) { "KslProgram computeStage is missing" } - return GlslGeneratorOutput.computeOutput(generateComputeSrc(computeStage, pipeline)) + return GlslGeneratorOutput.computeOutput(generateComputeSrc(computeStage, pipeline)).also { + if (program.dumpCode) { + it.dump() + } + } } private fun generateVertexSrc(vertexStage: KslVertexStage, pipeline: DrawPipeline): String { diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/RenderBackendGl.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/RenderBackendGl.kt index 1badb20a2..ff35d7b30 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/RenderBackendGl.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/gl/RenderBackendGl.kt @@ -114,9 +114,6 @@ abstract class RenderBackendGl(val numSamples: Int, internal val gl: GlApi, inte override fun generateKslShader(shader: KslShader, pipeline: DrawPipeline): ShaderCodeGl { val src = GlslGenerator(glslGeneratorHints).generateProgram(shader.program, pipeline) - if (shader.program.dumpCode) { - src.dump() - } return ShaderCodeGl(src.vertexSrc, src.fragmentSrc) } @@ -125,9 +122,6 @@ abstract class RenderBackendGl(val numSamples: Int, internal val gl: GlApi, inte logW { "Compute shaders require OpenGL 4.3 or higher" } } val src = GlslGenerator(glslGeneratorHints).generateComputeProgram(shader.program, pipeline) - if (shader.program.dumpCode) { - src.dump() - } return ComputeShaderCodeGl(src.computeSrc) } diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/wgsl/WgslGenerator.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/wgsl/WgslGenerator.kt index a51913842..95de65ce4 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/wgsl/WgslGenerator.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/pipeline/backend/wgsl/WgslGenerator.kt @@ -30,7 +30,11 @@ class WgslGenerator : KslGenerator() { return WgslGeneratorOutput.shaderOutput( generateVertexSrc(vertexStage, pipeline), generateFragmentSrc(fragmentStage, pipeline) - ) + ).also { + if (program.dumpCode) { + it.dump() + } + } } override fun generateComputeProgram(program: KslProgram, pipeline: ComputePipeline): WgslGeneratorOutput { @@ -39,7 +43,11 @@ class WgslGenerator : KslGenerator() { } generatorState = GeneratorState(pipeline.bindGroupLayouts, null) - return WgslGeneratorOutput.computeOutput(generateComputeSrc(computeStage, pipeline)) + return WgslGeneratorOutput.computeOutput(generateComputeSrc(computeStage, pipeline)).also { + if (program.dumpCode) { + it.dump() + } + } } private fun generateVertexSrc(vertexStage: KslVertexStage, pipeline: DrawPipeline): String { diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/util/LongHash.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/util/LongHash.kt index f5fa3c799..43406b988 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/util/LongHash.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/util/LongHash.kt @@ -40,4 +40,5 @@ class LongHashBuilder { inline fun LongHash(block: LongHashBuilder.() -> Unit): LongHash = LongHashBuilder().apply(block).build() +// intentionally not a value class to avoid continuous boxing when used as a map key data class LongHash(val hash: Long) diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/KoolVkExtensions.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/KoolVkExtensions.kt similarity index 98% rename from kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/KoolVkExtensions.kt rename to kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/KoolVkExtensions.kt index 5cf717308..aaf73485d 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/KoolVkExtensions.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/KoolVkExtensions.kt @@ -1,4 +1,4 @@ -package de.fabmax.kool.pipeline.backend.vk.util +package de.fabmax.kool.pipeline.backend.vk import de.fabmax.kool.pipeline.ShaderStage import de.fabmax.kool.pipeline.TexFormat diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/OnScreenRenderPass.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/OnScreenRenderPass.kt index 19c49a07a..707d5ac7a 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/OnScreenRenderPass.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/OnScreenRenderPass.kt @@ -5,8 +5,8 @@ import de.fabmax.kool.util.memStack import org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR import org.lwjgl.vulkan.VK10.* -class OnScreenRenderPass(val swapChain: SwapChain) : - VkRenderPass(swapChain.backend, swapChain.extent.width(), swapChain.extent.height(), listOf(swapChain.imageFormat)) +class OnScreenRenderPass(val swapchain: Swapchain) : + VkRenderPass(swapchain.backend, swapchain.extent.width(), swapchain.extent.height(), listOf(swapchain.imageFormat)) { override val vkRenderPass: Long @@ -15,7 +15,7 @@ class OnScreenRenderPass(val swapChain: SwapChain) : memStack { val attachments = callocVkAttachmentDescriptionN(3) { this[0] - .format(swapChain.imageFormat) + .format(swapchain.imageFormat) .samples(physicalDevice.msaaSamples) .loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR) .storeOp(VK_ATTACHMENT_STORE_OP_STORE) @@ -33,7 +33,7 @@ class OnScreenRenderPass(val swapChain: SwapChain) : .initialLayout(VK_IMAGE_LAYOUT_UNDEFINED) .finalLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) this[2] - .format(swapChain.imageFormat) + .format(swapchain.imageFormat) .samples(VK_SAMPLE_COUNT_1_BIT) .loadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE) .storeOp(VK_ATTACHMENT_STORE_OP_STORE) @@ -83,7 +83,7 @@ class OnScreenRenderPass(val swapChain: SwapChain) : vkRenderPass = checkCreateLongPtr { vkCreateRenderPass(logicalDevice.vkDevice, renderPassInfo, null, it) } } - swapChain.addDependingResource(this) + swapchain.addDependingResource(this) logD { "Created render pass" } } diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/RenderBackendVk.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/RenderBackendVk.kt index 43d58def7..a90d55d0d 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/RenderBackendVk.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/RenderBackendVk.kt @@ -44,7 +44,7 @@ class RenderBackendVk(val ctx: Lwjgl3Context) : RenderBackendJvm { val physicalDevice: PhysicalDevice val logicalDevice: LogicalDevice val memManager: MemoryManager - var swapchain: SwapChain + var swapchain: Swapchain val commandPool: CommandPool val commandBuffer: VkCommandBuffer @@ -54,8 +54,6 @@ class RenderBackendVk(val ctx: Lwjgl3Context) : RenderBackendJvm { //val transferCommandPool: CommandPool //val renderLoop: RenderLoop -// private val shaderCodes = mutableMapOf() - // private val vkScene = KoolVkScene() //private val semaPool: SemaphorePool @@ -79,7 +77,7 @@ class RenderBackendVk(val ctx: Lwjgl3Context) : RenderBackendJvm { deviceName = physicalDevice.deviceName memManager = MemoryManager(this) - swapchain = SwapChain(this) + swapchain = Swapchain(this) commandPool = CommandPool(this, logicalDevice.graphicsQueue) val buffers = commandPool.createCommandBuffers(1) @@ -125,27 +123,13 @@ class RenderBackendVk(val ctx: Lwjgl3Context) : RenderBackendJvm { } override fun generateKslShader(shader: KslShader, pipeline: DrawPipeline): ShaderCode { - TODO() -// val src = KslGlslGeneratorVk().generateProgram(shader.program, pipeline) -// if (shader.program.dumpCode) { -// src.dump() -// } -// val codeKey = src.vertexSrc + src.fragmentSrc -// return shaderCodes.getOrPut(codeKey) { -// ShaderCodeImplVk.vkCodeFromSource(src.vertexSrc, src.fragmentSrc) -// } + val src = KslGlslGeneratorVk().generateProgram(shader.program, pipeline) + return ShaderCodeVk.drawShaderCode(src.vertexSrc, src.fragmentSrc) } override fun generateKslComputeShader(shader: KslComputeShader, pipeline: ComputePipeline): ComputeShaderCode { - TODO() -// val src = KslGlslGeneratorVk().generateComputeProgram(shader.program, pipeline) -// if (shader.program.dumpCode) { -// src.dump() -// } -// val codeKey = src.computeSrc -// return shaderCodes.getOrPut(codeKey) { -// ShaderCodeImplVk.vkComputeCodeFromSource(src.computeSrc) -// } + val src = KslGlslGeneratorVk().generateComputeProgram(shader.program, pipeline) + return ShaderCodeVk.computeShaderCode(src.computeSrc) } override fun renderFrame(ctx: KoolContext) { diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeImplVk.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeImplVk.kt deleted file mode 100644 index a42045c2a..000000000 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeImplVk.kt +++ /dev/null @@ -1,66 +0,0 @@ -package de.fabmax.kool.pipeline.backend.vk - -import de.fabmax.kool.pipeline.ComputeShaderCode -import de.fabmax.kool.pipeline.ShaderCode -import de.fabmax.kool.pipeline.backend.vk.pipeline.ShaderStage -import de.fabmax.kool.util.LongHash -import de.fabmax.kool.util.logD -import de.fabmax.kool.util.logE -import org.lwjgl.vulkan.VK10 - -class ShaderCodeImplVk(val vkCode: VkCode): ShaderCode, ComputeShaderCode { - - constructor(vararg stages: ShaderStage): this(VkCode(stages.asList())) - - override val hash: LongHash = LongHash { this += vkCode.hash } - - val vkStages: List - get() = vkCode.stages - - class VkCode(val stages: List) { - val hash = LongHash { - stages.forEach { - this += it.hash - } - } - } - - companion object { - fun vkCodeFromSource(vertShaderSrc: String, fragShaderSrc: String): ShaderCodeImplVk { - try { - val vertShaderCode = ShaderStage.fromSource("vertShader", vertShaderSrc, VK10.VK_SHADER_STAGE_VERTEX_BIT) - val fragShaderCode = ShaderStage.fromSource("fragShader", fragShaderSrc, VK10.VK_SHADER_STAGE_FRAGMENT_BIT) - logD("ShaderCode") { "Successfully compiled shader: vertShader: ${vertShaderCode.code.size} bytes, fragShader: ${fragShaderCode.code.size} bytes" } - return ShaderCodeImplVk(vertShaderCode, fragShaderCode) - } catch (e: Exception) { - logE { "Compilation failed: $e" } - dumpCode(listOf( - "Vertex shader" to vertShaderSrc, - "Fragment shader" to fragShaderSrc - )) - throw RuntimeException(e) - } - } - - fun vkComputeCodeFromSource(computeShaderSrc: String): ShaderCodeImplVk { - try { - val computeShaderCode = ShaderStage.fromSource("computeShader", computeShaderSrc, VK10.VK_SHADER_STAGE_COMPUTE_BIT) - logD("ShaderCode") { "Successfully compiled compute shader: ${computeShaderCode.code.size} bytes" } - return ShaderCodeImplVk(computeShaderCode) - } catch (e: Exception) { - logE { "Compilation failed: $e" } - dumpCode(listOf("Compute shader" to computeShaderSrc)) - throw RuntimeException(e) - } - } - - private fun dumpCode(sources: List>) { - sources.forEach { (name, source) -> - println("$name:\n\n") - source.lines().forEachIndexed { i, l -> - println(String.format("%3d: %s", i+1, l)) - } - } - } - } -} diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeVk.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeVk.kt new file mode 100644 index 000000000..60b799f68 --- /dev/null +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/ShaderCodeVk.kt @@ -0,0 +1,59 @@ +package de.fabmax.kool.pipeline.backend.vk + +import de.fabmax.kool.pipeline.ComputeShaderCode +import de.fabmax.kool.pipeline.ShaderCode +import de.fabmax.kool.pipeline.backend.vk.pipeline.ShaderStage +import de.fabmax.kool.util.LongHash +import de.fabmax.kool.util.logE +import org.lwjgl.vulkan.VK10.* + +class ShaderCodeVk(val stages: List): ShaderCode, ComputeShaderCode { + + override val hash: LongHash = LongHash { + stages.forEach { this += it.hash } + } + + companion object { + private val shaderCache = mutableMapOf() + + fun drawShaderCode(vertShaderSrc: String, fragShaderSrc: String): ShaderCodeVk { + try { + val vertexStage = shaderCache.getOrPut(ShaderKey(vertShaderSrc, VK_SHADER_STAGE_VERTEX_BIT)) { + ShaderStage.fromSource("vertShader", vertShaderSrc, VK_SHADER_STAGE_VERTEX_BIT) + } + val fragmentStage = shaderCache.getOrPut(ShaderKey(fragShaderSrc, VK_SHADER_STAGE_FRAGMENT_BIT)) { + ShaderStage.fromSource("fragShader", fragShaderSrc, VK_SHADER_STAGE_FRAGMENT_BIT) + } + return ShaderCodeVk(listOf(vertexStage, fragmentStage)) + } catch (e: Exception) { + logE { "Compilation failed: $e" } + dumpCode(listOf("Vertex shader" to vertShaderSrc, "Fragment shader" to fragShaderSrc)) + throw RuntimeException(e) + } + } + + fun computeShaderCode(computeShaderSrc: String): ShaderCodeVk { + try { + val computeStage = shaderCache.getOrPut(ShaderKey(computeShaderSrc, VK_SHADER_STAGE_COMPUTE_BIT)) { + ShaderStage.fromSource("computeShader", computeShaderSrc, VK_SHADER_STAGE_COMPUTE_BIT) + } + return ShaderCodeVk(listOf(computeStage)) + } catch (e: Exception) { + logE { "Compilation failed: $e" } + dumpCode(listOf("Compute shader" to computeShaderSrc)) + throw RuntimeException(e) + } + } + + private fun dumpCode(sources: List>) { + sources.forEach { (name, source) -> + println("$name:\n\n") + source.lines().forEachIndexed { i, l -> + println(String.format("%3d: %s", i+1, l)) + } + } + } + } + + private data class ShaderKey(val src: String, val stageBit: Int) +} diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/Shaderc.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Shaderc.kt similarity index 80% rename from kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/Shaderc.kt rename to kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Shaderc.kt index 0e9897045..99832f45d 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/util/Shaderc.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Shaderc.kt @@ -1,4 +1,4 @@ -package de.fabmax.kool.pipeline.backend.vk.util +package de.fabmax.kool.pipeline.backend.vk import de.fabmax.kool.util.logE import org.lwjgl.util.shaderc.Shaderc @@ -7,16 +7,19 @@ import java.nio.ByteBuffer object Shaderc { private val compiler = Shaderc.shaderc_compiler_initialize() - fun compileVertexShader(src: String, name: String = "shader.vert", entryPoint: String = "main") = + fun compileVertexShader(src: String, name: String, entryPoint: String) = compileShader(src, Shaderc.shaderc_vertex_shader, name, entryPoint) - fun compileFragmentShader(src: String, name: String = "shader.frag", entryPoint: String = "main") = + fun compileFragmentShader(src: String, name: String, entryPoint: String) = compileShader(src, Shaderc.shaderc_fragment_shader, name, entryPoint) + fun compileComputeShader(src: String, name: String, entryPoint: String) = + compileShader(src, Shaderc.shaderc_compute_shader, name, entryPoint) + private fun compileShader(src: String, shaderKind: Int, fName: String, entryPoint: String): CompileResult { val options = Shaderc.shaderc_compile_options_initialize() Shaderc.shaderc_compile_options_set_optimization_level(options, Shaderc.shaderc_optimization_level_performance) - Shaderc.shaderc_compile_options_set_target_env(options, Shaderc.shaderc_target_env_webgpu, 0) + Shaderc.shaderc_compile_options_set_target_env(options, Shaderc.shaderc_target_env_vulkan, 0) val result = Shaderc.shaderc_compile_into_spv(compiler, src, shaderKind, fName, entryPoint, options) val status = Shaderc.shaderc_result_get_compilation_status(result) diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/SwapChain.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Swapchain.kt similarity index 97% rename from kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/SwapChain.kt rename to kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Swapchain.kt index 573b18389..f00911a56 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/SwapChain.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/Swapchain.kt @@ -10,7 +10,7 @@ import org.lwjgl.vulkan.KHRSwapchain.* import org.lwjgl.vulkan.VK10.* import org.lwjgl.vulkan.VkExtent2D -class SwapChain(val backend: RenderBackendVk) : VkResource() { +class Swapchain(val backend: RenderBackendVk) : VkResource() { private val physicalDevice: PhysicalDevice get() = backend.physicalDevice private val logicalDevice: LogicalDevice get() = backend.logicalDevice @@ -72,7 +72,7 @@ class SwapChain(val backend: RenderBackendVk) : VkResource() { } imageFormat = surfaceFormat.format() - this@SwapChain.extent.set(extent) + this@Swapchain.extent.set(extent) val imgs = enumerateLongs { cnt, imgs -> vkGetSwapchainImagesKHR(logicalDevice.vkDevice, vkSwapchain.handle, cnt, imgs) @@ -88,7 +88,7 @@ class SwapChain(val backend: RenderBackendVk) : VkResource() { } } - renderPass = OnScreenRenderPass(this@SwapChain) + renderPass = OnScreenRenderPass(this@Swapchain) val (cImage, cImageView) = createColorResources() colorImage = cImage.also { addDependingResource(it) } diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/TextureLoader.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/TextureLoader.kt index 07425f104..bf6ade314 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/TextureLoader.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/TextureLoader.kt @@ -2,8 +2,6 @@ package de.fabmax.kool.pipeline.backend.vk import de.fabmax.kool.math.getNumMipLevels import de.fabmax.kool.pipeline.* -import de.fabmax.kool.pipeline.backend.vk.util.vkBytesPerPx -import de.fabmax.kool.pipeline.backend.vk.util.vkFormat import de.fabmax.kool.util.Uint8Buffer import de.fabmax.kool.util.logE import de.fabmax.kool.util.logW diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPass2d.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPass2d.kt index f7419e394..5114bab26 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPass2d.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPass2d.kt @@ -1,7 +1,6 @@ package de.fabmax.kool.pipeline.backend.vk import de.fabmax.kool.pipeline.* -import de.fabmax.kool.pipeline.backend.vk.util.vkFormat import de.fabmax.kool.platform.Lwjgl3Context import de.fabmax.kool.util.BaseReleasable import de.fabmax.kool.util.launchDelayed diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPassCube.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPassCube.kt index 054454225..642134645 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPassCube.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkOffscreenPassCube.kt @@ -1,7 +1,6 @@ package de.fabmax.kool.pipeline.backend.vk import de.fabmax.kool.pipeline.* -import de.fabmax.kool.pipeline.backend.vk.util.vkFormat import de.fabmax.kool.platform.Lwjgl3Context import de.fabmax.kool.util.BaseReleasable import de.fabmax.kool.util.launchDelayed diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkScene.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkScene.kt index 1cf75c680..ed007e677 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkScene.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkScene.kt @@ -6,9 +6,9 @@ interface VkScene { fun onLoad(sys: VkSystem) - fun onSwapChainCreated(swapChain: SwapChain) + fun onSwapChainCreated(swapChain: Swapchain) - fun onDrawFrame(swapChain: SwapChain, imageIndex: Int, fence: LongBuffer, waitSema: LongBuffer, signalSema: LongBuffer) + fun onDrawFrame(swapChain: Swapchain, imageIndex: Int, fence: LongBuffer, waitSema: LongBuffer, signalSema: LongBuffer) fun onDestroy(sys: VkSystem) diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkSystem.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkSystem.kt index 11f7efcef..eb6cee570 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkSystem.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/VkSystem.kt @@ -19,7 +19,7 @@ class VkSystem(val setup: VkSetup = VkSetup(), val scene: VkScene, val ctx: Lwjg val renderLoop: RenderLoop - var swapChain: SwapChain? = null + var swapChain: Swapchain? = null val backend: RenderBackendVk get() = TODO() diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/GraphicsPipeline.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/GraphicsPipeline.kt index c5f1701bd..b9c65680b 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/GraphicsPipeline.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/GraphicsPipeline.kt @@ -4,7 +4,6 @@ import de.fabmax.kool.pipeline.* import de.fabmax.kool.pipeline.backend.gl.getAttribLocations import de.fabmax.kool.pipeline.backend.gl.locationSize import de.fabmax.kool.pipeline.backend.vk.* -import de.fabmax.kool.pipeline.backend.vk.util.bitValue import de.fabmax.kool.scene.geometry.PrimitiveType import de.fabmax.kool.util.logD import de.fabmax.kool.util.memStack @@ -31,7 +30,7 @@ class GraphicsPipeline(val sys: VkSystem, val koolRenderPass: RenderPass, val vk descriptorSetLayout = createDescriptorSetLayout(pipeline.bindGroupLayouts[BindGroupScope.PIPELINE]) descriptorPool = createDescriptorPool(pipeline.bindGroupLayouts[BindGroupScope.PIPELINE]) - val shaderStages = (pipeline.shaderCode as ShaderCodeImplVk).vkStages + val shaderStages = (pipeline.shaderCode as ShaderCodeVk).stages val shaderStageModules = shaderStages.map { createShaderModule(it) } val shaderStageInfos = callocVkPipelineShaderStageCreateInfoN(shaderStages.size) { for (i in shaderStages.indices) { diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/PipelineManager.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/PipelineManager.kt index 588e56567..e0ba9aea1 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/PipelineManager.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/PipelineManager.kt @@ -2,7 +2,7 @@ package de.fabmax.kool.pipeline.backend.vk.pipeline import de.fabmax.kool.pipeline.DrawPipeline import de.fabmax.kool.pipeline.RenderPass -import de.fabmax.kool.pipeline.backend.vk.SwapChain +import de.fabmax.kool.pipeline.backend.vk.Swapchain import de.fabmax.kool.pipeline.backend.vk.VkRenderPass import de.fabmax.kool.pipeline.backend.vk.VkSystem import de.fabmax.kool.util.LongHash @@ -12,7 +12,7 @@ class PipelineManager(val sys: VkSystem) { private val onScreenPipelineConfigs = mutableSetOf() - private var swapChain: SwapChain? = null + private var swapChain: Swapchain? = null private val createdPipelines = mutableMapOf() fun onSwapchainDestroyed() { @@ -20,7 +20,7 @@ class PipelineManager(val sys: VkSystem) { createdPipelines.clear() } - fun onSwapchainCreated(swapChain: SwapChain) { + fun onSwapchainCreated(swapChain: Swapchain) { this.swapChain = swapChain onScreenPipelineConfigs.forEach { createOnScreenPipeline(it.pipeline, it.koolRenderPass, swapChain.renderPass) } } diff --git a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/ShaderStage.kt b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/ShaderStage.kt index b2ba1e1ac..309e1fb13 100644 --- a/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/ShaderStage.kt +++ b/kool-core/src/desktopMain/kotlin/de/fabmax/kool/pipeline/backend/vk/pipeline/ShaderStage.kt @@ -1,9 +1,8 @@ package de.fabmax.kool.pipeline.backend.vk.pipeline -import de.fabmax.kool.pipeline.backend.vk.util.Shaderc +import de.fabmax.kool.pipeline.backend.vk.Shaderc import de.fabmax.kool.util.LongHash -import org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_FRAGMENT_BIT -import org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_VERTEX_BIT +import org.lwjgl.vulkan.VK10.* import java.io.InputStream class ShaderStage(val name: String, val code: ByteArray, val stage: Int, val entryPoint: String = "main") { @@ -25,7 +24,8 @@ class ShaderStage(val name: String, val code: ByteArray, val stage: Int, val ent val compileResult = when (stage) { VK_SHADER_STAGE_VERTEX_BIT -> Shaderc.compileVertexShader(srcCode, name, entryPoint) VK_SHADER_STAGE_FRAGMENT_BIT -> Shaderc.compileFragmentShader(srcCode, name, entryPoint) - else -> throw IllegalArgumentException("Invalid shader stage: $stage") + VK_SHADER_STAGE_COMPUTE_BIT -> Shaderc.compileComputeShader(srcCode, name, entryPoint) + else -> error("Invalid shader stage: $stage") } val data = checkNotNull(compileResult.spirvData) { "Shader compilation failed" } val codeArray = ByteArray(data.remaining()) diff --git a/kool-core/src/jsMain/kotlin/de/fabmax/kool/pipeline/backend/webgpu/RenderBackendWebGpu.kt b/kool-core/src/jsMain/kotlin/de/fabmax/kool/pipeline/backend/webgpu/RenderBackendWebGpu.kt index 809fe70b5..1d029dbde 100644 --- a/kool-core/src/jsMain/kotlin/de/fabmax/kool/pipeline/backend/webgpu/RenderBackendWebGpu.kt +++ b/kool-core/src/jsMain/kotlin/de/fabmax/kool/pipeline/backend/webgpu/RenderBackendWebGpu.kt @@ -176,9 +176,6 @@ class RenderBackendWebGpu(val ctx: KoolContext, val canvas: HTMLCanvasElement) : override fun generateKslShader(shader: KslShader, pipeline: DrawPipeline): ShaderCode { val output = WgslGenerator().generateProgram(shader.program, pipeline) - if (shader.program.dumpCode) { - output.dump() - } return WebGpuShaderCode( vertexSrc = output.vertexSrc, vertexEntryPoint = output.vertexEntryPoint, @@ -189,9 +186,6 @@ class RenderBackendWebGpu(val ctx: KoolContext, val canvas: HTMLCanvasElement) : override fun generateKslComputeShader(shader: KslComputeShader, pipeline: ComputePipeline): ComputeShaderCode { val output = WgslGenerator().generateComputeProgram(shader.program, pipeline) - if (shader.program.dumpCode) { - output.dump() - } return WebGpuComputeShaderCode( computeSrc = output.computeSrc, computeEntryPoint = output.computeEntryPoint