Kelsidavis-WoWee/include/rendering/vk_pipeline.hpp
Paul d54e262048 feat(rendering): GPU architecture + visual quality fixes
M2 GPU instancing
- M2InstanceGPU SSBO (96 B/entry, double-buffered, 16384 max)
- Group opaque instances by (modelId, LOD); single vkCmdDrawIndexed per group
- boneBase field indexes into mega bone SSBO via gl_InstanceIndex

Indirect terrain drawing
- 24 MB mega index buffer (6M uint32) + 64 MB mega vertex buffer
- CPU builds VkDrawIndexedIndirectCommand per visible chunk
- Single VB/IB bind per frame; shadow pass reuses mega buffers
- Replaced vkCmdDrawIndexedIndirect with direct vkCmdDrawIndexed to fix
  host-mapped buffer race condition that caused terrain flickering

GPU frustum culling (compute shader)
- m2_cull.comp.glsl: 64-thread workgroups, sphere-vs-6-planes + distance cull
- CullInstanceGPU SSBO input, uint visibility[] output, double-buffered
- dispatchCullCompute() runs before main pass via render graph node

Consolidated bone matrix SSBOs
- 16 MB double-buffered mega bone SSBO (2048 instances × 128 bones)
- Eliminated per-instance descriptor sets; one megaBoneSet_ per frame
- prepareRender() packs bone matrices consecutively into current frame slot

Render graph / frame graph
- RenderGraph: RGResource handles, RGPass nodes, Kahn topological sort
- Automatic VkImageMemoryBarrier/VkBufferMemoryBarrier between passes
- Passes: minimap_composite, worldmap_composite, preview_composite,
  shadow_pass, reflection_pass, compute_cull
- beginFrame() uses buildFrameGraph() + renderGraph_->execute(cmd)

Pipeline derivatives
- PipelineBuilder::setFlags/setBasePipeline for VK_PIPELINE_CREATE_DERIVATIVE_BIT
- M2 opaque = base; alphaTest/alpha/additive are derivatives
- Applied to terrain (wireframe) and WMO (alpha-test) renderers

Rendering bug fixes:
- fix(shadow): compute lightSpaceMatrix before updatePerFrameUBO to eliminate
  one-frame lag that caused shadow trails and flicker on moving objects
- fix(shadow): scale depth bias with shadowDistance_ instead of hardcoded 0.8f
  to prevent acne at close range and gaps at far range
- fix(visibility): WMO group distance threshold 500u → 1200u to match terrain
  view distance; buildings were disappearing on the horizon
- fix(precision): camera near plane 0.05 → 0.5 (ratio 600K:1 → 60K:1),
  eliminating Z-fighting and improving frustum plane extraction stability
- fix(streaming): terrain load radius 4 → 6 tiles (~2133u → ~3200u) to exceed
  M2 render distance (2800u) and eliminate pop-in when camera turns;
  unload radius 7 → 9; spawn radius 3 → 4
- fix(visibility): ground-detail M2 distance multiplier 0.75 → 0.9 to reduce
  early pop of grass and debris
2026-04-04 13:43:16 +03:00

127 lines
4.7 KiB
C++

#pragma once
#include <vulkan/vulkan.h>
#include <vector>
#include <string>
namespace wowee {
namespace rendering {
// Builder pattern for VkGraphicsPipeline creation.
// Usage:
// auto pipeline = PipelineBuilder()
// .setShaders(vertStage, fragStage)
// .setVertexInput(bindings, attributes)
// .setTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
// .setRasterization(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT)
// .setDepthTest(true, true, VK_COMPARE_OP_LESS)
// .setColorBlendAttachment(PipelineBuilder::blendAlpha())
// .setLayout(pipelineLayout)
// .setRenderPass(renderPass)
// .build(device);
class PipelineBuilder {
public:
PipelineBuilder();
// Shader stages
PipelineBuilder& setShaders(VkPipelineShaderStageCreateInfo vert,
VkPipelineShaderStageCreateInfo frag);
// Vertex input
PipelineBuilder& setVertexInput(
const std::vector<VkVertexInputBindingDescription>& bindings,
const std::vector<VkVertexInputAttributeDescription>& attributes);
// No vertex input (fullscreen quad generated in vertex shader)
PipelineBuilder& setNoVertexInput();
// Input assembly
PipelineBuilder& setTopology(VkPrimitiveTopology topology,
VkBool32 primitiveRestart = VK_FALSE);
// Rasterization
PipelineBuilder& setRasterization(VkPolygonMode polygonMode,
VkCullModeFlags cullMode,
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE);
// Depth test/write
PipelineBuilder& setDepthTest(bool enable, bool writeEnable,
VkCompareOp compareOp = VK_COMPARE_OP_LESS);
// No depth test (default)
PipelineBuilder& setNoDepthTest();
// Depth bias (for shadow maps)
PipelineBuilder& setDepthBias(float constantFactor, float slopeFactor);
// Color blend attachment
PipelineBuilder& setColorBlendAttachment(
VkPipelineColorBlendAttachmentState blendState);
// No color attachment (depth-only pass)
PipelineBuilder& setNoColorAttachment();
// Multisampling
PipelineBuilder& setMultisample(VkSampleCountFlagBits samples);
PipelineBuilder& setAlphaToCoverage(bool enable);
// Pipeline layout
PipelineBuilder& setLayout(VkPipelineLayout layout);
// Render pass
PipelineBuilder& setRenderPass(VkRenderPass renderPass, uint32_t subpass = 0);
// Dynamic state
PipelineBuilder& setDynamicStates(const std::vector<VkDynamicState>& states);
// Pipeline derivatives — hint driver to share compiled state between similar pipelines
PipelineBuilder& setFlags(VkPipelineCreateFlags flags);
PipelineBuilder& setBasePipeline(VkPipeline basePipeline);
// Build the pipeline (pass a VkPipelineCache for faster creation)
VkPipeline build(VkDevice device, VkPipelineCache cache = VK_NULL_HANDLE) const;
// Common blend states
static VkPipelineColorBlendAttachmentState blendDisabled();
static VkPipelineColorBlendAttachmentState blendAlpha();
static VkPipelineColorBlendAttachmentState blendPremultiplied();
static VkPipelineColorBlendAttachmentState blendAdditive();
private:
std::vector<VkPipelineShaderStageCreateInfo> shaderStages_;
std::vector<VkVertexInputBindingDescription> vertexBindings_;
std::vector<VkVertexInputAttributeDescription> vertexAttributes_;
VkPrimitiveTopology topology_ = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkBool32 primitiveRestart_ = VK_FALSE;
VkPolygonMode polygonMode_ = VK_POLYGON_MODE_FILL;
VkCullModeFlags cullMode_ = VK_CULL_MODE_NONE;
VkFrontFace frontFace_ = VK_FRONT_FACE_CLOCKWISE;
bool depthTestEnable_ = false;
bool depthWriteEnable_ = false;
VkCompareOp depthCompareOp_ = VK_COMPARE_OP_LESS;
bool depthBiasEnable_ = false;
float depthBiasConstant_ = 0.0f;
float depthBiasSlope_ = 0.0f;
VkSampleCountFlagBits msaaSamples_ = VK_SAMPLE_COUNT_1_BIT;
bool alphaToCoverage_ = false;
std::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachments_;
VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
VkRenderPass renderPass_ = VK_NULL_HANDLE;
uint32_t subpass_ = 0;
std::vector<VkDynamicState> dynamicStates_;
VkPipelineCreateFlags flags_ = 0;
VkPipeline basePipelineHandle_ = VK_NULL_HANDLE;
};
// Helper to create a pipeline layout from descriptor set layouts and push constant ranges
VkPipelineLayout createPipelineLayout(VkDevice device,
const std::vector<VkDescriptorSetLayout>& setLayouts,
const std::vector<VkPushConstantRange>& pushConstants = {});
// Helper to create a descriptor set layout from bindings
VkDescriptorSetLayout createDescriptorSetLayout(VkDevice device,
const std::vector<VkDescriptorSetLayoutBinding>& bindings);
} // namespace rendering
} // namespace wowee