Skip to content

Commit

Permalink
About to switch shader constant buffer organization
Browse files Browse the repository at this point in the history
  • Loading branch information
PazerOP committed Jun 8, 2019
1 parent 9625234 commit 0af6264
Show file tree
Hide file tree
Showing 38 changed files with 818 additions and 440 deletions.
2 changes: 2 additions & 0 deletions TF2VulkanUtil/TF2VulkanUtil.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<ClInclude Include="include\TF2Vulkan\Util\color.h" />
<ClInclude Include="include\TF2Vulkan\Util\ConstexprCRC.h" />
<ClInclude Include="include\TF2Vulkan\Util\ConstexprRandom.h" />
<ClInclude Include="include\TF2Vulkan\Util\CStringBuilder.h" />
<ClInclude Include="include\TF2Vulkan\Util\DirtyVar.h" />
<ClInclude Include="include\TF2Vulkan\Util\Enums.h" />
<ClInclude Include="include\TF2Vulkan\Util\ForwardingHelpers.h" />
Expand All @@ -118,6 +119,7 @@
<ClInclude Include="include\TF2Vulkan\Util\std_compare.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_stdio.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_string.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_string_view.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_type_traits.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_utility.h" />
<ClInclude Include="include\TF2Vulkan\Util\std_variant.h" />
Expand Down
46 changes: 46 additions & 0 deletions TF2VulkanUtil/include/TF2Vulkan/Util/CStringBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <string_view>

namespace Util
{
// Can't use stringstream due to linking issues
template<size_t bufSize, typename TChar = char>
class CStringBuilder
{
public:
CStringBuilder(const std::basic_string_view<TChar>& initialValue = {})
{
Append(initialValue);
}

const TChar* c_str()
{
if (m_Length >= bufSize)
throw std::overflow_error("Ran out of space when trying to add null terminator");

m_Buffer[m_Length] = '\0';
return m_Buffer;
}

std::basic_string_view<TChar> GetBuffer() const { return std::basic_string_view<TChar>(m_Buffer, m_Length); }

void Append(const std::basic_string_view<TChar>& sv)
{
memcpy_s(&m_Buffer[m_Length], sizeof(TChar) * (bufSize - m_Length), sv.data(), sizeof(TChar) * sv.size());
m_Length += sv.size();
}

template<typename... TArgs>
__declspec(noinline) void AppendF(const char* fmt, const TArgs& ... args)
{
auto written = sprintf_s(&m_Buffer[m_Length], bufSize - m_Length, fmt, args...);
if (written > 0)
m_Length += written;
}

private:
TChar m_Buffer[bufSize];
size_t m_Length = 0;
};
}
16 changes: 16 additions & 0 deletions TF2VulkanUtil/include/TF2Vulkan/Util/std_algorithm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#undef min
#undef max

#include <algorithm>

namespace Util{ namespace algorithm
Expand Down Expand Up @@ -83,4 +86,17 @@ namespace Util{ namespace algorithm
{
std::copy(std::begin(src), std::end(src), dest);
}

template<typename T1, typename T2>
inline constexpr auto max(const T1& t1, const T2& t2)
{
using CT = std::common_type_t<T1, T2>;
return std::max<CT>(CT(t1), CT(t2));
}
template<typename T1, typename T2>
inline constexpr auto min(const T1& t1, const T2& t2)
{
using CT = std::common_type_t<T1, T2>;
return std::min<CT>(CT(t1), CT(t2));
}
} }
18 changes: 18 additions & 0 deletions TF2VulkanUtil/include/TF2Vulkan/Util/std_string_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string_view>

#include "std_compare.h"

#ifndef __INTELLISENSE__
template<typename TChar, typename TTraits>
inline std::strong_ordering operator<=>(const std::basic_string_view<TChar, TTraits>& lhs, const std::basic_string_view<TChar, TTraits>& rhs)
{
if (lhs < rhs)
return std::strong_ordering::less;
else if (rhs < lhs)
return std::strong_ordering::greater;
else
return std::strong_ordering::equal;
}
#endif
14 changes: 14 additions & 0 deletions shaderapivulkan/include/TF2Vulkan/IShaderDynamicNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace TF2Vulkan
{
union SamplerSettings;

namespace Shaders
{
struct VSModelMatrices;
Expand Down Expand Up @@ -41,6 +43,9 @@ namespace TF2Vulkan
virtual int GetCurrentNumBones() const = 0;
virtual void LoadBoneMatrices(Shaders::VSModelMatrices& bones) const = 0;

virtual void BindSamplers(const SamplerSettings* begin, const SamplerSettings* end, bool merge, uint32_t first) = 0;
virtual void BindTextures(const ITexture* const* begin, const ITexture* const* end, bool merge, uint32_t first) = 0;

// Helpers
void SetPixelShader(const IShaderInstance* instance) { SetShaderInstance(ShaderType::Pixel, instance); }
void SetPixelShader(const IShaderInstance& instance) { SetShaderInstance(ShaderType::Pixel, &instance); }
Expand All @@ -59,6 +64,15 @@ namespace TF2Vulkan
return GetLights(lights, size);
}

void BindSamplers(const SamplerSettings* begin, const SamplerSettings* end)
{
return BindSamplers(begin, end, false, 0);
}
void BindTextures(const ITexture* const* begin, const ITexture* const* end)
{
return BindTextures(begin, end, false, 0);
}

protected:
};

Expand Down
52 changes: 41 additions & 11 deletions shaderapivulkan/include/TF2Vulkan/ISpecConstLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

#include "AlignedTypes.h"

#include <TF2Vulkan/Util/std_string_view.h>
#include <TF2Vulkan/Util/std_utility.h>

#include <string_view>

namespace TF2Vulkan
{
enum class SpecConstType
Expand Down Expand Up @@ -37,31 +36,62 @@ namespace TF2Vulkan
SpecConstType m_Type;
size_t m_Offset;

bool operator!=(const SpecConstLayoutEntry& other) const noexcept { return !operator==(other); }
bool operator==(const SpecConstLayoutEntry& other) const noexcept
{
return
m_Name == other.m_Name &&
m_Type == other.m_Type &&
m_Offset == other.m_Offset;
}
DEFAULT_STRONG_ORDERING_OPERATOR(SpecConstLayoutEntry);
};

struct SpecConstLayoutCreateInfo final
{
const SpecConstLayoutEntry* m_Entries = nullptr;
size_t m_EntryCount = 0;

#ifndef __INTELLISENSE__
std::strong_ordering operator<=>(const SpecConstLayoutCreateInfo& other) const noexcept
{
if (auto result = m_EntryCount <=> other.m_EntryCount; !std::is_eq(result))
return result;

for (size_t i = 0; i < m_EntryCount; i++)
{
if (auto result = m_Entries[i] <=> other.m_Entries[i]; !std::is_eq(result))
return result;
}

return std::strong_ordering::equal;
}
#endif

auto begin() const { return m_Entries; }
auto end() const { return m_Entries + m_EntryCount; }
};

class ISpecConstLayout
{
public:
virtual const SpecConstLayoutEntry* GetEntries(size_t& count) const = 0;
virtual const SpecConstLayoutCreateInfo& GetCreateInfo() const = 0;
virtual size_t GetBufferSize() const = 0;

const SpecConstLayoutEntry* GetEntries(size_t& count) const
{
auto& ci = GetCreateInfo();
count = ci.m_EntryCount;
return ci.m_Entries;
}
};

#define SPEC_CONST_BUF_ENTRY(type, name) \
::TF2Vulkan::SpecConstLayoutEntry name{ #name, GetSpecConstType<decltype(type::name)>(), offsetof(type, name) }

struct BaseSpecConstBuf
{
Shaders::uint1 TEXTURE2D_COUNT = 0;
Shaders::uint1 SAMPLER_COUNT = 0;
};

template<typename T> struct BaseSpecConstLayout
{
SPEC_CONST_BUF_ENTRY(T, TEXTURE2D_COUNT);
SPEC_CONST_BUF_ENTRY(T, SAMPLER_COUNT);
};
}

STD_HASH_DEFINITION(TF2Vulkan::SpecConstLayoutEntry,
Expand Down
101 changes: 101 additions & 0 deletions shaderapivulkan/include/TF2Vulkan/ShaderResourceBinder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once

#include "AlignedTypes.h"
#include "TF2Vulkan/SamplerSettings.h"
#include "TF2Vulkan/Util/InPlaceVector.h"
#include "TF2Vulkan/Util/SafeConvert.h"

#include <materialsystem/itexture.h>
#include <vtf/vtf.h>

namespace TF2Vulkan
{
static constexpr size_t MAX_SHADER_RESOURCE_BINDINGS = 32;

class ShaderTextureBinder final
{
public:
void AddBinding(ITexture& texture, int32_t& texLocationSpecConst, uint32_t& smpLocationSpecConst,
const SamplerSettings* sampler = nullptr);

const auto& GetTextures() const { return m_Textures; }
const auto& GetSamplers() const { return m_Samplers; }

private:
int32_t FindOrInsertTexture(ITexture& texture);
uint32_t FindOrInsertSampler(const SamplerSettings& sampler);
static SamplerSettings SamplerFromTexture(const ITexture& texture);

Util::InPlaceVector<ITexture*, MAX_SHADER_RESOURCE_BINDINGS> m_Textures;
Util::InPlaceVector<SamplerSettings, MAX_SHADER_RESOURCE_BINDINGS> m_Samplers;
};

inline void ShaderTextureBinder::AddBinding(ITexture& texture, int32_t& texLocationSpecConst,
uint32_t& smpLocationSpecConst, const SamplerSettings* sampler)
{
texLocationSpecConst = FindOrInsertTexture(texture);
smpLocationSpecConst = FindOrInsertSampler(sampler ? *sampler : SamplerFromTexture(texture));
}

inline int32_t ShaderTextureBinder::FindOrInsertTexture(ITexture& texture)
{
for (size_t i = 0; i < m_Textures.size(); i++)
{
if (m_Textures[i] == &texture)
return Util::SafeConvert<int32_t>(i);
}

m_Textures.emplace_back(&texture);
return Util::SafeConvert<int32_t>(m_Textures.size() - 1);
}

inline uint32_t ShaderTextureBinder::FindOrInsertSampler(const SamplerSettings& sampler)
{
for (uint32_t i = 0; i < m_Samplers.size(); i++)
{
if (m_Samplers[i] == sampler)
return i;
}

m_Samplers.emplace_back(sampler);
return m_Samplers.size() - 1;
}

inline SamplerSettings ShaderTextureBinder::SamplerFromTexture(const ITexture& tex)
{
const auto flags = tex.GetFlags();

SamplerSettings retVal;

// This code relies on the constructor for SamplerSettings initializing
// these to SHADER_TEXWRAPMODE_REPEAT
assert(retVal.m_WrapS == SHADER_TEXWRAPMODE_REPEAT);
assert(retVal.m_WrapT == SHADER_TEXWRAPMODE_REPEAT);
assert(retVal.m_WrapU == SHADER_TEXWRAPMODE_REPEAT);

if (flags & TEXTUREFLAGS_CLAMPS)
retVal.m_WrapS = SHADER_TEXWRAPMODE_CLAMP;
if ((flags & TEXTUREFLAGS_CLAMPT) && tex.GetActualHeight() > 1)
retVal.m_WrapT = SHADER_TEXWRAPMODE_CLAMP;
if ((flags & TEXTUREFLAGS_CLAMPU) && tex.GetActualDepth() > 1)
retVal.m_WrapU = SHADER_TEXWRAPMODE_CLAMP;

if (flags & TEXTUREFLAGS_POINTSAMPLE)
{
retVal.m_MinFilter = retVal.m_MagFilter =
SHADER_TEXFILTERMODE_NEAREST;
}
else if (flags & TEXTUREFLAGS_TRILINEAR)
{
retVal.m_MinFilter = retVal.m_MagFilter =
SHADER_TEXFILTERMODE_LINEAR_MIPMAP_LINEAR;
}
else if (flags & TEXTUREFLAGS_ANISOTROPIC)
{
retVal.m_MinFilter = retVal.m_MagFilter =
SHADER_TEXFILTERMODE_ANISOTROPIC;
}

return retVal;
}
}
41 changes: 0 additions & 41 deletions shaderapivulkan/include/TF2Vulkan/ShaderTextureBinder.h

This file was deleted.

5 changes: 2 additions & 3 deletions shaderapivulkan/shaderapivulkan.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@
<ClInclude Include="include\TF2Vulkan\IShaderShadowNext.h" />
<ClInclude Include="include\TF2Vulkan\ISpecConstLayout.h" />
<ClInclude Include="include\TF2Vulkan\IBufferPool.h" />
<ClInclude Include="include\TF2Vulkan\ShaderTextureBinder.h" />
<ClInclude Include="include\TF2Vulkan\ShaderResourceBinder.h" />
<ClInclude Include="include\TF2Vulkan\TextureSubrect.h" />
<ClInclude Include="src\interface\IMaterialInternal.h" />
<ClInclude Include="src\interface\internal\IDebugTextureInfoInternal.h" />
<ClInclude Include="src\interface\internal\IMeshInternal.h" />
<ClInclude Include="src\interface\internal\IShaderDeviceInternal.h" />
<ClInclude Include="src\interface\internal\IShaderInternal.h" />
<ClInclude Include="src\interface\internal\IStateManagerDynamic.h" />
<ClInclude Include="src\interface\internal\IBufferPoolInternal.h" />
<ClInclude Include="src\interface\internal\IVBAllocTrackerInternal.h" />
<ClInclude Include="src\interface\ITextureInternal.h" />
Expand All @@ -132,7 +131,7 @@
<ClInclude Include="src\interface\internal\IShaderAPIInternal.h" />
<ClInclude Include="src\TF2Vulkan\RenderDocAPI.h" />
<ClInclude Include="src\TF2Vulkan\ResourceBlob.h" />
<ClInclude Include="src\TF2Vulkan\SamplerSettings.h" />
<ClInclude Include="include\TF2Vulkan\SamplerSettings.h" />
<ClInclude Include="src\interface\internal\IShaderDeviceMgrInternal.h" />
<ClInclude Include="src\TF2Vulkan\shaders\VulkanShaderManager.h" />
<ClInclude Include="src\TF2Vulkan\IShaderAPI\IShaderAPI_StateManagerDynamic.h" />
Expand Down
Loading

0 comments on commit 0af6264

Please sign in to comment.