Hash :
fc65058c
Author :
Date :
2024-05-03T14:47:29
CL/Vulkan: Rework spec constant handling Specialization constants can be combined into a single hash map that's later iterated on when we create the compute pipeline. Bug: angleproject:364396920 Change-Id: I161356808ff0bd6a589f01854264210011bec512 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5834664 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Austin Annestrand <a.annestrand@samsung.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CLKernelVk.cpp: Implements the class methods for CLKernelVk.
#include "libANGLE/renderer/vulkan/CLKernelVk.h"
#include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/CLDeviceVk.h"
#include "libANGLE/renderer/vulkan/CLProgramVk.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLKernel.h"
#include "libANGLE/CLProgram.h"
#include "libANGLE/cl_utils.h"
namespace rx
{
CLKernelVk::CLKernelVk(const cl::Kernel &kernel,
std::string &name,
std::string &attributes,
CLKernelArguments &args)
: CLKernelImpl(kernel),
mProgram(&kernel.getProgram().getImpl<CLProgramVk>()),
mContext(&kernel.getProgram().getContext().getImpl<CLContextVk>()),
mName(name),
mAttributes(attributes),
mArgs(args)
{
mShaderProgramHelper.setShader(gl::ShaderType::Compute,
mKernel.getProgram().getImpl<CLProgramVk>().getShaderModule());
}
CLKernelVk::~CLKernelVk()
{
for (auto &dsLayouts : mDescriptorSetLayouts)
{
dsLayouts.reset();
}
mPipelineLayout.reset();
for (auto &pipelineHelper : mComputePipelineCache)
{
pipelineHelper.destroy(mContext->getDevice());
}
mShaderProgramHelper.destroy(mContext->getRenderer());
}
angle::Result CLKernelVk::setArg(cl_uint argIndex, size_t argSize, const void *argValue)
{
auto &arg = mArgs.at(argIndex);
if (arg.used)
{
arg.handle = const_cast<void *>(argValue);
arg.handleSize = argSize;
}
return angle::Result::Continue;
}
angle::Result CLKernelVk::createInfo(CLKernelImpl::Info *info) const
{
info->functionName = mName;
info->attributes = mAttributes;
info->numArgs = static_cast<cl_uint>(mArgs.size());
for (const auto &arg : mArgs)
{
ArgInfo argInfo;
argInfo.name = arg.info.name;
argInfo.typeName = arg.info.typeName;
argInfo.accessQualifier = arg.info.accessQualifier;
argInfo.addressQualifier = arg.info.addressQualifier;
argInfo.typeQualifier = arg.info.typeQualifier;
info->args.push_back(std::move(argInfo));
}
auto &ctx = mKernel.getProgram().getContext();
info->workGroups.resize(ctx.getDevices().size());
const CLProgramVk::DeviceProgramData *deviceProgramData = nullptr;
for (auto i = 0u; i < ctx.getDevices().size(); ++i)
{
auto &workGroup = info->workGroups[i];
const auto deviceVk = &ctx.getDevices()[i]->getImpl<CLDeviceVk>();
deviceProgramData = mProgram->getDeviceProgramData(ctx.getDevices()[i]->getNative());
if (deviceProgramData == nullptr)
{
continue;
}
// TODO: http://anglebug.com/42267005
ANGLE_TRY(
deviceVk->getInfoSizeT(cl::DeviceInfo::MaxWorkGroupSize, &workGroup.workGroupSize));
// TODO: http://anglebug.com/42267004
workGroup.privateMemSize = 0;
workGroup.localMemSize = 0;
workGroup.prefWorkGroupSizeMultiple = 16u;
workGroup.globalWorkSize = {0, 0, 0};
if (deviceProgramData->reflectionData.kernelCompileWorkgroupSize.contains(mName))
{
workGroup.compileWorkGroupSize = {
deviceProgramData->reflectionData.kernelCompileWorkgroupSize.at(mName)[0],
deviceProgramData->reflectionData.kernelCompileWorkgroupSize.at(mName)[1],
deviceProgramData->reflectionData.kernelCompileWorkgroupSize.at(mName)[2]};
}
else
{
workGroup.compileWorkGroupSize = {0, 0, 0};
}
}
return angle::Result::Continue;
}
angle::Result CLKernelVk::getOrCreateComputePipeline(vk::PipelineCacheAccess *pipelineCache,
const cl::NDRange &ndrange,
const cl::Device &device,
vk::PipelineHelper **pipelineOut,
cl::WorkgroupCount *workgroupCountOut)
{
const CLProgramVk::DeviceProgramData *devProgramData =
getProgram()->getDeviceProgramData(device.getNative());
ASSERT(devProgramData != nullptr);
// Start with Workgroup size (WGS) from kernel attribute (if available)
cl::WorkgroupSize workgroupSize = devProgramData->getCompiledWorkgroupSize(getKernelName());
if (workgroupSize == kEmptyWorkgroupSize)
{
if (ndrange.nullLocalWorkSize)
{
// NULL value was passed, in which case the OpenCL implementation will determine
// how to be break the global work-items into appropriate work-group instances.
workgroupSize = device.getImpl<CLDeviceVk>().selectWorkGroupSize(ndrange);
}
else
{
// Local work size (LWS) was valid, use that as WGS
workgroupSize = ndrange.localWorkSize;
}
}
// Calculate the workgroup count
// TODO: Add support for non-uniform WGS
// http://angleproject:8631
ASSERT(workgroupSize[0] != 0);
ASSERT(workgroupSize[1] != 0);
ASSERT(workgroupSize[2] != 0);
(*workgroupCountOut)[0] = static_cast<uint32_t>((ndrange.globalWorkSize[0] / workgroupSize[0]));
(*workgroupCountOut)[1] = static_cast<uint32_t>((ndrange.globalWorkSize[1] / workgroupSize[1]));
(*workgroupCountOut)[2] = static_cast<uint32_t>((ndrange.globalWorkSize[2] / workgroupSize[2]));
// Populate program specialization constants (if any)
uint32_t constantDataOffset = 0;
std::vector<uint32_t> specConstantData;
std::vector<VkSpecializationMapEntry> mapEntries;
for (const auto specConstantUsed : devProgramData->reflectionData.specConstantsUsed)
{
switch (specConstantUsed)
{
case SpecConstantType::WorkDimension:
specConstantData.push_back(ndrange.workDimensions);
break;
case SpecConstantType::WorkgroupSizeX:
specConstantData.push_back(static_cast<uint32_t>(workgroupSize[0]));
break;
case SpecConstantType::WorkgroupSizeY:
specConstantData.push_back(static_cast<uint32_t>(workgroupSize[1]));
break;
case SpecConstantType::WorkgroupSizeZ:
specConstantData.push_back(static_cast<uint32_t>(workgroupSize[2]));
break;
case SpecConstantType::GlobalOffsetX:
specConstantData.push_back(static_cast<uint32_t>(ndrange.globalWorkOffset[0]));
break;
case SpecConstantType::GlobalOffsetY:
specConstantData.push_back(static_cast<uint32_t>(ndrange.globalWorkOffset[1]));
break;
case SpecConstantType::GlobalOffsetZ:
specConstantData.push_back(static_cast<uint32_t>(ndrange.globalWorkOffset[2]));
break;
default:
UNIMPLEMENTED();
continue;
}
mapEntries.push_back(VkSpecializationMapEntry{
.constantID = devProgramData->reflectionData.specConstantIDs[specConstantUsed],
.offset = constantDataOffset,
.size = sizeof(uint32_t)});
constantDataOffset += sizeof(uint32_t);
}
VkSpecializationInfo computeSpecializationInfo{
.mapEntryCount = static_cast<uint32_t>(mapEntries.size()),
.pMapEntries = mapEntries.data(),
.dataSize = specConstantData.size() * sizeof(uint32_t),
.pData = specConstantData.data(),
};
// Now get or create (on compute pipeline cache miss) compute pipeline and return it
return mShaderProgramHelper.getOrCreateComputePipeline(
mContext, &mComputePipelineCache, pipelineCache, getPipelineLayout().get(),
vk::ComputePipelineOptions{}, PipelineSource::Draw, pipelineOut, mName.c_str(),
&computeSpecializationInfo);
}
} // namespace rx