Skip to content

Commit

Permalink
only update mesh bind group if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
fabmax committed Jan 25, 2025
1 parent 27c11e0 commit ad67808
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class RenderPassEncoderState(val backend: RenderBackendVk): PassEncoderState {
var layer = 0
private set
private var activePipeline: VkGraphicsPipeline = VkGraphicsPipeline(0L)
private var currentViewGroup: BindGroupDataVk? = null
private var currentPipelineGroup: BindGroupDataVk? = null

private val descriptorSets = MemoryUtil.memAllocLong(3)

Expand Down Expand Up @@ -98,6 +100,8 @@ class RenderPassEncoderState(val backend: RenderBackendVk): PassEncoderState {
mipLevel = 0
layer = 0
activePipeline = VkGraphicsPipeline(0L)
currentViewGroup = null
currentPipelineGroup = null
}
}

Expand All @@ -114,6 +118,13 @@ class RenderPassEncoderState(val backend: RenderBackendVk): PassEncoderState {
meshGroup: BindGroupDataVk,
pipelineLayout: VkPipelineLayout
) {
if (viewGroup == currentViewGroup && pipelineGroup == currentPipelineGroup) {
setBindGroup(meshGroup, pipelineLayout)
return
}
currentViewGroup = viewGroup
currentPipelineGroup = pipelineGroup

val viewIdx = viewGroup.data.layout.group
val pipelineIdx = pipelineGroup.data.layout.group
val meshIdx = meshGroup.data.layout.group
Expand All @@ -132,10 +143,7 @@ class RenderPassEncoderState(val backend: RenderBackendVk): PassEncoderState {
)
}

fun setBindGroup(
bindGroup: BindGroupDataVk,
pipelineLayout: VkPipelineLayout
) {
fun setBindGroup(bindGroup: BindGroupDataVk, pipelineLayout: VkPipelineLayout) {
descriptorSets.limit(1)
bindGroup.bindGroup?.let { descriptorSets.put(0, it.getDescriptorSet(frameIndex).handle) }
vkCmdBindDescriptorSets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import de.fabmax.kool.pipeline.Attribute
import de.fabmax.kool.scene.*
import de.fabmax.kool.toString
import de.fabmax.kool.util.Color
import de.fabmax.kool.util.MdColor
import de.fabmax.kool.util.ColorGradient
import de.fabmax.kool.util.Time

class InstancingTest : DemoScene("Instancing") {

private val numObjects = mutableStateOf(1000)
private val numObjects = mutableStateOf(5000)
private val drawInstanced = mutableStateOf(false)
private val updateInstances = mutableStateOf(false)
private val numInstancesPerMesh = mutableStateOf(10)
Expand Down Expand Up @@ -118,13 +118,11 @@ class InstancingTest : DemoScene("Instancing") {
objects += mainScene.addColorMesh {
isFrustumChecked = false
generate {
color = MdColor.RED
cube {
origin.set(x.toFloat(), -30f, z.toFloat())
size.set(0.3f, 0.3f, 0.3f)
}
color = ColorGradient.JET_MD.getColor(i / numObjects.value.toFloat())
cube { size.set(0.3f, 0.3f, 0.3f) }
}
shader = directShader
transform.translate(x.toFloat(), -30f, z.toFloat())
}
}
}
Expand Down Expand Up @@ -176,7 +174,7 @@ class InstancingTest : DemoScene("Instancing") {
super.renderScene(ctx)
val p = (Time.precisionTime - t)
updateT = updateT * 0.9 + p * 1000.0 * 0.1
drawT = drawT * 0.9 + sceneRecordTime.inWholeMilliseconds * 0.1
drawT = drawT * 0.9 + sceneRecordTime.inWholeMicroseconds / 1000.0 * 0.1

if (Time.frameCount % 5 == 0) {
updateTstate.set(updateT)
Expand Down

0 comments on commit ad67808

Please sign in to comment.