Bug 52190 - getUniform should support SAMPLER_2D or SAMPLER_CUBE
Summary: getUniform should support SAMPLER_2D or SAMPLER_CUBE
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebGL (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Zhenyao Mo
URL:
Keywords:
Depends on:
Blocks: 53542
  Show dependency treegraph
 
Reported: 2011-01-10 19:22 PST by Ben Vanik
Modified: 2011-02-01 15:42 PST (History)
4 users (show)

See Also:


Attachments
Patch (5.49 KB, patch)
2011-02-01 10:07 PST, Zhenyao Mo
kbr: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Vanik 2011-01-10 19:22:45 PST
The getUniform call will return 'null' and set INVALID_VALUE when any uniforms of type SAMPLER_2D or SAMPLER_CUBE are queried. Looking at the implementation in WebGLRenderContext.cpp line 2122, there is a '// FIXME: what to do about samplers?' and both type values get ignored.

SAMPLER_2D and SAMPLER_CUBE are both integer values set with glUniformi[v] and as such can be queried from GL with glGetUniformiv. Firefox currently implements this and as such in that browser getUniform works for these types.

I've tested Chromium on both OSX and Windows (with ANGLE) with the following simple change and it works in my tests:
@@ -2117,9 +2117,13 @@
                     baseType = GraphicsContext3D::FLOAT;
                     length = 16;
                     break;
+                case GraphicsContext3D::SAMPLER_2D:
+                case GraphicsContext3D::SAMPLER_CUBE:
+                    baseType = GraphicsContext3D::INT;
+                    length = 1;
+                    break;
                 default:
                     // Can't handle this type
-                    // FIXME: what to do about samplers?
                     m_context->synthesizeGLError(GraphicsContext3D::INVALID_VALUE);
                     return WebGLGetInfo();
                 }


With these changes, you should actually be able to do things like this:
var samplerLoc = gl.getUniformLocation(program, "mySampler");
gl.uniform1i(samplerLoc, 1);
var value = gl.getUniform(program, samplerLoc);
assert(value == 1);
Comment 1 Ben Vanik 2011-01-10 19:47:22 PST
Here's the current workaround I'm using until this is fixed, in case anyone else needs it - just call installUniformHack after getting the gl context.

    function installUniformHack(gl) {
        var uniqueUniformId = 0;
        var original_getUniformLocation = gl.getUniformLocation;
        gl.getUniformLocation = function (program, name) {
            var result = original_getUniformLocation.apply(this, arguments);
            if (result) {
                if (!result.hasOwnProperty("__uniqueId")) {
                    result.__uniqueId = String(uniqueUniformId++);
                }
            }
            return result;
        };
        var original_uniform1i = gl.uniform1i;
        gl.uniform1i = function (loc, value) {
            var program = this.getParameter(this.CURRENT_PROGRAM);
            var lookaside = program.__uniformLookaside;
            if (!lookaside) {
                lookaside = program.__uniformLookaside = {};
            }
            lookaside[loc.__uniqueId] = value;
            original_uniform1i.apply(this, arguments);
        };
        var original_uniform1iv = gl.uniform1iv;
        gl.uniform1iv = function (loc, value) {
            var program = this.getParameter(this.CURRENT_PROGRAM);
            var lookaside = program.__uniformLookaside;
            if (!lookaside) {
                lookaside = program.__uniformLookaside = {};
            }
            lookaside[loc.__uniqueId] = value[0];
            original_uniform1iv.apply(this, arguments);
        };
        var original_getUniform = gl.getUniform;
        gl.getUniform = function (program, loc) {
            var lookaside = program.__uniformLookaside;
            if (lookaside) {
                if (lookaside.hasOwnProperty(loc.__uniqueId)) {
                    return lookaside[loc.__uniqueId];
                }
            }
            return original_getUniform.apply(this, arguments);
        };
    };
Comment 2 Zhenyao Mo 2011-01-27 20:36:06 PST
I'll incorporate Ben's patch and add some test cases.
Comment 3 Zhenyao Mo 2011-02-01 10:07:23 PST
Created attachment 80779 [details]
Patch
Comment 4 Zhenyao Mo 2011-02-01 10:07:51 PST
Will update the khronos side test once this is reviewed.
Comment 5 Kenneth Russell 2011-02-01 14:18:13 PST
Comment on attachment 80779 [details]
Patch

Looks good. Thanks for cleaning this up.
Comment 6 Zhenyao Mo 2011-02-01 14:43:16 PST
Committed r77311: <http://trac.webkit.org/changeset/77311>