renderer: support allocation of compressed 2D textures

Submitted by ramin.azarmehr@gmail.com on June 15, 2018, 10:03 p.m.

Details

Message ID 20180615220309.17416-1-ramin.azarmehr@gmail.com
State New
Headers show
Series "renderer: support allocation of compressed 2D textures" ( rev: 1 ) in Virgil 3D

Not browsing as part of any series.

Commit Message

ramin.azarmehr@gmail.com June 15, 2018, 10:03 p.m.
From: Ramin Azarmehr <ramin.azarmehr@gmail.com>

The resource_allocate_texture() shall allow allocation of 2D compressed textures.
Example of use-case: the Google Earth app on Android allocates texture tiles with "dxt1_rgb" format to save memory (if S3TC is available).
---
 src/vrend_renderer.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/vrend_renderer.c b/src/vrend_renderer.c
index 85b68c1..a561f6e 100644
--- a/src/vrend_renderer.c
+++ b/src/vrend_renderer.c
@@ -4810,6 +4810,7 @@  static int vrend_renderer_resource_allocate_texture(struct vrend_resource *gr,
 {
    uint level;
    GLenum internalformat, glformat, gltype;
+   bool compressed;
    struct vrend_texture *gt = (struct vrend_texture *)gr;
    struct pipe_resource *pr = &gr->base;
    assert(pr->width0 > 0);
@@ -4841,6 +4842,7 @@  static int vrend_renderer_resource_allocate_texture(struct vrend_resource *gr,
    internalformat = tex_conv_table[pr->format].internalformat;
    glformat = tex_conv_table[pr->format].glformat;
    gltype = tex_conv_table[pr->format].gltype;
+   compressed = util_format_is_compressed(pr->format);
 
    if (internalformat == 0) {
       fprintf(stderr,"unknown format is %d\n", pr->format);
@@ -4931,9 +4933,17 @@  static int vrend_renderer_resource_allocate_texture(struct vrend_resource *gr,
          for (level = 0; level <= pr->last_level; level++) {
             unsigned mwidth = u_minify(pr->width0, level);
             unsigned mheight = u_minify(pr->height0, level);
-            glTexImage2D(gr->target, level, internalformat, mwidth,
-                         gr->target == GL_TEXTURE_1D_ARRAY ? pr->array_size : mheight,
-                         0, glformat, gltype, NULL);
+            unsigned comp_size;
+
+            if (compressed) {
+               comp_size = util_format_get_nblocks(pr->format, mwidth, mheight) *
+                           util_format_get_blocksize(pr->format);
+               glCompressedTexImage2D(gr->target, level, internalformat, mwidth, mheight,
+                                      0, comp_size, NULL);
+            } else {
+               glTexImage2D(gr->target, level, internalformat, mwidth,
+                            gr->target == GL_TEXTURE_1D_ARRAY ? pr->array_size : mheight,
+                            0, glformat, gltype, NULL);
          }
       }
    }