0

I'm working on a webgl scene that involves big textures. I have been querying to determine the largest texture size supported by a given device as follows:

var gl = document.createElement('canvas').getContext('webgl')
console.log(gl.getParameter(gl.MAX_TEXTURE_SIZE))

On my 2015 Macbook Pro, I can use a texture at the returned size just fine. On a 2015 MacBook Air, however, when I try to pass a texture of the returned size as a uniform to my shaders, I get an error:

[.WebGL-0x7fcb4489200]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION :
glGenerateMipmap <- error from previous GL command

If I use instead a texture that's size gl.getParameter(gl.MAX_TEXTURE_SIZE)/2 however, the MacBook Air renders the scene just fine and there's no error.

Is it possible the MacBook Air has mis-reported its gl capabilities? Or am I trying to determine the max texture size incorrectly? My goal is to query for the largest texture supported by the current browser device. Any suggestions others can offer on how best to achieve this goal would be entirely welcome!

I observed the behavior above in Chrome 78 and Safari 13.

Mugen87
  • 28,829
  • 4
  • 27
  • 50
duhaime
  • 25,611
  • 17
  • 169
  • 224
  • 1
    Shot in the dark: Is the max size reported a power of 2? – TheJim01 Nov 25 '19 at 17:41
  • @TheJim01 Thanks for your response! The max reported texture size is 2**14. Can you tell me what gave you that hunch though, or what it might mean if the max texture size wasn't a power of two? – duhaime Nov 25 '19 at 20:16
  • The hunch was that it was reporting a number that was a "starting at 1" value, rather than "starting at zero." This would mean the largest value you could send was actually `gl.MAX_TEXTURE_SIZE - 1`. But since you ruled that out, sending a `gl.MAX_TEXTURE_SIZE - 1 x gl.MAX_TEXTURE_SIZE - 1` texture would just result in the "power of two" warning. – TheJim01 Nov 25 '19 at 21:07
  • Ah, thanks for the follow up @TheJim01, makes sense... – duhaime Nov 25 '19 at 21:18

1 Answers1

1

Post some code! There is no guarantee you can allocate a texture of the max size since the system could be out of memory.

A texture that is MAX_TEXTURE_SIZE x 1 or 1 x MAX_TEXTURE_SIZE is likely to not run out of memory. MAX_TEXTURE_SIZE x MAX_TEXTURE_SIZE when MAX_TEXTURE_SIZE is 2^14 requires min 1gig of vram if it was RGBA/UNSIGNED_BYTE. With mips it would be 1.4gig of vram. On top of that the browser will potentially allocate 1.4gig of vram to try to clear the texture and the driver itself will potentially allocate 1.4gig of ram to backup the texture or to prepare it to be uploaded to the GPU or both. Lots of opportunity to run out of memory. If it was RGBA/FLOAT textures it would require 5.6gig.

Here's a test allocating MAX x 1

const gl = document.createElement('canvas').getContext('webgl');
const maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
const maxRenderbufferSize = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
console.log('max texture size:', maxTextureSize);
console.log('max renderbuffer size:', maxRenderbufferSize);

const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(
    gl.TEXTURE_2D,
    0,
    gl.RGBA,
    maxTextureSize,
    1,
    0,
    gl.RGBA,
    gl.UNSIGNED_BYTE,
    null);
gl.generateMipmap(gl.TEXTURE_2D);
console.log('GL ERROR:', glEnumToString(gl, gl.getError()));


function glEnumToString(gl, value) {
  const keys = [];
  for (const key in gl) {
    if (gl[key] === value) {
      keys.push(key);
    }
  }
  return keys.length ? keys.join(' | ') : `0x${value.toString(16)}`;
}
gman
  • 100,619
  • 31
  • 269
  • 393
  • Thanks @gman! You've taught me so much. Sorry for the lack of texture code--the way my textures are composed requires quite a bit of stagesetting and I was hoping this problem was related to something I was entirely overlooking (e.g. limits of RAM) rather than a mis-reported value. Quick followup: Are there ways to query the total available and allocated amounts of RAM available? – duhaime Nov 26 '19 at 14:23
  • 1
    unfortunately there is no way ti query ram or gpu memory – gman Nov 26 '19 at 23:49
  • I have a strong hunch you are right. I wish I could verify... – duhaime Nov 27 '19 at 00:33
  • 1
    see https://stackoverflow.com/questions/4552372/determining-available-video-memory and https://stackoverflow.com/questions/2027634/javascript-to-find-memory-available – gman Nov 27 '19 at 01:17