<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[198588] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/198588">198588</a></dd>
<dt>Author</dt> <dd>bfulgham@apple.com</dd>
<dt>Date</dt> <dd>2016-03-23 12:21:11 -0700 (Wed, 23 Mar 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[WebGL] Non-power-of-two texture optimization
https://bugs.webkit.org/show_bug.cgi?id=118409

Reviewed by Dean Jackson.

Source/WebCore:

Based on a patch by Przemyslaw Szymanski  &lt;p.szymanski3@samsung.com&gt;

This patch optimizes usage of handleNPOTTextures. We do not need to
iterate over each texture unit if no black textures were set. This
optimization provides a few more frames per seconds for certain
draw calls.

Tested by:
(1) Existing tests: webgl/resources/webgl_test_files/conformance/textures/texture-npot.html
(2) New test case: fast/canvas/webgl/texture-alternating-npot.html

* html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::compressedTexImage2D): Use new helper method.
(WebCore::WebGLRenderingContextBase::validateNPOTTextureLevel): Added.
(WebCore::WebGLRenderingContextBase::drawArrays): Only check texture completeness
if a black texture was used.
(WebCore::WebGLRenderingContextBase::drawElements): Ditto.
(WebCore::WebGLRenderingContextBase::texImage2DBase): Use new helper method.
(WebCore::WebGLRenderingContextBase::validateTexFunc): Ditto.
(WebCore::WebGLRenderingContextBase::checkTextureCompleteness): Return flag to indicate
if a black fallbacktexture was used.
* html/canvas/WebGLRenderingContextBase.h:

LayoutTests:

* fast/canvas/webgl/texture-alternating-npot-expected.txt: Added.
* fast/canvas/webgl/texture-alternating-npot.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasWebGLRenderingContextBasecpp">trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasWebGLRenderingContextBaseh">trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfastcanvaswebgltexturealternatingnpotexpectedtxt">trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastcanvaswebgltexturealternatingnpothtml">trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (198587 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-03-23 18:00:43 UTC (rev 198587)
+++ trunk/LayoutTests/ChangeLog        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2016-03-23  Brent Fulgham  &lt;bfulgham@apple.com&gt;
+
+        [WebGL] Non-power-of-two texture optimization
+        https://bugs.webkit.org/show_bug.cgi?id=118409
+
+        Reviewed by Dean Jackson.
+
+        * fast/canvas/webgl/texture-alternating-npot-expected.txt: Added.
+        * fast/canvas/webgl/texture-alternating-npot.html: Added.
+
</ins><span class="cx"> 2016-03-23  Saam barati  &lt;sbarati@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         We should not disable inlining when the debugger is enabled
</span></span></pre></div>
<a id="trunkLayoutTestsfastcanvaswebgltexturealternatingnpotexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot-expected.txt (0 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot-expected.txt        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -0,0 +1,33 @@
</span><ins>+ PASS getError was expected value: NO_ERROR : Should be no errors from setup.
+PASS gl.getError() is gl.NO_ERROR
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D[0] with NPOT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D[1] with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D[2] with NPOT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D[0] with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D[1] with POT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D[2] with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[0] with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[1] with POT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[2] with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[0] with POT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[1] with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D[2] with POT texture at level 0 should succeed
+
+check using cubemap
+PASS gl.getError() is gl.NO_ERROR
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: INVALID_VALUE : gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture with level &gt; 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with NPOT texture at level 0 should succeed
+PASS getError was expected value: NO_ERROR : gl.texImage2D with POT texture at level 0 should succeed
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsfastcanvaswebgltexturealternatingnpothtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot.html (0 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot.html                                (rev 0)
+++ trunk/LayoutTests/fast/canvas/webgl/texture-alternating-npot.html        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -0,0 +1,205 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;WebGL Non-Power of 2 texture conformance test.&lt;/title&gt;
+&lt;script src=&quot;../../../resources/js-test.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;resources/webgl-test.js&quot;&gt; &lt;/script&gt;
+&lt;script src=&quot;resources/webgl-test-utils.js&quot;&gt; &lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;canvas id=&quot;example&quot; width=&quot;4&quot; height=&quot;4&quot; style=&quot;width: 40px; height: 30px;&quot;&gt;&lt;/canvas&gt;
+&lt;div id=&quot;description&quot;&gt;&lt;/div&gt;
+&lt;div id=&quot;console&quot;&gt;&lt;/div&gt;
+&lt;script id=&quot;vshader&quot; type=&quot;x-shader/x-vertex&quot;&gt;
+attribute vec4 vPosition;
+attribute vec2 texCoord0;
+varying vec2 texCoord;
+void main()
+{
+    gl_Position = vPosition;
+    texCoord = texCoord0;
+}
+&lt;/script&gt;
+
+&lt;script id=&quot;fshader&quot; type=&quot;x-shader/x-fragment&quot;&gt;
+#ifdef GL_ES
+precision mediump float;
+#endif
+uniform samplerCube tex;
+varying vec2 texCoord;
+void main()
+{
+    gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
+}
+&lt;/script&gt;
+&lt;script&gt;
+if (window.internals)
+    window.internals.settings.setWebGLErrorsToConsoleEnabled(false);
+
+var wtu = WebGLTestUtils;
+var canvas = document.getElementById(&quot;example&quot;);
+var gl = wtu.create3DContext(canvas);
+var program = wtu.setupTexturedQuad(gl);
+
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;Should be no errors from setup.&quot;);
+
+// Make 3 textures by using 3 active texture units.
+var textures = [];
+for (var ii = 0; ii &lt; 3; ++ii) {
+    var tex = gl.createTexture();
+    gl.activeTexture(gl.TEXTURE0 + ii);
+    gl.bindTexture(gl.TEXTURE_2D, tex);
+    textures[ii] = tex;
+}
+shouldBe(&quot;gl.getError()&quot;, &quot;gl.NO_ERROR&quot;);
+
+// Alternate POT and NPOT textures, ending with NPOT.
+//
+// 1. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D[0] with NPOT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[1] with POT texture with level &gt; 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D[2] with NPOT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+// 2. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[0] with POT texture with level &gt; 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D[1] with POT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[2] with POT texture with level &gt; 0 should succeed&quot;);
+
+// 3. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+wtu.fillTexture(gl, textures[0], 5, 3, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[0] with NPOT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+wtu.fillTexture(gl, textures[1], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[1] with POT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+wtu.fillTexture(gl, textures[2], 5, 3, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[2] with NPOT texture at level 0 should succeed&quot;);
+
+// 4. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+wtu.fillTexture(gl, textures[0], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[0] with POT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+wtu.fillTexture(gl, textures[1], 5, 3, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[1] with NPOT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+wtu.fillTexture(gl, textures[2], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D[2] with POT texture at level 0 should succeed&quot;);
+
+debug(&quot;&quot;);
+debug(&quot;check using cubemap&quot;);
+var program = wtu.setupProgram(
+    gl,
+    [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER),
+     wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)],
+    ['vPosition', 'texCoord0'], [0, 1]);
+
+// Make 3 textures by using 3 active texture units.
+var cubeTextures = [];
+for (var ii = 0; ii &lt; 3; ++ii) {
+    var tex = gl.createTexture();
+    gl.activeTexture(gl.TEXTURE0 + ii);
+    gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
+    cubeTextures[ii] = tex;
+}
+shouldBe(&quot;gl.getError()&quot;, &quot;gl.NO_ERROR&quot;);
+
+// 5. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in NPOT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+fillCubeTexture(gl, cubeTextures[0], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture with level &gt; 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+fillCubeTexture(gl, cubeTextures[2], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+// 6. Check that an NPOT texture not on level 0 generates INVALID_VALUE (alternating, ending in POT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture with level &gt; 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+fillCubeTexture(gl, cubeTextures[1], 5, 3, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.INVALID_VALUE, &quot;gl.texImage2D with NPOT texture with level &gt; 0 should return INVALID_VALUE&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255], 1);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture with level &gt; 0 should succeed&quot;);
+
+// 7. Check that an NPOT texture on level 0 succeeds (alternating, ending in NPOT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+fillCubeTexture(gl, cubeTextures[0], 5, 5, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with NPOT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+fillCubeTexture(gl, cubeTextures[1], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+fillCubeTexture(gl, cubeTextures[2], 5, 5, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with NPOT texture at level 0 should succeed&quot;);
+
+// 8. Check that an NPOT texture on level 0 succeeds (alternating, ending in POT texture)
+gl.activeTexture(gl.TEXTURE0 + 0);
+fillCubeTexture(gl, cubeTextures[0], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 1);
+fillCubeTexture(gl, cubeTextures[1], 5, 5, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with NPOT texture at level 0 should succeed&quot;);
+
+gl.activeTexture(gl.TEXTURE0 + 2);
+fillCubeTexture(gl, cubeTextures[2], 4, 4, [0, 192, 128, 255]);
+glErrorShouldBe(gl, gl.NO_ERROR, &quot;gl.texImage2D with POT texture at level 0 should succeed&quot;);
+
+function fillCubeTexture(gl, tex, width, height, color, opt_level) {
+  opt_level = opt_level || 0;
+  var canvas = document.createElement('canvas');
+  canvas.width = width;
+  canvas.height = height;
+  var ctx2d = canvas.getContext('2d');
+  ctx2d.fillStyle = &quot;rgba(&quot; + color[0] + &quot;,&quot; + color[1] + &quot;,&quot; + color[2] + &quot;,&quot; + color[3] + &quot;)&quot;;
+  ctx2d.fillRect(0, 0, width, height);
+  gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
+  var targets = [
+    gl.TEXTURE_CUBE_MAP_POSITIVE_X,
+    gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
+    gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
+    gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
+    gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
+    gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
+  for (var tt = 0; tt &lt; targets.length; ++tt) {
+    gl.texImage2D(
+        targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
+  }
+};
+
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
+
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (198587 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-03-23 18:00:43 UTC (rev 198587)
+++ trunk/Source/WebCore/ChangeLog        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -1,3 +1,33 @@
</span><ins>+2016-03-23  Brent Fulgham  &lt;bfulgham@apple.com&gt;
+
+        [WebGL] Non-power-of-two texture optimization
+        https://bugs.webkit.org/show_bug.cgi?id=118409
+
+        Reviewed by Dean Jackson.
+
+        Based on a patch by Przemyslaw Szymanski  &lt;p.szymanski3@samsung.com&gt;

+        This patch optimizes usage of handleNPOTTextures. We do not need to
+        iterate over each texture unit if no black textures were set. This
+        optimization provides a few more frames per seconds for certain
+        draw calls.
+    
+        Tested by:
+        (1) Existing tests: webgl/resources/webgl_test_files/conformance/textures/texture-npot.html
+        (2) New test case: fast/canvas/webgl/texture-alternating-npot.html
+
+        * html/canvas/WebGLRenderingContextBase.cpp:
+        (WebCore::WebGLRenderingContextBase::compressedTexImage2D): Use new helper method.
+        (WebCore::WebGLRenderingContextBase::validateNPOTTextureLevel): Added.
+        (WebCore::WebGLRenderingContextBase::drawArrays): Only check texture completeness
+        if a black texture was used.
+        (WebCore::WebGLRenderingContextBase::drawElements): Ditto.
+        (WebCore::WebGLRenderingContextBase::texImage2DBase): Use new helper method.
+        (WebCore::WebGLRenderingContextBase::validateTexFunc): Ditto.
+        (WebCore::WebGLRenderingContextBase::checkTextureCompleteness): Return flag to indicate
+        if a black fallbacktexture was used.
+        * html/canvas/WebGLRenderingContextBase.h:
+
</ins><span class="cx"> 2016-03-23  Alexey Proskuryakov  &lt;ap@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Build fix for a new warning.
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasWebGLRenderingContextBasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp (198587 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp        2016-03-23 18:00:43 UTC (rev 198587)
+++ trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -1268,12 +1268,8 @@
</span><span class="cx">     WebGLTexture* tex = validateTextureBinding(&quot;compressedTexImage2D&quot;, target, true);
</span><span class="cx">     if (!tex)
</span><span class="cx">         return;
</span><del>-    if (!isGLES2NPOTStrict()) {
-        if (level &amp;&amp; WebGLTexture::isNPOT(width, height)) {
-            synthesizeGLError(GraphicsContext3D::INVALID_VALUE, &quot;compressedTexImage2D&quot;, &quot;level &gt; 0 not power of 2&quot;);
-            return;
-        }
-    }
</del><ins>+    if (!validateNPOTTextureLevel(width, height, level, &quot;compressedTexImage2D&quot;))
+        return;
</ins><span class="cx">     m_context-&gt;moveErrorsToSyntheticErrorList();
</span><span class="cx">     m_context-&gt;compressedTexImage2D(target, level, internalformat, width, height,
</span><span class="cx">         border, data-&gt;byteLength(), data-&gt;baseAddress());
</span><span class="lines">@@ -1604,6 +1600,16 @@
</span><span class="cx">         m_context-&gt;disableVertexAttribArray(index);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool WebGLRenderingContextBase::validateNPOTTextureLevel(GC3Dsizei width, GC3Dsizei height, GC3Dint level, const char* functionName)
+{
+    if (!isGLES2NPOTStrict() &amp;&amp; level &amp;&amp; WebGLTexture::isNPOT(width, height)) {
+        synthesizeGLError(GraphicsContext3D::INVALID_VALUE, functionName, &quot;level &gt; 0 not power of 2&quot;);
+        return false;
+    }
+
+    return true;
+}
+
</ins><span class="cx"> bool WebGLRenderingContextBase::validateElementArraySize(GC3Dsizei count, GC3Denum type, GC3Dintptr offset)
</span><span class="cx"> {
</span><span class="cx">     RefPtr&lt;WebGLBuffer&gt; elementArrayBuffer = m_boundVertexArrayObject-&gt;getElementArrayBuffer();
</span><span class="lines">@@ -1915,14 +1921,15 @@
</span><span class="cx">     bool vertexAttrib0Simulated = false;
</span><span class="cx">     if (!isGLES2Compliant())
</span><span class="cx">         vertexAttrib0Simulated = simulateVertexAttrib0(first + count - 1);
</span><ins>+    bool usesFallbackTexture = false;
</ins><span class="cx">     if (!isGLES2NPOTStrict())
</span><del>-        checkTextureCompleteness(&quot;drawArrays&quot;, true);
</del><ins>+        usesFallbackTexture = checkTextureCompleteness(&quot;drawArrays&quot;, true);
</ins><span class="cx"> 
</span><span class="cx">     m_context-&gt;drawArrays(mode, first, count);
</span><span class="cx"> 
</span><span class="cx">     if (!isGLES2Compliant() &amp;&amp; vertexAttrib0Simulated)
</span><span class="cx">         restoreStatesAfterVertexAttrib0Simulation();
</span><del>-    if (!isGLES2NPOTStrict())
</del><ins>+    if (usesFallbackTexture)
</ins><span class="cx">         checkTextureCompleteness(&quot;drawArrays&quot;, false);
</span><span class="cx">     markContextChanged();
</span><span class="cx"> }
</span><span class="lines">@@ -1941,14 +1948,16 @@
</span><span class="cx">             validateIndexArrayPrecise(count, type, static_cast&lt;GC3Dintptr&gt;(offset), numElements);
</span><span class="cx">         vertexAttrib0Simulated = simulateVertexAttrib0(numElements);
</span><span class="cx">     }
</span><ins>+
+    bool usesFallbackTexture = false;
</ins><span class="cx">     if (!isGLES2NPOTStrict())
</span><del>-        checkTextureCompleteness(&quot;drawElements&quot;, true);
</del><ins>+        usesFallbackTexture = checkTextureCompleteness(&quot;drawElements&quot;, true);
</ins><span class="cx"> 
</span><span class="cx">     m_context-&gt;drawElements(mode, count, type, static_cast&lt;GC3Dintptr&gt;(offset));
</span><span class="cx"> 
</span><span class="cx">     if (!isGLES2Compliant() &amp;&amp; vertexAttrib0Simulated)
</span><span class="cx">         restoreStatesAfterVertexAttrib0Simulation();
</span><del>-    if (!isGLES2NPOTStrict())
</del><ins>+    if (usesFallbackTexture)
</ins><span class="cx">         checkTextureCompleteness(&quot;drawElements&quot;, false);
</span><span class="cx">     markContextChanged();
</span><span class="cx"> }
</span><span class="lines">@@ -2999,7 +3008,7 @@
</span><span class="cx">     WebGLTexture* tex = validateTextureBinding(&quot;texImage2D&quot;, target, true);
</span><span class="cx">     ASSERT(validateTexFuncParameters(&quot;texImage2D&quot;, TexImage, target, level, internalformat, width, height, border, format, type));
</span><span class="cx">     ASSERT(tex);
</span><del>-    ASSERT(!level || !WebGLTexture::isNPOT(width, height));
</del><ins>+    ASSERT(validateNPOTTextureLevel(width, height, level, &quot;texImage2D&quot;));
</ins><span class="cx">     if (!pixels) {
</span><span class="cx">         // Note: Chromium's OpenGL implementation clears textures and isResourceSafe() is therefore true.
</span><span class="cx">         // For other implementations, if they are using ANGLE_depth_texture, ANGLE depth textures
</span><span class="lines">@@ -3067,10 +3076,8 @@
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     if (functionType != TexSubImage) {
</span><del>-        if (level &amp;&amp; WebGLTexture::isNPOT(width, height)) {
-            synthesizeGLError(GraphicsContext3D::INVALID_VALUE, functionName, &quot;level &gt; 0 not power of 2&quot;);
</del><ins>+        if (!validateNPOTTextureLevel(width, height, level, functionName))
</ins><span class="cx">             return false;
</span><del>-        }
</del><span class="cx">         // For SourceArrayBufferView, function validateTexFuncData() would handle whether to validate the SettableTexFormat
</span><span class="cx">         // by checking if the ArrayBufferView is null or not.
</span><span class="cx">         if (sourceType != SourceArrayBufferView) {
</span><span class="lines">@@ -3978,9 +3985,10 @@
</span><span class="cx">     return WebGLGetInfo(Int32Array::create(value, length).release());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebGLRenderingContextBase::checkTextureCompleteness(const char* functionName, bool prepareToDraw)
</del><ins>+bool WebGLRenderingContextBase::checkTextureCompleteness(const char* functionName, bool prepareToDraw)
</ins><span class="cx"> {
</span><span class="cx">     bool resetActiveUnit = false;
</span><ins>+    bool usesAtLeastOneBlackTexture = false;
</ins><span class="cx">     WebGLTexture::TextureExtensionFlag extensions = textureExtensionFlags();
</span><span class="cx"> 
</span><span class="cx">     Vector&lt;unsigned&gt; noLongerUnrenderable;
</span><span class="lines">@@ -3995,6 +4003,8 @@
</span><span class="cx">             continue;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        usesAtLeastOneBlackTexture = true;
+
</ins><span class="cx">         if (badTexture != m_activeTextureUnit) {
</span><span class="cx">             m_context-&gt;activeTexture(badTexture + GraphicsContext3D::TEXTURE0);
</span><span class="cx">             resetActiveUnit = true;
</span><span class="lines">@@ -4025,6 +4035,8 @@
</span><span class="cx"> 
</span><span class="cx">     for (unsigned renderable : noLongerUnrenderable)
</span><span class="cx">         m_unrenderableTextureUnits.remove(renderable);
</span><ins>+
+    return usesAtLeastOneBlackTexture;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebGLRenderingContextBase::createFallbackBlackTextures1x1()
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasWebGLRenderingContextBaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.h (198587 => 198588)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.h        2016-03-23 18:00:43 UTC (rev 198587)
+++ trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.h        2016-03-23 19:21:11 UTC (rev 198588)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -422,6 +422,7 @@
</span><span class="cx"> 
</span><span class="cx">     bool validateDrawArrays(const char* functionName, GC3Denum mode, GC3Dint first, GC3Dsizei count, GC3Dsizei primcount);
</span><span class="cx">     bool validateDrawElements(const char* functionName, GC3Denum mode, GC3Dsizei count, GC3Denum type, long long offset, unsigned&amp; numElements, GC3Dsizei primcount);
</span><ins>+    bool validateNPOTTextureLevel(GC3Dsizei width, GC3Dsizei height, GC3Dint level, const char* functionName);
</ins><span class="cx"> 
</span><span class="cx">     // Adds a compressed texture format.
</span><span class="cx">     void addCompressedTextureFormat(GC3Denum);
</span><span class="lines">@@ -616,7 +617,7 @@
</span><span class="cx">     virtual void texSubImage2DBase(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum internalformat, GC3Denum format, GC3Denum type, const void* pixels, ExceptionCode&amp;) = 0;
</span><span class="cx">     virtual void texSubImage2DImpl(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Denum format, GC3Denum type, Image*, GraphicsContext3D::ImageHtmlDomSource, bool flipY, bool premultiplyAlpha, ExceptionCode&amp;) = 0;
</span><span class="cx"> 
</span><del>-    void checkTextureCompleteness(const char*, bool);
</del><ins>+    bool checkTextureCompleteness(const char*, bool);
</ins><span class="cx"> 
</span><span class="cx">     void createFallbackBlackTextures1x1();
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>