From c3975f2640dc9755a1308ee9d7e0ca5ef447d3d5 Mon Sep 17 00:00:00 2001 From: Pixelflinger Date: Mon, 21 Sep 2020 15:03:26 -0700 Subject: [PATCH] APIs to enable/disable screen space refraction Just like with shadows, this is a toggle to entirely disable screen space refraction. Added missing getters, and minor refactoring. --- .../filament-android/src/main/cpp/View.cpp | 27 +++- .../com/google/android/filament/View.java | 40 +++++- filament/include/filament/View.h | 122 +++++++++++------- filament/src/Renderer.cpp | 14 +- filament/src/View.cpp | 18 ++- filament/src/details/View.h | 9 +- libs/filagui/src/ImGuiHelper.cpp | 2 +- libs/filamentapp/src/FilamentApp.cpp | 6 +- 8 files changed, 171 insertions(+), 67 deletions(-) diff --git a/android/filament-android/src/main/cpp/View.cpp b/android/filament-android/src/main/cpp/View.cpp index 4c773d7d5b..40a4aa9592 100644 --- a/android/filament-android/src/main/cpp/View.cpp +++ b/android/filament-android/src/main/cpp/View.cpp @@ -67,9 +67,9 @@ Java_com_google_android_filament_View_nSetVisibleLayers(JNIEnv*, jclass, jlong n } extern "C" JNIEXPORT void JNICALL -Java_com_google_android_filament_View_nSetShadowsEnabled(JNIEnv*, jclass, jlong nativeView, jboolean enabled) { +Java_com_google_android_filament_View_nSetShadowingEnabled(JNIEnv*, jclass, jlong nativeView, jboolean enabled) { View* view = (View*) nativeView; - view->setShadowsEnabled(enabled); + view->setShadowingEnabled(enabled); } extern "C" JNIEXPORT void JNICALL @@ -286,3 +286,26 @@ Java_com_google_android_filament_View_nSetTemporalAntiAliasingOptions(JNIEnv *, view->setTemporalAntiAliasingOptions({ .filterWidth = filterWidth, .feedback = feedback, .enabled = (bool) enabled}); } + +extern "C" +JNIEXPORT jboolean JNICALL +Java_com_google_android_filament_View_nIsShadowingEnabled(JNIEnv *, jclass, jlong nativeView) { + View* view = (View*) nativeView; + return (jboolean)view->isShadowingEnabled(); +} + +extern "C" +JNIEXPORT void JNICALL +Java_com_google_android_filament_View_nSetScreenSpaceRefractionEnabled(JNIEnv *, jclass, + jlong nativeView, jboolean enabled) { + View* view = (View*) nativeView; + view->setScreenSpaceRefractionEnabled((bool)enabled); +} + +extern "C" +JNIEXPORT jboolean JNICALL +Java_com_google_android_filament_View_nIsScreenSpaceRefractionEnabled(JNIEnv *, jclass, + jlong nativeView) { + View* view = (View*) nativeView; + return (jboolean)view->isScreenSpaceRefractionEnabled(); +} diff --git a/android/filament-android/src/main/java/com/google/android/filament/View.java b/android/filament-android/src/main/java/com/google/android/filament/View.java index c8cecfe897..92bdf76a54 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/View.java +++ b/android/filament-android/src/main/java/com/google/android/filament/View.java @@ -723,8 +723,41 @@ public class View { * @see RenderableManager.Builder#receiveShadows * @see RenderableManager.Builder#castShadows */ + public void setShadowingEnabled(boolean enabled) { + nSetShadowingEnabled(getNativeObject(), enabled); + } + + /** + * Enables or disables shadow mapping. Enabled by default. + * + * @deprecated Use {@link #setShadowingEnabled} + */ + @Deprecated public void setShadowsEnabled(boolean enabled) { - nSetShadowsEnabled(getNativeObject(), enabled); + setShadowingEnabled(enabled); + } + + /** + * @return whether shadowing is enabled + */ + boolean isShadowingEnabled() { + return nIsShadowingEnabled(getNativeObject()); + } + + /** + * Enables or disables screen space refraction. Enabled by default. + * + * @param enabled true enables screen space refraction, false disables it. + */ + public void setScreenSpaceRefractionEnabled(boolean enabled) { + nSetScreenSpaceRefractionEnabled(getNativeObject(), enabled); + } + + /** + * @return whether screen space refraction is enabled + */ + boolean isScreenSpaceRefractionEnabled() { + return nIsScreenSpaceRefractionEnabled(getNativeObject()); } /** @@ -1223,7 +1256,7 @@ public class View { private static native void nSetCamera(long nativeView, long nativeCamera); private static native void nSetViewport(long nativeView, int left, int bottom, int width, int height); private static native void nSetVisibleLayers(long nativeView, int select, int value); - private static native void nSetShadowsEnabled(long nativeView, boolean enabled); + private static native void nSetShadowingEnabled(long nativeView, boolean enabled); private static native void nSetRenderTarget(long nativeView, long nativeRenderTarget); private static native void nSetSampleCount(long nativeView, int count); private static native int nGetSampleCount(long nativeView); @@ -1248,4 +1281,7 @@ public class View { private static native void nSetDepthOfFieldOptions(long nativeView, float focusDistance, float cocScale, float maxApertureDiameter, boolean enabled); private static native void nSetVignetteOptions(long nativeView, float midPoint, float roundness, float feather, float r, float g, float b, float a, boolean enabled); private static native void nSetTemporalAntiAliasingOptions(long nativeView, float feedback, float filterWidth, boolean enabled); + private static native boolean nIsShadowingEnabled(long nativeView); + private static native void nSetScreenSpaceRefractionEnabled(long nativeView, boolean enabled); + private static native boolean nIsScreenSpaceRefractionEnabled(long nativeView); } diff --git a/filament/include/filament/View.h b/filament/include/filament/View.h index 9e70e40616..ff884266b4 100644 --- a/filament/include/filament/View.h +++ b/filament/include/filament/View.h @@ -262,20 +262,6 @@ public: VSM //!< variance shadows }; - /** - * Sets ambient occlusion options. - * - * @param options Options for ambient occlusion. - */ - void setAmbientOcclusionOptions(AmbientOcclusionOptions const& options) noexcept; - - /** - * Gets the ambient occlusion options. - * - * @return ambient occlusion options currently set. - */ - AmbientOcclusionOptions const& getAmbientOcclusionOptions() const noexcept; - /** * Sets the View's name. Only useful for debugging. * @param name Pointer to the View's name. The string is copied. @@ -321,6 +307,45 @@ public: return const_cast(this)->getScene(); } + /** + * Specifies an offscreen render target to render into. + * + * By default, the view's associated render target is nullptr, which corresponds to the + * SwapChain associated with the engine. + * + * A view with a custom render target cannot rely on Renderer::ClearOptions, which only apply + * to the SwapChain. Such view can use a Skybox instead. + * + * @param renderTarget Render target associated with view, or nullptr for the swap chain. + */ + void setRenderTarget(RenderTarget* renderTarget) noexcept; + + /** + * Gets the offscreen render target associated with this view. + * + * Returns nullptr if the render target is the swap chain (which is default). + * + * @see setRenderTarget + */ + RenderTarget* getRenderTarget() const noexcept; + + /** + * Sets the rectangular region to render to. + * + * The viewport specifies where the content of the View (i.e. the Scene) is rendered in + * the render target. The Render target is automatically clipped to the Viewport. + * + * @param viewport The Viewport to render the Scene into. The Viewport is a value-type, it is + * therefore copied. The parameter can be discarded after this call returns. + */ + void setViewport(Viewport const& viewport) noexcept; + + /** + * Returns the rectangular region that gets rendered to. + * @return A constant reference to View's viewport. + */ + Viewport const& getViewport() const noexcept; + /** * Sets this View's Camera. * @@ -351,23 +376,6 @@ public: return const_cast(this)->getCamera(); } - /** - * Sets the rectangular region to render to. - * - * The viewport specifies where the content of the View (i.e. the Scene) is rendered in - * the render target. The Render target is automatically clipped to the Viewport. - * - * @param viewport The Viewport to render the Scene into. The Viewport is a value-type, it is - * therefore copied. The parameter can be discarded after this call returns. - */ - void setViewport(Viewport const& viewport) noexcept; - - /** - * Returns the rectangular region that gets rendered to. - * @return A constant reference to View's viewport. - */ - Viewport const& getViewport() const noexcept; - /** * Sets the blending mode used to draw the view into the SwapChain. * @@ -418,29 +426,33 @@ public: * RenderableManager::Builder::receiveShadows(), * RenderableManager::Builder::castShadows(), */ - void setShadowsEnabled(bool enabled) noexcept; + void setShadowingEnabled(bool enabled) noexcept; /** - * Specifies an offscreen render target to render into. - * - * By default, the view's associated render target is nullptr, which corresponds to the - * SwapChain associated with the engine. - * - * A view with a custom render target cannot rely on Renderer::ClearOptions, which only apply - * to the SwapChain. Such view can use a Skybox instead. - * - * @param renderTarget Render target associated with view, or nullptr for the swap chain. + * Enables or disables shadow mapping. Enabled by default. + * @deprecated use setShadowingEnabled */ - void setRenderTarget(RenderTarget* renderTarget) noexcept; + UTILS_DEPRECATED + void setShadowsEnabled(bool enabled) noexcept { + setShadowingEnabled(enabled); + } /** - * Gets the offscreen render target associated with this view. - * - * Returns nullptr if the render target is the swap chain (which is default). - * - * @see setRenderTarget + * @return whether shadowing is enabled */ - RenderTarget* getRenderTarget() const noexcept; + bool isShadowingEnabled() const noexcept; + + /** + * Enables or disables screen space refraction. Enabled by default. + * + * @param enabled true enables screen space refraction, false disables it. + */ + void setScreenSpaceRefractionEnabled(bool enabled) noexcept; + + /** + * @return whether screen space refraction is enabled + */ + bool isScreenSpaceRefractionEnabled() const noexcept; /** * Sets how many samples are to be used for MSAA in the post-process stage. @@ -524,6 +536,20 @@ public: */ const ColorGrading* getColorGrading() const noexcept; + /** + * Sets ambient occlusion options. + * + * @param options Options for ambient occlusion. + */ + void setAmbientOcclusionOptions(AmbientOcclusionOptions const& options) noexcept; + + /** + * Gets the ambient occlusion options. + * + * @return ambient occlusion options currently set. + */ + AmbientOcclusionOptions const& getAmbientOcclusionOptions() const noexcept; + /** * Enables or disables bloom in the post-processing stage. Disabled by default. * diff --git a/filament/src/Renderer.cpp b/filament/src/Renderer.cpp index 02ce9cd0d8..659edb1472 100644 --- a/filament/src/Renderer.cpp +++ b/filament/src/Renderer.cpp @@ -374,8 +374,7 @@ void FRenderer::renderJob(ArenaScope& arena, FView& view) { // -------------------------------------------------------------------------------------------- // SSAO pass - const bool useSSAO = aoOptions.enabled; - if (useSSAO) { + if (aoOptions.enabled) { // we could rely on FrameGraph culling, but this creates unnecessary CPU work ppm.screenSpaceAmbientOcclusion(fg, pass, svp, cameraInfo, aoOptions); } @@ -425,15 +424,16 @@ void FRenderer::renderJob(ArenaScope& arena, FView& view) { colorGradingConfigForColor.asSubpass = colorGradingConfigForColor.asSubpass && !taaOptions.enabled; // the color pass itself + color-grading as subpass if needed - colorPass(fg, "Color Pass", desc, config, colorGradingConfigForColor, pass, view); + FrameGraphId colorPassOutput = colorPass(fg, "Color Pass", + desc, config, colorGradingConfigForColor, pass, view); // the color pass + refraction + color-grading as subpass if needed // this cancels the colorPass() call above if refraction is active. - // TODO: look for refraction draw calls only if screen-space refraction is enabled - FrameGraphId colorPassOutput = - refractionPass(fg, config, colorGradingConfigForColor, pass, view); - FrameGraphId input = colorPassOutput; + if (view.isScreenSpaceRefractionEnabled()) { + colorPassOutput = refractionPass(fg, config, colorGradingConfigForColor, pass, view); + } + FrameGraphId input = colorPassOutput; fg.addTrivialSideEffectPass("Finish Color Passes", [&view](DriverApi& driver) { // Unbind SSAO sampler, b/c the FrameGraph will delete the texture at the end of the pass. view.cleanupRenderPasses(); diff --git a/filament/src/View.cpp b/filament/src/View.cpp index 7c27058395..d703c006a2 100644 --- a/filament/src/View.cpp +++ b/filament/src/View.cpp @@ -888,8 +888,8 @@ Camera const* View::getDirectionalLightCamera() const noexcept { return upcast(this)->getDirectionalLightCamera(); } -void View::setShadowsEnabled(bool enabled) noexcept { - upcast(this)->setShadowsEnabled(enabled); +void View::setShadowingEnabled(bool enabled) noexcept { + upcast(this)->setShadowingEnabled(enabled); } void View::setRenderTarget(RenderTarget* renderTarget) noexcept { @@ -1047,5 +1047,17 @@ View::BlendMode View::getBlendMode() const noexcept { uint8_t View::getVisibleLayers() const noexcept { return upcast(this)->getVisibleLayers(); } - + +bool View::isShadowingEnabled() const noexcept { + return upcast(this)->isShadowingEnabled(); +} + +void View::setScreenSpaceRefractionEnabled(bool enabled) noexcept { + upcast(this)->setScreenSpaceRefractionEnabled(enabled); +} + +bool View::isScreenSpaceRefractionEnabled() const noexcept { + return upcast(this)->isScreenSpaceRefractionEnabled(); +} + } // namespace filament diff --git a/filament/src/details/View.h b/filament/src/details/View.h index 15f8ff2afe..af07f78997 100644 --- a/filament/src/details/View.h +++ b/filament/src/details/View.h @@ -182,7 +182,13 @@ public: FEngine& engine, const CameraInfo& camera, FScene::RenderableSoa& renderableData, Range visible) noexcept; - void setShadowsEnabled(bool enabled) noexcept { mShadowingEnabled = enabled; } + void setShadowingEnabled(bool enabled) noexcept { mShadowingEnabled = enabled; } + + bool isShadowingEnabled() const noexcept { return mShadowingEnabled; } + + void setScreenSpaceRefractionEnabled(bool enabled) noexcept { mScreenSpaceRefractionEnabled = enabled; } + + bool isScreenSpaceRefractionEnabled() const noexcept { return mScreenSpaceRefractionEnabled; } FCamera const* getDirectionalLightCamera() const noexcept { return &mShadowMapManager.getCascadeShadowMap(0)->getDebugCamera(); @@ -456,6 +462,7 @@ private: ToneMapping mToneMapping = ToneMapping::ACES; Dithering mDithering = Dithering::TEMPORAL; bool mShadowingEnabled = true; + bool mScreenSpaceRefractionEnabled = true; bool mHasPostProcessPass = true; AmbientOcclusionOptions mAmbientOcclusionOptions{}; ShadowType mShadowType = ShadowType::PCF; diff --git a/libs/filagui/src/ImGuiHelper.cpp b/libs/filagui/src/ImGuiHelper.cpp index 7d7fdfdeb0..8aecd808f9 100644 --- a/libs/filagui/src/ImGuiHelper.cpp +++ b/libs/filagui/src/ImGuiHelper.cpp @@ -61,7 +61,7 @@ ImGuiHelper::ImGuiHelper(Engine* engine, filament::View* view, const Path& fontP view->setPostProcessingEnabled(false); view->setBlendMode(View::BlendMode::TRANSLUCENT); - view->setShadowsEnabled(false); + view->setShadowingEnabled(false); // Attach a scene for our one and only Renderable. view->setScene(mScene); diff --git a/libs/filamentapp/src/FilamentApp.cpp b/libs/filamentapp/src/FilamentApp.cpp index 1d63c9c700..e52564b85d 100644 --- a/libs/filamentapp/src/FilamentApp.cpp +++ b/libs/filamentapp/src/FilamentApp.cpp @@ -124,9 +124,9 @@ void FilamentApp::run(const Config& config, SetupCallback setupCallback, window->mOrthoView->getView()->setVisibleLayers(0x6, 0x6); // only preserve the color buffer for additional views; depth and stencil can be discarded. - window->mDepthView->getView()->setShadowsEnabled(false); - window->mGodView->getView()->setShadowsEnabled(false); - window->mOrthoView->getView()->setShadowsEnabled(false); + window->mDepthView->getView()->setShadowingEnabled(false); + window->mGodView->getView()->setShadowingEnabled(false); + window->mOrthoView->getView()->setShadowingEnabled(false); } loadDirt(config);