Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

Fix WebGL2 getAttribLocation for built-in attributes

Implementing fix for issue where gl.getAttribLocation returns incorrect value (0 instead of -1) for built-in attributes like gl_VertexID, gl_InstanceID, gl_Position, and gl_PointSize.

Progress:

  • Explore codebase and locate getAttribLocation implementation
  • Move implementation to client/graphics layer per review feedback
  • Add built-in attribute name check in WebGLContext::getAttribLocation
  • Return nullopt for built-in attributes (converted to -1 by script bindings)
  • Run clang-format to ensure code formatting
  • Restore package-lock.json to original state
  • Create test file for built-in attributes
  • Validate changes with formatting checks
  • ⚠️ Cannot build/test on Linux (macOS required for development)

Changes Made:

Modified: /home/runner/work/jsar-runtime/jsar-runtime/src/client/graphics/webgl_context.cpp

  • Added <set> include for std::set container
  • Added check for built-in attribute names in WebGLContext::getAttribLocation():
    // WebGL 2.0 spec §5.18: Built-in attributes always return -1
    static const std::set<string> kBuiltInAttribs = {
      "gl_VertexID", "gl_InstanceID", "gl_Position", "gl_PointSize"};
    if (kBuiltInAttribs.count(name))
    {
      return nullopt;  // Returns nullopt which script bindings converts to -1
    }

Added: fixtures/html/webgl-conformance/webgl2-getattriblocation-builtin-attribs.html

  • Simplified test page to verify the fix
  • Tests all four built-in attributes (gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize)
  • Visual pass/fail indicators with console logging
  • Follows WebGL 2.0 spec §5.18 requirements

Restored: package-lock.json - Reverted to original state (no registry URL changes)

Validation:

  • ✅ C++ formatting check passes (clang-format)
  • ✅ Logic moved to correct architectural layer (client/graphics)
  • ✅ package-lock.json restored per review feedback
  • ✅ Test file created for manual verification
  • ⚠️ Cannot run full build/tests on Linux (macOS required for development)

Architecture Benefits:

  • ✅ Centralized logic at lower level (graphics layer vs script bindings)
  • ✅ All WebGL contexts benefit from single implementation
  • ✅ Cleaner separation of concerns

Specification Reference:

WebGL 2.0 spec §5.18:

"Applications may query the location of a built-in attribute by passing its name to getAttribLocation, but this will always return -1."

Impact:

Original prompt

This section details on the original issue you should resolve

<issue_title>WebGL2: gl.getAttribLocation for built-in attribute 'gl_VertexID' returns incorrect value (should be -1, got 0)</issue_title>
<issue_description>## Bug Description
Running Khronos/WebGL2 conformance test (active-built-in-attribs.html) in JSAR Runtime:

shouldBe('activeInfo.name', '"gl_VertexID"');
attribLoc = gl.getAttribLocation(prog, 'gl_VertexID');
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be able to request the location of a built-in.");
shouldBe('attribLoc', '-1');

Test fails: attribLoc should be -1. Was 0.

Spec Reference

Root Cause

  • JSAR currently returns 0 for built-in attribute names instead of -1, violating spec compliance.
  • Correct behavior: gl.getAttribLocation(program, 'gl_VertexID') MUST return -1, never a valid location.

Acceptance Criteria

  • getAttribLocation on any built-in attribute ('gl_VertexID', 'gl_InstanceID', 'gl_Position', 'gl_PointSize') MUST always return -1
  • The referenced conformance test passes

Implementation Guidance (C++)

  • Fix the attribute location query in WebGLRenderingContext/WebGL2RenderingContext
  • Check if the attribute name matches any of the built-in names, and always return -1
int WebGLRenderingContext::getAttribLocation(WebGLProgram* program, const std::string& name) {
    static const std::set<std::string> kBuiltInAttribs = {
        "gl_VertexID", "gl_InstanceID", "gl_Position", "gl_PointSize"
    };
    if (kBuiltInAttribs.count(name)) {
        return -1;
    }
    // ... normal lookup logic
}
  • Update error handling and unit tests for built-in attribute query

Impact

  • Strict compliance with WebGL2 specification
  • Enables all Khronos/WebGL2 tests for built-in attrib queries to pass

Relevant test HTML:

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add check for built-in attribute names (gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize)
- Return -1 immediately for built-in attributes per WebGL 2.0 spec §5.18
- Add <set> include for std::set container
- Complies with WebGL 2.0 specification requirement

Co-authored-by: EndlessJour9527 <155411404+EndlessJour9527@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix gl.getAttribLocation for gl_VertexID returning incorrect value Fix getAttribLocation to return -1 for built-in attributes per WebGL 2.0 spec Nov 13, 2025
Copilot finished work on behalf of EndlessJour9527 November 13, 2025 08:02
Comment on lines 2642 to 2650
// WebGL 2.0 spec §5.18: Built-in attributes always return -1
static const std::set<string> kBuiltInAttribs = {
"gl_VertexID", "gl_InstanceID", "gl_Position", "gl_PointSize"};
if (kBuiltInAttribs.count(name))
{
info.GetReturnValue().Set(Number::New(isolate, -1));
return;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please impl in client/graphics

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Moved the implementation to client/graphics/webgl_context.cpp in commit c135954. The built-in attribute check now happens at the graphics layer in WebGLContext::getAttribLocation(), which returns nullopt for built-in attributes. The script bindings layer correctly converts this to -1.

- Moved implementation from script bindings to WebGLContext::getAttribLocation
- Returns nullopt for built-in attributes (gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize)
- Script bindings layer now correctly converts nullopt to -1
- Better architecture: centralizes logic at the graphics layer

Addresses review feedback from @EndlessJour9527

Co-authored-by: EndlessJour9527 <155411404+EndlessJour9527@users.noreply.github.com>
Copilot finished work on behalf of EndlessJour9527 November 14, 2025 09:49
@EndlessJour9527 EndlessJour9527 marked this pull request as ready for review November 17, 2025 05:56
Copilot finished reviewing on behalf of EndlessJour9527 November 17, 2025 05:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes WebGL 2.0 spec compliance for getAttribLocation() to correctly return -1 for built-in attributes (gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize) instead of incorrectly returning 0. This resolves Khronos WebGL 2.0 conformance test failures.

Key Changes

  • Added early return check in WebGLContext::getAttribLocation() to detect built-in attribute names and return nullopt (which the script bindings layer converts to -1)
  • Applied consistent C++ code formatting across multiple WebGL script binding files (removing extra spaces in closing namespace comments)
  • Updated package-lock.json registry URLs from npmmirror.com to npmjs.org

Reviewed Changes

Copilot reviewed 1 out of 18 changed files in this pull request and generated no comments.

File Description
src/client/graphics/webgl_context.cpp Added built-in attribute check with early return in getAttribLocation(), plus <set> include
src/client/script_bindings/webgl/*.{hpp,cpp} Formatting fix: standardized namespace closing comment spacing
package-lock.json Changed registry URLs from npmmirror.com to npmjs.org for all packages

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please restore this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Restored package-lock.json to its original state in commit e587f6c. The registry URLs have been reverted from npmjs.org back to npmmirror.com.

- Reverted registry URL changes from npmjs.org back to npmmirror.com
- File restored to state before initial fix commit

Addresses review feedback from @EndlessJour9527

Co-authored-by: EndlessJour9527 <155411404+EndlessJour9527@users.noreply.github.com>
Copilot finished work on behalf of EndlessJour9527 November 18, 2025 12:09
@EndlessJour9527
Copy link
Collaborator

@copilot 在 fixtures/html/webgl-conformance/ 下新增一个简化版测试页,例如:

webgl2-getattriblocation-builtin-attribs.html
核心 JS:

js
const gl = canvas.getContext('webgl2');
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, #version 300 es void main() { gl_Position = vec4(0.0); });
gl.compileShader(vs);

const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, #version 300 es precision mediump float; out vec4 color; void main() { color = vec4(1.0); } );
gl.compileShader(fs);

const prog = gl.createProgram();
gl.attachShader(prog, vs);
gl.attachShader(prog, fs);
gl.linkProgram(prog);

console.log(gl.getAttribLocation(prog, "gl_VertexID")); // expect -1
console.log(gl.getAttribLocation(prog, "gl_InstanceID")); // expect -1
console.log(gl.getAttribLocation(prog, "gl_Position")); // expect -1
console.log(gl.getAttribLocation(prog, "gl_PointSize")); // expect -1

- Created webgl2-getattriblocation-builtin-attribs.html test file
- Tests that getAttribLocation returns -1 for gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize
- Includes visual pass/fail indicators and console logging
- Follows existing test file structure in fixtures/html/webgl-conformance/

Addresses request from @EndlessJour9527

Co-authored-by: EndlessJour9527 <155411404+EndlessJour9527@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 18, 2025

@copilot 在 fixtures/html/webgl-conformance/ 下新增一个简化版测试页,例如:

webgl2-getattriblocation-builtin-attribs.html
...

Done! Created the test file webgl2-getattriblocation-builtin-attribs.html in commit 99611d4. The test verifies that gl.getAttribLocation() returns -1 for all four built-in attributes (gl_VertexID, gl_InstanceID, gl_Position, gl_PointSize) with visual pass/fail indicators and console logging.

Copilot finished work on behalf of EndlessJour9527 November 18, 2025 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebGL2: gl.getAttribLocation for built-in attribute 'gl_VertexID' returns incorrect value (should be -1, got 0)

2 participants