| 1 | /*
|
| 2 | * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
|
| 3 | *
|
| 4 | * Copyright (c) 2015 - 2026 CCBlueX
|
| 5 | *
|
| 6 | * LiquidBounce is free software: you can redistribute it and/or modify
|
| 7 | * it under the terms of the GNU General Public License as published by
|
| 8 | * the Free Software Foundation, either version 3 of the License, or
|
| 9 | * (at your option) any later version.
|
| 10 | *
|
| 11 | * LiquidBounce is distributed in the hope that it will be useful,
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | * GNU General Public License for more details.
|
| 15 | *
|
| 16 | * You should have received a copy of the GNU General Public License
|
| 17 | * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
|
| 18 | */
|
| 19 |
|
| 20 | @file:Suppress("NOTHING_TO_INLINE", "TooManyFunctions")
|
| 21 |
|
| 22 | package net.ccbluex.liquidbounce.utils.render
|
| 23 |
|
| 24 | import com.google.common.base.Suppliers
|
| 25 | import com.google.common.util.concurrent.Runnables
|
| 26 | import com.mojang.blaze3d.GpuFormat
|
| 27 | import com.mojang.blaze3d.buffers.GpuBuffer
|
| 28 | import com.mojang.blaze3d.buffers.GpuBufferSlice
|
| 29 | import com.mojang.blaze3d.buffers.Std140Builder
|
| 30 | import com.mojang.blaze3d.buffers.Std140SizeCalculator
|
| 31 | import com.mojang.blaze3d.pipeline.RenderPipeline
|
| 32 | import com.mojang.blaze3d.pipeline.RenderTarget
|
| 33 | import com.mojang.blaze3d.platform.NativeImage
|
| 34 | import com.mojang.blaze3d.systems.GpuDevice
|
| 35 | import com.mojang.blaze3d.systems.RenderSystem
|
| 36 | import com.mojang.blaze3d.textures.GpuSampler
|
| 37 | import com.mojang.blaze3d.textures.GpuTexture
|
| 38 | import com.mojang.blaze3d.textures.GpuTextureView
|
| 39 | import com.mojang.blaze3d.vertex.BufferBuilder
|
| 40 | import com.mojang.blaze3d.vertex.ByteBufferBuilder
|
| 41 | import com.mojang.blaze3d.vertex.PoseStack
|
| 42 | import kotlinx.coroutines.Dispatchers
|
| 43 | import kotlinx.coroutines.asExecutor
|
| 44 | import net.ccbluex.liquidbounce.render.engine.type.Color4b
|
| 45 | import net.ccbluex.liquidbounce.utils.client.gpuDevice
|
| 46 | import net.ccbluex.liquidbounce.utils.client.mc
|
| 47 | import net.ccbluex.liquidbounce.utils.io.ensurePngOrConvertJpeg
|
| 48 | import net.minecraft.client.gui.render.TextureSetup
|
| 49 | import net.minecraft.client.renderer.texture.AbstractTexture
|
| 50 | import net.minecraft.client.renderer.texture.DynamicTexture
|
| 51 | import net.minecraft.resources.Identifier
|
| 52 | import net.minecraft.util.ARGB
|
| 53 | import okio.BufferedSource
|
| 54 | import okio.buffer
|
| 55 | import okio.source
|
| 56 | import org.lwjgl.system.MemoryUtil
|
| 57 | import java.awt.image.BufferedImage
|
| 58 | import java.awt.image.DataBufferInt
|
| 59 | import java.awt.image.SinglePixelPackedSampleModel
|
| 60 | import java.io.File
|
| 61 | import java.io.InputStream
|
| 62 | import java.nio.ByteBuffer
|
| 63 | import java.nio.IntBuffer
|
| 64 | import java.util.concurrent.CompletableFuture
|
| 65 | import java.util.function.Supplier
|
| 66 |
|
| 67 | fun PoseStack.reset() {
|
| 68 | while (!isEmpty) popPose()
|
| 69 | setIdentity()
|
| 70 | }
|
| 71 |
|
| 72 | inline fun ByteBufferBuilder.begin(pipeline: RenderPipeline): BufferBuilder =
|
| 73 | BufferBuilder(
|
| 74 | this,
|
| 75 | pipeline.primitiveTopology,
|
| 76 | requireNotNull(pipeline.getVertexFormatBinding(0)) {
|
| 77 | "Pipeline ${pipeline.location} has no vertex format binding"
|
| 78 | },
|
| 79 | )
|
| 80 |
|
| 81 | inline fun <T> withOutputTextureOverride(
|
| 82 | color: GpuTextureView? = null,
|
| 83 | depth: GpuTextureView? = null,
|
| 84 | block: () -> T,
|
| 85 | ): T {
|
| 86 | val oldColor = RenderSystem.outputColorTextureOverride
|
| 87 | val oldDepth = RenderSystem.outputDepthTextureOverride
|
| 88 |
|
| 89 | try {
|
| 90 | RenderSystem.outputColorTextureOverride = color
|
| 91 | RenderSystem.outputDepthTextureOverride = depth
|
| 92 | return block()
|
| 93 | } finally {
|
| 94 | RenderSystem.outputColorTextureOverride = oldColor
|
| 95 | RenderSystem.outputDepthTextureOverride = oldDepth
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | inline fun GpuTexture.clearColor(color: Color4b = Color4b.TRANSPARENT) =
|
| 100 | gpuDevice.createCommandEncoder().clearColorTexture(this, color.toVector4f())
|
| 101 |
|
| 102 | inline fun GpuTexture.clearDepth(depth: Double = 0.0) =
|
| 103 | gpuDevice.createCommandEncoder().clearDepthTexture(this, depth)
|
| 104 |
|
| 105 | fun RenderTarget.clearColorAndDepth(color: Color4b = Color4b.TRANSPARENT, depth: Double = 0.0) {
|
| 106 | val colorAttachment = colorTexture
|
| 107 | val depthAttachment = depthTexture.takeIf { useDepth }
|
| 108 |
|
| 109 | when {
|
| 110 | colorAttachment != null && depthAttachment != null ->
|
| 111 | gpuDevice.createCommandEncoder().clearColorAndDepthTextures(
|
| 112 | colorAttachment, color.toVector4f(), depthAttachment, depth
|
| 113 | )
|
| 114 | colorAttachment != null -> colorAttachment.clearColor(color)
|
| 115 | depthAttachment != null -> depthAttachment.clearDepth(depth)
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | inline fun GpuTexture.asView(baseMipLevel: Int = 0, mipLevels: Int = this.mipLevels): GpuTextureView =
|
| 120 | gpuDevice.createTextureView(this, baseMipLevel, mipLevels)
|
| 121 |
|
| 122 | inline fun GpuBuffer.mapBuffer(read: Boolean = false, write: Boolean = false): GpuBufferSlice.MappedView =
|
| 123 | this.map(read, write)
|
| 124 |
|
| 125 | inline fun GpuBufferSlice.mapBuffer(read: Boolean = false, write: Boolean = false): GpuBufferSlice.MappedView =
|
| 126 | this.map(read, write)
|
| 127 |
|
| 128 | fun GpuBuffer.readFully(): ByteBuffer = read(0L, this.size())
|
| 129 |
|
| 130 | /**
|
| 131 | * @receiver Should have flag [GpuBuffer.USAGE_MAP_READ]
|
| 132 | * @return A [ByteBuffer] allocated with [MemoryUtil]
|
| 133 | */
|
| 134 | fun GpuBuffer.read(offset: Long, length: Long): ByteBuffer = this.map(offset, length, true, false).use {
|
| 135 | val source = it.data
|
| 136 | val result = MemoryUtil.memAlloc(source.remaining())
|
| 137 | try {
|
| 138 | MemoryUtil.memCopy(
|
| 139 | MemoryUtil.memAddress(source),
|
| 140 | MemoryUtil.memAddress(result),
|
| 141 | result.remaining().toLong(),
|
| 142 | )
|
| 143 | result
|
| 144 | } catch (t: Throwable) {
|
| 145 | MemoryUtil.memFree(result)
|
| 146 | throw t
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | inline fun GpuBufferSlice.write(byteBuffer: ByteBuffer) =
|
| 151 | gpuDevice.createCommandEncoder().writeToBuffer(this, byteBuffer)
|
| 152 |
|
| 153 | inline fun GpuBufferSlice.copyFrom(source: GpuBufferSlice) =
|
| 154 | gpuDevice.createCommandEncoder().copyToBuffer(source, this)
|
| 155 |
|
| 156 | @Suppress("LongParameterList")
|
| 157 | inline fun GpuTexture.write(
|
| 158 | source: NativeImage,
|
| 159 | mipLevel: Int = 0,
|
| 160 | depthOrLayer: Int = 0,
|
| 161 | destX: Int = 0,
|
| 162 | destY: Int = 0,
|
| 163 | width: Int = getWidth(mipLevel),
|
| 164 | height: Int = getHeight(mipLevel),
|
| 165 | sourceX: Int = 0,
|
| 166 | sourceY: Int = 0,
|
| 167 | ) {
|
| 168 | val commandEncoder = gpuDevice.createCommandEncoder()
|
| 169 | val slice = commandEncoder.transientMemory()
|
| 170 | .uploadStaging(source.pixelBytes, 1L, GpuBuffer.USAGE_COPY_SRC)
|
| 171 | commandEncoder.copyBufferToTexture(
|
| 172 | slice, sourceX, sourceY, source.width, source.height,
|
| 173 | this, destX, destY, width, height,
|
| 174 | mipLevel,depthOrLayer,
|
| 175 | )
|
| 176 | }
|
| 177 |
|
| 178 | inline fun GpuTexture.copyTo(
|
| 179 | destination: GpuBuffer,
|
| 180 | offset: Long = 0L,
|
| 181 | mipLevel: Int = 0,
|
| 182 | x: Int = 0,
|
| 183 | y: Int = 0,
|
| 184 | width: Int = getWidth(mipLevel),
|
| 185 | height: Int = getHeight(mipLevel),
|
| 186 | callback: Runnable = Runnables.doNothing(),
|
| 187 | ) = gpuDevice.createCommandEncoder().copyTextureToBuffer(
|
| 188 | this, destination, offset, callback, mipLevel,
|
| 189 | x, y, width, height,
|
| 190 | )
|
| 191 |
|
| 192 | fun GpuTexture.asyncCopyTo(
|
| 193 | destination: GpuBuffer,
|
| 194 | offset: Long = 0L,
|
| 195 | mipLevel: Int = 0,
|
| 196 | x: Int = 0,
|
| 197 | y: Int = 0,
|
| 198 | width: Int = getWidth(mipLevel),
|
| 199 | height: Int = getHeight(mipLevel),
|
| 200 | ): CompletableFuture<*> {
|
| 201 | val future = CompletableFuture<Any?>()
|
| 202 | copyTo(destination, offset, mipLevel, x, y, width, height) { future.complete(null) }
|
| 203 | return future
|
| 204 | }
|
| 205 |
|
| 206 | @JvmOverloads
|
| 207 | fun GpuTexture.copyFully(
|
| 208 | labelGetter: Supplier<String>? = null,
|
| 209 | usage: @GpuTexture.Usage Int = 0,
|
| 210 | ): GpuTexture {
|
| 211 | val dest = gpuDevice.createTexture(
|
| 212 | labelGetter,
|
| 213 | GpuTexture.USAGE_COPY_DST or usage,
|
| 214 | format,
|
| 215 | getWidth(0), getHeight(0),
|
| 216 | depthOrLayers, mipLevels,
|
| 217 | )
|
| 218 |
|
| 219 | for (mipLevel in 0 until mipLevels) {
|
| 220 | dest.copyFrom(this, mipLevel)
|
| 221 | }
|
| 222 |
|
| 223 | return dest
|
| 224 | }
|
| 225 |
|
| 226 | @Suppress("LongParameterList")
|
| 227 | inline fun GpuTexture.copyFrom(
|
| 228 | source: GpuTexture,
|
| 229 | mipLevel: Int = 0,
|
| 230 | intoX: Int = 0,
|
| 231 | intoY: Int = 0,
|
| 232 | sourceX: Int = 0,
|
| 233 | sourceY: Int = 0,
|
| 234 | width: Int = source.getWidth(mipLevel),
|
| 235 | height: Int = source.getHeight(mipLevel),
|
| 236 | ) = gpuDevice.createCommandEncoder().copyTextureToTexture(
|
| 237 | source, this, mipLevel, intoX, intoY, sourceX, sourceY, width, height
|
| 238 | )
|
| 239 |
|
| 240 | fun GpuTexture.saveToFile(file: File): CompletableFuture<*> =
|
| 241 | this.toNativeImage().thenAcceptAsync({ nativeImage ->
|
| 242 | nativeImage.writeToFile(file)
|
| 243 | nativeImage.close()
|
| 244 | }, Dispatchers.IO.asExecutor())
|
| 245 |
|
| 246 | private fun GpuBufferSlice.readNativeImageRGBA(
|
| 247 | width: Int,
|
| 248 | height: Int,
|
| 249 | destination: NativeImage = NativeImage(width, height, false),
|
| 250 | ): NativeImage {
|
| 251 | this.mapBuffer(read = true, write = false).use { mappedView ->
|
| 252 | for (y in 0..<height) {
|
| 253 | for (x in 0..<width) {
|
| 254 | val abgr = mappedView.data().getInt((x + y * width) * GpuFormat.RGBA8_UNORM.blockSize())
|
| 255 | destination.setPixelABGR(x, height - y - 1, abgr)
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 | return destination
|
| 260 | }
|
| 261 |
|
| 262 | /**
|
| 263 | * @see net.minecraft.client.Screenshot.takeScreenshot
|
| 264 | */
|
| 265 | @JvmOverloads
|
| 266 | fun GpuTexture.toNativeImage(mipLevel: Int = 0): CompletableFuture<NativeImage> {
|
| 267 | val width = this.getWidth(mipLevel)
|
| 268 | val height = this.getHeight(mipLevel)
|
| 269 | val pixelSize = this.format.blockSize()
|
| 270 | val gpuBuffer = gpuDevice.createBuffer(
|
| 271 | { "PixelBuffer - " + (this.label ?: "Anonymous") },
|
| 272 | GpuBuffer.USAGE_MAP_READ or GpuBuffer.USAGE_COPY_DST,
|
| 273 | width * height * pixelSize.toLong()
|
| 274 | )
|
| 275 |
|
| 276 | val future = CompletableFuture<NativeImage>()
|
| 277 |
|
| 278 | this.copyTo(gpuBuffer, mipLevel = mipLevel) {
|
| 279 | future.complete(gpuBuffer.slice().readNativeImageRGBA(width, height))
|
| 280 | gpuBuffer.close()
|
| 281 | }
|
| 282 |
|
| 283 | return future
|
| 284 | }
|
| 285 |
|
| 286 | @JvmOverloads
|
| 287 | fun GpuTexture.toBufferedImage(mipLevel: Int = 0): CompletableFuture<BufferedImage> {
|
| 288 | val width = this.getWidth(mipLevel)
|
| 289 | val height = this.getHeight(mipLevel)
|
| 290 | val pixelSize = this.format.blockSize()
|
| 291 | val gpuBuffer = gpuDevice.createBuffer(
|
| 292 | { "PixelBuffer - " + (this.label ?: "Anonymous") },
|
| 293 | GpuBuffer.USAGE_MAP_READ or GpuBuffer.USAGE_COPY_DST,
|
| 294 | width * height * pixelSize.toLong()
|
| 295 | )
|
| 296 |
|
| 297 | val future = CompletableFuture<BufferedImage>()
|
| 298 | this.copyTo(gpuBuffer, mipLevel = mipLevel) {
|
| 299 | gpuBuffer.mapBuffer(read = true, write = false).use { mappedView ->
|
| 300 | val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
|
| 301 | for (y in 0..<height) {
|
| 302 | for (x in 0..<width) {
|
| 303 | val abgr = mappedView.data().getInt((x + y * width) * pixelSize)
|
| 304 | bufferedImage.setRGB(x, height - y - 1, ARGB.fromABGR(abgr))
|
| 305 | }
|
| 306 | }
|
| 307 | future.complete(bufferedImage)
|
| 308 | }
|
| 309 | gpuBuffer.close()
|
| 310 | }
|
| 311 |
|
| 312 | return future
|
| 313 | }
|
| 314 |
|
| 315 | fun DynamicTexture.uploadRect(
|
| 316 | mipLevel: Int,
|
| 317 | x: Int, y: Int,
|
| 318 | width: Int, height: Int,
|
| 319 | ) = this.texture.write(
|
| 320 | source = this.pixels!!,
|
| 321 | mipLevel, depthOrLayer = 0,
|
| 322 | x, y,
|
| 323 | width, height,
|
| 324 | x, y,
|
| 325 | )
|
| 326 |
|
| 327 | fun NativeImage.toBufferedImage(): BufferedImage {
|
| 328 | val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
|
| 329 |
|
| 330 | bufferedImage.setRGB(
|
| 331 | 0,
|
| 332 | 0,
|
| 333 | width,
|
| 334 | height,
|
| 335 | pixels,
|
| 336 | 0,
|
| 337 | width
|
| 338 | )
|
| 339 |
|
| 340 | return bufferedImage
|
| 341 | }
|
| 342 |
|
| 343 | fun BufferedImage.toNativeImage(): NativeImage {
|
| 344 | val nativeImage = NativeImage(NativeImage.Format.RGBA, this.width, this.height, false)
|
| 345 |
|
| 346 | try {
|
| 347 | copyToNativeImage(nativeImage, width = width, height = height)
|
| 348 | } catch (throwable: Throwable) {
|
| 349 | nativeImage.close()
|
| 350 | throw throwable
|
| 351 | }
|
| 352 |
|
| 353 | return nativeImage
|
| 354 | }
|
| 355 |
|
| 356 | @Suppress("LongParameterList")
|
| 357 | fun BufferedImage.copyToNativeImage(
|
| 358 | target: NativeImage,
|
| 359 | sourceX: Int = 0,
|
| 360 | sourceY: Int = 0,
|
| 361 | targetX: Int = 0,
|
| 362 | targetY: Int = 0,
|
| 363 | width: Int = this.width,
|
| 364 | height: Int = this.height,
|
| 365 | scratchBuffer: IntArray = IntArray(0),
|
| 366 | ): IntArray {
|
| 367 | require(!target.isClosed) { "Target image is closed" }
|
| 368 | require(target.format() == NativeImage.Format.RGBA) { "Target image must use RGBA format" }
|
| 369 | require(width >= 0 && height >= 0) { "Copy dimensions must not be negative" }
|
| 370 | require(sourceX >= 0 && sourceY >= 0 && width <= this.width - sourceX && height <= this.height - sourceY) {
|
| 371 | "Source rectangle is outside the BufferedImage"
|
| 372 | }
|
| 373 | require(targetX >= 0 && targetY >= 0 && width <= target.width - targetX && height <= target.height - targetY) {
|
| 374 | "Target rectangle is outside the NativeImage"
|
| 375 | }
|
| 376 |
|
| 377 | val targetPixels = MemoryUtil.memIntBuffer(target.pointer, target.width * target.height)
|
| 378 | val dataBuffer = raster.dataBuffer
|
| 379 | val sampleModel = raster.sampleModel
|
| 380 |
|
| 381 | if (type == BufferedImage.TYPE_INT_ARGB &&
|
| 382 | dataBuffer is DataBufferInt && sampleModel is SinglePixelPackedSampleModel
|
| 383 | ) {
|
| 384 | val sourcePixels = dataBuffer.data
|
| 385 | val sourceOffset = dataBuffer.offset +
|
| 386 | (sourceY - raster.sampleModelTranslateY) * sampleModel.scanlineStride +
|
| 387 | sourceX - raster.sampleModelTranslateX
|
| 388 |
|
| 389 | copyArgbRows(
|
| 390 | sourcePixels,
|
| 391 | sourceOffset,
|
| 392 | sampleModel.scanlineStride,
|
| 393 | targetPixels,
|
| 394 | targetX + targetY * target.width,
|
| 395 | target.width,
|
| 396 | width,
|
| 397 | height,
|
| 398 | )
|
| 399 | return scratchBuffer
|
| 400 | }
|
| 401 |
|
| 402 | val requiredSize = width * height
|
| 403 | val argbPixels = scratchBuffer.takeIf { it.size >= requiredSize } ?: IntArray(requiredSize)
|
| 404 | getRGB(sourceX, sourceY, width, height, argbPixels, 0, width)
|
| 405 | copyArgbRows(
|
| 406 | argbPixels,
|
| 407 | 0,
|
| 408 | width,
|
| 409 | targetPixels,
|
| 410 | targetX + targetY * target.width,
|
| 411 | target.width,
|
| 412 | width,
|
| 413 | height,
|
| 414 | )
|
| 415 | return argbPixels
|
| 416 | }
|
| 417 |
|
| 418 | private fun copyArgbRows(
|
| 419 | source: IntArray,
|
| 420 | sourceOffset: Int,
|
| 421 | sourceStride: Int,
|
| 422 | target: IntBuffer,
|
| 423 | targetOffset: Int,
|
| 424 | targetStride: Int,
|
| 425 | width: Int,
|
| 426 | height: Int,
|
| 427 | ) {
|
| 428 | for (y in 0 until height) {
|
| 429 | val sourceRow = sourceOffset + y * sourceStride
|
| 430 | val targetRow = targetOffset + y * targetStride
|
| 431 | for (x in 0 until width) {
|
| 432 | target.put(targetRow + x, ARGB.toABGR(source[sourceRow + x]))
|
| 433 | }
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | /**
|
| 438 | * Should be called from main thread.
|
| 439 | */
|
| 440 | fun NativeImage.registerTexture(identifier: Identifier): DynamicTexture {
|
| 441 | val texture = asTexture(identifier::toString)
|
| 442 | mc.textureManager.register(identifier, texture)
|
| 443 | return texture
|
| 444 | }
|
| 445 |
|
| 446 | /**
|
| 447 | * Read and close stream. Accepts PNG.
|
| 448 | */
|
| 449 | inline fun InputStream.readNativeImage(): NativeImage = NativeImage.read(this)
|
| 450 |
|
| 451 | /**
|
| 452 | * Read and close source. Accepts JPEG and PNG.
|
| 453 | */
|
| 454 | fun BufferedSource.readNativeImage(): NativeImage =
|
| 455 | this.ensurePngOrConvertJpeg().inputStream().readNativeImage()
|
| 456 |
|
| 457 | /**
|
| 458 | * Read from file. Accepts JPEG and PNG.
|
| 459 | */
|
| 460 | fun File.readNativeImage(): NativeImage =
|
| 461 | this.source().buffer().readNativeImage()
|
| 462 |
|
| 463 | inline fun NativeImage.asTexture(
|
| 464 | name: String = "Texture NativeImage@${this.hashCode().toString(16)} (${this.width}x${this.height})",
|
| 465 | ) = DynamicTexture(Suppliers.ofInstance(name), this)
|
| 466 |
|
| 467 | @JvmOverloads
|
| 468 | fun NativeImage.asTexture(
|
| 469 | nameSupplier: Supplier<String> = Supplier {
|
| 470 | "Texture NativeImage@${this.hashCode().toString(16)} (${this.width}x${this.height})"
|
| 471 | },
|
| 472 | ) = DynamicTexture(nameSupplier, this)
|
| 473 |
|
| 474 | val AbstractTexture.textureSetup: TextureSetup
|
| 475 | get() = TextureSetup.singleTexture(textureView, sampler)
|
| 476 |
|
| 477 | inline fun GpuTextureView.asTextureSetup(sampler: GpuSampler): TextureSetup =
|
| 478 | TextureSetup.singleTexture(this, sampler)
|
| 479 |
|
| 480 | inline fun ByteBuffer.toGpuBuffer(
|
| 481 | labelGetter: Supplier<String>? = null,
|
| 482 | usage: @GpuBuffer.Usage Int,
|
| 483 | ): GpuBuffer = gpuDevice.createBuffer(labelGetter, usage, this)
|
| 484 |
|
| 485 | @JvmInline
|
| 486 | value class KStd140SizeCalculator(val j: Std140SizeCalculator) {
|
| 487 | inline val float: Unit
|
| 488 | get() {
|
| 489 | j.putFloat()
|
| 490 | }
|
| 491 | inline val int: Unit
|
| 492 | get() {
|
| 493 | j.putInt()
|
| 494 | }
|
| 495 | inline val vec2: Unit
|
| 496 | get() {
|
| 497 | j.putVec2()
|
| 498 | }
|
| 499 | inline val ivec2: Unit
|
| 500 | get() {
|
| 501 | j.putIVec2()
|
| 502 | }
|
| 503 | inline val vec3: Unit
|
| 504 | get() {
|
| 505 | j.putVec3()
|
| 506 | }
|
| 507 | inline val ivec3: Unit
|
| 508 | get() {
|
| 509 | j.putIVec3()
|
| 510 | }
|
| 511 | inline val vec4: Unit
|
| 512 | get() {
|
| 513 | j.putVec4()
|
| 514 | }
|
| 515 | inline val ivec4: Unit
|
| 516 | get() {
|
| 517 | j.putIVec4()
|
| 518 | }
|
| 519 | inline val mat4f: Unit
|
| 520 | get() {
|
| 521 | j.putMat4f()
|
| 522 | }
|
| 523 |
|
| 524 | inline fun align(alignedSize: Int) {
|
| 525 | j.align(alignedSize)
|
| 526 | }
|
| 527 |
|
| 528 | inline operator fun Unit.plus(other: Unit) {
|
| 529 | // NOOP
|
| 530 | }
|
| 531 |
|
| 532 | inline fun get() = j.get()
|
| 533 | }
|
| 534 |
|
| 535 | inline fun std140Size(block: KStd140SizeCalculator.() -> Unit): Int =
|
| 536 | KStd140SizeCalculator(Std140SizeCalculator()).apply(block).get()
|
| 537 |
|
| 538 | inline fun GpuDevice.createUbo(
|
| 539 | labelGetter: Supplier<String>? = null,
|
| 540 | std140Size: KStd140SizeCalculator.() -> Unit,
|
| 541 | ): GpuBuffer =
|
| 542 | createBuffer(
|
| 543 | labelGetter,
|
| 544 | GpuBuffer.USAGE_UNIFORM or GpuBuffer.USAGE_MAP_WRITE,
|
| 545 | std140Size(std140Size).toLong()
|
| 546 | )
|
| 547 |
|
| 548 | inline fun ByteBuffer.writeStd140(action: Std140Builder.() -> Unit) {
|
| 549 | Std140Builder.intoBuffer(this).apply(action)
|
| 550 | }
|
| 551 |
|
| 552 | inline fun GpuBufferSlice.writeStd140(action: Std140Builder.() -> Unit): GpuBufferSlice =
|
| 553 | this.mapBuffer(read = false, write = true).use {
|
| 554 | it.data().writeStd140(action)
|
| 555 |
|
| 556 | this
|
| 557 | }
|
| 558 |
|
| 559 | inline fun Std140Builder.putVec4(color: Color4b): Std140Builder =
|
| 560 | putVec4(color.r / 255f, color.g / 255f, color.b / 255f, color.a / 255f)
|
| 561 |
|