mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-21 00:41:57 +09:00
[Feature] (SharedImage): sample NV12/P010 dma-bufs through GL_OES_EGL_image_external on both backends, copying foreign buffers into server YUV images
This commit is contained in:
@@ -51,6 +51,8 @@ namespace MobileGL {
|
||||
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
|
||||
case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
|
||||
return TextureTarget::Texture2DMultisampleArray;
|
||||
case GL_TEXTURE_EXTERNAL_OES:
|
||||
return TextureTarget::External;
|
||||
default:
|
||||
return TextureTarget::Unknown;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace MobileGL {
|
||||
return GL_TEXTURE_RECTANGLE;
|
||||
case TextureTarget::Texture2DMultisampleArray:
|
||||
return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
|
||||
case TextureTarget::External:
|
||||
return GL_TEXTURE_EXTERNAL_OES;
|
||||
default:
|
||||
return GL_UNKNOWN_MGL;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace MobileGL {
|
||||
return "Texture1DArray";
|
||||
case TextureTarget::Texture2DMultisampleArray:
|
||||
return "Texture2DMultisampleArray";
|
||||
case TextureTarget::External:
|
||||
return "External";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ namespace MobileGL {
|
||||
case TextureTarget::Texture1DArray:
|
||||
return VK_IMAGE_VIEW_TYPE_1D_ARRAY;
|
||||
case TextureTarget::TextureRectangle:
|
||||
case TextureTarget::External:
|
||||
return VK_IMAGE_VIEW_TYPE_2D;
|
||||
case TextureTarget::Texture2DMultisampleArray:
|
||||
return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
||||
|
||||
@@ -465,6 +465,30 @@ namespace MobileGL {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Bool PreprocessForInspection(GLenum shaderType, const String& source, String& out) {
|
||||
const EShLanguage lang = MG_Util::ConvertGLEnumToEShLanguage(shaderType);
|
||||
if (lang == EShLanguage::EShLangCount) return false;
|
||||
Bool ok = false;
|
||||
{
|
||||
glslang::TShader shader(lang);
|
||||
const char* strings[] = {source.c_str()};
|
||||
shader.setStrings(strings, 1);
|
||||
const String preamble = String("#undef VULKAN\n") + CollectEsPreambleMacroDefines(source);
|
||||
shader.setPreamble(preamble.c_str());
|
||||
shader.setEnvInput(glslang::EShSourceGlsl, lang, glslang::EShClientVulkan, 450);
|
||||
shader.setEnvClient(glslang::EShClientOpenGL, glslang::EShTargetOpenGL_450);
|
||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
||||
const auto resources = BuildTBuiltInResource(&*GetDefaultCompileEnv());
|
||||
glslang::TShader::ForbidIncluder includer;
|
||||
std::string text;
|
||||
ok = shader.preprocess(&resources, 460, ECoreProfile, false, true, EShMsgDefault, &text, includer);
|
||||
if (ok) out = text;
|
||||
}
|
||||
// The shader's pool is gone with it; leave no dangling thread allocator behind.
|
||||
glslang::SetThreadPoolAllocator(nullptr);
|
||||
return ok;
|
||||
}
|
||||
|
||||
UnorderedMap<String, Int> CollectExplicitUniformLocations(const glslang::TShader& shader) {
|
||||
UnorderedMap<String, Int> locations;
|
||||
const glslang::TIntermediate* intermediate = shader.getIntermediate();
|
||||
|
||||
@@ -666,6 +666,12 @@ namespace MobileGL {
|
||||
// reflection needs those spelled out. A uniform declared in several stages must
|
||||
// agree, which the caller enforces across stages.
|
||||
UnorderedMap<String, Int> CollectExplicitUniformLocations(const glslang::TShader& shader);
|
||||
|
||||
// `source` run through glslang's PREPROCESSOR only (inactive #if branches gone, macros
|
||||
// expanded), under the same preamble a parse would use, into `out`. For questions about
|
||||
// the text a parse actually sees - which of two #if-alternative declarations is live.
|
||||
// False (and `out` untouched) when glslang refuses it.
|
||||
Bool PreprocessForInspection(GLenum shaderType, const String& source, String& out);
|
||||
} // namespace ShaderTranspiler
|
||||
} // namespace MG_Util
|
||||
} // namespace MobileGL
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#include <Config.h>
|
||||
#include <MG_Backend/BackendObjects.h>
|
||||
#include <MG_Util/ShaderTranspiler/CompileEnv.h>
|
||||
#include <MG_Util/Converters/MGToGL/ProgramEnumConverter.h>
|
||||
#include <MG_Util/ShaderTranspiler/ShaderCompiler.h>
|
||||
#include <MG_Util/ShaderTranspiler/Types.h>
|
||||
|
||||
#include "EsslBuiltinFunctionNames.h"
|
||||
@@ -1813,6 +1815,84 @@ namespace {
|
||||
"#define gl_NumSamples mg_NumSamples\n";
|
||||
source.insert(afterVersion.Get(source), shim);
|
||||
}
|
||||
|
||||
// The uniforms `text` declares with the external sampler type, and (into `others`) the names it
|
||||
// declares with any other type - the same name can be both in a source whose #if branches pick
|
||||
// one (a compositor's shader generator does exactly that).
|
||||
void ScanExternalSamplerDeclarations(const MobileGL::String& text, Vector<MobileGL::String>& external,
|
||||
Vector<MobileGL::String>* others) {
|
||||
const Vector<CodeToken> tokens = TokenizeCode(text);
|
||||
for (SizeT i = 0; i < tokens.size(); ++i) {
|
||||
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"))
|
||||
++type;
|
||||
if (type >= tokens.size() || !IsIdentifierToken(tokens[type])) continue;
|
||||
const bool isExternal = tokens[type].text == "samplerExternalOES";
|
||||
Vector<MobileGL::String>* into = isExternal ? &external : others;
|
||||
// `name`, `name[N]`, and comma-separated lists of them, up to the `;` (a block's `{`
|
||||
// ends the scan: its members are not samplers).
|
||||
for (SizeT j = type + 1; j < tokens.size() && tokens[j].text != ";" && tokens[j].text != "{";) {
|
||||
if (IsIdentifierToken(tokens[j])) {
|
||||
if (into != nullptr && std::find(into->begin(), into->end(), tokens[j].text) == into->end())
|
||||
into->push_back(tokens[j].text);
|
||||
++j;
|
||||
if (j < tokens.size() && tokens[j].text == "[") {
|
||||
while (j < tokens.size() && tokens[j].text != "]") ++j;
|
||||
++j;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GL_OES_EGL_image_external's sampler type, which the desktop profile the shader is compiled
|
||||
// under has no texture() overloads for. Below the frontend an external texture IS a 2D one (its
|
||||
// EGLImage arrives converted to RGBA), so the type becomes sampler2D, and the names of the
|
||||
// uniforms declared with it ride in a marker (kExternalSamplerMarkerPrefix) to the reflection,
|
||||
// which reports them as GL_SAMPLER_EXTERNAL_OES and so sends their units to the external binding.
|
||||
// The marker is also what keeps such a shader's caches apart from a sampler2D twin's.
|
||||
//
|
||||
// THE NAMES COME FROM THE PREPROCESSED TEXT. A source may declare one name as samplerExternalOES
|
||||
// in one #if branch and as sampler2D in another (a compositor's shader generator does, per trait); only
|
||||
// the branch the preprocessor keeps says what the uniform is. Should glslang's preprocessor
|
||||
// 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) {
|
||||
if (source.find("samplerExternalOES") == MobileGL::String::npos) return;
|
||||
Vector<MobileGL::String> names;
|
||||
MobileGL::String preprocessed;
|
||||
if (MobileGL::MG_Util::ShaderTranspiler::PreprocessForInspection(
|
||||
MobileGL::MG_Util::ConvertShaderStageToGLEnum(stage), source, preprocessed)) {
|
||||
ScanExternalSamplerDeclarations(preprocessed, names, nullptr);
|
||||
} else {
|
||||
Vector<MobileGL::String> external, others;
|
||||
ScanExternalSamplerDeclarations(source, external, &others);
|
||||
for (const MobileGL::String& name : external) {
|
||||
if (std::find(others.begin(), others.end(), name) == others.end()) names.push_back(name);
|
||||
}
|
||||
}
|
||||
// Every spelling of the type, in any branch: what the preprocessor drops does not matter.
|
||||
Vector<SizeT> typeOffsets;
|
||||
for (const CodeToken& token : TokenizeCode(source)) {
|
||||
if (token.text == "samplerExternalOES") typeOffsets.push_back(token.begin);
|
||||
}
|
||||
if (typeOffsets.empty()) return;
|
||||
for (auto offset = typeOffsets.rbegin(); offset != typeOffsets.rend(); ++offset)
|
||||
source.replace(*offset, std::strlen("samplerExternalOES"), "sampler2D");
|
||||
MobileGL::String marker = kExternalSamplerMarkerPrefix;
|
||||
for (SizeT i = 0; i < names.size(); ++i) {
|
||||
if (i != 0) marker += ',';
|
||||
marker += names[i];
|
||||
}
|
||||
marker += "*/\n";
|
||||
source.insert(afterVersion.Get(source), marker);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace MobileGL {
|
||||
@@ -1870,12 +1950,30 @@ namespace MobileGL {
|
||||
|
||||
RenameBuiltinShadowingFunctions(source);
|
||||
|
||||
LowerExternalSamplers(stage, source, afterVersion);
|
||||
ModernizeLegacyGLSL(stage, source, afterVersion);
|
||||
InjectDepthRangeBuiltinShim(stage, source, afterVersion);
|
||||
InjectNumSamplesBuiltinShim(stage, originalLanguage, source, afterVersion);
|
||||
|
||||
}
|
||||
|
||||
Vector<String> ExternalSamplerUniformNames(const String& preprocessedSource) {
|
||||
Vector<String> names;
|
||||
const SizeT start = preprocessedSource.find(kExternalSamplerMarkerPrefix);
|
||||
if (start == String::npos) return names;
|
||||
const SizeT from = start + std::strlen(kExternalSamplerMarkerPrefix);
|
||||
const SizeT end = preprocessedSource.find("*/", from);
|
||||
if (end == String::npos) return names;
|
||||
SizeT at = from;
|
||||
while (at < end) {
|
||||
SizeT comma = preprocessedSource.find(',', at);
|
||||
if (comma == String::npos || comma > end) comma = end;
|
||||
if (comma > at) names.push_back(preprocessedSource.substr(at, comma - at));
|
||||
at = comma + 1;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
String CollectEsPreambleMacroDefines(const String& preprocessedSource) {
|
||||
const SizeT markerStart = preprocessedSource.find(kEsPreambleMarkerPrefix);
|
||||
if (markerStart == String::npos) return {};
|
||||
|
||||
@@ -61,6 +61,11 @@ namespace MobileGL {
|
||||
// GetNormalizedVersionDirective records why that has not happened.
|
||||
String CollectEsPreambleMacroDefines(const String& preprocessedSource);
|
||||
|
||||
// The uniforms a shader declared as samplerExternalOES (GL_OES_EGL_image_external),
|
||||
// which PreprocessShaderSource turned into sampler2D and named in a marker: the
|
||||
// reflection reports them as GL_SAMPLER_EXTERNAL_OES. Empty for every other shader.
|
||||
Vector<String> ExternalSamplerUniformNames(const String& preprocessedSource);
|
||||
|
||||
// GLSL reserves a few names glslang happily accepts as identifiers ("packed",
|
||||
// "row_major" outside a layout(...) list, the image*Shadow family). Returns the
|
||||
// compile-error text for the first violation, or nullopt for a clean source.
|
||||
|
||||
@@ -13,6 +13,20 @@
|
||||
|
||||
#define GL_UNKNOWN_MGL 0
|
||||
|
||||
// GL_OES_EGL_image_external, which the desktop headers do not carry.
|
||||
#ifndef GL_TEXTURE_EXTERNAL_OES
|
||||
#define GL_TEXTURE_EXTERNAL_OES 0x8D65
|
||||
#endif
|
||||
#ifndef GL_SAMPLER_EXTERNAL_OES
|
||||
#define GL_SAMPLER_EXTERNAL_OES 0x8D66
|
||||
#endif
|
||||
#ifndef GL_TEXTURE_BINDING_EXTERNAL_OES
|
||||
#define GL_TEXTURE_BINDING_EXTERNAL_OES 0x8D67
|
||||
#endif
|
||||
#ifndef GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES
|
||||
#define GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES 0x8D68
|
||||
#endif
|
||||
|
||||
#define THROW_EXCEPTION(msg) throw std::runtime_error(msg)
|
||||
|
||||
#define THROW_UNIMPL_EXCEPTION THROW_EXCEPTION("Unimplemented function called!")
|
||||
|
||||
Reference in New Issue
Block a user