Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dpjudas/VkDoom into gzdoo…
Browse files Browse the repository at this point in the history
…m_unstable
  • Loading branch information
nashmuhandes committed Jan 23, 2025
2 parents 948cd13 + 1d2a496 commit f182936
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 35 deletions.
5 changes: 4 additions & 1 deletion libraries/ZVulkan/src/vulkaninstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,13 @@ VkBool32 VulkanInstance::DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT me
if (parts.size() == 3)
{
msg = parts[2];
size_t pos = msg.find(" The Vulkan spec states:");
size_t pos = msg.find("The Vulkan spec states:");
if (pos != std::string::npos)
msg = msg.substr(0, pos);

while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r' || msg.back() == ' '))
msg.pop_back();

if (callbackData->objectCount > 0)
{
msg += " (";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void VkDescriptorSetManager::Init()
.AddBuffer(RSBuffer.Set.get(), 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, rsbuffers->Viewpoint.UBO.get(), 0, sizeof(HWViewpointUniforms))
.AddBuffer(RSBuffer.Set.get(), 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, rsbuffers->MatrixBuffer->UBO(), 0, sizeof(MatricesUBO))
.AddBuffer(RSBuffer.Set.get(), 2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, rsbuffers->SurfaceUniformsBuffer->UBO(), 0, sizeof(SurfaceUniformsUBO))
.AddBuffer(RSBuffer.Set.get(), 3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, rsbuffers->Lightbuffer.SSO.get(), 0, sizeof(LightBufferSSO))
.AddBuffer(RSBuffer.Set.get(), 3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, rsbuffers->Lightbuffer.SSO.get(), 0, sizeof(LightBufferSSO))
.AddBuffer(RSBuffer.Set.get(), 4, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, rsbuffers->Fogballbuffer.UBO.get(), 0, sizeof(FogballBufferUBO))
.AddBuffer(RSBuffer.Set.get(), 5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, rsbuffers->Bonebuffer.SSO.get())
.Execute(fb->GetDevice());
Expand Down Expand Up @@ -263,7 +263,7 @@ void VkDescriptorSetManager::CreateRSBufferLayout()
.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(4, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT)
.AddBinding(5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT)
.DebugName("VkDescriptorSetManager.RSBuffer.Layout")
Expand Down
12 changes: 1 addition & 11 deletions src/common/rendering/vulkan/pipelines/vk_renderpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ std::unique_ptr<VulkanPipeline> VkRenderPassSetup::CreatePipeline(const VkPipeli
if (program->frag)
builder.AddFragmentShader(program->frag.get());

const VkVertexFormat &vfmt = *fb->GetRenderPassManager()->GetVertexFormat(key.VertexFormat);
const VkVertexFormat &vfmt = *fb->GetRenderPassManager()->GetVertexFormat(key.ShaderKey.VertexFormat);

for (int i = 0; i < vfmt.BufferStrides.size(); i++)
builder.AddVertexBufferBinding(i, vfmt.BufferStrides[i]);
Expand All @@ -325,20 +325,10 @@ std::unique_ptr<VulkanPipeline> VkRenderPassSetup::CreatePipeline(const VkPipeli
VK_FORMAT_R32_SINT
};

bool inputLocations[VATTR_MAX] = {};

for (size_t i = 0; i < vfmt.Attrs.size(); i++)
{
const auto &attr = vfmt.Attrs[i];
builder.AddVertexAttribute(attr.location, attr.binding, vkfmts[attr.format], attr.offset);
inputLocations[attr.location] = true;
}

// Vulkan requires an attribute binding for each location specified in the shader
for (int i = 0; i < VATTR_MAX; i++)
{
if (!inputLocations[i])
builder.AddVertexAttribute(i, 0, i != 8 ? VK_FORMAT_R32G32B32_SFLOAT : VK_FORMAT_R8G8B8A8_UINT, 0);
}

builder.AddDynamicState(VK_DYNAMIC_STATE_VIEWPORT);
Expand Down
10 changes: 5 additions & 5 deletions src/common/rendering/vulkan/pipelines/vk_renderpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ class VkPipelineKey
uint64_t AsQWORD = 0;
};

int VertexFormat = 0;
int Padding0 = 0;
int Padding1 = 0;
int Padding2 = 0;

VkShaderKey ShaderKey;
FRenderStyle RenderStyle;

int Padding1 = 0; // for 64 bit alignment
int Padding3 = 0; // for 64 bit alignment

bool operator<(const VkPipelineKey &other) const { return memcmp(this, &other, sizeof(VkPipelineKey)) < 0; }
bool operator==(const VkPipelineKey &other) const { return memcmp(this, &other, sizeof(VkPipelineKey)) == 0; }
bool operator!=(const VkPipelineKey &other) const { return memcmp(this, &other, sizeof(VkPipelineKey)) != 0; }
};

static_assert(sizeof(FRenderStyle) == 4, "sizeof(FRenderStyle) is not its expected size!");
static_assert(sizeof(VkShaderKey) == 16, "sizeof(VkShaderKey) is not its expected size!");
static_assert(sizeof(VkPipelineKey) == 16 + 16 + 8, "sizeof(VkPipelineKey) is not its expected size!"); // If this assert fails, the flags union no longer adds up to 64 bits. Or there are gaps in the class so the memcmp doesn't work.
static_assert(sizeof(VkShaderKey) == 24, "sizeof(VkShaderKey) is not its expected size!");
static_assert(sizeof(VkPipelineKey) == 16 + 24 + 8, "sizeof(VkPipelineKey) is not its expected size!"); // If this assert fails, the flags union no longer adds up to 64 bits. Or there are gaps in the class so the memcmp doesn't work.

class VkRenderPassKey
{
Expand Down
42 changes: 30 additions & 12 deletions src/common/rendering/vulkan/shaders/vk_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "vk_shader.h"
#include "vk_ppshader.h"
#include "vulkan/vk_renderdevice.h"
#include "vulkan/pipelines/vk_renderpass.h"
#include <zvulkan/vulkanbuilders.h>
#include "hw_shaderpatcher.h"
#include "filesystem.h"
Expand Down Expand Up @@ -224,7 +225,17 @@ static std::vector<BuiltinFieldDesc> fragShaderOutputs
{"FragNormal", "", UniformType::Vec4, FieldCondition::GBUFFER_PASS}, //2
};

void AddFields(FString &layoutBlock, int &index, bool is_in, const std::vector<VaryingFieldDesc> &fields)
static void AddVertexInFields(VulkanRenderDevice* fb, FString& layoutBlock, const VkShaderKey& key)
{
const VkVertexFormat& vfmt = *fb->GetRenderPassManager()->GetVertexFormat(key.VertexFormat);
for (const FVertexBufferAttribute& attr : vfmt.Attrs)
{
const VaryingFieldDesc& desc = vertexShaderInputs[attr.location];
layoutBlock.AppendFormat("layout(location = %d) %s %s %s %s;\n", attr.location, desc.Property.GetChars(), "in", GetTypeStr(desc.Type), desc.Name.GetChars());
}
}

static void AddFields(FString &layoutBlock, int &index, bool is_in, const std::vector<VaryingFieldDesc> &fields)
{
for(auto &field : fields)
{
Expand All @@ -233,7 +244,7 @@ void AddFields(FString &layoutBlock, int &index, bool is_in, const std::vector<V
}
}

void AddBuiltinFields(FString &layoutBlock, int &index, bool is_in, const std::vector<BuiltinFieldDesc> &fields, const VkShaderKey& key, bool hasClipDistance)
static void AddBuiltinFields(FString &layoutBlock, int &index, bool is_in, const std::vector<BuiltinFieldDesc> &fields, const VkShaderKey& key, bool hasClipDistance)
{
for(auto &field : fields)
{
Expand Down Expand Up @@ -294,8 +305,7 @@ void VkShaderManager::BuildLayoutBlock(FString &layoutBlock, bool isFrag, const

if(!isFrag)
{
int index = 0;
AddFields(layoutBlock, index, true, vertexShaderInputs);
AddVertexInFields(fb, layoutBlock, key);
}

{
Expand All @@ -314,11 +324,6 @@ void VkShaderManager::BuildLayoutBlock(FString &layoutBlock, bool isFrag, const
int index = 0;
AddBuiltinFields(layoutBlock, index, false, fragShaderOutputs, key, hasClipDistance);
}

layoutBlock << "#line 1\n";

layoutBlock << LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars() << "\n";

}

void VkShaderManager::BuildDefinesBlock(FString &definesBlock, const char *defines, bool isFrag, const VkShaderKey& key, const UserShaderDesc *shader)
Expand Down Expand Up @@ -426,6 +431,19 @@ void VkShaderManager::BuildDefinesBlock(FString &definesBlock, const char *defin
if (key.UseSpriteCenter) definesBlock << "#define USE_SPRITE_CENTER\n";

definesBlock << ((key.Simple2D) ? "#define uFogEnabled -3\n" : "#define uFogEnabled 0\n");

// Setup fake variables for the 'in' attributes that aren't actually available because the garbage shader code thinks they exist
// God I hate this engine... :(
std::vector<bool> definedFields(vertexShaderInputs.size());
bool hasNormal = false;
const VkVertexFormat& vfmt = *fb->GetRenderPassManager()->GetVertexFormat(key.VertexFormat);
for (const FVertexBufferAttribute& attr : vfmt.Attrs)
definedFields[attr.location] = true;
for (size_t i = 0; i < vertexShaderInputs.size(); i++)
{
if (!definedFields[i])
definesBlock << "#define " << vertexShaderInputs[i].Name << " " << GetTypeStr(vertexShaderInputs[i].Type) << "(0)\n";
}
}

std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername, const char *vert_lump, const char *vert_lump_custom, const char *defines, const VkShaderKey& key, const UserShaderDesc *shader)
Expand All @@ -436,7 +454,6 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
FString layoutBlock;
BuildLayoutBlock(layoutBlock, false, key, shader);


FString codeBlock;
codeBlock << LoadPrivateShaderLump(vert_lump).GetChars() << "\n";
if(vert_lump_custom)
Expand All @@ -454,7 +471,8 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadVertShader(FString shadername
.DebugName(shadername.GetChars())
.AddSource("VersionBlock", GetVersionBlock().GetChars())
.AddSource("DefinesBlock", definesBlock.GetChars())
.AddSource("shaders/scene/layout_shared.glsl", layoutBlock.GetChars())
.AddSource("LayoutBlock", layoutBlock.GetChars())
.AddSource("shaders/scene/layout_shared.glsl", LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars())
.AddSource(vert_lump_custom ? vert_lump_custom : vert_lump, codeBlock.GetChars())
.OnIncludeLocal([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, false); })
.OnIncludeSystem([=](std::string headerName, std::string includerName, size_t depth) { return OnInclude(headerName.c_str(), includerName.c_str(), depth, true); })
Expand All @@ -469,7 +487,6 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
FString layoutBlock;
BuildLayoutBlock(layoutBlock, true, key, shader);


FString codeBlock;
codeBlock << LoadPrivateShaderLump(frag_lump).GetChars() << "\n";

Expand Down Expand Up @@ -554,6 +571,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
.AddSource("VersionBlock", GetVersionBlock().GetChars())
.AddSource("DefinesBlock", definesBlock.GetChars())
.AddSource("LayoutBlock", layoutBlock.GetChars())
.AddSource("shaders/scene/layout_shared.glsl", LoadPrivateShaderLump("shaders/scene/layout_shared.glsl").GetChars())
.AddSource("shaders/scene/includes.glsl", LoadPrivateShaderLump("shaders/scene/includes.glsl").GetChars())
.AddSource(mateffectname.GetChars(), mateffectBlock.GetChars())
.AddSource(materialname.GetChars(), materialBlock.GetChars())
Expand Down
4 changes: 3 additions & 1 deletion src/common/rendering/vulkan/shaders/vk_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ class VkShaderKey

int SpecialEffect = 0;
int EffectState = 0;
int VertexFormat = 0;
int Padding = 0;

bool operator<(const VkShaderKey& other) const { return memcmp(this, &other, sizeof(VkShaderKey)) < 0; }
bool operator==(const VkShaderKey& other) const { return memcmp(this, &other, sizeof(VkShaderKey)) == 0; }
bool operator!=(const VkShaderKey& other) const { return memcmp(this, &other, sizeof(VkShaderKey)) != 0; }
};

static_assert(sizeof(VkShaderKey) == 16, "sizeof(VkShaderKey) is not its expected size!"); // If this assert fails, the flags union no longer adds up to 64 bits. Or there are gaps in the class so the memcmp doesn't work.
static_assert(sizeof(VkShaderKey) == 24, "sizeof(VkShaderKey) is not its expected size!"); // If this assert fails, the flags union no longer adds up to 64 bits. Or there are gaps in the class so the memcmp doesn't work.

class VkShaderProgram
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/vulkan/vk_levelmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ VkLevelMesh::BLAS VkLevelMesh::CreateBLAS(bool preferFastBuild, int indexOffset,
accelStructBLDesc.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
accelStructBLDesc.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
accelStructBLDesc.geometry.triangles = { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR };
accelStructBLDesc.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32A32_SFLOAT;
accelStructBLDesc.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
accelStructBLDesc.geometry.triangles.vertexData.deviceAddress = VertexBuffer->GetDeviceAddress();
accelStructBLDesc.geometry.triangles.vertexStride = sizeof(FFlatVertex);
accelStructBLDesc.geometry.triangles.indexType = VK_INDEX_TYPE_UINT32;
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/vulkan/vk_renderdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,9 @@ int VulkanRenderDevice::GetLevelMeshPipelineID(const MeshApplyData& applyData, c

VkPipelineKey pipelineKey;
pipelineKey.DrawType = DT_Triangles;
pipelineKey.VertexFormat = levelVertexFormatIndex;
pipelineKey.RenderStyle = applyData.RenderStyle;
pipelineKey.DepthFunc = applyData.DepthFunc;
pipelineKey.ShaderKey.VertexFormat = levelVertexFormatIndex;
if (applyData.SpecialEffect > EFF_NONE)
{
pipelineKey.ShaderKey.SpecialEffect = applyData.SpecialEffect;
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/vulkan/vk_renderstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ void VkRenderState::ApplyRenderPass(int dt)
VkPipelineKey pipelineKey;
pipelineKey.DrawType = dt;
pipelineKey.DrawLine = mDrawLine || mWireframe;
pipelineKey.VertexFormat = mVertexBuffer ? static_cast<VkHardwareVertexBuffer*>(mVertexBuffer)->VertexFormat : mRSBuffers->Flatbuffer.VertexFormat;
pipelineKey.RenderStyle = mRenderStyle;
pipelineKey.DepthTest = mDepthTest && !mWireframe;
pipelineKey.DepthWrite = mDepthTest && !mWireframe && mDepthWrite;
Expand All @@ -260,6 +259,7 @@ void VkRenderState::ApplyRenderPass(int dt)
pipelineKey.StencilPassOp = mStencilOp;
pipelineKey.ColorMask = mColorMask;
pipelineKey.CullMode = mCullMode;
pipelineKey.ShaderKey.VertexFormat = mVertexBuffer ? static_cast<VkHardwareVertexBuffer*>(mVertexBuffer)->VertexFormat : mRSBuffers->Flatbuffer.VertexFormat;
if (mSpecialEffect > EFF_NONE)
{
pipelineKey.ShaderKey.SpecialEffect = mSpecialEffect;
Expand Down

0 comments on commit f182936

Please sign in to comment.