Texture compression size comparison study

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
tonma
DCEmu Freak
DCEmu Freak
Posts: 82
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Thu Mar 10, 2016 7:14 am
Has thanked: 0
Been thanked: 1 time

Re: Texture compression size comparison study

Post by tonma »

I'm trying to load texture made with texconv without success :
Image

Code: Select all

#include <kos.h>
#include <png/png.h>

/* textures */
pvr_ptr_t back_tex;
pvr_ptr_t front_tex;


/* init background */
void back_init() {
    back_tex = pvr_mem_malloc(512 * 512 * 2);
    png_to_texture("/rd/back1.png", back_tex, PNG_NO_ALPHA);

 	front_tex = pvr_mem_malloc(512 * 512 * 2);
   pvr_txr_load_ex("/rd/background_tex.tex", front_tex, 512, 512, PVR_TXRLOAD_16BPP);
}

/* draw background png */

void draw_back(void) {
    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    pvr_vertex_t vert;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, 512, 512, back_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 1;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 1;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = 480;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 480;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
}

/* draw a texture opaque */

void draw_back2(void) {
    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;

    pvr_vertex_t vert;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, 512, 512, front_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 256;
    vert.z = 10;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 256;
    vert.z = 10;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = 480;
    vert.z = 10;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 480;
    vert.z = 10;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
    
}



/* draw one frame */
void draw_frame(void) {
    pvr_wait_ready();
    pvr_scene_begin();

    pvr_list_begin(PVR_LIST_OP_POLY);

    draw_back();
    draw_back2();

    pvr_list_finish();

    pvr_list_begin(PVR_LIST_TR_POLY);

    pvr_list_finish();
    pvr_scene_finish();

}

/* romdisk */
extern uint8 romdisk_boot[];
KOS_INIT_ROMDISK(romdisk_boot);

int main(void) {
    int done = 0;

    /* init kos  */
    pvr_init_defaults();

    

    /* init background */
    back_init();

   

    /* keep drawing frames until start is pressed */
    while(!done) {

        draw_frame();
    }

    return 0;
}

User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Texture compression size comparison study

Post by BlueCrab »

A PNG of pixel art (especially one with all that space at the same color) will compress extremely well, so I personally don't find it weird that it would be smaller in size than the VQ texture.

And, you're correct that using the PNG, you're going to end up with roughly the TGA's size in video memory when you upload the texture -- whereas with the VQ texture, you get pretty much exactly the compressed (10 or 18kb) size in video ram.

You can use whatever texture formats you want when you initialize the video with RGB565 as you're doing in video_set_mode() -- that just sets what the rendering format of the hardware will be. That said, what you're doing there is already done by KOS as the default (if you don't do a video_set_mode, you'll get 640x480 with RGB565 automatically).

As for the palette formats, I've actually never used them myself. The general gist of how it works is that you have to set up the palette format with pvr_set_pal_fmt() and then fill in the entries with pvr_set_pal_entry(). Then when you set up your texture format, you need to set the appropriate paletted texture bits (PVR_TXRFMT_PAL4BPP or PVR_TXRFMT_PAL8BPP) and select which block of entries in the palette you're going to use with PVR_TXRFMT_PAL4BPP_PAL() or PVR_TXRFMT_PAL8BPP_PAL(). You also have to make sure that your textures are twiddled, but if you're using the output of the texture converter, I'd assume that's probably done for you?
tonma
DCEmu Freak
DCEmu Freak
Posts: 82
Joined: Thu Mar 10, 2016 7:14 am
Has thanked: 0
Been thanked: 1 time

Re: Texture compression size comparison study

Post by tonma »

Thanks for answer me.

I'll begin with vqenc rather than texconv. Texconv compress more but I don't think pvr can read directly from texconv.

I use vqenc -k -t image.name. All I have to do now is finding a way to draw kmg at the screen. :mrgreen: I'll look at palette after if I want to make some effects.

In this case the kmg file size from vqenc is 66ko. I can load a few image into the vram (nearly 60 files).

For my memory allocation, I always do (size pixel*size pixel*2) for 16bit. It does not play on the file size in vram ?
back_tex = pvr_mem_malloc(512 * 512 * 2);

Can I see the vram size occupation / free after loading my assets files ?

With that I'll know the size in memory : txr2 = pvr_mem_malloc(img.byte_count);
I use a sprintf for knowing the size and I have the same value than 512*512*2. So 524288 octet, is that size I use in memory (instead of 66ko) ?

I try to load kmg image without success :

Code: Select all

if(kmg_to_img("/rd/fruit.kmg", &img)) {
        printf("Failed to load /rd/back1.kmg\n");
        exit(1);
    }

    w2 = img.w;
    h2 = img.h;
    fmt2 = PVR_TXRFMT_RGB565 | PVR_TXRFMT_VQ_ENABLE | PVR_TXRFMT_TWIDDLED;
    txr2 = pvr_mem_malloc(img.byte_count);
    pvr_txr_load_kimg(&img, txr2, 0);
    kos_img_free(&img, 0);

//Draw Image
pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    pvr_vertex_t vert;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, 512, 512, back_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 1;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 1;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = 480;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 640;
    vert.y = 480;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
And the result :
Image
tonma
DCEmu Freak
DCEmu Freak
Posts: 82
Joined: Thu Mar 10, 2016 7:14 am
Has thanked: 0
Been thanked: 1 time

Re: Texture compression size comparison study

Post by tonma »

Please, help me.

Can anybody make me a .kmg file from this png to test.
Image

As you can see.
On the left OpenGL version of .vq file (vqenc -t -v fruit10.png)
On the right, pvr version with .kmg file (vqenc -t -k -v fruit10.png)
Image

I don't know why I have dirty texture with the kmg file version. Does I need do something special to convert png to kmg.
And before asking, I've try with a kmg file found on web. And it works, so I think the problem is with my conversion method.

I read the new wiki. It's great to have information in one place.
It's writing : For the VQ compressed RGB565 texture, No decompression is required. Just for sure, kmg is a VQ texture ?
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Texture compression size comparison study

Post by bogglez »

I wrote a quick PVR kmg texture loading example. Works for me.
Attachments
2016-03-16-150651_644x583_scrot.png
pvr_texture_example.7z
(78.12 KiB) Downloaded 90 times
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
tonma
DCEmu Freak
DCEmu Freak
Posts: 82
Joined: Thu Mar 10, 2016 7:14 am
Has thanked: 0
Been thanked: 1 time

Re: Texture compression size comparison study

Post by tonma »

Thanks very very much. It's wonderful.

I'll continue my dev with loading a full pack of texture to know how much I can upload in DreamCast Memory and draw on screen.
I am the happiest man on earth. :grin:
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Texture compression size comparison study

Post by bogglez »

tonma wrote:Thanks very very much. It's wonderful.

I'll continue my dev with loading a full pack of texture to know how much I can upload in DreamCast Memory and draw on screen.
I am the happiest man on earth. :grin:
I've added this wiki article: http://dcemulation.org/?title=KMG_Textures

If anything is unclear, please feel free to provide feedback in the "wiki feedback" thread. Thanks.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
bbmario
DCEmu Freak
DCEmu Freak
Posts: 88
Joined: Wed Feb 05, 2014 5:58 am
Has thanked: 9 times
Been thanked: 3 times

Re: Texture compression size comparison study

Post by bbmario »

Amazing article! That's very very helpful. THANKS! :mrgreen: :mrgreen:
Post Reply