Permalink
Browse files

Support materialMaps (PBS).

Currently only the red and green channels are used for metalness and
roughness.
  • Loading branch information...
1 parent 48fabc9 commit 2330e4410b90a2da513e9402fb80ac447e683d88 @gimhael gimhael committed Feb 10, 2016
@@ -1154,6 +1154,28 @@ bool GLCompileMacro_USE_REFLECTIVE_SPECULAR::MissesRequiredMacros( size_t permut
return false;
}
+bool GLCompileMacro_USE_PHYSICAL_SHADING::MissesRequiredMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const
+{
+ bool foundUSE_NORMAL_MAPPING = false;
+
+ for (const GLCompileMacro* macro : macros)
+ {
+ if ( ( permutation & macro->GetBit() ) != 0 && macro->GetType() == USE_NORMAL_MAPPING )
+ {
+ foundUSE_NORMAL_MAPPING = true;
+ break;
+ }
+ }
+
+ if ( !foundUSE_NORMAL_MAPPING )
+ {
+ //ri.Printf(PRINT_ALL, "missing macro! canceling '%s' <= '%s'\n", GetName(), "USE_NORMAL_MAPPING");
+ return true;
+ }
+
+ return false;
+}
+
bool GLShader::GetCompileMacrosString( size_t permutation, std::string &compileMacrosOut ) const
{
compileMacrosOut.clear();
@@ -1323,7 +1345,8 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
GLCompileMacro_USE_NORMAL_MAPPING( this ),
GLCompileMacro_USE_PARALLAX_MAPPING( this ),
GLCompileMacro_USE_GLOW_MAPPING( this ),
- GLCompileMacro_USE_SHADER_LIGHTS( this )
+ GLCompileMacro_USE_SHADER_LIGHTS( this ),
+ GLCompileMacro_USE_PHYSICAL_SHADING( this )
{
}
@@ -1378,7 +1401,8 @@ GLShader_vertexLighting_DBS_entity::GLShader_vertexLighting_DBS_entity( GLShader
GLCompileMacro_USE_PARALLAX_MAPPING( this ),
GLCompileMacro_USE_REFLECTIVE_SPECULAR( this ),
GLCompileMacro_USE_GLOW_MAPPING( this ),
- GLCompileMacro_USE_SHADER_LIGHTS( this )
+ GLCompileMacro_USE_SHADER_LIGHTS( this ),
+ GLCompileMacro_USE_PHYSICAL_SHADING( this )
{
}
@@ -1435,7 +1459,8 @@ GLShader_vertexLighting_DBS_world::GLShader_vertexLighting_DBS_world( GLShaderMa
GLCompileMacro_USE_NORMAL_MAPPING( this ),
GLCompileMacro_USE_PARALLAX_MAPPING( this ),
GLCompileMacro_USE_GLOW_MAPPING( this ),
- GLCompileMacro_USE_SHADER_LIGHTS( this )
+ GLCompileMacro_USE_SHADER_LIGHTS( this ),
+ GLCompileMacro_USE_PHYSICAL_SHADING( this )
{
}
@@ -752,7 +752,8 @@ class GLCompileMacro
LIGHT_DIRECTIONAL,
USE_GLOW_MAPPING,
USE_DEPTH_FADE,
- USE_SHADER_LIGHTS
+ USE_SHADER_LIGHTS,
+ USE_PHYSICAL_SHADING
};
public:
@@ -1379,6 +1380,50 @@ class GLCompileMacro_USE_SHADER_LIGHTS :
}
};
+class GLCompileMacro_USE_PHYSICAL_SHADING :
+ GLCompileMacro
+{
+public:
+ GLCompileMacro_USE_PHYSICAL_SHADING( GLShader *shader ) :
+ GLCompileMacro( shader )
+ {
+ }
+
+ const char *GetName() const
+ {
+ return "USE_PHYSICAL_SHADING";
+ }
+
+ EGLCompileMacro GetType() const
+ {
+ return USE_PHYSICAL_SHADING;
+ }
+
+ bool MissesRequiredMacros( size_t permutation, const std::vector< GLCompileMacro * > &macros ) const;
+
+ void EnableMacro_USE_PHYSICAL_SHADING()
+ {
+ EnableMacro();
+ }
+
+ void DisableMacro_USE_PHYSICAL_SHADING()
+ {
+ DisableMacro();
+ }
+
+ void SetPhysicalShading( bool enable )
+ {
+ if ( enable )
+ {
+ EnableMacro();
+ }
+ else
+ {
+ DisableMacro();
+ }
+ }
+};
+
class u_ColorTextureMatrix :
GLUniformMatrix4f
{
@@ -2354,7 +2399,8 @@ class GLShader_lightMapping :
public GLCompileMacro_USE_NORMAL_MAPPING,
public GLCompileMacro_USE_PARALLAX_MAPPING,
public GLCompileMacro_USE_GLOW_MAPPING,
- public GLCompileMacro_USE_SHADER_LIGHTS
+ public GLCompileMacro_USE_SHADER_LIGHTS,
+ public GLCompileMacro_USE_PHYSICAL_SHADING
{
public:
GLShader_lightMapping( GLShaderManager *manager );
@@ -2390,7 +2436,8 @@ class GLShader_vertexLighting_DBS_entity :
public GLCompileMacro_USE_PARALLAX_MAPPING,
public GLCompileMacro_USE_REFLECTIVE_SPECULAR,
public GLCompileMacro_USE_GLOW_MAPPING,
- public GLCompileMacro_USE_SHADER_LIGHTS
+ public GLCompileMacro_USE_SHADER_LIGHTS,
+ public GLCompileMacro_USE_PHYSICAL_SHADING
{
public:
GLShader_vertexLighting_DBS_entity( GLShaderManager *manager );
@@ -2423,7 +2470,8 @@ class GLShader_vertexLighting_DBS_world :
public GLCompileMacro_USE_NORMAL_MAPPING,
public GLCompileMacro_USE_PARALLAX_MAPPING,
public GLCompileMacro_USE_GLOW_MAPPING,
- public GLCompileMacro_USE_SHADER_LIGHTS
+ public GLCompileMacro_USE_SHADER_LIGHTS,
+ public GLCompileMacro_USE_PHYSICAL_SHADING
{
public:
GLShader_vertexLighting_DBS_world( GLShaderManager *manager );
Oops, something went wrong.
@@ -1012,6 +1012,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
TB_DIFFUSEMAP = 0,
TB_NORMALMAP,
TB_SPECULARMAP,
+ TB_MATERIALMAP = TB_SPECULARMAP,
TB_GLOWMAP,
MAX_TEXTURE_BUNDLES = 4
};
@@ -1037,6 +1038,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
ST_DIFFUSEMAP,
ST_NORMALMAP,
ST_SPECULARMAP,
+ ST_MATERIALMAP,
ST_REFLECTIONMAP, // cubeMap based reflection
ST_REFRACTIONMAP,
ST_DISPERSIONMAP,
@@ -1050,6 +1052,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
ST_COLLAPSE_lighting_DBG, // diffusemap + bumpmap + glowmap
ST_COLLAPSE_lighting_DBS, // diffusemap + bumpmap + specularmap
ST_COLLAPSE_lighting_DBSG, // diffusemap + bumpmap + specularmap + glowmap
+ ST_COLLAPSE_lighting_DBM, // diffusemap + bumpmap + materialmap
+ ST_COLLAPSE_lighting_DBMG, // diffusemap + bumpmap + materialmap + glowmap
ST_COLLAPSE_reflection_CB, // color cubemap + bumpmap
// light shader stage types
@@ -1065,6 +1069,8 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
COLLAPSE_lighting_DBG,
COLLAPSE_lighting_DBS,
COLLAPSE_lighting_DBSG,
+ COLLAPSE_lighting_DBM,
+ COLLAPSE_lighting_DBMG,
COLLAPSE_reflection_CB,
COLLAPSE_color_lightmap
};
@@ -710,6 +710,16 @@ static void Render_generic( int stage )
GL_CheckErrors();
}
+static bool hasMaterialMapping( shader_t *shader ) {
+ switch( shader->collapseType ) {
+ case collapseType_t::COLLAPSE_lighting_DBM:
+ case collapseType_t::COLLAPSE_lighting_DBMG:
+ return true;
+ default:
+ return false;
+ }
+}
+
static void Render_vertexLighting_DBS_entity( int stage )
{
vec3_t viewOrigin;
@@ -724,6 +734,7 @@ static void Render_vertexLighting_DBS_entity( int stage )
bool normalMapping = r_normalMapping->integer && ( pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr );
bool glowMapping = ( pStage->bundle[ TB_GLOWMAP ].image[ 0 ] != nullptr );
+ bool materialMapping = hasMaterialMapping( tess.surfaceShader );
// choose right shader program ----------------------------------
gl_vertexLightingShader_DBS_entity->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning );
@@ -743,6 +754,7 @@ static void Render_vertexLighting_DBS_entity( int stage )
gl_vertexLightingShader_DBS_entity->SetReflectiveSpecular( normalMapping && tr.cubeHashTable != nullptr );
+ gl_vertexLightingShader_DBS_entity->SetPhysicalShading( materialMapping );
gl_vertexLightingShader_DBS_entity->SetGlowMapping( glowMapping );
gl_vertexLightingShader_DBS_entity->BindProgram( pStage->deformIndex );
@@ -2751,6 +2763,8 @@ void Tess_StageIteratorGeneric()
case stageType_t::ST_COLLAPSE_lighting_DBG:
case stageType_t::ST_COLLAPSE_lighting_DB:
case stageType_t::ST_COLLAPSE_lighting_DBS:
+ case stageType_t::ST_COLLAPSE_lighting_DBM:
+ case stageType_t::ST_COLLAPSE_lighting_DBMG:
{
{
if ( r_precomputedLighting->integer || r_vertexLighting->integer )
@@ -1916,6 +1916,10 @@ static bool ParseStage( shaderStage_t *stage, const char **text )
{
stage->type = stageType_t::ST_SPECULARMAP;
}
+ else if ( !Q_stricmp( token, "materialMap" ) )
+ {
+ stage->type = stageType_t::ST_MATERIALMAP;
+ }
else if ( !Q_stricmp( token, "glowMap" ) )
{
stage->type = stageType_t::ST_GLOWMAP;
@@ -1978,6 +1982,10 @@ static bool ParseStage( shaderStage_t *stage, const char **text )
{
stage->type = stageType_t::ST_SPECULARMAP;
}
+ else if ( !Q_stricmp( token, "materialMap" ) )
+ {
+ stage->type = stageType_t::ST_MATERIALMAP;
+ }
else if ( !Q_stricmp( token, "glowMap" ) )
{
stage->type = stageType_t::ST_GLOWMAP;
@@ -3089,6 +3097,26 @@ static void ParseSpecularMap( shaderStage_t *stage, const char **text )
}
}
+static void ParseMaterialMap( shaderStage_t *stage, const char **text )
+{
+ char buffer[ 1024 ] = "";
+
+ stage->active = true;
+ stage->type = stageType_t::ST_MATERIALMAP;
+ stage->rgbGen = colorGen_t::CGEN_IDENTITY;
+ stage->stateBits = GLS_DEFAULT;
+
+ if ( !r_compressSpecularMaps->integer )
+ {
+ stage->uncompressed = true;
+ }
+
+ if ( ParseMap( text, buffer, sizeof( buffer ) ) )
+ {
+ LoadMap( stage, buffer );
+ }
+}
+
static void ParseGlowMap( shaderStage_t *stage, const char **text )
{
char buffer[ 1024 ] = "";
@@ -3627,6 +3655,13 @@ static bool ParseShader( const char *_text )
s++;
continue;
}
+ // materialMap <image>
+ else if ( !Q_stricmp( token, "materialMap" ) )
+ {
+ ParseMaterialMap( &stages[ s ], text );
+ s++;
+ continue;
+ }
// glowMap <image>
else if ( !Q_stricmp( token, "glowMap" ) )
{
@@ -3757,12 +3792,14 @@ static void CollapseStages()
bool hasDiffuseStage;
bool hasNormalStage;
bool hasSpecularStage;
+ bool hasMaterialStage;
bool hasReflectionStage;
bool hasGlowStage;
shaderStage_t tmpDiffuseStage;
shaderStage_t tmpNormalStage;
shaderStage_t tmpSpecularStage;
+ shaderStage_t tmpMaterialStage;
shaderStage_t tmpReflectionStage;
shaderStage_t tmpGlowStage;
@@ -3788,6 +3825,7 @@ static void CollapseStages()
hasDiffuseStage = false;
hasNormalStage = false;
hasSpecularStage = false;
+ hasMaterialStage = false;
hasReflectionStage = false;
hasGlowStage = false;
@@ -3848,6 +3886,11 @@ static void CollapseStages()
hasSpecularStage = true;
tmpSpecularStage = stages[ j + i ];
}
+ else if ( stages[ j + i ].type == stageType_t::ST_MATERIALMAP && !hasMaterialStage )
+ {
+ hasMaterialStage = true;
+ tmpMaterialStage = stages[ j + i ];
+ }
else if ( stages[ j + i ].type == stageType_t::ST_REFLECTIONMAP && !hasReflectionStage )
{
hasReflectionStage = true;
@@ -3861,6 +3904,10 @@ static void CollapseStages()
}
// NOTE: Tr3B - merge as many stages as possible
+ if( hasSpecularStage && hasMaterialStage ) {
+ Log::Warn( "specularMap disabled in favor of materialMap in shader '%s'\n", shader.name );
+ hasSpecularStage = false;
+ }
// try to merge diffuse/normal/specular/glow
if ( hasDiffuseStage &&
@@ -3906,6 +3953,46 @@ static void CollapseStages()
j += 2;
continue;
}
+ // try to merge diffuse/normal/material/glow
+ else if ( hasDiffuseStage &&
+ hasNormalStage &&
+ hasMaterialStage &&
+ hasGlowStage
+ )
+ {
+ tmpShader.collapseType = collapseType_t::COLLAPSE_lighting_DBMG;
+
+ tmpStages[ numStages ] = tmpDiffuseStage;
+ tmpStages[ numStages ].type = stageType_t::ST_COLLAPSE_lighting_DBMG;
+
+ tmpStages[ numStages ].bundle[ TB_NORMALMAP ] = tmpNormalStage.bundle[ 0 ];
+
+ tmpStages[ numStages ].bundle[ TB_MATERIALMAP ] = tmpMaterialStage.bundle[ 0 ];
+
+ tmpStages[ numStages ].bundle[ TB_GLOWMAP ] = tmpGlowStage.bundle[ 0 ];
+ numStages++;
+ j += 3;
+ continue;
+ }
+ // try to merge diffuse/normal/material
+ else if ( hasDiffuseStage &&
+ hasNormalStage &&
+ hasMaterialStage
+ )
+ {
+ tmpShader.collapseType = collapseType_t::COLLAPSE_lighting_DBM;
+
+ tmpStages[ numStages ] = tmpDiffuseStage;
+ tmpStages[ numStages ].type = stageType_t::ST_COLLAPSE_lighting_DBM;
+
+ tmpStages[ numStages ].bundle[ TB_NORMALMAP ] = tmpNormalStage.bundle[ 0 ];
+
+ tmpStages[ numStages ].bundle[ TB_MATERIALMAP ] = tmpMaterialStage.bundle[ 0 ];
+
+ numStages++;
+ j += 2;
+ continue;
+ }
// try to merge diffuse/normal/glow
else if ( hasDiffuseStage &&
hasNormalStage &&
@@ -5122,6 +5209,10 @@ void R_ShaderList_f()
{
str += "lighting_DBS ";
}
+ else if ( shader->collapseType == collapseType_t::COLLAPSE_lighting_DBM )
+ {
+ str += "lighting_DBM ";
+ }
else if ( shader->collapseType == collapseType_t::COLLAPSE_reflection_CB )
{
str += "reflection_CB ";
Oops, something went wrong.

0 comments on commit 2330e44

Please sign in to comment.