Hash :
87d61997
Author :
Date :
2024-11-21T14:20:55
Vulkan: Switch ShaderModule to use SharedPtr This CL gets rid of many vk::RefCounted<vk::ShaderModule> usage which is risky due to it allows you to direct manipulate reference count. Switch to vk::SharedPtr manages the reference counting automatically. Bug: angleproject:372268711 Change-Id: I14f5c509bcbd9ea7d17101637e033652a68710a0 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6039117 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com> Reviewed-by: Yuxin Hu <yuxinhu@google.com>
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//
// 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.
//
// CLProgramVk.h: Defines the class interface for CLProgramVk, implementing CLProgramImpl.
#ifndef LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_
#define LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_
#include <cstdint>
#include "common/SimpleMutex.h"
#include "common/hash_containers.h"
#include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/CLKernelVk.h"
#include "libANGLE/renderer/vulkan/cl_types.h"
#include "libANGLE/renderer/vulkan/clspv_utils.h"
#include "libANGLE/renderer/vulkan/vk_cache_utils.h"
#include "libANGLE/renderer/vulkan/vk_helpers.h"
#include "libANGLE/renderer/CLProgramImpl.h"
#include "libANGLE/CLProgram.h"
#include "clspv/Compiler.h"
#include "vulkan/vulkan_core.h"
#include "spirv-tools/libspirv.h"
#include "spirv/unified1/NonSemanticClspvReflection.h"
namespace rx
{
class CLProgramVk : public CLProgramImpl
{
public:
using Ptr = std::unique_ptr<CLProgramVk>;
// TODO: Look into moving this information in CLKernelArgument
// https://anglebug.com/378514267
struct ImagePushConstant
{
VkPushConstantRange pcRange;
uint32_t ordinal;
};
struct SpvReflectionData
{
angle::HashMap<uint32_t, uint32_t> spvIntLookup;
angle::HashMap<uint32_t, std::string> spvStrLookup;
angle::HashMap<uint32_t, CLKernelVk::ArgInfo> kernelArgInfos;
angle::HashMap<std::string, uint32_t> kernelFlags;
angle::HashMap<std::string, std::string> kernelAttributes;
angle::HashMap<std::string, std::array<uint32_t, 3>> kernelCompileWorkgroupSize;
angle::HashMap<uint32_t, VkPushConstantRange> pushConstants;
angle::PackedEnumMap<SpecConstantType, uint32_t> specConstantIDs;
angle::PackedEnumBitSet<SpecConstantType, uint32_t> specConstantsUsed;
angle::HashMap<uint32_t, std::vector<ImagePushConstant>> imagePushConstants;
CLKernelArgsMap kernelArgsMap;
angle::HashMap<std::string, CLKernelArgument> kernelArgMap;
angle::HashSet<uint32_t> kernelIDs;
ClspvPrintfBufferStorage printfBufferStorage;
angle::HashMap<uint32_t, ClspvPrintfInfo> printfInfoMap;
};
// Output binary structure (for CL_PROGRAM_BINARIES query)
static constexpr uint32_t kBinaryVersion = 2;
struct ProgramBinaryOutputHeader
{
uint32_t headerVersion{kBinaryVersion};
cl_program_binary_type binaryType{CL_PROGRAM_BINARY_TYPE_NONE};
cl_build_status buildStatus{CL_BUILD_NONE};
};
struct ScopedClspvContext : angle::NonCopyable
{
ScopedClspvContext() = default;
~ScopedClspvContext() { clspvFreeOutputBuildObjs(mOutputBin, mOutputBuildLog); }
size_t mOutputBinSize{0};
char *mOutputBin{nullptr};
char *mOutputBuildLog{nullptr};
};
struct ScopedProgramCallback : angle::NonCopyable
{
ScopedProgramCallback() = delete;
ScopedProgramCallback(cl::Program *notify) : mNotify(notify) {}
~ScopedProgramCallback()
{
if (mNotify)
{
mNotify->callback();
}
}
cl::Program *mNotify{nullptr};
};
enum class BuildType
{
BUILD = 0,
COMPILE,
LINK,
BINARY
};
struct DeviceProgramData
{
std::vector<char> IR;
std::string buildLog;
angle::spirv::Blob binary;
SpvReflectionData reflectionData;
VkPushConstantRange pushConstRange{};
cl_build_status buildStatus{CL_BUILD_NONE};
cl_program_binary_type binaryType{CL_PROGRAM_BINARY_TYPE_NONE};
size_t numKernels() const { return reflectionData.kernelArgsMap.size(); }
size_t numKernelArgs(const std::string &kernelName) const
{
return containsKernel(kernelName) ? getKernelArgsMap().at(kernelName).size() : 0;
}
const CLKernelArgsMap &getKernelArgsMap() const { return reflectionData.kernelArgsMap; }
bool containsKernel(const std::string &name) const
{
return reflectionData.kernelArgsMap.contains(name);
}
std::string getKernelNames() const
{
std::string names;
for (auto name = getKernelArgsMap().begin(); name != getKernelArgsMap().end(); ++name)
{
names += name->first + (std::next(name) != getKernelArgsMap().end() ? ";" : "\0");
}
return names;
}
uint32_t getKernelFlags(const std::string &kernelName) const
{
if (containsKernel(kernelName))
{
return reflectionData.kernelFlags.at(kernelName);
}
return 0;
}
CLKernelArguments getKernelArguments(const std::string &kernelName) const
{
CLKernelArguments kargsCopy;
if (containsKernel(kernelName))
{
const CLKernelArguments &kargs = getKernelArgsMap().at(kernelName);
for (const CLKernelArgument &karg : kargs)
{
kargsCopy.push_back(karg);
}
}
return kargsCopy;
}
cl::WorkgroupSize getCompiledWorkgroupSize(const std::string &kernelName) const
{
cl::WorkgroupSize compiledWorkgroupSize{0, 0, 0};
if (reflectionData.kernelCompileWorkgroupSize.contains(kernelName))
{
for (size_t i = 0; i < compiledWorkgroupSize.size(); ++i)
{
compiledWorkgroupSize[i] =
reflectionData.kernelCompileWorkgroupSize.at(kernelName)[i];
}
}
return compiledWorkgroupSize;
}
std::string getKernelAttributes(const std::string &kernelName) const
{
if (containsKernel(kernelName))
{
return reflectionData.kernelAttributes.at(kernelName.c_str());
}
return std::string{};
}
const VkPushConstantRange *getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionInstructions type) const
{
const VkPushConstantRange *pushConstantRangePtr = nullptr;
if (reflectionData.pushConstants.contains(type))
{
pushConstantRangePtr = &reflectionData.pushConstants.at(type);
}
return pushConstantRangePtr;
}
inline const VkPushConstantRange *getGlobalOffsetRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantGlobalOffset);
}
inline const VkPushConstantRange *getGlobalSizeRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantGlobalSize);
}
inline const VkPushConstantRange *getEnqueuedLocalSizeRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantEnqueuedLocalSize);
}
inline const VkPushConstantRange *getNumWorkgroupsRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantNumWorkgroups);
}
inline const VkPushConstantRange *getRegionOffsetRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantRegionOffset);
}
inline const VkPushConstantRange *getRegionGroupOffsetRange() const
{
return getPushConstantRangeFromClspvReflectionType(
NonSemanticClspvReflectionPushConstantRegionGroupOffset);
}
const VkPushConstantRange *getImageDataChannelOrderRange(size_t ordinal) const
{
const VkPushConstantRange *pushConstantRangePtr = nullptr;
if (reflectionData.imagePushConstants.contains(
NonSemanticClspvReflectionImageArgumentInfoChannelOrderPushConstant))
{
for (const auto &imageConstant : reflectionData.imagePushConstants.at(
NonSemanticClspvReflectionImageArgumentInfoChannelOrderPushConstant))
{
if (static_cast<size_t>(imageConstant.ordinal) == ordinal)
{
pushConstantRangePtr = &imageConstant.pcRange;
}
}
}
return pushConstantRangePtr;
}
const VkPushConstantRange *getImageDataChannelDataTypeRange(size_t ordinal) const
{
const VkPushConstantRange *pushConstantRangePtr = nullptr;
if (reflectionData.imagePushConstants.contains(
NonSemanticClspvReflectionImageArgumentInfoChannelDataTypePushConstant))
{
for (const auto &imageConstant : reflectionData.imagePushConstants.at(
NonSemanticClspvReflectionImageArgumentInfoChannelDataTypePushConstant))
{
if (static_cast<size_t>(imageConstant.ordinal) == ordinal)
{
pushConstantRangePtr = &imageConstant.pcRange;
}
}
}
return pushConstantRangePtr;
}
const VkPushConstantRange *getNormalizedSamplerMaskRange(size_t ordinal) const
{
const VkPushConstantRange *pushConstantRangePtr = nullptr;
if (reflectionData.imagePushConstants.contains(
NonSemanticClspvReflectionNormalizedSamplerMaskPushConstant))
{
for (const auto &imageConstant : reflectionData.imagePushConstants.at(
NonSemanticClspvReflectionNormalizedSamplerMaskPushConstant))
{
if (static_cast<size_t>(imageConstant.ordinal) == ordinal)
{
pushConstantRangePtr = &imageConstant.pcRange;
}
}
}
return pushConstantRangePtr;
}
};
using DevicePrograms = angle::HashMap<const _cl_device_id *, DeviceProgramData>;
using LinkPrograms = std::vector<const DeviceProgramData *>;
using LinkProgramsList = std::vector<LinkPrograms>;
CLProgramVk(const cl::Program &program);
~CLProgramVk() override;
angle::Result init();
angle::Result init(const size_t *lengths, const unsigned char **binaries, cl_int *binaryStatus);
angle::Result build(const cl::DevicePtrs &devices,
const char *options,
cl::Program *notify) override;
angle::Result compile(const cl::DevicePtrs &devices,
const char *options,
const cl::ProgramPtrs &inputHeaders,
const char **headerIncludeNames,
cl::Program *notify) override;
angle::Result getInfo(cl::ProgramInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const override;
angle::Result getBuildInfo(const cl::Device &device,
cl::ProgramBuildInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const override;
angle::Result createKernel(const cl::Kernel &kernel,
const char *name,
CLKernelImpl::Ptr *kernelOut) override;
angle::Result createKernels(cl_uint numKernels,
CLKernelImpl::CreateFuncs &createFuncs,
cl_uint *numKernelsRet) override;
const DeviceProgramData *getDeviceProgramData(const char *kernelName) const;
const DeviceProgramData *getDeviceProgramData(const _cl_device_id *device) const;
CLPlatformVk *getPlatform() { return mContext->getPlatform(); }
const vk::ShaderModulePtr &getShaderModule() const { return mShader; }
bool buildInternal(const cl::DevicePtrs &devices,
std::string options,
std::string internalOptions,
BuildType buildType,
const LinkProgramsList &LinkProgramsList);
angle::spirv::Blob stripReflection(const DeviceProgramData *deviceProgramData);
angle::Result allocateDescriptorSet(const DescriptorSetIndex setIndex,
const vk::DescriptorSetLayout &descriptorSetLayout,
vk::CommandBufferHelperCommon *commandBuffer,
vk::DescriptorSetPointer *descriptorSetOut);
// Sets the status for given associated device programs
void setBuildStatus(const cl::DevicePtrs &devices, cl_build_status status);
vk::MetaDescriptorPool &getMetaDescriptorPool(DescriptorSetIndex index)
{
return mMetaDescriptorPools[index];
}
vk::DynamicDescriptorPoolPointer &getDynamicDescriptorPoolPointer(DescriptorSetIndex index)
{
return mDynamicDescriptorPools[index];
}
const angle::HashMap<uint32_t, ClspvPrintfInfo> *getPrintfDescriptors(
const std::string &kernelName) const;
private:
CLContextVk *mContext;
std::string mProgramOpts;
vk::ShaderModulePtr mShader;
DevicePrograms mAssociatedDevicePrograms;
vk::DescriptorSetArray<vk::MetaDescriptorPool> mMetaDescriptorPools;
vk::DescriptorSetArray<vk::DynamicDescriptorPoolPointer> mDynamicDescriptorPools;
angle::SimpleMutex mProgramMutex;
std::shared_ptr<angle::WaitableEvent> mAsyncBuildEvent;
};
class CLAsyncBuildTask : public angle::Closure
{
public:
CLAsyncBuildTask(CLProgramVk *programVk,
const cl::DevicePtrs &devices,
std::string options,
std::string internalOptions,
CLProgramVk::BuildType buildType,
const CLProgramVk::LinkProgramsList &LinkProgramsList,
cl::Program *notify)
: mProgramVk(programVk),
mDevices(devices),
mOptions(options),
mInternalOptions(internalOptions),
mBuildType(buildType),
mLinkProgramsList(LinkProgramsList),
mNotify(notify)
{}
void operator()() override;
private:
CLProgramVk *mProgramVk;
const cl::DevicePtrs mDevices;
std::string mOptions;
std::string mInternalOptions;
CLProgramVk::BuildType mBuildType;
const CLProgramVk::LinkProgramsList mLinkProgramsList;
cl::Program *mNotify;
};
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_