Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

WebGL2 contexts were throwing TypeError: gl.framebufferTextureLayer is not a function due to missing implementation. This blocked rendering to texture array layers and 3D texture slices, breaking WebGL2 conformance.

Changes

Command buffer infrastructure

  • Added FRAMEBUFFER_TEXTURE_LAYER request type to command buffer macros
  • Implemented FramebufferTextureLayerCommandBufferRequest class (target, attachment, texture, level, layer)

Backend implementation

  • Replaced NOT_IMPLEMENTED() stub in WebGL2Context::framebufferTextureLayer
  • Routes to OpenGL via command buffer: glFramebufferTextureLayer(target, attachment, texture, level, layer)

JavaScript bindings

  • Exposed framebufferTextureLayer method on WebGL2RenderingContext
  • Validates 5 arguments: target (GLenum), attachment (GLenum), texture (WebGLTexture|null), level (GLint), layer (GLint)

Usage

const gl = canvas.getContext('webgl2');
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);
gl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, 256, 256, 8);

// Attach layer 3 to framebuffer
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, 0, 3);

Enables rendering to GL_TEXTURE_2D_ARRAY and GL_TEXTURE_3D layers per WebGL 2.0 §5.19.8.
case

Original prompt

This section details on the original issue you should resolve

<issue_title>WebGL2: gl.framebufferTextureLayer is not implemented (TypeError)</issue_title>
<issue_description>## Bug Description
When running WebGL2 content or Khronos/WebGL2 conformance tests in JSAR Runtime, the following error is triggered:

Uncaught TypeError: gl.framebufferTextureLayer is not a function

This occurs on calls like:

gl.framebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, level, layer);

Spec Reference

Expected Behavior

gl.framebufferTextureLayer() should be present and correctly bind a specific layer of a 3D/array texture to the framebuffer in all WebGL2 contexts.

  • Required for rendering to texture arrays and 3D textures (GL_TEXTURE_2D_ARRAY/GL_TEXTURE_3D)

Root Cause

  • JSAR does not currently implement the framebufferTextureLayer function in its WebGL2RenderingContext binding.
  • Calls to this function yield a TypeError, breaking WebGL2 applications and test suites.

Acceptance Criteria

  • gl.framebufferTextureLayer must exist and be callable on all WebGL2 contexts
  • The function should bind the specified texture layer and produce correct OpenGL calls
  • Should pass Khronos/WebGL2 conformance tests that use layered rendering

Implementation Guidance (C++)

Implement the binding for framebufferTextureLayer:

void WebGL2RenderingContext::framebufferTextureLayer(GLenum target, GLenum attachment, WebGLTexture* texture, GLint level, GLint layer) {
    glFramebufferTextureLayer(target, attachment, texture ? texture->glObject() : 0, level, layer);
}
  • Add argument validation per WebGL2 spec
  • Update JS-to-native binding so this function is exposed
  • Ensure proper GL error handling and spec compliance

Impact

  • Required for compatibility with WebGL2 content using layered rendering
  • Required for Khronos/WebGL2 conformance suite coverage
  • Breaks apps/libraries that depend on array/3D texture rendering

Please prioritize as this blocks WebGL2 specification compliance and advanced rendering support.</issue_description>

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 FRAMEBUFFER_TEXTURE_LAYER command buffer request to macros
- Implement FramebufferTextureLayerCommandBufferRequest class
- Implement framebufferTextureLayer C++ backend method
- Add JavaScript binding for framebufferTextureLayer
- Add OpenGL renderer handler for framebufferTextureLayer
- Pass C++ formatting checks

Co-authored-by: EndlessJour9527 <155411404+EndlessJour9527@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError for gl.framebufferTextureLayer implementation Implement WebGL2 framebufferTextureLayer for layered texture rendering Nov 18, 2025
Copilot finished work on behalf of EndlessJour9527 November 18, 2025 03:29
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 implements the missing WebGL2 framebufferTextureLayer API, which allows rendering to specific layers of 3D textures and texture arrays. The implementation follows established patterns in the codebase and correctly threads the functionality from JavaScript bindings through the command buffer system to the OpenGL backend.

  • Adds command buffer infrastructure for FRAMEBUFFER_TEXTURE_LAYER requests
  • Implements JavaScript binding with proper argument validation for all 5 parameters
  • Routes calls to OpenGL via glFramebufferTextureLayer in the rendering backend

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/common/command_buffers/shared.hpp Adds COMMAND_BUFFER_FRAMEBUFFER_TEXTURE_LAYER_REQ enum constant for the new command type
src/common/command_buffers/macros.hpp Registers the FRAMEBUFFER_TEXTURE_LAYER command buffer macro with class name and debug label
src/common/command_buffers/details/buffer.hpp Defines FramebufferTextureLayerCommandBufferRequest class with 5 parameters (target, attachment, texture, level, layer)
src/renderer/render_api_opengles.cpp Implements OnFramebufferTextureLayer handler that extracts texture ID from object manager and calls glFramebufferTextureLayer
src/client/script_bindings/webgl/webgl2_rendering_context.hpp Declares FramebufferTextureLayer method in WebGL2RenderingContext class
src/client/script_bindings/webgl/webgl2_rendering_context.cpp Implements JavaScript binding with comprehensive argument validation and WebGLTexture null handling
src/client/graphics/webgl_context.cpp Creates and sends command buffer request, replacing NOT_IMPLEMENTED() stub

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@EndlessJour9527 EndlessJour9527 marked this pull request as ready for review November 18, 2025 08:36
@yorkie yorkie merged commit 6fb4405 into main Nov 20, 2025
2 checks passed
@yorkie yorkie deleted the copilot/fix-framebuffer-texture-layer branch November 20, 2025 10:27
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.framebufferTextureLayer is not implemented (TypeError)

3 participants