Sign in to follow this  
lawnjelly

OpenGL GL_MAX_TEXTURE_SIZE barrier on OpenGL ES 2

Recommended Posts

Good 'morrow, this is for Android game (possibly iOS later) : I'm currently hitting up against maximum texture size limits on some devices. I've decided to not bother with trying to support 1024x1024, but I'm aiming to support 2048x2048 as a lot of devices still have this limit (including my 2013 nexus 7).
 
Now for some games this isn't an issue, you just downsize the textures .. however I'm using a pixel perfect spritesheet for depth ordered billboards, and want to be able to draw everything with one drawcall, and I'm thinking I'm going to need at least 4096 x 2048. I can't just render with 2 drawcalls, as the sprites are interleaved randomly and have to be drawn in depth order.
[attachment=35662:zorder.jpg]
So some options I have:
  1. Half size the spritesheet on load and upsize at rendering (ugly blocky)
  2. Half size the spritesheet and half the game resolution (very involved to support 2 version of the game, but would better support different screen sizes?)
  3. Use some shader jiggery pokery to get around the limit

I'm currently thinking along the lines of 3, something like :

  • Binding 2 halves of the spritesheet to active texture 0 and 1 (each 2048 x 2048)
  • Pass the uv coords in pixel space
  • In the fragment shader, if the u coord >= 2048, then -2048 and sample from texture 1 (and vice versa).
I could also maybe make the spritesheet generator avoid placing on the boundary, and decide which spritesheet in the vertex shader, if this would help.
 
Is this feasible? What kind of penalty should I be expecting for such an approach (with a conditional in the frag shader) versus the hardware supporting it natively?

 

 

Share this post


Link to post
Share on other sites

Option 3 with a 3D texture could work, seeing as you're also limited to 8 textures at once, then just select the layer based on your UV like you say.

Hm it's kinda funny considering back 5 years to when I was keeping fragment shaders uber simple, that's compared with today's crazy goings on with MSAA, lights galore, etc! But just like today I would say aim to optimize speed rather than size, perhaps even pass the texture layer as an attribute with each vertex.

 

edit: I was reading your post more than thinking; in terms of speed it'll just be your vertex shader passing a lowp int to the fragment shader. (or if adjusting the U element then an extra vertex instruction or two) [frag shader will be the same except for referencing a varying rather than a uniform]

Edited by jezham

Share this post


Link to post
Share on other sites

Yeah I guess I'll try passing the texture ID from the vertex shader and compare with just using the fragment shader. If there's not a lot of difference I might just go for the easier method so I don't have to adjust the spritesheet code.

If there is a performance hit I can have 2 sets of shaders, depending on whether the hardware supports > 2048, so the later hardware does not pay a price for the compatibility. :)

I've also had problems with devices not supporting frame buffers > 2048 for scrolling textures, but that's a whole other problem lol. Does make you wonder how the GPU guys decide what the limits should be... It must be intriguing what goes into GPU design, although I'm sure all top secret lol.

Share this post


Link to post
Share on other sites

Is this feasible? What kind of penalty should I be expecting for such an approach (with a conditional in the frag shader) versus the hardware supporting it natively?

Depends. If you do:

float4 val0 = texture( tex0, uv );
float4 val1 = texture( tex1, uv );

finalVal = uv.y > 0.5 ? val1 : val0;

There is no branch, and the cost is only slightly less performance, and higher power consumption (since you're consuming twice the bandwidth).

But if you do:

vec2 uvDdx = ddx( uv );
vec2 uvDdy = ddy( uv );

if( uv.y )
    val = texture( tex0, uv, uvDdx, uvDdy );
else
    val = texture( tex1, uv, uvDdx, uvDdy );

Then the performance impact could be quite serious (the cards you're targeting are very bad at hiding latency) but the power consumption should stay in check.

It boils down to whether the performance decrease you get puts you below the minimum you are targeting and decide to consume more power instead.

 

I've also had problems with devices not supporting frame buffers > 2048 for scrolling textures, but that's a whole other problem lol. Does make you wonder how the GPU guys decide what the limits should be... It must be intriguing what goes into GPU design, although I'm sure all top secret lol.

Smaller resolutions means less bits in the texture unit to perform addressing and filtering. Less bits means less transistors; which translates to lower costs, less power consumption therefore less heat and increased battery life.

Share this post


Link to post
Share on other sites

I'm not sure how feasible this is for your use case, but you could have a full mipmap chain for the texture, pack sprites into different miplevels, then do an explicit miplevel lookup (desktop GL has textureLod for this, I don't know GL ES but expect that it should have similar).

It won't give you the full extra texture space that a 4096x2048 texture would, but it will give you some extra.

Share this post


Link to post
Share on other sites

If you (miss-)use mipmaps you do not get very much out of it for making it more complicated. 33,3_% to be exact. And you can hardly make full use of the smaller mipmap levels.

My question would be if you really need more then a 2048x2048 texture for what you draw as ordered billboards? The only thing you normaly draw ordered without depth write are alpha transparent objects. And everything else (including stencil transparency) can be drawn before that with depth write enabled. So you can sort for texture affiliation and only need to bind each texture once per frame.

Share this post


Link to post
Share on other sites

Some good info there from Matias regarding the decision between branchless (more texture lookups) and branched shaders (have the same issue in some other shaders, I'll have to do some tests to compare performance .. it maybe the shader compiler decides it is quicker to do both the lookups even in the branched shader).

Using mipmaps is an interesting idea and something I hadn't thought of, but as osbios says this only gains a little extra texture space at quite a bit more complexity, so I'll probably go with the simpler approach.

My question would be if you really need more then a 2048x2048 texture for what you draw as ordered billboards? The only thing you normaly draw ordered without depth write are alpha transparent objects.

This is for that situation of alpha transparent depth sorted sprites (trees etc in the screenshot). :) 

Share this post


Link to post
Share on other sites

 I don't think it can be done much faster and smaller than just adjusting your UV coords in the vertex shader.

// vertex
uniform		lowp		int	u_baseTextureID;

attribute	mediump		vec2	a_uv; // normalized UV, .x also contains texture ID

varying		lowp		int	v_textureID;
varying		mediump		vec2	v_uv;

	...
	v_textureID = a_uv.x < 1.0 ? u_baseTextureID : 1 + u_baseTextureID;
	v_uv.x = a_uv.x < 1.0 ? a_uv.x : a_uv.x - 1.0;
	v_uv.y = a_uv.y;

edit...Better still, plus allows more textures than GLES2 can shake a stick at:


    //ex. vec2(2.0, 0.0) is adjusted to vec2(0.0, 0.0) w/ texture id 2
    v_textureID = int(a_uv.x);
    v_uv = vec2(a_uv.x - float(v_textureID), a_uv.y);

I suggest your UVs are pointing at pixel centers vec2i(2047, 0) would be vec2(2047.5, 0.5) / textureDim

 

edit2: might as well go all the way; use GL_REPEAT then you only have to pass the textureID and leave the UVs as is (v_uv = a_uv). Note the advantage of doing this in the vertex shader rather than for every texel.

Edited by jezham

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627746
    • Total Posts
      2978905
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now