content/canvas/test/webgl-conformance/conformance/extensions/ext-sRGB.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset="utf-8"/>
     5 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
     6 <script src="../../resources/js-test-pre.js"></script>
     7 <script src="../resources/webgl-test.js"></script>
     8 <script src="../resources/webgl-test-utils.js"></script>
     9 </head>
    10 <body>
    11 <div id="description"></div>
    12 <div id="console"></div>
    13 <canvas id="canvas" width="16" height="16" style="width: 50px; height: 50px; border: 1px solid black;"></canvas>
    15 <!-- Shaders to test output -->
    16 <script id="vertexShader" type="x-shader/x-vertex">
    17 attribute vec4 aPosition;
    18 void main() {
    19   gl_Position = aPosition;
    20 }
    21 </script>
    23 <script id="fragmentShader" type="x-shader/x-fragment">
    24 precision mediump float;
    25 uniform float uColor;
    26 void main() {
    27   gl_FragColor = vec4(uColor, uColor, uColor, 1);
    28 }
    29 </script>
    31 <script>
    32 "use strict";
    34 var wtu = WebGLTestUtils;
    35 var canvas;
    36 var gl;
    37 var ext = null;
    39 function getExtension() {
    40   ext = gl.getExtension("EXT_sRGB");
    41 }
    43 function listsExtension() {
    44   var supported = gl.getSupportedExtensions();
    45   return (supported.indexOf("EXT_sRGB") >= 0);
    46 }
    48 function readLocation(x, y) {
    49   var pixel = new Uint8Array(1 * 1 * 4);
    50   var px = Math.floor(x * canvas.drawingBufferWidth);
    51   var py = Math.floor(y * canvas.drawingBufferHeight);
    52   gl.readPixels(px, py, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
    53   return pixel;
    54 }
    56 function toVec3String(val) {
    57   if (typeof(val) == 'number') {
    58     return toVec3String([val, val, val]);
    59   }
    60   return '[' + val[0] + ', ' + val[1] + ', ' + val[2] + ']';
    61 }
    63 var e = 2; // Amount of variance to allow in result pixels - may need to be tweaked higher
    65 function expectResult(target, successMessage, failureMessage) {
    66   var anyDiffer = false;
    67   var source = readLocation(0.5, 0.5);
    68   for (var m = 0; m < 3; m++) {
    69     if (Math.abs(source[m] - target) > e) {
    70       anyDiffer = true;
    71       testFailed(failureMessage + "; should be " + toVec3String(target) + ", was " + toVec3String(source));
    72       break;
    73     }
    74   }
    76   if (!anyDiffer) {
    77     testPassed(successMessage);
    78   }
    79 }
    81 function createGreysRGBTexture(gl, color) {
    82   var numPixels = gl.drawingBufferWidth * gl.drawingBufferHeight;
    83   var size = numPixels * 3;
    84   var buf = new Uint8Array(size);
    85   for (var ii = 0; ii < numPixels; ++ii) {
    86     var off = ii * 3;
    87     buf[off + 0] = color;
    88     buf[off + 1] = color;
    89     buf[off + 2] = color;
    90   }
    92   var tex = gl.createTexture();
    93   gl.bindTexture(gl.TEXTURE_2D, tex);
    94   gl.texImage2D(gl.TEXTURE_2D,
    95                 0,
    96                 ext.SRGB_EXT,
    97                 gl.drawingBufferWidth,
    98                 gl.drawingBufferHeight,
    99                 0,
   100                 ext.SRGB_EXT,
   101                 gl.UNSIGNED_BYTE,
   102                 buf);
   103   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
   104   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   105   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   106   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   107   return tex;
   108 }
   110 function testValidFormat(fn, internalFormat, formatName) {
   111   fn(internalFormat);
   112   glErrorShouldBe(gl, gl.NO_ERROR, "was able to create type " + formatName);
   113 }
   115 function testInvalidFormat(fn, internalFormat, formatName) {
   116   fn(internalFormat);
   117   var err = gl.getError();
   118   if (err == gl.NO_ERROR) {
   119     testFailed("should NOT be able to create type " + formatName);
   120   } else if (err == gl.INVALID_OPERATION) {
   121     testFailed("should return gl.INVALID_ENUM for type " + formatName);
   122   } else if (err == gl.INVALID_ENUM) {
   123     testPassed("not able to create invalid format: " + formatName);
   124   }
   125 }
   127 var textureFormatFixture = {
   128   desc: "Checking texture formats",
   129   create: function(format) {
   130     var tex = gl.createTexture();
   131     gl.bindTexture(gl.TEXTURE_2D, tex);
   132     gl.texImage2D(gl.TEXTURE_2D,
   133                   0,                      // level
   134                   format,                 // internalFormat
   135                   gl.drawingBufferWidth,  // width
   136                   gl.drawingBufferHeight, // height
   137                   0,                      // border
   138                   format,                 // format
   139                   gl.UNSIGNED_BYTE,       // type
   140                   null);                  // data
   141   },
   142   tests: [
   143     {
   144       desc: "Checking valid formats",
   145       fn: testValidFormat,
   146       formats: [ 'SRGB_EXT', 'SRGB_ALPHA_EXT' ]
   147     },
   148     {
   149       desc: "Checking invalid formats",
   150       fn: testInvalidFormat,
   151       formats: [ 'SRGB8_ALPHA8_EXT' ]
   152     }
   153   ]
   154 };
   156 var renderbufferFormatFixture = {
   157   desc: "Checking renderbuffer formats",
   158   create: function(format) {
   159     var rbo = gl.createRenderbuffer();
   160     gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
   161     gl.renderbufferStorage(gl.RENDERBUFFER,
   162                            format,
   163                            gl.drawingBufferWidth,
   164                            gl.drawingBufferHeight);
   165   },
   166   tests: [
   167     {
   168       desc: "Checking valid formats",
   169       fn: testValidFormat,
   170       formats: [ 'SRGB8_ALPHA8_EXT' ]
   171     },
   172     {
   173       desc: "Checking invalid formats",
   174       fn: testInvalidFormat,
   175       formats: [ 'SRGB_EXT', 'SRGB_ALPHA_EXT' ]
   176     }
   177   ]
   178 };
   181 description("Test sRGB texture support");
   183 debug("");
   184 debug("Canvas.getContext");
   186 canvas = document.getElementById("canvas");
   187 gl = wtu.create3DContext(canvas);
   188 if (!gl) {
   189   testFailed("context does not exist");
   190 } else {
   191   testPassed("context exists");
   193   debug("");
   194   debug("Checking sRGB texture support");
   196   // Query the extension and store globally so shouldBe can access it
   197   ext = gl.getExtension("EXT_sRGB");
   199   if (!ext) {
   200     testPassed("No EXT_sRGB support -- this is legal");
   202     runSupportedTest(false);
   203   } else {
   204     testPassed("Successfully enabled EXT_sRGB extension");
   206     runSupportedTest(true);
   208     gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
   210     runFormatTest(textureFormatFixture);
   211     runFormatTest(renderbufferFormatFixture);
   212     runTextureReadConversionTest();
   213     runFramebufferTextureConversionTest();
   214     runFramebufferRenderbufferConversionTest();
   215   }
   216 }
   218 function runSupportedTest(extensionEnabled) {
   219   if (listsExtension()) {
   220     if (extensionEnabled) {
   221       testPassed("EXT_sRGB listed as supported and getExtension succeeded");
   222     } else {
   223       testFailed("EXT_sRGB listed as supported but getExtension failed");
   224     }
   225   } else {
   226     if (extensionEnabled) {
   227       testFailed("EXT_sRGB not listed as supported but getExtension succeeded");
   228     } else {
   229       testPassed("EXT_sRGB not listed as supported and getExtension failed -- this is legal");
   230     }
   231   }
   232 }
   234 function runFormatTest(fixture) {
   235   debug("");
   236   debug(fixture.desc);
   238   for (var tt = 0; tt < fixture.tests.length; ++tt) {
   239     var test = fixture.tests[tt];
   240     debug(test.desc);
   242     for (var ii = 0; ii < test.formats.length; ++ii) {
   243       var formatName = test.formats[ii];
   244       test.fn(fixture.create, ext[formatName], "ext." + formatName);
   245     }
   247     if (tt != fixture.tests.length - 1)
   248       debug("");
   249   }
   250 }
   252 function runTextureReadConversionTest() {
   253   debug("");
   254   debug("Test the conversion of colors from sRGB to linear on texture read");
   256   // Draw
   257   var conversions = [
   258     [   0,   0 ],
   259     [  63,  13 ],
   260     [ 127,  54 ],
   261     [ 191, 133 ],
   262     [ 255, 255 ]
   263   ];
   265   var program = wtu.setupTexturedQuad(gl);
   266   gl.uniform1i(gl.getUniformLocation(program, "tex2d"), 0);
   268   for (var ii = 0; ii < conversions.length; ii++) {
   269     var tex = createGreysRGBTexture(gl, conversions[ii][0]);
   270     wtu.drawQuad(gl);
   271     expectResult(conversions[ii][1],
   272                  "sRGB texture read returned correct data",
   273                  "sRGB texture read returned incorrect data");
   274   }
   275 }
   277 function runFramebufferTextureConversionTest() {
   278   debug("");
   279   debug("Test the conversion of colors from linear to sRGB on framebuffer (texture) write");
   281   var program = wtu.setupProgram(gl, ['vertexShader', 'fragmentShader'], ['aPosition'], [0]);
   282   var tex = createGreysRGBTexture(gl, 0);
   283   var fbo = gl.createFramebuffer();
   284   gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
   285   gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
   286   glErrorShouldBe(gl, gl.NO_ERROR);
   288   shouldBe('gl.getFramebufferAttachmentParameter(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, ext.FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT)', 'ext.SRGB_EXT');
   289   shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
   291   // Draw
   292   var conversions = [
   293     [   0,   0 ],
   294     [  13,  63 ],
   295     [  54, 127 ],
   296     [ 133, 191 ],
   297     [ 255, 255 ]
   298   ];
   300   wtu.setupUnitQuad(gl, 0);
   302   for (var ii = 0; ii < conversions.length; ii++) {
   303     gl.uniform1f(gl.getUniformLocation(program, "uColor"), conversions[ii][0]/255.0);
   304     wtu.drawQuad(gl, [0, 0, 0, 0]);
   305     expectResult(conversions[ii][1],
   306                  "framebuffer (texture) read returned correct data",
   307                  "framebuffer (texture) read returned incorrect data");
   308   }
   310   gl.bindFramebuffer(gl.FRAMEBUFFER, null);
   311 }
   313 function runFramebufferRenderbufferConversionTest() {
   314   debug("");
   315   debug("Test the conversion of colors from linear to sRGB on framebuffer (renderbuffer) write");
   317   function createsRGBFramebuffer(gl, width, height) {
   318     var rbo = gl.createRenderbuffer();
   319     gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
   320     gl.renderbufferStorage(gl.RENDERBUFFER, ext.SRGB8_ALPHA8_EXT, width, height);
   321     glErrorShouldBe(gl, gl.NO_ERROR);
   323     var fbo = gl.createFramebuffer();
   324     gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
   325     gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
   326                                gl.RENDERBUFFER, rbo);
   327     glErrorShouldBe(gl, gl.NO_ERROR);
   329     shouldBe('gl.getFramebufferAttachmentParameter(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, ext.FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT)', 'ext.SRGB_EXT');
   330     shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
   332     return fbo;
   333   }
   335   // Draw
   336   var conversions = [
   337     [   0,   0 ],
   338     [  13,  63 ],
   339     [  54, 127 ],
   340     [ 133, 191 ],
   341     [ 255, 255 ]
   342   ];
   344   var program = wtu.setupProgram(gl, ['vertexShader', 'fragmentShader'], ['aPosition'], [0]);
   345   wtu.setupUnitQuad(gl, 0);
   346   var fbo = createsRGBFramebuffer(gl, 4, 4);
   348   for (var ii = 0; ii < conversions.length; ii++) {
   349     gl.uniform1f(gl.getUniformLocation(program, "uColor"), conversions[ii][0]/255.0);
   350     wtu.drawQuad(gl, [0, 0, 0, 0]);
   351     expectResult(conversions[ii][1],
   352                  "framebuffer (renderbuffer) read returned the correct data",
   353                  "framebuffer (renderbuffer) read returned incorrect data");
   354   }
   355 }
   357 debug("");
   358 var successfullyParsed = true;
   359 </script>
   360 <script>finishTest();</script>
   362 </body>
   363 </html>

mercurial