-
Notifications
You must be signed in to change notification settings - Fork 164
DirectXOffscreenContext to allow drawing into offscreen texture #1016
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
skiko/src/awtMain/kotlin/org/jetbrains/skiko/graphicapi/DirectXOffscreenContext.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package org.jetbrains.skiko.graphicapi | ||
|
|
||
| import org.jetbrains.skia.BackendRenderTarget | ||
| import org.jetbrains.skia.DirectContext | ||
| import org.jetbrains.skiko.ExperimentalSkikoApi | ||
| import org.jetbrains.skiko.GpuPriority | ||
| import org.jetbrains.skiko.RenderException | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.alignedTextureWidth | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.chooseAdapter | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.createDirectXOffscreenDevice | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.disposeDevice | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.disposeDirectXTexture | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.makeDirectXContext | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.makeDirectXRenderTargetOffScreen | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.makeDirectXTexture | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.readPixels | ||
| import org.jetbrains.skiko.graphicapi.InternalDirectXApi.waitForCompletion | ||
|
|
||
| /** | ||
| * Class that allows drawing into offscreen DirectX texture. | ||
| * | ||
| * Used in the `benchmarks` project: | ||
| * https://github.com/JetBrains/compose-multiplatform/blob/cedd48f99e877f8b936ff574812d573baf231307/benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/MeasureComposable.kt#L16 | ||
| */ | ||
| @ExperimentalSkikoApi | ||
| class DirectXOffscreenContext : AutoCloseable { | ||
| private val adapter = chooseAdapter(GpuPriority.Integrated) | ||
|
|
||
| private val device = createDirectXOffscreenDevice(adapter).also { | ||
| if (it == 0L) { | ||
| throw RenderException("Failed to create DirectX12 device.") | ||
| } | ||
| } | ||
|
|
||
| val directContext = DirectContext(makeDirectXContext(device)) | ||
|
|
||
| override fun close() { | ||
| directContext.close() | ||
| disposeDevice(device) | ||
| } | ||
|
|
||
| inner class Texture(desiredWidth: Int, desiredHeight: Int) : AutoCloseable { | ||
| /** | ||
| * Aligned width/height that is needed for performance optimization, | ||
| * since DirectX uses aligned bytebuffer. | ||
| */ | ||
| val actualWidth = alignedTextureWidth(desiredWidth) | ||
|
|
||
| val actualHeight = desiredHeight | ||
|
|
||
| private val texture = makeDirectXTexture(device, 0, actualWidth, actualHeight).also { | ||
| if (it == 0L) { | ||
| throw RenderException("Can't allocate DirectX resources") | ||
| } | ||
| } | ||
|
|
||
| val backendRenderTarget = BackendRenderTarget(makeDirectXRenderTargetOffScreen(texture)) | ||
|
|
||
| override fun close() { | ||
| backendRenderTarget.close() | ||
| disposeDirectXTexture(texture) | ||
| } | ||
|
|
||
| fun waitForCompletion() { | ||
| waitForCompletion(device, texture) | ||
| } | ||
|
|
||
| fun readPixels(byteArray: ByteArray) { | ||
| if (!readPixels(texture, byteArray)) { | ||
| throw RenderException("Couldn't read pixels") | ||
| } | ||
| } | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
skiko/src/awtMain/kotlin/org/jetbrains/skiko/graphicapi/InternalDirectXApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package org.jetbrains.skiko.graphicapi | ||
|
|
||
| import org.jetbrains.skia.impl.NativePointer | ||
| import org.jetbrains.skiko.GpuPriority | ||
| import org.jetbrains.skiko.GraphicsApi | ||
| import org.jetbrains.skiko.Library | ||
| import org.jetbrains.skiko.hostOs | ||
| import org.jetbrains.skiko.isVideoCardSupported | ||
|
|
||
| internal object InternalDirectXApi { | ||
| init { | ||
| Library.load() | ||
| } | ||
|
|
||
| private external fun getTextureAlignment(): Long | ||
| private val rowBytesAlignment = getTextureAlignment().toInt() | ||
| private val widthSizeAlignment = rowBytesAlignment / 4 | ||
|
|
||
| /** | ||
| * Calculate aligned width/height that is needed for performance optimization, | ||
| * since DirectX uses aligned bytebuffer. | ||
| */ | ||
| fun alignedTextureWidth(width: Int) = if (width % widthSizeAlignment != 0) { | ||
| width + widthSizeAlignment - (width % widthSizeAlignment); | ||
| } else { | ||
| width | ||
| } | ||
|
|
||
| // Called from native code | ||
| private fun isAdapterSupported(name: String) = isVideoCardSupported(GraphicsApi.DIRECT3D, hostOs, name) | ||
|
|
||
| fun chooseAdapter(adapterPriority: GpuPriority): NativePointer = chooseAdapter(adapterPriority.ordinal) | ||
| private external fun chooseAdapter(adapterPriority: Int): NativePointer | ||
| external fun createDirectXOffscreenDevice(adapter: NativePointer): NativePointer | ||
| external fun makeDirectXContext(device: NativePointer): NativePointer | ||
|
|
||
| external fun waitForCompletion(device: NativePointer, texturePtr: NativePointer) | ||
| external fun readPixels(texturePtr: NativePointer, byteArray: ByteArray): Boolean | ||
|
|
||
|
|
||
| /** | ||
| * Provides ID3D12Resource texture taking given [oldTexturePtr] into account | ||
| * since it can be reused if width and height are not changed, | ||
| * or the new one will be created. | ||
| */ | ||
| external fun makeDirectXTexture(device: NativePointer, oldTexturePtr: NativePointer, width: Int, height: Int): NativePointer | ||
| external fun disposeDirectXTexture(texturePtr: NativePointer) | ||
|
|
||
| external fun makeDirectXRenderTargetOffScreen(texturePtr: NativePointer): NativePointer | ||
|
|
||
| external fun disposeDevice(device: NativePointer) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unrelated + breaking compatibility for no reason. Can we avoid it in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed in the test - to pass the graphic API:
https://github.com/JetBrains/skiko/pull/1016/files#diff-f09f8e25b6e613b4acc42736872562fec343ba85cd033a84464e2ee426a1cbd7R188
As for compatibility, we don't support it in Skiko