[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
+31 -17
View File
@@ -31,26 +31,38 @@ namespace MobileGL::MG_Util::Async {
if (!continuation) return;
try {
continuation();
} catch (const std::exception& e) {
}
catch (const std::exception& e) {
MGLOG_E_ONCE("JobNode: a terminal continuation threw (%s); it has been contained, but whatever it "
"was going to do did not happen",
e.what());
} catch (...) {
"was going to do did not happen",
e.what());
}
catch (...) {
MGLOG_E_ONCE("JobNode: a terminal continuation threw a non-std exception; it has been contained, "
"but whatever it was going to do did not happen");
"but whatever it was going to do did not happen");
}
}
} // namespace
Bool JobNode::IsTerminal() const { return IsTerminalState(m_state.load(std::memory_order_acquire)); }
Bool JobNode::IsTerminal() const {
return IsTerminalState(m_state.load(std::memory_order_acquire));
}
Bool JobNode::IsComplete() const { return m_state.load(std::memory_order_acquire) == JobState::Complete; }
Bool JobNode::IsComplete() const {
return m_state.load(std::memory_order_acquire) == JobState::Complete;
}
Bool JobNode::IsCancelled() const { return m_state.load(std::memory_order_acquire) == JobState::Cancelled; }
Bool JobNode::IsCancelled() const {
return m_state.load(std::memory_order_acquire) == JobState::Cancelled;
}
Bool JobNode::IsCancellationRequested() const { return m_cancelled.load(std::memory_order_acquire); }
Bool JobNode::IsCancellationRequested() const {
return m_cancelled.load(std::memory_order_acquire);
}
JobState JobNode::State() const { return m_state.load(std::memory_order_acquire); }
JobState JobNode::State() const {
return m_state.load(std::memory_order_acquire);
}
// The single place a node changes state. Keeping every transition here is what makes the
// continuation list exactly-once: the same critical section that publishes the terminal
@@ -90,18 +102,18 @@ namespace MobileGL::MG_Util::Async {
try {
RunBody();
} catch (const std::exception& e) {
}
catch (const std::exception& e) {
// Asio propagates an exception escaping a handler out of thread_pool::run(),
// which means std::terminate for the whole process. Every job boundary contains
// it and reports the job as Cancelled; the joining GL thread then sees a node
// that produced no result, which is the same shape as an abandoned node.
diagnostics.logLines.push_back(
{MOBILEGL_LOG_LEVEL_DEBUG, std::format("Job body threw: {}", e.what())});
diagnostics.logLines.push_back({MOBILEGL_LOG_LEVEL_DEBUG, std::format("Job body threw: {}", e.what())});
TryTransition(JobState::Running, JobState::Cancelled);
return;
} catch (...) {
diagnostics.logLines.push_back(
{MOBILEGL_LOG_LEVEL_DEBUG, String("Job body threw a non-std exception")});
}
catch (...) {
diagnostics.logLines.push_back({MOBILEGL_LOG_LEVEL_DEBUG, String("Job body threw a non-std exception")});
TryTransition(JobState::Running, JobState::Cancelled);
return;
}
@@ -119,7 +131,9 @@ namespace MobileGL::MG_Util::Async {
m_cancelled.load(std::memory_order_acquire) ? JobState::Cancelled : JobState::Complete);
}
void JobNode::RunInline() { Run(); }
void JobNode::RunInline() {
Run();
}
void JobNode::Wait() {
// Invariant I4, mechanically enforced: no job body ever blocks on another job, so the
+3 -3
View File
@@ -53,8 +53,8 @@ namespace MobileGL::MG_Util::Async {
};
struct JobDiagnostics {
Vector<DeferredError> errors; // replayed, in ascending `sequence`, by the join
Vector<DeferredLogLine> logLines; // worker-side MGLOG text, flushed in order by the join
Vector<DeferredError> errors; // replayed, in ascending `sequence`, by the join
Vector<DeferredLogLine> logLines; // worker-side MGLOG text, flushed in order by the join
};
// The scheduling primitive every asynchronous compile and link is built on. A node owns
@@ -83,7 +83,7 @@ namespace MobileGL::MG_Util::Async {
// Lock-free and non-blocking - safe from any thread, including a pool thread.
Bool IsTerminal() const;
Bool IsComplete() const; // Complete only; this is what backs GL_COMPLETION_STATUS_KHR
Bool IsComplete() const; // Complete only; this is what backs GL_COMPLETION_STATUS_KHR
Bool IsCancelled() const; // settled AS cancelled - the outcome, not the request
JobState State() const;
+30 -15
View File
@@ -40,7 +40,9 @@ namespace MobileGL::MG_Util::Async {
// stack-allocated pools a test builds - those join themselves in their destructor.
std::atomic<ShaderCompilePool*> g_processPool{nullptr};
Bool InProcessTeardown() { return g_processTeardown; }
Bool InProcessTeardown() {
return g_processTeardown;
}
void EnsureProcessTeardownSentinel() {
std::call_once(g_teardownSentinelOnce, [] {
@@ -63,8 +65,7 @@ namespace MobileGL::MG_Util::Async {
}
Uint64 ReadCpuMaxFrequencyKHz(const Uint cpu) {
const String path =
std::format("/sys/devices/system/cpu/cpu{}/cpufreq/cpuinfo_max_freq", cpu);
const String path = std::format("/sys/devices/system/cpu/cpu{}/cpufreq/cpuinfo_max_freq", cpu);
std::FILE* file = std::fopen(path.c_str(), "r");
if (file == nullptr) return 0;
unsigned long long value = 0;
@@ -101,9 +102,12 @@ namespace MobileGL::MG_Util::Async {
Bool AsyncShaderCompileEnabled() {
switch (MG_Config::Features.AsyncShaderCompile) {
case MG_Config::QuirkOverride::ForceOn: return true;
case MG_Config::QuirkOverride::ForceOff: return false;
case MG_Config::QuirkOverride::Auto: break;
case MG_Config::QuirkOverride::ForceOn:
return true;
case MG_Config::QuirkOverride::ForceOff:
return false;
case MG_Config::QuirkOverride::Auto:
break;
}
return kAsyncShaderCompileDefault;
}
@@ -129,9 +133,12 @@ namespace MobileGL::MG_Util::Async {
Bool OptimisticShaderStatusActive() {
switch (MG_Config::Features.AsyncOptimisticShaderStatus) {
case MG_Config::QuirkOverride::ForceOn: return AsyncShaderCompileActive();
case MG_Config::QuirkOverride::ForceOff: return false;
case MG_Config::QuirkOverride::Auto: break;
case MG_Config::QuirkOverride::ForceOn:
return AsyncShaderCompileActive();
case MG_Config::QuirkOverride::ForceOff:
return false;
case MG_Config::QuirkOverride::Auto:
break;
}
return kOptimisticShaderStatusDefault && AsyncShaderCompileActive();
}
@@ -185,7 +192,8 @@ namespace MobileGL::MG_Util::Async {
++inFlight;
try {
asio::post(*pool, [this, node]() mutable { RunOnWorker(Move(node)); });
} catch (...) {
}
catch (...) {
--inFlight;
toCancel.push_back(Move(node));
}
@@ -218,7 +226,9 @@ namespace MobileGL::MG_Util::Async {
ShaderCompilePool::ShaderCompilePool(const Uint threadCount) : m_impl(MakeUnique<Impl>(threadCount)) {}
ShaderCompilePool::~ShaderCompilePool() { StopAndDrain(); }
ShaderCompilePool::~ShaderCompilePool() {
StopAndDrain();
}
ShaderCompilePool& ShaderCompilePool::Get() {
// Leak-at-exit, like the other MobileGL singletons: the object itself is never
@@ -235,9 +245,13 @@ namespace MobileGL::MG_Util::Async {
return *pool;
}
Bool ShaderCompilePool::IsPoolThread() { return tl_isPoolThread; }
Bool ShaderCompilePool::IsPoolThread() {
return tl_isPoolThread;
}
Uint ShaderCompilePool::GetThreadCount() const { return m_impl->threadCount; }
Uint ShaderCompilePool::GetThreadCount() const {
return m_impl->threadCount;
}
Uint ShaderCompilePool::GetMaxConcurrency() const {
const std::lock_guard<std::mutex> lock(m_impl->mutex);
@@ -283,9 +297,10 @@ namespace MobileGL::MG_Util::Async {
m_impl->DispatchLocked(toCancel);
enqueued = true;
}
} catch (...) {
}
catch (...) {
MGLOG_E_ONCE("ShaderCompilePool::Post: enqueue failed; cancelling the job so its joiner "
"cannot block forever");
"cannot block forever");
if (node) node->Cancel();
return;
}
+138 -185
View File
@@ -90,8 +90,12 @@ namespace MobileGL::MG_Util::BackendLoader {
#if defined(MOBILEGL_IOS)
"@rpath/", "@executable_path/Frameworks/", "@loader_path/Frameworks/",
#else
"/opt/vc/lib/", "/usr/local/lib/", "/usr/lib/", "/usr/lib/x86_64-linux-gnu/",
"/usr/lib64/", "/lib64/",
"/opt/vc/lib/",
"/usr/local/lib/",
"/usr/lib/",
"/usr/lib/x86_64-linux-gnu/",
"/usr/lib64/",
"/lib64/",
#endif
"" // Keep this last so the dynamic loader can use LD_LIBRARY_PATH.
};
@@ -109,8 +113,7 @@ namespace MobileGL::MG_Util::BackendLoader {
}
lib = dlopen(signedPath.c_str(), flags);
if (lib == nullptr) {
MGLOG_F("Failed to open signed trace ANGLE library %s: %s",
signedPath.c_str(), dlerror());
MGLOG_F("Failed to open signed trace ANGLE library %s: %s", signedPath.c_str(), dlerror());
return nullptr;
}
MGLOG_I("Loaded signed trace ANGLE library: %s", signedPath.c_str());
@@ -180,8 +183,8 @@ namespace MobileGL::MG_Util::BackendLoader {
#endif
}
MG_External::EGL::eglSwapBuffersWithDamageEXT_PTR
ResolveSwapBuffersWithDamage(const MG_External::EGLFunctionsTable& funcs) {
MG_External::EGL::eglSwapBuffersWithDamageEXT_PTR ResolveSwapBuffersWithDamage(
const MG_External::EGLFunctionsTable& funcs) {
if (!funcs.eglGetCurrentDisplay || !funcs.eglQueryString || !funcs.eglGetProcAddress) {
return nullptr;
}
@@ -793,54 +796,26 @@ namespace MobileGL::MG_Util::BackendLoader {
} \
} while (0);
{
INIT_EGL_FUNC(eglBindAPI)
INIT_EGL_FUNC(eglBindTexImage)
INIT_EGL_FUNC(eglChooseConfig)
INIT_EGL_FUNC(eglCopyBuffers)
INIT_EGL_FUNC(eglCreateContext)
INIT_EGL_FUNC(eglCreatePbufferFromClientBuffer)
INIT_EGL_FUNC(eglCreatePbufferSurface)
INIT_EGL_FUNC(eglCreatePixmapSurface)
INIT_EGL_FUNC(eglCreatePlatformPixmapSurface)
INIT_EGL_FUNC(eglCreatePlatformWindowSurface)
INIT_EGL_FUNC(eglCreateWindowSurface)
INIT_EGL_FUNC(eglDestroyContext)
INIT_EGL_FUNC(eglDestroySurface)
INIT_EGL_FUNC(eglGetConfigAttrib)
INIT_EGL_FUNC(eglGetConfigs)
INIT_EGL_FUNC(eglGetCurrentContext)
INIT_EGL_FUNC(eglGetCurrentDisplay)
INIT_EGL_FUNC(eglGetCurrentSurface)
INIT_EGL_FUNC(eglGetDisplay)
INIT_EGL_FUNC(eglGetPlatformDisplay)
INIT_EGL_FUNC(eglGetError)
INIT_EGL_FUNC(eglGetProcAddress)
INIT_EGL_FUNC(eglInitialize)
INIT_EGL_FUNC(eglMakeCurrent)
INIT_EGL_FUNC(eglQueryAPI)
INIT_EGL_FUNC(eglQueryContext)
INIT_EGL_FUNC(eglQueryString)
INIT_EGL_FUNC(eglQuerySurface)
INIT_EGL_FUNC(eglReleaseTexImage)
INIT_EGL_FUNC(eglReleaseThread)
INIT_EGL_FUNC(eglSurfaceAttrib)
INIT_EGL_FUNC(eglSwapBuffers)
INIT_EGL_FUNC(eglSwapInterval)
INIT_EGL_FUNC(eglTerminate)
INIT_EGL_FUNC_OPTIONAL(eglUnlockSurfaceKHR)
INIT_EGL_FUNC(eglWaitClient)
INIT_EGL_FUNC(eglWaitGL)
INIT_EGL_FUNC(eglWaitNative)
INIT_EGL_FUNC(eglCreateSync)
INIT_EGL_FUNC(eglDestroySync)
INIT_EGL_FUNC(eglClientWaitSync)
INIT_EGL_FUNC(eglGetSyncAttrib)
INIT_EGL_FUNC(eglCreateImage)
INIT_EGL_FUNC(eglDestroyImage)
INIT_EGL_FUNC(eglGetPlatformDisplay)
INIT_EGL_FUNC(eglWaitSync)
}
{INIT_EGL_FUNC(eglBindAPI) INIT_EGL_FUNC(eglBindTexImage) INIT_EGL_FUNC(eglChooseConfig) INIT_EGL_FUNC(
eglCopyBuffers) INIT_EGL_FUNC(eglCreateContext) INIT_EGL_FUNC(eglCreatePbufferFromClientBuffer)
INIT_EGL_FUNC(eglCreatePbufferSurface) INIT_EGL_FUNC(eglCreatePixmapSurface) INIT_EGL_FUNC(
eglCreatePlatformPixmapSurface) INIT_EGL_FUNC(eglCreatePlatformWindowSurface)
INIT_EGL_FUNC(eglCreateWindowSurface) INIT_EGL_FUNC(eglDestroyContext) INIT_EGL_FUNC(eglDestroySurface)
INIT_EGL_FUNC(eglGetConfigAttrib) INIT_EGL_FUNC(eglGetConfigs) INIT_EGL_FUNC(eglGetCurrentContext)
INIT_EGL_FUNC(eglGetCurrentDisplay) INIT_EGL_FUNC(eglGetCurrentSurface) INIT_EGL_FUNC(
eglGetDisplay) INIT_EGL_FUNC(eglGetPlatformDisplay) INIT_EGL_FUNC(eglGetError)
INIT_EGL_FUNC(eglGetProcAddress) INIT_EGL_FUNC(eglInitialize) INIT_EGL_FUNC(eglMakeCurrent)
INIT_EGL_FUNC(eglQueryAPI) INIT_EGL_FUNC(eglQueryContext) INIT_EGL_FUNC(eglQueryString)
INIT_EGL_FUNC(eglQuerySurface) INIT_EGL_FUNC(eglReleaseTexImage) INIT_EGL_FUNC(
eglReleaseThread) INIT_EGL_FUNC(eglSurfaceAttrib) INIT_EGL_FUNC(eglSwapBuffers)
INIT_EGL_FUNC(eglSwapInterval) INIT_EGL_FUNC(eglTerminate)
INIT_EGL_FUNC_OPTIONAL(eglUnlockSurfaceKHR) INIT_EGL_FUNC(eglWaitClient)
INIT_EGL_FUNC(eglWaitGL) INIT_EGL_FUNC(eglWaitNative) INIT_EGL_FUNC(
eglCreateSync) INIT_EGL_FUNC(eglDestroySync)
INIT_EGL_FUNC(eglClientWaitSync) INIT_EGL_FUNC(eglGetSyncAttrib)
INIT_EGL_FUNC(eglCreateImage) INIT_EGL_FUNC(eglDestroyImage)
INIT_EGL_FUNC(eglGetPlatformDisplay)
INIT_EGL_FUNC(eglWaitSync)}
// Display extensions can only be checked after the display is initialized.
funcs.eglSwapBuffersWithDamageEXT = nullptr;
@@ -856,8 +831,8 @@ namespace MobileGL::MG_Util::BackendLoader {
const MG_External::GLESFunctionsTable& f) {
const Bool esVersionOk =
caps.GLESVersion.Major > 3 || (caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 1);
if (!esVersionOk || !f.glDrawArraysIndirect || !f.glBindBufferBase || !f.glMapBufferRange ||
!f.glUnmapBuffer || !f.glMemoryBarrier || !f.glCreateShader || !f.glCreateProgram) {
if (!esVersionOk || !f.glDrawArraysIndirect || !f.glBindBufferBase || !f.glMapBufferRange || !f.glUnmapBuffer ||
!f.glMemoryBarrier || !f.glCreateShader || !f.glCreateProgram) {
return false;
}
// Read from caps, not re-queried: the per-stage limits are resolved (and their query
@@ -872,8 +847,7 @@ namespace MobileGL::MG_Util::BackendLoader {
MGLOG_I("baseInstance probe skipped: GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS = %d", maxVertexSsboBlocks);
return false;
}
while (f.glGetError() != GL_NO_ERROR) {
}
while (f.glGetError() != GL_NO_ERROR) {}
const char* vsSource = "#version 310 es\n"
"layout(std430, binding = 0) buffer MgProbeResult { highp int mg_probeValue; };\n"
@@ -920,8 +894,7 @@ namespace MobileGL::MG_Util::BackendLoader {
if (fs != 0) f.glDeleteShader(fs);
if (program == 0) {
MGLOG_I("baseInstance probe skipped: probe program failed to build (vs=%u fs=%u)", vs, fs);
while (f.glGetError() != GL_NO_ERROR) {
}
while (f.glGetError() != GL_NO_ERROR) {}
return false;
}
@@ -994,8 +967,7 @@ namespace MobileGL::MG_Util::BackendLoader {
f.glDeleteVertexArrays(1, &vao);
f.glDeleteBuffers(2, buffers);
f.glDeleteProgram(program);
while (f.glGetError() != GL_NO_ERROR) {
}
while (f.glGetError() != GL_NO_ERROR) {}
return includesBase;
}
@@ -1077,8 +1049,7 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.FragmentShadingRateMasks[i] = mask;
}
// The queries above are best-effort: a pname a driver does not know only leaves its default.
while (glesFuncs.glGetError() != GL_NO_ERROR) {
}
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
MGLOG_I("GL_EXT_fragment_shading_rate: primitive=%d attachment=%d nonTrivialCombiners=%d rates(1x)=0x%04x "
"rates(4x)=0x%04x attachment texel %dx%d..%dx%d",
caps.SupportsFragmentShadingRatePrimitive, caps.SupportsFragmentShadingRateAttachment,
@@ -1258,8 +1229,7 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.SupportsMultiDrawIndirect = hasMultiDrawIndirectExtension &&
glesFuncs.glMultiDrawArraysIndirectEXT != nullptr &&
glesFuncs.glMultiDrawElementsIndirectEXT != nullptr;
caps.SupportsMultiDrawElementsBaseVertex = hasDrawElementsBaseVertexExtension &&
hasMultiDrawArraysExtension &&
caps.SupportsMultiDrawElementsBaseVertex = hasDrawElementsBaseVertexExtension && hasMultiDrawArraysExtension &&
glesFuncs.glMultiDrawElementsBaseVertexEXT != nullptr;
// All three, not any: DirectGLES picks native-vs-emulated once per draw entry point off
// this single flag, so a driver that resolved only some of them must count as absent.
@@ -1283,8 +1253,7 @@ namespace MobileGL::MG_Util::BackendLoader {
}
// Each companion builds on the base extension, and each needs the entry points it brings;
// the strings alone never count (see AcquireGLESFunctions on stub pointers).
caps.SupportsFragmentShadingRate = caps.SupportsFragmentShadingRate &&
glesFuncs.glShadingRateEXT != nullptr &&
caps.SupportsFragmentShadingRate = caps.SupportsFragmentShadingRate && glesFuncs.glShadingRateEXT != nullptr &&
glesFuncs.glShadingRateCombinerOpsEXT != nullptr &&
glesFuncs.glGetFragmentShadingRatesEXT != nullptr;
caps.SupportsFragmentShadingRatePrimitive =
@@ -1318,42 +1287,37 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.SubgroupSupportedFeatures);
// Core from ES 3.2 on, so an extension string is not required there; below 3.2 the
// extension is, and the pointer still has to have resolved either way.
const Bool esAtLeast32 = caps.GLESVersion.Major > 3 ||
(caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2);
const Bool esAtLeast31 = caps.GLESVersion.Major > 3 ||
(caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 1);
const Bool esAtLeast32 =
caps.GLESVersion.Major > 3 || (caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2);
const Bool esAtLeast31 =
caps.GLESVersion.Major > 3 || (caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 1);
caps.SupportsFramebufferDefaultLayers = esAtLeast32 || hasGeometryShaderExtension;
caps.SupportsDrawIndirect = esAtLeast31 && glesFuncs.glDrawArraysIndirect != nullptr &&
glesFuncs.glDrawElementsIndirect != nullptr;
caps.SupportsDrawElementsBaseVertex = (esAtLeast32 || hasDrawElementsBaseVertexExtension) &&
glesFuncs.glDrawElementsBaseVertex != nullptr;
caps.SupportsTessellationShader = (esAtLeast32 || (esAtLeast31 && hasTessellationShaderExtension)) &&
glesFuncs.glPatchParameteri != nullptr;
caps.SupportsDrawIndirect =
esAtLeast31 && glesFuncs.glDrawArraysIndirect != nullptr && glesFuncs.glDrawElementsIndirect != nullptr;
caps.SupportsDrawElementsBaseVertex =
(esAtLeast32 || hasDrawElementsBaseVertexExtension) && glesFuncs.glDrawElementsBaseVertex != nullptr;
caps.SupportsTessellationShader =
(esAtLeast32 || (esAtLeast31 && hasTessellationShaderExtension)) && glesFuncs.glPatchParameteri != nullptr;
// The string AND the pointer: eglGetProcAddress may hand back a stub for a name the
// driver does not implement.
caps.SupportsClipControl = hasClipControlExtension && glesFuncs.glClipControlEXT != nullptr;
caps.SupportsComputeShader = esAtLeast31 && glesFuncs.glDispatchCompute != nullptr &&
glesFuncs.glMemoryBarrier != nullptr &&
glesFuncs.glCreateShader != nullptr &&
glesFuncs.glMemoryBarrier != nullptr && glesFuncs.glCreateShader != nullptr &&
glesFuncs.glCreateProgram != nullptr;
caps.SupportsShaderMultisampleInterpolation =
caps.SupportsShaderMultisampleInterpolation || caps.GLESVersion.Major > 3 ||
(caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2);
caps.SupportsShaderMultisampleInterpolation = caps.SupportsShaderMultisampleInterpolation ||
caps.GLESVersion.Major > 3 ||
(caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2);
// Detect optional raster/color-mask entry points by whether they loaded. glColorMaski is GLES
// 3.2 core (no extension string), so pointer presence is the reliable signal for all of these.
caps.SupportsPolygonMode =
glesFuncs.glPolygonModeNV != nullptr || glesFuncs.glPolygonModeANGLE != nullptr;
caps.SupportsIndexedColorMask = glesFuncs.glColorMaski != nullptr ||
glesFuncs.glColorMaskiEXT != nullptr ||
caps.SupportsPolygonMode = glesFuncs.glPolygonModeNV != nullptr || glesFuncs.glPolygonModeANGLE != nullptr;
caps.SupportsIndexedColorMask = glesFuncs.glColorMaski != nullptr || glesFuncs.glColorMaskiEXT != nullptr ||
glesFuncs.glColorMaskiOES != nullptr;
MGLOG_I(" glPolygonMode (NV/ANGLE): %s", caps.SupportsPolygonMode ? "yes" : "no");
MGLOG_I(" indexed glColorMaski: %s", caps.SupportsIndexedColorMask ? "yes" : "no");
MGLOG_I(" dual-source blend (EXT_blend_func_extended): %s",
caps.SupportsDualSourceBlend ? "yes" : "no");
MGLOG_I(" dual-source blend (EXT_blend_func_extended): %s", caps.SupportsDualSourceBlend ? "yes" : "no");
MGLOG_I(" draw indirect (ES 3.1 core): %s", caps.SupportsDrawIndirect ? "yes" : "no");
MGLOG_I(" multi-draw indirect (EXT_multi_draw_indirect): %s",
caps.SupportsMultiDrawIndirect ? "yes" : "no");
MGLOG_I(" multi-draw indirect (EXT_multi_draw_indirect): %s", caps.SupportsMultiDrawIndirect ? "yes" : "no");
MGLOG_I(" multi-draw base vertex (EXT/OES_draw_elements_base_vertex + EXT_multi_draw_arrays): %s",
caps.SupportsMultiDrawElementsBaseVertex ? "yes" : "no");
MGLOG_I(" draw elements base vertex (ES 3.2 core or EXT/OES_draw_elements_base_vertex): %s",
@@ -1370,9 +1334,12 @@ namespace MobileGL::MG_Util::BackendLoader {
{
const auto pointSizeTierName = [](MG_External::GLESCapabilities::PointSizeTier tier) {
switch (tier) {
case MG_External::GLESCapabilities::PointSizeTier::ExtensionEXT: return "EXT";
case MG_External::GLESCapabilities::PointSizeTier::ExtensionOES: return "OES";
default: return "no";
case MG_External::GLESCapabilities::PointSizeTier::ExtensionEXT:
return "EXT";
case MG_External::GLESCapabilities::PointSizeTier::ExtensionOES:
return "OES";
default:
return "no";
}
};
MGLOG_I(" tessellation gl_PointSize (EXT/OES_tessellation_point_size): %s",
@@ -1394,8 +1361,7 @@ namespace MobileGL::MG_Util::BackendLoader {
// make every storage-range offset legal, so an unusable answer keeps the 256 default.
GLint shaderStorageOffsetAlignment = 0;
glesFuncs.glGetIntegerv(GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT, &shaderStorageOffsetAlignment);
while (glesFuncs.glGetError() != GL_NO_ERROR) {
}
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
if (shaderStorageOffsetAlignment > 0) {
caps.ShaderStorageBufferOffsetAlignment = shaderStorageOffsetAlignment;
}
@@ -1494,7 +1460,8 @@ namespace MobileGL::MG_Util::BackendLoader {
const auto drainErrors = [&glesFuncs]() {
Bool hadError = false;
if (glesFuncs.glGetError) {
while (glesFuncs.glGetError() != GL_NO_ERROR) hadError = true;
while (glesFuncs.glGetError() != GL_NO_ERROR)
hadError = true;
}
return hadError;
};
@@ -1606,8 +1573,7 @@ namespace MobileGL::MG_Util::BackendLoader {
// Geometry and tessellation shaders and their image-uniform queries are core only in
// ES 3.2. DirectGLES emits ESSL 3.10 on an ES 3.1 context, so reporting zero there is
// both legal and an accurate description of what the backend compiler can consume.
if (caps.GLESVersion.Major > 3 ||
(caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2)) {
if (caps.GLESVersion.Major > 3 || (caps.GLESVersion.Major == 3 && caps.GLESVersion.Minor >= 2)) {
glesFuncs.glGetIntegerv(GL_MAX_GEOMETRY_IMAGE_UNIFORMS, &maxGeometryImageUniforms);
glesFuncs.glGetIntegerv(GL_MAX_TESS_CONTROL_IMAGE_UNIFORMS, &maxTessControlImageUniforms);
glesFuncs.glGetIntegerv(GL_MAX_TESS_EVALUATION_IMAGE_UNIFORMS, &maxTessEvaluationImageUniforms);
@@ -1643,8 +1609,7 @@ namespace MobileGL::MG_Util::BackendLoader {
maxFragmentShaderStorageBlocks = 4;
}
if (esAtLeast32) {
glesFuncs.glGetIntegerv(GL_MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS,
&maxTessControlShaderStorageBlocks);
glesFuncs.glGetIntegerv(GL_MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS, &maxTessControlShaderStorageBlocks);
glesFuncs.glGetIntegerv(GL_MAX_TESS_EVALUATION_SHADER_STORAGE_BLOCKS,
&maxTessEvaluationShaderStorageBlocks);
glesFuncs.glGetIntegerv(GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS, &maxGeometryShaderStorageBlocks);
@@ -1739,8 +1704,7 @@ namespace MobileGL::MG_Util::BackendLoader {
viewportBoundsRange[0] = -32768.0f;
viewportBoundsRange[1] = 32767.0f;
} else {
viewportIndexProvokingVertex =
NormalizeProvokingVertexConvention(driverViewportIndexConvention);
viewportIndexProvokingVertex = NormalizeProvokingVertexConvention(driverViewportIndexConvention);
}
}
if (caps.SupportsShaderMultisampleInterpolation && glesFuncs.glGetFloatv) {
@@ -1795,8 +1759,7 @@ namespace MobileGL::MG_Util::BackendLoader {
// Drain first: an error left by any earlier probe would otherwise read as this
// query having failed, and the value would be discarded as a non-answer.
if (glesFuncs.glGetError) {
while (glesFuncs.glGetError() != GL_NO_ERROR) {
}
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
}
glesFuncs.glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureBufferSize);
if (glesFuncs.glGetError) {
@@ -1827,11 +1790,10 @@ namespace MobileGL::MG_Util::BackendLoader {
MGLOG_I(" Buffer textures: UNSUPPORTED (%s). Any shader sampling a "
"samplerBuffer will fail to compile, and MobileGL keeps advertising "
"GL_MAX_TEXTURE_BUFFER_SIZE = %d because a GL 4.x context may not report 0.",
claimsSupport
? "this driver advertises buffer textures but no glTexBuffer entry "
"point resolved, so none of them can be called"
: "ES core needs 3.2, and neither GL_EXT_texture_buffer nor "
"GL_OES_texture_buffer is present",
claimsSupport ? "this driver advertises buffer textures but no glTexBuffer entry "
"point resolved, so none of them can be called"
: "ES core needs 3.2, and neither GL_EXT_texture_buffer nor "
"GL_OES_texture_buffer is present",
maxTextureBufferSize);
}
}
@@ -1882,9 +1844,8 @@ namespace MobileGL::MG_Util::BackendLoader {
SelfTest::ExplicitVertexInputLocationCeiling(glesFuncs);
// Guarded on `detected` rather than on the number alone: a probe that reached no verdict
// has measured nothing, and the clamp must be driven by evidence or not applied at all.
caps.MaxVertexAttribs = locationCeiling.detected
? std::min(maxVertexAttribs, locationCeiling.usableLocations)
: maxVertexAttribs;
caps.MaxVertexAttribs =
locationCeiling.detected ? std::min(maxVertexAttribs, locationCeiling.usableLocations) : maxVertexAttribs;
if (locationCeiling.detected) {
MGLOG_I(" GL_MAX_VERTEX_ATTRIBS reduced from the driver's %d to %d: "
"layout(location = N) on a vertex input is refused from N = %d upward",
@@ -1909,7 +1870,8 @@ namespace MobileGL::MG_Util::BackendLoader {
GLint sharedMemory = 0;
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
glesFuncs.glGetIntegerv(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, &sharedMemory);
if (glesFuncs.glGetError() == GL_NO_ERROR && sharedMemory > 0) caps.MaxComputeSharedMemorySize = sharedMemory;
if (glesFuncs.glGetError() == GL_NO_ERROR && sharedMemory > 0)
caps.MaxComputeSharedMemorySize = sharedMemory;
}
// Through glesFuncs, like every other capability query here: a bare glGetIntegerv resolves
// to MobileGL's own exported entry point, which answers this pname from the very
@@ -2045,15 +2007,13 @@ namespace MobileGL::MG_Util::BackendLoader {
MGLOG_I(" GL_MAX_COMBINED_CLIP_AND_CULL_DISTANCES: %d", caps.MaxCombinedClipAndCullDistances);
MGLOG_I(" GL_MAX_VIEWPORTS: %d", caps.MaxViewports);
MGLOG_I(" GL_MAX_VIEWPORT_DIMS: [%d, %d]", caps.MaxViewportWidth, caps.MaxViewportHeight);
MGLOG_I(" GL_VIEWPORT_BOUNDS_RANGE: [%.3f, %.3f]", caps.ViewportBoundsRangeMin,
caps.ViewportBoundsRangeMax);
MGLOG_I(" GL_VIEWPORT_BOUNDS_RANGE: [%.3f, %.3f]", caps.ViewportBoundsRangeMin, caps.ViewportBoundsRangeMax);
MGLOG_I(" GL_VIEWPORT_SUBPIXEL_BITS: %d", caps.ViewportSubpixelBits);
MGLOG_I(" GL_LAYER_PROVOKING_VERTEX: %s", ProvokingVertexConventionName(caps.LayerProvokingVertex));
MGLOG_I(" GL_VIEWPORT_INDEX_PROVOKING_VERTEX: %s",
ProvokingVertexConventionName(caps.ViewportIndexProvokingVertex));
caps.IndirectDrawInstanceIdIncludesBaseInstance =
ProbeIndirectInstanceIdIncludesBaseInstance(caps, glesFuncs);
caps.IndirectDrawInstanceIdIncludesBaseInstance = ProbeIndirectInstanceIdIncludesBaseInstance(caps, glesFuncs);
MGLOG_I(" Indirect draw gl_InstanceID includes baseInstance: %s",
caps.IndirectDrawInstanceIdIncludesBaseInstance ? "true" : "false");
@@ -2063,27 +2023,26 @@ namespace MobileGL::MG_Util::BackendLoader {
// emulation can be exercised on a healthy driver (the integration lane) and turned
// off again as a negative control.
switch (MG_Config::Features.EsprytUnlocatedIoBlocks) {
case MG_Config::QuirkOverride::ForceOn:
caps.SupportsLocatedInterStageIoBlocks = false;
MGLOG_I(" Located inter-stage interface blocks: forced OFF by "
"MOBILEGL_ESPRYT_UNLOCATED_IO_BLOCKS; the driver was not probed");
break;
case MG_Config::QuirkOverride::ForceOff:
caps.SupportsLocatedInterStageIoBlocks = true;
MGLOG_I(" Located inter-stage interface blocks: forced ON by "
"MOBILEGL_ESPRYT_UNLOCATED_IO_BLOCKS; the driver was not probed");
break;
case MG_Config::QuirkOverride::Auto:
default:
// SelfTest::ProbeLocatedIoBlocksLosePayload - the Mali-G1-Ultra ES driver
// delivers nothing through an interface block that carries an explicit
// layout(location=) once a tessellation or geometry stage is in the pipeline.
// Probed with its own controls rather than matched on a renderer string; see
// DriverBugProbes.h for the shape and for why the two controls decide what the
// finding is allowed to claim.
caps.SupportsLocatedInterStageIoBlocks =
!SelfTest::LocatedIoBlocksLosePayload(glesFuncs).detected;
break;
case MG_Config::QuirkOverride::ForceOn:
caps.SupportsLocatedInterStageIoBlocks = false;
MGLOG_I(" Located inter-stage interface blocks: forced OFF by "
"MOBILEGL_ESPRYT_UNLOCATED_IO_BLOCKS; the driver was not probed");
break;
case MG_Config::QuirkOverride::ForceOff:
caps.SupportsLocatedInterStageIoBlocks = true;
MGLOG_I(" Located inter-stage interface blocks: forced ON by "
"MOBILEGL_ESPRYT_UNLOCATED_IO_BLOCKS; the driver was not probed");
break;
case MG_Config::QuirkOverride::Auto:
default:
// SelfTest::ProbeLocatedIoBlocksLosePayload - the Mali-G1-Ultra ES driver
// delivers nothing through an interface block that carries an explicit
// layout(location=) once a tessellation or geometry stage is in the pipeline.
// Probed with its own controls rather than matched on a renderer string; see
// DriverBugProbes.h for the shape and for why the two controls decide what the
// finding is allowed to claim.
caps.SupportsLocatedInterStageIoBlocks = !SelfTest::LocatedIoBlocksLosePayload(glesFuncs).detected;
break;
}
MGLOG_I(" Located inter-stage interface blocks transport their payload: %s",
caps.SupportsLocatedInterStageIoBlocks
@@ -2092,16 +2051,16 @@ namespace MobileGL::MG_Util::BackendLoader {
"blocks without a location qualifier)");
switch (MG_Config::Features.EsprytDeferGeometryEmits) {
case MG_Config::QuirkOverride::ForceOn:
caps.DropsGeometryWritesAfterEmit = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.DropsGeometryWritesAfterEmit = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
caps.DropsGeometryWritesAfterEmit = SelfTest::GeometryStageSsboWriteAfterEmitDropped(glesFuncs);
break;
case MG_Config::QuirkOverride::ForceOn:
caps.DropsGeometryWritesAfterEmit = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.DropsGeometryWritesAfterEmit = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
caps.DropsGeometryWritesAfterEmit = SelfTest::GeometryStageSsboWriteAfterEmitDropped(glesFuncs);
break;
}
MGLOG_I(" Geometry-stage storage writes after EmitVertex are dropped: %s",
caps.DropsGeometryWritesAfterEmit
@@ -2109,60 +2068,54 @@ namespace MobileGL::MG_Util::BackendLoader {
: "false");
switch (MG_Config::Features.EsprytAtomicSubgroupScratch) {
case MG_Config::QuirkOverride::ForceOn:
caps.SharedMemoryLoadsGoStale = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.SharedMemoryLoadsGoStale = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
caps.SharedMemoryLoadsGoStale = SelfTest::SharedMemoryLoadsGoStale(glesFuncs);
break;
case MG_Config::QuirkOverride::ForceOn:
caps.SharedMemoryLoadsGoStale = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.SharedMemoryLoadsGoStale = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
caps.SharedMemoryLoadsGoStale = SelfTest::SharedMemoryLoadsGoStale(glesFuncs);
break;
}
MGLOG_I(" Shared-memory loads go stale across barrier(): %s",
caps.SharedMemoryLoadsGoStale
? "true (the subgroup emulation reads its scratch through atomic loads)"
: "false");
caps.SharedMemoryLoadsGoStale ? "true (the subgroup emulation reads its scratch through atomic loads)"
: "false");
caps.IsAngleRenderer = caps.GLESRendererString.find("ANGLE") != String::npos;
switch (MG_Config::Features.EsprytInvalidImagePlaceholder) {
case MG_Config::QuirkOverride::ForceOn:
caps.UseInvalidImagePlaceholder = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.UseInvalidImagePlaceholder = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
// ANGLE dereferences a null texture when a dispatch or draw reaches an EMPTY image
// unit the program uses, so the probe cannot be run there: its subject variant is
// exactly that dispatch, and a crash is no verdict. ANGLE keeps the placeholder
// without being asked.
caps.UseInvalidImagePlaceholder =
caps.IsAngleRenderer || SelfTest::EmptyImageUnitsReadNonzero(glesFuncs);
break;
case MG_Config::QuirkOverride::ForceOn:
caps.UseInvalidImagePlaceholder = true;
break;
case MG_Config::QuirkOverride::ForceOff:
caps.UseInvalidImagePlaceholder = false;
break;
case MG_Config::QuirkOverride::Auto:
default:
// ANGLE dereferences a null texture when a dispatch or draw reaches an EMPTY image
// unit the program uses, so the probe cannot be run there: its subject variant is
// exactly that dispatch, and a crash is no verdict. ANGLE keeps the placeholder
// without being asked.
caps.UseInvalidImagePlaceholder = caps.IsAngleRenderer || SelfTest::EmptyImageUnitsReadNonzero(glesFuncs);
break;
}
MGLOG_I(" Invalid image units bound to a placeholder: %s",
!caps.UseInvalidImagePlaceholder ? "false (an invalid image unit stays empty)"
: caps.IsAngleRenderer ? "true (ANGLE faults on a use of an empty image unit)"
: "true (empty image units read nonzero)");
caps.IsAngleLlvmpipeRenderer =
caps.IsAngleRenderer && caps.GLESRendererString.find("llvmpipe") != String::npos;
caps.IsAngleLlvmpipeRenderer = caps.IsAngleRenderer && caps.GLESRendererString.find("llvmpipe") != String::npos;
caps.AvoidSamplerMipmapMinFilter =
caps.IsAngleLlvmpipeRenderer && MG_Config::Features.EsprytAvoidSamplerMipmapMinFilter;
caps.AvoidExplicitLodBias =
caps.IsAngleLlvmpipeRenderer && MG_Config::Features.EsprytAvoidExplicitLodBias;
MGLOG_I(" GL_EXT_disjoint_timer_query supported: %s",
caps.SupportsDisjointTimerQuery ? "true" : "false");
caps.AvoidExplicitLodBias = caps.IsAngleLlvmpipeRenderer && MG_Config::Features.EsprytAvoidExplicitLodBias;
MGLOG_I(" GL_EXT_disjoint_timer_query supported: %s", caps.SupportsDisjointTimerQuery ? "true" : "false");
MGLOG_I(" GL_KHR_parallel_shader_compile supported: %s",
caps.SupportsParallelShaderCompile ? "true" : "false");
MGLOG_I(" ANGLE renderer: %s", caps.IsAngleRenderer ? "true" : "false");
MGLOG_I(" ANGLE llvmpipe renderer: %s", caps.IsAngleLlvmpipeRenderer ? "true" : "false");
MGLOG_I(" Avoid sampler mipmap min filter: %s",
caps.AvoidSamplerMipmapMinFilter ? "true" : "false");
MGLOG_I(" Avoid sampler mipmap min filter: %s", caps.AvoidSamplerMipmapMinFilter ? "true" : "false");
MGLOG_I(" Avoid explicit LOD bias: %s", caps.AvoidExplicitLodBias ? "true" : "false");
// Last line of defence. Capability init is the very first thing that touches the driver,
@@ -614,11 +614,9 @@ namespace MobileGL {
// suffixed spellings instead, and a strict eglGetProcAddress returns NULL for the core
// one there - so resolving only the core name makes both extension tiers look absent.
GL_FUNC_TYPEDEF(void, glTextureViewEXT, GLuint texture, GLenum target, GLuint origtexture,
GLenum internalformat, GLuint minlevel, GLuint numlevels, GLuint minlayer,
GLuint numlayers)
GLenum internalformat, GLuint minlevel, GLuint numlevels, GLuint minlayer, GLuint numlayers)
GL_FUNC_TYPEDEF(void, glTextureViewOES, GLuint texture, GLenum target, GLuint origtexture,
GLenum internalformat, GLuint minlevel, GLuint numlevels, GLuint minlayer,
GLuint numlayers)
GLenum internalformat, GLuint minlevel, GLuint numlevels, GLuint minlayer, GLuint numlayers)
// GL_EXT_fragment_shading_rate (+ _attachment). Extension-only in every ES version.
GL_FUNC_TYPEDEF(void, glShadingRateEXT, GLenum rate)
GL_FUNC_TYPEDEF(void, glShadingRateCombinerOpsEXT, GLenum combinerOp0, GLenum combinerOp1)
@@ -1165,8 +1163,8 @@ namespace MobileGL {
// neither cannot compile such a shader at all. Gate on this, never on the entry point:
// eglGetProcAddress hands back live-looking stubs (see AcquireGLESFunctions).
enum class TextureBufferTier : Uint8 {
None = 0, // no core support and neither extension; glTexBuffer is unusable
CoreEs32, // ES >= 3.2, buffer textures are core and ESSL 320 needs no directive
None = 0, // no core support and neither extension; glTexBuffer is unusable
CoreEs32, // ES >= 3.2, buffer textures are core and ESSL 320 needs no directive
ExtensionEXT, // GL_EXT_texture_buffer; ESSL below 320 must say GL_EXT_texture_buffer
ExtensionOES, // GL_OES_texture_buffer; ESSL below 320 must say GL_OES_texture_buffer
};
@@ -1457,8 +1455,8 @@ namespace MobileGL {
// caller presents with plain eglSwapBuffers, which a zero-rectangle call to either
// spelling is defined to be equivalent to anyway. Exposed so MG_Test can pin the
// preference order without a driver.
MG_External::EGL::eglSwapBuffersWithDamageEXT_PTR
ResolveSwapBuffersWithDamage(const MG_External::EGLFunctionsTable& funcs);
MG_External::EGL::eglSwapBuffersWithDamageEXT_PTR ResolveSwapBuffersWithDamage(
const MG_External::EGLFunctionsTable& funcs);
Bool FillInGLESCapabilities(MG_External::GLESCapabilities& caps,
const MG_External::GLESFunctionsTable& glesFuncs);
// Detects whether indirect draws leak the command's baseInstance word into
@@ -105,8 +105,7 @@ namespace MobileGL::MG_Util::BackendLoader {
}
inline Bool HasUsableShaderSubgroupSupport(const VkPhysicalDeviceSubgroupProperties& subgroupProps) {
return subgroupProps.subgroupSize > 0 &&
(subgroupProps.supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) != 0 &&
return subgroupProps.subgroupSize > 0 && (subgroupProps.supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) != 0 &&
(subgroupProps.supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) != 0;
}
@@ -236,15 +235,14 @@ namespace MobileGL::MG_Util::BackendLoader {
// for one of them refuses per-slice attachment for that format only, which
// VkTextureManager detects and records at image creation; this field just says whether
// the capability is worth offering at all.
static constexpr VkFormat k3DSliceProbeFormats[] = {VK_FORMAT_R8G8B8A8_UNORM,
VK_FORMAT_R8G8B8A8_SRGB};
static constexpr VkFormat k3DSliceProbeFormats[] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB};
Bool all2DArrayCompatible = true;
for (const VkFormat probeFormat : k3DSliceProbeFormats) {
VkImageFormatProperties probeProperties{};
const VkResult probeResult = vkGetPhysicalDeviceImageFormatProperties(
physicalDevice, probeFormat, VK_IMAGE_TYPE_3D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT, &probeProperties);
if (probeResult != VK_SUCCESS) {
all2DArrayCompatible = false;
@@ -253,16 +251,15 @@ namespace MobileGL::MG_Util::BackendLoader {
}
caps.Supports2DArrayCompatible3DImages = all2DArrayCompatible;
}
caps.SupportsVertexPipelineStoresAndAtomics =
supportedFeatures.vertexPipelineStoresAndAtomics == VK_TRUE;
caps.SupportsVertexPipelineStoresAndAtomics = supportedFeatures.vertexPipelineStoresAndAtomics == VK_TRUE;
caps.SupportsFragmentStoresAndAtomics = supportedFeatures.fragmentStoresAndAtomics == VK_TRUE;
caps.SupportsGeometryShader = supportedFeatures.geometryShader == VK_TRUE;
caps.SupportsTessellationShader = supportedFeatures.tessellationShader == VK_TRUE;
caps.SupportsShaderClipDistance = supportedFeatures.shaderClipDistance == VK_TRUE;
caps.SupportsShaderCullDistance = supportedFeatures.shaderCullDistance == VK_TRUE;
caps.MaxShaderStorageBlockSize = static_cast<SizeT>(p.limits.maxStorageBufferRange);
const Bool supportsShaderSubgroup = vk.vkGetPhysicalDeviceProperties2 &&
HasUsableShaderSubgroupSupport(subgroupProps);
const Bool supportsShaderSubgroup =
vk.vkGetPhysicalDeviceProperties2 && HasUsableShaderSubgroupSupport(subgroupProps);
caps.SupportsShaderSubgroup = supportsShaderSubgroup;
if (caps.SupportsShaderSubgroup) {
caps.SubgroupSize = subgroupProps.subgroupSize;
@@ -277,8 +274,8 @@ namespace MobileGL::MG_Util::BackendLoader {
}
MGLOG_I("Vulkan shader subgroup support: native=%s size=%u stages=0x%x operations=0x%x",
supportsShaderSubgroup ? "true" : "false", subgroupProps.subgroupSize,
subgroupProps.supportedStages, subgroupProps.supportedOperations);
supportsShaderSubgroup ? "true" : "false", subgroupProps.subgroupSize, subgroupProps.supportedStages,
subgroupProps.supportedOperations);
return true;
}
@@ -290,8 +287,7 @@ namespace MobileGL::MG_Util::BackendLoader {
caps.DriverVersionString = DecodeDriverVersion(properties.driverVersion);
caps.VendorId = properties.vendorID;
caps.UniformBufferOffsetAlignment = static_cast<int>(properties.limits.minUniformBufferOffsetAlignment);
caps.ShaderStorageBufferOffsetAlignment =
static_cast<int>(properties.limits.minStorageBufferOffsetAlignment);
caps.ShaderStorageBufferOffsetAlignment = static_cast<int>(properties.limits.minStorageBufferOffsetAlignment);
caps.AliasedLineWidthRangeMin = properties.limits.lineWidthRange[0];
caps.AliasedLineWidthRangeMax = properties.limits.lineWidthRange[1];
caps.MaxSamplerAnisotropy = properties.limits.maxSamplerAnisotropy;
@@ -139,10 +139,10 @@ namespace MobileGL {
return TextureInternalFormat::RGBA16;
default:
MGLOG_W_ONCE("%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, "
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
return internalformat;
}
}
@@ -152,10 +152,10 @@ namespace MobileGL {
return TextureInternalFormat::RGB8;
default:
MGLOG_W_ONCE("%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, "
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
return internalformat;
}
}
@@ -167,10 +167,10 @@ namespace MobileGL {
return TextureInternalFormat::RG16;
default:
MGLOG_W_ONCE("%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, "
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
return internalformat;
}
}
@@ -182,10 +182,10 @@ namespace MobileGL {
return TextureInternalFormat::R16;
default:
MGLOG_W_ONCE("%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, "
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
"returning original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
return internalformat;
}
}
@@ -217,11 +217,12 @@ namespace MobileGL {
}
}
default: {
MGLOG_W_ONCE("%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, returning "
"original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
MGLOG_W_ONCE(
"%s: Can't infer sized internal format from internalformat=%s, format=%s, type=%s, returning "
"original.",
__func__, MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str(),
MG_Util::ConvertTextureInputFormatToString(format).c_str(),
MG_Util::ConvertTexturePixelDataTypeToString(type).c_str());
return internalformat;
}
}
@@ -311,7 +312,7 @@ namespace MobileGL {
return TextureInternalFormat::DepthStencil;
default:
MGLOG_W_ONCE("%s: Unknown or unhandled internal format %s, returning original.", __func__,
MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str());
MG_Util::ConvertTextureInternalFormatToString(internalformat).c_str());
return internalformat;
}
}
+17 -11
View File
@@ -105,7 +105,8 @@ namespace MobileGL::MG_Util::Damage {
for (const Rect& taken : out) {
Vector<Rect> next;
for (const Rect& piece : pieces) {
for (const Rect& left : Subtract(piece, taken)) next.push_back(left);
for (const Rect& left : Subtract(piece, taken))
next.push_back(left);
}
pieces = std::move(next);
if (pieces.empty()) break;
@@ -126,7 +127,8 @@ namespace MobileGL::MG_Util::Damage {
}
static Region FromRects(const Rect* rects, SizeT count) {
Region region;
for (SizeT i = 0; i < count; ++i) region.Add(rects[i]);
for (SizeT i = 0; i < count; ++i)
region.Add(rects[i]);
return region;
}
// EGL_KHR/EXT_swap_buffers_with_damage's argument: `count` groups of x, y, width, height,
@@ -157,7 +159,8 @@ namespace MobileGL::MG_Util::Damage {
SetFull();
return;
}
for (const Rect& rect : other.m_rects) Add(rect);
for (const Rect& rect : other.m_rects)
Add(rect);
}
void SetFull() {
m_full = true;
@@ -199,7 +202,8 @@ namespace MobileGL::MG_Util::Damage {
// Far too many to pair up every frame: their bounding box.
if (distinct.size() > 4 * kMaxRects && distinct.size() > limit) {
Rect box;
for (const Rect& rect : distinct) box = Bound(box, rect);
for (const Rect& rect : distinct)
box = Bound(box, rect);
distinct.assign(1, box);
}
while (distinct.size() > limit) {
@@ -229,7 +233,8 @@ namespace MobileGL::MG_Util::Damage {
// The rectangles flipped between bottom-left and top-left origin (a Full region stays Full).
Region FlippedY(Int32 height) const {
Region region = *this;
for (Rect& rect : region.m_rects) rect = FlipY(rect, height);
for (Rect& rect : region.m_rects)
rect = FlipY(rect, height);
return region;
}
@@ -253,7 +258,8 @@ namespace MobileGL::MG_Util::Damage {
const Vector<Rect>& rects = region.Rects();
if (rects.size() > maxRects) {
Rect box;
for (const Rect& rect : rects) box = Bound(box, rect);
for (const Rect& rect : rects)
box = Bound(box, rect);
out[0] = box.X, out[1] = box.Y, out[2] = box.Width, out[3] = box.Height;
return 1;
}
@@ -303,7 +309,8 @@ namespace MobileGL::MG_Util::Damage {
if (clipped.IsFull()) return {FlippedCopy{whole, whole}};
Vector<FlippedCopy> copies;
copies.reserve(clipped.Rects().size());
for (const Rect& rect : clipped.Rects()) copies.push_back({rect, FlipY(rect, height)});
for (const Rect& rect : clipped.Rects())
copies.push_back({rect, FlipY(rect, height)});
return copies;
}
@@ -312,9 +319,7 @@ namespace MobileGL::MG_Util::Damage {
public:
explicit BufferDamageTracker(SizeT buffers = 0) { Resize(buffers); }
void Resize(SizeT buffers) {
m_missed.assign(buffers, Region::Full());
}
void Resize(SizeT buffers) { m_missed.assign(buffers, Region::Full()); }
SizeT Size() const { return m_missed.size(); }
// The buffer's contents are unknown (new, reallocated, resized): its next write copies all.
@@ -322,7 +327,8 @@ namespace MobileGL::MG_Util::Damage {
if (index < m_missed.size()) m_missed[index].SetFull();
}
void InvalidateAll() {
for (Region& region : m_missed) region.SetFull();
for (Region& region : m_missed)
region.SetFull();
}
// A frame whose damage is `frame` is written into buffer `index` of a `width` x `height`
+26 -13
View File
@@ -50,7 +50,9 @@ namespace MobileGL {
t_forwardsToPeer = forwarder != nullptr;
}
void SetThreadForwardsLogToPeer(bool forwards) { t_forwardsToPeer = forwards; }
void SetThreadForwardsLogToPeer(bool forwards) {
t_forwardsToPeer = forwards;
}
void WithLogBarrier(void (*action)(void*), void* user) {
std::lock_guard<std::mutex> lock(LogMutex());
@@ -63,12 +65,18 @@ namespace MobileGL {
int LogLevelFromAndroidPriority(int priority) {
switch (priority) {
case ANDROID_LOG_VERBOSE:
case ANDROID_LOG_DEBUG: return MOBILEGL_LOG_LEVEL_DEBUG;
case ANDROID_LOG_INFO: return MOBILEGL_LOG_LEVEL_INFO;
case ANDROID_LOG_WARN: return MOBILEGL_LOG_LEVEL_WARN;
case ANDROID_LOG_ERROR: return MOBILEGL_LOG_LEVEL_ERROR;
case ANDROID_LOG_FATAL: return MOBILEGL_LOG_LEVEL_FATAL;
default: return MOBILEGL_LOG_LEVEL_INFO;
case ANDROID_LOG_DEBUG:
return MOBILEGL_LOG_LEVEL_DEBUG;
case ANDROID_LOG_INFO:
return MOBILEGL_LOG_LEVEL_INFO;
case ANDROID_LOG_WARN:
return MOBILEGL_LOG_LEVEL_WARN;
case ANDROID_LOG_ERROR:
return MOBILEGL_LOG_LEVEL_ERROR;
case ANDROID_LOG_FATAL:
return MOBILEGL_LOG_LEVEL_FATAL;
default:
return MOBILEGL_LOG_LEVEL_INFO;
}
}
@@ -125,7 +133,9 @@ namespace MobileGL {
return t_logRole == 1;
}
void SetThreadLogRole(LogRole role) { t_logRole = role == LogRole::Server ? 1 : 0; }
void SetThreadLogRole(LogRole role) {
t_logRole = role == LogRole::Server ? 1 : 0;
}
std::string ReadRoleLogs(const char* basePath) {
std::string all;
@@ -186,7 +196,9 @@ namespace MobileGL {
return t_logRole == 1 ? LogRole::Server : LogRole::Client;
}
bool CurrentProcessIsSpawnedServer() { return ProcessIsSpawnedServer(); }
bool CurrentProcessIsSpawnedServer() {
return ProcessIsSpawnedServer();
}
#endif
std::mutex& LogMutex() {
@@ -338,8 +350,7 @@ namespace MobileGL {
// every MobileGL log occupy TWO logcat records, the second one empty. That
// halved the useful depth of every `adb logcat -t N` window the CI
// diagnostics read (android-plugin/trace-replay-ci.sh).
__android_log_print(androidLogLevel, "MobileGL", "%.*s", static_cast<int>(out.size() - 1),
out.c_str());
__android_log_print(androidLogLevel, "MobileGL", "%.*s", static_cast<int>(out.size() - 1), out.c_str());
#endif
WriteToFile(out.c_str());
@@ -354,8 +365,10 @@ namespace MobileGL {
// A FATAL line goes out after the log mutex is released, like a session-scoped
// one: the forwarder may wait (bounded) for it to reach the peer before the
// process aborts, and must not hold every other thread's line while it does.
if (level >= MOBILEGL_LOG_LEVEL_FATAL) forwardOutsideLock = true;
else s_forwarder(s_forwarderUser, level, out.c_str());
if (level >= MOBILEGL_LOG_LEVEL_FATAL)
forwardOutsideLock = true;
else
s_forwarder(s_forwarderUser, level, out.c_str());
} else {
forwardOutsideLock = t_forwardsToPeer;
}
+14 -4
View File
@@ -198,7 +198,10 @@ namespace MobileGL {
// THREAD-SCOPED, NOT PROCESS-SCOPED, for the case that is easy to miss: under inproc
// the server role is the mgl-srv-apply THREAD, so a process-wide flag would file its
// lines under whichever role set it last.
enum class LogRole { Client, Server };
enum class LogRole {
Client,
Server
};
// Called by the apply thread when it starts (inproc), and once by ServerMain for a
// whole spawned server process. Without it a thread defaults to the process role,
@@ -252,7 +255,10 @@ namespace MobileGL {
// byte-identical to the baseline, so these may not become library symbols. Nothing
// inside the library calls them - only tests and harnesses do - so an inline
// definition emits code into those readers and nothing at all into libMobileGL.so.
enum class LogRole { Client, Server };
enum class LogRole {
Client,
Server
};
// No roles to separate, so nothing to record. Present so that a harness may say which
// role a thread is without asking which build it is in.
@@ -267,8 +273,12 @@ namespace MobileGL {
// one role in this build, so "which role is this thread" has exactly one true answer
// and no state to read - but a caller must still compile and mean the right thing in
// both flavours rather than wrapping these in an `#if` it might get wrong once.
inline LogRole CurrentThreadRole() { return LogRole::Client; }
inline bool CurrentProcessIsSpawnedServer() { return false; }
inline LogRole CurrentThreadRole() {
return LogRole::Client;
}
inline bool CurrentProcessIsSpawnedServer() {
return false;
}
inline std::string ReadRoleLogs(const char* basePath) {
std::string all;
+17 -12
View File
@@ -46,8 +46,12 @@ namespace MobileGL::MG_Util {
(mantissa >> (23u - static_cast<Uint32>(mantissaBits)));
}
inline Uint32 EncodeFloatToUnsignedF11(Float value) { return EncodeFloatToUnsignedSmallFloat(value, 6); }
inline Uint32 EncodeFloatToUnsignedF10(Float value) { return EncodeFloatToUnsignedSmallFloat(value, 5); }
inline Uint32 EncodeFloatToUnsignedF11(Float value) {
return EncodeFloatToUnsignedSmallFloat(value, 6);
}
inline Uint32 EncodeFloatToUnsignedF10(Float value) {
return EncodeFloatToUnsignedSmallFloat(value, 5);
}
// Decodes an unsigned small float (5-bit exponent, bias 15, mantissaBits mantissa bits).
inline Float DecodeUnsignedSmallFloatToFloat(Uint32 field, Int mantissaBits) {
@@ -58,15 +62,17 @@ namespace MobileGL::MG_Util {
return std::exp2(-14.0f) * static_cast<Float>(mantissa) * mantissaScale;
}
if (exponent == 31) {
return mantissa == 0 ? std::numeric_limits<Float>::infinity()
: std::numeric_limits<Float>::quiet_NaN();
return mantissa == 0 ? std::numeric_limits<Float>::infinity() : std::numeric_limits<Float>::quiet_NaN();
}
return std::exp2(static_cast<Float>(exponent) - 15.0f) *
(1.0f + static_cast<Float>(mantissa) * mantissaScale);
return std::exp2(static_cast<Float>(exponent) - 15.0f) * (1.0f + static_cast<Float>(mantissa) * mantissaScale);
}
inline Float DecodeUnsignedF11ToFloat(Uint32 field) { return DecodeUnsignedSmallFloatToFloat(field, 6); }
inline Float DecodeUnsignedF10ToFloat(Uint32 field) { return DecodeUnsignedSmallFloatToFloat(field, 5); }
inline Float DecodeUnsignedF11ToFloat(Uint32 field) {
return DecodeUnsignedSmallFloatToFloat(field, 6);
}
inline Float DecodeUnsignedF10ToFloat(Uint32 field) {
return DecodeUnsignedSmallFloatToFloat(field, 5);
}
// RGB9E5 shared-exponent encode, following the EXT_texture_shared_exponent spec algorithm
// (N = 9 mantissa bits, B = 15 exponent bias, Emax = 31).
@@ -84,11 +90,10 @@ namespace MobileGL::MG_Util {
Int sharedExponent = 0; // all-zero input keeps the all-zero word
if (maxComponent > 0.0f) {
sharedExponent = std::max(-kExponentBias - 1, static_cast<Int>(std::floor(std::log2(maxComponent)))) +
1 + kExponentBias;
sharedExponent =
std::max(-kExponentBias - 1, static_cast<Int>(std::floor(std::log2(maxComponent)))) + 1 + kExponentBias;
const Float maxScaled = std::floor(
maxComponent / std::exp2(static_cast<Float>(sharedExponent - kExponentBias - kMantissaBits)) +
0.5f);
maxComponent / std::exp2(static_cast<Float>(sharedExponent - kExponentBias - kMantissaBits)) + 0.5f);
if (maxScaled >= 512.0f) { // rounded up to 2^N: bump the shared exponent instead
++sharedExponent;
}
+8 -4
View File
@@ -12,10 +12,14 @@ namespace MobileGL {
namespace MG_Util {
Uint32 FixedRestartIndexForGLType(GLenum indexType) {
switch (indexType) {
case GL_UNSIGNED_BYTE: return 0xFFu;
case GL_UNSIGNED_SHORT: return 0xFFFFu;
case GL_UNSIGNED_INT: return 0xFFFFFFFFu;
default: return 0;
case GL_UNSIGNED_BYTE:
return 0xFFu;
case GL_UNSIGNED_SHORT:
return 0xFFFFu;
case GL_UNSIGNED_INT:
return 0xFFFFFFFFu;
default:
return 0;
}
}
+107 -67
View File
@@ -188,7 +188,9 @@ namespace MobileGL::MG_Util::PipeStats {
counter.fetch_add(amount, std::memory_order_relaxed);
}
inline Uint64 Read(const Counter& counter) { return counter.load(std::memory_order_relaxed); }
inline Uint64 Read(const Counter& counter) {
return counter.load(std::memory_order_relaxed);
}
// Bucket 0 is "0 bytes", bucket n>0 holds [2^(n-1), 2^n). Saturates at the last
// bucket so a pathological record cannot index out of the array.
@@ -218,22 +220,38 @@ namespace MobileGL::MG_Util::PipeStats {
}
const char* const kByteClassNames[kByteClassCount] = {
"stage-buffer", "stage-texture", "stage-ubo-global",
"stage-ubo-named", "stage-vertex-client", "stage-index-client",
"stage-indirect-cmd", "persistent-map-push", "residual-value-block",
"cso-blob-bytes", "stage-segment-bytes",
"stage-buffer", "stage-texture", "stage-ubo-global", "stage-ubo-named",
"stage-vertex-client", "stage-index-client", "stage-indirect-cmd", "persistent-map-push",
"residual-value-block", "cso-blob-bytes", "stage-segment-bytes",
};
const char* const kCallClassNames[kCallClassCount] = {
"draws", "accessor-calls", "tex-upload-emissions", "tex-upload-box", "tex-upload-rect",
"draws",
"accessor-calls",
"tex-upload-emissions",
"tex-upload-box",
"tex-upload-rect",
"tex-upload-jobs",
"render-state-cso-mints", "render-state-cso-binds", "map-persistent-roundtrips",
"framebuffer-emissions", "sampler-view-emissions", "sampler-state-emissions",
"shader-image-emissions", "client-tex-upload-emissions", "tex-remint-pulls",
"render-state-cso-mints",
"render-state-cso-binds",
"map-persistent-roundtrips",
"framebuffer-emissions",
"sampler-view-emissions",
"sampler-state-emissions",
"shader-image-emissions",
"client-tex-upload-emissions",
"tex-remint-pulls",
"resident-subdata-emissions",
"residual-pulls", "server-verb-boundaries", "wire-records",
"wire-indirect-native-draws", "wire-indirect-cpu-expansions", "wire-indirect-barriers",
"wire-host-waits", "wire-host-waits-indirect", "wire-host-wait-us",
"wire-indirect-native-dispatches", "resource-readbacks",
"residual-pulls",
"server-verb-boundaries",
"wire-records",
"wire-indirect-native-draws",
"wire-indirect-cpu-expansions",
"wire-indirect-barriers",
"wire-host-waits",
"wire-host-waits-indirect",
"wire-host-wait-us",
"wire-indirect-native-dispatches",
"resource-readbacks",
};
const char* const kGateNames[kGateCount] = {
"espryt-render-state", "espryt-texture-sync-list", "espryt-unit-bindings-epoch",
@@ -254,9 +272,8 @@ namespace MobileGL::MG_Util::PipeStats {
// "csob-blob" and not "csob": the cso[] bracket below already prints csob= for the
// render-state CSO BIND count, and two different numbers under one grep is how a
// recorded baseline stops meaning anything.
const char* const kByteClassShort[kByteClassCount] = {"buf", "tex", "ubog", "ubon", "vtxc",
"idxc", "icmd", "pmap", "resid",
"csob-blob", "seg",
const char* const kByteClassShort[kByteClassCount] = {
"buf", "tex", "ubog", "ubon", "vtxc", "idxc", "icmd", "pmap", "resid", "csob-blob", "seg",
};
const char* const kGateShort[kGateCount] = {"ers", "etl", "eub", "mfp", "mpm", "mdt"};
@@ -353,8 +370,7 @@ namespace MobileGL::MG_Util::PipeStats {
#endif
std::ofstream out(path, std::ios::out | std::ios::trunc);
if (!out) {
MGLOG_W("PipeStats: could not open MOBILEGL_PIPE_STATS_FILE='%s' for writing",
path.c_str());
MGLOG_W("PipeStats: could not open MOBILEGL_PIPE_STATS_FILE='%s' for writing", path.c_str());
return;
}
out << FormatJson();
@@ -388,8 +404,8 @@ namespace MobileGL::MG_Util::PipeStats {
// the price G1 charges for adding anything to this file at all.
const String dumpPath = RoleDerivedJsonDumpPathForTesting();
MGLOG_I("MGPipe stats: counters ON (MOBILEGL_PIPE_STATS), summary every %llu frames%s%s",
static_cast<unsigned long long>(g_summaryPeriod),
dumpPath.empty() ? "" : ", JSON dump to ", dumpPath.c_str());
static_cast<unsigned long long>(g_summaryPeriod), dumpPath.empty() ? "" : ", JSON dump to ",
dumpPath.c_str());
#else
MGLOG_I("MGPipe stats: counters ON (MOBILEGL_PIPE_STATS), summary every %llu frames%s%s",
static_cast<unsigned long long>(g_summaryPeriod),
@@ -399,7 +415,9 @@ namespace MobileGL::MG_Util::PipeStats {
}
}
Uint64 SummaryFramePeriod() { return g_summaryPeriod; }
Uint64 SummaryFramePeriod() {
return g_summaryPeriod;
}
void Shutdown() {
if (!g_pipeStatsEnabled || g_shutdownDone) {
@@ -431,7 +449,9 @@ namespace MobileGL::MG_Util::PipeStats {
g_gauges[static_cast<Uint32>(gauge)].store(value, std::memory_order_relaxed);
}
Uint64 GaugeValue(Gauge gauge) { return Read(g_gauges[static_cast<Uint32>(gauge)]); }
Uint64 GaugeValue(Gauge gauge) {
return Read(g_gauges[static_cast<Uint32>(gauge)]);
}
void CountGate(Gate gate, Bool hit) {
const Uint32 index = static_cast<Uint32>(gate);
@@ -444,7 +464,9 @@ namespace MobileGL::MG_Util::PipeStats {
}
}
void RecordDrawPayloadBytes(Uint64 bytes) { Bump(g_totalPayloadBuckets[PayloadBucketOf(bytes)], 1); }
void RecordDrawPayloadBytes(Uint64 bytes) {
Bump(g_totalPayloadBuckets[PayloadBucketOf(bytes)], 1);
}
// P6 gate 8's staged-blob distribution. A count, not a sum: the bytes already have a home
// in stage-segment-bytes, and what this exists to answer is the SHAPE (how many blobs there
@@ -499,23 +521,43 @@ namespace MobileGL::MG_Util::PipeStats {
}
}
Uint64 FrameBytes(ByteClass byteClass) { return Read(g_frameBytes[static_cast<Uint32>(byteClass)]); }
Uint64 TotalBytes(ByteClass byteClass) { return Read(g_totalBytes[static_cast<Uint32>(byteClass)]); }
Uint64 FrameCalls(CallClass callClass) { return Read(g_frameCalls[static_cast<Uint32>(callClass)]); }
Uint64 TotalCalls(CallClass callClass) { return Read(g_totalCalls[static_cast<Uint32>(callClass)]); }
Uint64 TotalGateHits(Gate gate) { return Read(g_totalGateHit[static_cast<Uint32>(gate)]); }
Uint64 TotalGateMisses(Gate gate) { return Read(g_totalGateMiss[static_cast<Uint32>(gate)]); }
Uint64 FrameBytes(ByteClass byteClass) {
return Read(g_frameBytes[static_cast<Uint32>(byteClass)]);
}
Uint64 TotalBytes(ByteClass byteClass) {
return Read(g_totalBytes[static_cast<Uint32>(byteClass)]);
}
Uint64 FrameCalls(CallClass callClass) {
return Read(g_frameCalls[static_cast<Uint32>(callClass)]);
}
Uint64 TotalCalls(CallClass callClass) {
return Read(g_totalCalls[static_cast<Uint32>(callClass)]);
}
Uint64 TotalGateHits(Gate gate) {
return Read(g_totalGateHit[static_cast<Uint32>(gate)]);
}
Uint64 TotalGateMisses(Gate gate) {
return Read(g_totalGateMiss[static_cast<Uint32>(gate)]);
}
Uint64 TotalPayloadBucket(Uint32 bucket) {
return bucket < kPayloadHistogramBuckets ? Read(g_totalPayloadBuckets[bucket]) : 0;
}
Uint64 TotalStagedBlobBucket(Uint32 bucket) {
return bucket < kStagedBlobHistogramBuckets ? Read(g_totalStagedBlobBuckets[bucket]) : 0;
}
Uint64 FrameCount() { return Read(g_frameCount); }
Uint64 FrameCount() {
return Read(g_frameCount);
}
const char* NameOf(ByteClass byteClass) { return kByteClassNames[static_cast<Uint32>(byteClass)]; }
const char* NameOf(CallClass callClass) { return kCallClassNames[static_cast<Uint32>(callClass)]; }
const char* NameOf(Gate gate) { return kGateNames[static_cast<Uint32>(gate)]; }
const char* NameOf(ByteClass byteClass) {
return kByteClassNames[static_cast<Uint32>(byteClass)];
}
const char* NameOf(CallClass callClass) {
return kCallClassNames[static_cast<Uint32>(callClass)];
}
const char* NameOf(Gate gate) {
return kGateNames[static_cast<Uint32>(gate)];
}
String FormatWindowLine() {
// Window values: everything since the previous summary. A run total over a workload
@@ -589,8 +631,7 @@ namespace MobileGL::MG_Util::PipeStats {
line += " sve=" + std::to_string(calls[static_cast<Uint32>(CallClass::SamplerViewEmissions)]);
line += " sse=" + std::to_string(calls[static_cast<Uint32>(CallClass::SamplerStateEmissions)]);
line += " sie=" + std::to_string(calls[static_cast<Uint32>(CallClass::ShaderImageEmissions)]);
line += " ctu=" +
std::to_string(calls[static_cast<Uint32>(CallClass::ClientTextureUploadEmissions)]);
line += " ctu=" + std::to_string(calls[static_cast<Uint32>(CallClass::ClientTextureUploadEmissions)]);
// trp is the texture re-mint count (ROADMAP open question 2): every one is a texture
// Espryt had already allocated and then had to re-mint image-bindable because
// ImageBindableHint reached it too late - replaying the client's shadow in monolith, the
@@ -602,8 +643,7 @@ namespace MobileGL::MG_Util::PipeStats {
// buffer are identical either way - so it is what the white-box cases read. Zero under
// monolith by construction, and zero under split too until the server publishes
// kCapResidentSubData off its own wire resource table (MG_Backend/Init.cpp).
line += " rsd=" +
std::to_string(calls[static_cast<Uint32>(CallClass::ResidentSubDataEmissions)]);
line += " rsd=" + std::to_string(calls[static_cast<Uint32>(CallClass::ResidentSubDataEmissions)]);
// rsp is P5's residual-pull count: reads, on the server side, of a PipeInputs field no
// pushed record supplies, answered out of the client's residual fill under the verb
// barrier. It is the SIZE OF THE P6/P7/P8 DEBT and it is published on this line rather
@@ -617,8 +657,7 @@ namespace MobileGL::MG_Util::PipeStats {
// the monolith arm never stamps, so a green lane with vbs=0 says only that the mechanism
// never ran. It is deliberately NOT rsp: rsp reaches zero when the phase SUCCEEDS, so a
// positive control built on it would fail on the day the debt is paid.
line += " vbs=" +
std::to_string(calls[static_cast<Uint32>(CallClass::ServerVerbBoundaries)]);
line += " vbs=" + std::to_string(calls[static_cast<Uint32>(CallClass::ServerVerbBoundaries)]);
// P6 GATE 8's SECOND NUMBER (CONTRACT-P6.md §9 item 8): records/frame AFTER chunking, i.e.
// the count of records the wire encoder actually committed this window rather than the
// number of times an emitter was asked for one. The gap between the two is the whole
@@ -633,10 +672,8 @@ namespace MobileGL::MG_Util::PipeStats {
// its numerator times the frame count is a formatting bug, and a per-frame figure over a
// window with no Present in it is the 47x overstatement PipeStatsTest already pins.
line += " wrec=" + std::to_string(calls[static_cast<Uint32>(CallClass::WireRecords)]);
line += " wrec/f=" + (perFrame
? FormatFixed2(calls[static_cast<Uint32>(CallClass::WireRecords)],
windowFrames)
: std::to_string(calls[static_cast<Uint32>(CallClass::WireRecords)]));
line += " wrec/f=" + (perFrame ? FormatFixed2(calls[static_cast<Uint32>(CallClass::WireRecords)], windowFrames)
: std::to_string(calls[static_cast<Uint32>(CallClass::WireRecords)]));
// P5's three wire gauges, and THEY ARE RUN TOTALS on a line whose every other field is
// a window - see the Gauge enum for the argument. `maxrec` is R-10's proof obligation
// (BRIEF 8 item 3): the largest single record this run wrote, in BYTES, beside the cap
@@ -674,8 +711,7 @@ namespace MobileGL::MG_Util::PipeStats {
line += " wdefsync=" + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireDeferredSyncs)]));
// P8-D: the Magma wire arm's indirect draws and its host-access waits, windowed like the
// other CallClass fields (per window, not per frame: the create fixtures hold one frame).
line += "] windr[wind=" +
std::to_string(calls[static_cast<Uint32>(CallClass::WireIndirectNativeDraws)]);
line += "] windr[wind=" + std::to_string(calls[static_cast<Uint32>(CallClass::WireIndirectNativeDraws)]);
line += " wixp=" + std::to_string(calls[static_cast<Uint32>(CallClass::WireIndirectCpuExpansions)]);
line += " wibar=" + std::to_string(calls[static_cast<Uint32>(CallClass::WireIndirectBarriers)]);
line += " whw=" + std::to_string(calls[static_cast<Uint32>(CallClass::WireHostWaits)]);
@@ -739,30 +775,30 @@ namespace MobileGL::MG_Util::PipeStats {
}
// The gauges, under their long names. Run totals here as on the summary line.
json += " },\n \"wire\": {\n";
json += " \"max-record-bytes\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::MaxRecordBytes)])) + ",\n";
json +=
" \"max-record-bytes\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::MaxRecordBytes)])) +
",\n";
json += " \"max-record-bytes-cap\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::MaxRecordBytesCap)])) + ",\n";
json += " \"ring-wraps\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWraps)])) + ",\n";
json += " \"ring-wrap-pads\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWrapPads)])) + ",\n";
json += " \"ring-waits\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWaits)])) + ",\n";
json += " \"ring-wraps\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWraps)])) + ",\n";
json += " \"ring-wrap-pads\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWrapPads)])) +
",\n";
json += " \"ring-waits\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::RingWaits)])) + ",\n";
// The wait ledger under its long names, same run totals as the summary line's wait[].
json += " \"server-waits\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ServerWaits)])) + ",\n";
json += " \"server-parks\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ServerParks)])) + ",\n";
json += " \"client-waits\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ClientWaits)])) + ",\n";
json += " \"client-parks\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ClientParks)])) + ",\n";
json +=
" \"server-waits\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ServerWaits)])) + ",\n";
json +=
" \"server-parks\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ServerParks)])) + ",\n";
json +=
" \"client-waits\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ClientWaits)])) + ",\n";
json +=
" \"client-parks\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::ClientParks)])) + ",\n";
// P7 wave 4 M2's four, run maxima / a run count as on the summary line's wbuf[].
json += " \"wire-buffers\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireBuffers)])) + ",\n";
json += " \"wire-stores-peak\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireStoresPeak)])) + ",\n";
json +=
" \"wire-buffers\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireBuffers)])) + ",\n";
json +=
" \"wire-stores-peak\": " + std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireStoresPeak)])) +
",\n";
json += " \"wire-deferred-bytes-peak\": " +
std::to_string(Read(g_gauges[static_cast<Uint32>(Gauge::WireDeferredBytesPeak)])) + ",\n";
json += " \"wire-deferred-syncs\": " +
@@ -793,9 +829,13 @@ namespace MobileGL::MG_Util::PipeStats {
return json;
}
void SetEnabledForTesting(Bool enabled) { g_pipeStatsEnabled = enabled; }
void SetEnabledForTesting(Bool enabled) {
g_pipeStatsEnabled = enabled;
}
void ResetForTesting() { ResetCounters(); }
void ResetForTesting() {
ResetCounters();
}
#if MOBILEGL_BUILD_DISAGGREGATED
String RoleDerivedJsonDumpPathForTesting() {
+3 -1
View File
@@ -448,7 +448,9 @@ namespace MobileGL::MG_Util::PipeStats {
// predicted branch - do not turn this into a function call.
extern Bool g_pipeStatsEnabled;
inline Bool Enabled() { return g_pipeStatsEnabled; }
inline Bool Enabled() {
return g_pipeStatsEnabled;
}
// Latches g_pipeStatsEnabled from MG_Config::Features.PipeStats and clears every
// counter. Called from MobileGL::Initialize() right after the config load.
+1 -1
View File
@@ -518,7 +518,7 @@ namespace MobileGL {
break;
default:
MGLOG_W_ONCE("Unimplemented internal format in GetComponentSizesForInternalFormat: %d",
static_cast<Int>(internal));
static_cast<Int>(internal));
break;
}
+39 -22
View File
@@ -135,8 +135,8 @@ namespace {
const uint64_t y = *static_cast<const uint64_t*>(b);
return x < y ? -1 : x > y;
}
#define now_ns NowNs
#define cmp_u64 CompareU64
#define now_ns NowNs
#define cmp_u64 CompareU64
// Failure funnel for the shared scene builder: remember the first failure,
// let the run finish reporting it instead of aborting the service process.
@@ -158,7 +158,7 @@ namespace {
glMultiDrawElementsBaseVertex(mode, counts, type, offsets, drawCount, baseVertices);
}
#include "../../MG_Benchmark/Driver/DriverBenchCases.inc"
#include "../../MG_Benchmark/Driver/DriverBenchCases.inc"
// Frame boundary: a real fence wait. MobileGL's glFinish/glFlush are
// deliberate no-ops, so a glFinish-paced loop would time only CPU submit.
@@ -177,11 +177,21 @@ namespace {
out.reserve(value.size() + 8);
for (const char c : value) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
case '"':
out += "\\\"";
break;
case '\\':
out += "\\\\";
break;
case '\n':
out += "\\n";
break;
case '\r':
out += "\\r";
break;
case '\t':
out += "\\t";
break;
default:
if (static_cast<unsigned char>(c) < 0x20 || static_cast<unsigned char>(c) >= 0x7F) {
char buffer[8];
@@ -206,7 +216,10 @@ namespace {
// Espryt's display teardown is precisely the hazard being avoided.
bool BootEgl(EglBench& out, std::string& error) {
out.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (out.display == EGL_NO_DISPLAY) { error = "eglGetDisplay failed"; return false; }
if (out.display == EGL_NO_DISPLAY) {
error = "eglGetDisplay failed";
return false;
}
EGLint major = 0, minor = 0;
if (eglInitialize(out.display, &major, &minor) != EGL_TRUE) {
char buffer[48];
@@ -214,13 +227,12 @@ namespace {
error = buffer;
return false;
}
const EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 24,
EGL_NONE};
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 24,
EGL_NONE};
EGLConfig config = nullptr;
EGLint numConfigs = 0;
if (eglChooseConfig(out.display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE ||
numConfigs < 1) {
if (eglChooseConfig(out.display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE || numConfigs < 1) {
error = "eglChooseConfig found no pbuffer config";
return false;
}
@@ -296,16 +308,17 @@ namespace {
qsort(samples, static_cast<size_t>(g_frames), sizeof(uint64_t), CompareU64);
const uint64_t median = samples[g_frames / 2];
const double frameMs = static_cast<double>(median) / 1e6;
const double nsPerOp =
benchCase.opsPerFrame > 0 ? static_cast<double>(median) / static_cast<double>(benchCase.opsPerFrame) : 0.0;
const double nsPerOp = benchCase.opsPerFrame > 0
? static_cast<double>(median) / static_cast<double>(benchCase.opsPerFrame)
: 0.0;
const GLenum caseError = glGetError();
char row[256];
snprintf(row, sizeof row,
"%s{\"case\":\"%s\",\"frames\":%d,\"opsPerFrame\":%ld,\"medianFrameMs\":%.3f,"
"\"nsPerOp\":%.1f,\"fps\":%.1f,\"glError\":%u}",
rows.empty() ? "" : ",", benchCase.name, g_frames, benchCase.opsPerFrame, frameMs,
nsPerOp, 1e9 / static_cast<double>(median), caseError);
rows.empty() ? "" : ",", benchCase.name, g_frames, benchCase.opsPerFrame, frameMs, nsPerOp,
1e9 / static_cast<double>(median), caseError);
rows += row;
}
@@ -324,8 +337,10 @@ namespace {
} // namespace
extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_BenchService_nativeRunDriverBench(
JNIEnv* env, jclass, jstring backendType, jint frames, jint warmupFrames) {
extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_BenchService_nativeRunDriverBench(JNIEnv* env, jclass,
jstring backendType,
jint frames,
jint warmupFrames) {
const char* backendChars = backendType ? env->GetStringUTFChars(backendType, nullptr) : nullptr;
const std::string backend = backendChars ? backendChars : "DirectGLES";
if (backendChars) env->ReleaseStringUTFChars(backendType, backendChars);
@@ -333,9 +348,11 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_BenchService_nativ
std::string result;
try {
result = RunBench(backend, frames, warmupFrames);
} catch (const std::exception& e) {
}
catch (const std::exception& e) {
result = std::string("{\"error\":\"") + EscapeJson(e.what()) + "\"}";
} catch (...) {
}
catch (...) {
result = "{\"error\":\"unknown native exception\"}";
}
return env->NewStringUTF(result.c_str());
File diff suppressed because it is too large Load Diff
+8 -11
View File
@@ -186,13 +186,12 @@ namespace MobileGL::MG_Util::SelfTest {
// Every invocation of a 32-wide compute workgroup stores 1 into its shared slot, barrier(),
// reads slot 0, barrier(), stores 7, barrier(), reads slot 0 again, and writes both reads
// to its own result word: the second read must see 7. The Adreno ES compiler treats ordinary shared loads as pure - it hands back the
// first read (and, with other addresses, moves a read past a later store) - while an
// atomicAdd(x, 0u) read of the same words is kept in place. That atomic twin is the
// control: the verdict is "stale" only when the plain program goes wrong while the atomic
// one stays right. Deterministic (a compiler transform), so latched; false when compute
// shaders or an entry point are missing or anything fails to set up. Restores the GL state
// it touches.
// to its own result word: the second read must see 7. The Adreno ES compiler treats ordinary shared loads as pure -
// it hands back the first read (and, with other addresses, moves a read past a later store) - while an atomicAdd(x,
// 0u) read of the same words is kept in place. That atomic twin is the control: the verdict is "stale" only when
// the plain program goes wrong while the atomic one stays right. Deterministic (a compiler transform), so latched;
// false when compute shaders or an entry point are missing or anything fails to set up. Restores the GL state it
// touches.
Bool ProbeSharedMemoryLoadsGoStale(const MG_External::GLESFunctionsTable& gl);
// ProbeSharedMemoryLoadsGoStale(), evaluated at most once per process.
@@ -319,12 +318,10 @@ namespace MobileGL::MG_Util::SelfTest {
//
// `detected` is false unless the strongest shape is dirty AND the control is clean. The
// shape MobileGL emits is measured either way, so the report can say what applications get.
ImageCoherencyResidualMeasurement ProbeImageWriteReadCoherencyResidual(
const MG_External::GLESFunctionsTable& gl);
ImageCoherencyResidualMeasurement ProbeImageWriteReadCoherencyResidual(const MG_External::GLESFunctionsTable& gl);
// ProbeImageWriteReadCoherencyResidual(), evaluated at most once per process.
const ImageCoherencyResidualMeasurement& ImageWriteReadCoherencyResidual(
const MG_External::GLESFunctionsTable& gl);
const ImageCoherencyResidualMeasurement& ImageWriteReadCoherencyResidual(const MG_External::GLESFunctionsTable& gl);
// Copies one known GL_UNSIGNED_SHORT_5_5_5_1 word out of both mip levels of a GL_RGB5_A1
// 2D array into plain 2D images with glCopyImageSubData and reads the landed texels back
+213 -253
View File
@@ -200,21 +200,19 @@ namespace MobileGL::MG_Util::SelfTest {
void AppendAsyncShaderCompileRow(ReportBuilder& builder) {
constexpr const char* rowName = "Asynchronous shader compilation";
if (!MG_Util::Async::AsyncShaderCompileEnabled()) {
builder.Warn(rowName,
"off; GL_KHR_parallel_shader_compile is not advertised and "
"glCompileShader/glLinkProgram run on the calling thread instead. The "
"programs are identical - only the overlap is lost, so a shaderpack load "
"takes as long as its compiles do (set environment variable "
"MOBILEGL_ASYNC_SHADER_COMPILE=1 to enable it)");
builder.Warn(rowName, "off; GL_KHR_parallel_shader_compile is not advertised and "
"glCompileShader/glLinkProgram run on the calling thread instead. The "
"programs are identical - only the overlap is lost, so a shaderpack load "
"takes as long as its compiles do (set environment variable "
"MOBILEGL_ASYNC_SHADER_COMPILE=1 to enable it)");
return;
}
const Uint threads = MG_Util::Async::DetectShaderCompileThreadCount();
builder.Pass(rowName,
format("on with {} compiler thread{}; GL_KHR_parallel_shader_compile is advertised "
"and GL_MAX_SHADER_COMPILER_THREADS_KHR = {} (set environment variable "
"MOBILEGL_ASYNC_SHADER_COMPILE=0 to disable it, or "
"MOBILEGL_ASYNC_SHADER_COMPILE_THREADS=n to change the count)",
threads, threads == 1 ? "" : "s", threads));
builder.Pass(rowName, format("on with {} compiler thread{}; GL_KHR_parallel_shader_compile is advertised "
"and GL_MAX_SHADER_COMPILER_THREADS_KHR = {} (set environment variable "
"MOBILEGL_ASYNC_SHADER_COMPILE=0 to disable it, or "
"MOBILEGL_ASYNC_SHADER_COMPILE_THREADS=n to change the count)",
threads, threads == 1 ? "" : "s", threads));
}
// Appends the four "MobileGL reported ..." rows for one backend section.
@@ -300,16 +298,15 @@ namespace MobileGL::MG_Util::SelfTest {
return;
}
if (deviceLimit > kMobileGLMaxVertexAttribs) {
builder.Warn(rowName,
format("{} = {} (> {}); MobileGL clamps GL_MAX_VERTEX_ATTRIBS to {} because its "
"current-vertex-attribute storage and its Uint32 attribute masks hold {} "
"locations, so the driver's extra attributes stay unusable",
driverLimitName, deviceLimit, kMobileGLMaxVertexAttribs,
kMobileGLMaxVertexAttribs, kMobileGLMaxVertexAttribs));
builder.Warn(rowName, format("{} = {} (> {}); MobileGL clamps GL_MAX_VERTEX_ATTRIBS to {} because its "
"current-vertex-attribute storage and its Uint32 attribute masks hold {} "
"locations, so the driver's extra attributes stay unusable",
driverLimitName, deviceLimit, kMobileGLMaxVertexAttribs,
kMobileGLMaxVertexAttribs, kMobileGLMaxVertexAttribs));
return;
}
builder.Pass(rowName, format("{} = {}; MobileGL advertises GL_MAX_VERTEX_ATTRIBS = {}",
driverLimitName, deviceLimit, deviceLimit));
builder.Pass(rowName, format("{} = {}; MobileGL advertises GL_MAX_VERTEX_ATTRIBS = {}", driverLimitName,
deviceLimit, deviceLimit));
}
// "Shader subgroups": how this backend implements GL_KHR_shader_subgroup on this driver
@@ -361,12 +358,10 @@ namespace MobileGL::MG_Util::SelfTest {
versionLabel + " (< 3.1: no compute shaders or native indirect draws)");
}
EvaluateVertexAttribLimit(builder, caps.MaxVertexAttribs, "Vertex attributes",
"GL_MAX_VERTEX_ATTRIBS");
EvaluateVertexAttribLimit(builder, caps.MaxVertexAttribs, "Vertex attributes", "GL_MAX_VERTEX_ATTRIBS");
if (caps.SupportsPolygonMode) {
builder.Pass("Polygon mode",
"glPolygonMode GL_LINE/GL_POINT available via GL_NV/ANGLE_polygon_mode");
builder.Pass("Polygon mode", "glPolygonMode GL_LINE/GL_POINT available via GL_NV/ANGLE_polygon_mode");
} else {
builder.FailOptional("Polygon mode",
"no GL_NV/ANGLE_polygon_mode; glPolygonMode GL_LINE/GL_POINT silently "
@@ -397,8 +392,7 @@ namespace MobileGL::MG_Util::SelfTest {
if (es31) {
GLint maxVertexSsboBlocks = 0;
glesFuncs.glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS, &maxVertexSsboBlocks);
while (glesFuncs.glGetError && glesFuncs.glGetError() != GL_NO_ERROR) {
}
while (glesFuncs.glGetError && glesFuncs.glGetError() != GL_NO_ERROR) {}
if (maxVertexSsboBlocks >= 1) {
builder.Pass("Vertex shader storage blocks",
format("GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS = {}", maxVertexSsboBlocks));
@@ -520,28 +514,25 @@ namespace MobileGL::MG_Util::SelfTest {
}
}
if (caps.SupportsTextureBorderClamp) {
builder.Pass("Texture border clamp",
"supported (GL_TEXTURE_BORDER_COLOR reaches the driver, so "
"GL_CLAMP_TO_BORDER samples the colour the application set)");
builder.Pass("Texture border clamp", "supported (GL_TEXTURE_BORDER_COLOR reaches the driver, so "
"GL_CLAMP_TO_BORDER samples the colour the application set)");
} else {
builder.FailOptional(
"Texture border clamp",
"not supported (pre-ES 3.2 without GL_EXT/OES_texture_border_clamp); "
"GL_TEXTURE_BORDER_COLOR is not synced to the driver at all, so anything "
"sampling outside a GL_CLAMP_TO_BORDER texture reads the driver's default "
"border instead of the requested colour, and no wrap mode substitutes for it");
builder.FailOptional("Texture border clamp",
"not supported (pre-ES 3.2 without GL_EXT/OES_texture_border_clamp); "
"GL_TEXTURE_BORDER_COLOR is not synced to the driver at all, so anything "
"sampling outside a GL_CLAMP_TO_BORDER texture reads the driver's default "
"border instead of the requested colour, and no wrap mode substitutes for it");
}
if (caps.SupportsTextureCubeMapArray) {
builder.Pass("Texture cube map array",
"supported (GL_TEXTURE_CUBE_MAP_ARRAY textures get real storage and can be "
"attached to a framebuffer)");
} else {
builder.FailOptional(
"Texture cube map array",
"not supported (pre-ES 3.2 without GL_EXT/OES_texture_cube_map_array); a cube "
"map array texture gets no driver storage at all, so sampling one reads nothing "
"and rendering to one does not reach the screen. Nothing substitutes: the "
"shaders that declare a samplerCubeArray do not compile either");
builder.FailOptional("Texture cube map array",
"not supported (pre-ES 3.2 without GL_EXT/OES_texture_cube_map_array); a cube "
"map array texture gets no driver storage at all, so sampling one reads nothing "
"and rendering to one does not reach the screen. Nothing substitutes: the "
"shaders that declare a samplerCubeArray do not compile either");
}
// FAIL, and specifically FailOptional. The consequence is severe - buffer textures
// are CORE in OpenGL 3.1 and MobileGL advertises a 4.x context, so an application
@@ -569,11 +560,10 @@ namespace MobileGL::MG_Util::SelfTest {
: "MobileGL's floor - this driver claims buffer textures but rejected the query";
switch (caps.TextureBufferSupport) {
case Tier::CoreEs32:
builder.Pass("Buffer textures",
format("core in ES 3.2; GL_MAX_TEXTURE_BUFFER_SIZE = {} is {}, and "
"ESSL 320 needs no #extension directive to declare a "
"samplerBuffer",
advertisedLimit, limitProvenance));
builder.Pass("Buffer textures", format("core in ES 3.2; GL_MAX_TEXTURE_BUFFER_SIZE = {} is {}, and "
"ESSL 320 needs no #extension directive to declare a "
"samplerBuffer",
advertisedLimit, limitProvenance));
break;
case Tier::ExtensionEXT:
builder.Pass("Buffer textures",
@@ -592,17 +582,16 @@ namespace MobileGL::MG_Util::SelfTest {
break;
case Tier::None:
default:
builder.FailOptional(
"Buffer textures",
format("not supported (pre-ES 3.2 without GL_EXT/OES_texture_buffer); "
"glTexBuffer does not exist, so a buffer texture gets no storage, "
"and any shader declaring a samplerBuffer fails to compile and "
"leaves its program unlinked - every draw using it is a silent "
"no-op. MobileGL still reports GL_MAX_TEXTURE_BUFFER_SIZE = {}: "
"the value is a floor it cannot honour, kept because an OpenGL "
"4.x context may not answer 0 and GL has no way to say that a "
"core feature is missing",
advertisedLimit));
builder.FailOptional("Buffer textures",
format("not supported (pre-ES 3.2 without GL_EXT/OES_texture_buffer); "
"glTexBuffer does not exist, so a buffer texture gets no storage, "
"and any shader declaring a samplerBuffer fails to compile and "
"leaves its program unlinked - every draw using it is a silent "
"no-op. MobileGL still reports GL_MAX_TEXTURE_BUFFER_SIZE = {}: "
"the value is a floor it cannot honour, kept because an OpenGL "
"4.x context may not answer 0 and GL has no way to say that a "
"core feature is missing",
advertisedLimit));
break;
}
}
@@ -752,8 +741,7 @@ namespace MobileGL::MG_Util::SelfTest {
}
// Drain stale errors so probe failures are attributable to the probe itself.
while (glesFuncs.glGetError() != GL_NO_ERROR) {
}
while (glesFuncs.glGetError() != GL_NO_ERROR) {}
GLuint queryId = 0;
glesFuncs.glGenQueries(1, &queryId);
@@ -802,9 +790,9 @@ namespace MobileGL::MG_Util::SelfTest {
fail(format("GL error 0x{:x} while reading GL_QUERY_RESULT", resultError));
return;
}
builder.Pass("Timer queries",
extensionPresent + format("; timer query functional (probe observed {} ns)", elapsedNs) +
disabledNote);
builder.Pass("Timer queries", extensionPresent +
format("; timer query functional (probe observed {} ns)", elapsedNs) +
disabledNote);
}
// Compiles + links a two-stage program on the probe context. Returns 0 on failure and writes a
@@ -825,16 +813,19 @@ namespace MobileGL::MG_Util::SelfTest {
GLchar log[512] = {};
GLsizei len = 0;
g.glGetShaderInfoLog(out, static_cast<GLsizei>(sizeof(log) - 1), &len, log);
detail = format("{} shader compile failed: {}",
stage == GL_VERTEX_SHADER ? "vertex" : "fragment",
detail = format("{} shader compile failed: {}", stage == GL_VERTEX_SHADER ? "vertex" : "fragment",
len > 0 ? log : "(no info log)");
return false;
}
return true;
};
GLuint v = 0, f = 0;
const ScopeGuard delV([&]() { if (v) g.glDeleteShader(v); });
const ScopeGuard delF([&]() { if (f) g.glDeleteShader(f); });
const ScopeGuard delV([&]() {
if (v) g.glDeleteShader(v);
});
const ScopeGuard delF([&]() {
if (f) g.glDeleteShader(f);
});
if (!compile(GL_VERTEX_SHADER, vs, v) || !compile(GL_FRAGMENT_SHADER, fs, f)) {
return 0;
}
@@ -884,13 +875,12 @@ namespace MobileGL::MG_Util::SelfTest {
if (!g.glCreateShader || !g.glShaderSource || !g.glCompileShader || !g.glGetShaderiv ||
!g.glGetShaderInfoLog || !g.glDeleteShader || !g.glCreateProgram || !g.glAttachShader ||
!g.glLinkProgram || !g.glGetProgramiv || !g.glUseProgram || !g.glDeleteProgram ||
!g.glGenFramebuffers || !g.glBindFramebuffer || !g.glDeleteFramebuffers ||
!g.glGenRenderbuffers || !g.glBindRenderbuffer || !g.glRenderbufferStorage ||
!g.glFramebufferRenderbuffer || !g.glDeleteRenderbuffers || !g.glCheckFramebufferStatus ||
!g.glGenBuffers || !g.glBindBuffer || !g.glBufferData || !g.glDeleteBuffers ||
!g.glGetAttribLocation || !g.glVertexAttribPointer || !g.glEnableVertexAttribArray ||
!g.glViewport || !g.glClearColor || !g.glClear || !g.glDrawArrays || !g.glReadPixels ||
!g.glFinish || !g.glGetError) {
!g.glGenFramebuffers || !g.glBindFramebuffer || !g.glDeleteFramebuffers || !g.glGenRenderbuffers ||
!g.glBindRenderbuffer || !g.glRenderbufferStorage || !g.glFramebufferRenderbuffer ||
!g.glDeleteRenderbuffers || !g.glCheckFramebufferStatus || !g.glGenBuffers || !g.glBindBuffer ||
!g.glBufferData || !g.glDeleteBuffers || !g.glGetAttribLocation || !g.glVertexAttribPointer ||
!g.glEnableVertexAttribArray || !g.glViewport || !g.glClearColor || !g.glClear || !g.glDrawArrays ||
!g.glReadPixels || !g.glFinish || !g.glGetError) {
fail("the render entry points did not resolve through eglGetProcAddress");
return;
}
@@ -901,43 +891,39 @@ namespace MobileGL::MG_Util::SelfTest {
// same header for consistency.
const Int esslVer = caps.GLESVersion.Major * 100 + caps.GLESVersion.Minor * 10;
const String header = format("#version {} es\n", esslVer >= 300 ? esslVer : 300);
static const char* const kVsNativeBody =
"#extension GL_NV_shader_noperspective_interpolation : require\n"
"in vec4 a_pos;\n"
"in float a_v;\n"
"noperspective out highp float v_out;\n"
"void main() { gl_Position = a_pos; v_out = a_v; }\n";
static const char* const kFsNativeBody =
"#extension GL_NV_shader_noperspective_interpolation : require\n"
"precision highp float;\n"
"noperspective in highp float v_out;\n"
"out vec4 fragColor;\n"
"void main() { fragColor = vec4(v_out, 0.0, 0.0, 1.0); }\n";
static const char* const kVsNativeBody = "#extension GL_NV_shader_noperspective_interpolation : require\n"
"in vec4 a_pos;\n"
"in float a_v;\n"
"noperspective out highp float v_out;\n"
"void main() { gl_Position = a_pos; v_out = a_v; }\n";
static const char* const kFsNativeBody = "#extension GL_NV_shader_noperspective_interpolation : require\n"
"precision highp float;\n"
"noperspective in highp float v_out;\n"
"out vec4 fragColor;\n"
"void main() { fragColor = vec4(v_out, 0.0, 0.0, 1.0); }\n";
// Exactly MobileGL's emulation (verified against EmulateNoPerspectivePass output): pre-multiply
// the varying by clip-w in the vertex stage, recover with gl_FragCoord.w in the fragment stage,
// no noperspective qualifier (so the driver interpolates it perspective-correct).
static const char* const kVsEmuBody =
"in vec4 a_pos;\n"
"in float a_v;\n"
"out highp float v_out;\n"
"void main() { gl_Position = a_pos; v_out = a_v * gl_Position.w; }\n";
static const char* const kVsEmuBody = "in vec4 a_pos;\n"
"in float a_v;\n"
"out highp float v_out;\n"
"void main() { gl_Position = a_pos; v_out = a_v * gl_Position.w; }\n";
static const char* const kFsEmuBody =
"precision highp float;\n"
"in highp float v_out;\n"
"out vec4 fragColor;\n"
"void main() { fragColor = vec4(v_out * gl_FragCoord.w, 0.0, 0.0, 1.0); }\n";
while (g.glGetError() != GL_NO_ERROR) {
}
while (g.glGetError() != GL_NO_ERROR) {}
const String vsSrc = header + (native ? kVsNativeBody : kVsEmuBody);
const String fsSrc = header + (native ? kFsNativeBody : kFsEmuBody);
String linkDetail;
const GLuint prog = CompileLinkProgram(g, vsSrc.c_str(), fsSrc.c_str(), linkDetail);
if (prog == 0) {
fail(native ? "a noperspective program failed to build though the extension is advertised: " +
linkDetail
: "the emulation program failed to build: " + linkDetail);
fail(native
? "a noperspective program failed to build though the extension is advertised: " + linkDetail
: "the emulation program failed to build: " + linkDetail);
return;
}
const ScopeGuard delProg([&]() { g.glDeleteProgram(prog); });
@@ -946,7 +932,9 @@ namespace MobileGL::MG_Util::SelfTest {
constexpr GLsizei kDim = 9;
GLuint rbo = 0, fbo = 0, vbo = 0;
g.glGenRenderbuffers(1, &rbo);
const ScopeGuard delRbo([&]() { if (rbo) g.glDeleteRenderbuffers(1, &rbo); });
const ScopeGuard delRbo([&]() {
if (rbo) g.glDeleteRenderbuffers(1, &rbo);
});
g.glBindRenderbuffer(GL_RENDERBUFFER, rbo);
g.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kDim, kDim);
g.glGenFramebuffers(1, &fbo);
@@ -972,7 +960,9 @@ namespace MobileGL::MG_Util::SelfTest {
8.f, 8.f, 0.f, 8.f, 1.f, //
};
g.glGenBuffers(1, &vbo);
const ScopeGuard delVbo([&]() { if (vbo) g.glDeleteBuffers(1, &vbo); });
const ScopeGuard delVbo([&]() {
if (vbo) g.glDeleteBuffers(1, &vbo);
});
g.glBindBuffer(GL_ARRAY_BUFFER, vbo);
g.glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
@@ -1022,15 +1012,14 @@ namespace MobileGL::MG_Util::SelfTest {
return;
}
if (native) {
builder.Pass("noperspective interpolation",
pathNote + format("; native noperspective renders screen-linear (centre {} ~= 0.5)",
observedByte));
builder.Pass(
"noperspective interpolation",
pathNote + format("; native noperspective renders screen-linear (centre {} ~= 0.5)", observedByte));
} else {
builder.Warn("noperspective interpolation",
pathNote +
format("; the emulation renders screen-linear correctly (centre {} ~= 0.5), "
"but this is the fallback path with less driver coverage",
observedByte));
pathNote + format("; the emulation renders screen-linear correctly (centre {} ~= 0.5), "
"but this is the fallback path with less driver coverage",
observedByte));
}
}
@@ -1221,9 +1210,18 @@ namespace MobileGL::MG_Util::SelfTest {
}
stageDone("eglBindAPI(EGL_OPENGL_ES_API) succeeded");
const EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
const EGLint configAttribs[] = {EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES3_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_NONE};
EGLConfig config = nullptr;
EGLint numConfigs = 0;
@@ -1281,8 +1279,8 @@ namespace MobileGL::MG_Util::SelfTest {
caps.SubgroupSupportedFeatures & MG_Backend::DirectGLES::kEsslPrintableSubgroupFeatures)
: String());
builder.report.formatCapabilities.emplace();
MG_Backend::DirectGLES::PopulateFormatCapabilities(
glesFuncs, caps, builder.report.formatCapabilities.value());
MG_Backend::DirectGLES::PopulateFormatCapabilities(glesFuncs, caps,
builder.report.formatCapabilities.value());
ReportThreeChannelColorAttachments(builder, caps, builder.report.formatCapabilities.value());
// The "Known Driver Bugs" section. Deliberately last, and deliberately not a
// builder.Pass/Warn/Fail row: these are not capability checks and they must not move
@@ -1372,8 +1370,7 @@ namespace MobileGL::MG_Util::SelfTest {
}
String VkApiVersionToString(Uint32 version) {
return format("{}.{}.{}", VK_VERSION_MAJOR(version), VK_VERSION_MINOR(version),
VK_VERSION_PATCH(version));
return format("{}.{}.{}", VK_VERSION_MAJOR(version), VK_VERSION_MINOR(version), VK_VERSION_PATCH(version));
}
// Real timestamp-query probe, emitting the backend's single "Timer queries" row:
@@ -1384,8 +1381,7 @@ namespace MobileGL::MG_Util::SelfTest {
// down from a scope guard before the caller's instance guard runs.
void ProbeVulkanTimerQuery(ReportBuilder& builder, PFN_vkGetInstanceProcAddr getInstanceProcAddr,
VkInstance instance, VkPhysicalDevice physicalDevice,
Uint32 graphicsQueueFamilyIndex, Uint32 timestampValidBits,
Float timestampPeriod) {
Uint32 graphicsQueueFamilyIndex, Uint32 timestampValidBits, Float timestampPeriod) {
const String disabledNote = TimerQueryDisabledNote();
const String timestampFacts =
format("timestampValidBits = {} on the graphics queue family; timestampPeriod = {} ns per tick",
@@ -1427,19 +1423,18 @@ namespace MobileGL::MG_Util::SelfTest {
reinterpret_cast<PFN_vkWaitForFences>(getInstanceProcAddr(instance, "vkWaitForFences"));
const auto vkQueueSubmitFn =
reinterpret_cast<PFN_vkQueueSubmit>(getInstanceProcAddr(instance, "vkQueueSubmit"));
const auto vkGetQueryPoolResultsFn = reinterpret_cast<PFN_vkGetQueryPoolResults>(
getInstanceProcAddr(instance, "vkGetQueryPoolResults"));
const auto vkGetQueryPoolResultsFn =
reinterpret_cast<PFN_vkGetQueryPoolResults>(getInstanceProcAddr(instance, "vkGetQueryPoolResults"));
const auto vkDeviceWaitIdleFn =
reinterpret_cast<PFN_vkDeviceWaitIdle>(getInstanceProcAddr(instance, "vkDeviceWaitIdle"));
if (vkCreateDeviceFn == nullptr || vkDestroyDeviceFn == nullptr || vkGetDeviceQueueFn == nullptr ||
vkCreateCommandPoolFn == nullptr || vkDestroyCommandPoolFn == nullptr ||
vkAllocateCommandBuffersFn == nullptr || vkBeginCommandBufferFn == nullptr ||
vkEndCommandBufferFn == nullptr || vkCreateQueryPoolFn == nullptr ||
vkDestroyQueryPoolFn == nullptr || vkCmdResetQueryPoolFn == nullptr ||
vkCmdWriteTimestampFn == nullptr || vkCreateFenceFn == nullptr || vkDestroyFenceFn == nullptr ||
vkWaitForFencesFn == nullptr || vkQueueSubmitFn == nullptr || vkGetQueryPoolResultsFn == nullptr ||
vkDeviceWaitIdleFn == nullptr) {
vkEndCommandBufferFn == nullptr || vkCreateQueryPoolFn == nullptr || vkDestroyQueryPoolFn == nullptr ||
vkCmdResetQueryPoolFn == nullptr || vkCmdWriteTimestampFn == nullptr || vkCreateFenceFn == nullptr ||
vkDestroyFenceFn == nullptr || vkWaitForFencesFn == nullptr || vkQueueSubmitFn == nullptr ||
vkGetQueryPoolResultsFn == nullptr || vkDeviceWaitIdleFn == nullptr) {
fail("vkGetInstanceProcAddr could not resolve the entry points required for the "
"timestamp probe");
return;
@@ -1573,21 +1568,19 @@ namespace MobileGL::MG_Util::SelfTest {
// Skip the teardown idle wait too (see the scope guard): the
// submission is still pending on a possibly-hung GPU.
fenceWaitTimedOut = true;
fail(format("vkWaitForFences did not signal within 5 s (VkResult = {})",
static_cast<Int>(result)));
fail(format("vkWaitForFences did not signal within 5 s (VkResult = {})", static_cast<Int>(result)));
return;
}
Uint64 timestamps[2] = {0, 0};
result = vkGetQueryPoolResultsFn(device, queryPool, 0, 2, sizeof(timestamps), timestamps,
sizeof(Uint64), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
result = vkGetQueryPoolResultsFn(device, queryPool, 0, 2, sizeof(timestamps), timestamps, sizeof(Uint64),
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
if (result != VK_SUCCESS) {
fail(format("vkGetQueryPoolResults failed (VkResult = {})", static_cast<Int>(result)));
return;
}
const Uint64 validMask =
timestampValidBits >= 64 ? ~0ull : ((1ull << timestampValidBits) - 1ull);
const Uint64 validMask = timestampValidBits >= 64 ? ~0ull : ((1ull << timestampValidBits) - 1ull);
const Uint64 t0 = timestamps[0] & validMask;
const Uint64 t1 = timestamps[1] & validMask;
if (t1 < t0) {
@@ -1618,8 +1611,7 @@ namespace MobileGL::MG_Util::SelfTest {
// Throwaway device on purpose, like every probe here: the row reports the
// driver, not the renderer's configuration - MOBILEGL_MAGMA_PRIMGEN_QUERY_REROUTE
// steers the renderer, never this row.
void ProbeVulkanPrimitivesGeneratedNoXfb(ReportBuilder& builder,
PFN_vkGetInstanceProcAddr getInstanceProcAddr,
void ProbeVulkanPrimitivesGeneratedNoXfb(ReportBuilder& builder, PFN_vkGetInstanceProcAddr getInstanceProcAddr,
VkInstance instance, VkPhysicalDevice physicalDevice,
Uint32 graphicsQueueFamilyIndex,
const Vector<VkExtensionProperties>& deviceExtensions,
@@ -1690,8 +1682,7 @@ namespace MobileGL::MG_Util::SelfTest {
Bool primitivesGeneratedQueryUsable = false;
VkPhysicalDevicePrimitivesGeneratedQueryFeaturesEXT enabledPgqFeatures{};
enabledPgqFeatures.sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVES_GENERATED_QUERY_FEATURES_EXT;
enabledPgqFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVES_GENERATED_QUERY_FEATURES_EXT;
if (HasVkExtension(deviceExtensions, VK_EXT_PRIMITIVES_GENERATED_QUERY_EXTENSION_NAME)) {
VkPhysicalDevicePrimitivesGeneratedQueryFeaturesEXT pgqQuery{};
pgqQuery.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVES_GENERATED_QUERY_FEATURES_EXT;
@@ -1705,16 +1696,14 @@ namespace MobileGL::MG_Util::SelfTest {
enabledPgqFeatures.primitivesGeneratedQuery = VK_TRUE;
enabledPgqFeatures.primitivesGeneratedQueryWithRasterizerDiscard = VK_TRUE;
enabledPgqFeatures.pNext = &enabledXfbFeatures;
enabledExtensions[enabledExtensionCount++] =
VK_EXT_PRIMITIVES_GENERATED_QUERY_EXTENSION_NAME;
enabledExtensions[enabledExtensionCount++] = VK_EXT_PRIMITIVES_GENERATED_QUERY_EXTENSION_NAME;
}
}
VkDeviceCreateInfo deviceInfo{};
deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceInfo.pNext = primitivesGeneratedQueryUsable
? static_cast<const void*>(&enabledPgqFeatures)
: static_cast<const void*>(&enabledXfbFeatures);
deviceInfo.pNext = primitivesGeneratedQueryUsable ? static_cast<const void*>(&enabledPgqFeatures)
: static_cast<const void*>(&enabledXfbFeatures);
deviceInfo.queueCreateInfoCount = 1;
deviceInfo.pQueueCreateInfos = &queueInfo;
deviceInfo.enabledExtensionCount = enabledExtensionCount;
@@ -1761,12 +1750,10 @@ namespace MobileGL::MG_Util::SelfTest {
auto& fns = probeContext.fns;
const auto resolve = [&](const char* name) { return getInstanceProcAddr(instance, name); };
fns.vkCreateCommandPool = reinterpret_cast<PFN_vkCreateCommandPool>(resolve("vkCreateCommandPool"));
fns.vkDestroyCommandPool =
reinterpret_cast<PFN_vkDestroyCommandPool>(resolve("vkDestroyCommandPool"));
fns.vkDestroyCommandPool = reinterpret_cast<PFN_vkDestroyCommandPool>(resolve("vkDestroyCommandPool"));
fns.vkAllocateCommandBuffers =
reinterpret_cast<PFN_vkAllocateCommandBuffers>(resolve("vkAllocateCommandBuffers"));
fns.vkBeginCommandBuffer =
reinterpret_cast<PFN_vkBeginCommandBuffer>(resolve("vkBeginCommandBuffer"));
fns.vkBeginCommandBuffer = reinterpret_cast<PFN_vkBeginCommandBuffer>(resolve("vkBeginCommandBuffer"));
fns.vkEndCommandBuffer = reinterpret_cast<PFN_vkEndCommandBuffer>(resolve("vkEndCommandBuffer"));
fns.vkCreateQueryPool = reinterpret_cast<PFN_vkCreateQueryPool>(resolve("vkCreateQueryPool"));
fns.vkDestroyQueryPool = reinterpret_cast<PFN_vkDestroyQueryPool>(resolve("vkDestroyQueryPool"));
@@ -1778,19 +1765,13 @@ namespace MobileGL::MG_Util::SelfTest {
fns.vkCmdEndQueryIndexedEXT =
reinterpret_cast<PFN_vkCmdEndQueryIndexedEXT>(resolve("vkCmdEndQueryIndexedEXT"));
fns.vkCreateRenderPass = reinterpret_cast<PFN_vkCreateRenderPass>(resolve("vkCreateRenderPass"));
fns.vkDestroyRenderPass =
reinterpret_cast<PFN_vkDestroyRenderPass>(resolve("vkDestroyRenderPass"));
fns.vkCreateFramebuffer =
reinterpret_cast<PFN_vkCreateFramebuffer>(resolve("vkCreateFramebuffer"));
fns.vkDestroyFramebuffer =
reinterpret_cast<PFN_vkDestroyFramebuffer>(resolve("vkDestroyFramebuffer"));
fns.vkCmdBeginRenderPass =
reinterpret_cast<PFN_vkCmdBeginRenderPass>(resolve("vkCmdBeginRenderPass"));
fns.vkDestroyRenderPass = reinterpret_cast<PFN_vkDestroyRenderPass>(resolve("vkDestroyRenderPass"));
fns.vkCreateFramebuffer = reinterpret_cast<PFN_vkCreateFramebuffer>(resolve("vkCreateFramebuffer"));
fns.vkDestroyFramebuffer = reinterpret_cast<PFN_vkDestroyFramebuffer>(resolve("vkDestroyFramebuffer"));
fns.vkCmdBeginRenderPass = reinterpret_cast<PFN_vkCmdBeginRenderPass>(resolve("vkCmdBeginRenderPass"));
fns.vkCmdEndRenderPass = reinterpret_cast<PFN_vkCmdEndRenderPass>(resolve("vkCmdEndRenderPass"));
fns.vkCreateShaderModule =
reinterpret_cast<PFN_vkCreateShaderModule>(resolve("vkCreateShaderModule"));
fns.vkDestroyShaderModule =
reinterpret_cast<PFN_vkDestroyShaderModule>(resolve("vkDestroyShaderModule"));
fns.vkCreateShaderModule = reinterpret_cast<PFN_vkCreateShaderModule>(resolve("vkCreateShaderModule"));
fns.vkDestroyShaderModule = reinterpret_cast<PFN_vkDestroyShaderModule>(resolve("vkDestroyShaderModule"));
fns.vkCreatePipelineLayout =
reinterpret_cast<PFN_vkCreatePipelineLayout>(resolve("vkCreatePipelineLayout"));
fns.vkDestroyPipelineLayout =
@@ -1804,12 +1785,10 @@ namespace MobileGL::MG_Util::SelfTest {
fns.vkDestroyFence = reinterpret_cast<PFN_vkDestroyFence>(resolve("vkDestroyFence"));
fns.vkQueueSubmit = reinterpret_cast<PFN_vkQueueSubmit>(resolve("vkQueueSubmit"));
fns.vkWaitForFences = reinterpret_cast<PFN_vkWaitForFences>(resolve("vkWaitForFences"));
fns.vkGetQueryPoolResults =
reinterpret_cast<PFN_vkGetQueryPoolResults>(resolve("vkGetQueryPoolResults"));
fns.vkGetQueryPoolResults = reinterpret_cast<PFN_vkGetQueryPoolResults>(resolve("vkGetQueryPoolResults"));
fns.vkDeviceWaitIdle = reinterpret_cast<PFN_vkDeviceWaitIdle>(resolve("vkDeviceWaitIdle"));
const PrimitivesGeneratedNoXfbMeasurement measurement =
RunPrimitivesGeneratedNoXfbProbe(probeContext);
const PrimitivesGeneratedNoXfbMeasurement measurement = RunPrimitivesGeneratedNoXfbProbe(probeContext);
// Before any return below: the guard above owns the device and must know.
probeFenceWaitTimedOut = measurement.fenceWaitTimedOut;
if (!measurement.ran) {
@@ -1819,8 +1798,7 @@ namespace MobileGL::MG_Util::SelfTest {
return;
}
const auto shapeFacts = [](const char* name,
const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
const auto shapeFacts = [](const char* name, const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
if (!shape.drawn) {
return format("{} not drawn (no tessellationShader)", name);
}
@@ -1839,8 +1817,8 @@ namespace MobileGL::MG_Util::SelfTest {
return facts;
};
const String facts = shapeFacts("triangles", measurement.trianglesPlain) + "; " +
shapeFacts("triangles under discard", measurement.trianglesDiscard) +
"; " + shapeFacts("patches under discard", measurement.patchesDiscard);
shapeFacts("triangles under discard", measurement.trianglesDiscard) + "; " +
shapeFacts("patches under discard", measurement.patchesDiscard);
const auto statisticsExactOn = [](const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
return shape.statisticsMeasured && shape.statisticsClippingInput == shape.expectedPrimitives;
@@ -1853,8 +1831,7 @@ namespace MobileGL::MG_Util::SelfTest {
// string printed right after it.
const auto describeMissedStatisticsShapes = [&]() {
String missed;
const auto note = [&](const char* name,
const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
const auto note = [&](const char* name, const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
if (!shape.drawn || statisticsExactOn(shape)) {
return;
}
@@ -1862,10 +1839,9 @@ namespace MobileGL::MG_Util::SelfTest {
missed += " and ";
}
missed += name;
missed += shape.statisticsMeasured
? format(" (read {} of {} expected)", shape.statisticsClippingInput,
shape.expectedPrimitives)
: String(" (its statistics slot did not read back)");
missed += shape.statisticsMeasured ? format(" (read {} of {} expected)",
shape.statisticsClippingInput, shape.expectedPrimitives)
: String(" (its statistics slot did not read back)");
};
note("the plain draw", measurement.trianglesPlain);
note("triangles under rasterizer discard", measurement.trianglesDiscard);
@@ -1890,21 +1866,19 @@ namespace MobileGL::MG_Util::SelfTest {
switch (EvaluatePrimitivesGeneratedNoXfbVerdict(measurement)) {
case PrimitivesGeneratedNoXfbVerdict::StreamCounts:
builder.Pass(RowName,
"the stream query counts a draw made with no capture span open, as "
"VK_EXT_transform_feedback defines (" +
facts + ")");
builder.Pass(RowName, "the stream query counts a draw made with no capture span open, as "
"VK_EXT_transform_feedback defines (" +
facts + ")");
return;
case PrimitivesGeneratedNoXfbVerdict::PrimitivesGeneratedExtSubstitute:
builder.Warn(RowName,
"the stream query answers 0 for a draw made with no capture span open - "
"the shape the CTS measures the tessellator with - while a "
"VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT query around an identical replay answers "
"exactly right, rasterizer discard included, so the renderer "
"accumulates GL_PRIMITIVES_GENERATED for such draws through the "
"dedicated query instead (one extra query slot per XFB-inactive draw "
"inside a GENERATED span; " +
facts + ")");
builder.Warn(RowName, "the stream query answers 0 for a draw made with no capture span open - "
"the shape the CTS measures the tessellator with - while a "
"VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT query around an identical replay answers "
"exactly right, rasterizer discard included, so the renderer "
"accumulates GL_PRIMITIVES_GENERATED for such draws through the "
"dedicated query instead (one extra query slot per XFB-inactive draw "
"inside a GENERATED span; " +
facts + ")");
return;
case PrimitivesGeneratedNoXfbVerdict::StatisticsSubstitute:
builder.Warn(RowName,
@@ -1936,8 +1910,7 @@ namespace MobileGL::MG_Util::SelfTest {
String downgradeShapes;
const auto noteDowngrade = [&](const char* name,
const PrimitivesGeneratedNoXfbShapeMeasurement& shape) {
if (!shape.drawn || statisticsExactOn(shape) ||
shape.streamGenerated != shape.expectedPrimitives) {
if (!shape.drawn || statisticsExactOn(shape) || shape.streamGenerated != shape.expectedPrimitives) {
return;
}
if (!downgradeShapes.empty()) {
@@ -1979,8 +1952,7 @@ namespace MobileGL::MG_Util::SelfTest {
// driver does, not what MobileGL elects to advertise to applications.
void ProbeVulkanIterationRPWitness(ReportBuilder& builder, PFN_vkGetInstanceProcAddr getInstanceProcAddr,
VkInstance instance, VkPhysicalDevice physicalDevice,
Uint32 computeQueueFamilyIndex,
const VkPhysicalDeviceProperties& properties,
Uint32 computeQueueFamilyIndex, const VkPhysicalDeviceProperties& properties,
Bool subgroupPropertiesAvailable,
const VkPhysicalDeviceSubgroupProperties& subgroupProperties) {
constexpr const char* RowName = "Subgroup first-reduction witness";
@@ -1998,8 +1970,7 @@ namespace MobileGL::MG_Util::SelfTest {
}
IterationRPWitnessLimits limits{};
limits.computeStageSupported =
(subgroupProperties.supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) != 0;
limits.computeStageSupported = (subgroupProperties.supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) != 0;
limits.basicSubgroupSupported =
(subgroupProperties.supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) != 0;
limits.arithmeticSubgroupSupported =
@@ -2007,8 +1978,8 @@ namespace MobileGL::MG_Util::SelfTest {
limits.subgroupSize = subgroupProperties.subgroupSize;
limits.maxComputeWorkGroupInvocations = properties.limits.maxComputeWorkGroupInvocations;
limits.maxComputeWorkGroupSize = {properties.limits.maxComputeWorkGroupSize[0],
properties.limits.maxComputeWorkGroupSize[1],
properties.limits.maxComputeWorkGroupSize[2]};
properties.limits.maxComputeWorkGroupSize[1],
properties.limits.maxComputeWorkGroupSize[2]};
limits.maxComputeSharedMemorySize = properties.limits.maxComputeSharedMemorySize;
limits.maxPerStageDescriptorStorageBuffers = properties.limits.maxPerStageDescriptorStorageBuffers;
limits.maxDescriptorSetStorageBuffers = properties.limits.maxDescriptorSetStorageBuffers;
@@ -2020,10 +1991,9 @@ namespace MobileGL::MG_Util::SelfTest {
// WARN, not FAIL: there is nothing to witness on a device with no native
// subgroup contract, and the renderer takes its non-subgroup iteration path,
// which produces the same image.
builder.Warn(RowName,
eligibility.detail +
"; the renderer takes its non-subgroup iteration path instead, which "
"renders the same thing without the first-reduction shortcut");
builder.Warn(RowName, eligibility.detail +
"; the renderer takes its non-subgroup iteration path instead, which "
"renders the same thing without the first-reduction shortcut");
return;
}
if (eligibility.eligibility == IterationRPWitnessEligibility::FailInadequateLimits) {
@@ -2056,8 +2026,7 @@ namespace MobileGL::MG_Util::SelfTest {
reinterpret_cast<PFN_vkFreeMemory>(getInstanceProcAddr(instance, "vkFreeMemory"));
const auto vkBindBufferMemoryFn =
reinterpret_cast<PFN_vkBindBufferMemory>(getInstanceProcAddr(instance, "vkBindBufferMemory"));
const auto vkMapMemoryFn =
reinterpret_cast<PFN_vkMapMemory>(getInstanceProcAddr(instance, "vkMapMemory"));
const auto vkMapMemoryFn = reinterpret_cast<PFN_vkMapMemory>(getInstanceProcAddr(instance, "vkMapMemory"));
const auto vkUnmapMemoryFn =
reinterpret_cast<PFN_vkUnmapMemory>(getInstanceProcAddr(instance, "vkUnmapMemory"));
const auto vkCreateDescriptorSetLayoutFn = reinterpret_cast<PFN_vkCreateDescriptorSetLayout>(
@@ -2066,8 +2035,8 @@ namespace MobileGL::MG_Util::SelfTest {
getInstanceProcAddr(instance, "vkDestroyDescriptorSetLayout"));
const auto vkCreateDescriptorPoolFn =
reinterpret_cast<PFN_vkCreateDescriptorPool>(getInstanceProcAddr(instance, "vkCreateDescriptorPool"));
const auto vkDestroyDescriptorPoolFn = reinterpret_cast<PFN_vkDestroyDescriptorPool>(
getInstanceProcAddr(instance, "vkDestroyDescriptorPool"));
const auto vkDestroyDescriptorPoolFn =
reinterpret_cast<PFN_vkDestroyDescriptorPool>(getInstanceProcAddr(instance, "vkDestroyDescriptorPool"));
const auto vkAllocateDescriptorSetsFn = reinterpret_cast<PFN_vkAllocateDescriptorSets>(
getInstanceProcAddr(instance, "vkAllocateDescriptorSets"));
const auto vkUpdateDescriptorSetsFn =
@@ -2078,8 +2047,8 @@ namespace MobileGL::MG_Util::SelfTest {
reinterpret_cast<PFN_vkDestroyShaderModule>(getInstanceProcAddr(instance, "vkDestroyShaderModule"));
const auto vkCreatePipelineLayoutFn =
reinterpret_cast<PFN_vkCreatePipelineLayout>(getInstanceProcAddr(instance, "vkCreatePipelineLayout"));
const auto vkDestroyPipelineLayoutFn = reinterpret_cast<PFN_vkDestroyPipelineLayout>(
getInstanceProcAddr(instance, "vkDestroyPipelineLayout"));
const auto vkDestroyPipelineLayoutFn =
reinterpret_cast<PFN_vkDestroyPipelineLayout>(getInstanceProcAddr(instance, "vkDestroyPipelineLayout"));
const auto vkCreateComputePipelinesFn = reinterpret_cast<PFN_vkCreateComputePipelines>(
getInstanceProcAddr(instance, "vkCreateComputePipelines"));
const auto vkDestroyPipelineFn =
@@ -2096,8 +2065,8 @@ namespace MobileGL::MG_Util::SelfTest {
reinterpret_cast<PFN_vkEndCommandBuffer>(getInstanceProcAddr(instance, "vkEndCommandBuffer"));
const auto vkCmdBindPipelineFn =
reinterpret_cast<PFN_vkCmdBindPipeline>(getInstanceProcAddr(instance, "vkCmdBindPipeline"));
const auto vkCmdBindDescriptorSetsFn = reinterpret_cast<PFN_vkCmdBindDescriptorSets>(
getInstanceProcAddr(instance, "vkCmdBindDescriptorSets"));
const auto vkCmdBindDescriptorSetsFn =
reinterpret_cast<PFN_vkCmdBindDescriptorSets>(getInstanceProcAddr(instance, "vkCmdBindDescriptorSets"));
const auto vkCmdDispatchFn =
reinterpret_cast<PFN_vkCmdDispatch>(getInstanceProcAddr(instance, "vkCmdDispatch"));
const auto vkCmdPipelineBarrierFn =
@@ -2123,10 +2092,10 @@ namespace MobileGL::MG_Util::SelfTest {
vkUpdateDescriptorSetsFn == nullptr || vkCreateShaderModuleFn == nullptr ||
vkDestroyShaderModuleFn == nullptr || vkCreatePipelineLayoutFn == nullptr ||
vkDestroyPipelineLayoutFn == nullptr || vkCreateComputePipelinesFn == nullptr ||
vkDestroyPipelineFn == nullptr || vkCreateCommandPoolFn == nullptr || vkDestroyCommandPoolFn == nullptr ||
vkAllocateCommandBuffersFn == nullptr || vkBeginCommandBufferFn == nullptr ||
vkEndCommandBufferFn == nullptr || vkCmdBindPipelineFn == nullptr ||
vkCmdBindDescriptorSetsFn == nullptr || vkCmdDispatchFn == nullptr ||
vkDestroyPipelineFn == nullptr || vkCreateCommandPoolFn == nullptr ||
vkDestroyCommandPoolFn == nullptr || vkAllocateCommandBuffersFn == nullptr ||
vkBeginCommandBufferFn == nullptr || vkEndCommandBufferFn == nullptr ||
vkCmdBindPipelineFn == nullptr || vkCmdBindDescriptorSetsFn == nullptr || vkCmdDispatchFn == nullptr ||
vkCmdPipelineBarrierFn == nullptr || vkCreateFenceFn == nullptr || vkDestroyFenceFn == nullptr ||
vkQueueSubmitFn == nullptr || vkWaitForFencesFn == nullptr || vkDeviceWaitIdleFn == nullptr) {
fail("vkGetInstanceProcAddr could not resolve the Vulkan entry points required for the witness");
@@ -2211,8 +2180,8 @@ namespace MobileGL::MG_Util::SelfTest {
Uint32 memoryTypeIndex = std::numeric_limits<Uint32>::max();
for (Uint32 index = 0; index < memoryProperties.memoryTypeCount; ++index) {
const Bool compatible = (memoryRequirements.memoryTypeBits & (1u << index)) != 0u;
const VkMemoryPropertyFlags required = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
const VkMemoryPropertyFlags required =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
if (compatible && (memoryProperties.memoryTypes[index].propertyFlags & required) == required) {
memoryTypeIndex = index;
break;
@@ -2537,9 +2506,8 @@ namespace MobileGL::MG_Util::SelfTest {
// fallback costs a buffer queue the headless path does not need, and because
// this used to abort the process instead.
if (HasVkExtension(instanceExtensions, VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME)) {
builder.Pass("Headless surface",
format("{} present; windowless contexts get a real headless surface",
VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME));
builder.Pass("Headless surface", format("{} present; windowless contexts get a real headless surface",
VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME));
} else {
builder.Warn("Headless surface",
format("{} absent; a windowless (pbuffer) context falls back to an AImageReader "
@@ -2579,16 +2547,14 @@ namespace MobileGL::MG_Util::SelfTest {
getInstanceProcAddr(instance, "vkGetPhysicalDeviceQueueFamilyProperties"));
const auto vkGetPhysicalDeviceFeaturesFn = reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures>(
getInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures"));
const auto vkEnumerateDeviceExtensionPropertiesFn =
reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
getInstanceProcAddr(instance, "vkEnumerateDeviceExtensionProperties"));
const auto vkEnumerateDeviceExtensionPropertiesFn = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
getInstanceProcAddr(instance, "vkEnumerateDeviceExtensionProperties"));
const auto vkGetPhysicalDeviceFeatures2Fn = reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures2>(
getInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures2"));
const auto vkGetPhysicalDeviceProperties2Fn = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>(
getInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties2"));
const auto vkGetPhysicalDeviceFormatPropertiesFn =
reinterpret_cast<PFN_vkGetPhysicalDeviceFormatProperties>(
getInstanceProcAddr(instance, "vkGetPhysicalDeviceFormatProperties"));
const auto vkGetPhysicalDeviceFormatPropertiesFn = reinterpret_cast<PFN_vkGetPhysicalDeviceFormatProperties>(
getInstanceProcAddr(instance, "vkGetPhysicalDeviceFormatProperties"));
// The instance is destroyed from a scope guard so it is released on every early-return
// path and even if a String/format allocation throws while report rows are being built.
@@ -2611,8 +2577,8 @@ namespace MobileGL::MG_Util::SelfTest {
Uint32 deviceCount = 0;
const VkResult countResult = vkEnumeratePhysicalDevicesFn(instance, &deviceCount, nullptr);
if (countResult != VK_SUCCESS) {
builder.Fail("Graphics device", format("vkEnumeratePhysicalDevices failed (VkResult = {})",
static_cast<Int>(countResult)));
builder.Fail("Graphics device",
format("vkEnumeratePhysicalDevices failed (VkResult = {})", static_cast<Int>(countResult)));
return;
}
if (deviceCount == 0) {
@@ -2679,14 +2645,12 @@ namespace MobileGL::MG_Util::SelfTest {
// The chosen-device facts (name, enumeration count, graphics queue) ride along
// on both outcomes so the device API verdict never hides them.
const String deviceFacts =
format("{} ({} device(s) enumerated, picked the first with a graphics queue); "
"graphics queue family present",
String(properties.deviceName), deviceCount);
const String deviceFacts = format("{} ({} device(s) enumerated, picked the first with a graphics queue); "
"graphics queue family present",
String(properties.deviceName), deviceCount);
if (properties.apiVersion >= VK_API_VERSION_1_1) {
builder.Pass("Graphics device",
deviceFacts +
format("; device API Vulkan {}", VkApiVersionToString(properties.apiVersion)));
deviceFacts + format("; device API Vulkan {}", VkApiVersionToString(properties.apiVersion)));
} else {
builder.Fail("Graphics device",
deviceFacts + format("; but Device API version: Vulkan {} (< 1.1); the DirectVulkan "
@@ -2761,7 +2725,7 @@ namespace MobileGL::MG_Util::SelfTest {
"unsupported; glMultiDraw* batches take the indirect or unrolled tier "
"instead, with identical output");
}
const char* resolvedTier = multiDrawExtUsable ? "ext"
const char* resolvedTier = multiDrawExtUsable ? "ext"
: features.multiDrawIndirect == VK_TRUE ? "indirect"
: "unroll";
String tierDetail = format("default tier \"{}\" (chain: ext -> indirect -> unroll)", resolvedTier);
@@ -2809,9 +2773,8 @@ namespace MobileGL::MG_Util::SelfTest {
if (features.dualSrcBlend == VK_TRUE) {
builder.Pass("dualSrcBlend", "GL_SRC1_* dual-source blend factors supported");
} else {
builder.FailOptional("dualSrcBlend",
"unsupported; a draw using a GL_SRC1_* blend factor hard-fails, and a "
"second fragment output cannot be produced any other way");
builder.FailOptional("dualSrcBlend", "unsupported; a draw using a GL_SRC1_* blend factor hard-fails, and a "
"second fragment output cannot be produced any other way");
}
// The Magma counterpart of the GLES "Buffer textures" row, so the two sections can be
// read side by side. Vulkan has no optional-feature bit here: a uniform texel buffer is
@@ -2841,8 +2804,8 @@ namespace MobileGL::MG_Util::SelfTest {
const Bool sliceCapable =
vkGetPhysicalDeviceImageFormatProperties(
physicalDevice, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TYPE_3D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT,
VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT, &sliceProbe) == VK_SUCCESS;
if (sliceCapable) {
builder.Pass("2D-array-compatible 3D images",
@@ -2946,8 +2909,7 @@ namespace MobileGL::MG_Util::SelfTest {
properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
properties2.pNext = &provokingVertexProperties;
vkGetPhysicalDeviceProperties2Fn(physicalDevice, &properties2);
provokingVertexModePerPipeline =
provokingVertexProperties.provokingVertexModePerPipeline == VK_TRUE;
provokingVertexModePerPipeline = provokingVertexProperties.provokingVertexModePerPipeline == VK_TRUE;
transformFeedbackPreservesTriangleFanProvokingVertex =
provokingVertexProperties.transformFeedbackPreservesTriangleFanProvokingVertex == VK_TRUE;
}
@@ -3047,13 +3009,13 @@ namespace MobileGL::MG_Util::SelfTest {
// Same usability rule as the Vulkan capability loader's
// HasUsableShaderSubgroupSupport, which decides native vs emulated in the real backend.
ReportShaderSubgroups(builder, subgroupUsable,
subgroupUsable ? format("subgroup size {}, stages 0x{:x}, operations 0x{:x}",
subgroupProperties.subgroupSize,
subgroupProperties.supportedStages,
subgroupProperties.supportedOperations)
: String());
summary.shaderSubgroupUsable = MG_Backend::ResolveSubgroupImplementation(subgroupUsable) !=
MG_Backend::SubgroupImplementation::None;
subgroupUsable
? format("subgroup size {}, stages 0x{:x}, operations 0x{:x}",
subgroupProperties.subgroupSize, subgroupProperties.supportedStages,
subgroupProperties.supportedOperations)
: String());
summary.shaderSubgroupUsable =
MG_Backend::ResolveSubgroupImplementation(subgroupUsable) != MG_Backend::SubgroupImplementation::None;
} else {
ReportShaderSubgroups(builder, false, String());
summary.shaderSubgroupUsable =
@@ -3061,7 +3023,7 @@ namespace MobileGL::MG_Util::SelfTest {
}
ProbeVulkanIterationRPWitness(builder, getInstanceProcAddr, instance, physicalDevice, computeQueueFamilyIndex,
properties, subgroupPropertiesAvailable, subgroupProperties);
properties, subgroupPropertiesAvailable, subgroupProperties);
ProbeVulkanPrimitivesGeneratedNoXfb(builder, getInstanceProcAddr, instance, physicalDevice,
graphicsQueueFamilyIndex, deviceExtensions, features,
@@ -3080,9 +3042,8 @@ namespace MobileGL::MG_Util::SelfTest {
}
if (HasVkExtension(deviceExtensions, VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME)) {
builder.Pass("VK_KHR_draw_indirect_count",
"supported (count-buffer indirect draws run as single native "
"vkCmdDraw*IndirectCount commands)");
builder.Pass("VK_KHR_draw_indirect_count", "supported (count-buffer indirect draws run as single native "
"vkCmdDraw*IndirectCount commands)");
} else {
builder.Warn("VK_KHR_draw_indirect_count",
"not supported; count-buffer indirect draws (glMultiDraw*IndirectCount) fall "
@@ -3110,25 +3071,24 @@ namespace MobileGL::MG_Util::SelfTest {
// the graphics queue family and a non-zero tick period.
summary.timerQueriesSupported = graphicsQueueTimestampValidBits > 0 && timestampPeriod > 0.0f;
if (graphicsQueueTimestampValidBits > 0) {
ProbeVulkanTimerQuery(builder, getInstanceProcAddr, instance, physicalDevice,
graphicsQueueFamilyIndex, graphicsQueueTimestampValidBits, timestampPeriod);
ProbeVulkanTimerQuery(builder, getInstanceProcAddr, instance, physicalDevice, graphicsQueueFamilyIndex,
graphicsQueueTimestampValidBits, timestampPeriod);
} else {
builder.FailOptional(
"Timer queries",
format("timestampValidBits = 0 on the graphics queue family; timestampPeriod = {} ns "
"per tick; the graphics queue cannot write a timestamp at all, so there is "
"nothing to time GPU work with and glBeginQuery(GL_TIME_ELAPSED) has no "
"substitute",
timestampPeriod) +
TimerQueryDisabledNote());
builder.FailOptional("Timer queries",
format("timestampValidBits = 0 on the graphics queue family; timestampPeriod = {} ns "
"per tick; the graphics queue cannot write a timestamp at all, so there is "
"nothing to time GPU work with and glBeginQuery(GL_TIME_ELAPSED) has no "
"substitute",
timestampPeriod) +
TimerQueryDisabledNote());
}
if (vkGetPhysicalDeviceFormatPropertiesFn != nullptr) {
MG_External::VulkanCapabilities formatProbeCapabilities{};
BackendLoader::FillInVulkanCapabilities(formatProbeCapabilities, properties);
builder.report.formatCapabilities.emplace();
MG_Backend::DirectVulkan::PopulateFormatCapabilities(
physicalDevice, vkGetPhysicalDeviceFormatPropertiesFn, formatProbeCapabilities,
builder.report.formatCapabilities.value());
MG_Backend::DirectVulkan::PopulateFormatCapabilities(physicalDevice, vkGetPhysicalDeviceFormatPropertiesFn,
formatProbeCapabilities,
builder.report.formatCapabilities.value());
}
}
@@ -16,8 +16,7 @@
namespace MobileGL::MG_Util::SelfTest {
namespace {
[[nodiscard]] IterationRPWitnessValidationResult Failure(IterationRPWitnessValidationFailure failure,
std::string detail,
std::uint32_t scanStage = 0u,
std::string detail, std::uint32_t scanStage = 0u,
std::uint32_t subgroup = 0u) {
IterationRPWitnessValidationResult result;
result.ok = false;
@@ -60,8 +59,7 @@ namespace MobileGL::MG_Util::SelfTest {
}
} // namespace
IterationRPWitnessEligibilityResult
EvaluateIterationRPWitnessEligibility(const IterationRPWitnessLimits& limits) {
IterationRPWitnessEligibilityResult EvaluateIterationRPWitnessEligibility(const IterationRPWitnessLimits& limits) {
// This classification deliberately precedes numeric limits. An absent native
// compute/basic/arithmetic subgroup contract means there is nothing to witness,
// whereas every resource/entry-point failure on a capable device is a POST FAIL.
@@ -70,9 +68,10 @@ namespace MobileGL::MG_Util::SelfTest {
if (!limits.computeStageSupported) missing.emplace_back("VK_SHADER_STAGE_COMPUTE_BIT");
if (!limits.basicSubgroupSupported) missing.emplace_back("VK_SUBGROUP_FEATURE_BASIC_BIT");
if (!limits.arithmeticSubgroupSupported) missing.emplace_back("VK_SUBGROUP_FEATURE_ARITHMETIC_BIT");
return {IterationRPWitnessEligibility::SkipUnsupportedNativeFeatureSet,
"skipped because the native compute/basic/arithmetic subgroup feature set is unsupported (missing " +
JoinRequirements(missing) + ')'};
return {
IterationRPWitnessEligibility::SkipUnsupportedNativeFeatureSet,
"skipped because the native compute/basic/arithmetic subgroup feature set is unsupported (missing " +
JoinRequirements(missing) + ')'};
}
std::vector<std::string> inadequate;
@@ -100,14 +99,13 @@ namespace MobileGL::MG_Util::SelfTest {
inadequate.emplace_back("maxBoundDescriptorSets < 1");
}
if (limits.maxStorageBufferRange < sizeof(IterationRPWitnessOutput)) {
inadequate.emplace_back("maxStorageBufferRange < " +
std::to_string(sizeof(IterationRPWitnessOutput)));
inadequate.emplace_back("maxStorageBufferRange < " + std::to_string(sizeof(IterationRPWitnessOutput)));
}
if (!inadequate.empty()) {
return {IterationRPWitnessEligibility::FailInadequateLimits,
"insufficient Vulkan limits for a 32x16x1 workgroup, one output SSBO, and " +
std::to_string(kIterationRPWitnessSharedMemoryBytes) + " bytes of shared memory: " +
JoinRequirements(inadequate)};
std::to_string(kIterationRPWitnessSharedMemoryBytes) +
" bytes of shared memory: " + JoinRequirements(inadequate)};
}
return {IterationRPWitnessEligibility::Execute, {}};
}
@@ -159,10 +157,9 @@ namespace MobileGL::MG_Util::SelfTest {
return Failure(IterationRPWitnessValidationFailure::Topology,
"topology: an invocation reported an invalid subgroup lane");
}
if ((output.topologyFlags & ~(IterationRPWitnessNonuniformNumSubgroups |
IterationRPWitnessInvalidNumSubgroups |
IterationRPWitnessInvalidSubgroupId |
IterationRPWitnessInvalidSubgroupLane)) != 0u) {
if ((output.topologyFlags & ~(IterationRPWitnessNonuniformNumSubgroups | IterationRPWitnessInvalidNumSubgroups |
IterationRPWitnessInvalidSubgroupId | IterationRPWitnessInvalidSubgroupLane)) !=
0u) {
std::ostringstream detail;
detail << "topology: unknown topology flags 0x" << std::hex << output.topologyFlags;
return Failure(IterationRPWitnessValidationFailure::Topology, detail.str());
@@ -170,8 +167,8 @@ namespace MobileGL::MG_Util::SelfTest {
const std::uint32_t expectedMask = ExpectedSeenSubgroupMask(numSubgroups);
if (output.seenSubgroupMask != expectedMask) {
std::ostringstream detail;
detail << "topology: seen subgroup-ID mask was 0x" << std::hex << output.seenSubgroupMask
<< ", expected 0x" << expectedMask;
detail << "topology: seen subgroup-ID mask was 0x" << std::hex << output.seenSubgroupMask << ", expected 0x"
<< expectedMask;
return Failure(IterationRPWitnessValidationFailure::Topology, detail.str());
}
const std::uint32_t expectedLoopLength = ComputeIterationRPWitnessLoopLength(numSubgroups);
@@ -184,8 +181,8 @@ namespace MobileGL::MG_Util::SelfTest {
for (std::uint32_t subgroup = 0u; subgroup < numSubgroups; ++subgroup) {
if (output.lastLaneWriterCount[subgroup] != 1u) {
std::ostringstream detail;
detail << "topology: subgroup " << subgroup << " has "
<< output.lastLaneWriterCount[subgroup] << " source last-lane writers, expected exactly 1";
detail << "topology: subgroup " << subgroup << " has " << output.lastLaneWriterCount[subgroup]
<< " source last-lane writers, expected exactly 1";
return Failure(IterationRPWitnessValidationFailure::Topology, detail.str(), 0u, subgroup);
}
}
@@ -203,8 +200,8 @@ namespace MobileGL::MG_Util::SelfTest {
}
if (output.owner511.x == 0u || output.owner511.w != output.owner511.x - 1u) {
std::ostringstream detail;
detail << "final owner: invocation 511 is not the last lane of highest subgroup (size "
<< output.owner511.x << ", lane " << output.owner511.w << ')';
detail << "final owner: invocation 511 is not the last lane of highest subgroup (size " << output.owner511.x
<< ", lane " << output.owner511.w << ')';
return Failure(IterationRPWitnessValidationFailure::FinalOwner, detail.str());
}
@@ -226,8 +223,7 @@ namespace MobileGL::MG_Util::SelfTest {
std::ostringstream detail;
detail << "initial subgroup handoff: subgroup " << subgroup << " rawPrefix observed "
<< Vec2String(output.rawPrefix[subgroup]) << ", expected " << Vec2String(expected);
return Failure(IterationRPWitnessValidationFailure::InitialSubgroupHandoff, detail.str(), 0u,
subgroup);
return Failure(IterationRPWitnessValidationFailure::InitialSubgroupHandoff, detail.str(), 0u, subgroup);
}
}
@@ -266,8 +262,8 @@ namespace MobileGL::MG_Util::SelfTest {
}
std::ostringstream detail;
detail << "N=" << numSubgroups << ", owner511=id" << output.owner511.z << "/lane" << output.owner511.w
<< ", " << expectedLoopLength << " scan stages, average=" << Vec2String(output.finalAverage);
detail << "N=" << numSubgroups << ", owner511=id" << output.owner511.z << "/lane" << output.owner511.w << ", "
<< expectedLoopLength << " scan stages, average=" << Vec2String(output.finalAverage);
IterationRPWitnessValidationResult result;
result.ok = true;
result.failure = IterationRPWitnessValidationFailure::None;
@@ -92,8 +92,7 @@ namespace MobileGL::MG_Util::SelfTest {
// two 32-entry scalar diagnostic arrays in the GLSL source. Keep this
// independent of the output SSBO size.
constexpr std::uint32_t kIterationRPWitnessSharedMemoryBytes =
kIterationRPWitnessMaxSubgroups * sizeof(IterationRPWitnessVec2) +
3u * sizeof(std::uint32_t) +
kIterationRPWitnessMaxSubgroups * sizeof(IterationRPWitnessVec2) + 3u * sizeof(std::uint32_t) +
2u * kIterationRPWitnessMaxSubgroups * sizeof(std::uint32_t);
enum class IterationRPWitnessEligibility {
@@ -142,12 +141,11 @@ namespace MobileGL::MG_Util::SelfTest {
std::string detail;
};
[[nodiscard]] IterationRPWitnessEligibilityResult
EvaluateIterationRPWitnessEligibility(const IterationRPWitnessLimits& limits);
[[nodiscard]] IterationRPWitnessEligibilityResult EvaluateIterationRPWitnessEligibility(
const IterationRPWitnessLimits& limits);
// Mirrors the source's findMSB expression for valid N in [2, 32].
[[nodiscard]] std::uint32_t ComputeIterationRPWitnessLoopLength(std::uint32_t numSubgroups);
[[nodiscard]] IterationRPWitnessValidationResult
ValidateIterationRPWitness(const IterationRPWitnessOutput& output);
[[nodiscard]] IterationRPWitnessValidationResult ValidateIterationRPWitness(const IterationRPWitnessOutput& output);
} // namespace MobileGL::MG_Util::SelfTest
+15 -17
View File
@@ -76,8 +76,8 @@ namespace {
Uint64 BuildFormatCapabilityMask(FormatCapabilityFlags capabilities) {
Uint64 mask = 0;
for (SizeT capabilityIndex = 0;
capabilityIndex < MobileGL::MG_Backend::kReportedFormatCapabilities.size(); ++capabilityIndex) {
for (SizeT capabilityIndex = 0; capabilityIndex < MobileGL::MG_Backend::kReportedFormatCapabilities.size();
++capabilityIndex) {
if (MobileGL::MG_Backend::HasFormatCapability(
capabilities, MobileGL::MG_Backend::kReportedFormatCapabilities[capabilityIndex])) {
mask |= 1ull << capabilityIndex;
@@ -88,18 +88,16 @@ namespace {
void AppendFormatCapabilitiesJson(StringStream& out, const FormatCapabilityCache& cache) {
out << ",\"formatCapabilities\":{\"capabilities\":[";
for (SizeT capabilityIndex = 0;
capabilityIndex < MobileGL::MG_Backend::kReportedFormatCapabilities.size(); ++capabilityIndex) {
for (SizeT capabilityIndex = 0; capabilityIndex < MobileGL::MG_Backend::kReportedFormatCapabilities.size();
++capabilityIndex) {
if (capabilityIndex != 0) {
out << ',';
}
AppendJsonString(
out, MobileGL::MG_Backend::GetFormatCapabilityName(
MobileGL::MG_Backend::kReportedFormatCapabilities[capabilityIndex]));
AppendJsonString(out, MobileGL::MG_Backend::GetFormatCapabilityName(
MobileGL::MG_Backend::kReportedFormatCapabilities[capabilityIndex]));
}
out << "],\"targets\":[";
for (SizeT targetIndex = 0; targetIndex < MobileGL::MG_Backend::kFormatCapabilityTargetCount;
++targetIndex) {
for (SizeT targetIndex = 0; targetIndex < MobileGL::MG_Backend::kFormatCapabilityTargetCount; ++targetIndex) {
if (targetIndex != 0) {
out << ',';
}
@@ -112,9 +110,8 @@ namespace {
out << ',';
}
out << '[';
AppendJsonString(
out, MobileGL::MG_Util::ConvertTextureInternalFormatToString(
static_cast<MobileGL::TextureInternalFormat>(formatIndex)));
AppendJsonString(out, MobileGL::MG_Util::ConvertTextureInternalFormatToString(
static_cast<MobileGL::TextureInternalFormat>(formatIndex)));
out << ',' << BuildFormatCapabilityMask(cache.FullCaps[targetIndex][formatIndex]);
out << ',' << BuildFormatCapabilityMask(cache.CaveatCaps[targetIndex][formatIndex]) << ']';
}
@@ -157,9 +154,8 @@ namespace {
out << "{\"name\":";
AppendJsonString(out, bug.name);
out << ",\"verdict\":";
AppendJsonString(out, bug.verdict == MobileGL::MG_Util::SelfTest::DriverBugVerdict::Fixed
? "FIXED"
: "UNFIXABLE");
AppendJsonString(out, bug.verdict == MobileGL::MG_Util::SelfTest::DriverBugVerdict::Fixed ? "FIXED"
: "UNFIXABLE");
out << ",\"detail\":";
AppendJsonString(out, bug.detail);
out << '}';
@@ -184,9 +180,11 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_PostActivity_nativ
AppendBackendReportJson(out, vulkanReport);
out << '}';
json = out.str();
} catch (const std::exception& exception) {
}
catch (const std::exception& exception) {
json = "{\"error\":\"" + EscapeJsonString(exception.what()) + "\"}";
} catch (...) {
}
catch (...) {
json = "{\"error\":\"driver POST failed with an unknown native exception\"}";
}
return env->NewStringUTF(json.c_str());
@@ -30,9 +30,18 @@ namespace MobileGL::MG_Util::SelfTest {
constexpr GLsizei kQuads = 64 * 32;
constexpr Int kAttempts = 3;
constexpr std::array<const char*, 3> kUploadNames = {"SubData", "Copy/persistent staging",
"Copy/SubData staging"};
enum class Shape { Unmapped, Mapped, FinishBefore, FinishBoth, MapThenUnmap, BarrierBefore };
struct Vertex { GLfloat x, y, r, g, b; };
"Copy/SubData staging"};
enum class Shape {
Unmapped,
Mapped,
FinishBefore,
FinishBoth,
MapThenUnmap,
BarrierBefore
};
struct Vertex {
GLfloat x, y, r, g, b;
};
constexpr GLsizeiptr kPayloadSize = kQuads * 6 * sizeof(Vertex);
static_assert(kOffset + kPayloadSize <= kArenaSize);
@@ -41,20 +50,20 @@ namespace MobileGL::MG_Util::SelfTest {
}
Bool CanProbe(const GLESFunctionsTable& gl) {
if (!(gl.glGetIntegerv && gl.glGetBooleanv && gl.glGetFloatv && gl.glGetError &&
gl.glGetStringi && gl.glIsEnabled && gl.glEnable && gl.glDisable &&
gl.glCreateShader && gl.glShaderSource && gl.glCompileShader && gl.glGetShaderiv &&
gl.glGetShaderInfoLog && gl.glDeleteShader && gl.glCreateProgram && gl.glAttachShader &&
gl.glLinkProgram && gl.glGetProgramiv && gl.glGetProgramInfoLog && gl.glDeleteProgram &&
gl.glUseProgram && gl.glGenBuffers && gl.glBindBuffer && gl.glBufferStorageEXT &&
gl.glMapBufferRange && gl.glUnmapBuffer && gl.glBufferData && gl.glBufferSubData &&
gl.glCopyBufferSubData && gl.glDeleteBuffers && gl.glGenVertexArrays &&
gl.glBindVertexArray && gl.glVertexAttribPointer && gl.glEnableVertexAttribArray &&
gl.glDeleteVertexArrays && gl.glGenTextures && gl.glBindTexture && gl.glTexStorage2D &&
gl.glDeleteTextures && gl.glGenFramebuffers && gl.glBindFramebuffer &&
if (!(gl.glGetIntegerv && gl.glGetBooleanv && gl.glGetFloatv && gl.glGetError && gl.glGetStringi &&
gl.glIsEnabled && gl.glEnable && gl.glDisable && gl.glCreateShader && gl.glShaderSource &&
gl.glCompileShader && gl.glGetShaderiv && gl.glGetShaderInfoLog && gl.glDeleteShader &&
gl.glCreateProgram && gl.glAttachShader && gl.glLinkProgram && gl.glGetProgramiv &&
gl.glGetProgramInfoLog && gl.glDeleteProgram && gl.glUseProgram && gl.glGenBuffers &&
gl.glBindBuffer && gl.glBufferStorageEXT && gl.glMapBufferRange && gl.glUnmapBuffer &&
gl.glBufferData && gl.glBufferSubData && gl.glCopyBufferSubData && gl.glDeleteBuffers &&
gl.glGenVertexArrays && gl.glBindVertexArray && gl.glVertexAttribPointer &&
gl.glEnableVertexAttribArray && gl.glDeleteVertexArrays && gl.glGenTextures && gl.glBindTexture &&
gl.glTexStorage2D && gl.glDeleteTextures && gl.glGenFramebuffers && gl.glBindFramebuffer &&
gl.glFramebufferTexture2D && gl.glCheckFramebufferStatus && gl.glDeleteFramebuffers &&
gl.glViewport && gl.glColorMask && gl.glClearColor && gl.glClear && gl.glDrawArrays &&
gl.glFinish && gl.glMemoryBarrier && gl.glPixelStorei && gl.glReadPixels)) return false;
gl.glViewport && gl.glColorMask && gl.glClearColor && gl.glClear && gl.glDrawArrays && gl.glFinish &&
gl.glMemoryBarrier && gl.glPixelStorei && gl.glReadPixels))
return false;
DrainErrors(gl);
GLint major = 0, minor = 0, count = 0;
gl.glGetIntegerv(GL_MAJOR_VERSION, &major);
@@ -63,8 +72,8 @@ namespace MobileGL::MG_Util::SelfTest {
if (gl.glGetError() != GL_NO_ERROR || major < 3 || (major == 3 && minor < 1)) return false;
for (GLint i = 0; i < count; ++i) {
const auto* extension = gl.glGetStringi(GL_EXTENSIONS, i);
if (extension && std::strcmp(reinterpret_cast<const char*>(extension),
"GL_EXT_buffer_storage") == 0) return true;
if (extension && std::strcmp(reinterpret_cast<const char*>(extension), "GL_EXT_buffer_storage") == 0)
return true;
}
return false;
}
@@ -79,11 +88,11 @@ namespace MobileGL::MG_Util::SelfTest {
GLfloat clear[4]{};
GLboolean colorMask[4]{};
static constexpr std::array<GLenum, 10> enables = {
GL_BLEND, GL_DEPTH_TEST, GL_STENCIL_TEST, GL_CULL_FACE, GL_SCISSOR_TEST,
GL_RASTERIZER_DISCARD, GL_DITHER, GL_SAMPLE_ALPHA_TO_COVERAGE,
GL_BLEND, GL_DEPTH_TEST, GL_STENCIL_TEST, GL_CULL_FACE,
GL_SCISSOR_TEST, GL_RASTERIZER_DISCARD, GL_DITHER, GL_SAMPLE_ALPHA_TO_COVERAGE,
GL_SAMPLE_COVERAGE, GL_SAMPLE_MASK};
static constexpr std::array<GLenum, 4> packNames = {
GL_PACK_ALIGNMENT, GL_PACK_ROW_LENGTH, GL_PACK_SKIP_PIXELS, GL_PACK_SKIP_ROWS};
static constexpr std::array<GLenum, 4> packNames = {GL_PACK_ALIGNMENT, GL_PACK_ROW_LENGTH,
GL_PACK_SKIP_PIXELS, GL_PACK_SKIP_ROWS};
std::array<GLboolean, enables.size()> enabled{};
std::array<GLint, packNames.size()> pack{};
@@ -100,14 +109,18 @@ namespace MobileGL::MG_Util::SelfTest {
gl.glGetIntegerv(GL_VIEWPORT, viewport);
gl.glGetFloatv(GL_COLOR_CLEAR_VALUE, clear);
gl.glGetBooleanv(GL_COLOR_WRITEMASK, colorMask);
for (SizeT i = 0; i < enables.size(); ++i) enabled[i] = gl.glIsEnabled(enables[i]);
for (SizeT i = 0; i < packNames.size(); ++i) gl.glGetIntegerv(packNames[i], &pack[i]);
for (SizeT i = 0; i < enables.size(); ++i)
enabled[i] = gl.glIsEnabled(enables[i]);
for (SizeT i = 0; i < packNames.size(); ++i)
gl.glGetIntegerv(packNames[i], &pack[i]);
}
void Prepare() {
for (auto cap : enables) gl.glDisable(cap);
for (auto cap : enables)
gl.glDisable(cap);
gl.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
gl.glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
for (auto name : packNames) gl.glPixelStorei(name, name == GL_PACK_ALIGNMENT ? 1 : 0);
for (auto name : packNames)
gl.glPixelStorei(name, name == GL_PACK_ALIGNMENT ? 1 : 0);
gl.glViewport(0, 0, kSide, kSide);
gl.glClearColor(0, 0, 0, 1);
}
@@ -125,9 +138,13 @@ namespace MobileGL::MG_Util::SelfTest {
gl.glClearColor(clear[0], clear[1], clear[2], clear[3]);
gl.glColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
for (SizeT i = 0; i < enables.size(); ++i) {
if (enabled[i]) gl.glEnable(enables[i]); else gl.glDisable(enables[i]);
if (enabled[i])
gl.glEnable(enables[i]);
else
gl.glDisable(enables[i]);
}
for (SizeT i = 0; i < packNames.size(); ++i) gl.glPixelStorei(packNames[i], pack[i]);
for (SizeT i = 0; i < packNames.size(); ++i)
gl.glPixelStorei(packNames[i], pack[i]);
}
};
@@ -159,12 +176,14 @@ namespace MobileGL::MG_Util::SelfTest {
return shader;
}
Bool Setup() {
const GLuint vs = Compile(GL_VERTEX_SHADER,
"#version 310 es\nlayout(location=0) in vec2 pos; layout(location=1) in vec3 color;\n"
"out highp vec3 vColor; void main(){gl_Position=vec4(pos,0,1);vColor=color;}\n");
const GLuint fs = Compile(GL_FRAGMENT_SHADER,
"#version 310 es\nprecision highp float; in highp vec3 vColor;\n"
"layout(location=0) out vec4 outColor; void main(){outColor=vec4(vColor,1);}\n");
const GLuint vs =
Compile(GL_VERTEX_SHADER,
"#version 310 es\nlayout(location=0) in vec2 pos; layout(location=1) in vec3 color;\n"
"out highp vec3 vColor; void main(){gl_Position=vec4(pos,0,1);vColor=color;}\n");
const GLuint fs =
Compile(GL_FRAGMENT_SHADER,
"#version 310 es\nprecision highp float; in highp vec3 vColor;\n"
"layout(location=0) out vec4 outColor; void main(){outColor=vec4(vColor,1);}\n");
if (vs && fs) {
program = gl.glCreateProgram();
if (program) {
@@ -194,8 +213,7 @@ namespace MobileGL::MG_Util::SelfTest {
gl.glBindTexture(GL_TEXTURE_2D, textures[i]);
gl.glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, kSide, kSide);
gl.glBindFramebuffer(GL_FRAMEBUFFER, fbos[i]);
gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
textures[i], 0);
gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[i], 0);
if (gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) return false;
}
return gl.glGetError() == GL_NO_ERROR;
@@ -216,8 +234,12 @@ namespace MobileGL::MG_Util::SelfTest {
};
void FillVertices(Vector<Vertex>& vertices, Int channel) {
constexpr std::array<Vertex, 6> quad = {{{-1,-1,0,0,0}, {1,-1,0,0,0}, {1,1,0,0,0},
{-1,-1,0,0,0}, {1,1,0,0,0}, {-1,1,0,0,0}}};
constexpr std::array<Vertex, 6> quad = {{{-1, -1, 0, 0, 0},
{1, -1, 0, 0, 0},
{1, 1, 0, 0, 0},
{-1, -1, 0, 0, 0},
{1, 1, 0, 0, 0},
{-1, 1, 0, 0, 0}}};
for (SizeT k = 0; k < vertices.size(); ++k) {
auto& v = vertices[k];
v = quad[k % 6];
@@ -230,8 +252,8 @@ namespace MobileGL::MG_Util::SelfTest {
}
}
BufferOrderingSample Run(const GLESFunctionsTable& gl, const Resources& resources,
const Vector<Uint8>& seed, Int upload, Shape shape) {
BufferOrderingSample Run(const GLESFunctionsTable& gl, const Resources& resources, const Vector<Uint8>& seed,
Int upload, Shape shape) {
BufferOrderingSample sample;
sample.status = BufferOrderingProbeStatus::Failed;
Buffers buffers(gl);
@@ -252,9 +274,9 @@ namespace MobileGL::MG_Util::SelfTest {
if (shape == Shape::MapThenUnmap && !gl.glUnmapBuffer(GL_ARRAY_BUFFER)) break;
}
gl.glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<const void*>(kOffset));
reinterpret_cast<const void*>(kOffset));
gl.glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<const void*>(kOffset + 2 * sizeof(GLfloat)));
reinterpret_cast<const void*>(kOffset + 2 * sizeof(GLfloat)));
gl.glEnableVertexAttribArray(0);
gl.glEnableVertexAttribArray(1);
void* sourceMap = nullptr;
@@ -283,19 +305,20 @@ namespace MobileGL::MG_Util::SelfTest {
} else {
// No slot is reused until the entire batch has finished on the GPU.
if (upload == 1) {
std::memcpy(static_cast<Uint8*>(sourceMap) + slot * kPayloadSize,
payload.data(), kPayloadSize);
std::memcpy(static_cast<Uint8*>(sourceMap) + slot * kPayloadSize, payload.data(),
kPayloadSize);
} else {
gl.glBufferSubData(GL_COPY_READ_BUFFER, slot * kPayloadSize,
kPayloadSize, payload.data());
gl.glBufferSubData(GL_COPY_READ_BUFFER, slot * kPayloadSize, kPayloadSize,
payload.data());
}
gl.glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
slot * kPayloadSize, kOffset, kPayloadSize);
gl.glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, slot * kPayloadSize,
kOffset, kPayloadSize);
}
if (shape == Shape::FinishBoth) gl.glFinish();
gl.glBindFramebuffer(GL_FRAMEBUFFER, resources.fbos[slot]);
gl.glClear(GL_COLOR_BUFFER_BIT);
for (Int draw = 0; draw < kDraws; ++draw) gl.glDrawArrays(GL_TRIANGLES, 0, kQuads * 6);
for (Int draw = 0; draw < kDraws; ++draw)
gl.glDrawArrays(GL_TRIANGLES, 0, kQuads * 6);
}
// No readback/Finish between subject update/draw pairs. Early readback
// would hide precisely the old-reader/new-writer overlap being tested.
@@ -337,9 +360,10 @@ namespace MobileGL::MG_Util::SelfTest {
String DescribeUpload(const BufferOrderingUploadMeasurement& row, Int upload) {
return format("{}: mapped {}, never-mapped {}, Finish-before {}, Finish-both {}, "
"map-then-unmap {}, barrier-before {}", kUploadNames[upload], Describe(row.mapped),
Describe(row.unmapped), Describe(row.finishBefore), Describe(row.finishBoth),
Describe(row.mapThenUnmap), Describe(row.barrierBefore));
"map-then-unmap {}, barrier-before {}",
kUploadNames[upload], Describe(row.mapped), Describe(row.unmapped),
Describe(row.finishBefore), Describe(row.finishBoth), Describe(row.mapThenUnmap),
Describe(row.barrierBefore));
}
} // namespace
@@ -351,7 +375,8 @@ namespace MobileGL::MG_Util::SelfTest {
state.Prepare();
Resources resources(gl);
if (gl.glGetError() != GL_NO_ERROR || !resources.Setup()) {
for (auto& row : measurement.uploads) row.unmapped.status = BufferOrderingProbeStatus::Failed;
for (auto& row : measurement.uploads)
row.unmapped.status = BufferOrderingProbeStatus::Failed;
MGLOG_I("[driver-bug] persistent buffer ordering: setup failed; inconclusive");
return measurement;
}
@@ -382,13 +407,15 @@ namespace MobileGL::MG_Util::SelfTest {
row.Detected() ? "detected" : "not detected or inconclusive");
}
return measurement;
} catch (const std::bad_alloc&) {
}
catch (const std::bad_alloc&) {
// The CPU initializer is arena-sized too. An allocation failure must not discard
// the rest of the POST report or turn a partially sampled case into a finding.
MGLOG_I("[driver-bug] persistent buffer ordering: host allocation failed; inconclusive");
PersistentBufferOrderingMeasurement measurement;
measurement.supported = true;
for (auto& row : measurement.uploads) row.unmapped.status = BufferOrderingProbeStatus::Failed;
for (auto& row : measurement.uploads)
row.unmapped.status = BufferOrderingProbeStatus::Failed;
return measurement;
}
@@ -12,7 +12,11 @@
#include <array>
namespace MobileGL::MG_Util::SelfTest {
enum class BufferOrderingProbeStatus : Uint8 { NotRun, Complete, Failed };
enum class BufferOrderingProbeStatus : Uint8 {
NotRun,
Complete,
Failed
};
struct BufferOrderingSample {
BufferOrderingProbeStatus status = BufferOrderingProbeStatus::NotRun;
@@ -33,8 +37,8 @@ namespace MobileGL::MG_Util::SelfTest {
BufferOrderingSample barrierBefore;
Bool Detected() const {
return unmapped.Passed() && finishBoth.Passed() &&
mapped.status == BufferOrderingProbeStatus::Complete && mapped.badFrames != 0;
return unmapped.Passed() && finishBoth.Passed() && mapped.status == BufferOrderingProbeStatus::Complete &&
mapped.badFrames != 0;
}
};
@@ -57,8 +61,7 @@ namespace MobileGL::MG_Util::SelfTest {
// Finish-before-and-after control permits a finding. Setup/GL failures are inconclusive.
// Explicit allocations are one 128 MiB arena, its initializer, and small staging/FBOs;
// allocations, batches and draws are bounded. Every touched GL state is restored.
PersistentBufferOrderingMeasurement ProbePersistentBufferUpdateOrdering(
const MG_External::GLESFunctionsTable& gl);
PersistentBufferOrderingMeasurement ProbePersistentBufferUpdateOrdering(const MG_External::GLESFunctionsTable& gl);
// Used by the POST collector. A report never labels an inconclusive sample as a bug.
Optional<DriverBugFinding> DescribePersistentBufferOrderingBug(
@@ -40,9 +40,8 @@ namespace MobileGL::MG_Util::SelfTest {
fns.vkCreateShaderModule != nullptr && fns.vkDestroyShaderModule != nullptr &&
fns.vkCreatePipelineLayout != nullptr && fns.vkDestroyPipelineLayout != nullptr &&
fns.vkCreateGraphicsPipelines != nullptr && fns.vkDestroyPipeline != nullptr &&
fns.vkCmdBindPipeline != nullptr && fns.vkCmdDraw != nullptr &&
fns.vkCreateFence != nullptr && fns.vkDestroyFence != nullptr &&
fns.vkQueueSubmit != nullptr && fns.vkWaitForFences != nullptr &&
fns.vkCmdBindPipeline != nullptr && fns.vkCmdDraw != nullptr && fns.vkCreateFence != nullptr &&
fns.vkDestroyFence != nullptr && fns.vkQueueSubmit != nullptr && fns.vkWaitForFences != nullptr &&
fns.vkGetQueryPoolResults != nullptr && fns.vkDeviceWaitIdle != nullptr;
}
} // namespace
@@ -111,12 +110,10 @@ namespace MobileGL::MG_Util::SelfTest {
}
fns.vkDeviceWaitIdle(device);
if (fence != VK_NULL_HANDLE) fns.vkDestroyFence(device, fence, nullptr);
if (trianglePlainPipeline != VK_NULL_HANDLE)
fns.vkDestroyPipeline(device, trianglePlainPipeline, nullptr);
if (trianglePlainPipeline != VK_NULL_HANDLE) fns.vkDestroyPipeline(device, trianglePlainPipeline, nullptr);
if (triangleDiscardPipeline != VK_NULL_HANDLE)
fns.vkDestroyPipeline(device, triangleDiscardPipeline, nullptr);
if (patchDiscardPipeline != VK_NULL_HANDLE)
fns.vkDestroyPipeline(device, patchDiscardPipeline, nullptr);
if (patchDiscardPipeline != VK_NULL_HANDLE) fns.vkDestroyPipeline(device, patchDiscardPipeline, nullptr);
if (pipelineLayout != VK_NULL_HANDLE) fns.vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
if (vertModule != VK_NULL_HANDLE) fns.vkDestroyShaderModule(device, vertModule, nullptr);
if (tescModule != VK_NULL_HANDLE) fns.vkDestroyShaderModule(device, tescModule, nullptr);
@@ -159,8 +156,7 @@ namespace MobileGL::MG_Util::SelfTest {
pgqPoolInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
pgqPoolInfo.queryType = VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT;
pgqPoolInfo.queryCount = kShapeSlots;
if (fns.vkCreateQueryPool(device, &pgqPoolInfo, nullptr, &primitivesGeneratedQueryPool) !=
VK_SUCCESS) {
if (fns.vkCreateQueryPool(device, &pgqPoolInfo, nullptr, &primitivesGeneratedQueryPool) != VK_SUCCESS) {
return fail("vkCreateQueryPool(PRIMITIVES_GENERATED_EXT) failed");
}
}
@@ -305,8 +301,7 @@ namespace MobileGL::MG_Util::SelfTest {
pipelineInfo.layout = pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = 0;
return fns.vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &out) ==
VK_SUCCESS;
return fns.vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &out) == VK_SUCCESS;
};
if (!makePipeline(false, false, trianglePlainPipeline)) {
return fail("vkCreateGraphicsPipelines(triangles) failed");
@@ -451,9 +446,8 @@ namespace MobileGL::MG_Util::SelfTest {
if (!measurement.ran || !measurement.trianglesPlain.drawn || !measurement.trianglesDiscard.drawn) {
return PrimitivesGeneratedNoXfbVerdict::Inconclusive;
}
const PrimitivesGeneratedNoXfbShapeMeasurement* shapes[3] = {&measurement.trianglesPlain,
&measurement.trianglesDiscard,
&measurement.patchesDiscard};
const PrimitivesGeneratedNoXfbShapeMeasurement* shapes[3] = {
&measurement.trianglesPlain, &measurement.trianglesDiscard, &measurement.patchesDiscard};
Bool anyStreamSilent = false;
Bool allStreamExact = true;
Bool allPrimitivesGeneratedExtExact = true;
@@ -483,12 +477,10 @@ namespace MobileGL::MG_Util::SelfTest {
return PrimitivesGeneratedNoXfbVerdict::Inconclusive;
}
}
if (!shape->primitivesGeneratedExtMeasured ||
shape->primitivesGeneratedExt != shape->expectedPrimitives) {
if (!shape->primitivesGeneratedExtMeasured || shape->primitivesGeneratedExt != shape->expectedPrimitives) {
allPrimitivesGeneratedExtExact = false;
}
if (!shape->statisticsMeasured ||
shape->statisticsClippingInput != shape->expectedPrimitives) {
if (!shape->statisticsMeasured || shape->statisticsClippingInput != shape->expectedPrimitives) {
allStatisticsExact = false;
// Only a shape the stream query was silent on may be left behind by
// the substitute; a shape it answered exactly must not be traded away.
@@ -533,8 +525,7 @@ namespace MobileGL::MG_Util::SelfTest {
if (primitivesGeneratedQueryUsable) {
return PrimGenRerouteKind::PrimitivesGeneratedExt;
}
return pipelineStatisticsEnabled ? PrimGenRerouteKind::ClippingStatistics
: PrimGenRerouteKind::None;
return pipelineStatisticsEnabled ? PrimGenRerouteKind::ClippingStatistics : PrimGenRerouteKind::None;
case MG_Config::QuirkOverride::Auto:
break;
}
@@ -544,8 +535,7 @@ namespace MobileGL::MG_Util::SelfTest {
: PrimGenRerouteKind::None;
case PrimitivesGeneratedNoXfbVerdict::StatisticsSubstitute:
case PrimitivesGeneratedNoXfbVerdict::StatisticsSubstitutePlainOnly:
return pipelineStatisticsEnabled ? PrimGenRerouteKind::ClippingStatistics
: PrimGenRerouteKind::None;
return pipelineStatisticsEnabled ? PrimGenRerouteKind::ClippingStatistics : PrimGenRerouteKind::None;
case PrimitivesGeneratedNoXfbVerdict::Inconclusive:
case PrimitivesGeneratedNoXfbVerdict::StreamCounts:
case PrimitivesGeneratedNoXfbVerdict::Unfixable:
@@ -22,134 +22,99 @@
namespace MobileGL::MG_Util::SelfTest {
inline constexpr std::uint32_t kPrimitivesGeneratedNoXfbProbeVertSpv[] = {
0x07230203, 0x00010300, 0x0008000b, 0x0000002a, 0x00000000, 0x00020011,
0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000000,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x0000001a, 0x00030003,
0x00000002, 0x000001c2, 0x00040005, 0x00000004, 0x6e69616d, 0x00000000,
0x00060005, 0x0000000b, 0x505f6c67, 0x65567265, 0x78657472, 0x00000000,
0x00060006, 0x0000000b, 0x00000000, 0x505f6c67, 0x7469736f, 0x006e6f69,
0x00070006, 0x0000000b, 0x00000001, 0x505f6c67, 0x746e696f, 0x657a6953,
0x00000000, 0x00070006, 0x0000000b, 0x00000002, 0x435f6c67, 0x4470696c,
0x61747369, 0x0065636e, 0x00070006, 0x0000000b, 0x00000003, 0x435f6c67,
0x446c6c75, 0x61747369, 0x0065636e, 0x00030005, 0x0000000d, 0x00000000,
0x00060005, 0x0000001a, 0x565f6c67, 0x65747265, 0x646e4978, 0x00007865,
0x00050005, 0x0000001f, 0x65646e69, 0x6c626178, 0x00000065, 0x00030047,
0x0000000b, 0x00000002, 0x00050048, 0x0000000b, 0x00000000, 0x0000000b,
0x00000000, 0x00050048, 0x0000000b, 0x00000001, 0x0000000b, 0x00000001,
0x00050048, 0x0000000b, 0x00000002, 0x0000000b, 0x00000003, 0x00050048,
0x0000000b, 0x00000003, 0x0000000b, 0x00000004, 0x00040047, 0x0000001a,
0x0000000b, 0x0000002a, 0x00020013, 0x00000002, 0x00030021, 0x00000003,
0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007,
0x00000006, 0x00000004, 0x00040015, 0x00000008, 0x00000020, 0x00000000,
0x0004002b, 0x00000008, 0x00000009, 0x00000001, 0x0004001c, 0x0000000a,
0x00000006, 0x00000009, 0x0006001e, 0x0000000b, 0x00000007, 0x00000006,
0x0000000a, 0x0000000a, 0x00040020, 0x0000000c, 0x00000003, 0x0000000b,
0x0004003b, 0x0000000c, 0x0000000d, 0x00000003, 0x00040015, 0x0000000e,
0x00000020, 0x00000001, 0x0004002b, 0x0000000e, 0x0000000f, 0x00000000,
0x00040017, 0x00000010, 0x00000006, 0x00000002, 0x0004002b, 0x00000008,
0x00000011, 0x00000003, 0x0004001c, 0x00000012, 0x00000010, 0x00000011,
0x0004002b, 0x00000006, 0x00000013, 0xbf800000, 0x0005002c, 0x00000010,
0x00000014, 0x00000013, 0x00000013, 0x0004002b, 0x00000006, 0x00000015,
0x40400000, 0x0005002c, 0x00000010, 0x00000016, 0x00000015, 0x00000013,
0x0005002c, 0x00000010, 0x00000017, 0x00000013, 0x00000015, 0x0006002c,
0x00000012, 0x00000018, 0x00000014, 0x00000016, 0x00000017, 0x00040020,
0x00000019, 0x00000001, 0x0000000e, 0x0004003b, 0x00000019, 0x0000001a,
0x00000001, 0x0004002b, 0x0000000e, 0x0000001c, 0x00000003, 0x00040020,
0x0000001e, 0x00000007, 0x00000012, 0x00040020, 0x00000020, 0x00000007,
0x00000010, 0x0004002b, 0x00000006, 0x00000023, 0x00000000, 0x0004002b,
0x00000006, 0x00000024, 0x3f800000, 0x00040020, 0x00000028, 0x00000003,
0x00000007, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003,
0x000200f8, 0x00000005, 0x0004003b, 0x0000001e, 0x0000001f, 0x00000007,
0x0004003d, 0x0000000e, 0x0000001b, 0x0000001a, 0x0005008b, 0x0000000e,
0x0000001d, 0x0000001b, 0x0000001c, 0x0003003e, 0x0000001f, 0x00000018,
0x00050041, 0x00000020, 0x00000021, 0x0000001f, 0x0000001d, 0x0004003d,
0x00000010, 0x00000022, 0x00000021, 0x00050051, 0x00000006, 0x00000025,
0x00000022, 0x00000000, 0x00050051, 0x00000006, 0x00000026, 0x00000022,
0x00000001, 0x00070050, 0x00000007, 0x00000027, 0x00000025, 0x00000026,
0x00000023, 0x00000024, 0x00050041, 0x00000028, 0x00000029, 0x0000000d,
0x0000000f, 0x0003003e, 0x00000029, 0x00000027, 0x000100fd, 0x00010038,
0x07230203, 0x00010300, 0x0008000b, 0x0000002a, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 0x00000001,
0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000000,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x0000001a, 0x00030003, 0x00000002, 0x000001c2, 0x00040005,
0x00000004, 0x6e69616d, 0x00000000, 0x00060005, 0x0000000b, 0x505f6c67, 0x65567265, 0x78657472, 0x00000000,
0x00060006, 0x0000000b, 0x00000000, 0x505f6c67, 0x7469736f, 0x006e6f69, 0x00070006, 0x0000000b, 0x00000001,
0x505f6c67, 0x746e696f, 0x657a6953, 0x00000000, 0x00070006, 0x0000000b, 0x00000002, 0x435f6c67, 0x4470696c,
0x61747369, 0x0065636e, 0x00070006, 0x0000000b, 0x00000003, 0x435f6c67, 0x446c6c75, 0x61747369, 0x0065636e,
0x00030005, 0x0000000d, 0x00000000, 0x00060005, 0x0000001a, 0x565f6c67, 0x65747265, 0x646e4978, 0x00007865,
0x00050005, 0x0000001f, 0x65646e69, 0x6c626178, 0x00000065, 0x00030047, 0x0000000b, 0x00000002, 0x00050048,
0x0000000b, 0x00000000, 0x0000000b, 0x00000000, 0x00050048, 0x0000000b, 0x00000001, 0x0000000b, 0x00000001,
0x00050048, 0x0000000b, 0x00000002, 0x0000000b, 0x00000003, 0x00050048, 0x0000000b, 0x00000003, 0x0000000b,
0x00000004, 0x00040047, 0x0000001a, 0x0000000b, 0x0000002a, 0x00020013, 0x00000002, 0x00030021, 0x00000003,
0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004, 0x00040015,
0x00000008, 0x00000020, 0x00000000, 0x0004002b, 0x00000008, 0x00000009, 0x00000001, 0x0004001c, 0x0000000a,
0x00000006, 0x00000009, 0x0006001e, 0x0000000b, 0x00000007, 0x00000006, 0x0000000a, 0x0000000a, 0x00040020,
0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c, 0x0000000d, 0x00000003, 0x00040015, 0x0000000e,
0x00000020, 0x00000001, 0x0004002b, 0x0000000e, 0x0000000f, 0x00000000, 0x00040017, 0x00000010, 0x00000006,
0x00000002, 0x0004002b, 0x00000008, 0x00000011, 0x00000003, 0x0004001c, 0x00000012, 0x00000010, 0x00000011,
0x0004002b, 0x00000006, 0x00000013, 0xbf800000, 0x0005002c, 0x00000010, 0x00000014, 0x00000013, 0x00000013,
0x0004002b, 0x00000006, 0x00000015, 0x40400000, 0x0005002c, 0x00000010, 0x00000016, 0x00000015, 0x00000013,
0x0005002c, 0x00000010, 0x00000017, 0x00000013, 0x00000015, 0x0006002c, 0x00000012, 0x00000018, 0x00000014,
0x00000016, 0x00000017, 0x00040020, 0x00000019, 0x00000001, 0x0000000e, 0x0004003b, 0x00000019, 0x0000001a,
0x00000001, 0x0004002b, 0x0000000e, 0x0000001c, 0x00000003, 0x00040020, 0x0000001e, 0x00000007, 0x00000012,
0x00040020, 0x00000020, 0x00000007, 0x00000010, 0x0004002b, 0x00000006, 0x00000023, 0x00000000, 0x0004002b,
0x00000006, 0x00000024, 0x3f800000, 0x00040020, 0x00000028, 0x00000003, 0x00000007, 0x00050036, 0x00000002,
0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, 0x0000001e, 0x0000001f, 0x00000007,
0x0004003d, 0x0000000e, 0x0000001b, 0x0000001a, 0x0005008b, 0x0000000e, 0x0000001d, 0x0000001b, 0x0000001c,
0x0003003e, 0x0000001f, 0x00000018, 0x00050041, 0x00000020, 0x00000021, 0x0000001f, 0x0000001d, 0x0004003d,
0x00000010, 0x00000022, 0x00000021, 0x00050051, 0x00000006, 0x00000025, 0x00000022, 0x00000000, 0x00050051,
0x00000006, 0x00000026, 0x00000022, 0x00000001, 0x00070050, 0x00000007, 0x00000027, 0x00000025, 0x00000026,
0x00000023, 0x00000024, 0x00050041, 0x00000028, 0x00000029, 0x0000000d, 0x0000000f, 0x0003003e, 0x00000029,
0x00000027, 0x000100fd, 0x00010038,
};
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeVertSpvWordCount = sizeof(kPrimitivesGeneratedNoXfbProbeVertSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeVertSpv[0]);
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeVertSpvWordCount =
sizeof(kPrimitivesGeneratedNoXfbProbeVertSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeVertSpv[0]);
inline constexpr std::uint32_t kPrimitivesGeneratedNoXfbProbeTescSpv[] = {
0x07230203, 0x00010300, 0x0008000b, 0x0000001d, 0x00000000, 0x00020011,
0x00000003, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000001,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x0000001a, 0x00040010,
0x00000004, 0x0000001a, 0x00000001, 0x00030003, 0x00000002, 0x000001c2,
0x00040005, 0x00000004, 0x6e69616d, 0x00000000, 0x00070005, 0x0000000b,
0x545f6c67, 0x4c737365, 0x6c657665, 0x6574754f, 0x00000072, 0x00070005,
0x0000001a, 0x545f6c67, 0x4c737365, 0x6c657665, 0x656e6e49, 0x00000072,
0x00040047, 0x0000000b, 0x0000000b, 0x0000000b, 0x00030047, 0x0000000b,
0x0000000f, 0x00040047, 0x0000001a, 0x0000000b, 0x0000000c, 0x00030047,
0x0000001a, 0x0000000f, 0x00020013, 0x00000002, 0x00030021, 0x00000003,
0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040015, 0x00000007,
0x00000020, 0x00000000, 0x0004002b, 0x00000007, 0x00000008, 0x00000004,
0x0004001c, 0x00000009, 0x00000006, 0x00000008, 0x00040020, 0x0000000a,
0x00000003, 0x00000009, 0x0004003b, 0x0000000a, 0x0000000b, 0x00000003,
0x00040015, 0x0000000c, 0x00000020, 0x00000001, 0x0004002b, 0x0000000c,
0x0000000d, 0x00000000, 0x0004002b, 0x00000006, 0x0000000e, 0x3f800000,
0x00040020, 0x0000000f, 0x00000003, 0x00000006, 0x0004002b, 0x0000000c,
0x00000011, 0x00000001, 0x0004002b, 0x0000000c, 0x00000013, 0x00000002,
0x0004002b, 0x0000000c, 0x00000015, 0x00000003, 0x0004002b, 0x00000007,
0x00000017, 0x00000002, 0x0004001c, 0x00000018, 0x00000006, 0x00000017,
0x00040020, 0x00000019, 0x00000003, 0x00000018, 0x0004003b, 0x00000019,
0x0000001a, 0x00000003, 0x00050036, 0x00000002, 0x00000004, 0x00000000,
0x00000003, 0x000200f8, 0x00000005, 0x00050041, 0x0000000f, 0x00000010,
0x0000000b, 0x0000000d, 0x0003003e, 0x00000010, 0x0000000e, 0x00050041,
0x0000000f, 0x00000012, 0x0000000b, 0x00000011, 0x0003003e, 0x00000012,
0x0000000e, 0x00050041, 0x0000000f, 0x00000014, 0x0000000b, 0x00000013,
0x0003003e, 0x00000014, 0x0000000e, 0x00050041, 0x0000000f, 0x00000016,
0x0000000b, 0x00000015, 0x0003003e, 0x00000016, 0x0000000e, 0x00050041,
0x0000000f, 0x0000001b, 0x0000001a, 0x0000000d, 0x0003003e, 0x0000001b,
0x0000000e, 0x00050041, 0x0000000f, 0x0000001c, 0x0000001a, 0x00000011,
0x0003003e, 0x0000001c, 0x0000000e, 0x000100fd, 0x00010038,
0x07230203, 0x00010300, 0x0008000b, 0x0000001d, 0x00000000, 0x00020011, 0x00000003, 0x0006000b, 0x00000001,
0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000001,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x0000001a, 0x00040010, 0x00000004, 0x0000001a, 0x00000001,
0x00030003, 0x00000002, 0x000001c2, 0x00040005, 0x00000004, 0x6e69616d, 0x00000000, 0x00070005, 0x0000000b,
0x545f6c67, 0x4c737365, 0x6c657665, 0x6574754f, 0x00000072, 0x00070005, 0x0000001a, 0x545f6c67, 0x4c737365,
0x6c657665, 0x656e6e49, 0x00000072, 0x00040047, 0x0000000b, 0x0000000b, 0x0000000b, 0x00030047, 0x0000000b,
0x0000000f, 0x00040047, 0x0000001a, 0x0000000b, 0x0000000c, 0x00030047, 0x0000001a, 0x0000000f, 0x00020013,
0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040015, 0x00000007,
0x00000020, 0x00000000, 0x0004002b, 0x00000007, 0x00000008, 0x00000004, 0x0004001c, 0x00000009, 0x00000006,
0x00000008, 0x00040020, 0x0000000a, 0x00000003, 0x00000009, 0x0004003b, 0x0000000a, 0x0000000b, 0x00000003,
0x00040015, 0x0000000c, 0x00000020, 0x00000001, 0x0004002b, 0x0000000c, 0x0000000d, 0x00000000, 0x0004002b,
0x00000006, 0x0000000e, 0x3f800000, 0x00040020, 0x0000000f, 0x00000003, 0x00000006, 0x0004002b, 0x0000000c,
0x00000011, 0x00000001, 0x0004002b, 0x0000000c, 0x00000013, 0x00000002, 0x0004002b, 0x0000000c, 0x00000015,
0x00000003, 0x0004002b, 0x00000007, 0x00000017, 0x00000002, 0x0004001c, 0x00000018, 0x00000006, 0x00000017,
0x00040020, 0x00000019, 0x00000003, 0x00000018, 0x0004003b, 0x00000019, 0x0000001a, 0x00000003, 0x00050036,
0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x00050041, 0x0000000f, 0x00000010,
0x0000000b, 0x0000000d, 0x0003003e, 0x00000010, 0x0000000e, 0x00050041, 0x0000000f, 0x00000012, 0x0000000b,
0x00000011, 0x0003003e, 0x00000012, 0x0000000e, 0x00050041, 0x0000000f, 0x00000014, 0x0000000b, 0x00000013,
0x0003003e, 0x00000014, 0x0000000e, 0x00050041, 0x0000000f, 0x00000016, 0x0000000b, 0x00000015, 0x0003003e,
0x00000016, 0x0000000e, 0x00050041, 0x0000000f, 0x0000001b, 0x0000001a, 0x0000000d, 0x0003003e, 0x0000001b,
0x0000000e, 0x00050041, 0x0000000f, 0x0000001c, 0x0000001a, 0x00000011, 0x0003003e, 0x0000001c, 0x0000000e,
0x000100fd, 0x00010038,
};
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeTescSpvWordCount = sizeof(kPrimitivesGeneratedNoXfbProbeTescSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeTescSpv[0]);
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeTescSpvWordCount =
sizeof(kPrimitivesGeneratedNoXfbProbeTescSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeTescSpv[0]);
inline constexpr std::uint32_t kPrimitivesGeneratedNoXfbProbeTeseSpv[] = {
0x07230203, 0x00010300, 0x0008000b, 0x00000021, 0x00000000, 0x00020011,
0x00000003, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000002,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x00000012, 0x00030010,
0x00000004, 0x00000016, 0x00030010, 0x00000004, 0x00000001, 0x00030010,
0x00000004, 0x00000004, 0x00030003, 0x00000002, 0x000001c2, 0x00040005,
0x00000004, 0x6e69616d, 0x00000000, 0x00060005, 0x0000000b, 0x505f6c67,
0x65567265, 0x78657472, 0x00000000, 0x00060006, 0x0000000b, 0x00000000,
0x505f6c67, 0x7469736f, 0x006e6f69, 0x00070006, 0x0000000b, 0x00000001,
0x505f6c67, 0x746e696f, 0x657a6953, 0x00000000, 0x00070006, 0x0000000b,
0x00000002, 0x435f6c67, 0x4470696c, 0x61747369, 0x0065636e, 0x00070006,
0x0000000b, 0x00000003, 0x435f6c67, 0x446c6c75, 0x61747369, 0x0065636e,
0x00030005, 0x0000000d, 0x00000000, 0x00060005, 0x00000012, 0x545f6c67,
0x43737365, 0x64726f6f, 0x00000000, 0x00030047, 0x0000000b, 0x00000002,
0x00050048, 0x0000000b, 0x00000000, 0x0000000b, 0x00000000, 0x00050048,
0x0000000b, 0x00000001, 0x0000000b, 0x00000001, 0x00050048, 0x0000000b,
0x00000002, 0x0000000b, 0x00000003, 0x00050048, 0x0000000b, 0x00000003,
0x0000000b, 0x00000004, 0x00040047, 0x00000012, 0x0000000b, 0x0000000d,
0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016,
0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004,
0x00040015, 0x00000008, 0x00000020, 0x00000000, 0x0004002b, 0x00000008,
0x00000009, 0x00000001, 0x0004001c, 0x0000000a, 0x00000006, 0x00000009,
0x0006001e, 0x0000000b, 0x00000007, 0x00000006, 0x0000000a, 0x0000000a,
0x00040020, 0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c,
0x0000000d, 0x00000003, 0x00040015, 0x0000000e, 0x00000020, 0x00000001,
0x0004002b, 0x0000000e, 0x0000000f, 0x00000000, 0x00040017, 0x00000010,
0x00000006, 0x00000003, 0x00040020, 0x00000011, 0x00000001, 0x00000010,
0x0004003b, 0x00000011, 0x00000012, 0x00000001, 0x00040017, 0x00000013,
0x00000006, 0x00000002, 0x0004002b, 0x00000006, 0x00000016, 0x40000000,
0x0004002b, 0x00000006, 0x00000018, 0x3f800000, 0x0004002b, 0x00000006,
0x0000001b, 0x00000000, 0x00040020, 0x0000001f, 0x00000003, 0x00000007,
0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8,
0x00000005, 0x0004003d, 0x00000010, 0x00000014, 0x00000012, 0x0007004f,
0x00000013, 0x00000015, 0x00000014, 0x00000014, 0x00000000, 0x00000001,
0x0005008e, 0x00000013, 0x00000017, 0x00000015, 0x00000016, 0x00050050,
0x00000013, 0x00000019, 0x00000018, 0x00000018, 0x00050083, 0x00000013,
0x0000001a, 0x00000017, 0x00000019, 0x00050051, 0x00000006, 0x0000001c,
0x0000001a, 0x00000000, 0x00050051, 0x00000006, 0x0000001d, 0x0000001a,
0x00000001, 0x00070050, 0x00000007, 0x0000001e, 0x0000001c, 0x0000001d,
0x0000001b, 0x00000018, 0x00050041, 0x0000001f, 0x00000020, 0x0000000d,
0x0000000f, 0x0003003e, 0x00000020, 0x0000001e, 0x000100fd, 0x00010038,
0x07230203, 0x00010300, 0x0008000b, 0x00000021, 0x00000000, 0x00020011, 0x00000003, 0x0006000b, 0x00000001,
0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000002,
0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x00000012, 0x00030010, 0x00000004, 0x00000016, 0x00030010,
0x00000004, 0x00000001, 0x00030010, 0x00000004, 0x00000004, 0x00030003, 0x00000002, 0x000001c2, 0x00040005,
0x00000004, 0x6e69616d, 0x00000000, 0x00060005, 0x0000000b, 0x505f6c67, 0x65567265, 0x78657472, 0x00000000,
0x00060006, 0x0000000b, 0x00000000, 0x505f6c67, 0x7469736f, 0x006e6f69, 0x00070006, 0x0000000b, 0x00000001,
0x505f6c67, 0x746e696f, 0x657a6953, 0x00000000, 0x00070006, 0x0000000b, 0x00000002, 0x435f6c67, 0x4470696c,
0x61747369, 0x0065636e, 0x00070006, 0x0000000b, 0x00000003, 0x435f6c67, 0x446c6c75, 0x61747369, 0x0065636e,
0x00030005, 0x0000000d, 0x00000000, 0x00060005, 0x00000012, 0x545f6c67, 0x43737365, 0x64726f6f, 0x00000000,
0x00030047, 0x0000000b, 0x00000002, 0x00050048, 0x0000000b, 0x00000000, 0x0000000b, 0x00000000, 0x00050048,
0x0000000b, 0x00000001, 0x0000000b, 0x00000001, 0x00050048, 0x0000000b, 0x00000002, 0x0000000b, 0x00000003,
0x00050048, 0x0000000b, 0x00000003, 0x0000000b, 0x00000004, 0x00040047, 0x00000012, 0x0000000b, 0x0000000d,
0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017,
0x00000007, 0x00000006, 0x00000004, 0x00040015, 0x00000008, 0x00000020, 0x00000000, 0x0004002b, 0x00000008,
0x00000009, 0x00000001, 0x0004001c, 0x0000000a, 0x00000006, 0x00000009, 0x0006001e, 0x0000000b, 0x00000007,
0x00000006, 0x0000000a, 0x0000000a, 0x00040020, 0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c,
0x0000000d, 0x00000003, 0x00040015, 0x0000000e, 0x00000020, 0x00000001, 0x0004002b, 0x0000000e, 0x0000000f,
0x00000000, 0x00040017, 0x00000010, 0x00000006, 0x00000003, 0x00040020, 0x00000011, 0x00000001, 0x00000010,
0x0004003b, 0x00000011, 0x00000012, 0x00000001, 0x00040017, 0x00000013, 0x00000006, 0x00000002, 0x0004002b,
0x00000006, 0x00000016, 0x40000000, 0x0004002b, 0x00000006, 0x00000018, 0x3f800000, 0x0004002b, 0x00000006,
0x0000001b, 0x00000000, 0x00040020, 0x0000001f, 0x00000003, 0x00000007, 0x00050036, 0x00000002, 0x00000004,
0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003d, 0x00000010, 0x00000014, 0x00000012, 0x0007004f,
0x00000013, 0x00000015, 0x00000014, 0x00000014, 0x00000000, 0x00000001, 0x0005008e, 0x00000013, 0x00000017,
0x00000015, 0x00000016, 0x00050050, 0x00000013, 0x00000019, 0x00000018, 0x00000018, 0x00050083, 0x00000013,
0x0000001a, 0x00000017, 0x00000019, 0x00050051, 0x00000006, 0x0000001c, 0x0000001a, 0x00000000, 0x00050051,
0x00000006, 0x0000001d, 0x0000001a, 0x00000001, 0x00070050, 0x00000007, 0x0000001e, 0x0000001c, 0x0000001d,
0x0000001b, 0x00000018, 0x00050041, 0x0000001f, 0x00000020, 0x0000000d, 0x0000000f, 0x0003003e, 0x00000020,
0x0000001e, 0x000100fd, 0x00010038,
};
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeTeseSpvWordCount = sizeof(kPrimitivesGeneratedNoXfbProbeTeseSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeTeseSpv[0]);
inline constexpr std::size_t kPrimitivesGeneratedNoXfbProbeTeseSpvWordCount =
sizeof(kPrimitivesGeneratedNoXfbProbeTeseSpv) / sizeof(kPrimitivesGeneratedNoXfbProbeTeseSpv[0]);
} // namespace MobileGL::MG_Util::SelfTest
@@ -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) {}
+35 -20
View File
@@ -15,7 +15,8 @@ namespace MobileGL::MG_Util::EtcDecoder {
// GL 4.6 core C.2 reads every block as a big-endian integer; bit 63 is the MSB of byte 0.
Uint64 ReadBigEndian64(const Uint8* bytes) {
Uint64 value = 0;
for (Int i = 0; i < 8; ++i) value = (value << 8) | bytes[i];
for (Int i = 0; i < 8; ++i)
value = (value << 8) | bytes[i];
return value;
}
@@ -23,25 +24,35 @@ namespace MobileGL::MG_Util::EtcDecoder {
return static_cast<Uint32>((word >> low) & ((Uint64{1} << (high - low + 1)) - 1));
}
Uint8 Clamp255(Int value) { return static_cast<Uint8>(std::clamp(value, 0, 255)); }
Int Extend4(Uint32 v) { return static_cast<Int>((v << 4) | v); }
Int Extend5(Uint32 v) { return static_cast<Int>((v << 3) | (v >> 2)); }
Int Extend6(Uint32 v) { return static_cast<Int>((v << 2) | (v >> 4)); }
Int Extend7(Uint32 v) { return static_cast<Int>((v << 1) | (v >> 6)); }
Uint8 Clamp255(Int value) {
return static_cast<Uint8>(std::clamp(value, 0, 255));
}
Int Extend4(Uint32 v) {
return static_cast<Int>((v << 4) | v);
}
Int Extend5(Uint32 v) {
return static_cast<Int>((v << 3) | (v >> 2));
}
Int Extend6(Uint32 v) {
return static_cast<Int>((v << 2) | (v >> 4));
}
Int Extend7(Uint32 v) {
return static_cast<Int>((v << 1) | (v >> 6));
}
// Table C.12: intensity modifiers of the individual/differential modes, indexed by the
// codeword; a texel's (msb, lsb) picks +a, +b, -a, -b.
constexpr Int kIntensityModifiers[8][2] = {{2, 8}, {5, 17}, {9, 29}, {13, 42},
constexpr Int kIntensityModifiers[8][2] = {{2, 8}, {5, 17}, {9, 29}, {13, 42},
{18, 60}, {24, 80}, {33, 106}, {47, 183}};
// Table C.14: the T and H mode distances.
constexpr Int kDistances[8] = {3, 6, 11, 16, 23, 32, 41, 64};
// Table C.16: the EAC modifier tables (alpha, R11 and RG11 share them).
constexpr Int kEacModifiers[16][8] = {
{-3, -6, -9, -15, 2, 5, 8, 14}, {-3, -7, -10, -13, 2, 6, 9, 12}, {-2, -5, -8, -13, 1, 4, 7, 12},
{-2, -4, -6, -13, 1, 3, 5, 12}, {-3, -6, -8, -12, 2, 5, 7, 11}, {-3, -7, -9, -11, 2, 6, 8, 10},
{-4, -7, -8, -11, 3, 6, 7, 10}, {-3, -5, -8, -11, 2, 4, 7, 10}, {-2, -6, -8, -10, 1, 5, 7, 9},
{-2, -5, -8, -10, 1, 4, 7, 9}, {-2, -4, -8, -10, 1, 3, 7, 9}, {-2, -5, -7, -10, 1, 4, 6, 9},
{-3, -4, -7, -10, 2, 3, 6, 9}, {-1, -2, -3, -10, 0, 1, 2, 9}, {-4, -6, -8, -9, 3, 5, 7, 8},
{-3, -6, -9, -15, 2, 5, 8, 14}, {-3, -7, -10, -13, 2, 6, 9, 12}, {-2, -5, -8, -13, 1, 4, 7, 12},
{-2, -4, -6, -13, 1, 3, 5, 12}, {-3, -6, -8, -12, 2, 5, 7, 11}, {-3, -7, -9, -11, 2, 6, 8, 10},
{-4, -7, -8, -11, 3, 6, 7, 10}, {-3, -5, -8, -11, 2, 4, 7, 10}, {-2, -6, -8, -10, 1, 5, 7, 9},
{-2, -5, -8, -10, 1, 4, 7, 9}, {-2, -4, -8, -10, 1, 3, 7, 9}, {-2, -5, -7, -10, 1, 4, 6, 9},
{-3, -4, -7, -10, 2, 3, 6, 9}, {-1, -2, -3, -10, 0, 1, 2, 9}, {-4, -6, -8, -9, 3, 5, 7, 8},
{-3, -5, -7, -9, 2, 4, 6, 8}};
// One ETC2 RGB block (with the punch-through alpha variant) into 16 RGBA texels,
@@ -152,10 +163,9 @@ namespace MobileGL::MG_Util::EtcDecoder {
}
if (b2 < 0 || b2 > 31) {
// Planar mode: always opaque, an origin and two gradients.
const Int o[3] = {Extend6(Bits(word, 62, 57)),
Extend7((Bits(word, 56, 56) << 6) | Bits(word, 54, 49)),
Extend6((Bits(word, 48, 48) << 5) | (Bits(word, 44, 43) << 3) |
Bits(word, 41, 39))};
const Int o[3] = {
Extend6(Bits(word, 62, 57)), Extend7((Bits(word, 56, 56) << 6) | Bits(word, 54, 49)),
Extend6((Bits(word, 48, 48) << 5) | (Bits(word, 44, 43) << 3) | Bits(word, 41, 39))};
const Int h[3] = {Extend6((Bits(word, 38, 34) << 1) | Bits(word, 32, 32)),
Extend7(Bits(word, 31, 25)), Extend6(Bits(word, 24, 19))};
const Int v[3] = {Extend6(Bits(word, 18, 13)), Extend7(Bits(word, 12, 6)),
@@ -261,7 +271,9 @@ namespace MobileGL::MG_Util::EtcDecoder {
}
} // namespace
Bool IsEtcFormat(GLenum compressedFormat) { return DecodedTexelBytes(compressedFormat) != 0; }
Bool IsEtcFormat(GLenum compressedFormat) {
return DecodedTexelBytes(compressedFormat) != 0;
}
SizeT DecodedTexelBytes(GLenum compressedFormat) {
switch (compressedFormat) {
@@ -290,12 +302,14 @@ namespace MobileGL::MG_Util::EtcDecoder {
case GL_COMPRESSED_RGB8_ETC2:
case GL_COMPRESSED_SRGB8_ETC2:
DecodeEtc2Rgb(block, false, rgba);
for (Int t = 0; t < 16; ++t) std::copy(rgba[t], rgba[t] + 3, out + t * 3);
for (Int t = 0; t < 16; ++t)
std::copy(rgba[t], rgba[t] + 3, out + t * 3);
return true;
case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
DecodeEtc2Rgb(block, true, rgba);
for (Int t = 0; t < 16; ++t) std::copy(rgba[t], rgba[t] + 4, out + t * 4);
for (Int t = 0; t < 16; ++t)
std::copy(rgba[t], rgba[t] + 4, out + t * 4);
return true;
case GL_COMPRESSED_RGBA8_ETC2_EAC:
case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC: {
@@ -313,7 +327,8 @@ namespace MobileGL::MG_Util::EtcDecoder {
const Bool isSigned = compressedFormat == GL_COMPRESSED_SIGNED_R11_EAC;
Int red[16];
DecodeEac11(block, isSigned, red);
for (Int t = 0; t < 16; ++t) out[t] = Eac11ToByte(red[t], isSigned);
for (Int t = 0; t < 16; ++t)
out[t] = Eac11ToByte(red[t], isSigned);
return true;
}
case GL_COMPRESSED_RG11_EAC:
+275 -110
View File
@@ -145,83 +145,187 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
return true;
case TextureInternalFormat::R8:
case TextureInternalFormat::Red: out = {1, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::Red:
out = {1, ShadowComponent::UNorm8, false};
return true;
case TextureInternalFormat::RG8:
case TextureInternalFormat::RG: out = {2, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::RG:
out = {2, ShadowComponent::UNorm8, false};
return true;
case TextureInternalFormat::RGB8:
case TextureInternalFormat::RGB:
case TextureInternalFormat::SRGB8: out = {3, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::SRGB8:
out = {3, ShadowComponent::UNorm8, false};
return true;
case TextureInternalFormat::RGBA8:
case TextureInternalFormat::RGBA:
case TextureInternalFormat::SRGB8Alpha8: out = {4, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::SRGB8Alpha8:
out = {4, ShadowComponent::UNorm8, false};
return true;
// Legacy desktop-GL sized normalized formats are stored in the closest ES-legal layout
// (see TextureFormatProcessor::NormalizePixelFormat): 8-bit unorm for <=8-bit channels,
// 16-bit unorm for 10/12-bit channels.
case TextureInternalFormat::R3G3B2:
case TextureInternalFormat::RGB4:
case TextureInternalFormat::RGB5: out = {3, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::RGB5:
out = {3, ShadowComponent::UNorm8, false};
return true;
case TextureInternalFormat::RGBA2:
case TextureInternalFormat::RGBA4:
case TextureInternalFormat::RGB5A1: out = {4, ShadowComponent::UNorm8, false}; return true;
case TextureInternalFormat::RGB5A1:
out = {4, ShadowComponent::UNorm8, false};
return true;
case TextureInternalFormat::RGB10:
case TextureInternalFormat::RGB12: out = {3, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::RGBA12: out = {4, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::RGB12:
out = {3, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::RGBA12:
out = {4, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::R8Snorm: out = {1, ShadowComponent::SNorm8, false}; return true;
case TextureInternalFormat::RG8Snorm: out = {2, ShadowComponent::SNorm8, false}; return true;
case TextureInternalFormat::RGB8Snorm: out = {3, ShadowComponent::SNorm8, false}; return true;
case TextureInternalFormat::RGBA8Snorm: out = {4, ShadowComponent::SNorm8, false}; return true;
case TextureInternalFormat::R8Snorm:
out = {1, ShadowComponent::SNorm8, false};
return true;
case TextureInternalFormat::RG8Snorm:
out = {2, ShadowComponent::SNorm8, false};
return true;
case TextureInternalFormat::RGB8Snorm:
out = {3, ShadowComponent::SNorm8, false};
return true;
case TextureInternalFormat::RGBA8Snorm:
out = {4, ShadowComponent::SNorm8, false};
return true;
case TextureInternalFormat::R16: out = {1, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::RG16: out = {2, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::RGB16: out = {3, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::RGBA16: out = {4, ShadowComponent::UNorm16, false}; return true;
case TextureInternalFormat::R16:
out = {1, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::RG16:
out = {2, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::RGB16:
out = {3, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::RGBA16:
out = {4, ShadowComponent::UNorm16, false};
return true;
case TextureInternalFormat::R16Snorm: out = {1, ShadowComponent::SNorm16, false}; return true;
case TextureInternalFormat::RG16Snorm: out = {2, ShadowComponent::SNorm16, false}; return true;
case TextureInternalFormat::RGB16Snorm: out = {3, ShadowComponent::SNorm16, false}; return true;
case TextureInternalFormat::RGBA16Snorm: out = {4, ShadowComponent::SNorm16, false}; return true;
case TextureInternalFormat::R16Snorm:
out = {1, ShadowComponent::SNorm16, false};
return true;
case TextureInternalFormat::RG16Snorm:
out = {2, ShadowComponent::SNorm16, false};
return true;
case TextureInternalFormat::RGB16Snorm:
out = {3, ShadowComponent::SNorm16, false};
return true;
case TextureInternalFormat::RGBA16Snorm:
out = {4, ShadowComponent::SNorm16, false};
return true;
case TextureInternalFormat::R16F: out = {1, ShadowComponent::Half, false}; return true;
case TextureInternalFormat::RG16F: out = {2, ShadowComponent::Half, false}; return true;
case TextureInternalFormat::RGB16F: out = {3, ShadowComponent::Half, false}; return true;
case TextureInternalFormat::RGBA16F: out = {4, ShadowComponent::Half, false}; return true;
case TextureInternalFormat::R16F:
out = {1, ShadowComponent::Half, false};
return true;
case TextureInternalFormat::RG16F:
out = {2, ShadowComponent::Half, false};
return true;
case TextureInternalFormat::RGB16F:
out = {3, ShadowComponent::Half, false};
return true;
case TextureInternalFormat::RGBA16F:
out = {4, ShadowComponent::Half, false};
return true;
case TextureInternalFormat::R32F: out = {1, ShadowComponent::Float32, false}; return true;
case TextureInternalFormat::RG32F: out = {2, ShadowComponent::Float32, false}; return true;
case TextureInternalFormat::RGB32F: out = {3, ShadowComponent::Float32, false}; return true;
case TextureInternalFormat::RGBA32F: out = {4, ShadowComponent::Float32, false}; return true;
case TextureInternalFormat::R32F:
out = {1, ShadowComponent::Float32, false};
return true;
case TextureInternalFormat::RG32F:
out = {2, ShadowComponent::Float32, false};
return true;
case TextureInternalFormat::RGB32F:
out = {3, ShadowComponent::Float32, false};
return true;
case TextureInternalFormat::RGBA32F:
out = {4, ShadowComponent::Float32, false};
return true;
case TextureInternalFormat::R8UI: out = {1, ShadowComponent::UInt8, true}; return true;
case TextureInternalFormat::RG8UI: out = {2, ShadowComponent::UInt8, true}; return true;
case TextureInternalFormat::RGB8UI: out = {3, ShadowComponent::UInt8, true}; return true;
case TextureInternalFormat::RGBA8UI: out = {4, ShadowComponent::UInt8, true}; return true;
case TextureInternalFormat::R8UI:
out = {1, ShadowComponent::UInt8, true};
return true;
case TextureInternalFormat::RG8UI:
out = {2, ShadowComponent::UInt8, true};
return true;
case TextureInternalFormat::RGB8UI:
out = {3, ShadowComponent::UInt8, true};
return true;
case TextureInternalFormat::RGBA8UI:
out = {4, ShadowComponent::UInt8, true};
return true;
case TextureInternalFormat::R8I: out = {1, ShadowComponent::Int8, true}; return true;
case TextureInternalFormat::RG8I: out = {2, ShadowComponent::Int8, true}; return true;
case TextureInternalFormat::RGB8I: out = {3, ShadowComponent::Int8, true}; return true;
case TextureInternalFormat::RGBA8I: out = {4, ShadowComponent::Int8, true}; return true;
case TextureInternalFormat::R8I:
out = {1, ShadowComponent::Int8, true};
return true;
case TextureInternalFormat::RG8I:
out = {2, ShadowComponent::Int8, true};
return true;
case TextureInternalFormat::RGB8I:
out = {3, ShadowComponent::Int8, true};
return true;
case TextureInternalFormat::RGBA8I:
out = {4, ShadowComponent::Int8, true};
return true;
case TextureInternalFormat::R16UI: out = {1, ShadowComponent::UInt16, true}; return true;
case TextureInternalFormat::RG16UI: out = {2, ShadowComponent::UInt16, true}; return true;
case TextureInternalFormat::RGB16UI: out = {3, ShadowComponent::UInt16, true}; return true;
case TextureInternalFormat::RGBA16UI: out = {4, ShadowComponent::UInt16, true}; return true;
case TextureInternalFormat::R16UI:
out = {1, ShadowComponent::UInt16, true};
return true;
case TextureInternalFormat::RG16UI:
out = {2, ShadowComponent::UInt16, true};
return true;
case TextureInternalFormat::RGB16UI:
out = {3, ShadowComponent::UInt16, true};
return true;
case TextureInternalFormat::RGBA16UI:
out = {4, ShadowComponent::UInt16, true};
return true;
case TextureInternalFormat::R16I: out = {1, ShadowComponent::Int16, true}; return true;
case TextureInternalFormat::RG16I: out = {2, ShadowComponent::Int16, true}; return true;
case TextureInternalFormat::RGB16I: out = {3, ShadowComponent::Int16, true}; return true;
case TextureInternalFormat::RGBA16I: out = {4, ShadowComponent::Int16, true}; return true;
case TextureInternalFormat::R16I:
out = {1, ShadowComponent::Int16, true};
return true;
case TextureInternalFormat::RG16I:
out = {2, ShadowComponent::Int16, true};
return true;
case TextureInternalFormat::RGB16I:
out = {3, ShadowComponent::Int16, true};
return true;
case TextureInternalFormat::RGBA16I:
out = {4, ShadowComponent::Int16, true};
return true;
case TextureInternalFormat::R32UI: out = {1, ShadowComponent::UInt32, true}; return true;
case TextureInternalFormat::RG32UI: out = {2, ShadowComponent::UInt32, true}; return true;
case TextureInternalFormat::RGB32UI: out = {3, ShadowComponent::UInt32, true}; return true;
case TextureInternalFormat::RGBA32UI: out = {4, ShadowComponent::UInt32, true}; return true;
case TextureInternalFormat::R32UI:
out = {1, ShadowComponent::UInt32, true};
return true;
case TextureInternalFormat::RG32UI:
out = {2, ShadowComponent::UInt32, true};
return true;
case TextureInternalFormat::RGB32UI:
out = {3, ShadowComponent::UInt32, true};
return true;
case TextureInternalFormat::RGBA32UI:
out = {4, ShadowComponent::UInt32, true};
return true;
case TextureInternalFormat::R32I: out = {1, ShadowComponent::Int32, true}; return true;
case TextureInternalFormat::RG32I: out = {2, ShadowComponent::Int32, true}; return true;
case TextureInternalFormat::RGB32I: out = {3, ShadowComponent::Int32, true}; return true;
case TextureInternalFormat::RGBA32I: out = {4, ShadowComponent::Int32, true}; return true;
case TextureInternalFormat::R32I:
out = {1, ShadowComponent::Int32, true};
return true;
case TextureInternalFormat::RG32I:
out = {2, ShadowComponent::Int32, true};
return true;
case TextureInternalFormat::RGB32I:
out = {3, ShadowComponent::Int32, true};
return true;
case TextureInternalFormat::RGBA32I:
out = {4, ShadowComponent::Int32, true};
return true;
default:
// Packed internal layouts (RGB10A2, RGB9E5, ...), depth/stencil and unsized formats
@@ -266,14 +370,12 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
// The one client (format, type) pair whose word is bit-identical to the packed internal
// word, if any. Everything else has to go through the decode/encode conversion.
Bool IsRawPackedPixelPair(PackedInternalKind kind, TextureInputFormat format,
TexturePixelDataType type) {
Bool IsRawPackedPixelPair(PackedInternalKind kind, TextureInputFormat format, TexturePixelDataType type) {
switch (kind) {
case PackedInternalKind::UNorm2101010Rev:
return format == TextureInputFormat::RGBA && type == TexturePixelDataType::UnsignedInt2101010Rev;
case PackedInternalKind::UInt2101010Rev:
return format == TextureInputFormat::RGBAInteger &&
type == TexturePixelDataType::UnsignedInt2101010Rev;
return format == TextureInputFormat::RGBAInteger && type == TexturePixelDataType::UnsignedInt2101010Rev;
case PackedInternalKind::FloatR11G11B10:
return format == TextureInputFormat::RGB && type == TexturePixelDataType::UnsignedInt101111Rev;
case PackedInternalKind::FloatRGB9E5:
@@ -321,31 +423,71 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
Bool GetUnpackChannelMapping(TextureInputFormat format, UnpackChannelMapping& out) {
switch (format) {
case TextureInputFormat::Red: out = {{0, -1, -1, -1}, 1, false}; return true;
case TextureInputFormat::RInteger: out = {{0, -1, -1, -1}, 1, true}; return true;
case TextureInputFormat::Green: out = {{-1, 0, -1, -1}, 1, false}; return true;
case TextureInputFormat::GreenInteger: out = {{-1, 0, -1, -1}, 1, true}; return true;
case TextureInputFormat::Blue: out = {{-1, -1, 0, -1}, 1, false}; return true;
case TextureInputFormat::BlueInteger: out = {{-1, -1, 0, -1}, 1, true}; return true;
case TextureInputFormat::Alpha: out = {{-1, -1, -1, 0}, 1, false}; return true;
case TextureInputFormat::AlphaInteger: out = {{-1, -1, -1, 0}, 1, true}; return true;
case TextureInputFormat::RG: out = {{0, 1, -1, -1}, 2, false}; return true;
case TextureInputFormat::RGInteger: out = {{0, 1, -1, -1}, 2, true}; return true;
case TextureInputFormat::RGB: out = {{0, 1, 2, -1}, 3, false}; return true;
case TextureInputFormat::RGBInteger: out = {{0, 1, 2, -1}, 3, true}; return true;
case TextureInputFormat::BGR: out = {{2, 1, 0, -1}, 3, false}; return true;
case TextureInputFormat::BGRInteger: out = {{2, 1, 0, -1}, 3, true}; return true;
case TextureInputFormat::RGBA: out = {{0, 1, 2, 3}, 4, false}; return true;
case TextureInputFormat::RGBAInteger: out = {{0, 1, 2, 3}, 4, true}; return true;
case TextureInputFormat::BGRA: out = {{2, 1, 0, 3}, 4, false}; return true;
case TextureInputFormat::BGRAInteger: out = {{2, 1, 0, 3}, 4, true}; return true;
case TextureInputFormat::Red:
out = {{0, -1, -1, -1}, 1, false};
return true;
case TextureInputFormat::RInteger:
out = {{0, -1, -1, -1}, 1, true};
return true;
case TextureInputFormat::Green:
out = {{-1, 0, -1, -1}, 1, false};
return true;
case TextureInputFormat::GreenInteger:
out = {{-1, 0, -1, -1}, 1, true};
return true;
case TextureInputFormat::Blue:
out = {{-1, -1, 0, -1}, 1, false};
return true;
case TextureInputFormat::BlueInteger:
out = {{-1, -1, 0, -1}, 1, true};
return true;
case TextureInputFormat::Alpha:
out = {{-1, -1, -1, 0}, 1, false};
return true;
case TextureInputFormat::AlphaInteger:
out = {{-1, -1, -1, 0}, 1, true};
return true;
case TextureInputFormat::RG:
out = {{0, 1, -1, -1}, 2, false};
return true;
case TextureInputFormat::RGInteger:
out = {{0, 1, -1, -1}, 2, true};
return true;
case TextureInputFormat::RGB:
out = {{0, 1, 2, -1}, 3, false};
return true;
case TextureInputFormat::RGBInteger:
out = {{0, 1, 2, -1}, 3, true};
return true;
case TextureInputFormat::BGR:
out = {{2, 1, 0, -1}, 3, false};
return true;
case TextureInputFormat::BGRInteger:
out = {{2, 1, 0, -1}, 3, true};
return true;
case TextureInputFormat::RGBA:
out = {{0, 1, 2, 3}, 4, false};
return true;
case TextureInputFormat::RGBAInteger:
out = {{0, 1, 2, 3}, 4, true};
return true;
case TextureInputFormat::BGRA:
out = {{2, 1, 0, 3}, 4, false};
return true;
case TextureInputFormat::BGRAInteger:
out = {{2, 1, 0, 3}, 4, true};
return true;
// A depth value converts like a single normalized/float channel.
case TextureInputFormat::DepthComponent: out = {{0, -1, -1, -1}, 1, false}; return true;
case TextureInputFormat::DepthComponent:
out = {{0, -1, -1, -1}, 1, false};
return true;
// A stencil index is a single INTEGER channel (GL 4.6 core 8.4.4.3). Without this the
// upload fell to the raw-memcpy branch, which copies the client element width into the
// one-byte STENCIL_INDEX8 shadow verbatim - right for GL_UNSIGNED_BYTE and wrong for
// every wider type. The state layer keeps this paired with stencil-only storage.
case TextureInputFormat::StencilIndex: out = {{0, -1, -1, -1}, 1, true}; return true;
case TextureInputFormat::StencilIndex:
out = {{0, -1, -1, -1}, 1, true};
return true;
default:
return false; // packed depth-stencil / unknown
}
@@ -360,18 +502,42 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
Bool GetPackedTypeLayout(TexturePixelDataType type, PackedTypeLayout& out) {
switch (type) {
case TexturePixelDataType::UnsignedByte332: out = {3, {3, 3, 2, 0}, 8, false}; return true;
case TexturePixelDataType::UnsignedByte233Rev: out = {3, {3, 3, 2, 0}, 8, true}; return true;
case TexturePixelDataType::UnsignedShort565: out = {3, {5, 6, 5, 0}, 16, false}; return true;
case TexturePixelDataType::UnsignedShort565Rev: out = {3, {5, 6, 5, 0}, 16, true}; return true;
case TexturePixelDataType::UnsignedShort4444: out = {4, {4, 4, 4, 4}, 16, false}; return true;
case TexturePixelDataType::UnsignedShort4444Rev: out = {4, {4, 4, 4, 4}, 16, true}; return true;
case TexturePixelDataType::UnsignedShort5551: out = {4, {5, 5, 5, 1}, 16, false}; return true;
case TexturePixelDataType::UnsignedShort1555Rev: out = {4, {5, 5, 5, 1}, 16, true}; return true;
case TexturePixelDataType::UnsignedInt8888: out = {4, {8, 8, 8, 8}, 32, false}; return true;
case TexturePixelDataType::UnsignedInt8888Rev: out = {4, {8, 8, 8, 8}, 32, true}; return true;
case TexturePixelDataType::UnsignedInt1010102: out = {4, {10, 10, 10, 2}, 32, false}; return true;
case TexturePixelDataType::UnsignedInt2101010Rev: out = {4, {10, 10, 10, 2}, 32, true}; return true;
case TexturePixelDataType::UnsignedByte332:
out = {3, {3, 3, 2, 0}, 8, false};
return true;
case TexturePixelDataType::UnsignedByte233Rev:
out = {3, {3, 3, 2, 0}, 8, true};
return true;
case TexturePixelDataType::UnsignedShort565:
out = {3, {5, 6, 5, 0}, 16, false};
return true;
case TexturePixelDataType::UnsignedShort565Rev:
out = {3, {5, 6, 5, 0}, 16, true};
return true;
case TexturePixelDataType::UnsignedShort4444:
out = {4, {4, 4, 4, 4}, 16, false};
return true;
case TexturePixelDataType::UnsignedShort4444Rev:
out = {4, {4, 4, 4, 4}, 16, true};
return true;
case TexturePixelDataType::UnsignedShort5551:
out = {4, {5, 5, 5, 1}, 16, false};
return true;
case TexturePixelDataType::UnsignedShort1555Rev:
out = {4, {5, 5, 5, 1}, 16, true};
return true;
case TexturePixelDataType::UnsignedInt8888:
out = {4, {8, 8, 8, 8}, 32, false};
return true;
case TexturePixelDataType::UnsignedInt8888Rev:
out = {4, {8, 8, 8, 8}, 32, true};
return true;
case TexturePixelDataType::UnsignedInt1010102:
out = {4, {10, 10, 10, 2}, 32, false};
return true;
case TexturePixelDataType::UnsignedInt2101010Rev:
out = {4, {10, 10, 10, 2}, 32, true};
return true;
default:
return false; // shared-exponent / packed-float / depth-stencil types stay on the legacy path
}
@@ -541,8 +707,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
out.isPacked = isPacked;
out.type = type;
out.inputPixelSize = GetInputBytesPerPixel(format, type);
const Bool isPackedFloatWord = type == TexturePixelDataType::UnsignedInt5999Rev ||
type == TexturePixelDataType::UnsignedInt101111Rev;
const Bool isPackedFloatWord =
type == TexturePixelDataType::UnsignedInt5999Rev || type == TexturePixelDataType::UnsignedInt101111Rev;
out.swapGroupSize = isPacked ? static_cast<SizeT>(packed.totalBits / 8)
: (isPackedFloatWord ? 4 : GetBaseTexturePixelDataTypeSize(type));
out.internalIsPacked = hasPackedInternal;
@@ -652,10 +818,12 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
Int shift;
if (packed.reversed) {
shift = 0;
for (Int i = 0; i < position; ++i) shift += packed.width[i];
for (Int i = 0; i < position; ++i)
shift += packed.width[i];
} else {
shift = packed.totalBits;
for (Int i = 0; i <= position; ++i) shift -= packed.width[i];
for (Int i = 0; i <= position; ++i)
shift -= packed.width[i];
}
outWidth = packed.width[position];
const Uint32 mask = (1u << outWidth) - 1u;
@@ -693,8 +861,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
Memcpy(dst, &v, sizeof(v));
break;
case ShadowComponent::UNorm32: {
const auto out = static_cast<Uint32>(
std::llround(static_cast<double>(std::clamp(v, 0.0f, 1.0f)) * 4294967295.0));
const auto out =
static_cast<Uint32>(std::llround(static_cast<double>(std::clamp(v, 0.0f, 1.0f)) * 4294967295.0));
Memcpy(dst, &out, sizeof(out));
break;
}
@@ -769,8 +937,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
continue;
}
for (Int ch = 0; ch < conv.internal.channelCount; ++ch) {
EncodeShadowComponentInt(d + static_cast<SizeT>(ch) * dstComponentSize,
conv.internal.component, rgba[ch]);
EncodeShadowComponentInt(d + static_cast<SizeT>(ch) * dstComponentSize, conv.internal.component,
rgba[ch]);
}
} else {
Float rgba[4] = {0.0f, 0.0f, 0.0f, 1.0f};
@@ -873,8 +1041,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
// format (the stencil half becomes zero); the generic channel converter cannot
// express the packed shadow words, so convert here.
const Bool packedDepthStencilInternal = targetInternalFormat == TextureInternalFormat::Depth24Stencil8 ||
targetInternalFormat == TextureInternalFormat::DepthStencil ||
targetInternalFormat == TextureInternalFormat::Depth32FStencil8;
targetInternalFormat == TextureInternalFormat::DepthStencil ||
targetInternalFormat == TextureInternalFormat::Depth32FStencil8;
if (!isBitmap && packedDepthStencilInternal && textureInputFormat == TextureInputFormat::DepthComponent &&
(inputDataType == TexturePixelDataType::Float || inputDataType == TexturePixelDataType::UnsignedInt ||
inputDataType == TexturePixelDataType::UnsignedShort)) {
@@ -886,7 +1054,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
outSize = 0;
return nullptr;
}
const Uint8* srcBase = static_cast<const Uint8*>(inputPixels) +
const Uint8* srcBase =
static_cast<const Uint8*>(inputPixels) +
static_cast<SizeT>(params.SkipImages) * static_cast<SizeT>(effectiveHeight) * inputRowStride +
static_cast<SizeT>(params.SkipRows) * inputRowStride +
static_cast<SizeT>(params.SkipPixels) * pixelSize;
@@ -894,8 +1063,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
for (Int z = 0; z < std::max(depth, 1); ++z) {
for (Int y = 0; y < height; ++y) {
const Uint8* srcRow = srcBase +
static_cast<SizeT>(z) * static_cast<SizeT>(effectiveHeight) * inputRowStride +
static_cast<SizeT>(y) * inputRowStride;
static_cast<SizeT>(z) * static_cast<SizeT>(effectiveHeight) * inputRowStride +
static_cast<SizeT>(y) * inputRowStride;
for (Int x = 0; x < width; ++x) {
Float depthValue = 0.0f;
if (inputDataType == TexturePixelDataType::Float) {
@@ -996,8 +1165,7 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
if (swapGroup == 0) swapGroup = GetBaseTexturePixelDataTypeSize(inputDataType);
if (swapGroup > 1) {
MGLOG_D("%s: SwapBytes (group %d)", __func__, static_cast<Int>(swapGroup));
SwapBytes(layerDst, swapGroup,
static_cast<SizeT>(copyWidth) * pixelSize / swapGroup);
SwapBytes(layerDst, swapGroup, static_cast<SizeT>(copyWidth) * pixelSize / swapGroup);
}
}
@@ -1018,10 +1186,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
return outputPixels;
}
Bool ConvertOnePixelToInternal(TextureInternalFormat targetInternalFormat,
TextureInputFormat textureInputFormat,
TexturePixelDataType inputDataType,
const void* inputPixel,
Bool ConvertOnePixelToInternal(TextureInternalFormat targetInternalFormat, TextureInputFormat textureInputFormat,
TexturePixelDataType inputDataType, const void* inputPixel,
Vector<Uint8>& outputPixel) {
outputPixel.clear();
// A stencil index became a transferable format when STENCIL_INDEX8 texture storage did (see
@@ -1034,9 +1200,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
PixelStoreParameters params{};
params.Alignment = 1;
SizeT convertedSize = 0;
void* converted = ProcessTexturePixelsDataUnpack(
inputPixel, params, targetInternalFormat, textureInputFormat, inputDataType, {1, 1, 1}, false,
convertedSize);
void* converted = ProcessTexturePixelsDataUnpack(inputPixel, params, targetInternalFormat, textureInputFormat,
inputDataType, {1, 1, 1}, false, convertedSize);
const SizeT expectedSize = MG_Util::GetSizedInternalFormatSizeInBytes(targetInternalFormat);
if (converted == nullptr || convertedSize != expectedSize || expectedSize == 0) {
if (converted != nullptr) free(converted);
@@ -21,10 +21,8 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
TextureInternalFormat srcInternalFormat, TexturePixelDataType srcDataType,
TextureInputFormat dstInputFormat, TexturePixelDataType dstDataType,
IntVec3 dimension, Bool isBitmap, SizeT& outSize);
Bool ConvertOnePixelToInternal(TextureInternalFormat targetInternalFormat,
TextureInputFormat textureInputFormat,
TexturePixelDataType inputDataType,
const void* inputPixel,
Bool ConvertOnePixelToInternal(TextureInternalFormat targetInternalFormat, TextureInputFormat textureInputFormat,
TexturePixelDataType inputDataType, const void* inputPixel,
Vector<Uint8>& outputPixel);
void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector<TextureSwizzleParam>& swizzle);
@@ -10,9 +10,8 @@
#include "MG_Util/Converters/GLToStr/GLEnumConverter.h"
namespace MobileGL::MG_Util::TextureFormatProcessor {
Flags<PixelFormatNormalizeOptionBit>
GetApplicablePixelFormatNormalizeOptions(GLenum internalFormat,
Flags<PixelFormatNormalizeOptionBit> options) {
Flags<PixelFormatNormalizeOptionBit> GetApplicablePixelFormatNormalizeOptions(
GLenum internalFormat, Flags<PixelFormatNormalizeOptionBit> options) {
Flags<PixelFormatNormalizeOptionBit> applicableOptions;
switch (internalFormat) {
case GL_DEPTH_COMPONENT32:
@@ -384,8 +383,7 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
*outInternalFormat = GL_RGBA8;
break;
case GL_RGBA12:
*outInternalFormat =
(options & PixelFormatNormalizeOptionBit::NoNorm16) ? GL_RGBA32F : GL_RGBA16;
*outInternalFormat = (options & PixelFormatNormalizeOptionBit::NoNorm16) ? GL_RGBA32F : GL_RGBA16;
break;
default:
*outInternalFormat = internalFormat;
@@ -566,7 +564,7 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
default:
MGLOG_E_ONCE("NormalizePixelFormat: outFormat: unhandled internalFormat: %s",
MG_Util::ConvertGLEnumToString(internalFormat).c_str());
MG_Util::ConvertGLEnumToString(internalFormat).c_str());
// Fallback handling for other formats
// Try to infer format from internal format name
if (strstr(MG_Util::ConvertGLEnumToString(internalFormat).c_str(), "RGBA") != nullptr) {
@@ -621,8 +619,7 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
case GL_RG16_SNORM:
case GL_R16_SNORM:
if ((options & PixelFormatNormalizeOptionBit::NoNorm16) ||
(internalFormat == GL_RGB16_SNORM &&
(options & PixelFormatNormalizeOptionBit::NoRGB16Snorm)) ||
(internalFormat == GL_RGB16_SNORM && (options & PixelFormatNormalizeOptionBit::NoRGB16Snorm)) ||
(options & PixelFormatNormalizeOptionBit::NoSnorm16) ||
(options & PixelFormatNormalizeOptionBit::NoSnorm16RenderTarget)) {
*outType = GL_FLOAT;
@@ -798,7 +795,7 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
default:
MGLOG_E_ONCE("NormalizePixelFormat: outType: unhandled internalFormat: %s",
MG_Util::ConvertGLEnumToString(internalFormat).c_str());
MG_Util::ConvertGLEnumToString(internalFormat).c_str());
// Fallback handling for other formats
*outType = GL_UNSIGNED_BYTE;
break;
@@ -60,9 +60,8 @@ namespace MobileGL {
None = 0,
};
namespace MG_Util::TextureFormatProcessor {
Flags<PixelFormatNormalizeOptionBit>
GetApplicablePixelFormatNormalizeOptions(GLenum internalFormat,
Flags<PixelFormatNormalizeOptionBit> options);
Flags<PixelFormatNormalizeOptionBit> GetApplicablePixelFormatNormalizeOptions(
GLenum internalFormat, Flags<PixelFormatNormalizeOptionBit> options);
void NormalizePixelFormat(GLenum internalFormat, Flags<PixelFormatNormalizeOptionBit> options,
GLenum* outInternalFormat, GLenum* outFormat, GLenum* outType);
} // namespace MG_Util::TextureFormatProcessor
+8 -5
View File
@@ -38,7 +38,8 @@ namespace MobileGL::MG_Util::X11 {
Int32 ToNumber(const char* begin, const char* end) {
if (!AllDigits(begin, end) || end - begin > 6) return -1;
Int32 value = 0;
for (const char* p = begin; p != end; ++p) value = value * 10 + (*p - '0');
for (const char* p = begin; p != end; ++p)
value = value * 10 + (*p - '0');
return value;
}
@@ -66,7 +67,8 @@ namespace MobileGL::MG_Util::X11 {
if (std::strncmp(path, kSocketDirectory, dirLength) == 0) {
const char* digits = path + dirLength;
const char* end = digits;
while (*end >= '0' && *end <= '9') ++end;
while (*end >= '0' && *end <= '9')
++end;
return ToNumber(digits, end);
}
// [protocol/][host]:display[.screen]: the LAST colon (an IPv6 host has colons of its own).
@@ -74,15 +76,16 @@ namespace MobileGL::MG_Util::X11 {
if (colon == nullptr) return -1;
const char* digits = colon + 1;
const char* end = digits;
while (*end >= '0' && *end <= '9') ++end;
while (*end >= '0' && *end <= '9')
++end;
if (*end != '\0' && *end != '.') return -1;
(void)length;
return ToNumber(digits, end);
}
Bool IsXServerProgram(const String& basename) {
static const char* const kServers[] = {"Xwayland", "Xorg", "Xorg.bin", "X", "Xvfb", "Xephyr",
"Xnest", "Xvnc", "Xwin", "XWin", "Xquartz", "Xfake"};
static const char* const kServers[] = {"Xwayland", "Xorg", "Xorg.bin", "X", "Xvfb", "Xephyr",
"Xnest", "Xvnc", "Xwin", "XWin", "Xquartz", "Xfake"};
for (const char* server : kServers) {
if (basename == server) return true;
}