Skip to content

Commit

Permalink
mesh emulation in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Aug 4, 2022
1 parent cc326ee commit 056a69b
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 27 deletions.
10 changes: 10 additions & 0 deletions Engine/gapi/vulkan/vcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,14 @@ void VCommandBuffer::addDependency(VSwapchain& s, size_t imgId) {
swapchainSync.push_back(sc);
}


void VMeshCommandBuffer::setPipeline(AbstractGraphicsApi::Pipeline& p) {
VPipeline& px = reinterpret_cast<VPipeline&>(p);
VCommandBuffer::setPipeline(px);
}

void VMeshCommandBuffer::dispatchMesh(size_t firstInstance, size_t instanceCount) {
// TODO
}

#endif
8 changes: 8 additions & 0 deletions Engine/gapi/vulkan/vcommandbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ class VCommandBuffer:public AbstractGraphicsApi::CommandBuffer {
size_t vboStride = 0;
};

class VMeshCommandBuffer:public VCommandBuffer {
public:
using VCommandBuffer::VCommandBuffer;

void setPipeline(AbstractGraphicsApi::Pipeline& p) override;
void dispatchMesh(size_t firstInstance, size_t instanceCount) override;
};

}}
3 changes: 1 addition & 2 deletions Engine/gapi/vulkan/vmeshshaderemulated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ VMeshShaderEmulated::VMeshShaderEmulated(VDevice& device, const void *source, si
if(vkCreateShaderModule(device.device.impl,&createInfo,nullptr,&compPass)!=VK_SUCCESS)
throw std::system_error(Tempest::GraphicsErrc::InvalidShaderModule);

// TODO: vertex shader
// throw std::system_error(Tempest::GraphicsErrc::InvalidShaderModule);
//stage = ShaderReflection::Stage::Vertex;
}

VMeshShaderEmulated::~VMeshShaderEmulated() {
Expand Down
34 changes: 24 additions & 10 deletions Engine/gapi/vulkan/vpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "vdevice.h"
#include "vshader.h"
#include "vpipelinelay.h"
#include "vmeshshaderemulated.h"

#include <algorithm>

Expand Down Expand Up @@ -52,6 +53,10 @@ VPipeline::VPipeline(VDevice& device, const RenderState& st, Topology tp,
}
}

if(auto vert=findShader(ShaderReflection::Stage::Mesh)) {
isMesh = true;
}

pipelineLayout = initLayout(device.device.impl,ulay,pushStageFlags,pushSize);
}
catch(...) {
Expand Down Expand Up @@ -106,6 +111,10 @@ VkPipeline VPipeline::instance(const VkPipelineRenderingCreateInfoKHR& info, siz
return instDr.back().val;
}

bool VPipeline::isMeshPipeline() const {
return isMesh;
}

const VShader* VPipeline::findShader(ShaderReflection::Stage sh) const {
for(auto& i:modules)
if(i.handler!=nullptr && i.handler->stage==sh) {
Expand All @@ -128,12 +137,19 @@ void VPipeline::cleanup() {
VkPipelineLayout VPipeline::initLayout(VkDevice device, const VPipelineLay& uboLay, VkShaderStageFlags& pushStageFlags, uint32_t& pushSize) {
VkPushConstantRange push = {};

VkDescriptorSetLayout pSetLayouts[4] = {uboLay.impl};

VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.pSetLayouts = &uboLay.impl;
pipelineLayoutInfo.pSetLayouts = pSetLayouts;
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pushConstantRangeCount = 0;

if(uboLay.msHelper!=VK_NULL_HANDLE) {
pSetLayouts[pipelineLayoutInfo.setLayoutCount] = uboLay.msHelper;
pipelineLayoutInfo.setLayoutCount++;
}

if(uboLay.pb.size>0) {
pushStageFlags = nativeFormat(uboLay.pb.stage);
push.stageFlags = pushStageFlags;
Expand Down Expand Up @@ -167,6 +183,10 @@ VkPipeline VPipeline::initGraphicsPipeline(VkDevice device, VkPipelineLayout lay
sh.stage = nativeFormat(shaders[i].handler->stage);
sh.module = shaders[i].handler->impl;
sh.pName = "main";
if(auto ms = dynamic_cast<const VMeshShaderEmulated*>(shaders[i].handler)) {
sh.stage = VK_SHADER_STAGE_VERTEX_BIT;
sh.module = ms->impl;
}
stagesCnt++;
}
}
Expand All @@ -179,13 +199,7 @@ VkPipeline VPipeline::initGraphicsPipeline(VkDevice device, VkPipelineLayout lay
vertexInputBindingDescription.stride = uint32_t(stride);
vertexInputBindingDescription.inputRate = VkVertexInputRate::VK_VERTEX_INPUT_RATE_VERTEX;

VkVertexInputAttributeDescription vsInputsStk[16]={};
std::unique_ptr<VkVertexInputAttributeDescription> vsInputHeap;
VkVertexInputAttributeDescription* vsInput = vsInputsStk;
if(declSize>16) {
vsInputHeap.reset(new VkVertexInputAttributeDescription[declSize]);
vsInput = vsInputHeap.get();
}
SmallArray<VkVertexInputAttributeDescription,16> vsInput(declSize);
uint32_t offset=0;
for(size_t i=0;i<declSize;++i){
auto& loc=vsInput[i];
Expand All @@ -204,7 +218,7 @@ VkPipeline VPipeline::initGraphicsPipeline(VkDevice device, VkPipelineLayout lay
vertexInputInfo.vertexBindingDescriptionCount = (declSize>0 ? 1 : 0);
vertexInputInfo.pVertexBindingDescriptions = &vertexInputBindingDescription;
vertexInputInfo.vertexAttributeDescriptionCount = uint32_t(declSize);
vertexInputInfo.pVertexAttributeDescriptions = vsInput;
vertexInputInfo.pVertexAttributeDescriptions = vsInput.get();

VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Expand Down Expand Up @@ -332,7 +346,7 @@ VkPipeline VPipeline::initGraphicsPipeline(VkDevice device, VkPipelineLayout lay
VCompPipeline::VCompPipeline() {
}

VCompPipeline::VCompPipeline(VDevice& dev, const VPipelineLay& ulay, VShader& comp)
VCompPipeline::VCompPipeline(VDevice& dev, const VPipelineLay& ulay, const VShader& comp)
:device(dev.device.impl) {
VkShaderStageFlags pushStageFlags = 0;
pipelineLayout = VPipeline::initLayout(device,ulay,pushStageFlags,pushSize);
Expand Down
8 changes: 6 additions & 2 deletions Engine/gapi/vulkan/vpipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class VPipeline : public AbstractGraphicsApi::Pipeline {
VkPipeline instance(const std::shared_ptr<VFramebufferMap::RenderPass>& lay, size_t stride);
VkPipeline instance(const VkPipelineRenderingCreateInfoKHR& info, size_t stride);

bool isMeshPipeline() const;

private:
struct InstRp : Inst {
InstRp(const std::shared_ptr<VFramebufferMap::RenderPass>& lay, size_t stride, VkPipeline val):Inst(val,stride),lay(lay){}
Expand All @@ -57,9 +59,11 @@ class VPipeline : public AbstractGraphicsApi::Pipeline {
VkDevice device=nullptr;
Tempest::RenderState st;
size_t declSize=0;
Topology tp = Topology::Triangles;
DSharedPtr<const VShader*> modules[5] = {};
std::unique_ptr<Decl::ComponentType[]> decl;
Topology tp = Topology::Triangles;
bool isMesh = false;

std::vector<InstRp> instRp;
std::vector<InstDr> instDr;
SpinLock sync;
Expand All @@ -79,7 +83,7 @@ class VPipeline : public AbstractGraphicsApi::Pipeline {
class VCompPipeline : public AbstractGraphicsApi::CompPipeline {
public:
VCompPipeline();
VCompPipeline(VDevice &device, const VPipelineLay& ulay, VShader &comp);
VCompPipeline(VDevice &device, const VPipelineLay& ulay, const VShader& comp);
VCompPipeline(VCompPipeline&& other) = delete;
~VCompPipeline();

Expand Down
57 changes: 50 additions & 7 deletions Engine/gapi/vulkan/vpipelinelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,37 @@ using namespace Tempest;
using namespace Tempest::Detail;

VPipelineLay::VPipelineLay(VDevice& dev, const std::vector<ShaderReflection::Binding>* sh[], size_t cnt)
: dev(dev.device.impl) {
: dev(dev) {
ShaderReflection::merge(lay, pb, sh, cnt);
adjustSsboBindings();

//if(!runtimeSized)
bool needMsHelper = false;
for(size_t i=0; i<cnt; ++i) {
if(sh[i]==nullptr)
continue;
for(auto& r:*sh[i])
if(r.stage==ShaderReflection::Mesh) {
needMsHelper = true;
break;
}
}

impl = create(MAX_BINDLESS);
if(needMsHelper) {
try {
msHelper = createMsHealper();
}
catch(...) {
vkDestroyDescriptorSetLayout(dev.device.impl,impl,nullptr);
throw;
}
}
}

VPipelineLay::~VPipelineLay() {
for(auto& i:pool)
vkDestroyDescriptorPool(dev,i.impl,nullptr);
vkDestroyDescriptorSetLayout(dev,impl,nullptr);
vkDestroyDescriptorPool(dev.device.impl,i.impl,nullptr);
vkDestroyDescriptorSetLayout(dev.device.impl,impl,nullptr);
}

size_t VPipelineLay::descriptorsCount() {
Expand All @@ -31,10 +50,10 @@ size_t VPipelineLay::descriptorsCount() {

VkDescriptorSetLayout VPipelineLay::create(uint32_t runtimeArraySz) const {
SmallArray<VkDescriptorSetLayoutBinding,32> bind(lay.size());
SmallArray<VkDescriptorBindingFlags,32> flg(lay.size());
SmallArray<VkDescriptorBindingFlags,32> flg (lay.size());

uint32_t count = 0;
for(size_t i=0;i<lay.size();++i){
for(size_t i=0;i<lay.size();++i) {
auto& b = bind[count];
auto& e = lay[i];

Expand All @@ -48,6 +67,8 @@ VkDescriptorSetLayout VPipelineLay::create(uint32_t runtimeArraySz) const {
b.descriptorCount = e.runtimeSized ? runtimeArraySz : e.arraySize;
b.descriptorType = nativeFormat(e.cls);
b.stageFlags = nativeFormat(e.stage);
if(b.stageFlags==VK_SHADER_STAGE_MESH_BIT_NV && dev.props.meshlets.meshShaderEmulated)
b.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
++count;
}

Expand All @@ -67,7 +88,29 @@ VkDescriptorSetLayout VPipelineLay::create(uint32_t runtimeArraySz) const {
info.pNext = &bindingFlags;

VkDescriptorSetLayout ret = VK_NULL_HANDLE;
vkAssert(vkCreateDescriptorSetLayout(dev,&info,nullptr,&ret));
vkAssert(vkCreateDescriptorSetLayout(dev.device.impl,&info,nullptr,&ret));
return ret;
}

VkDescriptorSetLayout VPipelineLay::createMsHealper() const {
VkDescriptorSetLayoutBinding bind[3] = {};

for(int i=0; i<3; ++i) {
bind[i].binding = i;
bind[i].descriptorCount = 1;
bind[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
bind[i].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; //VK_SHADER_STAGE_COMPUTE_BIT;
}

VkDescriptorSetLayoutCreateInfo info={};
info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
info.pNext = nullptr;
info.flags = 0;
info.bindingCount = 1; // one for vertex
info.pBindings = bind;

VkDescriptorSetLayout ret = VK_NULL_HANDLE;
vkAssert(vkCreateDescriptorSetLayout(dev.device.impl,&info,nullptr,&ret));
return ret;
}

Expand Down
7 changes: 5 additions & 2 deletions Engine/gapi/vulkan/vpipelinelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class VPipelineLay : public AbstractGraphicsApi::PipelineLay {

size_t descriptorsCount() override;
VkDescriptorSetLayout create(uint32_t runtimeArraySz) const;
VkDescriptorSetLayout createMsHealper() const;

using Binding = ShaderReflection::Binding;

VkDevice dev = nullptr;
VkDescriptorSetLayout impl = VK_NULL_HANDLE;
VDevice& dev;
VkDescriptorSetLayout impl = VK_NULL_HANDLE;
VkDescriptorSetLayout msHelper = VK_NULL_HANDLE;

std::vector<Binding> lay;
ShaderReflection::PushBlock pb;
bool runtimeSized = false;
Expand Down
3 changes: 1 addition & 2 deletions Engine/gapi/vulkan/vshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void VShader::fetchBindings(const uint32_t *source, size_t size) {

libspirv::Bytecode code(source, size);
stage = ShaderReflection::getExecutionModel(code);
if(stage==ShaderReflection::Compute ||
stage==ShaderReflection::Task || stage==ShaderReflection::Mesh) {
if(stage==ShaderReflection::Compute || stage==ShaderReflection::Task || stage==ShaderReflection::Mesh) {
for(auto& i:code) {
if(i.op()!=spv::OpExecutionMode)
continue;
Expand Down
4 changes: 4 additions & 0 deletions Engine/gapi/vulkanapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ AbstractGraphicsApi::Desc* VulkanApi::createDescriptors(AbstractGraphicsApi::Dev

AbstractGraphicsApi::CommandBuffer* VulkanApi::createCommandBuffer(AbstractGraphicsApi::Device* d) {
Detail::VDevice* dx=reinterpret_cast<Detail::VDevice*>(d);
if(dx->props.meshlets.meshShaderEmulated) {
// CB with meshlet emulation support
return new Detail::VMeshCommandBuffer(*dx);
}
return new Detail::VCommandBuffer(*dx);
}

Expand Down
2 changes: 0 additions & 2 deletions Engine/libspirv/libspirv.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ class Bytecode {
};
const OpCode* spirv = nullptr;
size_t codeLen = 0;
#ifndef NDEBUG
uint32_t iteratorGen = 0;
#endif

friend class MutableBytecode;
};
Expand Down

0 comments on commit 056a69b

Please sign in to comment.