• Advertisement
Sign in to follow this  

OpenGL Paletted Textures

This topic is 1654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I haven't seen paletted textures being used at all in shaders anymore. I think it'd be much faster too by using an 8-bit index per texel to a 1D texture containing 32-bit color data. If alpha texels have quite a lot of variance, just the RGB values could be stored in the 1D texture, and the alpha channel would be stored in a separate channel --since it's usually stored in 1 byte of data anyway bringing alpha textures to 16-bit textures with varying transparency (unlike 16-bit color textures), etc.

 

This is assuming that only 256 distinct colors are being used in the palette which would only make the 1D texture 256 pixels in total. The benefits, however, would be:

-Much less data needed per texture

-Much less memory consumption because the paletted texture

-Much less data to upload to the GPU on texture swaps

-Faster texture reads in the shader since there's less data to scan through (I'm hoping, I'm not familiar on how GPUs handle texture data)

 

It would require a little more effort to read these textures, and more instructions per shader per lookup, but I don't think it would be a big deal. but the largest benefit --less memory consumption. Mipmaps would probably have to be generated offline, but I think there are quite a few benefits here.

 

I'd like to make a game where the characters have really fluid animations in OpenGL ES 2.0. Animated vector graphics don't seem possible at all unless 3D modelers get involved. It's too bad ToonBoom Studio has the level of animation I'd like it to be, but it's exported as rasterized sprite sheets. 

Share this post


Link to post
Share on other sites
Advertisement

Why would you bother with with indexed textures when we already have DXTn, BC7 and (in the future hopefully) ASTC? These formats will give you better image quality and better compression ratios.

Share this post


Link to post
Share on other sites


Aren't those lossy compressions though? Sometimes, very lossy.

 

Converting a 24-bit texture to 8-bit indexed is extremely lossy. BC7 and ASTC have pretty decent quality, and even DXTn has better image quality than an 8-bit indexed image. If they didn't, there would be no point in having them and everyone would be using indexed textures. The way those formats work is essentially based on indexing with a palette, except each block of pixels (4x4 in DXTn) gets it's own color palette. The other advantage is that GPUs have dedicated hardware for handling them, so their performance will be greater than manually looking up colors in your shader. Not to mention, that if you were to manually implement indexed textures in your shaders, you would also have to manually implement texture filtering and mip-map generation, since hardware bi/tri-linear filtering is going to try and linearly interpolate between index values, which will not work.

Edited by Chris_F

Share this post


Link to post
Share on other sites
 

Why would you bother with with indexed textures when we already have DXTn, BC7 and (in the future hopefully) ASTC? These formats will give you better image quality and better compression ratios.

 
And worse palette animation.

Performance concerns are silly (if one has so little memory and so many textures that degrading them to 8-bit colour is acceptable, using high-performance compressed formats is also acceptable), but the indirection of using a palette allows an enormous variety of tricks with carefully drawn pixel art (for which mipmapping and interpolation are a very bad idea).

Share this post


Link to post
Share on other sites

For 2D images with a low shade count palettes can easily look better than those compression methods (though this heavily depends on the case). Also there's the issue because of the silhouettes: the compression formats that support translucency either do it by increasing the size or by reducing color precision even more.

 

The problem with the look-up idea is that it breaks texture filtering... This means you have to do it manually by reading the texture multiple times instead of just reading once, because otherwise you'll end up blending the indices. I think this is probably my biggest put off for attempting to use this idea (even if in practice it may not be as bad as I'm making it out to be, since usually you wouldn't be doing some of the really expensive operations 3D games do).

 

In practice I imagine this is one of those "try and see what happens" situations.

Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites

 

Why would you bother with with indexed textures when we already have DXTn, BC7 and (in the future hopefully) ASTC? These formats will give you better image quality and better compression ratios.

 
And worse palette animation.

Performance concerns are silly (if one has so little memory and so many textures that degrading them to 8-bit colour is acceptable, using high-performance compressed formats is also acceptable), but the indirection of using a palette allows an enormous variety of tricks with carefully drawn pixel art (for which mipmapping and interpolation are a very bad idea).

 

 

You do bring up an interesting point in that it's not entirely clear what the OP is trying to achieve with these textures. He only talks about performance and memory concerns, so I'm not sure what he's talking about in his last paragraph.

 

That said, I think palette-swap animation is probably not what anyone wants. It's certainly cool in 8/16 bit games with "extreme" (by today's standards) memory and rendering limitations as well as other idiosyncrasies, but it's not clear that there's any incentive to do it today.

 

To be honest the only time I've ever seen palette-swap animation combined with OpenGL is in that 3D Maze screensaver for Windows 95, on those weird animated fractally textures you could choose in the settings, and even then I'm only guessing that's what was going on.

Share this post


Link to post
Share on other sites

For 2D images with a low shade count palettes can easily look better than those compression methods (though this heavily depends on the case). Also there's the issue because of the silhouettes: the compression formats that support translucency either do it by increasing the size or by reducing color precision even more.

 

The problem with the look-up idea is that it breaks texture filtering... This means you have to do it manually by reading the texture multiple times instead of just reading once, because otherwise you'll end up blending the indices. I think this is probably my biggest put off for attempting to use this idea (even if in practice it may not be as bad as I'm making it out to be, since usually you wouldn't be doing some of the really expensive operations 3D games do).

 

In practice I imagine this is one of those "try and see what happens" situations.

 

Depending on the OP's requirements, might it be worth storing the textures in a paletted format (even like PNG?) and then sending them to the GPU as RGB data? It seems very likely that you'd not get much benefit from trying to process the indexed data in real-time on the GPU, but if you're mainly trying to reduce the storage requirements of the game (which seems likely if the OP is using OpenGL ES) then this seems like it might have some value (and require relatively minimal effort to implement)

Share this post


Link to post
Share on other sites

 

For 2D images with a low shade count palettes can easily look better than those compression methods (though this heavily depends on the case). Also there's the issue because of the silhouettes: the compression formats that support translucency either do it by increasing the size or by reducing color precision even more.

 

The problem with the look-up idea is that it breaks texture filtering... This means you have to do it manually by reading the texture multiple times instead of just reading once, because otherwise you'll end up blending the indices. I think this is probably my biggest put off for attempting to use this idea (even if in practice it may not be as bad as I'm making it out to be, since usually you wouldn't be doing some of the really expensive operations 3D games do).

 

In practice I imagine this is one of those "try and see what happens" situations.

 

Depending on the OP's requirements, might it be worth storing the textures in a paletted format (even like PNG?) and then sending them to the GPU as RGB data? It seems very likely that you'd not get much benefit from trying to process the indexed data in real-time on the GPU, but if you're mainly trying to reduce the storage requirements of the game (which seems likely if the OP is using OpenGL ES) then this seems like it might have some value (and require relatively minimal effort to implement)

 

 

It really depends on the type of game and art assets. 8-bit color textures is likely only going to look acceptable in a 8/16-bit era style game, and of course, if your game is 8/16-bit styled, then why would you have so much texture data to begin with that it would warrant the need for so many space saving measures? Chances are something like JPEG compression would give you better disk space utilization and better looking textures in most cases. If you really wanted a crisp looking retro style game, you could look into something like SVG, which would be way better than 8-bit PNG. smile.png

Share this post


Link to post
Share on other sites

It wouldn't be faster because it requires a dependent texture read.  You mentioned GL ES so here's Apple's advice (source: http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/BestPracticesforShaders/BestPracticesforShaders.html#//apple_ref/doc/uid/TP40008793-CH7-SW3):

 

 

Dynamic texture lookups, also known as dependent texture reads, occur when a fragment shader computes texture coordinates rather than using the unmodified texture coordinates passed into the shader. Although the shader language supports this, dependent texture reads can delay loading of texel data, reducing performance. When a shader has no dependent texture reads, the graphics hardware may prefetch texel data before the shader executes, hiding some of the latency of accessing memory.

 

Memory savings are moot - are you really that short on video memory anyway?  Maybe on mobile devices (I note that you mentioned GL ES) but there are other strategies to reduce video memory usage - compression or just flat-out reducing your texture sizes (who needs a 1024x1024 texture for a mobile device?  Maybe in a few years time - by which time one may expect that video memory sizes will have increased too - but not today).

 

Texture upload performance could go either way.  A standard texture change doesn't upload new data from GPU to CPU; it should be all resident on the GPU to begin with (for devices with shared/unified memory it won't matter one bit).  For dynamic texture uploads, the required synchronization is going to kill you far more than the amount of data used.

 

On today's hardware texture reads are expensive operations - that's why you want to keep them to a minimum.  And one's natural inclination to save memory must be kept in check because doing so can often be at the expense of performance, and often in quite unintuitive ways - the rules that you'd apply to CPUs just don't always work well on GPUs.

Share this post


Link to post
Share on other sites


That said, I think palette-swap animation is probably not what anyone wants. It's certainly cool in 8/16 bit games with "extreme" (by today's standards) memory and rendering limitations as well as other idiosyncrasies, but it's not clear that there's any incentive to do it today.

Try storing separate textures when the colors can change in completely arbitrary ways. You *will* end up eating memory like crazy. I suppose you could preprocess them before sending the data to the GPU, but in that case the source image is still paletted.

 


Depending on the OP's requirements, might it be worth storing the textures in a paletted format (even like PNG?) and then sending them to the GPU as RGB data?

For the record, I do this in my game, though it's a software renderer in my case (long story). Files are 8-bit PNGs, but they're kept as 32-bit ARGB images in RAM, because the latter are much faster to process since modern hardware completely ditched any support for lower spec formats.

 

Yes, I'm aware that 32-bit PNGs could have been just as good (in fact, GIF tends to beat PNG at 8-bit). A good reason to keep PNGs as 8-bit though is that they retain the palette which I can later use in the image editor with little trouble. It's easier to just read the palette from the image directly than having to load it separately, and if for some reason I decide to change colors en-masse, I can do it without any effort by just swapping the palettes of the PNG files.

 

None of that has to do with doing this GPU-side, though...

 


On today's hardware texture reads are expensive operations - that's why you want to keep them to a minimum.  And one's natural inclination to save memory must be kept in check because doing so can often be at the expense of performance, and often in quite unintuitive ways - the rules that you'd apply to CPUs just don't always work well on GPUs.

GPUs still have texture cache, and that cache still has the whole cache hit and miss issues.

 

But yeah, that actually brings in another detail: swizzling. Drivers will not load textures as-is, but swizzled, to optimize the order in which the pixels are stored to match better how they're going to be accessed by the shaders. However, this only works when using texture coordinates as-is (or with simple transformations), random accesses completely destroy this optimization and accessing consecutive texels in only one direction can actually be slower due to the swizzling.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement