Skip to content

Commit

Permalink
Add FILAMENT_ENABLE_FEATURE_LEVEL_0 option
Browse files Browse the repository at this point in the history
This allows clients to selectively exclude feature level 0 support.
  • Loading branch information
elizagamedev authored and poweifeng committed Nov 15, 2023
1 parent 5b2d3ac commit 1b10e7d
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ option(FILAMENT_LINUX_IS_MOBILE "Treat Linux as Mobile" OFF)

option(FILAMENT_ENABLE_ASAN_UBSAN "Enable Address and Undefined Behavior Sanitizers" OFF)

option(FILAMENT_ENABLE_FEATURE_LEVEL_0 "Enable Feature Level 0" ON)

set(FILAMENT_NDK_VERSION "" CACHE STRING
"Android NDK version or version prefix to be used when building for Android."
)
Expand Down
1 change: 0 additions & 1 deletion NEW_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ appropriate header in [RELEASE_NOTES.md](./RELEASE_NOTES.md).

## Release notes for next branch cut


2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Instead, if you are authoring a PR for the main branch, add your release note to
- engine: Add missing `Material::getReflectionMode()` method in Java
- engine: Support basic usage of post-processing materials on feature level 0
- engine: Fix critical GLES 2.0 bugs
- engine: Add `FILAMENT_ENABLE_FEATURE_LEVEL_0` build-time option optionally allow building Filament
without FL0 support.

## v1.45.1

Expand Down
61 changes: 58 additions & 3 deletions filament/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ set(MATERIAL_SRCS
src/materials/colorGrading/customResolveAsSubpass.mat
src/materials/debugShadowCascades.mat
src/materials/defaultMaterial.mat
src/materials/defaultMaterial0.mat
src/materials/dof/dof.mat
src/materials/dof/dofCoc.mat
src/materials/dof/dofDownsample.mat
Expand All @@ -229,7 +228,6 @@ set(MATERIAL_SRCS
src/materials/dof/dofMipmap.mat
src/materials/dof/dofMedian.mat
src/materials/flare/flare.mat
src/materials/blitLow.mat
src/materials/bloom/bloomDownsample.mat
src/materials/bloom/bloomDownsample2x.mat
src/materials/bloom/bloomDownsample9.mat
Expand All @@ -238,7 +236,6 @@ set(MATERIAL_SRCS
src/materials/ssao/bilateralBlurBentNormals.mat
src/materials/ssao/mipmapDepth.mat
src/materials/skybox.mat
src/materials/skybox0.mat
src/materials/ssao/sao.mat
src/materials/ssao/saoBentNormals.mat
src/materials/separableGaussianBlur.mat
Expand All @@ -247,6 +244,18 @@ set(MATERIAL_SRCS
src/materials/vsmMipmap.mat
)

set(MATERIAL_FL0_SRCS
src/materials/defaultMaterial0.mat
src/materials/skybox0.mat
)

# TODO(exv): Replace the below duplicated materials with a command-line option
# to matc to force disable including ESSL 1.0 code in FL0 materials.

set(MATERIAL_SWITCH_FL0_SRCS_HACK
src/materials/blitLow.mat
)

# Embed the binary resource blob for materials.
get_resgen_vars(${RESOURCE_DIR} materials)
list(APPEND PRIVATE_HDRS ${RESGEN_HEADER})
Expand All @@ -271,6 +280,11 @@ if (NOT DFG_LUT_SIZE)
endif()
message(STATUS "DFG LUT size set to ${DFG_LUT_SIZE}x${DFG_LUT_SIZE}")

# Whether to include FL0 materials.
if (FILAMENT_ENABLE_FEATURE_LEVEL_0)
add_definitions(-DFILAMENT_ENABLE_FEATURE_LEVEL_0)
endif()

# ==================================================================================================
# Definitions
# ==================================================================================================
Expand Down Expand Up @@ -312,6 +326,47 @@ foreach (mat_src ${MATERIAL_SRCS})
list(APPEND MATERIAL_BINS ${output_path})
endforeach()


# HACK: Pick between the normal and FL0-exclusionary variants of the materials
# based on the value of FILAMENT_ENABLE_FEATURE_LEVEL_0.
foreach (mat_src ${MATERIAL_SWITCH_FL0_SRCS_HACK})
get_filename_component(localname "${mat_src}" NAME_WE)
get_filename_component(fullname "${mat_src}" ABSOLUTE)
set(output_path "${MATERIAL_DIR}/${localname}.filamat")

if (NOT FILAMENT_ENABLE_FATURE_LEVEL_0)
# Pick the non-FL0 variant instead.
string(REGEX REPLACE "\\.mat$" "1.mat" fullname "${fullname}")
string(REGEX REPLACE "\\.mat$" "1.mat" mat_src "${mat_src}")
endif()

add_custom_command(
OUTPUT ${output_path}
COMMAND matc ${MATC_BASE_FLAGS} -o ${output_path} ${fullname}
MAIN_DEPENDENCY ${fullname}
DEPENDS matc
COMMENT "Compiling material ${mat_src} to ${output_path}"
)
list(APPEND MATERIAL_BINS ${output_path})
endforeach()

if (FILAMENT_ENABLE_FEATURE_LEVEL_0 AND FILAMENT_SUPPORTS_OPENGL)
foreach (mat_src ${MATERIAL_FL0_SRCS})
get_filename_component(localname "${mat_src}" NAME_WE)
get_filename_component(fullname "${mat_src}" ABSOLUTE)
set(output_path "${MATERIAL_DIR}/${localname}.filamat")

add_custom_command(
OUTPUT ${output_path}
COMMAND matc -a opengl -p ${MATC_TARGET} ${MATC_OPT_FLAGS} -o ${output_path} ${fullname}
MAIN_DEPENDENCY ${fullname}
DEPENDS matc
COMMENT "Compiling material ${mat_src} to ${output_path}"
)
list(APPEND MATERIAL_BINS ${output_path})
endforeach ()
endif ()

# Additional dependencies on included files for materials

add_custom_command(
Expand Down
9 changes: 8 additions & 1 deletion filament/src/details/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void FEngine::init() {

mActiveFeatureLevel = std::min(mActiveFeatureLevel, driverApi.getFeatureLevel());

#ifndef FILAMENT_ENABLE_FEATURE_LEVEL_0
assert_invariant(mActiveFeatureLevel > FeatureLevel::FEATURE_LEVEL_0);
#endif

slog.i << "Backend feature level: " << int(driverApi.getFeatureLevel()) << io::endl;
slog.i << "FEngine feature level: " << int(mActiveFeatureLevel) << io::endl;

Expand Down Expand Up @@ -328,12 +332,15 @@ void FEngine::init() {
driverApi.update3DImage(mDummyZeroTexture, 0, 0, 0, 0, 1, 1, 1,
{ zeroes, 4, Texture::Format::RGBA, Texture::Type::UBYTE });

#ifdef FILAMENT_ENABLE_FEATURE_LEVEL_0
if (UTILS_UNLIKELY(mActiveFeatureLevel == FeatureLevel::FEATURE_LEVEL_0)) {
FMaterial::DefaultMaterialBuilder defaultMaterialBuilder;
defaultMaterialBuilder.package(
MATERIALS_DEFAULTMATERIAL0_DATA, MATERIALS_DEFAULTMATERIAL0_SIZE);
mDefaultMaterial = downcast(defaultMaterialBuilder.build(*const_cast<FEngine*>(this)));
} else {
} else
#endif
{
mDefaultColorGrading = downcast(ColorGrading::Builder().build(*this));

FMaterial::DefaultMaterialBuilder defaultMaterialBuilder;
Expand Down
5 changes: 4 additions & 1 deletion filament/src/details/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ FSkybox::FSkybox(FEngine& engine, const Builder& builder) noexcept

FMaterial const* FSkybox::createMaterial(FEngine& engine) {
Material::Builder builder;
#ifdef FILAMENT_ENABLE_FEATURE_LEVEL_0
if (UTILS_UNLIKELY(engine.getActiveFeatureLevel() == Engine::FeatureLevel::FEATURE_LEVEL_0)) {
builder.package(MATERIALS_SKYBOX0_DATA, MATERIALS_SKYBOX0_SIZE);
} else {
} else
#endif
{
builder.package(MATERIALS_SKYBOX_DATA, MATERIALS_SKYBOX_SIZE);
}
auto material = builder.build(engine);
Expand Down
39 changes: 39 additions & 0 deletions filament/src/materials/blitLow1.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
material {
name : blitLow,
parameters : [
{
type : sampler2d,
name : color,
precision: medium
},
{
type : float4,
name : resolution,
precision: high
},
{
type : float4,
name : viewport,
precision: high
}
],
variables : [
vertex
],
depthWrite : false,
depthCulling : false,
domain: postprocess,
}

vertex {
void postProcessVertex(inout PostProcessVertexInputs postProcess) {
postProcess.vertex.xy = materialParams.viewport.xy + postProcess.normalizedUV * materialParams.viewport.zw;
postProcess.vertex.xy = uvToRenderTargetUV(postProcess.vertex.xy);
}
}

fragment {
void postProcess(inout PostProcessInputs postProcess) {
postProcess.color = textureLod(materialParams_color, variable_vertex.xy, 0.0);
}
}
9 changes: 9 additions & 0 deletions libs/filagui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@ set(MATERIAL_SRCS

file(MAKE_DIRECTORY ${MATERIAL_DIR})

# HACK: Pick between the normal and FL0-exclusionary variants of the materials
# based on the value of FILAMENT_ENABLE_FEATURE_LEVEL_0.
foreach (mat_src ${MATERIAL_SRCS})
get_filename_component(localname "${mat_src}" NAME_WE)
get_filename_component(fullname "${mat_src}" ABSOLUTE)
set(output_path "${MATERIAL_DIR}/${localname}.filamat")

if (NOT FILAMENT_ENABLE_FATURE_LEVEL_0)
# Pick the non-FL0 variant instead.
string(REGEX REPLACE "\\.mat$" "1.mat" fullname "${fullname}")
string(REGEX REPLACE "\\.mat$" "1.mat" mat_src "${mat_src}")
endif()

add_custom_command(
OUTPUT ${output_path}
COMMAND matc ${MATC_BASE_FLAGS} -o ${output_path} ${fullname}
Expand Down
28 changes: 28 additions & 0 deletions libs/filagui/src/materials/uiBlit1.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
material {
name : uiBlit,
parameters : [
{
type : sampler2d,
name : albedo
}
],
requires : [
uv0,
color
],
shadingModel : unlit,
culling : none,
depthCulling: false,
blending : transparent,
}

fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
vec2 uv = getUV0();
uv.y = 1.0 - uv.y;
vec4 albedo = texture(materialParams_albedo, uv);
material.baseColor = getColor() * albedo;
material.baseColor.rgb *= material.baseColor.a;
}
}

0 comments on commit 1b10e7d

Please sign in to comment.