Skip to content

Commit

Permalink
unified pbr shader
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jun 2, 2024
1 parent 0fa4ca1 commit 89feffc
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 37 deletions.
18 changes: 7 additions & 11 deletions engine/platform/Vulkan/material.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* Engine Copyright (c) 2024 Engine Development Team
https://github.com/beaumanvienna/vulkan
*
* PBR rendering; parts of this code are based on https://learnopengl.com/PBR/Lighting
*
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
Expand All @@ -24,14 +21,13 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/

// material
#define GLSL_HAS_VERTEX_COLOR (0x1 << 0x0)
#define GLSL_HAS_DIFFUSE_MAP (0x1 << 0x1)
#define GLSL_HAS_NORMAL_MAP (0x1 << 0x2)
#define GLSL_HAS_ROUGHNESS_MAP (0x1 << 0x3)
#define GLSL_HAS_METALLIC_MAP (0x1 << 0x4)
#define GLSL_HAS_ROUGHNESS_METALLIC_MAP (0x1 << 0x5)
#define GLSL_HAS_EMISSIVE_COLOR (0x1 << 0x6)
#define GLSL_HAS_EMISSIVE_MAP (0x1 << 0x7)
#define GLSL_HAS_DIFFUSE_MAP (0x1 << 0x0)
#define GLSL_HAS_NORMAL_MAP (0x1 << 0x1)
#define GLSL_HAS_ROUGHNESS_MAP (0x1 << 0x2)
#define GLSL_HAS_METALLIC_MAP (0x1 << 0x3)
#define GLSL_HAS_ROUGHNESS_METALLIC_MAP (0x1 << 0x4)
#define GLSL_HAS_EMISSIVE_COLOR (0x1 << 0x5)
#define GLSL_HAS_EMISSIVE_MAP (0x1 << 0x6)

// shader properties
#define GLSL_HAS_SKELETAL_ANIMATION (0x1 << 0x0)
Expand Down
10 changes: 1 addition & 9 deletions engine/platform/Vulkan/shaders/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,7 @@ void main()
}
else
{
if (bool(push.m_Features & GLSL_HAS_VERTEX_COLOR))
{
col = vec4(fragColor.r, fragColor.g, fragColor.b, 1.0);
}
else
{
col = push.m_DiffuseColor;
}

col = vec4(fragColor.r, fragColor.g, fragColor.b, 1.0);
}
if (col.w < 0.5)
{
Expand Down
10 changes: 1 addition & 9 deletions engine/platform/Vulkan/shaders/pbrSA.frag
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,7 @@ void main()
}
else
{
if (bool(push.m_Features & GLSL_HAS_VERTEX_COLOR))
{
col = vec4(fragColor.r, fragColor.g, fragColor.b, 1.0);
}
else
{
col = push.m_DiffuseColor;
}

col = vec4(fragColor.r, fragColor.g, fragColor.b, 1.0);
}
if (col.w < 0.5)
{
Expand Down
3 changes: 2 additions & 1 deletion engine/platform/Vulkan/systems/VKpbrSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ namespace GfxRenderEngine
{
m_Pipeline->Bind(frameInfo.m_CommandBuffer);

auto view = registry.view<MeshComponent, TransformComponent, PbrMaterialTag, InstanceTag>(entt::exclude<SkeletalAnimationTag>);
auto view = registry.view<MeshComponent, TransformComponent, PbrMaterialTag, InstanceTag>(
entt::exclude<SkeletalAnimationTag>);
for (auto mainInstance : view)
{
auto& mesh = view.get<MeshComponent>(mainInstance);
Expand Down
1 change: 0 additions & 1 deletion engine/renderer/builder/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ namespace GfxRenderEngine
submesh.m_Material.m_PbrMaterial.m_Roughness = 0.5f;
submesh.m_Material.m_PbrMaterial.m_Metallic = 0.1f;
submesh.m_Material.m_PbrMaterial.m_NormalMapIntensity = 1.0f;
submesh.m_Material.m_PbrMaterial.m_Features = Material::HAS_VERTEX_COLOR;

{ // create material descriptor
Material::MaterialTextures materialTextures;
Expand Down
7 changes: 4 additions & 3 deletions engine/renderer/builder/fbxBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ namespace GfxRenderEngine
if (texture)
{
materialTextures[Material::EMISSIVE_MAP_INDEX] = texture;
pbrMaterial.m_Features |= Material::HAS_EMISSIVE_COLOR;
pbrMaterial.m_Features |= Material::HAS_EMISSIVE_MAP;
pbrMaterial.m_EmissiveColor = glm::vec3(1.0f);
}
break;
}
Expand Down Expand Up @@ -441,13 +442,13 @@ namespace GfxRenderEngine

Material& material = m_Materials[materialIndex];

LoadProperties(fbxMaterial, material.m_PbrMaterial);

LoadMap(fbxMaterial, aiTextureType_DIFFUSE, materialIndex);
LoadMap(fbxMaterial, aiTextureType_NORMALS, materialIndex);
LoadMap(fbxMaterial, aiTextureType_SHININESS, materialIndex);
LoadMap(fbxMaterial, aiTextureType_METALNESS, materialIndex);
LoadMap(fbxMaterial, aiTextureType_EMISSIVE, materialIndex);

LoadProperties(fbxMaterial, material.m_PbrMaterial);
}
}

Expand Down
5 changes: 3 additions & 2 deletions engine/renderer/builder/ufbxBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ namespace GfxRenderEngine
float baseFactor = baseFactorMaterialMap.has_value ? baseFactorMaterialMap.value_real : 1.0f;
if (materialMap.texture)
{
std::string filename(materialMap.texture->filename.data);
std::string filename(materialMap.texture->absolute_filename.data);
materialTextures[Material::DIFFUSE_MAP_INDEX] = LoadTexture(filename, Texture::USE_SRGB);
pbrMaterial.m_Features |= Material::HAS_DIFFUSE_MAP;
pbrMaterial.m_DiffuseColor.r = baseFactor;
Expand Down Expand Up @@ -396,8 +396,9 @@ namespace GfxRenderEngine
std::string filename(materialMap.texture->filename.data);
materialTextures[Material::EMISSIVE_MAP_INDEX] = LoadTexture(filename, Texture::USE_SRGB);
pbrMaterial.m_Features |= Material::HAS_EMISSIVE_MAP;
pbrMaterial.m_EmissiveColor = glm::vec3(1.0f);
}

else
{
glm::vec3 emissiveColor(materialMap.value_vec3.x, materialMap.value_vec3.y, materialMap.value_vec3.z);
pbrMaterial.m_EmissiveColor = emissiveColor;
Expand Down
1 change: 0 additions & 1 deletion engine/scene/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ namespace GfxRenderEngine

enum MaterialFeatures // bitset
{
HAS_VERTEX_COLOR = GLSL_HAS_VERTEX_COLOR,
HAS_DIFFUSE_MAP = GLSL_HAS_DIFFUSE_MAP,
HAS_NORMAL_MAP = GLSL_HAS_NORMAL_MAP,
HAS_ROUGHNESS_MAP = GLSL_HAS_ROUGHNESS_MAP,
Expand Down

0 comments on commit 89feffc

Please sign in to comment.