Skip to content

Commit

Permalink
chore(api): Struct: add asSlice and change fixed size array
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 9, 2025
1 parent 88fcefb commit 2d2e8bd
Show file tree
Hide file tree
Showing 1,523 changed files with 16,624 additions and 3,720 deletions.
142 changes: 31 additions & 111 deletions generators/src/main/kotlin/overrungl/gen/Struct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Struct(
private val union: Boolean = false,
action: Struct.() -> Unit
) {
private val kindString = if (union) "union" else "struct"
private val members = mutableListOf<StructMember>()
val pointerType: CustomTypeSpec by lazy {
val className = ClassName.get(packageName, name)
Expand Down Expand Up @@ -107,8 +108,8 @@ class Struct(
sb.appendLine(
when (it) {
is ValueStructMember -> "/// [VarHandle][#VH_${it.name}] - [Getter][#${it.name}()] - [Setter][#${it.name}(${it.type.carrier})]"
is ByValueStructStructMember -> "/// [Byte offset][#OFFSET_${it.name}] - [Memory layout][#ML_${it.name}] - [Getter][#${it.name}()] - [Setter][#${it.name}(${it.type.carrier})]"
is FixedSizeStructMember -> "/// [Byte offset handle][#MH_${it.name}] - [Memory layout][#ML_${it.name}] - [Getter][#${it.name}(long)] - [Setter][#${it.name}(long, ${it.type.carrier})]"
is ByValueStructStructMember, is FixedSizeStructMember ->
"/// [Byte offset][#OFFSET_${it.name}] - [Memory layout][#ML_${it.name}] - [Getter][#${it.name}()] - [Setter][#${it.name}(${it.type.carrier})]"
}
)
}
Expand All @@ -118,7 +119,7 @@ class Struct(
/// ## Layout
/// [Java definition][#LAYOUT]
/// ```c
/// typedef ${if (union) "union" else "struct"} ${if (cType != null) "$cType " else ""}{
/// typedef $kindString ${if (cType != null) "$cType " else ""}{
""".trimIndent()
)
members.forEach {
Expand All @@ -134,7 +135,7 @@ class Struct(
sb.appendLine(
"""
public final class $name extends ${if (union) "Union" else "Struct"} {
/// The ${if (union) "union" else "struct"} layout of `$cType`.
/// The $kindString layout of `$cType`.
public static final ${if (union) "Union" else "Struct"}Layout LAYOUT = ${if (union) "MemoryLayout.unionLayout" else "LayoutBuilder.struct"}(
""".trimIndent()
)
Expand All @@ -154,23 +155,14 @@ class Struct(
""".trimMargin()
)

is ByValueStructStructMember -> sb.appendLine(
is ByValueStructStructMember, is FixedSizeStructMember -> sb.appendLine(
"""
| /// The byte offset of `${it.name}`.
| public static final long OFFSET_${it.name} = LAYOUT.byteOffset(PathElement.groupElement("${it.name}"));
| /// The memory layout of `${it.name}`.
| public static final MemoryLayout ML_${it.name} = LAYOUT.select(PathElement.groupElement("${it.name}"));
""".trimMargin()
)

is FixedSizeStructMember -> sb.appendLine(
"""
| /// The byte offset handle of `${it.name}` of type `(long baseOffset, long elementIndex)long`.
| public static final MethodHandle MH_${it.name} = LAYOUT.byteOffsetHandle(PathElement.groupElement("${it.name}"), PathElement.sequenceElement());
| /// The memory layout of `${it.name}`.
| public static final MemoryLayout ML_${it.name} = LAYOUT.select(PathElement.groupElement("${it.name}"));
""".trimMargin()
)
}
}
}
Expand Down Expand Up @@ -223,34 +215,49 @@ class Struct(
""".trimMargin()
)

// slice
sb.appendLine(
"""
| /// Creates a slice of `$name`.
| /// @param index the index of the $kindString buffer
| /// @return the slice of `$name`
| public $name asSlice(long index) { return new $name(this.segment().asSlice(LAYOUT.scale(0L, index), LAYOUT)); }
|
| /// Creates a slice of `$name`.
| /// @param index the index of the $kindString buffer
| /// @param count the count
| /// @return the slice of `$name`
| public $name asSlice(long index, long count) { return new $name(this.segment().asSlice(LAYOUT.scale(0L, index), LAYOUT.byteSize() * count)); }
|
""".trimMargin()
)

if (!opaque) {
members.forEach {
// getters
when (it) {
is ValueStructMember, is ByValueStructStructMember -> {
is ValueStructMember, is ByValueStructStructMember, is FixedSizeStructMember -> {
// static
sb.appendLine(
"""
| /// {@return `${it.name}` at the given index}
| /// @param segment the segment of the struct
| /// @param segment the segment of the $kindString
| /// @param index the index
| public static ${it.type.carrierWithC()} get_${it.name}(MemorySegment segment, long index) { ${
when (it) {
is ValueStructMember ->
"return (${it.type.carrier}) VH_${it.name}.get(segment, 0L, index);"
is ByValueStructStructMember ->
is ByValueStructStructMember, is FixedSizeStructMember ->
"""return segment.asSlice(LAYOUT.scale(OFFSET_${it.name}, index), ML_${it.name});"""
else -> error("should not reach here")
}
} }
""".trimMargin()
)
sb.appendLine(
"""
| /// {@return `${it.name}`}
| /// @param segment the segment of the struct
| /// @param segment the segment of the $kindString
| public static ${it.type.carrierWithC()} get_${it.name}(MemorySegment segment) { return $name.get_${it.name}(segment, 0L); }
""".trimMargin()
)
Expand All @@ -269,71 +276,30 @@ class Struct(
""".trimMargin()
)
}

is FixedSizeStructMember -> {
// static
sb.appendLine(
"""
| /// {@return `${it.name}` at the given index}
| /// @param segment the segment of the struct
| /// @param index the index of the struct buffer
| /// @param elementIndex the index of the element
| public static ${it.type.carrierWithC()} get_${it.name}(MemorySegment segment, long index, long elementIndex) {
| try { return segment.asSlice(LAYOUT.scale((long) MH_${it.name}.invokeExact(0L, elementIndex), index), ML_${it.name}); }
| catch (Throwable e) { throw new RuntimeException(e); }
| }
""".trimMargin()
)
sb.appendLine(
"""
| /// {@return `${it.name}`}
| /// @param segment the segment of the struct
| /// @param elementIndex the index of the element
| public static ${it.type.carrierWithC()} get_${it.name}(MemorySegment segment, long elementIndex) { return $name.get_${it.name}(segment, 0L, elementIndex); }
""".trimMargin()
)
// instance
sb.appendLine(
"""
| /// {@return `${it.name}` at the given index}
| /// @param index the index of the struct buffer
| /// @param elementIndex the index of the element
| public ${it.type.carrierWithC()} ${it.name}At(long index, long elementIndex) { return $name.get_${it.name}(this.segment(), index, elementIndex); }
""".trimMargin()
)
sb.appendLine(
"""
| /// {@return `${it.name}`}
| /// @param elementIndex the index of the element
| public ${it.type.carrierWithC()} ${it.name}(long elementIndex) { return $name.get_${it.name}(this.segment(), elementIndex); }
""".trimMargin()
)
}
}

// setters
when (it) {
is ValueStructMember, is ByValueStructStructMember -> {
is ValueStructMember, is ByValueStructStructMember, is FixedSizeStructMember -> {
// static
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value at the given index.
| /// @param segment the segment of the struct
| /// @param segment the segment of the $kindString
| /// @param index the index
| /// @param value the value
| public static void set_${it.name}(MemorySegment segment, long index, ${it.type.carrierWithC()} value) { ${
when (it) {
is ValueStructMember -> "VH_${it.name}.set(segment, 0L, index, value);"
is ByValueStructStructMember -> """MemorySegment.copy(value, 0L, segment, LAYOUT.scale(OFFSET_${it.name}, index), ML_${it.name}.byteSize());"""
else -> error("should not reach here")
is ByValueStructStructMember, is FixedSizeStructMember -> """MemorySegment.copy(value, 0L, segment, LAYOUT.scale(OFFSET_${it.name}, index), ML_${it.name}.byteSize());"""
}
} }
""".trimMargin()
)
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value.
| /// @param segment the segment of the struct
| /// @param segment the segment of the $kindString
| /// @param value the value
| public static void set_${it.name}(MemorySegment segment, ${it.type.carrierWithC()} value) { $name.set_${it.name}(segment, 0L, value); }
""".trimMargin()
Expand All @@ -357,52 +323,6 @@ class Struct(
""".trimMargin()
)
}

is FixedSizeStructMember -> {
// static
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value at the given index.
| /// @param segment the segment of the struct
| /// @param index the index of the struct buffer
| /// @param elementIndex the index of the element
| /// @param value the value
| public static void set_${it.name}(MemorySegment segment, long index, long elementIndex, ${it.type.carrierWithC()} value) {
| try { MemorySegment.copy(value, 0L, segment, LAYOUT.scale((long) MH_${it.name}.invokeExact(0L, elementIndex), index), ML_${it.name}.byteSize()); }
| catch (Throwable e) { throw new RuntimeException(e); }
| }
""".trimMargin()
)
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value.
| /// @param segment the segment of the struct
| /// @param elementIndex the index of the element
| /// @param value the value
| public static void set_${it.name}(MemorySegment segment, long elementIndex, ${it.type.carrierWithC()} value) { $name.set_${it.name}(segment, 0L, elementIndex, value); }
""".trimMargin()
)
// instance
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value at the given index.
| /// @param index the index of the struct buffer
| /// @param elementIndex the index of the element
| /// @param value the value
| /// @return `this`
| public $name ${it.name}At(long index, long elementIndex, ${it.type.carrierWithC()} value) { $name.set_${it.name}(this.segment(), index, elementIndex, value); return this; }
""".trimMargin()
)
sb.appendLine(
"""
| /// Sets `${it.name}` with the given value.
| /// @param elementIndex the index of the element
| /// @param value the value
| /// @return `this`
| public $name ${it.name}(long elementIndex, ${it.type.carrierWithC()} value) { $name.set_${it.name}(this.segment(), elementIndex, value); return this; }
""".trimMargin()
)
}
}

sb.appendLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class VkDowncall(
write: Boolean = true,
action: VkDowncall.() -> Unit
) {
var modifier: String? = null
val imports = mutableSetOf<String>()
val extends = mutableListOf<String>()
val fields = mutableListOf<VkDowncallField>()
Expand Down Expand Up @@ -192,7 +193,11 @@ class VkDowncall(
)
if (packageName != vulkanPackage) sb.appendLine("import overrungl.vulkan.*;")
imports.sorted().forEach { sb.appendLine("import $it;") }
sb.append("public class $className")
sb.append("public ")
if (modifier != null) {
sb.append("$modifier ")
}
sb.append("class $className")
if (extends.isNotEmpty()) {
sb.append(" extends ${extends.joinToString(", ")}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fun main() {
}
}
nameIndex++
if (nameIndex < childNodeList.length - 1) {
if (nameIndex < childNodeList.length) {
// scan fixed size array
val sb = StringBuilder()
for (i2 in nameIndex until childNodeList.length) {
Expand All @@ -297,7 +297,9 @@ fun main() {
}
}
}
fixedSize = sb.split('[', ']')[1]
if (sb.startsWith("[")) {
fixedSize = sb.split('[', ']')[1]
}
}
members.add(VkStructMember(typeComp, memberName!!, fixedSize))
if (fixedSize != null && fixedSize.startsWith("VK_")) {
Expand Down Expand Up @@ -575,6 +577,8 @@ fun main() {

if (featureNumber == "1.0") {
customCode = """
public static final MemorySegment VK_NULL_HANDLE = MemorySegment.NULL;
public static int VK_MAKE_API_VERSION(int variant, int major, int minor, int patch) {
return (variant << 29) | (major << 22) | (minor << 12) | patch;
}
Expand Down Expand Up @@ -671,19 +675,24 @@ fun main() {
addReqEnums(extReqEnums)
addReqCommand(extReqCommands, commandMap, commandAliasMap)

constructor = buildString {
append("public $extName(")
when (extType) {
"device" -> append("""@CType("VkDevice") MemorySegment device""")
"instance" -> append("""@CType("VkInstance") MemorySegment instance""")
}
appendLine(", VKLoadFunc func) {")
extReqCommands.forEach { command ->
append(""" PFN_$command = func.invoke($extType, "$command"""")
commandAliasMap[command]?.onEach { append(""", "$it"""") }
appendLine(");")
if (extReqCommands.isEmpty()){
modifier = "final"
constructor = "private $extName() { }"
}else {
constructor = buildString {
append("public $extName(")
when (extType) {
"device" -> append("""@CType("VkDevice") MemorySegment device""")
"instance" -> append("""@CType("VkInstance") MemorySegment instance""")
}
appendLine(", VKLoadFunc func) {")
extReqCommands.forEach { command ->
append(""" PFN_$command = func.invoke($extType, "$command"""")
commandAliasMap[command]?.onEach { append(""", "$it"""") }
appendLine(");")
}
append("}")
}
append("}")
}
}
extensionDowncalls[rawName] = downcall
Expand Down
Loading

0 comments on commit 2d2e8bd

Please sign in to comment.