Skip to content

Commit

Permalink
continue work on lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
LeHaine committed Jan 16, 2025
1 parent 77f5266 commit 58f914c
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PBREnvironment(override val buffers: CameraLightBuffers) : Environment(buf
else error("Unsupported $bindingUsage in PBREnvironment")
},
ProgrammableStage(boundsShader.shaderModule, boundsShader.computeEntryPoint),
label = "Computer Cluster Bounds Compute Pipeline",
)
)
private val boundsBindGroup =
Expand Down Expand Up @@ -78,6 +79,7 @@ class PBREnvironment(override val buffers: CameraLightBuffers) : Environment(buf
)
},
ProgrammableStage(lightsShader.shaderModule, lightsShader.computeEntryPoint),
label = "Compute Cluster Lights Compute Pipeline",
)
)
private val lightsBindGroup =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class ClusterBuffers(
)
)

/** The [BufferBinding] for [clusterBoundsStorageBuffer]. */
/** The [BufferBinding] for [clusterLightsStorageBuffer]. */
val clusterLightsStorageBufferBinding = BufferBinding(clusterLightsStorageBuffer)

/** Resets the [clusterBoundsStorageBuffer] offset back to zero. */
/** Resets the [clusterLightsStorageBuffer] offset back to zero. */
fun resetClusterLightsOffsetToZero() =
device.queue.writeBuffer(clusterLightsStorageBuffer, emptyData)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ class LightBuffer(val device: Device, val maxLightCount: Int) : Releasable {
device.queue.writeBuffer(buffer, lightsBuffer)
}

private fun printPointLights() {
println("--------------------------")
val pointLights = lightsBuffer[11].toInt()
println("total point lights: $pointLights")
repeat(pointLights) { i ->
val idx = i + 1
val offset = POINT_LIGHT_OFFSET * idx
println("Point light $idx:")
println(
"position: (${lightsBuffer[offset]}, ${lightsBuffer[offset + 1]}, ${lightsBuffer[offset + 2]})"
)
println("range: (${lightsBuffer[offset + 3]})")
println(
"color: (${lightsBuffer[offset + 4]}, ${lightsBuffer[offset + 5]}, ${lightsBuffer[offset + 6]})"
)
println("intensity: (${lightsBuffer[offset + 7]})")
}
}

fun ambient(color: Color) {
lightsBuffer[0] = color.r
lightsBuffer[1] = color.g
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.littlekt.graphics.webgpu.MemoryAccessMode
* @author Colton Daily
* @date 1/5/2025
*/
class ClusteredBoundsComputerShader(
class ClusteredBoundsComputeShaderBuilder(
tileCountX: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_X,
tileCountY: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Y,
tileCountZ: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Z,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.littlekt.graphics.webgpu.MemoryAccessMode
* @author Colton Daily
* @date 1/5/2025
*/
class ClusteredLightComputerShader(
class ClusteredLightComputeShaderBuilder(
tileCountX: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_X,
tileCountY: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Y,
tileCountZ: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Z,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ fun SubShaderBuilder.clusterLights(
tileCountX: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_X,
tileCountY: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Y,
tileCountZ: Int = CommonSubShaderFunctions.DEFAULT_TILE_COUNT_Z,
maxLightsPerCluster: Int = CommonSubShaderFunctions.DEFAULT_MAX_LIGHTS_PER_CLUSTER,
access: MemoryAccessMode = MemoryAccessMode.READ,
) {
val totalTiles = tileCountX * tileCountY * tileCountZ
Expand All @@ -272,7 +271,7 @@ fun SubShaderBuilder.clusterLights(
struct ClusterLightGroup {
offset : ${if(access == MemoryAccessMode.READ) "u32" else "atomic<u32>"},
lights : array<ClusterLights, ${totalTiles}>,
indices : array<u32, ${maxLightsPerCluster}>,
indices : array<u32, ${totalTiles * 64}>,
};
@group(${group}) @binding(${binding})
var<storage, ${access.value}> clusterLights : ClusterLightGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ open class ComputeShaderBuilder {
* [ClusteredComputeShaderBuilder.clusterLights] with [MemoryAccessMode.READ_WRITE], [light],
* and [ClusteredComputeShaderBuilder.tileFunctions].
*/
fun clusteredLight(block: ClusteredLightComputerShader.() -> Unit) {
val builder = ClusteredLightComputerShader()
fun clusteredLight(block: ClusteredLightComputeShaderBuilder.() -> Unit) {
val builder = ClusteredLightComputeShaderBuilder()
builder.block()
parts += builder.build()
}
Expand All @@ -24,8 +24,8 @@ open class ComputeShaderBuilder {
* Requires [ClusteredComputeShaderBuilder.cluster] with [MemoryAccessMode.READ_WRITE] and
* [camera].
*/
fun clusteredBounds(block: ClusteredBoundsComputerShader.() -> Unit) {
val builder = ClusteredBoundsComputerShader()
fun clusteredBounds(block: ClusteredBoundsComputeShaderBuilder.() -> Unit) {
val builder = ClusteredBoundsComputeShaderBuilder()
builder.block()
parts += builder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import com.littlekt.ContextListener
import com.littlekt.file.gltf.GltfModelPbrConfig
import com.littlekt.file.gltf.toModel
import com.littlekt.file.vfs.readGltf
import com.littlekt.graph.node.ui.Control
import com.littlekt.graph.node.ui.column
import com.littlekt.graph.node.ui.label
import com.littlekt.graph.sceneGraph
import com.littlekt.graphics.Color
import com.littlekt.graphics.PerspectiveCamera
import com.littlekt.graphics.g3d.ModelBatch
Expand Down Expand Up @@ -37,11 +41,47 @@ class PBRExample(context: Context) : ContextListener(context) {
DirectionalLight(color = Color(0.2f, 0.2f, 0.2f), intensity = 0.1f)
)
environment.setAmbientLight(AmbientLight(color = Color(0.002f, 0.002f, 0.002f)))
environment.addPointLight(PointLight(Vec3f(0f, 0f, 0f), color = Color.RED, range = 5f))
environment.addPointLight(
PointLight(Vec3f(1145f, 240f, 410f), color = Color.RED, range = 5f)
)
environment.addPointLight(
PointLight(Vec3f(-1180f, 240f, 420f), color = Color.GREEN, range = 4f)
)
// environment.addPointLight(PointLight(Vec3f(8.95f, 5f, 30.5f), range = 4f))

val surfaceCapabilities = graphics.surfaceCapabilities
val preferredFormat = graphics.preferredFormat
val graph =
sceneGraph(this) {
column {
anchor(Control.AnchorLayout.TOP_LEFT)
marginLeft = 10f
var fps = -1
var lowest = -1
var ticksToWaitBeforeTracking = 100

onUpdate {
if (ticksToWaitBeforeTracking > 0) {
ticksToWaitBeforeTracking--
return@onUpdate
}
fps = stats.fps.toInt()
if (lowest < 0 && fps > 0) {
lowest = fps
}

if (fps < lowest) {
lowest = fps
}
if (input.isKeyJustPressed(Key.R)) {
lowest = -1
}
}
label { onUpdate { text = "FPS: $fps" } }
label { onUpdate { text = "Lowest: $lowest" } }
}
}
.also { it.initialize() }

val queue = device.queue

Expand Down Expand Up @@ -100,6 +140,8 @@ class PBRExample(context: Context) : ContextListener(context) {
camera.virtualWidth = width.toFloat()
camera.virtualHeight = height.toFloat()
camera.update()

graph.resize(width, height)
}

addFlyController(camera, 0.5f)
Expand Down Expand Up @@ -168,6 +210,21 @@ class PBRExample(context: Context) : ContextListener(context) {
renderPassEncoder.end()
renderPassEncoder.release()

graph.update(dt)
graph.render(
commandEncoder,
RenderPassDescriptor(
listOf(
RenderPassColorAttachmentDescriptor(
view = frame,
loadOp = LoadOp.LOAD,
storeOp = StoreOp.STORE,
)
),
label = "ui render pass",
),
)

val commandBuffer = commandEncoder.finish()

queue.submit(commandBuffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ val availableExamples =
"-simpleGltf" to Pair("Simple GLtf", ::SimpleGltfExample),
"-mipMaps" to Pair("Mip Maps", ::MipMapsExample),
"-modelInstancing" to Pair("Model Instancing", ::ModelInstancingExample),
"-pbr" to Pair("Model Instancing", ::PBRExample),
"-pbr" to Pair("PBR", ::PBRExample),
)
10 changes: 3 additions & 7 deletions examples/src/commonMain/kotlin/com/littlekt/examples/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,20 @@ fun Context.addFlyController(camera: Camera, speed: Float) {
val right = MutableVec3f()
val up = MutableVec3f()

var locked = false

onUpdate {
if (!locked && input.isJustTouched(Pointer.POINTER1)) {
if (!input.cursorLocked && input.isJustTouched(Pointer.POINTER1)) {
input.lockCursor()
locked = true
}

if (locked && input.isKeyJustPressed(Key.ESCAPE)) {
if (input.cursorLocked && input.isKeyJustPressed(Key.ESCAPE)) {
input.releaseCursor()
locked = false
}
}

val quat = MutableQuaternion()

onUpdate { dt ->
if (!locked) return@onUpdate
if (!input.cursorLocked) return@onUpdate

val sprint = input.isKeyPressed(Key.SHIFT_LEFT) || input.isKeyPressed(Key.SHIFT_RIGHT)
val slow = input.isKeyPressed(Key.CTRL_LEFT) || input.isKeyPressed(Key.CTRL_RIGHT)
Expand Down

0 comments on commit 58f914c

Please sign in to comment.