From 506a9b25d6b995adaf071f4f7729e71297476727 Mon Sep 17 00:00:00 2001
From: Kelsey Gilbert <kelsey.gilbert@mozilla.com>
Date: Mon, 12 Sep 2022 21:51:55 +0000
Subject: [PATCH] Bug 1755973 - Try implied arr+[0] in GetUniformIndices.
 r=gfx-reviewers,bradwerth

Differential Revision: https://phabricator.services.mozilla.com/D156622
---
 dom/canvas/ClientWebGLContext.cpp             |  30 ++---
 .../conformance2/programs/00_test_list.txt    |   1 +
 .../programs/get-uniform-indices.html         | 121 ++++++++++++++++++
 dom/canvas/test/webgl-conf/cherry_picks.txt   |  16 +--
 dom/canvas/test/webgl-conf/moz.yaml           |   4 +-
 5 files changed, 146 insertions(+), 26 deletions(-)
 create mode 100644 dom/canvas/test/webgl-conf/checkout/conformance2/programs/get-uniform-indices.html

diff --git a/dom/canvas/ClientWebGLContext.cpp b/dom/canvas/ClientWebGLContext.cpp
index 17c8957d77a5d..a0bb2b070c98e 100644
--- a/dom/canvas/ClientWebGLContext.cpp
+++ b/dom/canvas/ClientWebGLContext.cpp
@@ -6046,23 +6046,21 @@ void ClientWebGLContext::GetUniformIndices(
   const auto& res = GetLinkResult(prog);
   auto ret = nsTArray<GLuint>(uniformNames.Length());
 
-  std::unordered_map<std::string, size_t> retIdByName;
-  retIdByName.reserve(ret.Length());
-
-  for (const auto i : IntegerRange(uniformNames.Length())) {
-    const auto& name = uniformNames[i];
-    auto nameU8 = ToString(NS_ConvertUTF16toUTF8(name));
-    retIdByName.insert({std::move(nameU8), i});
-    ret.AppendElement(LOCAL_GL_INVALID_INDEX);
-  }
-
-  GLuint i = 0;
-  for (const auto& cur : res.active.activeUniforms) {
-    const auto maybeRetId = MaybeFind(retIdByName, cur.name);
-    if (maybeRetId) {
-      ret[*maybeRetId] = i;
+  for (const auto& queriedNameU16 : uniformNames) {
+    const auto queriedName = ToString(NS_ConvertUTF16toUTF8(queriedNameU16));
+    const auto impliedProperArrayQueriedName = queriedName + "[0]";
+
+    GLuint activeId = LOCAL_GL_INVALID_INDEX;
+    for (const auto i : IntegerRange(res.active.activeUniforms.size())) {
+      // O(N^2) ok for small N.
+      const auto& activeInfoForI = res.active.activeUniforms[i];
+      if (queriedName == activeInfoForI.name ||
+          impliedProperArrayQueriedName == activeInfoForI.name) {
+        activeId = i;
+        break;
+      }
     }
-    i += 1;
+    ret.AppendElement(activeId);
   }
 
   retval.SetValue(std::move(ret));
diff --git a/dom/canvas/test/webgl-conf/checkout/conformance2/programs/00_test_list.txt b/dom/canvas/test/webgl-conf/checkout/conformance2/programs/00_test_list.txt
index cfc38addf1c3b..c88d255dabe04 100644
--- a/dom/canvas/test/webgl-conf/checkout/conformance2/programs/00_test_list.txt
+++ b/dom/canvas/test/webgl-conf/checkout/conformance2/programs/00_test_list.txt
@@ -1,3 +1,4 @@
 active-built-in-attribs.html
+--min-version 2.0.1 get-uniform-indices.html
 gl-get-frag-data-location.html
 --min-version 2.0.1 sampler-uniforms.html
diff --git a/dom/canvas/test/webgl-conf/checkout/conformance2/programs/get-uniform-indices.html b/dom/canvas/test/webgl-conf/checkout/conformance2/programs/get-uniform-indices.html
new file mode 100644
index 0000000000000..d42add065c6bc
--- /dev/null
+++ b/dom/canvas/test/webgl-conf/checkout/conformance2/programs/get-uniform-indices.html
@@ -0,0 +1,121 @@
+<!--
+Copyright (c) 2022 The Khronos Group Inc.
+Use of this source code is governed by an MIT-style license that can be
+found in the LICENSE.txt file.
+-->
+
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<link rel="stylesheet" href="../../resources/js-test-style.css"/>
+<script src="../../js/js-test-pre.js"></script>
+<script src="../../js/webgl-test-utils.js"></script>
+</head>
+<body>
+<div id="description"></div>
+<div id="console"></div>
+
+<script>
+"use strict";
+description("This test verifies getUniformIndices behaviors.");
+
+debug("");
+
+var wtu = WebGLTestUtils;
+
+const e_canvas = document.createElement('canvas');
+const gl = e_canvas.getContext('webgl2');
+
+const VSRC = `\
+  #version 300 es
+  precision mediump float;
+  uniform float u_vert_scalar;
+  uniform float u_vert_arr[3];
+  uniform float u_both_scalar;
+  uniform float u_both_arr[3];
+  void main() {
+    gl_Position = vec4(0, 0, 0, 1);
+    gl_Position.r += u_vert_scalar;
+    gl_Position.r += u_vert_arr[1];
+    gl_Position.r += u_both_scalar;
+    gl_Position.r += u_both_arr[1];
+  }
+`;
+
+const FSRC = `\
+  #version 300 es
+  precision mediump float;
+  uniform float u_frag_scalar;
+  uniform float u_frag_arr[3];
+  uniform float u_both_scalar;
+  uniform float u_both_arr[3];
+  out vec4 o_frag_color;
+  void main() {
+    o_frag_color = vec4(0, 0, 0, 1);
+    o_frag_color.r += u_frag_scalar;
+    o_frag_color.r += u_frag_arr[1];
+    o_frag_color.r += u_both_scalar;
+    o_frag_color.r += u_both_arr[1];
+  }
+`;
+
+(() => {
+  if (!gl) {
+    testFailed("WebGL context does not exist");
+    return;
+  }
+
+  window.prog = wtu.setupProgram(gl, [VSRC, FSRC]);
+  if (!prog) {
+    testFailed("Setting up program failed");
+    return;
+  }
+  let err = gl.getError();
+  if (err) throw err;
+
+  const IS_ACTIVE_BY_NAME = {
+    'u_vert_scalar'   : true,
+    'u_vert_scalar[0]': false,
+    'u_vert_arr'      : true,
+    'u_vert_arr[0]'   : true, // Even if the [0] is unused, the name enumerated
+                              // via getActiveUniforms for this array is 'u_vert_arr[0]'.
+    'u_vert_arr[1]'   : false,
+
+    'u_frag_scalar'   : true,
+    'u_frag_scalar[0]': false,
+    'u_frag_arr'      : true,
+    'u_frag_arr[0]'   : true,
+    'u_frag_arr[1]'   : false,
+
+    'u_both_scalar'   : true,
+    'u_both_scalar[0]': false,
+    'u_both_arr'      : true,
+    'u_both_arr[0]'   : true,
+    'u_both_arr[1]'   : false,
+  };
+  const NAMES = Object.keys(IS_ACTIVE_BY_NAME);
+  const active_ids = gl.getUniformIndices(prog, NAMES);
+
+  err = gl.getError();
+  if (err) throw err;
+
+  NAMES.forEach((name, i) => {
+    const active_id_was = active_ids[i];
+    const is_active_expected = IS_ACTIVE_BY_NAME[name];
+    const is_active_was = active_id_was != gl.INVALID_INDEX;
+    expectTrue(is_active_was == is_active_expected,
+               `getUniformIndices([, '${name}' ,]) -> [, ${active_id_was} ,], should be [, ${is_active_expected ? '0<=N<INVALID_INDEX' : 'INVALID_INDEX'} ,]`);
+    if (is_active_was) {
+      const info = gl.getActiveUniform(prog, active_id_was);
+      expectTrue(info.name.startsWith(name), `'${info.name}'.startsWith('${name}')`);
+    }
+  });
+})();
+
+var successfullyParsed = true;
+</script>
+<script src="../../js/js-test-post.js"></script>
+
+</body>
+</html>
diff --git a/dom/canvas/test/webgl-conf/cherry_picks.txt b/dom/canvas/test/webgl-conf/cherry_picks.txt
index 780db727080d5..88bffd64af998 100644
--- a/dom/canvas/test/webgl-conf/cherry_picks.txt
+++ b/dom/canvas/test/webgl-conf/cherry_picks.txt
@@ -1,14 +1,14 @@
 
 Cherries picked
 ================================================================================
-Merge base from: ups/main
+Merge base from: moz/main
 
-commit 4996b40a69857919579a12f828188c9f428c402c
-Author: Alexey Knyazev <3479527+lexaknyazev@users.noreply.github.com>
-Date:   Sat Aug 20 01:53:01 2022 +0400
+commit 92af460e46a82d60140b5a1df1379feb79730d3a
+Author: Kelsey Gilbert <jdashg@gmail.com>
+Date:   Tue Sep 6 15:21:32 2022 -0700
 
-    Allow makeXRCompatible in OffscreenCanvas contexts (#3480)
+    Add test for getUniformIndices.
     
-    Fixes failures of:
-    conformance/offscreencanvas/methods.html
-    conformance/offscreencanvas/methods-worker.html
+    Also test to ensure that it returns the correct id by checking the name from getActiveUniform.
+    
+    Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1755973
diff --git a/dom/canvas/test/webgl-conf/moz.yaml b/dom/canvas/test/webgl-conf/moz.yaml
index 7ebde7dcb24cf..c73aab48574c3 100644
--- a/dom/canvas/test/webgl-conf/moz.yaml
+++ b/dom/canvas/test/webgl-conf/moz.yaml
@@ -13,8 +13,8 @@ origin:
 
   license: MIT
 
-  release: commit 4996b40a69857919579a12f828188c9f428c402c Fri Aug 19 14:53:01 2022 -0700
-  revision: 4996b40a69857919579a12f828188c9f428c402c
+  release: commit 92af460e46a82d60140b5a1df1379feb79730d3a Mon Sep 12 12:00:05 2022 -0700
+  revision: 92af460e46a82d60140b5a1df1379feb79730d3a
 
 
 updatebot:
-- 
GitLab