Skip to content

Commit

Permalink
Revert "Remove now-redundant feature level 0 materials"
Browse files Browse the repository at this point in the history
This reverts most of commit 9a6b8bf. The hello
triangle sample remains unreverted.

The original commit inadvertently broke screen space reflections, and perhaps
other features when the default material was used. The source of the issue is
that MaterialBuilder.cpp (correctly) filters out variants that aren't supported
in feature level 0 materials, including screen space reflections.

Unfortunately, while the "feature level 0 compatibility" feature itself was
intended to make creating duplicate materials like this redundant in client
code, unfortunately, it seems the best solution for resolving this issue is to
simply keep these redundant materials in the core.

To elaborate: clients should expect that feature level 0 materials that they
create work on /all/ feature levels /exactly/ or /close to exactly/ identically.
This includes restricting more advanced features that theoretically could be
available on a higher feature level, like SSR. It's already true that if a user
would like to optionally opt-in to a more advanced material which takes
advantage of more advanced features, they would have to maintain two separate
versions of that material: one for feature level 3 and one for feature level 1.
It should be no different in this case.

However, the materials built into the engine core are an exception to this
expectation. Given that feature level 0 was tacked on after the fact with fewer
features, there must /by necessity/ have been a new material introduced for both
the default material and the default skybox specifically for feature level 0
with fewer features than extant client apps expected to be included by default.
I imagine if filament were to be rebuilt from the ground up, this exception
wouldn't exist. However, the end result is this somewhat messy redundancy.
  • Loading branch information
elizagamedev committed Sep 29, 2023
1 parent 0650b13 commit a7bb0a6
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 17 deletions.
22 changes: 22 additions & 0 deletions filament/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ set(MATERIAL_SRCS
src/materials/vsmMipmap.mat
)

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

# Embed the binary resource blob for materials.
get_resgen_vars(${RESOURCE_DIR} materials)
list(APPEND PRIVATE_HDRS ${RESGEN_HEADER})
Expand Down Expand Up @@ -310,6 +315,23 @@ foreach (mat_src ${MATERIAL_SRCS})
list(APPEND MATERIAL_BINS ${output_path})
endforeach()

if (IS_MOBILE_TARGET AND FILAMENT_SUPPORTS_OPENGL)
foreach (mat_src ${MATERIAL_ES2_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
17 changes: 11 additions & 6 deletions filament/src/details/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,22 @@ void FEngine::init() {
driverApi.update3DImage(mDummyZeroTexture, 0, 0, 0, 0, 1, 1, 1,
{ zeroes, 4, Texture::Format::RGBA, Texture::Type::UBYTE });

FMaterial::DefaultMaterialBuilder defaultMaterialBuilder;
defaultMaterialBuilder.package(
MATERIALS_DEFAULTMATERIAL_DATA, MATERIALS_DEFAULTMATERIAL_SIZE);
mDefaultMaterial = downcast(defaultMaterialBuilder.build(*const_cast<FEngine*>(this)));

#ifdef FILAMENT_TARGET_MOBILE
if (UTILS_LIKELY(mActiveFeatureLevel > FeatureLevel::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
#endif
{
mDefaultColorGrading = downcast(ColorGrading::Builder().build(*this));

FMaterial::DefaultMaterialBuilder defaultMaterialBuilder;
defaultMaterialBuilder.package(
MATERIALS_DEFAULTMATERIAL_DATA, MATERIALS_DEFAULTMATERIAL_SIZE);
mDefaultMaterial = downcast(defaultMaterialBuilder.build(*const_cast<FEngine*>(this)));

float3 dummyPositions[1] = {};
short4 dummyTangents[1] = {};
mDummyMorphTargetBuffer->setPositionsAt(*this, 0, dummyPositions, 1, 0);
Expand Down
9 changes: 8 additions & 1 deletion filament/src/details/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ FSkybox::FSkybox(FEngine& engine, const Builder& builder) noexcept

FMaterial const* FSkybox::createMaterial(FEngine& engine) {
Material::Builder builder;
builder.package(MATERIALS_SKYBOX_DATA, MATERIALS_SKYBOX_SIZE);
#ifdef FILAMENT_TARGET_MOBILE
if (UTILS_UNLIKELY(engine.getActiveFeatureLevel() == Engine::FeatureLevel::FEATURE_LEVEL_0)) {
builder.package(MATERIALS_SKYBOX0_DATA, MATERIALS_SKYBOX0_SIZE);
} else
#endif
{
builder.package(MATERIALS_SKYBOX_DATA, MATERIALS_SKYBOX_SIZE);
}
auto material = builder.build(engine);
return downcast(material);
}
Expand Down
3 changes: 1 addition & 2 deletions filament/src/materials/defaultMaterial.mat
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
material {
name : "Filament Default Material",
shadingModel : unlit,
featureLevel : 0
shadingModel : unlit
}

fragment {
Expand Down
12 changes: 12 additions & 0 deletions filament/src/materials/defaultMaterial0.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
material {
name : "Filament Default Material",
shadingModel : unlit,
featureLevel: 0
}

fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
material.baseColor.rgb = vec3(0.8);
}
}
10 changes: 2 additions & 8 deletions filament/src/materials/skybox.mat
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ material {
depthWrite : false,
shadingModel : unlit,
variantFilter : [ skinning, shadowReceiver, vsm ],
culling : none,
featureLevel : 0
culling: none
}

fragment {
Expand All @@ -36,15 +35,10 @@ fragment {
if (materialParams.constantColor != 0) {
sky = materialParams.color;
} else {
#if MATERIAL_FEATURE_LEVEL == 0
sky = vec4(textureCube(materialParams_skybox, variable_eyeDirection.xyz).rgb, 1.0);
#else
// textureLod() at 0.0 is more performant than texture().
sky = vec4(textureLod(materialParams_skybox, variable_eyeDirection.xyz, 0.0).rgb, 1.0);
#endif
sky.rgb *= frameUniforms.iblLuminance;
}
if (materialParams.showSun != 0 && frameUniforms.sun.w >= 0.0) {
if (materialParams.showSun != 0 && frameUniforms.sun.w >= 0.0f) {
vec3 direction = normalize(variable_eyeDirection.xyz);
// Assume the sun is a sphere
vec3 sun = frameUniforms.lightColorIntensity.rgb *
Expand Down
60 changes: 60 additions & 0 deletions filament/src/materials/skybox0.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
material {
name : Skybox,
parameters : [
{
type : int,
name : showSun
},
{
type : int,
name : constantColor
},
{
type : samplerCubemap,
name : skybox
},
{
type : float4,
name : color
}
],
variables : [
eyeDirection
],
vertexDomain : device,
depthWrite : false,
shadingModel : unlit,
variantFilter : [ skinning, shadowReceiver, vsm ],
culling: none,
featureLevel: 0
}

fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
vec4 sky;
if (materialParams.constantColor != 0) {
sky = materialParams.color;
} else {
sky = vec4(textureCube(materialParams_skybox, variable_eyeDirection.xyz).rgb, 1.0);
sky.rgb *= frameUniforms.iblLuminance;
}
if (materialParams.showSun != 0 && frameUniforms.sun.w >= 0.0) {
vec3 direction = normalize(variable_eyeDirection.xyz);
// Assume the sun is a sphere
vec3 sun = frameUniforms.lightColorIntensity.rgb *
(frameUniforms.lightColorIntensity.a * (4.0 * PI));
float cosAngle = dot(direction, frameUniforms.lightDirection);
float x = (cosAngle - frameUniforms.sun.x) * frameUniforms.sun.z;
float gradient = pow(1.0 - saturate(x), frameUniforms.sun.w);
sky.rgb = sky.rgb + gradient * sun;
}
material.baseColor = sky;
}
}

vertex {
void materialVertex(inout MaterialVertexInputs material) {
material.eyeDirection.xyz = material.worldPosition.xyz;
}
}

0 comments on commit a7bb0a6

Please sign in to comment.