DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (27a812186ff4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
From: Jeff Gilbert <jgilbert@mozilla.com>
Bug 771406 - Add emulation for GLSL faceforward() to ANGLE - r=bjacob

diff --git a/gfx/angle/src/compiler/BuiltInFunctionEmulator.cpp b/gfx/angle/src/compiler/BuiltInFunctionEmulator.cpp
--- a/gfx/angle/src/compiler/BuiltInFunctionEmulator.cpp
+++ b/gfx/angle/src/compiler/BuiltInFunctionEmulator.cpp
@@ -26,16 +26,22 @@ const char* kFunctionEmulationVertexSour
     "#error no emulation for distance(vec3, vec3)",
     "#error no emulation for distance(vec4, vec4)",
 
     "#define webgl_dot_emu(x, y) ((x) * (y))",
     "#error no emulation for dot(vec2, vec2)",
     "#error no emulation for dot(vec3, vec3)",
     "#error no emulation for dot(vec4, vec4)",
 
+    // |faceforward(N, I, Nref)| is |dot(NRef, I) < 0 ? N : -N|
+    "#define webgl_faceforward_emu(N, I, Nref) (((Nref) * (I) < 0.0) ? (N) : -(N))",
+    "#error no emulation for faceforward(vec2, vec2, vec2)",
+    "#error no emulation for faceforward(vec3, vec3, vec3)",
+    "#error no emulation for faceforward(vec4, vec4, vec4)",
+
     "#define webgl_length_emu(x) ((x) >= 0.0 ? (x) : -(x))",
     "#error no emulation for length(vec2)",
     "#error no emulation for length(vec3)",
     "#error no emulation for length(vec4)",
 
     "#define webgl_normalize_emu(x) ((x) == 0.0 ? 0.0 : ((x) > 0.0 ? 1.0 : -1.0))",
     "#error no emulation for normalize(vec2)",
     "#error no emulation for normalize(vec3)",
@@ -58,16 +64,22 @@ const char* kFunctionEmulationFragmentSo
     "#error no emulation for distance(vec3, vec3)",
     "#error no emulation for distance(vec4, vec4)",
 
     "#define webgl_dot_emu(x, y) ((x) * (y))",
     "#error no emulation for dot(vec2, vec2)",
     "#error no emulation for dot(vec3, vec3)",
     "#error no emulation for dot(vec4, vec4)",
 
+    // |faceforward(N, I, Nref)| is |dot(NRef, I) < 0 ? N : -N|
+    "#define webgl_faceforward_emu(N, I, Nref) (((Nref) * (I) < 0.0) ? (N) : -(N))",
+    "#error no emulation for faceforward(vec2, vec2, vec2)",
+    "#error no emulation for faceforward(vec3, vec3, vec3)",
+    "#error no emulation for faceforward(vec4, vec4, vec4)",
+
     "#define webgl_length_emu(x) ((x) >= 0.0 ? (x) : -(x))",
     "#error no emulation for length(vec2)",
     "#error no emulation for length(vec3)",
     "#error no emulation for length(vec4)",
 
     "#define webgl_normalize_emu(x) ((x) == 0.0 ? 0.0 : ((x) > 0.0 ? 1.0 : -1.0))",
     "#error no emulation for normalize(vec2)",
     "#error no emulation for normalize(vec3)",
@@ -89,16 +101,20 @@ const bool kFunctionEmulationVertexMask[
     true,  // TFunctionDistance1_1
     false, // TFunctionDistance2_2
     false, // TFunctionDistance3_3
     false, // TFunctionDistance4_4
     true,  // TFunctionDot1_1
     false, // TFunctionDot2_2
     false, // TFunctionDot3_3
     false, // TFunctionDot4_4
+    true,  // TFunctionFaceForward1_1_1
+    false, // TFunctionFaceForward2_2_2
+    false, // TFunctionFaceForward3_3_3
+    false, // TFunctionFaceForward4_4_4
     true,  // TFunctionLength1
     false, // TFunctionLength2
     false, // TFunctionLength3
     false, // TFunctionLength4
     true,  // TFunctionNormalize1
     false, // TFunctionNormalize2
     false, // TFunctionNormalize3
     false, // TFunctionNormalize4
@@ -115,16 +131,20 @@ const bool kFunctionEmulationVertexMask[
     false, // TFunctionDistance1_1
     false, // TFunctionDistance2_2
     false, // TFunctionDistance3_3
     false, // TFunctionDistance4_4
     false, // TFunctionDot1_1
     false, // TFunctionDot2_2
     false, // TFunctionDot3_3
     false, // TFunctionDot4_4
+    false, // TFunctionFaceForward1_1_1
+    false, // TFunctionFaceForward2_2_2
+    false, // TFunctionFaceForward3_3_3
+    false, // TFunctionFaceForward4_4_4
     false, // TFunctionLength1
     false, // TFunctionLength2
     false, // TFunctionLength3
     false, // TFunctionLength4
     false, // TFunctionNormalize1
     false, // TFunctionNormalize2
     false, // TFunctionNormalize3
     false, // TFunctionNormalize4
@@ -146,16 +166,20 @@ const bool kFunctionEmulationFragmentMas
     true,  // TFunctionDistance1_1
     false, // TFunctionDistance2_2
     false, // TFunctionDistance3_3
     false, // TFunctionDistance4_4
     true,  // TFunctionDot1_1
     false, // TFunctionDot2_2
     false, // TFunctionDot3_3
     false, // TFunctionDot4_4
+    true,  // TFunctionFaceForward1_1_1
+    false, // TFunctionFaceForward2_2_2
+    false, // TFunctionFaceForward3_3_3
+    false, // TFunctionFaceForward4_4_4
     true,  // TFunctionLength1
     false, // TFunctionLength2
     false, // TFunctionLength3
     false, // TFunctionLength4
     true,  // TFunctionNormalize1
     false, // TFunctionNormalize2
     false, // TFunctionNormalize3
     false, // TFunctionNormalize4
@@ -172,16 +196,20 @@ const bool kFunctionEmulationFragmentMas
     false, // TFunctionDistance1_1
     false, // TFunctionDistance2_2
     false, // TFunctionDistance3_3
     false, // TFunctionDistance4_4
     false, // TFunctionDot1_1
     false, // TFunctionDot2_2
     false, // TFunctionDot3_3
     false, // TFunctionDot4_4
+    false, // TFunctionFaceForward1_1_1
+    false, // TFunctionFaceForward2_2_2
+    false, // TFunctionFaceForward3_3_3
+    false, // TFunctionFaceForward4_4_4
     false, // TFunctionLength1
     false, // TFunctionLength2
     false, // TFunctionLength3
     false, // TFunctionLength4
     false, // TFunctionNormalize1
     false, // TFunctionNormalize2
     false, // TFunctionNormalize3
     false, // TFunctionNormalize4
@@ -239,25 +267,37 @@ public:
                 case EOpReflect:
                 case EOpRefract:
                 case EOpMul:
                     break;
                 default:
                     return true;
             };
             const TIntermSequence& sequence = node->getSequence();
-            // Right now we only handle built-in functions with two parameters.
-            if (sequence.size() != 2)
+            bool needToEmulate = false;
+
+            if (sequence.size() == 2) {
+                TIntermTyped* param1 = sequence[0]->getAsTyped();
+                TIntermTyped* param2 = sequence[1]->getAsTyped();
+                if (!param1 || !param2)
+                    return true;
+                needToEmulate = mEmulator.SetFunctionCalled(
+                    node->getOp(), param1->getType(), param2->getType());
+            } else if (sequence.size() == 3) {
+                TIntermTyped* param1 = sequence[0]->getAsTyped();
+                TIntermTyped* param2 = sequence[1]->getAsTyped();
+                TIntermTyped* param3 = sequence[2]->getAsTyped();
+                if (!param1 || !param2 || !param3)
+                    return true;
+                needToEmulate = mEmulator.SetFunctionCalled(
+                    node->getOp(), param1->getType(), param2->getType(), param3->getType());
+            } else {
                 return true;
-            TIntermTyped* param1 = sequence[0]->getAsTyped();
-            TIntermTyped* param2 = sequence[1]->getAsTyped();
-            if (!param1 || !param2)
-                return true;
-            bool needToEmulate = mEmulator.SetFunctionCalled(
-                node->getOp(), param1->getType(), param2->getType());
+            }
+
             if (needToEmulate)
                 node->setUseEmulatedFunction();
         }
         return true;
     }
 
 private:
     BuiltInFunctionEmulator& mEmulator;
@@ -286,16 +326,23 @@ bool BuiltInFunctionEmulator::SetFunctio
 bool BuiltInFunctionEmulator::SetFunctionCalled(
     TOperator op, const TType& param1, const TType& param2)
 {
     TBuiltInFunction function = IdentifyFunction(op, param1, param2);
     return SetFunctionCalled(function);
 }
 
 bool BuiltInFunctionEmulator::SetFunctionCalled(
+    TOperator op, const TType& param1, const TType& param2, const TType& param3)
+{
+    TBuiltInFunction function = IdentifyFunction(op, param1, param2, param3);
+    return SetFunctionCalled(function);
+}
+
+bool BuiltInFunctionEmulator::SetFunctionCalled(
     BuiltInFunctionEmulator::TBuiltInFunction function) {
     if (function == TFunctionUnknown || mFunctionMask[function] == false)
         return false;
     for (size_t i = 0; i < mFunctions.size(); ++i) {
         if (mFunctions[i] == function)
             return true;
     }
     mFunctions.push_back(function);
@@ -377,16 +424,44 @@ BuiltInFunctionEmulator::IdentifyFunctio
     }
     if (function == TFunctionUnknown)
         return TFunctionUnknown;
     if (param1.isVector())
         function += param1.getNominalSize() - 1;
     return static_cast<TBuiltInFunction>(function);
 }
 
+BuiltInFunctionEmulator::TBuiltInFunction
+BuiltInFunctionEmulator::IdentifyFunction(
+    TOperator op, const TType& param1, const TType& param2, const TType& param3)
+{
+    // Check that all params have the same type, length,
+    // and that they're not too large.
+    if (param1.isVector() != param2.isVector() ||
+        param2.isVector() != param3.isVector() ||
+        param1.getNominalSize() != param2.getNominalSize() ||
+        param2.getNominalSize() != param3.getNominalSize() ||
+        param1.getNominalSize() > 4)
+        return TFunctionUnknown;
+
+    unsigned int function = TFunctionUnknown;
+    switch (op) {
+        case EOpFaceForward:
+            function = TFunctionFaceForward1_1_1;
+            break;
+        default:
+            break;
+    }
+    if (function == TFunctionUnknown)
+        return TFunctionUnknown;
+    if (param1.isVector())
+        function += param1.getNominalSize() - 1;
+    return static_cast<TBuiltInFunction>(function);
+}
+
 void BuiltInFunctionEmulator::MarkBuiltInFunctionsForEmulation(
     TIntermNode* root)
 {
     ASSERT(root);
 
     BuiltInFunctionEmulationMarker marker(*this);
     root->traverse(&marker);
 }
diff --git a/gfx/angle/src/compiler/BuiltInFunctionEmulator.h b/gfx/angle/src/compiler/BuiltInFunctionEmulator.h
--- a/gfx/angle/src/compiler/BuiltInFunctionEmulator.h
+++ b/gfx/angle/src/compiler/BuiltInFunctionEmulator.h
@@ -23,16 +23,18 @@ public:
     // Records that a function is called by the shader and might needs to be
     // emulated.  If the function's group is not in mFunctionGroupFilter, this
     // becomes an no-op.
     // Returns true if the function call needs to be replaced with an emulated
     // one.
     bool SetFunctionCalled(TOperator op, const TType& param);
     bool SetFunctionCalled(
         TOperator op, const TType& param1, const TType& param2);
+    bool SetFunctionCalled(
+        TOperator op, const TType& param1, const TType& param2, const TType& param3);
 
     // Output function emulation definition.  This should be before any other
     // shader source.
     void OutputEmulatedFunctionDefinition(TInfoSinkBase& out, bool withPrecision) const;
 
     void MarkBuiltInFunctionsForEmulation(TIntermNode* root);
 
     void Cleanup();
@@ -55,16 +57,21 @@ private:
         TFunctionDistance3_3,  // vec3 distance(vec3, vec3);
         TFunctionDistance4_4,  // vec4 distance(vec4, vec4);
 
         TFunctionDot1_1,  // float dot(float, float);
         TFunctionDot2_2,  // vec2 dot(vec2, vec2);
         TFunctionDot3_3,  // vec3 dot(vec3, vec3);
         TFunctionDot4_4,  // vec4 dot(vec4, vec4);
 
+        TFunctionFaceForward1_1_1,  // float faceforward(float, float, float);
+        TFunctionFaceForward2_2_2,  // vec2 faceforward(vec2, vec2, vec2);
+        TFunctionFaceForward3_3_3,  // vec3 faceforward(vec3, vec3, vec3);
+        TFunctionFaceForward4_4_4,  // vec4 faceforward(vec4, vec4, vec4);
+
         TFunctionLength1,  // float length(float);
         TFunctionLength2,  // float length(vec2);
         TFunctionLength3,  // float length(vec3);
         TFunctionLength4,  // float length(vec4);
 
         TFunctionNormalize1,  // float normalize(float);
         TFunctionNormalize2,  // vec2 normalize(vec2);
         TFunctionNormalize3,  // vec3 normalize(vec3);
@@ -76,16 +83,18 @@ private:
         TFunctionReflect4_4,  // vec4 reflect(vec4, vec4);
 
         TFunctionUnknown
     };
 
     TBuiltInFunction IdentifyFunction(TOperator op, const TType& param);
     TBuiltInFunction IdentifyFunction(
         TOperator op, const TType& param1, const TType& param2);
+    TBuiltInFunction IdentifyFunction(
+        TOperator op, const TType& param1, const TType& param2, const TType& param3);
 
     bool SetFunctionCalled(TBuiltInFunction function);
 
     std::vector<TBuiltInFunction> mFunctions;
 
     const bool* mFunctionMask;  // a boolean flag for each function.
     const char** mFunctionSource;
 };