Skip to content

Commit

Permalink
task emulation cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Jan 2, 2024
1 parent 5ccc156 commit b0f47db
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions Engine/gapi/spirv/meshconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,11 +1131,11 @@ void MeshConverter::emitPayoadLoad(libspirv::MutableBytecode& comp, uint32_t eng
});

// Constants
const uint32_t const0 = constants[0];
const uint32_t const1 = constants[1];
const uint32_t const7 = constants[7];
const uint32_t const16 = comp.OpConstant(fn, uint_t, 16);
const uint32_t constFFFF = comp.OpConstant(fn, uint_t, 0xFFFF);
const uint32_t const0 = constants[0];
const uint32_t const1 = constants[1];
const uint32_t const7 = constants[7];
const uint32_t const20 = comp.OpConstant(fn, uint_t, 20);
const uint32_t constFFFFF = comp.OpConstant(fn, uint_t, 0xFFFFF);

// Function
fn = comp.end();
Expand Down Expand Up @@ -1187,13 +1187,13 @@ void MeshConverter::emitPayoadLoad(libspirv::MutableBytecode& comp, uint32_t eng
uint32_t rAt = comp.fetchAddBound();
{
// firstTask_lutPtr
fn.insert(spv::OpBitwiseAnd, {uint_t, rAt, firstTask_lutPtr, constFFFF});
fn.insert(spv::OpBitwiseAnd, {uint_t, rAt, firstTask_lutPtr, constFFFFF});
}

if(usr_WorkGroupID!=0) {
// firstTask_lutPtr
uint32_t rWg0 = comp.fetchAddBound();
fn.insert(spv::OpShiftRightLogical, {uint_t, rWg0, firstTask_lutPtr, const16});
fn.insert(spv::OpShiftRightLogical, {uint_t, rWg0, firstTask_lutPtr, const20});

const uint32_t ptrWgId = comp.fetchAddBound();
fn.insert(spv::OpAccessChain, {_ptr_Input_uint, ptrWgId, gl_WorkGroupID, const0});
Expand Down
7 changes: 2 additions & 5 deletions Engine/gapi/vulkan/vcommandbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ void VMeshCommandBuffer::pushChunk() {
ch.impl = cbTask;
chunks.push(ch);
cbTask = nullptr;
taskIndirectId = 0;
}
if(cbMesh!=nullptr) {
auto& ms = *device.meshHelper;
Expand All @@ -1084,6 +1085,7 @@ void VMeshCommandBuffer::pushChunk() {
ch.impl = cbMesh;
chunks.push(ch);
cbMesh = nullptr;
meshIndirectId = 0;
}
VCommandBuffer::pushChunk();
}
Expand All @@ -1096,11 +1098,6 @@ void VMeshCommandBuffer::setPipeline(AbstractGraphicsApi::Pipeline& p) {

auto& ms = *device.meshHelper;

if(cbTask==VK_NULL_HANDLE && cbMesh==VK_NULL_HANDLE) {
taskIndirectId = 0;
meshIndirectId = 0;
}

if(cbTask==VK_NULL_HANDLE && px.taskPipeline()!=VK_NULL_HANDLE) {
VkCommandBufferAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
Expand Down
10 changes: 5 additions & 5 deletions Engine/gapi/vulkan/vmeshlethelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ VMeshletHelper::VMeshletHelper(VDevice& dev) : dev(dev) {
uint32_t indirectRate;
uint32_t indirectCmdCount;
} push;
push.indirectRate = 2;
push.indirectRate = indirectRate;
push.indirectCmdCount = 0;

auto c = dev.dataMgr().get();
Expand Down Expand Up @@ -126,7 +126,7 @@ void VMeshletHelper::bindVS(VkCommandBuffer impl, VkPipelineLayout lay) {
}

void VMeshletHelper::drawIndirect(VkCommandBuffer impl, uint32_t drawId) {
assert(drawId<IndirectCmdCount);
assert(drawId*indirectRate < IndirectCmdCount);
uint32_t off = drawId*indirectOffset + 2*sizeof(uint32_t);
vkCmdDrawIndexedIndirect(impl, indirect.impl, off, 1, 0);
}
Expand Down Expand Up @@ -171,10 +171,10 @@ void VMeshletHelper::taskEpiloguePass(VkCommandBuffer impl, uint32_t meshCallsCo
uint32_t indirectRate;
uint32_t indirectCmdCount;
} push;
push.indirectRate = 2;
push.indirectRate = indirectRate;
push.indirectCmdCount = meshCallsCount;

// prefix summ pass
// post-process pass
barrier(impl, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT);
Expand Down Expand Up @@ -211,7 +211,7 @@ void VMeshletHelper::sortPass(VkCommandBuffer impl, uint32_t meshCallsCount) {
uint32_t indirectRate;
uint32_t indirectCmdCount;
} push;
push.indirectRate = 2;
push.indirectRate = indirectRate;
push.indirectCmdCount = meshCallsCount;

// prefix summ pass
Expand Down
10 changes: 2 additions & 8 deletions Engine/gapi/vulkan/vmeshlethelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class VCompPipeline;

class VMeshletHelper {
private:
struct IndirectCommand {
uint32_t indexCount;
};

struct DrawIndexedIndirectCommand {
uint32_t drawId;
uint32_t indexCountSrc;
Expand All @@ -26,7 +22,7 @@ class VMeshletHelper {
uint32_t firstIndex; // prefix sum
int32_t vertexOffset; // can be abused to offset into var_buffer
uint32_t firstInstance; // caps: should be zero
uint32_t vboOffset; // prefix sum
uint32_t lutPtr; // pointer to task-payload
};

public:
Expand All @@ -36,9 +32,7 @@ class VMeshletHelper {
PipelinewordsCount = 32*1024*1024,
PipelineMemorySize = PipelinewordsCount*4,
MeshletsMemorySize = MeshletsMaxCount*3*4,

IndirectScratchSize = IndirectCmdCount*sizeof(IndirectCommand),
IndirectMemorySize = IndirectCmdCount*sizeof(VkDrawIndexedIndirectCommand),
IndirectMemorySize = IndirectCmdCount*sizeof(DrawIndexedIndirectCommand),
};
explicit VMeshletHelper(VDevice& dev);
~VMeshletHelper();
Expand Down
2 changes: 1 addition & 1 deletion Engine/shaders/mesh_compactage.comp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct DrawIndexedIndirectCommand
uint firstIndex; // prefix sum
int vertexOffset; // can be abused to offset into var_buffer
uint firstInstance; // caps: should be zero
uint padding0;
uint lutPtr;
};

layout(binding = 0, std430) restrict buffer EngineInternal0
Expand Down
2 changes: 1 addition & 1 deletion Engine/shaders/mesh_prefix_pass.comp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void main() {
for(uint i=b; i<e; ++i) {
uint idx = i*indirectRate;
uint indexCount = indirect[idx].indexCountSrc;
uint firstIndex = grow + prefixIbo;
uint firstIndex = indexCount>0 ? grow + prefixIbo : 0;
uint inst = indexCount>0 ? 1 : 0;

prefixIbo += indexCount;
Expand Down
12 changes: 8 additions & 4 deletions Engine/shaders/task_lut_pass.comp
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ void main() {
if(at>=total)
break;

const Descriptor d = mesh.desc[at+first];
const Descriptor d = mesh.desc[at+first];
const uint idx = d.drawId*indirectRate;
if(indirect[idx].lutPtr==0) {
continue;
}

if(gl_LocalInvocationIndex==0) {
uint idx = d.drawId*indirectRate;
uint meshlets = atomicAdd(indirect[idx].lutIterator, d.dispatchSz);
lutOffset = meshlets + indirect[idx].lutPtr;
}
barrier();

[[loop]]
for(uint i=gl_LocalInvocationIndex; i<d.dispatchSz; i+=gl_WorkGroupSize.x) {
const uint usrWorkGroup = d.ptr!=0 ? i : 0;
var.heap[lutOffset+i] = (usrWorkGroup << 16u) | (d.ptr & 0xFFFF);
const uint usrWorkGroup = i;
var.heap[lutOffset+i] = (usrWorkGroup << 20u) | (d.ptr & 0xFFFFF);
}
}

Expand Down
5 changes: 5 additions & 0 deletions Engine/shaders/task_post_pass.comp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ layout(push_constant, std430) uniform UboPush {
};

void main() {
if(var.grow==0) {
var.grow = 1; // zero is reserved as null-value
}
barrier();

uint index = gl_LocalInvocationID.x;
uint len = indirectCmdCount;

Expand Down

0 comments on commit b0f47db

Please sign in to comment.