Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertions in PersistentTextureRequestsExample #737

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ open class VulkanTexture(

tmpBuffer?.let { b ->
with(VU.newCommandBuffer(device, commandPools.Transfer, autostart = true)) {
logger.info("${System.nanoTime()}: Copying $width $height $depth")
logger.debug("${System.nanoTime()}: Copying $width $height $depth")
transitionLayout(image.image,
from = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
to = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/graphics/scenery/utils/SystemHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,21 @@ class SystemHelpers {
*
* @param[buf] The ByteBuffer to dump.
*/
fun dumpToFile(buf: ByteBuffer, filename: String) {
fun dumpToFile(buf: ByteBuffer, filename: String): File? {
try {
val view = buf.duplicate().order(ByteOrder.LITTLE_ENDIAN)
val file = File(filename)
val channel = FileOutputStream(file, false).channel
channel.write(view)
channel.close()

return file
} catch (e: Exception) {
logger.error("Unable to dump byte buffer to $filename")
e.printStackTrace()
}

return null
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.lwjgl.system.MemoryUtil
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

/**
* Example to show how persistent texture requests - that are served once per frame - may be created
Expand Down Expand Up @@ -63,21 +64,22 @@ class PersistentTextureRequestsExample : SceneryBase("PersistentTextureRequestsE

val volume = Volume.fromRAI(img, UnsignedShortType(), AxisOrder.DEFAULT, "T1 head", hub, VolumeViewerOptions())
volume.transferFunction = TransferFunction.ramp(0.001f, 0.5f, 0.3f)
volume.spatial().scale = Vector3f(20.0f)
scene.addChild(volume)

val box = Box(Vector3f(1.0f, 1.0f, 1.0f))
val box = Box(Vector3f(2.0f))
box.name = "le box du win"
box.material {
textures["diffuse"] = outputTexture
metallic = 0.0f
metallic = 0.5f
roughness = 1.0f
}

scene.addChild(box)

val light = PointLight(radius = 15.0f)
light.spatial().position = Vector3f(0.0f, 0.0f, 2.0f)
light.intensity = 5.0f
light.intensity = 15.0f
light.emissionColor = Vector3f(1.0f, 1.0f, 1.0f)
scene.addChild(light)

Expand All @@ -89,6 +91,14 @@ class PersistentTextureRequestsExample : SceneryBase("PersistentTextureRequestsE
scene.addChild(this)
}

renderer?.runAfterRendering?.add {
// persistent texture requests run in sync with frame rendering,
// so their count should always be one less than the total number
// of frames rendered (totalFrames is only incremented at the very
// end of the render loop, in submitFrame).
renderer?.let { assertEquals(counter.get().toLong(), it.totalFrames+1) }
}

thread {
val opTexture = volumeManager.material().textures["OutputRender"]!!

Expand All @@ -108,33 +118,16 @@ class PersistentTextureRequestsExample : SceneryBase("PersistentTextureRequestsE
//the buffer can now, e.g., be transmitted, as is required for parallel rendering

if (buffer != null && prevCounter == 100) {
SystemHelpers.dumpToFile(buffer, "texture-${SystemHelpers.formatDateTime(delimiter = "_")}.raw")
}
}
}
val file = SystemHelpers.dumpToFile(buffer, "texture-${SystemHelpers.formatDateTime(delimiter = "_")}.raw")
assertNotNull(file, "File handle should not be null.")

thread {
while (renderer?.firstImageReady == false) {
Thread.sleep(5)
val sum = file.readBytes().sum()
logger.info("Dumped: $file, ${file.length()} bytes, sum=$sum")
assertEquals(file.length(), 1280*720*4, "File should contain the correct number of bytes for a 1280x720xRGBA image")
assert(sum > 3000000) { "Sum of bytes in file should be non-zero" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the sum compared with 3000000? Is it not sufficient to check that it is not equal to zero? Changes in volume rendering code down the line could change the image generated and therefore the sum

}
}

Thread.sleep(1000) //give some time for the rendering to take place

renderer?.close()
Thread.sleep(200) //give some time for the renderer to close

totalFrames = renderer?.totalFrames!!
}
}

override fun main() {
// add assertions, these only get called when the example is called
// as part of scenery's integration tests
assertions[AssertionCheckPoint.AfterClose]?.add {

assertEquals ( counter.get().toLong(), totalFrames, "One texture was returned per render frame" )
}
super.main()
}

/**
Expand Down
Loading