in RenderScriptMigrationSample/app/src/main/cpp/ComputePipeline.cpp [162:216]
bool ComputePipeline::createComputePipeline(const char* shader, AAssetManager* assetManager) {
// Create shader module
VulkanShaderModule shaderModule(mContext->device());
RET_CHECK(createShaderModuleFromAsset(mContext->device(), shader, assetManager,
shaderModule.pHandle()));
// Create pipeline layout
const bool hasPushConstant = mPushConstantSize > 0;
const VkPushConstantRange pushConstantRange = {
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
.offset = 0,
.size = mPushConstantSize,
};
const VkPipelineLayoutCreateInfo layoutDesc = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = mDescriptorSetLayout.pHandle(),
.pushConstantRangeCount = hasPushConstant ? 1u : 0u,
.pPushConstantRanges = hasPushConstant ? &pushConstantRange : nullptr,
};
CALL_VK(vkCreatePipelineLayout, mContext->device(), &layoutDesc, nullptr,
mPipelineLayout.pHandle());
// Create compute pipeline
const auto workGroupSize = mContext->getWorkGroupSize();
const uint32_t specializationData[] = {workGroupSize, workGroupSize};
const std::vector<VkSpecializationMapEntry> specializationMap = {
// clang-format off
// constantID, offset, size
{0, 0 * sizeof(uint32_t), sizeof(uint32_t)},
{1, 1 * sizeof(uint32_t), sizeof(uint32_t)},
// clang-format on
};
const VkSpecializationInfo specializationInfo = {
.mapEntryCount = static_cast<uint32_t>(specializationMap.size()),
.pMapEntries = specializationMap.data(),
.dataSize = sizeof(specializationData),
.pData = specializationData,
};
const VkComputePipelineCreateInfo pipelineDesc = {
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
.stage =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_COMPUTE_BIT,
.module = shaderModule.handle(),
.pName = "main",
.pSpecializationInfo = &specializationInfo,
},
.layout = mPipelineLayout.handle(),
};
CALL_VK(vkCreateComputePipelines, mContext->device(), VK_NULL_HANDLE, 1, &pipelineDesc, nullptr,
mPipeline.pHandle());
return true;
}