From 3b4ebcf611e38902be0f2de058419685430fcc20 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Wed, 9 Sep 2026 08:31:31 -0700 Subject: [PATCH] Update Celeritas (#2087) --- .github/scripts/umbra-build.sh | 13 + .github/workflows/umbra-build.yml | 25 + .gitignore | 1 + build.gradle.kts | 86 +-- dependencies.gradle.kts | 8 +- glsm/build.gradle.kts | 22 +- .../angelica/config/SystemProperties.java | 3 + .../gtnewhorizons/angelica/glsm/GLDebug.java | 2 +- .../angelica/glsm/GLStateManager.java | 14 +- .../angelica/glsm/RenderSystem.java | 2 +- .../glsm/backend/Lwjgl2GLRenderBackend.java | 2 +- .../angelica/glsm/hooks/FrameHooks.java | 65 +++ .../angelica/glsm/hooks/GLSMInitConfig.java | 20 +- .../angelica/glsm/hooks/TextureUtilHooks.java | 47 ++ .../angelica/glsm/loading/ClassDump.java | 26 + .../glsm/loading/DependencyVerifier.java | 43 ++ .../glsm/loading/EarlyRedirectorCore.java | 50 +- .../glsm/loading/EcosystemNarrowRules.java | 3 +- .../glsm/loading/Lwjgl3ifyExclusions.java | 19 + .../angelica/glsm/loading/ModRedirector.java | 78 +++ .../glsm/loading/RfbModRedirector.java | 76 +++ .../glsm/texture/TextureInfoCache.java | 19 + .../angelica/glsm/texture/TextureTargets.java | 30 + ...SM_TextureBindingTargetCache_UnitTest.java | 22 + .../GLSM_TextureUploadGeneration_GLTest.java | 15 + .../hooks/GLSMInitConfigDefaultsTest.java | 15 + .../glsm/loading/ModRedirectorTest.java | 127 ++++ .../angelica/glsm/GLSMCoreExtension.java | 7 +- .../angelica/glsm/GLSMExtension.java | 3 + gradle/angelica-run-common.gradle.kts | 68 +++ gradle/angelica-shadow-common.gradle.kts | 28 + gradle/libs.versions.toml | 7 +- scripts/flyby-capture.sh | 27 +- sdl-gpu/build.gradle.kts | 67 ++- .../sdlgpu/glsm/GlsmSdlRedirectAgent.java | 58 ++ .../src/glsmSdlStubs/java/org/lwjgl/Sys.java | 10 + .../org/lwjgl/opengl/ContextCapabilities.java | 31 + .../java/org/lwjgl/opengl/Drawable.java | 8 + .../java/org/lwjgl/opengl/GLContext.java | 12 + .../angelica/sdlgpu/SDLGPUGate.java | 4 +- .../angelica/sdlgpu/SDLGPULWJGLService.java | 60 +- .../angelica/sdlgpu/SDLGPURenderBackend.java | 430 ++++++++++---- .../sdlgpu/device/MemoryStackWrapper.java | 2 +- .../angelica/sdlgpu/frame/ContextState.java | 24 + .../angelica/sdlgpu/frame/FrameManager.java | 6 +- .../sdlgpu/pipeline/DrawDispatch.java | 7 +- .../angelica/sdlgpu/pipeline/FFPTrace.java | 211 +++++++ .../sdlgpu/pipeline/PipelineApplier.java | 25 +- .../sdlgpu/pipeline/PipelineCache.java | 4 + .../sdlgpu/resource/CopyRectClip.java | 71 +++ .../sdlgpu/resource/FBOClearTracker.java | 6 + .../angelica/sdlgpu/resource/PixelOps.java | 6 + .../sdlgpu/resource/ResourceManager.java | 22 +- .../angelica/sdlgpu/resource/TextureOps.java | 81 ++- .../angelica/sdlgpu/shader/ShaderManager.java | 90 ++- .../com.mitchej123.lwjgl.LWJGLService | 1 - .../sdlgpu/DispatchIndirectBindingTest.java | 44 ++ .../sdlgpu/ProxyTextureLimitsTest.java | 154 +++++ .../sdlgpu/ProxyTextureTargetTest.java | 159 +++++ .../sdlgpu/SDLGPULWJGLServiceGatingTest.java | 23 +- .../sdlgpu/glsm/GlsmSdlCopyBlitTest.java | 246 ++++++++ .../sdlgpu/glsm/GlsmSdlFbo0ViewportTest.java | 141 +++++ .../angelica/sdlgpu/glsm/GlsmSdlFfpTest.java | 213 +++++++ .../sdlgpu/glsm/GlsmSdlFrameExtension.java | 22 + .../sdlgpu/glsm/GlsmSdlHeadlessRig.java | 300 ++++++++++ .../glsm/GlsmSdlProxyTextureProbeTest.java | 171 ++++++ .../glsm/GlsmSdlVanillaTextureTest.java | 271 +++++++++ .../sdlgpu/pipeline/FFPTraceTest.java | 31 + .../sdlgpu/resource/CopyRectClipTest.java | 50 ++ .../CopyTexSubImageFromFbo0GpuTest.java | 195 +++++++ .../resource/ReadbackSubmitOrderGpuTest.java | 2 +- .../shader/AttribLocationPatchTest.java | 73 ++- .../AngelicaGLStateManagerService.java | 368 ------------ .../AngelicaRenderSystemService.java | 243 -------- .../angelica/debug/flyby/FlybyRunner.java | 33 +- .../loading/AngelicaClientTweaker.java | 61 +- .../AngelicaRedirectorTransformer.java | 39 +- .../EarlyRedirectorTransformer.java | 7 +- .../transformers/RFBAngelicaRedirector.java | 74 +-- .../loading/shared/AngelicaClassDump.java | 16 +- .../transformers/AngelicaRedirector.java | 42 +- .../gtnewhorizons/angelica/mixins/Mixins.java | 8 - .../interfaces/RenderListManagerAccessor.java | 5 - .../RenderSectionManagerAccessor.java | 3 - .../angelica/profiling/TracyFramePlots.java | 23 +- .../celeritas/AngelicaChunkRenderer.java | 218 +++++-- .../AngelicaRenderSectionManager.java | 42 +- .../celeritas/CeleritasWorldRenderer.java | 25 +- .../celeritas/IndividualDrawBatch.java | 32 ++ .../celeritas/IndividualDrawEmitter.java | 44 -- .../rendering/celeritas/TerrainDrawStats.java | 62 ++ .../celeritas/api/IrisShaderProvider.java | 2 +- .../iris/IrisExtendedChunkVertexEncoder.java | 9 +- .../iris/IrisExtendedChunkVertexType.java | 15 +- .../culling/GpuCulledMultiDrawBatch.java | 46 ++ .../culling/GpuIndirectMultiDrawEmitter.java | 382 ------------ .../rendering/culling/GpuTerrainCuller.java | 543 ++++++++++++++++++ .../rendering/culling/SectionMetaBuffer.java | 8 +- .../SdlShadowVoxelizationSink.java | 2 +- .../voxelization/ShadowVoxelizer.java | 79 ++- .../IrisCeleritasShaderProvider.java | 4 +- .../DeferredWorldRenderingPipeline.java | 4 +- .../iris/pipeline/ShadowRenderer.java | 196 ++++--- .../iris/pipeline/WorldRenderingPipeline.java | 2 +- .../iris/shadows/frustum/BoxCuller.java | 6 + .../frustum/CullEverythingFrustum.java | 5 + .../iris/shadows/frustum/FrustumHolder.java | 9 + .../AdvancedShadowCullingFrustum.java | 62 +- .../advanced/SafeZoneCullingFrustum.java | 59 +- .../frustum/fallback/BoxCullingFrustum.java | 9 + .../frustum/fallback/NonCullingFrustum.java | 5 + .../com.mitchej123.glsm.GLStateManagerService | 1 - .../com.mitchej123.glsm.RenderSystemService | 1 - .../com.mitchej123.lwjgl.LWJGLService | 3 - .../shaders/blocks/block_layer_opaque.fsh | 4 +- .../mixins/early/angelica/MixinMinecraft.java | 17 - .../angelica/MixinMinecraft_FrameHook.java | 44 +- .../glsm/MixinSplashProgressCaching.java | 13 +- .../startup/MixinInitGLStateManager.java | 5 - .../angelica/textures/MixinTextureUtil.java | 42 +- .../terrain/MixinDefaultChunkRenderer.java | 202 ------- .../terrain/MixinRenderListManager.java | 21 - .../terrain/MixinRenderSectionManager.java | 18 - .../MixinSectionRenderDataStorage.java | 13 +- ...faultChunkRenderer_ShadowVoxelization.java | 68 --- .../angelica/rendering/RenderRegionKeys.java | 28 + .../culling/IndirectCullDrawParityTest.java | 408 +++++++------ .../culling/SectionMetaBufferTest.java | 44 +- .../culling/SectionMetaSlotLifecycleTest.java | 22 +- .../voxelization/ShadowVoxelizerTest.java | 206 ++++--- .../iris/shadows/frustum/BoxCullerTest.java | 86 +++ .../frustum/SafeZoneFrustumReuseTest.java | 55 ++ .../ShadowFrustumIntersectAabTest.java | 158 +++++ .../frustum/ShadowFrustumTestSupport.java | 44 ++ .../client/renderer/culling/Frustrum.java | 24 + .../data/SectionRenderDataLayoutTest.java | 320 +++++++++++ .../chunk/multidraw/RegionCullMaskTest.java | 93 +++ .../lwjgl/HeadlessTestLWJGLService.java | 56 +- .../com.mitchej123.lwjgl.LWJGLService | 1 - umbra/build.gradle.kts | 31 +- umbra/buildSrc/build.gradle.kts | 9 + umbra/dependencies.gradle.kts | 3 +- umbra/settings.gradle.kts | 3 + .../rendering/UmbraGLStateManagerService.java | 115 ---- .../rendering/UmbraRenderSystemService.java | 75 --- .../umbra/loading/UmbraClientTweaker.java | 46 +- .../EarlyRedirectorTransformer.java | 6 +- .../UmbraRedirectorTransformer.java | 37 +- .../rfb/transformers/RFBUmbraRedirector.java | 65 +-- .../umbra/loading/shared/UmbraClassDump.java | 25 - .../shared/transformers/UmbraRedirector.java | 38 +- .../gtnewhorizons/umbra/mixins/Mixins.java | 17 +- .../com.mitchej123.glsm.GLStateManagerService | 1 - .../com.mitchej123.glsm.RenderSystemService | 1 - .../com.mitchej123.lwjgl.LWJGLService | 2 - .../ffp/MixinTessellator_CoreProfile.java | 12 +- .../early/glsm/MixinGameSettings_VSync.java | 16 + .../early/glsm/MixinMinecraft_FrameHook.java | 43 +- .../glsm/MixinMinecraft_IconifyGuard.java | 24 + .../early/glsm/MixinMinecraft_VSync.java | 16 + .../glsm/MixinSplashProgressCaching.java | 13 +- .../MixinForgeHooksClient_SDLGPUDisplay.java | 45 ++ .../sdlgpu/MixinMinecraft_SDLGPUIcons.java | 19 + .../startup/MixinInitGLStateManager.java | 15 +- .../early/textures/MixinTextureUtil.java | 42 ++ 165 files changed, 6776 insertions(+), 3036 deletions(-) create mode 100755 .github/scripts/umbra-build.sh create mode 100644 .github/workflows/umbra-build.yml create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/FrameHooks.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/TextureUtilHooks.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ClassDump.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/Lwjgl3ifyExclusions.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirector.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/RfbModRedirector.java create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureTargets.java create mode 100644 glsm/src/test/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfigDefaultsTest.java create mode 100644 glsm/src/test/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirectorTest.java create mode 100644 gradle/angelica-run-common.gradle.kts create mode 100644 gradle/angelica-shadow-common.gradle.kts create mode 100644 sdl-gpu/src/glsmSdlAgent/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlRedirectAgent.java create mode 100644 sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/Sys.java create mode 100644 sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/ContextCapabilities.java create mode 100644 sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/Drawable.java create mode 100644 sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/GLContext.java create mode 100644 sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTrace.java create mode 100644 sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClip.java delete mode 100644 sdl-gpu/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/DispatchIndirectBindingTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureLimitsTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureTargetTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlCopyBlitTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFbo0ViewportTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFfpTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFrameExtension.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlHeadlessRig.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlProxyTextureProbeTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlVanillaTextureTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTraceTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClipTest.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyTexSubImageFromFbo0GpuTest.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaGLStateManagerService.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaRenderSystemService.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderListManagerAccessor.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/IndividualDrawBatch.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/IndividualDrawEmitter.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuCulledMultiDrawBatch.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuIndirectMultiDrawEmitter.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuTerrainCuller.java delete mode 100644 src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService delete mode 100644 src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService delete mode 100644 src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService delete mode 100644 src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinDefaultChunkRenderer.java delete mode 100644 src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderListManager.java delete mode 100644 src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sdlgpu/MixinDefaultChunkRenderer_ShadowVoxelization.java create mode 100644 src/test/java/com/gtnewhorizons/angelica/rendering/RenderRegionKeys.java create mode 100644 src/test/java/net/coderbot/iris/shadows/frustum/BoxCullerTest.java create mode 100644 src/test/java/net/coderbot/iris/shadows/frustum/SafeZoneFrustumReuseTest.java create mode 100644 src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumIntersectAabTest.java create mode 100644 src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumTestSupport.java create mode 100644 src/test/java/net/minecraft/client/renderer/culling/Frustrum.java create mode 100644 src/test/java/org/embeddedt/embeddium/impl/render/chunk/data/SectionRenderDataLayoutTest.java create mode 100644 src/test/java/org/embeddedt/embeddium/impl/render/chunk/multidraw/RegionCullMaskTest.java rename src/test/java/{com/mitchej123 => org/taumc/celeritas}/lwjgl/HeadlessTestLWJGLService.java (92%) delete mode 100644 src/test/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService delete mode 100644 umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraGLStateManagerService.java delete mode 100644 umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraRenderSystemService.java delete mode 100644 umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/UmbraClassDump.java delete mode 100644 umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService delete mode 100644 umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService delete mode 100644 umbra/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinGameSettings_VSync.java create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_IconifyGuard.java create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_VSync.java create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinForgeHooksClient_SDLGPUDisplay.java create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinMinecraft_SDLGPUIcons.java create mode 100644 umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/textures/MixinTextureUtil.java diff --git a/.github/scripts/umbra-build.sh b/.github/scripts/umbra-build.sh new file mode 100755 index 000000000..36b2bcc6c --- /dev/null +++ b/.github/scripts/umbra-build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +cd "$REPO_ROOT/umbra" + +gradle_args=(:compileJava :compileMixinJava :test --no-daemon --console=plain --stacktrace) +if [[ -n "${UMBRA_GRADLE_ARGS:-}" ]]; then + read -ra extra_args <<< "$UMBRA_GRADLE_ARGS" + gradle_args+=("${extra_args[@]}") +fi + +sh ./gradlew "${gradle_args[@]}" diff --git a/.github/workflows/umbra-build.yml b/.github/workflows/umbra-build.yml new file mode 100644 index 000000000..980c2f1cb --- /dev/null +++ b/.github/workflows/umbra-build.yml @@ -0,0 +1,25 @@ +name: Umbra build + +on: + pull_request: + branches: [ master, main ] + push: + branches: [ master, main ] + +jobs: + umbra-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: actions/setup-java@v5 + with: + distribution: zulu + java-version: | + 21 + 25 + - uses: gradle/actions/setup-gradle@v5 + with: + validate-wrappers: true + - run: .github/scripts/umbra-build.sh diff --git a/.gitignore b/.gitignore index dc18ae0c6..62b944c87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .gradle .kotlin/ +.lwjgl/ .settings /.idea/ /.vs/ diff --git a/build.gradle.kts b/build.gradle.kts index 8e5fd5260..fa2849bef 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,19 +1,16 @@ import com.modrinth.minotaur.TaskModrinthUpload import net.darkhax.curseforgegradle.TaskPublishCurseForge -import xyz.wagyourtail.jvmdg.gradle.flags.DowngradeFlags -import xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar import xyz.wagyourtail.jvmdg.gradle.task.files.DowngradeFiles plugins { id("com.gtnewhorizons.gtnhconvention") } -tasks.withType().configureEach { logLevel.set("FATAL") } -tasks.withType().configureEach { logLevel.set("FATAL") } val lwjglDebug = false val gpuHud = false val renderdoc = false val rgp = false +extra["lwjglDebug"] = lwjglDebug minecraft { extraRunJvmArguments.add("-Dangelica.debug.testBlocks=true") @@ -24,24 +21,13 @@ minecraft { extraRunJvmArguments.add("-Dsun.net.client.defaultConnectTimeout=5000") extraRunJvmArguments.add("-Dsun.net.client.defaultReadTimeout=5000") - // TODO: Remove once GTNHGradle includes shaderc/spvc in the default lwjgl3Bindings list - lwjgl3Bindings.addAll("shaderc", "spvc") - lwjgl3Version = libs.versions.lwjgl3.get() // extraRunJvmArguments.addAll("-Dlegacy.debugClassLoadingSave=true") // extraRunJvmArguments.addAll("-Drfb.dumpLoadedClasses=true", "-Drfb.dumpLoadedClassesPerTransformer=true") //extraRunJvmArguments.add("-Dangelica.debug.redirectorLogspam=true") } -// Forward -Dangelica.* from the gradle invocation to the client -tasks.withType().matching { it.name.startsWith("runClient") }.configureEach { - for ((key, value) in System.getProperties()) { - val name = key.toString() - if (name.startsWith("angelica.")) { - jvmArgs("-D$name=$value") - } - } -} +apply(from = "gradle/angelica-run-common.gradle.kts") if (gpuHud) { tasks.withType().matching { it.name.startsWith("runClient") }.configureEach { @@ -57,47 +43,12 @@ if (gpuHud) { configurations.all { exclude(group = "com.github.GTNewHorizons", module = "Angelica") - resolutionStrategy.dependencySubstitution { - substitute(module("com.github.GTNewHorizons:lwjgl3ify")) - .using(module("com.github.GTNewHorizons:lwjgl3ify:${libs.versions.lwjgl3ify.get()}")) - } -} - -// lwjgl3ify pulls a pinned lwjgl 3.4.2 snapshot transitively from here. -repositories { - maven { - name = "Maven Central Snapshots" - url = uri("https://central.sonatype.com/repository/maven-snapshots/") - content { includeGroup("org.lwjgl") } - } } val osName = System.getProperty("os.name").lowercase() val isMacOs = org.gradle.internal.os.OperatingSystem.current().isMacOsX val isMacOsArm64 = isMacOs && System.getProperty("os.arch") == "aarch64" -tasks.withType().configureEach { - if (name.startsWith("runClient") && name != "runClient") { - if (isMacOs) { - // SDL3 / Cocoa / Metal must initialize on the JVM main thread. - jvmArgs("-XstartOnFirstThread") - jvmArgs("-Dangelica.sdlgpu.encoderAssertions=" + if (lwjglDebug) "fatal" else "warn") - if (lwjglDebug) { - environment("METAL_DEVICE_WRAPPER_TYPE", "1") - environment("METAL_DEBUG_ERROR_MODE", "0") - environment("MTL_SHADER_VALIDATION", "1") - environment("MTL_SHADER_VALIDATION_REPORT_TO_STDERR", "1") - environment("MTL_DEBUG_LAYER", "1") - environment("MallocScribble", "1") - environment("MallocPreScribble", "1") - environment("MallocGuardEdges", "1") - environment("MallocStackLogging", "1") - environment("MallocStackLoggingNoCompact", "1") - } - } - } -} - // Linux runClient: RenderDoc frame capture (F12) OR RADV RGP/SQTT capture. Mutually exclusive; RGP wins tasks.withType().configureEach { if (name.startsWith("runClient") && !isMacOs && !osName.contains("windows")) { @@ -126,14 +77,6 @@ tasks.withType().configureEach { } } -tasks.processResources { - val projectVersion = project.version.toString() - inputs.property("version", projectVersion) - filesMatching("META-INF/rfb-plugin/*") { - expand("version" to projectVersion) - } -} - tasks.register("copyDependencies") { group = "Angelica" description = "Collect dependencies into the testDependencies folder" @@ -206,6 +149,7 @@ fun Test.configureAngelicaJava8() { tasks.test { useJUnitPlatform { excludeTags = setOf("gl-core") } configureAngelicaJava8() + jvmArgs("-Dceleritas.lwjglService=org.taumc.celeritas.lwjgl.HeadlessTestLWJGLService") dependsOn(":glsm:classes", ":lwjgl3-backend:classes", ":sdl-gpu:classes") } @@ -233,29 +177,7 @@ val glCoreTest by tasks.registering(Test::class) { tasks.test { finalizedBy(glCoreTest) } -tasks.shadowJar { - dependsOn(embedOnly) - from(embedOnly.map(::zipTree)) - - duplicatesStrategy = DuplicatesStrategy.INCLUDE - mergeServiceFiles() - - minimize { - exclude(project(rootProject.path)) - exclude(project(":glsm")) - exclude(project(":lwjgl3-backend")) - exclude(project(":sdl-gpu")) - exclude(dependency("org.taumc:.*:.*")) - exclude(dependency("org.antlr:.*:.*")) - } - - relocate("com.mitchej123", "com.mitchej123") - relocate("org.embeddedt", "org.embeddedt") - relocate("com.gtnewhorizons.angelica.config", "com.gtnewhorizons.angelica.config") - relocate("com.gtnewhorizons.angelica.glsm", "com.gtnewhorizons.angelica.glsm") - relocate("com.gtnewhorizons.angelica.lwjgl3", "com.gtnewhorizons.angelica.lwjgl3") - relocate("com.gtnewhorizons.angelica.sdlgpu", "com.gtnewhorizons.angelica.sdlgpu") -} +apply(from = "gradle/angelica-shadow-common.gradle.kts") tasks.withType().configureEach { uploadArtifacts.forEach { it.addGameVersion("Client") } diff --git a/dependencies.gradle.kts b/dependencies.gradle.kts index 699ad6ac3..ea1827ef5 100644 --- a/dependencies.gradle.kts +++ b/dependencies.gradle.kts @@ -52,8 +52,8 @@ configurations.configureEach { } DependencyHandlerScope.of(dependencies).apply { - compileOnly("com.github.GTNewHorizons:Hodgepodge:2.7.187:dev") - runtimeOnlyNonPublishable("com.github.GTNewHorizons:Hodgepodge:2.7.187:dev") + compileOnly("com.github.GTNewHorizons:Hodgepodge:2.7.197:dev") + runtimeOnlyNonPublishable("com.github.GTNewHorizons:Hodgepodge:2.7.197:dev") runtimeOnlyNonPublishable(rfg.deobf("curse.maven:biomes-o-plenty-220318:2499612")) @@ -104,7 +104,6 @@ DependencyHandlerScope.of(dependencies).apply { // Celeritas (isTransitive=false: deps provided by GTNHLib/runtime) embedOnly(libs.celeritas.common) { isTransitive = false } - embedOnly(libs.celeritas.lwjgl2.service) { isTransitive = false } // Because who doesn't want NEI devOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.8.109-GTNH:dev") @@ -177,7 +176,7 @@ DependencyHandlerScope.of(dependencies).apply { compileOnly("com.github.GTNewHorizons:BetterCrashes:1.4.5-GTNH:dev") // Distant Horizons - compileOnly("maven.modrinth:DistantHorizonsApi:5.1.0") + compileOnly(libs.distant.horizons.api) compileOnly("mega:fluidlogged-mc1.7.10:0.1.2") @@ -208,5 +207,6 @@ afterEvaluate { testRuntimeOnly(libs.fastutil) testRuntimeOnly(libs.joml) + testImplementation(libs.distant.horizons.api) } } diff --git a/glsm/build.gradle.kts b/glsm/build.gradle.kts index 516e13d20..d732afb70 100644 --- a/glsm/build.gradle.kts +++ b/glsm/build.gradle.kts @@ -46,6 +46,27 @@ tasks.withType().configureEach { exclude("net/minecraft/**") } +val stubsElements by configurations.creating { + isCanBeConsumed = true + isCanBeResolved = false + attributes { + attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, Usage.JAVA_RUNTIME)) + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category::class.java, Category.LIBRARY)) + attribute(rfgObfAttr, "mcp") + attribute(rfgTransformedAttr, true) + } + outgoing { + capability("${project.group}:glsm-stubs:${project.version}") + } +} + +artifacts { + add(stubsElements.name, sourceSets["stubs"].java.classesDirectory) { + type = "java-classes-directory" + builtBy(tasks.named("stubsClasses")) + } +} + repositories { maven { name = "Mojang" @@ -141,7 +162,6 @@ dependencies { testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.log4j.core) testImplementation(libs.celeritas.common) { isTransitive = false } - testRuntimeOnly(libs.celeritas.lwjgl2.service) { isTransitive = false } testRuntimeOnly(libs.jvmdowngrader.java.api) { artifact { classifier = "downgraded-8" } } testRuntimeOnly(libs.commons.lang3) } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java b/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java index 6b347a80d..6962ee560 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/config/SystemProperties.java @@ -52,6 +52,7 @@ public final class SystemProperties { public static final int FLYBY_LENGTH = Integer.getInteger("angelica.flyby.length", 0); // Blocks for moving routes, ticks for stationary public static final double FLYBY_SPEED = parseDouble("angelica.flyby.speed"); // Travel speed in blocks per tick public static final boolean FLYBY_EXIT_WHEN_DONE = Boolean.getBoolean("angelica.flyby.exitWhenDone"); + public static final long FLYBY_TIME_OF_DAY = Math.floorMod(Long.getLong("angelica.flyby.timeOfDay", 6000L), 24000L); // Debug public static final boolean LWJGL_DEBUG = Boolean.getBoolean("org.lwjgl.util.Debug"); @@ -62,12 +63,14 @@ public final class SystemProperties { private static final boolean DUMP_SHADERS_PROP = Boolean.getBoolean("angelica.debug.dumpShaders"); public static final boolean REDIRECTOR_LOGSPAM = Boolean.getBoolean("angelica.debug.redirectorLogspam"); public static final boolean DEBUG_DISPLAY_LISTS = Boolean.getBoolean("angelica.debug.displayLists"); + public static final boolean FFP_TRACE = Boolean.getBoolean("angelica.debug.ffpTrace"); public static final boolean LOG_DISPLAY_LIST_COMPILATION = Boolean.getBoolean("angelica.debug.displayLists.compilation"); public static final boolean FORCE_ORPHAN_STREAMING = Boolean.getBoolean("angelica.debug.forceOrphanStreaming"); public static final String SHADER_DUMP_ROOT = "angelica_dumps"; // Set by us, read by celeritas public static final String KEY_CELERITAS_ENABLE_GL_DEBUG = "celeritas.enableGLDebug"; + public static final String KEY_CELERITAS_LWJGL_SERVICE = "celeritas.lwjglService"; public static boolean dumpShaders() { return DUMP_SHADERS_PROP || isDeobf(); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLDebug.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLDebug.java index b5a214889..3c8954074 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLDebug.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLDebug.java @@ -564,7 +564,7 @@ public final class GLDebug { /** * Overload that lets you gate the String concat behind the debugState */ - public static void pushGroup(String prefix, int value) { + public static void pushGroup(String prefix, long value) { if (debugState != null && Thread.currentThread() == GLStateManager.getMainThread()) { debugState.pushGroup(prefix + value); } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index f167305b0..bada36827 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -493,7 +493,9 @@ public class GLStateManager { public static void initialize(GLSMInitConfig config) { initConfig = config; - preInit(config.getDisplayWidth(), config.getDisplayHeight()); + final int width = config.getDisplayWidth() > 0 ? config.getDisplayWidth() : Display.getWidth(); + final int height = config.getDisplayHeight() > 0 ? config.getDisplayHeight() : Display.getHeight(); + preInit(width, height); init(config.getPostInitCallback()); } @@ -2003,6 +2005,7 @@ public class GLStateManager { } public static void glBindTexture(int target, int texture) { + if (TextureInfoCache.isProxyTarget(target)) return; final GLContextState glCtx = ctx(); final RecordMode mode = DisplayListManager.getRecordMode(); if (mode != RecordMode.NONE) { @@ -4576,6 +4579,11 @@ public class GLStateManager { }; } + private static int mipLevelSize(int base, int level) { + if (base <= 0 || level < 0 || level >= Integer.SIZE - 1) return 1; + return Math.max(base >> level, 1); + } + public static int glGetTexLevelParameteri(int target, int level, int pname) { if (target != GL11.GL_TEXTURE_2D || !isCachingEnabled()) { return RENDER_BACKEND.getTexLevelParameteri(target, level, pname); @@ -4590,8 +4598,8 @@ public class GLStateManager { return RENDER_BACKEND.getTexLevelParameteri(target, level, pname); } return switch (pname) { - case GL11.GL_TEXTURE_WIDTH -> Math.max(info.getWidth() >> level, 1); - case GL11.GL_TEXTURE_HEIGHT -> Math.max(info.getHeight() >> level, 1); + case GL11.GL_TEXTURE_WIDTH -> mipLevelSize(info.getWidth(), level); + case GL11.GL_TEXTURE_HEIGHT -> mipLevelSize(info.getHeight(), level); case GL11.GL_TEXTURE_INTERNAL_FORMAT -> { if (info.needsInternalFormatResolve() && isRecordingDisplayList()) { throw new IllegalStateException(String.format("glGetTexLevelParameteri(GL_TEXTURE_INTERNAL_FORMAT) needs to resolve a generic compressed format for texture %d during display list recording", getBoundTextureForServerState())); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/RenderSystem.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/RenderSystem.java index 0f43dbf9a..5262d2772 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/RenderSystem.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/RenderSystem.java @@ -9,7 +9,7 @@ import com.gtnewhorizons.angelica.glsm.dsa.DSAEXT; import com.gtnewhorizons.angelica.glsm.dsa.DSAUnsupported; import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; import com.gtnewhorizons.angelica.glsm.texture.TextureInfoCache; -import com.mitchej123.lwjgl.LWJGLServiceProvider; +import org.taumc.celeritas.lwjgl.LWJGLServiceProvider; import lombok.Getter; import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java index 05d110329..bb4993c3f 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/backend/Lwjgl2GLRenderBackend.java @@ -40,7 +40,7 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; -import static com.mitchej123.lwjgl.LWJGLServiceProvider.LWJGL; +import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; /** * LWJGL2 GL implementation of {@link RenderBackend}. diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/FrameHooks.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/FrameHooks.java new file mode 100644 index 000000000..6111510f9 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/FrameHooks.java @@ -0,0 +1,65 @@ +package com.gtnewhorizons.angelica.glsm.hooks; + +import com.gtnewhorizons.angelica.glsm.CaptureGate; +import com.gtnewhorizons.angelica.glsm.DisplayListManager; +import com.gtnewhorizons.angelica.glsm.GLDebug; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; + +import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND; + +public final class FrameHooks { + + private FrameHooks() {} + + private static boolean gameLoopStarted; + private static long frameCounter; + private static boolean frameGroupActive; + + public static void bootstrapFirstFrame() { + DisplayListManager.abortIfLeaked(); + if (!gameLoopStarted) { + gameLoopStarted = true; + RENDER_BACKEND.onFrameBegin(); + pushFrameGroup(); + } + } + + public static void frameEnd() { + popFrameGroup(); + RENDER_BACKEND.onFrameEnd(); + } + + public static void frameBegin() { + if (gameLoopStarted) { + RENDER_BACKEND.onFrameBegin(); + pushFrameGroup(); + } + } + + public static void shutdown() { + BackendManager.shutdown(); + } + + public static void bindSplashVao() { + GLStateManager.glBindVertexArray(GLStateManager.glGenVertexArrays()); + } + + public static void splashFinished() { + GLStateManager.markSplashComplete("SplashProgress.finish"); + } + + private static void pushFrameGroup() { + CaptureGate.refresh(); + GLDebug.pushGroup("frame:", frameCounter); + frameGroupActive = true; + } + + private static void popFrameGroup() { + if (frameGroupActive) { + GLDebug.popGroup(); + frameGroupActive = false; + frameCounter++; + } + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfig.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfig.java index e820e922d..bf1e7782f 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfig.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfig.java @@ -1,7 +1,9 @@ package com.gtnewhorizons.angelica.glsm.hooks; import com.gtnewhorizon.gtnhlib.client.renderer.DirectTessellator; +import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.streaming.StreamingUploader; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import lombok.Getter; import java.util.function.Consumer; @@ -14,7 +16,7 @@ public final class GLSMInitConfig { @Getter private final int displayWidth; @Getter private final int displayHeight; @Getter private final Runnable postInitCallback; - @Getter private final boolean DSAEnabled; + private final boolean dsaEnabled; @Getter private final boolean noErrorChecks; private GLSMInitConfig(Builder builder) { @@ -26,7 +28,11 @@ public final class GLSMInitConfig { this.displayWidth = builder.displayWidth; this.displayHeight = builder.displayHeight; this.postInitCallback = builder.postInitCallback; - this.DSAEnabled = builder.enableDSA; + this.dsaEnabled = builder.dsaEnabled; + } + + public boolean isDSAEnabled() { + return dsaEnabled; } public static Builder builder() { @@ -34,14 +40,14 @@ public final class GLSMInitConfig { } public static final class Builder { - private boolean lwjglDebug = false; + private boolean lwjglDebug = SystemProperties.LWJGL_DEBUG; private StreamingUploader.UploadStrategy streamingUploadStrategy = StreamingUploader.UploadStrategy.BUFFER_DATA; - private Consumer directDrawer = null; - private Runnable streamingDrawerDestroy = null; + private Consumer directDrawer = TessellatorStreamingDrawer::drawDirect; + private Runnable streamingDrawerDestroy = TessellatorStreamingDrawer::destroy; private int displayWidth = 0; private int displayHeight = 0; private Runnable postInitCallback = null; - private boolean enableDSA = false; + private boolean dsaEnabled = true; private boolean noErrorChecks; private Builder() {} @@ -83,7 +89,7 @@ public final class GLSMInitConfig { } public Builder enableDSA(boolean enableDSA) { - this.enableDSA = enableDSA; + this.dsaEnabled = enableDSA; return this; } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/TextureUtilHooks.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/TextureUtilHooks.java new file mode 100644 index 000000000..8b4b08f01 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/hooks/TextureUtilHooks.java @@ -0,0 +1,47 @@ +package com.gtnewhorizons.angelica.glsm.hooks; + +import com.gtnewhorizons.angelica.glsm.DisplayListManager; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.RenderSystem; +import org.lwjgl.opengl.EXTTextureFilterAnisotropic; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; + +public final class TextureUtilHooks { + + private TextureUtilHooks() {} + + private static int savedFilterTextureId; + + public static void setMaxLevel(int mipmapLevels) { + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, mipmapLevels); + } + + public static void captureBoundFilterTexture() { + savedFilterTextureId = GLStateManager.getBoundTextureForServerState(); + } + + public static void clearSavedFilterTexture() { + savedFilterTextureId = 0; + } + + public static void restoreFilter(int min, int mag) { + final int id = savedFilterTextureId; + if (id != 0 && DisplayListManager.getRecordMode() == DisplayListManager.RecordMode.NONE) { + RenderSystem.texParameteri(id, GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min); + RenderSystem.texParameteri(id, GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag); + } else { + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag); + } + } + + public static void restoreAniso(float aniso) { + final int id = savedFilterTextureId; + if (id != 0 && DisplayListManager.getRecordMode() == DisplayListManager.RecordMode.NONE) { + RenderSystem.texParameterf(id, GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); + } else { + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); + } + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ClassDump.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ClassDump.java new file mode 100644 index 000000000..69a20173b --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ClassDump.java @@ -0,0 +1,26 @@ +package com.gtnewhorizons.angelica.glsm.loading; + +import com.gtnewhorizon.gtnhlib.asm.ASMUtil; +import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; + +public final class ClassDump { + + public static final ClassDump DISABLED = new ClassDump(false); + + private final boolean enabled; + + public ClassDump(boolean enabled) { + this.enabled = enabled; + } + + public void dumpClass(String className, byte[] originalBytes, byte[] transformedBytes, Object transformer) { + if (!enabled) return; + ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); + ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); + } + + public void dumpRFBClass(String className, ClassNodeHandle classNode, Object transformer) { + if (!enabled) return; + dumpClass(className, classNode.getOriginalBytes(), classNode.computeBytes(), transformer); + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/DependencyVerifier.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/DependencyVerifier.java index 82ef4d843..3f17c1f09 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/DependencyVerifier.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/DependencyVerifier.java @@ -1,5 +1,10 @@ package com.gtnewhorizons.angelica.glsm.loading; +import net.minecraft.launchwrapper.Launch; +import org.apache.logging.log4j.Logger; + +import javax.swing.JOptionPane; +import java.awt.GraphicsEnvironment; import java.util.List; /** @@ -15,6 +20,10 @@ public final class DependencyVerifier { */ public record Check(String resourcePath, String errorMessage) {} + public interface FatalDialog { + void show(String title, String message); + } + /** * Verify all checks pass. Throws RuntimeException on first failure. * @param anchor Class whose classloader to use for resource lookups @@ -28,5 +37,39 @@ public final class DependencyVerifier { } } + public static List gtnhLibChecks(String productName) { + return List.of( + new Check("/it/unimi/dsi/fastutil/ints/Int2ObjectMap.class", "Missing dependency: " + productName + " requires GTNHLib! Download: https://modrinth.com/mod/gtnhlib"), + new Check("/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.class", "GTNHLib is outdated: " + productName + " requires GTNHLib 0.10.0 or newer! Download: https://modrinth.com/mod/gtnhlib"), + new Check("/it/unimi/dsi/fastutil/objects/ObjectBooleanBiConsumer.class", "GTNHLib is outdated: " + productName + " requires GTNHLib 0.11.19 or newer (fastutil 8.5.18+)! Download: https://modrinth.com/mod/gtnhlib")); + } + + public static void verifyOrHalt(Class anchor, String productName, List checks, Logger logger, FatalDialog sdlDialog) { + final String message; + try { + verify(anchor, checks); + return; + } catch (RuntimeException ex) { + message = ex.getMessage(); + } + + final String title = productName + " - Missing Dependency"; + System.err.println("FATAL: " + message); + try { + logger.fatal(message); + } catch (Throwable ignored) {} + + final int lwjgl3ifyMajor = ((Integer) Launch.blackboard.getOrDefault("lwjgl3ify:major-version", Integer.MIN_VALUE)); + try { + if (lwjgl3ifyMajor >= 3) { + sdlDialog.show(title, message); + } else if (!GraphicsEnvironment.isHeadless()) { + JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE); + } + } catch (Throwable ignored) {} + + Runtime.getRuntime().halt(1); + } + private DependencyVerifier() {} } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EarlyRedirectorCore.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EarlyRedirectorCore.java index 4d87d8107..080729b94 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EarlyRedirectorCore.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EarlyRedirectorCore.java @@ -1,54 +1,8 @@ package com.gtnewhorizons.angelica.glsm.loading; -import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public final class EarlyRedirectorCore { - - private final GLSMRedirector core; - private final String[] targets; - private final String[] exclusions; +public final class EarlyRedirectorCore extends ModRedirector { public EarlyRedirectorCore(String... extraExclusions) { - this.core = new GLSMRedirector(); - this.targets = EcosystemNarrowRules.EARLY_REDIRECTOR_TARGETS.clone(); - - final List excl = new ArrayList<>(Arrays.asList(core.getCoreExclusions())); - if (extraExclusions != null && extraExclusions.length > 0) { - Collections.addAll(excl, extraExclusions); - } - this.exclusions = excl.toArray(new String[0]); - } - - public byte[] transform(String transformedName, byte[] basicClass) { - if (basicClass == null) return null; - - if (!startsWithAny(transformedName, targets)) return basicClass; - if (startsWithAny(transformedName, exclusions)) return basicClass; - if (!core.shouldTransform(basicClass)) return basicClass; - - final ClassReader cr = new ClassReader(basicClass); - final ClassNode cn = new ClassNode(); - cr.accept(cn, 0); - if (core.transformClassNode(transformedName, cn)) { - final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - return cw.toByteArray(); - } - return basicClass; - } - - private static boolean startsWithAny(String name, String[] prefixes) { - for (String p : prefixes) { - if (name.startsWith(p)) return true; - } - return false; + super(ClassDump.DISABLED, EcosystemNarrowRules.EARLY_REDIRECTOR_TARGETS.clone(), extraExclusions); } } diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EcosystemNarrowRules.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EcosystemNarrowRules.java index 9bbb9d1e8..ee6e68440 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EcosystemNarrowRules.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/EcosystemNarrowRules.java @@ -91,9 +91,8 @@ public final class EcosystemNarrowRules { public static final String[] LWJGL3IFY_EXCLUSIONS_SHARED = { "org.embeddedt.embeddium", "org.taumc.celeritas", - "com.mitchej123.lwjgl.lwjgl3", - "com.mitchej123.glsm", "com.gtnewhorizons.angelica.lwjgl3", + "com.gtnewhorizons.angelica.sdlgpu", }; public static final String[] EARLY_REDIRECTOR_TARGETS = { diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/Lwjgl3ifyExclusions.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/Lwjgl3ifyExclusions.java new file mode 100644 index 000000000..4df8d5fe4 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/Lwjgl3ifyExclusions.java @@ -0,0 +1,19 @@ +package com.gtnewhorizons.angelica.glsm.loading; + +import com.gtnewhorizons.retrofuturabootstrap.SharedConfig; + +public final class Lwjgl3ifyExclusions { + + public static void apply() { + final var handle = SharedConfig.getRfbTransformers().stream() + .filter(transformer -> transformer.id().equals("lwjgl3ify:redirect")) + .findFirst() + .orElse(null); + if (handle == null) return; + for (String exclusion : EcosystemNarrowRules.LWJGL3IFY_EXCLUSIONS_SHARED) { + handle.exclusions().add(exclusion); + } + } + + private Lwjgl3ifyExclusions() {} +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirector.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirector.java new file mode 100644 index 000000000..6a7dabd44 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirector.java @@ -0,0 +1,78 @@ +package com.gtnewhorizons.angelica.glsm.loading; + +import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class ModRedirector { + + private final GLSMRedirector core = new GLSMRedirector(); + private final ClassDump dump; + private final String[] targets; + private final String[] allExclusions; + + public ModRedirector(ClassDump dump, String... extraExclusions) { + this(dump, (String[]) null, extraExclusions); + } + + public ModRedirector(ClassDump dump, String[] targets, String[] extraExclusions) { + this.dump = dump; + this.targets = targets; + final List exclusions = new ArrayList<>(Arrays.asList(core.getCoreExclusions())); + exclusions.add("com.gtnewhorizons.angelica.lwjgl3."); + Collections.addAll(exclusions, extraExclusions); + this.allExclusions = exclusions.toArray(new String[0]); + } + + public ClassDump getDump() { + return dump; + } + + public String[] getTransformerExclusions() { + return allExclusions; + } + + public boolean shouldTransform(byte[] basicClass) { + return core.shouldTransform(basicClass); + } + + public boolean transformClassNode(String transformedName, ClassNode cn, boolean lwjgl3Aware) { + return core.transformClassNode(transformedName, cn, lwjgl3Aware); + } + + public byte[] transform(String transformedName, byte[] basicClass, Object transformer) { + if (basicClass == null) return null; + + if (targets != null && !startsWithAny(transformedName, targets)) return basicClass; + if (startsWithAny(transformedName, allExclusions)) return basicClass; + + if (!core.shouldTransform(basicClass)) { + return basicClass; + } + + final ClassReader cr = new ClassReader(basicClass); + final ClassNode cn = new ClassNode(); + cr.accept(cn, 0); + if (!core.transformClassNode(transformedName, cn)) { + return basicClass; + } + final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); + cn.accept(cw); + final byte[] bytes = cw.toByteArray(); + dump.dumpClass(transformedName, basicClass, bytes, transformer); + return bytes; + } + + private static boolean startsWithAny(String name, String[] prefixes) { + for (String prefix : prefixes) { + if (name.startsWith(prefix)) return true; + } + return false; + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/RfbModRedirector.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/RfbModRedirector.java new file mode 100644 index 000000000..38e8d19ab --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/loading/RfbModRedirector.java @@ -0,0 +1,76 @@ +package com.gtnewhorizons.angelica.glsm.loading; + +import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; +import com.gtnewhorizons.retrofuturabootstrap.api.ExtensibleClassLoader; +import com.gtnewhorizons.retrofuturabootstrap.api.RfbClassTransformer; +import org.intellij.lang.annotations.Pattern; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +public abstract class RfbModRedirector implements RfbClassTransformer { + + private static final Attributes.Name LWJGL3_AWARE = new Attributes.Name("Lwjgl3ify-Aware"); + + private final String id; + private final ModRedirector inner; + + protected RfbModRedirector(@Pattern("[a-z0-9-]+") String id, ModRedirector inner) { + this.id = id; + this.inner = inner; + } + + @Override + public @NotNull String id() { + return id; + } + + private static boolean isLwjgl3Aware(@Nullable Manifest manifest) { + return manifest != null && "true".equals(manifest.getMainAttributes().getValue(LWJGL3_AWARE)); + } + + @Override + public @NotNull String @Nullable [] sortAfter() { + return new String[] {"*", "mixin:mixin"}; + } + + @Override + public @NotNull String @Nullable [] sortBefore() { + return new String[] {"lwjgl3ify:redirect"}; + } + + @Override + public @NotNull String @Nullable [] additionalExclusions() { + return inner.getTransformerExclusions(); + } + + @Override + public boolean shouldTransformClass(@NotNull ExtensibleClassLoader classLoader, + @NotNull RfbClassTransformer.Context context, @Nullable Manifest manifest, @NotNull String className, + @NotNull ClassNodeHandle classNode) { + if (!classNode.isPresent()) { + return false; + } + return inner.shouldTransform(classNode.getOriginalBytes()); + } + + @Override + public boolean transformClassIfNeeded(@NotNull ExtensibleClassLoader classLoader, + @NotNull RfbClassTransformer.Context context, @Nullable Manifest manifest, @NotNull String className, + @NotNull ClassNodeHandle classNode) { + final boolean changed = inner.transformClassNode(className, classNode.getNode(), isLwjgl3Aware(manifest)); + if (changed) { + classNode.computeMaxs(); + inner.getDump().dumpRFBClass(className, classNode, this); + } + return changed; + } + + @Override + public void transformClass(@NotNull ExtensibleClassLoader classLoader, @NotNull RfbClassTransformer.Context context, + @Nullable Manifest manifest, @NotNull String className, @NotNull ClassNodeHandle classNode) { + transformClassIfNeeded(classLoader, context, manifest, className, classNode); + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureInfoCache.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureInfoCache.java index b74804760..ec25abcff 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureInfoCache.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureInfoCache.java @@ -9,6 +9,9 @@ import org.lwjgl.opengl.GL12; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL21; import org.lwjgl.opengl.GL30; +import org.lwjgl.opengl.GL31; +import org.lwjgl.opengl.GL32; +import org.lwjgl.opengl.GL40; import java.nio.Buffer; import java.util.concurrent.locks.ReentrantLock; @@ -101,6 +104,22 @@ public class TextureInfoCache { }; } + public static boolean isProxyTarget(int target) { + return switch (target) { + case GL11.GL_PROXY_TEXTURE_1D, + GL11.GL_PROXY_TEXTURE_2D, + GL12.GL_PROXY_TEXTURE_3D, + GL13.GL_PROXY_TEXTURE_CUBE_MAP, + GL30.GL_PROXY_TEXTURE_1D_ARRAY, + GL30.GL_PROXY_TEXTURE_2D_ARRAY, + GL31.GL_PROXY_TEXTURE_RECTANGLE, + GL32.GL_PROXY_TEXTURE_2D_MULTISAMPLE, + GL32.GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY, + GL40.GL_PROXY_TEXTURE_CUBE_MAP_ARRAY -> true; + default -> false; + }; + } + private static boolean isTrackedTarget(int target) { return target == GL11.GL_TEXTURE_2D || target == GL11.GL_TEXTURE_1D diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureTargets.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureTargets.java new file mode 100644 index 000000000..c99b0b547 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/texture/TextureTargets.java @@ -0,0 +1,30 @@ +package com.gtnewhorizons.angelica.glsm.texture; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL30; +import org.lwjgl.opengl.GL31; +import org.lwjgl.opengl.GL32; +import org.lwjgl.opengl.GL40; + +public final class TextureTargets { + + private TextureTargets() {} + + public static boolean isProxyTarget(int target) { + return switch (target) { + case GL11.GL_PROXY_TEXTURE_1D, + GL11.GL_PROXY_TEXTURE_2D, + GL12.GL_PROXY_TEXTURE_3D, + GL13.GL_PROXY_TEXTURE_CUBE_MAP, + GL30.GL_PROXY_TEXTURE_1D_ARRAY, + GL30.GL_PROXY_TEXTURE_2D_ARRAY, + GL31.GL_PROXY_TEXTURE_RECTANGLE, + GL32.GL_PROXY_TEXTURE_2D_MULTISAMPLE, + GL32.GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY, + GL40.GL_PROXY_TEXTURE_CUBE_MAP_ARRAY -> true; + default -> false; + }; + } +} diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureBindingTargetCache_UnitTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureBindingTargetCache_UnitTest.java index 7aab05c7b..356dbbc18 100644 --- a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureBindingTargetCache_UnitTest.java +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureBindingTargetCache_UnitTest.java @@ -42,6 +42,28 @@ public class GLSM_TextureBindingTargetCache_UnitTest { } } + @Test + void proxyTargetBindIsNeitherForwardedNorCached() { + drainErrors(); + final int t2d = GL11.glGenTextures(); + try { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE5); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, t2d); + + GLStateManager.glBindTexture(GL11.GL_PROXY_TEXTURE_2D, 0); + GLStateManager.glBindTexture(GL12.GL_PROXY_TEXTURE_3D, t2d); + + assertEquals(GL11.GL_NO_ERROR, GL11.glGetError(), "a proxy target must never reach the driver"); + assertEquals(t2d, GLStateManager.getBoundTextureForServerState(5), "a proxy bind must not disturb the cache"); + assertEquals(t2d, GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D)); + } finally { + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glDeleteTextures(t2d); + drainErrors(); + } + } + @Test void popAttribRestoresRecordedTarget() { drainErrors(); diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureUploadGeneration_GLTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureUploadGeneration_GLTest.java index 46b310960..c494937bb 100644 --- a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureUploadGeneration_GLTest.java +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/GLSM_TextureUploadGeneration_GLTest.java @@ -22,6 +22,21 @@ public class GLSM_TextureUploadGeneration_GLTest { return texId; } + @Test + void aLevelBeyondTheShiftWidthClampsToOne() { + final int texId = allocate(64); + try { + assertEquals(64, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(1, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 6, GL11.GL_TEXTURE_WIDTH)); + assertEquals(1, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 31, GL11.GL_TEXTURE_WIDTH)); + assertEquals(1, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 32, GL11.GL_TEXTURE_WIDTH)); + assertEquals(1, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 64, GL11.GL_TEXTURE_HEIGHT)); + } finally { + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + GLStateManager.glDeleteTextures(texId); + } + } + @Test void anUploadLeavesANonZeroGeneration() { final int texId = allocate(4); diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfigDefaultsTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfigDefaultsTest.java new file mode 100644 index 000000000..214bdd0d1 --- /dev/null +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/hooks/GLSMInitConfigDefaultsTest.java @@ -0,0 +1,15 @@ +package com.gtnewhorizons.angelica.glsm.hooks; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class GLSMInitConfigDefaultsTest { + + @Test + void emptyBuilderLeavesDsaToTheProbe() { + assertTrue(GLSMInitConfig.builder().build().isDSAEnabled()); + assertFalse(GLSMInitConfig.builder().enableDSA(false).build().isDSAEnabled()); + } +} diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirectorTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirectorTest.java new file mode 100644 index 000000000..6470105ef --- /dev/null +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/loading/ModRedirectorTest.java @@ -0,0 +1,127 @@ +package com.gtnewhorizons.angelica.glsm.loading; + +import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; +import org.junit.jupiter.api.Test; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ModRedirectorTest { + + private static final String EXTRA_EXCLUSION = "com.example.excluded"; + + private static ClassNode emptyClass() { + final ClassNode cn = new ClassNode(); + cn.version = Opcodes.V1_8; + cn.access = Opcodes.ACC_PUBLIC; + cn.name = "com/example/TestClass"; + cn.superName = "java/lang/Object"; + cn.methods = new ArrayList<>(); + + final MethodNode mn = new MethodNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "test", "()V", null, null); + mn.instructions.add(new InsnNode(Opcodes.RETURN)); + cn.methods.add(mn); + return cn; + } + + private static byte[] bytesWithoutGlCall() { + return toBytes(emptyClass()); + } + + private static byte[] bytesWithGlCall() { + final ClassNode cn = emptyClass(); + final MethodNode mn = cn.methods.get(0); + mn.instructions.clear(); + mn.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/lwjgl/opengl/GL11", "glPopMatrix", "()V", false)); + mn.instructions.add(new InsnNode(Opcodes.RETURN)); + return toBytes(cn); + } + + private static byte[] toBytes(ClassNode cn) { + final ClassWriter cw = new ClassWriter(0); + cn.accept(cw); + return cw.toByteArray(); + } + + private static ModRedirector redirector() { + return new ModRedirector(ClassDump.DISABLED, EXTRA_EXCLUSION); + } + + private static void assertStandardExclusions(String[] actual) { + final List exclusions = Arrays.asList(actual); + final String[] coreExclusions = new GLSMRedirector().getCoreExclusions(); + for (String coreExclusion : coreExclusions) { + assertTrue(exclusions.contains(coreExclusion), coreExclusion); + } + assertTrue(exclusions.contains("com.gtnewhorizons.angelica.lwjgl3.")); + assertTrue(exclusions.contains(EXTRA_EXCLUSION)); + assertEquals(coreExclusions.length + 2, exclusions.size(), exclusions.toString()); + } + + @Test + void exclusionsCombineCoreLwjgl3AndExtra() { + assertStandardExclusions(redirector().getTransformerExclusions()); + } + + @Test + void earlyRedirectorExclusionsCombineCoreLwjgl3AndExtra() { + assertStandardExclusions(new EarlyRedirectorCore(EXTRA_EXCLUSION).getTransformerExclusions()); + } + + @Test + void nullInputReturnsNull() { + assertNull(redirector().transform("com.example.TestClass", null, this)); + } + + @Test + void excludedPrefixReturnsOriginalArray() { + final byte[] original = bytesWithGlCall(); + assertSame(original, redirector().transform(EXTRA_EXCLUSION + ".TestClass", original, this)); + } + + @Test + void coreExcludedPrefixReturnsOriginalArray() { + final byte[] original = bytesWithGlCall(); + assertSame(original, redirector().transform("org.lwjgl.opengl.TestClass", original, this)); + } + + @Test + void unchangedClassReturnsOriginalArray() { + final byte[] original = bytesWithoutGlCall(); + assertSame(original, redirector().transform("com.example.TestClass", original, this)); + } + + @Test + void redirectedClassReturnsNewArray() { + final byte[] original = bytesWithGlCall(); + assertNotSame(original, redirector().transform("com.example.TestClass", original, this)); + } + + @Test + void earlyRedirectorSkipsClassesOutsideItsTargets() { + final byte[] original = bytesWithGlCall(); + final EarlyRedirectorCore early = new EarlyRedirectorCore(EXTRA_EXCLUSION); + assertSame(original, early.transform("com.example.TestClass", original, this)); + } + + @Test + void earlyRedirectorTransformsItsTargets() { + final byte[] original = bytesWithGlCall(); + final EarlyRedirectorCore early = new EarlyRedirectorCore(EXTRA_EXCLUSION); + final String target = EcosystemNarrowRules.EARLY_REDIRECTOR_TARGETS[0] + "TestClass"; + assertNotSame(original, early.transform(target, original, this)); + } +} diff --git a/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMCoreExtension.java b/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMCoreExtension.java index 9f7c97b16..36dc4d127 100644 --- a/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMCoreExtension.java +++ b/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMCoreExtension.java @@ -39,7 +39,12 @@ public class GLSMCoreExtension implements BeforeAllCallback, BeforeEachCallback, new ContextAttribs(3, 3).withProfileCore(true).withForwardCompatible(true)); GLSMExtension.setMainThread(Thread.currentThread()); - GLStateManager.initialize(GLSMInitConfig.builder().displaySize(800, 600).build()); + GLStateManager.initialize(GLSMInitConfig.builder() + .displaySize(800, 600) + .directDrawer(t -> {}) + .streamingDrawerDestroy(() -> {}) + .enableDSA(false) + .build()); GLStateManager.setRunningSplash(false); GLStateManager.markSplashComplete("test"); diff --git a/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMExtension.java b/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMExtension.java index e9cd6c206..e26d2cc22 100644 --- a/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMExtension.java +++ b/glsm/src/testFixtures/java/com/gtnewhorizons/angelica/glsm/GLSMExtension.java @@ -69,6 +69,9 @@ public class GLSMExtension implements BeforeAllCallback, BeforeEachCallback, Aft displayMode = new DisplayMode(800, 600); final GLSMInitConfig config = GLSMInitConfig.builder() .displaySize(displayMode.getWidth(), displayMode.getHeight()) + .directDrawer(t -> {}) + .streamingDrawerDestroy(() -> {}) + .enableDSA(false) .build(); setMainThread(Thread.currentThread()); diff --git a/gradle/angelica-run-common.gradle.kts b/gradle/angelica-run-common.gradle.kts new file mode 100644 index 000000000..3fe47a8ad --- /dev/null +++ b/gradle/angelica-run-common.gradle.kts @@ -0,0 +1,68 @@ +import com.gtnewhorizons.retrofuturagradle.MinecraftExtension +import org.gradle.accessors.dm.LibrariesForLibs +import xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar +import xyz.wagyourtail.jvmdg.gradle.task.files.DowngradeFiles + +val libs = the() + +tasks.withType().configureEach { logLevel.set("FATAL") } +tasks.withType().configureEach { logLevel.set("FATAL") } + +configurations.all { + resolutionStrategy.dependencySubstitution { + substitute(module("com.github.GTNewHorizons:lwjgl3ify")) + .using(module("com.github.GTNewHorizons:lwjgl3ify:${libs.versions.lwjgl3ify.get()}")) + } +} + +repositories { + maven { + name = "Maven Central Snapshots" + url = uri("https://central.sonatype.com/repository/maven-snapshots/") + content { includeGroup("org.lwjgl") } + } +} + +tasks.withType().matching { it.name.startsWith("runClient") }.configureEach { + for ((key, value) in System.getProperties()) { + val name = key.toString() + if (name.startsWith("angelica.") || name.startsWith("quickPlay")) { + jvmArgs("-D$name=$value") + } + } +} + +val isMacOs = org.gradle.internal.os.OperatingSystem.current().isMacOsX +val lwjglDebug = project.extra.has("lwjglDebug") && project.extra["lwjglDebug"] as Boolean + +tasks.withType().configureEach { + if (name.startsWith("runClient") && name != "runClient" && isMacOs) { + jvmArgs("-XstartOnFirstThread") + jvmArgs("-Dangelica.sdlgpu.encoderAssertions=" + if (lwjglDebug) "fatal" else "warn") + if (lwjglDebug) { + environment("METAL_DEVICE_WRAPPER_TYPE", "1") + environment("METAL_DEBUG_ERROR_MODE", "0") + environment("MTL_SHADER_VALIDATION", "1") + environment("MTL_SHADER_VALIDATION_REPORT_TO_STDERR", "1") + environment("MTL_DEBUG_LAYER", "1") + environment("MallocScribble", "1") + environment("MallocPreScribble", "1") + environment("MallocGuardEdges", "1") + environment("MallocStackLogging", "1") + environment("MallocStackLoggingNoCompact", "1") + } + } +} + +configure { + lwjgl3Bindings.addAll("shaderc", "spvc") + lwjgl3Version = libs.versions.lwjgl3.get() +} + +tasks.named("processResources") { + val projectVersion = project.version.toString() + inputs.property("version", projectVersion) + filesMatching("META-INF/rfb-plugin/*") { + expand("version" to projectVersion) + } +} diff --git a/gradle/angelica-shadow-common.gradle.kts b/gradle/angelica-shadow-common.gradle.kts new file mode 100644 index 000000000..a82dcc930 --- /dev/null +++ b/gradle/angelica-shadow-common.gradle.kts @@ -0,0 +1,28 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar + +val embedOnly: Configuration by configurations + +tasks.named("shadowJar") { + dependsOn(embedOnly) + from(embedOnly.map(::zipTree)) + + duplicatesStrategy = DuplicatesStrategy.INCLUDE + mergeServiceFiles() + + minimize { + exclude(project(rootProject.path)) + exclude(project(":glsm")) + exclude(project(":lwjgl3-backend")) + exclude(project(":sdl-gpu")) + exclude(dependency("org.taumc:.*:.*")) + exclude(dependency("org.embeddedt.celeritas:.*:.*")) + exclude(dependency("org.antlr:.*:.*")) + } + + relocate("org.taumc.celeritas", "org.taumc.celeritas") + relocate("org.embeddedt", "org.embeddedt") + relocate("com.gtnewhorizons.angelica.config", "com.gtnewhorizons.angelica.config") + relocate("com.gtnewhorizons.angelica.glsm", "com.gtnewhorizons.angelica.glsm") + relocate("com.gtnewhorizons.angelica.lwjgl3", "com.gtnewhorizons.angelica.lwjgl3") + relocate("com.gtnewhorizons.angelica.sdlgpu", "com.gtnewhorizons.angelica.sdlgpu") +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 19f6ae345..4cbcedaf7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,10 +6,10 @@ log4j = "2.0-beta9" asmCompile = "5.0.3" # End Frozen -lwjgl3 = "3.4.2" +lwjgl3 = "3.4.2-SNAPSHOT" lwjgl3ify = "3.0.31" -celeritas = "2.5.10-GTNH" +celeritas = "2.5.11-GTNH" gtnhlib = "0.11.36" eventbus = "7.0-beta.15" glslTransformLib = "0.2.0-33.g3ec381e" @@ -30,10 +30,10 @@ asmBuild = "9.7" mockito = "3.12.4" archunit = "1.4.1" ant = "1.8.2" +distantHorizonsApi = "5.1.0" [libraries] celeritas-common = { module = "org.embeddedt.celeritas:celeritas-common", version.ref = "celeritas" } -celeritas-lwjgl2-service = { module = "org.embeddedt.celeritas:celeritas-lwjgl2-service", version.ref = "celeritas" } eventbus = { module = "net.minecraftforge:eventbus", version.ref = "eventbus" } glsl-transformation-lib = { module = "org.taumc:glsl-transformation-lib", version.ref = "glslTransformLib" } antlr4-runtime = { module = "org.antlr:antlr4-runtime", version.ref = "antlr4Runtime" } @@ -58,6 +58,7 @@ junit-platform-reporting = { module = "org.junit.platform:junit-platform-reporti mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" } archunit-junit5 = { module = "com.tngtech.archunit:archunit-junit5", version.ref = "archunit" } ant = { module = "org.apache.ant:ant", version.ref = "ant" } +distant-horizons-api = { module = "maven.modrinth:DistantHorizonsApi", version.ref = "distantHorizonsApi" } asm-tree-compile = { module = "org.ow2.asm:asm-tree", version.ref = "asmCompile" } asm-tree-test = { module = "org.ow2.asm:asm-tree", version.ref = "asmTest" } asm-build = { module = "org.ow2.asm:asm", version.ref = "asmBuild" } diff --git a/scripts/flyby-capture.sh b/scripts/flyby-capture.sh index 084bfdcab..d534f0892 100755 --- a/scripts/flyby-capture.sh +++ b/scripts/flyby-capture.sh @@ -5,25 +5,38 @@ # * Game waits for the tracy connection, runs the route, returns to the original coordinates and exits. # * tracy-capture finalizes # -# Usage: scripts/flyby-capture.sh [route] [length] +# Usage: scripts/flyby-capture.sh [route] [length] [extra gradle args...] # route - straight | pan | circuit | static # length - Units: blocks (straight), blocks per leg (circuit), degrees (pan), ticks (static). +# extra - passed through to gradle; -Dangelica.* reaches the client (e.g. -Dangelica.flyby.timeOfDay=1500). # -# FLYBY_SPEED - blocks per tick. (0.5 ~= creative flight). +# FLYBY_SPEED - blocks per tick. (0.5 ~= creative flight). +# QUICKPLAY_WORLD - save to auto-load (default: latest). +# SDLGPU=1 - launch on the SDL-GPU backend. # # Requires tracy-capture on PATH (TRACY_CAPTURE=/path/to/tracy-capture). -# Launch the game to the main menu and load your benchmark save; the run starts once the world is up. +# QuickPlay loads QUICKPLAY_WORLD on launch; the run starts once the world is up. # # Ensure the chunks in the route have been generated before capturing set -euo pipefail -OUT="${1:?usage: flyby-capture.sh [route] [length]}" +OUT="${1:?usage: flyby-capture.sh [route] [length] [extra gradle args...]}" ROUTE="${2:-straight}" LENGTH="${3:-0}" +if [ $# -ge 3 ]; then + shift 3 +else + shift $# +fi WARMUP="${FLYBY_WARMUP:-400}" TRACY_CAPTURE="${TRACY_CAPTURE:-tracy-capture}" +BACKEND_ARGS=() +if [ "${SDLGPU:-0}" = "1" ]; then + BACKEND_ARGS+=(-Dangelica.sdlgpu.enable=true) +fi + if ! command -v "$TRACY_CAPTURE" >/dev/null 2>&1 && [ ! -x "$TRACY_CAPTURE" ]; then cat >&2 <<'EOF' error: tracy-capture not found. @@ -56,7 +69,6 @@ cleanup() { trap cleanup EXIT echo "==> launching game (route=$ROUTE warmup=$WARMUP length=${LENGTH:-default})" -echo " load your benchmark save once the main menu appears" ./gradlew runClient25 --console=plain \ -Dangelica.flyby.route="$ROUTE" \ @@ -64,7 +76,10 @@ echo " load your benchmark save once the main menu appears" -Dangelica.flyby.speed="${FLYBY_SPEED:-0}" \ -Dangelica.flyby.warmupTicks="$WARMUP" \ -Dangelica.flyby.waitForTracy=true \ - -Dangelica.flyby.exitWhenDone=true + -Dangelica.flyby.exitWhenDone=true \ + -DquickPlaySingleplayer="${QUICKPLAY_WORLD:-latest}" \ + ${BACKEND_ARGS[@]+"${BACKEND_ARGS[@]}"} \ + "$@" echo "==> game exited, finalising capture" cleanup diff --git a/sdl-gpu/build.gradle.kts b/sdl-gpu/build.gradle.kts index 96788c90b..f9a1a5938 100644 --- a/sdl-gpu/build.gradle.kts +++ b/sdl-gpu/build.gradle.kts @@ -77,6 +77,9 @@ dependencies { testImplementation(platform(libs.junit.bom)) testImplementation(libs.junit.jupiter) testImplementation(testFixtures(project(":glsm"))) + testImplementation(project(":glsm")) { + capabilities { requireCapability("${project.group}:glsm-stubs") } + } testImplementation(libs.celeritas.common) { isTransitive = false } testImplementation(libs.lwjgl3.opengl) // GL. loads liblwjgl_opengl.so. Test scope only: a root-level runtimeOnly collides with lwjgl3Bindings. @@ -84,15 +87,77 @@ dependencies { testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.log4j.core) testRuntimeOnly(libs.fastutil) + testRuntimeOnly(libs.joml) + testRuntimeOnly(libs.commons.lang3) + testRuntimeOnly(libs.guava) + testImplementation(libs.asm.tree.test) +} + +val glsmSdlTag = "glsm-sdl" + +val glsmSdlStubs = sourceSets.create("glsmSdlStubs") { + java.srcDir("src/glsmSdlStubs/java") +} + +val glsmSdlAgent = sourceSets.create("glsmSdlAgent") { + java.srcDir("src/glsmSdlAgent/java") +} + +dependencies { + "glsmSdlAgentCompileOnly"(project(":glsm")) + "glsmSdlAgentCompileOnly"(libs.asm.tree.test) + testImplementation(glsmSdlAgent.output) } tasks.test { - useJUnitPlatform() + useJUnitPlatform { + excludeTags(glsmSdlTag) + } if (System.getProperty("os.name").startsWith("Mac")) { jvmArgs("-XstartOnFirstThread") } } +val glsmSdlRedirectAgentJar by tasks.registering(Jar::class) { + archiveClassifier = "glsm-sdl-redirect-agent" + from(glsmSdlAgent.output) + manifest { + attributes("Premain-Class" to "com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlRedirectAgent") + } +} + +val glsmSdlTest by tasks.registering(Test::class) { + description = "Runs the headless GLSM-on-SDL FFP rig in its own JVM, with the SDL GPU gate engaged." + group = "verification" + testClassesDirs = sourceSets["test"].output.classesDirs + classpath = glsmSdlStubs.output.classesDirs.plus( + sourceSets["test"].runtimeClasspath.filter { file -> + !file.name.startsWith("lwjgl-2.") && !file.name.startsWith("lwjgl_util-") && !file.name.startsWith("lwjgl-platform-") + } + ) + useJUnitPlatform { + includeTags(glsmSdlTag) + } + dependsOn(tasks.named("glsmSdlStubsClasses"), glsmSdlRedirectAgentJar) + val agentJarPath = glsmSdlRedirectAgentJar.flatMap { it.archiveFile }.map { it.asFile.path } + jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-javaagent:${agentJarPath.get()}") }) + jvmArgs( + "-Dangelica.sdlgpu.enable=true", + "-Dangelica.sdlgpu.disablePresenterThread=true", + "-Dceleritas.lwjglService=com.gtnewhorizons.angelica.sdlgpu.SDLGPULWJGLService", + ) + if (System.getProperty("os.name").startsWith("Mac")) { + jvmArgs("-XstartOnFirstThread") + } + testLogging { + events("passed", "skipped", "failed") + exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL + } + mustRunAfter(tasks.test) +} + +tasks.check { dependsOn(glsmSdlTest) } + tasks.named("compileJava") { val classesDir = destinationDirectory doLast { injectLwjgl3Aware(classesDir.get().asFile) } diff --git a/sdl-gpu/src/glsmSdlAgent/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlRedirectAgent.java b/sdl-gpu/src/glsmSdlAgent/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlRedirectAgent.java new file mode 100644 index 000000000..daa6098ce --- /dev/null +++ b/sdl-gpu/src/glsmSdlAgent/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlRedirectAgent.java @@ -0,0 +1,58 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; + +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.Instrumentation; +import java.security.ProtectionDomain; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public final class GlsmSdlRedirectAgent { + + private static final String GTNHLIB = "com/gtnewhorizon/gtnhlib/"; + private static final List FAILURES = Collections.synchronizedList(new ArrayList<>()); + private static final AtomicInteger TRANSFORMED_COUNT = new AtomicInteger(); + + private GlsmSdlRedirectAgent() {} + + public static void premain(String args, Instrumentation instrumentation) { + instrumentation.addTransformer(new Redirector()); + } + + public static List failures() { + return List.copyOf(FAILURES); + } + + public static int transformedCount() { + return TRANSFORMED_COUNT.get(); + } + + private static final class Redirector implements ClassFileTransformer { + + private final GLSMRedirector redirector = new GLSMRedirector(); + + @Override + public byte[] transform(ClassLoader loader, String className, Class redefined, ProtectionDomain domain, byte[] classfile) { + if (className == null || !className.startsWith(GTNHLIB)) return null; + try { + if (!redirector.shouldTransform(classfile)) return null; + final ClassNode node = new ClassNode(); + new ClassReader(classfile).accept(node, 0); + if (!redirector.transformClassNode(className.replace('/', '.'), node)) return null; + final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + node.accept(writer); + TRANSFORMED_COUNT.incrementAndGet(); + return writer.toByteArray(); + } catch (Throwable t) { + FAILURES.add(className + ": " + t); + return null; + } + } + } +} diff --git a/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/Sys.java b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/Sys.java new file mode 100644 index 000000000..5c33a5ce3 --- /dev/null +++ b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/Sys.java @@ -0,0 +1,10 @@ +package org.lwjgl; + +public final class Sys { + + private Sys() {} + + public static boolean is64Bit() { + return !"32".equals(System.getProperty("sun.arch.data.model")); + } +} diff --git a/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/ContextCapabilities.java b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/ContextCapabilities.java new file mode 100644 index 000000000..ade3af2db --- /dev/null +++ b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/ContextCapabilities.java @@ -0,0 +1,31 @@ +package org.lwjgl.opengl; + +public class ContextCapabilities { + + public boolean OpenGL30 = true; + public boolean OpenGL31 = true; + public boolean OpenGL32 = true; + public boolean OpenGL33 = true; + public boolean OpenGL40 = true; + public boolean OpenGL42 = true; + public boolean OpenGL43 = true; + public boolean OpenGL44 = true; + public boolean OpenGL45 = true; + + public boolean GL_ARB_buffer_storage = true; + public boolean GL_ARB_clear_texture = true; + public boolean GL_ARB_compute_shader = true; + public boolean GL_ARB_direct_state_access = false; + public boolean GL_ARB_draw_buffers_blend = false; + public boolean GL_ARB_multi_draw_indirect = false; + public boolean GL_ARB_sampler_objects = true; + public boolean GL_ARB_shader_image_load_store = true; + public boolean GL_ARB_shader_storage_buffer_object = true; + public boolean GL_ARB_tessellation_shader = false; + + public boolean GL_EXT_direct_state_access = false; + public boolean GL_EXT_gpu_shader4 = true; + public boolean GL_EXT_shader_image_load_store = true; + + public boolean GL_NVX_gpu_memory_info = false; +} diff --git a/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/Drawable.java b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/Drawable.java new file mode 100644 index 000000000..4c3084714 --- /dev/null +++ b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/Drawable.java @@ -0,0 +1,8 @@ +package org.lwjgl.opengl; + +public interface Drawable { + + void makeCurrent(); + + void releaseContext(); +} diff --git a/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/GLContext.java b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/GLContext.java new file mode 100644 index 000000000..4be14af54 --- /dev/null +++ b/sdl-gpu/src/glsmSdlStubs/java/org/lwjgl/opengl/GLContext.java @@ -0,0 +1,12 @@ +package org.lwjgl.opengl; + +public final class GLContext { + + private static final ContextCapabilities CAPABILITIES = new ContextCapabilities(); + + private GLContext() {} + + public static ContextCapabilities getCapabilities() { + return CAPABILITIES; + } +} diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPUGate.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPUGate.java index 8a2e9a919..33c55f115 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPUGate.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPUGate.java @@ -150,6 +150,7 @@ public final class SDLGPUGate { try { device().claimWindow(ctx.window()); engaged = true; + SDLGPULWJGLService.bind(); BackendManager.RENDER_BACKEND.onPostWindowCreate(ctx.window()); } catch (Throwable t) { initFailure = t; @@ -158,9 +159,10 @@ public final class SDLGPUGate { } public static void fallBackToGL() { - if (SDLGPULWJGLService.isOfferedAsAvailable()) { + if (SDLGPULWJGLService.isConstructed()) { throw new IllegalStateException("Celeritas already selected the SDL GPU LWJGL service; falling back to OpenGL would leave it bound to a dead backend"); } + SDLGPULWJGLService.unbind(); disarmed = true; engaged = false; deviceReady = false; diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLService.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLService.java index e3fbc1b44..aae8a2274 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLService.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLService.java @@ -5,10 +5,10 @@ import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.backend.BackendManager; import com.gtnewhorizons.angelica.sdlgpu.device.MemoryStackWrapper; import com.gtnewhorizons.angelica.sdlgpu.util.MemoryAccess; -import com.mitchej123.lwjgl.DebugMessageHandler; -import com.mitchej123.lwjgl.GLExtension; -import com.mitchej123.lwjgl.LWJGLService; -import com.mitchej123.lwjgl.MemoryStack; +import org.taumc.celeritas.lwjgl.DebugMessageHandler; +import org.taumc.celeritas.lwjgl.GLExtension; +import org.taumc.celeritas.lwjgl.LWJGLService; +import org.taumc.celeritas.lwjgl.MemoryStack; import org.lwjgl.system.MemoryUtil; import java.nio.Buffer; @@ -17,19 +17,32 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.Arrays; -/** SDL GPU implementation of {@link LWJGLService} for Celeritas */ -public final class SDLGPULWJGLService extends LWJGLService { +public final class SDLGPULWJGLService implements LWJGLService { - private static volatile boolean offeredAsAvailable; + private static final ThreadLocal STACK = + ThreadLocal.withInitial(() -> new MemoryStackWrapper(org.lwjgl.system.MemoryStack.stackGet())); - public static boolean isOfferedAsAvailable() { return offeredAsAvailable; } + private static volatile boolean constructed; - @Override public int getPriority() { - if (!SystemProperties.USE_SDL_GPU || !SDLGPUGate.isSDLGPUAvailable() || !SDLGPUGate.isEngaged()) { - return PRIORITY_UNAVAILABLE; + public static SDLGPULWJGLService create() { + if (!SDLGPUGate.isEngaged()) { + throw new IllegalStateException("SDL GPU is not engaged; " + SystemProperties.KEY_CELERITAS_LWJGL_SERVICE + " must not name " + SDLGPULWJGLService.class.getName()); + } + constructed = true; + return new SDLGPULWJGLService(); + } + + public static boolean isConstructed() { return constructed; } + + static void bind() { + System.setProperty(SystemProperties.KEY_CELERITAS_LWJGL_SERVICE, SDLGPULWJGLService.class.getName()); + } + + static void unbind() { + final String key = SystemProperties.KEY_CELERITAS_LWJGL_SERVICE; + if (SDLGPULWJGLService.class.getName().equals(System.getProperty(key))) { + System.clearProperty(key); } - offeredAsAvailable = true; - return 200; } @Override public boolean isOpenGLVersionSupported(int major, int minor) { @@ -47,7 +60,6 @@ public final class SDLGPULWJGLService extends LWJGLService { ARB_uniform_buffer_object, ARB_vertex_array_object, ARB_shader_storage_buffer_object, - ARB_instanced_arrays, KHR_debug -> true; case ARB_timer_query, @@ -75,11 +87,20 @@ public final class SDLGPULWJGLService extends LWJGLService { BackendManager.RENDER_BACKEND.bufferData(target, size, usage); } } - @Override public void glBufferSubData(int target, long offset, ByteBuffer data) { BackendManager.RENDER_BACKEND.bufferSubData(target, offset, data); } - @Override public void glBufferSubData(int target, long offset, long size, long data) { - BackendManager.RENDER_BACKEND.bufferSubData(target, offset, MemoryUtil.memByteBuffer(data, (int) size)); - } @Override public void glBufferStorage(int target, long size, int flags) { BackendManager.RENDER_BACKEND.bufferStorage(target, size, flags); } + @Override public void glBlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) { + BackendManager.RENDER_BACKEND.blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter); + } + @Override public void glDrawArrays(int mode, int first, int count) { BackendManager.RENDER_BACKEND.drawArrays(mode, first, count); } + @Override public void glDrawBuffers(int[] buffers) { + try (var stack = MemoryStack.stackPush()) { + BackendManager.RENDER_BACKEND.drawBuffers(stack.ints(buffers)); + } + } + @Override public void glTexImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, ByteBuffer pixels) { + GLStateManager.glTexImage2D(target, level, internalFormat, width, height, border, format, type, pixels); + } + @Override public void glTexParameteri(int target, int pname, int param) { GLStateManager.glTexParameteri(target, pname, param); } @Override public ByteBuffer glMapBufferRange(int target, long offset, long length, int flags) { return BackendManager.RENDER_BACKEND.mapBufferRange(target, offset, length, flags); } @Override public long nglMapBuffer(int target, int access) { return 0; } @Override public ByteBuffer glMapBuffer(int target, int access) { return BackendManager.RENDER_BACKEND.mapBuffer(target, access); } @@ -231,7 +252,8 @@ public final class SDLGPULWJGLService extends LWJGLService { @Override public int glGetAttribLocation(int program, CharSequence name) { return BackendManager.RENDER_BACKEND.getAttribLocation(program, name); } @Override public MemoryStack stackPush() { - return new MemoryStackWrapper(org.lwjgl.system.MemoryStack.stackPush()); + org.lwjgl.system.MemoryStack.stackPush(); + return STACK.get(); } @Override public long nmemAlloc(long size) { return MemoryUtil.nmemAlloc(size); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java index 43ddcc13c..c2323eec8 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPURenderBackend.java @@ -24,11 +24,13 @@ import com.gtnewhorizons.angelica.sdlgpu.frame.FrameManager.FrameState; import com.gtnewhorizons.angelica.sdlgpu.frame.Presenter; import com.gtnewhorizons.retrofuturabootstrap.MainStartOnFirstThread; import com.gtnewhorizons.angelica.sdlgpu.pipeline.DrawDispatch; +import com.gtnewhorizons.angelica.sdlgpu.pipeline.FFPTrace; import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineApplier; import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineCache; import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineStore; import com.gtnewhorizons.angelica.sdlgpu.pipeline.VertexAttribs; import com.gtnewhorizons.angelica.sdlgpu.resource.BufferParams; +import com.gtnewhorizons.angelica.sdlgpu.resource.CopyRectClip; import com.gtnewhorizons.angelica.sdlgpu.resource.FBOClearTracker; import com.gtnewhorizons.angelica.sdlgpu.resource.FboState; import com.gtnewhorizons.angelica.sdlgpu.resource.FormatMap; @@ -105,6 +107,8 @@ import com.gtnewhorizons.angelica.sdlgpu.compat.Lwjgl3GLCapabilitiesShim; import me.eigenraven.lwjgl3ify.api.GLCapabilitiesOverride; import static org.lwjgl.sdl.SDLGPU.*; +import static org.lwjgl.sdl.SDLSurface.SDL_FLIP_NONE; +import static org.lwjgl.sdl.SDLSurface.SDL_FLIP_VERTICAL; /** SDL GPU implementation of {@link RenderBackend}. */ public class SDLGPURenderBackend extends RenderBackend { @@ -147,6 +151,7 @@ public class SDLGPURenderBackend extends RenderBackend { }); private final PipelineApplier pipelineApplier = new PipelineApplier(frameManager, resourceManager, shaderManager, pipelineStore, fboClearTracker, persistentSync, samplerBinder, storageTextureBinder, storageBufferBinder); private final DrawDispatch drawDispatch = new DrawDispatch(device, frameManager, resourceManager, pipelineApplier, this::enqueuePreCopied); + private final FFPTrace ffpTrace = SystemProperties.FFP_TRACE ? new FFPTrace(resourceManager) : null; private final IntOpenHashSet missingProgramWarned = new IntOpenHashSet(); @@ -220,7 +225,11 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void onPostWindowCreate(long window) { buildCachedStrings(); - populateGLCapabilities(); + if (window != 0L) populateGLCapabilities(); + } + + private boolean hasWindow() { + return device.getClaimedWindow() != 0L; } @Override public void init() { @@ -280,12 +289,16 @@ public class SDLGPURenderBackend extends RenderBackend { st.bumpAttribStateGen(); } }); - DisplayEvents.addPreSwapchainInvalidatingChangeListener(SDLGPUGate.sdlGpuPreSwapchainInvalidatingCallback()); + if (hasWindow()) DisplayEvents.addPreSwapchainInvalidatingChangeListener(SDLGPUGate.sdlGpuPreSwapchainInvalidatingCallback()); GLSMConfig.expandVertexFormats = true; resourceManager.cachePreferredDepthFormats(); - LOG.warn("Swapchain texture format: 0x{} (12=B8G8R8A8_UNORM, 53=B8G8R8A8_SRGB, 4=R8G8B8A8_UNORM)", Integer.toHexString(device.getSwapchainTextureFormat())); - cachedSwapchainFormatArray = new int[]{device.getSwapchainTextureFormat()}; + cachedSwapchainFormatArray = new int[]{ + hasWindow() ? device.getSwapchainTextureFormat() : resourceManager.mapTextureFormat(GL11.GL_RGBA8) + }; PipelineCache.setSwapchainFormats(cachedSwapchainFormatArray); + if (hasWindow()) { + LOG.warn("Swapchain texture format: 0x{} (12=B8G8R8A8_UNORM, 53=B8G8R8A8_SRGB, 4=R8G8B8A8_UNORM)", Integer.toHexString(cachedSwapchainFormatArray[0])); + } pipelineStore.setBufferHandleResolver(resourceManager::getBufferHandle); pipelineStore.setVertexVariantResolver(shaderManager::getOrBuildVertexVariant); resourceManager.setBufferLivenessListener(this::markAllPipelineInputDirty); @@ -298,15 +311,17 @@ public class SDLGPURenderBackend extends RenderBackend { LOG.info("SDL presenter thread enabled: presents run on '{}'", device.getWindowThread() != null ? device.getWindowThread().getName() : "window thread"); } - final int splashW = Math.max(1, Display.getWidth()); - final int splashH = Math.max(1, Display.getHeight()); - final int[] maxDesktop = device.getMaxDesktopSizePixels(); - final int splashTexW = Math.max(splashW, maxDesktop[0]); - final int splashTexH = Math.max(splashH, maxDesktop[1]); - splashTarget = new OffscreenTarget(); - splashTarget.create(device, resourceManager, splashTexW, splashTexH, device.getSwapchainTextureFormat()); - SplashDispatcher.seedDrawnSize(splashW, splashH); - LOG.info("Splash offscreen target {}x{} (window {}x{}, max desktop {}x{})", splashTexW, splashTexH, splashW, splashH, maxDesktop[0], maxDesktop[1]); + if (hasWindow()) { + final int splashW = Math.max(1, Display.getWidth()); + final int splashH = Math.max(1, Display.getHeight()); + final int[] maxDesktop = device.getMaxDesktopSizePixels(); + final int splashTexW = Math.max(splashW, maxDesktop[0]); + final int splashTexH = Math.max(splashH, maxDesktop[1]); + splashTarget = new OffscreenTarget(); + splashTarget.create(device, resourceManager, splashTexW, splashTexH, device.getSwapchainTextureFormat()); + SplashDispatcher.seedDrawnSize(splashW, splashH); + LOG.info("Splash offscreen target {}x{} (window {}x{}, max desktop {}x{})", splashTexW, splashTexH, splashW, splashH, maxDesktop[0], maxDesktop[1]); + } Lwjgl3GLCapabilitiesShim.installOnCurrentThread(advertisedCapabilities()); @@ -673,6 +688,7 @@ public class SDLGPURenderBackend extends RenderBackend { } awaitUploadFlush(); + if (SystemProperties.FFP_TRACE) ffpTrace.frameEnd(f); frameManager.endFrame(); frameManager.presentFinalTarget(); fenceTracker.resolvePendingFences(); @@ -1004,13 +1020,15 @@ public class SDLGPURenderBackend extends RenderBackend { if (!f.frameActive) { f.droppedDrawsThisFrame++; return; } final ContextState st = s(); if (mode == GL11.GL_TRIANGLE_FAN && count >= 3) { - drawDispatch.drawTriangleFanAsTriangleList(st, first, count); + if (!drawDispatch.drawTriangleFanAsTriangleList(st, first, count)) { f.droppedDrawsThisFrame++; return; } + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arraysFan", mode, count, 0, first, 0); return; } drawDispatch.setPrimitiveTypeForDraw(st, FormatMap.mapPrimitiveType(mode)); pipelineApplier.ensureRenderPass(st, f); if (f.renderPass == 0) { f.droppedDrawsThisFrame++; drawDispatch.warnDrawArraysNoRenderPass(mode, st.boundFboId); return; } if (!pipelineApplier.applyPipelineAndState(st, f)) { f.droppedDrawsThisFrame++; return; } + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arrays", mode, count, 0, first, 0); SDL_DrawGPUPrimitives(f.renderPass, count, 1, first, 0); } @@ -1019,6 +1037,7 @@ public class SDLGPURenderBackend extends RenderBackend { final long rp = prepareIndexedDrawBind(st, mode, type, "drawElements"); if (rp == 0) return; final int firstIndex = (int) (indices / FormatMap.indexElementSize(type)); + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elements", mode, count, type, firstIndex, 0); drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, 1, firstIndex, 0); } @@ -1027,6 +1046,7 @@ public class SDLGPURenderBackend extends RenderBackend { final long rp = prepareIndexedDrawBind(st, mode, type, "drawElementsInstanced"); if (rp == 0) return; final int firstIndex = (int) (indices / FormatMap.indexElementSize(type)); + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsInstanced", mode, count, type, firstIndex, 0); drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, primcount, firstIndex, 0); } @@ -1036,6 +1056,7 @@ public class SDLGPURenderBackend extends RenderBackend { final long rp = prepareIndexedDrawBind(st, mode, type, "drawElementsBaseVertex"); if (rp == 0) return; final int firstIndex = (int) (indices / FormatMap.indexElementSize(type)); + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsBaseVertex", mode, count, type, firstIndex, baseVertex); drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, 1, firstIndex, baseVertex); } @@ -1064,7 +1085,10 @@ public class SDLGPURenderBackend extends RenderBackend { final int count = MemoryUtil.memGetInt(pCount + offset); if (count <= 0) continue; final long indices = MemoryUtil.memGetAddress(pIndices + (long) i * Pointer.POINTER_SIZE); - drawDispatch.issueIndexedDraw(st, rp, ebo, type, count, 1, (int) (indices / elementSize), MemoryUtil.memGetInt(pBaseVertex + offset)); + final int firstIndex = (int) (indices / elementSize); + final int baseVertex = MemoryUtil.memGetInt(pBaseVertex + offset); + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "multiElementsBaseVertex", mode, count, type, firstIndex, baseVertex); + drawDispatch.issueIndexedDraw(st, rp, ebo, type, count, 1, firstIndex, baseVertex); } } @@ -1089,6 +1113,7 @@ public class SDLGPURenderBackend extends RenderBackend { + "sentinel indices in the EBO will be drawn as regular indices."); drawDispatch.restartMultiDrawWarned = true; } + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsIndirect", mode, drawcount, type, (int) indirect, 0); SDL_DrawGPUIndexedPrimitivesIndirect(rp, indirectHandle, (int) indirect, drawcount); } finally { Tracy.endZone(); @@ -1102,7 +1127,7 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void dispatchComputeIndirect(long offset) { - final int indirectGlId = getBoundBuffer(GL40.GL_DRAW_INDIRECT_BUFFER); + final int indirectGlId = getBoundBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER); if (indirectGlId == 0) return; final long indirectHandle = resourceManager.getBufferHandle(indirectGlId); if (indirectHandle == 0) return; @@ -1200,6 +1225,7 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void bindTexture(int target, int texture) { + if (isProxyTarget(target)) return; final ContextState st = s(); if (st.activeTextureUnit >= 0 && st.activeTextureUnit < st.boundTextures.length) { if (st.boundTextures[st.activeTextureUnit] != texture) { @@ -1213,6 +1239,87 @@ public class SDLGPURenderBackend extends RenderBackend { st.activeTextureUnit = texture - GL13.GL_TEXTURE0; } + static boolean isProxyTarget(int target) { + return TextureInfoCache.isProxyTarget(target); + } + + private static boolean proxyLayersInHeight(int target) { + return target == GL30.GL_PROXY_TEXTURE_1D_ARRAY; + } + + private static boolean proxyLayersInDepth(int target) { + return isProxyArrayTarget(target) && !proxyLayersInHeight(target); + } + + private static boolean isProxyArrayTarget(int target) { + return target == GL30.GL_PROXY_TEXTURE_1D_ARRAY || target == GL30.GL_PROXY_TEXTURE_2D_ARRAY || target == GL32.GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY || target == GL40.GL_PROXY_TEXTURE_CUBE_MAP_ARRAY; + } + + public static int proxyLevelSize(int base, int level) { + return PixelOps.mipLevelSize(base, level); + } + + static int proxyDimensionLimit(int target) { + return switch (target) { + case GL12.GL_PROXY_TEXTURE_3D -> MAX_3D_TEXTURE_SIZE; + case GL13.GL_PROXY_TEXTURE_CUBE_MAP, GL40.GL_PROXY_TEXTURE_CUBE_MAP_ARRAY -> MAX_CUBE_MAP_TEXTURE_SIZE; + default -> MAX_TEXTURE_SIZE; + }; + } + + static int levelZeroEquivalentSize(int size, int level) { + if (size <= 0 || level < 0 || level >= Integer.SIZE - 1) return 0; + final long scaled = (long) size << level; + return scaled > Integer.MAX_VALUE ? 0 : (int) scaled; + } + + private void recordProxyTexImage(ContextState st, int target, int level, int internalFormat, int width, int height, int depth) { + final boolean layersInHeight = proxyLayersInHeight(target); + final boolean layersInDepth = proxyLayersInDepth(target); + final int dimLimit = proxyDimensionLimit(target); + final int w = levelZeroEquivalentSize(width, level); + final int h = layersInHeight ? height : levelZeroEquivalentSize(height, level); + final int d = layersInDepth ? depth : levelZeroEquivalentSize(depth, level); + final int hLimit = layersInHeight ? MAX_ARRAY_TEXTURE_LAYERS : dimLimit; + final int dLimit = layersInDepth ? MAX_ARRAY_TEXTURE_LAYERS : dimLimit; + final boolean fits = w >= 1 && w <= dimLimit && h >= 1 && h <= hLimit && d >= 1 && d <= dLimit + && resourceManager.isMappableTextureFormat(internalFormat); + if (fits) { + st.proxyTexture.accept(target, w, h, d, internalFormat); + } else { + st.proxyTexture.reject(target); + } + } + + private static int boundTextureFor(ContextState st, int target) { + return isProxyTarget(target) ? 0 : st.boundTextures[st.activeTextureUnit]; + } + + private void ensureLevel0Storage(ContextState st, int glId, int target, int internalFormat, int width, int height, int depth, int format, boolean mipmapped) { + final int numLevels; + if (mipmapped) { + final TextureSamplerState ss = resourceManager.getOrCreateTexSamplerState(glId); + numLevels = ss.maxLevel >= 0 ? ss.maxLevel + 1 : PixelOps.defaultMipLevels(width, height); + } else { + numLevels = 1; + } + final boolean bgraSwizzle = format == GL12.GL_BGRA && (internalFormat == GL11.GL_RGBA || internalFormat == GL11.GL_RGBA8); + final int targetSdlFormat = bgraSwizzle + ? SDLGPU.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM + : resourceManager.mapTextureFormat(internalFormat); + if (canReuseTexture(resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), + target, internalFormat, targetSdlFormat, width, height, depth, numLevels)) { + return; + } + textureOps.releaseTextureForRealloc(st, glId); + if (bgraSwizzle) { + resourceManager.createTextureWithSdlFormat(glId, target, SDLGPU.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, internalFormat, width, height, depth, numLevels); + } else { + resourceManager.createTexture(glId, target, internalFormat, width, height, depth, numLevels); + } + resourceManager.refreshTextureReferences(glId); + } + private static boolean canReuseTexture(ResourceManager.TextureMeta m, long handle, int target, int glFormat, int sdlFormat, int w, int h, int d, int levels) { return m != null && handle != 0 @@ -1227,26 +1334,15 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void texImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, ByteBuffer pixels) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, level, internalFormat, width, height, 1); + return; + } final int glId = st.boundTextures[st.activeTextureUnit]; if (glId == 0) return; if (level == 0) { - final TextureSamplerState ss = resourceManager.getOrCreateTexSamplerState(glId); - final int numLevels = ss.maxLevel >= 0 ? ss.maxLevel + 1 : PixelOps.defaultMipLevels(width, height); - final boolean bgraSwizzle = format == GL12.GL_BGRA && (internalFormat == GL11.GL_RGBA || internalFormat == GL11.GL_RGBA8); - final int targetSdlFormat = bgraSwizzle - ? SDLGPU.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM - : resourceManager.mapTextureFormat(internalFormat); - if (!canReuseTexture(resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), - target, internalFormat, targetSdlFormat, width, height, 1, numLevels)) { - textureOps.releaseTextureForRealloc(st, glId); - if (bgraSwizzle) { - resourceManager.createTextureWithSdlFormat(glId, target, SDLGPU.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, internalFormat, width, height, 1, numLevels); - } else { - resourceManager.createTexture(glId, target, internalFormat, width, height, 1, numLevels); - } - resourceManager.refreshTextureReferences(glId); - } + ensureLevel0Storage(st, glId, target, internalFormat, width, height, 1, format, true); } textureOps.uploadTextureRegion(st, glId, resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), pixels, 0, 0, width, height, level, format, type); @@ -1262,18 +1358,15 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void texImage3D(int target, int level, int internalFormat, int width, int height, int depth, int border, int format, int type, ByteBuffer pixels) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, level, internalFormat, width, height, depth); + return; + } final int glId = st.boundTextures[st.activeTextureUnit]; if (glId == 0) return; if (level == 0) { - final int numLevels = 1; - final int targetSdlFormat = resourceManager.mapTextureFormat(internalFormat); - if (!canReuseTexture(resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), - target, internalFormat, targetSdlFormat, width, height, depth, numLevels)) { - textureOps.releaseTextureForRealloc(st, glId); - resourceManager.createTexture(glId, target, internalFormat, width, height, depth, numLevels); - resourceManager.refreshTextureReferences(glId); - } + ensureLevel0Storage(st, glId, target, internalFormat, width, height, depth, GL11.GL_NONE, false); } if (pixels == null) return; final long texHandle = resourceManager.getTextureHandle(glId); @@ -1295,18 +1388,15 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void texImage1D(int target, int level, int internalFormat, int width, int border, int format, int type, ByteBuffer pixels) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, level, internalFormat, width, 1, 1); + return; + } final int glId = st.boundTextures[st.activeTextureUnit]; if (glId == 0) return; if (level == 0) { - final int numLevels = 1; - final int targetSdlFormat = resourceManager.mapTextureFormat(internalFormat); - if (!canReuseTexture(resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), - target, internalFormat, targetSdlFormat, width, 1, 1, numLevels)) { - textureOps.releaseTextureForRealloc(st, glId); - resourceManager.createTexture(glId, target, internalFormat, width, 1, 1, numLevels); - resourceManager.refreshTextureReferences(glId); - } + ensureLevel0Storage(st, glId, target, internalFormat, width, 1, 1, GL11.GL_NONE, false); } if (pixels == null) return; textureOps.uploadTextureRegion(st, glId, resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), @@ -1314,18 +1404,15 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void texImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, long pixelBufferOffset) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, level, internalFormat, width, height, 1); + return; + } final int glId = st.boundTextures[st.activeTextureUnit]; if (glId == 0) return; if (level == 0) { - final TextureSamplerState ss = resourceManager.getOrCreateTexSamplerState(glId); - final int numLevels = ss.maxLevel >= 0 ? ss.maxLevel + 1 : PixelOps.defaultMipLevels(width, height); - final int targetSdlFormat = resourceManager.mapTextureFormat(internalFormat); - if (!canReuseTexture(resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), target, internalFormat, targetSdlFormat, width, height, 1, numLevels)) { - textureOps.releaseTextureForRealloc(st, glId); - resourceManager.createTexture(glId, target, internalFormat, width, height, 1, numLevels); - resourceManager.refreshTextureReferences(glId); - } + ensureLevel0Storage(st, glId, target, internalFormat, width, height, 1, format, true); } if (st.boundPixelUnpackBuffer == 0) return; @@ -1342,7 +1429,7 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void texSubImage2D(int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, ByteBuffer pixels) { final ContextState st = s(); - final int glId = st.boundTextures[st.activeTextureUnit]; + final int glId = boundTextureFor(st, target); if (glId == 0 || pixels == null) return; textureOps.uploadTextureRegion(st, glId, resourceManager.getTextureMeta(glId), resourceManager.getTextureHandle(glId), pixels, xoffset, yoffset, width, height, level, format, type); } @@ -1352,7 +1439,7 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void texSubImage2D(int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, long pboOffset) { final ContextState st = s(); if (st.boundPixelUnpackBuffer == 0) return; - final int glId = st.boundTextures[st.activeTextureUnit]; + final int glId = boundTextureFor(st, target); if (glId == 0) return; final ByteBuffer staging = resourceManager.getPboStaging(st.boundPixelUnpackBuffer); if (staging == null) return; @@ -1366,24 +1453,29 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void copyTexSubImage2D(int target, int level, int xoffset, int yoffset, int x, int y, int width, int height) { final ContextState cs = s(); - final int destGlId = cs.boundTextures[cs.activeTextureUnit]; - textureOps.copyTexSubImageImpl(cs,destGlId, level, xoffset, yoffset, x, y, width, height); + textureOps.copyTexSubImageImpl(cs, boundTextureFor(cs, target), level, xoffset, yoffset, x, y, width, height); } @Override public void copyTexImage2D(int target, int level, int internalFormat, int x, int y, int width, int height, int border) { final ContextState cs = s(); - final int destGlId = cs.boundTextures[cs.activeTextureUnit]; + final int destGlId = boundTextureFor(cs, target); + if (destGlId == 0) return; + + if (level == 0) { + ensureLevel0Storage(cs, destGlId, target, internalFormat, width, height, 1, GL11.GL_NONE, true); + } + textureOps.copyTexSubImageImpl(cs, destGlId, level, 0, 0, x, y, width, height); } @Override public void texParameteri(int target, int pname, int param) { final ContextState cs = s(); - textureOps.texParameteri(cs, pname, param, cs.boundTextures[cs.activeTextureUnit]); + textureOps.texParameteri(cs, pname, param, boundTextureFor(cs, target)); } @Override public void texParameterf(int target, int pname, float param) { final ContextState cs = s(); - textureOps.texParameterf(cs, pname, param, cs.boundTextures[cs.activeTextureUnit]); + textureOps.texParameterf(cs, pname, param, boundTextureFor(cs, target)); } @Override public void texParameteriv(int target, int pname, IntBuffer params) { @@ -1394,22 +1486,31 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public int getTexParameteri(int target, int pname) { final ContextState cs = s(); - return getTextureParameteri(cs.boundTextures[cs.activeTextureUnit], target, pname); + return getTextureParameteri(boundTextureFor(cs, target), target, pname); } @Override public float getTexParameterf(int target, int pname) { final ContextState cs = s(); - return getTextureParameterf(cs.boundTextures[cs.activeTextureUnit], target, pname); + return getTextureParameterf(boundTextureFor(cs, target), target, pname); } @Override public int getTexLevelParameteri(int target, int level, int pname) { final ContextState cs = s(); + if (isProxyTarget(target)) { + final ContextState.ProxyTextureState proxy = cs.proxyTexture; + if (proxy.target != target) return 0; + return switch (pname) { + case GL11.GL_TEXTURE_WIDTH -> proxyLevelSize(proxy.width, level); + case GL11.GL_TEXTURE_HEIGHT -> proxyLayersInHeight(target) ? proxy.height : proxyLevelSize(proxy.height, level); + case GL12.GL_TEXTURE_DEPTH -> proxyLayersInDepth(target) ? proxy.depth : proxyLevelSize(proxy.depth, level); + case GL11.GL_TEXTURE_INTERNAL_FORMAT -> proxy.internalFormat; + default -> 0; + }; + } final int glId = cs.boundTextures[cs.activeTextureUnit]; final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(glId); if (meta == null) return 0; - final int mipW = Math.max(1, meta.width() >> level); - final int mipH = Math.max(1, meta.height() >> level); return switch (pname) { - case GL11.GL_TEXTURE_WIDTH -> mipW; - case GL11.GL_TEXTURE_HEIGHT -> mipH; + case GL11.GL_TEXTURE_WIDTH -> PixelOps.mipLevelSize(meta.width(), level); + case GL11.GL_TEXTURE_HEIGHT -> PixelOps.mipLevelSize(meta.height(), level); case GL11.GL_TEXTURE_INTERNAL_FORMAT -> { final int stored = meta.glFormat(); yield TextureInfoCache.isGenericCompressedInternalFormat(stored) ? GL11.GL_RGBA8 : stored; @@ -1418,8 +1519,7 @@ public class SDLGPURenderBackend extends RenderBackend { }; } @Override public void generateMipmap(int target) { - final ContextState st = s(); - generateTextureMipmap(st.boundTextures[st.activeTextureUnit]); + generateTextureMipmap(boundTextureFor(s(), target)); } @Override public void pixelStorei(int pname, int param) { final ContextState.PixelStoreState ps = s().pixelStore; @@ -1689,12 +1789,20 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public void readPixels(int x, int y, int width, int height, int format, int type, ByteBuffer pixels) { + if (pixels == null || width <= 0 || height <= 0) return; final ContextState cs = s(); + final boolean fromFbo0 = cs.boundReadFboId == 0; final long texHandle; final int srcSdlFormat; - if (cs.boundReadFboId == 0) { + final int srcWidth; + final int srcHeight; + final int srcY; + if (fromFbo0) { texHandle = frameManager.getFbo0Texture(); srcSdlFormat = frameManager.getSwapchainFormat(); + srcWidth = frameManager.getFbo0Width(); + srcHeight = frameManager.getFbo0Height(); + srcY = srcHeight - y - height; } else { final FboState fbo = resourceManager.getFbo(cs.boundReadFboId); if (fbo == null) return; @@ -1702,10 +1810,58 @@ public class SDLGPURenderBackend extends RenderBackend { texHandle = resourceManager.getTextureHandle(colorGlId); final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(colorGlId); srcSdlFormat = meta != null ? meta.sdlFormat() : SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; + srcWidth = meta != null ? meta.width() : fbo.width; + srcHeight = meta != null ? meta.height() : fbo.height; + srcY = y; + } + if (texHandle == 0 || srcWidth <= 0 || srcHeight <= 0) return; + + final long clip = CopyRectClip.clipCopyRect(x, srcY, 0, 0, width, height, srcWidth, srcHeight, width, height); + if (clip == CopyRectClip.EMPTY) return; + final int sx = CopyRectClip.srcX(clip); + final int sy = CopyRectClip.srcY(clip); + final int w = CopyRectClip.width(clip); + final int h = CopyRectClip.height(clip); + final int colOffset = CopyRectClip.dstX(clip, x, 0); + final int dy = CopyRectClip.dstY(clip, srcY, 0); + final int rowOffset = fromFbo0 ? (height - h - dy) : dy; + + final int basePos = pixels.position(); + final int baseLimit = pixels.limit(); + final int stride = width * 4; + if (baseLimit - basePos < height * stride) return; + try { + if (w == width) { + final int start = basePos + rowOffset * stride; + pixels.position(0); + pixels.limit(start + h * stride); + pixels.position(start); + textureOps.readbackTexture(texHandle, sx, sy, w, h, 0, pixels); + pixels.position(0); + pixels.limit(baseLimit); + pixels.position(start); + PixelOps.postProcessReadback(pixels, width, h, format, srcSdlFormat, fromFbo0); + } else { + final ByteBuffer staging = MemoryUtil.memAlloc(w * h * 4); + try { + textureOps.readbackTexture(texHandle, sx, sy, w, h, 0, staging); + staging.rewind(); + PixelOps.postProcessReadback(staging, w, h, format, srcSdlFormat, fromFbo0); + for (int row = 0; row < h; row++) { + staging.position(0); + staging.limit((row + 1) * w * 4); + staging.position(row * w * 4); + pixels.position(basePos + (rowOffset + row) * stride + colOffset * 4); + pixels.put(staging); + } + } finally { + MemoryUtil.memFree(staging); + } + } + } finally { + pixels.limit(baseLimit); + pixels.position(basePos); } - textureOps.readbackTexture(texHandle, x, y, width, height, 0, pixels); - pixels.rewind(); - PixelOps.postProcessReadback(pixels, width, height, format, srcSdlFormat, cs.boundReadFboId == 0); } @Override public void readPixels(int x, int y, int width, int height, int format, int type, FloatBuffer pixels) { readPixels(x, y, width, height, format, type, MemoryUtil.memByteBuffer(pixels)); @@ -1715,7 +1871,7 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void getTexImage(int target, int level, int format, int type, ByteBuffer pixels) { final ContextState cs = s(); - final int glId = cs.boundTextures[cs.activeTextureUnit]; + final int glId = boundTextureFor(cs, target); final long texHandle = resourceManager.getTextureHandle(glId); final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(glId); if (meta == null) return; @@ -1730,7 +1886,7 @@ public class SDLGPURenderBackend extends RenderBackend { final ContextState cs = s(); final int pbo = cs.boundPixelPackBuffer; if (pbo == 0) return; - final int glId = cs.boundTextures[cs.activeTextureUnit]; + final int glId = boundTextureFor(cs, target); final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(glId); if (meta == null) return; final ByteBuffer tmp = MemoryUtil.memAlloc(meta.width() * meta.height() * 4); @@ -2272,6 +2428,7 @@ public class SDLGPURenderBackend extends RenderBackend { case GL30.GL_PIXEL_UNPACK_BUFFER -> st.boundPixelUnpackBuffer = buffer; case GL30.GL_PIXEL_PACK_BUFFER -> st.boundPixelPackBuffer = buffer; case GL40.GL_DRAW_INDIRECT_BUFFER -> st.boundIndirectBuffer = buffer; + case GL43.GL_DISPATCH_INDIRECT_BUFFER -> st.boundDispatchIndirectBuffer = buffer; case GL31.GL_COPY_READ_BUFFER -> st.boundCopyReadBuffer = buffer; case GL31.GL_COPY_WRITE_BUFFER -> st.boundCopyWriteBuffer = buffer; case GL43.GL_SHADER_STORAGE_BUFFER -> { @@ -3068,14 +3225,26 @@ public class SDLGPURenderBackend extends RenderBackend { } @Override public void texStorage1D(int target, int levels, int internalFormat, int width) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, 0, internalFormat, width, 1, 1); + return; + } texStorageImpl(st, st.boundTextures[st.activeTextureUnit], target, internalFormat, width, 1, 1, levels); } @Override public void texStorage2D(int target, int levels, int internalFormat, int width, int height) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, 0, internalFormat, width, height, 1); + return; + } texStorageImpl(st, st.boundTextures[st.activeTextureUnit], target, internalFormat, width, height, 1, levels); } @Override public void texStorage3D(int target, int levels, int internalFormat, int width, int height, int depth) { final ContextState st = s(); + if (isProxyTarget(target)) { + recordProxyTexImage(st, target, 0, internalFormat, width, height, depth); + return; + } texStorageImpl(st, st.boundTextures[st.activeTextureUnit], target, internalFormat, width, height, depth, levels); } @Override public void textureStorage1D(int texture, int levels, int internalFormat, int width) { @@ -3166,41 +3335,72 @@ public class SDLGPURenderBackend extends RenderBackend { applyDrawBuffersFromIntBuffer(fbo, bufs, framebuffer == s().boundFboId); } @Override public void blitNamedFramebuffer(int readFramebuffer, int drawFramebuffer, int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) { - final FboState srcFbo = resourceManager.getFbo(readFramebuffer); - final FboState dstFbo = resourceManager.getFbo(drawFramebuffer); - if (srcFbo == null || dstFbo == null) return; + final boolean srcIsFbo0 = readFramebuffer == 0; + final boolean dstIsFbo0 = drawFramebuffer == 0; + final FboState srcFbo = srcIsFbo0 ? null : resourceManager.getFbo(readFramebuffer); + final FboState dstFbo = dstIsFbo0 ? null : resourceManager.getFbo(drawFramebuffer); + if ((srcFbo == null && !srcIsFbo0) || (dstFbo == null && !dstIsFbo0)) return; - final boolean isDepth = (mask & GL11.GL_DEPTH_BUFFER_BIT) != 0; - long srcTex = 0, dstTex = 0; - int dstGlId = 0; - if (isDepth) { - srcTex = srcFbo.depthTexture; - dstTex = dstFbo.depthTexture; - dstGlId = dstFbo.depthGlId; - } else if ((mask & GL11.GL_COLOR_BUFFER_BIT) != 0) { - srcTex = srcFbo.colorTextures[srcFbo.readBufferIndex]; - dstTex = dstFbo.colorTextures[0]; - dstGlId = dstFbo.colorGlIds[0]; + final boolean anyFbo0 = srcIsFbo0 || dstIsFbo0; + final int fbo0Height = frameManager.getFbo0Height(); + if (anyFbo0 && fbo0Height <= 0) return; + + final long colorSrcTex; + final long colorDstTex; + final int colorSrcGlId; + final int colorDstGlId; + if ((mask & GL11.GL_COLOR_BUFFER_BIT) != 0) { + colorSrcTex = srcIsFbo0 ? frameManager.getFbo0Texture() : srcFbo.colorTextures[srcFbo.readBufferIndex]; + colorDstTex = dstIsFbo0 ? frameManager.getFbo0Texture() : dstFbo.colorTextures[0]; + colorSrcGlId = srcIsFbo0 ? 0 : srcFbo.colorGlIds[srcFbo.readBufferIndex]; + colorDstGlId = dstIsFbo0 ? 0 : dstFbo.colorGlIds[0]; + } else { + colorSrcTex = 0L; + colorDstTex = 0L; + colorSrcGlId = 0; + colorDstGlId = 0; } - if (srcTex == 0 || dstTex == 0) return; + final boolean blitColor = colorSrcTex != 0 && colorDstTex != 0 && colorSrcTex != colorDstTex; + + final boolean depthRequested = (mask & (GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT)) != 0; + final long depthSrcTex = (depthRequested && !anyFbo0) ? srcFbo.depthTexture : 0L; + final long depthDstTex = (depthRequested && !anyFbo0) ? dstFbo.depthTexture : 0L; + final boolean blitDepth = depthSrcTex != 0 && depthDstTex != 0 && depthSrcTex != depthDstTex; + + if (!blitColor && !blitDepth) return; final ContextState cs = s(); - fboClearTracker.materializePendingClearForTexture(cs, srcTex); - if (!fboClearTracker.discardPendingClearIfFullyCovered(cs, dstTex, dstX0, dstY0, 0, dstX1 - dstX0, dstY1 - dstY0, resourceManager.getTextureMeta(dstGlId))) { - fboClearTracker.materializePendingClearForTexture(cs, dstTex); + if (dstIsFbo0 || (srcIsFbo0 && (cs.pendingSwapchainClear || cs.pendingSwapchainDepthClear || cs.pendingSwapchainStencilClear))) { + frameManager.ensureFbo0RenderPass(frameManager.frame(), cs); } - if (isDepth) { - textureOps.copyTexture(srcTex, srcX0, srcY0, dstTex, dstX0, dstY0, srcX1 - srcX0, srcY1 - srcY0); - } else { - final ResourceManager.TextureMeta srcMeta = resourceManager.getTextureMeta(srcFbo.colorGlIds[srcFbo.readBufferIndex]); - final ResourceManager.TextureMeta dstMeta = resourceManager.getTextureMeta(dstFbo.colorGlIds[0]); - if (TextureOps.canCopyInsteadOfBlit(srcMeta, dstMeta, srcX1 - srcX0, srcY1 - srcY0, dstX1 - dstX0, dstY1 - dstY0)) { - textureOps.copyTexture(srcTex, srcX0, srcY0, dstTex, dstX0, dstY0, srcX1 - srcX0, srcY1 - srcY0); + final int srcW = srcX1 - srcX0; + final int srcH = srcY1 - srcY0; + final int dstW = dstX1 - dstX0; + final int dstH = dstY1 - dstY0; + final int srcY = srcIsFbo0 ? (fbo0Height - srcY0 - srcH) : srcY0; + final int dstY = dstIsFbo0 ? (fbo0Height - dstY0 - dstH) : dstY0; + final int flipMode = (srcIsFbo0 != dstIsFbo0) ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE; + + if (blitColor) { + resolveBlitClears(cs, colorSrcTex, colorDstTex, colorDstGlId, dstX0, dstY, dstW, dstH); + final ResourceManager.TextureMeta srcMeta = resourceManager.getTextureMeta(colorSrcGlId); + final ResourceManager.TextureMeta dstMeta = resourceManager.getTextureMeta(colorDstGlId); + if (flipMode == SDL_FLIP_NONE && TextureOps.canCopyInsteadOfBlit(srcMeta, dstMeta, srcW, srcH, dstW, dstH)) { + textureOps.copyTexture(colorSrcTex, srcX0, srcY, colorDstTex, 0, dstX0, dstY, srcW, srcH); } else { - textureOps.blitTexture(srcTex, srcX0, srcY0, srcX1 - srcX0, srcY1 - srcY0, dstTex, dstX0, dstY0, dstX1 - dstX0, dstY1 - dstY0, filter); + textureOps.blitTexture(colorSrcTex, srcX0, srcY, srcW, srcH, colorDstTex, 0, dstX0, dstY, dstW, dstH, filter, flipMode); } } + if (blitDepth) { + resolveBlitClears(cs, depthSrcTex, depthDstTex, dstFbo.depthGlId, dstX0, dstY, dstW, dstH); + textureOps.copyTexture(depthSrcTex, srcX0, srcY, depthDstTex, 0, dstX0, dstY, srcW, srcH); + } + } + + private void resolveBlitClears(ContextState cs, long srcTex, long dstTex, int dstGlId, int dstX, int dstY, int dstW, int dstH) { + fboClearTracker.materializePendingClearForTexture(cs, srcTex); + fboClearTracker.resolveDestinationForWrite(cs, dstTex, resourceManager.getTextureMeta(dstGlId), 0, dstX, dstY, 0, dstW, dstH); } @Override public int createBuffers() { return resourceManager.genBuffer(); } @Override public void namedBufferData(int buffer, long size, int usage) { @@ -3280,11 +3480,9 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public int getTextureLevelParameteri(int texture, int level, int pname) { final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(texture); if (meta == null) return 0; - final int mipW = Math.max(1, meta.width() >> level); - final int mipH = Math.max(1, meta.height() >> level); return switch (pname) { - case GL11.GL_TEXTURE_WIDTH -> mipW; - case GL11.GL_TEXTURE_HEIGHT -> mipH; + case GL11.GL_TEXTURE_WIDTH -> PixelOps.mipLevelSize(meta.width(), level); + case GL11.GL_TEXTURE_HEIGHT -> PixelOps.mipLevelSize(meta.height(), level); case GL11.GL_TEXTURE_INTERNAL_FORMAT -> { final int stored = meta.glFormat(); yield TextureInfoCache.isGenericCompressedInternalFormat(stored) ? GL11.GL_RGBA8 : stored; @@ -3296,7 +3494,6 @@ public class SDLGPURenderBackend extends RenderBackend { @Override public int getInteger(int pname) { final ContextState cs = s(); return switch (pname) { - case GL11.GL_MAX_TEXTURE_SIZE -> 16384; case GL20.GL_MAX_TEXTURE_IMAGE_UNITS -> 17; case GL20.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS -> 17; case GL20.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS -> 32; @@ -3354,12 +3551,18 @@ public class SDLGPURenderBackend extends RenderBackend { return switch (pname) { case GL20.GL_MAX_VERTEX_UNIFORM_COMPONENTS, GL20.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS -> MAX_UNIFORM_COMPONENTS_PER_STAGE; case GL30.GL_MAX_ARRAY_TEXTURE_LAYERS -> MAX_ARRAY_TEXTURE_LAYERS; + case GL11.GL_MAX_TEXTURE_SIZE -> MAX_TEXTURE_SIZE; + case GL12.GL_MAX_3D_TEXTURE_SIZE -> MAX_3D_TEXTURE_SIZE; + case GL13.GL_MAX_CUBE_MAP_TEXTURE_SIZE -> MAX_CUBE_MAP_TEXTURE_SIZE; default -> -1; }; } static final int MAX_UNIFORM_COMPONENTS_PER_STAGE = 4096 / Float.BYTES; static final int MAX_ARRAY_TEXTURE_LAYERS = 256; + static final int MAX_TEXTURE_SIZE = 16384; + static final int MAX_CUBE_MAP_TEXTURE_SIZE = 16384; + static final int MAX_3D_TEXTURE_SIZE = 2048; private static int unknownGetInteger(int pname) { synchronized (UNKNOWN_GET_INTEGER_SEEN) { @@ -3479,9 +3682,9 @@ public class SDLGPURenderBackend extends RenderBackend { if (texHandle == 0 || frameManager.getCommandBuffer() == 0) return; final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(texture); if (meta == null || level >= meta.levels()) return; - final int mipW = Math.max(1, meta.width() >> level); - final int mipH = Math.max(1, meta.height() >> level); - final int mipD = Math.max(1, meta.depth() >> level); + final int mipW = PixelOps.mipLevelSize(meta.width(), level); + final int mipH = PixelOps.mipLevelSize(meta.height(), level); + final int mipD = PixelOps.mipLevelSize(meta.depth(), level); final int bpp = PixelOps.sdlFormatTexelBytes(meta.sdlFormat()); if (mipW <= 0 || mipH <= 0 || bpp <= 0) return; @@ -3538,9 +3741,7 @@ public class SDLGPURenderBackend extends RenderBackend { if (srcTex == 0 || dstTex == 0) return; final ContextState copySt = s(); fboClearTracker.materializePendingClearForTexture(copySt, srcTex); - if (dstZ != 0 || !fboClearTracker.discardPendingClearIfFullyCovered(copySt, dstTex, dstX, dstY, dstLevel, srcWidth, srcHeight, resourceManager.getTextureMeta(dstName))) { - fboClearTracker.materializePendingClearForTexture(copySt, dstTex); - } + fboClearTracker.resolveDestinationForWrite(copySt, dstTex, resourceManager.getTextureMeta(dstName), dstLevel, dstX, dstY, dstZ, srcWidth, srcHeight); final long cp = frameManager.ensureCopyPass(); if (cp == 0) return; try (var stack = MemoryStack.stackPush()) { @@ -3685,6 +3886,7 @@ public class SDLGPURenderBackend extends RenderBackend { pipelineApplier.ensureRenderPass(st); if (!frameManager.isRenderPassActive()) return; if (!pipelineApplier.applyPipelineAndState(st)) return; + if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arraysInstanced", mode, count, 0, first, 0); SDL_DrawGPUPrimitives(frameManager.getRenderPass(), count, primcount, first, 0); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/MemoryStackWrapper.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/MemoryStackWrapper.java index 370ee16d0..aaa72c444 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/MemoryStackWrapper.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/device/MemoryStackWrapper.java @@ -1,6 +1,6 @@ package com.gtnewhorizons.angelica.sdlgpu.device; -import com.mitchej123.lwjgl.MemoryStack; +import org.taumc.celeritas.lwjgl.MemoryStack; import java.nio.ByteBuffer; import java.nio.FloatBuffer; diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/ContextState.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/ContextState.java index 086db257d..43e5e5c46 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/ContextState.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/ContextState.java @@ -54,6 +54,28 @@ public final class ContextState { public final PixelStoreState pixelStore = new PixelStoreState(); + public static final class ProxyTextureState { + public int target; + public int width; + public int height; + public int depth; + public int internalFormat; + + public void accept(int target, int width, int height, int depth, int internalFormat) { + this.target = target; + this.width = width; + this.height = height; + this.depth = depth; + this.internalFormat = internalFormat; + } + + public void reject(int target) { + accept(target, 0, 0, 0, 0); + } + } + + public final ProxyTextureState proxyTexture = new ProxyTextureState(); + public static final class VAOState { public final boolean[] attribEnabled = new boolean[MAX_VERTEX_ATTRIBS]; public int attribEnabledMask; @@ -211,6 +233,7 @@ public final class ContextState { public int boundArrayBuffer; public int boundIndirectBuffer; + public int boundDispatchIndirectBuffer; public int boundUniformBuffer; public int boundCopyReadBuffer; public int boundCopyWriteBuffer; @@ -226,6 +249,7 @@ public final class ContextState { case GL30.GL_PIXEL_UNPACK_BUFFER -> boundPixelUnpackBuffer; case GL30.GL_PIXEL_PACK_BUFFER -> boundPixelPackBuffer; case GL40.GL_DRAW_INDIRECT_BUFFER -> boundIndirectBuffer; + case GL43.GL_DISPATCH_INDIRECT_BUFFER -> boundDispatchIndirectBuffer; case GL31.GL_COPY_READ_BUFFER -> boundCopyReadBuffer; case GL31.GL_COPY_WRITE_BUFFER -> boundCopyWriteBuffer; case GL43.GL_SHADER_STORAGE_BUFFER -> boundSSBO; diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/FrameManager.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/FrameManager.java index 7b00a0404..a260cf23d 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/FrameManager.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/frame/FrameManager.java @@ -4,6 +4,7 @@ import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.backend.BackendManager; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.sdlgpu.device.Device; +import com.gtnewhorizons.angelica.sdlgpu.resource.FboState; import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; import com.gtnewhorizons.angelica.sdlgpu.util.MemoryAccess; import com.gtnewhorizons.angelica.sdlgpu.util.ThreadRegistry; @@ -775,7 +776,10 @@ public final class FrameManager { } public int getSwapchainFormat() { - return device.getSwapchainTextureFormat(); + if (device.getClaimedWindow() != 0L) return device.getSwapchainTextureFormat(); + if (resourceManager == null || !finalTarget.isReady()) return 0; + final FboState fbo = resourceManager.getFbo(finalTarget.fboId()); + return fbo != null ? fbo.getColorFormat() : 0; } public boolean isFrameActive() { diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/DrawDispatch.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/DrawDispatch.java index f679e164b..5025f4383 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/DrawDispatch.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/DrawDispatch.java @@ -131,7 +131,7 @@ public final class DrawDispatch { } } - public void drawTriangleFanAsTriangleList(ContextState st, int first, int count) { + public boolean drawTriangleFanAsTriangleList(ContextState st, int first, int count) { final int numTriangles = count - 2; final boolean use32bit = (first + count - 1) > 65535; final int bytesPerIndex = use32bit ? 4 : 2; @@ -180,13 +180,14 @@ public final class DrawDispatch { setPrimitiveTypeForDraw(st, SDL_GPU_PRIMITIVETYPE_TRIANGLELIST); pipelineApplier.ensureRenderPass(st); - if (!frameManager.isRenderPassActive()) return; - if (!pipelineApplier.applyPipelineAndState(st)) return; + if (!frameManager.isRenderPassActive()) return false; + if (!pipelineApplier.applyPipelineAndState(st)) return false; final int elementSize = use32bit ? SDL_GPU_INDEXELEMENTSIZE_32BIT : SDL_GPU_INDEXELEMENTSIZE_16BIT; final long rp = frameManager.getRenderPass(); bindIndexBufferIfChanged(st, rp, st.fanIndexBuffer, elementSize, uploadOffset); SDL_DrawGPUIndexedPrimitives(rp, numTriangles * 3, 1, 0, 0, 0); + return true; } private long createFanIndexBuffer(int capacity) { diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTrace.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTrace.java new file mode 100644 index 000000000..50e3fb965 --- /dev/null +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTrace.java @@ -0,0 +1,211 @@ +package com.gtnewhorizons.angelica.sdlgpu.pipeline; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.ffp.FFPUniformBlock; +import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState; +import com.gtnewhorizons.angelica.sdlgpu.frame.FrameManager; +import com.gtnewhorizons.angelica.sdlgpu.resource.PersistentMapping; +import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; +import com.gtnewhorizons.angelica.sdlgpu.shader.ShaderManager; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +public final class FFPTrace { + private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU"); + private static final String PREFIX = "[FFPTrace] "; + private static final int MAX_KEYS = 512; + + private final ResourceManager resourceManager; + private final LongOpenHashSet seenKeys = new LongOpenHashSet(); + private final StringBuilder sb = new StringBuilder(1024); + private int drawsThisFrame; + private boolean capReported; + + public FFPTrace(ResourceManager resourceManager) { + this.resourceManager = resourceManager; + } + + public synchronized void trace(ContextState st, String kind, int mode, int count, int indexType, int first, int baseVertex) { + drawsThisFrame++; + final long key = key(st); + if (seenKeys.contains(key)) return; + if (seenKeys.size() >= MAX_KEYS) { + if (!capReported) { + capReported = true; + LOG.info("{}key cap {} reached; no further distinct draw states will be logged", PREFIX, MAX_KEYS); + } + return; + } + seenKeys.add(key); + LOG.info(format(st, kind, mode, count, indexType, first, baseVertex)); + } + + public synchronized void frameEnd(FrameManager.FrameState f) { + LOG.info("{}frame {} end: draws={} dropped={}", PREFIX, f.frameNumber, drawsThisFrame, f.droppedDrawsThisFrame); + drawsThisFrame = 0; + } + + private long key(ContextState st) { + final PipelineCache p = st.pipeline; + long h = p.lastKey(); + h = Hashing.fmix64(h, st.boundFboId); + h = Hashing.fmix64(h, Hashing.packHiLo(st.boundTextures[0], st.boundTextures[1])); + h = Hashing.fmix64(h, Hashing.packHiLo(st.scissorEnabled ? 1 : 0, 0)); + h = Hashing.fmix64(h, Hashing.packHiLo(Float.floatToRawIntBits(st.viewportX), Float.floatToRawIntBits(st.viewportY))); + h = Hashing.fmix64(h, Hashing.packHiLo(Float.floatToRawIntBits(st.viewportW), Float.floatToRawIntBits(st.viewportH))); + h = Hashing.fmix64(h, Hashing.packHiLo(st.scissorX, st.scissorY)); + h = Hashing.fmix64(h, Hashing.packHiLo(st.scissorW, st.scissorH)); + return h; + } + + private String format(ContextState st, String kind, int mode, int count, int indexType, int first, int baseVertex) { + final PipelineCache p = st.pipeline; + final ContextState.VAOState vao = st.currentVao; + final ShaderManager.ProgramObject prog = st.boundProgramObj; + sb.setLength(0); + sb.append(PREFIX); + sb.append("kind=").append(kind); + sb.append(" mode=0x").append(Integer.toHexString(mode)); + sb.append(" count=").append(count); + if (indexType != 0) sb.append(" indexType=0x").append(Integer.toHexString(indexType)); + sb.append(" first=").append(first).append(" baseVertex=").append(baseVertex); + + sb.append(" | prog=").append(st.boundProgram); + if (prog == null) { + sb.append(" progObj=null"); + } else { + sb.append(" ffpUboBinding=").append(prog.externalUboBinding); + sb.append(" fragSamplers=").append(prog.fragmentSamplerNames); + sb.append(" vsInputs=["); + boolean firstName = true; + for (int i = 0; i < prog.vertexInputName.length; i++) { + final String n = prog.vertexInputName[i]; + if (n == null) continue; + if (!firstName) sb.append(','); + firstName = false; + sb.append(i).append(':').append(n); + } + sb.append(']'); + } + + sb.append(" | fbo=").append(st.boundFboId); + sb.append(" vp=(").append(st.viewportX).append(',').append(st.viewportY).append(',') + .append(st.viewportW).append(',').append(st.viewportH).append(')'); + sb.append(" scissor=").append(st.scissorEnabled); + if (st.scissorEnabled) { + sb.append('(').append(st.scissorX).append(',').append(st.scissorY).append(',') + .append(st.scissorW).append(',').append(st.scissorH).append(')'); + } + sb.append(" cull=").append(p.cullEnabled).append("/mode=").append(p.cullFaceMode) + .append("/front=").append(p.frontFace).append("/all=").append(p.cullAll); + sb.append(" depth=").append(p.depthTestEnabled).append("/func=").append(p.depthCompareOp) + .append("/write=").append(p.depthWriteEnabled); + sb.append(" blend=").append(p.blendEnabledPerAttachment[0]) + .append("/src=").append(p.srcColorFactor).append("/dst=").append(p.dstColorFactor) + .append("/srcA=").append(p.srcAlphaFactor).append("/dstA=").append(p.dstAlphaFactor) + .append("/op=").append(p.colorBlendOp).append('/').append(p.alphaBlendOp); + sb.append(" colorMask=0x").append(Integer.toHexString(p.colorWriteMask)); + sb.append(" primitiveType=").append(p.primitiveType); + + appendTextureUnit(st, 0); + appendTextureUnit(st, 1); + + sb.append(" | attribs=["); + int enabled = vao.attribEnabledMask; + boolean firstAttrib = true; + while (enabled != 0) { + final int i = Integer.numberOfTrailingZeros(enabled); + enabled &= enabled - 1; + final int b = vao.attribBinding[i]; + if (!firstAttrib) sb.append(' '); + firstAttrib = false; + sb.append(i).append(":size=").append(vao.attribSize[i]) + .append(",type=0x").append(Integer.toHexString(vao.attribType[i])) + .append(",norm=").append(vao.attribNormalized[i]) + .append(",int=").append(vao.attribIsInteger[i]) + .append(",stride=").append(vao.attribStride[i]) + .append(",off=").append(vao.attribOffset[i]) + .append(",buf=").append(vao.bindingBuffer[b]) + .append(",bufOff=").append(vao.bindingOffset[b]); + } + sb.append("] enabledMask=0x").append(Integer.toHexString(vao.attribEnabledMask)) + .append(" shaderInputMask=0x").append(Integer.toHexString(p.shaderInputMask)) + .append(" ebo=").append(vao.elementBuffer); + + appendFfpUniforms(st, prog); + return sb.toString(); + } + + private void appendTextureUnit(ContextState st, int unit) { + final int id = st.boundTextures[unit]; + sb.append(" | tex").append(unit).append("=").append(id); + if (id != 0) { + sb.append(" handle=").append(resourceManager.getTextureHandle(id) != 0); + final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(id); + if (meta == null) { + sb.append(" meta=null"); + } else { + sb.append(" size=").append(meta.width()).append('x').append(meta.height()) + .append(" levels=").append(meta.levels()) + .append(" sdlFormat=").append(meta.sdlFormat()) + .append(" glFormat=0x").append(Integer.toHexString(meta.glFormat())); + } + } + sb.append(" ffpEnabled=").append(GLStateManager.getTextures().getTextureUnitStates(unit).isEnabled()); + } + + private void appendFfpUniforms(ContextState st, ShaderManager.ProgramObject prog) { + if (prog == null) return; + final int binding = prog.externalUboBinding; + if (binding < 0 || binding >= ContextState.MAX_INDEXED_BUFFERS) return; + final int glId = st.boundUboByIndex[binding]; + if (glId == 0) { + sb.append(" | ffpUbo=unbound"); + return; + } + final PersistentMapping pm = resourceManager.getPersistentMapping(glId); + final ByteBuffer buf = pm != null ? pm.staging : resourceManager.getUboShadow(glId); + if (buf == null) { + sb.append(" | ffpUbo=buffer").append(glId).append(" noStaging"); + return; + } + final int base = st.uboRangeOffset[binding]; + sb.append(" | ffpUbo=buffer").append(glId).append(" base=").append(base); + appendUboFloat(buf, base, FFPUniformBlock.ALPHA_REF, " u_AlphaRef="); + appendUboMatrix(buf, base, FFPUniformBlock.MVP_MATRIX, " u_MVPMatrix="); + appendUboMatrix(buf, base, FFPUniformBlock.LIGHTMAP_TEXTURE_MATRIX, " u_LightmapTextureMatrix="); + } + + private static float floatIgnoringLimit(ByteBuffer buf, int at) { + return MemoryUtil.memGetFloat(MemoryUtil.memAddress0(buf) + at); + } + + private void appendUboFloat(ByteBuffer buf, int base, int memberOffset, String label) { + final int at = base + memberOffset; + sb.append(label); + if (at < 0 || at + 4 > buf.capacity()) { + sb.append("oob"); + return; + } + sb.append(floatIgnoringLimit(buf, at)); + } + + private void appendUboMatrix(ByteBuffer buf, int base, int memberOffset, String label) { + final int at = base + memberOffset; + sb.append(label); + if (at < 0 || at + 64 > buf.capacity()) { + sb.append("oob"); + return; + } + sb.append('['); + for (int i = 0; i < 16; i++) { + if (i > 0) sb.append(','); + sb.append(floatIgnoringLimit(buf, at + i * 4)); + } + sb.append(']'); + } +} diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineApplier.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineApplier.java index b96d5494e..6ac6a9436 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineApplier.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineApplier.java @@ -337,6 +337,11 @@ public final class PipelineApplier { st.ringChunkUsedBlocks = 0; } + private int fbo0Height(ContextState st) { + final int h = frameManager.getFbo0Height(); + return h > 0 ? h : (int) (st.viewportY + st.viewportH); + } + public boolean applyPipelineAndState(ContextState st) { return applyPipelineAndState(st, frameManager.frame()); } @@ -435,11 +440,14 @@ public final class PipelineApplier { } if (st.viewportDirty) { - float vpY = st.viewportY; - float vpH = st.viewportH; + final float vpY; + final float vpH; if (renderingToFbo) { vpY = st.viewportY + st.viewportH; vpH = -st.viewportH; + } else { + vpY = fbo0Height(st) - st.viewportY - st.viewportH; + vpH = st.viewportH; } final long addr = st.cachedViewport.address(); MemoryAccess.putFloat(addr + SDL_GPUViewport.X, st.viewportX); @@ -455,15 +463,9 @@ public final class PipelineApplier { if (st.scissorDirty) { final long addr = st.cachedScissor.address(); final int sx, sy, sw, sh; - final int fbHeight; if (st.scissorEnabled) { - if (renderingToFbo) { - fbHeight = (fboState != null && fboState.height > 0) ? fboState.height : (int)(st.viewportY + st.viewportH); - } else { - fbHeight = (int)(st.viewportY + st.viewportH); - } sx = st.scissorX; - sy = fbHeight - st.scissorY - st.scissorH; + sy = renderingToFbo ? st.scissorY : (fbo0Height(st) - st.scissorY - st.scissorH); sw = st.scissorW; sh = st.scissorH; MemoryAccess.putInt(addr + SDL_Rect.X, sx); @@ -471,11 +473,12 @@ public final class PipelineApplier { MemoryAccess.putInt(addr + SDL_Rect.W, sw); MemoryAccess.putInt(addr + SDL_Rect.H, sh); if ((sw <= 0 || sh <= 0) && bogusScissorWarned.add(Hashing.packHiLo(sw, sh))) { - LOG.warn("applyPipelineAndState: scissor degenerate (x={} y={} w={} h={}) - all fragments clipped; boundProgram={} boundFbo={} fbHeight={} scissorEn={} src=[{},{},{},{}]", sx, sy, sw, sh, st.boundProgram, st.boundFboId, fbHeight, st.scissorEnabled, st.scissorX, st.scissorY, st.scissorW, st.scissorH); + LOG.warn("applyPipelineAndState: scissor degenerate (x={} y={} w={} h={}) - all fragments clipped; boundProgram={} boundFbo={} scissorEn={} src=[{},{},{},{}]", sx, sy, sw, sh, st.boundProgram, st.boundFboId, st.scissorEnabled, st.scissorX, st.scissorY, st.scissorW, st.scissorH); } } else { + final int rectY = renderingToFbo ? (int) st.viewportY : (int) (fbo0Height(st) - st.viewportY - st.viewportH); MemoryAccess.putInt(addr + SDL_Rect.X, (int) st.viewportX); - MemoryAccess.putInt(addr + SDL_Rect.Y, (int) st.viewportY); + MemoryAccess.putInt(addr + SDL_Rect.Y, rectY); MemoryAccess.putInt(addr + SDL_Rect.W, (int) st.viewportW); MemoryAccess.putInt(addr + SDL_Rect.H, (int) st.viewportH); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineCache.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineCache.java index f325265ac..f7aee3a82 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineCache.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/PipelineCache.java @@ -185,6 +185,10 @@ public final class PipelineCache { this.maxAttribs = ContextState.MAX_VERTEX_ATTRIBS; } + long lastKey() { + return lastKey; + } + public void markInputDirty() { inputDirty = true; lastKey = 0L; } public boolean markInputDirtyIfLivenessChanged(PipelineStore store, int oldBuffer, int newBuffer) { diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClip.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClip.java new file mode 100644 index 000000000..3ca644a34 --- /dev/null +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClip.java @@ -0,0 +1,71 @@ +package com.gtnewhorizons.angelica.sdlgpu.resource; + +public final class CopyRectClip { + + public static final long EMPTY = 0L; + + private static final long FIELD_MASK = 0xFFFFL; + + private CopyRectClip() {} + + public static long clipCopyRect(int srcX, int srcY, int dstX, int dstY, int width, int height, + int srcWidth, int srcHeight, int dstWidth, int dstHeight) { + int sx = srcX; + int sy = srcY; + int dx = dstX; + int dy = dstY; + int w = width; + int h = height; + if (sx < 0) { + w += sx; + dx -= sx; + sx = 0; + } + if (sy < 0) { + h += sy; + dy -= sy; + sy = 0; + } + if (dx < 0) { + w += dx; + sx -= dx; + dx = 0; + } + if (dy < 0) { + h += dy; + sy -= dy; + dy = 0; + } + if (srcWidth > 0 && sx + w > srcWidth) w = srcWidth - sx; + if (srcHeight > 0 && sy + h > srcHeight) h = srcHeight - sy; + if (dstWidth > 0 && dx + w > dstWidth) w = dstWidth - dx; + if (dstHeight > 0 && dy + h > dstHeight) h = dstHeight - dy; + if (w <= 0 || h <= 0) return EMPTY; + if ((sx | sy | w | h) > FIELD_MASK) return EMPTY; + return (sx & FIELD_MASK) | ((sy & FIELD_MASK) << 16) | ((w & FIELD_MASK) << 32) | ((h & FIELD_MASK) << 48); + } + + public static int srcX(long clip) { + return (int) (clip & FIELD_MASK); + } + + public static int srcY(long clip) { + return (int) ((clip >>> 16) & FIELD_MASK); + } + + public static int width(long clip) { + return (int) ((clip >>> 32) & FIELD_MASK); + } + + public static int height(long clip) { + return (int) ((clip >>> 48) & FIELD_MASK); + } + + public static int dstX(long clip, int origSrcX, int origDstX) { + return origDstX + (srcX(clip) - origSrcX); + } + + public static int dstY(long clip, int origSrcY, int origDstY) { + return origDstY + (srcY(clip) - origSrcY); + } +} diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/FBOClearTracker.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/FBOClearTracker.java index e0b4e3317..c8baf1bca 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/FBOClearTracker.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/FBOClearTracker.java @@ -242,6 +242,12 @@ public final class FBOClearTracker { return true; } + public void resolveDestinationForWrite(ContextState st, long destTex, ResourceManager.TextureMeta destMeta, int level, int dx, int dy, int dz, int w, int h) { + if (dz != 0 || !discardPendingClearIfFullyCovered(st, destTex, dx, dy, level, w, h, destMeta)) { + materializePendingClearForTexture(st, destTex); + } + } + public void materializePendingClearForTexture(ContextState st, long handle) { if (handle == 0) return; final boolean depthPending = st.pendingDepthTextures.remove(handle); diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/PixelOps.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/PixelOps.java index 4bf339a4e..a0da4b849 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/PixelOps.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/PixelOps.java @@ -13,6 +13,12 @@ import static org.lwjgl.sdl.SDLGPU.*; public final class PixelOps { private PixelOps() {} + public static int mipLevelSize(int base, int level) { + if (base <= 0 || level < 0) return 0; + if (level >= Integer.SIZE - 1) return 1; + return Math.max(1, base >> level); + } + public static int defaultMipLevels(int width, int height) { final int max = Math.max(1, Math.max(width, height)); return Math.max(1, 32 - Integer.numberOfLeadingZeros(max)); diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java index 4d31d5002..2026fea03 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/ResourceManager.java @@ -2208,7 +2208,24 @@ public final class ResourceManager { return mapTextureFormat(glFormat, preferredD24, preferredD24S8); } + public boolean isMappableTextureFormat(int glFormat) { + return lookupTextureFormat(glFormat, preferredD24, preferredD24S8) != SDL_GPU_TEXTUREFORMAT_INVALID; + } + public static int mapTextureFormat(int glFormat, int preferredD24, int preferredD24S8) { + final int mapped = lookupTextureFormat(glFormat, preferredD24, preferredD24S8); + if (mapped != SDL_GPU_TEXTUREFORMAT_INVALID) return mapped; + synchronized (UNMAPPED_TEXTURE_FORMAT_SEEN) { + if (UNMAPPED_TEXTURE_FORMAT_SEEN.add(glFormat)) { + LOG.warn("Unmapped GL texture format: 0x{}", Integer.toHexString(glFormat)); + } + } + return SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; + } + + private static final IntOpenHashSet UNMAPPED_TEXTURE_FORMAT_SEEN = new IntOpenHashSet(); + + private static int lookupTextureFormat(int glFormat, int preferredD24, int preferredD24S8) { return switch (glFormat) { case GL11.GL_RGBA, GL11.GL_RGBA8 -> SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; case GL11.GL_RGB, GL11.GL_RGB8 -> SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; // RGB -> RGBA promotion @@ -2262,10 +2279,7 @@ public final class ResourceManager { case GL31.GL_RGBA8_SNORM -> SDL_GPU_TEXTUREFORMAT_R8G8B8A8_SNORM; case GL31.GL_RGBA16_SNORM -> SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM; case GL31.GL_RGB16_SNORM -> SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM; // RGB SNORM -> RGBA SNORM promotion - default -> { - LOG.warn("Unmapped GL texture format: 0x{}", Integer.toHexString(glFormat)); - yield SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; - } + default -> SDL_GPU_TEXTUREFORMAT_INVALID; }; } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/TextureOps.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/TextureOps.java index c3332c55e..6480e61e4 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/TextureOps.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/resource/TextureOps.java @@ -19,6 +19,8 @@ import org.lwjgl.system.MemoryUtil; import java.nio.ByteBuffer; import static org.lwjgl.sdl.SDLGPU.*; +import static org.lwjgl.sdl.SDLSurface.SDL_FLIP_NONE; +import static org.lwjgl.sdl.SDLSurface.SDL_FLIP_VERTICAL; public final class TextureOps { private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU"); @@ -113,6 +115,11 @@ public final class TextureOps { final long destTex = resourceManager.getTextureHandle(destGlId); if (destTex == 0) return; + if (st.boundReadFboId == 0) { + copyFromDefaultFramebuffer(st, destGlId, destTex, level, xoffset, yoffset, x, y, width, height); + return; + } + final FboState fbo = resourceManager.getFbo(st.boundReadFboId); if (fbo == null) return; @@ -121,35 +128,81 @@ public final class TextureOps { final long srcTex = isDepthDest ? fbo.depthTexture : fbo.colorTextures[fbo.readBufferIndex]; if (srcTex == 0) return; - if (isDepthDest) { - copyTexture(srcTex, x, y, destTex, xoffset, yoffset, width, height); - return; - } - - final int srcGlId = fbo.colorGlIds[fbo.readBufferIndex]; + final int srcGlId = isDepthDest ? fbo.depthGlId : fbo.colorGlIds[fbo.readBufferIndex]; final ResourceManager.TextureMeta srcMeta = srcGlId != 0 ? resourceManager.getTextureMeta(srcGlId) : null; - if (srcMeta != null && destMeta != null && srcMeta.sdlFormat() == destMeta.sdlFormat()) { - copyTexture(srcTex, x, y, destTex, xoffset, yoffset, width, height); + final long clip = CopyRectClip.clipCopyRect(x, y, xoffset, yoffset, width, height, srcMeta != null ? srcMeta.width() : fbo.width, srcMeta != null ? srcMeta.height() : fbo.height, levelWidth(destMeta, level), levelHeight(destMeta, level)); + if (clip == CopyRectClip.EMPTY) return; + final int sx = CopyRectClip.srcX(clip); + final int sy = CopyRectClip.srcY(clip); + final int dx = CopyRectClip.dstX(clip, x, xoffset); + final int dy = CopyRectClip.dstY(clip, y, yoffset); + final int w = CopyRectClip.width(clip); + final int h = CopyRectClip.height(clip); + + fboClearTracker.materializePendingClearForTexture(st, srcTex); + fboClearTracker.resolveDestinationForWrite(st, destTex, destMeta, level, dx, dy, 0, w, h); + + if (isDepthDest || (srcMeta != null && destMeta != null && srcMeta.sdlFormat() == destMeta.sdlFormat())) { + copyTexture(srcTex, sx, sy, destTex, level, dx, dy, w, h); } else { - blitTexture(srcTex, x, y, width, height, destTex, xoffset, yoffset, width, height, GL11.GL_NEAREST); + blitTexture(srcTex, sx, sy, w, h, destTex, level, dx, dy, w, h, GL11.GL_NEAREST); } } + private static int levelWidth(ResourceManager.TextureMeta meta, int level) { + return meta != null ? PixelOps.mipLevelSize(meta.width(), level) : 0; + } + + private static int levelHeight(ResourceManager.TextureMeta meta, int level) { + return meta != null ? PixelOps.mipLevelSize(meta.height(), level) : 0; + } + + private void copyFromDefaultFramebuffer(ContextState st, int destGlId, long destTex, int level, int xoffset, int yoffset, int x, int y, int width, int height) { + final ResourceManager.TextureMeta destMeta = resourceManager.getTextureMeta(destGlId); + if (destMeta != null && PixelOps.isDepthFormat(destMeta.sdlFormat())) return; + + final long srcTex = frameManager.getFbo0Texture(); + if (srcTex == 0) return; + final int srcFullW = frameManager.getFbo0Width(); + final int srcFullH = frameManager.getFbo0Height(); + if (srcFullW <= 0 || srcFullH <= 0) return; + + final long clip = CopyRectClip.clipCopyRect(x, y, xoffset, yoffset, width, height, srcFullW, srcFullH, levelWidth(destMeta, level), levelHeight(destMeta, level)); + if (clip == CopyRectClip.EMPTY) return; + final int sx = CopyRectClip.srcX(clip); + final int sy = CopyRectClip.srcY(clip); + final int dx = CopyRectClip.dstX(clip, x, xoffset); + final int dy = CopyRectClip.dstY(clip, y, yoffset); + final int w = CopyRectClip.width(clip); + final int h = CopyRectClip.height(clip); + + if (st.pendingSwapchainClear || st.pendingSwapchainDepthClear || st.pendingSwapchainStencilClear) { + frameManager.ensureFbo0RenderPass(frameManager.frame(), st); + } + fboClearTracker.resolveDestinationForWrite(st, destTex, destMeta, level, dx, dy, 0, w, h); + + blitTexture(srcTex, sx, srcFullH - sy - h, w, h, destTex, level, dx, dy, w, h, GL11.GL_NEAREST, SDL_FLIP_VERTICAL); + } + public static boolean canCopyInsteadOfBlit(ResourceManager.TextureMeta src, ResourceManager.TextureMeta dst, int srcW, int srcH, int dstW, int dstH) { return src != null && dst != null && src.sdlFormat() == dst.sdlFormat() && srcW > 0 && srcH > 0 && srcW == dstW && srcH == dstH; } - public void copyTexture(long srcTex, int srcX, int srcY, long dstTex, int dstX, int dstY, int w, int h) { + public void copyTexture(long srcTex, int srcX, int srcY, long dstTex, int dstLevel, int dstX, int dstY, int w, int h) { final long cp = frameManager.ensureCopyPass(); if (cp == 0) return; try (var stack = MemoryStack.stackPush()) { final var src = SDL_GPUTextureLocation.calloc(stack).texture(srcTex).x(srcX).y(srcY); - final var dst = SDL_GPUTextureLocation.calloc(stack).texture(dstTex).x(dstX).y(dstY); + final var dst = SDL_GPUTextureLocation.calloc(stack).texture(dstTex).mip_level(dstLevel).x(dstX).y(dstY); SDL_CopyGPUTextureToTexture(cp, src, dst, w, h, 1, false); } } - public void blitTexture(long srcTex, int srcX, int srcY, int srcW, int srcH, long dstTex, int dstX, int dstY, int dstW, int dstH, int glFilter) { + public void blitTexture(long srcTex, int srcX, int srcY, int srcW, int srcH, long dstTex, int dstLevel, int dstX, int dstY, int dstW, int dstH, int glFilter) { + blitTexture(srcTex, srcX, srcY, srcW, srcH, dstTex, dstLevel, dstX, dstY, dstW, dstH, glFilter, SDL_FLIP_NONE); + } + + public void blitTexture(long srcTex, int srcX, int srcY, int srcW, int srcH, long dstTex, int dstLevel, int dstX, int dstY, int dstW, int dstH, int glFilter, int flipMode) { frameManager.endCopyPassIfActive(); frameManager.endRenderPassIfActive(); final long cb = frameManager.getCommandBuffer(); @@ -158,9 +211,9 @@ public final class TextureOps { try (var stack = MemoryStack.stackPush()) { final var info = SDL_GPUBlitInfo.calloc(stack); info.source(s -> s.texture(srcTex).x(srcX).y(srcY).w(srcW).h(srcH)); - info.destination(d -> d.texture(dstTex).x(dstX).y(dstY).w(dstW).h(dstH)); + info.destination(d -> d.texture(dstTex).mip_level(dstLevel).x(dstX).y(dstY).w(dstW).h(dstH)); info.filter(glFilter == GL11.GL_LINEAR ? SDL_GPU_FILTER_LINEAR : SDL_GPU_FILTER_NEAREST); - info.flip_mode(0); + info.flip_mode(flipMode); frameManager.noteBlit(); SDL_BlitGPUTexture(cb, info); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java index ef8b0eb33..80ed5ee12 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java @@ -58,6 +58,7 @@ import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntArrays; +import it.unimi.dsi.fastutil.ints.IntIterator; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; @@ -599,29 +600,84 @@ public final class ShaderManager { private static int patchAttribLocations(ByteBuffer spirv, StageReflection vs, Object2IntOpenHashMap bindings, int[] outVecSize, int[] outBaseType, String[] outName, Object2IntOpenHashMap resolvedOut) { final IntBuffer vsBuf = spirv.asIntBuffer(); - int mask = 0; - if (outName != null) Arrays.fill(outName, null); - for (VsInput vi : vs.vsInputs()) { - int finalLoc = vi.originalLocation(); - final int desired = bindings.getInt(vi.name()); - if (desired != -1 && desired != vi.originalLocation()) { - if (vsBuf.get(vi.binaryOffset()) != vi.originalLocation()) { - LOG.error("applyAttribLocations: sanity check failed for '{}' (expected {}, got {})", vi.name(), vi.originalLocation(), vsBuf.get(vi.binaryOffset())); - } else { - vsBuf.put(vi.binaryOffset(), desired); - finalLoc = desired; + final List inputs = vs.vsInputs(); + final int count = inputs.size(); + final int[] finalLoc = new int[count]; + int taken = 0; + + for (final IntIterator it = bindings.values().iterator(); it.hasNext(); ) { + final int bound = it.nextInt(); + if (bound >= 0 && bound < ContextState.MAX_VERTEX_ATTRIBS) taken |= 1 << bound; + } + for (int i = 0; i < count; i++) { + finalLoc[i] = bindings.getInt(inputs.get(i).name()); + } + + int movers = 0; + for (int i = 0; i < count; i++) { + if (finalLoc[i] != -1) continue; + final int original = inputs.get(i).originalLocation(); + if (original >= 0 && original < ContextState.MAX_VERTEX_ATTRIBS && (taken & (1 << original)) == 0) { + finalLoc[i] = original; + taken |= 1 << original; + } else { + movers++; + } + } + + for (int n = 0; n < movers; n++) { + int pick = -1; + for (int i = 0; i < count; i++) { + if (finalLoc[i] == -1 && (pick == -1 || inputs.get(i).originalLocation() < inputs.get(pick).originalLocation())) { + pick = i; } } - if (resolvedOut != null) resolvedOut.put(vi.name(), finalLoc); - if (finalLoc < 0 || finalLoc >= 16) continue; - mask |= (1 << finalLoc); - outVecSize[finalLoc] = vi.vecSize(); - outBaseType[finalLoc] = vi.baseType(); - if (outName != null) outName[finalLoc] = vi.name(); + final int free = Integer.numberOfTrailingZeros(~taken); + if (free >= ContextState.MAX_VERTEX_ATTRIBS) { + LOG.error("applyAttribLocations: no free vertex attribute slot for '{}'; leaving it at {}", inputs.get(pick).name(), inputs.get(pick).originalLocation()); + finalLoc[pick] = inputs.get(pick).originalLocation(); + } else { + finalLoc[pick] = free; + taken |= 1 << free; + } + } + + int mask = 0; + int seen = 0; + if (outName != null) Arrays.fill(outName, null); + for (int i = 0; i < count; i++) { + final VsInput vi = inputs.get(i); + int loc = finalLoc[i]; + if (loc != vi.originalLocation()) { + if (vsBuf.get(vi.binaryOffset()) != vi.originalLocation()) { + LOG.error("applyAttribLocations: sanity check failed for '{}' (expected {}, got {})", vi.name(), vi.originalLocation(), vsBuf.get(vi.binaryOffset())); + loc = vi.originalLocation(); + } else { + vsBuf.put(vi.binaryOffset(), loc); + } + } + finalLoc[i] = loc; + if (resolvedOut != null) resolvedOut.put(vi.name(), loc); + if (loc < 0 || loc >= ContextState.MAX_VERTEX_ATTRIBS) continue; + if ((seen & (1 << loc)) != 0) { + LOG.error("applyAttribLocations: '{}' and '{}' both resolved to location {}", inputNameAtLocation(inputs, finalLoc, i, loc), vi.name(), loc); + } + seen |= 1 << loc; + mask |= 1 << loc; + outVecSize[loc] = vi.vecSize(); + outBaseType[loc] = vi.baseType(); + if (outName != null) outName[loc] = vi.name(); } return mask; } + private static String inputNameAtLocation(List inputs, int[] finalLoc, int limit, int loc) { + for (int i = 0; i < limit; i++) { + if (finalLoc[i] == loc) return inputs.get(i).name(); + } + return ""; + } + public VertexVariant getOrBuildVertexVariant(int program, long key, List attribs) { final ProgramObject prog = programObjects.get(program); if (prog == null || !prog.linked || prog.vertexSource.isEmpty()) return null; diff --git a/sdl-gpu/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService b/sdl-gpu/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService deleted file mode 100644 index 5b37d99b4..000000000 --- a/sdl-gpu/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService +++ /dev/null @@ -1 +0,0 @@ -com.gtnewhorizons.angelica.sdlgpu.SDLGPULWJGLService diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/DispatchIndirectBindingTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/DispatchIndirectBindingTest.java new file mode 100644 index 000000000..ab6d91aba --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/DispatchIndirectBindingTest.java @@ -0,0 +1,44 @@ +package com.gtnewhorizons.angelica.sdlgpu; + +import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.lwjgl.opengl.GL40; +import org.lwjgl.opengl.GL43; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DispatchIndirectBindingTest { + + @AfterEach + void resetContextState() { + final ContextState st = SdlTestRig.contextState(); + st.boundIndirectBuffer = 0; + st.boundDispatchIndirectBuffer = 0; + } + + @Test + void dispatchIndirectHasItsOwnBindingPoint() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final ContextState st = SdlTestRig.contextState(); + + backend.bindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, 11); + backend.bindBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER, 22); + + assertEquals(11, st.getBoundBuffer(GL40.GL_DRAW_INDIRECT_BUFFER)); + assertEquals(22, st.getBoundBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER)); + } + + @Test + void bindingDispatchIndirectLeavesTheDrawIndirectSlotAlone() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final ContextState st = SdlTestRig.contextState(); + + backend.bindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, 33); + backend.bindBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER, 44); + backend.bindBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER, 0); + + assertEquals(33, st.getBoundBuffer(GL40.GL_DRAW_INDIRECT_BUFFER)); + assertEquals(0, st.getBoundBuffer(GL43.GL_DISPATCH_INDIRECT_BUFFER)); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureLimitsTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureLimitsTest.java new file mode 100644 index 000000000..bfd2b90d8 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureLimitsTest.java @@ -0,0 +1,154 @@ +package com.gtnewhorizons.angelica.sdlgpu; + +import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.lwjgl.opengl.EXTTextureFilterAnisotropic; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL14; +import org.lwjgl.opengl.GL30; +import org.lwjgl.opengl.GL40; + +import java.nio.ByteBuffer; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ProxyTextureLimitsTest { + + @AfterEach + void resetContextState() { + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + st.boundTextures[0] = 0; + st.proxyTexture.reject(0); + } + + @Test + void threeDimensionalProxyIsBoundedByTheVolumeLimit() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final int limit = SDLGPURenderBackend.MAX_3D_TEXTURE_SIZE; + + backend.texImage3D(GL12.GL_PROXY_TEXTURE_3D, 0, GL11.GL_RGBA, limit, limit, limit, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(limit, backend.getTexLevelParameteri(GL12.GL_PROXY_TEXTURE_3D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(limit, backend.getTexLevelParameteri(GL12.GL_PROXY_TEXTURE_3D, 0, GL12.GL_TEXTURE_DEPTH)); + + backend.texImage3D(GL12.GL_PROXY_TEXTURE_3D, 0, GL11.GL_RGBA, limit * 2, limit, limit, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL12.GL_PROXY_TEXTURE_3D, 0, GL11.GL_TEXTURE_WIDTH)); + + backend.texImage3D(GL12.GL_PROXY_TEXTURE_3D, 0, GL11.GL_RGBA, limit, limit, limit * 2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL12.GL_PROXY_TEXTURE_3D, 0, GL12.GL_TEXTURE_DEPTH)); + } + + @Test + void everyProxyDimensionLimitIsAlsoQueryableAsADeviceLimit() { + assertEquals(16384, SDLGPURenderBackend.deviceLimit(GL11.GL_MAX_TEXTURE_SIZE)); + assertEquals(2048, SDLGPURenderBackend.deviceLimit(GL12.GL_MAX_3D_TEXTURE_SIZE)); + assertEquals(16384, SDLGPURenderBackend.deviceLimit(GL13.GL_MAX_CUBE_MAP_TEXTURE_SIZE)); + + assertEquals(16384, SDLGPURenderBackend.proxyDimensionLimit(GL11.GL_PROXY_TEXTURE_2D)); + assertEquals(16384, SDLGPURenderBackend.proxyDimensionLimit(GL11.GL_PROXY_TEXTURE_1D)); + assertEquals(2048, SDLGPURenderBackend.proxyDimensionLimit(GL12.GL_PROXY_TEXTURE_3D)); + assertEquals(16384, SDLGPURenderBackend.proxyDimensionLimit(GL13.GL_PROXY_TEXTURE_CUBE_MAP)); + assertEquals(16384, SDLGPURenderBackend.proxyDimensionLimit(GL40.GL_PROXY_TEXTURE_CUBE_MAP_ARRAY)); + } + + @Test + void oneDimensionalArrayLayersUseTheLayerLimitNotTheDimensionLimit() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final int layers = SDLGPURenderBackend.MAX_ARRAY_TEXTURE_LAYERS; + + backend.texImage2D(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 0, GL11.GL_RGBA, 512, layers, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(512, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(layers, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 0, GL11.GL_TEXTURE_HEIGHT)); + + backend.texImage2D(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 0, GL11.GL_RGBA, 512, layers + 1, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 0, GL11.GL_TEXTURE_WIDTH)); + } + + @Test + void arrayLayerCountDoesNotHalvePerLevel() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + + backend.texImage2D(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 2, GL11.GL_RGBA, 128, 64, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(128, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 2, GL11.GL_TEXTURE_WIDTH)); + assertEquals(64, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_1D_ARRAY, 2, GL11.GL_TEXTURE_HEIGHT)); + } + + @Test + void aProbedLevelIsAcceptedOnItsLevelZeroEquivalentSize() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 3, GL11.GL_RGBA, 1024, 512, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(1024, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 3, GL11.GL_TEXTURE_WIDTH)); + assertEquals(512, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 3, GL11.GL_TEXTURE_HEIGHT)); + assertEquals(8192, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + } + + @Test + void aProbedLevelWhoseBaseWouldOverflowTheLimitIsRejected() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 2, GL11.GL_RGBA, SDLGPURenderBackend.MAX_TEXTURE_SIZE, 4, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 2, GL11.GL_TEXTURE_WIDTH)); + + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 40, GL11.GL_RGBA, 4, 4, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 40, GL11.GL_TEXTURE_WIDTH)); + } + + @Test + void levelZeroEquivalentSizeRejectsOverflowAndNonsense() { + assertEquals(4, SDLGPURenderBackend.levelZeroEquivalentSize(4, 0)); + assertEquals(32, SDLGPURenderBackend.levelZeroEquivalentSize(4, 3)); + assertEquals(0, SDLGPURenderBackend.levelZeroEquivalentSize(4, -1)); + assertEquals(0, SDLGPURenderBackend.levelZeroEquivalentSize(0, 2)); + assertEquals(0, SDLGPURenderBackend.levelZeroEquivalentSize(4, 31)); + assertEquals(0, SDLGPURenderBackend.levelZeroEquivalentSize(4, 30)); + assertEquals(1 << 30, SDLGPURenderBackend.levelZeroEquivalentSize(1, 30)); + } + + @Test + void proxyTargetsAnswerTheGlParameterDefaults() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final int target = GL11.GL_PROXY_TEXTURE_2D; + + assertEquals(GL11.GL_NEAREST_MIPMAP_LINEAR, backend.getTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER)); + assertEquals(GL11.GL_LINEAR, backend.getTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER)); + assertEquals(GL11.GL_REPEAT, backend.getTexParameteri(target, GL11.GL_TEXTURE_WRAP_S)); + assertEquals(GL11.GL_REPEAT, backend.getTexParameteri(target, GL11.GL_TEXTURE_WRAP_T)); + assertEquals(GL11.GL_REPEAT, backend.getTexParameteri(target, GL12.GL_TEXTURE_WRAP_R)); + assertEquals(0, backend.getTexParameteri(target, GL12.GL_TEXTURE_BASE_LEVEL)); + assertEquals(1000, backend.getTexParameteri(target, GL12.GL_TEXTURE_MAX_LEVEL)); + assertEquals(-1000.0f, backend.getTexParameterf(target, GL12.GL_TEXTURE_MIN_LOD)); + assertEquals(1000.0f, backend.getTexParameterf(target, GL12.GL_TEXTURE_MAX_LOD)); + assertEquals(0.0f, backend.getTexParameterf(target, GL14.GL_TEXTURE_LOD_BIAS)); + assertEquals(1.0f, backend.getTexParameterf(target, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT)); + } + + @Test + void proxyParameterDefaultsIgnoreTheBoundTexturesOwnState() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + st.boundTextures[0] = 8181; + + backend.texParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + assertEquals(GL11.GL_NEAREST, backend.getTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER)); + assertEquals(GL11.GL_NEAREST_MIPMAP_LINEAR, backend.getTexParameteri(GL11.GL_PROXY_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER)); + } + + @Test + void proxyTargetsNeverReceiveParameterWrites() { + final SDLGPURenderBackend backend = new SDLGPURenderBackend(); + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + st.boundTextures[0] = 8282; + + backend.texParameteri(GL11.GL_PROXY_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + backend.texParameterf(GL11.GL_PROXY_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, 4.0f); + + assertEquals(GL11.GL_NEAREST_MIPMAP_LINEAR, backend.getTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER)); + assertEquals(0.0f, backend.getTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS)); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureTargetTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureTargetTest.java new file mode 100644 index 000000000..31420d2fe --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/ProxyTextureTargetTest.java @@ -0,0 +1,159 @@ +package com.gtnewhorizons.angelica.sdlgpu; + +import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL30; +import org.lwjgl.opengl.GL31; +import org.lwjgl.opengl.GL32; +import org.lwjgl.opengl.GL40; + +import java.nio.ByteBuffer; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ProxyTextureTargetTest { + + private static final int[] PROXY_TARGETS = { + 0x8063, 0x8064, 0x8070, 0x851B, 0x8C19, 0x8C1B, 0x84F7, 0x900B, 0x9101, 0x9103 + }; + + private static SDLGPURenderBackend backend; + + @BeforeAll + static void setUp() { + backend = new SDLGPURenderBackend(); + } + + @AfterEach + void resetContextState() { + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + st.boundTextures[0] = 0; + st.boundTextures[1] = 0; + st.proxyTexture.reject(0); + } + + @Test + void everyProxyTargetIsRecognized() { + for (int target : PROXY_TARGETS) { + assertTrue(SDLGPURenderBackend.isProxyTarget(target), () -> "0x" + Integer.toHexString(target)); + } + } + + @Test + void realTargetsAreNotProxies() { + final int[] real = { + GL11.GL_TEXTURE_1D, GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_3D, GL13.GL_TEXTURE_CUBE_MAP, + GL30.GL_TEXTURE_1D_ARRAY, GL30.GL_TEXTURE_2D_ARRAY, GL31.GL_TEXTURE_RECTANGLE, + GL40.GL_TEXTURE_CUBE_MAP_ARRAY, GL32.GL_TEXTURE_2D_MULTISAMPLE, GL32.GL_TEXTURE_2D_MULTISAMPLE_ARRAY, + 0 + }; + for (int target : real) { + assertFalse(SDLGPURenderBackend.isProxyTarget(target), () -> "0x" + Integer.toHexString(target)); + } + } + + static Stream levelSizeCases() { + return Stream.of( + Arguments.of("level0", 16384, 0, 16384), + Arguments.of("level1", 16384, 1, 8192), + Arguments.of("level14FloorsAtOne", 16384, 14, 1), + Arguments.of("level15FloorsAtOne", 16384, 15, 1), + Arguments.of("level64FloorsAtOne", 16384, 64, 1), + Arguments.of("baseOneFloorsAtOne", 1, 3, 1), + Arguments.of("oddBaseRoundsDown", 6, 1, 3), + Arguments.of("rejectedProxyBaseZeroLevel0", 0, 0, 0), + Arguments.of("rejectedProxyBaseZeroLevel5", 0, 5, 0), + Arguments.of("rejectedProxyNegativeLevel", 16384, -1, 0)); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("levelSizeCases") + void levelSizeMatchesExpectation(String label, int base, int level, int expected) { + assertEquals(expected, SDLGPURenderBackend.proxyLevelSize(base, level), label); + } + + @Test + void proxyTexImage2DRecordsDimensionsWithoutTouchingTheBoundTexture() { + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 1; + st.boundTextures[1] = 4242; + + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, 16384, 16384, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + assertEquals(4242, st.boundTextures[1]); + assertEquals(16384, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(16384, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT)); + assertEquals(2048, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 3, GL11.GL_TEXTURE_WIDTH)); + assertEquals(GL11.GL_RGBA, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT)); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_BORDER)); + } + + @Test + void oversizedProxyTexImage2DZeroesEveryProxyField() { + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, 16384, 16384, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, 32768, 32768, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT)); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT)); + } + + @Test + void proxyArrayTargetIsLimitedByLayerCount() { + final int layers = SDLGPURenderBackend.MAX_ARRAY_TEXTURE_LAYERS; + + backend.texImage3D(GL30.GL_PROXY_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, 64, 64, layers, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(layers, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_2D_ARRAY, 0, GL12.GL_TEXTURE_DEPTH)); + + backend.texImage3D(GL30.GL_PROXY_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, 64, 64, layers + 1, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + assertEquals(0, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_2D_ARRAY, 0, GL12.GL_TEXTURE_DEPTH)); + assertEquals(0, backend.getTexLevelParameteri(GL30.GL_PROXY_TEXTURE_2D_ARRAY, 0, GL11.GL_TEXTURE_WIDTH)); + } + + @Test + void proxyStateIsScopedToTheProbedTarget() { + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, 256, 256, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + assertEquals(256, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(0, backend.getTexLevelParameteri(GL13.GL_PROXY_TEXTURE_CUBE_MAP, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(0, backend.getTexLevelParameteri(GL13.GL_PROXY_TEXTURE_CUBE_MAP, 0, GL11.GL_TEXTURE_HEIGHT)); + assertEquals(0, backend.getTexLevelParameteri(GL13.GL_PROXY_TEXTURE_CUBE_MAP, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT)); + assertEquals(0, backend.getTexLevelParameteri(GL12.GL_PROXY_TEXTURE_3D, 0, GL12.GL_TEXTURE_DEPTH)); + } + + @Test + void unmappableProxyFormatIsRejected() { + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, 256, 256, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + backend.texImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, 0x1BADF00D, 256, 256, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + assertEquals(0, backend.getTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT)); + } + + @Test + void proxyBindIsInert() { + final ContextState st = SdlTestRig.contextState(); + st.activeTextureUnit = 0; + st.boundTextures[0] = 77; + + for (int target : PROXY_TARGETS) { + backend.bindTexture(target, 99); + assertEquals(77, st.boundTextures[0], () -> "0x" + Integer.toHexString(target)); + } + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLServiceGatingTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLServiceGatingTest.java index efcc7194c..2e471cf99 100644 --- a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLServiceGatingTest.java +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/SDLGPULWJGLServiceGatingTest.java @@ -1,17 +1,34 @@ package com.gtnewhorizons.angelica.sdlgpu; -import com.mitchej123.lwjgl.LWJGLService; +import com.gtnewhorizons.angelica.config.SystemProperties; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; class SDLGPULWJGLServiceGatingTest { @Test void anUnengagedSdlDeclinesAndNeverArmsTheFallbackTripwire() { + final String key = SystemProperties.KEY_CELERITAS_LWJGL_SERVICE; assertFalse(SDLGPUGate.isEngaged(), "test JVM must not have an engaged SDL GPU device"); - assertEquals(LWJGLService.PRIORITY_UNAVAILABLE, new SDLGPULWJGLService().getPriority()); - assertFalse(SDLGPULWJGLService.isOfferedAsAvailable(), "declining must leave fallBackToGL() usable"); + assertNull(System.getProperty(key), "an unengaged SDL must not bind the celeritas service"); + + try { + SDLGPULWJGLService.bind(); + assertEquals(SDLGPULWJGLService.class.getName(), System.getProperty(key)); + + assertThrows( + IllegalStateException.class, + SDLGPULWJGLService::create, + "constructing the service while SDL GPU is unengaged must fail loudly"); + } finally { + SDLGPULWJGLService.unbind(); + } + + assertNull(System.getProperty(key)); + assertFalse(SDLGPULWJGLService.isConstructed(), "declining must leave fallBackToGL() usable"); } } diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlCopyBlitTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlCopyBlitTest.java new file mode 100644 index 000000000..8fe273cf9 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlCopyBlitTest.java @@ -0,0 +1,246 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; +import com.gtnewhorizons.angelica.sdlgpu.frame.FrameManager; +import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL14; +import org.lwjgl.opengl.GL30; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.SIZE; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Tag("glsm-sdl") +@ExtendWith(GlsmSdlFrameExtension.class) +class GlsmSdlCopyBlitTest { + + private static final int RED = 0xFFFF0000; + private static final int GREEN = 0xFF00FF00; + private static final int BLUE = 0xFF0000FF; + + private static final int SCISSOR_ROWS = 16; + private static final byte SENTINEL_BYTE = 0x7B; + private static final int SENTINEL = 0x7B7B7B7B; + + private static FrameManager frameManager; + private static ResourceManager resourceManager; + + @BeforeAll + static void boot() { + GlsmSdlHeadlessRig.boot(); + frameManager = Reflect.get(BackendManager.RENDER_BACKEND, "frameManager"); + resourceManager = Reflect.get(BackendManager.RENDER_BACKEND, "resourceManager"); + frameManager.destroyFinalTarget(); + frameManager.finalTarget().create(SDLGPUGate.device(), resourceManager, SIZE, SIZE, resourceManager.mapTextureFormat(GL11.GL_RGBA8)); + assertEquals(SIZE, frameManager.getFbo0Height()); + } + + @AfterAll + static void dropFinalTarget() { + GlsmSdlHeadlessRig.endFrame(); + if (frameManager != null) frameManager.destroyFinalTarget(); + } + + private static void assertRow(int[] pixels, int y, int expected, String label) { + for (int x = 0; x < SIZE; x += 8) { + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, x, y, expected, label + " row " + y); + } + } + + @Test + void scissorOnTheUserFboClipsTheGlBottomRows() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 1.0f, 1.0f); + + GLStateManager.glEnable(GL11.GL_SCISSOR_TEST); + GLStateManager.glScissor(0, 0, SIZE, SCISSOR_ROWS); + GlsmSdlHeadlessRig.solidQuad(1.0f, 0.0f, 0.0f); + GLStateManager.glDisable(GL11.GL_SCISSOR_TEST); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + assertRow(pixels, 0, RED, "inside the scissor"); + assertRow(pixels, SCISSOR_ROWS - 1, RED, "inside the scissor"); + assertRow(pixels, SCISSOR_ROWS, BLUE, "outside the scissor"); + assertRow(pixels, SIZE - 1, BLUE, "outside the scissor"); + } + + private static int createTwoLevelTexture(int level0Argb, int level1Argb) { + final int id = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 1); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MIN_LOD, 0.0f); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LOD, 1.0f); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, 0.0f); + uploadLevel(0, SIZE, level0Argb); + uploadLevel(1, SIZE / 2, level1Argb); + return id; + } + + private static void uploadLevel(int level, int size, int argb) { + final ByteBuffer texels = MemoryUtil.memAlloc(size * size * 4); + try { + for (int i = 0; i < size * size; i++) { + texels.put(i * 4, (byte) ((argb >> 16) & 0xFF)); + texels.put(i * 4 + 1, (byte) ((argb >> 8) & 0xFF)); + texels.put(i * 4 + 2, (byte) (argb & 0xFF)); + texels.put(i * 4 + 3, (byte) ((argb >>> 24) & 0xFF)); + } + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, level, GL11.GL_RGBA8, size, size, 0, + GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texels); + } finally { + MemoryUtil.memFree(texels); + } + } + + private static int copyGreenIntoLevelOne() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.solidQuad(0.0f, 1.0f, 0.0f); + + final int texture = createTwoLevelTexture(RED, BLUE); + GLStateManager.glCopyTexSubImage2D(GL11.GL_TEXTURE_2D, 1, 0, 0, 0, 0, SIZE / 2, SIZE / 2); + return texture; + } + + @Test + void copyTexSubImage2DWritesTheRequestedMipLevel() { + final int texture = copyGreenIntoLevelOne(); + + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.texturedQuad(texture, 0.5f); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE / 2, GREEN, "minified sample of level 1"); + } + + @Test + void copyTexSubImage2DLeavesTheOtherMipLevelsUntouched() { + final int texture = copyGreenIntoLevelOne(); + + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.texturedQuad(texture, 1.0f); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 4, SIZE / 4, RED, "magnified sample of the level 0 region the copy would land in"); + } + + @Test + void readPixelsCrossingTheTopEdgeReturnsTheInRangeRows() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 1.0f, 1.0f); + GlsmSdlHeadlessRig.halfQuad(0.0f, 1.0f, 0.0f, 1.0f, 0.0f); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(0, SIZE / 2, SIZE, SIZE, SENTINEL_BYTE); + assertRow(pixels, 0, GREEN, "first in-range row"); + assertRow(pixels, SIZE / 2 - 1, GREEN, "last in-range row"); + assertRow(pixels, SIZE / 2, SENTINEL, "first out-of-range row"); + assertRow(pixels, SIZE - 1, SENTINEL, "last out-of-range row"); + } + + private static int createSecondaryFbo() { + final int color = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, color); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, SIZE, SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + final int depth = GLStateManager.glGenRenderbuffers(); + GLStateManager.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depth); + GLStateManager.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, SIZE, SIZE); + + final int fbo = GLStateManager.glGenFramebuffers(); + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo); + GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, color, 0); + GLStateManager.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, depth); + return fbo; + } + + @Test + void blitFramebufferWithColorAndDepthBitsMovesColor() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 1.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.solidQuad(0.0f, 1.0f, 0.0f); + + final int destination = createSecondaryFbo(); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + GLStateManager.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); + GlsmSdlHeadlessRig.solidQuad(0.0f, 0.0f, 1.0f); + + GlsmSdlHeadlessRig.bindTarget(); + GLStateManager.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, destination); + GLStateManager.glBlitFramebuffer(0, 0, SIZE, SIZE, 0, 0, SIZE, SIZE, + GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, GL11.GL_NEAREST); + + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, destination); + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE / 2, GREEN, "color aspect of a color+depth blit"); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, 4, SIZE - 5, GREEN, "color aspect of a color+depth blit"); + } + + @Test + void blitFramebufferIntoFbo0SurvivesTheFramesFirstFbo0Pass() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 1.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.solidQuad(0.0f, 1.0f, 0.0f); + + GLStateManager.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GLStateManager.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, 0); + GLStateManager.glBlitFramebuffer(0, 0, SIZE, SIZE, 0, 0, SIZE, SIZE, GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST); + + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + GLStateManager.glClearColor(1.0f, 0.0f, 0.0f, 1.0f); + GLStateManager.glEnable(GL11.GL_SCISSOR_TEST); + GLStateManager.glScissor(0, 0, 4, 4); + GlsmSdlHeadlessRig.solidQuad(1.0f, 0.0f, 0.0f); + GLStateManager.glDisable(GL11.GL_SCISSOR_TEST); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE / 2, GREEN, "blit into fbo0 survived the first fbo0 pass"); + } + + @Test + void blitFramebufferFromFbo0ToTheUserFboKeepsGlOrientation() { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.solidQuad(0.0f, 0.0f, 0.0f); + + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + GLStateManager.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); + GlsmSdlHeadlessRig.halfQuad(-1.0f, 0.0f, 0.0f, 1.0f, 0.0f); + GlsmSdlHeadlessRig.halfQuad(0.0f, 1.0f, 1.0f, 0.0f, 0.0f); + + GlsmSdlHeadlessRig.bindTarget(); + GLStateManager.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, 0); + GLStateManager.glBlitFramebuffer(0, 0, SIZE, SIZE, 0, 0, SIZE, SIZE, GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST); + + GlsmSdlHeadlessRig.bindTarget(); + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, 4, GREEN, "blitted fbo0 bottom"); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE - 5, RED, "blitted fbo0 top"); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE / 2 - 4, GREEN, "blitted fbo0 below the split"); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE / 2 + 4, RED, "blitted fbo0 above the split"); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFbo0ViewportTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFbo0ViewportTest.java new file mode 100644 index 000000000..9aae0cc84 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFbo0ViewportTest.java @@ -0,0 +1,141 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; +import com.gtnewhorizons.angelica.sdlgpu.frame.FrameManager; +import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL30; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.SIZE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +@Tag("glsm-sdl") +@ExtendWith(GlsmSdlFrameExtension.class) +class GlsmSdlFbo0ViewportTest { + + private static final int FBO0_W = 64; + private static final int FBO0_H = 96; + private static final int VIEW = 32; + + private static final int RED = 0xFFFF0000; + private static final int GREEN = 0xFF00FF00; + private static final int BLUE = 0xFF0000FF; + + private static FrameManager frameManager; + private static ResourceManager resourceManager; + + @BeforeAll + static void boot() { + GlsmSdlHeadlessRig.boot(); + frameManager = Reflect.get(BackendManager.RENDER_BACKEND, "frameManager"); + resourceManager = Reflect.get(BackendManager.RENDER_BACKEND, "resourceManager"); + frameManager.destroyFinalTarget(); + frameManager.finalTarget().create(SDLGPUGate.device(), resourceManager, FBO0_W, FBO0_H, resourceManager.mapTextureFormat(GL11.GL_RGBA8)); + assertEquals(FBO0_H, frameManager.getFbo0Height()); + } + + @AfterAll + static void dropFinalTarget() { + GlsmSdlHeadlessRig.endFrame(); + if (frameManager != null) frameManager.destroyFinalTarget(); + } + + private static void bindFbo0WithSmallViewport() { + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); + GLStateManager.glViewport(0, 0, VIEW, VIEW); + GLStateManager.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); + } + + private static int createCopyTarget() { + final int id = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, VIEW, VIEW, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + return id; + } + + private static void drawTextureOverTarget(int texture) { + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + GlsmSdlHeadlessRig.texturedQuad(texture, 1.0f); + } + + @Test + void partialViewportDrawCoversOnlyTheGlViewportRows() { + bindFbo0WithSmallViewport(); + GlsmSdlHeadlessRig.solidQuad(1.0f, 0.0f, 0.0f); + + GlsmSdlHeadlessRig.assertUniform(GlsmSdlHeadlessRig.readTarget(0, 0, VIEW, VIEW), RED, "inside the viewport"); + GlsmSdlHeadlessRig.assertUniform(GlsmSdlHeadlessRig.readTarget(0, FBO0_H - VIEW, VIEW, VIEW), BLUE, "above the viewport"); + } + + @Test + void copyTexSubImage2DFromAPartialViewportKeepsGlOrientation() { + bindFbo0WithSmallViewport(); + GlsmSdlHeadlessRig.halfQuad(-1.0f, 0.0f, 0.0f, 1.0f, 0.0f); + GlsmSdlHeadlessRig.halfQuad(0.0f, 1.0f, 1.0f, 0.0f, 0.0f); + + final int copy = createCopyTarget(); + GLStateManager.glCopyTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, 0, 0, VIEW, VIEW); + + drawTextureOverTarget(copy); + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, 4, GREEN, "sampled copy bottom"); + GlsmSdlHeadlessRig.assertPixel(pixels, SIZE, SIZE / 2, SIZE - 5, RED, "sampled copy top"); + } + + @Test + void copyTexImage2DAllocatesTheDestinationLevel() { + bindFbo0WithSmallViewport(); + GlsmSdlHeadlessRig.solidQuad(1.0f, 0.0f, 0.0f); + + final int copy = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, copy); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0); + assertNull(resourceManager.getTextureMeta(copy), "destination was allocated before the copy"); + + GLStateManager.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 0, 0, VIEW, VIEW, 0); + + final ResourceManager.TextureMeta meta = resourceManager.getTextureMeta(copy); + assertNotNull(meta, "copyTexImage2D did not allocate the destination"); + assertEquals(VIEW, meta.width()); + assertEquals(VIEW, meta.height()); + + GlsmSdlHeadlessRig.endFrame(); + final ByteBuffer out = MemoryUtil.memAlloc(VIEW * VIEW * 4); + try { + GLStateManager.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, out); + for (int i = 0; i < VIEW * VIEW; i++) { + final int idx = i; + assertEquals((byte) 0xFF, out.get(i * 4), () -> "R at texel " + idx); + assertEquals((byte) 0x00, out.get(i * 4 + 1), () -> "G at texel " + idx); + assertEquals((byte) 0x00, out.get(i * 4 + 2), () -> "B at texel " + idx); + assertEquals((byte) 0xFF, out.get(i * 4 + 3), () -> "A at texel " + idx); + } + } finally { + MemoryUtil.memFree(out); + } + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFfpTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFfpTest.java new file mode 100644 index 000000000..4c99306c8 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFfpTest.java @@ -0,0 +1,213 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.SIZE; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.describe; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.pixelAt; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Tag("glsm-sdl") +@ExtendWith(GlsmSdlFrameExtension.class) +class GlsmSdlFfpTest { + + private static final int TEXEL = 0xFFFF4020; + private static final int LIGHTMAP_SIZE = 16; + + private static int baseTexture; + private static int lightmapTexture; + + @BeforeAll + static void boot() { + GlsmSdlHeadlessRig.boot(); + GlsmSdlHeadlessRig.beginFrame(); + baseTexture = GlsmSdlHeadlessRig.createSolidTexture(TEXEL); + lightmapTexture = createLightmapTexture(); + GlsmSdlHeadlessRig.endFrame(); + } + + private static int createLightmapTexture() { + final int id = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, LIGHTMAP_SIZE, LIGHTMAP_SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + final ByteBuffer texels = MemoryUtil.memAlloc(LIGHTMAP_SIZE * LIGHTMAP_SIZE * 4); + try { + for (int y = 0; y < LIGHTMAP_SIZE; y++) { + for (int x = 0; x < LIGHTMAP_SIZE; x++) { + final boolean brightest = (x == 0 && y == 0) || (x == LIGHTMAP_SIZE - 1 && y == LIGHTMAP_SIZE - 1); + texels.put((byte) (brightest ? 255 : 0)); + texels.put((byte) 255); + texels.put((byte) (brightest ? 255 : 0)); + texels.put((byte) 255); + } + } + texels.flip(); + GLStateManager.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, LIGHTMAP_SIZE, LIGHTMAP_SIZE, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texels); + } finally { + MemoryUtil.memFree(texels); + } + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + return id; + } + + private static void bindBaseTexture() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, baseTexture); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + } + + private static void enableLightmapUnit(boolean vanillaTextureMatrix) { + GlsmSdlHeadlessRig.enableLightmapUnit(lightmapTexture, vanillaTextureMatrix); + } + + private static void quadUnit0Only() { + GLStateManager.glBegin(GL11.GL_QUADS); + GlsmSdlHeadlessRig.emitVertex(-1.0f, -1.0f, 0.0f, 0.0f, false, (short) 0); + GlsmSdlHeadlessRig.emitVertex(1.0f, -1.0f, 1.0f, 0.0f, false, (short) 0); + GlsmSdlHeadlessRig.emitVertex(1.0f, 1.0f, 1.0f, 1.0f, false, (short) 0); + GlsmSdlHeadlessRig.emitVertex(-1.0f, 1.0f, 0.0f, 1.0f, false, (short) 0); + GLStateManager.glEnd(); + } + + private static void quadTwoUnits(boolean vanillaBrightness) { + final short brightness = (short) (vanillaBrightness ? 240 : 0); + GLStateManager.glBegin(GL11.GL_QUADS); + GlsmSdlHeadlessRig.emitVertex(-1.0f, -1.0f, 0.0f, 0.0f, true, brightness); + GlsmSdlHeadlessRig.emitVertex(1.0f, -1.0f, 1.0f, 0.0f, true, brightness); + GlsmSdlHeadlessRig.emitVertex(1.0f, 1.0f, 1.0f, 1.0f, true, brightness); + GlsmSdlHeadlessRig.emitVertex(-1.0f, 1.0f, 0.0f, 1.0f, true, brightness); + GLStateManager.glEnd(); + } + + private static void assertTexelEverywhere(String label) { + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + final int center = pixelAt(pixels, SIZE, SIZE / 2, SIZE / 2); + final int corner = pixelAt(pixels, SIZE, 2, 2); + assertEquals(TEXEL, center, () -> label + ": center " + describe(center)); + assertEquals(TEXEL, corner, () -> label + ": corner " + describe(corner)); + } + + @Test + void singleUnitTexturedQuadShowsTheTexel() { + bindBaseTexture(); + quadUnit0Only(); + assertTexelEverywhere("single unit"); + } + + @Test + void twoUnitTerrainQuadWithVanillaLightmapMatrixShowsTheTexel() { + bindBaseTexture(); + enableLightmapUnit(true); + GlsmSdlHeadlessRig.terrainRenderState(); + quadTwoUnits(true); + assertTexelEverywhere("two units, vanilla lightmap matrix"); + } + + @Test + void twoUnitTerrainQuadWithIdentityLightmapMatrixShowsTheTexel() { + bindBaseTexture(); + enableLightmapUnit(false); + GlsmSdlHeadlessRig.terrainRenderState(); + quadTwoUnits(false); + assertTexelEverywhere("two units, identity lightmap matrix, zero brightness"); + } + + @Test + void twoUnitTerrainQuadThroughDisplayListsShowsTheTexel() { + bindBaseTexture(); + enableLightmapUnit(true); + GlsmSdlHeadlessRig.terrainRenderState(); + + final int list = GLStateManager.glGenLists(1); + GLStateManager.glNewList(list, GL11.GL_COMPILE); + GLStateManager.glPushMatrix(); + GLStateManager.glTranslatef(0.0f, 0.0f, 0.0f); + quadTwoUnits(true); + GLStateManager.glPopMatrix(); + GLStateManager.glEndList(); + + GLStateManager.glCallList(list); + assertTexelEverywhere("two units, glCallList"); + + GlsmSdlHeadlessRig.beginFrame(); + GlsmSdlHeadlessRig.bindTarget(); + GlsmSdlHeadlessRig.clearTo(0.0f, 0.0f, 0.0f, 1.0f); + + final IntBuffer lists = BufferUtils.createIntBuffer(1); + lists.put(0, list); + lists.position(0); + GLStateManager.glCallLists(lists); + assertTexelEverywhere("two units, glCallLists"); + } + + @Test + void litTexturedQuadIsShaded() { + bindBaseTexture(); + enableGuiStandardItemLighting(); + GLStateManager.glEnable(GL12.GL_RESCALE_NORMAL); + quadUnit0Only(); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + final int center = pixelAt(pixels, SIZE, SIZE / 2, SIZE / 2); + final int red = (center >> 16) & 0xFF; + assertEquals(255, (center >>> 24) & 0xFF, () -> "lit quad alpha: " + describe(center)); + assertTrue(red > 100, () -> "lit quad red: " + describe(center)); + assertTrue(red < ((TEXEL >> 16) & 0xFF), () -> "lit quad is not attenuated: " + describe(center)); + } + + private static void enableGuiStandardItemLighting() { + GLStateManager.glPushMatrix(); + GLStateManager.glRotatef(-30.0f, 0.0f, 1.0f, 0.0f); + GLStateManager.glRotatef(165.0f, 1.0f, 0.0f, 0.0f); + + GLStateManager.glEnable(GL11.GL_LIGHTING); + GLStateManager.glEnable(GL11.GL_LIGHT0); + GLStateManager.glEnable(GL11.GL_LIGHT1); + GLStateManager.glEnable(GL11.GL_COLOR_MATERIAL); + GLStateManager.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE); + + final float[] first = normalize(0.2f, 1.0f, -0.7f); + final float[] second = normalize(-0.2f, 1.0f, 0.7f); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, floats(first[0], first[1], first[2], 0.0f)); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, floats(0.6f, 0.6f, 0.6f, 1.0f)); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, floats(0.0f, 0.0f, 0.0f, 1.0f)); + GLStateManager.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, floats(0.0f, 0.0f, 0.0f, 1.0f)); + GLStateManager.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, floats(second[0], second[1], second[2], 0.0f)); + GLStateManager.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, floats(0.6f, 0.6f, 0.6f, 1.0f)); + GLStateManager.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, floats(0.0f, 0.0f, 0.0f, 1.0f)); + GLStateManager.glLight(GL11.GL_LIGHT1, GL11.GL_SPECULAR, floats(0.0f, 0.0f, 0.0f, 1.0f)); + GLStateManager.glShadeModel(GL11.GL_FLAT); + GLStateManager.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, floats(0.4f, 0.4f, 0.4f, 1.0f)); + + GLStateManager.glPopMatrix(); + } + + private static float[] normalize(float x, float y, float z) { + final float len = (float) Math.sqrt(x * x + y * y + z * z); + return new float[]{ x / len, y / len, z / len }; + } + + private static FloatBuffer floats(float a, float b, float c, float d) { + final FloatBuffer buf = BufferUtils.createFloatBuffer(4); + buf.put(a).put(b).put(c).put(d); + buf.flip(); + return buf; + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFrameExtension.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFrameExtension.java new file mode 100644 index 000000000..fa263e19c --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlFrameExtension.java @@ -0,0 +1,22 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public final class GlsmSdlFrameExtension implements BeforeEachCallback, AfterEachCallback { + + @Override + public void beforeEach(ExtensionContext context) { + GlsmSdlHeadlessRig.beginFrameAndReset(); + } + + @Override + public void afterEach(ExtensionContext context) { + GlsmSdlHeadlessRig.endFrame(); + assertTrue(GlsmSdlRedirectAgent.failures().isEmpty(), + () -> "redirect agent recorded failures: " + GlsmSdlRedirectAgent.failures()); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlHeadlessRig.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlHeadlessRig.java new file mode 100644 index 000000000..46b440792 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlHeadlessRig.java @@ -0,0 +1,300 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; +import com.gtnewhorizons.angelica.glsm.hooks.GLSMInitConfig; +import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPURenderBackend; +import com.gtnewhorizons.angelica.sdlgpu.SdlTestRig; +import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineCache; +import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL30; +import org.lwjgl.sdl.SDLError; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public final class GlsmSdlHeadlessRig { + + public static final int SIZE = 64; + + private static boolean booted; + private static int fbo; + private static int colorTexture; + private static int depthRenderbuffer; + + private GlsmSdlHeadlessRig() {} + + public static void boot() { + if (booted) return; + SdlTestRig.initSdlVideo(); + assumeTrue(SDLGPUGate.device().createDevice(), () -> "no SDL GPU device: " + SDLError.SDL_GetError()); + Reflect.setStatic(SDLGPUGate.class, "engaged", true); + + final RenderBackend backend = BackendManager.RENDER_BACKEND; + assumeTrue(backend instanceof SDLGPURenderBackend, () -> "selected backend is " + backend.getName()); + + backend.onPostWindowCreate(0L); + Reflect.setStaticFinal(GLStateManager.class, "MainThread", Thread.class, Thread.currentThread()); + + GLStateManager.initialize(GLSMInitConfig.builder() + .displaySize(SIZE, SIZE) + .directDrawer(TessellatorStreamingDrawer::drawDirect) + .streamingDrawerDestroy(TessellatorStreamingDrawer::destroy) + .build()); + GLStateManager.setRunningSplash(false); + GLStateManager.markSplashComplete("glsmSdlHeadlessRig"); + + assertTrue(GlsmSdlRedirectAgent.transformedCount() > 0, "redirect agent transformed no classes"); + assertTrue(GlsmSdlRedirectAgent.failures().isEmpty(), + () -> "redirect agent recorded failures: " + GlsmSdlRedirectAgent.failures()); + + createTarget(); + booted = true; + } + + private static void createTarget() { + beginFrame(); + colorTexture = GLStateManager.glGenTextures(); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, colorTexture); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, SIZE, SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + depthRenderbuffer = GLStateManager.glGenRenderbuffers(); + GLStateManager.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthRenderbuffer); + GLStateManager.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, SIZE, SIZE); + + fbo = GLStateManager.glGenFramebuffers(); + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo); + GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colorTexture, 0); + GLStateManager.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, depthRenderbuffer); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + + PipelineCache.setSwapchainFormats(new int[]{ colorTargetSdlFormat() }); + endFrame(); + } + + private static int colorTargetSdlFormat() { + final ResourceManager rm = Reflect.get(BackendManager.RENDER_BACKEND, "resourceManager"); + final ResourceManager.TextureMeta meta = rm.getTextureMeta(colorTexture); + return meta != null ? meta.sdlFormat() : 0; + } + + private static boolean frameOpen; + + public static void beginFrame() { + if (frameOpen) return; + BackendManager.RENDER_BACKEND.onFrameBegin(); + frameOpen = true; + } + + public static void endFrame() { + if (!frameOpen) return; + frameOpen = false; + BackendManager.RENDER_BACKEND.onFrameEnd(); + } + + public static void bindTarget() { + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo); + GLStateManager.glViewport(0, 0, SIZE, SIZE); + } + + public static void clearTo(float r, float g, float b, float a) { + GLStateManager.glClearColor(r, g, b, a); + GLStateManager.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); + } + + public static void beginFrameAndReset() { + beginFrame(); + resetState(); + bindTarget(); + clearTo(0.0f, 0.0f, 0.0f, 1.0f); + } + + public static void resetState() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.glDisable(GL11.GL_TEXTURE_2D); + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glLoadIdentity(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glDisable(GL11.GL_TEXTURE_2D); + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glLoadIdentity(); + + GLStateManager.glDisable(GL11.GL_ALPHA_TEST); + GLStateManager.glDisable(GL11.GL_FOG); + GLStateManager.glDisable(GL11.GL_LIGHTING); + GLStateManager.glDisable(GL11.GL_LIGHT0); + GLStateManager.glDisable(GL11.GL_LIGHT1); + GLStateManager.glDisable(GL11.GL_COLOR_MATERIAL); + GLStateManager.glDisable(GL12.GL_RESCALE_NORMAL); + GLStateManager.glDisable(GL11.GL_DEPTH_TEST); + GLStateManager.glDisable(GL11.GL_BLEND); + GLStateManager.glDisable(GL11.GL_CULL_FACE); + GLStateManager.glDisable(GL11.GL_SCISSOR_TEST); + GLStateManager.glCullFace(GL11.GL_BACK); + GLStateManager.glShadeModel(GL11.GL_SMOOTH); + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLStateManager.glNormal3f(0.0f, 0.0f, 1.0f); + + GLStateManager.glMatrixMode(GL11.GL_PROJECTION); + GLStateManager.glLoadIdentity(); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glLoadIdentity(); + } + + public static void terrainRenderState() { + GLStateManager.glEnable(GL11.GL_ALPHA_TEST); + GLStateManager.glAlphaFunc(GL11.GL_GREATER, 0.1f); + GLStateManager.glEnable(GL11.GL_FOG); + GLStateManager.glFogi(GL11.GL_FOG_MODE, GL11.GL_LINEAR); + GLStateManager.glFogf(GL11.GL_FOG_START, 1.0e6f); + GLStateManager.glFogf(GL11.GL_FOG_END, 1.0e7f); + } + + public static int createSolidTexture(int argb) { + final int id = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + final ByteBuffer texels = MemoryUtil.memAlloc(2 * 2 * 4); + try { + for (int i = 0; i < 4; i++) { + texels.put((byte) ((argb >> 16) & 0xFF)); + texels.put((byte) ((argb >> 8) & 0xFF)); + texels.put((byte) (argb & 0xFF)); + texels.put((byte) ((argb >>> 24) & 0xFF)); + } + texels.flip(); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 2, 2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texels); + } finally { + MemoryUtil.memFree(texels); + } + return id; + } + + public static void enableLightmapUnit(int lightmapTexture, boolean vanillaTextureMatrix) { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glLoadIdentity(); + if (vanillaTextureMatrix) { + final float scale = 0.00390625f; + GLStateManager.glScalef(scale, scale, scale); + GLStateManager.glTranslatef(8.0f, 8.0f, 8.0f); + } + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + } + + public static void emitVertex(float x, float y, float u, float v, boolean lightmap, short brightness) { + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLStateManager.glNormal3f(0.0f, 0.0f, 1.0f); + GLStateManager.glTexCoord2f(u, v); + if (lightmap) { + GLStateManager.glMultiTexCoord2s(GL13.GL_TEXTURE1, brightness, brightness); + } + GLStateManager.glVertex3f(x, y, 0.0f); + } + + private static void vertex(float x, float y, float r, float g, float b) { + GLStateManager.glColor4f(r, g, b, 1.0f); + GLStateManager.glNormal3f(0.0f, 0.0f, 1.0f); + GLStateManager.glVertex3f(x, y, 0.0f); + } + + private static void texVertex(float x, float y, float u, float v) { + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLStateManager.glNormal3f(0.0f, 0.0f, 1.0f); + GLStateManager.glTexCoord2f(u, v); + GLStateManager.glVertex3f(x, y, 0.0f); + } + + public static void halfQuad(float y0, float y1, float r, float g, float b) { + GLStateManager.glBegin(GL11.GL_QUADS); + vertex(-1.0f, y0, r, g, b); + vertex(1.0f, y0, r, g, b); + vertex(1.0f, y1, r, g, b); + vertex(-1.0f, y1, r, g, b); + GLStateManager.glEnd(); + } + + public static void solidQuad(float r, float g, float b) { + halfQuad(-1.0f, 1.0f, r, g, b); + } + + public static void texturedQuad(int texture, float extent) { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLStateManager.glBegin(GL11.GL_QUADS); + texVertex(-extent, -extent, 0.0f, 0.0f); + texVertex(extent, -extent, 1.0f, 0.0f); + texVertex(extent, extent, 1.0f, 1.0f); + texVertex(-extent, extent, 0.0f, 1.0f); + GLStateManager.glEnd(); + } + + public static int[] readTarget() { + return readTarget(0, 0, SIZE, SIZE, (byte) 0); + } + + public static int[] readTarget(int x, int y, int w, int h) { + return readTarget(x, y, w, h, (byte) 0); + } + + public static int[] readTarget(int x, int y, int w, int h, byte fill) { + endFrame(); + final ByteBuffer pixels = MemoryUtil.memAlloc(w * h * 4); + try { + for (int i = 0; i < w * h * 4; i++) pixels.put(i, fill); + GLStateManager.glReadPixels(x, y, w, h, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels); + final int[] out = new int[w * h]; + for (int i = 0; i < out.length; i++) { + final int r = pixels.get(i * 4) & 0xFF; + final int g = pixels.get(i * 4 + 1) & 0xFF; + final int b = pixels.get(i * 4 + 2) & 0xFF; + final int a = pixels.get(i * 4 + 3) & 0xFF; + out[i] = (a << 24) | (r << 16) | (g << 8) | b; + } + return out; + } finally { + MemoryUtil.memFree(pixels); + } + } + + public static int pixelAt(int[] pixels, int rowStride, int x, int y) { + return pixels[y * rowStride + x]; + } + + public static String describe(int argb) { + return String.format("a=%d r=%d g=%d b=%d", (argb >>> 24) & 0xFF, (argb >> 16) & 0xFF, (argb >> 8) & 0xFF, argb & 0xFF); + } + + public static void assertPixel(int[] pixels, int stride, int x, int y, int expected, String label) { + final int got = pixelAt(pixels, stride, x, y); + assertEquals(expected, got, () -> label + " at (" + x + "," + y + "): " + describe(got)); + } + + public static void assertUniform(int[] pixels, int expected, String label) { + for (int i = 0; i < pixels.length; i++) { + final int got = pixels[i]; + final int idx = i; + assertEquals(expected, got, () -> label + " at index " + idx + ": " + describe(got)); + } + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlProxyTextureProbeTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlProxyTextureProbeTest.java new file mode 100644 index 000000000..84aa8dc9d --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlProxyTextureProbeTest.java @@ -0,0 +1,171 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.SIZE; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.describe; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.pixelAt; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Tag("glsm-sdl") +@ExtendWith(GlsmSdlFrameExtension.class) +class GlsmSdlProxyTextureProbeTest { + + private static final int TEXEL = 0xFFFF4020; + private static final int LIGHTMAP_SIZE = 16; + + private static final int[] PROXY_TARGETS = { + 0x8063, 0x8064, 0x8070, 0x851B, 0x8C19, 0x8C1B, 0x84F7, 0x900B, 0x9101, 0x9103 + }; + + private static int baseTexture; + private static int lightmapTexture; + + @BeforeAll + static void boot() { + GlsmSdlHeadlessRig.boot(); + GlsmSdlHeadlessRig.beginFrame(); + baseTexture = GlsmSdlHeadlessRig.createSolidTexture(TEXEL); + lightmapTexture = createLightmapTexture(); + GlsmSdlHeadlessRig.endFrame(); + } + + @Test + void vanillaMaximumTextureSizeProbeLeavesTheBoundLightmapIntact() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture); + + final ResourceManager rm = resourceManager(); + final long handleBefore = rm.getTextureHandle(lightmapTexture); + + assertEquals(16384, vanillaGetGLMaximumTextureSize()); + + final ResourceManager.TextureMeta meta = rm.getTextureMeta(lightmapTexture); + assertNotNull(meta, "lightmap meta was dropped by the proxy probe"); + assertEquals(LIGHTMAP_SIZE, meta.width(), "lightmap width after probe"); + assertEquals(LIGHTMAP_SIZE, meta.height(), "lightmap height after probe"); + assertEquals(handleBefore, rm.getTextureHandle(lightmapTexture), "lightmap handle after probe"); + assertEquals(LIGHTMAP_SIZE, GLStateManager.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH)); + + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + } + + @Test + void proxyTargetCallsLeaveTheBoundTextureIntact() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture); + + final ResourceManager rm = resourceManager(); + final long handleBefore = rm.getTextureHandle(lightmapTexture); + final ResourceManager.TextureMeta before = rm.getTextureMeta(lightmapTexture); + assertNotNull(before, "lightmap meta missing before the proxy calls"); + + for (int target : PROXY_TARGETS) { + final String where = "0x" + Integer.toHexString(target); + GLStateManager.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + GLStateManager.glTexParameterf(target, GL12.GL_TEXTURE_MIN_LOD, 4.0f); + GLStateManager.glTexSubImage2D(target, 0, 0, 0, 1, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + GLStateManager.glCopyTexSubImage2D(target, 0, 0, 0, 0, 0, 1, 1); + GLStateManager.glCopyTexImage2D(target, 0, GL11.GL_RGBA, 0, 0, 4, 4, 0); + GLStateManager.glGenerateMipmap(target); + + final ResourceManager.TextureMeta after = rm.getTextureMeta(lightmapTexture); + assertNotNull(after, where); + assertEquals(before.width(), after.width(), where); + assertEquals(before.height(), after.height(), where); + assertEquals(before.levels(), after.levels(), where); + assertEquals(before.glFormat(), after.glFormat(), where); + assertEquals(handleBefore, rm.getTextureHandle(lightmapTexture), where); + } + + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + } + + @Test + void proxyProbeDuringTerrainDrawStillShowsTheTexel() { + bindBaseTexture(); + enableLightmapUnit(); + + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + assertEquals(16384, vanillaGetGLMaximumTextureSize()); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + + GlsmSdlHeadlessRig.terrainRenderState(); + quadTwoUnits(); + + final int[] pixels = GlsmSdlHeadlessRig.readTarget(); + final int center = pixelAt(pixels, SIZE, SIZE / 2, SIZE / 2); + final int corner = pixelAt(pixels, SIZE, 2, 2); + assertEquals(TEXEL, center, () -> "center " + describe(center)); + assertEquals(TEXEL, corner, () -> "corner " + describe(corner)); + } + + private static int vanillaGetGLMaximumTextureSize() { + for (int i = 16384; i > 0; i >>= 1) { + GLStateManager.glTexImage2D(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_RGBA, i, i, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + final int j = GLStateManager.glGetTexLevelParameteri(GL11.GL_PROXY_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH); + if (j != 0) { + return i; + } + } + return -1; + } + + private static ResourceManager resourceManager() { + return Reflect.get(BackendManager.RENDER_BACKEND, "resourceManager"); + } + + private static int createLightmapTexture() { + final int id = GLStateManager.glGenTextures(); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, LIGHTMAP_SIZE, LIGHTMAP_SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + final ByteBuffer texels = MemoryUtil.memAlloc(LIGHTMAP_SIZE * LIGHTMAP_SIZE * 4); + try { + for (int i = 0; i < LIGHTMAP_SIZE * LIGHTMAP_SIZE; i++) { + texels.put((byte) 255).put((byte) 255).put((byte) 255).put((byte) 255); + } + texels.flip(); + GLStateManager.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, LIGHTMAP_SIZE, LIGHTMAP_SIZE, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texels); + } finally { + MemoryUtil.memFree(texels); + } + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + return id; + } + + private static void bindBaseTexture() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, baseTexture); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + } + + private static void enableLightmapUnit() { + GlsmSdlHeadlessRig.enableLightmapUnit(lightmapTexture, true); + } + + private static void quadTwoUnits() { + GLStateManager.glBegin(GL11.GL_QUADS); + GlsmSdlHeadlessRig.emitVertex(-1.0f, -1.0f, 0.0f, 0.0f, true, (short) 240); + GlsmSdlHeadlessRig.emitVertex(1.0f, -1.0f, 1.0f, 0.0f, true, (short) 240); + GlsmSdlHeadlessRig.emitVertex(1.0f, 1.0f, 1.0f, 1.0f, true, (short) 240); + GlsmSdlHeadlessRig.emitVertex(-1.0f, 1.0f, 0.0f, 1.0f, true, (short) 240); + GLStateManager.glEnd(); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlVanillaTextureTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlVanillaTextureTest.java new file mode 100644 index 000000000..58eba97af --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/glsm/GlsmSdlVanillaTextureTest.java @@ -0,0 +1,271 @@ +package com.gtnewhorizons.angelica.sdlgpu.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL14; + +import java.nio.IntBuffer; + +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.SIZE; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.describe; +import static com.gtnewhorizons.angelica.sdlgpu.glsm.GlsmSdlHeadlessRig.pixelAt; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Tag("glsm-sdl") +@ExtendWith(GlsmSdlFrameExtension.class) +class GlsmSdlVanillaTextureTest { + + private static final int GL_TEXTURE_MAX_ANISOTROPY_EXT = 34046; + + private static final int ATLAS_SIZE = 64; + private static final int ATLAS_MIPMAP_LEVELS = 4; + private static final int[] LEVEL_COLORS = { + 0xFFFF4020, + 0xFF00FF00, + 0xFF0000FF, + 0xFF20C080, + 0xFFFFFFFF, + }; + + private static final int LIGHTMAP_SIZE = 16; + private static final int CLEAR_COLOR = 0xFF000000; + + private static int atlasTexture; + private static int lightmapTexture; + + @BeforeAll + static void boot() { + GlsmSdlHeadlessRig.boot(); + GlsmSdlHeadlessRig.beginFrame(); + atlasTexture = createMipmappedAtlas(); + lightmapTexture = allocateDynamicLightmap(); + GlsmSdlHeadlessRig.endFrame(); + } + + private static int createMipmappedAtlas() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + final int id = GLStateManager.glGenTextures(); + allocateTextureImpl(id, ATLAS_MIPMAP_LEVELS, ATLAS_SIZE, ATLAS_SIZE, 1.0f); + for (int level = 0; level <= ATLAS_MIPMAP_LEVELS; level++) { + uploadTextureSub(level, LEVEL_COLORS[level], ATLAS_SIZE >> level, ATLAS_SIZE >> level, true); + } + return id; + } + + private static int allocateDynamicLightmap() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + final int id = GLStateManager.glGenTextures(); + allocateTextureImpl(id, 0, LIGHTMAP_SIZE, LIGHTMAP_SIZE, 1.0f); + return id; + } + + private static void allocateTextureImpl(int id, int maxLevel, int width, int height, float aniso) { + GLStateManager.glDeleteTextures(id); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, id); + + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); + + if (maxLevel > 0) { + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, maxLevel); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MIN_LOD, 0.0f); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LOD, (float) maxLevel); + GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, 0.0f); + } + + for (int level = 0; level <= maxLevel; level++) { + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, level, GL11.GL_RGBA, width >> level, height >> level, 0, + GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer) null); + } + } + + private static void uploadTextureSub(int level, int argb, int width, int height, boolean mipmapped) { + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, + mipmapped ? GL11.GL_NEAREST_MIPMAP_LINEAR : GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); + + final IntBuffer data = BufferUtils.createIntBuffer(width * height); + for (int i = 0; i < width * height; i++) { + data.put(i, argb); + } + data.position(0); + GLStateManager.glTexSubImage2D(GL11.GL_TEXTURE_2D, level, 0, 0, width, height, + GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, data); + } + + private static void uploadLightmapFrame(int argb) { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture); + uploadTextureSub(0, argb, LIGHTMAP_SIZE, LIGHTMAP_SIZE, false); + } + + private static void bindAtlasUnit0() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, atlasTexture); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + } + + private static void enableLightmap() { + GLStateManager.glActiveTexture(GL13.GL_TEXTURE1); + GLStateManager.glMatrixMode(GL11.GL_TEXTURE); + GLStateManager.glLoadIdentity(); + final float scale = 0.00390625f; + GLStateManager.glScalef(scale, scale, scale); + GLStateManager.glTranslatef(8.0f, 8.0f, 8.0f); + GLStateManager.glMatrixMode(GL11.GL_MODELVIEW); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, lightmapTexture); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); + GLStateManager.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLStateManager.glEnable(GL11.GL_TEXTURE_2D); + GLStateManager.glActiveTexture(GL13.GL_TEXTURE0); + } + + private static void quad(float extent, boolean lightmap, boolean counterClockwise) { + GLStateManager.glBegin(GL11.GL_QUADS); + if (counterClockwise) { + GlsmSdlHeadlessRig.emitVertex(-extent, -extent, 0.0f, 0.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(extent, -extent, 1.0f, 0.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(extent, extent, 1.0f, 1.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(-extent, extent, 0.0f, 1.0f, lightmap, (short) 240); + } else { + GlsmSdlHeadlessRig.emitVertex(-extent, extent, 0.0f, 1.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(extent, extent, 1.0f, 1.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(extent, -extent, 1.0f, 0.0f, lightmap, (short) 240); + GlsmSdlHeadlessRig.emitVertex(-extent, -extent, 0.0f, 0.0f, lightmap, (short) 240); + } + GLStateManager.glEnd(); + } + + private static int center() { + return pixelAt(GlsmSdlHeadlessRig.readTarget(), SIZE, SIZE / 2, SIZE / 2); + } + + private static void assertColorNear(int expected, int actual, int tolerance, String label) { + assertEquals(255, (actual >>> 24) & 0xFF, () -> label + ": alpha " + describe(actual)); + for (int shift = 0; shift <= 16; shift += 8) { + final int e = (expected >> shift) & 0xFF; + final int a = (actual >> shift) & 0xFF; + assertTrue(Math.abs(e - a) <= tolerance, + () -> label + ": expected " + describe(expected) + " got " + describe(actual)); + } + } + + private static int modulate(int a, int b) { + int out = 0xFF000000; + for (int shift = 0; shift <= 16; shift += 8) { + final int product = Math.round((((a >> shift) & 0xFF) / 255.0f) * (((b >> shift) & 0xFF) / 255.0f) * 255.0f); + out |= product << shift; + } + return out; + } + + @Test + void mipmappedAtlasMagnifiedShowsLevelZero() { + bindAtlasUnit0(); + quad(1.0f, false, true); + + final int actual = center(); + assertColorNear(LEVEL_COLORS[0], actual, 1, "magnified level 0"); + } + + @Test + void mipmappedAtlasMinifiedShowsALowerLevel() { + bindAtlasUnit0(); + quad(0.125f, false, true); + + final int actual = center(); + assertEquals(255, (actual >>> 24) & 0xFF, () -> "minified alpha: " + describe(actual)); + assertNotEquals(CLEAR_COLOR, actual, () -> "minified is the clear color: " + describe(actual)); + assertNotEquals(LEVEL_COLORS[0], actual, () -> "minified is level 0: " + describe(actual)); + assertColorNear(LEVEL_COLORS[3], actual, 1, "minified"); + } + + @Test + void perFrameDynamicLightmapModulatesTheAtlas() { + final int[] frames = { 0xFFFFFFFF, 0xFFFF8040, 0xFF40FF80 }; + for (int frame = 0; frame < frames.length; frame++) { + if (frame > 0) GlsmSdlHeadlessRig.beginFrameAndReset(); + + uploadLightmapFrame(frames[frame]); + bindAtlasUnit0(); + enableLightmap(); + GlsmSdlHeadlessRig.terrainRenderState(); + quad(1.0f, true, true); + + final int expected = modulate(LEVEL_COLORS[0], frames[frame]); + final int actual = center(); + assertColorNear(expected, actual, 2, "dynamic lightmap frame " + frame); + } + } + + @Test + void pushAttribWrappedCulledDrawKeepsFrontFace() { + uploadLightmapFrame(0xFFFFFFFF); + bindAtlasUnit0(); + enableLightmap(); + GlsmSdlHeadlessRig.terrainRenderState(); + + GLStateManager.glEnable(GL11.GL_CULL_FACE); + GLStateManager.glCullFace(GL11.GL_BACK); + GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); + quad(1.0f, true, true); + GLStateManager.glPopAttrib(); + + final int actual = center(); + assertColorNear(LEVEL_COLORS[0], actual, 2, "front-face winding kept"); + } + + @Test + void pushAttribWrappedCulledDrawDropsBackFace() { + uploadLightmapFrame(0xFFFFFFFF); + bindAtlasUnit0(); + enableLightmap(); + GlsmSdlHeadlessRig.terrainRenderState(); + + GLStateManager.glEnable(GL11.GL_CULL_FACE); + GLStateManager.glCullFace(GL11.GL_BACK); + GLStateManager.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); + quad(1.0f, true, false); + GLStateManager.glPopAttrib(); + + final int actual = center(); + assertEquals(CLEAR_COLOR, actual, () -> "back-face winding was not culled: " + describe(actual)); + } + + @Test + void displayListWithBindingsOutsideShowsTheModulatedTexel() { + final int list = GLStateManager.glGenLists(1); + GLStateManager.glNewList(list, GL11.GL_COMPILE); + quad(1.0f, true, true); + GLStateManager.glEndList(); + + uploadLightmapFrame(0xFFFF8040); + bindAtlasUnit0(); + enableLightmap(); + GlsmSdlHeadlessRig.terrainRenderState(); + + GLStateManager.glPushMatrix(); + GLStateManager.glTranslatef(0.0f, 0.0f, 0.0f); + GLStateManager.glCallList(list); + GLStateManager.glPopMatrix(); + + final int expected = modulate(LEVEL_COLORS[0], 0xFFFF8040); + final int actual = center(); + assertColorNear(expected, actual, 2, "display list with bindings outside"); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTraceTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTraceTest.java new file mode 100644 index 000000000..f9dc50f3d --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/pipeline/FFPTraceTest.java @@ -0,0 +1,31 @@ +package com.gtnewhorizons.angelica.sdlgpu.pipeline; + +import com.gtnewhorizons.angelica.glsm.testutil.Reflect; +import org.junit.jupiter.api.Test; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class FFPTraceTest { + + @Test + void uboFloatReadIgnoresATruncatedLimit() { + final ByteBuffer buf = MemoryUtil.memAlloc(64); + try { + for (int i = 0; i < 16; i++) buf.putFloat(i * 4, i + 0.5f); + buf.position(0).limit(4); + assertEquals(0.5f, readUboFloat(buf, 0)); + assertEquals(12.5f, readUboFloat(buf, 48)); + assertEquals(15.5f, readUboFloat(buf, 60)); + assertEquals(4, buf.limit()); + } finally { + MemoryUtil.memFree(buf); + } + } + + private static float readUboFloat(ByteBuffer buf, int at) { + return Reflect.invokeStatic(FFPTrace.class, "floatIgnoringLimit", new Class[] { ByteBuffer.class, int.class }, buf, at); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClipTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClipTest.java new file mode 100644 index 000000000..0c26c2c09 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyRectClipTest.java @@ -0,0 +1,50 @@ +package com.gtnewhorizons.angelica.sdlgpu.resource; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CopyRectClipTest { + + static Stream cases() { + return Stream.of( + Arguments.of("insideBothImages", 5, 9, 3, 2, 7, 4, 64, 64, 64, 64, false, 5, 9, 3, 2, 7, 4), + Arguments.of("negativeSourceOrigin", -3, -2, 4, 6, 10, 8, 64, 96, 64, 64, false, 0, 0, 7, 8, 7, 6), + Arguments.of("negativeDestinationOrigin", 10, 10, -4, -6, 20, 20, 64, 64, 64, 64, false, 14, 16, 0, 0, 16, 14), + Arguments.of("farSourceEdge", 60, 60, 0, 0, 10, 10, 64, 64, 64, 64, false, 60, 60, 0, 0, 4, 4), + Arguments.of("farDestinationEdge", 0, 0, 28, 30, 16, 16, 64, 64, 32, 32, false, 0, 0, 28, 30, 4, 2), + Arguments.of("oversizeBothImages", 61, 93, 62, 61, 10, 9, 64, 96, 64, 64, false, 61, 93, 62, 61, 2, 3), + Arguments.of("oversizeNearFarEdge", 60, 93, 61, 62, 10, 9, 64, 96, 64, 64, false, 60, 93, 61, 62, 3, 2), + Arguments.of("pastSourceEdge", 64, 0, 0, 0, 8, 8, 64, 64, 64, 64, true, 0, 0, 0, 0, 0, 0), + Arguments.of("beforeSourceOrigin", -20, 0, 0, 0, 8, 8, 64, 64, 64, 64, true, 0, 0, 0, 0, 0, 0), + Arguments.of("pastDestinationEdge", 0, 0, 32, 0, 8, 8, 64, 64, 32, 32, true, 0, 0, 0, 0, 0, 0), + Arguments.of("zeroWidth", 0, 0, 0, 0, 0, 8, 64, 64, 64, 64, true, 0, 0, 0, 0, 0, 0), + Arguments.of("zeroHeight", 0, 0, 0, 0, 8, 0, 64, 64, 64, 64, true, 0, 0, 0, 0, 0, 0), + Arguments.of("negativeWidth", 0, 0, 0, 0, -4, 8, 64, 64, 64, 64, true, 0, 0, 0, 0, 0, 0), + Arguments.of("unknownDestinationExtents", 0, 0, 200, 200, 8, 8, 64, 64, 0, 0, false, 0, 0, 200, 200, 8, 8), + Arguments.of("unknownSourceExtents", 200, 200, 0, 0, 8, 8, 0, 0, 64, 64, false, 200, 200, 0, 0, 8, 8), + Arguments.of("mipLevelSizedDestination", 0, 0, 0, 0, 64, 64, 64, 64, 32, 32, false, 0, 0, 0, 0, 32, 32)); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + void clipsAsExpected(String label, int srcX, int srcY, int dstX, int dstY, int width, int height, + int srcW, int srcH, int dstW, int dstH, boolean expectEmpty, + int expectedSrcX, int expectedSrcY, int expectedDstX, int expectedDstY, int expectedW, int expectedH) { + final long clip = CopyRectClip.clipCopyRect(srcX, srcY, dstX, dstY, width, height, srcW, srcH, dstW, dstH); + if (expectEmpty) { + assertEquals(CopyRectClip.EMPTY, clip, label); + return; + } + assertEquals(expectedSrcX, CopyRectClip.srcX(clip), label + " srcX"); + assertEquals(expectedSrcY, CopyRectClip.srcY(clip), label + " srcY"); + assertEquals(expectedDstX, CopyRectClip.dstX(clip, srcX, dstX), label + " dstX"); + assertEquals(expectedDstY, CopyRectClip.dstY(clip, srcY, dstY), label + " dstY"); + assertEquals(expectedW, CopyRectClip.width(clip), label + " width"); + assertEquals(expectedH, CopyRectClip.height(clip), label + " height"); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyTexSubImageFromFbo0GpuTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyTexSubImageFromFbo0GpuTest.java new file mode 100644 index 000000000..b19e8cf58 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/CopyTexSubImageFromFbo0GpuTest.java @@ -0,0 +1,195 @@ +package com.gtnewhorizons.angelica.sdlgpu.resource; + +import com.gtnewhorizons.angelica.sdlgpu.SdlTestRig; +import com.gtnewhorizons.angelica.sdlgpu.device.Device; +import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState; +import com.gtnewhorizons.angelica.sdlgpu.frame.FrameManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.lwjgl.opengl.GL11; +import org.lwjgl.system.MemoryUtil; + +import java.nio.ByteBuffer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.lwjgl.sdl.SDLGPU.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; + +@Timeout(120) +class CopyTexSubImageFromFbo0GpuTest { + + private static final int SIZE = 64; + private static final int TALL = 96; + private static final int ROW_BYTES = SIZE * 4; + + private static Device device; + private static FrameManager frameManager; + private static ResourceManager resourceManager; + private static TextureOps textureOps; + private static int nextGlId = 1000; + + @BeforeAll + static void setUp() throws Exception { + final SdlTestRig rig = SdlTestRig.acquireRealDevice(); + device = rig.device; + frameManager = rig.frameManager; + resourceManager = rig.resourceManager; + textureOps = new TextureOps(device, frameManager, resourceManager, new FBOClearTracker(frameManager, resourceManager, null)); + } + + @AfterAll + static void tearDown() { + if (frameManager != null && frameManager.isFrameActive()) frameManager.endFrame(); + if (frameManager != null) frameManager.destroyFinalTarget(); + SdlTestRig.releaseRealDevice(); + } + + private static byte srcRed(int col) { + return (byte) (16 + col * 3); + } + + private static byte srcGreen(int row) { + return (byte) (7 + row * 3); + } + + private static ByteBuffer fbo0Pattern(int height) { + final ByteBuffer buf = MemoryUtil.memAlloc(SIZE * height * 4); + for (int row = 0; row < height; row++) { + for (int col = 0; col < SIZE; col++) { + final int i = row * ROW_BYTES + col * 4; + buf.put(i, srcRed(col)); + buf.put(i + 1, srcGreen(row)); + buf.put(i + 2, (byte) 0x40); + buf.put(i + 3, (byte) 0xFF); + } + } + return buf; + } + + private static final byte[] SENTINEL = { 0x11, 0x22, 0x33, 0x44 }; + + private static ByteBuffer sentinelPattern() { + final ByteBuffer buf = MemoryUtil.memAlloc(SIZE * SIZE * 4); + for (int i = 0; i < SIZE * SIZE; i++) { + for (int c = 0; c < 4; c++) buf.put(i * 4 + c, SENTINEL[c]); + } + return buf; + } + + private static void upload(long texture, ByteBuffer data, int width, int height) { + final long cp = frameManager.ensureCopyPass(); + assertNotEquals(0L, cp, "no copy pass"); + resourceManager.uploadToTexture(cp, data, texture, 0, 0, width, height, 0); + resourceManager.flushBatchedUploads(cp); + frameManager.endCopyPassIfActive(); + } + + private static int createDest() { + final int glId = nextGlId++; + final long handle = resourceManager.createTexture(glId, GL11.GL_TEXTURE_2D, GL11.GL_RGBA8, SIZE, SIZE, 1, 1); + assertNotEquals(0L, handle, "dest texture creation failed"); + return glId; + } + + private static int createSentinelDest() { + final int destGlId = createDest(); + final ByteBuffer sentinel = sentinelPattern(); + try { + upload(resourceManager.getTextureHandle(destGlId), sentinel, SIZE, SIZE); + } finally { + MemoryUtil.memFree(sentinel); + } + return destGlId; + } + + private static void prepareFbo0(int height) { + frameManager.destroyFinalTarget(); + frameManager.finalTarget().create(device, resourceManager, SIZE, height, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM); + assertEquals(SIZE, frameManager.getFbo0Width()); + assertEquals(height, frameManager.getFbo0Height()); + final ByteBuffer pattern = fbo0Pattern(height); + try { + upload(frameManager.getFbo0Texture(), pattern, SIZE, height); + } finally { + MemoryUtil.memFree(pattern); + } + } + + private static ByteBuffer download(int destGlId) { + final ByteBuffer out = MemoryUtil.memAlloc(SIZE * SIZE * 4); + textureOps.readbackTexture(resourceManager.getTextureHandle(destGlId), 0, 0, SIZE, SIZE, 0, out); + out.rewind(); + return out; + } + + private static void assertPixel(ByteBuffer got, int col, int row, byte r, byte g, byte b, byte a, String what) { + final int i = row * ROW_BYTES + col * 4; + assertEquals(r, got.get(i), what + " R at col=" + col + " row=" + row); + assertEquals(g, got.get(i + 1), what + " G at col=" + col + " row=" + row); + assertEquals(b, got.get(i + 2), what + " B at col=" + col + " row=" + row); + assertEquals(a, got.get(i + 3), what + " A at col=" + col + " row=" + row); + } + + private static void assertCopiedRegion(ByteBuffer got, int fbo0Height, int dx, int dy, int sx, int sy, int w, int h, String what) { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + final boolean inside = col >= dx && col < dx + w && row >= dy && row < dy + h; + if (inside) { + final int srcCol = sx + (col - dx); + final int srcStorageRow = fbo0Height - 1 - (sy + (row - dy)); + assertPixel(got, col, row, srcRed(srcCol), srcGreen(srcStorageRow), (byte) 0x40, (byte) 0xFF, what); + } else { + assertPixel(got, col, row, SENTINEL[0], SENTINEL[1], SENTINEL[2], SENTINEL[3], "outside " + what); + } + } + } + } + + @Test + void fullCopyFromDefaultFramebufferIsFlippedIntoTheTexture() { + frameManager.beginFrame(); + frameManager.frame().swapchainUnavailable = true; + prepareFbo0(SIZE); + + final int destGlId = createSentinelDest(); + + final ContextState st = new ContextState(); + st.boundReadFboId = 0; + textureOps.copyTexSubImageImpl(st, destGlId, 0, 0, 0, 0, 0, SIZE, SIZE); + + final ByteBuffer got = download(destGlId); + try { + for (int row = 0; row < SIZE; row++) { + for (int col = 0; col < SIZE; col++) { + assertPixel(got, col, row, srcRed(col), srcGreen(SIZE - 1 - row), (byte) 0x40, (byte) 0xFF, "full copy"); + } + } + } finally { + MemoryUtil.memFree(got); + } + frameManager.endFrame(); + } + + @Test + void subRectCopyFromATallerDefaultFramebufferUsesTheFullHeightBasis() { + frameManager.beginFrame(); + frameManager.frame().swapchainUnavailable = true; + prepareFbo0(TALL); + + final int destGlId = createSentinelDest(); + + final ContextState st = new ContextState(); + st.boundReadFboId = 0; + textureOps.copyTexSubImageImpl(st, destGlId, 0, 5, 9, 3, 2, 7, 4); + + final ByteBuffer got = download(destGlId); + try { + assertCopiedRegion(got, TALL, 5, 9, 3, 2, 7, 4, "tall sub-rect copy"); + } finally { + MemoryUtil.memFree(got); + } + frameManager.endFrame(); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/ReadbackSubmitOrderGpuTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/ReadbackSubmitOrderGpuTest.java index cfde3f558..f2f2990f4 100644 --- a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/ReadbackSubmitOrderGpuTest.java +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/resource/ReadbackSubmitOrderGpuTest.java @@ -51,7 +51,7 @@ class ReadbackSubmitOrderGpuTest { } assertEquals(1, f.midFrameSubmitsThisFrame, "the frame command buffer must be submitted before the download buffer, or the readback sees stale contents"); - assertNotEquals(frameCb, f.commandBuffer, "the frame must continue on a freshly acquired command buffer"); + assertNotEquals(0L, f.commandBuffer, "the frame must continue on a freshly acquired command buffer"); fm.endFrame(); } diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/AttribLocationPatchTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/AttribLocationPatchTest.java index 454df39f8..799318cfd 100644 --- a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/AttribLocationPatchTest.java +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/AttribLocationPatchTest.java @@ -5,9 +5,12 @@ import org.lwjgl.util.spvc.Spvc; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class AttribLocationPatchTest { @@ -23,10 +26,14 @@ class AttribLocationPatchTest { }; private static ShaderManager.StageReflection reflectionWithInputs(ByteBuffer spirv) { - final ShaderManager.VsInput[] inputs = new ShaderManager.VsInput[NAMES.length]; - for (int i = 0; i < NAMES.length; i++) { + return reflectionWithInputs(spirv, NAMES); + } + + private static ShaderManager.StageReflection reflectionWithInputs(ByteBuffer spirv, String[] names) { + final ShaderManager.VsInput[] inputs = new ShaderManager.VsInput[names.length]; + for (int i = 0; i < names.length; i++) { spirv.asIntBuffer().put(i, i); - inputs[i] = new ShaderManager.VsInput(NAMES[i], i, i, VEC_SIZES[i], BASE_TYPES[i]); + inputs[i] = new ShaderManager.VsInput(names[i], i, i, i < VEC_SIZES.length ? VEC_SIZES[i] : 4, i < BASE_TYPES.length ? BASE_TYPES[i] : Spvc.SPVC_BASETYPE_FP32); } final ShaderManager.StageReflection e = ShaderManager.StageReflection.EMPTY; return new ShaderManager.StageReflection(e.counts(), e.samplerNames(), e.extraUniformNames(), @@ -71,6 +78,66 @@ class AttribLocationPatchTest { assertEquals(Spvc.SPVC_BASETYPE_INT32, prog.vertexInputBaseType[5]); } + @Test + void anUnboundInputMovesOffALocationABindingClaimed() { + final String[] names = { + "a_PosId", "a_Color", "a_TexCoord", "a_LightCoord", "a_RdhFactor", + "mc_midTexCoord", "at_tangent", "iris_Normal", "mc_Entity", "at_midBlock" + }; + final ShaderManager.ProgramObject prog = new ShaderManager.ProgramObject(); + prog.vertexSpirv = ByteBuffer.allocateDirect(names.length * Integer.BYTES).order(ByteOrder.nativeOrder()); + final ShaderManager.StageReflection vs = reflectionWithInputs(prog.vertexSpirv, names); + + int loc = 0; + for (String name : names) { + if (!name.equals("a_RdhFactor")) prog.attribLocationBindings.put(name, loc++); + } + + ShaderManager.applyAttribLocationsAndInputMask(prog, vs); + + assertEquals(4, prog.resolvedAttribLocations.getInt("mc_midTexCoord"), "the binding is authoritative"); + assertEquals(9, prog.resolvedAttribLocations.getInt("a_RdhFactor"), "the unbound input takes the lowest free slot"); + assertEquals(0x3ff, prog.vertexInputMask, "ten inputs at 0..9"); + + final Set seen = new HashSet<>(); + for (String name : names) { + final int resolved = prog.resolvedAttribLocations.getInt(name); + assertTrue(seen.add(resolved), () -> "duplicate location " + resolved + " for " + name); + assertEquals(resolved, prog.vertexSpirv.asIntBuffer().get(indexOf(names, name)), () -> "SPIR-V Location decoration for " + name); + } + } + + @Test + void aBoundNameAbsentFromTheShaderKeepsItsLocationReserved() { + final String[] bound = { + "a_PosId", "a_Color", "a_TexCoord", "a_LightCoord", "mc_midTexCoord", + "at_tangent", "iris_Normal", "mc_Entity", "at_midBlock" + }; + final String[] declared = { + "a_PosId", "a_Color", "a_TexCoord", "a_LightCoord", "a_RdhFactor", + "mc_midTexCoord", "iris_Normal", "mc_Entity", "at_midBlock" + }; + final ShaderManager.ProgramObject prog = new ShaderManager.ProgramObject(); + prog.vertexSpirv = ByteBuffer.allocateDirect(declared.length * Integer.BYTES).order(ByteOrder.nativeOrder()); + final ShaderManager.StageReflection vs = reflectionWithInputs(prog.vertexSpirv, declared); + + for (int i = 0; i < bound.length; i++) { + prog.attribLocationBindings.put(bound[i], i); + } + + ShaderManager.applyAttribLocationsAndInputMask(prog, vs); + + assertEquals(9, prog.resolvedAttribLocations.getInt("a_RdhFactor"), "location 5 stays reserved for the optimized-out at_tangent"); + assertEquals(0x3df, prog.vertexInputMask, "nine inputs at 0..4 and 6..9"); + } + + private static int indexOf(String[] names, String name) { + for (int i = 0; i < names.length; i++) { + if (names[i].equals(name)) return i; + } + throw new IllegalArgumentException(name); + } + @Test void unboundInputsKeepTheirOriginalLocation() { final ShaderManager.ProgramObject prog = new ShaderManager.ProgramObject(); diff --git a/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaGLStateManagerService.java b/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaGLStateManagerService.java deleted file mode 100644 index 8b79787e8..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaGLStateManagerService.java +++ /dev/null @@ -1,368 +0,0 @@ -package com.gtnewhorizons.angelica.client.rendering; - -import com.gtnewhorizon.gtnhlib.client.opengl.UniversalVAO; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.mitchej123.glsm.GLStateManagerService; -import org.lwjgl.BufferUtils; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL13; - -import java.nio.IntBuffer; - -public class AngelicaGLStateManagerService implements GLStateManagerService { - - private IntBuffer intArrayBuffer = BufferUtils.createIntBuffer(16); - - private IntBuffer getIntArrayBuffer(int size) { - if (intArrayBuffer.capacity() < size) { - intArrayBuffer = BufferUtils.createIntBuffer(size); - } - intArrayBuffer.clear(); - return intArrayBuffer; - } - - @Override - public int getPriority() { - return 100; - } - - // ===================== QUERY OPERATIONS ===================== - - @Override - public int glGetInteger(int pname) { - return GLStateManager.glGetInteger(pname); - } - - @Override - public String glGetString(int pname) { - return GLStateManager.glGetString(pname); - } - - // ===================== FRAMEBUFFER OPERATIONS ===================== - - @Override - public int glGenFramebuffers() { - return GLStateManager.glGenFramebuffers(); - } - - @Override - public void glDeleteFramebuffers(int framebuffer) { - GLStateManager.glDeleteFramebuffers(framebuffer); - } - - @Override - public void glBindFramebuffer(int target, int framebuffer) { - GLStateManager.glBindFramebuffer(target, framebuffer); - } - - @Override - public int glCheckFramebufferStatus(int target) { - return GLStateManager.glCheckFramebufferStatus(target); - } - - @Override - public void glFramebufferTexture2D(int target, int attachment, int textarget, int texture, int level) { - GLStateManager.glFramebufferTexture2D(target, attachment, textarget, texture, level); - } - - // ===================== SHADER OPERATIONS ===================== - - @Override - public int glCreateShader(int type) { - return GLStateManager.glCreateShader(type); - } - - @Override - public void glCompileShader(int shader) { - GLStateManager.glCompileShader(shader); - } - - @Override - public int glGetShaderi(int shader, int pname) { - return GLStateManager.glGetShaderi(shader, pname); - } - - @Override - public String glGetShaderInfoLog(int shader, int maxLength) { - return GLStateManager.glGetShaderInfoLog(shader, maxLength); - } - - @Override - public void glDeleteShader(int shader) { - GLStateManager.glDeleteShader(shader); - } - - @Override - public int glCreateProgram() { - return GLStateManager.glCreateProgram(); - } - - @Override - public void glAttachShader(int program, int shader) { - GLStateManager.glAttachShader(program, shader); - } - - @Override - public void glLinkProgram(int program) { - GLStateManager.glLinkProgram(program); - } - - @Override - public int glGetProgrami(int program, int pname) { - return GLStateManager.glGetProgrami(program, pname); - } - - @Override - public void glUseProgram(int program) { - GLStateManager.glUseProgram(program); - } - - @Override - public void glDeleteProgram(int program) { - GLStateManager.glDeleteProgram(program); - } - - // ===================== UNIFORM/ATTRIBUTE OPERATIONS ===================== - - @Override - public int glGetUniformLocation(int program, CharSequence name) { - return GLStateManager.glGetUniformLocation(program, name); - } - - @Override - public void glUniform1i(int location, int value) { - GLStateManager.glUniform1i(location, value); - } - - @Override - public int glGetAttribLocation(int program, CharSequence name) { - return GLStateManager.glGetAttribLocation(program, name); - } - - @Override - public void glBindAttribLocation(int program, int index, CharSequence name) { - GLStateManager.glBindAttribLocation(program, index, name); - } - - // ===================== VAO OPERATIONS ===================== - - @Override - public int glGenVertexArrays() { - return UniversalVAO.genVertexArrays(); - } - - @Override - public void glBindVertexArray(int array) { - GLStateManager.glBindVertexArray(array); - } - - @Override - public void glDeleteVertexArrays(int array) { - GLStateManager.glDeleteVertexArrays(array); - } - - // ===================== BUFFER OPERATIONS ===================== - - @Override - public int glGenBuffers() { - return GLStateManager.glGenBuffers(); - } - - @Override - public void glBindBuffer(int target, int buffer) { - GLStateManager.glBindBuffer(target, buffer); - } - - @Override - public void glDeleteBuffers(int buffer) { - GLStateManager.glDeleteBuffers(buffer); - } - - // ===================== TEXTURE OPERATIONS ===================== - - @Override - public int glGenTextures() { - return GLStateManager.glGenTextures(); - } - - @Override - public void glGenTextures(int[] textures) { - final IntBuffer buffer = getIntArrayBuffer(textures.length); - GLStateManager.glGenTextures(buffer); - buffer.get(textures); - } - - @Override - public void glDeleteTextures(int texture) { - GLStateManager.glDeleteTextures(texture); - } - - @Override - public void glDeleteTextures(int[] textures) { - final IntBuffer buffer = getIntArrayBuffer(textures.length); - buffer.put(textures); - buffer.flip(); - GLStateManager.glDeleteTextures(buffer); - } - - @Override - public void glActiveTexture(int texture) { - GLStateManager.glActiveTexture(texture); - } - - @Override - public int glGetTexLevelParameteri(int target, int level, int pname) { - return GLStateManager.glGetTexLevelParameteri(target, level, pname); - } - - @Override - public int glGetTexLevelParameter(int target, int level, int pname) { - return GLStateManager.glGetTexLevelParameteri(target, level, pname); - } - - @Override - public void glCopyTexSubImage2D(int target, int level, int xoffset, int yoffset, int x, int y, int width, int height) { - GLStateManager.glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height); - } - - @Override - public void glPixelStorei(int pname, int param) { - GLStateManager.glPixelStorei(pname, param); - } - - // ===================== STATE OPERATIONS ===================== - - @Override - public void enableCullFace() { - GLStateManager.enableCull(); - } - - @Override - public void disableCullFace() { - GLStateManager.disableCull(); - } - - @Override - public void enableBlend() { - GLStateManager.enableBlend(); - } - - @Override - public void disableBlend() { - GLStateManager.disableBlend(); - } - - @Override - public void glBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha) { - GLStateManager.tryBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); - } - - @Override - public void enableDepthTest() { - GLStateManager.enableDepthTest(); - } - - @Override - public void disableDepthTest() { - GLStateManager.disableDepthTest(); - } - - @Override - public void glDepthFunc(int func) { - GLStateManager.glDepthFunc(func); - } - - @Override - public void glDepthMask(boolean flag) { - GLStateManager.glDepthMask(flag); - } - - @Override - public void glViewport(int x, int y, int width, int height) { - GLStateManager.glViewport(x, y, width, height); - } - - @Override - public void glColorMask(boolean red, boolean green, boolean blue, boolean alpha) { - GLStateManager.glColorMask(red, green, blue, alpha); - } - - @Override - public void glClearColor(float red, float green, float blue, float alpha) { - GLStateManager.glClearColor(red, green, blue, alpha); - } - - @Override - public void glClear(int mask) { - GLStateManager.glClear(mask); - } - - // ===================== MOJANG ADDITIONS ===================== - - @Override - public void clear(int mask, boolean checkError) { - GLStateManager.glClear(mask); - } - - @Override - public void bindTexture(int texture) { - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); - } - - @Override - public int getActiveTexture() { - return GLStateManager.getActiveTextureUnit() + GL13.GL_TEXTURE0; - } - - @Override - public int getActiveTextureAccessor() { - return GLStateManager.getActiveTextureUnit(); - } - - @Override - public int getBoundTexture(int internalUnit) { - return GLStateManager.getBoundTextureForServerState(internalUnit); - } - - @Override - public int getActiveBoundTexture() { - return GLStateManager.getBoundTextureForServerState(); - } - - @Override - public int getViewportWidth() { - return GLStateManager.getViewportState().width; - } - - @Override - public int getViewportHeight() { - return GLStateManager.getViewportState().height; - } - - @Override - public boolean getDepthStateMask() { - return GLStateManager.getDepthState().isEnabled(); - } - - @Override - public boolean isBlendEnabled() { - return GLStateManager.getBlendMode().isEnabled(); - } - - @Override - public void setBoundTexture(int unit, int texture) { - final int currentUnit = GLStateManager.getActiveTextureUnit(); - if (currentUnit != unit) { - GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + unit); - } - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); - if (currentUnit != unit) { - GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + currentUnit); - } - } - - @Override - public int getTextureBinding(int unit) { - return GLStateManager.getBoundTextureForServerState(unit); - } -} diff --git a/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaRenderSystemService.java b/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaRenderSystemService.java deleted file mode 100644 index b1f552f3d..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/client/rendering/AngelicaRenderSystemService.java +++ /dev/null @@ -1,243 +0,0 @@ -package com.gtnewhorizons.angelica.client.rendering; - -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.mitchej123.glsm.RenderSystemService; -import org.joml.Matrix4f; -import org.lwjgl.BufferUtils; -import org.lwjgl.opengl.GL11; -import java.nio.FloatBuffer; - -public class AngelicaRenderSystemService implements RenderSystemService { - - // Shader state (Mojang-specific, tracked here) - private final int[] shaderTextures = new int[16]; - private final float[] shaderColor = {1f, 1f, 1f, 1f}; - private final float[] shaderFogColor = {0f, 0f, 0f, 1f}; - private float shaderFogStart = 0f; - private float shaderFogEnd = 0f; - private int fogShape = 0; - private float shaderLineWidth = 1f; - private final Matrix4f projectionMatrix = new Matrix4f(); - private final FloatBuffer projectionMatrixBuffer = BufferUtils.createFloatBuffer(16); - - @Override - public int getPriority() { - return 100; - } - - // ===================== TEXTURE OPERATIONS ===================== - - @Override - public void glActiveTexture(int texture) { - GLStateManager.glActiveTexture(texture); - } - - @Override - public void bindTexture(int texture) { - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); - } - - // ===================== STATE OPERATIONS ===================== - - @Override - public void enableCullFace() { - GLStateManager.enableCull(); - } - - @Override - public void disableCullFace() { - GLStateManager.disableCull(); - } - - @Override - public void enableBlend() { - GLStateManager.enableBlend(); - } - - @Override - public void disableBlend() { - GLStateManager.disableBlend(); - } - - @Override - public void defaultBlendFunc() { - GLStateManager.defaultBlendFunc(); - } - - @Override - public void blendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha) { - GLStateManager.tryBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha); - } - - @Override - public void setUnknownBlendState() { - // No-op: Angelica tracks blend state accurately - } - - @Override - public void enableDepthTest() { - GLStateManager.enableDepthTest(); - } - - @Override - public void disableDepthTest() { - GLStateManager.disableDepthTest(); - } - - @Override - public void depthFunc(int depthFunc) { - GLStateManager.glDepthFunc(depthFunc); - } - - @Override - public void depthMask(boolean flag) { - GLStateManager.glDepthMask(flag); - } - - @Override - public void glViewport(int x, int y, int width, int height) { - GLStateManager.glViewport(x, y, width, height); - } - - @Override - public void glClearColor(float red, float green, float blue, float alpha) { - GLStateManager.glClearColor(red, green, blue, alpha); - } - - @Override - public void clear(int mask, boolean checkError) { - GLStateManager.glClear(mask); - } - - // ===================== UNIFORM OPERATIONS ===================== - - @Override - public void glUniform1i(int location, int value) { - GLStateManager.glUniform1i(location, value); - } - - @Override - public void glUniformMatrix3(int location, boolean transpose, FloatBuffer value) { - GLStateManager.glUniformMatrix3(location, transpose, value); - } - - @Override - public void glUniformMatrix4(int location, boolean transpose, FloatBuffer value) { - GLStateManager.glUniformMatrix4(location, transpose, value); - } - - // ===================== THREAD ASSERTIONS ===================== - - @Override - public void assertOnRenderThread() { - // No-op: 1.7.10 is single-threaded for rendering - } - - @Override - public void assertOnRenderThreadOrInit() { - // No-op: 1.7.10 is single-threaded for rendering - } - - // ===================== SHADER STATE ===================== - - @Override - public void setShaderTexture(int shaderTexture, int textureId) { - if (shaderTexture >= 0 && shaderTexture < 16) { - shaderTextures[shaderTexture] = textureId; - } - } - - @Override - public int getShaderTexture(int shaderTexture) { - return (shaderTexture >= 0 && shaderTexture < 16) ? shaderTextures[shaderTexture] : 0; - } - - @Override - public void setShaderColor(float red, float green, float blue, float alpha) { - shaderColor[0] = red; - shaderColor[1] = green; - shaderColor[2] = blue; - shaderColor[3] = alpha; - } - - @Override - public float[] getShaderColor() { - return shaderColor.clone(); - } - - @Override - public void setShaderLineWidth(float lineWidth) { - shaderLineWidth = lineWidth; - } - - @Override - public float getShaderLineWidth() { - return shaderLineWidth; - } - - // ===================== FOG STATE ===================== - - @Override - public void setShaderFogColor(float red, float green, float blue, float alpha) { - shaderFogColor[0] = red; - shaderFogColor[1] = green; - shaderFogColor[2] = blue; - shaderFogColor[3] = alpha; - } - - @Override - public float[] getShaderFogColor() { - return shaderFogColor.clone(); - } - - @Override - public void setShaderFogStart(float start) { - shaderFogStart = start; - } - - @Override - public float getShaderFogStart() { - return shaderFogStart; - } - - @Override - public void setShaderFogEnd(float end) { - shaderFogEnd = end; - } - - @Override - public float getShaderFogEnd() { - return shaderFogEnd; - } - - @Override - public void setFogShape(int shape) { - fogShape = shape; - } - - @Override - public int getFogShape() { - return fogShape; - } - - // ===================== PROJECTION MATRIX ===================== - - @Override - public Matrix4f getProjectionMatrix() { - return new Matrix4f(projectionMatrix); - } - - @Override - public void setProjectionMatrixOrth(Matrix4f m) { - projectionMatrix.set(m); - GLStateManager.glMatrixMode(GL11.GL_PROJECTION); - projectionMatrix.get(projectionMatrixBuffer); - projectionMatrixBuffer.rewind(); - GLStateManager.glLoadMatrix(projectionMatrixBuffer); - } - - @Override - public void setProjectionMatrixOrigin(Matrix4f m) { - setProjectionMatrixOrth(m); - } -} diff --git a/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java index 2fdce8971..9ee8a5de8 100644 --- a/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java +++ b/src/main/java/com/gtnewhorizons/angelica/debug/flyby/FlybyRunner.java @@ -7,6 +7,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; @@ -27,7 +28,6 @@ public final class FlybyRunner { private static final float[] NO_YAW = new float[0]; private static final byte[] NO_PHASE = new byte[0]; private static final int MAX_RECORDED_FRAMES = 200_000; - private static final long FIXED_TIME_OF_DAY = 6000L; private enum State { IDLE, WAITING, WARMUP, RUNNING, SETTLE, EXITING, DONE } @@ -123,6 +123,14 @@ public final class FlybyRunner { if (event.phase != TickEvent.Phase.END || this.state == State.IDLE || this.state == State.DONE) return; final Minecraft mc = Minecraft.getMinecraft(); + if (this.state == State.EXITING) { + if (++this.tick >= EXIT_TICKS) { + this.state = State.DONE; + this.exitGame(mc); + } + return; + } + final EntityClientPlayerMP player = mc.thePlayer; if (player == null || mc.theWorld == null) return; @@ -152,13 +160,6 @@ public final class FlybyRunner { this.teardown(mc, player); } } - case EXITING -> { - if (++this.tick >= EXIT_TICKS) { - this.state = State.DONE; - LOGGER.info("Flyby complete, shutting down so the capture can finalise"); - mc.shutdown(); - } - } default -> { } } } @@ -383,9 +384,9 @@ public final class FlybyRunner { world.getWorldInfo().setRainTime(Integer.MAX_VALUE); world.getWorldInfo().setThunderTime(Integer.MAX_VALUE); - if (world.getWorldTime() % 24000L != FIXED_TIME_OF_DAY) { - LOGGER.info("Flyby: freezing time at {}", FIXED_TIME_OF_DAY); - world.setWorldTime(FIXED_TIME_OF_DAY); + if (world.getWorldTime() % 24000L != SystemProperties.FLYBY_TIME_OF_DAY) { + LOGGER.info("Flyby: freezing time at {}", SystemProperties.FLYBY_TIME_OF_DAY); + world.setWorldTime(SystemProperties.FLYBY_TIME_OF_DAY); } } } @@ -419,6 +420,16 @@ public final class FlybyRunner { this.state = this.exitWhenDone ? State.EXITING : State.DONE; } + private void exitGame(Minecraft mc) { + LOGGER.info("Flyby complete, shutting down."); + if (mc.theWorld != null) { + mc.theWorld.sendQuittingDisconnectingPacket(); + mc.loadWorld(null); + mc.displayGuiScreen(new GuiMainMenu()); + } + mc.shutdown(); + } + private void restorePauseOnLostFocus(Minecraft mc) { if (!this.pauseOnLostFocusOverridden) return; mc.gameSettings.pauseOnLostFocus = this.pauseOnLostFocusSaved; diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/AngelicaClientTweaker.java b/src/main/java/com/gtnewhorizons/angelica/loading/AngelicaClientTweaker.java index 0b3fe8297..3cb25ca7b 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/AngelicaClientTweaker.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/AngelicaClientTweaker.java @@ -10,12 +10,11 @@ import com.gtnewhorizons.angelica.config.CompatConfig; import com.gtnewhorizons.angelica.config.FontConfig; import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.loading.DependencyVerifier; -import com.gtnewhorizons.angelica.glsm.loading.EcosystemNarrowRules; +import com.gtnewhorizons.angelica.glsm.loading.Lwjgl3ifyExclusions; import com.gtnewhorizons.angelica.loading.fml.compat.CompatHandlers; import com.gtnewhorizons.angelica.lwjgl3.MissingDependencySdl; import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; import com.gtnewhorizons.angelica.mixins.Mixins; -import com.gtnewhorizons.retrofuturabootstrap.SharedConfig; import cpw.mods.fml.relauncher.FMLLaunchHandler; import cpw.mods.fml.relauncher.FMLRelaunchLog; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; @@ -33,8 +32,6 @@ import org.apache.logging.log4j.core.config.LoggerConfig; import org.spongepowered.asm.launch.GlobalProperties; import org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapper; -import javax.swing.JOptionPane; -import java.awt.GraphicsEnvironment; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -57,7 +54,7 @@ public final class AngelicaClientTweaker implements IFMLLoadingPlugin, IEarlyMix final boolean rfbLoaded = Launch.blackboard.getOrDefault("angelica.rfbPluginLoaded", Boolean.FALSE) == Boolean.TRUE; if (rfbLoaded) { - addLwjgl3ifyExclusions(); + Lwjgl3ifyExclusions.apply(); } else { // Fucking java 8 and non RFB try { @@ -86,7 +83,9 @@ public final class AngelicaClientTweaker implements IFMLLoadingPlugin, IEarlyMix } catch (ConfigException e) { throw new RuntimeException(e); } - verifyDependencies(); + DependencyVerifier.verifyOrHalt(AngelicaTweaker.class, "Angelica", + DependencyVerifier.gtnhLibChecks("Angelica"), LOGGER, + (title, message) -> MissingDependencySdl.showFatal(title, message)); if (SystemProperties.USE_SDL_GPU) { if (SDLGPUGate.isSDLGPUAvailable()) { @@ -109,56 +108,6 @@ public final class AngelicaClientTweaker implements IFMLLoadingPlugin, IEarlyMix } } - private static void addLwjgl3ifyExclusions() { - final var handle = SharedConfig.getRfbTransformers().stream() - .filter(transformer -> transformer.id().equals("lwjgl3ify:redirect")) - .findFirst() - .orElse(null); - if (handle != null) { - for (String exclusion : EcosystemNarrowRules.LWJGL3IFY_EXCLUSIONS_SHARED) { - handle.exclusions().add(exclusion); - } - handle.exclusions().add("com.gtnewhorizons.angelica.sdlgpu"); - } - } - - private static void verifyDependencies() { - try { - DependencyVerifier.verify(AngelicaTweaker.class, List.of( - new DependencyVerifier.Check( - "/it/unimi/dsi/fastutil/ints/Int2ObjectMap.class", - "Missing dependency: Angelica requires GTNHLib! Download: https://modrinth.com/mod/gtnhlib"), - new DependencyVerifier.Check( - "/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.class", - "GTNHLib is outdated: Angelica requires GTNHLib 0.10.0 or newer! Download: https://modrinth.com/mod/gtnhlib"), - new DependencyVerifier.Check( - "/it/unimi/dsi/fastutil/objects/ObjectBooleanBiConsumer.class", - "GTNHLib is outdated: Angelica requires GTNHLib 0.11.19 or newer (fastutil 8.5.18+)! Download: https://modrinth.com/mod/gtnhlib") - )); - } catch (RuntimeException ex) { - fatalDependencyError(ex.getMessage()); - } - } - - private static void fatalDependencyError(String message) { - final String title = "Angelica - Missing Dependency"; - System.err.println("FATAL: " + message); - try { - LOGGER.fatal(message); - } catch (Throwable ignored) {} - - final int lwjgl3ifyMajor = ((Integer) Launch.blackboard.getOrDefault("lwjgl3ify:major-version", Integer.MIN_VALUE)); - try { - if (lwjgl3ifyMajor >= 3) { - MissingDependencySdl.showFatal(title, message); - } else if (!GraphicsEnvironment.isHeadless()) { - JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE); - } - } catch (Throwable ignored) {} - - Runtime.getRuntime().halt(1); - } - @Override public String[] getASMTransformerClass() { if (transformerClasses == null) { diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/AngelicaRedirectorTransformer.java b/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/AngelicaRedirectorTransformer.java index 5efbdab49..6591a047d 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/AngelicaRedirectorTransformer.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/AngelicaRedirectorTransformer.java @@ -1,48 +1,15 @@ package com.gtnewhorizons.angelica.loading.fml.transformers; -import com.gtnewhorizons.angelica.loading.shared.AngelicaClassDump; +import com.gtnewhorizons.angelica.glsm.loading.ModRedirector; import com.gtnewhorizons.angelica.loading.shared.transformers.AngelicaRedirector; import net.minecraft.launchwrapper.IClassTransformer; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; -/** IClassTransformer wrapper for {@link AngelicaRedirector} */ public class AngelicaRedirectorTransformer implements IClassTransformer { - private final AngelicaRedirector inner; - private final String[] exclusions; - - public AngelicaRedirectorTransformer() { - inner = new AngelicaRedirector(); - exclusions = inner.getTransformerExclusions(); - } + private final ModRedirector inner = AngelicaRedirector.create(); @Override public byte[] transform(final String className, String transformedName, byte[] basicClass) { - if (basicClass == null) return null; - - // Ignore classes that are excluded from transformation - Doesn't fully work without the - // TransformerExclusions due to some nested classes - for (String exclusion : exclusions) { - if (transformedName.startsWith(exclusion)) return basicClass; - } - - if (!inner.shouldTransform(basicClass)) { - return basicClass; - } - - final ClassReader cr = new ClassReader(basicClass); - final ClassNode cn = new ClassNode(); - cr.accept(cn, 0); - final boolean changed = inner.transformClassNode(transformedName, cn); - if (changed) { - final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - final byte[] bytes = cw.toByteArray(); - AngelicaClassDump.dumpClass(transformedName, basicClass, bytes, this); - return bytes; - } - return basicClass; + return inner.transform(transformedName, basicClass, this); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/EarlyRedirectorTransformer.java b/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/EarlyRedirectorTransformer.java index 4c6e28125..6c60b6360 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/EarlyRedirectorTransformer.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/fml/transformers/EarlyRedirectorTransformer.java @@ -18,13 +18,10 @@ import net.minecraft.launchwrapper.IClassTransformer; */ public class EarlyRedirectorTransformer implements IClassTransformer { - private final EarlyRedirectorCore impl = new EarlyRedirectorCore( - "com.gtnewhorizons.angelica.lwjgl3.", - "com.gtnewhorizons.angelica.transform" - ); + private final EarlyRedirectorCore impl = new EarlyRedirectorCore("com.gtnewhorizons.angelica.transform"); @Override public byte[] transform(String name, String transformedName, byte[] basicClass) { - return impl.transform(transformedName, basicClass); + return impl.transform(transformedName, basicClass, this); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/rfb/transformers/RFBAngelicaRedirector.java b/src/main/java/com/gtnewhorizons/angelica/loading/rfb/transformers/RFBAngelicaRedirector.java index 4048ac3c4..1d50e010a 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/rfb/transformers/RFBAngelicaRedirector.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/rfb/transformers/RFBAngelicaRedirector.java @@ -1,79 +1,11 @@ package com.gtnewhorizons.angelica.loading.rfb.transformers; -import com.gtnewhorizons.angelica.loading.shared.AngelicaClassDump; +import com.gtnewhorizons.angelica.glsm.loading.RfbModRedirector; import com.gtnewhorizons.angelica.loading.shared.transformers.AngelicaRedirector; -import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; -import com.gtnewhorizons.retrofuturabootstrap.api.ExtensibleClassLoader; -import com.gtnewhorizons.retrofuturabootstrap.api.RfbClassTransformer; -import org.intellij.lang.annotations.Pattern; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.jar.Attributes; -import java.util.jar.Manifest; - -/** RfbClassTransformer wrapper for {@link AngelicaRedirector} */ -public class RFBAngelicaRedirector implements RfbClassTransformer { - - private final AngelicaRedirector inner; +public class RFBAngelicaRedirector extends RfbModRedirector { public RFBAngelicaRedirector() { - inner = new AngelicaRedirector(); - } - - @Pattern("[a-z0-9-]+") - @Override - public @NotNull String id() { - return "redirector"; - } - - private static final Attributes.Name LWJGL3_AWARE = new Attributes.Name("Lwjgl3ify-Aware"); - - /** We sort before lwjgl3ify:redirect, so an aware caller's descriptors are still org.lwjgl here and stay that way. */ - private static boolean isLwjgl3Aware(@Nullable Manifest manifest) { - return manifest != null && "true".equals(manifest.getMainAttributes().getValue(LWJGL3_AWARE)); - } - - @Override - public @NotNull String @Nullable [] sortAfter() { - return new String[] {"*", "mixin:mixin"}; - } - - @Override - public @NotNull String @Nullable [] sortBefore() { - return new String[] {"lwjgl3ify:redirect"}; - } - - @Override - public @NotNull String @Nullable [] additionalExclusions() { - return inner.getTransformerExclusions(); - } - - @Override - public boolean shouldTransformClass(@NotNull ExtensibleClassLoader classLoader, - @NotNull RfbClassTransformer.Context context, @Nullable Manifest manifest, @NotNull String className, - @NotNull ClassNodeHandle classNode) { - if (!classNode.isPresent()) { - return false; - } - return inner.shouldTransform(classNode.getOriginalBytes()); - } - - @Override - public boolean transformClassIfNeeded(@NotNull ExtensibleClassLoader classLoader, - @NotNull RfbClassTransformer.Context context, @Nullable Manifest manifest, - @NotNull String className, @NotNull ClassNodeHandle classNode) { - final boolean changed = inner.transformClassNode(className, classNode.getNode(), isLwjgl3Aware(manifest)); - if (changed) { - classNode.computeMaxs(); - AngelicaClassDump.dumpRFBClass(className, classNode, this); - } - return changed; - } - - @Override - public void transformClass(@NotNull ExtensibleClassLoader classLoader, @NotNull RfbClassTransformer.Context context, - @Nullable Manifest manifest, @NotNull String className, @NotNull ClassNodeHandle classNode) { - transformClassIfNeeded(classLoader, context, manifest, className, classNode); + super("redirector", AngelicaRedirector.create()); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/shared/AngelicaClassDump.java b/src/main/java/com/gtnewhorizons/angelica/loading/shared/AngelicaClassDump.java index 080ee9d99..539697c86 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/shared/AngelicaClassDump.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/shared/AngelicaClassDump.java @@ -1,24 +1,18 @@ package com.gtnewhorizons.angelica.loading.shared; -import com.gtnewhorizon.gtnhlib.asm.ASMUtil; import com.gtnewhorizons.angelica.config.SystemProperties; +import com.gtnewhorizons.angelica.glsm.loading.ClassDump; import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; public class AngelicaClassDump { + public static final ClassDump INSTANCE = new ClassDump(SystemProperties.DUMP_CLASS); + public static void dumpClass(String className, byte[] originalBytes, byte[] transformedBytes, Object transformer) { - if (SystemProperties.DUMP_CLASS) { - ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); - ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); - } + INSTANCE.dumpClass(className, originalBytes, transformedBytes, transformer); } public static void dumpRFBClass(String className, ClassNodeHandle classNode, Object transformer) { - if (SystemProperties.DUMP_CLASS) { - final byte[] originalBytes = classNode.getOriginalBytes(); - final byte[] transformedBytes = classNode.computeBytes(); - ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); - ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); - } + INSTANCE.dumpRFBClass(className, classNode, transformer); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java b/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java index 355e7bd41..5ecc71be9 100644 --- a/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java +++ b/src/main/java/com/gtnewhorizons/angelica/loading/shared/transformers/AngelicaRedirector.java @@ -1,45 +1,13 @@ package com.gtnewhorizons.angelica.loading.shared.transformers; -import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; -import org.objectweb.asm.tree.ClassNode; +import com.gtnewhorizons.angelica.glsm.loading.ModRedirector; +import com.gtnewhorizons.angelica.loading.shared.AngelicaClassDump; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Mod-level wrapper around {@link GLSMRedirector}. Adds mod-specific transformer exclusions and delegates all redirect logic to GLSM. - *

- * THIS CLASS MIGHT BE LOADED ON A DIFFERENT CLASS LOADER, - * IT SHOULD NOT CALL ANY CODE FROM THE MAIN MOD - */ public final class AngelicaRedirector { - private final GLSMRedirector core = new GLSMRedirector(); - private final String[] allExclusions; - - public AngelicaRedirector() { - final List exclusions = new ArrayList<>(Arrays.asList(core.getCoreExclusions())); - exclusions.add("com.gtnewhorizons.angelica.lwjgl3."); - exclusions.add("com.gtnewhorizons.angelica.transform"); - allExclusions = exclusions.toArray(new String[0]); + public static ModRedirector create() { + return new ModRedirector(AngelicaClassDump.INSTANCE, "com.gtnewhorizons.angelica.transform"); } - public String[] getTransformerExclusions() { - return allExclusions; - } - - public boolean shouldTransform(byte[] basicClass) { - return core.shouldTransform(basicClass); - } - - /** @return Was the class changed? */ - public boolean transformClassNode(String transformedName, ClassNode cn) { - return core.transformClassNode(transformedName, cn); - } - - /** @return Was the class changed? */ - public boolean transformClassNode(String transformedName, ClassNode cn, boolean lwjgl3Aware) { - return core.transformClassNode(transformedName, cn, lwjgl3Aware); - } + private AngelicaRedirector() {} } diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java index 2d4795963..da1d577c0 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/Mixins.java @@ -103,12 +103,6 @@ public enum Mixins implements IMixins { ) ), - ANGELICA_SDL_GPU_SHADOW_VOXEL_PREPASS(new MixinBuilder("Voxelization compute pre-pass before shadow raster (SDL_GPU)") - .setPhase(Phase.EARLY) - .setApplyIf(() -> SystemProperties.USE_SDL_GPU && SDLGPUGate.isSDLGPUAvailable()) - .addClientMixins("sdlgpu.MixinDefaultChunkRenderer_ShadowVoxelization") - ), - ANGELICA_VBO_CLOUDS( new MixinBuilder() .setApplyIf(() -> AngelicaConfig.enableVBOClouds) @@ -367,7 +361,6 @@ public enum Mixins implements IMixins { "celeritas.terrain.MixinChunkProviderClient" , "celeritas.terrain.MixinMinecraft_ChunkUpdates" , "celeritas.terrain.MixinRenderGlobal" - , "celeritas.terrain.MixinRenderListManager" , "celeritas.terrain.MixinRenderSectionManager" , "celeritas.terrain.MixinWorldClient" , "celeritas.frustum.MixinClippingHelper" @@ -391,7 +384,6 @@ public enum Mixins implements IMixins { , "celeritas.terrain.MixinWorld_AwaitingDescriptor" , "celeritas.terrain.MixinRenderRegion" , "celeritas.terrain.MixinSectionRenderDataStorage" - , "celeritas.terrain.MixinDefaultChunkRenderer" , "celeritas.terrain.MixinDefaultChunkShaderInterface" ) ), diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderListManagerAccessor.java b/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderListManagerAccessor.java deleted file mode 100644 index cc7950e82..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderListManagerAccessor.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.gtnewhorizons.angelica.mixins.interfaces; - -public interface RenderListManagerAccessor { - boolean angelica$hasOcclusionFutureInFlight(); -} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderSectionManagerAccessor.java b/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderSectionManagerAccessor.java index ce69f2691..496068479 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderSectionManagerAccessor.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/interfaces/RenderSectionManagerAccessor.java @@ -2,13 +2,10 @@ package com.gtnewhorizons.angelica.mixins.interfaces; import it.unimi.dsi.fastutil.longs.Long2ReferenceMap; import org.embeddedt.embeddium.impl.render.chunk.RenderSection; -import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegionManager; import java.util.concurrent.ConcurrentLinkedDeque; public interface RenderSectionManagerAccessor { Long2ReferenceMap angelica$getSectionByPosition(); ConcurrentLinkedDeque angelica$getAsyncSubmittedTasks(); - RenderRegionManager angelica$getRegions(); - float angelica$getSearchDistance(); } diff --git a/src/main/java/com/gtnewhorizons/angelica/profiling/TracyFramePlots.java b/src/main/java/com/gtnewhorizons/angelica/profiling/TracyFramePlots.java index 26864b04c..94120a624 100644 --- a/src/main/java/com/gtnewhorizons/angelica/profiling/TracyFramePlots.java +++ b/src/main/java/com/gtnewhorizons/angelica/profiling/TracyFramePlots.java @@ -4,7 +4,6 @@ import com.gtnewhorizons.angelica.dynamiclights.DynamicLights; import com.gtnewhorizons.angelica.glsm.CaptureGate; import com.gtnewhorizons.angelica.glsm.DisplayListManager; import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.rendering.culling.GpuIndirectMultiDrawEmitter; import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.glsm.profiling.TracyBackend; @@ -22,20 +21,18 @@ import net.coderbot.iris.pipeline.ShadowRenderer; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.WorldRenderer; import org.embeddedt.embeddium.impl.render.chunk.SharedQuadIndexBuffer; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.IndirectMultiDrawEmitter; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegionManager; public final class TracyFramePlots { private static long lastChunkUpdates; private static long lastDrawCalls, lastTexBindMisses, lastProgramSwitches, lastListPlaybacks; private static long lastStreamedBytes, lastStreamDraws, lastOrphanFallbacks, lastStreamContiguous; - private static long lastSectionMetaBytes, lastRingWraps, lastVariantSwitches; + private static long lastRingWraps, lastVariantSwitches; private static long lastTesrRebuilds, lastTesrRetainedDraws, lastTesrInstDraws, lastTesrInstInstances; private static long lastTesrStreamedInstances, lastTesrPromotions, lastTesrCacheHits, lastTesrCacheMisses; private static long lastBatcherParts, lastBatcherLiveFallbacks; private static final long[] lastBails = new long[ModelPartBatcher.BailReason.VALUES.length]; - private static long lastSectionsUploaded, lastBytesUploaded; - private static int lastCullSubmitted; + private static long lastSectionsUploaded, lastBytesUploaded, lastIndexBufferGrowths; private static final long P_ALLOC_RATE = Tracy.plotHandle("allocRate", TracyBackend.PLOT_FORMAT_MEMORY); private static final long P_HEAP_USED = Tracy.plotHandle("heapUsed", TracyBackend.PLOT_FORMAT_MEMORY); @@ -61,7 +58,7 @@ public final class TracyFramePlots { private static final long P_VOX_ENCODERS = Tracy.plotHandle("voxelization.encoders"); private static final long P_VOX_DISPATCHES = Tracy.plotHandle("voxelization.dispatches"); private static final long P_VOX_REGIONS = Tracy.plotHandle("voxelization.regions"); - private static final long P_DRAW_ARENA_GROWTHS = Tracy.plotHandle("draw.arenaGrowths"); + private static final long P_DRAW_BATCH_REBUILDS = Tracy.plotHandle("draw.batchRebuilds"); private static final long P_DRAW_INDEX_BUFFER_GROWTHS = Tracy.plotHandle("draw.indexBufferGrowths"); private static final long P_CULL_COMMANDS_SUBMITTED = Tracy.plotHandle("cull.commandsSubmitted"); @@ -175,20 +172,17 @@ public final class TracyFramePlots { lastOrphanFallbacks = delta(P_GL_ORPHAN_FALLBACKS, TessellatorStreamingDrawer.orphanFallbacks, lastOrphanFallbacks); lastStreamContiguous = delta(P_GL_STREAM_CONTIGUOUS, TessellatorStreamingDrawer.streamContiguous, lastStreamContiguous); lastRingWraps = delta(P_GL_RING_WRAPS, TessellatorStreamingDrawer.ringWraps(), lastRingWraps); - lastSectionMetaBytes = delta(P_GL_SECTION_META_BYTES, GpuIndirectMultiDrawEmitter.sectionMetaBytes, lastSectionMetaBytes); + Tracy.plotInt(P_GL_SECTION_META_BYTES, TerrainDrawStats.takeSectionMetaBytes()); Tracy.plotInt(P_SHADOW_RELAY_FRAMES, ShadowRenderer.SHADOW_TERRAIN_RELAID ? 1 : 0); Tracy.plotInt(P_TERRAIN_DRAW_COMMANDS, TerrainDrawStats.takeCommandsSubmitted()); Tracy.plotInt(P_TERRAIN_REGIONS_DRAWN, TerrainDrawStats.takeRegionsDrawn()); - Tracy.plotInt(P_DRAW_ARENA_GROWTHS, IndirectMultiDrawEmitter.takeArenaGrowths()); - Tracy.plotInt(P_DRAW_INDEX_BUFFER_GROWTHS, SharedQuadIndexBuffer.takeGrowths()); + Tracy.plotInt(P_DRAW_BATCH_REBUILDS, TerrainDrawStats.takeBatchRebuilds()); Tracy.plotInt(P_VOX_ENCODERS, SdlShadowVoxelizationSink.takeEncoders()); Tracy.plotInt(P_VOX_DISPATCHES, SdlShadowVoxelizationSink.takeDispatches()); Tracy.plotInt(P_VOX_REGIONS, SdlShadowVoxelizationSink.takeRegions()); - Tracy.plotInt(P_CULL_REGIONS_DRAWN, GpuIndirectMultiDrawEmitter.takeRegionsDrawn()); - Tracy.plotInt(P_CULL_MAX_REGION_COMMANDS, GpuIndirectMultiDrawEmitter.takeMaxRegionCommands()); - final int submitted = GpuIndirectMultiDrawEmitter.commandsSubmitted(); - Tracy.plotInt(P_CULL_COMMANDS_SUBMITTED, submitted - lastCullSubmitted); - lastCullSubmitted = submitted; + Tracy.plotInt(P_CULL_REGIONS_DRAWN, TerrainDrawStats.takeCullRegionsDrawn()); + Tracy.plotInt(P_CULL_MAX_REGION_COMMANDS, TerrainDrawStats.takeCullMaxRegionCommands()); + Tracy.plotInt(P_CULL_COMMANDS_SUBMITTED, TerrainDrawStats.takeCullCommandsSubmitted()); final ShaderManager ffp = ShaderManager.getInstance(); Tracy.plotInt(P_FFP_PRE_DRAW_CALLS, ffp.statLastFramePreDrawCalls()); @@ -231,6 +225,7 @@ public final class TracyFramePlots { lastSectionsUploaded = delta(P_MESH_UPLOADED_SECTIONS, RenderRegionManager.getSectionsUploaded(), lastSectionsUploaded); lastBytesUploaded = delta(P_MESH_UPLOADED_BYTES, RenderRegionManager.getBytesUploaded(), lastBytesUploaded); + lastIndexBufferGrowths = delta(P_DRAW_INDEX_BUFFER_GROWTHS, SharedQuadIndexBuffer.getGrowths(), lastIndexBufferGrowths); Tracy.plotInt(P_MESH_META_SLOTS, GpuCulling.sectionMeta().getHighWaterMark()); Tracy.plotInt(P_MT_QUEUE_DEPTH, AngelicaRenderQueue.getQueueDepth()); Tracy.plotInt(P_MT_QUEUE_TASKS, AngelicaRenderQueue.getLastFrameTasksRan()); diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkRenderer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkRenderer.java index d4a424bf0..9f2920062 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkRenderer.java @@ -1,16 +1,27 @@ package com.gtnewhorizons.angelica.rendering.celeritas; import com.gtnewhorizons.angelica.AngelicaMod; +import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.RenderSystem; import com.gtnewhorizons.angelica.glsm.backend.BackendManager; -import com.gtnewhorizons.angelica.rendering.culling.GpuIndirectMultiDrawEmitter; +import com.gtnewhorizons.angelica.rendering.culling.GpuTerrainCuller; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.proxy.ClientProxy; import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProvider; import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHolder; import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; import me.jellysquid.mods.sodium.client.gui.options.named.MultiDrawMode; +import com.gtnewhorizons.angelica.rendering.voxelization.SdlShadowVoxelizationSink; +import com.gtnewhorizons.angelica.rendering.voxelization.ShadowVoxelizer; +import net.coderbot.iris.Iris; +import net.coderbot.iris.gl.program.ComputeProgram; +import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline; +import net.coderbot.iris.pipeline.WorldRenderingPipeline; +import net.coderbot.iris.shadows.ShadowRenderingState; import org.embeddedt.embeddium.impl.gl.device.CommandList; +import org.embeddedt.embeddium.impl.gl.device.DirectMultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.device.IndirectMultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.device.MultiDrawBatch; import org.embeddedt.embeddium.impl.gl.device.RenderDevice; import org.embeddedt.embeddium.impl.gl.shader.GlProgram; import org.embeddedt.embeddium.impl.gl.shader.GlShader; @@ -19,15 +30,15 @@ import org.embeddedt.embeddium.impl.gl.shader.ShaderParser; import org.embeddedt.embeddium.impl.gl.shader.ShaderType; import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType; import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation; +import org.embeddedt.embeddium.impl.render.chunk.ChunkRenderMatrices; import org.embeddedt.embeddium.impl.render.chunk.DefaultChunkRenderer; import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration; import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage; import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderList; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.DrawCommandSink; +import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderListIterable; +import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler; +import org.embeddedt.embeddium.impl.render.chunk.multidraw.CachedBatch; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.DirectMultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.IndirectMultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.MultiDrawEmitter; import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderBindingPoints; import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderInterface; import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderOptions; @@ -36,7 +47,9 @@ import org.embeddedt.embeddium.impl.render.chunk.shader.DefaultChunkShaderInterf import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; +import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL20; import java.util.ArrayList; import java.util.List; @@ -44,20 +57,26 @@ import java.util.List; class AngelicaChunkRenderer extends DefaultChunkRenderer { private static final int BLOCK_TEXTURE_UNIT = 0; private static final Tracy.ZoneId Z_CHUNK_BEGIN = Tracy.zoneId("chunkBegin", Tracy.COLOR_TERRAIN); - private static final Tracy.ZoneId Z_CHUNK_REGION_FIRST = Tracy.zoneId("chunkRegionFirst", Tracy.COLOR_TERRAIN); - private static final Tracy.ZoneId Z_CHUNK_REGION = Tracy.zoneId("chunkRegion", Tracy.COLOR_TERRAIN); private static final Tracy.ZoneId Z_CHUNK_ASSEMBLE_REGION = Tracy.zoneId("chunkAssembleRegion", Tracy.COLOR_TERRAIN); - private static final Tracy.ZoneId Z_CHUNK_EXECUTE_BATCH = Tracy.zoneId("chunkExecuteBatch", Tracy.COLOR_TERRAIN); private GlProgram irisProgram; private boolean usingIrisProgram; private int rgssSampler; private boolean rgssSamplerResolved; private boolean rgssSamplerBound; - private int regionIndex; + private final GpuTerrainCuller culler; + private final ReusableCachedBatch gpuBatch = new ReusableCachedBatch(); + + private static final ShadowVoxelizer shadowVoxelizer = new ShadowVoxelizer(); + private static final SdlShadowVoxelizationSink shadowVoxelSink = new SdlShadowVoxelizationSink(); + private static int loggedVoxelizationSkips; + private static MultiDrawMode installedBatchMode; public AngelicaChunkRenderer(RenderDevice device, RenderPassConfiguration renderPassConfiguration) { - super(device, renderPassConfiguration, createEmitter()); + super(device, renderPassConfiguration); + + installBatchFactory(); + this.culler = createCuller(); final IrisShaderProvider provider = IrisShaderProviderHolder.getProvider(); if (provider != null) { @@ -65,10 +84,18 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer { } } - private static MultiDrawEmitter createEmitter() { + private static void installBatchFactory() { final MultiDrawMode configured = ClientProxy.options().advanced.multiDrawMode; final MultiDrawMode mode = MultiDrawModeResolver.resolve(); + if (BackendManager.RENDER_BACKEND.isIndirectRequired() && mode != MultiDrawMode.INDIRECT) { + throw new IllegalStateException( + "Indirect multi-draw was required by the backend but resolved mode is " + mode); + } + + if (mode == installedBatchMode) return; + installedBatchMode = mode; + if (mode != configured) { if (mode == MultiDrawMode.INDIRECT) { AngelicaMod.LOGGER.info("Backend requires indirect draw; {} -> INDIRECT", configured); @@ -77,22 +104,40 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer { } } - if (BackendManager.RENDER_BACKEND.isIndirectRequired() && mode != MultiDrawMode.INDIRECT) { - throw new IllegalStateException("Indirect multi-draw was required by the backend but resolved mode is " + mode); - } + BatchAssembler.setBatchFactory(switch (mode) { + case DIRECT -> DirectMultiDrawBatch::new; + case INDIRECT -> IndirectMultiDrawBatch::new; + case INDIVIDUAL -> IndividualDrawBatch::new; + }); + } + private enum VoxelizationSkip { + NO_RENDER_LISTS("pass has no render lists"), + NO_DEFERRED_PIPELINE("no deferred pipeline"), + NO_VOXELIZATION_COMPUTE("pack declares no shadow voxelization"); + + private final String message; + + VoxelizationSkip(String message) { + this.message = message; + } + } + + private static void logVoxelizationSkipOnce(VoxelizationSkip reason) { + final int bit = 1 << reason.ordinal(); + if ((loggedVoxelizationSkips & bit) != 0) return; + loggedVoxelizationSkips |= bit; + AngelicaMod.LOGGER.info("shadow voxelization skipped: {}", reason.message); + } + + private static GpuTerrainCuller createCuller() { final GpuCulling.Availability availability = GpuCulling.availability(); if (availability == GpuCulling.Availability.AVAILABLE) { AngelicaMod.LOGGER.info("Compute-driven chunk culling available, mode={}", GpuCulling.mode()); - return new GpuIndirectMultiDrawEmitter(GpuCulling.culler(), GpuCulling.sectionMeta()); + return new GpuTerrainCuller(GpuCulling.culler(), GpuCulling.sectionMeta()); } AngelicaMod.LOGGER.warn("GPU culling unavailable ({}); terrain will use CPU culling", availability); - - return switch (mode) { - case DIRECT -> new DirectMultiDrawEmitter(); - case INDIRECT -> new IndirectMultiDrawEmitter(); - case INDIVIDUAL -> new IndividualDrawEmitter(); - }; + return null; } private static GlShader loadShader(ShaderType type, String path, ShaderConstants constants) { @@ -102,7 +147,6 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer { @Override protected void begin(TerrainRenderPass pass) { - this.regionIndex = 0; if (Tracy.ENABLED) Tracy.beginZone(Z_CHUNK_BEGIN); try { final IrisShaderProvider provider = IrisShaderProviderHolder.getProvider(); @@ -181,6 +225,10 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer { public void delete(CommandList commandList) { super.delete(commandList); + if (culler != null) { + culler.delete(); + } + unbindRgssSampler(); RenderSystem.destroySampler(rgssSampler); rgssSampler = 0; @@ -210,48 +258,116 @@ class AngelicaChunkRenderer extends DefaultChunkRenderer { } @Override - protected void drawRegion(ChunkShaderInterface shader, CommandList commandList, RenderRegion region, - CameraTransform camera, long timestamp) { - if (Tracy.ENABLED) Tracy.beginZone(regionIndex == 0 ? Z_CHUNK_REGION_FIRST : Z_CHUNK_REGION); + public void render(ChunkRenderMatrices matrices, CommandList commandList, ChunkRenderListIterable renderLists, + TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera) { + if (culler != null) { + culler.beginRenderPass(matrices, renderLists, renderPass, occlusionCamera, camera, useBlockFaceCulling()); + } + try { - regionIndex++; - super.drawRegion(shader, commandList, region, camera, timestamp); + voxelizeShadowTerrain(matrices, renderLists, renderPass, occlusionCamera, camera); + super.render(matrices, commandList, renderLists, renderPass, occlusionCamera, camera); } finally { - if (Tracy.ENABLED) Tracy.endZone(); + if (culler != null) { + culler.endPass(); + } } } @Override - protected void assembleRegion(DrawCommandSink sink, RenderRegion region, SectionRenderDataStorage storage, - ChunkRenderList renderList, CameraTransform occlusionCamera, - TerrainRenderPass renderPass, boolean useBlockFaceCulling) { - if (getEmitter() instanceof GpuIndirectMultiDrawEmitter gpu && gpu.isComputeActiveThisPass()) { - gpu.prepareRegion(region); + protected @Nullable CachedBatch getRegionBatch(CommandList commandList, RenderRegion region, + SectionRenderDataStorage storage, ChunkRenderList renderList, + CameraTransform occlusionCamera, TerrainRenderPass renderPass, + boolean useBlockFaceCulling, + SectionRenderDataStorage.BatchCacheParams cacheParams) { + if (culler != null && culler.isComputeActiveThisPass()) { + final MultiDrawBatch batch = culler.batchForRegion(region); + if (batch == null) return null; + return this.gpuBatch.wrap(batch, prepareTessellation(commandList, region, renderPass)); + } + + if (!Tracy.ENABLED) { + return super.getRegionBatch(commandList, region, storage, renderList, occlusionCamera, renderPass, + useBlockFaceCulling, cacheParams); + } + + final long batchesCreatedBefore = BatchAssembler.getCachedBatchesCreated(); + Tracy.beginZone(Z_CHUNK_ASSEMBLE_REGION); + try { + final CachedBatch cached = super.getRegionBatch(commandList, region, storage, renderList, occlusionCamera, + renderPass, useBlockFaceCulling, cacheParams); + if (BatchAssembler.getCachedBatchesCreated() != batchesCreatedBefore) TerrainDrawStats.recordRebuild(); + final MultiDrawBatch batch = cached != null ? cached.getBatch() : null; + if (batch != null && !batch.isEmpty()) TerrainDrawStats.recordBatch(batch.size()); + return cached; + } finally { + Tracy.endZone(); + } + } + + @Override + protected void executeBatch(CommandList commandList, MultiDrawBatch batch, GlTessellation tessellation, GlPrimitiveType primitiveType) { + TerrainDrawStats.beginExecuteZone(); + try { + super.executeBatch(commandList, batch, tessellation, primitiveType); + } finally { + TerrainDrawStats.endExecuteZone(); + } + } + + private static final class ReusableCachedBatch extends CachedBatch { + private static final byte[] NO_SECTIONS = new byte[0]; + + private MultiDrawBatch batch; + private GlTessellation tessellation; + + ReusableCachedBatch() { + super(null, null, NO_SECTIONS, 0, 0, 0, 0, 0, 0, 0); + } + + CachedBatch wrap(MultiDrawBatch batch, GlTessellation tessellation) { + this.batch = batch; + this.tessellation = tessellation; + return this; + } + + @Override + public MultiDrawBatch getBatch() { + return this.batch; + } + + @Override + public GlTessellation getTessellation() { + return this.tessellation; + } + } + + private void voxelizeShadowTerrain(ChunkRenderMatrices matrices, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera) { + if (!BackendManager.RENDER_BACKEND.isSDLGPU()) return; + if (!ShadowRenderingState.areShadowsCurrentlyBeingRendered()) return; + if (!renderLists.hasPass(renderPass)) { + logVoxelizationSkipOnce(VoxelizationSkip.NO_RENDER_LISTS); return; } - if (Tracy.ENABLED) Tracy.beginZone(Z_CHUNK_ASSEMBLE_REGION); - try { - super.assembleRegion(sink, region, storage, renderList, occlusionCamera, renderPass, useBlockFaceCulling); - } finally { - if (Tracy.ENABLED) Tracy.endZone(); + final WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable(); + if (!(pipeline instanceof DeferredWorldRenderingPipeline deferred)) { + logVoxelizationSkipOnce(VoxelizationSkip.NO_DEFERRED_PIPELINE); + return; + } + final ComputeProgram voxelCompute = deferred.getShadowVoxelizationCompute(); + if (voxelCompute == null) { + logVoxelizationSkipOnce(VoxelizationSkip.NO_VOXELIZATION_COMPUTE); + return; } - } - @Override - protected void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType) { - if (Tracy.ENABLED) { - final MultiDrawEmitter emitter = getEmitter(); - // The compute path reports itself through GpuIndirectMultiDrawEmitter; counting it here too double-counts. - if (!(emitter instanceof GpuIndirectMultiDrawEmitter gpu && gpu.isComputeActiveThisPass())) { - TerrainDrawStats.recordBatch(emitter.getPendingCommandCount()); - } - Tracy.beginZone(Z_CHUNK_EXECUTE_BATCH); - } + final int prevProgram = GLStateManager.glGetInteger(GL20.GL_CURRENT_PROGRAM); + voxelCompute.use(); + deferred.prepareShadowVoxelizationCompute(matrices.modelView()); try { - super.executeBatch(commandList, tessellation, primitiveType); + shadowVoxelizer.walkPass(renderLists, renderPass, renderPass.vertexType().getVertexFormat(), camera, occlusionCamera, useBlockFaceCulling(), shadowVoxelSink); } finally { - if (Tracy.ENABLED) Tracy.endZone(); + if (prevProgram != 0) GLStateManager.glUseProgram(prevProgram); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java index ee99e4841..3a41a1ae1 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java @@ -5,7 +5,6 @@ import com.gtnewhorizons.angelica.compat.ModStatus; import com.gtnewhorizons.angelica.compat.cubicchunks.CubicChunksAPI; import com.gtnewhorizons.angelica.glsm.profiling.Tracy; import com.gtnewhorizons.angelica.glsm.profiling.TracyBackend; -import com.gtnewhorizons.angelica.mixins.interfaces.RenderListManagerAccessor; import com.gtnewhorizons.angelica.mixins.interfaces.RenderSectionManagerAccessor; import com.gtnewhorizons.angelica.proxy.ClientProxy; import com.gtnewhorizons.angelica.rendering.AngelicaRenderQueue; @@ -50,6 +49,7 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { private static final long P_MESH_SCHEDULED_JOBS = Tracy.plotHandle("mesh.scheduledJobs"); private static final long P_MESH_BUSY_THREADS = Tracy.plotHandle("mesh.busyThreads"); private static final long P_MESH_DEVICE_USED = Tracy.plotHandle("mesh.deviceUsed", TracyBackend.PLOT_FORMAT_MEMORY); + private static final long P_DRAW_REGION_BUFFERS = Tracy.plotHandle("draw.regionBuffers"); private static final long P_MESH_DEVICE_ALLOCATED = Tracy.plotHandle("mesh.deviceAllocated", TracyBackend.PLOT_FORMAT_MEMORY); private static final Tracy.ZoneId Z_BIOME_REBUILDS = Tracy.zoneId("biomeRebuilds", Tracy.COLOR_TERRAIN); @@ -91,27 +91,12 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { this.cameraPosition.set(x, y, z); } - private boolean isChunkNotLoaded(int chunkX, int chunkZ) { - return this.world.getChunkFromChunkCoords(chunkX, chunkZ).isEmpty(); - } - - @Override - public void onSectionAdded(int x, int y, int z) { - super.onSectionAdded(x, y, z); - - // If chunk isn't actually loaded (EmptyChunk placeholder), make sections opaque for main pass. - // This prevents BFS from traversing through chunks we haven't received from the server. - if (isChunkNotLoaded(x, z)) { - renderListManager.updateVisibilityData(x, y, z, 0L); - } - } - @Override public void update(Viewport positionedViewport, int frame, boolean spectator) { - if (isInShadowPass() && !needsUpdate()) { + if (isInShadowPass()) { return; } - if (!isInShadowPass() && !initialCameraSectionReady) { + if (!initialCameraSectionReady) { var origin = positionedViewport.getChunkCoord(); long key = PositionUtil.packSection(origin.x(), origin.y(), origin.z()); if (!((RenderSectionManagerAccessor) this).angelica$getSectionByPosition().containsKey(key)) { @@ -132,6 +117,11 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { return true; } + @Override + protected boolean useRasterOcclusionCulling() { + return false; + } + @Override protected boolean useFogOcclusion() { return ClientProxy.options().performance.useFogOcclusion && !IrisShaderProviderHolder.isActive(); @@ -267,14 +257,15 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { } } - public boolean preSubmitShadowGraphUpdate(Viewport viewport, int frame, boolean spectator) { - if (this.shadowRenderListManager == null || !this.shadowRenderListManager.isNeedsUpdate()) return false; - if (((RenderListManagerAccessor) this.shadowRenderListManager).angelica$hasOcclusionFutureInFlight()) return false; + @Override + public boolean canSubmitShadowGraphSearch() { + return super.canSubmitShadowGraphSearch() && this.shadowRenderListManager.isNeedsUpdate() + && !this.shadowRenderListManager.hasOcclusionFutureInFlight(); + } - final RenderSectionManagerAccessor self = (RenderSectionManagerAccessor) this; - final int targetQueueSize = shouldRespectUpdateTaskQueueSizeLimit() ? (int) Math.min(Integer.MAX_VALUE, (long) getBuilder().getTargetQueueSize() * 10) : Integer.MAX_VALUE; - this.shadowRenderListManager.startGraphUpdate(viewport, frame, self.angelica$getRegions().getRegionIdsLength(), self.angelica$getSearchDistance(), shouldUseOcclusionCulling(viewport, spectator), targetQueueSize); - return true; + @Override + public boolean submitShadowGraphSearch(Viewport viewport, int frame) { + return super.submitShadowGraphSearch(viewport, frame); } public boolean isShadowGraphDirty() { @@ -361,5 +352,6 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { final var mem = this.getDeviceMemoryStats(); Tracy.plotInt(P_MESH_DEVICE_USED, mem.deviceUsed + mem.indexUsed); Tracy.plotInt(P_MESH_DEVICE_ALLOCATED, mem.deviceAllocated + mem.indexAllocated); + Tracy.plotInt(P_DRAW_REGION_BUFFERS, mem.bufferCount); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java index 4793c2f74..4c34c7646 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java @@ -248,12 +248,19 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer list, TileEntityRenderContext context) { - for (int i = 0; i < list.size(); i++) { - final TileEntity te = list.get(i); + collectVisibleBlockEntities(list); + } + + private int collectVisibleBlockEntities(List blockEntities) { + int count = 0; + for (int i = 0; i < blockEntities.size(); i++) { + final TileEntity te = blockEntities.get(i); if (isTileEntityVisible(te)) { frameTEs.add(te); + count++; } } + return count; } private CameraState lastMainCameraState; @@ -290,7 +297,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer 0) { - LWJGL.glDrawElementsBaseVertex(mode, count, type, LWJGL.memGetAddress(batch.pElementPointer + (long) i * POINTER_SIZE), LWJGL.memGetInt(batch.pBaseVertex + (long) i * Integer.BYTES)); - } - } - } - } - - @Override - public void delete() { - this.batch.delete(); - } -} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/TerrainDrawStats.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/TerrainDrawStats.java index b4b450379..93a7577f9 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/TerrainDrawStats.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/TerrainDrawStats.java @@ -1,17 +1,55 @@ package com.gtnewhorizons.angelica.rendering.celeritas; +import com.gtnewhorizons.angelica.glsm.profiling.Tracy; + public final class TerrainDrawStats { + private static final Tracy.ZoneId Z_EXECUTE_BATCH = Tracy.zoneId("chunkExecuteBatch", Tracy.COLOR_TERRAIN); + private static int commandsSubmitted; private static int regionsDrawn; + private static int batchRebuilds; + + private static int cullCommandsSubmitted; + private static int cullRegionsDrawn; + private static int cullMaxRegionCommands; + private static long sectionMetaBytes; private TerrainDrawStats() { } + public static void beginExecuteZone() { + if (Tracy.ENABLED) Tracy.beginZone(Z_EXECUTE_BATCH); + } + + public static void endExecuteZone() { + if (Tracy.ENABLED) Tracy.endZone(); + } + public static void recordBatch(int commandCount) { commandsSubmitted += commandCount; regionsDrawn++; } + public static void recordRebuild() { + batchRebuilds++; + } + + public static void recordCullRegion(int commandCount) { + cullCommandsSubmitted += commandCount; + cullRegionsDrawn++; + if (commandCount > cullMaxRegionCommands) cullMaxRegionCommands = commandCount; + } + + public static void recordSectionMetaBytes(int bytes) { + sectionMetaBytes += bytes; + } + + public static int takeBatchRebuilds() { + final int n = batchRebuilds; + batchRebuilds = 0; + return n; + } + public static int takeCommandsSubmitted() { final int n = commandsSubmitted; commandsSubmitted = 0; @@ -23,4 +61,28 @@ public final class TerrainDrawStats { regionsDrawn = 0; return n; } + + public static int takeCullCommandsSubmitted() { + final int n = cullCommandsSubmitted; + cullCommandsSubmitted = 0; + return n; + } + + public static int takeCullRegionsDrawn() { + final int n = cullRegionsDrawn; + cullRegionsDrawn = 0; + return n; + } + + public static int takeCullMaxRegionCommands() { + final int n = cullMaxRegionCommands; + cullMaxRegionCommands = 0; + return n; + } + + public static long takeSectionMetaBytes() { + final long n = sectionMetaBytes; + sectionMetaBytes = 0; + return n; + } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/api/IrisShaderProvider.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/api/IrisShaderProvider.java index 525d2c184..2433f25e0 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/api/IrisShaderProvider.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/api/IrisShaderProvider.java @@ -28,5 +28,5 @@ public interface IrisShaderProvider { /** Block render layer overrides from shader pack */ @Nullable Map getBlockTypeIds(); - default void preSubmitShadowGraph(int frame, boolean spectator) {} + default void preSubmitShadowGraph(int frame) {} } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexEncoder.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexEncoder.java index c712da25c..30b81618e 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexEncoder.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexEncoder.java @@ -19,9 +19,6 @@ import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material; import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder; import org.joml.Vector3f; -/** - * Writes Iris-extended vertices. Delegates base 28 bytes to VANILLA_LIKE encoder. - */ public class IrisExtendedChunkVertexEncoder implements ContextAwareChunkVertexEncoder { // Offsets derived from format definition private static final int MID_TEX_OFFSET = IrisExtendedChunkVertexType.VERTEX_FORMAT.getAttribute("mc_midTexCoord").getPointer(); @@ -101,6 +98,11 @@ public class IrisExtendedChunkVertexEncoder implements ContextAwareChunkVertexEn } } + @Override + public boolean supportsBilinearCorrection() { + return false; + } + @Override public long write(long ptr, Material material, Vertex vertex, int sectionIndex) { uSum += vertex.u; @@ -109,6 +111,7 @@ public class IrisExtendedChunkVertexEncoder implements ContextAwareChunkVertexEn final BlockRenderContext ctx = context; + vertex.rdhFactor = 0; baseEncoder.write(ptr, material, vertex, sectionIndex); // Per-vertex: mc_Entity (packed blockId + renderType), midBlock, lightValue diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexType.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexType.java index 967f23fe6..7f5fb4942 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexType.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/iris/IrisExtendedChunkVertexType.java @@ -6,22 +6,15 @@ import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkMeshFormats; import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder; import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType; -/** - * VANILLA_LIKE (28 bytes) + Iris extensions (20 bytes) = 48 bytes total. - * - * Iris extensions: - * mc_midTexCoord ushort[2] - quad center UV (average of 4 vertices) - * at_tangent byte[4] - tangent vector, normalized - * iris_Normal byte[3]+1 - face normal, normalized - * mc_Entity uint - packed as ((blockId + 1) << 1) | (renderType & 1) - * at_midBlock byte[4] - (xyz offset from block center, lightValue) - */ +import java.util.stream.Collectors; + public class IrisExtendedChunkVertexType implements ChunkVertexType { public static final ChunkVertexType BASE_TYPE = ChunkMeshFormats.VANILLA_LIKE; public static final int STRIDE = 48; public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE) - .addAllElements(BASE_TYPE.getVertexFormat()) + .addElements(BASE_TYPE.getVertexFormat().getAttributes().stream() + .filter(a -> !a.getName().equals("a_RdhFactor")).collect(Collectors.toList())) .addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) .addElement("at_tangent", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 4, true, false) .addElement("iris_Normal", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 3, true, false) diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuCulledMultiDrawBatch.java b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuCulledMultiDrawBatch.java new file mode 100644 index 000000000..345748436 --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuCulledMultiDrawBatch.java @@ -0,0 +1,46 @@ +package com.gtnewhorizons.angelica.rendering.culling; + +import org.embeddedt.embeddium.impl.gl.device.CommandList; +import org.embeddedt.embeddium.impl.gl.device.MultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType; +import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation; + +public final class GpuCulledMultiDrawBatch extends MultiDrawBatch { + + private final GpuTerrainCuller culler; + private int drawStart; + + GpuCulledMultiDrawBatch(GpuTerrainCuller culler) { + super(MAX_COMMAND_COUNT); + this.culler = culler; + } + + void prepare(int drawStart, int drawCount, int maxElementCount) { + this.drawStart = drawStart; + this.size = drawCount; + this.maxElementCount = maxElementCount; + } + + @Override + public void appendDrawCommand(int baseVertex, int elementCount, long elementPointer) { + throw new UnsupportedOperationException("GPU-culled batches build their commands on the GPU"); + } + + @Override + public void mergeIntoLastCommand(int additionalElementCount) { + throw new UnsupportedOperationException("GPU-culled batches build their commands on the GPU"); + } + + @Override + public void upload(CommandList commandList) { + } + + @Override + public void delete() { + } + + @Override + public void execute(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType) { + this.culler.drawRegionRange(commandList, tessellation, primitiveType, this.drawStart, this.size); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuIndirectMultiDrawEmitter.java b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuIndirectMultiDrawEmitter.java deleted file mode 100644 index e20b8c912..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuIndirectMultiDrawEmitter.java +++ /dev/null @@ -1,382 +0,0 @@ -package com.gtnewhorizons.angelica.rendering.culling; - -import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.glsm.backend.BackendManager; -import com.gtnewhorizons.angelica.glsm.profiling.Tracy; -import it.unimi.dsi.fastutil.objects.Reference2LongOpenHashMap; -import lombok.Getter; -import lombok.Setter; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.embeddedt.embeddium.impl.gl.device.CommandList; -import org.embeddedt.embeddium.impl.gl.device.DrawCommandList; -import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType; -import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.DrawCommandSink; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.IndirectMultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.MultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL15; -import org.lwjgl.opengl.GL40; -import org.lwjgl.opengl.GL43; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -public final class GpuIndirectMultiDrawEmitter implements MultiDrawEmitter { - - private static final Logger LOG = LogManager.getLogger("Angelica-Culling"); - - private static final int ENTRY_BYTES = GpuDrivenChunkCuller.VISIBLE_ENTRY_BYTES; - - private final GpuDrivenChunkCuller culler; - private final SectionMetaBuffer sectionMeta; - - private ByteBuffer visibleStaging; - private ByteBuffer visibleStagingView; - private int stagingCapacityEntries; - - private int visibleSsboGlId; - private int indirectSsboGlId; - private int gpuCapacityEntries; - - private static final class PassState { - int entryBase; - int entryCount; - int indexPointerMask; - int maxElementCount; - boolean dispatched; - final Reference2LongOpenHashMap regionRanges = new Reference2LongOpenHashMap<>(); - final Reference2LongOpenHashMap regionEntryRanges = new Reference2LongOpenHashMap<>(); - - PassState() { - regionRanges.defaultReturnValue(-1L); - regionEntryRanges.defaultReturnValue(-1L); - } - - void reset(int mask, int base) { - entryBase = base; - entryCount = 0; - indexPointerMask = mask; - maxElementCount = 0; - dispatched = false; - regionRanges.clear(); - regionEntryRanges.clear(); - } - } - - private final PassState primaryPass = new PassState(); - private final PassState secondPass = new PassState(); - private PassState buildPass = primaryPass; - private PassState current = primaryPass; - private boolean secondPrepared; - private int totalAppendedEntries; - @Getter private boolean computeActiveThisPass; - - private int currentDrawStart; - private int currentDrawCount; - private int currentEntryStart; - private int currentEntryCount; - private RenderRegion currentRegion; - - @Setter private ByteBuffer frustumUboBytes; - - private IndirectMultiDrawEmitter cpuFallback; - - public static long sectionMetaBytes; - - private static int commandsSubmitted; - private static int regionsDrawn; - private static int maxRegionCommands; - public static int commandsSubmitted() { return commandsSubmitted; } - public static int takeRegionsDrawn() { final int n = regionsDrawn; regionsDrawn = 0; return n; } - public static int takeMaxRegionCommands() { final int n = maxRegionCommands; maxRegionCommands = 0; return n; } - - private final SectionMetaBuffer.Sink uploadSectionMetaSink; - - public GpuIndirectMultiDrawEmitter(GpuDrivenChunkCuller culler, SectionMetaBuffer sectionMeta) { - this.culler = culler; - this.sectionMeta = sectionMeta; - this.uploadSectionMetaSink = buf -> { - final int bytes = buf.remaining(); - if (!culler.uploadSectionMeta(buf)) return false; - if (Tracy.ENABLED) sectionMetaBytes += bytes; - return true; - }; - } - - public void beginCullPass(int indexPointerMask) { - this.computeActiveThisPass = GpuCulling.mode().computeEnabled(); - this.totalAppendedEntries = 0; - this.secondPrepared = false; - primaryPass.reset(indexPointerMask, 0); - this.buildPass = primaryPass; - this.current = primaryPass; - this.currentDrawStart = 0; - this.currentDrawCount = 0; - this.currentEntryStart = 0; - this.currentEntryCount = 0; - if (computeActiveThisPass && cpuFallback != null) { - cpuFallback.getCommandSink().clear(); - } - } - - private IndirectMultiDrawEmitter cpuFallback() { - if (cpuFallback == null) cpuFallback = new IndirectMultiDrawEmitter(); - return cpuFallback; - } - - public void beginCombinedPasses(int firstMask, int secondMask) { - beginCullPass(firstMask); - secondPass.reset(secondMask, 0); - } - - public void startSecondPass() { - primaryPass.entryCount = totalAppendedEntries - primaryPass.entryBase; - secondPass.entryBase = totalAppendedEntries; - this.buildPass = secondPass; - this.current = secondPass; - } - - public void finishCombinedBuild() { - secondPass.entryCount = totalAppendedEntries - secondPass.entryBase; - this.secondPrepared = true; - this.buildPass = primaryPass; - this.current = primaryPass; - } - - public boolean selectPreparedSecondPass() { - if (!secondPrepared || !computeActiveThisPass) return false; - this.current = secondPass; - this.currentDrawStart = 0; - this.currentDrawCount = 0; - this.currentEntryStart = 0; - this.currentEntryCount = 0; - return true; - } - - public void reserveSections(int count) { - if (count > 0) ensureStagingCapacity(totalAppendedEntries + count); - } - - public int appendSection(int slot, int facingMask, int outputBase) { - if ((outputBase & ~0x00FFFFFF) != 0) { - throw new IllegalStateException("packed outputBase overflow: " + outputBase + " (max " + 0x00FFFFFF + ")"); - } - if (totalAppendedEntries >= stagingCapacityEntries) ensureStagingCapacity(totalAppendedEntries + 1); - final int idx = totalAppendedEntries; - final int off = idx * ENTRY_BYTES; - visibleStaging.putInt(off + 0, slot); - visibleStaging.putInt(off + 4, (outputBase << 8) | (facingMask & 0xFF)); - totalAppendedEntries = idx + 1; - buildPass.entryCount = totalAppendedEntries - buildPass.entryBase; - visibleStagingView.limit(totalAppendedEntries * ENTRY_BYTES); - return idx; - } - - public void recordRegion(RenderRegion region, int drawStart, int drawCount, int entryStart, int entryCount, int maxElementCount) { - if (drawCount <= 0) return; - buildPass.regionRanges.put(region, (((long) drawStart) << 32) | (drawCount & 0xFFFFFFFFL)); - buildPass.regionEntryRanges.put(region, (((long) entryStart) << 32) | (entryCount & 0xFFFFFFFFL)); - if (maxElementCount > buildPass.maxElementCount) buildPass.maxElementCount = maxElementCount; - } - - public void prepareRegion(RenderRegion region) { - currentRegion = region; - final long packed = current.regionRanges.getLong(region); - if (packed < 0L) { - currentDrawStart = 0; - currentDrawCount = 0; - } else { - currentDrawStart = (int) (packed >>> 32); - currentDrawCount = (int) (packed & 0xFFFFFFFFL); - } - final long entryPacked = current.regionEntryRanges.getLong(region); - if (entryPacked < 0L) { - currentEntryStart = 0; - currentEntryCount = 0; - } else { - currentEntryStart = (int) (entryPacked >>> 32); - currentEntryCount = (int) (entryPacked & 0xFFFFFFFFL); - } - } - - public void syncSectionMetaIfDirty() { - culler.ensureReady(); - sectionMeta.syncIfDirty(uploadSectionMetaSink); - } - - public void endPass() { - currentDrawStart = 0; - currentDrawCount = 0; - currentEntryStart = 0; - currentEntryCount = 0; - if (current == secondPass) secondPrepared = false; - } - - public void dispatchPreparedPasses() { - if (!computeActiveThisPass || totalAppendedEntries == 0) return; - if (!needsDispatch(primaryPass) && (!secondPrepared || !needsDispatch(secondPass))) return; - if (!culler.ensureReady()) return; - if (frustumUboBytes == null) { - LOG.warn("GpuIndirectMultiDrawEmitter: frustum UBO not set before dispatch; skipping cull"); - return; - } - - ensureGpuBuffers(totalAppendedEntries); - visibleStagingView.position(0).limit(totalAppendedEntries * ENTRY_BYTES); - GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, visibleSsboGlId); - GLStateManager.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0L, visibleStagingView); - - BackendManager.RENDER_BACKEND.beginComputeDispatchBatch(); - try { - dispatchPass(primaryPass); - if (secondPrepared) dispatchPass(secondPass); - } finally { - BackendManager.RENDER_BACKEND.endComputeDispatchBatch(); - } - } - - private static boolean needsDispatch(PassState r) { - return !r.dispatched && r.entryCount > 0; - } - - private void dispatchPass(PassState r) { - if (!needsDispatch(r)) return; - FrustumExtractor.patchControl(r.entryCount, r.indexPointerMask, frustumUboBytes); - FrustumExtractor.patchBatchEntryBase(r.entryBase, frustumUboBytes); - culler.uploadFrustum(frustumUboBytes); - culler.dispatch(visibleSsboGlId, indirectSsboGlId, r.entryCount); - r.dispatched = true; - } - - private void ensureGpuBuffers(int maxVisible) { - if (visibleSsboGlId == 0) visibleSsboGlId = GLStateManager.glGenBuffers(); - if (indirectSsboGlId == 0) indirectSsboGlId = GLStateManager.glGenBuffers(); - if (maxVisible <= gpuCapacityEntries) return; - final int newCap = Math.max(maxVisible, Math.max(gpuCapacityEntries * 2, 64)); - GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, visibleSsboGlId); - GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, (long) newCap * ENTRY_BYTES, GL15.GL_STREAM_DRAW); - GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, indirectSsboGlId); - final int indirectBytes = newCap * GpuDrivenChunkCuller.FACINGS_PER_SECTION * GpuDrivenChunkCuller.INDIRECT_COMMAND_BYTES; - GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, ByteBuffer.allocateDirect(indirectBytes).order(ByteOrder.nativeOrder()), GL15.GL_STREAM_DRAW); - gpuCapacityEntries = newCap; - } - - private void ensureStagingCapacity(int needed) { - if (visibleStaging != null && needed <= stagingCapacityEntries) return; - final int newCap = Math.max(needed, Math.max(stagingCapacityEntries * 2, 64)); - final ByteBuffer next = MemoryUtilities.memAlloc(newCap * ENTRY_BYTES).order(ByteOrder.nativeOrder()); - if (visibleStaging != null) { - visibleStaging.position(0).limit(totalAppendedEntries * ENTRY_BYTES); - next.put(visibleStaging); - MemoryUtilities.memFree(visibleStaging); - } - next.position(0); - next.limit(newCap * ENTRY_BYTES); - visibleStaging = next; - visibleStagingView = visibleStaging.duplicate().order(ByteOrder.nativeOrder()); - stagingCapacityEntries = newCap; - } - - private final DrawCommandSink computeSink = new DrawCommandSink() { - @Override - public void clear() { - } - - @Override - public int size() { - return currentDrawCount; - } - - @Override - public int getIndexBufferSize() { - return current.maxElementCount; - } - - @Override - public void push(int baseVertex, int elementCount, long indexOffset) { - } - }; - - @Override - public DrawCommandSink getCommandSink() { - return computeActiveThisPass ? computeSink : cpuFallback().getCommandSink(); - } - - @Override - public boolean batchesWholePass() { - return !computeActiveThisPass && cpuFallback().batchesWholePass(); - } - - @Override - public void beginPass(CommandList commandList, int sectionCount) { - if (!computeActiveThisPass) cpuFallback().beginPass(commandList, sectionCount); - } - - @Override - public void finishAssembly(CommandList commandList) { - if (!computeActiveThisPass) cpuFallback().finishAssembly(commandList); - } - - @Override - public void selectDrawRange(int firstCommand, int commandCount) { - if (!computeActiveThisPass) cpuFallback().selectDrawRange(firstCommand, commandCount); - } - - @Override - public int getPendingCommandCount() { - return computeActiveThisPass ? currentDrawCount : cpuFallback().getPendingCommandCount(); - } - - @Override - public void onPassFinished(CommandList commandList) { - if (!computeActiveThisPass) cpuFallback().onPassFinished(commandList); - } - - @Override - public void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType) { - if (!computeActiveThisPass) { - cpuFallback().executeBatch(commandList, tessellation, primitiveType); - return; - } - if (currentDrawCount == 0) return; - dispatchPreparedPasses(); - if (!current.dispatched) return; - - if (Tracy.ENABLED) { - commandsSubmitted += currentDrawCount; - regionsDrawn++; - if (currentDrawCount > maxRegionCommands) maxRegionCommands = currentDrawCount; - } - - final long offsetBytes = (long) currentDrawStart * GpuDrivenChunkCuller.INDIRECT_COMMAND_BYTES; - GLStateManager.glBindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, indirectSsboGlId); - try (DrawCommandList ignored = commandList.beginTessellating(tessellation)) { - GLStateManager.glMultiDrawElementsIndirect(primitiveType.getId(), GL11.GL_UNSIGNED_INT, offsetBytes, currentDrawCount, 0); - } - GLStateManager.glBindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, 0); - } - - @Override - public void delete() { - if (visibleStaging != null) { - MemoryUtilities.memFree(visibleStaging); - visibleStaging = null; - visibleStagingView = null; - stagingCapacityEntries = 0; - } - if (visibleSsboGlId != 0) { GLStateManager.glDeleteBuffers(visibleSsboGlId); visibleSsboGlId = 0; } - if (indirectSsboGlId != 0) { GLStateManager.glDeleteBuffers(indirectSsboGlId); indirectSsboGlId = 0; } - gpuCapacityEntries = 0; - currentRegion = null; - frustumUboBytes = null; - primaryPass.regionRanges.clear(); - primaryPass.regionEntryRanges.clear(); - secondPass.regionRanges.clear(); - secondPass.regionEntryRanges.clear(); - if (cpuFallback != null) cpuFallback.delete(); - } -} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuTerrainCuller.java b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuTerrainCuller.java new file mode 100644 index 000000000..a5e68111e --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/GpuTerrainCuller.java @@ -0,0 +1,543 @@ +package com.gtnewhorizons.angelica.rendering.culling; + +import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.profiling.Tracy; +import com.gtnewhorizons.angelica.mixins.interfaces.SectionRenderDataStorageRegionAccessor; +import com.gtnewhorizons.angelica.rendering.celeritas.AngelicaRenderPassConfiguration; +import com.gtnewhorizons.angelica.rendering.celeritas.TerrainDrawStats; +import lombok.Getter; +import net.coderbot.iris.pipeline.ShadowRenderer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.embeddedt.embeddium.impl.gl.device.CommandList; +import org.embeddedt.embeddium.impl.gl.device.DrawCommandList; +import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType; +import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation; +import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; +import org.embeddedt.embeddium.impl.render.chunk.ChunkRenderMatrices; +import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex; +import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType; +import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage; +import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe; +import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderList; +import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderListIterable; +import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler; +import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; +import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; +import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; +import org.joml.Matrix4f; +import org.joml.Matrix4fc; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL40; +import org.lwjgl.opengl.GL43; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; + +public final class GpuTerrainCuller { + + private static final Logger LOG = LogManager.getLogger("Angelica-Culling"); + + private static final int ENTRY_BYTES = GpuDrivenChunkCuller.VISIBLE_ENTRY_BYTES; + + private final GpuDrivenChunkCuller culler; + private final SectionMetaBuffer sectionMeta; + + private ByteBuffer visibleStaging; + private ByteBuffer visibleStagingView; + private int stagingCapacityEntries; + + private int visibleSsboGlId; + private int indirectSsboGlId; + private int gpuCapacityEntries; + + private static final long[] EMPTY_RANGES = new long[0]; + private static final int[] EMPTY_STAMPS = new int[0]; + + private static final class PassState { + int entryBase; + int entryCount; + int indexPointerMask; + int maxElementCount; + boolean dispatched; + + int stamp; + long[] regionRanges = EMPTY_RANGES; + int[] regionStamps = EMPTY_STAMPS; + + void reset(int mask, int base) { + entryBase = base; + entryCount = 0; + indexPointerMask = mask; + maxElementCount = 0; + dispatched = false; + stamp++; + if (stamp == 0) { + Arrays.fill(regionStamps, 0); + stamp = 1; + } + } + + void putRange(int regionId, long packed) { + if (regionId >= regionStamps.length) { + final int cap = Math.max(regionId + 1, Math.max(regionStamps.length * 2, 64)); + regionRanges = Arrays.copyOf(regionRanges, cap); + regionStamps = Arrays.copyOf(regionStamps, cap); + } + regionRanges[regionId] = packed; + regionStamps[regionId] = stamp; + } + + long getRange(int regionId) { + if (regionId >= regionStamps.length || regionStamps[regionId] != stamp) return -1L; + return regionRanges[regionId]; + } + + void releaseRanges() { + regionRanges = EMPTY_RANGES; + regionStamps = EMPTY_STAMPS; + } + } + + private final PassState primaryPass = new PassState(); + private final PassState secondPass = new PassState(); + private PassState buildPass = primaryPass; + private PassState current = primaryPass; + private boolean secondPrepared; + private int totalAppendedEntries; + @Getter private boolean computeActiveThisPass; + + private int currentDrawStart; + private int currentDrawCount; + private boolean indirectBufferBound; + + private ByteBuffer frustumUboBytes; + + private final GpuCulledMultiDrawBatch batch = new GpuCulledMultiDrawBatch(this); + + private ByteBuffer uboBytes; + private Matrix4f mvp; + private Matrix4f mvNoTranslation; + private int passOutputBase; + private ChunkPrimitiveType walkPrimitiveType; + + private final SectionMetaBuffer.Sink uploadSectionMetaSink; + + public GpuTerrainCuller(GpuDrivenChunkCuller culler, SectionMetaBuffer sectionMeta) { + this.culler = culler; + this.sectionMeta = sectionMeta; + this.uploadSectionMetaSink = buf -> { + final int bytes = buf.remaining(); + if (!culler.uploadSectionMeta(buf)) return false; + if (Tracy.ENABLED) TerrainDrawStats.recordSectionMetaBytes(bytes); + return true; + }; + } + + void beginCullPass(int indexPointerMask) { + this.computeActiveThisPass = GpuCulling.mode().computeEnabled(); + this.totalAppendedEntries = 0; + this.secondPrepared = false; + primaryPass.reset(indexPointerMask, 0); + this.buildPass = primaryPass; + this.current = primaryPass; + this.currentDrawStart = 0; + this.currentDrawCount = 0; + } + + void beginCombinedPasses(int firstMask, int secondMask) { + beginCullPass(firstMask); + secondPass.reset(secondMask, 0); + } + + void startSecondPass() { + primaryPass.entryCount = totalAppendedEntries - primaryPass.entryBase; + secondPass.entryBase = totalAppendedEntries; + this.buildPass = secondPass; + this.current = secondPass; + } + + void finishCombinedBuild() { + secondPass.entryCount = totalAppendedEntries - secondPass.entryBase; + this.secondPrepared = true; + this.buildPass = primaryPass; + this.current = primaryPass; + } + + boolean selectPreparedSecondPass() { + if (!secondPrepared || !computeActiveThisPass) return false; + this.current = secondPass; + this.currentDrawStart = 0; + this.currentDrawCount = 0; + return true; + } + + void reserveSections(int count) { + if (count > 0) ensureStagingCapacity(totalAppendedEntries + count); + } + + void appendSection(int slot, int facingMask, int outputBase) { + if ((outputBase & ~0x00FFFFFF) != 0) { + throw new IllegalStateException("packed outputBase overflow: " + outputBase + " (max " + 0x00FFFFFF + ")"); + } + if (totalAppendedEntries >= stagingCapacityEntries) ensureStagingCapacity(totalAppendedEntries + 1); + final int off = totalAppendedEntries * ENTRY_BYTES; + visibleStaging.putInt(off + 0, slot); + visibleStaging.putInt(off + 4, (outputBase << 8) | (facingMask & 0xFF)); + totalAppendedEntries++; + buildPass.entryCount = totalAppendedEntries - buildPass.entryBase; + } + + void recordRegion(RenderRegion region, int drawStart, int drawCount, int maxElementCount) { + if (drawCount <= 0) return; + buildPass.putRange(region.getId(), (((long) drawStart) << 32) | (drawCount & 0xFFFFFFFFL)); + if (maxElementCount > buildPass.maxElementCount) buildPass.maxElementCount = maxElementCount; + } + + private void prepareRegion(RenderRegion region) { + final long packed = current.getRange(region.getId()); + if (packed < 0L) { + currentDrawStart = 0; + currentDrawCount = 0; + } else { + currentDrawStart = (int) (packed >>> 32); + currentDrawCount = (int) (packed & 0xFFFFFFFFL); + } + } + + void syncSectionMetaIfDirty() { + culler.ensureReady(); + sectionMeta.syncIfDirty(uploadSectionMetaSink); + } + + public void endPass() { + if (indirectBufferBound) { + GLStateManager.glBindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, 0); + indirectBufferBound = false; + } + currentDrawStart = 0; + currentDrawCount = 0; + if (current == secondPass) secondPrepared = false; + } + + private void dispatchPreparedPasses() { + if (!computeActiveThisPass || totalAppendedEntries == 0) return; + if (!needsDispatch(primaryPass) && (!secondPrepared || !needsDispatch(secondPass))) return; + if (!culler.ensureReady()) return; + if (frustumUboBytes == null) { + LOG.warn("GpuTerrainCuller: frustum UBO not set before dispatch; skipping cull"); + return; + } + + ensureGpuBuffers(totalAppendedEntries); + visibleStagingView.position(0).limit(totalAppendedEntries * ENTRY_BYTES); + GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, visibleSsboGlId); + GLStateManager.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0L, visibleStagingView); + + BackendManager.RENDER_BACKEND.beginComputeDispatchBatch(); + try { + dispatchPass(primaryPass); + if (secondPrepared) dispatchPass(secondPass); + } finally { + BackendManager.RENDER_BACKEND.endComputeDispatchBatch(); + } + } + + private static boolean needsDispatch(PassState r) { + return !r.dispatched && r.entryCount > 0; + } + + private void dispatchPass(PassState r) { + if (!needsDispatch(r)) return; + FrustumExtractor.patchControl(r.entryCount, r.indexPointerMask, frustumUboBytes); + FrustumExtractor.patchBatchEntryBase(r.entryBase, frustumUboBytes); + culler.uploadFrustum(frustumUboBytes); + culler.dispatch(visibleSsboGlId, indirectSsboGlId, r.entryCount); + r.dispatched = true; + } + + private void ensureGpuBuffers(int maxVisible) { + if (visibleSsboGlId == 0) visibleSsboGlId = GLStateManager.glGenBuffers(); + if (indirectSsboGlId == 0) indirectSsboGlId = GLStateManager.glGenBuffers(); + if (maxVisible <= gpuCapacityEntries) return; + final int newCap = Math.max(maxVisible, Math.max(gpuCapacityEntries * 2, 64)); + GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, visibleSsboGlId); + GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, (long) newCap * ENTRY_BYTES, GL15.GL_STREAM_DRAW); + GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, indirectSsboGlId); + GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, (long) newCap * GpuDrivenChunkCuller.FACINGS_PER_SECTION * GpuDrivenChunkCuller.INDIRECT_COMMAND_BYTES, GL15.GL_STREAM_DRAW); + gpuCapacityEntries = newCap; + } + + private void ensureStagingCapacity(int needed) { + if (visibleStaging != null && needed <= stagingCapacityEntries) return; + final int newCap = Math.max(needed, Math.max(stagingCapacityEntries * 2, 64)); + final ByteBuffer next = MemoryUtilities.memAlloc(newCap * ENTRY_BYTES).order(ByteOrder.nativeOrder()); + if (visibleStaging != null) { + visibleStaging.position(0).limit(totalAppendedEntries * ENTRY_BYTES); + next.put(visibleStaging); + MemoryUtilities.memFree(visibleStaging); + } + next.position(0); + next.limit(newCap * ENTRY_BYTES); + visibleStaging = next; + visibleStagingView = visibleStaging.duplicate().order(ByteOrder.nativeOrder()); + stagingCapacityEntries = newCap; + } + + public void beginRenderPass(ChunkRenderMatrices matrices, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera, boolean useBlockFaceCulling) { + if (renderPass == AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS && renderLists.hasPass(renderPass) + && selectPreparedSecondPass()) { + return; + } + if (!renderLists.hasPass(renderPass)) return; + + final boolean combined = renderPass == AngelicaRenderPassConfiguration.SOLID_PASS && AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS != null && renderLists.hasPass(AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS); + + final int indexPointerMask = renderPass.isSorted() ? 0xFFFFFFFF : 0; + if (combined) { + beginCombinedPasses(0, 0); + } else { + beginCullPass(indexPointerMask); + } + passOutputBase = 0; + walkPrimitiveType = null; + + if (!computeActiveThisPass) { + return; + } + + if (uboBytes == null) { + uboBytes = FrustumExtractor.allocateUboByteBuffer(); + } + final boolean shadow = ShadowRenderer.ACTIVE; + final Matrix4fc proj = matrices.projection(); + final Matrix4fc mv = matrices.modelView(); + if (mvp == null) mvp = new Matrix4f(); + if (mvNoTranslation == null) mvNoTranslation = new Matrix4f(); + mvNoTranslation.set(mv).setTranslation(0f, 0f, 0f); + proj.mul(mvNoTranslation, mvp); + final float camX = (float) camera.intX + camera.fracX; + final float camY = (float) camera.intY + camera.fracY; + final float camZ = (float) camera.intZ + camera.fracZ; + FrustumExtractor.writeStd140(mvp, 0, 0, uboBytes); + FrustumExtractor.patchCameraWorld(camX, camY, camZ, uboBytes); + FrustumExtractor.patchBypassFrustum(shadow, uboBytes); + + frustumUboBytes = uboBytes; + + syncSectionMetaIfDirty(); + + walkPass(renderLists, renderPass, occlusionCamera, useBlockFaceCulling); + if (combined) { + startSecondPass(); + walkPass(renderLists, AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS, occlusionCamera, useBlockFaceCulling); + finishCombinedBuild(); + } + + if (walkPrimitiveType != null) { + FrustumExtractor.patchPrimitiveRatio(walkPrimitiveType.getVerticesPerPrimitive(), walkPrimitiveType.getIndexBufferElementsPerPrimitive(), uboBytes); + } + } + + private void walkPass(ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, boolean useBlockFaceCulling) { + if (renderPass.isSorted()) { + walkSortedPass(renderLists, renderPass); + } else { + walkMergedPass(renderLists, renderPass, occlusionCamera, useBlockFaceCulling); + } + } + + private void walkMergedPass(ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, boolean useBlockFaceCulling) { + final boolean reverse = renderPass.isReverseOrder(); + final int step = reverse ? -1 : 1; + final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride(); + final int cameraX = occlusionCamera.intX; + final int cameraY = occlusionCamera.intY; + final int cameraZ = occlusionCamera.intZ; + + final int n = renderLists.getNumRegions(); + for (int regionIndex = 0; regionIndex < n; regionIndex++) { + final ChunkRenderList list = renderLists.getRegion(reverse ? n - 1 - regionIndex : regionIndex); + final RenderRegion region = list.getRegion(); + final SectionRenderDataStorage storage = region.getStorage(renderPass); + if (storage == null) continue; + final byte[] sections = list.getSectionsWithGeometry(); + final int sectionCount = list.getSectionsWithGeometryCount(); + if (sectionCount == 0) continue; + final SectionRenderDataStorageRegionAccessor accessor = (SectionRenderDataStorageRegionAccessor) storage; + final ChunkPrimitiveType primitiveType = storage.getPrimitiveType(); + trackPrimitiveType(primitiveType); + final int[] slotCache = accessor.angelica$getSlotCache(); + if (slotCache == null) continue; + + reserveSections(sectionCount); + + final long pBase = storage.getRowBasePointer(); + final int regionOriginX = region.getChunkX(); + final int regionOriginY = region.getChunkY(); + final int regionOriginZ = region.getChunkZ(); + final int uniformMask = useBlockFaceCulling ? BatchAssembler.uniformCullMask(region, occlusionCamera) : ModelQuadFacing.ALL; + + final int regionDrawStart = passOutputBase; + int regionDrawCount = 0; + int regionMaxElems = 0; + + int cursor = reverse ? sectionCount - 1 : 0; + + for (int i = 0; i < sectionCount; i++, cursor += step) { + final int sectionIndex = sections[cursor] & 0xFF; + + int slices = uniformMask != BatchAssembler.MASK_NOT_UNIFORM ? uniformMask : BatchAssembler.getVisibleFaces(cameraX, cameraY, cameraZ, regionOriginX + LocalSectionIndex.unpackX(sectionIndex), regionOriginY + LocalSectionIndex.unpackY(sectionIndex), regionOriginZ + LocalSectionIndex.unpackZ(sectionIndex)); + slices &= storage.getSliceMask(sectionIndex); + if (slices == 0) continue; + + final int slot = slotCache[sectionIndex]; + if (slot < 0) continue; + + final long pMeshData = pBase + (sectionIndex * stride); + final long runs = BatchAssembler.packRuns(slices & 0x7F); + final int sectionDraws = BatchAssembler.runCount(runs); + appendSection(slot, slices, passOutputBase); + passOutputBase += sectionDraws; + regionDrawCount += sectionDraws; + + int maxElems = 0; + for (int run = 0; run < sectionDraws; run++) { + final int startVertex = SectionRenderDataUnsafe.Strategy.COMPACT.getVertexOffset(pMeshData, BatchAssembler.runFirst(runs, run)); + final int endVertex = SectionRenderDataUnsafe.Strategy.COMPACT.getRunVertexEnd(pMeshData, BatchAssembler.runLast(runs, run), primitiveType); + final int ec = SectionRenderDataUnsafe.elementsForVertices(endVertex - startVertex, primitiveType); + if (ec > maxElems) maxElems = ec; + } + if (maxElems > regionMaxElems) regionMaxElems = maxElems; + } + + if (regionDrawCount > 0) { + recordRegion(region, regionDrawStart, regionDrawCount, regionMaxElems); + } + } + } + + private void walkSortedPass(ChunkRenderListIterable renderLists, TerrainRenderPass renderPass) { + final boolean reverse = renderPass.isReverseOrder(); + final int step = reverse ? -1 : 1; + final long stride = SectionRenderDataUnsafe.Strategy.FULL.getStride(); + + final int n = renderLists.getNumRegions(); + for (int regionIndex = 0; regionIndex < n; regionIndex++) { + final ChunkRenderList list = renderLists.getRegion(reverse ? n - 1 - regionIndex : regionIndex); + final RenderRegion region = list.getRegion(); + final SectionRenderDataStorage storage = region.getStorage(renderPass); + if (storage == null) continue; + final byte[] sections = list.getSectionsWithGeometry(); + final int sectionCount = list.getSectionsWithGeometryCount(); + if (sectionCount == 0) continue; + final SectionRenderDataStorageRegionAccessor accessor = (SectionRenderDataStorageRegionAccessor) storage; + final ChunkPrimitiveType primitiveType = storage.getPrimitiveType(); + trackPrimitiveType(primitiveType); + final int[] slotCache = accessor.angelica$getSlotCache(); + if (slotCache == null) continue; + + reserveSections(sectionCount); + + final long pBase = storage.getRowBasePointer(); + + final int regionDrawStart = passOutputBase; + int regionDrawCount = 0; + int regionMaxElems = 0; + + int cursor = reverse ? sectionCount - 1 : 0; + + for (int i = 0; i < sectionCount; i++, cursor += step) { + final int sectionIndex = sections[cursor] & 0xFF; + + final int slices = ModelQuadFacing.ALL & storage.getSliceMask(sectionIndex); + if (slices == 0) continue; + + final int slot = slotCache[sectionIndex]; + if (slot < 0) continue; + + final long pMeshData = pBase + (sectionIndex * stride); + final int sectionDraws = Integer.bitCount(slices); + appendSection(slot, slices, passOutputBase); + passOutputBase += sectionDraws; + regionDrawCount += sectionDraws; + + int maxElems = 0; + for (int f = 0; f < ModelQuadFacing.COUNT; f++) { + final int ec = SectionRenderDataUnsafe.Strategy.FULL.getElementCount(pMeshData, f, primitiveType); + if (ec > maxElems) maxElems = ec; + } + if (maxElems > regionMaxElems) regionMaxElems = maxElems; + } + + if (regionDrawCount > 0) { + recordRegion(region, regionDrawStart, regionDrawCount, regionMaxElems); + } + } + } + + private void trackPrimitiveType(ChunkPrimitiveType primitiveType) { + if (walkPrimitiveType == null) { + walkPrimitiveType = primitiveType; + } else if (walkPrimitiveType.getVerticesPerPrimitive() != primitiveType.getVerticesPerPrimitive() || walkPrimitiveType.getIndexBufferElementsPerPrimitive() != primitiveType.getIndexBufferElementsPerPrimitive()) { + throw new IllegalStateException("Batched cull dispatch spans passes with differing primitive ratios: " + walkPrimitiveType + " vs " + primitiveType); + } + } + + public GpuCulledMultiDrawBatch batchForRegion(RenderRegion region) { + prepareRegion(region); + if (currentDrawCount == 0) return null; + batch.prepare(currentDrawStart, currentDrawCount, current.maxElementCount); + return batch; + } + + void drawRegionRange(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType, int drawStart, int drawCount) { + if (drawCount == 0) return; + dispatchPreparedPasses(); + if (!current.dispatched) return; + + if (Tracy.ENABLED) { + TerrainDrawStats.recordCullRegion(drawCount); + } + + if (!indirectBufferBound) { + GLStateManager.glBindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, indirectSsboGlId); + indirectBufferBound = true; + } + + final long offsetBytes = (long) drawStart * GpuDrivenChunkCuller.INDIRECT_COMMAND_BYTES; + try (DrawCommandList ignored = commandList.beginTessellating(tessellation)) { + GLStateManager.glMultiDrawElementsIndirect(primitiveType.getId(), GL11.GL_UNSIGNED_INT, offsetBytes, drawCount, 0); + } + } + + public void delete() { + if (indirectBufferBound) { + GLStateManager.glBindBuffer(GL40.GL_DRAW_INDIRECT_BUFFER, 0); + indirectBufferBound = false; + } + if (visibleStaging != null) { + MemoryUtilities.memFree(visibleStaging); + visibleStaging = null; + visibleStagingView = null; + stagingCapacityEntries = 0; + } + if (visibleSsboGlId != 0) { + GLStateManager.glDeleteBuffers(visibleSsboGlId); + visibleSsboGlId = 0; + } + if (indirectSsboGlId != 0) { + GLStateManager.glDeleteBuffers(indirectSsboGlId); + indirectSsboGlId = 0; + } + gpuCapacityEntries = 0; + frustumUboBytes = null; + uboBytes = null; + primaryPass.releaseRanges(); + secondPass.releaseRanges(); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBuffer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBuffer.java index 44a7bab9a..3bf3b86d8 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBuffer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBuffer.java @@ -40,7 +40,7 @@ public final class SectionMetaBuffer { } public synchronized int update(int passIndex, int regionOriginX, int regionOriginY, int regionOriginZ, int localSectionIndex, - long dataPtr, SectionRenderDataUnsafe.Strategy layout, ChunkPrimitiveType primitiveType) { + long dataPtr, int sliceMask, SectionRenderDataUnsafe.Strategy layout, ChunkPrimitiveType primitiveType) { if (dataPtr == 0L) return -1; final int sectionOriginX = regionOriginX + (((localSectionIndex >> 5) & 0x07) << 4); final int sectionOriginY = regionOriginY + (((localSectionIndex ) & 0x03) << 4); @@ -51,7 +51,7 @@ public final class SectionMetaBuffer { slot = allocateSlot(); keyToSlot.put(key, slot); } - writeSlot(slot, sectionOriginX, sectionOriginY, sectionOriginZ, dataPtr, layout, primitiveType); + writeSlot(slot, sectionOriginX, sectionOriginY, sectionOriginZ, dataPtr, sliceMask, layout, primitiveType); dirty = true; return slot; } @@ -97,12 +97,12 @@ public final class SectionMetaBuffer { capacity = newCapacity; } - private void writeSlot(int slot, int sectionOriginX, int sectionOriginY, int sectionOriginZ, long dataPtr, SectionRenderDataUnsafe.Strategy layout, ChunkPrimitiveType primitiveType) { + private void writeSlot(int slot, int sectionOriginX, int sectionOriginY, int sectionOriginZ, long dataPtr, int sliceMask, SectionRenderDataUnsafe.Strategy layout, ChunkPrimitiveType primitiveType) { final int base = slot * BYTES_PER_SECTION; mirror.putInt(base + 0, sectionOriginX); mirror.putInt(base + 4, sectionOriginY); mirror.putInt(base + 8, sectionOriginZ); - mirror.putInt(base + OFFSET_SLICE_MASK, SectionRenderDataUnsafe.getSliceMask(dataPtr)); + mirror.putInt(base + OFFSET_SLICE_MASK, sliceMask); for (int facing = 0; facing < FACING_COUNT; facing++) { mirror.putInt(base + OFFSET_POSTS + facing * 4, layout.getVertexOffset(dataPtr, facing)); } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/SdlShadowVoxelizationSink.java b/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/SdlShadowVoxelizationSink.java index c9e21ca74..2c593df95 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/SdlShadowVoxelizationSink.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/SdlShadowVoxelizationSink.java @@ -32,7 +32,7 @@ public final class SdlShadowVoxelizationSink implements ShadowVoxelizer.Sink { @Override public boolean region(RenderRegion region, GlVertexFormat format, float offsetX, float offsetY, float offsetZ) { - final RenderRegion.DeviceResources resources = region.getResources(format); + final RenderRegion.DeviceResources resources = region.getResources(); if (resources == null) return false; final GlBuffer vertexBuffer = resources.getVertexBuffer(); if (vertexBuffer == null || vertexBuffer.handle() == 0) return false; diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizer.java index c73fead06..5f70f7120 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizer.java @@ -13,8 +13,6 @@ import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; -import java.util.Iterator; - public final class ShadowVoxelizer { public interface Sink { @@ -29,16 +27,23 @@ public final class ShadowVoxelizer { public void walkPass(ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, GlVertexFormat format, CameraTransform camera, CameraTransform occlusionCamera, boolean useBlockFaceCulling, Sink sink) { try { - final Iterator it = renderLists.iterator(renderPass.isReverseOrder()); - while (it.hasNext()) { - walkRegion(it.next(), renderPass, format, camera, occlusionCamera, useBlockFaceCulling, sink); + final boolean reverse = renderPass.isReverseOrder(); + final int n = renderLists.getNumRegions(); + if (renderPass.isSorted()) { + for (int i = 0; i < n; i++) { + walkRegionFull(renderLists.getRegion(reverse ? n - 1 - i : i), renderPass, format, camera, sink); + } + } else { + for (int i = 0; i < n; i++) { + walkRegionCompact(renderLists.getRegion(reverse ? n - 1 - i : i), renderPass, format, camera, occlusionCamera, useBlockFaceCulling, sink); + } } } finally { sink.finish(); } } - private static void walkRegion(ChunkRenderList list, TerrainRenderPass renderPass, GlVertexFormat format, CameraTransform camera, CameraTransform occlusionCamera, boolean useBlockFaceCulling, Sink sink) { + private static void walkRegionCompact(ChunkRenderList list, TerrainRenderPass renderPass, GlVertexFormat format, CameraTransform camera, CameraTransform occlusionCamera, boolean useBlockFaceCulling, Sink sink) { final RenderRegion region = list.getRegion(); final SectionRenderDataStorage storage = region.getStorage(renderPass); if (storage == null) return; @@ -47,7 +52,8 @@ public final class ShadowVoxelizer { if (sectionCount == 0) return; final byte[] sections = list.getSectionsWithGeometry(); - final SectionRenderDataUnsafe.Strategy layout = storage.getStorageStrategy(); + final long pBase = storage.getRowBasePointer(); + final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride(); final ChunkPrimitiveType primitiveType = storage.getPrimitiveType(); final int regionChunkX = region.getChunkX(); final int regionChunkY = region.getChunkY(); @@ -56,32 +62,27 @@ public final class ShadowVoxelizer { for (int i = 0; i < sectionCount; i++) { final int sectionIndex = sections[i] & 0xFF; - final long pMeshData = storage.getDataPointer(sectionIndex); int slices = useBlockFaceCulling ? BatchAssembler.getVisibleFaces(occlusionCamera.intX, occlusionCamera.intY, occlusionCamera.intZ, regionChunkX + LocalSectionIndex.unpackX(sectionIndex), regionChunkY + LocalSectionIndex.unpackY(sectionIndex), regionChunkZ + LocalSectionIndex.unpackZ(sectionIndex)) : ModelQuadFacing.ALL; - slices &= SectionRenderDataUnsafe.getSliceMask(pMeshData); + slices &= storage.getSliceMask(sectionIndex); slices &= 0x7F; if (slices == 0) continue; + final long pMeshData = pBase + sectionIndex * stride; final long runs = BatchAssembler.packRuns(slices); final int runCount = BatchAssembler.runCount(runs); for (int run = 0; run < runCount; run++) { - final int startVertex = layout.getVertexOffset(pMeshData, BatchAssembler.runFirst(runs, run)); - final int endVertex = layout.getRunVertexEnd(pMeshData, BatchAssembler.runLast(runs, run), primitiveType); + final int startVertex = SectionRenderDataUnsafe.Strategy.COMPACT.getVertexOffset(pMeshData, BatchAssembler.runFirst(runs, run)); + final int endVertex = SectionRenderDataUnsafe.Strategy.COMPACT.getRunVertexEnd(pMeshData, BatchAssembler.runLast(runs, run), primitiveType); final int vertexCount = endVertex - startVertex; if (vertexCount <= 0) continue; if (!bound) { - if (!sink.region(region, format, - cameraRelative(region.getOriginX(), camera.intX, camera.fracX), - cameraRelative(region.getOriginY(), camera.intY, camera.fracY), - cameraRelative(region.getOriginZ(), camera.intZ, camera.fracZ))) { - return; - } + if (!bindRegion(region, format, camera, sink)) return; bound = true; } if (!sink.range(startVertex, vertexCount)) return; @@ -89,6 +90,50 @@ public final class ShadowVoxelizer { } } + private static void walkRegionFull(ChunkRenderList list, TerrainRenderPass renderPass, GlVertexFormat format, CameraTransform camera, Sink sink) { + final RenderRegion region = list.getRegion(); + final SectionRenderDataStorage storage = region.getStorage(renderPass); + if (storage == null) return; + + final int sectionCount = list.getSectionsWithGeometryCount(); + if (sectionCount == 0) return; + + final byte[] sections = list.getSectionsWithGeometry(); + final long pBase = storage.getRowBasePointer(); + final long stride = SectionRenderDataUnsafe.Strategy.FULL.getStride(); + final ChunkPrimitiveType primitiveType = storage.getPrimitiveType(); + boolean bound = false; + + for (int i = 0; i < sectionCount; i++) { + final int sectionIndex = sections[i] & 0xFF; + final int slices = ModelQuadFacing.ALL & storage.getSliceMask(sectionIndex); + if (slices == 0) continue; + + final long pMeshData = pBase + sectionIndex * stride; + final long runs = BatchAssembler.packRuns(slices); + final int runCount = BatchAssembler.runCount(runs); + for (int run = 0; run < runCount; run++) { + final int startVertex = SectionRenderDataUnsafe.Strategy.FULL.getVertexOffset(pMeshData, BatchAssembler.runFirst(runs, run)); + final int endVertex = SectionRenderDataUnsafe.Strategy.FULL.getRunVertexEnd(pMeshData, BatchAssembler.runLast(runs, run), primitiveType); + final int vertexCount = endVertex - startVertex; + if (vertexCount <= 0) continue; + + if (!bound) { + if (!bindRegion(region, format, camera, sink)) return; + bound = true; + } + if (!sink.range(startVertex, vertexCount)) return; + } + } + } + + private static boolean bindRegion(RenderRegion region, GlVertexFormat format, CameraTransform camera, Sink sink) { + return sink.region(region, format, + cameraRelative(region.getOriginX(), camera.intX, camera.fracX), + cameraRelative(region.getOriginY(), camera.intY, camera.fracY), + cameraRelative(region.getOriginZ(), camera.intZ, camera.fracZ)); + } + private static float cameraRelative(int originBlock, int cameraBlock, float cameraFrac) { return (originBlock - cameraBlock) - cameraFrac; } diff --git a/src/main/java/net/coderbot/iris/celeritas/IrisCeleritasShaderProvider.java b/src/main/java/net/coderbot/iris/celeritas/IrisCeleritasShaderProvider.java index 647326d97..7db1ba836 100644 --- a/src/main/java/net/coderbot/iris/celeritas/IrisCeleritasShaderProvider.java +++ b/src/main/java/net/coderbot/iris/celeritas/IrisCeleritasShaderProvider.java @@ -67,9 +67,9 @@ public class IrisCeleritasShaderProvider implements IrisShaderProvider { } @Override - public void preSubmitShadowGraph(int frame, boolean spectator) { + public void preSubmitShadowGraph(int frame) { final WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable(); - if (pipeline != null) pipeline.preSubmitShadowGraph(frame, spectator); + if (pipeline != null) pipeline.preSubmitShadowGraph(frame); } public void deleteShaders() { diff --git a/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java b/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java index 212f38346..f1a61c8e9 100644 --- a/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java +++ b/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java @@ -1908,8 +1908,8 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R } @Override - public void preSubmitShadowGraph(int frame, boolean spectator) { - if (shadowRenderer != null) shadowRenderer.preSubmitGraphUpdate(frame, spectator); + public void preSubmitShadowGraph(int frame) { + if (shadowRenderer != null) shadowRenderer.preSubmitGraphUpdate(frame); } @Override diff --git a/src/main/java/net/coderbot/iris/pipeline/ShadowRenderer.java b/src/main/java/net/coderbot/iris/pipeline/ShadowRenderer.java index 06613498e..5bdd0a9c3 100644 --- a/src/main/java/net/coderbot/iris/pipeline/ShadowRenderer.java +++ b/src/main/java/net/coderbot/iris/pipeline/ShadowRenderer.java @@ -127,15 +127,41 @@ public class ShadowRenderer { private final CelestialUniforms celestialUniforms; + private static final class MemoizedBoxCuller { + private BoxCuller culler; + private double lastDistance; + + BoxCuller get(double distance) { + if (culler == null) { + culler = new BoxCuller(distance); + lastDistance = distance; + } else if (lastDistance != distance) { + culler.setMaxDistance(distance); + lastDistance = distance; + } + return culler; + } + } + private static final class FrustumCaches { final AdvancedShadowCullingFrustum advancedFrustum = new AdvancedShadowCullingFrustum(); - BoxCuller boxCuller; + final MemoizedBoxCuller boxCuller = new MemoizedBoxCuller(); + final MemoizedBoxCuller advancedBoxCuller = new MemoizedBoxCuller(); + final MemoizedBoxCuller safeZoneDistanceCuller = new MemoizedBoxCuller(); + SafeZoneCullingFrustum safeZoneFrustum; BoxCullingFrustum boxCullingFrustum; - BoxCuller advancedBoxCuller; - double lastBoxCullerDistance = -1; - double lastAdvancedBoxCullerDistance = -1; + String distanceInfo = "(unavailable)"; + String cullingInfo = "(unavailable)"; + int infoBranch = -1; + double infoDistance = Double.NaN; + int infoRenderDistanceBlocks = -1; + boolean infoHasSafeZone; + boolean infoPackSetter; } + private static final int INFO_BRANCH_DISTANCE_ONLY = 0; + private static final int INFO_BRANCH_ADVANCED = 1; + private static final long SHADOW_RELAY_MAX_AGE_NANOS = 250_000_000L; public static boolean SHADOW_TERRAIN_RELAID = true; @@ -145,7 +171,7 @@ public class ShadowRenderer { private final FrustumCaches entityFrustumCaches = new FrustumCaches(); private float lastGraphShadowAngle = Float.NaN; private float relayShadowAngle = Float.NaN; - private FrustumHolder preSubmitFrustumHolder = new FrustumHolder(); + private FrustumHolder preSubmitFrustumHolder = new FrustumHolder("preSubmit"); private final FrustumCaches preSubmitFrustumCaches = new FrustumCaches(); private boolean preSubmitActive; private float preSubmittedShadowAngle = Float.NaN; @@ -159,29 +185,28 @@ public class ShadowRenderer { AngelicaConfig.shadowGraphHorizonScale); } - public void preSubmitGraphUpdate(int frame, boolean spectator) { + public void preSubmitGraphUpdate(int frame) { final AngelicaRenderSectionManager rsm = CeleritasWorldRenderer.getInstance().getRenderSectionManager(); final float currentShadowAngle = getShadowAngle(); if (ShadowGraphGate.shouldMarkDirty(lastGraphShadowAngle, currentShadowAngle, shadowAngleDelta())) { rsm.markShadowGraphDirty(); lastGraphShadowAngle = currentShadowAngle; } - if (!rsm.isShadowGraphDirty()) return; + if (!rsm.canSubmitShadowGraphSearch()) return; preSubmitFrustumHolder = createShadowFrustum(renderDistanceMultiplier, preSubmitFrustumHolder, preSubmitFrustumCaches); if (!(preSubmitFrustumHolder.getFrustum() instanceof ViewportProvider provider)) return; final Vector3d entityPos = Camera.INSTANCE.getEntityPos(); preSubmitFrustumHolder.getFrustum().setPosition(entityPos.x, entityPos.y, entityPos.z); - if (rsm.preSubmitShadowGraphUpdate(provider.sodium$createViewport(), frame, spectator)) { + if (rsm.submitShadowGraphSearch(provider.sodium$createViewport(), frame)) { preSubmittedShadowAngle = lastGraphShadowAngle; preSubmitActive = true; } } private long lastRelayNanos; private final Vector3f shadowLightVectorCache = new Vector3f(); - private BoxCuller cachedTileEntityCuller; - private double lastTileEntityCullerDistance = -1; + private final MemoizedBoxCuller tileEntityCuller = new MemoizedBoxCuller(); private final boolean shouldRenderDH; private final float nearPlane, farPlane; private final BooleanSupplier packUsesShadowtex1; @@ -214,8 +239,8 @@ public class ShadowRenderer { debugStringOverall = "half plane = " + halfPlaneLength + " meters @ " + resolution + "x" + resolution; - this.terrainFrustumHolder = new FrustumHolder(); - this.entityFrustumHolder = new FrustumHolder(); + this.terrainFrustumHolder = new FrustumHolder("terrain"); + this.entityFrustumHolder = new FrustumHolder("entity"); this.fov = shadowDirectives.getFov(); this.targets = shadowRenderTargets; @@ -390,117 +415,122 @@ public class ShadowRenderer { private FrustumHolder createShadowFrustum(float renderMultiplier, FrustumHolder holder, FrustumCaches caches) { // TODO: Cull entities / block entities with Advanced Frustum Culling even if voxelization is detected. - String distanceInfo; - String cullingInfo; + final int renderDistanceBlocks = Minecraft.getMinecraft().gameSettings.renderDistanceChunks * 16; + if ((packCullingState == ShadowCullState.DISTANCE || packHasVoxelization) && packCullingState != ShadowCullState.ADVANCED && packCullingState != ShadowCullState.SAFE_ZONE) { - double distance = halfPlaneLength * renderMultiplier; + final double distance = halfPlaneLength * renderMultiplier; + final boolean packSetter = packCullingState == ShadowCullState.DISTANCE; + final boolean capped = distance <= 0 || distance > renderDistanceBlocks; - String reason; + if (infoInputsChanged(caches, INFO_BRANCH_DISTANCE_ONLY, distance, renderDistanceBlocks, false, packSetter)) { + final String reason = packSetter ? "(set by shader pack)" : "(voxelization detected)"; - if (packCullingState == ShadowCullState.DISTANCE) { - reason = "(set by shader pack)"; - } else /*if (packHasVoxelization)*/ { - reason = "(voxelization detected)"; + if (capped) { + caches.distanceInfo = renderDistanceBlocks + " blocks (capped by normal render distance)"; + caches.cullingInfo = "disabled " + reason; + } else { + caches.distanceInfo = distance + " blocks (set by shader pack)"; + caches.cullingInfo = "distance only " + reason; + } } - if (distance <= 0 || distance > Minecraft.getMinecraft().gameSettings.renderDistanceChunks * 16) { - distanceInfo = Minecraft.getMinecraft().gameSettings.renderDistanceChunks * 16 - + " blocks (capped by normal render distance)"; - cullingInfo = "disabled " + reason; - return holder.setInfo(NON_CULLING_FRUSTUM, distanceInfo, cullingInfo); - } else { - distanceInfo = distance + " blocks (set by shader pack)"; - cullingInfo = "distance only " + reason; - holder.setInfo(getOrCreateBoxCullingFrustum(distance, caches), distanceInfo, cullingInfo); + if (capped) { + return applyInfo(holder, NON_CULLING_FRUSTUM, caches); } + + return applyInfo(holder, getOrCreateBoxCullingFrustum(distance, caches), caches); } else { - BoxCuller boxCuller; - - boolean hasSafeZone = packCullingState == ShadowCullState.SAFE_ZONE; + final boolean hasSafeZone = packCullingState == ShadowCullState.SAFE_ZONE; if (hasSafeZone && renderMultiplier < 0) renderMultiplier = 1.0f; + final boolean userSetter = renderMultiplier < 0; double distance = (hasSafeZone ? voxelDistance : halfPlaneLength) * renderMultiplier; - String setter = "(set by shader pack)"; - if (renderMultiplier < 0) { + if (userSetter) { // TODO: GUI distance = IrisVideoSettings.shadowDistance * 16; // can be zero :( - setter = "(set by user)"; } - if (distance >= Minecraft.getMinecraft().gameSettings.renderDistanceChunks * 16 && !hasSafeZone) { - distanceInfo = Minecraft.getMinecraft().gameSettings.renderDistanceChunks * 16 - + " blocks (capped by normal render distance)"; - boxCuller = null; - } else { - distanceInfo = distance + " blocks " + setter; + final boolean capped = distance >= renderDistanceBlocks && !hasSafeZone; + final boolean cullEverything = !capped && distance == 0.0 && !hasSafeZone; - if (distance == 0.0 && !hasSafeZone) { - cullingInfo = "no shadows rendered"; - return holder.setInfo(CULL_EVERYTHING_FRUSTUM, distanceInfo, cullingInfo); + if (infoInputsChanged(caches, INFO_BRANCH_ADVANCED, distance, renderDistanceBlocks, hasSafeZone, !userSetter)) { + if (capped) { + caches.distanceInfo = renderDistanceBlocks + " blocks (capped by normal render distance)"; + } else { + caches.distanceInfo = distance + " blocks " + (userSetter ? "(set by user)" : "(set by shader pack)"); } - boxCuller = getOrCreateAdvancedBoxCuller(distance, caches); + if (cullEverything) { + caches.cullingInfo = "no shadows rendered"; + } else { + caches.cullingInfo = hasSafeZone ? "Safe Zone Frustum Culling enabled" : "Advanced Occlusion Culling enabled"; + } } - cullingInfo = (hasSafeZone ? "Safe Zone" : "Advanced") + " Frustum Culling enabled"; + if (cullEverything) { + return applyInfo(holder, CULL_EVERYTHING_FRUSTUM, caches); + } + + final BoxCuller boxCuller = capped ? null : caches.advancedBoxCuller.get(distance); final Vector4f shadowLightPosition = celestialUniforms.getShadowLightPositionInWorldSpace(); shadowLightVectorCache.set(shadowLightPosition.x(), shadowLightPosition.y(), shadowLightPosition.z()); shadowLightVectorCache.normalize(); - Matrix4fc projView = ((shouldRenderDH && DHCompat.hasRenderingEnabled()) ? DHCompat.getProjection() : RenderingState.INSTANCE.getProjectionMatrix()); + final Matrix4fc projView = ((shouldRenderDH && DHCompat.hasRenderingEnabled()) ? DHCompat.getProjection() : RenderingState.INSTANCE.getProjectionMatrix()); if (hasSafeZone) { - BoxCuller distanceCuller = new BoxCuller(halfPlaneLength * renderMultiplier); - SafeZoneCullingFrustum safeZoneFrustum = new SafeZoneCullingFrustum( - RenderingState.INSTANCE.getModelViewMatrix(), projView, - shadowLightVectorCache, boxCuller, distanceCuller); - return holder.setInfo(safeZoneFrustum, distanceInfo, cullingInfo); + if (caches.safeZoneFrustum == null) { + caches.safeZoneFrustum = new SafeZoneCullingFrustum(); + } + caches.safeZoneFrustum.init(RenderingState.INSTANCE.getModelViewMatrix(), projView, shadowLightVectorCache, + boxCuller, caches.safeZoneDistanceCuller.get(halfPlaneLength * renderMultiplier)); + return applyInfo(holder, caches.safeZoneFrustum, caches); } else { caches.advancedFrustum.init(RenderingState.INSTANCE.getModelViewMatrix(), projView, shadowLightVectorCache, boxCuller); - return holder.setInfo(caches.advancedFrustum, distanceInfo, cullingInfo); + return applyInfo(holder, caches.advancedFrustum, caches); } } + } + + private static boolean infoInputsChanged(FrustumCaches caches, int branch, double distance, int renderDistanceBlocks, boolean hasSafeZone, boolean packSetter) { + if (caches.infoBranch == branch && caches.infoDistance == distance + && caches.infoRenderDistanceBlocks == renderDistanceBlocks + && caches.infoHasSafeZone == hasSafeZone && caches.infoPackSetter == packSetter) { + return false; + } + + caches.infoBranch = branch; + caches.infoDistance = distance; + caches.infoRenderDistanceBlocks = renderDistanceBlocks; + caches.infoHasSafeZone = hasSafeZone; + caches.infoPackSetter = packSetter; + return true; + } + + private static FrustumHolder applyInfo(FrustumHolder holder, Frustrum frustum, FrustumCaches caches) { + final boolean changed = !caches.cullingInfo.equals(holder.getCullingInfo()) + || !caches.distanceInfo.equals(holder.getDistanceInfo()); + holder.setInfo(frustum, caches.distanceInfo, caches.cullingInfo); + + if (changed) { + Iris.logger.info("Shadow culling ({}): {}, {}", holder.getRole(), caches.cullingInfo, caches.distanceInfo); + } return holder; } private static BoxCullingFrustum getOrCreateBoxCullingFrustum(double distance, FrustumCaches caches) { - if (caches.boxCuller == null) { - caches.boxCuller = new BoxCuller(distance); - caches.boxCullingFrustum = new BoxCullingFrustum(caches.boxCuller); - caches.lastBoxCullerDistance = distance; - } else if (caches.lastBoxCullerDistance != distance) { - caches.boxCuller.setMaxDistance(distance); - caches.lastBoxCullerDistance = distance; + final BoxCuller culler = caches.boxCuller.get(distance); + + if (caches.boxCullingFrustum == null) { + caches.boxCullingFrustum = new BoxCullingFrustum(culler); } return caches.boxCullingFrustum; } - private static BoxCuller getOrCreateAdvancedBoxCuller(double distance, FrustumCaches caches) { - if (caches.advancedBoxCuller == null) { - caches.advancedBoxCuller = new BoxCuller(distance); - caches.lastAdvancedBoxCullerDistance = distance; - } else if (caches.lastAdvancedBoxCullerDistance != distance) { - caches.advancedBoxCuller.setMaxDistance(distance); - caches.lastAdvancedBoxCullerDistance = distance; - } - return caches.advancedBoxCuller; - } - - private BoxCuller getOrCreateTileEntityCuller(double distance) { - if (cachedTileEntityCuller == null) { - cachedTileEntityCuller = new BoxCuller(distance); - lastTileEntityCullerDistance = distance; - } else if (lastTileEntityCullerDistance != distance) { - cachedTileEntityCuller.setMaxDistance(distance); - lastTileEntityCullerDistance = distance; - } - return cachedTileEntityCuller; - } - private void setupGlState(Matrix4f projMatrix) { GLStateManager.glViewport(0, 0, resolution, resolution); @@ -780,7 +810,7 @@ public class ShadowRenderer { BoxCuller culler = null; if (hasEntityFrustum) { double distance = halfPlaneLength * (renderDistanceMultiplier * entityShadowDistanceMultiplier); - culler = getOrCreateTileEntityCuller(distance); + culler = tileEntityCuller.get(distance); culler.setPosition(cameraX, cameraY, cameraZ); } diff --git a/src/main/java/net/coderbot/iris/pipeline/WorldRenderingPipeline.java b/src/main/java/net/coderbot/iris/pipeline/WorldRenderingPipeline.java index 19c55945f..fa7647781 100644 --- a/src/main/java/net/coderbot/iris/pipeline/WorldRenderingPipeline.java +++ b/src/main/java/net/coderbot/iris/pipeline/WorldRenderingPipeline.java @@ -22,7 +22,7 @@ import java.util.OptionalInt; public interface WorldRenderingPipeline { void beginLevelRendering(); void renderShadows(EntityRenderer levelRenderer, Camera camera); - default void preSubmitShadowGraph(int frame, boolean spectator) {} + default void preSubmitShadowGraph(int frame) {} void addDebugText(List messages); OptionalInt getForcedShadowRenderDistanceChunksForDisplay(); diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/BoxCuller.java b/src/main/java/net/coderbot/iris/shadows/frustum/BoxCuller.java index 3fcc7764f..61d669769 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/BoxCuller.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/BoxCuller.java @@ -96,4 +96,10 @@ public class BoxCuller { return maxZ < this.minRelZ || minZ > this.maxRelZ; } + + public boolean isFullyInsideViewRelative(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + return minX >= this.minRelX && maxX <= this.maxRelX + && minY >= this.minRelY && maxY <= this.maxRelY + && minZ >= this.minRelZ && maxZ <= this.maxRelZ; + } } diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/CullEverythingFrustum.java b/src/main/java/net/coderbot/iris/shadows/frustum/CullEverythingFrustum.java index 1daa3232c..ccd585a77 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/CullEverythingFrustum.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/CullEverythingFrustum.java @@ -24,6 +24,11 @@ public class CullEverythingFrustum extends Frustrum implements ViewportProvider, return false; } + @Override + public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + return OUTSIDE; + } + @Override public Viewport sodium$createViewport() { return new Viewport(this, position.set(xPosition, yPosition, zPosition)); diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/FrustumHolder.java b/src/main/java/net/coderbot/iris/shadows/frustum/FrustumHolder.java index 701bb70c1..a9847e2de 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/FrustumHolder.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/FrustumHolder.java @@ -3,10 +3,15 @@ package net.coderbot.iris.shadows.frustum; import net.minecraft.client.renderer.culling.Frustrum; public class FrustumHolder { + private final String role; private Frustrum frustum; private String distanceInfo = "(unavailable)"; private String cullingInfo = "(unavailable)"; + public FrustumHolder(String role) { + this.role = role; + } + public FrustumHolder setInfo(Frustrum frustum, String distanceInfo, String cullingInfo) { this.frustum = frustum; this.distanceInfo = distanceInfo; @@ -14,6 +19,10 @@ public class FrustumHolder { return this; } + public String getRole() { + return role; + } + public Frustrum getFrustum() { return frustum; } diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/advanced/AdvancedShadowCullingFrustum.java b/src/main/java/net/coderbot/iris/shadows/frustum/advanced/AdvancedShadowCullingFrustum.java index 97a8da77e..d10c113f1 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/advanced/AdvancedShadowCullingFrustum.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/advanced/AdvancedShadowCullingFrustum.java @@ -9,6 +9,7 @@ import net.minecraft.util.AxisAlignedBB; import org.embeddedt.embeddium.impl.render.viewport.Viewport; import org.embeddedt.embeddium.impl.render.viewport.ViewportProvider; import org.embeddedt.embeddium.impl.render.viewport.frustum.Frustum; +import org.embeddedt.embeddium.impl.render.viewport.frustum.ShadowSearchFrustum; import org.joml.Math; import org.joml.Matrix4f; import org.joml.Matrix4fc; @@ -37,7 +38,7 @@ import org.joml.Vector4f; * cost of slightly more computations.

*/ @Optional.Interface(modid = "distanthorizons", iface = "com.seibel.distanthorizons.api.interfaces.override.rendering.IDhApiShadowCullingFrustum") -public class AdvancedShadowCullingFrustum extends Frustrum implements ViewportProvider, Frustum, IDhApiShadowCullingFrustum { +public class AdvancedShadowCullingFrustum extends Frustrum implements ViewportProvider, Frustum, ShadowSearchFrustum, IDhApiShadowCullingFrustum { private static final int MAX_CLIPPING_PLANES = 13; /** @@ -279,11 +280,47 @@ public class AdvancedShadowCullingFrustum extends Frustrum implements ViewportPr return checkCornerVisibility(minX, minY, minZ, maxX, maxY, maxZ); } + @Override + public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + if (boxCuller != null && boxCuller.isCulledViewRelative(minX, minY, minZ, maxX, maxY, maxZ)) { + return OUTSIDE; + } + + final int result = intersectCorners(minX, minY, minZ, maxX, maxY, maxZ); + + if (result == FULLY_INSIDE && boxCuller != null + && !boxCuller.isFullyInsideViewRelative(minX, minY, minZ, maxX, maxY, maxZ)) { + return PARTIALLY_INSIDE; + } + + return result; + } + @Override public Viewport sodium$createViewport() { return new Viewport(this, position.set(xPosition, yPosition, zPosition)); } + @Override + public boolean supportsOcclusionSearch() { + return true; + } + + @Override + public float shadowLightX() { + return this.shadowLightVectorFromOrigin.x(); + } + + @Override + public float shadowLightY() { + return this.shadowLightVectorFromOrigin.y(); + } + + @Override + public float shadowLightZ() { + return this.shadowLightVectorFromOrigin.z(); + } + protected boolean isVisible(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) { return this.checkCornerVisibility((float)(minX - xPosition), (float)(minY - yPosition), (float)(minZ - zPosition), (float)(maxX - xPosition), (float)(maxY - yPosition), (float)(maxZ - zPosition)); @@ -306,6 +343,29 @@ public class AdvancedShadowCullingFrustum extends Frustrum implements ViewportPr return true; } + protected int intersectCorners(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + boolean inside = true; + + for (int i = 0; i < planeCount; ++i) { + final Vector4f plane = this.planes[i]; + + final float px = plane.x(); + final float py = plane.y(); + final float pz = plane.z(); + final float pw = plane.w(); + + if (Math.fma(px, px < 0 ? minX : maxX, Math.fma(py, py < 0 ? minY : maxY, pz * (pz < 0 ? minZ : maxZ))) < -pw) { + return OUTSIDE; + } + + if (Math.fma(px, px < 0 ? maxX : minX, Math.fma(py, py < 0 ? maxY : minY, pz * (pz < 0 ? maxZ : minZ))) < -pw) { + inside = false; + } + } + + return inside ? FULLY_INSIDE : PARTIALLY_INSIDE; + } + @Optional.Method(modid = "distanthorizons") @Override public void update(int worldMinBlockY, int worldMaxBlockY, DhApiMat4f worldViewProjection) { diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/advanced/SafeZoneCullingFrustum.java b/src/main/java/net/coderbot/iris/shadows/frustum/advanced/SafeZoneCullingFrustum.java index 3a11b38ec..725ccfad5 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/advanced/SafeZoneCullingFrustum.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/advanced/SafeZoneCullingFrustum.java @@ -7,14 +7,18 @@ import org.joml.Matrix4fc; import org.joml.Vector3f; public class SafeZoneCullingFrustum extends AdvancedShadowCullingFrustum { - private final BoxCuller distanceCuller; + private BoxCuller distanceCuller; - public SafeZoneCullingFrustum(Matrix4fc playerView, Matrix4fc playerProjection, Vector3f shadowLightVector, BoxCuller voxelCuller, BoxCuller distanceCuller) { - super(); - init(playerView, playerProjection, shadowLightVector, voxelCuller); + public void init(Matrix4fc playerView, Matrix4fc playerProjection, Vector3f shadowLightVector, BoxCuller voxelCuller, BoxCuller distanceCuller) { + super.init(playerView, playerProjection, shadowLightVector, voxelCuller); this.distanceCuller = distanceCuller; } + @Override + public boolean supportsOcclusionSearch() { + return false; + } + @Override public void setPosition(double cameraX, double cameraY, double cameraZ) { if (this.distanceCuller != null) { @@ -53,4 +57,51 @@ public class SafeZoneCullingFrustum extends AdvancedShadowCullingFrustum { return checkCornerVisibility(minX, minY, minZ, maxX, maxY, maxZ); } + + @Override + public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + int distanceResult = FULLY_INSIDE; + + if (distanceCuller != null) { + distanceResult = intersectCuller(distanceCuller, minX, minY, minZ, maxX, maxY, maxZ); + + if (distanceResult == OUTSIDE) { + return OUTSIDE; + } + } + + int safeZoneResult = OUTSIDE; + + if (boxCuller != null) { + safeZoneResult = intersectCuller(boxCuller, minX, minY, minZ, maxX, maxY, maxZ); + + if (safeZoneResult == FULLY_INSIDE && distanceResult == FULLY_INSIDE) { + return FULLY_INSIDE; + } + } + + if (distanceResult == PARTIALLY_INSIDE && safeZoneResult == PARTIALLY_INSIDE) { + return PARTIALLY_INSIDE; + } + + final int frustumResult = intersectCorners(minX, minY, minZ, maxX, maxY, maxZ); + + if (safeZoneResult == OUTSIDE && frustumResult == OUTSIDE) { + return OUTSIDE; + } + + if (frustumResult == FULLY_INSIDE && distanceResult == FULLY_INSIDE) { + return FULLY_INSIDE; + } + + return PARTIALLY_INSIDE; + } + + private static int intersectCuller(BoxCuller culler, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + if (culler.isCulledViewRelative(minX, minY, minZ, maxX, maxY, maxZ)) { + return OUTSIDE; + } + + return culler.isFullyInsideViewRelative(minX, minY, minZ, maxX, maxY, maxZ) ? FULLY_INSIDE : PARTIALLY_INSIDE; + } } diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/fallback/BoxCullingFrustum.java b/src/main/java/net/coderbot/iris/shadows/frustum/fallback/BoxCullingFrustum.java index 0e34750ef..c348da12c 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/fallback/BoxCullingFrustum.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/fallback/BoxCullingFrustum.java @@ -38,6 +38,15 @@ public class BoxCullingFrustum extends Frustrum implements ViewportProvider, Fru return !boxCuller.isCulledViewRelative(minX, minY, minZ, maxX, maxY, maxZ); } + @Override + public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + if (boxCuller.isCulledViewRelative(minX, minY, minZ, maxX, maxY, maxZ)) { + return OUTSIDE; + } + + return boxCuller.isFullyInsideViewRelative(minX, minY, minZ, maxX, maxY, maxZ) ? FULLY_INSIDE : PARTIALLY_INSIDE; + } + @Override public Viewport sodium$createViewport() { return new Viewport(this, position.set(xPosition, yPosition, zPosition)); diff --git a/src/main/java/net/coderbot/iris/shadows/frustum/fallback/NonCullingFrustum.java b/src/main/java/net/coderbot/iris/shadows/frustum/fallback/NonCullingFrustum.java index 8244c9a43..e5410af38 100644 --- a/src/main/java/net/coderbot/iris/shadows/frustum/fallback/NonCullingFrustum.java +++ b/src/main/java/net/coderbot/iris/shadows/frustum/fallback/NonCullingFrustum.java @@ -24,6 +24,11 @@ public class NonCullingFrustum extends Frustrum implements ViewportProvider, Fru return true; } + @Override + public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { + return FULLY_INSIDE; + } + @Override public Viewport sodium$createViewport() { return new Viewport(this, position.set(xPosition, yPosition, zPosition)); diff --git a/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService b/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService deleted file mode 100644 index 07b99f451..000000000 --- a/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService +++ /dev/null @@ -1 +0,0 @@ -com.gtnewhorizons.angelica.client.rendering.AngelicaGLStateManagerService diff --git a/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService b/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService deleted file mode 100644 index 2b1c94854..000000000 --- a/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService +++ /dev/null @@ -1 +0,0 @@ -com.gtnewhorizons.angelica.client.rendering.AngelicaRenderSystemService diff --git a/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService b/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService deleted file mode 100644 index 33a5970b6..000000000 --- a/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService +++ /dev/null @@ -1,3 +0,0 @@ -com.mitchej123.lwjgl.lwjgl3.LWJGL3Service -com.mitchej123.lwjgl.lwjgl2.LWJGL2Service - diff --git a/src/main/resources/assets/angelica/shaders/blocks/block_layer_opaque.fsh b/src/main/resources/assets/angelica/shaders/blocks/block_layer_opaque.fsh index c658cd7ff..6482e1f86 100644 --- a/src/main/resources/assets/angelica/shaders/blocks/block_layer_opaque.fsh +++ b/src/main/resources/assets/angelica/shaders/blocks/block_layer_opaque.fsh @@ -35,7 +35,7 @@ uniform float u_EnvFogStart; uniform float u_EnvFogEnd; #endif -#ifdef USE_FOG_EXP2 +#if defined(USE_FOG_EXP) || defined(USE_FOG_EXP2) uniform float u_FogDensity; #endif @@ -130,6 +130,8 @@ void main() { fragColor = vec4(mix(diffuseColor.rgb, u_FogColor.rgb, fogValue * u_FogColor.a), diffuseColor.a); #elif defined(USE_FOG_EXP2) fragColor = _exp2Fog(diffuseColor, v_FragDistance, u_FogColor, u_FogDensity); +#elif defined(USE_FOG_EXP) + fragColor = _expFog(diffuseColor, v_FragDistance, u_FogColor, u_FogDensity); #elif defined(USE_FOG_SMOOTH) fragColor = _linearFog(diffuseColor, v_FragDistance, u_FogColor, u_FogStart, u_FogEnd); #endif diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java index 843dcafb9..97a36e378 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft.java @@ -18,7 +18,6 @@ import org.lwjgl.input.Keyboard; import com.gtnewhorizons.angelica.glsm.GLStateManager; import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -37,28 +36,12 @@ public abstract class MixinMinecraft { @Shadow public GuiScreen currentScreen; - @Shadow(remap = false) - private static int max_texture_size; - @Unique private final RenderAheadManager celeritas$renderAheadManager = new RenderAheadManager(); @Unique private static boolean angelica$hadWorld; - /** - * @author mitchej123 - * @reason Avoid GL_PROXY_TEXTURE_2D which doesn't work with GLSM's texture binding. - * Uses the standard GL_MAX_TEXTURE_SIZE query instead. - */ - @Overwrite - public static int getGLMaximumTextureSize() { - if (max_texture_size == -1) { - max_texture_size = GLStateManager.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE); - } - return max_texture_size; - } - @Inject( method = "runGameLoop", at = @At(value = "INVOKE", target = "Lcpw/mods/fml/common/FMLCommonHandler;onRenderTickEnd(F)V", shift = At.Shift.AFTER, remap = false) diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft_FrameHook.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft_FrameHook.java index 538366238..d01e5b7ec 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft_FrameHook.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/MixinMinecraft_FrameHook.java @@ -1,14 +1,9 @@ package com.gtnewhorizons.angelica.mixins.early.angelica; -import com.gtnewhorizons.angelica.glsm.CaptureGate; -import com.gtnewhorizons.angelica.glsm.DisplayListManager; -import com.gtnewhorizons.angelica.glsm.GLDebug; -import com.gtnewhorizons.angelica.glsm.backend.BackendManager; +import com.gtnewhorizons.angelica.glsm.hooks.FrameHooks; import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; -import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND; import net.minecraft.client.Minecraft; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -25,53 +20,24 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(Minecraft.class) public class MixinMinecraft_FrameHook { - @Unique private static boolean angelica$gameLoopStarted; - @Unique private static long angelica$frameCounter; - @Unique private static boolean angelica$frameGroupActive; - @Inject(method = "runGameLoop", at = @At("HEAD")) private void angelica$bootstrapFirstFrame(CallbackInfo ci) { - DisplayListManager.abortIfLeaked(); - if (!angelica$gameLoopStarted) { - angelica$gameLoopStarted = true; - RENDER_BACKEND.onFrameBegin(); - angelica$pushFrameGroup(); - } + FrameHooks.bootstrapFirstFrame(); } @Inject(method = "func_147120_f"/*resetSize*/, at = @At("HEAD")) private void angelica$onFrameEnd(CallbackInfo ci) { - angelica$popFrameGroup(); - RENDER_BACKEND.onFrameEnd(); + FrameHooks.frameEnd(); } @Inject(method = "func_147120_f"/*resetSize*/, at = @At("RETURN")) private void angelica$onFrameBegin(CallbackInfo ci) { - if (angelica$gameLoopStarted) { - RENDER_BACKEND.onFrameBegin(); - angelica$pushFrameGroup(); - } - } - - @Unique - private static void angelica$pushFrameGroup() { - CaptureGate.refresh(); - angelica$frameGroupActive = GLDebug.isActive(); - if (angelica$frameGroupActive) GLDebug.pushGroup("frame:", (int) angelica$frameCounter); - } - - @Unique - private static void angelica$popFrameGroup() { - if (angelica$frameGroupActive) { - GLDebug.popGroup(); - angelica$frameGroupActive = false; - } - angelica$frameCounter++; + FrameHooks.frameBegin(); } @Inject(method = "shutdownMinecraftApplet", at = @At("HEAD")) private void angelica$onShutdown(CallbackInfo ci) { GpuCulling.shutdown(); - BackendManager.shutdown(); + FrameHooks.shutdown(); } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java index 67716d5eb..bf591e119 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/glsm/MixinSplashProgressCaching.java @@ -1,6 +1,6 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.glsm; -import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.hooks.FrameHooks; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -10,10 +10,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(value = cpw.mods.fml.client.SplashProgress.class, remap = false) public class MixinSplashProgressCaching { - // VAOs aren't shared across GL contexts; the SharedDrawable the client thread just swapped - // onto has none. Core profile (macOS) rejects glValidateProgram without one. - @Inject(method = "start", at = @At("RETURN")) + @Inject(method = "start", at = @At("TAIL")) private static void angelica$bindSharedDrawableVAO(CallbackInfo ci) { - GLStateManager.glBindVertexArray(GLStateManager.glGenVertexArrays()); + FrameHooks.bindSplashVao(); + } + + @Inject(method = "finish", at = @At("RETURN")) + private static void angelica$enableCachingOnFinish(CallbackInfo ci) { + FrameHooks.splashFinished(); } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java index 730b2226f..368c403e6 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java @@ -5,7 +5,6 @@ import com.gtnewhorizons.angelica.AngelicaMod; import com.gtnewhorizons.angelica.client.rendering.TextureTracker; import com.gtnewhorizons.angelica.config.AngelicaConfig; import com.gtnewhorizons.angelica.config.SystemProperties; -import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import com.gtnewhorizons.angelica.compat.DriverCompatabilityCheck; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.backend.BackendManager; @@ -42,13 +41,9 @@ public class MixinInitGLStateManager { GLDebug.reloadDebugState(); } GLStateManager.initialize(GLSMInitConfig.builder() - .displaySize(mc.displayWidth, mc.displayHeight) - .lwjglDebug(AngelicaMod.lwjglDebug) .streamingUploadStrategy(ClientProxy.options().advanced.streamingUploadStrategy) .noErrorChecks(AngelicaConfig.disableErrorChecks && GTNHLibCore.isObf() && !AngelicaMod.lwjglDebug) .enableDSA(AngelicaConfig.enableDSA) - .directDrawer(TessellatorStreamingDrawer::drawDirect) - .streamingDrawerDestroy(TessellatorStreamingDrawer::destroy) .postInitCallback(SelectionBoxRenderer::init) .build()); diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/textures/MixinTextureUtil.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/textures/MixinTextureUtil.java index c8a57f65a..97f4c3836 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/textures/MixinTextureUtil.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/angelica/textures/MixinTextureUtil.java @@ -1,14 +1,8 @@ package com.gtnewhorizons.angelica.mixins.early.angelica.textures; -import com.gtnewhorizons.angelica.glsm.DisplayListManager; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.glsm.RenderSystem; +import com.gtnewhorizons.angelica.glsm.hooks.TextureUtilHooks; import net.minecraft.client.renderer.texture.TextureUtil; -import org.lwjgl.opengl.EXTTextureFilterAnisotropic; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL12; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -19,54 +13,30 @@ public class MixinTextureUtil { @Redirect(method = "allocateTextureImpl", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;deleteTexture(I)V")) private static void angelica$dontDeleteTexture(int textureId) { - // NO-OP - Not sure why it's deleting a texture that was just generated and subsequently being bound... } @Inject(method = "allocateTextureImpl", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;bindTexture(I)V", shift = At.Shift.AFTER)) private static void angelica$setMaxLevel(int textureId, int mipmapLevels, int width, int height, float anisotropicFiltering, CallbackInfo ci) { - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, mipmapLevels); + TextureUtilHooks.setMaxLevel(mipmapLevels); } - @Unique private static int angelica$savedFilterTextureId; - @Inject(method = "func_152777_a(ZZF)V", at = @At("HEAD")) private static void angelica$captureSaveBoundId(boolean blurred, boolean mipmap, float aniso, CallbackInfo ci) { - angelica$savedFilterTextureId = GLStateManager.getBoundTextureForServerState(); + TextureUtilHooks.captureBoundFilterTexture(); } @Redirect(method = "func_147945_b()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;func_147952_b(II)V")) private static void angelica$restoreFilterOnSavedTexture(int min, int mag) { - final int id = angelica$savedFilterTextureId; - if (id != 0) { - if (DisplayListManager.getRecordMode() == DisplayListManager.RecordMode.NONE) { - RenderSystem.texParameteri(id, GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min); - RenderSystem.texParameteri(id, GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag); - } else { - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min); - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag); - } - } else { - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, min); - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, mag); - } + TextureUtilHooks.restoreFilter(min, mag); } @Redirect(method = "func_147945_b()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;func_152778_a(F)V")) private static void angelica$restoreAnisoOnSavedTexture(float aniso) { - final int id = angelica$savedFilterTextureId; - if (id != 0) { - if (DisplayListManager.getRecordMode() == DisplayListManager.RecordMode.NONE) { - RenderSystem.texParameterf(id, GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); - } else { - GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); - } - } else { - GLStateManager.glTexParameterf(GL11.GL_TEXTURE_2D, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); - } + TextureUtilHooks.restoreAniso(aniso); } @Inject(method = "func_147945_b()V", at = @At("RETURN")) private static void angelica$clearSavedAfterRestore(CallbackInfo ci) { - angelica$savedFilterTextureId = 0; + TextureUtilHooks.clearSavedFilterTexture(); } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinDefaultChunkRenderer.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinDefaultChunkRenderer.java deleted file mode 100644 index c32ce4fcc..000000000 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinDefaultChunkRenderer.java +++ /dev/null @@ -1,202 +0,0 @@ -package com.gtnewhorizons.angelica.mixins.early.celeritas.terrain; - -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.rendering.culling.FrustumExtractor; -import com.gtnewhorizons.angelica.rendering.culling.GpuIndirectMultiDrawEmitter; -import com.gtnewhorizons.angelica.mixins.interfaces.SectionRenderDataStorageRegionAccessor; -import com.gtnewhorizons.angelica.rendering.celeritas.AngelicaRenderPassConfiguration; -import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; -import net.coderbot.iris.pipeline.ShadowRenderer; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL30; -import org.embeddedt.embeddium.impl.gl.device.CommandList; -import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; -import org.embeddedt.embeddium.impl.render.chunk.ChunkRenderMatrices; -import org.embeddedt.embeddium.impl.render.chunk.DefaultChunkRenderer; -import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex; -import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType; -import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler; -import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe; -import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderList; -import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderListIterable; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.MultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; -import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; -import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; -import org.joml.Matrix4f; -import org.joml.Matrix4fc; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import java.nio.ByteBuffer; -import java.util.Iterator; - -@Mixin(value = DefaultChunkRenderer.class, remap = false) -public abstract class MixinDefaultChunkRenderer { - - @Shadow @Final private MultiDrawEmitter emitter; - - @Shadow protected abstract boolean useBlockFaceCulling(); - - @Unique private ByteBuffer angelica$frustumUboBytes; - @Unique private Matrix4f angelica$mvp; - @Unique private Matrix4f angelica$mvNoTranslation; - @Unique private int angelica$passOutputBase; - @Unique private ChunkPrimitiveType angelica$primitiveType; - - @Inject(method = "render", at = @At("HEAD")) - private void angelica$gpuPreWalkPass(ChunkRenderMatrices matrices, CommandList commandList, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera, CallbackInfo ci) { - if (!(emitter instanceof GpuIndirectMultiDrawEmitter gpu)) return; - - if (renderPass == AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS && renderLists.hasPass(renderPass) && gpu.selectPreparedSecondPass()) { - return; - } - if (!renderLists.hasPass(renderPass)) return; - - final boolean combined = renderPass == AngelicaRenderPassConfiguration.SOLID_PASS && AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS != null && renderLists.hasPass(AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS); - - final int indexPointerMask = renderPass.isSorted() ? 0xFFFFFFFF : 0; - if (combined) { - gpu.beginCombinedPasses(0, 0); - } else { - gpu.beginCullPass(indexPointerMask); - } - angelica$passOutputBase = 0; - angelica$primitiveType = null; - - if (!gpu.isComputeActiveThisPass()) { - return; - } - - if (angelica$frustumUboBytes == null) { - angelica$frustumUboBytes = FrustumExtractor.allocateUboByteBuffer(); - } - final boolean shadow = ShadowRenderer.ACTIVE; - final Matrix4fc proj = matrices.projection(); - final Matrix4fc mv = matrices.modelView(); - if (angelica$mvp == null) angelica$mvp = new Matrix4f(); - if (angelica$mvNoTranslation == null) angelica$mvNoTranslation = new Matrix4f(); - angelica$mvNoTranslation.set(mv).setTranslation(0f, 0f, 0f); - proj.mul(angelica$mvNoTranslation, angelica$mvp); - final float camX = (float) camera.intX + camera.fracX; - final float camY = (float) camera.intY + camera.fracY; - final float camZ = (float) camera.intZ + camera.fracZ; - FrustumExtractor.writeStd140(angelica$mvp, 0, 0, angelica$frustumUboBytes); - FrustumExtractor.patchCameraWorld(camX, camY, camZ, angelica$frustumUboBytes); - FrustumExtractor.patchBypassFrustum(shadow, angelica$frustumUboBytes); - - gpu.setFrustumUboBytes(angelica$frustumUboBytes); - - gpu.syncSectionMetaIfDirty(); - - angelica$walkPass(gpu, renderLists, renderPass, occlusionCamera); - if (combined) { - gpu.startSecondPass(); - angelica$walkPass(gpu, renderLists, AngelicaRenderPassConfiguration.CUTOUT_MIPPED_PASS, occlusionCamera); - gpu.finishCombinedBuild(); - } - - if (angelica$primitiveType != null) { - FrustumExtractor.patchPrimitiveRatio(angelica$primitiveType.getVerticesPerPrimitive(), - angelica$primitiveType.getIndexBufferElementsPerPrimitive(), angelica$frustumUboBytes); - } - } - - @Unique - private void angelica$walkPass(GpuIndirectMultiDrawEmitter gpu, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera) { - final boolean useFaceCulling = useBlockFaceCulling() && !renderPass.isSorted(); - final boolean reverse = renderPass.isReverseOrder(); - final boolean mergeRuns = !renderPass.isSorted(); - final Iterator it = renderLists.iterator(reverse); - while (it.hasNext()) { - final ChunkRenderList list = it.next(); - final RenderRegion region = list.getRegion(); - final SectionRenderDataStorage storage = region.getStorage(renderPass); - if (storage == null) continue; - final byte[] sections = list.getSectionsWithGeometry(); - final int sectionCount = list.getSectionsWithGeometryCount(); - if (sectionCount == 0) continue; - final SectionRenderDataUnsafe.Strategy layout = storage.getStorageStrategy(); - final ChunkPrimitiveType primitiveType = storage.getPrimitiveType(); - if (angelica$primitiveType == null) { - angelica$primitiveType = primitiveType; - } else if (angelica$primitiveType.getVerticesPerPrimitive() != primitiveType.getVerticesPerPrimitive() || angelica$primitiveType.getIndexBufferElementsPerPrimitive() != primitiveType.getIndexBufferElementsPerPrimitive()) { - throw new IllegalStateException("Batched cull dispatch spans passes with differing primitive ratios: " + angelica$primitiveType + " vs " + primitiveType); - } - final SectionRenderDataStorageRegionAccessor accessor = (SectionRenderDataStorageRegionAccessor) storage; - final int[] slotCache = accessor.angelica$getSlotCache(); - if (slotCache == null) continue; - - gpu.reserveSections(list.getSectionsWithGeometryCount()); - - final int regionOriginX = region.getChunkX(); - final int regionOriginY = region.getChunkY(); - final int regionOriginZ = region.getChunkZ(); - - final int regionDrawStart = angelica$passOutputBase; - int regionDrawCount = 0; - int regionMaxElems = 0; - int regionEntryStart = -1; - int regionEntryCount = 0; - - int cursor = reverse ? sectionCount - 1 : 0; - final int step = reverse ? -1 : 1; - - for (int i = 0; i < sectionCount; i++, cursor += step) { - final int sectionIndex = sections[cursor] & 0xFF; - final int chunkX = regionOriginX + LocalSectionIndex.unpackX(sectionIndex); - final int chunkY = regionOriginY + LocalSectionIndex.unpackY(sectionIndex); - final int chunkZ = regionOriginZ + LocalSectionIndex.unpackZ(sectionIndex); - - final long pMeshData = storage.getDataPointer(sectionIndex); - int slices = useFaceCulling ? BatchAssembler.getVisibleFaces(occlusionCamera.intX, occlusionCamera.intY, occlusionCamera.intZ, chunkX, chunkY, chunkZ) : ModelQuadFacing.ALL; - slices &= SectionRenderDataUnsafe.getSliceMask(pMeshData); - if (slices == 0) continue; - - final int slot = slotCache[sectionIndex]; - if (slot < 0) continue; - - final long runs = mergeRuns ? BatchAssembler.packRuns(slices & 0x7F) : 0L; - final int sectionDraws = mergeRuns ? BatchAssembler.runCount(runs) : Integer.bitCount(slices & 0x7F); - final int entryIdx = gpu.appendSection(slot, slices, angelica$passOutputBase); - if (regionEntryStart < 0) regionEntryStart = entryIdx; - regionEntryCount++; - angelica$passOutputBase += sectionDraws; - regionDrawCount += sectionDraws; - - int maxElems = 0; - if (mergeRuns) { - for (int run = 0; run < sectionDraws; run++) { - final int startVertex = layout.getVertexOffset(pMeshData, BatchAssembler.runFirst(runs, run)); - final int endVertex = layout.getRunVertexEnd(pMeshData, BatchAssembler.runLast(runs, run), primitiveType); - final int ec = SectionRenderDataUnsafe.elementsForVertices(endVertex - startVertex, primitiveType); - if (ec > maxElems) maxElems = ec; - } - } else { - for (int f = 0; f < ModelQuadFacing.COUNT; f++) { - final int ec = layout.getElementCount(pMeshData, f, primitiveType); - if (ec > maxElems) maxElems = ec; - } - } - if (maxElems > regionMaxElems) regionMaxElems = maxElems; - } - - if (regionDrawCount > 0) { - gpu.recordRegion(region, regionDrawStart, regionDrawCount, regionEntryStart, regionEntryCount, regionMaxElems); - } - } - } - - @Inject(method = "render", at = @At("RETURN")) - private void angelica$gpuEndPass(ChunkRenderMatrices matrices, CommandList commandList, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera, CallbackInfo ci) { - if (emitter instanceof GpuIndirectMultiDrawEmitter gpu) { - gpu.endPass(); - } - } -} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderListManager.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderListManager.java deleted file mode 100644 index 263f6b72e..000000000 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderListManager.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.gtnewhorizons.angelica.mixins.early.celeritas.terrain; - -import com.gtnewhorizons.angelica.mixins.interfaces.RenderListManagerAccessor; -import org.embeddedt.embeddium.impl.render.chunk.lists.RenderListManager; -import org.embeddedt.embeddium.impl.render.chunk.lists.VisibleChunkCollector; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -import java.util.concurrent.CompletableFuture; - -@Mixin(value = RenderListManager.class, remap = false) -public class MixinRenderListManager implements RenderListManagerAccessor { - - @Shadow - private CompletableFuture currentOcclusionFuture; - - @Override - public boolean angelica$hasOcclusionFutureInFlight() { - return currentOcclusionFuture != null; - } -} diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderSectionManager.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderSectionManager.java index 5d943539e..5fa0e503d 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderSectionManager.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinRenderSectionManager.java @@ -4,7 +4,6 @@ import com.gtnewhorizons.angelica.mixins.interfaces.RenderSectionManagerAccessor import it.unimi.dsi.fastutil.longs.Long2ReferenceMap; import org.embeddedt.embeddium.impl.render.chunk.RenderSection; import org.embeddedt.embeddium.impl.render.chunk.RenderSectionManager; -import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegionManager; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -18,13 +17,6 @@ public class MixinRenderSectionManager implements RenderSectionManagerAccessor { @Shadow @Final private ConcurrentLinkedDeque asyncSubmittedTasks; - @Shadow @Final private RenderRegionManager regions; - - @Shadow - private float getSearchDistance() { - throw new AssertionError(); - } - @Override public Long2ReferenceMap angelica$getSectionByPosition() { return sectionByPosition; @@ -34,14 +26,4 @@ public class MixinRenderSectionManager implements RenderSectionManagerAccessor { public ConcurrentLinkedDeque angelica$getAsyncSubmittedTasks() { return asyncSubmittedTasks; } - - @Override - public RenderRegionManager angelica$getRegions() { - return regions; - } - - @Override - public float angelica$getSearchDistance() { - return getSearchDistance(); - } } diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinSectionRenderDataStorage.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinSectionRenderDataStorage.java index a27ca87a1..e5fa01264 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinSectionRenderDataStorage.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinSectionRenderDataStorage.java @@ -9,6 +9,7 @@ import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveT import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataStorage; import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -32,12 +33,14 @@ public abstract class MixinSectionRenderDataStorage implements SectionRenderData @Override public void angelica$setPassIndex(int passIndex) { this.angelica$passIndex = passIndex; } @Override public int[] angelica$getSlotCache() { return angelica$slotCache; } + @Shadow @Final private SectionRenderDataUnsafe.Strategy storageStrategy; + @Shadow public abstract long getDataPointer(int sectionIndex); - @Shadow public abstract SectionRenderDataUnsafe.Strategy getStorageStrategy(); - @Shadow public abstract ChunkPrimitiveType getPrimitiveType(); + @Shadow public abstract int getSliceMask(int sectionIndex); + @Inject(method = "setMeshes", at = @At("RETURN")) private void angelica$onSetMeshes(int localSectionIndex, GlBufferSegment allocation, GlBufferSegment indexAllocation, Map ranges, CallbackInfo ci) { angelica$pushUpdate(localSectionIndex); @@ -77,12 +80,12 @@ public abstract class MixinSectionRenderDataStorage implements SectionRenderData private void angelica$pushUpdate(int localSectionIndex) { final RenderRegion r = angelica$region; if (r == null) return; - final long dataPtr = getDataPointer(localSectionIndex); - if (SectionRenderDataUnsafe.getSliceMask(dataPtr) == 0) { + final int sliceMask = getSliceMask(localSectionIndex); + if (sliceMask == 0) { angelica$releaseSlot(r, localSectionIndex); return; } - final int slot = GpuCulling.sectionMeta().update(angelica$passIndex, r.getOriginX(), r.getOriginY(), r.getOriginZ(), localSectionIndex, dataPtr, getStorageStrategy(), getPrimitiveType()); + final int slot = GpuCulling.sectionMeta().update(angelica$passIndex, r.getOriginX(), r.getOriginY(), r.getOriginZ(), localSectionIndex, getDataPointer(localSectionIndex), sliceMask, storageStrategy, getPrimitiveType()); if (slot < 0) return; int[] cache = angelica$slotCache; if (cache == null) { diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sdlgpu/MixinDefaultChunkRenderer_ShadowVoxelization.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sdlgpu/MixinDefaultChunkRenderer_ShadowVoxelization.java deleted file mode 100644 index a0b4e1d59..000000000 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sdlgpu/MixinDefaultChunkRenderer_ShadowVoxelization.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.gtnewhorizons.angelica.mixins.early.sdlgpu; - -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.glsm.backend.BackendManager; -import com.gtnewhorizons.angelica.rendering.voxelization.SdlShadowVoxelizationSink; -import com.gtnewhorizons.angelica.rendering.voxelization.ShadowVoxelizer; -import net.coderbot.iris.Iris; -import net.coderbot.iris.gl.program.ComputeProgram; -import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline; -import net.coderbot.iris.pipeline.WorldRenderingPipeline; -import net.coderbot.iris.shadows.ShadowRenderingState; -import org.embeddedt.embeddium.impl.gl.device.CommandList; -import org.embeddedt.embeddium.impl.render.chunk.ChunkRenderMatrices; -import org.embeddedt.embeddium.impl.render.chunk.DefaultChunkRenderer; -import org.embeddedt.embeddium.impl.render.chunk.lists.ChunkRenderListIterable; -import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; -import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; -import org.apache.logging.log4j.LogManager; -import org.lwjgl.opengl.GL20; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import java.util.HashSet; -import java.util.Set; - -/** SDL_GPU allows read-write image access only in a compute pass, so the shadow VSH stores are replayed here. */ -@Mixin(value = DefaultChunkRenderer.class, remap = false) -public abstract class MixinDefaultChunkRenderer_ShadowVoxelization { - - @Shadow protected abstract boolean useBlockFaceCulling(); - - @Unique private static final ShadowVoxelizer angelica$voxelizer = new ShadowVoxelizer(); - @Unique private static final SdlShadowVoxelizationSink angelica$sink = new SdlShadowVoxelizationSink(); - - @Unique private static final Set angelica$loggedBails = new HashSet<>(); - - @Unique - private static void angelica$bail(String reason) { - if (angelica$loggedBails.add(reason)) { - LogManager.getLogger("Angelica").info("shadow voxelization skipped: {}", reason); - } - } - - @Inject(method = "render", at = @At("HEAD")) - private void angelica$voxelizeShadowTerrain(ChunkRenderMatrices matrices, CommandList commandList, ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform occlusionCamera, CameraTransform camera, CallbackInfo ci) { - if (!BackendManager.RENDER_BACKEND.isSDLGPU()) return; - if (!ShadowRenderingState.areShadowsCurrentlyBeingRendered()) return; - if (!renderLists.hasPass(renderPass)) { angelica$bail("pass has no render lists"); return; } - - final WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable(); - if (!(pipeline instanceof DeferredWorldRenderingPipeline deferred)) { angelica$bail("no deferred pipeline"); return; } - final ComputeProgram voxelCompute = deferred.getShadowVoxelizationCompute(); - if (voxelCompute == null) { angelica$bail("pack declares no shadow voxelization"); return; } - - final int prevProgram = GLStateManager.glGetInteger(GL20.GL_CURRENT_PROGRAM); - voxelCompute.use(); - deferred.prepareShadowVoxelizationCompute(matrices.modelView()); - try { - angelica$voxelizer.walkPass(renderLists, renderPass, renderPass.vertexType().getVertexFormat(), camera, occlusionCamera, useBlockFaceCulling() && !renderPass.isSorted(), angelica$sink); - } finally { - if (prevProgram != 0) GLStateManager.glUseProgram(prevProgram); - } - } -} diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/RenderRegionKeys.java b/src/test/java/com/gtnewhorizons/angelica/rendering/RenderRegionKeys.java new file mode 100644 index 000000000..767d0ddcc --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/RenderRegionKeys.java @@ -0,0 +1,28 @@ +package com.gtnewhorizons.angelica.rendering; + +import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; +import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkMeshFormats; + +import java.lang.reflect.Constructor; + +public final class RenderRegionKeys { + + private static final int COMMON_VERTEX_STRIDE = ChunkMeshFormats.VANILLA_LIKE.getVertexFormat().getStride(); + + private RenderRegionKeys() { + } + + public static RenderRegion create(int x, int y, int z, int id) { + try { + for (Constructor ctor : RenderRegion.class.getDeclaredConstructors()) { + if (ctor.getParameterCount() == 6) { + ctor.setAccessible(true); + return (RenderRegion) ctor.newInstance(x, y, z, id, null, COMMON_VERTEX_STRIDE); + } + } + } catch (ReflectiveOperationException e) { + throw new IllegalStateException(e); + } + throw new IllegalStateException("RenderRegion constructor shape changed"); + } +} diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/IndirectCullDrawParityTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/IndirectCullDrawParityTest.java index 326b2a969..ca54a888c 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/IndirectCullDrawParityTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/IndirectCullDrawParityTest.java @@ -1,13 +1,15 @@ package com.gtnewhorizons.angelica.rendering.culling; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.DrawCommandSink; import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe; +import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler; import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.QuadPrimitiveType; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; import com.gtnewhorizons.angelica.config.GpuCullingMode; import com.gtnewhorizons.angelica.glsm.GLCoreTest; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.RenderSystem; +import com.gtnewhorizons.angelica.rendering.RenderRegionKeys; +import com.gtnewhorizons.angelica.rendering.celeritas.IndividualDrawBatch; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttribute; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeBinding; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexAttributeFormat; @@ -20,10 +22,10 @@ import org.embeddedt.embeddium.impl.gl.device.RenderDevice; import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType; import org.embeddedt.embeddium.impl.gl.tessellation.GlVertexArrayTessellation; import org.embeddedt.embeddium.impl.gl.tessellation.TessellationBinding; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.BatchAssembler; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.DirectMultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.IndirectMultiDrawEmitter; -import org.embeddedt.embeddium.impl.render.chunk.multidraw.MultiDrawEmitter; +import org.embeddedt.embeddium.impl.gl.device.DirectMultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.device.IndirectMultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.device.MultiDrawBatch; +import org.embeddedt.embeddium.impl.gl.device.MultiDrawBatchFactory; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; import org.joml.Matrix4f; import org.junit.jupiter.api.AfterAll; @@ -36,7 +38,6 @@ import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL43; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -109,18 +110,86 @@ class IndirectCullDrawParityTest { } } + private static final class SceneRig implements AutoCloseable { + GlMutableBuffer vertexBuffer; + GlMutableBuffer sharedIndexBuffer; + GlMutableBuffer sortedIndexBuffer; + GlVertexArrayTessellation tessNonSorted; + GlVertexArrayTessellation tessSorted; + + SceneRig(Scene scene) { + try { + final ByteBuffer vertexBytes = BufferUtils.createByteBuffer(scene.vertices.capacity() * 4).order(ByteOrder.nativeOrder()); + vertexBytes.asFloatBuffer().put(scene.vertices.duplicate()); + vertexBuffer = upload(vertexBytes); + sharedIndexBuffer = upload(scene.sharedIndices.duplicate().order(ByteOrder.nativeOrder())); + sortedIndexBuffer = upload(scene.sortedIndices.duplicate().order(ByteOrder.nativeOrder())); + tessNonSorted = tessellation(vertexBuffer, sharedIndexBuffer); + tessSorted = tessellation(vertexBuffer, sortedIndexBuffer); + } catch (RuntimeException | Error e) { + close(); + throw e; + } + } + + @Override + public void close() { + if (tessNonSorted != null) tessNonSorted.delete(commandList); + if (tessSorted != null) tessSorted.delete(commandList); + if (vertexBuffer != null) commandList.deleteBuffer(vertexBuffer); + if (sharedIndexBuffer != null) commandList.deleteBuffer(sharedIndexBuffer); + if (sortedIndexBuffer != null) commandList.deleteBuffer(sortedIndexBuffer); + } + } + + private static final class Fbo implements AutoCloseable { + int id; + final int[] textures = new int[2]; + + Fbo() { + try { + textures[0] = GLStateManager.glGenTextures(); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, textures[0]); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL30.GL_DEPTH_COMPONENT32F, FBO_SIZE, FBO_SIZE, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); + GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + + textures[1] = GLStateManager.glGenTextures(); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, textures[1]); + GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, FBO_SIZE, FBO_SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); + + id = GLStateManager.glGenFramebuffers(); + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, id); + GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, textures[0], 0); + GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, textures[1], 0); + assertEquals(GL30.GL_FRAMEBUFFER_COMPLETE, GLStateManager.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER), "parity FBO incomplete"); + } catch (RuntimeException | Error e) { + close(); + throw e; + } + } + + @Override + public void close() { + GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); + if (id != 0) GLStateManager.glDeleteFramebuffers(id); + for (int tex : textures) { + if (tex != 0) GLStateManager.glDeleteTextures(tex); + } + } + } + @BeforeAll static void setUpDevice() { GLRenderDevice.VANILLA_STATE_RESETTER = () -> {}; RenderDevice.enterManagedCode(); commandList = RenderDevice.INSTANCE.createCommandList(); program = buildProgram(); - GpuCulling.setMode(GpuCullingMode.COMPUTE); } @AfterAll static void tearDownDevice() { - GpuCulling.setMode(GpuCullingMode.CPU_ONLY); if (program != 0) GLStateManager.glDeleteProgram(program); RenderDevice.exitManagedCode(); } @@ -275,27 +344,6 @@ class IndirectCullDrawParityTest { return tess; } - private static int newFbo(int[] texturesOut) { - final int depthTex = GLStateManager.glGenTextures(); - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, depthTex); - GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL30.GL_DEPTH_COMPONENT32F, FBO_SIZE, FBO_SIZE, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null); - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); - GLStateManager.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); - final int colorTex = GLStateManager.glGenTextures(); - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, colorTex); - GLStateManager.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, FBO_SIZE, FBO_SIZE, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, 0); - - final int fbo = GLStateManager.glGenFramebuffers(); - GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo); - GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTex, 0); - GLStateManager.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colorTex, 0); - assertEquals(GL30.GL_FRAMEBUFFER_COMPLETE, GLStateManager.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER), "parity FBO incomplete"); - texturesOut[0] = depthTex; - texturesOut[1] = colorTex; - return fbo; - } - private static void beginDrawing(int fbo) { GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo); GLStateManager.glViewport(0, 0, FBO_SIZE, FBO_SIZE); @@ -324,38 +372,41 @@ class IndirectCullDrawParityTest { return covered; } - private static void assertDepthParity(int directFbo, int gpuFbo, String label) { + private static void assertDepthParity(int directFbo, int otherFbo, String label) { final FloatBuffer direct = readDepth(directFbo); - final FloatBuffer gpu = readDepth(gpuFbo); + final FloatBuffer other = readDepth(otherFbo); final int covered = coveredPixels(direct); assertTrue(covered > 100, label + ": direct path drew almost nothing (" + covered + " covered pixels); scene setup is broken"); - assertEquals(covered, coveredPixels(gpu), label + ": covered pixel count differs between direct and GPU-culled indirect"); + assertEquals(covered, coveredPixels(other), label + ": covered pixel count differs between direct and the compared path"); for (int i = 0; i < direct.capacity(); i++) { - if (direct.get(i) != gpu.get(i)) { + if (direct.get(i) != other.get(i)) { final int x = i % FBO_SIZE; final int y = i / FBO_SIZE; - assertEquals(direct.get(i), gpu.get(i), label + ": depth mismatch at (" + x + "," + y + "); the GPU-culled indirect path rendered different geometry than the direct path"); + assertEquals(direct.get(i), other.get(i), label + ": depth mismatch at (" + x + "," + y + "); the compared path rendered different geometry than the direct path"); } } } - private static RenderRegion newRegionKey() { + private static void setFrustumUbo(GpuTerrainCuller culler, ByteBuffer ubo) { try { - for (Constructor ctor : RenderRegion.class.getDeclaredConstructors()) { - if (ctor.getParameterCount() == 5) { - ctor.setAccessible(true); - return (RenderRegion) ctor.newInstance(0, 0, 0, 0, null); - } - } + final Field field = GpuTerrainCuller.class.getDeclaredField("frustumUboBytes"); + field.setAccessible(true); + field.set(culler, ubo); } catch (ReflectiveOperationException e) { throw new IllegalStateException(e); } - throw new IllegalStateException("RenderRegion constructor shape changed"); } - private static int reflectInt(GpuIndirectMultiDrawEmitter emitter, String fieldName) { + private static void executeRegion(GpuTerrainCuller culler, RenderRegion region, GlVertexArrayTessellation tessellation) { + final MultiDrawBatch batch = culler.batchForRegion(region); + if (batch != null) { + batch.execute(commandList, tessellation, GlPrimitiveType.TRIANGLES); + } + } + + private static int reflectInt(GpuTerrainCuller emitter, String fieldName) { try { - final Field field = GpuIndirectMultiDrawEmitter.class.getDeclaredField(fieldName); + final Field field = GpuTerrainCuller.class.getDeclaredField(fieldName); field.setAccessible(true); return field.getInt(emitter); } catch (ReflectiveOperationException e) { @@ -374,72 +425,62 @@ class IndirectCullDrawParityTest { } private static void metaUpdate(SectionMetaBuffer meta, Section section, int localSectionIndex) { - section.slot = meta.update(section.passIndex, 0, 0, 0, localSectionIndex, section.srdAddress(), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + section.slot = meta.update(section.passIndex, 0, 0, 0, localSectionIndex, section.srdAddress(), section.sliceMask, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertTrue(section.slot >= 0, "meta rejected section for pass " + section.passIndex + " local index " + localSectionIndex); } - private static void pushSectionCommands(DrawCommandSink sink, long srdAddress, int sliceMask, int indexPointerMask) { + private static void pushSectionCommands(MultiDrawBatch batch, long srdAddress, int sliceMask, int indexPointerMask) { for (int facing = 0; facing < FACINGS; facing++) { if (((sliceMask >> facing) & 1) == 0) { continue; } - sink.push(SectionRenderDataUnsafe.Strategy.FULL.getVertexOffset(srdAddress, facing), + batch.appendDrawCommand(SectionRenderDataUnsafe.Strategy.FULL.getVertexOffset(srdAddress, facing), SectionRenderDataUnsafe.Strategy.FULL.getElementCount(srdAddress, facing, QuadPrimitiveType.TRIANGULATED), SectionRenderDataUnsafe.Strategy.FULL.getIndexOffset(srdAddress, facing) & indexPointerMask); } } - private static void renderCpuEmitter(Scene scene, int fbo, GlVertexArrayTessellation tessNonSorted, GlVertexArrayTessellation tessSorted, MultiDrawEmitter emitter) { + private static void renderCpuBatches(Scene scene, int fbo, GlVertexArrayTessellation tessNonSorted, GlVertexArrayTessellation tessSorted, MultiDrawBatchFactory factory) { beginDrawing(fbo); - renderCpuPass(emitter, tessNonSorted, 0, List.of( + renderCpuPass(factory, tessNonSorted, 0, List.of( scene.solid.subList(0, REGION1_SOLID_SECTIONS), scene.solid.subList(REGION1_SOLID_SECTIONS, scene.solid.size()))); - renderCpuPass(emitter, tessNonSorted, 0, List.of(scene.cutout)); - renderCpuPass(emitter, tessSorted, 0xFFFFFFFF, List.of(scene.sorted)); + renderCpuPass(factory, tessNonSorted, 0, List.of(scene.cutout)); + renderCpuPass(factory, tessSorted, 0xFFFFFFFF, List.of(scene.sorted)); } - private static void renderCpuPass(MultiDrawEmitter emitter, GlVertexArrayTessellation tessellation, int indexPointerMask, List> regions) { - final DrawCommandSink sink = emitter.getCommandSink(); - - if (!emitter.batchesWholePass()) { - for (List
region : regions) { - sink.clear(); - for (Section section : region) { - pushSectionCommands(sink, section.srdAddress(), section.sliceMask, indexPointerMask); - } - emitter.executeBatch(commandList, tessellation, GlPrimitiveType.TRIANGLES); - } - return; - } - - int sectionCount = 0; + private static void renderCpuPass(MultiDrawBatchFactory factory, GlVertexArrayTessellation tessellation, int indexPointerMask, List> regions) { for (List
region : regions) { - sectionCount += region.size(); - } - - emitter.beginPass(commandList, sectionCount); - - final int[] commandCounts = new int[regions.size()]; - for (int r = 0; r < regions.size(); r++) { - sink.clear(); - for (Section section : regions.get(r)) { - pushSectionCommands(sink, section.srdAddress(), section.sliceMask, indexPointerMask); + final MultiDrawBatch batch = factory.create(MultiDrawBatch.MAX_COMMAND_COUNT); + try { + for (Section section : region) { + pushSectionCommands(batch, section.srdAddress(), section.sliceMask, indexPointerMask); + } + batch.upload(commandList); + batch.execute(commandList, tessellation, GlPrimitiveType.TRIANGLES); + } finally { + batch.delete(); } - commandCounts[r] = sink.size(); } + } - emitter.finishAssembly(commandList); - - int firstCommand = 0; - for (int count : commandCounts) { - emitter.selectDrawRange(firstCommand, count); - firstCommand += count; - emitter.executeBatch(commandList, tessellation, GlPrimitiveType.TRIANGLES); + private static void mutateFirstSolidSection(Scene scene) { + final Section mutated = scene.solid.get(0); + int shrinkFacing = -1; + for (int f = 0; f < FACINGS; f++) { + if ((mutated.sliceMask & (1 << f)) != 0 && mutated.quadCount[f] > 1) { shrinkFacing = f; break; } } + assertTrue(shrinkFacing >= 0, "scene has no shrinkable facing"); + mutated.quadCount[shrinkFacing]--; + mutated.elementCount[shrinkFacing] -= 6; - emitter.onPassFinished(commandList); + for (int f = shrinkFacing + 1; f < FACINGS; f++) { + mutated.vertexOffset[f] -= 4; + if (mutated.passIndex == SORTED_PASS) mutated.indexOffset[f] -= 6 * 4; + } + mutated.writeSrd(); } private static final class GpuFramePasses { @@ -449,13 +490,13 @@ class IndirectCullDrawParityTest { int sortedIndirectBuffer; } - private static GpuFramePasses renderGpu(Scene scene, int fbo, GlVertexArrayTessellation tessNonSorted, GlVertexArrayTessellation tessSorted, GpuIndirectMultiDrawEmitter gpu, RenderRegion region1, RenderRegion region2, String frameLabel) { + private static GpuFramePasses renderGpu(Scene scene, int fbo, GlVertexArrayTessellation tessNonSorted, GlVertexArrayTessellation tessSorted, GpuTerrainCuller gpu, RenderRegion region1, RenderRegion region2, String frameLabel) { final GpuFramePasses result = new GpuFramePasses(); beginDrawing(fbo); gpu.beginCombinedPasses(0, 0); assertTrue(gpu.isComputeActiveThisPass(), "compute path unexpectedly inactive"); - gpu.setFrustumUboBytes(frustumUbo(0)); + setFrustumUbo(gpu, frustumUbo(0)); gpu.syncSectionMetaIfDirty(); int outputBase = 0; @@ -465,25 +506,21 @@ class IndirectCullDrawParityTest { walkSections(scene.cutout, gpu, region1, outputBase, result.expectedCombined, 0); gpu.finishCombinedBuild(); - gpu.prepareRegion(region1); - gpu.executeBatch(commandList, tessNonSorted, GlPrimitiveType.TRIANGLES); - gpu.prepareRegion(region2); - gpu.executeBatch(commandList, tessNonSorted, GlPrimitiveType.TRIANGLES); + executeRegion(gpu, region1, tessNonSorted); + executeRegion(gpu, region2, tessNonSorted); gpu.endPass(); assertTrue(gpu.selectPreparedSecondPass(), "prepared cutout pass was not selectable"); - gpu.prepareRegion(region1); - gpu.executeBatch(commandList, tessNonSorted, GlPrimitiveType.TRIANGLES); + executeRegion(gpu, region1, tessNonSorted); gpu.endPass(); result.combinedIndirectBuffer = reflectInt(gpu, "indirectSsboGlId"); assertIndirectBufferMatches(result.combinedIndirectBuffer, result.expectedCombined, frameLabel + " combined solid+cutout pass"); gpu.beginCullPass(0xFFFFFFFF); - gpu.setFrustumUboBytes(frustumUbo(0xFFFFFFFF)); + setFrustumUbo(gpu, frustumUbo(0xFFFFFFFF)); gpu.syncSectionMetaIfDirty(); walkSections(scene.sorted, gpu, region1, 0, result.expectedSorted, 0xFFFFFFFF); - gpu.prepareRegion(region1); - gpu.executeBatch(commandList, tessSorted, GlPrimitiveType.TRIANGLES); + executeRegion(gpu, region1, tessSorted); gpu.endPass(); result.sortedIndirectBuffer = reflectInt(gpu, "indirectSsboGlId"); assertIndirectBufferMatches(result.sortedIndirectBuffer, result.expectedSorted, frameLabel + " sorted pass"); @@ -491,18 +528,14 @@ class IndirectCullDrawParityTest { return result; } - private static int walkSections(List
sections, GpuIndirectMultiDrawEmitter gpu, RenderRegion region, int outputBase, List expectedCommands, int indexPointerMask) { + private static int walkSections(List
sections, GpuTerrainCuller gpu, RenderRegion region, int outputBase, List expectedCommands, int indexPointerMask) { gpu.reserveSections(sections.size()); final int drawStart = outputBase; int drawCount = 0; - int entryStart = -1; - int entryCount = 0; int maxElems = 0; for (int i = 0; i < sections.size(); i++) { final Section section = sections.get(i); - final int entryIdx = gpu.appendSection(section.slot, section.sliceMask, outputBase); - if (entryStart < 0) entryStart = entryIdx; - entryCount++; + gpu.appendSection(section.slot, section.sliceMask, outputBase); final boolean mergeRuns = indexPointerMask == 0; final long runs = mergeRuns ? BatchAssembler.packRuns(section.sliceMask & 0x7F) : 0L; @@ -530,7 +563,7 @@ class IndirectCullDrawParityTest { } } if (drawCount > 0) { - gpu.recordRegion(region, drawStart, drawCount, entryStart, entryCount, maxElems); + gpu.recordRegion(region, drawStart, drawCount, maxElems); } return outputBase; } @@ -549,97 +582,100 @@ class IndirectCullDrawParityTest { } } - @Test - void gpuCulledIndirectMatchesDirectAcrossFrames() { - assumeTrue(RenderSystem.supportsCompute(), "compute shaders unsupported"); + private static void runCpuFactoryParity(MultiDrawBatchFactory other, String label) { final Scene scene = buildScene(); - final GpuDrivenChunkCuller culler = new GpuDrivenChunkCuller(); - assertTrue(culler.ensureReady(), "chunk_cull.csh failed to load on a real driver"); - final SectionMetaBuffer meta = new SectionMetaBuffer(); - final GpuIndirectMultiDrawEmitter gpu = new GpuIndirectMultiDrawEmitter(culler, meta); - final RenderRegion region1 = newRegionKey(); - final RenderRegion region2 = newRegionKey(); + final MultiDrawBatchFactory direct = DirectMultiDrawBatch::new; - final DirectMultiDrawEmitter direct = new DirectMultiDrawEmitter(); - final IndirectMultiDrawEmitter cpuIndirect = new IndirectMultiDrawEmitter(); - GlMutableBuffer vertexBuffer = null; - GlMutableBuffer sharedIndexBuffer = null; - GlMutableBuffer sortedIndexBuffer = null; - GlVertexArrayTessellation tessNonSorted = null; - GlVertexArrayTessellation tessSorted = null; - final int[] directTex = new int[2]; - final int[] cpuTex = new int[2]; - final int[] gpuTex = new int[2]; - int directFbo = 0; - int cpuFbo = 0; - int gpuFbo = 0; try { - final ByteBuffer vertexBytes = BufferUtils.createByteBuffer(scene.vertices.capacity() * 4).order(ByteOrder.nativeOrder()); - vertexBytes.asFloatBuffer().put(scene.vertices.duplicate()); - vertexBuffer = upload(vertexBytes); - sharedIndexBuffer = upload(scene.sharedIndices.duplicate().order(ByteOrder.nativeOrder())); - sortedIndexBuffer = upload(scene.sortedIndices.duplicate().order(ByteOrder.nativeOrder())); - tessNonSorted = tessellation(vertexBuffer, sharedIndexBuffer); - tessSorted = tessellation(vertexBuffer, sortedIndexBuffer); - directFbo = newFbo(directTex); - cpuFbo = newFbo(cpuTex); - gpuFbo = newFbo(gpuTex); + try (SceneRig rig = new SceneRig(scene); + Fbo directFbo = new Fbo(); + Fbo otherFbo = new Fbo()) { - int local = 0; - for (Section section : scene.solid) metaUpdate(meta, section, local++); - local = 0; - for (Section section : scene.cutout) metaUpdate(meta, section, local++); - local = 0; - for (Section section : scene.sorted) metaUpdate(meta, section, local++); + for (int frame = 1; frame <= 2; frame++) { + renderCpuBatches(scene, directFbo.id, rig.tessNonSorted, rig.tessSorted, direct); + renderCpuBatches(scene, otherFbo.id, rig.tessNonSorted, rig.tessSorted, other); + assertDepthParity(directFbo.id, otherFbo.id, "frame " + frame + " (" + label + ")"); + } - for (int frame = 1; frame <= 2; frame++) { - renderCpuEmitter(scene, directFbo, tessNonSorted, tessSorted, direct); - renderCpuEmitter(scene, cpuFbo, tessNonSorted, tessSorted, cpuIndirect); - renderGpu(scene, gpuFbo, tessNonSorted, tessSorted, gpu, region1, region2, "frame " + frame); - assertDepthParity(directFbo, cpuFbo, "frame " + frame + " (celeritas CPU indirect emitter)"); - assertDepthParity(directFbo, gpuFbo, "frame " + frame + " (GPU-culled indirect emitter)"); + mutateFirstSolidSection(scene); + + renderCpuBatches(scene, directFbo.id, rig.tessNonSorted, rig.tessSorted, direct); + renderCpuBatches(scene, otherFbo.id, rig.tessNonSorted, rig.tessSorted, other); + assertDepthParity(directFbo.id, otherFbo.id, "frame 3 (" + label + ")"); } - - final Section mutated = scene.solid.get(0); - int shrinkFacing = -1; - for (int f = 0; f < FACINGS; f++) { - if ((mutated.sliceMask & (1 << f)) != 0 && mutated.quadCount[f] > 1) { shrinkFacing = f; break; } - } - assertTrue(shrinkFacing >= 0, "scene has no shrinkable facing"); - mutated.quadCount[shrinkFacing]--; - mutated.elementCount[shrinkFacing] -= 6; - - for (int f = shrinkFacing + 1; f < FACINGS; f++) { - mutated.vertexOffset[f] -= 4; - if (mutated.passIndex == SORTED_PASS) mutated.indexOffset[f] -= 6 * 4; - } - mutated.writeSrd(); - metaUpdate(meta, mutated, 0); - - renderCpuEmitter(scene, directFbo, tessNonSorted, tessSorted, direct); - renderCpuEmitter(scene, cpuFbo, tessNonSorted, tessSorted, cpuIndirect); - renderGpu(scene, gpuFbo, tessNonSorted, tessSorted, gpu, region1, region2, "frame 3 (after section mesh update)"); - assertDepthParity(directFbo, cpuFbo, "frame 3 (celeritas CPU indirect emitter)"); - assertDepthParity(directFbo, gpuFbo, "frame 3 (GPU-culled indirect emitter)"); } finally { - direct.delete(); - cpuIndirect.delete(); - gpu.delete(); - meta.shutdown(); - culler.shutdown(); - if (tessNonSorted != null) tessNonSorted.delete(commandList); - if (tessSorted != null) tessSorted.delete(commandList); - if (vertexBuffer != null) commandList.deleteBuffer(vertexBuffer); - if (sharedIndexBuffer != null) commandList.deleteBuffer(sharedIndexBuffer); - if (sortedIndexBuffer != null) commandList.deleteBuffer(sortedIndexBuffer); - GLStateManager.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); - if (directFbo != 0) GLStateManager.glDeleteFramebuffers(directFbo); - if (cpuFbo != 0) GLStateManager.glDeleteFramebuffers(cpuFbo); - if (gpuFbo != 0) GLStateManager.glDeleteFramebuffers(gpuFbo); - for (int tex : directTex) if (tex != 0) GLStateManager.glDeleteTextures(tex); - for (int tex : cpuTex) if (tex != 0) GLStateManager.glDeleteTextures(tex); - for (int tex : gpuTex) if (tex != 0) GLStateManager.glDeleteTextures(tex); for (Section section : scene.all()) MemoryUtilities.memFree(section.srd); } } + + @Test + void cpuIndirectBatchMatchesDirectAcrossFrames() { + assumeTrue(RenderSystem.supportsMultiDrawIndirect(), "multi-draw-indirect unsupported"); + runCpuFactoryParity(IndirectMultiDrawBatch::new, "celeritas CPU indirect emitter"); + } + + @Test + void individualDrawBatchMatchesDirectAcrossFrames() { + runCpuFactoryParity(IndividualDrawBatch::new, "individual draw batch"); + } + + @Test + void gpuCulledIndirectMatchesDirectAcrossFrames() { + assumeTrue(RenderSystem.supportsCompute() && RenderSystem.supportsMultiDrawIndirect(), + "compute-driven indirect culling unsupported"); + + GpuCulling.setMode(GpuCullingMode.COMPUTE); + try { + final Scene scene = buildScene(); + final GpuDrivenChunkCuller culler = new GpuDrivenChunkCuller(); + final SectionMetaBuffer meta = new SectionMetaBuffer(); + final GpuTerrainCuller gpu; + try { + assertTrue(culler.ensureReady(), "chunk_cull.csh failed to load on a real driver"); + gpu = new GpuTerrainCuller(culler, meta); + } catch (RuntimeException | Error e) { + meta.shutdown(); + culler.shutdown(); + throw e; + } + + final RenderRegion region1 = RenderRegionKeys.create(0, 0, 0, 0); + final RenderRegion region2 = RenderRegionKeys.create(0, 0, 0, 1); + final MultiDrawBatchFactory direct = DirectMultiDrawBatch::new; + + try { + try (SceneRig rig = new SceneRig(scene); + Fbo directFbo = new Fbo(); + Fbo gpuFbo = new Fbo()) { + + int local = 0; + for (Section section : scene.solid) metaUpdate(meta, section, local++); + local = 0; + for (Section section : scene.cutout) metaUpdate(meta, section, local++); + local = 0; + for (Section section : scene.sorted) metaUpdate(meta, section, local++); + + for (int frame = 1; frame <= 2; frame++) { + renderCpuBatches(scene, directFbo.id, rig.tessNonSorted, rig.tessSorted, direct); + renderGpu(scene, gpuFbo.id, rig.tessNonSorted, rig.tessSorted, gpu, region1, region2, "frame " + frame); + assertDepthParity(directFbo.id, gpuFbo.id, "frame " + frame + " (GPU-culled indirect emitter)"); + } + + mutateFirstSolidSection(scene); + metaUpdate(meta, scene.solid.get(0), 0); + + renderCpuBatches(scene, directFbo.id, rig.tessNonSorted, rig.tessSorted, direct); + renderGpu(scene, gpuFbo.id, rig.tessNonSorted, rig.tessSorted, gpu, region1, region2, "frame 3 (after section mesh update)"); + assertDepthParity(directFbo.id, gpuFbo.id, "frame 3 (GPU-culled indirect emitter)"); + } + } finally { + gpu.delete(); + meta.shutdown(); + culler.shutdown(); + for (Section section : scene.all()) MemoryUtilities.memFree(section.srd); + } + } finally { + GpuCulling.setMode(GpuCullingMode.CPU_ONLY); + } + } } diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBufferTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBufferTest.java index ed2c6449c..65c9a3254 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBufferTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaBufferTest.java @@ -27,7 +27,9 @@ class SectionMetaBufferTest { final ByteBuffer src = SyntheticSectionData.row(sliceMaskLow, 0xDEADBEEF, vo, ec, io); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), + SectionRenderDataUnsafe.Strategy.FULL.getSliceMask(MemoryUtilities.memAddress(src), 0), + SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final int slot = meta.lookupSlot(0, 16, 32, 64); assertEquals(0, slot, "first allocated slot should be 0"); @@ -59,18 +61,18 @@ class SectionMetaBufferTest { final int[] io = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, vo, ec, io); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final int firstSlot = meta.lookupSlot(0, 16, 32, 64); - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(firstSlot, meta.lookupSlot(0, 16, 32, 64), "slot must be stable across updates"); - meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final int otherSlot = meta.lookupSlot(0, 0, 0, 0); assertEquals(firstSlot + 1, otherSlot, "fresh section -> next high-water slot"); meta.remove(0, 16, 32, 64, 0); assertEquals(-1, meta.lookupSlot(0, 16, 32, 64), "removed slot should not resolve"); - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(firstSlot, meta.lookupSlot(0, 16, 32, 64), "free-list reuse should reclaim the slot"); } finally { MemoryUtilities.memFree(src); @@ -88,7 +90,7 @@ class SectionMetaBufferTest { try { final int x = 3, y = 2, z = 5; final int localIdx = (x << 5) | (z << 2) | y; - meta.update(0, 0, 0, 0, localIdx, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 0, 0, 0, localIdx, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final ByteBuffer mirror = meta.getMirrorForReadOnly(); assertEquals(x << 4, mirror.getInt(0), "origin.x = sectionX << 4"); assertEquals(y << 4, mirror.getInt(4), "origin.y = sectionY << 4"); @@ -110,8 +112,8 @@ class SectionMetaBufferTest { final ByteBuffer srcA = SyntheticSectionData.row(0x33, 0, voA, ecA, io); final ByteBuffer srcB = SyntheticSectionData.row(0x40, 0, voB, ecB, io); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(srcA), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - meta.update(1, 16, 32, 64, 0, MemoryUtilities.memAddress(srcB), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(srcA), 0x33, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(1, 16, 32, 64, 0, MemoryUtilities.memAddress(srcB), 0x40, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final int slotA = meta.lookupSlot(0, 16, 32, 64); final int slotB = meta.lookupSlot(1, 16, 32, 64); assertEquals(0, slotA, "pass 0 -> slot 0"); @@ -138,12 +140,12 @@ class SectionMetaBufferTest { final int[] io = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, vo, ec, io); try { - final int slotA = meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - final int slotB = meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int slotA = meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int slotB = meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(0, slotA, "first allocation -> slot 0"); assertEquals(1, slotB, "second distinct section -> slot 1"); - assertEquals(slotA, meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "re-update returns the stable slot"); - assertEquals(-1, meta.update(0, 0, 0, 0, 0, 0L, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "null dataPtr -> -1"); + assertEquals(slotA, meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "re-update returns the stable slot"); + assertEquals(-1, meta.update(0, 0, 0, 0, 0, 0L, 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "null dataPtr -> -1"); } finally { MemoryUtilities.memFree(src); meta.shutdown(); @@ -156,8 +158,8 @@ class SectionMetaBufferTest { final int[] zeros = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, zeros, zeros, zeros); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertTrue(meta.isDirty(), "dirty after update"); final AtomicReference seen = new AtomicReference<>(); final boolean ran = meta.syncIfDirty(b -> { seen.set(b); return true; }); @@ -180,7 +182,7 @@ class SectionMetaBufferTest { final int[] zeros = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, zeros, zeros, zeros); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final AtomicInteger offers = new AtomicInteger(); assertFalse(meta.syncIfDirty(b -> { offers.incrementAndGet(); return false; }), "a refused sink reports no sync"); @@ -214,13 +216,13 @@ class SectionMetaBufferTest { final int[] zeros = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, zeros, zeros, zeros); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); meta.reset(); assertNull(meta.getMirrorForReadOnly(), "mirror gone after reset"); assertEquals(-1, meta.lookupSlot(0, 16, 32, 64), "slot map cleared"); meta.reset(); meta.reset(); - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(0, meta.lookupSlot(0, 16, 32, 64), "reset is reusable, allocates fresh slot"); } finally { MemoryUtilities.memFree(src); @@ -241,7 +243,7 @@ class SectionMetaBufferTest { try { int x = 0, y = 0, z = 0; while (!stop.get()) { - meta.update(0, x << 4, y << 4, z << 4, 0, srcAddr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, x << 4, y << 4, z << 4, 0, srcAddr, 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); x++; if (x > 63) { x = 0; y++; } if (y > 63) { y = 0; z++; } @@ -286,8 +288,8 @@ class SectionMetaBufferTest { final int[] io = { 0, 0, 0, 0, 0, 0, 0 }; final ByteBuffer src = SyntheticSectionData.row(0, 0, vo, ec, io); try { - meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - meta.update(1, 16, 32, 64, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(1, 16, 32, 64, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); final int slotA = meta.lookupSlot(0, 16, 32, 64); final int slotB = meta.lookupSlot(1, 16, 32, 64); @@ -295,7 +297,7 @@ class SectionMetaBufferTest { assertEquals(-1, meta.lookupSlot(0, 16, 32, 64), "pass 0 slot must be gone"); assertEquals(slotB, meta.lookupSlot(1, 16, 32, 64), "pass 1 slot must survive pass 0 removal"); - meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + meta.update(0, 0, 0, 0, 0, MemoryUtilities.memAddress(src), 0, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(slotA, meta.lookupSlot(0, 0, 0, 0), "free-list reuse claims pass 0's vacated slot"); } finally { MemoryUtilities.memFree(src); diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaSlotLifecycleTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaSlotLifecycleTest.java index 9a8d0bdd3..6c985d6fa 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaSlotLifecycleTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/culling/SectionMetaSlotLifecycleTest.java @@ -19,13 +19,13 @@ class SectionMetaSlotLifecycleTest { final ByteBuffer src = SyntheticSectionData.sliceMaskOnly(0x7F); final long ptr = MemoryUtilities.memAddress(src); try { - for (int i = 0; i < 8; i++) meta.update(0, i * 64, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + for (int i = 0; i < 8; i++) meta.update(0, i * 64, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(8, meta.getHighWaterMark()); for (int i = 0; i < 8; i++) meta.remove(0, i * 64, 0, 0, 0); assertEquals(8, meta.getHighWaterMark(), "high-water mark is monotonic within a session"); - for (int i = 0; i < 8; i++) meta.update(0, (100 + i) * 64, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + for (int i = 0; i < 8; i++) meta.update(0, (100 + i) * 64, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(8, meta.getHighWaterMark(), "eight fresh sections must reuse the eight freed slots"); } finally { MemoryUtilities.memFree(src); @@ -39,12 +39,12 @@ class SectionMetaSlotLifecycleTest { final ByteBuffer src = SyntheticSectionData.sliceMaskOnly(0x7F); final long ptr = MemoryUtilities.memAddress(src); try { - final int a = meta.update(0, 0, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - final int b = meta.update(0, 64, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int a = meta.update(0, 0, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int b = meta.update(0, 64, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertNotEquals(a, b); meta.remove(0, 0, 0, 0, 0); - final int c = meta.update(0, 128, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int c = meta.update(0, 128, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertEquals(a, c, "the freed slot is handed to the next section"); assertNotEquals(b, c, "a live slot is never handed out twice"); } finally { @@ -59,14 +59,14 @@ class SectionMetaSlotLifecycleTest { final ByteBuffer src = SyntheticSectionData.sliceMaskOnly(0x7F); final long ptr = MemoryUtilities.memAddress(src); try { - for (int i = 0; i < 5; i++) meta.update(0, i * 64, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + for (int i = 0; i < 5; i++) meta.update(0, i * 64, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertTrue(meta.getHighWaterMark() > 0); meta.reset(); assertEquals(0, meta.getHighWaterMark()); assertEquals(-1, meta.lookupSlot(0, 0, 0, 0), "slots do not survive a reset"); - assertEquals(0, meta.update(0, 0, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "allocation restarts from slot 0"); + assertEquals(0, meta.update(0, 0, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED), "allocation restarts from slot 0"); } finally { MemoryUtilities.memFree(src); meta.reset(); @@ -79,9 +79,9 @@ class SectionMetaSlotLifecycleTest { final ByteBuffer src = SyntheticSectionData.sliceMaskOnly(0x7F); final long ptr = MemoryUtilities.memAddress(src); try { - final int solid = meta.update(0, 0, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - final int cutout = meta.update(1, 0, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); - final int translucent = meta.update(2, 0, 0, 0, 0, ptr, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int solid = meta.update(0, 0, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int cutout = meta.update(1, 0, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); + final int translucent = meta.update(2, 0, 0, 0, 0, ptr, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED); assertNotEquals(solid, cutout); assertNotEquals(cutout, translucent); assertEquals(3, meta.getHighWaterMark()); @@ -100,7 +100,7 @@ class SectionMetaSlotLifecycleTest { void aNullDataPointerAllocatesNothing() { final SectionMetaBuffer meta = new SectionMetaBuffer(); try { - assertEquals(-1, meta.update(0, 0, 0, 0, 0, 0L, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED)); + assertEquals(-1, meta.update(0, 0, 0, 0, 0, 0L, 0x7F, SectionRenderDataUnsafe.Strategy.FULL, QuadPrimitiveType.TRIANGULATED)); assertEquals(0, meta.getHighWaterMark()); } finally { meta.reset(); diff --git a/src/test/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizerTest.java b/src/test/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizerTest.java index 4b22cc6e7..52d82902d 100644 --- a/src/test/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizerTest.java +++ b/src/test/java/com/gtnewhorizons/angelica/rendering/voxelization/ShadowVoxelizerTest.java @@ -1,8 +1,6 @@ package com.gtnewhorizons.angelica.rendering.voxelization; -import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; -import com.gtnewhorizons.angelica.config.GpuCullingMode; -import com.gtnewhorizons.angelica.rendering.culling.GpuCulling; +import com.gtnewhorizons.angelica.rendering.RenderRegionKeys; import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat; import org.embeddedt.embeddium.impl.gl.util.VertexRange; import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; @@ -20,12 +18,10 @@ import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.util.ArrayList; import java.util.EnumMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -73,34 +69,38 @@ class ShadowVoxelizerTest { }; private static TerrainRenderPass pass(String name) { - return new TerrainRenderPass(name, null, false, false, false, false, VERTEX_TYPE, PRIMITIVE, Map.of()); + return pass(name, false); + } + + private static TerrainRenderPass pass(String name, boolean sorted) { + return new TerrainRenderPass(name, null, false, false, sorted, false, VERTEX_TYPE, PRIMITIVE, Map.of()); } private static RenderRegion region(int x, int y, int z) { - try { - for (Constructor ctor : RenderRegion.class.getDeclaredConstructors()) { - if (ctor.getParameterCount() == 5) { - ctor.setAccessible(true); - return (RenderRegion) ctor.newInstance(x, y, z, 0, null); - } - } - } catch (ReflectiveOperationException e) { - throw new IllegalStateException(e); - } - throw new IllegalStateException("RenderRegion constructor shape changed"); + return RenderRegionKeys.create(x, y, z, 0); } - private SectionRenderDataStorage storage() { - final SectionRenderDataUnsafe.Strategy layout = SectionRenderDataUnsafe.Strategy.COMPACT; - final int bytes = (int) layout.getStride() * RenderRegion.REGION_SIZE; - final ByteBuffer rows = MemoryUtilities.memAlloc(bytes).order(ByteOrder.nativeOrder()); - for (int i = 0; i < bytes; i++) rows.put(i, (byte) 0); - allocated.add(rows); + private static final SectionRenderDataUnsafe.Strategy LAYOUT = SectionRenderDataUnsafe.Strategy.COMPACT; + private long heap() { + return heap(LAYOUT); + } + + private long heap(SectionRenderDataUnsafe.Strategy layout) { + final long heap = layout.allocateHeap(); + heaps.add(heap); + return heap; + } + + private static SectionRenderDataStorage storage(long heap) { + return storage(heap, LAYOUT); + } + + private static SectionRenderDataStorage storage(long heap, SectionRenderDataUnsafe.Strategy layout) { final SectionRenderDataStorage storage = allocateInstance(SectionRenderDataStorage.class); setField(storage, SectionRenderDataStorage.class, "storageStrategy", layout); setField(storage, SectionRenderDataStorage.class, "primitiveType", PRIMITIVE); - setField(storage, SectionRenderDataStorage.class, "pMeshDataArray", MemoryUtilities.memAddress(rows)); + setField(storage, SectionRenderDataStorage.class, "pMeshDataArray", heap); return storage; } @@ -150,10 +150,29 @@ class ShadowVoxelizerTest { } private static ChunkRenderListIterable lists(ChunkRenderList... lists) { - return reverse -> List.of(lists).iterator(); + return new ChunkRenderListIterable() { + @Override + public Iterator iterator(boolean reverse) { + return List.of(lists).iterator(); + } + + @Override + public int getNumRegions() { + return lists.length; + } + + @Override + public ChunkRenderList getRegion(int index) { + return lists[index]; + } + }; } - private static void writeSection(SectionRenderDataStorage storage, int localSectionIndex, int baseVertex, int... vertsPerFacing) { + private static void writeSection(long heap, int localSectionIndex, int baseVertex, int... vertsPerFacing) { + writeSection(heap, LAYOUT, localSectionIndex, baseVertex, vertsPerFacing); + } + + private static void writeSection(long heap, SectionRenderDataUnsafe.Strategy layout, int localSectionIndex, int baseVertex, int... vertsPerFacing) { final Map ranges = new EnumMap<>(ModelQuadFacing.class); int vertex = baseVertex; for (int f = 0; f < vertsPerFacing.length; f++) { @@ -162,7 +181,7 @@ class ShadowVoxelizerTest { } vertex += vertsPerFacing[f]; } - storage.getStorageStrategy().writeMeshes(storage.getDataPointer(localSectionIndex), baseVertex, 0, ranges, PRIMITIVE); + layout.writeMeshesAndSliceMask(heap, localSectionIndex, baseVertex, 0, ranges, PRIMITIVE); } private static RecordingSink walk(ChunkRenderListIterable renderLists, TerrainRenderPass renderPass, CameraTransform camera, boolean faceCulling) { @@ -171,46 +190,44 @@ class ShadowVoxelizerTest { return sink; } - private final List allocated = new ArrayList<>(); + private final List heaps = new ArrayList<>(); @AfterEach void tearDown() { - GpuCulling.setMode(GpuCullingMode.CPU_ONLY); - allocated.forEach(MemoryUtilities::memFree); - allocated.clear(); + heaps.forEach(LAYOUT::freeHeap); + heaps.clear(); } @Test - void emittedRangesAreIdenticalAcrossCullModes() { + void emittedRangesCoverMultipleSectionsInARegion() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(0, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 0, 8, 4, 0, 0, 12); - writeSection(storage, 5, 100, 0, 16, 0, 0, 4, 0, 0); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 0, 8, 4, 0, 0, 12); + writeSection(heap, 5, 100, 0, 16, 0, 0, 4, 0, 0); attachStorage(region, renderPass, storage); final ChunkRenderListIterable renderLists = lists(renderList(region, 0, 5)); final CameraTransform camera = new CameraTransform(0, 0, 0); - GpuCulling.setMode(GpuCullingMode.CPU_ONLY); - final RecordingSink cpu = walk(renderLists, renderPass, camera, false); - GpuCulling.setMode(GpuCullingMode.COMPUTE); - final RecordingSink compute = walk(renderLists, renderPass, camera, false); + final RecordingSink sink = walk(renderLists, renderPass, camera, false); - assertFalse(cpu.ranges.isEmpty(), "the walk produced nothing to compare"); - assertEquals(cpu.ranges.size(), compute.ranges.size(), "range count differs between cull modes"); - for (int i = 0; i < cpu.ranges.size(); i++) { - assertArrayEqualsInt(cpu.ranges.get(i), compute.ranges.get(i), "range " + i); - } - assertEquals(1, cpu.regions.size(), "one region announced once"); - assertEquals(1, compute.regions.size()); + assertEquals(1, sink.regions.size(), "one region announced once"); + assertEquals(5, sink.ranges.size(), "section 0's facings 2-3 merge, section 5's facings stay separate"); + assertArrayEqualsInt(new int[]{0, 4}, sink.ranges.get(0), "section 0 facing 0"); + assertArrayEqualsInt(new int[]{4, 12}, sink.ranges.get(1), "section 0 facings 2-3"); + assertArrayEqualsInt(new int[]{16, 12}, sink.ranges.get(2), "section 0 facing 6"); + assertArrayEqualsInt(new int[]{100, 16}, sink.ranges.get(3), "section 5 facing 1"); + assertArrayEqualsInt(new int[]{116, 4}, sink.ranges.get(4), "section 5 facing 4"); } @Test void regionOffsetIsCameraRelative() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(1, 0, 3); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 0, 0, 0, 0, 0, 0); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 0, 0, 0, 0, 0, 0); attachStorage(region, renderPass, storage); final CameraTransform camera = new CameraTransform(70.25, 12.5, -3.75); @@ -227,8 +244,9 @@ class ShadowVoxelizerTest { void regionOffsetUsesTheRegionOriginNotA256BlockGrid() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(1, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 0, 0, 0, 0, 0, 0); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 0, 0, 0, 0, 0, 0); attachStorage(region, renderPass, storage); final RecordingSink sink = walk(lists(renderList(region, 0)), renderPass, new CameraTransform(0, 0, 0), false); @@ -240,8 +258,9 @@ class ShadowVoxelizerTest { void contiguousFacingsCollapseToOneRange() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(0, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 4, 4, 4, 4, 4, 4); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 4, 4, 4, 4, 4, 4); attachStorage(region, renderPass, storage); final RecordingSink sink = walk(lists(renderList(region, 0)), renderPass, new CameraTransform(0, 0, 0), false); @@ -255,8 +274,9 @@ class ShadowVoxelizerTest { void gapsInTheFacingMaskSplitRuns() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(0, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 0, 8, 0, 0, 0, 4); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 0, 8, 0, 0, 0, 4); attachStorage(region, renderPass, storage); final RecordingSink sink = walk(lists(renderList(region, 0)), renderPass, new CameraTransform(0, 0, 0), false); @@ -271,8 +291,9 @@ class ShadowVoxelizerTest { void sectionsWithoutGeometryNeverAnnounceTheRegion() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(0, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 0, 0, 0, 0, 0, 0, 0); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 0, 0, 0, 0, 0, 0, 0); attachStorage(region, renderPass, storage); final RecordingSink sink = walk(lists(renderList(region, 0)), renderPass, new CameraTransform(0, 0, 0), false); @@ -298,10 +319,12 @@ class ShadowVoxelizerTest { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion regionA = region(0, 0, 0); final RenderRegion regionB = region(1, 0, 0); - final SectionRenderDataStorage storageA = storage(); - final SectionRenderDataStorage storageB = storage(); - writeSection(storageA, 0, 0, 4, 0, 0, 0, 0, 0, 0); - writeSection(storageB, 0, 0, 4, 0, 0, 0, 0, 0, 0); + final long heapA = heap(); + final long heapB = heap(); + final SectionRenderDataStorage storageA = storage(heapA); + final SectionRenderDataStorage storageB = storage(heapB); + writeSection(heapA, 0, 0, 4, 0, 0, 0, 0, 0, 0); + writeSection(heapB, 0, 0, 4, 0, 0, 0, 0, 0, 0); attachStorage(regionA, renderPass, storageA); attachStorage(regionB, renderPass, storageB); @@ -319,10 +342,12 @@ class ShadowVoxelizerTest { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion regionA = region(0, 0, 0); final RenderRegion regionB = region(1, 0, 0); - final SectionRenderDataStorage storageA = storage(); - final SectionRenderDataStorage storageB = storage(); - writeSection(storageA, 0, 0, 4, 0, 4, 0, 0, 0, 0); - writeSection(storageB, 0, 0, 4, 0, 0, 0, 0, 0, 0); + final long heapA = heap(); + final long heapB = heap(); + final SectionRenderDataStorage storageA = storage(heapA); + final SectionRenderDataStorage storageB = storage(heapB); + writeSection(heapA, 0, 0, 4, 0, 4, 0, 0, 0, 0); + writeSection(heapB, 0, 0, 4, 0, 0, 0, 0, 0, 0); attachStorage(regionA, renderPass, storageA); attachStorage(regionB, renderPass, storageB); @@ -339,25 +364,52 @@ class ShadowVoxelizerTest { } @Test - void faceCullingNarrowsTheEmittedRangesTheSameWayInBothModes() { + void faceCullingNarrowsTheEmittedRanges() { final TerrainRenderPass renderPass = pass("solid"); final RenderRegion region = region(0, 0, 0); - final SectionRenderDataStorage storage = storage(); - writeSection(storage, 0, 0, 4, 4, 4, 4, 4, 4, 4); + final long heap = heap(); + final SectionRenderDataStorage storage = storage(heap); + writeSection(heap, 0, 0, 4, 4, 4, 4, 4, 4, 4); attachStorage(region, renderPass, storage); final ChunkRenderListIterable renderLists = lists(renderList(region, 0)); final CameraTransform camera = new CameraTransform(500, 200, 500); - GpuCulling.setMode(GpuCullingMode.CPU_ONLY); - final RecordingSink cpu = walk(renderLists, renderPass, camera, true); - GpuCulling.setMode(GpuCullingMode.COMPUTE); - final RecordingSink compute = walk(renderLists, renderPass, camera, true); + final RecordingSink sink = walk(renderLists, renderPass, camera, true); - assertFalse(cpu.ranges.isEmpty(), "face culling removed everything; the fixture is not exercising the path"); - assertTrue(cpu.ranges.size() < 7 || cpu.ranges.get(0)[1] < 28, "face culling did not narrow anything"); - assertEquals(cpu.ranges.size(), compute.ranges.size()); - for (int i = 0; i < cpu.ranges.size(); i++) { - assertArrayEqualsInt(cpu.ranges.get(i), compute.ranges.get(i), "range " + i); + assertFalse(sink.ranges.isEmpty(), "face culling removed everything; the fixture is not exercising the path"); + assertTrue(sink.ranges.size() < 7 || sink.ranges.get(0)[1] < 28, "face culling did not narrow anything"); + } + + @Test + void sortedPassWalksTheFullLayoutAndYieldsTheSameSpansAsCompact() { + final SectionRenderDataUnsafe.Strategy full = SectionRenderDataUnsafe.Strategy.FULL; + final int[] facings = { 4, 0, 8, 0, 0, 0, 4 }; + + final TerrainRenderPass sortedPass = pass("translucent", true); + final RenderRegion sortedRegion = region(0, 0, 0); + final long sortedHeap = heap(full); + final SectionRenderDataStorage sortedStorage = storage(sortedHeap, full); + writeSection(sortedHeap, full, 0, 0, facings); + writeSection(sortedHeap, full, 5, 100, facings); + attachStorage(sortedRegion, sortedPass, sortedStorage); + + final TerrainRenderPass solidPass = pass("solid"); + final RenderRegion solidRegion = region(0, 0, 0); + final long solidHeap = heap(); + final SectionRenderDataStorage solidStorage = storage(solidHeap); + writeSection(solidHeap, 0, 0, facings); + writeSection(solidHeap, 5, 100, facings); + attachStorage(solidRegion, solidPass, solidStorage); + + final RecordingSink sorted = walk(lists(renderList(sortedRegion, 0, 5)), sortedPass, new CameraTransform(0, 0, 0), false); + final RecordingSink compact = walk(lists(renderList(solidRegion, 0, 5)), solidPass, new CameraTransform(0, 0, 0), false); + + assertEquals(1, sorted.regions.size(), "the sorted region is announced once"); + assertFalse(sorted.ranges.isEmpty(), "the FULL layout walk produced nothing"); + assertEquals(6, compact.ranges.size(), "two sections of three runs each"); + assertEquals(compact.ranges.size(), sorted.ranges.size(), "FULL and COMPACT disagree on run count"); + for (int i = 0; i < compact.ranges.size(); i++) { + assertArrayEqualsInt(compact.ranges.get(i), sorted.ranges.get(i), "range " + i); } } diff --git a/src/test/java/net/coderbot/iris/shadows/frustum/BoxCullerTest.java b/src/test/java/net/coderbot/iris/shadows/frustum/BoxCullerTest.java new file mode 100644 index 000000000..e3f914dd9 --- /dev/null +++ b/src/test/java/net/coderbot/iris/shadows/frustum/BoxCullerTest.java @@ -0,0 +1,86 @@ +package net.coderbot.iris.shadows.frustum; + +import org.junit.jupiter.api.Test; + +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BoxCullerTest { + + private static BoxCuller culler(double maxDistance, double cameraX, double cameraY, double cameraZ) { + final BoxCuller culler = new BoxCuller(maxDistance); + culler.setPosition(cameraX, cameraY, cameraZ); + return culler; + } + + @Test + void fullyInsideMatchesTheSnappedPaddedBounds() { + final BoxCuller culler = culler(64.0, 8.0, 8.0, 8.0); + + assertTrue(culler.isFullyInsideViewRelative(-72, -72, -72, 72, 72, 72)); + assertTrue(culler.isFullyInsideViewRelative(-1, -1, -1, 1, 1, 1)); + assertFalse(culler.isFullyInsideViewRelative(-72.5f, 0, 0, 0, 0, 0)); + assertFalse(culler.isFullyInsideViewRelative(0, 0, 0, 0, 72.5f, 0)); + assertFalse(culler.isFullyInsideViewRelative(0, 0, -72.5f, 0, 0, 72.5f)); + } + + @Test + void snappingIsNotCameraSymmetric() { + final BoxCuller culler = culler(64.0, 0.0, 0.0, 0.0); + + assertTrue(culler.isFullyInsideViewRelative(-64, -64, -64, 80, 80, 80)); + assertFalse(culler.isFullyInsideViewRelative(-64.5f, 0, 0, 0, 0, 0)); + assertFalse(culler.isFullyInsideViewRelative(0, 0, 0, 80.5f, 0, 0)); + } + + @Test + void fullyInsideNeverOverlapsCulled() { + final BoxCuller culler = culler(48.0, 123.75, -7.5, 640.0); + final Random random = new Random(20260906L); + + int fullyInside = 0; + int culled = 0; + int straddling = 0; + + for (int i = 0; i < 20000; i++) { + final float minX = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float minY = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float minZ = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float maxX = minX + random.nextFloat() * 48.0f; + final float maxY = minY + random.nextFloat() * 48.0f; + final float maxZ = minZ + random.nextFloat() * 48.0f; + + final boolean inside = culler.isFullyInsideViewRelative(minX, minY, minZ, maxX, maxY, maxZ); + final boolean outside = culler.isCulledViewRelative(minX, minY, minZ, maxX, maxY, maxZ); + final String box = minX + "," + minY + "," + minZ + " -> " + maxX + "," + maxY + "," + maxZ; + + assertFalse(inside && outside, box); + + if (inside) { + fullyInside++; + assertCornersKept(culler, minX, minY, minZ, maxX, maxY, maxZ, box); + } else if (outside) { + culled++; + } else { + straddling++; + } + } + + assertTrue(fullyInside > 0, "no fully inside samples"); + assertTrue(culled > 0, "no culled samples"); + assertTrue(straddling > 0, "no straddling samples"); + } + + private static void assertCornersKept(BoxCuller culler, float minX, float minY, float minZ, float maxX, float maxY, float maxZ, String box) { + for (int corner = 0; corner < 8; corner++) { + final float x = (corner & 1) == 0 ? minX : maxX; + final float y = (corner & 2) == 0 ? minY : maxY; + final float z = (corner & 4) == 0 ? minZ : maxZ; + + assertFalse(culler.isCulledViewRelative(x, y, z, x, y, z), box + " corner " + corner); + assertTrue(culler.isFullyInsideViewRelative(x, y, z, x, y, z), box + " corner " + corner); + } + } +} diff --git a/src/test/java/net/coderbot/iris/shadows/frustum/SafeZoneFrustumReuseTest.java b/src/test/java/net/coderbot/iris/shadows/frustum/SafeZoneFrustumReuseTest.java new file mode 100644 index 000000000..915ea2490 --- /dev/null +++ b/src/test/java/net/coderbot/iris/shadows/frustum/SafeZoneFrustumReuseTest.java @@ -0,0 +1,55 @@ +package net.coderbot.iris.shadows.frustum; + +import net.coderbot.iris.shadows.frustum.advanced.SafeZoneCullingFrustum; +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.junit.jupiter.api.Test; + +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.SUN_UP; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.culler; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.forEachSampleAab; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.projection; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SafeZoneFrustumReuseTest { + private static final Vector3f SUN_ANGLED = new Vector3f(0.4f, 0.8f, 0.45f).normalize(); + + private static SafeZoneCullingFrustum initialized(Matrix4f view, Vector3f light, BoxCuller voxel, BoxCuller distance) { + return reinitialized(new SafeZoneCullingFrustum(), view, light, voxel, distance); + } + + private static SafeZoneCullingFrustum reinitialized(SafeZoneCullingFrustum frustum, Matrix4f view, Vector3f light, BoxCuller voxel, BoxCuller distance) { + frustum.init(view, projection(), light, voxel, distance); + frustum.setPosition(8.0, 8.0, 8.0); + return frustum; + } + + private static void assertSameResults(SafeZoneCullingFrustum reused, SafeZoneCullingFrustum reference, long seed) { + forEachSampleAab(seed, (minX, minY, minZ, maxX, maxY, maxZ) -> { + assertEquals(reference.intersectAab(minX, minY, minZ, maxX, maxY, maxZ), + reused.intersectAab(minX, minY, minZ, maxX, maxY, maxZ), + () -> minX + "," + minY + "," + minZ + " -> " + maxX + "," + maxY + "," + maxZ); + assertEquals(reference.testAab(minX, minY, minZ, maxX, maxY, maxZ), + reused.testAab(minX, minY, minZ, maxX, maxY, maxZ), + () -> minX + "," + minY + "," + minZ + " -> " + maxX + "," + maxY + "," + maxZ); + }); + } + + @Test + void reinitSwapsCullers() { + final SafeZoneCullingFrustum reused = initialized(new Matrix4f(), SUN_UP, culler(16.0), culler(96.0)); + reinitialized(reused, new Matrix4f(), SUN_UP, culler(96.0), culler(16.0)); + + assertSameResults(reused, initialized(new Matrix4f(), SUN_UP, culler(96.0), culler(16.0)), 20250911L); + } + + @Test + void reinitReplacesClippingPlanes() { + final Matrix4f rotated = new Matrix4f().rotateY((float) Math.toRadians(37.0)); + + final SafeZoneCullingFrustum reused = initialized(new Matrix4f(), SUN_UP, culler(16.0), culler(96.0)); + reinitialized(reused, rotated, SUN_ANGLED, culler(24.0), culler(120.0)); + + assertSameResults(reused, initialized(rotated, SUN_ANGLED, culler(24.0), culler(120.0)), 20250912L); + } +} diff --git a/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumIntersectAabTest.java b/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumIntersectAabTest.java new file mode 100644 index 000000000..bbacdc50d --- /dev/null +++ b/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumIntersectAabTest.java @@ -0,0 +1,158 @@ +package net.coderbot.iris.shadows.frustum; + +import net.coderbot.iris.shadows.frustum.advanced.AdvancedShadowCullingFrustum; +import net.coderbot.iris.shadows.frustum.advanced.SafeZoneCullingFrustum; +import net.coderbot.iris.shadows.frustum.fallback.BoxCullingFrustum; +import net.minecraft.client.renderer.culling.Frustrum; +import org.embeddedt.embeddium.impl.render.viewport.frustum.Frustum; +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.SUN_UP; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.culler; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.forEachSampleAab; +import static net.coderbot.iris.shadows.frustum.ShadowFrustumTestSupport.projection; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ShadowFrustumIntersectAabTest { + private static AdvancedShadowCullingFrustum advanced(BoxCuller boxCuller) { + final AdvancedShadowCullingFrustum frustum = new AdvancedShadowCullingFrustum(); + frustum.init(new Matrix4f(), projection(), SUN_UP, boxCuller); + frustum.setPosition(8.0, 8.0, 8.0); + return frustum; + } + + private static SafeZoneCullingFrustum safeZone(BoxCuller voxelCuller, BoxCuller distanceCuller) { + final SafeZoneCullingFrustum frustum = new SafeZoneCullingFrustum(); + frustum.init(new Matrix4f(), projection(), SUN_UP, voxelCuller, distanceCuller); + frustum.setPosition(8.0, 8.0, 8.0); + return frustum; + } + + @Test + void frustrumStubIsActive() { + assertThrows(NoSuchFieldException.class, () -> Frustrum.class.getDeclaredField("clippingHelper"), + "the real Frustrum's GL-touching clippingHelper field is on the classpath; the test stub did not win"); + } + + @Test + void boxCullingFrustumReportsTriState() { + final BoxCullingFrustum frustum = new BoxCullingFrustum(new BoxCuller(64.0)); + frustum.setPosition(8.0, 8.0, 8.0); + + assertEquals(Frustum.FULLY_INSIDE, frustum.intersectAab(-16, -16, -16, 16, 16, 16)); + assertEquals(Frustum.PARTIALLY_INSIDE, frustum.intersectAab(-16, -16, -16, 16, 16, 100)); + assertEquals(Frustum.OUTSIDE, frustum.intersectAab(-16, -16, 80, 16, 16, 100)); + assertConsistent(frustum, 1L); + } + + @Test + void advancedReportsTriStateWithoutABoxCuller() { + final AdvancedShadowCullingFrustum frustum = advanced(null); + + assertEquals(Frustum.FULLY_INSIDE, frustum.intersectAab(-1, -1, -51, 1, 1, -49)); + assertEquals(Frustum.PARTIALLY_INSIDE, frustum.intersectAab(-1, -60, -51, 1, -40, -49)); + assertEquals(Frustum.OUTSIDE, frustum.intersectAab(-1, -1, 99, 1, 1, 101)); + } + + @Test + void advancedDowngradesToPartialOutsideTheBoxCuller() { + final AdvancedShadowCullingFrustum frustum = advanced(culler(64.0)); + + assertEquals(Frustum.FULLY_INSIDE, frustum.intersectAab(-1, -1, -51, 1, 1, -49)); + assertEquals(Frustum.PARTIALLY_INSIDE, frustum.intersectAab(-1, -1, -80, 1, 1, -60)); + assertEquals(Frustum.OUTSIDE, frustum.intersectAab(-1, -1, -200, 1, 1, -190)); + } + + @Test + void advancedAgreesWithTestAab() { + assertConsistent(advanced(null), 20250906L); + assertConsistent(advanced(culler(64.0)), 20250907L); + } + + @Test + void advancedReportsOcclusionSearchSupportAndLightVector() { + final Vector3f light = new Vector3f(0.4f, 0.8f, 0.45f).normalize(); + final AdvancedShadowCullingFrustum frustum = new AdvancedShadowCullingFrustum(); + frustum.init(new Matrix4f(), projection(), light, null); + + assertTrue(frustum.supportsOcclusionSearch()); + assertEquals(light.x(), frustum.shadowLightX()); + assertEquals(light.y(), frustum.shadowLightY()); + assertEquals(light.z(), frustum.shadowLightZ()); + } + + @Test + void safeZoneReportsTriState() { + final SafeZoneCullingFrustum frustum = safeZone(culler(16.0), culler(96.0)); + + assertEquals(Frustum.FULLY_INSIDE, frustum.intersectAab(-1, -1, -11, 1, 1, -9)); + assertEquals(Frustum.PARTIALLY_INSIDE, frustum.intersectAab(-1, -60, -51, 1, -40, -49)); + assertEquals(Frustum.OUTSIDE, frustum.intersectAab(-1, -1, 50, 1, 1, 60)); + assertEquals(Frustum.OUTSIDE, frustum.intersectAab(-1, -1, -200, 1, 1, -190)); + } + + @Test + void safeZoneAgreesWithTestAab() { + assertConsistent(safeZone(culler(16.0), culler(96.0)), 20250908L); + assertConsistent(safeZone(culler(16.0), null), 20250909L); + assertConsistent(safeZone(culler(96.0), culler(16.0)), 20250910L); + } + + @Test + void safeZoneLargerThanDistanceIsOnlyPartiallyInside() { + final SafeZoneCullingFrustum frustum = safeZone(culler(96.0), culler(16.0)); + + assertEquals(Frustum.PARTIALLY_INSIDE, frustum.intersectAab(-30, -1, -1, 30, 1, 1)); + assertEquals(Frustum.FULLY_INSIDE, frustum.intersectAab(-1, -1, -11, 1, 1, -9)); + } + + @Test + void safeZoneDoesNotSupportOcclusionSearch() { + final SafeZoneCullingFrustum frustum = safeZone(culler(16.0), culler(96.0)); + + assertFalse(frustum.supportsOcclusionSearch()); + } + + private static void assertConsistent(Frustum frustum, long seed) { + final AtomicInteger fullyInsideCount = new AtomicInteger(); + final AtomicInteger partiallyInsideCount = new AtomicInteger(); + final AtomicInteger outsideCount = new AtomicInteger(); + + forEachSampleAab(seed, (minX, minY, minZ, maxX, maxY, maxZ) -> { + final int result = frustum.intersectAab(minX, minY, minZ, maxX, maxY, maxZ); + + assertEquals(frustum.testAab(minX, minY, minZ, maxX, maxY, maxZ), result != Frustum.OUTSIDE, + () -> minX + "," + minY + "," + minZ + " -> " + maxX + "," + maxY + "," + maxZ); + + if (result == Frustum.FULLY_INSIDE) { + fullyInsideCount.incrementAndGet(); + + for (int corner = 0; corner < 8; corner++) { + final float x = (corner & 1) == 0 ? minX : maxX; + final float y = (corner & 2) == 0 ? minY : maxY; + final float z = (corner & 4) == 0 ? minZ : maxZ; + final int cornerIndex = corner; + + assertTrue(frustum.testAab(x, y, z, x, y, z), + () -> minX + "," + minY + "," + minZ + " -> " + maxX + "," + maxY + "," + maxZ + + " corner " + cornerIndex); + } + } else if (result == Frustum.PARTIALLY_INSIDE) { + partiallyInsideCount.incrementAndGet(); + } else { + outsideCount.incrementAndGet(); + } + }); + + assertTrue(fullyInsideCount.get() > 0, "no fully inside samples"); + assertTrue(partiallyInsideCount.get() > 0, "no partially inside samples"); + assertTrue(outsideCount.get() > 0, "no outside samples"); + } +} diff --git a/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumTestSupport.java b/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumTestSupport.java new file mode 100644 index 000000000..d4bd20f38 --- /dev/null +++ b/src/test/java/net/coderbot/iris/shadows/frustum/ShadowFrustumTestSupport.java @@ -0,0 +1,44 @@ +package net.coderbot.iris.shadows.frustum; + +import org.joml.Matrix4f; +import org.joml.Vector3f; + +import java.util.Random; + +final class ShadowFrustumTestSupport { + static final Vector3f SUN_UP = new Vector3f(0.0f, 1.0f, 0.0f); + + private static final int SAMPLE_COUNT = 20000; + + interface AabConsumer { + void accept(float minX, float minY, float minZ, float maxX, float maxY, float maxZ); + } + + private ShadowFrustumTestSupport() { + } + + static Matrix4f projection() { + return new Matrix4f().perspective((float) Math.toRadians(90.0), 1.0f, 0.05f, 256.0f); + } + + static BoxCuller culler(double maxDistance) { + final BoxCuller culler = new BoxCuller(maxDistance); + culler.setPosition(8.0, 8.0, 8.0); + return culler; + } + + static void forEachSampleAab(long seed, AabConsumer consumer) { + final Random random = new Random(seed); + + for (int i = 0; i < SAMPLE_COUNT; i++) { + final float minX = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float minY = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float minZ = (random.nextFloat() * 2.0f - 1.0f) * 160.0f; + final float maxX = minX + random.nextFloat() * 48.0f; + final float maxY = minY + random.nextFloat() * 48.0f; + final float maxZ = minZ + random.nextFloat() * 48.0f; + + consumer.accept(minX, minY, minZ, maxX, maxY, maxZ); + } + } +} diff --git a/src/test/java/net/minecraft/client/renderer/culling/Frustrum.java b/src/test/java/net/minecraft/client/renderer/culling/Frustrum.java new file mode 100644 index 000000000..47a9e6c67 --- /dev/null +++ b/src/test/java/net/minecraft/client/renderer/culling/Frustrum.java @@ -0,0 +1,24 @@ +package net.minecraft.client.renderer.culling; + +import net.minecraft.util.AxisAlignedBB; + +public class Frustrum implements ICamera { + public double xPosition; + public double yPosition; + public double zPosition; + + public void setPosition(double x, double y, double z) { + this.xPosition = x; + this.yPosition = y; + this.zPosition = z; + } + + public boolean isBoxInFrustum(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) { + return true; + } + + @Override + public boolean isBoundingBoxInFrustum(AxisAlignedBB aabb) { + return this.isBoxInFrustum(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ); + } +} diff --git a/src/test/java/org/embeddedt/embeddium/impl/render/chunk/data/SectionRenderDataLayoutTest.java b/src/test/java/org/embeddedt/embeddium/impl/render/chunk/data/SectionRenderDataLayoutTest.java new file mode 100644 index 000000000..11849b2af --- /dev/null +++ b/src/test/java/org/embeddedt/embeddium/impl/render/chunk/data/SectionRenderDataLayoutTest.java @@ -0,0 +1,320 @@ +package org.embeddedt.embeddium.impl.render.chunk.data; + +import org.embeddedt.embeddium.impl.gl.util.VertexRange; +import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; +import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.ChunkPrimitiveType; +import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; +import org.embeddedt.embeddium.impl.render.chunk.sorting.TranslucentQuadAnalyzer; +import org.junit.jupiter.api.Test; + +import java.nio.ByteBuffer; +import java.util.EnumMap; +import java.util.Map; +import java.util.Random; + +import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class SectionRenderDataLayoutTest { + private static final ChunkPrimitiveType QUADS = new StubPrimitiveType(4, 6); + private static final ChunkPrimitiveType TRIANGLES = new StubPrimitiveType(3, 3); + + private static final SectionRenderDataUnsafe.Strategy FULL = SectionRenderDataUnsafe.Strategy.FULL; + private static final SectionRenderDataUnsafe.Strategy COMPACT = SectionRenderDataUnsafe.Strategy.COMPACT; + + private record StubPrimitiveType(int verticesPerPrimitive, int elementsPerPrimitive) implements ChunkPrimitiveType { + @Override + public int getVerticesPerPrimitive() { + return this.verticesPerPrimitive; + } + + @Override + public int getIndexBufferElementsPerPrimitive() { + return this.elementsPerPrimitive; + } + + @Override + public void generateSimpleIndexBuffer(ByteBuffer indexBuffer, int numPrimitives) { + throw new UnsupportedOperationException(); + } + + @Override + public void generateSortedIndexBuffer(ByteBuffer indexBuffer, int numPrimitives, TranslucentQuadAnalyzer.SortState chunkData, float x, float y, float z) { + throw new UnsupportedOperationException(); + } + } + + private static Map ranges(ChunkPrimitiveType type, int... primitivesPerFacing) { + Map map = new EnumMap<>(ModelQuadFacing.class); + int start = 0; + + for (int facing = 0; facing < primitivesPerFacing.length; facing++) { + int vertexCount = primitivesPerFacing[facing] * type.getVerticesPerPrimitive(); + + if (vertexCount > 0) { + map.put(ModelQuadFacing.VALUES[facing], new VertexRange(start, vertexCount)); + } + + start += vertexCount; + } + + return map; + } + + private static long alloc(SectionRenderDataUnsafe.Strategy strategy) { + long heap = strategy.allocateHeap(); + assertNotEquals(0L, heap, "allocation failed"); + return heap; + } + + @Test + void bothLayoutsAgreeOnEveryPerFacingField() { + Random random = new Random(20260801L); + + for (int trial = 0; trial < 200; trial++) { + int[] primitives = new int[ModelQuadFacing.COUNT]; + for (int facing = 0; facing < primitives.length; facing++) { + primitives[facing] = random.nextInt(4) == 0 ? 0 : random.nextInt(64); + } + + int vertexBase = random.nextInt(100_000); + var meshes = ranges(QUADS, primitives); + + long fullHeap = alloc(FULL); + long compactHeap = alloc(COMPACT); + + try { + FULL.writeMeshesAndSliceMask(fullHeap, 0, vertexBase, 0, meshes, QUADS); + COMPACT.writeMeshesAndSliceMask(compactHeap, 0, vertexBase, 0, meshes, QUADS); + + assertEquals(FULL.getSliceMask(fullHeap, 0), COMPACT.getSliceMask(compactHeap, 0), + "slice mask must be layout independent"); + + long full = FULL.heapPointer(fullHeap, 0); + long compact = COMPACT.heapPointer(compactHeap, 0); + + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + assertEquals(FULL.getVertexOffset(full, facing), + COMPACT.getVertexOffset(compact, facing), + "vertex offset for facing " + facing); + + assertEquals(FULL.getElementCount(full, facing, QUADS), + COMPACT.getElementCount(compact, facing, QUADS), + "element count for facing " + facing); + + assertEquals(FULL.getRunVertexEnd(full, facing, QUADS), + COMPACT.getRunVertexEnd(compact, facing, QUADS), + "run end for facing " + facing); + } + } finally { + FULL.freeHeap(fullHeap); + COMPACT.freeHeap(compactHeap); + } + } + } + + @Test + void mergedRunElementCountEqualsSumOfItsFacings() { + for (ChunkPrimitiveType type : new ChunkPrimitiveType[] { QUADS, TRIANGLES }) { + var meshes = ranges(type, 3, 0, 7, 11, 0, 5, 2); + long heap = alloc(COMPACT); + + try { + COMPACT.writeMeshesAndSliceMask(heap, 0, 512, 0, meshes, type); + long ptr = COMPACT.heapPointer(heap, 0); + + for (int first = 0; first < ModelQuadFacing.COUNT; first++) { + for (int last = first; last < ModelQuadFacing.COUNT; last++) { + int summed = 0; + for (int facing = first; facing <= last; facing++) { + summed += COMPACT.getElementCount(ptr, facing, type); + } + + int start = COMPACT.getVertexOffset(ptr, first); + int end = COMPACT.getRunVertexEnd(ptr, last, type); + int merged = SectionRenderDataUnsafe.elementsForVertices(end - start, type); + + assertEquals(summed, merged, "run [" + first + ", " + last + "]"); + } + } + } finally { + COMPACT.freeHeap(heap); + } + } + } + + @Test + void compactRebasePreservesEveryFacingSpan() { + var meshes = ranges(QUADS, 5, 0, 9, 0, 0, 13, 1); + + long heap = alloc(COMPACT); + + try { + COMPACT.writeMeshesAndSliceMask(heap, 0, 1024, 0, meshes, QUADS); + long ptr = COMPACT.heapPointer(heap, 0); + + int[] spansBefore = new int[ModelQuadFacing.COUNT]; + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + spansBefore[facing] = COMPACT.getElementCount(ptr, facing, QUADS); + } + + COMPACT.rebase(ptr, 8192, 0, QUADS); + + assertEquals(8192, COMPACT.getVertexOffset(ptr, 0), "rebased base offset"); + + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + assertEquals(spansBefore[facing], COMPACT.getElementCount(ptr, facing, QUADS), + "element count for facing " + facing + " changed across rebase"); + } + } finally { + COMPACT.freeHeap(heap); + } + } + + @Test + void fullRebasePreservesEveryFacingSpan() { + var meshes = ranges(QUADS, 5, 0, 9, 0, 0, 13, 1); + + long heap = alloc(FULL); + + try { + FULL.writeMeshesAndSliceMask(heap, 0, 1024, 256, meshes, QUADS); + long ptr = FULL.heapPointer(heap, 0); + + int[] spansBefore = new int[ModelQuadFacing.COUNT]; + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + spansBefore[facing] = FULL.getElementCount(ptr, facing, QUADS); + } + + FULL.rebase(ptr, 8192, 512, QUADS); + + assertEquals(8192, FULL.getVertexOffset(ptr, 0), "rebased base offset"); + assertEquals(512, FULL.getIndexOffset(ptr, 0), "rebased index offset"); + + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + assertEquals(spansBefore[facing], FULL.getElementCount(ptr, facing, QUADS), + "element count for facing " + facing + " changed across rebase"); + } + } finally { + FULL.freeHeap(heap); + } + } + + @Test + void compactDrawsFromTheSharedIndexBufferSoEveryFacingHasAZeroIndexOffset() { + var meshes = ranges(QUADS, 4, 4, 4, 4, 4, 4, 4); + long heap = alloc(COMPACT); + + try { + COMPACT.writeMeshesAndSliceMask(heap, 0, 0, 4096, meshes, QUADS); + long ptr = COMPACT.heapPointer(heap, 0); + + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + assertEquals(0, COMPACT.getIndexOffset(ptr, facing), + "facing " + facing + " must draw from the shared index buffer"); + } + } finally { + COMPACT.freeHeap(heap); + } + } + + @Test + void compactRejectsPerFacingIndexOffsets() { + long heap = alloc(COMPACT); + + try { + long ptr = COMPACT.heapPointer(heap, 0); + assertThrows(UnsupportedOperationException.class, () -> COMPACT.writeIndexOffsets(ptr, 0, QUADS)); + } finally { + COMPACT.freeHeap(heap); + } + } + + @Test + void compactIsSmallerThanFull() { + assertEquals(32, COMPACT.getStride()); + assertEquals(92, FULL.getStride()); + } + + @Test + void onlyCompactReservesASliceMaskHeader() { + assertEquals(0, FULL.getHeaderSize(), "the full layout keeps its slice mask in the row"); + assertEquals(RenderRegion.REGION_SIZE, COMPACT.getHeaderSize(), "one slice mask byte per section"); + + for (var strategy : SectionRenderDataUnsafe.Strategy.values()) { + long heap = alloc(strategy); + + try { + assertEquals(strategy.getHeaderSize(), strategy.heapPointer(heap, 0) - heap, + strategy + " row 0 must start after the header"); + } finally { + strategy.freeHeap(heap); + } + } + } + + @Test + void sliceMasksAreIndependentAcrossRows() { + long heap = alloc(COMPACT); + + try { + COMPACT.writeMeshesAndSliceMask(heap, 0, 0, 0, ranges(QUADS, 1, 0, 0, 0, 0, 0, 0), QUADS); + COMPACT.writeMeshesAndSliceMask(heap, 1, 64, 0, ranges(QUADS, 0, 0, 2, 0, 0, 0, 0), QUADS); + + assertEquals(1 << 0, COMPACT.getSliceMask(heap, 0), "row 0 slice mask"); + assertEquals(1 << 2, COMPACT.getSliceMask(heap, 1), "row 1 slice mask"); + + COMPACT.clearRow(heap, 0); + + assertEquals(0, COMPACT.getSliceMask(heap, 0), "row 0 cleared"); + assertEquals(1 << 2, COMPACT.getSliceMask(heap, 1), "clearing row 0 must not disturb row 1"); + } finally { + COMPACT.freeHeap(heap); + } + } + + @Test + void clearedRowsHaveNoPopulatedFacings() { + var meshes = ranges(QUADS, 1, 2, 3, 4, 5, 6, 7); + + for (var strategy : SectionRenderDataUnsafe.Strategy.values()) { + long heap = alloc(strategy); + + try { + strategy.writeMeshesAndSliceMask(heap, 0, 64, 64, meshes, QUADS); + assertNotEquals(0, strategy.getSliceMask(heap, 0)); + + strategy.clearRow(heap, 0); + assertEquals(0, strategy.getSliceMask(heap, 0), strategy + " slice mask after clear"); + + long ptr = strategy.heapPointer(heap, 0); + for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { + assertEquals(0, strategy.getElementCount(ptr, facing, QUADS), + strategy + " element count for facing " + facing + " after clear"); + } + } finally { + strategy.freeHeap(heap); + } + } + } + + @Test + void heapRowsAreStridedAndZeroInitialized() { + for (var strategy : SectionRenderDataUnsafe.Strategy.values()) { + long heap = alloc(strategy); + + try { + long rowBase = strategy.getRowBasePointer(heap); + + for (int i = 0; i < RenderRegion.REGION_SIZE; i++) { + assertEquals(rowBase + (i * strategy.getStride()), strategy.heapPointer(heap, i), strategy + " row " + i); + assertEquals(0, LWJGL.memGetInt(strategy.heapPointer(heap, i)), strategy + " row " + i + " not zeroed"); + } + } finally { + strategy.freeHeap(heap); + } + } + } +} diff --git a/src/test/java/org/embeddedt/embeddium/impl/render/chunk/multidraw/RegionCullMaskTest.java b/src/test/java/org/embeddedt/embeddium/impl/render/chunk/multidraw/RegionCullMaskTest.java new file mode 100644 index 000000000..56ff5d933 --- /dev/null +++ b/src/test/java/org/embeddedt/embeddium/impl/render/chunk/multidraw/RegionCullMaskTest.java @@ -0,0 +1,93 @@ +package org.embeddedt.embeddium.impl.render.chunk.multidraw; + +import com.gtnewhorizons.angelica.rendering.RenderRegionKeys; +import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing; +import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; +import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class RegionCullMaskTest { + @Test + void agreeingCornersImplyAgreementThroughoutTheRegion() { + for (int cameraX = -40; cameraX <= 40; cameraX += 7) { + for (int cameraY = -40; cameraY <= 40; cameraY += 11) { + for (int cameraZ = -40; cameraZ <= 40; cameraZ += 7) { + assertCornerAgreementHolds(cameraX, cameraY, cameraZ, 0, 0, 0); + assertCornerAgreementHolds(cameraX, cameraY, cameraZ, -8, -4, -8); + } + } + } + } + + private static void assertCornerAgreementHolds(int cameraX, int cameraY, int cameraZ, int minX, int minY, int minZ) { + int maxX = minX + RenderRegion.REGION_WIDTH - 1; + int maxY = minY + RenderRegion.REGION_HEIGHT - 1; + int maxZ = minZ + RenderRegion.REGION_LENGTH - 1; + + int atMin = BatchAssembler.getVisibleFaces(cameraX, cameraY, cameraZ, minX, minY, minZ); + int atMax = BatchAssembler.getVisibleFaces(cameraX, cameraY, cameraZ, maxX, maxY, maxZ); + + if (atMin != atMax) { + return; + } + + for (int x = minX; x <= maxX; x++) { + for (int y = minY; y <= maxY; y++) { + for (int z = minZ; z <= maxZ; z++) { + assertEquals(atMin, BatchAssembler.getVisibleFaces(cameraX, cameraY, cameraZ, x, y, z), + "corners agreed on " + Integer.toBinaryString(atMin) + " but section (" + x + ", " + y + ", " + z + + ") disagreed for camera (" + cameraX + ", " + cameraY + ", " + cameraZ + ")"); + } + } + } + } + + @Test + void unassignedIsAlwaysVisibleSoPerSectionRunEmissionAlwaysTerminates() { + for (int camera = -64; camera <= 64; camera += 3) { + int mask = BatchAssembler.getVisibleFaces(camera, camera, camera, 0, 0, 0); + + assertTrue((mask & (1 << ModelQuadFacing.UNASSIGNED.ordinal())) != 0, + "UNASSIGNED must always be set; the per-section run loop relies on a non-zero mask"); + } + } + + @Test + void uniformCullMaskEqualsEverySectionMaskWhenTheCameraIsOutsideTheRegion() { + final RenderRegion region = RenderRegionKeys.create(0, 0, 0, 0); + + assertUniformMaskCoversTheRegion(region, new CameraTransform(-1000.0, -1000.0, -1000.0)); + assertUniformMaskCoversTheRegion(region, new CameraTransform(1000.0, 1000.0, 1000.0)); + } + + @Test + void uniformCullMaskReportsNonUniformWhenTheCameraIsInsideTheRegion() { + final RenderRegion region = RenderRegionKeys.create(0, 0, 0, 0); + final CameraTransform camera = new CameraTransform( + region.getCenterX(), region.getCenterY(), region.getCenterZ()); + + assertEquals(BatchAssembler.MASK_NOT_UNIFORM, BatchAssembler.uniformCullMask(region, camera)); + } + + private static void assertUniformMaskCoversTheRegion(RenderRegion region, CameraTransform camera) { + final int mask = BatchAssembler.uniformCullMask(region, camera); + + assertNotEquals(BatchAssembler.MASK_NOT_UNIFORM, mask, + "camera (" + camera.intX + ", " + camera.intY + ", " + camera.intZ + ") is outside the region"); + + final int minX = region.getChunkX(), minY = region.getChunkY(), minZ = region.getChunkZ(); + + for (int x = minX; x < minX + RenderRegion.REGION_WIDTH; x++) { + for (int y = minY; y < minY + RenderRegion.REGION_HEIGHT; y++) { + for (int z = minZ; z < minZ + RenderRegion.REGION_LENGTH; z++) { + assertEquals(mask, BatchAssembler.getVisibleFaces(camera.intX, camera.intY, camera.intZ, x, y, z), + "section (" + x + ", " + y + ", " + z + ")"); + } + } + } + } +} diff --git a/src/test/java/com/mitchej123/lwjgl/HeadlessTestLWJGLService.java b/src/test/java/org/taumc/celeritas/lwjgl/HeadlessTestLWJGLService.java similarity index 92% rename from src/test/java/com/mitchej123/lwjgl/HeadlessTestLWJGLService.java rename to src/test/java/org/taumc/celeritas/lwjgl/HeadlessTestLWJGLService.java index b1a2d6582..e55aeb129 100644 --- a/src/test/java/com/mitchej123/lwjgl/HeadlessTestLWJGLService.java +++ b/src/test/java/org/taumc/celeritas/lwjgl/HeadlessTestLWJGLService.java @@ -1,21 +1,16 @@ -package com.mitchej123.lwjgl; +package org.taumc.celeritas.lwjgl; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; -import java.io.PrintStream; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; -/** - * Headless {@link LWJGLService} for unit tests. - */ -public final class HeadlessTestLWJGLService extends LWJGLService { +public final class HeadlessTestLWJGLService implements LWJGLService { - @Override - public int getPriority() { - return -1000; + public static HeadlessTestLWJGLService create() { + return new HeadlessTestLWJGLService(); } @Override @@ -64,13 +59,30 @@ public final class HeadlessTestLWJGLService extends LWJGLService { } @Override - public void glBufferSubData(int target, long offset, ByteBuffer data) { - throw new UnsupportedOperationException("glBufferSubData requires a GL context"); + public void glBlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, + int dstY1, int mask, int filter) { + throw new UnsupportedOperationException("glBlitFramebuffer requires a GL context"); } @Override - public void glBufferSubData(int target, long offset, long size, long data) { - throw new UnsupportedOperationException("glBufferSubData requires a GL context"); + public void glDrawArrays(int mode, int first, int count) { + throw new UnsupportedOperationException("glDrawArrays requires a GL context"); + } + + @Override + public void glDrawBuffers(int[] buffers) { + throw new UnsupportedOperationException("glDrawBuffers requires a GL context"); + } + + @Override + public void glTexImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, + int type, ByteBuffer pixels) { + throw new UnsupportedOperationException("glTexImage2D requires a GL context"); + } + + @Override + public void glTexParameteri(int target, int pname, int param) { + throw new UnsupportedOperationException("glTexParameteri requires a GL context"); } @Override @@ -565,7 +577,18 @@ public final class HeadlessTestLWJGLService extends LWJGLService { @Override public long nmemAlignedAlloc(long alignment, long size) { - throw new UnsupportedOperationException("nmemAlignedAlloc is not provided by GTNHLib MemoryUtilities"); + final long align = Math.max(alignment, 8L); + final long base = MemoryUtilities.nmemAlloc(size + align + 8L); + if (base == 0L) { + return 0L; + } + long shift = align - (base % align); + if (shift < 8L) { + shift += align; + } + final long aligned = base + shift; + MemoryUtilities.memPutLong(aligned - 8L, base); + return aligned; } @Override @@ -580,7 +603,10 @@ public final class HeadlessTestLWJGLService extends LWJGLService { @Override public void nmemAlignedFree(long ptr) { - throw new UnsupportedOperationException("nmemAlignedFree is not provided by GTNHLib MemoryUtilities"); + if (ptr == 0L) { + return; + } + MemoryUtilities.nmemFree(MemoryUtilities.memGetLong(ptr - 8L)); } @Override diff --git a/src/test/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService b/src/test/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService deleted file mode 100644 index ec22b8797..000000000 --- a/src/test/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService +++ /dev/null @@ -1 +0,0 @@ -com.mitchej123.lwjgl.HeadlessTestLWJGLService diff --git a/umbra/build.gradle.kts b/umbra/build.gradle.kts index 967ac77fc..a913c9135 100644 --- a/umbra/build.gradle.kts +++ b/umbra/build.gradle.kts @@ -4,34 +4,9 @@ plugins { minecraft { extraRunJvmArguments.add("-Dumbra.dumpClass=true") + extraRunJvmArguments.add("-Dangelica.sdlgpu.enable=true") } +apply(from = "../gradle/angelica-run-common.gradle.kts") -tasks.processResources { - val projectVersion = project.version.toString() - inputs.property("version", projectVersion) - filesMatching("META-INF/rfb-plugin/*") { - expand("version" to projectVersion) - } -} - -val embedOnly: Configuration by configurations -val shadowImplementation: Configuration by configurations - -tasks.shadowJar { - dependsOn(embedOnly) - from(embedOnly.map(::zipTree)) - - duplicatesStrategy = DuplicatesStrategy.INCLUDE - mergeServiceFiles() - - minimize { - exclude(project(":glsm")) - exclude(project(":lwjgl3-backend")) - } - - relocate("com.mitchej123", "com.mitchej123") - relocate("org.embeddedt", "org.embeddedt") - relocate("com.gtnewhorizons.angelica.glsm", "com.gtnewhorizons.angelica.glsm") - relocate("com.gtnewhorizons.angelica.lwjgl3", "com.gtnewhorizons.angelica.lwjgl3") -} +apply(from = "../gradle/angelica-shadow-common.gradle.kts") diff --git a/umbra/buildSrc/build.gradle.kts b/umbra/buildSrc/build.gradle.kts index 81747e0fc..18eb6a0b3 100644 --- a/umbra/buildSrc/build.gradle.kts +++ b/umbra/buildSrc/build.gradle.kts @@ -10,4 +10,13 @@ repositories { // Re-export Gradle's generated typed catalog accessors so applied scripts (dependencies.gradle.kts) can use them. dependencies { implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) + implementation(libs.asm.build) +} + +sourceSets { + main { + kotlin { + srcDir("../../buildSrc/src/main/kotlin") + } + } } diff --git a/umbra/dependencies.gradle.kts b/umbra/dependencies.gradle.kts index a3271b785..e2f61256d 100644 --- a/umbra/dependencies.gradle.kts +++ b/umbra/dependencies.gradle.kts @@ -34,6 +34,7 @@ DependencyHandlerScope.of(dependencies).apply { shadowImplementation(project(":glsm")) { isTransitive = false } shadowImplementation(project(":lwjgl3-backend")) { isTransitive = false } + shadowImplementation(project(":sdl-gpu")) { isTransitive = false } shadowImplementation(libs.eventbus) shadowImplementation(libs.jcpp) // Apache 2.0 @@ -43,7 +44,5 @@ DependencyHandlerScope.of(dependencies).apply { shadowImplementation(libs.antlr4.runtime) embedOnly(libs.celeritas.common) { isTransitive = false } - embedOnly(libs.celeritas.lwjgl2.service) { isTransitive = false } runtimeOnlyNonPublishable(libs.celeritas.common) { isTransitive = false } - runtimeOnlyNonPublishable(libs.celeritas.lwjgl2.service) { isTransitive = false } } diff --git a/umbra/settings.gradle.kts b/umbra/settings.gradle.kts index da5e1c374..8d3edb826 100644 --- a/umbra/settings.gradle.kts +++ b/umbra/settings.gradle.kts @@ -32,3 +32,6 @@ project(":glsm").projectDir = file("../glsm") include("lwjgl3-backend") project(":lwjgl3-backend").projectDir = file("../lwjgl3-backend") + +include("sdl-gpu") +project(":sdl-gpu").projectDir = file("../sdl-gpu") diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraGLStateManagerService.java b/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraGLStateManagerService.java deleted file mode 100644 index 40bc773fa..000000000 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraGLStateManagerService.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.gtnewhorizons.umbra.client.rendering; - -import com.gtnewhorizon.gtnhlib.client.opengl.UniversalVAO; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.mitchej123.glsm.GLStateManagerService; -import org.lwjgl.BufferUtils; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL13; - -import java.nio.IntBuffer; - -public class UmbraGLStateManagerService implements GLStateManagerService { - - private IntBuffer intArrayBuffer = BufferUtils.createIntBuffer(16); - - private IntBuffer getIntArrayBuffer(int size) { - if (intArrayBuffer.capacity() < size) { - intArrayBuffer = BufferUtils.createIntBuffer(size); - } - intArrayBuffer.clear(); - return intArrayBuffer; - } - - @Override public int getPriority() { return 100; } - - @Override public int glGetInteger(int pname) { return GLStateManager.glGetInteger(pname); } - @Override public String glGetString(int pname) { return GLStateManager.glGetString(pname); } - - @Override public int glGenFramebuffers() { return GLStateManager.glGenFramebuffers(); } - @Override public void glDeleteFramebuffers(int fb) { GLStateManager.glDeleteFramebuffers(fb); } - @Override public void glBindFramebuffer(int target, int fb) { GLStateManager.glBindFramebuffer(target, fb); } - @Override public int glCheckFramebufferStatus(int target) { return GLStateManager.glCheckFramebufferStatus(target); } - @Override public void glFramebufferTexture2D(int target, int attachment, int textarget, int texture, int level) { - GLStateManager.glFramebufferTexture2D(target, attachment, textarget, texture, level); - } - - @Override public int glCreateShader(int type) { return GLStateManager.glCreateShader(type); } - @Override public void glCompileShader(int shader) { GLStateManager.glCompileShader(shader); } - @Override public int glGetShaderi(int shader, int pname) { return GLStateManager.glGetShaderi(shader, pname); } - @Override public String glGetShaderInfoLog(int shader, int maxLen) { return GLStateManager.glGetShaderInfoLog(shader, maxLen); } - @Override public void glDeleteShader(int shader) { GLStateManager.glDeleteShader(shader); } - - @Override public int glCreateProgram() { return GLStateManager.glCreateProgram(); } - @Override public void glAttachShader(int program, int shader) { GLStateManager.glAttachShader(program, shader); } - @Override public void glLinkProgram(int program) { GLStateManager.glLinkProgram(program); } - @Override public int glGetProgrami(int program, int pname) { return GLStateManager.glGetProgrami(program, pname); } - @Override public void glUseProgram(int program) { GLStateManager.glUseProgram(program); } - @Override public void glDeleteProgram(int program) { GLStateManager.glDeleteProgram(program); } - - @Override public int glGetUniformLocation(int program, CharSequence name) { return GLStateManager.glGetUniformLocation(program, name); } - @Override public void glUniform1i(int location, int value) { GLStateManager.glUniform1i(location, value); } - @Override public int glGetAttribLocation(int program, CharSequence name) { return GLStateManager.glGetAttribLocation(program, name); } - @Override public void glBindAttribLocation(int program, int index, CharSequence name) { GLStateManager.glBindAttribLocation(program, index, name); } - - @Override public int glGenVertexArrays() { return UniversalVAO.genVertexArrays(); } - @Override public void glBindVertexArray(int array) { GLStateManager.glBindVertexArray(array); } - @Override public void glDeleteVertexArrays(int array) { GLStateManager.glDeleteVertexArrays(array); } - - @Override public int glGenBuffers() { return GLStateManager.glGenBuffers(); } - @Override public void glBindBuffer(int target, int buffer) { GLStateManager.glBindBuffer(target, buffer); } - @Override public void glDeleteBuffers(int buffer) { GLStateManager.glDeleteBuffers(buffer); } - - @Override public int glGenTextures() { return GLStateManager.glGenTextures(); } - @Override public void glGenTextures(int[] textures) { - final IntBuffer buffer = getIntArrayBuffer(textures.length); - GLStateManager.glGenTextures(buffer); - buffer.get(textures); - } - @Override public void glDeleteTextures(int texture) { GLStateManager.glDeleteTextures(texture); } - @Override public void glDeleteTextures(int[] textures) { - final IntBuffer buffer = getIntArrayBuffer(textures.length); - buffer.put(textures); - buffer.flip(); - GLStateManager.glDeleteTextures(buffer); - } - @Override public void glActiveTexture(int texture) { GLStateManager.glActiveTexture(texture); } - @Override public int glGetTexLevelParameteri(int target, int level, int pname) { return GLStateManager.glGetTexLevelParameteri(target, level, pname); } - @Override public int glGetTexLevelParameter(int target, int level, int pname) { return GLStateManager.glGetTexLevelParameteri(target, level, pname); } - @Override public void glCopyTexSubImage2D(int target, int level, int xo, int yo, int x, int y, int w, int h) { - GLStateManager.glCopyTexSubImage2D(target, level, xo, yo, x, y, w, h); - } - @Override public void glPixelStorei(int pname, int param) { GLStateManager.glPixelStorei(pname, param); } - - @Override public void enableCullFace() { GLStateManager.enableCull(); } - @Override public void disableCullFace() { GLStateManager.disableCull(); } - @Override public void enableBlend() { GLStateManager.enableBlend(); } - @Override public void disableBlend() { GLStateManager.disableBlend(); } - @Override public void glBlendFuncSeparate(int srcRGB, int dstRGB, int srcA, int dstA) { GLStateManager.tryBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA); } - @Override public void enableDepthTest() { GLStateManager.enableDepthTest(); } - @Override public void disableDepthTest() { GLStateManager.disableDepthTest(); } - @Override public void glDepthFunc(int func) { GLStateManager.glDepthFunc(func); } - @Override public void glDepthMask(boolean flag) { GLStateManager.glDepthMask(flag); } - @Override public void glViewport(int x, int y, int w, int h) { GLStateManager.glViewport(x, y, w, h); } - @Override public void glColorMask(boolean r, boolean g, boolean b, boolean a) { GLStateManager.glColorMask(r, g, b, a); } - @Override public void glClearColor(float r, float g, float b, float a) { GLStateManager.glClearColor(r, g, b, a); } - @Override public void glClear(int mask) { GLStateManager.glClear(mask); } - - @Override public void clear(int mask, boolean checkError) { GLStateManager.glClear(mask); } - @Override public void bindTexture(int texture) { GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); } - @Override public int getActiveTexture() { return GLStateManager.getActiveTextureUnit() + GL13.GL_TEXTURE0; } - @Override public int getActiveTextureAccessor() { return GLStateManager.getActiveTextureUnit(); } - @Override public int getBoundTexture(int internalUnit) { return GLStateManager.getBoundTextureForServerState(internalUnit); } - @Override public int getActiveBoundTexture() { return GLStateManager.getBoundTextureForServerState(); } - @Override public int getViewportWidth() { return GLStateManager.getViewportState().width; } - @Override public int getViewportHeight() { return GLStateManager.getViewportState().height; } - @Override public boolean getDepthStateMask() { return GLStateManager.getDepthState().isEnabled(); } - @Override public boolean isBlendEnabled() { return GLStateManager.getBlendMode().isEnabled(); } - @Override public void setBoundTexture(int unit, int texture) { - final int currentUnit = GLStateManager.getActiveTextureUnit(); - if (currentUnit != unit) GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + unit); - GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); - if (currentUnit != unit) GLStateManager.glActiveTexture(GL13.GL_TEXTURE0 + currentUnit); - } - @Override public int getTextureBinding(int unit) { return GLStateManager.getBoundTextureForServerState(unit); } -} diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraRenderSystemService.java b/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraRenderSystemService.java deleted file mode 100644 index 129ea3c31..000000000 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/client/rendering/UmbraRenderSystemService.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.gtnewhorizons.umbra.client.rendering; - -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.mitchej123.glsm.RenderSystemService; -import org.joml.Matrix4f; -import org.lwjgl.BufferUtils; -import org.lwjgl.opengl.GL11; - -import java.nio.FloatBuffer; - -public class UmbraRenderSystemService implements RenderSystemService { - - private final int[] shaderTextures = new int[16]; - private final float[] shaderColor = {1f, 1f, 1f, 1f}; - private final float[] shaderFogColor = {0f, 0f, 0f, 1f}; - private float shaderFogStart = 0f; - private float shaderFogEnd = 0f; - private int fogShape = 0; - private float shaderLineWidth = 1f; - private final Matrix4f projectionMatrix = new Matrix4f(); - private final FloatBuffer projectionMatrixBuffer = BufferUtils.createFloatBuffer(16); - - @Override public int getPriority() { return 100; } - - @Override public void glActiveTexture(int texture) { GLStateManager.glActiveTexture(texture); } - @Override public void bindTexture(int texture) { GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, texture); } - - @Override public void enableCullFace() { GLStateManager.enableCull(); } - @Override public void disableCullFace() { GLStateManager.disableCull(); } - @Override public void enableBlend() { GLStateManager.enableBlend(); } - @Override public void disableBlend() { GLStateManager.disableBlend(); } - @Override public void defaultBlendFunc() { GLStateManager.defaultBlendFunc(); } - @Override public void blendFuncSeparate(int srcRGB, int dstRGB, int srcA, int dstA) { GLStateManager.tryBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA); } - @Override public void setUnknownBlendState() { } - @Override public void enableDepthTest() { GLStateManager.enableDepthTest(); } - @Override public void disableDepthTest() { GLStateManager.disableDepthTest(); } - @Override public void depthFunc(int func) { GLStateManager.glDepthFunc(func); } - @Override public void depthMask(boolean flag) { GLStateManager.glDepthMask(flag); } - @Override public void glViewport(int x, int y, int w, int h) { GLStateManager.glViewport(x, y, w, h); } - @Override public void glClearColor(float r, float g, float b, float a) { GLStateManager.glClearColor(r, g, b, a); } - @Override public void clear(int mask, boolean checkError) { GLStateManager.glClear(mask); } - - @Override public void glUniform1i(int loc, int val) { GLStateManager.glUniform1i(loc, val); } - @Override public void glUniformMatrix3(int loc, boolean transpose, FloatBuffer val) { GLStateManager.glUniformMatrix3(loc, transpose, val); } - @Override public void glUniformMatrix4(int loc, boolean transpose, FloatBuffer val) { GLStateManager.glUniformMatrix4(loc, transpose, val); } - - @Override public void assertOnRenderThread() { } - @Override public void assertOnRenderThreadOrInit() { } - - @Override public void setShaderTexture(int slot, int id) { if (slot >= 0 && slot < 16) shaderTextures[slot] = id; } - @Override public int getShaderTexture(int slot) { return (slot >= 0 && slot < 16) ? shaderTextures[slot] : 0; } - @Override public void setShaderColor(float r, float g, float b, float a) { shaderColor[0]=r; shaderColor[1]=g; shaderColor[2]=b; shaderColor[3]=a; } - @Override public float[] getShaderColor() { return shaderColor.clone(); } - @Override public void setShaderLineWidth(float w) { shaderLineWidth = w; } - @Override public float getShaderLineWidth() { return shaderLineWidth; } - - @Override public void setShaderFogColor(float r, float g, float b, float a) { shaderFogColor[0]=r; shaderFogColor[1]=g; shaderFogColor[2]=b; shaderFogColor[3]=a; } - @Override public float[] getShaderFogColor() { return shaderFogColor.clone(); } - @Override public void setShaderFogStart(float start) { shaderFogStart = start; } - @Override public float getShaderFogStart() { return shaderFogStart; } - @Override public void setShaderFogEnd(float end) { shaderFogEnd = end; } - @Override public float getShaderFogEnd() { return shaderFogEnd; } - @Override public void setFogShape(int shape) { fogShape = shape; } - @Override public int getFogShape() { return fogShape; } - - @Override public Matrix4f getProjectionMatrix() { return new Matrix4f(projectionMatrix); } - @Override public void setProjectionMatrixOrth(Matrix4f m) { - projectionMatrix.set(m); - GLStateManager.glMatrixMode(GL11.GL_PROJECTION); - projectionMatrix.get(projectionMatrixBuffer); - projectionMatrixBuffer.rewind(); - GLStateManager.glLoadMatrix(projectionMatrixBuffer); - } - @Override public void setProjectionMatrixOrigin(Matrix4f m) { setProjectionMatrixOrth(m); } -} diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/UmbraClientTweaker.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/UmbraClientTweaker.java index 887e68b3e..92f7dde6d 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/UmbraClientTweaker.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/UmbraClientTweaker.java @@ -2,14 +2,18 @@ package com.gtnewhorizons.umbra.loading; import com.gtnewhorizon.gtnhmixins.IEarlyMixinLoader; import com.gtnewhorizon.gtnhmixins.builders.IMixins; +import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.loading.DependencyVerifier; -import com.gtnewhorizons.angelica.glsm.loading.EcosystemNarrowRules; -import com.gtnewhorizons.retrofuturabootstrap.SharedConfig; +import com.gtnewhorizons.angelica.glsm.loading.Lwjgl3ifyExclusions; +import com.gtnewhorizons.angelica.lwjgl3.MissingDependencySdl; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; import com.gtnewhorizons.umbra.loading.shared.AngelicaDetector; import com.gtnewhorizons.umbra.mixins.Mixins; import cpw.mods.fml.relauncher.FMLRelaunchLog; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; import net.minecraft.launchwrapper.Launch; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.spongepowered.asm.service.mojang.MixinServiceLaunchWrapper; import java.util.Collections; @@ -23,6 +27,7 @@ import java.util.Set; */ public class UmbraClientTweaker implements IFMLLoadingPlugin, IEarlyMixinLoader { + public static final Logger LOGGER = LogManager.getLogger("Umbra"); private static Boolean OBF_ENV; private final boolean disabled; @@ -42,10 +47,20 @@ public class UmbraClientTweaker implements IFMLLoadingPlugin, IEarlyMixinLoader disabled = false; if (Boolean.TRUE.equals(Launch.blackboard.get("umbra.rfbPluginLoaded"))) { - addLwjgl3ifyExclusions(); + Lwjgl3ifyExclusions.apply(); } - verifyDependencies(); + DependencyVerifier.verifyOrHalt(UmbraClientTweaker.class, "Umbra", + DependencyVerifier.gtnhLibChecks("Umbra"), LOGGER, + (title, message) -> MissingDependencySdl.showFatal(title, message)); + + if (SystemProperties.USE_SDL_GPU) { + if (SDLGPUGate.isSDLGPUAvailable()) { + LOGGER.info("SDL GPU window mode enabled"); + } else { + LOGGER.warn("angelica.sdlgpu.enable=true but SDL GPU dependencies not available, falling back to GL"); + } + } // Register early redirector if RFB is not loaded (RFB handles its own registration) if (!Boolean.TRUE.equals(Launch.blackboard.get("umbra.rfbPluginLoaded"))) { @@ -55,29 +70,6 @@ public class UmbraClientTweaker implements IFMLLoadingPlugin, IEarlyMixinLoader } } - private static void addLwjgl3ifyExclusions() { - final var handle = SharedConfig.getRfbTransformers().stream() - .filter(transformer -> transformer.id().equals("lwjgl3ify:redirect")) - .findFirst() - .orElse(null); - if (handle != null) { - for (String exclusion : EcosystemNarrowRules.LWJGL3IFY_EXCLUSIONS_SHARED) { - handle.exclusions().add(exclusion); - } - } - } - - private static void verifyDependencies() { - DependencyVerifier.verify(UmbraClientTweaker.class, List.of( - new DependencyVerifier.Check( - "/it/unimi/dsi/fastutil/ints/Int2ObjectMap.class", - "Missing dependency: Umbra requires GTNHLib! Download: https://modrinth.com/mod/gtnhlib"), - new DependencyVerifier.Check( - "/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.class", - "GTNHLib is outdated: Angelica requires GTNHLib 0.10.0 or newer! Download: https://modrinth.com/mod/gtnhlib") - )); - } - @Override public String[] getASMTransformerClass() { if (disabled) return new String[0]; diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/EarlyRedirectorTransformer.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/EarlyRedirectorTransformer.java index 3fde7cb93..2935a337a 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/EarlyRedirectorTransformer.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/EarlyRedirectorTransformer.java @@ -18,12 +18,10 @@ import net.minecraft.launchwrapper.IClassTransformer; */ public class EarlyRedirectorTransformer implements IClassTransformer { - private final EarlyRedirectorCore impl = new EarlyRedirectorCore( - "com.gtnewhorizons.umbra.loading" - ); + private final EarlyRedirectorCore impl = new EarlyRedirectorCore("com.gtnewhorizons.umbra.loading"); @Override public byte[] transform(String name, String transformedName, byte[] basicClass) { - return impl.transform(transformedName, basicClass); + return impl.transform(transformedName, basicClass, this); } } diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/UmbraRedirectorTransformer.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/UmbraRedirectorTransformer.java index b64258d4a..aa7093db3 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/UmbraRedirectorTransformer.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/fml/transformers/UmbraRedirectorTransformer.java @@ -1,46 +1,15 @@ package com.gtnewhorizons.umbra.loading.fml.transformers; -import com.gtnewhorizons.umbra.loading.shared.UmbraClassDump; +import com.gtnewhorizons.angelica.glsm.loading.ModRedirector; import com.gtnewhorizons.umbra.loading.shared.transformers.UmbraRedirector; import net.minecraft.launchwrapper.IClassTransformer; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; -/** IClassTransformer wrapper for {@link UmbraRedirector} */ public class UmbraRedirectorTransformer implements IClassTransformer { - private final UmbraRedirector inner; - private final String[] exclusions; - - public UmbraRedirectorTransformer() { - inner = new UmbraRedirector(); - exclusions = inner.getTransformerExclusions(); - } + private final ModRedirector inner = UmbraRedirector.create(); @Override public byte[] transform(final String className, String transformedName, byte[] basicClass) { - if (basicClass == null) return null; - - for (String exclusion : exclusions) { - if (transformedName.startsWith(exclusion)) return basicClass; - } - - if (!inner.shouldTransform(basicClass)) { - return basicClass; - } - - final ClassReader cr = new ClassReader(basicClass); - final ClassNode cn = new ClassNode(); - cr.accept(cn, 0); - final boolean changed = inner.transformClassNode(transformedName, cn); - if (changed) { - final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cn.accept(cw); - final byte[] bytes = cw.toByteArray(); - UmbraClassDump.dumpClass(transformedName, basicClass, bytes, this); - return bytes; - } - return basicClass; + return inner.transform(transformedName, basicClass, this); } } diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/rfb/transformers/RFBUmbraRedirector.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/rfb/transformers/RFBUmbraRedirector.java index 42aabf6a1..ef88e1490 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/rfb/transformers/RFBUmbraRedirector.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/rfb/transformers/RFBUmbraRedirector.java @@ -1,70 +1,11 @@ package com.gtnewhorizons.umbra.loading.rfb.transformers; -import com.gtnewhorizons.umbra.loading.shared.UmbraClassDump; +import com.gtnewhorizons.angelica.glsm.loading.RfbModRedirector; import com.gtnewhorizons.umbra.loading.shared.transformers.UmbraRedirector; -import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; -import com.gtnewhorizons.retrofuturabootstrap.api.ExtensibleClassLoader; -import com.gtnewhorizons.retrofuturabootstrap.api.RfbClassTransformer; -import org.intellij.lang.annotations.Pattern; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.jar.Manifest; - -/** RfbClassTransformer wrapper for {@link UmbraRedirector} */ -public class RFBUmbraRedirector implements RfbClassTransformer { - - private final UmbraRedirector inner; +public class RFBUmbraRedirector extends RfbModRedirector { public RFBUmbraRedirector() { - inner = new UmbraRedirector(); - } - - @Pattern("[a-z0-9-]+") - @Override - public @NotNull String id() { - return "umbra-redirector"; - } - - @Override - public @NotNull String @Nullable [] sortAfter() { - return new String[] {"*", "mixin:mixin"}; - } - - @Override - public @NotNull String @Nullable [] sortBefore() { - return new String[] {"lwjgl3ify:redirect"}; - } - - @Override - public @NotNull String @Nullable [] additionalExclusions() { - return inner.getTransformerExclusions(); - } - - @Override - public boolean shouldTransformClass(@NotNull ExtensibleClassLoader classLoader, - @NotNull RfbClassTransformer.Context context, @Nullable Manifest manifest, @NotNull String className, - @NotNull ClassNodeHandle classNode) { - if (!classNode.isPresent()) { - return false; - } - return inner.shouldTransform(classNode.getOriginalBytes()); - } - - @Override - public boolean transformClassIfNeeded(@NotNull ExtensibleClassLoader classLoader, @NotNull RfbClassTransformer.Context context, - @Nullable Manifest manifest, @NotNull String className, @NotNull ClassNodeHandle classNode) { - final boolean changed = inner.transformClassNode(className, classNode.getNode()); - if (changed) { - classNode.computeMaxs(); - UmbraClassDump.dumpRFBClass(className, classNode, this); - } - return changed; - } - - @Override - public void transformClass(@NotNull ExtensibleClassLoader classLoader, @NotNull RfbClassTransformer.Context context, - @Nullable Manifest manifest, @NotNull String className, @NotNull ClassNodeHandle classNode) { - transformClassIfNeeded(classLoader, context, manifest, className, classNode); + super("umbra-redirector", UmbraRedirector.create()); } } diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/UmbraClassDump.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/UmbraClassDump.java deleted file mode 100644 index 0df5a1962..000000000 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/UmbraClassDump.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.gtnewhorizons.umbra.loading.shared; - -import com.gtnewhorizon.gtnhlib.asm.ASMUtil; -import com.gtnewhorizons.retrofuturabootstrap.api.ClassNodeHandle; - -public class UmbraClassDump { - - private static final boolean DUMP_CLASS = Boolean.getBoolean("umbra.dumpClass"); - - public static void dumpClass(String className, byte[] originalBytes, byte[] transformedBytes, Object transformer) { - if (DUMP_CLASS) { - ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); - ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); - } - } - - public static void dumpRFBClass(String className, ClassNodeHandle classNode, Object transformer) { - if (DUMP_CLASS) { - final byte[] originalBytes = classNode.getOriginalBytes(); - final byte[] transformedBytes = classNode.computeBytes(); - ASMUtil.saveAsRawClassFile(originalBytes, className + "_PRE", transformer); - ASMUtil.saveAsRawClassFile(transformedBytes, className + "_POST", transformer); - } - } -} diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/transformers/UmbraRedirector.java b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/transformers/UmbraRedirector.java index 4ad9fa4e3..e1f10de33 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/transformers/UmbraRedirector.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/loading/shared/transformers/UmbraRedirector.java @@ -1,41 +1,15 @@ package com.gtnewhorizons.umbra.loading.shared.transformers; -import com.gtnewhorizons.angelica.glsm.redirect.GLSMRedirector; -import org.objectweb.asm.tree.ClassNode; +import com.gtnewhorizons.angelica.glsm.loading.ClassDump; +import com.gtnewhorizons.angelica.glsm.loading.ModRedirector; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Mod-level wrapper around {@link GLSMRedirector}. Adds Umbra-specific transformer exclusions - * and delegates all redirect logic to GLSM. - *

- * THIS CLASS MIGHT BE LOADED ON A DIFFERENT CLASS LOADER, - * IT SHOULD NOT CALL ANY CODE FROM THE MAIN MOD - */ public final class UmbraRedirector { - private final GLSMRedirector core = new GLSMRedirector(); - private final String[] allExclusions; + private static final ClassDump DUMP = new ClassDump(Boolean.getBoolean("umbra.dumpClass")); - public UmbraRedirector() { - final List exclusions = new ArrayList<>(Arrays.asList(core.getCoreExclusions())); - exclusions.add("com.gtnewhorizons.umbra.loading"); - exclusions.add("com.gtnewhorizons.angelica.lwjgl3."); - allExclusions = exclusions.toArray(new String[0]); + public static ModRedirector create() { + return new ModRedirector(DUMP, "com.gtnewhorizons.umbra.loading"); } - public String[] getTransformerExclusions() { - return allExclusions; - } - - public boolean shouldTransform(byte[] basicClass) { - return core.shouldTransform(basicClass); - } - - /** @return Was the class changed? */ - public boolean transformClassNode(String transformedName, ClassNode cn) { - return core.transformClassNode(transformedName, cn); - } + private UmbraRedirector() {} } diff --git a/umbra/src/main/java/com/gtnewhorizons/umbra/mixins/Mixins.java b/umbra/src/main/java/com/gtnewhorizons/umbra/mixins/Mixins.java index be3818f4a..d160d6255 100644 --- a/umbra/src/main/java/com/gtnewhorizons/umbra/mixins/Mixins.java +++ b/umbra/src/main/java/com/gtnewhorizons/umbra/mixins/Mixins.java @@ -2,6 +2,8 @@ package com.gtnewhorizons.umbra.mixins; import com.gtnewhorizon.gtnhmixins.builders.IMixins; import com.gtnewhorizon.gtnhmixins.builders.MixinBuilder; +import com.gtnewhorizons.angelica.config.SystemProperties; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -27,9 +29,22 @@ public enum Mixins implements IMixins { .setPhase(Phase.EARLY) .addClientMixins( "early.glsm.MixinForgeHooksClient_CoreProfile", + "early.glsm.MixinGameSettings_VSync", "early.glsm.MixinSplashProgressCaching", "early.glsm.MixinMinecraft_FrameHook", - "early.glsm.MixinWorldRenderer_VertexState" + "early.glsm.MixinMinecraft_IconifyGuard", + "early.glsm.MixinMinecraft_VSync", + "early.glsm.MixinWorldRenderer_VertexState", + "early.textures.MixinTextureUtil" + ) + ), + + UMBRA_SDL_GPU_DISPLAY(new MixinBuilder("SDL-GPU-aware Display.create path") + .setPhase(Phase.EARLY) + .setApplyIf(() -> SystemProperties.USE_SDL_GPU && SDLGPUGate.isSDLGPUAvailable()) + .addClientMixins( + "early.sdlgpu.MixinForgeHooksClient_SDLGPUDisplay", + "early.sdlgpu.MixinMinecraft_SDLGPUIcons" ) ), diff --git a/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService b/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService deleted file mode 100644 index 3c34b00ce..000000000 --- a/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.GLStateManagerService +++ /dev/null @@ -1 +0,0 @@ -com.gtnewhorizons.umbra.client.rendering.UmbraGLStateManagerService diff --git a/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService b/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService deleted file mode 100644 index c34ad6aec..000000000 --- a/umbra/src/main/resources/META-INF/services/com.mitchej123.glsm.RenderSystemService +++ /dev/null @@ -1 +0,0 @@ -com.gtnewhorizons.umbra.client.rendering.UmbraRenderSystemService diff --git a/umbra/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService b/umbra/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService deleted file mode 100644 index f1903bf7c..000000000 --- a/umbra/src/main/resources/META-INF/services/com.mitchej123.lwjgl.LWJGLService +++ /dev/null @@ -1,2 +0,0 @@ -com.mitchej123.lwjgl.lwjgl3.LWJGL3Service -com.mitchej123.lwjgl.lwjgl2.LWJGL2Service diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/ffp/MixinTessellator_CoreProfile.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/ffp/MixinTessellator_CoreProfile.java index f25022eac..b22914119 100644 --- a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/ffp/MixinTessellator_CoreProfile.java +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/ffp/MixinTessellator_CoreProfile.java @@ -2,8 +2,8 @@ package com.gtnewhorizons.umbra.mixins.early.ffp; import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import net.minecraft.client.renderer.Tessellator; +import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -11,14 +11,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Tessellator.class) public class MixinTessellator_CoreProfile { - @Shadow public void reset() {} - - public void angelica$reset() { - reset(); - } - - @Inject(method = "draw", at = @At("HEAD"), cancellable = true) + @Inject(method = "draw", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/Tessellator;isDrawing:Z", opcode = Opcodes.GETFIELD), cancellable = true) private void umbra$coreProfileDraw(CallbackInfoReturnable cir) { - cir.setReturnValue(TessellatorStreamingDrawer.draw((Tessellator)(Object)this)); + cir.setReturnValue(TessellatorStreamingDrawer.draw((Tessellator) (Object) this)); } } diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinGameSettings_VSync.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinGameSettings_VSync.java new file mode 100644 index 000000000..bdd88fc26 --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinGameSettings_VSync.java @@ -0,0 +1,16 @@ +package com.gtnewhorizons.umbra.mixins.early.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.settings.GameSettings; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(GameSettings.class) +public class MixinGameSettings_VSync { + + @Redirect(method = "setOptionValue", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/Display;setVSyncEnabled(Z)V", remap = false)) + private void umbra$redirectVSync(boolean sync) { + GLStateManager.setVSyncEnabled(sync); + } +} diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_FrameHook.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_FrameHook.java index 657eb53b1..58192ecf8 100644 --- a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_FrameHook.java +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_FrameHook.java @@ -1,20 +1,55 @@ package com.gtnewhorizons.umbra.mixins.early.glsm; +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.ffp.ShaderManager; +import com.gtnewhorizons.angelica.glsm.hooks.FrameHooks; import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; import net.minecraft.client.Minecraft; +import org.lwjgl.opengl.GL11; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -/** - * Per-frame hook to flush streaming buffer fences. - */ @Mixin(Minecraft.class) public class MixinMinecraft_FrameHook { @Inject(method = "runGameLoop", at = @At("HEAD")) - private void umbra$onFrameStart(CallbackInfo ci) { + private void umbra$bootstrapFirstFrame(CallbackInfo ci) { + FrameHooks.bootstrapFirstFrame(); + } + + @Inject(method = "func_147120_f", at = @At("HEAD")) + private void umbra$onFrameEnd(CallbackInfo ci) { + FrameHooks.frameEnd(); + } + + @Inject(method = "func_147120_f", at = @At("RETURN")) + private void umbra$onFrameBegin(CallbackInfo ci) { + FrameHooks.frameBegin(); + } + + @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/EntityRenderer;updateCameraAndRender(F)V")) + private void umbra$freshenMouseInput(CallbackInfo ci) { + GLStateManager.pumpDisplayMessages(); + } + + @Inject( + method = "runGameLoop", + at = @At(value = "INVOKE", target = "Lcpw/mods/fml/common/FMLCommonHandler;onRenderTickEnd(F)V", shift = At.Shift.AFTER, remap = false) + ) + private void umbra$injectLightingFixPostRenderTick(CallbackInfo ci) { + GLStateManager.glEnable(GL11.GL_LIGHTING); + } + + @Inject(method = "runGameLoop", at = @At("RETURN")) + private void umbra$streamingBufferEndFrame(CallbackInfo ci) { TessellatorStreamingDrawer.endFrame(); + ShaderManager.endFrame(); + } + + @Inject(method = "shutdownMinecraftApplet", at = @At("HEAD")) + private void umbra$onShutdown(CallbackInfo ci) { + FrameHooks.shutdown(); } } diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_IconifyGuard.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_IconifyGuard.java new file mode 100644 index 000000000..710ce0131 --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_IconifyGuard.java @@ -0,0 +1,24 @@ +package com.gtnewhorizons.umbra.mixins.early.glsm; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.shader.Framebuffer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(Minecraft.class) +public class MixinMinecraft_IconifyGuard { + + @Redirect( + method = "runGameLoop", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/EntityRenderer;updateCameraAndRender(F)V") + ) + private void umbra$skipRenderWhenIconified(EntityRenderer entityRenderer, float partialTicks) { + final Framebuffer fb = Minecraft.getMinecraft().getFramebuffer(); + if (fb == null || fb.framebufferWidth < 16 || fb.framebufferHeight < 16) { + return; + } + entityRenderer.updateCameraAndRender(partialTicks); + } +} diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_VSync.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_VSync.java new file mode 100644 index 000000000..28b07ce0f --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinMinecraft_VSync.java @@ -0,0 +1,16 @@ +package com.gtnewhorizons.umbra.mixins.early.glsm; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import net.minecraft.client.Minecraft; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(Minecraft.class) +public class MixinMinecraft_VSync { + + @Redirect(method = {"startGame", "toggleFullscreen"}, at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/Display;setVSyncEnabled(Z)V", remap = false)) + private void umbra$redirectVSync(boolean sync) { + GLStateManager.setVSyncEnabled(sync); + } +} diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinSplashProgressCaching.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinSplashProgressCaching.java index 78060da04..f6e916562 100644 --- a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinSplashProgressCaching.java +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/glsm/MixinSplashProgressCaching.java @@ -1,8 +1,6 @@ package com.gtnewhorizons.umbra.mixins.early.glsm; -import com.gtnewhorizons.angelica.glsm.GLStateManager; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import com.gtnewhorizons.angelica.glsm.hooks.FrameHooks; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -11,11 +9,14 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @SuppressWarnings("deprecation") @Mixin(value = cpw.mods.fml.client.SplashProgress.class, remap = false) public class MixinSplashProgressCaching { - private static final Logger LOGGER = LogManager.getLogger("Umbra"); + + @Inject(method = "start", at = @At("TAIL")) + private static void umbra$bindSharedDrawableVAO(CallbackInfo ci) { + FrameHooks.bindSplashVao(); + } @Inject(method = "finish", at = @At("RETURN")) private static void umbra$enableCachingOnFinish(CallbackInfo ci) { - GLStateManager.markSplashComplete("SplashProgress.finish"); - LOGGER.info("Splash Complete"); + FrameHooks.splashFinished(); } } diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinForgeHooksClient_SDLGPUDisplay.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinForgeHooksClient_SDLGPUDisplay.java new file mode 100644 index 000000000..c63843648 --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinForgeHooksClient_SDLGPUDisplay.java @@ -0,0 +1,45 @@ +package com.gtnewhorizons.umbra.mixins.early.sdlgpu; + +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; +import net.minecraftforge.client.ForgeHooksClient; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.lwjgl.LWJGLException; +import org.lwjgl.opengl.PixelFormat; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import javax.imageio.ImageIO; + +@Mixin(value = ForgeHooksClient.class, remap = false) +public abstract class MixinForgeHooksClient_SDLGPUDisplay { + + @Unique private static final Logger LOGGER = LogManager.getLogger("Umbra/SDLGPU"); + + @Shadow static int stencilBits; + + @Inject(method = "createDisplay", at = @At("HEAD"), cancellable = true) + private static void umbraSdl$createDisplay(CallbackInfo ci) throws LWJGLException { + if (!SDLGPUGate.isDeviceReady()) { + LOGGER.warn("No usable SDL GPU device; creating an OpenGL window instead"); + return; + } + + ImageIO.setUseCache(false); + stencilBits = 8; + final PixelFormat format = new PixelFormat().withDepthBits(24).withStencilBits(stencilBits); + + if (SDLGPUGate.createSDLGPUDisplay(format, null)) { + LOGGER.info("Created SDL GPU window"); + ci.cancel(); + return; + } + + LOGGER.warn("SDL GPU could not take the window; falling back to OpenGL"); + SDLGPUGate.fallBackToGL(); + } +} diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinMinecraft_SDLGPUIcons.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinMinecraft_SDLGPUIcons.java new file mode 100644 index 000000000..56f8fa570 --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/sdlgpu/MixinMinecraft_SDLGPUIcons.java @@ -0,0 +1,19 @@ +package com.gtnewhorizons.umbra.mixins.early.sdlgpu; + +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; +import net.minecraft.client.Minecraft; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +import java.nio.ByteBuffer; + +@Mixin(Minecraft.class) +public abstract class MixinMinecraft_SDLGPUIcons { + + @ModifyArg(method = "startGame", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/Display;setIcon([Ljava/nio/ByteBuffer;)I", remap = false), index = 0) + private ByteBuffer[] umbraSdl$rememberIcons(ByteBuffer[] icons) { + SDLGPUGate.rememberIcons(icons); + return icons; + } +} diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/startup/MixinInitGLStateManager.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/startup/MixinInitGLStateManager.java index 8207ab25c..6eaa3e78f 100644 --- a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/startup/MixinInitGLStateManager.java +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/startup/MixinInitGLStateManager.java @@ -3,8 +3,7 @@ package com.gtnewhorizons.umbra.mixins.early.startup; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.glsm.hooks.GLSMHooks; import com.gtnewhorizons.angelica.glsm.hooks.GLSMInitConfig; -import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer; -import net.minecraft.client.Minecraft; +import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate; import net.minecraft.client.renderer.OpenGlHelper; import org.lwjgl.opengl.Display; import org.spongepowered.asm.mixin.Mixin; @@ -15,15 +14,15 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(value = OpenGlHelper.class, priority = 100) public class MixinInitGLStateManager { + @Inject(method = "initializeTextures", at = @At("HEAD")) + private static void umbra$installSDLDrawable(CallbackInfo ci) { + SDLGPUGate.ensureDrawableInstalled(); + } + @Inject(method = "initializeTextures", at = @At("RETURN")) private static void umbra$initializeGLStateManager(CallbackInfo ci) { - final Minecraft mc = Minecraft.getMinecraft(); GLStateManager.setDrawableGL(Display.getDrawable()); - GLStateManager.initialize(GLSMInitConfig.builder() - .displaySize(mc.displayWidth, mc.displayHeight) - .directDrawer(TessellatorStreamingDrawer::drawDirect) - .streamingDrawerDestroy(TessellatorStreamingDrawer::destroy) - .build()); + GLStateManager.initialize(GLSMInitConfig.builder().build()); GLSMHooks.LIGHTMAP_COORDS.addListener(event -> { OpenGlHelper.lastBrightnessX = event.x; diff --git a/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/textures/MixinTextureUtil.java b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/textures/MixinTextureUtil.java new file mode 100644 index 000000000..5dec7141d --- /dev/null +++ b/umbra/src/mixin/java/com/gtnewhorizons/umbra/mixins/early/textures/MixinTextureUtil.java @@ -0,0 +1,42 @@ +package com.gtnewhorizons.umbra.mixins.early.textures; + +import com.gtnewhorizons.angelica.glsm.hooks.TextureUtilHooks; +import net.minecraft.client.renderer.texture.TextureUtil; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(TextureUtil.class) +public class MixinTextureUtil { + + @Redirect(method = "allocateTextureImpl", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;deleteTexture(I)V")) + private static void umbra$dontDeleteTexture(int textureId) { + } + + @Inject(method = "allocateTextureImpl", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;bindTexture(I)V", shift = At.Shift.AFTER)) + private static void umbra$setMaxLevel(int textureId, int mipmapLevels, int width, int height, float anisotropicFiltering, CallbackInfo ci) { + TextureUtilHooks.setMaxLevel(mipmapLevels); + } + + @Inject(method = "func_152777_a(ZZF)V", at = @At("HEAD")) + private static void umbra$captureSaveBoundId(boolean blurred, boolean mipmap, float aniso, CallbackInfo ci) { + TextureUtilHooks.captureBoundFilterTexture(); + } + + @Redirect(method = "func_147945_b()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;func_147952_b(II)V")) + private static void umbra$restoreFilterOnSavedTexture(int min, int mag) { + TextureUtilHooks.restoreFilter(min, mag); + } + + @Redirect(method = "func_147945_b()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureUtil;func_152778_a(F)V")) + private static void umbra$restoreAnisoOnSavedTexture(float aniso) { + TextureUtilHooks.restoreAniso(aniso); + } + + @Inject(method = "func_147945_b()V", at = @At("RETURN")) + private static void umbra$clearSavedAfterRestore(CallbackInfo ci) { + TextureUtilHooks.clearSavedFilterTexture(); + } +}