[Chore] (MG_Backend, MG_Backend/DirectGLES, MG_Backend/DirectVulkan, MG_Backend/MGPipe, MG_Backend/Record, MG_Benchmark/Buffer, MG_Benchmark/Container, MG_Benchmark/Driver, MG_Benchmark/Program, MG_Benchmark/ShaderCache, MG_Benchmark/Transpile, MG_Gbm, MG_Impl, MG_Impl/CGLImpl, MG_Impl/DyldInterpose, MG_Impl/EGLImpl, MG_Impl/GLImpl, MG_Impl/GLXImpl, MG_Impl/NSOpenGLImpl, MG_Impl/Pipe, MG_Impl/WGLImpl, MG_IntegrationTest, MG_IntegrationTest/Harness, MG_IntegrationTest/Scenarios, MG_Pipe, MG_Remote, MG_Remote/Client, MG_Remote/Protocol, MG_Remote/Server, MG_Remote/Transport, MG_Remote/Wire, MG_State/EGLState, MG_State/GLState, MG_Test, MG_Test/Backend, MG_Test/BackendLoader, MG_Test/Buffer, MG_Test/EGLState, MG_Test/Framebuffer, MG_Test/Gbm, MG_Test/Pipe, MG_Test/Pipeline, MG_Test/Program, MG_Test/Query, MG_Test/SelfTest, MG_Test/ShaderTranspiler, MG_Test/State, MG_Test/Swapchain, MG_Test/Texture, MG_Test/Util, MG_Test/VertexArray, MG_Test/Wire, MG_Util/Async, MG_Util/BackendLoaders, MG_Util/Converters, MG_Util/Damage, MG_Util/Debug, MG_Util/Math, MG_Util/Metrics, MG_Util/SelfTest, MG_Util/ShaderTranspiler, MG_Util/Texture, MG_Util/X11, MobileGL, scripts/data, tools/cts, tools/device_bench, tools/spikes, tools/trace_replay, tools/wgl-smoke): clang-format every C and C++ source

- Layout only: token streams match up to split literals, continuations and using order
- The size totals fall, so the structure baseline is lowered
This commit is contained in:
2026-10-16 06:30:38 -04:00
parent 76b10cc35b
commit f4994ee36e
747 changed files with 35842 additions and 36261 deletions
@@ -45,8 +45,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
// changing the fingerprint, which is exactly the memo-hazard property wanted here.
HashBytes(state, &env.params, sizeof(env.params));
if (!env.advertisedExtensions.empty()) {
HashBytes(state, env.advertisedExtensions.data(),
env.advertisedExtensions.size() * sizeof(GLExtension));
HashBytes(state, env.advertisedExtensions.data(), env.advertisedExtensions.size() * sizeof(GLExtension));
}
return state;
}
@@ -61,10 +61,10 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
// exact no-backend fallback it had before: extensions read as advertised, limits
// read as the frontend defaults.
BackendType backend = BackendType::Unknown;
MG_Backend::DynamicBackendParameters params{}; // by value, never by reference
MG_Backend::DynamicBackendParameters params{}; // by value, never by reference
Vector<GLExtension> advertisedExtensions;
Uint64 fingerprint = 0; // set by CaptureCompileEnv()
Uint64 fingerprint = 0; // set by CaptureCompileEnv()
// The FRONT-END half of the environment: the subset of the fields above that can
// change what glslang PRODUCES - the SPIR-V or the reflection - as opposed to what a
@@ -142,7 +142,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
// only such argument left in this classification. The moment anything hands this
// value to glslang - a TBuiltInResource field, a built-in constant - it MUST move
// into the fingerprint, exactly as its companions just did.
Uint64 frontendFingerprint = 0; // set by CaptureCompileEnv()
Uint64 frontendFingerprint = 0; // set by CaptureCompileEnv()
Bool HasBackend() const { return backend != BackendType::Unknown; }
// Whether the backend this env was captured against can CONSUME a module that still
@@ -163,12 +163,8 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
// compiles, unit tests) is the untouched module. Like nativeFloat64, each bit is L1
// key material of its own (SpirvTranslationKeyInputs), never part of the frontend
// fingerprint: glslang produces the same thing either way.
Bool DemotesTessellationPointSize() const {
return HasBackend() && !params.SupportsTessellationPointSize;
}
Bool DemotesGeometryPointSize() const {
return HasBackend() && !params.SupportsGeometryPointSize;
}
Bool DemotesTessellationPointSize() const { return HasBackend() && !params.SupportsTessellationPointSize; }
Bool DemotesGeometryPointSize() const { return HasBackend() && !params.SupportsGeometryPointSize; }
// Matches the historical rule exactly: with no active backend every extension counts
// as advertised, because the frontend then has nothing to gate against.
Bool IsExtensionAdvertised(GLExtension extension) const {
@@ -44,47 +44,158 @@ namespace MobileGL {
//
// "main" is deliberately absent from both.
inline constexpr std::string_view kEsslBuiltinFunctionNames[] = {
"EmitVertex", "EndPrimitive",
"abs", "acos", "acosh", "all", "any", "asin", "asinh", "atan", "atanh",
"atomicAdd", "atomicAnd", "atomicCompSwap", "atomicCounter",
"atomicCounterDecrement", "atomicCounterIncrement", "atomicExchange",
"atomicMax", "atomicMin", "atomicOr", "atomicXor",
"barrier", "bitCount", "bitfieldExtract", "bitfieldInsert", "bitfieldReverse",
"ceil", "clamp", "cos", "cosh", "cross",
"dFdx", "dFdy", "degrees", "determinant", "distance", "dot",
"equal", "exp", "exp2",
"faceforward", "findLSB", "findMSB", "floatBitsToInt", "floatBitsToUint",
"floor", "fma", "fract", "frexp", "fwidth",
"greaterThan", "greaterThanEqual", "groupMemoryBarrier",
"imageAtomicAdd", "imageAtomicAnd", "imageAtomicCompSwap",
"imageAtomicExchange", "imageAtomicMax", "imageAtomicMin", "imageAtomicOr",
"imageAtomicXor", "imageLoad", "imageSize", "imageStore", "imulExtended",
"intBitsToFloat", "interpolateAtCentroid", "interpolateAtOffset",
"interpolateAtSample", "inverse", "inversesqrt", "isinf", "isnan",
"ldexp", "length", "length_squared", "lessThan", "lessThanEqual", "log", "log2",
"matrixCompMult", "max", "max3", "memoryBarrier",
"memoryBarrierAtomicCounter", "memoryBarrierBuffer", "memoryBarrierImage",
"memoryBarrierShared", "mid3", "min", "min3", "mix", "mod", "modf",
"normalize", "not", "notEqual",
"EmitVertex",
"EndPrimitive",
"abs",
"acos",
"acosh",
"all",
"any",
"asin",
"asinh",
"atan",
"atanh",
"atomicAdd",
"atomicAnd",
"atomicCompSwap",
"atomicCounter",
"atomicCounterDecrement",
"atomicCounterIncrement",
"atomicExchange",
"atomicMax",
"atomicMin",
"atomicOr",
"atomicXor",
"barrier",
"bitCount",
"bitfieldExtract",
"bitfieldInsert",
"bitfieldReverse",
"ceil",
"clamp",
"cos",
"cosh",
"cross",
"dFdx",
"dFdy",
"degrees",
"determinant",
"distance",
"dot",
"equal",
"exp",
"exp2",
"faceforward",
"findLSB",
"findMSB",
"floatBitsToInt",
"floatBitsToUint",
"floor",
"fma",
"fract",
"frexp",
"fwidth",
"greaterThan",
"greaterThanEqual",
"groupMemoryBarrier",
"imageAtomicAdd",
"imageAtomicAnd",
"imageAtomicCompSwap",
"imageAtomicExchange",
"imageAtomicMax",
"imageAtomicMin",
"imageAtomicOr",
"imageAtomicXor",
"imageLoad",
"imageSize",
"imageStore",
"imulExtended",
"intBitsToFloat",
"interpolateAtCentroid",
"interpolateAtOffset",
"interpolateAtSample",
"inverse",
"inversesqrt",
"isinf",
"isnan",
"ldexp",
"length",
"length_squared",
"lessThan",
"lessThanEqual",
"log",
"log2",
"matrixCompMult",
"max",
"max3",
"memoryBarrier",
"memoryBarrierAtomicCounter",
"memoryBarrierBuffer",
"memoryBarrierImage",
"memoryBarrierShared",
"mid3",
"min",
"min3",
"mix",
"mod",
"modf",
"normalize",
"not",
"notEqual",
"outerProduct",
"packHalf2x16", "packSnorm2x16", "packSnorm4x8", "packUnorm2x16",
"packUnorm4x8", "pow",
"radians", "reflect", "refract", "round", "roundEven",
"sign", "sin", "sinh", "smoothstep", "sqrt", "step",
"tan", "tanh", "texelFetch", "texelFetchOffset", "texture",
"textureGather", "textureGatherOffset", "textureGatherOffsets",
"textureGrad", "textureGradOffset", "textureLod", "textureLodOffset",
"textureOffset", "textureProj", "textureProjGrad", "textureProjGradOffset",
"textureProjLod", "textureProjLodOffset", "textureProjOffset", "textureSize",
"transpose", "trunc",
"uaddCarry", "uintBitsToFloat", "umulExtended", "unpackHalf2x16",
"unpackSnorm2x16", "unpackSnorm4x8", "unpackUnorm2x16", "unpackUnorm4x8",
"packHalf2x16",
"packSnorm2x16",
"packSnorm4x8",
"packUnorm2x16",
"packUnorm4x8",
"pow",
"radians",
"reflect",
"refract",
"round",
"roundEven",
"sign",
"sin",
"sinh",
"smoothstep",
"sqrt",
"step",
"tan",
"tanh",
"texelFetch",
"texelFetchOffset",
"texture",
"textureGather",
"textureGatherOffset",
"textureGatherOffsets",
"textureGrad",
"textureGradOffset",
"textureLod",
"textureLodOffset",
"textureOffset",
"textureProj",
"textureProjGrad",
"textureProjGradOffset",
"textureProjLod",
"textureProjLodOffset",
"textureProjOffset",
"textureSize",
"transpose",
"trunc",
"uaddCarry",
"uintBitsToFloat",
"umulExtended",
"unpackHalf2x16",
"unpackSnorm2x16",
"unpackSnorm4x8",
"unpackUnorm2x16",
"unpackUnorm4x8",
"usubBorrow",
};
inline bool IsEsslBuiltinFunctionName(std::string_view name) {
return std::binary_search(std::begin(kEsslBuiltinFunctionNames),
std::end(kEsslBuiltinFunctionNames), name);
return std::binary_search(std::begin(kEsslBuiltinFunctionNames), std::end(kEsslBuiltinFunctionNames),
name);
}
// The parse-level subset, sorted for std::binary_search.
@@ -115,8 +226,8 @@ namespace MobileGL {
};
inline bool IsLexicalPreemptRenameName(std::string_view name) {
return std::binary_search(std::begin(kLexicalPreemptRenameNames),
std::end(kLexicalPreemptRenameNames), name);
return std::binary_search(std::begin(kLexicalPreemptRenameNames), std::end(kLexicalPreemptRenameNames),
name);
}
} // namespace ShaderTranspiler
} // namespace MG_Util
File diff suppressed because it is too large Load Diff
@@ -46,10 +46,8 @@ namespace MobileGL {
// for the WHOLE program (CompileEnv::ConsumesFloat64Natively() minus the
// 64-bit-vertex-input exception) before touching any module. Do not re-derive it
// per module: the global UBO is one buffer every stage reads.
static bool SanitizeAndOptimizeBinary(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool validateOutput = true,
bool enableSpirvValidation = false,
static bool SanitizeAndOptimizeBinary(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool validateOutput = true, bool enableSpirvValidation = false,
bool nativeFloat64 = false);
// Demotes DrawIndex/BaseInstance/BaseVertex builtins to plain Private globals
// (mg_DrawID/mg_BaseInstance/mg_BaseVertex) so SPIRV-Cross can emit ESSL.
@@ -64,15 +62,13 @@ namespace MobileGL {
// runs, instead of every draw made with it becoming a silent no-op. Only for the
// DirectGLES transpile path on a driver WITHOUT GL_OES_viewport_array; gl_Layer is
// deliberately left alone, being core in ESSL 3.20 geometry shaders.
static bool LowerViewportIndexForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool LowerViewportIndexForEssl(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Moves a geometry stage's emits behind its memory writes (DeferGeometryEmitsPass), for
// an ES driver that drops storage writes made after EmitVertex(). `outDeferred` says
// whether the module was rewritten; a module the pass declines comes back unchanged.
static bool DeferGeometryEmitsForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary, bool& outDeferred,
bool enableSpirvValidation = false);
static bool DeferGeometryEmitsForEssl(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool& outDeferred, bool enableSpirvValidation = false);
// Whether the module declares an output decorated BuiltIn ViewportIndex, i.e.
// whether the pass above has anything to do. The gate that keeps every other
// stage off an optimizer round trip it does not need.
@@ -87,10 +83,8 @@ namespace MobileGL {
// only. See ClampMultisampleFetchPass, which also says what
// deferDirectUnsignedFetches hands over to the caller's runtime clamp.
static bool ClampMultisampleFetchesForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
Int32 maxColorSamples,
Int32 maxIntegerSamples,
Int32 maxDepthSamples,
Vector<uint32_t>& outputBinary, Int32 maxColorSamples,
Int32 maxIntegerSamples, Int32 maxDepthSamples,
Int32 advertisedMaxSamples,
bool enableSpirvValidation = false,
bool deferDirectUnsignedFetches = false);
@@ -172,9 +166,9 @@ namespace MobileGL {
// located block across any tessellation or geometry boundary; only for the
// DirectGLES transpile path, and only when the driver POST says so. See
// StripIoBlockLocationsPass.
static bool StripIoBlockLocationsForEssl(const Vector<Uint32>& inputBinary,
bool stripInputBlocks, bool stripOutputBlocks,
bool& strippedAny, Vector<uint32_t>& outputBinary,
static bool StripIoBlockLocationsForEssl(const Vector<Uint32>& inputBinary, bool stripInputBlocks,
bool stripOutputBlocks, bool& strippedAny,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Drops RelaxedPrecision member decorations from uniform-block structs so
// SPIRV-Cross prints the same (highp) member precision in every stage; ES
@@ -182,19 +176,18 @@ namespace MobileGL {
// Only for the DirectGLES transpile path.
static bool StripUboMemberRelaxedPrecisionForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Removes NoPerspective decorations so SPIRV-Cross emits plain (smooth) ESSL varyings.
// DirectGLES fallback only, for devices lacking GL_NV_shader_noperspective_interpolation
// (SPIRV-Cross would otherwise require that extension and the driver would reject it).
static bool StripNoPerspectiveForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool StripNoPerspectiveForEssl(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Emulates noperspective (screen-linear) interpolation via gl_Position.w / gl_FragCoord.w
// so no NV extension is needed; strips what it cannot emulate. DirectGLES fallback for
// devices lacking GL_NV_shader_noperspective_interpolation. See EmulateNoPerspectivePass.
static bool EmulateNoPerspectiveForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Makes every index into a fragment-output array a constant integral
// expression, which is what GLSL ES requires and SPIR-V does not. Runs the
// stock folding chain first (loop unrolling folds the loop-derived indices
@@ -206,7 +199,7 @@ namespace MobileGL {
// See LegalizeFragmentOutputIndexPass.
static bool LegalizeFragmentOutputIndexingForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Makes every index into an ARRAY OF SHADER STORAGE BLOCKS or an ARRAY OF IMAGE
// UNIFORMS a constant integral expression. Desktop GL allows any
// dynamically-uniform index in either; ES keeps the ES 3.1
@@ -223,8 +216,8 @@ namespace MobileGL {
// when no such array is indexed dynamically, which is every shader but a
// handful. See LegalizeResourceArrayIndexPass.
static bool LegalizeResourceArrayIndexingForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Collapses each synthesized gl_AtomicCounterBlock_<N> into one uint array at
// offset 0, re-indexing every counter access to the element that used to sit at
// its byte offset. glslang preserves the application's layout(offset = N) as the
@@ -256,8 +249,7 @@ namespace MobileGL {
// and must see the module unchanged. Copies the input through untouched when the
// module declares no such image, which is every shader but a handful. See
// Lower1DArrayImagesPass for what it declines and why.
static bool Lower1DArrayImagesForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool Lower1DArrayImagesForEssl(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// The SAMPLED-image counterpart. SPIRV-Cross widens a 1D sampler's COORDINATE for
// ES and prints the OFFSET and GRADIENT operands with their original 1D arity, so
@@ -280,8 +272,7 @@ namespace MobileGL {
// natively. See BakeImageFormatsPass for what it declines and why.
static bool BakeImageFormatsForEssl(const Vector<Uint32>& inputBinary,
const UnorderedMap<String, Uint>& glFormatByName,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
Vector<uint32_t>& outputBinary, bool enableSpirvValidation = false);
// Whether the module declares a storage image with no format qualifier at all,
// i.e. whether BakeImageFormatsForEssl could change anything. One module parse,
// so the ~every shader that declares none pays no optimizer run.
@@ -312,8 +303,7 @@ namespace MobileGL {
// ESSL route even WITH GL_NV_image_formats, because SPIRV-Cross throws for them
// rather than printing a token - which is the whole set a driver that advertises
// the extension still needs. See WidenImageFormatsPass.
static bool WidenImageFormatsForEssl(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool WidenImageFormatsForEssl(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool onlyFormatsSpirvCrossRefusesToPrint = false,
bool enableSpirvValidation = false);
// Whether the module declares a storage image WidenImageFormatsForEssl would
@@ -345,13 +335,12 @@ namespace MobileGL {
static Uint SplitCoreEsslBufferImageFormat(Uint glInternalFormat);
static bool RebaseInstanceIndexForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Builds the non-indexed-draw variant of a vertex shader: every gl_BaseVertex
// read becomes zero, which is what GL defines for a command carrying no
// baseVertex parameter while Vulkan's builtin would report firstVertex.
// See ZeroBaseVertexPass.
static bool ZeroBaseVertexForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool ZeroBaseVertexForVulkan(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Drops `layout(pixel_center_integer)` from a fragment module, which the Vulkan
// environment forbids, and shifts every gl_FragCoord.xy read by -0.5 to keep its
@@ -362,9 +351,8 @@ namespace MobileGL {
// Builds the variant of a pre-rasterization module that a non-layered framebuffer
// draws with: every gl_Layer store writes 0, since GL ignores gl_Layer there. A
// module that writes no Layer comes back unchanged. See ZeroLayerOutputPass.
static bool ZeroLayerOutputForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
static bool ZeroLayerOutputForVulkan(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Builds the variant of a fragment module that a draw without multisample fragment
// operations (single-sample framebuffer, or MULTISAMPLE disabled) uses: every
// gl_SampleMask store writes all-ones, since GL ignores the mask there. A module
@@ -393,8 +381,7 @@ namespace MobileGL {
// minimum). atomicScratchReads reads the scratch back through atomic loads,
// for a driver whose plain shared-memory loads go stale across barrier()
// (SelfTest::SharedMemoryLoadsGoStale). See EmulateSubgroupsPass.
static bool EmulateSubgroupsForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool EmulateSubgroupsForVulkan(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
Uint32 maxWorkgroupScratchBytes,
bool enableSpirvValidation = false,
bool atomicScratchReads = false);
@@ -423,7 +410,7 @@ namespace MobileGL {
// VertexInputStateFactory::ToVkVertexFormat.
static bool PackDoubleVertexInputsForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Adds the Invariant decoration to every Position builtin output. GL apps
// routinely rely on cross-program position invariance for multi-pass
// equality depth tests (e.g. GEQUAL re-draws of the same geometry), and
@@ -431,22 +418,21 @@ namespace MobileGL {
// decoration. DirectVulkan only.
static bool DecoratePositionInvariantForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
bool enableSpirvValidation = false);
// Replaces the declared format of float storage images with Unknown and adds the
// matching SPIR-V capabilities. DirectVulkan uses this only when both Vulkan
// shaderStorageImage*WithoutFormat features are enabled, allowing the
// glBindImageTexture format to select the descriptor view at runtime. Integer
// storage images deliberately keep their declared format for GL-compatible bit
// reinterpretation paths (for example, R32F storage accessed as r32ui).
static bool UseUnformattedFloatStorageImagesForVulkan(
const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
static bool UseUnformattedFloatStorageImagesForVulkan(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
// Rewrites every 64-bit float in the module to a 32-bit one, preserving every
// block offset and stride exactly (see DemoteFloat64Pass). Already part of
// SanitizeAndOptimizeBinary, which is where production reaches it; exposed
// separately so a test can drive the demotion on its own.
static bool DemoteFloat64ToFloat32(const Vector<Uint32>& inputBinary,
Vector<uint32_t>& outputBinary,
static bool DemoteFloat64ToFloat32(const Vector<Uint32>& inputBinary, Vector<uint32_t>& outputBinary,
bool enableSpirvValidation = false);
static Result<String> DecompileShader(SpvcSession& session);
@@ -489,9 +475,9 @@ namespace MobileGL {
// groups look like the second.
enum class SpecializationFailure {
None,
UnknownConstantId, // GL_INVALID_VALUE
UnknownEntryPoint, // GL_INVALID_VALUE
ModuleRejected, // COMPILE_STATUS false + info log
UnknownConstantId, // GL_INVALID_VALUE
UnknownEntryPoint, // GL_INVALID_VALUE
ModuleRejected, // COMPILE_STATUS false + info log
};
// What a specialized module turns into: the GLSL the ordinary pipeline compiles,
@@ -512,10 +498,12 @@ namespace MobileGL {
GLenum xfbBufferMode = GL_INTERLEAVED_ATTRIBS;
};
static Result<SpecializedModule> SpecializeAndDecompileSpirvModule(
const Vector<Uint32>& spirv, GLenum shaderType, const String& entryPoint,
const Vector<Uint32>& constantIds, const Vector<Uint32>& constantValues,
SpecializationFailure& outFailure);
static Result<SpecializedModule> SpecializeAndDecompileSpirvModule(const Vector<Uint32>& spirv,
GLenum shaderType,
const String& entryPoint,
const Vector<Uint32>& constantIds,
const Vector<Uint32>& constantValues,
SpecializationFailure& outFailure);
// spirv-val over an application-supplied module, against the environment MobileGL
// parses and emits under. glShaderBinary is where a malformed module has to be
@@ -643,10 +631,9 @@ namespace MobileGL {
// trip the backends' own "reads a located input" refusal against a name the
// application never wrote.
static Bool DemoteTessellationGeometryPointSizeForProgram(
Vector<Vector<Uint32>>& modules, const Vector<GLenum>& shaderTypes,
Bool demoteTessellation, Bool demoteGeometry, Bool captureRequestsPointSize,
PointSizeDemotionOutcome& outcome, bool validateOutput = true,
bool enableSpirvValidation = false);
Vector<Vector<Uint32>>& modules, const Vector<GLenum>& shaderTypes, Bool demoteTessellation,
Bool demoteGeometry, Bool captureRequestsPointSize, PointSizeDemotionOutcome& outcome,
bool validateOutput = true, bool enableSpirvValidation = false);
// True when the module still declares a 64-bit float type. After
// SanitizeAndOptimizeBinary that can only mean DemoteFloat64Pass declined the
@@ -53,7 +53,12 @@ namespace {
// contains "/*" one byte in, and a naive search for that opener treats the rest of the file as
// an unterminated comment.
MobileGL::String MaskCommentsAndQuotedText(const MobileGL::String& source) {
enum class Region { Code, SingleLineComment, MultiLineComment, QuotedText };
enum class Region {
Code,
SingleLineComment,
MultiLineComment,
QuotedText
};
MobileGL::String masked = source;
Region region = Region::Code;
@@ -216,13 +221,26 @@ namespace {
// desktop and ES version) so the normalizer never starts rejecting a form it used to accept.
bool IsRecognizedGlslVersion(unsigned version) {
switch (version) {
case 100: case 110: case 120: case 130: case 140: case 150:
case 300: case 310: case 320:
case 330: case 400: case 410: case 420: case 430:
case 440: case 450: case 460:
return true;
default:
return false;
case 100:
case 110:
case 120:
case 130:
case 140:
case 150:
case 300:
case 310:
case 320:
case 330:
case 400:
case 410:
case 420:
case 430:
case 440:
case 450:
case 460:
return true;
default:
return false;
}
}
@@ -396,8 +414,7 @@ namespace {
if (info.hasValidVersionDirective && probe < lineEnd) {
const void* hash = std::memchr(code.data() + probe, '#', lineEnd - probe);
if (hash != nullptr) {
recordIfRedundant(static_cast<SizeT>(static_cast<const char*>(hash) - code.data()),
lineEnd);
recordIfRedundant(static_cast<SizeT>(static_cast<const char*>(hash) - code.data()), lineEnd);
}
}
} else {
@@ -427,10 +444,9 @@ namespace {
probe++;
SkipDirectiveWhitespace(code, probe, lineEnd);
const MobileGL::String behavior = ReadDirectiveIdentifier(code, probe, lineEnd);
const bool isGpuShader5 = extension == "GL_ARB_gpu_shader5" ||
extension == "GL_NV_gpu_shader5";
const bool enablesExtension = behavior == "enable" || behavior == "require" ||
behavior == "warn";
const bool isGpuShader5 = extension == "GL_ARB_gpu_shader5" || extension == "GL_NV_gpu_shader5";
const bool enablesExtension =
behavior == "enable" || behavior == "require" || behavior == "warn";
if (!extension.empty()) {
info.namedExtensions.insert(extension);
if (enablesExtension) info.enabledExtensions.insert(extension);
@@ -493,8 +509,7 @@ namespace {
return "#version " + std::to_string(info.version) + " core\n";
}
const bool useLegacyDesktopVersion =
info.version < 400 && !info.enablesGpuShader5;
const bool useLegacyDesktopVersion = info.version < 400 && !info.enablesGpuShader5;
// The trailing marker records that this 330 came from a legacy declaration
// (or none at all), so the compile-failure retry may re-raise it to 460.
// An application's own "#version 330" never carries it and keeps strict
@@ -641,10 +656,9 @@ namespace {
bool IsInDirectiveLine(const Vector<std::pair<SizeT, SizeT>>& ranges, SizeT offset) {
// Ranges are disjoint and sorted, so the only candidate is the last one starting at or
// before the offset.
const auto next = std::upper_bound(ranges.begin(), ranges.end(), offset,
[](SizeT value, const std::pair<SizeT, SizeT>& range) {
return value < range.first;
});
const auto next =
std::upper_bound(ranges.begin(), ranges.end(), offset,
[](SizeT value, const std::pair<SizeT, SizeT>& range) { return value < range.first; });
return next != ranges.begin() && offset < std::prev(next)->second;
}
@@ -657,8 +671,8 @@ namespace {
};
bool IsStatementKeywordToken(const CodeToken& token) {
return std::binary_search(std::begin(kStatementKeywordsBeforeCall),
std::end(kStatementKeywordsBeforeCall), std::string_view(token.text));
return std::binary_search(std::begin(kStatementKeywordsBeforeCall), std::end(kStatementKeywordsBeforeCall),
std::string_view(token.text));
}
// A brace counter over raw tokens is preprocessor-blind: it counts the braces of BOTH arms of
@@ -856,7 +870,8 @@ namespace {
if (lastLine) lineEnd = source.size();
SizeT probe = lineStart;
while (probe < lineEnd && (source[probe] == ' ' || source[probe] == '\t')) probe++;
while (probe < lineEnd && (source[probe] == ' ' || source[probe] == '\t'))
probe++;
const bool isLineDirective = masked.compare(probe, 5, "#line") == 0 &&
(probe + 5 >= lineEnd || !IsIdentifierChar(source[probe + 5]));
@@ -890,7 +905,6 @@ namespace {
source = std::move(result);
}
MobileGL::String TrimDirectiveToken(const MobileGL::String& token) {
SizeT start = 0;
while (start < token.size() && std::isspace(static_cast<unsigned char>(token[start]))) {
@@ -907,13 +921,20 @@ namespace {
// GL_SHADER_STAGE bit of a stage, as GL_SUBGROUP_SUPPORTED_STAGES_KHR spells it.
GLbitfield SubgroupStageBit(MobileGL::ShaderStage stage) {
switch (MobileGL::MG_Util::ConvertShaderStageToGLEnum(stage)) {
case GL_VERTEX_SHADER: return GL_VERTEX_SHADER_BIT;
case GL_TESS_CONTROL_SHADER: return GL_TESS_CONTROL_SHADER_BIT;
case GL_TESS_EVALUATION_SHADER: return GL_TESS_EVALUATION_SHADER_BIT;
case GL_GEOMETRY_SHADER: return GL_GEOMETRY_SHADER_BIT;
case GL_FRAGMENT_SHADER: return GL_FRAGMENT_SHADER_BIT;
case GL_COMPUTE_SHADER: return GL_COMPUTE_SHADER_BIT;
default: return 0;
case GL_VERTEX_SHADER:
return GL_VERTEX_SHADER_BIT;
case GL_TESS_CONTROL_SHADER:
return GL_TESS_CONTROL_SHADER_BIT;
case GL_TESS_EVALUATION_SHADER:
return GL_TESS_EVALUATION_SHADER_BIT;
case GL_GEOMETRY_SHADER:
return GL_GEOMETRY_SHADER_BIT;
case GL_FRAGMENT_SHADER:
return GL_FRAGMENT_SHADER_BIT;
case GL_COMPUTE_SHADER:
return GL_COMPUTE_SHADER_BIT;
default:
return 0;
}
}
@@ -986,7 +1007,8 @@ namespace {
const SizeT replaceLen = lineEnd - lineStart + (hasLineBreak ? 1 : 0);
SizeT probe = 0;
const auto skipSpace = [&] {
while (probe < line.size() && std::isspace(static_cast<unsigned char>(line[probe]))) probe++;
while (probe < line.size() && std::isspace(static_cast<unsigned char>(line[probe])))
probe++;
};
skipSpace();
if (probe < line.size() && line[probe] == '#') {
@@ -999,15 +1021,17 @@ namespace {
probe += extensionLen;
skipSpace();
const SizeT nameStart = probe;
while (probe < line.size() && IsIdentifierChar(line[probe])) probe++;
while (probe < line.size() && IsIdentifierChar(line[probe]))
probe++;
const MobileGL::String name = line.substr(nameStart, probe - nameStart);
skipSpace();
if (!name.empty() && probe < line.size() && line[probe] == ':' &&
IsUnavailableShaderExtension(env, stage, name)) {
const MobileGL::String behavior = TrimDirectiveToken(line.substr(probe + 1));
if (behavior == "require") {
edits.push_back({lineStart, replaceLen,
"#error " + name + " is not available to this shader stage in MobileGL\n"});
edits.push_back(
{lineStart, replaceLen,
"#error " + name + " is not available to this shader stage in MobileGL\n"});
} else if (behavior == "enable" || behavior == "warn") {
edits.push_back({lineStart, replaceLen, "\n"});
}
@@ -1057,9 +1081,8 @@ namespace {
}
const auto isDecimalInteger = [](const String& text) {
return !text.empty() && std::all_of(text.begin(), text.end(), [](char ch) {
return ch >= '0' && ch <= '9';
});
return !text.empty() &&
std::all_of(text.begin(), text.end(), [](char ch) { return ch >= '0' && ch <= '9'; });
};
const Vector<CodeToken> tokens = TokenizeCode(source);
@@ -1185,17 +1208,17 @@ namespace {
bool IsParameterQualifierKeyword(const String& text) {
static constexpr std::string_view kQualifiers[] = {
"const", "in", "out", "inout", "highp", "mediump", "lowp",
"precise", "coherent", "volatile", "restrict", "readonly", "writeonly",
"const", "in", "out", "inout", "highp", "mediump", "lowp",
"precise", "coherent", "volatile", "restrict", "readonly", "writeonly",
};
return std::find(std::begin(kQualifiers), std::end(kQualifiers), std::string_view(text)) !=
std::end(kQualifiers);
std::end(kQualifiers);
}
// The #if/#ifdef/#ifndef nesting in effect at each offset, as (offset, depth) marks. Every
// mark takes effect at the END of the directive line that changed the depth.
Vector<std::pair<SizeT, int>> BuildConditionalDepthMarks(const MobileGL::String& source,
const Vector<std::pair<SizeT, SizeT>>& ranges) {
const Vector<std::pair<SizeT, SizeT>>& ranges) {
Vector<std::pair<SizeT, int>> marks;
marks.emplace_back(static_cast<SizeT>(0), 0);
int depth = 0;
@@ -1219,10 +1242,9 @@ namespace {
}
int ConditionalDepthAt(const Vector<std::pair<SizeT, int>>& marks, SizeT offset) {
const auto next = std::upper_bound(marks.begin(), marks.end(), offset,
[](SizeT value, const std::pair<SizeT, int>& mark) {
return value < mark.first;
});
const auto next =
std::upper_bound(marks.begin(), marks.end(), offset,
[](SizeT value, const std::pair<SizeT, int>& mark) { return value < mark.first; });
return next == marks.begin() ? 0 : std::prev(next)->second;
}
@@ -1346,8 +1368,7 @@ namespace {
if (count < 4 || !HasBalancedBraces(tokens)) return;
const Vector<std::pair<SizeT, SizeT>> directiveRanges = FindDirectiveLineRanges(source);
const Vector<std::pair<SizeT, int>> conditionalDepth =
BuildConditionalDepthMarks(source, directiveRanges);
const Vector<std::pair<SizeT, int>> conditionalDepth = BuildConditionalDepthMarks(source, directiveRanges);
struct SubroutineType {
String returnText; // empty until the type declaration itself is seen
@@ -1519,7 +1540,7 @@ namespace {
const String signature = type.returnText + " " + variable + "(" + parameterList + ")";
text += signature + "; ";
appended += signature + " {\n " + (type.returnText == "void" ? "" : "return ") +
type.implementations.front() + "(" + arguments + ");\n}\n";
type.implementations.front() + "(" + arguments + ");\n}\n";
}
prototypes.emplace_back(site.begin, std::move(text));
}
@@ -1533,10 +1554,9 @@ namespace {
for (const UniformSite& site : uniformSites) {
BlankSpan(source, site.begin, site.end);
}
std::sort(prototypes.begin(), prototypes.end(),
[](const std::pair<SizeT, String>& a, const std::pair<SizeT, String>& b) {
return a.first < b.first;
});
std::sort(
prototypes.begin(), prototypes.end(),
[](const std::pair<SizeT, String>& a, const std::pair<SizeT, String>& b) { return a.first < b.first; });
for (auto it = prototypes.rbegin(); it != prototypes.rend(); ++it) {
source.insert(it->first, it->second);
}
@@ -1593,8 +1613,8 @@ namespace {
identifierEnd++;
}
const SizeT identifierLen = identifierEnd - cursor;
if (identifierLen == 6 && (source.compare(cursor, 6, "packed") == 0 ||
source.compare(cursor, 6, "shared") == 0)) {
if (identifierLen == 6 &&
(source.compare(cursor, 6, "packed") == 0 || source.compare(cursor, 6, "shared") == 0)) {
source.replace(cursor, 6, "std140");
}
cursor = identifierEnd;
@@ -1610,8 +1630,7 @@ namespace {
// tracker rather than a bare offset because this pass rewrites identifiers first, and those
// rewrites are raw text: a leading comment banner mentioning `varying` or `texture2D` moves the
// anchor, and the tracker notices.
void ModernizeLegacyGLSL(MobileGL::ShaderStage stage, MobileGL::String& source,
AfterVersionAnchor& afterVersion) {
void ModernizeLegacyGLSL(MobileGL::ShaderStage stage, MobileGL::String& source, AfterVersionAnchor& afterVersion) {
// Precision qualifiers (highp/mediump/lowp and default-precision statements) are legal and
// ignored in the normalized desktop core profiles, so glslang handles them natively.
@@ -1781,11 +1800,10 @@ namespace {
// and defines the ES macros regardless of behavior, so `all` adds no information - and
// emitting all thirty-five for a source that named nothing is exactly the broad rewrite
// the named-extensions-only policy exists to avoid.
const bool hasMacroToRestore =
std::any_of(info.namedExtensions.begin(), info.namedExtensions.end(),
[&info](const MobileGL::String& extension) {
return IsEsOnlyPreambleExtensionMacro(extension, info.version);
});
const bool hasMacroToRestore = std::any_of(info.namedExtensions.begin(), info.namedExtensions.end(),
[&info](const MobileGL::String& extension) {
return IsEsOnlyPreambleExtensionMacro(extension, info.version);
});
// Nothing the desktop preamble is missing: leave the source byte-identical.
if (!hasMacroToRestore) return;
@@ -1857,9 +1875,8 @@ namespace {
return;
}
constexpr const char* shim =
"uniform int mg_NumSamples;\n"
"#define gl_NumSamples mg_NumSamples\n";
constexpr const char* shim = "uniform int mg_NumSamples;\n"
"#define gl_NumSamples mg_NumSamples\n";
source.insert(afterVersion.Get(source), shim);
}
@@ -1873,8 +1890,8 @@ namespace {
if (tokens[i].text != "uniform") continue;
// The type is the first identifier after `uniform` that is not a precision qualifier.
SizeT type = i + 1;
while (type < tokens.size() && (tokens[type].text == "lowp" || tokens[type].text == "mediump" ||
tokens[type].text == "highp"))
while (type < tokens.size() &&
(tokens[type].text == "lowp" || tokens[type].text == "mediump" || tokens[type].text == "highp"))
++type;
if (type >= tokens.size() || !IsIdentifierToken(tokens[type])) continue;
const bool isExternal = tokens[type].text == "samplerExternalOES";
@@ -1887,7 +1904,8 @@ namespace {
into->push_back(tokens[j].text);
++j;
if (j < tokens.size() && tokens[j].text == "[") {
while (j < tokens.size() && tokens[j].text != "]") ++j;
while (j < tokens.size() && tokens[j].text != "]")
++j;
++j;
}
continue;
@@ -1910,7 +1928,8 @@ namespace {
// refuse the source, a name declared both ways is left out - a 2D binding is the safe reading.
constexpr const char* kExternalSamplerMarkerPrefix = "/*mobilegl-external-samplers:";
void LowerExternalSamplers(MobileGL::ShaderStage stage, MobileGL::String& source, AfterVersionAnchor& afterVersion) {
void LowerExternalSamplers(MobileGL::ShaderStage stage, MobileGL::String& source,
AfterVersionAnchor& afterVersion) {
if (source.find("samplerExternalOES") == MobileGL::String::npos) return;
Vector<MobileGL::String> names;
MobileGL::String preprocessed;
@@ -2001,7 +2020,6 @@ namespace MobileGL {
ModernizeLegacyGLSL(stage, source, afterVersion);
InjectDepthRangeBuiltinShim(stage, source, afterVersion);
InjectNumSamplesBuiltinShim(stage, originalLanguage, source, afterVersion);
}
Vector<String> ExternalSamplerUniformNames(const String& preprocessedSource) {
@@ -2042,8 +2060,8 @@ namespace MobileGL {
const ShaderLanguageInfo info = InspectShaderLanguage(preprocessedSource);
EsOrigin origin;
origin.version = esVersion;
origin.gpuShader5 = ExtensionTurnedOn(info, "GL_EXT_gpu_shader5") ||
ExtensionTurnedOn(info, "GL_OES_gpu_shader5");
origin.gpuShader5 =
ExtensionTurnedOn(info, "GL_EXT_gpu_shader5") || ExtensionTurnedOn(info, "GL_OES_gpu_shader5");
return origin;
}
@@ -2119,18 +2137,20 @@ namespace MobileGL {
const SizeT length = source.size();
SizeT i = 0;
Int layoutParenDepth = 0; // >0 while inside layout(...)
Int layoutParenDepth = 0; // >0 while inside layout(...)
Bool pendingLayoutParen = false; // saw "layout", awaiting its '('
while (i < length) {
const char c = source[i];
// Comments.
if (c == '/' && i + 1 < length && source[i + 1] == '/') {
while (i < length && source[i] != '\n') ++i;
while (i < length && source[i] != '\n')
++i;
continue;
}
if (c == '/' && i + 1 < length && source[i + 1] == '*') {
i += 2;
while (i + 1 < length && !(source[i] == '*' && source[i + 1] == '/')) ++i;
while (i + 1 < length && !(source[i] == '*' && source[i + 1] == '/'))
++i;
i = (i + 1 < length) ? i + 2 : length;
continue;
}
@@ -2165,7 +2185,8 @@ namespace MobileGL {
}
if (isIdentChar(c) && !(c >= '0' && c <= '9')) {
const SizeT start = i;
while (i < length && isIdentChar(source[i])) ++i;
while (i < length && isIdentChar(source[i]))
++i;
const StringView word(source.data() + start, i - start);
if (word == "layout") {
pendingLayoutParen = true;
@@ -2181,14 +2202,15 @@ namespace MobileGL {
for (const char* keyword : kLayoutOnlyKeywords) {
if (word == keyword) {
return String("ERROR: '") + keyword +
"' is a keyword and may not be used as an identifier.";
"' is a keyword and may not be used as an identifier.";
}
}
}
continue;
}
if (isIdentChar(c)) { // digit-led token: skip the whole number/identifier tail
while (i < length && isIdentChar(source[i])) ++i;
while (i < length && isIdentChar(source[i]))
++i;
pendingLayoutParen = false;
continue;
}
@@ -2201,9 +2223,9 @@ namespace MobileGL {
namespace {
bool IsNonLayoutQualifierKeyword(const String& text) {
static const char* kQualifiers[] = {
"highp", "mediump", "lowp", "precise", "const", "flat",
"noperspective", "smooth", "centroid", "sample", "patch", "invariant",
"coherent", "volatile", "restrict", "readonly", "writeonly", "subroutine",
"highp", "mediump", "lowp", "precise", "const", "flat",
"noperspective", "smooth", "centroid", "sample", "patch", "invariant",
"coherent", "volatile", "restrict", "readonly", "writeonly", "subroutine",
};
for (const char* qualifier : kQualifiers) {
if (text == qualifier) return true;
@@ -2239,8 +2261,7 @@ namespace MobileGL {
// form, which declares no block at all); the element count for an instance array,
// whose elements take base, base+1, ... (GLSL 4.30 4.4.5). -1 means "the grammar
// here is outside this scanner's narrow subset", i.e. do not judge this one.
long long StorageBlockBindingPointCount(const Vector<CodeToken>& tokens, SizeT bufferPos,
SizeT count) {
long long StorageBlockBindingPointCount(const Vector<CodeToken>& tokens, SizeT bufferPos, SizeT count) {
SizeT k = bufferPos + 1;
if (k < count && IsIdentifierToken(tokens[k])) ++k; // block type name
if (k >= count || tokens[k].text != "{") return 1;
@@ -56,7 +56,12 @@ namespace MobileGL {
// requires the Image Format's class to agree with the OpTypeImage's Sampled
// Type, so a bake that disagrees would produce an invalid module rather than a
// merely wrong one.
enum class ComponentClass { Float, SignedInt, UnsignedInt, None };
enum class ComponentClass {
Float,
SignedInt,
UnsignedInt,
None
};
ComponentClass ClassOfImageFormat(spv::ImageFormat format) {
switch (format) {
@@ -114,7 +119,7 @@ namespace MobileGL {
if (sampledType->opcode() == spv::Op::OpTypeInt) {
// OpTypeInt in-operands: 0 width, 1 signedness.
return sampledType->GetSingleWordInOperand(1) != 0 ? ComponentClass::SignedInt
: ComponentClass::UnsignedInt;
: ComponentClass::UnsignedInt;
}
return ComponentClass::None;
}
@@ -136,7 +141,8 @@ namespace MobileGL {
return chain;
}
chain.pointerType = pointerType;
Instruction* pointee = defUseMgr->GetDef(pointerType->GetSingleWordInOperand(kPointerPointeeOperand));
Instruction* pointee =
defUseMgr->GetDef(pointerType->GetSingleWordInOperand(kPointerPointeeOperand));
if (pointee != nullptr && pointee->opcode() == spv::Op::OpTypeArray) {
chain.arrayType = pointee;
pointee = defUseMgr->GetDef(pointee->GetSingleWordInOperand(kArrayElementOperand));
@@ -189,45 +195,84 @@ namespace MobileGL {
// The GL 4.2 image format table (core spec table 8.26), in its order. Written as
// literals rather than through the GL headers because this lives in MG_Util,
// which the GL frontend's enums do not reach.
case 0x8814: /*GL_RGBA32F*/ return static_cast<Uint32>(spv::ImageFormat::Rgba32f);
case 0x881A: /*GL_RGBA16F*/ return static_cast<Uint32>(spv::ImageFormat::Rgba16f);
case 0x8230: /*GL_RG32F*/ return static_cast<Uint32>(spv::ImageFormat::Rg32f);
case 0x822F: /*GL_RG16F*/ return static_cast<Uint32>(spv::ImageFormat::Rg16f);
case 0x8C3A: /*GL_R11F_G11F_B10F*/ return static_cast<Uint32>(spv::ImageFormat::R11fG11fB10f);
case 0x822E: /*GL_R32F*/ return static_cast<Uint32>(spv::ImageFormat::R32f);
case 0x822D: /*GL_R16F*/ return static_cast<Uint32>(spv::ImageFormat::R16f);
case 0x8D70: /*GL_RGBA32UI*/ return static_cast<Uint32>(spv::ImageFormat::Rgba32ui);
case 0x8D76: /*GL_RGBA16UI*/ return static_cast<Uint32>(spv::ImageFormat::Rgba16ui);
case 0x8D7C: /*GL_RGBA8UI*/ return static_cast<Uint32>(spv::ImageFormat::Rgba8ui);
case 0x906F: /*GL_RGB10_A2UI*/ return static_cast<Uint32>(spv::ImageFormat::Rgb10a2ui);
case 0x823C: /*GL_RG32UI*/ return static_cast<Uint32>(spv::ImageFormat::Rg32ui);
case 0x823A: /*GL_RG16UI*/ return static_cast<Uint32>(spv::ImageFormat::Rg16ui);
case 0x8238: /*GL_RG8UI*/ return static_cast<Uint32>(spv::ImageFormat::Rg8ui);
case 0x8236: /*GL_R32UI*/ return static_cast<Uint32>(spv::ImageFormat::R32ui);
case 0x8234: /*GL_R16UI*/ return static_cast<Uint32>(spv::ImageFormat::R16ui);
case 0x8232: /*GL_R8UI*/ return static_cast<Uint32>(spv::ImageFormat::R8ui);
case 0x8D82: /*GL_RGBA32I*/ return static_cast<Uint32>(spv::ImageFormat::Rgba32i);
case 0x8D88: /*GL_RGBA16I*/ return static_cast<Uint32>(spv::ImageFormat::Rgba16i);
case 0x8D8E: /*GL_RGBA8I*/ return static_cast<Uint32>(spv::ImageFormat::Rgba8i);
case 0x823B: /*GL_RG32I*/ return static_cast<Uint32>(spv::ImageFormat::Rg32i);
case 0x8239: /*GL_RG16I*/ return static_cast<Uint32>(spv::ImageFormat::Rg16i);
case 0x8237: /*GL_RG8I*/ return static_cast<Uint32>(spv::ImageFormat::Rg8i);
case 0x8235: /*GL_R32I*/ return static_cast<Uint32>(spv::ImageFormat::R32i);
case 0x8233: /*GL_R16I*/ return static_cast<Uint32>(spv::ImageFormat::R16i);
case 0x8231: /*GL_R8I*/ return static_cast<Uint32>(spv::ImageFormat::R8i);
case 0x8058: /*GL_RGBA8*/ return static_cast<Uint32>(spv::ImageFormat::Rgba8);
case 0x805B: /*GL_RGBA16*/ return static_cast<Uint32>(spv::ImageFormat::Rgba16);
case 0x8059: /*GL_RGB10_A2*/ return static_cast<Uint32>(spv::ImageFormat::Rgb10A2);
case 0x822B: /*GL_RG8*/ return static_cast<Uint32>(spv::ImageFormat::Rg8);
case 0x822C: /*GL_RG16*/ return static_cast<Uint32>(spv::ImageFormat::Rg16);
case 0x8229: /*GL_R8*/ return static_cast<Uint32>(spv::ImageFormat::R8);
case 0x822A: /*GL_R16*/ return static_cast<Uint32>(spv::ImageFormat::R16);
case 0x8F97: /*GL_RGBA8_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::Rgba8Snorm);
case 0x8F9B: /*GL_RGBA16_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::Rgba16Snorm);
case 0x8F95: /*GL_RG8_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::Rg8Snorm);
case 0x8F99: /*GL_RG16_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::Rg16Snorm);
case 0x8F94: /*GL_R8_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::R8Snorm);
case 0x8F98: /*GL_R16_SNORM*/ return static_cast<Uint32>(spv::ImageFormat::R16Snorm);
case 0x8814: /*GL_RGBA32F*/
return static_cast<Uint32>(spv::ImageFormat::Rgba32f);
case 0x881A: /*GL_RGBA16F*/
return static_cast<Uint32>(spv::ImageFormat::Rgba16f);
case 0x8230: /*GL_RG32F*/
return static_cast<Uint32>(spv::ImageFormat::Rg32f);
case 0x822F: /*GL_RG16F*/
return static_cast<Uint32>(spv::ImageFormat::Rg16f);
case 0x8C3A: /*GL_R11F_G11F_B10F*/
return static_cast<Uint32>(spv::ImageFormat::R11fG11fB10f);
case 0x822E: /*GL_R32F*/
return static_cast<Uint32>(spv::ImageFormat::R32f);
case 0x822D: /*GL_R16F*/
return static_cast<Uint32>(spv::ImageFormat::R16f);
case 0x8D70: /*GL_RGBA32UI*/
return static_cast<Uint32>(spv::ImageFormat::Rgba32ui);
case 0x8D76: /*GL_RGBA16UI*/
return static_cast<Uint32>(spv::ImageFormat::Rgba16ui);
case 0x8D7C: /*GL_RGBA8UI*/
return static_cast<Uint32>(spv::ImageFormat::Rgba8ui);
case 0x906F: /*GL_RGB10_A2UI*/
return static_cast<Uint32>(spv::ImageFormat::Rgb10a2ui);
case 0x823C: /*GL_RG32UI*/
return static_cast<Uint32>(spv::ImageFormat::Rg32ui);
case 0x823A: /*GL_RG16UI*/
return static_cast<Uint32>(spv::ImageFormat::Rg16ui);
case 0x8238: /*GL_RG8UI*/
return static_cast<Uint32>(spv::ImageFormat::Rg8ui);
case 0x8236: /*GL_R32UI*/
return static_cast<Uint32>(spv::ImageFormat::R32ui);
case 0x8234: /*GL_R16UI*/
return static_cast<Uint32>(spv::ImageFormat::R16ui);
case 0x8232: /*GL_R8UI*/
return static_cast<Uint32>(spv::ImageFormat::R8ui);
case 0x8D82: /*GL_RGBA32I*/
return static_cast<Uint32>(spv::ImageFormat::Rgba32i);
case 0x8D88: /*GL_RGBA16I*/
return static_cast<Uint32>(spv::ImageFormat::Rgba16i);
case 0x8D8E: /*GL_RGBA8I*/
return static_cast<Uint32>(spv::ImageFormat::Rgba8i);
case 0x823B: /*GL_RG32I*/
return static_cast<Uint32>(spv::ImageFormat::Rg32i);
case 0x8239: /*GL_RG16I*/
return static_cast<Uint32>(spv::ImageFormat::Rg16i);
case 0x8237: /*GL_RG8I*/
return static_cast<Uint32>(spv::ImageFormat::Rg8i);
case 0x8235: /*GL_R32I*/
return static_cast<Uint32>(spv::ImageFormat::R32i);
case 0x8233: /*GL_R16I*/
return static_cast<Uint32>(spv::ImageFormat::R16i);
case 0x8231: /*GL_R8I*/
return static_cast<Uint32>(spv::ImageFormat::R8i);
case 0x8058: /*GL_RGBA8*/
return static_cast<Uint32>(spv::ImageFormat::Rgba8);
case 0x805B: /*GL_RGBA16*/
return static_cast<Uint32>(spv::ImageFormat::Rgba16);
case 0x8059: /*GL_RGB10_A2*/
return static_cast<Uint32>(spv::ImageFormat::Rgb10A2);
case 0x822B: /*GL_RG8*/
return static_cast<Uint32>(spv::ImageFormat::Rg8);
case 0x822C: /*GL_RG16*/
return static_cast<Uint32>(spv::ImageFormat::Rg16);
case 0x8229: /*GL_R8*/
return static_cast<Uint32>(spv::ImageFormat::R8);
case 0x822A: /*GL_R16*/
return static_cast<Uint32>(spv::ImageFormat::R16);
case 0x8F97: /*GL_RGBA8_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::Rgba8Snorm);
case 0x8F9B: /*GL_RGBA16_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::Rgba16Snorm);
case 0x8F95: /*GL_RG8_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::Rg8Snorm);
case 0x8F99: /*GL_RG16_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::Rg16Snorm);
case 0x8F94: /*GL_R8_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::R8Snorm);
case 0x8F98: /*GL_R16_SNORM*/
return static_cast<Uint32>(spv::ImageFormat::R16Snorm);
default:
return static_cast<Uint32>(spv::ImageFormat::Unknown);
}
@@ -295,45 +340,84 @@ namespace MobileGL {
String BakeImageFormatsPass::EsslSpellingOfGLInternalFormat(Uint glInternalFormat) {
switch (static_cast<spv::ImageFormat>(SpirvImageFormatFromGLInternalFormat(glInternalFormat))) {
case spv::ImageFormat::Rgba32f: return "rgba32f";
case spv::ImageFormat::Rgba16f: return "rgba16f";
case spv::ImageFormat::R32f: return "r32f";
case spv::ImageFormat::Rgba8: return "rgba8";
case spv::ImageFormat::Rgba8Snorm: return "rgba8_snorm";
case spv::ImageFormat::Rg32f: return "rg32f";
case spv::ImageFormat::Rg16f: return "rg16f";
case spv::ImageFormat::R11fG11fB10f: return "r11f_g11f_b10f";
case spv::ImageFormat::R16f: return "r16f";
case spv::ImageFormat::Rgba16: return "rgba16";
case spv::ImageFormat::Rgb10A2: return "rgb10_a2";
case spv::ImageFormat::Rg16: return "rg16";
case spv::ImageFormat::Rg8: return "rg8";
case spv::ImageFormat::R16: return "r16";
case spv::ImageFormat::R8: return "r8";
case spv::ImageFormat::Rgba16Snorm: return "rgba16_snorm";
case spv::ImageFormat::Rg16Snorm: return "rg16_snorm";
case spv::ImageFormat::Rg8Snorm: return "rg8_snorm";
case spv::ImageFormat::R16Snorm: return "r16_snorm";
case spv::ImageFormat::R8Snorm: return "r8_snorm";
case spv::ImageFormat::Rgba32i: return "rgba32i";
case spv::ImageFormat::Rgba16i: return "rgba16i";
case spv::ImageFormat::Rgba8i: return "rgba8i";
case spv::ImageFormat::R32i: return "r32i";
case spv::ImageFormat::Rg32i: return "rg32i";
case spv::ImageFormat::Rg16i: return "rg16i";
case spv::ImageFormat::Rg8i: return "rg8i";
case spv::ImageFormat::R16i: return "r16i";
case spv::ImageFormat::R8i: return "r8i";
case spv::ImageFormat::Rgba32ui: return "rgba32ui";
case spv::ImageFormat::Rgba16ui: return "rgba16ui";
case spv::ImageFormat::Rgba8ui: return "rgba8ui";
case spv::ImageFormat::R32ui: return "r32ui";
case spv::ImageFormat::Rgb10a2ui: return "rgb10_a2ui";
case spv::ImageFormat::Rg32ui: return "rg32ui";
case spv::ImageFormat::Rg16ui: return "rg16ui";
case spv::ImageFormat::Rg8ui: return "rg8ui";
case spv::ImageFormat::R16ui: return "r16ui";
case spv::ImageFormat::R8ui: return "r8ui";
case spv::ImageFormat::Rgba32f:
return "rgba32f";
case spv::ImageFormat::Rgba16f:
return "rgba16f";
case spv::ImageFormat::R32f:
return "r32f";
case spv::ImageFormat::Rgba8:
return "rgba8";
case spv::ImageFormat::Rgba8Snorm:
return "rgba8_snorm";
case spv::ImageFormat::Rg32f:
return "rg32f";
case spv::ImageFormat::Rg16f:
return "rg16f";
case spv::ImageFormat::R11fG11fB10f:
return "r11f_g11f_b10f";
case spv::ImageFormat::R16f:
return "r16f";
case spv::ImageFormat::Rgba16:
return "rgba16";
case spv::ImageFormat::Rgb10A2:
return "rgb10_a2";
case spv::ImageFormat::Rg16:
return "rg16";
case spv::ImageFormat::Rg8:
return "rg8";
case spv::ImageFormat::R16:
return "r16";
case spv::ImageFormat::R8:
return "r8";
case spv::ImageFormat::Rgba16Snorm:
return "rgba16_snorm";
case spv::ImageFormat::Rg16Snorm:
return "rg16_snorm";
case spv::ImageFormat::Rg8Snorm:
return "rg8_snorm";
case spv::ImageFormat::R16Snorm:
return "r16_snorm";
case spv::ImageFormat::R8Snorm:
return "r8_snorm";
case spv::ImageFormat::Rgba32i:
return "rgba32i";
case spv::ImageFormat::Rgba16i:
return "rgba16i";
case spv::ImageFormat::Rgba8i:
return "rgba8i";
case spv::ImageFormat::R32i:
return "r32i";
case spv::ImageFormat::Rg32i:
return "rg32i";
case spv::ImageFormat::Rg16i:
return "rg16i";
case spv::ImageFormat::Rg8i:
return "rg8i";
case spv::ImageFormat::R16i:
return "r16i";
case spv::ImageFormat::R8i:
return "r8i";
case spv::ImageFormat::Rgba32ui:
return "rgba32ui";
case spv::ImageFormat::Rgba16ui:
return "rgba16ui";
case spv::ImageFormat::Rgba8ui:
return "rgba8ui";
case spv::ImageFormat::R32ui:
return "r32ui";
case spv::ImageFormat::Rgb10a2ui:
return "rgb10_a2ui";
case spv::ImageFormat::Rg32ui:
return "rg32ui";
case spv::ImageFormat::Rg16ui:
return "rg16ui";
case spv::ImageFormat::Rg8ui:
return "rg8ui";
case spv::ImageFormat::R16ui:
return "r16ui";
case spv::ImageFormat::R8ui:
return "r8ui";
default:
return {};
}
@@ -599,8 +683,7 @@ namespace MobileGL {
// Only an ID operand names a definition the clone has to sit behind. The
// format operand is a LITERAL, and looking it up would resolve some unrelated
// instruction that happens to carry that number as its result id.
Instruction* anchor =
valueIsId ? laterInGlobals(original, defUseMgr->GetDef(value)) : original;
Instruction* anchor = valueIsId ? laterInGlobals(original, defUseMgr->GetDef(value)) : original;
if (anchor == nullptr) return 0;
Instruction* inserted = clone.release();
inserted->InsertAfter(anchor);
@@ -618,16 +701,16 @@ namespace MobileGL {
bool changed = false;
for (Candidate& candidate : candidates) {
const uint32_t newImageId = cloneTypeWithOperand(candidate.chain.imageType, kImageFormatOperand,
static_cast<uint32_t>(candidate.format),
/*valueIsId=*/false);
static_cast<uint32_t>(candidate.format),
/*valueIsId=*/false);
if (newImageId == 0) return Status::Failure;
// The pointer-to-image type every access chain and every single-image
// variable resolves through.
uint32_t newPointeeId = newImageId;
if (candidate.chain.arrayType != nullptr) {
newPointeeId = cloneTypeWithOperand(candidate.chain.arrayType, kArrayElementOperand,
newImageId, /*valueIsId=*/true);
newPointeeId = cloneTypeWithOperand(candidate.chain.arrayType, kArrayElementOperand, newImageId,
/*valueIsId=*/true);
if (newPointeeId == 0) return Status::Failure;
}
const uint32_t newVariablePointerId = cloneTypeWithOperand(
@@ -711,8 +794,7 @@ namespace MobileGL {
spvtools::Optimizer::PassToken BakeImageFormatsPass::CreateBakeImageFormatsPass(
GLFormatByName glFormatByName) {
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<BakeImageFormatsPass>(Move(glFormatByName)));
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<BakeImageFormatsPass>(Move(glFormatByName)));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -73,8 +73,7 @@ namespace MobileGL {
// spelling, and names the module does not declare, are ignored.
using GLFormatByName = UnorderedMap<String, Uint>;
explicit BakeImageFormatsPass(GLFormatByName glFormatByName)
: m_glFormatByName(Move(glFormatByName)) {}
explicit BakeImageFormatsPass(GLFormatByName glFormatByName) : m_glFormatByName(Move(glFormatByName)) {}
const char* name() const override { return "mobilegl-bake-image-formats"; }
Status Process() override;
@@ -53,7 +53,11 @@ namespace MobileGL {
constexpr uint32_t kImageOperandsMaskOperand = 2;
// The categories GL keeps a separate GL_MAX_*_SAMPLES ceiling for.
enum class SampleCategory { Color, Depth, Integer };
enum class SampleCategory {
Color,
Depth,
Integer
};
// The two operations that can carry a Sample image-operand and take their
// coordinate in in-operand 1. OpImageWrite can carry one too, but its operand
@@ -248,8 +252,7 @@ namespace MobileGL {
// No category is squeezed, so no fetch can be out of range. This is the whole
// answer on a driver whose per-format ceilings all reach what MobileGL
// advertises, and it costs nothing.
if (m_maxColorSamples >= m_advertisedMaxSamples &&
m_maxIntegerSamples >= m_advertisedMaxSamples &&
if (m_maxColorSamples >= m_advertisedMaxSamples && m_maxIntegerSamples >= m_advertisedMaxSamples &&
m_maxDepthSamples >= m_advertisedMaxSamples) {
return Status::SuccessWithoutChange;
}
@@ -346,8 +349,7 @@ namespace MobileGL {
// One sample exists, and its index is 0.
const analysis::Constant* zero = constantMgr->GetConstant(sampleType, {0u});
const Instruction* zeroInst =
zero != nullptr ? constantMgr->GetDefiningInstruction(zero, sampleTypeId)
: nullptr;
zero != nullptr ? constantMgr->GetDefiningInstruction(zero, sampleTypeId) : nullptr;
if (zeroInst == nullptr) {
return Status::Failure;
}
@@ -373,26 +375,24 @@ namespace MobileGL {
return Status::Failure;
}
const uint32_t resultId = irContext->TakeNextId();
const analysis::Constant* bound = constantMgr->GetConstant(
sampleType, {static_cast<uint32_t>(categoryMaxSamples - 1)});
const analysis::Constant* bound =
constantMgr->GetConstant(sampleType, {static_cast<uint32_t>(categoryMaxSamples - 1)});
const Instruction* boundInst =
bound != nullptr ? constantMgr->GetDefiningInstruction(bound, sampleTypeId)
: nullptr;
bound != nullptr ? constantMgr->GetDefiningInstruction(bound, sampleTypeId) : nullptr;
if (resultId == 0u || boundInst == nullptr) {
return Status::Failure;
}
InstructionBuilder builder(
irContext, &instruction,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, &instruction,
IRContext::kAnalysisDefUse |
IRContext::kAnalysisInstrToBlockMapping);
Instruction* clamped = builder.AddInstruction(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpExtInst, sampleTypeId, resultId,
Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {glslStd450Id}},
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
{sampleInteger->IsSigned() ? kGlslSMin : kGlslUMin}},
{SPV_OPERAND_TYPE_ID, {sampleOperandId}},
{SPV_OPERAND_TYPE_ID, {boundInst->result_id()}}}));
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {glslStd450Id}},
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
{sampleInteger->IsSigned() ? kGlslSMin : kGlslUMin}},
{SPV_OPERAND_TYPE_ID, {sampleOperandId}},
{SPV_OPERAND_TYPE_ID, {boundInst->result_id()}}}));
if (clamped == nullptr) {
return Status::Failure;
}
@@ -413,9 +413,9 @@ namespace MobileGL {
spvtools::Optimizer::PassToken ClampMultisampleFetchPass::CreateClampMultisampleFetchPass(
const Int32 maxColorSamples, const Int32 maxIntegerSamples, const Int32 maxDepthSamples,
const Int32 advertisedMaxSamples, const bool deferDirectUnsignedFetches) {
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<ClampMultisampleFetchPass>(
maxColorSamples, maxIntegerSamples, maxDepthSamples, advertisedMaxSamples,
deferDirectUnsignedFetches));
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<ClampMultisampleFetchPass>(maxColorSamples, maxIntegerSamples, maxDepthSamples,
advertisedMaxSamples, deferDirectUnsignedFetches));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -72,13 +72,10 @@ namespace MobileGL {
// The three backend-REAL per-category ceilings, plus the count the GL frontend
// advertises (GL_Getter's GetAdvertisedMaxSamples). A category whose real ceiling
// already reaches the advertised one is left completely alone.
ClampMultisampleFetchPass(Int32 maxColorSamples, Int32 maxIntegerSamples,
Int32 maxDepthSamples, Int32 advertisedMaxSamples,
bool deferDirectUnsignedFetches = false)
: m_maxColorSamples(maxColorSamples),
m_maxIntegerSamples(maxIntegerSamples),
m_maxDepthSamples(maxDepthSamples),
m_advertisedMaxSamples(advertisedMaxSamples),
ClampMultisampleFetchPass(Int32 maxColorSamples, Int32 maxIntegerSamples, Int32 maxDepthSamples,
Int32 advertisedMaxSamples, bool deferDirectUnsignedFetches = false)
: m_maxColorSamples(maxColorSamples), m_maxIntegerSamples(maxIntegerSamples),
m_maxDepthSamples(maxDepthSamples), m_advertisedMaxSamples(advertisedMaxSamples),
m_deferDirectUnsignedFetches(deferDirectUnsignedFetches) {}
const char* name() const override { return "clamp-multisample-fetch"; }
@@ -95,8 +92,8 @@ namespace MobileGL {
static bool DeclaresMultisampledImage(spvtools::opt::IRContext* context);
static spvtools::Optimizer::PassToken CreateClampMultisampleFetchPass(
Int32 maxColorSamples, Int32 maxIntegerSamples, Int32 maxDepthSamples,
Int32 advertisedMaxSamples, bool deferDirectUnsignedFetches = false);
Int32 maxColorSamples, Int32 maxIntegerSamples, Int32 maxDepthSamples, Int32 advertisedMaxSamples,
bool deferDirectUnsignedFetches = false);
private:
Int32 m_maxColorSamples;
@@ -25,10 +25,10 @@ namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::Operand;
using spvtools::opt::BasicBlock;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
using spvtools::opt::analysis::Array;
using spvtools::opt::analysis::Type;
@@ -67,8 +67,7 @@ namespace MobileGL {
// Recursively rebuilds a pointee type, replacing the vec3 leaf with a
// scalar array [3]. Returns the new type id.
uint32_t RebuildPointeeType(IRContext* context, uint32_t typeId,
uint32_t scalarArr3TypeId,
uint32_t RebuildPointeeType(IRContext* context, uint32_t typeId, uint32_t scalarArr3TypeId,
const std::unordered_map<uint32_t, uint32_t>& vec3ToArr3) {
auto* defUseMgr = context->get_def_use_mgr();
auto* typeMgr = context->get_type_mgr();
@@ -83,16 +82,13 @@ namespace MobileGL {
}
if (typeInst->opcode() == spv::Op::OpTypeArray) {
const uint32_t oldElemId = typeInst->GetSingleWordInOperand(0);
const uint32_t newElemId =
RebuildPointeeType(context, oldElemId, scalarArr3TypeId, vec3ToArr3);
const uint32_t newElemId = RebuildPointeeType(context, oldElemId, scalarArr3TypeId, vec3ToArr3);
if (newElemId == oldElemId) {
return typeId;
}
const uint32_t lengthId = typeInst->GetSingleWordInOperand(1);
const Array* oldArrTy =
typeMgr->GetType(typeId)->AsArray();
Array newArrTy(typeMgr->GetType(newElemId),
oldArrTy->length_info());
const Array* oldArrTy = typeMgr->GetType(typeId)->AsArray();
Array newArrTy(typeMgr->GetType(newElemId), oldArrTy->length_info());
Type* regNewArrTy = typeMgr->GetRegisteredType(&newArrTy);
return typeMgr->GetTypeInstruction(regNewArrTy);
}
@@ -114,8 +110,8 @@ namespace MobileGL {
// Inserts an instruction before |where|, sets its debug info, and updates
// def-use and block mapping. Returns a pointer to the inserted instruction.
Instruction* InsertBefore(BasicBlock* block, Instruction* where,
std::unique_ptr<Instruction> inst, IRContext* context) {
Instruction* InsertBefore(BasicBlock* block, Instruction* where, std::unique_ptr<Instruction> inst,
IRContext* context) {
auto iter = BasicBlock::iterator(where).InsertBefore(std::move(inst));
if (where != nullptr) {
iter->UpdateDebugInfoFrom(where);
@@ -148,16 +144,14 @@ namespace MobileGL {
const uint32_t acId = context->TakeNextId();
auto acInst = MakeUnique<Instruction>(
context, spv::Op::OpAccessChain, ptrScalarTypeId, acId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constCId}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constCId}}});
InsertBefore(block, where, std::move(acInst), context);
const uint32_t loadId = context->TakeNextId();
auto loadInst = MakeUnique<Instruction>(
context, spv::Op::OpLoad, scalarTypeId, loadId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {acId}}});
auto loadInst =
MakeUnique<Instruction>(context, spv::Op::OpLoad, scalarTypeId, loadId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {acId}}});
for (uint32_t opIdx = 1; opIdx < sourceLoad->NumInOperands(); ++opIdx) {
loadInst->AddOperand(Operand(sourceLoad->GetInOperand(opIdx)));
}
@@ -166,9 +160,8 @@ namespace MobileGL {
}
const uint32_t compositeId = context->TakeNextId();
auto composite = MakeUnique<Instruction>(
context, spv::Op::OpCompositeConstruct, originalTypeId, compositeId,
std::initializer_list<Operand>{});
auto composite = MakeUnique<Instruction>(context, spv::Op::OpCompositeConstruct, originalTypeId,
compositeId, std::initializer_list<Operand>{});
for (uint32_t componentId : componentIds) {
composite->AddOperand({SPV_OPERAND_TYPE_ID, {componentId}});
}
@@ -198,18 +191,16 @@ namespace MobileGL {
const uint32_t elemPtrId = context->TakeNextId();
auto elemAc = MakeUnique<Instruction>(
context, spv::Op::OpAccessChain, ptrDecomposedElemTypeId, elemPtrId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constIId}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constIId}}});
InsertBefore(block, where, std::move(elemAc), context);
elementIds.push_back(BuildCompositeLoad(context, block, where, originalElemTypeId,
elemPtrId, vec3ToArr3, sourceLoad));
}
const uint32_t compositeId = context->TakeNextId();
auto composite = MakeUnique<Instruction>(
context, spv::Op::OpCompositeConstruct, originalTypeId, compositeId,
std::initializer_list<Operand>{});
auto composite = MakeUnique<Instruction>(context, spv::Op::OpCompositeConstruct, originalTypeId,
compositeId, std::initializer_list<Operand>{});
for (uint32_t elementId : elementIds) {
composite->AddOperand({SPV_OPERAND_TYPE_ID, {elementId}});
}
@@ -241,25 +232,22 @@ namespace MobileGL {
const uint32_t extractId = context->TakeNextId();
auto extract = MakeUnique<Instruction>(
context, spv::Op::OpCompositeExtract, scalarTypeId, extractId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {c}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {c}}});
InsertBefore(block, where, std::move(extract), context);
const uint32_t constCId = constMgr->GetUIntConstId(c);
const uint32_t acId = context->TakeNextId();
auto acInst = MakeUnique<Instruction>(
context, spv::Op::OpAccessChain, ptrScalarTypeId, acId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constCId}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constCId}}});
InsertBefore(block, where, std::move(acInst), context);
auto compStore = MakeUnique<Instruction>(
context, spv::Op::OpStore, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {acId}},
{SPV_OPERAND_TYPE_ID, {extractId}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {acId}},
{SPV_OPERAND_TYPE_ID, {extractId}}});
for (uint32_t opIdx = 2; opIdx < sourceStore->NumInOperands(); ++opIdx) {
compStore->AddOperand(Operand(sourceStore->GetInOperand(opIdx)));
}
@@ -287,22 +275,20 @@ namespace MobileGL {
const uint32_t extractId = context->TakeNextId();
auto extract = MakeUnique<Instruction>(
context, spv::Op::OpCompositeExtract, originalElemTypeId, extractId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}}});
InsertBefore(block, where, std::move(extract), context);
const uint32_t constIId = constMgr->GetUIntConstId(i);
const uint32_t elemPtrId = context->TakeNextId();
auto elemAc = MakeUnique<Instruction>(
context, spv::Op::OpAccessChain, ptrDecomposedElemTypeId, elemPtrId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constIId}}});
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {decomposedPtrId}},
{SPV_OPERAND_TYPE_ID, {constIId}}});
InsertBefore(block, where, std::move(elemAc), context);
BuildCompositeStore(context, block, where, originalElemTypeId, elemPtrId,
extractId, vec3ToArr3, sourceStore);
BuildCompositeStore(context, block, where, originalElemTypeId, elemPtrId, extractId,
vec3ToArr3, sourceStore);
}
return;
}
@@ -336,8 +322,7 @@ namespace MobileGL {
if (sawFirstVariable &&
(spvtools::opt::IsTypeInst(inst->opcode()) ||
spvtools::opt::IsConstantInst(inst->opcode()) ||
inst->opcode() == spv::Op::OpUndef)) {
spvtools::opt::IsConstantInst(inst->opcode()) || inst->opcode() == spv::Op::OpUndef)) {
inst->InsertBefore(firstVariable);
}
}
@@ -366,8 +351,7 @@ namespace MobileGL {
std::unordered_map<uint32_t, uint32_t> ptrCompositeToDecomposed;
for (Instruction& typeInst : ctx->types_values()) {
if (typeInst.opcode() == spv::Op::OpTypeVector &&
IsVec3Type(&typeInst) != 0) {
if (typeInst.opcode() == spv::Op::OpTypeVector && IsVec3Type(&typeInst) != 0) {
vec3TypeIds.insert(typeInst.result_id());
}
}
@@ -413,8 +397,7 @@ namespace MobileGL {
if (varInst.opcode() != spv::Op::OpVariable) {
continue;
}
const auto sc = static_cast<spv::StorageClass>(
varInst.GetSingleWordInOperand(0));
const auto sc = static_cast<spv::StorageClass>(varInst.GetSingleWordInOperand(0));
if (sc != spv::StorageClass::Workgroup) {
continue;
}
@@ -436,11 +419,9 @@ namespace MobileGL {
if (pointeeId == vec3LeafId) {
newPointeeId = arr3LeafId;
} else {
newPointeeId = RebuildPointeeType(ctx, pointeeId,
arr3LeafId, vec3ToArr3);
newPointeeId = RebuildPointeeType(ctx, pointeeId, arr3LeafId, vec3ToArr3);
}
const uint32_t newPtrId = typeMgr->FindPointerToType(
newPointeeId, spv::StorageClass::Workgroup);
const uint32_t newPtrId = typeMgr->FindPointerToType(newPointeeId, spv::StorageClass::Workgroup);
varInst.SetResultType(newPtrId);
defUseMgr->AnalyzeInstUse(&varInst);
modified = true;
@@ -454,8 +435,7 @@ namespace MobileGL {
if (typeInst.opcode() != spv::Op::OpTypePointer) {
continue;
}
const auto sc = static_cast<spv::StorageClass>(
typeInst.GetSingleWordInOperand(0));
const auto sc = static_cast<spv::StorageClass>(typeInst.GetSingleWordInOperand(0));
if (sc != spv::StorageClass::Workgroup) {
continue;
}
@@ -468,12 +448,10 @@ namespace MobileGL {
if (it == vec3ToArr3.end()) {
continue;
}
const uint32_t newPointeeId =
pointeeId == vec3LeafId
? it->second
: RebuildPointeeType(ctx, pointeeId, it->second, vec3ToArr3);
const uint32_t newPtrId =
typeMgr->FindPointerToType(newPointeeId, spv::StorageClass::Workgroup);
const uint32_t newPointeeId = pointeeId == vec3LeafId
? it->second
: RebuildPointeeType(ctx, pointeeId, it->second, vec3ToArr3);
const uint32_t newPtrId = typeMgr->FindPointerToType(newPointeeId, spv::StorageClass::Workgroup);
ptrCompositeToDecomposed[typeInst.result_id()] = newPtrId;
}
@@ -604,21 +582,14 @@ namespace MobileGL {
for (auto& bb : func) {
for (auto& inst : bb) {
const spv::Op op = inst.opcode();
bool isAtomic = (op == spv::Op::OpAtomicLoad ||
op == spv::Op::OpAtomicStore ||
op == spv::Op::OpAtomicExchange ||
op == spv::Op::OpAtomicCompareExchange ||
op == spv::Op::OpAtomicIAdd ||
op == spv::Op::OpAtomicISub ||
op == spv::Op::OpAtomicSMin ||
op == spv::Op::OpAtomicUMin ||
op == spv::Op::OpAtomicSMax ||
op == spv::Op::OpAtomicUMax ||
op == spv::Op::OpAtomicAnd ||
op == spv::Op::OpAtomicOr ||
op == spv::Op::OpAtomicXor);
bool isCopyMem = (op == spv::Op::OpCopyMemory ||
op == spv::Op::OpCopyMemorySized);
bool isAtomic =
(op == spv::Op::OpAtomicLoad || op == spv::Op::OpAtomicStore ||
op == spv::Op::OpAtomicExchange || op == spv::Op::OpAtomicCompareExchange ||
op == spv::Op::OpAtomicIAdd || op == spv::Op::OpAtomicISub ||
op == spv::Op::OpAtomicSMin || op == spv::Op::OpAtomicUMin ||
op == spv::Op::OpAtomicSMax || op == spv::Op::OpAtomicUMax ||
op == spv::Op::OpAtomicAnd || op == spv::Op::OpAtomicOr || op == spv::Op::OpAtomicXor);
bool isCopyMem = (op == spv::Op::OpCopyMemory || op == spv::Op::OpCopyMemorySized);
if (!isAtomic && !isCopyMem) {
continue;
}
@@ -628,11 +599,9 @@ namespace MobileGL {
if (ptrDef == nullptr) {
continue;
}
if (decomposedPtrTypeIds.find(ptrDef->type_id()) !=
decomposedPtrTypeIds.end()) {
MOBILEGL_ASSERT(false,
"DecomposeWorkgroupVec3Pass: unsupported atomic/CopyMemory "
"on workgroup vec3 pointer");
if (decomposedPtrTypeIds.find(ptrDef->type_id()) != decomposedPtrTypeIds.end()) {
MOBILEGL_ASSERT(false, "DecomposeWorkgroupVec3Pass: unsupported atomic/CopyMemory "
"on workgroup vec3 pointer");
return Status::Failure;
}
}
@@ -644,8 +613,7 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken
DecomposeWorkgroupVec3Pass::CreateDecomposeWorkgroupVec3Pass() {
spvtools::Optimizer::PassToken DecomposeWorkgroupVec3Pass::CreateDecomposeWorkgroupVec3Pass() {
return spvtools::Optimizer::PassToken(MakeUnique<DecomposeWorkgroupVec3Pass>());
}
} // namespace ShaderTranspiler
@@ -28,9 +28,7 @@ namespace MobileGL {
struct MemberKey {
uint32_t id = 0;
uint32_t member = 0;
bool operator==(const MemberKey& other) const {
return id == other.id && member == other.member;
}
bool operator==(const MemberKey& other) const { return id == other.id && member == other.member; }
};
// OpDecorate <target-id> <decoration> [literals...]
@@ -58,8 +56,8 @@ namespace MobileGL {
if (annotation.NumInOperands() <= kDecorateDecorationOperand) {
continue;
}
const auto decoration = static_cast<spv::Decoration>(
annotation.GetSingleWordInOperand(kDecorateDecorationOperand));
const auto decoration =
static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(kDecorateDecorationOperand));
const uint32_t target = annotation.GetSingleWordInOperand(kDecorateTargetOperand);
if (decoration == spv::Decoration::Invariant) {
invariantIds.push_back(target);
@@ -75,9 +73,8 @@ namespace MobileGL {
}
const auto decoration = static_cast<spv::Decoration>(
annotation.GetSingleWordInOperand(kMemberDecorateDecorationOperand));
const MemberKey key{
annotation.GetSingleWordInOperand(kMemberDecorateStructOperand),
annotation.GetSingleWordInOperand(kMemberDecorateMemberOperand)};
const MemberKey key{annotation.GetSingleWordInOperand(kMemberDecorateStructOperand),
annotation.GetSingleWordInOperand(kMemberDecorateMemberOperand)};
if (decoration == spv::Decoration::Invariant) {
invariantMembers.push_back(key);
} else if (decoration == spv::Decoration::BuiltIn &&
@@ -99,16 +96,14 @@ namespace MobileGL {
irContext, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {target}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::Invariant)}}}));
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::Invariant)}}}));
// Guard against a second Position decoration on the same target.
invariantIds.push_back(target);
changed = true;
}
for (const MemberKey& key : positionMembers) {
if (std::find(invariantMembers.begin(), invariantMembers.end(), key) !=
invariantMembers.end()) {
if (std::find(invariantMembers.begin(), invariantMembers.end(), key) != invariantMembers.end()) {
continue;
}
irContext->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
@@ -116,8 +111,7 @@ namespace MobileGL {
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {key.id}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {key.member}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::Invariant)}}}));
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::Invariant)}}}));
invariantMembers.push_back(key);
changed = true;
}
@@ -130,8 +124,7 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken
DecoratePositionInvariantPass::CreateDecoratePositionInvariantPass() {
spvtools::Optimizer::PassToken DecoratePositionInvariantPass::CreateDecoratePositionInvariantPass() {
return spvtools::Optimizer::PassToken(MakeUnique<DecoratePositionInvariantPass>());
}
} // namespace ShaderTranspiler
@@ -31,9 +31,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
case spv::Op::OpTypeRuntimeArray:
case spv::Op::OpTypePointer:
case spv::Op::OpTypeFunction:
type.ForEachInId([&](const uint32_t* id) {
imageRelated |= imageTypes.count(*id) != 0;
});
type.ForEachInId([&](const uint32_t* id) { imageRelated |= imageTypes.count(*id) != 0; });
break;
default:
break;
@@ -51,8 +49,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
auto& candidates = canonical[key];
Instruction* match = nullptr;
for (auto* candidate : candidates) {
if (context()->get_decoration_mgr()->HaveTheSameDecorations(
type.result_id(), candidate->result_id())) {
if (context()->get_decoration_mgr()->HaveTheSameDecorations(type.result_id(), candidate->result_id())) {
match = candidate;
break;
}
@@ -65,11 +62,12 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
context()->ReplaceAllUsesWith(type.result_id(), match->result_id());
duplicates.push_back(&type);
}
for (auto* type : duplicates) context()->KillInst(type);
for (auto* type : duplicates)
context()->KillInst(type);
return duplicates.empty() ? Status::SuccessWithoutChange : Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken DeduplicateImageTypesPass::Create() {
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<DeduplicateImageTypesPass>());
}
}
} // namespace MobileGL::MG_Util::ShaderTranspiler
@@ -18,4 +18,4 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
Status Process() override;
static spvtools::Optimizer::PassToken Create();
};
}
} // namespace MobileGL::MG_Util::ShaderTranspiler
@@ -42,7 +42,9 @@ namespace MobileGL {
// only guards against a pathological one turning into a huge private array.
constexpr uint32_t kMaxSnapshotScalars = 8192;
Operand Id(uint32_t id) { return {SPV_OPERAND_TYPE_ID, {id}}; }
Operand Id(uint32_t id) {
return {SPV_OPERAND_TYPE_ID, {id}};
}
std::unique_ptr<Instruction> Make(IRContext* context, spv::Op op, uint32_t typeId, uint32_t resultId,
OperandList operands) {
@@ -190,9 +192,9 @@ namespace MobileGL {
return;
}
}
context->AddDebug2Inst(Make(context, spv::Op::OpName, 0, 0,
{Id(id), {SPV_OPERAND_TYPE_LITERAL_STRING,
spvtools::utils::MakeVector(name)}}));
context->AddDebug2Inst(
Make(context, spv::Op::OpName, 0, 0,
{Id(id), {SPV_OPERAND_TYPE_LITERAL_STRING, spvtools::utils::MakeVector(name)}}));
}
} // namespace
@@ -206,16 +208,14 @@ namespace MobileGL {
if (entryPoint != nullptr) return Status::SuccessWithoutChange;
entryPoint = &candidate;
}
if (entryPoint == nullptr ||
static_cast<spv::ExecutionModel>(entryPoint->GetSingleWordInOperand(0)) !=
spv::ExecutionModel::Geometry) {
if (entryPoint == nullptr || static_cast<spv::ExecutionModel>(entryPoint->GetSingleWordInOperand(0)) !=
spv::ExecutionModel::Geometry) {
return Status::SuccessWithoutChange;
}
const uint32_t bodyFunctionId = entryPoint->GetSingleWordInOperand(1);
uint32_t maxVertices = 0;
for (const Instruction& mode : irContext->module()->execution_modes()) {
if (mode.opcode() == spv::Op::OpExecutionMode &&
mode.GetSingleWordInOperand(0) == bodyFunctionId &&
if (mode.opcode() == spv::Op::OpExecutionMode && mode.GetSingleWordInOperand(0) == bodyFunctionId &&
static_cast<spv::ExecutionMode>(mode.GetSingleWordInOperand(1)) ==
spv::ExecutionMode::OutputVertices) {
maxVertices = mode.GetSingleWordInOperand(2);
@@ -230,10 +230,16 @@ namespace MobileGL {
Bool streams = false;
function.ForEachInst([&](Instruction* inst) {
switch (inst->opcode()) {
case spv::Op::OpEmitVertex: emits.push_back(inst); break;
case spv::Op::OpEndPrimitive: cuts.push_back(inst); break;
case spv::Op::OpEmitVertex:
emits.push_back(inst);
break;
case spv::Op::OpEndPrimitive:
cuts.push_back(inst);
break;
case spv::Op::OpEmitStreamVertex:
case spv::Op::OpEndStreamPrimitive: streams = true; break;
case spv::Op::OpEndStreamPrimitive:
streams = true;
break;
default:
if (!writesMemory && IsMemoryWrite(irContext, *inst)) writesMemory = true;
break;
@@ -270,7 +276,8 @@ namespace MobileGL {
std::vector<Bool> written(valueType->NumInOperands(), false);
Bool dynamicMember = false;
defUse->ForEachUser(&global, [&](Instruction* user) {
if (user->opcode() == spv::Op::OpStore && user->GetSingleWordInOperand(0) == global.result_id()) {
if (user->opcode() == spv::Op::OpStore &&
user->GetSingleWordInOperand(0) == global.result_id()) {
std::fill(written.begin(), written.end(), true);
return;
}
@@ -278,7 +285,8 @@ namespace MobileGL {
std::fill(written.begin(), written.end(), true);
return;
}
if (user->opcode() != spv::Op::OpAccessChain && user->opcode() != spv::Op::OpInBoundsAccessChain)
if (user->opcode() != spv::Op::OpAccessChain &&
user->opcode() != spv::Op::OpInBoundsAccessChain)
return;
if (user->NumInOperands() < 2) return;
const auto member = ConstantValue(irContext, user->GetSingleWordInOperand(1));
@@ -311,17 +319,21 @@ namespace MobileGL {
const uint32_t lengthId = constMgr->GetUIntConstId(maxVertices);
const uint32_t lastId = constMgr->GetUIntConstId(maxVertices - 1);
const uint32_t cutLengthId = constMgr->GetUIntConstId(maxVertices + 1);
const uint32_t privateUintPointerId = typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Private);
const uint32_t functionUintPointerId = typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Function);
const uint32_t privateUintPointerId =
typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Private);
const uint32_t functionUintPointerId =
typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Function);
for (Slot& slot : slots) {
slot.snapshotElementPointerTypeId =
typeMgr->FindPointerToType(slot.valueTypeId, spv::StorageClass::Private);
if (slot.member >= 0) {
slot.memberIndexId = constMgr->GetUIntConstId(uint32_t(slot.member));
slot.outputPointerTypeId = typeMgr->FindPointerToType(slot.valueTypeId, spv::StorageClass::Output);
slot.outputPointerTypeId =
typeMgr->FindPointerToType(slot.valueTypeId, spv::StorageClass::Output);
}
}
const uint32_t bodyFunctionTypeId = irContext->GetFunction(bodyFunctionId)->DefInst().GetSingleWordInOperand(1);
const uint32_t bodyFunctionTypeId =
irContext->GetFunction(bodyFunctionId)->DefInst().GetSingleWordInOperand(1);
// Hand-built from here on; analyses are rebuilt once at the end.
auto newPrivateArray = [&](uint32_t elementTypeId, uint32_t lengthConstantId) {
@@ -329,22 +341,23 @@ namespace MobileGL {
irContext->AddType(Make(irContext, spv::Op::OpTypeArray, 0, arrayTypeId,
{Id(elementTypeId), Id(lengthConstantId)}));
const uint32_t pointerTypeId = irContext->TakeNextId();
irContext->AddType(Make(irContext, spv::Op::OpTypePointer, 0, pointerTypeId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Private)}},
Id(arrayTypeId)}));
irContext->AddType(Make(
irContext, spv::Op::OpTypePointer, 0, pointerTypeId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Private)}}, Id(arrayTypeId)}));
const uint32_t variableId = irContext->TakeNextId();
irContext->AddGlobalValue(Make(irContext, spv::Op::OpVariable, pointerTypeId, variableId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS,
{uint32_t(spv::StorageClass::Private)}}}));
irContext->AddGlobalValue(
Make(irContext, spv::Op::OpVariable, pointerTypeId, variableId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Private)}}}));
return variableId;
};
for (Slot& slot : slots) slot.snapshotVariableId = newPrivateArray(slot.valueTypeId, lengthId);
for (Slot& slot : slots)
slot.snapshotVariableId = newPrivateArray(slot.valueTypeId, lengthId);
// One slot past max_vertices: where an EndPrimitive with no vertex before it lands.
const uint32_t cutVariableId = newPrivateArray(uintTypeId, cutLengthId);
const uint32_t countVariableId = irContext->TakeNextId();
irContext->AddGlobalValue(Make(irContext, spv::Op::OpVariable, privateUintPointerId, countVariableId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS,
{uint32_t(spv::StorageClass::Private)}}}));
irContext->AddGlobalValue(
Make(irContext, spv::Op::OpVariable, privateUintPointerId, countVariableId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Private)}}}));
ReplaceName(irContext, countVariableId, "mg_deferredVertexCount");
ReplaceName(irContext, cutVariableId, "mg_deferredStripEnd");
@@ -366,7 +379,8 @@ namespace MobileGL {
for (Instruction* emit : emits) {
const uint32_t count = before(emit, spv::Op::OpLoad, uintTypeId, {Id(countVariableId)});
const uint32_t inRange = before(emit, spv::Op::OpULessThan, boolTypeId, {Id(count), Id(lengthId)});
const uint32_t index = before(emit, spv::Op::OpSelect, uintTypeId, {Id(inRange), Id(count), Id(lastId)});
const uint32_t index =
before(emit, spv::Op::OpSelect, uintTypeId, {Id(inRange), Id(count), Id(lastId)});
for (const Slot& slot : slots) {
const uint32_t source = outputPointer(emit, slot);
const uint32_t value = before(emit, spv::Op::OpLoad, slot.valueTypeId, {Id(source)});
@@ -374,8 +388,8 @@ namespace MobileGL {
{Id(slot.snapshotVariableId), Id(index)});
before(emit, spv::Op::OpStore, 0, {Id(target), Id(value)});
}
const uint32_t cut = before(emit, spv::Op::OpAccessChain, privateUintPointerId,
{Id(cutVariableId), Id(index)});
const uint32_t cut =
before(emit, spv::Op::OpAccessChain, privateUintPointerId, {Id(cutVariableId), Id(index)});
before(emit, spv::Op::OpStore, 0, {Id(cut), Id(zeroId)});
const uint32_t next = before(emit, spv::Op::OpIAdd, uintTypeId, {Id(count), Id(oneId)});
before(emit, spv::Op::OpStore, 0, {Id(countVariableId), Id(next)});
@@ -384,16 +398,20 @@ namespace MobileGL {
const uint32_t count = before(end, spv::Op::OpLoad, uintTypeId, {Id(countVariableId)});
const uint32_t any = before(end, spv::Op::OpINotEqual, boolTypeId, {Id(count), Id(zeroId)});
const uint32_t previous = before(end, spv::Op::OpISub, uintTypeId, {Id(count), Id(oneId)});
const uint32_t inRange = before(end, spv::Op::OpULessThan, boolTypeId, {Id(previous), Id(lengthId)});
const uint32_t inRange =
before(end, spv::Op::OpULessThan, boolTypeId, {Id(previous), Id(lengthId)});
const uint32_t clamped =
before(end, spv::Op::OpSelect, uintTypeId, {Id(inRange), Id(previous), Id(lastId)});
const uint32_t index = before(end, spv::Op::OpSelect, uintTypeId, {Id(any), Id(clamped), Id(lengthId)});
const uint32_t cut = before(end, spv::Op::OpAccessChain, privateUintPointerId,
{Id(cutVariableId), Id(index)});
const uint32_t index =
before(end, spv::Op::OpSelect, uintTypeId, {Id(any), Id(clamped), Id(lengthId)});
const uint32_t cut =
before(end, spv::Op::OpAccessChain, privateUintPointerId, {Id(cutVariableId), Id(index)});
before(end, spv::Op::OpStore, 0, {Id(cut), Id(oneId)});
}
for (Instruction* inst : emits) irContext->KillInst(inst);
for (Instruction* inst : cuts) irContext->KillInst(inst);
for (Instruction* inst : emits)
irContext->KillInst(inst);
for (Instruction* inst : cuts)
irContext->KillInst(inst);
// ---- The new entry point: run the body, then replay -----------------------------
const uint32_t entryId = irContext->TakeNextId();
@@ -421,13 +439,15 @@ namespace MobileGL {
};
auto start = newBlock(startLabel);
const uint32_t iterator = add(*start, spv::Op::OpVariable, functionUintPointerId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Function)}}});
const uint32_t iterator =
add(*start, spv::Op::OpVariable, functionUintPointerId,
{{SPV_OPERAND_TYPE_STORAGE_CLASS, {uint32_t(spv::StorageClass::Function)}}});
add(*start, spv::Op::OpStore, 0, {Id(countVariableId), Id(zeroId)});
add(*start, spv::Op::OpFunctionCall, voidTypeId, {Id(bodyFunctionId)});
const uint32_t recorded = add(*start, spv::Op::OpLoad, uintTypeId, {Id(countVariableId)});
const uint32_t fits = add(*start, spv::Op::OpULessThan, boolTypeId, {Id(recorded), Id(lengthId)});
const uint32_t limit = add(*start, spv::Op::OpSelect, uintTypeId, {Id(fits), Id(recorded), Id(lengthId)});
const uint32_t limit =
add(*start, spv::Op::OpSelect, uintTypeId, {Id(fits), Id(recorded), Id(lengthId)});
add(*start, spv::Op::OpStore, 0, {Id(iterator), Id(zeroId)});
add(*start, spv::Op::OpBranch, 0, {Id(headerLabel)});
@@ -29,8 +29,8 @@ namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
constexpr Uint32 kFloat64Width = 64;
@@ -50,12 +50,14 @@ namespace MobileGL {
// Offset decoration sets with it.
Bool IsDuplicableType(spv::Op opcode) {
switch (opcode) {
case spv::Op::OpTypeFloat:
case spv::Op::OpTypeVector:
case spv::Op::OpTypeMatrix:
case spv::Op::OpTypePointer:
case spv::Op::OpTypeFunction: return true;
default: return false;
case spv::Op::OpTypeFloat:
case spv::Op::OpTypeVector:
case spv::Op::OpTypeMatrix:
case spv::Op::OpTypePointer:
case spv::Op::OpTypeFunction:
return true;
default:
return false;
}
}
@@ -89,8 +91,7 @@ namespace MobileGL {
// demoted shader reads.
class BlockRelayout {
public:
BlockRelayout(IRContext* irContext, Bool std140)
: m_irContext(irContext), m_std140(std140) {}
BlockRelayout(IRContext* irContext, Bool std140) : m_irContext(irContext), m_std140(std140) {}
// Size and alignment of `typeId`, QUEUING every offset/stride decoration it
// implies on the way down. Zero size means "not a type this layout knows how
@@ -143,42 +144,44 @@ namespace MobileGL {
if (type == nullptr) return {};
switch (type->opcode()) {
case spv::Op::OpTypeInt:
case spv::Op::OpTypeFloat: {
const Uint32 bytes = type->GetSingleWordInOperand(0) / 8;
return {bytes, bytes};
case spv::Op::OpTypeInt:
case spv::Op::OpTypeFloat: {
const Uint32 bytes = type->GetSingleWordInOperand(0) / 8;
return {bytes, bytes};
}
case spv::Op::OpTypeBool:
return {4, 4};
case spv::Op::OpTypeVector: {
const Extent component = Measure(type->GetSingleWordInOperand(0));
if (component.size == 0) return {};
const Uint32 count = type->GetSingleWordInOperand(1);
// A three-component vector aligns like a four-component one.
return {component.size * count, component.alignment * (count == 3 ? 4 : count)};
}
case spv::Op::OpTypeMatrix: {
const Extent column = Measure(type->GetSingleWordInOperand(0));
if (column.size == 0) return {};
const Uint32 stride = MatrixOrArrayStride(column.alignment);
return {stride * type->GetSingleWordInOperand(1), stride};
}
case spv::Op::OpTypeArray:
case spv::Op::OpTypeRuntimeArray: {
const Extent element = Measure(type->GetSingleWordInOperand(0));
if (element.size == 0) return {};
const Uint32 alignment = MatrixOrArrayStride(element.alignment);
const Uint32 stride = RoundUp(element.size, alignment);
SetTypeDecoration(typeId, spv::Decoration::ArrayStride, stride);
if (type->opcode() == spv::Op::OpTypeRuntimeArray) {
// An unsized array contributes its stride and nothing more; the
// block's size is whatever the application bound.
return {stride, alignment};
}
case spv::Op::OpTypeBool: return {4, 4};
case spv::Op::OpTypeVector: {
const Extent component = Measure(type->GetSingleWordInOperand(0));
if (component.size == 0) return {};
const Uint32 count = type->GetSingleWordInOperand(1);
// A three-component vector aligns like a four-component one.
return {component.size * count,
component.alignment * (count == 3 ? 4 : count)};
}
case spv::Op::OpTypeMatrix: {
const Extent column = Measure(type->GetSingleWordInOperand(0));
if (column.size == 0) return {};
const Uint32 stride = MatrixOrArrayStride(column.alignment);
return {stride * type->GetSingleWordInOperand(1), stride};
}
case spv::Op::OpTypeArray:
case spv::Op::OpTypeRuntimeArray: {
const Extent element = Measure(type->GetSingleWordInOperand(0));
if (element.size == 0) return {};
const Uint32 alignment = MatrixOrArrayStride(element.alignment);
const Uint32 stride = RoundUp(element.size, alignment);
SetTypeDecoration(typeId, spv::Decoration::ArrayStride, stride);
if (type->opcode() == spv::Op::OpTypeRuntimeArray) {
// An unsized array contributes its stride and nothing more; the
// block's size is whatever the application bound.
return {stride, alignment};
}
return {stride * ArrayLength(type->GetSingleWordInOperand(1)), alignment};
}
case spv::Op::OpTypeStruct: return MeasureStruct(*type);
default: return {};
return {stride * ArrayLength(type->GetSingleWordInOperand(1)), alignment};
}
case spv::Op::OpTypeStruct:
return MeasureStruct(*type);
default:
return {};
}
}
@@ -199,8 +202,7 @@ namespace MobileGL {
m_irContext->get_def_use_mgr()->GetDef(PeelArrays(memberTypeId));
if (memberType != nullptr && memberType->opcode() == spv::Op::OpTypeMatrix) {
const Extent column = Measure(memberType->GetSingleWordInOperand(0));
SetMemberDecoration(structType.result_id(), member,
spv::Decoration::MatrixStride,
SetMemberDecoration(structType.result_id(), member, spv::Decoration::MatrixStride,
MatrixOrArrayStride(column.alignment));
}
@@ -238,8 +240,7 @@ namespace MobileGL {
m_pendingWrites.push_back({false, targetId, 0, decoration, value});
}
void SetMemberDecoration(Uint32 structId, Uint32 member, spv::Decoration decoration,
Uint32 value) {
void SetMemberDecoration(Uint32 structId, Uint32 member, spv::Decoration decoration, Uint32 value) {
m_pendingWrites.push_back({true, structId, member, decoration, value});
}
@@ -320,24 +321,25 @@ namespace MobileGL {
return wideTypeIds.count(type.GetSingleWordInOperand(operand)) != 0;
};
switch (type.opcode()) {
case spv::Op::OpTypeVector:
case spv::Op::OpTypeMatrix:
case spv::Op::OpTypeArray:
case spv::Op::OpTypeRuntimeArray:
if (contains(0)) wideTypeIds.insert(type.result_id());
break;
case spv::Op::OpTypePointer:
if (contains(1)) wideTypeIds.insert(type.result_id());
break;
case spv::Op::OpTypeStruct:
for (Uint32 member = 0; member < type.NumInOperands(); ++member) {
if (contains(member)) {
wideTypeIds.insert(type.result_id());
break;
}
case spv::Op::OpTypeVector:
case spv::Op::OpTypeMatrix:
case spv::Op::OpTypeArray:
case spv::Op::OpTypeRuntimeArray:
if (contains(0)) wideTypeIds.insert(type.result_id());
break;
case spv::Op::OpTypePointer:
if (contains(1)) wideTypeIds.insert(type.result_id());
break;
case spv::Op::OpTypeStruct:
for (Uint32 member = 0; member < type.NumInOperands(); ++member) {
if (contains(member)) {
wideTypeIds.insert(type.result_id());
break;
}
break;
default: break;
}
break;
default:
break;
}
}
@@ -365,32 +367,31 @@ namespace MobileGL {
[&](Instruction* inst) {
if (declineReason != nullptr) return;
switch (inst->opcode()) {
case spv::Op::OpBitcast: {
// "Total bit width of Result Type and Operand must match" - true
// today, false the moment one of the two sides halves.
const Bool resultIs64 = float64ValueTypeIds.count(inst->type_id()) != 0;
const Bool operandIs64 = valueIsFloat64(inst->GetSingleWordInOperand(0));
if (resultIs64 != operandIs64) {
declineReason = "an OpBitcast across the 64-bit boundary "
"(doubleBitsToUint64 / uint64BitsToDouble / "
"packDouble2x32)";
}
case spv::Op::OpBitcast: {
// "Total bit width of Result Type and Operand must match" - true
// today, false the moment one of the two sides halves.
const Bool resultIs64 = float64ValueTypeIds.count(inst->type_id()) != 0;
const Bool operandIs64 = valueIsFloat64(inst->GetSingleWordInOperand(0));
if (resultIs64 != operandIs64) {
declineReason = "an OpBitcast across the 64-bit boundary "
"(doubleBitsToUint64 / uint64BitsToDouble / "
"packDouble2x32)";
}
break;
}
case spv::Op::OpExtInst: {
if (glslStd450SetId == 0 || inst->GetSingleWordInOperand(0) != glslStd450SetId) {
break;
}
case spv::Op::OpExtInst: {
if (glslStd450SetId == 0 ||
inst->GetSingleWordInOperand(0) != glslStd450SetId) {
break;
}
const Uint32 extOpcode = inst->GetSingleWordInOperand(1);
if (extOpcode == GLSLstd450PackDouble2x32 ||
extOpcode == GLSLstd450UnpackDouble2x32) {
declineReason = "GLSL.std.450 PackDouble2x32 / UnpackDouble2x32, "
"which are defined only for a 64-bit float";
}
break;
const Uint32 extOpcode = inst->GetSingleWordInOperand(1);
if (extOpcode == GLSLstd450PackDouble2x32 || extOpcode == GLSLstd450UnpackDouble2x32) {
declineReason = "GLSL.std.450 PackDouble2x32 / UnpackDouble2x32, "
"which are defined only for a 64-bit float";
}
default: break;
break;
}
default:
break;
}
},
/*run_on_debug_line_insts=*/false);
@@ -539,8 +540,7 @@ namespace MobileGL {
/*run_on_debug_line_insts=*/false);
for (Instruction* conversion : identityConversions) {
irContext->ReplaceAllUsesWith(conversion->result_id(),
conversion->GetSingleWordInOperand(0));
irContext->ReplaceAllUsesWith(conversion->result_id(), conversion->GetSingleWordInOperand(0));
irContext->KillInst(conversion);
}
@@ -597,8 +597,7 @@ namespace MobileGL {
// already correct.
for (Instruction& variable : irContext->types_values()) {
if (variable.opcode() != spv::Op::OpVariable) continue;
const auto storageClass =
static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
const auto storageClass = static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Uniform &&
storageClass != spv::StorageClass::StorageBuffer &&
storageClass != spv::StorageClass::PushConstant) {
@@ -54,8 +54,8 @@ namespace MobileGL {
bool IsFloat32Type(IRContext* ctx, uint32_t typeId) {
Instruction* t = ctx->get_def_use_mgr()->GetDef(typeId);
return t != nullptr && t->opcode() == spv::Op::OpTypeFloat &&
t->NumInOperands() >= 1 && t->GetSingleWordInOperand(0) == 32;
return t != nullptr && t->opcode() == spv::Op::OpTypeFloat && t->NumInOperands() >= 1 &&
t->GetSingleWordInOperand(0) == 32;
}
// The value of a plain 32-bit OpConstant, or false (spec constants and anything
@@ -79,8 +79,7 @@ namespace MobileGL {
analysis::Float f(32);
analysis::Type* floatReg = ctx->get_type_mgr()->GetRegisteredType(&f);
const analysis::Array::LengthInfo lengthInfo{
lengthConstId,
{static_cast<uint32_t>(analysis::Array::LengthInfo::kConstant), lengthValue}};
lengthConstId, {static_cast<uint32_t>(analysis::Array::LengthInfo::kConstant), lengthValue}};
analysis::Array arr(floatReg, lengthInfo);
return ctx->get_type_mgr()->GetTypeInstruction(&arr);
}
@@ -88,10 +87,8 @@ namespace MobileGL {
void AddNameFor(IRContext* ctx, uint32_t id, const String& name) {
std::vector<Operand> operands;
operands.push_back({SPV_OPERAND_TYPE_ID, {id}});
operands.push_back(
{SPV_OPERAND_TYPE_LITERAL_STRING, spvtools::utils::MakeVector(name)});
ctx->AddDebug2Inst(
spvtools::MakeUnique<Instruction>(ctx, spv::Op::OpName, 0, 0, operands));
operands.push_back({SPV_OPERAND_TYPE_LITERAL_STRING, spvtools::utils::MakeVector(name)});
ctx->AddDebug2Inst(spvtools::MakeUnique<Instruction>(ctx, spv::Op::OpName, 0, 0, operands));
}
void AddLocationDecoration(IRContext* ctx, uint32_t id, uint32_t location) {
@@ -99,8 +96,7 @@ namespace MobileGL {
ctx, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {id}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {location}}}));
}
@@ -108,8 +104,7 @@ namespace MobileGL {
// point, and registered with the def-use manager so ReplaceAllUsesWith may name
// it before the end-of-pass invalidation.
uint32_t CreateCarrierVariable(IRContext* ctx, Instruction* entryPoint, uint32_t pointeeTypeId,
spv::StorageClass storage, const String& name,
uint32_t location) {
spv::StorageClass storage, const String& name, uint32_t location) {
const uint32_t ptrTypeId = ctx->get_type_mgr()->FindPointerToType(pointeeTypeId, storage);
if (ptrTypeId == 0) return 0;
const uint32_t varId = ctx->TakeNextId();
@@ -173,21 +168,16 @@ namespace MobileGL {
};
for (Instruction& ann : ctx->annotations()) {
if (ann.opcode() == spv::Op::OpMemberDecorate && ann.NumInOperands() >= 4 &&
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(2)) ==
spv::Decoration::BuiltIn) {
if (static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(3)) ==
spv::BuiltIn::PointSize) {
memberSites.push_back(
{ann.GetSingleWordInOperand(0), ann.GetSingleWordInOperand(1)});
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(2)) == spv::Decoration::BuiltIn) {
if (static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(3)) == spv::BuiltIn::PointSize) {
memberSites.push_back({ann.GetSingleWordInOperand(0), ann.GetSingleWordInOperand(1)});
} else if (isClipOrCull(ann.GetSingleWordInOperand(3))) {
clipCullSites.push_back(
{ann.GetSingleWordInOperand(0), ann.GetSingleWordInOperand(1)});
clipCullSites.push_back({ann.GetSingleWordInOperand(0), ann.GetSingleWordInOperand(1)});
}
} else if (ann.opcode() == spv::Op::OpDecorate && ann.NumInOperands() >= 3 &&
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) ==
spv::Decoration::BuiltIn) {
if (static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(2)) ==
spv::BuiltIn::PointSize) {
if (static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(2)) == spv::BuiltIn::PointSize) {
Instruction* var = defUse->GetDef(ann.GetSingleWordInOperand(0));
if (var != nullptr && var->opcode() == spv::Op::OpVariable) {
standaloneVars.push_back(var);
@@ -287,8 +277,7 @@ namespace MobileGL {
uint32_t memberConstId = 0;
uint32_t ptrOutputFloatId = 0;
if (blockVar != nullptr) {
memberConstId = ctx->get_constant_mgr()->GetSIntConstId(
static_cast<int32_t>(memberIndex));
memberConstId = ctx->get_constant_mgr()->GetSIntConstId(static_cast<int32_t>(memberIndex));
ptrOutputFloatId =
ctx->get_type_mgr()->FindPointerToType(floatTypeId, spv::StorageClass::Output);
if (ptrOutputFloatId == 0) return decline("no Output float pointer type");
@@ -303,8 +292,7 @@ namespace MobileGL {
for (auto funcIt = ctx->module()->begin(); funcIt != ctx->module()->end(); ++funcIt) {
if (funcIt->result_id() != entryFunctionId) continue;
funcIt->ForEachInst([&](Instruction* inst) {
if (inst->opcode() != spv::Op::OpReturn &&
inst->opcode() != spv::Op::OpReturnValue) {
if (inst->opcode() != spv::Op::OpReturn && inst->opcode() != spv::Op::OpReturnValue) {
return;
}
uint32_t valueId = 0;
@@ -312,9 +300,8 @@ namespace MobileGL {
const uint32_t chainId = ctx->TakeNextId();
inst->InsertBefore(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpAccessChain, ptrOutputFloatId, chainId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {blockVar->result_id()}},
{SPV_OPERAND_TYPE_ID, {memberConstId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {blockVar->result_id()}},
{SPV_OPERAND_TYPE_ID, {memberConstId}}}));
valueId = ctx->TakeNextId();
inst->InsertBefore(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpLoad, floatTypeId, valueId,
@@ -408,8 +395,7 @@ namespace MobileGL {
chainUser->opcode() == spv::Op::OpInBoundsAccessChain) &&
chainUser->NumInOperands() >= 2) {
uint32_t member = 0;
if (PlainConstantValue(ctx, chainUser->GetSingleWordInOperand(1),
member) &&
if (PlainConstantValue(ctx, chainUser->GetSingleWordInOperand(1), member) &&
member == blockVar.memberIndex) {
touchesPointSize = true;
}
@@ -437,8 +423,7 @@ namespace MobileGL {
reason = "an access chain that continues past the PointSize member";
return;
}
arrayedRedirects.push_back(
{user, blockVar.storage == spv::StorageClass::Input});
arrayedRedirects.push_back({user, blockVar.storage == spv::StorageClass::Input});
return;
}
case spv::Op::OpLoad:
@@ -519,8 +504,7 @@ namespace MobileGL {
if (needsInputCarrier && m_options.inputCarrierName.empty()) {
return decline("a PointSize read with no input carrier name to bind it to");
}
if ((needsOutputCarrier && !standaloneOutputSwapped) &&
m_options.outputCarrierName.empty()) {
if ((needsOutputCarrier && !standaloneOutputSwapped) && m_options.outputCarrierName.empty()) {
return decline("a PointSize write with no output carrier name to bind it to");
}
@@ -545,8 +529,7 @@ namespace MobileGL {
// built-in for a stage that may not declare it. A control stage asked to
// produce a value it never computes is also a program reading undefined data
// either way, so this declines rather than growing the pass for it.
if (needsOutputCarrier && !standaloneOutputSwapped && !outputCarrierHasWriter &&
isTessControl) {
if (needsOutputCarrier && !standaloneOutputSwapped && !outputCarrierHasWriter && isTessControl) {
return decline("a control stage asked to produce a point-size carrier it never writes");
}
@@ -571,21 +554,18 @@ namespace MobileGL {
if (outputArrayLengthValue == 0) {
return decline("a control stage with neither gl_out nor OutputVertices");
}
outputArrayLengthConstId =
ctx->get_constant_mgr()->GetUIntConstId(outputArrayLengthValue);
outputArrayLengthConstId = ctx->get_constant_mgr()->GetUIntConstId(outputArrayLengthValue);
}
}
const bool anyWork = needsInputCarrier || needsOutputCarrier ||
!standaloneSwaps.empty();
const bool anyWork = needsInputCarrier || needsOutputCarrier || !standaloneSwaps.empty();
// Even with no access left to redirect (a dead read the sanitize chain already
// removed), a declared TessellationPointSize/GeometryPointSize capability must
// still be stripped - it alone makes the module unbuildable on the device.
std::vector<Instruction*> capabilitiesToStrip;
for (Instruction& capability : ctx->module()->capabilities()) {
if (capability.NumInOperands() < 1) continue;
const auto declared =
static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
const auto declared = static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
if (declared == spv::Capability::TessellationPointSize ||
declared == spv::Capability::GeometryPointSize) {
capabilitiesToStrip.push_back(&capability);
@@ -613,8 +593,7 @@ namespace MobileGL {
// variable leaves with the demotion.
if (isTessControl && !blockVars.empty()) {
bool clipOrCullIsLive = false;
const auto memberIsClipOrCull = [&](const uint32_t structId,
const uint32_t member) {
const auto memberIsClipOrCull = [&](const uint32_t structId, const uint32_t member) {
for (const MemberSite& site : clipCullSites) {
if (site.structId == structId && site.memberIndex == member) return true;
}
@@ -623,10 +602,8 @@ namespace MobileGL {
for (Instruction& inst : ctx->module()->types_values()) {
if (clipOrCullIsLive) break;
if (inst.opcode() != spv::Op::OpVariable) continue;
const auto storage =
static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
if (storage != spv::StorageClass::Input &&
storage != spv::StorageClass::Output) {
const auto storage = static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
if (storage != spv::StorageClass::Input && storage != spv::StorageClass::Output) {
continue;
}
// A standalone clip/cull variable counts the moment anything but its
@@ -642,8 +619,7 @@ namespace MobileGL {
pointee = defUse->GetDef(pointee->GetSingleWordInOperand(0));
}
const bool blockCarriesClipCull =
pointee != nullptr && pointee->opcode() == spv::Op::OpTypeStruct &&
[&] {
pointee != nullptr && pointee->opcode() == spv::Op::OpTypeStruct && [&] {
for (const MemberSite& site : clipCullSites) {
if (site.structId == pointee->result_id()) return true;
}
@@ -677,8 +653,7 @@ namespace MobileGL {
}
uint32_t member = 0;
if (user->NumInOperands() <= memberOperand ||
!PlainConstantValue(ctx, user->GetSingleWordInOperand(memberOperand),
member)) {
!PlainConstantValue(ctx, user->GetSingleWordInOperand(memberOperand), member)) {
clipOrCullIsLive = true; // cannot prove it misses clip/cull
return;
}
@@ -688,9 +663,8 @@ namespace MobileGL {
if (clipOrCullIsLive) {
for (const BlockVar& blockVar : blockVars) {
if (blockVar.storage == spv::StorageClass::Output) {
return decline(
"a control stage with live clip/cull distance, whose "
"redeclared output block would still print gl_PointSize");
return decline("a control stage with live clip/cull distance, whose "
"redeclared output block would still print gl_PointSize");
}
}
}
@@ -700,22 +674,19 @@ namespace MobileGL {
const uint32_t floatTypeId = Float32Type(ctx);
uint32_t inputCarrierId = 0;
if (needsInputCarrier) {
const uint32_t arrayTypeId = ArrayOfFloat32Type(
ctx, arrayedInput->arrayLengthConstId, arrayedInput->arrayLengthValue);
inputCarrierId =
CreateCarrierVariable(ctx, entryPoint, arrayTypeId, spv::StorageClass::Input,
m_options.inputCarrierName, m_options.location);
const uint32_t arrayTypeId =
ArrayOfFloat32Type(ctx, arrayedInput->arrayLengthConstId, arrayedInput->arrayLengthValue);
inputCarrierId = CreateCarrierVariable(ctx, entryPoint, arrayTypeId, spv::StorageClass::Input,
m_options.inputCarrierName, m_options.location);
}
uint32_t outputCarrierId = 0;
if (needsOutputCarrier && !standaloneOutputSwapped) {
uint32_t pointeeTypeId = floatTypeId;
if (isTessControl) {
pointeeTypeId =
ArrayOfFloat32Type(ctx, outputArrayLengthConstId, outputArrayLengthValue);
pointeeTypeId = ArrayOfFloat32Type(ctx, outputArrayLengthConstId, outputArrayLengthValue);
}
outputCarrierId =
CreateCarrierVariable(ctx, entryPoint, pointeeTypeId, spv::StorageClass::Output,
m_options.outputCarrierName, m_options.location);
outputCarrierId = CreateCarrierVariable(ctx, entryPoint, pointeeTypeId, spv::StorageClass::Output,
m_options.outputCarrierName, m_options.location);
}
// Seed a forced carrier nothing writes, so the ES hop keeps it (see the
@@ -726,22 +697,20 @@ namespace MobileGL {
const uint32_t defaultPointSizeId = ctx->get_constant_mgr()->GetFloatConstId(1.0f);
const uint32_t entryFunctionId = entryPoint->GetSingleWordInOperand(1);
std::vector<Instruction*> seedSites;
for (auto funcIt = ctx->module()->begin(); funcIt != ctx->module()->end();
++funcIt) {
for (auto funcIt = ctx->module()->begin(); funcIt != ctx->module()->end(); ++funcIt) {
if (funcIt->result_id() != entryFunctionId) continue;
funcIt->ForEachInst([&](Instruction* inst) {
const bool emit = inst->opcode() == spv::Op::OpEmitVertex ||
inst->opcode() == spv::Op::OpEmitStreamVertex;
const bool ret = inst->opcode() == spv::Op::OpReturn ||
inst->opcode() == spv::Op::OpReturnValue;
const bool ret =
inst->opcode() == spv::Op::OpReturn || inst->opcode() == spv::Op::OpReturnValue;
if (isGeometry ? emit : ret) seedSites.push_back(inst);
});
// A geometry stage with no EmitVertex emits nothing at all; seeding the
// ends of the function still keeps the varying alive for the capture.
if (isGeometry && seedSites.empty()) {
funcIt->ForEachInst([&](Instruction* inst) {
if (inst->opcode() == spv::Op::OpReturn ||
inst->opcode() == spv::Op::OpReturnValue) {
if (inst->opcode() == spv::Op::OpReturn || inst->opcode() == spv::Op::OpReturnValue) {
seedSites.push_back(inst);
}
});
@@ -750,9 +719,8 @@ namespace MobileGL {
for (Instruction* site : seedSites) {
site->InsertBefore(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpStore, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {outputCarrierId}},
{SPV_OPERAND_TYPE_ID, {defaultPointSizeId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {outputCarrierId}},
{SPV_OPERAND_TYPE_ID, {defaultPointSizeId}}}));
}
}
@@ -766,8 +734,8 @@ namespace MobileGL {
for (const ArrayedRedirect& redirect : arrayedRedirects) {
const uint32_t carrierId = redirect.input ? inputCarrierId : outputCarrierId;
const Operand vertexIndex = redirect.chain->GetInOperand(1);
redirect.chain->SetInOperands(Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {carrierId}}, vertexIndex});
redirect.chain->SetInOperands(
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {carrierId}}, vertexIndex});
}
// Standalone form: the variable becomes its own carrier.
for (const StandaloneSwap& swap : standaloneSwaps) {
@@ -780,7 +748,8 @@ namespace MobileGL {
oldNames.push_back(&debugInst);
}
}
for (Instruction* oldName : oldNames) ctx->KillInst(oldName);
for (Instruction* oldName : oldNames)
ctx->KillInst(oldName);
AddNameFor(ctx, swap.var->result_id(),
swap.input ? m_options.inputCarrierName : m_options.outputCarrierName);
}
@@ -797,8 +766,7 @@ namespace MobileGL {
spvtools::Optimizer::PassToken DemotePointSizePass::CreateDemotePointSizePass(
DemotePointSizeOptions options, DemotePointSizeReport* report) {
return spvtools::Optimizer::PassToken(
MakeUnique<DemotePointSizePass>(Move(options), report));
return spvtools::Optimizer::PassToken(MakeUnique<DemotePointSizePass>(Move(options), report));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -106,8 +106,8 @@ namespace MobileGL {
const char* name() const override { return "mobilegl-demote-point-size"; }
Status Process() override;
static spvtools::Optimizer::PassToken CreateDemotePointSizePass(
DemotePointSizeOptions options, DemotePointSizeReport* report);
static spvtools::Optimizer::PassToken CreateDemotePointSizePass(DemotePointSizeOptions options,
DemotePointSizeReport* report);
private:
DemotePointSizeOptions m_options;
@@ -61,9 +61,8 @@ namespace MobileGL {
bool IsUnsignedInt32(IRContext* context, uint32_t typeId) {
const Instruction* type = context->get_def_use_mgr()->GetDef(typeId);
return type != nullptr && type->opcode() == spv::Op::OpTypeInt &&
type->NumInOperands() >= 2 && type->GetSingleWordInOperand(0) == 32u &&
type->GetSingleWordInOperand(1) == 0u;
return type != nullptr && type->opcode() == spv::Op::OpTypeInt && type->NumInOperands() >= 2 &&
type->GetSingleWordInOperand(0) == 32u && type->GetSingleWordInOperand(1) == 0u;
}
uint32_t SynthesizeSubgroupSizeVariable(IRContext* context, uint32_t pointerTypeId) {
@@ -71,16 +70,13 @@ namespace MobileGL {
context->AddGlobalValue(spvtools::MakeUnique<Instruction>(
context, spv::Op::OpVariable, pointerTypeId, variableId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Input)}}}));
context->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
context, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {variableId}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{static_cast<uint32_t>(spv::BuiltIn::SubgroupSize)}}}));
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {static_cast<uint32_t>(spv::BuiltIn::SubgroupSize)}}}));
for (Instruction& entryPoint : context->module()->entry_points()) {
entryPoint.AddOperand({SPV_OPERAND_TYPE_ID, {variableId}});
@@ -104,8 +100,7 @@ namespace MobileGL {
defUseMgr->ForEachUser(numSubgroupsVar, [&](Instruction* user) {
switch (user->opcode()) {
case spv::Op::OpLoad:
if (user->NumInOperands() >= 1 &&
user->GetSingleWordInOperand(0) == numSubgroupsVarId) {
if (user->NumInOperands() >= 1 && user->GetSingleWordInOperand(0) == numSubgroupsVarId) {
numSubgroupsLoads.push_back(user);
} else {
sawUnexpectedUser = true;
@@ -141,9 +136,8 @@ namespace MobileGL {
}
Instruction* workgroupSize = FindBuiltinDefinition(irContext, spv::BuiltIn::WorkgroupSize);
if (workgroupSize == nullptr ||
(workgroupSize->opcode() != spv::Op::OpConstantComposite &&
workgroupSize->opcode() != spv::Op::OpSpecConstantComposite)) {
if (workgroupSize == nullptr || (workgroupSize->opcode() != spv::Op::OpConstantComposite &&
workgroupSize->opcode() != spv::Op::OpSpecConstantComposite)) {
return Status::Failure;
}
const Instruction* workgroupSizeType = defUseMgr->GetDef(workgroupSize->type_id());
@@ -155,8 +149,7 @@ namespace MobileGL {
}
Instruction* subgroupSizeVar = FindBuiltinDefinition(irContext, spv::BuiltIn::SubgroupSize);
if (subgroupSizeVar != nullptr &&
!IsInputPointerTo(irContext, subgroupSizeVar, valueTypeId)) {
if (subgroupSizeVar != nullptr && !IsInputPointerTo(irContext, subgroupSizeVar, valueTypeId)) {
return Status::Failure;
}
@@ -174,9 +167,9 @@ namespace MobileGL {
}
const uint32_t oneId = oneInst->result_id();
const uint32_t subgroupSizeVarId = subgroupSizeVar != nullptr
? subgroupSizeVar->result_id()
: SynthesizeSubgroupSizeVariable(irContext, numSubgroupsVar->type_id());
const uint32_t subgroupSizeVarId =
subgroupSizeVar != nullptr ? subgroupSizeVar->result_id()
: SynthesizeSubgroupSizeVariable(irContext, numSubgroupsVar->type_id());
const uint32_t workgroupSizeId = workgroupSize->result_id();
// ceil(local invocation count / SubgroupSize): the subgroup count of a
@@ -200,49 +193,41 @@ namespace MobileGL {
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpCompositeExtract, valueTypeId, localSizeXId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpCompositeExtract, valueTypeId, localSizeYId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {1u}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {1u}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpCompositeExtract, valueTypeId, localSizeZId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {2u}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {workgroupSizeId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {2u}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpIMul, valueTypeId, localSizeXYId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {localSizeXId}},
{SPV_OPERAND_TYPE_ID, {localSizeYId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {localSizeXId}},
{SPV_OPERAND_TYPE_ID, {localSizeYId}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpIMul, valueTypeId, invocationCountId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {localSizeXYId}},
{SPV_OPERAND_TYPE_ID, {localSizeZId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {localSizeXYId}},
{SPV_OPERAND_TYPE_ID, {localSizeZId}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpISub, valueTypeId, adjustedCountId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {invocationCountId}},
{SPV_OPERAND_TYPE_ID, {oneId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {invocationCountId}},
{SPV_OPERAND_TYPE_ID, {oneId}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpLoad, valueTypeId, subgroupSizeId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {subgroupSizeVarId}}}));
load->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpUDiv, valueTypeId, quotientId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {adjustedCountId}},
{SPV_OPERAND_TYPE_ID, {subgroupSizeId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {adjustedCountId}},
{SPV_OPERAND_TYPE_ID, {subgroupSizeId}}}));
// Preserve the original result id so every downstream use automatically sees
// the derived value instead of the driver's NumSubgroups builtin.
load->SetOpcode(spv::Op::OpIAdd);
load->SetInOperands(Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {quotientId}},
{SPV_OPERAND_TYPE_ID, {oneId}}});
load->SetInOperands(
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {quotientId}}, {SPV_OPERAND_TYPE_ID, {oneId}}});
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -104,9 +104,12 @@ namespace MobileGL {
// which is correct: `x == -0.0` and `x == 0.0` are the same
// predicate in IEEE, and abs() maps both zeroes onto +0.
switch (floatType->width()) {
case 32: return floatConstant->GetFloatValue() == 0.0f;
case 64: return floatConstant->GetDoubleValue() == 0.0;
default: return false;
case 32:
return floatConstant->GetFloatValue() == 0.0f;
case 64:
return floatConstant->GetDoubleValue() == 0.0;
default:
return false;
}
};
@@ -93,8 +93,8 @@ namespace MobileGL {
// Multiply |valueId| (of type |valueTypeId|) by the scalar |scalarId|, inserting the op
// before |before|. Returns the product's id.
uint32_t InsertScale(IRContext* ctx, Instruction* before, uint32_t valueTypeId,
uint32_t valueId, uint32_t scalarId, bool isVector) {
uint32_t InsertScale(IRContext* ctx, Instruction* before, uint32_t valueTypeId, uint32_t valueId,
uint32_t scalarId, bool isVector) {
const uint32_t productId = ctx->TakeNextId();
const spv::Op op = isVector ? spv::Op::OpVectorTimesScalar : spv::Op::OpFMul;
before->InsertBefore(spvtools::MakeUnique<Instruction>(
@@ -115,10 +115,8 @@ namespace MobileGL {
uint32_t member = 0;
for (Instruction& ann : ctx->annotations()) {
if (ann.opcode() == spv::Op::OpMemberDecorate && ann.NumInOperands() >= 4 &&
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(2)) ==
spv::Decoration::BuiltIn &&
static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(3)) ==
spv::BuiltIn::Position) {
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(2)) == spv::Decoration::BuiltIn &&
static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(3)) == spv::BuiltIn::Position) {
structId = ann.GetSingleWordInOperand(0);
member = ann.GetSingleWordInOperand(1);
break;
@@ -148,8 +146,7 @@ namespace MobileGL {
Instruction* FindBuiltinInput(IRContext* ctx, spv::BuiltIn builtin) {
for (Instruction& ann : ctx->annotations()) {
if (ann.opcode() != spv::Op::OpDecorate || ann.NumInOperands() < 3) continue;
if (static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) !=
spv::Decoration::BuiltIn)
if (static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) != spv::Decoration::BuiltIn)
continue;
if (static_cast<spv::BuiltIn>(ann.GetSingleWordInOperand(2)) != builtin) continue;
Instruction* var = ctx->get_def_use_mgr()->GetDef(ann.GetSingleWordInOperand(0));
@@ -168,16 +165,13 @@ namespace MobileGL {
ctx->AddGlobalValue(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpVariable, ptrType, varId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Input)}}}));
ctx->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {varId}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{static_cast<uint32_t>(spv::BuiltIn::FragCoord)}}}));
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {static_cast<uint32_t>(spv::BuiltIn::FragCoord)}}}));
for (Instruction& ep : ctx->module()->entry_points()) {
ep.AddOperand({SPV_OPERAND_TYPE_ID, {varId}});
}
@@ -196,8 +190,7 @@ namespace MobileGL {
std::vector<Instruction*> decorationsToKill;
for (Instruction& ann : ctx->annotations()) {
if (ann.opcode() == spv::Op::OpDecorate && ann.NumInOperands() >= 2 &&
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) ==
spv::Decoration::NoPerspective) {
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) == spv::Decoration::NoPerspective) {
plainVarIds.push_back(ann.GetSingleWordInOperand(0));
decorationsToKill.push_back(&ann);
} else if (ann.opcode() == spv::Op::OpMemberDecorate && ann.NumInOperands() >= 3 &&
@@ -213,8 +206,7 @@ namespace MobileGL {
return Status::SuccessWithoutChange;
}
const spv::StorageClass wantStorage =
isVertex ? spv::StorageClass::Output : spv::StorageClass::Input;
const spv::StorageClass wantStorage = isVertex ? spv::StorageClass::Output : spv::StorageClass::Input;
// Emulatable = plain variable of the stage's interface direction, float or floatN.
struct Target {
@@ -228,8 +220,7 @@ namespace MobileGL {
for (const uint32_t id : plainVarIds) {
Instruction* var = ctx->get_def_use_mgr()->GetDef(id);
if (var == nullptr || var->opcode() != spv::Op::OpVariable) continue;
if (static_cast<spv::StorageClass>(var->GetSingleWordInOperand(0)) != wantStorage)
continue;
if (static_cast<spv::StorageClass>(var->GetSingleWordInOperand(0)) != wantStorage) continue;
const uint32_t pointee = VariablePointeeType(ctx, var);
uint32_t floatTypeId = 0;
bool isVector = false;
@@ -245,13 +236,14 @@ namespace MobileGL {
if (!targets.empty()) {
std::vector<uint32_t> targetIds;
targetIds.reserve(targets.size());
for (const Target& t : targets) targetIds.push_back(t.var->result_id());
for (const Target& t : targets)
targetIds.push_back(t.var->result_id());
for (Instruction& ann : ctx->annotations()) {
if (ann.opcode() == spv::Op::OpDecorate && ann.NumInOperands() >= 2 &&
static_cast<spv::Decoration>(ann.GetSingleWordInOperand(1)) ==
spv::Decoration::RelaxedPrecision &&
std::find(targetIds.begin(), targetIds.end(),
ann.GetSingleWordInOperand(0)) != targetIds.end()) {
std::find(targetIds.begin(), targetIds.end(), ann.GetSingleWordInOperand(0)) !=
targetIds.end()) {
decorationsToKill.push_back(&ann);
}
}
@@ -262,8 +254,7 @@ namespace MobileGL {
uint32_t memberIndex = 0;
uint32_t v4floatTypeId = 0;
if (FindPositionBlock(ctx, blockVarId, memberIndex, v4floatTypeId)) {
const uint32_t ptrOutV4 =
PointerTypeTo(ctx, v4floatTypeId, spv::StorageClass::Output);
const uint32_t ptrOutV4 = PointerTypeTo(ctx, v4floatTypeId, spv::StorageClass::Output);
const uint32_t memberConst = SignedIntConstant(ctx, static_cast<int32_t>(memberIndex));
const uint32_t floatTy = FloatType(ctx);
@@ -281,8 +272,7 @@ namespace MobileGL {
for (auto funcIt = ctx->module()->begin(); funcIt != ctx->module()->end(); ++funcIt) {
if (funcIt->result_id() != entryFuncId) continue;
funcIt->ForEachInst([&](Instruction* inst) {
if (inst->opcode() != spv::Op::OpReturn &&
inst->opcode() != spv::Op::OpReturnValue) {
if (inst->opcode() != spv::Op::OpReturn && inst->opcode() != spv::Op::OpReturnValue) {
return;
}
const uint32_t posPtrId = ctx->TakeNextId();
@@ -303,15 +293,12 @@ namespace MobileGL {
const uint32_t valId = ctx->TakeNextId();
inst->InsertBefore(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpLoad, t.typeId, valId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {t.var->result_id()}}}));
const uint32_t scaledId =
InsertScale(ctx, inst, t.typeId, valId, wId, t.isVector);
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {t.var->result_id()}}}));
const uint32_t scaledId = InsertScale(ctx, inst, t.typeId, valId, wId, t.isVector);
inst->InsertBefore(spvtools::MakeUnique<Instruction>(
ctx, spv::Op::OpStore, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {t.var->result_id()}},
{SPV_OPERAND_TYPE_ID, {scaledId}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {t.var->result_id()}},
{SPV_OPERAND_TYPE_ID, {scaledId}}}));
}
});
}
@@ -383,8 +370,8 @@ namespace MobileGL {
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {fcId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {3u}}}));
load->SetOpcode(loadIsVector ? spv::Op::OpVectorTimesScalar : spv::Op::OpFMul);
load->SetInOperands(Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {origId}}, {SPV_OPERAND_TYPE_ID, {wId}}});
load->SetInOperands(Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {origId}},
{SPV_OPERAND_TYPE_ID, {wId}}});
}
}
}
@@ -48,7 +48,9 @@ namespace MobileGL {
constexpr uint32_t kGlslFindILsb = 73u;
constexpr uint32_t kGlslFindUMsb = 75u;
Operand IdOp(uint32_t id) { return {SPV_OPERAND_TYPE_ID, {id}}; }
Operand IdOp(uint32_t id) {
return {SPV_OPERAND_TYPE_ID, {id}};
}
Instruction* FindBuiltinDefinition(IRContext* context, spv::BuiltIn builtin) {
auto* defUseMgr = context->get_def_use_mgr();
@@ -69,13 +71,18 @@ namespace MobileGL {
}
// The scalar shapes a value participating in an emulated exchange may have.
enum class ScalarKind { Float, SInt, UInt, Bool };
enum class ScalarKind {
Float,
SInt,
UInt,
Bool
};
struct ValueTypeInfo {
ScalarKind kind = ScalarKind::UInt;
uint32_t componentCount = 1; // 1 for scalars, 2..4 for vectors
uint32_t typeId = 0; // the value's own type
uint32_t scalarTypeId = 0; // the component type
uint32_t componentCount = 1; // 1 for scalars, 2..4 for vectors
uint32_t typeId = 0; // the value's own type
uint32_t scalarTypeId = 0; // the component type
};
bool ClassifyScalar(const Instruction* type, ScalarKind* kind) {
@@ -121,14 +128,14 @@ namespace MobileGL {
uint32_t Emit(spv::Op opcode, uint32_t typeId, Instruction::OperandList operands) {
const uint32_t resultId = m_context->TakeNextId();
m_anchor->InsertBefore(spvtools::MakeUnique<Instruction>(
m_context, opcode, typeId, resultId, std::move(operands)));
m_anchor->InsertBefore(spvtools::MakeUnique<Instruction>(m_context, opcode, typeId, resultId,
std::move(operands)));
return resultId;
}
void EmitNoResult(spv::Op opcode, Instruction::OperandList operands) {
m_anchor->InsertBefore(spvtools::MakeUnique<Instruction>(
m_context, opcode, 0, 0, std::move(operands)));
m_anchor->InsertBefore(
spvtools::MakeUnique<Instruction>(m_context, opcode, 0, 0, std::move(operands)));
}
private:
@@ -142,11 +149,11 @@ namespace MobileGL {
Instruction* inst = nullptr;
spv::Op opcode = spv::Op::OpNop;
spv::GroupOperation groupOperation = spv::GroupOperation::Reduce;
uint32_t clusterSize = 0; // ClusteredReduce only
uint32_t quadDirection = 0; // QuadSwap only
ValueTypeInfo valueType{}; // participating value (where applicable)
uint32_t clusterSize = 0; // ClusteredReduce only
uint32_t quadDirection = 0; // QuadSwap only
ValueTypeInfo valueType{}; // participating value (where applicable)
uint32_t valueId = 0;
uint32_t indexId = 0; // broadcast/shuffle/extract index operand
uint32_t indexId = 0; // broadcast/shuffle/extract index operand
};
struct BarrierSite {
@@ -156,8 +163,7 @@ namespace MobileGL {
};
bool IsGroupNonUniformOpcode(spv::Op opcode) {
return opcode >= spv::Op::OpGroupNonUniformElect &&
opcode <= spv::Op::OpGroupNonUniformQuadSwap;
return opcode >= spv::Op::OpGroupNonUniformElect && opcode <= spv::Op::OpGroupNonUniformQuadSwap;
}
} // namespace
@@ -188,17 +194,15 @@ namespace MobileGL {
// Subgroup builtin variables and their loads.
static constexpr std::array<spv::BuiltIn, 9> kSubgroupBuiltins = {
spv::BuiltIn::SubgroupSize, spv::BuiltIn::SubgroupLocalInvocationId,
spv::BuiltIn::SubgroupId, spv::BuiltIn::NumSubgroups,
spv::BuiltIn::SubgroupEqMask, spv::BuiltIn::SubgroupGeMask,
spv::BuiltIn::SubgroupGtMask, spv::BuiltIn::SubgroupLeMask,
spv::BuiltIn::SubgroupSize, spv::BuiltIn::SubgroupLocalInvocationId,
spv::BuiltIn::SubgroupId, spv::BuiltIn::NumSubgroups,
spv::BuiltIn::SubgroupEqMask, spv::BuiltIn::SubgroupGeMask,
spv::BuiltIn::SubgroupGtMask, spv::BuiltIn::SubgroupLeMask,
spv::BuiltIn::SubgroupLtMask};
const auto isMaskBuiltin = [](spv::BuiltIn builtin) {
return builtin == spv::BuiltIn::SubgroupEqMask ||
builtin == spv::BuiltIn::SubgroupGeMask ||
builtin == spv::BuiltIn::SubgroupGtMask ||
builtin == spv::BuiltIn::SubgroupLeMask ||
return builtin == spv::BuiltIn::SubgroupEqMask || builtin == spv::BuiltIn::SubgroupGeMask ||
builtin == spv::BuiltIn::SubgroupGtMask || builtin == spv::BuiltIn::SubgroupLeMask ||
builtin == spv::BuiltIn::SubgroupLtMask;
};
struct BuiltinUse {
@@ -221,8 +225,7 @@ namespace MobileGL {
defUseMgr->ForEachUser(variable, [&](Instruction* user) {
switch (user->opcode()) {
case spv::Op::OpLoad:
if (user->NumInOperands() >= 1 &&
user->GetSingleWordInOperand(0) == variableId) {
if (user->NumInOperands() >= 1 && user->GetSingleWordInOperand(0) == variableId) {
use.loads.push_back(user);
} else {
sawUnexpectedUser = true;
@@ -236,8 +239,7 @@ namespace MobileGL {
sawUnexpectedUser = true;
return;
}
const Instruction* index =
defUseMgr->GetDef(user->GetSingleWordInOperand(1));
const Instruction* index = defUseMgr->GetDef(user->GetSingleWordInOperand(1));
if (index == nullptr || index->opcode() != spv::Op::OpConstant ||
(component = index->GetSingleWordInOperand(0)) > 3u) {
sawUnexpectedUser = true;
@@ -245,8 +247,7 @@ namespace MobileGL {
}
const uint32_t chainId = user->result_id();
defUseMgr->ForEachUser(user, [&](Instruction* chainUser) {
if (chainUser->opcode() == spv::Op::OpLoad &&
chainUser->NumInOperands() >= 1 &&
if (chainUser->opcode() == spv::Op::OpLoad && chainUser->NumInOperands() >= 1 &&
chainUser->GetSingleWordInOperand(0) == chainId) {
use.componentLoads.emplace_back(chainUser, component);
} else {
@@ -349,15 +350,13 @@ namespace MobileGL {
break;
case spv::Op::OpGroupNonUniformQuadSwap:
site.valueId = inst.GetSingleWordInOperand(1);
if (!resolveUintConstant(inst.GetSingleWordInOperand(2),
&site.quadDirection) ||
if (!resolveUintConstant(inst.GetSingleWordInOperand(2), &site.quadDirection) ||
site.quadDirection > 2u) {
sawUnsupported = true;
}
break;
case spv::Op::OpGroupNonUniformBallotBitCount:
site.groupOperation = static_cast<spv::GroupOperation>(
inst.GetSingleWordInOperand(1));
site.groupOperation = static_cast<spv::GroupOperation>(inst.GetSingleWordInOperand(1));
site.valueId = inst.GetSingleWordInOperand(2);
break;
case spv::Op::OpGroupNonUniformIAdd:
@@ -376,15 +375,12 @@ namespace MobileGL {
case spv::Op::OpGroupNonUniformLogicalAnd:
case spv::Op::OpGroupNonUniformLogicalOr:
case spv::Op::OpGroupNonUniformLogicalXor:
site.groupOperation = static_cast<spv::GroupOperation>(
inst.GetSingleWordInOperand(1));
site.groupOperation = static_cast<spv::GroupOperation>(inst.GetSingleWordInOperand(1));
site.valueId = inst.GetSingleWordInOperand(2);
if (site.groupOperation == spv::GroupOperation::ClusteredReduce) {
if (inst.NumInOperands() < 4 ||
!resolveUintConstant(inst.GetSingleWordInOperand(3),
&site.clusterSize) ||
site.clusterSize == 0u ||
(site.clusterSize & (site.clusterSize - 1u)) != 0u) {
!resolveUintConstant(inst.GetSingleWordInOperand(3), &site.clusterSize) ||
site.clusterSize == 0u || (site.clusterSize & (site.clusterSize - 1u)) != 0u) {
sawUnsupported = true;
}
} else if (site.groupOperation != spv::GroupOperation::Reduce &&
@@ -415,8 +411,7 @@ namespace MobileGL {
const Instruction* value = defUseMgr->GetDef(site.valueId);
if (value == nullptr ||
!ClassifyValueType(irContext, value->type_id(), &site.valueType) ||
site.valueType.kind != ScalarKind::Bool ||
site.valueType.componentCount != 1u) {
site.valueType.kind != ScalarKind::Bool || site.valueType.componentCount != 1u) {
sawUnsupported = true;
}
break;
@@ -452,14 +447,12 @@ namespace MobileGL {
// is accepted; spec-constant sizes are not (GL has no specialization).
uint32_t localSize[3] = {0, 0, 0};
bool haveLocalSize = false;
if (Instruction* workgroupSize =
FindBuiltinDefinition(irContext, spv::BuiltIn::WorkgroupSize)) {
if (Instruction* workgroupSize = FindBuiltinDefinition(irContext, spv::BuiltIn::WorkgroupSize)) {
if (workgroupSize->opcode() == spv::Op::OpConstantComposite &&
workgroupSize->NumInOperands() == 3) {
haveLocalSize =
resolveUintConstant(workgroupSize->GetSingleWordInOperand(0), &localSize[0]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(1), &localSize[1]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(2), &localSize[2]);
haveLocalSize = resolveUintConstant(workgroupSize->GetSingleWordInOperand(0), &localSize[0]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(1), &localSize[1]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(2), &localSize[2]);
}
}
if (!haveLocalSize) {
@@ -478,8 +471,7 @@ namespace MobileGL {
if (!haveLocalSize || localSize[0] == 0u || localSize[1] == 0u || localSize[2] == 0u) {
return Status::Failure;
}
const uint64_t total64 =
static_cast<uint64_t>(localSize[0]) * localSize[1] * localSize[2];
const uint64_t total64 = static_cast<uint64_t>(localSize[0]) * localSize[1] * localSize[2];
if (total64 == 0u || total64 > (1u << 20)) {
return Status::Failure;
}
@@ -501,13 +493,12 @@ namespace MobileGL {
std::set<uint32_t> scratchTypeKeys;
uint64_t scratchBytes = 0;
for (const GroupOpSite& site : groupOps) {
if (site.valueType.typeId == 0u) continue; // no shared-memory exchange
if (site.valueType.typeId == 0u) continue; // no shared-memory exchange
const uint32_t key = site.valueType.kind == ScalarKind::Bool
? (0x80000000u | site.valueType.componentCount)
: site.valueType.typeId;
if (!scratchTypeKeys.insert(key).second) continue;
scratchBytes += static_cast<uint64_t>(paddedSlots) * 4u *
site.valueType.componentCount;
scratchBytes += static_cast<uint64_t>(paddedSlots) * 4u * site.valueType.componentCount;
}
if (scratchBytes > scratchBudgetBytes) {
return Status::Failure;
@@ -528,38 +519,31 @@ namespace MobileGL {
const analysis::Type* type = typeMgr->GetType(uintTypeId);
const analysis::Constant* constant = constMgr->GetConstant(type, {value});
const Instruction* inst =
constant != nullptr ? constMgr->GetDefiningInstruction(constant, uintTypeId)
: nullptr;
constant != nullptr ? constMgr->GetDefiningInstruction(constant, uintTypeId) : nullptr;
return inst != nullptr ? inst->result_id() : 0u;
};
const auto boolConst = [&](bool value) -> uint32_t {
const analysis::Type* type = typeMgr->GetType(boolTypeId);
const analysis::Constant* constant =
constMgr->GetConstant(type, {value ? 1u : 0u});
const analysis::Constant* constant = constMgr->GetConstant(type, {value ? 1u : 0u});
const Instruction* inst =
constant != nullptr ? constMgr->GetDefiningInstruction(constant, boolTypeId)
: nullptr;
constant != nullptr ? constMgr->GetDefiningInstruction(constant, boolTypeId) : nullptr;
return inst != nullptr ? inst->result_id() : 0u;
};
// A constant of an arbitrary 32-bit scalar type from its bit pattern, splat
// to the value type when it is a vector.
const auto typedConst = [&](const ValueTypeInfo& info, uint32_t scalarBits,
uint32_t scratchScalarTypeId,
uint32_t scratchTypeId) -> uint32_t {
uint32_t scratchScalarTypeId, uint32_t scratchTypeId) -> uint32_t {
const analysis::Type* scalarType = typeMgr->GetType(scratchScalarTypeId);
const analysis::Constant* scalar = constMgr->GetConstant(scalarType, {scalarBits});
if (scalar == nullptr) return 0u;
const Instruction* scalarInst =
constMgr->GetDefiningInstruction(scalar, scratchScalarTypeId);
const Instruction* scalarInst = constMgr->GetDefiningInstruction(scalar, scratchScalarTypeId);
if (scalarInst == nullptr) return 0u;
if (info.componentCount == 1u) return scalarInst->result_id();
const analysis::Type* vectorType = typeMgr->GetType(scratchTypeId);
std::vector<uint32_t> componentIds(info.componentCount,
scalarInst->result_id());
std::vector<uint32_t> componentIds(info.componentCount, scalarInst->result_id());
const analysis::Constant* vector = constMgr->GetConstant(vectorType, componentIds);
if (vector == nullptr) return 0u;
const Instruction* vectorInst =
constMgr->GetDefiningInstruction(vector, scratchTypeId);
const Instruction* vectorInst = constMgr->GetDefiningInstruction(vector, scratchTypeId);
return vectorInst != nullptr ? vectorInst->result_id() : 0u;
};
@@ -571,16 +555,15 @@ namespace MobileGL {
const uint32_t cTotal = uintConst(totalInvocations);
const uint32_t cAllOnes = uintConst(0xffffffffu);
const uint32_t cTrue = boolConst(true);
if (c0 == 0u || c1 == 0u || c31 == 0u || c32 == 0u || cNotLane == 0u ||
cTotal == 0u || cAllOnes == 0u || cTrue == 0u) {
if (c0 == 0u || c1 == 0u || c31 == 0u || c32 == 0u || cNotLane == 0u || cTotal == 0u ||
cAllOnes == 0u || cTrue == 0u) {
return Status::Failure;
}
// GLSL.std.450 import (for min/max combines and ballot find ops).
uint32_t glslStd450Id = 0;
for (const Instruction& import : irContext->module()->ext_inst_imports()) {
const std::string importName =
spvtools::utils::MakeString(import.GetInOperand(0).words);
const std::string importName = spvtools::utils::MakeString(import.GetInOperand(0).words);
if (importName == "GLSL.std.450") {
glslStd450Id = import.result_id();
break;
@@ -592,34 +575,29 @@ namespace MobileGL {
irContext->module()->AddExtInstImport(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpExtInstImport, 0, glslStd450Id,
Instruction::OperandList{
{SPV_OPERAND_TYPE_LITERAL_STRING,
spvtools::utils::MakeVector("GLSL.std.450")}}));
{SPV_OPERAND_TYPE_LITERAL_STRING, spvtools::utils::MakeVector("GLSL.std.450")}}));
return glslStd450Id;
};
// gl_LocalInvocationIndex drives the whole virtual topology; synthesize the
// builtin when the module never declared it.
Instruction* liiVariable =
FindBuiltinDefinition(irContext, spv::BuiltIn::LocalInvocationIndex);
Instruction* liiVariable = FindBuiltinDefinition(irContext, spv::BuiltIn::LocalInvocationIndex);
uint32_t liiVariableId = 0;
if (liiVariable != nullptr && liiVariable->opcode() == spv::Op::OpVariable) {
liiVariableId = liiVariable->result_id();
} else {
const uint32_t pointerTypeId =
typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Input);
const uint32_t pointerTypeId = typeMgr->FindPointerToType(uintTypeId, spv::StorageClass::Input);
if (pointerTypeId == 0u) return Status::Failure;
liiVariableId = irContext->TakeNextId();
irContext->AddGlobalValue(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVariable, pointerTypeId, liiVariableId,
Instruction::OperandList{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Input)}}}));
irContext->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpDecorate, 0, 0,
Instruction::OperandList{
IdOp(liiVariableId),
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{static_cast<uint32_t>(spv::BuiltIn::LocalInvocationIndex)}}}));
for (Instruction& entryPoint : irContext->module()->entry_points()) {
@@ -646,8 +624,7 @@ namespace MobileGL {
if (m_atomicScratchReads) elementTypeId = uintTypeId;
auto it = scratchByType.find(key);
if (it != scratchByType.end()) return &it->second;
const uint32_t lengthId =
m_atomicScratchReads ? uintConst(paddedSlots * wordsPerSlot) : cPadded;
const uint32_t lengthId = m_atomicScratchReads ? uintConst(paddedSlots * wordsPerSlot) : cPadded;
if (lengthId == 0u) return nullptr;
const uint32_t arrayTypeId = irContext->TakeNextId();
irContext->AddType(spvtools::MakeUnique<Instruction>(
@@ -662,16 +639,15 @@ namespace MobileGL {
irContext->AddGlobalValue(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVariable, arrayPointerTypeId, variableId,
Instruction::OperandList{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Workgroup)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Workgroup)}}}));
Scratch scratch{variableId, elementTypeId, elementPointerTypeId};
return &scratchByType.emplace(key, scratch).first->second;
};
// The uint-domain twin of a participating type: bools travel as 0/1 words.
struct ScratchTypeInfo {
uint32_t typeId = 0; // element type stored in scratch
uint32_t scalarTypeId = 0; // its component type
uint32_t typeId = 0; // element type stored in scratch
uint32_t scalarTypeId = 0; // its component type
bool boolConverted = false;
};
const auto scratchTypeFor = [&](const ValueTypeInfo& info) -> ScratchTypeInfo {
@@ -705,32 +681,26 @@ namespace MobileGL {
// actually has: the masks name lane positions, and GL/Vulkan define them for
// every position below the subgroup size.
const auto emitMaskWord = [&](Emitter& em, spv::BuiltIn builtin) -> uint32_t {
const uint32_t index =
em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t lane =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(index), IdOp(c31)});
const uint32_t index = em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t lane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(index), IdOp(c31)});
const auto lowMaskThroughLane = [&](bool inclusive) -> uint32_t {
if (!inclusive) {
const uint32_t bit = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(c1), IdOp(lane)});
const uint32_t bit =
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(c1), IdOp(lane)});
return em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(bit), IdOp(c1)});
}
const uint32_t lanePlusOne =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(lane), IdOp(c1)});
const uint32_t atTop = em.Emit(spv::Op::OpUGreaterThanEqual, boolTypeId,
{IdOp(lanePlusOne), IdOp(c32)});
const uint32_t shifted = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(c1), IdOp(lanePlusOne)});
const uint32_t below =
em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(shifted), IdOp(c1)});
return em.Emit(spv::Op::OpSelect, uintTypeId,
{IdOp(atTop), IdOp(cAllOnes), IdOp(below)});
const uint32_t lanePlusOne = em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(lane), IdOp(c1)});
const uint32_t atTop =
em.Emit(spv::Op::OpUGreaterThanEqual, boolTypeId, {IdOp(lanePlusOne), IdOp(c32)});
const uint32_t shifted =
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(c1), IdOp(lanePlusOne)});
const uint32_t below = em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(shifted), IdOp(c1)});
return em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(atTop), IdOp(cAllOnes), IdOp(below)});
};
uint32_t word = 0;
switch (builtin) {
case spv::BuiltIn::SubgroupEqMask:
word = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(c1), IdOp(lane)});
word = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(c1), IdOp(lane)});
break;
case spv::BuiltIn::SubgroupLtMask:
word = lowMaskThroughLane(false);
@@ -763,15 +733,13 @@ namespace MobileGL {
load->SetInOperands({IdOp(numVirtualConstId)});
break;
case spv::BuiltIn::SubgroupLocalInvocationId: {
const uint32_t index =
em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t index = em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
load->SetOpcode(spv::Op::OpBitwiseAnd);
load->SetInOperands({IdOp(index), IdOp(c31)});
break;
}
case spv::BuiltIn::SubgroupId: {
const uint32_t index =
em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t index = em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
load->SetOpcode(spv::Op::OpShiftRightLogical);
load->SetInOperands({IdOp(index), IdOp(uintConst(kIdShift))});
break;
@@ -780,8 +748,7 @@ namespace MobileGL {
const uint32_t word = emitMaskWord(em, use.builtin);
if (word == 0u) return Status::Failure;
load->SetOpcode(spv::Op::OpCompositeConstruct);
load->SetInOperands(
{IdOp(word), IdOp(c0), IdOp(c0), IdOp(c0)});
load->SetInOperands({IdOp(word), IdOp(c0), IdOp(c0), IdOp(c0)});
break;
}
}
@@ -805,38 +772,29 @@ namespace MobileGL {
Instruction* inst = site.inst;
Emitter em(irContext, inst);
const uint32_t lii =
em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t lane =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(lii), IdOp(c31)});
const uint32_t base =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(lii), IdOp(cNotLane)});
const uint32_t lii = em.Emit(spv::Op::OpLoad, uintTypeId, {IdOp(liiVariableId)});
const uint32_t lane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(lii), IdOp(c31)});
const uint32_t base = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(lii), IdOp(cNotLane)});
const auto barrier = [&]() {
const uint32_t scopeId = uintConst(static_cast<uint32_t>(spv::Scope::Workgroup));
const uint32_t semanticsId = uintConst(
static_cast<uint32_t>(spv::MemorySemanticsMask::AcquireRelease) |
static_cast<uint32_t>(spv::MemorySemanticsMask::WorkgroupMemory));
em.EmitNoResult(spv::Op::OpControlBarrier,
{IdOp(scopeId), IdOp(scopeId), IdOp(semanticsId)});
const uint32_t semanticsId =
uintConst(static_cast<uint32_t>(spv::MemorySemanticsMask::AcquireRelease) |
static_cast<uint32_t>(spv::MemorySemanticsMask::WorkgroupMemory));
em.EmitNoResult(spv::Op::OpControlBarrier, {IdOp(scopeId), IdOp(scopeId), IdOp(semanticsId)});
};
const ScratchTypeInfo scratchType = scratchTypeFor(site.valueType);
const auto toScratch = [&](uint32_t valueId) -> uint32_t {
if (!scratchType.boolConverted) return valueId;
const uint32_t ones =
typedConst(site.valueType, 1u, uintTypeId, scratchType.typeId);
const uint32_t zeros =
typedConst(site.valueType, 0u, uintTypeId, scratchType.typeId);
return em.Emit(spv::Op::OpSelect, scratchType.typeId,
{IdOp(valueId), IdOp(ones), IdOp(zeros)});
const uint32_t ones = typedConst(site.valueType, 1u, uintTypeId, scratchType.typeId);
const uint32_t zeros = typedConst(site.valueType, 0u, uintTypeId, scratchType.typeId);
return em.Emit(spv::Op::OpSelect, scratchType.typeId, {IdOp(valueId), IdOp(ones), IdOp(zeros)});
};
const auto fromScratch = [&](uint32_t valueId) -> uint32_t {
if (!scratchType.boolConverted) return valueId;
const uint32_t zeros =
typedConst(site.valueType, 0u, uintTypeId, scratchType.typeId);
return em.Emit(spv::Op::OpINotEqual, site.valueType.typeId,
{IdOp(valueId), IdOp(zeros)});
const uint32_t zeros = typedConst(site.valueType, 0u, uintTypeId, scratchType.typeId);
return em.Emit(spv::Op::OpINotEqual, site.valueType.typeId, {IdOp(valueId), IdOp(zeros)});
};
// Atomic scratch reads keep each slot as componentCount uint words; the
@@ -851,8 +809,8 @@ namespace MobileGL {
const bool wordsNeedBitcast = scratchType.scalarTypeId != uintTypeId;
const auto wordIndex = [&](uint32_t slotId, uint32_t component) -> uint32_t {
if (wordsPerSlot == 1u) return slotId;
const uint32_t scaled = em.Emit(spv::Op::OpIMul, uintTypeId,
{IdOp(slotId), IdOp(uintConst(wordsPerSlot))});
const uint32_t scaled =
em.Emit(spv::Op::OpIMul, uintTypeId, {IdOp(slotId), IdOp(uintConst(wordsPerSlot))});
return em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(scaled), IdOp(uintConst(component))});
};
@@ -861,9 +819,8 @@ namespace MobileGL {
scratch = scratchFor(scratchType.typeId, wordsPerSlot);
if (scratch == nullptr) return false;
if (!m_atomicScratchReads) {
const uint32_t pointer =
em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(lii)});
const uint32_t pointer = em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(lii)});
em.EmitNoResult(spv::Op::OpStore, {IdOp(pointer), IdOp(valueId)});
} else {
const uint32_t words =
@@ -874,9 +831,8 @@ namespace MobileGL {
? words
: em.Emit(spv::Op::OpCompositeExtract, uintTypeId,
{IdOp(words), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {c}}});
const uint32_t pointer =
em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(wordIndex(lii, c))});
const uint32_t pointer = em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(wordIndex(lii, c))});
em.EmitNoResult(spv::Op::OpStore, {IdOp(pointer), IdOp(word)});
}
}
@@ -885,20 +841,18 @@ namespace MobileGL {
};
const auto readSlot = [&](uint32_t indexId) -> uint32_t {
if (!m_atomicScratchReads) {
const uint32_t pointer =
em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(indexId)});
const uint32_t pointer = em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(indexId)});
return em.Emit(spv::Op::OpLoad, scratchType.typeId, {IdOp(pointer)});
}
const uint32_t scopeId = uintConst(static_cast<uint32_t>(spv::Scope::Workgroup));
Instruction::OperandList words;
uint32_t packed = 0;
for (uint32_t c = 0; c < wordsPerSlot; ++c) {
const uint32_t pointer =
em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(wordIndex(indexId, c))});
packed = em.Emit(spv::Op::OpAtomicLoad, uintTypeId,
{IdOp(pointer), IdOp(scopeId), IdOp(c0)});
const uint32_t pointer = em.Emit(spv::Op::OpAccessChain, scratch->pointerTypeId,
{IdOp(scratch->variableId), IdOp(wordIndex(indexId, c))});
packed =
em.Emit(spv::Op::OpAtomicLoad, uintTypeId, {IdOp(pointer), IdOp(scopeId), IdOp(c0)});
words.push_back(IdOp(packed));
}
if (wordsPerSlot > 1u) {
@@ -909,16 +863,15 @@ namespace MobileGL {
};
// A select whose condition is a scalar bool but whose values may be
// vectors; pre-SPIR-V-1.4 needs the condition splat to a bvec.
const auto guardedSelect = [&](uint32_t condId, uint32_t thenId,
uint32_t elseId) -> uint32_t {
const auto guardedSelect = [&](uint32_t condId, uint32_t thenId, uint32_t elseId) -> uint32_t {
const uint32_t componentCount = site.valueType.componentCount;
uint32_t conditionId = condId;
if (componentCount > 1u) {
const uint32_t conditionTypeId = boolVectorTypeId(componentCount);
Instruction::OperandList splat;
for (uint32_t i = 0; i < componentCount; ++i) splat.push_back(IdOp(condId));
conditionId =
em.Emit(spv::Op::OpCompositeConstruct, conditionTypeId, std::move(splat));
for (uint32_t i = 0; i < componentCount; ++i)
splat.push_back(IdOp(condId));
conditionId = em.Emit(spv::Op::OpCompositeConstruct, conditionTypeId, std::move(splat));
}
return em.Emit(spv::Op::OpSelect, scratchType.typeId,
{IdOp(conditionId), IdOp(thenId), IdOp(elseId)});
@@ -929,15 +882,14 @@ namespace MobileGL {
return em.Emit(spv::Op::OpExtInst, typeId,
{IdOp(setId),
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {instNumber}},
IdOp(a), IdOp(b)});
IdOp(a),
IdOp(b)});
};
const auto extInst1 = [&](uint32_t typeId, uint32_t instNumber,
uint32_t a) -> uint32_t {
const auto extInst1 = [&](uint32_t typeId, uint32_t instNumber, uint32_t a) -> uint32_t {
const uint32_t setId = ensureGlslStd450();
return em.Emit(spv::Op::OpExtInst, typeId,
{IdOp(setId),
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {instNumber}},
IdOp(a)});
return em.Emit(
spv::Op::OpExtInst, typeId,
{IdOp(setId), {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {instNumber}}, IdOp(a)});
};
// Combine two scratch-domain values with the site's operation.
@@ -1037,15 +989,14 @@ namespace MobileGL {
for (uint32_t k = 0; k < kWidth; ++k) {
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t exists = em.Emit(spv::Op::OpULessThan, boolTypeId,
{IdOp(slot), IdOp(cTotal)});
const uint32_t exists =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(slot), IdOp(cTotal)});
const uint32_t value = readSlot(slot);
const uint32_t guarded = em.Emit(
spv::Op::OpSelect, uintTypeId,
{IdOp(exists), IdOp(value), IdOp(neutral)});
const uint32_t guarded =
em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(exists), IdOp(value), IdOp(neutral)});
acc = k == 0 ? guarded
: em.Emit(isAll ? spv::Op::OpBitwiseAnd : spv::Op::OpBitwiseOr,
uintTypeId, {IdOp(acc), IdOp(guarded)});
: em.Emit(isAll ? spv::Op::OpBitwiseAnd : spv::Op::OpBitwiseOr, uintTypeId,
{IdOp(acc), IdOp(guarded)});
}
barrier();
finish(em.Emit(spv::Op::OpINotEqual, boolTypeId, {IdOp(acc), IdOp(c0)}));
@@ -1059,28 +1010,23 @@ namespace MobileGL {
for (uint32_t k = 0; k < kWidth; ++k) {
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t exists = em.Emit(spv::Op::OpULessThan, boolTypeId,
{IdOp(slot), IdOp(cTotal)});
const uint32_t exists =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(slot), IdOp(cTotal)});
const uint32_t value = readSlot(slot);
const spv::Op compareOpcode = site.valueType.kind == ScalarKind::Float
? spv::Op::OpFOrdEqual
: spv::Op::OpIEqual;
const spv::Op compareOpcode =
site.valueType.kind == ScalarKind::Float ? spv::Op::OpFOrdEqual : spv::Op::OpIEqual;
uint32_t equal = 0;
if (site.valueType.componentCount == 1u) {
equal = em.Emit(compareOpcode, boolTypeId,
{IdOp(value), IdOp(reference)});
equal = em.Emit(compareOpcode, boolTypeId, {IdOp(value), IdOp(reference)});
} else {
const uint32_t comparisonTypeId =
boolVectorTypeId(site.valueType.componentCount);
const uint32_t componentsEqual = em.Emit(
compareOpcode, comparisonTypeId, {IdOp(value), IdOp(reference)});
const uint32_t comparisonTypeId = boolVectorTypeId(site.valueType.componentCount);
const uint32_t componentsEqual =
em.Emit(compareOpcode, comparisonTypeId, {IdOp(value), IdOp(reference)});
equal = em.Emit(spv::Op::OpAll, boolTypeId, {IdOp(componentsEqual)});
}
const uint32_t guarded = em.Emit(
spv::Op::OpSelect, boolTypeId,
{IdOp(exists), IdOp(equal), IdOp(cTrue)});
acc = em.Emit(spv::Op::OpLogicalAnd, boolTypeId,
{IdOp(acc), IdOp(guarded)});
const uint32_t guarded =
em.Emit(spv::Op::OpSelect, boolTypeId, {IdOp(exists), IdOp(equal), IdOp(cTrue)});
acc = em.Emit(spv::Op::OpLogicalAnd, boolTypeId, {IdOp(acc), IdOp(guarded)});
}
barrier();
finish(acc);
@@ -1103,46 +1049,39 @@ namespace MobileGL {
break;
case spv::Op::OpGroupNonUniformBroadcast:
case spv::Op::OpGroupNonUniformShuffle:
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(site.indexId), IdOp(c31)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(site.indexId), IdOp(c31)});
break;
case spv::Op::OpGroupNonUniformShuffleXor: {
const uint32_t flipped = em.Emit(
spv::Op::OpBitwiseXor, uintTypeId, {IdOp(lane), IdOp(site.indexId)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(flipped), IdOp(c31)});
const uint32_t flipped =
em.Emit(spv::Op::OpBitwiseXor, uintTypeId, {IdOp(lane), IdOp(site.indexId)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(flipped), IdOp(c31)});
break;
}
case spv::Op::OpGroupNonUniformShuffleUp: {
const uint32_t shifted =
em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(lane), IdOp(site.indexId)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(shifted), IdOp(c31)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c31)});
break;
}
case spv::Op::OpGroupNonUniformShuffleDown: {
const uint32_t shifted =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(lane), IdOp(site.indexId)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(shifted), IdOp(c31)});
sourceLane = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c31)});
break;
}
case spv::Op::OpGroupNonUniformQuadBroadcast: {
const uint32_t quadBase =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(lane), IdOp(uintConst(~3u & kLaneMask))});
const uint32_t quadBase = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(lane), IdOp(uintConst(~3u & kLaneMask))});
const uint32_t withinQuad =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(site.indexId), IdOp(uintConst(3u))});
sourceLane = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(quadBase), IdOp(withinQuad)});
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(site.indexId), IdOp(uintConst(3u))});
sourceLane = em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(quadBase), IdOp(withinQuad)});
break;
}
case spv::Op::OpGroupNonUniformQuadSwap: {
const uint32_t partnerMask = uintConst(
site.quadDirection == 0u ? 1u : site.quadDirection == 1u ? 2u : 3u);
sourceLane = em.Emit(spv::Op::OpBitwiseXor, uintTypeId,
{IdOp(lane), IdOp(partnerMask)});
const uint32_t partnerMask = uintConst(site.quadDirection == 0u ? 1u
: site.quadDirection == 1u ? 2u
: 3u);
sourceLane = em.Emit(spv::Op::OpBitwiseXor, uintTypeId, {IdOp(lane), IdOp(partnerMask)});
break;
}
default:
@@ -1162,13 +1101,13 @@ namespace MobileGL {
for (uint32_t k = 0; k < kWidth; ++k) {
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t exists = em.Emit(spv::Op::OpULessThan, boolTypeId,
{IdOp(slot), IdOp(cTotal)});
const uint32_t exists =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(slot), IdOp(cTotal)});
const uint32_t value = readSlot(slot);
const uint32_t guarded = em.Emit(
spv::Op::OpSelect, uintTypeId, {IdOp(exists), IdOp(value), IdOp(c0)});
const uint32_t bit = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(guarded), IdOp(uintConst(k))});
const uint32_t guarded =
em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(exists), IdOp(value), IdOp(c0)});
const uint32_t bit =
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(guarded), IdOp(uintConst(k))});
word = em.Emit(spv::Op::OpBitwiseOr, uintTypeId, {IdOp(word), IdOp(bit)});
}
barrier();
@@ -1178,12 +1117,10 @@ namespace MobileGL {
}
case spv::Op::OpGroupNonUniformInverseBallot: {
const uint32_t word = em.Emit(spv::Op::OpCompositeExtract, uintTypeId,
{IdOp(site.valueId),
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
const uint32_t shifted = em.Emit(spv::Op::OpShiftRightLogical, uintTypeId,
{IdOp(word), IdOp(lane)});
const uint32_t bit =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c1)});
{IdOp(site.valueId), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
const uint32_t shifted =
em.Emit(spv::Op::OpShiftRightLogical, uintTypeId, {IdOp(word), IdOp(lane)});
const uint32_t bit = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c1)});
finish(em.Emit(spv::Op::OpINotEqual, boolTypeId, {IdOp(bit), IdOp(c0)}));
break;
}
@@ -1197,34 +1134,29 @@ namespace MobileGL {
{IdOp(site.valueId), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {2u}}}),
em.Emit(spv::Op::OpCompositeExtract, uintTypeId,
{IdOp(site.valueId), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {3u}}})};
const uint32_t inFirstPair = em.Emit(
spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(uintConst(64u))});
const uint32_t inWord0 = em.Emit(
spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(c32)});
const uint32_t inWord2 = em.Emit(
spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(uintConst(96u))});
const uint32_t firstPair = em.Emit(
spv::Op::OpSelect, uintTypeId,
{IdOp(inWord0), IdOp(words[0]), IdOp(words[1])});
const uint32_t secondPair = em.Emit(
spv::Op::OpSelect, uintTypeId,
{IdOp(inWord2), IdOp(words[2]), IdOp(words[3])});
const uint32_t word = em.Emit(
spv::Op::OpSelect, uintTypeId,
{IdOp(inFirstPair), IdOp(firstPair), IdOp(secondPair)});
const uint32_t bitIndex = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(site.indexId), IdOp(c31)});
const uint32_t shifted = em.Emit(spv::Op::OpShiftRightLogical, uintTypeId,
{IdOp(word), IdOp(bitIndex)});
const uint32_t bit =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c1)});
const uint32_t inFirstPair =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(uintConst(64u))});
const uint32_t inWord0 =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(c32)});
const uint32_t inWord2 =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(site.indexId), IdOp(uintConst(96u))});
const uint32_t firstPair =
em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(inWord0), IdOp(words[0]), IdOp(words[1])});
const uint32_t secondPair =
em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(inWord2), IdOp(words[2]), IdOp(words[3])});
const uint32_t word = em.Emit(spv::Op::OpSelect, uintTypeId,
{IdOp(inFirstPair), IdOp(firstPair), IdOp(secondPair)});
const uint32_t bitIndex =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(site.indexId), IdOp(c31)});
const uint32_t shifted =
em.Emit(spv::Op::OpShiftRightLogical, uintTypeId, {IdOp(word), IdOp(bitIndex)});
const uint32_t bit = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(shifted), IdOp(c1)});
finish(em.Emit(spv::Op::OpINotEqual, boolTypeId, {IdOp(bit), IdOp(c0)}));
break;
}
case spv::Op::OpGroupNonUniformBallotBitCount: {
const uint32_t word = em.Emit(spv::Op::OpCompositeExtract, uintTypeId,
{IdOp(site.valueId),
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
{IdOp(site.valueId), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
uint32_t masked = 0;
// Counted over the gl_SubgroupSize (32) bits of the x word - the value is
// an arbitrary uvec4, and bits of lanes a partial subgroup lacks still
@@ -1232,30 +1164,24 @@ namespace MobileGL {
if (site.groupOperation == spv::GroupOperation::Reduce) {
masked = word;
} else {
const bool inclusive =
site.groupOperation == spv::GroupOperation::InclusiveScan;
const bool inclusive = site.groupOperation == spv::GroupOperation::InclusiveScan;
uint32_t laneMask = 0;
if (inclusive) {
const uint32_t lanePlusOne =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(lane), IdOp(c1)});
const uint32_t atTop =
em.Emit(spv::Op::OpUGreaterThanEqual, boolTypeId,
{IdOp(lanePlusOne), IdOp(c32)});
em.Emit(spv::Op::OpUGreaterThanEqual, boolTypeId, {IdOp(lanePlusOne), IdOp(c32)});
const uint32_t shifted =
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(c1), IdOp(lanePlusOne)});
const uint32_t below =
em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(shifted), IdOp(c1)});
laneMask = em.Emit(spv::Op::OpSelect, uintTypeId,
{IdOp(atTop), IdOp(cAllOnes), IdOp(below)});
} else {
const uint32_t bit = em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId,
{IdOp(c1), IdOp(lane)});
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(c1), IdOp(lanePlusOne)});
const uint32_t below = em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(shifted), IdOp(c1)});
laneMask =
em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(bit), IdOp(c1)});
em.Emit(spv::Op::OpSelect, uintTypeId, {IdOp(atTop), IdOp(cAllOnes), IdOp(below)});
} else {
const uint32_t bit =
em.Emit(spv::Op::OpShiftLeftLogical, uintTypeId, {IdOp(c1), IdOp(lane)});
laneMask = em.Emit(spv::Op::OpISub, uintTypeId, {IdOp(bit), IdOp(c1)});
}
masked = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(word), IdOp(laneMask)});
masked = em.Emit(spv::Op::OpBitwiseAnd, uintTypeId, {IdOp(word), IdOp(laneMask)});
}
finish(em.Emit(spv::Op::OpBitCount, uintTypeId, {IdOp(masked)}));
break;
@@ -1263,13 +1189,11 @@ namespace MobileGL {
case spv::Op::OpGroupNonUniformBallotFindLSB:
case spv::Op::OpGroupNonUniformBallotFindMSB: {
const uint32_t word = em.Emit(spv::Op::OpCompositeExtract, uintTypeId,
{IdOp(site.valueId),
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
{IdOp(site.valueId), {SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}}});
// Over the gl_SubgroupSize (32) bits of the x word, like the bit count.
const uint32_t masked = word;
const uint32_t instNumber =
site.opcode == spv::Op::OpGroupNonUniformBallotFindLSB ? kGlslFindILsb
: kGlslFindUMsb;
site.opcode == spv::Op::OpGroupNonUniformBallotFindLSB ? kGlslFindILsb : kGlslFindUMsb;
finish(extInst1(uintTypeId, instNumber, masked));
break;
}
@@ -1284,13 +1208,16 @@ namespace MobileGL {
case spv::GroupOperation::Reduce: {
uint32_t acc = readSlot(base);
for (uint32_t k = 1; k < kWidth; ++k) {
const uint32_t slot = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(base), IdOp(uintConst(k))});
const uint32_t exists = em.Emit(spv::Op::OpULessThan, boolTypeId,
{IdOp(slot), IdOp(cTotal)});
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t exists =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(slot), IdOp(cTotal)});
const uint32_t value = readSlot(slot);
const uint32_t combined = combine(acc, value);
if (combined == 0u) { ok = false; break; }
if (combined == 0u) {
ok = false;
break;
}
acc = guardedSelect(exists, combined, acc);
}
result = acc;
@@ -1302,13 +1229,15 @@ namespace MobileGL {
uint32_t acc = readSlot(base);
for (uint32_t k = 1; k < kWidth; ++k) {
const uint32_t included =
em.Emit(spv::Op::OpULessThanEqual, boolTypeId,
{IdOp(uintConst(k)), IdOp(lane)});
const uint32_t slot = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(base), IdOp(uintConst(k))});
em.Emit(spv::Op::OpULessThanEqual, boolTypeId, {IdOp(uintConst(k)), IdOp(lane)});
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t value = readSlot(slot);
const uint32_t combined = combine(acc, value);
if (combined == 0u) { ok = false; break; }
if (combined == 0u) {
ok = false;
break;
}
acc = guardedSelect(included, combined, acc);
}
result = acc;
@@ -1316,37 +1245,45 @@ namespace MobileGL {
}
case spv::GroupOperation::ExclusiveScan: {
uint32_t acc = identity();
if (acc == 0u) { ok = false; break; }
if (acc == 0u) {
ok = false;
break;
}
for (uint32_t k = 0; k < kWidth; ++k) {
const uint32_t included = em.Emit(
spv::Op::OpULessThan, boolTypeId, {IdOp(uintConst(k)), IdOp(lane)});
const uint32_t slot = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(base), IdOp(uintConst(k))});
const uint32_t included =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(uintConst(k)), IdOp(lane)});
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(uintConst(k))});
const uint32_t value = readSlot(slot);
const uint32_t combined = combine(acc, value);
if (combined == 0u) { ok = false; break; }
if (combined == 0u) {
ok = false;
break;
}
acc = guardedSelect(included, combined, acc);
}
result = acc;
break;
}
case spv::GroupOperation::ClusteredReduce: {
const uint32_t cluster =
site.clusterSize >= kWidth ? kWidth : site.clusterSize;
const uint32_t clusterBase = em.Emit(
spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(lane), IdOp(uintConst(~(cluster - 1u) & kLaneMask))});
const uint32_t firstSlot = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(base), IdOp(clusterBase)});
const uint32_t cluster = site.clusterSize >= kWidth ? kWidth : site.clusterSize;
const uint32_t clusterBase =
em.Emit(spv::Op::OpBitwiseAnd, uintTypeId,
{IdOp(lane), IdOp(uintConst(~(cluster - 1u) & kLaneMask))});
const uint32_t firstSlot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(base), IdOp(clusterBase)});
uint32_t acc = readSlot(firstSlot);
for (uint32_t k = 1; k < cluster; ++k) {
const uint32_t slot = em.Emit(spv::Op::OpIAdd, uintTypeId,
{IdOp(firstSlot), IdOp(uintConst(k))});
const uint32_t exists = em.Emit(spv::Op::OpULessThan, boolTypeId,
{IdOp(slot), IdOp(cTotal)});
const uint32_t slot =
em.Emit(spv::Op::OpIAdd, uintTypeId, {IdOp(firstSlot), IdOp(uintConst(k))});
const uint32_t exists =
em.Emit(spv::Op::OpULessThan, boolTypeId, {IdOp(slot), IdOp(cTotal)});
const uint32_t value = readSlot(slot);
const uint32_t combined = combine(acc, value);
if (combined == 0u) { ok = false; break; }
if (combined == 0u) {
ok = false;
break;
}
acc = guardedSelect(exists, combined, acc);
}
result = acc;
@@ -1372,8 +1309,7 @@ namespace MobileGL {
// Widen subgroup-scoped barriers to the workgroup scope the emulation
// synchronizes at anyway; strictly stronger, so always safe.
const uint32_t workgroupScopeId =
uintConst(static_cast<uint32_t>(spv::Scope::Workgroup));
const uint32_t workgroupScopeId = uintConst(static_cast<uint32_t>(spv::Scope::Workgroup));
if (workgroupScopeId == 0u) return Status::Failure;
for (const BarrierSite& site : barriers) {
if (site.inst->opcode() == spv::Op::OpControlBarrier) {
@@ -1424,8 +1360,7 @@ namespace MobileGL {
spv::Capability::GroupNonUniformPartitionedNV};
std::vector<Instruction*> deadCapabilities;
for (Instruction& capability : irContext->module()->capabilities()) {
const auto value =
static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
const auto value = static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
for (const spv::Capability candidate : kSubgroupCapabilities) {
if (value == candidate) {
deadCapabilities.push_back(&capability);
@@ -64,14 +64,13 @@ namespace MobileGL {
class EmulateSubgroupsPass : public spvtools::opt::Pass {
public:
EmulateSubgroupsPass(Uint32 maxWorkgroupScratchBytes, bool atomicScratchReads)
: m_maxWorkgroupScratchBytes(maxWorkgroupScratchBytes),
m_atomicScratchReads(atomicScratchReads) {}
: m_maxWorkgroupScratchBytes(maxWorkgroupScratchBytes), m_atomicScratchReads(atomicScratchReads) {}
const char* name() const override { return "emulate-subgroups"; }
Status Process() override;
static spvtools::Optimizer::PassToken CreateEmulateSubgroupsPass(
Uint32 maxWorkgroupScratchBytes, bool atomicScratchReads = false);
static spvtools::Optimizer::PassToken CreateEmulateSubgroupsPass(Uint32 maxWorkgroupScratchBytes,
bool atomicScratchReads = false);
private:
Uint32 m_maxWorkgroupScratchBytes;
@@ -87,8 +87,7 @@ namespace MobileGL {
type = defUseMgr->GetDef(type->GetSingleWordInOperand(0));
if (type == nullptr) return 0u;
}
if (type->opcode() != spv::Op::OpTypeFloat ||
type->GetSingleWordInOperand(0) != 32u) {
if (type->opcode() != spv::Op::OpTypeFloat || type->GetSingleWordInOperand(0) != 32u) {
return 0u;
}
return components;
@@ -105,8 +104,8 @@ namespace MobileGL {
// budget check matters. Returns false for anything not modelled here,
// which the caller answers by declining to grow at all rather than by
// certifying growth against a total it knows is an underestimate.
bool WorkgroupTypeLayout(IRContext* context, uint32_t typeId, uint32_t* size,
uint32_t* alignment, uint32_t depth = 0u) {
bool WorkgroupTypeLayout(IRContext* context, uint32_t typeId, uint32_t* size, uint32_t* alignment,
uint32_t depth = 0u) {
if (depth > 8u) return false;
auto* defUseMgr = context->get_def_use_mgr();
const Instruction* type = defUseMgr->GetDef(typeId);
@@ -127,8 +126,8 @@ namespace MobileGL {
case spv::Op::OpTypeVector: {
uint32_t componentSize = 0u;
uint32_t componentAlignment = 0u;
if (!WorkgroupTypeLayout(context, type->GetSingleWordInOperand(0),
&componentSize, &componentAlignment, depth + 1u)) {
if (!WorkgroupTypeLayout(context, type->GetSingleWordInOperand(0), &componentSize,
&componentAlignment, depth + 1u)) {
return false;
}
const uint32_t components = type->GetSingleWordInOperand(1);
@@ -150,10 +149,9 @@ namespace MobileGL {
if (type->opcode() == spv::Op::OpTypeMatrix) {
count = type->GetSingleWordInOperand(1);
} else {
const Instruction* length =
defUseMgr->GetDef(type->GetSingleWordInOperand(1));
const Instruction* length = defUseMgr->GetDef(type->GetSingleWordInOperand(1));
if (length == nullptr || length->opcode() != spv::Op::OpConstant) {
return false; // spec-constant length: not sizeable here
return false; // spec-constant length: not sizeable here
}
count = length->GetSingleWordInOperand(0);
}
@@ -167,8 +165,8 @@ namespace MobileGL {
for (uint32_t i = 0; i < type->NumInOperands(); ++i) {
uint32_t memberSize = 0u;
uint32_t memberAlignment = 0u;
if (!WorkgroupTypeLayout(context, type->GetSingleWordInOperand(i),
&memberSize, &memberAlignment, depth + 1u)) {
if (!WorkgroupTypeLayout(context, type->GetSingleWordInOperand(i), &memberSize,
&memberAlignment, depth + 1u)) {
return false;
}
offset = RoundUp(offset, memberAlignment) + memberSize;
@@ -222,14 +220,12 @@ namespace MobileGL {
};
uint32_t localSize[3] = {0, 0, 0};
bool haveLocalSize = false;
if (Instruction* workgroupSize =
FindBuiltinDefinition(irContext, spv::BuiltIn::WorkgroupSize)) {
if (Instruction* workgroupSize = FindBuiltinDefinition(irContext, spv::BuiltIn::WorkgroupSize)) {
if (workgroupSize->opcode() == spv::Op::OpConstantComposite &&
workgroupSize->NumInOperands() == 3) {
haveLocalSize =
resolveUintConstant(workgroupSize->GetSingleWordInOperand(0), &localSize[0]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(1), &localSize[1]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(2), &localSize[2]);
haveLocalSize = resolveUintConstant(workgroupSize->GetSingleWordInOperand(0), &localSize[0]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(1), &localSize[1]) &&
resolveUintConstant(workgroupSize->GetSingleWordInOperand(2), &localSize[2]);
}
}
if (!haveLocalSize) {
@@ -248,13 +244,12 @@ namespace MobileGL {
if (!haveLocalSize || localSize[0] == 0u || localSize[1] == 0u || localSize[2] == 0u) {
return Status::SuccessWithoutChange;
}
const uint64_t totalInvocations =
static_cast<uint64_t>(localSize[0]) * localSize[1] * localSize[2];
const uint64_t totalInvocations = static_cast<uint64_t>(localSize[0]) * localSize[1] * localSize[2];
if (totalInvocations == 0u || totalInvocations > (1u << 20)) {
return Status::SuccessWithoutChange;
}
const uint32_t requiredLength = static_cast<uint32_t>(
(totalInvocations + m_nativeSubgroupSize - 1u) / m_nativeSubgroupSize);
const uint32_t requiredLength =
static_cast<uint32_t>((totalInvocations + m_nativeSubgroupSize - 1u) / m_nativeSubgroupSize);
// Fingerprint 2: a subgroup scan over a 32-bit float value - the pack's
// prefix-sum reduction, and the reason its scratch is indexed per subgroup.
@@ -268,8 +263,7 @@ namespace MobileGL {
continue;
}
if (inst.NumInOperands() < 2) continue;
if (!IsScanOrReduce(static_cast<spv::GroupOperation>(
inst.GetSingleWordInOperand(1)))) {
if (!IsScanOrReduce(static_cast<spv::GroupOperation>(inst.GetSingleWordInOperand(1)))) {
continue;
}
if (Float32ComponentCount(irContext, inst.type_id()) != 0u) {
@@ -283,10 +277,8 @@ namespace MobileGL {
}
// gl_SubgroupID, whose value range the pack's scratch size bakes in.
const Instruction* subgroupIdVariable =
FindBuiltinDefinition(irContext, spv::BuiltIn::SubgroupId);
if (subgroupIdVariable == nullptr ||
subgroupIdVariable->opcode() != spv::Op::OpVariable) {
const Instruction* subgroupIdVariable = FindBuiltinDefinition(irContext, spv::BuiltIn::SubgroupId);
if (subgroupIdVariable == nullptr || subgroupIdVariable->opcode() != spv::Op::OpVariable) {
return Status::SuccessWithoutChange;
}
const uint32_t subgroupIdVariableId = subgroupIdVariable->result_id();
@@ -316,8 +308,8 @@ namespace MobileGL {
if (target == nullptr || target->opcode() != spv::Op::OpVariable) {
continue;
}
const auto storageClass = static_cast<spv::StorageClass>(
target->GetSingleWordInOperand(0));
const auto storageClass =
static_cast<spv::StorageClass>(target->GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Function &&
storageClass != spv::StorageClass::Private) {
continue;
@@ -340,22 +332,20 @@ namespace MobileGL {
for (auto& function : *irContext->module()) {
for (auto& block : function) {
for (auto& inst : block) {
if (inst.result_id() == 0 ||
subgroupIdValues.count(inst.result_id())) {
if (inst.result_id() == 0 || subgroupIdValues.count(inst.result_id())) {
continue;
}
bool isSubgroupId = false;
switch (inst.opcode()) {
case spv::Op::OpLoad: {
const uint32_t pointerId = inst.GetSingleWordInOperand(0);
isSubgroupId = pointerId == subgroupIdVariableId ||
subgroupIdVariables.count(pointerId) != 0u;
isSubgroupId =
pointerId == subgroupIdVariableId || subgroupIdVariables.count(pointerId) != 0u;
break;
}
case spv::Op::OpCopyObject:
case spv::Op::OpBitcast:
isSubgroupId =
subgroupIdValues.count(inst.GetSingleWordInOperand(0)) != 0u;
isSubgroupId = subgroupIdValues.count(inst.GetSingleWordInOperand(0)) != 0u;
break;
default:
break;
@@ -384,12 +374,9 @@ namespace MobileGL {
}
if (inst.NumInOperands() < 2) continue;
if (!subgroupIdValues.count(inst.GetSingleWordInOperand(1))) continue;
Instruction* baseVariable =
defUseMgr->GetDef(inst.GetSingleWordInOperand(0));
if (baseVariable == nullptr ||
baseVariable->opcode() != spv::Op::OpVariable ||
static_cast<spv::StorageClass>(
baseVariable->GetSingleWordInOperand(0)) !=
Instruction* baseVariable = defUseMgr->GetDef(inst.GetSingleWordInOperand(0));
if (baseVariable == nullptr || baseVariable->opcode() != spv::Op::OpVariable ||
static_cast<spv::StorageClass>(baseVariable->GetSingleWordInOperand(0)) !=
spv::StorageClass::Workgroup) {
continue;
}
@@ -442,16 +429,14 @@ namespace MobileGL {
if (pointerType == nullptr || pointerType->opcode() != spv::Op::OpTypePointer) {
continue;
}
const Instruction* arrayType =
defUseMgr->GetDef(pointerType->GetSingleWordInOperand(1));
const Instruction* arrayType = defUseMgr->GetDef(pointerType->GetSingleWordInOperand(1));
if (arrayType == nullptr || arrayType->opcode() != spv::Op::OpTypeArray) {
continue;
}
const uint32_t elementTypeId = arrayType->GetSingleWordInOperand(0);
const uint32_t components = Float32ComponentCount(irContext, elementTypeId);
if (components == 0u) continue;
const Instruction* lengthConstant =
defUseMgr->GetDef(arrayType->GetSingleWordInOperand(1));
const Instruction* lengthConstant = defUseMgr->GetDef(arrayType->GetSingleWordInOperand(1));
if (lengthConstant == nullptr || lengthConstant->opcode() != spv::Op::OpConstant) {
continue;
}
@@ -486,10 +471,9 @@ namespace MobileGL {
const Instruction* pointerType = defUseMgr->GetDef(global.type_id());
uint32_t bytes = 0u;
uint32_t alignment = 0u;
if (pointerType == nullptr ||
pointerType->opcode() != spv::Op::OpTypePointer ||
!WorkgroupTypeLayout(irContext, pointerType->GetSingleWordInOperand(1),
&bytes, &alignment)) {
if (pointerType == nullptr || pointerType->opcode() != spv::Op::OpTypePointer ||
!WorkgroupTypeLayout(irContext, pointerType->GetSingleWordInOperand(1), &bytes,
&alignment)) {
sawUnsizeable = true;
break;
}
@@ -501,11 +485,11 @@ namespace MobileGL {
if (sawUnsizeable) {
return Status::SuccessWithoutChange;
}
for (const Growth& growth : growths) declaredBytes += growth.addedBytes;
for (const Growth& growth : growths)
declaredBytes += growth.addedBytes;
const uint32_t deviceBudget = m_maxWorkgroupScratchBytes != 0u
? m_maxWorkgroupScratchBytes
: kMinimumSharedMemoryBytes;
const uint32_t deviceBudget =
m_maxWorkgroupScratchBytes != 0u ? m_maxWorkgroupScratchBytes : kMinimumSharedMemoryBytes;
if (declaredBytes > deviceBudget) {
return Status::SuccessWithoutChange;
}
@@ -524,20 +508,17 @@ namespace MobileGL {
const uint32_t newLengthId = irContext->TakeNextId();
variable->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpConstant, growth.lengthTypeId, newLengthId,
Instruction::OperandList{{SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
{requiredLength}}}));
Instruction::OperandList{{SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, {requiredLength}}}));
const uint32_t newArrayTypeId = irContext->TakeNextId();
variable->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpTypeArray, 0, newArrayTypeId,
Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {growth.elementTypeId}},
{SPV_OPERAND_TYPE_ID, {newLengthId}}}));
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {growth.elementTypeId}},
{SPV_OPERAND_TYPE_ID, {newLengthId}}}));
const uint32_t newPointerTypeId = irContext->TakeNextId();
variable->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpTypePointer, 0, newPointerTypeId,
Instruction::OperandList{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Workgroup)}},
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Workgroup)}},
{SPV_OPERAND_TYPE_ID, {newArrayTypeId}}}));
variable->SetResultType(newPointerTypeId);
@@ -547,11 +528,10 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken
FixIterationRPSubgroupScratchPass::CreateFixIterationRPSubgroupScratchPass(
spvtools::Optimizer::PassToken FixIterationRPSubgroupScratchPass::CreateFixIterationRPSubgroupScratchPass(
const Uint32 nativeSubgroupSize, const Uint32 maxWorkgroupScratchBytes) {
return spvtools::Optimizer::PassToken(MakeUnique<FixIterationRPSubgroupScratchPass>(
nativeSubgroupSize, maxWorkgroupScratchBytes));
return spvtools::Optimizer::PassToken(
MakeUnique<FixIterationRPSubgroupScratchPass>(nativeSubgroupSize, maxWorkgroupScratchBytes));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -53,10 +53,8 @@ namespace MobileGL {
// maxWorkgroupScratchBytes; 0 falls back to the 16384-byte Vulkan minimum.
class FixIterationRPSubgroupScratchPass : public spvtools::opt::Pass {
public:
FixIterationRPSubgroupScratchPass(Uint32 nativeSubgroupSize,
Uint32 maxWorkgroupScratchBytes)
: m_nativeSubgroupSize(nativeSubgroupSize),
m_maxWorkgroupScratchBytes(maxWorkgroupScratchBytes) {}
FixIterationRPSubgroupScratchPass(Uint32 nativeSubgroupSize, Uint32 maxWorkgroupScratchBytes)
: m_nativeSubgroupSize(nativeSubgroupSize), m_maxWorkgroupScratchBytes(maxWorkgroupScratchBytes) {}
const char* name() const override { return "fix-iterationrp-subgroup-scratch"; }
Status Process() override;
@@ -77,8 +77,7 @@ namespace MobileGL {
if (debug.opcode() != spv::Op::OpName || debug.NumInOperands() < 2) continue;
if (debug.GetSingleWordInOperand(0) != id) continue;
const std::string name = debug.GetInOperand(1).AsString();
return name.compare(0, std::strlen(kAtomicCounterBlockPrefix),
kAtomicCounterBlockPrefix) == 0;
return name.compare(0, std::strlen(kAtomicCounterBlockPrefix), kAtomicCounterBlockPrefix) == 0;
}
return false;
}
@@ -86,10 +85,8 @@ namespace MobileGL {
// The literal of the first OpMemberDecorate <structId> <member> <kind>, or none.
bool TryGetMemberDecorationLiteral(IRContext* context, uint32_t structId, uint32_t member,
spv::Decoration kind, uint32_t* literal) {
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->NumInOperands() < 4 ||
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->NumInOperands() < 4 ||
decoration->GetSingleWordInOperand(1) != member ||
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(2)) != kind) {
continue;
@@ -100,8 +97,7 @@ namespace MobileGL {
return false;
}
bool TryGetDecorationLiteral(IRContext* context, uint32_t id, spv::Decoration kind,
uint32_t* literal) {
bool TryGetDecorationLiteral(IRContext* context, uint32_t id, spv::Decoration kind, uint32_t* literal) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(id, false)) {
if (decoration->opcode() != spv::Op::OpDecorate || decoration->NumInOperands() < 3 ||
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(1)) != kind) {
@@ -114,15 +110,13 @@ namespace MobileGL {
}
bool IsUint32Type(const Instruction* type) {
return type != nullptr && type->opcode() == spv::Op::OpTypeInt &&
type->NumInOperands() >= 2 && type->GetSingleWordInOperand(0) == 32u &&
type->GetSingleWordInOperand(1) == 0u;
return type != nullptr && type->opcode() == spv::Op::OpTypeInt && type->NumInOperands() >= 2 &&
type->GetSingleWordInOperand(0) == 32u && type->GetSingleWordInOperand(1) == 0u;
}
// The member's shape as this pass needs it, or false when it is one the pass
// cannot re-index.
bool DescribeMember(IRContext* context, uint32_t memberTypeId, uint32_t* uintTypeId,
MemberPlan* plan) {
bool DescribeMember(IRContext* context, uint32_t memberTypeId, uint32_t* uintTypeId, MemberPlan* plan) {
auto* defUseMgr = context->get_def_use_mgr();
Instruction* memberType = defUseMgr->GetDef(memberTypeId);
if (memberType == nullptr) return false;
@@ -208,8 +202,7 @@ namespace MobileGL {
return;
case spv::Op::OpAccessChain:
case spv::Op::OpInBoundsAccessChain:
if (user->NumInOperands() >= 2 &&
user->GetSingleWordInOperand(0) == variableId) {
if (user->NumInOperands() >= 2 && user->GetSingleWordInOperand(0) == variableId) {
chainsByStruct[structId].push_back(user);
return;
}
@@ -231,8 +224,8 @@ namespace MobileGL {
bool expressible = true;
for (uint32_t member = 0; member < memberCount; ++member) {
uint32_t byteOffset = 0;
if (!TryGetMemberDecorationLiteral(context, structId, member,
spv::Decoration::Offset, &byteOffset) ||
if (!TryGetMemberDecorationLiteral(context, structId, member, spv::Decoration::Offset,
&byteOffset) ||
byteOffset % kCounterBytes != 0u) {
expressible = false;
break;
@@ -272,8 +265,7 @@ namespace MobileGL {
if (chains != chainsByStruct.end()) {
for (Instruction* chain : chains->second) {
const spvtools::opt::analysis::Constant* memberIndex =
context->get_constant_mgr()->FindDeclaredConstant(
chain->GetSingleWordInOperand(1));
context->get_constant_mgr()->FindDeclaredConstant(chain->GetSingleWordInOperand(1));
if (memberIndex == nullptr || memberIndex->AsIntConstant() == nullptr) {
expressible = false;
break;
@@ -297,8 +289,7 @@ namespace MobileGL {
// The id of |value| as a constant of the same integer type as |likeId|.
uint32_t ConstantLike(IRContext* context, uint32_t likeId, uint32_t value) {
Instruction* likeDef = context->get_def_use_mgr()->GetDef(likeId);
const spvtools::opt::analysis::Type* type =
context->get_type_mgr()->GetType(likeDef->type_id());
const spvtools::opt::analysis::Type* type = context->get_type_mgr()->GetType(likeDef->type_id());
const spvtools::opt::analysis::Constant* constant =
context->get_constant_mgr()->GetConstant(type, {value});
return context->get_constant_mgr()->GetDefiningInstruction(constant)->result_id();
@@ -350,8 +341,8 @@ namespace MobileGL {
// the constant's old position and therefore after its new one too, so no use is
// left dangling - moving a definition earlier in the types/constants section
// cannot invalidate anything. Ordering is all that changes; def-use is untouched.
uint32_t CreateCounterArrayTypeBefore(IRContext* context, Instruction* structType,
uint32_t uintTypeId, uint32_t length) {
uint32_t CreateCounterArrayTypeBefore(IRContext* context, Instruction* structType, uint32_t uintTypeId,
uint32_t length) {
auto* constantMgr = context->get_constant_mgr();
const spvtools::opt::analysis::Type* uintType = context->get_type_mgr()->GetType(uintTypeId);
if (uintType == nullptr) return 0;
@@ -390,16 +381,13 @@ namespace MobileGL {
// rather than where they sit - are deliberately kept.
void StripMemberAnnotations(IRContext* context, uint32_t structId) {
std::vector<Instruction*> doomed;
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->NumInOperands() < 3) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->NumInOperands() < 3) {
continue;
}
const bool pastMemberZero = decoration->GetSingleWordInOperand(1) != 0u;
const bool isOffset =
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(2)) ==
spv::Decoration::Offset;
const bool isOffset = static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(2)) ==
spv::Decoration::Offset;
if (pastMemberZero || isOffset) doomed.push_back(decoration);
}
for (Instruction& debug : context->module()->debugs2()) {
@@ -408,7 +396,8 @@ namespace MobileGL {
if (debug.GetSingleWordInOperand(1) == 0u) continue; // member 0 keeps its name
doomed.push_back(&debug);
}
for (Instruction* inst : doomed) context->KillInst(inst);
for (Instruction* inst : doomed)
context->KillInst(inst);
}
} // namespace
@@ -417,8 +406,7 @@ namespace MobileGL {
return false;
}
std::unique_ptr<IRContext> context = spvtools::BuildModule(
SPV_ENV_VULKAN_1_1,
[](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
SPV_ENV_VULKAN_1_1, [](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
binary.data(), binary.size());
if (!context) {
// Unparseable here means unusable downstream too; let the ordinary transpile
@@ -438,8 +426,8 @@ namespace MobileGL {
bool modified = false;
for (const BlockPlan& plan : plans) {
const uint32_t structId = plan.structType->result_id();
const uint32_t arrayTypeId = CreateCounterArrayTypeBefore(
irContext, plan.structType, plan.uintTypeId, plan.totalElements);
const uint32_t arrayTypeId =
CreateCounterArrayTypeBefore(irContext, plan.structType, plan.uintTypeId, plan.totalElements);
if (arrayTypeId == 0) {
MGLOG_D("[spirv] atomic-counter block %%%u: no legal place for the flattened array "
"type; leaving the block alone",
@@ -461,9 +449,8 @@ namespace MobileGL {
operands.push_back(chain->GetInOperand(0));
operands.push_back({SPV_OPERAND_TYPE_ID, {ConstantLike(irContext, memberIndexId, 0u)}});
if (!memberPlan.isArray) {
operands.push_back(
{SPV_OPERAND_TYPE_ID,
{ConstantLike(irContext, memberIndexId, memberPlan.elementOffset)}});
operands.push_back({SPV_OPERAND_TYPE_ID,
{ConstantLike(irContext, memberIndexId, memberPlan.elementOffset)}});
} else {
const uint32_t elementId = chain->GetSingleWordInOperand(2);
uint32_t shiftedId = elementId;
@@ -475,16 +462,15 @@ namespace MobileGL {
elementConstant->AsIntConstant()->GetU32BitValue() +
memberPlan.elementOffset);
} else {
InstructionBuilder builder(
irContext, chain,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, chain,
IRContext::kAnalysisDefUse |
IRContext::kAnalysisInstrToBlockMapping);
Instruction* elementDef = irContext->get_def_use_mgr()->GetDef(elementId);
shiftedId = builder
.AddBinaryOp(elementDef->type_id(), spv::Op::OpIAdd,
elementId,
ConstantLike(irContext, elementId,
memberPlan.elementOffset))
->result_id();
shiftedId =
builder
.AddBinaryOp(elementDef->type_id(), spv::Op::OpIAdd, elementId,
ConstantLike(irContext, elementId, memberPlan.elementOffset))
->result_id();
}
}
operands.push_back({SPV_OPERAND_TYPE_ID, {shiftedId}});
@@ -52,9 +52,9 @@ namespace MobileGL {
// member would turn a two-instruction copy into a 32k-instruction one.
constexpr uint32_t kMaxLeavesPerAccess = 1024u;
// The analyses every builder in this pass keeps current as it inserts.
constexpr IRContext::Analysis kPreservedAnalyses = static_cast<IRContext::Analysis>(
static_cast<uint32_t>(IRContext::kAnalysisDefUse) |
static_cast<uint32_t>(IRContext::kAnalysisInstrToBlockMapping));
constexpr IRContext::Analysis kPreservedAnalyses =
static_cast<IRContext::Analysis>(static_cast<uint32_t>(IRContext::kAnalysisDefUse) |
static_cast<uint32_t>(IRContext::kAnalysisInstrToBlockMapping));
// How a type sits in memory, as the ENCLOSING struct member described it.
// MatrixStride and RowMajor are member decorations rather than type decorations,
@@ -99,8 +99,8 @@ namespace MobileGL {
};
bool IsDoubleType(const Instruction* type) {
return type != nullptr && type->opcode() == spv::Op::OpTypeFloat &&
type->NumInOperands() >= 1 && type->GetSingleWordInOperand(0) == 64u;
return type != nullptr && type->opcode() == spv::Op::OpTypeFloat && type->NumInOperands() >= 1 &&
type->GetSingleWordInOperand(0) == 64u;
}
// Byte size of a scalar this pass can carry, or 0 for one it cannot.
@@ -115,8 +115,7 @@ namespace MobileGL {
return 0;
}
bool TryGetDecorationLiteral(IRContext* context, uint32_t id, spv::Decoration kind,
uint32_t* literal) {
bool TryGetDecorationLiteral(IRContext* context, uint32_t id, spv::Decoration kind, uint32_t* literal) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(id, false)) {
if (decoration->opcode() != spv::Op::OpDecorate || decoration->NumInOperands() < 3 ||
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(1)) != kind) {
@@ -130,10 +129,8 @@ namespace MobileGL {
bool TryGetMemberDecorationLiteral(IRContext* context, uint32_t structId, uint32_t member,
spv::Decoration kind, uint32_t* literal) {
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->NumInOperands() < 4 ||
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->NumInOperands() < 4 ||
decoration->GetSingleWordInOperand(1) != member ||
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(2)) != kind) {
continue;
@@ -144,12 +141,9 @@ namespace MobileGL {
return false;
}
bool HasMemberDecoration(IRContext* context, uint32_t structId, uint32_t member,
spv::Decoration kind) {
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->NumInOperands() < 3 ||
bool HasMemberDecoration(IRContext* context, uint32_t structId, uint32_t member, spv::Decoration kind) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->NumInOperands() < 3 ||
decoration->GetSingleWordInOperand(1) != member) {
continue;
}
@@ -182,8 +176,8 @@ namespace MobileGL {
spv::Decoration::MatrixStride, &stride)) {
cursor.matrixStride = stride;
}
cursor.rowMajor = HasMemberDecoration(context, structType->result_id(), member,
spv::Decoration::RowMajor);
cursor.rowMajor =
HasMemberDecoration(context, structType->result_id(), member, spv::Decoration::RowMajor);
return cursor;
}
@@ -199,8 +193,8 @@ namespace MobileGL {
case spv::Op::OpTypeFloat:
return ScalarByteSize(type);
case spv::Op::OpTypeVector: {
const uint32_t component = ScalarByteSize(
context->get_def_use_mgr()->GetDef(type->GetSingleWordInOperand(0)));
const uint32_t component =
ScalarByteSize(context->get_def_use_mgr()->GetDef(type->GetSingleWordInOperand(0)));
if (component == 0) return 0;
return component * type->GetSingleWordInOperand(1);
}
@@ -210,8 +204,7 @@ namespace MobileGL {
}
case spv::Op::OpTypeArray: {
uint32_t stride = 0;
if (!TryGetDecorationLiteral(context, cursor.typeId, spv::Decoration::ArrayStride,
&stride) ||
if (!TryGetDecorationLiteral(context, cursor.typeId, spv::Decoration::ArrayStride, &stride) ||
stride == 0) {
return 0;
}
@@ -226,8 +219,8 @@ namespace MobileGL {
uint64_t end = 0;
for (uint32_t member = 0; member < type->NumInOperands(); ++member) {
uint32_t offset = 0;
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member,
spv::Decoration::Offset, &offset)) {
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member, spv::Decoration::Offset,
&offset)) {
return 0;
}
const uint32_t size = LaidOutByteSize(context, MemberCursor(context, type, member));
@@ -266,22 +259,19 @@ namespace MobileGL {
case spv::Op::OpTypeVector: {
TypeCursor component;
component.typeId = type->GetSingleWordInOperand(0);
const uint32_t repeats =
leafCount == nullptr ? 1u : type->GetSingleWordInOperand(1);
const uint32_t repeats = leafCount == nullptr ? 1u : type->GetSingleWordInOperand(1);
for (uint32_t i = 0; i < repeats; ++i) {
if (!CanDecompose(context, component, leafCount)) return false;
}
return true;
}
case spv::Op::OpTypeMatrix: {
if (cursor.matrixStride == 0 || cursor.rowMajor ||
cursor.matrixStride % kWordBytes != 0) {
if (cursor.matrixStride == 0 || cursor.rowMajor || cursor.matrixStride % kWordBytes != 0) {
return false;
}
TypeCursor column;
column.typeId = type->GetSingleWordInOperand(0);
const uint32_t repeats =
leafCount == nullptr ? 1u : type->GetSingleWordInOperand(1);
const uint32_t repeats = leafCount == nullptr ? 1u : type->GetSingleWordInOperand(1);
for (uint32_t i = 0; i < repeats; ++i) {
if (!CanDecompose(context, column, leafCount)) return false;
}
@@ -289,8 +279,7 @@ namespace MobileGL {
}
case spv::Op::OpTypeArray: {
uint32_t stride = 0;
if (!TryGetDecorationLiteral(context, cursor.typeId, spv::Decoration::ArrayStride,
&stride) ||
if (!TryGetDecorationLiteral(context, cursor.typeId, spv::Decoration::ArrayStride, &stride) ||
stride == 0 || stride % kWordBytes != 0) {
return false;
}
@@ -311,8 +300,8 @@ namespace MobileGL {
case spv::Op::OpTypeStruct: {
for (uint32_t member = 0; member < type->NumInOperands(); ++member) {
uint32_t offset = 0;
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member,
spv::Decoration::Offset, &offset) ||
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member, spv::Decoration::Offset,
&offset) ||
offset % kWordBytes != 0) {
return false;
}
@@ -334,8 +323,8 @@ namespace MobileGL {
// stride alongside. A runtime array anywhere else, one without a stride the
// words can step by, or one whose element the rewrite could not take apart is a
// shape this pass does not describe, and so is a bounded block it cannot size.
bool MeasureBlock(IRContext* context, const Instruction* structType, uint32_t* bytes,
bool* openEnded, uint32_t* tailStrideBytes) {
bool MeasureBlock(IRContext* context, const Instruction* structType, uint32_t* bytes, bool* openEnded,
uint32_t* tailStrideBytes) {
*bytes = 0;
*openEnded = false;
*tailStrideBytes = 0;
@@ -384,8 +373,7 @@ namespace MobileGL {
return true;
}
bool TypeContainsFloat64(IRContext* context, uint32_t typeId,
std::unordered_set<uint32_t>& visiting) {
bool TypeContainsFloat64(IRContext* context, uint32_t typeId, std::unordered_set<uint32_t>& visiting) {
const Instruction* type = context->get_def_use_mgr()->GetDef(typeId);
if (type == nullptr || !visiting.insert(typeId).second) return false;
switch (type->opcode()) {
@@ -420,8 +408,7 @@ namespace MobileGL {
// Walks one access chain against the block's type tree, recording the byte offset
// it names as a constant part plus a list of (index, stride) terms. False for any
// shape the rewrite cannot address exactly.
bool PlanChain(IRContext* context, Instruction* chain, const TypeCursor& blockCursor,
ChainPlan* plan) {
bool PlanChain(IRContext* context, Instruction* chain, const TypeCursor& blockCursor, ChainPlan* plan) {
TypeCursor cursor = blockCursor;
uint64_t constantBytes = 0;
for (uint32_t operand = 1; operand < chain->NumInOperands(); ++operand) {
@@ -432,13 +419,12 @@ namespace MobileGL {
uint32_t stride = 0;
if (type->opcode() == spv::Op::OpTypeStruct) {
uint32_t member = 0;
if (!TryGetConstantIndex(context, indexId, &member) ||
member >= type->NumInOperands()) {
if (!TryGetConstantIndex(context, indexId, &member) || member >= type->NumInOperands()) {
return false;
}
uint32_t offset = 0;
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member,
spv::Decoration::Offset, &offset) ||
if (!TryGetMemberDecorationLiteral(context, cursor.typeId, member, spv::Decoration::Offset,
&offset) ||
offset % kWordBytes != 0) {
return false;
}
@@ -466,8 +452,8 @@ namespace MobileGL {
cursor.matrixStride = 0;
break;
case spv::Op::OpTypeVector:
stride = ScalarByteSize(
context->get_def_use_mgr()->GetDef(type->GetSingleWordInOperand(0)));
stride =
ScalarByteSize(context->get_def_use_mgr()->GetDef(type->GetSingleWordInOperand(0)));
cursor.typeId = type->GetSingleWordInOperand(0);
cursor.matrixStride = 0;
break;
@@ -508,8 +494,7 @@ namespace MobileGL {
m_floatTypeId(floatTypeId) {}
uint32_t UintConstant(uint32_t value) {
const spvtools::opt::analysis::Type* type =
m_context->get_type_mgr()->GetType(m_uintTypeId);
const spvtools::opt::analysis::Type* type = m_context->get_type_mgr()->GetType(m_uintTypeId);
const spvtools::opt::analysis::Constant* constant =
m_context->get_constant_mgr()->GetConstant(type, {value});
return m_context->get_constant_mgr()->GetDefiningInstruction(constant)->result_id();
@@ -523,22 +508,19 @@ namespace MobileGL {
for (const auto& [indexId, wordsPerStep] : plan.dynamicTerms) {
uint32_t term = AsUint(builder, indexId);
if (wordsPerStep != 1u) {
term = Binary(builder, spv::Op::OpIMul, m_uintTypeId, term,
UintConstant(wordsPerStep));
term = Binary(builder, spv::Op::OpIMul, m_uintTypeId, term, UintConstant(wordsPerStep));
}
total = total == 0 ? term
: Binary(builder, spv::Op::OpIAdd, m_uintTypeId, total, term);
total = total == 0 ? term : Binary(builder, spv::Op::OpIAdd, m_uintTypeId, total, term);
}
if (total == 0) return UintConstant(plan.constantWords);
if (plan.constantWords == 0) return total;
return Binary(builder, spv::Op::OpIAdd, m_uintTypeId, total,
UintConstant(plan.constantWords));
return Binary(builder, spv::Op::OpIAdd, m_uintTypeId, total, UintConstant(plan.constantWords));
}
// Rebuilds the value an OpLoad of `cursor` would have produced, out of the
// words that live at `baseWordId + relWords`.
uint32_t BuildValue(InstructionBuilder& builder, const Access& access,
const TypeCursor& cursor, uint32_t relWords) {
uint32_t BuildValue(InstructionBuilder& builder, const Access& access, const TypeCursor& cursor,
uint32_t relWords) {
const Instruction* type = m_context->get_def_use_mgr()->GetDef(cursor.typeId);
switch (type->opcode()) {
case spv::Op::OpTypeInt:
@@ -550,9 +532,7 @@ namespace MobileGL {
.AddUnaryOp(m_floatTypeId, spv::Op::OpBitcast,
NarrowDoubleBits(builder, lo, hi))
->result_id();
return builder
.AddUnaryOp(cursor.typeId, spv::Op::OpFConvert, narrowed)
->result_id();
return builder.AddUnaryOp(cursor.typeId, spv::Op::OpFConvert, narrowed)->result_id();
}
const uint32_t word = LoadWord(builder, access, relWords);
if (cursor.typeId == m_uintTypeId) return word;
@@ -582,8 +562,7 @@ namespace MobileGL {
TypeCursor element = cursor;
element.typeId = type->GetSingleWordInOperand(0);
uint32_t stride = 0;
TryGetDecorationLiteral(m_context, cursor.typeId, spv::Decoration::ArrayStride,
&stride);
TryGetDecorationLiteral(m_context, cursor.typeId, spv::Decoration::ArrayStride, &stride);
const uint32_t count = m_context->get_constant_mgr()
->FindDeclaredConstant(type->GetSingleWordInOperand(1))
->AsIntConstant()
@@ -599,8 +578,8 @@ namespace MobileGL {
std::vector<uint32_t> parts;
for (uint32_t member = 0; member < type->NumInOperands(); ++member) {
uint32_t offset = 0;
TryGetMemberDecorationLiteral(m_context, cursor.typeId, member,
spv::Decoration::Offset, &offset);
TryGetMemberDecorationLiteral(m_context, cursor.typeId, member, spv::Decoration::Offset,
&offset);
parts.push_back(BuildValue(builder, access, MemberCursor(m_context, type, member),
relWords + offset / kWordBytes));
}
@@ -614,9 +593,8 @@ namespace MobileGL {
// The mirror image: writes `valueId` into the words at
// `baseWordId + relWords`. `path` is the composite-extract index list that
// reaches the part being written, empty at the root.
void StoreValue(InstructionBuilder& builder, const Access& access,
const TypeCursor& cursor, uint32_t relWords, uint32_t rootValueId,
std::vector<uint32_t>& path) {
void StoreValue(InstructionBuilder& builder, const Access& access, const TypeCursor& cursor,
uint32_t relWords, uint32_t rootValueId, std::vector<uint32_t>& path) {
const Instruction* type = m_context->get_def_use_mgr()->GetDef(cursor.typeId);
switch (type->opcode()) {
case spv::Op::OpTypeInt:
@@ -667,16 +645,15 @@ namespace MobileGL {
TypeCursor element = cursor;
element.typeId = type->GetSingleWordInOperand(0);
uint32_t stride = 0;
TryGetDecorationLiteral(m_context, cursor.typeId, spv::Decoration::ArrayStride,
&stride);
TryGetDecorationLiteral(m_context, cursor.typeId, spv::Decoration::ArrayStride, &stride);
const uint32_t count = m_context->get_constant_mgr()
->FindDeclaredConstant(type->GetSingleWordInOperand(1))
->AsIntConstant()
->GetU32BitValue();
for (uint32_t i = 0; i < count; ++i) {
path.push_back(i);
StoreValue(builder, access, element, relWords + i * (stride / kWordBytes),
rootValueId, path);
StoreValue(builder, access, element, relWords + i * (stride / kWordBytes), rootValueId,
path);
path.pop_back();
}
return;
@@ -684,8 +661,8 @@ namespace MobileGL {
case spv::Op::OpTypeStruct: {
for (uint32_t member = 0; member < type->NumInOperands(); ++member) {
uint32_t offset = 0;
TryGetMemberDecorationLiteral(m_context, cursor.typeId, member,
spv::Decoration::Offset, &offset);
TryGetMemberDecorationLiteral(m_context, cursor.typeId, member, spv::Decoration::Offset,
&offset);
path.push_back(member);
StoreValue(builder, access, MemberCursor(m_context, type, member),
relWords + offset / kWordBytes, rootValueId, path);
@@ -724,8 +701,7 @@ namespace MobileGL {
count = Select(builder, tooShort, UintConstant(0), past);
}
if (strideWords != 1) {
count = Binary(builder, spv::Op::OpUDiv, m_uintTypeId, count,
UintConstant(strideWords));
count = Binary(builder, spv::Op::OpUDiv, m_uintTypeId, count, UintConstant(strideWords));
}
m_context->ReplaceAllUsesWith(arrayLength->result_id(), count);
m_context->KillInst(arrayLength);
@@ -733,8 +709,7 @@ namespace MobileGL {
private:
uint32_t ComponentWords(uint32_t componentTypeId) {
return ScalarByteSize(m_context->get_def_use_mgr()->GetDef(componentTypeId)) /
kWordBytes;
return ScalarByteSize(m_context->get_def_use_mgr()->GetDef(componentTypeId)) / kWordBytes;
}
uint32_t Binary(InstructionBuilder& builder, spv::Op opcode, uint32_t typeId, uint32_t a,
@@ -744,8 +719,7 @@ namespace MobileGL {
uint32_t Select(InstructionBuilder& builder, uint32_t condition, uint32_t whenTrue,
uint32_t whenFalse) {
return builder
.AddTernaryOp(m_uintTypeId, spv::Op::OpSelect, condition, whenTrue, whenFalse)
return builder.AddTernaryOp(m_uintTypeId, spv::Op::OpSelect, condition, whenTrue, whenFalse)
->result_id();
}
@@ -763,16 +737,16 @@ namespace MobileGL {
uint32_t LoadWord(InstructionBuilder& builder, const Access& access, uint32_t words) {
const uint32_t indexId = Offset(builder, access.baseWordId, words);
Instruction* pointer = builder.AddAccessChain(
access.wordPointerTypeId, access.variableId, {access.memberZeroId, indexId});
Instruction* pointer = builder.AddAccessChain(access.wordPointerTypeId, access.variableId,
{access.memberZeroId, indexId});
return builder.AddLoad(m_uintTypeId, pointer->result_id())->result_id();
}
void StoreWord(InstructionBuilder& builder, const Access& access, uint32_t words,
uint32_t valueId) {
const uint32_t indexId = Offset(builder, access.baseWordId, words);
Instruction* pointer = builder.AddAccessChain(
access.wordPointerTypeId, access.variableId, {access.memberZeroId, indexId});
Instruction* pointer = builder.AddAccessChain(access.wordPointerTypeId, access.variableId,
{access.memberZeroId, indexId});
builder.AddStore(pointer->result_id(), valueId);
}
@@ -787,20 +761,20 @@ namespace MobileGL {
// mantissa bits binary32 cannot hold. Straight-line by construction: every
// case is an OpSelect, so this needs no control flow and never splits a block.
uint32_t NarrowDoubleBits(InstructionBuilder& builder, uint32_t lo, uint32_t hi) {
const uint32_t sign = Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, hi,
UintConstant(0x80000000u));
const uint32_t exponent = Binary(
builder, spv::Op::OpBitwiseAnd, m_uintTypeId,
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, hi, UintConstant(20)),
UintConstant(0x7FFu));
const uint32_t significandHigh = Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId,
hi, UintConstant(0xFFFFFu));
const uint32_t sign =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, hi, UintConstant(0x80000000u));
const uint32_t exponent =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId,
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, hi, UintConstant(20)),
UintConstant(0x7FFu));
const uint32_t significandHigh =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, hi, UintConstant(0xFFFFFu));
// The 23 bits binary32 keeps: 20 from the high word, 3 from the low one.
const uint32_t significand = Binary(
builder, spv::Op::OpBitwiseOr, m_uintTypeId,
Binary(builder, spv::Op::OpShiftLeftLogical, m_uintTypeId, significandHigh,
UintConstant(3)),
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, lo, UintConstant(29)));
const uint32_t significand =
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId,
Binary(builder, spv::Op::OpShiftLeftLogical, m_uintTypeId, significandHigh,
UintConstant(3)),
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, lo, UintConstant(29)));
// 1023 - 127 = 896, so the binary32 exponent field is `exponent - 896` and
// every bound on it is an unsigned comparison against that bias.
@@ -808,25 +782,21 @@ namespace MobileGL {
builder, spv::Op::OpBitwiseOr, m_uintTypeId, sign,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId,
Binary(builder, spv::Op::OpShiftLeftLogical, m_uintTypeId,
Binary(builder, spv::Op::OpISub, m_uintTypeId, exponent,
UintConstant(896)),
Binary(builder, spv::Op::OpISub, m_uintTypeId, exponent, UintConstant(896)),
UintConstant(23)),
significand));
const uint32_t infinityBits = Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, sign,
UintConstant(0x7F800000u));
const uint32_t infinityBits =
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, sign, UintConstant(0x7F800000u));
// A quiet NaN that stays one even when every significant bit sat in the 29
// low bits binary32 discards.
const uint32_t nanBits =
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, infinityBits,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId,
UintConstant(0x400000u), significand));
const uint32_t nanBits = Binary(
builder, spv::Op::OpBitwiseOr, m_uintTypeId, infinityBits,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, UintConstant(0x400000u), significand));
const uint32_t significandIsZero =
Binary(builder, spv::Op::OpIEqual, m_boolTypeId,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, significandHigh, lo),
UintConstant(0));
const uint32_t maxExponentBits =
Select(builder, significandIsZero, infinityBits, nanBits);
const uint32_t significandIsZero = Binary(
builder, spv::Op::OpIEqual, m_boolTypeId,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, significandHigh, lo), UintConstant(0));
const uint32_t maxExponentBits = Select(builder, significandIsZero, infinityBits, nanBits);
const uint32_t isZeroExponent =
Binary(builder, spv::Op::OpIEqual, m_boolTypeId, exponent, UintConstant(0));
@@ -834,10 +804,10 @@ namespace MobileGL {
Binary(builder, spv::Op::OpIEqual, m_boolTypeId, exponent, UintConstant(0x7FFu));
// <= 896 is every magnitude binary32 could hold only as a subnormal;
// >= 1151 is every one it cannot hold at all.
const uint32_t underflows = Binary(builder, spv::Op::OpULessThanEqual, m_boolTypeId,
exponent, UintConstant(896));
const uint32_t overflows = Binary(builder, spv::Op::OpUGreaterThanEqual, m_boolTypeId,
exponent, UintConstant(1151));
const uint32_t underflows =
Binary(builder, spv::Op::OpULessThanEqual, m_boolTypeId, exponent, UintConstant(896));
const uint32_t overflows =
Binary(builder, spv::Op::OpUGreaterThanEqual, m_boolTypeId, exponent, UintConstant(1151));
uint32_t bits = Select(builder, overflows, infinityBits, normalBits);
bits = Select(builder, underflows, sign, bits);
@@ -846,27 +816,25 @@ namespace MobileGL {
}
// The inverse: a binary32 bit pattern -> the binary64 word pair for it.
void WidenFloatBits(InstructionBuilder& builder, uint32_t bits, uint32_t* lo,
uint32_t* hi) {
const uint32_t sign = Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, bits,
UintConstant(0x80000000u));
const uint32_t exponent = Binary(
builder, spv::Op::OpBitwiseAnd, m_uintTypeId,
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, bits, UintConstant(23)),
UintConstant(0xFFu));
const uint32_t significand = Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, bits,
UintConstant(0x7FFFFFu));
const uint32_t significandHigh = Binary(builder, spv::Op::OpShiftRightLogical,
m_uintTypeId, significand, UintConstant(3));
const uint32_t significandLow = Binary(builder, spv::Op::OpShiftLeftLogical,
m_uintTypeId, significand, UintConstant(29));
void WidenFloatBits(InstructionBuilder& builder, uint32_t bits, uint32_t* lo, uint32_t* hi) {
const uint32_t sign =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, bits, UintConstant(0x80000000u));
const uint32_t exponent =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId,
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, bits, UintConstant(23)),
UintConstant(0xFFu));
const uint32_t significand =
Binary(builder, spv::Op::OpBitwiseAnd, m_uintTypeId, bits, UintConstant(0x7FFFFFu));
const uint32_t significandHigh =
Binary(builder, spv::Op::OpShiftRightLogical, m_uintTypeId, significand, UintConstant(3));
const uint32_t significandLow =
Binary(builder, spv::Op::OpShiftLeftLogical, m_uintTypeId, significand, UintConstant(29));
const uint32_t normalHigh = Binary(
builder, spv::Op::OpBitwiseOr, m_uintTypeId, sign,
Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId,
Binary(builder, spv::Op::OpShiftLeftLogical, m_uintTypeId,
Binary(builder, spv::Op::OpIAdd, m_uintTypeId, exponent,
UintConstant(896)),
Binary(builder, spv::Op::OpIAdd, m_uintTypeId, exponent, UintConstant(896)),
UintConstant(20)),
significandHigh));
const uint32_t maxHigh = Binary(builder, spv::Op::OpBitwiseOr, m_uintTypeId, sign,
@@ -933,8 +901,8 @@ namespace MobileGL {
// NOTHING IS WRITTEN until every reason to decline has been ruled out, so a block
// this returns 0 for leaves the module as it found it - which is what lets
// Process() truthfully report SuccessWithoutChange for a module of only those.
uint32_t CreateWordArrayTypeBefore(IRContext* context, Instruction* structType,
uint32_t uintTypeId, uint32_t length, bool openEnded) {
uint32_t CreateWordArrayTypeBefore(IRContext* context, Instruction* structType, uint32_t uintTypeId,
uint32_t length, bool openEnded) {
Instruction* uintType = context->get_def_use_mgr()->GetDef(uintTypeId);
if (uintType == nullptr || uintType->opcode() != spv::Op::OpTypeInt) return 0;
const bool hoistUint = !DeclaredBefore(context, uintTypeId, structType->result_id());
@@ -954,8 +922,7 @@ namespace MobileGL {
if (position == context->types_values_end()) return 0;
// Created in front of the block when it is not there yet, so the only way
// this declines is a constant the module already declares after it.
Instruction* lengthInst =
constantMgr->GetDefiningInstruction(lengthConstant, 0, &position);
Instruction* lengthInst = constantMgr->GetDefiningInstruction(lengthConstant, 0, &position);
if (lengthInst == nullptr) return 0;
if (!DeclaredBefore(context, lengthInst->result_id(), structType->result_id())) return 0;
lengthConstantId = lengthInst->result_id();
@@ -966,14 +933,13 @@ namespace MobileGL {
if (hoistUint) uintType->InsertBefore(structType);
std::unique_ptr<Instruction> arrayType =
openEnded
? MakeUnique<Instruction>(
context, spv::Op::OpTypeRuntimeArray, 0, arrayTypeId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {uintTypeId}}})
: MakeUnique<Instruction>(
context, spv::Op::OpTypeArray, 0, arrayTypeId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {uintTypeId}},
{SPV_OPERAND_TYPE_ID, {lengthConstantId}}});
openEnded ? MakeUnique<Instruction>(
context, spv::Op::OpTypeRuntimeArray, 0, arrayTypeId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {uintTypeId}}})
: MakeUnique<Instruction>(
context, spv::Op::OpTypeArray, 0, arrayTypeId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {uintTypeId}},
{SPV_OPERAND_TYPE_ID, {lengthConstantId}}});
Instruction* inserted = structType->InsertBefore(std::move(arrayType));
context->AnalyzeDefUse(inserted);
context->get_decoration_mgr()->AddDecorationVal(
@@ -987,12 +953,10 @@ namespace MobileGL {
// taken only from ALL of them, because each is a promise the shader would break
// if one member never made it.
std::vector<spv::Decoration> SurvivingAccessQualifiers(IRContext* context, uint32_t structId,
uint32_t memberCount) {
static constexpr spv::Decoration kAny[] = {spv::Decoration::Coherent,
spv::Decoration::Volatile};
static constexpr spv::Decoration kAll[] = {spv::Decoration::Restrict,
spv::Decoration::NonWritable,
spv::Decoration::NonReadable};
uint32_t memberCount) {
static constexpr spv::Decoration kAny[] = {spv::Decoration::Coherent, spv::Decoration::Volatile};
static constexpr spv::Decoration kAll[] = {spv::Decoration::Restrict, spv::Decoration::NonWritable,
spv::Decoration::NonReadable};
std::vector<spv::Decoration> surviving;
for (const spv::Decoration kind : kAny) {
for (uint32_t member = 0; member < memberCount; ++member) {
@@ -1017,8 +981,7 @@ namespace MobileGL {
// different thing entirely from the one that used to be member 0.
void StripMemberAnnotations(IRContext* context, uint32_t structId) {
std::vector<Instruction*> doomed;
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structId, false)) {
if (decoration->opcode() == spv::Op::OpMemberDecorate) doomed.push_back(decoration);
}
for (Instruction& debug : context->module()->debugs2()) {
@@ -1026,7 +989,8 @@ namespace MobileGL {
if (debug.GetSingleWordInOperand(0) != structId) continue;
doomed.push_back(&debug);
}
for (Instruction* inst : doomed) context->KillInst(inst);
for (Instruction* inst : doomed)
context->KillInst(inst);
}
// Plans every shader storage block in the module that holds a 64-bit float and
@@ -1045,8 +1009,7 @@ namespace MobileGL {
const uint32_t storageClass = inst.GetSingleWordInOperand(0);
const bool isStorageBufferClass =
storageClass == static_cast<uint32_t>(spv::StorageClass::StorageBuffer);
const bool isUniformClass =
storageClass == static_cast<uint32_t>(spv::StorageClass::Uniform);
const bool isUniformClass = storageClass == static_cast<uint32_t>(spv::StorageClass::Uniform);
if (!isStorageBufferClass && !isUniformClass) continue;
Instruction* pointerType = defUseMgr->GetDef(inst.type_id());
@@ -1250,8 +1213,8 @@ namespace MobileGL {
bool touched = false;
for (BlockPlan& plan : plans) {
const uint32_t structId = plan.structType->result_id();
const uint32_t arrayTypeId = CreateWordArrayTypeBefore(
irContext, plan.structType, uintTypeId, plan.wordCount, plan.openEnded);
const uint32_t arrayTypeId = CreateWordArrayTypeBefore(irContext, plan.structType, uintTypeId,
plan.wordCount, plan.openEnded);
if (arrayTypeId == 0) {
// Nothing was written for it, so the module is still the one that came in.
MGLOG_D("[spirv] storage block %%%u: no legal place for the flattened word array; "
@@ -1283,8 +1246,8 @@ namespace MobileGL {
for (Instruction* store : chainPlan.stores) {
InstructionBuilder builder(irContext, store, kPreservedAnalyses);
std::vector<uint32_t> path;
emitter.StoreValue(builder, access, chainPlan.pointee, 0,
store->GetSingleWordInOperand(1), path);
emitter.StoreValue(builder, access, chainPlan.pointee, 0, store->GetSingleWordInOperand(1),
path);
irContext->KillInst(store);
}
irContext->KillInst(chainPlan.chain);
@@ -1293,8 +1256,8 @@ namespace MobileGL {
emitter.RewriteArrayLength(arrayLength, plan.wordCount, plan.tailStrideWords);
}
const std::vector<spv::Decoration> surviving = SurvivingAccessQualifiers(
irContext, structId, plan.structType->NumInOperands());
const std::vector<spv::Decoration> surviving =
SurvivingAccessQualifiers(irContext, structId, plan.structType->NumInOperands());
StripMemberAnnotations(irContext, structId);
plan.structType->SetInOperands({{SPV_OPERAND_TYPE_ID, {arrayTypeId}}});
irContext->UpdateDefUse(plan.structType);
@@ -1304,10 +1267,9 @@ namespace MobileGL {
// AddMemberDecoration always carries a literal value; these have none, so
// the instruction has to be spelled out.
irContext->get_decoration_mgr()->AddDecoration(
spv::Op::OpMemberDecorate,
{{SPV_OPERAND_TYPE_ID, {structId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(kind)}}});
spv::Op::OpMemberDecorate, {{SPV_OPERAND_TYPE_ID, {structId}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(kind)}}});
}
modified = true;
if (plan.openEnded) {
@@ -1329,8 +1291,7 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken
FlattenFloat64StorageBlockPass::CreateFlattenFloat64StorageBlockPass() {
spvtools::Optimizer::PassToken FlattenFloat64StorageBlockPass::CreateFlattenFloat64StorageBlockPass() {
return spvtools::Optimizer::PassToken(MakeUnique<FlattenFloat64StorageBlockPass>());
}
} // namespace ShaderTranspiler
@@ -22,8 +22,8 @@
#include <vector>
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
constexpr const char* kTargetTypeName = "DailyWeatherVariation";
@@ -186,10 +186,10 @@ namespace {
void AddLocationDecoration(IRContext* context, uint32_t id, uint32_t location) {
context->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
context, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {id}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {location}}}));
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {id}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {location}}}));
}
std::optional<uint32_t> GetVariableDecorationLiteral(IRContext* context, uint32_t id, spv::Decoration kind) {
@@ -209,7 +209,8 @@ namespace {
std::optional<uint32_t> GetMemberDecorationLiteral(IRContext* context, uint32_t structTypeId, uint32_t memberIndex,
spv::Decoration kind) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structTypeId, false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->GetSingleWordInOperand(1) != memberIndex ||
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->GetSingleWordInOperand(1) != memberIndex ||
static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(2)) != kind ||
decoration->NumInOperands() < 4) {
continue;
@@ -239,8 +240,10 @@ namespace {
void CloneMemberDecorations(IRContext* context, const Instruction* structType, uint32_t memberIndex,
Instruction* target) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(structType->result_id(), false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate || decoration->GetSingleWordInOperand(1) != memberIndex) {
for (Instruction* decoration :
context->get_decoration_mgr()->GetDecorationsFor(structType->result_id(), false)) {
if (decoration->opcode() != spv::Op::OpMemberDecorate ||
decoration->GetSingleWordInOperand(1) != memberIndex) {
continue;
}
@@ -304,7 +307,8 @@ namespace {
}
replacementChain->UpdateDebugInfoFrom(accessChain);
auto insertPoint = spvtools::opt::BasicBlock::iterator(accessChain).InsertBefore(std::move(replacementChain));
auto insertPoint =
spvtools::opt::BasicBlock::iterator(accessChain).InsertBefore(std::move(replacementChain));
context->AnalyzeDefUse(&*insertPoint);
context->set_instr_block(&*insertPoint, context->get_instr_block(accessChain));
context->ReplaceAllUsesWith(accessChain->result_id(), replacementId);
@@ -490,14 +494,15 @@ namespace MobileGL {
uint32_t locationCursor =
GetVariableDecorationLiteral(irContext, targetVariable->result_id(), spv::Decoration::Location)
.value_or(0u);
for (uint32_t memberIndex = 0; memberIndex < static_cast<uint32_t>(kFlattenedMembers.size()); ++memberIndex) {
const uint32_t location = GetMemberDecorationLiteral(irContext, targetStructType->result_id(), memberIndex,
spv::Decoration::Location)
for (uint32_t memberIndex = 0; memberIndex < static_cast<uint32_t>(kFlattenedMembers.size());
++memberIndex) {
const uint32_t location = GetMemberDecorationLiteral(irContext, targetStructType->result_id(),
memberIndex, spv::Decoration::Location)
.value_or(locationCursor);
Instruction* const memberVar = CreateFlattenedVariable(
irContext, defUseMgr, typeMgr, targetVariable, targetStructType, memberIndex, location);
replacements.push_back({memberIndex, targetStructType->GetSingleWordInOperand(memberIndex), location,
memberVar});
replacements.push_back(
{memberIndex, targetStructType->GetSingleWordInOperand(memberIndex), location, memberVar});
locationCursor = location + kFlattenedMembers[memberIndex].locationSpan;
}
@@ -15,8 +15,10 @@
namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
// This is a custom SPIR-V optimization pass that flattens the "DailyWeatherVariation" interface struct into separate variables.
// This is actually a workaround for Adreno drivers that fail to work with struct as an interface block. Mali drivers don't have this issue, but we want to keep the shader code consistent across platforms.
// This is a custom SPIR-V optimization pass that flattens the "DailyWeatherVariation" interface struct into
// separate variables. This is actually a workaround for Adreno drivers that fail to work with struct as an
// interface block. Mali drivers don't have this issue, but we want to keep the shader code consistent
// across platforms.
class FlattenInterfaceStructPass : public spvtools::opt::Pass {
public:
const char* name() const override { return "flatten-interface-struct"; }
@@ -25,8 +25,8 @@ namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
namespace analysis = spvtools::opt::analysis;
@@ -83,8 +83,7 @@ namespace MobileGL {
// place it", which declines the whole block rather than guessing.
Uint32 LocationSpan(const analysis::Type* type) {
if (type == nullptr) return 0;
if (type->AsFloat() != nullptr || type->AsInteger() != nullptr ||
type->AsBool() != nullptr) {
if (type->AsFloat() != nullptr || type->AsInteger() != nullptr || type->AsBool() != nullptr) {
return 1u;
}
if (const auto* vector = type->AsVector()) {
@@ -205,8 +204,7 @@ namespace MobileGL {
// publish once, at the end, which is silently the wrong data rather than a
// failure - so those stages keep their blocks and the capture keeps its
// spelling.
const auto executionModel =
static_cast<spv::ExecutionModel>(entryPoint->GetSingleWordInOperand(0));
const auto executionModel = static_cast<spv::ExecutionModel>(entryPoint->GetSingleWordInOperand(0));
if (executionModel == spv::ExecutionModel::Geometry ||
executionModel == spv::ExecutionModel::TessellationControl) {
MGLOG_D("FlattenXfbInterfaceBlocksPass: execution model %u publishes outputs outside "
@@ -240,10 +238,8 @@ namespace MobileGL {
for (Instruction& inst : irContext->types_values()) {
if (inst.opcode() != spv::Op::OpVariable) continue;
const auto storageClass =
static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input &&
storageClass != spv::StorageClass::Output) {
const auto storageClass = static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input && storageClass != spv::StorageClass::Output) {
continue;
}
Instruction* pointerType = defUseMgr->GetDef(inst.type_id());
@@ -254,8 +250,7 @@ namespace MobileGL {
if (structType == nullptr) continue;
const String blockName = FindName(*irContext, pointeeTypeId);
if (blockName.empty() ||
m_blockNames.find(blockName) == m_blockNames.end()) {
if (blockName.empty() || m_blockNames.find(blockName) == m_blockNames.end()) {
continue;
}
@@ -266,8 +261,7 @@ namespace MobileGL {
target.blockName = blockName;
Uint32 blockLocation = 0;
const Bool hasBlockLocation =
FindLocationDecoration(*irContext, inst.result_id(), blockLocation);
const Bool hasBlockLocation = FindLocationDecoration(*irContext, inst.result_id(), blockLocation);
Bool usable = true;
for (auto& annotation : irContext->annotations()) {
@@ -296,8 +290,7 @@ namespace MobileGL {
break;
}
Uint32 memberLocation = 0;
if (FindMemberLocationDecoration(*irContext, pointeeTypeId, memberIndex,
memberLocation)) {
if (FindMemberLocationDecoration(*irContext, pointeeTypeId, memberIndex, memberLocation)) {
member.location = memberLocation;
member.hasLocation = true;
} else if (hasBlockLocation) {
@@ -337,7 +330,8 @@ namespace MobileGL {
{
auto& probeBlock = *entryFunction->begin();
auto probe = probeBlock.begin();
while (probe != probeBlock.end() && probe->opcode() == spv::Op::OpVariable) ++probe;
while (probe != probeBlock.end() && probe->opcode() == spv::Op::OpVariable)
++probe;
if (probe == probeBlock.end()) return Status::SuccessWithoutChange;
}
@@ -391,8 +385,7 @@ namespace MobileGL {
target.privatePointerTypeId =
typeMgr->FindPointerToType(target.structTypeId, spv::StorageClass::Private);
for (auto& member : target.members) {
member.interfacePointerTypeId =
typeMgr->FindPointerToType(member.typeId, target.storageClass);
member.interfacePointerTypeId = typeMgr->FindPointerToType(member.typeId, target.storageClass);
member.privatePointerTypeId =
typeMgr->FindPointerToType(member.typeId, spv::StorageClass::Private);
}
@@ -415,8 +408,7 @@ namespace MobileGL {
irContext->AddGlobalValue(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVariable, member.interfacePointerTypeId, member.variableId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<Uint32>(target.storageClass)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<Uint32>(target.storageClass)}}}));
// The name IS the contract: it is what the rewritten capture request
// asks the driver for, and what makes the producer and consumer of a
@@ -425,18 +417,16 @@ namespace MobileGL {
std::vector<Operand> nameOperands;
nameOperands.push_back({SPV_OPERAND_TYPE_ID, {member.variableId}});
nameOperands.push_back(
{SPV_OPERAND_TYPE_LITERAL_STRING,
spvtools::utils::MakeVector(flatName)});
newNames.push_back(spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpName, 0, 0,
nameOperands));
{SPV_OPERAND_TYPE_LITERAL_STRING, spvtools::utils::MakeVector(flatName)});
newNames.push_back(
spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpName, 0, 0, nameOperands));
if (member.hasLocation) {
newDecorations.push_back(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {member.variableId}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<Uint32>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<Uint32>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {member.location}}}));
}
}
@@ -448,8 +438,7 @@ namespace MobileGL {
if (annotation.GetSingleWordInOperand(0) != target.structTypeId) continue;
const Uint32 memberIndex = annotation.GetSingleWordInOperand(1);
if (memberIndex >= target.members.size()) continue;
const auto decoration =
static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(2));
const auto decoration = static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(2));
if (!IsMemberDecorationToCarry(decoration)) continue;
std::vector<Operand> operands;
operands.push_back({SPV_OPERAND_TYPE_ID, {target.members[memberIndex].variableId}});
@@ -467,8 +456,7 @@ namespace MobileGL {
// A struct type shared by two flattened variables would otherwise have its
// Block decoration killed twice - and the second kill is a use-after-free,
// not a no-op.
const Bool structTypeAlreadyStripped =
strippedBlockStructTypes.count(target.structTypeId) != 0;
const Bool structTypeAlreadyStripped = strippedBlockStructTypes.count(target.structTypeId) != 0;
strippedBlockStructTypes.insert(target.structTypeId);
for (auto& annotation : irContext->annotations()) {
if (annotation.opcode() != spv::Op::OpDecorate) continue;
@@ -593,8 +581,7 @@ namespace MobileGL {
const Uint32 memberPointerId = irContext->TakeNextId();
const Uint32 loadedId = irContext->TakeNextId();
returnInst->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpAccessChain, member.privatePointerTypeId,
memberPointerId,
irContext, spv::Op::OpAccessChain, member.privatePointerTypeId, memberPointerId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {target.variable->result_id()}},
{SPV_OPERAND_TYPE_ID, {memberIndexConstants[memberIndex]}}}));
@@ -618,8 +605,8 @@ namespace MobileGL {
spv::StorageClass::Private) {
continue;
}
pointer->SetResultType(typeMgr->FindPointerToType(resultType->GetSingleWordInOperand(1),
spv::StorageClass::Private));
pointer->SetResultType(
typeMgr->FindPointerToType(resultType->GetSingleWordInOperand(1), spv::StorageClass::Private));
}
if (m_flattenedBlockNames != nullptr) {
@@ -48,8 +48,7 @@ namespace MobileGL {
// "StageData.attrib[0]". `flattenedBlockNames` receives the subset this pass
// actually rewrote, so the caller can rename exactly those capture requests
// and leave the rest spelled as the application wrote them.
FlattenXfbInterfaceBlocksPass(const std::set<String>& blockNames,
std::set<String>* flattenedBlockNames)
FlattenXfbInterfaceBlocksPass(const std::set<String>& blockNames, std::set<String>* flattenedBlockNames)
: m_blockNames(blockNames), m_flattenedBlockNames(flattenedBlockNames) {}
const char* name() const override { return "mobilegl-flatten-xfb-interface-blocks"; }
@@ -34,8 +34,7 @@ namespace MobileGL {
if (static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1)) !=
spv::Decoration::BuiltIn)
continue;
if (static_cast<spv::BuiltIn>(annotation.GetSingleWordInOperand(2)) !=
spv::BuiltIn::SampleMask)
if (static_cast<spv::BuiltIn>(annotation.GetSingleWordInOperand(2)) != spv::BuiltIn::SampleMask)
continue;
Instruction* variable = defUseMgr->GetDef(annotation.GetSingleWordInOperand(0));
if (variable != nullptr && variable->opcode() == spv::Op::OpVariable &&
@@ -116,7 +115,8 @@ namespace MobileGL {
const Instruction* allOnesInst =
allOnes ? constantMgr->GetDefiningInstruction(allOnes, elementTypeId) : nullptr;
if (elementTypeId == 0 || allOnesInst == nullptr) return Status::Failure;
for (Instruction* store : elementStores) store->SetInOperand(1, {allOnesInst->result_id()});
for (Instruction* store : elementStores)
store->SetInOperand(1, {allOnesInst->result_id()});
if (!arrayStores.empty()) {
// The array's length is a constant (the builtin is sized at link time); a
@@ -133,7 +133,8 @@ namespace MobileGL {
const Instruction* allOnesArrayInst =
allOnesArray ? constantMgr->GetDefiningInstruction(allOnesArray, arrayTypeId) : nullptr;
if (arrayTypeId == 0 || allOnesArrayInst == nullptr) return Status::Failure;
for (Instruction* store : arrayStores) store->SetInOperand(1, {allOnesArrayInst->result_id()});
for (Instruction* store : arrayStores)
store->SetInOperand(1, {allOnesArrayInst->result_id()});
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -114,8 +114,8 @@ namespace MobileGL {
// driver rejects.
bool IsConstantIndex(IRContext* context, uint32_t indexId) {
Instruction* def = context->get_def_use_mgr()->GetDef(indexId);
return def != nullptr && (def->opcode() == spv::Op::OpConstant ||
def->opcode() == spv::Op::OpConstantNull);
return def != nullptr &&
(def->opcode() == spv::Op::OpConstant || def->opcode() == spv::Op::OpConstantNull);
}
// Access chains that index a fragment output array with a non-constant.
@@ -172,9 +172,8 @@ namespace MobileGL {
operands.push_back(accessChain->GetInOperand(i));
}
}
return builder.AddInstruction(MakeUnique<Instruction>(context, accessChain->opcode(),
accessChain->type_id(),
context->TakeNextId(), operands));
return builder.AddInstruction(MakeUnique<Instruction>(
context, accessChain->opcode(), accessChain->type_id(), context->TakeNextId(), operands));
}
// The id of |element| as a constant of the same integer type as |indexId|.
@@ -195,10 +194,8 @@ namespace MobileGL {
if (indexDef == nullptr) {
return false;
}
const spvtools::opt::analysis::Type* type =
context->get_type_mgr()->GetType(indexDef->type_id());
const spvtools::opt::analysis::Integer* integer =
type != nullptr ? type->AsInteger() : nullptr;
const spvtools::opt::analysis::Type* type = context->get_type_mgr()->GetType(indexDef->type_id());
const spvtools::opt::analysis::Integer* integer = type != nullptr ? type->AsInteger() : nullptr;
return integer != nullptr && integer->width() == 32;
}
@@ -207,8 +204,8 @@ namespace MobileGL {
// vector of the same width - built by broadcasting the scalar comparison.
// Anything that is neither scalar nor vector (a matrix or struct element) is
// refused: pre-1.4 OpSelect cannot express it either.
bool TryGetSelectConditionType(IRContext* context, uint32_t resultTypeId,
uint32_t* conditionTypeId, uint32_t* dimension) {
bool TryGetSelectConditionType(IRContext* context, uint32_t resultTypeId, uint32_t* conditionTypeId,
uint32_t* dimension) {
auto* typeMgr = context->get_type_mgr();
const spvtools::opt::analysis::Type* resultType = typeMgr->GetType(resultTypeId);
if (resultType == nullptr) {
@@ -261,14 +258,12 @@ namespace MobileGL {
}
} // namespace
bool LegalizeFragmentOutputIndexPass::BinaryHasDynamicOutputIndexing(
const std::vector<uint32_t>& binary) {
bool LegalizeFragmentOutputIndexPass::BinaryHasDynamicOutputIndexing(const std::vector<uint32_t>& binary) {
if (binary.empty()) {
return false;
}
std::unique_ptr<IRContext> context = spvtools::BuildModule(
SPV_ENV_VULKAN_1_1,
[](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
SPV_ENV_VULKAN_1_1, [](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
binary.data(), binary.size());
if (!context) {
return false;
@@ -317,8 +312,7 @@ namespace MobileGL {
// cost proportional to the output array's length.
spvtools::opt::LoopDescriptor* loops = irContext->GetLoopDescriptor(function);
size_t nestIterations = 1;
for (spvtools::opt::Loop* loop = (*loops)[block->id()]; loop != nullptr;
loop = loop->GetParent()) {
for (spvtools::opt::Loop* loop = (*loops)[block->id()]; loop != nullptr; loop = loop->GetParent()) {
size_t iterations = 0;
if (!TryGetUnrollTripCount(loop, &iterations)) {
break;
@@ -340,8 +334,7 @@ namespace MobileGL {
static_cast<uint32_t>(spv::LoopControlMask::MaskNone)) {
break;
}
mergeInst->SetOperand(
2, {static_cast<uint32_t>(spv::LoopControlMask::Unroll)});
mergeInst->SetOperand(2, {static_cast<uint32_t>(spv::LoopControlMask::Unroll)});
nestIterations *= iterations;
modified = true;
}
@@ -518,8 +511,7 @@ namespace MobileGL {
irContext->set_instr_block(casePtr->GetLabelInst(), casePtr);
InstructionBuilder caseBuilder(
irContext, casePtr,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
irContext, casePtr, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
const uint32_t constantId = ConstantLikeIndex(irContext, indexId, element);
Instruction* elementChain =
CloneChainWithConstantIndex(caseBuilder, irContext, accessChain, constantId);
@@ -538,16 +530,15 @@ namespace MobileGL {
insertAfter = casePtr;
}
InstructionBuilder switchBuilder(
irContext, block, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder switchBuilder(irContext, block,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
switchBuilder.AddSwitch(indexId, mergeLabelId, targets, mergeLabelId);
if (irContext->get_def_use_mgr()->NumUsers(accessChain) == 0) {
irContext->KillInst(accessChain);
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
MGLOG_D("[spirv] fragment-output index: lowered a dynamic write to a %u-way switch",
arrayLength);
MGLOG_D("[spirv] fragment-output index: lowered a dynamic write to a %u-way switch", arrayLength);
return LoweringOutcome::Changed;
}
@@ -566,8 +557,8 @@ namespace MobileGL {
const uint32_t boolTypeId = irContext->get_type_mgr()->GetBoolTypeId();
const uint32_t indexId = accessChain->GetSingleWordInOperand(1);
InstructionBuilder builder(
irContext, load, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, load,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
uint32_t selectedId = 0;
for (uint32_t element = 0; element < arrayLength; ++element) {
@@ -582,16 +573,13 @@ namespace MobileGL {
continue;
}
Instruction* isElement =
builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
Instruction* isElement = builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
uint32_t conditionId = isElement->result_id();
if (dimension > 1) {
std::vector<uint32_t> components(dimension, conditionId);
conditionId = builder.AddCompositeConstruct(conditionTypeId, components)->result_id();
}
selectedId = builder
.AddSelect(load->type_id(), conditionId, elementLoad->result_id(),
selectedId)
selectedId = builder.AddSelect(load->type_id(), conditionId, elementLoad->result_id(), selectedId)
->result_id();
}
@@ -74,8 +74,7 @@ namespace MobileGL {
bool HasDecoration(IRContext* context, uint32_t id, spv::Decoration kind) {
for (Instruction* decoration : context->get_decoration_mgr()->GetDecorationsFor(id, false)) {
if (decoration->opcode() != spv::Op::OpDecorate ||
decoration->NumInOperands() < 2) {
if (decoration->opcode() != spv::Op::OpDecorate || decoration->NumInOperands() < 2) {
continue;
}
if (static_cast<spv::Decoration>(decoration->GetSingleWordInOperand(1)) == kind) {
@@ -110,8 +109,7 @@ namespace MobileGL {
if (inst.opcode() != spv::Op::OpVariable) {
continue;
}
const auto storageClass =
static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
const auto storageClass = static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::StorageBuffer &&
storageClass != spv::StorageClass::Uniform &&
storageClass != spv::StorageClass::UniformConstant) {
@@ -135,8 +133,7 @@ namespace MobileGL {
if (storageClass == spv::StorageClass::UniformConstant) {
// OpTypeImage <result> <sampled type> <dim> <depth> <arrayed> <ms>
// <sampled> <format>
if (elementType->opcode() != spv::Op::OpTypeImage ||
elementType->NumInOperands() < 6 ||
if (elementType->opcode() != spv::Op::OpTypeImage || elementType->NumInOperands() < 6 ||
elementType->GetSingleWordInOperand(5) != 2u) {
continue;
}
@@ -147,10 +144,8 @@ namespace MobileGL {
}
const bool isStorageBlock =
storageClass == spv::StorageClass::StorageBuffer
? HasDecoration(context, elementType->result_id(),
spv::Decoration::Block)
: HasDecoration(context, elementType->result_id(),
spv::Decoration::BufferBlock);
? HasDecoration(context, elementType->result_id(), spv::Decoration::Block)
: HasDecoration(context, elementType->result_id(), spv::Decoration::BufferBlock);
if (!isStorageBlock) {
continue;
}
@@ -173,8 +168,8 @@ namespace MobileGL {
// driver rejects.
bool IsConstantIndex(IRContext* context, uint32_t indexId) {
Instruction* def = context->get_def_use_mgr()->GetDef(indexId);
return def != nullptr && (def->opcode() == spv::Op::OpConstant ||
def->opcode() == spv::Op::OpConstantNull);
return def != nullptr &&
(def->opcode() == spv::Op::OpConstant || def->opcode() == spv::Op::OpConstantNull);
}
// Access chains that index an array of storage blocks or of images with a
@@ -185,8 +180,7 @@ namespace MobileGL {
// already inside one element.
std::vector<DynamicIndexUse> CollectDynamicIndexUses(IRContext* context) {
std::vector<DynamicIndexUse> uses;
const std::unordered_map<uint32_t, ResourceArray> resourceArrays =
CollectResourceArrays(context);
const std::unordered_map<uint32_t, ResourceArray> resourceArrays = CollectResourceArrays(context);
if (resourceArrays.empty()) {
return uses;
}
@@ -208,8 +202,7 @@ namespace MobileGL {
if (IsConstantIndex(context, inst.GetSingleWordInOperand(1))) {
continue;
}
uses.push_back(
{&inst, arrayIt->second.length, arrayIt->second.isImage});
uses.push_back({&inst, arrayIt->second.length, arrayIt->second.isImage});
}
}
}
@@ -230,9 +223,8 @@ namespace MobileGL {
operands.push_back(accessChain->GetInOperand(i));
}
}
return builder.AddInstruction(MakeUnique<Instruction>(context, accessChain->opcode(),
accessChain->type_id(),
context->TakeNextId(), operands));
return builder.AddInstruction(MakeUnique<Instruction>(
context, accessChain->opcode(), accessChain->type_id(), context->TakeNextId(), operands));
}
// The id of |element| as a constant of the same integer type as |indexId|.
@@ -253,10 +245,8 @@ namespace MobileGL {
if (indexDef == nullptr) {
return false;
}
const spvtools::opt::analysis::Type* type =
context->get_type_mgr()->GetType(indexDef->type_id());
const spvtools::opt::analysis::Integer* integer =
type != nullptr ? type->AsInteger() : nullptr;
const spvtools::opt::analysis::Type* type = context->get_type_mgr()->GetType(indexDef->type_id());
const spvtools::opt::analysis::Integer* integer = type != nullptr ? type->AsInteger() : nullptr;
return integer != nullptr && integer->width() == 32;
}
@@ -265,8 +255,8 @@ namespace MobileGL {
// vector of the same width - built by broadcasting the scalar comparison.
// Anything that is neither scalar nor vector (a matrix or struct element) is
// refused: pre-1.4 OpSelect cannot express it either.
bool TryGetSelectConditionType(IRContext* context, uint32_t resultTypeId,
uint32_t* conditionTypeId, uint32_t* dimension) {
bool TryGetSelectConditionType(IRContext* context, uint32_t resultTypeId, uint32_t* conditionTypeId,
uint32_t* dimension) {
auto* typeMgr = context->get_type_mgr();
const spvtools::opt::analysis::Type* resultType = typeMgr->GetType(resultTypeId);
if (resultType == nullptr) {
@@ -325,8 +315,7 @@ namespace MobileGL {
return false;
}
std::unique_ptr<IRContext> context = spvtools::BuildModule(
SPV_ENV_VULKAN_1_1,
[](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
SPV_ENV_VULKAN_1_1, [](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
binary.data(), binary.size());
if (!context) {
return false;
@@ -382,8 +371,7 @@ namespace MobileGL {
// the ARRAY LENGTH rather than to the trip counts.
spvtools::opt::LoopDescriptor* loops = irContext->GetLoopDescriptor(function);
size_t nestIterations = 1;
for (spvtools::opt::Loop* loop = (*loops)[block->id()]; loop != nullptr;
loop = loop->GetParent()) {
for (spvtools::opt::Loop* loop = (*loops)[block->id()]; loop != nullptr; loop = loop->GetParent()) {
size_t iterations = 0;
if (!TryGetUnrollTripCount(loop, &iterations)) {
break;
@@ -405,8 +393,7 @@ namespace MobileGL {
static_cast<uint32_t>(spv::LoopControlMask::MaskNone)) {
break;
}
mergeInst->SetOperand(
2, {static_cast<uint32_t>(spv::LoopControlMask::Unroll)});
mergeInst->SetOperand(2, {static_cast<uint32_t>(spv::LoopControlMask::Unroll)});
nestIterations *= iterations;
modified = true;
}
@@ -462,13 +449,11 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerOneChain(Instruction* accessChain, uint32_t arrayLength,
bool isImageArray) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerOneChain(
Instruction* accessChain, uint32_t arrayLength, bool isImageArray) {
auto* irContext = context();
if (arrayLength == 0 || arrayLength > kMaxLoweredArrayLength) {
MGLOG_D("[spirv] resource array index: array length %u is not lowerable",
arrayLength);
MGLOG_D("[spirv] resource array index: array length %u is not lowerable", arrayLength);
return LoweringOutcome::Declined;
}
if (!IsLowerableIndexType(irContext, accessChain->GetSingleWordInOperand(1))) {
@@ -541,9 +526,8 @@ namespace MobileGL {
// every path. An index outside [0, length) reaches the default target, which is
// the merge block: nothing is stored, which is what indexing a block array out of
// range already meant.
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerStore(Instruction* accessChain, uint32_t arrayLength,
Instruction* store) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerStore(
Instruction* accessChain, uint32_t arrayLength, Instruction* store) {
auto* irContext = context();
BasicBlock* block = irContext->get_instr_block(store);
if (block == nullptr) {
@@ -589,8 +573,7 @@ namespace MobileGL {
irContext->set_instr_block(casePtr->GetLabelInst(), casePtr);
InstructionBuilder caseBuilder(
irContext, casePtr,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
irContext, casePtr, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
const uint32_t constantId = ConstantLikeIndex(irContext, indexId, element);
Instruction* elementChain =
CloneChainWithConstantIndex(caseBuilder, irContext, accessChain, constantId);
@@ -609,16 +592,15 @@ namespace MobileGL {
insertAfter = casePtr;
}
InstructionBuilder switchBuilder(
irContext, block, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder switchBuilder(irContext, block,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
switchBuilder.AddSwitch(indexId, mergeLabelId, targets, mergeLabelId);
if (irContext->get_def_use_mgr()->NumUsers(accessChain) == 0) {
irContext->KillInst(accessChain);
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
MGLOG_D("[spirv] storage-block array index: lowered a dynamic write to a %u-way switch",
arrayLength);
MGLOG_D("[spirv] storage-block array index: lowered a dynamic write to a %u-way switch", arrayLength);
return LoweringOutcome::Changed;
}
@@ -626,9 +608,8 @@ namespace MobileGL {
// pick with OpSelect. Reading the elements the shader did not ask for is safe -
// every one of them is a storage block this stage already declares, and an ES
// driver bounds-checks a storage buffer read that lands outside what is bound.
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerLoad(Instruction* accessChain, uint32_t arrayLength,
Instruction* load) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerLoad(
Instruction* accessChain, uint32_t arrayLength, Instruction* load) {
auto* irContext = context();
uint32_t conditionTypeId = 0;
uint32_t dimension = 0;
@@ -639,8 +620,8 @@ namespace MobileGL {
const uint32_t boolTypeId = irContext->get_type_mgr()->GetBoolTypeId();
const uint32_t indexId = accessChain->GetSingleWordInOperand(1);
InstructionBuilder builder(
irContext, load, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, load,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
uint32_t selectedId = 0;
for (uint32_t element = 0; element < arrayLength; ++element) {
@@ -655,16 +636,13 @@ namespace MobileGL {
continue;
}
Instruction* isElement =
builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
Instruction* isElement = builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
uint32_t conditionId = isElement->result_id();
if (dimension > 1) {
std::vector<uint32_t> components(dimension, conditionId);
conditionId = builder.AddCompositeConstruct(conditionTypeId, components)->result_id();
}
selectedId = builder
.AddSelect(load->type_id(), conditionId, elementLoad->result_id(),
selectedId)
selectedId = builder.AddSelect(load->type_id(), conditionId, elementLoad->result_id(), selectedId)
->result_id();
}
@@ -682,8 +660,8 @@ namespace MobileGL {
// consume that object. So this resolves the chain one CONSUMER at a time - the
// round loop in LowerToConstantSwitch recollects after each - and refuses anything
// that is not a plain read or write of the loaded image.
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerImageChain(Instruction* accessChain, uint32_t arrayLength) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerImageChain(
Instruction* accessChain, uint32_t arrayLength) {
auto* irContext = context();
std::vector<Instruction*> loads;
@@ -774,8 +752,7 @@ namespace MobileGL {
// Drops |load| and |accessChain| once the rewrite above has taken their last user,
// in that order - the load is what uses the chain. Anything still using either is
// another consumer a later round will come back for.
void LegalizeResourceArrayIndexPass::KillImageChainIfDead(Instruction* accessChain,
Instruction* load) {
void LegalizeResourceArrayIndexPass::KillImageChainIfDead(Instruction* accessChain, Instruction* load) {
auto* irContext = context();
if (irContext->get_def_use_mgr()->NumUsers(load) == 0) {
irContext->KillInst(load);
@@ -791,9 +768,8 @@ namespace MobileGL {
// write still runs exactly once on every path, and an index outside [0, length)
// reaches the default target - the merge block - so nothing is written, which is
// what indexing an image array out of range already meant.
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerImageWrite(Instruction* accessChain, uint32_t arrayLength,
Instruction* load, Instruction* imageWrite) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerImageWrite(
Instruction* accessChain, uint32_t arrayLength, Instruction* load, Instruction* imageWrite) {
auto* irContext = context();
BasicBlock* block = irContext->get_instr_block(imageWrite);
if (block == nullptr) {
@@ -839,13 +815,11 @@ namespace MobileGL {
irContext->set_instr_block(casePtr->GetLabelInst(), casePtr);
InstructionBuilder caseBuilder(
irContext, casePtr,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
irContext, casePtr, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
const uint32_t constantId = ConstantLikeIndex(irContext, indexId, element);
Instruction* elementChain =
CloneChainWithConstantIndex(caseBuilder, irContext, accessChain, constantId);
Instruction* elementImage =
caseBuilder.AddLoad(imageTypeId, elementChain->result_id());
Instruction* elementImage = caseBuilder.AddLoad(imageTypeId, elementChain->result_id());
std::vector<Operand> writeOperands;
writeOperands.push_back({SPV_OPERAND_TYPE_ID, {elementImage->result_id()}});
@@ -860,14 +834,13 @@ namespace MobileGL {
insertAfter = casePtr;
}
InstructionBuilder switchBuilder(
irContext, block, IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder switchBuilder(irContext, block,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
switchBuilder.AddSwitch(indexId, mergeLabelId, targets, mergeLabelId);
KillImageChainIfDead(accessChain, load);
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
MGLOG_D("[spirv] image array index: lowered a dynamic imageStore to a %u-way switch",
arrayLength);
MGLOG_D("[spirv] image array index: lowered a dynamic imageStore to a %u-way switch", arrayLength);
return LoweringOutcome::Changed;
}
@@ -885,15 +858,12 @@ namespace MobileGL {
// an error, and never a fault - which the select then discards. A size query does not
// even touch memory. Contrast an imageAtomic*, which LowerImageChain refuses for
// exactly the opposite reason.
LegalizeResourceArrayIndexPass::LoweringOutcome
LegalizeResourceArrayIndexPass::LowerImageReadOrQuery(Instruction* accessChain,
uint32_t arrayLength, Instruction* load,
Instruction* consumer) {
LegalizeResourceArrayIndexPass::LoweringOutcome LegalizeResourceArrayIndexPass::LowerImageReadOrQuery(
Instruction* accessChain, uint32_t arrayLength, Instruction* load, Instruction* consumer) {
auto* irContext = context();
uint32_t conditionTypeId = 0;
uint32_t dimension = 0;
if (!TryGetSelectConditionType(irContext, consumer->type_id(), &conditionTypeId,
&dimension)) {
if (!TryGetSelectConditionType(irContext, consumer->type_id(), &conditionTypeId, &dimension)) {
MGLOG_D("[spirv] image array index: result type is not selectable, declining");
return LoweringOutcome::Declined;
}
@@ -905,9 +875,8 @@ namespace MobileGL {
tailOperands.push_back(consumer->GetInOperand(i));
}
InstructionBuilder builder(
irContext, consumer,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, consumer,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
uint32_t selectedId = 0;
for (uint32_t element = 0; element < arrayLength; ++element) {
@@ -921,9 +890,8 @@ namespace MobileGL {
for (const Operand& tailOperand : tailOperands) {
elementOperands.push_back(tailOperand);
}
Instruction* elementResult = builder.AddInstruction(
MakeUnique<Instruction>(irContext, consumer->opcode(), consumer->type_id(),
irContext->TakeNextId(), elementOperands));
Instruction* elementResult = builder.AddInstruction(MakeUnique<Instruction>(
irContext, consumer->opcode(), consumer->type_id(), irContext->TakeNextId(), elementOperands));
if (element == 0) {
// Element 0 is the else-arm of the whole ladder, so an out-of-range
// index reads it - an undefined element for an undefined index.
@@ -931,17 +899,15 @@ namespace MobileGL {
continue;
}
Instruction* isElement =
builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
Instruction* isElement = builder.AddBinaryOp(boolTypeId, spv::Op::OpIEqual, indexId, constantId);
uint32_t conditionId = isElement->result_id();
if (dimension > 1) {
std::vector<uint32_t> components(dimension, conditionId);
conditionId = builder.AddCompositeConstruct(conditionTypeId, components)->result_id();
}
selectedId = builder
.AddSelect(consumer->type_id(), conditionId,
elementResult->result_id(), selectedId)
->result_id();
selectedId =
builder.AddSelect(consumer->type_id(), conditionId, elementResult->result_id(), selectedId)
->result_id();
}
irContext->ReplaceAllUsesWith(consumer->result_id(), selectedId);
@@ -954,14 +920,12 @@ namespace MobileGL {
return LoweringOutcome::Changed;
}
spvtools::Optimizer::PassToken
LegalizeResourceArrayIndexPass::CreateMarkLoopsForUnrollPass() {
spvtools::Optimizer::PassToken LegalizeResourceArrayIndexPass::CreateMarkLoopsForUnrollPass() {
return spvtools::Optimizer::PassToken(
MakeUnique<LegalizeResourceArrayIndexPass>(Mode::MarkLoopsForUnroll));
}
spvtools::Optimizer::PassToken
LegalizeResourceArrayIndexPass::CreateLowerToConstantSwitchPass() {
spvtools::Optimizer::PassToken LegalizeResourceArrayIndexPass::CreateLowerToConstantSwitchPass() {
return spvtools::Optimizer::PassToken(
MakeUnique<LegalizeResourceArrayIndexPass>(Mode::LowerToConstantSwitch));
}
@@ -106,9 +106,8 @@ namespace MobileGL {
explicit LegalizeResourceArrayIndexPass(Mode mode) : m_mode(mode) {}
const char* name() const override {
return m_mode == Mode::MarkLoopsForUnroll
? "mobilegl-mark-resource-array-index-loops"
: "mobilegl-lower-resource-array-index";
return m_mode == Mode::MarkLoopsForUnroll ? "mobilegl-mark-resource-array-index-loops"
: "mobilegl-lower-resource-array-index";
}
Status Process() override;
@@ -141,13 +140,11 @@ namespace MobileGL {
LoweringOutcome LowerLoad(spvtools::opt::Instruction* accessChain, uint32_t arrayLength,
spvtools::opt::Instruction* load);
LoweringOutcome LowerImageChain(spvtools::opt::Instruction* accessChain, uint32_t arrayLength);
void KillImageChainIfDead(spvtools::opt::Instruction* accessChain,
spvtools::opt::Instruction* load);
void KillImageChainIfDead(spvtools::opt::Instruction* accessChain, spvtools::opt::Instruction* load);
LoweringOutcome LowerImageWrite(spvtools::opt::Instruction* accessChain, uint32_t arrayLength,
spvtools::opt::Instruction* load,
spvtools::opt::Instruction* imageWrite);
LoweringOutcome LowerImageReadOrQuery(spvtools::opt::Instruction* accessChain,
uint32_t arrayLength,
LoweringOutcome LowerImageReadOrQuery(spvtools::opt::Instruction* accessChain, uint32_t arrayLength,
spvtools::opt::Instruction* load,
spvtools::opt::Instruction* consumer);
@@ -204,8 +204,7 @@ namespace MobileGL {
if (!QueriesImageSize(instruction.opcode()) || instruction.NumInOperands() < 1) {
continue;
}
if (scope.Covers(
ResolveImageType(context.get(), instruction.GetSingleWordInOperand(0)))) {
if (scope.Covers(ResolveImageType(context.get(), instruction.GetSingleWordInOperand(0)))) {
traits.queriesImageSize = true;
return traits;
}
@@ -236,8 +235,7 @@ namespace MobileGL {
for (auto& block : function) {
for (auto& instruction : block) {
if (QueriesImageSize(instruction.opcode()) && instruction.NumInOperands() >= 1 &&
scope.Covers(
ResolveImageType(irContext, instruction.GetSingleWordInOperand(0)))) {
scope.Covers(ResolveImageType(irContext, instruction.GetSingleWordInOperand(0)))) {
return Status::SuccessWithoutChange;
}
}
@@ -284,36 +282,32 @@ namespace MobileGL {
if (arrayed && (coordinateVector == nullptr || coordinateVector->element_count() != 2)) {
return Status::Failure;
}
const auto* component =
arrayed ? coordinateVector->element_type() : coordinateType;
const auto* component = arrayed ? coordinateVector->element_type() : coordinateType;
const auto* componentInteger = component != nullptr ? component->AsInteger() : nullptr;
if (componentInteger == nullptr) return Status::Failure;
spvtools::opt::analysis::Vector widenedVector(component, arrayed ? 3 : 2);
const uint32_t widenedTypeId = typeMgr->GetTypeInstruction(&widenedVector);
const uint32_t intTypeId = typeMgr->GetTypeInstruction(component);
const uint32_t zeroId = componentInteger->IsSigned()
? constantMgr->GetSIntConstId(0)
: constantMgr->GetUIntConstId(0);
const uint32_t zeroId = componentInteger->IsSigned() ? constantMgr->GetSIntConstId(0)
: constantMgr->GetUIntConstId(0);
if (widenedTypeId == 0 || intTypeId == 0 || zeroId == 0) {
return Status::Failure;
}
InstructionBuilder builder(
irContext, &instruction,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, &instruction,
IRContext::kAnalysisDefUse |
IRContext::kAnalysisInstrToBlockMapping);
Instruction* widened = nullptr;
if (arrayed) {
Instruction* u =
builder.AddCompositeExtract(intTypeId, coordinateId, {0});
Instruction* layer =
builder.AddCompositeExtract(intTypeId, coordinateId, {1});
Instruction* u = builder.AddCompositeExtract(intTypeId, coordinateId, {0});
Instruction* layer = builder.AddCompositeExtract(intTypeId, coordinateId, {1});
if (u == nullptr || layer == nullptr) {
return Status::Failure;
}
widened = builder.AddCompositeConstruct(
widenedTypeId, {u->result_id(), zeroId, layer->result_id()});
widened = builder.AddCompositeConstruct(widenedTypeId,
{u->result_id(), zeroId, layer->result_id()});
} else {
widened = builder.AddCompositeConstruct(widenedTypeId, {coordinateId, zeroId});
}
@@ -46,8 +46,7 @@ namespace MobileGL {
bool Is1DSampledImageType(const Instruction* imageType) {
return imageType != nullptr && imageType->opcode() == spv::Op::OpTypeImage &&
imageType->NumInOperands() > kSampledOperand &&
static_cast<spv::Dim>(imageType->GetSingleWordInOperand(kDimOperand)) ==
spv::Dim::Dim1D &&
static_cast<spv::Dim>(imageType->GetSingleWordInOperand(kDimOperand)) == spv::Dim::Dim1D &&
imageType->GetSingleWordInOperand(kSampledOperand) == 1u;
}
@@ -84,11 +83,10 @@ namespace MobileGL {
// Each names its element type in its last in-operand, except arrays,
// whose element type is the FIRST. Both are reached here because a
// sampler uniform may be declared as an array of samplers.
type = defUseMgr->GetDef(
type->opcode() == spv::Op::OpTypeArray ||
type->opcode() == spv::Op::OpTypeRuntimeArray
? type->GetSingleWordInOperand(0)
: type->GetSingleWordInOperand(type->NumInOperands() - 1));
type = defUseMgr->GetDef(type->opcode() == spv::Op::OpTypeArray ||
type->opcode() == spv::Op::OpTypeRuntimeArray
? type->GetSingleWordInOperand(0)
: type->GetSingleWordInOperand(type->NumInOperands() - 1));
continue;
default:
return nullptr;
@@ -225,11 +223,9 @@ namespace MobileGL {
bool Any() const { return gradX != 0 || constOffset != 0 || offset != 0; }
};
OperandPositions LocateOperands(const Instruction& instruction,
uint32_t imageOperandsIndex) {
OperandPositions LocateOperands(const Instruction& instruction, uint32_t imageOperandsIndex) {
OperandPositions positions;
if (imageOperandsIndex == 0xFFFFFFFFu ||
instruction.NumInOperands() <= imageOperandsIndex) {
if (imageOperandsIndex == 0xFFFFFFFFu || instruction.NumInOperands() <= imageOperandsIndex) {
return positions;
}
const uint32_t mask = instruction.GetSingleWordInOperand(imageOperandsIndex);
@@ -297,8 +293,7 @@ namespace MobileGL {
const analysis::Type* type = context->get_type_mgr()->GetType(def->type_id());
if (type == nullptr) return shape;
const analysis::Vector* asVector = type->AsVector();
const analysis::Type* component =
asVector != nullptr ? asVector->element_type() : type;
const analysis::Type* component = asVector != nullptr ? asVector->element_type() : type;
if (component == nullptr) return shape;
if (component->AsInteger() == nullptr && component->AsFloat() == nullptr) {
return shape;
@@ -341,8 +336,7 @@ namespace MobileGL {
for (auto& function : *context->module()) {
for (auto& block : function) {
for (auto& instruction : block) {
const OpClassification classification =
ClassifyOpcode(instruction.opcode());
const OpClassification classification = ClassifyOpcode(instruction.opcode());
if (classification.kind != OpKind::Texel ||
instruction.NumInOperands() <= classification.coordinateOperand) {
continue;
@@ -380,15 +374,12 @@ namespace MobileGL {
RewritePlan PlanRewrite(IRContext* context, const LoweringScope& scope) {
RewritePlan plan;
const auto covered = [&scope](const Instruction* type) {
return scope.Covers(type);
};
const auto covered = [&scope](const Instruction* type) { return scope.Covers(type); };
for (auto& function : *context->module()) {
for (auto& block : function) {
for (auto& instruction : block) {
const OpClassification classification =
ClassifyOpcode(instruction.opcode());
const OpClassification classification = ClassifyOpcode(instruction.opcode());
if (classification.kind == OpKind::NotImageOp ||
classification.kind == OpKind::DimIndependentQuery) {
@@ -443,19 +434,16 @@ namespace MobileGL {
// (GetPlaneCoordSize(1D) == 1). Checking it up front is what
// keeps the apply phase total.
const ValueShape coordinate = DescribeValue(
context, instruction.GetSingleWordInOperand(
classification.coordinateOperand));
context, instruction.GetSingleWordInOperand(classification.coordinateOperand));
if (coordinate.componentCount == 0u || coordinate.componentCount > 3u) {
plan.declined = true;
return plan;
}
const uint32_t scalarOperands[] = {positions.gradX, positions.gradY,
positions.offset,
const uint32_t scalarOperands[] = {positions.gradX, positions.gradY, positions.offset,
positions.constOffset};
for (const uint32_t position : scalarOperands) {
if (position == 0u) continue;
if (!DescribeValue(context,
instruction.GetSingleWordInOperand(position))
if (!DescribeValue(context, instruction.GetSingleWordInOperand(position))
.IsScalar()) {
plan.declined = true;
return plan;
@@ -466,14 +454,12 @@ namespace MobileGL {
// possible if the operand really is one.
if (positions.constOffset != 0u &&
context->get_constant_mgr()->FindDeclaredConstant(
instruction.GetSingleWordInOperand(positions.constOffset)) ==
nullptr) {
instruction.GetSingleWordInOperand(positions.constOffset)) == nullptr) {
plan.declined = true;
return plan;
}
plan.sites.push_back(
{&instruction, classification.coordinateOperand, positions});
plan.sites.push_back({&instruction, classification.coordinateOperand, positions});
}
}
}
@@ -481,14 +467,12 @@ namespace MobileGL {
}
} // namespace
bool Lower1DSampledImagesPass::BinaryHasOffsetOrGrad1DSampledImage(
const Vector<Uint32>& binary) {
bool Lower1DSampledImagesPass::BinaryHasOffsetOrGrad1DSampledImage(const Vector<Uint32>& binary) {
if (binary.empty()) {
return false;
}
std::unique_ptr<IRContext> context = spvtools::BuildModule(
SPV_ENV_VULKAN_1_1,
[](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
SPV_ENV_VULKAN_1_1, [](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
binary.data(), binary.size());
if (!context) {
return false;
@@ -515,21 +499,17 @@ namespace MobileGL {
// pattern, which for a float zero is 0 as well - so one helper serves the integer
// coordinate of a fetch, the float coordinate of a sample and the float gradients
// alike, without a second spelling to keep in step.
const auto zeroOf = [&](const analysis::Type* componentType,
uint32_t componentTypeId) -> uint32_t {
const analysis::Constant* constant =
constantMgr->GetConstant(componentType, {0u});
const auto zeroOf = [&](const analysis::Type* componentType, uint32_t componentTypeId) -> uint32_t {
const analysis::Constant* constant = constantMgr->GetConstant(componentType, {0u});
if (constant == nullptr) return 0u;
const Instruction* defining =
constantMgr->GetDefiningInstruction(constant, componentTypeId);
const Instruction* defining = constantMgr->GetDefiningInstruction(constant, componentTypeId);
return defining != nullptr ? defining->result_id() : 0u;
};
// The whole of the arity repair, in one place: insert a zero at component 1.
// Scalar u becomes (u, 0); (u, layer) becomes (u, 0, layer); (u, q) becomes
// (u, 0, q). See the header for why one rule covers every shape.
const auto widen = [&](uint32_t valueId, Instruction* before,
bool mustBeConstant) -> uint32_t {
const auto widen = [&](uint32_t valueId, Instruction* before, bool mustBeConstant) -> uint32_t {
const ValueShape shape = DescribeValue(irContext, valueId);
if (shape.componentCount == 0u) return 0u;
@@ -550,18 +530,15 @@ namespace MobileGL {
if (!shape.IsScalar()) return 0u;
const analysis::Type* widenedType = typeMgr->GetType(widenedTypeId);
const analysis::Constant* widenedConstant =
widenedType != nullptr
? constantMgr->GetConstant(widenedType, {valueId, zeroId})
: nullptr;
widenedType != nullptr ? constantMgr->GetConstant(widenedType, {valueId, zeroId}) : nullptr;
if (widenedConstant == nullptr) return 0u;
const Instruction* defining =
constantMgr->GetDefiningInstruction(widenedConstant, widenedTypeId);
return defining != nullptr ? defining->result_id() : 0u;
}
InstructionBuilder builder(
irContext, before,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, before,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
std::vector<uint32_t> componentIds;
componentIds.reserve(shape.componentCount + 1u);
if (shape.IsScalar()) {
@@ -569,15 +546,13 @@ namespace MobileGL {
componentIds.push_back(zeroId);
} else {
for (uint32_t i = 0; i < shape.componentCount; ++i) {
Instruction* extracted =
builder.AddCompositeExtract(componentTypeId, valueId, {i});
Instruction* extracted = builder.AddCompositeExtract(componentTypeId, valueId, {i});
if (extracted == nullptr) return 0u;
componentIds.push_back(extracted->result_id());
if (i == 0u) componentIds.push_back(zeroId);
}
}
Instruction* widened =
builder.AddCompositeConstruct(widenedTypeId, componentIds);
Instruction* widened = builder.AddCompositeConstruct(widenedTypeId, componentIds);
return widened != nullptr ? widened->result_id() : 0u;
};
@@ -589,19 +564,16 @@ namespace MobileGL {
bool mustBeConstant;
};
const Target targets[] = {
{site.coordinateOperand, false},
{site.operands.gradX, false},
{site.operands.gradY, false},
{site.operands.offset, false},
{site.coordinateOperand, false}, {site.operands.gradX, false},
{site.operands.gradY, false}, {site.operands.offset, false},
{site.operands.constOffset, true},
};
for (const Target& target : targets) {
// Position 0 is the image operand, so it is this plan's "absent" marker
// for everything except the coordinate, which is never 0.
if (target.position == 0u) continue;
const uint32_t widenedId =
widen(instruction->GetSingleWordInOperand(target.position), instruction,
target.mustBeConstant);
const uint32_t widenedId = widen(instruction->GetSingleWordInOperand(target.position),
instruction, target.mustBeConstant);
if (widenedId == 0u) {
// Reachable only if the module's shapes disagree with what the plan
// recorded. Failing here makes the caller keep the input binary,
@@ -631,8 +603,7 @@ namespace MobileGL {
// instruction valid and RemoveDuplicates collapses the pair.
if (!AnyDim1DSampledTypeLeft(irContext)) {
for (Instruction& capability : irContext->capabilities()) {
const auto value =
static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
const auto value = static_cast<spv::Capability>(capability.GetSingleWordInOperand(0));
if (value == spv::Capability::Sampled1D) {
capability.SetInOperand(0, {static_cast<uint32_t>(spv::Capability::Shader)});
}
@@ -644,8 +615,7 @@ namespace MobileGL {
}
spvtools::Optimizer::PassToken Lower1DSampledImagesPass::CreateLower1DSampledImagesPass() {
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<Lower1DSampledImagesPass>());
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<Lower1DSampledImagesPass>());
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -77,8 +77,7 @@ namespace MobileGL {
std::vector<Instruction*> modes;
for (Instruction& mode : irContext->module()->execution_modes()) {
if (mode.opcode() == spv::Op::OpExecutionMode &&
mode.NumInOperands() > kExecutionModeModeOperand &&
if (mode.opcode() == spv::Op::OpExecutionMode && mode.NumInOperands() > kExecutionModeModeOperand &&
static_cast<spv::ExecutionMode>(mode.GetSingleWordInOperand(kExecutionModeModeOperand)) ==
spv::ExecutionMode::PixelCenterInteger) {
modes.push_back(&mode);
@@ -94,7 +93,8 @@ namespace MobileGL {
const auto* floatType = typeMgr->GetType(floatTypeId);
if (floatType == nullptr || floatType->AsFloat() == nullptr) return 0;
const auto* constant = constantMgr->GetConstant(floatType, {std::bit_cast<uint32_t>(value)});
const Instruction* inst = constant ? constantMgr->GetDefiningInstruction(constant, floatTypeId) : nullptr;
const Instruction* inst =
constant ? constantMgr->GetDefiningInstruction(constant, floatTypeId) : nullptr;
return inst ? inst->result_id() : 0;
};
@@ -155,7 +155,8 @@ namespace MobileGL {
}
if (refused) return Status::Failure;
for (Instruction* mode : modes) irContext->KillInst(mode);
for (Instruction* mode : modes)
irContext->KillInst(mode);
// `%r = OpLoad %ty %ptr` becomes `%fresh = OpLoad %ty %ptr` followed by
// `%r = OpFSub %ty %fresh %offset`: every existing use of %r picks the shifted
@@ -170,10 +171,10 @@ namespace MobileGL {
Instruction* next = shifted.load->NextNode();
if (next == nullptr) return Status::Failure;
shifted.load->SetResultId(freshId);
next->InsertBefore(MakeUnique<Instruction>(
irContext, spv::Op::OpFSub, typeId, resultId,
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {freshId}},
{SPV_OPERAND_TYPE_ID, {shifted.offsetId}}}));
next->InsertBefore(
MakeUnique<Instruction>(irContext, spv::Op::OpFSub, typeId, resultId,
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {freshId}},
{SPV_OPERAND_TYPE_ID, {shifted.offsetId}}}));
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -51,10 +51,8 @@ namespace MobileGL {
// global has no defined value to read, so lowering that one would answer the
// shader with garbage instead of the viewport it asked for. That case is left for
// the driver to reject.
Instruction* GetDecoratedViewportIndexOutput(IRContext* context,
const Instruction& annotation) {
Instruction* variable =
context->get_def_use_mgr()->GetDef(annotation.GetSingleWordInOperand(0));
Instruction* GetDecoratedViewportIndexOutput(IRContext* context, const Instruction& annotation) {
Instruction* variable = context->get_def_use_mgr()->GetDef(annotation.GetSingleWordInOperand(0));
if (variable == nullptr || variable->opcode() != spv::Op::OpVariable ||
static_cast<spv::StorageClass>(variable->GetSingleWordInOperand(0)) !=
spv::StorageClass::Output) {
@@ -27,8 +27,8 @@ namespace MobileGL {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::InstructionBuilder;
using spvtools::opt::IRContext;
// Image operations whose coordinate operand (in-operand 1) is a plain
// normalized coordinate and nothing else. The Dref *sample* forms are absent:
@@ -80,7 +80,7 @@ namespace MobileGL {
// The bare image an OpImageQuerySizeLod needs. An operation on a sampled image
// has to unwrap it first; one already holding a bare image is used as is.
uint32_t GetQueryableImage(IRContext* context, InstructionBuilder& builder, uint32_t imageOperandId,
uint32_t imageTypeId) {
uint32_t imageTypeId) {
auto* defUseMgr = context->get_def_use_mgr();
Instruction* object = defUseMgr->GetDef(imageOperandId);
if (object == nullptr) return 0;
@@ -136,9 +136,9 @@ namespace MobileGL {
}
const uint32_t coordinateId = instruction.GetSingleWordInOperand(1);
InstructionBuilder builder(
irContext, &instruction,
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
InstructionBuilder builder(irContext, &instruction,
IRContext::kAnalysisDefUse |
IRContext::kAnalysisInstrToBlockMapping);
const uint32_t queryableImageId =
GetQueryableImage(irContext, builder, imageOperandId, imageType->result_id());
if (queryableImageId == 0) {
@@ -151,8 +151,8 @@ namespace MobileGL {
queryableImageId, lodZeroId);
Instruction* sizeFloat =
builder.AddUnaryOp(float2TypeId, spv::Op::OpConvertSToF, size->result_id());
Instruction* normalized = builder.AddBinaryOp(
float2TypeId, spv::Op::OpFDiv, coordinateId, sizeFloat->result_id());
Instruction* normalized = builder.AddBinaryOp(float2TypeId, spv::Op::OpFDiv, coordinateId,
sizeFloat->result_id());
instruction.SetInOperand(1, {normalized->result_id()});
irContext->UpdateDefUse(&instruction);
rewroteCoordinate = true;
@@ -23,8 +23,8 @@ namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
namespace analysis = spvtools::opt::analysis;
@@ -69,8 +69,7 @@ namespace MobileGL {
for (Instruction& inst : irContext->types_values()) {
if (inst.opcode() != spv::Op::OpVariable) continue;
if (static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0)) !=
spv::StorageClass::Input) {
if (static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0)) != spv::StorageClass::Input) {
continue;
}
Instruction* pointerType = defUseMgr->GetDef(inst.type_id());
@@ -83,8 +82,8 @@ namespace MobileGL {
// an input over two attribute locations. Left alone; the vertex-input
// factory declines the matching attribute for the same reason.
MGLOG_E_ONCE("PackDoubleVertexInputsPass: vertex input %%%u is a %u-component 64-bit "
"float; only double and dvec2 inputs can be packed",
inst.result_id(), components);
"float; only double and dvec2 inputs can be packed",
inst.result_id(), components);
continue;
}
targets.push_back({&inst, pointeeTypeId, components});
@@ -138,18 +137,15 @@ namespace MobileGL {
irContext->AddGlobalValue(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVariable, packedPointerTypeId, packedVariableId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<Uint32>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<Uint32>(spv::StorageClass::Input)}}}));
// The interface decorations belong to whatever is actually the Input now.
std::vector<Instruction*> deadDecorations;
for (auto& annotation : irContext->annotations()) {
if (annotation.opcode() != spv::Op::OpDecorate) continue;
if (annotation.GetSingleWordInOperand(0) != oldVariableId) continue;
const auto decoration =
static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1));
if (decoration == spv::Decoration::Location ||
decoration == spv::Decoration::Component ||
const auto decoration = static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1));
if (decoration == spv::Decoration::Location || decoration == spv::Decoration::Component ||
decoration == spv::Decoration::RelaxedPrecision) {
annotation.SetInOperand(0, {packedVariableId});
} else {
@@ -141,21 +141,19 @@ namespace MobileGL {
// The cases here have to match the cases in UpdateUse: a use the rewrite
// does not know how to update disqualifies the variable.
switch (inst->opcode()) {
case spv::Op::OpLoad:
case spv::Op::OpImageTexelPointer: // treat like a load
return true;
case spv::Op::OpStore:
// Storing the variable's ADDRESS somewhere else escapes it.
return inst->GetOperand(1).AsId() != variableId;
case spv::Op::OpAccessChain:
return context()->get_def_use_mgr()->WhileEachUser(
inst, [this, inst](const Instruction* user) {
return IsValidUse(user, inst->result_id());
});
case spv::Op::OpName:
return true;
default:
return spvOpcodeIsDecoration(inst->opcode());
case spv::Op::OpLoad:
case spv::Op::OpImageTexelPointer: // treat like a load
return true;
case spv::Op::OpStore:
// Storing the variable's ADDRESS somewhere else escapes it.
return inst->GetOperand(1).AsId() != variableId;
case spv::Op::OpAccessChain:
return context()->get_def_use_mgr()->WhileEachUser(
inst, [this, inst](const Instruction* user) { return IsValidUse(user, inst->result_id()); });
case spv::Op::OpName:
return true;
default:
return spvOpcodeIsDecoration(inst->opcode());
}
}
@@ -166,8 +164,7 @@ namespace MobileGL {
std::unique_ptr<Instruction> var(variable); // take ownership
context()->ForgetUses(variable);
variable->SetInOperand(kVariableStorageClassInIdx,
{uint32_t(spv::StorageClass::Function)});
variable->SetInOperand(kVariableStorageClassInIdx, {uint32_t(spv::StorageClass::Function)});
const uint32_t newTypeId = GetNewType(variable->type_id());
if (newTypeId == 0) {
@@ -185,10 +182,8 @@ namespace MobileGL {
uint32_t PrivateToEntryLocalPass::GetNewType(uint32_t oldTypeId) {
auto* typeMgr = context()->get_type_mgr();
Instruction* oldTypeInst = get_def_use_mgr()->GetDef(oldTypeId);
const uint32_t pointeeTypeId =
oldTypeInst->GetSingleWordInOperand(kSpvTypePointerTypeIdInIdx);
const uint32_t newTypeId =
typeMgr->FindPointerToType(pointeeTypeId, spv::StorageClass::Function);
const uint32_t pointeeTypeId = oldTypeInst->GetSingleWordInOperand(kSpvTypePointerTypeIdInIdx);
const uint32_t newTypeId = typeMgr->FindPointerToType(pointeeTypeId, spv::StorageClass::Function);
if (newTypeId != 0) {
context()->UpdateDefUse(context()->get_def_use_mgr()->GetDef(newTypeId));
}
@@ -198,31 +193,30 @@ namespace MobileGL {
bool PrivateToEntryLocalPass::UpdateUse(Instruction* inst, Instruction* user) {
// The cases here have to match the cases in IsValidUse.
switch (inst->opcode()) {
case spv::Op::OpLoad:
case spv::Op::OpStore:
case spv::Op::OpImageTexelPointer: // treat like a load
// Fine as-is: their type is the pointed-to type, which is unchanged.
break;
case spv::Op::OpAccessChain: {
context()->ForgetUses(inst);
const uint32_t newTypeId = GetNewType(inst->type_id());
if (newTypeId == 0) {
return false;
}
inst->SetResultType(newTypeId);
context()->AnalyzeUses(inst);
if (!UpdateUses(inst)) {
return false;
}
break;
case spv::Op::OpLoad:
case spv::Op::OpStore:
case spv::Op::OpImageTexelPointer: // treat like a load
// Fine as-is: their type is the pointed-to type, which is unchanged.
break;
case spv::Op::OpAccessChain: {
context()->ForgetUses(inst);
const uint32_t newTypeId = GetNewType(inst->type_id());
if (newTypeId == 0) {
return false;
}
case spv::Op::OpName:
case spv::Op::OpEntryPoint: // handled separately in Process()
break;
default:
assert(spvOpcodeIsDecoration(inst->opcode()) &&
"PrivateToEntryLocalPass: unexpected use opcode");
break;
inst->SetResultType(newTypeId);
context()->AnalyzeUses(inst);
if (!UpdateUses(inst)) {
return false;
}
break;
}
case spv::Op::OpName:
case spv::Op::OpEntryPoint: // handled separately in Process()
break;
default:
assert(spvOpcodeIsDecoration(inst->opcode()) && "PrivateToEntryLocalPass: unexpected use opcode");
break;
}
(void)user;
return true;
@@ -231,8 +225,7 @@ namespace MobileGL {
bool PrivateToEntryLocalPass::UpdateUses(Instruction* inst) {
const uint32_t id = inst->result_id();
std::vector<Instruction*> uses;
context()->get_def_use_mgr()->ForEachUser(id,
[&uses](Instruction* use) { uses.push_back(use); });
context()->get_def_use_mgr()->ForEachUser(id, [&uses](Instruction* use) { uses.push_back(use); });
for (Instruction* use : uses) {
if (!UpdateUse(use, inst)) {
return false;
@@ -242,8 +235,7 @@ namespace MobileGL {
}
spvtools::Optimizer::PassToken PrivateToEntryLocalPass::CreatePrivateToEntryLocalPass() {
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<PrivateToEntryLocalPass>());
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<PrivateToEntryLocalPass>());
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -63,17 +63,14 @@ namespace MobileGL {
context->AddGlobalValue(spvtools::MakeUnique<Instruction>(
context, spv::Op::OpVariable, pointerTypeId, variableId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<uint32_t>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<uint32_t>(spv::StorageClass::Input)}}}));
context->AddAnnotationInst(spvtools::MakeUnique<Instruction>(
context, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {variableId}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{static_cast<uint32_t>(spv::BuiltIn::BaseInstance)}}}));
{SPV_OPERAND_TYPE_DECORATION, {static_cast<uint32_t>(spv::Decoration::BuiltIn)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {static_cast<uint32_t>(spv::BuiltIn::BaseInstance)}}}));
// Input variables must appear in the entry point interface list.
for (Instruction& entryPoint : context->module()->entry_points()) {
@@ -97,8 +94,7 @@ namespace MobileGL {
// Collect every load of the InstanceIndex variable before mutating anything.
std::vector<Instruction*> instanceLoads;
defUseMgr->ForEachUser(instanceIndexVar, [&](Instruction* user) {
if (user->opcode() == spv::Op::OpLoad &&
user->GetSingleWordInOperand(0) == instanceIndexVarId) {
if (user->opcode() == spv::Op::OpLoad && user->GetSingleWordInOperand(0) == instanceIndexVarId) {
instanceLoads.push_back(user);
}
});
@@ -114,8 +110,8 @@ namespace MobileGL {
Instruction* baseInstanceVar = FindBuiltinInputVariable(irContext, spv::BuiltIn::BaseInstance);
const uint32_t baseInstanceVarId = (baseInstanceVar != nullptr)
? baseInstanceVar->result_id()
: SynthesizeBaseInstanceVariable(irContext, instanceIndexVar);
? baseInstanceVar->result_id()
: SynthesizeBaseInstanceVariable(irContext, instanceIndexVar);
// Replace each `OpLoad %ty %res %instanceIndex` with
// OpLoad %ty %fresh1 %instanceIndex
@@ -135,9 +131,8 @@ namespace MobileGL {
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {baseInstanceVarId}}}));
loadInst->SetOpcode(spv::Op::OpISub);
loadInst->SetInOperands(Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {instanceValueId}},
{SPV_OPERAND_TYPE_ID, {baseValueId}}});
loadInst->SetInOperands(Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {instanceValueId}},
{SPV_OPERAND_TYPE_ID, {baseValueId}}});
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -39,22 +39,21 @@ namespace MobileGL {
// glslang mangles function OpNames as "name(<paramcodes>"; the base name is
// everything before the '(' (entry points like "main" carry no mangling).
const std::string mangled = debugInst.GetInOperand(1).AsString();
const std::string_view baseName =
std::string_view(mangled).substr(0, mangled.find('('));
const std::string_view baseName = std::string_view(mangled).substr(0, mangled.find('('));
if (!IsEsslBuiltinFunctionName(baseName)) {
continue;
}
debugInst.SetInOperand(1, spvtools::utils::MakeVector<spvtools::opt::Operand::OperandData>(
"mg_" + mangled));
debugInst.SetInOperand(
1, spvtools::utils::MakeVector<spvtools::opt::Operand::OperandData>("mg_" + mangled));
modified = true;
}
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}
spvtools::Optimizer::PassToken
RenameBuiltinShadowingFunctionsPass::CreateRenameBuiltinShadowingFunctionsPass() {
spvtools::Optimizer::PassToken RenameBuiltinShadowingFunctionsPass::
CreateRenameBuiltinShadowingFunctionsPass() {
return spvtools::Optimizer::PassToken(MakeUnique<RenameBuiltinShadowingFunctionsPass>());
}
} // namespace ShaderTranspiler
@@ -26,7 +26,7 @@ namespace MobileGL {
}
const char* GetConflictingFunctionParameterCompatName(spvtools::opt::IRContext* context,
spvtools::opt::Instruction& nameInst) {
spvtools::opt::Instruction& nameInst) {
if (nameInst.opcode() != spv::Op::OpName || nameInst.NumInOperands() < 2) {
return nullptr;
}
@@ -61,7 +61,8 @@ namespace MobileGL {
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}
spvtools::Optimizer::PassToken RenameSamplerFunctionParameterPass::CreateRenameSamplerFunctionParameterPass() {
spvtools::Optimizer::PassToken RenameSamplerFunctionParameterPass::
CreateRenameSamplerFunctionParameterPass() {
return spvtools::Optimizer::PassToken(MakeUnique<RenameSamplerFunctionParameterPass>());
}
} // namespace ShaderTranspiler
@@ -25,8 +25,8 @@ namespace MobileGL {
namespace MG_Util {
namespace ShaderTranspiler {
namespace {
using spvtools::opt::IRContext;
using spvtools::opt::Instruction;
using spvtools::opt::IRContext;
using spvtools::opt::Operand;
namespace analysis = spvtools::opt::analysis;
@@ -39,8 +39,7 @@ namespace MobileGL {
Uint32 LocationsPerElement(const analysis::Type* type) {
if (type == nullptr) return 0;
// Any scalar takes one location, a 64-bit one included.
if (type->AsFloat() != nullptr || type->AsInteger() != nullptr ||
type->AsBool() != nullptr) {
if (type->AsFloat() != nullptr || type->AsInteger() != nullptr || type->AsBool() != nullptr) {
return 1u;
}
if (const auto* vector = type->AsVector()) {
@@ -109,8 +108,7 @@ namespace MobileGL {
for (Instruction& inst : irContext->types_values()) {
if (inst.opcode() != spv::Op::OpVariable) continue;
if (static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0)) !=
spv::StorageClass::Input) {
if (static_cast<spv::StorageClass>(inst.GetSingleWordInOperand(0)) != spv::StorageClass::Input) {
continue;
}
Instruction* pointerType = defUseMgr->GetDef(inst.type_id());
@@ -176,10 +174,9 @@ namespace MobileGL {
// it) either cannot happen for a vertex input or would need the callee
// rewritten as well, and silently getting that wrong is worse than
// leaving the module for SPIRV-Cross to reject out loud.
const Bool indexes =
opcode == spv::Op::OpAccessChain ||
opcode == spv::Op::OpInBoundsAccessChain ||
opcode == spv::Op::OpCopyObject;
const Bool indexes = opcode == spv::Op::OpAccessChain ||
opcode == spv::Op::OpInBoundsAccessChain ||
opcode == spv::Op::OpCopyObject;
if (indexes) {
if (inst.NumInOperands() > 0 &&
derivedPointers.count(inst.GetSingleWordInOperand(0)) != 0) {
@@ -265,17 +262,14 @@ namespace MobileGL {
const Uint32 elementVariableId = irContext->TakeNextId();
elementVariableIds[element] = elementVariableId;
irContext->AddGlobalValue(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVariable, target.elementInputPointerTypeId,
elementVariableId,
irContext, spv::Op::OpVariable, target.elementInputPointerTypeId, elementVariableId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_STORAGE_CLASS,
{static_cast<Uint32>(spv::StorageClass::Input)}}}));
{SPV_OPERAND_TYPE_STORAGE_CLASS, {static_cast<Uint32>(spv::StorageClass::Input)}}}));
newDecorations.push_back(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpDecorate, 0, 0,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {elementVariableId}},
{SPV_OPERAND_TYPE_DECORATION,
{static_cast<Uint32>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_DECORATION, {static_cast<Uint32>(spv::Decoration::Location)}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER,
{target.baseLocation + element * target.locationsPerElement}}}));
}
@@ -288,12 +282,9 @@ namespace MobileGL {
for (auto& annotation : irContext->annotations()) {
if (annotation.opcode() != spv::Op::OpDecorate) continue;
if (annotation.GetSingleWordInOperand(0) != oldVariableId) continue;
const auto decoration =
static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1));
if (decoration == spv::Decoration::RelaxedPrecision ||
decoration == spv::Decoration::Flat ||
decoration == spv::Decoration::NoPerspective ||
decoration == spv::Decoration::Centroid ||
const auto decoration = static_cast<spv::Decoration>(annotation.GetSingleWordInOperand(1));
if (decoration == spv::Decoration::RelaxedPrecision || decoration == spv::Decoration::Flat ||
decoration == spv::Decoration::NoPerspective || decoration == spv::Decoration::Centroid ||
decoration == spv::Decoration::Sample) {
for (const Uint32 elementVariableId : elementVariableIds) {
std::vector<Operand> operands;
@@ -301,8 +292,8 @@ namespace MobileGL {
for (Uint32 i = 1; i < annotation.NumInOperands(); ++i) {
operands.push_back(annotation.GetInOperand(i));
}
newDecorations.push_back(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpDecorate, 0, 0, operands));
newDecorations.push_back(
spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpDecorate, 0, 0, operands));
}
}
deadDecorations.push_back(&annotation);
@@ -329,8 +320,8 @@ namespace MobileGL {
std::vector<Operand> rebuilt;
for (Uint32 i = 0; i < interfaceOperands.size(); ++i) {
const Operand& operand = interfaceOperands[i];
if (i >= 3 && operand.type == SPV_OPERAND_TYPE_ID &&
operand.words.size() == 1 && operand.words[0] == oldVariableId) {
if (i >= 3 && operand.type == SPV_OPERAND_TYPE_ID && operand.words.size() == 1 &&
operand.words[0] == oldVariableId) {
for (const Uint32 elementVariableId : elementVariableIds) {
rebuilt.push_back({SPV_OPERAND_TYPE_ID, {elementVariableId}});
}
@@ -347,15 +338,12 @@ namespace MobileGL {
const Uint32 elementPointerId = irContext->TakeNextId();
insertPoint = insertPoint.InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpLoad, target.elementTypeId, loadedId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {elementVariableIds[element]}}}));
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {elementVariableIds[element]}}}));
++insertPoint;
insertPoint = insertPoint.InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpAccessChain, target.elementPrivatePointerTypeId,
elementPointerId,
std::initializer_list<Operand>{
{SPV_OPERAND_TYPE_ID, {oldVariableId}},
{SPV_OPERAND_TYPE_ID, {indexConstantIds[element]}}}));
irContext, spv::Op::OpAccessChain, target.elementPrivatePointerTypeId, elementPointerId,
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {oldVariableId}},
{SPV_OPERAND_TYPE_ID, {indexConstantIds[element]}}}));
++insertPoint;
insertPoint = insertPoint.InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpStore, 0, 0,
@@ -378,8 +366,8 @@ namespace MobileGL {
spv::StorageClass::Private) {
continue;
}
pointer->SetResultType(typeMgr->FindPointerToType(resultType->GetSingleWordInOperand(1),
spv::StorageClass::Private));
pointer->SetResultType(
typeMgr->FindPointerToType(resultType->GetSingleWordInOperand(1), spv::StorageClass::Private));
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -61,10 +61,8 @@ namespace MobileGL {
const std::unordered_set<uint32_t>& blockStructIds,
spv::StorageClass& outStorageClass) {
if (variable.opcode() != spv::Op::OpVariable) return 0;
const auto storageClass =
static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input &&
storageClass != spv::StorageClass::Output) {
const auto storageClass = static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input && storageClass != spv::StorageClass::Output) {
return 0;
}
@@ -87,8 +85,7 @@ namespace MobileGL {
return pointeeId;
}
Bool DirectionIsArmed(spv::StorageClass storageClass, Bool stripInputBlocks,
Bool stripOutputBlocks) {
Bool DirectionIsArmed(spv::StorageClass storageClass, Bool stripInputBlocks, Bool stripOutputBlocks) {
return storageClass == spv::StorageClass::Input ? stripInputBlocks : stripOutputBlocks;
}
} // namespace
@@ -155,16 +152,14 @@ namespace MobileGL {
for (Instruction& annotation : irContext->module()->annotations()) {
if (annotation.opcode() == spv::Op::OpDecorate) {
if (!isLocationOrComponent(annotation.GetSingleWordInOperand(1))) continue;
if (armedVariableIds.find(annotation.GetSingleWordInOperand(0)) ==
armedVariableIds.end()) {
if (armedVariableIds.find(annotation.GetSingleWordInOperand(0)) == armedVariableIds.end()) {
continue;
}
toKill.push_back(&annotation);
} else if (annotation.opcode() == spv::Op::OpMemberDecorate) {
// OpMemberDecorate <struct> <member> <decoration> ...
if (!isLocationOrComponent(annotation.GetSingleWordInOperand(2))) continue;
if (armedStructIds.find(annotation.GetSingleWordInOperand(0)) ==
armedStructIds.end()) {
if (armedStructIds.find(annotation.GetSingleWordInOperand(0)) == armedStructIds.end()) {
continue;
}
toKill.push_back(&annotation);
@@ -180,8 +175,8 @@ namespace MobileGL {
spvtools::Optimizer::PassToken StripIoBlockLocationsPass::CreateStripIoBlockLocationsPass(
Bool stripInputBlocks, Bool stripOutputBlocks, Bool* strippedAny) {
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<StripIoBlockLocationsPass>(
stripInputBlocks, stripOutputBlocks, strippedAny));
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<StripIoBlockLocationsPass>(stripInputBlocks, stripOutputBlocks, strippedAny));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -70,16 +70,16 @@ namespace MobileGL {
// `stripOutputBlocks` the ones it PRODUCES. `strippedAny`, when non-null,
// receives whether this stage actually had one, so the caller can decline the
// re-serialised module when there was nothing to strip.
StripIoBlockLocationsPass(Bool stripInputBlocks, Bool stripOutputBlocks,
Bool* strippedAny = nullptr)
StripIoBlockLocationsPass(Bool stripInputBlocks, Bool stripOutputBlocks, Bool* strippedAny = nullptr)
: m_stripInputBlocks(stripInputBlocks), m_stripOutputBlocks(stripOutputBlocks),
m_strippedAny(strippedAny) {}
const char* name() const override { return "mobilegl-strip-io-block-locations"; }
Status Process() override;
static spvtools::Optimizer::PassToken CreateStripIoBlockLocationsPass(
Bool stripInputBlocks, Bool stripOutputBlocks, Bool* strippedAny);
static spvtools::Optimizer::PassToken CreateStripIoBlockLocationsPass(Bool stripInputBlocks,
Bool stripOutputBlocks,
Bool* strippedAny);
private:
Bool m_stripInputBlocks = false;
@@ -110,8 +110,8 @@ namespace MobileGL {
return Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken
StripUboMemberRelaxedPrecisionPass::CreateStripUboMemberRelaxedPrecisionPass() {
spvtools::Optimizer::PassToken StripUboMemberRelaxedPrecisionPass::
CreateStripUboMemberRelaxedPrecisionPass() {
return spvtools::Optimizer::PassToken(MakeUnique<StripUboMemberRelaxedPrecisionPass>());
}
} // namespace ShaderTranspiler
@@ -24,23 +24,21 @@ namespace MobileGL {
if (annotation.opcode() != spv::Op::OpDecorate) {
continue;
}
if (annotation.GetSingleWordInOperand(1) !=
static_cast<uint32_t>(spv::Decoration::Location)) {
if (annotation.GetSingleWordInOperand(1) != static_cast<uint32_t>(spv::Decoration::Location)) {
continue;
}
Instruction* target =
get_def_use_mgr()->GetDef(annotation.GetSingleWordInOperand(0));
Instruction* target = get_def_use_mgr()->GetDef(annotation.GetSingleWordInOperand(0));
if (target == nullptr || target->opcode() != spv::Op::OpVariable) {
continue;
}
switch (spv::StorageClass(target->GetSingleWordInOperand(0))) {
case spv::StorageClass::UniformConstant:
case spv::StorageClass::Uniform:
case spv::StorageClass::StorageBuffer:
toKill.push_back(&annotation);
break;
default:
break;
case spv::StorageClass::UniformConstant:
case spv::StorageClass::Uniform:
case spv::StorageClass::StorageBuffer:
toKill.push_back(&annotation);
break;
default:
break;
}
}
@@ -51,8 +49,7 @@ namespace MobileGL {
}
spvtools::Optimizer::PassToken StripUniformLocationsPass::CreateStripUniformLocationsPass() {
return spvtools::Optimizer::PassToken(
spvtools::MakeUnique<StripUniformLocationsPass>());
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<StripUniformLocationsPass>());
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -66,10 +66,8 @@ namespace MobileGL {
const std::unordered_set<uint32_t>& blockStructIds,
spv::StorageClass& outStorageClass) {
if (variable.opcode() != spv::Op::OpVariable) return 0;
const auto storageClass =
static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input &&
storageClass != spv::StorageClass::Output) {
const auto storageClass = static_cast<spv::StorageClass>(variable.GetSingleWordInOperand(0));
if (storageClass != spv::StorageClass::Input && storageClass != spv::StorageClass::Output) {
return 0;
}
@@ -129,8 +127,7 @@ namespace MobileGL {
std::unordered_map<uint32_t, Uint32> storageMaskByStructId;
};
IoBlockIndex IndexIoBlocks(IRContext* irContext,
const std::unordered_set<uint32_t>& blockStructIds) {
IoBlockIndex IndexIoBlocks(IRContext* irContext, const std::unordered_set<uint32_t>& blockStructIds) {
IoBlockIndex index;
for (Instruction& variable : irContext->module()->types_values()) {
spv::StorageClass storageClass = spv::StorageClass::Input;
@@ -196,8 +193,7 @@ namespace MobileGL {
const Bool isInput = direction == 0;
const std::map<String, uint32_t>& byName =
isInput ? index.inputStructByName : index.outputStructByName;
const std::map<String, String>& renames =
isInput ? m_inputBlockRenames : m_outputBlockRenames;
const std::map<String, String>& renames = isInput ? m_inputBlockRenames : m_outputBlockRenames;
const Uint32 wantedMask = isInput ? kSeenAsInput : kSeenAsOutput;
for (const auto& block : byName) {
@@ -221,10 +217,10 @@ namespace MobileGL {
}
spvtools::Optimizer::PassToken UniquifyIoBlockNamesPass::CreateUniquifyIoBlockNamesPass(
const std::map<String, String>& inputBlockRenames,
const std::map<String, String>& outputBlockRenames, std::set<String>* renamedBlockNames) {
return spvtools::Optimizer::PassToken(MakeUnique<UniquifyIoBlockNamesPass>(
inputBlockRenames, outputBlockRenames, renamedBlockNames));
const std::map<String, String>& inputBlockRenames, const std::map<String, String>& outputBlockRenames,
std::set<String>* renamedBlockNames) {
return spvtools::Optimizer::PassToken(
MakeUnique<UniquifyIoBlockNamesPass>(inputBlockRenames, outputBlockRenames, renamedBlockNames));
}
} // namespace ShaderTranspiler
} // namespace MG_Util
@@ -77,8 +77,7 @@ namespace MobileGL {
static spvtools::Optimizer::PassToken CreateUniquifyIoBlockNamesPass(
const std::map<String, String>& inputBlockRenames,
const std::map<String, String>& outputBlockRenames,
std::set<String>* renamedBlockNames);
const std::map<String, String>& outputBlockRenames, std::set<String>* renamedBlockNames);
private:
std::map<String, String> m_inputBlockRenames;
@@ -136,43 +136,62 @@ namespace MobileGL {
ImageFormatWidening WideningOfSpirvImageFormat(spv::ImageFormat format) {
switch (format) {
// Float.
case spv::ImageFormat::Rg32f: return {spv::ImageFormat::Rgba32f, 2};
case spv::ImageFormat::Rg16f: return {spv::ImageFormat::Rgba16f, 2};
case spv::ImageFormat::R16f: return {spv::ImageFormat::Rgba16f, 1};
case spv::ImageFormat::Rg32f:
return {spv::ImageFormat::Rgba32f, 2};
case spv::ImageFormat::Rg16f:
return {spv::ImageFormat::Rgba16f, 2};
case spv::ImageFormat::R16f:
return {spv::ImageFormat::Rgba16f, 1};
// Not a channel widening but a lossless re-encoding - see above. Three
// channels, so the fourth reads as the 1 GL defines for a format without one.
case spv::ImageFormat::R11fG11fB10f: return {spv::ImageFormat::Rgba16f, 3};
case spv::ImageFormat::R11fG11fB10f:
return {spv::ImageFormat::Rgba16f, 3};
// Unsigned normalized.
case spv::ImageFormat::Rg8: return {spv::ImageFormat::Rgba8, 2};
case spv::ImageFormat::R8: return {spv::ImageFormat::Rgba8, 1};
case spv::ImageFormat::Rg8:
return {spv::ImageFormat::Rgba8, 2};
case spv::ImageFormat::R8:
return {spv::ImageFormat::Rgba8, 1};
// Signed normalized.
case spv::ImageFormat::Rg8Snorm: return {spv::ImageFormat::Rgba8Snorm, 2};
case spv::ImageFormat::R8Snorm: return {spv::ImageFormat::Rgba8Snorm, 1};
case spv::ImageFormat::Rg8Snorm:
return {spv::ImageFormat::Rgba8Snorm, 2};
case spv::ImageFormat::R8Snorm:
return {spv::ImageFormat::Rgba8Snorm, 1};
// Signed integer.
case spv::ImageFormat::Rg32i: return {spv::ImageFormat::Rgba32i, 2};
case spv::ImageFormat::Rg16i: return {spv::ImageFormat::Rgba16i, 2};
case spv::ImageFormat::R16i: return {spv::ImageFormat::Rgba16i, 1};
case spv::ImageFormat::Rg8i: return {spv::ImageFormat::Rgba8i, 2};
case spv::ImageFormat::R8i: return {spv::ImageFormat::Rgba8i, 1};
case spv::ImageFormat::Rg32i:
return {spv::ImageFormat::Rgba32i, 2};
case spv::ImageFormat::Rg16i:
return {spv::ImageFormat::Rgba16i, 2};
case spv::ImageFormat::R16i:
return {spv::ImageFormat::Rgba16i, 1};
case spv::ImageFormat::Rg8i:
return {spv::ImageFormat::Rgba8i, 2};
case spv::ImageFormat::R8i:
return {spv::ImageFormat::Rgba8i, 1};
// Unsigned integer.
case spv::ImageFormat::Rg32ui: return {spv::ImageFormat::Rgba32ui, 2};
case spv::ImageFormat::Rg16ui: return {spv::ImageFormat::Rgba16ui, 2};
case spv::ImageFormat::R16ui: return {spv::ImageFormat::Rgba16ui, 1};
case spv::ImageFormat::Rg8ui: return {spv::ImageFormat::Rgba8ui, 2};
case spv::ImageFormat::R8ui: return {spv::ImageFormat::Rgba8ui, 1};
case spv::ImageFormat::Rg32ui:
return {spv::ImageFormat::Rgba32ui, 2};
case spv::ImageFormat::Rg16ui:
return {spv::ImageFormat::Rgba16ui, 2};
case spv::ImageFormat::R16ui:
return {spv::ImageFormat::Rgba16ui, 1};
case spv::ImageFormat::Rg8ui:
return {spv::ImageFormat::Rgba8ui, 2};
case spv::ImageFormat::R8ui:
return {spv::ImageFormat::Rgba8ui, 1};
// FOUR channels, so there is no surplus channel to mask and no access is
// rewritten - 10, 10, 10 and 2 bits of unsigned integer all fit in sixteen.
case spv::ImageFormat::Rgb10a2ui: return {spv::ImageFormat::Rgba16ui, 4};
case spv::ImageFormat::Rgb10a2ui:
return {spv::ImageFormat::Rgba16ui, 4};
// Unsigned normalized, carried as codes in [0, 2^b - 1].
case spv::ImageFormat::Rgba16:
return {spv::ImageFormat::Rgba16ui, 4,
{kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
return {
spv::ImageFormat::Rgba16ui, 4, {kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
case spv::ImageFormat::Rg16:
return {spv::ImageFormat::Rgba16ui, 2,
{kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
return {
spv::ImageFormat::Rgba16ui, 2, {kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
case spv::ImageFormat::R16:
return {spv::ImageFormat::Rgba16ui, 1,
{kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
return {
spv::ImageFormat::Rgba16ui, 1, {kUnorm16Max, kUnorm16Max, kUnorm16Max, kUnorm16Max}, false};
// The one entry whose channels are not all the same width, which is the whole
// reason ChannelMax is per channel rather than one number.
case spv::ImageFormat::Rgb10A2:
@@ -181,14 +200,14 @@ namespace MobileGL {
// 2^(b-1) - 1]. The carrier's channel is UNSIGNED, so the code's sixteen bits
// are stored verbatim and sign-extended again on the way out.
case spv::ImageFormat::Rgba16Snorm:
return {spv::ImageFormat::Rgba16ui, 4,
{kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
return {
spv::ImageFormat::Rgba16ui, 4, {kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
case spv::ImageFormat::Rg16Snorm:
return {spv::ImageFormat::Rgba16ui, 2,
{kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
return {
spv::ImageFormat::Rgba16ui, 2, {kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
case spv::ImageFormat::R16Snorm:
return {spv::ImageFormat::Rgba16ui, 1,
{kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
return {
spv::ImageFormat::Rgba16ui, 1, {kSnorm16Max, kSnorm16Max, kSnorm16Max, kSnorm16Max}, true};
default:
return {};
}
@@ -200,45 +219,84 @@ namespace MobileGL {
// order, as BakeImageFormatsPass::SpirvImageFormatFromGLInternalFormat.
spv::ImageFormat SpirvImageFormatOfGL(Uint glInternalFormat) {
switch (glInternalFormat) {
case 0x8814: /*GL_RGBA32F*/ return spv::ImageFormat::Rgba32f;
case 0x881A: /*GL_RGBA16F*/ return spv::ImageFormat::Rgba16f;
case 0x8230: /*GL_RG32F*/ return spv::ImageFormat::Rg32f;
case 0x822F: /*GL_RG16F*/ return spv::ImageFormat::Rg16f;
case 0x8C3A: /*GL_R11F_G11F_B10F*/ return spv::ImageFormat::R11fG11fB10f;
case 0x822E: /*GL_R32F*/ return spv::ImageFormat::R32f;
case 0x822D: /*GL_R16F*/ return spv::ImageFormat::R16f;
case 0x8D70: /*GL_RGBA32UI*/ return spv::ImageFormat::Rgba32ui;
case 0x8D76: /*GL_RGBA16UI*/ return spv::ImageFormat::Rgba16ui;
case 0x8D7C: /*GL_RGBA8UI*/ return spv::ImageFormat::Rgba8ui;
case 0x906F: /*GL_RGB10_A2UI*/ return spv::ImageFormat::Rgb10a2ui;
case 0x823C: /*GL_RG32UI*/ return spv::ImageFormat::Rg32ui;
case 0x823A: /*GL_RG16UI*/ return spv::ImageFormat::Rg16ui;
case 0x8238: /*GL_RG8UI*/ return spv::ImageFormat::Rg8ui;
case 0x8236: /*GL_R32UI*/ return spv::ImageFormat::R32ui;
case 0x8234: /*GL_R16UI*/ return spv::ImageFormat::R16ui;
case 0x8232: /*GL_R8UI*/ return spv::ImageFormat::R8ui;
case 0x8D82: /*GL_RGBA32I*/ return spv::ImageFormat::Rgba32i;
case 0x8D88: /*GL_RGBA16I*/ return spv::ImageFormat::Rgba16i;
case 0x8D8E: /*GL_RGBA8I*/ return spv::ImageFormat::Rgba8i;
case 0x823B: /*GL_RG32I*/ return spv::ImageFormat::Rg32i;
case 0x8239: /*GL_RG16I*/ return spv::ImageFormat::Rg16i;
case 0x8237: /*GL_RG8I*/ return spv::ImageFormat::Rg8i;
case 0x8235: /*GL_R32I*/ return spv::ImageFormat::R32i;
case 0x8233: /*GL_R16I*/ return spv::ImageFormat::R16i;
case 0x8231: /*GL_R8I*/ return spv::ImageFormat::R8i;
case 0x8058: /*GL_RGBA8*/ return spv::ImageFormat::Rgba8;
case 0x805B: /*GL_RGBA16*/ return spv::ImageFormat::Rgba16;
case 0x8059: /*GL_RGB10_A2*/ return spv::ImageFormat::Rgb10A2;
case 0x822B: /*GL_RG8*/ return spv::ImageFormat::Rg8;
case 0x822C: /*GL_RG16*/ return spv::ImageFormat::Rg16;
case 0x8229: /*GL_R8*/ return spv::ImageFormat::R8;
case 0x822A: /*GL_R16*/ return spv::ImageFormat::R16;
case 0x8F97: /*GL_RGBA8_SNORM*/ return spv::ImageFormat::Rgba8Snorm;
case 0x8F9B: /*GL_RGBA16_SNORM*/ return spv::ImageFormat::Rgba16Snorm;
case 0x8F95: /*GL_RG8_SNORM*/ return spv::ImageFormat::Rg8Snorm;
case 0x8F99: /*GL_RG16_SNORM*/ return spv::ImageFormat::Rg16Snorm;
case 0x8F94: /*GL_R8_SNORM*/ return spv::ImageFormat::R8Snorm;
case 0x8F98: /*GL_R16_SNORM*/ return spv::ImageFormat::R16Snorm;
case 0x8814: /*GL_RGBA32F*/
return spv::ImageFormat::Rgba32f;
case 0x881A: /*GL_RGBA16F*/
return spv::ImageFormat::Rgba16f;
case 0x8230: /*GL_RG32F*/
return spv::ImageFormat::Rg32f;
case 0x822F: /*GL_RG16F*/
return spv::ImageFormat::Rg16f;
case 0x8C3A: /*GL_R11F_G11F_B10F*/
return spv::ImageFormat::R11fG11fB10f;
case 0x822E: /*GL_R32F*/
return spv::ImageFormat::R32f;
case 0x822D: /*GL_R16F*/
return spv::ImageFormat::R16f;
case 0x8D70: /*GL_RGBA32UI*/
return spv::ImageFormat::Rgba32ui;
case 0x8D76: /*GL_RGBA16UI*/
return spv::ImageFormat::Rgba16ui;
case 0x8D7C: /*GL_RGBA8UI*/
return spv::ImageFormat::Rgba8ui;
case 0x906F: /*GL_RGB10_A2UI*/
return spv::ImageFormat::Rgb10a2ui;
case 0x823C: /*GL_RG32UI*/
return spv::ImageFormat::Rg32ui;
case 0x823A: /*GL_RG16UI*/
return spv::ImageFormat::Rg16ui;
case 0x8238: /*GL_RG8UI*/
return spv::ImageFormat::Rg8ui;
case 0x8236: /*GL_R32UI*/
return spv::ImageFormat::R32ui;
case 0x8234: /*GL_R16UI*/
return spv::ImageFormat::R16ui;
case 0x8232: /*GL_R8UI*/
return spv::ImageFormat::R8ui;
case 0x8D82: /*GL_RGBA32I*/
return spv::ImageFormat::Rgba32i;
case 0x8D88: /*GL_RGBA16I*/
return spv::ImageFormat::Rgba16i;
case 0x8D8E: /*GL_RGBA8I*/
return spv::ImageFormat::Rgba8i;
case 0x823B: /*GL_RG32I*/
return spv::ImageFormat::Rg32i;
case 0x8239: /*GL_RG16I*/
return spv::ImageFormat::Rg16i;
case 0x8237: /*GL_RG8I*/
return spv::ImageFormat::Rg8i;
case 0x8235: /*GL_R32I*/
return spv::ImageFormat::R32i;
case 0x8233: /*GL_R16I*/
return spv::ImageFormat::R16i;
case 0x8231: /*GL_R8I*/
return spv::ImageFormat::R8i;
case 0x8058: /*GL_RGBA8*/
return spv::ImageFormat::Rgba8;
case 0x805B: /*GL_RGBA16*/
return spv::ImageFormat::Rgba16;
case 0x8059: /*GL_RGB10_A2*/
return spv::ImageFormat::Rgb10A2;
case 0x822B: /*GL_RG8*/
return spv::ImageFormat::Rg8;
case 0x822C: /*GL_RG16*/
return spv::ImageFormat::Rg16;
case 0x8229: /*GL_R8*/
return spv::ImageFormat::R8;
case 0x822A: /*GL_R16*/
return spv::ImageFormat::R16;
case 0x8F97: /*GL_RGBA8_SNORM*/
return spv::ImageFormat::Rgba8Snorm;
case 0x8F9B: /*GL_RGBA16_SNORM*/
return spv::ImageFormat::Rgba16Snorm;
case 0x8F95: /*GL_RG8_SNORM*/
return spv::ImageFormat::Rg8Snorm;
case 0x8F99: /*GL_RG16_SNORM*/
return spv::ImageFormat::Rg16Snorm;
case 0x8F94: /*GL_R8_SNORM*/
return spv::ImageFormat::R8Snorm;
case 0x8F98: /*GL_R16_SNORM*/
return spv::ImageFormat::R16Snorm;
default:
return spv::ImageFormat::Unknown;
}
@@ -246,16 +304,26 @@ namespace MobileGL {
Uint GLInternalFormatOfSpirvImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::Rgba32f: return 0x8814; // GL_RGBA32F
case spv::ImageFormat::Rgba16f: return 0x881A; // GL_RGBA16F
case spv::ImageFormat::Rgba8: return 0x8058; // GL_RGBA8
case spv::ImageFormat::Rgba8Snorm: return 0x8F97; // GL_RGBA8_SNORM
case spv::ImageFormat::Rgba32i: return 0x8D82; // GL_RGBA32I
case spv::ImageFormat::Rgba16i: return 0x8D88; // GL_RGBA16I
case spv::ImageFormat::Rgba8i: return 0x8D8E; // GL_RGBA8I
case spv::ImageFormat::Rgba32ui: return 0x8D70; // GL_RGBA32UI
case spv::ImageFormat::Rgba16ui: return 0x8D76; // GL_RGBA16UI
case spv::ImageFormat::Rgba8ui: return 0x8D7C; // GL_RGBA8UI
case spv::ImageFormat::Rgba32f:
return 0x8814; // GL_RGBA32F
case spv::ImageFormat::Rgba16f:
return 0x881A; // GL_RGBA16F
case spv::ImageFormat::Rgba8:
return 0x8058; // GL_RGBA8
case spv::ImageFormat::Rgba8Snorm:
return 0x8F97; // GL_RGBA8_SNORM
case spv::ImageFormat::Rgba32i:
return 0x8D82; // GL_RGBA32I
case spv::ImageFormat::Rgba16i:
return 0x8D88; // GL_RGBA16I
case spv::ImageFormat::Rgba8i:
return 0x8D8E; // GL_RGBA8I
case spv::ImageFormat::Rgba32ui:
return 0x8D70; // GL_RGBA32UI
case spv::ImageFormat::Rgba16ui:
return 0x8D76; // GL_RGBA16UI
case spv::ImageFormat::Rgba8ui:
return 0x8D7C; // GL_RGBA8UI
default:
// Only the carriers need the reverse direction, and every carrier is one
// of the four-channel core formats above.
@@ -336,20 +404,21 @@ namespace MobileGL {
BufferImageSplit SplitOfBufferImageFormat(spv::ImageFormat format) {
switch (format) {
case spv::ImageFormat::Rg32f: return {spv::ImageFormat::R32f, 2};
case spv::ImageFormat::Rg32i: return {spv::ImageFormat::R32i, 2};
case spv::ImageFormat::Rg32ui: return {spv::ImageFormat::R32ui, 2};
case spv::ImageFormat::Rg32f:
return {spv::ImageFormat::R32f, 2};
case spv::ImageFormat::Rg32i:
return {spv::ImageFormat::R32i, 2};
case spv::ImageFormat::Rg32ui:
return {spv::ImageFormat::R32ui, 2};
default:
return {};
}
}
Bool IsSplittableBufferImageType(const Instruction* type,
bool onlyFormatsSpirvCrossRefusesToPrint) {
Bool IsSplittableBufferImageType(const Instruction* type, bool onlyFormatsSpirvCrossRefusesToPrint) {
if (type == nullptr || type->opcode() != spv::Op::OpTypeImage) return false;
if (type->GetSingleWordInOperand(kImageSampledOperand) != kSampledStorageImage) return false;
if (static_cast<spv::Dim>(type->GetSingleWordInOperand(kImageDimOperand)) !=
spv::Dim::Buffer) {
if (static_cast<spv::Dim>(type->GetSingleWordInOperand(kImageDimOperand)) != spv::Dim::Buffer) {
return false;
}
const auto format =
@@ -365,8 +434,7 @@ namespace MobileGL {
return true;
}
Bool IsWidenableStorageImageType(const Instruction* type,
bool onlyFormatsSpirvCrossRefusesToPrint) {
Bool IsWidenableStorageImageType(const Instruction* type, bool onlyFormatsSpirvCrossRefusesToPrint) {
if (type == nullptr || type->opcode() != spv::Op::OpTypeImage) return false;
if (type->GetSingleWordInOperand(kImageSampledOperand) != kSampledStorageImage) return false;
// A BUFFER image is never widened, whatever its format. Widening works because
@@ -384,8 +452,7 @@ namespace MobileGL {
// the application's buffer. It is SPLIT instead where its format allows
// (IsSplittableBufferImageType), which addresses the same bytes rather than
// restriding them, and left alone where it does not.
if (static_cast<spv::Dim>(type->GetSingleWordInOperand(kImageDimOperand)) ==
spv::Dim::Buffer) {
if (static_cast<spv::Dim>(type->GetSingleWordInOperand(kImageDimOperand)) == spv::Dim::Buffer) {
return false;
}
const auto format =
@@ -432,18 +499,18 @@ namespace MobileGL {
// longer describes the function bodies.
struct NormalizedCarrierMaterial {
uint32_t Glsl450Id = 0;
uint32_t FloatTypeId = 0; // the component types, kept only so the declaration
uint32_t IntTypeId = 0; // order below can put each vector after its own
uint32_t UintTypeId = 0; // component - and the last is the image's new Sampled Type
uint32_t UvecTypeId = 0; // uvec4: what an OpImageRead of the carrier yields
uint32_t IvecTypeId = 0; // ivec4: the sign-extended snorm code
uint32_t FvecTypeId = 0; // vec4: what the shader asked for
uint32_t ShiftWidthId = 0; // ivec4(16), the snorm sign extension
uint32_t FloatTypeId = 0; // the component types, kept only so the declaration
uint32_t IntTypeId = 0; // order below can put each vector after its own
uint32_t UintTypeId = 0; // component - and the last is the image's new Sampled Type
uint32_t UvecTypeId = 0; // uvec4: what an OpImageRead of the carrier yields
uint32_t IvecTypeId = 0; // ivec4: the sign-extended snorm code
uint32_t FvecTypeId = 0; // vec4: what the shader asked for
uint32_t ShiftWidthId = 0; // ivec4(16), the snorm sign extension
uint32_t LowWordMaskId = 0; // uvec4(0xFFFF)
uint32_t ZeroId = 0; // vec4(0.0)
uint32_t OneId = 0; // vec4(1.0)
uint32_t MinusOneId = 0; // vec4(-1.0)
uint32_t HalfId = 0; // vec4(0.5)
uint32_t ZeroId = 0; // vec4(0.0)
uint32_t OneId = 0; // vec4(1.0)
uint32_t MinusOneId = 0; // vec4(-1.0)
uint32_t HalfId = 0; // vec4(0.5)
explicit operator bool() const { return UvecTypeId != 0u; }
};
@@ -465,13 +532,12 @@ namespace MobileGL {
uint32_t MakeScalarConstant(IRContext* context, analysis::Type* scalarType, uint32_t word) {
const analysis::Constant* constant = context->get_constant_mgr()->GetConstant(scalarType, {word});
if (constant == nullptr) return 0u;
const Instruction* definition =
context->get_constant_mgr()->GetDefiningInstruction(constant);
const Instruction* definition = context->get_constant_mgr()->GetDefiningInstruction(constant);
return definition == nullptr ? 0u : definition->result_id();
}
uint32_t MakeSplatVec4Constant(IRContext* context, uint32_t vectorTypeId,
analysis::Type* scalarType, uint32_t word) {
uint32_t MakeSplatVec4Constant(IRContext* context, uint32_t vectorTypeId, analysis::Type* scalarType,
uint32_t word) {
const uint32_t scalarId = MakeScalarConstant(context, scalarType, word);
if (scalarId == 0u) return 0u;
const uint32_t componentIds[4] = {scalarId, scalarId, scalarId, scalarId};
@@ -567,8 +633,8 @@ namespace MobileGL {
if (floatReg == nullptr) return 0u;
uint32_t componentIds[4] = {0u, 0u, 0u, 0u};
for (uint32_t i = 0; i < 4; ++i) {
componentIds[i] = MakeScalarConstant(context, floatReg,
FloatBits(static_cast<float>(channelMax[i])));
componentIds[i] =
MakeScalarConstant(context, floatReg, FloatBits(static_cast<float>(channelMax[i])));
if (componentIds[i] == 0u) return 0u;
}
return MakeVec4Constant(context, material.FvecTypeId, componentIds);
@@ -576,32 +642,32 @@ namespace MobileGL {
} // namespace
Uint WidenImageFormatsPass::WidenedCoreEsslImageFormat(Uint glInternalFormat) {
const ImageFormatWidening widening =
WideningOfSpirvImageFormat(SpirvImageFormatOfGL(glInternalFormat));
const ImageFormatWidening widening = WideningOfSpirvImageFormat(SpirvImageFormatOfGL(glInternalFormat));
if (!widening) return 0;
return GLInternalFormatOfSpirvImageFormat(widening.Carrier);
}
Uint WidenImageFormatsPass::SplitCoreEsslBufferImageFormat(Uint glInternalFormat) {
const BufferImageSplit split =
SplitOfBufferImageFormat(SpirvImageFormatOfGL(glInternalFormat));
const BufferImageSplit split = SplitOfBufferImageFormat(SpirvImageFormatOfGL(glInternalFormat));
if (!split) return 0;
switch (split.Base) {
case spv::ImageFormat::R32f: return 0x822E; // GL_R32F
case spv::ImageFormat::R32i: return 0x8235; // GL_R32I
case spv::ImageFormat::R32ui: return 0x8236; // GL_R32UI
case spv::ImageFormat::R32f:
return 0x822E; // GL_R32F
case spv::ImageFormat::R32i:
return 0x8235; // GL_R32I
case spv::ImageFormat::R32ui:
return 0x8236; // GL_R32UI
default:
return 0;
}
}
bool WidenImageFormatsPass::NormalizedImageCarrierCodes(Uint glInternalFormat,
Uint32 (&outChannelMax)[4],
bool WidenImageFormatsPass::NormalizedImageCarrierCodes(Uint glInternalFormat, Uint32 (&outChannelMax)[4],
bool& outSignedNormalized) {
const ImageFormatWidening widening =
WideningOfSpirvImageFormat(SpirvImageFormatOfGL(glInternalFormat));
const ImageFormatWidening widening = WideningOfSpirvImageFormat(SpirvImageFormatOfGL(glInternalFormat));
if (!widening || !widening.CarriesNormalizedCodes()) return false;
for (Uint i = 0; i < 4; ++i) outChannelMax[i] = widening.ChannelMax[i];
for (Uint i = 0; i < 4; ++i)
outChannelMax[i] = widening.ChannelMax[i];
outSignedNormalized = widening.SignedNormalized;
return true;
}
@@ -610,8 +676,8 @@ namespace MobileGL {
return ChannelsOfSpirvImageFormat(SpirvImageFormatOfGL(glInternalFormat));
}
bool WidenImageFormatsPass::DeclaresWidenableImageFormat(
IRContext* context, const bool onlyFormatsSpirvCrossRefusesToPrint) {
bool WidenImageFormatsPass::DeclaresWidenableImageFormat(IRContext* context,
const bool onlyFormatsSpirvCrossRefusesToPrint) {
if (context == nullptr) {
return false;
}
@@ -624,8 +690,8 @@ namespace MobileGL {
return false;
}
bool WidenImageFormatsPass::DeclaresWidenableImageFormat(
const Vector<Uint32>& binary, const bool onlyFormatsSpirvCrossRefusesToPrint) {
bool WidenImageFormatsPass::DeclaresWidenableImageFormat(const Vector<Uint32>& binary,
const bool onlyFormatsSpirvCrossRefusesToPrint) {
std::unique_ptr<IRContext> context = spvtools::BuildModule(
SPV_ENV_VULKAN_1_1, [](spv_message_level_t, const char*, const spv_position_t&, const char*) {},
binary.data(), binary.size());
@@ -670,11 +736,10 @@ namespace MobileGL {
static_cast<spv::ImageFormat>(type->GetSingleWordInOperand(kImageFormatOperand));
const ImageFormatWidening widening = WideningOfSpirvImageFormat(format);
anyNormalizedCarrier = anyNormalizedCarrier || widening.CarriesNormalizedCodes();
widenedByTypeId.emplace(
type->result_id(),
WidenedImage{widening.Carrier, widening.Channels,
type->GetSingleWordInOperand(kImageSampledTypeOperand), 0u,
widening.SignedNormalized});
widenedByTypeId.emplace(type->result_id(),
WidenedImage{widening.Carrier, widening.Channels,
type->GetSingleWordInOperand(kImageSampledTypeOperand), 0u,
widening.SignedNormalized});
}
// Collect the accesses BEFORE anything is mutated, and refuse the whole rewrite if
@@ -689,10 +754,8 @@ namespace MobileGL {
// stage that addresses one image right and the other wrong.
std::map<uint32_t, BufferImageSplit> splitByTypeId;
for (Instruction* type : bufferSplitTypes) {
splitByTypeId.emplace(
type->result_id(),
SplitOfBufferImageFormat(
static_cast<spv::ImageFormat>(type->GetSingleWordInOperand(kImageFormatOperand))));
splitByTypeId.emplace(type->result_id(), SplitOfBufferImageFormat(static_cast<spv::ImageFormat>(
type->GetSingleWordInOperand(kImageFormatOperand))));
}
std::vector<Instruction*> reads;
@@ -701,8 +764,8 @@ namespace MobileGL {
std::vector<Instruction*> splitWrites;
std::vector<Instruction*> splitSizeQueries;
Bool rewritable = true;
for (auto funcIt = irContext->module()->begin();
funcIt != irContext->module()->end() && rewritable; ++funcIt) {
for (auto funcIt = irContext->module()->begin(); funcIt != irContext->module()->end() && rewritable;
++funcIt) {
funcIt->ForEachInst([&](Instruction* inst) {
if (!rewritable) return;
switch (inst->opcode()) {
@@ -728,12 +791,18 @@ namespace MobileGL {
}
if (splitByTypeId.count(imageTypeId) != 0) {
switch (inst->opcode()) {
case spv::Op::OpImageRead: splitReads.push_back(inst); return;
case spv::Op::OpImageWrite: splitWrites.push_back(inst); return;
case spv::Op::OpImageRead:
splitReads.push_back(inst);
return;
case spv::Op::OpImageWrite:
splitWrites.push_back(inst);
return;
// imageSize() has to be halved with everything else: the ES view has
// twice the texels the application's format describes, and a shader
// that walks the buffer by its own size would run off the end of it.
case spv::Op::OpImageQuerySize: splitSizeQueries.push_back(inst); return;
case spv::Op::OpImageQuerySize:
splitSizeQueries.push_back(inst);
return;
default:
rewritable = false;
return;
@@ -787,15 +856,14 @@ namespace MobileGL {
uint32_t oneWord = 0;
std::unique_ptr<analysis::Type> component;
if (sampledType->opcode() == spv::Op::OpTypeFloat &&
sampledType->GetSingleWordInOperand(0) == 32) {
if (sampledType->opcode() == spv::Op::OpTypeFloat && sampledType->GetSingleWordInOperand(0) == 32) {
component = spvtools::MakeUnique<analysis::Float>(32);
oneWord = 0x3F800000u; // 1.0f
} else if (sampledType->opcode() == spv::Op::OpTypeInt &&
sampledType->GetSingleWordInOperand(0) == 32) {
// OpTypeInt in-operands: 0 width, 1 signedness.
component = spvtools::MakeUnique<analysis::Integer>(
32, sampledType->GetSingleWordInOperand(1) != 0);
component =
spvtools::MakeUnique<analysis::Integer>(32, sampledType->GetSingleWordInOperand(1) != 0);
oneWord = 1u;
} else {
return false;
@@ -823,9 +891,9 @@ namespace MobileGL {
if (vectorReg == nullptr) return false;
// A vector constant's "literal words" are the IDS of its components
// (ConstantManager::CreateConstant -> GetConstantsFromIds).
const analysis::Constant* zeroOne = constantMgr->GetConstant(
vectorReg, {zeroInst->result_id(), zeroInst->result_id(), zeroInst->result_id(),
oneInst->result_id()});
const analysis::Constant* zeroOne =
constantMgr->GetConstant(vectorReg, {zeroInst->result_id(), zeroInst->result_id(),
zeroInst->result_id(), oneInst->result_id()});
if (zeroOne == nullptr) return false;
const Instruction* zeroOneInst = constantMgr->GetDefiningInstruction(zeroOne);
if (zeroOneInst == nullptr) return false;
@@ -845,8 +913,7 @@ namespace MobileGL {
std::vector<Operand> components;
components.reserve(4);
for (uint32_t i = 0; i < 4; ++i) {
components.push_back(
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {i < channels ? i : 4u + i}});
components.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i < channels ? i : 4u + i}});
}
return components;
};
@@ -925,8 +992,7 @@ namespace MobileGL {
{SPV_OPERAND_TYPE_ID, {componentsId}}}));
before->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpIAdd, coord->type_id(), secondId,
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {firstId}},
{SPV_OPERAND_TYPE_ID, {oneId}}}));
Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {firstId}}, {SPV_OPERAND_TYPE_ID, {oneId}}}));
outFirst = firstId;
outSecond = secondId;
return true;
@@ -951,8 +1017,8 @@ namespace MobileGL {
for (Instruction* type : bufferSplitTypes) {
uint32_t unusedConstantId = 0;
uint32_t unusedVec4TypeId = 0;
if (!resolveMaskMaterial(type->GetSingleWordInOperand(kImageSampledTypeOperand),
unusedConstantId, unusedVec4TypeId)) {
if (!resolveMaskMaterial(type->GetSingleWordInOperand(kImageSampledTypeOperand), unusedConstantId,
unusedVec4TypeId)) {
return Status::SuccessWithoutChange;
}
}
@@ -1016,11 +1082,10 @@ namespace MobileGL {
// just exist. See HoistTypeDeclarationsBefore: the type manager appends, and
// the image type that names the new uint32 is already further up.
if (firstNormalizedImageType != nullptr) {
HoistTypeDeclarationsBefore(
irContext, firstNormalizedImageType,
{normalizedMaterial.FloatTypeId, normalizedMaterial.IntTypeId,
normalizedMaterial.UintTypeId, normalizedMaterial.FvecTypeId,
normalizedMaterial.IvecTypeId, normalizedMaterial.UvecTypeId});
HoistTypeDeclarationsBefore(irContext, firstNormalizedImageType,
{normalizedMaterial.FloatTypeId, normalizedMaterial.IntTypeId,
normalizedMaterial.UintTypeId, normalizedMaterial.FvecTypeId,
normalizedMaterial.IvecTypeId, normalizedMaterial.UvecTypeId});
}
}
@@ -1047,14 +1112,13 @@ namespace MobileGL {
// two-channel format does not have: the mask shuffle runs on the float side either
// way, so whatever the surplus channels hold is discarded on the way out and
// written as the format's own 0 and 1 on the way in.
auto insertUnpack = [&](Instruction* before, const WidenedImage& widened,
uint32_t rawId) -> uint32_t {
auto insertUnpack = [&](Instruction* before, const WidenedImage& widened, uint32_t rawId) -> uint32_t {
const auto emit = [&](spv::Op opcode, uint32_t typeId,
Instruction::OperandList operands) -> uint32_t {
const uint32_t resultId = irContext->TakeNextId();
if (resultId == 0u) return 0u;
before->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, opcode, typeId, resultId,
Move(operands)));
before->InsertBefore(
spvtools::MakeUnique<Instruction>(irContext, opcode, typeId, resultId, Move(operands)));
return resultId;
};
// The raw texel through an OpCopyObject before anything reads it, which costs
@@ -1069,19 +1133,16 @@ namespace MobileGL {
// the emitter does; the copy is the cheapest way to stay out of it, and every
// other driver folds it away.
const uint32_t texelId =
emit(spv::Op::OpCopyObject, normalizedMaterial.UvecTypeId,
{{SPV_OPERAND_TYPE_ID, {rawId}}});
emit(spv::Op::OpCopyObject, normalizedMaterial.UvecTypeId, {{SPV_OPERAND_TYPE_ID, {rawId}}});
if (texelId == 0u) return 0u;
uint32_t codeId = 0;
if (widened.SignedNormalized) {
const uint32_t asIntId =
emit(spv::Op::OpBitcast, normalizedMaterial.IvecTypeId,
{{SPV_OPERAND_TYPE_ID, {texelId}}});
emit(spv::Op::OpBitcast, normalizedMaterial.IvecTypeId, {{SPV_OPERAND_TYPE_ID, {texelId}}});
if (asIntId == 0u) return 0u;
const uint32_t shiftedUpId =
emit(spv::Op::OpShiftLeftLogical, normalizedMaterial.IvecTypeId,
{{SPV_OPERAND_TYPE_ID, {asIntId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.ShiftWidthId}}});
const uint32_t shiftedUpId = emit(spv::Op::OpShiftLeftLogical, normalizedMaterial.IvecTypeId,
{{SPV_OPERAND_TYPE_ID, {asIntId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.ShiftWidthId}}});
if (shiftedUpId == 0u) return 0u;
const uint32_t signExtendedId =
emit(spv::Op::OpShiftRightArithmetic, normalizedMaterial.IvecTypeId,
@@ -1108,30 +1169,27 @@ namespace MobileGL {
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.MinusOneId}}});
};
auto insertPack = [&](Instruction* before, const WidenedImage& widened,
uint32_t valueId) -> uint32_t {
auto insertPack = [&](Instruction* before, const WidenedImage& widened, uint32_t valueId) -> uint32_t {
const auto emit = [&](spv::Op opcode, uint32_t typeId,
Instruction::OperandList operands) -> uint32_t {
const uint32_t resultId = irContext->TakeNextId();
if (resultId == 0u) return 0u;
before->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, opcode, typeId, resultId,
Move(operands)));
before->InsertBefore(
spvtools::MakeUnique<Instruction>(irContext, opcode, typeId, resultId, Move(operands)));
return resultId;
};
const uint32_t lowBoundId =
widened.SignedNormalized ? normalizedMaterial.MinusOneId : normalizedMaterial.ZeroId;
const uint32_t clampedId =
emit(spv::Op::OpExtInst, normalizedMaterial.FvecTypeId,
{{SPV_OPERAND_TYPE_ID, {normalizedMaterial.Glsl450Id}},
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {kGlslFClamp}},
{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_ID, {lowBoundId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.OneId}}});
const uint32_t clampedId = emit(spv::Op::OpExtInst, normalizedMaterial.FvecTypeId,
{{SPV_OPERAND_TYPE_ID, {normalizedMaterial.Glsl450Id}},
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {kGlslFClamp}},
{SPV_OPERAND_TYPE_ID, {valueId}},
{SPV_OPERAND_TYPE_ID, {lowBoundId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.OneId}}});
if (clampedId == 0u) return 0u;
const uint32_t scaledId =
emit(spv::Op::OpFMul, normalizedMaterial.FvecTypeId,
{{SPV_OPERAND_TYPE_ID, {clampedId}},
{SPV_OPERAND_TYPE_ID, {widened.DenominatorId}}});
{{SPV_OPERAND_TYPE_ID, {clampedId}}, {SPV_OPERAND_TYPE_ID, {widened.DenominatorId}}});
if (scaledId == 0u) return 0u;
uint32_t biasId = normalizedMaterial.HalfId;
if (widened.SignedNormalized) {
@@ -1142,9 +1200,9 @@ namespace MobileGL {
{SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {kGlslFSign}},
{SPV_OPERAND_TYPE_ID, {scaledId}}});
if (signId == 0u) return 0u;
biasId = emit(spv::Op::OpFMul, normalizedMaterial.FvecTypeId,
{{SPV_OPERAND_TYPE_ID, {signId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.HalfId}}});
biasId =
emit(spv::Op::OpFMul, normalizedMaterial.FvecTypeId,
{{SPV_OPERAND_TYPE_ID, {signId}}, {SPV_OPERAND_TYPE_ID, {normalizedMaterial.HalfId}}});
if (biasId == 0u) return 0u;
}
const uint32_t roundedId =
@@ -1161,9 +1219,9 @@ namespace MobileGL {
const uint32_t asUintId = emit(spv::Op::OpBitcast, normalizedMaterial.UvecTypeId,
{{SPV_OPERAND_TYPE_ID, {signedCodeId}}});
if (asUintId == 0u) return 0u;
return emit(spv::Op::OpBitwiseAnd, normalizedMaterial.UvecTypeId,
{{SPV_OPERAND_TYPE_ID, {asUintId}},
{SPV_OPERAND_TYPE_ID, {normalizedMaterial.LowWordMaskId}}});
return emit(
spv::Op::OpBitwiseAnd, normalizedMaterial.UvecTypeId,
{{SPV_OPERAND_TYPE_ID, {asUintId}}, {SPV_OPERAND_TYPE_ID, {normalizedMaterial.LowWordMaskId}}});
};
// Masks first, while every image type still carries its ORIGINAL format: the
@@ -1201,8 +1259,8 @@ namespace MobileGL {
for (const Operand& component : maskComponents(widened->Channels)) {
shuffleOperands.push_back(component);
}
write->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVectorShuffle, vec4TypeId, maskedId, shuffleOperands));
write->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpVectorShuffle,
vec4TypeId, maskedId, shuffleOperands));
storedId = maskedId;
}
// The pack goes AFTER the mask, so the carrier's surplus channels are written
@@ -1246,8 +1304,8 @@ namespace MobileGL {
readOperands.push_back(read->GetInOperand(i));
}
read->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpImageRead,
normalized ? normalizedMaterial.UvecTypeId : vec4TypeId, rawReadId, readOperands));
irContext, spv::Op::OpImageRead, normalized ? normalizedMaterial.UvecTypeId : vec4TypeId,
rawReadId, readOperands));
uint32_t loadedId = rawReadId;
if (normalized) {
loadedId = insertUnpack(read, *widened, rawReadId);
@@ -1308,8 +1366,8 @@ namespace MobileGL {
shuffleOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {4u}});
shuffleOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {4u}});
shuffleOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {7u}});
write->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVectorShuffle, vec4TypeId, maskedId, shuffleOperands));
write->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpVectorShuffle,
vec4TypeId, maskedId, shuffleOperands));
componentTexelIds[component] = maskedId;
}
// The FIRST component's write is the inserted one and the second is the
@@ -1321,8 +1379,8 @@ namespace MobileGL {
}
firstWriteOperands[kImageAccessCoordinateOperand] = {SPV_OPERAND_TYPE_ID, {firstCoordId}};
firstWriteOperands[kImageWriteTexelOperand] = {SPV_OPERAND_TYPE_ID, {componentTexelIds[0]}};
write->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpImageWrite, 0, 0, firstWriteOperands));
write->InsertBefore(
spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpImageWrite, 0, 0, firstWriteOperands));
write->SetInOperand(kImageAccessCoordinateOperand, {secondCoordId});
write->SetInOperand(kImageWriteTexelOperand, {componentTexelIds[1]});
}
@@ -1368,8 +1426,8 @@ namespace MobileGL {
gatherOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {4u}});
gatherOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}});
gatherOperands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {0u}});
read->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpVectorShuffle, vec4TypeId, gatheredId, gatherOperands));
read->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpVectorShuffle,
vec4TypeId, gatheredId, gatherOperands));
read->SetOpcode(spv::Op::OpVectorShuffle);
Instruction::OperandList maskOperands{{SPV_OPERAND_TYPE_ID, {gatheredId}},
@@ -1398,12 +1456,10 @@ namespace MobileGL {
for (uint32_t i = 0; i < query->NumInOperands(); ++i) {
queryOperands.push_back(query->GetInOperand(i));
}
query->InsertBefore(spvtools::MakeUnique<Instruction>(
irContext, spv::Op::OpImageQuerySize, query->type_id(), rawSizeId, queryOperands));
query->SetOpcode(resultType->GetSingleWordInOperand(1) != 0 ? spv::Op::OpSDiv
: spv::Op::OpUDiv);
query->SetInOperands({{SPV_OPERAND_TYPE_ID, {rawSizeId}},
{SPV_OPERAND_TYPE_ID, {componentsId}}});
query->InsertBefore(spvtools::MakeUnique<Instruction>(irContext, spv::Op::OpImageQuerySize,
query->type_id(), rawSizeId, queryOperands));
query->SetOpcode(resultType->GetSingleWordInOperand(1) != 0 ? spv::Op::OpSDiv : spv::Op::OpUDiv);
query->SetInOperands({{SPV_OPERAND_TYPE_ID, {rawSizeId}}, {SPV_OPERAND_TYPE_ID, {componentsId}}});
}
for (Instruction* type : bufferSplitTypes) {
@@ -133,8 +133,7 @@ namespace MobileGL {
// %res makes every downstream use pick the zero up with no further rewriting.
for (size_t i = 0; i < baseVertexLoads.size(); ++i) {
baseVertexLoads[i]->SetOpcode(spv::Op::OpCopyObject);
baseVertexLoads[i]->SetInOperands(Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {zeroIds[i]}}});
baseVertexLoads[i]->SetInOperands(Instruction::OperandList{{SPV_OPERAND_TYPE_ID, {zeroIds[i]}}});
}
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
@@ -95,7 +95,8 @@ namespace MobileGL {
if (valueTypeId == 0 || zeroInst == nullptr) return Status::Failure;
const uint32_t zeroId = zeroInst->result_id();
for (Instruction* store : stores) store->SetInOperand(1, {zeroId});
for (Instruction* store : stores)
store->SetInOperand(1, {zeroId});
irContext->InvalidateAnalysesExceptFor(IRContext::kAnalysisNone);
return Status::SuccessWithChange;
@@ -21,27 +21,41 @@ namespace MobileGL {
auto signedness = member.numeric.scalar.signedness;
if (flags & SPV_REFLECT_TYPE_FLAG_FLOAT) {
switch (width) {
case 16: return SPVC_BASETYPE_FP16;
case 32: return SPVC_BASETYPE_FP32;
case 64: return SPVC_BASETYPE_FP64;
default: return SPVC_BASETYPE_UNKNOWN;
case 16:
return SPVC_BASETYPE_FP16;
case 32:
return SPVC_BASETYPE_FP32;
case 64:
return SPVC_BASETYPE_FP64;
default:
return SPVC_BASETYPE_UNKNOWN;
}
} else if (flags & SPV_REFLECT_TYPE_FLAG_INT) {
if (signedness) {
switch (width) {
case 8: return SPVC_BASETYPE_INT8;
case 16: return SPVC_BASETYPE_INT16;
case 32: return SPVC_BASETYPE_INT32;
case 64: return SPVC_BASETYPE_INT64;
default: return SPVC_BASETYPE_UNKNOWN;
case 8:
return SPVC_BASETYPE_INT8;
case 16:
return SPVC_BASETYPE_INT16;
case 32:
return SPVC_BASETYPE_INT32;
case 64:
return SPVC_BASETYPE_INT64;
default:
return SPVC_BASETYPE_UNKNOWN;
}
} else {
switch (width) {
case 8: return SPVC_BASETYPE_UINT8;
case 16: return SPVC_BASETYPE_UINT16;
case 32: return SPVC_BASETYPE_UINT32;
case 64: return SPVC_BASETYPE_UINT64;
default: return SPVC_BASETYPE_UNKNOWN;
case 8:
return SPVC_BASETYPE_UINT8;
case 16:
return SPVC_BASETYPE_UINT16;
case 32:
return SPVC_BASETYPE_UINT32;
case 64:
return SPVC_BASETYPE_UINT64;
default:
return SPVC_BASETYPE_UNKNOWN;
}
}
} else if (flags & SPV_REFLECT_TYPE_FLAG_BOOL) {
@@ -107,8 +121,8 @@ namespace MobileGL {
const Uint32 extent = member.array.dims[dim];
if (extent == 0) {
MGLOG_W_ONCE("RecordGlobalUboLeaf: multi-dimensional uniform '%s' has a non-constant "
"dimension, recording the base entry only",
name.c_str());
"dimension, recording the base entry only",
name.c_str());
RecordGlobalUboLeafEntry(member, name, offsetInUBO, arrayStride, member.size, metadata);
return;
}
@@ -127,8 +141,8 @@ namespace MobileGL {
for (const Uint32 index : indices) {
elementName += "[" + std::to_string(index) + "]";
}
RecordGlobalUboLeafEntry(member, elementName, offsetInUBO + subArray * subArrayStride,
arrayStride, subArrayStride, metadata);
RecordGlobalUboLeafEntry(member, elementName, offsetInUBO + subArray * subArrayStride, arrayStride,
subArrayStride, metadata);
for (SizeT dim = indices.size(); dim-- > 0;) {
if (++indices[dim] < member.array.dims[dim]) break;
indices[dim] = 0;
@@ -165,8 +179,8 @@ namespace MobileGL {
// Arrays of arrays of structs cannot be declared in the GL 3.3-era GLSL
// MobileGL ingests; record the base so at least element 0 resolves.
MGLOG_W_ONCE("FlattenGlobalUboMember: multi-dimensional struct array '%s' is not supported, "
"flattening element 0 only",
name.c_str());
"flattening element 0 only",
name.c_str());
}
const Uint32 elementCount = member.array.dims[0] > 0 ? member.array.dims[0] : 1;
@@ -180,8 +194,7 @@ namespace MobileGL {
}
}
SpvcSession::SpvcSession(const Vector<unsigned int>& spirv, Flags<SessionUsageBit> usage)
: usage(usage) {
SpvcSession::SpvcSession(const Vector<unsigned int>& spirv, Flags<SessionUsageBit> usage) : usage(usage) {
if (usage & SessionUsageBit::Transpile) {
const SpvId* p_spirv = spirv.data();
size_t word_count = spirv.size();
@@ -200,8 +213,8 @@ namespace MobileGL {
ir = nullptr;
return;
}
if (spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir,
SPVC_CAPTURE_MODE_TAKE_OWNERSHIP, &compiler) != SPVC_SUCCESS) {
if (spvc_context_create_compiler(context, SPVC_BACKEND_GLSL, ir, SPVC_CAPTURE_MODE_TAKE_OWNERSHIP,
&compiler) != SPVC_SUCCESS) {
compiler = nullptr;
return;
}
@@ -210,8 +223,8 @@ namespace MobileGL {
return;
}
} else if (usage & SessionUsageBit::Reflection) {
SpvReflectResult result = spvReflectCreateShaderModule(
spirv.size() * sizeof(uint32_t), spirv.data(), &reflectModule);
SpvReflectResult result =
spvReflectCreateShaderModule(spirv.size() * sizeof(uint32_t), spirv.data(), &reflectModule);
reflectModuleValid = (result == SPV_REFLECT_RESULT_SUCCESS);
}
}
@@ -382,8 +395,8 @@ namespace MobileGL {
SPVC_CHK_INIT
const spvc_reflected_resource* list = nullptr;
size_t count = 0;
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(
resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER, &list, &count));
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER,
&list, &count));
for (size_t i = 0; i < count; ++i) {
auto& resource = list[i];
// Two spellings, because neither one alone identifies the block the GL
@@ -438,15 +451,15 @@ namespace MobileGL {
SPVC_CHK_INIT
const spvc_reflected_resource* list = nullptr;
size_t count = 0;
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(
resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER, &list, &count));
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER,
&list, &count));
for (size_t i = 0; i < count; ++i) {
auto& resource = list[i];
// The block TYPE name: glslang gives the synthesized block an EMPTY instance
// name, so resource.name carries nothing to match on. Read before Compile(),
// which is where SPIRV-Cross renames the reserved "gl_" prefix away.
const Int glBinding = AtomicCounterBlockBinding(
spvc_compiler_get_name(compiler, resource.base_type_id));
const Int glBinding =
AtomicCounterBlockBinding(spvc_compiler_get_name(compiler, resource.base_type_id));
if (glBinding < 0) continue;
const Int esslBinding = topBinding - glBinding;
if (esslBinding < 0) {
@@ -468,8 +481,8 @@ namespace MobileGL {
SPVC_CHK_INIT
const spvc_reflected_resource* list = nullptr;
size_t count = 0;
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(
resources, SPVC_RESOURCE_TYPE_STAGE_OUTPUT, &list, &count));
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_STAGE_OUTPUT,
&list, &count));
Bool dualSourceIndex = false;
for (size_t i = 0; i < count; ++i) {
const spvc_reflected_resource& resource = list[i];
@@ -495,8 +508,8 @@ namespace MobileGL {
SPVC_CHK_INIT
const spvc_reflected_resource* list = nullptr;
size_t count = 0;
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(
resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER, &list, &count));
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(resources, SPVC_RESOURCE_TYPE_STORAGE_BUFFER,
&list, &count));
for (size_t i = 0; i < count; ++i) {
const spvc_reflected_resource& resource = list[i];
// The variable itself, for a block the application qualified as a whole.
@@ -540,8 +553,7 @@ namespace MobileGL {
}
// A double occupies two component slots per scalar (GL 4.6 core 11.1.2.1).
const spvc_basetype base = spvc_type_get_basetype(type);
if (base == SPVC_BASETYPE_FP64 || base == SPVC_BASETYPE_INT64 ||
base == SPVC_BASETYPE_UINT64) {
if (base == SPVC_BASETYPE_FP64 || base == SPVC_BASETYPE_INT64 || base == SPVC_BASETYPE_UINT64) {
components *= 2;
}
return components;
@@ -627,12 +639,12 @@ namespace MobileGL {
// GL's capture interface spells an application block's member
// "Block.member"; a redeclared built-in block contributes its members
// by their own names, which the built-in walk below handles.
capture.name = blockName.empty() ? String(memberName)
: blockName + "." + String(memberName);
capture.name =
blockName.empty() ? String(memberName) : blockName + "." + String(memberName);
capture.buffer = buffer;
capture.stride = stride;
capture.offset = spvc_compiler_get_member_decoration(compiler, output.base_type_id,
member, SpvDecorationOffset);
capture.offset = spvc_compiler_get_member_decoration(compiler, output.base_type_id, member,
SpvDecorationOffset);
capture.componentCount =
XfbComponentCount(compiler, spvc_type_get_member_type(type, member));
captures.push_back(Move(capture));
@@ -648,8 +660,7 @@ namespace MobileGL {
const spvc_reflected_builtin_resource* builtins = nullptr;
SizeT builtinCount = 0;
if (spvc_resources_get_builtin_resource_list_for_type(
resources, SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT, &builtins, &builtinCount) ==
SPVC_SUCCESS) {
resources, SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT, &builtins, &builtinCount) == SPVC_SUCCESS) {
for (SizeT i = 0; i < builtinCount; ++i) {
const spvc_reflected_builtin_resource& entry = builtins[i];
const char* name = XfbBuiltInName(entry.builtin);
@@ -661,8 +672,7 @@ namespace MobileGL {
const spvc_type blockType =
spvc_compiler_get_type_handle(compiler, entry.resource.base_type_id);
if (blockType == nullptr ||
spvc_type_get_basetype(blockType) != SPVC_BASETYPE_STRUCT) {
if (blockType == nullptr || spvc_type_get_basetype(blockType) != SPVC_BASETYPE_STRUCT) {
continue;
}
// The member index is not in the reflection entry, so it is recovered by
@@ -680,14 +690,14 @@ namespace MobileGL {
}
if (spvc_compiler_has_member_decoration(compiler, entry.resource.base_type_id, member,
SpvDecorationOffset) != SPVC_TRUE) {
break; // this built-in is present but not captured
break; // this built-in is present but not captured
}
SpirvXfbCapture capture;
capture.name = name;
capture.buffer = buffer;
capture.stride = stride;
capture.offset = spvc_compiler_get_member_decoration(
compiler, entry.resource.base_type_id, member, SpvDecorationOffset);
capture.offset = spvc_compiler_get_member_decoration(compiler, entry.resource.base_type_id,
member, SpvDecorationOffset);
capture.componentCount =
XfbComponentCount(compiler, spvc_type_get_member_type(blockType, member));
captures.push_back(Move(capture));
@@ -736,8 +746,7 @@ namespace MobileGL {
const spvc_reflected_builtin_resource* builtins = nullptr;
SizeT builtinCount = 0;
if (spvc_resources_get_builtin_resource_list_for_type(
resources, SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT, &builtins, &builtinCount) ==
SPVC_SUCCESS) {
resources, SPVC_BUILTIN_RESOURCE_TYPE_STAGE_OUTPUT, &builtins, &builtinCount) == SPVC_SUCCESS) {
for (SizeT i = 0; i < builtinCount; ++i) {
stripVariable(builtins[i].resource.id, builtins[i].resource.base_type_id);
}
@@ -834,7 +843,8 @@ namespace MobileGL {
const String word(keyword);
for (SizeT at = head.find(word); at != String::npos; at = head.find(word, at + 1)) {
const Bool startOk = at == 0 || !IsIdentifierChar(head[at - 1]);
const Bool endOk = at + word.size() >= head.size() || !IsIdentifierChar(head[at + word.size()]);
const Bool endOk =
at + word.size() >= head.size() || !IsIdentifierChar(head[at + word.size()]);
if (startOk && endOk) return true;
}
}
@@ -885,8 +895,8 @@ namespace MobileGL {
while (lineBegin < essl.size()) {
SizeT lineEnd = essl.find('\n', lineBegin);
const SizeT next = lineEnd == String::npos ? essl.size() : lineEnd + 1;
const String line = essl.substr(lineBegin, (lineEnd == String::npos ? essl.size() : lineEnd) -
lineBegin);
const String line =
essl.substr(lineBegin, (lineEnd == String::npos ? essl.size() : lineEnd) - lineBegin);
if (!IsInterfaceDeclarationOf(line, sentinel)) {
rewritten.append(essl, lineBegin, next - lineBegin);
}
@@ -930,7 +940,7 @@ namespace MobileGL {
size_t count = 0;
SPVC_CHK_RESULT(spvc_resources_get_resource_list_for_type(
resources, SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, &list, &count);)
resources, SPVC_RESOURCE_TYPE_UNIFORM_BUFFER, &list, &count);)
for (size_t i = 0; i < count; ++i) {
if (spvc_compiler_has_decoration(compiler, list[i].id, SpvDecorationBuiltIn)) {
continue;
@@ -62,11 +62,11 @@ namespace MobileGL {
// so a module that carries them and an implementation that ignores them capture
// nothing at all.
struct SpirvXfbCapture {
String name; // the GL interface name: "gl_Position", or "Block.member"
Uint32 buffer = 0; // XfbBuffer on the declaring variable
Uint32 offset = 0; // Offset on the variable or on the member
Uint32 stride = 0; // XfbStride on the declaring variable
Uint32 componentCount = 0; // how many 32-bit components the capture occupies
String name; // the GL interface name: "gl_Position", or "Block.member"
Uint32 buffer = 0; // XfbBuffer on the declaring variable
Uint32 offset = 0; // Offset on the variable or on the member
Uint32 stride = 0; // XfbStride on the declaring variable
Uint32 componentCount = 0; // how many 32-bit components the capture occupies
};
enum class SessionUsageBit {
@@ -90,8 +90,7 @@ namespace MobileGL {
public:
SpvcSession() = default;
explicit SpvcSession(const Vector<unsigned int>& spirv,
Flags<SessionUsageBit> usage);
explicit SpvcSession(const Vector<unsigned int>& spirv, Flags<SessionUsageBit> usage);
SpvcSession(SpvcSession&) = delete;
@@ -222,8 +221,7 @@ namespace MobileGL {
// sets `outUnknownConstantId` when an id the caller passed is not a
// specialization constant of this module, which the extension makes
// GL_INVALID_VALUE.
Bool SetSpecializationConstants(const Vector<Uint32>& constantIds,
const Vector<Uint32>& constantValues,
Bool SetSpecializationConstants(const Vector<Uint32>& constantIds, const Vector<Uint32>& constantValues,
Uint32& outUnknownConstantId);
spvc_result Compile(const char** result);
const SpvcMetadata& GetMetadata() const;
@@ -14,8 +14,8 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
namespace {
// Tags keep two different key builders from ever producing the same blob,
// even if their inputs happened to serialize identically.
constexpr Uint32 kSpirvKeyTag = 0x4d474c31u; // "MGL1"
constexpr Uint32 kEsslKeyTag = 0x4d474c32u; // "MGL2"
constexpr Uint32 kSpirvKeyTag = 0x4d474c31u; // "MGL1"
constexpr Uint32 kEsslKeyTag = 0x4d474c32u; // "MGL2"
constexpr Uint32 kParseVerdictKeyTag = 0x4d474c43u; // "MGLC" - L1c, the compile half
// Bumped whenever the SHAPE of a key changes (a field added, a field's
@@ -102,7 +102,8 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
void TranslationKeyBuilder::TextList(const Vector<String>& values) {
Value(static_cast<Uint64>(values.size()));
for (const String& value : values) Text(value);
for (const String& value : values)
Text(value);
}
void TranslationKeyBuilder::StringMap(const std::map<String, String>& map) {
@@ -115,7 +116,8 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
void TranslationKeyBuilder::NameSet(const std::set<String>& names) {
Value(static_cast<Uint64>(names.size()));
for (const String& name : names) Text(name);
for (const String& name : names)
Text(name);
}
TranslationCacheKey MakeTranslationCacheKey(String blob) {
@@ -170,9 +172,9 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
// Leaked for the same exit-order reason as the other two; see the note below.
BoundedTranslationCache<ShaderParseVerdict>& GetShaderParseVerdictCache() {
static auto* const kCache = new BoundedTranslationCache<ShaderParseVerdict>(
"ShaderTranslationCache L1c (GLSL->parse verdict)", kParseVerdictCacheMaxEntries,
kParseVerdictCacheMaxBytes);
static auto* const kCache =
new BoundedTranslationCache<ShaderParseVerdict>("ShaderTranslationCache L1c (GLSL->parse verdict)",
kParseVerdictCacheMaxEntries, kParseVerdictCacheMaxBytes);
return *kCache;
}
@@ -200,8 +202,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
static const UnorderedMap<String, Uint> kEmptyFormats;
builder.NameMap(inputs.glFormatByUniformName ? *inputs.glFormatByUniformName : kEmptyFormats);
static const UnorderedMap<String, Int> kEmptyBindings;
builder.NameMap(inputs.storageBlockBindingOverrides ? *inputs.storageBlockBindingOverrides
: kEmptyBindings);
builder.NameMap(inputs.storageBlockBindingOverrides ? *inputs.storageBlockBindingOverrides : kEmptyBindings);
static const std::map<String, String> kEmptyRenames;
builder.StringMap(inputs.inputBlockRenames ? *inputs.inputBlockRenames : kEmptyRenames);
builder.StringMap(inputs.outputBlockRenames ? *inputs.outputBlockRenames : kEmptyRenames);
@@ -214,7 +215,8 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
SizeT EsslTranslationResultBytes(const EsslTranslationResult& result) {
SizeT bytes = result.essl.size();
for (const String& name : result.flattenedXfbBlockNames) bytes += name.size();
for (const String& name : result.flattenedXfbBlockNames)
bytes += name.size();
bytes += result.atomicCounterGlBindings.size() * sizeof(Int);
return bytes;
}
@@ -119,9 +119,9 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
static_assert(std::is_trivially_copyable_v<ValueT>);
Vector<Pair<StringView, ValueT>> sorted;
sorted.reserve(map.size());
for (const auto& [name, value] : map) sorted.emplace_back(StringView(name), value);
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) { return a.first < b.first; });
for (const auto& [name, value] : map)
sorted.emplace_back(StringView(name), value);
std::sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
Value(static_cast<Uint64>(sorted.size()));
for (const auto& [name, value] : sorted) {
Text(name);
@@ -281,14 +281,11 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
const Uint64 lookups = stats.hits + stats.misses;
MGLOG_D("%s: %llu/%llu hits (%.1f%%), %llu inserts, %llu evictions, %llu oversize, "
"%llu duplicate, %zu entries / %zu KiB",
m_name, static_cast<unsigned long long>(stats.hits),
static_cast<unsigned long long>(lookups),
m_name, static_cast<unsigned long long>(stats.hits), static_cast<unsigned long long>(lookups),
lookups ? 100.0 * static_cast<double>(stats.hits) / static_cast<double>(lookups) : 0.0,
static_cast<unsigned long long>(stats.inserts),
static_cast<unsigned long long>(stats.evictions),
static_cast<unsigned long long>(stats.inserts), static_cast<unsigned long long>(stats.evictions),
static_cast<unsigned long long>(stats.rejectedOversize),
static_cast<unsigned long long>(stats.duplicateInserts), EntryCount(),
StoredBytes() / 1024u);
static_cast<unsigned long long>(stats.duplicateInserts), EntryCount(), StoredBytes() / 1024u);
}
// Tests only: makes the caps small enough to exercise eviction without
@@ -313,8 +310,7 @@ namespace MobileGL::MG_Util::ShaderTranspiler {
using EntryList = std::list<Entry>;
void EvictUntilWithinBudgetLocked() {
while (!m_entries.empty() &&
(m_entries.size() > m_maxEntries || m_storedBytes > m_maxBytes)) {
while (!m_entries.empty() && (m_entries.size() > m_maxEntries || m_storedBytes > m_maxBytes)) {
const auto victim = m_entries.begin();
m_storedBytes -= victim->bytes;
m_index.erase(victim->key);
+7 -7
View File
@@ -107,9 +107,9 @@ namespace MobileGL {
return std::min(std::max(limit, 0), storageBindings);
};
const Int stageStorageBlocks[ATOMIC_COUNTER_STAGE_COUNT] = {
p.MaxVertexShaderStorageBlocks, p.MaxTessControlShaderStorageBlocks,
p.MaxVertexShaderStorageBlocks, p.MaxTessControlShaderStorageBlocks,
p.MaxTessEvaluationShaderStorageBlocks, p.MaxGeometryShaderStorageBlocks,
p.MaxFragmentShaderStorageBlocks, p.MaxComputeShaderStorageBlocks};
p.MaxFragmentShaderStorageBlocks, p.MaxComputeShaderStorageBlocks};
AtomicCounterLimits limits;
Int bufferSum = 0;
for (SizeT stage = 0; stage < ATOMIC_COUNTER_STAGE_COUNT; ++stage) {
@@ -217,11 +217,11 @@ namespace MobileGL {
// Zero or negative means "no ceiling to enforce for this kind" - a backendless
// environment, which every unit test and the pre-init preload path run in.
struct ResourceBindingLimits {
Int MaxSamplerBindings = 0; // GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
Int MaxImageBindings = 0; // GL_MAX_IMAGE_UNITS
Int MaxUniformBufferBindings = 0; // GL_MAX_UNIFORM_BUFFER_BINDINGS
Int MaxShaderStorageBufferBindings = 0; // GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS
Int MaxAtomicCounterBufferBindings = 0; // GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS
Int MaxSamplerBindings = 0; // GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
Int MaxImageBindings = 0; // GL_MAX_IMAGE_UNITS
Int MaxUniformBufferBindings = 0; // GL_MAX_UNIFORM_BUFFER_BINDINGS
Int MaxShaderStorageBufferBindings = 0; // GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS
Int MaxAtomicCounterBufferBindings = 0; // GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS
};
struct ProgramAttrib {
@@ -168,7 +168,8 @@ namespace MobileGL {
const int key = buildStorageKey(EShLangFragment, glslang::EvqInOut);
for (int location = top; location >= 0; --location) {
bool free = true;
for (int i = 0; i < size && free; ++i) free = checkEmpty(key, location + i);
for (int i = 0; i < size && free; ++i)
free = checkEmpty(key, location + i);
if (!free) continue;
reserveSlot(key, location, size);
ent.symbol->getWritableType().getQualifier().layoutLocation = location;
@@ -212,7 +213,7 @@ namespace MobileGL {
// is the single derivation, and neither site computes a ceiling of its own.
void TMglGlslIoResolver::CheckDeclaredBindingRange(const glslang::TType& type, const glslang::TString& name) {
if (m_bindingLimits == nullptr || m_bindingViolation == nullptr) return;
if (!m_bindingViolation->empty()) return; // first violation wins; the link is already lost
if (!m_bindingViolation->empty()) return; // first violation wins; the link is already lost
const glslang::TQualifier& qualifier = type.getQualifier();
const char* kind = nullptr;
@@ -232,8 +233,8 @@ namespace MobileGL {
// TRAILING NUMBER is the GL binding the shader asked for (ParseContextBase::
// growAtomicCounterBlock names it from bufferBinding). That name is the only surviving
// record of the declaration, so it is what the counter ceiling is read off.
const Int counterBinding = MG_Util::ShaderTranspiler::AtomicCounterBlockGlBinding(
StringView(name.c_str(), name.size()));
const Int counterBinding =
MG_Util::ShaderTranspiler::AtomicCounterBlockGlBinding(StringView(name.c_str(), name.size()));
if (counterBinding >= 0) {
kind = "atomic_uint";
limitName = "GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS";
@@ -275,8 +276,8 @@ namespace MobileGL {
const long long lastBinding = binding + elementCount - 1;
if (lastBinding < static_cast<long long>(limit)) return;
String message = "Error: layout(binding = " + std::to_string(binding) + ") on " + kind + " '" +
String(name.c_str()) + "'";
String message =
"Error: layout(binding = " + std::to_string(binding) + ") on " + kind + " '" + String(name.c_str()) + "'";
if (elementCount > 1) {
message += " (an array of " + std::to_string(elementCount) + ", occupying bindings " +
std::to_string(binding) + ".." + std::to_string(lastBinding) + ")";
@@ -33,8 +33,7 @@ namespace MobileGL {
ExplicitVarSlotMap* opaqueUniformBindings,
std::set<String>* storageBlocksWithoutBinding = nullptr,
std::set<String>* uniformBlocksWithoutBinding = nullptr,
const ResourceBindingLimits* bindingLimits = nullptr,
String* bindingViolation = nullptr)
const ResourceBindingLimits* bindingLimits = nullptr, String* bindingViolation = nullptr)
: TDefaultGlslIoResolver(intermediate), m_explicitVertexIns(vertexIns), m_explicitFragOuts(fragOuts),
m_explicitFragOutIndices(fragOutIndices), m_explicitOpaqueUniformBindings(opaqueUniformBindings),
m_storageBlocksWithoutBinding(storageBlocksWithoutBinding),
@@ -45,8 +44,7 @@ namespace MobileGL {
const ExplicitVarSlotMap& fragOutIndices, ExplicitVarSlotMap* opaqueUniformBindings,
std::set<String>* storageBlocksWithoutBinding = nullptr,
std::set<String>* uniformBlocksWithoutBinding = nullptr,
const ResourceBindingLimits* bindingLimits = nullptr,
String* bindingViolation = nullptr)
const ResourceBindingLimits* bindingLimits = nullptr, String* bindingViolation = nullptr)
: TMglGlslIoResolver(*program.getIntermediate(stage), vertexIns, fragOuts, fragOutIndices,
opaqueUniformBindings, storageBlocksWithoutBinding, uniformBlocksWithoutBinding,
bindingLimits, bindingViolation) {}