Skip to content

Commit 7347a5d

Browse files
committed
Some simple cleanups
1 parent b275e6a commit 7347a5d

File tree

4 files changed

+70
-45
lines changed

4 files changed

+70
-45
lines changed

openjfx/build.gradle.kts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,17 @@ dependencies {
66

77
implementation(project(":core"))
88

9-
val platform = when {
10-
current().isWindows -> "win"
11-
current().isLinux -> "linux"
12-
else -> "mac"
9+
val (platform, lwjglNatives) = when {
10+
current().isWindows -> "win" to "windows"
11+
current().isLinux -> "linux" to "linux"
12+
else -> "mac" to "macos"
1313
}
1414
listOf("base", "graphics").forEach {
1515
implementation("org.openjfx:javafx-$it:11:$platform")
1616
}
1717

18-
val kx = "com.github.kotlin-graphics"
19-
implementation("$kx:glm:${findProperty("glmVersion")}")
20-
21-
val lwjglNatives = "natives-" + when (current()) {
22-
WINDOWS -> "windows"
23-
LINUX -> "linux"
24-
else -> "macos"
25-
}
26-
2718
implementation("org.lwjgl", "lwjgl")
28-
runtimeOnly("org.lwjgl", "lwjgl", classifier = lwjglNatives)
19+
runtimeOnly("org.lwjgl", "lwjgl", classifier = "natives-$lwjglNatives")
2920
}
3021

3122
tasks.compileJava {

openjfx/src/main/kotlin/imgui/impl/ImplJFX.kt

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import javafx.scene.shape.FillRule
2222
import javafx.scene.shape.StrokeLineCap
2323
import javafx.scene.shape.StrokeLineJoin
2424
import javafx.stage.Stage
25+
import kotlin.math.abs
2526
import kotlin.math.roundToInt
2627

2728
typealias JFXColor = javafx.scene.paint.Color
@@ -400,9 +401,9 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
400401
val isBary = vtx1.col != vtx2.col || vtx2.col != vtx3.col
401402
val doBary = if (isBary) {
402403
triangleArea(vtx1.pos, vtx2.pos, vtx3.pos) >= BARYCENTRIC_SIZE_THRESHOLD &&
403-
atLeastTwo(Math.abs(vtx1.pos.x - vtx2.pos.x) >= 2.0,
404-
Math.abs(vtx1.pos.x - vtx3.pos.x) >= 2.0,
405-
Math.abs(vtx3.pos.x - vtx2.pos.x) >= 2.0)
404+
atLeastTwo(abs(vtx1.pos.x - vtx2.pos.x) >= 2.0,
405+
abs(vtx1.pos.x - vtx3.pos.x) >= 2.0,
406+
abs(vtx3.pos.x - vtx2.pos.x) >= 2.0)
406407
} else {
407408
false
408409
}
@@ -492,8 +493,8 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
492493

493494
//if it's a line, needs to be drawn specially so that only 1 pixel isn't drawn
494495
if (maxY - minY > 1.0f) {
495-
for (scanlineY in Math.round(minY).i..Math.round(maxY).i) {
496-
for (x in Math.round(curx1).i..Math.round(curx2).i) {
496+
for (scanlineY in minY.roundToInt()..maxY.roundToInt()) {
497+
for (x in curx1.roundToInt()..curx2.roundToInt()) {
497498
baryColor(Vec2(x, scanlineY))
498499
}
499500
curx1 += invslope1
@@ -504,7 +505,7 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
504505
} else {
505506
//average where the line goes
506507
val scanlineY = (maxY + minY) / 2.0f
507-
for (x in Math.round(minX).i..Math.round(maxX).i) {
508+
for (x in minX.roundToInt()..maxX.roundToInt()) {
508509
//draw the color at each point on the line
509510
baryColor(Vec2(x, scanlineY))
510511
}
@@ -532,8 +533,8 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
532533
val maxX = vs3.x max vs2.x max vs1.y
533534

534535
if (maxY - minY > 1.0f) {
535-
for (scanlineY in Math.round(maxY).i downTo Math.round(minY).i) {
536-
for (x in Math.round(curx1).i..Math.round(curx2).i) {
536+
for (scanlineY in maxY.roundToInt() downTo minY.roundToInt()) {
537+
for (x in curx1.roundToInt()..curx2.roundToInt()) {
537538
baryColor(Vec2(x, scanlineY))
538539
}
539540
curx1 -= invslope1
@@ -543,7 +544,7 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
543544
}
544545
} else {
545546
val scanlineY = (maxY + minY) / 2.0f
546-
for (x in Math.round(minX).i..Math.round(maxX).i) {
547+
for (x in minX.roundToInt()..maxX.roundToInt()) {
547548
baryColor(Vec2(x, scanlineY))
548549
}
549550
}
@@ -594,12 +595,36 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
594595
(((col1 ushr COL32_B_SHIFT) and COLOR_SIZE_MASK) * color.blue).i,
595596
(((col1 ushr COL32_A_SHIFT) and COLOR_SIZE_MASK) / COLOR_SIZE_MASK.toDouble()) * color.opacity)
596597
//add initial points
597-
addPoint(vtx1.pos.x, vtx1.pos.y)
598-
addPoint(vtx2.pos.x, vtx2.pos.y)
599-
addPoint(vtx3.pos.x, vtx3.pos.y)
598+
if (pos + 2 >= xs.size) {
599+
if (DEBUG)
600+
println("increase points buffer size (old ${xs.size}, new ${xs.size * 2})")
601+
val nx = DoubleArray(xs.size * 2)
602+
val ny = DoubleArray(ys.size * 2)
603+
xs.copyInto(nx)
604+
ys.copyInto(ny)
605+
xs = nx
606+
ys = ny
607+
}
608+
xs[pos] = vtx1.pos.x.d
609+
ys[pos++] = vtx1.pos.y.d
610+
xs[pos] = vtx2.pos.x.d
611+
ys[pos++] = vtx2.pos.y.d
612+
xs[pos] = vtx3.pos.x.d
613+
ys[pos++] = vtx3.pos.y.d
600614
}
601615
//add bordering point
602-
addPoint(vtx6.pos.x, vtx6.pos.y)
616+
if (pos == xs.size) {
617+
if (DEBUG)
618+
println("increase points buffer size (old ${xs.size}, new ${xs.size * 2})")
619+
val nx = DoubleArray(xs.size * 2)
620+
val ny = DoubleArray(ys.size * 2)
621+
xs.copyInto(nx)
622+
ys.copyInto(ny)
623+
xs = nx
624+
ys = ny
625+
}
626+
xs[pos] = vtx6.pos.x.d
627+
ys[pos++] = vtx6.pos.y.d
603628
} else {
604629
//does not border the next triangle , do either draw this triangle or draw the last one
605630
//if the last triangle bordered this one, the vertex is already in the shape, so just draw
@@ -755,7 +780,7 @@ fun Int.toVec4(): Vec4 {
755780

756781
inline fun dotProd(a: Vec2, b: Vec2) = ((a.x * b.x) + (a.y * b.y)).d
757782

758-
inline fun triangleArea(a: Vec2, b: Vec2, c: Vec2) = Math.abs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y)))
783+
inline fun triangleArea(a: Vec2, b: Vec2, c: Vec2) = abs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y)))
759784

760785
inline fun atLeastTwo(a: Boolean, b: Boolean, c: Boolean): Boolean {
761786
return if (a) b || c else b && c

vk/src/test/kotlin/imguiVk/helpers.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import vkk.*
1313
import vkk.entities.VkSurfaceKHR
1414
import vkk.extensions.*
1515
import vkk.identifiers.Instance
16-
import vkk.identifiers.VK
1716
import vkk.vk10.*
1817
import vkk.vk10.structs.*
18+
import kotlin.system.exitProcess
1919

2020
fun debugReport(flags: VkDebugReportFlagsEXT, objectType: VkDebugReportObjectTypeEXT, `object`: Long, location: Long, messageCode: Int,
2121
layerPrefix: String, message: String, userData: Ptr) =
@@ -106,7 +106,7 @@ fun setupVulkanWindow(wd: ImplVulkanH.Window, surface: VkSurfaceKHR, size: Vec2i
106106
val res = gPhysicalDevice.getSurfaceSupportKHR(gQueueFamily, wd.surface)
107107
if (!res) {
108108
System.err.println("Error no WSI support on physical device 0")
109-
System.exit(-1)
109+
exitProcess(-1)
110110
}
111111

112112
// Select Surface Format

vk/src/test/kotlin/imguiVk_/helpers_.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ val debugReport = VkDebugReportCallbackEXT.create { _, objectType, _, _, _, _, p
2323
}
2424

2525
var debugCallbackUtils = callback@ { severity: Int, type: Int, callbackDataPointer: Long, _: Long ->
26-
val dbg = if (type and EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT == EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
27-
" (performance)"
28-
} else if(type and EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT == EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
29-
" (validation)"
30-
} else {
31-
""
26+
val dbg = when {
27+
type and VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT == VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT -> " (performance)"
28+
type and VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT == VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT -> " (validation)"
29+
else -> ""
3230
}
3331

3432
val callbackData = VkDebugUtilsMessengerCallbackDataEXT.create(callbackDataPointer)
@@ -37,12 +35,9 @@ var debugCallbackUtils = callback@ { severity: Int, type: Int, callbackDataPoint
3735
val objectType = 0
3836

3937
when (severity) {
40-
EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT ->
41-
println("!! $obj($objectType) Validation$dbg: $message")
42-
EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT ->
43-
println("!! $obj($objectType) Validation$dbg: $message")
44-
EXTDebugUtils.VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT ->
45-
println("!! $obj($objectType) Validation$dbg: $message")
38+
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT -> println("!! $obj($objectType) Validation$dbg: $message")
39+
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT -> println("!! $obj($objectType) Validation$dbg: $message")
40+
VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT -> println("!! $obj($objectType) Validation$dbg: $message")
4641
else -> println("!! $obj($objectType) Validation (unknown message type)$dbg: $message")
4742
}
4843

@@ -63,8 +58,21 @@ fun setupVulkan_(extensions: PointerBuffer) = Stack { s ->
6358
System.err.println("DEBUG ENABLED, LOL")
6459

6560
// Enabling validation layers
66-
val layers = s.callocPointer(1)
67-
layers[0] = MemoryUtil.memASCII("VK_LAYER_KHRONOS_validation")
61+
val ib = s.mallocInt(1)
62+
vkEnumerateInstanceLayerProperties(ib, null)
63+
val pProps = VkLayerProperties.callocStack(ib[0])
64+
vkEnumerateInstanceLayerProperties(ib, pProps)
65+
var useKhrValidation = false
66+
for (p in pProps) {
67+
if (p.layerNameString() == "VK_LAYER_KHRONOS_validation") {
68+
useKhrValidation = true
69+
break
70+
}
71+
}
72+
System.err.println("using VK_LAYER_KHRONOS_validation? $useKhrValidation")
73+
val layers = s.callocPointer(if (useKhrValidation) 1 else 0)
74+
if (useKhrValidation)
75+
layers[0] = MemoryUtil.memASCII("VK_LAYER_KHRONOS_validation")
6876
createInfo.ppEnabledLayerNames(layers)
6977

7078
// Enable debug report extension (we need additional storage, so we duplicate the user array to add our new extension to it)
@@ -75,6 +83,7 @@ fun setupVulkan_(extensions: PointerBuffer) = Stack { s ->
7583

7684
// Create Vulkan Instance
7785
err = vkCreateInstance(createInfo, gAllocator, pP)
86+
check(err == 0) { "Failed to create a Vulkan instance! (code $err)" }
7887
gInstance = VkInstance(pP[0], createInfo)
7988
checkVkResult(err)
8089

@@ -86,7 +95,7 @@ fun setupVulkan_(extensions: PointerBuffer) = Stack { s ->
8695
.messageSeverity(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT or VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT or VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT or VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
8796
.messageType(VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT)
8897
// .pUserData(MemoryUtil.NULL)
89-
err = EXTDebugUtils.vkCreateDebugUtilsMessengerEXT(gInstance, debugReportCi, gAllocator, pL)
98+
err = vkCreateDebugUtilsMessengerEXT(gInstance, debugReportCi, gAllocator, pL)
9099
gDebugReport = pL[0]
91100
checkVkResult(err)
92101
} else {

0 commit comments

Comments
 (0)