Skip to content

Commit

Permalink
lightmapping: ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
PazerOP committed Jun 17, 2019
1 parent caee81e commit 202558e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 72 deletions.
7 changes: 4 additions & 3 deletions shaderapivulkan/src/TF2Vulkan/IStateManagerVulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TF2Vulkan
{
class IMeshInternal;
struct LogicalShadowState;
struct LogicalDynamicState;

Expand All @@ -19,13 +20,13 @@ namespace TF2Vulkan
virtual VulkanStateID FindOrCreateState(
const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState,
const IMesh& mesh) = 0;
const IMeshInternal& mesh) = 0;

virtual void ApplyState(VulkanStateID stateID, const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh, IVulkanCommandBuffer& buf) = 0;
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh, IVulkanCommandBuffer& buf) = 0;

VulkanStateID ApplyState(const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh,
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh,
IVulkanCommandBuffer& buf)
{
const auto& stateID = FindOrCreateState(staticState, dynamicState, mesh);
Expand Down
118 changes: 66 additions & 52 deletions shaderapivulkan/src/TF2Vulkan/StateManagerVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <stdshader_vulkan/ShaderData.h>

#include <materialsystem/imesh.h>
#include "interface/internal/IMeshInternal.h"

#undef min
#undef max
Expand Down Expand Up @@ -93,20 +93,20 @@ namespace
struct PipelineLayoutKey : DescriptorSetLayoutKey
{
PipelineLayoutKey(const LogicalShadowState& staticState, const LogicalDynamicState& dynamicState,
const IMesh& mesh);
const IMeshInternal& mesh);
DEFAULT_STRONG_ORDERING_OPERATOR(PipelineLayoutKey);

// TODO: Pipeline layout only needs to know shader groups, not specific instances
const IShaderInstanceInternal* m_VSShaderInstance;
VertexFormat m_VSVertexFormat;
std::array<VertexFormat, 2> m_VSVertexFormats;

const IShaderInstanceInternal* m_PSShaderInstance;
};
}

STD_HASH_DEFINITION(PipelineLayoutKey,
v.m_VSShaderInstance,
v.m_VSVertexFormat,
v.m_VSVertexFormats,

v.m_PSShaderInstance
);
Expand All @@ -116,7 +116,7 @@ namespace
struct PipelineKey final : RenderPassKey, PipelineLayoutKey
{
PipelineKey(const LogicalShadowState& staticState, const LogicalDynamicState& dynamicState,
const IMesh& mesh);
const IMeshInternal& mesh);
DEFAULT_STRONG_ORDERING_OPERATOR(PipelineKey);

ShaderDepthFunc_t m_DepthCompareFunc;
Expand Down Expand Up @@ -402,10 +402,10 @@ namespace TF2Vulkan
public:
void ApplyState(VulkanStateID stateID,
const LogicalShadowState& staticState, const LogicalDynamicState& dynamicState,
const IMesh& mesh, IVulkanCommandBuffer& buf) override;
const IMeshInternal& mesh, IVulkanCommandBuffer& buf) override;

VulkanStateID FindOrCreateState(const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh) override;
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh) override;

const DescriptorSet& FindOrCreateDescriptorSet(const DescriptorSetKey& key);
const DescriptorPool& FindOrCreateDescriptorPool(const DescriptorPoolKey& key);
Expand Down Expand Up @@ -705,36 +705,35 @@ static vk::Format ConvertVertexFormat(const ShaderReflection::VertexAttribute& v
return FormatInfo::ConvertDataFormat(componentType, var.m_ComponentCount, componentSize);
}

Pipeline::Pipeline(const PipelineKey& key, const PipelineLayout& layout,
const RenderPass& renderPass) :
m_Layout(&layout),
m_RenderPass(&renderPass)
static void InitVertexInputState(
std::vector<vk::VertexInputAttributeDescription>& attrDescs,
std::vector<vk::VertexInputBindingDescription>& bindDescs,
vk::PipelineVertexInputStateCreateInfo& ci,
const ShaderReflection::ReflectionData& vertexShaderRefl,
const std::array<VertexFormat, 2>& vertexFormats)
{
// Shader stage create info(s)
using VIAD = vk::VertexInputAttributeDescription;
for (size_t streamIndex = 0; streamIndex < vertexFormats.size(); streamIndex++)
{
auto& cis = m_ShaderStageCIs;
cis.emplace_back(*key.m_VSShaderInstance, vk::ShaderStageFlagBits::eVertex);
cis.emplace_back(*key.m_PSShaderInstance, vk::ShaderStageFlagBits::eFragment);
}

// Vertex input state create info
{
auto& attrs = m_VertexInputAttributeDescriptions;

const auto& vertexShaderRefl = key.m_VSShaderInstance->GetGroup().GetVulkanShader().GetReflectionData();
const VertexFormat& stream = vertexFormats[streamIndex];

VertexFormat::Element vertexElements[VERTEX_ELEMENT_NUMELEMENTS];
size_t totalVertexSize;
const auto vertexElementCount = key.m_VSVertexFormat.GetVertexElements(vertexElements, std::size(vertexElements), &totalVertexSize);
using VIAD = vk::VertexInputAttributeDescription;
const auto vertexElementCount = stream.GetVertexElements(vertexElements, std::size(vertexElements), &totalVertexSize);
if (vertexElementCount <= 0)
continue;

if (streamIndex > 0)
DebuggerBreakIfDebugging();

for (uint_fast8_t i = 0; i < vertexElementCount; i++)
{
const auto& vertexElement = vertexElements[i];

VIAD attr;
attr.offset = vertexElement.m_Offset;
attr.format = vertexElement.m_Type->GetVKFormat();
attr.binding = 0;
attr.binding = streamIndex + 1;

static constexpr auto INVALID_LOCATION = std::numeric_limits<uint32_t>::max();
attr.location = INVALID_LOCATION;
Expand All @@ -753,40 +752,55 @@ Pipeline::Pipeline(const PipelineKey& key, const PipelineLayout& layout,
continue;
}

attrs.emplace_back(attr);
attrDescs.emplace_back(attr);
}

for (const auto& input : vertexShaderRefl.m_VertexInputs)
bindDescs.emplace_back(vk::VertexInputBindingDescription(streamIndex + 1, totalVertexSize));
}

// Bind missing vertex input attributes to binding 0 (the fake binding)
for (const auto& input : vertexShaderRefl.m_VertexInputs)
{
bool found = false;
for (auto& attr : attrDescs)
{
bool found = false;
for (auto& attr : attrs)
if (attr.location == input.m_Location)
{
if (attr.location == input.m_Location)
{
found = true;
break;
}
found = true;
break;
}
}

if (found)
continue;
if (found)
continue;

// Otherwise, insert an empty one
//attrs.emplace_back(VIAD(input.m_Location, 1, vk::Format::eR32G32B32A32Sfloat));
attrs.emplace_back(VIAD(input.m_Location, 1, ConvertVertexFormat(input)));
}
// Insert a reference to the fake binding (index 0)
attrDescs.emplace_back(VIAD(input.m_Location, 0, ConvertVertexFormat(input)));
}

auto & binds = m_VertexInputBindingDescriptions;
binds.emplace_back(vk::VertexInputBindingDescription(0, totalVertexSize));
// "Fake" binding with no real data
bindDescs.emplace_back(vk::VertexInputBindingDescription(0, /*0*/sizeof(float) * 4, vk::VertexInputRate::eInstance));

// "Fake" binding with no real data
binds.emplace_back(vk::VertexInputBindingDescription(1, sizeof(float) * 4, vk::VertexInputRate::eInstance));
AttachVector(ci.pVertexAttributeDescriptions, ci.vertexAttributeDescriptionCount, attrDescs);
AttachVector(ci.pVertexBindingDescriptions, ci.vertexBindingDescriptionCount, bindDescs);
}

auto& ci = m_VertexInputStateCI;
AttachVector(ci.pVertexAttributeDescriptions, ci.vertexAttributeDescriptionCount, attrs);
AttachVector(ci.pVertexBindingDescriptions, ci.vertexBindingDescriptionCount, binds);
Pipeline::Pipeline(const PipelineKey& key, const PipelineLayout& layout,
const RenderPass& renderPass) :
m_Layout(&layout),
m_RenderPass(&renderPass)
{
// Shader stage create info(s)
{
auto& cis = m_ShaderStageCIs;
cis.emplace_back(*key.m_VSShaderInstance, vk::ShaderStageFlagBits::eVertex);
cis.emplace_back(*key.m_PSShaderInstance, vk::ShaderStageFlagBits::eFragment);
}

// Vertex input state create info
InitVertexInputState(m_VertexInputAttributeDescriptions, m_VertexInputBindingDescriptions, m_VertexInputStateCI,
key.m_VSShaderInstance->GetGroup().GetVulkanShader().GetReflectionData(), key.m_VSVertexFormats);

// Vertex input assembly state create info
{
auto& ci = m_InputAssemblyStateCI;
Expand Down Expand Up @@ -1258,7 +1272,7 @@ void StateManagerVulkan::ApplyDescriptorSets(const Pipeline& pipeline,
}

void StateManagerVulkan::ApplyState(VulkanStateID id, const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh, IVulkanCommandBuffer& buf)
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh, IVulkanCommandBuffer& buf)
{
LOG_FUNC();
std::lock_guard lock(m_Mutex);
Expand Down Expand Up @@ -1327,7 +1341,7 @@ const PipelineLayout& StateManagerVulkan::FindOrCreatePipelineLayout(const Pipel

VulkanStateID StateManagerVulkan::FindOrCreateState(
const LogicalShadowState& staticState, const LogicalDynamicState& dynamicState,
const IMesh& mesh)
const IMeshInternal& mesh)
{
LOG_FUNC();
std::lock_guard lock(m_Mutex);
Expand All @@ -1348,7 +1362,7 @@ VulkanStateID StateManagerVulkan::FindOrCreateState(
}

PipelineKey::PipelineKey(const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh) :
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh) :

RenderPassKey(staticState, dynamicState),
PipelineLayoutKey(staticState, dynamicState, mesh),
Expand Down Expand Up @@ -1388,11 +1402,11 @@ PipelineKey::PipelineKey(const LogicalShadowState& staticState,
}

PipelineLayoutKey::PipelineLayoutKey(const LogicalShadowState& staticState,
const LogicalDynamicState& dynamicState, const IMesh& mesh) :
const LogicalDynamicState& dynamicState, const IMeshInternal& mesh) :
DescriptorSetLayoutKey(staticState, dynamicState),

m_VSShaderInstance(dynamicState.m_VSShader),
m_VSVertexFormat(mesh.GetVertexFormat()),
m_VSVertexFormats{ VertexFormat(mesh.GetVertexFormat()), mesh.GetColorMeshFormat() },

m_PSShaderInstance(dynamicState.m_PSShader)
{
Expand Down
36 changes: 30 additions & 6 deletions shaderapivulkan/src/TF2Vulkan/meshes/VulkanMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ int VulkanMesh::GetRoomRemaining() const
return Util::algorithm::min(vtxRoom, idxRoom);
}

VertexFormat VulkanMesh::GetColorMeshFormat() const
{
LOG_FUNC_ANYTHREAD();
return m_ColorMesh ? VertexFormat(m_ColorMesh->GetVertexFormat()) : VertexFormat{};
}

void VulkanMesh::OverrideVertexBuffer(IMesh* src)
{
auto lock = ScopeThreadLock();
Expand Down Expand Up @@ -264,26 +270,40 @@ void VulkanMesh::DrawInternal(IVulkanCommandBuffer& cmdBuf, int firstIndex, int
return;
}

vk::Buffer indexBuffer, vertexBuffer;
size_t indexBufferOffset, vertexBufferOffset;
vk::Buffer indexBuffer, vertexBuffer, vertexBufferColor;
size_t indexBufferOffset, vertexBufferOffset, vertexBufferColorOffset = 0; // intentionally only last one assigned

m_IndexBuffer->GetGPUBuffer(indexBuffer, indexBufferOffset);
m_VertexBuffer->GetGPUBuffer(vertexBuffer, vertexBufferOffset);
cmdBuf.bindIndexBuffer(indexBuffer, indexBufferOffset, vk::IndexType::eUint16);

m_VertexBuffer->GetGPUBuffer(vertexBuffer, vertexBufferOffset);
if (m_ColorMesh)
{
auto colorMeshInternal = assert_cast<IMeshInternal*>(m_ColorMesh);
auto& colorVB = colorMeshInternal->GetVertexBuffer();
colorVB.GetGPUBuffer(vertexBufferColor, vertexBufferColorOffset);
vertexBufferColorOffset += m_ColorMeshVertexOffset;
}

// Bind vertex buffers
{
const vk::Buffer vtxBufs[] =
{
vertexBuffer,
g_ShaderDevice.GetDummyVertexBuffer(),
vertexBuffer,
vertexBufferColor,
};
const vk::DeviceSize offsets[] =
{
vertexBufferOffset,
0,
vertexBufferOffset,
Util::SafeConvert<vk::DeviceSize>(vertexBufferColorOffset)
};

const uint32_t vbCount = m_ColorMesh ? 3 : 2;

static_assert(std::size(vtxBufs) == std::size(offsets));
cmdBuf.bindVertexBuffers(0, TF2Vulkan::to_array_proxy(vtxBufs), TF2Vulkan::to_array_proxy(offsets));
cmdBuf.bindVertexBuffers(0, vk::ArrayProxy(vbCount, vtxBufs), vk::ArrayProxy(vbCount, offsets));
}

cmdBuf.drawIndexed(Util::SafeConvert<uint32_t>(indexCount), 1, Util::SafeConvert<uint32_t>(firstIndex));
Expand All @@ -295,6 +315,7 @@ void VulkanMesh::SetColorMesh(IMesh* colorMesh, int vertexOffset)
{
auto lock = ScopeThreadLock();
LOG_FUNC_ANYTHREAD();

m_ColorMesh = colorMesh;
m_ColorMeshVertexOffset = vertexOffset;
}
Expand All @@ -305,6 +326,7 @@ void VulkanMesh::Draw(CPrimList* lists, int listCount)
LOG_FUNC();

// TODO: Indirect rendering?
//assert(listCount == 1);
for (int i = 0; i < listCount; i++)
Draw(lists[i].m_FirstIndex, lists[i].m_NumIndices);
}
Expand Down Expand Up @@ -405,6 +427,8 @@ VulkanGPUBuffer::VulkanGPUBuffer(bool isDynamic, vk::BufferUsageFlags usage) :

void VulkanGPUBuffer::GetGPUBuffer(vk::Buffer& buffer, size_t& offset)
{
auto lock = ScopeThreadLock();

if (IsDynamic())
UpdateDynamicBuffer();

Expand Down
Loading

0 comments on commit 202558e

Please sign in to comment.