From 5317496ddb7e888f8b349ab1002677d51ebd5071 Mon Sep 17 00:00:00 2001 From: Jamie Nicol Date: Wed, 10 Mar 2021 17:03:24 +0000 Subject: [PATCH] Add function to count number of unpacked varying vectors This counts the number of vectors used by each active varying in a shader. This can be used to ensure that a shader will succesfully compile without using more than GL_MAX_VARYING_VECTORS on devices that do not perform spec-compliant packing of varyings. This is in contrast to sh::CheckVariablesWithinPackingLimits(), which checks whether there are enough vectors *with* spec-compliant packing. --- Cargo.toml | 2 +- src/shaders/glslang-c.cpp | 22 ++++++++++++++++++++++ src/shaders/mod.rs | 7 +++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 977a78776..ce34f5fa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mozangle" -version = "0.3.2" +version = "0.3.3" authors = ["The ANGLE Project Authors", "The Servo Project Developers"] license = " BSD-3-Clause" description = "Mozilla’s fork of Google ANGLE, repackaged as a Rust crate " diff --git a/src/shaders/glslang-c.cpp b/src/shaders/glslang-c.cpp index 7a1bb944f..af3a35311 100644 --- a/src/shaders/glslang-c.cpp +++ b/src/shaders/glslang-c.cpp @@ -1,4 +1,5 @@ #include "GLSLANG/ShaderLang.h" +#include "common/utilities.h" extern "C" int GLSLangInitialize() { @@ -91,3 +92,24 @@ void GLSLangIterUniformNameMapping(const ShHandle handle, StrPairFunction each, ); } } + +// Returns the number of vectors that the shader's active varyings fit +// in to without additional packing. Can be used to test whether a +// shader will compile on drivers that do not perform spec-compliant +// packing. This contrasts with sh::CheckVariablesWithinPackingLimits +// which does pack the varyings in accordance with the spec. +extern "C" +int GLSLangGetNumUnpackedVaryingVectors(const ShHandle handle) { + int total_rows = 0; + const std::vector* varyings = sh::GetVaryings(handle); + + if (varyings) { + for (const auto& varying : *varyings) { + if (varying.active) { + total_rows += gl::VariableRowCount(varying.type) * varying.getArraySizeProduct(); + } + } + } + + return total_rows; +} diff --git a/src/shaders/mod.rs b/src/shaders/mod.rs index 07481831a..fd9064613 100644 --- a/src/shaders/mod.rs +++ b/src/shaders/mod.rs @@ -24,6 +24,7 @@ pub mod ffi { each: unsafe extern fn(*mut c_void, *const c_char, usize, *const c_char, usize), closure_each: *mut c_void ); + pub fn GLSLangGetNumUnpackedVaryingVectors(handle: ShHandle) -> c_int; } } @@ -292,6 +293,12 @@ impl ShaderValidator { } closure.map } + + pub fn get_num_unpacked_varying_vectors(&self) -> i32 { + unsafe { + GLSLangGetNumUnpackedVaryingVectors(self.handle) + } + } } impl Drop for ShaderValidator {