Skip to content

Commit

Permalink
Sorta gamma correct rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
PazerOP committed May 25, 2019
1 parent 9c7f46b commit 59f2f83
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 21 deletions.
3 changes: 3 additions & 0 deletions TF2Vulkan.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Inactive Projects", "Inactive Projects", "{2B33EDE1-32E5-4408-848E-D5BEBEC7523F}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ShaderDebuggingEnv", "ShaderDebuggingEnv\ShaderDebuggingEnv.vcxproj", "{7B521A4D-BB8E-466F-9E8B-EBE3FB1BE919}"
ProjectSection(ProjectDependencies) = postProject
{BCAA3EF6-6521-4314-A301-BF1D2FB740C5} = {BCAA3EF6-6521-4314-A301-BF1D2FB740C5}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
28 changes: 18 additions & 10 deletions shaderapivulkan/include/TF2Vulkan/AlignedTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ namespace TF2Vulkan{ namespace Shaders
};

template<typename T, int sizeX, int sizeY> struct matrix;
template<typename T, size_t size> struct vector;
template<typename T, size_t size, size_t alignment> struct vector;

namespace detail
{
static constexpr bool IsVector(const void*) { return false; }
template<typename T, size_t size> static constexpr bool IsVector(const vector<T, size>*) { return true; }
template<typename T, size_t size, size_t alignment> static constexpr bool IsVector(const vector<T, size, alignment>*) { return true; }
template<typename T> static constexpr bool is_vector_v = IsVector((T*)nullptr);
template<typename T> static constexpr bool is_vector_or_scalar_v = is_vector_v<T> || std::is_arithmetic_v<T>;

Expand All @@ -41,36 +41,42 @@ namespace TF2Vulkan{ namespace Shaders
template<typename T, int sizeY, int sizeX> static constexpr auto GetMatrixSizeX(const matrix<T, sizeX, sizeY>&) { return sizeX; }
template<typename T, int sizeY, int sizeX> static constexpr auto GetMatrixSizeY(const matrix<T, sizeX, sizeY>&) { return sizeY; }

template<typename T, size_t elements> struct vector_storage;
template<typename T, size_t elements, size_t alignment> struct vector_storage;

template<typename T> struct alignas(sizeof(T)) vector_storage<T, 1>
template<typename T, size_t alignment> struct alignas(alignment) vector_storage<T, 1, alignment>
{
T x;
};
template<typename T> struct alignas(sizeof(T) * 2) vector_storage<T, 2>
template<typename T, size_t alignment> struct alignas(alignment) vector_storage<T, 2, alignment>
{
T x;
T y;
};
template<typename T> struct vector_storage<T, 3>
template<typename T, size_t alignment> struct alignas(alignment) vector_storage<T, 3, alignment>
{
T x;
T y;
T z;
};
template<typename T> struct alignas(sizeof(T) * 4) vector_storage<T, 4>
template<typename T, size_t alignment> struct alignas(alignment) vector_storage<T, 4, alignment>
{
T x;
T y;
T z;
T w;
};

template<typename T, size_t size> static constexpr size_t GetDefaultAlignment()
{
return sizeof(T) * (size == 3 ? 1 : size);
}
}

template<typename T, size_t size> struct vector : detail::vector_storage<T, size>
template<typename T, size_t size, size_t alignment = detail::GetDefaultAlignment<T, size>()>
struct vector : detail::vector_storage<T, size, alignment>
{
private:
using BaseType = detail::vector_storage<T, size>;
using BaseType = detail::vector_storage<T, size, alignment>;

template<typename TArg, typename... TInit>
static constexpr BaseType InitBaseType(const TArg& arg1, const TInit&... args)
Expand Down Expand Up @@ -101,7 +107,7 @@ namespace TF2Vulkan{ namespace Shaders
public:
static_assert(size >= 1 && size <= 4);

using ThisType = vector<T, size>;
using ThisType = vector<T, size, alignment>;
constexpr vector() = default;
constexpr vector(const ThisType&) noexcept = default;
constexpr vector(ThisType&&) noexcept = default;
Expand Down Expand Up @@ -319,6 +325,8 @@ namespace TF2Vulkan{ namespace Shaders
using float3 = vector<float, 3>;
using float4 = vector<float, 4>;

using float3_aligned = vector<float, 3, 16>;

static_assert(sizeof(float1) == sizeof(float) * 1);
static_assert(sizeof(float2) == sizeof(float) * 2);
static_assert(sizeof(float3) == sizeof(float) * 3);
Expand Down
4 changes: 3 additions & 1 deletion shaderapivulkan/include/TF2Vulkan/IShaderDynamicNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ namespace TF2Vulkan
virtual void SetShaderInstance(ShaderType type, const IShaderInstance* instance) = 0;
virtual void BindUniformBuffer(const BufferPoolEntry& buf, UniformBufferIndex index) = 0;

[[nodiscard]] virtual size_t GetLights(LightDesc_t* lights, size_t maxLights) const = 0;
virtual void GetAmbientLightCube(Shaders::AmbientLightCube& cube) const = 0;

// From IShaderDynamicAPI
virtual void BindStandardTexture(Sampler_t sampler, StandardTextureId_t id) = 0;
virtual void GetWorldSpaceCameraPosition(Vector& pPos) const = 0;
virtual void GetMatrix(MaterialMatrixMode_t matrixMode, VMatrix& dst) const = 0;
[[nodiscard]] virtual size_t GetLights(LightDesc_t* lights, size_t maxLights) const = 0;

virtual int GetCurrentNumBones() const = 0;
virtual void LoadBoneMatrices(Shaders::VSModelMatrices& bones) const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ void IShaderAPI_StateManagerDynamic::SetAmbientLightCube(Vector4D cube[6])
*reinterpret_cast<const std::array<Vector4D, 6>*>(cube), m_Dirty);
}

void IShaderAPI_StateManagerDynamic::GetAmbientLightCube(Shaders::AmbientLightCube& cube) const
{
LOG_FUNC();

cube.x[0].SetFrom(m_State.m_LightAmbientCube[0].Base());
cube.x[1].SetFrom(m_State.m_LightAmbientCube[1].Base());
cube.y[0].SetFrom(m_State.m_LightAmbientCube[2].Base());
cube.y[1].SetFrom(m_State.m_LightAmbientCube[3].Base());
cube.z[0].SetFrom(m_State.m_LightAmbientCube[4].Base());
cube.z[1].SetFrom(m_State.m_LightAmbientCube[5].Base());
}

void IShaderAPI_StateManagerDynamic::CullMode(MaterialCullMode_t mode)
{
LOG_FUNC();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace TF2Vulkan
void SetLightingOrigin(Vector lightingOrigin) override final { NOT_IMPLEMENTED_FUNC(); }
void SetAmbientLight(float r, float g, float b) override final { NOT_IMPLEMENTED_FUNC(); }
void SetAmbientLightCube(Vector4D cube[6]) override final;
void GetAmbientLightCube(Shaders::AmbientLightCube& cube) const override final;
void SetPixelShaderStateAmbientLightCube(int pshReg, bool forceToBlack) override final;
void CommitPixelShaderLighting(int pshReg) override final;
void GetDX9LightState(LightState_t* state) const override final;
Expand Down
2 changes: 1 addition & 1 deletion shaderapivulkan/src/TF2Vulkan/StateManagerVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ Pipeline::Pipeline(const PipelineKey& key, const PipelineLayout& layout,
auto& attrs = m_VertexInputAttributeDescriptions;

const auto& vertexShaderRefl = key.m_VSShaderGroup->GetVulkanShader().GetReflectionData();

VertexFormat::Element vertexElements[VERTEX_ELEMENT_NUMELEMENTS];
size_t totalVertexSize;
const auto vertexElementCount = key.m_VSVertexFormat.GetVertexElements(vertexElements, std::size(vertexElements), &totalVertexSize);
Expand Down
13 changes: 10 additions & 3 deletions stdshader_vulkan/include/stdshader_vulkan/ShaderDataShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <TF2Vulkan/Util/std_compare.h>
namespace TF2Vulkan{ namespace Shaders
{
#define FLOAT3_ARRAY(name, size) float3_aligned name[size];
#else
#define FLOAT3_ARRAY(name, size) float3 name[size]
#endif // __cplusplus

struct LightInfo
Expand Down Expand Up @@ -49,11 +52,15 @@ namespace TF2Vulkan{ namespace Shaders

struct AmbientLightCube
{
float3 x[2];
float3 y[2];
float3 z[2];
FLOAT3_ARRAY(x, 2);
FLOAT3_ARRAY(y, 2);
FLOAT3_ARRAY(z, 2);
};

#ifdef __cplusplus
static_assert(sizeof(AmbientLightCube) == 96);
#endif

struct FogParams
{
#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions stdshader_vulkan/src/shaders/BaseShaderNext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ void BaseShaderNext::LoadLights(ShaderDataCommon& data) const

lightOut.atten.Set(lightIn.m_Attenuation0, lightIn.m_Attenuation1, lightIn.m_Attenuation2);
}

dynamic->GetAmbientLightCube(data.m_AmbientCube);
}

void BaseShaderNext::OnDrawElements(IMaterialVar** params, IShaderShadow* pShaderShadow,
Expand Down
2 changes: 1 addition & 1 deletion stdshader_vulkan/src/shaders/HLSL/common_fxc.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[[vk::constant_id(6)]] const bool SEAMLESS_DETAIL = false;
[[vk::constant_id(7)]] const bool SEPARATE_DETAIL_UVS = false;
[[vk::constant_id(8)]] const bool DECAL = false;
[[vk::constant_id(9)]] const bool DONT_GAMMA_CONVERT_VERTEX_COLOR = false;
[[vk::constant_id(9)]] const bool GAMMA_CONVERT_VERTEX_COLOR = true;

[[vk::constant_id(10)]] const bool DYNAMIC_LIGHT = false;
[[vk::constant_id(11)]] const bool STATIC_LIGHT_VERTEX = false;
Expand Down
11 changes: 11 additions & 0 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
[[vk::constant_id(SPEC_CONST_ID_BASE + 1)]] const bool TEXACTIVE_BASETEXTURE = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 2)]] const bool TEXACTIVE_BUMPMAP = false;

#if false // TODO: Array-based textures
[[vk::constant_id(SPEC_CONST_ID_BASE + 3)]] const uint TEXTURE_COUNT = 1;
[[vk::constant_id(SPEC_CONST_ID_BASE + 4)]] const uint SAMPLER_COUNT = 1;

[[vk::constant_id(SPEC_CONST_ID_BASE + 5)]] const uint TEXINDEX_BASE = 0;
[[vk::constant_id(SPEC_CONST_ID_BASE + 5)]] const uint SMPINDEX_BASE = 0;

[[vk::binding(100)]] Texture2D g_Textures[TEXTURE_COUNT];
[[vk::binding(100)]] SamplerState g_Samplers[SAMPLER_COUNT];
#endif

[[vk::binding(0)]] Texture2D BaseTexture;
[[vk::binding(0)]] SamplerState BaseTextureSampler;

Expand Down
2 changes: 1 addition & 1 deletion stdshader_vulkan/src/shaders/HLSL/xlitgeneric.frag.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ float4 main(const PS_INPUT i) : SV_Target
//if (DIFFUSELIGHTING)
// return float4(diffuseColor, 1);

const float3 finalColor = diffuseColor.rgb * baseTextureColor.rgb;
const float3 finalColor = LinearToGamma(diffuseColor.rgb) * baseTextureColor.rgb;
const float finalAlpha = lerp(baseTextureColor.a, baseTextureColor.a * i.color.a, g_fVertexAlpha);
return float4(finalColor, finalAlpha);
}
2 changes: 1 addition & 1 deletion stdshader_vulkan/src/shaders/HLSL/xlitgeneric.vert.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ VS_OUTPUT main(const VS_INPUT v)
if (VERTEXCOLOR)
{
// Assume that this is unlitgeneric if you are using vertex color.
o.color.rgb = (DONT_GAMMA_CONVERT_VERTEX_COLOR) ? v.vColor.rgb : GammaToLinear(v.vColor.rgb);
o.color.rgb = GAMMA_CONVERT_VERTEX_COLOR ? GammaToLinear(v.vColor.rgb) : v.vColor.rgb;
o.color.a = v.vColor.a;
}
else
Expand Down
6 changes: 3 additions & 3 deletions stdshader_vulkan/src/shaders/XLitGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ inline namespace XLitGeneric
bool32 SKINNING;
bool32 COMPRESSED_VERTS;
bool32 DIFFUSELIGHTING;
bool32 DONT_GAMMA_CONVERT_VERTEX_COLOR;
bool32 GAMMA_CONVERT_VERTEX_COLOR;

bool32 DYNAMIC_LIGHT;

Expand All @@ -94,7 +94,7 @@ inline namespace XLitGeneric
SPEC_CONST_BUF_ENTRY(SpecConstBuf, SKINNING);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, COMPRESSED_VERTS);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, DIFFUSELIGHTING);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, DONT_GAMMA_CONVERT_VERTEX_COLOR);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, GAMMA_CONVERT_VERTEX_COLOR);

SPEC_CONST_BUF_ENTRY(SpecConstBuf, DYNAMIC_LIGHT);

Expand Down Expand Up @@ -502,7 +502,7 @@ void Shader::OnDrawElements(const OnDrawElementsParams& params)
const bool bHasNormal = IsPC() || bVertexLitGeneric || bHasEnvmap || bHasFlashlight || bSeamlessBase || bSeamlessDetail;

const bool bSRGBWrite = !params[LINEARWRITE]->GetBoolValue();
drawParams.m_SpecConsts.DONT_GAMMA_CONVERT_VERTEX_COLOR = !bSRGBWrite && bHasVertexColor;
drawParams.m_SpecConsts.GAMMA_CONVERT_VERTEX_COLOR = !(!bSRGBWrite && bHasVertexColor);

static constexpr auto SAMPLER_BASETEXTURE = SHADER_SAMPLER0;
static constexpr auto SAMPLER_BUMPMAP = SHADER_SAMPLER1;
Expand Down

0 comments on commit 59f2f83

Please sign in to comment.