Commit 639c44a0 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#65] Added dispatch recording commands and adjusted test compute shader

parent f1ba9662
Pipeline #25642 passed with stages
in 4 minutes and 8 seconds
......@@ -224,7 +224,7 @@ namespace vkcv
*/
[[nodiscard]]
DescriptorSetHandle createDescriptorSet(const std::vector<DescriptorBinding> &bindings);
void writeResourceDescription(DescriptorSetHandle handle, size_t setIndex, const DescriptorWrites& writes);
void writeDescriptorSet(DescriptorSetHandle handle, const DescriptorWrites& writes);
DescriptorSet getDescriptorSet(const DescriptorSetHandle handle) const;
......@@ -241,6 +241,13 @@ namespace vkcv
const std::vector<DrawcallInfo> &drawcalls,
const std::vector<ImageHandle> &renderTargets);
void recordComputeDispatchToCmdStream(
CommandStreamHandle cmdStream,
PipelineHandle computePipeline,
const uint32_t dispatchCount[3],
const std::vector<DescriptorSetUsage> &descriptorSetUsages,
const PushConstantData& pushConstantData);
/**
* @brief end recording and present image
*/
......
......@@ -207,7 +207,7 @@ int main(int argc, const char** argv) {
vkcv::SamplerDescriptorWrite(1, sampler),
vkcv::SamplerDescriptorWrite(4, shadowSampler) };
setWrites.uniformBufferWrites = { vkcv::UniformBufferDescriptorWrite(2, lightBuffer.getHandle()) };
core.writeResourceDescription(descriptorSet, 0, setWrites);
core.writeDescriptorSet(descriptorSet, setWrites);
auto start = std::chrono::system_clock::now();
const auto appStartTime = start;
......
......@@ -130,9 +130,9 @@ int main(int argc, const char** argv) {
};
vkcv::DescriptorWrites setWrites;
setWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, texture.getHandle()) };
setWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, sampler) };
core.writeResourceDescription(descriptorSet, 0, setWrites);
setWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, texture.getHandle()) };
setWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, sampler) };
core.writeDescriptorSet(descriptorSet, setWrites);
vkcv::ImageHandle depthBuffer = core.createImage(vk::Format::eD32Sfloat, windowWidth, windowHeight).getHandle();
......
%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv
%VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv
%VULKAN_SDK%\Bin32\glslc.exe shader.comp -o comp.spv
pause
\ No newline at end of file
#version 440
layout(local_size_x_id = 0, local_size_y_id = 1) in; // workgroup size defined with specialization constants. On cpp side there is associated SpecializationInfo entry in PipelineShaderStageCreateInfo
layout(push_constant) uniform Parameters { // specify push constants. on cpp side its layout is fixed at PipelineLayout, and values are provided via vk::CommandBuffer::pushConstants()
uint Width;
uint Height;
float a;
} params;
layout(std430, binding = 0) buffer testBuffer
{
float test1[10];
float test2[10];
float test3[10];
};
layout(std430, binding = 0) buffer lay0 { float arr_y[]; };
layout(std430, binding = 1) buffer lay1 { float arr_x[]; };
layout( push_constant ) uniform constants{
float pushConstant;
};
layout(local_size_x = 5) in;
void main(){
// drop threads outside the buffer dimensions.
if(params.Width <= gl_GlobalInvocationID.x || params.Height <= gl_GlobalInvocationID.y){
if(gl_GlobalInvocationID.x >= 10){
return;
}
const uint id = params.Width*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x; // current offset
arr_y[id] += params.a*arr_x[id]; // saxpy
test1[gl_GlobalInvocationID.x] = gl_GlobalInvocationID.x;
test2[gl_GlobalInvocationID.x] = 69; // nice!
test3[gl_GlobalInvocationID.x] = pushConstant;
}
\ No newline at end of file
......@@ -128,6 +128,18 @@ int main(int argc, const char** argv) {
computeShaderProgram,
{ core.getDescriptorSet(computeDescriptorSet).layout });
struct ComputeTestBuffer {
float test1[10];
float test2[10];
float test3[10];
};
vkcv::Buffer computeTestBuffer = core.createBuffer<ComputeTestBuffer>(vkcv::BufferType::STORAGE, 1);
vkcv::DescriptorWrites computeDescriptorWrites;
computeDescriptorWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0, computeTestBuffer.getHandle()) };
core.writeDescriptorSet(computeDescriptorSet, computeDescriptorWrites);
/*
* BufferHandle triangleVertices = core.createBuffer(vertices);
* BufferHandle triangleIndices = core.createBuffer(indices);
......@@ -177,6 +189,17 @@ int main(int argc, const char** argv) {
pushConstantData,
{ drawcall },
{ swapchainInput });
const uint32_t dispatchSize[3] = { 2, 1, 1 };
const float theMeaningOfLife = 42;
core.recordComputeDispatchToCmdStream(
cmdStream,
computePipeline,
dispatchSize,
{ vkcv::DescriptorSetUsage(0, core.getDescriptorSet(computeDescriptorSet).vulkanHandle) },
vkcv::PushConstantData((void*)&theMeaningOfLife, sizeof(theMeaningOfLife)));
core.prepareSwapchainImageForPresent(cmdStream);
core.submitCommandStream(cmdStream);
......
......@@ -305,6 +305,40 @@ namespace vkcv
recordCommandsToStream(cmdStreamHandle, submitFunction, finishFunction);
}
void Core::recordComputeDispatchToCmdStream(
CommandStreamHandle cmdStreamHandle,
PipelineHandle computePipeline,
const uint32_t dispatchCount[3],
const std::vector<DescriptorSetUsage>& descriptorSetUsages,
const PushConstantData& pushConstantData) {
auto submitFunction = [&](const vk::CommandBuffer& cmdBuffer) {
const auto pipelineLayout = m_PipelineManager->getVkPipelineLayout(computePipeline);
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eCompute, m_PipelineManager->getVkPipeline(computePipeline));
for (const auto& usage : descriptorSetUsages) {
cmdBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eCompute,
pipelineLayout,
usage.setLocation,
{ usage.vulkanHandle },
{});
}
if (pushConstantData.sizePerDrawcall > 0) {
cmdBuffer.pushConstants(
pipelineLayout,
vk::ShaderStageFlagBits::eCompute,
0,
pushConstantData.sizePerDrawcall,
pushConstantData.data);
}
cmdBuffer.dispatch(dispatchCount[0], dispatchCount[1], dispatchCount[2]);
};
recordCommandsToStream(cmdStreamHandle, submitFunction, nullptr);
}
void Core::endFrame() {
if (m_currentSwapchainImageIndex == std::numeric_limits<uint32_t>::max()) {
return;
......@@ -409,10 +443,9 @@ namespace vkcv
return m_DescriptorManager->createDescriptorSet(bindings);
}
void Core::writeResourceDescription(DescriptorSetHandle handle, size_t setIndex, const DescriptorWrites &writes) {
m_DescriptorManager->writeResourceDescription(
handle,
setIndex,
void Core::writeDescriptorSet(DescriptorSetHandle handle, const DescriptorWrites &writes) {
m_DescriptorManager->writeDescriptorSet(
handle,
writes,
*m_ImageManager,
*m_BufferManager,
......
......@@ -90,9 +90,8 @@ namespace vkcv
vk::DescriptorType type;
};
void DescriptorManager::writeResourceDescription(
void DescriptorManager::writeDescriptorSet(
const DescriptorSetHandle &handle,
size_t setIndex,
const DescriptorWrites &writes,
const ImageManager &imageManager,
const BufferManager &bufferManager,
......
......@@ -23,9 +23,8 @@ namespace vkcv
DescriptorSetHandle createDescriptorSet(const std::vector<DescriptorBinding> &descriptorBindings);
void writeResourceDescription(
void writeDescriptorSet(
const DescriptorSetHandle &handle,
size_t setIndex,
const DescriptorWrites &writes,
const ImageManager &imageManager,
const BufferManager &bufferManager,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment