Skip to content

Commit

Permalink
update LightBuffer underlying data to use a ByteBuffer to write data …
Browse files Browse the repository at this point in the history
…due to float to u32 conversion to something really high
  • Loading branch information
LeHaine committed Jan 17, 2025
1 parent 5e6c8a6 commit 2665a23
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class CameraLightBuffers(
override val cameraDynamicSize: Int = 1

/** The [BufferBinding] for [cameraUniformBufferBinding]. */
override val cameraUniformBufferBinding =
BufferBinding(cameraUniformBuffer, size = Float.SIZE_BYTES * BUFFER_SIZE.toLong())
override val cameraUniformBufferBinding = BufferBinding(cameraUniformBuffer)

override val bindGroupLayout: BindGroupLayout =
device.createBindGroupLayout(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.littlekt.graphics.g3d.util

import com.littlekt.Releasable
import com.littlekt.file.FloatBuffer
import com.littlekt.file.ByteBuffer
import com.littlekt.graphics.Color
import com.littlekt.graphics.webgpu.BufferBinding
import com.littlekt.graphics.webgpu.BufferUsage
Expand All @@ -17,11 +17,11 @@ class LightBuffer(val device: Device, val maxLightCount: Int) : Releasable {
/** Size of light buffer in number of components / floats. */
private val lightBufferSize =
AMBIENT_LIGHT_SIZE + DIRECTIONAL_LIGHT_SIZE + (POINT_LIGHT_SIZE * maxLightCount)
private val lightsBuffer = FloatBuffer(lightBufferSize)
private val lightsBuffer = ByteBuffer(lightBufferSize * Float.SIZE_BYTES)

/** The [GPUBuffer] that holds the light data */
val buffer =
device.createGPUFloatBuffer(
device.createGPUByteBuffer(
"light",
lightsBuffer.toArray(),
BufferUsage.STORAGE or BufferUsage.COPY_DST,
Expand All @@ -35,52 +35,81 @@ class LightBuffer(val device: Device, val maxLightCount: Int) : Releasable {
}

fun ambient(color: Color) {
lightsBuffer[0] = color.r
lightsBuffer[1] = color.g
lightsBuffer[2] = color.b
lightsBuffer[3] = 0f // padding
lightsBuffer.position = 0
lightsBuffer.putFloat(color.r)
lightsBuffer.putFloat(color.g)
lightsBuffer.putFloat(color.b)
lightsBuffer.putFloat(0f) // padding
// lightsBuffer[0] = color.r
// lightsBuffer[1] = color.g
// lightsBuffer[2] = color.b
// lightsBuffer[3] = 0f // padding
}

fun dirColor(color: Color) {
lightsBuffer[4] = color.r
lightsBuffer[5] = color.g
lightsBuffer[6] = color.b
lightsBuffer.position = 4 * Float.SIZE_BYTES
lightsBuffer.putFloat(color.r)
lightsBuffer.putFloat(color.g)
lightsBuffer.putFloat(color.b)
// lightsBuffer[4] = color.r
// lightsBuffer[5] = color.g
// lightsBuffer[6] = color.b
}

fun dirIntensity(intensity: Float) {
lightsBuffer[7] = intensity
lightsBuffer.position = 7 * Float.SIZE_BYTES
lightsBuffer.putFloat(intensity)
// lightsBuffer[7] = intensity
}

fun dirDirection(direction: Vec3f) {
lightsBuffer[8] = direction.x
lightsBuffer[9] = direction.y
lightsBuffer[10] = direction.z
lightsBuffer.position = 8 * Float.SIZE_BYTES
lightsBuffer.putFloat(direction.x)
lightsBuffer.putFloat(direction.y)
lightsBuffer.putFloat(direction.z)
// lightsBuffer[8] = direction.x
// lightsBuffer[9] = direction.y
// lightsBuffer[10] = direction.z
}

fun pointLight(index: Int, position: Vec3f, color: Color, intensity: Float, range: Float) {
if (index >= maxLightCount) {
return
}
if (intensity > 0) {
val offset = (POINT_LIGHT_OFFSET * index) + POINT_LIGHT_OFFSET
lightsBuffer[offset] = position.x
lightsBuffer[offset + 1] = position.y
lightsBuffer[offset + 2] = position.z
lightsBuffer[offset + 3] = range
lightsBuffer[offset + 4] = color.r
lightsBuffer[offset + 5] = color.g
lightsBuffer[offset + 6] = color.b
lightsBuffer[offset + 7] = intensity
val offset = (POINT_LIGHT_OFFSET * index * 4) + POINT_LIGHT_OFFSET * 4
lightsBuffer.position = offset
lightsBuffer.putFloat(position.x)
lightsBuffer.putFloat(position.y)
lightsBuffer.putFloat(position.z)
lightsBuffer.putFloat(range)
lightsBuffer.putFloat(color.r)
lightsBuffer.putFloat(color.g)
lightsBuffer.putFloat(color.b)
lightsBuffer.putFloat(intensity)
// lightsBuffer[offset] = position.x
// lightsBuffer[offset + 1] = position.y
// lightsBuffer[offset + 2] = position.z
// lightsBuffer[offset + 3] = range
// lightsBuffer[offset + 4] = color.r
// lightsBuffer[offset + 5] = color.g
// lightsBuffer[offset + 6] = color.b
// lightsBuffer[offset + 7] = intensity
incrementLightCount()
}
}

private fun incrementLightCount() {
lightsBuffer[11] += 1f
lightsBuffer.position = 11 * Float.SIZE_BYTES
val currentValue = lightsBuffer.readInt
lightsBuffer.position = 11 * Float.SIZE_BYTES
lightsBuffer.putInt(currentValue + 1)
}

fun resetLightCount() {
lightsBuffer[11] = 0f
lightsBuffer.position = 11 * Float.SIZE_BYTES
lightsBuffer.putInt(0)
// lightsBuffer[11] = 0f
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class PBRExample(context: Context) : ContextListener(context) {
environment.setAmbientLight(AmbientLight(color = Color(0.002f, 0.002f, 0.002f)))

environment.addPointLight(
PointLight(Vec3f(-1180f, 240f, 420f), color = Color.GREEN, range = 1f)
PointLight(Vec3f(-1180f, 240f, 420f), color = Color.GREEN, range = 4f)
)

environment.addPointLight(PointLight(Vec3f(8.95f, 5f, 30.5f), range = 1f))
environment.addPointLight(PointLight(Vec3f(8.95f, 5f, 30.5f), range = 4f))

val surfaceCapabilities = graphics.surfaceCapabilities
val preferredFormat = graphics.preferredFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class SimpleGltfExample(context: Context) : ContextListener(context) {
)
setAmbientLight(AmbientLight(color = Color(0.002f, 0.002f, 0.002f)))

addPointLight(PointLight(Vec3f(0f, 50f, 0f), color = Color.GREEN, range = 4f))
addPointLight(PointLight(Vec3f(0f, 50f, 0f), color = Color.GREEN, range = 4f))
addPointLight(PointLight(Vec3f(0f, 250f, 0f), color = Color.GREEN))
addPointLight(PointLight(Vec3f(0f, 250f, 0f), color = Color.GREEN))
}

val surfaceCapabilities = graphics.surfaceCapabilities
Expand Down

0 comments on commit 2665a23

Please sign in to comment.