From 2d9064d2bf2f3bda278ad970018965a1e169fa8d Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Mon, 26 Apr 2021 13:42:26 -0700 Subject: [PATCH] Cleanup some shader #defines - create a new FILAMENT_HAS_FEATURE_TEXTURE_GATHER, which can be used in the shaders to use textureGather. - A new TARGET_GLES_ENVIRONMENT and TARGET_GL_ENVIRONMENT in addition to TARGET_METAL_ and TARGET_VULKAN_, for completeness. - Rename TARGET_LANGUAGE_SPIRV to FILAMENT_VULKAN_SEMANTICS and add FILAMENT_OPENGL_SEMANTICS for completeness. - expose the shader model to shaders as: FILAMENT_SHADER_MODEL_ES30 and FILAMENT_SHADER_MODEL_GL41 Use these new #define where appropriate. The main change here is that TARGET_MOBILE is now only used for dealing with the `mediump` default on mobile. This change also enables textureGather on Metal. --- filament/src/materials/antiAliasing/fxaa.fs | 2 +- filament/src/materials/dof/dofDownsample.mat | 2 +- filament/src/materials/dof/dofTiles.mat | 2 +- .../src/materials/dof/dofTilesSwizzle.mat | 2 +- .../filamat/include/filamat/MaterialBuilder.h | 4 ++-- libs/filamat/src/shaders/CodeGenerator.cpp | 24 ++++++++++++++++++- shaders/src/ambient_occlusion.fs | 10 ++++---- shaders/src/common_types.fs | 2 +- 8 files changed, 35 insertions(+), 13 deletions(-) diff --git a/filament/src/materials/antiAliasing/fxaa.fs b/filament/src/materials/antiAliasing/fxaa.fs index 48b6977535..e7465720a4 100644 --- a/filament/src/materials/antiAliasing/fxaa.fs +++ b/filament/src/materials/antiAliasing/fxaa.fs @@ -2,7 +2,7 @@ #define gpu_shader5 1 // ES 3.0 does not have gather though -#if defined(TARGET_VULKAN_ENVIRONMENT) || !defined(TARGET_MOBILE) +#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER) #define FXAA_GATHER4_ALPHA 1 #else #define FXAA_GATHER4_ALPHA 0 diff --git a/filament/src/materials/dof/dofDownsample.mat b/filament/src/materials/dof/dofDownsample.mat index 7bcea00e45..e3a22b0012 100644 --- a/filament/src/materials/dof/dofDownsample.mat +++ b/filament/src/materials/dof/dofDownsample.mat @@ -78,7 +78,7 @@ void postProcess(inout PostProcessInputs postProcess) { hdr[3] = 1.0 / (1.0 + max3(s00.rgb)); vec4 d; -#if defined(TARGET_VULKAN_ENVIRONMENT) || !defined(TARGET_MOBILE) +#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER) d = textureGather(materialParams_depth, uv, 0); // 01, 11, 10, 00 #else d[0] = textureLodOffset(materialParams_depth, uv, 0.0, ivec2(0, 1)).r; diff --git a/filament/src/materials/dof/dofTiles.mat b/filament/src/materials/dof/dofTiles.mat index b3c4c3f342..797e8da33a 100644 --- a/filament/src/materials/dof/dofTiles.mat +++ b/filament/src/materials/dof/dofTiles.mat @@ -48,7 +48,7 @@ void postProcess(inout PostProcessInputs postProcess) { // note: the first time, the source texture is swizzled -- so .r and .g are the same channel // if swizzling is not supported, we use dofTilesSwizzle instead. -#if defined(TARGET_VULKAN_ENVIRONMENT) || !defined(TARGET_MOBILE) +#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER) vec4 mi = textureGather(materialParams_cocMinMax, uv, 0); // 01, 11, 10, 00 vec4 ma = textureGather(materialParams_cocMinMax, uv, 1); // 01, 11, 10, 00 #else diff --git a/filament/src/materials/dof/dofTilesSwizzle.mat b/filament/src/materials/dof/dofTilesSwizzle.mat index e852b57e8c..d3ca61ccb9 100644 --- a/filament/src/materials/dof/dofTilesSwizzle.mat +++ b/filament/src/materials/dof/dofTilesSwizzle.mat @@ -46,7 +46,7 @@ void postProcess(inout PostProcessInputs postProcess) { ivec2 i = ivec2(uv * size - 0.5); -#if defined(TARGET_VULKAN_ENVIRONMENT) || !defined(TARGET_MOBILE) +#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER) vec4 mi = textureGather(materialParams_cocMinMax, uv, 0); // 01, 11, 10, 00 #else vec4 mi; diff --git a/libs/filamat/include/filamat/MaterialBuilder.h b/libs/filamat/include/filamat/MaterialBuilder.h index 92c80aaf41..e92d78ee5c 100644 --- a/libs/filamat/include/filamat/MaterialBuilder.h +++ b/libs/filamat/include/filamat/MaterialBuilder.h @@ -68,8 +68,8 @@ public: }; enum class TargetLanguage { - GLSL, - SPIRV + GLSL, // GLSL with OpenGL semantics + SPIRV // GLSL with Vulkan semantics }; enum class Optimization { diff --git a/libs/filamat/src/shaders/CodeGenerator.cpp b/libs/filamat/src/shaders/CodeGenerator.cpp index 75fa57068f..8dbf1b5f90 100644 --- a/libs/filamat/src/shaders/CodeGenerator.cpp +++ b/libs/filamat/src/shaders/CodeGenerator.cpp @@ -18,9 +18,13 @@ #include "generated/shaders.h" +#include + #include #include +#include + namespace filamat { // From driverEnum namespace @@ -74,8 +78,26 @@ io::sstream& CodeGenerator::generateProlog(io::sstream& out, ShaderType type, if (mTargetApi == TargetApi::METAL) { out << "#define TARGET_METAL_ENVIRONMENT\n"; } + if (mTargetApi == TargetApi::OPENGL && mShaderModel == ShaderModel::GL_ES_30) { + out << "#define TARGET_GLES_ENVIRONMENT\n"; + } + if (mTargetApi == TargetApi::OPENGL && mShaderModel == ShaderModel::GL_CORE_41) { + out << "#define TARGET_GL_ENVIRONMENT\n"; + } + + out << '\n'; if (mTargetLanguage == TargetLanguage::SPIRV) { - out << "#define TARGET_LANGUAGE_SPIRV\n"; + out << "#define FILAMENT_VULKAN_SEMANTICS\n"; + } + if (mTargetLanguage == TargetLanguage::GLSL) { + out << "#define FILAMENT_OPENGL_SEMANTICS\n"; + } + + out << '\n'; + if (mTargetApi == TargetApi::VULKAN || + mTargetApi == TargetApi::METAL || + mTargetApi == TargetApi::OPENGL && mShaderModel == ShaderModel::GL_CORE_41) { + out << "#define FILAMENT_HAS_FEATURE_TEXTURE_GATHER\n"; } out << '\n'; diff --git a/shaders/src/ambient_occlusion.fs b/shaders/src/ambient_occlusion.fs index 59ac2b3ee5..8cfe4264b0 100644 --- a/shaders/src/ambient_occlusion.fs +++ b/shaders/src/ambient_occlusion.fs @@ -27,7 +27,11 @@ float evaluateSSAO() { highp vec2 size = vec2(textureSize(light_ssao, 0)); // Read four AO samples and their depths values -#if defined(TARGET_MOBILE) +#if defined(FILAMENT_HAS_FEATURE_TEXTURE_GATHER) + vec4 ao = textureGather(light_ssao, uv, 0); // 01, 11, 10, 00 + vec4 dg = textureGather(light_ssao, uv, 1); // 01, 11, 10, 00 + vec4 db = textureGather(light_ssao, uv, 2); // 01, 11, 10, 00 +#else // on mobile we can't use textureGather() because we're limited to ES3.0, // so we emulate it with texelFetch(), on Pixel 4 this doesn't seem to have any // significant impact on performance. @@ -38,10 +42,6 @@ float evaluateSSAO() { vec4 ao = vec4(s01.r, s11.r, s10.r, s00.r); vec4 dg = vec4(s01.g, s11.g, s10.g, s00.g); vec4 db = vec4(s01.b, s11.b, s10.b, s00.b); -#else - vec4 ao = textureGather(light_ssao, uv, 0); // 01, 11, 10, 00 - vec4 dg = textureGather(light_ssao, uv, 1); // 01, 11, 10, 00 - vec4 db = textureGather(light_ssao, uv, 2); // 01, 11, 10, 00 #endif // bilinear weights vec2 f = fract(uv * size - 0.5); diff --git a/shaders/src/common_types.fs b/shaders/src/common_types.fs index e5b7341a74..a28a6da7cc 100644 --- a/shaders/src/common_types.fs +++ b/shaders/src/common_types.fs @@ -1,4 +1,4 @@ -#if !defined(TARGET_MOBILE) || defined(TARGET_LANGUAGE_SPIRV) +#if defined(FILAMENT_VULKAN_SEMANTICS) #define LAYOUT_LOCATION(x) layout(location = x) #else #define LAYOUT_LOCATION(x)